Home | History | Annotate | Line # | Download | only in finger
finger.c revision 1.7
      1 /*	$NetBSD: finger.c,v 1.7 1997/05/17 19:42:24 pk 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.7 1997/05/17 19:42:24 pk 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, 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, "lmpsho")) != 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 '?':
    118 		default:
    119 			(void)fprintf(stderr,
    120 			    "usage: finger [-lmpsho] [login ...]\n");
    121 			exit(1);
    122 		}
    123 	argc -= optind;
    124 	argv += optind;
    125 
    126 	(void)time(&now);
    127 	setpassent(1);
    128 	if (!*argv) {
    129 		/*
    130 		 * Assign explicit "small" format if no names given and -l
    131 		 * not selected.  Force the -s BEFORE we get names so proper
    132 		 * screening will be done.
    133 		 */
    134 		if (!lflag)
    135 			sflag = 1;	/* if -l not explicit, force -s */
    136 		loginlist();
    137 		if (entries == 0)
    138 			(void)printf("No one logged on.\n");
    139 	} else {
    140 		userlist(argc, argv);
    141 		/*
    142 		 * Assign explicit "large" format if names given and -s not
    143 		 * explicitly stated.  Force the -l AFTER we get names so any
    144 		 * remote finger attempts specified won't be mishandled.
    145 		 */
    146 		if (!sflag)
    147 			lflag = 1;	/* if -s not explicit, force -l */
    148 	}
    149 	if (entries != 0) {
    150 		if (lflag)
    151 			lflag_print();
    152 		else
    153 			sflag_print();
    154 	}
    155 	exit(0);
    156 }
    157 
    158 void
    159 loginlist()
    160 {
    161 	PERSON *pn;
    162 	struct passwd *pw;
    163 	struct utmp user;
    164 	char name[UT_NAMESIZE + 1];
    165 
    166 	if (!freopen(_PATH_UTMP, "r", stdin)) {
    167 		(void)fprintf(stderr, "finger: can't read %s.\n", _PATH_UTMP);
    168 		exit(2);
    169 	}
    170 	name[UT_NAMESIZE] = '\0';
    171 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
    172 		if (!user.ut_name[0])
    173 			continue;
    174 		if ((pn = find_person(user.ut_name)) == NULL) {
    175 			bcopy(user.ut_name, name, UT_NAMESIZE);
    176 			if ((pw = getpwnam(name)) == NULL)
    177 				continue;
    178 			pn = enter_person(pw);
    179 		}
    180 		enter_where(&user, pn);
    181 	}
    182 	for (pn = phead; lflag && pn != NULL; pn = pn->next)
    183 		enter_lastlog(pn);
    184 }
    185 
    186 void
    187 userlist(argc, argv)
    188 	int argc;
    189 	char **argv;
    190 {
    191 	register int i;
    192 	register PERSON *pn;
    193 	PERSON *nethead, **nettail;
    194 	struct utmp user;
    195 	struct passwd *pw;
    196 	int dolocal, *used;
    197 
    198 	if (!(used = (int *)calloc((u_int)argc, (u_int)sizeof(int)))) {
    199 		(void)fprintf(stderr, "finger: out of space.\n");
    200 		exit(1);
    201 	}
    202 
    203 	/* pull out all network requests */
    204 	for (i = 0, dolocal = 0, nettail = &nethead; i < argc; i++) {
    205 		if (!strchr(argv[i], '@')) {
    206 			dolocal = 1;
    207 			continue;
    208 		}
    209 		pn = palloc();
    210 		*nettail = pn;
    211 		nettail = &pn->next;
    212 		pn->name = argv[i];
    213 		used[i] = -1;
    214 	}
    215 	*nettail = NULL;
    216 
    217 	if (!dolocal)
    218 		goto net;
    219 
    220 	/*
    221 	 * traverse the list of possible login names and check the login name
    222 	 * and real name against the name specified by the user.
    223 	 */
    224 	if (mflag) {
    225 		for (i = 0; i < argc; i++)
    226 			if (used[i] >= 0 && (pw = getpwnam(argv[i]))) {
    227 				enter_person(pw);
    228 				used[i] = 1;
    229 			}
    230 	} else while ((pw = getpwent()) != NULL)
    231 		for (i = 0; i < argc; i++)
    232 			if (used[i] >= 0 &&
    233 			    (!strcasecmp(pw->pw_name, argv[i]) ||
    234 			    match(pw, argv[i]))) {
    235 				enter_person(pw);
    236 				used[i] = 1;
    237 			}
    238 
    239 	/* list errors */
    240 	for (i = 0; i < argc; i++)
    241 		if (!used[i])
    242 			(void)fprintf(stderr,
    243 			    "finger: %s: no such user.\n", argv[i]);
    244 
    245 	/* handle network requests */
    246 net:	for (pn = nethead; pn; pn = pn->next) {
    247 		netfinger(pn->name);
    248 		if (pn->next || entries)
    249 			putchar('\n');
    250 	}
    251 
    252 	if (entries == 0)
    253 		return;
    254 
    255 	/*
    256 	 * Scan thru the list of users currently logged in, saving
    257 	 * appropriate data whenever a match occurs.
    258 	 */
    259 	if (!freopen(_PATH_UTMP, "r", stdin)) {
    260 		(void)fprintf( stderr, "finger: can't read %s.\n", _PATH_UTMP);
    261 		exit(1);
    262 	}
    263 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
    264 		if (!user.ut_name[0])
    265 			continue;
    266 		if ((pn = find_person(user.ut_name)) == NULL)
    267 			continue;
    268 		enter_where(&user, pn);
    269 	}
    270 	for (pn = phead; pn != NULL; pn = pn->next)
    271 		enter_lastlog(pn);
    272 }
    273