2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
37 static char sccsid[] = "@(#)util.c 8.3 (Berkeley) 4/28/95";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/socket.h>
60 #include "pathnames.h"
62 static void find_idle_and_ttywrite(WHERE *);
63 static void userinfo(PERSON *, struct passwd *);
64 static WHERE *walloc(PERSON *);
67 match(struct passwd *pw, const char *user)
72 if (!strcasecmp(pw->pw_name, user))
77 * Why do we skip asterisks!?!?
79 (void)strncpy(p = tbuf, pw->pw_gecos, sizeof(tbuf));
80 tbuf[sizeof(tbuf) - 1] = '\0';
84 /* Ampersands get replaced by the login name. */
85 if ((p = strtok(p, ",")) == NULL)
88 for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) {
90 (void)strncpy(t, pw->pw_name,
91 sizeof(name) - (t - name));
92 name[sizeof(name) - 1] = '\0';
93 while (t < &name[sizeof(name) - 1] && *++t)
100 for (t = name; (p = strtok(t, "\t ")) != NULL; t = NULL)
101 if (!strcasecmp(p, user))
107 enter_lastlog(PERSON *pn)
110 struct utmpx *ut = NULL;
113 if (setutxdb(UTXDB_LASTLOGIN, NULL) == 0)
114 ut = getutxuser(pn->name);
115 if ((w = pn->whead) == NULL)
117 else if (ut != NULL && ut->ut_type == USER_PROCESS) {
118 /* if last login is earlier than some current login */
119 for (; !doit && w != NULL; w = w->next)
120 if (w->info == LOGGEDIN &&
121 w->loginat < ut->ut_tv.tv_sec)
124 * and if it's not any of the current logins
125 * can't use time comparison because there may be a small
126 * discrepancy since login calls time() twice
128 for (w = pn->whead; doit && w != NULL; w = w->next)
129 if (w->info == LOGGEDIN &&
130 strcmp(w->tty, ut->ut_line) == 0)
133 if (ut != NULL && doit) {
136 strcpy(w->tty, ut->ut_line);
137 strcpy(w->host, ut->ut_host);
138 w->loginat = ut->ut_tv.tv_sec;
144 enter_where(struct utmpx *ut, PERSON *pn)
150 strcpy(w->tty, ut->ut_line);
151 strcpy(w->host, ut->ut_host);
152 w->loginat = ut->ut_tv.tv_sec;
153 find_idle_and_ttywrite(w);
157 enter_person(struct passwd *pw)
163 (db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL)) == NULL)
166 key.data = pw->pw_name;
167 key.size = strlen(pw->pw_name);
169 switch ((*db->get)(db, &key, &data, 0)) {
171 memmove(&pn, data.data, sizeof pn);
183 data.size = sizeof(PERSON *);
185 if ((*db->put)(db, &key, &data, 0))
192 find_person(char *name)
202 if ((pw = getpwnam(name)) && hide(pw))
206 key.size = strlen(name);
208 if ((*db->get)(db, &key, &data, 0))
210 memmove(&p, data.data, sizeof p);
219 if ((p = malloc(sizeof(PERSON))) == NULL)
229 if ((w = malloc(sizeof(WHERE))) == NULL)
231 if (pn->whead == NULL)
232 pn->whead = pn->wtail = w;
246 static char pbuf[20];
248 /* don't touch anything if the user has their own formatting */
249 for (p = num; *p; ++p)
255 case 11: /* +0-123-456-7890 */
260 case 10: /* 012-345-6789 */
266 case 7: /* 012-3456 */
271 case 5: /* x0-1234 */
291 find_idle_and_ttywrite(WHERE *w)
297 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_DEV, w->tty);
299 error = stat(tbuf, &sb);
300 if (error < 0 && errno == ENOENT) {
302 * The terminal listed is not actually a terminal (i.e.,
303 * ":0"). This is a failure, so we'll skip printing
304 * out the idle time, which is non-ideal but better
305 * than a bogus warning and idle time.
309 } else if (error < 0) {
314 touched = sb.st_atime;
315 if (touched < w->loginat) {
316 /* tty untouched since before login */
317 touched = w->loginat;
319 w->idletime = now < touched ? 0 : now - touched;
321 #define TALKABLE 0220 /* tty is writable if 220 mode */
322 w->writable = ((sb.st_mode & TALKABLE) == TALKABLE);
326 userinfo(PERSON *pn, struct passwd *pw)
329 char *bp, name[1024];
332 pn->realname = pn->office = pn->officephone = pn->homephone = NULL;
334 pn->uid = pw->pw_uid;
335 if ((pn->name = strdup(pw->pw_name)) == NULL)
336 err(1, "strdup failed");
337 if ((pn->dir = strdup(pw->pw_dir)) == NULL)
338 err(1, "strdup failed");
339 if ((pn->shell = strdup(pw->pw_shell)) == NULL)
340 err(1, "strdup failed");
342 /* why do we skip asterisks!?!? */
343 (void)strncpy(bp = tbuf, pw->pw_gecos, sizeof(tbuf));
344 tbuf[sizeof(tbuf) - 1] = '\0';
348 /* ampersands get replaced by the login name */
349 if (!(p = strsep(&bp, ",")))
351 for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) {
353 (void)strncpy(t, pw->pw_name,
354 sizeof(name) - (t - name));
355 name[sizeof(name) - 1] = '\0';
358 while (t < &name[sizeof(name) - 1] && *++t)
365 if ((pn->realname = strdup(name)) == NULL)
366 err(1, "strdup failed");
367 pn->office = ((p = strsep(&bp, ",")) && *p) ?
369 pn->officephone = ((p = strsep(&bp, ",")) && *p) ?
371 pn->homephone = ((p = strsep(&bp, ",")) && *p) ?
373 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pw->pw_name);
374 pn->mailrecv = -1; /* -1 == not_valid */
375 if (stat(tbuf, &sb) < 0) {
376 if (errno != ENOENT) {
380 } else if (sb.st_size != 0) {
381 pn->mailrecv = sb.st_mtime;
382 pn->mailread = sb.st_atime;
387 * Is this user hiding from finger?
388 * If ~<user>/.nofinger exists, return 1 (hide), else return 0 (nohide).
389 * Nobody can hide from root.
393 hide(struct passwd *pw)
396 char buf[MAXPATHLEN];
398 if (invoker_root || !pw->pw_dir)
401 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, _PATH_NOFINGER);
403 if (stat(buf, &st) == 0)