1 /* $OpenBSD: finger.c,v 1.27 2018/04/26 12:42:51 guenther Exp $ */ 2 3 /* 4 * Copyright (c) 1989 The Regents of the University of California. 5 * 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 <lukem@netbsd.org> added the following on 961121: 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 /* 46 * Finger prints out information about users. It is not portable since 47 * certain fields (e.g. the full user name, office, and phone numbers) are 48 * extracted from the gecos field of the passwd file which other UNIXes 49 * may not have or may use for other things. 50 * 51 * There are currently two output formats; the short format is one line 52 * per user and displays login name, tty, login time, real name, idle time, 53 * and either remote host information (default) or office location/phone 54 * number, depending on if -h or -o is used respectively. 55 * The long format gives the same information (in a more legible format) as 56 * well as home directory, shell, mail info, and .plan/.project files. 57 */ 58 59 #include <sys/stat.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <time.h> 64 #include <unistd.h> 65 #include <limits.h> 66 #include <err.h> 67 #include "finger.h" 68 #include "extern.h" 69 70 time_t now; 71 int entries, lflag, sflag, mflag, oflag, pplan, Mflag; 72 char tbuf[1024]; 73 PERSON *htab[HSIZE]; 74 PERSON *phead, *ptail; 75 76 int 77 main(int argc, char *argv[]) 78 { 79 extern int optind; 80 extern char *__progname; 81 int ch; 82 char domain[HOST_NAME_MAX+1]; 83 struct stat sb; 84 85 oflag = 1; /* default to old "office" behavior */ 86 87 while ((ch = getopt(argc, argv, "lmMpsho")) != -1) 88 switch(ch) { 89 case 'l': 90 lflag = 1; /* long format */ 91 break; 92 case 'm': 93 mflag = 1; /* force exact match of names */ 94 break; 95 case 'M': 96 Mflag = 1; /* allow name matching */ 97 break; 98 case 'p': 99 pplan = 1; /* don't show .plan/.project */ 100 break; 101 case 's': 102 sflag = 1; /* short format */ 103 break; 104 case 'h': 105 oflag = 0; /* remote host info */ 106 break; 107 case 'o': 108 oflag = 1; /* office info */ 109 break; 110 case '?': 111 default: 112 (void)fprintf(stderr, 113 "usage: %s [-hlMmops] [login ...]\n", __progname); 114 exit(1); 115 } 116 argc -= optind; 117 argv += optind; 118 119 /* If a domainname is set, increment mflag. */ 120 if ((getdomainname(domain, sizeof(domain)) == 0) && domain[0]) 121 mflag++; 122 /* If _PATH_MP_DB is larger than 1MB, increment mflag. */ 123 if (stat(_PATH_MP_DB, &sb) == 0) { 124 if (sb.st_size > 1048576) 125 mflag++; 126 } 127 128 if (pledge("stdio rpath getpw dns inet", NULL) == -1) 129 err(1, "pledge"); 130 131 (void)time(&now); 132 if (!*argv) { 133 /* 134 * Assign explicit "small" format if no names given and -l 135 * not selected. Force the -s BEFORE we get names so proper 136 * screening will be done. 137 */ 138 if (pledge("stdio rpath getpw", NULL) == -1) 139 err(1, "pledge"); 140 141 if (!lflag) 142 sflag = 1; /* if -l not explicit, force -s */ 143 loginlist(); 144 if (entries == 0) 145 (void)printf("No one logged on.\n"); 146 } else { 147 userlist(argc, argv); 148 /* 149 * Assign explicit "large" format if names given and -s not 150 * explicitly stated. Force the -l AFTER we get names so any 151 * remote finger attempts specified won't be mishandled. 152 */ 153 if (!sflag) 154 lflag = 1; /* if -s not explicit, force -l */ 155 } 156 if (entries != 0) { 157 if (lflag) 158 lflag_print(); 159 else 160 sflag_print(); 161 } 162 exit(0); 163 } 164 165 void 166 loginlist(void) 167 { 168 PERSON *pn; 169 struct passwd *pw; 170 struct utmp user; 171 char name[UT_NAMESIZE + 1]; 172 173 if (!freopen(_PATH_UTMP, "r", stdin)) 174 err(2, _PATH_UTMP); 175 name[UT_NAMESIZE] = '\0'; 176 setpassent(1); 177 while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 178 if (!user.ut_name[0]) 179 continue; 180 if ((pn = find_person(user.ut_name)) == NULL) { 181 bcopy(user.ut_name, name, UT_NAMESIZE); 182 if ((pw = getpwnam(name)) == NULL) 183 continue; 184 pn = enter_person(pw); 185 } 186 enter_where(&user, pn); 187 } 188 endpwent(); 189 for (pn = phead; lflag && pn != NULL; pn = pn->next) 190 enter_lastlog(pn); 191 } 192 193 void 194 userlist(int argc, char **argv) 195 { 196 int i; 197 PERSON *pn; 198 PERSON *nethead, **nettail; 199 struct utmp user; 200 struct passwd *pw; 201 int dolocal, *used; 202 203 if (!(used = calloc((u_int)argc, (u_int)sizeof(int)))) 204 err(2, "malloc"); 205 206 /* pull out all network requests */ 207 for (i = 0, dolocal = 0, nettail = &nethead; i < argc; i++) { 208 if (!strchr(argv[i], '@')) { 209 dolocal = 1; 210 continue; 211 } 212 pn = palloc(); 213 *nettail = pn; 214 nettail = &pn->next; 215 pn->name = argv[i]; 216 used[i] = -1; 217 } 218 *nettail = NULL; 219 220 if (!dolocal) 221 goto net; 222 223 if (nettail == &nethead) 224 if (pledge("stdio rpath getpw", NULL) == -1) 225 err(1, "pledge"); 226 227 /* 228 * traverse the list of possible login names and check the login name 229 * and real name against the name specified by the user. 230 */ 231 setpassent(1); 232 if ((mflag - Mflag) > 0) { 233 for (i = 0; i < argc; i++) 234 if (used[i] >= 0 && (pw = getpwnam(argv[i]))) { 235 enter_person(pw); 236 used[i] = 1; 237 } 238 } else while ((pw = getpwent()) != NULL) 239 for (i = 0; i < argc; i++) 240 if (used[i] >= 0 && 241 (!strcasecmp(pw->pw_name, argv[i]) || 242 match(pw, argv[i]))) { 243 enter_person(pw); 244 used[i] = 1; 245 } 246 endpwent(); 247 248 /* list errors */ 249 for (i = 0; i < argc; i++) 250 if (!used[i]) 251 warnx("%s: no such user.", argv[i]); 252 253 /* handle network requests */ 254 net: for (pn = nethead; pn; pn = pn->next) { 255 netfinger(pn->name); 256 if (pn->next || entries) 257 putchar('\n'); 258 } 259 260 free(used); 261 if (entries == 0) 262 return; 263 264 /* 265 * Scan thru the list of users currently logged in, saving 266 * appropriate data whenever a match occurs. 267 */ 268 if (!freopen(_PATH_UTMP, "r", stdin)) 269 err(1, _PATH_UTMP); 270 while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 271 if (!user.ut_name[0]) 272 continue; 273 if ((pn = find_person(user.ut_name)) == NULL) 274 continue; 275 enter_where(&user, pn); 276 } 277 for (pn = phead; pn != NULL; pn = pn->next) 278 enter_lastlog(pn); 279 } 280