finger.c revision 168635
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 4867467Srustatic const 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 5387628Sdwmalone#if 0 541590Srgrimes#ifndef lint 5587628Sdwmalonestatic char sccsid[] = "@(#)finger.c 8.5 (Berkeley) 5/4/95"; 5672109Scharnier#endif 5787628Sdwmalone#endif 581590Srgrimes 5987628Sdwmalone#include <sys/cdefs.h> 6087628Sdwmalone__FBSDID("$FreeBSD: head/usr.bin/finger/finger.c 168635 2007-04-11 20:32:09Z des $"); 6187628Sdwmalone 621590Srgrimes/* 631590Srgrimes * Finger prints out information about users. It is not portable since 641590Srgrimes * certain fields (e.g. the full user name, office, and phone numbers) are 651590Srgrimes * extracted from the gecos field of the passwd file which other UNIXes 661590Srgrimes * may not have or may use for other things. 671590Srgrimes * 681590Srgrimes * There are currently two output formats; the short format is one line 691590Srgrimes * per user and displays login name, tty, login time, real name, idle time, 702537Spst * and either remote host information (default) or office location/phone 712537Spst * number, depending on if -h or -o is used respectively. 722537Spst * The long format gives the same information (in a more legible format) as 732537Spst * well as home directory, shell, mail info, and .plan/.project files. 741590Srgrimes */ 751590Srgrimes 76100521Sume#include <sys/types.h> 77100521Sume#include <sys/socket.h> 7823693Speter#include <db.h> 7923693Speter#include <err.h> 801590Srgrimes#include <pwd.h> 811590Srgrimes#include <stdio.h> 821590Srgrimes#include <stdlib.h> 831590Srgrimes#include <string.h> 8423693Speter#include <time.h> 8523693Speter#include <unistd.h> 8623693Speter#include <utmp.h> 8711759Sache#include <locale.h> 8823693Speter 891590Srgrimes#include "finger.h" 9064775Sbrian#include "pathnames.h" 911590Srgrimes 921590SrgrimesDB *db; 931590Srgrimestime_t now; 94168632Sdesint entries, gflag, kflag, lflag, mflag, pplan, sflag, oflag; 95106250Ssobomaxsa_family_t family = PF_UNSPEC; 9674586Sacheint d_first = -1; 971590Srgrimeschar tbuf[1024]; 98150316Sddsint invoker_root = 0; 991590Srgrimes 10092920Simpstatic void loginlist(void); 10192920Simpstatic int option(int, char **); 10292920Simpstatic void usage(void); 10392920Simpstatic void userlist(int, char **); 1041590Srgrimes 10587229Smarkmstatic int 106102944Sdwmaloneoption(int argc, char **argv) 1071590Srgrimes{ 1081590Srgrimes int ch; 1091590Srgrimes 1102589Spst optind = 1; /* reset getopt */ 1112537Spst 112168635Sdes while ((ch = getopt(argc, argv, "46gklmpsho")) != -1) 1131590Srgrimes switch(ch) { 114100521Sume case '4': 115100521Sume family = AF_INET; 116100521Sume break; 117100521Sume case '6': 118100521Sume family = AF_INET6; 119100521Sume break; 12099249Smini case 'g': 12199249Smini gflag = 1; 12299249Smini break; 123117010Sjmallett case 'k': 124117010Sjmallett kflag = 1; /* keep going without utmp */ 125117010Sjmallett break; 1261590Srgrimes case 'l': 1271590Srgrimes lflag = 1; /* long format */ 1281590Srgrimes break; 1291590Srgrimes case 'm': 1301590Srgrimes mflag = 1; /* force exact match of names */ 1311590Srgrimes break; 1321590Srgrimes case 'p': 1331590Srgrimes pplan = 1; /* don't show .plan/.project */ 1341590Srgrimes break; 1351590Srgrimes case 's': 1361590Srgrimes sflag = 1; /* short format */ 1371590Srgrimes break; 1382537Spst case 'h': 1392537Spst oflag = 0; /* remote host info */ 1402537Spst break; 1412537Spst case 'o': 1422537Spst oflag = 1; /* office info */ 1432537Spst break; 1441590Srgrimes case '?': 1451590Srgrimes default: 14627169Scharnier usage(); 1471590Srgrimes } 1481590Srgrimes 1492589Spst return optind; 1502589Spst} 1512589Spst 15227169Scharnierstatic void 153102944Sdwmaloneusage(void) 15427169Scharnier{ 155146466Sru (void)fprintf(stderr, 156168635Sdes "usage: finger [-46gklmpsho] [user ...] [user@host ...]\n"); 15727169Scharnier exit(1); 15827169Scharnier} 15927169Scharnier 16027169Scharnierint 161102944Sdwmalonemain(int argc, char **argv) 1622589Spst{ 16327169Scharnier int envargc, argcnt; 1642589Spst char *envargv[3]; 16546662Sobrien struct passwd *pw; 16687229Smarkm static char myname[] = "finger"; 1672589Spst 16846662Sobrien if (getuid() == 0 || geteuid() == 0) { 169150316Sdds invoker_root = 1; 17046662Sobrien if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) { 17146662Sobrien setgid(pw->pw_gid); 17246662Sobrien setuid(pw->pw_uid); 17346662Sobrien } else { 17446662Sobrien setgid(UNPRIV_UGID); 17546662Sobrien setuid(UNPRIV_UGID); 17646662Sobrien } 17746662Sobrien } 17846662Sobrien 17911811Sache (void) setlocale(LC_ALL, ""); 18011759Sache 1812589Spst /* remove this line to get remote host */ 1822589Spst oflag = 1; /* default to old "office" behavior */ 1832589Spst 1842589Spst /* 1852589Spst * Process environment variables followed by command line arguments. 1862589Spst */ 1872589Spst if ((envargv[1] = getenv("FINGER"))) { 1882589Spst envargc = 2; 18987229Smarkm envargv[0] = myname; 1902589Spst envargv[2] = NULL; 1912589Spst (void) option(envargc, envargv); 1922589Spst } 1932589Spst 1942589Spst argcnt = option(argc, argv); 1952589Spst argc -= argcnt; 1962589Spst argv += argcnt; 1972589Spst 1981590Srgrimes (void)time(&now); 1991590Srgrimes setpassent(1); 2001590Srgrimes if (!*argv) { 2011590Srgrimes /* 2021590Srgrimes * Assign explicit "small" format if no names given and -l 2031590Srgrimes * not selected. Force the -s BEFORE we get names so proper 2041590Srgrimes * screening will be done. 2051590Srgrimes */ 2061590Srgrimes if (!lflag) 2071590Srgrimes sflag = 1; /* if -l not explicit, force -s */ 2081590Srgrimes loginlist(); 2091590Srgrimes if (entries == 0) 2101590Srgrimes (void)printf("No one logged on.\n"); 2111590Srgrimes } else { 2121590Srgrimes userlist(argc, argv); 2131590Srgrimes /* 2141590Srgrimes * Assign explicit "large" format if names given and -s not 2151590Srgrimes * explicitly stated. Force the -l AFTER we get names so any 2161590Srgrimes * remote finger attempts specified won't be mishandled. 2171590Srgrimes */ 2181590Srgrimes if (!sflag) 2191590Srgrimes lflag = 1; /* if -s not explicit, force -l */ 2201590Srgrimes } 22148566Sbillf if (entries) { 2221590Srgrimes if (lflag) 2231590Srgrimes lflag_print(); 2241590Srgrimes else 2251590Srgrimes sflag_print(); 22648566Sbillf } 22723693Speter return (0); 2281590Srgrimes} 2291590Srgrimes 2301590Srgrimesstatic void 231102944Sdwmaloneloginlist(void) 2321590Srgrimes{ 23387229Smarkm PERSON *pn; 2341590Srgrimes DBT data, key; 2351590Srgrimes struct passwd *pw; 2361590Srgrimes struct utmp user; 23787229Smarkm int r, sflag1; 2381590Srgrimes char name[UT_NAMESIZE + 1]; 2391590Srgrimes 240117010Sjmallett if (kflag) 241117010Sjmallett errx(1, "can't list logins without reading utmp"); 242117010Sjmallett 2431590Srgrimes if (!freopen(_PATH_UTMP, "r", stdin)) 24423693Speter err(1, "%s", _PATH_UTMP); 24527169Scharnier name[UT_NAMESIZE] = '\0'; 2461590Srgrimes while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 2471590Srgrimes if (!user.ut_name[0]) 2481590Srgrimes continue; 2491590Srgrimes if ((pn = find_person(user.ut_name)) == NULL) { 2501590Srgrimes bcopy(user.ut_name, name, UT_NAMESIZE); 2511590Srgrimes if ((pw = getpwnam(name)) == NULL) 2521590Srgrimes continue; 2535369Sjkh if (hide(pw)) 2545369Sjkh continue; 2551590Srgrimes pn = enter_person(pw); 2561590Srgrimes } 2571590Srgrimes enter_where(&user, pn); 2581590Srgrimes } 2591590Srgrimes if (db && lflag) 26087229Smarkm for (sflag1 = R_FIRST;; sflag1 = R_NEXT) { 26123693Speter PERSON *tmp; 26223693Speter 26387229Smarkm r = (*db->seq)(db, &key, &data, sflag1); 2641590Srgrimes if (r == -1) 26523693Speter err(1, "db seq"); 2661590Srgrimes if (r == 1) 2671590Srgrimes break; 26823693Speter memmove(&tmp, data.data, sizeof tmp); 26923693Speter enter_lastlog(tmp); 2701590Srgrimes } 2711590Srgrimes} 2721590Srgrimes 2731590Srgrimesstatic void 274102944Sdwmaloneuserlist(int argc, char **argv) 2751590Srgrimes{ 27687229Smarkm PERSON *pn; 2771590Srgrimes DBT data, key; 2781590Srgrimes struct utmp user; 2791590Srgrimes struct passwd *pw; 28087229Smarkm int r, sflag1, *used, *ip; 2811590Srgrimes char **ap, **nargv, **np, **p; 28264775Sbrian FILE *conf_fp; 28364775Sbrian char conf_alias[LINE_MAX]; 28464775Sbrian char *conf_realname; 28564775Sbrian int conf_length; 2861590Srgrimes 2871590Srgrimes if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL || 2881590Srgrimes (used = calloc(argc, sizeof(int))) == NULL) 28923693Speter err(1, NULL); 2901590Srgrimes 2911590Srgrimes /* Pull out all network requests. */ 2921590Srgrimes for (ap = p = argv, np = nargv; *p; ++p) 2931590Srgrimes if (index(*p, '@')) 2941590Srgrimes *np++ = *p; 2951590Srgrimes else 2961590Srgrimes *ap++ = *p; 2971590Srgrimes 2981590Srgrimes *np++ = NULL; 2991590Srgrimes *ap++ = NULL; 3001590Srgrimes 3011590Srgrimes if (!*argv) 3021590Srgrimes goto net; 3031590Srgrimes 3041590Srgrimes /* 30566563Sbrian * Mark any arguments beginning with '/' as invalid so that we 30666563Sbrian * don't accidently confuse them with expansions from finger.conf 30766563Sbrian */ 30866563Sbrian for (p = argv, ip = used; *p; ++p, ++ip) 30966563Sbrian if (**p == '/') { 31066563Sbrian *ip = 1; 31166563Sbrian warnx("%s: no such user", *p); 31266563Sbrian } 31366563Sbrian 31466563Sbrian /* 31564775Sbrian * Traverse the finger alias configuration file of the form 31664775Sbrian * alias:(user|alias), ignoring comment lines beginning '#'. 31764775Sbrian */ 31864775Sbrian if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) { 31964775Sbrian while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) { 32064775Sbrian conf_length = strlen(conf_alias); 32164775Sbrian if (*conf_alias == '#' || conf_alias[--conf_length] != '\n') 32264775Sbrian continue; 32364775Sbrian conf_alias[conf_length] = '\0'; /* Remove trailing LF */ 32464775Sbrian if ((conf_realname = strchr(conf_alias, ':')) == NULL) 32564775Sbrian continue; 32664775Sbrian *conf_realname = '\0'; /* Replace : with NUL */ 32764775Sbrian for (p = argv; *p; ++p) { 328126960Sbde if (strcmp(*p, conf_alias) == 0) { 32964775Sbrian if ((*p = strdup(conf_realname+1)) == NULL) { 33064775Sbrian err(1, NULL); 33164775Sbrian } 33264775Sbrian } 33364775Sbrian } 33464775Sbrian } 33564775Sbrian (void)fclose(conf_fp); 33664775Sbrian } 33764775Sbrian 33864775Sbrian /* 3391590Srgrimes * Traverse the list of possible login names and check the login name 34065064Sbrian * and real name against the name specified by the user. If the name 34165064Sbrian * begins with a '/', try to read the file of that name instead of 34265064Sbrian * gathering the traditional finger information. 3431590Srgrimes */ 3441590Srgrimes if (mflag) 34566675Sru for (p = argv, ip = used; *p; ++p, ++ip) { 34666675Sru if (**p != '/' || *ip == 1 || !show_text("", *p, "")) { 34765064Sbrian if (((pw = getpwnam(*p)) != NULL) && !hide(pw)) 34865064Sbrian enter_person(pw); 34966675Sru else if (!*ip) 35065064Sbrian warnx("%s: no such user", *p); 35165064Sbrian } 35265064Sbrian } 3531590Srgrimes else { 35465787Sbrian while ((pw = getpwent()) != NULL) { 3551590Srgrimes for (p = argv, ip = used; *p; ++p, ++ip) 35665064Sbrian if (**p == '/' && *ip != 1 35765787Sbrian && show_text("", *p, "")) 35865064Sbrian *ip = 1; 35965787Sbrian else if (match(pw, *p) && !hide(pw)) { 3601590Srgrimes enter_person(pw); 3611590Srgrimes *ip = 1; 3621590Srgrimes } 3635369Sjkh } 3641590Srgrimes for (p = argv, ip = used; *p; ++p, ++ip) 3651590Srgrimes if (!*ip) 36627169Scharnier warnx("%s: no such user", *p); 3671590Srgrimes } 3681590Srgrimes 3691590Srgrimes /* Handle network requests. */ 3704991Spstnet: for (p = nargv; *p;) { 3711590Srgrimes netfinger(*p++); 3724991Spst if (*p || entries) 3734991Spst printf("\n"); 3744991Spst } 3751590Srgrimes 3761590Srgrimes if (entries == 0) 3771590Srgrimes return; 3781590Srgrimes 379117010Sjmallett if (kflag) 380117010Sjmallett return; 381117010Sjmallett 3821590Srgrimes /* 3831590Srgrimes * Scan thru the list of users currently logged in, saving 3841590Srgrimes * appropriate data whenever a match occurs. 3851590Srgrimes */ 3861590Srgrimes if (!freopen(_PATH_UTMP, "r", stdin)) 38723693Speter err(1, "%s", _PATH_UTMP); 3881590Srgrimes while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 3891590Srgrimes if (!user.ut_name[0]) 3901590Srgrimes continue; 3911590Srgrimes if ((pn = find_person(user.ut_name)) == NULL) 3921590Srgrimes continue; 3931590Srgrimes enter_where(&user, pn); 3941590Srgrimes } 3951590Srgrimes if (db) 39687229Smarkm for (sflag1 = R_FIRST;; sflag1 = R_NEXT) { 39723693Speter PERSON *tmp; 39823693Speter 39987229Smarkm r = (*db->seq)(db, &key, &data, sflag1); 4001590Srgrimes if (r == -1) 40123693Speter err(1, "db seq"); 4021590Srgrimes if (r == 1) 4031590Srgrimes break; 40423693Speter memmove(&tmp, data.data, sizeof tmp); 40523693Speter enter_lastlog(tmp); 4061590Srgrimes } 4071590Srgrimes} 408