Home | History | Annotate | Line # | Download | only in finger
finger.c revision 1.5
      1 /*
      2  * Copyright (c) 1989 The Regents of the University of California.
      3  * 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 /*
     38  * Luke Mewburn <lukem (at) netbsd.org> added the following on 961121:
     39  *    - mail status ("No Mail", "Mail read:...", or "New Mail ...,
     40  *	Unread since ...".)
     41  *    - 4 digit phone extensions (3210 is printed as x3210.)
     42  *    - host/office toggling in short format with -h & -o.
     43  *    - short day names (`Tue' printed instead of `Jun 21' if the
     44  *	login time is < 6 days.
     45  */
     46 
     47 #ifndef lint
     48 char copyright[] =
     49 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
     50  All rights reserved.\n";
     51 #endif /* not lint */
     52 
     53 #ifndef lint
     54 /*static char sccsid[] = "from: @(#)finger.c	5.22 (Berkeley) 6/29/90";*/
     55 static char rcsid[] = "$Id: finger.c,v 1.5 1996/11/21 06:01:48 lukem Exp $";
     56 #endif /* not lint */
     57 
     58 /*
     59  * Finger prints out information about users.  It is not portable since
     60  * certain fields (e.g. the full user name, office, and phone numbers) are
     61  * extracted from the gecos field of the passwd file which other UNIXes
     62  * may not have or may use for other things.
     63  *
     64  * There are currently two output formats; the short format is one line
     65  * per user and displays login name, tty, login time, real name, idle time,
     66  * and either remote host information (default) or office location/phone
     67  * number, depending on if -h or -o is used respectively.
     68  * The long format gives the same information (in a more legible format) as
     69  * well as home directory, shell, mail info, and .plan/.project files.
     70  */
     71 
     72 #include <sys/param.h>
     73 #include <sys/file.h>
     74 #include <stdio.h>
     75 #include <stdlib.h>
     76 #include <string.h>
     77 #include <time.h>
     78 #include "finger.h"
     79 #include "extern.h"
     80 
     81 time_t now;
     82 int entries, lflag, sflag, mflag, oflag, pplan;
     83 char tbuf[1024];
     84 
     85 int
     86 main(argc, argv)
     87 	int argc;
     88 	char **argv;
     89 {
     90 	extern int optind;
     91 	int ch;
     92 
     93 	oflag = 1;		/* default to old "office" behavior */
     94 
     95 	while ((ch = getopt(argc, argv, "lmpsho")) != EOF)
     96 		switch(ch) {
     97 		case 'l':
     98 			lflag = 1;		/* long format */
     99 			break;
    100 		case 'm':
    101 			mflag = 1;		/* force exact match of names */
    102 			break;
    103 		case 'p':
    104 			pplan = 1;		/* don't show .plan/.project */
    105 			break;
    106 		case 's':
    107 			sflag = 1;		/* short format */
    108 			break;
    109 		case 'h':
    110 			oflag = 0;		/* remote host info */
    111 			break;
    112 		case 'o':
    113 			oflag = 1;		/* office info */
    114 			break;
    115 		case '?':
    116 		default:
    117 			(void)fprintf(stderr,
    118 			    "usage: finger [-lmpsho] [login ...]\n");
    119 			exit(1);
    120 		}
    121 	argc -= optind;
    122 	argv += optind;
    123 
    124 	(void)time(&now);
    125 	setpassent(1);
    126 	if (!*argv) {
    127 		/*
    128 		 * Assign explicit "small" format if no names given and -l
    129 		 * not selected.  Force the -s BEFORE we get names so proper
    130 		 * screening will be done.
    131 		 */
    132 		if (!lflag)
    133 			sflag = 1;	/* if -l not explicit, force -s */
    134 		loginlist();
    135 		if (entries == 0)
    136 			(void)printf("No one logged on.\n");
    137 	} else {
    138 		userlist(argc, argv);
    139 		/*
    140 		 * Assign explicit "large" format if names given and -s not
    141 		 * explicitly stated.  Force the -l AFTER we get names so any
    142 		 * remote finger attempts specified won't be mishandled.
    143 		 */
    144 		if (!sflag)
    145 			lflag = 1;	/* if -s not explicit, force -l */
    146 	}
    147 	if (entries != 0) {
    148 		if (lflag)
    149 			lflag_print();
    150 		else
    151 			sflag_print();
    152 	}
    153 	exit(0);
    154 }
    155 
    156 void
    157 loginlist()
    158 {
    159 	PERSON *pn;
    160 	struct passwd *pw;
    161 	struct utmp user;
    162 	char name[UT_NAMESIZE + 1];
    163 
    164 	if (!freopen(_PATH_UTMP, "r", stdin)) {
    165 		(void)fprintf(stderr, "finger: can't read %s.\n", _PATH_UTMP);
    166 		exit(2);
    167 	}
    168 	name[UT_NAMESIZE] = NULL;
    169 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
    170 		if (!user.ut_name[0])
    171 			continue;
    172 		if ((pn = find_person(user.ut_name)) == NULL) {
    173 			bcopy(user.ut_name, name, UT_NAMESIZE);
    174 			if ((pw = getpwnam(name)) == NULL)
    175 				continue;
    176 			pn = enter_person(pw);
    177 		}
    178 		enter_where(&user, pn);
    179 	}
    180 	for (pn = phead; lflag && pn != NULL; pn = pn->next)
    181 		enter_lastlog(pn);
    182 }
    183 
    184 void
    185 userlist(argc, argv)
    186 	int argc;
    187 	char **argv;
    188 {
    189 	register int i;
    190 	register PERSON *pn;
    191 	PERSON *nethead, **nettail;
    192 	struct utmp user;
    193 	struct passwd *pw;
    194 	int dolocal, *used;
    195 
    196 	if (!(used = (int *)calloc((u_int)argc, (u_int)sizeof(int)))) {
    197 		(void)fprintf(stderr, "finger: out of space.\n");
    198 		exit(1);
    199 	}
    200 
    201 	/* pull out all network requests */
    202 	for (i = 0, dolocal = 0, nettail = &nethead; i < argc; i++) {
    203 		if (!strchr(argv[i], '@')) {
    204 			dolocal = 1;
    205 			continue;
    206 		}
    207 		pn = palloc();
    208 		*nettail = pn;
    209 		nettail = &pn->next;
    210 		pn->name = argv[i];
    211 		used[i] = -1;
    212 	}
    213 	*nettail = NULL;
    214 
    215 	if (!dolocal)
    216 		goto net;
    217 
    218 	/*
    219 	 * traverse the list of possible login names and check the login name
    220 	 * and real name against the name specified by the user.
    221 	 */
    222 	if (mflag) {
    223 		for (i = 0; i < argc; i++)
    224 			if (used[i] >= 0 && (pw = getpwnam(argv[i]))) {
    225 				enter_person(pw);
    226 				used[i] = 1;
    227 			}
    228 	} else while ((pw = getpwent()) != NULL)
    229 		for (i = 0; i < argc; i++)
    230 			if (used[i] >= 0 &&
    231 			    (!strcasecmp(pw->pw_name, argv[i]) ||
    232 			    match(pw, argv[i]))) {
    233 				enter_person(pw);
    234 				used[i] = 1;
    235 			}
    236 
    237 	/* list errors */
    238 	for (i = 0; i < argc; i++)
    239 		if (!used[i])
    240 			(void)fprintf(stderr,
    241 			    "finger: %s: no such user.\n", argv[i]);
    242 
    243 	/* handle network requests */
    244 net:	for (pn = nethead; pn; pn = pn->next) {
    245 		netfinger(pn->name);
    246 		if (pn->next || entries)
    247 			putchar('\n');
    248 	}
    249 
    250 	if (entries == 0)
    251 		return;
    252 
    253 	/*
    254 	 * Scan thru the list of users currently logged in, saving
    255 	 * appropriate data whenever a match occurs.
    256 	 */
    257 	if (!freopen(_PATH_UTMP, "r", stdin)) {
    258 		(void)fprintf( stderr, "finger: can't read %s.\n", _PATH_UTMP);
    259 		exit(1);
    260 	}
    261 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
    262 		if (!user.ut_name[0])
    263 			continue;
    264 		if ((pn = find_person(user.ut_name)) == NULL)
    265 			continue;
    266 		enter_where(&user, pn);
    267 	}
    268 	for (pn = phead; pn != NULL; pn = pn->next)
    269 		enter_lastlog(pn);
    270 }
    271