allow limited 8-bit support; allow [TCP] as an alias for [IPC], even
[unix-history] / usr / src / usr.sbin / sendmail / src / util.c
index 260eadf..3f046f3 100644 (file)
@@ -1,15 +1,24 @@
+/*
+ * Copyright (c) 1983 Eric P. Allman
+ * Copyright (c) 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)util.c     5.23 (Berkeley) %G%";
+#endif /* not lint */
+
 # include <stdio.h>
 # include <pwd.h>
 # include <sys/types.h>
 # include <sys/stat.h>
 # include <sysexits.h>
 # include <errno.h>
 # include <stdio.h>
 # include <pwd.h>
 # include <sys/types.h>
 # include <sys/stat.h>
 # include <sysexits.h>
 # include <errno.h>
-# include <ctype.h>
 # include "sendmail.h"
 # include "conf.h"
 
 # include "sendmail.h"
 # include "conf.h"
 
-SCCSID(@(#)util.c      3.25            %G%);
-
 /*
 **  STRIPQUOTES -- Strip quotes & quote bits from a string.
 **
 /*
 **  STRIPQUOTES -- Strip quotes & quote bits from a string.
 **
@@ -50,6 +59,33 @@ stripquotes(s, qf)
        *q = '\0';
 }
 \f/*
        *q = '\0';
 }
 \f/*
+**  QSTRLEN -- give me the string length assuming 0200 bits add a char
+**
+**     Parameters:
+**             s -- the string to measure.
+**
+**     Reurns:
+**             The length of s, including space for backslash escapes.
+**
+**     Side Effects:
+**             none.
+*/
+
+qstrlen(s)
+       register char *s;
+{
+       register int l = 0;
+       register char c;
+
+       while ((c = *s++) != '\0')
+       {
+               if (bitset(0200, c))
+                       l++;
+               l++;
+       }
+       return (l);
+}
+\f/*
 **  CAPITALIZE -- return a copy of a string, properly capitalized.
 **
 **     Parameters:
 **  CAPITALIZE -- return a copy of a string, properly capitalized.
 **
 **     Parameters:
@@ -77,7 +113,8 @@ capitalize(s)
                        *p++ = *s++;
                if (*s == '\0')
                        break;
                        *p++ = *s++;
                if (*s == '\0')
                        break;
-               *p++ = toupper(*s++);
+               *p++ = toupper(*s);
+               s++;
                while (isalpha(*s))
                        *p++ = *s++;
        }
                while (isalpha(*s))
                        *p++ = *s++;
        }
@@ -106,41 +143,18 @@ xalloc(sz)
        register int sz;
 {
        register char *p;
        register int sz;
 {
        register char *p;
+       extern char *malloc();
 
 
-       p = malloc(sz);
+       p = malloc((unsigned) sz);
        if (p == NULL)
        {
                syserr("Out of memory!!");
        if (p == NULL)
        {
                syserr("Out of memory!!");
-               exit(EX_UNAVAILABLE);
+               abort();
+               /* exit(EX_UNAVAILABLE); */
        }
        return (p);
 }
 \f/*
        }
        return (p);
 }
 \f/*
-**  NEWSTR -- make copy of string.
-**
-**     Space is allocated for it using xalloc.
-**
-**     Parameters:
-**             string to copy.
-**
-**     Returns:
-**             pointer to new string.
-**
-**     Side Effects:
-**             none.
-*/
-
-char *
-newstr(s)
-       register char *s;
-{
-       register char *p;
-
-       p = xalloc(strlen(s) + 1);
-       (void) strcpy(p, s);
-       return (p);
-}
-\f/*
 **  COPYPLIST -- copy list of pointers.
 **
 **     This routine is the equivalent of newstr for lists of
 **  COPYPLIST -- copy list of pointers.
 **
 **     This routine is the equivalent of newstr for lists of
@@ -172,8 +186,8 @@ copyplist(list, copycont)
 
        vp++;
 
 
        vp++;
 
-       newvp = (char **) xalloc((vp - list) * sizeof *vp);
-       bmove((char *) list, (char *) newvp, (vp - list) * sizeof *vp);
+       newvp = (char **) xalloc((int) (vp - list) * sizeof *vp);
+       bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp);
 
        if (copycont)
        {
 
        if (copycont)
        {
@@ -196,18 +210,19 @@ copyplist(list, copycont)
 **             prints av.
 */
 
 **             prints av.
 */
 
