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.312 2010/02/03 16:36:40 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;
250 e->e_flags &= ~EF_QUEUERUN;
251 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
253 if (QS_IS_UNDELIVERED(q->q_state))
257 /* see if a notification is needed */
258 if (bitset(QPINGONFAILURE, q->q_flags) &&
259 ((IS_MSG_ERR(msg_timeout) &&
260 QS_IS_UNDELIVERED(q->q_state)) ||
261 QS_IS_BADADDR(q->q_state) ||
262 IS_IMM_RET(msg_timeout)))
264 failure_return = true;
265 if (!done && q->q_owner == NULL &&
266 !emptyaddr(&e->e_from))
268 (void) sendtolist(e->e_from.q_paddr, NULLADDR,
269 &e->e_errorqueue, 0, e);
273 else if ((bitset(QPINGONSUCCESS, q->q_flags) &&
274 ((QS_IS_SENT(q->q_state) &&
275 bitnset(M_LOCALMAILER, q->q_mailer->m_flags)) ||
276 bitset(QRELAYED|QEXPANDED|QDELIVERED, q->q_flags))) ||
277 bitset(QBYTRACE, q->q_flags) ||
278 bitset(QBYNRELAY, q->q_flags))
280 success_return = true;
285 e->e_flags |= EF_NO_BODY_RETN;
288 ** See if the message timed out.
293 /* nothing to do */ ;
294 else if (IS_MSG_ERR(msg_timeout))
298 if (msg_timeout == MSG_NOT_BY)
300 (void) sm_snprintf(buf, sizeof(buf),
301 "delivery time expired %lds",
306 (void) sm_snprintf(buf, sizeof(buf),
307 "Cannot send message for %s",
308 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
312 /* don't free, allocated from e_rpool */
313 e->e_message = sm_rpool_strdup_x(e->e_rpool, buf);
315 e->e_flags |= EF_CLRQUEUE;
317 if (msg_timeout == MSG_NOT_BY)
319 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
320 "Delivery time (%lds) expired\n",
324 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
325 "Message could not be delivered for %s\n",
326 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
328 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
329 "Message will be deleted from queue\n");
330 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
332 if (QS_IS_UNDELIVERED(q->q_state))
334 q->q_state = QS_BADADDR;
335 if (msg_timeout == MSG_NOT_BY)
336 q->q_status = "5.4.7";
338 q->q_status = "4.4.7";
344 if (TimeOuts.to_q_warning[e->e_timeoutclass] > 0 &&
346 TimeOuts.to_q_warning[e->e_timeoutclass])
347 msg_timeout = MSG_WARN;
348 else if (IS_DLVR_NOTIFY(e) &&
349 e->e_deliver_by > 0 &&
350 now >= e->e_ctime + e->e_deliver_by)
351 msg_timeout = MSG_WARN_BY;
353 if (IS_MSG_WARN(msg_timeout))
355 if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
357 e->e_from.q_paddr != NULL &&
358 strcmp(e->e_from.q_paddr, "<>") != 0 &&
359 sm_strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 &&
360 (strlen(e->e_from.q_paddr) <= 8 ||
361 sm_strcasecmp(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8],
364 for (q = e->e_sendqueue; q != NULL;
367 if (QS_IS_UNDELIVERED(q->q_state)
368 #if _FFR_NODELAYDSN_ON_HOLD
370 q->q_mailer->m_flags)
371 #endif /* _FFR_NODELAYDSN_ON_HOLD */
376 (bitset(QPINGONDELAY,
382 q->q_flags |= QBYNDELAY;
385 if (bitset(QPINGONDELAY,
388 q->q_flags |= QDELAYED;
396 if (msg_timeout == MSG_WARN_BY)
398 (void) sm_snprintf(buf, sizeof(buf),
399 "Warning: Delivery time (%lds) exceeded",
403 (void) sm_snprintf(buf, sizeof(buf),
404 "Warning: could not send message for past %s",
405 pintvl(TimeOuts.to_q_warning[e->e_timeoutclass],
408 /* don't free, allocated from e_rpool */
409 e->e_message = sm_rpool_strdup_x(e->e_rpool,
412 e->e_flags |= EF_WARNING;
414 if (msg_timeout == MSG_WARN_BY)
416 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
417 "Warning: Delivery time (%lds) exceeded\n",
421 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
422 "Warning: message still undelivered after %s\n",
423 pintvl(TimeOuts.to_q_warning[e->e_timeoutclass],
425 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
426 "Will keep trying until message is %s old\n",
427 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
433 sm_dprintf("failure_return=%d delay_return=%d success_return=%d queueit=%d\n",
434 failure_return, delay_return, success_return, queueit);
437 ** If we had some fatal error, but no addresses are marked as
438 ** bad, mark them _all_ as bad.
441 if (bitset(EF_FATALERRS, e->e_flags) && !failure_return)
443 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
445 if ((QS_IS_OK(q->q_state) ||
446 QS_IS_VERIFIED(q->q_state)) &&
447 bitset(QPINGONFAILURE, q->q_flags))
449 failure_return = true;
450 q->q_state = QS_BADADDR;
456 ** Send back return receipts as requested.
459 if (success_return && !failure_return && !delay_return && fulldrop &&
460 !bitset(PRIV_NORECEIPTS, PrivacyFlags) &&
461 strcmp(e->e_from.q_paddr, "<>") != 0)
463 auto ADDRESS *rlist = NULL;
466 sm_dprintf("dropenvelope(%s): sending return receipt\n",
468 e->e_flags |= EF_SENDRECEIPT;
469 (void) sendtolist(e->e_from.q_paddr, NULLADDR, &rlist, 0, e);
470 (void) returntosender("Return receipt", rlist, RTSF_NO_BODY, e);
472 e->e_flags &= ~EF_SENDRECEIPT;
475 ** Arrange to send error messages if there are fatal errors.
478 if ((failure_return || delay_return) && e->e_errormode != EM_QUIET)
481 sm_dprintf("dropenvelope(%s): saving mail\n", id);
482 panic = savemail(e, !bitset(EF_NO_BODY_RETN, e->e_flags));
486 ** Arrange to send warning messages to postmaster as requested.
489 if ((failure_return || pmnotify) &&
490 PostMasterCopy != NULL &&
491 !bitset(EF_RESPONSE, e->e_flags) &&
494 auto ADDRESS *rlist = NULL;
499 expand(PostMasterCopy, pcopy, sizeof(pcopy), e);
502 sm_dprintf("dropenvelope(%s): sending postmaster copy to %s\n",
504 (void) sendtolist(pcopy, NULLADDR, &rlist, 0, e);
507 (void) sendtolist("postmaster", NULLADDR,
509 (void) returntosender(e->e_message, rlist,
510 RTSF_PM_BOUNCE|RTSF_NO_BODY, e);
514 ** Instantiate or deinstantiate the queue.
519 sm_dprintf("dropenvelope(%s): at simpledrop, queueit=%d\n",
521 if (!queueit || bitset(EF_CLRQUEUE, e->e_flags))
525 sm_dprintf("\n===== Dropping queue files for %s... queueit=%d, e_flags=",
531 if (e->e_dfp != NULL)
533 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
536 (void) xunlink(queuename(e, DATAFL_LETTER));
538 if (panic && QueueMode == QM_LOST)
541 ** leave the Qf file behind as
542 ** the delivery attempt failed.
548 if (xunlink(queuename(e, ANYQFL_LETTER)) == 0)
550 /* add to available space in filesystem */
551 updfs(e, -1, panic ? 0 : -1, "dropenvelope");
554 if (e->e_ntries > 0 && LogLevel > 9)
555 sm_syslog(LOG_INFO, id, "done; delay=%s, ntries=%d",
556 pintvl(curtime() - e->e_ctime, true),
559 else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
562 queueup(e, false, true);
569 ** Save old sibling and set it to NULL to avoid
570 ** queueing up the same envelopes again.
571 ** This requires that envelopes in that list have
572 ** been take care of before (or at some other place).
575 oldsib = e->e_sibling;
577 if (!split_by_recipient(e) &&
578 bitset(EF_FATALERRS, e->e_flags))
580 syserr("!dropenvelope(%s): cannot commit data file %s, uid=%d",
581 e->e_id, queuename(e, DATAFL_LETTER),
584 for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling)
585 queueup(ee, false, true);
586 queueup(e, false, true);
589 for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling)
591 /* now unlock the job */
593 sm_dprintf("dropenvelope(%s): unlocking job\n",
598 /* this envelope is marked unused */
599 if (ee->e_dfp != NULL)
601 (void) sm_io_close(ee->e_dfp,
606 ee->e_flags &= ~EF_HAS_DF;
608 e->e_sibling = oldsib;
612 /* now unlock the job */
614 sm_dprintf("dropenvelope(%s): unlocking job\n", id);
618 /* make sure that this envelope is marked unused */
619 if (e->e_dfp != NULL)
621 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
625 e->e_flags &= ~EF_HAS_DF;
632 ** CLEARENVELOPE -- clear an envelope without unlocking
634 ** This is normally used by a child process to get a clean
635 ** envelope without disturbing the parent.
638 ** e -- the envelope to clear.
639 ** fullclear - if set, the current envelope is total
640 ** garbage and should be ignored; otherwise,
641 ** release any resources it may indicate.
642 ** rpool -- either NULL, or a pointer to a resource pool
643 ** from which envelope memory is allocated, and
644 ** to which envelope resources are attached.
650 ** Closes files associated with the envelope.
651 ** Marks the envelope as unallocated.
655 clearenvelope(e, fullclear, rpool)
656 register ENVELOPE *e;
662 extern ENVELOPE BlankEnvelope;
667 /* clear out any file information */
668 if (e->e_xfp != NULL)
669 (void) sm_io_close(e->e_xfp, SM_TIME_DEFAULT);
670 if (e->e_dfp != NULL)
671 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
672 e->e_xfp = e->e_dfp = NULL;
676 ** Copy BlankEnvelope into *e.
677 ** It is not safe to simply copy pointers to strings;
678 ** the strings themselves must be copied (or set to NULL).
679 ** The problem is that when we assign a new string value to
680 ** a member of BlankEnvelope, we free the old string.
681 ** We did not need to do this copying in sendmail 8.11 :-(
682 ** and it is a potential performance hit. Reference counted
683 ** strings are one way out.
688 e->e_qfletter = '\0';
690 macdefine(&e->e_macro, A_PERM, macid("{quarantine}"), "");
693 ** Copy the macro table.
694 ** We might be able to avoid this by zeroing the macro table
695 ** and always searching BlankEnvelope.e_macro after e->e_macro
699 for (p = &e->e_macro.mac_table[0];
700 p <= &e->e_macro.mac_table[MAXMACROID];
704 *p = sm_rpool_strdup_x(rpool, *p);
708 ** XXX There are many strings in the envelope structure
709 ** XXX that we are not attempting to copy here.
710 ** XXX Investigate this further.
714 e->e_macro.mac_rpool = rpool;
716 set_delivery_mode(SM_DELIVER, e);
717 bh = BlankEnvelope.e_header;
721 *nhp = (HDR *) sm_rpool_malloc_x(rpool, sizeof(*bh));
722 memmove((char *) *nhp, (char *) bh, sizeof(*bh));
724 nhp = &(*nhp)->h_link;
726 #if _FFR_MILTER_ENHSC
727 e->e_enhsc[0] = '\0';
728 #endif /* _FFR_MILTER_ENHSC */
731 ** INITSYS -- initialize instantiation of system
733 ** In Daemon mode, this is done in the child.
736 ** e -- the envelope to use.
742 ** Initializes the system macros, some global variables,
743 ** etc. In particular, the current time in various
749 register ENVELOPE *e;
753 static char ybuf[60]; /* holds tty id */
755 extern char *ttyname();
759 ** Give this envelope a reality.
760 ** I.e., an id, a transcript, and a creation time.
761 ** We don't select the queue until all of the recipients are known.
765 e->e_ctime = curtime();
766 e->e_qfletter = '\0';
769 ** Set OutChannel to something useful if stdout isn't it.
770 ** This arranges that any extra stuff the mailer produces
771 ** gets sent back to the user on error (because it is
772 ** tucked away in the transcript).
775 if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) &&
777 OutChannel = e->e_xfp;
780 ** Set up some basic system macros.
784 (void) sm_snprintf(buf, sizeof(buf), "%d", (int) CurrentPid);
785 macdefine(&e->e_macro, A_TEMP, 'p', buf);
788 (void) sm_snprintf(buf, sizeof(buf), "%d", e->e_hopcount);
789 macdefine(&e->e_macro, A_TEMP, 'c', buf);
791 /* time as integer, unix time, arpa time */
799 if (macvalue('y', e) == NULL)
804 if (strrchr(p, '/') != NULL)
805 p = strrchr(p, '/') + 1;
806 (void) sm_strlcpy(ybuf, sizeof(ybuf), p);
807 macdefine(&e->e_macro, A_PERM, 'y', ybuf);
813 ** SETTIME -- set the current time.
816 ** e -- the envelope in which the macros should be set.
822 ** Sets the various time macros -- $a, $b, $d, $t.
827 register ENVELOPE *e;
832 register struct tm *tm;
835 (void) sm_snprintf(buf, sizeof(buf), "%ld", (long) now);
836 macdefine(&e->e_macro, A_TEMP, macid("{time}"), buf);
838 (void) sm_snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d",
839 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
840 tm->tm_hour, tm->tm_min);
841 macdefine(&e->e_macro, A_TEMP, 't', buf);
842 (void) sm_strlcpy(buf, ctime(&now), sizeof(buf));
843 p = strchr(buf, '\n');
846 macdefine(&e->e_macro, A_TEMP, 'd', buf);
847 macdefine(&e->e_macro, A_TEMP, 'b', arpadate(buf));
848 if (macvalue('a', e) == NULL)
849 macdefine(&e->e_macro, A_PERM, 'a', macvalue('b', e));
852 ** OPENXSCRIPT -- Open transcript file
854 ** Creates a transcript file for possible eventual mailing or
858 ** e -- the envelope to create the transcript in/for.
864 ** Creates the transcript file.
869 #endif /* ! O_APPEND */
873 register ENVELOPE *e;
877 if (e->e_xfp != NULL)
881 if (e->e_lockfp == NULL && bitset(EF_INQUEUE, e->e_flags))
882 syserr("openxscript: job not locked");
885 p = queuename(e, XSCRPT_LETTER);
886 e->e_xfp = bfopen(p, FileMode, XscriptFileBufferSize,
887 SFF_NOTEXCL|SFF_OPENASROOT);
889 if (e->e_xfp == NULL)
891 syserr("Can't create transcript file %s", p);
892 e->e_xfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT,
893 SM_PATH_DEVNULL, SM_IO_RDWR, NULL);
894 if (e->e_xfp == NULL)
895 syserr("!Can't open %s", SM_PATH_DEVNULL);
897 (void) sm_io_setvbuf(e->e_xfp, SM_TIME_DEFAULT, NULL, SM_IO_LBF, 0);
900 sm_dprintf("openxscript(%s):\n ", p);
901 dumpfd(sm_io_getinfo(e->e_xfp, SM_IO_WHAT_FD, NULL), true,
906 ** CLOSEXSCRIPT -- close the transcript file.
909 ** e -- the envelope containing the transcript to close.
920 register ENVELOPE *e;
922 if (e->e_xfp == NULL)
925 if (e->e_lockfp == NULL)
926 syserr("closexscript: job not locked");
928 (void) sm_io_close(e->e_xfp, SM_TIME_DEFAULT);
932 ** SETSENDER -- set the person who this message is from
934 ** Under certain circumstances allow the user to say who
935 ** s/he is (using -f or -r). These are:
936 ** 1. The user's uid is zero (root).
937 ** 2. The user's login name is in an approved list (typically
938 ** from a network server).
939 ** 3. The address the user is trying to claim has a
940 ** "!" character in it (since #2 doesn't do it for
941 ** us if we are dialing out for UUCP).
942 ** A better check to replace #3 would be if the
943 ** effective uid is "UUCP" -- this would require me
944 ** to rewrite getpwent to "grab" uucp as it went by,
945 ** make getname more nasty, do another passwd file
946 ** scan, or compile the UID of "UUCP" into the code,
947 ** all of which are reprehensible.
949 ** Assuming all of these fail, we figure out something
953 ** from -- the person we would like to believe this message
954 ** is from, as specified on the command line.
955 ** e -- the envelope in which we would like the sender set.
956 ** delimptr -- if non-NULL, set to the location of the
957 ** trailing delimiter.
958 ** delimchar -- the character that will delimit the sender
960 ** internal -- set if this address is coming from an internal
961 ** source such as an owner alias.
967 ** sets sendmail's notion of who the from person is.
971 setsender(from, e, delimptr, delimchar, internal)
973 register ENVELOPE *e;
979 char *realname = NULL;
981 char buf[MAXNAME + 2];
982 char pvpbuf[PSBUFSIZE];
983 extern char *FullName;
986 sm_dprintf("setsender(%s)\n", from == NULL ? "" : from);
988 /* may be set from earlier calls */
989 macdefine(&e->e_macro, A_PERM, 'x', "");
992 ** Figure out the real user executing us.
993 ** Username can return errno != 0 on non-errors.
996 if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP ||
997 OpMode == MD_ARPAFTP || OpMode == MD_DAEMON)
999 if (realname == NULL || realname[0] == '\0')
1000 realname = username();
1002 if (ConfigLevel < 2)
1005 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e s");
1007 /* preset state for then clause in case from == NULL */
1008 e->e_from.q_state = QS_BADADDR;
1009 e->e_from.q_flags = 0;
1011 parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR,
1012 delimchar, delimptr, e, false) == NULL ||
1013 QS_IS_BADADDR(e->e_from.q_state) ||
1014 e->e_from.q_mailer == ProgMailer ||
1015 e->e_from.q_mailer == FileMailer ||
1016 e->e_from.q_mailer == InclMailer)
1018 /* log garbage addresses for traceback */
1019 if (from != NULL && LogLevel > 2)
1022 char ebuf[MAXNAME * 2 + 2];
1024 p = macvalue('_', e);
1027 char *host = RealHostName;
1031 (void) sm_snprintf(ebuf, sizeof(ebuf),
1032 "%.*s@%.*s", MAXNAME,
1033 realname, MAXNAME, host);
1036 sm_syslog(LOG_NOTICE, e->e_id,
1037 "setsender: %s: invalid or unparsable, received from %s",
1038 shortenstring(from, 83), p);
1042 if (!QS_IS_BADADDR(e->e_from.q_state))
1044 /* it was a bogus mailer in the from addr */
1045 e->e_status = "5.1.7";
1046 usrerrenh(e->e_status,
1047 "553 Invalid sender address");
1051 if (from == realname ||
1052 parseaddr(from = realname,
1053 &e->e_from, RF_COPYALL|RF_SENDERADDR, ' ',
1054 NULL, e, false) == NULL)
1059 expand("\201n", nbuf, sizeof(nbuf), e);
1060 from = sm_rpool_strdup_x(e->e_rpool, nbuf);
1061 if (parseaddr(from, &e->e_from, RF_COPYALL, ' ',
1062 NULL, e, false) == NULL &&
1063 parseaddr(from = "postmaster", &e->e_from,
1064 RF_COPYALL, ' ', NULL, e, false) == NULL)
1065 syserr("553 5.3.0 setsender: can't even parse postmaster!");
1070 e->e_from.q_state = QS_SENDER;
1073 sm_dprintf("setsender: QS_SENDER ");
1074 printaddr(sm_debug_file(), &e->e_from, false);
1079 if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags))
1083 p = udbsender(e->e_from.q_user, e->e_rpool);
1089 if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
1095 /* if the user already given fullname don't redefine */
1096 if (FullName == NULL)
1097 FullName = macvalue('x', e);
1098 if (FullName != NULL)
1100 if (FullName[0] == '\0')
1103 FullName = newstr(FullName);
1107 if (e->e_from.q_user[0] != '\0' &&
1108 sm_mbdb_lookup(e->e_from.q_user, &user) == EX_OK)
1111 ** Process passwd file entry.
1114 /* extract home directory */
1115 if (*user.mbdb_homedir == '\0')
1116 e->e_from.q_home = NULL;
1117 else if (strcmp(user.mbdb_homedir, "/") == 0)
1118 e->e_from.q_home = "";
1120 e->e_from.q_home = sm_rpool_strdup_x(e->e_rpool,
1122 macdefine(&e->e_macro, A_PERM, 'z', e->e_from.q_home);
1124 /* extract user and group id */
1125 if (user.mbdb_uid != SM_NO_UID)
1127 e->e_from.q_uid = user.mbdb_uid;
1128 e->e_from.q_gid = user.mbdb_gid;
1129 e->e_from.q_flags |= QGOODUID;
1132 /* extract full name from passwd file */
1133 if (FullName == NULL && !internal &&
1134 user.mbdb_fullname[0] != '\0' &&
1135 strcmp(user.mbdb_name, e->e_from.q_user) == 0)
1137 FullName = newstr(user.mbdb_fullname);
1142 e->e_from.q_home = NULL;
1144 if (FullName != NULL && !internal)
1145 macdefine(&e->e_macro, A_TEMP, 'x', FullName);
1147 else if (!internal && OpMode != MD_DAEMON && OpMode != MD_SMTP)
1149 if (e->e_from.q_home == NULL)
1151 e->e_from.q_home = getenv("HOME");
1152 if (e->e_from.q_home != NULL)
1154 if (*e->e_from.q_home == '\0')
1155 e->e_from.q_home = NULL;
1156 else if (strcmp(e->e_from.q_home, "/") == 0)
1160 e->e_from.q_uid = RealUid;
1161 e->e_from.q_gid = RealGid;
1162 e->e_from.q_flags |= QGOODUID;
1166 ** Rewrite the from person to dispose of possible implicit
1167 ** links in the net.
1170 pvp = prescan(from, delimchar, pvpbuf, sizeof(pvpbuf), NULL,
1171 IntTokenTab, false);
1174 /* don't need to give error -- prescan did that already */
1176 sm_syslog(LOG_NOTICE, e->e_id,
1177 "cannot prescan from (%s)",
1178 shortenstring(from, MAXSHORTSTR));
1179 finis(true, true, ExitStat);
1181 (void) REWRITE(pvp, 3, e);
1182 (void) REWRITE(pvp, 1, e);
1183 (void) REWRITE(pvp, 4, e);
1184 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
1186 cataddr(pvp, NULL, bp, sizeof(buf) - 2, '\0', false);
1187 if (*bp == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags))
1189 /* heuristic: route-addr: add angle brackets */
1190 (void) sm_strlcat(bp, ">", sizeof(buf) - 1);
1193 e->e_sender = sm_rpool_strdup_x(e->e_rpool, bp);
1194 macdefine(&e->e_macro, A_PERM, 'f', e->e_sender);
1196 /* save the domain spec if this mailer wants it */
1197 if (e->e_from.q_mailer != NULL &&
1198 bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
1202 /* get rid of any pesky angle brackets */
1203 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e s");
1204 (void) REWRITE(pvp, 3, e);
1205 (void) REWRITE(pvp, 1, e);
1206 (void) REWRITE(pvp, 4, e);
1207 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
1209 /* strip off to the last "@" sign */
1210 for (lastat = NULL; *pvp != NULL; pvp++)
1212 if (strcmp(*pvp, "@") == 0)
1217 e->e_fromdomain = copyplist(lastat, true, e->e_rpool);
1220 sm_dprintf("Saving from domain: ");
1221 printav(sm_debug_file(), e->e_fromdomain);
1227 ** PRINTENVFLAGS -- print envelope flags for debugging
1230 ** e -- the envelope with the flags to be printed.
1239 unsigned long ef_bit;
1242 static struct eflags EnvelopeFlags[] =
1244 { "OLDSTYLE", EF_OLDSTYLE },
1245 { "INQUEUE", EF_INQUEUE },
1246 { "NO_BODY_RETN", EF_NO_BODY_RETN },
1247 { "CLRQUEUE", EF_CLRQUEUE },
1248 { "SENDRECEIPT", EF_SENDRECEIPT },
1249 { "FATALERRS", EF_FATALERRS },
1250 { "DELETE_BCC", EF_DELETE_BCC },
1251 { "RESPONSE", EF_RESPONSE },
1252 { "RESENT", EF_RESENT },
1253 { "VRFYONLY", EF_VRFYONLY },
1254 { "WARNING", EF_WARNING },
1255 { "QUEUERUN", EF_QUEUERUN },
1256 { "GLOBALERRS", EF_GLOBALERRS },
1257 { "PM_NOTIFY", EF_PM_NOTIFY },
1258 { "METOO", EF_METOO },
1259 { "LOGSENDER", EF_LOGSENDER },
1260 { "NORECEIPT", EF_NORECEIPT },
1261 { "HAS8BIT", EF_HAS8BIT },
1262 { "NL_NOT_EOL", EF_NL_NOT_EOL },
1263 { "CRLF_NOT_EOL", EF_CRLF_NOT_EOL },
1264 { "RET_PARAM", EF_RET_PARAM },
1265 { "HAS_DF", EF_HAS_DF },
1266 { "IS_MIME", EF_IS_MIME },
1267 { "DONT_MIME", EF_DONT_MIME },
1268 { "DISCARD", EF_DISCARD },
1269 { "TOOBIG", EF_TOOBIG },
1270 { "SPLIT", EF_SPLIT },
1271 { "UNSAFE", EF_UNSAFE },
1277 register ENVELOPE *e;
1279 register struct eflags *ef;
1282 sm_dprintf("%lx", e->e_flags);
1283 for (ef = EnvelopeFlags; ef->ef_name != NULL; ef++)
1285 if (!bitset(ef->ef_bit, e->e_flags))
1288 sm_dprintf("<%s", ef->ef_name);
1290 sm_dprintf(",%s", ef->ef_name);