xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 66017)
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*66017Seric static char sccsid[] = "@(#)util.c	8.31 (Berkeley) 02/06/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 
7723121Seric 	p = malloc((unsigned) sz);
78298Seric 	if (p == NULL)
79298Seric 	{
80298Seric 		syserr("Out of memory!!");
8110685Seric 		abort();
8210685Seric 		/* exit(EX_UNAVAILABLE); */
83298Seric 	}
84298Seric 	return (p);
85298Seric }
86298Seric /*
873151Seric **  COPYPLIST -- copy list of pointers.
883151Seric **
893151Seric **	This routine is the equivalent of newstr for lists of
903151Seric **	pointers.
913151Seric **
923151Seric **	Parameters:
933151Seric **		list -- list of pointers to copy.
943151Seric **			Must be NULL terminated.
953151Seric **		copycont -- if TRUE, copy the contents of the vector
963151Seric **			(which must be a string) also.
973151Seric **
983151Seric **	Returns:
993151Seric **		a copy of 'list'.
1003151Seric **
1013151Seric **	Side Effects:
1023151Seric **		none.
1033151Seric */
1043151Seric 
1053151Seric char **
1063151Seric copyplist(list, copycont)
1073151Seric 	char **list;
1083151Seric 	bool copycont;
1093151Seric {
1103151Seric 	register char **vp;
1113151Seric 	register char **newvp;
1123151Seric 
1133151Seric 	for (vp = list; *vp != NULL; vp++)
1143151Seric 		continue;
1153151Seric 
1163151Seric 	vp++;
1173151Seric 
11816897Seric 	newvp = (char **) xalloc((int) (vp - list) * sizeof *vp);
11916897Seric 	bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp);
1203151Seric 
1213151Seric 	if (copycont)
1223151Seric 	{
1233151Seric 		for (vp = newvp; *vp != NULL; vp++)
1243151Seric 			*vp = newstr(*vp);
1253151Seric 	}
1263151Seric 
1273151Seric 	return (newvp);
1283151Seric }
1293151Seric /*
13058170Seric **  COPYQUEUE -- copy address queue.
13158170Seric **
13258170Seric **	This routine is the equivalent of newstr for address queues
13358170Seric **	addresses marked with QDONTSEND aren't copied
13458170Seric **
13558170Seric **	Parameters:
13658170Seric **		addr -- list of address structures to copy.
13758170Seric **
13858170Seric **	Returns:
13958170Seric **		a copy of 'addr'.
14058170Seric **
14158170Seric **	Side Effects:
14258170Seric **		none.
14358170Seric */
14458170Seric 
14558170Seric ADDRESS *
14658170Seric copyqueue(addr)
14758170Seric 	ADDRESS *addr;
14858170Seric {
14958170Seric 	register ADDRESS *newaddr;
15058170Seric 	ADDRESS *ret;
15158170Seric 	register ADDRESS **tail = &ret;
15258170Seric 
15358170Seric 	while (addr != NULL)
15458170Seric 	{
15558170Seric 		if (!bitset(QDONTSEND, addr->q_flags))
15658170Seric 		{
15758170Seric 			newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS));
15858170Seric 			STRUCTCOPY(*addr, *newaddr);
15958170Seric 			*tail = newaddr;
16058170Seric 			tail = &newaddr->q_next;
16158170Seric 		}
16258170Seric 		addr = addr->q_next;
16358170Seric 	}
16458170Seric 	*tail = NULL;
16558170Seric 
16658170Seric 	return ret;
16758170Seric }
16858170Seric /*
1693151Seric **  PRINTAV -- print argument vector.
1703151Seric **
1713151Seric **	Parameters:
1723151Seric **		av -- argument vector.
1733151Seric **
1743151Seric **	Returns:
1753151Seric **		none.
1763151Seric **
1773151Seric **	Side Effects:
1783151Seric **		prints av.
1793151Seric */
1803151Seric 
1813151Seric printav(av)
1823151Seric 	register char **av;
1833151Seric {
1843151Seric 	while (*av != NULL)
1853151Seric 	{
1868063Seric 		if (tTd(0, 44))
1878063Seric 			printf("\n\t%08x=", *av);
1888063Seric 		else
18923105Seric 			(void) putchar(' ');
1903151Seric 		xputs(*av++);
1913151Seric 	}
19223105Seric 	(void) putchar('\n');
1933151Seric }
1943151Seric /*
1953151Seric **  LOWER -- turn letter into lower case.
1963151Seric **
1973151Seric **	Parameters:
1983151Seric **		c -- character to turn into lower case.
1993151Seric **
2003151Seric **	Returns:
2013151Seric **		c, in lower case.
2023151Seric **
2033151Seric **	Side Effects:
2043151Seric **		none.
2053151Seric */
2063151Seric 
2073151Seric char
2083151Seric lower(c)
2093151Seric 	register char c;
2103151Seric {
21158050Seric 	return((isascii(c) && isupper(c)) ? tolower(c) : c);
2123151Seric }
2133151Seric /*
2143151Seric **  XPUTS -- put string doing control escapes.
2153151Seric **
2163151Seric **	Parameters:
2173151Seric **		s -- string to put.
2183151Seric **
2193151Seric **	Returns:
2203151Seric **		none.
2213151Seric **
2223151Seric **	Side Effects:
2233151Seric **		output to stdout
2243151Seric */
2253151Seric 
2263151Seric xputs(s)
2273151Seric 	register char *s;
2283151Seric {
22958050Seric 	register int c;
23051781Seric 	register struct metamac *mp;
23151781Seric 	extern struct metamac MetaMacros[];
2323151Seric 
2338055Seric 	if (s == NULL)
2348055Seric 	{
2358055Seric 		printf("<null>");
2368055Seric 		return;
2378055Seric 	}
23858050Seric 	while ((c = (*s++ & 0377)) != '\0')
2393151Seric 	{
2403151Seric 		if (!isascii(c))
2413151Seric 		{
24258050Seric 			if (c == MATCHREPL || c == MACROEXPAND)
24358050Seric 			{
24458050Seric 				putchar('$');
24558050Seric 				continue;
24658050Seric 			}
24758050Seric 			for (mp = MetaMacros; mp->metaname != '\0'; mp++)
24858050Seric 			{
24958050Seric 				if ((mp->metaval & 0377) == c)
25058050Seric 				{
25158050Seric 					printf("$%c", mp->metaname);
25258050Seric 					break;
25358050Seric 				}
25458050Seric 			}
25558050Seric 			if (mp->metaname != '\0')
25658050Seric 				continue;
25723105Seric 			(void) putchar('\\');
2583151Seric 			c &= 0177;
2593151Seric 		}
26057589Seric 		if (isprint(c))
2613151Seric 		{
26257589Seric 			putchar(c);
26357589Seric 			continue;
26457589Seric 		}
26552050Seric 
26657589Seric 		/* wasn't a meta-macro -- find another way to print it */
26757589Seric 		switch (c)
26857589Seric 		{
26957589Seric 		  case '\0':
27057589Seric 			continue;
27152050Seric 
27257589Seric 		  case '\n':
27357589Seric 			c = 'n';
27457589Seric 			break;
27552050Seric 
27657589Seric 		  case '\r':
27757589Seric 			c = 'r';
27857589Seric 			break;
27952637Seric 
28057589Seric 		  case '\t':
28157589Seric 			c = 't';
28257589Seric 			break;
28357589Seric 
28457589Seric 		  default:
28557589Seric 			(void) putchar('^');
28657589Seric 			(void) putchar(c ^ 0100);
28757589Seric 			continue;
2883151Seric 		}
2893151Seric 	}
2904086Seric 	(void) fflush(stdout);
2913151Seric }
2923151Seric /*
2933151Seric **  MAKELOWER -- Translate a line into lower case
2943151Seric **
2953151Seric **	Parameters:
2963151Seric **		p -- the string to translate.  If NULL, return is
2973151Seric **			immediate.
2983151Seric **
2993151Seric **	Returns:
3003151Seric **		none.
3013151Seric **
3023151Seric **	Side Effects:
3033151Seric **		String pointed to by p is translated to lower case.
3043151Seric **
3053151Seric **	Called By:
3063151Seric **		parse
3073151Seric */
3083151Seric 
3093151Seric makelower(p)
3103151Seric 	register char *p;
3113151Seric {
3123151Seric 	register char c;
3133151Seric 
3143151Seric 	if (p == NULL)
3153151Seric 		return;
3163151Seric 	for (; (c = *p) != '\0'; p++)
3173151Seric 		if (isascii(c) && isupper(c))
31833724Sbostic 			*p = tolower(c);
3193151Seric }
3204059Seric /*
3215196Seric **  BUILDFNAME -- build full name from gecos style entry.
3224375Seric **
3235196Seric **	This routine interprets the strange entry that would appear
3245196Seric **	in the GECOS field of the password file.
3255196Seric **
3264375Seric **	Parameters:
3275196Seric **		p -- name to build.
3285196Seric **		login -- the login name of this user (for &).
3295196Seric **		buf -- place to put the result.
3304375Seric **
3314375Seric **	Returns:
33265006Seric **		none.
3334375Seric **
3344375Seric **	Side Effects:
3354375Seric **		none.
3364375Seric */
3374375Seric 
33854984Seric buildfname(gecos, login, buf)
33965006Seric 	register char *gecos;
34065006Seric 	char *login;
34165006Seric 	char *buf;
3424375Seric {
34365006Seric 	register char *p;
34465006Seric 	register char *bp = buf;
34565006Seric 	int l;
3464375Seric 
34765006Seric 	if (*gecos == '*')
34865006Seric 		gecos++;
34965006Seric 
35065006Seric 	/* find length of final string */
35165006Seric 	l = 0;
35265006Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
35354984Seric 	{
35465006Seric 		if (*p == '&')
35565006Seric 			l += strlen(login);
35665006Seric 		else
35765006Seric 			l++;
35854984Seric 	}
35965006Seric 
36065006Seric 	/* now fill in buf */
36165006Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
3624375Seric 	{
36365006Seric 		if (*p == '&')
3644375Seric 		{
36565006Seric 			(void) strcpy(bp, login);
36665006Seric 			*bp = toupper(*bp);
36765006Seric 			while (*bp != '\0')
36865006Seric 				bp++;
3694375Seric 		}
37065006Seric 		else
37165006Seric 			*bp++ = *p;
3724375Seric 	}
3734375Seric 	*bp = '\0';
3744375Seric }
3754375Seric /*
3764538Seric **  SAFEFILE -- return true if a file exists and is safe for a user.
3774538Seric **
3784538Seric **	Parameters:
3794538Seric **		fn -- filename to check.
38064083Seric **		uid -- user id to compare against.
38164083Seric **		gid -- group id to compare against.
38264083Seric **		uname -- user name to compare against (used for group
38364083Seric **			sets).
38464944Seric **		flags -- modifiers:
38565064Seric **			SFF_MUSTOWN -- "uid" must own this file.
38665064Seric **			SFF_NOSLINK -- file cannot be a symbolic link.
3874538Seric **		mode -- mode bits that must match.
3884538Seric **
3894538Seric **	Returns:
39058247Seric **		0 if fn exists, is owned by uid, and matches mode.
39158247Seric **		An errno otherwise.  The actual errno is cleared.
3924538Seric **
3934538Seric **	Side Effects:
3944538Seric **		none.
3954538Seric */
3964538Seric 
39764083Seric #include <grp.h>
39864083Seric 
39963581Seric #ifndef S_IXOTH
40063581Seric # define S_IXOTH	(S_IEXEC >> 6)
40163581Seric #endif
40263581Seric 
40364083Seric #ifndef S_IXGRP
40464083Seric # define S_IXGRP	(S_IEXEC >> 3)
40564083Seric #endif
40664083Seric 
40763753Seric #ifndef S_IXUSR
40863753Seric # define S_IXUSR	(S_IEXEC)
40963753Seric #endif
41063753Seric 
41158247Seric int
41264944Seric safefile(fn, uid, gid, uname, flags, mode)
4134538Seric 	char *fn;
41455372Seric 	uid_t uid;
41564083Seric 	gid_t gid;
41664083Seric 	char *uname;
41764944Seric 	int flags;
4184538Seric 	int mode;
4194538Seric {
42063581Seric 	register char *p;
42164083Seric 	register struct group *gr = NULL;
4224538Seric 	struct stat stbuf;
4234538Seric 
42463581Seric 	if (tTd(54, 4))
42564944Seric 		printf("safefile(%s, uid=%d, gid=%d, flags=%x, mode=%o):\n",
42664944Seric 			fn, uid, gid, flags, mode);
42763581Seric 	errno = 0;
42863581Seric 
42963581Seric 	for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/')
43063581Seric 	{
43163581Seric 		*p = '\0';
43264083Seric 		if (stat(fn, &stbuf) < 0)
43364083Seric 			break;
43465225Seric 		if (uid == 0 && !bitset(SFF_ROOTOK, flags))
43565225Seric 		{
43665225Seric 			if (bitset(S_IXOTH, stbuf.st_mode))
43765225Seric 				continue;
43865225Seric 			break;
43965225Seric 		}
44064362Seric 		if (stbuf.st_uid == uid && bitset(S_IXUSR, stbuf.st_mode))
44164362Seric 			continue;
44264083Seric 		if (stbuf.st_gid == gid && bitset(S_IXGRP, stbuf.st_mode))
44364083Seric 			continue;
44464084Seric #ifndef NO_GROUP_SET
44564083Seric 		if (uname != NULL &&
44664083Seric 		    ((gr != NULL && gr->gr_gid == stbuf.st_gid) ||
44764083Seric 		     (gr = getgrgid(stbuf.st_gid)) != NULL))
44863581Seric 		{
44964083Seric 			register char **gp;
45063581Seric 
45164083Seric 			for (gp = gr->gr_mem; *gp != NULL; gp++)
45264083Seric 				if (strcmp(*gp, uname) == 0)
45364083Seric 					break;
45464362Seric 			if (*gp != NULL && bitset(S_IXGRP, stbuf.st_mode))
45564362Seric 				continue;
45663581Seric 		}
45764084Seric #endif
45864083Seric 		if (!bitset(S_IXOTH, stbuf.st_mode))
45964083Seric 			break;
46063581Seric 	}
46164083Seric 	if (p != NULL)
46264083Seric 	{
46364083Seric 		int ret = errno;
46463581Seric 
46564083Seric 		if (ret == 0)
46664083Seric 			ret = EACCES;
46764083Seric 		if (tTd(54, 4))
46864083Seric 			printf("\t[dir %s] %s\n", fn, errstring(ret));
46964083Seric 		*p = '/';
47064083Seric 		return ret;
47164083Seric 	}
47264083Seric 
47364944Seric #ifdef HASLSTAT
47465064Seric 	if ((bitset(SFF_NOSLINK, flags) ? lstat(fn, &stbuf)
47565064Seric 					: stat(fn, &stbuf)) < 0)
47664944Seric #else
47758247Seric 	if (stat(fn, &stbuf) < 0)
47864944Seric #endif
47958247Seric 	{
48058247Seric 		int ret = errno;
48158247Seric 
48263581Seric 		if (tTd(54, 4))
48364083Seric 			printf("\t%s\n", errstring(ret));
48463581Seric 
48558247Seric 		errno = 0;
48658247Seric 		return ret;
48758247Seric 	}
48864944Seric 
48964944Seric #ifdef S_ISLNK
49065064Seric 	if (bitset(SFF_NOSLINK, flags) && S_ISLNK(stbuf.st_mode))
49164944Seric 	{
49264944Seric 		if (tTd(54, 4))
49365123Seric 			printf("\t[slink mode %o]\tEPERM\n", stbuf.st_mode);
49464944Seric 		return EPERM;
49564944Seric 	}
49664944Seric #endif
49764944Seric 
49865139Seric 	if (uid == 0 && !bitset(SFF_ROOTOK, flags))
49963581Seric 		mode >>= 6;
50064084Seric 	else if (stbuf.st_uid != uid)
50164084Seric 	{
50264084Seric 		mode >>= 3;
50364084Seric 		if (stbuf.st_gid == gid)
50464084Seric 			;
50564084Seric #ifndef NO_GROUP_SET
50664084Seric 		else if (uname != NULL &&
50764084Seric 			 ((gr != NULL && gr->gr_gid == stbuf.st_gid) ||
50864084Seric 			  (gr = getgrgid(stbuf.st_gid)) != NULL))
50964084Seric 		{
51064084Seric 			register char **gp;
51164084Seric 
51264084Seric 			for (gp = gr->gr_mem; *gp != NULL; gp++)
51364084Seric 				if (strcmp(*gp, uname) == 0)
51464084Seric 					break;
51564084Seric 			if (*gp == NULL)
51664084Seric 				mode >>= 3;
51764084Seric 		}
51864084Seric #endif
51964084Seric 		else
52064084Seric 			mode >>= 3;
52164084Seric 	}
52263581Seric 	if (tTd(54, 4))
52364084Seric 		printf("\t[uid %d, stat %o, mode %o] ",
52464084Seric 			stbuf.st_uid, stbuf.st_mode, mode);
52564944Seric 	if ((stbuf.st_uid == uid || stbuf.st_uid == 0 ||
52665064Seric 	     !bitset(SFF_MUSTOWN, flags)) &&
52763581Seric 	    (stbuf.st_mode & mode) == mode)
52863581Seric 	{
52963581Seric 		if (tTd(54, 4))
53064083Seric 			printf("\tOK\n");
53158247Seric 		return 0;
53263581Seric 	}
53363581Seric 	if (tTd(54, 4))
53464083Seric 		printf("\tEACCES\n");
53563581Seric 	return EACCES;
5364538Seric }
5374538Seric /*
5384557Seric **  FIXCRLF -- fix <CR><LF> in line.
5394557Seric **
5404557Seric **	Looks for the <CR><LF> combination and turns it into the
5414557Seric **	UNIX canonical <NL> character.  It only takes one line,
5424557Seric **	i.e., it is assumed that the first <NL> found is the end
5434557Seric **	of the line.
5444557Seric **
5454557Seric **	Parameters:
5464557Seric **		line -- the line to fix.
5474557Seric **		stripnl -- if true, strip the newline also.
5484557Seric **
5494557Seric **	Returns:
5504557Seric **		none.
5514557Seric **
5524557Seric **	Side Effects:
5534557Seric **		line is changed in place.
5544557Seric */
5554557Seric 
5564557Seric fixcrlf(line, stripnl)
5574557Seric 	char *line;
5584557Seric 	bool stripnl;
5594557Seric {
5604557Seric 	register char *p;
5614557Seric 
56256795Seric 	p = strchr(line, '\n');
5634557Seric 	if (p == NULL)
5644557Seric 		return;
56536291Sbostic 	if (p > line && p[-1] == '\r')
5664557Seric 		p--;
5674557Seric 	if (!stripnl)
5684557Seric 		*p++ = '\n';
5694557Seric 	*p = '\0';
5704557Seric }
5714557Seric /*
5726890Seric **  DFOPEN -- determined file open
5736890Seric **
5746890Seric **	This routine has the semantics of fopen, except that it will
5756890Seric **	keep trying a few times to make this happen.  The idea is that
5766890Seric **	on very loaded systems, we may run out of resources (inodes,
5776890Seric **	whatever), so this tries to get around it.
5786890Seric */
5796890Seric 
58063753Seric #ifndef O_ACCMODE
58163753Seric # define O_ACCMODE	(O_RDONLY|O_WRONLY|O_RDWR)
58263753Seric #endif
58363753Seric 
58459745Seric struct omodes
58559745Seric {
58659745Seric 	int	mask;
58759745Seric 	int	mode;
58859745Seric 	char	*farg;
58959745Seric } OpenModes[] =
59059745Seric {
59159745Seric 	O_ACCMODE,		O_RDONLY,		"r",
59259745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY,		"w",
59359745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY|O_APPEND,	"a",
59459745Seric 	O_TRUNC,		0,			"w+",
59559745Seric 	O_APPEND,		O_APPEND,		"a+",
59659745Seric 	0,			0,			"r+",
59759745Seric };
59859745Seric 
5996890Seric FILE *
60059745Seric dfopen(filename, omode, cmode)
6016890Seric 	char *filename;
60259745Seric 	int omode;
60359745Seric 	int cmode;
6046890Seric {
6056890Seric 	register int tries;
60659745Seric 	int fd;
60759745Seric 	register struct omodes *om;
60859431Seric 	struct stat st;
6096890Seric 
61059745Seric 	for (om = OpenModes; om->mask != 0; om++)
61159745Seric 		if ((omode & om->mask) == om->mode)
61259745Seric 			break;
61359745Seric 
6146890Seric 	for (tries = 0; tries < 10; tries++)
6156890Seric 	{
61625618Seric 		sleep((unsigned) (10 * tries));
6176890Seric 		errno = 0;
61859745Seric 		fd = open(filename, omode, cmode);
61959745Seric 		if (fd >= 0)
6206890Seric 			break;
621*66017Seric 		switch (errno)
622*66017Seric 		{
623*66017Seric 		  case ENFILE:		/* system file table full */
624*66017Seric 		  case EINTR:		/* interrupted syscall */
625*66017Seric #ifdef ETXTBSY
626*66017Seric 		  case ETXTBSY:		/* Apollo: net file locked */
627*66017Seric #endif
6289376Seric 			break;
629*66017Seric 
630*66017Seric 		  default:
631*66017Seric 			continue;
632*66017Seric 		}
633*66017Seric 		break;
6346890Seric 	}
63559745Seric 	if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode))
63656328Seric 	{
63756328Seric 		int locktype;
63856328Seric 
63956328Seric 		/* lock the file to avoid accidental conflicts */
64059745Seric 		if ((omode & O_ACCMODE) != O_RDONLY)
64156328Seric 			locktype = LOCK_EX;
64256328Seric 		else
64356328Seric 			locktype = LOCK_SH;
64464335Seric 		(void) lockfile(fd, filename, NULL, locktype);
64556328Seric 		errno = 0;
64656328Seric 	}
64763787Seric 	if (fd < 0)
64863787Seric 		return NULL;
64963787Seric 	else
65063787Seric 		return fdopen(fd, om->farg);
6516890Seric }
6527124Seric /*
6537124Seric **  PUTLINE -- put a line like fputs obeying SMTP conventions
6547124Seric **
6557753Seric **	This routine always guarantees outputing a newline (or CRLF,
6567753Seric **	as appropriate) at the end of the string.
6577753Seric **
6587124Seric **	Parameters:
6597124Seric **		l -- line to put.
66065870Seric **		mci -- the mailer connection information.
6617124Seric **
6627124Seric **	Returns:
6637124Seric **		none
6647124Seric **
6657124Seric **	Side Effects:
6667124Seric **		output of l to fp.
6677124Seric */
6687124Seric 
66965870Seric putline(l, mci)
6707753Seric 	register char *l;
67165870Seric 	register MCI *mci;
6727124Seric {
6737124Seric 	register char *p;
67447157Sbostic 	register char svchar;
67566004Seric 	int slop = 0;
6767124Seric 
67711275Seric 	/* strip out 0200 bits -- these can look like TELNET protocol */
67865870Seric 	if (bitset(MCIF_7BIT, mci->mci_flags))
67911275Seric 	{
68061707Seric 		for (p = l; (svchar = *p) != '\0'; ++p)
68161707Seric 			if (bitset(0200, svchar))
68247157Sbostic 				*p = svchar &~ 0200;
68311275Seric 	}
68411275Seric 
6857753Seric 	do
6867124Seric 	{
6877753Seric 		/* find the end of the line */
68856795Seric 		p = strchr(l, '\n');
6897753Seric 		if (p == NULL)
6907753Seric 			p = &l[strlen(l)];
6917124Seric 
69263753Seric 		if (TrafficLogFile != NULL)
69363753Seric 			fprintf(TrafficLogFile, "%05d >>> ", getpid());
69463753Seric 
6957753Seric 		/* check for line overflow */
69665870Seric 		while (mci->mci_mailer->m_linelimit > 0 &&
69766004Seric 		       (p - l + slop) > mci->mci_mailer->m_linelimit)
6987753Seric 		{
69966004Seric 			register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1];
7007124Seric 
7017753Seric 			svchar = *q;
7027753Seric 			*q = '\0';
70366004Seric 			if (l[0] == '.' && slop == 0 &&
70466004Seric 			    bitnset(M_XDOT, mci->mci_mailer->m_flags))
70563753Seric 			{
70665870Seric 				(void) putc('.', mci->mci_out);
70763753Seric 				if (TrafficLogFile != NULL)
70863753Seric 					(void) putc('.', TrafficLogFile);
70963753Seric 			}
71065870Seric 			fputs(l, mci->mci_out);
71165870Seric 			(void) putc('!', mci->mci_out);
71265870Seric 			fputs(mci->mci_mailer->m_eol, mci->mci_out);
71366004Seric 			(void) putc(' ', mci->mci_out);
71463753Seric 			if (TrafficLogFile != NULL)
71566004Seric 				fprintf(TrafficLogFile, "%s!\n%05d >>>  ",
71663753Seric 					l, getpid());
7177753Seric 			*q = svchar;
7187753Seric 			l = q;
71966004Seric 			slop = 1;
7207753Seric 		}
7217124Seric 
7227753Seric 		/* output last part */
72366004Seric 		if (l[0] == '.' && slop == 0 &&
72466004Seric 		    bitnset(M_XDOT, mci->mci_mailer->m_flags))
72563753Seric 		{
72665870Seric 			(void) putc('.', mci->mci_out);
72763753Seric 			if (TrafficLogFile != NULL)
72863753Seric 				(void) putc('.', TrafficLogFile);
72963753Seric 		}
73063753Seric 		if (TrafficLogFile != NULL)
73163753Seric 			fprintf(TrafficLogFile, "%.*s\n", p - l, l);
73247157Sbostic 		for ( ; l < p; ++l)
73365870Seric 			(void) putc(*l, mci->mci_out);
73465870Seric 		fputs(mci->mci_mailer->m_eol, mci->mci_out);
7357753Seric 		if (*l == '\n')
73647157Sbostic 			++l;
7377753Seric 	} while (l[0] != '\0');
7387124Seric }
7397676Seric /*
7407676Seric **  XUNLINK -- unlink a file, doing logging as appropriate.
7417676Seric **
7427676Seric **	Parameters:
7437676Seric **		f -- name of file to unlink.
7447676Seric **
7457676Seric **	Returns:
7467676Seric **		none.
7477676Seric **
7487676Seric **	Side Effects:
7497676Seric **		f is unlinked.
7507676Seric */
7517676Seric 
7527676Seric xunlink(f)
7537676Seric 	char *f;
7547676Seric {
7557676Seric 	register int i;
7567676Seric 
7577676Seric # ifdef LOG
75858020Seric 	if (LogLevel > 98)
75958020Seric 		syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f);
76056795Seric # endif /* LOG */
7617676Seric 
7627676Seric 	i = unlink(f);
7637676Seric # ifdef LOG
76458020Seric 	if (i < 0 && LogLevel > 97)
7657942Seric 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
76656795Seric # endif /* LOG */
7677676Seric }
7687685Seric /*
76958680Seric **  XFCLOSE -- close a file, doing logging as appropriate.
77058680Seric **
77158680Seric **	Parameters:
77258680Seric **		fp -- file pointer for the file to close
77358680Seric **		a, b -- miscellaneous crud to print for debugging
77458680Seric **
77558680Seric **	Returns:
77658680Seric **		none.
77758680Seric **
77858680Seric **	Side Effects:
77958680Seric **		fp is closed.
78058680Seric */
78158680Seric 
78258680Seric xfclose(fp, a, b)
78358680Seric 	FILE *fp;
78458680Seric 	char *a, *b;
78558680Seric {
78658796Seric 	if (tTd(53, 99))
78758680Seric 		printf("xfclose(%x) %s %s\n", fp, a, b);
78864401Seric #ifdef XDEBUG
78964401Seric 	if (fileno(fp) == 1)
79064401Seric 		syserr("xfclose(%s %s): fd = 1", a, b);
79164401Seric #endif
79258796Seric 	if (fclose(fp) < 0 && tTd(53, 99))
79358680Seric 		printf("xfclose FAILURE: %s\n", errstring(errno));
79458680Seric }
79558680Seric /*
79614885Seric **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
7977685Seric **
7987685Seric **	Parameters:
7997685Seric **		buf -- place to put the input line.
8007685Seric **		siz -- size of buf.
8017685Seric **		fp -- file to read from.
80257384Seric **		timeout -- the timeout before error occurs.
80361093Seric **		during -- what we are trying to read (for error messages).
8047685Seric **
8057685Seric **	Returns:
80615533Seric **		NULL on error (including timeout).  This will also leave
80715533Seric **			buf containing a null string.
8087685Seric **		buf otherwise.
8097685Seric **
8107685Seric **	Side Effects:
8117685Seric **		none.
8127685Seric */
8137685Seric 
81414885Seric static jmp_buf	CtxReadTimeout;
81563937Seric static int	readtimeout();
8167685Seric 
8177685Seric char *
81861093Seric sfgets(buf, siz, fp, timeout, during)
8197685Seric 	char *buf;
8207685Seric 	int siz;
8217685Seric 	FILE *fp;
82257384Seric 	time_t timeout;
82361093Seric 	char *during;
8247685Seric {
8257942Seric 	register EVENT *ev = NULL;
8267685Seric 	register char *p;
8277685Seric 
82814885Seric 	/* set the timeout */
82957384Seric 	if (timeout != 0)
83014885Seric 	{
83114885Seric 		if (setjmp(CtxReadTimeout) != 0)
83214885Seric 		{
83336233Skarels # ifdef LOG
83436230Skarels 			syslog(LOG_NOTICE,
83561093Seric 			    "timeout waiting for input from %s during %s\n",
83661093Seric 			    CurHostName? CurHostName: "local", during);
83736233Skarels # endif
83836230Skarels 			errno = 0;
83961093Seric 			usrerr("451 timeout waiting for input during %s",
84061093Seric 				during);
84119037Seric 			buf[0] = '\0';
84263753Seric #ifdef XDEBUG
84363753Seric 			checkfd012(during);
84463753Seric #endif
84514885Seric 			return (NULL);
84614885Seric 		}
84757384Seric 		ev = setevent(timeout, readtimeout, 0);
84814885Seric 	}
84914885Seric 
85014885Seric 	/* try to read */
85115533Seric 	p = NULL;
85265190Seric 	while (!feof(fp) && !ferror(fp))
8537942Seric 	{
8547942Seric 		errno = 0;
8557942Seric 		p = fgets(buf, siz, fp);
85665190Seric 		if (p != NULL || errno != EINTR)
85765186Seric 			break;
85865190Seric 		clearerr(fp);
85915533Seric 	}
86014885Seric 
86114885Seric 	/* clear the event if it has not sprung */
8627685Seric 	clrevent(ev);
86314885Seric 
86414885Seric 	/* clean up the books and exit */
8658055Seric 	LineNumber++;
86615533Seric 	if (p == NULL)
86716880Seric 	{
86815533Seric 		buf[0] = '\0';
86963753Seric 		if (TrafficLogFile != NULL)
87063753Seric 			fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid());
87116880Seric 		return (NULL);
87216880Seric 	}
87363753Seric 	if (TrafficLogFile != NULL)
87463753Seric 		fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf);
87559709Seric 	if (SevenBit)
87652106Seric 		for (p = buf; *p != '\0'; p++)
87752106Seric 			*p &= ~0200;
87816880Seric 	return (buf);
8797685Seric }
8807685Seric 
8817685Seric static
8827685Seric readtimeout()
8837685Seric {
88414885Seric 	longjmp(CtxReadTimeout, 1);
8857685Seric }
8867786Seric /*
8877786Seric **  FGETFOLDED -- like fgets, but know about folded lines.
8887786Seric **
8897786Seric **	Parameters:
8907786Seric **		buf -- place to put result.
8917786Seric **		n -- bytes available.
8927786Seric **		f -- file to read from.
8937786Seric **
8947786Seric **	Returns:
89557135Seric **		input line(s) on success, NULL on error or EOF.
89657135Seric **		This will normally be buf -- unless the line is too
89757135Seric **			long, when it will be xalloc()ed.
8987786Seric **
8997786Seric **	Side Effects:
9007786Seric **		buf gets lines from f, with continuation lines (lines
9017786Seric **		with leading white space) appended.  CRLF's are mapped
9027786Seric **		into single newlines.  Any trailing NL is stripped.
9037786Seric */
9047786Seric 
9057786Seric char *
9067786Seric fgetfolded(buf, n, f)
9077786Seric 	char *buf;
9087786Seric 	register int n;
9097786Seric 	FILE *f;
9107786Seric {
9117786Seric 	register char *p = buf;
91257135Seric 	char *bp = buf;
9137786Seric 	register int i;
9147786Seric 
9157786Seric 	n--;
91617350Seric 	while ((i = getc(f)) != EOF)
9177786Seric 	{
91817350Seric 		if (i == '\r')
91917350Seric 		{
92017350Seric 			i = getc(f);
92117350Seric 			if (i != '\n')
92217350Seric 			{
92317350Seric 				if (i != EOF)
92423105Seric 					(void) ungetc(i, f);
92517350Seric 				i = '\r';
92617350Seric 			}
92717350Seric 		}
92857135Seric 		if (--n <= 0)
92957135Seric 		{
93057135Seric 			/* allocate new space */
93157135Seric 			char *nbp;
93257135Seric 			int nn;
93357135Seric 
93457135Seric 			nn = (p - bp);
93557232Seric 			if (nn < MEMCHUNKSIZE)
93657135Seric 				nn *= 2;
93757135Seric 			else
93857232Seric 				nn += MEMCHUNKSIZE;
93957135Seric 			nbp = xalloc(nn);
94057135Seric 			bcopy(bp, nbp, p - bp);
94157135Seric 			p = &nbp[p - bp];
94257135Seric 			if (bp != buf)
94357135Seric 				free(bp);
94457135Seric 			bp = nbp;
94557135Seric 			n = nn - (p - bp);
94657135Seric 		}
94757135Seric 		*p++ = i;
94817350Seric 		if (i == '\n')
94917350Seric 		{
95017350Seric 			LineNumber++;
95117350Seric 			i = getc(f);
95217350Seric 			if (i != EOF)
95323105Seric 				(void) ungetc(i, f);
95417350Seric 			if (i != ' ' && i != '\t')
95552647Seric 				break;
95617350Seric 		}
9577786Seric 	}
95857135Seric 	if (p == bp)
95952647Seric 		return (NULL);
96052647Seric 	*--p = '\0';
96157135Seric 	return (bp);
9627786Seric }
9637860Seric /*
9647886Seric **  CURTIME -- return current time.
9657886Seric **
9667886Seric **	Parameters:
9677886Seric **		none.
9687886Seric **
9697886Seric **	Returns:
9707886Seric **		the current time.
9717886Seric **
9727886Seric **	Side Effects:
9737886Seric **		none.
9747886Seric */
9757886Seric 
9767886Seric time_t
9777886Seric curtime()
9787886Seric {
9797886Seric 	auto time_t t;
9807886Seric 
9817886Seric 	(void) time(&t);
9827886Seric 	return (t);
9837886Seric }
9848264Seric /*
9858264Seric **  ATOBOOL -- convert a string representation to boolean.
9868264Seric **
9878264Seric **	Defaults to "TRUE"
9888264Seric **
9898264Seric **	Parameters:
9908264Seric **		s -- string to convert.  Takes "tTyY" as true,
9918264Seric **			others as false.
9928264Seric **
9938264Seric **	Returns:
9948264Seric **		A boolean representation of the string.
9958264Seric **
9968264Seric **	Side Effects:
9978264Seric **		none.
9988264Seric */
9998264Seric 
10008264Seric bool
10018264Seric atobool(s)
10028264Seric 	register char *s;
10038264Seric {
100463833Seric 	if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL)
10058264Seric 		return (TRUE);
10068264Seric 	return (FALSE);
10078264Seric }
10089048Seric /*
10099048Seric **  ATOOCT -- convert a string representation to octal.
10109048Seric **
10119048Seric **	Parameters:
10129048Seric **		s -- string to convert.
10139048Seric **
10149048Seric **	Returns:
10159048Seric **		An integer representing the string interpreted as an
10169048Seric **		octal number.
10179048Seric **
10189048Seric **	Side Effects:
10199048Seric **		none.
10209048Seric */
10219048Seric 
10229048Seric atooct(s)
10239048Seric 	register char *s;
10249048Seric {
10259048Seric 	register int i = 0;
10269048Seric 
10279048Seric 	while (*s >= '0' && *s <= '7')
10289048Seric 		i = (i << 3) | (*s++ - '0');
10299048Seric 	return (i);
10309048Seric }
10319376Seric /*
10329376Seric **  WAITFOR -- wait for a particular process id.
10339376Seric **
10349376Seric **	Parameters:
10359376Seric **		pid -- process id to wait for.
10369376Seric **
10379376Seric **	Returns:
10389376Seric **		status of pid.
10399376Seric **		-1 if pid never shows up.
10409376Seric **
10419376Seric **	Side Effects:
10429376Seric **		none.
10439376Seric */
10449376Seric 
104564562Seric int
10469376Seric waitfor(pid)
10479376Seric 	int pid;
10489376Seric {
104964562Seric #ifdef WAITUNION
105064562Seric 	union wait st;
105164562Seric #else
10529376Seric 	auto int st;
105364562Seric #endif
10549376Seric 	int i;
10559376Seric 
10569376Seric 	do
10579376Seric 	{
10589376Seric 		errno = 0;
10599376Seric 		i = wait(&st);
10609376Seric 	} while ((i >= 0 || errno == EINTR) && i != pid);
10619376Seric 	if (i < 0)
106264562Seric 		return -1;
106364562Seric #ifdef WAITUNION
106464562Seric 	return st.w_status;
106564562Seric #else
106664562Seric 	return st;
106764562Seric #endif
10689376Seric }
10699376Seric /*
107010685Seric **  BITINTERSECT -- tell if two bitmaps intersect
107110685Seric **
107210685Seric **	Parameters:
107310685Seric **		a, b -- the bitmaps in question
107410685Seric **
107510685Seric **	Returns:
107610685Seric **		TRUE if they have a non-null intersection
107710685Seric **		FALSE otherwise
107810685Seric **
107910685Seric **	Side Effects:
108010685Seric **		none.
108110685Seric */
108210685Seric 
108310685Seric bool
108410685Seric bitintersect(a, b)
108510685Seric 	BITMAP a;
108610685Seric 	BITMAP b;
108710685Seric {
108810685Seric 	int i;
108910685Seric 
109010685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
109110685Seric 		if ((a[i] & b[i]) != 0)
109210685Seric 			return (TRUE);
109310685Seric 	return (FALSE);
109410685Seric }
109510685Seric /*
109610685Seric **  BITZEROP -- tell if a bitmap is all zero
109710685Seric **
109810685Seric **	Parameters:
109910685Seric **		map -- the bit map to check
110010685Seric **
110110685Seric **	Returns:
110210685Seric **		TRUE if map is all zero.
110310685Seric **		FALSE if there are any bits set in map.
110410685Seric **
110510685Seric **	Side Effects:
110610685Seric **		none.
110710685Seric */
110810685Seric 
110910685Seric bool
111010685Seric bitzerop(map)
111110685Seric 	BITMAP map;
111210685Seric {
111310685Seric 	int i;
111410685Seric 
111510685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
111610685Seric 		if (map[i] != 0)
111710685Seric 			return (FALSE);
111810685Seric 	return (TRUE);
111910685Seric }
112058247Seric /*
112158318Seric **  STRCONTAINEDIN -- tell if one string is contained in another
112258318Seric **
112358318Seric **	Parameters:
112458318Seric **		a -- possible substring.
112558318Seric **		b -- possible superstring.
112658318Seric **
112758318Seric **	Returns:
112858318Seric **		TRUE if a is contained in b.
112958318Seric **		FALSE otherwise.
113058318Seric */
113158318Seric 
113258318Seric bool
113358318Seric strcontainedin(a, b)
113458318Seric 	register char *a;
113558318Seric 	register char *b;
113658318Seric {
113765012Seric 	int la;
113865012Seric 	int lb;
113965012Seric 	int c;
114058318Seric 
114165012Seric 	la = strlen(a);
114265012Seric 	lb = strlen(b);
114365012Seric 	c = *a;
114465012Seric 	if (isascii(c) && isupper(c))
114565012Seric 		c = tolower(c);
114665012Seric 	for (; lb-- >= la; b++)
114758318Seric 	{
114865012Seric 		if (*b != c && isascii(*b) && isupper(*b) && tolower(*b) != c)
114965012Seric 			continue;
115065012Seric 		if (strncasecmp(a, b, la) == 0)
115158318Seric 			return TRUE;
115258318Seric 	}
115365012Seric 	return FALSE;
115458318Seric }
115563753Seric /*
115663753Seric **  CHECKFD012 -- check low numbered file descriptors
115763753Seric **
115863753Seric **	File descriptors 0, 1, and 2 should be open at all times.
115963753Seric **	This routine verifies that, and fixes it if not true.
116063753Seric **
116163753Seric **	Parameters:
116263753Seric **		where -- a tag printed if the assertion failed
116363753Seric **
116463753Seric **	Returns:
116563753Seric **		none
116663753Seric */
116763753Seric 
116863753Seric checkfd012(where)
116963753Seric 	char *where;
117063753Seric {
117163753Seric #ifdef XDEBUG
117263753Seric 	register int i;
117363753Seric 	struct stat stbuf;
117463753Seric 
117563753Seric 	for (i = 0; i < 3; i++)
117663753Seric 	{
117764735Seric 		if (fstat(i, &stbuf) < 0 && errno != EOPNOTSUPP)
117863753Seric 		{
117963753Seric 			/* oops.... */
118063753Seric 			int fd;
118163753Seric 
118263753Seric 			syserr("%s: fd %d not open", where, i);
118363753Seric 			fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666);
118463753Seric 			if (fd != i)
118563753Seric 			{
118663753Seric 				(void) dup2(fd, i);
118763753Seric 				(void) close(fd);
118863753Seric 			}
118963753Seric 		}
119063753Seric 	}
119163937Seric #endif /* XDEBUG */
119263753Seric }
119364725Seric /*
119464725Seric **  PRINTOPENFDS -- print the open file descriptors (for debugging)
119564725Seric **
119664725Seric **	Parameters:
119764725Seric **		logit -- if set, send output to syslog; otherwise
119864725Seric **			print for debugging.
119964725Seric **
120064725Seric **	Returns:
120164725Seric **		none.
120264725Seric */
120364725Seric 
120464725Seric #include <netdb.h>
120564725Seric #include <arpa/inet.h>
120664725Seric 
120764725Seric printopenfds(logit)
120864725Seric 	bool logit;
120964725Seric {
121064725Seric 	register int fd;
121164743Seric 	extern int DtableSize;
121264743Seric 
121364743Seric 	for (fd = 0; fd < DtableSize; fd++)
121464743Seric 		dumpfd(fd, FALSE, logit);
121564743Seric }
121664743Seric /*
121764743Seric **  DUMPFD -- dump a file descriptor
121864743Seric **
121964743Seric **	Parameters:
122064743Seric **		fd -- the file descriptor to dump.
122164743Seric **		printclosed -- if set, print a notification even if
122264743Seric **			it is closed; otherwise print nothing.
122364743Seric **		logit -- if set, send output to syslog instead of stdout.
122464743Seric */
122564743Seric 
122664743Seric dumpfd(fd, printclosed, logit)
122764743Seric 	int fd;
122864743Seric 	bool printclosed;
122964743Seric 	bool logit;
123064743Seric {
123164725Seric 	register struct hostent *hp;
123264725Seric 	register char *p;
123364743Seric 	struct sockaddr_in sin;
123464743Seric 	auto int slen;
123564743Seric 	struct stat st;
123664725Seric 	char buf[200];
123764725Seric 
123864743Seric 	p = buf;
123964743Seric 	sprintf(p, "%3d: ", fd);
124064743Seric 	p += strlen(p);
124164743Seric 
124264743Seric 	if (fstat(fd, &st) < 0)
124364725Seric 	{
124464743Seric 		if (printclosed || errno != EBADF)
124564743Seric 		{
124664743Seric 			sprintf(p, "CANNOT STAT (%s)", errstring(errno));
124764743Seric 			goto printit;
124864743Seric 		}
124964743Seric 		return;
125064743Seric 	}
125164725Seric 
125264743Seric 	slen = fcntl(fd, F_GETFL, NULL);
125364743Seric 	if (slen != -1)
125464743Seric 	{
125564743Seric 		sprintf(p, "fl=0x%x, ", slen);
125664743Seric 		p += strlen(p);
125764743Seric 	}
125864725Seric 
125964743Seric 	sprintf(p, "mode=%o: ", st.st_mode);
126064743Seric 	p += strlen(p);
126164743Seric 	switch (st.st_mode & S_IFMT)
126264743Seric 	{
126364807Seric #ifdef S_IFSOCK
126464743Seric 	  case S_IFSOCK:
126564743Seric 		sprintf(p, "SOCK ");
126664725Seric 		p += strlen(p);
126764743Seric 		slen = sizeof sin;
126864743Seric 		if (getsockname(fd, (struct sockaddr *) &sin, &slen) < 0)
126964743Seric 			sprintf(p, "(badsock)");
127064743Seric 		else
127164725Seric 		{
127264743Seric 			hp = gethostbyaddr((char *) &sin.sin_addr, slen, AF_INET);
127364743Seric 			sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr)
127464743Seric 						   : hp->h_name, ntohs(sin.sin_port));
127564743Seric 		}
127664743Seric 		p += strlen(p);
127764743Seric 		sprintf(p, "->");
127864743Seric 		p += strlen(p);
127964743Seric 		slen = sizeof sin;
128064743Seric 		if (getpeername(fd, (struct sockaddr *) &sin, &slen) < 0)
128164743Seric 			sprintf(p, "(badsock)");
128264743Seric 		else
128364743Seric 		{
128464743Seric 			hp = gethostbyaddr((char *) &sin.sin_addr, slen, AF_INET);
128564743Seric 			sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr)
128664743Seric 						   : hp->h_name, ntohs(sin.sin_port));
128764743Seric 		}
128864743Seric 		break;
128964807Seric #endif
129064725Seric 
129164743Seric 	  case S_IFCHR:
129264743Seric 		sprintf(p, "CHR: ");
129364743Seric 		p += strlen(p);
129464743Seric 		goto defprint;
129564725Seric 
129664743Seric 	  case S_IFBLK:
129764743Seric 		sprintf(p, "BLK: ");
129864743Seric 		p += strlen(p);
129964743Seric 		goto defprint;
130064725Seric 
130165378Seric #ifdef S_IFIFO
130265378Seric 	  case S_IFIFO:
130365378Seric 		sprintf(p, "FIFO: ");
130465378Seric 		p += strlen(p);
130565378Seric 		goto defprint;
130665378Seric #endif
130765378Seric 
130865378Seric #ifdef S_IFDIR
130965378Seric 	  case S_IFDIR:
131065378Seric 		sprintf(p, "DIR: ");
131165378Seric 		p += strlen(p);
131265378Seric 		goto defprint;
131365378Seric #endif
131465378Seric 
131565378Seric #ifdef S_IFLNK
131665378Seric 	  case S_IFLNK:
131765378Seric 		sprintf(p, "LNK: ");
131865378Seric 		p += strlen(p);
131965378Seric 		goto defprint;
132065378Seric #endif
132165378Seric 
132264743Seric 	  default:
132364725Seric defprint:
132464743Seric 		sprintf(p, "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld",
132564743Seric 			major(st.st_dev), minor(st.st_dev), st.st_ino,
132664743Seric 			st.st_nlink, st.st_uid, st.st_gid, st.st_size);
132764743Seric 		break;
132864743Seric 	}
132964725Seric 
133064743Seric printit:
133164743Seric 	if (logit)
133265006Seric 		syslog(LOG_DEBUG, "%s", buf);
133364743Seric 	else
133464743Seric 		printf("%s\n", buf);
133564725Seric }
133665015Seric /*
133765015Seric **  SHORTENSTRING -- return short version of a string
133865015Seric **
133965015Seric **	If the string is already short, just return it.  If it is too
134065015Seric **	long, return the head and tail of the string.
134165015Seric **
134265015Seric **	Parameters:
134365015Seric **		s -- the string to shorten.
134465015Seric **		m -- the max length of the string.
134565015Seric **
134665015Seric **	Returns:
134765015Seric **		Either s or a short version of s.
134865015Seric */
134965015Seric 
135065015Seric #ifndef MAXSHORTSTR
135165055Seric # define MAXSHORTSTR	203
135265015Seric #endif
135365015Seric 
135465015Seric char *
135565015Seric shortenstring(s, m)
135665015Seric 	register char *s;
135765015Seric 	int m;
135865015Seric {
135965015Seric 	int l;
136065015Seric 	static char buf[MAXSHORTSTR + 1];
136165015Seric 
136265015Seric 	l = strlen(s);
136365015Seric 	if (l < m)
136465015Seric 		return s;
136565015Seric 	if (m > MAXSHORTSTR)
136665015Seric 		m = MAXSHORTSTR;
136765015Seric 	else if (m < 10)
136865015Seric 	{
136965015Seric 		if (m < 5)
137065015Seric 		{
137165015Seric 			strncpy(buf, s, m);
137265015Seric 			buf[m] = '\0';
137365015Seric 			return buf;
137465015Seric 		}
137565015Seric 		strncpy(buf, s, m - 3);
137665015Seric 		strcpy(buf + m - 3, "...");
137765015Seric 		return buf;
137865015Seric 	}
137965015Seric 	m = (m - 3) / 2;
138065015Seric 	strncpy(buf, s, m);
138165015Seric 	strcpy(buf + m, "...");
138265015Seric 	strcpy(buf + m + 3, s + l - m);
138365015Seric 	return buf;
138465015Seric }
1385