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 $
36 #include <sys/param.h>
37 #include <sys/socket.h>
50 #include "utmpentry.h"
53 #include "pathnames.h"
55 static void find_idle_and_ttywrite(WHERE
*);
56 static void userinfo(PERSON
*, struct passwd
*);
57 static WHERE
*walloc(PERSON
*);
60 match(struct passwd
*pw
, char *user
)
65 if (!strcasecmp(pw
->pw_name
, user
))
70 * Why do we skip asterisks!?!?
72 (void)strncpy(p
= tbuf
, pw
->pw_gecos
, sizeof(tbuf
));
73 tbuf
[sizeof(tbuf
) - 1] = '\0';
77 /* Ampersands get replaced by the login name. */
78 if ((p
= strtok(p
, ",")) == NULL
)
81 for (t
= name
; t
< &name
[sizeof(name
) - 1] && (*t
= *p
) != '\0'; ++p
) {
83 (void)strncpy(t
, pw
->pw_name
,
84 sizeof(name
) - (t
- name
));
85 name
[sizeof(name
) - 1] = '\0';
86 while (t
< &name
[sizeof(name
) - 1] && *++t
)
93 for (t
= name
; (p
= strtok(t
, "\t ")) != NULL
; t
= NULL
)
94 if (!strcasecmp(p
, user
))
100 enter_lastlog(PERSON
*pn
)
106 memset(&ll
, 0, sizeof(ll
));
107 getlastlogx(_PATH_LASTLOGX
, pn
->uid
, &ll
);
108 if ((w
= pn
->whead
) == NULL
)
111 /* if last login is earlier than some current login */
112 for (; !doit
&& w
!= NULL
; w
= w
->next
)
113 if (w
->info
== LOGGEDIN
&&
114 w
->loginat
< ll
.ll_tv
.tv_sec
)
117 * and if it's not any of the current logins
118 * can't use time comparison because there may be a small
119 * discrepancy since login calls time() twice
121 for (w
= pn
->whead
; doit
&& w
!= NULL
; w
= w
->next
)
122 if (w
->info
== LOGGEDIN
&&
123 strcmp(w
->tty
, ll
.ll_line
) == 0)
129 strlcpy(w
->tty
, ll
.ll_line
, sizeof(w
->tty
));
130 strlcpy(w
->host
, ll
.ll_host
, sizeof(w
->host
));
131 w
->host
[_UTX_HOSTSIZE
] = '\0';
132 w
->loginat
= ll
.ll_tv
.tv_sec
;
137 enter_where(struct utmpentry
*ep
, PERSON
*pn
)
143 strlcpy(w
->tty
, ep
->line
, sizeof(w
->tty
));
144 strlcpy(w
->host
, ep
->host
, sizeof(w
->host
));
145 w
->loginat
= (time_t)ep
->tv
.tv_sec
;
146 find_idle_and_ttywrite(w
);
150 enter_person(struct passwd
*pw
)
156 (db
= dbopen(NULL
, O_RDWR
, 0, DB_BTREE
, NULL
)) == NULL
)
159 key
.data
= pw
->pw_name
;
160 key
.size
= strlen(pw
->pw_name
);
162 switch ((*db
->get
)(db
, &key
, &data
, 0)) {
164 memmove(&pn
, data
.data
, sizeof pn
);
176 data
.size
= sizeof(PERSON
*);
178 if ((*db
->put
)(db
, &key
, &data
, 0))
185 find_person(char *name
)
195 if ((pw
= getpwnam(name
)) && hide(pw
))
199 key
.size
= strlen(name
);
201 if ((*db
->get
)(db
, &key
, &data
, 0))
203 memmove(&p
, data
.data
, sizeof p
);
212 if ((p
= malloc(sizeof(PERSON
))) == NULL
)
222 if ((w
= malloc(sizeof(WHERE
))) == NULL
)
224 bzero(w
, sizeof(WHERE
));
225 if (pn
->whead
== NULL
) {
226 pn
->whead
= pn
->wtail
= w
;
240 static char pbuf
[20];
242 /* don't touch anything if the user has their own formatting */
243 for (p
= num
; *p
; ++p
)
249 case 11: /* +0-123-456-7890 */
254 case 10: /* 012-345-6789 */
260 case 7: /* 012-3456 */
265 case 5: /* x0-1234 */
285 find_idle_and_ttywrite(WHERE
*w
)
290 (void)snprintf(tbuf
, sizeof(tbuf
), "%s/%s", _PATH_DEV
, w
->tty
);
291 if (stat(tbuf
, &sb
) < 0) {
295 touched
= sb
.st_atime
;
296 if (touched
< w
->loginat
) {
297 /* tty untouched since before login */
298 touched
= w
->loginat
;
300 w
->idletime
= now
< touched
? 0 : now
- touched
;
302 #define TALKABLE 0220 /* tty is writable if 220 mode */
303 w
->writable
= ((sb
.st_mode
& TALKABLE
) == TALKABLE
);
307 userinfo(PERSON
*pn
, struct passwd
*pw
)
310 char *bp
, name
[1024];
313 pn
->realname
= pn
->office
= pn
->officephone
= pn
->homephone
= NULL
;
315 pn
->uid
= pw
->pw_uid
;
316 if ((pn
->name
= strdup(pw
->pw_name
)) == NULL
)
317 err(1, "strdup failed");
318 if ((pn
->dir
= strdup(pw
->pw_dir
)) == NULL
)
319 err(1, "strdup failed");
320 if ((pn
->shell
= strdup(pw
->pw_shell
)) == NULL
)
321 err(1, "strdup failed");
323 /* why do we skip asterisks!?!? */
324 (void)strncpy(bp
= tbuf
, pw
->pw_gecos
, sizeof(tbuf
));
325 tbuf
[sizeof(tbuf
) - 1] = '\0';
329 /* ampersands get replaced by the login name */
330 if (!(p
= strsep(&bp
, ",")))
332 for (t
= name
; t
< &name
[sizeof(name
) - 1] && (*t
= *p
) != '\0'; ++p
) {
334 (void)strncpy(t
, pw
->pw_name
,
335 sizeof(name
) - (t
- name
));
336 name
[sizeof(name
) - 1] = '\0';
339 while (t
< &name
[sizeof(name
) - 1] && *++t
)
346 if ((pn
->realname
= strdup(name
)) == NULL
)
347 err(1, "strdup failed");
348 pn
->office
= ((p
= strsep(&bp
, ",")) && *p
) ?
350 pn
->officephone
= ((p
= strsep(&bp
, ",")) && *p
) ?
352 pn
->homephone
= ((p
= strsep(&bp
, ",")) && *p
) ?
354 (void)snprintf(tbuf
, sizeof(tbuf
), "%s/%s", _PATH_MAILDIR
, pw
->pw_name
);
355 pn
->mailrecv
= -1; /* -1 == not_valid */
356 if (stat(tbuf
, &sb
) < 0) {
357 if (errno
!= ENOENT
) {
361 } else if (sb
.st_size
!= 0) {
362 pn
->mailrecv
= sb
.st_mtime
;
363 pn
->mailread
= sb
.st_atime
;
368 * Is this user hiding from finger?
369 * If ~<user>/.nofinger exists, return 1 (hide), else return 0 (nohide).
373 hide(struct passwd
*pw
)
376 char buf
[MAXPATHLEN
];
381 snprintf(buf
, sizeof(buf
), "%s/%s", pw
->pw_dir
, _PATH_NOFINGER
);
383 if (stat(buf
, &st
) == 0)