1 /* $OpenBSD: util.c,v 1.18 2003/06/25 21:10:55 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 1989 The Regents of the University of California. 5 * All rights reserved. 6 * Portions Copyright (c) 1983, 1995, 1996 Eric P. Allman (woof!) 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 /*static char sccsid[] = "from: @(#)util.c 5.14 (Berkeley) 1/17/91";*/ 38 static char rcsid[] = "$OpenBSD: util.c,v 1.18 2003/06/25 21:10:55 deraadt Exp $"; 39 #endif /* not lint */ 40 41 #include <sys/types.h> 42 #include <sys/uio.h> 43 #include <sys/param.h> 44 #include <sys/stat.h> 45 #include <err.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <ctype.h> 49 #include <string.h> 50 #include <paths.h> 51 #include <errno.h> 52 #include <fcntl.h> 53 #include <unistd.h> 54 #include <vis.h> 55 #include <err.h> 56 #include "finger.h" 57 #include "extern.h" 58 59 char *estrdup(char *); 60 WHERE *walloc(PERSON *pn); 61 62 void 63 find_idle_and_ttywrite(WHERE *w) 64 { 65 struct stat sb; 66 67 (void)snprintf(tbuf, sizeof(tbuf), "%s%s", _PATH_DEV, w->tty); 68 if (stat(tbuf, &sb) < 0) { 69 /* Don't bitch about it, just handle it... */ 70 w->idletime = 0; 71 w->writable = 0; 72 73 return; 74 } 75 w->idletime = now < sb.st_atime ? 0 : now - sb.st_atime; 76 77 #define TALKABLE 0220 /* tty is writable if 220 mode */ 78 w->writable = ((sb.st_mode & TALKABLE) == TALKABLE); 79 } 80 81 char * 82 estrdup(char *s) 83 { 84 char *p = strdup(s); 85 if (!p) 86 err(1, "strdup"); 87 return (p); 88 } 89 90 void 91 userinfo(PERSON *pn, struct passwd *pw) 92 { 93 char *p; 94 char *bp, name[1024]; 95 struct stat sb; 96 97 pn->realname = pn->office = pn->officephone = pn->homephone = NULL; 98 99 pn->uid = pw->pw_uid; 100 pn->name = estrdup(pw->pw_name); 101 pn->dir = estrdup(pw->pw_dir); 102 pn->shell = estrdup(pw->pw_shell); 103 104 (void)strncpy(bp = tbuf, pw->pw_gecos, sizeof(tbuf)); 105 106 /* ampersands get replaced by the login name */ 107 if (!(p = strsep(&bp, ","))) 108 return; 109 expandusername(p, pw->pw_name, name, sizeof(name)); 110 pn->realname = estrdup(name); 111 pn->office = ((p = strsep(&bp, ",")) && *p) ? 112 estrdup(p) : NULL; 113 pn->officephone = ((p = strsep(&bp, ",")) && *p) ? 114 estrdup(p) : NULL; 115 pn->homephone = ((p = strsep(&bp, ",")) && *p) ? 116 estrdup(p) : NULL; 117 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILSPOOL, 118 pw->pw_name); 119 pn->mailrecv = -1; /* -1 == not_valid */ 120 if (stat(tbuf, &sb) < 0) { 121 if (errno != ENOENT) { 122 warn("%s", tbuf); 123 return; 124 } 125 } else if (sb.st_size != 0) { 126 pn->mailrecv = sb.st_mtime; 127 pn->mailread = sb.st_atime; 128 } 129 } 130 131 int 132 match(struct passwd *pw, char *user) 133 { 134 char *p, *t; 135 char name[1024]; 136 137 (void)strncpy(p = tbuf, pw->pw_gecos, sizeof(tbuf)); 138 139 /* ampersands get replaced by the login name */ 140 if (!(p = strtok(p, ","))) 141 return(0); 142 expandusername(p, pw->pw_name, name, sizeof(name)); 143 for (t = name; (p = strtok(t, "\t ")) != NULL; t = NULL) 144 if (!strcasecmp(p, user)) 145 return(1); 146 return(0); 147 } 148 149 /* inspired by usr.sbin/sendmail/util.c::buildfname */ 150 void 151 expandusername(char *gecos, char *login, char *buf, int buflen) 152 { 153 char *p, *bp; 154 155 /* why do we skip asterisks!?!? */ 156 if (*gecos == '*') 157 gecos++; 158 bp = buf; 159 160 /* copy gecos, interpolating & to be full name */ 161 for (p = gecos; *p != '\0'; p++) { 162 if (bp >= &buf[buflen - 1]) { 163 /* buffer overflow - just use login name */ 164 snprintf(buf, buflen, "%s", login); 165 buf[buflen - 1] = '\0'; 166 return; 167 } 168 if (*p == '&') { 169 /* interpolate full name */ 170 snprintf(bp, buflen - (bp - buf), "%s", login); 171 *bp = toupper(*bp); 172 bp += strlen(bp); 173 } 174 else 175 *bp++ = *p; 176 } 177 *bp = '\0'; 178 } 179 180 void 181 enter_lastlog(PERSON *pn) 182 { 183 WHERE *w; 184 static int opened, fd; 185 struct lastlog ll; 186 char doit = 0; 187 188 /* some systems may not maintain lastlog, don't report errors. */ 189 if (!opened) { 190 fd = open(_PATH_LASTLOG, O_RDONLY); 191 opened = 1; 192 } 193 if (fd == -1 || 194 lseek(fd, (off_t)(pn->uid * sizeof(ll)), SEEK_SET) != 195 (long)(pn->uid * sizeof(ll)) || 196 read(fd, (char *)&ll, sizeof(ll)) != sizeof(ll)) { 197 /* as if never logged in */ 198 ll.ll_line[0] = ll.ll_host[0] = '\0'; 199 ll.ll_time = 0; 200 } 201 if ((w = pn->whead) == NULL) 202 doit = 1; 203 else if (ll.ll_time != 0) { 204 /* if last login is earlier than some current login */ 205 for (; !doit && w != NULL; w = w->next) 206 if (w->info == LOGGEDIN && w->loginat < ll.ll_time) 207 doit = 1; 208 /* 209 * and if it's not any of the current logins 210 * can't use time comparison because there may be a small 211 * discrepency since login calls time() twice 212 */ 213 for (w = pn->whead; doit && w != NULL; w = w->next) 214 if (w->info == LOGGEDIN && 215 strncmp(w->tty, ll.ll_line, UT_LINESIZE) == 0) 216 doit = 0; 217 } 218 if (doit) { 219 w = walloc(pn); 220 w->info = LASTLOG; 221 bcopy(ll.ll_line, w->tty, UT_LINESIZE); 222 w->tty[UT_LINESIZE] = 0; 223 bcopy(ll.ll_host, w->host, UT_HOSTSIZE); 224 w->host[UT_HOSTSIZE] = 0; 225 w->loginat = ll.ll_time; 226 } 227 } 228 229 void 230 enter_where(struct utmp *ut, PERSON *pn) 231 { 232 WHERE *w = walloc(pn); 233 234 w->info = LOGGEDIN; 235 bcopy(ut->ut_line, w->tty, UT_LINESIZE); 236 w->tty[UT_LINESIZE] = 0; 237 bcopy(ut->ut_host, w->host, UT_HOSTSIZE); 238 w->host[UT_HOSTSIZE] = 0; 239 w->loginat = (time_t)ut->ut_time; 240 find_idle_and_ttywrite(w); 241 } 242 243 PERSON * 244 enter_person(struct passwd *pw) 245 { 246 PERSON *pn, **pp; 247 248 for (pp = htab + hash(pw->pw_name); 249 *pp != NULL && strcmp((*pp)->name, pw->pw_name) != 0; 250 pp = &(*pp)->hlink) 251 ; 252 if ((pn = *pp) == NULL) { 253 pn = palloc(); 254 entries++; 255 if (phead == NULL) 256 phead = ptail = pn; 257 else { 258 ptail->next = pn; 259 ptail = pn; 260 } 261 pn->next = NULL; 262 pn->hlink = NULL; 263 *pp = pn; 264 userinfo(pn, pw); 265 pn->whead = NULL; 266 } 267 return(pn); 268 } 269 270 PERSON * 271 find_person(char *name) 272 { 273 PERSON *pn; 274 275 /* name may be only UT_NAMESIZE long and not terminated */ 276 for (pn = htab[hash(name)]; 277 pn != NULL && strncmp(pn->name, name, UT_NAMESIZE) != 0; 278 pn = pn->hlink) 279 ; 280 return(pn); 281 } 282 283 int 284 hash(char *name) 285 { 286 int h, i; 287 288 h = 0; 289 /* name may be only UT_NAMESIZE long and not terminated */ 290 for (i = UT_NAMESIZE; --i >= 0 && *name;) 291 h = ((h << 2 | h >> (HBITS - 2)) ^ *name++) & HMASK; 292 return(h); 293 } 294 295 PERSON * 296 palloc(void) 297 { 298 PERSON *p; 299 300 if ((p = (PERSON *)malloc((u_int) sizeof(PERSON))) == NULL) 301 err(1, "malloc"); 302 return(p); 303 } 304 305 WHERE * 306 walloc(PERSON *pn) 307 { 308 WHERE *w; 309 310 if ((w = (WHERE *)malloc((u_int) sizeof(WHERE))) == NULL) 311 err(1, "malloc"); 312 if (pn->whead == NULL) 313 pn->whead = pn->wtail = w; 314 else { 315 pn->wtail->next = w; 316 pn->wtail = w; 317 } 318 w->next = NULL; 319 return(w); 320 } 321 322 char * 323 prphone(char *num) 324 { 325 char *p; 326 int len; 327 static char pbuf[15]; 328 329 /* don't touch anything if the user has their own formatting */ 330 for (p = num; *p; ++p) 331 if (!isdigit(*p)) 332 return(num); 333 len = p - num; 334 p = pbuf; 335 switch(len) { 336 case 11: /* +0-123-456-7890 */ 337 *p++ = '+'; 338 *p++ = *num++; 339 *p++ = '-'; 340 /* FALLTHROUGH */ 341 case 10: /* 012-345-6789 */ 342 *p++ = *num++; 343 *p++ = *num++; 344 *p++ = *num++; 345 *p++ = '-'; 346 /* FALLTHROUGH */ 347 case 7: /* 012-3456 */ 348 *p++ = *num++; 349 *p++ = *num++; 350 *p++ = *num++; 351 break; 352 case 5: /* x0-1234 */ 353 case 4: /* x1234 */ 354 *p++ = 'x'; 355 *p++ = *num++; 356 break; 357 default: 358 return(num); 359 } 360 if (len != 4) { 361 *p++ = '-'; 362 *p++ = *num++; 363 } 364 *p++ = *num++; 365 *p++ = *num++; 366 *p++ = *num++; 367 *p = '\0'; 368 return(pbuf); 369 } 370 371 /* Like strvis(), but use malloc() to get the space and return a pointer 372 * to the beginning of the converted string, not the end. 373 * 374 * The caller is responsible for free()'ing the returned string. 375 */ 376 char * 377 vs(char *src) 378 { 379 char *dst; 380 381 if ((dst = malloc((4 * strlen(src)) + 1)) == NULL) 382 err(1, "malloc failed"); 383 384 strvis(dst, src, VIS_SAFE|VIS_NOSLASH); 385 return(dst); 386 } 387