xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 52050)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988 Regents of the University of California.
4  * All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)util.c	5.22 (Berkeley) 12/21/91";
11 #endif /* not lint */
12 
13 # include <stdio.h>
14 # include <sys/types.h>
15 # include <sys/stat.h>
16 # include <sysexits.h>
17 # include <errno.h>
18 # include "sendmail.h"
19 
20 /*
21 **  STRIPQUOTES -- Strip quotes & quote bits from a string.
22 **
23 **	Runs through a string and strips off unquoted quote
24 **	characters and quote bits.  This is done in place.
25 **
26 **	Parameters:
27 **		s -- the string to strip.
28 **		qf -- if set, remove actual `` " '' characters
29 **			as well as the quote bits.
30 **
31 **	Returns:
32 **		none.
33 **
34 **	Side Effects:
35 **		none.
36 **
37 **	Called By:
38 **		deliver
39 */
40 
41 stripquotes(s, qf)
42 	char *s;
43 	bool qf;
44 {
45 	register char *p;
46 	register char *q;
47 	register char c;
48 
49 	if (s == NULL)
50 		return;
51 
52 	for (p = q = s; (c = *p++) != '\0'; )
53 	{
54 		if (c != '"' || !qf)
55 			*q++ = c & 0177;
56 	}
57 	*q = '\0';
58 }
59 /*
60 **  QSTRLEN -- give me the string length assuming 0200 bits add a char
61 **
62 **	Parameters:
63 **		s -- the string to measure.
64 **
65 **	Reurns:
66 **		The length of s, including space for backslash escapes.
67 **
68 **	Side Effects:
69 **		none.
70 */
71 
72 qstrlen(s)
73 	register char *s;
74 {
75 	register int l = 0;
76 	register char c;
77 
78 	while ((c = *s++) != '\0')
79 	{
80 		if (bitset(0200, c))
81 			l++;
82 		l++;
83 	}
84 	return (l);
85 }
86 /*
87 **  CAPITALIZE -- return a copy of a string, properly capitalized.
88 **
89 **	Parameters:
90 **		s -- the string to capitalize.
91 **
92 **	Returns:
93 **		a pointer to a properly capitalized string.
94 **
95 **	Side Effects:
96 **		none.
97 */
98 
99 char *
100 capitalize(s)
101 	register char *s;
102 {
103 	static char buf[50];
104 	register char *p;
105 
106 	p = buf;
107 
108 	for (;;)
109 	{
110 		while (!isalpha(*s) && *s != '\0')
111 			*p++ = *s++;
112 		if (*s == '\0')
113 			break;
114 		*p++ = toupper(*s);
115 		s++;
116 		while (isalpha(*s))
117 			*p++ = *s++;
118 	}
119 
120 	*p = '\0';
121 	return (buf);
122 }
123 /*
124 **  XALLOC -- Allocate memory and bitch wildly on failure.
125 **
126 **	THIS IS A CLUDGE.  This should be made to give a proper
127 **	error -- but after all, what can we do?
128 **
129 **	Parameters:
130 **		sz -- size of area to allocate.
131 **
132 **	Returns:
133 **		pointer to data region.
134 **
135 **	Side Effects:
136 **		Memory is allocated.
137 */
138 
139 char *
140 xalloc(sz)
141 	register int sz;
142 {
143 	register char *p;
144 	extern char *malloc();
145 
146 	p = malloc((unsigned) sz);
147 	if (p == NULL)
148 	{
149 		syserr("Out of memory!!");
150 		abort();
151 		/* exit(EX_UNAVAILABLE); */
152 	}
153 	return (p);
154 }
155 /*
156 **  COPYPLIST -- copy list of pointers.
157 **
158 **	This routine is the equivalent of newstr for lists of
159 **	pointers.
160 **
161 **	Parameters:
162 **		list -- list of pointers to copy.
163 **			Must be NULL terminated.
164 **		copycont -- if TRUE, copy the contents of the vector
165 **			(which must be a string) also.
166 **
167 **	Returns:
168 **		a copy of 'list'.
169 **
170 **	Side Effects:
171 **		none.
172 */
173 
174 char **
175 copyplist(list, copycont)
176 	char **list;
177 	bool copycont;
178 {
179 	register char **vp;
180 	register char **newvp;
181 
182 	for (vp = list; *vp != NULL; vp++)
183 		continue;
184 
185 	vp++;
186 
187 	newvp = (char **) xalloc((int) (vp - list) * sizeof *vp);
188 	bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp);
189 
190 	if (copycont)
191 	{
192 		for (vp = newvp; *vp != NULL; vp++)
193 			*vp = newstr(*vp);
194 	}
195 
196 	return (newvp);
197 }
198 /*
199 **  PRINTAV -- print argument vector.
200 **
201 **	Parameters:
202 **		av -- argument vector.
203 **
204 **	Returns:
205 **		none.
206 **
207 **	Side Effects:
208 **		prints av.
209 */
210 
211 printav(av)
212 	register char **av;
213 {
214 	while (*av != NULL)
215 	{
216 		if (tTd(0, 44))
217 			printf("\n\t%08x=", *av);
218 		else
219 			(void) putchar(' ');
220 		xputs(*av++);
221 	}
222 	(void) putchar('\n');
223 }
224 /*
225 **  LOWER -- turn letter into lower case.
226 **
227 **	Parameters:
228 **		c -- character to turn into lower case.
229 **
230 **	Returns:
231 **		c, in lower case.
232 **
233 **	Side Effects:
234 **		none.
235 */
236 
237 char
238 lower(c)
239 	register char c;
240 {
241 	return(isascii(c) && isupper(c) ? tolower(c) : c);
242 }
243 /*
244 **  XPUTS -- put string doing control escapes.
245 **
246 **	Parameters:
247 **		s -- string to put.
248 **
249 **	Returns:
250 **		none.
251 **
252 **	Side Effects:
253 **		output to stdout
254 */
255 
256 xputs(s)
257 	register char *s;
258 {
259 	register char c;
260 	register struct metamac *mp;
261 	extern struct metamac MetaMacros[];
262 
263 	if (s == NULL)
264 	{
265 		printf("<null>");
266 		return;
267 	}
268 	c = *s;
269 	if (c == MATCHREPL && isdigit(s[1]) && s[2] == '\0')
270 	{
271 		printf("$%c", s[1]);
272 		return;
273 	}
274 	for (mp = MetaMacros; mp->metaname != NULL; mp++)
275 	{
276 		if (mp->metaval == c)
277 		{
278 			printf("$%c%s", mp->metaname, ++s);
279 			return;
280 		}
281 	}
282 	(void) putchar('"');
283 	while ((c = *s++) != '\0')
284 	{
285 		if (!isascii(c))
286 		{
287 			(void) putchar('\\');
288 			c &= 0177;
289 		}
290 		if (c < 040 || c >= 0177)
291 		{
292 			switch (c)
293 			{
294 			  case '\n':
295 				c = 'n';
296 				break;
297 
298 			  case '\r':
299 				c = 'r';
300 				break;
301 
302 			  case '\t':
303 				c = 't';
304 				break;
305 
306 			  default:
307 				(void) putchar('^');
308 				(void) putchar(c ^ 0100);
309 				continue;
310 			}
311 			(void) putchar('\\');
312 		}
313 		(void) putchar(c);
314 	}
315 	(void) putchar('"');
316 	(void) fflush(stdout);
317 }
318 /*
319 **  MAKELOWER -- Translate a line into lower case
320 **
321 **	Parameters:
322 **		p -- the string to translate.  If NULL, return is
323 **			immediate.
324 **
325 **	Returns:
326 **		none.
327 **
328 **	Side Effects:
329 **		String pointed to by p is translated to lower case.
330 **
331 **	Called By:
332 **		parse
333 */
334 
335 makelower(p)
336 	register char *p;
337 {
338 	register char c;
339 
340 	if (p == NULL)
341 		return;
342 	for (; (c = *p) != '\0'; p++)
343 		if (isascii(c) && isupper(c))
344 			*p = tolower(c);
345 }
346 /*
347 **  BUILDFNAME -- build full name from gecos style entry.
348 **
349 **	This routine interprets the strange entry that would appear
350 **	in the GECOS field of the password file.
351 **
352 **	Parameters:
353 **		p -- name to build.
354 **		login -- the login name of this user (for &).
355 **		buf -- place to put the result.
356 **
357 **	Returns:
358 **		none.
359 **
360 **	Side Effects:
361 **		none.
362 */
363 
364 buildfname(p, login, buf)
365 	register char *p;
366 	char *login;
367 	char *buf;
368 {
369 	register char *bp = buf;
370 
371 	if (*p == '*')
372 		p++;
373 	while (*p != '\0' && *p != ',' && *p != ';' && *p != '%')
374 	{
375 		if (*p == '&')
376 		{
377 			(void) strcpy(bp, login);
378 			*bp = toupper(*bp);
379 			while (*bp != '\0')
380 				bp++;
381 			p++;
382 		}
383 		else
384 			*bp++ = *p++;
385 	}
386 	*bp = '\0';
387 }
388 /*
389 **  SAFEFILE -- return true if a file exists and is safe for a user.
390 **
391 **	Parameters:
392 **		fn -- filename to check.
393 **		uid -- uid to compare against.
394 **		mode -- mode bits that must match.
395 **
396 **	Returns:
397 **		TRUE if fn exists, is owned by uid, and matches mode.
398 **		FALSE otherwise.
399 **
400 **	Side Effects:
401 **		none.
402 */
403 
404 bool
405 safefile(fn, uid, mode)
406 	char *fn;
407 	int uid;
408 	int mode;
409 {
410 	struct stat stbuf;
411 
412 	if (stat(fn, &stbuf) >= 0 && stbuf.st_uid == uid &&
413 	    (stbuf.st_mode & mode) == mode)
414 		return (TRUE);
415 	errno = 0;
416 	return (FALSE);
417 }
418 /*
419 **  FIXCRLF -- fix <CR><LF> in line.
420 **
421 **	Looks for the <CR><LF> combination and turns it into the
422 **	UNIX canonical <NL> character.  It only takes one line,
423 **	i.e., it is assumed that the first <NL> found is the end
424 **	of the line.
425 **
426 **	Parameters:
427 **		line -- the line to fix.
428 **		stripnl -- if true, strip the newline also.
429 **
430 **	Returns:
431 **		none.
432 **
433 **	Side Effects:
434 **		line is changed in place.
435 */
436 
437 fixcrlf(line, stripnl)
438 	char *line;
439 	bool stripnl;
440 {
441 	register char *p;
442 
443 	p = index(line, '\n');
444 	if (p == NULL)
445 		return;
446 	if (p > line && p[-1] == '\r')
447 		p--;
448 	if (!stripnl)
449 		*p++ = '\n';
450 	*p = '\0';
451 }
452 /*
453 **  DFOPEN -- determined file open
454 **
455 **	This routine has the semantics of fopen, except that it will
456 **	keep trying a few times to make this happen.  The idea is that
457 **	on very loaded systems, we may run out of resources (inodes,
458 **	whatever), so this tries to get around it.
459 */
460 
461 FILE *
462 dfopen(filename, mode)
463 	char *filename;
464 	char *mode;
465 {
466 	register int tries;
467 	register FILE *fp;
468 
469 	for (tries = 0; tries < 10; tries++)
470 	{
471 		sleep((unsigned) (10 * tries));
472 		errno = 0;
473 		fp = fopen(filename, mode);
474 		if (fp != NULL)
475 			break;
476 		if (errno != ENFILE && errno != EINTR)
477 			break;
478 	}
479 	errno = 0;
480 	return (fp);
481 }
482 /*
483 **  PUTLINE -- put a line like fputs obeying SMTP conventions
484 **
485 **	This routine always guarantees outputing a newline (or CRLF,
486 **	as appropriate) at the end of the string.
487 **
488 **	Parameters:
489 **		l -- line to put.
490 **		fp -- file to put it onto.
491 **		m -- the mailer used to control output.
492 **
493 **	Returns:
494 **		none
495 **
496 **	Side Effects:
497 **		output of l to fp.
498 */
499 
500 # define SMTPLINELIM	990	/* maximum line length */
501 
502 putline(l, fp, m)
503 	register char *l;
504 	FILE *fp;
505 	MAILER *m;
506 {
507 	register char *p;
508 	register char svchar;
509 
510 	/* strip out 0200 bits -- these can look like TELNET protocol */
511 	if (bitnset(M_LIMITS, m->m_flags))
512 	{
513 		for (p = l; svchar = *p; ++p)
514 			if (svchar & 0200)
515 				*p = svchar &~ 0200;
516 	}
517 
518 	do
519 	{
520 		/* find the end of the line */
521 		p = index(l, '\n');
522 		if (p == NULL)
523 			p = &l[strlen(l)];
524 
525 		/* check for line overflow */
526 		while ((p - l) > SMTPLINELIM && bitnset(M_LIMITS, m->m_flags))
527 		{
528 			register char *q = &l[SMTPLINELIM - 1];
529 
530 			svchar = *q;
531 			*q = '\0';
532 			if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
533 				(void) putc('.', fp);
534 			fputs(l, fp);
535 			(void) putc('!', fp);
536 			fputs(m->m_eol, fp);
537 			*q = svchar;
538 			l = q;
539 		}
540 
541 		/* output last part */
542 		if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
543 			(void) putc('.', fp);
544 		for ( ; l < p; ++l)
545 			(void) putc(*l, fp);
546 		fputs(m->m_eol, fp);
547 		if (*l == '\n')
548 			++l;
549 	} while (l[0] != '\0');
550 }
551 /*
552 **  XUNLINK -- unlink a file, doing logging as appropriate.
553 **
554 **	Parameters:
555 **		f -- name of file to unlink.
556 **
557 **	Returns:
558 **		none.
559 **
560 **	Side Effects:
561 **		f is unlinked.
562 */
563 
564 xunlink(f)
565 	char *f;
566 {
567 	register int i;
568 
569 # ifdef LOG
570 	if (LogLevel > 20)
571 		syslog(LOG_DEBUG, "%s: unlink %s\n", CurEnv->e_id, f);
572 # endif LOG
573 
574 	i = unlink(f);
575 # ifdef LOG
576 	if (i < 0 && LogLevel > 21)
577 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
578 # endif LOG
579 }
580 /*
581 **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
582 **
583 **	Parameters:
584 **		buf -- place to put the input line.
585 **		siz -- size of buf.
586 **		fp -- file to read from.
587 **
588 **	Returns:
589 **		NULL on error (including timeout).  This will also leave
590 **			buf containing a null string.
591 **		buf otherwise.
592 **
593 **	Side Effects:
594 **		none.
595 */
596 
597 static jmp_buf	CtxReadTimeout;
598 
599 char *
600 sfgets(buf, siz, fp)
601 	char *buf;
602 	int siz;
603 	FILE *fp;
604 {
605 	register EVENT *ev = NULL;
606 	register char *p;
607 	static int readtimeout();
608 
609 	/* set the timeout */
610 	if (ReadTimeout != 0)
611 	{
612 		if (setjmp(CtxReadTimeout) != 0)
613 		{
614 # ifdef LOG
615 			syslog(LOG_NOTICE,
616 			    "timeout waiting for input from %s\n",
617 			    RealHostName? RealHostName: "local");
618 # endif
619 			errno = 0;
620 			usrerr("451 timeout waiting for input");
621 			buf[0] = '\0';
622 			return (NULL);
623 		}
624 		ev = setevent((time_t) ReadTimeout, readtimeout, 0);
625 	}
626 
627 	/* try to read */
628 	p = NULL;
629 	while (p == NULL && !feof(fp) && !ferror(fp))
630 	{
631 		errno = 0;
632 		p = fgets(buf, siz, fp);
633 		if (errno == EINTR)
634 			clearerr(fp);
635 	}
636 
637 	/* clear the event if it has not sprung */
638 	clrevent(ev);
639 
640 	/* clean up the books and exit */
641 	LineNumber++;
642 	if (p == NULL)
643 	{
644 		buf[0] = '\0';
645 		return (NULL);
646 	}
647 	for (p = buf; *p != '\0'; p++)
648 		*p &= ~0200;
649 	return (buf);
650 }
651 
652 static
653 readtimeout()
654 {
655 	longjmp(CtxReadTimeout, 1);
656 }
657 /*
658 **  FGETFOLDED -- like fgets, but know about folded lines.
659 **
660 **	Parameters:
661 **		buf -- place to put result.
662 **		n -- bytes available.
663 **		f -- file to read from.
664 **
665 **	Returns:
666 **		buf on success, NULL on error or EOF.
667 **
668 **	Side Effects:
669 **		buf gets lines from f, with continuation lines (lines
670 **		with leading white space) appended.  CRLF's are mapped
671 **		into single newlines.  Any trailing NL is stripped.
672 */
673 
674 char *
675 fgetfolded(buf, n, f)
676 	char *buf;
677 	register int n;
678 	FILE *f;
679 {
680 	register char *p = buf;
681 	register int i;
682 
683 	n--;
684 	while ((i = getc(f)) != EOF)
685 	{
686 		if (i == '\r')
687 		{
688 			i = getc(f);
689 			if (i != '\n')
690 			{
691 				if (i != EOF)
692 					(void) ungetc(i, f);
693 				i = '\r';
694 			}
695 		}
696 		if (--n > 0)
697 			*p++ = i;
698 		if (i == '\n')
699 		{
700 			LineNumber++;
701 			i = getc(f);
702 			if (i != EOF)
703 				(void) ungetc(i, f);
704 			if (i != ' ' && i != '\t')
705 			{
706 				*--p = '\0';
707 				return (buf);
708 			}
709 		}
710 	}
711 	return (NULL);
712 }
713 /*
714 **  CURTIME -- return current time.
715 **
716 **	Parameters:
717 **		none.
718 **
719 **	Returns:
720 **		the current time.
721 **
722 **	Side Effects:
723 **		none.
724 */
725 
726 time_t
727 curtime()
728 {
729 	auto time_t t;
730 
731 	(void) time(&t);
732 	return (t);
733 }
734 /*
735 **  ATOBOOL -- convert a string representation to boolean.
736 **
737 **	Defaults to "TRUE"
738 **
739 **	Parameters:
740 **		s -- string to convert.  Takes "tTyY" as true,
741 **			others as false.
742 **
743 **	Returns:
744 **		A boolean representation of the string.
745 **
746 **	Side Effects:
747 **		none.
748 */
749 
750 bool
751 atobool(s)
752 	register char *s;
753 {
754 	if (*s == '\0' || index("tTyY", *s) != NULL)
755 		return (TRUE);
756 	return (FALSE);
757 }
758 /*
759 **  ATOOCT -- convert a string representation to octal.
760 **
761 **	Parameters:
762 **		s -- string to convert.
763 **
764 **	Returns:
765 **		An integer representing the string interpreted as an
766 **		octal number.
767 **
768 **	Side Effects:
769 **		none.
770 */
771 
772 atooct(s)
773 	register char *s;
774 {
775 	register int i = 0;
776 
777 	while (*s >= '0' && *s <= '7')
778 		i = (i << 3) | (*s++ - '0');
779 	return (i);
780 }
781 /*
782 **  WAITFOR -- wait for a particular process id.
783 **
784 **	Parameters:
785 **		pid -- process id to wait for.
786 **
787 **	Returns:
788 **		status of pid.
789 **		-1 if pid never shows up.
790 **
791 **	Side Effects:
792 **		none.
793 */
794 
795 waitfor(pid)
796 	int pid;
797 {
798 	auto int st;
799 	int i;
800 
801 	do
802 	{
803 		errno = 0;
804 		i = wait(&st);
805 	} while ((i >= 0 || errno == EINTR) && i != pid);
806 	if (i < 0)
807 		st = -1;
808 	return (st);
809 }
810 /*
811 **  BITINTERSECT -- tell if two bitmaps intersect
812 **
813 **	Parameters:
814 **		a, b -- the bitmaps in question
815 **
816 **	Returns:
817 **		TRUE if they have a non-null intersection
818 **		FALSE otherwise
819 **
820 **	Side Effects:
821 **		none.
822 */
823 
824 bool
825 bitintersect(a, b)
826 	BITMAP a;
827 	BITMAP b;
828 {
829 	int i;
830 
831 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
832 		if ((a[i] & b[i]) != 0)
833 			return (TRUE);
834 	return (FALSE);
835 }
836 /*
837 **  BITZEROP -- tell if a bitmap is all zero
838 **
839 **	Parameters:
840 **		map -- the bit map to check
841 **
842 **	Returns:
843 **		TRUE if map is all zero.
844 **		FALSE if there are any bits set in map.
845 **
846 **	Side Effects:
847 **		none.
848 */
849 
850 bool
851 bitzerop(map)
852 	BITMAP map;
853 {
854 	int i;
855 
856 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
857 		if (map[i] != 0)
858 			return (FALSE);
859 	return (TRUE);
860 }
861