xref: /csrg-svn/usr.bin/finger/finger.c (revision 40027)
121554Sdist /*
237659Sbostic  * Copyright (c) 1989 The Regents of the University of California.
335627Sbostic  * All rights reserved.
435627Sbostic  *
5*40027Sbostic  * This code is derived from software contributed to Berkeley by
6*40027Sbostic  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
7*40027Sbostic  *
835627Sbostic  * Redistribution and use in source and binary forms are permitted
935627Sbostic  * provided that the above copyright notice and this paragraph are
1035627Sbostic  * duplicated in all such forms and that any documentation,
1135627Sbostic  * advertising materials, and other materials related to such
1235627Sbostic  * distribution and use acknowledge that the software was developed
1335627Sbostic  * by the University of California, Berkeley.  The name of the
1435627Sbostic  * University may not be used to endorse or promote products derived
1535627Sbostic  * from this software without specific prior written permission.
1635627Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1735627Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1837659Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1921554Sdist  */
2021554Sdist 
2113619Ssam #ifndef lint
2221554Sdist char copyright[] =
2337659Sbostic "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
2421554Sdist  All rights reserved.\n";
2535627Sbostic #endif /* not lint */
261014Sbill 
2721554Sdist #ifndef lint
28*40027Sbostic static char sccsid[] = "@(#)finger.c	5.19 (Berkeley) 02/07/90";
2935627Sbostic #endif /* not lint */
3021554Sdist 
3118606Sedward /*
3237659Sbostic  * Finger prints out information about users.  It is not portable since
3337659Sbostic  * certain fields (e.g. the full user name, office, and phone numbers) are
3437659Sbostic  * extracted from the gecos field of the passwd file which other UNIXes
3537659Sbostic  * may not have or may use for other things.
361014Sbill  *
3737659Sbostic  * There are currently two output formats; the short format is one line
3837659Sbostic  * per user and displays login name, tty, login time, real name, idle time,
3937659Sbostic  * and office location/phone number.  The long format gives the same
4037659Sbostic  * information (in a more legible format) as well as home directory, shell,
4137659Sbostic  * mail info, and .plan/.project files.
421014Sbill  */
431014Sbill 
4437659Sbostic #include <sys/param.h>
4537659Sbostic #include <sys/file.h>
4637629Sbostic #include <stdio.h>
4737659Sbostic #include "finger.h"
4837629Sbostic #include "pathnames.h"
491014Sbill 
5037659Sbostic time_t now;
5137664Sedward int lflag, sflag, mflag, pplan;
5237659Sbostic char tbuf[1024];
531014Sbill 
5418606Sedward main(argc, argv)
5518606Sedward 	int argc;
5637659Sbostic 	char **argv;
5718606Sedward {
5837659Sbostic 	extern int optind;
5937659Sbostic 	int ch;
6037659Sbostic 	time_t time();
611014Sbill 
6237659Sbostic 	while ((ch = getopt(argc, argv, "lmps")) != EOF)
6337659Sbostic 		switch(ch) {
6437659Sbostic 		case 'l':
6537659Sbostic 			lflag = 1;		/* long format */
6637659Sbostic 			break;
6737659Sbostic 		case 'm':
6837659Sbostic 			mflag = 1;		/* force exact match of names */
6937659Sbostic 			break;
7037659Sbostic 		case 'p':
7137659Sbostic 			pplan = 1;		/* don't show .plan/.project */
7237659Sbostic 			break;
7337659Sbostic 		case 's':
7437659Sbostic 			sflag = 1;		/* short format */
7537659Sbostic 			break;
7637659Sbostic 		case '?':
7737659Sbostic 		default:
7837659Sbostic 			(void)fprintf(stderr,
7937659Sbostic 			    "usage: finger [-lmps] [login ...]\n");
8037659Sbostic 			exit(1);
8137659Sbostic 		}
8237659Sbostic 	argc -= optind;
8337659Sbostic 	argv += optind;
8437659Sbostic 
8537659Sbostic 	(void)time(&now);
8637659Sbostic 	setpassent(1);
8737659Sbostic 	if (!*argv) {
8837659Sbostic 		/*
8937659Sbostic 		 * Assign explicit "small" format if no names given and -l
9037659Sbostic 		 * not selected.  Force the -s BEFORE we get names so proper
9137659Sbostic 		 * screening will be done.
9237659Sbostic 		 */
9337659Sbostic 		if (!lflag)
9437659Sbostic 			sflag = 1;	/* if -l not explicit, force -s */
9537659Sbostic 		loginlist();
9637664Sedward 		if (entries == 0)
9737659Sbostic 			(void)printf("No one logged on.\n");
9837659Sbostic 	} else {
9938586Sedward 		userlist(argc, argv);
10037659Sbostic 		/*
10137659Sbostic 		 * Assign explicit "large" format if names given and -s not
10237659Sbostic 		 * explicitly stated.  Force the -l AFTER we get names so any
10337659Sbostic 		 * remote finger attempts specified won't be mishandled.
10437659Sbostic 		 */
10537659Sbostic 		if (!sflag)
10637659Sbostic 			lflag = 1;	/* if -s not explicit, force -l */
10737659Sbostic 	}
10837664Sedward 	if (entries != 0) {
10937659Sbostic 		if (lflag)
11037659Sbostic 			lflag_print();
11137659Sbostic 		else
11237659Sbostic 			sflag_print();
11337659Sbostic 	}
11418606Sedward 	exit(0);
11518606Sedward }
1161014Sbill 
11737659Sbostic loginlist()
1181014Sbill {
11937659Sbostic 	register PERSON *pn;
12037659Sbostic 	struct passwd *pw;
12137664Sedward 	struct utmp user;
12237664Sedward 	char name[UT_NAMESIZE + 1];
1231014Sbill 
12438901Sbostic 	if (!freopen(_PATH_UTMP, "r", stdin)) {
12537659Sbostic 		(void)fprintf(stderr, "finger: can't read %s.\n", _PATH_UTMP);
12618606Sedward 		exit(2);
1271014Sbill 	}
12837659Sbostic 	name[UT_NAMESIZE] = NULL;
12938901Sbostic 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
13037659Sbostic 		if (!user.ut_name[0])
13118606Sedward 			continue;
13237664Sedward 		if ((pn = find_person(user.ut_name)) == NULL) {
13337664Sedward 			bcopy(user.ut_name, name, UT_NAMESIZE);
13437664Sedward 			if ((pw = getpwnam(name)) == NULL)
13537664Sedward 				continue;
13637664Sedward 			pn = enter_person(pw);
1371014Sbill 		}
13837664Sedward 		enter_where(&user, pn);
13918606Sedward 	}
14037664Sedward 	for (pn = phead; lflag && pn != NULL; pn = pn->next)
14137664Sedward 		enter_lastlog(pn);
14218606Sedward }
1431014Sbill 
14438586Sedward userlist(argc, argv)
14538586Sedward 	register argc;
14638586Sedward 	register char **argv;
14718606Sedward {
14838586Sedward 	register i;
14937664Sedward 	register PERSON *pn;
15037664Sedward 	PERSON *nethead;
15137664Sedward 	struct utmp user;
15237664Sedward 	struct passwd *pw;
15338586Sedward 	int dolocal, *used;
15438586Sedward 	char *index();
1551014Sbill 
15638586Sedward 	if (!(used = (int *)calloc((u_int)argc, (u_int)sizeof(int)))) {
15738586Sedward 		(void)fprintf(stderr, "finger: out of space.\n");
15838586Sedward 		exit(1);
15938586Sedward 	}
16038586Sedward 
16137659Sbostic 	/* pull out all network requests */
16238586Sedward 	for (i = 0, dolocal = 0, nethead = NULL; i < argc; i++) {
16338586Sedward 		if (!index(argv[i], '@')) {
16437659Sbostic 			dolocal = 1;
16518606Sedward 			continue;
16616469Ssam 		}
16737664Sedward 		pn = palloc();
16837659Sbostic 		pn->next = nethead;
16937659Sbostic 		nethead = pn;
17038586Sedward 		pn->name = argv[i];
17138586Sedward 		used[i] = -1;
17218606Sedward 	}
17337659Sbostic 
17437659Sbostic 	if (!dolocal)
17537659Sbostic 		goto net;
17637659Sbostic 
17718606Sedward 	/*
17837659Sbostic 	 * traverse the list of possible login names and check the login name
17937664Sedward 	 * and real name against the name specified by the user.
18018606Sedward 	 */
18137996Sbostic 	if (mflag) {
18238586Sedward 		for (i = 0; i < argc; i++)
18338586Sedward 			if (used[i] >= 0 && (pw = getpwnam(argv[i]))) {
18437996Sbostic 				enter_person(pw);
18538586Sedward 				used[i] = 1;
18637996Sbostic 			}
18737996Sbostic 	} else while (pw = getpwent())
18838586Sedward 		for (i = 0; i < argc; i++)
18938586Sedward 			if (used[i] >= 0 &&
19038586Sedward 			    (!strcasecmp(pw->pw_name, argv[i]) ||
19138586Sedward 			    match(pw, argv[i]))) {
19237996Sbostic 				enter_person(pw);
19338586Sedward 				used[i] = 1;
19437996Sbostic 			}
19537659Sbostic 
19637659Sbostic 	/* list errors */
19738586Sedward 	for (i = 0; i < argc; i++)
19838586Sedward 		if (!used[i])
19937659Sbostic 			(void)fprintf(stderr,
20038586Sedward 			    "finger: %s: no such user.\n", argv[i]);
20137659Sbostic 
20237659Sbostic 	/* handle network requests */
20337659Sbostic net:	for (pn = nethead; pn; pn = pn->next) {
20437659Sbostic 		netfinger(pn->name);
20537659Sbostic 		if (pn->next || entries)
20637659Sbostic 			putchar('\n');
2071014Sbill 	}
2081014Sbill 
20937664Sedward 	if (entries == 0)
21037659Sbostic 		return;
2111014Sbill 
21218606Sedward 	/*
21337659Sbostic 	 * Scan thru the list of users currently logged in, saving
21437659Sbostic 	 * appropriate data whenever a match occurs.
21518606Sedward 	 */
21638901Sbostic 	if (!freopen(_PATH_UTMP, "r", stdin)) {
21737659Sbostic 		(void)fprintf( stderr, "finger: can't read %s.\n", _PATH_UTMP);
21837659Sbostic 		exit(1);
21918606Sedward 	}
22038901Sbostic 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
22137659Sbostic 		if (!user.ut_name[0])
22218606Sedward 			continue;
22337664Sedward 		if ((pn = find_person(user.ut_name)) == NULL)
22437664Sedward 			continue;
22537664Sedward 		enter_where(&user, pn);
22618606Sedward 	}
22737664Sedward 	for (pn = phead; pn != NULL; pn = pn->next)
22837664Sedward 		enter_lastlog(pn);
2291014Sbill }
230