2 * Copyright (c) 1998-2003 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.294 2005/02/16 23:38:51 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
;
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.
89 clearenvelope(e
, true, rpool
);
91 memmove((char *) &e
->e_from
,
92 (char *) &NullAddress
,
95 memmove((char *) &e
->e_from
,
96 (char *) &CurEnv
->e_from
,
100 e
->e_ctime
= curtime();
103 e
->e_msgpriority
= parent
->e_msgsize
;
104 if (parent
->e_quarmsg
== NULL
)
107 macdefine(&e
->e_macro
, A_PERM
,
108 macid("{quarantine}"), "");
112 e
->e_quarmsg
= sm_rpool_strdup_x(rpool
,
114 macdefine(&e
->e_macro
, A_PERM
,
115 macid("{quarantine}"), e
->e_quarmsg
);
118 e
->e_puthdr
= putheader
;
119 e
->e_putbody
= putbody
;
120 if (CurEnv
->e_xfp
!= NULL
)
121 (void) sm_io_flush(CurEnv
->e_xfp
, SM_TIME_DEFAULT
);
126 /* values for msg_timeout, see also IS_* below for usage (bit layout) */
127 #define MSG_T_O 0x01 /* normal timeout */
128 #define MSG_T_O_NOW 0x02 /* NOW timeout */
129 #define MSG_NOT_BY 0x04 /* Deliver-By time exceeded, mode R */
130 #define MSG_WARN 0x10 /* normal queue warning */
131 #define MSG_WARN_BY 0x20 /* Deliver-By time exceeded, mode N */
133 #define IS_MSG_ERR(x) (((x) & 0x0f) != 0) /* return an error */
135 /* immediate return */
136 #define IS_IMM_RET(x) (((x) & (MSG_T_O_NOW|MSG_NOT_BY)) != 0)
137 #define IS_MSG_WARN(x) (((x) & 0xf0) != 0) /* return a warning */
140 ** DROPENVELOPE -- deallocate an envelope.
143 ** e -- the envelope to deallocate.
144 ** fulldrop -- if set, do return receipts.
145 ** split -- if true, split by recipient if message is queued up
151 ** housekeeping necessary to dispose of an envelope.
152 ** Unlocks this queue file.
156 dropenvelope(e
, fulldrop
, split
)
157 register ENVELOPE
*e
;
162 bool queueit
= false;
164 bool failure_return
= false;
165 bool delay_return
= false;
166 bool success_return
= false;
167 bool pmnotify
= bitset(EF_PM_NOTIFY
, e
->e_flags
);
176 sm_dprintf("dropenvelope %p: id=", e
);
177 xputs(sm_debug_file(), e
->e_id
);
178 sm_dprintf(", flags=");
182 sm_dprintf("sendq=");
183 printaddr(sm_debug_file(), e
->e_sendqueue
, true);
188 sm_syslog(LOG_DEBUG
, id
,
189 "dropenvelope, e_flags=0x%lx, OpMode=%c, pid=%d",
190 e
->e_flags
, OpMode
, (int) CurrentPid
);
192 /* we must have an id to remove disk files */
196 /* if verify-only mode, we can skip most of this */
197 if (OpMode
== MD_VERIFY
)
200 if (LogLevel
> 4 && bitset(EF_LOGSENDER
, e
->e_flags
))
202 e
->e_flags
&= ~EF_LOGSENDER
;
204 /* post statistics */
208 ** Extract state information from dregs of send list.
212 if (now
>= e
->e_ctime
+ TimeOuts
.to_q_return
[e
->e_timeoutclass
])
213 msg_timeout
= MSG_T_O
;
214 if (IS_DLVR_RETURN(e
) && e
->e_deliver_by
> 0 &&
215 now
>= e
->e_ctime
+ e
->e_deliver_by
&&
216 !bitset(EF_RESPONSE
, e
->e_flags
))
218 msg_timeout
= MSG_NOT_BY
;
219 e
->e_flags
|= EF_FATALERRS
|EF_CLRQUEUE
;
221 else if (TimeOuts
.to_q_return
[e
->e_timeoutclass
] == NOW
&&
222 !bitset(EF_RESPONSE
, e
->e_flags
))
224 msg_timeout
= MSG_T_O_NOW
;
225 e
->e_flags
|= EF_FATALERRS
|EF_CLRQUEUE
;
228 e
->e_flags
&= ~EF_QUEUERUN
;
229 for (q
= e
->e_sendqueue
; q
!= NULL
; q
= q
->q_next
)
231 if (QS_IS_UNDELIVERED(q
->q_state
))
234 /* see if a notification is needed */
235 if (bitset(QPINGONFAILURE
, q
->q_flags
) &&
236 ((IS_MSG_ERR(msg_timeout
) &&
237 QS_IS_UNDELIVERED(q
->q_state
)) ||
238 QS_IS_BADADDR(q
->q_state
) ||
239 IS_IMM_RET(msg_timeout
)))
241 failure_return
= true;
242 if (!done
&& q
->q_owner
== NULL
&&
243 !emptyaddr(&e
->e_from
))
245 (void) sendtolist(e
->e_from
.q_paddr
, NULLADDR
,
246 &e
->e_errorqueue
, 0, e
);
250 else if ((bitset(QPINGONSUCCESS
, q
->q_flags
) &&
251 ((QS_IS_SENT(q
->q_state
) &&
252 bitnset(M_LOCALMAILER
, q
->q_mailer
->m_flags
)) ||
253 bitset(QRELAYED
|QEXPANDED
|QDELIVERED
, q
->q_flags
))) ||
254 bitset(QBYTRACE
, q
->q_flags
) ||
255 bitset(QBYNRELAY
, q
->q_flags
))
257 success_return
= true;
262 e
->e_flags
|= EF_NO_BODY_RETN
;
265 ** See if the message timed out.
270 /* nothing to do */ ;
271 else if (IS_MSG_ERR(msg_timeout
))
275 if (msg_timeout
== MSG_NOT_BY
)
277 (void) sm_snprintf(buf
, sizeof buf
,
278 "delivery time expired %lds",
283 (void) sm_snprintf(buf
, sizeof buf
,
284 "Cannot send message for %s",
285 pintvl(TimeOuts
.to_q_return
[e
->e_timeoutclass
],
289 /* don't free, allocated from e_rpool */
290 e
->e_message
= sm_rpool_strdup_x(e
->e_rpool
, buf
);
292 e
->e_flags
|= EF_CLRQUEUE
;
294 if (msg_timeout
== MSG_NOT_BY
)
296 (void) sm_io_fprintf(e
->e_xfp
, SM_TIME_DEFAULT
,
297 "Delivery time (%lds) expired\n",
301 (void) sm_io_fprintf(e
->e_xfp
, SM_TIME_DEFAULT
,
302 "Message could not be delivered for %s\n",
303 pintvl(TimeOuts
.to_q_return
[e
->e_timeoutclass
],
305 (void) sm_io_fprintf(e
->e_xfp
, SM_TIME_DEFAULT
,
306 "Message will be deleted from queue\n");
307 for (q
= e
->e_sendqueue
; q
!= NULL
; q
= q
->q_next
)
309 if (QS_IS_UNDELIVERED(q
->q_state
))
311 q
->q_state
= QS_BADADDR
;
312 if (msg_timeout
== MSG_NOT_BY
)
313 q
->q_status
= "5.4.7";
315 q
->q_status
= "4.4.7";
321 if (TimeOuts
.to_q_warning
[e
->e_timeoutclass
] > 0 &&
323 TimeOuts
.to_q_warning
[e
->e_timeoutclass
])
324 msg_timeout
= MSG_WARN
;
325 else if (IS_DLVR_NOTIFY(e
) &&
326 e
->e_deliver_by
> 0 &&
327 now
>= e
->e_ctime
+ e
->e_deliver_by
)
328 msg_timeout
= MSG_WARN_BY
;
330 if (IS_MSG_WARN(msg_timeout
))
332 if (!bitset(EF_WARNING
|EF_RESPONSE
, e
->e_flags
) &&
334 e
->e_from
.q_paddr
!= NULL
&&
335 strcmp(e
->e_from
.q_paddr
, "<>") != 0 &&
336 sm_strncasecmp(e
->e_from
.q_paddr
, "owner-", 6) != 0 &&
337 (strlen(e
->e_from
.q_paddr
) <= 8 ||
338 sm_strcasecmp(&e
->e_from
.q_paddr
[strlen(e
->e_from
.q_paddr
) - 8],
341 for (q
= e
->e_sendqueue
; q
!= NULL
;
344 if (QS_IS_UNDELIVERED(q
->q_state
)
345 #if _FFR_NODELAYDSN_ON_HOLD
347 q
->q_mailer
->m_flags
)
348 #endif /* _FFR_NODELAYDSN_ON_HOLD */
353 (bitset(QPINGONDELAY
,
359 q
->q_flags
|= QBYNDELAY
;
362 if (bitset(QPINGONDELAY
,
365 q
->q_flags
|= QDELAYED
;
373 if (msg_timeout
== MSG_WARN_BY
)
375 (void) sm_snprintf(buf
, sizeof buf
,
376 "Warning: Delivery time (%lds) exceeded",
380 (void) sm_snprintf(buf
, sizeof buf
,
381 "Warning: could not send message for past %s",
382 pintvl(TimeOuts
.to_q_warning
[e
->e_timeoutclass
],
385 /* don't free, allocated from e_rpool */
386 e
->e_message
= sm_rpool_strdup_x(e
->e_rpool
,
389 e
->e_flags
|= EF_WARNING
;
391 if (msg_timeout
== MSG_WARN_BY
)
393 (void) sm_io_fprintf(e
->e_xfp
, SM_TIME_DEFAULT
,
394 "Warning: Delivery time (%lds) exceeded\n",
398 (void) sm_io_fprintf(e
->e_xfp
, SM_TIME_DEFAULT
,
399 "Warning: message still undelivered after %s\n",
400 pintvl(TimeOuts
.to_q_warning
[e
->e_timeoutclass
],
402 (void) sm_io_fprintf(e
->e_xfp
, SM_TIME_DEFAULT
,
403 "Will keep trying until message is %s old\n",
404 pintvl(TimeOuts
.to_q_return
[e
->e_timeoutclass
],
410 sm_dprintf("failure_return=%d delay_return=%d success_return=%d queueit=%d\n",
411 failure_return
, delay_return
, success_return
, queueit
);
414 ** If we had some fatal error, but no addresses are marked as
415 ** bad, mark them _all_ as bad.
418 if (bitset(EF_FATALERRS
, e
->e_flags
) && !failure_return
)
420 for (q
= e
->e_sendqueue
; q
!= NULL
; q
= q
->q_next
)
422 if ((QS_IS_OK(q
->q_state
) ||
423 QS_IS_VERIFIED(q
->q_state
)) &&
424 bitset(QPINGONFAILURE
, q
->q_flags
))
426 failure_return
= true;
427 q
->q_state
= QS_BADADDR
;
433 ** Send back return receipts as requested.
436 if (success_return
&& !failure_return
&& !delay_return
&& fulldrop
&&
437 !bitset(PRIV_NORECEIPTS
, PrivacyFlags
) &&
438 strcmp(e
->e_from
.q_paddr
, "<>") != 0)
440 auto ADDRESS
*rlist
= NULL
;
443 sm_dprintf("dropenvelope(%s): sending return receipt\n",
445 e
->e_flags
|= EF_SENDRECEIPT
;
446 (void) sendtolist(e
->e_from
.q_paddr
, NULLADDR
, &rlist
, 0, e
);
447 (void) returntosender("Return receipt", rlist
, RTSF_NO_BODY
, e
);
449 e
->e_flags
&= ~EF_SENDRECEIPT
;
452 ** Arrange to send error messages if there are fatal errors.
455 if ((failure_return
|| delay_return
) && e
->e_errormode
!= EM_QUIET
)
458 sm_dprintf("dropenvelope(%s): saving mail\n", id
);
459 panic
= savemail(e
, !bitset(EF_NO_BODY_RETN
, e
->e_flags
));
463 ** Arrange to send warning messages to postmaster as requested.
466 if ((failure_return
|| pmnotify
) &&
467 PostMasterCopy
!= NULL
&&
468 !bitset(EF_RESPONSE
, e
->e_flags
) &&
471 auto ADDRESS
*rlist
= NULL
;
476 expand(PostMasterCopy
, pcopy
, sizeof pcopy
, e
);
479 sm_dprintf("dropenvelope(%s): sending postmaster copy to %s\n",
481 (void) sendtolist(pcopy
, NULLADDR
, &rlist
, 0, e
);
484 (void) sendtolist("postmaster", NULLADDR
,
486 (void) returntosender(e
->e_message
, rlist
,
487 RTSF_PM_BOUNCE
|RTSF_NO_BODY
, e
);
491 ** Instantiate or deinstantiate the queue.
496 sm_dprintf("dropenvelope(%s): at simpledrop, queueit=%d\n",
498 if (!queueit
|| bitset(EF_CLRQUEUE
, e
->e_flags
))
502 sm_dprintf("\n===== Dropping queue files for %s... queueit=%d, e_flags=",
507 (void) xunlink(queuename(e
, DATAFL_LETTER
));
508 if (panic
&& QueueMode
== QM_LOST
)
511 ** leave the Qf file behind as
512 ** the delivery attempt failed.
518 if (xunlink(queuename(e
, ANYQFL_LETTER
)) == 0)
520 /* add to available space in filesystem */
521 updfs(e
, -1, panic
? 0 : -1, "dropenvelope");
524 if (e
->e_ntries
> 0 && LogLevel
> 9)
525 sm_syslog(LOG_INFO
, id
, "done; delay=%s, ntries=%d",
526 pintvl(curtime() - e
->e_ctime
, true),
529 else if (queueit
|| !bitset(EF_INQUEUE
, e
->e_flags
))
532 queueup(e
, false, true);
539 ** Save old sibling and set it to NULL to avoid
540 ** queueing up the same envelopes again.
541 ** This requires that envelopes in that list have
542 ** been take care of before (or at some other place).
545 oldsib
= e
->e_sibling
;
547 if (!split_by_recipient(e
) &&
548 bitset(EF_FATALERRS
, e
->e_flags
))
550 syserr("!dropenvelope(%s): cannot commit data file %s, uid=%d",
551 e
->e_id
, queuename(e
, DATAFL_LETTER
),
554 for (ee
= e
->e_sibling
; ee
!= NULL
; ee
= ee
->e_sibling
)
555 queueup(ee
, false, true);
556 queueup(e
, false, true);
559 for (ee
= e
->e_sibling
; ee
!= NULL
; ee
= ee
->e_sibling
)
561 /* now unlock the job */
563 sm_dprintf("dropenvelope(%s): unlocking job\n",
568 /* this envelope is marked unused */
569 if (ee
->e_dfp
!= NULL
)
571 (void) sm_io_close(ee
->e_dfp
,
576 ee
->e_flags
&= ~EF_HAS_DF
;
578 e
->e_sibling
= oldsib
;
582 /* now unlock the job */
584 sm_dprintf("dropenvelope(%s): unlocking job\n", id
);
588 /* make sure that this envelope is marked unused */
589 if (e
->e_dfp
!= NULL
)
591 (void) sm_io_close(e
->e_dfp
, SM_TIME_DEFAULT
);
595 e
->e_flags
&= ~EF_HAS_DF
;
598 ** CLEARENVELOPE -- clear an envelope without unlocking
600 ** This is normally used by a child process to get a clean
601 ** envelope without disturbing the parent.
604 ** e -- the envelope to clear.
605 ** fullclear - if set, the current envelope is total
606 ** garbage and should be ignored; otherwise,
607 ** release any resources it may indicate.
608 ** rpool -- either NULL, or a pointer to a resource pool
609 ** from which envelope memory is allocated, and
610 ** to which envelope resources are attached.
616 ** Closes files associated with the envelope.
617 ** Marks the envelope as unallocated.
621 clearenvelope(e
, fullclear
, rpool
)
622 register ENVELOPE
*e
;
628 extern ENVELOPE BlankEnvelope
;
633 /* clear out any file information */
634 if (e
->e_xfp
!= NULL
)
635 (void) sm_io_close(e
->e_xfp
, SM_TIME_DEFAULT
);
636 if (e
->e_dfp
!= NULL
)
637 (void) sm_io_close(e
->e_dfp
, SM_TIME_DEFAULT
);
638 e
->e_xfp
= e
->e_dfp
= NULL
;
642 ** Copy BlankEnvelope into *e.
643 ** It is not safe to simply copy pointers to strings;
644 ** the strings themselves must be copied (or set to NULL).
645 ** The problem is that when we assign a new string value to
646 ** a member of BlankEnvelope, we free the old string.
647 ** We did not need to do this copying in sendmail 8.11 :-(
648 ** and it is a potential performance hit. Reference counted
649 ** strings are one way out.
654 e
->e_qfletter
= '\0';
656 macdefine(&e
->e_macro
, A_PERM
, macid("{quarantine}"), "");
659 ** Copy the macro table.
660 ** We might be able to avoid this by zeroing the macro table
661 ** and always searching BlankEnvelope.e_macro after e->e_macro
665 for (p
= &e
->e_macro
.mac_table
[0];
666 p
<= &e
->e_macro
.mac_table
[MAXMACROID
];
670 *p
= sm_rpool_strdup_x(rpool
, *p
);
674 ** XXX There are many strings in the envelope structure
675 ** XXX that we are not attempting to copy here.
676 ** XXX Investigate this further.
680 e
->e_macro
.mac_rpool
= rpool
;
682 set_delivery_mode(SM_DELIVER
, e
);
683 bh
= BlankEnvelope
.e_header
;
687 *nhp
= (HDR
*) sm_rpool_malloc_x(rpool
, sizeof *bh
);
688 memmove((char *) *nhp
, (char *) bh
, sizeof *bh
);
690 nhp
= &(*nhp
)->h_link
;
694 ** INITSYS -- initialize instantiation of system
696 ** In Daemon mode, this is done in the child.
699 ** e -- the envelope to use.
705 ** Initializes the system macros, some global variables,
706 ** etc. In particular, the current time in various
712 register ENVELOPE
*e
;
716 static char ybuf
[60]; /* holds tty id */
718 extern char *ttyname();
722 ** Give this envelope a reality.
723 ** I.e., an id, a transcript, and a creation time.
724 ** We don't select the queue until all of the recipients are known.
728 e
->e_ctime
= curtime();
729 e
->e_qfletter
= '\0';
732 ** Set OutChannel to something useful if stdout isn't it.
733 ** This arranges that any extra stuff the mailer produces
734 ** gets sent back to the user on error (because it is
735 ** tucked away in the transcript).
738 if (OpMode
== MD_DAEMON
&& bitset(EF_QUEUERUN
, e
->e_flags
) &&
740 OutChannel
= e
->e_xfp
;
743 ** Set up some basic system macros.
747 (void) sm_snprintf(buf
, sizeof buf
, "%d", (int) CurrentPid
);
748 macdefine(&e
->e_macro
, A_TEMP
, 'p', buf
);
751 (void) sm_snprintf(buf
, sizeof buf
, "%d", e
->e_hopcount
);
752 macdefine(&e
->e_macro
, A_TEMP
, 'c', buf
);
754 /* time as integer, unix time, arpa time */
762 if (macvalue('y', e
) == NULL
)
767 if (strrchr(p
, '/') != NULL
)
768 p
= strrchr(p
, '/') + 1;
769 (void) sm_strlcpy(ybuf
, sizeof ybuf
, p
);
770 macdefine(&e
->e_macro
, A_PERM
, 'y', ybuf
);
776 ** SETTIME -- set the current time.
779 ** e -- the envelope in which the macros should be set.
785 ** Sets the various time macros -- $a, $b, $d, $t.
790 register ENVELOPE
*e
;
795 register struct tm
*tm
;
798 (void) sm_snprintf(buf
, sizeof buf
, "%ld", (long) now
);
799 macdefine(&e
->e_macro
, A_TEMP
, macid("{time}"), buf
);
801 (void) sm_snprintf(buf
, sizeof buf
, "%04d%02d%02d%02d%02d",
802 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
803 tm
->tm_hour
, tm
->tm_min
);
804 macdefine(&e
->e_macro
, A_TEMP
, 't', buf
);
805 (void) sm_strlcpy(buf
, ctime(&now
), sizeof buf
);
806 p
= strchr(buf
, '\n');
809 macdefine(&e
->e_macro
, A_TEMP
, 'd', buf
);
810 macdefine(&e
->e_macro
, A_TEMP
, 'b', arpadate(buf
));
811 if (macvalue('a', e
) == NULL
)
812 macdefine(&e
->e_macro
, A_PERM
, 'a', macvalue('b', e
));
815 ** OPENXSCRIPT -- Open transcript file
817 ** Creates a transcript file for possible eventual mailing or
821 ** e -- the envelope to create the transcript in/for.
827 ** Creates the transcript file.
832 #endif /* ! O_APPEND */
836 register ENVELOPE
*e
;
840 if (e
->e_xfp
!= NULL
)
844 if (e
->e_lockfp
== NULL
&& bitset(EF_INQUEUE
, e
->e_flags
))
845 syserr("openxscript: job not locked");
848 p
= queuename(e
, XSCRPT_LETTER
);
849 e
->e_xfp
= bfopen(p
, FileMode
, XscriptFileBufferSize
,
850 SFF_NOTEXCL
|SFF_OPENASROOT
);
852 if (e
->e_xfp
== NULL
)
854 syserr("Can't create transcript file %s", p
);
855 e
->e_xfp
= sm_io_open(SmFtStdio
, SM_TIME_DEFAULT
,
856 SM_PATH_DEVNULL
, SM_IO_RDWR
, NULL
);
857 if (e
->e_xfp
== NULL
)
858 syserr("!Can't open %s", SM_PATH_DEVNULL
);
860 (void) sm_io_setvbuf(e
->e_xfp
, SM_TIME_DEFAULT
, NULL
, SM_IO_LBF
, 0);
863 sm_dprintf("openxscript(%s):\n ", p
);
864 dumpfd(sm_io_getinfo(e
->e_xfp
, SM_IO_WHAT_FD
, NULL
), true,
869 ** CLOSEXSCRIPT -- close the transcript file.
872 ** e -- the envelope containing the transcript to close.
883 register ENVELOPE
*e
;
885 if (e
->e_xfp
== NULL
)
888 if (e
->e_lockfp
== NULL
)
889 syserr("closexscript: job not locked");
891 (void) sm_io_close(e
->e_xfp
, SM_TIME_DEFAULT
);
895 ** SETSENDER -- set the person who this message is from
897 ** Under certain circumstances allow the user to say who
898 ** s/he is (using -f or -r). These are:
899 ** 1. The user's uid is zero (root).
900 ** 2. The user's login name is in an approved list (typically
901 ** from a network server).
902 ** 3. The address the user is trying to claim has a
903 ** "!" character in it (since #2 doesn't do it for
904 ** us if we are dialing out for UUCP).
905 ** A better check to replace #3 would be if the
906 ** effective uid is "UUCP" -- this would require me
907 ** to rewrite getpwent to "grab" uucp as it went by,
908 ** make getname more nasty, do another passwd file
909 ** scan, or compile the UID of "UUCP" into the code,
910 ** all of which are reprehensible.
912 ** Assuming all of these fail, we figure out something
916 ** from -- the person we would like to believe this message
917 ** is from, as specified on the command line.
918 ** e -- the envelope in which we would like the sender set.
919 ** delimptr -- if non-NULL, set to the location of the
920 ** trailing delimiter.
921 ** delimchar -- the character that will delimit the sender
923 ** internal -- set if this address is coming from an internal
924 ** source such as an owner alias.
930 ** sets sendmail's notion of who the from person is.
934 setsender(from
, e
, delimptr
, delimchar
, internal
)
936 register ENVELOPE
*e
;
942 char *realname
= NULL
;
944 char buf
[MAXNAME
+ 2];
945 char pvpbuf
[PSBUFSIZE
];
946 extern char *FullName
;
949 sm_dprintf("setsender(%s)\n", from
== NULL
? "" : from
);
951 /* may be set from earlier calls */
952 macdefine(&e
->e_macro
, A_PERM
, 'x', "");
955 ** Figure out the real user executing us.
956 ** Username can return errno != 0 on non-errors.
959 if (bitset(EF_QUEUERUN
, e
->e_flags
) || OpMode
== MD_SMTP
||
960 OpMode
== MD_ARPAFTP
|| OpMode
== MD_DAEMON
)
962 if (realname
== NULL
|| realname
[0] == '\0')
963 realname
= username();
968 macdefine(&e
->e_macro
, A_PERM
, macid("{addr_type}"), "e s");
970 /* preset state for then clause in case from == NULL */
971 e
->e_from
.q_state
= QS_BADADDR
;
972 e
->e_from
.q_flags
= 0;
974 parseaddr(from
, &e
->e_from
, RF_COPYALL
|RF_SENDERADDR
,
975 delimchar
, delimptr
, e
, false) == NULL
||
976 QS_IS_BADADDR(e
->e_from
.q_state
) ||
977 e
->e_from
.q_mailer
== ProgMailer
||
978 e
->e_from
.q_mailer
== FileMailer
||
979 e
->e_from
.q_mailer
== InclMailer
)
981 /* log garbage addresses for traceback */
982 if (from
!= NULL
&& LogLevel
> 2)
985 char ebuf
[MAXNAME
* 2 + 2];
987 p
= macvalue('_', e
);
990 char *host
= RealHostName
;
994 (void) sm_snprintf(ebuf
, sizeof ebuf
,
995 "%.*s@%.*s", MAXNAME
,
996 realname
, MAXNAME
, host
);
999 sm_syslog(LOG_NOTICE
, e
->e_id
,
1000 "setsender: %s: invalid or unparsable, received from %s",
1001 shortenstring(from
, 83), p
);
1005 if (!QS_IS_BADADDR(e
->e_from
.q_state
))
1007 /* it was a bogus mailer in the from addr */
1008 e
->e_status
= "5.1.7";
1009 usrerrenh(e
->e_status
,
1010 "553 Invalid sender address");
1014 if (from
== realname
||
1015 parseaddr(from
= realname
,
1016 &e
->e_from
, RF_COPYALL
|RF_SENDERADDR
, ' ',
1017 NULL
, e
, false) == NULL
)
1022 expand("\201n", nbuf
, sizeof nbuf
, e
);
1023 from
= sm_rpool_strdup_x(e
->e_rpool
, nbuf
);
1024 if (parseaddr(from
, &e
->e_from
, RF_COPYALL
, ' ',
1025 NULL
, e
, false) == NULL
&&
1026 parseaddr(from
= "postmaster", &e
->e_from
,
1027 RF_COPYALL
, ' ', NULL
, e
, false) == NULL
)
1028 syserr("553 5.3.0 setsender: can't even parse postmaster!");
1033 e
->e_from
.q_state
= QS_SENDER
;
1036 sm_dprintf("setsender: QS_SENDER ");
1037 printaddr(sm_debug_file(), &e
->e_from
, false);
1042 if (bitnset(M_CHECKUDB
, e
->e_from
.q_mailer
->m_flags
))
1046 p
= udbsender(e
->e_from
.q_user
, e
->e_rpool
);
1052 if (bitnset(M_HASPWENT
, e
->e_from
.q_mailer
->m_flags
))
1058 /* if the user already given fullname don't redefine */
1059 if (FullName
== NULL
)
1060 FullName
= macvalue('x', e
);
1061 if (FullName
!= NULL
)
1063 if (FullName
[0] == '\0')
1066 FullName
= newstr(FullName
);
1070 if (e
->e_from
.q_user
[0] != '\0' &&
1071 sm_mbdb_lookup(e
->e_from
.q_user
, &user
) == EX_OK
)
1074 ** Process passwd file entry.
1077 /* extract home directory */
1078 if (*user
.mbdb_homedir
== '\0')
1079 e
->e_from
.q_home
= NULL
;
1080 else if (strcmp(user
.mbdb_homedir
, "/") == 0)
1081 e
->e_from
.q_home
= "";
1083 e
->e_from
.q_home
= sm_rpool_strdup_x(e
->e_rpool
,
1085 macdefine(&e
->e_macro
, A_PERM
, 'z', e
->e_from
.q_home
);
1087 /* extract user and group id */
1088 if (user
.mbdb_uid
!= SM_NO_UID
)
1090 e
->e_from
.q_uid
= user
.mbdb_uid
;
1091 e
->e_from
.q_gid
= user
.mbdb_gid
;
1092 e
->e_from
.q_flags
|= QGOODUID
;
1095 /* extract full name from passwd file */
1096 if (FullName
== NULL
&& !internal
&&
1097 user
.mbdb_fullname
[0] != '\0' &&
1098 strcmp(user
.mbdb_name
, e
->e_from
.q_user
) == 0)
1100 FullName
= newstr(user
.mbdb_fullname
);
1105 e
->e_from
.q_home
= NULL
;
1107 if (FullName
!= NULL
&& !internal
)
1108 macdefine(&e
->e_macro
, A_TEMP
, 'x', FullName
);
1110 else if (!internal
&& OpMode
!= MD_DAEMON
&& OpMode
!= MD_SMTP
)
1112 if (e
->e_from
.q_home
== NULL
)
1114 e
->e_from
.q_home
= getenv("HOME");
1115 if (e
->e_from
.q_home
!= NULL
)
1117 if (*e
->e_from
.q_home
== '\0')
1118 e
->e_from
.q_home
= NULL
;
1119 else if (strcmp(e
->e_from
.q_home
, "/") == 0)
1123 e
->e_from
.q_uid
= RealUid
;
1124 e
->e_from
.q_gid
= RealGid
;
1125 e
->e_from
.q_flags
|= QGOODUID
;
1129 ** Rewrite the from person to dispose of possible implicit
1130 ** links in the net.
1133 pvp
= prescan(from
, delimchar
, pvpbuf
, sizeof pvpbuf
, NULL
, NULL
, false);
1136 /* don't need to give error -- prescan did that already */
1138 sm_syslog(LOG_NOTICE
, e
->e_id
,
1139 "cannot prescan from (%s)",
1140 shortenstring(from
, MAXSHORTSTR
));
1141 finis(true, true, ExitStat
);
1143 (void) REWRITE(pvp
, 3, e
);
1144 (void) REWRITE(pvp
, 1, e
);
1145 (void) REWRITE(pvp
, 4, e
);
1146 macdefine(&e
->e_macro
, A_PERM
, macid("{addr_type}"), NULL
);
1148 cataddr(pvp
, NULL
, bp
, sizeof buf
- 2, '\0');
1149 if (*bp
== '@' && !bitnset(M_NOBRACKET
, e
->e_from
.q_mailer
->m_flags
))
1151 /* heuristic: route-addr: add angle brackets */
1152 (void) sm_strlcat(bp
, ">", sizeof buf
- 1);
1155 e
->e_sender
= sm_rpool_strdup_x(e
->e_rpool
, bp
);
1156 macdefine(&e
->e_macro
, A_PERM
, 'f', e
->e_sender
);
1158 /* save the domain spec if this mailer wants it */
1159 if (e
->e_from
.q_mailer
!= NULL
&&
1160 bitnset(M_CANONICAL
, e
->e_from
.q_mailer
->m_flags
))
1164 /* get rid of any pesky angle brackets */
1165 macdefine(&e
->e_macro
, A_PERM
, macid("{addr_type}"), "e s");
1166 (void) REWRITE(pvp
, 3, e
);
1167 (void) REWRITE(pvp
, 1, e
);
1168 (void) REWRITE(pvp
, 4, e
);
1169 macdefine(&e
->e_macro
, A_PERM
, macid("{addr_type}"), NULL
);
1171 /* strip off to the last "@" sign */
1172 for (lastat
= NULL
; *pvp
!= NULL
; pvp
++)
1174 if (strcmp(*pvp
, "@") == 0)
1179 e
->e_fromdomain
= copyplist(lastat
, true, e
->e_rpool
);
1182 sm_dprintf("Saving from domain: ");
1183 printav(sm_debug_file(), e
->e_fromdomain
);
1189 ** PRINTENVFLAGS -- print envelope flags for debugging
1192 ** e -- the envelope with the flags to be printed.
1201 unsigned long ef_bit
;
1204 static struct eflags EnvelopeFlags
[] =
1206 { "OLDSTYLE", EF_OLDSTYLE
},
1207 { "INQUEUE", EF_INQUEUE
},
1208 { "NO_BODY_RETN", EF_NO_BODY_RETN
},
1209 { "CLRQUEUE", EF_CLRQUEUE
},
1210 { "SENDRECEIPT", EF_SENDRECEIPT
},
1211 { "FATALERRS", EF_FATALERRS
},
1212 { "DELETE_BCC", EF_DELETE_BCC
},
1213 { "RESPONSE", EF_RESPONSE
},
1214 { "RESENT", EF_RESENT
},
1215 { "VRFYONLY", EF_VRFYONLY
},
1216 { "WARNING", EF_WARNING
},
1217 { "QUEUERUN", EF_QUEUERUN
},
1218 { "GLOBALERRS", EF_GLOBALERRS
},
1219 { "PM_NOTIFY", EF_PM_NOTIFY
},
1220 { "METOO", EF_METOO
},
1221 { "LOGSENDER", EF_LOGSENDER
},
1222 { "NORECEIPT", EF_NORECEIPT
},
1223 { "HAS8BIT", EF_HAS8BIT
},
1224 { "NL_NOT_EOL", EF_NL_NOT_EOL
},
1225 { "CRLF_NOT_EOL", EF_CRLF_NOT_EOL
},
1226 { "RET_PARAM", EF_RET_PARAM
},
1227 { "HAS_DF", EF_HAS_DF
},
1228 { "IS_MIME", EF_IS_MIME
},
1229 { "DONT_MIME", EF_DONT_MIME
},
1230 { "DISCARD", EF_DISCARD
},
1231 { "TOOBIG", EF_TOOBIG
},
1232 { "SPLIT", EF_SPLIT
},
1233 { "UNSAFE", EF_UNSAFE
},
1239 register ENVELOPE
*e
;
1241 register struct eflags
*ef
;
1244 sm_dprintf("%lx", e
->e_flags
);
1245 for (ef
= EnvelopeFlags
; ef
->ef_name
!= NULL
; ef
++)
1247 if (!bitset(ef
->ef_bit
, e
->e_flags
))
1250 sm_dprintf("<%s", ef
->ef_name
);
1252 sm_dprintf(",%s", ef
->ef_name
);