* Copyright (c) 1983 Eric P. Allman
* Copyright (c) 1988 Regents of the University of California.
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)savemail.c 6.31 (Berkeley) %G%";
** SAVEMAIL -- Save mail on error
** If mailing back errors, mail it back to the originator
** together with an error message; otherwise, just put it in
** dead.letter in the user's home directory (if he exists on
** e -- the envelope containing the message in error.
** Saves the letter, by writing or mailing it back to the
** sender, or by putting it in dead.letter in her home
/* defines for state machine */
# define ESM_REPORT 0 /* report to sender's terminal */
# define ESM_MAIL 1 /* mail back to sender */
# define ESM_QUIET 2 /* messages have already been returned */
# define ESM_DEADLETTER 3 /* save in ~/dead.letter */
# define ESM_POSTMASTER 4 /* return to postmaster */
# define ESM_USRTMP 5 /* save in /usr/tmp/dead.letter */
# define ESM_PANIC 6 /* leave the locked queue/transcript files */
# define ESM_DONE 7 /* the message is successfully delivered */
register struct passwd
*pw
;
extern struct passwd
*getpwnam();
printf("\nsavemail, errormode = %c\n e_from=", e
->e_errormode
);
printaddr(&e
->e_from
, FALSE
);
if (bitset(EF_RESPONSE
, e
->e_flags
))
e
->e_flags
&= ~EF_FATALERRS
;
** In the unhappy event we don't know who to return the mail
if (CurEnv
->e_returnto
== NULL
)
CurEnv
->e_returnto
= parse("root", (ADDRESS
*) NULL
, 0);
if (CurEnv
->e_returnto
== NULL
)
syserr("553 Cannot parse Postmaster!");
** This machine runs through the following states:
** ESM_QUIET Errors have already been printed iff the
** ESM_REPORT Report directly to the sender's terminal.
** ESM_MAIL Mail response to the sender.
** ESM_DEADLETTER Save response in ~/dead.letter.
** ESM_POSTMASTER Mail response to the postmaster.
** ESM_PANIC Save response anywhere possible.
/* determine starting state */
/* mail back, but return o.k. exit status */
/* no need to return anything at all */
syserr("554 savemail: bogus errormode x%x\n", e
->e_errormode
);
while (state
!= ESM_DONE
)
printf(" state %d\n", state
);
if (e
->e_from
.q_mailer
== LocalMailer
)
** If the user is still logged in on the same terminal,
** then write the error messages back to hir (sic).
if (p
== NULL
|| freopen(p
, "w", stdout
) == NULL
)
expand("\201n", buf
, &buf
[sizeof buf
- 1], e
);
printf("\r\nMessage from %s...\r\n", buf
);
printf("Errors occurred while sending mail.\r\n");
fp
= fopen(queuename(e
, 'x'), "r");
syserr("Cannot open %s", queuename(e
, 'x'));
printf("Transcript of session is unavailable.\r\n");
printf("Transcript follows:\r\n");
while (fgets(buf
, sizeof buf
, fp
) != NULL
&&
(void) xfclose(fp
, "savemail transcript", e
->e_id
);
printf("Original message will be saved in dead.letter.\r\n");
** If mailing back, do it.
** Throw away all further output. Don't alias,
** since this could cause loops, e.g., if joe
** mails to joe@x, and for some reason the network
** for @x is down, then the response gets sent to
** joe@x, which gives a response, etc. Also force
** the mail to be delivered even if a version of
** it has already been sent to the sender.
if (strcmp(e
->e_from
.q_paddr
, "<>") != 0)
(void) sendtolist(e
->e_from
.q_paddr
,
/* deliver a cc: to the postmaster if desired */
if (PostMasterCopy
!= NULL
)
auto ADDRESS
*rlist
= NULL
;
(void) sendtolist(PostMasterCopy
,
(void) returntosender(e
->e_message
,
/* this is an error-error */
if (returntosender(e
->e_message
,
q
, (e
->e_class
>= 0), e
) == 0)
/* didn't work -- return to postmaster */
** Similar to previous case, but to system postmaster.
q
= parseaddr("postmaster", q
, 0, '\0', NULL
, e
);
syserr("553 cannot parse postmaster!");
if (returntosender(e
->e_message
,
q
, (e
->e_class
>= 0), e
) == 0)
/* didn't work -- last resort */
** Save the message in dead.letter.
** If we weren't mailing back, and the user is
** local, we should save the message in
** ~/dead.letter so that the poor person doesn't
** have to type it over again -- and we all know
** what poor typists UNIX users are.
if (e
->e_from
.q_mailer
== LocalMailer
)
if (e
->e_from
.q_home
!= NULL
)
else if ((pw
= getpwnam(e
->e_from
.q_user
)) != NULL
)
/* we have a home directory; open dead.letter */
expand("\201z/dead.letter", buf
, &buf
[sizeof buf
- 1], e
);
message("Saving message in %s", buf
);
(void) sendtolist(buf
, &e
->e_from
, &q
, e
);
/* no data file -- try mailing back */
** Log the mail in /usr/tmp/dead.letter.
fp
= dfopen("/usr/tmp/dead.letter", "a");
putfromline(fp
, FileMailer
, e
);
(*e
->e_puthdr
)(fp
, FileMailer
, e
);
putline("\n", fp
, FileMailer
);
(*e
->e_putbody
)(fp
, FileMailer
, e
);
putline("\n", fp
, FileMailer
);
state
= ferror(fp
) ? ESM_PANIC
: ESM_DONE
;
(void) xfclose(fp
, "savemail", "/usr/tmp/dead.letter");
syserr("554 savemail: unknown state %d", state
);
/* leave the locked queue & transcript files around */
syserr("554 savemail: cannot save rejected email anywhere");
** RETURNTOSENDER -- return a message to the sender with an error.
** msg -- the explanatory message.
** returnq -- the queue of people to send the message to.
** sendbody -- if TRUE, also send back the body of the
** message; otherwise just send the header.
** e -- the current envelope.
** zero -- if everything went ok.
** Returns the current message to the sender via
#define MAXRETURNS 6 /* max depth of returning messages */
#define ERRORFUDGE 100 /* nominal size of error message text */
returntosender(msg
, returnq
, sendbody
, e
)
extern putheader(), errbody();
ENVELOPE
*oldcur
= CurEnv
;
extern ENVELOPE
*newenvelope();
msg
= "Unable to deliver mail";
printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
printaddr(returnq
, TRUE
);
if (++returndepth
>= MAXRETURNS
)
if (returndepth
!= MAXRETURNS
)
syserr("554 returntosender: infinite recursion on %s", returnq
->q_paddr
);
/* don't "unrecurse" and fake a clean exit */
define('g', e
->e_from
.q_paddr
, e
);
ee
= newenvelope(&errenvelope
, e
);
define('a', "\201b", ee
);
define('r', "internal", ee
);
define('s', "localhost", ee
);
define('_', "localhost", ee
);
ee
->e_puthdr
= putheader
;
ee
->e_flags
|= EF_RESPONSE
;
if (!bitset(EF_OLDSTYLE
, e
->e_flags
))
ee
->e_flags
&= ~EF_OLDSTYLE
;
ee
->e_sendqueue
= returnq
;
ee
->e_msgsize
= e
->e_msgsize
+ ERRORFUDGE
;
for (q
= returnq
; q
!= NULL
; q
= q
->q_next
)
if (bitset(QDONTSEND
, q
->q_flags
))
if (!DontPruneRoutes
&& pruneroute(q
->q_paddr
))
parseaddr(q
->q_paddr
, q
, 0, '\0', NULL
, e
);
addheader("to", q
->q_paddr
, ee
);
syslog(LOG_INFO
, "%s: %s: return to sender: %s",
(void) sprintf(buf
, "Returned mail: %s", msg
);
addheader("subject", buf
, ee
);
/* fake up an address header for the from person */
expand("\201n", buf
, &buf
[sizeof buf
- 1], e
);
if (parseaddr(buf
, &ee
->e_from
, 1, '\0', NULL
, e
) == NULL
)
syserr("553 Can't parse myself!");
ee
->e_sender
= ee
->e_from
.q_paddr
;
/* push state into submessage */
define('f', "\201n", ee
);
define('x', "Mail Delivery Subsystem", ee
);
/* actually deliver the error message */
/* should check for delivery errors here */
** ERRBODY -- output the body of an error message.
** Typically this is a copy of the transcript plus a copy of the
** original offending message.
** fp -- the output file.
** m -- the mailer to output to.
** e -- the envelope we are working in.
** Outputs the body of an error message.
register struct mailer
*m
;
syserr("errbody: null parent");
putline(" ----- Original message lost -----\n", fp
, m
);
** Output error message header (if specified and available).
xfile
= fopen(ErrMsgFile
, "r");
while (fgets(buf
, sizeof buf
, xfile
) != NULL
)
expand(buf
, buf
, &buf
[sizeof buf
- 1], e
);
expand(ErrMsgFile
, buf
, &buf
[sizeof buf
- 1], e
);
** Output message introduction
for (q
= e
->e_parent
->e_sendqueue
; q
!= NULL
; q
= q
->q_next
)
if (bitset(QBADADDR
, q
->q_flags
))
putline("The following addresses failed:\n",
sprintf(buf
, "\t%s\n", q
->q_paddr
);
** Output transcript of errors
p
= queuename(e
->e_parent
, 'x');
if ((xfile
= fopen(p
, "r")) == NULL
)
syserr("Cannot open %s", p
);
putline(" ----- Transcript of session is unavailable -----\n", fp
, m
);
putline(" ----- Transcript of session follows -----\n", fp
, m
);
while (fgets(buf
, sizeof buf
, xfile
) != NULL
)
(void) xfclose(xfile
, "errbody xscript", p
);
** Output text of original message
if (e
->e_parent
->e_df
!= NULL
)
putline(" ----- Unsent message follows -----\n", fp
, m
);
putheader(fp
, m
, e
->e_parent
);
putbody(fp
, m
, e
->e_parent
);
putline(" ----- Message header follows -----\n", fp
, m
);
putheader(fp
, m
, e
->e_parent
);
putline(" ----- No message was collected -----\n", fp
, m
);
syserr("errbody: I/O error");
** PRUNEROUTE -- prune an RFC-822 source route
** Trims down a source route to the last internet-registered hop.
** This is encouraged by RFC 1123 section 5.3.3.
** TRUE -- address was modified
** FALSE -- address could not be pruned
** modifies addr in-place
char *start
, *at
, *comma
;
char *mxhosts
[MAXMXHOSTS
+ 1];
/* check to see if this is really a route-addr */
if (*addr
!= '<' || addr
[1] != '@' || addr
[strlen(addr
) - 1] != '>')
start
= strchr(addr
, ':');
if (start
== NULL
|| at
== NULL
|| at
< start
)
/* slice off the angle brackets */
hostbuf
[strlen(hostbuf
) - 1] = '\0';
if (getmxrr(hostbuf
, mxhosts
, "", &rcode
) > 0)
strcpy(addr
+ 1, start
+ 1);
comma
= strrchr(addr
, ',');
if (comma
&& comma
[1] == '@')
strcpy(hostbuf
, comma
+ 2);