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 * 4. Neither the name of the University nor the names of its contributors 171590Srgrimes * may be used to endorse or promote products derived from this software 181590Srgrimes * without specific prior written permission. 191590Srgrimes * 201590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301590Srgrimes * SUCH DAMAGE. 311590Srgrimes */ 321590Srgrimes 332537Spst/* 342537Spst * Luke Mewburn <lm@rmit.edu.au> added the following on 940622: 352537Spst * - mail status ("No Mail", "Mail read:...", or "New Mail ..., 362537Spst * Unread since ...".) 372537Spst * - 4 digit phone extensions (3210 is printed as x3210.) 382537Spst * - host/office toggling in short format with -h & -o. 392537Spst * - short day names (`Tue' printed instead of `Jun 21' if the 402537Spst * login time is < 6 days. 412537Spst */ 422537Spst 431590Srgrimes#ifndef lint 4467467Srustatic const char copyright[] = 451590Srgrimes"@(#) Copyright (c) 1989, 1993\n\ 461590Srgrimes The Regents of the University of California. All rights reserved.\n"; 471590Srgrimes#endif /* not lint */ 481590Srgrimes 4987628Sdwmalone#if 0 501590Srgrimes#ifndef lint 5187628Sdwmalonestatic char sccsid[] = "@(#)finger.c 8.5 (Berkeley) 5/4/95"; 5272109Scharnier#endif 5387628Sdwmalone#endif 541590Srgrimes 5587628Sdwmalone#include <sys/cdefs.h> 5687628Sdwmalone__FBSDID("$FreeBSD$"); 5787628Sdwmalone 581590Srgrimes/* 591590Srgrimes * Finger prints out information about users. It is not portable since 601590Srgrimes * certain fields (e.g. the full user name, office, and phone numbers) are 611590Srgrimes * extracted from the gecos field of the passwd file which other UNIXes 621590Srgrimes * may not have or may use for other things. 631590Srgrimes * 641590Srgrimes * There are currently two output formats; the short format is one line 651590Srgrimes * per user and displays login name, tty, login time, real name, idle time, 662537Spst * and either remote host information (default) or office location/phone 672537Spst * number, depending on if -h or -o is used respectively. 682537Spst * The long format gives the same information (in a more legible format) as 692537Spst * well as home directory, shell, mail info, and .plan/.project files. 701590Srgrimes */ 711590Srgrimes 72100521Sume#include <sys/types.h> 73100521Sume#include <sys/socket.h> 7423693Speter#include <db.h> 7523693Speter#include <err.h> 761590Srgrimes#include <pwd.h> 771590Srgrimes#include <stdio.h> 781590Srgrimes#include <stdlib.h> 791590Srgrimes#include <string.h> 8023693Speter#include <time.h> 8123693Speter#include <unistd.h> 82202191Sed#include <utmpx.h> 8311759Sache#include <locale.h> 8423693Speter 851590Srgrimes#include "finger.h" 8664775Sbrian#include "pathnames.h" 871590Srgrimes 881590SrgrimesDB *db; 891590Srgrimestime_t now; 90168632Sdesint entries, gflag, kflag, lflag, mflag, pplan, sflag, oflag; 91106250Ssobomaxsa_family_t family = PF_UNSPEC; 9274586Sacheint d_first = -1; 931590Srgrimeschar tbuf[1024]; 94150316Sddsint invoker_root = 0; 951590Srgrimes 9692920Simpstatic void loginlist(void); 9792920Simpstatic int option(int, char **); 9892920Simpstatic void usage(void); 9992920Simpstatic void userlist(int, char **); 1001590Srgrimes 10187229Smarkmstatic int 102102944Sdwmaloneoption(int argc, char **argv) 1031590Srgrimes{ 1041590Srgrimes int ch; 1051590Srgrimes 1062589Spst optind = 1; /* reset getopt */ 1072537Spst 108168635Sdes while ((ch = getopt(argc, argv, "46gklmpsho")) != -1) 1091590Srgrimes switch(ch) { 110100521Sume case '4': 111100521Sume family = AF_INET; 112100521Sume break; 113100521Sume case '6': 114100521Sume family = AF_INET6; 115100521Sume break; 11699249Smini case 'g': 11799249Smini gflag = 1; 11899249Smini break; 119117010Sjmallett case 'k': 120117010Sjmallett kflag = 1; /* keep going without utmp */ 121117010Sjmallett break; 1221590Srgrimes case 'l': 1231590Srgrimes lflag = 1; /* long format */ 1241590Srgrimes break; 1251590Srgrimes case 'm': 1261590Srgrimes mflag = 1; /* force exact match of names */ 1271590Srgrimes break; 1281590Srgrimes case 'p': 1291590Srgrimes pplan = 1; /* don't show .plan/.project */ 1301590Srgrimes break; 1311590Srgrimes case 's': 1321590Srgrimes sflag = 1; /* short format */ 1331590Srgrimes break; 1342537Spst case 'h': 1352537Spst oflag = 0; /* remote host info */ 1362537Spst break; 1372537Spst case 'o': 1382537Spst oflag = 1; /* office info */ 1392537Spst break; 1401590Srgrimes case '?': 1411590Srgrimes default: 14227169Scharnier usage(); 1431590Srgrimes } 1441590Srgrimes 1452589Spst return optind; 1462589Spst} 1472589Spst 14827169Scharnierstatic void 149102944Sdwmaloneusage(void) 15027169Scharnier{ 151146466Sru (void)fprintf(stderr, 152168635Sdes "usage: finger [-46gklmpsho] [user ...] [user@host ...]\n"); 15327169Scharnier exit(1); 15427169Scharnier} 15527169Scharnier 15627169Scharnierint 157102944Sdwmalonemain(int argc, char **argv) 1582589Spst{ 15927169Scharnier int envargc, argcnt; 1602589Spst char *envargv[3]; 16146662Sobrien struct passwd *pw; 16287229Smarkm static char myname[] = "finger"; 1632589Spst 16446662Sobrien if (getuid() == 0 || geteuid() == 0) { 165150316Sdds invoker_root = 1; 16646662Sobrien if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) { 167220971Ssimon if (setgid(pw->pw_gid) != 0) 168220971Ssimon err(1, "setgid()"); 169220971Ssimon if (setuid(pw->pw_uid) != 0) 170220971Ssimon err(1, "setuid()"); 17146662Sobrien } else { 172220971Ssimon if (setgid(UNPRIV_UGID) != 0) 173220971Ssimon err(1, "setgid()"); 174220971Ssimon if (setuid(UNPRIV_UGID) != 0) 175220971Ssimon err(1, "setuid()"); 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; 236201140Sed struct utmpx *user; 23787229Smarkm int r, sflag1; 2381590Srgrimes 239117010Sjmallett if (kflag) 240117010Sjmallett errx(1, "can't list logins without reading utmp"); 241117010Sjmallett 242201140Sed setutxent(); 243201140Sed while ((user = getutxent()) != NULL) { 244201140Sed if (user->ut_type != USER_PROCESS) 2451590Srgrimes continue; 246201140Sed if ((pn = find_person(user->ut_user)) == NULL) { 247201140Sed if ((pw = getpwnam(user->ut_user)) == NULL) 2481590Srgrimes continue; 2495369Sjkh if (hide(pw)) 2505369Sjkh continue; 2511590Srgrimes pn = enter_person(pw); 2521590Srgrimes } 253201140Sed enter_where(user, pn); 2541590Srgrimes } 255201140Sed endutxent(); 2561590Srgrimes if (db && lflag) 25787229Smarkm for (sflag1 = R_FIRST;; sflag1 = R_NEXT) { 25823693Speter PERSON *tmp; 25923693Speter 26087229Smarkm r = (*db->seq)(db, &key, &data, sflag1); 2611590Srgrimes if (r == -1) 26223693Speter err(1, "db seq"); 2631590Srgrimes if (r == 1) 2641590Srgrimes break; 26523693Speter memmove(&tmp, data.data, sizeof tmp); 26623693Speter enter_lastlog(tmp); 2671590Srgrimes } 2681590Srgrimes} 2691590Srgrimes 2701590Srgrimesstatic void 271102944Sdwmaloneuserlist(int argc, char **argv) 2721590Srgrimes{ 27387229Smarkm PERSON *pn; 2741590Srgrimes DBT data, key; 275201140Sed struct utmpx *user; 2761590Srgrimes struct passwd *pw; 27787229Smarkm int r, sflag1, *used, *ip; 2781590Srgrimes char **ap, **nargv, **np, **p; 27964775Sbrian FILE *conf_fp; 28064775Sbrian char conf_alias[LINE_MAX]; 28164775Sbrian char *conf_realname; 28264775Sbrian int conf_length; 2831590Srgrimes 2841590Srgrimes if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL || 2851590Srgrimes (used = calloc(argc, sizeof(int))) == NULL) 28623693Speter err(1, NULL); 2871590Srgrimes 2881590Srgrimes /* Pull out all network requests. */ 2891590Srgrimes for (ap = p = argv, np = nargv; *p; ++p) 2901590Srgrimes if (index(*p, '@')) 2911590Srgrimes *np++ = *p; 2921590Srgrimes else 2931590Srgrimes *ap++ = *p; 2941590Srgrimes 2951590Srgrimes *np++ = NULL; 2961590Srgrimes *ap++ = NULL; 2971590Srgrimes 2981590Srgrimes if (!*argv) 2991590Srgrimes goto net; 3001590Srgrimes 3011590Srgrimes /* 30266563Sbrian * Mark any arguments beginning with '/' as invalid so that we 30366563Sbrian * don't accidently confuse them with expansions from finger.conf 30466563Sbrian */ 30566563Sbrian for (p = argv, ip = used; *p; ++p, ++ip) 30666563Sbrian if (**p == '/') { 30766563Sbrian *ip = 1; 30866563Sbrian warnx("%s: no such user", *p); 30966563Sbrian } 31066563Sbrian 31166563Sbrian /* 31264775Sbrian * Traverse the finger alias configuration file of the form 31364775Sbrian * alias:(user|alias), ignoring comment lines beginning '#'. 31464775Sbrian */ 31564775Sbrian if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) { 31664775Sbrian while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) { 31764775Sbrian conf_length = strlen(conf_alias); 31864775Sbrian if (*conf_alias == '#' || conf_alias[--conf_length] != '\n') 31964775Sbrian continue; 32064775Sbrian conf_alias[conf_length] = '\0'; /* Remove trailing LF */ 32164775Sbrian if ((conf_realname = strchr(conf_alias, ':')) == NULL) 32264775Sbrian continue; 32364775Sbrian *conf_realname = '\0'; /* Replace : with NUL */ 32464775Sbrian for (p = argv; *p; ++p) { 325126960Sbde if (strcmp(*p, conf_alias) == 0) { 32664775Sbrian if ((*p = strdup(conf_realname+1)) == NULL) { 32764775Sbrian err(1, NULL); 32864775Sbrian } 32964775Sbrian } 33064775Sbrian } 33164775Sbrian } 33264775Sbrian (void)fclose(conf_fp); 33364775Sbrian } 33464775Sbrian 33564775Sbrian /* 3361590Srgrimes * Traverse the list of possible login names and check the login name 33765064Sbrian * and real name against the name specified by the user. If the name 33865064Sbrian * begins with a '/', try to read the file of that name instead of 33965064Sbrian * gathering the traditional finger information. 3401590Srgrimes */ 3411590Srgrimes if (mflag) 34266675Sru for (p = argv, ip = used; *p; ++p, ++ip) { 34366675Sru if (**p != '/' || *ip == 1 || !show_text("", *p, "")) { 34465064Sbrian if (((pw = getpwnam(*p)) != NULL) && !hide(pw)) 34565064Sbrian enter_person(pw); 34666675Sru else if (!*ip) 34765064Sbrian warnx("%s: no such user", *p); 34865064Sbrian } 34965064Sbrian } 3501590Srgrimes else { 35165787Sbrian while ((pw = getpwent()) != NULL) { 3521590Srgrimes for (p = argv, ip = used; *p; ++p, ++ip) 35365064Sbrian if (**p == '/' && *ip != 1 35465787Sbrian && show_text("", *p, "")) 35565064Sbrian *ip = 1; 35665787Sbrian else if (match(pw, *p) && !hide(pw)) { 3571590Srgrimes enter_person(pw); 3581590Srgrimes *ip = 1; 3591590Srgrimes } 3605369Sjkh } 3611590Srgrimes for (p = argv, ip = used; *p; ++p, ++ip) 3621590Srgrimes if (!*ip) 36327169Scharnier warnx("%s: no such user", *p); 3641590Srgrimes } 3651590Srgrimes 3661590Srgrimes /* Handle network requests. */ 3674991Spstnet: for (p = nargv; *p;) { 3681590Srgrimes netfinger(*p++); 3694991Spst if (*p || entries) 3704991Spst printf("\n"); 3714991Spst } 3721590Srgrimes 373200793Sdelphij free(used); 3741590Srgrimes if (entries == 0) 3751590Srgrimes return; 3761590Srgrimes 377117010Sjmallett if (kflag) 378117010Sjmallett return; 379117010Sjmallett 3801590Srgrimes /* 3811590Srgrimes * Scan thru the list of users currently logged in, saving 3821590Srgrimes * appropriate data whenever a match occurs. 3831590Srgrimes */ 384201140Sed setutxent(); 385201140Sed while ((user = getutxent()) != NULL) { 386201140Sed if (user->ut_type != USER_PROCESS) 3871590Srgrimes continue; 388201140Sed if ((pn = find_person(user->ut_user)) == NULL) 3891590Srgrimes continue; 390201140Sed enter_where(user, pn); 3911590Srgrimes } 392201140Sed endutxent(); 3931590Srgrimes if (db) 39487229Smarkm for (sflag1 = R_FIRST;; sflag1 = R_NEXT) { 39523693Speter PERSON *tmp; 39623693Speter 39787229Smarkm r = (*db->seq)(db, &key, &data, sflag1); 3981590Srgrimes if (r == -1) 39923693Speter err(1, "db seq"); 4001590Srgrimes if (r == 1) 4011590Srgrimes break; 40223693Speter memmove(&tmp, data.data, sizeof tmp); 40323693Speter enter_lastlog(tmp); 4041590Srgrimes } 4051590Srgrimes} 406