xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 68500)
122717Sdist /*
242833Sbostic  * Copyright (c) 1983 Eric P. Allman
363589Sbostic  * Copyright (c) 1988, 1993
463589Sbostic  *	The Regents of the University of California.  All rights reserved.
533731Sbostic  *
642833Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822717Sdist 
922717Sdist #ifndef lint
10*68500Seric static char sccsid[] = "@(#)util.c	8.56 (Berkeley) 03/07/95";
1133731Sbostic #endif /* not lint */
1222717Sdist 
1358332Seric # include "sendmail.h"
14298Seric # include <sysexits.h>
1557135Seric /*
16298Seric **  STRIPQUOTES -- Strip quotes & quote bits from a string.
17298Seric **
18298Seric **	Runs through a string and strips off unquoted quote
19298Seric **	characters and quote bits.  This is done in place.
20298Seric **
21298Seric **	Parameters:
22298Seric **		s -- the string to strip.
23298Seric **
24298Seric **	Returns:
25298Seric **		none.
26298Seric **
27298Seric **	Side Effects:
28298Seric **		none.
29298Seric **
30298Seric **	Called By:
31298Seric **		deliver
32298Seric */
33298Seric 
3454983Seric stripquotes(s)
35298Seric 	char *s;
36298Seric {
37298Seric 	register char *p;
38298Seric 	register char *q;
39298Seric 	register char c;
40298Seric 
414101Seric 	if (s == NULL)
424101Seric 		return;
434101Seric 
4454983Seric 	p = q = s;
4554983Seric 	do
46298Seric 	{
4754983Seric 		c = *p++;
4854983Seric 		if (c == '\\')
4954983Seric 			c = *p++;
5054983Seric 		else if (c == '"')
5154983Seric 			continue;
5254983Seric 		*q++ = c;
5354983Seric 	} while (c != '\0');
54298Seric }
55298Seric /*
56298Seric **  XALLOC -- Allocate memory and bitch wildly on failure.
57298Seric **
58298Seric **	THIS IS A CLUDGE.  This should be made to give a proper
59298Seric **	error -- but after all, what can we do?
60298Seric **
61298Seric **	Parameters:
62298Seric **		sz -- size of area to allocate.
63298Seric **
64298Seric **	Returns:
65298Seric **		pointer to data region.
66298Seric **
67298Seric **	Side Effects:
68298Seric **		Memory is allocated.
69298Seric */
70298Seric 
71298Seric char *
72298Seric xalloc(sz)
737007Seric 	register int sz;
74298Seric {
75298Seric 	register char *p;
76298Seric 
7766747Seric 	/* some systems can't handle size zero mallocs */
7866747Seric 	if (sz <= 0)
7966747Seric 		sz = 1;
8066747Seric 
8123121Seric 	p = malloc((unsigned) sz);
82298Seric 	if (p == NULL)
83298Seric 	{
84298Seric 		syserr("Out of memory!!");
8510685Seric 		abort();
8610685Seric 		/* exit(EX_UNAVAILABLE); */
87298Seric 	}
88298Seric 	return (p);
89298Seric }
90298Seric /*
913151Seric **  COPYPLIST -- copy list of pointers.
923151Seric **
933151Seric **	This routine is the equivalent of newstr for lists of
943151Seric **	pointers.
953151Seric **
963151Seric **	Parameters:
973151Seric **		list -- list of pointers to copy.
983151Seric **			Must be NULL terminated.
993151Seric **		copycont -- if TRUE, copy the contents of the vector
1003151Seric **			(which must be a string) also.
1013151Seric **
1023151Seric **	Returns:
1033151Seric **		a copy of 'list'.
1043151Seric **
1053151Seric **	Side Effects:
1063151Seric **		none.
1073151Seric */
1083151Seric 
1093151Seric char **
1103151Seric copyplist(list, copycont)
1113151Seric 	char **list;
1123151Seric 	bool copycont;
1133151Seric {
1143151Seric 	register char **vp;
1153151Seric 	register char **newvp;
1163151Seric 
1173151Seric 	for (vp = list; *vp != NULL; vp++)
1183151Seric 		continue;
1193151Seric 
1203151Seric 	vp++;
1213151Seric 
12216897Seric 	newvp = (char **) xalloc((int) (vp - list) * sizeof *vp);
12316897Seric 	bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp);
1243151Seric 
1253151Seric 	if (copycont)
1263151Seric 	{
1273151Seric 		for (vp = newvp; *vp != NULL; vp++)
1283151Seric 			*vp = newstr(*vp);
1293151Seric 	}
1303151Seric 
1313151Seric 	return (newvp);
1323151Seric }
1333151Seric /*
13458170Seric **  COPYQUEUE -- copy address queue.
13558170Seric **
13658170Seric **	This routine is the equivalent of newstr for address queues
13758170Seric **	addresses marked with QDONTSEND aren't copied
13858170Seric **
13958170Seric **	Parameters:
14058170Seric **		addr -- list of address structures to copy.
14158170Seric **
14258170Seric **	Returns:
14358170Seric **		a copy of 'addr'.
14458170Seric **
14558170Seric **	Side Effects:
14658170Seric **		none.
14758170Seric */
14858170Seric 
14958170Seric ADDRESS *
15058170Seric copyqueue(addr)
15158170Seric 	ADDRESS *addr;
15258170Seric {
15358170Seric 	register ADDRESS *newaddr;
15458170Seric 	ADDRESS *ret;
15558170Seric 	register ADDRESS **tail = &ret;
15658170Seric 
15758170Seric 	while (addr != NULL)
15858170Seric 	{
15958170Seric 		if (!bitset(QDONTSEND, addr->q_flags))
16058170Seric 		{
16158170Seric 			newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS));
16258170Seric 			STRUCTCOPY(*addr, *newaddr);
16358170Seric 			*tail = newaddr;
16458170Seric 			tail = &newaddr->q_next;
16558170Seric 		}
16658170Seric 		addr = addr->q_next;
16758170Seric 	}
16858170Seric 	*tail = NULL;
16958170Seric 
17058170Seric 	return ret;
17158170Seric }
17258170Seric /*
1733151Seric **  PRINTAV -- print argument vector.
1743151Seric **
1753151Seric **	Parameters:
1763151Seric **		av -- argument vector.
1773151Seric **
1783151Seric **	Returns:
1793151Seric **		none.
1803151Seric **
1813151Seric **	Side Effects:
1823151Seric **		prints av.
1833151Seric */
1843151Seric 
1853151Seric printav(av)
1863151Seric 	register char **av;
1873151Seric {
1883151Seric 	while (*av != NULL)
1893151Seric 	{
1908063Seric 		if (tTd(0, 44))
1918063Seric 			printf("\n\t%08x=", *av);
1928063Seric 		else
19323105Seric 			(void) putchar(' ');
1943151Seric 		xputs(*av++);
1953151Seric 	}
19623105Seric 	(void) putchar('\n');
1973151Seric }
1983151Seric /*
1993151Seric **  LOWER -- turn letter into lower case.
2003151Seric **
2013151Seric **	Parameters:
2023151Seric **		c -- character to turn into lower case.
2033151Seric **
2043151Seric **	Returns:
2053151Seric **		c, in lower case.
2063151Seric **
2073151Seric **	Side Effects:
2083151Seric **		none.
2093151Seric */
2103151Seric 
2113151Seric char
2123151Seric lower(c)
2133151Seric 	register char c;
2143151Seric {
21558050Seric 	return((isascii(c) && isupper(c)) ? tolower(c) : c);
2163151Seric }
2173151Seric /*
2183151Seric **  XPUTS -- put string doing control escapes.
2193151Seric **
2203151Seric **	Parameters:
2213151Seric **		s -- string to put.
2223151Seric **
2233151Seric **	Returns:
2243151Seric **		none.
2253151Seric **
2263151Seric **	Side Effects:
2273151Seric **		output to stdout
2283151Seric */
2293151Seric 
2303151Seric xputs(s)
2313151Seric 	register char *s;
2323151Seric {
23358050Seric 	register int c;
23451781Seric 	register struct metamac *mp;
23551781Seric 	extern struct metamac MetaMacros[];
2363151Seric 
2378055Seric 	if (s == NULL)
2388055Seric 	{
2398055Seric 		printf("<null>");
2408055Seric 		return;
2418055Seric 	}
24258050Seric 	while ((c = (*s++ & 0377)) != '\0')
2433151Seric 	{
2443151Seric 		if (!isascii(c))
2453151Seric 		{
24668481Seric 			if (c == MATCHREPL)
24758050Seric 			{
24858050Seric 				putchar('$');
24958050Seric 				continue;
25058050Seric 			}
25168481Seric 			if (c == MACROEXPAND)
25268481Seric 			{
25368481Seric 				putchar('$');
25468481Seric 				if (bitset(0200, *s))
25568481Seric 					printf("{%s}", macname(*s++ & 0377));
25668481Seric 				continue;
25768481Seric 			}
25858050Seric 			for (mp = MetaMacros; mp->metaname != '\0'; mp++)
25958050Seric 			{
26058050Seric 				if ((mp->metaval & 0377) == c)
26158050Seric 				{
26258050Seric 					printf("$%c", mp->metaname);
26358050Seric 					break;
26458050Seric 				}
26558050Seric 			}
26658050Seric 			if (mp->metaname != '\0')
26758050Seric 				continue;
26823105Seric 			(void) putchar('\\');
2693151Seric 			c &= 0177;
2703151Seric 		}
27157589Seric 		if (isprint(c))
2723151Seric 		{
27357589Seric 			putchar(c);
27457589Seric 			continue;
27557589Seric 		}
27652050Seric 
27757589Seric 		/* wasn't a meta-macro -- find another way to print it */
27857589Seric 		switch (c)
27957589Seric 		{
28057589Seric 		  case '\n':
28157589Seric 			c = 'n';
28257589Seric 			break;
28352050Seric 
28457589Seric 		  case '\r':
28557589Seric 			c = 'r';
28657589Seric 			break;
28752637Seric 
28857589Seric 		  case '\t':
28957589Seric 			c = 't';
29057589Seric 			break;
29157589Seric 
29257589Seric 		  default:
29357589Seric 			(void) putchar('^');
29457589Seric 			(void) putchar(c ^ 0100);
29557589Seric 			continue;
2963151Seric 		}
297*68500Seric 		(void) putchar('\\');
298*68500Seric 		(void) putchar(c);
2993151Seric 	}
3004086Seric 	(void) fflush(stdout);
3013151Seric }
3023151Seric /*
3033151Seric **  MAKELOWER -- Translate a line into lower case
3043151Seric **
3053151Seric **	Parameters:
3063151Seric **		p -- the string to translate.  If NULL, return is
3073151Seric **			immediate.
3083151Seric **
3093151Seric **	Returns:
3103151Seric **		none.
3113151Seric **
3123151Seric **	Side Effects:
3133151Seric **		String pointed to by p is translated to lower case.
3143151Seric **
3153151Seric **	Called By:
3163151Seric **		parse
3173151Seric */
3183151Seric 
31968481Seric void
3203151Seric makelower(p)
3213151Seric 	register char *p;
3223151Seric {
3233151Seric 	register char c;
3243151Seric 
3253151Seric 	if (p == NULL)
3263151Seric 		return;
3273151Seric 	for (; (c = *p) != '\0'; p++)
3283151Seric 		if (isascii(c) && isupper(c))
32933724Sbostic 			*p = tolower(c);
3303151Seric }
3314059Seric /*
3325196Seric **  BUILDFNAME -- build full name from gecos style entry.
3334375Seric **
3345196Seric **	This routine interprets the strange entry that would appear
3355196Seric **	in the GECOS field of the password file.
3365196Seric **
3374375Seric **	Parameters:
3385196Seric **		p -- name to build.
3395196Seric **		login -- the login name of this user (for &).
3405196Seric **		buf -- place to put the result.
3414375Seric **
3424375Seric **	Returns:
34365006Seric **		none.
3444375Seric **
3454375Seric **	Side Effects:
3464375Seric **		none.
3474375Seric */
3484375Seric 
34954984Seric buildfname(gecos, login, buf)
35065006Seric 	register char *gecos;
35165006Seric 	char *login;
35265006Seric 	char *buf;
3534375Seric {
35465006Seric 	register char *p;
35565006Seric 	register char *bp = buf;
35665006Seric 	int l;
3574375Seric 
35865006Seric 	if (*gecos == '*')
35965006Seric 		gecos++;
36065006Seric 
36165006Seric 	/* find length of final string */
36265006Seric 	l = 0;
36365006Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
36454984Seric 	{
36565006Seric 		if (*p == '&')
36665006Seric 			l += strlen(login);
36765006Seric 		else
36865006Seric 			l++;
36954984Seric 	}
37065006Seric 
37165006Seric 	/* now fill in buf */
37265006Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
3734375Seric 	{
37465006Seric 		if (*p == '&')
3754375Seric 		{
37665006Seric 			(void) strcpy(bp, login);
37765006Seric 			*bp = toupper(*bp);
37865006Seric 			while (*bp != '\0')
37965006Seric 				bp++;
3804375Seric 		}
38165006Seric 		else
38265006Seric 			*bp++ = *p;
3834375Seric 	}
3844375Seric 	*bp = '\0';
3854375Seric }
3864375Seric /*
3874538Seric **  SAFEFILE -- return true if a file exists and is safe for a user.
3884538Seric **
3894538Seric **	Parameters:
3904538Seric **		fn -- filename to check.
39164083Seric **		uid -- user id to compare against.
39264083Seric **		gid -- group id to compare against.
39364083Seric **		uname -- user name to compare against (used for group
39464083Seric **			sets).
39564944Seric **		flags -- modifiers:
39665064Seric **			SFF_MUSTOWN -- "uid" must own this file.
39765064Seric **			SFF_NOSLINK -- file cannot be a symbolic link.
3984538Seric **		mode -- mode bits that must match.
39968494Seric **		st -- if set, points to a stat structure that will
40068494Seric **			get the stat info for the file.
4014538Seric **
4024538Seric **	Returns:
40358247Seric **		0 if fn exists, is owned by uid, and matches mode.
40458247Seric **		An errno otherwise.  The actual errno is cleared.
4054538Seric **
4064538Seric **	Side Effects:
4074538Seric **		none.
4084538Seric */
4094538Seric 
41064083Seric #include <grp.h>
41164083Seric 
41263581Seric #ifndef S_IXOTH
41363581Seric # define S_IXOTH	(S_IEXEC >> 6)
41463581Seric #endif
41563581Seric 
41664083Seric #ifndef S_IXGRP
41764083Seric # define S_IXGRP	(S_IEXEC >> 3)
41864083Seric #endif
41964083Seric 
42063753Seric #ifndef S_IXUSR
42163753Seric # define S_IXUSR	(S_IEXEC)
42263753Seric #endif
42363753Seric 
42468494Seric #define ST_MODE_NOFILE	0171147		/* unlikely to occur */
42568494Seric 
42658247Seric int
42768494Seric safefile(fn, uid, gid, uname, flags, mode, st)
4284538Seric 	char *fn;
42955372Seric 	uid_t uid;
43064083Seric 	gid_t gid;
43164083Seric 	char *uname;
43264944Seric 	int flags;
4334538Seric 	int mode;
43468494Seric 	struct stat *st;
4354538Seric {
43663581Seric 	register char *p;
43764083Seric 	register struct group *gr = NULL;
4384538Seric 	struct stat stbuf;
4394538Seric 
44063581Seric 	if (tTd(54, 4))
44164944Seric 		printf("safefile(%s, uid=%d, gid=%d, flags=%x, mode=%o):\n",
44264944Seric 			fn, uid, gid, flags, mode);
44363581Seric 	errno = 0;
44468494Seric 	if (st == NULL)
44568494Seric 		st = &stbuf;
44663581Seric 
44768481Seric 	if (!bitset(SFF_NOPATHCHECK, flags) ||
44868481Seric 	    (uid == 0 && !bitset(SFF_ROOTOK, flags)))
44963581Seric 	{
45068481Seric 		/* check the path to the file for acceptability */
45168481Seric 		for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/')
45265225Seric 		{
45368481Seric 			*p = '\0';
45468481Seric 			if (stat(fn, &stbuf) < 0)
45568481Seric 				break;
45668481Seric 			if (uid == 0 && !bitset(SFF_ROOTOK, flags))
45768481Seric 			{
45868481Seric 				if (bitset(S_IXOTH, stbuf.st_mode))
45968481Seric 					continue;
46068481Seric 				break;
46168481Seric 			}
46268481Seric 			if (stbuf.st_uid == uid &&
46368481Seric 			    bitset(S_IXUSR, stbuf.st_mode))
46465225Seric 				continue;
46568481Seric 			if (stbuf.st_gid == gid &&
46668481Seric 			    bitset(S_IXGRP, stbuf.st_mode))
46768481Seric 				continue;
46868481Seric #ifndef NO_GROUP_SET
46968481Seric 			if (uname != NULL &&
47068481Seric 			    ((gr != NULL && gr->gr_gid == stbuf.st_gid) ||
47168481Seric 			     (gr = getgrgid(stbuf.st_gid)) != NULL))
47268481Seric 			{
47368481Seric 				register char **gp;
47468481Seric 
47568481Seric 				for (gp = gr->gr_mem; gp != NULL && *gp != NULL; gp++)
47668481Seric 					if (strcmp(*gp, uname) == 0)
47768481Seric 						break;
47868481Seric 				if (gp != NULL && *gp != NULL &&
47968481Seric 				    bitset(S_IXGRP, stbuf.st_mode))
48068481Seric 					continue;
48168481Seric 			}
48268481Seric #endif
48368481Seric 			if (!bitset(S_IXOTH, stbuf.st_mode))
48468481Seric 				break;
48568478Seric 		}
48668481Seric 		if (p != NULL)
48763581Seric 		{
48868481Seric 			int ret = errno;
48963581Seric 
49068481Seric 			if (ret == 0)
49168481Seric 				ret = EACCES;
49268481Seric 			if (tTd(54, 4))
49368481Seric 				printf("\t[dir %s] %s\n", fn, errstring(ret));
49468481Seric 			*p = '/';
49568481Seric 			return ret;
49663581Seric 		}
49763581Seric 	}
49863581Seric 
49964944Seric #ifdef HASLSTAT
50068494Seric 	if ((bitset(SFF_NOSLINK, flags) ? lstat(fn, st)
50168494Seric 					: stat(fn, st)) < 0)
50264944Seric #else
50368494Seric 	if (stat(fn, st) < 0)
50464944Seric #endif
50558247Seric 	{
50658247Seric 		int ret = errno;
50758247Seric 
50863581Seric 		if (tTd(54, 4))
50964083Seric 			printf("\t%s\n", errstring(ret));
51063581Seric 
51158247Seric 		errno = 0;
51268494Seric 		if (!bitset(SFF_CREAT, flags))
51368494Seric 			return ret;
51468494Seric 
51568494Seric 		/* check to see if legal to create the file */
51668494Seric 		p = strrchr(fn, '/');
51768494Seric 		if (p == NULL)
51868494Seric 			return ENOTDIR;
51968494Seric 		*p = '\0';
52068494Seric 		if (stat(fn, &stbuf) >= 0)
52168494Seric 		{
52268494Seric 			int md = S_IWRITE|S_IEXEC;
52368494Seric 			if (stbuf.st_uid != uid)
52468494Seric 				md >>= 6;
52568494Seric 			if ((stbuf.st_mode & md) != md)
52668494Seric 				errno = EACCES;
52768494Seric 		}
52868494Seric 		ret = errno;
52968494Seric 		if (tTd(54, 4))
53068494Seric 			printf("\t[final dir %s uid %d mode %o] %s\n",
53168494Seric 				fn, stbuf.st_uid, stbuf.st_mode,
53268494Seric 				errstring(ret));
53368494Seric 		*p = '/';
53468494Seric 		st->st_mode = ST_MODE_NOFILE;
53558247Seric 		return ret;
53658247Seric 	}
53764944Seric 
53864944Seric #ifdef S_ISLNK
53968494Seric 	if (bitset(SFF_NOSLINK, flags) && S_ISLNK(st->st_mode))
54064944Seric 	{
54164944Seric 		if (tTd(54, 4))
54268494Seric 			printf("\t[slink mode %o]\tEPERM\n", st->st_mode);
54364944Seric 		return EPERM;
54464944Seric 	}
54564944Seric #endif
54664944Seric 
54768494Seric 	if (bitset(S_IWUSR|S_IWGRP|S_IWOTH, mode) && bitset(0111, st->st_mode))
54868494Seric 	{
54968494Seric 		if (tTd(29, 5))
55068494Seric 			printf("failed (mode %o: x bits)\n", st->st_mode);
55168494Seric 		errno = EPERM;
55268494Seric 		return FALSE;
55368494Seric 	}
55468494Seric 
55568494Seric 	if (bitset(SFF_SETUIDOK, flags))
55668494Seric 	{
55768494Seric 		if (bitset(S_ISUID, st->st_mode) &&
55868494Seric 		    (st->st_uid != 0 || bitset(SFF_ROOTOK, flags)))
55968494Seric 		{
56068494Seric 			uid = st->st_uid;
56168494Seric 			uname = NULL;
56268494Seric 		}
56368494Seric 		if (bitset(S_ISGID, st->st_mode) &&
56468494Seric 		    (st->st_gid != 0 || bitset(SFF_ROOTOK, flags)))
56568494Seric 			gid = st->st_gid;
56668494Seric 	}
56768494Seric 
56865139Seric 	if (uid == 0 && !bitset(SFF_ROOTOK, flags))
56963581Seric 		mode >>= 6;
57068494Seric 	else if (st->st_uid != uid)
57164084Seric 	{
57264084Seric 		mode >>= 3;
57368494Seric 		if (st->st_gid == gid)
57464084Seric 			;
57564084Seric #ifndef NO_GROUP_SET
57664084Seric 		else if (uname != NULL &&
57768494Seric 			 ((gr != NULL && gr->gr_gid == st->st_gid) ||
57868494Seric 			  (gr = getgrgid(st->st_gid)) != NULL))
57964084Seric 		{
58064084Seric 			register char **gp;
58164084Seric 
58264084Seric 			for (gp = gr->gr_mem; *gp != NULL; gp++)
58364084Seric 				if (strcmp(*gp, uname) == 0)
58464084Seric 					break;
58564084Seric 			if (*gp == NULL)
58664084Seric 				mode >>= 3;
58764084Seric 		}
58864084Seric #endif
58964084Seric 		else
59064084Seric 			mode >>= 3;
59164084Seric 	}
59263581Seric 	if (tTd(54, 4))
59364084Seric 		printf("\t[uid %d, stat %o, mode %o] ",
59468494Seric 			st->st_uid, st->st_mode, mode);
59568494Seric 	if ((st->st_uid == uid || st->st_uid == 0 ||
59665064Seric 	     !bitset(SFF_MUSTOWN, flags)) &&
59768494Seric 	    (st->st_mode & mode) == mode)
59863581Seric 	{
59963581Seric 		if (tTd(54, 4))
60064083Seric 			printf("\tOK\n");
60158247Seric 		return 0;
60263581Seric 	}
60363581Seric 	if (tTd(54, 4))
60464083Seric 		printf("\tEACCES\n");
60563581Seric 	return EACCES;
6064538Seric }
6074538Seric /*
60868494Seric **  SAFEFOPEN -- do a file open with extra checking
60968494Seric **
61068494Seric **	Parameters:
61168494Seric **		fn -- the file name to open.
61268494Seric **		omode -- the open-style mode flags.
61368494Seric **		cmode -- the create-style mode flags.
61468494Seric **		sff -- safefile flags.
61568494Seric **
61668494Seric **	Returns:
61768494Seric **		Same as fopen.
61868494Seric */
61968494Seric 
62068494Seric FILE *
62168494Seric safefopen(fn, omode, cmode, sff)
62268494Seric 	char *fn;
62368494Seric 	int omode;
62468494Seric 	int cmode;
62568494Seric 	int sff;
62668494Seric {
62768494Seric 	int rval;
62868494Seric 	FILE *fp;
62968494Seric 	int smode;
63068494Seric 	struct stat stb, sta;
63168494Seric 	extern char RealUserName[];
63268494Seric 
63368494Seric 	if (bitset(O_CREAT, omode))
63468494Seric 		sff |= SFF_CREAT;
63568494Seric 	smode = 0;
63668494Seric 	switch (omode & O_ACCMODE)
63768494Seric 	{
63868494Seric 	  case O_RDONLY:
63968494Seric 		smode = S_IREAD;
64068494Seric 		break;
64168494Seric 
64268494Seric 	  case O_WRONLY:
64368494Seric 		smode = S_IWRITE;
64468494Seric 		break;
64568494Seric 
64668494Seric 	  case O_RDWR:
64768494Seric 		smode = S_IREAD|S_IWRITE;
64868494Seric 		break;
64968494Seric 
65068494Seric 	  default:
65168494Seric 		smode = 0;
65268494Seric 		break;
65368494Seric 	}
65468494Seric 	rval = safefile(fn, RealUid, RealGid, RealUserName, sff, smode, &stb);
65568494Seric 	if (rval != 0)
65668494Seric 	{
65768494Seric 		errno = rval;
65868494Seric 		return NULL;
65968494Seric 	}
66068494Seric 	if (stb.st_mode == ST_MODE_NOFILE)
66168494Seric 		omode |= O_EXCL;
66268494Seric 
66368494Seric 	fp = dfopen(fn, omode, cmode);
66468494Seric 	if (fp == NULL)
66568494Seric 		return NULL;
66668494Seric 	if (bitset(O_EXCL, omode))
66768494Seric 		return fp;
66868494Seric 	if (fstat(fileno(fp), &sta) < 0 ||
66968494Seric 	    sta.st_nlink != stb.st_nlink ||
67068494Seric 	    sta.st_dev != stb.st_dev ||
67168494Seric 	    sta.st_ino != stb.st_ino ||
67268494Seric 	    sta.st_uid != stb.st_uid ||
67368494Seric 	    sta.st_gid != stb.st_gid)
67468494Seric 	{
67568494Seric 		syserr("554 cannot open: file %s changed after open", fn);
67668494Seric 		errno = EPERM;
67768494Seric 		fclose(fp);
67868494Seric 		return NULL;
67968494Seric 	}
68068494Seric 	return fp;
68168494Seric }
68268494Seric /*
6834557Seric **  FIXCRLF -- fix <CR><LF> in line.
6844557Seric **
6854557Seric **	Looks for the <CR><LF> combination and turns it into the
6864557Seric **	UNIX canonical <NL> character.  It only takes one line,
6874557Seric **	i.e., it is assumed that the first <NL> found is the end
6884557Seric **	of the line.
6894557Seric **
6904557Seric **	Parameters:
6914557Seric **		line -- the line to fix.
6924557Seric **		stripnl -- if true, strip the newline also.
6934557Seric **
6944557Seric **	Returns:
6954557Seric **		none.
6964557Seric **
6974557Seric **	Side Effects:
6984557Seric **		line is changed in place.
6994557Seric */
7004557Seric 
7014557Seric fixcrlf(line, stripnl)
7024557Seric 	char *line;
7034557Seric 	bool stripnl;
7044557Seric {
7054557Seric 	register char *p;
7064557Seric 
70756795Seric 	p = strchr(line, '\n');
7084557Seric 	if (p == NULL)
7094557Seric 		return;
71036291Sbostic 	if (p > line && p[-1] == '\r')
7114557Seric 		p--;
7124557Seric 	if (!stripnl)
7134557Seric 		*p++ = '\n';
7144557Seric 	*p = '\0';
7154557Seric }
7164557Seric /*
7176890Seric **  DFOPEN -- determined file open
7186890Seric **
7196890Seric **	This routine has the semantics of fopen, except that it will
7206890Seric **	keep trying a few times to make this happen.  The idea is that
7216890Seric **	on very loaded systems, we may run out of resources (inodes,
7226890Seric **	whatever), so this tries to get around it.
7236890Seric */
7246890Seric 
72563753Seric #ifndef O_ACCMODE
72663753Seric # define O_ACCMODE	(O_RDONLY|O_WRONLY|O_RDWR)
72763753Seric #endif
72863753Seric 
72959745Seric struct omodes
73059745Seric {
73159745Seric 	int	mask;
73259745Seric 	int	mode;
73359745Seric 	char	*farg;
73459745Seric } OpenModes[] =
73559745Seric {
73659745Seric 	O_ACCMODE,		O_RDONLY,		"r",
73759745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY,		"w",
73859745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY|O_APPEND,	"a",
73959745Seric 	O_TRUNC,		0,			"w+",
74059745Seric 	O_APPEND,		O_APPEND,		"a+",
74159745Seric 	0,			0,			"r+",
74259745Seric };
74359745Seric 
7446890Seric FILE *
74559745Seric dfopen(filename, omode, cmode)
7466890Seric 	char *filename;
74759745Seric 	int omode;
74859745Seric 	int cmode;
7496890Seric {
7506890Seric 	register int tries;
75159745Seric 	int fd;
75259745Seric 	register struct omodes *om;
75359431Seric 	struct stat st;
7546890Seric 
75559745Seric 	for (om = OpenModes; om->mask != 0; om++)
75659745Seric 		if ((omode & om->mask) == om->mode)
75759745Seric 			break;
75859745Seric 
7596890Seric 	for (tries = 0; tries < 10; tries++)
7606890Seric 	{
76125618Seric 		sleep((unsigned) (10 * tries));
7626890Seric 		errno = 0;
76359745Seric 		fd = open(filename, omode, cmode);
76459745Seric 		if (fd >= 0)
7656890Seric 			break;
76666017Seric 		switch (errno)
76766017Seric 		{
76866017Seric 		  case ENFILE:		/* system file table full */
76966017Seric 		  case EINTR:		/* interrupted syscall */
77066017Seric #ifdef ETXTBSY
77166017Seric 		  case ETXTBSY:		/* Apollo: net file locked */
77266017Seric #endif
77366017Seric 			continue;
77466017Seric 		}
77566017Seric 		break;
7766890Seric 	}
77759745Seric 	if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode))
77856328Seric 	{
77956328Seric 		int locktype;
78056328Seric 
78156328Seric 		/* lock the file to avoid accidental conflicts */
78259745Seric 		if ((omode & O_ACCMODE) != O_RDONLY)
78356328Seric 			locktype = LOCK_EX;
78456328Seric 		else
78556328Seric 			locktype = LOCK_SH;
78664335Seric 		(void) lockfile(fd, filename, NULL, locktype);
78756328Seric 		errno = 0;
78856328Seric 	}
78963787Seric 	if (fd < 0)
79063787Seric 		return NULL;
79163787Seric 	else
79263787Seric 		return fdopen(fd, om->farg);
7936890Seric }
7947124Seric /*
7957124Seric **  PUTLINE -- put a line like fputs obeying SMTP conventions
7967124Seric **
7977753Seric **	This routine always guarantees outputing a newline (or CRLF,
7987753Seric **	as appropriate) at the end of the string.
7997753Seric **
8007124Seric **	Parameters:
8017124Seric **		l -- line to put.
80265870Seric **		mci -- the mailer connection information.
8037124Seric **
8047124Seric **	Returns:
8057124Seric **		none
8067124Seric **
8077124Seric **	Side Effects:
8087124Seric **		output of l to fp.
8097124Seric */
8107124Seric 
81165870Seric putline(l, mci)
8127753Seric 	register char *l;
81365870Seric 	register MCI *mci;
8147124Seric {
8157124Seric 	register char *p;
81647157Sbostic 	register char svchar;
81766004Seric 	int slop = 0;
8187124Seric 
81911275Seric 	/* strip out 0200 bits -- these can look like TELNET protocol */
82065870Seric 	if (bitset(MCIF_7BIT, mci->mci_flags))
82111275Seric 	{
82261707Seric 		for (p = l; (svchar = *p) != '\0'; ++p)
82361707Seric 			if (bitset(0200, svchar))
82447157Sbostic 				*p = svchar &~ 0200;
82511275Seric 	}
82611275Seric 
8277753Seric 	do
8287124Seric 	{
8297753Seric 		/* find the end of the line */
83056795Seric 		p = strchr(l, '\n');
8317753Seric 		if (p == NULL)
8327753Seric 			p = &l[strlen(l)];
8337124Seric 
83463753Seric 		if (TrafficLogFile != NULL)
83563753Seric 			fprintf(TrafficLogFile, "%05d >>> ", getpid());
83663753Seric 
8377753Seric 		/* check for line overflow */
83865870Seric 		while (mci->mci_mailer->m_linelimit > 0 &&
83966004Seric 		       (p - l + slop) > mci->mci_mailer->m_linelimit)
8407753Seric 		{
84166004Seric 			register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1];
8427124Seric 
8437753Seric 			svchar = *q;
8447753Seric 			*q = '\0';
84566004Seric 			if (l[0] == '.' && slop == 0 &&
84666004Seric 			    bitnset(M_XDOT, mci->mci_mailer->m_flags))
84763753Seric 			{
84865870Seric 				(void) putc('.', mci->mci_out);
84963753Seric 				if (TrafficLogFile != NULL)
85063753Seric 					(void) putc('.', TrafficLogFile);
85163753Seric 			}
85265870Seric 			fputs(l, mci->mci_out);
85365870Seric 			(void) putc('!', mci->mci_out);
85465870Seric 			fputs(mci->mci_mailer->m_eol, mci->mci_out);
85566004Seric 			(void) putc(' ', mci->mci_out);
85663753Seric 			if (TrafficLogFile != NULL)
85766004Seric 				fprintf(TrafficLogFile, "%s!\n%05d >>>  ",
85863753Seric 					l, getpid());
8597753Seric 			*q = svchar;
8607753Seric 			l = q;
86166004Seric 			slop = 1;
8627753Seric 		}
8637124Seric 
8647753Seric 		/* output last part */
86566004Seric 		if (l[0] == '.' && slop == 0 &&
86666004Seric 		    bitnset(M_XDOT, mci->mci_mailer->m_flags))
86763753Seric 		{
86865870Seric 			(void) putc('.', mci->mci_out);
86963753Seric 			if (TrafficLogFile != NULL)
87063753Seric 				(void) putc('.', TrafficLogFile);
87163753Seric 		}
87263753Seric 		if (TrafficLogFile != NULL)
87363753Seric 			fprintf(TrafficLogFile, "%.*s\n", p - l, l);
87447157Sbostic 		for ( ; l < p; ++l)
87565870Seric 			(void) putc(*l, mci->mci_out);
87665870Seric 		fputs(mci->mci_mailer->m_eol, mci->mci_out);
8777753Seric 		if (*l == '\n')
87847157Sbostic 			++l;
8797753Seric 	} while (l[0] != '\0');
8807124Seric }
8817676Seric /*
8827676Seric **  XUNLINK -- unlink a file, doing logging as appropriate.
8837676Seric **
8847676Seric **	Parameters:
8857676Seric **		f -- name of file to unlink.
8867676Seric **
8877676Seric **	Returns:
8887676Seric **		none.
8897676Seric **
8907676Seric **	Side Effects:
8917676Seric **		f is unlinked.
8927676Seric */
8937676Seric 
8947676Seric xunlink(f)
8957676Seric 	char *f;
8967676Seric {
8977676Seric 	register int i;
8987676Seric 
8997676Seric # ifdef LOG
90058020Seric 	if (LogLevel > 98)
90158020Seric 		syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f);
90256795Seric # endif /* LOG */
9037676Seric 
9047676Seric 	i = unlink(f);
9057676Seric # ifdef LOG
90658020Seric 	if (i < 0 && LogLevel > 97)
9077942Seric 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
90856795Seric # endif /* LOG */
9097676Seric }
9107685Seric /*
91158680Seric **  XFCLOSE -- close a file, doing logging as appropriate.
91258680Seric **
91358680Seric **	Parameters:
91458680Seric **		fp -- file pointer for the file to close
91558680Seric **		a, b -- miscellaneous crud to print for debugging
91658680Seric **
91758680Seric **	Returns:
91858680Seric **		none.
91958680Seric **
92058680Seric **	Side Effects:
92158680Seric **		fp is closed.
92258680Seric */
92358680Seric 
92458680Seric xfclose(fp, a, b)
92558680Seric 	FILE *fp;
92658680Seric 	char *a, *b;
92758680Seric {
92858796Seric 	if (tTd(53, 99))
92958680Seric 		printf("xfclose(%x) %s %s\n", fp, a, b);
93064401Seric #ifdef XDEBUG
93164401Seric 	if (fileno(fp) == 1)
93264401Seric 		syserr("xfclose(%s %s): fd = 1", a, b);
93364401Seric #endif
93458796Seric 	if (fclose(fp) < 0 && tTd(53, 99))
93558680Seric 		printf("xfclose FAILURE: %s\n", errstring(errno));
93658680Seric }
93758680Seric /*
93814885Seric **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
9397685Seric **
9407685Seric **	Parameters:
9417685Seric **		buf -- place to put the input line.
9427685Seric **		siz -- size of buf.
9437685Seric **		fp -- file to read from.
94457384Seric **		timeout -- the timeout before error occurs.
94561093Seric **		during -- what we are trying to read (for error messages).
9467685Seric **
9477685Seric **	Returns:
94815533Seric **		NULL on error (including timeout).  This will also leave
94915533Seric **			buf containing a null string.
9507685Seric **		buf otherwise.
9517685Seric **
9527685Seric **	Side Effects:
9537685Seric **		none.
9547685Seric */
9557685Seric 
95614885Seric static jmp_buf	CtxReadTimeout;
95768481Seric static void	readtimeout();
9587685Seric 
9597685Seric char *
96061093Seric sfgets(buf, siz, fp, timeout, during)
9617685Seric 	char *buf;
9627685Seric 	int siz;
9637685Seric 	FILE *fp;
96457384Seric 	time_t timeout;
96561093Seric 	char *during;
9667685Seric {
9677942Seric 	register EVENT *ev = NULL;
9687685Seric 	register char *p;
9697685Seric 
97066332Seric 	if (fp == NULL)
97166332Seric 	{
97266332Seric 		buf[0] = '\0';
97366332Seric 		return NULL;
97466332Seric 	}
97566332Seric 
97614885Seric 	/* set the timeout */
97757384Seric 	if (timeout != 0)
97814885Seric 	{
97914885Seric 		if (setjmp(CtxReadTimeout) != 0)
98014885Seric 		{
98136233Skarels # ifdef LOG
98236230Skarels 			syslog(LOG_NOTICE,
98361093Seric 			    "timeout waiting for input from %s during %s\n",
98461093Seric 			    CurHostName? CurHostName: "local", during);
98536233Skarels # endif
98636230Skarels 			errno = 0;
98761093Seric 			usrerr("451 timeout waiting for input during %s",
98861093Seric 				during);
98919037Seric 			buf[0] = '\0';
99063753Seric #ifdef XDEBUG
99163753Seric 			checkfd012(during);
99263753Seric #endif
99314885Seric 			return (NULL);
99414885Seric 		}
99568481Seric 		ev = setevent(timeout, readtimeout, 0);
99614885Seric 	}
99714885Seric 
99814885Seric 	/* try to read */
99915533Seric 	p = NULL;
100065190Seric 	while (!feof(fp) && !ferror(fp))
10017942Seric 	{
10027942Seric 		errno = 0;
10037942Seric 		p = fgets(buf, siz, fp);
100465190Seric 		if (p != NULL || errno != EINTR)
100565186Seric 			break;
100665190Seric 		clearerr(fp);
100715533Seric 	}
100814885Seric 
100914885Seric 	/* clear the event if it has not sprung */
101068481Seric 	clrevent(ev);
101114885Seric 
101214885Seric 	/* clean up the books and exit */
10138055Seric 	LineNumber++;
101415533Seric 	if (p == NULL)
101516880Seric 	{
101615533Seric 		buf[0] = '\0';
101763753Seric 		if (TrafficLogFile != NULL)
101863753Seric 			fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid());
101916880Seric 		return (NULL);
102016880Seric 	}
102163753Seric 	if (TrafficLogFile != NULL)
102263753Seric 		fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf);
102368481Seric 	if (SevenBitInput)
102468481Seric 	{
102552106Seric 		for (p = buf; *p != '\0'; p++)
102652106Seric 			*p &= ~0200;
102767546Seric 	}
102868481Seric 	else if (!HasEightBits)
102967546Seric 	{
103068481Seric 		for (p = buf; *p != '\0'; p++)
103168481Seric 		{
103268481Seric 			if (bitset(0200, *p))
103368481Seric 			{
103468481Seric 				HasEightBits = TRUE;
103568481Seric 				break;
103668481Seric 			}
103768481Seric 		}
103867546Seric 	}
103968481Seric 	return (buf);
10407685Seric }
10417685Seric 
104268481Seric static void
104366765Seric readtimeout(timeout)
104466765Seric 	time_t timeout;
10457685Seric {
104668481Seric 	longjmp(CtxReadTimeout, 1);
10477685Seric }
10487786Seric /*
10497786Seric **  FGETFOLDED -- like fgets, but know about folded lines.
10507786Seric **
10517786Seric **	Parameters:
10527786Seric **		buf -- place to put result.
10537786Seric **		n -- bytes available.
10547786Seric **		f -- file to read from.
10557786Seric **
10567786Seric **	Returns:
105757135Seric **		input line(s) on success, NULL on error or EOF.
105857135Seric **		This will normally be buf -- unless the line is too
105957135Seric **			long, when it will be xalloc()ed.
10607786Seric **
10617786Seric **	Side Effects:
10627786Seric **		buf gets lines from f, with continuation lines (lines
10637786Seric **		with leading white space) appended.  CRLF's are mapped
10647786Seric **		into single newlines.  Any trailing NL is stripped.
10657786Seric */
10667786Seric 
10677786Seric char *
10687786Seric fgetfolded(buf, n, f)
10697786Seric 	char *buf;
10707786Seric 	register int n;
10717786Seric 	FILE *f;
10727786Seric {
10737786Seric 	register char *p = buf;
107457135Seric 	char *bp = buf;
10757786Seric 	register int i;
10767786Seric 
10777786Seric 	n--;
107817350Seric 	while ((i = getc(f)) != EOF)
10797786Seric 	{
108017350Seric 		if (i == '\r')
108117350Seric 		{
108217350Seric 			i = getc(f);
108317350Seric 			if (i != '\n')
108417350Seric 			{
108517350Seric 				if (i != EOF)
108623105Seric 					(void) ungetc(i, f);
108717350Seric 				i = '\r';
108817350Seric 			}
108917350Seric 		}
109057135Seric 		if (--n <= 0)
109157135Seric 		{
109257135Seric 			/* allocate new space */
109357135Seric 			char *nbp;
109457135Seric 			int nn;
109557135Seric 
109657135Seric 			nn = (p - bp);
109757232Seric 			if (nn < MEMCHUNKSIZE)
109857135Seric 				nn *= 2;
109957135Seric 			else
110057232Seric 				nn += MEMCHUNKSIZE;
110157135Seric 			nbp = xalloc(nn);
110257135Seric 			bcopy(bp, nbp, p - bp);
110357135Seric 			p = &nbp[p - bp];
110457135Seric 			if (bp != buf)
110557135Seric 				free(bp);
110657135Seric 			bp = nbp;
110757135Seric 			n = nn - (p - bp);
110857135Seric 		}
110957135Seric 		*p++ = i;
111017350Seric 		if (i == '\n')
111117350Seric 		{
111217350Seric 			LineNumber++;
111317350Seric 			i = getc(f);
111417350Seric 			if (i != EOF)
111523105Seric 				(void) ungetc(i, f);
111617350Seric 			if (i != ' ' && i != '\t')
111752647Seric 				break;
111817350Seric 		}
11197786Seric 	}
112057135Seric 	if (p == bp)
112152647Seric 		return (NULL);
112252647Seric 	*--p = '\0';
112357135Seric 	return (bp);
11247786Seric }
11257860Seric /*
11267886Seric **  CURTIME -- return current time.
11277886Seric **
11287886Seric **	Parameters:
11297886Seric **		none.
11307886Seric **
11317886Seric **	Returns:
11327886Seric **		the current time.
11337886Seric **
11347886Seric **	Side Effects:
11357886Seric **		none.
11367886Seric */
11377886Seric 
11387886Seric time_t
11397886Seric curtime()
11407886Seric {
11417886Seric 	auto time_t t;
11427886Seric 
11437886Seric 	(void) time(&t);
11447886Seric 	return (t);
11457886Seric }
11468264Seric /*
11478264Seric **  ATOBOOL -- convert a string representation to boolean.
11488264Seric **
11498264Seric **	Defaults to "TRUE"
11508264Seric **
11518264Seric **	Parameters:
11528264Seric **		s -- string to convert.  Takes "tTyY" as true,
11538264Seric **			others as false.
11548264Seric **
11558264Seric **	Returns:
11568264Seric **		A boolean representation of the string.
11578264Seric **
11588264Seric **	Side Effects:
11598264Seric **		none.
11608264Seric */
11618264Seric 
11628264Seric bool
11638264Seric atobool(s)
11648264Seric 	register char *s;
11658264Seric {
116663833Seric 	if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL)
11678264Seric 		return (TRUE);
11688264Seric 	return (FALSE);
11698264Seric }
11709048Seric /*
11719048Seric **  ATOOCT -- convert a string representation to octal.
11729048Seric **
11739048Seric **	Parameters:
11749048Seric **		s -- string to convert.
11759048Seric **
11769048Seric **	Returns:
11779048Seric **		An integer representing the string interpreted as an
11789048Seric **		octal number.
11799048Seric **
11809048Seric **	Side Effects:
11819048Seric **		none.
11829048Seric */
11839048Seric 
11849048Seric atooct(s)
11859048Seric 	register char *s;
11869048Seric {
11879048Seric 	register int i = 0;
11889048Seric 
11899048Seric 	while (*s >= '0' && *s <= '7')
11909048Seric 		i = (i << 3) | (*s++ - '0');
11919048Seric 	return (i);
11929048Seric }
11939376Seric /*
11949376Seric **  WAITFOR -- wait for a particular process id.
11959376Seric **
11969376Seric **	Parameters:
11979376Seric **		pid -- process id to wait for.
11989376Seric **
11999376Seric **	Returns:
12009376Seric **		status of pid.
12019376Seric **		-1 if pid never shows up.
12029376Seric **
12039376Seric **	Side Effects:
12049376Seric **		none.
12059376Seric */
12069376Seric 
120764562Seric int
12089376Seric waitfor(pid)
12099376Seric 	int pid;
12109376Seric {
121164562Seric #ifdef WAITUNION
121264562Seric 	union wait st;
121364562Seric #else
12149376Seric 	auto int st;
121564562Seric #endif
12169376Seric 	int i;
12179376Seric 
12189376Seric 	do
12199376Seric 	{
12209376Seric 		errno = 0;
12219376Seric 		i = wait(&st);
12229376Seric 	} while ((i >= 0 || errno == EINTR) && i != pid);
12239376Seric 	if (i < 0)
122464562Seric 		return -1;
122564562Seric #ifdef WAITUNION
122664562Seric 	return st.w_status;
122764562Seric #else
122864562Seric 	return st;
122964562Seric #endif
12309376Seric }
12319376Seric /*
123210685Seric **  BITINTERSECT -- tell if two bitmaps intersect
123310685Seric **
123410685Seric **	Parameters:
123510685Seric **		a, b -- the bitmaps in question
123610685Seric **
123710685Seric **	Returns:
123810685Seric **		TRUE if they have a non-null intersection
123910685Seric **		FALSE otherwise
124010685Seric **
124110685Seric **	Side Effects:
124210685Seric **		none.
124310685Seric */
124410685Seric 
124510685Seric bool
124610685Seric bitintersect(a, b)
124710685Seric 	BITMAP a;
124810685Seric 	BITMAP b;
124910685Seric {
125010685Seric 	int i;
125110685Seric 
125210685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
125310685Seric 		if ((a[i] & b[i]) != 0)
125410685Seric 			return (TRUE);
125510685Seric 	return (FALSE);
125610685Seric }
125710685Seric /*
125810685Seric **  BITZEROP -- tell if a bitmap is all zero
125910685Seric **
126010685Seric **	Parameters:
126110685Seric **		map -- the bit map to check
126210685Seric **
126310685Seric **	Returns:
126410685Seric **		TRUE if map is all zero.
126510685Seric **		FALSE if there are any bits set in map.
126610685Seric **
126710685Seric **	Side Effects:
126810685Seric **		none.
126910685Seric */
127010685Seric 
127110685Seric bool
127210685Seric bitzerop(map)
127310685Seric 	BITMAP map;
127410685Seric {
127510685Seric 	int i;
127610685Seric 
127710685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
127810685Seric 		if (map[i] != 0)
127910685Seric 			return (FALSE);
128010685Seric 	return (TRUE);
128110685Seric }
128258247Seric /*
128358318Seric **  STRCONTAINEDIN -- tell if one string is contained in another
128458318Seric **
128558318Seric **	Parameters:
128658318Seric **		a -- possible substring.
128758318Seric **		b -- possible superstring.
128858318Seric **
128958318Seric **	Returns:
129058318Seric **		TRUE if a is contained in b.
129158318Seric **		FALSE otherwise.
129258318Seric */
129358318Seric 
129458318Seric bool
129558318Seric strcontainedin(a, b)
129658318Seric 	register char *a;
129758318Seric 	register char *b;
129858318Seric {
129965012Seric 	int la;
130065012Seric 	int lb;
130165012Seric 	int c;
130258318Seric 
130365012Seric 	la = strlen(a);
130465012Seric 	lb = strlen(b);
130565012Seric 	c = *a;
130665012Seric 	if (isascii(c) && isupper(c))
130765012Seric 		c = tolower(c);
130865012Seric 	for (; lb-- >= la; b++)
130958318Seric 	{
131065012Seric 		if (*b != c && isascii(*b) && isupper(*b) && tolower(*b) != c)
131165012Seric 			continue;
131265012Seric 		if (strncasecmp(a, b, la) == 0)
131358318Seric 			return TRUE;
131458318Seric 	}
131565012Seric 	return FALSE;
131658318Seric }
131763753Seric /*
131863753Seric **  CHECKFD012 -- check low numbered file descriptors
131963753Seric **
132063753Seric **	File descriptors 0, 1, and 2 should be open at all times.
132163753Seric **	This routine verifies that, and fixes it if not true.
132263753Seric **
132363753Seric **	Parameters:
132463753Seric **		where -- a tag printed if the assertion failed
132563753Seric **
132663753Seric **	Returns:
132763753Seric **		none
132863753Seric */
132963753Seric 
133063753Seric checkfd012(where)
133163753Seric 	char *where;
133263753Seric {
133363753Seric #ifdef XDEBUG
133463753Seric 	register int i;
133563753Seric 	struct stat stbuf;
133663753Seric 
133763753Seric 	for (i = 0; i < 3; i++)
133863753Seric 	{
133964735Seric 		if (fstat(i, &stbuf) < 0 && errno != EOPNOTSUPP)
134063753Seric 		{
134163753Seric 			/* oops.... */
134263753Seric 			int fd;
134363753Seric 
134463753Seric 			syserr("%s: fd %d not open", where, i);
134563753Seric 			fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666);
134663753Seric 			if (fd != i)
134763753Seric 			{
134863753Seric 				(void) dup2(fd, i);
134963753Seric 				(void) close(fd);
135063753Seric 			}
135163753Seric 		}
135263753Seric 	}
135363937Seric #endif /* XDEBUG */
135463753Seric }
135564725Seric /*
135664725Seric **  PRINTOPENFDS -- print the open file descriptors (for debugging)
135764725Seric **
135864725Seric **	Parameters:
135964725Seric **		logit -- if set, send output to syslog; otherwise
136064725Seric **			print for debugging.
136164725Seric **
136264725Seric **	Returns:
136364725Seric **		none.
136464725Seric */
136564725Seric 
136664725Seric #include <netdb.h>
136764725Seric #include <arpa/inet.h>
136864725Seric 
136964725Seric printopenfds(logit)
137064725Seric 	bool logit;
137164725Seric {
137264725Seric 	register int fd;
137364743Seric 	extern int DtableSize;
137464743Seric 
137564743Seric 	for (fd = 0; fd < DtableSize; fd++)
137664743Seric 		dumpfd(fd, FALSE, logit);
137764743Seric }
137864743Seric /*
137964743Seric **  DUMPFD -- dump a file descriptor
138064743Seric **
138164743Seric **	Parameters:
138264743Seric **		fd -- the file descriptor to dump.
138364743Seric **		printclosed -- if set, print a notification even if
138464743Seric **			it is closed; otherwise print nothing.
138564743Seric **		logit -- if set, send output to syslog instead of stdout.
138664743Seric */
138764743Seric 
138864743Seric dumpfd(fd, printclosed, logit)
138964743Seric 	int fd;
139064743Seric 	bool printclosed;
139164743Seric 	bool logit;
139264743Seric {
139364725Seric 	register struct hostent *hp;
139464725Seric 	register char *p;
139566747Seric 	char *fmtstr;
139664743Seric 	struct sockaddr_in sin;
139764743Seric 	auto int slen;
139864743Seric 	struct stat st;
139964725Seric 	char buf[200];
140064725Seric 
140164743Seric 	p = buf;
140264743Seric 	sprintf(p, "%3d: ", fd);
140364743Seric 	p += strlen(p);
140464743Seric 
140564743Seric 	if (fstat(fd, &st) < 0)
140664725Seric 	{
140764743Seric 		if (printclosed || errno != EBADF)
140864743Seric 		{
140964743Seric 			sprintf(p, "CANNOT STAT (%s)", errstring(errno));
141064743Seric 			goto printit;
141164743Seric 		}
141264743Seric 		return;
141364743Seric 	}
141464725Seric 
141564743Seric 	slen = fcntl(fd, F_GETFL, NULL);
141664743Seric 	if (slen != -1)
141764743Seric 	{
141864743Seric 		sprintf(p, "fl=0x%x, ", slen);
141964743Seric 		p += strlen(p);
142064743Seric 	}
142164725Seric 
142264743Seric 	sprintf(p, "mode=%o: ", st.st_mode);
142364743Seric 	p += strlen(p);
142464743Seric 	switch (st.st_mode & S_IFMT)
142564743Seric 	{
142664807Seric #ifdef S_IFSOCK
142764743Seric 	  case S_IFSOCK:
142864743Seric 		sprintf(p, "SOCK ");
142964725Seric 		p += strlen(p);
143064743Seric 		slen = sizeof sin;
143164743Seric 		if (getsockname(fd, (struct sockaddr *) &sin, &slen) < 0)
143264743Seric 			sprintf(p, "(badsock)");
143364743Seric 		else
143464725Seric 		{
143568481Seric 			hp = gethostbyaddr((char *) &sin.sin_addr,
143668481Seric 					   INADDRSZ, AF_INET);
143764743Seric 			sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr)
143864743Seric 						   : hp->h_name, ntohs(sin.sin_port));
143964743Seric 		}
144064743Seric 		p += strlen(p);
144164743Seric 		sprintf(p, "->");
144264743Seric 		p += strlen(p);
144364743Seric 		slen = sizeof sin;
144464743Seric 		if (getpeername(fd, (struct sockaddr *) &sin, &slen) < 0)
144564743Seric 			sprintf(p, "(badsock)");
144664743Seric 		else
144764743Seric 		{
144868481Seric 			hp = gethostbyaddr((char *) &sin.sin_addr,
144968481Seric 					   INADDRSZ, AF_INET);
145064743Seric 			sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr)
145164743Seric 						   : hp->h_name, ntohs(sin.sin_port));
145264743Seric 		}
145364743Seric 		break;
145464807Seric #endif
145564725Seric 
145664743Seric 	  case S_IFCHR:
145764743Seric 		sprintf(p, "CHR: ");
145864743Seric 		p += strlen(p);
145964743Seric 		goto defprint;
146064725Seric 
146164743Seric 	  case S_IFBLK:
146264743Seric 		sprintf(p, "BLK: ");
146364743Seric 		p += strlen(p);
146464743Seric 		goto defprint;
146564725Seric 
146666252Seric #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK)
146765378Seric 	  case S_IFIFO:
146865378Seric 		sprintf(p, "FIFO: ");
146965378Seric 		p += strlen(p);
147065378Seric 		goto defprint;
147165378Seric #endif
147265378Seric 
147365378Seric #ifdef S_IFDIR
147465378Seric 	  case S_IFDIR:
147565378Seric 		sprintf(p, "DIR: ");
147665378Seric 		p += strlen(p);
147765378Seric 		goto defprint;
147865378Seric #endif
147965378Seric 
148065378Seric #ifdef S_IFLNK
148165378Seric 	  case S_IFLNK:
148265378Seric 		sprintf(p, "LNK: ");
148365378Seric 		p += strlen(p);
148465378Seric 		goto defprint;
148565378Seric #endif
148665378Seric 
148764743Seric 	  default:
148864725Seric defprint:
148966785Seric 		if (sizeof st.st_size > sizeof (long))
149066751Seric 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%qd";
149166747Seric 		else
149266751Seric 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld";
149366747Seric 		sprintf(p, fmtstr,
149464743Seric 			major(st.st_dev), minor(st.st_dev), st.st_ino,
149564743Seric 			st.st_nlink, st.st_uid, st.st_gid, st.st_size);
149664743Seric 		break;
149764743Seric 	}
149864725Seric 
149964743Seric printit:
150066748Seric #ifdef LOG
150164743Seric 	if (logit)
150265006Seric 		syslog(LOG_DEBUG, "%s", buf);
150364743Seric 	else
150466748Seric #endif
150564743Seric 		printf("%s\n", buf);
150664725Seric }
150765015Seric /*
150865015Seric **  SHORTENSTRING -- return short version of a string
150965015Seric **
151065015Seric **	If the string is already short, just return it.  If it is too
151165015Seric **	long, return the head and tail of the string.
151265015Seric **
151365015Seric **	Parameters:
151465015Seric **		s -- the string to shorten.
151565015Seric **		m -- the max length of the string.
151665015Seric **
151765015Seric **	Returns:
151865015Seric **		Either s or a short version of s.
151965015Seric */
152065015Seric 
152165015Seric #ifndef MAXSHORTSTR
152265055Seric # define MAXSHORTSTR	203
152365015Seric #endif
152465015Seric 
152565015Seric char *
152665015Seric shortenstring(s, m)
152765015Seric 	register char *s;
152865015Seric 	int m;
152965015Seric {
153065015Seric 	int l;
153165015Seric 	static char buf[MAXSHORTSTR + 1];
153265015Seric 
153365015Seric 	l = strlen(s);
153465015Seric 	if (l < m)
153565015Seric 		return s;
153665015Seric 	if (m > MAXSHORTSTR)
153765015Seric 		m = MAXSHORTSTR;
153865015Seric 	else if (m < 10)
153965015Seric 	{
154065015Seric 		if (m < 5)
154165015Seric 		{
154265015Seric 			strncpy(buf, s, m);
154365015Seric 			buf[m] = '\0';
154465015Seric 			return buf;
154565015Seric 		}
154665015Seric 		strncpy(buf, s, m - 3);
154765015Seric 		strcpy(buf + m - 3, "...");
154865015Seric 		return buf;
154965015Seric 	}
155065015Seric 	m = (m - 3) / 2;
155165015Seric 	strncpy(buf, s, m);
155265015Seric 	strcpy(buf + m, "...");
155365015Seric 	strcpy(buf + m + 3, s + l - m);
155465015Seric 	return buf;
155565015Seric }
155667848Seric /*
155768481Seric **  GET_COLUMN  -- look up a Column in a line buffer
155868481Seric **
155968481Seric **	Parameters:
156068481Seric **		line -- the raw text line to search.
156168481Seric **		col -- the column number to fetch.
156268481Seric **		delim -- the delimiter between columns.  If null,
156368481Seric **			use white space.
156468481Seric **		buf -- the output buffer.
156568481Seric **
156668481Seric **	Returns:
156768481Seric **		buf if successful.
156868481Seric **		NULL otherwise.
156968481Seric */
157068481Seric 
157168481Seric char *
157268481Seric get_column(line, col, delim, buf)
157368481Seric 	char line[];
157468481Seric 	int col;
157568481Seric 	char delim;
157668481Seric 	char buf[];
157768481Seric {
157868481Seric 	char *p;
157968481Seric 	char *begin, *end;
158068481Seric 	int i;
158168481Seric 	char delimbuf[3];
158268481Seric 
158368481Seric 	if (delim == '\0')
158468481Seric 		strcpy(delimbuf, "\t ");
158568481Seric 	else
158668481Seric 	{
158768481Seric 		delimbuf[0] = delim;
158868481Seric 		delimbuf[1] = '\0';
158968481Seric 	}
159068481Seric 
159168481Seric 	p = line;
159268481Seric 	if (*p == '\0')
159368481Seric 		return NULL;			/* line empty */
159468481Seric 	if (*p == delim && col == 0)
159568481Seric 		return NULL;			/* first column empty */
159668481Seric 
159768481Seric 	begin = line;
159868481Seric 
159968481Seric 	if (col == 0 && delim == '\0')
160068481Seric 	{
160168481Seric 		while (*begin && isspace(*begin))
160268481Seric 			begin++;
160368481Seric 	}
160468481Seric 
160568481Seric 	for (i = 0; i < col; i++)
160668481Seric 	{
160768481Seric 		if ((begin = strpbrk(begin, delimbuf)) == NULL)
160868481Seric 			return NULL;		/* no such column */
160968481Seric 		begin++;
161068481Seric 		if (delim == '\0')
161168481Seric 		{
161268481Seric 			while (*begin && isspace(*begin))
161368481Seric 				begin++;
161468481Seric 		}
161568481Seric 	}
161668481Seric 
161768481Seric 	end = strpbrk(begin, delimbuf);
161868481Seric 	if (end == NULL)
161968481Seric 	{
162068481Seric 		strcpy(buf, begin);
162168481Seric 	}
162268481Seric 	else
162368481Seric 	{
162468481Seric 		strncpy(buf, begin, end - begin);
162568481Seric 		buf[end - begin] = '\0';
162668481Seric 	}
162768481Seric 	return buf;
162868481Seric }
162968481Seric /*
163068267Seric **  CLEANSTRCPY -- copy string keeping out bogus characters
163168267Seric **
163268267Seric **	Parameters:
163368267Seric **		t -- "to" string.
163468267Seric **		f -- "from" string.
163568267Seric **		l -- length of space available in "to" string.
163668267Seric **
163768267Seric **	Returns:
163868267Seric **		none.
163968267Seric */
164068267Seric 
164168267Seric void
164268267Seric cleanstrcpy(t, f, l)
164368267Seric 	register char *t;
164468267Seric 	register char *f;
164568267Seric 	int l;
164668267Seric {
164768281Seric #ifdef LOG
164868281Seric 	/* check for newlines and log if necessary */
164968478Seric 	(void) denlstring(f, TRUE, TRUE);
165068281Seric #endif
165168281Seric 
165268267Seric 	l--;
165368267Seric 	while (l > 0 && *f != '\0')
165468267Seric 	{
165568267Seric 		if (isascii(*f) &&
165668267Seric 		    (isalnum(*f) || strchr("!#$%&'*+-./^_`{|}~", *f) != NULL))
165768267Seric 		{
165868267Seric 			l--;
165968267Seric 			*t++ = *f;
166068267Seric 		}
166168267Seric 		f++;
166268267Seric 	}
166368267Seric 	*t = '\0';
166468267Seric }
166568267Seric /*
166668267Seric **  DENLSTRING -- convert newlines in a string to spaces
166768267Seric **
166868267Seric **	Parameters:
166968267Seric **		s -- the input string
167068478Seric **		strict -- if set, don't permit continuation lines.
167168457Seric **		logattacks -- if set, log attempted attacks.
167268267Seric **
167368267Seric **	Returns:
167468267Seric **		A pointer to a version of the string with newlines
167568267Seric **		mapped to spaces.  This should be copied.
167668267Seric */
167768267Seric 
167868267Seric char *
167968478Seric denlstring(s, strict, logattacks)
168068267Seric 	char *s;
168168478Seric 	int strict;
168268465Seric 	int logattacks;
168368267Seric {
168468267Seric 	register char *p;
168568267Seric 	int l;
168668267Seric 	static char *bp = NULL;
168768267Seric 	static int bl = 0;
168868267Seric 
168968478Seric 	p = s;
169068478Seric 	while ((p = strchr(p, '\n')) != NULL)
169168478Seric 		if (strict || (*++p != ' ' && *p != '\t'))
169268478Seric 			break;
169368478Seric 	if (p == NULL)
169468267Seric 		return s;
169568267Seric 
169668267Seric 	l = strlen(s) + 1;
169768267Seric 	if (bl < l)
169868267Seric 	{
169968267Seric 		/* allocate more space */
170068267Seric 		if (bp != NULL)
170168267Seric 			free(bp);
170268267Seric 		bp = xalloc(l);
170368267Seric 		bl = l;
170468267Seric 	}
170568267Seric 	strcpy(bp, s);
170668267Seric 	for (p = bp; (p = strchr(p, '\n')) != NULL; )
170768267Seric 		*p++ = ' ';
170868281Seric 
170968481Seric /*
171068281Seric #ifdef LOG
171168457Seric 	if (logattacks)
171268457Seric 	{
171368457Seric 		syslog(LOG_NOTICE, "POSSIBLE ATTACK from %s: newline in string \"%s\"",
171468457Seric 			RealHostName == NULL ? "[UNKNOWN]" : RealHostName,
171568457Seric 			shortenstring(bp, 80));
171668457Seric 	}
171768281Seric #endif
171868481Seric */
171968281Seric 
172068267Seric 	return bp;
172168267Seric }
1722