Debian Codegraph @

rusers/rusers.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*-
2 * Copyright (c) 1993 John Brezak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef lint
30 char rusers_rcsid[] =
31 "$Id: rusers.c,v 1.17 1999/12/12 19:32:05 dholland Exp $";
32 #endif /* not lint */
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <netdb.h>
38 #include <stdio.h>
39 #include <getopt.h>
40 #include <string.h>
41 #include <rpc/rpc.h>
42 #include <rpc/pmap_clnt.h>
43 #include <arpa/inet.h>
44 #include <utmp.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47
48 #include "../version.h"
49
50 /*
51 * For now we only try version 2 of the protocol. The current
52 * version is 3 (rusers.h), but only Solaris and NetBSD seem
53 * to support it currently.
54 */
55 /*#include <rpcsvc/rnusers.h>*/ /* Old version */
56
57 #include "rusers.h" /* get the one we just built with rpcgen */
58
59 /*
60 * Sigh.
61 */
62 #ifdef GNU_LIBC
63 #define RUT_TIME ut_time
64 #else
65 #define RUT_TIME ut_time
66 #endif
67
68 #define MAX_INT 0x7fffffff
69 #define HOST_WIDTH 20
70 #define LINE_WIDTH 8
71 char *argv0;
72
73 struct timeval timeout = { 25, 0 };
74 int longopt;
75 int allopt;
76
77 struct host_list {
78 struct host_list *next;
79 struct in_addr addr;
80 } *hosts;
81
82 static
83 int
84 search_host(struct in_addr addr)
85 {
86 struct host_list *hp;
87
88 if (!hosts)
89 return(0);
90
91 for (hp = hosts; hp != NULL; hp = hp->next) {
92 if (hp->addr.s_addr == addr.s_addr)
93 return(1);
94 }
95 return(0);
96 }
97
98 static
99 void
100 remember_host(struct in_addr addr)
101 {
102 struct host_list *hp;
103
104 if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
105 fprintf(stderr, "%s: no memory.\n", argv0);
106 exit(1);
107 }
108 hp->addr.s_addr = addr.s_addr;
109 hp->next = hosts;
110 hosts = hp;
111 }
112
113 static
114 int
115 rusers_reply(char *replyp, struct sockaddr_in *raddrp)
116 {
117 int x, idlee;
118 char date[32], idle_time[64], remote[64], local[64];
119 struct hostent *hp;
120 struct utmpidlearr *up = (struct utmpidlearr *)replyp;
121 const char *host;
122 int days, hours, minutes, seconds;
123
124 if (search_host(raddrp->sin_addr))
125 return(0);
126
127 if (!allopt && !up->uia_cnt)
128 return(0);
129
130 hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
131 sizeof(struct in_addr), AF_INET);
132 if (hp)
133 host = hp->h_name;
134 else
135 host = inet_ntoa(raddrp->sin_addr);
136
137 if (!longopt)
138 printf("%-*.*s ", HOST_WIDTH, HOST_WIDTH, host);
139
140 for (x = 0; x < up->uia_cnt; x++) {
141 time_t tmptime = up->uia_arr[x]->ui_utmp.RUT_TIME;
142 strncpy(date, ctime(&tmptime) + 4, sizeof(date)-1);
143 date[sizeof(date)-1] = 0;
144
145 idlee = up->uia_arr[x]->ui_idle;
146 snprintf(idle_time, sizeof(idle_time), " :%02d", idlee);
147 if (idlee == MAX_INT)
148 strcpy(idle_time, "??");
149 else if (idlee == 0)
150 strcpy(idle_time, "");
151 else {
152 seconds = idlee;
153 days = seconds/(60*60*24);
154 seconds %= (60*60*24);
155 hours = seconds/(60*60);
156 seconds %= (60*60);
157 minutes = seconds/60;
158 seconds %= 60;
159 if (idlee > 60)
160 snprintf(idle_time, sizeof(idle_time),
161 "%2d:%02d", minutes, seconds);
162 if (idlee >= (60*60))
163 snprintf(idle_time, sizeof(idle_time),
164 "%2d:%02d:%02d",
165 hours, minutes, seconds);
166 if (idlee >= (24*60*60))
167 snprintf(idle_time, sizeof(idle_time),
168 "%d days, %d:%02d:%02d",
169 days, hours, minutes, seconds);
170 }
171
172 strncpy(remote, up->uia_arr[x]->ui_utmp.ut_host,
173 sizeof(remote)-1);
174 remote[sizeof(remote)-1] = 0;
175
176 if (strlen(remote) != 0)
177 snprintf(remote, sizeof(remote), "(%.16s)",
178 up->uia_arr[x]->ui_utmp.ut_host);
179
180 if (longopt) {
181 /* Fit into HOST_WIDTH+LINE_WIDTH+1 chars */
182 int len1 = strlen(host);
183 int len2 = strlen(up->uia_arr[x]->ui_utmp.ut_line);
184 if (len1 + len2 > HOST_WIDTH+LINE_WIDTH+1) {
185 int excess = len1 + len2 - HOST_WIDTH-LINE_WIDTH-1;
186 if (excess < len1) len1 -= excess;
187 else if (excess < len2) len2 -= excess;
188 else {
189 /* Hmm. Probably an attack... */
190 len1 = HOST_WIDTH;
191 len2 = LINE_WIDTH;
192 }
193 }
194 snprintf(local, sizeof(local),
195 "%-.*s:%-.*s", len1, host, len2,
196 up->uia_arr[x]->ui_utmp.ut_line);
197
198 printf("%-8.8s %-*.*s %-12.12s %8s %.18s\n",
199 up->uia_arr[x]->ui_utmp.ut_name,
200 HOST_WIDTH+LINE_WIDTH+1, HOST_WIDTH+LINE_WIDTH+1, local,
201 date,
202 idle_time,
203 remote);
204 } else
205 printf("%.8s ",
206 up->uia_arr[x]->ui_utmp.ut_name);
207 }
208 if (!longopt)
209 putchar('\n');
210
211 remember_host(raddrp->sin_addr);
212 return(0);
213 }
214
215 static
216 void
217 onehost(char *host)
218 {
219 struct utmpidlearr up;
220 CLIENT *rusers_clnt;
221 struct sockaddr_in addr;
222 struct hostent *hp;
223
224 hp = gethostbyname(host);
225 if (hp == NULL) {
226 fprintf(stderr, "%s: unknown host \"%s\"\n",
227 argv0, host);
228 exit(1);
229 }
230
231 rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
232 if (rusers_clnt == NULL) {
233 clnt_pcreateerror(argv0);
234 exit(1);
235 }
236
237 memset(&up, 0, sizeof(up));
238 memset(&addr, 0, sizeof(addr));
239 if (clnt_call(rusers_clnt, RUSERSPROC_NAMES, (xdrproc_t)xdr_void, NULL,
240 (xdrproc_t) xdr_utmpidlearr, &up, timeout) != RPC_SUCCESS) {
241 clnt_perror(rusers_clnt, argv0);
242 exit(1);
243 }
244 if (hp->h_length > (int)sizeof(addr.sin_addr)) {
245 hp->h_length = sizeof(addr.sin_addr);
246 }
247 memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
248 rusers_reply((char *)&up, &addr);
249 }
250
251 static
252 void
253 allhosts(void)
254 {
255 struct utmpidlearr up;
256 enum clnt_stat clnt_stat;
257
258 bzero((char *)&up, sizeof(up));
259 clnt_stat = clnt_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
260 RUSERSPROC_NAMES,
261 (xdrproc_t) xdr_void, NULL,
262 (xdrproc_t) xdr_utmpidlearr,
263 (char *) &up,
264 (resultproc_t) rusers_reply);
265 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
266 fprintf(stderr, "%s: %s\n", argv0, clnt_sperrno(clnt_stat));
267 exit(1);
268 }
269 }
270
271 static
272 void usage(void)
273 {
274 fprintf(stderr, "Usage: %s [-la] [hosts ...]\n", argv0);
275 exit(1);
276 }
277
278 int main(int argc, char *argv[])
279 {
280 int ch;
281
282 if (!(argv0 = rindex(argv[0], '/')))
283 argv0 = argv[0];
284 else
285 argv0++;
286
287
288 while ((ch = getopt(argc, argv, "al")) != -1)
289 switch (ch) {
290 case 'a':
291 allopt++;
292 break;
293 case 'l':
294 longopt++;
295 break;
296 default:
297 usage();
298 /*NOTREACHED*/
299 }
300
301 setlinebuf(stdout);
302 if (argc == optind)
303 allhosts();
304 else {
305 for (; optind < argc; optind++)
306 (void) onehost(argv[optind]);
307 }
308 exit(0);
309 }