finger.c revision 102944
10SN/A/* 211926Sserb * Copyright (c) 1989, 1993 30SN/A * The Regents of the University of California. All rights reserved. 40SN/A * 50SN/A * This code is derived from software contributed to Berkeley by 60SN/A * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 72362SN/A * 80SN/A * Redistribution and use in source and binary forms, with or without 92362SN/A * modification, are permitted provided that the following conditions 100SN/A * are met: 110SN/A * 1. Redistributions of source code must retain the above copyright 120SN/A * notice, this list of conditions and the following disclaimer. 130SN/A * 2. Redistributions in binary form must reproduce the above copyright 140SN/A * notice, this list of conditions and the following disclaimer in the 150SN/A * documentation and/or other materials provided with the distribution. 160SN/A * 3. All advertising materials mentioning features or use of this software 170SN/A * must display the following acknowledgement: 180SN/A * This product includes software developed by the University of 190SN/A * California, Berkeley and its contributors. 200SN/A * 4. Neither the name of the University nor the names of its contributors 212362SN/A * may be used to endorse or promote products derived from this software 222362SN/A * without specific prior written permission. 232362SN/A * 240SN/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 250SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 260SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 270SN/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 280SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 290SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 300SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 319416SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3211926Sserb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 330SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 340SN/A * SUCH DAMAGE. 350SN/A */ 3613629Savstepan 370SN/A/* 380SN/A * Luke Mewburn <lm@rmit.edu.au> added the following on 940622: 390SN/A * - mail status ("No Mail", "Mail read:...", or "New Mail ..., 400SN/A * Unread since ...".) 410SN/A * - 4 digit phone extensions (3210 is printed as x3210.) 420SN/A * - host/office toggling in short format with -h & -o. 439416SN/A * - short day names (`Tue' printed instead of `Jun 21' if the 440SN/A * login time is < 6 days. 450SN/A */ 4613629Savstepan 470SN/A#ifndef lint 480SN/Astatic const char copyright[] = 490SN/A"@(#) Copyright (c) 1989, 1993\n\ 509416SN/A The Regents of the University of California. All rights reserved.\n"; 519416SN/A#endif /* not lint */ 520SN/A 530SN/A#if 0 540SN/A#ifndef lint 550SN/Astatic char sccsid[] = "@(#)finger.c 8.5 (Berkeley) 5/4/95"; 560SN/A#endif 5713629Savstepan#endif 580SN/A 590SN/A#include <sys/cdefs.h> 600SN/A__FBSDID("$FreeBSD: head/usr.bin/finger/finger.c 102944 2002-09-04 23:29:10Z dwmalone $"); 6111926Sserb 6211926Sserb/* 630SN/A * Finger prints out information about users. It is not portable since 644398SN/A * certain fields (e.g. the full user name, office, and phone numbers) are 650SN/A * extracted from the gecos field of the passwd file which other UNIXes 660SN/A * may not have or may use for other things. 670SN/A * 680SN/A * There are currently two output formats; the short format is one line 690SN/A * per user and displays login name, tty, login time, real name, idle time, 70 * and either remote host information (default) or office location/phone 71 * number, depending on if -h or -o is used respectively. 72 * The long format gives the same information (in a more legible format) as 73 * well as home directory, shell, mail info, and .plan/.project files. 74 */ 75 76#include <sys/types.h> 77#include <sys/socket.h> 78#include <db.h> 79#include <err.h> 80#include <pwd.h> 81#include <stdio.h> 82#include <stdlib.h> 83#include <string.h> 84#include <time.h> 85#include <unistd.h> 86#include <utmp.h> 87#include <locale.h> 88 89#include "finger.h" 90#include "pathnames.h" 91 92DB *db; 93time_t now; 94int entries, gflag, lflag, mflag, pplan, sflag, oflag, Tflag; 95sa_family_t family = PF_UNSPEC; 96int d_first = -1; 97char tbuf[1024]; 98 99static void loginlist(void); 100static int option(int, char **); 101static void usage(void); 102static void userlist(int, char **); 103 104static int 105option(int argc, char **argv) 106{ 107 int ch; 108 109 optind = 1; /* reset getopt */ 110 111 while ((ch = getopt(argc, argv, "46glmpshoT")) != -1) 112 switch(ch) { 113 case '4': 114 family = AF_INET; 115 break; 116 case '6': 117 family = AF_INET6; 118 break; 119 case 'g': 120 gflag = 1; 121 break; 122 case 'l': 123 lflag = 1; /* long format */ 124 break; 125 case 'm': 126 mflag = 1; /* force exact match of names */ 127 break; 128 case 'p': 129 pplan = 1; /* don't show .plan/.project */ 130 break; 131 case 's': 132 sflag = 1; /* short format */ 133 break; 134 case 'h': 135 oflag = 0; /* remote host info */ 136 break; 137 case 'o': 138 oflag = 1; /* office info */ 139 break; 140 case 'T': 141 Tflag = 1; /* disable T/TCP */ 142 break; 143 case '?': 144 default: 145 usage(); 146 } 147 148 return optind; 149} 150 151static void 152usage(void) 153{ 154 (void)fprintf(stderr, "usage: finger [-46lmpshoT] [login ...]\n"); 155 exit(1); 156} 157 158int 159main(int argc, char **argv) 160{ 161 int envargc, argcnt; 162 char *envargv[3]; 163 struct passwd *pw; 164 static char myname[] = "finger"; 165 166 if (getuid() == 0 || geteuid() == 0) { 167 if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) { 168 setgid(pw->pw_gid); 169 setuid(pw->pw_uid); 170 } else { 171 setgid(UNPRIV_UGID); 172 setuid(UNPRIV_UGID); 173 } 174 } 175 176 (void) setlocale(LC_ALL, ""); 177 178 /* remove this line to get remote host */ 179 oflag = 1; /* default to old "office" behavior */ 180 181 /* 182 * Process environment variables followed by command line arguments. 183 */ 184 if ((envargv[1] = getenv("FINGER"))) { 185 envargc = 2; 186 envargv[0] = myname; 187 envargv[2] = NULL; 188 (void) option(envargc, envargv); 189 } 190 191 argcnt = option(argc, argv); 192 argc -= argcnt; 193 argv += argcnt; 194 195 (void)time(&now); 196 setpassent(1); 197 if (!*argv) { 198 /* 199 * Assign explicit "small" format if no names given and -l 200 * not selected. Force the -s BEFORE we get names so proper 201 * screening will be done. 202 */ 203 if (!lflag) 204 sflag = 1; /* if -l not explicit, force -s */ 205 loginlist(); 206 if (entries == 0) 207 (void)printf("No one logged on.\n"); 208 } else { 209 userlist(argc, argv); 210 /* 211 * Assign explicit "large" format if names given and -s not 212 * explicitly stated. Force the -l AFTER we get names so any 213 * remote finger attempts specified won't be mishandled. 214 */ 215 if (!sflag) 216 lflag = 1; /* if -s not explicit, force -l */ 217 } 218 if (entries) { 219 if (lflag) 220 lflag_print(); 221 else 222 sflag_print(); 223 } 224 return (0); 225} 226 227static void 228loginlist(void) 229{ 230 PERSON *pn; 231 DBT data, key; 232 struct passwd *pw; 233 struct utmp user; 234 int r, sflag1; 235 char name[UT_NAMESIZE + 1]; 236 237 if (!freopen(_PATH_UTMP, "r", stdin)) 238 err(1, "%s", _PATH_UTMP); 239 name[UT_NAMESIZE] = '\0'; 240 while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 241 if (!user.ut_name[0]) 242 continue; 243 if ((pn = find_person(user.ut_name)) == NULL) { 244 bcopy(user.ut_name, name, UT_NAMESIZE); 245 if ((pw = getpwnam(name)) == NULL) 246 continue; 247 if (hide(pw)) 248 continue; 249 pn = enter_person(pw); 250 } 251 enter_where(&user, pn); 252 } 253 if (db && lflag) 254 for (sflag1 = R_FIRST;; sflag1 = R_NEXT) { 255 PERSON *tmp; 256 257 r = (*db->seq)(db, &key, &data, sflag1); 258 if (r == -1) 259 err(1, "db seq"); 260 if (r == 1) 261 break; 262 memmove(&tmp, data.data, sizeof tmp); 263 enter_lastlog(tmp); 264 } 265} 266 267static void 268userlist(int argc, char **argv) 269{ 270 PERSON *pn; 271 DBT data, key; 272 struct utmp user; 273 struct passwd *pw; 274 int r, sflag1, *used, *ip; 275 char **ap, **nargv, **np, **p; 276 FILE *conf_fp; 277 char conf_alias[LINE_MAX]; 278 char *conf_realname; 279 int conf_length; 280 281 if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL || 282 (used = calloc(argc, sizeof(int))) == NULL) 283 err(1, NULL); 284 285 /* Pull out all network requests. */ 286 for (ap = p = argv, np = nargv; *p; ++p) 287 if (index(*p, '@')) 288 *np++ = *p; 289 else 290 *ap++ = *p; 291 292 *np++ = NULL; 293 *ap++ = NULL; 294 295 if (!*argv) 296 goto net; 297 298 /* 299 * Mark any arguments beginning with '/' as invalid so that we 300 * don't accidently confuse them with expansions from finger.conf 301 */ 302 for (p = argv, ip = used; *p; ++p, ++ip) 303 if (**p == '/') { 304 *ip = 1; 305 warnx("%s: no such user", *p); 306 } 307 308 /* 309 * Traverse the finger alias configuration file of the form 310 * alias:(user|alias), ignoring comment lines beginning '#'. 311 */ 312 if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) { 313 while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) { 314 conf_length = strlen(conf_alias); 315 if (*conf_alias == '#' || conf_alias[--conf_length] != '\n') 316 continue; 317 conf_alias[conf_length] = '\0'; /* Remove trailing LF */ 318 if ((conf_realname = strchr(conf_alias, ':')) == NULL) 319 continue; 320 *conf_realname = '\0'; /* Replace : with NUL */ 321 for (p = argv; *p; ++p) { 322 if (strcmp(*p, conf_alias) == NULL) { 323 if ((*p = strdup(conf_realname+1)) == NULL) { 324 err(1, NULL); 325 } 326 } 327 } 328 } 329 (void)fclose(conf_fp); 330 } 331 332 /* 333 * Traverse the list of possible login names and check the login name 334 * and real name against the name specified by the user. If the name 335 * begins with a '/', try to read the file of that name instead of 336 * gathering the traditional finger information. 337 */ 338 if (mflag) 339 for (p = argv, ip = used; *p; ++p, ++ip) { 340 if (**p != '/' || *ip == 1 || !show_text("", *p, "")) { 341 if (((pw = getpwnam(*p)) != NULL) && !hide(pw)) 342 enter_person(pw); 343 else if (!*ip) 344 warnx("%s: no such user", *p); 345 } 346 } 347 else { 348 while ((pw = getpwent()) != NULL) { 349 for (p = argv, ip = used; *p; ++p, ++ip) 350 if (**p == '/' && *ip != 1 351 && show_text("", *p, "")) 352 *ip = 1; 353 else if (match(pw, *p) && !hide(pw)) { 354 enter_person(pw); 355 *ip = 1; 356 } 357 } 358 for (p = argv, ip = used; *p; ++p, ++ip) 359 if (!*ip) 360 warnx("%s: no such user", *p); 361 } 362 363 /* Handle network requests. */ 364net: for (p = nargv; *p;) { 365 netfinger(*p++); 366 if (*p || entries) 367 printf("\n"); 368 } 369 370 if (entries == 0) 371 return; 372 373 /* 374 * Scan thru the list of users currently logged in, saving 375 * appropriate data whenever a match occurs. 376 */ 377 if (!freopen(_PATH_UTMP, "r", stdin)) 378 err(1, "%s", _PATH_UTMP); 379 while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 380 if (!user.ut_name[0]) 381 continue; 382 if ((pn = find_person(user.ut_name)) == NULL) 383 continue; 384 enter_where(&user, pn); 385 } 386 if (db) 387 for (sflag1 = R_FIRST;; sflag1 = R_NEXT) { 388 PERSON *tmp; 389 390 r = (*db->seq)(db, &key, &data, sflag1); 391 if (r == -1) 392 err(1, "db seq"); 393 if (r == 1) 394 break; 395 memmove(&tmp, data.data, sizeof tmp); 396 enter_lastlog(tmp); 397 } 398} 399