[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.12

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