-# ifdef DEBUG
 printav(av)
        register char **av;
 {
        while (*av != NULL)
        {
 printav(av)
        register char **av;
 {
        while (*av != NULL)
        {
-               printf("\t%08x=", *av);
+               if (tTd(0, 44))
+                       printf("\n\t%08x=", *av);
+               else
+                       (void) putchar(' ');
                xputs(*av++);
                xputs(*av++);
-               putchar('\n');
        }
        }
+       (void) putchar('\n');
 }
 }
-# endif DEBUG
 \f/*
 **  LOWER -- turn letter into lower case.
 **
 \f/*
 **  LOWER -- turn letter into lower case.
 **
@@ -225,9 +240,7 @@ char
 lower(c)
        register char c;
 {
 lower(c)
        register char c;
 {
-       if (isascii(c) && isupper(c))
-               c = c - 'A' + 'a';
-       return (c);
+       return(isascii(c) && isupper(c) ? tolower(c) : c);
 }
 \f/*
 **  XPUTS -- put string doing control escapes.
 }
 \f/*
 **  XPUTS -- put string doing control escapes.
@@ -242,29 +255,68 @@ lower(c)
 **             output to stdout
 */
 
 **             output to stdout
 */
 
-# ifdef DEBUG
 xputs(s)
        register char *s;
 {
        register char c;
 xputs(s)
        register char *s;
 {
        register char c;
+       register struct metamac *mp;
+       extern struct metamac MetaMacros[];
 
 
+       if (s == NULL)
+       {
+               printf("<null>");
+               return;
+       }
+       c = *s;
+       if (c == MATCHREPL && isdigit(s[1]) && s[2] == '\0')
+       {
+               printf("$%c", s[1]);
+               return;
+       }
+       for (mp = MetaMacros; mp->metaname != NULL; mp++)
+       {
+               if (mp->metaval == c)
+               {
+                       printf("$%c%s", mp->metaname, ++s);
+                       return;
+               }
+       }
+       (void) putchar('"');
        while ((c = *s++) != '\0')
        {
                if (!isascii(c))
                {
        while ((c = *s++) != '\0')
        {
                if (!isascii(c))
                {
-                       putchar('\\');
+                       (void) putchar('\\');
                        c &= 0177;
                }
                        c &= 0177;
                }
-               if (iscntrl(c))
+               if (c < 040 || c >= 0177)
                {
                {
-                       putchar('^');
-                       c |= 0100;
+                       switch (c)
+                       {
+                         case '\n':
+                               c = 'n';
+                               break;
+
+                         case '\r':
+                               c = 'r';
+                               break;
+
+                         case '\t':
+                               c = 't';
+                               break;
+
+                         default:
+                               (void) putchar('^');
+                               (void) putchar(c ^ 0100);
+                               continue;
+                       }
+                       (void) putchar('\\');
                }
                }
-               putchar(c);
+               (void) putchar(c);
        }
        }
+       (void) putchar('"');
        (void) fflush(stdout);
 }
        (void) fflush(stdout);
 }
