BSD 4_3_Reno release
[unix-history] / usr / src / usr.sbin / sendmail / src / recipient.c
index 86b429f..56b26b5 100644 (file)
@@ -1,8 +1,31 @@
+/*
+ * 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 <pwd.h>
 # include "sendmail.h"
 # include <pwd.h>
 # include "sendmail.h"
-# include <sys/stat.h>
-
-SCCSID(@(#)recipient.c 3.54            %G%);
 
 /*
 **  SENDTOLIST -- Designate a send list.
 
 /*
 **  SENDTOLIST -- Designate a send list.
@@ -10,12 +33,6 @@ SCCSID(@(#)recipient.c       3.54            %G%);
 **     The parameter is a comma-separated list of people to send to.
 **     This routine arranges to send to all of them.
 **
 **     The parameter is a comma-separated list of people to send to.
 **     This routine arranges to send to all of them.
 **
-**     The `ctladdr' is the address that expanded to be this one,
-**     e.g., in an alias expansion.  This is used for a number of
-**     purposed, most notably inheritance of uid/gid for protection
-**     purposes.  It is also used to detect self-reference in group
-**     expansions and the like.
-**
 **     Parameters:
 **             list -- the send list.
 **             ctladdr -- the address template for the person to
 **     Parameters:
 **             list -- the send list.
 **             ctladdr -- the address template for the person to
@@ -24,10 +41,9 @@ SCCSID(@(#)recipient.c       3.54            %G%);
 **                     expansion.
 **             sendq -- a pointer to the head of a queue to put
 **                     these people into.
 **                     expansion.
 **             sendq -- a pointer to the head of a queue to put
 **                     these people into.
-**             qflags -- special flags to set in the q_flags field.
 **
 **     Returns:
 **
 **     Returns:
-**             pointer to chain of addresses.
+**             none
 **
 **     Side Effects:
 **             none.
 **
 **     Side Effects:
 **             none.
@@ -35,28 +51,22 @@ SCCSID(@(#)recipient.c      3.54            %G%);
 
 # define MAXRCRSN      10
 
 
 # define MAXRCRSN      10
 
-ADDRESS *
-sendto(list, copyf, ctladdr, qflags)
+sendtolist(list, ctladdr, sendq)
        char *list;
        ADDRESS *ctladdr;
        ADDRESS **sendq;
        char *list;
        ADDRESS *ctladdr;
        ADDRESS **sendq;
-       u_short qflags;
 {
        register char *p;
        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 */
 {
        register char *p;
        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 */
-       ADDRESS *sibl;          /* sibling pointer in tree */
-       ADDRESS *prev;          /* previous sibling */
 
 
-# ifdef DEBUG
        if (tTd(25, 1))
        {
                printf("sendto: %s\n   ctladdr=", list);
                printaddr(ctladdr, FALSE);
        }
        if (tTd(25, 1))
        {
                printf("sendto: %s\n   ctladdr=", list);
                printaddr(ctladdr, FALSE);
        }
-# endif DEBUG
 
        /* heuristic to determine old versus new style addresses */
        if (ctladdr == NULL &&
 
        /* heuristic to determine old versus new style addresses */
        if (ctladdr == NULL &&
@@ -85,9 +95,6 @@ sendto(list, copyf, ctladdr, qflags)
                        continue;
                a->q_next = al;
                a->q_alias = ctladdr;
                        continue;
                a->q_next = al;
                a->q_alias = ctladdr;
-               if (ctladdr != NULL)
-                       a->q_flags |= ctladdr->q_flags & ~QPRIMARY;
-               a->q_flags |= qflags;
 
                /* see if this should be marked as a primary address */
                if (ctladdr == NULL ||
 
                /* see if this should be marked as a primary address */
                if (ctladdr == NULL ||
@@ -107,72 +114,21 @@ sendto(list, copyf, ctladdr, qflags)
                ctladdr->q_flags |= QDONTSEND;
 
        /* arrange to send to everyone on the local send list */
                ctladdr->q_flags |= QDONTSEND;
 
        /* arrange to send to everyone on the local send list */
-       prev = sibl = NULL;
-       if (ctladdr != NULL)
-               prev = ctladdr->q_child;
        while (al != NULL)
        {
                register ADDRESS *a = al;
                extern ADDRESS *recipient();
        while (al != NULL)
        {
                register ADDRESS *a = al;
                extern ADDRESS *recipient();
-               extern ADDRESS *recipient();
 
                al = a->q_next;
 
                al = a->q_next;
-               sibl = recipient(a);
-               if (sibl != NULL)
-               {
-                       extern ADDRESS *addrref();
+               setctladdr(a);
+               a = recipient(a, sendq);
 
 
-                       /* inherit full name */
-                       if (sibl->q_fullname == NULL && ctladdr != NULL)
-                               sibl->q_fullname = ctladdr->q_fullname;
-
-                       /* link tree together (but only if the node is new) */
-                       if (sibl == a)
-                       {
-                               sibl->q_sibling = prev;
-                               prev = sibl;
-                       }
-               }
+               /* arrange to inherit full name */
+               if (a->q_fullname == NULL && ctladdr != NULL)
+                       a->q_fullname = ctladdr->q_fullname;
        }
 
        CurEnv->e_to = NULL;
        }
 
        CurEnv->e_to = NULL;
-       if (ctladdr != NULL)
-               ctladdr->q_child = prev;
-       return (prev);
-}
-\f/*
-**  ADDRREF -- return pointer to address that references another address.
-**
-**     Parameters:
-**             a -- address to check.
-**             r -- reference to find.
-**
-**     Returns:
-**             address of node in tree rooted at 'a' that references
-**                     'r'.
-**             NULL if no such node exists.
-**
-**     Side Effects:
-**             none.
-*/
-
-ADDRESS *
-addrref(a, r)
-       register ADDRESS *a;
-       register ADDRESS *r;
-{
-       register ADDRESS *q;
-
-       while (a != NULL)
-       {
-               if (a->q_child == r || a->q_sibling == r)
-                       return (a);
-               q = addrref(a->q_child, r);
-               if (q != NULL)
-                       return (q);
-               a = a->q_sibling;
-       }
-       return (NULL);
 }
 \f/*
 **  RECIPIENT -- Designate a message recipient
 }
 \f/*
 **  RECIPIENT -- Designate a message recipient
@@ -186,13 +142,13 @@ addrref(a, r)
 **                     in this queue.
 **
 **     Returns:
 **                     in this queue.
 **
 **     Returns:
-**             pointer to address actually inserted in send list.
+**             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.
 */
 
-ADDRESS *
 ADDRESS *
 recipient(a, sendq)
        register ADDRESS *a;
 ADDRESS *
 recipient(a, sendq)
        register ADDRESS *a;
@@ -210,27 +166,31 @@ recipient(a, sendq)
        CurEnv->e_to = a->q_paddr;
        m = a->q_mailer;
        errno = 0;
        CurEnv->e_to = a->q_paddr;
        m = a->q_mailer;
        errno = 0;
-# ifdef DEBUG
        if (tTd(26, 1))
        {
                printf("\nrecipient: ");
                printaddr(a, FALSE);
        }
        if (tTd(26, 1))
        {
                printf("\nrecipient: ");
                printaddr(a, FALSE);
        }
-# endif DEBUG
 
        /* break aliasing loops */
        if (AliasLevel > MAXRCRSN)
        {
                usrerr("aliasing/forwarding loop broken");
 
        /* break aliasing loops */
        if (AliasLevel > MAXRCRSN)
        {
                usrerr("aliasing/forwarding loop broken");
-               return (NULL);
+               return (a);
        }
 
        /*
        **  Finish setting up address structure.
        */
 
        }
 
        /*
        **  Finish setting up address structure.
        */
 
+       /* set the queue timeout */
        a->q_timeout = 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++)
        {
        (void) strcpy(buf, a->q_user);
        for (p = buf; *p != '\0' && !quoted; p++)
        {
@@ -244,10 +204,10 @@ recipient(a, sendq)
        {
                a->q_mailer = m = ProgMailer;
                a->q_user++;
        {
                a->q_mailer = m = ProgMailer;
                a->q_user++;
-               if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
+               if (a->q_alias == NULL && !QueueRun && !ForceMail)
                {
                {
+                       a->q_flags |= QDONTSEND|QBADADDR;
                        usrerr("Cannot mail directly to programs");
                        usrerr("Cannot mail directly to programs");
-                       a->q_flags |= QDONTSEND;
                }
        }
 
                }
        }
 
@@ -264,19 +224,15 @@ recipient(a, sendq)
        {
                if (!ForceMail && sameaddr(q, a))
                {
        {
                if (!ForceMail && sameaddr(q, a))
                {
-# ifdef DEBUG
                        if (tTd(26, 1))
                        {
                                printf("%s in sendq: ", a->q_paddr);
                                printaddr(q, FALSE);
                        }
                        if (tTd(26, 1))
                        {
                                printf("%s in sendq: ", a->q_paddr);
                                printaddr(q, FALSE);
                        }
-# endif DEBUG
-                       if (Verbose && !bitset(QDONTSEND|QPSEUDO, a->q_flags))
+                       if (!bitset(QDONTSEND, a->q_flags))
                                message(Arpa_Info, "duplicate suppressed");
                        if (!bitset(QPRIMARY, q->q_flags))
                                q->q_flags |= a->q_flags;
                                message(Arpa_Info, "duplicate suppressed");
                        if (!bitset(QPRIMARY, q->q_flags))
                                q->q_flags |= a->q_flags;
-                       if (!bitset(QPSEUDO, a->q_flags))
-                               q->q_flags &= ~QPSEUDO;
                        return (q);
                }
        }
                        return (q);
                }
        }
@@ -284,6 +240,7 @@ recipient(a, sendq)
        /* add address on list */
        *pq = a;
        a->q_next = NULL;
        /* add address on list */
        *pq = a;
        a->q_next = NULL;
+       CurEnv->e_nrcpts++;
 
        /*
        **  Alias the name and handle :include: specs.
 
        /*
        **  Alias the name and handle :include: specs.
@@ -294,8 +251,11 @@ recipient(a, sendq)
                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 && !tTd(0, 1) && !QueueRun && !ForceMail)
+                       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
                        {
                                message(Arpa_Info, "including file %s", &a->q_user[9]);
                        else
                        {
                                message(Arpa_Info, "including file %s", &a->q_user[9]);
@@ -324,10 +284,10 @@ recipient(a, sendq)
                {
                        p = rindex(buf, '/');
                        /* check if writable or creatable */
                {
                        p = rindex(buf, '/');
                        /* check if writable or creatable */
-                       if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
+                       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)) :
                            (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
                        }
                        else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
                            (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
@@ -352,8 +312,6 @@ recipient(a, sendq)
                        {
                                char nbuf[MAXNAME];
 
                        {
                                char nbuf[MAXNAME];
 
-                               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);
@@ -364,9 +322,6 @@ recipient(a, sendq)
                                a->q_gid = pw->pw_gid;
                                a->q_flags |= QGOODUID;
                                buildfname(pw->pw_gecos, pw->pw_name, nbuf);
                                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);
-                               fullname(pw, nbuf);
                                if (nbuf[0] != '\0')
                                        a->q_fullname = newstr(nbuf);
                                if (!quoted)
                                if (nbuf[0] != '\0')
                                        a->q_fullname = newstr(nbuf);
                                if (!quoted)
@@ -375,8 +330,6 @@ recipient(a, sendq)
                }
        }
        return (a);
                }
        }
        return (a);
-
-       return (a);
 }
 \f/*
 **  FINDUSER -- find the password entry for a user.
 }
 \f/*
 **  FINDUSER -- find the password entry for a user.
@@ -402,38 +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++)
        {
        for (p = name; *p != '\0'; p++)
        {
-               if (*p == (SpaceSub & 0177) || *p == '_')
-                       *p = ' ';
+               if (isascii(*p) && isupper(*p))
+                       *p = tolower(*p);
        }
 
        }
 
-       /* look up this login name */
+       /* look up this login name using fast path */
        if ((pw = getpwnam(name)) != NULL)
                return (pw);
 
        /* search for a matching full name instead */
        if ((pw = getpwnam(name)) != NULL)
                return (pw);
 
        /* search for a matching full name instead */
