xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 68513)
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.57 (Berkeley) 03/10/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 	register char *p;
827 	register char svchar;
828 	int slop = 0;
829 
830 	/* strip out 0200 bits -- these can look like TELNET protocol */
831 	if (bitset(MCIF_7BIT, mci->mci_flags))
832 	{
833 		for (p = l; (svchar = *p) != '\0'; ++p)
834 			if (bitset(0200, svchar))
835 				*p = svchar &~ 0200;
836 	}
837 
838 	do
839 	{
840 		/* find the end of the line */
841 		p = strchr(l, '\n');
842 		if (p == NULL)
843 			p = &l[strlen(l)];
844 
845 		if (TrafficLogFile != NULL)
846 			fprintf(TrafficLogFile, "%05d >>> ", getpid());
847 
848 		/* check for line overflow */
849 		while (mci->mci_mailer->m_linelimit > 0 &&
850 		       (p - l + slop) > mci->mci_mailer->m_linelimit)
851 		{
852 			register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1];
853 
854 			svchar = *q;
855 			*q = '\0';
856 			if (l[0] == '.' && slop == 0 &&
857 			    bitnset(M_XDOT, mci->mci_mailer->m_flags))
858 			{
859 				(void) putc('.', mci->mci_out);
860 				if (TrafficLogFile != NULL)
861 					(void) putc('.', TrafficLogFile);
862 			}
863 			fputs(l, mci->mci_out);
864 			(void) putc('!', mci->mci_out);
865 			fputs(mci->mci_mailer->m_eol, mci->mci_out);
866 			(void) putc(' ', mci->mci_out);
867 			if (TrafficLogFile != NULL)
868 				fprintf(TrafficLogFile, "%s!\n%05d >>>  ",
869 					l, getpid());
870 			*q = svchar;
871 			l = q;
872 			slop = 1;
873 		}
874 
875 		/* output last part */
876 		if (l[0] == '.' && slop == 0 &&
877 		    bitnset(M_XDOT, mci->mci_mailer->m_flags))
878 		{
879 			(void) putc('.', mci->mci_out);
880 			if (TrafficLogFile != NULL)
881 				(void) putc('.', TrafficLogFile);
882 		}
883 		if (TrafficLogFile != NULL)
884 			fprintf(TrafficLogFile, "%.*s\n", p - l, l);
885 		for ( ; l < p; ++l)
886 			(void) putc(*l, mci->mci_out);
887 		fputs(mci->mci_mailer->m_eol, mci->mci_out);
888 		if (*l == '\n')
889 			++l;
890 	} while (l[0] != '\0');
891 }
892 /*
893 **  XUNLINK -- unlink a file, doing logging as appropriate.
894 **
895 **	Parameters:
896 **		f -- name of file to unlink.
897 **
898 **	Returns:
899 **		none.
900 **
901 **	Side Effects:
902 **		f is unlinked.
903 */
904 
905 xunlink(f)
906 	char *f;
907 {
908 	register int i;
909 
910 # ifdef LOG
911 	if (LogLevel > 98)
912 		syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f);
913 # endif /* LOG */
914 
915 	i = unlink(f);
916 # ifdef LOG
917 	if (i < 0 && LogLevel > 97)
918 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
919 # endif /* LOG */
920 }
921 /*
922 **  XFCLOSE -- close a file, doing logging as appropriate.
923 **
924 **	Parameters:
925 **		fp -- file pointer for the file to close
926 **		a, b -- miscellaneous crud to print for debugging
927 **
928 **	Returns:
929 **		none.
930 **
931 **	Side Effects:
932 **		fp is closed.
933 */
934 
935 xfclose(fp, a, b)
936 	FILE *fp;
937 	char *a, *b;
938 {
939 	if (tTd(53, 99))
940 		printf("xfclose(%x) %s %s\n", fp, a, b);
941 #ifdef XDEBUG
942 	if (fileno(fp) == 1)
943 		syserr("xfclose(%s %s): fd = 1", a, b);
944 #endif
945 	if (fclose(fp) < 0 && tTd(53, 99))
946 		printf("xfclose FAILURE: %s\n", errstring(errno));
947 }
948 /*
949 **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
950 **
951 **	Parameters:
952 **		buf -- place to put the input line.
953 **		siz -- size of buf.
954 **		fp -- file to read from.
955 **		timeout -- the timeout before error occurs.
956 **		during -- what we are trying to read (for error messages).
957 **
958 **	Returns:
959 **		NULL on error (including timeout).  This will also leave
960 **			buf containing a null string.
961 **		buf otherwise.
962 **
963 **	Side Effects:
964 **		none.
965 */
966 
967 static jmp_buf	CtxReadTimeout;
968 static void	readtimeout();
969 
970 char *
971 sfgets(buf, siz, fp, timeout, during)
972 	char *buf;
973 	int siz;
974 	FILE *fp;
975 	time_t timeout;
976 	char *during;
977 {
978 	register EVENT *ev = NULL;
979 	register char *p;
980 
981 	if (fp == NULL)
982 	{
983 		buf[0] = '\0';
984 		return NULL;
985 	}
986 
987 	/* set the timeout */
988 	if (timeout != 0)
989 	{
990 		if (setjmp(CtxReadTimeout) != 0)
991 		{
992 # ifdef LOG
993 			syslog(LOG_NOTICE,
994 			    "timeout waiting for input from %s during %s\n",
995 			    CurHostName? CurHostName: "local", during);
996 # endif
997 			errno = 0;
998 			usrerr("451 timeout waiting for input during %s",
999 				during);
1000 			buf[0] = '\0';
1001 #ifdef XDEBUG
1002 			checkfd012(during);
1003 #endif
1004 			return (NULL);
1005 		}
1006 		ev = setevent(timeout, readtimeout, 0);
1007 	}
1008 
1009 	/* try to read */
1010 	p = NULL;
1011 	while (!feof(fp) && !ferror(fp))
1012 	{
1013 		errno = 0;
1014 		p = fgets(buf, siz, fp);
1015 		if (p != NULL || errno != EINTR)
1016 			break;
1017 		clearerr(fp);
1018 	}
1019 
1020 	/* clear the event if it has not sprung */
1021 	clrevent(ev);
1022 
1023 	/* clean up the books and exit */
1024 	LineNumber++;
1025 	if (p == NULL)
1026 	{
1027 		buf[0] = '\0';
1028 		if (TrafficLogFile != NULL)
1029 			fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid());
1030 		return (NULL);
1031 	}
1032 	if (TrafficLogFile != NULL)
1033 		fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf);
1034 	if (SevenBitInput)
1035 	{
1036 		for (p = buf; *p != '\0'; p++)
1037 			*p &= ~0200;
1038 	}
1039 	else if (!HasEightBits)
1040 	{
1041 		for (p = buf; *p != '\0'; p++)
1042 		{
1043 			if (bitset(0200, *p))
1044 			{
1045 				HasEightBits = TRUE;
1046 				break;
1047 			}
1048 		}
1049 	}
1050 	return (buf);
1051 }
1052 
1053 static void
1054 readtimeout(timeout)
1055 	time_t timeout;
1056 {
1057 	longjmp(CtxReadTimeout, 1);
1058 }
1059 /*
1060 **  FGETFOLDED -- like fgets, but know about folded lines.
1061 **
1062 **	Parameters:
1063 **		buf -- place to put result.
1064 **		n -- bytes available.
1065 **		f -- file to read from.
1066 **
1067 **	Returns:
1068 **		input line(s) on success, NULL on error or EOF.
1069 **		This will normally be buf -- unless the line is too
1070 **			long, when it will be xalloc()ed.
1071 **
1072 **	Side Effects:
1073 **		buf gets lines from f, with continuation lines (lines
1074 **		with leading white space) appended.  CRLF's are mapped
1075 **		into single newlines.  Any trailing NL is stripped.
1076 */
1077 
1078 char *
1079 fgetfolded(buf, n, f)
1080 	char *buf;
1081 	register int n;
1082 	FILE *f;
1083 {
1084 	register char *p = buf;
1085 	char *bp = buf;
1086 	register int i;
1087 
1088 	n--;
1089 	while ((i = getc(f)) != EOF)
1090 	{
1091 		if (i == '\r')
1092 		{
1093 			i = getc(f);
1094 			if (i != '\n')
1095 			{
1096 				if (i != EOF)
1097 					(void) ungetc(i, f);
1098 				i = '\r';
1099 			}
1100 		}
1101 		if (--n <= 0)
1102 		{
1103 			/* allocate new space */
1104 			char *nbp;
1105 			int nn;
1106 
1107 			nn = (p - bp);
1108 			if (nn < MEMCHUNKSIZE)
1109 				nn *= 2;
1110 			else
1111 				nn += MEMCHUNKSIZE;
1112 			nbp = xalloc(nn);
1113 			bcopy(bp, nbp, p - bp);
1114 			p = &nbp[p - bp];
1115 			if (bp != buf)
1116 				free(bp);
1117 			bp = nbp;
1118 			n = nn - (p - bp);
1119 		}
1120 		*p++ = i;
1121 		if (i == '\n')
1122 		{
1123 			LineNumber++;
1124 			i = getc(f);
1125 			if (i != EOF)
1126 				(void) ungetc(i, f);
1127 			if (i != ' ' && i != '\t')
1128 				break;
1129 		}
1130 	}
1131 	if (p == bp)
1132 		return (NULL);
1133 	*--p = '\0';
1134 	return (bp);
1135 }
1136 /*
1137 **  CURTIME -- return current time.
1138 **
1139 **	Parameters:
1140 **		none.
1141 **
1142 **	Returns:
1143 **		the current time.
1144 **
1145 **	Side Effects:
1146 **		none.
1147 */
1148 
1149 time_t
1150 curtime()
1151 {
1152 	auto time_t t;
1153 
1154 	(void) time(&t);
1155 	return (t);
1156 }
1157 /*
1158 **  ATOBOOL -- convert a string representation to boolean.
1159 **
1160 **	Defaults to "TRUE"
1161 **
1162 **	Parameters:
1163 **		s -- string to convert.  Takes "tTyY" as true,
1164 **			others as false.
1165 **
1166 **	Returns:
1167 **		A boolean representation of the string.
1168 **
1169 **	Side Effects:
1170 **		none.
1171 */
1172 
1173 bool
1174 atobool(s)
1175 	register char *s;
1176 {
1177 	if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL)
1178 		return (TRUE);
1179 	return (FALSE);
1180 }
1181 /*
1182 **  ATOOCT -- convert a string representation to octal.
1183 **
1184 **	Parameters:
1185 **		s -- string to convert.
1186 **
1187 **	Returns:
1188 **		An integer representing the string interpreted as an
1189 **		octal number.
1190 **
1191 **	Side Effects:
1192 **		none.
1193 */
1194 
1195 atooct(s)
1196 	register char *s;
1197 {
1198 	register int i = 0;
1199 
1200 	while (*s >= '0' && *s <= '7')
1201 		i = (i << 3) | (*s++ - '0');
1202 	return (i);
1203 }
1204 /*
1205 **  WAITFOR -- wait for a particular process id.
1206 **
1207 **	Parameters:
1208 **		pid -- process id to wait for.
1209 **
1210 **	Returns:
1211 **		status of pid.
1212 **		-1 if pid never shows up.
1213 **
1214 **	Side Effects:
1215 **		none.
1216 */
1217 
1218 int
1219 waitfor(pid)
1220 	int pid;
1221 {
1222 #ifdef WAITUNION
1223 	union wait st;
1224 #else
1225 	auto int st;
1226 #endif
1227 	int i;
1228 
1229 	do
1230 	{
1231 		errno = 0;
1232 		i = wait(&st);
1233 	} while ((i >= 0 || errno == EINTR) && i != pid);
1234 	if (i < 0)
1235 		return -1;
1236 #ifdef WAITUNION
1237 	return st.w_status;
1238 #else
1239 	return st;
1240 #endif
1241 }
1242 /*
1243 **  BITINTERSECT -- tell if two bitmaps intersect
1244 **
1245 **	Parameters:
1246 **		a, b -- the bitmaps in question
1247 **
1248 **	Returns:
1249 **		TRUE if they have a non-null intersection
1250 **		FALSE otherwise
1251 **
1252 **	Side Effects:
1253 **		none.
1254 */
1255 
1256 bool
1257 bitintersect(a, b)
1258 	BITMAP a;
1259 	BITMAP b;
1260 {
1261 	int i;
1262 
1263 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
1264 		if ((a[i] & b[i]) != 0)
1265 			return (TRUE);
1266 	return (FALSE);
1267 }
1268 /*
1269 **  BITZEROP -- tell if a bitmap is all zero
1270 **
1271 **	Parameters:
1272 **		map -- the bit map to check
1273 **
1274 **	Returns:
1275 **		TRUE if map is all zero.
1276 **		FALSE if there are any bits set in map.
1277 **
1278 **	Side Effects:
1279 **		none.
1280 */
1281 
1282 bool
1283 bitzerop(map)
1284 	BITMAP map;
1285 {
1286 	int i;
1287 
1288 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
1289 		if (map[i] != 0)
1290 			return (FALSE);
1291 	return (TRUE);
1292 }
1293 /*
1294 **  STRCONTAINEDIN -- tell if one string is contained in another
1295 **
1296 **	Parameters:
1297 **		a -- possible substring.
1298 **		b -- possible superstring.
1299 **
1300 **	Returns:
1301 **		TRUE if a is contained in b.
1302 **		FALSE otherwise.
1303 */
1304 
1305 bool
1306 strcontainedin(a, b)
1307 	register char *a;
1308 	register char *b;
1309 {
1310 	int la;
1311 	int lb;
1312 	int c;
1313 
1314 	la = strlen(a);
1315 	lb = strlen(b);
1316 	c = *a;
1317 	if (isascii(c) && isupper(c))
1318 		c = tolower(c);
1319 	for (; lb-- >= la; b++)
1320 	{
1321 		if (*b != c && isascii(*b) && isupper(*b) && tolower(*b) != c)
1322 			continue;
1323 		if (strncasecmp(a, b, la) == 0)
1324 			return TRUE;
1325 	}
1326 	return FALSE;
1327 }
1328 /*
1329 **  CHECKFD012 -- check low numbered file descriptors
1330 **
1331 **	File descriptors 0, 1, and 2 should be open at all times.
1332 **	This routine verifies that, and fixes it if not true.
1333 **
1334 **	Parameters:
1335 **		where -- a tag printed if the assertion failed
1336 **
1337 **	Returns:
1338 **		none
1339 */
1340 
1341 checkfd012(where)
1342 	char *where;
1343 {
1344 #ifdef XDEBUG
1345 	register int i;
1346 	struct stat stbuf;
1347 
1348 	for (i = 0; i < 3; i++)
1349 	{
1350 		if (fstat(i, &stbuf) < 0 && errno != EOPNOTSUPP)
1351 		{
1352 			/* oops.... */
1353 			int fd;
1354 
1355 			syserr("%s: fd %d not open", where, i);
1356 			fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666);
1357 			if (fd != i)
1358 			{
1359 				(void) dup2(fd, i);
1360 				(void) close(fd);
1361 			}
1362 		}
1363 	}
1364 #endif /* XDEBUG */
1365 }
1366 /*
1367 **  PRINTOPENFDS -- print the open file descriptors (for debugging)
1368 **
1369 **	Parameters:
1370 **		logit -- if set, send output to syslog; otherwise
1371 **			print for debugging.
1372 **
1373 **	Returns:
1374 **		none.
1375 */
1376 
1377 #include <netdb.h>
1378 #include <arpa/inet.h>
1379 
1380 printopenfds(logit)
1381 	bool logit;
1382 {
1383 	register int fd;
1384 	extern int DtableSize;
1385 
1386 	for (fd = 0; fd < DtableSize; fd++)
1387 		dumpfd(fd, FALSE, logit);
1388 }
1389 /*
1390 **  DUMPFD -- dump a file descriptor
1391 **
1392 **	Parameters:
1393 **		fd -- the file descriptor to dump.
1394 **		printclosed -- if set, print a notification even if
1395 **			it is closed; otherwise print nothing.
1396 **		logit -- if set, send output to syslog instead of stdout.
1397 */
1398 
1399 dumpfd(fd, printclosed, logit)
1400 	int fd;
1401 	bool printclosed;
1402 	bool logit;
1403 {
1404 	register struct hostent *hp;
1405 	register char *p;
1406 	char *fmtstr;
1407 	struct sockaddr_in sin;
1408 	auto int slen;
1409 	struct stat st;
1410 	char buf[200];
1411 
1412 	p = buf;
1413 	sprintf(p, "%3d: ", fd);
1414 	p += strlen(p);
1415 
1416 	if (fstat(fd, &st) < 0)
1417 	{
1418 		if (printclosed || errno != EBADF)
1419 		{
1420 			sprintf(p, "CANNOT STAT (%s)", errstring(errno));
1421 			goto printit;
1422 		}
1423 		return;
1424 	}
1425 
1426 	slen = fcntl(fd, F_GETFL, NULL);
1427 	if (slen != -1)
1428 	{
1429 		sprintf(p, "fl=0x%x, ", slen);
1430 		p += strlen(p);
1431 	}
1432 
1433 	sprintf(p, "mode=%o: ", st.st_mode);
1434 	p += strlen(p);
1435 	switch (st.st_mode & S_IFMT)
1436 	{
1437 #ifdef S_IFSOCK
1438 	  case S_IFSOCK:
1439 		sprintf(p, "SOCK ");
1440 		p += strlen(p);
1441 		slen = sizeof sin;
1442 		if (getsockname(fd, (struct sockaddr *) &sin, &slen) < 0)
1443 			sprintf(p, "(badsock)");
1444 		else
1445 		{
1446 			hp = gethostbyaddr((char *) &sin.sin_addr,
1447 					   INADDRSZ, AF_INET);
1448 			sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr)
1449 						   : hp->h_name, ntohs(sin.sin_port));
1450 		}
1451 		p += strlen(p);
1452 		sprintf(p, "->");
1453 		p += strlen(p);
1454 		slen = sizeof sin;
1455 		if (getpeername(fd, (struct sockaddr *) &sin, &slen) < 0)
1456 			sprintf(p, "(badsock)");
1457 		else
1458 		{
1459 			hp = gethostbyaddr((char *) &sin.sin_addr,
1460 					   INADDRSZ, AF_INET);
1461 			sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr)
1462 						   : hp->h_name, ntohs(sin.sin_port));
1463 		}
1464 		break;
1465 #endif
1466 
1467 	  case S_IFCHR:
1468 		sprintf(p, "CHR: ");
1469 		p += strlen(p);
1470 		goto defprint;
1471 
1472 	  case S_IFBLK:
1473 		sprintf(p, "BLK: ");
1474 		p += strlen(p);
1475 		goto defprint;
1476 
1477 #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK)
1478 	  case S_IFIFO:
1479 		sprintf(p, "FIFO: ");
1480 		p += strlen(p);
1481 		goto defprint;
1482 #endif
1483 
1484 #ifdef S_IFDIR
1485 	  case S_IFDIR:
1486 		sprintf(p, "DIR: ");
1487 		p += strlen(p);
1488 		goto defprint;
1489 #endif
1490 
1491 #ifdef S_IFLNK
1492 	  case S_IFLNK:
1493 		sprintf(p, "LNK: ");
1494 		p += strlen(p);
1495 		goto defprint;
1496 #endif
1497 
1498 	  default:
1499 defprint:
1500 		if (sizeof st.st_size > sizeof (long))
1501 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%qd";
1502 		else
1503 			fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld";
1504 		sprintf(p, fmtstr,
1505 			major(st.st_dev), minor(st.st_dev), st.st_ino,
1506 			st.st_nlink, st.st_uid, st.st_gid, st.st_size);
1507 		break;
1508 	}
1509 
1510 printit:
1511 #ifdef LOG
1512 	if (logit)
1513 		syslog(LOG_DEBUG, "%s", buf);
1514 	else
1515 #endif
1516 		printf("%s\n", buf);
1517 }
1518 /*
1519 **  SHORTENSTRING -- return short version of a string
1520 **
1521 **	If the string is already short, just return it.  If it is too
1522 **	long, return the head and tail of the string.
1523 **
1524 **	Parameters:
1525 **		s -- the string to shorten.
1526 **		m -- the max length of the string.
1527 **
1528 **	Returns:
1529 **		Either s or a short version of s.
1530 */
1531 
1532 #ifndef MAXSHORTSTR
1533 # define MAXSHORTSTR	203
1534 #endif
1535 
1536 char *
1537 shortenstring(s, m)
1538 	register char *s;
1539 	int m;
1540 {
1541 	int l;
1542 	static char buf[MAXSHORTSTR + 1];
1543 
1544 	l = strlen(s);
1545 	if (l < m)
1546 		return s;
1547 	if (m > MAXSHORTSTR)
1548 		m = MAXSHORTSTR;
1549 	else if (m < 10)
1550 	{
1551 		if (m < 5)
1552 		{
1553 			strncpy(buf, s, m);
1554 			buf[m] = '\0';
1555 			return buf;
1556 		}
1557 		strncpy(buf, s, m - 3);
1558 		strcpy(buf + m - 3, "...");
1559 		return buf;
1560 	}
1561 	m = (m - 3) / 2;
1562 	strncpy(buf, s, m);
1563 	strcpy(buf + m, "...");
1564 	strcpy(buf + m + 3, s + l - m);
1565 	return buf;
1566 }
1567 /*
1568 **  GET_COLUMN  -- look up a Column in a line buffer
1569 **
1570 **	Parameters:
1571 **		line -- the raw text line to search.
1572 **		col -- the column number to fetch.
1573 **		delim -- the delimiter between columns.  If null,
1574 **			use white space.
1575 **		buf -- the output buffer.
1576 **
1577 **	Returns:
1578 **		buf if successful.
1579 **		NULL otherwise.
1580 */
1581 
1582 char *
1583 get_column(line, col, delim, buf)
1584 	char line[];
1585 	int col;
1586 	char delim;
1587 	char buf[];
1588 {
1589 	char *p;
1590 	char *begin, *end;
1591 	int i;
1592 	char delimbuf[3];
1593 
1594 	if (delim == '\0')
1595 		strcpy(delimbuf, "\t ");
1596 	else
1597 	{
1598 		delimbuf[0] = delim;
1599 		delimbuf[1] = '\0';
1600 	}
1601 
1602 	p = line;
1603 	if (*p == '\0')
1604 		return NULL;			/* line empty */
1605 	if (*p == delim && col == 0)
1606 		return NULL;			/* first column empty */
1607 
1608 	begin = line;
1609 
1610 	if (col == 0 && delim == '\0')
1611 	{
1612 		while (*begin && isspace(*begin))
1613 			begin++;
1614 	}
1615 
1616 	for (i = 0; i < col; i++)
1617 	{
1618 		if ((begin = strpbrk(begin, delimbuf)) == NULL)
1619 			return NULL;		/* no such column */
1620 		begin++;
1621 		if (delim == '\0')
1622 		{
1623 			while (*begin && isspace(*begin))
1624 				begin++;
1625 		}
1626 	}
1627 
1628 	end = strpbrk(begin, delimbuf);
1629 	if (end == NULL)
1630 	{
1631 		strcpy(buf, begin);
1632 	}
1633 	else
1634 	{
1635 		strncpy(buf, begin, end - begin);
1636 		buf[end - begin] = '\0';
1637 	}
1638 	return buf;
1639 }
1640 /*
1641 **  CLEANSTRCPY -- copy string keeping out bogus characters
1642 **
1643 **	Parameters:
1644 **		t -- "to" string.
1645 **		f -- "from" string.
1646 **		l -- length of space available in "to" string.
1647 **
1648 **	Returns:
1649 **		none.
1650 */
1651 
1652 void
1653 cleanstrcpy(t, f, l)
1654 	register char *t;
1655 	register char *f;
1656 	int l;
1657 {
1658 #ifdef LOG
1659 	/* check for newlines and log if necessary */
1660 	(void) denlstring(f, TRUE, TRUE);
1661 #endif
1662 
1663 	l--;
1664 	while (l > 0 && *f != '\0')
1665 	{
1666 		if (isascii(*f) &&
1667 		    (isalnum(*f) || strchr("!#$%&'*+-./^_`{|}~", *f) != NULL))
1668 		{
1669 			l--;
1670 			*t++ = *f;
1671 		}
1672 		f++;
1673 	}
1674 	*t = '\0';
1675 }
1676 /*
1677 **  DENLSTRING -- convert newlines in a string to spaces
1678 **
1679 **	Parameters:
1680 **		s -- the input string
1681 **		strict -- if set, don't permit continuation lines.
1682 **		logattacks -- if set, log attempted attacks.
1683 **
1684 **	Returns:
1685 **		A pointer to a version of the string with newlines
1686 **		mapped to spaces.  This should be copied.
1687 */
1688 
1689 char *
1690 denlstring(s, strict, logattacks)
1691 	char *s;
1692 	int strict;
1693 	int logattacks;
1694 {
1695 	register char *p;
1696 	int l;
1697 	static char *bp = NULL;
1698 	static int bl = 0;
1699 
1700 	p = s;
1701 	while ((p = strchr(p, '\n')) != NULL)
1702 		if (strict || (*++p != ' ' && *p != '\t'))
1703 			break;
1704 	if (p == NULL)
1705 		return s;
1706 
1707 	l = strlen(s) + 1;
1708 	if (bl < l)
1709 	{
1710 		/* allocate more space */
1711 		if (bp != NULL)
1712 			free(bp);
1713 		bp = xalloc(l);
1714 		bl = l;
1715 	}
1716 	strcpy(bp, s);
1717 	for (p = bp; (p = strchr(p, '\n')) != NULL; )
1718 		*p++ = ' ';
1719 
1720 /*
1721 #ifdef LOG
1722 	if (logattacks)
1723 	{
1724 		syslog(LOG_NOTICE, "POSSIBLE ATTACK from %s: newline in string \"%s\"",
1725 			RealHostName == NULL ? "[UNKNOWN]" : RealHostName,
1726 			shortenstring(bp, 80));
1727 	}
1728 #endif
1729 */
1730 
1731 	return bp;
1732 }
1733