xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 68513)
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*68513Seric static char sccsid[] = "@(#)util.c	8.57 (Berkeley) 03/10/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 		}
29768500Seric 		(void) putchar('\\');
29868500Seric 		(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;
456*68513Seric 			if (uid == 0 && bitset(S_IWGRP|S_IWOTH, stbuf.st_mode))
457*68513Seric 				message("051 WARNING: writable directory %s",
458*68513Seric 					fn);
45968481Seric 			if (uid == 0 && !bitset(SFF_ROOTOK, flags))
46068481Seric 			{
46168481Seric 				if (bitset(S_IXOTH, stbuf.st_mode))
46268481Seric 					continue;
46368481Seric 				break;
46468481Seric 			}
46568481Seric 			if (stbuf.st_uid == uid &&
46668481Seric 			    bitset(S_IXUSR, stbuf.st_mode))
46765225Seric 				continue;
46868481Seric 			if (stbuf.st_gid == gid &&
46968481Seric 			    bitset(S_IXGRP, stbuf.st_mode))
47068481Seric 				continue;
47168481Seric #ifndef NO_GROUP_SET
47268481Seric 			if (uname != NULL &&
47368481Seric 			    ((gr != NULL && gr->gr_gid == stbuf.st_gid) ||
47468481Seric 			     (gr = getgrgid(stbuf.st_gid)) != NULL))
47568481Seric 			{
47668481Seric 				register char **gp;
47768481Seric 
47868481Seric 				for (gp = gr->gr_mem; gp != NULL && *gp != NULL; gp++)
47968481Seric 					if (strcmp(*gp, uname) == 0)
48068481Seric 						break;
48168481Seric 				if (gp != NULL && *gp != NULL &&
48268481Seric 				    bitset(S_IXGRP, stbuf.st_mode))
48368481Seric 					continue;
48468481Seric 			}
48568481Seric #endif
48668481Seric 			if (!bitset(S_IXOTH, stbuf.st_mode))
48768481Seric 				break;
48868478Seric 		}
48968481Seric 		if (p != NULL)
49063581Seric 		{
49168481Seric 			int ret = errno;
49263581Seric 
49368481Seric 			if (ret == 0)
49468481Seric 				ret = EACCES;
49568481Seric 			if (tTd(54, 4))
49668481Seric 				printf("\t[dir %s] %s\n", fn, errstring(ret));
49768481Seric 			*p = '/';
49868481Seric 			return ret;
49963581Seric 		}
50063581Seric 	}
50163581Seric 
50264944Seric #ifdef HASLSTAT
50368494Seric 	if ((bitset(SFF_NOSLINK, flags) ? lstat(fn, st)
50468494Seric 					: stat(fn, st)) < 0)
50564944Seric #else
50668494Seric 	if (stat(fn, st) < 0)
50764944Seric #endif
50858247Seric 	{
50958247Seric 		int ret = errno;
51058247Seric 
51163581Seric 		if (tTd(54, 4))
51264083Seric 			printf("\t%s\n", errstring(ret));
51363581Seric 
51458247Seric 		errno = 0;
51568494Seric 		if (!bitset(SFF_CREAT, flags))
51668494Seric 			return ret;
51768494Seric 
51868494Seric 		/* check to see if legal to create the file */
51968494Seric 		p = strrchr(fn, '/');
52068494Seric 		if (p == NULL)
52168494Seric 			return ENOTDIR;
52268494Seric 		*p = '\0';
52368494Seric 		if (stat(fn, &stbuf) >= 0)
52468494Seric 		{
52568494Seric 			int md = S_IWRITE|S_IEXEC;
52668494Seric 			if (stbuf.st_uid != uid)
52768494Seric 				md >>= 6;
52868494Seric 			if ((stbuf.st_mode & md) != md)
52968494Seric 				errno = EACCES;
53068494Seric 		}
53168494Seric 		ret = errno;
53268494Seric 		if (tTd(54, 4))
53368494Seric 			printf("\t[final dir %s uid %d mode %o] %s\n",
53468494Seric 				fn, stbuf.st_uid, stbuf.st_mode,
53568494Seric 				errstring(ret));
53668494Seric 		*p = '/';
53768494Seric 		st->st_mode = ST_MODE_NOFILE;
53858247Seric 		return ret;
53958247Seric 	}
54064944Seric 
54164944Seric #ifdef S_ISLNK
54268494Seric 	if (bitset(SFF_NOSLINK, flags) && S_ISLNK(st->st_mode))
54364944Seric 	{
54464944Seric 		if (tTd(54, 4))
54568494Seric 			printf("\t[slink mode %o]\tEPERM\n", st->st_mode);
54664944Seric 		return EPERM;
54764944Seric 	}
54864944Seric #endif
549*68513Seric 	if (bitset(SFF_REGONLY, flags) && !S_ISREG(st->st_mode))
550*68513Seric 	{
551*68513Seric 		if (tTd(54, 4))
552*68513Seric 			printf("\t[non-reg mode %o]\tEPERM\n", st->st_mode);
553*68513Seric 		return EPERM;
554*68513Seric 	}
55568494Seric 	if (bitset(S_IWUSR|S_IWGRP|S_IWOTH, mode) && bitset(0111, st->st_mode))
55668494Seric 	{
55768494Seric 		if (tTd(29, 5))
55868494Seric 			printf("failed (mode %o: x bits)\n", st->st_mode);
559*68513Seric 		return EPERM;
56068494Seric 	}
56168494Seric 
56268494Seric 	if (bitset(SFF_SETUIDOK, flags))
56368494Seric 	{
56468494Seric 		if (bitset(S_ISUID, st->st_mode) &&
56568494Seric 		    (st->st_uid != 0 || bitset(SFF_ROOTOK, flags)))
56668494Seric 		{
56768494Seric 			uid = st->st_uid;
56868494Seric 			uname = NULL;
56968494Seric 		}
57068494Seric 		if (bitset(S_ISGID, st->st_mode) &&
57168494Seric 		    (st->st_gid != 0 || bitset(SFF_ROOTOK, flags)))
57268494Seric 			gid = st->st_gid;
57368494Seric 	}
57468494Seric 
57565139Seric 	if (uid == 0 && !bitset(SFF_ROOTOK, flags))
57663581Seric 		mode >>= 6;
57768494Seric 	else if (st->st_uid != uid)
57864084Seric 	{
57964084Seric 		mode >>= 3;
58068494Seric 		if (st->st_gid == gid)
58164084Seric 			;
58264084Seric #ifndef NO_GROUP_SET
58364084Seric 		else if (uname != NULL &&
58468494Seric 			 ((gr != NULL && gr->gr_gid == st->st_gid) ||
58568494Seric 			  (gr = getgrgid(st->st_gid)) != NULL))
58664084Seric 		{
58764084Seric 			register char **gp;
58864084Seric 
58964084Seric 			for (gp = gr->gr_mem; *gp != NULL; gp++)
59064084Seric 				if (strcmp(*gp, uname) == 0)
59164084Seric 					break;
59264084Seric 			if (*gp == NULL)
59364084Seric 				mode >>= 3;
59464084Seric 		}
59564084Seric #endif
59664084Seric 		else
59764084Seric 			mode >>= 3;
59864084Seric 	}
59963581Seric 	if (tTd(54, 4))
60064084Seric 		printf("\t[uid %d, stat %o, mode %o] ",
60168494Seric 			st->st_uid, st->st_mode, mode);
60268494Seric 	if ((st->st_uid == uid || st->st_uid == 0 ||
60365064Seric 	     !bitset(SFF_MUSTOWN, flags)) &&
60468494Seric 	    (st->st_mode & mode) == mode)
60563581Seric 	{
60663581Seric 		if (tTd(54, 4))
60764083Seric 			printf("\tOK\n");
60858247Seric 		return 0;
60963581Seric 	}
61063581Seric 	if (tTd(54, 4))
61164083Seric 		printf("\tEACCES\n");
61263581Seric 	return EACCES;
6134538Seric }
6144538Seric /*
61568494Seric **  SAFEFOPEN -- do a file open with extra checking
61668494Seric **
61768494Seric **	Parameters:
61868494Seric **		fn -- the file name to open.
61968494Seric **		omode -- the open-style mode flags.
62068494Seric **		cmode -- the create-style mode flags.
62168494Seric **		sff -- safefile flags.
62268494Seric **
62368494Seric **	Returns:
62468494Seric **		Same as fopen.
62568494Seric */
62668494Seric 
62768494Seric FILE *
62868494Seric safefopen(fn, omode, cmode, sff)
62968494Seric 	char *fn;
63068494Seric 	int omode;
63168494Seric 	int cmode;
63268494Seric 	int sff;
63368494Seric {
63468494Seric 	int rval;
63568494Seric 	FILE *fp;
63668494Seric 	int smode;
63768494Seric 	struct stat stb, sta;
63868494Seric 	extern char RealUserName[];
63968494Seric 
64068494Seric 	if (bitset(O_CREAT, omode))
64168494Seric 		sff |= SFF_CREAT;
64268494Seric 	smode = 0;
64368494Seric 	switch (omode & O_ACCMODE)
64468494Seric 	{
64568494Seric 	  case O_RDONLY:
64668494Seric 		smode = S_IREAD;
64768494Seric 		break;
64868494Seric 
64968494Seric 	  case O_WRONLY:
65068494Seric 		smode = S_IWRITE;
65168494Seric 		break;
65268494Seric 
65368494Seric 	  case O_RDWR:
65468494Seric 		smode = S_IREAD|S_IWRITE;
65568494Seric 		break;
65668494Seric 
65768494Seric 	  default:
65868494Seric 		smode = 0;
65968494Seric 		break;
66068494Seric 	}
661*68513Seric 	if (bitset(SFF_OPENASROOT, sff))
662*68513Seric 		rval = safefile(fn, 0, 0, NULL, sff, smode, &stb);
663*68513Seric 	else
664*68513Seric 		rval = safefile(fn, RealUid, RealGid, RealUserName,
665*68513Seric 				sff, smode, &stb);
66668494Seric 	if (rval != 0)
66768494Seric 	{
66868494Seric 		errno = rval;
66968494Seric 		return NULL;
67068494Seric 	}
67168494Seric 	if (stb.st_mode == ST_MODE_NOFILE)
67268494Seric 		omode |= O_EXCL;
67368494Seric 
67468494Seric 	fp = dfopen(fn, omode, cmode);
67568494Seric 	if (fp == NULL)
67668494Seric 		return NULL;
67768494Seric 	if (bitset(O_EXCL, omode))
67868494Seric 		return fp;
67968494Seric 	if (fstat(fileno(fp), &sta) < 0 ||
68068494Seric 	    sta.st_nlink != stb.st_nlink ||
68168494Seric 	    sta.st_dev != stb.st_dev ||
68268494Seric 	    sta.st_ino != stb.st_ino ||
68368494Seric 	    sta.st_uid != stb.st_uid ||
68468494Seric 	    sta.st_gid != stb.st_gid)
68568494Seric 	{
68668494Seric 		syserr("554 cannot open: file %s changed after open", fn);
687*68513Seric 		fclose(fp);
68868494Seric 		errno = EPERM;
68968494Seric 		return NULL;
69068494Seric 	}
69168494Seric 	return fp;
69268494Seric }
69368494Seric /*
6944557Seric **  FIXCRLF -- fix <CR><LF> in line.
6954557Seric **
6964557Seric **	Looks for the <CR><LF> combination and turns it into the
6974557Seric **	UNIX canonical <NL> character.  It only takes one line,
6984557Seric **	i.e., it is assumed that the first <NL> found is the end
6994557Seric **	of the line.
7004557Seric **
7014557Seric **	Parameters:
7024557Seric **		line -- the line to fix.
7034557Seric **		stripnl -- if true, strip the newline also.
7044557Seric **
7054557Seric **	Returns:
7064557Seric **		none.
7074557Seric **
7084557Seric **	Side Effects:
7094557Seric **		line is changed in place.
7104557Seric */
7114557Seric 
7124557Seric fixcrlf(line, stripnl)
7134557Seric 	char *line;
7144557Seric 	bool stripnl;
7154557Seric {
7164557Seric 	register char *p;
7174557Seric 
71856795Seric 	p = strchr(line, '\n');
7194557Seric 	if (p == NULL)
7204557Seric 		return;
72136291Sbostic 	if (p > line && p[-1] == '\r')
7224557Seric 		p--;
7234557Seric 	if (!stripnl)
7244557Seric 		*p++ = '\n';
7254557Seric 	*p = '\0';
7264557Seric }
7274557Seric /*
7286890Seric **  DFOPEN -- determined file open
7296890Seric **
7306890Seric **	This routine has the semantics of fopen, except that it will
7316890Seric **	keep trying a few times to make this happen.  The idea is that
7326890Seric **	on very loaded systems, we may run out of resources (inodes,
7336890Seric **	whatever), so this tries to get around it.
7346890Seric */
7356890Seric 
73663753Seric #ifndef O_ACCMODE
73763753Seric # define O_ACCMODE	(O_RDONLY|O_WRONLY|O_RDWR)
73863753Seric #endif
73963753Seric 
74059745Seric struct omodes
74159745Seric {
74259745Seric 	int	mask;
74359745Seric 	int	mode;
74459745Seric 	char	*farg;
74559745Seric } OpenModes[] =
74659745Seric {
74759745Seric 	O_ACCMODE,		O_RDONLY,		"r",
74859745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY,		"w",
74959745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY|O_APPEND,	"a",
75059745Seric 	O_TRUNC,		0,			"w+",
75159745Seric 	O_APPEND,		O_APPEND,		"a+",
75259745Seric 	0,			0,			"r+",
75359745Seric };
75459745Seric 
7556890Seric FILE *
75659745Seric dfopen(filename, omode, cmode)
7576890Seric 	char *filename;
75859745Seric 	int omode;
75959745Seric 	int cmode;
7606890Seric {
7616890Seric 	register int tries;
76259745Seric 	int fd;
76359745Seric 	register struct omodes *om;
76459431Seric 	struct stat st;
7656890Seric 
76659745Seric 	for (om = OpenModes; om->mask != 0; om++)
76759745Seric 		if ((omode & om->mask) == om->mode)
76859745Seric 			break;
76959745Seric 
7706890Seric 	for (tries = 0; tries < 10; tries++)
7716890Seric 	{
77225618Seric 		sleep((unsigned) (10 * tries));
7736890Seric 		errno = 0;
77459745Seric 		fd = open(filename, omode, cmode);
77559745Seric 		if (fd >= 0)
7766890Seric 			break;
77766017Seric 		switch (errno)
77866017Seric 		{
77966017Seric 		  case ENFILE:		/* system file table full */
78066017Seric 		  case EINTR:		/* interrupted syscall */
78166017Seric #ifdef ETXTBSY
78266017Seric 		  case ETXTBSY:		/* Apollo: net file locked */
78366017Seric #endif
78466017Seric 			continue;
78566017Seric 		}
78666017Seric 		break;
7876890Seric 	}
78859745Seric 	if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode))
78956328Seric 	{
79056328Seric 		int locktype;
79156328Seric 
79256328Seric 		/* lock the file to avoid accidental conflicts */
79359745Seric 		if ((omode & O_ACCMODE) != O_RDONLY)
79456328Seric 			locktype = LOCK_EX;
79556328Seric 		else
79656328Seric 			locktype = LOCK_SH;
79764335Seric 		(void) lockfile(fd, filename, NULL, locktype);
79856328Seric 		errno = 0;
79956328Seric 	}
80063787Seric 	if (fd < 0)
80163787Seric 		return NULL;
80263787Seric 	else
80363787Seric 		return fdopen(fd, om->farg);
8046890Seric }
8057124Seric /*
8067124Seric **  PUTLINE -- put a line like fputs obeying SMTP conventions
8077124Seric **
8087753Seric **	This routine always guarantees outputing a newline (or CRLF,
8097753Seric **	as appropriate) at the end of the string.
8107753Seric **
8117124Seric **	Parameters:
8127124Seric **		l -- line to put.
81365870Seric **		mci -- the mailer connection information.
8147124Seric **
8157124Seric **	Returns:
8167124Seric **		none
8177124Seric **
8187124Seric **	Side Effects:
8197124Seric **		output of l to fp.
8207124Seric */
8217124Seric 
82265870Seric putline(l, mci)
8237753Seric 	register char *l;
82465870Seric 	register MCI *mci;
8257124Seric {
8267124Seric 	register char *p;
82747157Sbostic 	register char svchar;
82866004Seric 	int slop = 0;
8297124Seric 
83011275Seric 	/* strip out 0200 bits -- these can look like TELNET protocol */
83165870Seric 	if (bitset(MCIF_7BIT, mci->mci_flags))
83211275Seric 	{
83361707Seric 		for (p = l; (svchar = *p) != '\0'; ++p)
83461707Seric 			if (bitset(0200, svchar))
83547157Sbostic 				*p = svchar &~ 0200;
83611275Seric 	}
83711275Seric 
8387753Seric 	do
8397124Seric 	{
8407753Seric 		/* find the end of the line */
84156795Seric 		p = strchr(l, '\n');
8427753Seric 		if (p == NULL)
8437753Seric 			p = &l[strlen(l)];
8447124Seric 
84563753Seric 		if (TrafficLogFile != NULL)
84663753Seric 			fprintf(TrafficLogFile, "%05d >>> ", getpid());
84763753Seric 
8487753Seric 		/* check for line overflow */
84965870Seric 		while (mci->mci_mailer->m_linelimit > 0 &&
85066004Seric 		       (p - l + slop) > mci->mci_mailer->m_linelimit)
8517753Seric 		{
85266004Seric 			register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1];
8537124Seric 
8547753Seric 			svchar = *q;
8557753Seric 			*q = '\0';
85666004Seric 			if (l[0] == '.' && slop == 0 &&
85766004Seric 			    bitnset(M_XDOT, mci->mci_mailer->m_flags))
85863753Seric 			{
85965870Seric 				(void) putc('.', mci->mci_out);
86063753Seric 				if (TrafficLogFile != NULL)
86163753Seric 					(void) putc('.', TrafficLogFile);
86263753Seric 			}
86365870Seric 			fputs(l, mci->mci_out);
86465870Seric 			(void) putc('!', mci->mci_out);
86565870Seric 			fputs(mci->mci_mailer->m_eol, mci->mci_out);
86666004Seric 			(void) putc(' ', mci->mci_out);
86763753Seric 			if (TrafficLogFile != NULL)
86866004Seric 				fprintf(TrafficLogFile, "%s!\n%05d >>>  ",
86963753Seric 					l, getpid());
8707753Seric 			*q = svchar;
8717753Seric 			l = q;
87266004Seric 			slop = 1;
8737753Seric 		}
8747124Seric 
8757753Seric 		/* output last part */
87666004Seric 		if (l[0] == '.' && slop == 0 &&
87766004Seric 		    bitnset(M_XDOT, mci->mci_mailer->m_flags))
87863753Seric 		{
87965870Seric 			(void) putc('.', mci->mci_out);
88063753Seric 			if (TrafficLogFile != NULL)
88163753Seric 				(void) putc('.', TrafficLogFile);
88263753Seric 		}
88363753Seric 		if (TrafficLogFile != NULL)
88463753Seric 			fprintf(TrafficLogFile, "%.*s\n", p - l, l);
88547157Sbostic 		for ( ; l < p; ++l)
88665870Seric 			(void) putc(*l, mci->mci_out);
88765870Seric 		fputs(mci->mci_mailer->m_eol, mci->mci_out);
8887753Seric 		if (*l == '\n')
88947157Sbostic 			++l;
8907753Seric 	} while (l[0] != '\0');
8917124Seric }
8927676Seric /*
8937676Seric **  XUNLINK -- unlink a file, doing logging as appropriate.
8947676Seric **
8957676Seric **	Parameters:
8967676Seric **		f -- name of file to unlink.
8977676Seric **
8987676Seric **	Returns:
8997676Seric **		none.
9007676Seric **
9017676Seric **	Side Effects:
9027676Seric **		f is unlinked.
9037676Seric */
9047676Seric 
9057676Seric xunlink(f)
9067676Seric 	char *f;
9077676Seric {
9087676Seric 	register int i;
9097676Seric 
9107676Seric # ifdef LOG
91158020Seric 	if (LogLevel > 98)
91258020Seric 		syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f);
91356795Seric # endif /* LOG */
9147676Seric 
9157676Seric 	i = unlink(f);
9167676Seric # ifdef LOG
91758020Seric 	if (i < 0 && LogLevel > 97)
9187942Seric 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
91956795Seric # endif /* LOG */
9207676Seric }
9217685Seric /*
92258680Seric **  XFCLOSE -- close a file, doing logging as appropriate.
92358680Seric **
92458680Seric **	Parameters:
92558680Seric **		fp -- file pointer for the file to close
92658680Seric **		a, b -- miscellaneous crud to print for debugging
92758680Seric **
92858680Seric **	Returns:
92958680Seric **		none.
93058680Seric **
93158680Seric **	Side Effects:
93258680Seric **		fp is closed.
93358680Seric */
93458680Seric 
93558680Seric xfclose(fp, a, b)
93658680Seric 	FILE *fp;
93758680Seric 	char *a, *b;
93858680Seric {
93958796Seric 	if (tTd(53, 99))
94058680Seric 		printf("xfclose(%x) %s %s\n", fp, a, b);
94164401Seric #ifdef XDEBUG
94264401Seric 	if (fileno(fp) == 1)
94364401Seric 		syserr("xfclose(%s %s): fd = 1", a, b);
94464401Seric #endif
94558796Seric 	if (fclose(fp) < 0 && tTd(53, 99))
94658680Seric 		printf("xfclose FAILURE: %s\n", errstring(errno));
94758680Seric }
94858680Seric /*
94914885Seric **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
9507685Seric **
9517685Seric **	Parameters:
9527685Seric **		buf -- place to put the input line.
9537685Seric **		siz -- size of buf.
9547685Seric **		fp -- file to read from.
95557384Seric **		timeout -- the timeout before error occurs.
95661093Seric **		during -- what we are trying to read (for error messages).
9577685Seric **
9587685Seric **	Returns:
95915533Seric **		NULL on error (including timeout).  This will also leave
96015533Seric **			buf containing a null string.
9617685Seric **		buf otherwise.
9627685Seric **
9637685Seric **	Side Effects:
9647685Seric **		none.
9657685Seric */
9667685Seric 
96714885Seric static jmp_buf	CtxReadTimeout;
96868481Seric static void	readtimeout();
9697685Seric 
9707685Seric char *
97161093Seric sfgets(buf, siz, fp, timeout, during)
9727685Seric 	char *buf;
9737685Seric 	int siz;
9747685Seric 	FILE *fp;
97557384Seric 	time_t timeout;
97661093Seric 	char *during;
9777685Seric {
9787942Seric 	register EVENT *ev = NULL;
9797685Seric 	register char *p;
9807685Seric 
98166332Seric 	if (fp == NULL)
98266332Seric 	{
98366332Seric 		buf[0] = '\0';
98466332Seric 		return NULL;
98566332Seric 	}
98666332Seric 
98714885Seric 	/* set the timeout */
98857384Seric 	if (timeout != 0)
98914885Seric 	{
99014885Seric 		if (setjmp(CtxReadTimeout) != 0)
99114885Seric 		{
99236233Skarels # ifdef LOG
99336230Skarels 			syslog(LOG_NOTICE,
99461093Seric 			    "timeout waiting for input from %s during %s\n",
99561093Seric 			    CurHostName? CurHostName: "local", during);
99636233Skarels # endif
99736230Skarels 			errno = 0;
99861093Seric 			usrerr("451 timeout waiting for input during %s",
99961093Seric 				during);
100019037Seric 			buf[0] = '\0';
100163753Seric #ifdef XDEBUG
100263753Seric 			checkfd012(during);
100363753Seric #endif
100414885Seric 			return (NULL);
100514885Seric 		}
100668481Seric 		ev = setevent(timeout, readtimeout, 0);
100714885Seric 	}
100814885Seric 
100914885Seric 	/* try to read */
101015533Seric 	p = NULL;
101165190Seric 	while (!feof(fp) && !ferror(fp))
10127942Seric 	{
10137942Seric 		errno = 0;
10147942Seric 		p = fgets(buf, siz, fp);
101565190Seric 		if (p != NULL || errno != EINTR)
101665186Seric 			break;
101765190Seric 		clearerr(fp);
101815533Seric 	}
101914885Seric 
102014885Seric 	/* clear the event if it has not sprung */
102168481Seric 	clrevent(ev);
102214885Seric 
102314885Seric 	/* clean up the books and exit */
10248055Seric 	LineNumber++;
102515533Seric 	if (p == NULL)
102616880Seric 	{
102715533Seric 		buf[0] = '\0';
102863753Seric 		if (TrafficLogFile != NULL)
102963753Seric 			fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid());
103016880Seric 		return (NULL);
103116880Seric 	}
103263753Seric 	if (TrafficLogFile != NULL)
103363753Seric 		fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf);
103468481Seric 	if (SevenBitInput)
103568481Seric 	{
103652106Seric 		for (p = buf; *p != '\0'; p++)
103752106Seric 			*p &= ~0200;
103867546Seric 	}
103968481Seric 	else if (!HasEightBits)
104067546Seric 	{
104168481Seric 		for (p = buf; *p != '\0'; p++)
104268481Seric 		{
104368481Seric 			if (bitset(0200, *p))
104468481Seric 			{
104568481Seric 				HasEightBits = TRUE;
104668481Seric 				break;
104768481Seric 			}
104868481Seric 		}
104967546Seric 	}
105068481Seric 	return (buf);
10517685Seric }
10527685Seric 
105368481Seric static void
105466765Seric readtimeout(timeout)
105566765Seric 	time_t timeout;
10567685Seric {
105768481Seric 	longjmp(CtxReadTimeout, 1);
10587685Seric }
10597786Seric /*
10607786Seric **  FGETFOLDED -- like fgets, but know about folded lines.
10617786Seric **
10627786Seric **	Parameters:
10637786Seric **		buf -- place to put result.
10647786Seric **		n -- bytes available.
10657786Seric **		f -- file to read from.
10667786Seric **
10677786Seric **	Returns:
106857135Seric **		input line(s) on success, NULL on error or EOF.
106957135Seric **		This will normally be buf -- unless the line is too
107057135Seric **			long, when it will be xalloc()ed.
10717786Seric **
10727786Seric **	Side Effects:
10737786Seric **		buf gets lines from f, with continuation lines (lines
10747786Seric **		with leading white space) appended.  CRLF's are mapped
10757786Seric **		into single newlines.  Any trailing NL is stripped.
10767786Seric */
10777786Seric 
10787786Seric char *
10797786Seric fgetfolded(buf, n, f)
10807786Seric 	char *buf;
10817786Seric 	register int n;
10827786Seric 	FILE *f;
10837786Seric {
10847786Seric 	register char *p = buf;
108557135Seric 	char *bp = buf;
10867786Seric 	register int i;
10877786Seric 
10887786Seric 	n--;
108917350Seric 	while ((i = getc(f)) != EOF)
10907786Seric 	{
109117350Seric 		if (i == '\r')
109217350Seric 		{
109317350Seric 			i = getc(f);
109417350Seric 			if (i != '\n')
109517350Seric 			{
109617350Seric 				if (i != EOF)
109723105Seric 					(void) ungetc(i, f);
109817350Seric 				i = '\r';
109917350Seric 			}
110017350Seric 		}
110157135Seric 		if (--n <= 0)
110257135Seric 		{
110357135Seric 			/* allocate new space */
110457135Seric 			char *nbp;
110557135Seric 			int nn;
110657135Seric 
110757135Seric 			nn = (p - bp);
110857232Seric 			if (nn < MEMCHUNKSIZE)
110957135Seric 				nn *= 2;
111057135Seric 			else
111157232Seric 				nn += MEMCHUNKSIZE;
111257135Seric 			nbp = xalloc(nn);
111357135Seric 			bcopy(bp, nbp, p - bp);
111457135Seric 			p = &nbp[p - bp];
111557135Seric 			if (bp != buf)
111657135Seric 				free(bp);
111757135Seric 			bp = nbp;
111857135Seric 			n = nn - (p - bp);
111957135Seric 		}
112057135Seric 		*p++ = i;
112117350Seric 		if (i == '\n')
112217350Seric 		{
112317350Seric 			LineNumber++;
112417350Seric 			i = getc(f);
112517350Seric 			if (i != EOF)
112623105Seric 				(void) ungetc(i, f);
112717350Seric 			if (i != ' ' && i != '\t')
112852647Seric 				break;
112917350Seric 		}
11307786Seric 	}
113157135Seric 	if (p == bp)
113252647Seric 		return (NULL);
113352647Seric 	*--p = '\0';
113457135Seric 	return (bp);
11357786Seric }
11367860Seric /*
11377886Seric **  CURTIME -- return current time.
11387886Seric **
11397886Seric **	Parameters:
11407886Seric **		none.
11417886Seric **
11427886Seric **	Returns:
11437886Seric **		the current time.
11447886Seric **
11457886Seric **	Side Effects:
11467886Seric **		none.
11477886Seric */
11487886Seric 
11497886Seric time_t
11507886Seric curtime()
11517886Seric {
11527886Seric 	auto time_t t;
11537886Seric 
11547886Seric 	(void) time(&t);
11557886Seric 	return (t);
11567886Seric }
11578264Seric /*
11588264Seric **  ATOBOOL -- convert a string representation to boolean.
11598264Seric **
11608264Seric **	Defaults to "TRUE"
11618264Seric **
11628264Seric **	Parameters:
11638264Seric **		s -- string to convert.  Takes "tTyY" as true,
11648264Seric **			others as false.
11658264Seric **
11668264Seric **	Returns:
11678264Seric **		A boolean representation of the string.
11688264Seric **
11698264Seric **	Side Effects:
11708264Seric **		none.
11718264Seric */
11728264Seric 
11738264Seric bool
11748264Seric atobool(s)
11758264Seric 	register char *s;
11768264Seric {
117763833Seric 	if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL)
11788264Seric 		return (TRUE);
11798264Seric 	return (FALSE);
11808264Seric }
11819048Seric /*
11829048Seric **  ATOOCT -- convert a string representation to octal.
11839048Seric **
11849048Seric **	Parameters:
11859048Seric **		s -- string to convert.
11869048Seric **
11879048Seric **	Returns:
11889048Seric **		An integer representing the string interpreted as an
11899048Seric **		octal number.
11909048Seric **
11919048Seric **	Side Effects:
11929048Seric **		none.
11939048Seric */
11949048Seric 
11959048Seric atooct(s)
11969048Seric 	register char *s;
11979048Seric {
11989048Seric 	register int i = 0;
11999048Seric 
12009048Seric 	while (*s >= '0' && *s <= '7')
12019048Seric 		i = (i << 3) | (*s++ - '0');
12029048Seric 	return (i);
12039048Seric }
12049376Seric /*
12059376Seric **  WAITFOR -- wait for a particular process id.
12069376Seric **
12079376Seric **	Parameters:
12089376Seric **		pid -- process id to wait for.
12099376Seric **
12109376Seric **	Returns:
12119376Seric **		status of pid.
12129376Seric **		-1 if pid never shows up.
12139376Seric **
12149376Seric **	Side Effects:
12159376Seric **		none.
12169376Seric */
12179376Seric 
121864562Seric int
12199376Seric waitfor(pid)
12209376Seric 	int pid;
12219376Seric {
122264562Seric #ifdef WAITUNION
122364562Seric 	union wait st;
122464562Seric #else
12259376Seric 	auto int st;
122664562Seric #endif
12279376Seric 	int i;
12289376Seric 
12299376Seric 	do
12309376Seric 	{
12319376Seric 		errno = 0;
12329376Seric 		i = wait(&st);
12339376Seric 	} while ((i >= 0 || errno == EINTR) && i != pid);
12349376Seric 	if (i < 0)
123564562Seric 		return -1;
123664562Seric #ifdef WAITUNION
123764562Seric 	return st.w_status;
123864562Seric #else
123964562Seric 	return st;
124064562Seric #endif
12419376Seric }
12429376Seric /*
124310685Seric **  BITINTERSECT -- tell if two bitmaps intersect
124410685Seric **
124510685Seric **	Parameters:
124610685Seric **		a, b -- the bitmaps in question
124710685Seric **
124810685Seric **	Returns:
124910685Seric **		TRUE if they have a non-null intersection
125010685Seric **		FALSE otherwise
125110685Seric **
125210685Seric **	Side Effects:
125310685Seric **		none.
125410685Seric */
125510685Seric 
125610685Seric bool
125710685Seric bitintersect(a, b)
125810685Seric 	BITMAP a;
125910685Seric 	BITMAP b;
126010685Seric {
126110685Seric 	int i;
126210685Seric 
126310685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
126410685Seric 		if ((a[i] & b[i]) != 0)
126510685Seric 			return (TRUE);
126610685Seric 	return (FALSE);
126710685Seric }
126810685Seric /*
126910685Seric **  BITZEROP -- tell if a bitmap is all zero
127010685Seric **
127110685Seric **	Parameters:
127210685Seric **		map -- the bit map to check
127310685Seric **
127410685Seric **	Returns:
127510685Seric **		TRUE if map is all zero.
127610685Seric **		FALSE if there are any bits set in map.
127710685Seric **
127810685Seric **	Side Effects:
127910685Seric **		none.
128010685Seric */
128110685Seric 
128210685Seric bool
128310685Seric bitzerop(map)
128410685Seric 	BITMAP map;
128510685Seric {
128610685Seric 	int i;
128710685Seric 
128810685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
128910685Seric 		if (map[i] != 0)
129010685Seric 			return (FALSE);
129110685Seric 	return (TRUE);
129210685Seric }
129358247Seric /*
129458318Seric **  STRCONTAINEDIN -- tell if one string is contained in another
129558318Seric **
129658318Seric **	Parameters:
129758318Seric **		a -- possible substring.
129858318Seric **		b -- possible superstring.
129958318Seric **
130058318Seric **	Returns:
130158318Seric **		TRUE if a is contained in b.
130258318Seric **		FALSE otherwise.
130358318Seric */
130458318Seric 
130558318Seric bool
130658318Seric strcontainedin(a, b)
130758318Seric 	register char *a;
130858318Seric 	register char *b;
130958318Seric {
131065012Seric 	int la;
131165012Seric 	int lb;
131265012Seric 	int c;
131358318Seric 
131465012Seric 	la = strlen(a);
131565012Seric 	lb = strlen(b);
131665012Seric 	c = *a;
131765012Seric 	if (isascii(c) && isupper(c))
131865012Seric 		c = tolower(c);
131965012Seric 	for (; lb-- >= la; b++)
132058318Seric 	{
132165012Seric 		if (*b != c && isascii(*b) && isupper(*b) && tolower(*b) != c)
132265012Seric 			continue;
132365012Seric 		if (strncasecmp(a, b, la) == 0)
132458318Seric 			return TRUE;
132558318Seric 	}
132665012Seric 	return FALSE;
132758318Seric }
132863753Seric /*
132963753Seric **  CHECKFD012 -- check low numbered file descriptors
133063753Seric **
133163753Seric **	File descriptors 0, 1, and 2 should be open at all times.
133263753Seric **	This routine verifies that, and fixes it if not true.
133363753Seric **
133463753Seric **	Parameters:
133563753Seric **		where -- a tag printed if the assertion failed
133663753Seric **
133763753Seric **	Returns:
133863753Seric **		none
133963753Seric */
134063753Seric 
134163753Seric checkfd012(where)
134263753Seric 	char *where;
134363753Seric {
134463753Seric #ifdef XDEBUG
134563753Seric 	register int i;
134663753Seric 	struct stat stbuf;
134763753Seric 
134863753Seric 	for (i = 0; i < 3; i++)
134963753Seric 	{
135064735Seric 		if (fstat(i, &stbuf) < 0 && errno != EOPNOTSUPP)
135163753Seric 		{
135263753Seric 			/* oops.... */
135363753Seric 			int fd;
135463753Seric 
135563753Seric 			syserr("%s: fd %d not open", where, i);
135663753Seric 			fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666);
135763753Seric 			if (fd != i)
135863753Seric 			{
135963753Seric 				(void) dup2(fd, i);
136063753Seric 				(void) close(fd);
136163753Seric 			}
136263753Seric 		}
136363753Seric 	}
136463937Seric #endif /* XDEBUG */
136563753Seric }
136664725Seric /*
136764725Seric **  PRINTOPENFDS -- print the open file descriptors (for debugging)
136864725Seric **
136964725Seric **	Parameters:
137064725Seric **		logit -- if set, send output to syslog; otherwise
137164725Seric **			print for debugging.
137264725Seric **
137364725Seric **	Returns:
137464725Seric **		none.
137564725Seric */
137664725Seric 
137764725Seric #include <netdb.h>
137864725Seric #include <arpa/inet.h>
137964725Seric 
138064725Seric printopenfds(logit)
138164725Seric 	bool logit;
138264725Seric {
138364725Seric 	register int fd;
138464743Seric 	extern int DtableSize;
138564743Seric 
138664743Seric 	for (fd = 0; fd < DtableSize; fd++)
138764743Seric 		dumpfd(fd, FALSE, logit);
138864743Seric }
138964743Seric /*
139064743Seric **  DUMPFD -- dump a file descriptor
139164743Seric **
139264743Seric **	Parameters:
139364743Seric **		fd -- the file descriptor to dump.
139464743Seric **		printclosed -- if set, print a notification even if
139564743Seric **			it is closed; otherwise print nothing.
139664743Seric **		logit -- if set, send output to syslog instead of stdout.
139764743Seric */
139864743Seric 
139964743Seric dumpfd(fd, printclosed, logit)
140064743Seric 	int fd;
140164743Seric 	bool printclosed;
140264743Seric 	bool logit;
140364743Seric {
140464725Seric 	register struct hostent *hp;
140564725Seric 	register char *p;
140666747Seric 	char *fmtstr;
140764743Seric 	struct sockaddr_in sin;
140864743Seric 	auto int slen;
140964743Seric 	struct stat st;
141064725Seric 	char buf[200];
141164725Seric 
141264743Seric 	p = buf;
141364743Seric 	sprintf(p, "%3d: ", fd);
141464743Seric 	p += strlen(p);
141564743Seric 
141664743Seric 	if (fstat(fd, &st) < 0)
141764725Seric 	{
141864743Seric 		if (printclosed || errno != EBADF)
141964743Seric 		{
142064743Seric 			sprintf(p, "CANNOT STAT (%s)", errstring(errno));
142164743Seric 			goto printit;
142264743Seric 		}
142364743Seric 		return;
142464743Seric 	}
142564725Seric 
142664743Seric 	slen = fcntl(fd, F_GETFL, NULL);
142764743Seric 	if (slen != -1)
142864743Seric 	{
142964743Seric 		sprintf(p, "fl=0x%x, ", slen);
143064743Seric 		p += strlen(p);
143164743Seric 	}
143264725Seric 
143364743Seric 	sprintf(p, "mode=%o: ", st.st_mode);
143464743Seric 	p += strlen(p);
143564743Seric 	switch (st.st_mode & S_IFMT)
143664743Seric 	{
143764807Seric #ifdef S_IFSOCK
143864743Seric 	  case S_IFSOCK:
143964743Seric 		sprintf(p, "SOCK ");
144064725Seric 		p += strlen(p);
144164743Seric 		slen = sizeof sin;
144264743Seric 		if (getsockname(fd, (struct sockaddr *) &sin, &slen) < 0)
144364743Seric 			sprintf(p, "(badsock)");
144464743Seric 		else
144564725Seric 		{
144668481Seric 			hp = gethostbyaddr((char *) &sin.sin_addr,
144768481Seric 					   INADDRSZ, AF_INET);
144864743Seric 			sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr)
144964743Seric 						   : hp->h_name, ntohs(sin.sin_port));
145064743Seric 		}
145164743Seric 		p += strlen(p);
145264743Seric 		sprintf(p, "->");
145364743Seric 		p += strlen(p);
145464743Seric 		slen = sizeof sin;
145564743Seric 		if (getpeername(fd, (struct sockaddr *) &sin, &slen) < 0)
145664743Seric 			sprintf(p, "(badsock)");
145764743Seric 		else
145864743Seric 		{
145968481Seric 			hp = gethostbyaddr((char *) &sin.sin_addr,
146068481Seric 					   INADDRSZ, AF_INET);
146164743Seric 			sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr)
146264743Seric 						   : hp->h_name, ntohs(sin.sin_port));
146364743Seric 		}
146464743Seric 		break;
146564807Seric #endif
146664725Seric 
146764743Seric 	  case S_IFCHR:
146864743Seric 		sprintf(p, "CHR: ");
146964743Seric 		p += strlen(p);
147064743Seric 		goto defprint;
147164725Seric 
147264743Seric 	  case S_IFBLK:
147364743Seric 		sprintf(p, "BLK: ");
147464743Seric 		p += strlen(p);
147564743Seric 		goto defprint;
147664725Seric 
147766252Seric #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK)
147865378Seric 	  case S_IFIFO:
147965378Seric 		sprintf(p, "FIFO: ");
148065378Seric 		p += strlen(p);
148165378Seric 		goto defprint;
148265378Seric #endif
148365378Seric 
148465378Seric #ifdef S_IFDIR
148565378Seric 	  case S_IFDIR:
148665378Seric 		sprintf(p, "DIR: ");
148765378Seric 		p += strlen(p);
148865378Seric 		goto defprint;
148965378Seric #endif
149065378Seric 
149165378Seric #ifdef S_IFLNK
149265378Seric 	  case S_IFLNK:
149365378Seric 		sprintf(p, "LNK: ");
149465378Seric 		p += strlen(p);
149565378Seric 		goto defprint;
149665378Seric #endif
149765378Seric 
149864743Seric 	  default:
149964725Seric defprint:
150066785Seric 		if (sizeof st.st_size > sizeof (long))
150166751Seric 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%qd";
150266747Seric 		else
150366751Seric 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld";
150466747Seric 		sprintf(p, fmtstr,
150564743Seric 			major(st.st_dev), minor(st.st_dev), st.st_ino,
150664743Seric 			st.st_nlink, st.st_uid, st.st_gid, st.st_size);
150764743Seric 		break;
150864743Seric 	}
150964725Seric 
151064743Seric printit:
151166748Seric #ifdef LOG
151264743Seric 	if (logit)
151365006Seric 		syslog(LOG_DEBUG, "%s", buf);
151464743Seric 	else
151566748Seric #endif
151664743Seric 		printf("%s\n", buf);
151764725Seric }
151865015Seric /*
151965015Seric **  SHORTENSTRING -- return short version of a string
152065015Seric **
152165015Seric **	If the string is already short, just return it.  If it is too
152265015Seric **	long, return the head and tail of the string.
152365015Seric **
152465015Seric **	Parameters:
152565015Seric **		s -- the string to shorten.
152665015Seric **		m -- the max length of the string.
152765015Seric **
152865015Seric **	Returns:
152965015Seric **		Either s or a short version of s.
153065015Seric */
153165015Seric 
153265015Seric #ifndef MAXSHORTSTR
153365055Seric # define MAXSHORTSTR	203
153465015Seric #endif
153565015Seric 
153665015Seric char *
153765015Seric shortenstring(s, m)
153865015Seric 	register char *s;
153965015Seric 	int m;
154065015Seric {
154165015Seric 	int l;
154265015Seric 	static char buf[MAXSHORTSTR + 1];
154365015Seric 
154465015Seric 	l = strlen(s);
154565015Seric 	if (l < m)
154665015Seric 		return s;
154765015Seric 	if (m > MAXSHORTSTR)
154865015Seric 		m = MAXSHORTSTR;
154965015Seric 	else if (m < 10)
155065015Seric 	{
155165015Seric 		if (m < 5)
155265015Seric 		{
155365015Seric 			strncpy(buf, s, m);
155465015Seric 			buf[m] = '\0';
155565015Seric 			return buf;
155665015Seric 		}
155765015Seric 		strncpy(buf, s, m - 3);
155865015Seric 		strcpy(buf + m - 3, "...");
155965015Seric 		return buf;
156065015Seric 	}
156165015Seric 	m = (m - 3) / 2;
156265015Seric 	strncpy(buf, s, m);
156365015Seric 	strcpy(buf + m, "...");
156465015Seric 	strcpy(buf + m + 3, s + l - m);
156565015Seric 	return buf;
156665015Seric }
156767848Seric /*
156868481Seric **  GET_COLUMN  -- look up a Column in a line buffer
156968481Seric **
157068481Seric **	Parameters:
157168481Seric **		line -- the raw text line to search.
157268481Seric **		col -- the column number to fetch.
157368481Seric **		delim -- the delimiter between columns.  If null,
157468481Seric **			use white space.
157568481Seric **		buf -- the output buffer.
157668481Seric **
157768481Seric **	Returns:
157868481Seric **		buf if successful.
157968481Seric **		NULL otherwise.
158068481Seric */
158168481Seric 
158268481Seric char *
158368481Seric get_column(line, col, delim, buf)
158468481Seric 	char line[];
158568481Seric 	int col;
158668481Seric 	char delim;
158768481Seric 	char buf[];
158868481Seric {
158968481Seric 	char *p;
159068481Seric 	char *begin, *end;
159168481Seric 	int i;
159268481Seric 	char delimbuf[3];
159368481Seric 
159468481Seric 	if (delim == '\0')
159568481Seric 		strcpy(delimbuf, "\t ");
159668481Seric 	else
159768481Seric 	{
159868481Seric 		delimbuf[0] = delim;
159968481Seric 		delimbuf[1] = '\0';
160068481Seric 	}
160168481Seric 
160268481Seric 	p = line;
160368481Seric 	if (*p == '\0')
160468481Seric 		return NULL;			/* line empty */
160568481Seric 	if (*p == delim && col == 0)
160668481Seric 		return NULL;			/* first column empty */
160768481Seric 
160868481Seric 	begin = line;
160968481Seric 
161068481Seric 	if (col == 0 && delim == '\0')
161168481Seric 	{
161268481Seric 		while (*begin && isspace(*begin))
161368481Seric 			begin++;
161468481Seric 	}
161568481Seric 
161668481Seric 	for (i = 0; i < col; i++)
161768481Seric 	{
161868481Seric 		if ((begin = strpbrk(begin, delimbuf)) == NULL)
161968481Seric 			return NULL;		/* no such column */
162068481Seric 		begin++;
162168481Seric 		if (delim == '\0')
162268481Seric 		{
162368481Seric 			while (*begin && isspace(*begin))
162468481Seric 				begin++;
162568481Seric 		}
162668481Seric 	}
162768481Seric 
162868481Seric 	end = strpbrk(begin, delimbuf);
162968481Seric 	if (end == NULL)
163068481Seric 	{
163168481Seric 		strcpy(buf, begin);
163268481Seric 	}
163368481Seric 	else
163468481Seric 	{
163568481Seric 		strncpy(buf, begin, end - begin);
163668481Seric 		buf[end - begin] = '\0';
163768481Seric 	}
163868481Seric 	return buf;
163968481Seric }
164068481Seric /*
164168267Seric **  CLEANSTRCPY -- copy string keeping out bogus characters
164268267Seric **
164368267Seric **	Parameters:
164468267Seric **		t -- "to" string.
164568267Seric **		f -- "from" string.
164668267Seric **		l -- length of space available in "to" string.
164768267Seric **
164868267Seric **	Returns:
164968267Seric **		none.
165068267Seric */
165168267Seric 
165268267Seric void
165368267Seric cleanstrcpy(t, f, l)
165468267Seric 	register char *t;
165568267Seric 	register char *f;
165668267Seric 	int l;
165768267Seric {
165868281Seric #ifdef LOG
165968281Seric 	/* check for newlines and log if necessary */
166068478Seric 	(void) denlstring(f, TRUE, TRUE);
166168281Seric #endif
166268281Seric 
166368267Seric 	l--;
166468267Seric 	while (l > 0 && *f != '\0')
166568267Seric 	{
166668267Seric 		if (isascii(*f) &&
166768267Seric 		    (isalnum(*f) || strchr("!#$%&'*+-./^_`{|}~", *f) != NULL))
166868267Seric 		{
166968267Seric 			l--;
167068267Seric 			*t++ = *f;
167168267Seric 		}
167268267Seric 		f++;
167368267Seric 	}
167468267Seric 	*t = '\0';
167568267Seric }
167668267Seric /*
167768267Seric **  DENLSTRING -- convert newlines in a string to spaces
167868267Seric **
167968267Seric **	Parameters:
168068267Seric **		s -- the input string
168168478Seric **		strict -- if set, don't permit continuation lines.
168268457Seric **		logattacks -- if set, log attempted attacks.
168368267Seric **
168468267Seric **	Returns:
168568267Seric **		A pointer to a version of the string with newlines
168668267Seric **		mapped to spaces.  This should be copied.
168768267Seric */
168868267Seric 
168968267Seric char *
169068478Seric denlstring(s, strict, logattacks)
169168267Seric 	char *s;
169268478Seric 	int strict;
169368465Seric 	int logattacks;
169468267Seric {
169568267Seric 	register char *p;
169668267Seric 	int l;
169768267Seric 	static char *bp = NULL;
169868267Seric 	static int bl = 0;
169968267Seric 
170068478Seric 	p = s;
170168478Seric 	while ((p = strchr(p, '\n')) != NULL)
170268478Seric 		if (strict || (*++p != ' ' && *p != '\t'))
170368478Seric 			break;
170468478Seric 	if (p == NULL)
170568267Seric 		return s;
170668267Seric 
170768267Seric 	l = strlen(s) + 1;
170868267Seric 	if (bl < l)
170968267Seric 	{
171068267Seric 		/* allocate more space */
171168267Seric 		if (bp != NULL)
171268267Seric 			free(bp);
171368267Seric 		bp = xalloc(l);
171468267Seric 		bl = l;
171568267Seric 	}
171668267Seric 	strcpy(bp, s);
171768267Seric 	for (p = bp; (p = strchr(p, '\n')) != NULL; )
171868267Seric 		*p++ = ' ';
171968281Seric 
172068481Seric /*
172168281Seric #ifdef LOG
172268457Seric 	if (logattacks)
172368457Seric 	{
172468457Seric 		syslog(LOG_NOTICE, "POSSIBLE ATTACK from %s: newline in string \"%s\"",
172568457Seric 			RealHostName == NULL ? "[UNKNOWN]" : RealHostName,
172668457Seric 			shortenstring(bp, 80));
172768457Seric 	}
172868281Seric #endif
172968481Seric */
173068281Seric 
173168267Seric 	return bp;
173268267Seric }
1733