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

Annotation of src/usr.bin/finger/finger.c, Revision 1.7

1.7     ! kstailey    1: /*     $OpenBSD: finger.c,v 1.6 1997/01/17 07:12:31 millert Exp $      */
1.2       deraadt     2:
1.1       deraadt     3: /*
                      4:  * Copyright (c) 1989 The Regents of the University of California.
                      5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the University of
                     21:  *     California, Berkeley and its contributors.
                     22:  * 4. Neither the name of the University nor the names of its contributors
                     23:  *    may be used to endorse or promote products derived from this software
                     24:  *    without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     36:  * SUCH DAMAGE.
                     37:  */
                     38:
                     39: /*
1.7     ! kstailey   40:  * Luke Mewburn <lukem@netbsd.org> added the following on 961121:
        !            41:  *    - mail status ("No Mail", "Mail read:...", or "New Mail ...,
        !            42:  *     Unread since ...".)
        !            43:  *    - 4 digit phone extensions (3210 is printed as x3210.)
        !            44:  *    - host/office toggling in short format with -h & -o.
        !            45:  *    - short day names (`Tue' printed instead of `Jun 21' if the
        !            46:  *     login time is < 6 days.
1.1       deraadt    47:  */
                     48:
                     49: #ifndef lint
                     50: char copyright[] =
                     51: "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
                     52:  All rights reserved.\n";
                     53: #endif /* not lint */
                     54:
                     55: #ifndef lint
                     56: /*static char sccsid[] = "from: @(#)finger.c   5.22 (Berkeley) 6/29/90";*/
1.7     ! kstailey   57: static char rcsid[] = "$OpenBSD: finger.c,v 1.6 1997/01/17 07:12:31 millert Exp $";
1.1       deraadt    58: #endif /* not lint */
                     59:
                     60: /*
                     61:  * Finger prints out information about users.  It is not portable since
                     62:  * certain fields (e.g. the full user name, office, and phone numbers) are
                     63:  * extracted from the gecos field of the passwd file which other UNIXes
                     64:  * may not have or may use for other things.
                     65:  *
                     66:  * There are currently two output formats; the short format is one line
                     67:  * per user and displays login name, tty, login time, real name, idle time,
1.7     ! kstailey   68:  * and either remote host information (default) or office location/phone
        !            69:  * number, depending on if -h or -o is used respectively.
        !            70:  * The long format gives the same information (in a more legible format) as
        !            71:  * well as home directory, shell, mail info, and .plan/.project files.
1.1       deraadt    72:  */
                     73:
                     74: #include <sys/param.h>
                     75: #include <sys/file.h>
                     76: #include <stdio.h>
                     77: #include <stdlib.h>
1.7     ! kstailey   78: #include <string.h>
        !            79: #include <time.h>
1.1       deraadt    80: #include "finger.h"
1.7     ! kstailey   81: #include "extern.h"
1.1       deraadt    82:
                     83: time_t now;
1.7     ! kstailey   84: int entries, lflag, sflag, mflag, oflag, pplan, Mflag;
1.1       deraadt    85: char tbuf[1024];
                     86:
1.3       deraadt    87: int
1.1       deraadt    88: main(argc, argv)
                     89:        int argc;
                     90:        char **argv;
                     91: {
                     92:        extern int optind;
                     93:        int ch;
1.4       downsj     94:        char domain[256];
1.1       deraadt    95:
1.7     ! kstailey   96:        oflag = 1;              /* default to old "office" behavior */
        !            97:
        !            98:        while ((ch = getopt(argc, argv, "lmMpsho")) != -1)
1.1       deraadt    99:                switch(ch) {
                    100:                case 'l':
                    101:                        lflag = 1;              /* long format */
                    102:                        break;
                    103:                case 'm':
                    104:                        mflag = 1;              /* force exact match of names */
                    105:                        break;
1.4       downsj    106:                case 'M':
                    107:                        Mflag = 1;              /* allow name matching */
                    108:                        break;
1.1       deraadt   109:                case 'p':
                    110:                        pplan = 1;              /* don't show .plan/.project */
                    111:                        break;
                    112:                case 's':
                    113:                        sflag = 1;              /* short format */
                    114:                        break;
1.7     ! kstailey  115:                case 'h':
        !           116:                        oflag = 0;              /* remote host info */
        !           117:                        break;
        !           118:                case 'o':
        !           119:                        oflag = 1;              /* office info */
        !           120:                        break;
1.1       deraadt   121:                case '?':
                    122:                default:
                    123:                        (void)fprintf(stderr,
1.7     ! kstailey  124:                            "usage: finger [-lmMpsho] [login ...]\n");
1.1       deraadt   125:                        exit(1);
                    126:                }
                    127:        argc -= optind;
                    128:        argv += optind;
                    129:
1.4       downsj    130:        /* if a domainname is set, increment mflag. */
                    131:        if ((getdomainname(&domain, sizeof(domain)) == 0) && domain[0])
                    132:                mflag++;
                    133:
1.1       deraadt   134:        (void)time(&now);
                    135:        setpassent(1);
                    136:        if (!*argv) {
                    137:                /*
                    138:                 * Assign explicit "small" format if no names given and -l
                    139:                 * not selected.  Force the -s BEFORE we get names so proper
                    140:                 * screening will be done.
                    141:                 */
                    142:                if (!lflag)
                    143:                        sflag = 1;      /* if -l not explicit, force -s */
                    144:                loginlist();
                    145:                if (entries == 0)
                    146:                        (void)printf("No one logged on.\n");
                    147:        } else {
                    148:                userlist(argc, argv);
                    149:                /*
                    150:                 * Assign explicit "large" format if names given and -s not
                    151:                 * explicitly stated.  Force the -l AFTER we get names so any
                    152:                 * remote finger attempts specified won't be mishandled.
                    153:                 */
                    154:                if (!sflag)
                    155:                        lflag = 1;      /* if -s not explicit, force -l */
                    156:        }
                    157:        if (entries != 0) {
                    158:                if (lflag)
                    159:                        lflag_print();
                    160:                else
                    161:                        sflag_print();
                    162:        }
                    163:        exit(0);
                    164: }
                    165:
