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.310 2009/12/18 17:08:01 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
166 ** EX_* status (currently: 0: success, EX_IOERR on panic)
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)
219 sm_dprintf("dropenvelope: e_id=%s, EF_LOGSENDER=%d, LogLevel=%d\n",
220 e->e_id, bitset(EF_LOGSENDER, e->e_flags), LogLevel);
221 if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
223 e->e_flags &= ~EF_LOGSENDER;
225 /* post statistics */
229 ** Extract state information from dregs of send list.
233 if (now >= e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass])
234 msg_timeout = MSG_T_O;
235 if (IS_DLVR_RETURN(e) && e->e_deliver_by > 0 &&
236 now >= e->e_ctime + e->e_deliver_by &&
237 !bitset(EF_RESPONSE, e->e_flags))
239 msg_timeout = MSG_NOT_BY;
240 e->e_flags |= EF_FATALERRS|EF_CLRQUEUE;
242 else if (TimeOuts.to_q_return[e->e_timeoutclass] == NOW &&
243 !bitset(EF_RESPONSE, e->e_flags))
245 msg_timeout = MSG_T_O_NOW;
246 e->e_flags |= EF_FATALERRS|EF_CLRQUEUE;
249 e->e_flags &= ~EF_QUEUERUN;
250 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
252 if (QS_IS_UNDELIVERED(q->q_state))
255 /* see if a notification is needed */
256 if (bitset(QPINGONFAILURE, q->q_flags) &&
257 ((IS_MSG_ERR(msg_timeout) &&
258 QS_IS_UNDELIVERED(q->q_state)) ||
259 QS_IS_BADADDR(q->q_state) ||
260 IS_IMM_RET(msg_timeout)))
262 failure_return = true;
263 if (!done && q->q_owner == NULL &&
264 !emptyaddr(&e->e_from))
266 (void) sendtolist(e->e_from.q_paddr, NULLADDR,
267 &e->e_errorqueue, 0, e);
271 else if ((bitset(QPINGONSUCCESS, q->q_flags) &&
272 ((QS_IS_SENT(q->q_state) &&
273 bitnset(M_LOCALMAILER, q->q_mailer->m_flags)) ||
274 bitset(QRELAYED|QEXPANDED|QDELIVERED, q->q_flags))) ||
275 bitset(QBYTRACE, q->q_flags) ||
276 bitset(QBYNRELAY, q->q_flags))
278 success_return = true;
283 e->e_flags |= EF_NO_BODY_RETN;
286 ** See if the message timed out.
291 /* nothing to do */ ;
292 else if (IS_MSG_ERR(msg_timeout))
296 if (msg_timeout == MSG_NOT_BY)
298 (void) sm_snprintf(buf, sizeof(buf),
299 "delivery time expired %lds",
304 (void) sm_snprintf(buf, sizeof(buf),
305 "Cannot send message for %s",
306 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
310 /* don't free, allocated from e_rpool */
311 e->e_message = sm_rpool_strdup_x(e->e_rpool, buf);
313 e->e_flags |= EF_CLRQUEUE;
315 if (msg_timeout == MSG_NOT_BY)
317 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
318 "Delivery time (%lds) expired\n",
322 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
323 "Message could not be delivered for %s\n",
324 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
326 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
327 "Message will be deleted from queue\n");
328 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
330 if (QS_IS_UNDELIVERED(q->q_state))
332 q->q_state = QS_BADADDR;
333 if (msg_timeout == MSG_NOT_BY)
334 q->q_status = "5.4.7";
336 q->q_status = "4.4.7";
342 if (TimeOuts.to_q_warning[e->e_timeoutclass] > 0 &&
344 TimeOuts.to_q_warning[e->e_timeoutclass])
345 msg_timeout = MSG_WARN;
346 else if (IS_DLVR_NOTIFY(e) &&
347 e->e_deliver_by > 0 &&
348 now >= e->e_ctime + e->e_deliver_by)
349 msg_timeout = MSG_WARN_BY;
351 if (IS_MSG_WARN(msg_timeout))
353 if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
355 e->e_from.q_paddr != NULL &&
356 strcmp(e->e_from.q_paddr, "<>") != 0 &&
357 sm_strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 &&
358 (strlen(e->e_from.q_paddr) <= 8 ||
359 sm_strcasecmp(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8],
362 for (q = e->e_sendqueue; q != NULL;
365 if (QS_IS_UNDELIVERED(q->q_state)
366 #if _FFR_NODELAYDSN_ON_HOLD
368 q->q_mailer->m_flags)
369 #endif /* _FFR_NODELAYDSN_ON_HOLD */
374 (bitset(QPINGONDELAY,
380 q->q_flags |= QBYNDELAY;
383 if (bitset(QPINGONDELAY,
386 q->q_flags |= QDELAYED;
394 if (msg_timeout == MSG_WARN_BY)
396 (void) sm_snprintf(buf, sizeof(buf),
397 "Warning: Delivery time (%lds) exceeded",
401 (void) sm_snprintf(buf, sizeof(buf),
402 "Warning: could not send message for past %s",
403 pintvl(TimeOuts.to_q_warning[e->e_timeoutclass],
406 /* don't free, allocated from e_rpool */
407 e->e_message = sm_rpool_strdup_x(e->e_rpool,
410 e->e_flags |= EF_WARNING;
412 if (msg_timeout == MSG_WARN_BY)
414 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
415 "Warning: Delivery time (%lds) exceeded\n",
419 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
420 "Warning: message still undelivered after %s\n",
421 pintvl(TimeOuts.to_q_warning[e->e_timeoutclass],
423 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
424 "Will keep trying until message is %s old\n",
425 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
431 sm_dprintf("failure_return=%d delay_return=%d success_return=%d queueit=%d\n",
432 failure_return, delay_return, success_return, queueit);
435 ** If we had some fatal error, but no addresses are marked as
436 ** bad, mark them _all_ as bad.
439 if (bitset(EF_FATALERRS, e->e_flags) && !failure_return)
441 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
443 if ((QS_IS_OK(q->q_state) ||
444 QS_IS_VERIFIED(q->q_state)) &&
445 bitset(QPINGONFAILURE, q->q_flags))
447 failure_return = true;
448 q->q_state = QS_BADADDR;
454 ** Send back return receipts as requested.
457 if (success_return && !failure_return && !delay_return && fulldrop &&
458 !bitset(PRIV_NORECEIPTS, PrivacyFlags) &&
459 strcmp(e->e_from.q_paddr, "<>") != 0)
461 auto ADDRESS *rlist = NULL;
464 sm_dprintf("dropenvelope(%s): sending return receipt\n",
466 e->e_flags |= EF_SENDRECEIPT;
467 (void) sendtolist(e->e_from.q_paddr, NULLADDR, &rlist, 0, e);
468 (void) returntosender("Return receipt", rlist, RTSF_NO_BODY, e);
470 e->e_flags &= ~EF_SENDRECEIPT;
473 ** Arrange to send error messages if there are fatal errors.
476 if ((failure_return || delay_return) && e->e_errormode != EM_QUIET)
479 sm_dprintf("dropenvelope(%s): saving mail\n", id);
480 panic = savemail(e, !bitset(EF_NO_BODY_RETN, e->e_flags));
484 ** Arrange to send warning messages to postmaster as requested.
487 if ((failure_return || pmnotify) &&
488 PostMasterCopy != NULL &&
489 !bitset(EF_RESPONSE, e->e_flags) &&
492 auto ADDRESS *rlist = NULL;
497 expand(PostMasterCopy, pcopy, sizeof(pcopy), e);
500 sm_dprintf("dropenvelope(%s): sending postmaster copy to %s\n",
502 (void) sendtolist(pcopy, NULLADDR, &rlist, 0, e);
505 (void) sendtolist("postmaster", NULLADDR,
507 (void) returntosender(e->e_message, rlist,
508 RTSF_PM_BOUNCE|RTSF_NO_BODY, e);
512 ** Instantiate or deinstantiate the queue.
517 sm_dprintf("dropenvelope(%s): at simpledrop, queueit=%d\n",
519 if (!queueit || bitset(EF_CLRQUEUE, e->e_flags))
523 sm_dprintf("\n===== Dropping queue files for %s... queueit=%d, e_flags=",
529 if (e->e_dfp != NULL)
531 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
534 (void) xunlink(queuename(e, DATAFL_LETTER));
536 if (panic && QueueMode == QM_LOST)
539 ** leave the Qf file behind as
540 ** the delivery attempt failed.
546 if (xunlink(queuename(e, ANYQFL_LETTER)) == 0)
548 /* add to available space in filesystem */
549 updfs(e, -1, panic ? 0 : -1, "dropenvelope");
552 if (e->e_ntries > 0 && LogLevel > 9)
553 sm_syslog(LOG_INFO, id, "done; delay=%s, ntries=%d",
554 pintvl(curtime() - e->e_ctime, true),
557 else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
560 queueup(e, false, true);
567 ** Save old sibling and set it to NULL to avoid
568 ** queueing up the same envelopes again.
569 ** This requires that envelopes in that list have
570 ** been take care of before (or at some other place).
573 oldsib = e->e_sibling;
575 if (!split_by_recipient(e) &&
576 bitset(EF_FATALERRS, e->e_flags))
578 syserr("!dropenvelope(%s): cannot commit data file %s, uid=%d",
579 e->e_id, queuename(e, DATAFL_LETTER),
582 for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling)
583 queueup(ee, false, true);
584 queueup(e, false, true);
587 for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling)
589 /* now unlock the job */
591 sm_dprintf("dropenvelope(%s): unlocking job\n",
596 /* this envelope is marked unused */
597 if (ee->e_dfp != NULL)
599 (void) sm_io_close(ee->e_dfp,
604 ee->e_flags &= ~EF_HAS_DF;
606 e->e_sibling = oldsib;
610 /* now unlock the job */
612 sm_dprintf("dropenvelope(%s): unlocking job\n", id);
616 /* make sure that this envelope is marked unused */
617 if (e->e_dfp != NULL)
619 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
623 e->e_flags &= ~EF_HAS_DF;
630 ** CLEARENVELOPE -- clear an envelope without unlocking
632 ** This is normally used by a child process to get a clean
633 ** envelope without disturbing the parent.
636 ** e -- the envelope to clear.
637 ** fullclear - if set, the current envelope is total
638 ** garbage and should be ignored; otherwise,
639 ** release any resources it may indicate.
640 ** rpool -- either NULL, or a pointer to a resource pool
641 ** from which envelope memory is allocated, and
642 ** to which envelope resources are attached.
648 ** Closes files associated with the envelope.
649 ** Marks the envelope as unallocated.
653 clearenvelope(e, fullclear, rpool)
654 register ENVELOPE *e;
660 extern ENVELOPE BlankEnvelope;
665 /* clear out any file information */
666 if (e->e_xfp != NULL)
667 (void) sm_io_close(e->e_xfp, SM_TIME_DEFAULT);
668 if (e->e_dfp != NULL)
669 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
670 e->e_xfp = e->e_dfp = NULL;
674 ** Copy BlankEnvelope into *e.
675 ** It is not safe to simply copy pointers to strings;
676 ** the strings themselves must be copied (or set to NULL).
677 ** The problem is that when we assign a new string value to
678 ** a member of BlankEnvelope, we free the old string.
679 ** We did not need to do this copying in sendmail 8.11 :-(
680 ** and it is a potential performance hit. Reference counted
681 ** strings are one way out.
686 e->e_qfletter = '\0';
688 macdefine(&e->e_macro, A_PERM, macid("{quarantine}"), "");
691 ** Copy the macro table.
692 ** We might be able to avoid this by zeroing the macro table
693 ** and always searching BlankEnvelope.e_macro after e->e_macro
697 for (p = &e->e_macro.mac_table[0];
698 p <= &e->e_macro.mac_table[MAXMACROID];
702 *p = sm_rpool_strdup_x(rpool, *p);
706 ** XXX There are many strings in the envelope structure
707 ** XXX that we are not attempting to copy here.
708 ** XXX Investigate this further.
712 e->e_macro.mac_rpool = rpool;
714 set_delivery_mode(SM_DELIVER, e);
715 bh = BlankEnvelope.e_header;
719 *nhp = (HDR *) sm_rpool_malloc_x(rpool, sizeof(*bh));
720 memmove((char *) *nhp, (char *) bh, sizeof(*bh));
722 nhp = &(*nhp)->h_link;
724 #if _FFR_MILTER_ENHSC
725 e->e_enhsc[0] = '\0';
726 #endif /* _FFR_MILTER_ENHSC */
729 ** INITSYS -- initialize instantiation of system
731 ** In Daemon mode, this is done in the child.
734 ** e -- the envelope to use.
740 ** Initializes the system macros, some global variables,
741 ** etc. In particular, the current time in various
747 register ENVELOPE *e;
751 static char ybuf[60]; /* holds tty id */
753 extern char *ttyname();
757 ** Give this envelope a reality.
758 ** I.e., an id, a transcript, and a creation time.
759 ** We don't select the queue until all of the recipients are known.
763 e->e_ctime = curtime();
764 e->e_qfletter = '\0';
767 ** Set OutChannel to something useful if stdout isn't it.
768 ** This arranges that any extra stuff the mailer produces
769 ** gets sent back to the user on error (because it is
770 ** tucked away in the transcript).
773 if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) &&
775 OutChannel = e->e_xfp;
778 ** Set up some basic system macros.
782 (void) sm_snprintf(buf, sizeof(buf), "%d", (int) CurrentPid);
783 macdefine(&e->e_macro, A_TEMP, 'p', buf);
786 (void) sm_snprintf(buf, sizeof(buf), "%d", e->e_hopcount);
787 macdefine(&e->e_macro, A_TEMP, 'c', buf);
789 /* time as integer, unix time, arpa time */
797 if (macvalue('y', e) == NULL)
802 if (strrchr(p, '/') != NULL)
803 p = strrchr(p, '/') + 1;
804 (void) sm_strlcpy(ybuf, sizeof(ybuf), p);
805 macdefine(&e->e_macro, A_PERM, 'y', ybuf);
811 ** SETTIME -- set the current time.
814 ** e -- the envelope in which the macros should be set.
820 ** Sets the various time macros -- $a, $b, $d, $t.
825 register ENVELOPE *e;
830 register struct tm *tm;
833 (void) sm_snprintf(buf, sizeof(buf), "%ld", (long) now);
834 macdefine(&e->e_macro, A_TEMP, macid("{time}"), buf);
836 (void) sm_snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d",
837 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
838 tm->tm_hour, tm->tm_min);
839 macdefine(&e->e_macro, A_TEMP, 't', buf);
840 (void) sm_strlcpy(buf, ctime(&now), sizeof(buf));
841 p = strchr(buf, '\n');
844 macdefine(&e->e_macro, A_TEMP, 'd', buf);
845 macdefine(&e->e_macro, A_TEMP, 'b', arpadate(buf));
846 if (macvalue('a', e) == NULL)
847 macdefine(&e->e_macro, A_PERM, 'a', macvalue('b', e));
850 ** OPENXSCRIPT -- Open transcript file
852 ** Creates a transcript file for possible eventual mailing or
856 ** e -- the envelope to create the transcript in/for.
862 ** Creates the transcript file.
867 #endif /* ! O_APPEND */
871 register ENVELOPE *e;
875 if (e->e_xfp != NULL)
879 if (e->e_lockfp == NULL && bitset(EF_INQUEUE, e->e_flags))
880 syserr("openxscript: job not locked");
883 p = queuename(e, XSCRPT_LETTER);
884 e->e_xfp = bfopen(p, FileMode, XscriptFileBufferSize,
885 SFF_NOTEXCL|SFF_OPENASROOT);
887 if (e->e_xfp == NULL)
889 syserr("Can't create transcript file %s", p);
890 e->e_xfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT,
891 SM_PATH_DEVNULL, SM_IO_RDWR, NULL);
892 if (e->e_xfp == NULL)
893 syserr("!Can't open %s", SM_PATH_DEVNULL);
895 (void) sm_io_setvbuf(e->e_xfp, SM_TIME_DEFAULT, NULL, SM_IO_LBF, 0);
898 sm_dprintf("openxscript(%s):\n ", p);
899 dumpfd(sm_io_getinfo(e->e_xfp, SM_IO_WHAT_FD, NULL), true,
904 ** CLOSEXSCRIPT -- close the transcript file.
907 ** e -- the envelope containing the transcript to close.
918 register ENVELOPE *e;
920 if (e->e_xfp == NULL)
923 if (e->e_lockfp == NULL)
924 syserr("closexscript: job not locked");
926 (void) sm_io_close(e->e_xfp, SM_TIME_DEFAULT);
930 ** SETSENDER -- set the person who this message is from
932 ** Under certain circumstances allow the user to say who
933 ** s/he is (using -f or -r). These are:
934 ** 1. The user's uid is zero (root).
935 ** 2. The user's login name is in an approved list (typically
936 ** from a network server).
937 ** 3. The address the user is trying to claim has a
938 ** "!" character in it (since #2 doesn't do it for
939 ** us if we are dialing out for UUCP).
940 ** A better check to replace #3 would be if the
941 ** effective uid is "UUCP" -- this would require me
942 ** to rewrite getpwent to "grab" uucp as it went by,
943 ** make getname more nasty, do another passwd file
944 ** scan, or compile the UID of "UUCP" into the code,
945 ** all of which are reprehensible.
947 ** Assuming all of these fail, we figure out something
951 ** from -- the person we would like to believe this message
952 ** is from, as specified on the command line.
953 ** e -- the envelope in which we would like the sender set.
954 ** delimptr -- if non-NULL, set to the location of the
955 ** trailing delimiter.
956 ** delimchar -- the character that will delimit the sender
958 ** internal -- set if this address is coming from an internal
959 ** source such as an owner alias.
965 ** sets sendmail's notion of who the from person is.
969 setsender(from, e, delimptr, delimchar, internal)
971 register ENVELOPE *e;
977 char *realname = NULL;
979 char buf[MAXNAME + 2];
980 char pvpbuf[PSBUFSIZE];
981 extern char *FullName;
984 sm_dprintf("setsender(%s)\n", from == NULL ? "" : from);
986 /* may be set from earlier calls */
987 macdefine(&e->e_macro, A_PERM, 'x', "");
990 ** Figure out the real user executing us.
991 ** Username can return errno != 0 on non-errors.
994 if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP ||
995 OpMode == MD_ARPAFTP || OpMode == MD_DAEMON)
997 if (realname == NULL || realname[0] == '\0')
998 realname = username();
1000 if (ConfigLevel < 2)
1003 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e s");
1005 /* preset state for then clause in case from == NULL */
1006 e->e_from.q_state = QS_BADADDR;
1007 e->e_from.q_flags = 0;
1009 parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR,
1010 delimchar, delimptr, e, false) == NULL ||
1011 QS_IS_BADADDR(e->e_from.q_state) ||
1012 e->e_from.q_mailer == ProgMailer ||
1013 e->e_from.q_mailer == FileMailer ||
1014 e->e_from.q_mailer == InclMailer)
1016 /* log garbage addresses for traceback */
1017 if (from != NULL && LogLevel > 2)
1020 char ebuf[MAXNAME * 2 + 2];
1022 p = macvalue('_', e);
1025 char *host = RealHostName;
1029 (void) sm_snprintf(ebuf, sizeof(ebuf),
1030 "%.*s@%.*s", MAXNAME,
1031 realname, MAXNAME, host);
1034 sm_syslog(LOG_NOTICE, e->e_id,
1035 "setsender: %s: invalid or unparsable, received from %s",
1036 shortenstring(from, 83), p);
1040 if (!QS_IS_BADADDR(e->e_from.q_state))
1042 /* it was a bogus mailer in the from addr */
1043 e->e_status = "5.1.7";
1044 usrerrenh(e->e_status,
1045 "553 Invalid sender address");
1049 if (from == realname ||
1050 parseaddr(from = realname,
1051 &e->e_from, RF_COPYALL|RF_SENDERADDR, ' ',
1052 NULL, e, false) == NULL)
1057 expand("\201n", nbuf, sizeof(nbuf), e);
1058 from = sm_rpool_strdup_x(e->e_rpool, nbuf);
1059 if (parseaddr(from, &e->e_from, RF_COPYALL, ' ',
1060 NULL, e, false) == NULL &&
1061 parseaddr(from = "postmaster", &e->e_from,
1062 RF_COPYALL, ' ', NULL, e, false) == NULL)
1063 syserr("553 5.3.0 setsender: can't even parse postmaster!");
1068 e->e_from.q_state = QS_SENDER;
1071 sm_dprintf("setsender: QS_SENDER ");
1072 printaddr(sm_debug_file(), &e->e_from, false);
1077 if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags))
1081 p = udbsender(e->e_from.q_user, e->e_rpool);
1087 if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
1093 /* if the user already given fullname don't redefine */
1094 if (FullName == NULL)
1095 FullName = macvalue('x', e);
1096 if (FullName != NULL)
1098 if (FullName[0] == '\0')
1101 FullName = newstr(FullName);
1105 if (e->e_from.q_user[0] != '\0' &&
1106 sm_mbdb_lookup(e->e_from.q_user, &user) == EX_OK)
1109 ** Process passwd file entry.
1112 /* extract home directory */
1113 if (*user.mbdb_homedir == '\0')
1114 e->e_from.q_home = NULL;
1115 else if (strcmp(user.mbdb_homedir, "/") == 0)
1116 e->e_from.q_home = "";
1118 e->e_from.q_home = sm_rpool_strdup_x(e->e_rpool,
1120 macdefine(&e->e_macro, A_PERM, 'z', e->e_from.q_home);
1122 /* extract user and group id */
1123 if (user.mbdb_uid != SM_NO_UID)
1125 e->e_from.q_uid = user.mbdb_uid;
1126 e->e_from.q_gid = user.mbdb_gid;
1127 e->e_from.q_flags |= QGOODUID;
1130 /* extract full name from passwd file */
1131 if (FullName == NULL && !internal &&
1132 user.mbdb_fullname[0] != '\0' &&
1133 strcmp(user.mbdb_name, e->e_from.q_user) == 0)
1135 FullName = newstr(user.mbdb_fullname);
1140 e->e_from.q_home = NULL;
1142 if (FullName != NULL && !internal)
1143 macdefine(&e->e_macro, A_TEMP, 'x', FullName);
1145 else if (!internal && OpMode != MD_DAEMON && OpMode != MD_SMTP)
1147 if (e->e_from.q_home == NULL)
1149 e->e_from.q_home = getenv("HOME");
1150 if (e->e_from.q_home != NULL)
1152 if (*e->e_from.q_home == '\0')
1153 e->e_from.q_home = NULL;
1154 else if (strcmp(e->e_from.q_home, "/") == 0)
1158 e->e_from.q_uid = RealUid;
1159 e->e_from.q_gid = RealGid;
1160 e->e_from.q_flags |= QGOODUID;
1164 ** Rewrite the from person to dispose of possible implicit
1165 ** links in the net.
1168 pvp = prescan(from, delimchar, pvpbuf, sizeof(pvpbuf), NULL,
1169 IntTokenTab, false);
1172 /* don't need to give error -- prescan did that already */
1174 sm_syslog(LOG_NOTICE, e->e_id,
1175 "cannot prescan from (%s)",
1176 shortenstring(from, MAXSHORTSTR));
1177 finis(true, true, ExitStat);
1179 (void) REWRITE(pvp, 3, e);
1180 (void) REWRITE(pvp, 1, e);
1181 (void) REWRITE(pvp, 4, e);
1182 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
1184 cataddr(pvp, NULL, bp, sizeof(buf) - 2, '\0', false);
1185 if (*bp == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags))
1187 /* heuristic: route-addr: add angle brackets */
1188 (void) sm_strlcat(bp, ">", sizeof(buf) - 1);
1191 e->e_sender = sm_rpool_strdup_x(e->e_rpool, bp);
1192 macdefine(&e->e_macro, A_PERM, 'f', e->e_sender);
1194 /* save the domain spec if this mailer wants it */
1195 if (e->e_from.q_mailer != NULL &&
1196 bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
1200 /* get rid of any pesky angle brackets */
1201 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e s");
1202 (void) REWRITE(pvp, 3, e);
1203 (void) REWRITE(pvp, 1, e);
1204 (void) REWRITE(pvp, 4, e);
1205 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
1207 /* strip off to the last "@" sign */
1208 for (lastat = NULL; *pvp != NULL; pvp++)
1210 if (strcmp(*pvp, "@") == 0)
1215 e->e_fromdomain = copyplist(lastat, true, e->e_rpool);
1218 sm_dprintf("Saving from domain: ");
1219 printav(sm_debug_file(), e->e_fromdomain);
1225 ** PRINTENVFLAGS -- print envelope flags for debugging
1228 ** e -- the envelope with the flags to be printed.
1237 unsigned long ef_bit;
1240 static struct eflags EnvelopeFlags[] =
1242 { "OLDSTYLE", EF_OLDSTYLE },
1243 { "INQUEUE", EF_INQUEUE },
1244 { "NO_BODY_RETN", EF_NO_BODY_RETN },
1245 { "CLRQUEUE", EF_CLRQUEUE },
1246 { "SENDRECEIPT", EF_SENDRECEIPT },
1247 { "FATALERRS", EF_FATALERRS },
1248 { "DELETE_BCC", EF_DELETE_BCC },
1249 { "RESPONSE", EF_RESPONSE },
1250 { "RESENT", EF_RESENT },
1251 { "VRFYONLY", EF_VRFYONLY },
1252 { "WARNING", EF_WARNING },
1253 { "QUEUERUN", EF_QUEUERUN },
1254 { "GLOBALERRS", EF_GLOBALERRS },
1255 { "PM_NOTIFY", EF_PM_NOTIFY },
1256 { "METOO", EF_METOO },
1257 { "LOGSENDER", EF_LOGSENDER },
1258 { "NORECEIPT", EF_NORECEIPT },
1259 { "HAS8BIT", EF_HAS8BIT },
1260 { "NL_NOT_EOL", EF_NL_NOT_EOL },
1261 { "CRLF_NOT_EOL", EF_CRLF_NOT_EOL },
1262 { "RET_PARAM", EF_RET_PARAM },
1263 { "HAS_DF", EF_HAS_DF },
1264 { "IS_MIME", EF_IS_MIME },
1265 { "DONT_MIME", EF_DONT_MIME },
1266 { "DISCARD", EF_DISCARD },
1267 { "TOOBIG", EF_TOOBIG },
1268 { "SPLIT", EF_SPLIT },
1269 { "UNSAFE", EF_UNSAFE },
1275 register ENVELOPE *e;
1277 register struct eflags *ef;
1280 sm_dprintf("%lx", e->e_flags);
1281 for (ef = EnvelopeFlags; ef->ef_name != NULL; ef++)
1283 if (!bitset(ef->ef_bit, e->e_flags))
1286 sm_dprintf("<%s", ef->ef_name);
1288 sm_dprintf(",%s", ef->ef_name);