xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 69773)
122717Sdist /*
268839Seric  * Copyright (c) 1983, 1995 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*69773Seric static char sccsid[] = "@(#)util.c	8.73 (Berkeley) 05/30/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 
3469748Seric void
3554983Seric stripquotes(s)
36298Seric 	char *s;
37298Seric {
38298Seric 	register char *p;
39298Seric 	register char *q;
40298Seric 	register char c;
41298Seric 
424101Seric 	if (s == NULL)
434101Seric 		return;
444101Seric 
4554983Seric 	p = q = s;
4654983Seric 	do
47298Seric 	{
4854983Seric 		c = *p++;
4954983Seric 		if (c == '\\')
5054983Seric 			c = *p++;
5154983Seric 		else if (c == '"')
5254983Seric 			continue;
5354983Seric 		*q++ = c;
5454983Seric 	} while (c != '\0');
55298Seric }
56298Seric /*
57298Seric **  XALLOC -- Allocate memory and bitch wildly on failure.
58298Seric **
59298Seric **	THIS IS A CLUDGE.  This should be made to give a proper
60298Seric **	error -- but after all, what can we do?
61298Seric **
62298Seric **	Parameters:
63298Seric **		sz -- size of area to allocate.
64298Seric **
65298Seric **	Returns:
66298Seric **		pointer to data region.
67298Seric **
68298Seric **	Side Effects:
69298Seric **		Memory is allocated.
70298Seric */
71298Seric 
72298Seric char *
73298Seric xalloc(sz)
747007Seric 	register int sz;
75298Seric {
76298Seric 	register char *p;
77298Seric 
7866747Seric 	/* some systems can't handle size zero mallocs */
7966747Seric 	if (sz <= 0)
8066747Seric 		sz = 1;
8166747Seric 
8223121Seric 	p = malloc((unsigned) sz);
83298Seric 	if (p == NULL)
84298Seric 	{
85298Seric 		syserr("Out of memory!!");
8610685Seric 		abort();
8710685Seric 		/* exit(EX_UNAVAILABLE); */
88298Seric 	}
89298Seric 	return (p);
90298Seric }
91298Seric /*
923151Seric **  COPYPLIST -- copy list of pointers.
933151Seric **
943151Seric **	This routine is the equivalent of newstr for lists of
953151Seric **	pointers.
963151Seric **
973151Seric **	Parameters:
983151Seric **		list -- list of pointers to copy.
993151Seric **			Must be NULL terminated.
1003151Seric **		copycont -- if TRUE, copy the contents of the vector
1013151Seric **			(which must be a string) also.
1023151Seric **
1033151Seric **	Returns:
1043151Seric **		a copy of 'list'.
1053151Seric **
1063151Seric **	Side Effects:
1073151Seric **		none.
1083151Seric */
1093151Seric 
1103151Seric char **
1113151Seric copyplist(list, copycont)
1123151Seric 	char **list;
1133151Seric 	bool copycont;
1143151Seric {
1153151Seric 	register char **vp;
1163151Seric 	register char **newvp;
1173151Seric 
1183151Seric 	for (vp = list; *vp != NULL; vp++)
1193151Seric 		continue;
1203151Seric 
1213151Seric 	vp++;
1223151Seric 
12316897Seric 	newvp = (char **) xalloc((int) (vp - list) * sizeof *vp);
12416897Seric 	bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp);
1253151Seric 
1263151Seric 	if (copycont)
1273151Seric 	{
1283151Seric 		for (vp = newvp; *vp != NULL; vp++)
1293151Seric 			*vp = newstr(*vp);
1303151Seric 	}
1313151Seric 
1323151Seric 	return (newvp);
1333151Seric }
1343151Seric /*
13558170Seric **  COPYQUEUE -- copy address queue.
13658170Seric **
13758170Seric **	This routine is the equivalent of newstr for address queues
13858170Seric **	addresses marked with QDONTSEND aren't copied
13958170Seric **
14058170Seric **	Parameters:
14158170Seric **		addr -- list of address structures to copy.
14258170Seric **
14358170Seric **	Returns:
14458170Seric **		a copy of 'addr'.
14558170Seric **
14658170Seric **	Side Effects:
14758170Seric **		none.
14858170Seric */
14958170Seric 
15058170Seric ADDRESS *
15158170Seric copyqueue(addr)
15258170Seric 	ADDRESS *addr;
15358170Seric {
15458170Seric 	register ADDRESS *newaddr;
15558170Seric 	ADDRESS *ret;
15658170Seric 	register ADDRESS **tail = &ret;
15758170Seric 
15858170Seric 	while (addr != NULL)
15958170Seric 	{
16058170Seric 		if (!bitset(QDONTSEND, addr->q_flags))
16158170Seric 		{
16258170Seric 			newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS));
16358170Seric 			STRUCTCOPY(*addr, *newaddr);
16458170Seric 			*tail = newaddr;
16558170Seric 			tail = &newaddr->q_next;
16658170Seric 		}
16758170Seric 		addr = addr->q_next;
16858170Seric 	}
16958170Seric 	*tail = NULL;
17058170Seric 
17158170Seric 	return ret;
17258170Seric }
17358170Seric /*
1743151Seric **  PRINTAV -- print argument vector.
1753151Seric **
1763151Seric **	Parameters:
1773151Seric **		av -- argument vector.
1783151Seric **
1793151Seric **	Returns:
1803151Seric **		none.
1813151Seric **
1823151Seric **	Side Effects:
1833151Seric **		prints av.
1843151Seric */
1853151Seric 
18669748Seric void
1873151Seric printav(av)
1883151Seric 	register char **av;
1893151Seric {
1903151Seric 	while (*av != NULL)
1913151Seric 	{
1928063Seric 		if (tTd(0, 44))
1938063Seric 			printf("\n\t%08x=", *av);
1948063Seric 		else
19523105Seric 			(void) putchar(' ');
1963151Seric 		xputs(*av++);
1973151Seric 	}
19823105Seric 	(void) putchar('\n');
1993151Seric }
2003151Seric /*
2013151Seric **  LOWER -- turn letter into lower case.
2023151Seric **
2033151Seric **	Parameters:
2043151Seric **		c -- character to turn into lower case.
2053151Seric **
2063151Seric **	Returns:
2073151Seric **		c, in lower case.
2083151Seric **
2093151Seric **	Side Effects:
2103151Seric **		none.
2113151Seric */
2123151Seric 
2133151Seric char
2143151Seric lower(c)
2153151Seric 	register char c;
2163151Seric {
21758050Seric 	return((isascii(c) && isupper(c)) ? tolower(c) : c);
2183151Seric }
2193151Seric /*
2203151Seric **  XPUTS -- put string doing control escapes.
2213151Seric **
2223151Seric **	Parameters:
2233151Seric **		s -- string to put.
2243151Seric **
2253151Seric **	Returns:
2263151Seric **		none.
2273151Seric **
2283151Seric **	Side Effects:
2293151Seric **		output to stdout
2303151Seric */
2313151Seric 
23269748Seric void
2333151Seric xputs(s)
23469748Seric 	register const char *s;
2353151Seric {
23658050Seric 	register int c;
23751781Seric 	register struct metamac *mp;
23851781Seric 	extern struct metamac MetaMacros[];
2393151Seric 
2408055Seric 	if (s == NULL)
2418055Seric 	{
2428055Seric 		printf("<null>");
2438055Seric 		return;
2448055Seric 	}
24558050Seric 	while ((c = (*s++ & 0377)) != '\0')
2463151Seric 	{
2473151Seric 		if (!isascii(c))
2483151Seric 		{
24968481Seric 			if (c == MATCHREPL)
25058050Seric 			{
25158050Seric 				putchar('$');
25258050Seric 				continue;
25358050Seric 			}
25468481Seric 			if (c == MACROEXPAND)
25568481Seric 			{
25668481Seric 				putchar('$');
25768481Seric 				if (bitset(0200, *s))
25868481Seric 					printf("{%s}", macname(*s++ & 0377));
25968481Seric 				continue;
26068481Seric 			}
26158050Seric 			for (mp = MetaMacros; mp->metaname != '\0'; mp++)
26258050Seric 			{
26358050Seric 				if ((mp->metaval & 0377) == c)
26458050Seric 				{
26558050Seric 					printf("$%c", mp->metaname);
26658050Seric 					break;
26758050Seric 				}
26858050Seric 			}
26958050Seric 			if (mp->metaname != '\0')
27058050Seric 				continue;
27123105Seric 			(void) putchar('\\');
2723151Seric 			c &= 0177;
2733151Seric 		}
27457589Seric 		if (isprint(c))
2753151Seric 		{
27657589Seric 			putchar(c);
27757589Seric 			continue;
27857589Seric 		}
27952050Seric 
28057589Seric 		/* wasn't a meta-macro -- find another way to print it */
28157589Seric 		switch (c)
28257589Seric 		{
28357589Seric 		  case '\n':
28457589Seric 			c = 'n';
28557589Seric 			break;
28652050Seric 
28757589Seric 		  case '\r':
28857589Seric 			c = 'r';
28957589Seric 			break;
29052637Seric 
29157589Seric 		  case '\t':
29257589Seric 			c = 't';
29357589Seric 			break;
29457589Seric 
29557589Seric 		  default:
29657589Seric 			(void) putchar('^');
29757589Seric 			(void) putchar(c ^ 0100);
29857589Seric 			continue;
2993151Seric 		}
30068500Seric 		(void) putchar('\\');
30168500Seric 		(void) putchar(c);
3023151Seric 	}
3034086Seric 	(void) fflush(stdout);
3043151Seric }
3053151Seric /*
3063151Seric **  MAKELOWER -- Translate a line into lower case
3073151Seric **
3083151Seric **	Parameters:
3093151Seric **		p -- the string to translate.  If NULL, return is
3103151Seric **			immediate.
3113151Seric **
3123151Seric **	Returns:
3133151Seric **		none.
3143151Seric **
3153151Seric **	Side Effects:
3163151Seric **		String pointed to by p is translated to lower case.
3173151Seric **
3183151Seric **	Called By:
3193151Seric **		parse
3203151Seric */
3213151Seric 
32268481Seric void
3233151Seric makelower(p)
3243151Seric 	register char *p;
3253151Seric {
3263151Seric 	register char c;
3273151Seric 
3283151Seric 	if (p == NULL)
3293151Seric 		return;
3303151Seric 	for (; (c = *p) != '\0'; p++)
3313151Seric 		if (isascii(c) && isupper(c))
33233724Sbostic 			*p = tolower(c);
3333151Seric }
3344059Seric /*
3355196Seric **  BUILDFNAME -- build full name from gecos style entry.
3364375Seric **
3375196Seric **	This routine interprets the strange entry that would appear
3385196Seric **	in the GECOS field of the password file.
3395196Seric **
3404375Seric **	Parameters:
3415196Seric **		p -- name to build.
3425196Seric **		login -- the login name of this user (for &).
3435196Seric **		buf -- place to put the result.
3444375Seric **
3454375Seric **	Returns:
34665006Seric **		none.
3474375Seric **
3484375Seric **	Side Effects:
3494375Seric **		none.
3504375Seric */
3514375Seric 
35269748Seric void
35354984Seric buildfname(gecos, login, buf)
35465006Seric 	register char *gecos;
35565006Seric 	char *login;
35665006Seric 	char *buf;
3574375Seric {
35865006Seric 	register char *p;
35965006Seric 	register char *bp = buf;
36065006Seric 	int l;
3614375Seric 
36265006Seric 	if (*gecos == '*')
36365006Seric 		gecos++;
36465006Seric 
36565006Seric 	/* find length of final string */
36665006Seric 	l = 0;
36765006Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
36854984Seric 	{
36965006Seric 		if (*p == '&')
37065006Seric 			l += strlen(login);
37165006Seric 		else
37265006Seric 			l++;
37354984Seric 	}
37465006Seric 
37565006Seric 	/* now fill in buf */
37665006Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
3774375Seric 	{
37865006Seric 		if (*p == '&')
3794375Seric 		{
38065006Seric 			(void) strcpy(bp, login);
38165006Seric 			*bp = toupper(*bp);
38265006Seric 			while (*bp != '\0')
38365006Seric 				bp++;
3844375Seric 		}
38565006Seric 		else
38665006Seric 			*bp++ = *p;
3874375Seric 	}
3884375Seric 	*bp = '\0';
3894375Seric }
3904375Seric /*
3914538Seric **  SAFEFILE -- return true if a file exists and is safe for a user.
3924538Seric **
3934538Seric **	Parameters:
3944538Seric **		fn -- filename to check.
39564083Seric **		uid -- user id to compare against.
39664083Seric **		gid -- group id to compare against.
39764083Seric **		uname -- user name to compare against (used for group
39864083Seric **			sets).
39964944Seric **		flags -- modifiers:
40065064Seric **			SFF_MUSTOWN -- "uid" must own this file.
40165064Seric **			SFF_NOSLINK -- file cannot be a symbolic link.
4024538Seric **		mode -- mode bits that must match.
40368494Seric **		st -- if set, points to a stat structure that will
40468494Seric **			get the stat info for the file.
4054538Seric **
4064538Seric **	Returns:
40758247Seric **		0 if fn exists, is owned by uid, and matches mode.
40858247Seric **		An errno otherwise.  The actual errno is cleared.
4094538Seric **
4104538Seric **	Side Effects:
4114538Seric **		none.
4124538Seric */
4134538Seric 
41464083Seric #include <grp.h>
41564083Seric 
41663581Seric #ifndef S_IXOTH
41763581Seric # define S_IXOTH	(S_IEXEC >> 6)
41863581Seric #endif
41963581Seric 
42064083Seric #ifndef S_IXGRP
42164083Seric # define S_IXGRP	(S_IEXEC >> 3)
42264083Seric #endif
42364083Seric 
42463753Seric #ifndef S_IXUSR
42563753Seric # define S_IXUSR	(S_IEXEC)
42663753Seric #endif
42763753Seric 
42868494Seric #define ST_MODE_NOFILE	0171147		/* unlikely to occur */
42968494Seric 
43058247Seric int
43168494Seric safefile(fn, uid, gid, uname, flags, mode, st)
4324538Seric 	char *fn;
43355372Seric 	uid_t uid;
43464083Seric 	gid_t gid;
43564083Seric 	char *uname;
43664944Seric 	int flags;
4374538Seric 	int mode;
43868494Seric 	struct stat *st;
4394538Seric {
44063581Seric 	register char *p;
44164083Seric 	register struct group *gr = NULL;
4424538Seric 	struct stat stbuf;
4434538Seric 
44463581Seric 	if (tTd(54, 4))
44564944Seric 		printf("safefile(%s, uid=%d, gid=%d, flags=%x, mode=%o):\n",
44664944Seric 			fn, uid, gid, flags, mode);
44763581Seric 	errno = 0;
44868494Seric 	if (st == NULL)
44968494Seric 		st = &stbuf;
45063581Seric 
45168481Seric 	if (!bitset(SFF_NOPATHCHECK, flags) ||
45268481Seric 	    (uid == 0 && !bitset(SFF_ROOTOK, flags)))
45363581Seric 	{
45468481Seric 		/* check the path to the file for acceptability */
45568481Seric 		for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/')
45665225Seric 		{
45768481Seric 			*p = '\0';
45868481Seric 			if (stat(fn, &stbuf) < 0)
45968481Seric 				break;
46068513Seric 			if (uid == 0 && bitset(S_IWGRP|S_IWOTH, stbuf.st_mode))
46168513Seric 				message("051 WARNING: writable directory %s",
46268513Seric 					fn);
46368481Seric 			if (uid == 0 && !bitset(SFF_ROOTOK, flags))
46468481Seric 			{
46568481Seric 				if (bitset(S_IXOTH, stbuf.st_mode))
46668481Seric 					continue;
46768481Seric 				break;
46868481Seric 			}
46968481Seric 			if (stbuf.st_uid == uid &&
47068481Seric 			    bitset(S_IXUSR, stbuf.st_mode))
47165225Seric 				continue;
47268481Seric 			if (stbuf.st_gid == gid &&
47368481Seric 			    bitset(S_IXGRP, stbuf.st_mode))
47468481Seric 				continue;
47568481Seric #ifndef NO_GROUP_SET
47668481Seric 			if (uname != NULL &&
47768481Seric 			    ((gr != NULL && gr->gr_gid == stbuf.st_gid) ||
47868481Seric 			     (gr = getgrgid(stbuf.st_gid)) != NULL))
47968481Seric 			{
48068481Seric 				register char **gp;
48168481Seric 
48268481Seric 				for (gp = gr->gr_mem; gp != NULL && *gp != NULL; gp++)
48368481Seric 					if (strcmp(*gp, uname) == 0)
48468481Seric 						break;
48568481Seric 				if (gp != NULL && *gp != NULL &&
48668481Seric 				    bitset(S_IXGRP, stbuf.st_mode))
48768481Seric 					continue;
48868481Seric 			}
48968481Seric #endif
49068481Seric 			if (!bitset(S_IXOTH, stbuf.st_mode))
49168481Seric 				break;
49268478Seric 		}
49368481Seric 		if (p != NULL)
49463581Seric 		{
49568481Seric 			int ret = errno;
49663581Seric 
49768481Seric 			if (ret == 0)
49868481Seric 				ret = EACCES;
49968481Seric 			if (tTd(54, 4))
50068481Seric 				printf("\t[dir %s] %s\n", fn, errstring(ret));
50168481Seric 			*p = '/';
50268481Seric 			return ret;
50363581Seric 		}
50463581Seric 	}
50563581Seric 
50664944Seric #ifdef HASLSTAT
50768494Seric 	if ((bitset(SFF_NOSLINK, flags) ? lstat(fn, st)
50868494Seric 					: stat(fn, st)) < 0)
50964944Seric #else
51068494Seric 	if (stat(fn, st) < 0)
51164944Seric #endif
51258247Seric 	{
51358247Seric 		int ret = errno;
51458247Seric 
51563581Seric 		if (tTd(54, 4))
51664083Seric 			printf("\t%s\n", errstring(ret));
51763581Seric 
51858247Seric 		errno = 0;
51968494Seric 		if (!bitset(SFF_CREAT, flags))
52068494Seric 			return ret;
52168494Seric 
52268494Seric 		/* check to see if legal to create the file */
52368494Seric 		p = strrchr(fn, '/');
52468494Seric 		if (p == NULL)
52568494Seric 			return ENOTDIR;
52668494Seric 		*p = '\0';
52768494Seric 		if (stat(fn, &stbuf) >= 0)
52868494Seric 		{
52968494Seric 			int md = S_IWRITE|S_IEXEC;
53068494Seric 			if (stbuf.st_uid != uid)
53168494Seric 				md >>= 6;
53268494Seric 			if ((stbuf.st_mode & md) != md)
53368494Seric 				errno = EACCES;
53468494Seric 		}
53568494Seric 		ret = errno;
53668494Seric 		if (tTd(54, 4))
53768494Seric 			printf("\t[final dir %s uid %d mode %o] %s\n",
53868494Seric 				fn, stbuf.st_uid, stbuf.st_mode,
53968494Seric 				errstring(ret));
54068494Seric 		*p = '/';
54168494Seric 		st->st_mode = ST_MODE_NOFILE;
54258247Seric 		return ret;
54358247Seric 	}
54464944Seric 
54564944Seric #ifdef S_ISLNK
54668494Seric 	if (bitset(SFF_NOSLINK, flags) && S_ISLNK(st->st_mode))
54764944Seric 	{
54864944Seric 		if (tTd(54, 4))
54968494Seric 			printf("\t[slink mode %o]\tEPERM\n", st->st_mode);
55064944Seric 		return EPERM;
55164944Seric 	}
55264944Seric #endif
55368513Seric 	if (bitset(SFF_REGONLY, flags) && !S_ISREG(st->st_mode))
55468513Seric 	{
55568513Seric 		if (tTd(54, 4))
55668513Seric 			printf("\t[non-reg mode %o]\tEPERM\n", st->st_mode);
55768513Seric 		return EPERM;
55868513Seric 	}
55968494Seric 	if (bitset(S_IWUSR|S_IWGRP|S_IWOTH, mode) && bitset(0111, st->st_mode))
56068494Seric 	{
56168494Seric 		if (tTd(29, 5))
56268494Seric 			printf("failed (mode %o: x bits)\n", st->st_mode);
56368513Seric 		return EPERM;
56468494Seric 	}
56568494Seric 
56668494Seric 	if (bitset(SFF_SETUIDOK, flags))
56768494Seric 	{
56868494Seric 		if (bitset(S_ISUID, st->st_mode) &&
56968494Seric 		    (st->st_uid != 0 || bitset(SFF_ROOTOK, flags)))
57068494Seric 		{
57168494Seric 			uid = st->st_uid;
57268494Seric 			uname = NULL;
57368494Seric 		}
57468494Seric 		if (bitset(S_ISGID, st->st_mode) &&
57568494Seric 		    (st->st_gid != 0 || bitset(SFF_ROOTOK, flags)))
57668494Seric 			gid = st->st_gid;
57768494Seric 	}
57868494Seric 
57965139Seric 	if (uid == 0 && !bitset(SFF_ROOTOK, flags))
58063581Seric 		mode >>= 6;
58168494Seric 	else if (st->st_uid != uid)
58264084Seric 	{
58364084Seric 		mode >>= 3;
58468494Seric 		if (st->st_gid == gid)
58564084Seric 			;
58664084Seric #ifndef NO_GROUP_SET
58764084Seric 		else if (uname != NULL &&
58868494Seric 			 ((gr != NULL && gr->gr_gid == st->st_gid) ||
58968494Seric 			  (gr = getgrgid(st->st_gid)) != NULL))
59064084Seric 		{
59164084Seric 			register char **gp;
59264084Seric 
59364084Seric 			for (gp = gr->gr_mem; *gp != NULL; gp++)
59464084Seric 				if (strcmp(*gp, uname) == 0)
59564084Seric 					break;
59664084Seric 			if (*gp == NULL)
59764084Seric 				mode >>= 3;
59864084Seric 		}
59964084Seric #endif
60064084Seric 		else
60164084Seric 			mode >>= 3;
60264084Seric 	}
60363581Seric 	if (tTd(54, 4))
60464084Seric 		printf("\t[uid %d, stat %o, mode %o] ",
60568494Seric 			st->st_uid, st->st_mode, mode);
60668494Seric 	if ((st->st_uid == uid || st->st_uid == 0 ||
60765064Seric 	     !bitset(SFF_MUSTOWN, flags)) &&
60868494Seric 	    (st->st_mode & mode) == mode)
60963581Seric 	{
61063581Seric 		if (tTd(54, 4))
61164083Seric 			printf("\tOK\n");
61258247Seric 		return 0;
61363581Seric 	}
61463581Seric 	if (tTd(54, 4))
61564083Seric 		printf("\tEACCES\n");
61663581Seric 	return EACCES;
6174538Seric }
6184538Seric /*
61968494Seric **  SAFEFOPEN -- do a file open with extra checking
62068494Seric **
62168494Seric **	Parameters:
62268494Seric **		fn -- the file name to open.
62368494Seric **		omode -- the open-style mode flags.
62468494Seric **		cmode -- the create-style mode flags.
62568494Seric **		sff -- safefile flags.
62668494Seric **
62768494Seric **	Returns:
62868494Seric **		Same as fopen.
62968494Seric */
63068494Seric 
63168703Seric #ifndef O_ACCMODE
63268703Seric # define O_ACCMODE	(O_RDONLY|O_WRONLY|O_RDWR)
63368703Seric #endif
63468703Seric 
63568494Seric FILE *
63668494Seric safefopen(fn, omode, cmode, sff)
63768494Seric 	char *fn;
63868494Seric 	int omode;
63968494Seric 	int cmode;
64068494Seric 	int sff;
64168494Seric {
64268494Seric 	int rval;
64368494Seric 	FILE *fp;
64468494Seric 	int smode;
64568494Seric 	struct stat stb, sta;
64668494Seric 	extern char RealUserName[];
64768494Seric 
64868494Seric 	if (bitset(O_CREAT, omode))
64968494Seric 		sff |= SFF_CREAT;
65068494Seric 	smode = 0;
65168494Seric 	switch (omode & O_ACCMODE)
65268494Seric 	{
65368494Seric 	  case O_RDONLY:
65468494Seric 		smode = S_IREAD;
65568494Seric 		break;
65668494Seric 
65768494Seric 	  case O_WRONLY:
65868494Seric 		smode = S_IWRITE;
65968494Seric 		break;
66068494Seric 
66168494Seric 	  case O_RDWR:
66268494Seric 		smode = S_IREAD|S_IWRITE;
66368494Seric 		break;
66468494Seric 
66568494Seric 	  default:
66668494Seric 		smode = 0;
66768494Seric 		break;
66868494Seric 	}
66968513Seric 	if (bitset(SFF_OPENASROOT, sff))
67068513Seric 		rval = safefile(fn, 0, 0, NULL, sff, smode, &stb);
67168513Seric 	else
67268513Seric 		rval = safefile(fn, RealUid, RealGid, RealUserName,
67368513Seric 				sff, smode, &stb);
67468494Seric 	if (rval != 0)
67568494Seric 	{
67668494Seric 		errno = rval;
67768494Seric 		return NULL;
67868494Seric 	}
67968494Seric 	if (stb.st_mode == ST_MODE_NOFILE)
68068494Seric 		omode |= O_EXCL;
68168494Seric 
68268494Seric 	fp = dfopen(fn, omode, cmode);
68368494Seric 	if (fp == NULL)
68468494Seric 		return NULL;
68568494Seric 	if (bitset(O_EXCL, omode))
68668494Seric 		return fp;
68768494Seric 	if (fstat(fileno(fp), &sta) < 0 ||
68868494Seric 	    sta.st_nlink != stb.st_nlink ||
68968494Seric 	    sta.st_dev != stb.st_dev ||
69068494Seric 	    sta.st_ino != stb.st_ino ||
69168494Seric 	    sta.st_uid != stb.st_uid ||
69268494Seric 	    sta.st_gid != stb.st_gid)
69368494Seric 	{
69468494Seric 		syserr("554 cannot open: file %s changed after open", fn);
69568513Seric 		fclose(fp);
69668494Seric 		errno = EPERM;
69768494Seric 		return NULL;
69868494Seric 	}
69968494Seric 	return fp;
70068494Seric }
70168494Seric /*
7024557Seric **  FIXCRLF -- fix <CR><LF> in line.
7034557Seric **
7044557Seric **	Looks for the <CR><LF> combination and turns it into the
7054557Seric **	UNIX canonical <NL> character.  It only takes one line,
7064557Seric **	i.e., it is assumed that the first <NL> found is the end
7074557Seric **	of the line.
7084557Seric **
7094557Seric **	Parameters:
7104557Seric **		line -- the line to fix.
7114557Seric **		stripnl -- if true, strip the newline also.
7124557Seric **
7134557Seric **	Returns:
7144557Seric **		none.
7154557Seric **
7164557Seric **	Side Effects:
7174557Seric **		line is changed in place.
7184557Seric */
7194557Seric 
72069748Seric void
7214557Seric fixcrlf(line, stripnl)
7224557Seric 	char *line;
7234557Seric 	bool stripnl;
7244557Seric {
7254557Seric 	register char *p;
7264557Seric 
72756795Seric 	p = strchr(line, '\n');
7284557Seric 	if (p == NULL)
7294557Seric 		return;
73036291Sbostic 	if (p > line && p[-1] == '\r')
7314557Seric 		p--;
7324557Seric 	if (!stripnl)
7334557Seric 		*p++ = '\n';
7344557Seric 	*p = '\0';
7354557Seric }
7364557Seric /*
7376890Seric **  DFOPEN -- determined file open
7386890Seric **
7396890Seric **	This routine has the semantics of fopen, except that it will
7406890Seric **	keep trying a few times to make this happen.  The idea is that
7416890Seric **	on very loaded systems, we may run out of resources (inodes,
7426890Seric **	whatever), so this tries to get around it.
7436890Seric */
7446890Seric 
74559745Seric struct omodes
74659745Seric {
74759745Seric 	int	mask;
74859745Seric 	int	mode;
74959745Seric 	char	*farg;
75059745Seric } OpenModes[] =
75159745Seric {
75259745Seric 	O_ACCMODE,		O_RDONLY,		"r",
75359745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY,		"w",
75459745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY|O_APPEND,	"a",
75559745Seric 	O_TRUNC,		0,			"w+",
75659745Seric 	O_APPEND,		O_APPEND,		"a+",
75759745Seric 	0,			0,			"r+",
75859745Seric };
75959745Seric 
7606890Seric FILE *
76159745Seric dfopen(filename, omode, cmode)
7626890Seric 	char *filename;
76359745Seric 	int omode;
76459745Seric 	int cmode;
7656890Seric {
7666890Seric 	register int tries;
76759745Seric 	int fd;
76859745Seric 	register struct omodes *om;
76959431Seric 	struct stat st;
7706890Seric 
77159745Seric 	for (om = OpenModes; om->mask != 0; om++)
77259745Seric 		if ((omode & om->mask) == om->mode)
77359745Seric 			break;
77459745Seric 
7756890Seric 	for (tries = 0; tries < 10; tries++)
7766890Seric 	{
77725618Seric 		sleep((unsigned) (10 * tries));
7786890Seric 		errno = 0;
77959745Seric 		fd = open(filename, omode, cmode);
78059745Seric 		if (fd >= 0)
7816890Seric 			break;
78266017Seric 		switch (errno)
78366017Seric 		{
78466017Seric 		  case ENFILE:		/* system file table full */
78566017Seric 		  case EINTR:		/* interrupted syscall */
78666017Seric #ifdef ETXTBSY
78766017Seric 		  case ETXTBSY:		/* Apollo: net file locked */
78866017Seric #endif
78966017Seric 			continue;
79066017Seric 		}
79166017Seric 		break;
7926890Seric 	}
79359745Seric 	if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode))
79456328Seric 	{
79556328Seric 		int locktype;
79656328Seric 
79756328Seric 		/* lock the file to avoid accidental conflicts */
79859745Seric 		if ((omode & O_ACCMODE) != O_RDONLY)
79956328Seric 			locktype = LOCK_EX;
80056328Seric 		else
80156328Seric 			locktype = LOCK_SH;
80264335Seric 		(void) lockfile(fd, filename, NULL, locktype);
80356328Seric 		errno = 0;
80456328Seric 	}
80563787Seric 	if (fd < 0)
80663787Seric 		return NULL;
80763787Seric 	else
80863787Seric 		return fdopen(fd, om->farg);
8096890Seric }
8107124Seric /*
8117124Seric **  PUTLINE -- put a line like fputs obeying SMTP conventions
8127124Seric **
8137753Seric **	This routine always guarantees outputing a newline (or CRLF,
8147753Seric **	as appropriate) at the end of the string.
8157753Seric **
8167124Seric **	Parameters:
8177124Seric **		l -- line to put.
81865870Seric **		mci -- the mailer connection information.
8197124Seric **
8207124Seric **	Returns:
8217124Seric **		none
8227124Seric **
8237124Seric **	Side Effects:
8247124Seric **		output of l to fp.
8257124Seric */
8267124Seric 
82769748Seric void
82865870Seric putline(l, mci)
8297753Seric 	register char *l;
83065870Seric 	register MCI *mci;
8317124Seric {
83268847Seric 	putxline(l, mci, PXLF_MAPFROM);
83368515Seric }
83468847Seric /*
83568847Seric **  PUTXLINE -- putline with flags bits.
83668847Seric **
83768847Seric **	This routine always guarantees outputing a newline (or CRLF,
83868847Seric **	as appropriate) at the end of the string.
83968847Seric **
84068847Seric **	Parameters:
84168847Seric **		l -- line to put.
84268847Seric **		mci -- the mailer connection information.
84368847Seric **		pxflags -- flag bits:
84468847Seric **		    PXLF_MAPFROM -- map From_ to >From_.
84568847Seric **		    PXLF_STRIP8BIT -- strip 8th bit.
84668847Seric **
84768847Seric **	Returns:
84868847Seric **		none
84968847Seric **
85068847Seric **	Side Effects:
85168847Seric **		output of l to fp.
85268847Seric */
85368515Seric 
85468515Seric void
85568847Seric putxline(l, mci, pxflags)
85668515Seric 	register char *l;
85768515Seric 	register MCI *mci;
85868847Seric 	int pxflags;
85968515Seric {
8607124Seric 	register char *p;
86147157Sbostic 	register char svchar;
86266004Seric 	int slop = 0;
8637124Seric 
86411275Seric 	/* strip out 0200 bits -- these can look like TELNET protocol */
86568847Seric 	if (bitset(MCIF_7BIT, mci->mci_flags) ||
86668847Seric 	    bitset(PXLF_STRIP8BIT, pxflags))
86711275Seric 	{
86861707Seric 		for (p = l; (svchar = *p) != '\0'; ++p)
86961707Seric 			if (bitset(0200, svchar))
87047157Sbostic 				*p = svchar &~ 0200;
87111275Seric 	}
87211275Seric 
8737753Seric 	do
8747124Seric 	{
8757753Seric 		/* find the end of the line */
87656795Seric 		p = strchr(l, '\n');
8777753Seric 		if (p == NULL)
8787753Seric 			p = &l[strlen(l)];
8797124Seric 
88063753Seric 		if (TrafficLogFile != NULL)
88163753Seric 			fprintf(TrafficLogFile, "%05d >>> ", getpid());
88263753Seric 
8837753Seric 		/* check for line overflow */
88465870Seric 		while (mci->mci_mailer->m_linelimit > 0 &&
88566004Seric 		       (p - l + slop) > mci->mci_mailer->m_linelimit)
8867753Seric 		{
88766004Seric 			register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1];
8887124Seric 
8897753Seric 			svchar = *q;
8907753Seric 			*q = '\0';
89166004Seric 			if (l[0] == '.' && slop == 0 &&
89266004Seric 			    bitnset(M_XDOT, mci->mci_mailer->m_flags))
89363753Seric 			{
89465870Seric 				(void) putc('.', mci->mci_out);
89563753Seric 				if (TrafficLogFile != NULL)
89663753Seric 					(void) putc('.', TrafficLogFile);
89763753Seric 			}
89868847Seric 			else if (l[0] == 'F' && slop == 0 &&
89968847Seric 				 bitset(PXLF_MAPFROM, pxflags) &&
90068847Seric 				 strncmp(l, "From ", 5) == 0 &&
90168515Seric 				 bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
90268515Seric 			{
90368515Seric 				(void) putc('>', mci->mci_out);
90468515Seric 				if (TrafficLogFile != NULL)
90568515Seric 					(void) putc('>', TrafficLogFile);
90668515Seric 			}
90765870Seric 			fputs(l, mci->mci_out);
90865870Seric 			(void) putc('!', mci->mci_out);
90965870Seric 			fputs(mci->mci_mailer->m_eol, mci->mci_out);
91066004Seric 			(void) putc(' ', mci->mci_out);
91163753Seric 			if (TrafficLogFile != NULL)
91266004Seric 				fprintf(TrafficLogFile, "%s!\n%05d >>>  ",
91363753Seric 					l, getpid());
9147753Seric 			*q = svchar;
9157753Seric 			l = q;
91666004Seric 			slop = 1;
9177753Seric 		}
9187124Seric 
9197753Seric 		/* output last part */
92066004Seric 		if (l[0] == '.' && slop == 0 &&
92166004Seric 		    bitnset(M_XDOT, mci->mci_mailer->m_flags))
92263753Seric 		{
92365870Seric 			(void) putc('.', mci->mci_out);
92463753Seric 			if (TrafficLogFile != NULL)
92563753Seric 				(void) putc('.', TrafficLogFile);
92663753Seric 		}
92763753Seric 		if (TrafficLogFile != NULL)
92863753Seric 			fprintf(TrafficLogFile, "%.*s\n", p - l, l);
92947157Sbostic 		for ( ; l < p; ++l)
93065870Seric 			(void) putc(*l, mci->mci_out);
93165870Seric 		fputs(mci->mci_mailer->m_eol, mci->mci_out);
9327753Seric 		if (*l == '\n')
93347157Sbostic 			++l;
9347753Seric 	} while (l[0] != '\0');
9357124Seric }
9367676Seric /*
9377676Seric **  XUNLINK -- unlink a file, doing logging as appropriate.
9387676Seric **
9397676Seric **	Parameters:
9407676Seric **		f -- name of file to unlink.
9417676Seric **
9427676Seric **	Returns:
9437676Seric **		none.
9447676Seric **
9457676Seric **	Side Effects:
9467676Seric **		f is unlinked.
9477676Seric */
9487676Seric 
94969748Seric void
9507676Seric xunlink(f)
9517676Seric 	char *f;
9527676Seric {
9537676Seric 	register int i;
9547676Seric 
9557676Seric # ifdef LOG
95658020Seric 	if (LogLevel > 98)
95758020Seric 		syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f);
95856795Seric # endif /* LOG */
9597676Seric 
9607676Seric 	i = unlink(f);
9617676Seric # ifdef LOG
96258020Seric 	if (i < 0 && LogLevel > 97)
9637942Seric 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
96456795Seric # endif /* LOG */
9657676Seric }
9667685Seric /*
96758680Seric **  XFCLOSE -- close a file, doing logging as appropriate.
96858680Seric **
96958680Seric **	Parameters:
97058680Seric **		fp -- file pointer for the file to close
97158680Seric **		a, b -- miscellaneous crud to print for debugging
97258680Seric **
97358680Seric **	Returns:
97458680Seric **		none.
97558680Seric **
97658680Seric **	Side Effects:
97758680Seric **		fp is closed.
97858680Seric */
97958680Seric 
98069748Seric void
98158680Seric xfclose(fp, a, b)
98258680Seric 	FILE *fp;
98358680Seric 	char *a, *b;
98458680Seric {
98558796Seric 	if (tTd(53, 99))
98658680Seric 		printf("xfclose(%x) %s %s\n", fp, a, b);
98764401Seric #ifdef XDEBUG
98864401Seric 	if (fileno(fp) == 1)
98964401Seric 		syserr("xfclose(%s %s): fd = 1", a, b);
99064401Seric #endif
99158796Seric 	if (fclose(fp) < 0 && tTd(53, 99))
99258680Seric 		printf("xfclose FAILURE: %s\n", errstring(errno));
99358680Seric }
99458680Seric /*
99514885Seric **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
9967685Seric **
9977685Seric **	Parameters:
9987685Seric **		buf -- place to put the input line.
9997685Seric **		siz -- size of buf.
10007685Seric **		fp -- file to read from.
100157384Seric **		timeout -- the timeout before error occurs.
100261093Seric **		during -- what we are trying to read (for error messages).
10037685Seric **
10047685Seric **	Returns:
100515533Seric **		NULL on error (including timeout).  This will also leave
100615533Seric **			buf containing a null string.
10077685Seric **		buf otherwise.
10087685Seric **
10097685Seric **	Side Effects:
10107685Seric **		none.
10117685Seric */
10127685Seric 
101314885Seric static jmp_buf	CtxReadTimeout;
101468481Seric static void	readtimeout();
10157685Seric 
10167685Seric char *
101761093Seric sfgets(buf, siz, fp, timeout, during)
10187685Seric 	char *buf;
10197685Seric 	int siz;
10207685Seric 	FILE *fp;
102157384Seric 	time_t timeout;
102261093Seric 	char *during;
10237685Seric {
10247942Seric 	register EVENT *ev = NULL;
10257685Seric 	register char *p;
10267685Seric 
102766332Seric 	if (fp == NULL)
102866332Seric 	{
102966332Seric 		buf[0] = '\0';
103066332Seric 		return NULL;
103166332Seric 	}
103266332Seric 
103314885Seric 	/* set the timeout */
103457384Seric 	if (timeout != 0)
103514885Seric 	{
103614885Seric 		if (setjmp(CtxReadTimeout) != 0)
103714885Seric 		{
103836233Skarels # ifdef LOG
103936230Skarels 			syslog(LOG_NOTICE,
104061093Seric 			    "timeout waiting for input from %s during %s\n",
104161093Seric 			    CurHostName? CurHostName: "local", during);
104236233Skarels # endif
104336230Skarels 			errno = 0;
104461093Seric 			usrerr("451 timeout waiting for input during %s",
104561093Seric 				during);
104619037Seric 			buf[0] = '\0';
104763753Seric #ifdef XDEBUG
104863753Seric 			checkfd012(during);
104963753Seric #endif
105014885Seric 			return (NULL);
105114885Seric 		}
105268481Seric 		ev = setevent(timeout, readtimeout, 0);
105314885Seric 	}
105414885Seric 
105514885Seric 	/* try to read */
105615533Seric 	p = NULL;
105765190Seric 	while (!feof(fp) && !ferror(fp))
10587942Seric 	{
10597942Seric 		errno = 0;
10607942Seric 		p = fgets(buf, siz, fp);
106165190Seric 		if (p != NULL || errno != EINTR)
106265186Seric 			break;
106365190Seric 		clearerr(fp);
106415533Seric 	}
106514885Seric 
106614885Seric 	/* clear the event if it has not sprung */
106768481Seric 	clrevent(ev);
106814885Seric 
106914885Seric 	/* clean up the books and exit */
10708055Seric 	LineNumber++;
107115533Seric 	if (p == NULL)
107216880Seric 	{
107315533Seric 		buf[0] = '\0';
107463753Seric 		if (TrafficLogFile != NULL)
107563753Seric 			fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid());
107616880Seric 		return (NULL);
107716880Seric 	}
107863753Seric 	if (TrafficLogFile != NULL)
107963753Seric 		fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf);
108068481Seric 	if (SevenBitInput)
108168481Seric 	{
108252106Seric 		for (p = buf; *p != '\0'; p++)
108352106Seric 			*p &= ~0200;
108467546Seric 	}
108568481Seric 	else if (!HasEightBits)
108667546Seric 	{
108768481Seric 		for (p = buf; *p != '\0'; p++)
108868481Seric 		{
108968481Seric 			if (bitset(0200, *p))
109068481Seric 			{
109168481Seric 				HasEightBits = TRUE;
109268481Seric 				break;
109368481Seric 			}
109468481Seric 		}
109567546Seric 	}
109668481Seric 	return (buf);
10977685Seric }
10987685Seric 
109968481Seric static void
110066765Seric readtimeout(timeout)
110166765Seric 	time_t timeout;
11027685Seric {
110368481Seric 	longjmp(CtxReadTimeout, 1);
11047685Seric }
11057786Seric /*
11067786Seric **  FGETFOLDED -- like fgets, but know about folded lines.
11077786Seric **
11087786Seric **	Parameters:
11097786Seric **		buf -- place to put result.
11107786Seric **		n -- bytes available.
11117786Seric **		f -- file to read from.
11127786Seric **
11137786Seric **	Returns:
111457135Seric **		input line(s) on success, NULL on error or EOF.
111557135Seric **		This will normally be buf -- unless the line is too
111657135Seric **			long, when it will be xalloc()ed.
11177786Seric **
11187786Seric **	Side Effects:
11197786Seric **		buf gets lines from f, with continuation lines (lines
11207786Seric **		with leading white space) appended.  CRLF's are mapped
11217786Seric **		into single newlines.  Any trailing NL is stripped.
11227786Seric */
11237786Seric 
11247786Seric char *
11257786Seric fgetfolded(buf, n, f)
11267786Seric 	char *buf;
11277786Seric 	register int n;
11287786Seric 	FILE *f;
11297786Seric {
11307786Seric 	register char *p = buf;
113157135Seric 	char *bp = buf;
11327786Seric 	register int i;
11337786Seric 
11347786Seric 	n--;
113517350Seric 	while ((i = getc(f)) != EOF)
11367786Seric 	{
113717350Seric 		if (i == '\r')
113817350Seric 		{
113917350Seric 			i = getc(f);
114017350Seric 			if (i != '\n')
114117350Seric 			{
114217350Seric 				if (i != EOF)
114323105Seric 					(void) ungetc(i, f);
114417350Seric 				i = '\r';
114517350Seric 			}
114617350Seric 		}
114757135Seric 		if (--n <= 0)
114857135Seric 		{
114957135Seric 			/* allocate new space */
115057135Seric 			char *nbp;
115157135Seric 			int nn;
115257135Seric 
115357135Seric 			nn = (p - bp);
115457232Seric 			if (nn < MEMCHUNKSIZE)
115557135Seric 				nn *= 2;
115657135Seric 			else
115757232Seric 				nn += MEMCHUNKSIZE;
115857135Seric 			nbp = xalloc(nn);
115957135Seric 			bcopy(bp, nbp, p - bp);
116057135Seric 			p = &nbp[p - bp];
116157135Seric 			if (bp != buf)
116257135Seric 				free(bp);
116357135Seric 			bp = nbp;
116457135Seric 			n = nn - (p - bp);
116557135Seric 		}
116657135Seric 		*p++ = i;
116717350Seric 		if (i == '\n')
116817350Seric 		{
116917350Seric 			LineNumber++;
117017350Seric 			i = getc(f);
117117350Seric 			if (i != EOF)
117223105Seric 				(void) ungetc(i, f);
117317350Seric 			if (i != ' ' && i != '\t')
117452647Seric 				break;
117517350Seric 		}
11767786Seric 	}
117757135Seric 	if (p == bp)
117852647Seric 		return (NULL);
117969697Seric 	if (p[-1] == '\n')
118069697Seric 		p--;
118169697Seric 	*p = '\0';
118257135Seric 	return (bp);
11837786Seric }
11847860Seric /*
11857886Seric **  CURTIME -- return current time.
11867886Seric **
11877886Seric **	Parameters:
11887886Seric **		none.
11897886Seric **
11907886Seric **	Returns:
11917886Seric **		the current time.
11927886Seric **
11937886Seric **	Side Effects:
11947886Seric **		none.
11957886Seric */
11967886Seric 
11977886Seric time_t
11987886Seric curtime()
11997886Seric {
12007886Seric 	auto time_t t;
12017886Seric 
12027886Seric 	(void) time(&t);
12037886Seric 	return (t);
12047886Seric }
12058264Seric /*
12068264Seric **  ATOBOOL -- convert a string representation to boolean.
12078264Seric **
12088264Seric **	Defaults to "TRUE"
12098264Seric **
12108264Seric **	Parameters:
12118264Seric **		s -- string to convert.  Takes "tTyY" as true,
12128264Seric **			others as false.
12138264Seric **
12148264Seric **	Returns:
12158264Seric **		A boolean representation of the string.
12168264Seric **
12178264Seric **	Side Effects:
12188264Seric **		none.
12198264Seric */
12208264Seric 
12218264Seric bool
12228264Seric atobool(s)
12238264Seric 	register char *s;
12248264Seric {
122563833Seric 	if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL)
12268264Seric 		return (TRUE);
12278264Seric 	return (FALSE);
12288264Seric }
12299048Seric /*
12309048Seric **  ATOOCT -- convert a string representation to octal.
12319048Seric **
12329048Seric **	Parameters:
12339048Seric **		s -- string to convert.
12349048Seric **
12359048Seric **	Returns:
12369048Seric **		An integer representing the string interpreted as an
12379048Seric **		octal number.
12389048Seric **
12399048Seric **	Side Effects:
12409048Seric **		none.
12419048Seric */
12429048Seric 
124369748Seric int
12449048Seric atooct(s)
12459048Seric 	register char *s;
12469048Seric {
12479048Seric 	register int i = 0;
12489048Seric 
12499048Seric 	while (*s >= '0' && *s <= '7')
12509048Seric 		i = (i << 3) | (*s++ - '0');
12519048Seric 	return (i);
12529048Seric }
12539376Seric /*
12549376Seric **  WAITFOR -- wait for a particular process id.
12559376Seric **
12569376Seric **	Parameters:
12579376Seric **		pid -- process id to wait for.
12589376Seric **
12599376Seric **	Returns:
12609376Seric **		status of pid.
12619376Seric **		-1 if pid never shows up.
12629376Seric **
12639376Seric **	Side Effects:
12649376Seric **		none.
12659376Seric */
12669376Seric 
126764562Seric int
12689376Seric waitfor(pid)
12699376Seric 	int pid;
12709376Seric {
127164562Seric #ifdef WAITUNION
127264562Seric 	union wait st;
127364562Seric #else
12749376Seric 	auto int st;
127564562Seric #endif
12769376Seric 	int i;
12779376Seric 
12789376Seric 	do
12799376Seric 	{
12809376Seric 		errno = 0;
12819376Seric 		i = wait(&st);
12829376Seric 	} while ((i >= 0 || errno == EINTR) && i != pid);
12839376Seric 	if (i < 0)
128464562Seric 		return -1;
128564562Seric #ifdef WAITUNION
128664562Seric 	return st.w_status;
128764562Seric #else
128864562Seric 	return st;
128964562Seric #endif
12909376Seric }
12919376Seric /*
129210685Seric **  BITINTERSECT -- tell if two bitmaps intersect
129310685Seric **
129410685Seric **	Parameters:
129510685Seric **		a, b -- the bitmaps in question
129610685Seric **
129710685Seric **	Returns:
129810685Seric **		TRUE if they have a non-null intersection
129910685Seric **		FALSE otherwise
130010685Seric **
130110685Seric **	Side Effects:
130210685Seric **		none.
130310685Seric */
130410685Seric 
130510685Seric bool
130610685Seric bitintersect(a, b)
130710685Seric 	BITMAP a;
130810685Seric 	BITMAP b;
130910685Seric {
131010685Seric 	int i;
131110685Seric 
131210685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
131310685Seric 		if ((a[i] & b[i]) != 0)
131410685Seric 			return (TRUE);
131510685Seric 	return (FALSE);
131610685Seric }
131710685Seric /*
131810685Seric **  BITZEROP -- tell if a bitmap is all zero
131910685Seric **
132010685Seric **	Parameters:
132110685Seric **		map -- the bit map to check
132210685Seric **
132310685Seric **	Returns:
132410685Seric **		TRUE if map is all zero.
132510685Seric **		FALSE if there are any bits set in map.
132610685Seric **
132710685Seric **	Side Effects:
132810685Seric **		none.
132910685Seric */
133010685Seric 
133110685Seric bool
133210685Seric bitzerop(map)
133310685Seric 	BITMAP map;
133410685Seric {
133510685Seric 	int i;
133610685Seric 
133710685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
133810685Seric 		if (map[i] != 0)
133910685Seric 			return (FALSE);
134010685Seric 	return (TRUE);
134110685Seric }
134258247Seric /*
134358318Seric **  STRCONTAINEDIN -- tell if one string is contained in another
134458318Seric **
134558318Seric **	Parameters:
134658318Seric **		a -- possible substring.
134758318Seric **		b -- possible superstring.
134858318Seric **
134958318Seric **	Returns:
135058318Seric **		TRUE if a is contained in b.
135158318Seric **		FALSE otherwise.
135258318Seric */
135358318Seric 
135458318Seric bool
135558318Seric strcontainedin(a, b)
135658318Seric 	register char *a;
135758318Seric 	register char *b;
135858318Seric {
135965012Seric 	int la;
136065012Seric 	int lb;
136165012Seric 	int c;
136258318Seric 
136365012Seric 	la = strlen(a);
136465012Seric 	lb = strlen(b);
136565012Seric 	c = *a;
136665012Seric 	if (isascii(c) && isupper(c))
136765012Seric 		c = tolower(c);
136865012Seric 	for (; lb-- >= la; b++)
136958318Seric 	{
137065012Seric 		if (*b != c && isascii(*b) && isupper(*b) && tolower(*b) != c)
137165012Seric 			continue;
137265012Seric 		if (strncasecmp(a, b, la) == 0)
137358318Seric 			return TRUE;
137458318Seric 	}
137565012Seric 	return FALSE;
137658318Seric }
137763753Seric /*
137863753Seric **  CHECKFD012 -- check low numbered file descriptors
137963753Seric **
138063753Seric **	File descriptors 0, 1, and 2 should be open at all times.
138163753Seric **	This routine verifies that, and fixes it if not true.
138263753Seric **
138363753Seric **	Parameters:
138463753Seric **		where -- a tag printed if the assertion failed
138563753Seric **
138663753Seric **	Returns:
138763753Seric **		none
138863753Seric */
138963753Seric 
139069748Seric void
139163753Seric checkfd012(where)
139263753Seric 	char *where;
139363753Seric {
139463753Seric #ifdef XDEBUG
139563753Seric 	register int i;
139663753Seric 	struct stat stbuf;
139763753Seric 
139863753Seric 	for (i = 0; i < 3; i++)
139963753Seric 	{
140064735Seric 		if (fstat(i, &stbuf) < 0 && errno != EOPNOTSUPP)
140163753Seric 		{
140263753Seric 			/* oops.... */
140363753Seric 			int fd;
140463753Seric 
140563753Seric 			syserr("%s: fd %d not open", where, i);
140663753Seric 			fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666);
140763753Seric 			if (fd != i)
140863753Seric 			{
140963753Seric 				(void) dup2(fd, i);
141063753Seric 				(void) close(fd);
141163753Seric 			}
141263753Seric 		}
141363753Seric 	}
141463937Seric #endif /* XDEBUG */
141563753Seric }
141664725Seric /*
141764725Seric **  PRINTOPENFDS -- print the open file descriptors (for debugging)
141864725Seric **
141964725Seric **	Parameters:
142064725Seric **		logit -- if set, send output to syslog; otherwise
142164725Seric **			print for debugging.
142264725Seric **
142364725Seric **	Returns:
142464725Seric **		none.
142564725Seric */
142664725Seric 
142764725Seric #include <arpa/inet.h>
142864725Seric 
142969748Seric void
143064725Seric printopenfds(logit)
143164725Seric 	bool logit;
143264725Seric {
143364725Seric 	register int fd;
143464743Seric 	extern int DtableSize;
143564743Seric 
143664743Seric 	for (fd = 0; fd < DtableSize; fd++)
143764743Seric 		dumpfd(fd, FALSE, logit);
143864743Seric }
143964743Seric /*
144064743Seric **  DUMPFD -- dump a file descriptor
144164743Seric **
144264743Seric **	Parameters:
144364743Seric **		fd -- the file descriptor to dump.
144464743Seric **		printclosed -- if set, print a notification even if
144564743Seric **			it is closed; otherwise print nothing.
144664743Seric **		logit -- if set, send output to syslog instead of stdout.
144764743Seric */
144864743Seric 
144969748Seric void
145064743Seric dumpfd(fd, printclosed, logit)
145164743Seric 	int fd;
145264743Seric 	bool printclosed;
145364743Seric 	bool logit;
145464743Seric {
145564725Seric 	register char *p;
145668777Seric 	char *hp;
145766747Seric 	char *fmtstr;
145868777Seric 	SOCKADDR sa;
145964743Seric 	auto int slen;
146064743Seric 	struct stat st;
146164725Seric 	char buf[200];
146268777Seric 	extern char *hostnamebyanyaddr();
146364725Seric 
146464743Seric 	p = buf;
146564743Seric 	sprintf(p, "%3d: ", fd);
146664743Seric 	p += strlen(p);
146764743Seric 
146864743Seric 	if (fstat(fd, &st) < 0)
146964725Seric 	{
147064743Seric 		if (printclosed || errno != EBADF)
147164743Seric 		{
147264743Seric 			sprintf(p, "CANNOT STAT (%s)", errstring(errno));
147364743Seric 			goto printit;
147464743Seric 		}
147564743Seric 		return;
147664743Seric 	}
147764725Seric 
147864743Seric 	slen = fcntl(fd, F_GETFL, NULL);
147964743Seric 	if (slen != -1)
148064743Seric 	{
148164743Seric 		sprintf(p, "fl=0x%x, ", slen);
148264743Seric 		p += strlen(p);
148364743Seric 	}
148464725Seric 
148564743Seric 	sprintf(p, "mode=%o: ", st.st_mode);
148664743Seric 	p += strlen(p);
148764743Seric 	switch (st.st_mode & S_IFMT)
148864743Seric 	{
148964807Seric #ifdef S_IFSOCK
149064743Seric 	  case S_IFSOCK:
149164743Seric 		sprintf(p, "SOCK ");
149264725Seric 		p += strlen(p);
149368777Seric 		slen = sizeof sa;
149468777Seric 		if (getsockname(fd, &sa.sa, &slen) < 0)
149568810Seric 			sprintf(p, "(%s)", errstring(errno));
149664743Seric 		else
149764725Seric 		{
149868777Seric 			hp = hostnamebyanyaddr(&sa);
149968777Seric 			if (sa.sa.sa_family == AF_INET)
150068777Seric 				sprintf(p, "%s/%d", hp, ntohs(sa.sin.sin_port));
150168777Seric 			else
150268777Seric 				sprintf(p, "%s", hp);
150364743Seric 		}
150464743Seric 		p += strlen(p);
150564743Seric 		sprintf(p, "->");
150664743Seric 		p += strlen(p);
150768777Seric 		slen = sizeof sa;
150868777Seric 		if (getpeername(fd, &sa.sa, &slen) < 0)
150968810Seric 			sprintf(p, "(%s)", errstring(errno));
151064743Seric 		else
151164743Seric 		{
151268777Seric 			hp = hostnamebyanyaddr(&sa);
151368777Seric 			if (sa.sa.sa_family == AF_INET)
151468777Seric 				sprintf(p, "%s/%d", hp, ntohs(sa.sin.sin_port));
151568777Seric 			else
151668777Seric 				sprintf(p, "%s", hp);
151764743Seric 		}
151864743Seric 		break;
151964807Seric #endif
152064725Seric 
152164743Seric 	  case S_IFCHR:
152264743Seric 		sprintf(p, "CHR: ");
152364743Seric 		p += strlen(p);
152464743Seric 		goto defprint;
152564725Seric 
152664743Seric 	  case S_IFBLK:
152764743Seric 		sprintf(p, "BLK: ");
152864743Seric 		p += strlen(p);
152964743Seric 		goto defprint;
153064725Seric 
153166252Seric #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK)
153265378Seric 	  case S_IFIFO:
153365378Seric 		sprintf(p, "FIFO: ");
153465378Seric 		p += strlen(p);
153565378Seric 		goto defprint;
153665378Seric #endif
153765378Seric 
153865378Seric #ifdef S_IFDIR
153965378Seric 	  case S_IFDIR:
154065378Seric 		sprintf(p, "DIR: ");
154165378Seric 		p += strlen(p);
154265378Seric 		goto defprint;
154365378Seric #endif
154465378Seric 
154565378Seric #ifdef S_IFLNK
154665378Seric 	  case S_IFLNK:
154765378Seric 		sprintf(p, "LNK: ");
154865378Seric 		p += strlen(p);
154965378Seric 		goto defprint;
155065378Seric #endif
155165378Seric 
155264743Seric 	  default:
155364725Seric defprint:
155466785Seric 		if (sizeof st.st_size > sizeof (long))
155566751Seric 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%qd";
155666747Seric 		else
155766751Seric 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld";
155866747Seric 		sprintf(p, fmtstr,
155964743Seric 			major(st.st_dev), minor(st.st_dev), st.st_ino,
156064743Seric 			st.st_nlink, st.st_uid, st.st_gid, st.st_size);
156164743Seric 		break;
156264743Seric 	}
156364725Seric 
156464743Seric printit:
156566748Seric #ifdef LOG
156664743Seric 	if (logit)
156765006Seric 		syslog(LOG_DEBUG, "%s", buf);
156864743Seric 	else
156966748Seric #endif
157064743Seric 		printf("%s\n", buf);
157164725Seric }
157265015Seric /*
157365015Seric **  SHORTENSTRING -- return short version of a string
157465015Seric **
157565015Seric **	If the string is already short, just return it.  If it is too
157665015Seric **	long, return the head and tail of the string.
157765015Seric **
157865015Seric **	Parameters:
157965015Seric **		s -- the string to shorten.
158065015Seric **		m -- the max length of the string.
158165015Seric **
158265015Seric **	Returns:
158365015Seric **		Either s or a short version of s.
158465015Seric */
158565015Seric 
158665015Seric #ifndef MAXSHORTSTR
158765055Seric # define MAXSHORTSTR	203
158865015Seric #endif
158965015Seric 
159065015Seric char *
159165015Seric shortenstring(s, m)
159269748Seric 	register const char *s;
159365015Seric 	int m;
159465015Seric {
159565015Seric 	int l;
159665015Seric 	static char buf[MAXSHORTSTR + 1];
159765015Seric 
159865015Seric 	l = strlen(s);
159965015Seric 	if (l < m)
160069748Seric 		return (char *) s;
160165015Seric 	if (m > MAXSHORTSTR)
160265015Seric 		m = MAXSHORTSTR;
160365015Seric 	else if (m < 10)
160465015Seric 	{
160565015Seric 		if (m < 5)
160665015Seric 		{
160765015Seric 			strncpy(buf, s, m);
160865015Seric 			buf[m] = '\0';
160965015Seric 			return buf;
161065015Seric 		}
161165015Seric 		strncpy(buf, s, m - 3);
161265015Seric 		strcpy(buf + m - 3, "...");
161365015Seric 		return buf;
161465015Seric 	}
161565015Seric 	m = (m - 3) / 2;
161665015Seric 	strncpy(buf, s, m);
161765015Seric 	strcpy(buf + m, "...");
161865015Seric 	strcpy(buf + m + 3, s + l - m);
161965015Seric 	return buf;
162065015Seric }
162167848Seric /*
162269401Seric **  SHORTEN_HOSTNAME -- strip local domain information off of hostname.
162369401Seric **
162469401Seric **	Parameters:
162569401Seric **		host -- the host to shorten (stripped in place).
162669401Seric **
162769401Seric **	Returns:
162869401Seric **		none.
162969401Seric */
163069401Seric 
163169401Seric void
163269401Seric shorten_hostname(host)
163369401Seric 	char host[];
163469401Seric {
163569401Seric 	register char *p;
163669401Seric 	char *mydom;
163769401Seric 	int i;
1638*69773Seric 	bool canon = FALSE;
163969401Seric 
164069401Seric 	/* strip off final dot */
164169401Seric 	p = &host[strlen(host) - 1];
164269401Seric 	if (*p == '.')
1643*69773Seric 	{
164469401Seric 		*p = '\0';
1645*69773Seric 		canon = TRUE;
1646*69773Seric 	}
164769401Seric 
164869401Seric 	/* see if there is any domain at all -- if not, we are done */
164969401Seric 	p = strchr(host, '.');
165069401Seric 	if (p == NULL)
165169401Seric 		return;
165269401Seric 
165369401Seric 	/* yes, we have a domain -- see if it looks like us */
165469401Seric 	mydom = macvalue('m', CurEnv);
165569401Seric 	if (mydom == NULL)
165669401Seric 		mydom = "";
165769401Seric 	i = strlen(++p);
1658*69773Seric 	if ((canon ? strcasecmp(p, mydom) : strncasecmp(p, mydom, i)) == 0 &&
165969401Seric 	    (mydom[i] == '.' || mydom[i] == '\0'))
166069401Seric 		*--p = '\0';
166169401Seric }
166269401Seric /*
166369453Seric **  PROG_OPEN -- open a program for reading
166469453Seric **
166569453Seric **	Parameters:
166669453Seric **		argv -- the argument list.
166769453Seric **		pfd -- pointer to a place to store the file descriptor.
166869453Seric **		e -- the current envelope.
166969453Seric **
167069453Seric **	Returns:
167169453Seric **		pid of the process -- -1 if it failed.
167269453Seric */
167369453Seric 
167469453Seric int
167569453Seric prog_open(argv, pfd, e)
167669453Seric 	char **argv;
167769453Seric 	int *pfd;
167869453Seric 	ENVELOPE *e;
167969453Seric {
168069453Seric 	int pid;
168169453Seric 	int i;
168269453Seric 	int saveerrno;
168369453Seric 	int fdv[2];
168469453Seric 	char *p, *q;
168569453Seric 	char buf[MAXLINE + 1];
168669453Seric 	extern int DtableSize;
168769453Seric 
168869453Seric 	if (pipe(fdv) < 0)
168969453Seric 	{
169069453Seric 		syserr("%s: cannot create pipe for stdout", argv[0]);
169169453Seric 		return -1;
169269453Seric 	}
169369453Seric 	pid = fork();
169469453Seric 	if (pid < 0)
169569453Seric 	{
169669453Seric 		syserr("%s: cannot fork", argv[0]);
169769453Seric 		close(fdv[0]);
169869453Seric 		close(fdv[1]);
169969453Seric 		return -1;
170069453Seric 	}
170169453Seric 	if (pid > 0)
170269453Seric 	{
170369453Seric 		/* parent */
170469453Seric 		close(fdv[1]);
170569453Seric 		*pfd = fdv[0];
170669453Seric 		return pid;
170769453Seric 	}
170869453Seric 
170969453Seric 	/* child -- close stdin */
171069453Seric 	close(0);
171169453Seric 
171269453Seric 	/* stdout goes back to parent */
171369453Seric 	close(fdv[0]);
171469453Seric 	if (dup2(fdv[1], 1) < 0)
171569453Seric 	{
171669453Seric 		syserr("%s: cannot dup2 for stdout", argv[0]);
171769453Seric 		_exit(EX_OSERR);
171869453Seric 	}
171969453Seric 	close(fdv[1]);
172069453Seric 
172169453Seric 	/* stderr goes to transcript if available */
172269453Seric 	if (e->e_xfp != NULL)
172369453Seric 	{
172469453Seric 		if (dup2(fileno(e->e_xfp), 2) < 0)
172569453Seric 		{
172669453Seric 			syserr("%s: cannot dup2 for stderr", argv[0]);
172769453Seric 			_exit(EX_OSERR);
172869453Seric 		}
172969453Seric 	}
173069453Seric 
173169453Seric 	/* this process has no right to the queue file */
173269453Seric 	if (e->e_lockfp != NULL)
173369453Seric 		close(fileno(e->e_lockfp));
173469453Seric 
173569453Seric 	/* run as default user */
173669453Seric 	setgid(DefGid);
173769453Seric 	setuid(DefUid);
173869453Seric 
173969453Seric 	/* run in some directory */
174069453Seric 	if (ProgMailer != NULL)
174169453Seric 		p = ProgMailer->m_execdir;
174269453Seric 	else
174369453Seric 		p = NULL;
174469453Seric 	for (; p != NULL; p = q)
174569453Seric 	{
174669453Seric 		q = strchr(p, ':');
174769453Seric 		if (q != NULL)
174869453Seric 			*q = '\0';
174969453Seric 		expand(p, buf, sizeof buf, e);
175069453Seric 		if (q != NULL)
175169453Seric 			*q++ = ':';
175269453Seric 		if (buf[0] != '\0' && chdir(buf) >= 0)
175369453Seric 			break;
175469453Seric 	}
175569453Seric 	if (p == NULL)
175669453Seric 	{
175769453Seric 		/* backup directories */
175869453Seric 		if (chdir("/tmp") < 0)
175969453Seric 			(void) chdir("/");
176069453Seric 	}
176169453Seric 
176269453Seric 	/* arrange for all the files to be closed */
176369453Seric 	for (i = 3; i < DtableSize; i++)
176469453Seric 	{
176569453Seric 		register int j;
176669453Seric 
176769453Seric 		if ((j = fcntl(i, F_GETFD, 0)) != -1)
176869453Seric 			(void) fcntl(i, F_SETFD, j | 1);
176969453Seric 	}
177069453Seric 
177169453Seric 	/* now exec the process */
177269476Seric 	execve(argv[0], (ARGV_T) argv, (ARGV_T) UserEnviron);
177369453Seric 
177469453Seric 	/* woops!  failed */
177569453Seric 	saveerrno = errno;
177669453Seric 	syserr("%s: cannot exec", argv[0]);
177769453Seric 	if (transienterror(saveerrno))
177869453Seric 		_exit(EX_OSERR);
177969453Seric 	_exit(EX_CONFIG);
178069453Seric }
178169453Seric /*
178268481Seric **  GET_COLUMN  -- look up a Column in a line buffer
178368481Seric **
178468481Seric **	Parameters:
178568481Seric **		line -- the raw text line to search.
178668481Seric **		col -- the column number to fetch.
178768481Seric **		delim -- the delimiter between columns.  If null,
178868481Seric **			use white space.
178968481Seric **		buf -- the output buffer.
179068481Seric **
179168481Seric **	Returns:
179268481Seric **		buf if successful.
179368481Seric **		NULL otherwise.
179468481Seric */
179568481Seric 
179668481Seric char *
179768481Seric get_column(line, col, delim, buf)
179868481Seric 	char line[];
179968481Seric 	int col;
180068481Seric 	char delim;
180168481Seric 	char buf[];
180268481Seric {
180368481Seric 	char *p;
180468481Seric 	char *begin, *end;
180568481Seric 	int i;
180668481Seric 	char delimbuf[3];
180768481Seric 
180868481Seric 	if (delim == '\0')
180969662Seric 		strcpy(delimbuf, "\n\t ");
181068481Seric 	else
181168481Seric 	{
181268481Seric 		delimbuf[0] = delim;
181368481Seric 		delimbuf[1] = '\0';
181468481Seric 	}
181568481Seric 
181668481Seric 	p = line;
181768481Seric 	if (*p == '\0')
181868481Seric 		return NULL;			/* line empty */
181968481Seric 	if (*p == delim && col == 0)
182068481Seric 		return NULL;			/* first column empty */
182168481Seric 
182268481Seric 	begin = line;
182368481Seric 
182468481Seric 	if (col == 0 && delim == '\0')
182568481Seric 	{
182668481Seric 		while (*begin && isspace(*begin))
182768481Seric 			begin++;
182868481Seric 	}
182968481Seric 
183068481Seric 	for (i = 0; i < col; i++)
183168481Seric 	{
183268481Seric 		if ((begin = strpbrk(begin, delimbuf)) == NULL)
183368481Seric 			return NULL;		/* no such column */
183468481Seric 		begin++;
183568481Seric 		if (delim == '\0')
183668481Seric 		{
183768481Seric 			while (*begin && isspace(*begin))
183868481Seric 				begin++;
183968481Seric 		}
184068481Seric 	}
184168481Seric 
184268481Seric 	end = strpbrk(begin, delimbuf);
184368481Seric 	if (end == NULL)
184468481Seric 	{
184568481Seric 		strcpy(buf, begin);
184668481Seric 	}
184768481Seric 	else
184868481Seric 	{
184968481Seric 		strncpy(buf, begin, end - begin);
185068481Seric 		buf[end - begin] = '\0';
185168481Seric 	}
185268481Seric 	return buf;
185368481Seric }
185468481Seric /*
185568267Seric **  CLEANSTRCPY -- copy string keeping out bogus characters
185668267Seric **
185768267Seric **	Parameters:
185868267Seric **		t -- "to" string.
185968267Seric **		f -- "from" string.
186068267Seric **		l -- length of space available in "to" string.
186168267Seric **
186268267Seric **	Returns:
186368267Seric **		none.
186468267Seric */
186568267Seric 
186668267Seric void
186768267Seric cleanstrcpy(t, f, l)
186868267Seric 	register char *t;
186968267Seric 	register char *f;
187068267Seric 	int l;
187168267Seric {
187268281Seric #ifdef LOG
187368281Seric 	/* check for newlines and log if necessary */
187468478Seric 	(void) denlstring(f, TRUE, TRUE);
187568281Seric #endif
187668281Seric 
187768267Seric 	l--;
187868267Seric 	while (l > 0 && *f != '\0')
187968267Seric 	{
188068267Seric 		if (isascii(*f) &&
188168267Seric 		    (isalnum(*f) || strchr("!#$%&'*+-./^_`{|}~", *f) != NULL))
188268267Seric 		{
188368267Seric 			l--;
188468267Seric 			*t++ = *f;
188568267Seric 		}
188668267Seric 		f++;
188768267Seric 	}
188868267Seric 	*t = '\0';
188968267Seric }
189068267Seric /*
189168267Seric **  DENLSTRING -- convert newlines in a string to spaces
189268267Seric **
189368267Seric **	Parameters:
189468267Seric **		s -- the input string
189568478Seric **		strict -- if set, don't permit continuation lines.
189668457Seric **		logattacks -- if set, log attempted attacks.
189768267Seric **
189868267Seric **	Returns:
189968267Seric **		A pointer to a version of the string with newlines
190068267Seric **		mapped to spaces.  This should be copied.
190168267Seric */
190268267Seric 
190368267Seric char *
190468478Seric denlstring(s, strict, logattacks)
190568267Seric 	char *s;
190668702Seric 	bool strict;
190768702Seric 	bool logattacks;
190868267Seric {
190968267Seric 	register char *p;
191068267Seric 	int l;
191168267Seric 	static char *bp = NULL;
191268267Seric 	static int bl = 0;
191368267Seric 
191468478Seric 	p = s;
191568478Seric 	while ((p = strchr(p, '\n')) != NULL)
191668478Seric 		if (strict || (*++p != ' ' && *p != '\t'))
191768478Seric 			break;
191868478Seric 	if (p == NULL)
191968267Seric 		return s;
192068267Seric 
192168267Seric 	l = strlen(s) + 1;
192268267Seric 	if (bl < l)
192368267Seric 	{
192468267Seric 		/* allocate more space */
192568267Seric 		if (bp != NULL)
192668267Seric 			free(bp);
192768267Seric 		bp = xalloc(l);
192868267Seric 		bl = l;
192968267Seric 	}
193068267Seric 	strcpy(bp, s);
193168267Seric 	for (p = bp; (p = strchr(p, '\n')) != NULL; )
193268267Seric 		*p++ = ' ';
193368281Seric 
193468481Seric /*
193568281Seric #ifdef LOG
193668457Seric 	if (logattacks)
193768457Seric 	{
193868457Seric 		syslog(LOG_NOTICE, "POSSIBLE ATTACK from %s: newline in string \"%s\"",
193968457Seric 			RealHostName == NULL ? "[UNKNOWN]" : RealHostName,
194068457Seric 			shortenstring(bp, 80));
194168457Seric 	}
194268281Seric #endif
194368481Seric */
194468281Seric 
194568267Seric 	return bp;
194668267Seric }
1947