xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 66748)
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*66748Seric static char sccsid[] = "@(#)util.c	8.36 (Berkeley) 04/12/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();
8177685Seric 
8187685Seric char *
81961093Seric sfgets(buf, siz, fp, timeout, during)
8207685Seric 	char *buf;
8217685Seric 	int siz;
8227685Seric 	FILE *fp;
82357384Seric 	time_t timeout;
82461093Seric 	char *during;
8257685Seric {
8267942Seric 	register EVENT *ev = NULL;
8277685Seric 	register char *p;
8287685Seric 
82966332Seric 	if (fp == NULL)
83066332Seric 	{
83166332Seric 		buf[0] = '\0';
83266332Seric 		return NULL;
83366332Seric 	}
83466332Seric 
83514885Seric 	/* set the timeout */
83657384Seric 	if (timeout != 0)
83714885Seric 	{
83814885Seric 		if (setjmp(CtxReadTimeout) != 0)
83914885Seric 		{
84036233Skarels # ifdef LOG
84136230Skarels 			syslog(LOG_NOTICE,
84261093Seric 			    "timeout waiting for input from %s during %s\n",
84361093Seric 			    CurHostName? CurHostName: "local", during);
84436233Skarels # endif
84536230Skarels 			errno = 0;
84661093Seric 			usrerr("451 timeout waiting for input during %s",
84761093Seric 				during);
84819037Seric 			buf[0] = '\0';
84963753Seric #ifdef XDEBUG
85063753Seric 			checkfd012(during);
85163753Seric #endif
85214885Seric 			return (NULL);
85314885Seric 		}
85457384Seric 		ev = setevent(timeout, readtimeout, 0);
85514885Seric 	}
85614885Seric 
85714885Seric 	/* try to read */
85815533Seric 	p = NULL;
85965190Seric 	while (!feof(fp) && !ferror(fp))
8607942Seric 	{
8617942Seric 		errno = 0;
8627942Seric 		p = fgets(buf, siz, fp);
86365190Seric 		if (p != NULL || errno != EINTR)
86465186Seric 			break;
86565190Seric 		clearerr(fp);
86615533Seric 	}
86714885Seric 
86814885Seric 	/* clear the event if it has not sprung */
8697685Seric 	clrevent(ev);
87014885Seric 
87114885Seric 	/* clean up the books and exit */
8728055Seric 	LineNumber++;
87315533Seric 	if (p == NULL)
87416880Seric 	{
87515533Seric 		buf[0] = '\0';
87663753Seric 		if (TrafficLogFile != NULL)
87763753Seric 			fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid());
87816880Seric 		return (NULL);
87916880Seric 	}
88063753Seric 	if (TrafficLogFile != NULL)
88163753Seric 		fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf);
88259709Seric 	if (SevenBit)
88352106Seric 		for (p = buf; *p != '\0'; p++)
88452106Seric 			*p &= ~0200;
88516880Seric 	return (buf);
8867685Seric }
8877685Seric 
8887685Seric static
8897685Seric readtimeout()
8907685Seric {
89114885Seric 	longjmp(CtxReadTimeout, 1);
8927685Seric }
8937786Seric /*
8947786Seric **  FGETFOLDED -- like fgets, but know about folded lines.
8957786Seric **
8967786Seric **	Parameters:
8977786Seric **		buf -- place to put result.
8987786Seric **		n -- bytes available.
8997786Seric **		f -- file to read from.
9007786Seric **
9017786Seric **	Returns:
90257135Seric **		input line(s) on success, NULL on error or EOF.
90357135Seric **		This will normally be buf -- unless the line is too
90457135Seric **			long, when it will be xalloc()ed.
9057786Seric **
9067786Seric **	Side Effects:
9077786Seric **		buf gets lines from f, with continuation lines (lines
9087786Seric **		with leading white space) appended.  CRLF's are mapped
9097786Seric **		into single newlines.  Any trailing NL is stripped.
9107786Seric */
9117786Seric 
9127786Seric char *
9137786Seric fgetfolded(buf, n, f)
9147786Seric 	char *buf;
9157786Seric 	register int n;
9167786Seric 	FILE *f;
9177786Seric {
9187786Seric 	register char *p = buf;
91957135Seric 	char *bp = buf;
9207786Seric 	register int i;
9217786Seric 
9227786Seric 	n--;
92317350Seric 	while ((i = getc(f)) != EOF)
9247786Seric 	{
92517350Seric 		if (i == '\r')
92617350Seric 		{
92717350Seric 			i = getc(f);
92817350Seric 			if (i != '\n')
92917350Seric 			{
93017350Seric 				if (i != EOF)
93123105Seric 					(void) ungetc(i, f);
93217350Seric 				i = '\r';
93317350Seric 			}
93417350Seric 		}
93557135Seric 		if (--n <= 0)
93657135Seric 		{
93757135Seric 			/* allocate new space */
93857135Seric 			char *nbp;
93957135Seric 			int nn;
94057135Seric 
94157135Seric 			nn = (p - bp);
94257232Seric 			if (nn < MEMCHUNKSIZE)
94357135Seric 				nn *= 2;
94457135Seric 			else
94557232Seric 				nn += MEMCHUNKSIZE;
94657135Seric 			nbp = xalloc(nn);
94757135Seric 			bcopy(bp, nbp, p - bp);
94857135Seric 			p = &nbp[p - bp];
94957135Seric 			if (bp != buf)
95057135Seric 				free(bp);
95157135Seric 			bp = nbp;
95257135Seric 			n = nn - (p - bp);
95357135Seric 		}
95457135Seric 		*p++ = i;
95517350Seric 		if (i == '\n')
95617350Seric 		{
95717350Seric 			LineNumber++;
95817350Seric 			i = getc(f);
95917350Seric 			if (i != EOF)
96023105Seric 				(void) ungetc(i, f);
96117350Seric 			if (i != ' ' && i != '\t')
96252647Seric 				break;
96317350Seric 		}
9647786Seric 	}
96557135Seric 	if (p == bp)
96652647Seric 		return (NULL);
96752647Seric 	*--p = '\0';
96857135Seric 	return (bp);
9697786Seric }
9707860Seric /*
9717886Seric **  CURTIME -- return current time.
9727886Seric **
9737886Seric **	Parameters:
9747886Seric **		none.
9757886Seric **
9767886Seric **	Returns:
9777886Seric **		the current time.
9787886Seric **
9797886Seric **	Side Effects:
9807886Seric **		none.
9817886Seric */
9827886Seric 
9837886Seric time_t
9847886Seric curtime()
9857886Seric {
9867886Seric 	auto time_t t;
9877886Seric 
9887886Seric 	(void) time(&t);
9897886Seric 	return (t);
9907886Seric }
9918264Seric /*
9928264Seric **  ATOBOOL -- convert a string representation to boolean.
9938264Seric **
9948264Seric **	Defaults to "TRUE"
9958264Seric **
9968264Seric **	Parameters:
9978264Seric **		s -- string to convert.  Takes "tTyY" as true,
9988264Seric **			others as false.
9998264Seric **
10008264Seric **	Returns:
10018264Seric **		A boolean representation of the string.
10028264Seric **
10038264Seric **	Side Effects:
10048264Seric **		none.
10058264Seric */
10068264Seric 
10078264Seric bool
10088264Seric atobool(s)
10098264Seric 	register char *s;
10108264Seric {
101163833Seric 	if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL)
10128264Seric 		return (TRUE);
10138264Seric 	return (FALSE);
10148264Seric }
10159048Seric /*
10169048Seric **  ATOOCT -- convert a string representation to octal.
10179048Seric **
10189048Seric **	Parameters:
10199048Seric **		s -- string to convert.
10209048Seric **
10219048Seric **	Returns:
10229048Seric **		An integer representing the string interpreted as an
10239048Seric **		octal number.
10249048Seric **
10259048Seric **	Side Effects:
10269048Seric **		none.
10279048Seric */
10289048Seric 
10299048Seric atooct(s)
10309048Seric 	register char *s;
10319048Seric {
10329048Seric 	register int i = 0;
10339048Seric 
10349048Seric 	while (*s >= '0' && *s <= '7')
10359048Seric 		i = (i << 3) | (*s++ - '0');
10369048Seric 	return (i);
10379048Seric }
10389376Seric /*
10399376Seric **  WAITFOR -- wait for a particular process id.
10409376Seric **
10419376Seric **	Parameters:
10429376Seric **		pid -- process id to wait for.
10439376Seric **
10449376Seric **	Returns:
10459376Seric **		status of pid.
10469376Seric **		-1 if pid never shows up.
10479376Seric **
10489376Seric **	Side Effects:
10499376Seric **		none.
10509376Seric */
10519376Seric 
105264562Seric int
10539376Seric waitfor(pid)
10549376Seric 	int pid;
10559376Seric {
105664562Seric #ifdef WAITUNION
105764562Seric 	union wait st;
105864562Seric #else
10599376Seric 	auto int st;
106064562Seric #endif
10619376Seric 	int i;
10629376Seric 
10639376Seric 	do
10649376Seric 	{
10659376Seric 		errno = 0;
10669376Seric 		i = wait(&st);
10679376Seric 	} while ((i >= 0 || errno == EINTR) && i != pid);
10689376Seric 	if (i < 0)
106964562Seric 		return -1;
107064562Seric #ifdef WAITUNION
107164562Seric 	return st.w_status;
107264562Seric #else
107364562Seric 	return st;
107464562Seric #endif
10759376Seric }
10769376Seric /*
107710685Seric **  BITINTERSECT -- tell if two bitmaps intersect
107810685Seric **
107910685Seric **	Parameters:
108010685Seric **		a, b -- the bitmaps in question
108110685Seric **
108210685Seric **	Returns:
108310685Seric **		TRUE if they have a non-null intersection
108410685Seric **		FALSE otherwise
108510685Seric **
108610685Seric **	Side Effects:
108710685Seric **		none.
108810685Seric */
108910685Seric 
109010685Seric bool
109110685Seric bitintersect(a, b)
109210685Seric 	BITMAP a;
109310685Seric 	BITMAP b;
109410685Seric {
109510685Seric 	int i;
109610685Seric 
109710685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
109810685Seric 		if ((a[i] & b[i]) != 0)
109910685Seric 			return (TRUE);
110010685Seric 	return (FALSE);
110110685Seric }
110210685Seric /*
110310685Seric **  BITZEROP -- tell if a bitmap is all zero
110410685Seric **
110510685Seric **	Parameters:
110610685Seric **		map -- the bit map to check
110710685Seric **
110810685Seric **	Returns:
110910685Seric **		TRUE if map is all zero.
111010685Seric **		FALSE if there are any bits set in map.
111110685Seric **
111210685Seric **	Side Effects:
111310685Seric **		none.
111410685Seric */
111510685Seric 
111610685Seric bool
111710685Seric bitzerop(map)
111810685Seric 	BITMAP map;
111910685Seric {
112010685Seric 	int i;
112110685Seric 
112210685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
112310685Seric 		if (map[i] != 0)
112410685Seric 			return (FALSE);
112510685Seric 	return (TRUE);
112610685Seric }
112758247Seric /*
112858318Seric **  STRCONTAINEDIN -- tell if one string is contained in another
112958318Seric **
113058318Seric **	Parameters:
113158318Seric **		a -- possible substring.
113258318Seric **		b -- possible superstring.
113358318Seric **
113458318Seric **	Returns:
113558318Seric **		TRUE if a is contained in b.
113658318Seric **		FALSE otherwise.
113758318Seric */
113858318Seric 
113958318Seric bool
114058318Seric strcontainedin(a, b)
114158318Seric 	register char *a;
114258318Seric 	register char *b;
114358318Seric {
114465012Seric 	int la;
114565012Seric 	int lb;
114665012Seric 	int c;
114758318Seric 
114865012Seric 	la = strlen(a);
114965012Seric 	lb = strlen(b);
115065012Seric 	c = *a;
115165012Seric 	if (isascii(c) && isupper(c))
115265012Seric 		c = tolower(c);
115365012Seric 	for (; lb-- >= la; b++)
115458318Seric 	{
115565012Seric 		if (*b != c && isascii(*b) && isupper(*b) && tolower(*b) != c)
115665012Seric 			continue;
115765012Seric 		if (strncasecmp(a, b, la) == 0)
115858318Seric 			return TRUE;
115958318Seric 	}
116065012Seric 	return FALSE;
116158318Seric }
116263753Seric /*
116363753Seric **  CHECKFD012 -- check low numbered file descriptors
116463753Seric **
116563753Seric **	File descriptors 0, 1, and 2 should be open at all times.
116663753Seric **	This routine verifies that, and fixes it if not true.
116763753Seric **
116863753Seric **	Parameters:
116963753Seric **		where -- a tag printed if the assertion failed
117063753Seric **
117163753Seric **	Returns:
117263753Seric **		none
117363753Seric */
117463753Seric 
117563753Seric checkfd012(where)
117663753Seric 	char *where;
117763753Seric {
117863753Seric #ifdef XDEBUG
117963753Seric 	register int i;
118063753Seric 	struct stat stbuf;
118163753Seric 
118263753Seric 	for (i = 0; i < 3; i++)
118363753Seric 	{
118464735Seric 		if (fstat(i, &stbuf) < 0 && errno != EOPNOTSUPP)
118563753Seric 		{
118663753Seric 			/* oops.... */
118763753Seric 			int fd;
118863753Seric 
118963753Seric 			syserr("%s: fd %d not open", where, i);
119063753Seric 			fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666);
119163753Seric 			if (fd != i)
119263753Seric 			{
119363753Seric 				(void) dup2(fd, i);
119463753Seric 				(void) close(fd);
119563753Seric 			}
119663753Seric 		}
119763753Seric 	}
119863937Seric #endif /* XDEBUG */
119963753Seric }
120064725Seric /*
120164725Seric **  PRINTOPENFDS -- print the open file descriptors (for debugging)
120264725Seric **
120364725Seric **	Parameters:
120464725Seric **		logit -- if set, send output to syslog; otherwise
120564725Seric **			print for debugging.
120664725Seric **
120764725Seric **	Returns:
120864725Seric **		none.
120964725Seric */
121064725Seric 
121164725Seric #include <netdb.h>
121264725Seric #include <arpa/inet.h>
121364725Seric 
121464725Seric printopenfds(logit)
121564725Seric 	bool logit;
121664725Seric {
121764725Seric 	register int fd;
121864743Seric 	extern int DtableSize;
121964743Seric 
122064743Seric 	for (fd = 0; fd < DtableSize; fd++)
122164743Seric 		dumpfd(fd, FALSE, logit);
122264743Seric }
122364743Seric /*
122464743Seric **  DUMPFD -- dump a file descriptor
122564743Seric **
122664743Seric **	Parameters:
122764743Seric **		fd -- the file descriptor to dump.
122864743Seric **		printclosed -- if set, print a notification even if
122964743Seric **			it is closed; otherwise print nothing.
123064743Seric **		logit -- if set, send output to syslog instead of stdout.
123164743Seric */
123264743Seric 
123364743Seric dumpfd(fd, printclosed, logit)
123464743Seric 	int fd;
123564743Seric 	bool printclosed;
123664743Seric 	bool logit;
123764743Seric {
123864725Seric 	register struct hostent *hp;
123964725Seric 	register char *p;
124066747Seric 	char *fmtstr;
124164743Seric 	struct sockaddr_in sin;
124264743Seric 	auto int slen;
124364743Seric 	struct stat st;
124464725Seric 	char buf[200];
124564725Seric 
124664743Seric 	p = buf;
124764743Seric 	sprintf(p, "%3d: ", fd);
124864743Seric 	p += strlen(p);
124964743Seric 
125064743Seric 	if (fstat(fd, &st) < 0)
125164725Seric 	{
125264743Seric 		if (printclosed || errno != EBADF)
125364743Seric 		{
125464743Seric 			sprintf(p, "CANNOT STAT (%s)", errstring(errno));
125564743Seric 			goto printit;
125664743Seric 		}
125764743Seric 		return;
125864743Seric 	}
125964725Seric 
126064743Seric 	slen = fcntl(fd, F_GETFL, NULL);
126164743Seric 	if (slen != -1)
126264743Seric 	{
126364743Seric 		sprintf(p, "fl=0x%x, ", slen);
126464743Seric 		p += strlen(p);
126564743Seric 	}
126664725Seric 
126764743Seric 	sprintf(p, "mode=%o: ", st.st_mode);
126864743Seric 	p += strlen(p);
126964743Seric 	switch (st.st_mode & S_IFMT)
127064743Seric 	{
127164807Seric #ifdef S_IFSOCK
127264743Seric 	  case S_IFSOCK:
127364743Seric 		sprintf(p, "SOCK ");
127464725Seric 		p += strlen(p);
127564743Seric 		slen = sizeof sin;
127664743Seric 		if (getsockname(fd, (struct sockaddr *) &sin, &slen) < 0)
127764743Seric 			sprintf(p, "(badsock)");
127864743Seric 		else
127964725Seric 		{
128064743Seric 			hp = gethostbyaddr((char *) &sin.sin_addr, slen, AF_INET);
128164743Seric 			sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr)
128264743Seric 						   : hp->h_name, ntohs(sin.sin_port));
128364743Seric 		}
128464743Seric 		p += strlen(p);
128564743Seric 		sprintf(p, "->");
128664743Seric 		p += strlen(p);
128764743Seric 		slen = sizeof sin;
128864743Seric 		if (getpeername(fd, (struct sockaddr *) &sin, &slen) < 0)
128964743Seric 			sprintf(p, "(badsock)");
129064743Seric 		else
129164743Seric 		{
129264743Seric 			hp = gethostbyaddr((char *) &sin.sin_addr, slen, AF_INET);
129364743Seric 			sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr)
129464743Seric 						   : hp->h_name, ntohs(sin.sin_port));
129564743Seric 		}
129664743Seric 		break;
129764807Seric #endif
129864725Seric 
129964743Seric 	  case S_IFCHR:
130064743Seric 		sprintf(p, "CHR: ");
130164743Seric 		p += strlen(p);
130264743Seric 		goto defprint;
130364725Seric 
130464743Seric 	  case S_IFBLK:
130564743Seric 		sprintf(p, "BLK: ");
130664743Seric 		p += strlen(p);
130764743Seric 		goto defprint;
130864725Seric 
130966252Seric #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK)
131065378Seric 	  case S_IFIFO:
131165378Seric 		sprintf(p, "FIFO: ");
131265378Seric 		p += strlen(p);
131365378Seric 		goto defprint;
131465378Seric #endif
131565378Seric 
131665378Seric #ifdef S_IFDIR
131765378Seric 	  case S_IFDIR:
131865378Seric 		sprintf(p, "DIR: ");
131965378Seric 		p += strlen(p);
132065378Seric 		goto defprint;
132165378Seric #endif
132265378Seric 
132365378Seric #ifdef S_IFLNK
132465378Seric 	  case S_IFLNK:
132565378Seric 		sprintf(p, "LNK: ");
132665378Seric 		p += strlen(p);
132765378Seric 		goto defprint;
132865378Seric #endif
132965378Seric 
133064743Seric 	  default:
133164725Seric defprint:
133266747Seric 		if (sizeof st.st_size > 4)
133366747Seric 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%qd",
133466747Seric 		else
133566747Seric 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld",
133666747Seric 		sprintf(p, fmtstr,
133764743Seric 			major(st.st_dev), minor(st.st_dev), st.st_ino,
133864743Seric 			st.st_nlink, st.st_uid, st.st_gid, st.st_size);
133964743Seric 		break;
134064743Seric 	}
134164725Seric 
134264743Seric printit:
1343*66748Seric #ifdef LOG
134464743Seric 	if (logit)
134565006Seric 		syslog(LOG_DEBUG, "%s", buf);
134664743Seric 	else
1347*66748Seric #endif
134864743Seric 		printf("%s\n", buf);
134964725Seric }
135065015Seric /*
135165015Seric **  SHORTENSTRING -- return short version of a string
135265015Seric **
135365015Seric **	If the string is already short, just return it.  If it is too
135465015Seric **	long, return the head and tail of the string.
135565015Seric **
135665015Seric **	Parameters:
135765015Seric **		s -- the string to shorten.
135865015Seric **		m -- the max length of the string.
135965015Seric **
136065015Seric **	Returns:
136165015Seric **		Either s or a short version of s.
136265015Seric */
136365015Seric 
136465015Seric #ifndef MAXSHORTSTR
136565055Seric # define MAXSHORTSTR	203
136665015Seric #endif
136765015Seric 
136865015Seric char *
136965015Seric shortenstring(s, m)
137065015Seric 	register char *s;
137165015Seric 	int m;
137265015Seric {
137365015Seric 	int l;
137465015Seric 	static char buf[MAXSHORTSTR + 1];
137565015Seric 
137665015Seric 	l = strlen(s);
137765015Seric 	if (l < m)
137865015Seric 		return s;
137965015Seric 	if (m > MAXSHORTSTR)
138065015Seric 		m = MAXSHORTSTR;
138165015Seric 	else if (m < 10)
138265015Seric 	{
138365015Seric 		if (m < 5)
138465015Seric 		{
138565015Seric 			strncpy(buf, s, m);
138665015Seric 			buf[m] = '\0';
138765015Seric 			return buf;
138865015Seric 		}
138965015Seric 		strncpy(buf, s, m - 3);
139065015Seric 		strcpy(buf + m - 3, "...");
139165015Seric 		return buf;
139265015Seric 	}
139365015Seric 	m = (m - 3) / 2;
139465015Seric 	strncpy(buf, s, m);
139565015Seric 	strcpy(buf + m, "...");
139665015Seric 	strcpy(buf + m + 3, s + l - m);
139765015Seric 	return buf;
139865015Seric }
1399