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