xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 67848)
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*67848Seric static char sccsid[] = "@(#)util.c	8.46 (Berkeley) 10/24/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 		{
24667768Seric 			if (c == MATCHREPL)
24758050Seric 			{
24858050Seric 				putchar('$');
24958050Seric 				continue;
25058050Seric 			}
25167768Seric 			if (c == MACROEXPAND)
25267768Seric 			{
25367768Seric 				putchar('$');
25467768Seric 				if (bitset(0200, *s))
25567768Seric 					printf("{%s}", macname(*s++ & 0377));
25667768Seric 				continue;
25767768Seric 			}
25858050Seric 			for (mp = MetaMacros; mp->metaname != '\0'; mp++)
25958050Seric 			{
26058050Seric 				if ((mp->metaval & 0377) == c)
26158050Seric 				{
26258050Seric 					printf("$%c", mp->metaname);
26358050Seric 					break;
26458050Seric 				}
26558050Seric 			}
26658050Seric 			if (mp->metaname != '\0')
26758050Seric 				continue;
26823105Seric 			(void) putchar('\\');
2693151Seric 			c &= 0177;
2703151Seric 		}
27157589Seric 		if (isprint(c))
2723151Seric 		{
27357589Seric 			putchar(c);
27457589Seric 			continue;
27557589Seric 		}
27652050Seric 
27757589Seric 		/* wasn't a meta-macro -- find another way to print it */
27857589Seric 		switch (c)
27957589Seric 		{
28057589Seric 		  case '\0':
28157589Seric 			continue;
28252050Seric 
28357589Seric 		  case '\n':
28457589Seric 			c = 'n';
28557589Seric 			break;
28652050Seric 
28757589Seric 		  case '\r':
28857589Seric 			c = 'r';
28957589Seric 			break;
29052637Seric 
29157589Seric 		  case '\t':
29257589Seric 			c = 't';
29357589Seric 			break;
29457589Seric 
29557589Seric 		  default:
29657589Seric 			(void) putchar('^');
29757589Seric 			(void) putchar(c ^ 0100);
29857589Seric 			continue;
2993151Seric 		}
3003151Seric 	}
3014086Seric 	(void) fflush(stdout);
3023151Seric }
3033151Seric /*
3043151Seric **  MAKELOWER -- Translate a line into lower case
3053151Seric **
3063151Seric **	Parameters:
3073151Seric **		p -- the string to translate.  If NULL, return is
3083151Seric **			immediate.
3093151Seric **
3103151Seric **	Returns:
3113151Seric **		none.
3123151Seric **
3133151Seric **	Side Effects:
3143151Seric **		String pointed to by p is translated to lower case.
3153151Seric **
3163151Seric **	Called By:
3173151Seric **		parse
3183151Seric */
3193151Seric 
3203151Seric makelower(p)
3213151Seric 	register char *p;
3223151Seric {
3233151Seric 	register char c;
3243151Seric 
3253151Seric 	if (p == NULL)
3263151Seric 		return;
3273151Seric 	for (; (c = *p) != '\0'; p++)
3283151Seric 		if (isascii(c) && isupper(c))
32933724Sbostic 			*p = tolower(c);
3303151Seric }
3314059Seric /*
3325196Seric **  BUILDFNAME -- build full name from gecos style entry.
3334375Seric **
3345196Seric **	This routine interprets the strange entry that would appear
3355196Seric **	in the GECOS field of the password file.
3365196Seric **
3374375Seric **	Parameters:
3385196Seric **		p -- name to build.
3395196Seric **		login -- the login name of this user (for &).
3405196Seric **		buf -- place to put the result.
3414375Seric **
3424375Seric **	Returns:
34365006Seric **		none.
3444375Seric **
3454375Seric **	Side Effects:
3464375Seric **		none.
3474375Seric */
3484375Seric 
34954984Seric buildfname(gecos, login, buf)
35065006Seric 	register char *gecos;
35165006Seric 	char *login;
35265006Seric 	char *buf;
3534375Seric {
35465006Seric 	register char *p;
35565006Seric 	register char *bp = buf;
35665006Seric 	int l;
3574375Seric 
35865006Seric 	if (*gecos == '*')
35965006Seric 		gecos++;
36065006Seric 
36165006Seric 	/* find length of final string */
36265006Seric 	l = 0;
36365006Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
36454984Seric 	{
36565006Seric 		if (*p == '&')
36665006Seric 			l += strlen(login);
36765006Seric 		else
36865006Seric 			l++;
36954984Seric 	}
37065006Seric 
37165006Seric 	/* now fill in buf */
37265006Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
3734375Seric 	{
37465006Seric 		if (*p == '&')
3754375Seric 		{
37665006Seric 			(void) strcpy(bp, login);
37765006Seric 			*bp = toupper(*bp);
37865006Seric 			while (*bp != '\0')
37965006Seric 				bp++;
3804375Seric 		}
38165006Seric 		else
38265006Seric 			*bp++ = *p;
3834375Seric 	}
3844375Seric 	*bp = '\0';
3854375Seric }
3864375Seric /*
3874538Seric **  SAFEFILE -- return true if a file exists and is safe for a user.
3884538Seric **
3894538Seric **	Parameters:
3904538Seric **		fn -- filename to check.
39164083Seric **		uid -- user id to compare against.
39264083Seric **		gid -- group id to compare against.
39364083Seric **		uname -- user name to compare against (used for group
39464083Seric **			sets).
39564944Seric **		flags -- modifiers:
39665064Seric **			SFF_MUSTOWN -- "uid" must own this file.
39765064Seric **			SFF_NOSLINK -- file cannot be a symbolic link.
3984538Seric **		mode -- mode bits that must match.
3994538Seric **
4004538Seric **	Returns:
40158247Seric **		0 if fn exists, is owned by uid, and matches mode.
40258247Seric **		An errno otherwise.  The actual errno is cleared.
4034538Seric **
4044538Seric **	Side Effects:
4054538Seric **		none.
4064538Seric */
4074538Seric 
40864083Seric #include <grp.h>
40964083Seric 
41063581Seric #ifndef S_IXOTH
41163581Seric # define S_IXOTH	(S_IEXEC >> 6)
41263581Seric #endif
41363581Seric 
41464083Seric #ifndef S_IXGRP
41564083Seric # define S_IXGRP	(S_IEXEC >> 3)
41664083Seric #endif
41764083Seric 
41863753Seric #ifndef S_IXUSR
41963753Seric # define S_IXUSR	(S_IEXEC)
42063753Seric #endif
42163753Seric 
42258247Seric int
42364944Seric safefile(fn, uid, gid, uname, flags, mode)
4244538Seric 	char *fn;
42555372Seric 	uid_t uid;
42664083Seric 	gid_t gid;
42764083Seric 	char *uname;
42864944Seric 	int flags;
4294538Seric 	int mode;
4304538Seric {
43163581Seric 	register char *p;
43264083Seric 	register struct group *gr = NULL;
4334538Seric 	struct stat stbuf;
4344538Seric 
43563581Seric 	if (tTd(54, 4))
43664944Seric 		printf("safefile(%s, uid=%d, gid=%d, flags=%x, mode=%o):\n",
43764944Seric 			fn, uid, gid, flags, mode);
43863581Seric 	errno = 0;
43963581Seric 
44063581Seric 	for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/')
44163581Seric 	{
44263581Seric 		*p = '\0';
44364083Seric 		if (stat(fn, &stbuf) < 0)
44464083Seric 			break;
44565225Seric 		if (uid == 0 && !bitset(SFF_ROOTOK, flags))
44665225Seric 		{
44765225Seric 			if (bitset(S_IXOTH, stbuf.st_mode))
44865225Seric 				continue;
44965225Seric 			break;
45065225Seric 		}
45164362Seric 		if (stbuf.st_uid == uid && bitset(S_IXUSR, stbuf.st_mode))
45264362Seric 			continue;
45364083Seric 		if (stbuf.st_gid == gid && bitset(S_IXGRP, stbuf.st_mode))
45464083Seric 			continue;
45564084Seric #ifndef NO_GROUP_SET
45664083Seric 		if (uname != NULL &&
45764083Seric 		    ((gr != NULL && gr->gr_gid == stbuf.st_gid) ||
45864083Seric 		     (gr = getgrgid(stbuf.st_gid)) != NULL))
45963581Seric 		{
46064083Seric 			register char **gp;
46163581Seric 
46267788Seric 			for (gp = gr->gr_mem; gp != NULL && *gp != NULL; gp++)
46364083Seric 				if (strcmp(*gp, uname) == 0)
46464083Seric 					break;
46567788Seric 			if (gp != NULL && *gp != NULL &&
46667788Seric 			    bitset(S_IXGRP, stbuf.st_mode))
46764362Seric 				continue;
46863581Seric 		}
46964084Seric #endif
47064083Seric 		if (!bitset(S_IXOTH, stbuf.st_mode))
47164083Seric 			break;
47263581Seric 	}
47364083Seric 	if (p != NULL)
47464083Seric 	{
47564083Seric 		int ret = errno;
47663581Seric 
47764083Seric 		if (ret == 0)
47864083Seric 			ret = EACCES;
47964083Seric 		if (tTd(54, 4))
48064083Seric 			printf("\t[dir %s] %s\n", fn, errstring(ret));
48164083Seric 		*p = '/';
48264083Seric 		return ret;
48364083Seric 	}
48464083Seric 
48564944Seric #ifdef HASLSTAT
48665064Seric 	if ((bitset(SFF_NOSLINK, flags) ? lstat(fn, &stbuf)
48765064Seric 					: stat(fn, &stbuf)) < 0)
48864944Seric #else
48958247Seric 	if (stat(fn, &stbuf) < 0)
49064944Seric #endif
49158247Seric 	{
49258247Seric 		int ret = errno;
49358247Seric 
49463581Seric 		if (tTd(54, 4))
49564083Seric 			printf("\t%s\n", errstring(ret));
49663581Seric 
49758247Seric 		errno = 0;
49858247Seric 		return ret;
49958247Seric 	}
50064944Seric 
50164944Seric #ifdef S_ISLNK
50265064Seric 	if (bitset(SFF_NOSLINK, flags) && S_ISLNK(stbuf.st_mode))
50364944Seric 	{
50464944Seric 		if (tTd(54, 4))
50565123Seric 			printf("\t[slink mode %o]\tEPERM\n", stbuf.st_mode);
50664944Seric 		return EPERM;
50764944Seric 	}
50864944Seric #endif
50964944Seric 
51065139Seric 	if (uid == 0 && !bitset(SFF_ROOTOK, flags))
51163581Seric 		mode >>= 6;
51264084Seric 	else if (stbuf.st_uid != uid)
51364084Seric 	{
51464084Seric 		mode >>= 3;
51564084Seric 		if (stbuf.st_gid == gid)
51664084Seric 			;
51764084Seric #ifndef NO_GROUP_SET
51864084Seric 		else if (uname != NULL &&
51964084Seric 			 ((gr != NULL && gr->gr_gid == stbuf.st_gid) ||
52064084Seric 			  (gr = getgrgid(stbuf.st_gid)) != NULL))
52164084Seric 		{
52264084Seric 			register char **gp;
52364084Seric 
52464084Seric 			for (gp = gr->gr_mem; *gp != NULL; gp++)
52564084Seric 				if (strcmp(*gp, uname) == 0)
52664084Seric 					break;
52764084Seric 			if (*gp == NULL)
52864084Seric 				mode >>= 3;
52964084Seric 		}
53064084Seric #endif
53164084Seric 		else
53264084Seric 			mode >>= 3;
53364084Seric 	}
53463581Seric 	if (tTd(54, 4))
53564084Seric 		printf("\t[uid %d, stat %o, mode %o] ",
53664084Seric 			stbuf.st_uid, stbuf.st_mode, mode);
53764944Seric 	if ((stbuf.st_uid == uid || stbuf.st_uid == 0 ||
53865064Seric 	     !bitset(SFF_MUSTOWN, flags)) &&
53963581Seric 	    (stbuf.st_mode & mode) == mode)
54063581Seric 	{
54163581Seric 		if (tTd(54, 4))
54264083Seric 			printf("\tOK\n");
54358247Seric 		return 0;
54463581Seric 	}
54563581Seric 	if (tTd(54, 4))
54664083Seric 		printf("\tEACCES\n");
54763581Seric 	return EACCES;
5484538Seric }
5494538Seric /*
5504557Seric **  FIXCRLF -- fix <CR><LF> in line.
5514557Seric **
5524557Seric **	Looks for the <CR><LF> combination and turns it into the
5534557Seric **	UNIX canonical <NL> character.  It only takes one line,
5544557Seric **	i.e., it is assumed that the first <NL> found is the end
5554557Seric **	of the line.
5564557Seric **
5574557Seric **	Parameters:
5584557Seric **		line -- the line to fix.
5594557Seric **		stripnl -- if true, strip the newline also.
5604557Seric **
5614557Seric **	Returns:
5624557Seric **		none.
5634557Seric **
5644557Seric **	Side Effects:
5654557Seric **		line is changed in place.
5664557Seric */
5674557Seric 
5684557Seric fixcrlf(line, stripnl)
5694557Seric 	char *line;
5704557Seric 	bool stripnl;
5714557Seric {
5724557Seric 	register char *p;
5734557Seric 
57456795Seric 	p = strchr(line, '\n');
5754557Seric 	if (p == NULL)
5764557Seric 		return;
57736291Sbostic 	if (p > line && p[-1] == '\r')
5784557Seric 		p--;
5794557Seric 	if (!stripnl)
5804557Seric 		*p++ = '\n';
5814557Seric 	*p = '\0';
5824557Seric }
5834557Seric /*
5846890Seric **  DFOPEN -- determined file open
5856890Seric **
5866890Seric **	This routine has the semantics of fopen, except that it will
5876890Seric **	keep trying a few times to make this happen.  The idea is that
5886890Seric **	on very loaded systems, we may run out of resources (inodes,
5896890Seric **	whatever), so this tries to get around it.
5906890Seric */
5916890Seric 
59263753Seric #ifndef O_ACCMODE
59363753Seric # define O_ACCMODE	(O_RDONLY|O_WRONLY|O_RDWR)
59463753Seric #endif
59563753Seric 
59659745Seric struct omodes
59759745Seric {
59859745Seric 	int	mask;
59959745Seric 	int	mode;
60059745Seric 	char	*farg;
60159745Seric } OpenModes[] =
60259745Seric {
60359745Seric 	O_ACCMODE,		O_RDONLY,		"r",
60459745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY,		"w",
60559745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY|O_APPEND,	"a",
60659745Seric 	O_TRUNC,		0,			"w+",
60759745Seric 	O_APPEND,		O_APPEND,		"a+",
60859745Seric 	0,			0,			"r+",
60959745Seric };
61059745Seric 
6116890Seric FILE *
61259745Seric dfopen(filename, omode, cmode)
6136890Seric 	char *filename;
61459745Seric 	int omode;
61559745Seric 	int cmode;
6166890Seric {
6176890Seric 	register int tries;
61859745Seric 	int fd;
61959745Seric 	register struct omodes *om;
62059431Seric 	struct stat st;
6216890Seric 
62259745Seric 	for (om = OpenModes; om->mask != 0; om++)
62359745Seric 		if ((omode & om->mask) == om->mode)
62459745Seric 			break;
62559745Seric 
6266890Seric 	for (tries = 0; tries < 10; tries++)
6276890Seric 	{
62825618Seric 		sleep((unsigned) (10 * tries));
6296890Seric 		errno = 0;
63059745Seric 		fd = open(filename, omode, cmode);
63159745Seric 		if (fd >= 0)
6326890Seric 			break;
63366017Seric 		switch (errno)
63466017Seric 		{
63566017Seric 		  case ENFILE:		/* system file table full */
63666017Seric 		  case EINTR:		/* interrupted syscall */
63766017Seric #ifdef ETXTBSY
63866017Seric 		  case ETXTBSY:		/* Apollo: net file locked */
63966017Seric #endif
64066017Seric 			continue;
64166017Seric 		}
64266017Seric 		break;
6436890Seric 	}
64459745Seric 	if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode))
64556328Seric 	{
64656328Seric 		int locktype;
64756328Seric 
64856328Seric 		/* lock the file to avoid accidental conflicts */
64959745Seric 		if ((omode & O_ACCMODE) != O_RDONLY)
65056328Seric 			locktype = LOCK_EX;
65156328Seric 		else
65256328Seric 			locktype = LOCK_SH;
65364335Seric 		(void) lockfile(fd, filename, NULL, locktype);
65456328Seric 		errno = 0;
65556328Seric 	}
65663787Seric 	if (fd < 0)
65763787Seric 		return NULL;
65863787Seric 	else
65963787Seric 		return fdopen(fd, om->farg);
6606890Seric }
6617124Seric /*
6627124Seric **  PUTLINE -- put a line like fputs obeying SMTP conventions
6637124Seric **
6647753Seric **	This routine always guarantees outputing a newline (or CRLF,
6657753Seric **	as appropriate) at the end of the string.
6667753Seric **
6677124Seric **	Parameters:
6687124Seric **		l -- line to put.
66965870Seric **		mci -- the mailer connection information.
6707124Seric **
6717124Seric **	Returns:
6727124Seric **		none
6737124Seric **
6747124Seric **	Side Effects:
6757124Seric **		output of l to fp.
6767124Seric */
6777124Seric 
67865870Seric putline(l, mci)
6797753Seric 	register char *l;
68065870Seric 	register MCI *mci;
6817124Seric {
6827124Seric 	register char *p;
68347157Sbostic 	register char svchar;
68466004Seric 	int slop = 0;
6857124Seric 
68611275Seric 	/* strip out 0200 bits -- these can look like TELNET protocol */
68765870Seric 	if (bitset(MCIF_7BIT, mci->mci_flags))
68811275Seric 	{
68961707Seric 		for (p = l; (svchar = *p) != '\0'; ++p)
69061707Seric 			if (bitset(0200, svchar))
69147157Sbostic 				*p = svchar &~ 0200;
69211275Seric 	}
69311275Seric 
6947753Seric 	do
6957124Seric 	{
6967753Seric 		/* find the end of the line */
69756795Seric 		p = strchr(l, '\n');
6987753Seric 		if (p == NULL)
6997753Seric 			p = &l[strlen(l)];
7007124Seric 
70163753Seric 		if (TrafficLogFile != NULL)
70263753Seric 			fprintf(TrafficLogFile, "%05d >>> ", getpid());
70363753Seric 
7047753Seric 		/* check for line overflow */
70565870Seric 		while (mci->mci_mailer->m_linelimit > 0 &&
70666004Seric 		       (p - l + slop) > mci->mci_mailer->m_linelimit)
7077753Seric 		{
70866004Seric 			register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1];
7097124Seric 
7107753Seric 			svchar = *q;
7117753Seric 			*q = '\0';
71266004Seric 			if (l[0] == '.' && slop == 0 &&
71366004Seric 			    bitnset(M_XDOT, mci->mci_mailer->m_flags))
71463753Seric 			{
71565870Seric 				(void) putc('.', mci->mci_out);
71663753Seric 				if (TrafficLogFile != NULL)
71763753Seric 					(void) putc('.', TrafficLogFile);
71863753Seric 			}
71965870Seric 			fputs(l, mci->mci_out);
72065870Seric 			(void) putc('!', mci->mci_out);
72165870Seric 			fputs(mci->mci_mailer->m_eol, mci->mci_out);
72266004Seric 			(void) putc(' ', mci->mci_out);
72363753Seric 			if (TrafficLogFile != NULL)
72466004Seric 				fprintf(TrafficLogFile, "%s!\n%05d >>>  ",
72563753Seric 					l, getpid());
7267753Seric 			*q = svchar;
7277753Seric 			l = q;
72866004Seric 			slop = 1;
7297753Seric 		}
7307124Seric 
7317753Seric 		/* output last part */
73266004Seric 		if (l[0] == '.' && slop == 0 &&
73366004Seric 		    bitnset(M_XDOT, mci->mci_mailer->m_flags))
73463753Seric 		{
73565870Seric 			(void) putc('.', mci->mci_out);
73663753Seric 			if (TrafficLogFile != NULL)
73763753Seric 				(void) putc('.', TrafficLogFile);
73863753Seric 		}
73963753Seric 		if (TrafficLogFile != NULL)
74063753Seric 			fprintf(TrafficLogFile, "%.*s\n", p - l, l);
74147157Sbostic 		for ( ; l < p; ++l)
74265870Seric 			(void) putc(*l, mci->mci_out);
74365870Seric 		fputs(mci->mci_mailer->m_eol, mci->mci_out);
7447753Seric 		if (*l == '\n')
74547157Sbostic 			++l;
7467753Seric 	} while (l[0] != '\0');
7477124Seric }
7487676Seric /*
7497676Seric **  XUNLINK -- unlink a file, doing logging as appropriate.
7507676Seric **
7517676Seric **	Parameters:
7527676Seric **		f -- name of file to unlink.
7537676Seric **
7547676Seric **	Returns:
7557676Seric **		none.
7567676Seric **
7577676Seric **	Side Effects:
7587676Seric **		f is unlinked.
7597676Seric */
7607676Seric 
7617676Seric xunlink(f)
7627676Seric 	char *f;
7637676Seric {
7647676Seric 	register int i;
7657676Seric 
7667676Seric # ifdef LOG
76758020Seric 	if (LogLevel > 98)
76858020Seric 		syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f);
76956795Seric # endif /* LOG */
7707676Seric 
7717676Seric 	i = unlink(f);
7727676Seric # ifdef LOG
77358020Seric 	if (i < 0 && LogLevel > 97)
7747942Seric 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
77556795Seric # endif /* LOG */
7767676Seric }
7777685Seric /*
77858680Seric **  XFCLOSE -- close a file, doing logging as appropriate.
77958680Seric **
78058680Seric **	Parameters:
78158680Seric **		fp -- file pointer for the file to close
78258680Seric **		a, b -- miscellaneous crud to print for debugging
78358680Seric **
78458680Seric **	Returns:
78558680Seric **		none.
78658680Seric **
78758680Seric **	Side Effects:
78858680Seric **		fp is closed.
78958680Seric */
79058680Seric 
79158680Seric xfclose(fp, a, b)
79258680Seric 	FILE *fp;
79358680Seric 	char *a, *b;
79458680Seric {
79558796Seric 	if (tTd(53, 99))
79658680Seric 		printf("xfclose(%x) %s %s\n", fp, a, b);
79764401Seric #ifdef XDEBUG
79864401Seric 	if (fileno(fp) == 1)
79964401Seric 		syserr("xfclose(%s %s): fd = 1", a, b);
80064401Seric #endif
80158796Seric 	if (fclose(fp) < 0 && tTd(53, 99))
80258680Seric 		printf("xfclose FAILURE: %s\n", errstring(errno));
80358680Seric }
80458680Seric /*
80514885Seric **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
8067685Seric **
8077685Seric **	Parameters:
8087685Seric **		buf -- place to put the input line.
8097685Seric **		siz -- size of buf.
8107685Seric **		fp -- file to read from.
81157384Seric **		timeout -- the timeout before error occurs.
81261093Seric **		during -- what we are trying to read (for error messages).
8137685Seric **
8147685Seric **	Returns:
81515533Seric **		NULL on error (including timeout).  This will also leave
81615533Seric **			buf containing a null string.
8177685Seric **		buf otherwise.
8187685Seric **
8197685Seric **	Side Effects:
8207685Seric **		none.
8217685Seric */
8227685Seric 
82314885Seric static jmp_buf	CtxReadTimeout;
82463937Seric static int	readtimeout();
8257685Seric 
8267685Seric char *
82761093Seric sfgets(buf, siz, fp, timeout, during)
8287685Seric 	char *buf;
8297685Seric 	int siz;
8307685Seric 	FILE *fp;
83157384Seric 	time_t timeout;
83261093Seric 	char *during;
8337685Seric {
8347942Seric 	register EVENT *ev = NULL;
8357685Seric 	register char *p;
8367685Seric 
83766332Seric 	if (fp == NULL)
83866332Seric 	{
83966332Seric 		buf[0] = '\0';
84066332Seric 		return NULL;
84166332Seric 	}
84266332Seric 
84314885Seric 	/* set the timeout */
84457384Seric 	if (timeout != 0)
84514885Seric 	{
84614885Seric 		if (setjmp(CtxReadTimeout) != 0)
84714885Seric 		{
84836233Skarels # ifdef LOG
84936230Skarels 			syslog(LOG_NOTICE,
85061093Seric 			    "timeout waiting for input from %s during %s\n",
85161093Seric 			    CurHostName? CurHostName: "local", during);
85236233Skarels # endif
85336230Skarels 			errno = 0;
85461093Seric 			usrerr("451 timeout waiting for input during %s",
85561093Seric 				during);
85619037Seric 			buf[0] = '\0';
85763753Seric #ifdef XDEBUG
85863753Seric 			checkfd012(during);
85963753Seric #endif
86014885Seric 			return (NULL);
86114885Seric 		}
86267599Seric 		ev = setevent(timeout, readtimeout, 0);
86314885Seric 	}
86414885Seric 
86514885Seric 	/* try to read */
86615533Seric 	p = NULL;
86765190Seric 	while (!feof(fp) && !ferror(fp))
8687942Seric 	{
8697942Seric 		errno = 0;
8707942Seric 		p = fgets(buf, siz, fp);
87165190Seric 		if (p != NULL || errno != EINTR)
87265186Seric 			break;
87365190Seric 		clearerr(fp);
87415533Seric 	}
87514885Seric 
87614885Seric 	/* clear the event if it has not sprung */
87767599Seric 	clrevent(ev);
87814885Seric 
87914885Seric 	/* clean up the books and exit */
8808055Seric 	LineNumber++;
88115533Seric 	if (p == NULL)
88216880Seric 	{
88315533Seric 		buf[0] = '\0';
88463753Seric 		if (TrafficLogFile != NULL)
88563753Seric 			fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid());
88616880Seric 		return (NULL);
88716880Seric 	}
88863753Seric 	if (TrafficLogFile != NULL)
88963753Seric 		fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf);
89067546Seric 	if (SevenBitInput)
89167546Seric 	{
89252106Seric 		for (p = buf; *p != '\0'; p++)
89352106Seric 			*p &= ~0200;
89467546Seric 	}
89567546Seric 	else if (!HasEightBits)
89667546Seric 	{
89767546Seric 		for (p = buf; *p != '\0'; p++)
89867546Seric 		{
89967546Seric 			if (bitset(0200, *p))
90067546Seric 			{
90167546Seric 				HasEightBits = TRUE;
90267546Seric 				break;
90367546Seric 			}
90467546Seric 		}
90567546Seric 	}
90616880Seric 	return (buf);
9077685Seric }
9087685Seric 
9097685Seric static
91066765Seric readtimeout(timeout)
91166765Seric 	time_t timeout;
9127685Seric {
91367599Seric 	longjmp(CtxReadTimeout, 1);
9147685Seric }
9157786Seric /*
9167786Seric **  FGETFOLDED -- like fgets, but know about folded lines.
9177786Seric **
9187786Seric **	Parameters:
9197786Seric **		buf -- place to put result.
9207786Seric **		n -- bytes available.
9217786Seric **		f -- file to read from.
9227786Seric **
9237786Seric **	Returns:
92457135Seric **		input line(s) on success, NULL on error or EOF.
92557135Seric **		This will normally be buf -- unless the line is too
92657135Seric **			long, when it will be xalloc()ed.
9277786Seric **
9287786Seric **	Side Effects:
9297786Seric **		buf gets lines from f, with continuation lines (lines
9307786Seric **		with leading white space) appended.  CRLF's are mapped
9317786Seric **		into single newlines.  Any trailing NL is stripped.
9327786Seric */
9337786Seric 
9347786Seric char *
9357786Seric fgetfolded(buf, n, f)
9367786Seric 	char *buf;
9377786Seric 	register int n;
9387786Seric 	FILE *f;
9397786Seric {
9407786Seric 	register char *p = buf;
94157135Seric 	char *bp = buf;
9427786Seric 	register int i;
9437786Seric 
9447786Seric 	n--;
94517350Seric 	while ((i = getc(f)) != EOF)
9467786Seric 	{
94717350Seric 		if (i == '\r')
94817350Seric 		{
94917350Seric 			i = getc(f);
95017350Seric 			if (i != '\n')
95117350Seric 			{
95217350Seric 				if (i != EOF)
95323105Seric 					(void) ungetc(i, f);
95417350Seric 				i = '\r';
95517350Seric 			}
95617350Seric 		}
95757135Seric 		if (--n <= 0)
95857135Seric 		{
95957135Seric 			/* allocate new space */
96057135Seric 			char *nbp;
96157135Seric 			int nn;
96257135Seric 
96357135Seric 			nn = (p - bp);
96457232Seric 			if (nn < MEMCHUNKSIZE)
96557135Seric 				nn *= 2;
96657135Seric 			else
96757232Seric 				nn += MEMCHUNKSIZE;
96857135Seric 			nbp = xalloc(nn);
96957135Seric 			bcopy(bp, nbp, p - bp);
97057135Seric 			p = &nbp[p - bp];
97157135Seric 			if (bp != buf)
97257135Seric 				free(bp);
97357135Seric 			bp = nbp;
97457135Seric 			n = nn - (p - bp);
97557135Seric 		}
97657135Seric 		*p++ = i;
97717350Seric 		if (i == '\n')
97817350Seric 		{
97917350Seric 			LineNumber++;
98017350Seric 			i = getc(f);
98117350Seric 			if (i != EOF)
98223105Seric 				(void) ungetc(i, f);
98317350Seric 			if (i != ' ' && i != '\t')
98452647Seric 				break;
98517350Seric 		}
9867786Seric 	}
98757135Seric 	if (p == bp)
98852647Seric 		return (NULL);
98952647Seric 	*--p = '\0';
99057135Seric 	return (bp);
9917786Seric }
9927860Seric /*
9937886Seric **  CURTIME -- return current time.
9947886Seric **
9957886Seric **	Parameters:
9967886Seric **		none.
9977886Seric **
9987886Seric **	Returns:
9997886Seric **		the current time.
10007886Seric **
10017886Seric **	Side Effects:
10027886Seric **		none.
10037886Seric */
10047886Seric 
10057886Seric time_t
10067886Seric curtime()
10077886Seric {
10087886Seric 	auto time_t t;
10097886Seric 
10107886Seric 	(void) time(&t);
10117886Seric 	return (t);
10127886Seric }
10138264Seric /*
10148264Seric **  ATOBOOL -- convert a string representation to boolean.
10158264Seric **
10168264Seric **	Defaults to "TRUE"
10178264Seric **
10188264Seric **	Parameters:
10198264Seric **		s -- string to convert.  Takes "tTyY" as true,
10208264Seric **			others as false.
10218264Seric **
10228264Seric **	Returns:
10238264Seric **		A boolean representation of the string.
10248264Seric **
10258264Seric **	Side Effects:
10268264Seric **		none.
10278264Seric */
10288264Seric 
10298264Seric bool
10308264Seric atobool(s)
10318264Seric 	register char *s;
10328264Seric {
103363833Seric 	if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL)
10348264Seric 		return (TRUE);
10358264Seric 	return (FALSE);
10368264Seric }
10379048Seric /*
10389048Seric **  ATOOCT -- convert a string representation to octal.
10399048Seric **
10409048Seric **	Parameters:
10419048Seric **		s -- string to convert.
10429048Seric **
10439048Seric **	Returns:
10449048Seric **		An integer representing the string interpreted as an
10459048Seric **		octal number.
10469048Seric **
10479048Seric **	Side Effects:
10489048Seric **		none.
10499048Seric */
10509048Seric 
10519048Seric atooct(s)
10529048Seric 	register char *s;
10539048Seric {
10549048Seric 	register int i = 0;
10559048Seric 
10569048Seric 	while (*s >= '0' && *s <= '7')
10579048Seric 		i = (i << 3) | (*s++ - '0');
10589048Seric 	return (i);
10599048Seric }
10609376Seric /*
10619376Seric **  WAITFOR -- wait for a particular process id.
10629376Seric **
10639376Seric **	Parameters:
10649376Seric **		pid -- process id to wait for.
10659376Seric **
10669376Seric **	Returns:
10679376Seric **		status of pid.
10689376Seric **		-1 if pid never shows up.
10699376Seric **
10709376Seric **	Side Effects:
10719376Seric **		none.
10729376Seric */
10739376Seric 
107464562Seric int
10759376Seric waitfor(pid)
10769376Seric 	int pid;
10779376Seric {
107864562Seric #ifdef WAITUNION
107964562Seric 	union wait st;
108064562Seric #else
10819376Seric 	auto int st;
108264562Seric #endif
10839376Seric 	int i;
10849376Seric 
10859376Seric 	do
10869376Seric 	{
10879376Seric 		errno = 0;
10889376Seric 		i = wait(&st);
10899376Seric 	} while ((i >= 0 || errno == EINTR) && i != pid);
10909376Seric 	if (i < 0)
109164562Seric 		return -1;
109264562Seric #ifdef WAITUNION
109364562Seric 	return st.w_status;
109464562Seric #else
109564562Seric 	return st;
109664562Seric #endif
10979376Seric }
10989376Seric /*
109910685Seric **  BITINTERSECT -- tell if two bitmaps intersect
110010685Seric **
110110685Seric **	Parameters:
110210685Seric **		a, b -- the bitmaps in question
110310685Seric **
110410685Seric **	Returns:
110510685Seric **		TRUE if they have a non-null intersection
110610685Seric **		FALSE otherwise
110710685Seric **
110810685Seric **	Side Effects:
110910685Seric **		none.
111010685Seric */
111110685Seric 
111210685Seric bool
111310685Seric bitintersect(a, b)
111410685Seric 	BITMAP a;
111510685Seric 	BITMAP b;
111610685Seric {
111710685Seric 	int i;
111810685Seric 
111910685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
112010685Seric 		if ((a[i] & b[i]) != 0)
112110685Seric 			return (TRUE);
112210685Seric 	return (FALSE);
112310685Seric }
112410685Seric /*
112510685Seric **  BITZEROP -- tell if a bitmap is all zero
112610685Seric **
112710685Seric **	Parameters:
112810685Seric **		map -- the bit map to check
112910685Seric **
113010685Seric **	Returns:
113110685Seric **		TRUE if map is all zero.
113210685Seric **		FALSE if there are any bits set in map.
113310685Seric **
113410685Seric **	Side Effects:
113510685Seric **		none.
113610685Seric */
113710685Seric 
113810685Seric bool
113910685Seric bitzerop(map)
114010685Seric 	BITMAP map;
114110685Seric {
114210685Seric 	int i;
114310685Seric 
114410685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
114510685Seric 		if (map[i] != 0)
114610685Seric 			return (FALSE);
114710685Seric 	return (TRUE);
114810685Seric }
114958247Seric /*
115058318Seric **  STRCONTAINEDIN -- tell if one string is contained in another
115158318Seric **
115258318Seric **	Parameters:
115358318Seric **		a -- possible substring.
115458318Seric **		b -- possible superstring.
115558318Seric **
115658318Seric **	Returns:
115758318Seric **		TRUE if a is contained in b.
115858318Seric **		FALSE otherwise.
115958318Seric */
116058318Seric 
116158318Seric bool
116258318Seric strcontainedin(a, b)
116358318Seric 	register char *a;
116458318Seric 	register char *b;
116558318Seric {
116665012Seric 	int la;
116765012Seric 	int lb;
116865012Seric 	int c;
116958318Seric 
117065012Seric 	la = strlen(a);
117165012Seric 	lb = strlen(b);
117265012Seric 	c = *a;
117365012Seric 	if (isascii(c) && isupper(c))
117465012Seric 		c = tolower(c);
117565012Seric 	for (; lb-- >= la; b++)
117658318Seric 	{
117765012Seric 		if (*b != c && isascii(*b) && isupper(*b) && tolower(*b) != c)
117865012Seric 			continue;
117965012Seric 		if (strncasecmp(a, b, la) == 0)
118058318Seric 			return TRUE;
118158318Seric 	}
118265012Seric 	return FALSE;
118358318Seric }
118463753Seric /*
118563753Seric **  CHECKFD012 -- check low numbered file descriptors
118663753Seric **
118763753Seric **	File descriptors 0, 1, and 2 should be open at all times.
118863753Seric **	This routine verifies that, and fixes it if not true.
118963753Seric **
119063753Seric **	Parameters:
119163753Seric **		where -- a tag printed if the assertion failed
119263753Seric **
119363753Seric **	Returns:
119463753Seric **		none
119563753Seric */
119663753Seric 
119763753Seric checkfd012(where)
119863753Seric 	char *where;
119963753Seric {
120063753Seric #ifdef XDEBUG
120163753Seric 	register int i;
120263753Seric 	struct stat stbuf;
120363753Seric 
120463753Seric 	for (i = 0; i < 3; i++)
120563753Seric 	{
120664735Seric 		if (fstat(i, &stbuf) < 0 && errno != EOPNOTSUPP)
120763753Seric 		{
120863753Seric 			/* oops.... */
120963753Seric 			int fd;
121063753Seric 
121163753Seric 			syserr("%s: fd %d not open", where, i);
121263753Seric 			fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666);
121363753Seric 			if (fd != i)
121463753Seric 			{
121563753Seric 				(void) dup2(fd, i);
121663753Seric 				(void) close(fd);
121763753Seric 			}
121863753Seric 		}
121963753Seric 	}
122063937Seric #endif /* XDEBUG */
122163753Seric }
122264725Seric /*
122364725Seric **  PRINTOPENFDS -- print the open file descriptors (for debugging)
122464725Seric **
122564725Seric **	Parameters:
122664725Seric **		logit -- if set, send output to syslog; otherwise
122764725Seric **			print for debugging.
122864725Seric **
122964725Seric **	Returns:
123064725Seric **		none.
123164725Seric */
123264725Seric 
123364725Seric #include <netdb.h>
123464725Seric #include <arpa/inet.h>
123564725Seric 
123664725Seric printopenfds(logit)
123764725Seric 	bool logit;
123864725Seric {
123964725Seric 	register int fd;
124064743Seric 	extern int DtableSize;
124164743Seric 
124264743Seric 	for (fd = 0; fd < DtableSize; fd++)
124364743Seric 		dumpfd(fd, FALSE, logit);
124464743Seric }
124564743Seric /*
124664743Seric **  DUMPFD -- dump a file descriptor
124764743Seric **
124864743Seric **	Parameters:
124964743Seric **		fd -- the file descriptor to dump.
125064743Seric **		printclosed -- if set, print a notification even if
125164743Seric **			it is closed; otherwise print nothing.
125264743Seric **		logit -- if set, send output to syslog instead of stdout.
125364743Seric */
125464743Seric 
125564743Seric dumpfd(fd, printclosed, logit)
125664743Seric 	int fd;
125764743Seric 	bool printclosed;
125864743Seric 	bool logit;
125964743Seric {
126064725Seric 	register struct hostent *hp;
126164725Seric 	register char *p;
126266747Seric 	char *fmtstr;
126364743Seric 	struct sockaddr_in sin;
126464743Seric 	auto int slen;
126564743Seric 	struct stat st;
126664725Seric 	char buf[200];
126764725Seric 
126864743Seric 	p = buf;
126964743Seric 	sprintf(p, "%3d: ", fd);
127064743Seric 	p += strlen(p);
127164743Seric 
127264743Seric 	if (fstat(fd, &st) < 0)
127364725Seric 	{
127464743Seric 		if (printclosed || errno != EBADF)
127564743Seric 		{
127664743Seric 			sprintf(p, "CANNOT STAT (%s)", errstring(errno));
127764743Seric 			goto printit;
127864743Seric 		}
127964743Seric 		return;
128064743Seric 	}
128164725Seric 
128264743Seric 	slen = fcntl(fd, F_GETFL, NULL);
128364743Seric 	if (slen != -1)
128464743Seric 	{
128564743Seric 		sprintf(p, "fl=0x%x, ", slen);
128664743Seric 		p += strlen(p);
128764743Seric 	}
128864725Seric 
128964743Seric 	sprintf(p, "mode=%o: ", st.st_mode);
129064743Seric 	p += strlen(p);
129164743Seric 	switch (st.st_mode & S_IFMT)
129264743Seric 	{
129364807Seric #ifdef S_IFSOCK
129464743Seric 	  case S_IFSOCK:
129564743Seric 		sprintf(p, "SOCK ");
129664725Seric 		p += strlen(p);
129764743Seric 		slen = sizeof sin;
129864743Seric 		if (getsockname(fd, (struct sockaddr *) &sin, &slen) < 0)
129964743Seric 			sprintf(p, "(badsock)");
130064743Seric 		else
130164725Seric 		{
130267419Seric 			hp = gethostbyaddr((char *) &sin.sin_addr,
130367421Seric 					   INADDRSZ, AF_INET);
130464743Seric 			sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr)
130564743Seric 						   : hp->h_name, ntohs(sin.sin_port));
130664743Seric 		}
130764743Seric 		p += strlen(p);
130864743Seric 		sprintf(p, "->");
130964743Seric 		p += strlen(p);
131064743Seric 		slen = sizeof sin;
131164743Seric 		if (getpeername(fd, (struct sockaddr *) &sin, &slen) < 0)
131264743Seric 			sprintf(p, "(badsock)");
131364743Seric 		else
131464743Seric 		{
131567419Seric 			hp = gethostbyaddr((char *) &sin.sin_addr,
131667421Seric 					   INADDRSZ, AF_INET);
131764743Seric 			sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr)
131864743Seric 						   : hp->h_name, ntohs(sin.sin_port));
131964743Seric 		}
132064743Seric 		break;
132164807Seric #endif
132264725Seric 
132364743Seric 	  case S_IFCHR:
132464743Seric 		sprintf(p, "CHR: ");
132564743Seric 		p += strlen(p);
132664743Seric 		goto defprint;
132764725Seric 
132864743Seric 	  case S_IFBLK:
132964743Seric 		sprintf(p, "BLK: ");
133064743Seric 		p += strlen(p);
133164743Seric 		goto defprint;
133264725Seric 
133366252Seric #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK)
133465378Seric 	  case S_IFIFO:
133565378Seric 		sprintf(p, "FIFO: ");
133665378Seric 		p += strlen(p);
133765378Seric 		goto defprint;
133865378Seric #endif
133965378Seric 
134065378Seric #ifdef S_IFDIR
134165378Seric 	  case S_IFDIR:
134265378Seric 		sprintf(p, "DIR: ");
134365378Seric 		p += strlen(p);
134465378Seric 		goto defprint;
134565378Seric #endif
134665378Seric 
134765378Seric #ifdef S_IFLNK
134865378Seric 	  case S_IFLNK:
134965378Seric 		sprintf(p, "LNK: ");
135065378Seric 		p += strlen(p);
135165378Seric 		goto defprint;
135265378Seric #endif
135365378Seric 
135464743Seric 	  default:
135564725Seric defprint:
135666785Seric 		if (sizeof st.st_size > sizeof (long))
135766751Seric 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%qd";
135866747Seric 		else
135966751Seric 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld";
136066747Seric 		sprintf(p, fmtstr,
136164743Seric 			major(st.st_dev), minor(st.st_dev), st.st_ino,
136264743Seric 			st.st_nlink, st.st_uid, st.st_gid, st.st_size);
136364743Seric 		break;
136464743Seric 	}
136564725Seric 
136664743Seric printit:
136766748Seric #ifdef LOG
136864743Seric 	if (logit)
136965006Seric 		syslog(LOG_DEBUG, "%s", buf);
137064743Seric 	else
137166748Seric #endif
137264743Seric 		printf("%s\n", buf);
137364725Seric }
137465015Seric /*
137565015Seric **  SHORTENSTRING -- return short version of a string
137665015Seric **
137765015Seric **	If the string is already short, just return it.  If it is too
137865015Seric **	long, return the head and tail of the string.
137965015Seric **
138065015Seric **	Parameters:
138165015Seric **		s -- the string to shorten.
138265015Seric **		m -- the max length of the string.
138365015Seric **
138465015Seric **	Returns:
138565015Seric **		Either s or a short version of s.
138665015Seric */
138765015Seric 
138865015Seric #ifndef MAXSHORTSTR
138965055Seric # define MAXSHORTSTR	203
139065015Seric #endif
139165015Seric 
139265015Seric char *
139365015Seric shortenstring(s, m)
139465015Seric 	register char *s;
139565015Seric 	int m;
139665015Seric {
139765015Seric 	int l;
139865015Seric 	static char buf[MAXSHORTSTR + 1];
139965015Seric 
140065015Seric 	l = strlen(s);
140165015Seric 	if (l < m)
140265015Seric 		return s;
140365015Seric 	if (m > MAXSHORTSTR)
140465015Seric 		m = MAXSHORTSTR;
140565015Seric 	else if (m < 10)
140665015Seric 	{
140765015Seric 		if (m < 5)
140865015Seric 		{
140965015Seric 			strncpy(buf, s, m);
141065015Seric 			buf[m] = '\0';
141165015Seric 			return buf;
141265015Seric 		}
141365015Seric 		strncpy(buf, s, m - 3);
141465015Seric 		strcpy(buf + m - 3, "...");
141565015Seric 		return buf;
141665015Seric 	}
141765015Seric 	m = (m - 3) / 2;
141865015Seric 	strncpy(buf, s, m);
141965015Seric 	strcpy(buf + m, "...");
142065015Seric 	strcpy(buf + m + 3, s + l - m);
142165015Seric 	return buf;
142265015Seric }
1423*67848Seric /*
1424*67848Seric **  GET_COLUMN  -- look up a Column in a line buffer
1425*67848Seric **
1426*67848Seric **	Parameters:
1427*67848Seric **		line -- the raw text line to search.
1428*67848Seric **		col -- the column number to fetch.
1429*67848Seric **		delim -- the delimiter between columns.  If null,
1430*67848Seric **			use white space.
1431*67848Seric **		buf -- the output buffer.
1432*67848Seric **
1433*67848Seric **	Returns:
1434*67848Seric **		buf if successful.
1435*67848Seric **		NULL otherwise.
1436*67848Seric */
1437*67848Seric 
1438*67848Seric char *
1439*67848Seric get_column(line, col, delim, buf)
1440*67848Seric 	char line[];
1441*67848Seric 	int col;
1442*67848Seric 	char delim;
1443*67848Seric 	char buf[];
1444*67848Seric {
1445*67848Seric 	char *p;
1446*67848Seric 	char *begin, *end;
1447*67848Seric 	int i;
1448*67848Seric 	char delimbuf[3];
1449*67848Seric 
1450*67848Seric 	if (delim == '\0')
1451*67848Seric 		strcpy(delimbuf, "\t ");
1452*67848Seric 	else
1453*67848Seric 	{
1454*67848Seric 		delimbuf[0] = delim;
1455*67848Seric 		delimbuf[1] = '\0';
1456*67848Seric 	}
1457*67848Seric 
1458*67848Seric 	p = line;
1459*67848Seric 	if (*p == '\0')
1460*67848Seric 		return NULL;			/* line empty */
1461*67848Seric 	if (*p == delim && col == 0)
1462*67848Seric 		return NULL;			/* first column empty */
1463*67848Seric 
1464*67848Seric 	begin = line;
1465*67848Seric 
1466*67848Seric 	if (col == 0 && delim == '\0')
1467*67848Seric 	{
1468*67848Seric 		while (*begin && isspace(*begin))
1469*67848Seric 			begin++;
1470*67848Seric 	}
1471*67848Seric 
1472*67848Seric 	for (i = 0; i < col; i++)
1473*67848Seric 	{
1474*67848Seric 		if ((begin = strpbrk(begin, delimbuf)) == NULL)
1475*67848Seric 			return NULL;		/* no such column */
1476*67848Seric 		begin++;
1477*67848Seric 		if (delim == '\0')
1478*67848Seric 		{
1479*67848Seric 			while (*begin && isspace(*begin))
1480*67848Seric 				begin++;
1481*67848Seric 		}
1482*67848Seric 	}
1483*67848Seric 
1484*67848Seric 	end = strpbrk(begin, delimbuf);
1485*67848Seric 	if (end == NULL)
1486*67848Seric 	{
1487*67848Seric 		strcpy(buf, begin);
1488*67848Seric 	}
1489*67848Seric 	else
1490*67848Seric 	{
1491*67848Seric 		strncpy(buf, begin, end - begin);
1492*67848Seric 		buf[end - begin] = '\0';
1493*67848Seric 	}
1494*67848Seric 	return buf;
1495*67848Seric }
1496