Home | History | Annotate | Line # | Download | only in rusers
rusers.c revision 1.6
      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. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #ifndef lint
     30 static char rcsid[] = "$Id: rusers.c,v 1.6 1993/11/21 19:01:02 brezak Exp $";
     31 #endif /* not lint */
     32 
     33 #include <sys/types.h>
     34 #include <sys/param.h>
     35 #include <sys/socket.h>
     36 #include <netdb.h>
     37 #include <stdio.h>
     38 #include <strings.h>
     39 #include <rpc/rpc.h>
     40 #include <arpa/inet.h>
     41 #include <utmp.h>
     42 #include <stdlib.h>
     43 
     44 /*
     45  * For now we only try version 2 of the protocol. The current
     46  * version is 3 (rusers.h), but only Solairis and NetBSD seem
     47  * to support it currently.
     48  */
     49 #include <rpcsvc/rnusers.h>	/* Old version */
     50 
     51 #define MAX_INT 0x7fffffff
     52 #define HOST_WIDTH 20
     53 #define LINE_WIDTH 8
     54 char *argv0;
     55 
     56 int longopt;
     57 int allopt;
     58 
     59 struct host_list {
     60 	struct host_list *next;
     61 	struct in_addr addr;
     62 } *hosts;
     63 
     64 int
     65 search_host(struct in_addr addr)
     66 {
     67 	struct host_list *hp;
     68 
     69 	if (!hosts)
     70 		return(0);
     71 
     72 	for (hp = hosts; hp != NULL; hp = hp->next) {
     73 		if (hp->addr.s_addr == addr.s_addr)
     74 			return(1);
     75 	}
     76 	return(0);
     77 }
     78 
     79 void
     80 remember_host(struct in_addr addr)
     81 {
     82 	struct host_list *hp;
     83 
     84 	if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
     85 		fprintf(stderr, "%s: no memory.\n", argv0);
     86 		exit(1);
     87 	}
     88 	hp->addr.s_addr = addr.s_addr;
     89 	hp->next = hosts;
     90 	hosts = hp;
     91 }
     92 
     93 int
     94 rusers_reply(char *replyp, struct sockaddr_in *raddrp)
     95 {
     96 	int x, idle;
     97 	char date[32], idle_time[64], remote[64];
     98 	struct hostent *hp;
     99 	struct utmpidlearr *up = (struct utmpidlearr *)replyp;
    100 	char *host;
    101 	int days, hours, minutes, seconds;
    102 
    103 	if (search_host(raddrp->sin_addr))
    104 		return(0);
    105 
    106 	if (!allopt && !up->uia_cnt)
    107 		return(0);
    108 
    109 	hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
    110 			   sizeof(struct in_addr), AF_INET);
    111 	if (hp)
    112 		host = hp->h_name;
    113 	else
    114 		host = inet_ntoa(raddrp->sin_addr);
    115 
    116 	if (!longopt)
    117 		printf("%-*.*s ", HOST_WIDTH, HOST_WIDTH, host);
    118 
    119 	for (x = 0; x < up->uia_cnt; x++) {
    120 		strncpy(date,
    121 			&(ctime((time_t *)&(up->uia_arr[x]->ui_utmp.ut_time))[4]),
    122 			sizeof(date)-1);
    123 
    124 		idle = up->uia_arr[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, "%2d:%02d",
    140 					minutes, seconds);
    141 			if (idle >= (60*60))
    142 				sprintf(idle_time, "%2d:%02d:%02d",
    143 					hours, minutes, seconds);
    144 			if (idle >= (24*60*60))
    145 				sprintf(idle_time, "%d days, %d:%02d:%02d",
    146 					days, hours, minutes, seconds);
    147 		}
    148 
    149 		strncpy(remote, up->uia_arr[x]->ui_utmp.ut_host,
    150 		    sizeof(remote)-1);
    151 		if (strlen(remote) != 0)
    152 			sprintf(remote, "(%.16s)",
    153 			    up->uia_arr[x]->ui_utmp.ut_host);
    154 
    155 		if (longopt)
    156 			printf("%-8.8s %-*.*s:%-*.*s %-12.12s %8s %.18s\n",
    157 			    up->uia_arr[x]->ui_utmp.ut_name,
    158 			    HOST_WIDTH, HOST_WIDTH, host,
    159 			    LINE_WIDTH, LINE_WIDTH,
    160 			    up->uia_arr[x]->ui_utmp.ut_line,
    161 			    date,
    162 			    idle_time,
    163 			    remote);
    164 		else
    165 			printf("%s ",
    166 			    up->uia_arr[x]->ui_utmp.ut_name);
    167 	}
    168 	if (!longopt)
    169 		putchar('\n');
    170 
    171 	remember_host(raddrp->sin_addr);
    172 	return(0);
    173 }
    174 
    175 void
    176 onehost(char *host)
    177 {
    178 	struct utmpidlearr up;
    179 	CLIENT *rusers_clnt;
    180 	struct sockaddr_in addr;
    181 	struct hostent *hp;
    182 
    183 	hp = gethostbyname(host);
    184 	if (hp == NULL) {
    185 		fprintf(stderr, "%s: unknown host \"%s\"\n",
    186 			argv0, host);
    187 		exit(1);
    188 	}
    189 
    190 	rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
    191 	if (rusers_clnt == NULL) {
    192 		clnt_pcreateerror(argv0);
    193 		exit(1);
    194 	}
    195 
    196 	bzero((char *)&up, sizeof(up));
    197 	if (clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
    198 	    xdr_utmpidlearr, &up, NULL) != RPC_SUCCESS) {
    199 		clnt_perror(rusers_clnt, argv0);
    200 		exit(1);
    201 	}
    202 	addr.sin_addr.s_addr = *(int *)hp->h_addr;
    203 	rusers_reply((char *)&up, &addr);
    204 }
    205 
    206 void
    207 allhosts(void)
    208 {
    209 	struct utmpidlearr up;
    210 	enum clnt_stat clnt_stat;
    211 
    212 	bzero((char *)&up, sizeof(up));
    213 	clnt_stat = clnt_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
    214 	    RUSERSPROC_NAMES, xdr_void, NULL, xdr_utmpidlearr,
    215 	    &up, rusers_reply);
    216 	if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
    217 		fprintf(stderr, "%s: %s\n", argv0, clnt_sperrno(clnt_stat));
    218 		exit(1);
    219 	}
    220 }
    221 
    222 void usage(void)
    223 {
    224 	fprintf(stderr, "Usage: %s [-la] [hosts ...]\n", argv0);
    225 	exit(1);
    226 }
    227 
    228 void main(int argc, char *argv[])
    229 {
    230 	int ch;
    231 	extern int optind;
    232 
    233 	if (!(argv0 = rindex(argv[0], '/')))
    234 		argv0 = argv[0];
    235 	else
    236 		argv0++;
    237 
    238 
    239 	while ((ch = getopt(argc, argv, "al")) != -1)
    240 		switch (ch) {
    241 		case 'a':
    242 			allopt++;
    243 			break;
    244 		case 'l':
    245 			longopt++;
    246 			break;
    247 		default:
    248 			usage();
    249 			/*NOTREACHED*/
    250 		}
    251 
    252 	setlinebuf(stdout);
    253 	if (argc == optind)
    254 		allhosts();
    255 	else {
    256 		for (; optind < argc; optind++)
    257 			(void) onehost(argv[optind]);
    258 	}
    259 	exit(0);
    260 }
    261