xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 64083)
122717Sdist /*
242833Sbostic  * Copyright (c) 1983 Eric P. Allman
363589Sbostic  * Copyright (c) 1988, 1993
463589Sbostic  *	The Regents of the University of California.  All rights reserved.
533731Sbostic  *
642833Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822717Sdist 
922717Sdist #ifndef lint
10*64083Seric static char sccsid[] = "@(#)util.c	8.6 (Berkeley) 07/28/93";
1133731Sbostic #endif /* not lint */
1222717Sdist 
1358332Seric # include "sendmail.h"
14298Seric # include <sysexits.h>
1557135Seric /*
16298Seric **  STRIPQUOTES -- Strip quotes & quote bits from a string.
17298Seric **
18298Seric **	Runs through a string and strips off unquoted quote
19298Seric **	characters and quote bits.  This is done in place.
20298Seric **
21298Seric **	Parameters:
22298Seric **		s -- the string to strip.
23298Seric **
24298Seric **	Returns:
25298Seric **		none.
26298Seric **
27298Seric **	Side Effects:
28298Seric **		none.
29298Seric **
30298Seric **	Called By:
31298Seric **		deliver
32298Seric */
33298Seric 
3454983Seric stripquotes(s)
35298Seric 	char *s;
36298Seric {
37298Seric 	register char *p;
38298Seric 	register char *q;
39298Seric 	register char c;
40298Seric 
414101Seric 	if (s == NULL)
424101Seric 		return;
434101Seric 
4454983Seric 	p = q = s;
4554983Seric 	do
46298Seric 	{
4754983Seric 		c = *p++;
4854983Seric 		if (c == '\\')
4954983Seric 			c = *p++;
5054983Seric 		else if (c == '"')
5154983Seric 			continue;
5254983Seric 		*q++ = c;
5354983Seric 	} while (c != '\0');
54298Seric }
55298Seric /*
56298Seric **  XALLOC -- Allocate memory and bitch wildly on failure.
57298Seric **
58298Seric **	THIS IS A CLUDGE.  This should be made to give a proper
59298Seric **	error -- but after all, what can we do?
60298Seric **
61298Seric **	Parameters:
62298Seric **		sz -- size of area to allocate.
63298Seric **
64298Seric **	Returns:
65298Seric **		pointer to data region.
66298Seric **
67298Seric **	Side Effects:
68298Seric **		Memory is allocated.
69298Seric */
70298Seric 
71298Seric char *
72298Seric xalloc(sz)
737007Seric 	register int sz;
74298Seric {
75298Seric 	register char *p;
76298Seric 
7723121Seric 	p = malloc((unsigned) sz);
78298Seric 	if (p == NULL)
79298Seric 	{
80298Seric 		syserr("Out of memory!!");
8110685Seric 		abort();
8210685Seric 		/* exit(EX_UNAVAILABLE); */
83298Seric 	}
84298Seric 	return (p);
85298Seric }
86298Seric /*
873151Seric **  COPYPLIST -- copy list of pointers.
883151Seric **
893151Seric **	This routine is the equivalent of newstr for lists of
903151Seric **	pointers.
913151Seric **
923151Seric **	Parameters:
933151Seric **		list -- list of pointers to copy.
943151Seric **			Must be NULL terminated.
953151Seric **		copycont -- if TRUE, copy the contents of the vector
963151Seric **			(which must be a string) also.
973151Seric **
983151Seric **	Returns:
993151Seric **		a copy of 'list'.
1003151Seric **
1013151Seric **	Side Effects:
1023151Seric **		none.
1033151Seric */
1043151Seric 
1053151Seric char **
1063151Seric copyplist(list, copycont)
1073151Seric 	char **list;
1083151Seric 	bool copycont;
1093151Seric {
1103151Seric 	register char **vp;
1113151Seric 	register char **newvp;
1123151Seric 
1133151Seric 	for (vp = list; *vp != NULL; vp++)
1143151Seric 		continue;
1153151Seric 
1163151Seric 	vp++;
1173151Seric 
11816897Seric 	newvp = (char **) xalloc((int) (vp - list) * sizeof *vp);
11916897Seric 	bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp);
1203151Seric 
1213151Seric 	if (copycont)
1223151Seric 	{
1233151Seric 		for (vp = newvp; *vp != NULL; vp++)
1243151Seric 			*vp = newstr(*vp);
1253151Seric 	}
1263151Seric 
1273151Seric 	return (newvp);
1283151Seric }
1293151Seric /*
13058170Seric **  COPYQUEUE -- copy address queue.
13158170Seric **
13258170Seric **	This routine is the equivalent of newstr for address queues
13358170Seric **	addresses marked with QDONTSEND aren't copied
13458170Seric **
13558170Seric **	Parameters:
13658170Seric **		addr -- list of address structures to copy.
13758170Seric **
13858170Seric **	Returns:
13958170Seric **		a copy of 'addr'.
14058170Seric **
14158170Seric **	Side Effects:
14258170Seric **		none.
14358170Seric */
14458170Seric 
14558170Seric ADDRESS *
14658170Seric copyqueue(addr)
14758170Seric 	ADDRESS *addr;
14858170Seric {
14958170Seric 	register ADDRESS *newaddr;
15058170Seric 	ADDRESS *ret;
15158170Seric 	register ADDRESS **tail = &ret;
15258170Seric 
15358170Seric 	while (addr != NULL)
15458170Seric 	{
15558170Seric 		if (!bitset(QDONTSEND, addr->q_flags))
15658170Seric 		{
15758170Seric 			newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS));
15858170Seric 			STRUCTCOPY(*addr, *newaddr);
15958170Seric 			*tail = newaddr;
16058170Seric 			tail = &newaddr->q_next;
16158170Seric 		}
16258170Seric 		addr = addr->q_next;
16358170Seric 	}
16458170Seric 	*tail = NULL;
16558170Seric 
16658170Seric 	return ret;
16758170Seric }
16858170Seric /*
1693151Seric **  PRINTAV -- print argument vector.
1703151Seric **
1713151Seric **	Parameters:
1723151Seric **		av -- argument vector.
1733151Seric **
1743151Seric **	Returns:
1753151Seric **		none.
1763151Seric **
1773151Seric **	Side Effects:
1783151Seric **		prints av.
1793151Seric */
1803151Seric 
1813151Seric printav(av)
1823151Seric 	register char **av;
1833151Seric {
1843151Seric 	while (*av != NULL)
1853151Seric 	{
1868063Seric 		if (tTd(0, 44))
1878063Seric 			printf("\n\t%08x=", *av);
1888063Seric 		else
18923105Seric 			(void) putchar(' ');
1903151Seric 		xputs(*av++);
1913151Seric 	}
19223105Seric 	(void) putchar('\n');
1933151Seric }
1943151Seric /*
1953151Seric **  LOWER -- turn letter into lower case.
1963151Seric **
1973151Seric **	Parameters:
1983151Seric **		c -- character to turn into lower case.
1993151Seric **
2003151Seric **	Returns:
2013151Seric **		c, in lower case.
2023151Seric **
2033151Seric **	Side Effects:
2043151Seric **		none.
2053151Seric */
2063151Seric 
2073151Seric char
2083151Seric lower(c)
2093151Seric 	register char c;
2103151Seric {
21158050Seric 	return((isascii(c) && isupper(c)) ? tolower(c) : c);
2123151Seric }
2133151Seric /*
2143151Seric **  XPUTS -- put string doing control escapes.
2153151Seric **
2163151Seric **	Parameters:
2173151Seric **		s -- string to put.
2183151Seric **
2193151Seric **	Returns:
2203151Seric **		none.
2213151Seric **
2223151Seric **	Side Effects:
2233151Seric **		output to stdout
2243151Seric */
2253151Seric 
2263151Seric xputs(s)
2273151Seric 	register char *s;
2283151Seric {
22958050Seric 	register int c;
23051781Seric 	register struct metamac *mp;
23151781Seric 	extern struct metamac MetaMacros[];
2323151Seric 
2338055Seric 	if (s == NULL)
2348055Seric 	{
2358055Seric 		printf("<null>");
2368055Seric 		return;
2378055Seric 	}
23858050Seric 	while ((c = (*s++ & 0377)) != '\0')
2393151Seric 	{
2403151Seric 		if (!isascii(c))
2413151Seric 		{
24258050Seric 			if (c == MATCHREPL || c == MACROEXPAND)
24358050Seric 			{
24458050Seric 				putchar('$');
24558050Seric 				continue;
24658050Seric 			}
24758050Seric 			for (mp = MetaMacros; mp->metaname != '\0'; mp++)
24858050Seric 			{
24958050Seric 				if ((mp->metaval & 0377) == c)
25058050Seric 				{
25158050Seric 					printf("$%c", mp->metaname);
25258050Seric 					break;
25358050Seric 				}
25458050Seric 			}
25558050Seric 			if (mp->metaname != '\0')
25658050Seric 				continue;
25723105Seric 			(void) putchar('\\');
2583151Seric 			c &= 0177;
2593151Seric 		}
26057589Seric 		if (isprint(c))
2613151Seric 		{
26257589Seric 			putchar(c);
26357589Seric 			continue;
26457589Seric 		}
26552050Seric 
26657589Seric 		/* wasn't a meta-macro -- find another way to print it */
26757589Seric 		switch (c)
26857589Seric 		{
26957589Seric 		  case '\0':
27057589Seric 			continue;
27152050Seric 
27257589Seric 		  case '\n':
27357589Seric 			c = 'n';
27457589Seric 			break;
27552050Seric 
27657589Seric 		  case '\r':
27757589Seric 			c = 'r';
27857589Seric 			break;
27952637Seric 
28057589Seric 		  case '\t':
28157589Seric 			c = 't';
28257589Seric 			break;
28357589Seric 
28457589Seric 		  default:
28557589Seric 			(void) putchar('^');
28657589Seric 			(void) putchar(c ^ 0100);
28757589Seric 			continue;
2883151Seric 		}
2893151Seric 	}
2904086Seric 	(void) fflush(stdout);
2913151Seric }
2923151Seric /*
2933151Seric **  MAKELOWER -- Translate a line into lower case
2943151Seric **
2953151Seric **	Parameters:
2963151Seric **		p -- the string to translate.  If NULL, return is
2973151Seric **			immediate.
2983151Seric **
2993151Seric **	Returns:
3003151Seric **		none.
3013151Seric **
3023151Seric **	Side Effects:
3033151Seric **		String pointed to by p is translated to lower case.
3043151Seric **
3053151Seric **	Called By:
3063151Seric **		parse
3073151Seric */
3083151Seric 
3093151Seric makelower(p)
3103151Seric 	register char *p;
3113151Seric {
3123151Seric 	register char c;
3133151Seric 
3143151Seric 	if (p == NULL)
3153151Seric 		return;
3163151Seric 	for (; (c = *p) != '\0'; p++)
3173151Seric 		if (isascii(c) && isupper(c))
31833724Sbostic 			*p = tolower(c);
3193151Seric }
3204059Seric /*
3215196Seric **  BUILDFNAME -- build full name from gecos style entry.
3224375Seric **
3235196Seric **	This routine interprets the strange entry that would appear
3245196Seric **	in the GECOS field of the password file.
3255196Seric **
3264375Seric **	Parameters:
3275196Seric **		p -- name to build.
3285196Seric **		login -- the login name of this user (for &).
3295196Seric **		buf -- place to put the result.
3304375Seric **
3314375Seric **	Returns:
3324375Seric **		none.
3334375Seric **
3344375Seric **	Side Effects:
3354375Seric **		none.
3364375Seric */
3374375Seric 
33854984Seric buildfname(gecos, login, buf)
33954984Seric 	register char *gecos;
3405196Seric 	char *login;
3414375Seric 	char *buf;
3424375Seric {
34354984Seric 	register char *p;
3444375Seric 	register char *bp = buf;
34554984Seric 	int l;
3464375Seric 
34754984Seric 	if (*gecos == '*')
34854984Seric 		gecos++;
34954984Seric 
35057123Seric 	/* find length of final string */
35154984Seric 	l = 0;
35254984Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
35354984Seric 	{
35454984Seric 		if (*p == '&')
35554984Seric 			l += strlen(login);
35654984Seric 		else
35754984Seric 			l++;
35854984Seric 	}
35954984Seric 
36054984Seric 	/* now fill in buf */
36155193Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
3624375Seric 	{
3634375Seric 		if (*p == '&')
3644375Seric 		{
3655196Seric 			(void) strcpy(bp, login);
3664375Seric 			*bp = toupper(*bp);
3674375Seric 			while (*bp != '\0')
3684375Seric 				bp++;
3694375Seric 		}
3704375Seric 		else
37155193Seric 			*bp++ = *p;
3724375Seric 	}
3734375Seric 	*bp = '\0';
3744375Seric }
3754375Seric /*
3764538Seric **  SAFEFILE -- return true if a file exists and is safe for a user.
3774538Seric **
3784538Seric **	Parameters:
3794538Seric **		fn -- filename to check.
380*64083Seric **		uid -- user id to compare against.
381*64083Seric **		gid -- group id to compare against.
382*64083Seric **		uname -- user name to compare against (used for group
383*64083Seric **			sets).
38463753Seric **		mustown -- to be safe, this uid must own the file.
3854538Seric **		mode -- mode bits that must match.
3864538Seric **
3874538Seric **	Returns:
38858247Seric **		0 if fn exists, is owned by uid, and matches mode.
38958247Seric **		An errno otherwise.  The actual errno is cleared.
3904538Seric **
3914538Seric **	Side Effects:
3924538Seric **		none.
3934538Seric */
3944538Seric 
395*64083Seric #include <grp.h>
396*64083Seric 
39763581Seric #ifndef S_IXOTH
39863581Seric # define S_IXOTH	(S_IEXEC >> 6)
39963581Seric #endif
40063581Seric 
401*64083Seric #ifndef S_IXGRP
402*64083Seric # define S_IXGRP	(S_IEXEC >> 3)
403*64083Seric #endif
404*64083Seric 
40563753Seric #ifndef S_IXUSR
40663753Seric # define S_IXUSR	(S_IEXEC)
40763753Seric #endif
40863753Seric 
40958247Seric int
410*64083Seric safefile(fn, uid, gid, uname, mustown, mode)
4114538Seric 	char *fn;
41255372Seric 	uid_t uid;
413*64083Seric 	gid_t gid;
414*64083Seric 	char *uname;
41563753Seric 	bool mustown;
4164538Seric 	int mode;
4174538Seric {
41863581Seric 	register char *p;
419*64083Seric 	register struct group *gr = NULL;
4204538Seric 	struct stat stbuf;
4214538Seric 
42263581Seric 	if (tTd(54, 4))
423*64083Seric 		printf("safefile(%s, uid=%d, gid=%d, mustown=%d, mode=%o):\n",
424*64083Seric 			fn, uid, gid, mustown, mode);
42563581Seric 	errno = 0;
42663581Seric 
42763581Seric 	for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/')
42863581Seric 	{
42963581Seric 		*p = '\0';
430*64083Seric 		if (stat(fn, &stbuf) < 0)
431*64083Seric 			break;
432*64083Seric 		if (stbuf.st_uid == uid && !bitset(S_IXUSR, stbuf.st_mode))
433*64083Seric 			break;
434*64083Seric 		if (stbuf.st_gid == gid && bitset(S_IXGRP, stbuf.st_mode))
435*64083Seric 			continue;
436*64083Seric 		if (uname != NULL &&
437*64083Seric 		    ((gr != NULL && gr->gr_gid == stbuf.st_gid) ||
438*64083Seric 		     (gr = getgrgid(stbuf.st_gid)) != NULL))
43963581Seric 		{
440*64083Seric 			register char **gp;
44163581Seric 
442*64083Seric 			for (gp = gr->gr_mem; *gp != NULL; gp++)
443*64083Seric 				if (strcmp(*gp, uname) == 0)
444*64083Seric 					break;
445*64083Seric 			if (*gp != NULL && !bitset(S_IXGRP, stbuf.st_mode))
446*64083Seric 				break;
44763581Seric 		}
448*64083Seric 		if (!bitset(S_IXOTH, stbuf.st_mode))
449*64083Seric 			break;
45063581Seric 	}
451*64083Seric 	if (p != NULL)
452*64083Seric 	{
453*64083Seric 		int ret = errno;
45463581Seric 
455*64083Seric 		if (ret == 0)
456*64083Seric 			ret = EACCES;
457*64083Seric 		if (tTd(54, 4))
458*64083Seric 			printf("\t[dir %s] %s\n", fn, errstring(ret));
459*64083Seric 		*p = '/';
460*64083Seric 		return ret;
461*64083Seric 	}
462*64083Seric 
46358247Seric 	if (stat(fn, &stbuf) < 0)
46458247Seric 	{
46558247Seric 		int ret = errno;
46658247Seric 
46763581Seric 		if (tTd(54, 4))
468*64083Seric 			printf("\t%s\n", errstring(ret));
46963581Seric 
47058247Seric 		errno = 0;
47158247Seric 		return ret;
47258247Seric 	}
47363753Seric 	if (stbuf.st_uid != uid || uid == 0 || !mustown)
47463581Seric 		mode >>= 6;
47563581Seric 	if (tTd(54, 4))
476*64083Seric 		printf("\t[uid %d, stat %o] ", stbuf.st_uid, stbuf.st_mode);
47763753Seric 	if ((stbuf.st_uid == uid || uid == 0 || !mustown) &&
47863581Seric 	    (stbuf.st_mode & mode) == mode)
47963581Seric 	{
48063581Seric 		if (tTd(54, 4))
481*64083Seric 			printf("\tOK\n");
48258247Seric 		return 0;
48363581Seric 	}
48463581Seric 	if (tTd(54, 4))
485*64083Seric 		printf("\tEACCES\n");
48663581Seric 	return EACCES;
4874538Seric }
4884538Seric /*
4894557Seric **  FIXCRLF -- fix <CR><LF> in line.
4904557Seric **
4914557Seric **	Looks for the <CR><LF> combination and turns it into the
4924557Seric **	UNIX canonical <NL> character.  It only takes one line,
4934557Seric **	i.e., it is assumed that the first <NL> found is the end
4944557Seric **	of the line.
4954557Seric **
4964557Seric **	Parameters:
4974557Seric **		line -- the line to fix.
4984557Seric **		stripnl -- if true, strip the newline also.
4994557Seric **
5004557Seric **	Returns:
5014557Seric **		none.
5024557Seric **
5034557Seric **	Side Effects:
5044557Seric **		line is changed in place.
5054557Seric */
5064557Seric 
5074557Seric fixcrlf(line, stripnl)
5084557Seric 	char *line;
5094557Seric 	bool stripnl;
5104557Seric {
5114557Seric 	register char *p;
5124557Seric 
51356795Seric 	p = strchr(line, '\n');
5144557Seric 	if (p == NULL)
5154557Seric 		return;
51636291Sbostic 	if (p > line && p[-1] == '\r')
5174557Seric 		p--;
5184557Seric 	if (!stripnl)
5194557Seric 		*p++ = '\n';
5204557Seric 	*p = '\0';
5214557Seric }
5224557Seric /*
5236890Seric **  DFOPEN -- determined file open
5246890Seric **
5256890Seric **	This routine has the semantics of fopen, except that it will
5266890Seric **	keep trying a few times to make this happen.  The idea is that
5276890Seric **	on very loaded systems, we may run out of resources (inodes,
5286890Seric **	whatever), so this tries to get around it.
5296890Seric */
5306890Seric 
53163753Seric #ifndef O_ACCMODE
53263753Seric # define O_ACCMODE	(O_RDONLY|O_WRONLY|O_RDWR)
53363753Seric #endif
53463753Seric 
53559745Seric struct omodes
53659745Seric {
53759745Seric 	int	mask;
53859745Seric 	int	mode;
53959745Seric 	char	*farg;
54059745Seric } OpenModes[] =
54159745Seric {
54259745Seric 	O_ACCMODE,		O_RDONLY,		"r",
54359745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY,		"w",
54459745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY|O_APPEND,	"a",
54559745Seric 	O_TRUNC,		0,			"w+",
54659745Seric 	O_APPEND,		O_APPEND,		"a+",
54759745Seric 	0,			0,			"r+",
54859745Seric };
54959745Seric 
5506890Seric FILE *
55159745Seric dfopen(filename, omode, cmode)
5526890Seric 	char *filename;
55359745Seric 	int omode;
55459745Seric 	int cmode;
5556890Seric {
5566890Seric 	register int tries;
55759745Seric 	int fd;
55859745Seric 	register struct omodes *om;
55959431Seric 	struct stat st;
5606890Seric 
56159745Seric 	for (om = OpenModes; om->mask != 0; om++)
56259745Seric 		if ((omode & om->mask) == om->mode)
56359745Seric 			break;
56459745Seric 
5656890Seric 	for (tries = 0; tries < 10; tries++)
5666890Seric 	{
56725618Seric 		sleep((unsigned) (10 * tries));
5686890Seric 		errno = 0;
56959745Seric 		fd = open(filename, omode, cmode);
57059745Seric 		if (fd >= 0)
5716890Seric 			break;
5729376Seric 		if (errno != ENFILE && errno != EINTR)
5739376Seric 			break;
5746890Seric 	}
57559745Seric 	if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode))
57656328Seric 	{
57756328Seric 		int locktype;
57856328Seric 
57956328Seric 		/* lock the file to avoid accidental conflicts */
58059745Seric 		if ((omode & O_ACCMODE) != O_RDONLY)
58156328Seric 			locktype = LOCK_EX;
58256328Seric 		else
58356328Seric 			locktype = LOCK_SH;
58459745Seric 		(void) lockfile(fd, filename, locktype);
58556328Seric 		errno = 0;
58656328Seric 	}
58763787Seric 	if (fd < 0)
58863787Seric 		return NULL;
58963787Seric 	else
59063787Seric 		return fdopen(fd, om->farg);
5916890Seric }
5927124Seric /*
5937124Seric **  PUTLINE -- put a line like fputs obeying SMTP conventions
5947124Seric **
5957753Seric **	This routine always guarantees outputing a newline (or CRLF,
5967753Seric **	as appropriate) at the end of the string.
5977753Seric **
5987124Seric **	Parameters:
5997124Seric **		l -- line to put.
6007124Seric **		fp -- file to put it onto.
60110172Seric **		m -- the mailer used to control output.
6027124Seric **
6037124Seric **	Returns:
6047124Seric **		none
6057124Seric **
6067124Seric **	Side Effects:
6077124Seric **		output of l to fp.
6087124Seric */
6097124Seric 
61010172Seric putline(l, fp, m)
6117753Seric 	register char *l;
6127124Seric 	FILE *fp;
61310172Seric 	MAILER *m;
6147124Seric {
6157124Seric 	register char *p;
61647157Sbostic 	register char svchar;
6177124Seric 
61811275Seric 	/* strip out 0200 bits -- these can look like TELNET protocol */
61952106Seric 	if (bitnset(M_7BITS, m->m_flags))
62011275Seric 	{
62161707Seric 		for (p = l; (svchar = *p) != '\0'; ++p)
62261707Seric 			if (bitset(0200, svchar))
62347157Sbostic 				*p = svchar &~ 0200;
62411275Seric 	}
62511275Seric 
6267753Seric 	do
6277124Seric 	{
6287753Seric 		/* find the end of the line */
62956795Seric 		p = strchr(l, '\n');
6307753Seric 		if (p == NULL)
6317753Seric 			p = &l[strlen(l)];
6327124Seric 
63363753Seric 		if (TrafficLogFile != NULL)
63463753Seric 			fprintf(TrafficLogFile, "%05d >>> ", getpid());
63563753Seric 
6367753Seric 		/* check for line overflow */
63752106Seric 		while (m->m_linelimit > 0 && (p - l) > m->m_linelimit)
6387753Seric 		{
63952106Seric 			register char *q = &l[m->m_linelimit - 1];
6407124Seric 
6417753Seric 			svchar = *q;
6427753Seric 			*q = '\0';
64310685Seric 			if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
64463753Seric 			{
64523105Seric 				(void) putc('.', fp);
64663753Seric 				if (TrafficLogFile != NULL)
64763753Seric 					(void) putc('.', TrafficLogFile);
64863753Seric 			}
6497753Seric 			fputs(l, fp);
65023105Seric 			(void) putc('!', fp);
65110326Seric 			fputs(m->m_eol, fp);
65263753Seric 			if (TrafficLogFile != NULL)
65363753Seric 				fprintf(TrafficLogFile, "%s!\n%05d >>> ",
65463753Seric 					l, getpid());
6557753Seric 			*q = svchar;
6567753Seric 			l = q;
6577753Seric 		}
6587124Seric 
6597753Seric 		/* output last part */
66010685Seric 		if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
66163753Seric 		{
66223105Seric 			(void) putc('.', fp);
66363753Seric 			if (TrafficLogFile != NULL)
66463753Seric 				(void) putc('.', TrafficLogFile);
66563753Seric 		}
66663753Seric 		if (TrafficLogFile != NULL)
66763753Seric 			fprintf(TrafficLogFile, "%.*s\n", p - l, l);
66847157Sbostic 		for ( ; l < p; ++l)
66947157Sbostic 			(void) putc(*l, fp);
67010326Seric 		fputs(m->m_eol, fp);
6717753Seric 		if (*l == '\n')
67247157Sbostic 			++l;
6737753Seric 	} while (l[0] != '\0');
6747124Seric }
6757676Seric /*
6767676Seric **  XUNLINK -- unlink a file, doing logging as appropriate.
6777676Seric **
6787676Seric **	Parameters:
6797676Seric **		f -- name of file to unlink.
6807676Seric **
6817676Seric **	Returns:
6827676Seric **		none.
6837676Seric **
6847676Seric **	Side Effects:
6857676Seric **		f is unlinked.
6867676Seric */
6877676Seric 
6887676Seric xunlink(f)
6897676Seric 	char *f;
6907676Seric {
6917676Seric 	register int i;
6927676Seric 
6937676Seric # ifdef LOG
69458020Seric 	if (LogLevel > 98)
69558020Seric 		syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f);
69656795Seric # endif /* LOG */
6977676Seric 
6987676Seric 	i = unlink(f);
6997676Seric # ifdef LOG
70058020Seric 	if (i < 0 && LogLevel > 97)
7017942Seric 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
70256795Seric # endif /* LOG */
7037676Seric }
7047685Seric /*
70558680Seric **  XFCLOSE -- close a file, doing logging as appropriate.
70658680Seric **
70758680Seric **	Parameters:
70858680Seric **		fp -- file pointer for the file to close
70958680Seric **		a, b -- miscellaneous crud to print for debugging
71058680Seric **
71158680Seric **	Returns:
71258680Seric **		none.
71358680Seric **
71458680Seric **	Side Effects:
71558680Seric **		fp is closed.
71658680Seric */
71758680Seric 
71858680Seric xfclose(fp, a, b)
71958680Seric 	FILE *fp;
72058680Seric 	char *a, *b;
72158680Seric {
72258796Seric 	if (tTd(53, 99))
72358680Seric 		printf("xfclose(%x) %s %s\n", fp, a, b);
72458796Seric 	if (fclose(fp) < 0 && tTd(53, 99))
72558680Seric 		printf("xfclose FAILURE: %s\n", errstring(errno));
72658680Seric }
72758680Seric /*
72814885Seric **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
7297685Seric **
7307685Seric **	Parameters:
7317685Seric **		buf -- place to put the input line.
7327685Seric **		siz -- size of buf.
7337685Seric **		fp -- file to read from.
73457384Seric **		timeout -- the timeout before error occurs.
73561093Seric **		during -- what we are trying to read (for error messages).
7367685Seric **
7377685Seric **	Returns:
73815533Seric **		NULL on error (including timeout).  This will also leave
73915533Seric **			buf containing a null string.
7407685Seric **		buf otherwise.
7417685Seric **
7427685Seric **	Side Effects:
7437685Seric **		none.
7447685Seric */
7457685Seric 
74614885Seric static jmp_buf	CtxReadTimeout;
74763937Seric static int	readtimeout();
7487685Seric 
7497685Seric char *
75061093Seric sfgets(buf, siz, fp, timeout, during)
7517685Seric 	char *buf;
7527685Seric 	int siz;
7537685Seric 	FILE *fp;
75457384Seric 	time_t timeout;
75561093Seric 	char *during;
7567685Seric {
7577942Seric 	register EVENT *ev = NULL;
7587685Seric 	register char *p;
7597685Seric 
76014885Seric 	/* set the timeout */
76157384Seric 	if (timeout != 0)
76214885Seric 	{
76314885Seric 		if (setjmp(CtxReadTimeout) != 0)
76414885Seric 		{
76536233Skarels # ifdef LOG
76636230Skarels 			syslog(LOG_NOTICE,
76761093Seric 			    "timeout waiting for input from %s during %s\n",
76861093Seric 			    CurHostName? CurHostName: "local", during);
76936233Skarels # endif
77036230Skarels 			errno = 0;
77161093Seric 			usrerr("451 timeout waiting for input during %s",
77261093Seric 				during);
77319037Seric 			buf[0] = '\0';
77463753Seric #ifdef XDEBUG
77563753Seric 			checkfd012(during);
77663753Seric #endif
77714885Seric 			return (NULL);
77814885Seric 		}
77957384Seric 		ev = setevent(timeout, readtimeout, 0);
78014885Seric 	}
78114885Seric 
78214885Seric 	/* try to read */
78315533Seric 	p = NULL;
78415533Seric 	while (p == NULL && !feof(fp) && !ferror(fp))
7857942Seric 	{
7867942Seric 		errno = 0;
7877942Seric 		p = fgets(buf, siz, fp);
78815533Seric 		if (errno == EINTR)
78915533Seric 			clearerr(fp);
79015533Seric 	}
79114885Seric 
79214885Seric 	/* clear the event if it has not sprung */
7937685Seric 	clrevent(ev);
79414885Seric 
79514885Seric 	/* clean up the books and exit */
7968055Seric 	LineNumber++;
79715533Seric 	if (p == NULL)
79816880Seric 	{
79915533Seric 		buf[0] = '\0';
80063753Seric 		if (TrafficLogFile != NULL)
80163753Seric 			fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid());
80216880Seric 		return (NULL);
80316880Seric 	}
80463753Seric 	if (TrafficLogFile != NULL)
80563753Seric 		fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf);
80659709Seric 	if (SevenBit)
80752106Seric 		for (p = buf; *p != '\0'; p++)
80852106Seric 			*p &= ~0200;
80916880Seric 	return (buf);
8107685Seric }
8117685Seric 
8127685Seric static
8137685Seric readtimeout()
8147685Seric {
81514885Seric 	longjmp(CtxReadTimeout, 1);
8167685Seric }
8177786Seric /*
8187786Seric **  FGETFOLDED -- like fgets, but know about folded lines.
8197786Seric **
8207786Seric **	Parameters:
8217786Seric **		buf -- place to put result.
8227786Seric **		n -- bytes available.
8237786Seric **		f -- file to read from.
8247786Seric **
8257786Seric **	Returns:
82657135Seric **		input line(s) on success, NULL on error or EOF.
82757135Seric **		This will normally be buf -- unless the line is too
82857135Seric **			long, when it will be xalloc()ed.
8297786Seric **
8307786Seric **	Side Effects:
8317786Seric **		buf gets lines from f, with continuation lines (lines
8327786Seric **		with leading white space) appended.  CRLF's are mapped
8337786Seric **		into single newlines.  Any trailing NL is stripped.
8347786Seric */
8357786Seric 
8367786Seric char *
8377786Seric fgetfolded(buf, n, f)
8387786Seric 	char *buf;
8397786Seric 	register int n;
8407786Seric 	FILE *f;
8417786Seric {
8427786Seric 	register char *p = buf;
84357135Seric 	char *bp = buf;
8447786Seric 	register int i;
8457786Seric 
8467786Seric 	n--;
84717350Seric 	while ((i = getc(f)) != EOF)
8487786Seric 	{
84917350Seric 		if (i == '\r')
85017350Seric 		{
85117350Seric 			i = getc(f);
85217350Seric 			if (i != '\n')
85317350Seric 			{
85417350Seric 				if (i != EOF)
85523105Seric 					(void) ungetc(i, f);
85617350Seric 				i = '\r';
85717350Seric 			}
85817350Seric 		}
85957135Seric 		if (--n <= 0)
86057135Seric 		{
86157135Seric 			/* allocate new space */
86257135Seric 			char *nbp;
86357135Seric 			int nn;
86457135Seric 
86557135Seric 			nn = (p - bp);
86657232Seric 			if (nn < MEMCHUNKSIZE)
86757135Seric 				nn *= 2;
86857135Seric 			else
86957232Seric 				nn += MEMCHUNKSIZE;
87057135Seric 			nbp = xalloc(nn);
87157135Seric 			bcopy(bp, nbp, p - bp);
87257135Seric 			p = &nbp[p - bp];
87357135Seric 			if (bp != buf)
87457135Seric 				free(bp);
87557135Seric 			bp = nbp;
87657135Seric 			n = nn - (p - bp);
87757135Seric 		}
87857135Seric 		*p++ = i;
87917350Seric 		if (i == '\n')
88017350Seric 		{
88117350Seric 			LineNumber++;
88217350Seric 			i = getc(f);
88317350Seric 			if (i != EOF)
88423105Seric 				(void) ungetc(i, f);
88517350Seric 			if (i != ' ' && i != '\t')
88652647Seric 				break;
88717350Seric 		}
8887786Seric 	}
88957135Seric 	if (p == bp)
89052647Seric 		return (NULL);
89152647Seric 	*--p = '\0';
89257135Seric 	return (bp);
8937786Seric }
8947860Seric /*
8957886Seric **  CURTIME -- return current time.
8967886Seric **
8977886Seric **	Parameters:
8987886Seric **		none.
8997886Seric **
9007886Seric **	Returns:
9017886Seric **		the current time.
9027886Seric **
9037886Seric **	Side Effects:
9047886Seric **		none.
9057886Seric */
9067886Seric 
9077886Seric time_t
9087886Seric curtime()
9097886Seric {
9107886Seric 	auto time_t t;
9117886Seric 
9127886Seric 	(void) time(&t);
9137886Seric 	return (t);
9147886Seric }
9158264Seric /*
9168264Seric **  ATOBOOL -- convert a string representation to boolean.
9178264Seric **
9188264Seric **	Defaults to "TRUE"
9198264Seric **
9208264Seric **	Parameters:
9218264Seric **		s -- string to convert.  Takes "tTyY" as true,
9228264Seric **			others as false.
9238264Seric **
9248264Seric **	Returns:
9258264Seric **		A boolean representation of the string.
9268264Seric **
9278264Seric **	Side Effects:
9288264Seric **		none.
9298264Seric */
9308264Seric 
9318264Seric bool
9328264Seric atobool(s)
9338264Seric 	register char *s;
9348264Seric {
93563833Seric 	if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL)
9368264Seric 		return (TRUE);
9378264Seric 	return (FALSE);
9388264Seric }
9399048Seric /*
9409048Seric **  ATOOCT -- convert a string representation to octal.
9419048Seric **
9429048Seric **	Parameters:
9439048Seric **		s -- string to convert.
9449048Seric **
9459048Seric **	Returns:
9469048Seric **		An integer representing the string interpreted as an
9479048Seric **		octal number.
9489048Seric **
9499048Seric **	Side Effects:
9509048Seric **		none.
9519048Seric */
9529048Seric 
9539048Seric atooct(s)
9549048Seric 	register char *s;
9559048Seric {
9569048Seric 	register int i = 0;
9579048Seric 
9589048Seric 	while (*s >= '0' && *s <= '7')
9599048Seric 		i = (i << 3) | (*s++ - '0');
9609048Seric 	return (i);
9619048Seric }
9629376Seric /*
9639376Seric **  WAITFOR -- wait for a particular process id.
9649376Seric **
9659376Seric **	Parameters:
9669376Seric **		pid -- process id to wait for.
9679376Seric **
9689376Seric **	Returns:
9699376Seric **		status of pid.
9709376Seric **		-1 if pid never shows up.
9719376Seric **
9729376Seric **	Side Effects:
9739376Seric **		none.
9749376Seric */
9759376Seric 
9769376Seric waitfor(pid)
9779376Seric 	int pid;
9789376Seric {
9799376Seric 	auto int st;
9809376Seric 	int i;
9819376Seric 
9829376Seric 	do
9839376Seric 	{
9849376Seric 		errno = 0;
9859376Seric 		i = wait(&st);
9869376Seric 	} while ((i >= 0 || errno == EINTR) && i != pid);
9879376Seric 	if (i < 0)
9889376Seric 		st = -1;
9899376Seric 	return (st);
9909376Seric }
9919376Seric /*
99210685Seric **  BITINTERSECT -- tell if two bitmaps intersect
99310685Seric **
99410685Seric **	Parameters:
99510685Seric **		a, b -- the bitmaps in question
99610685Seric **
99710685Seric **	Returns:
99810685Seric **		TRUE if they have a non-null intersection
99910685Seric **		FALSE otherwise
100010685Seric **
100110685Seric **	Side Effects:
100210685Seric **		none.
100310685Seric */
100410685Seric 
100510685Seric bool
100610685Seric bitintersect(a, b)
100710685Seric 	BITMAP a;
100810685Seric 	BITMAP b;
100910685Seric {
101010685Seric 	int i;
101110685Seric 
101210685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
101310685Seric 		if ((a[i] & b[i]) != 0)
101410685Seric 			return (TRUE);
101510685Seric 	return (FALSE);
101610685Seric }
101710685Seric /*
101810685Seric **  BITZEROP -- tell if a bitmap is all zero
101910685Seric **
102010685Seric **	Parameters:
102110685Seric **		map -- the bit map to check
102210685Seric **
102310685Seric **	Returns:
102410685Seric **		TRUE if map is all zero.
102510685Seric **		FALSE if there are any bits set in map.
102610685Seric **
102710685Seric **	Side Effects:
102810685Seric **		none.
102910685Seric */
103010685Seric 
103110685Seric bool
103210685Seric bitzerop(map)
103310685Seric 	BITMAP map;
103410685Seric {
103510685Seric 	int i;
103610685Seric 
103710685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
103810685Seric 		if (map[i] != 0)
103910685Seric 			return (FALSE);
104010685Seric 	return (TRUE);
104110685Seric }
104258247Seric /*
104358318Seric **  STRCONTAINEDIN -- tell if one string is contained in another
104458318Seric **
104558318Seric **	Parameters:
104658318Seric **		a -- possible substring.
104758318Seric **		b -- possible superstring.
104858318Seric **
104958318Seric **	Returns:
105058318Seric **		TRUE if a is contained in b.
105158318Seric **		FALSE otherwise.
105258318Seric */
105358318Seric 
105458318Seric bool
105558318Seric strcontainedin(a, b)
105658318Seric 	register char *a;
105758318Seric 	register char *b;
105858318Seric {
105958318Seric 	int l;
106058318Seric 
106158318Seric 	l = strlen(a);
106258318Seric 	for (;;)
106358318Seric 	{
106458318Seric 		b = strchr(b, a[0]);
106558318Seric 		if (b == NULL)
106658318Seric 			return FALSE;
106758318Seric 		if (strncmp(a, b, l) == 0)
106858318Seric 			return TRUE;
106958318Seric 		b++;
107058318Seric 	}
107158318Seric }
107263753Seric /*
107363753Seric **  CHECKFD012 -- check low numbered file descriptors
107463753Seric **
107563753Seric **	File descriptors 0, 1, and 2 should be open at all times.
107663753Seric **	This routine verifies that, and fixes it if not true.
107763753Seric **
107863753Seric **	Parameters:
107963753Seric **		where -- a tag printed if the assertion failed
108063753Seric **
108163753Seric **	Returns:
108263753Seric **		none
108363753Seric */
108463753Seric 
108563753Seric checkfd012(where)
108663753Seric 	char *where;
108763753Seric {
108863753Seric #ifdef XDEBUG
108963753Seric 	register int i;
109063753Seric 	struct stat stbuf;
109163753Seric 
109263753Seric 	for (i = 0; i < 3; i++)
109363753Seric 	{
109463753Seric 		if (fstat(i, &stbuf) < 0)
109563753Seric 		{
109663753Seric 			/* oops.... */
109763753Seric 			int fd;
109863753Seric 
109963753Seric 			syserr("%s: fd %d not open", where, i);
110063753Seric 			fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666);
110163753Seric 			if (fd != i)
110263753Seric 			{
110363753Seric 				(void) dup2(fd, i);
110463753Seric 				(void) close(fd);
110563753Seric 			}
110663753Seric 		}
110763753Seric 	}
110863937Seric #endif /* XDEBUG */
110963753Seric }
1110