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