1N/A/*
1N/A * Copyright (c) 1998-2003, 2006 Sendmail, Inc. and its suppliers.
1N/A * All rights reserved.
1N/A * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
1N/A * Copyright (c) 1988, 1993
1N/A * The Regents of the University of California. All rights reserved.
1N/A *
1N/A * By using this file, you agree to the terms and conditions set
1N/A * forth in the LICENSE file which can be found at the top level of
1N/A * the sendmail distribution.
1N/A *
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#include <sendmail.h>
1N/A
1N/ASM_RCSID("@(#)$Id: recipient.c,v 8.349 2007/07/10 17:01:22 ca Exp $")
1N/A
1N/Astatic void includetimeout __P((int));
1N/Astatic ADDRESS *self_reference __P((ADDRESS *));
1N/Astatic int sortexpensive __P((ADDRESS *, ADDRESS *));
1N/Astatic int sortbysignature __P((ADDRESS *, ADDRESS *));
1N/Astatic int sorthost __P((ADDRESS *, ADDRESS *));
1N/A
1N/Atypedef int sortfn_t __P((ADDRESS *, ADDRESS *));
1N/A
1N/A/*
1N/A** SORTHOST -- strcmp()-like func for host portion of an ADDRESS
1N/A**
1N/A** Parameters:
1N/A** xx -- first ADDRESS
1N/A** yy -- second ADDRESS
1N/A**
1N/A** Returns:
1N/A** <0 when xx->q_host is less than yy->q_host
1N/A** >0 when xx->q_host is greater than yy->q_host
1N/A** 0 when equal
1N/A*/
1N/A
1N/Astatic int
1N/Asorthost(xx, yy)
1N/A register ADDRESS *xx;
1N/A register ADDRESS *yy;
1N/A{
1N/A#if _FFR_HOST_SORT_REVERSE
1N/A /* XXX maybe compare hostnames from the end? */
1N/A return sm_strrevcasecmp(xx->q_host, yy->q_host);
1N/A#else /* _FFR_HOST_SORT_REVERSE */
1N/A return sm_strcasecmp(xx->q_host, yy->q_host);
1N/A#endif /* _FFR_HOST_SORT_REVERSE */
1N/A}
1N/A
1N/A/*
1N/A** SORTEXPENSIVE -- strcmp()-like func for expensive mailers
1N/A**
1N/A** The mailer has been noted already as "expensive" for 'xx'. This
1N/A** will give a result relative to 'yy'. Expensive mailers get rated
1N/A** "greater than" non-expensive mailers because during the delivery phase
1N/A** it will get queued -- no use it getting in the way of less expensive
1N/A** recipients. We avoid an MX RR lookup when both 'xx' and 'yy' are
1N/A** expensive since an MX RR lookup happens when extracted from the queue
1N/A** later.
1N/A**
1N/A** Parameters:
1N/A** xx -- first ADDRESS
1N/A** yy -- second ADDRESS
1N/A**
1N/A** Returns:
1N/A** <0 when xx->q_host is less than yy->q_host and both are
1N/A** expensive
1N/A** >0 when xx->q_host is greater than yy->q_host, or when
1N/A** 'yy' is non-expensive
1N/A** 0 when equal (by expense and q_host)
1N/A*/
1N/A
1N/Astatic int
1N/Asortexpensive(xx, yy)
1N/A ADDRESS *xx;
1N/A ADDRESS *yy;
1N/A{
1N/A if (!bitnset(M_EXPENSIVE, yy->q_mailer->m_flags))
1N/A return 1; /* xx should go later */
1N/A#if _FFR_HOST_SORT_REVERSE
1N/A /* XXX maybe compare hostnames from the end? */
1N/A return sm_strrevcasecmp(xx->q_host, yy->q_host);
1N/A#else /* _FFR_HOST_SORT_REVERSE */
1N/A return sm_strcasecmp(xx->q_host, yy->q_host);
1N/A#endif /* _FFR_HOST_SORT_REVERSE */
1N/A}
1N/A
1N/A/*
1N/A** SORTBYSIGNATURE -- a strcmp()-like func for q_mailer and q_host in ADDRESS
1N/A**
1N/A** Parameters:
1N/A** xx -- first ADDRESS
1N/A** yy -- second ADDRESS
1N/A**
1N/A** Returns:
1N/A** 0 when the "signature"'s are same
1N/A** <0 when xx->q_signature is less than yy->q_signature
1N/A** >0 when xx->q_signature is greater than yy->q_signature
1N/A**
1N/A** Side Effect:
1N/A** May set ADDRESS pointer for q_signature if not already set.
1N/A*/
1N/A
1N/Astatic int
1N/Asortbysignature(xx, yy)
1N/A ADDRESS *xx;
1N/A ADDRESS *yy;
1N/A{
1N/A register int ret;
1N/A
1N/A /* Let's avoid redoing the signature over and over again */
1N/A if (xx->q_signature == NULL)
1N/A xx->q_signature = hostsignature(xx->q_mailer, xx->q_host);
1N/A if (yy->q_signature == NULL)
1N/A yy->q_signature = hostsignature(yy->q_mailer, yy->q_host);
1N/A ret = strcmp(xx->q_signature, yy->q_signature);
1N/A
1N/A /*
1N/A ** If the two signatures are the same then we will return a sort
1N/A ** value based on 'q_user'. But note that we have reversed xx and yy
1N/A ** on purpose. This additional compare helps reduce the number of
1N/A ** sameaddr() calls and loops in recipient() for the case when
1N/A ** the rcpt list has been provided already in-order.
1N/A */
1N/A
1N/A if (ret == 0)
1N/A return strcmp(yy->q_user, xx->q_user);
1N/A else
1N/A return ret;
1N/A}
1N/A
1N/A/*
1N/A** SENDTOLIST -- Designate a send list.
1N/A**
1N/A** The parameter is a comma-separated list of people to send to.
1N/A** This routine arranges to send to all of them.
1N/A**
1N/A** Parameters:
1N/A** list -- the send list.
1N/A** ctladdr -- the address template for the person to
1N/A** send to -- effective uid/gid are important.
1N/A** This is typically the alias that caused this
1N/A** expansion.
1N/A** sendq -- a pointer to the head of a queue to put
1N/A** these people into.
1N/A** aliaslevel -- the current alias nesting depth -- to
1N/A** diagnose loops.
1N/A** e -- the envelope in which to add these recipients.
1N/A**
1N/A** Returns:
1N/A** The number of addresses actually on the list.
1N/A*/
1N/A
1N/A/* q_flags bits inherited from ctladdr */
1N/A#define QINHERITEDBITS (QPINGONSUCCESS|QPINGONFAILURE|QPINGONDELAY|QHASNOTIFY)
1N/A
1N/Aint
1N/Asendtolist(list, ctladdr, sendq, aliaslevel, e)
1N/A char *list;
1N/A ADDRESS *ctladdr;
1N/A ADDRESS **sendq;
1N/A int aliaslevel;
1N/A register ENVELOPE *e;
1N/A{
1N/A register char *p;
1N/A register ADDRESS *SM_NONVOLATILE al; /* list of addresses to send to */
1N/A SM_NONVOLATILE char delimiter; /* the address delimiter */
1N/A SM_NONVOLATILE int naddrs;
1N/A SM_NONVOLATILE int i;
1N/A char *endp;
1N/A char *oldto = e->e_to;
1N/A char *SM_NONVOLATILE bufp;
1N/A char buf[MAXNAME + 1];
1N/A
1N/A if (list == NULL)
1N/A {
1N/A syserr("sendtolist: null list");
1N/A return 0;
1N/A }
1N/A
1N/A if (tTd(25, 1))
1N/A {
1N/A sm_dprintf("sendto: %s\n ctladdr=", list);
1N/A printaddr(sm_debug_file(), ctladdr, false);
1N/A }
1N/A
1N/A /* heuristic to determine old versus new style addresses */
1N/A if (ctladdr == NULL &&
1N/A (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
1N/A strchr(list, '<') != NULL || strchr(list, '(') != NULL))
1N/A e->e_flags &= ~EF_OLDSTYLE;
1N/A delimiter = ' ';
1N/A if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL)
1N/A delimiter = ',';
1N/A
1N/A al = NULL;
1N/A naddrs = 0;
1N/A
1N/A /* make sure we have enough space to copy the string */
1N/A i = strlen(list) + 1;
1N/A if (i <= sizeof(buf))
1N/A {
1N/A bufp = buf;
1N/A i = sizeof(buf);
1N/A }
1N/A else
1N/A bufp = sm_malloc_x(i);
1N/A endp = bufp + i;
1N/A
1N/A SM_TRY
1N/A {
1N/A (void) sm_strlcpy(bufp, denlstring(list, false, true), i);
1N/A
1N/A macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e r");
1N/A for (p = bufp; *p != '\0'; )
1N/A {
1N/A auto char *delimptr;
1N/A register ADDRESS *a;
1N/A
1N/A SM_ASSERT(p < endp);
1N/A
1N/A /* parse the address */
1N/A while ((isascii(*p) && isspace(*p)) || *p == ',')
1N/A p++;
1N/A SM_ASSERT(p < endp);
1N/A a = parseaddr(p, NULLADDR, RF_COPYALL, delimiter,
1N/A &delimptr, e, true);
1N/A p = delimptr;
1N/A SM_ASSERT(p < endp);
1N/A if (a == NULL)
1N/A continue;
1N/A a->q_next = al;
1N/A a->q_alias = ctladdr;
1N/A
1N/A /* arrange to inherit attributes from parent */
1N/A if (ctladdr != NULL)
1N/A {
1N/A ADDRESS *b;
1N/A
1N/A /* self reference test */
1N/A if (sameaddr(ctladdr, a))
1N/A {
1N/A if (tTd(27, 5))
1N/A {
1N/A sm_dprintf("sendtolist: QSELFREF ");
1N/A printaddr(sm_debug_file(), ctladdr, false);
1N/A }
1N/A ctladdr->q_flags |= QSELFREF;
1N/A }
1N/A
1N/A /* check for address loops */
1N/A b = self_reference(a);
1N/A if (b != NULL)
1N/A {
1N/A b->q_flags |= QSELFREF;
1N/A if (tTd(27, 5))
1N/A {
1N/A sm_dprintf("sendtolist: QSELFREF ");
1N/A printaddr(sm_debug_file(), b, false);
1N/A }
1N/A if (a != b)
1N/A {
1N/A if (tTd(27, 5))
1N/A {
1N/A sm_dprintf("sendtolist: QS_DONTSEND ");
1N/A printaddr(sm_debug_file(), a, false);
1N/A }
1N/A a->q_state = QS_DONTSEND;
1N/A b->q_flags |= a->q_flags & QNOTREMOTE;
1N/A continue;
1N/A }
1N/A }
1N/A
1N/A /* full name */
1N/A if (a->q_fullname == NULL)
1N/A a->q_fullname = ctladdr->q_fullname;
1N/A
1N/A /* various flag bits */
1N/A a->q_flags &= ~QINHERITEDBITS;
1N/A a->q_flags |= ctladdr->q_flags & QINHERITEDBITS;
1N/A
1N/A /* DSN recipient information */
1N/A a->q_finalrcpt = ctladdr->q_finalrcpt;
1N/A a->q_orcpt = ctladdr->q_orcpt;
1N/A }
1N/A
1N/A al = a;
1N/A }
1N/A
1N/A /* arrange to send to everyone on the local send list */
1N/A while (al != NULL)
1N/A {
1N/A register ADDRESS *a = al;
1N/A
1N/A al = a->q_next;
1N/A a = recipient(a, sendq, aliaslevel, e);
1N/A naddrs++;
1N/A }
1N/A }
1N/A SM_FINALLY
1N/A {
1N/A e->e_to = oldto;
1N/A if (bufp != buf)
1N/A sm_free(bufp);
1N/A macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
1N/A }
1N/A SM_END_TRY
1N/A return naddrs;
1N/A}
1N/A
1N/A#if MILTER
1N/A/*
1N/A** REMOVEFROMLIST -- Remove addresses from a send list.
1N/A**
1N/A** The parameter is a comma-separated list of recipients to remove.
1N/A** Note that it only deletes matching addresses. If those addresses
1N/A** have been expanded already in the sendq, it won't mark the
1N/A** expanded recipients as QS_REMOVED.
1N/A**
1N/A** Parameters:
1N/A** list -- the list to remove.
1N/A** sendq -- a pointer to the head of a queue to remove
1N/A** these addresses from.
1N/A** e -- the envelope in which to remove these recipients.
1N/A**
1N/A** Returns:
1N/A** The number of addresses removed from the list.
1N/A**
1N/A*/
1N/A
1N/Aint
1N/Aremovefromlist(list, sendq, e)
1N/A char *list;
1N/A ADDRESS **sendq;
1N/A ENVELOPE *e;
1N/A{
1N/A SM_NONVOLATILE char delimiter; /* the address delimiter */
1N/A SM_NONVOLATILE int naddrs;
1N/A SM_NONVOLATILE int i;
1N/A char *p;
1N/A char *oldto = e->e_to;
1N/A char *SM_NONVOLATILE bufp;
1N/A char buf[MAXNAME + 1];
1N/A
1N/A if (list == NULL)
1N/A {
1N/A syserr("removefromlist: null list");
1N/A return 0;
1N/A }
1N/A
1N/A if (tTd(25, 1))
1N/A sm_dprintf("removefromlist: %s\n", list);
1N/A
1N/A /* heuristic to determine old versus new style addresses */
1N/A if (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
1N/A strchr(list, '<') != NULL || strchr(list, '(') != NULL)
1N/A e->e_flags &= ~EF_OLDSTYLE;
1N/A delimiter = ' ';
1N/A if (!bitset(EF_OLDSTYLE, e->e_flags))
1N/A delimiter = ',';
1N/A
1N/A naddrs = 0;
1N/A
1N/A /* make sure we have enough space to copy the string */
1N/A i = strlen(list) + 1;
1N/A if (i <= sizeof(buf))
1N/A {
1N/A bufp = buf;
1N/A i = sizeof(buf);
1N/A }
1N/A else
1N/A bufp = sm_malloc_x(i);
1N/A
1N/A SM_TRY
1N/A {
1N/A (void) sm_strlcpy(bufp, denlstring(list, false, true), i);
1N/A
1N/A#if _FFR_ADDR_TYPE_MODES
1N/A if (AddrTypeModes)
1N/A macdefine(&e->e_macro, A_PERM, macid("{addr_type}"),
1N/A "e r d");
1N/A else
1N/A#endif /* _FFR_ADDR_TYPE_MODES */
1N/A macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e r");
1N/A for (p = bufp; *p != '\0'; )
1N/A {
1N/A ADDRESS a; /* parsed address to be removed */
1N/A ADDRESS *q;
1N/A ADDRESS **pq;
1N/A char *delimptr;
1N/A
1N/A /* parse the address */
1N/A while ((isascii(*p) && isspace(*p)) || *p == ',')
1N/A p++;
1N/A if (parseaddr(p, &a, RF_COPYALL|RF_RM_ADDR,
1N/A delimiter, &delimptr, e, true) == NULL)
1N/A {
1N/A p = delimptr;
1N/A continue;
1N/A }
1N/A p = delimptr;
1N/A for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
1N/A {
1N/A if (!QS_IS_DEAD(q->q_state) &&
1N/A (sameaddr(q, &a) ||
1N/A strcmp(q->q_paddr, a.q_paddr) == 0))
1N/A {
1N/A if (tTd(25, 5))
1N/A {
1N/A sm_dprintf("removefromlist: QS_REMOVED ");
1N/A printaddr(sm_debug_file(), &a, false);
1N/A }
1N/A q->q_state = QS_REMOVED;
1N/A naddrs++;
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A }
1N/A SM_FINALLY
1N/A {
1N/A e->e_to = oldto;
1N/A if (bufp != buf)
1N/A sm_free(bufp);
1N/A macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
1N/A }
1N/A SM_END_TRY
1N/A return naddrs;
1N/A}
1N/A#endif /* MILTER */
1N/A
1N/A/*
1N/A** RECIPIENT -- Designate a message recipient
1N/A** Saves the named person for future mailing (after some checks).
1N/A**
1N/A** Parameters:
1N/A** new -- the (preparsed) address header for the recipient.
1N/A** sendq -- a pointer to the head of a queue to put the
1N/A** recipient in. Duplicate suppression is done
1N/A** in this queue.
1N/A** aliaslevel -- the current alias nesting depth.
1N/A** e -- the current envelope.
1N/A**
1N/A** Returns:
1N/A** The actual address in the queue. This will be "a" if
1N/A** the address is not a duplicate, else the original address.
1N/A**
1N/A*/
1N/A
1N/AADDRESS *
1N/Arecipient(new, sendq, aliaslevel, e)
1N/A register ADDRESS *new;
1N/A register ADDRESS **sendq;
1N/A int aliaslevel;
1N/A register ENVELOPE *e;
1N/A{
1N/A register ADDRESS *q;
1N/A ADDRESS **pq;
1N/A ADDRESS **prev;
1N/A register struct mailer *m;
1N/A register char *p;
1N/A int i, buflen;
1N/A bool quoted; /* set if the addr has a quote bit */
1N/A bool insert;
1N/A int findusercount;
1N/A bool initialdontsend;
1N/A char *buf;
1N/A char buf0[MAXNAME + 1]; /* unquoted image of the user name */
1N/A sortfn_t *sortfn;
1N/A
1N/A p = NULL;
1N/A quoted = false;
1N/A insert = false;
1N/A findusercount = 0;
1N/A initialdontsend = QS_IS_DEAD(new->q_state);
1N/A e->e_to = new->q_paddr;
1N/A m = new->q_mailer;
1N/A errno = 0;
1N/A if (aliaslevel == 0)
1N/A new->q_flags |= QPRIMARY;
1N/A if (tTd(26, 1))
1N/A {
1N/A sm_dprintf("\nrecipient (%d): ", aliaslevel);
1N/A printaddr(sm_debug_file(), new, false);
1N/A }
1N/A
1N/A /* if this is primary, use it as original recipient */
1N/A if (new->q_alias == NULL)
1N/A {
1N/A if (e->e_origrcpt == NULL)
1N/A e->e_origrcpt = new->q_paddr;
1N/A else if (e->e_origrcpt != new->q_paddr)
1N/A e->e_origrcpt = "";
1N/A }
1N/A
1N/A /* find parent recipient for finalrcpt and orcpt */
1N/A for (q = new; q->q_alias != NULL; q = q->q_alias)
1N/A continue;
1N/A
1N/A /* find final recipient DSN address */
1N/A if (new->q_finalrcpt == NULL &&
1N/A e->e_from.q_mailer != NULL)
1N/A {
1N/A char frbuf[MAXLINE];
1N/A
1N/A p = e->e_from.q_mailer->m_addrtype;
1N/A if (p == NULL)
1N/A p = "rfc822";
1N/A if (sm_strcasecmp(p, "rfc822") != 0)
1N/A {
1N/A (void) sm_snprintf(frbuf, sizeof(frbuf), "%s; %.800s",
1N/A q->q_mailer->m_addrtype,
1N/A q->q_user);
1N/A }
1N/A else if (strchr(q->q_user, '@') != NULL)
1N/A {
1N/A (void) sm_snprintf(frbuf, sizeof(frbuf), "%s; %.800s",
1N/A p, q->q_user);
1N/A }
1N/A else if (strchr(q->q_paddr, '@') != NULL)
1N/A {
1N/A char *qp;
1N/A bool b;
1N/A
1N/A qp = q->q_paddr;
1N/A
1N/A /* strip brackets from address */
1N/A b = false;
1N/A if (*qp == '<')
1N/A {
1N/A b = qp[strlen(qp) - 1] == '>';
1N/A if (b)
1N/A qp[strlen(qp) - 1] = '\0';
1N/A qp++;
1N/A }
1N/A (void) sm_snprintf(frbuf, sizeof(frbuf), "%s; %.800s",
1N/A p, qp);
1N/A
1N/A /* undo damage */
1N/A if (b)
1N/A qp[strlen(qp)] = '>';
1N/A }
1N/A else
1N/A {
1N/A (void) sm_snprintf(frbuf, sizeof(frbuf),
1N/A "%s; %.700s@%.100s",
1N/A p, q->q_user, MyHostName);
1N/A }
1N/A new->q_finalrcpt = sm_rpool_strdup_x(e->e_rpool, frbuf);
1N/A }
1N/A
1N/A#if _FFR_GEN_ORCPT
1N/A /* set ORCPT DSN arg if not already set */
1N/A if (new->q_orcpt == NULL)
1N/A {
1N/A /* check for an existing ORCPT */
1N/A if (q->q_orcpt != NULL)
1N/A new->q_orcpt = q->q_orcpt;
1N/A else
1N/A {
1N/A /* make our own */
1N/A bool b = false;
1N/A char *qp;
1N/A char obuf[MAXLINE];
1N/A
1N/A if (e->e_from.q_mailer != NULL)
1N/A p = e->e_from.q_mailer->m_addrtype;
1N/A if (p == NULL)
1N/A p = "rfc822";
1N/A (void) sm_strlcpyn(obuf, sizeof(obuf), 2, p, ";");
1N/A
1N/A qp = q->q_paddr;
1N/A
1N/A /* FFR: Needs to strip comments from stdin addrs */
1N/A
1N/A /* strip brackets from address */
1N/A if (*qp == '<')
1N/A {
1N/A b = qp[strlen(qp) - 1] == '>';
1N/A if (b)
1N/A qp[strlen(qp) - 1] = '\0';
1N/A qp++;
1N/A }
1N/A
1N/A p = xtextify(denlstring(qp, true, false), "=");
1N/A
1N/A if (sm_strlcat(obuf, p, sizeof(obuf)) >= sizeof(obuf))
1N/A {
1N/A /* if too big, don't use it */
1N/A obuf[0] = '\0';
1N/A }
1N/A
1N/A /* undo damage */
1N/A if (b)
1N/A qp[strlen(qp)] = '>';
1N/A
1N/A if (obuf[0] != '\0')
1N/A new->q_orcpt =
1N/A sm_rpool_strdup_x(e->e_rpool, obuf);
1N/A }
1N/A }
1N/A#endif /* _FFR_GEN_ORCPT */
1N/A
1N/A /* break aliasing loops */
1N/A if (aliaslevel > MaxAliasRecursion)
1N/A {
1N/A new->q_state = QS_BADADDR;
1N/A new->q_status = "5.4.6";
1N/A if (new->q_alias != NULL)
1N/A {
1N/A new->q_alias->q_state = QS_BADADDR;
1N/A new->q_alias->q_status = "5.4.6";
1N/A }
1N/A if ((SuprErrs || !LogUsrErrs) && LogLevel > 0)
1N/A {
1N/A sm_syslog(LOG_ERR, e->e_id,
1N/A "aliasing/forwarding loop broken: %s (%d aliases deep; %d max)",
1N/A FileName != NULL ? FileName : "", aliaslevel,
1N/A MaxAliasRecursion);
1N/A }
1N/A usrerrenh(new->q_status,
1N/A "554 aliasing/forwarding loop broken (%d aliases deep; %d max)",
1N/A aliaslevel, MaxAliasRecursion);
1N/A return new;
1N/A }
1N/A
1N/A /*
1N/A ** Finish setting up address structure.
1N/A */
1N/A
1N/A /* get unquoted user for file, program or user.name check */
1N/A i = strlen(new->q_user);
1N/A if (i >= sizeof(buf0))
1N/A {
1N/A buflen = i + 1;
1N/A buf = xalloc(buflen);
1N/A }
1N/A else
1N/A {
1N/A buf = buf0;
1N/A buflen = sizeof(buf0);
1N/A }
1N/A (void) sm_strlcpy(buf, new->q_user, buflen);
1N/A for (p = buf; *p != '\0' && !quoted; p++)
1N/A {
1N/A if (*p == '\\')
1N/A quoted = true;
1N/A }
1N/A stripquotes(buf);
1N/A
1N/A /* check for direct mailing to restricted mailers */
1N/A if (m == ProgMailer)
1N/A {
1N/A if (new->q_alias == NULL || UseMSP ||
1N/A bitset(EF_UNSAFE, e->e_flags))
1N/A {
1N/A new->q_state = QS_BADADDR;
1N/A new->q_status = "5.7.1";
1N/A usrerrenh(new->q_status,
1N/A "550 Cannot mail directly to programs");
1N/A }
1N/A else if (bitset(QBOGUSSHELL, new->q_alias->q_flags))
1N/A {
1N/A new->q_state = QS_BADADDR;
1N/A new->q_status = "5.7.1";
1N/A if (new->q_alias->q_ruser == NULL)
1N/A usrerrenh(new->q_status,
1N/A "550 UID %d is an unknown user: cannot mail to programs",
1N/A new->q_alias->q_uid);
1N/A else
1N/A usrerrenh(new->q_status,
1N/A "550 User %s@%s doesn't have a valid shell for mailing to programs",
1N/A new->q_alias->q_ruser, MyHostName);
1N/A }
1N/A else if (bitset(QUNSAFEADDR, new->q_alias->q_flags))
1N/A {
1N/A new->q_state = QS_BADADDR;
1N/A new->q_status = "5.7.1";
1N/A new->q_rstatus = "550 Unsafe for mailing to programs";
1N/A usrerrenh(new->q_status,
1N/A "550 Address %s is unsafe for mailing to programs",
1N/A new->q_alias->q_paddr);
1N/A }
1N/A }
1N/A
1N/A /*
1N/A ** Look up this person in the recipient list.
1N/A ** If they are there already, return, otherwise continue.
1N/A ** If the list is empty, just add it. Notice the cute
1N/A ** hack to make from addresses suppress things correctly:
1N/A ** the QS_DUPLICATE state will be set in the send list.
1N/A ** [Please note: the emphasis is on "hack."]
1N/A */
1N/A
1N/A prev = NULL;
1N/A
1N/A /*
1N/A ** If this message is going to the queue or FastSplit is set
1N/A ** and it is the first try and the envelope hasn't split, then we
1N/A ** avoid doing an MX RR lookup now because one will be done when the
1N/A ** message is extracted from the queue later. It can go to the queue
1N/A ** because all messages are going to the queue or this mailer of
1N/A ** the current recipient is marked expensive.
1N/A */
1N/A
1N/A if (UseMSP || WILL_BE_QUEUED(e->e_sendmode) ||
1N/A (!bitset(EF_SPLIT, e->e_flags) && e->e_ntries == 0 &&
1N/A FastSplit > 0))
1N/A sortfn = sorthost;
1N/A else if (NoConnect && bitnset(M_EXPENSIVE, new->q_mailer->m_flags))
1N/A sortfn = sortexpensive;
1N/A else
1N/A sortfn = sortbysignature;
1N/A
1N/A for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
1N/A {
1N/A /*
1N/A ** If address is "less than" it should be inserted now.
1N/A ** If address is "greater than" current comparison it'll
1N/A ** insert later in the list; so loop again (if possible).
1N/A ** If address is "equal" (different equal than sameaddr()
1N/A ** call) then check if sameaddr() will be true.
1N/A ** Because this list is now sorted, it'll mean fewer
1N/A ** comparisons and fewer loops which is important for more
1N/A ** recipients.
1N/A */
1N/A
1N/A i = (*sortfn)(new, q);
1N/A if (i == 0) /* equal */
1N/A {
1N/A /*
1N/A ** Sortbysignature() has said that the two have
1N/A ** equal MX RR's and the same user. Calling sameaddr()
1N/A ** now checks if the two hosts are as identical as the
1N/A ** MX RR's are (which might not be the case)
1N/A ** before saying these are the identical addresses.
1N/A */
1N/A
1N/A if (sameaddr(q, new) &&
1N/A (bitset(QRCPTOK, q->q_flags) ||
1N/A !bitset(QPRIMARY, q->q_flags)))
1N/A {
1N/A if (tTd(26, 1))
1N/A {
1N/A sm_dprintf("%s in sendq: ",
1N/A new->q_paddr);
1N/A printaddr(sm_debug_file(), q, false);
1N/A }
1N/A if (!bitset(QPRIMARY, q->q_flags))
1N/A {
1N/A if (!QS_IS_DEAD(new->q_state))
1N/A message("duplicate suppressed");
1N/A else
1N/A q->q_state = QS_DUPLICATE;
1N/A q->q_flags |= new->q_flags;
1N/A }
1N/A else if (bitset(QSELFREF, q->q_flags)
1N/A || q->q_state == QS_REMOVED)
1N/A {
1N/A /*
1N/A ** If an earlier milter removed the
1N/A ** address, a later one can still add
1N/A ** it back.
1N/A */
1N/A
1N/A q->q_state = new->q_state;
1N/A q->q_flags |= new->q_flags;
1N/A }
1N/A new = q;
1N/A goto done;
1N/A }
1N/A }
1N/A else if (i < 0) /* less than */
1N/A {
1N/A insert = true;
1N/A break;
1N/A }
1N/A prev = pq;
1N/A }
1N/A
1N/A /* pq should point to an address, never NULL */
1N/A SM_ASSERT(pq != NULL);
1N/A
1N/A /* add address on list */
1N/A if (insert)
1N/A {
1N/A /*
1N/A ** insert before 'pq'. Only possible when at least 1
1N/A ** ADDRESS is in the list already.
1N/A */
1N/A
1N/A new->q_next = *pq;
1N/A if (prev == NULL)
1N/A *sendq = new; /* To be the first ADDRESS */
1N/A else
1N/A (*prev)->q_next = new;
1N/A }
1N/A else
1N/A {
1N/A /*
1N/A ** Place in list at current 'pq' position. Possible
1N/A ** when there are 0 or more ADDRESS's in the list.
1N/A */
1N/A
1N/A new->q_next = NULL;
1N/A *pq = new;
1N/A }
1N/A
1N/A /* added a new address: clear split flag */
1N/A e->e_flags &= ~EF_SPLIT;
1N/A
1N/A /*
1N/A ** Alias the name and handle special mailer types.
1N/A */
1N/A
1N/A trylocaluser:
1N/A if (tTd(29, 7))
1N/A {
1N/A sm_dprintf("at trylocaluser: ");
1N/A printaddr(sm_debug_file(), new, false);
1N/A }
1N/A
1N/A if (!QS_IS_OK(new->q_state))
1N/A {
1N/A if (QS_IS_UNDELIVERED(new->q_state))
1N/A e->e_nrcpts++;
1N/A goto testselfdestruct;
1N/A }
1N/A
1N/A if (m == InclMailer)
1N/A {
1N/A new->q_state = QS_INCLUDED;
1N/A if (new->q_alias == NULL || UseMSP ||
1N/A bitset(EF_UNSAFE, e->e_flags))
1N/A {
1N/A new->q_state = QS_BADADDR;
1N/A new->q_status = "5.7.1";
1N/A usrerrenh(new->q_status,
1N/A "550 Cannot mail directly to :include:s");
1N/A }
1N/A else
1N/A {
1N/A int ret;
1N/A
1N/A message("including file %s", new->q_user);
1N/A ret = include(new->q_user, false, new,
1N/A sendq, aliaslevel, e);
1N/A if (transienterror(ret))
1N/A {
1N/A if (LogLevel > 2)
1N/A sm_syslog(LOG_ERR, e->e_id,
1N/A "include %s: transient error: %s",
1N/A shortenstring(new->q_user,
1N/A MAXSHORTSTR),
1N/A sm_errstring(ret));
1N/A new->q_state = QS_QUEUEUP;
1N/A usrerr("451 4.2.4 Cannot open %s: %s",
1N/A shortenstring(new->q_user,
1N/A MAXSHORTSTR),
1N/A sm_errstring(ret));
1N/A }
1N/A else if (ret != 0)
1N/A {
1N/A new->q_state = QS_BADADDR;
1N/A new->q_status = "5.2.4";
1N/A usrerrenh(new->q_status,
1N/A "550 Cannot open %s: %s",
1N/A shortenstring(new->q_user,
1N/A MAXSHORTSTR),
1N/A sm_errstring(ret));
1N/A }
1N/A }
1N/A }
1N/A else if (m == FileMailer)
1N/A {
1N/A /* check if allowed */
1N/A if (new->q_alias == NULL || UseMSP ||
1N/A bitset(EF_UNSAFE, e->e_flags))
1N/A {
1N/A new->q_state = QS_BADADDR;
1N/A new->q_status = "5.7.1";
1N/A usrerrenh(new->q_status,
1N/A "550 Cannot mail directly to files");
1N/A }
1N/A else if (bitset(QBOGUSSHELL, new->q_alias->q_flags))
1N/A {
1N/A new->q_state = QS_BADADDR;
1N/A new->q_status = "5.7.1";
1N/A if (new->q_alias->q_ruser == NULL)
1N/A usrerrenh(new->q_status,
1N/A "550 UID %d is an unknown user: cannot mail to files",
1N/A new->q_alias->q_uid);
1N/A else
1N/A usrerrenh(new->q_status,
1N/A "550 User %s@%s doesn't have a valid shell for mailing to files",
1N/A new->q_alias->q_ruser, MyHostName);
1N/A }
1N/A else if (bitset(QUNSAFEADDR, new->q_alias->q_flags))
1N/A {
1N/A new->q_state = QS_BADADDR;
1N/A new->q_status = "5.7.1";
1N/A new->q_rstatus = "550 Unsafe for mailing to files";
1N/A usrerrenh(new->q_status,
1N/A "550 Address %s is unsafe for mailing to files",
1N/A new->q_alias->q_paddr);
1N/A }
1N/A }
1N/A
1N/A /* try aliasing */
1N/A if (!quoted && QS_IS_OK(new->q_state) &&
1N/A bitnset(M_ALIASABLE, m->m_flags))
1N/A alias(new, sendq, aliaslevel, e);
1N/A
1N/A#if USERDB
1N/A /* if not aliased, look it up in the user database */
1N/A if (!bitset(QNOTREMOTE, new->q_flags) &&
1N/A QS_IS_SENDABLE(new->q_state) &&
1N/A bitnset(M_CHECKUDB, m->m_flags))
1N/A {
1N/A if (udbexpand(new, sendq, aliaslevel, e) == EX_TEMPFAIL)
1N/A {
1N/A new->q_state = QS_QUEUEUP;
1N/A if (e->e_message == NULL)
1N/A e->e_message = sm_rpool_strdup_x(e->e_rpool,
1N/A "Deferred: user database error");
1N/A if (new->q_message == NULL)
1N/A new->q_message = "Deferred: user database error";
1N/A if (LogLevel > 8)
1N/A sm_syslog(LOG_INFO, e->e_id,
1N/A "deferred: udbexpand: %s",
1N/A sm_errstring(errno));
1N/A message("queued (user database error): %s",
1N/A sm_errstring(errno));
1N/A e->e_nrcpts++;
1N/A goto testselfdestruct;
1N/A }
1N/A }
1N/A#endif /* USERDB */
1N/A
1N/A /*
1N/A ** If we have a level two config file, then pass the name through
1N/A ** Ruleset 5 before sending it off. Ruleset 5 has the right
1N/A ** to rewrite it to another mailer. This gives us a hook
1N/A ** after local aliasing has been done.
1N/A */
1N/A
1N/A if (tTd(29, 5))
1N/A {
1N/A sm_dprintf("recipient: testing local? cl=%d, rr5=%p\n\t",
1N/A ConfigLevel, RewriteRules[5]);
1N/A printaddr(sm_debug_file(), new, false);
1N/A }
1N/A if (ConfigLevel >= 2 && RewriteRules[5] != NULL &&
1N/A bitnset(M_TRYRULESET5, m->m_flags) &&
1N/A !bitset(QNOTREMOTE, new->q_flags) &&
1N/A QS_IS_OK(new->q_state))
1N/A {
1N/A maplocaluser(new, sendq, aliaslevel + 1, e);
1N/A }
1N/A
1N/A /*
1N/A ** If it didn't get rewritten to another mailer, go ahead
1N/A ** and deliver it.
1N/A */
1N/A
1N/A if (QS_IS_OK(new->q_state) &&
1N/A bitnset(M_HASPWENT, m->m_flags))
1N/A {
1N/A auto bool fuzzy;
1N/A SM_MBDB_T user;
1N/A int status;
1N/A
1N/A /* warning -- finduser may trash buf */
1N/A status = finduser(buf, &fuzzy, &user);
1N/A switch (status)
1N/A {
1N/A case EX_TEMPFAIL:
1N/A new->q_state = QS_QUEUEUP;
1N/A new->q_status = "4.5.2";
1N/A giveresponse(EX_TEMPFAIL, new->q_status, m, NULL,
1N/A new->q_alias, (time_t) 0, e, new);
1N/A break;
1N/A default:
1N/A new->q_state = QS_BADADDR;
1N/A new->q_status = "5.1.1";
1N/A new->q_rstatus = "550 5.1.1 User unknown";
1N/A giveresponse(EX_NOUSER, new->q_status, m, NULL,
1N/A new->q_alias, (time_t) 0, e, new);
1N/A break;
1N/A case EX_OK:
1N/A if (fuzzy)
1N/A {
1N/A /* name was a fuzzy match */
1N/A new->q_user = sm_rpool_strdup_x(e->e_rpool,
1N/A user.mbdb_name);
1N/A if (findusercount++ > 3)
1N/A {
1N/A new->q_state = QS_BADADDR;
1N/A new->q_status = "5.4.6";
1N/A usrerrenh(new->q_status,
1N/A "554 aliasing/forwarding loop for %s broken",
1N/A user.mbdb_name);
1N/A goto done;
1N/A }
1N/A
1N/A /* see if it aliases */
1N/A (void) sm_strlcpy(buf, user.mbdb_name, buflen);
1N/A goto trylocaluser;
1N/A }
1N/A if (*user.mbdb_homedir == '\0')
1N/A new->q_home = NULL;
1N/A else if (strcmp(user.mbdb_homedir, "/") == 0)
1N/A new->q_home = "";
1N/A else
1N/A new->q_home = sm_rpool_strdup_x(e->e_rpool,
1N/A user.mbdb_homedir);
1N/A if (user.mbdb_uid != SM_NO_UID)
1N/A {
1N/A new->q_uid = user.mbdb_uid;
1N/A new->q_gid = user.mbdb_gid;
1N/A new->q_flags |= QGOODUID;
1N/A }
1N/A new->q_ruser = sm_rpool_strdup_x(e->e_rpool,
1N/A user.mbdb_name);
1N/A if (user.mbdb_fullname[0] != '\0')
1N/A new->q_fullname = sm_rpool_strdup_x(e->e_rpool,
1N/A user.mbdb_fullname);
1N/A if (!usershellok(user.mbdb_name, user.mbdb_shell))
1N/A {
1N/A new->q_flags |= QBOGUSSHELL;
1N/A }
1N/A if (bitset(EF_VRFYONLY, e->e_flags))
1N/A {
1N/A /* don't do any more now */
1N/A new->q_state = QS_VERIFIED;
1N/A }
1N/A else if (!quoted)
1N/A forward(new, sendq, aliaslevel, e);
1N/A }
1N/A }
1N/A if (!QS_IS_DEAD(new->q_state))
1N/A e->e_nrcpts++;
1N/A
1N/A testselfdestruct:
1N/A new->q_flags |= QTHISPASS;
1N/A if (tTd(26, 8))
1N/A {
1N/A sm_dprintf("testselfdestruct: ");
1N/A printaddr(sm_debug_file(), new, false);
1N/A if (tTd(26, 10))
1N/A {
1N/A sm_dprintf("SENDQ:\n");
1N/A printaddr(sm_debug_file(), *sendq, true);
1N/A sm_dprintf("----\n");
1N/A }
1N/A }
1N/A if (new->q_alias == NULL && new != &e->e_from &&
1N/A QS_IS_DEAD(new->q_state))
1N/A {
1N/A for (q = *sendq; q != NULL; q = q->q_next)
1N/A {
1N/A if (!QS_IS_DEAD(q->q_state))
1N/A break;
1N/A }
1N/A if (q == NULL)
1N/A {
1N/A new->q_state = QS_BADADDR;
1N/A new->q_status = "5.4.6";
1N/A usrerrenh(new->q_status,
1N/A "554 aliasing/forwarding loop broken");
1N/A }
1N/A }
1N/A
1N/A done:
1N/A new->q_flags |= QTHISPASS;
1N/A if (buf != buf0)
1N/A sm_free(buf); /* XXX leak if above code raises exception */
1N/A
1N/A /*
1N/A ** If we are at the top level, check to see if this has
1N/A ** expanded to exactly one address. If so, it can inherit
1N/A ** the primaryness of the address.
1N/A **
1N/A ** While we're at it, clear the QTHISPASS bits.
1N/A */
1N/A
1N/A if (aliaslevel == 0)
1N/A {
1N/A int nrcpts = 0;
1N/A ADDRESS *only = NULL;
1N/A
1N/A for (q = *sendq; q != NULL; q = q->q_next)
1N/A {
1N/A if (bitset(QTHISPASS, q->q_flags) &&
1N/A QS_IS_SENDABLE(q->q_state))
1N/A {
1N/A nrcpts++;
1N/A only = q;
1N/A }
1N/A q->q_flags &= ~QTHISPASS;
1N/A }
1N/A if (nrcpts == 1)
1N/A {
1N/A /* check to see if this actually got a new owner */
1N/A q = only;
1N/A while ((q = q->q_alias) != NULL)
1N/A {
1N/A if (q->q_owner != NULL)
1N/A break;
1N/A }
1N/A if (q == NULL)
1N/A only->q_flags |= QPRIMARY;
1N/A }
1N/A else if (!initialdontsend && nrcpts > 0)
1N/A {
1N/A /* arrange for return receipt */
1N/A e->e_flags |= EF_SENDRECEIPT;
1N/A new->q_flags |= QEXPANDED;
1N/A if (e->e_xfp != NULL &&
1N/A bitset(QPINGONSUCCESS, new->q_flags))
1N/A (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
1N/A "%s... expanded to multiple addresses\n",
1N/A new->q_paddr);
1N/A }
1N/A }
1N/A new->q_flags |= QRCPTOK;
1N/A (void) sm_snprintf(buf0, sizeof(buf0), "%d", e->e_nrcpts);
1N/A macdefine(&e->e_macro, A_TEMP, macid("{nrcpts}"), buf0);
1N/A return new;
1N/A}
1N/A
1N/A/*
1N/A** FINDUSER -- find the password entry for a user.
1N/A**
1N/A** This looks a lot like getpwnam, except that it may want to
1N/A** do some fancier pattern matching in /etc/passwd.
1N/A**
1N/A** This routine contains most of the time of many sendmail runs.
1N/A** It deserves to be optimized.
1N/A**
1N/A** Parameters:
1N/A** name -- the name to match against.
1N/A** fuzzyp -- an outarg that is set to true if this entry
1N/A** was found using the fuzzy matching algorithm;
1N/A** set to false otherwise.
1N/A** user -- structure to fill in if user is found
1N/A**
1N/A** Returns:
1N/A** On success, fill in *user, set *fuzzyp and return EX_OK.
1N/A** If the user was not found, return EX_NOUSER.
1N/A** On error, return EX_TEMPFAIL or EX_OSERR.
1N/A**
1N/A** Side Effects:
1N/A** may modify name.
1N/A*/
1N/A
1N/Aint
1N/Afinduser(name, fuzzyp, user)
1N/A char *name;
1N/A bool *fuzzyp;
1N/A SM_MBDB_T *user;
1N/A{
1N/A#if MATCHGECOS
1N/A register struct passwd *pw;
1N/A#endif /* MATCHGECOS */
1N/A register char *p;
1N/A bool tryagain;
1N/A int status;
1N/A
1N/A if (tTd(29, 4))
1N/A sm_dprintf("finduser(%s): ", name);
1N/A
1N/A *fuzzyp = false;
1N/A
1N/A#if HESIOD
1N/A /* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */
1N/A for (p = name; *p != '\0'; p++)
1N/A if (!isascii(*p) || !isdigit(*p))
1N/A break;
1N/A if (*p == '\0')
1N/A {
1N/A if (tTd(29, 4))
1N/A sm_dprintf("failed (numeric input)\n");
1N/A return EX_NOUSER;
1N/A }
1N/A#endif /* HESIOD */
1N/A
1N/A /* look up this login name using fast path */
1N/A status = sm_mbdb_lookup(name, user);
1N/A if (status != EX_NOUSER)
1N/A {
1N/A if (tTd(29, 4))
1N/A sm_dprintf("%s (non-fuzzy)\n", sm_strexit(status));
1N/A return status;
1N/A }
1N/A
1N/A /* try mapping it to lower case */
1N/A tryagain = false;
1N/A for (p = name; *p != '\0'; p++)
1N/A {
1N/A if (isascii(*p) && isupper(*p))
1N/A {
1N/A *p = tolower(*p);
1N/A tryagain = true;
1N/A }
1N/A }
1N/A if (tryagain && (status = sm_mbdb_lookup(name, user)) != EX_NOUSER)
1N/A {
1N/A if (tTd(29, 4))
1N/A sm_dprintf("%s (lower case)\n", sm_strexit(status));
1N/A *fuzzyp = true;
1N/A return status;
1N/A }
1N/A
1N/A#if MATCHGECOS
1N/A /* see if fuzzy matching allowed */
1N/A if (!MatchGecos)
1N/A {
1N/A if (tTd(29, 4))
1N/A sm_dprintf("not found (fuzzy disabled)\n");
1N/A return EX_NOUSER;
1N/A }
1N/A
1N/A /* search for a matching full name instead */
1N/A for (p = name; *p != '\0'; p++)
1N/A {
1N/A if (*p == (SpaceSub & 0177) || *p == '_')
1N/A *p = ' ';
1N/A }
1N/A (void) setpwent();
1N/A while ((pw = getpwent()) != NULL)
1N/A {
1N/A char buf[MAXNAME + 1];
1N/A
1N/A# if 0
1N/A if (sm_strcasecmp(pw->pw_name, name) == 0)
1N/A {
1N/A if (tTd(29, 4))
1N/A sm_dprintf("found (case wrapped)\n");
1N/A break;
1N/A }
1N/A# endif /* 0 */
1N/A
1N/A sm_pwfullname(pw->pw_gecos, pw->pw_name, buf, sizeof(buf));
1N/A if (strchr(buf, ' ') != NULL && sm_strcasecmp(buf, name) == 0)
1N/A {
1N/A if (tTd(29, 4))
1N/A sm_dprintf("fuzzy matches %s\n", pw->pw_name);
1N/A message("sending to login name %s", pw->pw_name);
1N/A break;
1N/A }
1N/A }
1N/A if (pw != NULL)
1N/A *fuzzyp = true;
1N/A else if (tTd(29, 4))
1N/A sm_dprintf("no fuzzy match found\n");
1N/A# if DEC_OSF_BROKEN_GETPWENT /* DEC OSF/1 3.2 or earlier */
1N/A endpwent();
1N/A# endif /* DEC_OSF_BROKEN_GETPWENT */
1N/A if (pw == NULL)
1N/A return EX_NOUSER;
1N/A sm_mbdb_frompw(user, pw);
1N/A return EX_OK;
1N/A#else /* MATCHGECOS */
1N/A if (tTd(29, 4))
1N/A sm_dprintf("not found (fuzzy disabled)\n");
1N/A return EX_NOUSER;
1N/A#endif /* MATCHGECOS */
1N/A}
1N/A
1N/A/*
1N/A** WRITABLE -- predicate returning if the file is writable.
1N/A**
1N/A** This routine must duplicate the algorithm in sys/fio.c.
1N/A** Unfortunately, we cannot use the access call since we
1N/A** won't necessarily be the real uid when we try to
1N/A** actually open the file.
1N/A**
1N/A** Notice that ANY file with ANY execute bit is automatically
1N/A** not writable. This is also enforced by mailfile.
1N/A**
1N/A** Parameters:
1N/A** filename -- the file name to check.
1N/A** ctladdr -- the controlling address for this file.
1N/A** flags -- SFF_* flags to control the function.
1N/A**
1N/A** Returns:
1N/A** true -- if we will be able to write this file.
1N/A** false -- if we cannot write this file.
1N/A**
1N/A** Side Effects:
1N/A** none.
1N/A*/
1N/A
1N/Abool
1N/Awritable(filename, ctladdr, flags)
1N/A char *filename;
1N/A ADDRESS *ctladdr;
1N/A long flags;
1N/A{
1N/A uid_t euid = 0;
1N/A gid_t egid = 0;
1N/A char *user = NULL;
1N/A
1N/A if (tTd(44, 5))
1N/A sm_dprintf("writable(%s, 0x%lx)\n", filename, flags);
1N/A
1N/A /*
1N/A ** File does exist -- check that it is writable.
1N/A */
1N/A
1N/A if (geteuid() != 0)
1N/A {
1N/A euid = geteuid();
1N/A egid = getegid();
1N/A user = NULL;
1N/A }
1N/A else if (ctladdr != NULL)
1N/A {
1N/A euid = ctladdr->q_uid;
1N/A egid = ctladdr->q_gid;
1N/A user = ctladdr->q_user;
1N/A }
1N/A else if (bitset(SFF_RUNASREALUID, flags))
1N/A {
1N/A euid = RealUid;
1N/A egid = RealGid;
1N/A user = RealUserName;
1N/A }
1N/A else if (FileMailer != NULL && !bitset(SFF_ROOTOK, flags))
1N/A {
1N/A if (FileMailer->m_uid == NO_UID)
1N/A {
1N/A euid = DefUid;
1N/A user = DefUser;
1N/A }
1N/A else
1N/A {
1N/A euid = FileMailer->m_uid;
1N/A user = NULL;
1N/A }
1N/A if (FileMailer->m_gid == NO_GID)
1N/A egid = DefGid;
1N/A else
1N/A egid = FileMailer->m_gid;
1N/A }
1N/A else
1N/A {
1N/A euid = egid = 0;
1N/A user = NULL;
1N/A }
1N/A if (!bitset(SFF_ROOTOK, flags))
1N/A {
1N/A if (euid == 0)
1N/A {
1N/A euid = DefUid;
1N/A user = DefUser;
1N/A }
1N/A if (egid == 0)
1N/A egid = DefGid;
1N/A }
1N/A if (geteuid() == 0 &&
1N/A (ctladdr == NULL || !bitset(QGOODUID, ctladdr->q_flags)))
1N/A flags |= SFF_SETUIDOK;
1N/A
1N/A if (!bitnset(DBS_FILEDELIVERYTOSYMLINK, DontBlameSendmail))
1N/A flags |= SFF_NOSLINK;
1N/A if (!bitnset(DBS_FILEDELIVERYTOHARDLINK, DontBlameSendmail))
1N/A flags |= SFF_NOHLINK;
1N/A
1N/A errno = safefile(filename, euid, egid, user, flags, S_IWRITE, NULL);
1N/A return errno == 0;
1N/A}
1N/A
1N/A/*
1N/A** INCLUDE -- handle :include: specification.
1N/A**
1N/A** Parameters:
1N/A** fname -- filename to include.
1N/A** forwarding -- if true, we are reading a .forward file.
1N/A** if false, it's a :include: file.
1N/A** ctladdr -- address template to use to fill in these
1N/A** addresses -- effective user/group id are
1N/A** the important things.
1N/A** sendq -- a pointer to the head of the send queue
1N/A** to put these addresses in.
1N/A** aliaslevel -- the alias nesting depth.
1N/A** e -- the current envelope.
1N/A**
1N/A** Returns:
1N/A** open error status
1N/A**
1N/A** Side Effects:
1N/A** reads the :include: file and sends to everyone
1N/A** listed in that file.
1N/A**
1N/A** Security Note:
1N/A** If you have restricted chown (that is, you can't
1N/A** give a file away), it is reasonable to allow programs
1N/A** and files called from this :include: file to be to be
1N/A** run as the owner of the :include: file. This is bogus
1N/A** if there is any chance of someone giving away a file.
1N/A** We assume that pre-POSIX systems can give away files.
1N/A**
1N/A** There is an additional restriction that if you
1N/A** forward to a :include: file, it will not take on
1N/A** the ownership of the :include: file. This may not
1N/A** be necessary, but shouldn't hurt.
1N/A*/
1N/A
1N/Astatic jmp_buf CtxIncludeTimeout;
1N/A
1N/Aint
1N/Ainclude(fname, forwarding, ctladdr, sendq, aliaslevel, e)
1N/A char *fname;
1N/A bool forwarding;
1N/A ADDRESS *ctladdr;
1N/A ADDRESS **sendq;
1N/A int aliaslevel;
1N/A ENVELOPE *e;
1N/A{
1N/A SM_FILE_T *volatile fp = NULL;
1N/A char *oldto = e->e_to;
1N/A char *oldfilename = FileName;
1N/A int oldlinenumber = LineNumber;
1N/A register SM_EVENT *ev = NULL;
1N/A int nincludes;
1N/A int mode;
1N/A volatile bool maxreached = false;
1N/A register ADDRESS *ca;
1N/A volatile uid_t saveduid;
1N/A volatile gid_t savedgid;
1N/A volatile uid_t uid;
1N/A volatile gid_t gid;
1N/A char *volatile user;
1N/A int rval = 0;
1N/A volatile long sfflags = SFF_REGONLY;
1N/A register char *p;
1N/A bool safechown = false;
1N/A volatile bool safedir = false;
1N/A struct stat st;
1N/A char buf[MAXLINE];
1N/A
1N/A if (tTd(27, 2))
1N/A sm_dprintf("include(%s)\n", fname);
1N/A if (tTd(27, 4))
1N/A sm_dprintf(" ruid=%d euid=%d\n",
1N/A (int) getuid(), (int) geteuid());
1N/A if (tTd(27, 14))
1N/A {
1N/A sm_dprintf("ctladdr ");
1N/A printaddr(sm_debug_file(), ctladdr, false);
1N/A }
1N/A
1N/A if (tTd(27, 9))
1N/A sm_dprintf("include: old uid = %d/%d\n",
1N/A (int) getuid(), (int) geteuid());
1N/A
1N/A if (forwarding)
1N/A {
1N/A sfflags |= SFF_MUSTOWN|SFF_ROOTOK;
1N/A if (!bitnset(DBS_GROUPWRITABLEFORWARDFILE, DontBlameSendmail))
1N/A sfflags |= SFF_NOGWFILES;
1N/A if (!bitnset(DBS_WORLDWRITABLEFORWARDFILE, DontBlameSendmail))
1N/A sfflags |= SFF_NOWWFILES;
1N/A }
1N/A else
1N/A {
1N/A if (!bitnset(DBS_GROUPWRITABLEINCLUDEFILE, DontBlameSendmail))
1N/A sfflags |= SFF_NOGWFILES;
1N/A if (!bitnset(DBS_WORLDWRITABLEINCLUDEFILE, DontBlameSendmail))
1N/A sfflags |= SFF_NOWWFILES;
1N/A }
1N/A
1N/A /*
1N/A ** If RunAsUser set, won't be able to run programs as user
1N/A ** so mark them as unsafe unless the administrator knows better.
1N/A */
1N/A
1N/A if ((geteuid() != 0 || RunAsUid != 0) &&
1N/A !bitnset(DBS_NONROOTSAFEADDR, DontBlameSendmail))
1N/A {
1N/A if (tTd(27, 4))
1N/A sm_dprintf("include: not safe (euid=%d, RunAsUid=%d)\n",
1N/A (int) geteuid(), (int) RunAsUid);
1N/A ctladdr->q_flags |= QUNSAFEADDR;
1N/A }
1N/A
1N/A ca = getctladdr(ctladdr);
1N/A if (ca == NULL ||
1N/A (ca->q_uid == DefUid && ca->q_gid == 0))
1N/A {
1N/A uid = DefUid;
1N/A gid = DefGid;
1N/A user = DefUser;
1N/A }
1N/A else
1N/A {
1N/A uid = ca->q_uid;
1N/A gid = ca->q_gid;
1N/A user = ca->q_user;
1N/A }
1N/A#if MAILER_SETUID_METHOD != USE_SETUID
1N/A saveduid = geteuid();
1N/A savedgid = getegid();
1N/A if (saveduid == 0)
1N/A {
1N/A if (!DontInitGroups)
1N/A {
1N/A if (initgroups(user, gid) == -1)
1N/A {
1N/A rval = EAGAIN;
1N/A syserr("include: initgroups(%s, %d) failed",
1N/A user, gid);
1N/A goto resetuid;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A GIDSET_T gidset[1];
1N/A
1N/A gidset[0] = gid;
1N/A if (setgroups(1, gidset) == -1)
1N/A {
1N/A rval = EAGAIN;
1N/A syserr("include: setgroups() failed");
1N/A goto resetuid;
1N/A }
1N/A }
1N/A
1N/A if (gid != 0 && setgid(gid) < -1)
1N/A {
1N/A rval = EAGAIN;
1N/A syserr("setgid(%d) failure", gid);
1N/A goto resetuid;
1N/A }
1N/A if (uid != 0)
1N/A {
1N/A# if MAILER_SETUID_METHOD == USE_SETEUID
1N/A if (seteuid(uid) < 0)
1N/A {
1N/A rval = EAGAIN;
1N/A syserr("seteuid(%d) failure (real=%d, eff=%d)",
1N/A uid, (int) getuid(), (int) geteuid());
1N/A goto resetuid;
1N/A }
1N/A# endif /* MAILER_SETUID_METHOD == USE_SETEUID */
1N/A# if MAILER_SETUID_METHOD == USE_SETREUID
1N/A if (setreuid(0, uid) < 0)
1N/A {
1N/A rval = EAGAIN;
1N/A syserr("setreuid(0, %d) failure (real=%d, eff=%d)",
1N/A uid, (int) getuid(), (int) geteuid());
1N/A goto resetuid;
1N/A }
1N/A# endif /* MAILER_SETUID_METHOD == USE_SETREUID */
1N/A }
1N/A }
1N/A#endif /* MAILER_SETUID_METHOD != USE_SETUID */
1N/A
1N/A if (tTd(27, 9))
1N/A sm_dprintf("include: new uid = %d/%d\n",
1N/A (int) getuid(), (int) geteuid());
1N/A
1N/A /*
1N/A ** If home directory is remote mounted but server is down,
1N/A ** this can hang or give errors; use a timeout to avoid this
1N/A */
1N/A
1N/A if (setjmp(CtxIncludeTimeout) != 0)
1N/A {
1N/A ctladdr->q_state = QS_QUEUEUP;
1N/A errno = 0;
1N/A
1N/A /* return pseudo-error code */
1N/A rval = E_SM_OPENTIMEOUT;
1N/A goto resetuid;
1N/A }
1N/A if (TimeOuts.to_fileopen > 0)
1N/A ev = sm_setevent(TimeOuts.to_fileopen, includetimeout, 0);
1N/A else
1N/A ev = NULL;
1N/A
1N/A
1N/A /* check for writable parent directory */
1N/A p = strrchr(fname, '/');
1N/A if (p != NULL)
1N/A {
1N/A int ret;
1N/A
1N/A *p = '\0';
1N/A ret = safedirpath(fname, uid, gid, user,
1N/A sfflags|SFF_SAFEDIRPATH, 0, 0);
1N/A if (ret == 0)
1N/A {
1N/A /* in safe directory: relax chown & link rules */
1N/A safedir = true;
1N/A sfflags |= SFF_NOPATHCHECK;
1N/A }
1N/A else
1N/A {
1N/A if (bitnset((forwarding ?
1N/A DBS_FORWARDFILEINUNSAFEDIRPATH :
1N/A DBS_INCLUDEFILEINUNSAFEDIRPATH),
1N/A DontBlameSendmail))
1N/A sfflags |= SFF_NOPATHCHECK;
1N/A else if (bitnset((forwarding ?
1N/A DBS_FORWARDFILEINGROUPWRITABLEDIRPATH :
1N/A DBS_INCLUDEFILEINGROUPWRITABLEDIRPATH),
1N/A DontBlameSendmail) &&
1N/A ret == E_SM_GWDIR)
1N/A {
1N/A setbitn(DBS_GROUPWRITABLEDIRPATHSAFE,
1N/A DontBlameSendmail);
1N/A ret = safedirpath(fname, uid, gid, user,
1N/A sfflags|SFF_SAFEDIRPATH,
1N/A 0, 0);
1N/A clrbitn(DBS_GROUPWRITABLEDIRPATHSAFE,
1N/A DontBlameSendmail);
1N/A if (ret == 0)
1N/A sfflags |= SFF_NOPATHCHECK;
1N/A else
1N/A sfflags |= SFF_SAFEDIRPATH;
1N/A }
1N/A else
1N/A sfflags |= SFF_SAFEDIRPATH;
1N/A if (ret > E_PSEUDOBASE &&
1N/A !bitnset((forwarding ?
1N/A DBS_FORWARDFILEINUNSAFEDIRPATHSAFE :
1N/A DBS_INCLUDEFILEINUNSAFEDIRPATHSAFE),
1N/A DontBlameSendmail))
1N/A {
1N/A if (LogLevel > 11)
1N/A sm_syslog(LOG_INFO, e->e_id,
1N/A "%s: unsafe directory path, marked unsafe",
1N/A shortenstring(fname, MAXSHORTSTR));
1N/A ctladdr->q_flags |= QUNSAFEADDR;
1N/A }
1N/A }
1N/A *p = '/';
1N/A }
1N/A
1N/A /* allow links only in unwritable directories */
1N/A if (!safedir &&
1N/A !bitnset((forwarding ?
1N/A DBS_LINKEDFORWARDFILEINWRITABLEDIR :
1N/A DBS_LINKEDINCLUDEFILEINWRITABLEDIR),
1N/A DontBlameSendmail))
1N/A sfflags |= SFF_NOLINK;
1N/A
1N/A rval = safefile(fname, uid, gid, user, sfflags, S_IREAD, &st);
1N/A if (rval != 0)
1N/A {
1N/A /* don't use this :include: file */
1N/A if (tTd(27, 4))
1N/A sm_dprintf("include: not safe (uid=%d): %s\n",
1N/A (int) uid, sm_errstring(rval));
1N/A }
1N/A else if ((fp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, fname,
1N/A SM_IO_RDONLY, NULL)) == NULL)
1N/A {
1N/A rval = errno;
1N/A if (tTd(27, 4))
1N/A sm_dprintf("include: open: %s\n", sm_errstring(rval));
1N/A }
1N/A else if (filechanged(fname, sm_io_getinfo(fp,SM_IO_WHAT_FD, NULL), &st))
1N/A {
1N/A rval = E_SM_FILECHANGE;
1N/A if (tTd(27, 4))
1N/A sm_dprintf("include: file changed after open\n");
1N/A }
1N/A if (ev != NULL)
1N/A sm_clrevent(ev);
1N/A
1N/Aresetuid:
1N/A
1N/A#if HASSETREUID || USESETEUID
1N/A if (saveduid == 0)
1N/A {
1N/A if (uid != 0)
1N/A {
1N/A# if USESETEUID
1N/A if (seteuid(0) < 0)
1N/A syserr("!seteuid(0) failure (real=%d, eff=%d)",
1N/A (int) getuid(), (int) geteuid());
1N/A# else /* USESETEUID */
1N/A if (setreuid(-1, 0) < 0)
1N/A syserr("!setreuid(-1, 0) failure (real=%d, eff=%d)",
1N/A (int) getuid(), (int) geteuid());
1N/A if (setreuid(RealUid, 0) < 0)
1N/A syserr("!setreuid(%d, 0) failure (real=%d, eff=%d)",
1N/A (int) RealUid, (int) getuid(),
1N/A (int) geteuid());
1N/A# endif /* USESETEUID */
1N/A }
1N/A if (setgid(savedgid) < 0)
1N/A syserr("!setgid(%d) failure (real=%d eff=%d)",
1N/A (int) savedgid, (int) getgid(),
1N/A (int) getegid());
1N/A }
1N/A#endif /* HASSETREUID || USESETEUID */
1N/A
1N/A if (tTd(27, 9))
1N/A sm_dprintf("include: reset uid = %d/%d\n",
1N/A (int) getuid(), (int) geteuid());
1N/A
1N/A if (rval == E_SM_OPENTIMEOUT)
1N/A usrerr("451 4.4.1 open timeout on %s", fname);
1N/A
1N/A if (fp == NULL)
1N/A return rval;
1N/A
1N/A if (fstat(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), &st) < 0)
1N/A {
1N/A rval = errno;
1N/A syserr("Cannot fstat %s!", fname);
1N/A (void) sm_io_close(fp, SM_TIME_DEFAULT);
1N/A return rval;
1N/A }
1N/A
1N/A /* if path was writable, check to avoid file giveaway tricks */
1N/A safechown = chownsafe(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), safedir);
1N/A if (tTd(27, 6))
1N/A sm_dprintf("include: parent of %s is %s, chown is %ssafe\n",
1N/A fname, safedir ? "safe" : "dangerous",
1N/A safechown ? "" : "un");
1N/A
1N/A /* if no controlling user or coming from an alias delivery */
1N/A if (safechown &&
1N/A (ca == NULL ||
1N/A (ca->q_uid == DefUid && ca->q_gid == 0)))
1N/A {
1N/A ctladdr->q_uid = st.st_uid;
1N/A ctladdr->q_gid = st.st_gid;
1N/A ctladdr->q_flags |= QGOODUID;
1N/A }
1N/A if (ca != NULL && ca->q_uid == st.st_uid)
1N/A {
1N/A /* optimization -- avoid getpwuid if we already have info */
1N/A ctladdr->q_flags |= ca->q_flags & QBOGUSSHELL;
1N/A ctladdr->q_ruser = ca->q_ruser;
1N/A }
1N/A else if (!forwarding)
1N/A {
1N/A register struct passwd *pw;
1N/A
1N/A pw = sm_getpwuid(st.st_uid);
1N/A if (pw == NULL)
1N/A {
1N/A ctladdr->q_uid = st.st_uid;
1N/A ctladdr->q_flags |= QBOGUSSHELL;
1N/A }
1N/A else
1N/A {
1N/A char *sh;
1N/A
1N/A ctladdr->q_ruser = sm_rpool_strdup_x(e->e_rpool,
1N/A pw->pw_name);
1N/A if (safechown)
1N/A sh = pw->pw_shell;
1N/A else
1N/A sh = "/SENDMAIL/ANY/SHELL/";
1N/A if (!usershellok(pw->pw_name, sh))
1N/A {
1N/A if (LogLevel > 11)
1N/A sm_syslog(LOG_INFO, e->e_id,
1N/A "%s: user %s has bad shell %s, marked %s",
1N/A shortenstring(fname,
1N/A MAXSHORTSTR),
1N/A pw->pw_name, sh,
1N/A safechown ? "bogus" : "unsafe");
1N/A if (safechown)
1N/A ctladdr->q_flags |= QBOGUSSHELL;
1N/A else
1N/A ctladdr->q_flags |= QUNSAFEADDR;
1N/A }
1N/A }
1N/A }
1N/A
1N/A if (bitset(EF_VRFYONLY, e->e_flags))
1N/A {
1N/A /* don't do any more now */
1N/A ctladdr->q_state = QS_VERIFIED;
1N/A e->e_nrcpts++;
1N/A (void) sm_io_close(fp, SM_TIME_DEFAULT);
1N/A return rval;
1N/A }
1N/A
1N/A /*
1N/A ** Check to see if some bad guy can write this file
1N/A **
1N/A ** Group write checking could be more clever, e.g.,
1N/A ** guessing as to which groups are actually safe ("sys"
1N/A ** may be; "user" probably is not).
1N/A */
1N/A
1N/A mode = S_IWOTH;
1N/A if (!bitnset((forwarding ?
1N/A DBS_GROUPWRITABLEFORWARDFILESAFE :
1N/A DBS_GROUPWRITABLEINCLUDEFILESAFE),
1N/A DontBlameSendmail))
1N/A mode |= S_IWGRP;
1N/A
1N/A if (bitset(mode, st.st_mode))
1N/A {
1N/A if (tTd(27, 6))
1N/A sm_dprintf("include: %s is %s writable, marked unsafe\n",
1N/A shortenstring(fname, MAXSHORTSTR),
1N/A bitset(S_IWOTH, st.st_mode) ? "world"
1N/A : "group");
1N/A if (LogLevel > 11)
1N/A sm_syslog(LOG_INFO, e->e_id,
1N/A "%s: %s writable %s file, marked unsafe",
1N/A shortenstring(fname, MAXSHORTSTR),
1N/A bitset(S_IWOTH, st.st_mode) ? "world" : "group",
1N/A forwarding ? "forward" : ":include:");
1N/A ctladdr->q_flags |= QUNSAFEADDR;
1N/A }
1N/A
1N/A /* read the file -- each line is a comma-separated list. */
1N/A FileName = fname;
1N/A LineNumber = 0;
1N/A ctladdr->q_flags &= ~QSELFREF;
1N/A nincludes = 0;
1N/A while (sm_io_fgets(fp, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL &&
1N/A !maxreached)
1N/A {
1N/A fixcrlf(buf, true);
1N/A LineNumber++;
1N/A if (buf[0] == '#' || buf[0] == '\0')
1N/A continue;
1N/A
1N/A /* <sp>#@# introduces a comment anywhere */
1N/A /* for Japanese character sets */
1N/A for (p = buf; (p = strchr(++p, '#')) != NULL; )
1N/A {
1N/A if (p[1] == '@' && p[2] == '#' &&
1N/A isascii(p[-1]) && isspace(p[-1]) &&
1N/A (p[3] == '\0' || (isascii(p[3]) && isspace(p[3]))))
1N/A {
1N/A --p;
1N/A while (p > buf && isascii(p[-1]) &&
1N/A isspace(p[-1]))
1N/A --p;
1N/A p[0] = '\0';
1N/A break;
1N/A }
1N/A }
1N/A if (buf[0] == '\0')
1N/A continue;
1N/A
1N/A e->e_to = NULL;
1N/A message("%s to %s",
1N/A forwarding ? "forwarding" : "sending", buf);
1N/A if (forwarding && LogLevel > 10)
1N/A sm_syslog(LOG_INFO, e->e_id,
1N/A "forward %.200s => %s",
1N/A oldto, shortenstring(buf, MAXSHORTSTR));
1N/A
1N/A nincludes += sendtolist(buf, ctladdr, sendq, aliaslevel + 1, e);
1N/A
1N/A if (forwarding &&
1N/A MaxForwardEntries > 0 &&
1N/A nincludes >= MaxForwardEntries)
1N/A {
1N/A /* just stop reading and processing further entries */
1N/A#if 0
1N/A /* additional: (?) */
1N/A ctladdr->q_state = QS_DONTSEND;
1N/A#endif /* 0 */
1N/A
1N/A syserr("Attempt to forward to more than %d addresses (in %s)!",
1N/A MaxForwardEntries, fname);
1N/A maxreached = true;
1N/A }
1N/A }
1N/A
1N/A if (sm_io_error(fp) && tTd(27, 3))
1N/A sm_dprintf("include: read error: %s\n", sm_errstring(errno));
1N/A if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
1N/A {
1N/A if (aliaslevel <= MaxAliasRecursion ||
1N/A ctladdr->q_state != QS_BADADDR)
1N/A {
1N/A ctladdr->q_state = QS_DONTSEND;
1N/A if (tTd(27, 5))
1N/A {
1N/A sm_dprintf("include: QS_DONTSEND ");
1N/A printaddr(sm_debug_file(), ctladdr, false);
1N/A }
1N/A }
1N/A }
1N/A
1N/A (void) sm_io_close(fp, SM_TIME_DEFAULT);
1N/A FileName = oldfilename;
1N/A LineNumber = oldlinenumber;
1N/A e->e_to = oldto;
1N/A return rval;
1N/A}
1N/A
1N/Astatic void
1N/Aincludetimeout(ignore)
1N/A int ignore;
1N/A{
1N/A /*
1N/A ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
1N/A ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1N/A ** DOING.
1N/A */
1N/A
1N/A errno = ETIMEDOUT;
1N/A longjmp(CtxIncludeTimeout, 1);
1N/A}
1N/A
1N/A/*
1N/A** SENDTOARGV -- send to an argument vector.
1N/A**
1N/A** Parameters:
1N/A** argv -- argument vector to send to.
1N/A** e -- the current envelope.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** puts all addresses on the argument vector onto the
1N/A** send queue.
1N/A*/
1N/A
1N/Avoid
1N/Asendtoargv(argv, e)
1N/A register char **argv;
1N/A register ENVELOPE *e;
1N/A{
1N/A register char *p;
1N/A
1N/A while ((p = *argv++) != NULL)
1N/A (void) sendtolist(p, NULLADDR, &e->e_sendqueue, 0, e);
1N/A}
1N/A
1N/A/*
1N/A** GETCTLADDR -- get controlling address from an address header.
1N/A**
1N/A** If none, get one corresponding to the effective userid.
1N/A**
1N/A** Parameters:
1N/A** a -- the address to find the controller of.
1N/A**
1N/A** Returns:
1N/A** the controlling address.
1N/A*/
1N/A
1N/AADDRESS *
1N/Agetctladdr(a)
1N/A register ADDRESS *a;
1N/A{
1N/A while (a != NULL && !bitset(QGOODUID, a->q_flags))
1N/A a = a->q_alias;
1N/A return a;
1N/A}
1N/A
1N/A/*
1N/A** SELF_REFERENCE -- check to see if an address references itself
1N/A**
1N/A** The check is done through a chain of aliases. If it is part of
1N/A** a loop, break the loop at the "best" address, that is, the one
1N/A** that exists as a real user.
1N/A**
1N/A** This is to handle the case of:
1N/A** awc: Andrew.Chang
1N/A** Andrew.Chang: awc@mail.server
1N/A** which is a problem only on mail.server.
1N/A**
1N/A** Parameters:
1N/A** a -- the address to check.
1N/A**
1N/A** Returns:
1N/A** The address that should be retained.
1N/A*/
1N/A
1N/Astatic ADDRESS *
1N/Aself_reference(a)
1N/A ADDRESS *a;
1N/A{
1N/A ADDRESS *b; /* top entry in self ref loop */
1N/A ADDRESS *c; /* entry that point to a real mail box */
1N/A
1N/A if (tTd(27, 1))
1N/A sm_dprintf("self_reference(%s)\n", a->q_paddr);
1N/A
1N/A for (b = a->q_alias; b != NULL; b = b->q_alias)
1N/A {
1N/A if (sameaddr(a, b))
1N/A break;
1N/A }
1N/A
1N/A if (b == NULL)
1N/A {
1N/A if (tTd(27, 1))
1N/A sm_dprintf("\t... no self ref\n");
1N/A return NULL;
1N/A }
1N/A
1N/A /*
1N/A ** Pick the first address that resolved to a real mail box
1N/A ** i.e has a mbdb entry. The returned value will be marked
1N/A ** QSELFREF in recipient(), which in turn will disable alias()
1N/A ** from marking it as QS_IS_DEAD(), which mean it will be used
1N/A ** as a deliverable address.
1N/A **
1N/A ** The 2 key thing to note here are:
1N/A ** 1) we are in a recursive call sequence:
1N/A ** alias->sendtolist->recipient->alias
1N/A ** 2) normally, when we return back to alias(), the address
1N/A ** will be marked QS_EXPANDED, since alias() assumes the
1N/A ** expanded form will be used instead of the current address.
1N/A ** This behaviour is turned off if the address is marked
1N/A ** QSELFREF. We set QSELFREF when we return to recipient().
1N/A */
1N/A
1N/A c = a;
1N/A while (c != NULL)
1N/A {
1N/A if (tTd(27, 10))
1N/A sm_dprintf(" %s", c->q_user);
1N/A if (bitnset(M_HASPWENT, c->q_mailer->m_flags))
1N/A {
1N/A SM_MBDB_T user;
1N/A
1N/A if (tTd(27, 2))
1N/A sm_dprintf("\t... getpwnam(%s)... ", c->q_user);
1N/A if (sm_mbdb_lookup(c->q_user, &user) == EX_OK)
1N/A {
1N/A if (tTd(27, 2))
1N/A sm_dprintf("found\n");
1N/A
1N/A /* ought to cache results here */
1N/A if (sameaddr(b, c))
1N/A return b;
1N/A else
1N/A return c;
1N/A }
1N/A if (tTd(27, 2))
1N/A sm_dprintf("failed\n");
1N/A }
1N/A else
1N/A {
1N/A /* if local delivery, compare usernames */
1N/A if (bitnset(M_LOCALMAILER, c->q_mailer->m_flags) &&
1N/A b->q_mailer == c->q_mailer)
1N/A {
1N/A if (tTd(27, 2))
1N/A sm_dprintf("\t... local match (%s)\n",
1N/A c->q_user);
1N/A if (sameaddr(b, c))
1N/A return b;
1N/A else
1N/A return c;
1N/A }
1N/A }
1N/A if (tTd(27, 10))
1N/A sm_dprintf("\n");
1N/A c = c->q_alias;
1N/A }
1N/A
1N/A if (tTd(27, 1))
1N/A sm_dprintf("\t... cannot break loop for \"%s\"\n", a->q_paddr);
1N/A
1N/A return NULL;
1N/A}