19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1989, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by 69b50d902SRodney W. Grimes * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 79b50d902SRodney W. Grimes * 89b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 99b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 109b50d902SRodney W. Grimes * are met: 119b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 129b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 139b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 159b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 169b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 179b50d902SRodney W. Grimes * must display the following acknowledgement: 189b50d902SRodney W. Grimes * This product includes software developed by the University of 199b50d902SRodney W. Grimes * California, Berkeley and its contributors. 209b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 219b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 229b50d902SRodney W. Grimes * without specific prior written permission. 239b50d902SRodney W. Grimes * 249b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 259b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 269b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 279b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 289b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 299b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 309b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 319b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 329b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 339b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 349b50d902SRodney W. Grimes * SUCH DAMAGE. 359b50d902SRodney W. Grimes */ 369b50d902SRodney W. Grimes 3786641d8fSPaul Traina /* 3886641d8fSPaul Traina * Luke Mewburn <lm@rmit.edu.au> added the following on 940622: 3986641d8fSPaul Traina * - mail status ("No Mail", "Mail read:...", or "New Mail ..., 4086641d8fSPaul Traina * Unread since ...".) 4186641d8fSPaul Traina * - 4 digit phone extensions (3210 is printed as x3210.) 4286641d8fSPaul Traina * - host/office toggling in short format with -h & -o. 4386641d8fSPaul Traina * - short day names (`Tue' printed instead of `Jun 21' if the 4486641d8fSPaul Traina * login time is < 6 days. 4586641d8fSPaul Traina */ 4686641d8fSPaul Traina 479b50d902SRodney W. Grimes #ifndef lint 489b50d902SRodney W. Grimes static char copyright[] = 499b50d902SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\ 509b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 519b50d902SRodney W. Grimes #endif /* not lint */ 529b50d902SRodney W. Grimes 539b50d902SRodney W. Grimes #ifndef lint 549b50d902SRodney W. Grimes static char sccsid[] = "@(#)finger.c 8.2 (Berkeley) 9/30/93"; 559b50d902SRodney W. Grimes #endif /* not lint */ 569b50d902SRodney W. Grimes 579b50d902SRodney W. Grimes /* 589b50d902SRodney W. Grimes * Finger prints out information about users. It is not portable since 599b50d902SRodney W. Grimes * certain fields (e.g. the full user name, office, and phone numbers) are 609b50d902SRodney W. Grimes * extracted from the gecos field of the passwd file which other UNIXes 619b50d902SRodney W. Grimes * may not have or may use for other things. 629b50d902SRodney W. Grimes * 639b50d902SRodney W. Grimes * There are currently two output formats; the short format is one line 649b50d902SRodney W. Grimes * per user and displays login name, tty, login time, real name, idle time, 6586641d8fSPaul Traina * and either remote host information (default) or office location/phone 6686641d8fSPaul Traina * number, depending on if -h or -o is used respectively. 6786641d8fSPaul Traina * The long format gives the same information (in a more legible format) as 6886641d8fSPaul Traina * well as home directory, shell, mail info, and .plan/.project files. 699b50d902SRodney W. Grimes */ 709b50d902SRodney W. Grimes 719b50d902SRodney W. Grimes #include <sys/param.h> 729b50d902SRodney W. Grimes #include <fcntl.h> 739b50d902SRodney W. Grimes #include <time.h> 749b50d902SRodney W. Grimes #include <pwd.h> 759b50d902SRodney W. Grimes #include <utmp.h> 769b50d902SRodney W. Grimes #include <errno.h> 779b50d902SRodney W. Grimes #include <stdio.h> 789b50d902SRodney W. Grimes #include <stdlib.h> 799b50d902SRodney W. Grimes #include <string.h> 809b50d902SRodney W. Grimes #include <db.h> 81dc675fc2SAndrey A. Chernov #include <locale.h> 829b50d902SRodney W. Grimes #include "finger.h" 839b50d902SRodney W. Grimes 849b50d902SRodney W. Grimes DB *db; 859b50d902SRodney W. Grimes time_t now; 8686641d8fSPaul Traina int entries, lflag, mflag, pplan, sflag, oflag; 879b50d902SRodney W. Grimes char tbuf[1024]; 889b50d902SRodney W. Grimes 899b50d902SRodney W. Grimes static void loginlist __P((void)); 909b50d902SRodney W. Grimes static void userlist __P((int, char **)); 919b50d902SRodney W. Grimes 9259be6088SPaul Traina int 9359be6088SPaul Traina option(argc, argv) 949b50d902SRodney W. Grimes int argc; 959b50d902SRodney W. Grimes char **argv; 969b50d902SRodney W. Grimes { 979b50d902SRodney W. Grimes int ch; 989b50d902SRodney W. Grimes 9959be6088SPaul Traina optind = 1; /* reset getopt */ 10086641d8fSPaul Traina 10186641d8fSPaul Traina while ((ch = getopt(argc, argv, "lmpsho")) != EOF) 1029b50d902SRodney W. Grimes switch(ch) { 1039b50d902SRodney W. Grimes case 'l': 1049b50d902SRodney W. Grimes lflag = 1; /* long format */ 1059b50d902SRodney W. Grimes break; 1069b50d902SRodney W. Grimes case 'm': 1079b50d902SRodney W. Grimes mflag = 1; /* force exact match of names */ 1089b50d902SRodney W. Grimes break; 1099b50d902SRodney W. Grimes case 'p': 1109b50d902SRodney W. Grimes pplan = 1; /* don't show .plan/.project */ 1119b50d902SRodney W. Grimes break; 1129b50d902SRodney W. Grimes case 's': 1139b50d902SRodney W. Grimes sflag = 1; /* short format */ 1149b50d902SRodney W. Grimes break; 11586641d8fSPaul Traina case 'h': 11686641d8fSPaul Traina oflag = 0; /* remote host info */ 11786641d8fSPaul Traina break; 11886641d8fSPaul Traina case 'o': 11986641d8fSPaul Traina oflag = 1; /* office info */ 12086641d8fSPaul Traina break; 1219b50d902SRodney W. Grimes case '?': 1229b50d902SRodney W. Grimes default: 1239b50d902SRodney W. Grimes (void)fprintf(stderr, 12486641d8fSPaul Traina "usage: finger [-lmpsho] [login ...]\n"); 1259b50d902SRodney W. Grimes exit(1); 1269b50d902SRodney W. Grimes } 12759be6088SPaul Traina 12859be6088SPaul Traina return optind; 12959be6088SPaul Traina } 13059be6088SPaul Traina 13159be6088SPaul Traina main(argc, argv) 13259be6088SPaul Traina int argc; 13359be6088SPaul Traina char **argv; 13459be6088SPaul Traina { 13559be6088SPaul Traina int ch, envargc, argcnt; 13659be6088SPaul Traina char *envargv[3]; 13759be6088SPaul Traina 138d1b2ad1aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 139dc675fc2SAndrey A. Chernov 14059be6088SPaul Traina /* remove this line to get remote host */ 14159be6088SPaul Traina oflag = 1; /* default to old "office" behavior */ 14259be6088SPaul Traina 14359be6088SPaul Traina /* 14459be6088SPaul Traina * Process environment variables followed by command line arguments. 14559be6088SPaul Traina */ 14659be6088SPaul Traina if ((envargv[1] = getenv("FINGER"))) { 14759be6088SPaul Traina envargc = 2; 14859be6088SPaul Traina envargv[0] = "finger"; 14959be6088SPaul Traina envargv[2] = NULL; 15059be6088SPaul Traina (void) option(envargc, envargv); 15159be6088SPaul Traina } 15259be6088SPaul Traina 15359be6088SPaul Traina argcnt = option(argc, argv); 15459be6088SPaul Traina argc -= argcnt; 15559be6088SPaul Traina argv += argcnt; 1569b50d902SRodney W. Grimes 1579b50d902SRodney W. Grimes (void)time(&now); 1589b50d902SRodney W. Grimes setpassent(1); 1599b50d902SRodney W. Grimes if (!*argv) { 1609b50d902SRodney W. Grimes /* 1619b50d902SRodney W. Grimes * Assign explicit "small" format if no names given and -l 1629b50d902SRodney W. Grimes * not selected. Force the -s BEFORE we get names so proper 1639b50d902SRodney W. Grimes * screening will be done. 1649b50d902SRodney W. Grimes */ 1659b50d902SRodney W. Grimes if (!lflag) 1669b50d902SRodney W. Grimes sflag = 1; /* if -l not explicit, force -s */ 1679b50d902SRodney W. Grimes loginlist(); 1689b50d902SRodney W. Grimes if (entries == 0) 1699b50d902SRodney W. Grimes (void)printf("No one logged on.\n"); 1709b50d902SRodney W. Grimes } else { 1719b50d902SRodney W. Grimes userlist(argc, argv); 1729b50d902SRodney W. Grimes /* 1739b50d902SRodney W. Grimes * Assign explicit "large" format if names given and -s not 1749b50d902SRodney W. Grimes * explicitly stated. Force the -l AFTER we get names so any 1759b50d902SRodney W. Grimes * remote finger attempts specified won't be mishandled. 1769b50d902SRodney W. Grimes */ 1779b50d902SRodney W. Grimes if (!sflag) 1789b50d902SRodney W. Grimes lflag = 1; /* if -s not explicit, force -l */ 1799b50d902SRodney W. Grimes } 1809b50d902SRodney W. Grimes if (entries) 1819b50d902SRodney W. Grimes if (lflag) 1829b50d902SRodney W. Grimes lflag_print(); 1839b50d902SRodney W. Grimes else 1849b50d902SRodney W. Grimes sflag_print(); 1859b50d902SRodney W. Grimes exit(0); 1869b50d902SRodney W. Grimes } 1879b50d902SRodney W. Grimes 1889b50d902SRodney W. Grimes static void 1899b50d902SRodney W. Grimes loginlist() 1909b50d902SRodney W. Grimes { 1919b50d902SRodney W. Grimes register PERSON *pn; 1929b50d902SRodney W. Grimes DBT data, key; 1939b50d902SRodney W. Grimes struct passwd *pw; 1949b50d902SRodney W. Grimes struct utmp user; 1959b50d902SRodney W. Grimes int r, sflag; 1969b50d902SRodney W. Grimes char name[UT_NAMESIZE + 1]; 1979b50d902SRodney W. Grimes 1989b50d902SRodney W. Grimes if (!freopen(_PATH_UTMP, "r", stdin)) 1999b50d902SRodney W. Grimes err("%s: %s", _PATH_UTMP, strerror(errno)); 2009b50d902SRodney W. Grimes name[UT_NAMESIZE] = NULL; 2019b50d902SRodney W. Grimes while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 2029b50d902SRodney W. Grimes if (!user.ut_name[0]) 2039b50d902SRodney W. Grimes continue; 2049b50d902SRodney W. Grimes if ((pn = find_person(user.ut_name)) == NULL) { 2059b50d902SRodney W. Grimes bcopy(user.ut_name, name, UT_NAMESIZE); 2069b50d902SRodney W. Grimes if ((pw = getpwnam(name)) == NULL) 2079b50d902SRodney W. Grimes continue; 2088829c73eSJordan K. Hubbard if (hide(pw)) 2098829c73eSJordan K. Hubbard continue; 2109b50d902SRodney W. Grimes pn = enter_person(pw); 2119b50d902SRodney W. Grimes } 2129b50d902SRodney W. Grimes enter_where(&user, pn); 2139b50d902SRodney W. Grimes } 2149b50d902SRodney W. Grimes if (db && lflag) 2159b50d902SRodney W. Grimes for (sflag = R_FIRST;; sflag = R_NEXT) { 2169b50d902SRodney W. Grimes r = (*db->seq)(db, &key, &data, sflag); 2179b50d902SRodney W. Grimes if (r == -1) 2189b50d902SRodney W. Grimes err("db seq: %s", strerror(errno)); 2199b50d902SRodney W. Grimes if (r == 1) 2209b50d902SRodney W. Grimes break; 2219b50d902SRodney W. Grimes enter_lastlog(*(PERSON **)data.data); 2229b50d902SRodney W. Grimes } 2239b50d902SRodney W. Grimes } 2249b50d902SRodney W. Grimes 2259b50d902SRodney W. Grimes static void 2269b50d902SRodney W. Grimes userlist(argc, argv) 2279b50d902SRodney W. Grimes register int argc; 2289b50d902SRodney W. Grimes register char **argv; 2299b50d902SRodney W. Grimes { 2309b50d902SRodney W. Grimes register PERSON *pn; 2319b50d902SRodney W. Grimes DBT data, key; 2329b50d902SRodney W. Grimes struct utmp user; 2339b50d902SRodney W. Grimes struct passwd *pw; 2349b50d902SRodney W. Grimes int r, sflag, *used, *ip; 2359b50d902SRodney W. Grimes char **ap, **nargv, **np, **p; 2369b50d902SRodney W. Grimes 2379b50d902SRodney W. Grimes if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL || 2389b50d902SRodney W. Grimes (used = calloc(argc, sizeof(int))) == NULL) 2399b50d902SRodney W. Grimes err("%s", strerror(errno)); 2409b50d902SRodney W. Grimes 2419b50d902SRodney W. Grimes /* Pull out all network requests. */ 2429b50d902SRodney W. Grimes for (ap = p = argv, np = nargv; *p; ++p) 2439b50d902SRodney W. Grimes if (index(*p, '@')) 2449b50d902SRodney W. Grimes *np++ = *p; 2459b50d902SRodney W. Grimes else 2469b50d902SRodney W. Grimes *ap++ = *p; 2479b50d902SRodney W. Grimes 2489b50d902SRodney W. Grimes *np++ = NULL; 2499b50d902SRodney W. Grimes *ap++ = NULL; 2509b50d902SRodney W. Grimes 2519b50d902SRodney W. Grimes if (!*argv) 2529b50d902SRodney W. Grimes goto net; 2539b50d902SRodney W. Grimes 2549b50d902SRodney W. Grimes /* 2559b50d902SRodney W. Grimes * Traverse the list of possible login names and check the login name 2569b50d902SRodney W. Grimes * and real name against the name specified by the user. 2579b50d902SRodney W. Grimes */ 2589b50d902SRodney W. Grimes if (mflag) 2599b50d902SRodney W. Grimes for (p = argv; *p; ++p) 2608829c73eSJordan K. Hubbard if ((pw = getpwnam(*p)) && !hide(pw)) 2619b50d902SRodney W. Grimes enter_person(pw); 2629b50d902SRodney W. Grimes else 2639b50d902SRodney W. Grimes (void)fprintf(stderr, 2649b50d902SRodney W. Grimes "finger: %s: no such user\n", *p); 2659b50d902SRodney W. Grimes else { 2668829c73eSJordan K. Hubbard while (pw = getpwent()) { 2679b50d902SRodney W. Grimes for (p = argv, ip = used; *p; ++p, ++ip) 268c2ac9540SAndrey A. Chernov if (match(pw, *p) && !hide(pw)) { 2699b50d902SRodney W. Grimes enter_person(pw); 2709b50d902SRodney W. Grimes *ip = 1; 2719b50d902SRodney W. Grimes } 2728829c73eSJordan K. Hubbard } 2739b50d902SRodney W. Grimes for (p = argv, ip = used; *p; ++p, ++ip) 2749b50d902SRodney W. Grimes if (!*ip) 2759b50d902SRodney W. Grimes (void)fprintf(stderr, 2769b50d902SRodney W. Grimes "finger: %s: no such user\n", *p); 2779b50d902SRodney W. Grimes } 2789b50d902SRodney W. Grimes 2799b50d902SRodney W. Grimes /* Handle network requests. */ 280a5af661eSPaul Traina net: for (p = nargv; *p;) { 2819b50d902SRodney W. Grimes netfinger(*p++); 282a5af661eSPaul Traina if (*p || entries) 283a5af661eSPaul Traina printf("\n"); 284a5af661eSPaul Traina } 2859b50d902SRodney W. Grimes 2869b50d902SRodney W. Grimes if (entries == 0) 2879b50d902SRodney W. Grimes return; 2889b50d902SRodney W. Grimes 2899b50d902SRodney W. Grimes /* 2909b50d902SRodney W. Grimes * Scan thru the list of users currently logged in, saving 2919b50d902SRodney W. Grimes * appropriate data whenever a match occurs. 2929b50d902SRodney W. Grimes */ 2939b50d902SRodney W. Grimes if (!freopen(_PATH_UTMP, "r", stdin)) 2949b50d902SRodney W. Grimes err("%s: %s", _PATH_UTMP, strerror(errno)); 2959b50d902SRodney W. Grimes while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 2969b50d902SRodney W. Grimes if (!user.ut_name[0]) 2979b50d902SRodney W. Grimes continue; 2989b50d902SRodney W. Grimes if ((pn = find_person(user.ut_name)) == NULL) 2999b50d902SRodney W. Grimes continue; 3009b50d902SRodney W. Grimes enter_where(&user, pn); 3019b50d902SRodney W. Grimes } 3029b50d902SRodney W. Grimes if (db) 3039b50d902SRodney W. Grimes for (sflag = R_FIRST;; sflag = R_NEXT) { 3049b50d902SRodney W. Grimes r = (*db->seq)(db, &key, &data, sflag); 3059b50d902SRodney W. Grimes if (r == -1) 3069b50d902SRodney W. Grimes err("db seq: %s", strerror(errno)); 3079b50d902SRodney W. Grimes if (r == 1) 3089b50d902SRodney W. Grimes break; 3099b50d902SRodney W. Grimes enter_lastlog(*(PERSON **)data.data); 3109b50d902SRodney W. Grimes } 3119b50d902SRodney W. Grimes } 312