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