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