]>
git.cameronkatri.com Git - apple_cmds.git/blob - adv_cmds/finger/util.c
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: src/usr.bin/finger/util.c,v 1.22 2005/09/19 10:11:47 dds Exp $");
46 #include <sys/param.h>
47 #include <sys/socket.h>
62 #include "pathnames.h"
64 static void find_idle_and_ttywrite(WHERE
*);
65 static void userinfo(PERSON
*, struct passwd
*);
66 static WHERE
*walloc(PERSON
*);
69 match(struct passwd
*pw
, const char *user
)
74 if (!strcasecmp(pw
->pw_name
, user
))
79 * Why do we skip asterisks!?!?
81 (void)strncpy(p
= tbuf
, pw
->pw_gecos
, sizeof(tbuf
));
82 tbuf
[sizeof(tbuf
) - 1] = '\0';
86 /* Ampersands get replaced by the login name. */
87 if ((p
= strtok(p
, ",")) == NULL
)
90 for (t
= name
; t
< &name
[sizeof(name
) - 1] && (*t
= *p
) != '\0'; ++p
) {
92 (void)strncpy(t
, pw
->pw_name
,
93 sizeof(name
) - (t
- name
));
94 name
[sizeof(name
) - 1] = '\0';
95 while (t
< &name
[sizeof(name
) - 1] && *++t
)
102 for (t
= name
; (p
= strtok(t
, "\t ")) != NULL
; t
= NULL
)
103 if (!strcasecmp(p
, user
))
109 enter_lastlog(PERSON
*pn
)
112 struct lastlogx l
, *ll
;
115 if ((ll
= getlastlogxbyname(pn
->name
, &l
)) == NULL
) {
116 bzero(&l
, sizeof(l
));
119 if ((w
= pn
->whead
) == NULL
)
121 else if (ll
->ll_tv
.tv_sec
!= 0) {
122 /* if last login is earlier than some current login */
123 for (; !doit
&& w
!= NULL
; w
= w
->next
)
124 if (w
->info
== LOGGEDIN
&& w
->loginat
< ll
->ll_tv
.tv_sec
)
127 * and if it's not any of the current logins
128 * can't use time comparison because there may be a small
129 * discrepancy since login calls time() twice
131 for (w
= pn
->whead
; doit
&& w
!= NULL
; w
= w
->next
)
132 if (w
->info
== LOGGEDIN
&&
133 strncmp(w
->tty
, ll
->ll_line
, _UTX_LINESIZE
) == 0)
139 bcopy(ll
->ll_line
, w
->tty
, _UTX_LINESIZE
);
140 w
->tty
[_UTX_LINESIZE
] = 0;
141 bcopy(ll
->ll_host
, w
->host
, _UTX_HOSTSIZE
);
142 w
->host
[_UTX_HOSTSIZE
] = 0;
143 w
->loginat
= ll
->ll_tv
.tv_sec
;
148 enter_where(struct utmpx
*ut
, PERSON
*pn
)
154 bcopy(ut
->ut_line
, w
->tty
, _UTX_LINESIZE
);
155 w
->tty
[_UTX_LINESIZE
] = 0;
156 bcopy(ut
->ut_host
, w
->host
, _UTX_HOSTSIZE
);
157 w
->host
[_UTX_HOSTSIZE
] = 0;
158 w
->loginat
= (time_t)ut
->ut_tv
.tv_sec
;
159 find_idle_and_ttywrite(w
);
163 enter_person(struct passwd
*pw
)
169 (db
= dbopen(NULL
, O_RDWR
, 0, DB_BTREE
, NULL
)) == NULL
)
172 key
.data
= pw
->pw_name
;
173 key
.size
= strlen(pw
->pw_name
);
175 switch ((*db
->get
)(db
, &key
, &data
, 0)) {
177 memmove(&pn
, data
.data
, sizeof pn
);
189 data
.size
= sizeof(PERSON
*);
191 if ((*db
->put
)(db
, &key
, &data
, 0))
198 find_person(const char *name
)
205 char buf
[_UTX_USERSIZE
+ 1];
210 if ((pw
= getpwnam(name
)) && hide(pw
))
213 /* Name may be only _UTX_USERSIZE long and not NUL terminated. */
214 for (cnt
= 0; cnt
< _UTX_USERSIZE
&& *name
; ++name
, ++cnt
)
220 if ((*db
->get
)(db
, &key
, &data
, 0))
222 memmove(&p
, data
.data
, sizeof p
);
231 if ((p
= malloc(sizeof(PERSON
))) == NULL
)
241 if ((w
= malloc(sizeof(WHERE
))) == NULL
)
243 if (pn
->whead
== NULL
)
244 pn
->whead
= pn
->wtail
= w
;
258 static char pbuf
[20];
260 /* don't touch anything if the user has their own formatting */
261 for (p
= num
; *p
; ++p
)
267 case 11: /* +0-123-456-7890 */
272 case 10: /* 012-345-6789 */
278 case 7: /* 012-3456 */
283 case 5: /* x0-1234 */
303 find_idle_and_ttywrite(WHERE
*w
)
309 (void)snprintf(tbuf
, sizeof(tbuf
), "%s/%s", _PATH_DEV
, w
->tty
);
311 error
= stat(tbuf
, &sb
);
312 if (error
< 0 && errno
== ENOENT
) {
314 * The terminal listed is not actually a terminal (i.e.,
315 * ":0"). This is a failure, so we'll skip printing
316 * out the idle time, which is non-ideal but better
317 * than a bogus warning and idle time.
321 } else if (error
< 0) {
326 touched
= sb
.st_atime
;
327 if (touched
< w
->loginat
) {
328 /* tty untouched since before login */
329 touched
= w
->loginat
;
331 w
->idletime
= now
< touched
? 0 : now
- touched
;
333 #define TALKABLE 0220 /* tty is writable if 220 mode */
334 w
->writable
= ((sb
.st_mode
& TALKABLE
) == TALKABLE
);
338 userinfo(PERSON
*pn
, struct passwd
*pw
)
341 char *bp
, name
[1024];
344 pn
->realname
= pn
->office
= pn
->officephone
= pn
->homephone
= NULL
;
346 pn
->uid
= pw
->pw_uid
;
347 if ((pn
->name
= strdup(pw
->pw_name
)) == NULL
)
348 err(1, "strdup failed");
349 if ((pn
->dir
= strdup(pw
->pw_dir
)) == NULL
)
350 err(1, "strdup failed");
351 if ((pn
->shell
= strdup(pw
->pw_shell
)) == NULL
)
352 err(1, "strdup failed");
354 /* why do we skip asterisks!?!? */
355 (void)strncpy(bp
= tbuf
, pw
->pw_gecos
, sizeof(tbuf
));
356 tbuf
[sizeof(tbuf
) - 1] = '\0';
360 /* ampersands get replaced by the login name */
361 if (!(p
= strsep(&bp
, ",")))
363 for (t
= name
; t
< &name
[sizeof(name
) - 1] && (*t
= *p
) != '\0'; ++p
) {
365 (void)strncpy(t
, pw
->pw_name
,
366 sizeof(name
) - (t
- name
));
367 name
[sizeof(name
) - 1] = '\0';
370 while (t
< &name
[sizeof(name
) - 1] && *++t
)
377 if ((pn
->realname
= strdup(name
)) == NULL
)
378 err(1, "strdup failed");
379 pn
->office
= ((p
= strsep(&bp
, ",")) && *p
) ?
381 pn
->officephone
= ((p
= strsep(&bp
, ",")) && *p
) ?
383 pn
->homephone
= ((p
= strsep(&bp
, ",")) && *p
) ?
385 (void)snprintf(tbuf
, sizeof(tbuf
), "%s/%s", _PATH_MAILDIR
, pw
->pw_name
);
386 pn
->mailrecv
= -1; /* -1 == not_valid */
387 if (stat(tbuf
, &sb
) < 0) {
388 if (errno
!= ENOENT
) {
392 } else if (sb
.st_size
!= 0) {
393 pn
->mailrecv
= sb
.st_mtime
;
394 pn
->mailread
= sb
.st_atime
;
399 * Is this user hiding from finger?
400 * If ~<user>/.nofinger exists, return 1 (hide), else return 0 (nohide).
401 * Nobody can hide from root.
405 hide(struct passwd
*pw
)
408 char buf
[MAXPATHLEN
];
410 if (invoker_root
|| !pw
->pw_dir
)
413 snprintf(buf
, sizeof(buf
), "%s/%s", pw
->pw_dir
, _PATH_NOFINGER
);
415 if (stat(buf
, &st
) == 0)