rusers.c revision 1.1 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 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <netdb.h>
38 #include <stdio.h>
39 #include <strings.h>
40 #include <rpc/rpc.h>
41 #include <arpa/inet.h>
42 #include <rpcsvc/rnusers.h>
43
44 #define MAX_INT 0x7fffffff
45 #define HOST_WIDTH 12
46 #define LINE_WIDTH 15
47 char *argv0;
48
49 int longopt;
50 int allopt;
51
52 struct host_list {
53 struct host_list *next;
54 struct in_addr addr;
55 } *hosts;
56
57 int search_host(struct in_addr addr)
58 {
59 struct host_list *hp;
60
61 if (!hosts)
62 return(0);
63
64 for (hp = hosts; hp != NULL; hp = hp->next) {
65 if (hp->addr.s_addr == addr.s_addr)
66 return(1);
67 }
68 return(0);
69 }
70
71 void remember_host(struct in_addr addr)
72 {
73 struct host_list *hp;
74
75 if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
76 fprintf(stderr, "%s: no memory.\n", argv0);
77 exit(1);
78 }
79 hp->addr.s_addr = addr.s_addr;
80 hp->next = hosts;
81 hosts = hp;
82 }
83
84 rusers_reply(char *replyp, struct sockaddr_in *raddrp)
85 {
86 int x, idle;
87 char date[128], idle_time[128], remote[128];
88 struct hostent *hp;
89 utmpidlearr *up = (utmpidlearr *)replyp;
90 char *host;
91
92 if (search_host(raddrp->sin_addr))
93 return(0);
94
95 if (!allopt && !up->utmpidlearr_len)
96 return(0);
97
98 hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
99 sizeof(struct in_addr), AF_INET);
100 if (hp)
101 host = hp->h_name;
102 else
103 host = inet_ntoa(raddrp->sin_addr);
104
105 if (!longopt)
106 printf("%-*.*s ", HOST_WIDTH, HOST_WIDTH, host);
107
108 for (x = 0; x < up->utmpidlearr_len; x++) {
109
110 strcpy(date, &(ctime((time_t *)&(up->utmpidlearr_val[x].ui_utmp.ut_time))[4]));
111
112 idle = up->utmpidlearr_val[x].ui_idle;
113 sprintf(idle_time, " :%02d", idle);
114 if (idle == 0)
115 strcpy(idle_time, "");
116 if (idle > 60)
117 sprintf(idle_time, "%d:%02d", idle/60, idle%60);
118 if (idle == MAX_INT)
119 strcpy(idle_time, "??");
120
121 strcpy(remote, up->utmpidlearr_val[x].ui_utmp.ut_host);
122 if (strlen(remote) != 0)
123 sprintf(remote, "(%.16s)", up->utmpidlearr_val[x].ui_utmp.ut_host);
124
125 if (longopt)
126 printf("%-8.8s %*.*s:%-*.*s %-12.12s %6s %.18s\n",
127 up->utmpidlearr_val[x].ui_utmp.ut_name,
128 HOST_WIDTH, HOST_WIDTH, host,
129 LINE_WIDTH, LINE_WIDTH, up->utmpidlearr_val[x].ui_utmp.ut_line,
130 date,
131 idle_time,
132 remote
133 );
134 else
135 printf("%s ",
136 up->utmpidlearr_val[x].ui_utmp.ut_name);
137 }
138 if (!longopt)
139 putchar('\n');
140
141 remember_host(raddrp->sin_addr);
142 return(0);
143 }
144
145 onehost(char *host)
146 {
147 utmpidlearr up;
148 CLIENT *rusers_clnt;
149 struct sockaddr_in addr;
150 struct hostent *hp;
151
152 hp = gethostbyname(host);
153 if (hp == NULL) {
154 fprintf(stderr, "%s: unknown host \"%s\"\n",
155 argv0, host);
156 exit(1);
157 }
158
159 rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
160 if (rusers_clnt == NULL) {
161 clnt_pcreateerror(argv0);
162 exit(1);
163 }
164
165 bzero((char *)&up, sizeof(up));
166 if (clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL, xdr_utmpidlearr, &up, NULL) != RPC_SUCCESS) {
167 clnt_perror(rusers_clnt, argv0);
168 exit(1);
169 }
170 addr.sin_addr.s_addr = *(int *)hp->h_addr;
171 rusers_reply((char *)&up, &addr);
172 }
173
174 allhosts()
175 {
176 utmpidlearr up;
177 enum clnt_stat clnt_stat;
178
179 bzero((char *)&up, sizeof(up));
180 clnt_stat = clnt_broadcast(RUSERSPROG, RUSERSVERS_IDLE, RUSERSPROC_NAMES,
181 xdr_void, NULL,
182 xdr_utmpidlearr, &up, rusers_reply);
183 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
184 fprintf(stderr, "%s: %s\n", argv0, clnt_sperrno(clnt_stat));
185 exit(1);
186 }
187 }
188
189 usage()
190 {
191 fprintf(stderr, "Usage: %s [-la] [hosts ...]\n", argv0);
192 exit(1);
193 }
194
195 main(int argc, char *argv[])
196 {
197 int ch;
198 extern int optind;
199
200 if (!(argv0 = rindex(argv[0], '/')))
201 argv0 = argv[0];
202 else
203 argv0++;
204
205
206 while ((ch = getopt(argc, argv, "al")) != -1)
207 switch (ch) {
208 case 'a':
209 allopt++;
210 break;
211 case 'l':
212 longopt++;
213 break;
214 default:
215 usage();
216 /*NOTREACHED*/
217 }
218
219 if (argc == optind)
220 allhosts();
221 else {
222 for (; optind < argc; optind++)
223 (void) onehost(argv[optind]);
224 }
225 exit(0);
226 }
227