-# endif DEBUG
 \f/*
 **  MAKELOWER -- Translate a line into lower case
 **
 \f/*
 **  MAKELOWER -- Translate a line into lower case
 **
@@ -291,57 +343,7 @@ makelower(p)
                return;
        for (; (c = *p) != '\0'; p++)
                if (isascii(c) && isupper(c))
                return;
        for (; (c = *p) != '\0'; p++)
                if (isascii(c) && isupper(c))
-                       *p = c - 'A' + 'a';
-}
-\f/*
-**  SAMEWORD -- return TRUE if the words are the same
-**
-**     Ignores case.
-**
-**     Parameters:
-**             a, b -- the words to compare.
-**
-**     Returns:
-**             TRUE if a & b match exactly (modulo case)
-**             FALSE otherwise.
-**
-**     Side Effects:
-**             none.
-*/
-
-bool
-sameword(a, b)
-       register char *a, *b;
-{
-       while (lower(*a) == lower(*b))
-       {
-               if (*a == '\0')
-                       return (TRUE);
-               a++;
-               b++;
-       }
-       return (FALSE);
-}
-\f/*
-**  CLEAR -- clear a block of memory
-**
-**     Parameters:
-**             p -- location to clear.
-**             l -- number of bytes to clear.
-**
-**     Returns:
-**             none.
-**
-**     Side Effects:
-**             none.
-*/
-
-clear(p, l)
-       register char *p;
-       register int l;
-{
-       while (l-- > 0)
-               *p++ = 0;
+                       *p = tolower(c);
 }
 \f/*
 **  FULLNAME -- extract full name from a passwd file entry.
 }
 \f/*
 **  FULLNAME -- extract full name from a passwd file entry.
@@ -408,6 +410,7 @@ safefile(fn, uid, mode)
        if (stat(fn, &stbuf) >= 0 && stbuf.st_uid == uid &&
            (stbuf.st_mode & mode) == mode)
                return (TRUE);
        if (stat(fn, &stbuf) >= 0 && stbuf.st_uid == uid &&
            (stbuf.st_mode & mode) == mode)
                return (TRUE);
+       errno = 0;
        return (FALSE);
 }
 \f/*
        return (FALSE);
 }
 \f/*
@@ -438,32 +441,13 @@ fixcrlf(line, stripnl)
        p = index(line, '\n');
        if (p == NULL)
                return;
        p = index(line, '\n');
        if (p == NULL)
                return;
-       if (p[-1] == '\r')
+       if (p > line && p[-1] == '\r')
                p--;
        if (!stripnl)
                *p++ = '\n';
        *p = '\0';
 }
 \f/*
                p--;
        if (!stripnl)
                *p++ = '\n';
        *p = '\0';
 }
 \f/*
-**  SYSLOG -- fake entry to fool lint
-*/
-
-# ifdef LOG
-# ifdef lint
-
-/*VARARGS2*/
-syslog(pri, fmt, args)
-       int pri;
-       char *fmt;
-{
-       pri = *fmt;
-       args = pri;
-       pri = args;
-}
-
-# endif lint
-# endif LOG
-\f/*
 **  DFOPEN -- determined file open
 **
 **     This routine has the semantics of fopen, except that it will
 **  DFOPEN -- determined file open
 **
 **     This routine has the semantics of fopen, except that it will
@@ -479,16 +463,18 @@ dfopen(filename, mode)
 {
        register int tries;
        register FILE *fp;
 {
        register int tries;
        register FILE *fp;
-       extern int errno;
 
        for (tries = 0; tries < 10; tries++)
        {
 
        for (tries = 0; tries < 10; tries++)
        {
-               sleep(10 * tries);
+               sleep((unsigned) (10 * tries));
                errno = 0;
                fp = fopen(filename, mode);
                errno = 0;
                fp = fopen(filename, mode);
-               if (fp != NULL || errno != ENFILE)
+               if (fp != NULL)
+                       break;
+               if (errno != ENFILE && errno != EINTR)
                        break;
        }
                        break;
        }
+       errno = 0;
        return (fp);
 }
 \f/*
        return (fp);
 }
 \f/*
@@ -500,7 +486,7 @@ dfopen(filename, mode)
 **     Parameters:
 **             l -- line to put.
 **             fp -- file to put it onto.
 **     Parameters:
 **             l -- line to put.
 **             fp -- file to put it onto.
-**             fullsmtp -- if set, obey strictest SMTP conventions.
+**             m -- the mailer used to control output.
 **
 **     Returns:
 **             none
 **
 **     Returns:
 **             none
@@ -509,15 +495,21 @@ dfopen(filename, mode)
 **             output of l to fp.
 */
 
 **             output of l to fp.
 */
 
-# define SMTPLINELIM   990     /* maximum line length */
-
-putline(l, fp, fullsmtp)
+putline(l, fp, m)
        register char *l;
        FILE *fp;
        register char *l;
        FILE *fp;
