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