1*28d00b8aSnia /* $NetBSD: finger.c,v 1.31 2021/10/30 09:12:09 nia Exp $ */
29d225a17Stls
361f28255Scgd /*
4987dbad8Smrg * Copyright (c) 1989, 1993
5987dbad8Smrg * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * This code is derived from software contributed to Berkeley by
861f28255Scgd * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
961f28255Scgd *
1061f28255Scgd * Redistribution and use in source and binary forms, with or without
1161f28255Scgd * modification, are permitted provided that the following conditions
1261f28255Scgd * are met:
1361f28255Scgd * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd * notice, this list of conditions and the following disclaimer.
1561f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd * notice, this list of conditions and the following disclaimer in the
1761f28255Scgd * documentation and/or other materials provided with the distribution.
1889aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd * may be used to endorse or promote products derived from this software
2061f28255Scgd * without specific prior written permission.
2161f28255Scgd *
2261f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd * SUCH DAMAGE.
3361f28255Scgd */
3461f28255Scgd
354eae27f3Sbrezak /*
360f5a0c15Ssalo * Luke Mewburn <lukem@NetBSD.org> added the following on 961121:
373ccb8ba1Slukem * - mail status ("No Mail", "Mail read:...", or "New Mail ...,
383ccb8ba1Slukem * Unread since ...".)
393ccb8ba1Slukem * - 4 digit phone extensions (3210 is printed as x3210.)
403ccb8ba1Slukem * - host/office toggling in short format with -h & -o.
413ccb8ba1Slukem * - short day names (`Tue' printed instead of `Jun 21' if the
423ccb8ba1Slukem * login time is < 6 days.
434eae27f3Sbrezak */
444eae27f3Sbrezak
45987dbad8Smrg #include <sys/cdefs.h>
4661f28255Scgd #ifndef lint
4798e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
4898e5374cSlukem The Regents of the University of California. All rights reserved.");
4961f28255Scgd #endif /* not lint */
5061f28255Scgd
5161f28255Scgd #ifndef lint
52987dbad8Smrg #if 0
53987dbad8Smrg static char sccsid[] = "@(#)finger.c 8.5 (Berkeley) 5/4/95";
54987dbad8Smrg #else
55*28d00b8aSnia __RCSID("$NetBSD: finger.c,v 1.31 2021/10/30 09:12:09 nia Exp $");
56987dbad8Smrg #endif
5761f28255Scgd #endif /* not lint */
5861f28255Scgd
5961f28255Scgd /*
6061f28255Scgd * Finger prints out information about users. It is not portable since
6161f28255Scgd * certain fields (e.g. the full user name, office, and phone numbers) are
6261f28255Scgd * extracted from the gecos field of the passwd file which other UNIXes
6361f28255Scgd * may not have or may use for other things.
6461f28255Scgd *
6561f28255Scgd * There are currently two output formats; the short format is one line
6661f28255Scgd * per user and displays login name, tty, login time, real name, idle time,
673ccb8ba1Slukem * and either remote host information (default) or office location/phone
683ccb8ba1Slukem * number, depending on if -h or -o is used respectively.
693ccb8ba1Slukem * The long format gives the same information (in a more legible format) as
703ccb8ba1Slukem * well as home directory, shell, mail info, and .plan/.project files.
7161f28255Scgd */
7261f28255Scgd
7361f28255Scgd #include <sys/param.h>
74987dbad8Smrg
75987dbad8Smrg #include <db.h>
76987dbad8Smrg #include <err.h>
77987dbad8Smrg #include <errno.h>
78987dbad8Smrg #include <fcntl.h>
79987dbad8Smrg #include <pwd.h>
8061f28255Scgd #include <stdio.h>
81fcb1f857Scgd #include <stdlib.h>
823ccb8ba1Slukem #include <string.h>
833ccb8ba1Slukem #include <time.h>
84987dbad8Smrg #include <unistd.h>
858bff4369Schristos
86dfb75c5aSkim #include <locale.h>
87dfb75c5aSkim #include <langinfo.h>
88dfb75c5aSkim
898bff4369Schristos #include "utmpentry.h"
90987dbad8Smrg
9161f28255Scgd #include "finger.h"
923ccb8ba1Slukem #include "extern.h"
9361f28255Scgd
94987dbad8Smrg DB *db;
9561f28255Scgd time_t now;
96b3399fd7Skim int entries, gflag, lflag, mflag, oflag, sflag, eightflag, pplan;
9761f28255Scgd char tbuf[1024];
988bff4369Schristos struct utmpentry *ehead;
9961f28255Scgd
100ee5c979cSperry static void loginlist(void);
101ee5c979cSperry static void userlist(int, char **);
102987dbad8Smrg
1033ccb8ba1Slukem int
main(int argc,char ** argv)104ee5c979cSperry main(int argc, char **argv)
10561f28255Scgd {
10661f28255Scgd int ch;
10761f28255Scgd
108dfb75c5aSkim /* Allow user's locale settings to affect character output. */
10970daf20dSmrg setlocale(LC_CTYPE, "");
110dfb75c5aSkim
111dfb75c5aSkim /*
112dfb75c5aSkim * Reset back to the C locale, unless we are using a known
113dfb75c5aSkim * single-byte 8-bit locale.
114dfb75c5aSkim */
115dfb75c5aSkim if (strncmp(nl_langinfo(CODESET), "ISO8859-", 8))
11670daf20dSmrg setlocale(LC_CTYPE, "C");
117dfb75c5aSkim
1183ccb8ba1Slukem oflag = 1; /* default to old "office" behavior */
1193ccb8ba1Slukem
120b3399fd7Skim while ((ch = getopt(argc, argv, "lmpshog8")) != -1)
12161f28255Scgd switch(ch) {
12261f28255Scgd case 'l':
12361f28255Scgd lflag = 1; /* long format */
12461f28255Scgd break;
12561f28255Scgd case 'm':
12661f28255Scgd mflag = 1; /* force exact match of names */
12761f28255Scgd break;
12861f28255Scgd case 'p':
12961f28255Scgd pplan = 1; /* don't show .plan/.project */
13061f28255Scgd break;
13161f28255Scgd case 's':
13261f28255Scgd sflag = 1; /* short format */
13361f28255Scgd break;
1343ccb8ba1Slukem case 'h':
1353ccb8ba1Slukem oflag = 0; /* remote host info */
1363ccb8ba1Slukem break;
1373ccb8ba1Slukem case 'o':
1383ccb8ba1Slukem oflag = 1; /* office info */
1393ccb8ba1Slukem break;
14027032902Smrg case 'g':
14127032902Smrg gflag = 1; /* no gecos info, besides name */
14227032902Smrg break;
143b3399fd7Skim case '8':
144b3399fd7Skim eightflag = 1; /* 8-bit pass-through */
145b3399fd7Skim break;
14661f28255Scgd case '?':
14761f28255Scgd default:
14861f28255Scgd (void)fprintf(stderr,
149b3399fd7Skim "usage: finger [-lmpshog8] [login ...]\n");
15061f28255Scgd exit(1);
15161f28255Scgd }
15261f28255Scgd argc -= optind;
15361f28255Scgd argv += optind;
15461f28255Scgd
15561f28255Scgd (void)time(&now);
15661f28255Scgd setpassent(1);
1578bff4369Schristos entries = getutentries(NULL, &ehead);
158a626c509Stron if (argc == 0) {
15961f28255Scgd /*
16061f28255Scgd * Assign explicit "small" format if no names given and -l
16161f28255Scgd * not selected. Force the -s BEFORE we get names so proper
16261f28255Scgd * screening will be done.
16361f28255Scgd */
16461f28255Scgd if (!lflag)
16561f28255Scgd sflag = 1; /* if -l not explicit, force -s */
16661f28255Scgd loginlist();
16761f28255Scgd if (entries == 0)
16861f28255Scgd (void)printf("No one logged on.\n");
16961f28255Scgd } else {
17061f28255Scgd userlist(argc, argv);
17161f28255Scgd /*
17261f28255Scgd * Assign explicit "large" format if names given and -s not
17361f28255Scgd * explicitly stated. Force the -l AFTER we get names so any
17461f28255Scgd * remote finger attempts specified won't be mishandled.
17561f28255Scgd */
17661f28255Scgd if (!sflag)
17761f28255Scgd lflag = 1; /* if -s not explicit, force -l */
17861f28255Scgd }
179f670fa10Sross if (entries) {
18061f28255Scgd if (lflag)
18161f28255Scgd lflag_print();
18261f28255Scgd else
18361f28255Scgd sflag_print();
184f670fa10Sross }
185987dbad8Smrg return (0);
18661f28255Scgd }
18761f28255Scgd
188987dbad8Smrg static void
loginlist(void)189ee5c979cSperry loginlist(void)
19061f28255Scgd {
1913ccb8ba1Slukem PERSON *pn;
192987dbad8Smrg DBT data, key;
19361f28255Scgd struct passwd *pw;
194f8d0e355Slukem int r, seqflag;
1958bff4369Schristos struct utmpentry *ep;
19661f28255Scgd
1978bff4369Schristos for (ep = ehead; ep; ep = ep->next) {
1988bff4369Schristos if ((pn = find_person(ep->name)) == NULL) {
1998bff4369Schristos if ((pw = getpwnam(ep->name)) == NULL)
20061f28255Scgd continue;
20161f28255Scgd pn = enter_person(pw);
20261f28255Scgd }
2038bff4369Schristos enter_where(ep, pn);
20461f28255Scgd }
205987dbad8Smrg if (db && lflag)
206f8d0e355Slukem for (seqflag = R_FIRST;; seqflag = R_NEXT) {
207987dbad8Smrg PERSON *tmp;
208987dbad8Smrg
209f8d0e355Slukem r = (*db->seq)(db, &key, &data, seqflag);
210987dbad8Smrg if (r == -1)
211987dbad8Smrg err(1, "db seq");
212987dbad8Smrg if (r == 1)
213987dbad8Smrg break;
214987dbad8Smrg memmove(&tmp, data.data, sizeof tmp);
215987dbad8Smrg enter_lastlog(tmp);
216987dbad8Smrg }
21761f28255Scgd }
21861f28255Scgd
219987dbad8Smrg static void
userlist(int argc,char ** argv)220ee5c979cSperry userlist(int argc, char **argv)
22161f28255Scgd {
222ee5c979cSperry PERSON *pn;
223987dbad8Smrg DBT data, key;
22461f28255Scgd struct passwd *pw;
225f8d0e355Slukem int r, seqflag, *used, *ip;
226987dbad8Smrg char **ap, **nargv, **np, **p;
2278bff4369Schristos struct utmpentry *ep;
22861f28255Scgd
229*28d00b8aSnia nargv = NULL;
230*28d00b8aSnia if (reallocarr(&nargv, argc + 1, sizeof(char *)) != 0)
231*28d00b8aSnia err(1, NULL);
232*28d00b8aSnia if ((used = calloc(argc, sizeof(int))) == NULL)
233987dbad8Smrg err(1, NULL);
23461f28255Scgd
235987dbad8Smrg /* Pull out all network requests. */
236987dbad8Smrg for (ap = p = argv, np = nargv; *p; ++p)
237b4775af2Schristos if (strchr(*p, '@'))
238987dbad8Smrg *np++ = *p;
239987dbad8Smrg else
240987dbad8Smrg *ap++ = *p;
24161f28255Scgd
242987dbad8Smrg *np++ = NULL;
243987dbad8Smrg *ap++ = NULL;
244987dbad8Smrg
245987dbad8Smrg if (!*argv)
24661f28255Scgd goto net;
24761f28255Scgd
24861f28255Scgd /*
249987dbad8Smrg * Traverse the list of possible login names and check the login name
25061f28255Scgd * and real name against the name specified by the user.
25161f28255Scgd */
25261f28255Scgd if (mflag) {
253987dbad8Smrg for (p = argv; *p; ++p)
254987dbad8Smrg if ((pw = getpwnam(*p)) != NULL)
25561f28255Scgd enter_person(pw);
256987dbad8Smrg else
257e8877ca7Schristos warnx("%s: no such user", *p);
258987dbad8Smrg } else {
259987dbad8Smrg while ((pw = getpwent()) != NULL)
260987dbad8Smrg for (p = argv, ip = used; *p; ++p, ++ip)
261987dbad8Smrg if (match(pw, *p)) {
262987dbad8Smrg enter_person(pw);
263987dbad8Smrg *ip = 1;
26461f28255Scgd }
265987dbad8Smrg for (p = argv, ip = used; *p; ++p, ++ip)
266987dbad8Smrg if (!*ip)
267e8877ca7Schristos warnx("%s: no such user", *p);
268987dbad8Smrg }
269987dbad8Smrg
270987dbad8Smrg /* Handle network requests. */
271987dbad8Smrg net:
272987dbad8Smrg for (p = nargv; *p;)
273987dbad8Smrg netfinger(*p++);
27461f28255Scgd
27561f28255Scgd if (entries == 0)
276e8877ca7Schristos goto done;
27761f28255Scgd
27861f28255Scgd /*
27961f28255Scgd * Scan thru the list of users currently logged in, saving
28061f28255Scgd * appropriate data whenever a match occurs.
28161f28255Scgd */
2828bff4369Schristos for (ep = ehead; ep; ep = ep->next) {
2838bff4369Schristos if ((pn = find_person(ep->name)) == NULL)
28461f28255Scgd continue;
2858bff4369Schristos enter_where(ep, pn);
28661f28255Scgd }
287a626c509Stron if (db != NULL)
288f8d0e355Slukem for (seqflag = R_FIRST;; seqflag = R_NEXT) {
289987dbad8Smrg PERSON *tmp;
290987dbad8Smrg
291f8d0e355Slukem r = (*db->seq)(db, &key, &data, seqflag);
292987dbad8Smrg if (r == -1)
293987dbad8Smrg err(1, "db seq");
294987dbad8Smrg if (r == 1)
295987dbad8Smrg break;
296987dbad8Smrg memmove(&tmp, data.data, sizeof tmp);
297987dbad8Smrg enter_lastlog(tmp);
298987dbad8Smrg }
299e8877ca7Schristos done:
300e8877ca7Schristos free(nargv);
301e8877ca7Schristos free(used);
30261f28255Scgd }
303