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