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

1.12    ! mpech       1: /*     $OpenBSD: finger.c,v 1.11 2001/07/04 06:30:44 deraadt 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.12    ! mpech      57: static char rcsid[] = "$OpenBSD: finger.c,v 1.11 2001/07/04 06:30:44 deraadt 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>
1.9       downsj     76: #include <sys/stat.h>
1.1       deraadt    77: #include <stdio.h>
                     78: #include <stdlib.h>
1.7       kstailey   79: #include <string.h>
                     80: #include <time.h>
1.11      deraadt    81: #include <unistd.h>
1.8       mickey     82: #include <err.h>
1.1       deraadt    83: #include "finger.h"
1.7       kstailey   84: #include "extern.h"
1.1       deraadt    85:
                     86: time_t now;
1.7       kstailey   87: int entries, lflag, sflag, mflag, oflag, pplan, Mflag;
1.1       deraadt    88: char tbuf[1024];
                     89:
1.3       deraadt    90: int
1.1       deraadt    91: main(argc, argv)
                     92:        int argc;
                     93:        char **argv;
                     94: {
                     95:        extern int optind;
1.8       mickey     96:        extern char *__progname;
1.1       deraadt    97:        int ch;
1.10      aaron      98:        char domain[MAXHOSTNAMELEN];
1.9       downsj     99:        struct stat sb;
1.1       deraadt   100:
1.7       kstailey  101:        oflag = 1;              /* default to old "office" behavior */
                    102:
                    103:        while ((ch = getopt(argc, argv, "lmMpsho")) != -1)
1.1       deraadt   104:                switch(ch) {
                    105:                case 'l':
                    106:                        lflag = 1;              /* long format */
                    107:                        break;
                    108:                case 'm':
                    109:                        mflag = 1;              /* force exact match of names */
                    110:                        break;
1.4       downsj    111:                case 'M':
                    112:                        Mflag = 1;              /* allow name matching */
                    113:                        break;
1.1       deraadt   114:                case 'p':
                    115:                        pplan = 1;              /* don't show .plan/.project */
                    116:                        break;
                    117:                case 's':
                    118:                        sflag = 1;              /* short format */
                    119:                        break;
1.7       kstailey  120:                case 'h':
                    121:                        oflag = 0;              /* remote host info */
                    122:                        break;
                    123:                case 'o':
                    124:                        oflag = 1;              /* office info */
                    125:                        break;
1.1       deraadt   126:                case '?':
                    127:                default:
                    128:                        (void)fprintf(stderr,
1.8       mickey    129:                            "usage: %s [-lmMpsho] [login ...]\n", __progname);
1.1       deraadt   130:                        exit(1);
                    131:                }
                    132:        argc -= optind;
                    133:        argv += optind;
                    134:
1.9       downsj    135:        /* If a domainname is set, increment mflag. */
1.11      deraadt   136:        if ((getdomainname(domain, sizeof(domain)) == 0) && domain[0])
1.4       downsj    137:                mflag++;
1.9       downsj    138:        /* If _PATH_MP_DB is larger than 1MB, increment mflag. */
                    139:        if (stat(_PATH_MP_DB, &sb) == 0) {
                    140:                if (sb.st_size > 1048576)
                    141:                        mflag++;
                    142:        }
1.4       downsj    143:
1.1       deraadt   144:        (void)time(&now);
                    145:        setpassent(1);
                    146:        if (!*argv) {
                    147:                /*
                    148:                 * Assign explicit "small" format if no names given and -l
                    149:                 * not selected.  Force the -s BEFORE we get names so proper
                    150:                 * screening will be done.
                    151:                 */
                    152:                if (!lflag)
                    153:                        sflag = 1;      /* if -l not explicit, force -s */
                    154:                loginlist();
                    155:                if (entries == 0)
                    156:                        (void)printf("No one logged on.\n");
                    157:        } else {
                    158:                userlist(argc, argv);
                    159:                /*
                    160:                 * Assign explicit "large" format if names given and -s not
                    161:                 * explicitly stated.  Force the -l AFTER we get names so any
                    162:                 * remote finger attempts specified won't be mishandled.
                    163:                 */
                    164:                if (!sflag)
                    165:                        lflag = 1;      /* if -s not explicit, force -l */
                    166:        }
                    167:        if (entries != 0) {
                    168:                if (lflag)
                    169:                        lflag_print();
                    170:                else
                    171:                        sflag_print();
                    172:        }
                    173:        exit(0);
                    174: }
                    175:
