2 * Copyright (c) 1998-2003, 2006 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.305 2008/03/31 16:32:13 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;
81 ** This code used to read:
82 ** if (e == parent && e->e_parent != NULL)
83 ** parent = e->e_parent;
84 ** So if e == parent && e->e_parent == NULL then we would
85 ** set e->e_parent = e, which creates a loop in the e_parent chain.
86 ** This meant macvalue() could go into an infinite loop.
90 sendmode = parent->e_sendmode;
96 clearenvelope(e, true, rpool);
98 memmove((char *) &e->e_from,
99 (char *) &NullAddress,
102 memmove((char *) &e->e_from,
103 (char *) &CurEnv->e_from,
105 e->e_parent = parent;
107 e->e_ctime = curtime();
109 e->e_sessid = e->e_id;
110 #endif /* _FFR_SESSID */
113 e->e_msgpriority = parent->e_msgsize;
115 if (parent->e_sessid != NULL)
116 e->e_sessid = sm_rpool_strdup_x(rpool,
118 #endif /* _FFR_SESSID */
120 if (parent->e_quarmsg == NULL)
123 macdefine(&e->e_macro, A_PERM,
124 macid("{quarantine}"), "");
128 e->e_quarmsg = sm_rpool_strdup_x(rpool,
130 macdefine(&e->e_macro, A_PERM,
131 macid("{quarantine}"), e->e_quarmsg);
134 e->e_puthdr = putheader;
135 e->e_putbody = putbody;
136 if (CurEnv->e_xfp != NULL)
137 (void) sm_io_flush(CurEnv->e_xfp, SM_TIME_DEFAULT);
138 if (sendmode != DM_NOTSET)
139 set_delivery_mode(sendmode, e);
144 /* values for msg_timeout, see also IS_* below for usage (bit layout) */
145 #define MSG_T_O 0x01 /* normal timeout */
146 #define MSG_T_O_NOW 0x02 /* NOW timeout */
147 #define MSG_NOT_BY 0x04 /* Deliver-By time exceeded, mode R */
148 #define MSG_WARN 0x10 /* normal queue warning */
149 #define MSG_WARN_BY 0x20 /* Deliver-By time exceeded, mode N */
151 #define IS_MSG_ERR(x) (((x) & 0x0f) != 0) /* return an error */
153 /* immediate return */
154 #define IS_IMM_RET(x) (((x) & (MSG_T_O_NOW|MSG_NOT_BY)) != 0)
155 #define IS_MSG_WARN(x) (((x) & 0xf0) != 0) /* return a warning */
158 ** DROPENVELOPE -- deallocate an envelope.
161 ** e -- the envelope to deallocate.
162 ** fulldrop -- if set, do return receipts.
163 ** split -- if true, split by recipient if message is queued up
169 ** housekeeping necessary to dispose of an envelope.
170 ** Unlocks this queue file.
174 dropenvelope(e, fulldrop, split)
175 register ENVELOPE *e;
180 bool queueit = false;
182 bool failure_return = false;
183 bool delay_return = false;
184 bool success_return = false;
185 bool pmnotify = bitset(EF_PM_NOTIFY, e->e_flags);
194 sm_dprintf("dropenvelope %p: id=", e);
195 xputs(sm_debug_file(), e->e_id);
196 sm_dprintf(", flags=");
200 sm_dprintf("sendq=");
201 printaddr(sm_debug_file(), e->e_sendqueue, true);
206 sm_syslog(LOG_DEBUG, id,
207 "dropenvelope, e_flags=0x%lx, OpMode=%c, pid=%d",
208 e->e_flags, OpMode, (int) CurrentPid);
210 /* we must have an id to remove disk files */
214 /* if verify-only mode, we can skip most of this */
215 if (OpMode == MD_VERIFY)
218 if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
220 e->e_flags &= ~EF_LOGSENDER;
222 /* post statistics */
226 ** Extract state information from dregs of send list.
230 if (now >= e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass])
231 msg_timeout = MSG_T_O;
232 if (IS_DLVR_RETURN(e) && e->e_deliver_by > 0 &&
233 now >= e->e_ctime + e->e_deliver_by &&
234 !bitset(EF_RESPONSE, e->e_flags))
236 msg_timeout = MSG_NOT_BY;
237 e->e_flags |= EF_FATALERRS|EF_CLRQUEUE;
239 else if (TimeOuts.to_q_return[e->e_timeoutclass] == NOW &&
240 !bitset(EF_RESPONSE, e->e_flags))
242 msg_timeout = MSG_T_O_NOW;
243 e->e_flags |= EF_FATALERRS|EF_CLRQUEUE;
246 e->e_flags &= ~EF_QUEUERUN;
247 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
249 if (QS_IS_UNDELIVERED(q->q_state))
252 /* see if a notification is needed */
253 if (bitset(QPINGONFAILURE, q->q_flags) &&
254 ((IS_MSG_ERR(msg_timeout) &&
255 QS_IS_UNDELIVERED(q->q_state)) ||
256 QS_IS_BADADDR(q->q_state) ||
257 IS_IMM_RET(msg_timeout)))
259 failure_return = true;
260 if (!done && q->q_owner == NULL &&
261 !emptyaddr(&e->e_from))
263 (void) sendtolist(e->e_from.q_paddr, NULLADDR,
264 &e->e_errorqueue, 0, e);
268 else if ((bitset(QPINGONSUCCESS, q->q_flags) &&
269 ((QS_IS_SENT(q->q_state) &&
270 bitnset(M_LOCALMAILER, q->q_mailer->m_flags)) ||
271 bitset(QRELAYED|QEXPANDED|QDELIVERED, q->q_flags))) ||
272 bitset(QBYTRACE, q->q_flags) ||
273 bitset(QBYNRELAY, q->q_flags))
275 success_return = true;
280 e->e_flags |= EF_NO_BODY_RETN;
283 ** See if the message timed out.
288 /* nothing to do */ ;
289 else if (IS_MSG_ERR(msg_timeout))
293 if (msg_timeout == MSG_NOT_BY)
295 (void) sm_snprintf(buf, sizeof(buf),
296 "delivery time expired %lds",
301 (void) sm_snprintf(buf, sizeof(buf),
302 "Cannot send message for %s",
303 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
307 /* don't free, allocated from e_rpool */
308 e->e_message = sm_rpool_strdup_x(e->e_rpool, buf);
310 e->e_flags |= EF_CLRQUEUE;
312 if (msg_timeout == MSG_NOT_BY)
314 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
315 "Delivery time (%lds) expired\n",
319 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
320 "Message could not be delivered for %s\n",
321 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
323 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
324 "Message will be deleted from queue\n");
325 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
327 if (QS_IS_UNDELIVERED(q->q_state))
329 q->q_state = QS_BADADDR;
330 if (msg_timeout == MSG_NOT_BY)
331 q->q_status = "5.4.7";
333 q->q_status = "4.4.7";
339 if (TimeOuts.to_q_warning[e->e_timeoutclass] > 0 &&
341 TimeOuts.to_q_warning[e->e_timeoutclass])
342 msg_timeout = MSG_WARN;
343 else if (IS_DLVR_NOTIFY(e) &&
344 e->e_deliver_by > 0 &&
345 now >= e->e_ctime + e->e_deliver_by)
346 msg_timeout = MSG_WARN_BY;
348 if (IS_MSG_WARN(msg_timeout))
350 if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
352 e->e_from.q_paddr != NULL &&
353 strcmp(e->e_from.q_paddr, "<>") != 0 &&
354 sm_strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 &&
355 (strlen(e->e_from.q_paddr) <= 8 ||
356 sm_strcasecmp(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8],
359 for (q = e->e_sendqueue; q != NULL;
362 if (QS_IS_UNDELIVERED(q->q_state)
363 #if _FFR_NODELAYDSN_ON_HOLD
365 q->q_mailer->m_flags)
366 #endif /* _FFR_NODELAYDSN_ON_HOLD */
371 (bitset(QPINGONDELAY,
377 q->q_flags |= QBYNDELAY;
380 if (bitset(QPINGONDELAY,
383 q->q_flags |= QDELAYED;
391 if (msg_timeout == MSG_WARN_BY)
393 (void) sm_snprintf(buf, sizeof(buf),
394 "Warning: Delivery time (%lds) exceeded",
398 (void) sm_snprintf(buf, sizeof(buf),
399 "Warning: could not send message for past %s",
400 pintvl(TimeOuts.to_q_warning[e->e_timeoutclass],
403 /* don't free, allocated from e_rpool */
404 e->e_message = sm_rpool_strdup_x(e->e_rpool,
407 e->e_flags |= EF_WARNING;
409 if (msg_timeout == MSG_WARN_BY)
411 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
412 "Warning: Delivery time (%lds) exceeded\n",
416 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
417 "Warning: message still undelivered after %s\n",
418 pintvl(TimeOuts.to_q_warning[e->e_timeoutclass],
420 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
421 "Will keep trying until message is %s old\n",
422 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
428 sm_dprintf("failure_return=%d delay_return=%d success_return=%d queueit=%d\n",
429 failure_return, delay_return, success_return, queueit);
432 ** If we had some fatal error, but no addresses are marked as
433 ** bad, mark them _all_ as bad.
436 if (bitset(EF_FATALERRS, e->e_flags) && !failure_return)
438 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
440 if ((QS_IS_OK(q->q_state) ||
441 QS_IS_VERIFIED(q->q_state)) &&
442 bitset(QPINGONFAILURE, q->q_flags))
444 failure_return = true;
445 q->q_state = QS_BADADDR;
451 ** Send back return receipts as requested.
454 if (success_return && !failure_return && !delay_return && fulldrop &&
455 !bitset(PRIV_NORECEIPTS, PrivacyFlags) &&
456 strcmp(e->e_from.q_paddr, "<>") != 0)
458 auto ADDRESS *rlist = NULL;
461 sm_dprintf("dropenvelope(%s): sending return receipt\n",
463 e->e_flags |= EF_SENDRECEIPT;
464 (void) sendtolist(e->e_from.q_paddr, NULLADDR, &rlist, 0, e);
465 (void) returntosender("Return receipt", rlist, RTSF_NO_BODY, e);
467 e->e_flags &= ~EF_SENDRECEIPT;
470 ** Arrange to send error messages if there are fatal errors.
473 if ((failure_return || delay_return) && e->e_errormode != EM_QUIET)
476 sm_dprintf("dropenvelope(%s): saving mail\n", id);
477 panic = savemail(e, !bitset(EF_NO_BODY_RETN, e->e_flags));
481 ** Arrange to send warning messages to postmaster as requested.
484 if ((failure_return || pmnotify) &&
485 PostMasterCopy != NULL &&
486 !bitset(EF_RESPONSE, e->e_flags) &&
489 auto ADDRESS *rlist = NULL;
494 expand(PostMasterCopy, pcopy, sizeof(pcopy), e);
497 sm_dprintf("dropenvelope(%s): sending postmaster copy to %s\n",
499 (void) sendtolist(pcopy, NULLADDR, &rlist, 0, e);
502 (void) sendtolist("postmaster", NULLADDR,
504 (void) returntosender(e->e_message, rlist,
505 RTSF_PM_BOUNCE|RTSF_NO_BODY, e);
509 ** Instantiate or deinstantiate the queue.
514 sm_dprintf("dropenvelope(%s): at simpledrop, queueit=%d\n",
516 if (!queueit || bitset(EF_CLRQUEUE, e->e_flags))
520 sm_dprintf("\n===== Dropping queue files for %s... queueit=%d, e_flags=",
526 if (e->e_dfp != NULL)
528 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
531 (void) xunlink(queuename(e, DATAFL_LETTER));
533 if (panic && QueueMode == QM_LOST)
536 ** leave the Qf file behind as
537 ** the delivery attempt failed.
543 if (xunlink(queuename(e, ANYQFL_LETTER)) == 0)
545 /* add to available space in filesystem */
546 updfs(e, -1, panic ? 0 : -1, "dropenvelope");
549 if (e->e_ntries > 0 && LogLevel > 9)
550 sm_syslog(LOG_INFO, id, "done; delay=%s, ntries=%d",
551 pintvl(curtime() - e->e_ctime, true),
554 else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
557 queueup(e, false, true);
564 ** Save old sibling and set it to NULL to avoid
565 ** queueing up the same envelopes again.
566 ** This requires that envelopes in that list have
567 ** been take care of before (or at some other place).
570 oldsib = e->e_sibling;
572 if (!split_by_recipient(e) &&
573 bitset(EF_FATALERRS, e->e_flags))
575 syserr("!dropenvelope(%s): cannot commit data file %s, uid=%d",
576 e->e_id, queuename(e, DATAFL_LETTER),
579 for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling)
580 queueup(ee, false, true);
581 queueup(e, false, true);
584 for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling)
586 /* now unlock the job */
588 sm_dprintf("dropenvelope(%s): unlocking job\n",
593 /* this envelope is marked unused */
594 if (ee->e_dfp != NULL)
596 (void) sm_io_close(ee->e_dfp,
601 ee->e_flags &= ~EF_HAS_DF;
603 e->e_sibling = oldsib;
607 /* now unlock the job */
609 sm_dprintf("dropenvelope(%s): unlocking job\n", id);
613 /* make sure that this envelope is marked unused */
614 if (e->e_dfp != NULL)
616 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
620 e->e_flags &= ~EF_HAS_DF;
623 ** CLEARENVELOPE -- clear an envelope without unlocking
625 ** This is normally used by a child process to get a clean
626 ** envelope without disturbing the parent.
629 ** e -- the envelope to clear.
630 ** fullclear - if set, the current envelope is total
631 ** garbage and should be ignored; otherwise,
632 ** release any resources it may indicate.
633 ** rpool -- either NULL, or a pointer to a resource pool
634 ** from which envelope memory is allocated, and
635 ** to which envelope resources are attached.
641 ** Closes files associated with the envelope.
642 ** Marks the envelope as unallocated.
646 clearenvelope(e, fullclear, rpool)
647 register ENVELOPE *e;
653 extern ENVELOPE BlankEnvelope;
658 /* clear out any file information */
659 if (e->e_xfp != NULL)
660 (void) sm_io_close(e->e_xfp, SM_TIME_DEFAULT);
661 if (e->e_dfp != NULL)
662 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
663 e->e_xfp = e->e_dfp = NULL;
667 ** Copy BlankEnvelope into *e.
668 ** It is not safe to simply copy pointers to strings;
669 ** the strings themselves must be copied (or set to NULL).
670 ** The problem is that when we assign a new string value to
671 ** a member of BlankEnvelope, we free the old string.
672 ** We did not need to do this copying in sendmail 8.11 :-(
673 ** and it is a potential performance hit. Reference counted
674 ** strings are one way out.
679 e->e_qfletter = '\0';
681 macdefine(&e->e_macro, A_PERM, macid("{quarantine}"), "");
684 ** Copy the macro table.
685 ** We might be able to avoid this by zeroing the macro table
686 ** and always searching BlankEnvelope.e_macro after e->e_macro
690 for (p = &e->e_macro.mac_table[0];
691 p <= &e->e_macro.mac_table[MAXMACROID];
695 *p = sm_rpool_strdup_x(rpool, *p);
699 ** XXX There are many strings in the envelope structure
700 ** XXX that we are not attempting to copy here.
701 ** XXX Investigate this further.
705 e->e_macro.mac_rpool = rpool;
707 set_delivery_mode(SM_DELIVER, e);
708 bh = BlankEnvelope.e_header;
712 *nhp = (HDR *) sm_rpool_malloc_x(rpool, sizeof(*bh));
713 memmove((char *) *nhp, (char *) bh, sizeof(*bh));
715 nhp = &(*nhp)->h_link;
719 ** INITSYS -- initialize instantiation of system
721 ** In Daemon mode, this is done in the child.
724 ** e -- the envelope to use.
730 ** Initializes the system macros, some global variables,
731 ** etc. In particular, the current time in various
737 register ENVELOPE *e;
741 static char ybuf[60]; /* holds tty id */
743 extern char *ttyname();
747 ** Give this envelope a reality.
748 ** I.e., an id, a transcript, and a creation time.
749 ** We don't select the queue until all of the recipients are known.
753 e->e_ctime = curtime();
754 e->e_qfletter = '\0';
757 ** Set OutChannel to something useful if stdout isn't it.
758 ** This arranges that any extra stuff the mailer produces
759 ** gets sent back to the user on error (because it is
760 ** tucked away in the transcript).
763 if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) &&
765 OutChannel = e->e_xfp;
768 ** Set up some basic system macros.
772 (void) sm_snprintf(buf, sizeof(buf), "%d", (int) CurrentPid);
773 macdefine(&e->e_macro, A_TEMP, 'p', buf);
776 (void) sm_snprintf(buf, sizeof(buf), "%d", e->e_hopcount);
777 macdefine(&e->e_macro, A_TEMP, 'c', buf);
779 /* time as integer, unix time, arpa time */
787 if (macvalue('y', e) == NULL)
792 if (strrchr(p, '/') != NULL)
793 p = strrchr(p, '/') + 1;
794 (void) sm_strlcpy(ybuf, sizeof(ybuf), p);
795 macdefine(&e->e_macro, A_PERM, 'y', ybuf);
801 ** SETTIME -- set the current time.
804 ** e -- the envelope in which the macros should be set.
810 ** Sets the various time macros -- $a, $b, $d, $t.
815 register ENVELOPE *e;
820 register struct tm *tm;
823 (void) sm_snprintf(buf, sizeof(buf), "%ld", (long) now);
824 macdefine(&e->e_macro, A_TEMP, macid("{time}"), buf);
826 (void) sm_snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d",
827 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
828 tm->tm_hour, tm->tm_min);
829 macdefine(&e->e_macro, A_TEMP, 't', buf);
830 (void) sm_strlcpy(buf, ctime(&now), sizeof(buf));
831 p = strchr(buf, '\n');
834 macdefine(&e->e_macro, A_TEMP, 'd', buf);
835 macdefine(&e->e_macro, A_TEMP, 'b', arpadate(buf));
836 if (macvalue('a', e) == NULL)
837 macdefine(&e->e_macro, A_PERM, 'a', macvalue('b', e));
840 ** OPENXSCRIPT -- Open transcript file
842 ** Creates a transcript file for possible eventual mailing or
846 ** e -- the envelope to create the transcript in/for.
852 ** Creates the transcript file.
857 #endif /* ! O_APPEND */
861 register ENVELOPE *e;
865 if (e->e_xfp != NULL)
869 if (e->e_lockfp == NULL && bitset(EF_INQUEUE, e->e_flags))
870 syserr("openxscript: job not locked");
873 p = queuename(e, XSCRPT_LETTER);
874 e->e_xfp = bfopen(p, FileMode, XscriptFileBufferSize,
875 SFF_NOTEXCL|SFF_OPENASROOT);
877 if (e->e_xfp == NULL)
879 syserr("Can't create transcript file %s", p);
880 e->e_xfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT,
881 SM_PATH_DEVNULL, SM_IO_RDWR, NULL);
882 if (e->e_xfp == NULL)
883 syserr("!Can't open %s", SM_PATH_DEVNULL);
885 (void) sm_io_setvbuf(e->e_xfp, SM_TIME_DEFAULT, NULL, SM_IO_LBF, 0);
888 sm_dprintf("openxscript(%s):\n ", p);
889 dumpfd(sm_io_getinfo(e->e_xfp, SM_IO_WHAT_FD, NULL), true,
894 ** CLOSEXSCRIPT -- close the transcript file.
897 ** e -- the envelope containing the transcript to close.
908 register ENVELOPE *e;
910 if (e->e_xfp == NULL)
913 if (e->e_lockfp == NULL)
914 syserr("closexscript: job not locked");
916 (void) sm_io_close(e->e_xfp, SM_TIME_DEFAULT);
920 ** SETSENDER -- set the person who this message is from
922 ** Under certain circumstances allow the user to say who
923 ** s/he is (using -f or -r). These are:
924 ** 1. The user's uid is zero (root).
925 ** 2. The user's login name is in an approved list (typically
926 ** from a network server).
927 ** 3. The address the user is trying to claim has a
928 ** "!" character in it (since #2 doesn't do it for
929 ** us if we are dialing out for UUCP).
930 ** A better check to replace #3 would be if the
931 ** effective uid is "UUCP" -- this would require me
932 ** to rewrite getpwent to "grab" uucp as it went by,
933 ** make getname more nasty, do another passwd file
934 ** scan, or compile the UID of "UUCP" into the code,
935 ** all of which are reprehensible.
937 ** Assuming all of these fail, we figure out something
941 ** from -- the person we would like to believe this message
942 ** is from, as specified on the command line.
943 ** e -- the envelope in which we would like the sender set.
944 ** delimptr -- if non-NULL, set to the location of the
945 ** trailing delimiter.
946 ** delimchar -- the character that will delimit the sender
948 ** internal -- set if this address is coming from an internal
949 ** source such as an owner alias.
955 ** sets sendmail's notion of who the from person is.
959 setsender(from, e, delimptr, delimchar, internal)
961 register ENVELOPE *e;
967 char *realname = NULL;
969 char buf[MAXNAME + 2];
970 char pvpbuf[PSBUFSIZE];
971 extern char *FullName;
974 sm_dprintf("setsender(%s)\n", from == NULL ? "" : from);
976 /* may be set from earlier calls */
977 macdefine(&e->e_macro, A_PERM, 'x', "");
980 ** Figure out the real user executing us.
981 ** Username can return errno != 0 on non-errors.
984 if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP ||
985 OpMode == MD_ARPAFTP || OpMode == MD_DAEMON)
987 if (realname == NULL || realname[0] == '\0')
988 realname = username();
993 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e s");
995 /* preset state for then clause in case from == NULL */
996 e->e_from.q_state = QS_BADADDR;
997 e->e_from.q_flags = 0;
999 parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR,
1000 delimchar, delimptr, e, false) == NULL ||
1001 QS_IS_BADADDR(e->e_from.q_state) ||
1002 e->e_from.q_mailer == ProgMailer ||
1003 e->e_from.q_mailer == FileMailer ||
1004 e->e_from.q_mailer == InclMailer)
1006 /* log garbage addresses for traceback */
1007 if (from != NULL && LogLevel > 2)
1010 char ebuf[MAXNAME * 2 + 2];
1012 p = macvalue('_', e);
1015 char *host = RealHostName;
1019 (void) sm_snprintf(ebuf, sizeof(ebuf),
1020 "%.*s@%.*s", MAXNAME,
1021 realname, MAXNAME, host);
1024 sm_syslog(LOG_NOTICE, e->e_id,
1025 "setsender: %s: invalid or unparsable, received from %s",
1026 shortenstring(from, 83), p);
1030 if (!QS_IS_BADADDR(e->e_from.q_state))
1032 /* it was a bogus mailer in the from addr */
1033 e->e_status = "5.1.7";
1034 usrerrenh(e->e_status,
1035 "553 Invalid sender address");
1039 if (from == realname ||
1040 parseaddr(from = realname,
1041 &e->e_from, RF_COPYALL|RF_SENDERADDR, ' ',
1042 NULL, e, false) == NULL)
1047 expand("\201n", nbuf, sizeof(nbuf), e);
1048 from = sm_rpool_strdup_x(e->e_rpool, nbuf);
1049 if (parseaddr(from, &e->e_from, RF_COPYALL, ' ',
1050 NULL, e, false) == NULL &&
1051 parseaddr(from = "postmaster", &e->e_from,
1052 RF_COPYALL, ' ', NULL, e, false) == NULL)
1053 syserr("553 5.3.0 setsender: can't even parse postmaster!");
1058 e->e_from.q_state = QS_SENDER;
1061 sm_dprintf("setsender: QS_SENDER ");
1062 printaddr(sm_debug_file(), &e->e_from, false);
1067 if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags))
1071 p = udbsender(e->e_from.q_user, e->e_rpool);
1077 if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
1083 /* if the user already given fullname don't redefine */
1084 if (FullName == NULL)
1085 FullName = macvalue('x', e);
1086 if (FullName != NULL)
1088 if (FullName[0] == '\0')
1091 FullName = newstr(FullName);
1095 if (e->e_from.q_user[0] != '\0' &&
1096 sm_mbdb_lookup(e->e_from.q_user, &user) == EX_OK)
1099 ** Process passwd file entry.
1102 /* extract home directory */
1103 if (*user.mbdb_homedir == '\0')
1104 e->e_from.q_home = NULL;
1105 else if (strcmp(user.mbdb_homedir, "/") == 0)
1106 e->e_from.q_home = "";
1108 e->e_from.q_home = sm_rpool_strdup_x(e->e_rpool,
1110 macdefine(&e->e_macro, A_PERM, 'z', e->e_from.q_home);
1112 /* extract user and group id */
1113 if (user.mbdb_uid != SM_NO_UID)
1115 e->e_from.q_uid = user.mbdb_uid;
1116 e->e_from.q_gid = user.mbdb_gid;
1117 e->e_from.q_flags |= QGOODUID;
1120 /* extract full name from passwd file */
1121 if (FullName == NULL && !internal &&
1122 user.mbdb_fullname[0] != '\0' &&
1123 strcmp(user.mbdb_name, e->e_from.q_user) == 0)
1125 FullName = newstr(user.mbdb_fullname);
1130 e->e_from.q_home = NULL;
1132 if (FullName != NULL && !internal)
1133 macdefine(&e->e_macro, A_TEMP, 'x', FullName);
1135 else if (!internal && OpMode != MD_DAEMON && OpMode != MD_SMTP)
1137 if (e->e_from.q_home == NULL)
1139 e->e_from.q_home = getenv("HOME");
1140 if (e->e_from.q_home != NULL)
1142 if (*e->e_from.q_home == '\0')
1143 e->e_from.q_home = NULL;
1144 else if (strcmp(e->e_from.q_home, "/") == 0)
1148 e->e_from.q_uid = RealUid;
1149 e->e_from.q_gid = RealGid;
1150 e->e_from.q_flags |= QGOODUID;
1154 ** Rewrite the from person to dispose of possible implicit
1155 ** links in the net.
1158 pvp = prescan(from, delimchar, pvpbuf, sizeof(pvpbuf), NULL,
1159 IntTokenTab, false);
1162 /* don't need to give error -- prescan did that already */
1164 sm_syslog(LOG_NOTICE, e->e_id,
1165 "cannot prescan from (%s)",
1166 shortenstring(from, MAXSHORTSTR));
1167 finis(true, true, ExitStat);
1169 (void) REWRITE(pvp, 3, e);
1170 (void) REWRITE(pvp, 1, e);
1171 (void) REWRITE(pvp, 4, e);
1172 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
1174 cataddr(pvp, NULL, bp, sizeof(buf) - 2, '\0', false);
1175 if (*bp == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags))
1177 /* heuristic: route-addr: add angle brackets */
1178 (void) sm_strlcat(bp, ">", sizeof(buf) - 1);
1181 e->e_sender = sm_rpool_strdup_x(e->e_rpool, bp);
1182 macdefine(&e->e_macro, A_PERM, 'f', e->e_sender);
1184 /* save the domain spec if this mailer wants it */
1185 if (e->e_from.q_mailer != NULL &&
1186 bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
1190 /* get rid of any pesky angle brackets */
1191 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e s");
1192 (void) REWRITE(pvp, 3, e);
1193 (void) REWRITE(pvp, 1, e);
1194 (void) REWRITE(pvp, 4, e);
1195 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
1197 /* strip off to the last "@" sign */
1198 for (lastat = NULL; *pvp != NULL; pvp++)
1200 if (strcmp(*pvp, "@") == 0)
1205 e->e_fromdomain = copyplist(lastat, true, e->e_rpool);
1208 sm_dprintf("Saving from domain: ");
1209 printav(sm_debug_file(), e->e_fromdomain);
1215 ** PRINTENVFLAGS -- print envelope flags for debugging
1218 ** e -- the envelope with the flags to be printed.
1227 unsigned long ef_bit;
1230 static struct eflags EnvelopeFlags[] =
1232 { "OLDSTYLE", EF_OLDSTYLE },
1233 { "INQUEUE", EF_INQUEUE },
1234 { "NO_BODY_RETN", EF_NO_BODY_RETN },
1235 { "CLRQUEUE", EF_CLRQUEUE },
1236 { "SENDRECEIPT", EF_SENDRECEIPT },
1237 { "FATALERRS", EF_FATALERRS },
1238 { "DELETE_BCC", EF_DELETE_BCC },
1239 { "RESPONSE", EF_RESPONSE },
1240 { "RESENT", EF_RESENT },
1241 { "VRFYONLY", EF_VRFYONLY },
1242 { "WARNING", EF_WARNING },
1243 { "QUEUERUN", EF_QUEUERUN },
1244 { "GLOBALERRS", EF_GLOBALERRS },
1245 { "PM_NOTIFY", EF_PM_NOTIFY },
1246 { "METOO", EF_METOO },
1247 { "LOGSENDER", EF_LOGSENDER },
1248 { "NORECEIPT", EF_NORECEIPT },
1249 { "HAS8BIT", EF_HAS8BIT },
1250 { "NL_NOT_EOL", EF_NL_NOT_EOL },
1251 { "CRLF_NOT_EOL", EF_CRLF_NOT_EOL },
1252 { "RET_PARAM", EF_RET_PARAM },
1253 { "HAS_DF", EF_HAS_DF },
1254 { "IS_MIME", EF_IS_MIME },
1255 { "DONT_MIME", EF_DONT_MIME },
1256 { "DISCARD", EF_DISCARD },
1257 { "TOOBIG", EF_TOOBIG },
1258 { "SPLIT", EF_SPLIT },
1259 { "UNSAFE", EF_UNSAFE },
1265 register ENVELOPE *e;
1267 register struct eflags *ef;
1270 sm_dprintf("%lx", e->e_flags);
1271 for (ef = EnvelopeFlags; ef->ef_name != NULL; ef++)
1273 if (!bitset(ef->ef_bit, e->e_flags))
1276 sm_dprintf("<%s", ef->ef_name);
1278 sm_dprintf(",%s", ef->ef_name);