2 * Copyright (c) 1998-2003, 2006 Proofpoint, Inc. and its suppliers.
4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
16 SM_RCSID("@(#)$Id: recipient.c,v 8.351 2013-11-22 20:51:56 ca Exp $")
18 #include <sm/sendmail.h>
20 # include <sm/ixlen.h>
23 static void includetimeout __P((int));
24 static ADDRESS *self_reference __P((ADDRESS *));
25 static int sortexpensive __P((ADDRESS *, ADDRESS *));
26 static int sortbysignature __P((ADDRESS *, ADDRESS *));
27 static int sorthost __P((ADDRESS *, ADDRESS *));
29 typedef int sortfn_t __P((ADDRESS *, ADDRESS *));
32 ** SORTHOST -- strcmp()-like func for host portion of an ADDRESS
35 ** xx -- first ADDRESS
36 ** yy -- second ADDRESS
39 ** <0 when xx->q_host is less than yy->q_host
40 ** >0 when xx->q_host is greater than yy->q_host
49 #if _FFR_HOST_SORT_REVERSE
50 /* XXX maybe compare hostnames from the end? */
51 return sm_strrevcasecmp(xx->q_host, yy->q_host);
53 return sm_strcasecmp(xx->q_host, yy->q_host);
58 ** SORTEXPENSIVE -- strcmp()-like func for expensive mailers
60 ** The mailer has been noted already as "expensive" for 'xx'. This
61 ** will give a result relative to 'yy'. Expensive mailers get rated
62 ** "greater than" non-expensive mailers because during the delivery phase
63 ** it will get queued -- no use it getting in the way of less expensive
64 ** recipients. We avoid an MX RR lookup when both 'xx' and 'yy' are
65 ** expensive since an MX RR lookup happens when extracted from the queue
69 ** xx -- first ADDRESS
70 ** yy -- second ADDRESS
73 ** <0 when xx->q_host is less than yy->q_host and both are
75 ** >0 when xx->q_host is greater than yy->q_host, or when
76 ** 'yy' is non-expensive
77 ** 0 when equal (by expense and q_host)
85 if (!bitnset(M_EXPENSIVE, yy->q_mailer->m_flags))
86 return 1; /* xx should go later */
87 #if _FFR_HOST_SORT_REVERSE
88 /* XXX maybe compare hostnames from the end? */
89 return sm_strrevcasecmp(xx->q_host, yy->q_host);
91 return sm_strcasecmp(xx->q_host, yy->q_host);
96 ** SORTBYSIGNATURE -- a strcmp()-like func for q_mailer and q_host in ADDRESS
99 ** xx -- first ADDRESS
100 ** yy -- second ADDRESS
103 ** 0 when the "signature"'s are same
104 ** <0 when xx->q_signature is less than yy->q_signature
105 ** >0 when xx->q_signature is greater than yy->q_signature
108 ** May set ADDRESS pointer for q_signature if not already set.
112 sortbysignature(xx, yy)
118 /* Let's avoid redoing the signature over and over again */
119 if (xx->q_signature == NULL)
120 xx->q_signature = hostsignature(xx->q_mailer, xx->q_host,
121 QISSECURE(xx), NULL);
122 if (yy->q_signature == NULL)
123 yy->q_signature = hostsignature(yy->q_mailer, yy->q_host,
124 QISSECURE(yy), NULL);
125 ret = strcmp(xx->q_signature, yy->q_signature);
128 ** If the two signatures are the same then we will return a sort
129 ** value based on 'q_user'. But note that we have reversed xx and yy
130 ** on purpose. This additional compare helps reduce the number of
131 ** sameaddr() calls and loops in recipient() for the case when
132 ** the rcpt list has been provided already in-order.
136 return strcmp(yy->q_user, xx->q_user);
142 ** SENDTOLIST -- Designate a send list.
144 ** The parameter is a comma-separated list of people to send to.
145 ** This routine arranges to send to all of them.
148 ** list -- the send list.
149 ** ctladdr -- the address template for the person to
150 ** send to -- effective uid/gid are important.
151 ** This is typically the alias that caused this
153 ** sendq -- a pointer to the head of a queue to put
154 ** these people into.
155 ** aliaslevel -- the current alias nesting depth -- to
157 ** e -- the envelope in which to add these recipients.
160 ** The number of addresses actually on the list.
163 /* q_flags bits inherited from ctladdr */
164 #define QINHERITEDBITS (QPINGONSUCCESS|QPINGONFAILURE|QPINGONDELAY|QHASNOTIFY)
167 sendtolist(list, ctladdr, sendq, aliaslevel, e)
172 register ENVELOPE *e;
175 register ADDRESS *SM_NONVOLATILE al; /* list of addresses to send to */
176 SM_NONVOLATILE char delimiter; /* the address delimiter */
177 SM_NONVOLATILE int naddrs;
178 SM_NONVOLATILE int i;
180 char *oldto = e->e_to;
181 char *SM_NONVOLATILE bufp;
182 char buf[MAXNAME + 1]; /* EAI: ok, uses bufp dynamically expanded */
186 syserr("sendtolist: null list");
192 sm_dprintf("sendto: %s\n ctladdr=", list);
193 printaddr(sm_debug_file(), ctladdr, false);
196 /* heuristic to determine old versus new style addresses */
197 if (ctladdr == NULL &&
198 (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
199 strchr(list, '<') != NULL || strchr(list, '(') != NULL))
200 e->e_flags &= ~EF_OLDSTYLE;
202 if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL)
208 /* make sure we have enough space to copy the string */
209 i = strlen(list) + 1;
210 if (i <= sizeof(buf))
216 bufp = sm_malloc_x(i);
221 (void) sm_strlcpy(bufp, denlstring(list, false, true), i);
223 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e r");
224 for (p = bufp; *p != '\0'; )
231 /* parse the address */
232 while ((SM_ISSPACE(*p)) || *p == ',')
235 /* XXX p must be [i] */
236 a = parseaddr(p, NULLADDR, RF_COPYALL, delimiter,
243 a->q_alias = ctladdr;
245 /* arrange to inherit attributes from parent */
250 /* self reference test */
251 if (sameaddr(ctladdr, a))
255 sm_dprintf("sendtolist: QSELFREF ");
256 printaddr(sm_debug_file(), ctladdr, false);
258 ctladdr->q_flags |= QSELFREF;
261 /* check for address loops */
262 b = self_reference(a);
265 b->q_flags |= QSELFREF;
268 sm_dprintf("sendtolist: QSELFREF ");
269 printaddr(sm_debug_file(), b, false);
275 sm_dprintf("sendtolist: QS_DONTSEND ");
276 printaddr(sm_debug_file(), a, false);
278 a->q_state = QS_DONTSEND;
279 b->q_flags |= a->q_flags & QNOTREMOTE;
285 if (a->q_fullname == NULL)
286 a->q_fullname = ctladdr->q_fullname;
288 /* various flag bits */
289 a->q_flags &= ~QINHERITEDBITS;
290 a->q_flags |= ctladdr->q_flags & QINHERITEDBITS;
292 /* DSN recipient information */
293 a->q_finalrcpt = ctladdr->q_finalrcpt;
294 a->q_orcpt = ctladdr->q_orcpt;
300 /* arrange to send to everyone on the local send list */
303 register ADDRESS *a = al;
306 a = recipient(a, sendq, aliaslevel, e);
315 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
323 ** REMOVEFROMLIST -- Remove addresses from a send list.
325 ** The parameter is a comma-separated list of recipients to remove.
326 ** Note that it only deletes matching addresses. If those addresses
327 ** have been expanded already in the sendq, it won't mark the
328 ** expanded recipients as QS_REMOVED.
331 ** list -- the list to remove.
332 ** sendq -- a pointer to the head of a queue to remove
333 ** these addresses from.
334 ** e -- the envelope in which to remove these recipients.
337 ** The number of addresses removed from the list.
342 removefromlist(list, sendq, e)
347 SM_NONVOLATILE char delimiter; /* the address delimiter */
348 SM_NONVOLATILE int naddrs;
349 SM_NONVOLATILE int i;
351 char *oldto = e->e_to;
352 char *SM_NONVOLATILE bufp;
353 char buf[MAXNAME + 1]; /* EAI: ok, uses bufp dynamically expanded */
357 syserr("removefromlist: null list");
362 sm_dprintf("removefromlist: %s\n", list);
364 /* heuristic to determine old versus new style addresses */
365 if (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
366 strchr(list, '<') != NULL || strchr(list, '(') != NULL)
367 e->e_flags &= ~EF_OLDSTYLE;
369 if (!bitset(EF_OLDSTYLE, e->e_flags))
374 /* make sure we have enough space to copy the string */
375 i = strlen(list) + 1;
376 if (i <= sizeof(buf))
382 bufp = sm_malloc_x(i);
386 (void) sm_strlcpy(bufp, denlstring(list, false, true), i);
388 # if _FFR_ADDR_TYPE_MODES
390 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"),
393 # endif /* _FFR_ADDR_TYPE_MODES */
394 /* "else" in #if code above */
396 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"),
399 for (p = bufp; *p != '\0'; )
401 ADDRESS a; /* parsed address to be removed */
406 /* parse the address */
407 while ((SM_ISSPACE(*p)) || *p == ',')
409 /* XXX p must be [i] */
410 if (parseaddr(p, &a, RF_COPYALL|RF_RM_ADDR,
411 delimiter, &delimptr, e, true) == NULL)
417 for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
419 if (!QS_IS_DEAD(q->q_state) &&
421 strcmp(q->q_paddr, a.q_paddr) == 0))
425 sm_dprintf("removefromlist: QS_REMOVED ");
426 printaddr(sm_debug_file(), &a, false);
428 q->q_state = QS_REMOVED;
440 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
448 ** RECIPIENT -- Designate a message recipient
449 ** Saves the named person for future mailing (after some checks).
452 ** new -- the (preparsed) address header for the recipient.
453 ** sendq -- a pointer to the head of a queue to put the
454 ** recipient in. Duplicate suppression is done
456 ** aliaslevel -- the current alias nesting depth.
457 ** e -- the current envelope.
460 ** The actual address in the queue. This will be "a" if
461 ** the address is not a duplicate, else the original address.
466 recipient(new, sendq, aliaslevel, e)
467 register ADDRESS *new;
468 register ADDRESS **sendq;
470 register ENVELOPE *e;
475 register struct mailer *m;
478 bool quoted; /* set if the addr has a quote bit */
481 bool initialdontsend;
483 char buf0[MAXNAME + 1]; /* EAI: ok, uses bufp dynamically expanded */
484 /* unquoted image of the user name */
491 initialdontsend = QS_IS_DEAD(new->q_state);
492 e->e_to = new->q_paddr;
496 new->q_flags |= QPRIMARY;
499 sm_dprintf("\nrecipient (%d): ", aliaslevel);
500 printaddr(sm_debug_file(), new, false);
503 /* if this is primary, use it as original recipient */
504 if (new->q_alias == NULL)
506 if (e->e_origrcpt == NULL)
507 e->e_origrcpt = new->q_paddr;
508 else if (e->e_origrcpt != new->q_paddr)
512 /* find parent recipient for finalrcpt and orcpt */
513 for (q = new; q->q_alias != NULL; q = q->q_alias)
516 /* find final recipient DSN address */
517 if (new->q_finalrcpt == NULL &&
518 e->e_from.q_mailer != NULL)
522 p = e->e_from.q_mailer->m_addrtype;
526 if (SM_STRCASEEQ(p, "rfc822") &&
527 !addr_is_ascii(q->q_user))
530 if (sm_strcasecmp(p, "rfc822") != 0)
532 (void) sm_snprintf(frbuf, sizeof(frbuf), "%s; %.800s",
533 q->q_mailer->m_addrtype,
536 else if (strchr(q->q_user, '@') != NULL)
538 (void) sm_snprintf(frbuf, sizeof(frbuf), "%s; %.800s",
541 else if (strchr(q->q_paddr, '@') != NULL)
548 /* strip brackets from address */
552 b = qp[strlen(qp) - 1] == '>';
554 qp[strlen(qp) - 1] = '\0';
557 (void) sm_snprintf(frbuf, sizeof(frbuf), "%s; %.800s",
562 qp[strlen(qp)] = '>';
566 (void) sm_snprintf(frbuf, sizeof(frbuf),
568 p, q->q_user, MyHostName);
570 new->q_finalrcpt = sm_rpool_strdup_x(e->e_rpool, frbuf);
574 /* set ORCPT DSN arg if not already set */
575 if (new->q_orcpt == NULL)
577 /* check for an existing ORCPT */
578 if (q->q_orcpt != NULL)
579 new->q_orcpt = q->q_orcpt;
587 if (e->e_from.q_mailer != NULL)
588 p = e->e_from.q_mailer->m_addrtype;
591 (void) sm_strlcpyn(obuf, sizeof(obuf), 2, p, ";");
595 /* FFR: Needs to strip comments from stdin addrs */
597 /* strip brackets from address */
600 b = qp[strlen(qp) - 1] == '>';
602 qp[strlen(qp) - 1] = '\0';
606 p = xtextify(denlstring(qp, true, false), "=");
608 if (sm_strlcat(obuf, p, sizeof(obuf)) >= sizeof(obuf))
610 /* if too big, don't use it */
616 qp[strlen(qp)] = '>';
620 sm_rpool_strdup_x(e->e_rpool, obuf);
623 #endif /* _FFR_GEN_ORCPT */
625 /* break aliasing loops */
626 if (aliaslevel > MaxAliasRecursion)
628 new->q_state = QS_BADADDR;
629 new->q_status = "5.4.6";
630 if (new->q_alias != NULL)
632 new->q_alias->q_state = QS_BADADDR;
633 new->q_alias->q_status = "5.4.6";
635 if ((SuprErrs || !LogUsrErrs) && LogLevel > 0)
637 sm_syslog(LOG_ERR, e->e_id,
638 "aliasing/forwarding loop broken: %s (%d aliases deep; %d max)",
639 FileName != NULL ? FileName : "", aliaslevel,
642 usrerrenh(new->q_status,
643 "554 aliasing/forwarding loop broken (%d aliases deep; %d max)",
644 aliaslevel, MaxAliasRecursion);
649 ** Finish setting up address structure.
652 /* get unquoted user for file, program or user.name check */
653 i = strlen(new->q_user);
654 if (i >= sizeof(buf0))
657 buf = xalloc(buflen);
662 buflen = sizeof(buf0);
664 (void) sm_strlcpy(buf, new->q_user, buflen);
665 for (p = buf; *p != '\0' && !quoted; p++)
672 /* check for direct mailing to restricted mailers */
675 if (new->q_alias == NULL || UseMSP ||
676 bitset(EF_UNSAFE, e->e_flags))
678 new->q_state = QS_BADADDR;
679 new->q_status = "5.7.1";
680 usrerrenh(new->q_status,
681 "550 Cannot mail directly to programs");
683 else if (bitset(QBOGUSSHELL, new->q_alias->q_flags))
685 new->q_state = QS_BADADDR;
686 new->q_status = "5.7.1";
687 if (new->q_alias->q_ruser == NULL)
688 usrerrenh(new->q_status,
689 "550 UID %ld is an unknown user: cannot mail to programs",
690 (long) new->q_alias->q_uid);
692 usrerrenh(new->q_status,
693 "550 User %s@%s doesn't have a valid shell for mailing to programs",
694 new->q_alias->q_ruser, MyHostName);
696 else if (bitset(QUNSAFEADDR, new->q_alias->q_flags))
698 new->q_state = QS_BADADDR;
699 new->q_status = "5.7.1";
700 new->q_rstatus = "550 Unsafe for mailing to programs";
701 usrerrenh(new->q_status,
702 "550 Address %s is unsafe for mailing to programs",
703 new->q_alias->q_paddr);
708 ** Look up this person in the recipient list.
709 ** If they are there already, return, otherwise continue.
710 ** If the list is empty, just add it. Notice the cute
711 ** hack to make from addresses suppress things correctly:
712 ** the QS_DUPLICATE state will be set in the send list.
713 ** [Please note: the emphasis is on "hack."]
719 ** If this message is going to the queue or FastSplit is set
720 ** and it is the first try and the envelope hasn't split, then we
721 ** avoid doing an MX RR lookup now because one will be done when the
722 ** message is extracted from the queue later. It can go to the queue
723 ** because all messages are going to the queue or this mailer of
724 ** the current recipient is marked expensive.
727 if (UseMSP || WILL_BE_QUEUED(e->e_sendmode) ||
728 (!bitset(EF_SPLIT, e->e_flags) && e->e_ntries == 0 &&
731 else if (NoConnect && bitnset(M_EXPENSIVE, new->q_mailer->m_flags))
732 sortfn = sortexpensive;
734 sortfn = sortbysignature;
736 for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
739 ** If address is "less than" it should be inserted now.
740 ** If address is "greater than" current comparison it'll
741 ** insert later in the list; so loop again (if possible).
742 ** If address is "equal" (different equal than sameaddr()
743 ** call) then check if sameaddr() will be true.
744 ** Because this list is now sorted, it'll mean fewer
745 ** comparisons and fewer loops which is important for more
749 i = (*sortfn)(new, q);
750 if (i == 0) /* equal */
753 ** sortbysignature() has said that the two have
754 ** equal MX RR's and the same user. Calling sameaddr()
755 ** now checks if the two hosts are as identical as the
756 ** MX RR's are (which might not be the case)
757 ** before saying these are the identical addresses.
760 if (sameaddr(q, new) &&
761 (bitset(QRCPTOK, q->q_flags) ||
762 !bitset(QPRIMARY, q->q_flags)))
766 sm_dprintf("%s in sendq: ",
768 printaddr(sm_debug_file(), q, false);
770 if (!bitset(QPRIMARY, q->q_flags))
772 if (!QS_IS_DEAD(new->q_state))
773 message("duplicate suppressed");
775 q->q_state = QS_DUPLICATE;
776 q->q_flags |= new->q_flags;
778 else if (bitset(QSELFREF, q->q_flags)
779 || q->q_state == QS_REMOVED)
782 ** If an earlier milter removed the
783 ** address, a later one can still add
787 q->q_state = new->q_state;
788 q->q_flags |= new->q_flags;
794 else if (i < 0) /* less than */
802 /* pq should point to an address, never NULL */
803 SM_ASSERT(pq != NULL);
805 /* add address on list */
809 ** insert before 'pq'. Only possible when at least 1
810 ** ADDRESS is in the list already.
815 *sendq = new; /* To be the first ADDRESS */
817 (*prev)->q_next = new;
822 ** Place in list at current 'pq' position. Possible
823 ** when there are 0 or more ADDRESS's in the list.
830 /* added a new address: clear split flag */
831 e->e_flags &= ~EF_SPLIT;
834 ** Alias the name and handle special mailer types.
840 sm_dprintf("at trylocaluser: ");
841 printaddr(sm_debug_file(), new, false);
844 if (!QS_IS_OK(new->q_state))
846 if (QS_IS_UNDELIVERED(new->q_state))
848 goto testselfdestruct;
853 new->q_state = QS_INCLUDED;
854 if (new->q_alias == NULL || UseMSP ||
855 bitset(EF_UNSAFE, e->e_flags))
857 new->q_state = QS_BADADDR;
858 new->q_status = "5.7.1";
859 usrerrenh(new->q_status,
860 "550 Cannot mail directly to :include:s");
866 message("including file %s", new->q_user);
867 ret = include(new->q_user, false, new,
868 sendq, aliaslevel, e);
869 if (transienterror(ret))
872 sm_syslog(LOG_ERR, e->e_id,
873 "include %s: transient error: %s",
874 shortenstring(new->q_user,
877 new->q_state = QS_QUEUEUP;
878 usrerr("451 4.2.4 Cannot open %s: %s",
879 shortenstring(new->q_user,
885 new->q_state = QS_BADADDR;
886 new->q_status = "5.2.4";
887 usrerrenh(new->q_status,
888 "550 Cannot open %s: %s",
889 shortenstring(new->q_user,
895 else if (m == FileMailer)
897 /* check if allowed */
898 if (new->q_alias == NULL || UseMSP ||
899 bitset(EF_UNSAFE, e->e_flags))
901 new->q_state = QS_BADADDR;
902 new->q_status = "5.7.1";
903 usrerrenh(new->q_status,
904 "550 Cannot mail directly to files");
906 else if (bitset(QBOGUSSHELL, new->q_alias->q_flags))
908 new->q_state = QS_BADADDR;
909 new->q_status = "5.7.1";
910 if (new->q_alias->q_ruser == NULL)
911 usrerrenh(new->q_status,
912 "550 UID %ld is an unknown user: cannot mail to files",
913 (long) new->q_alias->q_uid);
915 usrerrenh(new->q_status,
916 "550 User %s@%s doesn't have a valid shell for mailing to files",
917 new->q_alias->q_ruser, MyHostName);
919 else if (bitset(QUNSAFEADDR, new->q_alias->q_flags))
921 new->q_state = QS_BADADDR;
922 new->q_status = "5.7.1";
923 new->q_rstatus = "550 Unsafe for mailing to files";
924 usrerrenh(new->q_status,
925 "550 Address %s is unsafe for mailing to files",
926 new->q_alias->q_paddr);
931 if (!quoted && QS_IS_OK(new->q_state) &&
932 bitnset(M_ALIASABLE, m->m_flags))
933 alias(new, sendq, aliaslevel, e);
936 /* if not aliased, look it up in the user database */
937 if (!bitset(QNOTREMOTE, new->q_flags) &&
938 QS_IS_SENDABLE(new->q_state) &&
939 bitnset(M_CHECKUDB, m->m_flags))
941 if (udbexpand(new, sendq, aliaslevel, e) == EX_TEMPFAIL)
943 new->q_state = QS_QUEUEUP;
944 if (e->e_message == NULL)
945 e->e_message = sm_rpool_strdup_x(e->e_rpool,
946 "Deferred: user database error");
947 if (new->q_message == NULL)
948 new->q_message = "Deferred: user database error";
950 sm_syslog(LOG_INFO, e->e_id,
951 "deferred: udbexpand: %s",
952 sm_errstring(errno));
953 message("queued (user database error): %s",
954 sm_errstring(errno));
956 goto testselfdestruct;
962 ** If we have a level two config file, then pass the name through
963 ** Ruleset 5 before sending it off. Ruleset 5 has the right
964 ** to rewrite it to another mailer. This gives us a hook
965 ** after local aliasing has been done.
970 sm_dprintf("recipient: testing local? cl=%d, rr5=%p\n\t",
971 ConfigLevel, (void *)RewriteRules[5]);
972 printaddr(sm_debug_file(), new, false);
974 if (ConfigLevel >= 2 && RewriteRules[5] != NULL &&
975 bitnset(M_TRYRULESET5, m->m_flags) &&
976 !bitset(QNOTREMOTE, new->q_flags) &&
977 QS_IS_OK(new->q_state))
979 maplocaluser(new, sendq, aliaslevel + 1, e);
983 ** If it didn't get rewritten to another mailer, go ahead
987 if (QS_IS_OK(new->q_state) &&
988 bitnset(M_HASPWENT, m->m_flags))
994 /* warning -- finduser may trash buf */
995 status = finduser(buf, &fuzzy, &user);
999 new->q_state = QS_QUEUEUP;
1000 new->q_status = "4.5.2";
1001 giveresponse(EX_TEMPFAIL, new->q_status, m, NULL,
1002 new->q_alias, (time_t) 0, e, new);
1005 new->q_state = QS_BADADDR;
1006 new->q_status = "5.1.1";
1007 new->q_rstatus = "550 5.1.1 User unknown";
1008 giveresponse(EX_NOUSER, new->q_status, m, NULL,
1009 new->q_alias, (time_t) 0, e, new);
1014 /* name was a fuzzy match */
1015 new->q_user = sm_rpool_strdup_x(e->e_rpool,
1017 if (findusercount++ > 3)
1019 new->q_state = QS_BADADDR;
1020 new->q_status = "5.4.6";
1021 usrerrenh(new->q_status,
1022 "554 aliasing/forwarding loop for %s broken",
1027 /* see if it aliases */
1028 (void) sm_strlcpy(buf, user.mbdb_name, buflen);
1031 if (*user.mbdb_homedir == '\0')
1033 else if (strcmp(user.mbdb_homedir, "/") == 0)
1036 new->q_home = sm_rpool_strdup_x(e->e_rpool,
1038 if (user.mbdb_uid != SM_NO_UID)
1040 new->q_uid = user.mbdb_uid;
1041 new->q_gid = user.mbdb_gid;
1042 new->q_flags |= QGOODUID;
1044 new->q_ruser = sm_rpool_strdup_x(e->e_rpool,
1046 if (user.mbdb_fullname[0] != '\0')
1047 new->q_fullname = sm_rpool_strdup_x(e->e_rpool,
1048 user.mbdb_fullname);
1049 if (!usershellok(user.mbdb_name, user.mbdb_shell))
1051 new->q_flags |= QBOGUSSHELL;
1053 if (bitset(EF_VRFYONLY, e->e_flags))
1055 /* don't do any more now */
1056 new->q_state = QS_VERIFIED;
1059 forward(new, sendq, aliaslevel, e);
1062 if (!QS_IS_DEAD(new->q_state))
1066 new->q_flags |= QTHISPASS;
1069 sm_dprintf("testselfdestruct: ");
1070 printaddr(sm_debug_file(), new, false);
1073 sm_dprintf("SENDQ:\n");
1074 printaddr(sm_debug_file(), *sendq, true);
1075 sm_dprintf("----\n");
1078 if (new->q_alias == NULL && new != &e->e_from &&
1079 QS_IS_DEAD(new->q_state))
1081 for (q = *sendq; q != NULL; q = q->q_next)
1083 if (!QS_IS_DEAD(q->q_state))
1088 new->q_state = QS_BADADDR;
1089 new->q_status = "5.4.6";
1090 usrerrenh(new->q_status,
1091 "554 aliasing/forwarding loop broken");
1096 new->q_flags |= QTHISPASS;
1098 sm_free(buf); /* XXX leak if above code raises exception */
1101 ** If we are at the top level, check to see if this has
1102 ** expanded to exactly one address. If so, it can inherit
1103 ** the primaryness of the address.
1105 ** While we're at it, clear the QTHISPASS bits.
1108 if (aliaslevel == 0)
1111 ADDRESS *only = NULL;
1113 for (q = *sendq; q != NULL; q = q->q_next)
1115 if (bitset(QTHISPASS, q->q_flags) &&
1116 QS_IS_SENDABLE(q->q_state))
1121 q->q_flags &= ~QTHISPASS;
1125 /* check to see if this actually got a new owner */
1127 while ((q = q->q_alias) != NULL)
1129 if (q->q_owner != NULL)
1133 only->q_flags |= QPRIMARY;
1135 else if (!initialdontsend && nrcpts > 0)
1137 /* arrange for return receipt */
1138 e->e_flags |= EF_SENDRECEIPT;
1139 new->q_flags |= QEXPANDED;
1140 if (e->e_xfp != NULL &&
1141 bitset(QPINGONSUCCESS, new->q_flags))
1142 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
1143 "%s... expanded to multiple addresses\n",
1147 new->q_flags |= QRCPTOK;
1148 (void) sm_snprintf(buf0, sizeof(buf0), "%d", e->e_nrcpts);
1149 macdefine(&e->e_macro, A_TEMP, macid("{nrcpts}"), buf0);
1154 ** FINDUSER -- find the password entry for a user.
1156 ** This looks a lot like getpwnam, except that it may want to
1157 ** do some fancier pattern matching in /etc/passwd.
1159 ** This routine contains most of the time of many sendmail runs.
1160 ** It deserves to be optimized.
1163 ** name -- the name to match against.
1164 ** fuzzyp -- an outarg that is set to true if this entry
1165 ** was found using the fuzzy matching algorithm;
1166 ** set to false otherwise.
1167 ** user -- structure to fill in if user is found
1170 ** On success, fill in *user, set *fuzzyp and return EX_OK.
1171 ** If the user was not found, return EX_NOUSER.
1172 ** On error, return EX_TEMPFAIL or EX_OSERR.
1179 finduser(name, fuzzyp, user)
1185 register struct passwd *pw;
1192 sm_dprintf("finduser(%s): ", name);
1196 #if HESIOD && !HESIOD_ALLOW_NUMERIC_LOGIN
1197 /* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */
1198 for (p = name; *p != '\0'; p++)
1199 if (!isascii(*p) || !isdigit(*p))
1204 sm_dprintf("failed (numeric input)\n");
1207 #endif /* HESIOD && !HESIOD_ALLOW_NUMERIC_LOGIN */
1209 /* look up this login name using fast path */
1210 status = sm_mbdb_lookup(name, user);
1211 if (status != EX_NOUSER)
1214 sm_dprintf("%s (non-fuzzy)\n", sm_strexit(status));
1218 /* try mapping it to lower case */
1220 for (p = name; *p != '\0'; p++)
1222 if (isascii(*p) && isupper(*p))
1228 if (tryagain && (status = sm_mbdb_lookup(name, user)) != EX_NOUSER)
1231 sm_dprintf("%s (lower case)\n", sm_strexit(status));
1237 /* see if fuzzy matching allowed */
1241 sm_dprintf("not found (fuzzy disabled)\n");
1245 /* search for a matching full name instead */
1246 for (p = name; *p != '\0'; p++)
1248 if (*p == (SpaceSub & 0177) || *p == '_')
1252 while ((pw = getpwent()) != NULL)
1254 char buf[MAXNAME + 1]; /* EAI: ok: for pw_gecos */
1257 if (SM_STRCASEEQ(pw->pw_name, name))
1260 sm_dprintf("found (case wrapped)\n");
1265 sm_pwfullname(pw->pw_gecos, pw->pw_name, buf, sizeof(buf));
1266 if (strchr(buf, ' ') != NULL && SM_STRCASEEQ(buf, name))
1269 sm_dprintf("fuzzy matches %s\n", pw->pw_name);
1270 message("sending to login name %s", pw->pw_name);
1276 else if (tTd(29, 4))
1277 sm_dprintf("no fuzzy match found\n");
1278 # if DEC_OSF_BROKEN_GETPWENT /* DEC OSF/1 3.2 or earlier */
1283 sm_mbdb_frompw(user, pw);
1285 #else /* MATCHGECOS */
1287 sm_dprintf("not found (fuzzy disabled)\n");
1289 #endif /* MATCHGECOS */
1293 ** WRITABLE -- predicate returning if the file is writable.
1295 ** This routine must duplicate the algorithm in sys/fio.c.
1296 ** Unfortunately, we cannot use the access call since we
1297 ** won't necessarily be the real uid when we try to
1298 ** actually open the file.
1300 ** Notice that ANY file with ANY execute bit is automatically
1301 ** not writable. This is also enforced by mailfile.
1304 ** filename -- the file name to check.
1305 ** ctladdr -- the controlling address for this file.
1306 ** flags -- SFF_* flags to control the function.
1309 ** true -- if we will be able to write this file.
1310 ** false -- if we cannot write this file.
1317 writable(filename, ctladdr, flags)
1327 sm_dprintf("writable(%s, 0x%lx)\n", filename, flags);
1330 ** File does exist -- check that it is writable.
1339 else if (ctladdr != NULL)
1341 euid = ctladdr->q_uid;
1342 egid = ctladdr->q_gid;
1343 user = ctladdr->q_user;
1345 else if (bitset(SFF_RUNASREALUID, flags))
1349 user = RealUserName;
1351 else if (FileMailer != NULL && !bitset(SFF_ROOTOK, flags))
1353 if (FileMailer->m_uid == NO_UID)
1360 euid = FileMailer->m_uid;
1363 if (FileMailer->m_gid == NO_GID)
1366 egid = FileMailer->m_gid;
1373 if (!bitset(SFF_ROOTOK, flags))
1383 if (geteuid() == 0 &&
1384 (ctladdr == NULL || !bitset(QGOODUID, ctladdr->q_flags)))
1385 flags |= SFF_SETUIDOK;
1387 if (!bitnset(DBS_FILEDELIVERYTOSYMLINK, DontBlameSendmail))
1388 flags |= SFF_NOSLINK;
1389 if (!bitnset(DBS_FILEDELIVERYTOHARDLINK, DontBlameSendmail))
1390 flags |= SFF_NOHLINK;
1392 errno = safefile(filename, euid, egid, user, flags, S_IWRITE, NULL);
1397 ** INCLUDE -- handle :include: specification.
1400 ** fname -- filename to include.
1401 ** forwarding -- if true, we are reading a .forward file.
1402 ** if false, it's a :include: file.
1403 ** ctladdr -- address template to use to fill in these
1404 ** addresses -- effective user/group id are
1405 ** the important things.
1406 ** sendq -- a pointer to the head of the send queue
1407 ** to put these addresses in.
1408 ** aliaslevel -- the alias nesting depth.
1409 ** e -- the current envelope.
1412 ** open error status
1415 ** reads the :include: file and sends to everyone
1416 ** listed in that file.
1419 ** If you have restricted chown (that is, you can't
1420 ** give a file away), it is reasonable to allow programs
1421 ** and files called from this :include: file to be to be
1422 ** run as the owner of the :include: file. This is bogus
1423 ** if there is any chance of someone giving away a file.
1424 ** We assume that pre-POSIX systems can give away files.
1426 ** There is an additional restriction that if you
1427 ** forward to a :include: file, it will not take on
1428 ** the ownership of the :include: file. This may not
1429 ** be necessary, but shouldn't hurt.
1432 static jmp_buf CtxIncludeTimeout;
1435 include(fname, forwarding, ctladdr, sendq, aliaslevel, e)
1443 SM_FILE_T *volatile fp = NULL;
1444 char *oldto = e->e_to;
1445 char *oldfilename = FileName;
1446 int oldlinenumber = LineNumber;
1447 register SM_EVENT *ev = NULL;
1450 volatile bool maxreached = false;
1451 register ADDRESS *ca;
1452 volatile uid_t saveduid;
1453 volatile gid_t savedgid;
1456 char *volatile user;
1458 volatile long sfflags = SFF_REGONLY;
1460 bool safechown = false;
1461 volatile bool safedir = false;
1466 sm_dprintf("include(%s)\n", fname);
1468 sm_dprintf(" ruid=%ld euid=%ld\n",
1469 (long) getuid(), (long) geteuid());
1472 sm_dprintf("ctladdr ");
1473 printaddr(sm_debug_file(), ctladdr, false);
1477 sm_dprintf("include: old uid = %ld/%ld\n",
1478 (long) getuid(), (long) geteuid());
1482 sfflags |= SFF_MUSTOWN|SFF_ROOTOK;
1483 if (!bitnset(DBS_GROUPWRITABLEFORWARDFILE, DontBlameSendmail))
1484 sfflags |= SFF_NOGWFILES;
1485 if (!bitnset(DBS_WORLDWRITABLEFORWARDFILE, DontBlameSendmail))
1486 sfflags |= SFF_NOWWFILES;
1490 if (!bitnset(DBS_GROUPWRITABLEINCLUDEFILE, DontBlameSendmail))
1491 sfflags |= SFF_NOGWFILES;
1492 if (!bitnset(DBS_WORLDWRITABLEINCLUDEFILE, DontBlameSendmail))
1493 sfflags |= SFF_NOWWFILES;
1497 ** If RunAsUser set, won't be able to run programs as user
1498 ** so mark them as unsafe unless the administrator knows better.
1501 if ((geteuid() != 0 || RunAsUid != 0) &&
1502 !bitnset(DBS_NONROOTSAFEADDR, DontBlameSendmail))
1505 sm_dprintf("include: not safe (euid=%ld, RunAsUid=%ld)\n",
1506 (long) geteuid(), (long) RunAsUid);
1507 ctladdr->q_flags |= QUNSAFEADDR;
1510 ca = getctladdr(ctladdr);
1512 (ca->q_uid == DefUid && ca->q_gid == 0))
1524 #if MAILER_SETUID_METHOD != USE_SETUID
1525 saveduid = geteuid();
1526 savedgid = getegid();
1529 if (!DontInitGroups)
1531 if (initgroups(user, gid) == -1)
1534 syserr("include: initgroups(%s, %ld) failed",
1544 if (setgroups(1, gidset) == -1)
1547 syserr("include: setgroups() failed");
1552 if (gid != 0 && setgid(gid) < -1)
1555 syserr("setgid(%ld) failure", (long) gid);
1560 # if MAILER_SETUID_METHOD == USE_SETEUID
1561 if (seteuid(uid) < 0)
1564 syserr("seteuid(%ld) failure (real=%ld, eff=%ld)",
1565 (long) uid, (long) getuid(), (long) geteuid());
1568 # endif /* MAILER_SETUID_METHOD == USE_SETEUID */
1569 # if MAILER_SETUID_METHOD == USE_SETREUID
1570 if (setreuid(0, uid) < 0)
1573 syserr("setreuid(0, %ld) failure (real=%ld, eff=%ld)",
1574 (long) uid, (long) getuid(), (long) geteuid());
1577 # endif /* MAILER_SETUID_METHOD == USE_SETREUID */
1580 #endif /* MAILER_SETUID_METHOD != USE_SETUID */
1583 sm_dprintf("include: new uid = %ld/%ld\n",
1584 (long) getuid(), (long) geteuid());
1587 ** If home directory is remote mounted but server is down,
1588 ** this can hang or give errors; use a timeout to avoid this
1591 if (setjmp(CtxIncludeTimeout) != 0)
1593 ctladdr->q_state = QS_QUEUEUP;
1596 /* return pseudo-error code */
1597 rval = E_SM_OPENTIMEOUT;
1600 if (TimeOuts.to_fileopen > 0)
1601 ev = sm_setevent(TimeOuts.to_fileopen, includetimeout, 0);
1605 /* check for writable parent directory */
1606 p = strrchr(fname, '/');
1612 ret = safedirpath(fname, uid, gid, user,
1613 sfflags|SFF_SAFEDIRPATH, 0, 0);
1616 /* in safe directory: relax chown & link rules */
1618 sfflags |= SFF_NOPATHCHECK;
1622 if (bitnset((forwarding ?
1623 DBS_FORWARDFILEINUNSAFEDIRPATH :
1624 DBS_INCLUDEFILEINUNSAFEDIRPATH),
1626 sfflags |= SFF_NOPATHCHECK;
1627 else if (bitnset((forwarding ?
1628 DBS_FORWARDFILEINGROUPWRITABLEDIRPATH :
1629 DBS_INCLUDEFILEINGROUPWRITABLEDIRPATH),
1630 DontBlameSendmail) &&
1633 setbitn(DBS_GROUPWRITABLEDIRPATHSAFE,
1635 ret = safedirpath(fname, uid, gid, user,
1636 sfflags|SFF_SAFEDIRPATH,
1638 clrbitn(DBS_GROUPWRITABLEDIRPATHSAFE,
1641 sfflags |= SFF_NOPATHCHECK;
1643 sfflags |= SFF_SAFEDIRPATH;
1646 sfflags |= SFF_SAFEDIRPATH;
1647 if (ret > E_PSEUDOBASE &&
1648 !bitnset((forwarding ?
1649 DBS_FORWARDFILEINUNSAFEDIRPATHSAFE :
1650 DBS_INCLUDEFILEINUNSAFEDIRPATHSAFE),
1654 sm_syslog(LOG_INFO, e->e_id,
1655 "%s: unsafe directory path, marked unsafe",
1656 shortenstring(fname, MAXSHORTSTR));
1657 ctladdr->q_flags |= QUNSAFEADDR;
1663 /* allow links only in unwritable directories */
1665 !bitnset((forwarding ?
1666 DBS_LINKEDFORWARDFILEINWRITABLEDIR :
1667 DBS_LINKEDINCLUDEFILEINWRITABLEDIR),
1669 sfflags |= SFF_NOLINK;
1671 rval = safefile(fname, uid, gid, user, sfflags, S_IREAD, &st);
1674 /* don't use this :include: file */
1676 sm_dprintf("include: not safe (uid=%ld): %s\n",
1677 (long) uid, sm_errstring(rval));
1679 else if ((fp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, fname,
1680 SM_IO_RDONLY, NULL)) == NULL)
1684 sm_dprintf("include: open: %s\n", sm_errstring(rval));
1686 else if (filechanged(fname, sm_io_getinfo(fp,SM_IO_WHAT_FD, NULL), &st))
1688 rval = E_SM_FILECHANGE;
1690 sm_dprintf("include: file changed after open\n");
1697 #if HASSETREUID || USESETEUID
1704 syserr("!seteuid(0) failure (real=%ld, eff=%ld)",
1705 (long) getuid(), (long) geteuid());
1706 # else /* USESETEUID */
1707 if (setreuid(-1, 0) < 0)
1708 syserr("!setreuid(-1, 0) failure (real=%ld, eff=%ld)",
1709 (long) getuid(), (long) geteuid());
1710 if (setreuid(RealUid, 0) < 0)
1711 syserr("!setreuid(%ld, 0) failure (real=%ld, eff=%ld)",
1712 (long) RealUid, (long) getuid(),
1714 # endif /* USESETEUID */
1716 if (setgid(savedgid) < 0)
1717 syserr("!setgid(%ld) failure (real=%ld eff=%ld)",
1718 (long) savedgid, (long) getgid(),
1721 #endif /* HASSETREUID || USESETEUID */
1724 sm_dprintf("include: reset uid = %ld/%ld\n",
1725 (long) getuid(), (long) geteuid());
1727 if (rval == E_SM_OPENTIMEOUT)
1728 usrerr("451 4.4.1 open timeout on %s", fname);
1733 if (fstat(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), &st) < 0)
1736 syserr("Cannot fstat %s!", fname);
1737 (void) sm_io_close(fp, SM_TIME_DEFAULT);
1741 /* if path was writable, check to avoid file giveaway tricks */
1742 safechown = chownsafe(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), safedir);
1744 sm_dprintf("include: parent of %s is %s, chown is %ssafe\n",
1745 fname, safedir ? "safe" : "dangerous",
1746 safechown ? "" : "un");
1748 /* if no controlling user or coming from an alias delivery */
1751 (ca->q_uid == DefUid && ca->q_gid == 0)))
1753 ctladdr->q_uid = st.st_uid;
1754 ctladdr->q_gid = st.st_gid;
1755 ctladdr->q_flags |= QGOODUID;
1757 if (ca != NULL && ca->q_uid == st.st_uid)
1759 /* optimization -- avoid getpwuid if we already have info */
1760 ctladdr->q_flags |= ca->q_flags & QBOGUSSHELL;
1761 ctladdr->q_ruser = ca->q_ruser;
1763 else if (!forwarding)
1765 register struct passwd *pw;
1767 pw = sm_getpwuid(st.st_uid);
1770 ctladdr->q_uid = st.st_uid;
1771 ctladdr->q_flags |= QBOGUSSHELL;
1777 ctladdr->q_ruser = sm_rpool_strdup_x(e->e_rpool,
1782 sh = "/SENDMAIL/ANY/SHELL/";
1783 if (!usershellok(pw->pw_name, sh))
1786 sm_syslog(LOG_INFO, e->e_id,
1787 "%s: user %s has bad shell %s, marked %s",
1788 shortenstring(fname,
1791 safechown ? "bogus" : "unsafe");
1793 ctladdr->q_flags |= QBOGUSSHELL;
1795 ctladdr->q_flags |= QUNSAFEADDR;
1800 if (bitset(EF_VRFYONLY, e->e_flags))
1802 /* don't do any more now */
1803 ctladdr->q_state = QS_VERIFIED;
1805 (void) sm_io_close(fp, SM_TIME_DEFAULT);
1810 ** Check to see if some bad guy can write this file
1812 ** Group write checking could be more clever, e.g.,
1813 ** guessing as to which groups are actually safe ("sys"
1814 ** may be; "user" probably is not).
1818 if (!bitnset((forwarding ?
1819 DBS_GROUPWRITABLEFORWARDFILESAFE :
1820 DBS_GROUPWRITABLEINCLUDEFILESAFE),
1824 if (bitset(mode, st.st_mode))
1827 sm_dprintf("include: %s is %s writable, marked unsafe\n",
1828 shortenstring(fname, MAXSHORTSTR),
1829 bitset(S_IWOTH, st.st_mode) ? "world"
1832 sm_syslog(LOG_INFO, e->e_id,
1833 "%s: %s writable %s file, marked unsafe",
1834 shortenstring(fname, MAXSHORTSTR),
1835 bitset(S_IWOTH, st.st_mode) ? "world" : "group",
1836 forwarding ? "forward" : ":include:");
1837 ctladdr->q_flags |= QUNSAFEADDR;
1840 /* read the file -- each line is a comma-separated list. */
1843 ctladdr->q_flags &= ~QSELFREF;
1845 while (sm_io_fgets(fp, SM_TIME_DEFAULT, buf, sizeof(buf)) >= 0 &&
1850 if (buf[0] == '#' || buf[0] == '\0')
1853 /* <sp>#@# introduces a comment anywhere */
1854 /* for Japanese character sets */
1855 for (p = buf; (p = strchr(++p, '#')) != NULL; )
1857 if (p[1] == '@' && p[2] == '#' &&
1858 isascii(p[-1]) && isspace(p[-1]) &&
1859 (p[3] == '\0' || (SM_ISSPACE(p[3]))))
1862 while (p > buf && isascii(p[-1]) &&
1874 forwarding ? "forwarding" : "sending", buf);
1875 if (forwarding && LogLevel > 10)
1876 sm_syslog(LOG_INFO, e->e_id,
1877 "forward %.200s => %s",
1878 oldto, shortenstring(buf, MAXSHORTSTR));
1880 nincludes += sendtolist(buf, ctladdr, sendq, aliaslevel + 1, e);
1883 MaxForwardEntries > 0 &&
1884 nincludes >= MaxForwardEntries)
1886 /* just stop reading and processing further entries */
1888 /* additional: (?) */
1889 ctladdr->q_state = QS_DONTSEND;
1892 syserr("Attempt to forward to more than %d addresses (in %s)!",
1893 MaxForwardEntries, fname);
1898 if (sm_io_error(fp) && tTd(27, 3))
1899 sm_dprintf("include: read error: %s\n", sm_errstring(errno));
1900 if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
1902 if (aliaslevel <= MaxAliasRecursion ||
1903 ctladdr->q_state != QS_BADADDR)
1905 ctladdr->q_state = QS_DONTSEND;
1908 sm_dprintf("include: QS_DONTSEND ");
1909 printaddr(sm_debug_file(), ctladdr, false);
1914 (void) sm_io_close(fp, SM_TIME_DEFAULT);
1915 FileName = oldfilename;
1916 LineNumber = oldlinenumber;
1922 includetimeout(ignore)
1926 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
1927 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1932 longjmp(CtxIncludeTimeout, 1);
1936 ** SENDTOARGV -- send to an argument vector.
1939 ** argv -- argument vector to send to.
1940 ** e -- the current envelope.
1946 ** puts all addresses on the argument vector onto the
1952 register char **argv;
1953 register ENVELOPE *e;
1963 while ((p = *av++) != NULL)
1965 if (!addr_is_ascii(p))
1967 e->e_smtputf8 = true;
1972 #endif /* USE_EAI */
1974 while ((p = *argv++) != NULL)
1981 if (!SMTP_UTF8 && !asciistr(p))
1983 usrerr("non-ASCII recipient address %s requires SMTPUTF8",
1985 finis(false, true, EX_USAGE);
1987 p = quote_internal_chars(p, NULL, &len, NULL);
1989 #endif /* USE_EAI */
1990 (void) sendtolist(p, NULLADDR, &e->e_sendqueue, 0, e);
1995 ** GETCTLADDR -- get controlling address from an address header.
1997 ** If none, get one corresponding to the effective userid.
2000 ** a -- the address to find the controller of.
2003 ** the controlling address.
2008 register ADDRESS *a;
2010 while (a != NULL && !bitset(QGOODUID, a->q_flags))
2016 ** SELF_REFERENCE -- check to see if an address references itself
2018 ** The check is done through a chain of aliases. If it is part of
2019 ** a loop, break the loop at the "best" address, that is, the one
2020 ** that exists as a real user.
2022 ** This is to handle the case of:
2023 ** awc: Andrew.Chang
2024 ** Andrew.Chang: awc@mail.server
2025 ** which is a problem only on mail.server.
2028 ** a -- the address to check.
2031 ** The address that should be retained.
2038 ADDRESS *b; /* top entry in self ref loop */
2039 ADDRESS *c; /* entry that point to a real mail box */
2042 sm_dprintf("self_reference(%s)\n", a->q_paddr);
2044 for (b = a->q_alias; b != NULL; b = b->q_alias)
2053 sm_dprintf("\t... no self ref\n");
2058 ** Pick the first address that resolved to a real mail box
2059 ** i.e has a mbdb entry. The returned value will be marked
2060 ** QSELFREF in recipient(), which in turn will disable alias()
2061 ** from marking it as QS_IS_DEAD(), which mean it will be used
2062 ** as a deliverable address.
2064 ** The 2 key thing to note here are:
2065 ** 1) we are in a recursive call sequence:
2066 ** alias->sendtolist->recipient->alias
2067 ** 2) normally, when we return back to alias(), the address
2068 ** will be marked QS_EXPANDED, since alias() assumes the
2069 ** expanded form will be used instead of the current address.
2070 ** This behaviour is turned off if the address is marked
2071 ** QSELFREF. We set QSELFREF when we return to recipient().
2078 sm_dprintf(" %s", c->q_user);
2079 if (bitnset(M_HASPWENT, c->q_mailer->m_flags))
2084 sm_dprintf("\t... getpwnam(%s)... ", c->q_user);
2085 if (sm_mbdb_lookup(c->q_user, &user) == EX_OK)
2088 sm_dprintf("found\n");
2090 /* ought to cache results here */
2097 sm_dprintf("failed\n");
2101 /* if local delivery, compare usernames */
2102 if (bitnset(M_LOCALMAILER, c->q_mailer->m_flags) &&
2103 b->q_mailer == c->q_mailer)
2106 sm_dprintf("\t... local match (%s)\n",
2120 sm_dprintf("\t... cannot break loop for \"%s\"\n", a->q_paddr);