xref: /freebsd/usr.bin/rusers/rusers.c (revision 6780ab54325a71e7e70112b11657973edde8655e)
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/cdefs.h>
35 
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 
41 #include <rpc/rpc.h>
42 #include <rpc/pmap_clnt.h>
43 #include <rpcsvc/rnusers.h>
44 
45 #include <arpa/inet.h>
46 
47 #include <err.h>
48 #include <netdb.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 
54 #define MAX_INT		0x7fffffff
55 #define HOST_WIDTH	20
56 #define LINE_WIDTH	15
57 
58 int longopt;
59 int allopt;
60 
61 struct host_list {
62 	struct	host_list *next;
63 	struct	in_addr addr;
64 } *hosts;
65 
66 static int
67 search_host(struct in_addr addr)
68 {
69 	struct host_list *hp;
70 
71 	if (hosts == NULL)
72 		return (0);
73 
74 	for (hp = hosts; hp != NULL; hp = hp->next) {
75 		if (hp->addr.s_addr == addr.s_addr)
76 			return (1);
77 	}
78 	return (0);
79 }
80 
81 static void
82 remember_host(struct in_addr addr)
83 {
84 	struct host_list *hp;
85 
86 	if ((hp = (struct host_list *)malloc(sizeof(struct host_list))) == NULL)
87 		errx(1, "no memory");
88 	hp->addr.s_addr = addr.s_addr;
89 	hp->next = hosts;
90 	hosts = hp;
91 }
92 
93 static int
94 rusers_reply(caddr_t replyp, struct sockaddr_in *raddrp)
95 {
96 	u_int x;
97 	int idle;
98 	char date[32], idle_time[64], remote[64];
99 	struct hostent *hp;
100 	utmpidlearr *up = (utmpidlearr *)replyp;
101 	char *host;
102 	int days, hours, minutes, seconds;
103 
104 	if (search_host(raddrp->sin_addr))
105 		return (0);
106 
107 	if (!allopt && up->utmpidlearr_len == 0)
108 		return (0);
109 
110 	hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
111 	    sizeof(struct in_addr), AF_INET);
112 	if (hp != NULL)
113 		host = hp->h_name;
114 	else
115 		host = inet_ntoa(raddrp->sin_addr);
116 
117 	if (!longopt)
118 		printf("%-*s ", HOST_WIDTH, host);
119 
120 	for (x = 0; x < up->utmpidlearr_len; x++) {
121 		time_t t = _int_to_time(up->utmpidlearr_val[x].ui_utmp.ut_time);
122 		strncpy(date, &(ctime(&t)[4]), 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, "%d:%02d", minutes, seconds);
140 			if (idle >= (60 * 60))
141 				sprintf(idle_time, "%d:%02d:%02d",
142 				    hours, minutes, seconds);
143 			if (idle >= (24 * 60 * 60))
144 				sprintf(idle_time, "%d days, %d:%02d:%02d",
145 				    days, hours, minutes, seconds);
146 		}
147 
148 		strncpy(remote, up->utmpidlearr_val[x].ui_utmp.ut_host,
149 		    sizeof(remote) - 1);
150 		if (strlen(remote) != 0)
151 			sprintf(remote, "(%.16s)",
152 			    up->utmpidlearr_val[x].ui_utmp.ut_host);
153 
154 		if (longopt)
155 			printf("%-8.8s %*s:%-*.*s %-12.12s  %6s %.18s\n",
156 			    up->utmpidlearr_val[x].ui_utmp.ut_name,
157 			    HOST_WIDTH, host, LINE_WIDTH, LINE_WIDTH,
158 			    up->utmpidlearr_val[x].ui_utmp.ut_line, date,
159 			    idle_time, remote );
160 		else
161 			printf("%s ",
162 			    up->utmpidlearr_val[x].ui_utmp.ut_name);
163 	}
164 	if (!longopt)
165 		putchar('\n');
166 
167 	remember_host(raddrp->sin_addr);
168 	return (0);
169 }
170 
171 static void
172 onehost(char *host)
173 {
174 	utmpidlearr up;
175 	CLIENT *rusers_clnt;
176 	struct sockaddr_in addr;
177 	struct hostent *hp;
178 	struct timeval tv;
179 
180 	hp = gethostbyname(host);
181 	if (hp == NULL)
182 		errx(1, "unknown host \"%s\"", host);
183 
184 	rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
185 	if (rusers_clnt == NULL)
186 		errx(1, "%s", clnt_spcreateerror(""));
187 
188 	bzero((char *)&up, sizeof(up));
189 	tv.tv_sec = 15;	/* XXX ?? */
190 	tv.tv_usec = 0;
191 	if (clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
192 	    xdr_utmpidlearr, &up, tv) != RPC_SUCCESS)
193 		errx(1, "%s", clnt_sperror(rusers_clnt, ""));
194 	addr.sin_addr.s_addr = *(int *)hp->h_addr;
195 	rusers_reply((caddr_t)&up, &addr);
196 	clnt_destroy(rusers_clnt);
197 }
198 
199 static void
200 allhosts(void)
201 {
202 	utmpidlearr up;
203 	enum clnt_stat clnt_stat;
204 
205 	bzero((char *)&up, sizeof(up));
206 	clnt_stat = clnt_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
207 	    RUSERSPROC_NAMES, xdr_void, NULL, xdr_utmpidlearr, (char *)&up,
208 	    rusers_reply);
209 	if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
210 		errx(1, "%s", clnt_sperrno(clnt_stat));
211 }
212 
213 static void
214 usage(void)
215 {
216 
217 	fprintf(stderr, "usage: rusers [-la] [hosts ...]\n");
218 	exit(1);
219 }
220 
221 int
222 main(int argc, char *argv[])
223 {
224 	int ch;
225 
226 	while ((ch = getopt(argc, argv, "al")) != -1)
227 		switch (ch) {
228 		case 'a':
229 			allopt++;
230 			break;
231 		case 'l':
232 			longopt++;
233 			break;
234 		default:
235 			usage();
236 			/* NOTREACHED */
237 		}
238 
239 	setlinebuf(stdout);
240 	if (argc == optind)
241 		allhosts();
242 	else {
243 		for (; optind < argc; optind++)
244 			(void)onehost(argv[optind]);
245 	}
246 	exit(0);
247 }
248