rusers.c revision 1.12 1 /* $NetBSD: rusers.c,v 1.12 1997/01/09 20:21:18 tls Exp $ */
2
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
32 static char rcsid[] = "$NetBSD: rusers.c,v 1.12 1997/01/09 20:21:18 tls Exp $";
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>
40 #include <strings.h>
41 #include <rpc/rpc.h>
42 #include <arpa/inet.h>
43 #include <utmp.h>
44 #include <stdlib.h>
45
46 /*
47 * For now we only try version 2 of the protocol. The current
48 * version is 3 (rusers.h), but only Solaris and NetBSD seem
49 * to support it currently.
50 */
51 #include <rpcsvc/rnusers.h> /* Old version */
52
53 #define MAX_INT 0x7fffffff
54 #define HOST_WIDTH 20
55 #define LINE_WIDTH 8
56 char *argv0;
57
58 struct timeval timeout = { 25, 0 };
59 int longopt;
60 int allopt;
61
62 struct host_list {
63 struct host_list *next;
64 struct in_addr addr;
65 } *hosts;
66
67 int
68 search_host(struct in_addr addr)
69 {
70 struct host_list *hp;
71
72 if (!hosts)
73 return(0);
74
75 for (hp = hosts; hp != NULL; hp = hp->next) {
76 if (hp->addr.s_addr == addr.s_addr)
77 return(1);
78 }
79 return(0);
80 }
81
82 void
83 remember_host(struct in_addr addr)
84 {
85 struct host_list *hp;
86
87 if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
88 fprintf(stderr, "%s: no memory.\n", argv0);
89 exit(1);
90 }
91 hp->addr.s_addr = addr.s_addr;
92 hp->next = hosts;
93 hosts = hp;
94 }
95
96 int
97 rusers_reply(char *replyp, struct sockaddr_in *raddrp)
98 {
99 int x, idle;
100 char date[32], idle_time[64], remote[64], local[64];
101 struct hostent *hp;
102 struct utmpidlearr *up = (struct utmpidlearr *)replyp;
103 char *host;
104 int days, hours, minutes, seconds;
105
106 if (search_host(raddrp->sin_addr))
107 return(0);
108
109 if (!allopt && !up->uia_cnt)
110 return(0);
111
112 hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
113 sizeof(struct in_addr), AF_INET);
114 if (hp)
115 host = hp->h_name;
116 else
117 host = inet_ntoa(raddrp->sin_addr);
118
119 if (!longopt)
120 printf("%-*.*s ", HOST_WIDTH, HOST_WIDTH, host);
121
122 for (x = 0; x < up->uia_cnt; x++) {
123 strncpy(date,
124 &(ctime((time_t *)&(up->uia_arr[x]->ui_utmp.ut_time))[4]),
125 sizeof(date)-1);
126
127 idle = up->uia_arr[x]->ui_idle;
128 sprintf(idle_time, " :%02d", idle);
129 if (idle == MAX_INT)
130 strcpy(idle_time, "??");
131 else if (idle == 0)
132 strcpy(idle_time, "");
133 else {
134 seconds = idle;
135 days = seconds/(60*60*24);
136 seconds %= (60*60*24);
137 hours = seconds/(60*60);
138 seconds %= (60*60);
139 minutes = seconds/60;
140 seconds %= 60;
141 if (idle > 60)
142 sprintf(idle_time, "%2d:%02d",
143 minutes, seconds);
144 if (idle >= (60*60))
145 sprintf(idle_time, "%2d:%02d:%02d",
146 hours, minutes, seconds);
147 if (idle >= (24*60*60))
148 sprintf(idle_time, "%d days, %d:%02d:%02d",
149 days, hours, minutes, seconds);
150 }
151
152 strncpy(remote, up->uia_arr[x]->ui_utmp.ut_host,
153 sizeof(remote)-1);
154 if (strlen(remote) != 0)
155 sprintf(remote, "(%.16s)",
156 up->uia_arr[x]->ui_utmp.ut_host);
157
158 if (longopt) {
159 strncpy(local, host, sizeof(local));
160 local[HOST_WIDTH + LINE_WIDTH + 1 -
161 strlen(up->uia_arr[x]->ui_utmp.ut_line) - 1] = 0;
162 strcat(local, ":");
163 strcat(local, up->uia_arr[x]->ui_utmp.ut_line);
164
165 printf("%-8.8s %-*.*s %-12.12s %8s %.18s\n",
166 up->uia_arr[x]->ui_utmp.ut_name,
167 HOST_WIDTH+LINE_WIDTH+1, HOST_WIDTH+LINE_WIDTH+1, local,
168 date,
169 idle_time,
170 remote);
171 } else
172 printf("%0.8s ",
173 up->uia_arr[x]->ui_utmp.ut_name);
174 }
175 if (!longopt)
176 putchar('\n');
177
178 remember_host(raddrp->sin_addr);
179 return(0);
180 }
181
182 void
183 onehost(char *host)
184 {
185 struct utmpidlearr up;
186 CLIENT *rusers_clnt;
187 struct sockaddr_in addr;
188 struct hostent *hp;
189
190 hp = gethostbyname(host);
191 if (hp == NULL) {
192 fprintf(stderr, "%s: unknown host \"%s\"\n",
193 argv0, host);
194 exit(1);
195 }
196
197 rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
198 if (rusers_clnt == NULL) {
199 clnt_pcreateerror(argv0);
200 exit(1);
201 }
202
203 bzero((char *)&up, sizeof(up));
204 if (clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
205 xdr_utmpidlearr, &up, timeout) != RPC_SUCCESS) {
206 clnt_perror(rusers_clnt, argv0);
207 exit(1);
208 }
209 addr.sin_addr.s_addr = *(int *)hp->h_addr;
210 rusers_reply((char *)&up, &addr);
211 }
212
213 void
214 allhosts(void)
215 {
216 struct utmpidlearr up;
217 enum clnt_stat clnt_stat;
218
219 bzero((char *)&up, sizeof(up));
220 clnt_stat = clnt_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
221 RUSERSPROC_NAMES, xdr_void, NULL, xdr_utmpidlearr,
222 &up, rusers_reply);
223 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
224 fprintf(stderr, "%s: %s\n", argv0, clnt_sperrno(clnt_stat));
225 exit(1);
226 }
227 }
228
229 void
230 usage(void)
231 {
232 fprintf(stderr, "Usage: %s [-la] [hosts ...]\n", argv0);
233 exit(1);
234 }
235
236 int
237 main(int argc, char *argv[])
238 {
239 int ch;
240 extern int optind;
241
242 if (!(argv0 = rindex(argv[0], '/')))
243 argv0 = argv[0];
244 else
245 argv0++;
246
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 }
270