Home | History | Annotate | Line # | Download | only in rusers
rusers.c revision 1.21
      1  1.21      cgd /*	$NetBSD: rusers.c,v 1.21 2001/02/19 23:03:51 cgd Exp $	*/
      2  1.12      tls 
      3   1.1   brezak /*-
      4   1.6   brezak  *  Copyright (c) 1993 John Brezak
      5   1.6   brezak  *  All rights reserved.
      6   1.6   brezak  *
      7   1.6   brezak  *  Redistribution and use in source and binary forms, with or without
      8   1.6   brezak  *  modification, are permitted provided that the following conditions
      9   1.6   brezak  *  are met:
     10   1.6   brezak  *  1. Redistributions of source code must retain the above copyright
     11   1.6   brezak  *     notice, this list of conditions and the following disclaimer.
     12   1.6   brezak  *  2. Redistributions in binary form must reproduce the above copyright
     13   1.6   brezak  *     notice, this list of conditions and the following disclaimer in the
     14   1.6   brezak  *     documentation and/or other materials provided with the distribution.
     15   1.6   brezak  *  3. The name of the author may not be used to endorse or promote products
     16   1.6   brezak  *     derived from this software without specific prior written permission.
     17   1.6   brezak  *
     18   1.6   brezak  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
     19   1.6   brezak  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20   1.6   brezak  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21   1.6   brezak  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     22   1.6   brezak  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     23   1.6   brezak  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24   1.6   brezak  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25   1.6   brezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     26   1.6   brezak  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     27   1.6   brezak  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28   1.6   brezak  * POSSIBILITY OF SUCH DAMAGE.
     29   1.1   brezak  */
     30   1.1   brezak 
     31  1.13    lukem #include <sys/cdefs.h>
     32   1.4  mycroft #ifndef lint
     33  1.21      cgd __RCSID("$NetBSD: rusers.c,v 1.21 2001/02/19 23:03:51 cgd Exp $");
     34   1.4  mycroft #endif /* not lint */
     35   1.4  mycroft 
     36   1.1   brezak #include <sys/types.h>
     37   1.1   brezak #include <sys/socket.h>
     38  1.15   kleink 
     39  1.13    lukem #include <rpc/rpc.h>
     40  1.13    lukem #include <arpa/inet.h>
     41  1.14    perry 
     42  1.13    lukem #include <err.h>
     43   1.1   brezak #include <netdb.h>
     44   1.1   brezak #include <stdio.h>
     45  1.13    lukem #include <stdlib.h>
     46  1.14    perry #include <string.h>
     47  1.15   kleink #include <time.h>
     48  1.14    perry #include <unistd.h>
     49   1.6   brezak #include <utmp.h>
     50   1.6   brezak 
     51   1.6   brezak /*
     52   1.6   brezak  * For now we only try version 2 of the protocol. The current
     53   1.9  deraadt  * version is 3 (rusers.h), but only Solaris and NetBSD seem
     54   1.6   brezak  * to support it currently.
     55   1.6   brezak  */
     56   1.6   brezak #include <rpcsvc/rnusers.h>	/* Old version */
     57   1.1   brezak 
     58  1.13    lukem 
     59   1.1   brezak #define MAX_INT 0x7fffffff
     60   1.1   brezak 
     61  1.10      jtc struct timeval timeout = { 25, 0 };
     62   1.1   brezak int longopt;
     63   1.1   brezak int allopt;
     64   1.1   brezak 
     65  1.19     fvdl void	allhosts(void);
     66  1.19     fvdl int	main(int, char *[]);
     67  1.19     fvdl void	onehost(char *);
     68  1.19     fvdl void	remember_host(struct sockaddr *);
     69  1.19     fvdl int	rusers_reply(char *, struct netbuf *, struct netconfig *);
     70  1.19     fvdl int	search_host(struct sockaddr *);
     71  1.19     fvdl void	usage(void);
     72  1.13    lukem 
     73   1.1   brezak struct host_list {
     74   1.1   brezak 	struct host_list *next;
     75  1.19     fvdl 	int family;
     76  1.19     fvdl 	union {
     77  1.19     fvdl 		struct in6_addr _addr6;
     78  1.19     fvdl 		struct in_addr _addr4;
     79  1.19     fvdl 	} addr;
     80   1.1   brezak } *hosts;
     81   1.1   brezak 
     82  1.19     fvdl #define addr6 addr._addr6
     83  1.19     fvdl #define addr4 addr._addr4
     84  1.19     fvdl 
     85   1.5  deraadt int
     86  1.19     fvdl search_host(struct sockaddr *sa)
     87   1.1   brezak {
     88   1.1   brezak 	struct host_list *hp;
     89   1.1   brezak 
     90   1.1   brezak 	if (!hosts)
     91   1.1   brezak 		return(0);
     92   1.1   brezak 
     93   1.1   brezak 	for (hp = hosts; hp != NULL; hp = hp->next) {
     94  1.19     fvdl 		switch (hp->family) {
     95  1.19     fvdl 		case AF_INET6:
     96  1.19     fvdl 			if (!memcmp(&hp->addr6,
     97  1.19     fvdl 			    &((struct sockaddr_in6 *)sa)->sin6_addr,
     98  1.19     fvdl 			    sizeof (struct in6_addr)))
     99  1.19     fvdl 				return 1;
    100  1.19     fvdl 			break;
    101  1.19     fvdl 		case AF_INET:
    102  1.19     fvdl 			if (!memcmp(&hp->addr4,
    103  1.19     fvdl 			    &((struct sockaddr_in *)sa)->sin_addr,
    104  1.19     fvdl 			    sizeof (struct in_addr)))
    105  1.19     fvdl 				return 1;
    106  1.19     fvdl 			break;
    107  1.19     fvdl 		default:
    108  1.20      cgd 			break;
    109  1.19     fvdl 		}
    110   1.1   brezak 	}
    111   1.1   brezak 	return(0);
    112   1.1   brezak }
    113   1.1   brezak 
    114   1.5  deraadt void
    115  1.19     fvdl remember_host(struct sockaddr *sa)
    116   1.1   brezak {
    117   1.1   brezak 	struct host_list *hp;
    118   1.1   brezak 
    119  1.19     fvdl 	if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
    120  1.19     fvdl 		err(1, "malloc");
    121  1.19     fvdl 		/* NOTREACHED */
    122  1.19     fvdl 	}
    123  1.19     fvdl 	hp->family = sa->sa_family;
    124   1.1   brezak 	hp->next = hosts;
    125  1.19     fvdl 	switch (sa->sa_family) {
    126  1.19     fvdl 	case AF_INET6:
    127  1.19     fvdl 		memcpy(&hp->addr6, &((struct sockaddr_in6 *)sa)->sin6_addr,
    128  1.19     fvdl 		    sizeof (struct in6_addr));
    129  1.19     fvdl 		break;
    130  1.19     fvdl 	case AF_INET:
    131  1.19     fvdl 		memcpy(&hp->addr4, &((struct sockaddr_in *)sa)->sin_addr,
    132  1.19     fvdl 		    sizeof (struct in_addr));
    133  1.19     fvdl 		break;
    134  1.19     fvdl 	default:
    135  1.19     fvdl 		err(1, "unknown address family");
    136  1.19     fvdl 		/* NOTREACHED */
    137  1.19     fvdl 	}
    138   1.1   brezak 	hosts = hp;
    139   1.1   brezak }
    140   1.1   brezak 
    141   1.6   brezak int
    142  1.19     fvdl rusers_reply(char *replyp, struct netbuf *raddrp, struct netconfig *nconf)
    143   1.5  deraadt {
    144  1.19     fvdl 	char host[NI_MAXHOST];
    145  1.13    lukem 	int x;
    146   1.6   brezak 	struct utmpidlearr *up = (struct utmpidlearr *)replyp;
    147  1.19     fvdl 	struct sockaddr *sa = raddrp->buf;
    148   1.5  deraadt 
    149  1.19     fvdl 	if (search_host(sa))
    150   1.1   brezak 		return(0);
    151   1.1   brezak 
    152   1.6   brezak 	if (!allopt && !up->uia_cnt)
    153   1.5  deraadt 		return(0);
    154  1.19     fvdl 
    155  1.19     fvdl 	if (getnameinfo(sa, sa->sa_len, host, sizeof host, NULL, 0, 0))
    156  1.19     fvdl 		return 0;
    157  1.19     fvdl 
    158  1.13    lukem #define HOSTWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_host)
    159  1.13    lukem #define LINEWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_line)
    160  1.13    lukem #define NAMEWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_name)
    161  1.13    lukem 
    162   1.5  deraadt 	if (!longopt)
    163  1.13    lukem 		printf("%-*.*s ", HOSTWID, HOSTWID, host);
    164   1.5  deraadt 
    165   1.6   brezak 	for (x = 0; x < up->uia_cnt; x++) {
    166  1.13    lukem 		unsigned int minutes;
    167  1.13    lukem 		char	date[26], idle[8];
    168  1.13    lukem 		char	remote[HOSTWID + 3];		/* "(" host ")" \0 */
    169  1.13    lukem 		char	local[HOSTWID + LINEWID + 2];	/* host ":" line \0 */
    170  1.13    lukem 
    171  1.13    lukem 		if (!longopt) {
    172  1.13    lukem 			printf("%.*s ", NAMEWID,
    173  1.13    lukem 			    up->uia_arr[x]->ui_utmp.ut_name);
    174  1.13    lukem 			continue;
    175  1.13    lukem 		}
    176  1.13    lukem 
    177  1.13    lukem 		snprintf(local, sizeof(local), "%.*s:%s",
    178  1.13    lukem 		    HOSTWID, host,
    179  1.13    lukem 		    up->uia_arr[x]->ui_utmp.ut_line);
    180  1.13    lukem 
    181  1.13    lukem 		snprintf(date, sizeof(date), "%s",
    182  1.13    lukem 		    &(ctime((time_t *)&(up->uia_arr[x]->ui_utmp.ut_time)))[4]);
    183  1.13    lukem 
    184  1.13    lukem 		minutes = up->uia_arr[x]->ui_idle;
    185  1.13    lukem 		if (minutes == MAX_INT)
    186  1.13    lukem 			strcpy(idle, "??");
    187  1.13    lukem 		else if (minutes == 0)
    188  1.13    lukem 			strcpy(idle, "");
    189   1.5  deraadt 		else {
    190  1.13    lukem 			unsigned int days, hours;
    191  1.13    lukem 
    192  1.13    lukem 			days = minutes / (24 * 60);
    193  1.13    lukem 			minutes %= (24 * 60);
    194  1.16  hubertf 			hours = minutes / 60;
    195  1.16  hubertf 			minutes %= 60;
    196  1.13    lukem 
    197  1.13    lukem 			if (days > 0)
    198  1.13    lukem 				snprintf(idle, sizeof(idle), "%d d ", days);
    199  1.13    lukem 			else if (hours > 0)
    200  1.13    lukem 				snprintf(idle, sizeof(idle), "%2d:%02d",
    201  1.13    lukem 				    hours, minutes);
    202  1.13    lukem 			else
    203  1.13    lukem 				snprintf(idle, sizeof(idle), ":%02d", minutes);
    204   1.5  deraadt 		}
    205   1.5  deraadt 
    206  1.13    lukem 		if (up->uia_arr[x]->ui_utmp.ut_host[0] != '\0')
    207  1.13    lukem 			snprintf(remote, sizeof(remote), "(%.*s)",
    208  1.13    lukem 			    HOSTWID, up->uia_arr[x]->ui_utmp.ut_host);
    209  1.13    lukem 		else
    210  1.13    lukem 			remote[0] = '\0';
    211  1.13    lukem 
    212  1.13    lukem 		printf("%-*.*s  %-*.*s  %-12.12s  %8.8s  %s\n",
    213  1.13    lukem 		    NAMEWID, NAMEWID, up->uia_arr[x]->ui_utmp.ut_name,
    214  1.13    lukem 		    HOSTWID+LINEWID+1, HOSTWID+LINEWID+1, local,
    215  1.13    lukem 		    date, idle, remote);
    216   1.5  deraadt 	}
    217   1.5  deraadt 	if (!longopt)
    218   1.5  deraadt 		putchar('\n');
    219   1.5  deraadt 
    220  1.19     fvdl 	remember_host(sa);
    221   1.1   brezak 	return(0);
    222   1.1   brezak }
    223   1.1   brezak 
    224   1.6   brezak void
    225   1.6   brezak onehost(char *host)
    226   1.1   brezak {
    227   1.6   brezak 	struct utmpidlearr up;
    228   1.5  deraadt 	CLIENT *rusers_clnt;
    229  1.13    lukem 	enum clnt_stat clnt_stat;
    230  1.19     fvdl 	struct netbuf nb;
    231  1.19     fvdl 	struct addrinfo *ai;
    232  1.19     fvdl 	int ecode;
    233   1.5  deraadt 
    234   1.5  deraadt 	rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
    235   1.5  deraadt 	if (rusers_clnt == NULL) {
    236  1.21      cgd 		clnt_pcreateerror(getprogname());
    237   1.5  deraadt 		exit(1);
    238   1.5  deraadt 	}
    239   1.1   brezak 
    240  1.19     fvdl 	ecode = getaddrinfo(host, NULL, NULL, &ai);
    241  1.19     fvdl 	if (ecode != 0)
    242  1.19     fvdl 		err(1, "%s", gai_strerror(ecode));
    243  1.19     fvdl 
    244  1.13    lukem 	memset((char *)&up, 0, sizeof(up));
    245  1.13    lukem 	clnt_stat = clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
    246  1.13    lukem 	    xdr_utmpidlearr, &up, timeout);
    247  1.13    lukem 	if (clnt_stat != RPC_SUCCESS)
    248  1.13    lukem 		errx(1, "%s", clnt_sperrno(clnt_stat));
    249  1.19     fvdl 	nb.buf = ai->ai_addr;
    250  1.19     fvdl 	nb.len = nb.maxlen = ai->ai_addrlen;
    251  1.19     fvdl 	rusers_reply((char *)&up, &nb, NULL);
    252  1.19     fvdl 	freeaddrinfo(ai);
    253   1.1   brezak }
    254   1.1   brezak 
    255   1.6   brezak void
    256   1.6   brezak allhosts(void)
    257   1.1   brezak {
    258   1.6   brezak 	struct utmpidlearr up;
    259   1.1   brezak 	enum clnt_stat clnt_stat;
    260   1.1   brezak 
    261  1.13    lukem 	memset((char *)&up, 0, sizeof(up));
    262  1.19     fvdl 	clnt_stat = rpc_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
    263   1.5  deraadt 	    RUSERSPROC_NAMES, xdr_void, NULL, xdr_utmpidlearr,
    264  1.19     fvdl 	    (char *)&up, (resultproc_t)rusers_reply, "udp");
    265  1.13    lukem 	if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
    266  1.13    lukem 		errx(1, "%s", clnt_sperrno(clnt_stat));
    267   1.1   brezak }
    268   1.1   brezak 
    269  1.11      jtc void
    270  1.11      jtc usage(void)
    271   1.1   brezak {
    272  1.21      cgd 	fprintf(stderr, "Usage: %s [-la] [hosts ...]\n", getprogname());
    273   1.5  deraadt 	exit(1);
    274   1.1   brezak }
    275   1.1   brezak 
    276  1.11      jtc int
    277  1.11      jtc main(int argc, char *argv[])
    278   1.1   brezak {
    279   1.5  deraadt 	int ch;
    280   1.5  deraadt 
    281   1.5  deraadt 	while ((ch = getopt(argc, argv, "al")) != -1)
    282   1.5  deraadt 		switch (ch) {
    283   1.5  deraadt 		case 'a':
    284   1.5  deraadt 			allopt++;
    285   1.5  deraadt 			break;
    286   1.5  deraadt 		case 'l':
    287   1.5  deraadt 			longopt++;
    288   1.5  deraadt 			break;
    289   1.5  deraadt 		default:
    290   1.5  deraadt 			usage();
    291   1.5  deraadt 			/*NOTREACHED*/
    292   1.5  deraadt 		}
    293   1.3   brezak 
    294   1.5  deraadt 	setlinebuf(stdout);
    295   1.1   brezak 	if (argc == optind)
    296   1.1   brezak 		allhosts();
    297   1.1   brezak 	else {
    298   1.1   brezak 		for (; optind < argc; optind++)
    299   1.1   brezak 			(void) onehost(argv[optind]);
    300   1.1   brezak 	}
    301   1.5  deraadt 	exit(0);
    302   1.1   brezak }
    303