Home | History | Annotate | Line # | Download | only in finger
finger.c revision 1.18
      1 /*	$NetBSD: finger.c,v 1.18 2002/08/10 16:10:46 kim Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  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 /*
     40  * Luke Mewburn <lukem (at) 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.
     47  */
     48 
     49 #include <sys/cdefs.h>
     50 #ifndef lint
     51 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
     52 	The Regents of the University of California.  All rights reserved.\n");
     53 #endif /* not lint */
     54 
     55 #ifndef lint
     56 #if 0
     57 static char sccsid[] = "@(#)finger.c	8.5 (Berkeley) 5/4/95";
     58 #else
     59 __RCSID("$NetBSD: finger.c,v 1.18 2002/08/10 16:10:46 kim Exp $");
     60 #endif
     61 #endif /* not lint */
     62 
     63 /*
     64  * Finger prints out information about users.  It is not portable since
     65  * certain fields (e.g. the full user name, office, and phone numbers) are
     66  * extracted from the gecos field of the passwd file which other UNIXes
     67  * may not have or may use for other things.
     68  *
     69  * There are currently two output formats; the short format is one line
     70  * per user and displays login name, tty, login time, real name, idle time,
     71  * and either remote host information (default) or office location/phone
     72  * number, depending on if -h or -o is used respectively.
     73  * The long format gives the same information (in a more legible format) as
     74  * well as home directory, shell, mail info, and .plan/.project files.
     75  */
     76 
     77 #include <sys/param.h>
     78 
     79 #include <db.h>
     80 #include <err.h>
     81 #include <errno.h>
     82 #include <fcntl.h>
     83 #include <pwd.h>
     84 #include <stdio.h>
     85 #include <stdlib.h>
     86 #include <string.h>
     87 #include <time.h>
     88 #include <unistd.h>
     89 
     90 #include <locale.h>
     91 
     92 #include "utmpentry.h"
     93 
     94 #include "finger.h"
     95 #include "extern.h"
     96 
     97 DB *db;
     98 time_t now;
     99 int entries, gflag, lflag, mflag, oflag, sflag, pplan;
    100 char tbuf[1024];
    101 struct utmpentry *ehead;
    102 
    103 static void loginlist __P((void));
    104 static void userlist __P((int, char **));
    105 int main __P((int, char **));
    106 
    107 int
    108 main(argc, argv)
    109 	int argc;
    110 	char **argv;
    111 {
    112 	int ch;
    113 
    114 	/* Allow user's locale settings to affect character output. */
    115 	(void *) setlocale(LC_CTYPE, "");
    116 
    117 	oflag = 1;		/* default to old "office" behavior */
    118 
    119 	while ((ch = getopt(argc, argv, "lmpshog")) != -1)
    120 		switch(ch) {
    121 		case 'l':
    122 			lflag = 1;		/* long format */
    123 			break;
    124 		case 'm':
    125 			mflag = 1;		/* force exact match of names */
    126 			break;
    127 		case 'p':
    128 			pplan = 1;		/* don't show .plan/.project */
    129 			break;
    130 		case 's':
    131 			sflag = 1;		/* short format */
    132 			break;
    133 		case 'h':
    134 			oflag = 0;		/* remote host info */
    135 			break;
    136 		case 'o':
    137 			oflag = 1;		/* office info */
    138 			break;
    139 		case 'g':
    140 			gflag = 1;		/* no gecos info, besides name */
    141 			break;
    142 		case '?':
    143 		default:
    144 			(void)fprintf(stderr,
    145 			    "usage: finger [-lmpshog] [login ...]\n");
    146 			exit(1);
    147 		}
    148 	argc -= optind;
    149 	argv += optind;
    150 
    151 	(void)time(&now);
    152 	setpassent(1);
    153 	entries = getutentries(NULL, &ehead);
    154 	if (argc == 0) {
    155 		/*
    156 		 * Assign explicit "small" format if no names given and -l
    157 		 * not selected.  Force the -s BEFORE we get names so proper
    158 		 * screening will be done.
    159 		 */
    160 		if (!lflag)
    161 			sflag = 1;	/* if -l not explicit, force -s */
    162 		loginlist();
    163 		if (entries == 0)
    164 			(void)printf("No one logged on.\n");
    165 	} else {
    166 		userlist(argc, argv);
    167 		/*
    168 		 * Assign explicit "large" format if names given and -s not
    169 		 * explicitly stated.  Force the -l AFTER we get names so any
    170 		 * remote finger attempts specified won't be mishandled.
    171 		 */
    172 		if (!sflag)
    173 			lflag = 1;	/* if -s not explicit, force -l */
    174 	}
    175 	if (entries) {
    176 		if (lflag)
    177 			lflag_print();
    178 		else
    179 			sflag_print();
    180 	}
    181 	return (0);
    182 }
    183 
    184 static void
    185 loginlist()
    186 {
    187 	PERSON *pn;
    188 	DBT data, key;
    189 	struct passwd *pw;
    190 	int r, sflag;
    191 	struct utmpentry *ep;
    192 
    193 	for (ep = ehead; ep; ep = ep->next) {
    194 		if ((pn = find_person(ep->name)) == NULL) {
    195 			if ((pw = getpwnam(ep->name)) == NULL)
    196 				continue;
    197 			pn = enter_person(pw);
    198 		}
    199 		enter_where(ep, pn);
    200 	}
    201 	if (db && lflag)
    202 		for (sflag = R_FIRST;; sflag = R_NEXT) {
    203 			PERSON *tmp;
    204 
    205 			r = (*db->seq)(db, &key, &data, sflag);
    206 			if (r == -1)
    207 				err(1, "db seq");
    208 			if (r == 1)
    209 				break;
    210 			memmove(&tmp, data.data, sizeof tmp);
    211 			enter_lastlog(tmp);
    212 		}
    213 }
    214 
    215 static void
    216 userlist(argc, argv)
    217 	int argc;
    218 	char **argv;
    219 {
    220 	register PERSON *pn;
    221 	DBT data, key;
    222 	struct passwd *pw;
    223 	int r, sflag, *used, *ip;
    224 	char **ap, **nargv, **np, **p;
    225 	struct utmpentry *ep;
    226 
    227 	if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
    228 	    (used = calloc(argc, sizeof(int))) == NULL)
    229 		err(1, NULL);
    230 
    231 	/* Pull out all network requests. */
    232 	for (ap = p = argv, np = nargv; *p; ++p)
    233 		if (strchr(*p, '@'))
    234 			*np++ = *p;
    235 		else
    236 			*ap++ = *p;
    237 
    238 	*np++ = NULL;
    239 	*ap++ = NULL;
    240 
    241 	if (!*argv)
    242 		goto net;
    243 
    244 	/*
    245 	 * Traverse the list of possible login names and check the login name
    246 	 * and real name against the name specified by the user.
    247 	 */
    248 	if (mflag) {
    249 		for (p = argv; *p; ++p)
    250 			if ((pw = getpwnam(*p)) != NULL)
    251 				enter_person(pw);
    252 			else
    253 				(void)fprintf(stderr,
    254 				    "finger: %s: no such user\n", *p);
    255 	} else {
    256 		while ((pw = getpwent()) != NULL)
    257 			for (p = argv, ip = used; *p; ++p, ++ip)
    258 				if (match(pw, *p)) {
    259 					enter_person(pw);
    260 					*ip = 1;
    261 				}
    262 		for (p = argv, ip = used; *p; ++p, ++ip)
    263 			if (!*ip)
    264 				(void)fprintf(stderr,
    265 				    "finger: %s: no such user\n", *p);
    266 	}
    267 
    268 	/* Handle network requests. */
    269 net:
    270 	for (p = nargv; *p;)
    271 		netfinger(*p++);
    272 
    273 	if (entries == 0)
    274 		return;
    275 
    276 	/*
    277 	 * Scan thru the list of users currently logged in, saving
    278 	 * appropriate data whenever a match occurs.
    279 	 */
    280 	for (ep = ehead; ep; ep = ep->next) {
    281 		if ((pn = find_person(ep->name)) == NULL)
    282 			continue;
    283 		enter_where(ep, pn);
    284 	}
    285 	if (db != NULL)
    286 		for (sflag = R_FIRST;; sflag = R_NEXT) {
    287 			PERSON *tmp;
    288 
    289 			r = (*db->seq)(db, &key, &data, sflag);
    290 			if (r == -1)
    291 				err(1, "db seq");
    292 			if (r == 1)
    293 				break;
    294 			memmove(&tmp, data.data, sizeof tmp);
    295 			enter_lastlog(tmp);
    296 		}
    297 }
    298 
    299