Home | History | Annotate | Line # | Download | only in finger
finger.c revision 1.11
      1  1.11   perry /*	$NetBSD: finger.c,v 1.11 1998/08/10 03:11:07 perry Exp $	*/
      2   1.6     tls 
      3   1.1     cgd /*
      4  1.10     mrg  * Copyright (c) 1989, 1993
      5  1.10     mrg  *	The Regents of the University of California.  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.10     mrg #include <sys/cdefs.h>
     50   1.1     cgd #ifndef lint
     51  1.10     mrg __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
     52  1.10     mrg 	The Regents of the University of California.  All rights reserved.\n");
     53   1.1     cgd #endif /* not lint */
     54   1.1     cgd 
     55   1.1     cgd #ifndef lint
     56  1.10     mrg #if 0
     57  1.10     mrg static char sccsid[] = "@(#)finger.c	8.5 (Berkeley) 5/4/95";
     58  1.10     mrg #else
     59  1.11   perry __RCSID("$NetBSD: finger.c,v 1.11 1998/08/10 03:11:07 perry Exp $");
     60  1.10     mrg #endif
     61   1.1     cgd #endif /* not lint */
     62   1.1     cgd 
     63   1.1     cgd /*
     64   1.1     cgd  * Finger prints out information about users.  It is not portable since
     65   1.1     cgd  * certain fields (e.g. the full user name, office, and phone numbers) are
     66   1.1     cgd  * extracted from the gecos field of the passwd file which other UNIXes
     67   1.1     cgd  * may not have or may use for other things.
     68   1.1     cgd  *
     69   1.1     cgd  * There are currently two output formats; the short format is one line
     70   1.1     cgd  * per user and displays login name, tty, login time, real name, idle time,
     71   1.5   lukem  * and either remote host information (default) or office location/phone
     72   1.5   lukem  * number, depending on if -h or -o is used respectively.
     73   1.5   lukem  * The long format gives the same information (in a more legible format) as
     74   1.5   lukem  * well as home directory, shell, mail info, and .plan/.project files.
     75   1.1     cgd  */
     76   1.1     cgd 
     77   1.1     cgd #include <sys/param.h>
     78  1.10     mrg 
     79  1.10     mrg #include <db.h>
     80  1.10     mrg #include <err.h>
     81  1.10     mrg #include <errno.h>
     82  1.10     mrg #include <fcntl.h>
     83  1.10     mrg #include <pwd.h>
     84   1.1     cgd #include <stdio.h>
     85   1.4     cgd #include <stdlib.h>
     86   1.5   lukem #include <string.h>
     87   1.5   lukem #include <time.h>
     88  1.10     mrg #include <unistd.h>
     89  1.10     mrg #include <utmp.h>
     90  1.10     mrg 
     91   1.1     cgd #include "finger.h"
     92   1.5   lukem #include "extern.h"
     93   1.1     cgd 
     94  1.10     mrg DB *db;
     95   1.1     cgd time_t now;
     96  1.10     mrg int entries, gflag, lflag, mflag, oflag, sflag, pplan;
     97   1.1     cgd char tbuf[1024];
     98   1.1     cgd 
     99  1.10     mrg static void loginlist __P((void));
    100  1.10     mrg static void userlist __P((int, char **));
    101  1.10     mrg int main __P((int, char **));
    102  1.10     mrg 
    103   1.5   lukem int
    104   1.1     cgd main(argc, argv)
    105   1.1     cgd 	int argc;
    106   1.1     cgd 	char **argv;
    107   1.1     cgd {
    108   1.1     cgd 	int ch;
    109   1.1     cgd 
    110   1.5   lukem 	oflag = 1;		/* default to old "office" behavior */
    111   1.5   lukem 
    112   1.9   lukem 	while ((ch = getopt(argc, argv, "lmpshog")) != -1)
    113   1.1     cgd 		switch(ch) {
    114   1.1     cgd 		case 'l':
    115   1.1     cgd 			lflag = 1;		/* long format */
    116   1.1     cgd 			break;
    117   1.1     cgd 		case 'm':
    118   1.1     cgd 			mflag = 1;		/* force exact match of names */
    119   1.1     cgd 			break;
    120   1.1     cgd 		case 'p':
    121   1.1     cgd 			pplan = 1;		/* don't show .plan/.project */
    122   1.1     cgd 			break;
    123   1.1     cgd 		case 's':
    124   1.1     cgd 			sflag = 1;		/* short format */
    125   1.1     cgd 			break;
    126   1.5   lukem 		case 'h':
    127   1.5   lukem 			oflag = 0;		/* remote host info */
    128   1.5   lukem 			break;
    129   1.5   lukem 		case 'o':
    130   1.5   lukem 			oflag = 1;		/* office info */
    131   1.8     mrg 			break;
    132   1.8     mrg 		case 'g':
    133   1.8     mrg 			gflag = 1;		/* no gecos info, besides name */
    134   1.5   lukem 			break;
    135   1.1     cgd 		case '?':
    136   1.1     cgd 		default:
    137   1.1     cgd 			(void)fprintf(stderr,
    138   1.5   lukem 			    "usage: finger [-lmpsho] [login ...]\n");
    139   1.1     cgd 			exit(1);
    140   1.1     cgd 		}
    141   1.1     cgd 	argc -= optind;
    142   1.1     cgd 	argv += optind;
    143   1.1     cgd 
    144   1.1     cgd 	(void)time(&now);
    145   1.1     cgd 	setpassent(1);
    146   1.1     cgd 	if (!*argv) {
    147   1.1     cgd 		/*
    148   1.1     cgd 		 * Assign explicit "small" format if no names given and -l
    149   1.1     cgd 		 * not selected.  Force the -s BEFORE we get names so proper
    150   1.1     cgd 		 * screening will be done.
    151   1.1     cgd 		 */
    152   1.1     cgd 		if (!lflag)
    153   1.1     cgd 			sflag = 1;	/* if -l not explicit, force -s */
    154   1.1     cgd 		loginlist();
    155   1.1     cgd 		if (entries == 0)
    156   1.1     cgd 			(void)printf("No one logged on.\n");
    157   1.1     cgd 	} else {
    158   1.1     cgd 		userlist(argc, argv);
    159   1.1     cgd 		/*
    160   1.1     cgd 		 * Assign explicit "large" format if names given and -s not
    161   1.1     cgd 		 * explicitly stated.  Force the -l AFTER we get names so any
    162   1.1     cgd 		 * remote finger attempts specified won't be mishandled.
    163   1.1     cgd 		 */
    164   1.1     cgd 		if (!sflag)
    165   1.1     cgd 			lflag = 1;	/* if -s not explicit, force -l */
    166   1.1     cgd 	}
    167  1.10     mrg 	if (entries)
    168   1.1     cgd 		if (lflag)
    169   1.1     cgd 			lflag_print();
    170   1.1     cgd 		else
    171   1.1     cgd 			sflag_print();
    172  1.10     mrg 	return (0);
    173   1.1     cgd }
    174   1.1     cgd 
    175  1.10     mrg static void
    176   1.1     cgd loginlist()
    177   1.1     cgd {
    178   1.5   lukem 	PERSON *pn;
    179  1.10     mrg 	DBT data, key;
    180   1.1     cgd 	struct passwd *pw;
    181   1.1     cgd 	struct utmp user;
    182  1.10     mrg 	int r, sflag;
    183   1.1     cgd 	char name[UT_NAMESIZE + 1];
    184   1.1     cgd 
    185  1.10     mrg 	if (!freopen(_PATH_UTMP, "r", stdin))
    186  1.10     mrg 		err(1, "cant read %s", _PATH_UTMP);
    187   1.7      pk 	name[UT_NAMESIZE] = '\0';
    188   1.1     cgd 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
    189   1.1     cgd 		if (!user.ut_name[0])
    190   1.1     cgd 			continue;
    191   1.1     cgd 		if ((pn = find_person(user.ut_name)) == NULL) {
    192  1.11   perry 			memcpy(name, user.ut_name, UT_NAMESIZE);
    193   1.1     cgd 			if ((pw = getpwnam(name)) == NULL)
    194   1.1     cgd 				continue;
    195   1.1     cgd 			pn = enter_person(pw);
    196   1.1     cgd 		}
    197   1.1     cgd 		enter_where(&user, pn);
    198   1.1     cgd 	}
    199  1.10     mrg 	if (db && lflag)
    200  1.10     mrg 		for (sflag = R_FIRST;; sflag = R_NEXT) {
    201  1.10     mrg 			PERSON *tmp;
    202  1.10     mrg 
    203  1.10     mrg 			r = (*db->seq)(db, &key, &data, sflag);
    204  1.10     mrg 			if (r == -1)
    205  1.10     mrg 				err(1, "db seq");
    206  1.10     mrg 			if (r == 1)
    207  1.10     mrg 				break;
    208  1.10     mrg 			memmove(&tmp, data.data, sizeof tmp);
    209  1.10     mrg 			enter_lastlog(tmp);
    210  1.10     mrg 		}
    211   1.1     cgd }
    212   1.1     cgd 
    213  1.10     mrg static void
    214   1.1     cgd userlist(argc, argv)
    215   1.5   lukem 	int argc;
    216   1.5   lukem 	char **argv;
    217   1.1     cgd {
    218   1.1     cgd 	register PERSON *pn;
    219  1.10     mrg 	DBT data, key;
    220   1.1     cgd 	struct utmp user;
    221   1.1     cgd 	struct passwd *pw;
    222  1.10     mrg 	int r, sflag, *used, *ip;
    223  1.10     mrg 	char **ap, **nargv, **np, **p;
    224   1.1     cgd 
    225  1.10     mrg 	if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
    226  1.10     mrg 	    (used = calloc(argc, sizeof(int))) == NULL)
    227  1.10     mrg #ifdef __GNUC__
    228  1.10     mrg 		err(1, "%s", "");	/* XXX gcc */
    229  1.10     mrg #else
    230  1.10     mrg 		err(1, NULL);
    231  1.10     mrg #endif
    232  1.10     mrg 
    233  1.10     mrg 	/* Pull out all network requests. */
    234  1.10     mrg 	for (ap = p = argv, np = nargv; *p; ++p)
    235  1.10     mrg 		if (index(*p, '@'))
    236  1.10     mrg 			*np++ = *p;
    237  1.10     mrg 		else
    238  1.10     mrg 			*ap++ = *p;
    239   1.1     cgd 
    240  1.10     mrg 	*np++ = NULL;
    241  1.10     mrg 	*ap++ = NULL;
    242   1.1     cgd 
    243  1.10     mrg 	if (!*argv)
    244   1.1     cgd 		goto net;
    245   1.1     cgd 
    246   1.1     cgd 	/*
    247  1.10     mrg 	 * Traverse the list of possible login names and check the login name
    248   1.1     cgd 	 * and real name against the name specified by the user.
    249   1.1     cgd 	 */
    250   1.1     cgd 	if (mflag) {
    251  1.10     mrg 		for (p = argv; *p; ++p)
    252  1.10     mrg 			if ((pw = getpwnam(*p)) != NULL)
    253   1.1     cgd 				enter_person(pw);
    254  1.10     mrg 			else
    255  1.10     mrg 				(void)fprintf(stderr,
    256  1.10     mrg 				    "finger: %s: no such user\n", *p);
    257  1.10     mrg 	} else {
    258  1.10     mrg 		while ((pw = getpwent()) != NULL)
    259  1.10     mrg 			for (p = argv, ip = used; *p; ++p, ++ip)
    260  1.10     mrg 				if (match(pw, *p)) {
    261  1.10     mrg 					enter_person(pw);
    262  1.10     mrg 					*ip = 1;
    263  1.10     mrg 				}
    264  1.10     mrg 		for (p = argv, ip = used; *p; ++p, ++ip)
    265  1.10     mrg 			if (!*ip)
    266  1.10     mrg 				(void)fprintf(stderr,
    267  1.10     mrg 				    "finger: %s: no such user\n", *p);
    268  1.10     mrg 	}
    269   1.1     cgd 
    270  1.10     mrg 	/* Handle network requests. */
    271  1.10     mrg net:
    272  1.10     mrg 	for (p = nargv; *p;)
    273  1.10     mrg 		netfinger(*p++);
    274   1.1     cgd 
    275   1.1     cgd 	if (entries == 0)
    276   1.1     cgd 		return;
    277   1.1     cgd 
    278   1.1     cgd 	/*
    279   1.1     cgd 	 * Scan thru the list of users currently logged in, saving
    280   1.1     cgd 	 * appropriate data whenever a match occurs.
    281   1.1     cgd 	 */
    282  1.10     mrg 	if (!freopen(_PATH_UTMP, "r", stdin))
    283  1.10     mrg 		err(1, "%s", _PATH_UTMP);
    284   1.1     cgd 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
    285   1.1     cgd 		if (!user.ut_name[0])
    286   1.1     cgd 			continue;
    287   1.1     cgd 		if ((pn = find_person(user.ut_name)) == NULL)
    288   1.1     cgd 			continue;
    289   1.1     cgd 		enter_where(&user, pn);
    290   1.1     cgd 	}
    291  1.10     mrg 	if (db)
    292  1.10     mrg 		for (sflag = R_FIRST;; sflag = R_NEXT) {
    293  1.10     mrg 			PERSON *tmp;
    294  1.10     mrg 
    295  1.10     mrg 			r = (*db->seq)(db, &key, &data, sflag);
    296  1.10     mrg 			if (r == -1)
    297  1.10     mrg 				err(1, "db seq");
    298  1.10     mrg 			if (r == 1)
    299  1.10     mrg 				break;
    300  1.10     mrg 			memmove(&tmp, data.data, sizeof tmp);
    301  1.10     mrg 			enter_lastlog(tmp);
    302  1.10     mrg 		}
    303   1.1     cgd }
    304