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