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 * 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.
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
39 static char sccsid[] = "@(#)util.c 8.3 (Berkeley) 4/28/95";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
61 #include "pathnames.h"
63 static void find_idle_and_ttywrite(WHERE *);
64 static void userinfo(PERSON *, struct passwd *);
65 static WHERE *walloc(PERSON *);
75 if (!strcasecmp(pw->pw_name, user))
80 * Why do we skip asterisks!?!?
82 (void)strncpy(p = tbuf, pw->pw_gecos, sizeof(tbuf));
83 tbuf[sizeof(tbuf) - 1] = '\0';
87 /* Ampersands get replaced by the login name. */
88 if ((p = strtok(p, ",")) == NULL)
91 for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) {
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)
103 for (t = name; (p = strtok(t, "\t ")) != NULL; t = NULL)
104 if (!strcasecmp(p, user))
114 static int opened, fd;
118 /* some systems may not maintain lastlog, don't report errors. */
120 fd = open(_PATH_LASTLOG, O_RDONLY, 0);
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';
131 if ((w = pn->whead) == NULL)
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)
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
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)
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;
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);
184 (db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL)) == NULL)
187 key.data = pw->pw_name;
188 key.size = strlen(pw->pw_name);
190 switch ((*db->get)(db, &key, &data, 0)) {
192 memmove(&pn, data.data, sizeof pn);
204 data.size = sizeof(PERSON *);
206 if ((*db->put)(db, &key, &data, 0))
221 char buf[UT_NAMESIZE + 1];
226 if ((pw = getpwnam(name)) && hide(pw))
229 /* Name may be only UT_NAMESIZE long and not NUL terminated. */
230 for (cnt = 0; cnt < UT_NAMESIZE && *name; ++name, ++cnt)
236 if ((*db->get)(db, &key, &data, 0))
238 memmove(&p, data.data, sizeof p);
247 if ((p = malloc(sizeof(PERSON))) == NULL)
258 if ((w = malloc(sizeof(WHERE))) == NULL)
260 if (pn->whead == NULL)
261 pn->whead = pn->wtail = w;
276 static char pbuf[20];
278 /* don't touch anything if the user has their own formatting */
279 for (p = num; *p; ++p)
285 case 11: /* +0-123-456-7890 */
290 case 10: /* 012-345-6789 */
296 case 7: /* 012-3456 */
301 case 5: /* x0-1234 */
321 find_idle_and_ttywrite(w)
328 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_DEV, w->tty);
329 if (stat(tbuf, &sb) < 0) {
333 touched = sb.st_atime;
334 if (touched < w->loginat) {
335 /* tty untouched since before login */
336 touched = w->loginat;
338 w->idletime = now < touched ? 0 : now - touched;
340 #define TALKABLE 0220 /* tty is writable if 220 mode */
341 w->writable = ((sb.st_mode & TALKABLE) == TALKABLE);
350 char *bp, name[1024];
353 pn->realname = pn->office = pn->officephone = pn->homephone = NULL;
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");
363 /* why do we skip asterisks!?!? */
364 (void)strncpy(bp = tbuf, pw->pw_gecos, sizeof(tbuf));
365 tbuf[sizeof(tbuf) - 1] = '\0';
369 /* ampersands get replaced by the login name */
370 if (!(p = strsep(&bp, ",")))
372 for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) {
374 (void)strncpy(t, pw->pw_name,
375 sizeof(name) - (t - name));
376 name[sizeof(name) - 1] = '\0';
379 while (t < &name[sizeof(name) - 1] && *++t)
386 if ((pn->realname = strdup(name)) == NULL)
387 err(1, "strdup failed");
388 pn->office = ((p = strsep(&bp, ",")) && *p) ?
390 pn->officephone = ((p = strsep(&bp, ",")) && *p) ?
392 pn->homephone = ((p = strsep(&bp, ",")) && *p) ?
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) {
401 } else if (sb.st_size != 0) {
402 pn->mailrecv = sb.st_mtime;
403 pn->mailread = sb.st_atime;
408 * Is this user hiding from finger?
409 * If ~<user>/.nofinger exists, return 1 (hide), else return 0 (nohide).
417 char buf[MAXPATHLEN];
422 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, _PATH_NOFINGER);
424 if (stat(buf, &st) == 0)