rusers.c revision 1.21 1 /* $NetBSD: rusers.c,v 1.21 2001/02/19 23:03:51 cgd 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 #include <sys/cdefs.h>
32 #ifndef lint
33 __RCSID("$NetBSD: rusers.c,v 1.21 2001/02/19 23:03:51 cgd Exp $");
34 #endif /* not lint */
35
36 #include <sys/types.h>
37 #include <sys/socket.h>
38
39 #include <rpc/rpc.h>
40 #include <arpa/inet.h>
41
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>
50
51 /*
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.
55 */
56 #include <rpcsvc/rnusers.h> /* Old version */
57
58
59 #define MAX_INT 0x7fffffff
60
61 struct timeval timeout = { 25, 0 };
62 int longopt;
63 int allopt;
64
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);
72
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;
81
82 #define addr6 addr._addr6
83 #define addr4 addr._addr4
84
85 int
86 search_host(struct sockaddr *sa)
87 {
88 struct host_list *hp;
89
90 if (!hosts)
91 return(0);
92
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;
109 }
110 }
111 return(0);
112 }
113
114 void
115 remember_host(struct sockaddr *sa)
116 {
117 struct host_list *hp;
118
119 if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
120 err(1, "malloc");
121 /* NOTREACHED */
122 }
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 */
137 }
138 hosts = hp;
139 }
140
141 int
142 rusers_reply(char *replyp, struct netbuf *raddrp, struct netconfig *nconf)
143 {
144 char host[NI_MAXHOST];
145 int x;
146 struct utmpidlearr *up = (struct utmpidlearr *)replyp;
147 struct sockaddr *sa = raddrp->buf;
148
149 if (search_host(sa))
150 return(0);
151
152 if (!allopt && !up->uia_cnt)
153 return(0);
154
155 if (getnameinfo(sa, sa->sa_len, host, sizeof host, NULL, 0, 0))
156 return 0;
157
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)
161
162 if (!longopt)
163 printf("%-*.*s ", HOSTWID, HOSTWID, host);
164
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
171 if (!longopt) {
172 printf("%.*s ", NAMEWID,
173 up->uia_arr[x]->ui_utmp.ut_name);
174 continue;
175 }
176
177 snprintf(local, sizeof(local), "%.*s:%s",
178 HOSTWID, host,
179 up->uia_arr[x]->ui_utmp.ut_line);
180
181 snprintf(date, sizeof(date), "%s",
182 &(ctime((time_t *)&(up->uia_arr[x]->ui_utmp.ut_time)))[4]);
183
184 minutes = up->uia_arr[x]->ui_idle;
185 if (minutes == MAX_INT)
186 strcpy(idle, "??");
187 else if (minutes == 0)
188 strcpy(idle, "");
189 else {
190 unsigned int days, hours;
191
192 days = minutes / (24 * 60);
193 minutes %= (24 * 60);
194 hours = minutes / 60;
195 minutes %= 60;
196
197 if (days > 0)
198 snprintf(idle, sizeof(idle), "%d d ", days);
199 else if (hours > 0)
200 snprintf(idle, sizeof(idle), "%2d:%02d",
201 hours, minutes);
202 else
203 snprintf(idle, sizeof(idle), ":%02d", minutes);
204 }
205
206 if (up->uia_arr[x]->ui_utmp.ut_host[0] != '\0')
207 snprintf(remote, sizeof(remote), "(%.*s)",
208 HOSTWID, up->uia_arr[x]->ui_utmp.ut_host);
209 else
210 remote[0] = '\0';
211
212 printf("%-*.*s %-*.*s %-12.12s %8.8s %s\n",
213 NAMEWID, NAMEWID, up->uia_arr[x]->ui_utmp.ut_name,
214 HOSTWID+LINEWID+1, HOSTWID+LINEWID+1, local,
215 date, idle, remote);
216 }
217 if (!longopt)
218 putchar('\n');
219
220 remember_host(sa);
221 return(0);
222 }
223
224 void
225 onehost(char *host)
226 {
227 struct utmpidlearr up;
228 CLIENT *rusers_clnt;
229 enum clnt_stat clnt_stat;
230 struct netbuf nb;
231 struct addrinfo *ai;
232 int ecode;
233
234 rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
235 if (rusers_clnt == NULL) {
236 clnt_pcreateerror(getprogname());
237 exit(1);
238 }
239
240 ecode = getaddrinfo(host, NULL, NULL, &ai);
241 if (ecode != 0)
242 err(1, "%s", gai_strerror(ecode));
243
244 memset((char *)&up, 0, sizeof(up));
245 clnt_stat = clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
246 xdr_utmpidlearr, &up, timeout);
247 if (clnt_stat != RPC_SUCCESS)
248 errx(1, "%s", clnt_sperrno(clnt_stat));
249 nb.buf = ai->ai_addr;
250 nb.len = nb.maxlen = ai->ai_addrlen;
251 rusers_reply((char *)&up, &nb, NULL);
252 freeaddrinfo(ai);
253 }
254
255 void
256 allhosts(void)
257 {
258 struct utmpidlearr up;
259 enum clnt_stat clnt_stat;
260
261 memset((char *)&up, 0, sizeof(up));
262 clnt_stat = rpc_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
263 RUSERSPROC_NAMES, xdr_void, NULL, xdr_utmpidlearr,
264 (char *)&up, (resultproc_t)rusers_reply, "udp");
265 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
266 errx(1, "%s", clnt_sperrno(clnt_stat));
267 }
268
269 void
270 usage(void)
271 {
272 fprintf(stderr, "Usage: %s [-la] [hosts ...]\n", getprogname());
273 exit(1);
274 }
275
276 int
277 main(int argc, char *argv[])
278 {
279 int ch;
280
281 while ((ch = getopt(argc, argv, "al")) != -1)
282 switch (ch) {
283 case 'a':
284 allopt++;
285 break;
286 case 'l':
287 longopt++;
288 break;
289 default:
290 usage();
291 /*NOTREACHED*/
292 }
293
294 setlinebuf(stdout);
295 if (argc == optind)
296 allhosts();
297 else {
298 for (; optind < argc; optind++)
299 (void) onehost(argv[optind]);
300 }
301 exit(0);
302 }
303