finger.c revision 4991
11590Srgrimes/* 21590Srgrimes * Copyright (c) 1989, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * This code is derived from software contributed to Berkeley by 61590Srgrimes * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 71590Srgrimes * 81590Srgrimes * Redistribution and use in source and binary forms, with or without 91590Srgrimes * modification, are permitted provided that the following conditions 101590Srgrimes * are met: 111590Srgrimes * 1. Redistributions of source code must retain the above copyright 121590Srgrimes * notice, this list of conditions and the following disclaimer. 131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141590Srgrimes * notice, this list of conditions and the following disclaimer in the 151590Srgrimes * documentation and/or other materials provided with the distribution. 161590Srgrimes * 3. All advertising materials mentioning features or use of this software 171590Srgrimes * must display the following acknowledgement: 181590Srgrimes * This product includes software developed by the University of 191590Srgrimes * California, Berkeley and its contributors. 201590Srgrimes * 4. Neither the name of the University nor the names of its contributors 211590Srgrimes * may be used to endorse or promote products derived from this software 221590Srgrimes * without specific prior written permission. 231590Srgrimes * 241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341590Srgrimes * SUCH DAMAGE. 351590Srgrimes */ 361590Srgrimes 372537Spst/* 382537Spst * Luke Mewburn <lm@rmit.edu.au> added the following on 940622: 392537Spst * - mail status ("No Mail", "Mail read:...", or "New Mail ..., 402537Spst * Unread since ...".) 412537Spst * - 4 digit phone extensions (3210 is printed as x3210.) 422537Spst * - host/office toggling in short format with -h & -o. 432537Spst * - short day names (`Tue' printed instead of `Jun 21' if the 442537Spst * login time is < 6 days. 452537Spst */ 462537Spst 471590Srgrimes#ifndef lint 481590Srgrimesstatic char copyright[] = 491590Srgrimes"@(#) Copyright (c) 1989, 1993\n\ 501590Srgrimes The Regents of the University of California. All rights reserved.\n"; 511590Srgrimes#endif /* not lint */ 521590Srgrimes 531590Srgrimes#ifndef lint 541590Srgrimesstatic char sccsid[] = "@(#)finger.c 8.2 (Berkeley) 9/30/93"; 551590Srgrimes#endif /* not lint */ 561590Srgrimes 571590Srgrimes/* 581590Srgrimes * Finger prints out information about users. It is not portable since 591590Srgrimes * certain fields (e.g. the full user name, office, and phone numbers) are 601590Srgrimes * extracted from the gecos field of the passwd file which other UNIXes 611590Srgrimes * may not have or may use for other things. 621590Srgrimes * 631590Srgrimes * There are currently two output formats; the short format is one line 641590Srgrimes * per user and displays login name, tty, login time, real name, idle time, 652537Spst * and either remote host information (default) or office location/phone 662537Spst * number, depending on if -h or -o is used respectively. 672537Spst * The long format gives the same information (in a more legible format) as 682537Spst * well as home directory, shell, mail info, and .plan/.project files. 691590Srgrimes */ 701590Srgrimes 711590Srgrimes#include <sys/param.h> 721590Srgrimes#include <fcntl.h> 731590Srgrimes#include <time.h> 741590Srgrimes#include <pwd.h> 751590Srgrimes#include <utmp.h> 761590Srgrimes#include <errno.h> 771590Srgrimes#include <stdio.h> 781590Srgrimes#include <stdlib.h> 791590Srgrimes#include <string.h> 801590Srgrimes#include <db.h> 811590Srgrimes#include "finger.h" 821590Srgrimes 831590SrgrimesDB *db; 841590Srgrimestime_t now; 852537Spstint entries, lflag, mflag, pplan, sflag, oflag; 861590Srgrimeschar tbuf[1024]; 871590Srgrimes 881590Srgrimesstatic void loginlist __P((void)); 891590Srgrimesstatic void userlist __P((int, char **)); 901590Srgrimes 912589Spstint 922589Spstoption(argc, argv) 931590Srgrimes int argc; 941590Srgrimes char **argv; 951590Srgrimes{ 961590Srgrimes int ch; 971590Srgrimes 982589Spst optind = 1; /* reset getopt */ 992537Spst 1002537Spst while ((ch = getopt(argc, argv, "lmpsho")) != EOF) 1011590Srgrimes switch(ch) { 1021590Srgrimes case 'l': 1031590Srgrimes lflag = 1; /* long format */ 1041590Srgrimes break; 1051590Srgrimes case 'm': 1061590Srgrimes mflag = 1; /* force exact match of names */ 1071590Srgrimes break; 1081590Srgrimes case 'p': 1091590Srgrimes pplan = 1; /* don't show .plan/.project */ 1101590Srgrimes break; 1111590Srgrimes case 's': 1121590Srgrimes sflag = 1; /* short format */ 1131590Srgrimes break; 1142537Spst case 'h': 1152537Spst oflag = 0; /* remote host info */ 1162537Spst break; 1172537Spst case 'o': 1182537Spst oflag = 1; /* office info */ 1192537Spst break; 1201590Srgrimes case '?': 1211590Srgrimes default: 1221590Srgrimes (void)fprintf(stderr, 1232537Spst "usage: finger [-lmpsho] [login ...]\n"); 1241590Srgrimes exit(1); 1251590Srgrimes } 1261590Srgrimes 1272589Spst return optind; 1282589Spst} 1292589Spst 1302589Spstmain(argc, argv) 1312589Spst int argc; 1322589Spst char **argv; 1332589Spst{ 1342589Spst int ch, envargc, argcnt; 1352589Spst char *envargv[3]; 1362589Spst 1372589Spst /* remove this line to get remote host */ 1382589Spst oflag = 1; /* default to old "office" behavior */ 1392589Spst 1402589Spst /* 1412589Spst * Process environment variables followed by command line arguments. 1422589Spst */ 1432589Spst if ((envargv[1] = getenv("FINGER"))) { 1442589Spst envargc = 2; 1452589Spst envargv[0] = "finger"; 1462589Spst envargv[2] = NULL; 1472589Spst (void) option(envargc, envargv); 1482589Spst } 1492589Spst 1502589Spst argcnt = option(argc, argv); 1512589Spst argc -= argcnt; 1522589Spst argv += argcnt; 1532589Spst 1541590Srgrimes (void)time(&now); 1551590Srgrimes setpassent(1); 1561590Srgrimes if (!*argv) { 1571590Srgrimes /* 1581590Srgrimes * Assign explicit "small" format if no names given and -l 1591590Srgrimes * not selected. Force the -s BEFORE we get names so proper 1601590Srgrimes * screening will be done. 1611590Srgrimes */ 1621590Srgrimes if (!lflag) 1631590Srgrimes sflag = 1; /* if -l not explicit, force -s */ 1641590Srgrimes loginlist(); 1651590Srgrimes if (entries == 0) 1661590Srgrimes (void)printf("No one logged on.\n"); 1671590Srgrimes } else { 1681590Srgrimes userlist(argc, argv); 1691590Srgrimes /* 1701590Srgrimes * Assign explicit "large" format if names given and -s not 1711590Srgrimes * explicitly stated. Force the -l AFTER we get names so any 1721590Srgrimes * remote finger attempts specified won't be mishandled. 1731590Srgrimes */ 1741590Srgrimes if (!sflag) 1751590Srgrimes lflag = 1; /* if -s not explicit, force -l */ 1761590Srgrimes } 1771590Srgrimes if (entries) 1781590Srgrimes if (lflag) 1791590Srgrimes lflag_print(); 1801590Srgrimes else 1811590Srgrimes sflag_print(); 1821590Srgrimes exit(0); 1831590Srgrimes} 1841590Srgrimes 1851590Srgrimesstatic void 1861590Srgrimesloginlist() 1871590Srgrimes{ 1881590Srgrimes register PERSON *pn; 1891590Srgrimes DBT data, key; 1901590Srgrimes struct passwd *pw; 1911590Srgrimes struct utmp user; 1921590Srgrimes int r, sflag; 1931590Srgrimes char name[UT_NAMESIZE + 1]; 1941590Srgrimes 1951590Srgrimes if (!freopen(_PATH_UTMP, "r", stdin)) 1961590Srgrimes err("%s: %s", _PATH_UTMP, strerror(errno)); 1971590Srgrimes name[UT_NAMESIZE] = NULL; 1981590Srgrimes while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 1991590Srgrimes if (!user.ut_name[0]) 2001590Srgrimes continue; 2011590Srgrimes if ((pn = find_person(user.ut_name)) == NULL) { 2021590Srgrimes bcopy(user.ut_name, name, UT_NAMESIZE); 2031590Srgrimes if ((pw = getpwnam(name)) == NULL) 2041590Srgrimes continue; 2051590Srgrimes pn = enter_person(pw); 2061590Srgrimes } 2071590Srgrimes enter_where(&user, pn); 2081590Srgrimes } 2091590Srgrimes if (db && lflag) 2101590Srgrimes for (sflag = R_FIRST;; sflag = R_NEXT) { 2111590Srgrimes r = (*db->seq)(db, &key, &data, sflag); 2121590Srgrimes if (r == -1) 2131590Srgrimes err("db seq: %s", strerror(errno)); 2141590Srgrimes if (r == 1) 2151590Srgrimes break; 2161590Srgrimes enter_lastlog(*(PERSON **)data.data); 2171590Srgrimes } 2181590Srgrimes} 2191590Srgrimes 2201590Srgrimesstatic void 2211590Srgrimesuserlist(argc, argv) 2221590Srgrimes register int argc; 2231590Srgrimes register char **argv; 2241590Srgrimes{ 2251590Srgrimes register PERSON *pn; 2261590Srgrimes DBT data, key; 2271590Srgrimes struct utmp user; 2281590Srgrimes struct passwd *pw; 2291590Srgrimes int r, sflag, *used, *ip; 2301590Srgrimes char **ap, **nargv, **np, **p; 2311590Srgrimes 2321590Srgrimes if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL || 2331590Srgrimes (used = calloc(argc, sizeof(int))) == NULL) 2341590Srgrimes err("%s", strerror(errno)); 2351590Srgrimes 2361590Srgrimes /* Pull out all network requests. */ 2371590Srgrimes for (ap = p = argv, np = nargv; *p; ++p) 2381590Srgrimes if (index(*p, '@')) 2391590Srgrimes *np++ = *p; 2401590Srgrimes else 2411590Srgrimes *ap++ = *p; 2421590Srgrimes 2431590Srgrimes *np++ = NULL; 2441590Srgrimes *ap++ = NULL; 2451590Srgrimes 2461590Srgrimes if (!*argv) 2471590Srgrimes goto net; 2481590Srgrimes 2491590Srgrimes /* 2501590Srgrimes * Traverse the list of possible login names and check the login name 2511590Srgrimes * and real name against the name specified by the user. 2521590Srgrimes */ 2531590Srgrimes if (mflag) 2541590Srgrimes for (p = argv; *p; ++p) 2551590Srgrimes if (pw = getpwnam(*p)) 2561590Srgrimes enter_person(pw); 2571590Srgrimes else 2581590Srgrimes (void)fprintf(stderr, 2591590Srgrimes "finger: %s: no such user\n", *p); 2601590Srgrimes else { 2611590Srgrimes while (pw = getpwent()) 2621590Srgrimes for (p = argv, ip = used; *p; ++p, ++ip) 2631590Srgrimes if (match(pw, *p)) { 2641590Srgrimes enter_person(pw); 2651590Srgrimes *ip = 1; 2661590Srgrimes } 2671590Srgrimes for (p = argv, ip = used; *p; ++p, ++ip) 2681590Srgrimes if (!*ip) 2691590Srgrimes (void)fprintf(stderr, 2701590Srgrimes "finger: %s: no such user\n", *p); 2711590Srgrimes } 2721590Srgrimes 2731590Srgrimes /* Handle network requests. */ 2744991Spstnet: for (p = nargv; *p;) { 2751590Srgrimes netfinger(*p++); 2764991Spst if (*p || entries) 2774991Spst printf("\n"); 2784991Spst } 2791590Srgrimes 2801590Srgrimes if (entries == 0) 2811590Srgrimes return; 2821590Srgrimes 2831590Srgrimes /* 2841590Srgrimes * Scan thru the list of users currently logged in, saving 2851590Srgrimes * appropriate data whenever a match occurs. 2861590Srgrimes */ 2871590Srgrimes if (!freopen(_PATH_UTMP, "r", stdin)) 2881590Srgrimes err("%s: %s", _PATH_UTMP, strerror(errno)); 2891590Srgrimes while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 2901590Srgrimes if (!user.ut_name[0]) 2911590Srgrimes continue; 2921590Srgrimes if ((pn = find_person(user.ut_name)) == NULL) 2931590Srgrimes continue; 2941590Srgrimes enter_where(&user, pn); 2951590Srgrimes } 2961590Srgrimes if (db) 2971590Srgrimes for (sflag = R_FIRST;; sflag = R_NEXT) { 2981590Srgrimes r = (*db->seq)(db, &key, &data, sflag); 2991590Srgrimes if (r == -1) 3001590Srgrimes err("db seq: %s", strerror(errno)); 3011590Srgrimes if (r == 1) 3021590Srgrimes break; 3031590Srgrimes enter_lastlog(*(PERSON **)data.data); 3041590Srgrimes } 3051590Srgrimes} 306