Annotation of src/usr.bin/rusers/rusers.c, Revision 1.8
1.8 ! millert 1: /* $OpenBSD: rusers.c,v 1.7 1997/07/08 16:49:45 bitblt 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.8 ! millert 32: static char rcsid[] = "$OpenBSD: rusers.c,v 1.7 1997/07/08 16:49:45 bitblt 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:
168: printf("%-8.8s %-*.*s %-12.12s %8s %.18s\n",
169: up->uia_arr[x]->ui_utmp.ut_name,
1.4 deraadt 170: HOST_WIDTH+LINE_WIDTH+1, HOST_WIDTH+LINE_WIDTH+1,
171: local, date, idle_time, remote);
1.1 deraadt 172: } else
1.6 deraadt 173: printf("%.8s ",
1.1 deraadt 174: up->uia_arr[x]->ui_utmp.ut_name);
175: }
176: if (!longopt)
177: putchar('\n');
178:
179: remember_host(raddrp->sin_addr);
180: return(0);
181: }
182:
183: void
184: onehost(char *host)
185: {
1.4 deraadt 186: utmpidlearr up;
1.1 deraadt 187: CLIENT *rusers_clnt;
188: struct sockaddr_in addr;
189: struct hostent *hp;
190:
191: hp = gethostbyname(host);
192: if (hp == NULL) {
193: fprintf(stderr, "%s: unknown host \"%s\"\n",
194: argv0, host);
195: exit(1);
196: }
197:
198: rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
199: if (rusers_clnt == NULL) {
200: clnt_pcreateerror(argv0);
201: exit(1);
202: }
203:
204: bzero((char *)&up, sizeof(up));
205: if (clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
206: xdr_utmpidlearr, &up, timeout) != RPC_SUCCESS) {
207: clnt_perror(rusers_clnt, argv0);
208: exit(1);
209: }
210: addr.sin_addr.s_addr = *(int *)hp->h_addr;
211: rusers_reply((char *)&up, &addr);
212: }
213:
214: void
215: allhosts(void)
216: {
1.4 deraadt 217: utmpidlearr up;
1.1 deraadt 218: enum clnt_stat clnt_stat;
219:
220: bzero((char *)&up, sizeof(up));
221: clnt_stat = clnt_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
222: RUSERSPROC_NAMES, xdr_void, NULL, xdr_utmpidlearr,
1.6 deraadt 223: (char *)&up, rusers_reply);
1.1 deraadt 224: if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
225: fprintf(stderr, "%s: %s\n", argv0, clnt_sperrno(clnt_stat));
226: exit(1);
227: }
228: }
229:
1.2 deraadt 230: void
231: usage(void)
1.1 deraadt 232: {
233: fprintf(stderr, "Usage: %s [-la] [hosts ...]\n", argv0);
234: exit(1);
235: }
236:
1.2 deraadt 237: int
238: main(int argc, char *argv[])
1.1 deraadt 239: {
240: int ch;
241: extern int optind;
242:
1.5 millert 243: if (!(argv0 = strrchr(argv[0], '/')))
1.1 deraadt 244: argv0 = argv[0];
245: else
246: argv0++;
247:
248: while ((ch = getopt(argc, argv, "al")) != -1)
249: switch (ch) {
250: case 'a':
251: allopt++;
252: break;
253: case 'l':
254: longopt++;
255: break;
256: default:
257: usage();
258: /*NOTREACHED*/
259: }
260:
261: setlinebuf(stdout);
262: if (argc == optind)
263: allhosts();
264: else {
265: for (; optind < argc; optind++)
266: (void) onehost(argv[optind]);
267: }
268: exit(0);
269: }