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 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 #ifndef lint 54 static char sccsid[] = "@(#)finger.c 8.5 (Berkeley) 5/4/95"; 55 #endif /* not lint */ 56 57 /* 58 * Finger prints out information about users. It is not portable since 59 * certain fields (e.g. the full user name, office, and phone numbers) are 60 * extracted from the gecos field of the passwd file which other UNIXes 61 * may not have or may use for other things. 62 * 63 * There are currently two output formats; the short format is one line 64 * per user and displays login name, tty, login time, real name, idle time, 65 * and either remote host information (default) or office location/phone 66 * number, depending on if -h or -o is used respectively. 67 * The long format gives the same information (in a more legible format) as 68 * well as home directory, shell, mail info, and .plan/.project files. 69 */ 70 71 #include <sys/param.h> 72 73 #include <db.h> 74 #include <err.h> 75 #include <errno.h> 76 #include <fcntl.h> 77 #include <pwd.h> 78 #include <stdio.h> 79 #include <stdlib.h> 80 #include <string.h> 81 #include <time.h> 82 #include <unistd.h> 83 #include <utmp.h> 84 #include <db.h> 85 #include <locale.h> 86 87 #include "finger.h" 88 89 DB *db; 90 time_t now; 91 int entries, lflag, mflag, pplan, sflag, oflag, Tflag; 92 char tbuf[1024]; 93 94 static void loginlist __P((void)); 95 static void userlist __P((int, char **)); 96 97 int 98 option(argc, argv) 99 int argc; 100 char **argv; 101 { 102 int ch; 103 104 optind = 1; /* reset getopt */ 105 106 while ((ch = getopt(argc, argv, "lmpshoT")) != EOF) 107 switch(ch) { 108 case 'l': 109 lflag = 1; /* long format */ 110 break; 111 case 'm': 112 mflag = 1; /* force exact match of names */ 113 break; 114 case 'p': 115 pplan = 1; /* don't show .plan/.project */ 116 break; 117 case 's': 118 sflag = 1; /* short format */ 119 break; 120 case 'h': 121 oflag = 0; /* remote host info */ 122 break; 123 case 'o': 124 oflag = 1; /* office info */ 125 break; 126 case 'T': 127 Tflag = 1; /* disable T/TCP */ 128 break; 129 case '?': 130 default: 131 (void)fprintf(stderr, 132 "usage: finger [-lmpshoT] [login ...]\n"); 133 exit(1); 134 } 135 136 return optind; 137 } 138 139 main(argc, argv) 140 int argc; 141 char **argv; 142 { 143 int ch, envargc, argcnt; 144 char *envargv[3]; 145 146 (void) setlocale(LC_ALL, ""); 147 148 /* remove this line to get remote host */ 149 oflag = 1; /* default to old "office" behavior */ 150 151 /* 152 * Process environment variables followed by command line arguments. 153 */ 154 if ((envargv[1] = getenv("FINGER"))) { 155 envargc = 2; 156 envargv[0] = "finger"; 157 envargv[2] = NULL; 158 (void) option(envargc, envargv); 159 } 160 161 argcnt = option(argc, argv); 162 argc -= argcnt; 163 argv += argcnt; 164 165 (void)time(&now); 166 setpassent(1); 167 if (!*argv) { 168 /* 169 * Assign explicit "small" format if no names given and -l 170 * not selected. Force the -s BEFORE we get names so proper 171 * screening will be done. 172 */ 173 if (!lflag) 174 sflag = 1; /* if -l not explicit, force -s */ 175 loginlist(); 176 if (entries == 0) 177 (void)printf("No one logged on.\n"); 178 } else { 179 userlist(argc, argv); 180 /* 181 * Assign explicit "large" format if names given and -s not 182 * explicitly stated. Force the -l AFTER we get names so any 183 * remote finger attempts specified won't be mishandled. 184 */ 185 if (!sflag) 186 lflag = 1; /* if -s not explicit, force -l */ 187 } 188 if (entries) 189 if (lflag) 190 lflag_print(); 191 else 192 sflag_print(); 193 return (0); 194 } 195 196 static void 197 loginlist() 198 { 199 register PERSON *pn; 200 DBT data, key; 201 struct passwd *pw; 202 struct utmp user; 203 int r, sflag; 204 char name[UT_NAMESIZE + 1]; 205 206 if (!freopen(_PATH_UTMP, "r", stdin)) 207 err(1, "%s", _PATH_UTMP); 208 name[UT_NAMESIZE] = NULL; 209 while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 210 if (!user.ut_name[0]) 211 continue; 212 if ((pn = find_person(user.ut_name)) == NULL) { 213 bcopy(user.ut_name, name, UT_NAMESIZE); 214 if ((pw = getpwnam(name)) == NULL) 215 continue; 216 if (hide(pw)) 217 continue; 218 pn = enter_person(pw); 219 } 220 enter_where(&user, pn); 221 } 222 if (db && lflag) 223 for (sflag = R_FIRST;; sflag = R_NEXT) { 224 PERSON *tmp; 225 226 r = (*db->seq)(db, &key, &data, sflag); 227 if (r == -1) 228 err(1, "db seq"); 229 if (r == 1) 230 break; 231 memmove(&tmp, data.data, sizeof tmp); 232 enter_lastlog(tmp); 233 } 234 } 235 236 static void 237 userlist(argc, argv) 238 register int argc; 239 register char **argv; 240 { 241 register PERSON *pn; 242 DBT data, key; 243 struct utmp user; 244 struct passwd *pw; 245 int r, sflag, *used, *ip; 246 char **ap, **nargv, **np, **p; 247 248 if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL || 249 (used = calloc(argc, sizeof(int))) == NULL) 250 err(1, NULL); 251 252 /* Pull out all network requests. */ 253 for (ap = p = argv, np = nargv; *p; ++p) 254 if (index(*p, '@')) 255 *np++ = *p; 256 else 257 *ap++ = *p; 258 259 *np++ = NULL; 260 *ap++ = NULL; 261 262 if (!*argv) 263 goto net; 264 265 /* 266 * Traverse the list of possible login names and check the login name 267 * and real name against the name specified by the user. 268 */ 269 if (mflag) 270 for (p = argv; *p; ++p) 271 if (((pw = getpwnam(*p)) != NULL) && !hide(pw)) 272 enter_person(pw); 273 else 274 (void)fprintf(stderr, 275 "finger: %s: no such user\n", *p); 276 else { 277 while ((pw = getpwent()) != NULL) { 278 for (p = argv, ip = used; *p; ++p, ++ip) 279 if (match(pw, *p) && !hide(pw)) { 280 enter_person(pw); 281 *ip = 1; 282 } 283 } 284 for (p = argv, ip = used; *p; ++p, ++ip) 285 if (!*ip) 286 (void)fprintf(stderr, 287 "finger: %s: no such user\n", *p); 288 } 289 290 /* Handle network requests. */ 291 net: for (p = nargv; *p;) { 292 netfinger(*p++); 293 if (*p || entries) 294 printf("\n"); 295 } 296 297 if (entries == 0) 298 return; 299 300 /* 301 * Scan thru the list of users currently logged in, saving 302 * appropriate data whenever a match occurs. 303 */ 304 if (!freopen(_PATH_UTMP, "r", stdin)) 305 err(1, "%s", _PATH_UTMP); 306 while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 307 if (!user.ut_name[0]) 308 continue; 309 if ((pn = find_person(user.ut_name)) == NULL) 310 continue; 311 enter_where(&user, pn); 312 } 313 if (db) 314 for (sflag = R_FIRST;; sflag = R_NEXT) { 315 PERSON *tmp; 316 317 r = (*db->seq)(db, &key, &data, sflag); 318 if (r == -1) 319 err(1, "db seq"); 320 if (r == 1) 321 break; 322 memmove(&tmp, data.data, sizeof tmp); 323 enter_lastlog(tmp); 324 } 325 } 326