BSD 4_3_Reno release
[unix-history] / usr / src / usr.sbin / sendmail / src / recipient.c
index 73151ee..56b26b5 100644 (file)
@@ -1,21 +1,46 @@
-# include <pwd.h>
+/*
+ * Copyright (c) 1983 Eric P. Allman
+ * Copyright (c) 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted provided
+ * that: (1) source distributions retain this entire copyright notice and
+ * comment, and (2) distributions including binaries display the following
+ * acknowledgement:  ``This product includes software developed by the
+ * University of California, Berkeley and its contributors'' in the
+ * documentation or other materials provided with the distribution and in
+ * all advertising materials mentioning features or use of this software.
+ * Neither the name of the University nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)recipient.c        5.18 (Berkeley) 6/1/90";
+#endif /* not lint */
+
 # include <sys/types.h>
 # include <sys/stat.h>
 # include <sys/types.h>
 # include <sys/stat.h>
+# include <pwd.h>
 # include "sendmail.h"
 
 # include "sendmail.h"
 
-static char SccsId[] = "@(#)recipient.c        3.21    %G%";
-
 /*
 /*
-**  SENDTO -- Designate a send list.
+**  SENDTOLIST -- Designate a send list.
 **
 **     The parameter is a comma-separated list of people to send to.
 **     This routine arranges to send to all of them.
 **
 **     Parameters:
 **             list -- the send list.
 **
 **     The parameter is a comma-separated list of people to send to.
 **     This routine arranges to send to all of them.
 **
 **     Parameters:
 **             list -- the send list.
-**             copyf -- the copy flag; passed to parse.
 **             ctladdr -- the address template for the person to
 **                     send to -- effective uid/gid are important.
 **             ctladdr -- the address template for the person to
 **                     send to -- effective uid/gid are important.
+**                     This is typically the alias that caused this
+**                     expansion.
+**             sendq -- a pointer to the head of a queue to put
+**                     these people into.
 **
 **     Returns:
 **             none
 **
 **     Returns:
 **             none
@@ -26,61 +51,84 @@ static char SccsId[] = "@(#)recipient.c     3.21    %G%";
 
 # define MAXRCRSN      10
 
 
 # define MAXRCRSN      10
 
-sendto(list, copyf, ctladdr)
+sendtolist(list, ctladdr, sendq)
        char *list;
        char *list;
-       int copyf;
        ADDRESS *ctladdr;
        ADDRESS *ctladdr;
+       ADDRESS **sendq;
 {
        register char *p;
 {
        register char *p;
-       bool more;              /* set if more addresses to send to */
-       ADDRESS *al;            /* list of addresses to send to */
+       register ADDRESS *al;   /* list of addresses to send to */
+       bool firstone;          /* set on first address sent */
+       bool selfref;           /* set if this list includes ctladdr */
+       char delimiter;         /* the address delimiter */
 
 
-# ifdef DEBUG
-       if (Debug > 1)
-               printf("sendto: %s\n", list);
-# endif DEBUG
+       if (tTd(25, 1))
+       {
+               printf("sendto: %s\n   ctladdr=", list);
+               printaddr(ctladdr, FALSE);
+       }
 
 
-       more = TRUE;
+       /* heuristic to determine old versus new style addresses */
+       if (ctladdr == NULL &&
+           (index(list, ',') != NULL || index(list, ';') != NULL ||
+            index(list, '<') != NULL || index(list, '(') != NULL))
+               CurEnv->e_flags &= ~EF_OLDSTYLE;
+       delimiter = ' ';
+       if (!bitset(EF_OLDSTYLE, CurEnv->e_flags) || ctladdr != NULL)
+               delimiter = ',';
+
+       firstone = TRUE;
+       selfref = FALSE;
        al = NULL;
        al = NULL;
