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