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