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

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