Home | History | Annotate | Line # | Download | only in finger
finger.c revision 1.7
      1  1.7       pk /*	$NetBSD: finger.c,v 1.7 1997/05/17 19:42:24 pk Exp $	*/
      2  1.6      tls 
      3  1.1      cgd /*
      4  1.1      cgd  * Copyright (c) 1989 The Regents of the University of California.
      5  1.1      cgd  * All rights reserved.
      6  1.1      cgd  *
      7  1.1      cgd  * This code is derived from software contributed to Berkeley by
      8  1.1      cgd  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
      9  1.1      cgd  *
     10  1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11  1.1      cgd  * modification, are permitted provided that the following conditions
     12  1.1      cgd  * are met:
     13  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.1      cgd  *    must display the following acknowledgement:
     20  1.1      cgd  *	This product includes software developed by the University of
     21  1.1      cgd  *	California, Berkeley and its contributors.
     22  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.1      cgd  *    may be used to endorse or promote products derived from this software
     24  1.1      cgd  *    without specific prior written permission.
     25  1.1      cgd  *
     26  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1      cgd  * SUCH DAMAGE.
     37  1.1      cgd  */
     38  1.1      cgd 
     39  1.3   brezak /*
     40  1.5    lukem  * Luke Mewburn <lukem (at) netbsd.org> added the following on 961121:
     41  1.5    lukem  *    - mail status ("No Mail", "Mail read:...", or "New Mail ...,
     42  1.5    lukem  *	Unread since ...".)
     43  1.5    lukem  *    - 4 digit phone extensions (3210 is printed as x3210.)
     44  1.5    lukem  *    - host/office toggling in short format with -h & -o.
     45  1.5    lukem  *    - short day names (`Tue' printed instead of `Jun 21' if the
     46  1.5    lukem  *	login time is < 6 days.
     47  1.3   brezak  */
     48  1.3   brezak 
     49  1.1      cgd #ifndef lint
     50  1.1      cgd char copyright[] =
     51  1.1      cgd "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
     52  1.1      cgd  All rights reserved.\n";
     53  1.1      cgd #endif /* not lint */
     54  1.1      cgd 
     55  1.1      cgd #ifndef lint
     56  1.2  mycroft /*static char sccsid[] = "from: @(#)finger.c	5.22 (Berkeley) 6/29/90";*/
     57  1.7       pk static char rcsid[] = "$NetBSD: finger.c,v 1.7 1997/05/17 19:42:24 pk Exp $";
     58  1.1      cgd #endif /* not lint */
     59  1.1      cgd 
     60  1.1      cgd /*
     61  1.1      cgd  * Finger prints out information about users.  It is not portable since
     62  1.1      cgd  * certain fields (e.g. the full user name, office, and phone numbers) are
     63  1.1      cgd  * extracted from the gecos field of the passwd file which other UNIXes
     64  1.1      cgd  * may not have or may use for other things.
     65  1.1      cgd  *
     66  1.1      cgd  * There are currently two output formats; the short format is one line
     67  1.1      cgd  * per user and displays login name, tty, login time, real name, idle time,
     68  1.5    lukem  * and either remote host information (default) or office location/phone
     69  1.5    lukem  * number, depending on if -h or -o is used respectively.
     70  1.5    lukem  * The long format gives the same information (in a more legible format) as
     71  1.5    lukem  * well as home directory, shell, mail info, and .plan/.project files.
     72  1.1      cgd  */
     73  1.1      cgd 
     74  1.1      cgd #include <sys/param.h>
     75  1.1      cgd #include <sys/file.h>
     76  1.1      cgd #include <stdio.h>
     77  1.4      cgd #include <stdlib.h>
     78  1.5    lukem #include <string.h>
     79  1.5    lukem #include <time.h>
     80  1.1      cgd #include "finger.h"
     81  1.5    lukem #include "extern.h"
     82  1.1      cgd 
     83  1.1      cgd time_t now;
     84  1.5    lukem int entries, lflag, sflag, mflag, oflag, pplan;
     85  1.1      cgd char tbuf[1024];
     86  1.1      cgd 
     87  1.5    lukem int
     88  1.1      cgd main(argc, argv)
     89  1.1      cgd 	int argc;
     90  1.1      cgd 	char **argv;
     91  1.1      cgd {
     92  1.1      cgd 	extern int optind;
     93  1.1      cgd 	int ch;
     94  1.1      cgd 
     95  1.5    lukem 	oflag = 1;		/* default to old "office" behavior */
     96  1.5    lukem 
     97  1.5    lukem 	while ((ch = getopt(argc, argv, "lmpsho")) != EOF)
     98  1.1      cgd 		switch(ch) {
     99  1.1      cgd 		case 'l':
    100  1.1      cgd 			lflag = 1;		/* long format */
    101  1.1      cgd 			break;
    102  1.1      cgd 		case 'm':
    103  1.1      cgd 			mflag = 1;		/* force exact match of names */
    104  1.1      cgd 			break;
    105  1.1      cgd 		case 'p':
    106  1.1      cgd 			pplan = 1;		/* don't show .plan/.project */
    107  1.1      cgd 			break;
    108  1.1      cgd 		case 's':
    109  1.1      cgd 			sflag = 1;		/* short format */
    110  1.1      cgd 			break;
    111  1.5    lukem 		case 'h':
    112  1.5    lukem 			oflag = 0;		/* remote host info */
    113  1.5    lukem 			break;
    114  1.5    lukem 		case 'o':
    115  1.5    lukem 			oflag = 1;		/* office info */
    116  1.5    lukem 			break;
    117  1.1      cgd 		case '?':
    118  1.1      cgd 		default:
    119  1.1      cgd 			(void)fprintf(stderr,
    120  1.5    lukem 			    "usage: finger [-lmpsho] [login ...]\n");
    121  1.1      cgd 			exit(1);
    122  1.1      cgd 		}
    123  1.1      cgd 	argc -= optind;
    124  1.1      cgd 	argv += optind;
    125  1.1      cgd 
    126  1.1      cgd 	(void)time(&now);
    127  1.1      cgd 	setpassent(1);
    128  1.1      cgd 	if (!*argv) {
    129  1.1      cgd 		/*
    130  1.1      cgd 		 * Assign explicit "small" format if no names given and -l
    131  1.1      cgd 		 * not selected.  Force the -s BEFORE we get names so proper
    132  1.1      cgd 		 * screening will be done.
    133  1.1      cgd 		 */
    134  1.1      cgd 		if (!lflag)
    135  1.1      cgd 			sflag = 1;	/* if -l not explicit, force -s */
    136  1.1      cgd 		loginlist();
    137  1.1      cgd 		if (entries == 0)
    138  1.1      cgd 			(void)printf("No one logged on.\n");
    139  1.1      cgd 	} else {
    140  1.1      cgd 		userlist(argc, argv);
    141  1.1      cgd 		/*
    142  1.1      cgd 		 * Assign explicit "large" format if names given and -s not
    143  1.1      cgd 		 * explicitly stated.  Force the -l AFTER we get names so any
    144  1.1      cgd 		 * remote finger attempts specified won't be mishandled.
    145  1.1      cgd 		 */
    146  1.1      cgd 		if (!sflag)
    147  1.1      cgd 			lflag = 1;	/* if -s not explicit, force -l */
    148  1.1      cgd 	}
    149  1.1      cgd 	if (entries != 0) {
    150  1.1      cgd 		if (lflag)
    151  1.1      cgd 			lflag_print();
    152  1.1      cgd 		else
    153  1.1      cgd 			sflag_print();
    154  1.1      cgd 	}
    155  1.1      cgd 	exit(0);
    156  1.1      cgd }
    157  1.1      cgd 
    158  1.5    lukem void
    159  1.1      cgd loginlist()
    160  1.1      cgd {
    161  1.5    lukem 	PERSON *pn;
    162  1.1      cgd 	struct passwd *pw;
    163  1.1      cgd 	struct utmp user;
    164  1.1      cgd 	char name[UT_NAMESIZE + 1];
    165  1.1      cgd 
    166  1.1      cgd 	if (!freopen(_PATH_UTMP, "r", stdin)) {
    167  1.1      cgd 		(void)fprintf(stderr, "finger: can't read %s.\n", _PATH_UTMP);
    168  1.1      cgd 		exit(2);
    169  1.1      cgd 	}
    170  1.7       pk 	name[UT_NAMESIZE] = '\0';
    171  1.1      cgd 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
    172  1.1      cgd 		if (!user.ut_name[0])
    173  1.1      cgd 			continue;
    174  1.1      cgd 		if ((pn = find_person(user.ut_name)) == NULL) {
    175  1.1      cgd 			bcopy(user.ut_name, name, UT_NAMESIZE);
    176  1.1      cgd 			if ((pw = getpwnam(name)) == NULL)
    177  1.1      cgd 				continue;
    178  1.1      cgd 			pn = enter_person(pw);
    179  1.1      cgd 		}
    180  1.1      cgd 		enter_where(&user, pn);
    181  1.1      cgd 	}
    182  1.1      cgd 	for (pn = phead; lflag && pn != NULL; pn = pn->next)
    183  1.1      cgd 		enter_lastlog(pn);
    184  1.1      cgd }
    185  1.1      cgd 
    186  1.5    lukem void
    187  1.1      cgd userlist(argc, argv)
    188  1.5    lukem 	int argc;
    189  1.5    lukem 	char **argv;
    190  1.1      cgd {
    191  1.5    lukem 	register int i;
    192  1.1      cgd 	register PERSON *pn;
    193  1.1      cgd 	PERSON *nethead, **nettail;
    194  1.1      cgd 	struct utmp user;
    195  1.1      cgd 	struct passwd *pw;
    196  1.1      cgd 	int dolocal, *used;
    197  1.1      cgd 
    198  1.1      cgd 	if (!(used = (int *)calloc((u_int)argc, (u_int)sizeof(int)))) {
    199  1.1      cgd 		(void)fprintf(stderr, "finger: out of space.\n");
    200  1.1      cgd 		exit(1);
    201  1.1      cgd 	}
    202  1.1      cgd 
    203  1.1      cgd 	/* pull out all network requests */
    204  1.1      cgd 	for (i = 0, dolocal = 0, nettail = &nethead; i < argc; i++) {
    205  1.5    lukem 		if (!strchr(argv[i], '@')) {
    206  1.1      cgd 			dolocal = 1;
    207  1.1      cgd 			continue;
    208  1.1      cgd 		}
    209  1.1      cgd 		pn = palloc();
    210  1.1      cgd 		*nettail = pn;
    211  1.1      cgd 		nettail = &pn->next;
    212  1.1      cgd 		pn->name = argv[i];
    213  1.1      cgd 		used[i] = -1;
    214  1.1      cgd 	}
    215  1.1      cgd 	*nettail = NULL;
    216  1.1      cgd 
    217  1.1      cgd 	if (!dolocal)
    218  1.1      cgd 		goto net;
    219  1.1      cgd 
    220  1.1      cgd 	/*
    221  1.1      cgd 	 * traverse the list of possible login names and check the login name
    222  1.1      cgd 	 * and real name against the name specified by the user.
    223  1.1      cgd 	 */
    224  1.1      cgd 	if (mflag) {
    225  1.1      cgd 		for (i = 0; i < argc; i++)
    226  1.1      cgd 			if (used[i] >= 0 && (pw = getpwnam(argv[i]))) {
    227  1.1      cgd 				enter_person(pw);
    228  1.1      cgd 				used[i] = 1;
    229  1.1      cgd 			}
    230  1.5    lukem 	} else while ((pw = getpwent()) != NULL)
    231  1.1      cgd 		for (i = 0; i < argc; i++)
    232  1.1      cgd 			if (used[i] >= 0 &&
    233  1.1      cgd 			    (!strcasecmp(pw->pw_name, argv[i]) ||
    234  1.1      cgd 			    match(pw, argv[i]))) {
    235  1.1      cgd 				enter_person(pw);
    236  1.1      cgd 				used[i] = 1;
    237  1.1      cgd 			}
    238  1.1      cgd 
    239  1.1      cgd 	/* list errors */
    240  1.1      cgd 	for (i = 0; i < argc; i++)
    241  1.1      cgd 		if (!used[i])
    242  1.1      cgd 			(void)fprintf(stderr,
    243  1.1      cgd 			    "finger: %s: no such user.\n", argv[i]);
    244  1.1      cgd 
    245  1.1      cgd 	/* handle network requests */
    246  1.1      cgd net:	for (pn = nethead; pn; pn = pn->next) {
    247  1.1      cgd 		netfinger(pn->name);
    248  1.1      cgd 		if (pn->next || entries)
    249  1.1      cgd 			putchar('\n');
    250  1.1      cgd 	}
    251  1.1      cgd 
    252  1.1      cgd 	if (entries == 0)
    253  1.1      cgd 		return;
    254  1.1      cgd 
    255  1.1      cgd 	/*
    256  1.1      cgd 	 * Scan thru the list of users currently logged in, saving
    257  1.1      cgd 	 * appropriate data whenever a match occurs.
    258  1.1      cgd 	 */
    259  1.1      cgd 	if (!freopen(_PATH_UTMP, "r", stdin)) {
    260  1.1      cgd 		(void)fprintf( stderr, "finger: can't read %s.\n", _PATH_UTMP);
    261  1.1      cgd 		exit(1);
    262  1.1      cgd 	}
    263  1.1      cgd 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
    264  1.1      cgd 		if (!user.ut_name[0])
    265  1.1      cgd 			continue;
    266  1.1      cgd 		if ((pn = find_person(user.ut_name)) == NULL)
    267  1.1      cgd 			continue;
    268  1.1      cgd 		enter_where(&user, pn);
    269  1.1      cgd 	}
    270  1.1      cgd 	for (pn = phead; pn != NULL; pn = pn->next)
    271  1.1      cgd 		enter_lastlog(pn);
    272  1.1      cgd }
    273