-       for (p = list; more; )
-       {
-               register char *q;
-               register char c;
-               ADDRESS *a;
 
 
-               /* find the end of this address */
-               while (*p == ' ' || *p == '\t')
-                       p++;
-               q = p;
-               while ((c = *p++) != '\0' && c != ',' && c != '\n')
-                       continue;
-               more = c != '\0';
-               *--p = '\0';
-               if (more)
-                       p++;
-               if (*q == '\0')
-                       continue;
+       for (p = list; *p != '\0'; )
+       {
+               register ADDRESS *a;
+               extern char *DelimChar;         /* defined in prescan */
 
                /* parse the address */
 
                /* parse the address */
-               if ((a = parse(q, (ADDRESS *) NULL, copyf)) == NULL)
+               while (isspace(*p) || *p == ',')
+                       p++;
+               a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter);
+               p = DelimChar;
+               if (a == NULL)
                        continue;
                        continue;
-
-               /* put it on the local send list */
                a->q_next = al;
                a->q_alias = ctladdr;
                a->q_next = al;
                a->q_alias = ctladdr;
-               al = a;
+
+               /* see if this should be marked as a primary address */
+               if (ctladdr == NULL ||
+                   (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
+                       a->q_flags |= QPRIMARY;
+
+               /* put on send queue or suppress self-reference */
+               if (ctladdr != NULL && sameaddr(ctladdr, a))
+                       selfref = TRUE;
+               else
+                       al = a;
+               firstone = FALSE;
        }
 
        }
 
+       /* if this alias doesn't include itself, delete ctladdr */
+       if (!selfref && ctladdr != NULL)
+               ctladdr->q_flags |= QDONTSEND;
+
        /* arrange to send to everyone on the local send list */
        while (al != NULL)
        {
                register ADDRESS *a = al;
        /* arrange to send to everyone on the local send list */
        while (al != NULL)
        {
                register ADDRESS *a = al;
+               extern ADDRESS *recipient();
 
                al = a->q_next;
 
                al = a->q_next;
-               recipient(a);
+               setctladdr(a);
+               a = recipient(a, sendq);
+
+               /* arrange to inherit full name */
+               if (a->q_fullname == NULL && ctladdr != NULL)
+                       a->q_fullname = ctladdr->q_fullname;
        }
 
        }
 
-       To = NULL;
+       CurEnv->e_to = NULL;
 }
 \f/*
 **  RECIPIENT -- Designate a message recipient
 }
 \f/*
 **  RECIPIENT -- Designate a message recipient
@@ -89,102 +137,133 @@ sendto(list, copyf, ctladdr)
 **
 **     Parameters:
 **             a -- the (preparsed) address header for the recipient.
 **
 **     Parameters:
 **             a -- the (preparsed) address header for the recipient.
+**             sendq -- a pointer to the head of a queue to put the
+**                     recipient in.  Duplicate supression is done
+**                     in this queue.
 **
 **     Returns:
 **
 **     Returns:
-**             none.
+**             The actual address in the queue.  This will be "a" if
+**             the address is not a duplicate, else the original address.
 **
 **     Side Effects:
 **             none.
 */
 
 **
 **     Side Effects:
 **             none.
 */
 
-recipient(a)
+ADDRESS *
+recipient(a, sendq)
        register ADDRESS *a;
        register ADDRESS *a;
