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