2 * Copyright (c) 1998-2002 Sendmail, 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: envelope.c,v 8.282.2.2 2002/12/04 15:44:08 ca Exp $")
19 ** CLRSESSENVELOPE -- clear session oriented data in an envelope
22 ** e -- the envelope to clear.
33 macdefine(&e->e_macro, A_PERM, macid("{auth_type}"), "");
34 macdefine(&e->e_macro, A_PERM, macid("{auth_authen}"), "");
35 macdefine(&e->e_macro, A_PERM, macid("{auth_author}"), "");
36 macdefine(&e->e_macro, A_PERM, macid("{auth_ssf}"), "");
39 macdefine(&e->e_macro, A_PERM, macid("{cert_issuer}"), "");
40 macdefine(&e->e_macro, A_PERM, macid("{cert_subject}"), "");
41 macdefine(&e->e_macro, A_PERM, macid("{cipher_bits}"), "");
42 macdefine(&e->e_macro, A_PERM, macid("{cipher}"), "");
43 macdefine(&e->e_macro, A_PERM, macid("{tls_version}"), "");
44 macdefine(&e->e_macro, A_PERM, macid("{verify}"), "");
46 macdefine(&e->e_macro, A_PERM, macid("{alg_bits}"), "");
47 macdefine(&e->e_macro, A_PERM, macid("{cn_issuer}"), "");
48 macdefine(&e->e_macro, A_PERM, macid("{cn_subject}"), "");
49 # endif /* _FFR_TLS_1 */
54 ** NEWENVELOPE -- fill in a new envelope
56 ** Supports inheritance.
59 ** e -- the new envelope to fill in.
60 ** parent -- the envelope to be the parent of e.
61 ** rpool -- either NULL, or a pointer to a resource pool
62 ** from which envelope memory is allocated, and
63 ** to which envelope resources are attached.
73 newenvelope(e, parent, rpool)
75 register ENVELOPE *parent;
79 ** This code used to read:
80 ** if (e == parent && e->e_parent != NULL)
81 ** parent = e->e_parent;
82 ** So if e == parent && e->e_parent == NULL then we would
83 ** set e->e_parent = e, which creates a loop in the e_parent chain.
84 ** This meant macvalue() could go into an infinite loop.
89 clearenvelope(e, true, rpool);
91 memmove((char *) &e->e_from,
92 (char *) &NullAddress,
95 memmove((char *) &e->e_from,
96 (char *) &CurEnv->e_from,
100 e->e_ctime = curtime();
103 e->e_msgpriority = parent->e_msgsize;
105 if (parent->e_quarmsg == NULL)
108 macdefine(&e->e_macro, A_PERM,
109 macid("{quarantine}"), "");
113 e->e_quarmsg = sm_rpool_strdup_x(rpool,
115 macdefine(&e->e_macro, A_PERM,
116 macid("{quarantine}"), e->e_quarmsg);
118 #endif /* _FFR_QUARANTINE */
120 e->e_puthdr = putheader;
121 e->e_putbody = putbody;
122 if (CurEnv->e_xfp != NULL)
123 (void) sm_io_flush(CurEnv->e_xfp, SM_TIME_DEFAULT);
128 /* values for msg_timeout, see also IS_* below for usage (bit layout) */
129 #define MSG_T_O 0x01 /* normal timeout */
130 #define MSG_T_O_NOW 0x02 /* NOW timeout */
131 #define MSG_NOT_BY 0x04 /* Deliver-By time exceeded, mode R */
132 #define MSG_WARN 0x10 /* normal queue warning */
133 #define MSG_WARN_BY 0x20 /* Deliver-By time exceeded, mode N */
135 #define IS_MSG_ERR(x) (((x) & 0x0f) != 0) /* return an error */
137 /* immediate return */
138 #define IS_IMM_RET(x) (((x) & (MSG_T_O_NOW|MSG_NOT_BY)) != 0)
139 #define IS_MSG_WARN(x) (((x) & 0xf0) != 0) /* return a warning */
142 ** DROPENVELOPE -- deallocate an envelope.
145 ** e -- the envelope to deallocate.
146 ** fulldrop -- if set, do return receipts.
147 ** split -- if true, split by recipient if message is queued up
153 ** housekeeping necessary to dispose of an envelope.
154 ** Unlocks this queue file.
158 dropenvelope(e, fulldrop, split)
159 register ENVELOPE *e;
164 bool queueit = false;
166 bool failure_return = false;
167 bool delay_return = false;
168 bool success_return = false;
169 bool pmnotify = bitset(EF_PM_NOTIFY, e->e_flags);
178 sm_dprintf("dropenvelope %p: id=", e);
180 sm_dprintf(", flags=");
184 sm_dprintf("sendq=");
185 printaddr(e->e_sendqueue, true);
190 sm_syslog(LOG_DEBUG, id,
191 "dropenvelope, e_flags=0x%lx, OpMode=%c, pid=%d",
192 e->e_flags, OpMode, (int) CurrentPid);
194 /* we must have an id to remove disk files */
198 /* if verify-only mode, we can skip most of this */
199 if (OpMode == MD_VERIFY)
202 if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
204 e->e_flags &= ~EF_LOGSENDER;
206 /* post statistics */
210 ** Extract state information from dregs of send list.
214 if (now >= e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass])
215 msg_timeout = MSG_T_O;
216 if (IS_DLVR_RETURN(e) && e->e_deliver_by > 0 &&
217 now >= e->e_ctime + e->e_deliver_by &&
218 !bitset(EF_RESPONSE, e->e_flags))
220 msg_timeout = MSG_NOT_BY;
221 e->e_flags |= EF_FATALERRS|EF_CLRQUEUE;
223 else if (TimeOuts.to_q_return[e->e_timeoutclass] == NOW &&
224 !bitset(EF_RESPONSE, e->e_flags))
226 msg_timeout = MSG_T_O_NOW;
227 e->e_flags |= EF_FATALERRS|EF_CLRQUEUE;
230 e->e_flags &= ~EF_QUEUERUN;
231 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
233 if (QS_IS_UNDELIVERED(q->q_state))
236 /* see if a notification is needed */
237 if (bitset(QPINGONFAILURE, q->q_flags) &&
238 ((IS_MSG_ERR(msg_timeout) &&
239 QS_IS_UNDELIVERED(q->q_state)) ||
240 QS_IS_BADADDR(q->q_state) ||
241 IS_IMM_RET(msg_timeout)))
243 failure_return = true;
244 if (!done && q->q_owner == NULL &&
245 !emptyaddr(&e->e_from))
247 (void) sendtolist(e->e_from.q_paddr, NULLADDR,
248 &e->e_errorqueue, 0, e);
252 else if ((bitset(QPINGONSUCCESS, q->q_flags) &&
253 ((QS_IS_SENT(q->q_state) &&
254 bitnset(M_LOCALMAILER, q->q_mailer->m_flags)) ||
255 bitset(QRELAYED|QEXPANDED|QDELIVERED, q->q_flags))) ||
256 bitset(QBYTRACE, q->q_flags) ||
257 bitset(QBYNRELAY, q->q_flags))
259 success_return = true;
264 e->e_flags |= EF_NO_BODY_RETN;
267 ** See if the message timed out.
272 /* nothing to do */ ;
273 else if (IS_MSG_ERR(msg_timeout))
277 if (msg_timeout == MSG_NOT_BY)
279 (void) sm_snprintf(buf, sizeof buf,
280 "delivery time expired %lds",
285 (void) sm_snprintf(buf, sizeof buf,
286 "Cannot send message for %s",
287 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
291 /* don't free, allocated from e_rpool */
292 e->e_message = sm_rpool_strdup_x(e->e_rpool, buf);
294 e->e_flags |= EF_CLRQUEUE;
296 if (msg_timeout == MSG_NOT_BY)
298 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
299 "Delivery time (%lds) expired\n",
303 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
304 "Message could not be delivered for %s\n",
305 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
307 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
308 "Message will be deleted from queue\n");
309 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
311 if (QS_IS_UNDELIVERED(q->q_state))
313 q->q_state = QS_BADADDR;
314 if (msg_timeout == MSG_NOT_BY)
315 q->q_status = "5.4.7";
317 q->q_status = "4.4.7";
323 if (TimeOuts.to_q_warning[e->e_timeoutclass] > 0 &&
325 TimeOuts.to_q_warning[e->e_timeoutclass])
326 msg_timeout = MSG_WARN;
327 else if (IS_DLVR_NOTIFY(e) &&
328 e->e_deliver_by > 0 &&
329 now >= e->e_ctime + e->e_deliver_by)
330 msg_timeout = MSG_WARN_BY;
332 if (IS_MSG_WARN(msg_timeout))
334 if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
336 e->e_from.q_paddr != NULL &&
337 strcmp(e->e_from.q_paddr, "<>") != 0 &&
338 sm_strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 &&
339 (strlen(e->e_from.q_paddr) <= 8 ||
340 sm_strcasecmp(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8],
343 for (q = e->e_sendqueue; q != NULL;
346 if (QS_IS_UNDELIVERED(q->q_state)
347 #if _FFR_NODELAYDSN_ON_HOLD
349 q->q_mailer->m_flags)
350 #endif /* _FFR_NODELAYDSN_ON_HOLD */
355 (bitset(QPINGONDELAY,
361 q->q_flags |= QBYNDELAY;
364 if (bitset(QPINGONDELAY,
367 q->q_flags |= QDELAYED;
375 if (msg_timeout == MSG_WARN_BY)
377 (void) sm_snprintf(buf, sizeof buf,
378 "Warning: Delivery time (%lds) exceeded",
382 (void) sm_snprintf(buf, sizeof buf,
383 "Warning: could not send message for past %s",
384 pintvl(TimeOuts.to_q_warning[e->e_timeoutclass],
387 /* don't free, allocated from e_rpool */
388 e->e_message = sm_rpool_strdup_x(e->e_rpool,
391 e->e_flags |= EF_WARNING;
393 if (msg_timeout == MSG_WARN_BY)
395 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
396 "Warning: Delivery time (%lds) exceeded\n",
400 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
401 "Warning: message still undelivered after %s\n",
402 pintvl(TimeOuts.to_q_warning[e->e_timeoutclass],
404 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
405 "Will keep trying until message is %s old\n",
406 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
412 sm_dprintf("failure_return=%d delay_return=%d success_return=%d queueit=%d\n",
413 failure_return, delay_return, success_return, queueit);
416 ** If we had some fatal error, but no addresses are marked as
417 ** bad, mark them _all_ as bad.
420 if (bitset(EF_FATALERRS, e->e_flags) && !failure_return)
422 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
424 if ((QS_IS_OK(q->q_state) ||
425 QS_IS_VERIFIED(q->q_state)) &&
426 bitset(QPINGONFAILURE, q->q_flags))
428 failure_return = true;
429 q->q_state = QS_BADADDR;
435 ** Send back return receipts as requested.
438 if (success_return && !failure_return && !delay_return && fulldrop &&
439 !bitset(PRIV_NORECEIPTS, PrivacyFlags) &&
440 strcmp(e->e_from.q_paddr, "<>") != 0)
442 auto ADDRESS *rlist = NULL;
445 sm_dprintf("dropenvelope(%s): sending return receipt\n",
447 e->e_flags |= EF_SENDRECEIPT;
448 (void) sendtolist(e->e_from.q_paddr, NULLADDR, &rlist, 0, e);
449 (void) returntosender("Return receipt", rlist, RTSF_NO_BODY, e);
451 e->e_flags &= ~EF_SENDRECEIPT;
454 ** Arrange to send error messages if there are fatal errors.
457 if ((failure_return || delay_return) && e->e_errormode != EM_QUIET)
460 sm_dprintf("dropenvelope(%s): saving mail\n", id);
461 panic = savemail(e, !bitset(EF_NO_BODY_RETN, e->e_flags));
465 ** Arrange to send warning messages to postmaster as requested.
468 if ((failure_return || pmnotify) &&
469 PostMasterCopy != NULL &&
470 !bitset(EF_RESPONSE, e->e_flags) &&
473 auto ADDRESS *rlist = NULL;
478 expand(PostMasterCopy, pcopy, sizeof pcopy, e);
481 sm_dprintf("dropenvelope(%s): sending postmaster copy to %s\n",
483 (void) sendtolist(pcopy, NULLADDR, &rlist, 0, e);
486 (void) sendtolist("postmaster", NULLADDR,
488 (void) returntosender(e->e_message, rlist,
489 RTSF_PM_BOUNCE|RTSF_NO_BODY, e);
493 ** Instantiate or deinstantiate the queue.
498 sm_dprintf("dropenvelope(%s): at simpledrop, queueit=%d\n",
500 if (!queueit || bitset(EF_CLRQUEUE, e->e_flags))
504 sm_dprintf("\n===== Dropping queue files for %s... queueit=%d, e_flags=",
509 (void) xunlink(queuename(e, DATAFL_LETTER));
511 if (panic && QueueMode == QM_LOST)
514 ** leave the Qf file behind as
515 ** the delivery attempt failed.
521 #endif /* _FFR_QUARANTINE */
522 if (xunlink(queuename(e, ANYQFL_LETTER)) == 0)
524 /* add to available space in filesystem */
525 updfs(e, true, !panic);
528 if (e->e_ntries > 0 && LogLevel > 9)
529 sm_syslog(LOG_INFO, id, "done; delay=%s, ntries=%d",
530 pintvl(curtime() - e->e_ctime, true),
533 else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
536 queueup(e, false, true);
543 ** Save old sibling and set it to NULL to avoid
544 ** queueing up the same envelopes again.
545 ** This requires that envelopes in that list have
546 ** been take care of before (or at some other place).
549 oldsib = e->e_sibling;
551 if (!split_by_recipient(e) &&
552 bitset(EF_FATALERRS, e->e_flags))
554 syserr("!dropenvelope(%s): cannot commit data file %s, uid=%d",
555 e->e_id, queuename(e, DATAFL_LETTER),
558 for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling)
559 queueup(ee, false, true);
560 queueup(e, false, true);
563 for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling)
565 /* now unlock the job */
567 sm_dprintf("dropenvelope(%s): unlocking job\n",
572 /* this envelope is marked unused */
573 if (ee->e_dfp != NULL)
575 (void) sm_io_close(ee->e_dfp,
580 ee->e_flags &= ~EF_HAS_DF;
582 e->e_sibling = oldsib;
586 /* now unlock the job */
588 sm_dprintf("dropenvelope(%s): unlocking job\n", id);
592 /* make sure that this envelope is marked unused */
593 if (e->e_dfp != NULL)
595 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
599 e->e_flags &= ~EF_HAS_DF;
602 ** CLEARENVELOPE -- clear an envelope without unlocking
604 ** This is normally used by a child process to get a clean
605 ** envelope without disturbing the parent.
608 ** e -- the envelope to clear.
609 ** fullclear - if set, the current envelope is total
610 ** garbage and should be ignored; otherwise,
611 ** release any resources it may indicate.
612 ** rpool -- either NULL, or a pointer to a resource pool
613 ** from which envelope memory is allocated, and
614 ** to which envelope resources are attached.
620 ** Closes files associated with the envelope.
621 ** Marks the envelope as unallocated.
625 clearenvelope(e, fullclear, rpool)
626 register ENVELOPE *e;
632 extern ENVELOPE BlankEnvelope;
637 /* clear out any file information */
638 if (e->e_xfp != NULL)
639 (void) sm_io_close(e->e_xfp, SM_TIME_DEFAULT);
640 if (e->e_dfp != NULL)
641 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
642 e->e_xfp = e->e_dfp = NULL;
646 ** Copy BlankEnvelope into *e.
647 ** It is not safe to simply copy pointers to strings;
648 ** the strings themselves must be copied (or set to NULL).
649 ** The problem is that when we assign a new string value to
650 ** a member of BlankEnvelope, we free the old string.
651 ** We did not need to do this copying in sendmail 8.11 :-(
652 ** and it is a potential performance hit. Reference counted
653 ** strings are one way out.
659 e->e_qfletter = '\0';
661 macdefine(&e->e_macro, A_PERM, macid("{quarantine}"), "");
662 #endif /* _FFR_QUARANTINE */
665 ** Copy the macro table.
666 ** We might be able to avoid this by zeroing the macro table
667 ** and always searching BlankEnvelope.e_macro after e->e_macro
671 for (p = &e->e_macro.mac_table[0];
672 p <= &e->e_macro.mac_table[MAXMACROID];
676 *p = sm_rpool_strdup_x(rpool, *p);
680 ** XXX There are many strings in the envelope structure
681 ** XXX that we are not attempting to copy here.
682 ** XXX Investigate this further.
686 e->e_macro.mac_rpool = rpool;
688 set_delivery_mode(SM_DELIVER, e);
689 bh = BlankEnvelope.e_header;
693 *nhp = (HDR *) sm_rpool_malloc_x(rpool, sizeof *bh);
694 memmove((char *) *nhp, (char *) bh, sizeof *bh);
696 nhp = &(*nhp)->h_link;
700 ** INITSYS -- initialize instantiation of system
702 ** In Daemon mode, this is done in the child.
705 ** e -- the envelope to use.
711 ** Initializes the system macros, some global variables,
712 ** etc. In particular, the current time in various
718 register ENVELOPE *e;
722 static char ybuf[60]; /* holds tty id */
724 extern char *ttyname();
728 ** Give this envelope a reality.
729 ** I.e., an id, a transcript, and a creation time.
730 ** We don't select the queue until all of the recipients are known.
734 e->e_ctime = curtime();
736 e->e_qfletter = '\0';
737 #endif /* _FFR_QUARANTINE */
739 e->e_queuealg = QueueAlg;
740 e->e_queuedelay = QueueInitDelay;
741 #endif /* _FFR_QUEUEDELAY */
744 ** Set OutChannel to something useful if stdout isn't it.
745 ** This arranges that any extra stuff the mailer produces
746 ** gets sent back to the user on error (because it is
747 ** tucked away in the transcript).
750 if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) &&
752 OutChannel = e->e_xfp;
755 ** Set up some basic system macros.
759 (void) sm_snprintf(buf, sizeof buf, "%d", (int) CurrentPid);
760 macdefine(&e->e_macro, A_TEMP, 'p', buf);
763 (void) sm_snprintf(buf, sizeof buf, "%d", e->e_hopcount);
764 macdefine(&e->e_macro, A_TEMP, 'c', buf);
766 /* time as integer, unix time, arpa time */
774 if (macvalue('y', e) == NULL)
779 if (strrchr(p, '/') != NULL)
780 p = strrchr(p, '/') + 1;
781 (void) sm_strlcpy(ybuf, sizeof ybuf, p);
782 macdefine(&e->e_macro, A_PERM, 'y', ybuf);
788 ** SETTIME -- set the current time.
791 ** e -- the envelope in which the macros should be set.
797 ** Sets the various time macros -- $a, $b, $d, $t.
802 register ENVELOPE *e;
807 register struct tm *tm;
811 (void) sm_snprintf(buf, sizeof buf, "%04d%02d%02d%02d%02d",
812 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
813 tm->tm_hour, tm->tm_min);
814 macdefine(&e->e_macro, A_TEMP, 't', buf);
815 (void) sm_strlcpy(buf, ctime(&now), sizeof buf);
816 p = strchr(buf, '\n');
819 macdefine(&e->e_macro, A_TEMP, 'd', buf);
820 macdefine(&e->e_macro, A_TEMP, 'b', arpadate(buf));
821 if (macvalue('a', e) == NULL)
822 macdefine(&e->e_macro, A_PERM, 'a', macvalue('b', e));
825 ** OPENXSCRIPT -- Open transcript file
827 ** Creates a transcript file for possible eventual mailing or
831 ** e -- the envelope to create the transcript in/for.
837 ** Creates the transcript file.
842 #endif /* ! O_APPEND */
846 register ENVELOPE *e;
850 if (e->e_xfp != NULL)
854 if (e->e_lockfp == NULL && bitset(EF_INQUEUE, e->e_flags))
855 syserr("openxscript: job not locked");
858 p = queuename(e, XSCRPT_LETTER);
859 e->e_xfp = bfopen(p, FileMode, XscriptFileBufferSize,
860 SFF_NOTEXCL|SFF_OPENASROOT);
862 if (e->e_xfp == NULL)
864 syserr("Can't create transcript file %s", p);
865 e->e_xfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT,
866 SM_PATH_DEVNULL, SM_IO_RDWR, NULL);
867 if (e->e_xfp == NULL)
868 syserr("!Can't open %s", SM_PATH_DEVNULL);
870 (void) sm_io_setvbuf(e->e_xfp, SM_TIME_DEFAULT, NULL, SM_IO_LBF, 0);
873 sm_dprintf("openxscript(%s):\n ", p);
874 dumpfd(sm_io_getinfo(e->e_xfp, SM_IO_WHAT_FD, NULL), true,
879 ** CLOSEXSCRIPT -- close the transcript file.
882 ** e -- the envelope containing the transcript to close.
893 register ENVELOPE *e;
895 if (e->e_xfp == NULL)
898 if (e->e_lockfp == NULL)
899 syserr("closexscript: job not locked");
901 (void) sm_io_close(e->e_xfp, SM_TIME_DEFAULT);
905 ** SETSENDER -- set the person who this message is from
907 ** Under certain circumstances allow the user to say who
908 ** s/he is (using -f or -r). These are:
909 ** 1. The user's uid is zero (root).
910 ** 2. The user's login name is in an approved list (typically
911 ** from a network server).
912 ** 3. The address the user is trying to claim has a
913 ** "!" character in it (since #2 doesn't do it for
914 ** us if we are dialing out for UUCP).
915 ** A better check to replace #3 would be if the
916 ** effective uid is "UUCP" -- this would require me
917 ** to rewrite getpwent to "grab" uucp as it went by,
918 ** make getname more nasty, do another passwd file
919 ** scan, or compile the UID of "UUCP" into the code,
920 ** all of which are reprehensible.
922 ** Assuming all of these fail, we figure out something
926 ** from -- the person we would like to believe this message
927 ** is from, as specified on the command line.
928 ** e -- the envelope in which we would like the sender set.
929 ** delimptr -- if non-NULL, set to the location of the
930 ** trailing delimiter.
931 ** delimchar -- the character that will delimit the sender
933 ** internal -- set if this address is coming from an internal
934 ** source such as an owner alias.
940 ** sets sendmail's notion of who the from person is.
944 setsender(from, e, delimptr, delimchar, internal)
946 register ENVELOPE *e;
952 char *realname = NULL;
954 char buf[MAXNAME + 2];
955 char pvpbuf[PSBUFSIZE];
956 extern char *FullName;
959 sm_dprintf("setsender(%s)\n", from == NULL ? "" : from);
961 /* may be set from earlier calls */
962 macdefine(&e->e_macro, A_PERM, 'x', "");
965 ** Figure out the real user executing us.
966 ** Username can return errno != 0 on non-errors.
969 if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP ||
970 OpMode == MD_ARPAFTP || OpMode == MD_DAEMON)
972 if (realname == NULL || realname[0] == '\0')
973 realname = username();
978 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e s");
980 /* preset state for then clause in case from == NULL */
981 e->e_from.q_state = QS_BADADDR;
982 e->e_from.q_flags = 0;
984 parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR,
985 delimchar, delimptr, e, false) == NULL ||
986 QS_IS_BADADDR(e->e_from.q_state) ||
987 e->e_from.q_mailer == ProgMailer ||
988 e->e_from.q_mailer == FileMailer ||
989 e->e_from.q_mailer == InclMailer)
991 /* log garbage addresses for traceback */
992 if (from != NULL && LogLevel > 2)
995 char ebuf[MAXNAME * 2 + 2];
997 p = macvalue('_', e);
1000 char *host = RealHostName;
1004 (void) sm_snprintf(ebuf, sizeof ebuf,
1005 "%.*s@%.*s", MAXNAME,
1006 realname, MAXNAME, host);
1009 sm_syslog(LOG_NOTICE, e->e_id,
1010 "setsender: %s: invalid or unparsable, received from %s",
1011 shortenstring(from, 83), p);
1015 if (!QS_IS_BADADDR(e->e_from.q_state))
1017 /* it was a bogus mailer in the from addr */
1018 e->e_status = "5.1.7";
1019 usrerrenh(e->e_status,
1020 "553 Invalid sender address");
1024 if (from == realname ||
1025 parseaddr(from = realname,
1026 &e->e_from, RF_COPYALL|RF_SENDERADDR, ' ',
1027 NULL, e, false) == NULL)
1032 expand("\201n", nbuf, sizeof nbuf, e);
1033 from = sm_rpool_strdup_x(e->e_rpool, nbuf);
1034 if (parseaddr(from, &e->e_from, RF_COPYALL, ' ',
1035 NULL, e, false) == NULL &&
1036 parseaddr(from = "postmaster", &e->e_from,
1037 RF_COPYALL, ' ', NULL, e, false) == NULL)
1038 syserr("553 5.3.0 setsender: can't even parse postmaster!");
1043 e->e_from.q_state = QS_SENDER;
1046 sm_dprintf("setsender: QS_SENDER ");
1047 printaddr(&e->e_from, false);
1052 if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags))
1056 p = udbsender(e->e_from.q_user, e->e_rpool);
1062 if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
1068 /* if the user already given fullname don't redefine */
1069 if (FullName == NULL)
1070 FullName = macvalue('x', e);
1071 if (FullName != NULL)
1073 if (FullName[0] == '\0')
1076 FullName = newstr(FullName);
1080 if (e->e_from.q_user[0] != '\0' &&
1081 sm_mbdb_lookup(e->e_from.q_user, &user) == EX_OK)
1084 ** Process passwd file entry.
1087 /* extract home directory */
1088 if (*user.mbdb_homedir == '\0')
1089 e->e_from.q_home = NULL;
1090 else if (strcmp(user.mbdb_homedir, "/") == 0)
1091 e->e_from.q_home = "";
1093 e->e_from.q_home = sm_rpool_strdup_x(e->e_rpool,
1095 macdefine(&e->e_macro, A_PERM, 'z', e->e_from.q_home);
1097 /* extract user and group id */
1098 if (user.mbdb_uid != SM_NO_UID)
1100 e->e_from.q_uid = user.mbdb_uid;
1101 e->e_from.q_gid = user.mbdb_gid;
1102 e->e_from.q_flags |= QGOODUID;
1105 /* extract full name from passwd file */
1106 if (FullName == NULL && !internal &&
1107 user.mbdb_fullname[0] != '\0' &&
1108 strcmp(user.mbdb_name, e->e_from.q_user) == 0)
1110 FullName = newstr(user.mbdb_fullname);
1115 e->e_from.q_home = NULL;
1117 if (FullName != NULL && !internal)
1118 macdefine(&e->e_macro, A_TEMP, 'x', FullName);
1120 else if (!internal && OpMode != MD_DAEMON && OpMode != MD_SMTP)
1122 if (e->e_from.q_home == NULL)
1124 e->e_from.q_home = getenv("HOME");
1125 if (e->e_from.q_home != NULL)
1127 if (*e->e_from.q_home == '\0')
1128 e->e_from.q_home = NULL;
1129 else if (strcmp(e->e_from.q_home, "/") == 0)
1133 e->e_from.q_uid = RealUid;
1134 e->e_from.q_gid = RealGid;
1135 e->e_from.q_flags |= QGOODUID;
1139 ** Rewrite the from person to dispose of possible implicit
1140 ** links in the net.
1143 pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL, NULL);
1146 /* don't need to give error -- prescan did that already */
1148 sm_syslog(LOG_NOTICE, e->e_id,
1149 "cannot prescan from (%s)",
1150 shortenstring(from, MAXSHORTSTR));
1151 finis(true, true, ExitStat);
1153 (void) REWRITE(pvp, 3, e);
1154 (void) REWRITE(pvp, 1, e);
1155 (void) REWRITE(pvp, 4, e);
1156 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
1158 cataddr(pvp, NULL, bp, sizeof buf - 2, '\0');
1159 if (*bp == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags))
1161 /* heuristic: route-addr: add angle brackets */
1162 (void) sm_strlcat(bp, ">", sizeof buf - 1);
1165 e->e_sender = sm_rpool_strdup_x(e->e_rpool, bp);
1166 macdefine(&e->e_macro, A_PERM, 'f', e->e_sender);
1168 /* save the domain spec if this mailer wants it */
1169 if (e->e_from.q_mailer != NULL &&
1170 bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
1174 /* get rid of any pesky angle brackets */
1175 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e s");
1176 (void) REWRITE(pvp, 3, e);
1177 (void) REWRITE(pvp, 1, e);
1178 (void) REWRITE(pvp, 4, e);
1179 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
1181 /* strip off to the last "@" sign */
1182 for (lastat = NULL; *pvp != NULL; pvp++)
1183 if (strcmp(*pvp, "@") == 0)
1187 e->e_fromdomain = copyplist(lastat, true, e->e_rpool);
1190 sm_dprintf("Saving from domain: ");
1191 printav(e->e_fromdomain);
1197 ** PRINTENVFLAGS -- print envelope flags for debugging
1200 ** e -- the envelope with the flags to be printed.
1209 unsigned long ef_bit;
1212 static struct eflags EnvelopeFlags[] =
1214 { "OLDSTYLE", EF_OLDSTYLE },
1215 { "INQUEUE", EF_INQUEUE },
1216 { "NO_BODY_RETN", EF_NO_BODY_RETN },
1217 { "CLRQUEUE", EF_CLRQUEUE },
1218 { "SENDRECEIPT", EF_SENDRECEIPT },
1219 { "FATALERRS", EF_FATALERRS },
1220 { "DELETE_BCC", EF_DELETE_BCC },
1221 { "RESPONSE", EF_RESPONSE },
1222 { "RESENT", EF_RESENT },
1223 { "VRFYONLY", EF_VRFYONLY },
1224 { "WARNING", EF_WARNING },
1225 { "QUEUERUN", EF_QUEUERUN },
1226 { "GLOBALERRS", EF_GLOBALERRS },
1227 { "PM_NOTIFY", EF_PM_NOTIFY },
1228 { "METOO", EF_METOO },
1229 { "LOGSENDER", EF_LOGSENDER },
1230 { "NORECEIPT", EF_NORECEIPT },
1231 { "HAS8BIT", EF_HAS8BIT },
1232 { "NL_NOT_EOL", EF_NL_NOT_EOL },
1233 { "CRLF_NOT_EOL", EF_CRLF_NOT_EOL },
1234 { "RET_PARAM", EF_RET_PARAM },
1235 { "HAS_DF", EF_HAS_DF },
1236 { "IS_MIME", EF_IS_MIME },
1237 { "DONT_MIME", EF_DONT_MIME },
1238 { "DISCARD", EF_DISCARD },
1239 { "TOOBIG", EF_TOOBIG },
1240 { "SPLIT", EF_SPLIT },
1241 { "UNSAFE", EF_UNSAFE },
1247 register ENVELOPE *e;
1249 register struct eflags *ef;
1252 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%lx", e->e_flags);
1253 for (ef = EnvelopeFlags; ef->ef_name != NULL; ef++)
1255 if (!bitset(ef->ef_bit, e->e_flags))
1258 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "<%s",
1261 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, ",%s",
1266 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, ">\n");