Home | History | Annotate | Line # | Download | only in rusers
rusers.c revision 1.20
      1 /*	$NetBSD: rusers.c,v 1.20 2001/01/16 02:43:37 cgd 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.20 2001/01/16 02:43:37 cgd 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(void);
     67 int	main(int, char *[]);
     68 void	onehost(char *);
     69 void	remember_host(struct sockaddr *);
     70 int	rusers_reply(char *, struct netbuf *, struct netconfig *);
     71 int	search_host(struct sockaddr *);
     72 void	usage(void);
     73 
     74 struct host_list {
     75 	struct host_list *next;
     76 	int family;
     77 	union {
     78 		struct in6_addr _addr6;
     79 		struct in_addr _addr4;
     80 	} addr;
     81 } *hosts;
     82 
     83 #define addr6 addr._addr6
     84 #define addr4 addr._addr4
     85 
     86 int
     87 search_host(struct sockaddr *sa)
     88 {
     89 	struct host_list *hp;
     90 
     91 	if (!hosts)
     92 		return(0);
     93 
     94 	for (hp = hosts; hp != NULL; hp = hp->next) {
     95 		switch (hp->family) {
     96 		case AF_INET6:
     97 			if (!memcmp(&hp->addr6,
     98 			    &((struct sockaddr_in6 *)sa)->sin6_addr,
     99 			    sizeof (struct in6_addr)))
    100 				return 1;
    101 			break;
    102 		case AF_INET:
    103 			if (!memcmp(&hp->addr4,
    104 			    &((struct sockaddr_in *)sa)->sin_addr,
    105 			    sizeof (struct in_addr)))
    106 				return 1;
    107 			break;
    108 		default:
    109 			break;
    110 		}
    111 	}
    112 	return(0);
    113 }
    114 
    115 void
    116 remember_host(struct sockaddr *sa)
    117 {
    118 	struct host_list *hp;
    119 
    120 	if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
    121 		err(1, "malloc");
    122 		/* NOTREACHED */
    123 	}
    124 	hp->family = sa->sa_family;
    125 	hp->next = hosts;
    126 	switch (sa->sa_family) {
    127 	case AF_INET6:
    128 		memcpy(&hp->addr6, &((struct sockaddr_in6 *)sa)->sin6_addr,
    129 		    sizeof (struct in6_addr));
    130 		break;
    131 	case AF_INET:
    132 		memcpy(&hp->addr4, &((struct sockaddr_in *)sa)->sin_addr,
    133 		    sizeof (struct in_addr));
    134 		break;
    135 	default:
    136 		err(1, "unknown address family");
    137 		/* NOTREACHED */
    138 	}
    139 	hosts = hp;
    140 }
    141 
    142 int
    143 rusers_reply(char *replyp, struct netbuf *raddrp, struct netconfig *nconf)
    144 {
    145 	char host[NI_MAXHOST];
    146 	int x;
    147 	struct utmpidlearr *up = (struct utmpidlearr *)replyp;
    148 	struct sockaddr *sa = raddrp->buf;
    149 
    150 	if (search_host(sa))
    151 		return(0);
    152 
    153 	if (!allopt && !up->uia_cnt)
    154 		return(0);
    155 
    156 	if (getnameinfo(sa, sa->sa_len, host, sizeof host, NULL, 0, 0))
    157 		return 0;
    158 
    159 #define HOSTWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_host)
    160 #define LINEWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_line)
    161 #define NAMEWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_name)
    162 
    163 	if (!longopt)
    164 		printf("%-*.*s ", HOSTWID, HOSTWID, host);
    165 
    166 	for (x = 0; x < up->uia_cnt; x++) {
    167 		unsigned int minutes;
    168 		char	date[26], idle[8];
    169 		char	remote[HOSTWID + 3];		/* "(" host ")" \0 */
    170 		char	local[HOSTWID + LINEWID + 2];	/* host ":" line \0 */
    171 
    172 		if (!longopt) {
    173 			printf("%.*s ", NAMEWID,
    174 			    up->uia_arr[x]->ui_utmp.ut_name);
    175 			continue;
    176 		}
    177 
    178 		snprintf(local, sizeof(local), "%.*s:%s",
    179 		    HOSTWID, host,
    180 		    up->uia_arr[x]->ui_utmp.ut_line);
    181 
    182 		snprintf(date, sizeof(date), "%s",
    183 		    &(ctime((time_t *)&(up->uia_arr[x]->ui_utmp.ut_time)))[4]);
    184 
    185 		minutes = up->uia_arr[x]->ui_idle;
    186 		if (minutes == MAX_INT)
    187 			strcpy(idle, "??");
    188 		else if (minutes == 0)
    189 			strcpy(idle, "");
    190 		else {
    191 			unsigned int days, hours;
    192 
    193 			days = minutes / (24 * 60);
    194 			minutes %= (24 * 60);
    195 			hours = minutes / 60;
    196 			minutes %= 60;
    197 
    198 			if (days > 0)
    199 				snprintf(idle, sizeof(idle), "%d d ", days);
    200 			else if (hours > 0)
    201 				snprintf(idle, sizeof(idle), "%2d:%02d",
    202 				    hours, minutes);
    203 			else
    204 				snprintf(idle, sizeof(idle), ":%02d", minutes);
    205 		}
    206 
    207 		if (up->uia_arr[x]->ui_utmp.ut_host[0] != '\0')
    208 			snprintf(remote, sizeof(remote), "(%.*s)",
    209 			    HOSTWID, up->uia_arr[x]->ui_utmp.ut_host);
    210 		else
    211 			remote[0] = '\0';
    212 
    213 		printf("%-*.*s  %-*.*s  %-12.12s  %8.8s  %s\n",
    214 		    NAMEWID, NAMEWID, up->uia_arr[x]->ui_utmp.ut_name,
    215 		    HOSTWID+LINEWID+1, HOSTWID+LINEWID+1, local,
    216 		    date, idle, remote);
    217 	}
    218 	if (!longopt)
    219 		putchar('\n');
    220 
    221 	remember_host(sa);
    222 	return(0);
    223 }
    224 
    225 void
    226 onehost(char *host)
    227 {
    228 	struct utmpidlearr up;
    229 	CLIENT *rusers_clnt;
    230 	enum clnt_stat clnt_stat;
    231 	struct netbuf nb;
    232 	struct addrinfo *ai;
    233 	int ecode;
    234 
    235 	rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
    236 	if (rusers_clnt == NULL) {
    237 		clnt_pcreateerror(__progname);
    238 		exit(1);
    239 	}
    240 
    241 	ecode = getaddrinfo(host, NULL, NULL, &ai);
    242 	if (ecode != 0)
    243 		err(1, "%s", gai_strerror(ecode));
    244 
    245 	memset((char *)&up, 0, sizeof(up));
    246 	clnt_stat = clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
    247 	    xdr_utmpidlearr, &up, timeout);
    248 	if (clnt_stat != RPC_SUCCESS)
    249 		errx(1, "%s", clnt_sperrno(clnt_stat));
    250 	nb.buf = ai->ai_addr;
    251 	nb.len = nb.maxlen = ai->ai_addrlen;
    252 	rusers_reply((char *)&up, &nb, NULL);
    253 	freeaddrinfo(ai);
    254 }
    255 
    256 void
    257 allhosts(void)
    258 {
    259 	struct utmpidlearr up;
    260 	enum clnt_stat clnt_stat;
    261 
    262 	memset((char *)&up, 0, sizeof(up));
    263 	clnt_stat = rpc_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
    264 	    RUSERSPROC_NAMES, xdr_void, NULL, xdr_utmpidlearr,
    265 	    (char *)&up, (resultproc_t)rusers_reply, "udp");
    266 	if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
    267 		errx(1, "%s", clnt_sperrno(clnt_stat));
    268 }
    269 
    270 void
    271 usage(void)
    272 {
    273 	fprintf(stderr, "Usage: %s [-la] [hosts ...]\n", __progname);
    274 	exit(1);
    275 }
    276 
    277 int
    278 main(int argc, char *argv[])
    279 {
    280 	int ch;
    281 
    282 	while ((ch = getopt(argc, argv, "al")) != -1)
    283 		switch (ch) {
    284 		case 'a':
    285 			allopt++;
    286 			break;
    287 		case 'l':
    288 			longopt++;
    289 			break;
    290 		default:
    291 			usage();
    292 			/*NOTREACHED*/
    293 		}
    294 
    295 	setlinebuf(stdout);
    296 	if (argc == optind)
    297 		allhosts();
    298 	else {
    299 		for (; optind < argc; optind++)
    300 			(void) onehost(argv[optind]);
    301 	}
    302 	exit(0);
    303 }
    304