-       setpwent();
+       for (p = name; *p != '\0'; p++)
+       {
+               if (*p == (SpaceSub & 0177) || *p == '_')
+                       *p = ' ';
+       }
+       (void) setpwent();
        while ((pw = getpwent()) != NULL)
        {
        while ((pw = getpwent()) != NULL)
        {
-               extern bool sameword();
                char buf[MAXNAME];
 
                char buf[MAXNAME];
 
-               if (strcmp(pw->pw_name, name) == 0)
-                       return (pw);
-               fullname(pw, buf);
-               if (index(buf, ' ') != NULL && sameword(buf, name))
+               buildfname(pw->pw_gecos, pw->pw_name, buf);
+               if (index(buf, ' ') != NULL && !strcasecmp(buf, name))
                {
                {
-                               message(Arpa_Info, "sending to %s <%s>",
-                                   buf, pw->pw_name);
+                       message(Arpa_Info, "sending to login name %s", pw->pw_name);
                        return (pw);
                }
        }
                        return (pw);
                }
        }
@@ -547,6 +499,7 @@ include(fname, msg, ctladdr, sendq)
        {
                register char *p = index(buf, '\n');
 
        {
                register char *p = index(buf, '\n');
 
+               LineNumber++;
                if (p != NULL)
                        *p = '\0';
                if (buf[0] == '\0')
                if (p != NULL)
                        *p = '\0';
                if (buf[0] == '\0')
@@ -554,7 +507,7 @@ include(fname, msg, ctladdr, sendq)
                CurEnv->e_to = oldto;
                message(Arpa_Info, "%s to %s", msg, buf);
                AliasLevel++;
                CurEnv->e_to = oldto;
                message(Arpa_Info, "%s to %s", msg, buf);
                AliasLevel++;
-               sendto(buf, 1, ctladdr, 0);
+               sendtolist(buf, ctladdr, sendq);
                AliasLevel--;
        }
 
                AliasLevel--;
        }
 
@@ -580,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];
 
@@ -599,7 +551,7 @@ sendtoargv(argv)
                                argv += 2;
                        }
                }
                                argv += 2;
                        }
                }
-               sendto(p, 0, (ADDRESS *) NULL, 0);
+               sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
        }
 }
 \f/*
        }
 }
 \f/*