xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 69800)
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*69800Seric static char sccsid[] = "@(#)util.c	8.75 (Berkeley) 06/05/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 	{
85*69800Seric 		syserr("!Out of memory!!");
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 
18569748Seric void
1863151Seric printav(av)
1873151Seric 	register char **av;
1883151Seric {
1893151Seric 	while (*av != NULL)
1903151Seric 	{
1918063Seric 		if (tTd(0, 44))
1928063Seric 			printf("\n\t%08x=", *av);
1938063Seric 		else
19423105Seric 			(void) putchar(' ');
1953151Seric 		xputs(*av++);
1963151Seric 	}
19723105Seric 	(void) putchar('\n');
1983151Seric }
1993151Seric /*
2003151Seric **  LOWER -- turn letter into lower case.
2013151Seric **
2023151Seric **	Parameters:
2033151Seric **		c -- character to turn into lower case.
2043151Seric **
2053151Seric **	Returns:
2063151Seric **		c, in lower case.
2073151Seric **
2083151Seric **	Side Effects:
2093151Seric **		none.
2103151Seric */
2113151Seric 
2123151Seric char
2133151Seric lower(c)
2143151Seric 	register char c;
2153151Seric {
21658050Seric 	return((isascii(c) && isupper(c)) ? tolower(c) : c);
2173151Seric }
2183151Seric /*
2193151Seric **  XPUTS -- put string doing control escapes.
2203151Seric **
2213151Seric **	Parameters:
2223151Seric **		s -- string to put.
2233151Seric **
2243151Seric **	Returns:
2253151Seric **		none.
2263151Seric **
2273151Seric **	Side Effects:
2283151Seric **		output to stdout
2293151Seric */
2303151Seric 
23169748Seric void
2323151Seric xputs(s)
23369748Seric 	register const char *s;
2343151Seric {
23558050Seric 	register int c;
23651781Seric 	register struct metamac *mp;
23751781Seric 	extern struct metamac MetaMacros[];
2383151Seric 
2398055Seric 	if (s == NULL)
2408055Seric 	{
2418055Seric 		printf("<null>");
2428055Seric 		return;
2438055Seric 	}
24458050Seric 	while ((c = (*s++ & 0377)) != '\0')
2453151Seric 	{
2463151Seric 		if (!isascii(c))
2473151Seric 		{
24868481Seric 			if (c == MATCHREPL)
24958050Seric 			{
25058050Seric 				putchar('$');
25158050Seric 				continue;
25258050Seric 			}
25368481Seric 			if (c == MACROEXPAND)
25468481Seric 			{
25568481Seric 				putchar('$');
25668481Seric 				if (bitset(0200, *s))
25768481Seric 					printf("{%s}", macname(*s++ & 0377));
25868481Seric 				continue;
25968481Seric 			}
26058050Seric 			for (mp = MetaMacros; mp->metaname != '\0'; mp++)
26158050Seric 			{
26258050Seric 				if ((mp->metaval & 0377) == c)
26358050Seric 				{
26458050Seric 					printf("$%c", mp->metaname);
26558050Seric 					break;
26658050Seric 				}
26758050Seric 			}
26858050Seric 			if (mp->metaname != '\0')
26958050Seric 				continue;
27023105Seric 			(void) putchar('\\');
2713151Seric 			c &= 0177;
2723151Seric 		}
27357589Seric 		if (isprint(c))
2743151Seric 		{
27557589Seric 			putchar(c);
27657589Seric 			continue;
27757589Seric 		}
27852050Seric 
27957589Seric 		/* wasn't a meta-macro -- find another way to print it */
28057589Seric 		switch (c)
28157589Seric 		{
28257589Seric 		  case '\n':
28357589Seric 			c = 'n';
28457589Seric 			break;
28552050Seric 
28657589Seric 		  case '\r':
28757589Seric 			c = 'r';
28857589Seric 			break;
28952637Seric 
29057589Seric 		  case '\t':
29157589Seric 			c = 't';
29257589Seric 			break;
29357589Seric 
29457589Seric 		  default:
29557589Seric 			(void) putchar('^');
29657589Seric 			(void) putchar(c ^ 0100);
29757589Seric 			continue;
2983151Seric 		}
29968500Seric 		(void) putchar('\\');
30068500Seric 		(void) putchar(c);
3013151Seric 	}
3024086Seric 	(void) fflush(stdout);
3033151Seric }
3043151Seric /*
3053151Seric **  MAKELOWER -- Translate a line into lower case
3063151Seric **
3073151Seric **	Parameters:
3083151Seric **		p -- the string to translate.  If NULL, return is
3093151Seric **			immediate.
3103151Seric **
3113151Seric **	Returns:
3123151Seric **		none.
3133151Seric **
3143151Seric **	Side Effects:
3153151Seric **		String pointed to by p is translated to lower case.
3163151Seric **
3173151Seric **	Called By:
3183151Seric **		parse
3193151Seric */
3203151Seric 
32168481Seric void
3223151Seric makelower(p)
3233151Seric 	register char *p;
3243151Seric {
3253151Seric 	register char c;
3263151Seric 
3273151Seric 	if (p == NULL)
3283151Seric 		return;
3293151Seric 	for (; (c = *p) != '\0'; p++)
3303151Seric 		if (isascii(c) && isupper(c))
33133724Sbostic 			*p = tolower(c);
3323151Seric }
3334059Seric /*
3345196Seric **  BUILDFNAME -- build full name from gecos style entry.
3354375Seric **
3365196Seric **	This routine interprets the strange entry that would appear
3375196Seric **	in the GECOS field of the password file.
3385196Seric **
3394375Seric **	Parameters:
3405196Seric **		p -- name to build.
3415196Seric **		login -- the login name of this user (for &).
3425196Seric **		buf -- place to put the result.
3434375Seric **
3444375Seric **	Returns:
34565006Seric **		none.
3464375Seric **
3474375Seric **	Side Effects:
3484375Seric **		none.
3494375Seric */
3504375Seric 
35169748Seric void
35254984Seric buildfname(gecos, login, buf)
35365006Seric 	register char *gecos;
35465006Seric 	char *login;
35565006Seric 	char *buf;
3564375Seric {
35765006Seric 	register char *p;
35865006Seric 	register char *bp = buf;
35965006Seric 	int l;
3604375Seric 
36165006Seric 	if (*gecos == '*')
36265006Seric 		gecos++;
36365006Seric 
36465006Seric 	/* find length of final string */
36565006Seric 	l = 0;
36665006Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
36754984Seric 	{
36865006Seric 		if (*p == '&')
36965006Seric 			l += strlen(login);
37065006Seric 		else
37165006Seric 			l++;
37254984Seric 	}
37365006Seric 
37465006Seric 	/* now fill in buf */
37565006Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
3764375Seric 	{
37765006Seric 		if (*p == '&')
3784375Seric 		{
37965006Seric 			(void) strcpy(bp, login);
38065006Seric 			*bp = toupper(*bp);
38165006Seric 			while (*bp != '\0')
38265006Seric 				bp++;
3834375Seric 		}
38465006Seric 		else
38565006Seric 			*bp++ = *p;
3864375Seric 	}
3874375Seric 	*bp = '\0';
3884375Seric }
3894375Seric /*
3904538Seric **  SAFEFILE -- return true if a file exists and is safe for a user.
3914538Seric **
3924538Seric **	Parameters:
3934538Seric **		fn -- filename to check.
39464083Seric **		uid -- user id to compare against.
39564083Seric **		gid -- group id to compare against.
39664083Seric **		uname -- user name to compare against (used for group
39764083Seric **			sets).
39864944Seric **		flags -- modifiers:
39965064Seric **			SFF_MUSTOWN -- "uid" must own this file.
40065064Seric **			SFF_NOSLINK -- file cannot be a symbolic link.
4014538Seric **		mode -- mode bits that must match.
40268494Seric **		st -- if set, points to a stat structure that will
40368494Seric **			get the stat info for the file.
4044538Seric **
4054538Seric **	Returns:
40658247Seric **		0 if fn exists, is owned by uid, and matches mode.
40758247Seric **		An errno otherwise.  The actual errno is cleared.
4084538Seric **
4094538Seric **	Side Effects:
4104538Seric **		none.
4114538Seric */
4124538Seric 
41364083Seric #include <grp.h>
41464083Seric 
41563581Seric #ifndef S_IXOTH
41663581Seric # define S_IXOTH	(S_IEXEC >> 6)
41763581Seric #endif
41863581Seric 
41964083Seric #ifndef S_IXGRP
42064083Seric # define S_IXGRP	(S_IEXEC >> 3)
42164083Seric #endif
42264083Seric 
42363753Seric #ifndef S_IXUSR
42463753Seric # define S_IXUSR	(S_IEXEC)
42563753Seric #endif
42663753Seric 
42768494Seric #define ST_MODE_NOFILE	0171147		/* unlikely to occur */
42868494Seric 
42958247Seric int
43068494Seric safefile(fn, uid, gid, uname, flags, mode, st)
4314538Seric 	char *fn;
43255372Seric 	uid_t uid;
43364083Seric 	gid_t gid;
43464083Seric 	char *uname;
43564944Seric 	int flags;
4364538Seric 	int mode;
43768494Seric 	struct stat *st;
4384538Seric {
43963581Seric 	register char *p;
44064083Seric 	register struct group *gr = NULL;
4414538Seric 	struct stat stbuf;
4424538Seric 
44363581Seric 	if (tTd(54, 4))
44464944Seric 		printf("safefile(%s, uid=%d, gid=%d, flags=%x, mode=%o):\n",
44564944Seric 			fn, uid, gid, flags, mode);
44663581Seric 	errno = 0;
44768494Seric 	if (st == NULL)
44868494Seric 		st = &stbuf;
44963581Seric 
45068481Seric 	if (!bitset(SFF_NOPATHCHECK, flags) ||
45168481Seric 	    (uid == 0 && !bitset(SFF_ROOTOK, flags)))
45263581Seric 	{
45368481Seric 		/* check the path to the file for acceptability */
45468481Seric 		for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/')
45565225Seric 		{
45668481Seric 			*p = '\0';
45768481Seric 			if (stat(fn, &stbuf) < 0)
45868481Seric 				break;
45968513Seric 			if (uid == 0 && bitset(S_IWGRP|S_IWOTH, stbuf.st_mode))
46068513Seric 				message("051 WARNING: writable directory %s",
46168513Seric 					fn);
46268481Seric 			if (uid == 0 && !bitset(SFF_ROOTOK, flags))
46368481Seric 			{
46468481Seric 				if (bitset(S_IXOTH, stbuf.st_mode))
46568481Seric 					continue;
46668481Seric 				break;
46768481Seric 			}
46868481Seric 			if (stbuf.st_uid == uid &&
46968481Seric 			    bitset(S_IXUSR, stbuf.st_mode))
47065225Seric 				continue;
47168481Seric 			if (stbuf.st_gid == gid &&
47268481Seric 			    bitset(S_IXGRP, stbuf.st_mode))
47368481Seric 				continue;
47468481Seric #ifndef NO_GROUP_SET
47568481Seric 			if (uname != NULL &&
47668481Seric 			    ((gr != NULL && gr->gr_gid == stbuf.st_gid) ||
47768481Seric 			     (gr = getgrgid(stbuf.st_gid)) != NULL))
47868481Seric 			{
47968481Seric 				register char **gp;
48068481Seric 
48168481Seric 				for (gp = gr->gr_mem; gp != NULL && *gp != NULL; gp++)
48268481Seric 					if (strcmp(*gp, uname) == 0)
48368481Seric 						break;
48468481Seric 				if (gp != NULL && *gp != NULL &&
48568481Seric 				    bitset(S_IXGRP, stbuf.st_mode))
48668481Seric 					continue;
48768481Seric 			}
48868481Seric #endif
48968481Seric 			if (!bitset(S_IXOTH, stbuf.st_mode))
49068481Seric 				break;
49168478Seric 		}
49268481Seric 		if (p != NULL)
49363581Seric 		{
49468481Seric 			int ret = errno;
49563581Seric 
49668481Seric 			if (ret == 0)
49768481Seric 				ret = EACCES;
49868481Seric 			if (tTd(54, 4))
49968481Seric 				printf("\t[dir %s] %s\n", fn, errstring(ret));
50068481Seric 			*p = '/';
50168481Seric 			return ret;
50263581Seric 		}
50363581Seric 	}
50463581Seric 
50564944Seric #ifdef HASLSTAT
50668494Seric 	if ((bitset(SFF_NOSLINK, flags) ? lstat(fn, st)
50768494Seric 					: stat(fn, st)) < 0)
50864944Seric #else
50968494Seric 	if (stat(fn, st) < 0)
51064944Seric #endif
51158247Seric 	{
51258247Seric 		int ret = errno;
51358247Seric 
51463581Seric 		if (tTd(54, 4))
51564083Seric 			printf("\t%s\n", errstring(ret));
51663581Seric 
51758247Seric 		errno = 0;
51868494Seric 		if (!bitset(SFF_CREAT, flags))
51968494Seric 			return ret;
52068494Seric 
52168494Seric 		/* check to see if legal to create the file */
52268494Seric 		p = strrchr(fn, '/');
52368494Seric 		if (p == NULL)
52468494Seric 			return ENOTDIR;
52568494Seric 		*p = '\0';
52668494Seric 		if (stat(fn, &stbuf) >= 0)
52768494Seric 		{
52868494Seric 			int md = S_IWRITE|S_IEXEC;
52968494Seric 			if (stbuf.st_uid != uid)
53068494Seric 				md >>= 6;
53168494Seric 			if ((stbuf.st_mode & md) != md)
53268494Seric 				errno = EACCES;
53368494Seric 		}
53468494Seric 		ret = errno;
53568494Seric 		if (tTd(54, 4))
53668494Seric 			printf("\t[final dir %s uid %d mode %o] %s\n",
53768494Seric 				fn, stbuf.st_uid, stbuf.st_mode,
53868494Seric 				errstring(ret));
53968494Seric 		*p = '/';
54068494Seric 		st->st_mode = ST_MODE_NOFILE;
54158247Seric 		return ret;
54258247Seric 	}
54364944Seric 
54464944Seric #ifdef S_ISLNK
54568494Seric 	if (bitset(SFF_NOSLINK, flags) && S_ISLNK(st->st_mode))
54664944Seric 	{
54764944Seric 		if (tTd(54, 4))
54868494Seric 			printf("\t[slink mode %o]\tEPERM\n", st->st_mode);
54964944Seric 		return EPERM;
55064944Seric 	}
55164944Seric #endif
55268513Seric 	if (bitset(SFF_REGONLY, flags) && !S_ISREG(st->st_mode))
55368513Seric 	{
55468513Seric 		if (tTd(54, 4))
55568513Seric 			printf("\t[non-reg mode %o]\tEPERM\n", st->st_mode);
55668513Seric 		return EPERM;
55768513Seric 	}
55868494Seric 	if (bitset(S_IWUSR|S_IWGRP|S_IWOTH, mode) && bitset(0111, st->st_mode))
55968494Seric 	{
56068494Seric 		if (tTd(29, 5))
56168494Seric 			printf("failed (mode %o: x bits)\n", st->st_mode);
56268513Seric 		return EPERM;
56368494Seric 	}
56468494Seric 
56568494Seric 	if (bitset(SFF_SETUIDOK, flags))
56668494Seric 	{
56768494Seric 		if (bitset(S_ISUID, st->st_mode) &&
56868494Seric 		    (st->st_uid != 0 || bitset(SFF_ROOTOK, flags)))
56968494Seric 		{
57068494Seric 			uid = st->st_uid;
57168494Seric 			uname = NULL;
57268494Seric 		}
57368494Seric 		if (bitset(S_ISGID, st->st_mode) &&
57468494Seric 		    (st->st_gid != 0 || bitset(SFF_ROOTOK, flags)))
57568494Seric 			gid = st->st_gid;
57668494Seric 	}
57768494Seric 
57865139Seric 	if (uid == 0 && !bitset(SFF_ROOTOK, flags))
57963581Seric 		mode >>= 6;
58068494Seric 	else if (st->st_uid != uid)
58164084Seric 	{
58264084Seric 		mode >>= 3;
58368494Seric 		if (st->st_gid == gid)
58464084Seric 			;
58564084Seric #ifndef NO_GROUP_SET
58664084Seric 		else if (uname != NULL &&
58768494Seric 			 ((gr != NULL && gr->gr_gid == st->st_gid) ||
58868494Seric 			  (gr = getgrgid(st->st_gid)) != NULL))
58964084Seric 		{
59064084Seric 			register char **gp;
59164084Seric 
59264084Seric 			for (gp = gr->gr_mem; *gp != NULL; gp++)
59364084Seric 				if (strcmp(*gp, uname) == 0)
59464084Seric 					break;
59564084Seric 			if (*gp == NULL)
59664084Seric 				mode >>= 3;
59764084Seric 		}
59864084Seric #endif
59964084Seric 		else
60064084Seric 			mode >>= 3;
60164084Seric 	}
60263581Seric 	if (tTd(54, 4))
60364084Seric 		printf("\t[uid %d, stat %o, mode %o] ",
60468494Seric 			st->st_uid, st->st_mode, mode);
60568494Seric 	if ((st->st_uid == uid || st->st_uid == 0 ||
60665064Seric 	     !bitset(SFF_MUSTOWN, flags)) &&
60768494Seric 	    (st->st_mode & mode) == mode)
60863581Seric 	{
60963581Seric 		if (tTd(54, 4))
61064083Seric 			printf("\tOK\n");
61158247Seric 		return 0;
61263581Seric 	}
61363581Seric 	if (tTd(54, 4))
61464083Seric 		printf("\tEACCES\n");
61563581Seric 	return EACCES;
6164538Seric }
6174538Seric /*
61868494Seric **  SAFEFOPEN -- do a file open with extra checking
61968494Seric **
62068494Seric **	Parameters:
62168494Seric **		fn -- the file name to open.
62268494Seric **		omode -- the open-style mode flags.
62368494Seric **		cmode -- the create-style mode flags.
62468494Seric **		sff -- safefile flags.
62568494Seric **
62668494Seric **	Returns:
62768494Seric **		Same as fopen.
62868494Seric */
62968494Seric 
63068703Seric #ifndef O_ACCMODE
63168703Seric # define O_ACCMODE	(O_RDONLY|O_WRONLY|O_RDWR)
63268703Seric #endif
63368703Seric 
63468494Seric FILE *
63568494Seric safefopen(fn, omode, cmode, sff)
63668494Seric 	char *fn;
63768494Seric 	int omode;
63868494Seric 	int cmode;
63968494Seric 	int sff;
64068494Seric {
64168494Seric 	int rval;
64268494Seric 	FILE *fp;
64368494Seric 	int smode;
64468494Seric 	struct stat stb, sta;
64568494Seric 
64668494Seric 	if (bitset(O_CREAT, omode))
64768494Seric 		sff |= SFF_CREAT;
64868494Seric 	smode = 0;
64968494Seric 	switch (omode & O_ACCMODE)
65068494Seric 	{
65168494Seric 	  case O_RDONLY:
65268494Seric 		smode = S_IREAD;
65368494Seric 		break;
65468494Seric 
65568494Seric 	  case O_WRONLY:
65668494Seric 		smode = S_IWRITE;
65768494Seric 		break;
65868494Seric 
65968494Seric 	  case O_RDWR:
66068494Seric 		smode = S_IREAD|S_IWRITE;
66168494Seric 		break;
66268494Seric 
66368494Seric 	  default:
66468494Seric 		smode = 0;
66568494Seric 		break;
66668494Seric 	}
66768513Seric 	if (bitset(SFF_OPENASROOT, sff))
66868513Seric 		rval = safefile(fn, 0, 0, NULL, sff, smode, &stb);
66968513Seric 	else
67068513Seric 		rval = safefile(fn, RealUid, RealGid, RealUserName,
67168513Seric 				sff, smode, &stb);
67268494Seric 	if (rval != 0)
67368494Seric 	{
67468494Seric 		errno = rval;
67568494Seric 		return NULL;
67668494Seric 	}
67768494Seric 	if (stb.st_mode == ST_MODE_NOFILE)
67868494Seric 		omode |= O_EXCL;
67968494Seric 
68068494Seric 	fp = dfopen(fn, omode, cmode);
68168494Seric 	if (fp == NULL)
68268494Seric 		return NULL;
68368494Seric 	if (bitset(O_EXCL, omode))
68468494Seric 		return fp;
68568494Seric 	if (fstat(fileno(fp), &sta) < 0 ||
68668494Seric 	    sta.st_nlink != stb.st_nlink ||
68768494Seric 	    sta.st_dev != stb.st_dev ||
68868494Seric 	    sta.st_ino != stb.st_ino ||
68968494Seric 	    sta.st_uid != stb.st_uid ||
69068494Seric 	    sta.st_gid != stb.st_gid)
69168494Seric 	{
69268494Seric 		syserr("554 cannot open: file %s changed after open", fn);
69368513Seric 		fclose(fp);
69468494Seric 		errno = EPERM;
69568494Seric 		return NULL;
69668494Seric 	}
69768494Seric 	return fp;
69868494Seric }
69968494Seric /*
7004557Seric **  FIXCRLF -- fix <CR><LF> in line.
7014557Seric **
7024557Seric **	Looks for the <CR><LF> combination and turns it into the
7034557Seric **	UNIX canonical <NL> character.  It only takes one line,
7044557Seric **	i.e., it is assumed that the first <NL> found is the end
7054557Seric **	of the line.
7064557Seric **
7074557Seric **	Parameters:
7084557Seric **		line -- the line to fix.
7094557Seric **		stripnl -- if true, strip the newline also.
7104557Seric **
7114557Seric **	Returns:
7124557Seric **		none.
7134557Seric **
7144557Seric **	Side Effects:
7154557Seric **		line is changed in place.
7164557Seric */
7174557Seric 
71869748Seric void
7194557Seric fixcrlf(line, stripnl)
7204557Seric 	char *line;
7214557Seric 	bool stripnl;
7224557Seric {
7234557Seric 	register char *p;
7244557Seric 
72556795Seric 	p = strchr(line, '\n');
7264557Seric 	if (p == NULL)
7274557Seric 		return;
72836291Sbostic 	if (p > line && p[-1] == '\r')
7294557Seric 		p--;
7304557Seric 	if (!stripnl)
7314557Seric 		*p++ = '\n';
7324557Seric 	*p = '\0';
7334557Seric }
7344557Seric /*
7356890Seric **  DFOPEN -- determined file open
7366890Seric **
7376890Seric **	This routine has the semantics of fopen, except that it will
7386890Seric **	keep trying a few times to make this happen.  The idea is that
7396890Seric **	on very loaded systems, we may run out of resources (inodes,
7406890Seric **	whatever), so this tries to get around it.
7416890Seric */
7426890Seric 
74359745Seric struct omodes
74459745Seric {
74559745Seric 	int	mask;
74659745Seric 	int	mode;
74759745Seric 	char	*farg;
74859745Seric } OpenModes[] =
74959745Seric {
75059745Seric 	O_ACCMODE,		O_RDONLY,		"r",
75159745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY,		"w",
75259745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY|O_APPEND,	"a",
75359745Seric 	O_TRUNC,		0,			"w+",
75459745Seric 	O_APPEND,		O_APPEND,		"a+",
75559745Seric 	0,			0,			"r+",
75659745Seric };
75759745Seric 
7586890Seric FILE *
75959745Seric dfopen(filename, omode, cmode)
7606890Seric 	char *filename;
76159745Seric 	int omode;
76259745Seric 	int cmode;
7636890Seric {
7646890Seric 	register int tries;
76559745Seric 	int fd;
76659745Seric 	register struct omodes *om;
76759431Seric 	struct stat st;
7686890Seric 
76959745Seric 	for (om = OpenModes; om->mask != 0; om++)
77059745Seric 		if ((omode & om->mask) == om->mode)
77159745Seric 			break;
77259745Seric 
7736890Seric 	for (tries = 0; tries < 10; tries++)
7746890Seric 	{
77525618Seric 		sleep((unsigned) (10 * tries));
7766890Seric 		errno = 0;
77759745Seric 		fd = open(filename, omode, cmode);
77859745Seric 		if (fd >= 0)
7796890Seric 			break;
78066017Seric 		switch (errno)
78166017Seric 		{
78266017Seric 		  case ENFILE:		/* system file table full */
78366017Seric 		  case EINTR:		/* interrupted syscall */
78466017Seric #ifdef ETXTBSY
78566017Seric 		  case ETXTBSY:		/* Apollo: net file locked */
78666017Seric #endif
78766017Seric 			continue;
78866017Seric 		}
78966017Seric 		break;
7906890Seric 	}
79159745Seric 	if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode))
79256328Seric 	{
79356328Seric 		int locktype;
79456328Seric 
79556328Seric 		/* lock the file to avoid accidental conflicts */
79659745Seric 		if ((omode & O_ACCMODE) != O_RDONLY)
79756328Seric 			locktype = LOCK_EX;
79856328Seric 		else
79956328Seric 			locktype = LOCK_SH;
80064335Seric 		(void) lockfile(fd, filename, NULL, locktype);
80156328Seric 		errno = 0;
80256328Seric 	}
80363787Seric 	if (fd < 0)
80463787Seric 		return NULL;
80563787Seric 	else
80663787Seric 		return fdopen(fd, om->farg);
8076890Seric }
8087124Seric /*
8097124Seric **  PUTLINE -- put a line like fputs obeying SMTP conventions
8107124Seric **
8117753Seric **	This routine always guarantees outputing a newline (or CRLF,
8127753Seric **	as appropriate) at the end of the string.
8137753Seric **
8147124Seric **	Parameters:
8157124Seric **		l -- line to put.
81665870Seric **		mci -- the mailer connection information.
8177124Seric **
8187124Seric **	Returns:
8197124Seric **		none
8207124Seric **
8217124Seric **	Side Effects:
8227124Seric **		output of l to fp.
8237124Seric */
8247124Seric 
82569748Seric void
82665870Seric putline(l, mci)
8277753Seric 	register char *l;
82865870Seric 	register MCI *mci;
8297124Seric {
83068847Seric 	putxline(l, mci, PXLF_MAPFROM);
83168515Seric }
83268847Seric /*
83368847Seric **  PUTXLINE -- putline with flags bits.
83468847Seric **
83568847Seric **	This routine always guarantees outputing a newline (or CRLF,
83668847Seric **	as appropriate) at the end of the string.
83768847Seric **
83868847Seric **	Parameters:
83968847Seric **		l -- line to put.
84068847Seric **		mci -- the mailer connection information.
84168847Seric **		pxflags -- flag bits:
84268847Seric **		    PXLF_MAPFROM -- map From_ to >From_.
84368847Seric **		    PXLF_STRIP8BIT -- strip 8th bit.
84468847Seric **
84568847Seric **	Returns:
84668847Seric **		none
84768847Seric **
84868847Seric **	Side Effects:
84968847Seric **		output of l to fp.
85068847Seric */
85168515Seric 
85268515Seric void
85368847Seric putxline(l, mci, pxflags)
85468515Seric 	register char *l;
85568515Seric 	register MCI *mci;
85668847Seric 	int pxflags;
85768515Seric {
8587124Seric 	register char *p;
85947157Sbostic 	register char svchar;
86066004Seric 	int slop = 0;
8617124Seric 
86211275Seric 	/* strip out 0200 bits -- these can look like TELNET protocol */
86368847Seric 	if (bitset(MCIF_7BIT, mci->mci_flags) ||
86468847Seric 	    bitset(PXLF_STRIP8BIT, pxflags))
86511275Seric 	{
86661707Seric 		for (p = l; (svchar = *p) != '\0'; ++p)
86761707Seric 			if (bitset(0200, svchar))
86847157Sbostic 				*p = svchar &~ 0200;
86911275Seric 	}
87011275Seric 
8717753Seric 	do
8727124Seric 	{
8737753Seric 		/* find the end of the line */
87456795Seric 		p = strchr(l, '\n');
8757753Seric 		if (p == NULL)
8767753Seric 			p = &l[strlen(l)];
8777124Seric 
87863753Seric 		if (TrafficLogFile != NULL)
87963753Seric 			fprintf(TrafficLogFile, "%05d >>> ", getpid());
88063753Seric 
8817753Seric 		/* check for line overflow */
88265870Seric 		while (mci->mci_mailer->m_linelimit > 0 &&
88366004Seric 		       (p - l + slop) > mci->mci_mailer->m_linelimit)
8847753Seric 		{
88566004Seric 			register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1];
8867124Seric 
8877753Seric 			svchar = *q;
8887753Seric 			*q = '\0';
88966004Seric 			if (l[0] == '.' && slop == 0 &&
89066004Seric 			    bitnset(M_XDOT, mci->mci_mailer->m_flags))
89163753Seric 			{
89265870Seric 				(void) putc('.', mci->mci_out);
89363753Seric 				if (TrafficLogFile != NULL)
89463753Seric 					(void) putc('.', TrafficLogFile);
89563753Seric 			}
89668847Seric 			else if (l[0] == 'F' && slop == 0 &&
89768847Seric 				 bitset(PXLF_MAPFROM, pxflags) &&
89868847Seric 				 strncmp(l, "From ", 5) == 0 &&
89968515Seric 				 bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
90068515Seric 			{
90168515Seric 				(void) putc('>', mci->mci_out);
90268515Seric 				if (TrafficLogFile != NULL)
90368515Seric 					(void) putc('>', TrafficLogFile);
90468515Seric 			}
90565870Seric 			fputs(l, mci->mci_out);
90665870Seric 			(void) putc('!', mci->mci_out);
90765870Seric 			fputs(mci->mci_mailer->m_eol, mci->mci_out);
90866004Seric 			(void) putc(' ', mci->mci_out);
90963753Seric 			if (TrafficLogFile != NULL)
91066004Seric 				fprintf(TrafficLogFile, "%s!\n%05d >>>  ",
91163753Seric 					l, getpid());
9127753Seric 			*q = svchar;
9137753Seric 			l = q;
91466004Seric 			slop = 1;
9157753Seric 		}
9167124Seric 
9177753Seric 		/* output last part */
91866004Seric 		if (l[0] == '.' && slop == 0 &&
91966004Seric 		    bitnset(M_XDOT, mci->mci_mailer->m_flags))
92063753Seric 		{
92165870Seric 			(void) putc('.', mci->mci_out);
92263753Seric 			if (TrafficLogFile != NULL)
92363753Seric 				(void) putc('.', TrafficLogFile);
92463753Seric 		}
92563753Seric 		if (TrafficLogFile != NULL)
92663753Seric 			fprintf(TrafficLogFile, "%.*s\n", p - l, l);
92747157Sbostic 		for ( ; l < p; ++l)
92865870Seric 			(void) putc(*l, mci->mci_out);
92965870Seric 		fputs(mci->mci_mailer->m_eol, mci->mci_out);
9307753Seric 		if (*l == '\n')
93147157Sbostic 			++l;
9327753Seric 	} while (l[0] != '\0');
9337124Seric }
9347676Seric /*
9357676Seric **  XUNLINK -- unlink a file, doing logging as appropriate.
9367676Seric **
9377676Seric **	Parameters:
9387676Seric **		f -- name of file to unlink.
9397676Seric **
9407676Seric **	Returns:
9417676Seric **		none.
9427676Seric **
9437676Seric **	Side Effects:
9447676Seric **		f is unlinked.
9457676Seric */
9467676Seric 
94769748Seric void
9487676Seric xunlink(f)
9497676Seric 	char *f;
9507676Seric {
9517676Seric 	register int i;
9527676Seric 
9537676Seric # ifdef LOG
95458020Seric 	if (LogLevel > 98)
95558020Seric 		syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f);
95656795Seric # endif /* LOG */
9577676Seric 
9587676Seric 	i = unlink(f);
9597676Seric # ifdef LOG
96058020Seric 	if (i < 0 && LogLevel > 97)
9617942Seric 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
96256795Seric # endif /* LOG */
9637676Seric }
9647685Seric /*
96558680Seric **  XFCLOSE -- close a file, doing logging as appropriate.
96658680Seric **
96758680Seric **	Parameters:
96858680Seric **		fp -- file pointer for the file to close
96958680Seric **		a, b -- miscellaneous crud to print for debugging
97058680Seric **
97158680Seric **	Returns:
97258680Seric **		none.
97358680Seric **
97458680Seric **	Side Effects:
97558680Seric **		fp is closed.
97658680Seric */
97758680Seric 
97869748Seric void
97958680Seric xfclose(fp, a, b)
98058680Seric 	FILE *fp;
98158680Seric 	char *a, *b;
98258680Seric {
98358796Seric 	if (tTd(53, 99))
98458680Seric 		printf("xfclose(%x) %s %s\n", fp, a, b);
98564401Seric #ifdef XDEBUG
98664401Seric 	if (fileno(fp) == 1)
98764401Seric 		syserr("xfclose(%s %s): fd = 1", a, b);
98864401Seric #endif
98958796Seric 	if (fclose(fp) < 0 && tTd(53, 99))
99058680Seric 		printf("xfclose FAILURE: %s\n", errstring(errno));
99158680Seric }
99258680Seric /*
99314885Seric **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
9947685Seric **
9957685Seric **	Parameters:
9967685Seric **		buf -- place to put the input line.
9977685Seric **		siz -- size of buf.
9987685Seric **		fp -- file to read from.
99957384Seric **		timeout -- the timeout before error occurs.
100061093Seric **		during -- what we are trying to read (for error messages).
10017685Seric **
10027685Seric **	Returns:
100315533Seric **		NULL on error (including timeout).  This will also leave
100415533Seric **			buf containing a null string.
10057685Seric **		buf otherwise.
10067685Seric **
10077685Seric **	Side Effects:
10087685Seric **		none.
10097685Seric */
10107685Seric 
101114885Seric static jmp_buf	CtxReadTimeout;
101268481Seric static void	readtimeout();
10137685Seric 
10147685Seric char *
101561093Seric sfgets(buf, siz, fp, timeout, during)
10167685Seric 	char *buf;
10177685Seric 	int siz;
10187685Seric 	FILE *fp;
101957384Seric 	time_t timeout;
102061093Seric 	char *during;
10217685Seric {
10227942Seric 	register EVENT *ev = NULL;
10237685Seric 	register char *p;
10247685Seric 
102566332Seric 	if (fp == NULL)
102666332Seric 	{
102766332Seric 		buf[0] = '\0';
102866332Seric 		return NULL;
102966332Seric 	}
103066332Seric 
103114885Seric 	/* set the timeout */
103257384Seric 	if (timeout != 0)
103314885Seric 	{
103414885Seric 		if (setjmp(CtxReadTimeout) != 0)
103514885Seric 		{
103636233Skarels # ifdef LOG
103736230Skarels 			syslog(LOG_NOTICE,
103861093Seric 			    "timeout waiting for input from %s during %s\n",
103961093Seric 			    CurHostName? CurHostName: "local", during);
104036233Skarels # endif
104136230Skarels 			errno = 0;
104261093Seric 			usrerr("451 timeout waiting for input during %s",
104361093Seric 				during);
104419037Seric 			buf[0] = '\0';
104563753Seric #ifdef XDEBUG
104663753Seric 			checkfd012(during);
104763753Seric #endif
104814885Seric 			return (NULL);
104914885Seric 		}
105068481Seric 		ev = setevent(timeout, readtimeout, 0);
105114885Seric 	}
105214885Seric 
105314885Seric 	/* try to read */
105415533Seric 	p = NULL;
105565190Seric 	while (!feof(fp) && !ferror(fp))
10567942Seric 	{
10577942Seric 		errno = 0;
10587942Seric 		p = fgets(buf, siz, fp);
105965190Seric 		if (p != NULL || errno != EINTR)
106065186Seric 			break;
106165190Seric 		clearerr(fp);
106215533Seric 	}
106314885Seric 
106414885Seric 	/* clear the event if it has not sprung */
106568481Seric 	clrevent(ev);
106614885Seric 
106714885Seric 	/* clean up the books and exit */
10688055Seric 	LineNumber++;
106915533Seric 	if (p == NULL)
107016880Seric 	{
107115533Seric 		buf[0] = '\0';
107263753Seric 		if (TrafficLogFile != NULL)
107363753Seric 			fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid());
107416880Seric 		return (NULL);
107516880Seric 	}
107663753Seric 	if (TrafficLogFile != NULL)
107763753Seric 		fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf);
107868481Seric 	if (SevenBitInput)
107968481Seric 	{
108052106Seric 		for (p = buf; *p != '\0'; p++)
108152106Seric 			*p &= ~0200;
108267546Seric 	}
108368481Seric 	else if (!HasEightBits)
108467546Seric 	{
108568481Seric 		for (p = buf; *p != '\0'; p++)
108668481Seric 		{
108768481Seric 			if (bitset(0200, *p))
108868481Seric 			{
108968481Seric 				HasEightBits = TRUE;
109068481Seric 				break;
109168481Seric 			}
109268481Seric 		}
109367546Seric 	}
109468481Seric 	return (buf);
10957685Seric }
10967685Seric 
109768481Seric static void
109866765Seric readtimeout(timeout)
109966765Seric 	time_t timeout;
11007685Seric {
110168481Seric 	longjmp(CtxReadTimeout, 1);
11027685Seric }
11037786Seric /*
11047786Seric **  FGETFOLDED -- like fgets, but know about folded lines.
11057786Seric **
11067786Seric **	Parameters:
11077786Seric **		buf -- place to put result.
11087786Seric **		n -- bytes available.
11097786Seric **		f -- file to read from.
11107786Seric **
11117786Seric **	Returns:
111257135Seric **		input line(s) on success, NULL on error or EOF.
111357135Seric **		This will normally be buf -- unless the line is too
111457135Seric **			long, when it will be xalloc()ed.
11157786Seric **
11167786Seric **	Side Effects:
11177786Seric **		buf gets lines from f, with continuation lines (lines
11187786Seric **		with leading white space) appended.  CRLF's are mapped
11197786Seric **		into single newlines.  Any trailing NL is stripped.
11207786Seric */
11217786Seric 
11227786Seric char *
11237786Seric fgetfolded(buf, n, f)
11247786Seric 	char *buf;
11257786Seric 	register int n;
11267786Seric 	FILE *f;
11277786Seric {
11287786Seric 	register char *p = buf;
112957135Seric 	char *bp = buf;
11307786Seric 	register int i;
11317786Seric 
11327786Seric 	n--;
113317350Seric 	while ((i = getc(f)) != EOF)
11347786Seric 	{
113517350Seric 		if (i == '\r')
113617350Seric 		{
113717350Seric 			i = getc(f);
113817350Seric 			if (i != '\n')
113917350Seric 			{
114017350Seric 				if (i != EOF)
114123105Seric 					(void) ungetc(i, f);
114217350Seric 				i = '\r';
114317350Seric 			}
114417350Seric 		}
114557135Seric 		if (--n <= 0)
114657135Seric 		{
114757135Seric 			/* allocate new space */
114857135Seric 			char *nbp;
114957135Seric 			int nn;
115057135Seric 
115157135Seric 			nn = (p - bp);
115257232Seric 			if (nn < MEMCHUNKSIZE)
115357135Seric 				nn *= 2;
115457135Seric 			else
115557232Seric 				nn += MEMCHUNKSIZE;
115657135Seric 			nbp = xalloc(nn);
115757135Seric 			bcopy(bp, nbp, p - bp);
115857135Seric 			p = &nbp[p - bp];
115957135Seric 			if (bp != buf)
116057135Seric 				free(bp);
116157135Seric 			bp = nbp;
116257135Seric 			n = nn - (p - bp);
116357135Seric 		}
116457135Seric 		*p++ = i;
116517350Seric 		if (i == '\n')
116617350Seric 		{
116717350Seric 			LineNumber++;
116817350Seric 			i = getc(f);
116917350Seric 			if (i != EOF)
117023105Seric 				(void) ungetc(i, f);
117117350Seric 			if (i != ' ' && i != '\t')
117252647Seric 				break;
117317350Seric 		}
11747786Seric 	}
117557135Seric 	if (p == bp)
117652647Seric 		return (NULL);
117769697Seric 	if (p[-1] == '\n')
117869697Seric 		p--;
117969697Seric 	*p = '\0';
118057135Seric 	return (bp);
11817786Seric }
11827860Seric /*
11837886Seric **  CURTIME -- return current time.
11847886Seric **
11857886Seric **	Parameters:
11867886Seric **		none.
11877886Seric **
11887886Seric **	Returns:
11897886Seric **		the current time.
11907886Seric **
11917886Seric **	Side Effects:
11927886Seric **		none.
11937886Seric */
11947886Seric 
11957886Seric time_t
11967886Seric curtime()
11977886Seric {
11987886Seric 	auto time_t t;
11997886Seric 
12007886Seric 	(void) time(&t);
12017886Seric 	return (t);
12027886Seric }
12038264Seric /*
12048264Seric **  ATOBOOL -- convert a string representation to boolean.
12058264Seric **
12068264Seric **	Defaults to "TRUE"
12078264Seric **
12088264Seric **	Parameters:
12098264Seric **		s -- string to convert.  Takes "tTyY" as true,
12108264Seric **			others as false.
12118264Seric **
12128264Seric **	Returns:
12138264Seric **		A boolean representation of the string.
12148264Seric **
12158264Seric **	Side Effects:
12168264Seric **		none.
12178264Seric */
12188264Seric 
12198264Seric bool
12208264Seric atobool(s)
12218264Seric 	register char *s;
12228264Seric {
122363833Seric 	if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL)
12248264Seric 		return (TRUE);
12258264Seric 	return (FALSE);
12268264Seric }
12279048Seric /*
12289048Seric **  ATOOCT -- convert a string representation to octal.
12299048Seric **
12309048Seric **	Parameters:
12319048Seric **		s -- string to convert.
12329048Seric **
12339048Seric **	Returns:
12349048Seric **		An integer representing the string interpreted as an
12359048Seric **		octal number.
12369048Seric **
12379048Seric **	Side Effects:
12389048Seric **		none.
12399048Seric */
12409048Seric 
124169748Seric int
12429048Seric atooct(s)
12439048Seric 	register char *s;
12449048Seric {
12459048Seric 	register int i = 0;
12469048Seric 
12479048Seric 	while (*s >= '0' && *s <= '7')
12489048Seric 		i = (i << 3) | (*s++ - '0');
12499048Seric 	return (i);
12509048Seric }
12519376Seric /*
12529376Seric **  WAITFOR -- wait for a particular process id.
12539376Seric **
12549376Seric **	Parameters:
12559376Seric **		pid -- process id to wait for.
12569376Seric **
12579376Seric **	Returns:
12589376Seric **		status of pid.
12599376Seric **		-1 if pid never shows up.
12609376Seric **
12619376Seric **	Side Effects:
12629376Seric **		none.
12639376Seric */
12649376Seric 
126564562Seric int
12669376Seric waitfor(pid)
12679376Seric 	int pid;
12689376Seric {
126964562Seric #ifdef WAITUNION
127064562Seric 	union wait st;
127164562Seric #else
12729376Seric 	auto int st;
127364562Seric #endif
12749376Seric 	int i;
12759376Seric 
12769376Seric 	do
12779376Seric 	{
12789376Seric 		errno = 0;
12799376Seric 		i = wait(&st);
12809376Seric 	} while ((i >= 0 || errno == EINTR) && i != pid);
12819376Seric 	if (i < 0)
128264562Seric 		return -1;
128364562Seric #ifdef WAITUNION
128464562Seric 	return st.w_status;
128564562Seric #else
128664562Seric 	return st;
128764562Seric #endif
12889376Seric }
12899376Seric /*
129010685Seric **  BITINTERSECT -- tell if two bitmaps intersect
129110685Seric **
129210685Seric **	Parameters:
129310685Seric **		a, b -- the bitmaps in question
129410685Seric **
129510685Seric **	Returns:
129610685Seric **		TRUE if they have a non-null intersection
129710685Seric **		FALSE otherwise
129810685Seric **
129910685Seric **	Side Effects:
130010685Seric **		none.
130110685Seric */
130210685Seric 
130310685Seric bool
130410685Seric bitintersect(a, b)
130510685Seric 	BITMAP a;
130610685Seric 	BITMAP b;
130710685Seric {
130810685Seric 	int i;
130910685Seric 
131010685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
131110685Seric 		if ((a[i] & b[i]) != 0)
131210685Seric 			return (TRUE);
131310685Seric 	return (FALSE);
131410685Seric }
131510685Seric /*
131610685Seric **  BITZEROP -- tell if a bitmap is all zero
131710685Seric **
131810685Seric **	Parameters:
131910685Seric **		map -- the bit map to check
132010685Seric **
132110685Seric **	Returns:
132210685Seric **		TRUE if map is all zero.
132310685Seric **		FALSE if there are any bits set in map.
132410685Seric **
132510685Seric **	Side Effects:
132610685Seric **		none.
132710685Seric */
132810685Seric 
132910685Seric bool
133010685Seric bitzerop(map)
133110685Seric 	BITMAP map;
133210685Seric {
133310685Seric 	int i;
133410685Seric 
133510685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
133610685Seric 		if (map[i] != 0)
133710685Seric 			return (FALSE);
133810685Seric 	return (TRUE);
133910685Seric }
134058247Seric /*
134158318Seric **  STRCONTAINEDIN -- tell if one string is contained in another
134258318Seric **
134358318Seric **	Parameters:
134458318Seric **		a -- possible substring.
134558318Seric **		b -- possible superstring.
134658318Seric **
134758318Seric **	Returns:
134858318Seric **		TRUE if a is contained in b.
134958318Seric **		FALSE otherwise.
135058318Seric */
135158318Seric 
135258318Seric bool
135358318Seric strcontainedin(a, b)
135458318Seric 	register char *a;
135558318Seric 	register char *b;
135658318Seric {
135765012Seric 	int la;
135865012Seric 	int lb;
135965012Seric 	int c;
136058318Seric 
136165012Seric 	la = strlen(a);
136265012Seric 	lb = strlen(b);
136365012Seric 	c = *a;
136465012Seric 	if (isascii(c) && isupper(c))
136565012Seric 		c = tolower(c);
136665012Seric 	for (; lb-- >= la; b++)
136758318Seric 	{
136865012Seric 		if (*b != c && isascii(*b) && isupper(*b) && tolower(*b) != c)
136965012Seric 			continue;
137065012Seric 		if (strncasecmp(a, b, la) == 0)
137158318Seric 			return TRUE;
137258318Seric 	}
137365012Seric 	return FALSE;
137458318Seric }
137563753Seric /*
137663753Seric **  CHECKFD012 -- check low numbered file descriptors
137763753Seric **
137863753Seric **	File descriptors 0, 1, and 2 should be open at all times.
137963753Seric **	This routine verifies that, and fixes it if not true.
138063753Seric **
138163753Seric **	Parameters:
138263753Seric **		where -- a tag printed if the assertion failed
138363753Seric **
138463753Seric **	Returns:
138563753Seric **		none
138663753Seric */
138763753Seric 
138869748Seric void
138963753Seric checkfd012(where)
139063753Seric 	char *where;
139163753Seric {
139263753Seric #ifdef XDEBUG
139363753Seric 	register int i;
139463753Seric 	struct stat stbuf;
139563753Seric 
139663753Seric 	for (i = 0; i < 3; i++)
139763753Seric 	{
139864735Seric 		if (fstat(i, &stbuf) < 0 && errno != EOPNOTSUPP)
139963753Seric 		{
140063753Seric 			/* oops.... */
140163753Seric 			int fd;
140263753Seric 
140363753Seric 			syserr("%s: fd %d not open", where, i);
140463753Seric 			fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666);
140563753Seric 			if (fd != i)
140663753Seric 			{
140763753Seric 				(void) dup2(fd, i);
140863753Seric 				(void) close(fd);
140963753Seric 			}
141063753Seric 		}
141163753Seric 	}
141263937Seric #endif /* XDEBUG */
141363753Seric }
141464725Seric /*
141564725Seric **  PRINTOPENFDS -- print the open file descriptors (for debugging)
141664725Seric **
141764725Seric **	Parameters:
141864725Seric **		logit -- if set, send output to syslog; otherwise
141964725Seric **			print for debugging.
142064725Seric **
142164725Seric **	Returns:
142264725Seric **		none.
142364725Seric */
142464725Seric 
142564725Seric #include <arpa/inet.h>
142664725Seric 
142769748Seric void
142864725Seric printopenfds(logit)
142964725Seric 	bool logit;
143064725Seric {
143164725Seric 	register int fd;
143264743Seric 	extern int DtableSize;
143364743Seric 
143464743Seric 	for (fd = 0; fd < DtableSize; fd++)
143564743Seric 		dumpfd(fd, FALSE, logit);
143664743Seric }
143764743Seric /*
143864743Seric **  DUMPFD -- dump a file descriptor
143964743Seric **
144064743Seric **	Parameters:
144164743Seric **		fd -- the file descriptor to dump.
144264743Seric **		printclosed -- if set, print a notification even if
144364743Seric **			it is closed; otherwise print nothing.
144464743Seric **		logit -- if set, send output to syslog instead of stdout.
144564743Seric */
144664743Seric 
144769748Seric void
144864743Seric dumpfd(fd, printclosed, logit)
144964743Seric 	int fd;
145064743Seric 	bool printclosed;
145164743Seric 	bool logit;
145264743Seric {
145364725Seric 	register char *p;
145468777Seric 	char *hp;
145566747Seric 	char *fmtstr;
145668777Seric 	SOCKADDR sa;
145764743Seric 	auto int slen;
145864743Seric 	struct stat st;
145964725Seric 	char buf[200];
146068777Seric 	extern char *hostnamebyanyaddr();
146164725Seric 
146264743Seric 	p = buf;
146364743Seric 	sprintf(p, "%3d: ", fd);
146464743Seric 	p += strlen(p);
146564743Seric 
146664743Seric 	if (fstat(fd, &st) < 0)
146764725Seric 	{
146864743Seric 		if (printclosed || errno != EBADF)
146964743Seric 		{
147064743Seric 			sprintf(p, "CANNOT STAT (%s)", errstring(errno));
147164743Seric 			goto printit;
147264743Seric 		}
147364743Seric 		return;
147464743Seric 	}
147564725Seric 
147664743Seric 	slen = fcntl(fd, F_GETFL, NULL);
147764743Seric 	if (slen != -1)
147864743Seric 	{
147964743Seric 		sprintf(p, "fl=0x%x, ", slen);
148064743Seric 		p += strlen(p);
148164743Seric 	}
148264725Seric 
148364743Seric 	sprintf(p, "mode=%o: ", st.st_mode);
148464743Seric 	p += strlen(p);
148564743Seric 	switch (st.st_mode & S_IFMT)
148664743Seric 	{
148764807Seric #ifdef S_IFSOCK
148864743Seric 	  case S_IFSOCK:
148964743Seric 		sprintf(p, "SOCK ");
149064725Seric 		p += strlen(p);
149168777Seric 		slen = sizeof sa;
149268777Seric 		if (getsockname(fd, &sa.sa, &slen) < 0)
149368810Seric 			sprintf(p, "(%s)", errstring(errno));
149464743Seric 		else
149564725Seric 		{
149668777Seric 			hp = hostnamebyanyaddr(&sa);
149768777Seric 			if (sa.sa.sa_family == AF_INET)
149868777Seric 				sprintf(p, "%s/%d", hp, ntohs(sa.sin.sin_port));
149968777Seric 			else
150068777Seric 				sprintf(p, "%s", hp);
150164743Seric 		}
150264743Seric 		p += strlen(p);
150364743Seric 		sprintf(p, "->");
150464743Seric 		p += strlen(p);
150568777Seric 		slen = sizeof sa;
150668777Seric 		if (getpeername(fd, &sa.sa, &slen) < 0)
150768810Seric 			sprintf(p, "(%s)", errstring(errno));
150864743Seric 		else
150964743Seric 		{
151068777Seric 			hp = hostnamebyanyaddr(&sa);
151168777Seric 			if (sa.sa.sa_family == AF_INET)
151268777Seric 				sprintf(p, "%s/%d", hp, ntohs(sa.sin.sin_port));
151368777Seric 			else
151468777Seric 				sprintf(p, "%s", hp);
151564743Seric 		}
151664743Seric 		break;
151764807Seric #endif
151864725Seric 
151964743Seric 	  case S_IFCHR:
152064743Seric 		sprintf(p, "CHR: ");
152164743Seric 		p += strlen(p);
152264743Seric 		goto defprint;
152364725Seric 
152464743Seric 	  case S_IFBLK:
152564743Seric 		sprintf(p, "BLK: ");
152664743Seric 		p += strlen(p);
152764743Seric 		goto defprint;
152864725Seric 
152966252Seric #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK)
153065378Seric 	  case S_IFIFO:
153165378Seric 		sprintf(p, "FIFO: ");
153265378Seric 		p += strlen(p);
153365378Seric 		goto defprint;
153465378Seric #endif
153565378Seric 
153665378Seric #ifdef S_IFDIR
153765378Seric 	  case S_IFDIR:
153865378Seric 		sprintf(p, "DIR: ");
153965378Seric 		p += strlen(p);
154065378Seric 		goto defprint;
154165378Seric #endif
154265378Seric 
154365378Seric #ifdef S_IFLNK
154465378Seric 	  case S_IFLNK:
154565378Seric 		sprintf(p, "LNK: ");
154665378Seric 		p += strlen(p);
154765378Seric 		goto defprint;
154865378Seric #endif
154965378Seric 
155064743Seric 	  default:
155164725Seric defprint:
155266785Seric 		if (sizeof st.st_size > sizeof (long))
155366751Seric 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%qd";
155466747Seric 		else
155566751Seric 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld";
155666747Seric 		sprintf(p, fmtstr,
155764743Seric 			major(st.st_dev), minor(st.st_dev), st.st_ino,
155864743Seric 			st.st_nlink, st.st_uid, st.st_gid, st.st_size);
155964743Seric 		break;
156064743Seric 	}
156164725Seric 
156264743Seric printit:
156366748Seric #ifdef LOG
156464743Seric 	if (logit)
156565006Seric 		syslog(LOG_DEBUG, "%s", buf);
156664743Seric 	else
156766748Seric #endif
156864743Seric 		printf("%s\n", buf);
156964725Seric }
157065015Seric /*
157165015Seric **  SHORTENSTRING -- return short version of a string
157265015Seric **
157365015Seric **	If the string is already short, just return it.  If it is too
157465015Seric **	long, return the head and tail of the string.
157565015Seric **
157665015Seric **	Parameters:
157765015Seric **		s -- the string to shorten.
157865015Seric **		m -- the max length of the string.
157965015Seric **
158065015Seric **	Returns:
158165015Seric **		Either s or a short version of s.
158265015Seric */
158365015Seric 
158465015Seric #ifndef MAXSHORTSTR
158565055Seric # define MAXSHORTSTR	203
158665015Seric #endif
158765015Seric 
158865015Seric char *
158965015Seric shortenstring(s, m)
159069748Seric 	register const char *s;
159165015Seric 	int m;
159265015Seric {
159365015Seric 	int l;
159465015Seric 	static char buf[MAXSHORTSTR + 1];
159565015Seric 
159665015Seric 	l = strlen(s);
159765015Seric 	if (l < m)
159869748Seric 		return (char *) s;
159965015Seric 	if (m > MAXSHORTSTR)
160065015Seric 		m = MAXSHORTSTR;
160165015Seric 	else if (m < 10)
160265015Seric 	{
160365015Seric 		if (m < 5)
160465015Seric 		{
160565015Seric 			strncpy(buf, s, m);
160665015Seric 			buf[m] = '\0';
160765015Seric 			return buf;
160865015Seric 		}
160965015Seric 		strncpy(buf, s, m - 3);
161065015Seric 		strcpy(buf + m - 3, "...");
161165015Seric 		return buf;
161265015Seric 	}
161365015Seric 	m = (m - 3) / 2;
161465015Seric 	strncpy(buf, s, m);
161565015Seric 	strcpy(buf + m, "...");
161665015Seric 	strcpy(buf + m + 3, s + l - m);
161765015Seric 	return buf;
161865015Seric }
161967848Seric /*
162069401Seric **  SHORTEN_HOSTNAME -- strip local domain information off of hostname.
162169401Seric **
162269401Seric **	Parameters:
162369401Seric **		host -- the host to shorten (stripped in place).
162469401Seric **
162569401Seric **	Returns:
162669401Seric **		none.
162769401Seric */
162869401Seric 
162969401Seric void
163069401Seric shorten_hostname(host)
163169401Seric 	char host[];
163269401Seric {
163369401Seric 	register char *p;
163469401Seric 	char *mydom;
163569401Seric 	int i;
163669773Seric 	bool canon = FALSE;
163769401Seric 
163869401Seric 	/* strip off final dot */
163969401Seric 	p = &host[strlen(host) - 1];
164069401Seric 	if (*p == '.')
164169773Seric 	{
164269401Seric 		*p = '\0';
164369773Seric 		canon = TRUE;
164469773Seric 	}
164569401Seric 
164669401Seric 	/* see if there is any domain at all -- if not, we are done */
164769401Seric 	p = strchr(host, '.');
164869401Seric 	if (p == NULL)
164969401Seric 		return;
165069401Seric 
165169401Seric 	/* yes, we have a domain -- see if it looks like us */
165269401Seric 	mydom = macvalue('m', CurEnv);
165369401Seric 	if (mydom == NULL)
165469401Seric 		mydom = "";
165569401Seric 	i = strlen(++p);
165669773Seric 	if ((canon ? strcasecmp(p, mydom) : strncasecmp(p, mydom, i)) == 0 &&
165769401Seric 	    (mydom[i] == '.' || mydom[i] == '\0'))
165869401Seric 		*--p = '\0';
165969401Seric }
166069401Seric /*
166169453Seric **  PROG_OPEN -- open a program for reading
166269453Seric **
166369453Seric **	Parameters:
166469453Seric **		argv -- the argument list.
166569453Seric **		pfd -- pointer to a place to store the file descriptor.
166669453Seric **		e -- the current envelope.
166769453Seric **
166869453Seric **	Returns:
166969453Seric **		pid of the process -- -1 if it failed.
167069453Seric */
167169453Seric 
167269453Seric int
167369453Seric prog_open(argv, pfd, e)
167469453Seric 	char **argv;
167569453Seric 	int *pfd;
167669453Seric 	ENVELOPE *e;
167769453Seric {
167869453Seric 	int pid;
167969453Seric 	int i;
168069453Seric 	int saveerrno;
168169453Seric 	int fdv[2];
168269453Seric 	char *p, *q;
168369453Seric 	char buf[MAXLINE + 1];
168469453Seric 	extern int DtableSize;
168569453Seric 
168669453Seric 	if (pipe(fdv) < 0)
168769453Seric 	{
168869453Seric 		syserr("%s: cannot create pipe for stdout", argv[0]);
168969453Seric 		return -1;
169069453Seric 	}
169169453Seric 	pid = fork();
169269453Seric 	if (pid < 0)
169369453Seric 	{
169469453Seric 		syserr("%s: cannot fork", argv[0]);
169569453Seric 		close(fdv[0]);
169669453Seric 		close(fdv[1]);
169769453Seric 		return -1;
169869453Seric 	}
169969453Seric 	if (pid > 0)
170069453Seric 	{
170169453Seric 		/* parent */
170269453Seric 		close(fdv[1]);
170369453Seric 		*pfd = fdv[0];
170469453Seric 		return pid;
170569453Seric 	}
170669453Seric 
170769453Seric 	/* child -- close stdin */
170869453Seric 	close(0);
170969453Seric 
171069453Seric 	/* stdout goes back to parent */
171169453Seric 	close(fdv[0]);
171269453Seric 	if (dup2(fdv[1], 1) < 0)
171369453Seric 	{
171469453Seric 		syserr("%s: cannot dup2 for stdout", argv[0]);
171569453Seric 		_exit(EX_OSERR);
171669453Seric 	}
171769453Seric 	close(fdv[1]);
171869453Seric 
171969453Seric 	/* stderr goes to transcript if available */
172069453Seric 	if (e->e_xfp != NULL)
172169453Seric 	{
172269453Seric 		if (dup2(fileno(e->e_xfp), 2) < 0)
172369453Seric 		{
172469453Seric 			syserr("%s: cannot dup2 for stderr", argv[0]);
172569453Seric 			_exit(EX_OSERR);
172669453Seric 		}
172769453Seric 	}
172869453Seric 
172969453Seric 	/* this process has no right to the queue file */
173069453Seric 	if (e->e_lockfp != NULL)
173169453Seric 		close(fileno(e->e_lockfp));
173269453Seric 
173369453Seric 	/* run as default user */
173469453Seric 	setgid(DefGid);
173569453Seric 	setuid(DefUid);
173669453Seric 
173769453Seric 	/* run in some directory */
173869453Seric 	if (ProgMailer != NULL)
173969453Seric 		p = ProgMailer->m_execdir;
174069453Seric 	else
174169453Seric 		p = NULL;
174269453Seric 	for (; p != NULL; p = q)
174369453Seric 	{
174469453Seric 		q = strchr(p, ':');
174569453Seric 		if (q != NULL)
174669453Seric 			*q = '\0';
174769453Seric 		expand(p, buf, sizeof buf, e);
174869453Seric 		if (q != NULL)
174969453Seric 			*q++ = ':';
175069453Seric 		if (buf[0] != '\0' && chdir(buf) >= 0)
175169453Seric 			break;
175269453Seric 	}
175369453Seric 	if (p == NULL)
175469453Seric 	{
175569453Seric 		/* backup directories */
175669453Seric 		if (chdir("/tmp") < 0)
175769453Seric 			(void) chdir("/");
175869453Seric 	}
175969453Seric 
176069453Seric 	/* arrange for all the files to be closed */
176169453Seric 	for (i = 3; i < DtableSize; i++)
176269453Seric 	{
176369453Seric 		register int j;
176469453Seric 
176569453Seric 		if ((j = fcntl(i, F_GETFD, 0)) != -1)
176669453Seric 			(void) fcntl(i, F_SETFD, j | 1);
176769453Seric 	}
176869453Seric 
176969453Seric 	/* now exec the process */
177069476Seric 	execve(argv[0], (ARGV_T) argv, (ARGV_T) UserEnviron);
177169453Seric 
177269453Seric 	/* woops!  failed */
177369453Seric 	saveerrno = errno;
177469453Seric 	syserr("%s: cannot exec", argv[0]);
177569453Seric 	if (transienterror(saveerrno))
177669453Seric 		_exit(EX_OSERR);
177769453Seric 	_exit(EX_CONFIG);
177869453Seric }
177969453Seric /*
178068481Seric **  GET_COLUMN  -- look up a Column in a line buffer
178168481Seric **
178268481Seric **	Parameters:
178368481Seric **		line -- the raw text line to search.
178468481Seric **		col -- the column number to fetch.
178568481Seric **		delim -- the delimiter between columns.  If null,
178668481Seric **			use white space.
178768481Seric **		buf -- the output buffer.
178868481Seric **
178968481Seric **	Returns:
179068481Seric **		buf if successful.
179168481Seric **		NULL otherwise.
179268481Seric */
179368481Seric 
179468481Seric char *
179568481Seric get_column(line, col, delim, buf)
179668481Seric 	char line[];
179768481Seric 	int col;
179868481Seric 	char delim;
179968481Seric 	char buf[];
180068481Seric {
180168481Seric 	char *p;
180268481Seric 	char *begin, *end;
180368481Seric 	int i;
180468481Seric 	char delimbuf[3];
180568481Seric 
180668481Seric 	if (delim == '\0')
180769662Seric 		strcpy(delimbuf, "\n\t ");
180868481Seric 	else
180968481Seric 	{
181068481Seric 		delimbuf[0] = delim;
181168481Seric 		delimbuf[1] = '\0';
181268481Seric 	}
181368481Seric 
181468481Seric 	p = line;
181568481Seric 	if (*p == '\0')
181668481Seric 		return NULL;			/* line empty */
181768481Seric 	if (*p == delim && col == 0)
181868481Seric 		return NULL;			/* first column empty */
181968481Seric 
182068481Seric 	begin = line;
182168481Seric 
182268481Seric 	if (col == 0 && delim == '\0')
182368481Seric 	{
182468481Seric 		while (*begin && isspace(*begin))
182568481Seric 			begin++;
182668481Seric 	}
182768481Seric 
182868481Seric 	for (i = 0; i < col; i++)
182968481Seric 	{
183068481Seric 		if ((begin = strpbrk(begin, delimbuf)) == NULL)
183168481Seric 			return NULL;		/* no such column */
183268481Seric 		begin++;
183368481Seric 		if (delim == '\0')
183468481Seric 		{
183568481Seric 			while (*begin && isspace(*begin))
183668481Seric 				begin++;
183768481Seric 		}
183868481Seric 	}
183968481Seric 
184068481Seric 	end = strpbrk(begin, delimbuf);
184168481Seric 	if (end == NULL)
184268481Seric 	{
184368481Seric 		strcpy(buf, begin);
184468481Seric 	}
184568481Seric 	else
184668481Seric 	{
184768481Seric 		strncpy(buf, begin, end - begin);
184868481Seric 		buf[end - begin] = '\0';
184968481Seric 	}
185068481Seric 	return buf;
185168481Seric }
185268481Seric /*
185368267Seric **  CLEANSTRCPY -- copy string keeping out bogus characters
185468267Seric **
185568267Seric **	Parameters:
185668267Seric **		t -- "to" string.
185768267Seric **		f -- "from" string.
185868267Seric **		l -- length of space available in "to" string.
185968267Seric **
186068267Seric **	Returns:
186168267Seric **		none.
186268267Seric */
186368267Seric 
186468267Seric void
186568267Seric cleanstrcpy(t, f, l)
186668267Seric 	register char *t;
186768267Seric 	register char *f;
186868267Seric 	int l;
186968267Seric {
187068281Seric #ifdef LOG
187168281Seric 	/* check for newlines and log if necessary */
187268478Seric 	(void) denlstring(f, TRUE, TRUE);
187368281Seric #endif
187468281Seric 
187568267Seric 	l--;
187668267Seric 	while (l > 0 && *f != '\0')
187768267Seric 	{
187868267Seric 		if (isascii(*f) &&
187968267Seric 		    (isalnum(*f) || strchr("!#$%&'*+-./^_`{|}~", *f) != NULL))
188068267Seric 		{
188168267Seric 			l--;
188268267Seric 			*t++ = *f;
188368267Seric 		}
188468267Seric 		f++;
188568267Seric 	}
188668267Seric 	*t = '\0';
188768267Seric }
188868267Seric /*
188968267Seric **  DENLSTRING -- convert newlines in a string to spaces
189068267Seric **
189168267Seric **	Parameters:
189268267Seric **		s -- the input string
189368478Seric **		strict -- if set, don't permit continuation lines.
189468457Seric **		logattacks -- if set, log attempted attacks.
189568267Seric **
189668267Seric **	Returns:
189768267Seric **		A pointer to a version of the string with newlines
189868267Seric **		mapped to spaces.  This should be copied.
189968267Seric */
190068267Seric 
190168267Seric char *
190268478Seric denlstring(s, strict, logattacks)
190368267Seric 	char *s;
190468702Seric 	bool strict;
190568702Seric 	bool logattacks;
190668267Seric {
190768267Seric 	register char *p;
190868267Seric 	int l;
190968267Seric 	static char *bp = NULL;
191068267Seric 	static int bl = 0;
191168267Seric 
191268478Seric 	p = s;
191368478Seric 	while ((p = strchr(p, '\n')) != NULL)
191468478Seric 		if (strict || (*++p != ' ' && *p != '\t'))
191568478Seric 			break;
191668478Seric 	if (p == NULL)
191768267Seric 		return s;
191868267Seric 
191968267Seric 	l = strlen(s) + 1;
192068267Seric 	if (bl < l)
192168267Seric 	{
192268267Seric 		/* allocate more space */
192368267Seric 		if (bp != NULL)
192468267Seric 			free(bp);
192568267Seric 		bp = xalloc(l);
192668267Seric 		bl = l;
192768267Seric 	}
192868267Seric 	strcpy(bp, s);
192968267Seric 	for (p = bp; (p = strchr(p, '\n')) != NULL; )
193068267Seric 		*p++ = ' ';
193168281Seric 
193268481Seric /*
193368281Seric #ifdef LOG
193468457Seric 	if (logattacks)
193568457Seric 	{
193668457Seric 		syslog(LOG_NOTICE, "POSSIBLE ATTACK from %s: newline in string \"%s\"",
193768457Seric 			RealHostName == NULL ? "[UNKNOWN]" : RealHostName,
193868457Seric 			shortenstring(bp, 80));
193968457Seric 	}
194068281Seric #endif
194168481Seric */
194268281Seric 
194368267Seric 	return bp;
194468267Seric }
1945