-       bool fullsmtp;
+       MAILER *m;
 {
        register char *p;
 {
        register char *p;
-       char svchar;
+       register char svchar;
+
+       /* strip out 0200 bits -- these can look like TELNET protocol */
+       if (bitnset(M_7BITS, m->m_flags))
+       {
+               for (p = l; svchar = *p; ++p)
+                       if (svchar & 0200)
+                               *p = svchar &~ 0200;
+       }
 
        do
        {
 
        do
        {
@@ -527,29 +519,29 @@ putline(l, fp, fullsmtp)
                        p = &l[strlen(l)];
 
                /* check for line overflow */
                        p = &l[strlen(l)];
 
                /* check for line overflow */
-               while (fullsmtp && (p - l) > SMTPLINELIM)
+               while (m->m_linelimit > 0 && (p - l) > m->m_linelimit)
                {
                {
-                       register char *q = &l[SMTPLINELIM - 1];
+                       register char *q = &l[m->m_linelimit - 1];
 
                        svchar = *q;
                        *q = '\0';
 
                        svchar = *q;
                        *q = '\0';
+                       if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
+                               (void) putc('.', fp);
                        fputs(l, fp);
                        fputs(l, fp);
-                       fputs("!\r\n", fp);
+                       (void) putc('!', fp);
+                       fputs(m->m_eol, fp);
                        *q = svchar;
                        l = q;
                }
 
                /* output last part */
                        *q = svchar;
                        l = q;
                }
 
                /* output last part */
-               svchar = *p;
-               *p = '\0';
-               fputs(l, fp);
-               if (fullsmtp)
-                       fputc('\r', fp);
-               fputc('\n', fp);
-               *p = svchar;
-               l = p;
+               if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
+                       (void) putc('.', fp);
+               for ( ; l < p; ++l)
+                       (void) putc(*l, fp);
+               fputs(m->m_eol, fp);
                if (*l == '\n')
                if (*l == '\n')
-                       l++;
+                       ++l;
        } while (l[0] != '\0');
 }
 \f/*
        } while (l[0] != '\0');
 }
 \f/*
@@ -578,11 +570,11 @@ xunlink(f)
        i = unlink(f);
 # ifdef LOG
        if (i < 0 && LogLevel > 21)
        i = unlink(f);
 # ifdef LOG
        if (i < 0 && LogLevel > 21)
-               syslog(LOG_DEBUG, "%s: unlink-fail %e");
+               syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
 # endif LOG
 }
 \f/*
 # endif LOG
 }
 \f/*
-**  SFGETS -- "safe" fgets -- times out.
+**  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
 **
 **     Parameters:
 **             buf -- place to put the input line.
 **
 **     Parameters:
 **             buf -- place to put the input line.
@@ -590,14 +582,15 @@ xunlink(f)
 **             fp -- file to read from.
 **
 **     Returns:
 **             fp -- file to read from.
 **
 **     Returns:
-**             NULL on error (including timeout).
+**             NULL on error (including timeout).  This will also leave
+**                     buf containing a null string.
 **             buf otherwise.
 **
 **     Side Effects:
 **             none.
 */
 
 **             buf otherwise.
 **
 **     Side Effects:
 **             none.
 */
 
-jmp_buf        TimeoFrame;
+static jmp_buf CtxReadTimeout;
 
 char *
 sfgets(buf, siz, fp)
 
 char *
 sfgets(buf, siz, fp)
@@ -605,22 +598,58 @@ sfgets(buf, siz, fp)
        int siz;
        FILE *fp;
 {
        int siz;
        FILE *fp;
 {
-       register EVENT *ev;
+       register EVENT *ev = NULL;
        register char *p;
        register char *p;
-       extern readtimeout();
+       static int readtimeout();
 
 
-       ev = setevent(ReadTimeout, readtimeout, 0);
-       if (setjmp(TimeoFrame) != 0)
-               return (NULL);
-       p = fgets(buf, siz, fp);
+       /* set the timeout */
+       if (ReadTimeout != 0)
+       {
+               if (setjmp(CtxReadTimeout) != 0)
+               {
+# ifdef LOG
+                       syslog(LOG_NOTICE,
+                           "timeout waiting for input from %s\n",
+                           RealHostName? RealHostName: "local");
+# endif
+                       errno = 0;
+                       usrerr("451 timeout waiting for input");
+                       buf[0] = '\0';
+                       return (NULL);
+               }
+               ev = setevent((time_t) ReadTimeout, readtimeout, 0);
+       }
+
+       /* try to read */
+       p = NULL;
+       while (p == NULL && !feof(fp) && !ferror(fp))
+       {
+               errno = 0;
+               p = fgets(buf, siz, fp);
+               if (errno == EINTR)
+                       clearerr(fp);
+       }
+
+       /* clear the event if it has not sprung */
        clrevent(ev);
        clrevent(ev);
-       return (p);
+
+       /* clean up the books and exit */
+       LineNumber++;
+       if (p == NULL)
+       {
+               buf[0] = '\0';
+               return (NULL);
+       }
+       if (!EightBit)
+               for (p = buf; *p != '\0'; p++)
+                       *p &= ~0200;
+       return (buf);
 }
 
 static
 readtimeout()
 {
 }
 
 static
 readtimeout()
 {
-       longjmp(TimeoFrame, 1);
+       longjmp(CtxReadTimeout, 1);
 }
 \f/*
 **  FGETFOLDED -- like fgets, but know about folded lines.
 }
 \f/*
 **  FGETFOLDED -- like fgets, but know about folded lines.
@@ -649,18 +678,187 @@ fgetfolded(buf, n, f)
        register int i;
 
        n--;
        register int i;
 
        n--;
-       while (fgets(p, n, f) != NULL)
+       while ((i = getc(f)) != EOF)
        {
        {
-               fixcrlf(p, TRUE);
-               i = fgetc(f);
-               if (i != EOF)
-                       ungetc(i, f);
-               if (i != ' ' && i != '\t')
-                       return (buf);
-               i = strlen(p);
-               p += i;
-               *p++ = '\n';
-               n -= i + 1;
+               if (i == '\r')
+               {
+                       i = getc(f);
+                       if (i != '\n')
+                       {
+                               if (i != EOF)
+                                       (void) ungetc(i, f);
+                               i = '\r';
+                       }
+               }
+               if (--n > 0)
+                       *p++ = i;
+               if (i == '\n')
+               {
+                       LineNumber++;
+                       i = getc(f);
+                       if (i != EOF)
+                               (void) ungetc(i, f);
+                       if (i != ' ' && i != '\t')
+                       {
+                               *--p = '\0';
+                               if (!EightBit)
+                               {
+                                       /* headers always have to be 7-bit */
+                                       for (p = buf; (i = *p) != '\0'; *p++)
+                                               if (bitset(0200, i))
+                                                       *p = i & ~0200;
+                               }
+                               return (buf);
+                       }
+               }
        }
        return (NULL);
 }
        }
        return (NULL);
 }