1.7       kstailey  176: void
1.1       deraadt   177: loginlist()
                    178: {
1.7       kstailey  179:        PERSON *pn;
1.1       deraadt   180:        struct passwd *pw;
                    181:        struct utmp user;
                    182:        char name[UT_NAMESIZE + 1];
                    183:
1.8       mickey    184:        if (!freopen(_PATH_UTMP, "r", stdin))
                    185:                err(2, _PATH_UTMP);
1.7       kstailey  186:        name[UT_NAMESIZE] = '\0';
1.1       deraadt   187:        while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
                    188:                if (!user.ut_name[0])
                    189:                        continue;
                    190:                if ((pn = find_person(user.ut_name)) == NULL) {
                    191:                        bcopy(user.ut_name, name, UT_NAMESIZE);
                    192:                        if ((pw = getpwnam(name)) == NULL)
                    193:                                continue;
                    194:                        pn = enter_person(pw);
                    195:                }
                    196:                enter_where(&user, pn);
                    197:        }
                    198:        for (pn = phead; lflag && pn != NULL; pn = pn->next)
                    199:                enter_lastlog(pn);
                    200: }
                    201:
1.3       deraadt   202: void
1.1       deraadt   203: userlist(argc, argv)
1.7       kstailey  204:        int argc;
                    205:        char **argv;
1.1       deraadt   206: {
1.12    ! mpech     207:        int i;
        !           208:        PERSON *pn;
1.1       deraadt   209:        PERSON *nethead, **nettail;
                    210:        struct utmp user;
                    211:        struct passwd *pw;
                    212:        int dolocal, *used;
                    213:
1.8       mickey    214:        if (!(used = (int *)calloc((u_int)argc, (u_int)sizeof(int))))
                    215:                err(2, "malloc");
1.1       deraadt   216:
                    217:        /* pull out all network requests */
                    218:        for (i = 0, dolocal = 0, nettail = &nethead; i < argc; i++) {
1.6       millert   219:                if (!strchr(argv[i], '@')) {
1.1       deraadt   220:                        dolocal = 1;
                    221:                        continue;
                    222:                }
                    223:                pn = palloc();
                    224:                *nettail = pn;
                    225:                nettail = &pn->next;
                    226:                pn->name = argv[i];
                    227:                used[i] = -1;
                    228:        }
                    229:        *nettail = NULL;
                    230:
                    231:        if (!dolocal)
                    232:                goto net;
                    233:
                    234:        /*
                    235:         * traverse the list of possible login names and check the login name
                    236:         * and real name against the name specified by the user.
                    237:         */
1.4       downsj    238:        if ((mflag - Mflag) > 0) {
1.1       deraadt   239:                for (i = 0; i < argc; i++)
                    240:                        if (used[i] >= 0 && (pw = getpwnam(argv[i]))) {
                    241:                                enter_person(pw);
                    242:                                used[i] = 1;
                    243:                        }
1.7       kstailey  244:        } else while ((pw = getpwent()) != NULL)
1.1       deraadt   245:                for (i = 0; i < argc; i++)
                    246:                        if (used[i] >= 0 &&
                    247:                            (!strcasecmp(pw->pw_name, argv[i]) ||
                    248:                            match(pw, argv[i]))) {
                    249:                                enter_person(pw);
                    250:                                used[i] = 1;
                    251:                        }
                    252:
                    253:        /* list errors */
                    254:        for (i = 0; i < argc; i++)
                    255:                if (!used[i])
1.8       mickey    256:                        warnx("%s: no such user.", argv[i]);
1.1       deraadt   257:
                    258:        /* handle network requests */
                    259: net:   for (pn = nethead; pn; pn = pn->next) {
                    260:                netfinger(pn->name);
                    261:                if (pn->next || entries)
                    262:                        putchar('\n');
                    263:        }
                    264:
                    265:        if (entries == 0)
                    266:                return;
                    267:
                    268:        /*
                    269:         * Scan thru the list of users currently logged in, saving
                    270:         * appropriate data whenever a match occurs.
                    271:         */
1.8       mickey    272:        if (!freopen(_PATH_UTMP, "r", stdin))
                    273:                err(1, _PATH_UTMP);
1.1       deraadt   274:        while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
                    275:                if (!user.ut_name[0])
                    276:                        continue;
                    277:                if ((pn = find_person(user.ut_name)) == NULL)
                    278:                        continue;
                    279:                enter_where(&user, pn);
                    280:        }
                    281:        for (pn = phead; pn != NULL; pn = pn->next)
                    282:                enter_lastlog(pn);
                    283: }