2 * Copyright (c) 1998-2003, 2006 Proofpoint, Inc. and its suppliers.
4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
16 SM_RCSID("@(#)$Id: envelope.c,v 8.313 2013-11-22 20:51:55 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}"), "");
45 macdefine(&e->e_macro, A_PERM, macid("{alg_bits}"), "");
46 macdefine(&e->e_macro, A_PERM, macid("{cn_issuer}"), "");
47 macdefine(&e->e_macro, A_PERM, macid("{cn_subject}"), "");
52 ** NEWENVELOPE -- fill in a new envelope
54 ** Supports inheritance.
57 ** e -- the new envelope to fill in.
58 ** parent -- the envelope to be the parent of e.
59 ** rpool -- either NULL, or a pointer to a resource pool
60 ** from which envelope memory is allocated, and
61 ** to which envelope resources are attached.
71 newenvelope(e, parent, rpool)
73 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.
88 sendmode = parent->e_sendmode;
94 clearenvelope(e, true, rpool);
96 memmove((char *) &e->e_from,
97 (char *) &NullAddress,
100 memmove((char *) &e->e_from,
101 (char *) &CurEnv->e_from,
103 e->e_parent = parent;
105 e->e_ctime = curtime();
107 e->e_sessid = e->e_id;
108 #endif /* _FFR_SESSID */
111 e->e_msgpriority = parent->e_msgsize;
113 if (parent->e_sessid != NULL)
114 e->e_sessid = sm_rpool_strdup_x(rpool,
116 #endif /* _FFR_SESSID */
118 if (parent->e_quarmsg == NULL)
121 macdefine(&e->e_macro, A_PERM,
122 macid("{quarantine}"), "");
126 e->e_quarmsg = sm_rpool_strdup_x(rpool,
128 macdefine(&e->e_macro, A_PERM,
129 macid("{quarantine}"), e->e_quarmsg);
132 e->e_puthdr = putheader;
133 e->e_putbody = putbody;
134 if (CurEnv->e_xfp != NULL)
135 (void) sm_io_flush(CurEnv->e_xfp, SM_TIME_DEFAULT);
136 if (sendmode != DM_NOTSET)
137 set_delivery_mode(sendmode, e);
142 /* values for msg_timeout, see also IS_* below for usage (bit layout) */
143 #define MSG_T_O 0x01 /* normal timeout */
144 #define MSG_T_O_NOW 0x02 /* NOW timeout */
145 #define MSG_NOT_BY 0x04 /* Deliver-By time exceeded, mode R */
146 #define MSG_WARN 0x10 /* normal queue warning */
147 #define MSG_WARN_BY 0x20 /* Deliver-By time exceeded, mode N */
149 #define IS_MSG_ERR(x) (((x) & 0x0f) != 0) /* return an error */
151 /* immediate return */
152 #define IS_IMM_RET(x) (((x) & (MSG_T_O_NOW|MSG_NOT_BY)) != 0)
153 #define IS_MSG_WARN(x) (((x) & 0xf0) != 0) /* return a warning */
156 ** DROPENVELOPE -- deallocate an envelope.
159 ** e -- the envelope to deallocate.
160 ** fulldrop -- if set, do return receipts.
161 ** split -- if true, split by recipient if message is queued up
164 ** EX_* status (currently: 0: success, EX_IOERR on panic)
167 ** housekeeping necessary to dispose of an envelope.
168 ** Unlocks this queue file.
172 dropenvelope(e, fulldrop, split)
173 register ENVELOPE *e;
178 bool queueit = false;
180 bool failure_return = false;
181 bool delay_return = false;
182 bool success_return = false;
183 bool pmnotify = bitset(EF_PM_NOTIFY, e->e_flags);
192 sm_dprintf("dropenvelope %p: id=", e);
193 xputs(sm_debug_file(), e->e_id);
194 sm_dprintf(", flags=");
198 sm_dprintf("sendq=");
199 printaddr(sm_debug_file(), e->e_sendqueue, true);
204 sm_syslog(LOG_DEBUG, id,
205 "dropenvelope, e_flags=0x%lx, OpMode=%c, pid=%d",
206 e->e_flags, OpMode, (int) CurrentPid);
208 /* we must have an id to remove disk files */
212 /* if verify-only mode, we can skip most of this */
213 if (OpMode == MD_VERIFY)
217 sm_dprintf("dropenvelope: e_id=%s, EF_LOGSENDER=%d, LogLevel=%d\n",
218 e->e_id, bitset(EF_LOGSENDER, e->e_flags), LogLevel);
219 if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
221 e->e_flags &= ~EF_LOGSENDER;
223 /* post statistics */
227 ** Extract state information from dregs of send list.
231 if (now >= e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass])
232 msg_timeout = MSG_T_O;
233 if (IS_DLVR_RETURN(e) && e->e_deliver_by > 0 &&
234 now >= e->e_ctime + e->e_deliver_by &&
235 !bitset(EF_RESPONSE, e->e_flags))
237 msg_timeout = MSG_NOT_BY;
238 e->e_flags |= EF_FATALERRS|EF_CLRQUEUE;
240 else if (TimeOuts.to_q_return[e->e_timeoutclass] == NOW &&
241 !bitset(EF_RESPONSE, e->e_flags))
243 msg_timeout = MSG_T_O_NOW;
244 e->e_flags |= EF_FATALERRS|EF_CLRQUEUE;
251 sm_dprintf("dropenvelope: mode=%c, e=%p, sibling=%p, nrcpts=%d, sendqueue=%p, next=%p, state=%d\n",
252 e->e_sendmode, e, e->e_sibling, e->e_nrcpts, q,
253 (q == NULL) ? (void *)0 : q->q_next,
254 (q == NULL) ? -1 : q->q_state);
256 #endif /* _FFR_PROXY */
258 e->e_flags &= ~EF_QUEUERUN;
259 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
261 if (QS_IS_UNDELIVERED(q->q_state))
265 if (queueit && e->e_sendmode == SM_PROXY)
267 #endif /* _FFR_PROXY */
269 /* see if a notification is needed */
270 if (bitset(QPINGONFAILURE, q->q_flags) &&
271 ((IS_MSG_ERR(msg_timeout) &&
272 QS_IS_UNDELIVERED(q->q_state)) ||
273 QS_IS_BADADDR(q->q_state) ||
274 IS_IMM_RET(msg_timeout)))
276 failure_return = true;
277 if (!done && q->q_owner == NULL &&
278 !emptyaddr(&e->e_from))
280 (void) sendtolist(e->e_from.q_paddr, NULLADDR,
281 &e->e_errorqueue, 0, e);
285 else if ((bitset(QPINGONSUCCESS, q->q_flags) &&
286 ((QS_IS_SENT(q->q_state) &&
287 bitnset(M_LOCALMAILER, q->q_mailer->m_flags)) ||
288 bitset(QRELAYED|QEXPANDED|QDELIVERED, q->q_flags))) ||
289 bitset(QBYTRACE, q->q_flags) ||
290 bitset(QBYNRELAY, q->q_flags))
292 success_return = true;
297 e->e_flags |= EF_NO_BODY_RETN;
300 ** See if the message timed out.
305 /* nothing to do */ ;
306 else if (IS_MSG_ERR(msg_timeout))
310 if (msg_timeout == MSG_NOT_BY)
312 (void) sm_snprintf(buf, sizeof(buf),
313 "delivery time expired %lds",
318 (void) sm_snprintf(buf, sizeof(buf),
319 "Cannot send message for %s",
320 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
324 /* don't free, allocated from e_rpool */
325 e->e_message = sm_rpool_strdup_x(e->e_rpool, buf);
327 e->e_flags |= EF_CLRQUEUE;
329 if (msg_timeout == MSG_NOT_BY)
331 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
332 "Delivery time (%lds) expired\n",
336 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
337 "Message could not be delivered for %s\n",
338 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
340 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
341 "Message will be deleted from queue\n");
342 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
344 if (QS_IS_UNDELIVERED(q->q_state))
346 q->q_state = QS_BADADDR;
347 if (msg_timeout == MSG_NOT_BY)
348 q->q_status = "5.4.7";
350 q->q_status = "4.4.7";
356 if (TimeOuts.to_q_warning[e->e_timeoutclass] > 0 &&
358 TimeOuts.to_q_warning[e->e_timeoutclass])
359 msg_timeout = MSG_WARN;
360 else if (IS_DLVR_NOTIFY(e) &&
361 e->e_deliver_by > 0 &&
362 now >= e->e_ctime + e->e_deliver_by)
363 msg_timeout = MSG_WARN_BY;
365 if (IS_MSG_WARN(msg_timeout))
367 if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
369 e->e_from.q_paddr != NULL &&
370 strcmp(e->e_from.q_paddr, "<>") != 0 &&
371 sm_strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 &&
372 (strlen(e->e_from.q_paddr) <= 8 ||
373 sm_strcasecmp(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8],
376 for (q = e->e_sendqueue; q != NULL;
379 if (QS_IS_UNDELIVERED(q->q_state)
380 #if _FFR_NODELAYDSN_ON_HOLD
382 q->q_mailer->m_flags)
383 #endif /* _FFR_NODELAYDSN_ON_HOLD */
388 (bitset(QPINGONDELAY,
394 q->q_flags |= QBYNDELAY;
397 if (bitset(QPINGONDELAY,
400 q->q_flags |= QDELAYED;
408 if (msg_timeout == MSG_WARN_BY)
410 (void) sm_snprintf(buf, sizeof(buf),
411 "Warning: Delivery time (%lds) exceeded",
415 (void) sm_snprintf(buf, sizeof(buf),
416 "Warning: could not send message for past %s",
417 pintvl(TimeOuts.to_q_warning[e->e_timeoutclass],
420 /* don't free, allocated from e_rpool */
421 e->e_message = sm_rpool_strdup_x(e->e_rpool,
424 e->e_flags |= EF_WARNING;
426 if (msg_timeout == MSG_WARN_BY)
428 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
429 "Warning: Delivery time (%lds) exceeded\n",
433 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
434 "Warning: message still undelivered after %s\n",
435 pintvl(TimeOuts.to_q_warning[e->e_timeoutclass],
437 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
438 "Will keep trying until message is %s old\n",
439 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
445 sm_dprintf("failure_return=%d delay_return=%d success_return=%d queueit=%d\n",
446 failure_return, delay_return, success_return, queueit);
449 ** If we had some fatal error, but no addresses are marked as
450 ** bad, mark them _all_ as bad.
453 if (bitset(EF_FATALERRS, e->e_flags) && !failure_return)
455 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
457 if ((QS_IS_OK(q->q_state) ||
458 QS_IS_VERIFIED(q->q_state)) &&
459 bitset(QPINGONFAILURE, q->q_flags))
461 failure_return = true;
462 q->q_state = QS_BADADDR;
468 ** Send back return receipts as requested.
471 if (success_return && !failure_return && !delay_return && fulldrop &&
472 !bitset(PRIV_NORECEIPTS, PrivacyFlags) &&
473 strcmp(e->e_from.q_paddr, "<>") != 0)
475 auto ADDRESS *rlist = NULL;
478 sm_dprintf("dropenvelope(%s): sending return receipt\n",
480 e->e_flags |= EF_SENDRECEIPT;
481 (void) sendtolist(e->e_from.q_paddr, NULLADDR, &rlist, 0, e);
482 (void) returntosender("Return receipt", rlist, RTSF_NO_BODY, e);
484 e->e_flags &= ~EF_SENDRECEIPT;
487 ** Arrange to send error messages if there are fatal errors.
490 if ((failure_return || delay_return) && e->e_errormode != EM_QUIET)
493 sm_dprintf("dropenvelope(%s): saving mail\n", id);
494 panic = savemail(e, !bitset(EF_NO_BODY_RETN, e->e_flags));
498 ** Arrange to send warning messages to postmaster as requested.
501 if ((failure_return || pmnotify) &&
502 PostMasterCopy != NULL &&
503 !bitset(EF_RESPONSE, e->e_flags) &&
506 auto ADDRESS *rlist = NULL;
511 expand(PostMasterCopy, pcopy, sizeof(pcopy), e);
514 sm_dprintf("dropenvelope(%s): sending postmaster copy to %s\n",
516 (void) sendtolist(pcopy, NULLADDR, &rlist, 0, e);
519 (void) sendtolist("postmaster", NULLADDR,
521 (void) returntosender(e->e_message, rlist,
522 RTSF_PM_BOUNCE|RTSF_NO_BODY, e);
526 ** Instantiate or deinstantiate the queue.
531 sm_dprintf("dropenvelope(%s): at simpledrop, queueit=%d\n",
533 if (!queueit || bitset(EF_CLRQUEUE, e->e_flags))
537 sm_dprintf("\n===== Dropping queue files for %s... queueit=%d, e_flags=",
543 if (e->e_dfp != NULL)
545 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
548 (void) xunlink(queuename(e, DATAFL_LETTER));
550 if (panic && QueueMode == QM_LOST)
553 ** leave the Qf file behind as
554 ** the delivery attempt failed.
560 if (xunlink(queuename(e, ANYQFL_LETTER)) == 0)
562 /* add to available space in filesystem */
563 updfs(e, -1, panic ? 0 : -1, "dropenvelope");
566 if (e->e_ntries > 0 && LogLevel > 9)
567 sm_syslog(LOG_INFO, id, "done; delay=%s, ntries=%d",
568 pintvl(curtime() - e->e_ctime, true),
571 else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
574 queueup(e, false, true);
581 ** Save old sibling and set it to NULL to avoid
582 ** queueing up the same envelopes again.
583 ** This requires that envelopes in that list have
584 ** been take care of before (or at some other place).
587 oldsib = e->e_sibling;
589 if (!split_by_recipient(e) &&
590 bitset(EF_FATALERRS, e->e_flags))
592 syserr("!dropenvelope(%s): cannot commit data file %s, uid=%ld",
593 e->e_id, queuename(e, DATAFL_LETTER),
596 for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling)
597 queueup(ee, false, true);
598 queueup(e, false, true);
601 for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling)
603 /* now unlock the job */
605 sm_dprintf("dropenvelope(%s): unlocking job\n",
610 /* this envelope is marked unused */
611 if (ee->e_dfp != NULL)
613 (void) sm_io_close(ee->e_dfp,
618 ee->e_flags &= ~EF_HAS_DF;
620 e->e_sibling = oldsib;
624 /* now unlock the job */
626 sm_dprintf("dropenvelope(%s): unlocking job\n", id);
630 /* make sure that this envelope is marked unused */
631 if (e->e_dfp != NULL)
633 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
637 e->e_flags &= ~EF_HAS_DF;
644 ** CLEARENVELOPE -- clear an envelope without unlocking
646 ** This is normally used by a child process to get a clean
647 ** envelope without disturbing the parent.
650 ** e -- the envelope to clear.
651 ** fullclear - if set, the current envelope is total
652 ** garbage and should be ignored; otherwise,
653 ** release any resources it may indicate.
654 ** rpool -- either NULL, or a pointer to a resource pool
655 ** from which envelope memory is allocated, and
656 ** to which envelope resources are attached.
662 ** Closes files associated with the envelope.
663 ** Marks the envelope as unallocated.
667 clearenvelope(e, fullclear, rpool)
668 register ENVELOPE *e;
674 extern ENVELOPE BlankEnvelope;
679 /* clear out any file information */
680 if (e->e_xfp != NULL)
681 (void) sm_io_close(e->e_xfp, SM_TIME_DEFAULT);
682 if (e->e_dfp != NULL)
683 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
684 e->e_xfp = e->e_dfp = NULL;
688 ** Copy BlankEnvelope into *e.
689 ** It is not safe to simply copy pointers to strings;
690 ** the strings themselves must be copied (or set to NULL).
691 ** The problem is that when we assign a new string value to
692 ** a member of BlankEnvelope, we free the old string.
693 ** We did not need to do this copying in sendmail 8.11 :-(
694 ** and it is a potential performance hit. Reference counted
695 ** strings are one way out.
700 e->e_qfletter = '\0';
702 macdefine(&e->e_macro, A_PERM, macid("{quarantine}"), "");
705 ** Copy the macro table.
706 ** We might be able to avoid this by zeroing the macro table
707 ** and always searching BlankEnvelope.e_macro after e->e_macro
711 for (p = &e->e_macro.mac_table[0];
712 p <= &e->e_macro.mac_table[MAXMACROID];
716 *p = sm_rpool_strdup_x(rpool, *p);
720 ** XXX There are many strings in the envelope structure
721 ** XXX that we are not attempting to copy here.
722 ** XXX Investigate this further.
726 e->e_macro.mac_rpool = rpool;
728 set_delivery_mode(SM_DELIVER, e);
729 bh = BlankEnvelope.e_header;
733 *nhp = (HDR *) sm_rpool_malloc_x(rpool, sizeof(*bh));
734 memmove((char *) *nhp, (char *) bh, sizeof(*bh));
736 nhp = &(*nhp)->h_link;
738 #if _FFR_MILTER_ENHSC
739 e->e_enhsc[0] = '\0';
740 #endif /* _FFR_MILTER_ENHSC */
743 ** INITSYS -- initialize instantiation of system
745 ** In Daemon mode, this is done in the child.
748 ** e -- the envelope to use.
754 ** Initializes the system macros, some global variables,
755 ** etc. In particular, the current time in various
761 register ENVELOPE *e;
765 static char ybuf[60]; /* holds tty id */
767 extern char *ttyname();
771 ** Give this envelope a reality.
772 ** I.e., an id, a transcript, and a creation time.
773 ** We don't select the queue until all of the recipients are known.
777 e->e_ctime = curtime();
778 e->e_qfletter = '\0';
781 ** Set OutChannel to something useful if stdout isn't it.
782 ** This arranges that any extra stuff the mailer produces
783 ** gets sent back to the user on error (because it is
784 ** tucked away in the transcript).
787 if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) &&
789 OutChannel = e->e_xfp;
792 ** Set up some basic system macros.
796 (void) sm_snprintf(buf, sizeof(buf), "%d", (int) CurrentPid);
797 macdefine(&e->e_macro, A_TEMP, 'p', buf);
800 (void) sm_snprintf(buf, sizeof(buf), "%d", e->e_hopcount);
801 macdefine(&e->e_macro, A_TEMP, 'c', buf);
803 /* time as integer, unix time, arpa time */
811 if (macvalue('y', e) == NULL)
816 if (strrchr(p, '/') != NULL)
817 p = strrchr(p, '/') + 1;
818 (void) sm_strlcpy(ybuf, sizeof(ybuf), p);
819 macdefine(&e->e_macro, A_PERM, 'y', ybuf);
825 ** SETTIME -- set the current time.
828 ** e -- the envelope in which the macros should be set.
834 ** Sets the various time macros -- $a, $b, $d, $t.
839 register ENVELOPE *e;
844 register struct tm *tm;
847 (void) sm_snprintf(buf, sizeof(buf), "%ld", (long) now);
848 macdefine(&e->e_macro, A_TEMP, macid("{time}"), buf);
850 (void) sm_snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d",
851 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
852 tm->tm_hour, tm->tm_min);
853 macdefine(&e->e_macro, A_TEMP, 't', buf);
854 (void) sm_strlcpy(buf, ctime(&now), sizeof(buf));
855 p = strchr(buf, '\n');
858 macdefine(&e->e_macro, A_TEMP, 'd', buf);
859 macdefine(&e->e_macro, A_TEMP, 'b', arpadate(buf));
860 if (macvalue('a', e) == NULL)
861 macdefine(&e->e_macro, A_PERM, 'a', macvalue('b', e));
864 ** OPENXSCRIPT -- Open transcript file
866 ** Creates a transcript file for possible eventual mailing or
870 ** e -- the envelope to create the transcript in/for.
876 ** Creates the transcript file.
881 #endif /* ! O_APPEND */
885 register ENVELOPE *e;
889 if (e->e_xfp != NULL)
893 if (e->e_lockfp == NULL && bitset(EF_INQUEUE, e->e_flags))
894 syserr("openxscript: job not locked");
897 p = queuename(e, XSCRPT_LETTER);
898 e->e_xfp = bfopen(p, FileMode, XscriptFileBufferSize,
899 SFF_NOTEXCL|SFF_OPENASROOT);
901 if (e->e_xfp == NULL)
903 syserr("Can't create transcript file %s", p);
904 e->e_xfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT,
905 SM_PATH_DEVNULL, SM_IO_RDWR, NULL);
906 if (e->e_xfp == NULL)
907 syserr("!Can't open %s", SM_PATH_DEVNULL);
909 (void) sm_io_setvbuf(e->e_xfp, SM_TIME_DEFAULT, NULL, SM_IO_LBF, 0);
912 sm_dprintf("openxscript(%s):\n ", p);
913 dumpfd(sm_io_getinfo(e->e_xfp, SM_IO_WHAT_FD, NULL), true,
918 ** CLOSEXSCRIPT -- close the transcript file.
921 ** e -- the envelope containing the transcript to close.
932 register ENVELOPE *e;
934 if (e->e_xfp == NULL)
937 if (e->e_lockfp == NULL)
938 syserr("closexscript: job not locked");
940 (void) sm_io_close(e->e_xfp, SM_TIME_DEFAULT);
944 ** SETSENDER -- set the person who this message is from
946 ** Under certain circumstances allow the user to say who
947 ** s/he is (using -f or -r). These are:
948 ** 1. The user's uid is zero (root).
949 ** 2. The user's login name is in an approved list (typically
950 ** from a network server).
951 ** 3. The address the user is trying to claim has a
952 ** "!" character in it (since #2 doesn't do it for
953 ** us if we are dialing out for UUCP).
954 ** A better check to replace #3 would be if the
955 ** effective uid is "UUCP" -- this would require me
956 ** to rewrite getpwent to "grab" uucp as it went by,
957 ** make getname more nasty, do another passwd file
958 ** scan, or compile the UID of "UUCP" into the code,
959 ** all of which are reprehensible.
961 ** Assuming all of these fail, we figure out something
965 ** from -- the person we would like to believe this message
966 ** is from, as specified on the command line.
967 ** e -- the envelope in which we would like the sender set.
968 ** delimptr -- if non-NULL, set to the location of the
969 ** trailing delimiter.
970 ** delimchar -- the character that will delimit the sender
972 ** internal -- set if this address is coming from an internal
973 ** source such as an owner alias.
979 ** sets sendmail's notion of who the from person is.
983 setsender(from, e, delimptr, delimchar, internal)
985 register ENVELOPE *e;
991 char *realname = NULL;
993 char buf[MAXNAME + 2];
994 char pvpbuf[PSBUFSIZE];
995 extern char *FullName;
998 sm_dprintf("setsender(%s)\n", from == NULL ? "" : from);
1000 /* may be set from earlier calls */
1001 macdefine(&e->e_macro, A_PERM, 'x', "");
1004 ** Figure out the real user executing us.
1005 ** Username can return errno != 0 on non-errors.
1008 if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP ||
1009 OpMode == MD_ARPAFTP || OpMode == MD_DAEMON)
1011 if (realname == NULL || realname[0] == '\0')
1012 realname = username();
1014 if (ConfigLevel < 2)
1017 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e s");
1019 /* preset state for then clause in case from == NULL */
1020 e->e_from.q_state = QS_BADADDR;
1021 e->e_from.q_flags = 0;
1023 parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR,
1024 delimchar, delimptr, e, false) == NULL ||
1025 QS_IS_BADADDR(e->e_from.q_state) ||
1026 e->e_from.q_mailer == ProgMailer ||
1027 e->e_from.q_mailer == FileMailer ||
1028 e->e_from.q_mailer == InclMailer)
1030 /* log garbage addresses for traceback */
1031 if (from != NULL && LogLevel > 2)
1034 char ebuf[MAXNAME * 2 + 2];
1036 p = macvalue('_', e);
1039 char *host = RealHostName;
1043 (void) sm_snprintf(ebuf, sizeof(ebuf),
1044 "%.*s@%.*s", MAXNAME,
1045 realname, MAXNAME, host);
1048 sm_syslog(LOG_NOTICE, e->e_id,
1049 "setsender: %s: invalid or unparsable, received from %s",
1050 shortenstring(from, 83), p);
1054 if (!QS_IS_BADADDR(e->e_from.q_state))
1056 /* it was a bogus mailer in the from addr */
1057 e->e_status = "5.1.7";
1058 usrerrenh(e->e_status,
1059 "553 Invalid sender address");
1063 if (from == realname ||
1064 parseaddr(from = realname,
1065 &e->e_from, RF_COPYALL|RF_SENDERADDR, ' ',
1066 NULL, e, false) == NULL)
1071 expand("\201n", nbuf, sizeof(nbuf), e);
1072 from = sm_rpool_strdup_x(e->e_rpool, nbuf);
1073 if (parseaddr(from, &e->e_from, RF_COPYALL, ' ',
1074 NULL, e, false) == NULL &&
1075 parseaddr(from = "postmaster", &e->e_from,
1076 RF_COPYALL, ' ', NULL, e, false) == NULL)
1077 syserr("553 5.3.0 setsender: can't even parse postmaster!");
1082 e->e_from.q_state = QS_SENDER;
1085 sm_dprintf("setsender: QS_SENDER ");
1086 printaddr(sm_debug_file(), &e->e_from, false);
1091 if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags))
1095 p = udbsender(e->e_from.q_user, e->e_rpool);
1101 if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
1107 /* if the user already given fullname don't redefine */
1108 if (FullName == NULL)
1109 FullName = macvalue('x', e);
1110 if (FullName != NULL)
1112 if (FullName[0] == '\0')
1115 FullName = newstr(FullName);
1119 if (e->e_from.q_user[0] != '\0' &&
1120 sm_mbdb_lookup(e->e_from.q_user, &user) == EX_OK)
1123 ** Process passwd file entry.
1126 /* extract home directory */
1127 if (*user.mbdb_homedir == '\0')
1128 e->e_from.q_home = NULL;
1129 else if (strcmp(user.mbdb_homedir, "/") == 0)
1130 e->e_from.q_home = "";
1132 e->e_from.q_home = sm_rpool_strdup_x(e->e_rpool,
1134 macdefine(&e->e_macro, A_PERM, 'z', e->e_from.q_home);
1136 /* extract user and group id */
1137 if (user.mbdb_uid != SM_NO_UID)
1139 e->e_from.q_uid = user.mbdb_uid;
1140 e->e_from.q_gid = user.mbdb_gid;
1141 e->e_from.q_flags |= QGOODUID;
1144 /* extract full name from passwd file */
1145 if (FullName == NULL && !internal &&
1146 user.mbdb_fullname[0] != '\0' &&
1147 strcmp(user.mbdb_name, e->e_from.q_user) == 0)
1149 FullName = newstr(user.mbdb_fullname);
1154 e->e_from.q_home = NULL;
1156 if (FullName != NULL && !internal)
1157 macdefine(&e->e_macro, A_TEMP, 'x', FullName);
1159 else if (!internal && OpMode != MD_DAEMON && OpMode != MD_SMTP)
1161 if (e->e_from.q_home == NULL)
1163 e->e_from.q_home = getenv("HOME");
1164 if (e->e_from.q_home != NULL)
1166 if (*e->e_from.q_home == '\0')
1167 e->e_from.q_home = NULL;
1168 else if (strcmp(e->e_from.q_home, "/") == 0)
1172 e->e_from.q_uid = RealUid;
1173 e->e_from.q_gid = RealGid;
1174 e->e_from.q_flags |= QGOODUID;
1178 ** Rewrite the from person to dispose of possible implicit
1179 ** links in the net.
1182 pvp = prescan(from, delimchar, pvpbuf, sizeof(pvpbuf), NULL,
1183 IntTokenTab, false);
1186 /* don't need to give error -- prescan did that already */
1188 sm_syslog(LOG_NOTICE, e->e_id,
1189 "cannot prescan from (%s)",
1190 shortenstring(from, MAXSHORTSTR));
1191 finis(true, true, ExitStat);
1193 (void) REWRITE(pvp, 3, e);
1194 (void) REWRITE(pvp, 1, e);
1195 (void) REWRITE(pvp, 4, e);
1196 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
1198 cataddr(pvp, NULL, bp, sizeof(buf) - 2, '\0', false);
1199 if (*bp == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags))
1201 /* heuristic: route-addr: add angle brackets */
1202 (void) sm_strlcat(bp, ">", sizeof(buf) - 1);
1205 e->e_sender = sm_rpool_strdup_x(e->e_rpool, bp);
1206 macdefine(&e->e_macro, A_PERM, 'f', e->e_sender);
1208 /* save the domain spec if this mailer wants it */
1209 if (e->e_from.q_mailer != NULL &&
1210 bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
1214 /* get rid of any pesky angle brackets */
1215 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e s");
1216 (void) REWRITE(pvp, 3, e);
1217 (void) REWRITE(pvp, 1, e);
1218 (void) REWRITE(pvp, 4, e);
1219 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
1221 /* strip off to the last "@" sign */
1222 for (lastat = NULL; *pvp != NULL; pvp++)
1224 if (strcmp(*pvp, "@") == 0)
1229 e->e_fromdomain = copyplist(lastat, true, e->e_rpool);
1232 sm_dprintf("Saving from domain: ");
1233 printav(sm_debug_file(), e->e_fromdomain);
1239 ** PRINTENVFLAGS -- print envelope flags for debugging
1242 ** e -- the envelope with the flags to be printed.
1251 unsigned long ef_bit;
1254 static struct eflags EnvelopeFlags[] =
1256 { "OLDSTYLE", EF_OLDSTYLE },
1257 { "INQUEUE", EF_INQUEUE },
1258 { "NO_BODY_RETN", EF_NO_BODY_RETN },
1259 { "CLRQUEUE", EF_CLRQUEUE },
1260 { "SENDRECEIPT", EF_SENDRECEIPT },
1261 { "FATALERRS", EF_FATALERRS },
1262 { "DELETE_BCC", EF_DELETE_BCC },
1263 { "RESPONSE", EF_RESPONSE },
1264 { "RESENT", EF_RESENT },
1265 { "VRFYONLY", EF_VRFYONLY },
1266 { "WARNING", EF_WARNING },
1267 { "QUEUERUN", EF_QUEUERUN },
1268 { "GLOBALERRS", EF_GLOBALERRS },
1269 { "PM_NOTIFY", EF_PM_NOTIFY },
1270 { "METOO", EF_METOO },
1271 { "LOGSENDER", EF_LOGSENDER },
1272 { "NORECEIPT", EF_NORECEIPT },
1273 { "HAS8BIT", EF_HAS8BIT },
1274 { "NL_NOT_EOL", EF_NL_NOT_EOL },
1275 { "CRLF_NOT_EOL", EF_CRLF_NOT_EOL },
1276 { "RET_PARAM", EF_RET_PARAM },
1277 { "HAS_DF", EF_HAS_DF },
1278 { "IS_MIME", EF_IS_MIME },
1279 { "DONT_MIME", EF_DONT_MIME },
1280 { "DISCARD", EF_DISCARD },
1281 { "TOOBIG", EF_TOOBIG },
1282 { "SPLIT", EF_SPLIT },
1283 { "UNSAFE", EF_UNSAFE },
1289 register ENVELOPE *e;
1291 register struct eflags *ef;
1294 sm_dprintf("%lx", e->e_flags);
1295 for (ef = EnvelopeFlags; ef->ef_name != NULL; ef++)
1297 if (!bitset(ef->ef_bit, e->e_flags))
1300 sm_dprintf("<%s", ef->ef_name);
1302 sm_dprintf(",%s", ef->ef_name);