1 /* $NetBSD: finger.c,v 1.15 2002/05/02 13:04:10 wiz Exp $ */ 2 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 /* 40 * Luke Mewburn <lukem@netbsd.org> added the following on 961121: 41 * - mail status ("No Mail", "Mail read:...", or "New Mail ..., 42 * Unread since ...".) 43 * - 4 digit phone extensions (3210 is printed as x3210.) 44 * - host/office toggling in short format with -h & -o. 45 * - short day names (`Tue' printed instead of `Jun 21' if the 46 * login time is < 6 days. 47 */ 48 49 #include <sys/cdefs.h> 50 #ifndef lint 51 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\ 52 The Regents of the University of California. All rights reserved.\n"); 53 #endif /* not lint */ 54 55 #ifndef lint 56 #if 0 57 static char sccsid[] = "@(#)finger.c 8.5 (Berkeley) 5/4/95"; 58 #else 59 __RCSID("$NetBSD: finger.c,v 1.15 2002/05/02 13:04:10 wiz Exp $"); 60 #endif 61 #endif /* not lint */ 62 63 /* 64 * Finger prints out information about users. It is not portable since 65 * certain fields (e.g. the full user name, office, and phone numbers) are 66 * extracted from the gecos field of the passwd file which other UNIXes 67 * may not have or may use for other things. 68 * 69 * There are currently two output formats; the short format is one line 70 * per user and displays login name, tty, login time, real name, idle time, 71 * and either remote host information (default) or office location/phone 72 * number, depending on if -h or -o is used respectively. 73 * The long format gives the same information (in a more legible format) as 74 * well as home directory, shell, mail info, and .plan/.project files. 75 */ 76 77 #include <sys/param.h> 78 79 #include <db.h> 80 #include <err.h> 81 #include <errno.h> 82 #include <fcntl.h> 83 #include <pwd.h> 84 #include <stdio.h> 85 #include <stdlib.h> 86 #include <string.h> 87 #include <time.h> 88 #include <unistd.h> 89 #include <utmp.h> 90 91 #include "finger.h" 92 #include "extern.h" 93 94 DB *db; 95 time_t now; 96 int entries, gflag, lflag, mflag, oflag, sflag, pplan; 97 char tbuf[1024]; 98 99 static void loginlist __P((void)); 100 static void userlist __P((int, char **)); 101 int main __P((int, char **)); 102 103 int 104 main(argc, argv) 105 int argc; 106 char **argv; 107 { 108 int ch; 109 110 oflag = 1; /* default to old "office" behavior */ 111 112 while ((ch = getopt(argc, argv, "lmpshog")) != -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 'g': 133 gflag = 1; /* no gecos info, besides name */ 134 break; 135 case '?': 136 default: 137 (void)fprintf(stderr, 138 "usage: finger [-lmpshog] [login ...]\n"); 139 exit(1); 140 } 141 argc -= optind; 142 argv += optind; 143 144 (void)time(&now); 145 setpassent(1); 146 if (!*argv) { 147 /* 148 * Assign explicit "small" format if no names given and -l 149 * not selected. Force the -s BEFORE we get names so proper 150 * screening will be done. 151 */ 152 if (!lflag) 153 sflag = 1; /* if -l not explicit, force -s */ 154 loginlist(); 155 if (entries == 0) 156 (void)printf("No one logged on.\n"); 157 } else { 158 userlist(argc, argv); 159 /* 160 * Assign explicit "large" format if names given and -s not 161 * explicitly stated. Force the -l AFTER we get names so any 162 * remote finger attempts specified won't be mishandled. 163 */ 164 if (!sflag) 165 lflag = 1; /* if -s not explicit, force -l */ 166 } 167 if (entries) { 168 if (lflag) 169 lflag_print(); 170 else 171 sflag_print(); 172 } 173 return (0); 174 } 175 176 static void 177 loginlist() 178 { 179 PERSON *pn; 180 DBT data, key; 181 struct passwd *pw; 182 struct utmp user; 183 int r, sflag; 184 char name[UT_NAMESIZE + 1]; 185 186 if (!freopen(_PATH_UTMP, "r", stdin)) 187 err(1, "cant read %s", _PATH_UTMP); 188 name[UT_NAMESIZE] = '\0'; 189 while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 190 if (!user.ut_name[0]) 191 continue; 192 if ((pn = find_person(user.ut_name)) == NULL) { 193 memcpy(name, user.ut_name, UT_NAMESIZE); 194 if ((pw = getpwnam(name)) == NULL) 195 continue; 196 pn = enter_person(pw); 197 } 198 enter_where(&user, pn); 199 } 200 if (db && lflag) 201 for (sflag = R_FIRST;; sflag = R_NEXT) { 202 PERSON *tmp; 203 204 r = (*db->seq)(db, &key, &data, sflag); 205 if (r == -1) 206 err(1, "db seq"); 207 if (r == 1) 208 break; 209 memmove(&tmp, data.data, sizeof tmp); 210 enter_lastlog(tmp); 211 } 212 } 213 214 static void 215 userlist(argc, argv) 216 int argc; 217 char **argv; 218 { 219 register PERSON *pn; 220 DBT data, key; 221 struct utmp user; 222 struct passwd *pw; 223 int r, sflag, *used, *ip; 224 char **ap, **nargv, **np, **p; 225 226 if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL || 227 (used = calloc(argc, sizeof(int))) == NULL) 228 err(1, NULL); 229 230 /* Pull out all network requests. */ 231 for (ap = p = argv, np = nargv; *p; ++p) 232 if (strchr(*p, '@')) 233 *np++ = *p; 234 else 235 *ap++ = *p; 236 237 *np++ = NULL; 238 *ap++ = NULL; 239 240 if (!*argv) 241 goto net; 242 243 /* 244 * Traverse the list of possible login names and check the login name 245 * and real name against the name specified by the user. 246 */ 247 if (mflag) { 248 for (p = argv; *p; ++p) 249 if ((pw = getpwnam(*p)) != NULL) 250 enter_person(pw); 251 else 252 (void)fprintf(stderr, 253 "finger: %s: no such user\n", *p); 254 } else { 255 while ((pw = getpwent()) != NULL) 256 for (p = argv, ip = used; *p; ++p, ++ip) 257 if (match(pw, *p)) { 258 enter_person(pw); 259 *ip = 1; 260 } 261 for (p = argv, ip = used; *p; ++p, ++ip) 262 if (!*ip) 263 (void)fprintf(stderr, 264 "finger: %s: no such user\n", *p); 265 } 266 267 /* Handle network requests. */ 268 net: 269 for (p = nargv; *p;) 270 netfinger(*p++); 271 272 if (entries == 0) 273 return; 274 275 /* 276 * Scan thru the list of users currently logged in, saving 277 * appropriate data whenever a match occurs. 278 */ 279 if (!freopen(_PATH_UTMP, "r", stdin)) 280 err(1, "%s", _PATH_UTMP); 281 while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 282 if (!user.ut_name[0]) 283 continue; 284 if ((pn = find_person(user.ut_name)) == NULL) 285 continue; 286 enter_where(&user, pn); 287 } 288 if (db) 289 for (sflag = R_FIRST;; sflag = R_NEXT) { 290 PERSON *tmp; 291 292 r = (*db->seq)(db, &key, &data, sflag); 293 if (r == -1) 294 err(1, "db seq"); 295 if (r == 1) 296 break; 297 memmove(&tmp, data.data, sizeof tmp); 298 enter_lastlog(tmp); 299 } 300 } 301