122711Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 362531Sbostic * Copyright (c) 1988, 1993 462531Sbostic * The Regents of the University of California. All rights reserved. 533731Sbostic * 642829Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822711Sdist 922711Sdist #ifndef lint 10*65174Seric static char sccsid[] = "@(#)savemail.c 8.24 (Berkeley) 12/18/93"; 1133731Sbostic #endif /* not lint */ 1222711Sdist 1363937Seric # include "sendmail.h" 14297Seric # include <pwd.h> 15297Seric 16297Seric /* 17297Seric ** SAVEMAIL -- Save mail on error 18297Seric ** 199375Seric ** If mailing back errors, mail it back to the originator 20297Seric ** together with an error message; otherwise, just put it in 21297Seric ** dead.letter in the user's home directory (if he exists on 22297Seric ** this machine). 23297Seric ** 24297Seric ** Parameters: 259337Seric ** e -- the envelope containing the message in error. 26297Seric ** 27297Seric ** Returns: 28297Seric ** none 29297Seric ** 30297Seric ** Side Effects: 31297Seric ** Saves the letter, by writing or mailing it back to the 32297Seric ** sender, or by putting it in dead.letter in her home 33297Seric ** directory. 34297Seric */ 35297Seric 3624942Seric /* defines for state machine */ 3724942Seric # define ESM_REPORT 0 /* report to sender's terminal */ 3824942Seric # define ESM_MAIL 1 /* mail back to sender */ 3924942Seric # define ESM_QUIET 2 /* messages have already been returned */ 4024942Seric # define ESM_DEADLETTER 3 /* save in ~/dead.letter */ 4124942Seric # define ESM_POSTMASTER 4 /* return to postmaster */ 4224942Seric # define ESM_USRTMP 5 /* save in /usr/tmp/dead.letter */ 4324942Seric # define ESM_PANIC 6 /* leave the locked queue/transcript files */ 4424942Seric # define ESM_DONE 7 /* the message is successfully delivered */ 4524942Seric 46*65174Seric # ifndef _PATH_VARTMP 47*65174Seric # define _PATH_VARTMP "/usr/tmp/" 48*65174Seric # endif 4924942Seric 50*65174Seric 519337Seric savemail(e) 529337Seric register ENVELOPE *e; 53297Seric { 54297Seric register struct passwd *pw; 5524942Seric register FILE *fp; 5624942Seric int state; 5759290Seric auto ADDRESS *q = NULL; 58297Seric char buf[MAXLINE+1]; 59297Seric extern struct passwd *getpwnam(); 60297Seric register char *p; 61297Seric extern char *ttypath(); 625846Seric typedef int (*fnptr)(); 6364945Seric extern bool writable(); 64297Seric 657676Seric if (tTd(6, 1)) 6658680Seric { 6759101Seric printf("\nsavemail, errormode = %c, id = %s\n e_from=", 6859101Seric e->e_errormode, e->e_id == NULL ? "NONE" : e->e_id); 6958680Seric printaddr(&e->e_from, FALSE); 7058680Seric } 717361Seric 7259101Seric if (e->e_id == NULL) 7359101Seric { 7459101Seric /* can't return a message with no id */ 7559101Seric return; 7659101Seric } 7759101Seric 78297Seric /* 79297Seric ** In the unhappy event we don't know who to return the mail 80297Seric ** to, make someone up. 81297Seric */ 82297Seric 839337Seric if (e->e_from.q_paddr == NULL) 84297Seric { 8558733Seric e->e_sender = "Postmaster"; 8664284Seric if (parseaddr(e->e_sender, &e->e_from, 8764284Seric RF_COPYPARSE|RF_SENDERADDR, '\0', NULL, e) == NULL) 88297Seric { 8958733Seric syserr("553 Cannot parse Postmaster!"); 90297Seric ExitStat = EX_SOFTWARE; 91297Seric finis(); 92297Seric } 93297Seric } 949337Seric e->e_to = NULL; 95297Seric 96297Seric /* 9724942Seric ** Basic state machine. 9824942Seric ** 9924942Seric ** This machine runs through the following states: 10024942Seric ** 10124942Seric ** ESM_QUIET Errors have already been printed iff the 10224942Seric ** sender is local. 10324942Seric ** ESM_REPORT Report directly to the sender's terminal. 10424942Seric ** ESM_MAIL Mail response to the sender. 10524942Seric ** ESM_DEADLETTER Save response in ~/dead.letter. 10624942Seric ** ESM_POSTMASTER Mail response to the postmaster. 10724942Seric ** ESM_PANIC Save response anywhere possible. 108297Seric */ 109297Seric 11024942Seric /* determine starting state */ 11158734Seric switch (e->e_errormode) 112297Seric { 11324942Seric case EM_WRITE: 11424942Seric state = ESM_REPORT; 11524942Seric break; 11624942Seric 11724942Seric case EM_BERKNET: 11824942Seric /* mail back, but return o.k. exit status */ 119401Seric ExitStat = EX_OK; 12024942Seric 12124942Seric /* fall through.... */ 12224942Seric 12324942Seric case EM_MAIL: 12424942Seric state = ESM_MAIL; 12524942Seric break; 12624942Seric 12724942Seric case EM_PRINT: 12824979Seric case '\0': 12924942Seric state = ESM_QUIET; 13024942Seric break; 13124942Seric 13224942Seric case EM_QUIET: 13324942Seric /* no need to return anything at all */ 13424942Seric return; 13524979Seric 13624979Seric default: 13758734Seric syserr("554 savemail: bogus errormode x%x\n", e->e_errormode); 13824979Seric state = ESM_MAIL; 13924979Seric break; 140297Seric } 141297Seric 14259094Seric /* if this is already an error response, send to postmaster */ 14359094Seric if (bitset(EF_RESPONSE, e->e_flags)) 14459094Seric { 14559094Seric if (e->e_parent != NULL && 14659094Seric bitset(EF_RESPONSE, e->e_parent->e_flags)) 14759094Seric { 14859094Seric /* got an error sending a response -- can it */ 14959094Seric return; 15059094Seric } 15159094Seric state = ESM_POSTMASTER; 15259094Seric } 15359094Seric 15424942Seric while (state != ESM_DONE) 155297Seric { 15624979Seric if (tTd(6, 5)) 15724979Seric printf(" state %d\n", state); 15824979Seric 15924942Seric switch (state) 160297Seric { 16124979Seric case ESM_QUIET: 16224979Seric if (e->e_from.q_mailer == LocalMailer) 16324979Seric state = ESM_DEADLETTER; 16424979Seric else 16524979Seric state = ESM_MAIL; 16624979Seric break; 16724979Seric 16824942Seric case ESM_REPORT: 16924942Seric 17024942Seric /* 17124942Seric ** If the user is still logged in on the same terminal, 17224942Seric ** then write the error messages back to hir (sic). 17324942Seric */ 17424942Seric 17524942Seric p = ttypath(); 17624942Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 17724942Seric { 17824942Seric state = ESM_MAIL; 17924942Seric break; 18024942Seric } 18124942Seric 18258050Seric expand("\201n", buf, &buf[sizeof buf - 1], e); 1839375Seric printf("\r\nMessage from %s...\r\n", buf); 1849375Seric printf("Errors occurred while sending mail.\r\n"); 1859542Seric if (e->e_xfp != NULL) 1869375Seric { 1879542Seric (void) fflush(e->e_xfp); 18824942Seric fp = fopen(queuename(e, 'x'), "r"); 1899375Seric } 1909375Seric else 19124942Seric fp = NULL; 19224942Seric if (fp == NULL) 1939375Seric { 1949337Seric syserr("Cannot open %s", queuename(e, 'x')); 1959375Seric printf("Transcript of session is unavailable.\r\n"); 1969375Seric } 1979375Seric else 1989375Seric { 1999375Seric printf("Transcript follows:\r\n"); 20024942Seric while (fgets(buf, sizeof buf, fp) != NULL && 2019375Seric !ferror(stdout)) 2029375Seric fputs(buf, stdout); 20358680Seric (void) xfclose(fp, "savemail transcript", e->e_id); 2049375Seric } 20524942Seric printf("Original message will be saved in dead.letter.\r\n"); 20624942Seric state = ESM_DEADLETTER; 20724942Seric break; 208297Seric 20924942Seric case ESM_MAIL: 21024942Seric /* 21124942Seric ** If mailing back, do it. 21224942Seric ** Throw away all further output. Don't alias, 21324942Seric ** since this could cause loops, e.g., if joe 21424942Seric ** mails to joe@x, and for some reason the network 21524942Seric ** for @x is down, then the response gets sent to 21624942Seric ** joe@x, which gives a response, etc. Also force 21724942Seric ** the mail to be delivered even if a version of 21824942Seric ** it has already been sent to the sender. 21963841Seric ** 22063841Seric ** If this is a configuration or local software 22163841Seric ** error, send to the local postmaster as well, 22263841Seric ** since the originator can't do anything 22363841Seric ** about it anyway. Note that this is a full 22463841Seric ** copy of the message (intentionally) so that 22563841Seric ** the Postmaster can forward things along. 22624942Seric */ 227297Seric 22863841Seric if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE) 22963841Seric { 23063841Seric (void) sendtolist("postmaster", 23164284Seric NULLADDR, &e->e_errorqueue, e); 23263841Seric } 23358680Seric if (strcmp(e->e_from.q_paddr, "<>") != 0) 23463841Seric { 23558680Seric (void) sendtolist(e->e_from.q_paddr, 23664284Seric NULLADDR, &e->e_errorqueue, e); 23763841Seric } 23858680Seric 23963841Seric /* 24063841Seric ** Deliver a non-delivery report to the 24163841Seric ** Postmaster-designate (not necessarily 24263841Seric ** Postmaster). This does not include the 24363841Seric ** body of the message, for privacy reasons. 24463841Seric ** You really shouldn't need this. 24563841Seric */ 24663841Seric 24763849Seric e->e_flags |= EF_PM_NOTIFY; 24858178Seric 24964792Seric /* check to see if there are any good addresses */ 25064792Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 25164792Seric if (!bitset(QBADADDR|QDONTSEND, q->q_flags)) 25264792Seric break; 25358680Seric if (q == NULL) 25458680Seric { 25558680Seric /* this is an error-error */ 25658680Seric state = ESM_POSTMASTER; 25758680Seric break; 25858680Seric } 25958966Seric if (returntosender(e->e_message, 26058680Seric q, (e->e_class >= 0), e) == 0) 26158680Seric { 26258680Seric state = ESM_DONE; 26358680Seric break; 26458680Seric } 26524981Seric 26658680Seric /* didn't work -- return to postmaster */ 26758680Seric state = ESM_POSTMASTER; 26858680Seric break; 26958306Seric 27058680Seric case ESM_POSTMASTER: 27158680Seric /* 27258680Seric ** Similar to previous case, but to system postmaster. 27358680Seric */ 27458680Seric 27559432Seric q = NULL; 27659432Seric if (sendtolist("postmaster", NULL, &q, e) <= 0) 27724942Seric { 27858680Seric syserr("553 cannot parse postmaster!"); 27958680Seric ExitStat = EX_SOFTWARE; 28058680Seric state = ESM_USRTMP; 28158680Seric break; 28224942Seric } 28358966Seric if (returntosender(e->e_message, 28457438Seric q, (e->e_class >= 0), e) == 0) 28524942Seric { 28624942Seric state = ESM_DONE; 28724942Seric break; 28824942Seric } 289297Seric 29058680Seric /* didn't work -- last resort */ 29158680Seric state = ESM_USRTMP; 29224942Seric break; 293297Seric 29424942Seric case ESM_DEADLETTER: 29524942Seric /* 29624942Seric ** Save the message in dead.letter. 29724942Seric ** If we weren't mailing back, and the user is 29824942Seric ** local, we should save the message in 29924942Seric ** ~/dead.letter so that the poor person doesn't 30024942Seric ** have to type it over again -- and we all know 30124942Seric ** what poor typists UNIX users are. 30224942Seric */ 3035315Seric 30424942Seric p = NULL; 30524942Seric if (e->e_from.q_mailer == LocalMailer) 30624942Seric { 30724942Seric if (e->e_from.q_home != NULL) 30824942Seric p = e->e_from.q_home; 30924942Seric else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 31024942Seric p = pw->pw_dir; 31124942Seric } 31224942Seric if (p == NULL) 31324942Seric { 31458865Seric /* no local directory */ 31524942Seric state = ESM_MAIL; 31624942Seric break; 31724942Seric } 31824942Seric if (e->e_dfp != NULL) 31924942Seric { 32024942Seric bool oldverb = Verbose; 32124942Seric 32224942Seric /* we have a home directory; open dead.letter */ 32324942Seric define('z', p, e); 32458050Seric expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e); 32524942Seric Verbose = TRUE; 32658151Seric message("Saving message in %s", buf); 32724942Seric Verbose = oldverb; 32824942Seric e->e_to = buf; 32924942Seric q = NULL; 33058082Seric (void) sendtolist(buf, &e->e_from, &q, e); 33164354Seric if (q != NULL && 33264354Seric !bitset(QBADADDR, q->q_flags) && 33364354Seric deliver(e, q) == 0) 33424942Seric state = ESM_DONE; 33524942Seric else 33624942Seric state = ESM_MAIL; 33724942Seric } 33825569Seric else 33925569Seric { 34025569Seric /* no data file -- try mailing back */ 34125569Seric state = ESM_MAIL; 34225569Seric } 34324942Seric break; 34424942Seric 34524942Seric case ESM_USRTMP: 34624942Seric /* 34724942Seric ** Log the mail in /usr/tmp/dead.letter. 34824942Seric */ 34924942Seric 35057438Seric if (e->e_class < 0) 35157438Seric { 35257438Seric state = ESM_DONE; 35357438Seric break; 35457438Seric } 35557438Seric 356*65174Seric strcpy(buf, _PATH_VARTMP); 357*65174Seric strcat(buf, "dead.letter"); 35865112Seric if (!writable(buf, NULLADDR, SFF_NOSLINK)) 35964945Seric { 36064945Seric state = ESM_PANIC; 36164945Seric break; 36264945Seric } 36364945Seric fp = dfopen(buf, O_WRONLY|O_CREAT|O_APPEND, FileMode); 36424942Seric if (fp == NULL) 36524942Seric { 36624942Seric state = ESM_PANIC; 36724942Seric break; 36824942Seric } 36924942Seric 37058010Seric putfromline(fp, FileMailer, e); 37158010Seric (*e->e_puthdr)(fp, FileMailer, e); 37258010Seric putline("\n", fp, FileMailer); 37359730Seric (*e->e_putbody)(fp, FileMailer, e, NULL); 37458010Seric putline("\n", fp, FileMailer); 37524942Seric (void) fflush(fp); 37624942Seric state = ferror(fp) ? ESM_PANIC : ESM_DONE; 37758680Seric (void) xfclose(fp, "savemail", "/usr/tmp/dead.letter"); 37824942Seric break; 37924942Seric 38024942Seric default: 38158151Seric syserr("554 savemail: unknown state %d", state); 38224942Seric 38324942Seric /* fall through ... */ 38424942Seric 38524942Seric case ESM_PANIC: 38624942Seric /* leave the locked queue & transcript files around */ 38764949Seric syserr("!554 savemail: cannot save rejected email anywhere"); 38824942Seric } 389297Seric } 390297Seric } 391297Seric /* 3924633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 3934633Seric ** 3944633Seric ** Parameters: 3954633Seric ** msg -- the explanatory message. 39616479Seric ** returnq -- the queue of people to send the message to. 3975984Seric ** sendbody -- if TRUE, also send back the body of the 3985984Seric ** message; otherwise just send the header. 39955012Seric ** e -- the current envelope. 4004633Seric ** 4014633Seric ** Returns: 4024633Seric ** zero -- if everything went ok. 4034633Seric ** else -- some error. 4044633Seric ** 4054633Seric ** Side Effects: 4064633Seric ** Returns the current message to the sender via 4074633Seric ** mail. 4084633Seric */ 4094633Seric 4105984Seric static bool SendBody; 4114633Seric 4127045Seric #define MAXRETURNS 6 /* max depth of returning messages */ 41358559Seric #define ERRORFUDGE 100 /* nominal size of error message text */ 4147045Seric 41555012Seric returntosender(msg, returnq, sendbody, e) 4164633Seric char *msg; 41716479Seric ADDRESS *returnq; 4185984Seric bool sendbody; 41955012Seric register ENVELOPE *e; 4204633Seric { 4214633Seric char buf[MAXNAME]; 4226978Seric extern putheader(), errbody(); 4236978Seric register ENVELOPE *ee; 42458680Seric ENVELOPE *oldcur = CurEnv; 4256978Seric ENVELOPE errenvelope; 4267045Seric static int returndepth; 4279375Seric register ADDRESS *q; 4284633Seric 42960010Seric if (returnq == NULL) 43060010Seric return (-1); 43160010Seric 43258966Seric if (msg == NULL) 43358966Seric msg = "Unable to deliver mail"; 43458966Seric 4357676Seric if (tTd(6, 1)) 4367287Seric { 43758680Seric printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=", 43855012Seric msg, returndepth, e); 43916479Seric printaddr(returnq, TRUE); 4407287Seric } 4417287Seric 4427045Seric if (++returndepth >= MAXRETURNS) 4437045Seric { 4447045Seric if (returndepth != MAXRETURNS) 44558151Seric syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr); 4467045Seric /* don't "unrecurse" and fake a clean exit */ 4477045Seric /* returndepth--; */ 4487045Seric return (0); 4497045Seric } 4507045Seric 4515984Seric SendBody = sendbody; 45258680Seric define('g', e->e_from.q_paddr, e); 45364940Seric define('u', NULL, e); 45458179Seric ee = newenvelope(&errenvelope, e); 45558050Seric define('a', "\201b", ee); 45659057Seric define('r', "internal", ee); 45759057Seric define('s', "localhost", ee); 45859057Seric define('_', "localhost", ee); 4596978Seric ee->e_puthdr = putheader; 4606978Seric ee->e_putbody = errbody; 46164120Seric ee->e_flags |= EF_RESPONSE|EF_METOO; 46255012Seric if (!bitset(EF_OLDSTYLE, e->e_flags)) 46345155Seric ee->e_flags &= ~EF_OLDSTYLE; 46416479Seric ee->e_sendqueue = returnq; 46564655Seric ee->e_msgsize = ERRORFUDGE; 46664655Seric if (!NoReturn) 46764655Seric ee->e_msgsize += e->e_msgsize; 46864737Seric initsys(ee); 46916479Seric for (q = returnq; q != NULL; q = q->q_next) 4709375Seric { 47159989Seric if (bitset(QBADADDR, q->q_flags)) 47258559Seric continue; 47358559Seric 47459989Seric if (!bitset(QDONTSEND, q->q_flags)) 47559989Seric ee->e_nrcpts++; 47658559Seric 47758144Seric if (!DontPruneRoutes && pruneroute(q->q_paddr)) 47864284Seric parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e); 47958144Seric 4809375Seric if (q->q_alias == NULL) 48159580Seric addheader("To", q->q_paddr, ee); 4829375Seric } 48324942Seric 48457642Seric # ifdef LOG 48558020Seric if (LogLevel > 5) 48665054Seric syslog(LOG_INFO, "%s: %s: return to sender: %s", 48757642Seric e->e_id, ee->e_id, msg); 48857642Seric # endif 48957642Seric 49065054Seric (void) sprintf(buf, "Returned mail: %s", msg); 49165054Seric addheader("Subject", buf, ee); 49259730Seric if (SendMIMEErrors) 49359730Seric { 49459987Seric addheader("MIME-Version", "1.0", ee); 49559730Seric (void) sprintf(buf, "%s.%ld/%s", 49659730Seric ee->e_id, curtime(), MyHostName); 49759730Seric ee->e_msgboundary = newstr(buf); 49859730Seric (void) sprintf(buf, "multipart/mixed; boundary=\"%s\"", 49959730Seric ee->e_msgboundary); 50059730Seric addheader("Content-Type", buf, ee); 50159730Seric } 5024633Seric 5034633Seric /* fake up an address header for the from person */ 50458050Seric expand("\201n", buf, &buf[sizeof buf - 1], e); 50564284Seric if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL) 5064633Seric { 50758151Seric syserr("553 Can't parse myself!"); 5084633Seric ExitStat = EX_SOFTWARE; 5097045Seric returndepth--; 5104633Seric return (-1); 5114633Seric } 51258704Seric ee->e_sender = ee->e_from.q_paddr; 5135984Seric 5146978Seric /* push state into submessage */ 5156978Seric CurEnv = ee; 51658050Seric define('f', "\201n", ee); 5179375Seric define('x', "Mail Delivery Subsystem", ee); 51858929Seric eatheader(ee, TRUE); 5195984Seric 52063753Seric /* mark statistics */ 52164284Seric markstats(ee, NULLADDR); 52263753Seric 5236978Seric /* actually deliver the error message */ 52414876Seric sendall(ee, SM_DEFAULT); 5256978Seric 5266978Seric /* restore state */ 5277811Seric dropenvelope(ee); 52858680Seric CurEnv = oldcur; 5297045Seric returndepth--; 5306978Seric 5317045Seric /* should check for delivery errors here */ 5324633Seric return (0); 5334633Seric } 5344633Seric /* 5356978Seric ** ERRBODY -- output the body of an error message. 5366978Seric ** 5376978Seric ** Typically this is a copy of the transcript plus a copy of the 5386978Seric ** original offending message. 5396978Seric ** 540297Seric ** Parameters: 541297Seric ** fp -- the output file. 54210170Seric ** m -- the mailer to output to. 5439542Seric ** e -- the envelope we are working in. 544297Seric ** 545297Seric ** Returns: 546297Seric ** none 547297Seric ** 548297Seric ** Side Effects: 5496978Seric ** Outputs the body of an error message. 550297Seric */ 551297Seric 55210170Seric errbody(fp, m, e) 553297Seric register FILE *fp; 5544318Seric register struct mailer *m; 5559542Seric register ENVELOPE *e; 556297Seric { 5576978Seric register FILE *xfile; 55859082Seric char *p; 55959082Seric register ADDRESS *q; 56059082Seric bool printheader; 5613189Seric char buf[MAXLINE]; 562297Seric 56358680Seric if (e->e_parent == NULL) 56458680Seric { 56558680Seric syserr("errbody: null parent"); 56658680Seric putline(" ----- Original message lost -----\n", fp, m); 56758680Seric return; 56858680Seric } 56958680Seric 5709057Seric /* 57159730Seric ** Output MIME header. 57259730Seric */ 57359730Seric 57459730Seric if (e->e_msgboundary != NULL) 57559730Seric { 57659730Seric putline("This is a MIME-encapsulated message", fp, m); 57759730Seric putline("", fp, m); 57859730Seric (void) sprintf(buf, "--%s", e->e_msgboundary); 57959730Seric putline(buf, fp, m); 58059730Seric putline("", fp, m); 58159730Seric } 58259730Seric 58359730Seric /* 58463852Seric ** Output introductory information. 58563852Seric */ 58663852Seric 58764718Seric for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 58864718Seric if (bitset(QBADADDR, q->q_flags)) 58964718Seric break; 59065054Seric if (q == NULL && 59165054Seric !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags)) 59264718Seric { 59364718Seric putline(" **********************************************", 59464718Seric fp, m); 59564718Seric putline(" ** THIS IS A WARNING MESSAGE ONLY **", 59664718Seric fp, m); 59764718Seric putline(" ** YOU DO NOT NEED TO RESEND YOUR MESSAGE **", 59864718Seric fp, m); 59964718Seric putline(" **********************************************", 60064718Seric fp, m); 60164718Seric putline("", fp, m); 60264718Seric } 60364718Seric sprintf(buf, "The original message was received at %s", 60464718Seric arpadate(ctime(&e->e_parent->e_ctime))); 60563852Seric putline(buf, fp, m); 60664025Seric expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent); 60763852Seric putline(buf, fp, m); 60863852Seric putline("", fp, m); 60963852Seric 61063852Seric /* 61155372Seric ** Output error message header (if specified and available). 61255372Seric */ 61355372Seric 61455372Seric if (ErrMsgFile != NULL) 61555372Seric { 61655372Seric if (*ErrMsgFile == '/') 61755372Seric { 61855372Seric xfile = fopen(ErrMsgFile, "r"); 61955372Seric if (xfile != NULL) 62055372Seric { 62155372Seric while (fgets(buf, sizeof buf, xfile) != NULL) 62255425Seric { 62355425Seric expand(buf, buf, &buf[sizeof buf - 1], e); 62455372Seric putline(buf, fp, m); 62555425Seric } 62655372Seric (void) fclose(xfile); 62759082Seric putline("\n", fp, m); 62855372Seric } 62955372Seric } 63055372Seric else 63155372Seric { 63255425Seric expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e); 63355425Seric putline(buf, fp, m); 63459730Seric putline("", fp, m); 63555372Seric } 63655372Seric } 63755372Seric 63855372Seric /* 63959082Seric ** Output message introduction 64059082Seric */ 64159082Seric 64259082Seric printheader = TRUE; 64359082Seric for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 64459082Seric { 64563787Seric if (bitset(QBADADDR|QREPORT, q->q_flags)) 64659082Seric { 64759082Seric if (printheader) 64859082Seric { 64963787Seric putline(" ----- The following addresses had delivery problems -----", 65059082Seric fp, m); 65159082Seric printheader = FALSE; 65259082Seric } 65363849Seric strcpy(buf, q->q_paddr); 65463787Seric if (bitset(QBADADDR, q->q_flags)) 65563849Seric strcat(buf, " (unrecoverable error)"); 65663787Seric else 65763849Seric strcat(buf, " (transient failure)"); 65863787Seric putline(buf, fp, m); 65963849Seric if (q->q_alias != NULL) 66063849Seric { 66163849Seric strcpy(buf, " (expanded from: "); 66263849Seric strcat(buf, q->q_alias->q_paddr); 66363849Seric strcat(buf, ")"); 66463849Seric putline(buf, fp, m); 66563849Seric } 66659082Seric } 66759082Seric } 66859082Seric if (!printheader) 66959082Seric putline("\n", fp, m); 67059082Seric 67159082Seric /* 6729057Seric ** Output transcript of errors 6739057Seric */ 6749057Seric 6754086Seric (void) fflush(stdout); 6769542Seric p = queuename(e->e_parent, 'x'); 6779337Seric if ((xfile = fopen(p, "r")) == NULL) 6789057Seric { 6799337Seric syserr("Cannot open %s", p); 68059082Seric putline(" ----- Transcript of session is unavailable -----\n", fp, m); 6819057Seric } 6829057Seric else 6839057Seric { 68458680Seric putline(" ----- Transcript of session follows -----\n", fp, m); 6859542Seric if (e->e_xfp != NULL) 6869542Seric (void) fflush(e->e_xfp); 6879057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 68810170Seric putline(buf, fp, m); 68958680Seric (void) xfclose(xfile, "errbody xscript", p); 6909057Seric } 691297Seric errno = 0; 6924318Seric 6934318Seric /* 6944318Seric ** Output text of original message 6954318Seric */ 6964318Seric 6974289Seric if (NoReturn) 69858665Seric SendBody = FALSE; 69959730Seric putline("", fp, m); 70058680Seric if (e->e_parent->e_df != NULL) 7014199Seric { 7025984Seric if (SendBody) 70363852Seric putline(" ----- Original message follows -----\n", fp, m); 7045984Seric else 70559987Seric putline(" ----- Message header follows -----\n", fp, m); 70659730Seric (void) fflush(fp); 70759730Seric 70859730Seric if (e->e_msgboundary != NULL) 7095984Seric { 71059730Seric putline("", fp, m); 71159730Seric (void) sprintf(buf, "--%s", e->e_msgboundary); 71259730Seric putline(buf, fp, m); 71359987Seric putline("Content-Type: message/rfc822", fp, m); 71459730Seric putline("", fp, m); 7155984Seric } 71659730Seric putheader(fp, m, e->e_parent); 71759730Seric putline("", fp, m); 71859730Seric if (SendBody) 71959730Seric putbody(fp, m, e->e_parent, e->e_msgboundary); 72059987Seric else 72160010Seric putline(" ----- Message body suppressed -----", fp, m); 7224199Seric } 7234199Seric else 72410170Seric { 72510170Seric putline(" ----- No message was collected -----\n", fp, m); 72610170Seric } 7274318Seric 72860010Seric if (e->e_msgboundary != NULL) 72960010Seric { 73060010Seric putline("", fp, m); 73160010Seric (void) sprintf(buf, "--%s--", e->e_msgboundary); 73260010Seric putline(buf, fp, m); 73360010Seric } 73459730Seric putline("", fp, m); 73559730Seric 7364318Seric /* 7374318Seric ** Cleanup and exit 7384318Seric */ 7394318Seric 740297Seric if (errno != 0) 7416978Seric syserr("errbody: I/O error"); 742297Seric } 74358144Seric /* 74458144Seric ** PRUNEROUTE -- prune an RFC-822 source route 74558144Seric ** 74658144Seric ** Trims down a source route to the last internet-registered hop. 74758144Seric ** This is encouraged by RFC 1123 section 5.3.3. 74858144Seric ** 74958144Seric ** Parameters: 75058144Seric ** addr -- the address 75158144Seric ** 75258144Seric ** Returns: 75358144Seric ** TRUE -- address was modified 75458144Seric ** FALSE -- address could not be pruned 75558144Seric ** 75658144Seric ** Side Effects: 75758144Seric ** modifies addr in-place 75858144Seric */ 75958144Seric 76058144Seric pruneroute(addr) 76158144Seric char *addr; 76258144Seric { 76358144Seric #ifdef NAMED_BIND 76458144Seric char *start, *at, *comma; 76558144Seric char c; 76658144Seric int rcode; 76758144Seric char hostbuf[BUFSIZ]; 76858144Seric char *mxhosts[MAXMXHOSTS + 1]; 76958144Seric 77058144Seric /* check to see if this is really a route-addr */ 77158144Seric if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>') 77258144Seric return FALSE; 77358144Seric start = strchr(addr, ':'); 77458144Seric at = strrchr(addr, '@'); 77558144Seric if (start == NULL || at == NULL || at < start) 77658144Seric return FALSE; 77758144Seric 77858144Seric /* slice off the angle brackets */ 77958144Seric strcpy(hostbuf, at + 1); 78058144Seric hostbuf[strlen(hostbuf) - 1] = '\0'; 78158144Seric 78258144Seric while (start) 78358144Seric { 78459273Seric if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0) 78558144Seric { 78658144Seric strcpy(addr + 1, start + 1); 78758144Seric return TRUE; 78858144Seric } 78958144Seric c = *start; 79058144Seric *start = '\0'; 79158144Seric comma = strrchr(addr, ','); 79258144Seric if (comma && comma[1] == '@') 79358144Seric strcpy(hostbuf, comma + 2); 79458144Seric else 79558144Seric comma = 0; 79658144Seric *start = c; 79758144Seric start = comma; 79858144Seric } 79958144Seric #endif 80058144Seric return FALSE; 80158144Seric } 802