finger.c revision 65064
1184588Sdfr/* 2184588Sdfr * Copyright (c) 1989, 1993 3184588Sdfr * The Regents of the University of California. All rights reserved. 4184588Sdfr * 5184588Sdfr * This code is derived from software contributed to Berkeley by 6184588Sdfr * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 7184588Sdfr * 8184588Sdfr * Redistribution and use in source and binary forms, with or without 9184588Sdfr * modification, are permitted provided that the following conditions 10184588Sdfr * are met: 11184588Sdfr * 1. Redistributions of source code must retain the above copyright 12184588Sdfr * notice, this list of conditions and the following disclaimer. 13184588Sdfr * 2. Redistributions in binary form must reproduce the above copyright 14184588Sdfr * notice, this list of conditions and the following disclaimer in the 15184588Sdfr * documentation and/or other materials provided with the distribution. 16184588Sdfr * 3. All advertising materials mentioning features or use of this software 17184588Sdfr * must display the following acknowledgement: 18184588Sdfr * This product includes software developed by the University of 19184588Sdfr * California, Berkeley and its contributors. 20184588Sdfr * 4. Neither the name of the University nor the names of its contributors 21184588Sdfr * may be used to endorse or promote products derived from this software 22184588Sdfr * without specific prior written permission. 23184588Sdfr * 24184588Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25184588Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26184588Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27184588Sdfr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28184588Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29184588Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30184588Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31184588Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32184588Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33184588Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34184588Sdfr * SUCH DAMAGE. 35184588Sdfr */ 36184588Sdfr 37184588Sdfr/* 38184588Sdfr * Luke Mewburn <lm@rmit.edu.au> added the following on 940622: 39184588Sdfr * - mail status ("No Mail", "Mail read:...", or "New Mail ..., 40184588Sdfr * Unread since ...".) 41184588Sdfr * - 4 digit phone extensions (3210 is printed as x3210.) 42184588Sdfr * - host/office toggling in short format with -h & -o. 43190293Srwatson * - short day names (`Tue' printed instead of `Jun 21' if the 44184588Sdfr * login time is < 6 days. 45184588Sdfr */ 46184588Sdfr 47184588Sdfr#ifndef lint 48184588Sdfrstatic char copyright[] = 49184588Sdfr"@(#) Copyright (c) 1989, 1993\n\ 50184588Sdfr The Regents of the University of California. All rights reserved.\n"; 51184588Sdfr#endif /* not lint */ 52184588Sdfr 53184588Sdfr#ifndef lint 54184588Sdfr#if 0 55184588Sdfrstatic char sccsid[] = "@(#)finger.c 8.5 (Berkeley) 5/4/95"; 56184588Sdfr#else 57184588Sdfrstatic const char rcsid[] = 58184588Sdfr "$FreeBSD: head/usr.bin/finger/finger.c 65064 2000-08-25 01:01:07Z brian $"; 59184588Sdfr#endif 60184588Sdfr#endif /* not lint */ 61184588Sdfr 62184588Sdfr/* 63184588Sdfr * Finger prints out information about users. It is not portable since 64184588Sdfr * certain fields (e.g. the full user name, office, and phone numbers) are 65184588Sdfr * extracted from the gecos field of the passwd file which other UNIXes 66184588Sdfr * may not have or may use for other things. 67184588Sdfr * 68184588Sdfr * There are currently two output formats; the short format is one line 69184588Sdfr * per user and displays login name, tty, login time, real name, idle time, 70184588Sdfr * and either remote host information (default) or office location/phone 71190293Srwatson * number, depending on if -h or -o is used respectively. 72190293Srwatson * The long format gives the same information (in a more legible format) as 73190293Srwatson * well as home directory, shell, mail info, and .plan/.project files. 74190293Srwatson */ 75190293Srwatson 76190293Srwatson#include <sys/param.h> 77190293Srwatson 78190293Srwatson#include <db.h> 79190293Srwatson#include <err.h> 80190293Srwatson#include <errno.h> 81190293Srwatson#include <fcntl.h> 82190293Srwatson#include <pwd.h> 83190293Srwatson#include <stdio.h> 84190293Srwatson#include <stdlib.h> 85190293Srwatson#include <string.h> 86190293Srwatson#include <time.h> 87190293Srwatson#include <unistd.h> 88190293Srwatson#include <utmp.h> 89190293Srwatson#include <db.h> 90184588Sdfr#include <locale.h> 91184588Sdfr#include <sys/syslimits.h> 92184588Sdfr 93184588Sdfr#include "finger.h" 94184588Sdfr#include "pathnames.h" 95184588Sdfr 96221973SrmacklemDB *db; 97184588Sdfrtime_t now; 98221973Srmacklemint entries, lflag, mflag, pplan, sflag, oflag, Tflag; 99184588Sdfrchar tbuf[1024]; 100221973Srmacklem 101184588Sdfrstatic void loginlist __P((void)); 102221973Srmacklemstatic void usage __P((void)); 103203731Smariusstatic void userlist __P((int, char **)); 104184588Sdfr 105221973Srmacklemint 106203731Smariusoption(argc, argv) 107184588Sdfr int argc; 108184588Sdfr char **argv; 109184588Sdfr{ 110184588Sdfr int ch; 111184588Sdfr 112184588Sdfr optind = 1; /* reset getopt */ 113184588Sdfr 114184588Sdfr while ((ch = getopt(argc, argv, "lmpshoT")) != -1) 115184588Sdfr switch(ch) { 116184588Sdfr case 'l': 117184588Sdfr lflag = 1; /* long format */ 118184588Sdfr break; 119184588Sdfr case 'm': 120184588Sdfr mflag = 1; /* force exact match of names */ 121184588Sdfr break; 122184588Sdfr case 'p': 123184588Sdfr pplan = 1; /* don't show .plan/.project */ 124184588Sdfr break; 125184588Sdfr case 's': 126184588Sdfr sflag = 1; /* short format */ 127184588Sdfr break; 128184588Sdfr case 'h': 129184588Sdfr oflag = 0; /* remote host info */ 130184588Sdfr break; 131184588Sdfr case 'o': 132184588Sdfr oflag = 1; /* office info */ 133184588Sdfr break; 134184588Sdfr case 'T': 135184588Sdfr Tflag = 1; /* disable T/TCP */ 136184588Sdfr break; 137184588Sdfr case '?': 138184588Sdfr default: 139184588Sdfr usage(); 140184588Sdfr } 141184588Sdfr 142184588Sdfr return optind; 143184588Sdfr} 144184588Sdfr 145184588Sdfrstatic void 146184588Sdfrusage() 147184588Sdfr{ 148184588Sdfr (void)fprintf(stderr, "usage: finger [-lmpshoT] [login ...]\n"); 149184588Sdfr exit(1); 150184588Sdfr} 151184588Sdfr 152184588Sdfrint 153184588Sdfrmain(argc, argv) 154184588Sdfr int argc; 155184588Sdfr char **argv; 156184588Sdfr{ 157184588Sdfr int envargc, argcnt; 158184588Sdfr char *envargv[3]; 159203731Smarius struct passwd *pw; 160203731Smarius 161184588Sdfr if (getuid() == 0 || geteuid() == 0) { 162184588Sdfr if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) { 163184588Sdfr setgid(pw->pw_gid); 164184588Sdfr setuid(pw->pw_uid); 165184588Sdfr } else { 166184588Sdfr setgid(UNPRIV_UGID); 167184588Sdfr setuid(UNPRIV_UGID); 168184588Sdfr } 169184588Sdfr } 170184588Sdfr 171184588Sdfr (void) setlocale(LC_ALL, ""); 172184588Sdfr 173184588Sdfr /* remove this line to get remote host */ 174184588Sdfr oflag = 1; /* default to old "office" behavior */ 175184588Sdfr 176184588Sdfr /* 177184588Sdfr * Process environment variables followed by command line arguments. 178184588Sdfr */ 179184588Sdfr if ((envargv[1] = getenv("FINGER"))) { 180184588Sdfr envargc = 2; 181184588Sdfr envargv[0] = "finger"; 182184588Sdfr envargv[2] = NULL; 183195203Sdfr (void) option(envargc, envargv); 184184588Sdfr } 185184588Sdfr 186184588Sdfr argcnt = option(argc, argv); 187184588Sdfr argc -= argcnt; 188184588Sdfr argv += argcnt; 189184588Sdfr 190184588Sdfr (void)time(&now); 191184588Sdfr setpassent(1); 192184588Sdfr if (!*argv) { 193184588Sdfr /* 194228757Srmacklem * Assign explicit "small" format if no names given and -l 195184588Sdfr * not selected. Force the -s BEFORE we get names so proper 196184588Sdfr * screening will be done. 197184588Sdfr */ 198184588Sdfr if (!lflag) 199184588Sdfr sflag = 1; /* if -l not explicit, force -s */ 200184588Sdfr loginlist(); 201184588Sdfr if (entries == 0) 202184588Sdfr (void)printf("No one logged on.\n"); 203184588Sdfr } else { 204184588Sdfr userlist(argc, argv); 205184588Sdfr /* 206184588Sdfr * Assign explicit "large" format if names given and -s not 207184588Sdfr * explicitly stated. Force the -l AFTER we get names so any 208184588Sdfr * remote finger attempts specified won't be mishandled. 209184588Sdfr */ 210184588Sdfr if (!sflag) 211184588Sdfr lflag = 1; /* if -s not explicit, force -l */ 212184588Sdfr } 213184588Sdfr if (entries) { 214184588Sdfr if (lflag) 215184588Sdfr lflag_print(); 216184588Sdfr else 217184588Sdfr sflag_print(); 218184588Sdfr } 219184588Sdfr return (0); 220184588Sdfr} 221184588Sdfr 222184588Sdfrstatic void 223184588Sdfrloginlist() 224184588Sdfr{ 225184588Sdfr register PERSON *pn; 226203731Smarius DBT data, key; 227184588Sdfr struct passwd *pw; 228184588Sdfr struct utmp user; 229184588Sdfr int r, sflag; 230184588Sdfr char name[UT_NAMESIZE + 1]; 231184588Sdfr 232184588Sdfr if (!freopen(_PATH_UTMP, "r", stdin)) 233184588Sdfr err(1, "%s", _PATH_UTMP); 234184588Sdfr name[UT_NAMESIZE] = '\0'; 235184588Sdfr while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 236184588Sdfr if (!user.ut_name[0]) 237184588Sdfr continue; 238184588Sdfr if ((pn = find_person(user.ut_name)) == NULL) { 239184588Sdfr bcopy(user.ut_name, name, UT_NAMESIZE); 240184588Sdfr if ((pw = getpwnam(name)) == NULL) 241184588Sdfr continue; 242184588Sdfr if (hide(pw)) 243184588Sdfr continue; 244184588Sdfr pn = enter_person(pw); 245184588Sdfr } 246184588Sdfr enter_where(&user, pn); 247184588Sdfr } 248184588Sdfr if (db && lflag) 249184588Sdfr for (sflag = R_FIRST;; sflag = R_NEXT) { 250184588Sdfr PERSON *tmp; 251184588Sdfr 252184588Sdfr r = (*db->seq)(db, &key, &data, sflag); 253184588Sdfr if (r == -1) 254184588Sdfr err(1, "db seq"); 255184588Sdfr if (r == 1) 256184588Sdfr break; 257184588Sdfr memmove(&tmp, data.data, sizeof tmp); 258184588Sdfr enter_lastlog(tmp); 259184588Sdfr } 260184588Sdfr} 261184588Sdfr 262228757Srmacklemstatic void 263228757Srmacklemuserlist(argc, argv) 264228757Srmacklem register int argc; 265228757Srmacklem register char **argv; 266228757Srmacklem{ 267228757Srmacklem register PERSON *pn; 268228757Srmacklem DBT data, key; 269228757Srmacklem struct utmp user; 270228757Srmacklem struct passwd *pw; 271228757Srmacklem int r, sflag, *used, *ip; 272228757Srmacklem char **ap, **nargv, **np, **p; 273184588Sdfr FILE *conf_fp; 274184588Sdfr char conf_alias[LINE_MAX]; 275184588Sdfr char *conf_realname; 276228757Srmacklem int conf_length; 277228757Srmacklem int nip; 278228757Srmacklem 279228757Srmacklem if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL || 280228757Srmacklem (used = calloc(argc, sizeof(int))) == NULL) 281228757Srmacklem err(1, NULL); 282228757Srmacklem 283228757Srmacklem /* Pull out all network requests. */ 284228757Srmacklem for (ap = p = argv, np = nargv; *p; ++p) 285228757Srmacklem if (index(*p, '@')) 286228757Srmacklem *np++ = *p; 287228757Srmacklem else 288228757Srmacklem *ap++ = *p; 289228757Srmacklem 290228757Srmacklem *np++ = NULL; 291228757Srmacklem *ap++ = NULL; 292228757Srmacklem 293184588Sdfr if (!*argv) 294184588Sdfr goto net; 295184588Sdfr 296184588Sdfr /* 297184588Sdfr * Traverse the finger alias configuration file of the form 298184588Sdfr * alias:(user|alias), ignoring comment lines beginning '#'. 299203731Smarius */ 300184588Sdfr if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) { 301184588Sdfr while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) { 302184588Sdfr conf_length = strlen(conf_alias); 303184588Sdfr if (*conf_alias == '#' || conf_alias[--conf_length] != '\n') 304184588Sdfr continue; 305184588Sdfr conf_alias[conf_length] = '\0'; /* Remove trailing LF */ 306184588Sdfr if ((conf_realname = strchr(conf_alias, ':')) == NULL) 307184588Sdfr continue; 308184588Sdfr *conf_realname = '\0'; /* Replace : with NUL */ 309203731Smarius for (p = argv; *p; ++p) { 310184588Sdfr if (strcmp(*p, conf_alias) == NULL) { 311184588Sdfr if ((*p = strdup(conf_realname+1)) == NULL) { 312184588Sdfr err(1, NULL); 313184588Sdfr } 314184588Sdfr } 315184588Sdfr } 316203731Smarius } 317184588Sdfr (void)fclose(conf_fp); 318184588Sdfr } 319184588Sdfr 320184588Sdfr /* 321184588Sdfr * Traverse the list of possible login names and check the login name 322184588Sdfr * and real name against the name specified by the user. If the name 323203731Smarius * begins with a '/', try to read the file of that name instead of 324184588Sdfr * gathering the traditional finger information. 325184588Sdfr */ 326184588Sdfr if (mflag) 327184588Sdfr for (p = argv; *p; ++p) { 328184588Sdfr if (**p == '/' && !show_text("", *p, "")) { 329184588Sdfr if (((pw = getpwnam(*p)) != NULL) && !hide(pw)) 330184588Sdfr enter_person(pw); 331184588Sdfr else 332184588Sdfr warnx("%s: no such user", *p); 333184588Sdfr } 334184588Sdfr } 335223309Srmacklem else { 336184588Sdfr nip = 0; 337184588Sdfr while (nip < argc && (pw = getpwent()) != NULL) { 338203731Smarius for (p = argv, ip = used; *p; ++p, ++ip) 339184588Sdfr if (**p == '/' && *ip != 1 340184588Sdfr && show_text("", *p, "")) { 341184588Sdfr *ip = 1; 342184588Sdfr nip++; 343184588Sdfr } else if (match(pw, *p) && !hide(pw)) { 344184588Sdfr enter_person(pw); 345184588Sdfr *ip = 1; 346184588Sdfr nip++; 347184588Sdfr } 348184588Sdfr } 349184588Sdfr for (p = argv, ip = used; *p; ++p, ++ip) 350184588Sdfr if (!*ip) 351184588Sdfr warnx("%s: no such user", *p); 352184588Sdfr } 353184588Sdfr 354184588Sdfr /* Handle network requests. */ 355184588Sdfrnet: for (p = nargv; *p;) { 356184588Sdfr netfinger(*p++); 357184588Sdfr if (*p || entries) 358184588Sdfr printf("\n"); 359203731Smarius } 360223309Srmacklem 361203731Smarius if (entries == 0) 362184588Sdfr return; 363184588Sdfr 364184588Sdfr /* 365184588Sdfr * Scan thru the list of users currently logged in, saving 366184588Sdfr * appropriate data whenever a match occurs. 367184588Sdfr */ 368184588Sdfr if (!freopen(_PATH_UTMP, "r", stdin)) 369223309Srmacklem err(1, "%s", _PATH_UTMP); 370184588Sdfr while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 371184588Sdfr if (!user.ut_name[0]) 372184588Sdfr continue; 373184588Sdfr if ((pn = find_person(user.ut_name)) == NULL) 374184588Sdfr continue; 375184588Sdfr enter_where(&user, pn); 376184588Sdfr } 377184588Sdfr if (db) 378184588Sdfr for (sflag = R_FIRST;; sflag = R_NEXT) { 379184588Sdfr PERSON *tmp; 380184588Sdfr 381184588Sdfr r = (*db->seq)(db, &key, &data, sflag); 382184588Sdfr if (r == -1) 383184588Sdfr err(1, "db seq"); 384184588Sdfr if (r == 1) 385184588Sdfr break; 386184588Sdfr memmove(&tmp, data.data, sizeof tmp); 387184588Sdfr enter_lastlog(tmp); 388184588Sdfr } 389184588Sdfr} 390184588Sdfr