2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)util.c 8.3 (Berkeley) 4/28/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/socket.h>
58 #include "pathnames.h"
60 static void find_idle_and_ttywrite(WHERE *);
61 static void userinfo(PERSON *, struct passwd *);
62 static WHERE *walloc(PERSON *);
65 match(struct passwd *pw, const char *user)
70 if (!strcasecmp(pw->pw_name, user))
75 * Why do we skip asterisks!?!?
77 (void)strncpy(p = tbuf, pw->pw_gecos, sizeof(tbuf));
78 tbuf[sizeof(tbuf) - 1] = '\0';
82 /* Ampersands get replaced by the login name. */
83 if ((p = strtok(p, ",")) == NULL)
86 for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) {
88 (void)strncpy(t, pw->pw_name,
89 sizeof(name) - (t - name));
90 name[sizeof(name) - 1] = '\0';
91 while (t < &name[sizeof(name) - 1] && *++t)
98 for (t = name; (p = strtok(t, "\t ")) != NULL; t = NULL)
99 if (!strcasecmp(p, user))
105 enter_lastlog(PERSON *pn)
108 struct utmpx *ut = NULL;
111 if (setutxdb(UTXDB_LASTLOGIN, NULL) == 0)
112 ut = getutxuser(pn->name);
113 if ((w = pn->whead) == NULL)
115 else if (ut != NULL && ut->ut_type == USER_PROCESS) {
116 /* if last login is earlier than some current login */
117 for (; !doit && w != NULL; w = w->next)
118 if (w->info == LOGGEDIN &&
119 w->loginat < ut->ut_tv.tv_sec)
122 * and if it's not any of the current logins
123 * can't use time comparison because there may be a small
124 * discrepancy since login calls time() twice
126 for (w = pn->whead; doit && w != NULL; w = w->next)
127 if (w->info == LOGGEDIN &&
128 strcmp(w->tty, ut->ut_line) == 0)
131 if (ut != NULL && doit) {
134 strcpy(w->tty, ut->ut_line);
135 strcpy(w->host, ut->ut_host);
136 w->loginat = ut->ut_tv.tv_sec;
142 enter_where(struct utmpx *ut, PERSON *pn)
148 strcpy(w->tty, ut->ut_line);
149 strcpy(w->host, ut->ut_host);
150 w->loginat = ut->ut_tv.tv_sec;
151 find_idle_and_ttywrite(w);
155 enter_person(struct passwd *pw)
161 (db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL)) == NULL)
164 key.data = pw->pw_name;
165 key.size = strlen(pw->pw_name);
167 switch ((*db->get)(db, &key, &data, 0)) {
169 memmove(&pn, data.data, sizeof pn);
181 data.size = sizeof(PERSON *);
183 if ((*db->put)(db, &key, &data, 0))
190 find_person(char *name)
200 if ((pw = getpwnam(name)) && hide(pw))
204 key.size = strlen(name);
206 if ((*db->get)(db, &key, &data, 0))
208 memmove(&p, data.data, sizeof p);
217 if ((p = malloc(sizeof(PERSON))) == NULL)
227 if ((w = malloc(sizeof(WHERE))) == NULL)
229 if (pn->whead == NULL)
230 pn->whead = pn->wtail = w;
244 static char pbuf[20];
246 /* don't touch anything if the user has their own formatting */
247 for (p = num; *p; ++p)
253 case 11: /* +0-123-456-7890 */
258 case 10: /* 012-345-6789 */
264 case 7: /* 012-3456 */
269 case 5: /* x0-1234 */
289 find_idle_and_ttywrite(WHERE *w)
295 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_DEV, w->tty);
297 error = stat(tbuf, &sb);
298 if (error < 0 && errno == ENOENT) {
300 * The terminal listed is not actually a terminal (i.e.,
301 * ":0"). This is a failure, so we'll skip printing
302 * out the idle time, which is non-ideal but better
303 * than a bogus warning and idle time.
307 } else if (error < 0) {
312 touched = sb.st_atime;
313 if (touched < w->loginat) {
314 /* tty untouched since before login */
315 touched = w->loginat;
317 w->idletime = now < touched ? 0 : now - touched;
319 #define TALKABLE 0220 /* tty is writable if 220 mode */
320 w->writable = ((sb.st_mode & TALKABLE) == TALKABLE);
324 userinfo(PERSON *pn, struct passwd *pw)
327 char *bp, name[1024];
330 pn->realname = pn->office = pn->officephone = pn->homephone = NULL;
332 pn->uid = pw->pw_uid;
333 if ((pn->name = strdup(pw->pw_name)) == NULL)
334 err(1, "strdup failed");
335 if ((pn->dir = strdup(pw->pw_dir)) == NULL)
336 err(1, "strdup failed");
337 if ((pn->shell = strdup(pw->pw_shell)) == NULL)
338 err(1, "strdup failed");
340 /* why do we skip asterisks!?!? */
341 (void)strncpy(bp = tbuf, pw->pw_gecos, sizeof(tbuf));
342 tbuf[sizeof(tbuf) - 1] = '\0';
346 /* ampersands get replaced by the login name */
347 if (!(p = strsep(&bp, ",")))
349 for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) {
351 (void)strncpy(t, pw->pw_name,
352 sizeof(name) - (t - name));
353 name[sizeof(name) - 1] = '\0';
356 while (t < &name[sizeof(name) - 1] && *++t)
363 if ((pn->realname = strdup(name)) == NULL)
364 err(1, "strdup failed");
365 pn->office = ((p = strsep(&bp, ",")) && *p) ?
367 pn->officephone = ((p = strsep(&bp, ",")) && *p) ?
369 pn->homephone = ((p = strsep(&bp, ",")) && *p) ?
371 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pw->pw_name);
372 pn->mailrecv = -1; /* -1 == not_valid */
373 if (stat(tbuf, &sb) < 0) {
374 if (errno != ENOENT) {
378 } else if (sb.st_size != 0) {
379 pn->mailrecv = sb.st_mtime;
380 pn->mailread = sb.st_atime;
385 * Is this user hiding from finger?
386 * If ~<user>/.nofinger exists, return 1 (hide), else return 0 (nohide).
387 * Nobody can hide from root.
391 hide(struct passwd *pw)
394 char buf[MAXPATHLEN];
396 if (invoker_root || !pw->pw_dir)
399 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, _PATH_NOFINGER);
401 if (stat(buf, &st) == 0)