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