Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.bin / rusers / rusers.c
blobe0d66fecd828d474c6f7559e823a4791c4f6b72f
1 /* $NetBSD: rusers.c,v 1.22 2004/01/05 23:23:36 jmmv Exp $ */
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.
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.
31 #include <sys/cdefs.h>
32 #ifndef lint
33 __RCSID("$NetBSD: rusers.c,v 1.22 2004/01/05 23:23:36 jmmv Exp $");
34 #endif /* not lint */
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <rpc/rpc.h>
40 #include <arpa/inet.h>
42 #include <err.h>
43 #include <netdb.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <time.h>
48 #include <unistd.h>
49 #include <utmp.h>
52 * For now we only try version 2 of the protocol. The current
53 * version is 3 (rusers.h), but only Solaris and NetBSD seem
54 * to support it currently.
56 #include <rpcsvc/rnusers.h> /* Old version */
59 #define MAX_INT 0x7fffffff
61 struct timeval timeout = { 25, 0 };
62 int longopt;
63 int allopt;
65 void allhosts(void);
66 int main(int, char *[]);
67 void onehost(char *);
68 void remember_host(struct sockaddr *);
69 int rusers_reply(char *, struct netbuf *, struct netconfig *);
70 int search_host(struct sockaddr *);
71 void usage(void);
73 struct host_list {
74 struct host_list *next;
75 int family;
76 union {
77 struct in6_addr _addr6;
78 struct in_addr _addr4;
79 } addr;
80 } *hosts;
82 #define addr6 addr._addr6
83 #define addr4 addr._addr4
85 int
86 search_host(struct sockaddr *sa)
88 struct host_list *hp;
90 if (!hosts)
91 return(0);
93 for (hp = hosts; hp != NULL; hp = hp->next) {
94 switch (hp->family) {
95 case AF_INET6:
96 if (!memcmp(&hp->addr6,
97 &((struct sockaddr_in6 *)sa)->sin6_addr,
98 sizeof (struct in6_addr)))
99 return 1;
100 break;
101 case AF_INET:
102 if (!memcmp(&hp->addr4,
103 &((struct sockaddr_in *)sa)->sin_addr,
104 sizeof (struct in_addr)))
105 return 1;
106 break;
107 default:
108 break;
111 return(0);
114 void
115 remember_host(struct sockaddr *sa)
117 struct host_list *hp;
119 if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
120 err(1, "malloc");
121 /* NOTREACHED */
123 hp->family = sa->sa_family;
124 hp->next = hosts;
125 switch (sa->sa_family) {
126 case AF_INET6:
127 memcpy(&hp->addr6, &((struct sockaddr_in6 *)sa)->sin6_addr,
128 sizeof (struct in6_addr));
129 break;
130 case AF_INET:
131 memcpy(&hp->addr4, &((struct sockaddr_in *)sa)->sin_addr,
132 sizeof (struct in_addr));
133 break;
134 default:
135 err(1, "unknown address family");
136 /* NOTREACHED */
138 hosts = hp;
142 rusers_reply(char *replyp, struct netbuf *raddrp, struct netconfig *nconf)
144 char host[NI_MAXHOST];
145 int x;
146 struct utmpidlearr *up = (struct utmpidlearr *)replyp;
147 struct sockaddr *sa = raddrp->buf;
149 if (search_host(sa))
150 return(0);
152 if (!allopt && !up->uia_cnt)
153 return(0);
155 if (getnameinfo(sa, sa->sa_len, host, sizeof host, NULL, 0, 0))
156 return 0;
158 #define HOSTWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_host)
159 #define LINEWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_line)
160 #define NAMEWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_name)
162 if (!longopt)
163 printf("%-*.*s ", HOSTWID, HOSTWID, host);
165 for (x = 0; x < up->uia_cnt; x++) {
166 unsigned int minutes;
167 char date[26], idle[8];
168 char remote[HOSTWID + 3]; /* "(" host ")" \0 */
169 char local[HOSTWID + LINEWID + 2]; /* host ":" line \0 */
170 time_t uttime;
172 if (!longopt) {
173 printf("%.*s ", NAMEWID,
174 up->uia_arr[x]->ui_utmp.ut_name);
175 continue;
178 snprintf(local, sizeof(local), "%.*s:%s",
179 HOSTWID, host,
180 up->uia_arr[x]->ui_utmp.ut_line);
182 uttime = up->uia_arr[x]->ui_utmp.ut_time;
183 snprintf(date, sizeof(date), "%s",
184 &(ctime(&uttime))[4]);
186 minutes = up->uia_arr[x]->ui_idle;
187 if (minutes == MAX_INT)
188 strcpy(idle, "??");
189 else if (minutes == 0)
190 strcpy(idle, "");
191 else {
192 unsigned int days, hours;
194 days = minutes / (24 * 60);
195 minutes %= (24 * 60);
196 hours = minutes / 60;
197 minutes %= 60;
199 if (days > 0)
200 snprintf(idle, sizeof(idle), "%d d ", days);
201 else if (hours > 0)
202 snprintf(idle, sizeof(idle), "%2d:%02d",
203 hours, minutes);
204 else
205 snprintf(idle, sizeof(idle), ":%02d", minutes);
208 if (up->uia_arr[x]->ui_utmp.ut_host[0] != '\0')
209 snprintf(remote, sizeof(remote), "(%.*s)",
210 HOSTWID, up->uia_arr[x]->ui_utmp.ut_host);
211 else
212 remote[0] = '\0';
214 printf("%-*.*s %-*.*s %-12.12s %8.8s %s\n",
215 NAMEWID, NAMEWID, up->uia_arr[x]->ui_utmp.ut_name,
216 HOSTWID+LINEWID+1, HOSTWID+LINEWID+1, local,
217 date, idle, remote);
219 if (!longopt)
220 putchar('\n');
222 remember_host(sa);
223 return(0);
226 void
227 onehost(char *host)
229 struct utmpidlearr up;
230 CLIENT *rusers_clnt;
231 enum clnt_stat clnt_stat;
232 struct netbuf nb;
233 struct addrinfo *ai;
234 int ecode;
236 rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
237 if (rusers_clnt == NULL) {
238 clnt_pcreateerror(getprogname());
239 exit(1);
242 ecode = getaddrinfo(host, NULL, NULL, &ai);
243 if (ecode != 0)
244 err(1, "%s", gai_strerror(ecode));
246 memset((char *)&up, 0, sizeof(up));
247 clnt_stat = clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
248 xdr_utmpidlearr, &up, timeout);
249 if (clnt_stat != RPC_SUCCESS)
250 errx(1, "%s", clnt_sperrno(clnt_stat));
251 nb.buf = ai->ai_addr;
252 nb.len = nb.maxlen = ai->ai_addrlen;
253 rusers_reply((char *)&up, &nb, NULL);
254 freeaddrinfo(ai);
257 void
258 allhosts(void)
260 struct utmpidlearr up;
261 enum clnt_stat clnt_stat;
263 memset((char *)&up, 0, sizeof(up));
264 clnt_stat = rpc_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
265 RUSERSPROC_NAMES, xdr_void, NULL, xdr_utmpidlearr,
266 (char *)&up, (resultproc_t)rusers_reply, "udp");
267 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
268 errx(1, "%s", clnt_sperrno(clnt_stat));
271 void
272 usage(void)
274 fprintf(stderr, "usage: %s [-la] [hosts ...]\n", getprogname());
275 exit(1);
279 main(int argc, char *argv[])
281 int ch;
283 while ((ch = getopt(argc, argv, "al")) != -1)
284 switch (ch) {
285 case 'a':
286 allopt++;
287 break;
288 case 'l':
289 longopt++;
290 break;
291 default:
292 usage();
293 /*NOTREACHED*/
296 setlinebuf(stdout);
297 if (argc == optind)
298 allhosts();
299 else {
300 for (; optind < argc; optind++)
301 (void) onehost(argv[optind]);
303 exit(0);