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