Annotation of src/usr.bin/rusers/rusers.c, Revision 1.19
1.19 ! millert 1: /* $OpenBSD: rusers.c,v 1.18 2001/11/06 20:51:19 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.19 ! millert 58: static const char rcsid[] = "$OpenBSD: rusers.c,v 1.18 2001/11/06 20:51:19 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:
358: hp = gethostbyname(host);
1.11 pjanzen 359: if (hp == NULL)
360: errx(1, "unknown host \"%s\"", host);
1.1 deraadt 361:
1.18 millert 362: /* Try version 3 first. */
1.14 millert 363: rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_3, "udp");
364: if (rusers_clnt == NULL) {
365: clnt_pcreateerror(__progname);
366: exit(1);
367: }
368:
369: memset(&up3, 0, sizeof(up3));
370: error = clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
371: xdr_utmp_array, &up3, tv);
372: switch (error) {
373: case RPC_SUCCESS:
1.16 millert 374: sin.sin_addr.s_addr = *(int *)hp->h_addr;
375: rusers_reply_3((char *)&up3, &sin);
1.14 millert 376: clnt_destroy(rusers_clnt);
377: return;
378: case RPC_PROGVERSMISMATCH:
379: clnt_destroy(rusers_clnt);
380: break;
381: default:
382: clnt_perror(rusers_clnt, __progname);
383: clnt_destroy(rusers_clnt);
384: exit(1);
385: }
386:
1.18 millert 387: /* Fall back to version 2. */
1.1 deraadt 388: rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
389: if (rusers_clnt == NULL) {
1.11 pjanzen 390: clnt_pcreateerror(__progname);
1.1 deraadt 391: exit(1);
392: }
393:
1.14 millert 394: memset(&up, 0, sizeof(up));
395: error = clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
396: xdr_utmpidlearr, &up, tv);
397: if (error != RPC_SUCCESS) {
1.11 pjanzen 398: clnt_perror(rusers_clnt, __progname);
1.12 deraadt 399: clnt_destroy(rusers_clnt);
1.1 deraadt 400: exit(1);
401: }
1.16 millert 402: sin.sin_addr.s_addr = *(int *)hp->h_addr;
403: rusers_reply((char *)&up, &sin);
1.12 deraadt 404: clnt_destroy(rusers_clnt);
1.1 deraadt 405: }
406:
1.16 millert 407: enum clnt_stat
408: get_reply(int sock, in_port_t port, u_long xid, struct rpc_msg *msgp,
409: struct rmtcallres *resp, bool_t (*callback)())
410: {
411: ssize_t inlen;
412: socklen_t fromlen;
413: struct sockaddr_in raddr;
414: char inbuf[UDPMSGSIZE];
415: XDR xdr;
416:
417: retry:
418: msgp->acpted_rply.ar_verf = _null_auth;
419: msgp->acpted_rply.ar_results.where = (caddr_t)resp;
420: msgp->acpted_rply.ar_results.proc = xdr_rmtcallres;
421:
422: fromlen = sizeof(struct sockaddr);
423: inlen = recvfrom(sock, inbuf, sizeof(inbuf), 0,
424: (struct sockaddr *)&raddr, &fromlen);
425: if (inlen < 0) {
426: if (errno == EINTR)
427: goto retry;
428: return (RPC_CANTRECV);
429: }
430: if (inlen < sizeof(u_int32_t))
431: goto retry;
432:
433: /*
434: * If the reply we got matches our request, decode the
435: * replay and pass it to the callback function.
436: */
437: xdrmem_create(&xdr, inbuf, (u_int)inlen, XDR_DECODE);
438: if (xdr_replymsg(&xdr, msgp)) {
439: if ((msgp->rm_xid == xid) &&
440: (msgp->rm_reply.rp_stat == MSG_ACCEPTED) &&
441: (msgp->acpted_rply.ar_stat == SUCCESS)) {
442: raddr.sin_port = htons(port);
443: (void)(*callback)(resp->results_ptr, &raddr);
444: }
445: }
446: xdr.x_op = XDR_FREE;
447: msgp->acpted_rply.ar_results.proc = xdr_void;
448: (void)xdr_replymsg(&xdr, msgp);
449: (void)(*resp->xdr_results)(&xdr, resp->results_ptr);
450: xdr_destroy(&xdr);
451:
452: return(RPC_SUCCESS);
453: }
454:
455: enum clnt_stat
456: rpc_setup(int *fdp, XDR *xdr, struct rpc_msg *msg, struct rmtcallargs *args,
457: AUTH *unix_auth, char *buf)
458: {
459: int on = 1;
460:
461: if ((*fdp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
462: return(RPC_CANTSEND);
463:
464: if (setsockopt(*fdp, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0)
465: return(RPC_CANTSEND);
466:
467: msg->rm_xid = arc4random();
468: msg->rm_direction = CALL;
469: msg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
470: msg->rm_call.cb_prog = PMAPPROG;
471: msg->rm_call.cb_vers = PMAPVERS;
472: msg->rm_call.cb_proc = PMAPPROC_CALLIT;
473: msg->rm_call.cb_cred = unix_auth->ah_cred;
474: msg->rm_call.cb_verf = unix_auth->ah_verf;
475:
476: xdrmem_create(xdr, buf, MAX_BROADCAST_SIZE, XDR_ENCODE);
477: if (!xdr_callmsg(xdr, msg) || !xdr_rmtcall_args(xdr, args))
478: return(RPC_CANTENCODEARGS);
479:
480: return(RPC_SUCCESS);
481: }
482:
1.1 deraadt 483: void
484: allhosts(void)
485: {
1.16 millert 486: enum clnt_stat stat;
487: struct itimerval timeout;
488: AUTH *unix_auth = authunix_create_default();
489: size_t outlen[2];
490: int sock[2] = { -1, -1 };
491: int i, maxfd, rval;
492: u_long xid[2], port[2];
493: fd_set *fds = NULL;
494: struct sockaddr_in *sin, baddr;
495: struct rmtcallargs args;
496: struct rmtcallres res[2];
497: struct rpc_msg msg[2];
498: struct ifaddrs *ifa, *ifap = NULL;
499: char buf[2][MAX_BROADCAST_SIZE];
1.4 deraadt 500: utmpidlearr up;
1.14 millert 501: utmp_array up3;
1.16 millert 502: XDR xdr;
1.14 millert 503:
1.16 millert 504: if (getifaddrs(&ifap) != 0)
505: err(1, "can't get list of interface addresses");
506:
507: memset(&up, 0, sizeof(up));
1.15 millert 508: memset(&up3, 0, sizeof(up3));
1.16 millert 509: memset(&baddr, 0, sizeof(baddr));
510: memset(&res, 0, sizeof(res));
511: memset(&msg, 0, sizeof(msg));
1.17 millert 512: memset(&timeout, 0, sizeof(timeout));
1.16 millert 513:
514: args.prog = RUSERSPROG;
515: args.vers = RUSERSVERS_IDLE;
516: args.proc = RUSERSPROC_NAMES;
517: args.xdr_args = xdr_void;
518: args.args_ptr = NULL;
519:
520: stat = rpc_setup(&sock[0], &xdr, &msg[0], &args, unix_auth, buf[0]);
521: if (stat != RPC_SUCCESS)
522: goto cleanup;
523: xid[0] = msg[0].rm_xid;
524: outlen[0] = xdr_getpos(&xdr);
525: xdr_destroy(&xdr);
526:
527: args.vers = RUSERSVERS_3;
528: stat = rpc_setup(&sock[1], &xdr, &msg[1], &args, unix_auth, buf[1]);
529: if (stat != RPC_SUCCESS)
530: goto cleanup;
531: xid[1] = msg[1].rm_xid;
532: outlen[1] = xdr_getpos(&xdr);
533: xdr_destroy(&xdr);
534:
535: maxfd = MAX(sock[0], sock[1]) + 1;
536: fds = (fd_set *)calloc(howmany(maxfd, NFDBITS), sizeof(fd_mask));
537: if (fds == NULL)
538: err(1, NULL);
539:
540: baddr.sin_len = sizeof(struct sockaddr_in);
541: baddr.sin_family = AF_INET;
542: baddr.sin_port = htons(PMAPPORT);
543: baddr.sin_addr.s_addr = htonl(INADDR_ANY);
544:
545: res[0].port_ptr = &port[0];
546: res[0].xdr_results = xdr_utmpidlearr;
547: res[0].results_ptr = (caddr_t)&up;
548:
549: res[1].port_ptr = &port[1];
550: res[1].xdr_results = xdr_utmp_array;
551: res[1].results_ptr = (caddr_t)&up3;
552:
553: (void)signal(SIGALRM, alarmclock);
554:
555: /*
556: * We do 6 runs through the loop. On even runs we send
557: * a version 3 broadcast. On odd ones we send a version 2
558: * broadcast. This should give version 3 replies enough
559: * of an 'edge' over the old version 2 ones in most cases.
560: * We select() waiting for replies for 5 seconds in between
561: * each broadcast.
562: */
563: for (i = 0; i < 6; i++) {
564: for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
565: if (ifa->ifa_addr->sa_family != AF_INET ||
566: !(ifa->ifa_flags & IFF_BROADCAST) ||
567: !(ifa->ifa_flags & IFF_UP) ||
568: ifa->ifa_broadaddr == NULL ||
569: ifa->ifa_broadaddr->sa_family != AF_INET)
570: continue;
571: sin = (struct sockaddr_in *)ifa->ifa_broadaddr;
572: baddr.sin_addr = sin->sin_addr;
573:
574: /* use protocol 2 or 3 depending on i (odd or even) */
575: if (i & 1) {
576: if (sendto(sock[0], buf[0], outlen[0], 0,
577: (struct sockaddr *)&baddr,
578: sizeof(struct sockaddr)) != outlen[0])
579: err(1, "can't send broadcast packet");
580: } else {
581: if (sendto(sock[1], buf[1], outlen[1], 0,
582: (struct sockaddr *)&baddr,
583: sizeof(struct sockaddr)) != outlen[1])
584: err(1, "can't send broadcast packet");
585: }
586: }
1.14 millert 587:
1.16 millert 588: /*
589: * We stay in the select loop for ~5 seconds
590: */
591: timeout.it_value.tv_sec = 5;
592: timeout.it_value.tv_usec = 0;
1.18 millert 593: while (timerisset(&timeout.it_value)) {
1.16 millert 594: FD_SET(sock[0], fds);
595: FD_SET(sock[1], fds);
596: setitimer(ITIMER_REAL, &timeout, NULL);
597: rval = select(maxfd, fds, NULL, NULL, NULL);
598: setitimer(ITIMER_REAL, NULL, &timeout);
599: if (rval == -1) {
600: if (errno == EINTR)
601: break;
602: err(1, "select"); /* shouldn't happen */
603: }
604: if (FD_ISSET(sock[1], fds)) {
605: stat = get_reply(sock[1], (in_port_t)port[1],
606: xid[1], &msg[1], &res[1], rusers_reply_3);
607: if (stat != RPC_SUCCESS)
608: goto cleanup;
609: }
610: if (FD_ISSET(sock[0], fds)) {
611: stat = get_reply(sock[0], (in_port_t)port[0],
612: xid[0], &msg[0], &res[0], rusers_reply);
613: if (stat != RPC_SUCCESS)
614: goto cleanup;
615: }
616: }
617: }
618: cleanup:
619: if (ifap != NULL)
620: freeifaddrs(ifap);
621: if (fds != NULL)
622: free(fds);
623: if (sock[0] >= 0)
624: (void)close(sock[0]);
625: if (sock[1] >= 0)
626: (void)close(sock[1]);
627: AUTH_DESTROY(unix_auth);
628: if (stat != RPC_SUCCESS) {
629: clnt_perrno(stat);
630: exit(1);
631: }
632: }
633:
634: void
1.18 millert 635: print_entry(struct host_info *entry)
636: {
637: char date[32], idle_time[64];
638: char remote[RUSERS_MAXHOSTLEN + 3];
639: struct rusers_utmp *ut;
640: int i, len;
641:
642: if (!lflag)
643: printf("%-*.*s ", HOST_WIDTH, HOST_WIDTH, entry->host);
644:
645: for (i = 0, ut = entry->users; i < entry->count; i++, ut++) {
646: if (lflag) {
647: strftime(date, sizeof(date), "%h %d %R",
648: localtime((time_t *)&ut->ut_time));
649: date[sizeof(date) - 1] = '\0';
650: fmt_idle(ut->ut_idle, idle_time, sizeof(idle_time));
651: len = termwidth -
652: (MAX(strlen(ut->ut_user), NAME_WIDTH) + 1 +
653: HOST_WIDTH + 1 + LINE_WIDTH + 1 + strlen(date) +
654: 1 + MAX(8, strlen(idle_time)) + 1 + 2);
655: if (len > 0 && ut->ut_host[0] != '\0')
656: snprintf(remote, sizeof(remote), "(%.*s)",
657: MIN(len, RUSERS_MAXHOSTLEN), ut->ut_host);
658: else
659: remote[0] = '\0';
660: len = HOST_WIDTH - MIN(HOST_WIDTH, strlen(entry->host)) +
661: LINE_WIDTH - MIN(LINE_WIDTH, strlen(ut->ut_line));
662: printf("%-*s %.*s:%.*s%-*s %-12s %8s %s\n",
663: NAME_WIDTH, ut->ut_user, HOST_WIDTH, entry->host,
664: LINE_WIDTH, ut->ut_line, len, "", date,
665: idle_time, remote);
666: } else {
667: fputs(ut->ut_user, stdout);
668: putchar(' ');
669: }
670: }
671: if (!lflag)
672: putchar('\n');
673: }
674:
675: void
1.19 ! millert 676: expandhosts()
! 677: {
! 678: struct host_info *new_hostinfo, *entry;
! 679: u_int count;
! 680: int i, j;
! 681:
! 682: for (i = 0, count = 0; i < nentries; i++)
! 683: count += hostinfo[i].count;
! 684:
! 685: new_hostinfo = (struct host_info *)malloc(sizeof(*entry) * count);
! 686: if (new_hostinfo == NULL)
! 687: err(1, NULL);
! 688: for (i = 0, entry = new_hostinfo; i < nentries; i++) {
! 689: for (j = 0; j < hostinfo[i].count; j++) {
! 690: memcpy(entry, &hostinfo[i], sizeof(*entry));
! 691: entry->users = &hostinfo[i].users[j];
! 692: entry->idle = entry->users->ut_idle;
! 693: entry->count = 1;
! 694: entry++;
! 695: }
! 696: }
! 697: free(hostinfo);
! 698: hostinfo = new_hostinfo;
! 699: nentries = maxentries = count;
! 700: }
! 701:
! 702: void
1.18 millert 703: sorthosts()
704: {
705: int i;
706: int (*compar)(const void *, const void *);
1.19 ! millert 707:
! 708: if (iflag && lflag)
! 709: expandhosts();
1.18 millert 710:
711: if (hflag)
712: compar = hcompare;
713: else if (iflag)
714: compar = icompare;
715: else
716: compar = ucompare;
717: qsort(hostinfo, nentries, sizeof(*hostinfo), compar);
718:
719: for (i = 0; i < nentries; i++)
720: print_entry(&hostinfo[i]);
721: }
722:
723: int
724: hcompare(const void *aa, const void *bb)
725: {
726: const struct host_info *a = (struct host_info *)aa;
727: const struct host_info *b = (struct host_info *)bb;
728: int rval;
729:
730: if ((rval = strcasecmp(a->host, b->host)) != 0)
731: return(rval);
732:
733: if (a->idle < b->idle)
734: return(-1);
735: else if (a->idle > b->idle)
736: return(1);
737:
738: if (a->count > b->count)
739: return(-1);
740: else if (a->count < b->count)
741: return(1);
742:
743: return(0);
744: }
745:
746: int
747: icompare(const void *aa, const void *bb)
748: {
749: const struct host_info *a = (struct host_info *)aa;
750: const struct host_info *b = (struct host_info *)bb;
751:
752: if (a->idle < b->idle)
753: return(-1);
754: else if (a->idle > b->idle)
755: return(1);
756:
757: if (a->count > b->count)
758: return(-1);
759: else if (a->count < b->count)
760: return(1);
761:
762: return(strcasecmp(a->host, b->host));
763: }
764:
765: int
766: ucompare(const void *aa, const void *bb)
767: {
768: const struct host_info *a = (struct host_info *)aa;
769: const struct host_info *b = (struct host_info *)bb;
770:
771: if (a->count > b->count)
772: return(-1);
773: else if (a->count < b->count)
774: return(1);
775:
776: if (a->idle < b->idle)
777: return(-1);
778: else if (a->idle > b->idle)
779: return(1);
780:
781: return(strcasecmp(a->host, b->host));
782: }
783:
784: void
1.16 millert 785: alarmclock(int signo)
786: {
787:
788: ; /* just interupt */
1.1 deraadt 789: }
790:
1.18 millert 791: char *
792: estrndup(const char *src, size_t len)
793: {
794: char *dst, *end;
795:
796: if ((end = memchr(src, '\0', len)) != NULL)
797: len = end - src;
798:
799: if ((dst = malloc(len + 1)) == NULL)
800: err(1, NULL);
801: memcpy(dst, src, len);
802: dst[len] = '\0';
803:
804: return(dst);
805: }
806:
1.14 millert 807: void
808: usage(void)
1.1 deraadt 809: {
810:
1.18 millert 811: fprintf(stderr, "usage: %s [-al] [-h | -i | -u] [hosts ...]\n",
812: __progname);
1.14 millert 813: exit(1);
1.1 deraadt 814: }