+\f/*
+**  CURTIME -- return current time.
+**
+**     Parameters:
+**             none.
+**
+**     Returns:
+**             the current time.
+**
+**     Side Effects:
+**             none.
+*/
+
+time_t
+curtime()
+{
+       auto time_t t;
+
+       (void) time(&t);
+       return (t);
+}
+\f/*
+**  ATOBOOL -- convert a string representation to boolean.
+**
+**     Defaults to "TRUE"
+**
+**     Parameters:
+**             s -- string to convert.  Takes "tTyY" as true,
+**                     others as false.
+**
+**     Returns:
+**             A boolean representation of the string.
+**
+**     Side Effects:
+**             none.
+*/
+
+bool
+atobool(s)
+       register char *s;
+{
+       if (*s == '\0' || index("tTyY", *s) != NULL)
+               return (TRUE);
+       return (FALSE);
+}
+\f/*
+**  ATOOCT -- convert a string representation to octal.
+**
+**     Parameters:
+**             s -- string to convert.
+**
+**     Returns:
+**             An integer representing the string interpreted as an
+**             octal number.
+**
+**     Side Effects:
+**             none.
+*/
+
+atooct(s)
+       register char *s;
+{
+       register int i = 0;
+
+       while (*s >= '0' && *s <= '7')
+               i = (i << 3) | (*s++ - '0');
+       return (i);
+}
+\f/*
+**  WAITFOR -- wait for a particular process id.
+**
+**     Parameters:
+**             pid -- process id to wait for.
+**
+**     Returns:
+**             status of pid.
+**             -1 if pid never shows up.
+**
+**     Side Effects:
+**             none.
+*/
+
+waitfor(pid)
+       int pid;
+{
+       auto int st;
+       int i;
+
+       do
+       {
+               errno = 0;
+               i = wait(&st);
+       } while ((i >= 0 || errno == EINTR) && i != pid);
+       if (i < 0)
+               st = -1;
+       return (st);
+}
+\f/*
+**  BITINTERSECT -- tell if two bitmaps intersect
+**
+**     Parameters:
+**             a, b -- the bitmaps in question
+**
+**     Returns:
+**             TRUE if they have a non-null intersection
+**             FALSE otherwise
+**
+**     Side Effects:
+**             none.
+*/
+
+bool
+bitintersect(a, b)
+       BITMAP a;
+       BITMAP b;
+{
+       int i;
+
+       for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
+               if ((a[i] & b[i]) != 0)
+                       return (TRUE);
+       return (FALSE);
+}
+\f/*
+**  BITZEROP -- tell if a bitmap is all zero
+**
+**     Parameters:
+**             map -- the bit map to check
+**
+**     Returns:
+**             TRUE if map is all zero.
+**             FALSE if there are any bits set in map.
+**
+**     Side Effects:
+**             none.
+*/
+
+bool
+bitzerop(map)
+       BITMAP map;
+{
+       int i;
+
+       for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
+               if (map[i] != 0)
+                       return (FALSE);
+       return (TRUE);
+}