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.302 2006/11/10 23:12:52 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();
110 e->e_msgpriority = parent->e_msgsize;
111 if (parent->e_quarmsg == NULL)
114 macdefine(&e->e_macro, A_PERM,
115 macid("{quarantine}"), "");
119 e->e_quarmsg = sm_rpool_strdup_x(rpool,
121 macdefine(&e->e_macro, A_PERM,
122 macid("{quarantine}"), e->e_quarmsg);
125 e->e_puthdr = putheader;
126 e->e_putbody = putbody;
127 if (CurEnv->e_xfp != NULL)
128 (void) sm_io_flush(CurEnv->e_xfp, SM_TIME_DEFAULT);
129 if (sendmode != DM_NOTSET)
130 e->e_sendmode = sendmode;
135 /* values for msg_timeout, see also IS_* below for usage (bit layout) */
136 #define MSG_T_O 0x01 /* normal timeout */
137 #define MSG_T_O_NOW 0x02 /* NOW timeout */
138 #define MSG_NOT_BY 0x04 /* Deliver-By time exceeded, mode R */
139 #define MSG_WARN 0x10 /* normal queue warning */
140 #define MSG_WARN_BY 0x20 /* Deliver-By time exceeded, mode N */
142 #define IS_MSG_ERR(x) (((x) & 0x0f) != 0) /* return an error */
144 /* immediate return */
145 #define IS_IMM_RET(x) (((x) & (MSG_T_O_NOW|MSG_NOT_BY)) != 0)
146 #define IS_MSG_WARN(x) (((x) & 0xf0) != 0) /* return a warning */
149 ** DROPENVELOPE -- deallocate an envelope.
152 ** e -- the envelope to deallocate.
153 ** fulldrop -- if set, do return receipts.
154 ** split -- if true, split by recipient if message is queued up
160 ** housekeeping necessary to dispose of an envelope.
161 ** Unlocks this queue file.
165 dropenvelope(e, fulldrop, split)
166 register ENVELOPE *e;
171 bool queueit = false;
173 bool failure_return = false;
174 bool delay_return = false;
175 bool success_return = false;
176 bool pmnotify = bitset(EF_PM_NOTIFY, e->e_flags);
185 sm_dprintf("dropenvelope %p: id=", e);
186 xputs(sm_debug_file(), e->e_id);
187 sm_dprintf(", flags=");
191 sm_dprintf("sendq=");
192 printaddr(sm_debug_file(), e->e_sendqueue, true);
197 sm_syslog(LOG_DEBUG, id,
198 "dropenvelope, e_flags=0x%lx, OpMode=%c, pid=%d",
199 e->e_flags, OpMode, (int) CurrentPid);
201 /* we must have an id to remove disk files */
205 /* if verify-only mode, we can skip most of this */
206 if (OpMode == MD_VERIFY)
209 if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
211 e->e_flags &= ~EF_LOGSENDER;
213 /* post statistics */
217 ** Extract state information from dregs of send list.
221 if (now >= e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass])
222 msg_timeout = MSG_T_O;
223 if (IS_DLVR_RETURN(e) && e->e_deliver_by > 0 &&
224 now >= e->e_ctime + e->e_deliver_by &&
225 !bitset(EF_RESPONSE, e->e_flags))
227 msg_timeout = MSG_NOT_BY;
228 e->e_flags |= EF_FATALERRS|EF_CLRQUEUE;
230 else if (TimeOuts.to_q_return[e->e_timeoutclass] == NOW &&
231 !bitset(EF_RESPONSE, e->e_flags))
233 msg_timeout = MSG_T_O_NOW;
234 e->e_flags |= EF_FATALERRS|EF_CLRQUEUE;
237 e->e_flags &= ~EF_QUEUERUN;
238 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
240 if (QS_IS_UNDELIVERED(q->q_state))
243 /* see if a notification is needed */
244 if (bitset(QPINGONFAILURE, q->q_flags) &&
245 ((IS_MSG_ERR(msg_timeout) &&
246 QS_IS_UNDELIVERED(q->q_state)) ||
247 QS_IS_BADADDR(q->q_state) ||
248 IS_IMM_RET(msg_timeout)))
250 failure_return = true;
251 if (!done && q->q_owner == NULL &&
252 !emptyaddr(&e->e_from))
254 (void) sendtolist(e->e_from.q_paddr, NULLADDR,
255 &e->e_errorqueue, 0, e);
259 else if ((bitset(QPINGONSUCCESS, q->q_flags) &&
260 ((QS_IS_SENT(q->q_state) &&
261 bitnset(M_LOCALMAILER, q->q_mailer->m_flags)) ||
262 bitset(QRELAYED|QEXPANDED|QDELIVERED, q->q_flags))) ||
263 bitset(QBYTRACE, q->q_flags) ||
264 bitset(QBYNRELAY, q->q_flags))
266 success_return = true;
271 e->e_flags |= EF_NO_BODY_RETN;
274 ** See if the message timed out.
279 /* nothing to do */ ;
280 else if (IS_MSG_ERR(msg_timeout))
284 if (msg_timeout == MSG_NOT_BY)
286 (void) sm_snprintf(buf, sizeof(buf),
287 "delivery time expired %lds",
292 (void) sm_snprintf(buf, sizeof(buf),
293 "Cannot send message for %s",
294 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
298 /* don't free, allocated from e_rpool */
299 e->e_message = sm_rpool_strdup_x(e->e_rpool, buf);
301 e->e_flags |= EF_CLRQUEUE;
303 if (msg_timeout == MSG_NOT_BY)
305 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
306 "Delivery time (%lds) expired\n",
310 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
311 "Message could not be delivered for %s\n",
312 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
314 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
315 "Message will be deleted from queue\n");
316 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
318 if (QS_IS_UNDELIVERED(q->q_state))
320 q->q_state = QS_BADADDR;
321 if (msg_timeout == MSG_NOT_BY)
322 q->q_status = "5.4.7";
324 q->q_status = "4.4.7";
330 if (TimeOuts.to_q_warning[e->e_timeoutclass] > 0 &&
332 TimeOuts.to_q_warning[e->e_timeoutclass])
333 msg_timeout = MSG_WARN;
334 else if (IS_DLVR_NOTIFY(e) &&
335 e->e_deliver_by > 0 &&
336 now >= e->e_ctime + e->e_deliver_by)
337 msg_timeout = MSG_WARN_BY;
339 if (IS_MSG_WARN(msg_timeout))
341 if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
343 e->e_from.q_paddr != NULL &&
344 strcmp(e->e_from.q_paddr, "<>") != 0 &&
345 sm_strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 &&
346 (strlen(e->e_from.q_paddr) <= 8 ||
347 sm_strcasecmp(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8],
350 for (q = e->e_sendqueue; q != NULL;
353 if (QS_IS_UNDELIVERED(q->q_state)
354 #if _FFR_NODELAYDSN_ON_HOLD
356 q->q_mailer->m_flags)
357 #endif /* _FFR_NODELAYDSN_ON_HOLD */
362 (bitset(QPINGONDELAY,
368 q->q_flags |= QBYNDELAY;
371 if (bitset(QPINGONDELAY,
374 q->q_flags |= QDELAYED;
382 if (msg_timeout == MSG_WARN_BY)
384 (void) sm_snprintf(buf, sizeof(buf),
385 "Warning: Delivery time (%lds) exceeded",
389 (void) sm_snprintf(buf, sizeof(buf),
390 "Warning: could not send message for past %s",
391 pintvl(TimeOuts.to_q_warning[e->e_timeoutclass],
394 /* don't free, allocated from e_rpool */
395 e->e_message = sm_rpool_strdup_x(e->e_rpool,
398 e->e_flags |= EF_WARNING;
400 if (msg_timeout == MSG_WARN_BY)
402 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
403 "Warning: Delivery time (%lds) exceeded\n",
407 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
408 "Warning: message still undelivered after %s\n",
409 pintvl(TimeOuts.to_q_warning[e->e_timeoutclass],
411 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
412 "Will keep trying until message is %s old\n",
413 pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
419 sm_dprintf("failure_return=%d delay_return=%d success_return=%d queueit=%d\n",
420 failure_return, delay_return, success_return, queueit);
423 ** If we had some fatal error, but no addresses are marked as
424 ** bad, mark them _all_ as bad.
427 if (bitset(EF_FATALERRS, e->e_flags) && !failure_return)
429 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
431 if ((QS_IS_OK(q->q_state) ||
432 QS_IS_VERIFIED(q->q_state)) &&
433 bitset(QPINGONFAILURE, q->q_flags))
435 failure_return = true;
436 q->q_state = QS_BADADDR;
442 ** Send back return receipts as requested.
445 if (success_return && !failure_return && !delay_return && fulldrop &&
446 !bitset(PRIV_NORECEIPTS, PrivacyFlags) &&
447 strcmp(e->e_from.q_paddr, "<>") != 0)
449 auto ADDRESS *rlist = NULL;
452 sm_dprintf("dropenvelope(%s): sending return receipt\n",
454 e->e_flags |= EF_SENDRECEIPT;
455 (void) sendtolist(e->e_from.q_paddr, NULLADDR, &rlist, 0, e);
456 (void) returntosender("Return receipt", rlist, RTSF_NO_BODY, e);
458 e->e_flags &= ~EF_SENDRECEIPT;
461 ** Arrange to send error messages if there are fatal errors.
464 if ((failure_return || delay_return) && e->e_errormode != EM_QUIET)
467 sm_dprintf("dropenvelope(%s): saving mail\n", id);
468 panic = savemail(e, !bitset(EF_NO_BODY_RETN, e->e_flags));
472 ** Arrange to send warning messages to postmaster as requested.
475 if ((failure_return || pmnotify) &&
476 PostMasterCopy != NULL &&
477 !bitset(EF_RESPONSE, e->e_flags) &&
480 auto ADDRESS *rlist = NULL;
485 expand(PostMasterCopy, pcopy, sizeof(pcopy), e);
488 sm_dprintf("dropenvelope(%s): sending postmaster copy to %s\n",
490 (void) sendtolist(pcopy, NULLADDR, &rlist, 0, e);
493 (void) sendtolist("postmaster", NULLADDR,
495 (void) returntosender(e->e_message, rlist,
496 RTSF_PM_BOUNCE|RTSF_NO_BODY, e);
500 ** Instantiate or deinstantiate the queue.
505 sm_dprintf("dropenvelope(%s): at simpledrop, queueit=%d\n",
507 if (!queueit || bitset(EF_CLRQUEUE, e->e_flags))
511 sm_dprintf("\n===== Dropping queue files for %s... queueit=%d, e_flags=",
517 if (e->e_dfp != NULL)
519 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
522 (void) xunlink(queuename(e, DATAFL_LETTER));
524 if (panic && QueueMode == QM_LOST)
527 ** leave the Qf file behind as
528 ** the delivery attempt failed.
534 if (xunlink(queuename(e, ANYQFL_LETTER)) == 0)
536 /* add to available space in filesystem */
537 updfs(e, -1, panic ? 0 : -1, "dropenvelope");
540 if (e->e_ntries > 0 && LogLevel > 9)
541 sm_syslog(LOG_INFO, id, "done; delay=%s, ntries=%d",
542 pintvl(curtime() - e->e_ctime, true),
545 else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
548 queueup(e, false, true);
555 ** Save old sibling and set it to NULL to avoid
556 ** queueing up the same envelopes again.
557 ** This requires that envelopes in that list have
558 ** been take care of before (or at some other place).
561 oldsib = e->e_sibling;
563 if (!split_by_recipient(e) &&
564 bitset(EF_FATALERRS, e->e_flags))
566 syserr("!dropenvelope(%s): cannot commit data file %s, uid=%d",
567 e->e_id, queuename(e, DATAFL_LETTER),
570 for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling)
571 queueup(ee, false, true);
572 queueup(e, false, true);
575 for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling)
577 /* now unlock the job */
579 sm_dprintf("dropenvelope(%s): unlocking job\n",
584 /* this envelope is marked unused */
585 if (ee->e_dfp != NULL)
587 (void) sm_io_close(ee->e_dfp,
592 ee->e_flags &= ~EF_HAS_DF;
594 e->e_sibling = oldsib;
598 /* now unlock the job */
600 sm_dprintf("dropenvelope(%s): unlocking job\n", id);
604 /* make sure that this envelope is marked unused */
605 if (e->e_dfp != NULL)
607 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
611 e->e_flags &= ~EF_HAS_DF;
614 ** CLEARENVELOPE -- clear an envelope without unlocking
616 ** This is normally used by a child process to get a clean
617 ** envelope without disturbing the parent.
620 ** e -- the envelope to clear.
621 ** fullclear - if set, the current envelope is total
622 ** garbage and should be ignored; otherwise,
623 ** release any resources it may indicate.
624 ** rpool -- either NULL, or a pointer to a resource pool
625 ** from which envelope memory is allocated, and
626 ** to which envelope resources are attached.
632 ** Closes files associated with the envelope.
633 ** Marks the envelope as unallocated.
637 clearenvelope(e, fullclear, rpool)
638 register ENVELOPE *e;
644 extern ENVELOPE BlankEnvelope;
649 /* clear out any file information */
650 if (e->e_xfp != NULL)
651 (void) sm_io_close(e->e_xfp, SM_TIME_DEFAULT);
652 if (e->e_dfp != NULL)
653 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
654 e->e_xfp = e->e_dfp = NULL;
658 ** Copy BlankEnvelope into *e.
659 ** It is not safe to simply copy pointers to strings;
660 ** the strings themselves must be copied (or set to NULL).
661 ** The problem is that when we assign a new string value to
662 ** a member of BlankEnvelope, we free the old string.
663 ** We did not need to do this copying in sendmail 8.11 :-(
664 ** and it is a potential performance hit. Reference counted
665 ** strings are one way out.
670 e->e_qfletter = '\0';
672 macdefine(&e->e_macro, A_PERM, macid("{quarantine}"), "");
675 ** Copy the macro table.
676 ** We might be able to avoid this by zeroing the macro table
677 ** and always searching BlankEnvelope.e_macro after e->e_macro
681 for (p = &e->e_macro.mac_table[0];
682 p <= &e->e_macro.mac_table[MAXMACROID];
686 *p = sm_rpool_strdup_x(rpool, *p);
690 ** XXX There are many strings in the envelope structure
691 ** XXX that we are not attempting to copy here.
692 ** XXX Investigate this further.
696 e->e_macro.mac_rpool = rpool;
698 set_delivery_mode(SM_DELIVER, e);
699 bh = BlankEnvelope.e_header;
703 *nhp = (HDR *) sm_rpool_malloc_x(rpool, sizeof(*bh));
704 memmove((char *) *nhp, (char *) bh, sizeof(*bh));
706 nhp = &(*nhp)->h_link;
710 ** INITSYS -- initialize instantiation of system
712 ** In Daemon mode, this is done in the child.
715 ** e -- the envelope to use.
721 ** Initializes the system macros, some global variables,
722 ** etc. In particular, the current time in various
728 register ENVELOPE *e;
732 static char ybuf[60]; /* holds tty id */
734 extern char *ttyname();
738 ** Give this envelope a reality.
739 ** I.e., an id, a transcript, and a creation time.
740 ** We don't select the queue until all of the recipients are known.
744 e->e_ctime = curtime();
745 e->e_qfletter = '\0';
748 ** Set OutChannel to something useful if stdout isn't it.
749 ** This arranges that any extra stuff the mailer produces
750 ** gets sent back to the user on error (because it is
751 ** tucked away in the transcript).
754 if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) &&
756 OutChannel = e->e_xfp;
759 ** Set up some basic system macros.
763 (void) sm_snprintf(buf, sizeof(buf), "%d", (int) CurrentPid);
764 macdefine(&e->e_macro, A_TEMP, 'p', buf);
767 (void) sm_snprintf(buf, sizeof(buf), "%d", e->e_hopcount);
768 macdefine(&e->e_macro, A_TEMP, 'c', buf);
770 /* time as integer, unix time, arpa time */
778 if (macvalue('y', e) == NULL)
783 if (strrchr(p, '/') != NULL)
784 p = strrchr(p, '/') + 1;
785 (void) sm_strlcpy(ybuf, sizeof(ybuf), p);
786 macdefine(&e->e_macro, A_PERM, 'y', ybuf);
792 ** SETTIME -- set the current time.
795 ** e -- the envelope in which the macros should be set.
801 ** Sets the various time macros -- $a, $b, $d, $t.
806 register ENVELOPE *e;
811 register struct tm *tm;
814 (void) sm_snprintf(buf, sizeof(buf), "%ld", (long) now);
815 macdefine(&e->e_macro, A_TEMP, macid("{time}"), buf);
817 (void) sm_snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d",
818 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
819 tm->tm_hour, tm->tm_min);
820 macdefine(&e->e_macro, A_TEMP, 't', buf);
821 (void) sm_strlcpy(buf, ctime(&now), sizeof(buf));
822 p = strchr(buf, '\n');
825 macdefine(&e->e_macro, A_TEMP, 'd', buf);
826 macdefine(&e->e_macro, A_TEMP, 'b', arpadate(buf));
827 if (macvalue('a', e) == NULL)
828 macdefine(&e->e_macro, A_PERM, 'a', macvalue('b', e));
831 ** OPENXSCRIPT -- Open transcript file
833 ** Creates a transcript file for possible eventual mailing or
837 ** e -- the envelope to create the transcript in/for.
843 ** Creates the transcript file.
848 #endif /* ! O_APPEND */
852 register ENVELOPE *e;
856 if (e->e_xfp != NULL)
860 if (e->e_lockfp == NULL && bitset(EF_INQUEUE, e->e_flags))
861 syserr("openxscript: job not locked");
864 p = queuename(e, XSCRPT_LETTER);
865 e->e_xfp = bfopen(p, FileMode, XscriptFileBufferSize,
866 SFF_NOTEXCL|SFF_OPENASROOT);
868 if (e->e_xfp == NULL)
870 syserr("Can't create transcript file %s", p);
871 e->e_xfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT,
872 SM_PATH_DEVNULL, SM_IO_RDWR, NULL);
873 if (e->e_xfp == NULL)
874 syserr("!Can't open %s", SM_PATH_DEVNULL);
876 (void) sm_io_setvbuf(e->e_xfp, SM_TIME_DEFAULT, NULL, SM_IO_LBF, 0);
879 sm_dprintf("openxscript(%s):\n ", p);
880 dumpfd(sm_io_getinfo(e->e_xfp, SM_IO_WHAT_FD, NULL), true,
885 ** CLOSEXSCRIPT -- close the transcript file.
888 ** e -- the envelope containing the transcript to close.
899 register ENVELOPE *e;
901 if (e->e_xfp == NULL)
904 if (e->e_lockfp == NULL)
905 syserr("closexscript: job not locked");
907 (void) sm_io_close(e->e_xfp, SM_TIME_DEFAULT);
911 ** SETSENDER -- set the person who this message is from
913 ** Under certain circumstances allow the user to say who
914 ** s/he is (using -f or -r). These are:
915 ** 1. The user's uid is zero (root).
916 ** 2. The user's login name is in an approved list (typically
917 ** from a network server).
918 ** 3. The address the user is trying to claim has a
919 ** "!" character in it (since #2 doesn't do it for
920 ** us if we are dialing out for UUCP).
921 ** A better check to replace #3 would be if the
922 ** effective uid is "UUCP" -- this would require me
923 ** to rewrite getpwent to "grab" uucp as it went by,
924 ** make getname more nasty, do another passwd file
925 ** scan, or compile the UID of "UUCP" into the code,
926 ** all of which are reprehensible.
928 ** Assuming all of these fail, we figure out something
932 ** from -- the person we would like to believe this message
933 ** is from, as specified on the command line.
934 ** e -- the envelope in which we would like the sender set.
935 ** delimptr -- if non-NULL, set to the location of the
936 ** trailing delimiter.
937 ** delimchar -- the character that will delimit the sender
939 ** internal -- set if this address is coming from an internal
940 ** source such as an owner alias.
946 ** sets sendmail's notion of who the from person is.
950 setsender(from, e, delimptr, delimchar, internal)
952 register ENVELOPE *e;
958 char *realname = NULL;
960 char buf[MAXNAME + 2];
961 char pvpbuf[PSBUFSIZE];
962 extern char *FullName;
965 sm_dprintf("setsender(%s)\n", from == NULL ? "" : from);
967 /* may be set from earlier calls */
968 macdefine(&e->e_macro, A_PERM, 'x', "");
971 ** Figure out the real user executing us.
972 ** Username can return errno != 0 on non-errors.
975 if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP ||
976 OpMode == MD_ARPAFTP || OpMode == MD_DAEMON)
978 if (realname == NULL || realname[0] == '\0')
979 realname = username();
984 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e s");
986 /* preset state for then clause in case from == NULL */
987 e->e_from.q_state = QS_BADADDR;
988 e->e_from.q_flags = 0;
990 parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR,
991 delimchar, delimptr, e, false) == NULL ||
992 QS_IS_BADADDR(e->e_from.q_state) ||
993 e->e_from.q_mailer == ProgMailer ||
994 e->e_from.q_mailer == FileMailer ||
995 e->e_from.q_mailer == InclMailer)
997 /* log garbage addresses for traceback */
998 if (from != NULL && LogLevel > 2)
1001 char ebuf[MAXNAME * 2 + 2];
1003 p = macvalue('_', e);
1006 char *host = RealHostName;
1010 (void) sm_snprintf(ebuf, sizeof(ebuf),
1011 "%.*s@%.*s", MAXNAME,
1012 realname, MAXNAME, host);
1015 sm_syslog(LOG_NOTICE, e->e_id,
1016 "setsender: %s: invalid or unparsable, received from %s",
1017 shortenstring(from, 83), p);
1021 if (!QS_IS_BADADDR(e->e_from.q_state))
1023 /* it was a bogus mailer in the from addr */
1024 e->e_status = "5.1.7";
1025 usrerrenh(e->e_status,
1026 "553 Invalid sender address");
1030 if (from == realname ||
1031 parseaddr(from = realname,
1032 &e->e_from, RF_COPYALL|RF_SENDERADDR, ' ',
1033 NULL, e, false) == NULL)
1038 expand("\201n", nbuf, sizeof(nbuf), e);
1039 from = sm_rpool_strdup_x(e->e_rpool, nbuf);
1040 if (parseaddr(from, &e->e_from, RF_COPYALL, ' ',
1041 NULL, e, false) == NULL &&
1042 parseaddr(from = "postmaster", &e->e_from,
1043 RF_COPYALL, ' ', NULL, e, false) == NULL)
1044 syserr("553 5.3.0 setsender: can't even parse postmaster!");
1049 e->e_from.q_state = QS_SENDER;
1052 sm_dprintf("setsender: QS_SENDER ");
1053 printaddr(sm_debug_file(), &e->e_from, false);
1058 if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags))
1062 p = udbsender(e->e_from.q_user, e->e_rpool);
1068 if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
1074 /* if the user already given fullname don't redefine */
1075 if (FullName == NULL)
1076 FullName = macvalue('x', e);
1077 if (FullName != NULL)
1079 if (FullName[0] == '\0')
1082 FullName = newstr(FullName);
1086 if (e->e_from.q_user[0] != '\0' &&
1087 sm_mbdb_lookup(e->e_from.q_user, &user) == EX_OK)
1090 ** Process passwd file entry.
1093 /* extract home directory */
1094 if (*user.mbdb_homedir == '\0')
1095 e->e_from.q_home = NULL;
1096 else if (strcmp(user.mbdb_homedir, "/") == 0)
1097 e->e_from.q_home = "";
1099 e->e_from.q_home = sm_rpool_strdup_x(e->e_rpool,
1101 macdefine(&e->e_macro, A_PERM, 'z', e->e_from.q_home);
1103 /* extract user and group id */
1104 if (user.mbdb_uid != SM_NO_UID)
1106 e->e_from.q_uid = user.mbdb_uid;
1107 e->e_from.q_gid = user.mbdb_gid;
1108 e->e_from.q_flags |= QGOODUID;
1111 /* extract full name from passwd file */
1112 if (FullName == NULL && !internal &&
1113 user.mbdb_fullname[0] != '\0' &&
1114 strcmp(user.mbdb_name, e->e_from.q_user) == 0)
1116 FullName = newstr(user.mbdb_fullname);
1121 e->e_from.q_home = NULL;
1123 if (FullName != NULL && !internal)
1124 macdefine(&e->e_macro, A_TEMP, 'x', FullName);
1126 else if (!internal && OpMode != MD_DAEMON && OpMode != MD_SMTP)
1128 if (e->e_from.q_home == NULL)
1130 e->e_from.q_home = getenv("HOME");
1131 if (e->e_from.q_home != NULL)
1133 if (*e->e_from.q_home == '\0')
1134 e->e_from.q_home = NULL;
1135 else if (strcmp(e->e_from.q_home, "/") == 0)
1139 e->e_from.q_uid = RealUid;
1140 e->e_from.q_gid = RealGid;
1141 e->e_from.q_flags |= QGOODUID;
1145 ** Rewrite the from person to dispose of possible implicit
1146 ** links in the net.
1149 pvp = prescan(from, delimchar, pvpbuf, sizeof(pvpbuf), NULL,
1150 IntTokenTab, false);
1153 /* don't need to give error -- prescan did that already */
1155 sm_syslog(LOG_NOTICE, e->e_id,
1156 "cannot prescan from (%s)",
1157 shortenstring(from, MAXSHORTSTR));
1158 finis(true, true, ExitStat);
1160 (void) REWRITE(pvp, 3, e);
1161 (void) REWRITE(pvp, 1, e);
1162 (void) REWRITE(pvp, 4, e);
1163 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
1165 cataddr(pvp, NULL, bp, sizeof(buf) - 2, '\0', false);
1166 if (*bp == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags))
1168 /* heuristic: route-addr: add angle brackets */
1169 (void) sm_strlcat(bp, ">", sizeof(buf) - 1);
1172 e->e_sender = sm_rpool_strdup_x(e->e_rpool, bp);
1173 macdefine(&e->e_macro, A_PERM, 'f', e->e_sender);
1175 /* save the domain spec if this mailer wants it */
1176 if (e->e_from.q_mailer != NULL &&
1177 bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
1181 /* get rid of any pesky angle brackets */
1182 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e s");
1183 (void) REWRITE(pvp, 3, e);
1184 (void) REWRITE(pvp, 1, e);
1185 (void) REWRITE(pvp, 4, e);
1186 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
1188 /* strip off to the last "@" sign */
1189 for (lastat = NULL; *pvp != NULL; pvp++)
1191 if (strcmp(*pvp, "@") == 0)
1196 e->e_fromdomain = copyplist(lastat, true, e->e_rpool);
1199 sm_dprintf("Saving from domain: ");
1200 printav(sm_debug_file(), e->e_fromdomain);
1206 ** PRINTENVFLAGS -- print envelope flags for debugging
1209 ** e -- the envelope with the flags to be printed.
1218 unsigned long ef_bit;
1221 static struct eflags EnvelopeFlags[] =
1223 { "OLDSTYLE", EF_OLDSTYLE },
1224 { "INQUEUE", EF_INQUEUE },
1225 { "NO_BODY_RETN", EF_NO_BODY_RETN },
1226 { "CLRQUEUE", EF_CLRQUEUE },
1227 { "SENDRECEIPT", EF_SENDRECEIPT },
1228 { "FATALERRS", EF_FATALERRS },
1229 { "DELETE_BCC", EF_DELETE_BCC },
1230 { "RESPONSE", EF_RESPONSE },
1231 { "RESENT", EF_RESENT },
1232 { "VRFYONLY", EF_VRFYONLY },
1233 { "WARNING", EF_WARNING },
1234 { "QUEUERUN", EF_QUEUERUN },
1235 { "GLOBALERRS", EF_GLOBALERRS },
1236 { "PM_NOTIFY", EF_PM_NOTIFY },
1237 { "METOO", EF_METOO },
1238 { "LOGSENDER", EF_LOGSENDER },
1239 { "NORECEIPT", EF_NORECEIPT },
1240 { "HAS8BIT", EF_HAS8BIT },
1241 { "NL_NOT_EOL", EF_NL_NOT_EOL },
1242 { "CRLF_NOT_EOL", EF_CRLF_NOT_EOL },
1243 { "RET_PARAM", EF_RET_PARAM },
1244 { "HAS_DF", EF_HAS_DF },
1245 { "IS_MIME", EF_IS_MIME },
1246 { "DONT_MIME", EF_DONT_MIME },
1247 { "DISCARD", EF_DISCARD },
1248 { "TOOBIG", EF_TOOBIG },
1249 { "SPLIT", EF_SPLIT },
1250 { "UNSAFE", EF_UNSAFE },
1256 register ENVELOPE *e;
1258 register struct eflags *ef;
1261 sm_dprintf("%lx", e->e_flags);
1262 for (ef = EnvelopeFlags; ef->ef_name != NULL; ef++)
1264 if (!bitset(ef->ef_bit, e->e_flags))
1267 sm_dprintf("<%s", ef->ef_name);
1269 sm_dprintf(",%s", ef->ef_name);