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