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

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