Home | History | Annotate | Line # | Download | only in finger
finger.c revision 1.29
      1  1.29     lukem /*	$NetBSD: finger.c,v 1.29 2009/04/12 06:18:54 lukem 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.23       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35   1.3    brezak /*
     36  1.22      salo  * Luke Mewburn <lukem (at) NetBSD.org> added the following on 961121:
     37   1.5     lukem  *    - mail status ("No Mail", "Mail read:...", or "New Mail ...,
     38   1.5     lukem  *	Unread since ...".)
     39   1.5     lukem  *    - 4 digit phone extensions (3210 is printed as x3210.)
     40   1.5     lukem  *    - host/office toggling in short format with -h & -o.
     41   1.5     lukem  *    - short day names (`Tue' printed instead of `Jun 21' if the
     42   1.5     lukem  *	login time is < 6 days.
     43   1.3    brezak  */
     44   1.3    brezak 
     45  1.10       mrg #include <sys/cdefs.h>
     46   1.1       cgd #ifndef lint
     47  1.28     lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
     48  1.28     lukem  The Regents of the University of California.  All rights reserved.");
     49   1.1       cgd #endif /* not lint */
     50   1.1       cgd 
     51   1.1       cgd #ifndef lint
     52  1.10       mrg #if 0
     53  1.10       mrg static char sccsid[] = "@(#)finger.c	8.5 (Berkeley) 5/4/95";
     54  1.10       mrg #else
     55  1.29     lukem __RCSID("$NetBSD: finger.c,v 1.29 2009/04/12 06:18:54 lukem Exp $");
     56  1.10       mrg #endif
     57   1.1       cgd #endif /* not lint */
     58   1.1       cgd 
     59   1.1       cgd /*
     60   1.1       cgd  * Finger prints out information about users.  It is not portable since
     61   1.1       cgd  * certain fields (e.g. the full user name, office, and phone numbers) are
     62   1.1       cgd  * extracted from the gecos field of the passwd file which other UNIXes
     63   1.1       cgd  * may not have or may use for other things.
     64   1.1       cgd  *
     65   1.1       cgd  * There are currently two output formats; the short format is one line
     66   1.1       cgd  * per user and displays login name, tty, login time, real name, idle time,
     67   1.5     lukem  * and either remote host information (default) or office location/phone
     68   1.5     lukem  * number, depending on if -h or -o is used respectively.
     69   1.5     lukem  * The long format gives the same information (in a more legible format) as
     70   1.5     lukem  * well as home directory, shell, mail info, and .plan/.project files.
     71   1.1       cgd  */
     72   1.1       cgd 
     73   1.1       cgd #include <sys/param.h>
     74  1.10       mrg 
     75  1.10       mrg #include <db.h>
     76  1.10       mrg #include <err.h>
     77  1.10       mrg #include <errno.h>
     78  1.10       mrg #include <fcntl.h>
     79  1.10       mrg #include <pwd.h>
     80   1.1       cgd #include <stdio.h>
     81   1.4       cgd #include <stdlib.h>
     82   1.5     lukem #include <string.h>
     83   1.5     lukem #include <time.h>
     84  1.10       mrg #include <unistd.h>
     85  1.16  christos 
     86  1.20       kim #include <locale.h>
     87  1.20       kim #include <langinfo.h>
     88  1.20       kim 
     89  1.16  christos #include "utmpentry.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.21       kim int entries, gflag, lflag, mflag, oflag, sflag, eightflag, pplan;
     97   1.1       cgd char tbuf[1024];
     98  1.16  christos struct utmpentry *ehead;
     99   1.1       cgd 
    100  1.25     perry static void loginlist(void);
    101  1.25     perry static void userlist(int, char **);
    102  1.25     perry int main(int, char **);
    103  1.10       mrg 
    104   1.5     lukem int
    105  1.25     perry main(int argc, char **argv)
    106   1.1       cgd {
    107   1.1       cgd 	int ch;
    108  1.20       kim 
    109  1.20       kim 	/* Allow user's locale settings to affect character output. */
    110  1.27       mrg 	setlocale(LC_CTYPE, "");
    111  1.20       kim 
    112  1.20       kim 	/*
    113  1.20       kim 	 * Reset back to the C locale, unless we are using a known
    114  1.20       kim 	 * single-byte 8-bit locale.
    115  1.20       kim 	 */
    116  1.20       kim 	if (strncmp(nl_langinfo(CODESET), "ISO8859-", 8))
    117  1.27       mrg 		setlocale(LC_CTYPE, "C");
    118   1.1       cgd 
    119   1.5     lukem 	oflag = 1;		/* default to old "office" behavior */
    120   1.5     lukem 
    121  1.21       kim 	while ((ch = getopt(argc, argv, "lmpshog8")) != -1)
    122   1.1       cgd 		switch(ch) {
    123   1.1       cgd 		case 'l':
    124   1.1       cgd 			lflag = 1;		/* long format */
    125   1.1       cgd 			break;
    126   1.1       cgd 		case 'm':
    127   1.1       cgd 			mflag = 1;		/* force exact match of names */
    128   1.1       cgd 			break;
    129   1.1       cgd 		case 'p':
    130   1.1       cgd 			pplan = 1;		/* don't show .plan/.project */
    131   1.1       cgd 			break;
    132   1.1       cgd 		case 's':
    133   1.1       cgd 			sflag = 1;		/* short format */
    134   1.1       cgd 			break;
    135   1.5     lukem 		case 'h':
    136   1.5     lukem 			oflag = 0;		/* remote host info */
    137   1.5     lukem 			break;
    138   1.5     lukem 		case 'o':
    139   1.5     lukem 			oflag = 1;		/* office info */
    140   1.8       mrg 			break;
    141   1.8       mrg 		case 'g':
    142   1.8       mrg 			gflag = 1;		/* no gecos info, besides name */
    143   1.5     lukem 			break;
    144  1.21       kim 		case '8':
    145  1.21       kim 			eightflag = 1;		/* 8-bit pass-through */
    146  1.21       kim 			break;
    147   1.1       cgd 		case '?':
    148   1.1       cgd 		default:
    149   1.1       cgd 			(void)fprintf(stderr,
    150  1.21       kim 			    "usage: finger [-lmpshog8] [login ...]\n");
    151   1.1       cgd 			exit(1);
    152   1.1       cgd 		}
    153   1.1       cgd 	argc -= optind;
    154   1.1       cgd 	argv += optind;
    155   1.1       cgd 
    156   1.1       cgd 	(void)time(&now);
    157   1.1       cgd 	setpassent(1);
    158  1.16  christos 	entries = getutentries(NULL, &ehead);
    159  1.17      tron 	if (argc == 0) {
    160   1.1       cgd 		/*
    161   1.1       cgd 		 * Assign explicit "small" format if no names given and -l
    162   1.1       cgd 		 * not selected.  Force the -s BEFORE we get names so proper
    163   1.1       cgd 		 * screening will be done.
    164   1.1       cgd 		 */
    165   1.1       cgd 		if (!lflag)
    166   1.1       cgd 			sflag = 1;	/* if -l not explicit, force -s */
    167   1.1       cgd 		loginlist();
    168   1.1       cgd 		if (entries == 0)
    169   1.1       cgd 			(void)printf("No one logged on.\n");
    170   1.1       cgd 	} else {
    171   1.1       cgd 		userlist(argc, argv);
    172   1.1       cgd 		/*
    173   1.1       cgd 		 * Assign explicit "large" format if names given and -s not
    174   1.1       cgd 		 * explicitly stated.  Force the -l AFTER we get names so any
    175   1.1       cgd 		 * remote finger attempts specified won't be mishandled.
    176   1.1       cgd 		 */
    177   1.1       cgd 		if (!sflag)
    178   1.1       cgd 			lflag = 1;	/* if -s not explicit, force -l */
    179   1.1       cgd 	}
    180  1.12      ross 	if (entries) {
    181   1.1       cgd 		if (lflag)
    182   1.1       cgd 			lflag_print();
    183   1.1       cgd 		else
    184   1.1       cgd 			sflag_print();
    185  1.12      ross 	}
    186  1.10       mrg 	return (0);
    187   1.1       cgd }
    188   1.1       cgd 
    189  1.10       mrg static void
    190  1.25     perry loginlist(void)
    191   1.1       cgd {
    192   1.5     lukem 	PERSON *pn;
    193  1.10       mrg 	DBT data, key;
    194   1.1       cgd 	struct passwd *pw;
    195  1.29     lukem 	int r, seqflag;
    196  1.16  christos 	struct utmpentry *ep;
    197   1.1       cgd 
    198  1.16  christos 	for (ep = ehead; ep; ep = ep->next) {
    199  1.16  christos 		if ((pn = find_person(ep->name)) == NULL) {
    200  1.16  christos 			if ((pw = getpwnam(ep->name)) == NULL)
    201   1.1       cgd 				continue;
    202   1.1       cgd 			pn = enter_person(pw);
    203   1.1       cgd 		}
    204  1.16  christos 		enter_where(ep, pn);
    205   1.1       cgd 	}
    206  1.10       mrg 	if (db && lflag)
    207  1.29     lukem 		for (seqflag = R_FIRST;; seqflag = R_NEXT) {
    208  1.10       mrg 			PERSON *tmp;
    209  1.10       mrg 
    210  1.29     lukem 			r = (*db->seq)(db, &key, &data, seqflag);
    211  1.10       mrg 			if (r == -1)
    212  1.10       mrg 				err(1, "db seq");
    213  1.10       mrg 			if (r == 1)
    214  1.10       mrg 				break;
    215  1.10       mrg 			memmove(&tmp, data.data, sizeof tmp);
    216  1.10       mrg 			enter_lastlog(tmp);
    217  1.10       mrg 		}
    218   1.1       cgd }
    219   1.1       cgd 
    220  1.10       mrg static void
    221  1.25     perry userlist(int argc, char **argv)
    222   1.1       cgd {
    223  1.25     perry 	PERSON *pn;
    224  1.10       mrg 	DBT data, key;
    225   1.1       cgd 	struct passwd *pw;
    226  1.29     lukem 	int r, seqflag, *used, *ip;
    227  1.10       mrg 	char **ap, **nargv, **np, **p;
    228  1.16  christos 	struct utmpentry *ep;
    229   1.1       cgd 
    230  1.10       mrg 	if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
    231  1.10       mrg 	    (used = calloc(argc, sizeof(int))) == NULL)
    232  1.10       mrg 		err(1, NULL);
    233  1.10       mrg 
    234  1.10       mrg 	/* Pull out all network requests. */
    235  1.10       mrg 	for (ap = p = argv, np = nargv; *p; ++p)
    236  1.13  christos 		if (strchr(*p, '@'))
    237  1.10       mrg 			*np++ = *p;
    238  1.10       mrg 		else
    239  1.10       mrg 			*ap++ = *p;
    240   1.1       cgd 
    241  1.10       mrg 	*np++ = NULL;
    242  1.10       mrg 	*ap++ = NULL;
    243   1.1       cgd 
    244  1.10       mrg 	if (!*argv)
    245   1.1       cgd 		goto net;
    246   1.1       cgd 
    247   1.1       cgd 	/*
    248  1.10       mrg 	 * Traverse the list of possible login names and check the login name
    249   1.1       cgd 	 * and real name against the name specified by the user.
    250   1.1       cgd 	 */
    251   1.1       cgd 	if (mflag) {
    252  1.10       mrg 		for (p = argv; *p; ++p)
    253  1.10       mrg 			if ((pw = getpwnam(*p)) != NULL)
    254   1.1       cgd 				enter_person(pw);
    255  1.10       mrg 			else
    256  1.26  christos 				warnx("%s: no such user", *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.26  christos 				warnx("%s: no such user", *p);
    267  1.10       mrg 	}
    268   1.1       cgd 
    269  1.10       mrg 	/* Handle network requests. */
    270  1.10       mrg net:
    271  1.10       mrg 	for (p = nargv; *p;)
    272  1.10       mrg 		netfinger(*p++);
    273   1.1       cgd 
    274   1.1       cgd 	if (entries == 0)
    275  1.26  christos 		goto done;
    276   1.1       cgd 
    277   1.1       cgd 	/*
    278   1.1       cgd 	 * Scan thru the list of users currently logged in, saving
    279   1.1       cgd 	 * appropriate data whenever a match occurs.
    280   1.1       cgd 	 */
    281  1.16  christos 	for (ep = ehead; ep; ep = ep->next) {
    282  1.16  christos 		if ((pn = find_person(ep->name)) == NULL)
    283   1.1       cgd 			continue;
    284  1.16  christos 		enter_where(ep, pn);
    285   1.1       cgd 	}
    286  1.17      tron 	if (db != NULL)
    287  1.29     lukem 		for (seqflag = R_FIRST;; seqflag = R_NEXT) {
    288  1.10       mrg 			PERSON *tmp;
    289  1.10       mrg 
    290  1.29     lukem 			r = (*db->seq)(db, &key, &data, seqflag);
    291  1.10       mrg 			if (r == -1)
    292  1.10       mrg 				err(1, "db seq");
    293  1.10       mrg 			if (r == 1)
    294  1.10       mrg 				break;
    295  1.10       mrg 			memmove(&tmp, data.data, sizeof tmp);
    296  1.10       mrg 			enter_lastlog(tmp);
    297  1.10       mrg 		}
    298  1.26  christos done:
    299  1.26  christos 	free(nargv);
    300  1.26  christos 	free(used);
    301   1.1       cgd }
    302