xref: /netbsd-src/usr.bin/finger/util.c (revision eb961d0e02b7a46a9acfa877b02df48df6637278)
1 /*	$NetBSD: util.c,v 1.24 2006/01/04 01:17:54 perry Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Portions Copyright (c) 1983, 1995, 1996 Eric P. Allman
37  *
38  * This code is derived from software contributed to Berkeley by
39  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. All advertising materials mentioning features or use of this software
50  *    must display the following acknowledgement:
51  *	This product includes software developed by the University of
52  *	California, Berkeley and its contributors.
53  * 4. Neither the name of the University nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  */
69 
70 #include <sys/cdefs.h>
71 #ifndef lint
72 #if 0
73 static char sccsid[] = "@(#)util.c	8.3 (Berkeley) 4/28/95";
74 #else
75 __RCSID("$NetBSD: util.c,v 1.24 2006/01/04 01:17:54 perry Exp $");
76 #endif
77 #endif /* not lint */
78 
79 #include <sys/param.h>
80 #include <sys/stat.h>
81 
82 #include <db.h>
83 #include <ctype.h>
84 #include <err.h>
85 #include <errno.h>
86 #include <fcntl.h>
87 #include <paths.h>
88 #include <pwd.h>
89 #include <stdio.h>
90 #include <stdlib.h>
91 #include <string.h>
92 #include <unistd.h>
93 #include <utmp.h>
94 
95 #include "utmpentry.h"
96 
97 #include "finger.h"
98 #include "extern.h"
99 
100 static void	 find_idle_and_ttywrite(WHERE *);
101 static void	 userinfo(PERSON *, struct passwd *);
102 static WHERE	*walloc(PERSON *);
103 
104 int
105 match(struct passwd *pw, char *user)
106 {
107 	char *p;
108 	char *bp, name[1024];
109 
110 	if (!strcasecmp(pw->pw_name, user))
111 		return(1);
112 
113 	(void)strlcpy(bp = tbuf, pw->pw_gecos, sizeof(tbuf));
114 
115 	/* Ampersands get replaced by the login name. */
116 	if (!(p = strsep(&bp, ",")))
117 		return(0);
118 
119 	expandusername(p, pw->pw_name, name, sizeof(name));
120 	bp = name;
121 	while ((p = strsep(&bp, "\t ")))
122 		if (!strcasecmp(p, user))
123 			return(1);
124 	return(0);
125 }
126 
127 /* inspired by usr.sbin/sendmail/util.c::buildfname */
128 void
129 expandusername(const char *gecos, const char *login, char *buf, int buflen)
130 {
131 	const char *p;
132 	char *bp;
133 
134 	/* why do we skip asterisks!?!? */
135 	if (*gecos == '*')
136 		gecos++;
137 	bp = buf;
138 
139 	/* copy gecos, interpolating & to be full name */
140 	for (p = gecos; *p != '\0'; p++) {
141 		if (bp >= &buf[buflen - 1]) {
142 			/* buffer overflow - just use login name */
143 			snprintf(buf, buflen, "%s", login);
144 			buf[buflen - 1] = '\0';
145 			return;
146 		}
147 		if (*p == '&') {
148 			/* interpolate full name */
149 			snprintf(bp, buflen - (bp - buf), "%s", login);
150 			*bp = toupper((unsigned char)*bp);
151 			bp += strlen(bp);
152 		}
153 		else
154 			*bp++ = *p;
155 	}
156 	*bp = '\0';
157 }
158 
159 void
160 enter_lastlog(PERSON *pn)
161 {
162 	WHERE *w;
163 	static int opened, fd;
164 	struct lastlog ll;
165 	char doit = 0;
166 
167 	/* some systems may not maintain lastlog, don't report errors. */
168 	if (!opened) {
169 		fd = open(_PATH_LASTLOG, O_RDONLY, 0);
170 		opened = 1;
171 	}
172 	if (fd == -1 ||
173 	    lseek(fd, (off_t)pn->uid * sizeof(ll), SEEK_SET) !=
174 	    (long)pn->uid * sizeof(ll) ||
175 	    read(fd, (char *)&ll, sizeof(ll)) != sizeof(ll)) {
176 			/* as if never logged in */
177 			ll.ll_line[0] = ll.ll_host[0] = '\0';
178 			ll.ll_time = 0;
179 		}
180 	if ((w = pn->whead) == NULL)
181 		doit = 1;
182 	else if (ll.ll_time != 0) {
183 		/* if last login is earlier than some current login */
184 		for (; !doit && w != NULL; w = w->next)
185 			if (w->info == LOGGEDIN && w->loginat < ll.ll_time)
186 				doit = 1;
187 		/*
188 		 * and if it's not any of the current logins
189 		 * can't use time comparison because there may be a small
190 		 * discrepency since login calls time() twice
191 		 */
192 		for (w = pn->whead; doit && w != NULL; w = w->next)
193 			if (w->info == LOGGEDIN &&
194 			    strncmp(w->tty, ll.ll_line, UT_LINESIZE) == 0)
195 				doit = 0;
196 	}
197 	if (doit) {
198 		w = walloc(pn);
199 		w->info = LASTLOG;
200 		memcpy(w->tty = malloc(UT_LINESIZE + 1),
201 		    ll.ll_line, UT_LINESIZE);
202 		w->tty[UT_LINESIZE + 1] = '\0';
203 		memcpy(w->host = malloc(UT_HOSTSIZE + 1),
204 		    ll.ll_host, UT_HOSTSIZE);
205 		w->host[UT_HOSTSIZE + 1] = '\0';
206 		w->loginat = ll.ll_time;
207 	}
208 }
209 
210 void
211 enter_where(struct utmpentry *ep, PERSON *pn)
212 {
213 	WHERE *w = walloc(pn);
214 
215 	w->info = LOGGEDIN;
216 	w->tty = ep->line;
217 	w->host = ep->host;
218 	w->loginat = (time_t)ep->tv.tv_sec;
219 	find_idle_and_ttywrite(w);
220 }
221 
222 PERSON *
223 enter_person(struct passwd *pw)
224 {
225 	DBT data, key;
226 	PERSON *pn;
227 
228 	if (db == NULL &&
229 	    (db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL)) == NULL)
230 		err(1, NULL);
231 
232 	key.data = (char *)pw->pw_name;
233 	key.size = strlen(pw->pw_name);
234 
235 	switch ((*db->get)(db, &key, &data, 0)) {
236 	case 0:
237 		memmove(&pn, data.data, sizeof pn);
238 		return (pn);
239 	default:
240 	case -1:
241 		err(1, "db get");
242 		/* NOTREACHED */
243 	case 1:
244 		++entries;
245 		pn = palloc();
246 		userinfo(pn, pw);
247 		pn->whead = NULL;
248 
249 		data.size = sizeof(PERSON *);
250 		data.data = &pn;
251 		if ((*db->put)(db, &key, &data, 0))
252 			err(1, "db put");
253 		return (pn);
254 	}
255 }
256 
257 PERSON *
258 find_person(char *name)
259 {
260 	DBT data, key;
261 	PERSON *p;
262 
263 	if (!db)
264 		return(NULL);
265 
266 	key.data = name;
267 	key.size = strlen(name);
268 
269 	if ((*db->get)(db, &key, &data, 0))
270 		return (NULL);
271 	memmove(&p, data.data, sizeof p);
272 	return (p);
273 }
274 
275 PERSON *
276 palloc(void)
277 {
278 	PERSON *p;
279 
280 	if ((p = malloc((u_int) sizeof(PERSON))) == NULL)
281 		err(1, NULL);
282 	return(p);
283 }
284 
285 static WHERE *
286 walloc(PERSON *pn)
287 {
288 	WHERE *w;
289 
290 	if ((w = malloc((u_int) sizeof(WHERE))) == NULL)
291 		err(1, NULL);
292 	if (pn->whead == NULL)
293 		pn->whead = pn->wtail = w;
294 	else {
295 		pn->wtail->next = w;
296 		pn->wtail = w;
297 	}
298 	w->next = NULL;
299 	return(w);
300 }
301 
302 char *
303 prphone(char *num)
304 {
305 	char *p;
306 	int len;
307 	static char pbuf[15];
308 
309 	/* don't touch anything if the user has their own formatting */
310 	for (p = num; *p; ++p)
311 		if (!isdigit((unsigned char)*p))
312 			return(num);
313 	len = p - num;
314 	p = pbuf;
315 	switch(len) {
316 	case 11:			/* +0-123-456-7890 */
317 		*p++ = '+';
318 		*p++ = *num++;
319 		*p++ = '-';
320 		/* FALLTHROUGH */
321 	case 10:			/* 012-345-6789 */
322 		*p++ = *num++;
323 		*p++ = *num++;
324 		*p++ = *num++;
325 		*p++ = '-';
326 		/* FALLTHROUGH */
327 	case 7:				/* 012-3456 */
328 		*p++ = *num++;
329 		*p++ = *num++;
330 		*p++ = *num++;
331 		break;
332 	case 5:				/* x0-1234 */
333 	case 4:				/* x1234 */
334 		*p++ = 'x';
335 		*p++ = *num++;
336 		break;
337 	default:
338 		return(num);
339 	}
340 	if (len != 4) {
341 		*p++ = '-';
342 		*p++ = *num++;
343 	}
344 	*p++ = *num++;
345 	*p++ = *num++;
346 	*p++ = *num++;
347 	*p = '\0';
348 	return(pbuf);
349 }
350 
351 static void
352 find_idle_and_ttywrite(WHERE *w)
353 {
354 	struct stat sb;
355 
356 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_DEV, w->tty);
357 	if (stat(tbuf, &sb) < 0) {
358 		warn("%s", tbuf);
359 		return;
360 	}
361 	w->idletime = now < sb.st_atime ? 0 : now - sb.st_atime;
362 
363 #define	TALKABLE	0220		/* tty is writable if 220 mode */
364 	w->writable = ((sb.st_mode & TALKABLE) == TALKABLE);
365 }
366 
367 static void
368 userinfo(PERSON *pn, struct passwd *pw)
369 {
370 	char *p;
371 	char *bp, name[1024];
372 	struct stat sb;
373 
374 	pn->realname = pn->office = pn->officephone = pn->homephone = NULL;
375 
376 	pn->uid = pw->pw_uid;
377 	pn->name = strdup(pw->pw_name);
378 	pn->dir = strdup(pw->pw_dir);
379 	pn->shell = strdup(pw->pw_shell);
380 
381 	(void)strlcpy(bp = tbuf, pw->pw_gecos, sizeof(tbuf));
382 
383 	/* ampersands get replaced by the login name */
384 	if (!(p = strsep(&bp, ",")))
385 		return;
386 	expandusername(p, pw->pw_name, name, sizeof(name));
387 	pn->realname = strdup(name);
388 	pn->office = ((p = strsep(&bp, ",")) && *p) ?
389 	    strdup(p) : NULL;
390 	pn->officephone = ((p = strsep(&bp, ",")) && *p) ?
391 	    strdup(p) : NULL;
392 	pn->homephone = ((p = strsep(&bp, ",")) && *p) ?
393 	    strdup(p) : NULL;
394 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILSPOOL,
395 	    pw->pw_name);
396 	pn->mailrecv = -1;		/* -1 == not_valid */
397 	if (stat(tbuf, &sb) < 0) {
398 		if (errno != ENOENT) {
399 			(void)fprintf(stderr,
400 			    "finger: %s: %s\n", tbuf, strerror(errno));
401 			return;
402 		}
403 	} else if (sb.st_size != 0) {
404 		pn->mailrecv = sb.st_mtime;
405 		pn->mailread = sb.st_atime;
406 	}
407 }
408