+       register ADDRESS **sendq;
 {
        register ADDRESS *q;
        ADDRESS **pq;
        register struct mailer *m;
 {
        register ADDRESS *q;
        ADDRESS **pq;
        register struct mailer *m;
+       register char *p;
+       bool quoted = FALSE;            /* set if the addr has a quote bit */
+       char buf[MAXNAME];              /* unquoted image of the user name */
        extern ADDRESS *getctladdr();
        extern ADDRESS *getctladdr();
+       extern bool safefile();
 
 
-       To = a->q_paddr;
-       m = Mailer[a->q_mailer];
+       CurEnv->e_to = a->q_paddr;
+       m = a->q_mailer;
        errno = 0;
        errno = 0;
-# ifdef DEBUG
-       if (Debug)
-               printf("recipient(%s)\n", To);
-# endif DEBUG
+       if (tTd(26, 1))
+       {
+               printf("\nrecipient: ");
+               printaddr(a, FALSE);
+       }
 
        /* break aliasing loops */
        if (AliasLevel > MAXRCRSN)
        {
                usrerr("aliasing/forwarding loop broken");
 
        /* break aliasing loops */
        if (AliasLevel > MAXRCRSN)
        {
                usrerr("aliasing/forwarding loop broken");
-               return;
+               return (a);
        }
 
        /*
        }
 
        /*
-       **  Do sickly crude mapping for program mailing, etc.
+       **  Finish setting up address structure.
        */
 
        */
 
-       if (a->q_mailer == MN_LOCAL)
+       /* set the queue timeout */
+       a->q_timeout = TimeOut;
+
+       /* map user & host to lower case if requested on non-aliases */
+       if (a->q_alias == NULL)
+               loweraddr(a);
+
+       /* get unquoted user for file, program or user.name check */
+       (void) strcpy(buf, a->q_user);
+       for (p = buf; *p != '\0' && !quoted; p++)
        {
        {
-               if (a->q_user[0] == '|')
+               if (!isascii(*p) && (*p & 0377) != (SpaceSub & 0377))
+                       quoted = TRUE;
+       }
+       stripquotes(buf, TRUE);
+
+       /* do sickly crude mapping for program mailing, etc. */
+       if (m == LocalMailer && buf[0] == '|')
+       {
+               a->q_mailer = m = ProgMailer;
+               a->q_user++;
+               if (a->q_alias == NULL && !QueueRun && !ForceMail)
                {
                {
-                       a->q_mailer = MN_PROG;
-                       m = Mailer[MN_PROG];
-                       a->q_user++;
-                       if (a->q_alias == NULL && Debug == 0)
-                       {
-                               usrerr("Cannot mail directly to programs");
-                               a->q_flags |= QDONTSEND;
-                       }
+                       a->q_flags |= QDONTSEND|QBADADDR;
+                       usrerr("Cannot mail directly to programs");
                }
        }
 
        /*
                }
        }
 
        /*
-       **  Look up this person in the recipient list.  If they
-       **  are there already, return, otherwise continue.
-       **  If the list is empty, just add it.
+       **  Look up this person in the recipient list.
+       **      If they are there already, return, otherwise continue.
+       **      If the list is empty, just add it.  Notice the cute
+       **      hack to make from addresses suppress things correctly:
+       **      the QDONTSEND bit will be set in the send list.
+       **      [Please note: the emphasis is on "hack."]
        */
 
        */
 
-       for (pq = &m->m_sendq; (q = *pq) != NULL; pq = &q->q_next)
+       for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
        {
        {
-               if (!ForceMail && sameaddr(q, a, FALSE))
+               if (!ForceMail && sameaddr(q, a))
                {
                {
-# ifdef DEBUG
-                       if (Debug)
-                               printf("(%s in sendq)\n", a->q_paddr);
-# endif DEBUG
-                       if (Verbose && !bitset(QDONTSEND, a->q_flags))
+                       if (tTd(26, 1))
+                       {
+                               printf("%s in sendq: ", a->q_paddr);
+                               printaddr(q, FALSE);
+                       }
+                       if (!bitset(QDONTSEND, a->q_flags))
                                message(Arpa_Info, "duplicate suppressed");
                                message(Arpa_Info, "duplicate suppressed");
-                       return;
+                       if (!bitset(QPRIMARY, q->q_flags))
+                               q->q_flags |= a->q_flags;
+                       return (q);
                }
        }
 
        /* add address on list */
        *pq = a;
        a->q_next = NULL;
                }
        }
 
        /* add address on list */
        *pq = a;
        a->q_next = NULL;
-       if (DontSend)
-               a->q_flags |= QDONTSEND;
+       CurEnv->e_nrcpts++;
 
        /*
        **  Alias the name and handle :include: specs.
        */
 
 
        /*
        **  Alias the name and handle :include: specs.
        */
 
-       if (a->q_mailer == MN_LOCAL)
+       if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags))
        {
                if (strncmp(a->q_user, ":include:", 9) == 0)
                {
                        a->q_flags |= QDONTSEND;
        {
                if (strncmp(a->q_user, ":include:", 9) == 0)
                {
                        a->q_flags |= QDONTSEND;
-                       if (a->q_alias == NULL && Debug == 0)
+                       if (a->q_alias == NULL && !QueueRun && !ForceMail)
+                       {
+                               a->q_flags |= QBADADDR;
                                usrerr("Cannot mail directly to :include:s");
                                usrerr("Cannot mail directly to :include:s");
+                       }
                        else
                        {
                        else
                        {
-                               if (Verbose)
-                                       message(Arpa_Info, "including file %s", &a->q_user[9]);
-                               include(&a->q_user[9], " sending", a);
+                               message(Arpa_Info, "including file %s", &a->q_user[9]);
+                               include(&a->q_user[9], " sending", a, sendq);
                        }
                }
                else
                        }
                }
                else
-                       alias(a);
+                       alias(a, sendq);
        }
 
        /*
        }
 
        /*
@@ -195,36 +274,26 @@ recipient(a)
        **  the user (which is probably correct anyway).
        */
 
        **  the user (which is probably correct anyway).
        */
 
-       if (!bitset(QDONTSEND, a->q_flags) && a->q_mailer == MN_LOCAL)
+       if (!bitset(QDONTSEND, a->q_flags) && m == LocalMailer)
        {
        {
-               char buf[MAXNAME];
-               register char *p;
                struct stat stb;
                extern bool writable();
                struct stat stb;
                extern bool writable();
-               bool quoted = FALSE;
-
-               strcpy(buf, a->q_user);
-               for (p = buf; *p != '\0' && !quoted; p++)
-               {
-                       if (!isascii(*p))
-                               quoted = TRUE;
-               }
-               stripquotes(buf, TRUE);
 
                /* see if this is to a file */
 
                /* see if this is to a file */
-               if ((p = rindex(buf, '/')) != NULL)
+               if (buf[0] == '/')
                {
                {
+                       p = rindex(buf, '/');
                        /* check if writable or creatable */
                        /* check if writable or creatable */
-                       if (a->q_alias == NULL && Debug == 0)
+                       if (a->q_alias == NULL && !QueueRun && !ForceMail)
                        {
                        {
+                               a->q_flags |= QDONTSEND|QBADADDR;
                                usrerr("Cannot mail directly to files");
                                usrerr("Cannot mail directly to files");
-                               a->q_flags |= QDONTSEND;
                        }
                        else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
                        }
                        else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
-                           (*p = '\0', access(buf, 3) < 0))
+                           (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
                        {
                                a->q_flags |= QBADADDR;
                        {
                                a->q_flags |= QBADADDR;
-                               giveresponse(EX_CANTCREAT, TRUE, m);
+                               giveresponse(EX_CANTCREAT, m, CurEnv);
                        }
                }
                else
                        }
                }
                else
@@ -237,24 +306,30 @@ recipient(a)
                        if (pw == NULL)
                        {
                                a->q_flags |= QBADADDR;
                        if (pw == NULL)
                        {
                                a->q_flags |= QBADADDR;
-                               giveresponse(EX_NOUSER, TRUE, m);
+                               giveresponse(EX_NOUSER, m, CurEnv);
                        }
                        else
                        {
                        }
                        else
                        {
+                               char nbuf[MAXNAME];
+
                                if (strcmp(a->q_user, pw->pw_name) != 0)
                                {
                                        a->q_user = newstr(pw->pw_name);
                                if (strcmp(a->q_user, pw->pw_name) != 0)
                                {
                                        a->q_user = newstr(pw->pw_name);
-                                       strcpy(buf, pw->pw_name);
+                                       (void) strcpy(buf, pw->pw_name);
                                }
                                a->q_home = newstr(pw->pw_dir);
                                a->q_uid = pw->pw_uid;
                                a->q_gid = pw->pw_gid;
                                a->q_flags |= QGOODUID;
                                }
                                a->q_home = newstr(pw->pw_dir);
                                a->q_uid = pw->pw_uid;
                                a->q_gid = pw->pw_gid;
                                a->q_flags |= QGOODUID;
+                               buildfname(pw->pw_gecos, pw->pw_name, nbuf);
+                               if (nbuf[0] != '\0')
+                                       a->q_fullname = newstr(nbuf);
                                if (!quoted)
                                if (!quoted)
-                                       forward(a);
+                                       forward(a, sendq);
                        }
                }
        }
                        }
                }
        }
+       return (a);
 }
 \f/*
 **  FINDUSER -- find the password entry for a user.
 }
 \f/*
 **  FINDUSER -- find the password entry for a user.
@@ -262,6 +337,9 @@ recipient(a)
 **     This looks a lot like getpwnam, except that it may want to
 **     do some fancier pattern matching in /etc/passwd.
 **
 **     This looks a lot like getpwnam, except that it may want to
 **     do some fancier pattern matching in /etc/passwd.
 **
+**     This routine contains most of the time of many sendmail runs.
+**     It deserves to be optimized.
+**
 **     Parameters:
 **             name -- the name to match against.
 **
 **     Parameters:
 **             name -- the name to match against.
 **
@@ -277,34 +355,37 @@ struct passwd *
 finduser(name)
        char *name;
 {
 finduser(name)
        char *name;
 {
-       extern struct passwd *getpwent();
        register struct passwd *pw;
        register char *p;
        register struct passwd *pw;
        register char *p;
+       extern struct passwd *getpwent();
+       extern struct passwd *getpwnam();
 
 
-       /*
-       **  Make name canonical.
-       */
+       /* map upper => lower case */
+       for (p = name; *p != '\0'; p++)
+       {
+               if (isascii(*p) && isupper(*p))
+                       *p = tolower(*p);
+       }
 
 
+       /* look up this login name using fast path */
+       if ((pw = getpwnam(name)) != NULL)
+               return (pw);
+
+       /* search for a matching full name instead */
        for (p = name; *p != '\0'; p++)
        {
        for (p = name; *p != '\0'; p++)
        {
-               if (*p == (SPACESUB & 0177) || *p == '_')
+               if (*p == (SpaceSub & 0177) || *p == '_')
                        *p = ' ';
        }
                        *p = ' ';
        }
-
-       setpwent();
+       (void) setpwent();
        while ((pw = getpwent()) != NULL)
        {
                char buf[MAXNAME];
        while ((pw = getpwent()) != NULL)
        {
                char buf[MAXNAME];
-               extern bool sameword();
 
 
-               if (strcmp(pw->pw_name, name) == 0)
-                       return (pw);
                buildfname(pw->pw_gecos, pw->pw_name, buf);
                buildfname(pw->pw_gecos, pw->pw_name, buf);
-               if (index(buf, ' ') != NULL && sameword(buf, name))
+               if (index(buf, ' ') != NULL && !strcasecmp(buf, name))
                {
                {
-                       if (Verbose)
-                               message(Arpa_Info, "sending to login name %s",
-                                   pw->pw_name);
+                       message(Arpa_Info, "sending to login name %s", pw->pw_name);
                        return (pw);
                }
        }
                        return (pw);
                }
        }
@@ -371,6 +452,8 @@ writable(s)
 **             ctladdr -- address template to use to fill in these
 **                     addresses -- effective user/group id are
 **                     the important things.
 **             ctladdr -- address template to use to fill in these
 **                     addresses -- effective user/group id are
 **                     the important things.
+**             sendq -- a pointer to the head of the send queue
+**                     to put these addresses in.
 **
 **     Returns:
 **             none.
 **
 **     Returns:
 **             none.
@@ -380,14 +463,17 @@ writable(s)
 **             listed in that file.
 */
 
 **             listed in that file.
 */
 
-include(fname, msg, ctladdr)
+include(fname, msg, ctladdr, sendq)
        char *fname;
        char *msg;
        ADDRESS *ctladdr;
        char *fname;
        char *msg;
        ADDRESS *ctladdr;
+       ADDRESS **sendq;
 {
        char buf[MAXLINE];
        register FILE *fp;
 {
        char buf[MAXLINE];
        register FILE *fp;
-       char *oldto = To;
+       char *oldto = CurEnv->e_to;
+       char *oldfilename = FileName;
+       int oldlinenumber = LineNumber;
 
        fp = fopen(fname, "r");
        if (fp == NULL)
 
        fp = fopen(fname, "r");
        if (fp == NULL)
@@ -407,23 +493,27 @@ include(fname, msg, ctladdr)
        }
 
        /* read the file -- each line is a comma-separated list. */
        }
 
        /* read the file -- each line is a comma-separated list. */
+       FileName = fname;
+       LineNumber = 0;
        while (fgets(buf, sizeof buf, fp) != NULL)
        {
                register char *p = index(buf, '\n');
 
        while (fgets(buf, sizeof buf, fp) != NULL)
        {
                register char *p = index(buf, '\n');
 
+               LineNumber++;
                if (p != NULL)
                        *p = '\0';
                if (buf[0] == '\0')
                        continue;
                if (p != NULL)
                        *p = '\0';
                if (buf[0] == '\0')
                        continue;
-               To = oldto;
-               if (Verbose)
-                       message(Arpa_Info, "%s to %s", msg, buf);
+               CurEnv->e_to = oldto;
+               message(Arpa_Info, "%s to %s", msg, buf);
                AliasLevel++;
                AliasLevel++;
-               sendto(buf, 1, ctladdr);
+               sendtolist(buf, ctladdr, sendq);
                AliasLevel--;
        }
 
        (void) fclose(fp);
                AliasLevel--;
        }
 
        (void) fclose(fp);
+       FileName = oldfilename;
+       LineNumber = oldlinenumber;
 }
 \f/*
 **  SENDTOARGV -- send to an argument vector.
 }
 \f/*
 **  SENDTOARGV -- send to an argument vector.
@@ -443,11 +533,10 @@ sendtoargv(argv)
        register char **argv;
 {
        register char *p;
        register char **argv;
 {
        register char *p;
-       extern bool sameword();
 
        while ((p = *argv++) != NULL)
        {
 
        while ((p = *argv++) != NULL)
        {
-               if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at"))
+               if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at"))
                {
                        char nbuf[MAXNAME];
 
                {
                        char nbuf[MAXNAME];
 
@@ -462,7 +551,7 @@ sendtoargv(argv)
                                argv += 2;
                        }
                }
                                argv += 2;
                        }
                }
-               sendto(p, 0, NULL);
+               sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
        }
 }
 \f/*
        }
 }
 \f/*