1.7     ! kstailey  166: void
1.1       deraadt   167: loginlist()
                    168: {
1.7     ! kstailey  169:        PERSON *pn;
1.1       deraadt   170:        struct passwd *pw;
                    171:        struct utmp user;
                    172:        char name[UT_NAMESIZE + 1];
                    173:
                    174:        if (!freopen(_PATH_UTMP, "r", stdin)) {
                    175:                (void)fprintf(stderr, "finger: can't read %s.\n", _PATH_UTMP);
                    176:                exit(2);
                    177:        }
1.7     ! kstailey  178:        name[UT_NAMESIZE] = '\0';
1.1       deraadt   179:        while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
                    180:                if (!user.ut_name[0])
                    181:                        continue;
                    182:                if ((pn = find_person(user.ut_name)) == NULL) {
                    183:                        bcopy(user.ut_name, name, UT_NAMESIZE);
                    184:                        if ((pw = getpwnam(name)) == NULL)
                    185:                                continue;
                    186:                        pn = enter_person(pw);
                    187:                }
                    188:                enter_where(&user, pn);
                    189:        }
                    190:        for (pn = phead; lflag && pn != NULL; pn = pn->next)
                    191:                enter_lastlog(pn);
                    192: }
                    193:
1.3       deraadt   194: void
1.1       deraadt   195: userlist(argc, argv)
1.7     ! kstailey  196:        int argc;
        !           197:        char **argv;
1.1       deraadt   198: {
1.7     ! kstailey  199:        register int i;
1.1       deraadt   200:        register PERSON *pn;
                    201:        PERSON *nethead, **nettail;
                    202:        struct utmp user;
                    203:        struct passwd *pw;
                    204:        int dolocal, *used;
                    205:
                    206:        if (!(used = (int *)calloc((u_int)argc, (u_int)sizeof(int)))) {
                    207:                (void)fprintf(stderr, "finger: out of space.\n");
                    208:                exit(1);
                    209:        }
                    210:
                    211:        /* pull out all network requests */
                    212:        for (i = 0, dolocal = 0, nettail = &nethead; i < argc; i++) {
1.6       millert   213:                if (!strchr(argv[i], '@')) {
1.1       deraadt   214:                        dolocal = 1;
                    215:                        continue;
                    216:                }
                    217:                pn = palloc();
                    218:                *nettail = pn;
                    219:                nettail = &pn->next;
                    220:                pn->name = argv[i];
                    221:                used[i] = -1;
                    222:        }
                    223:        *nettail = NULL;
                    224:
                    225:        if (!dolocal)
                    226:                goto net;
                    227:
                    228:        /*
                    229:         * traverse the list of possible login names and check the login name
                    230:         * and real name against the name specified by the user.
                    231:         */
1.4       downsj    232:        if ((mflag - Mflag) > 0) {
1.1       deraadt   233:                for (i = 0; i < argc; i++)
                    234:                        if (used[i] >= 0 && (pw = getpwnam(argv[i]))) {
                    235:                                enter_person(pw);
                    236:                                used[i] = 1;
                    237:                        }
1.7     ! kstailey  238:        } else while ((pw = getpwent()) != NULL)
1.1       deraadt   239:                for (i = 0; i < argc; i++)
                    240:                        if (used[i] >= 0 &&
                    241:                            (!strcasecmp(pw->pw_name, argv[i]) ||
                    242:                            match(pw, argv[i]))) {
                    243:                                enter_person(pw);
                    244:                                used[i] = 1;
                    245:                        }
                    246:
                    247:        /* list errors */
                    248:        for (i = 0; i < argc; i++)
                    249:                if (!used[i])
                    250:                        (void)fprintf(stderr,
                    251:                            "finger: %s: no such user.\n", argv[i]);
                    252:
                    253:        /* handle network requests */
                    254: net:   for (pn = nethead; pn; pn = pn->next) {
                    255:                netfinger(pn->name);
                    256:                if (pn->next || entries)
                    257:                        putchar('\n');
                    258:        }
                    259:
                    260:        if (entries == 0)
                    261:                return;
                    262:
                    263:        /*
                    264:         * Scan thru the list of users currently logged in, saving
                    265:         * appropriate data whenever a match occurs.
                    266:         */
                    267:        if (!freopen(_PATH_UTMP, "r", stdin)) {
                    268:                (void)fprintf( stderr, "finger: can't read %s.\n", _PATH_UTMP);
                    269:                exit(1);
                    270:        }
                    271:        while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
                    272:                if (!user.ut_name[0])
                    273:                        continue;
                    274:                if ((pn = find_person(user.ut_name)) == NULL)
                    275:                        continue;
                    276:                enter_where(&user, pn);
                    277:        }
                    278:        for (pn = phead; pn != NULL; pn = pn->next)
                    279:                enter_lastlog(pn);
                    280: }