[BACK]Return to rusers.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rusers

Annotation of src/usr.bin/rusers/rusers.c, Revision 1.5

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