Annotation of src/usr.bin/rusers/rusers.c, Revision 1.20
1.20 ! deraadt 1: /* $OpenBSD: rusers.c,v 1.19 2001/11/07 00:02:58 millert Exp $ */
1.3 deraadt 2:
1.14 millert 3: /*
4: * Copyright (c) 2001 Todd C. Miller <Todd.Miller@courtesan.com>
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. The name of the author may not be used to endorse or promote products
16: * derived from this software without specific prior written permission.
17: *
18: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28: */
1.1 deraadt 29: /*-
30: * Copyright (c) 1993 John Brezak
31: * All rights reserved.
1.14 millert 32: *
1.1 deraadt 33: * Redistribution and use in source and binary forms, with or without
34: * modification, are permitted provided that the following conditions
35: * are met:
36: * 1. Redistributions of source code must retain the above copyright
37: * notice, this list of conditions and the following disclaimer.
38: * 2. Redistributions in binary form must reproduce the above copyright
39: * notice, this list of conditions and the following disclaimer in the
40: * documentation and/or other materials provided with the distribution.
41: * 3. The name of the author may not be used to endorse or promote products
42: * derived from this software without specific prior written permission.
1.14 millert 43: *
1.1 deraadt 44: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
45: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
46: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47: * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
48: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
49: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
53: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54: * POSSIBILITY OF SUCH DAMAGE.
55: */
56:
57: #ifndef lint
1.20 ! deraadt 58: static const char rcsid[] = "$OpenBSD: rusers.c,v 1.19 2001/11/07 00:02:58 millert Exp $";
1.1 deraadt 59: #endif /* not lint */
60:
61: #include <sys/param.h>
1.14 millert 62: #include <sys/ioctl.h>
1.1 deraadt 63: #include <sys/socket.h>
1.14 millert 64: #include <rpc/rpc.h>
1.16 millert 65: #include <rpc/pmap_prot.h>
66: #include <rpc/pmap_rmt.h>
1.14 millert 67: #include <rpcsvc/rusers.h>
68: #include <rpcsvc/rnusers.h> /* Old protocol version */
1.16 millert 69: #include <arpa/inet.h>
70: #include <net/if.h>
1.14 millert 71: #include <err.h>
1.16 millert 72: #include <errno.h>
73: #include <ifaddrs.h>
1.1 deraadt 74: #include <netdb.h>
75: #include <stdio.h>
1.14 millert 76: #include <stdlib.h>
1.8 millert 77: #include <string.h>
1.14 millert 78: #include <termios.h>
79: #include <unistd.h>
1.1 deraadt 80:
1.11 pjanzen 81: /* Preferred formatting */
1.14 millert 82: #define HOST_WIDTH 17
1.1 deraadt 83: #define LINE_WIDTH 8
1.11 pjanzen 84: #define NAME_WIDTH 8
1.1 deraadt 85:
1.18 millert 86: #define MAX_BROADCAST_SIZE 1400
87:
88: struct host_info {
89: u_int count;
90: u_int idle;
91: char *host;
92: rusers_utmp *users;
93: } *hostinfo;
94:
95: void print_entry(struct host_info *);
1.14 millert 96: void fmt_idle(int, char *, size_t);
97: void onehost(char *);
98: void allhosts(void);
1.18 millert 99: void sorthosts(void);
1.19 millert 100: void expandhosts(void);
1.16 millert 101: void alarmclock(int);
1.18 millert 102: char *estrndup(const char *, size_t);
103: struct host_info *add_host(char *);
104: int hcompare(const void *, const void *);
105: int icompare(const void *, const void *);
106: int ucompare(const void *, const void *);
1.14 millert 107: bool_t rusers_reply(char *, struct sockaddr_in *);
108: bool_t rusers_reply_3(char *, struct sockaddr_in *);
1.16 millert 109: enum clnt_stat get_reply(int, in_port_t, u_long, struct rpc_msg *,
110: struct rmtcallres *, bool_t (*)());
1.14 millert 111: __dead void usage(void);
112:
1.18 millert 113: int aflag, hflag, iflag, lflag, uflag;
114: u_int nentries, maxentries;
1.14 millert 115: long termwidth;
116: extern char *__progname;
1.1 deraadt 117:
1.14 millert 118: int
119: main(int argc, char **argv)
120: {
121: struct winsize win;
122: char *cp, *ep;
123: int ch;
124:
1.18 millert 125: while ((ch = getopt(argc, argv, "ahilu")) != -1)
1.14 millert 126: switch (ch) {
127: case 'a':
1.18 millert 128: aflag = 1;
129: break;
130: case 'h':
131: hflag = 1;
132: break;
133: case 'i':
134: iflag = 1;
1.14 millert 135: break;
136: case 'l':
1.18 millert 137: lflag = 1;
138: break;
139: case 'u':
140: uflag = 1;
1.14 millert 141: break;
142: default:
143: usage();
144: /*NOTREACHED*/
145: }
146:
1.18 millert 147: if (hflag + iflag + uflag > 1)
148: usage();
149:
1.14 millert 150: if (isatty(STDOUT_FILENO)) {
151: if ((cp = getenv("COLUMNS")) != NULL && *cp != '\0') {
152: termwidth = strtol(cp, &ep, 10);
153: if (*ep != '\0' || termwidth >= INT_MAX ||
154: termwidth < 0)
155: termwidth = 0;
156: }
157: if (termwidth == 0 &&
158: ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
159: win.ws_col > 0)
160: termwidth = win.ws_col;
161: else
162: termwidth = 80;
163: }
164: setlinebuf(stdout);
1.18 millert 165:
166: if (argc == optind) {
167: if (hflag || iflag || uflag) {
168: puts("Collecting responses...");
169: allhosts();
170: sorthosts();
171: } else
172: allhosts();
173: } else {
1.14 millert 174: for (; optind < argc; optind++)
175: (void) onehost(argv[optind]);
176: }
1.18 millert 177:
1.14 millert 178: exit(0);
179: }
1.11 pjanzen 180:
1.18 millert 181: struct host_info *
182: add_host(char *host)
1.1 deraadt 183: {
1.18 millert 184: int i;
1.1 deraadt 185:
1.18 millert 186: for (i = 0; i < nentries; i++) {
187: /* Existing entry. */
188: if (strcmp(host, hostinfo[i].host) == 0)
189: return(NULL);
1.1 deraadt 190: }
191:
1.18 millert 192: /* New entry, allocate space if needed and store. */
193: if (nentries == maxentries) {
194: maxentries += 128;
195: hostinfo = realloc(hostinfo,
196: sizeof(*hostinfo) * maxentries);
197: if (hostinfo == NULL)
1.14 millert 198: err(1, NULL);
199: }
1.18 millert 200: if ((hostinfo[nentries].host = strdup(host)) == NULL)
201: err(1, NULL);
202: return(&hostinfo[nentries++]);
1.14 millert 203: }
204:
205: void
206: fmt_idle(int idle, char *idle_time, size_t idle_time_len)
207: {
208: int days, hours, minutes, seconds;
209:
210: switch (idle) {
211: case 0:
212: *idle_time = '\0';
213: break;
214: case INT_MAX:
215: strlcpy(idle_time, "??", idle_time_len);
216: break;
217: default:
218: seconds = idle;
219: days = seconds / (60*60*24);
220: seconds %= (60*60*24);
221: hours = seconds / (60*60);
222: seconds %= (60*60);
223: minutes = seconds / 60;
224: seconds %= 60;
225: if (idle >= (24*60*60))
226: snprintf(idle_time, idle_time_len,
227: "%d days, %d:%02d:%02d",
228: days, hours, minutes, seconds);
229: else if (idle >= (60*60))
230: snprintf(idle_time, idle_time_len, "%2d:%02d:%02d",
231: hours, minutes, seconds);
232: else if (idle > 60)
233: snprintf(idle_time, idle_time_len, "%2d:%02d",
234: minutes, seconds);
235: else
236: snprintf(idle_time, idle_time_len, " :%02d", idle);
237: break;
238: }
239: }
240:
1.15 millert 241: bool_t
1.1 deraadt 242: rusers_reply(char *replyp, struct sockaddr_in *raddrp)
243: {
1.14 millert 244: utmpidlearr *up = (utmpidlearr *)replyp;
1.18 millert 245: struct host_info *entry;
1.1 deraadt 246: struct hostent *hp;
1.18 millert 247: rusers_utmp *ut;
248: char *host;
1.14 millert 249: int i;
1.1 deraadt 250:
1.18 millert 251: if (!aflag && up->uia_cnt == 0)
1.1 deraadt 252: return(0);
253:
1.15 millert 254: hp = gethostbyaddr((char *)&raddrp->sin_addr,
1.14 millert 255: sizeof(struct in_addr), AF_INET);
1.18 millert 256: if (hp)
1.1 deraadt 257: host = hp->h_name;
1.18 millert 258: else
1.1 deraadt 259: host = inet_ntoa(raddrp->sin_addr);
1.18 millert 260: if ((entry = add_host(host)) == NULL)
261: return(0);
262:
263: if ((ut = malloc(up->uia_cnt * sizeof(*ut))) == NULL)
264: err(1, NULL);
265: entry->users = ut;
266: entry->count = up->uia_cnt;
267: entry->idle = UINT_MAX;
268: for (i = 0; i < up->uia_cnt; i++, ut++) {
269: ut->ut_user = estrndup(up->uia_arr[i]->ui_utmp.ut_name,
270: RNUSERS_MAXUSERLEN);
271: ut->ut_line = estrndup(up->uia_arr[i]->ui_utmp.ut_line,
272: RNUSERS_MAXLINELEN);
273: ut->ut_host = estrndup(up->uia_arr[i]->ui_utmp.ut_host,
274: RNUSERS_MAXHOSTLEN);
275: ut->ut_time = up->uia_arr[i]->ui_utmp.ut_time;
276: ut->ut_idle = up->uia_arr[i]->ui_idle;
277: if (ut->ut_idle < entry->idle)
278: entry->idle = ut->ut_idle;
279: }
280:
281: if (!hflag && !iflag && !uflag) {
282: print_entry(entry);
283: for (i = 0, ut = entry->users; i < entry->count; i++, ut++) {
284: free(ut->ut_user);
285: free(ut->ut_line);
286: free(ut->ut_host);
1.14 millert 287: }
1.18 millert 288: free(entry->users);
1.14 millert 289: }
290:
291: return(0);
292: }
1.1 deraadt 293:
1.15 millert 294: bool_t
1.14 millert 295: rusers_reply_3(char *replyp, struct sockaddr_in *raddrp)
296: {
297: utmp_array *up3 = (utmp_array *)replyp;
1.18 millert 298: struct host_info *entry;
1.14 millert 299: struct hostent *hp;
1.18 millert 300: rusers_utmp *ut;
301: char *host;
1.14 millert 302: int i;
303:
1.18 millert 304: if (!aflag && !up3->utmp_array_len)
1.14 millert 305: return(0);
1.16 millert 306:
1.15 millert 307: hp = gethostbyaddr((char *)&raddrp->sin_addr,
1.14 millert 308: sizeof(struct in_addr), AF_INET);
1.18 millert 309: if (hp)
1.14 millert 310: host = hp->h_name;
1.18 millert 311: else
1.14 millert 312: host = inet_ntoa(raddrp->sin_addr);
1.18 millert 313: if ((entry = add_host(host)) == NULL)
314: return(0);
315:
316: if ((ut = malloc(up3->utmp_array_len * sizeof(*ut))) == NULL)
317: err(1, NULL);
318: entry->users = ut;
319: entry->count = up3->utmp_array_len;
320: entry->idle = UINT_MAX;
321: for (i = 0; i < up3->utmp_array_len; i++, ut++) {
322: ut->ut_user = estrndup(up3->utmp_array_val[i].ut_user,
323: RUSERS_MAXUSERLEN);
324: ut->ut_line = estrndup(up3->utmp_array_val[i].ut_line,
325: RUSERS_MAXLINELEN);
326: ut->ut_host = estrndup(up3->utmp_array_val[i].ut_host,
327: RUSERS_MAXHOSTLEN);
328: ut->ut_time = up3->utmp_array_val[i].ut_time;
329: ut->ut_idle = up3->utmp_array_val[i].ut_idle;
330: if (ut->ut_idle < entry->idle)
331: entry->idle = ut->ut_idle;
332: }
333:
334: if (!hflag && !iflag && !uflag) {
335: print_entry(entry);
336: for (i = 0, ut = entry->users; i < entry->count; i++, ut++) {
337: free(ut->ut_user);
338: free(ut->ut_line);
339: free(ut->ut_host);
1.14 millert 340: }
1.18 millert 341: free(entry->users);
1.1 deraadt 342: }
343:
344: return(0);
345: }
346:
347: void
348: onehost(char *host)
349: {
1.4 deraadt 350: utmpidlearr up;
1.14 millert 351: utmp_array up3;
1.1 deraadt 352: CLIENT *rusers_clnt;
1.16 millert 353: struct sockaddr_in sin;
1.1 deraadt 354: struct hostent *hp;
1.14 millert 355: struct timeval tv = { 25, 0 };
356: int error;
1.1 deraadt 357:
1.20 ! deraadt 358: memset(&sin, 0, sizeof sin);
! 359:
1.1 deraadt 360: hp = gethostbyname(host);
1.11 pjanzen 361: if (hp == NULL)
362: errx(1, "unknown host \"%s\"", host);
1.1 deraadt 363:
1.18 millert 364: /* Try version 3 first. */
1.14 millert 365: rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_3, "udp");
366: if (rusers_clnt == NULL) {
367: clnt_pcreateerror(__progname);
368: exit(1);
369: }
370:
371: memset(&up3, 0, sizeof(up3));
372: error = clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
373: xdr_utmp_array, &up3, tv);
374: switch (error) {
375: case RPC_SUCCESS:
1.16 millert 376: sin.sin_addr.s_addr = *(int *)hp->h_addr;
377: rusers_reply_3((char *)&up3, &sin);
1.14 millert 378: clnt_destroy(rusers_clnt);
379: return;
380: case RPC_PROGVERSMISMATCH:
381: clnt_destroy(rusers_clnt);
382: break;
383: default:
384: clnt_perror(rusers_clnt, __progname);
385: clnt_destroy(rusers_clnt);
386: exit(1);
387: }
388:
1.18 millert 389: /* Fall back to version 2. */
1.1 deraadt 390: rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
391: if (rusers_clnt == NULL) {
1.11 pjanzen 392: clnt_pcreateerror(__progname);
1.1 deraadt 393: exit(1);
394: }
395:
1.14 millert 396: memset(&up, 0, sizeof(up));
397: error = clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
398: xdr_utmpidlearr, &up, tv);
399: if (error != RPC_SUCCESS) {
1.11 pjanzen 400: clnt_perror(rusers_clnt, __progname);
1.12 deraadt 401: clnt_destroy(rusers_clnt);
1.1 deraadt 402: exit(1);
403: }
1.16 millert 404: sin.sin_addr.s_addr = *(int *)hp->h_addr;
405: rusers_reply((char *)&up, &sin);
1.12 deraadt 406: clnt_destroy(rusers_clnt);
1.1 deraadt 407: }
408:
1.16 millert 409: enum clnt_stat
410: get_reply(int sock, in_port_t port, u_long xid, struct rpc_msg *msgp,
411: struct rmtcallres *resp, bool_t (*callback)())
412: {
413: ssize_t inlen;
414: socklen_t fromlen;
415: struct sockaddr_in raddr;
416: char inbuf[UDPMSGSIZE];
417: XDR xdr;
418:
419: retry:
420: msgp->acpted_rply.ar_verf = _null_auth;
421: msgp->acpted_rply.ar_results.where = (caddr_t)resp;
422: msgp->acpted_rply.ar_results.proc = xdr_rmtcallres;
423:
424: fromlen = sizeof(struct sockaddr);
425: inlen = recvfrom(sock, inbuf, sizeof(inbuf), 0,
426: (struct sockaddr *)&raddr, &fromlen);
427: if (inlen < 0) {
428: if (errno == EINTR)
429: goto retry;
430: return (RPC_CANTRECV);
431: }
432: if (inlen < sizeof(u_int32_t))
433: goto retry;
434:
435: /*
436: * If the reply we got matches our request, decode the
437: * replay and pass it to the callback function.
438: */
439: xdrmem_create(&xdr, inbuf, (u_int)inlen, XDR_DECODE);
440: if (xdr_replymsg(&xdr, msgp)) {
441: if ((msgp->rm_xid == xid) &&
442: (msgp->rm_reply.rp_stat == MSG_ACCEPTED) &&
443: (msgp->acpted_rply.ar_stat == SUCCESS)) {
444: raddr.sin_port = htons(port);
445: (void)(*callback)(resp->results_ptr, &raddr);
446: }
447: }
448: xdr.x_op = XDR_FREE;
449: msgp->acpted_rply.ar_results.proc = xdr_void;
450: (void)xdr_replymsg(&xdr, msgp);
451: (void)(*resp->xdr_results)(&xdr, resp->results_ptr);
452: xdr_destroy(&xdr);
453:
454: return(RPC_SUCCESS);
455: }
456:
457: enum clnt_stat
458: rpc_setup(int *fdp, XDR *xdr, struct rpc_msg *msg, struct rmtcallargs *args,
459: AUTH *unix_auth, char *buf)
460: {
461: int on = 1;
462:
463: if ((*fdp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
464: return(RPC_CANTSEND);
465:
466: if (setsockopt(*fdp, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0)
467: return(RPC_CANTSEND);
468:
469: msg->rm_xid = arc4random();
470: msg->rm_direction = CALL;
471: msg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
472: msg->rm_call.cb_prog = PMAPPROG;
473: msg->rm_call.cb_vers = PMAPVERS;
474: msg->rm_call.cb_proc = PMAPPROC_CALLIT;
475: msg->rm_call.cb_cred = unix_auth->ah_cred;
476: msg->rm_call.cb_verf = unix_auth->ah_verf;
477:
478: xdrmem_create(xdr, buf, MAX_BROADCAST_SIZE, XDR_ENCODE);
479: if (!xdr_callmsg(xdr, msg) || !xdr_rmtcall_args(xdr, args))
480: return(RPC_CANTENCODEARGS);
481:
482: return(RPC_SUCCESS);
483: }
484:
1.1 deraadt 485: void
486: allhosts(void)
487: {
1.16 millert 488: enum clnt_stat stat;
489: struct itimerval timeout;
490: AUTH *unix_auth = authunix_create_default();
491: size_t outlen[2];
492: int sock[2] = { -1, -1 };
493: int i, maxfd, rval;
494: u_long xid[2], port[2];
495: fd_set *fds = NULL;
496: struct sockaddr_in *sin, baddr;
497: struct rmtcallargs args;
498: struct rmtcallres res[2];
499: struct rpc_msg msg[2];
500: struct ifaddrs *ifa, *ifap = NULL;
501: char buf[2][MAX_BROADCAST_SIZE];
1.4 deraadt 502: utmpidlearr up;
1.14 millert 503: utmp_array up3;
1.16 millert 504: XDR xdr;
1.14 millert 505:
1.16 millert 506: if (getifaddrs(&ifap) != 0)
507: err(1, "can't get list of interface addresses");
508:
509: memset(&up, 0, sizeof(up));
1.15 millert 510: memset(&up3, 0, sizeof(up3));
1.16 millert 511: memset(&baddr, 0, sizeof(baddr));
512: memset(&res, 0, sizeof(res));
513: memset(&msg, 0, sizeof(msg));
1.17 millert 514: memset(&timeout, 0, sizeof(timeout));
1.16 millert 515:
516: args.prog = RUSERSPROG;
517: args.vers = RUSERSVERS_IDLE;
518: args.proc = RUSERSPROC_NAMES;
519: args.xdr_args = xdr_void;
520: args.args_ptr = NULL;
521:
522: stat = rpc_setup(&sock[0], &xdr, &msg[0], &args, unix_auth, buf[0]);
523: if (stat != RPC_SUCCESS)
524: goto cleanup;
525: xid[0] = msg[0].rm_xid;
526: outlen[0] = xdr_getpos(&xdr);
527: xdr_destroy(&xdr);
528:
529: args.vers = RUSERSVERS_3;
530: stat = rpc_setup(&sock[1], &xdr, &msg[1], &args, unix_auth, buf[1]);
531: if (stat != RPC_SUCCESS)
532: goto cleanup;
533: xid[1] = msg[1].rm_xid;
534: outlen[1] = xdr_getpos(&xdr);
535: xdr_destroy(&xdr);
536:
537: maxfd = MAX(sock[0], sock[1]) + 1;
538: fds = (fd_set *)calloc(howmany(maxfd, NFDBITS), sizeof(fd_mask));
539: if (fds == NULL)
540: err(1, NULL);
541:
542: baddr.sin_len = sizeof(struct sockaddr_in);
543: baddr.sin_family = AF_INET;
544: baddr.sin_port = htons(PMAPPORT);
545: baddr.sin_addr.s_addr = htonl(INADDR_ANY);
546:
547: res[0].port_ptr = &port[0];
548: res[0].xdr_results = xdr_utmpidlearr;
549: res[0].results_ptr = (caddr_t)&up;
550:
551: res[1].port_ptr = &port[1];
552: res[1].xdr_results = xdr_utmp_array;
553: res[1].results_ptr = (caddr_t)&up3;
554:
555: (void)signal(SIGALRM, alarmclock);
556:
557: /*
558: * We do 6 runs through the loop. On even runs we send
559: * a version 3 broadcast. On odd ones we send a version 2
560: * broadcast. This should give version 3 replies enough
561: * of an 'edge' over the old version 2 ones in most cases.
562: * We select() waiting for replies for 5 seconds in between
563: * each broadcast.
564: */
565: for (i = 0; i < 6; i++) {
566: for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
567: if (ifa->ifa_addr->sa_family != AF_INET ||
568: !(ifa->ifa_flags & IFF_BROADCAST) ||
569: !(ifa->ifa_flags & IFF_UP) ||
570: ifa->ifa_broadaddr == NULL ||
571: ifa->ifa_broadaddr->sa_family != AF_INET)
572: continue;
573: sin = (struct sockaddr_in *)ifa->ifa_broadaddr;
574: baddr.sin_addr = sin->sin_addr;
575:
576: /* use protocol 2 or 3 depending on i (odd or even) */
577: if (i & 1) {
578: if (sendto(sock[0], buf[0], outlen[0], 0,
579: (struct sockaddr *)&baddr,
580: sizeof(struct sockaddr)) != outlen[0])
581: err(1, "can't send broadcast packet");
582: } else {
583: if (sendto(sock[1], buf[1], outlen[1], 0,
584: (struct sockaddr *)&baddr,
585: sizeof(struct sockaddr)) != outlen[1])
586: err(1, "can't send broadcast packet");
587: }
588: }
1.14 millert 589:
1.16 millert 590: /*
591: * We stay in the select loop for ~5 seconds
592: */
593: timeout.it_value.tv_sec = 5;
594: timeout.it_value.tv_usec = 0;
1.18 millert 595: while (timerisset(&timeout.it_value)) {
1.16 millert 596: FD_SET(sock[0], fds);
597: FD_SET(sock[1], fds);
598: setitimer(ITIMER_REAL, &timeout, NULL);
599: rval = select(maxfd, fds, NULL, NULL, NULL);
600: setitimer(ITIMER_REAL, NULL, &timeout);
601: if (rval == -1) {
602: if (errno == EINTR)
603: break;
604: err(1, "select"); /* shouldn't happen */
605: }
606: if (FD_ISSET(sock[1], fds)) {
607: stat = get_reply(sock[1], (in_port_t)port[1],
608: xid[1], &msg[1], &res[1], rusers_reply_3);
609: if (stat != RPC_SUCCESS)
610: goto cleanup;
611: }
612: if (FD_ISSET(sock[0], fds)) {
613: stat = get_reply(sock[0], (in_port_t)port[0],
614: xid[0], &msg[0], &res[0], rusers_reply);
615: if (stat != RPC_SUCCESS)
616: goto cleanup;
617: }
618: }
619: }
620: cleanup:
621: if (ifap != NULL)
622: freeifaddrs(ifap);
623: if (fds != NULL)
624: free(fds);
625: if (sock[0] >= 0)
626: (void)close(sock[0]);
627: if (sock[1] >= 0)
628: (void)close(sock[1]);
629: AUTH_DESTROY(unix_auth);
630: if (stat != RPC_SUCCESS) {
631: clnt_perrno(stat);
632: exit(1);
633: }
634: }
635:
636: void
1.18 millert 637: print_entry(struct host_info *entry)
638: {
639: char date[32], idle_time[64];
640: char remote[RUSERS_MAXHOSTLEN + 3];
641: struct rusers_utmp *ut;
642: int i, len;
643:
644: if (!lflag)
645: printf("%-*.*s ", HOST_WIDTH, HOST_WIDTH, entry->host);
646:
647: for (i = 0, ut = entry->users; i < entry->count; i++, ut++) {
648: if (lflag) {
649: strftime(date, sizeof(date), "%h %d %R",
650: localtime((time_t *)&ut->ut_time));
651: date[sizeof(date) - 1] = '\0';
652: fmt_idle(ut->ut_idle, idle_time, sizeof(idle_time));
653: len = termwidth -
654: (MAX(strlen(ut->ut_user), NAME_WIDTH) + 1 +
655: HOST_WIDTH + 1 + LINE_WIDTH + 1 + strlen(date) +
656: 1 + MAX(8, strlen(idle_time)) + 1 + 2);
657: if (len > 0 && ut->ut_host[0] != '\0')
658: snprintf(remote, sizeof(remote), "(%.*s)",
659: MIN(len, RUSERS_MAXHOSTLEN), ut->ut_host);
660: else
661: remote[0] = '\0';
662: len = HOST_WIDTH - MIN(HOST_WIDTH, strlen(entry->host)) +
663: LINE_WIDTH - MIN(LINE_WIDTH, strlen(ut->ut_line));
664: printf("%-*s %.*s:%.*s%-*s %-12s %8s %s\n",
665: NAME_WIDTH, ut->ut_user, HOST_WIDTH, entry->host,
666: LINE_WIDTH, ut->ut_line, len, "", date,
667: idle_time, remote);
668: } else {
669: fputs(ut->ut_user, stdout);
670: putchar(' ');
671: }
672: }
673: if (!lflag)
674: putchar('\n');
675: }
676:
677: void
1.19 millert 678: expandhosts()
679: {
680: struct host_info *new_hostinfo, *entry;
681: u_int count;
682: int i, j;
683:
684: for (i = 0, count = 0; i < nentries; i++)
685: count += hostinfo[i].count;
686:
687: new_hostinfo = (struct host_info *)malloc(sizeof(*entry) * count);
688: if (new_hostinfo == NULL)
689: err(1, NULL);
690: for (i = 0, entry = new_hostinfo; i < nentries; i++) {
691: for (j = 0; j < hostinfo[i].count; j++) {
692: memcpy(entry, &hostinfo[i], sizeof(*entry));
693: entry->users = &hostinfo[i].users[j];
694: entry->idle = entry->users->ut_idle;
695: entry->count = 1;
696: entry++;
697: }
698: }
699: free(hostinfo);
700: hostinfo = new_hostinfo;
701: nentries = maxentries = count;
702: }
703:
704: void
1.18 millert 705: sorthosts()
706: {
707: int i;
708: int (*compar)(const void *, const void *);
1.19 millert 709:
710: if (iflag && lflag)
711: expandhosts();
1.18 millert 712:
713: if (hflag)
714: compar = hcompare;
715: else if (iflag)
716: compar = icompare;
717: else
718: compar = ucompare;
719: qsort(hostinfo, nentries, sizeof(*hostinfo), compar);
720:
721: for (i = 0; i < nentries; i++)
722: print_entry(&hostinfo[i]);
723: }
724:
725: int
726: hcompare(const void *aa, const void *bb)
727: {
728: const struct host_info *a = (struct host_info *)aa;
729: const struct host_info *b = (struct host_info *)bb;
730: int rval;
731:
732: if ((rval = strcasecmp(a->host, b->host)) != 0)
733: return(rval);
734:
735: if (a->idle < b->idle)
736: return(-1);
737: else if (a->idle > b->idle)
738: return(1);
739:
740: if (a->count > b->count)
741: return(-1);
742: else if (a->count < b->count)
743: return(1);
744:
745: return(0);
746: }
747:
748: int
749: icompare(const void *aa, const void *bb)
750: {
751: const struct host_info *a = (struct host_info *)aa;
752: const struct host_info *b = (struct host_info *)bb;
753:
754: if (a->idle < b->idle)
755: return(-1);
756: else if (a->idle > b->idle)
757: return(1);
758:
759: if (a->count > b->count)
760: return(-1);
761: else if (a->count < b->count)
762: return(1);
763:
764: return(strcasecmp(a->host, b->host));
765: }
766:
767: int
768: ucompare(const void *aa, const void *bb)
769: {
770: const struct host_info *a = (struct host_info *)aa;
771: const struct host_info *b = (struct host_info *)bb;
772:
773: if (a->count > b->count)
774: return(-1);
775: else if (a->count < b->count)
776: return(1);
777:
778: if (a->idle < b->idle)
779: return(-1);
780: else if (a->idle > b->idle)
781: return(1);
782:
783: return(strcasecmp(a->host, b->host));
784: }
785:
786: void
1.16 millert 787: alarmclock(int signo)
788: {
789:
790: ; /* just interupt */
1.1 deraadt 791: }
792:
1.18 millert 793: char *
794: estrndup(const char *src, size_t len)
795: {
796: char *dst, *end;
797:
798: if ((end = memchr(src, '\0', len)) != NULL)
799: len = end - src;
800:
801: if ((dst = malloc(len + 1)) == NULL)
802: err(1, NULL);
803: memcpy(dst, src, len);
804: dst[len] = '\0';
805:
806: return(dst);
807: }
808:
1.14 millert 809: void
810: usage(void)
1.1 deraadt 811: {
812:
1.18 millert 813: fprintf(stderr, "usage: %s [-al] [-h | -i | -u] [hosts ...]\n",
814: __progname);
1.14 millert 815: exit(1);
1.1 deraadt 816: }