Home | History | Annotate | Line # | Download | only in finger
finger.c revision 1.8
      1 /*	$NetBSD: finger.c,v 1.8 1997/09/09 02:41:08 mrg Exp $	*/
      2 
      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 /*
     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 #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";*/
     57 static char rcsid[] = "$NetBSD: finger.c,v 1.8 1997/09/09 02:41:08 mrg Exp $";
     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,
     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.
     72  */
     73 
     74 #include <sys/param.h>
     75 #include <sys/file.h>
     76 #include <stdio.h>
     77 #include <stdlib.h>
     78 #include <string.h>
     79 #include <time.h>
     80 #include "finger.h"
     81 #include "extern.h"
     82 
     83 time_t now;
     84 int entries, lflag, sflag, mflag, oflag, gflag, pplan;
     85 char tbuf[1024];
     86 
     87 int
     88 main(argc, argv)
     89 	int argc;
     90 	char **argv;
     91 {
     92 	extern int optind;
     93 	int ch;
     94 
     95 	oflag = 1;		/* default to old "office" behavior */
     96 
     97 	while ((ch = getopt(argc, argv, "lmpshog")) != EOF)
     98 		switch(ch) {
     99 		case 'l':
    100 			lflag = 1;		/* long format */
    101 			break;
    102 		case 'm':
    103 			mflag = 1;		/* force exact match of names */
    104 			break;
    105 		case 'p':
    106 			pplan = 1;		/* don't show .plan/.project */
    107 			break;
    108 		case 's':
    109 			sflag = 1;		/* short format */
    110 			break;
    111 		case 'h':
    112 			oflag = 0;		/* remote host info */
    113 			break;
    114 		case 'o':
    115 			oflag = 1;		/* office info */
    116 			break;
    117 		case 'g':
    118 			gflag = 1;		/* no gecos info, besides name */
    119 			break;
    120 		case '?':
    121 		default:
    122 			(void)fprintf(stderr,
    123 			    "usage: finger [-lmpsho] [login ...]\n");
    124 			exit(1);
    125 		}
    126 	argc -= optind;
    127 	argv += optind;
    128 
    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 
    161 void
    162 loginlist()
    163 {
    164 	PERSON *pn;
    165 	struct passwd *pw;
    166 	struct utmp user;
    167 	char name[UT_NAMESIZE + 1];
    168 
    169 	if (!freopen(_PATH_UTMP, "r", stdin)) {
    170 		(void)fprintf(stderr, "finger: can't read %s.\n", _PATH_UTMP);
    171 		exit(2);
    172 	}
    173 	name[UT_NAMESIZE] = '\0';
    174 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
    175 		if (!user.ut_name[0])
    176 			continue;
    177 		if ((pn = find_person(user.ut_name)) == NULL) {
    178 			bcopy(user.ut_name, name, UT_NAMESIZE);
    179 			if ((pw = getpwnam(name)) == NULL)
    180 				continue;
    181 			pn = enter_person(pw);
    182 		}
    183 		enter_where(&user, pn);
    184 	}
    185 	for (pn = phead; lflag && pn != NULL; pn = pn->next)
    186 		enter_lastlog(pn);
    187 }
    188 
    189 void
    190 userlist(argc, argv)
    191 	int argc;
    192 	char **argv;
    193 {
    194 	register int i;
    195 	register PERSON *pn;
    196 	PERSON *nethead, **nettail;
    197 	struct utmp user;
    198 	struct passwd *pw;
    199 	int dolocal, *used;
    200 
    201 	if (!(used = (int *)calloc((u_int)argc, (u_int)sizeof(int)))) {
    202 		(void)fprintf(stderr, "finger: out of space.\n");
    203 		exit(1);
    204 	}
    205 
    206 	/* pull out all network requests */
    207 	for (i = 0, dolocal = 0, nettail = &nethead; i < argc; i++) {
    208 		if (!strchr(argv[i], '@')) {
    209 			dolocal = 1;
    210 			continue;
    211 		}
    212 		pn = palloc();
    213 		*nettail = pn;
    214 		nettail = &pn->next;
    215 		pn->name = argv[i];
    216 		used[i] = -1;
    217 	}
    218 	*nettail = NULL;
    219 
    220 	if (!dolocal)
    221 		goto net;
    222 
    223 	/*
    224 	 * traverse the list of possible login names and check the login name
    225 	 * and real name against the name specified by the user.
    226 	 */
    227 	if (mflag) {
    228 		for (i = 0; i < argc; i++)
    229 			if (used[i] >= 0 && (pw = getpwnam(argv[i]))) {
    230 				enter_person(pw);
    231 				used[i] = 1;
    232 			}
    233 	} else while ((pw = getpwent()) != NULL)
    234 		for (i = 0; i < argc; i++)
    235 			if (used[i] >= 0 &&
    236 			    (!strcasecmp(pw->pw_name, argv[i]) ||
    237 			    match(pw, argv[i]))) {
    238 				enter_person(pw);
    239 				used[i] = 1;
    240 			}
    241 
    242 	/* list errors */
    243 	for (i = 0; i < argc; i++)
    244 		if (!used[i])
    245 			(void)fprintf(stderr,
    246 			    "finger: %s: no such user.\n", argv[i]);
    247 
    248 	/* handle network requests */
    249 net:	for (pn = nethead; pn; pn = pn->next) {
    250 		netfinger(pn->name);
    251 		if (pn->next || entries)
    252 			putchar('\n');
    253 	}
    254 
    255 	if (entries == 0)
    256 		return;
    257 
    258 	/*
    259 	 * Scan thru the list of users currently logged in, saving
    260 	 * appropriate data whenever a match occurs.
    261 	 */
    262 	if (!freopen(_PATH_UTMP, "r", stdin)) {
    263 		(void)fprintf( stderr, "finger: can't read %s.\n", _PATH_UTMP);
    264 		exit(1);
    265 	}
    266 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
    267 		if (!user.ut_name[0])
    268 			continue;
    269 		if ((pn = find_person(user.ut_name)) == NULL)
    270 			continue;
    271 		enter_where(&user, pn);
    272 	}
    273 	for (pn = phead; pn != NULL; pn = pn->next)
    274 		enter_lastlog(pn);
    275 }
    276