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 #if 0 38 #ifndef lint 39 static char sccsid[] = "@(#)util.c 8.3 (Berkeley) 4/28/95"; 40 #endif 41 #endif 42 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #include <sys/param.h> 47 #include <sys/stat.h> 48 #include <ctype.h> 49 #include <db.h> 50 #include <err.h> 51 #include <errno.h> 52 #include <fcntl.h> 53 #include <paths.h> 54 #include <pwd.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <unistd.h> 59 #include <utmp.h> 60 #include "finger.h" 61 #include "pathnames.h" 62 63 static void find_idle_and_ttywrite(WHERE *); 64 static void userinfo(PERSON *, struct passwd *); 65 static WHERE *walloc(PERSON *); 66 67 int 68 match(pw, user) 69 struct passwd *pw; 70 char *user; 71 { 72 char *p, *t; 73 char name[1024]; 74 75 if (!strcasecmp(pw->pw_name, user)) 76 return(1); 77 78 /* 79 * XXX 80 * Why do we skip asterisks!?!? 81 */ 82 (void)strncpy(p = tbuf, pw->pw_gecos, sizeof(tbuf)); 83 tbuf[sizeof(tbuf) - 1] = '\0'; 84 if (*p == '*') 85 ++p; 86 87 /* Ampersands get replaced by the login name. */ 88 if ((p = strtok(p, ",")) == NULL) 89 return(0); 90 91 for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) { 92 if (*t == '&') { 93 (void)strncpy(t, pw->pw_name, 94 sizeof(name) - (t - name)); 95 name[sizeof(name) - 1] = '\0'; 96 while (t < &name[sizeof(name) - 1] && *++t) 97 continue; 98 } else { 99 ++t; 100 } 101 } 102 *t = '\0'; 103 for (t = name; (p = strtok(t, "\t ")) != NULL; t = NULL) 104 if (!strcasecmp(p, user)) 105 return(1); 106 return(0); 107 } 108 109 void 110 enter_lastlog(pn) 111 PERSON *pn; 112 { 113 WHERE *w; 114 static int opened, fd; 115 struct lastlog ll; 116 char doit = 0; 117 118 /* some systems may not maintain lastlog, don't report errors. */ 119 if (!opened) { 120 fd = open(_PATH_LASTLOG, O_RDONLY, 0); 121 opened = 1; 122 } 123 if (fd == -1 || 124 lseek(fd, (long)pn->uid * sizeof(ll), SEEK_SET) != 125 (long)pn->uid * sizeof(ll) || 126 read(fd, (char *)&ll, sizeof(ll)) != sizeof(ll)) { 127 /* as if never logged in */ 128 ll.ll_line[0] = ll.ll_host[0] = '\0'; 129 ll.ll_time = 0; 130 } 131 if ((w = pn->whead) == NULL) 132 doit = 1; 133 else if (ll.ll_time != 0) { 134 /* if last login is earlier than some current login */ 135 for (; !doit && w != NULL; w = w->next) 136 if (w->info == LOGGEDIN && w->loginat < ll.ll_time) 137 doit = 1; 138 /* 139 * and if it's not any of the current logins 140 * can't use time comparison because there may be a small 141 * discrepancy since login calls time() twice 142 */ 143 for (w = pn->whead; doit && w != NULL; w = w->next) 144 if (w->info == LOGGEDIN && 145 strncmp(w->tty, ll.ll_line, UT_LINESIZE) == 0) 146 doit = 0; 147 } 148 if (doit) { 149 w = walloc(pn); 150 w->info = LASTLOG; 151 bcopy(ll.ll_line, w->tty, UT_LINESIZE); 152 w->tty[UT_LINESIZE] = 0; 153 bcopy(ll.ll_host, w->host, UT_HOSTSIZE); 154 w->host[UT_HOSTSIZE] = 0; 155 w->loginat = ll.ll_time; 156 } 157 } 158 159 void 160 enter_where(ut, pn) 161 struct utmp *ut; 162 PERSON *pn; 163 { 164 WHERE *w; 165 166 w = walloc(pn); 167 w->info = LOGGEDIN; 168 bcopy(ut->ut_line, w->tty, UT_LINESIZE); 169 w->tty[UT_LINESIZE] = 0; 170 bcopy(ut->ut_host, w->host, UT_HOSTSIZE); 171 w->host[UT_HOSTSIZE] = 0; 172 w->loginat = (time_t)ut->ut_time; 173 find_idle_and_ttywrite(w); 174 } 175 176 PERSON * 177 enter_person(pw) 178 struct passwd *pw; 179 { 180 DBT data, key; 181 PERSON *pn; 182 183 if (db == NULL && 184 (db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL)) == NULL) 185 err(1, NULL); 186 187 key.data = pw->pw_name; 188 key.size = strlen(pw->pw_name); 189 190 switch ((*db->get)(db, &key, &data, 0)) { 191 case 0: 192 memmove(&pn, data.data, sizeof pn); 193 return (pn); 194 default: 195 case -1: 196 err(1, "db get"); 197 /* NOTREACHED */ 198 case 1: 199 ++entries; 200 pn = palloc(); 201 userinfo(pn, pw); 202 pn->whead = NULL; 203 204 data.size = sizeof(PERSON *); 205 data.data = &pn; 206 if ((*db->put)(db, &key, &data, 0)) 207 err(1, "db put"); 208 return (pn); 209 } 210 } 211 212 PERSON * 213 find_person(name) 214 char *name; 215 { 216 struct passwd *pw; 217 218 int cnt; 219 DBT data, key; 220 PERSON *p; 221 char buf[UT_NAMESIZE + 1]; 222 223 if (!db) 224 return(NULL); 225 226 if ((pw = getpwnam(name)) && hide(pw)) 227 return(NULL); 228 229 /* Name may be only UT_NAMESIZE long and not NUL terminated. */ 230 for (cnt = 0; cnt < UT_NAMESIZE && *name; ++name, ++cnt) 231 buf[cnt] = *name; 232 buf[cnt] = '\0'; 233 key.data = buf; 234 key.size = cnt; 235 236 if ((*db->get)(db, &key, &data, 0)) 237 return (NULL); 238 memmove(&p, data.data, sizeof p); 239 return (p); 240 } 241 242 PERSON * 243 palloc() 244 { 245 PERSON *p; 246 247 if ((p = malloc(sizeof(PERSON))) == NULL) 248 err(1, NULL); 249 return(p); 250 } 251 252 static WHERE * 253 walloc(pn) 254 PERSON *pn; 255 { 256 WHERE *w; 257 258 if ((w = malloc(sizeof(WHERE))) == NULL) 259 err(1, NULL); 260 if (pn->whead == NULL) 261 pn->whead = pn->wtail = w; 262 else { 263 pn->wtail->next = w; 264 pn->wtail = w; 265 } 266 w->next = NULL; 267 return(w); 268 } 269 270 char * 271 prphone(num) 272 char *num; 273 { 274 char *p; 275 int len; 276 static char pbuf[20]; 277 278 /* don't touch anything if the user has their own formatting */ 279 for (p = num; *p; ++p) 280 if (!isdigit(*p)) 281 return(num); 282 len = p - num; 283 p = pbuf; 284 switch(len) { 285 case 11: /* +0-123-456-7890 */ 286 *p++ = '+'; 287 *p++ = *num++; 288 *p++ = '-'; 289 /* FALLTHROUGH */ 290 case 10: /* 012-345-6789 */ 291 *p++ = *num++; 292 *p++ = *num++; 293 *p++ = *num++; 294 *p++ = '-'; 295 /* FALLTHROUGH */ 296 case 7: /* 012-3456 */ 297 *p++ = *num++; 298 *p++ = *num++; 299 *p++ = *num++; 300 break; 301 case 5: /* x0-1234 */ 302 case 4: /* x1234 */ 303 *p++ = 'x'; 304 *p++ = *num++; 305 break; 306 default: 307 return(num); 308 } 309 if (len != 4) { 310 *p++ = '-'; 311 *p++ = *num++; 312 } 313 *p++ = *num++; 314 *p++ = *num++; 315 *p++ = *num++; 316 *p = '\0'; 317 return(pbuf); 318 } 319 320 static void 321 find_idle_and_ttywrite(w) 322 WHERE *w; 323 { 324 extern time_t now; 325 struct stat sb; 326 time_t touched; 327 328 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_DEV, w->tty); 329 if (stat(tbuf, &sb) < 0) { 330 warn("%s", tbuf); 331 return; 332 } 333 touched = sb.st_atime; 334 if (touched < w->loginat) { 335 /* tty untouched since before login */ 336 touched = w->loginat; 337 } 338 w->idletime = now < touched ? 0 : now - touched; 339 340 #define TALKABLE 0220 /* tty is writable if 220 mode */ 341 w->writable = ((sb.st_mode & TALKABLE) == TALKABLE); 342 } 343 344 static void 345 userinfo(pn, pw) 346 PERSON *pn; 347 struct passwd *pw; 348 { 349 char *p, *t; 350 char *bp, name[1024]; 351 struct stat sb; 352 353 pn->realname = pn->office = pn->officephone = pn->homephone = NULL; 354 355 pn->uid = pw->pw_uid; 356 if ((pn->name = strdup(pw->pw_name)) == NULL) 357 err(1, "strdup failed"); 358 if ((pn->dir = strdup(pw->pw_dir)) == NULL) 359 err(1, "strdup failed"); 360 if ((pn->shell = strdup(pw->pw_shell)) == NULL) 361 err(1, "strdup failed"); 362 363 /* why do we skip asterisks!?!? */ 364 (void)strncpy(bp = tbuf, pw->pw_gecos, sizeof(tbuf)); 365 tbuf[sizeof(tbuf) - 1] = '\0'; 366 if (*bp == '*') 367 ++bp; 368 369 /* ampersands get replaced by the login name */ 370 if (!(p = strsep(&bp, ","))) 371 return; 372 for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) { 373 if (*t == '&') { 374 (void)strncpy(t, pw->pw_name, 375 sizeof(name) - (t - name)); 376 name[sizeof(name) - 1] = '\0'; 377 if (islower(*t)) 378 *t = toupper(*t); 379 while (t < &name[sizeof(name) - 1] && *++t) 380 continue; 381 } else { 382 ++t; 383 } 384 } 385 *t = '\0'; 386 if ((pn->realname = strdup(name)) == NULL) 387 err(1, "strdup failed"); 388 pn->office = ((p = strsep(&bp, ",")) && *p) ? 389 strdup(p) : NULL; 390 pn->officephone = ((p = strsep(&bp, ",")) && *p) ? 391 strdup(p) : NULL; 392 pn->homephone = ((p = strsep(&bp, ",")) && *p) ? 393 strdup(p) : NULL; 394 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pw->pw_name); 395 pn->mailrecv = -1; /* -1 == not_valid */ 396 if (stat(tbuf, &sb) < 0) { 397 if (errno != ENOENT) { 398 warn("%s", tbuf); 399 return; 400 } 401 } else if (sb.st_size != 0) { 402 pn->mailrecv = sb.st_mtime; 403 pn->mailread = sb.st_atime; 404 } 405 } 406 407 /* 408 * Is this user hiding from finger? 409 * If ~<user>/.nofinger exists, return 1 (hide), else return 0 (nohide). 410 */ 411 412 int 413 hide(pw) 414 struct passwd *pw; 415 { 416 struct stat st; 417 char buf[MAXPATHLEN]; 418 419 if (!pw->pw_dir) 420 return 0; 421 422 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, _PATH_NOFINGER); 423 424 if (stat(buf, &st) == 0) 425 return 1; 426 427 return 0; 428 } 429