Home | History | Annotate | Line # | Download | only in rusers
rusers.c revision 1.14
      1 /*	$NetBSD: rusers.c,v 1.14 1998/02/03 04:08:01 perry 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.14 1998/02/03 04:08:01 perry Exp $");
     34 #endif /* not lint */
     35 
     36 #include <sys/types.h>
     37 #include <sys/param.h>
     38 #include <sys/socket.h>
     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 <unistd.h>
     48 #include <utmp.h>
     49 
     50 /*
     51  * For now we only try version 2 of the protocol. The current
     52  * version is 3 (rusers.h), but only Solaris and NetBSD seem
     53  * to support it currently.
     54  */
     55 #include <rpcsvc/rnusers.h>	/* Old version */
     56 
     57 
     58 #define MAX_INT 0x7fffffff
     59 extern char *__progname;	/* from crt0.o */
     60 
     61 struct timeval timeout = { 25, 0 };
     62 int longopt;
     63 int allopt;
     64 
     65 void	allhosts __P((void));
     66 int	main __P((int, char *[]));
     67 void	onehost __P((char *));
     68 void	remember_host __P((struct in_addr));
     69 int	rusers_reply __P((char *, struct sockaddr_in *));
     70 int	search_host __P((struct in_addr));
     71 void	usage __P((void));
     72 
     73 struct host_list {
     74 	struct host_list *next;
     75 	struct in_addr addr;
     76 } *hosts;
     77 
     78 int
     79 search_host(struct in_addr addr)
     80 {
     81 	struct host_list *hp;
     82 
     83 	if (!hosts)
     84 		return(0);
     85 
     86 	for (hp = hosts; hp != NULL; hp = hp->next) {
     87 		if (hp->addr.s_addr == addr.s_addr)
     88 			return(1);
     89 	}
     90 	return(0);
     91 }
     92 
     93 void
     94 remember_host(struct in_addr addr)
     95 {
     96 	struct host_list *hp;
     97 
     98 	if (!(hp = (struct host_list *)malloc(sizeof(struct host_list))))
     99 		errx(1, "no memory");
    100 	hp->addr.s_addr = addr.s_addr;
    101 	hp->next = hosts;
    102 	hosts = hp;
    103 }
    104 
    105 int
    106 rusers_reply(char *replyp, struct sockaddr_in *raddrp)
    107 {
    108 	int x;
    109 	struct hostent *hp;
    110 	struct utmpidlearr *up = (struct utmpidlearr *)replyp;
    111 	char *host;
    112 
    113 	if (search_host(raddrp->sin_addr))
    114 		return(0);
    115 
    116 	if (!allopt && !up->uia_cnt)
    117 		return(0);
    118 
    119 	hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
    120 			   sizeof(struct in_addr), AF_INET);
    121 	if (hp)
    122 		host = hp->h_name;
    123 	else
    124 		host = inet_ntoa(raddrp->sin_addr);
    125 
    126 #define HOSTWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_host)
    127 #define LINEWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_line)
    128 #define NAMEWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_name)
    129 
    130 	if (!longopt)
    131 		printf("%-*.*s ", HOSTWID, HOSTWID, host);
    132 
    133 	for (x = 0; x < up->uia_cnt; x++) {
    134 		unsigned int minutes;
    135 		char	date[26], idle[8];
    136 		char	remote[HOSTWID + 3];		/* "(" host ")" \0 */
    137 		char	local[HOSTWID + LINEWID + 2];	/* host ":" line \0 */
    138 
    139 		if (!longopt) {
    140 			printf("%.*s ", NAMEWID,
    141 			    up->uia_arr[x]->ui_utmp.ut_name);
    142 			continue;
    143 		}
    144 
    145 		snprintf(local, sizeof(local), "%.*s:%s",
    146 		    HOSTWID, host,
    147 		    up->uia_arr[x]->ui_utmp.ut_line);
    148 
    149 		snprintf(date, sizeof(date), "%s",
    150 		    &(ctime((time_t *)&(up->uia_arr[x]->ui_utmp.ut_time)))[4]);
    151 
    152 		minutes = up->uia_arr[x]->ui_idle;
    153 		if (minutes == MAX_INT)
    154 			strcpy(idle, "??");
    155 		else if (minutes == 0)
    156 			strcpy(idle, "");
    157 		else {
    158 			unsigned int days, hours;
    159 
    160 			days = minutes / (24 * 60);
    161 			minutes %= (24 * 60);
    162 			hours = minutes / 24;
    163 			minutes %= 24;
    164 
    165 			if (days > 0)
    166 				snprintf(idle, sizeof(idle), "%d d ", days);
    167 			else if (hours > 0)
    168 				snprintf(idle, sizeof(idle), "%2d:%02d",
    169 				    hours, minutes);
    170 			else
    171 				snprintf(idle, sizeof(idle), ":%02d", minutes);
    172 		}
    173 
    174 		if (up->uia_arr[x]->ui_utmp.ut_host[0] != '\0')
    175 			snprintf(remote, sizeof(remote), "(%.*s)",
    176 			    HOSTWID, up->uia_arr[x]->ui_utmp.ut_host);
    177 		else
    178 			remote[0] = '\0';
    179 
    180 		printf("%-*.*s  %-*.*s  %-12.12s  %8.8s  %s\n",
    181 		    NAMEWID, NAMEWID, up->uia_arr[x]->ui_utmp.ut_name,
    182 		    HOSTWID+LINEWID+1, HOSTWID+LINEWID+1, local,
    183 		    date, idle, remote);
    184 	}
    185 	if (!longopt)
    186 		putchar('\n');
    187 
    188 	remember_host(raddrp->sin_addr);
    189 	return(0);
    190 }
    191 
    192 void
    193 onehost(char *host)
    194 {
    195 	struct utmpidlearr up;
    196 	CLIENT *rusers_clnt;
    197 	enum clnt_stat clnt_stat;
    198 	struct sockaddr_in addr;
    199 	struct hostent *hp;
    200 
    201 	hp = gethostbyname(host);
    202 	if (hp == NULL)
    203 		errx(1, "unknown host \"%s\"", host);
    204 
    205 	rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
    206 	if (rusers_clnt == NULL) {
    207 		clnt_pcreateerror(__progname);
    208 		exit(1);
    209 	}
    210 
    211 	memset((char *)&up, 0, sizeof(up));
    212 	clnt_stat = clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
    213 	    xdr_utmpidlearr, &up, timeout);
    214 	if (clnt_stat != RPC_SUCCESS)
    215 		errx(1, "%s", clnt_sperrno(clnt_stat));
    216 	addr.sin_addr.s_addr = *(int *)hp->h_addr;
    217 	rusers_reply((char *)&up, &addr);
    218 }
    219 
    220 void
    221 allhosts(void)
    222 {
    223 	struct utmpidlearr up;
    224 	enum clnt_stat clnt_stat;
    225 
    226 	memset((char *)&up, 0, sizeof(up));
    227 	clnt_stat = clnt_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
    228 	    RUSERSPROC_NAMES, xdr_void, NULL, xdr_utmpidlearr,
    229 	    (char *)&up, rusers_reply);
    230 	if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
    231 		errx(1, "%s", clnt_sperrno(clnt_stat));
    232 }
    233 
    234 void
    235 usage(void)
    236 {
    237 	fprintf(stderr, "Usage: %s [-la] [hosts ...]\n", __progname);
    238 	exit(1);
    239 }
    240 
    241 int
    242 main(int argc, char *argv[])
    243 {
    244 	int ch;
    245 	extern int optind;
    246 
    247 	while ((ch = getopt(argc, argv, "al")) != -1)
    248 		switch (ch) {
    249 		case 'a':
    250 			allopt++;
    251 			break;
    252 		case 'l':
    253 			longopt++;
    254 			break;
    255 		default:
    256 			usage();
    257 			/*NOTREACHED*/
    258 		}
    259 
    260 	setlinebuf(stdout);
    261 	if (argc == optind)
    262 		allhosts();
    263 	else {
    264 		for (; optind < argc; optind++)
    265 			(void) onehost(argv[optind]);
    266 	}
    267 	exit(0);
    268 }
    269