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. 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
32 * @(#)util.c 8.3 (Berkeley) 4/28/95
33 * $FreeBSD: src/usr.bin/finger/util.c,v 1.8.2.6 2002/07/03 01:14:24 des Exp $
34 * $DragonFly: src/usr.bin/finger/util.c,v 1.3 2003/10/04 20:36:44 hmp Exp $
37 #include <sys/param.h>
38 #include <sys/socket.h>
51 #include "utmpentry.h"
54 #include "pathnames.h"
56 static void find_idle_and_ttywrite(WHERE
*);
57 static void userinfo(PERSON
*, struct passwd
*);
58 static WHERE
*walloc(PERSON
*);
61 match(struct passwd
*pw
, char *user
)
66 if (!strcasecmp(pw
->pw_name
, user
))
71 * Why do we skip asterisks!?!?
73 (void)strncpy(p
= tbuf
, pw
->pw_gecos
, sizeof(tbuf
));
74 tbuf
[sizeof(tbuf
) - 1] = '\0';
78 /* Ampersands get replaced by the login name. */
79 if ((p
= strtok(p
, ",")) == NULL
)
82 for (t
= name
; t
< &name
[sizeof(name
) - 1] && (*t
= *p
) != '\0'; ++p
) {
84 (void)strncpy(t
, pw
->pw_name
,
85 sizeof(name
) - (t
- name
));
86 name
[sizeof(name
) - 1] = '\0';
87 while (t
< &name
[sizeof(name
) - 1] && *++t
)
94 for (t
= name
; (p
= strtok(t
, "\t ")) != NULL
; t
= NULL
)
95 if (!strcasecmp(p
, user
))
101 enter_lastlog(PERSON
*pn
)
104 static int opened
, fd
;
108 /* some systems may not maintain lastlog, don't report errors. */
110 fd
= open(_PATH_LASTLOG
, O_RDONLY
, 0);
114 lseek(fd
, (long)pn
->uid
* sizeof(ll
), SEEK_SET
) !=
115 (off_t
)((long)pn
->uid
* sizeof(ll
)) ||
116 read(fd
, (char *)&ll
, sizeof(ll
)) != sizeof(ll
)) {
117 /* as if never logged in */
118 ll
.ll_line
[0] = ll
.ll_host
[0] = '\0';
121 if ((w
= pn
->whead
) == NULL
)
123 else if (ll
.ll_time
!= 0) {
124 /* if last login is earlier than some current login */
125 for (; !doit
&& w
!= NULL
; w
= w
->next
)
126 if (w
->info
== LOGGEDIN
&& w
->loginat
< ll
.ll_time
)
129 * and if it's not any of the current logins
130 * can't use time comparison because there may be a small
131 * discrepancy since login calls time() twice
133 for (w
= pn
->whead
; doit
&& w
!= NULL
; w
= w
->next
)
134 if (w
->info
== LOGGEDIN
&&
135 strncmp(w
->tty
, ll
.ll_line
, UT_LINESIZE
) == 0)
141 asprintf(&w
->tty
, "%s", ll
.ll_line
);
142 asprintf(&w
->host
, "%s", ll
.ll_host
);
143 w
->loginat
= ll
.ll_time
;
148 enter_where(struct utmpentry
*ep
, PERSON
*pn
)
156 w
->loginat
= (time_t)ep
->tv
.tv_sec
;
157 find_idle_and_ttywrite(w
);
161 enter_person(struct passwd
*pw
)
167 (db
= dbopen(NULL
, O_RDWR
, 0, DB_BTREE
, NULL
)) == NULL
)
170 key
.data
= pw
->pw_name
;
171 key
.size
= strlen(pw
->pw_name
);
173 switch ((*db
->get
)(db
, &key
, &data
, 0)) {
175 memmove(&pn
, data
.data
, sizeof pn
);
187 data
.size
= sizeof(PERSON
*);
189 if ((*db
->put
)(db
, &key
, &data
, 0))
196 find_person(char *name
)
203 char buf
[UT_NAMESIZE
+ 1];
208 if ((pw
= getpwnam(name
)) && hide(pw
))
211 /* Name may be only UT_NAMESIZE long and not NUL terminated. */
212 for (cnt
= 0; cnt
< UT_NAMESIZE
&& *name
; ++name
, ++cnt
)
218 if ((*db
->get
)(db
, &key
, &data
, 0))
220 memmove(&p
, data
.data
, sizeof p
);
229 if ((p
= malloc(sizeof(PERSON
))) == NULL
)
239 if ((w
= malloc(sizeof(WHERE
))) == NULL
)
241 bzero(w
, sizeof(WHERE
));
242 if (pn
->whead
== NULL
) {
243 pn
->whead
= pn
->wtail
= w
;
257 static char pbuf
[20];
259 /* don't touch anything if the user has their own formatting */
260 for (p
= num
; *p
; ++p
)
266 case 11: /* +0-123-456-7890 */
271 case 10: /* 012-345-6789 */
277 case 7: /* 012-3456 */
282 case 5: /* x0-1234 */
302 find_idle_and_ttywrite(WHERE
*w
)
307 (void)snprintf(tbuf
, sizeof(tbuf
), "%s/%s", _PATH_DEV
, w
->tty
);
308 if (stat(tbuf
, &sb
) < 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).
390 hide(struct passwd
*pw
)
393 char buf
[MAXPATHLEN
];
398 snprintf(buf
, sizeof(buf
), "%s/%s", pw
->pw_dir
, _PATH_NOFINGER
);
400 if (stat(buf
, &st
) == 0)