rusers.c revision 1.16 1 /* $NetBSD: rusers.c,v 1.16 1998/07/03 18:35:35 hubertf 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.16 1998/07/03 18:35:35 hubertf 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 extern char *__progname; /* from crt0.o */
61
62 struct timeval timeout = { 25, 0 };
63 int longopt;
64 int allopt;
65
66 void allhosts __P((void));
67 int main __P((int, char *[]));
68 void onehost __P((char *));
69 void remember_host __P((struct in_addr));
70 int rusers_reply __P((char *, struct sockaddr_in *));
71 int search_host __P((struct in_addr));
72 void usage __P((void));
73
74 struct host_list {
75 struct host_list *next;
76 struct in_addr addr;
77 } *hosts;
78
79 int
80 search_host(struct in_addr addr)
81 {
82 struct host_list *hp;
83
84 if (!hosts)
85 return(0);
86
87 for (hp = hosts; hp != NULL; hp = hp->next) {
88 if (hp->addr.s_addr == addr.s_addr)
89 return(1);
90 }
91 return(0);
92 }
93
94 void
95 remember_host(struct in_addr addr)
96 {
97 struct host_list *hp;
98
99 if (!(hp = (struct host_list *)malloc(sizeof(struct host_list))))
100 errx(1, "no memory");
101 hp->addr.s_addr = addr.s_addr;
102 hp->next = hosts;
103 hosts = hp;
104 }
105
106 int
107 rusers_reply(char *replyp, struct sockaddr_in *raddrp)
108 {
109 int x;
110 struct hostent *hp;
111 struct utmpidlearr *up = (struct utmpidlearr *)replyp;
112 char *host;
113
114 if (search_host(raddrp->sin_addr))
115 return(0);
116
117 if (!allopt && !up->uia_cnt)
118 return(0);
119
120 hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
121 sizeof(struct in_addr), AF_INET);
122 if (hp)
123 host = hp->h_name;
124 else
125 host = inet_ntoa(raddrp->sin_addr);
126
127 #define HOSTWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_host)
128 #define LINEWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_line)
129 #define NAMEWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_name)
130
131 if (!longopt)
132 printf("%-*.*s ", HOSTWID, HOSTWID, host);
133
134 for (x = 0; x < up->uia_cnt; x++) {
135 unsigned int minutes;
136 char date[26], idle[8];
137 char remote[HOSTWID + 3]; /* "(" host ")" \0 */
138 char local[HOSTWID + LINEWID + 2]; /* host ":" line \0 */
139
140 if (!longopt) {
141 printf("%.*s ", NAMEWID,
142 up->uia_arr[x]->ui_utmp.ut_name);
143 continue;
144 }
145
146 snprintf(local, sizeof(local), "%.*s:%s",
147 HOSTWID, host,
148 up->uia_arr[x]->ui_utmp.ut_line);
149
150 snprintf(date, sizeof(date), "%s",
151 &(ctime((time_t *)&(up->uia_arr[x]->ui_utmp.ut_time)))[4]);
152
153 minutes = up->uia_arr[x]->ui_idle;
154 if (minutes == MAX_INT)
155 strcpy(idle, "??");
156 else if (minutes == 0)
157 strcpy(idle, "");
158 else {
159 unsigned int days, hours;
160
161 days = minutes / (24 * 60);
162 minutes %= (24 * 60);
163 hours = minutes / 60;
164 minutes %= 60;
165
166 if (days > 0)
167 snprintf(idle, sizeof(idle), "%d d ", days);
168 else if (hours > 0)
169 snprintf(idle, sizeof(idle), "%2d:%02d",
170 hours, minutes);
171 else
172 snprintf(idle, sizeof(idle), ":%02d", minutes);
173 }
174
175 if (up->uia_arr[x]->ui_utmp.ut_host[0] != '\0')
176 snprintf(remote, sizeof(remote), "(%.*s)",
177 HOSTWID, up->uia_arr[x]->ui_utmp.ut_host);
178 else
179 remote[0] = '\0';
180
181 printf("%-*.*s %-*.*s %-12.12s %8.8s %s\n",
182 NAMEWID, NAMEWID, up->uia_arr[x]->ui_utmp.ut_name,
183 HOSTWID+LINEWID+1, HOSTWID+LINEWID+1, local,
184 date, idle, remote);
185 }
186 if (!longopt)
187 putchar('\n');
188
189 remember_host(raddrp->sin_addr);
190 return(0);
191 }
192
193 void
194 onehost(char *host)
195 {
196 struct utmpidlearr up;
197 CLIENT *rusers_clnt;
198 enum clnt_stat clnt_stat;
199 struct sockaddr_in addr;
200 struct hostent *hp;
201
202 hp = gethostbyname(host);
203 if (hp == NULL)
204 errx(1, "unknown host \"%s\"", host);
205
206 rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
207 if (rusers_clnt == NULL) {
208 clnt_pcreateerror(__progname);
209 exit(1);
210 }
211
212 memset((char *)&up, 0, sizeof(up));
213 clnt_stat = clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
214 xdr_utmpidlearr, &up, timeout);
215 if (clnt_stat != RPC_SUCCESS)
216 errx(1, "%s", clnt_sperrno(clnt_stat));
217 addr.sin_addr.s_addr = *(int *)hp->h_addr;
218 rusers_reply((char *)&up, &addr);
219 }
220
221 void
222 allhosts(void)
223 {
224 struct utmpidlearr up;
225 enum clnt_stat clnt_stat;
226
227 memset((char *)&up, 0, sizeof(up));
228 clnt_stat = clnt_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
229 RUSERSPROC_NAMES, xdr_void, NULL, xdr_utmpidlearr,
230 (char *)&up, rusers_reply);
231 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
232 errx(1, "%s", clnt_sperrno(clnt_stat));
233 }
234
235 void
236 usage(void)
237 {
238 fprintf(stderr, "Usage: %s [-la] [hosts ...]\n", __progname);
239 exit(1);
240 }
241
242 int
243 main(int argc, char *argv[])
244 {
245 int ch;
246 extern int optind;
247
248 while ((ch = getopt(argc, argv, "al")) != -1)
249 switch (ch) {
250 case 'a':
251 allopt++;
252 break;
253 case 'l':
254 longopt++;
255 break;
256 default:
257 usage();
258 /*NOTREACHED*/
259 }
260
261 setlinebuf(stdout);
262 if (argc == optind)
263 allhosts();
264 else {
265 for (; optind < argc; optind++)
266 (void) onehost(argv[optind]);
267 }
268 exit(0);
269 }
270