allow limited 8-bit support; allow [TCP] as an alias for [IPC], even
[unix-history] / usr / src / usr.sbin / sendmail / src / util.c
index ce52d4c..3f046f3 100644 (file)
@@ -1,16 +1,14 @@
 /*
 /*
-**  Sendmail
-**  Copyright (c) 1983  Eric P. Allman
-**  Berkeley, California
-**
-**  Copyright (c) 1983 Regents of the University of California.
-**  All rights reserved.  The Berkeley software License Agreement
-**  specifies the terms and conditions for redistribution.
-*/
+ * Copyright (c) 1983 Eric P. Allman
+ * Copyright (c) 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ */
 
 #ifndef lint
 
 #ifndef lint
-static char    SccsId[] = "@(#)util.c  5.8 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)util.c     5.23 (Berkeley) %G%";
+#endif /* not lint */
 
 # include <stdio.h>
 # include <pwd.h>
 
 # include <stdio.h>
 # include <pwd.h>
@@ -18,7 +16,6 @@ static char   SccsId[] = "@(#)util.c  5.8 (Berkeley) %G%";
 # include <sys/stat.h>
 # include <sysexits.h>
 # include <errno.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"
 
@@ -116,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++;
        }
@@ -242,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.
@@ -263,12 +259,28 @@ xputs(s)
        register char *s;
 {
        register char c;
        register char *s;
 {
        register char c;
+       register struct metamac *mp;
+       extern struct metamac MetaMacros[];
 
        if (s == NULL)
        {
                printf("<null>");
                return;
        }
 
        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')
        {
        (void) putchar('"');
        while ((c = *s++) != '\0')
        {
@@ -279,8 +291,26 @@ xputs(s)
                }
                if (c < 040 || c >= 0177)
                {
                }
                if (c < 040 || c >= 0177)
                {
-                       (void) 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('\\');
                }
                (void) putchar(c);
        }
                }
                (void) putchar(c);
        }
@@ -313,40 +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;
-{
-       char ca, cb;
-
-       do
-       {
-               ca = *a++;
-               cb = *b++;
-               if (isascii(ca) && isupper(ca))
-                       ca = ca - 'A' + 'a';
-               if (isascii(cb) && isupper(cb))
-                       cb = cb - 'A' + 'a';
-       } while (ca != '\0' && ca == cb);
-       return (ca == cb);
+                       *p = tolower(c);
 }
 \f/*
 **  FULLNAME -- extract full name from a passwd file entry.
 }
 \f/*
 **  FULLNAME -- extract full name from a passwd file entry.
@@ -444,7 +441,7 @@ 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--;
        if (!stripnl)
                *p++ = '\n';
@@ -498,22 +495,20 @@ dfopen(filename, mode)
 **             output of l to fp.
 */
 
 **             output of l to fp.
 */
 
-# define SMTPLINELIM   990     /* maximum line length */
-
 putline(l, fp, m)
        register char *l;
        FILE *fp;
        MAILER *m;
 {
        register char *p;
 putline(l, fp, m)
        register char *l;
        FILE *fp;
        MAILER *m;
 {
        register char *p;
-       char svchar;
+       register char svchar;
 
        /* strip out 0200 bits -- these can look like TELNET protocol */
 
        /* strip out 0200 bits -- these can look like TELNET protocol */
-       if (bitnset(M_LIMITS, m->m_flags))
+       if (bitnset(M_7BITS, m->m_flags))
        {
        {
-               p = l;
-               while ((*p++ &= ~0200) != 0)
-                       continue;
+               for (p = l; svchar = *p; ++p)
+                       if (svchar & 0200)
+                               *p = svchar &~ 0200;
        }
 
        do
        }
 
        do
@@ -524,9 +519,9 @@ putline(l, fp, m)
                        p = &l[strlen(l)];
 
                /* check for line overflow */
                        p = &l[strlen(l)];
 
                /* check for line overflow */
-               while ((p - l) > SMTPLINELIM && bitnset(M_LIMITS, m->m_flags))
+               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';
@@ -540,16 +535,13 @@ putline(l, fp, m)
                }
 
                /* output last part */
                }
 
                /* output last part */
-               svchar = *p;
-               *p = '\0';
                if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
                        (void) putc('.', fp);
                if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
                        (void) putc('.', fp);
-               fputs(l, fp);
+               for ( ; l < p; ++l)
+                       (void) putc(*l, fp);
                fputs(m->m_eol, fp);
                fputs(m->m_eol, fp);
-               *p = svchar;
-               l = p;
                if (*l == '\n')
                if (*l == '\n')
-                       l++;
+                       ++l;
        } while (l[0] != '\0');
 }
 \f/*
        } while (l[0] != '\0');
 }
 \f/*
@@ -600,10 +592,6 @@ xunlink(f)
 
 static jmp_buf CtxReadTimeout;
 
 
 static jmp_buf CtxReadTimeout;
 
-#ifndef ETIMEDOUT
-#define ETIMEDOUT      EINTR
-#endif
-
 char *
 sfgets(buf, siz, fp)
        char *buf;
 char *
 sfgets(buf, siz, fp)
        char *buf;
@@ -612,15 +600,20 @@ sfgets(buf, siz, fp)
 {
        register EVENT *ev = NULL;
        register char *p;
 {
        register EVENT *ev = NULL;
        register char *p;
-       extern readtimeout();
+       static int readtimeout();
 
        /* set the timeout */
        if (ReadTimeout != 0)
        {
                if (setjmp(CtxReadTimeout) != 0)
                {
 
        /* set the timeout */
        if (ReadTimeout != 0)
        {
                if (setjmp(CtxReadTimeout) != 0)
                {
-                       errno = ETIMEDOUT;
-                       syserr("net timeout");
+# 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);
                }
                        buf[0] = '\0';
                        return (NULL);
                }
@@ -647,8 +640,9 @@ sfgets(buf, siz, fp)
                buf[0] = '\0';
                return (NULL);
        }
                buf[0] = '\0';
                return (NULL);
        }
-       for (p = buf; *p != '\0'; p++)
-               *p &= ~0200;
+       if (!EightBit)
+               for (p = buf; *p != '\0'; p++)
+                       *p &= ~0200;
        return (buf);
 }
 
        return (buf);
 }
 
@@ -707,6 +701,13 @@ fgetfolded(buf, n, f)
                        if (i != ' ' && i != '\t')
                        {
                                *--p = '\0';
                        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 (buf);
                        }
                }