xref: /original-bsd/usr.sbin/sendmail/src/util.c (revision fac0c393)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)util.c	8.58 (Berkeley) 03/11/95";
11 #endif /* not lint */
12 
13 # include "sendmail.h"
14 # include <sysexits.h>
15 /*
16 **  STRIPQUOTES -- Strip quotes & quote bits from a string.
17 **
18 **	Runs through a string and strips off unquoted quote
19 **	characters and quote bits.  This is done in place.
20 **
21 **	Parameters:
22 **		s -- the string to strip.
23 **
24 **	Returns:
25 **		none.
26 **
27 **	Side Effects:
28 **		none.
29 **
30 **	Called By:
31 **		deliver
32 */
33 
34 stripquotes(s)
35 	char *s;
36 {
37 	register char *p;
38 	register char *q;
39 	register char c;
40 
41 	if (s == NULL)
42 		return;
43 
44 	p = q = s;
45 	do
46 	{
47 		c = *p++;
48 		if (c == '\\')
49 			c = *p++;
50 		else if (c == '"')
51 			continue;
52 		*q++ = c;
53 	} while (c != '\0');
54 }
55 /*
56 **  XALLOC -- Allocate memory and bitch wildly on failure.
57 **
58 **	THIS IS A CLUDGE.  This should be made to give a proper
59 **	error -- but after all, what can we do?
60 **
61 **	Parameters:
62 **		sz -- size of area to allocate.
63 **
64 **	Returns:
65 **		pointer to data region.
66 **
67 **	Side Effects:
68 **		Memory is allocated.
69 */
70 
71 char *
72 xalloc(sz)
73 	register int sz;
74 {
75 	register char *p;
76 
77 	/* some systems can't handle size zero mallocs */
78 	if (sz <= 0)
79 		sz = 1;
80 
81 	p = malloc((unsigned) sz);
82 	if (p == NULL)
83 	{
84 		syserr("Out of memory!!");
85 		abort();
86 		/* exit(EX_UNAVAILABLE); */
87 	}
88 	return (p);
89 }
90 /*
91 **  COPYPLIST -- copy list of pointers.
92 **
93 **	This routine is the equivalent of newstr for lists of
94 **	pointers.
95 **
96 **	Parameters:
97 **		list -- list of pointers to copy.
98 **			Must be NULL terminated.
99 **		copycont -- if TRUE, copy the contents of the vector
100 **			(which must be a string) also.
101 **
102 **	Returns:
103 **		a copy of 'list'.
104 **
105 **	Side Effects:
106 **		none.
107 */
108 
109 char **
110 copyplist(list, copycont)
111 	char **list;
112 	bool copycont;
113 {
114 	register char **vp;
115 	register char **newvp;
116 
117 	for (vp = list; *vp != NULL; vp++)
118 		continue;
119 
120 	vp++;
121 
122 	newvp = (char **) xalloc((int) (vp - list) * sizeof *vp);
123 	bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp);
124 
125 	if (copycont)
126 	{
127 		for (vp = newvp; *vp != NULL; vp++)
128 			*vp = newstr(*vp);
129 	}
130 
131 	return (newvp);
132 }
133 /*
134 **  COPYQUEUE -- copy address queue.
135 **
136 **	This routine is the equivalent of newstr for address queues
137 **	addresses marked with QDONTSEND aren't copied
138 **
139 **	Parameters:
140 **		addr -- list of address structures to copy.
141 **
142 **	Returns:
143 **		a copy of 'addr'.
144 **
145 **	Side Effects:
146 **		none.
147 */
148 
149 ADDRESS *
150 copyqueue(addr)
151 	ADDRESS *addr;
152 {
153 	register ADDRESS *newaddr;
154 	ADDRESS *ret;
155 	register ADDRESS **tail = &ret;
156 
157 	while (addr != NULL)
158 	{
159 		if (!bitset(QDONTSEND, addr->q_flags))
160 		{
161 			newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS));
162 			STRUCTCOPY(*addr, *newaddr);
163 			*tail = newaddr;
164 			tail = &newaddr->q_next;
165 		}
166 		addr = addr->q_next;
167 	}
168 	*tail = NULL;
169 
170 	return ret;
171 }
172 /*
173 **  PRINTAV -- print argument vector.
174 **
175 **	Parameters:
176 **		av -- argument vector.
177 **
178 **	Returns:
179 **		none.
180 **
181 **	Side Effects:
182 **		prints av.
183 */
184 
185 printav(av)
186 	register char **av;
187 {
188 	while (*av != NULL)
189 	{
190 		if (tTd(0, 44))
191 			printf("\n\t%08x=", *av);
192 		else
193 			(void) putchar(' ');
194 		xputs(*av++);
195 	}
196 	(void) putchar('\n');
197 }
198 /*
199 **  LOWER -- turn letter into lower case.
200 **
201 **	Parameters:
202 **		c -- character to turn into lower case.
203 **
204 **	Returns:
205 **		c, in lower case.
206 **
207 **	Side Effects:
208 **		none.
209 */
210 
211 char
212 lower(c)
213 	register char c;
214 {
215 	return((isascii(c) && isupper(c)) ? tolower(c) : c);
216 }
217 /*
218 **  XPUTS -- put string doing control escapes.
219 **
220 **	Parameters:
221 **		s -- string to put.
222 **
223 **	Returns:
224 **		none.
225 **
226 **	Side Effects:
227 **		output to stdout
228 */
229 
230 xputs(s)
231 	register char *s;
232 {
233 	register int c;
234 	register struct metamac *mp;
235 	extern struct metamac MetaMacros[];
236 
237 	if (s == NULL)
238 	{
239 		printf("<null>");
240 		return;
241 	}
242 	while ((c = (*s++ & 0377)) != '\0')
243 	{
244 		if (!isascii(c))
245 		{
246 			if (c == MATCHREPL)
247 			{
248 				putchar('$');
249 				continue;
250 			}
251 			if (c == MACROEXPAND)
252 			{
253 				putchar('$');
254 				if (bitset(0200, *s))
255 					printf("{%s}", macname(*s++ & 0377));
256 				continue;
257 			}
258 			for (mp = MetaMacros; mp->metaname != '\0'; mp++)
259 			{
260 				if ((mp->metaval & 0377) == c)
261 				{
262 					printf("$%c", mp->metaname);
263 					break;
264 				}
265 			}
266 			if (mp->metaname != '\0')
267 				continue;
268 			(void) putchar('\\');
269 			c &= 0177;
270 		}
271 		if (isprint(c))
272 		{
273 			putchar(c);
274 			continue;
275 		}
276 
277 		/* wasn't a meta-macro -- find another way to print it */
278 		switch (c)
279 		{
280 		  case '\n':
281 			c = 'n';
282 			break;
283 
284 		  case '\r':
285 			c = 'r';
286 			break;
287 
288 		  case '\t':
289 			c = 't';
290 			break;
291 
292 		  default:
293 			(void) putchar('^');
294 			(void) putchar(c ^ 0100);
295 			continue;
296 		}
297 		(void) putchar('\\');
298 		(void) putchar(c);
299 	}
300 	(void) fflush(stdout);
301 }
302 /*
303 **  MAKELOWER -- Translate a line into lower case
304 **
305 **	Parameters:
306 **		p -- the string to translate.  If NULL, return is
307 **			immediate.
308 **
309 **	Returns:
310 **		none.
311 **
312 **	Side Effects:
313 **		String pointed to by p is translated to lower case.
314 **
315 **	Called By:
316 **		parse
317 */
318 
319 void
320 makelower(p)
321 	register char *p;
322 {
323 	register char c;
324 
325 	if (p == NULL)
326 		return;
327 	for (; (c = *p) != '\0'; p++)
328 		if (isascii(c) && isupper(c))
329 			*p = tolower(c);
330 }
331 /*
332 **  BUILDFNAME -- build full name from gecos style entry.
333 **
334 **	This routine interprets the strange entry that would appear
335 **	in the GECOS field of the password file.
336 **
337 **	Parameters:
338 **		p -- name to build.
339 **		login -- the login name of this user (for &).
340 **		buf -- place to put the result.
341 **
342 **	Returns:
343 **		none.
344 **
345 **	Side Effects:
346 **		none.
347 */
348 
349 buildfname(gecos, login, buf)
350 	register char *gecos;
351 	char *login;
352 	char *buf;
353 {
354 	register char *p;
355 	register char *bp = buf;
356 	int l;
357 
358 	if (*gecos == '*')
359 		gecos++;
360 
361 	/* find length of final string */
362 	l = 0;
363 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
364 	{
365 		if (*p == '&')
366 			l += strlen(login);
367 		else
368 			l++;
369 	}
370 
371 	/* now fill in buf */
372 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
373 	{
374 		if (*p == '&')
375 		{
376 			(void) strcpy(bp, login);
377 			*bp = toupper(*bp);
378 			while (*bp != '\0')
379 				bp++;
380 		}
381 		else
382 			*bp++ = *p;
383 	}
384 	*bp = '\0';
385 }
386 /*
387 **  SAFEFILE -- return true if a file exists and is safe for a user.
388 **
389 **	Parameters:
390 **		fn -- filename to check.
391 **		uid -- user id to compare against.
392 **		gid -- group id to compare against.
393 **		uname -- user name to compare against (used for group
394 **			sets).
395 **		flags -- modifiers:
396 **			SFF_MUSTOWN -- "uid" must own this file.
397 **			SFF_NOSLINK -- file cannot be a symbolic link.
398 **		mode -- mode bits that must match.
399 **		st -- if set, points to a stat structure that will
400 **			get the stat info for the file.
401 **
402 **	Returns:
403 **		0 if fn exists, is owned by uid, and matches mode.
404 **		An errno otherwise.  The actual errno is cleared.
405 **
406 **	Side Effects:
407 **		none.
408 */
409 
410 #include <grp.h>
411 
412 #ifndef S_IXOTH
413 # define S_IXOTH	(S_IEXEC >> 6)
414 #endif
415 
416 #ifndef S_IXGRP
417 # define S_IXGRP	(S_IEXEC >> 3)
418 #endif
419 
420 #ifndef S_IXUSR
421 # define S_IXUSR	(S_IEXEC)
422 #endif
423 
424 #define ST_MODE_NOFILE	0171147		/* unlikely to occur */
425 
426 int
427 safefile(fn, uid, gid, uname, flags, mode, st)
428 	char *fn;
429 	uid_t uid;
430 	gid_t gid;
431 	char *uname;
432 	int flags;
433 	int mode;
434 	struct stat *st;
435 {
436 	register char *p;
437 	register struct group *gr = NULL;
438 	struct stat stbuf;
439 
440 	if (tTd(54, 4))
441 		printf("safefile(%s, uid=%d, gid=%d, flags=%x, mode=%o):\n",
442 			fn, uid, gid, flags, mode);
443 	errno = 0;
444 	if (st == NULL)
445 		st = &stbuf;
446 
447 	if (!bitset(SFF_NOPATHCHECK, flags) ||
448 	    (uid == 0 && !bitset(SFF_ROOTOK, flags)))
449 	{
450 		/* check the path to the file for acceptability */
451 		for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/')
452 		{
453 			*p = '\0';
454 			if (stat(fn, &stbuf) < 0)
455 				break;
456 			if (uid == 0 && bitset(S_IWGRP|S_IWOTH, stbuf.st_mode))
457 				message("051 WARNING: writable directory %s",
458 					fn);
459 			if (uid == 0 && !bitset(SFF_ROOTOK, flags))
460 			{
461 				if (bitset(S_IXOTH, stbuf.st_mode))
462 					continue;
463 				break;
464 			}
465 			if (stbuf.st_uid == uid &&
466 			    bitset(S_IXUSR, stbuf.st_mode))
467 				continue;
468 			if (stbuf.st_gid == gid &&
469 			    bitset(S_IXGRP, stbuf.st_mode))
470 				continue;
471 #ifndef NO_GROUP_SET
472 			if (uname != NULL &&
473 			    ((gr != NULL && gr->gr_gid == stbuf.st_gid) ||
474 			     (gr = getgrgid(stbuf.st_gid)) != NULL))
475 			{
476 				register char **gp;
477 
478 				for (gp = gr->gr_mem; gp != NULL && *gp != NULL; gp++)
479 					if (strcmp(*gp, uname) == 0)
480 						break;
481 				if (gp != NULL && *gp != NULL &&
482 				    bitset(S_IXGRP, stbuf.st_mode))
483 					continue;
484 			}
485 #endif
486 			if (!bitset(S_IXOTH, stbuf.st_mode))
487 				break;
488 		}
489 		if (p != NULL)
490 		{
491 			int ret = errno;
492 
493 			if (ret == 0)
494 				ret = EACCES;
495 			if (tTd(54, 4))
496 				printf("\t[dir %s] %s\n", fn, errstring(ret));
497 			*p = '/';
498 			return ret;
499 		}
500 	}
501 
502 #ifdef HASLSTAT
503 	if ((bitset(SFF_NOSLINK, flags) ? lstat(fn, st)
504 					: stat(fn, st)) < 0)
505 #else
506 	if (stat(fn, st) < 0)
507 #endif
508 	{
509 		int ret = errno;
510 
511 		if (tTd(54, 4))
512 			printf("\t%s\n", errstring(ret));
513 
514 		errno = 0;
515 		if (!bitset(SFF_CREAT, flags))
516 			return ret;
517 
518 		/* check to see if legal to create the file */
519 		p = strrchr(fn, '/');
520 		if (p == NULL)
521 			return ENOTDIR;
522 		*p = '\0';
523 		if (stat(fn, &stbuf) >= 0)
524 		{
525 			int md = S_IWRITE|S_IEXEC;
526 			if (stbuf.st_uid != uid)
527 				md >>= 6;
528 			if ((stbuf.st_mode & md) != md)
529 				errno = EACCES;
530 		}
531 		ret = errno;
532 		if (tTd(54, 4))
533 			printf("\t[final dir %s uid %d mode %o] %s\n",
534 				fn, stbuf.st_uid, stbuf.st_mode,
535 				errstring(ret));
536 		*p = '/';
537 		st->st_mode = ST_MODE_NOFILE;
538 		return ret;
539 	}
540 
541 #ifdef S_ISLNK
542 	if (bitset(SFF_NOSLINK, flags) && S_ISLNK(st->st_mode))
543 	{
544 		if (tTd(54, 4))
545 			printf("\t[slink mode %o]\tEPERM\n", st->st_mode);
546 		return EPERM;
547 	}
548 #endif
549 	if (bitset(SFF_REGONLY, flags) && !S_ISREG(st->st_mode))
550 	{
551 		if (tTd(54, 4))
552 			printf("\t[non-reg mode %o]\tEPERM\n", st->st_mode);
553 		return EPERM;
554 	}
555 	if (bitset(S_IWUSR|S_IWGRP|S_IWOTH, mode) && bitset(0111, st->st_mode))
556 	{
557 		if (tTd(29, 5))
558 			printf("failed (mode %o: x bits)\n", st->st_mode);
559 		return EPERM;
560 	}
561 
562 	if (bitset(SFF_SETUIDOK, flags))
563 	{
564 		if (bitset(S_ISUID, st->st_mode) &&
565 		    (st->st_uid != 0 || bitset(SFF_ROOTOK, flags)))
566 		{
567 			uid = st->st_uid;
568 			uname = NULL;
569 		}
570 		if (bitset(S_ISGID, st->st_mode) &&
571 		    (st->st_gid != 0 || bitset(SFF_ROOTOK, flags)))
572 			gid = st->st_gid;
573 	}
574 
575 	if (uid == 0 && !bitset(SFF_ROOTOK, flags))
576 		mode >>= 6;
577 	else if (st->st_uid != uid)
578 	{
579 		mode >>= 3;
580 		if (st->st_gid == gid)
581 			;
582 #ifndef NO_GROUP_SET
583 		else if (uname != NULL &&
584 			 ((gr != NULL && gr->gr_gid == st->st_gid) ||
585 			  (gr = getgrgid(st->st_gid)) != NULL))
586 		{
587 			register char **gp;
588 
589 			for (gp = gr->gr_mem; *gp != NULL; gp++)
590 				if (strcmp(*gp, uname) == 0)
591 					break;
592 			if (*gp == NULL)
593 				mode >>= 3;
594 		}
595 #endif
596 		else
597 			mode >>= 3;
598 	}
599 	if (tTd(54, 4))
600 		printf("\t[uid %d, stat %o, mode %o] ",
601 			st->st_uid, st->st_mode, mode);
602 	if ((st->st_uid == uid || st->st_uid == 0 ||
603 	     !bitset(SFF_MUSTOWN, flags)) &&
604 	    (st->st_mode & mode) == mode)
605 	{
606 		if (tTd(54, 4))
607 			printf("\tOK\n");
608 		return 0;
609 	}
610 	if (tTd(54, 4))
611 		printf("\tEACCES\n");
612 	return EACCES;
613 }
614 /*
615 **  SAFEFOPEN -- do a file open with extra checking
616 **
617 **	Parameters:
618 **		fn -- the file name to open.
619 **		omode -- the open-style mode flags.
620 **		cmode -- the create-style mode flags.
621 **		sff -- safefile flags.
622 **
623 **	Returns:
624 **		Same as fopen.
625 */
626 
627 FILE *
628 safefopen(fn, omode, cmode, sff)
629 	char *fn;
630 	int omode;
631 	int cmode;
632 	int sff;
633 {
634 	int rval;
635 	FILE *fp;
636 	int smode;
637 	struct stat stb, sta;
638 	extern char RealUserName[];
639 
640 	if (bitset(O_CREAT, omode))
641 		sff |= SFF_CREAT;
642 	smode = 0;
643 	switch (omode & O_ACCMODE)
644 	{
645 	  case O_RDONLY:
646 		smode = S_IREAD;
647 		break;
648 
649 	  case O_WRONLY:
650 		smode = S_IWRITE;
651 		break;
652 
653 	  case O_RDWR:
654 		smode = S_IREAD|S_IWRITE;
655 		break;
656 
657 	  default:
658 		smode = 0;
659 		break;
660 	}
661 	if (bitset(SFF_OPENASROOT, sff))
662 		rval = safefile(fn, 0, 0, NULL, sff, smode, &stb);
663 	else
664 		rval = safefile(fn, RealUid, RealGid, RealUserName,
665 				sff, smode, &stb);
666 	if (rval != 0)
667 	{
668 		errno = rval;
669 		return NULL;
670 	}
671 	if (stb.st_mode == ST_MODE_NOFILE)
672 		omode |= O_EXCL;
673 
674 	fp = dfopen(fn, omode, cmode);
675 	if (fp == NULL)
676 		return NULL;
677 	if (bitset(O_EXCL, omode))
678 		return fp;
679 	if (fstat(fileno(fp), &sta) < 0 ||
680 	    sta.st_nlink != stb.st_nlink ||
681 	    sta.st_dev != stb.st_dev ||
682 	    sta.st_ino != stb.st_ino ||
683 	    sta.st_uid != stb.st_uid ||
684 	    sta.st_gid != stb.st_gid)
685 	{
686 		syserr("554 cannot open: file %s changed after open", fn);
687 		fclose(fp);
688 		errno = EPERM;
689 		return NULL;
690 	}
691 	return fp;
692 }
693 /*
694 **  FIXCRLF -- fix <CR><LF> in line.
695 **
696 **	Looks for the <CR><LF> combination and turns it into the
697 **	UNIX canonical <NL> character.  It only takes one line,
698 **	i.e., it is assumed that the first <NL> found is the end
699 **	of the line.
700 **
701 **	Parameters:
702 **		line -- the line to fix.
703 **		stripnl -- if true, strip the newline also.
704 **
705 **	Returns:
706 **		none.
707 **
708 **	Side Effects:
709 **		line is changed in place.
710 */
711 
712 fixcrlf(line, stripnl)
713 	char *line;
714 	bool stripnl;
715 {
716 	register char *p;
717 
718 	p = strchr(line, '\n');
719 	if (p == NULL)
720 		return;
721 	if (p > line && p[-1] == '\r')
722 		p--;
723 	if (!stripnl)
724 		*p++ = '\n';
725 	*p = '\0';
726 }
727 /*
728 **  DFOPEN -- determined file open
729 **
730 **	This routine has the semantics of fopen, except that it will
731 **	keep trying a few times to make this happen.  The idea is that
732 **	on very loaded systems, we may run out of resources (inodes,
733 **	whatever), so this tries to get around it.
734 */
735 
736 #ifndef O_ACCMODE
737 # define O_ACCMODE	(O_RDONLY|O_WRONLY|O_RDWR)
738 #endif
739 
740 struct omodes
741 {
742 	int	mask;
743 	int	mode;
744 	char	*farg;
745 } OpenModes[] =
746 {
747 	O_ACCMODE,		O_RDONLY,		"r",
748 	O_ACCMODE|O_APPEND,	O_WRONLY,		"w",
749 	O_ACCMODE|O_APPEND,	O_WRONLY|O_APPEND,	"a",
750 	O_TRUNC,		0,			"w+",
751 	O_APPEND,		O_APPEND,		"a+",
752 	0,			0,			"r+",
753 };
754 
755 FILE *
756 dfopen(filename, omode, cmode)
757 	char *filename;
758 	int omode;
759 	int cmode;
760 {
761 	register int tries;
762 	int fd;
763 	register struct omodes *om;
764 	struct stat st;
765 
766 	for (om = OpenModes; om->mask != 0; om++)
767 		if ((omode & om->mask) == om->mode)
768 			break;
769 
770 	for (tries = 0; tries < 10; tries++)
771 	{
772 		sleep((unsigned) (10 * tries));
773 		errno = 0;
774 		fd = open(filename, omode, cmode);
775 		if (fd >= 0)
776 			break;
777 		switch (errno)
778 		{
779 		  case ENFILE:		/* system file table full */
780 		  case EINTR:		/* interrupted syscall */
781 #ifdef ETXTBSY
782 		  case ETXTBSY:		/* Apollo: net file locked */
783 #endif
784 			continue;
785 		}
786 		break;
787 	}
788 	if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode))
789 	{
790 		int locktype;
791 
792 		/* lock the file to avoid accidental conflicts */
793 		if ((omode & O_ACCMODE) != O_RDONLY)
794 			locktype = LOCK_EX;
795 		else
796 			locktype = LOCK_SH;
797 		(void) lockfile(fd, filename, NULL, locktype);
798 		errno = 0;
799 	}
800 	if (fd < 0)
801 		return NULL;
802 	else
803 		return fdopen(fd, om->farg);
804 }
805 /*
806 **  PUTLINE -- put a line like fputs obeying SMTP conventions
807 **
808 **	This routine always guarantees outputing a newline (or CRLF,
809 **	as appropriate) at the end of the string.
810 **
811 **	Parameters:
812 **		l -- line to put.
813 **		mci -- the mailer connection information.
814 **
815 **	Returns:
816 **		none
817 **
818 **	Side Effects:
819 **		output of l to fp.
820 */
821 
822 putline(l, mci)
823 	register char *l;
824 	register MCI *mci;
825 {
826 	extern void putxline();
827 
828 	putxline(l, mci, TRUE);
829 }
830 
831 void
832 putxline(l, mci, mapfrom)
833 	register char *l;
834 	register MCI *mci;
835 	bool mapfrom;
836 {
837 	register char *p;
838 	register char svchar;
839 	int slop = 0;
840 
841 	/* strip out 0200 bits -- these can look like TELNET protocol */
842 	if (bitset(MCIF_7BIT, mci->mci_flags))
843 	{
844 		for (p = l; (svchar = *p) != '\0'; ++p)
845 			if (bitset(0200, svchar))
846 				*p = svchar &~ 0200;
847 	}
848 
849 	do
850 	{
851 		/* find the end of the line */
852 		p = strchr(l, '\n');
853 		if (p == NULL)
854 			p = &l[strlen(l)];
855 
856 		if (TrafficLogFile != NULL)
857 			fprintf(TrafficLogFile, "%05d >>> ", getpid());
858 
859 		/* check for line overflow */
860 		while (mci->mci_mailer->m_linelimit > 0 &&
861 		       (p - l + slop) > mci->mci_mailer->m_linelimit)
862 		{
863 			register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1];
864 
865 			svchar = *q;
866 			*q = '\0';
867 			if (l[0] == '.' && slop == 0 &&
868 			    bitnset(M_XDOT, mci->mci_mailer->m_flags))
869 			{
870 				(void) putc('.', mci->mci_out);
871 				if (TrafficLogFile != NULL)
872 					(void) putc('.', TrafficLogFile);
873 			}
874 			else if (l[0] == 'F' && slop == 0 && mapfrom &&
875 				 strncmp(l, "From ", 5) == 0 &
876 				 bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
877 			{
878 				(void) putc('>', mci->mci_out);
879 				if (TrafficLogFile != NULL)
880 					(void) putc('>', TrafficLogFile);
881 			}
882 			fputs(l, mci->mci_out);
883 			(void) putc('!', mci->mci_out);
884 			fputs(mci->mci_mailer->m_eol, mci->mci_out);
885 			(void) putc(' ', mci->mci_out);
886 			if (TrafficLogFile != NULL)
887 				fprintf(TrafficLogFile, "%s!\n%05d >>>  ",
888 					l, getpid());
889 			*q = svchar;
890 			l = q;
891 			slop = 1;
892 		}
893 
894 		/* output last part */
895 		if (l[0] == '.' && slop == 0 &&
896 		    bitnset(M_XDOT, mci->mci_mailer->m_flags))
897 		{
898 			(void) putc('.', mci->mci_out);
899 			if (TrafficLogFile != NULL)
900 				(void) putc('.', TrafficLogFile);
901 		}
902 		if (TrafficLogFile != NULL)
903 			fprintf(TrafficLogFile, "%.*s\n", p - l, l);
904 		for ( ; l < p; ++l)
905 			(void) putc(*l, mci->mci_out);
906 		fputs(mci->mci_mailer->m_eol, mci->mci_out);
907 		if (*l == '\n')
908 			++l;
909 	} while (l[0] != '\0');
910 }
911 /*
912 **  XUNLINK -- unlink a file, doing logging as appropriate.
913 **
914 **	Parameters:
915 **		f -- name of file to unlink.
916 **
917 **	Returns:
918 **		none.
919 **
920 **	Side Effects:
921 **		f is unlinked.
922 */
923 
924 xunlink(f)
925 	char *f;
926 {
927 	register int i;
928 
929 # ifdef LOG
930 	if (LogLevel > 98)
931 		syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f);
932 # endif /* LOG */
933 
934 	i = unlink(f);
935 # ifdef LOG
936 	if (i < 0 && LogLevel > 97)
937 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
938 # endif /* LOG */
939 }
940 /*
941 **  XFCLOSE -- close a file, doing logging as appropriate.
942 **
943 **	Parameters:
944 **		fp -- file pointer for the file to close
945 **		a, b -- miscellaneous crud to print for debugging
946 **
947 **	Returns:
948 **		none.
949 **
950 **	Side Effects:
951 **		fp is closed.
952 */
953 
954 xfclose(fp, a, b)
955 	FILE *fp;
956 	char *a, *b;
957 {
958 	if (tTd(53, 99))
959 		printf("xfclose(%x) %s %s\n", fp, a, b);
960 #ifdef XDEBUG
961 	if (fileno(fp) == 1)
962 		syserr("xfclose(%s %s): fd = 1", a, b);
963 #endif
964 	if (fclose(fp) < 0 && tTd(53, 99))
965 		printf("xfclose FAILURE: %s\n", errstring(errno));
966 }
967 /*
968 **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
969 **
970 **	Parameters:
971 **		buf -- place to put the input line.
972 **		siz -- size of buf.
973 **		fp -- file to read from.
974 **		timeout -- the timeout before error occurs.
975 **		during -- what we are trying to read (for error messages).
976 **
977 **	Returns:
978 **		NULL on error (including timeout).  This will also leave
979 **			buf containing a null string.
980 **		buf otherwise.
981 **
982 **	Side Effects:
983 **		none.
984 */
985 
986 static jmp_buf	CtxReadTimeout;
987 static void	readtimeout();
988 
989 char *
990 sfgets(buf, siz, fp, timeout, during)
991 	char *buf;
992 	int siz;
993 	FILE *fp;
994 	time_t timeout;
995 	char *during;
996 {
997 	register EVENT *ev = NULL;
998 	register char *p;
999 
1000 	if (fp == NULL)
1001 	{
1002 		buf[0] = '\0';
1003 		return NULL;
1004 	}
1005 
1006 	/* set the timeout */
1007 	if (timeout != 0)
1008 	{
1009 		if (setjmp(CtxReadTimeout) != 0)
1010 		{
1011 # ifdef LOG
1012 			syslog(LOG_NOTICE,
1013 			    "timeout waiting for input from %s during %s\n",
1014 			    CurHostName? CurHostName: "local", during);
1015 # endif
1016 			errno = 0;
1017 			usrerr("451 timeout waiting for input during %s",
1018 				during);
1019 			buf[0] = '\0';
1020 #ifdef XDEBUG
1021 			checkfd012(during);
1022 #endif
1023 			return (NULL);
1024 		}
1025 		ev = setevent(timeout, readtimeout, 0);
1026 	}
1027 
1028 	/* try to read */
1029 	p = NULL;
1030 	while (!feof(fp) && !ferror(fp))
1031 	{
1032 		errno = 0;
1033 		p = fgets(buf, siz, fp);
1034 		if (p != NULL || errno != EINTR)
1035 			break;
1036 		clearerr(fp);
1037 	}
1038 
1039 	/* clear the event if it has not sprung */
1040 	clrevent(ev);
1041 
1042 	/* clean up the books and exit */
1043 	LineNumber++;
1044 	if (p == NULL)
1045 	{
1046 		buf[0] = '\0';
1047 		if (TrafficLogFile != NULL)
1048 			fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid());
1049 		return (NULL);
1050 	}
1051 	if (TrafficLogFile != NULL)
1052 		fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf);
1053 	if (SevenBitInput)
1054 	{
1055 		for (p = buf; *p != '\0'; p++)
1056 			*p &= ~0200;
1057 	}
1058 	else if (!HasEightBits)
1059 	{
1060 		for (p = buf; *p != '\0'; p++)
1061 		{
1062 			if (bitset(0200, *p))
1063 			{
1064 				HasEightBits = TRUE;
1065 				break;
1066 			}
1067 		}
1068 	}
1069 	return (buf);
1070 }
1071 
1072 static void
1073 readtimeout(timeout)
1074 	time_t timeout;
1075 {
1076 	longjmp(CtxReadTimeout, 1);
1077 }
1078 /*
1079 **  FGETFOLDED -- like fgets, but know about folded lines.
1080 **
1081 **	Parameters:
1082 **		buf -- place to put result.
1083 **		n -- bytes available.
1084 **		f -- file to read from.
1085 **
1086 **	Returns:
1087 **		input line(s) on success, NULL on error or EOF.
1088 **		This will normally be buf -- unless the line is too
1089 **			long, when it will be xalloc()ed.
1090 **
1091 **	Side Effects:
1092 **		buf gets lines from f, with continuation lines (lines
1093 **		with leading white space) appended.  CRLF's are mapped
1094 **		into single newlines.  Any trailing NL is stripped.
1095 */
1096 
1097 char *
1098 fgetfolded(buf, n, f)
1099 	char *buf;
1100 	register int n;
1101 	FILE *f;
1102 {
1103 	register char *p = buf;
1104 	char *bp = buf;
1105 	register int i;
1106 
1107 	n--;
1108 	while ((i = getc(f)) != EOF)
1109 	{
1110 		if (i == '\r')
1111 		{
1112 			i = getc(f);
1113 			if (i != '\n')
1114 			{
1115 				if (i != EOF)
1116 					(void) ungetc(i, f);
1117 				i = '\r';
1118 			}
1119 		}
1120 		if (--n <= 0)
1121 		{
1122 			/* allocate new space */
1123 			char *nbp;
1124 			int nn;
1125 
1126 			nn = (p - bp);
1127 			if (nn < MEMCHUNKSIZE)
1128 				nn *= 2;
1129 			else
1130 				nn += MEMCHUNKSIZE;
1131 			nbp = xalloc(nn);
1132 			bcopy(bp, nbp, p - bp);
1133 			p = &nbp[p - bp];
1134 			if (bp != buf)
1135 				free(bp);
1136 			bp = nbp;
1137 			n = nn - (p - bp);
1138 		}
1139 		*p++ = i;
1140 		if (i == '\n')
1141 		{
1142 			LineNumber++;
1143 			i = getc(f);
1144 			if (i != EOF)
1145 				(void) ungetc(i, f);
1146 			if (i != ' ' && i != '\t')
1147 				break;
1148 		}
1149 	}
1150 	if (p == bp)
1151 		return (NULL);
1152 	*--p = '\0';
1153 	return (bp);
1154 }
1155 /*
1156 **  CURTIME -- return current time.
1157 **
1158 **	Parameters:
1159 **		none.
1160 **
1161 **	Returns:
1162 **		the current time.
1163 **
1164 **	Side Effects:
1165 **		none.
1166 */
1167 
1168 time_t
1169 curtime()
1170 {
1171 	auto time_t t;
1172 
1173 	(void) time(&t);
1174 	return (t);
1175 }
1176 /*
1177 **  ATOBOOL -- convert a string representation to boolean.
1178 **
1179 **	Defaults to "TRUE"
1180 **
1181 **	Parameters:
1182 **		s -- string to convert.  Takes "tTyY" as true,
1183 **			others as false.
1184 **
1185 **	Returns:
1186 **		A boolean representation of the string.
1187 **
1188 **	Side Effects:
1189 **		none.
1190 */
1191 
1192 bool
1193 atobool(s)
1194 	register char *s;
1195 {
1196 	if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL)
1197 		return (TRUE);
1198 	return (FALSE);
1199 }
1200 /*
1201 **  ATOOCT -- convert a string representation to octal.
1202 **
1203 **	Parameters:
1204 **		s -- string to convert.
1205 **
1206 **	Returns:
1207 **		An integer representing the string interpreted as an
1208 **		octal number.
1209 **
1210 **	Side Effects:
1211 **		none.
1212 */
1213 
1214 atooct(s)
1215 	register char *s;
1216 {
1217 	register int i = 0;
1218 
1219 	while (*s >= '0' && *s <= '7')
1220 		i = (i << 3) | (*s++ - '0');
1221 	return (i);
1222 }
1223 /*
1224 **  WAITFOR -- wait for a particular process id.
1225 **
1226 **	Parameters:
1227 **		pid -- process id to wait for.
1228 **
1229 **	Returns:
1230 **		status of pid.
1231 **		-1 if pid never shows up.
1232 **
1233 **	Side Effects:
1234 **		none.
1235 */
1236 
1237 int
1238 waitfor(pid)
1239 	int pid;
1240 {
1241 #ifdef WAITUNION
1242 	union wait st;
1243 #else
1244 	auto int st;
1245 #endif
1246 	int i;
1247 
1248 	do
1249 	{
1250 		errno = 0;
1251 		i = wait(&st);
1252 	} while ((i >= 0 || errno == EINTR) && i != pid);
1253 	if (i < 0)
1254 		return -1;
1255 #ifdef WAITUNION
1256 	return st.w_status;
1257 #else
1258 	return st;
1259 #endif
1260 }
1261 /*
1262 **  BITINTERSECT -- tell if two bitmaps intersect
1263 **
1264 **	Parameters:
1265 **		a, b -- the bitmaps in question
1266 **
1267 **	Returns:
1268 **		TRUE if they have a non-null intersection
1269 **		FALSE otherwise
1270 **
1271 **	Side Effects:
1272 **		none.
1273 */
1274 
1275 bool
1276 bitintersect(a, b)
1277 	BITMAP a;
1278 	BITMAP b;
1279 {
1280 	int i;
1281 
1282 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
1283 		if ((a[i] & b[i]) != 0)
1284 			return (TRUE);
1285 	return (FALSE);
1286 }
1287 /*
1288 **  BITZEROP -- tell if a bitmap is all zero
1289 **
1290 **	Parameters:
1291 **		map -- the bit map to check
1292 **
1293 **	Returns:
1294 **		TRUE if map is all zero.
1295 **		FALSE if there are any bits set in map.
1296 **
1297 **	Side Effects:
1298 **		none.
1299 */
1300 
1301 bool
1302 bitzerop(map)
1303 	BITMAP map;
1304 {
1305 	int i;
1306 
1307 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
1308 		if (map[i] != 0)
1309 			return (FALSE);
1310 	return (TRUE);
1311 }
1312 /*
1313 **  STRCONTAINEDIN -- tell if one string is contained in another
1314 **
1315 **	Parameters:
1316 **		a -- possible substring.
1317 **		b -- possible superstring.
1318 **
1319 **	Returns:
1320 **		TRUE if a is contained in b.
1321 **		FALSE otherwise.
1322 */
1323 
1324 bool
1325 strcontainedin(a, b)
1326 	register char *a;
1327 	register char *b;
1328 {
1329 	int la;
1330 	int lb;
1331 	int c;
1332 
1333 	la = strlen(a);
1334 	lb = strlen(b);
1335 	c = *a;
1336 	if (isascii(c) && isupper(c))
1337 		c = tolower(c);
1338 	for (; lb-- >= la; b++)
1339 	{
1340 		if (*b != c && isascii(*b) && isupper(*b) && tolower(*b) != c)
1341 			continue;
1342 		if (strncasecmp(a, b, la) == 0)
1343 			return TRUE;
1344 	}
1345 	return FALSE;
1346 }
1347 /*
1348 **  CHECKFD012 -- check low numbered file descriptors
1349 **
1350 **	File descriptors 0, 1, and 2 should be open at all times.
1351 **	This routine verifies that, and fixes it if not true.
1352 **
1353 **	Parameters:
1354 **		where -- a tag printed if the assertion failed
1355 **
1356 **	Returns:
1357 **		none
1358 */
1359 
1360 checkfd012(where)
1361 	char *where;
1362 {
1363 #ifdef XDEBUG
1364 	register int i;
1365 	struct stat stbuf;
1366 
1367 	for (i = 0; i < 3; i++)
1368 	{
1369 		if (fstat(i, &stbuf) < 0 && errno != EOPNOTSUPP)
1370 		{
1371 			/* oops.... */
1372 			int fd;
1373 
1374 			syserr("%s: fd %d not open", where, i);
1375 			fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666);
1376 			if (fd != i)
1377 			{
1378 				(void) dup2(fd, i);
1379 				(void) close(fd);
1380 			}
1381 		}
1382 	}
1383 #endif /* XDEBUG */
1384 }
1385 /*
1386 **  PRINTOPENFDS -- print the open file descriptors (for debugging)
1387 **
1388 **	Parameters:
1389 **		logit -- if set, send output to syslog; otherwise
1390 **			print for debugging.
1391 **
1392 **	Returns:
1393 **		none.
1394 */
1395 
1396 #include <netdb.h>
1397 #include <arpa/inet.h>
1398 
1399 printopenfds(logit)
1400 	bool logit;
1401 {
1402 	register int fd;
1403 	extern int DtableSize;
1404 
1405 	for (fd = 0; fd < DtableSize; fd++)
1406 		dumpfd(fd, FALSE, logit);
1407 }
1408 /*
1409 **  DUMPFD -- dump a file descriptor
1410 **
1411 **	Parameters:
1412 **		fd -- the file descriptor to dump.
1413 **		printclosed -- if set, print a notification even if
1414 **			it is closed; otherwise print nothing.
1415 **		logit -- if set, send output to syslog instead of stdout.
1416 */
1417 
1418 dumpfd(fd, printclosed, logit)
1419 	int fd;
1420 	bool printclosed;
1421 	bool logit;
1422 {
1423 	register struct hostent *hp;
1424 	register char *p;
1425 	char *fmtstr;
1426 	struct sockaddr_in sin;
1427 	auto int slen;
1428 	struct stat st;
1429 	char buf[200];
1430 
1431 	p = buf;
1432 	sprintf(p, "%3d: ", fd);
1433 	p += strlen(p);
1434 
1435 	if (fstat(fd, &st) < 0)
1436 	{
1437 		if (printclosed || errno != EBADF)
1438 		{
1439 			sprintf(p, "CANNOT STAT (%s)", errstring(errno));
1440 			goto printit;
1441 		}
1442 		return;
1443 	}
1444 
1445 	slen = fcntl(fd, F_GETFL, NULL);
1446 	if (slen != -1)
1447 	{
1448 		sprintf(p, "fl=0x%x, ", slen);
1449 		p += strlen(p);
1450 	}
1451 
1452 	sprintf(p, "mode=%o: ", st.st_mode);
1453 	p += strlen(p);
1454 	switch (st.st_mode & S_IFMT)
1455 	{
1456 #ifdef S_IFSOCK
1457 	  case S_IFSOCK:
1458 		sprintf(p, "SOCK ");
1459 		p += strlen(p);
1460 		slen = sizeof sin;
1461 		if (getsockname(fd, (struct sockaddr *) &sin, &slen) < 0)
1462 			sprintf(p, "(badsock)");
1463 		else
1464 		{
1465 			hp = gethostbyaddr((char *) &sin.sin_addr,
1466 					   INADDRSZ, AF_INET);
1467 			sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr)
1468 						   : hp->h_name, ntohs(sin.sin_port));
1469 		}
1470 		p += strlen(p);
1471 		sprintf(p, "->");
1472 		p += strlen(p);
1473 		slen = sizeof sin;
1474 		if (getpeername(fd, (struct sockaddr *) &sin, &slen) < 0)
1475 			sprintf(p, "(badsock)");
1476 		else
1477 		{
1478 			hp = gethostbyaddr((char *) &sin.sin_addr,
1479 					   INADDRSZ, AF_INET);
1480 			sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr)
1481 						   : hp->h_name, ntohs(sin.sin_port));
1482 		}
1483 		break;
1484 #endif
1485 
1486 	  case S_IFCHR:
1487 		sprintf(p, "CHR: ");
1488 		p += strlen(p);
1489 		goto defprint;
1490 
1491 	  case S_IFBLK:
1492 		sprintf(p, "BLK: ");
1493 		p += strlen(p);
1494 		goto defprint;
1495 
1496 #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK)
1497 	  case S_IFIFO:
1498 		sprintf(p, "FIFO: ");
1499 		p += strlen(p);
1500 		goto defprint;
1501 #endif
1502 
1503 #ifdef S_IFDIR
1504 	  case S_IFDIR:
1505 		sprintf(p, "DIR: ");
1506 		p += strlen(p);
1507 		goto defprint;
1508 #endif
1509 
1510 #ifdef S_IFLNK
1511 	  case S_IFLNK:
1512 		sprintf(p, "LNK: ");
1513 		p += strlen(p);
1514 		goto defprint;
1515 #endif
1516 
1517 	  default:
1518 defprint:
1519 		if (sizeof st.st_size > sizeof (long))
1520 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%qd";
1521 		else
1522 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld";
1523 		sprintf(p, fmtstr,
1524 			major(st.st_dev), minor(st.st_dev), st.st_ino,
1525 			st.st_nlink, st.st_uid, st.st_gid, st.st_size);
1526 		break;
1527 	}
1528 
1529 printit:
1530 #ifdef LOG
1531 	if (logit)
1532 		syslog(LOG_DEBUG, "%s", buf);
1533 	else
1534 #endif
1535 		printf("%s\n", buf);
1536 }
1537 /*
1538 **  SHORTENSTRING -- return short version of a string
1539 **
1540 **	If the string is already short, just return it.  If it is too
1541 **	long, return the head and tail of the string.
1542 **
1543 **	Parameters:
1544 **		s -- the string to shorten.
1545 **		m -- the max length of the string.
1546 **
1547 **	Returns:
1548 **		Either s or a short version of s.
1549 */
1550 
1551 #ifndef MAXSHORTSTR
1552 # define MAXSHORTSTR	203
1553 #endif
1554 
1555 char *
1556 shortenstring(s, m)
1557 	register char *s;
1558 	int m;
1559 {
1560 	int l;
1561 	static char buf[MAXSHORTSTR + 1];
1562 
1563 	l = strlen(s);
1564 	if (l < m)
1565 		return s;
1566 	if (m > MAXSHORTSTR)
1567 		m = MAXSHORTSTR;
1568 	else if (m < 10)
1569 	{
1570 		if (m < 5)
1571 		{
1572 			strncpy(buf, s, m);
1573 			buf[m] = '\0';
1574 			return buf;
1575 		}
1576 		strncpy(buf, s, m - 3);
1577 		strcpy(buf + m - 3, "...");
1578 		return buf;
1579 	}
1580 	m = (m - 3) / 2;
1581 	strncpy(buf, s, m);
1582 	strcpy(buf + m, "...");
1583 	strcpy(buf + m + 3, s + l - m);
1584 	return buf;
1585 }
1586 /*
1587 **  GET_COLUMN  -- look up a Column in a line buffer
1588 **
1589 **	Parameters:
1590 **		line -- the raw text line to search.
1591 **		col -- the column number to fetch.
1592 **		delim -- the delimiter between columns.  If null,
1593 **			use white space.
1594 **		buf -- the output buffer.
1595 **
1596 **	Returns:
1597 **		buf if successful.
1598 **		NULL otherwise.
1599 */
1600 
1601 char *
1602 get_column(line, col, delim, buf)
1603 	char line[];
1604 	int col;
1605 	char delim;
1606 	char buf[];
1607 {
1608 	char *p;
1609 	char *begin, *end;
1610 	int i;
1611 	char delimbuf[3];
1612 
1613 	if (delim == '\0')
1614 		strcpy(delimbuf, "\t ");
1615 	else
1616 	{
1617 		delimbuf[0] = delim;
1618 		delimbuf[1] = '\0';
1619 	}
1620 
1621 	p = line;
1622 	if (*p == '\0')
1623 		return NULL;			/* line empty */
1624 	if (*p == delim && col == 0)
1625 		return NULL;			/* first column empty */
1626 
1627 	begin = line;
1628 
1629 	if (col == 0 && delim == '\0')
1630 	{
1631 		while (*begin && isspace(*begin))
1632 			begin++;
1633 	}
1634 
1635 	for (i = 0; i < col; i++)
1636 	{
1637 		if ((begin = strpbrk(begin, delimbuf)) == NULL)
1638 			return NULL;		/* no such column */
1639 		begin++;
1640 		if (delim == '\0')
1641 		{
1642 			while (*begin && isspace(*begin))
1643 				begin++;
1644 		}
1645 	}
1646 
1647 	end = strpbrk(begin, delimbuf);
1648 	if (end == NULL)
1649 	{
1650 		strcpy(buf, begin);
1651 	}
1652 	else
1653 	{
1654 		strncpy(buf, begin, end - begin);
1655 		buf[end - begin] = '\0';
1656 	}
1657 	return buf;
1658 }
1659 /*
1660 **  CLEANSTRCPY -- copy string keeping out bogus characters
1661 **
1662 **	Parameters:
1663 **		t -- "to" string.
1664 **		f -- "from" string.
1665 **		l -- length of space available in "to" string.
1666 **
1667 **	Returns:
1668 **		none.
1669 */
1670 
1671 void
1672 cleanstrcpy(t, f, l)
1673 	register char *t;
1674 	register char *f;
1675 	int l;
1676 {
1677 #ifdef LOG
1678 	/* check for newlines and log if necessary */
1679 	(void) denlstring(f, TRUE, TRUE);
1680 #endif
1681 
1682 	l--;
1683 	while (l > 0 && *f != '\0')
1684 	{
1685 		if (isascii(*f) &&
1686 		    (isalnum(*f) || strchr("!#$%&'*+-./^_`{|}~", *f) != NULL))
1687 		{
1688 			l--;
1689 			*t++ = *f;
1690 		}
1691 		f++;
1692 	}
1693 	*t = '\0';
1694 }
1695 /*
1696 **  DENLSTRING -- convert newlines in a string to spaces
1697 **
1698 **	Parameters:
1699 **		s -- the input string
1700 **		strict -- if set, don't permit continuation lines.
1701 **		logattacks -- if set, log attempted attacks.
1702 **
1703 **	Returns:
1704 **		A pointer to a version of the string with newlines
1705 **		mapped to spaces.  This should be copied.
1706 */
1707 
1708 char *
1709 denlstring(s, strict, logattacks)
1710 	char *s;
1711 	int strict;
1712 	int logattacks;
1713 {
1714 	register char *p;
1715 	int l;
1716 	static char *bp = NULL;
1717 	static int bl = 0;
1718 
1719 	p = s;
1720 	while ((p = strchr(p, '\n')) != NULL)
1721 		if (strict || (*++p != ' ' && *p != '\t'))
1722 			break;
1723 	if (p == NULL)
1724 		return s;
1725 
1726 	l = strlen(s) + 1;
1727 	if (bl < l)
1728 	{
1729 		/* allocate more space */
1730 		if (bp != NULL)
1731 			free(bp);
1732 		bp = xalloc(l);
1733 		bl = l;
1734 	}
1735 	strcpy(bp, s);
1736 	for (p = bp; (p = strchr(p, '\n')) != NULL; )
1737 		*p++ = ' ';
1738 
1739 /*
1740 #ifdef LOG
1741 	if (logattacks)
1742 	{
1743 		syslog(LOG_NOTICE, "POSSIBLE ATTACK from %s: newline in string \"%s\"",
1744 			RealHostName == NULL ? "[UNKNOWN]" : RealHostName,
1745 			shortenstring(bp, 80));
1746 	}
1747 #endif
1748 */
1749 
1750 	return bp;
1751 }
1752