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