xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 67546)
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*67546Seric static char sccsid[] = "@(#)util.c	8.42 (Berkeley) 07/23/94";
1133731Sbostic #endif /* not lint */
1222717Sdist 
1358332Seric # include "sendmail.h"
14298Seric # include <sysexits.h>
1557135Seric /*
16298Seric **  STRIPQUOTES -- Strip quotes & quote bits from a string.
17298Seric **
18298Seric **	Runs through a string and strips off unquoted quote
19298Seric **	characters and quote bits.  This is done in place.
20298Seric **
21298Seric **	Parameters:
22298Seric **		s -- the string to strip.
23298Seric **
24298Seric **	Returns:
25298Seric **		none.
26298Seric **
27298Seric **	Side Effects:
28298Seric **		none.
29298Seric **
30298Seric **	Called By:
31298Seric **		deliver
32298Seric */
33298Seric 
3454983Seric stripquotes(s)
35298Seric 	char *s;
36298Seric {
37298Seric 	register char *p;
38298Seric 	register char *q;
39298Seric 	register char c;
40298Seric 
414101Seric 	if (s == NULL)
424101Seric 		return;
434101Seric 
4454983Seric 	p = q = s;
4554983Seric 	do
46298Seric 	{
4754983Seric 		c = *p++;
4854983Seric 		if (c == '\\')
4954983Seric 			c = *p++;
5054983Seric 		else if (c == '"')
5154983Seric 			continue;
5254983Seric 		*q++ = c;
5354983Seric 	} while (c != '\0');
54298Seric }
55298Seric /*
56298Seric **  XALLOC -- Allocate memory and bitch wildly on failure.
57298Seric **
58298Seric **	THIS IS A CLUDGE.  This should be made to give a proper
59298Seric **	error -- but after all, what can we do?
60298Seric **
61298Seric **	Parameters:
62298Seric **		sz -- size of area to allocate.
63298Seric **
64298Seric **	Returns:
65298Seric **		pointer to data region.
66298Seric **
67298Seric **	Side Effects:
68298Seric **		Memory is allocated.
69298Seric */
70298Seric 
71298Seric char *
72298Seric xalloc(sz)
737007Seric 	register int sz;
74298Seric {
75298Seric 	register char *p;
76298Seric 
7766747Seric 	/* some systems can't handle size zero mallocs */
7866747Seric 	if (sz <= 0)
7966747Seric 		sz = 1;
8066747Seric 
8123121Seric 	p = malloc((unsigned) sz);
82298Seric 	if (p == NULL)
83298Seric 	{
84298Seric 		syserr("Out of memory!!");
8510685Seric 		abort();
8610685Seric 		/* exit(EX_UNAVAILABLE); */
87298Seric 	}
88298Seric 	return (p);
89298Seric }
90298Seric /*
913151Seric **  COPYPLIST -- copy list of pointers.
923151Seric **
933151Seric **	This routine is the equivalent of newstr for lists of
943151Seric **	pointers.
953151Seric **
963151Seric **	Parameters:
973151Seric **		list -- list of pointers to copy.
983151Seric **			Must be NULL terminated.
993151Seric **		copycont -- if TRUE, copy the contents of the vector
1003151Seric **			(which must be a string) also.
1013151Seric **
1023151Seric **	Returns:
1033151Seric **		a copy of 'list'.
1043151Seric **
1053151Seric **	Side Effects:
1063151Seric **		none.
1073151Seric */
1083151Seric 
1093151Seric char **
1103151Seric copyplist(list, copycont)
1113151Seric 	char **list;
1123151Seric 	bool copycont;
1133151Seric {
1143151Seric 	register char **vp;
1153151Seric 	register char **newvp;
1163151Seric 
1173151Seric 	for (vp = list; *vp != NULL; vp++)
1183151Seric 		continue;
1193151Seric 
1203151Seric 	vp++;
1213151Seric 
12216897Seric 	newvp = (char **) xalloc((int) (vp - list) * sizeof *vp);
12316897Seric 	bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp);
1243151Seric 
1253151Seric 	if (copycont)
1263151Seric 	{
1273151Seric 		for (vp = newvp; *vp != NULL; vp++)
1283151Seric 			*vp = newstr(*vp);
1293151Seric 	}
1303151Seric 
1313151Seric 	return (newvp);
1323151Seric }
1333151Seric /*
13458170Seric **  COPYQUEUE -- copy address queue.
13558170Seric **
13658170Seric **	This routine is the equivalent of newstr for address queues
13758170Seric **	addresses marked with QDONTSEND aren't copied
13858170Seric **
13958170Seric **	Parameters:
14058170Seric **		addr -- list of address structures to copy.
14158170Seric **
14258170Seric **	Returns:
14358170Seric **		a copy of 'addr'.
14458170Seric **
14558170Seric **	Side Effects:
14658170Seric **		none.
14758170Seric */
14858170Seric 
14958170Seric ADDRESS *
15058170Seric copyqueue(addr)
15158170Seric 	ADDRESS *addr;
15258170Seric {
15358170Seric 	register ADDRESS *newaddr;
15458170Seric 	ADDRESS *ret;
15558170Seric 	register ADDRESS **tail = &ret;
15658170Seric 
15758170Seric 	while (addr != NULL)
15858170Seric 	{
15958170Seric 		if (!bitset(QDONTSEND, addr->q_flags))
16058170Seric 		{
16158170Seric 			newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS));
16258170Seric 			STRUCTCOPY(*addr, *newaddr);
16358170Seric 			*tail = newaddr;
16458170Seric 			tail = &newaddr->q_next;
16558170Seric 		}
16658170Seric 		addr = addr->q_next;
16758170Seric 	}
16858170Seric 	*tail = NULL;
16958170Seric 
17058170Seric 	return ret;
17158170Seric }
17258170Seric /*
1733151Seric **  PRINTAV -- print argument vector.
1743151Seric **
1753151Seric **	Parameters:
1763151Seric **		av -- argument vector.
1773151Seric **
1783151Seric **	Returns:
1793151Seric **		none.
1803151Seric **
1813151Seric **	Side Effects:
1823151Seric **		prints av.
1833151Seric */
1843151Seric 
1853151Seric printav(av)
1863151Seric 	register char **av;
1873151Seric {
1883151Seric 	while (*av != NULL)
1893151Seric 	{
1908063Seric 		if (tTd(0, 44))
1918063Seric 			printf("\n\t%08x=", *av);
1928063Seric 		else
19323105Seric 			(void) putchar(' ');
1943151Seric 		xputs(*av++);
1953151Seric 	}
19623105Seric 	(void) putchar('\n');
1973151Seric }
1983151Seric /*
1993151Seric **  LOWER -- turn letter into lower case.
2003151Seric **
2013151Seric **	Parameters:
2023151Seric **		c -- character to turn into lower case.
2033151Seric **
2043151Seric **	Returns:
2053151Seric **		c, in lower case.
2063151Seric **
2073151Seric **	Side Effects:
2083151Seric **		none.
2093151Seric */
2103151Seric 
2113151Seric char
2123151Seric lower(c)
2133151Seric 	register char c;
2143151Seric {
21558050Seric 	return((isascii(c) && isupper(c)) ? tolower(c) : c);
2163151Seric }
2173151Seric /*
2183151Seric **  XPUTS -- put string doing control escapes.
2193151Seric **
2203151Seric **	Parameters:
2213151Seric **		s -- string to put.
2223151Seric **
2233151Seric **	Returns:
2243151Seric **		none.
2253151Seric **
2263151Seric **	Side Effects:
2273151Seric **		output to stdout
2283151Seric */
2293151Seric 
2303151Seric xputs(s)
2313151Seric 	register char *s;
2323151Seric {
23358050Seric 	register int c;
23451781Seric 	register struct metamac *mp;
23551781Seric 	extern struct metamac MetaMacros[];
2363151Seric 
2378055Seric 	if (s == NULL)
2388055Seric 	{
2398055Seric 		printf("<null>");
2408055Seric 		return;
2418055Seric 	}
24258050Seric 	while ((c = (*s++ & 0377)) != '\0')
2433151Seric 	{
2443151Seric 		if (!isascii(c))
2453151Seric 		{
24658050Seric 			if (c == MATCHREPL || c == MACROEXPAND)
24758050Seric 			{
24858050Seric 				putchar('$');
24958050Seric 				continue;
25058050Seric 			}
25158050Seric 			for (mp = MetaMacros; mp->metaname != '\0'; mp++)
25258050Seric 			{
25358050Seric 				if ((mp->metaval & 0377) == c)
25458050Seric 				{
25558050Seric 					printf("$%c", mp->metaname);
25658050Seric 					break;
25758050Seric 				}
25858050Seric 			}
25958050Seric 			if (mp->metaname != '\0')
26058050Seric 				continue;
26123105Seric 			(void) putchar('\\');
2623151Seric 			c &= 0177;
2633151Seric 		}
26457589Seric 		if (isprint(c))
2653151Seric 		{
26657589Seric 			putchar(c);
26757589Seric 			continue;
26857589Seric 		}
26952050Seric 
27057589Seric 		/* wasn't a meta-macro -- find another way to print it */
27157589Seric 		switch (c)
27257589Seric 		{
27357589Seric 		  case '\0':
27457589Seric 			continue;
27552050Seric 
27657589Seric 		  case '\n':
27757589Seric 			c = 'n';
27857589Seric 			break;
27952050Seric 
28057589Seric 		  case '\r':
28157589Seric 			c = 'r';
28257589Seric 			break;
28352637Seric 
28457589Seric 		  case '\t':
28557589Seric 			c = 't';
28657589Seric 			break;
28757589Seric 
28857589Seric 		  default:
28957589Seric 			(void) putchar('^');
29057589Seric 			(void) putchar(c ^ 0100);
29157589Seric 			continue;
2923151Seric 		}
2933151Seric 	}
2944086Seric 	(void) fflush(stdout);
2953151Seric }
2963151Seric /*
2973151Seric **  MAKELOWER -- Translate a line into lower case
2983151Seric **
2993151Seric **	Parameters:
3003151Seric **		p -- the string to translate.  If NULL, return is
3013151Seric **			immediate.
3023151Seric **
3033151Seric **	Returns:
3043151Seric **		none.
3053151Seric **
3063151Seric **	Side Effects:
3073151Seric **		String pointed to by p is translated to lower case.
3083151Seric **
3093151Seric **	Called By:
3103151Seric **		parse
3113151Seric */
3123151Seric 
3133151Seric makelower(p)
3143151Seric 	register char *p;
3153151Seric {
3163151Seric 	register char c;
3173151Seric 
3183151Seric 	if (p == NULL)
3193151Seric 		return;
3203151Seric 	for (; (c = *p) != '\0'; p++)
3213151Seric 		if (isascii(c) && isupper(c))
32233724Sbostic 			*p = tolower(c);
3233151Seric }
3244059Seric /*
3255196Seric **  BUILDFNAME -- build full name from gecos style entry.
3264375Seric **
3275196Seric **	This routine interprets the strange entry that would appear
3285196Seric **	in the GECOS field of the password file.
3295196Seric **
3304375Seric **	Parameters:
3315196Seric **		p -- name to build.
3325196Seric **		login -- the login name of this user (for &).
3335196Seric **		buf -- place to put the result.
3344375Seric **
3354375Seric **	Returns:
33665006Seric **		none.
3374375Seric **
3384375Seric **	Side Effects:
3394375Seric **		none.
3404375Seric */
3414375Seric 
34254984Seric buildfname(gecos, login, buf)
34365006Seric 	register char *gecos;
34465006Seric 	char *login;
34565006Seric 	char *buf;
3464375Seric {
34765006Seric 	register char *p;
34865006Seric 	register char *bp = buf;
34965006Seric 	int l;
3504375Seric 
35165006Seric 	if (*gecos == '*')
35265006Seric 		gecos++;
35365006Seric 
35465006Seric 	/* find length of final string */
35565006Seric 	l = 0;
35665006Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
35754984Seric 	{
35865006Seric 		if (*p == '&')
35965006Seric 			l += strlen(login);
36065006Seric 		else
36165006Seric 			l++;
36254984Seric 	}
36365006Seric 
36465006Seric 	/* now fill in buf */
36565006Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
3664375Seric 	{
36765006Seric 		if (*p == '&')
3684375Seric 		{
36965006Seric 			(void) strcpy(bp, login);
37065006Seric 			*bp = toupper(*bp);
37165006Seric 			while (*bp != '\0')
37265006Seric 				bp++;
3734375Seric 		}
37465006Seric 		else
37565006Seric 			*bp++ = *p;
3764375Seric 	}
3774375Seric 	*bp = '\0';
3784375Seric }
3794375Seric /*
3804538Seric **  SAFEFILE -- return true if a file exists and is safe for a user.
3814538Seric **
3824538Seric **	Parameters:
3834538Seric **		fn -- filename to check.
38464083Seric **		uid -- user id to compare against.
38564083Seric **		gid -- group id to compare against.
38664083Seric **		uname -- user name to compare against (used for group
38764083Seric **			sets).
38864944Seric **		flags -- modifiers:
38965064Seric **			SFF_MUSTOWN -- "uid" must own this file.
39065064Seric **			SFF_NOSLINK -- file cannot be a symbolic link.
3914538Seric **		mode -- mode bits that must match.
3924538Seric **
3934538Seric **	Returns:
39458247Seric **		0 if fn exists, is owned by uid, and matches mode.
39558247Seric **		An errno otherwise.  The actual errno is cleared.
3964538Seric **
3974538Seric **	Side Effects:
3984538Seric **		none.
3994538Seric */
4004538Seric 
40164083Seric #include <grp.h>
40264083Seric 
40363581Seric #ifndef S_IXOTH
40463581Seric # define S_IXOTH	(S_IEXEC >> 6)
40563581Seric #endif
40663581Seric 
40764083Seric #ifndef S_IXGRP
40864083Seric # define S_IXGRP	(S_IEXEC >> 3)
40964083Seric #endif
41064083Seric 
41163753Seric #ifndef S_IXUSR
41263753Seric # define S_IXUSR	(S_IEXEC)
41363753Seric #endif
41463753Seric 
41558247Seric int
41664944Seric safefile(fn, uid, gid, uname, flags, mode)
4174538Seric 	char *fn;
41855372Seric 	uid_t uid;
41964083Seric 	gid_t gid;
42064083Seric 	char *uname;
42164944Seric 	int flags;
4224538Seric 	int mode;
4234538Seric {
42463581Seric 	register char *p;
42564083Seric 	register struct group *gr = NULL;
4264538Seric 	struct stat stbuf;
4274538Seric 
42863581Seric 	if (tTd(54, 4))
42964944Seric 		printf("safefile(%s, uid=%d, gid=%d, flags=%x, mode=%o):\n",
43064944Seric 			fn, uid, gid, flags, mode);
43163581Seric 	errno = 0;
43263581Seric 
43363581Seric 	for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/')
43463581Seric 	{
43563581Seric 		*p = '\0';
43664083Seric 		if (stat(fn, &stbuf) < 0)
43764083Seric 			break;
43865225Seric 		if (uid == 0 && !bitset(SFF_ROOTOK, flags))
43965225Seric 		{
44065225Seric 			if (bitset(S_IXOTH, stbuf.st_mode))
44165225Seric 				continue;
44265225Seric 			break;
44365225Seric 		}
44464362Seric 		if (stbuf.st_uid == uid && bitset(S_IXUSR, stbuf.st_mode))
44564362Seric 			continue;
44664083Seric 		if (stbuf.st_gid == gid && bitset(S_IXGRP, stbuf.st_mode))
44764083Seric 			continue;
44864084Seric #ifndef NO_GROUP_SET
44964083Seric 		if (uname != NULL &&
45064083Seric 		    ((gr != NULL && gr->gr_gid == stbuf.st_gid) ||
45164083Seric 		     (gr = getgrgid(stbuf.st_gid)) != NULL))
45263581Seric 		{
45364083Seric 			register char **gp;
45463581Seric 
45564083Seric 			for (gp = gr->gr_mem; *gp != NULL; gp++)
45664083Seric 				if (strcmp(*gp, uname) == 0)
45764083Seric 					break;
45864362Seric 			if (*gp != NULL && bitset(S_IXGRP, stbuf.st_mode))
45964362Seric 				continue;
46063581Seric 		}
46164084Seric #endif
46264083Seric 		if (!bitset(S_IXOTH, stbuf.st_mode))
46364083Seric 			break;
46463581Seric 	}
46564083Seric 	if (p != NULL)
46664083Seric 	{
46764083Seric 		int ret = errno;
46863581Seric 
46964083Seric 		if (ret == 0)
47064083Seric 			ret = EACCES;
47164083Seric 		if (tTd(54, 4))
47264083Seric 			printf("\t[dir %s] %s\n", fn, errstring(ret));
47364083Seric 		*p = '/';
47464083Seric 		return ret;
47564083Seric 	}
47664083Seric 
47764944Seric #ifdef HASLSTAT
47865064Seric 	if ((bitset(SFF_NOSLINK, flags) ? lstat(fn, &stbuf)
47965064Seric 					: stat(fn, &stbuf)) < 0)
48064944Seric #else
48158247Seric 	if (stat(fn, &stbuf) < 0)
48264944Seric #endif
48358247Seric 	{
48458247Seric 		int ret = errno;
48558247Seric 
48663581Seric 		if (tTd(54, 4))
48764083Seric 			printf("\t%s\n", errstring(ret));
48863581Seric 
48958247Seric 		errno = 0;
49058247Seric 		return ret;
49158247Seric 	}
49264944Seric 
49364944Seric #ifdef S_ISLNK
49465064Seric 	if (bitset(SFF_NOSLINK, flags) && S_ISLNK(stbuf.st_mode))
49564944Seric 	{
49664944Seric 		if (tTd(54, 4))
49765123Seric 			printf("\t[slink mode %o]\tEPERM\n", stbuf.st_mode);
49864944Seric 		return EPERM;
49964944Seric 	}
50064944Seric #endif
50164944Seric 
50265139Seric 	if (uid == 0 && !bitset(SFF_ROOTOK, flags))
50363581Seric 		mode >>= 6;
50464084Seric 	else if (stbuf.st_uid != uid)
50564084Seric 	{
50664084Seric 		mode >>= 3;
50764084Seric 		if (stbuf.st_gid == gid)
50864084Seric 			;
50964084Seric #ifndef NO_GROUP_SET
51064084Seric 		else if (uname != NULL &&
51164084Seric 			 ((gr != NULL && gr->gr_gid == stbuf.st_gid) ||
51264084Seric 			  (gr = getgrgid(stbuf.st_gid)) != NULL))
51364084Seric 		{
51464084Seric 			register char **gp;
51564084Seric 
51664084Seric 			for (gp = gr->gr_mem; *gp != NULL; gp++)
51764084Seric 				if (strcmp(*gp, uname) == 0)
51864084Seric 					break;
51964084Seric 			if (*gp == NULL)
52064084Seric 				mode >>= 3;
52164084Seric 		}
52264084Seric #endif
52364084Seric 		else
52464084Seric 			mode >>= 3;
52564084Seric 	}
52663581Seric 	if (tTd(54, 4))
52764084Seric 		printf("\t[uid %d, stat %o, mode %o] ",
52864084Seric 			stbuf.st_uid, stbuf.st_mode, mode);
52964944Seric 	if ((stbuf.st_uid == uid || stbuf.st_uid == 0 ||
53065064Seric 	     !bitset(SFF_MUSTOWN, flags)) &&
53163581Seric 	    (stbuf.st_mode & mode) == mode)
53263581Seric 	{
53363581Seric 		if (tTd(54, 4))
53464083Seric 			printf("\tOK\n");
53558247Seric 		return 0;
53663581Seric 	}
53763581Seric 	if (tTd(54, 4))
53864083Seric 		printf("\tEACCES\n");
53963581Seric 	return EACCES;
5404538Seric }
5414538Seric /*
5424557Seric **  FIXCRLF -- fix <CR><LF> in line.
5434557Seric **
5444557Seric **	Looks for the <CR><LF> combination and turns it into the
5454557Seric **	UNIX canonical <NL> character.  It only takes one line,
5464557Seric **	i.e., it is assumed that the first <NL> found is the end
5474557Seric **	of the line.
5484557Seric **
5494557Seric **	Parameters:
5504557Seric **		line -- the line to fix.
5514557Seric **		stripnl -- if true, strip the newline also.
5524557Seric **
5534557Seric **	Returns:
5544557Seric **		none.
5554557Seric **
5564557Seric **	Side Effects:
5574557Seric **		line is changed in place.
5584557Seric */
5594557Seric 
5604557Seric fixcrlf(line, stripnl)
5614557Seric 	char *line;
5624557Seric 	bool stripnl;
5634557Seric {
5644557Seric 	register char *p;
5654557Seric 
56656795Seric 	p = strchr(line, '\n');
5674557Seric 	if (p == NULL)
5684557Seric 		return;
56936291Sbostic 	if (p > line && p[-1] == '\r')
5704557Seric 		p--;
5714557Seric 	if (!stripnl)
5724557Seric 		*p++ = '\n';
5734557Seric 	*p = '\0';
5744557Seric }
5754557Seric /*
5766890Seric **  DFOPEN -- determined file open
5776890Seric **
5786890Seric **	This routine has the semantics of fopen, except that it will
5796890Seric **	keep trying a few times to make this happen.  The idea is that
5806890Seric **	on very loaded systems, we may run out of resources (inodes,
5816890Seric **	whatever), so this tries to get around it.
5826890Seric */
5836890Seric 
58463753Seric #ifndef O_ACCMODE
58563753Seric # define O_ACCMODE	(O_RDONLY|O_WRONLY|O_RDWR)
58663753Seric #endif
58763753Seric 
58859745Seric struct omodes
58959745Seric {
59059745Seric 	int	mask;
59159745Seric 	int	mode;
59259745Seric 	char	*farg;
59359745Seric } OpenModes[] =
59459745Seric {
59559745Seric 	O_ACCMODE,		O_RDONLY,		"r",
59659745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY,		"w",
59759745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY|O_APPEND,	"a",
59859745Seric 	O_TRUNC,		0,			"w+",
59959745Seric 	O_APPEND,		O_APPEND,		"a+",
60059745Seric 	0,			0,			"r+",
60159745Seric };
60259745Seric 
6036890Seric FILE *
60459745Seric dfopen(filename, omode, cmode)
6056890Seric 	char *filename;
60659745Seric 	int omode;
60759745Seric 	int cmode;
6086890Seric {
6096890Seric 	register int tries;
61059745Seric 	int fd;
61159745Seric 	register struct omodes *om;
61259431Seric 	struct stat st;
6136890Seric 
61459745Seric 	for (om = OpenModes; om->mask != 0; om++)
61559745Seric 		if ((omode & om->mask) == om->mode)
61659745Seric 			break;
61759745Seric 
6186890Seric 	for (tries = 0; tries < 10; tries++)
6196890Seric 	{
62025618Seric 		sleep((unsigned) (10 * tries));
6216890Seric 		errno = 0;
62259745Seric 		fd = open(filename, omode, cmode);
62359745Seric 		if (fd >= 0)
6246890Seric 			break;
62566017Seric 		switch (errno)
62666017Seric 		{
62766017Seric 		  case ENFILE:		/* system file table full */
62866017Seric 		  case EINTR:		/* interrupted syscall */
62966017Seric #ifdef ETXTBSY
63066017Seric 		  case ETXTBSY:		/* Apollo: net file locked */
63166017Seric #endif
63266017Seric 			continue;
63366017Seric 		}
63466017Seric 		break;
6356890Seric 	}
63659745Seric 	if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode))
63756328Seric 	{
63856328Seric 		int locktype;
63956328Seric 
64056328Seric 		/* lock the file to avoid accidental conflicts */
64159745Seric 		if ((omode & O_ACCMODE) != O_RDONLY)
64256328Seric 			locktype = LOCK_EX;
64356328Seric 		else
64456328Seric 			locktype = LOCK_SH;
64564335Seric 		(void) lockfile(fd, filename, NULL, locktype);
64656328Seric 		errno = 0;
64756328Seric 	}
64863787Seric 	if (fd < 0)
64963787Seric 		return NULL;
65063787Seric 	else
65163787Seric 		return fdopen(fd, om->farg);
6526890Seric }
6537124Seric /*
6547124Seric **  PUTLINE -- put a line like fputs obeying SMTP conventions
6557124Seric **
6567753Seric **	This routine always guarantees outputing a newline (or CRLF,
6577753Seric **	as appropriate) at the end of the string.
6587753Seric **
6597124Seric **	Parameters:
6607124Seric **		l -- line to put.
66165870Seric **		mci -- the mailer connection information.
6627124Seric **
6637124Seric **	Returns:
6647124Seric **		none
6657124Seric **
6667124Seric **	Side Effects:
6677124Seric **		output of l to fp.
6687124Seric */
6697124Seric 
67065870Seric putline(l, mci)
6717753Seric 	register char *l;
67265870Seric 	register MCI *mci;
6737124Seric {
6747124Seric 	register char *p;
67547157Sbostic 	register char svchar;
67666004Seric 	int slop = 0;
6777124Seric 
67811275Seric 	/* strip out 0200 bits -- these can look like TELNET protocol */
67965870Seric 	if (bitset(MCIF_7BIT, mci->mci_flags))
68011275Seric 	{
68161707Seric 		for (p = l; (svchar = *p) != '\0'; ++p)
68261707Seric 			if (bitset(0200, svchar))
68347157Sbostic 				*p = svchar &~ 0200;
68411275Seric 	}
68511275Seric 
6867753Seric 	do
6877124Seric 	{
6887753Seric 		/* find the end of the line */
68956795Seric 		p = strchr(l, '\n');
6907753Seric 		if (p == NULL)
6917753Seric 			p = &l[strlen(l)];
6927124Seric 
69363753Seric 		if (TrafficLogFile != NULL)
69463753Seric 			fprintf(TrafficLogFile, "%05d >>> ", getpid());
69563753Seric 
6967753Seric 		/* check for line overflow */
69765870Seric 		while (mci->mci_mailer->m_linelimit > 0 &&
69866004Seric 		       (p - l + slop) > mci->mci_mailer->m_linelimit)
6997753Seric 		{
70066004Seric 			register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1];
7017124Seric 
7027753Seric 			svchar = *q;
7037753Seric 			*q = '\0';
70466004Seric 			if (l[0] == '.' && slop == 0 &&
70566004Seric 			    bitnset(M_XDOT, mci->mci_mailer->m_flags))
70663753Seric 			{
70765870Seric 				(void) putc('.', mci->mci_out);
70863753Seric 				if (TrafficLogFile != NULL)
70963753Seric 					(void) putc('.', TrafficLogFile);
71063753Seric 			}
71165870Seric 			fputs(l, mci->mci_out);
71265870Seric 			(void) putc('!', mci->mci_out);
71365870Seric 			fputs(mci->mci_mailer->m_eol, mci->mci_out);
71466004Seric 			(void) putc(' ', mci->mci_out);
71563753Seric 			if (TrafficLogFile != NULL)
71666004Seric 				fprintf(TrafficLogFile, "%s!\n%05d >>>  ",
71763753Seric 					l, getpid());
7187753Seric 			*q = svchar;
7197753Seric 			l = q;
72066004Seric 			slop = 1;
7217753Seric 		}
7227124Seric 
7237753Seric 		/* output last part */
72466004Seric 		if (l[0] == '.' && slop == 0 &&
72566004Seric 		    bitnset(M_XDOT, mci->mci_mailer->m_flags))
72663753Seric 		{
72765870Seric 			(void) putc('.', mci->mci_out);
72863753Seric 			if (TrafficLogFile != NULL)
72963753Seric 				(void) putc('.', TrafficLogFile);
73063753Seric 		}
73163753Seric 		if (TrafficLogFile != NULL)
73263753Seric 			fprintf(TrafficLogFile, "%.*s\n", p - l, l);
73347157Sbostic 		for ( ; l < p; ++l)
73465870Seric 			(void) putc(*l, mci->mci_out);
73565870Seric 		fputs(mci->mci_mailer->m_eol, mci->mci_out);
7367753Seric 		if (*l == '\n')
73747157Sbostic 			++l;
7387753Seric 	} while (l[0] != '\0');
7397124Seric }
7407676Seric /*
7417676Seric **  XUNLINK -- unlink a file, doing logging as appropriate.
7427676Seric **
7437676Seric **	Parameters:
7447676Seric **		f -- name of file to unlink.
7457676Seric **
7467676Seric **	Returns:
7477676Seric **		none.
7487676Seric **
7497676Seric **	Side Effects:
7507676Seric **		f is unlinked.
7517676Seric */
7527676Seric 
7537676Seric xunlink(f)
7547676Seric 	char *f;
7557676Seric {
7567676Seric 	register int i;
7577676Seric 
7587676Seric # ifdef LOG
75958020Seric 	if (LogLevel > 98)
76058020Seric 		syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f);
76156795Seric # endif /* LOG */
7627676Seric 
7637676Seric 	i = unlink(f);
7647676Seric # ifdef LOG
76558020Seric 	if (i < 0 && LogLevel > 97)
7667942Seric 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
76756795Seric # endif /* LOG */
7687676Seric }
7697685Seric /*
77058680Seric **  XFCLOSE -- close a file, doing logging as appropriate.
77158680Seric **
77258680Seric **	Parameters:
77358680Seric **		fp -- file pointer for the file to close
77458680Seric **		a, b -- miscellaneous crud to print for debugging
77558680Seric **
77658680Seric **	Returns:
77758680Seric **		none.
77858680Seric **
77958680Seric **	Side Effects:
78058680Seric **		fp is closed.
78158680Seric */
78258680Seric 
78358680Seric xfclose(fp, a, b)
78458680Seric 	FILE *fp;
78558680Seric 	char *a, *b;
78658680Seric {
78758796Seric 	if (tTd(53, 99))
78858680Seric 		printf("xfclose(%x) %s %s\n", fp, a, b);
78964401Seric #ifdef XDEBUG
79064401Seric 	if (fileno(fp) == 1)
79164401Seric 		syserr("xfclose(%s %s): fd = 1", a, b);
79264401Seric #endif
79358796Seric 	if (fclose(fp) < 0 && tTd(53, 99))
79458680Seric 		printf("xfclose FAILURE: %s\n", errstring(errno));
79558680Seric }
79658680Seric /*
79714885Seric **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
7987685Seric **
7997685Seric **	Parameters:
8007685Seric **		buf -- place to put the input line.
8017685Seric **		siz -- size of buf.
8027685Seric **		fp -- file to read from.
80357384Seric **		timeout -- the timeout before error occurs.
80461093Seric **		during -- what we are trying to read (for error messages).
8057685Seric **
8067685Seric **	Returns:
80715533Seric **		NULL on error (including timeout).  This will also leave
80815533Seric **			buf containing a null string.
8097685Seric **		buf otherwise.
8107685Seric **
8117685Seric **	Side Effects:
8127685Seric **		none.
8137685Seric */
8147685Seric 
81514885Seric static jmp_buf	CtxReadTimeout;
81663937Seric static int	readtimeout();
81766765Seric static EVENT	*GlobalTimeout = NULL;
81866765Seric static bool	EnableTimeout = FALSE;
81966765Seric static int	ReadProgress;
8207685Seric 
8217685Seric char *
82261093Seric sfgets(buf, siz, fp, timeout, during)
8237685Seric 	char *buf;
8247685Seric 	int siz;
8257685Seric 	FILE *fp;
82657384Seric 	time_t timeout;
82761093Seric 	char *during;
8287685Seric {
8297942Seric 	register EVENT *ev = NULL;
8307685Seric 	register char *p;
8317685Seric 
83266332Seric 	if (fp == NULL)
83366332Seric 	{
83466332Seric 		buf[0] = '\0';
83566332Seric 		return NULL;
83666332Seric 	}
83766332Seric 
83814885Seric 	/* set the timeout */
83957384Seric 	if (timeout != 0)
84014885Seric 	{
84114885Seric 		if (setjmp(CtxReadTimeout) != 0)
84214885Seric 		{
84336233Skarels # ifdef LOG
84436230Skarels 			syslog(LOG_NOTICE,
84561093Seric 			    "timeout waiting for input from %s during %s\n",
84661093Seric 			    CurHostName? CurHostName: "local", during);
84736233Skarels # endif
84836230Skarels 			errno = 0;
84961093Seric 			usrerr("451 timeout waiting for input during %s",
85061093Seric 				during);
85119037Seric 			buf[0] = '\0';
85263753Seric #ifdef XDEBUG
85363753Seric 			checkfd012(during);
85463753Seric #endif
85514885Seric 			return (NULL);
85614885Seric 		}
85766765Seric 		if (GlobalTimeout == NULL)
85866765Seric 			ev = setevent(timeout, readtimeout, 0);
85966765Seric 		else
86066765Seric 			EnableTimeout = TRUE;
86114885Seric 	}
86214885Seric 
86314885Seric 	/* try to read */
86415533Seric 	p = NULL;
86565190Seric 	while (!feof(fp) && !ferror(fp))
8667942Seric 	{
8677942Seric 		errno = 0;
8687942Seric 		p = fgets(buf, siz, fp);
86965190Seric 		if (p != NULL || errno != EINTR)
87065186Seric 			break;
87165190Seric 		clearerr(fp);
87215533Seric 	}
87314885Seric 
87414885Seric 	/* clear the event if it has not sprung */
87566765Seric 	if (GlobalTimeout == NULL)
87666765Seric 		clrevent(ev);
87766765Seric 	else
87866765Seric 		EnableTimeout = FALSE;
87914885Seric 
88014885Seric 	/* clean up the books and exit */
8818055Seric 	LineNumber++;
88215533Seric 	if (p == NULL)
88316880Seric 	{
88415533Seric 		buf[0] = '\0';
88563753Seric 		if (TrafficLogFile != NULL)
88663753Seric 			fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid());
88716880Seric 		return (NULL);
88816880Seric 	}
88963753Seric 	if (TrafficLogFile != NULL)
89063753Seric 		fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf);
891*67546Seric 	if (SevenBitInput)
892*67546Seric 	{
89352106Seric 		for (p = buf; *p != '\0'; p++)
89452106Seric 			*p &= ~0200;
895*67546Seric 	}
896*67546Seric 	else if (!HasEightBits)
897*67546Seric 	{
898*67546Seric 		for (p = buf; *p != '\0'; p++)
899*67546Seric 		{
900*67546Seric 			if (bitset(0200, *p))
901*67546Seric 			{
902*67546Seric 				HasEightBits = TRUE;
903*67546Seric 				break;
904*67546Seric 			}
905*67546Seric 		}
906*67546Seric 	}
90716880Seric 	return (buf);
9087685Seric }
9097685Seric 
91066765Seric void
91166765Seric sfgetset(timeout)
91266765Seric 	time_t timeout;
91366765Seric {
91466765Seric 	/* cancel pending timer */
91566765Seric 	if (GlobalTimeout != NULL)
91666765Seric 	{
91766765Seric 		clrevent(GlobalTimeout);
91866765Seric 		GlobalTimeout = NULL;
91966765Seric 	}
92066765Seric 
92166765Seric 	/* schedule fresh one if so requested */
92266765Seric 	if (timeout != 0)
92366765Seric 	{
92466765Seric 		ReadProgress = LineNumber;
92566765Seric 		GlobalTimeout = setevent(timeout, readtimeout, timeout);
92666765Seric 	}
92766765Seric }
92866765Seric 
9297685Seric static
93066765Seric readtimeout(timeout)
93166765Seric 	time_t timeout;
9327685Seric {
93366765Seric 	/* terminate if ordinary timeout */
93466765Seric 	if (GlobalTimeout == NULL)
93566765Seric 		longjmp(CtxReadTimeout, 1);
93666765Seric 
93766765Seric 	/* terminate if no progress was made -- reset state */
93866765Seric 	if (EnableTimeout && (LineNumber <= ReadProgress))
93966765Seric 	{
94066765Seric 		EnableTimeout = FALSE;
94166765Seric 		GlobalTimeout = NULL;
94266765Seric 		longjmp(CtxReadTimeout, 2);
94366765Seric 	}
94466765Seric 
94566765Seric 	/* schedule a new timeout */
94666765Seric 	GlobalTimeout = NULL;
94766765Seric 	sfgetset(timeout);
9487685Seric }
9497786Seric /*
9507786Seric **  FGETFOLDED -- like fgets, but know about folded lines.
9517786Seric **
9527786Seric **	Parameters:
9537786Seric **		buf -- place to put result.
9547786Seric **		n -- bytes available.
9557786Seric **		f -- file to read from.
9567786Seric **
9577786Seric **	Returns:
95857135Seric **		input line(s) on success, NULL on error or EOF.
95957135Seric **		This will normally be buf -- unless the line is too
96057135Seric **			long, when it will be xalloc()ed.
9617786Seric **
9627786Seric **	Side Effects:
9637786Seric **		buf gets lines from f, with continuation lines (lines
9647786Seric **		with leading white space) appended.  CRLF's are mapped
9657786Seric **		into single newlines.  Any trailing NL is stripped.
9667786Seric */
9677786Seric 
9687786Seric char *
9697786Seric fgetfolded(buf, n, f)
9707786Seric 	char *buf;
9717786Seric 	register int n;
9727786Seric 	FILE *f;
9737786Seric {
9747786Seric 	register char *p = buf;
97557135Seric 	char *bp = buf;
9767786Seric 	register int i;
9777786Seric 
9787786Seric 	n--;
97917350Seric 	while ((i = getc(f)) != EOF)
9807786Seric 	{
98117350Seric 		if (i == '\r')
98217350Seric 		{
98317350Seric 			i = getc(f);
98417350Seric 			if (i != '\n')
98517350Seric 			{
98617350Seric 				if (i != EOF)
98723105Seric 					(void) ungetc(i, f);
98817350Seric 				i = '\r';
98917350Seric 			}
99017350Seric 		}
99157135Seric 		if (--n <= 0)
99257135Seric 		{
99357135Seric 			/* allocate new space */
99457135Seric 			char *nbp;
99557135Seric 			int nn;
99657135Seric 
99757135Seric 			nn = (p - bp);
99857232Seric 			if (nn < MEMCHUNKSIZE)
99957135Seric 				nn *= 2;
100057135Seric 			else
100157232Seric 				nn += MEMCHUNKSIZE;
100257135Seric 			nbp = xalloc(nn);
100357135Seric 			bcopy(bp, nbp, p - bp);
100457135Seric 			p = &nbp[p - bp];
100557135Seric 			if (bp != buf)
100657135Seric 				free(bp);
100757135Seric 			bp = nbp;
100857135Seric 			n = nn - (p - bp);
100957135Seric 		}
101057135Seric 		*p++ = i;
101117350Seric 		if (i == '\n')
101217350Seric 		{
101317350Seric 			LineNumber++;
101417350Seric 			i = getc(f);
101517350Seric 			if (i != EOF)
101623105Seric 				(void) ungetc(i, f);
101717350Seric 			if (i != ' ' && i != '\t')
101852647Seric 				break;
101917350Seric 		}
10207786Seric 	}
102157135Seric 	if (p == bp)
102252647Seric 		return (NULL);
102352647Seric 	*--p = '\0';
102457135Seric 	return (bp);
10257786Seric }
10267860Seric /*
10277886Seric **  CURTIME -- return current time.
10287886Seric **
10297886Seric **	Parameters:
10307886Seric **		none.
10317886Seric **
10327886Seric **	Returns:
10337886Seric **		the current time.
10347886Seric **
10357886Seric **	Side Effects:
10367886Seric **		none.
10377886Seric */
10387886Seric 
10397886Seric time_t
10407886Seric curtime()
10417886Seric {
10427886Seric 	auto time_t t;
10437886Seric 
10447886Seric 	(void) time(&t);
10457886Seric 	return (t);
10467886Seric }
10478264Seric /*
10488264Seric **  ATOBOOL -- convert a string representation to boolean.
10498264Seric **
10508264Seric **	Defaults to "TRUE"
10518264Seric **
10528264Seric **	Parameters:
10538264Seric **		s -- string to convert.  Takes "tTyY" as true,
10548264Seric **			others as false.
10558264Seric **
10568264Seric **	Returns:
10578264Seric **		A boolean representation of the string.
10588264Seric **
10598264Seric **	Side Effects:
10608264Seric **		none.
10618264Seric */
10628264Seric 
10638264Seric bool
10648264Seric atobool(s)
10658264Seric 	register char *s;
10668264Seric {
106763833Seric 	if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL)
10688264Seric 		return (TRUE);
10698264Seric 	return (FALSE);
10708264Seric }
10719048Seric /*
10729048Seric **  ATOOCT -- convert a string representation to octal.
10739048Seric **
10749048Seric **	Parameters:
10759048Seric **		s -- string to convert.
10769048Seric **
10779048Seric **	Returns:
10789048Seric **		An integer representing the string interpreted as an
10799048Seric **		octal number.
10809048Seric **
10819048Seric **	Side Effects:
10829048Seric **		none.
10839048Seric */
10849048Seric 
10859048Seric atooct(s)
10869048Seric 	register char *s;
10879048Seric {
10889048Seric 	register int i = 0;
10899048Seric 
10909048Seric 	while (*s >= '0' && *s <= '7')
10919048Seric 		i = (i << 3) | (*s++ - '0');
10929048Seric 	return (i);
10939048Seric }
10949376Seric /*
10959376Seric **  WAITFOR -- wait for a particular process id.
10969376Seric **
10979376Seric **	Parameters:
10989376Seric **		pid -- process id to wait for.
10999376Seric **
11009376Seric **	Returns:
11019376Seric **		status of pid.
11029376Seric **		-1 if pid never shows up.
11039376Seric **
11049376Seric **	Side Effects:
11059376Seric **		none.
11069376Seric */
11079376Seric 
110864562Seric int
11099376Seric waitfor(pid)
11109376Seric 	int pid;
11119376Seric {
111264562Seric #ifdef WAITUNION
111364562Seric 	union wait st;
111464562Seric #else
11159376Seric 	auto int st;
111664562Seric #endif
11179376Seric 	int i;
11189376Seric 
11199376Seric 	do
11209376Seric 	{
11219376Seric 		errno = 0;
11229376Seric 		i = wait(&st);
11239376Seric 	} while ((i >= 0 || errno == EINTR) && i != pid);
11249376Seric 	if (i < 0)
112564562Seric 		return -1;
112664562Seric #ifdef WAITUNION
112764562Seric 	return st.w_status;
112864562Seric #else
112964562Seric 	return st;
113064562Seric #endif
11319376Seric }
11329376Seric /*
113310685Seric **  BITINTERSECT -- tell if two bitmaps intersect
113410685Seric **
113510685Seric **	Parameters:
113610685Seric **		a, b -- the bitmaps in question
113710685Seric **
113810685Seric **	Returns:
113910685Seric **		TRUE if they have a non-null intersection
114010685Seric **		FALSE otherwise
114110685Seric **
114210685Seric **	Side Effects:
114310685Seric **		none.
114410685Seric */
114510685Seric 
114610685Seric bool
114710685Seric bitintersect(a, b)
114810685Seric 	BITMAP a;
114910685Seric 	BITMAP b;
115010685Seric {
115110685Seric 	int i;
115210685Seric 
115310685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
115410685Seric 		if ((a[i] & b[i]) != 0)
115510685Seric 			return (TRUE);
115610685Seric 	return (FALSE);
115710685Seric }
115810685Seric /*
115910685Seric **  BITZEROP -- tell if a bitmap is all zero
116010685Seric **
116110685Seric **	Parameters:
116210685Seric **		map -- the bit map to check
116310685Seric **
116410685Seric **	Returns:
116510685Seric **		TRUE if map is all zero.
116610685Seric **		FALSE if there are any bits set in map.
116710685Seric **
116810685Seric **	Side Effects:
116910685Seric **		none.
117010685Seric */
117110685Seric 
117210685Seric bool
117310685Seric bitzerop(map)
117410685Seric 	BITMAP map;
117510685Seric {
117610685Seric 	int i;
117710685Seric 
117810685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
117910685Seric 		if (map[i] != 0)
118010685Seric 			return (FALSE);
118110685Seric 	return (TRUE);
118210685Seric }
118358247Seric /*
118458318Seric **  STRCONTAINEDIN -- tell if one string is contained in another
118558318Seric **
118658318Seric **	Parameters:
118758318Seric **		a -- possible substring.
118858318Seric **		b -- possible superstring.
118958318Seric **
119058318Seric **	Returns:
119158318Seric **		TRUE if a is contained in b.
119258318Seric **		FALSE otherwise.
119358318Seric */
119458318Seric 
119558318Seric bool
119658318Seric strcontainedin(a, b)
119758318Seric 	register char *a;
119858318Seric 	register char *b;
119958318Seric {
120065012Seric 	int la;
120165012Seric 	int lb;
120265012Seric 	int c;
120358318Seric 
120465012Seric 	la = strlen(a);
120565012Seric 	lb = strlen(b);
120665012Seric 	c = *a;
120765012Seric 	if (isascii(c) && isupper(c))
120865012Seric 		c = tolower(c);
120965012Seric 	for (; lb-- >= la; b++)
121058318Seric 	{
121165012Seric 		if (*b != c && isascii(*b) && isupper(*b) && tolower(*b) != c)
121265012Seric 			continue;
121365012Seric 		if (strncasecmp(a, b, la) == 0)
121458318Seric 			return TRUE;
121558318Seric 	}
121665012Seric 	return FALSE;
121758318Seric }
121863753Seric /*
121963753Seric **  CHECKFD012 -- check low numbered file descriptors
122063753Seric **
122163753Seric **	File descriptors 0, 1, and 2 should be open at all times.
122263753Seric **	This routine verifies that, and fixes it if not true.
122363753Seric **
122463753Seric **	Parameters:
122563753Seric **		where -- a tag printed if the assertion failed
122663753Seric **
122763753Seric **	Returns:
122863753Seric **		none
122963753Seric */
123063753Seric 
123163753Seric checkfd012(where)
123263753Seric 	char *where;
123363753Seric {
123463753Seric #ifdef XDEBUG
123563753Seric 	register int i;
123663753Seric 	struct stat stbuf;
123763753Seric 
123863753Seric 	for (i = 0; i < 3; i++)
123963753Seric 	{
124064735Seric 		if (fstat(i, &stbuf) < 0 && errno != EOPNOTSUPP)
124163753Seric 		{
124263753Seric 			/* oops.... */
124363753Seric 			int fd;
124463753Seric 
124563753Seric 			syserr("%s: fd %d not open", where, i);
124663753Seric 			fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666);
124763753Seric 			if (fd != i)
124863753Seric 			{
124963753Seric 				(void) dup2(fd, i);
125063753Seric 				(void) close(fd);
125163753Seric 			}
125263753Seric 		}
125363753Seric 	}
125463937Seric #endif /* XDEBUG */
125563753Seric }
125664725Seric /*
125764725Seric **  PRINTOPENFDS -- print the open file descriptors (for debugging)
125864725Seric **
125964725Seric **	Parameters:
126064725Seric **		logit -- if set, send output to syslog; otherwise
126164725Seric **			print for debugging.
126264725Seric **
126364725Seric **	Returns:
126464725Seric **		none.
126564725Seric */
126664725Seric 
126764725Seric #include <netdb.h>
126864725Seric #include <arpa/inet.h>
126964725Seric 
127064725Seric printopenfds(logit)
127164725Seric 	bool logit;
127264725Seric {
127364725Seric 	register int fd;
127464743Seric 	extern int DtableSize;
127564743Seric 
127664743Seric 	for (fd = 0; fd < DtableSize; fd++)
127764743Seric 		dumpfd(fd, FALSE, logit);
127864743Seric }
127964743Seric /*
128064743Seric **  DUMPFD -- dump a file descriptor
128164743Seric **
128264743Seric **	Parameters:
128364743Seric **		fd -- the file descriptor to dump.
128464743Seric **		printclosed -- if set, print a notification even if
128564743Seric **			it is closed; otherwise print nothing.
128664743Seric **		logit -- if set, send output to syslog instead of stdout.
128764743Seric */
128864743Seric 
128964743Seric dumpfd(fd, printclosed, logit)
129064743Seric 	int fd;
129164743Seric 	bool printclosed;
129264743Seric 	bool logit;
129364743Seric {
129464725Seric 	register struct hostent *hp;
129564725Seric 	register char *p;
129666747Seric 	char *fmtstr;
129764743Seric 	struct sockaddr_in sin;
129864743Seric 	auto int slen;
129964743Seric 	struct stat st;
130064725Seric 	char buf[200];
130164725Seric 
130264743Seric 	p = buf;
130364743Seric 	sprintf(p, "%3d: ", fd);
130464743Seric 	p += strlen(p);
130564743Seric 
130664743Seric 	if (fstat(fd, &st) < 0)
130764725Seric 	{
130864743Seric 		if (printclosed || errno != EBADF)
130964743Seric 		{
131064743Seric 			sprintf(p, "CANNOT STAT (%s)", errstring(errno));
131164743Seric 			goto printit;
131264743Seric 		}
131364743Seric 		return;
131464743Seric 	}
131564725Seric 
131664743Seric 	slen = fcntl(fd, F_GETFL, NULL);
131764743Seric 	if (slen != -1)
131864743Seric 	{
131964743Seric 		sprintf(p, "fl=0x%x, ", slen);
132064743Seric 		p += strlen(p);
132164743Seric 	}
132264725Seric 
132364743Seric 	sprintf(p, "mode=%o: ", st.st_mode);
132464743Seric 	p += strlen(p);
132564743Seric 	switch (st.st_mode & S_IFMT)
132664743Seric 	{
132764807Seric #ifdef S_IFSOCK
132864743Seric 	  case S_IFSOCK:
132964743Seric 		sprintf(p, "SOCK ");
133064725Seric 		p += strlen(p);
133164743Seric 		slen = sizeof sin;
133264743Seric 		if (getsockname(fd, (struct sockaddr *) &sin, &slen) < 0)
133364743Seric 			sprintf(p, "(badsock)");
133464743Seric 		else
133564725Seric 		{
133667419Seric 			hp = gethostbyaddr((char *) &sin.sin_addr,
133767421Seric 					   INADDRSZ, AF_INET);
133864743Seric 			sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr)
133964743Seric 						   : hp->h_name, ntohs(sin.sin_port));
134064743Seric 		}
134164743Seric 		p += strlen(p);
134264743Seric 		sprintf(p, "->");
134364743Seric 		p += strlen(p);
134464743Seric 		slen = sizeof sin;
134564743Seric 		if (getpeername(fd, (struct sockaddr *) &sin, &slen) < 0)
134664743Seric 			sprintf(p, "(badsock)");
134764743Seric 		else
134864743Seric 		{
134967419Seric 			hp = gethostbyaddr((char *) &sin.sin_addr,
135067421Seric 					   INADDRSZ, AF_INET);
135164743Seric 			sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr)
135264743Seric 						   : hp->h_name, ntohs(sin.sin_port));
135364743Seric 		}
135464743Seric 		break;
135564807Seric #endif
135664725Seric 
135764743Seric 	  case S_IFCHR:
135864743Seric 		sprintf(p, "CHR: ");
135964743Seric 		p += strlen(p);
136064743Seric 		goto defprint;
136164725Seric 
136264743Seric 	  case S_IFBLK:
136364743Seric 		sprintf(p, "BLK: ");
136464743Seric 		p += strlen(p);
136564743Seric 		goto defprint;
136664725Seric 
136766252Seric #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK)
136865378Seric 	  case S_IFIFO:
136965378Seric 		sprintf(p, "FIFO: ");
137065378Seric 		p += strlen(p);
137165378Seric 		goto defprint;
137265378Seric #endif
137365378Seric 
137465378Seric #ifdef S_IFDIR
137565378Seric 	  case S_IFDIR:
137665378Seric 		sprintf(p, "DIR: ");
137765378Seric 		p += strlen(p);
137865378Seric 		goto defprint;
137965378Seric #endif
138065378Seric 
138165378Seric #ifdef S_IFLNK
138265378Seric 	  case S_IFLNK:
138365378Seric 		sprintf(p, "LNK: ");
138465378Seric 		p += strlen(p);
138565378Seric 		goto defprint;
138665378Seric #endif
138765378Seric 
138864743Seric 	  default:
138964725Seric defprint:
139066785Seric 		if (sizeof st.st_size > sizeof (long))
139166751Seric 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%qd";
139266747Seric 		else
139366751Seric 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld";
139466747Seric 		sprintf(p, fmtstr,
139564743Seric 			major(st.st_dev), minor(st.st_dev), st.st_ino,
139664743Seric 			st.st_nlink, st.st_uid, st.st_gid, st.st_size);
139764743Seric 		break;
139864743Seric 	}
139964725Seric 
140064743Seric printit:
140166748Seric #ifdef LOG
140264743Seric 	if (logit)
140365006Seric 		syslog(LOG_DEBUG, "%s", buf);
140464743Seric 	else
140566748Seric #endif
140664743Seric 		printf("%s\n", buf);
140764725Seric }
140865015Seric /*
140965015Seric **  SHORTENSTRING -- return short version of a string
141065015Seric **
141165015Seric **	If the string is already short, just return it.  If it is too
141265015Seric **	long, return the head and tail of the string.
141365015Seric **
141465015Seric **	Parameters:
141565015Seric **		s -- the string to shorten.
141665015Seric **		m -- the max length of the string.
141765015Seric **
141865015Seric **	Returns:
141965015Seric **		Either s or a short version of s.
142065015Seric */
142165015Seric 
142265015Seric #ifndef MAXSHORTSTR
142365055Seric # define MAXSHORTSTR	203
142465015Seric #endif
142565015Seric 
142665015Seric char *
142765015Seric shortenstring(s, m)
142865015Seric 	register char *s;
142965015Seric 	int m;
143065015Seric {
143165015Seric 	int l;
143265015Seric 	static char buf[MAXSHORTSTR + 1];
143365015Seric 
143465015Seric 	l = strlen(s);
143565015Seric 	if (l < m)
143665015Seric 		return s;
143765015Seric 	if (m > MAXSHORTSTR)
143865015Seric 		m = MAXSHORTSTR;
143965015Seric 	else if (m < 10)
144065015Seric 	{
144165015Seric 		if (m < 5)
144265015Seric 		{
144365015Seric 			strncpy(buf, s, m);
144465015Seric 			buf[m] = '\0';
144565015Seric 			return buf;
144665015Seric 		}
144765015Seric 		strncpy(buf, s, m - 3);
144865015Seric 		strcpy(buf + m - 3, "...");
144965015Seric 		return buf;
145065015Seric 	}
145165015Seric 	m = (m - 3) / 2;
145265015Seric 	strncpy(buf, s, m);
145365015Seric 	strcpy(buf + m, "...");
145465015Seric 	strcpy(buf + m + 3, s + l - m);
145565015Seric 	return buf;
145665015Seric }
1457