rusers.c revision 85638
152419Sjulian/*-
252419Sjulian * Copyright (c) 1993, John Brezak
352419Sjulian * All rights reserved.
4139823Simp *
5139823Simp * Redistribution and use in source and binary forms, with or without
6139823Simp * modification, are permitted provided that the following conditions
752419Sjulian * are met:
852419Sjulian * 1. Redistributions of source code must retain the above copyright
952419Sjulian *    notice, this list of conditions and the following disclaimer.
1052419Sjulian * 2. Redistributions in binary form must reproduce the above copyright
1152419Sjulian *    notice, this list of conditions and the following disclaimer in the
1252419Sjulian *    documentation and/or other materials provided with the distribution.
1352419Sjulian * 3. All advertising materials mentioning features or use of this software
1452419Sjulian *    must display the following acknowledgement:
1552419Sjulian *	This product includes software developed by the University of
1652419Sjulian *	California, Berkeley and its contributors.
1752419Sjulian * 4. Neither the name of the University nor the names of its contributors
1852419Sjulian *    may be used to endorse or promote products derived from this software
1952419Sjulian *    without specific prior written permission.
2052419Sjulian *
2152419Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2252419Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2352419Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2452419Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2552419Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2652419Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2752419Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2852419Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2952419Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3052419Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3152419Sjulian * SUCH DAMAGE.
3252419Sjulian */
3352419Sjulian
3452419Sjulian#ifndef lint
3552419Sjulianstatic const char rcsid[] =
3652419Sjulian  "$FreeBSD: head/usr.bin/rusers/rusers.c 85638 2001-10-28 20:24:51Z dillon $";
3752419Sjulian#endif /* not lint */
3852419Sjulian
3967506Sjulian#include <sys/types.h>
4052419Sjulian#include <sys/socket.h>
4152419Sjulian#include <rpc/rpc.h>
4252419Sjulian#include <rpc/pmap_clnt.h>
4352419Sjulian#include <rpcsvc/rnusers.h>
4452419Sjulian#include <arpa/inet.h>
4552419Sjulian#include <err.h>
4652419Sjulian#include <netdb.h>
4752419Sjulian#include <stdio.h>
4852419Sjulian#include <stdlib.h>
4952419Sjulian#include <strings.h>
50141756Sglebius#include <unistd.h>
5152419Sjulian
5252419Sjulian#define MAX_INT		0x7fffffff
5362143Sarchie#define HOST_WIDTH	20
5462143Sarchie#define LINE_WIDTH	15
5562143Sarchie
5662143Sarchieint longopt;
5752419Sjulianint allopt;
5862143Sarchie
5953913Sarchiestruct host_list {
6062143Sarchie	struct	host_list *next;
6162143Sarchie	struct	in_addr addr;
6264358Sarchie} *hosts;
6364653Sarchie
6464653Sarchieint
6564358Sarchiesearch_host(struct in_addr addr)
6664653Sarchie{
6764358Sarchie	struct host_list *hp;
68141721Sglebius
69141721Sglebius	if (hosts == NULL)
70141910Sglebius		return (0);
7153913Sarchie
7253913Sarchie	for (hp = hosts; hp != NULL; hp = hp->next) {
7352419Sjulian		if (hp->addr.s_addr == addr.s_addr)
74			return (1);
75	}
76	return (0);
77}
78
79void
80remember_host(struct in_addr addr)
81{
82	struct host_list *hp;
83
84	if ((hp = (struct host_list *)malloc(sizeof(struct host_list))) == NULL)
85		errx(1, "no memory");
86	hp->addr.s_addr = addr.s_addr;
87	hp->next = hosts;
88	hosts = hp;
89}
90
91int
92rusers_reply(caddr_t replyp, struct sockaddr_in *raddrp)
93{
94	int x, idle;
95	char date[32], idle_time[64], remote[64];
96	struct hostent *hp;
97	utmpidlearr *up = (utmpidlearr *)replyp;
98	char *host;
99	int days, hours, minutes, seconds;
100
101	if (search_host(raddrp->sin_addr))
102		return (0);
103
104	if (!allopt && up->utmpidlearr_len == 0)
105		return (0);
106
107	hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
108	    sizeof(struct in_addr), AF_INET);
109	if (hp != NULL)
110		host = hp->h_name;
111	else
112		host = inet_ntoa(raddrp->sin_addr);
113
114	if (!longopt)
115		printf("%-*s ", HOST_WIDTH, host);
116
117	for (x = 0; x < up->utmpidlearr_len; x++) {
118		time_t t = int_to_time(up->utmpidlearr_val[x].ui_utmp.ut_time);
119		strncpy(date, &(ctime(&t)[4]), sizeof(date) - 1);
120
121		idle = up->utmpidlearr_val[x].ui_idle;
122		sprintf(idle_time, "  :%02d", idle);
123		if (idle == MAX_INT)
124			strcpy(idle_time, "??");
125		else if (idle == 0)
126			strcpy(idle_time, "");
127		else {
128			seconds = idle;
129			days = seconds / (60 * 60 * 24);
130			seconds %= (60 * 60 * 24);
131			hours = seconds / (60 * 60);
132			seconds %= (60 * 60);
133			minutes = seconds / 60;
134			seconds %= 60;
135			if (idle > 60)
136				sprintf(idle_time, "%d:%02d", minutes, seconds);
137			if (idle >= (60 * 60))
138				sprintf(idle_time, "%d:%02d:%02d",
139				    hours, minutes, seconds);
140			if (idle >= (24 * 60 * 60))
141				sprintf(idle_time, "%d days, %d:%02d:%02d",
142				    days, hours, minutes, seconds);
143		}
144
145		strncpy(remote, up->utmpidlearr_val[x].ui_utmp.ut_host,
146		    sizeof(remote) - 1);
147		if (strlen(remote) != 0)
148			sprintf(remote, "(%.16s)",
149			    up->utmpidlearr_val[x].ui_utmp.ut_host);
150
151		if (longopt)
152			printf("%-8.8s %*s:%-*.*s %-12.12s  %6s %.18s\n",
153			    up->utmpidlearr_val[x].ui_utmp.ut_name,
154			    HOST_WIDTH, host, LINE_WIDTH, LINE_WIDTH,
155			    up->utmpidlearr_val[x].ui_utmp.ut_line, date,
156			    idle_time, remote );
157		else
158			printf("%s ",
159			    up->utmpidlearr_val[x].ui_utmp.ut_name);
160	}
161	if (!longopt)
162		putchar('\n');
163
164	remember_host(raddrp->sin_addr);
165	return (0);
166}
167
168void
169onehost(char *host)
170{
171	utmpidlearr up;
172	CLIENT *rusers_clnt;
173	struct sockaddr_in addr;
174	struct hostent *hp;
175	struct timeval tv;
176
177	hp = gethostbyname(host);
178	if (hp == NULL)
179		errx(1, "unknown host \"%s\"", host);
180
181	rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
182	if (rusers_clnt == NULL)
183		errx(1, "%s", clnt_spcreateerror(""));
184
185	bzero((char *)&up, sizeof(up));
186	tv.tv_sec = 15;	/* XXX ?? */
187	tv.tv_usec = 0;
188	if (clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
189	    xdr_utmpidlearr, &up, tv) != RPC_SUCCESS)
190		errx(1, "%s", clnt_sperror(rusers_clnt, ""));
191	addr.sin_addr.s_addr = *(int *)hp->h_addr;
192	rusers_reply((caddr_t)&up, &addr);
193	clnt_destroy(rusers_clnt);
194}
195
196void
197allhosts()
198{
199	utmpidlearr up;
200	enum clnt_stat clnt_stat;
201
202	bzero((char *)&up, sizeof(up));
203	clnt_stat = clnt_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
204	    RUSERSPROC_NAMES, xdr_void, NULL, xdr_utmpidlearr, (char *)&up,
205	    rusers_reply);
206	if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
207		errx(1, "%s", clnt_sperrno(clnt_stat));
208}
209
210static void
211usage()
212{
213
214	fprintf(stderr, "usage: rusers [-la] [hosts ...]\n");
215	exit(1);
216}
217
218int
219main(int argc, char *argv[])
220{
221	int ch;
222
223	while ((ch = getopt(argc, argv, "al")) != -1)
224		switch (ch) {
225		case 'a':
226			allopt++;
227			break;
228		case 'l':
229			longopt++;
230			break;
231		default:
232			usage();
233			/* NOTREACHED */
234		}
235
236	setlinebuf(stdout);
237	if (argc == optind)
238		allhosts();
239	else {
240		for (; optind < argc; optind++)
241			(void)onehost(argv[optind]);
242	}
243	exit(0);
244}
245