Annotation of src/usr.bin/rusers/rusers.c, Revision 1.10
1.10 ! deraadt 1: /* $OpenBSD: rusers.c,v 1.9 2001/01/31 19:48:06 deraadt Exp $ */
1.3 deraadt 2:
1.1 deraadt 3: /*-
4: * Copyright (c) 1993 John Brezak
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 BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
19: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21: * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28: * POSSIBILITY OF SUCH DAMAGE.
29: */
30:
31: #ifndef lint
1.10 ! deraadt 32: static char rcsid[] = "$OpenBSD: rusers.c,v 1.9 2001/01/31 19:48:06 deraadt Exp $";
1.1 deraadt 33: #endif /* not lint */
34:
35: #include <sys/types.h>
36: #include <sys/param.h>
37: #include <sys/socket.h>
38: #include <netdb.h>
39: #include <stdio.h>
1.8 millert 40: #include <string.h>
1.1 deraadt 41: #include <rpc/rpc.h>
1.6 deraadt 42: #include <rpc/pmap_clnt.h>
1.1 deraadt 43: #include <arpa/inet.h>
44: #include <utmp.h>
45: #include <stdlib.h>
46:
47: /*
48: * For now we only try version 2 of the protocol. The current
49: * version is 3 (rusers.h), but only Solaris and NetBSD seem
50: * to support it currently.
51: */
52: #include <rpcsvc/rnusers.h> /* Old version */
53:
54: #define MAX_INT 0x7fffffff
55: #define HOST_WIDTH 20
56: #define LINE_WIDTH 8
57: char *argv0;
58:
59: struct timeval timeout = { 25, 0 };
60: int longopt;
61: int allopt;
62:
63: struct host_list {
64: struct host_list *next;
65: struct in_addr addr;
66: } *hosts;
67:
68: int
69: search_host(struct in_addr addr)
70: {
71: struct host_list *hp;
72:
73: if (!hosts)
74: return(0);
75:
76: for (hp = hosts; hp != NULL; hp = hp->next) {
77: if (hp->addr.s_addr == addr.s_addr)
78: return(1);
79: }
80: return(0);
81: }
82:
83: void
84: remember_host(struct in_addr addr)
85: {
86: struct host_list *hp;
87:
88: if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
89: fprintf(stderr, "%s: no memory.\n", argv0);
90: exit(1);
91: }
92: hp->addr.s_addr = addr.s_addr;
93: hp->next = hosts;
94: hosts = hp;
95: }
96:
97: int
98: rusers_reply(char *replyp, struct sockaddr_in *raddrp)
99: {
100: int x, idle;
101: char date[32], idle_time[64], remote[64], local[64];
102: struct hostent *hp;
1.4 deraadt 103: utmpidlearr *up = (utmpidlearr *)replyp;
1.1 deraadt 104: char *host;
105: int days, hours, minutes, seconds;
106:
107: if (search_host(raddrp->sin_addr))
108: return(0);
109:
110: if (!allopt && !up->uia_cnt)
111: return(0);
112:
113: hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
114: sizeof(struct in_addr), AF_INET);
115: if (hp)
116: host = hp->h_name;
117: else
118: host = inet_ntoa(raddrp->sin_addr);
119:
120: if (!longopt)
121: printf("%-*.*s ", HOST_WIDTH, HOST_WIDTH, host);
122:
123: for (x = 0; x < up->uia_cnt; x++) {
124: strncpy(date,
1.4 deraadt 125: &(ctime((time_t *)&(up->uia_arr[x]->ui_utmp.ut_time))[4]),
126: sizeof(date)-1);
1.1 deraadt 127:
128: idle = up->uia_arr[x]->ui_idle;
129: sprintf(idle_time, " :%02d", idle);
130: if (idle == MAX_INT)
131: strcpy(idle_time, "??");
132: else if (idle == 0)
133: strcpy(idle_time, "");
134: else {
135: seconds = idle;
136: days = seconds/(60*60*24);
137: seconds %= (60*60*24);
138: hours = seconds/(60*60);
139: seconds %= (60*60);
140: minutes = seconds/60;
141: seconds %= 60;
142: if (idle > 60)
143: sprintf(idle_time, "%2d:%02d",
1.4 deraadt 144: minutes, seconds);
1.1 deraadt 145: if (idle >= (60*60))
146: sprintf(idle_time, "%2d:%02d:%02d",
1.4 deraadt 147: hours, minutes, seconds);
1.1 deraadt 148: if (idle >= (24*60*60))
149: sprintf(idle_time, "%d days, %d:%02d:%02d",
1.4 deraadt 150: days, hours, minutes, seconds);
1.1 deraadt 151: }
152:
153: strncpy(remote, up->uia_arr[x]->ui_utmp.ut_host,
154: sizeof(remote)-1);
155: if (strlen(remote) != 0)
156: sprintf(remote, "(%.16s)",
157: up->uia_arr[x]->ui_utmp.ut_host);
158:
159: if (longopt) {
160: strncpy(local, host, sizeof(local));
161: local[HOST_WIDTH + LINE_WIDTH + 1 -
162: strlen(up->uia_arr[x]->ui_utmp.ut_line) - 1] = 0;
163: strcat(local, ":");
1.7 bitblt 164: strncat(local, up->uia_arr[x]->ui_utmp.ut_line,
165: sizeof (local) - strlen (local) - 1);
166: local[sizeof (local) - 1] = 0;
1.1 deraadt 167:
1.10 ! deraadt 168: #define MAXNAME 8
1.9 deraadt 169: printf("%-*.*s %-*.*s %-12.12s %8s %.18s\n",
170: MAXNAME, UT_NAMESIZE,
1.1 deraadt 171: up->uia_arr[x]->ui_utmp.ut_name,
1.4 deraadt 172: HOST_WIDTH+LINE_WIDTH+1, HOST_WIDTH+LINE_WIDTH+1,
173: local, date, idle_time, remote);
1.1 deraadt 174: } else
1.9 deraadt 175: printf("%.*s ", UT_NAMESIZE,
1.1 deraadt 176: up->uia_arr[x]->ui_utmp.ut_name);
177: }
178: if (!longopt)
179: putchar('\n');
180:
181: remember_host(raddrp->sin_addr);
182: return(0);
183: }
184:
185: void
186: onehost(char *host)
187: {
1.4 deraadt 188: utmpidlearr up;
1.1 deraadt 189: CLIENT *rusers_clnt;
190: struct sockaddr_in addr;
191: struct hostent *hp;
192:
193: hp = gethostbyname(host);
194: if (hp == NULL) {
195: fprintf(stderr, "%s: unknown host \"%s\"\n",
196: argv0, host);
197: exit(1);
198: }
199:
200: rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
201: if (rusers_clnt == NULL) {
202: clnt_pcreateerror(argv0);
203: exit(1);
204: }
205:
206: bzero((char *)&up, sizeof(up));
207: if (clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
208: xdr_utmpidlearr, &up, timeout) != RPC_SUCCESS) {
209: clnt_perror(rusers_clnt, argv0);
210: exit(1);
211: }
212: addr.sin_addr.s_addr = *(int *)hp->h_addr;
213: rusers_reply((char *)&up, &addr);
214: }
215:
216: void
217: allhosts(void)
218: {
1.4 deraadt 219: utmpidlearr up;
1.1 deraadt 220: enum clnt_stat clnt_stat;
221:
222: bzero((char *)&up, sizeof(up));
223: clnt_stat = clnt_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
224: RUSERSPROC_NAMES, xdr_void, NULL, xdr_utmpidlearr,
1.6 deraadt 225: (char *)&up, rusers_reply);
1.1 deraadt 226: if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
227: fprintf(stderr, "%s: %s\n", argv0, clnt_sperrno(clnt_stat));
228: exit(1);
229: }
230: }
231:
1.2 deraadt 232: void
233: usage(void)
1.1 deraadt 234: {
235: fprintf(stderr, "Usage: %s [-la] [hosts ...]\n", argv0);
236: exit(1);
237: }
238:
1.2 deraadt 239: int
240: main(int argc, char *argv[])
1.1 deraadt 241: {
242: int ch;
243: extern int optind;
244:
1.5 millert 245: if (!(argv0 = strrchr(argv[0], '/')))
1.1 deraadt 246: argv0 = argv[0];
247: else
248: argv0++;
249:
250: while ((ch = getopt(argc, argv, "al")) != -1)
251: switch (ch) {
252: case 'a':
253: allopt++;
254: break;
255: case 'l':
256: longopt++;
257: break;
258: default:
259: usage();
260: /*NOTREACHED*/
261: }
262:
263: setlinebuf(stdout);
264: if (argc == optind)
265: allhosts();
266: else {
267: for (; optind < argc; optind++)
268: (void) onehost(argv[optind]);
269: }
270: exit(0);
271: }