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*64718Seric static char sccsid[] = "@(#)savemail.c 8.14 (Berkeley) 10/15/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 4624942Seric 479337Seric savemail(e) 489337Seric register ENVELOPE *e; 49297Seric { 50297Seric register struct passwd *pw; 5124942Seric register FILE *fp; 5224942Seric int state; 5359290Seric auto ADDRESS *q = NULL; 54297Seric char buf[MAXLINE+1]; 55297Seric extern struct passwd *getpwnam(); 56297Seric register char *p; 57297Seric extern char *ttypath(); 585846Seric typedef int (*fnptr)(); 59297Seric 607676Seric if (tTd(6, 1)) 6158680Seric { 6259101Seric printf("\nsavemail, errormode = %c, id = %s\n e_from=", 6359101Seric e->e_errormode, e->e_id == NULL ? "NONE" : e->e_id); 6458680Seric printaddr(&e->e_from, FALSE); 6558680Seric } 667361Seric 6759101Seric if (e->e_id == NULL) 6859101Seric { 6959101Seric /* can't return a message with no id */ 7059101Seric return; 7159101Seric } 7259101Seric 739337Seric e->e_flags &= ~EF_FATALERRS; 74297Seric 75297Seric /* 76297Seric ** In the unhappy event we don't know who to return the mail 77297Seric ** to, make someone up. 78297Seric */ 79297Seric 809337Seric if (e->e_from.q_paddr == NULL) 81297Seric { 8258733Seric e->e_sender = "Postmaster"; 8364284Seric if (parseaddr(e->e_sender, &e->e_from, 8464284Seric RF_COPYPARSE|RF_SENDERADDR, '\0', NULL, e) == NULL) 85297Seric { 8658733Seric syserr("553 Cannot parse Postmaster!"); 87297Seric ExitStat = EX_SOFTWARE; 88297Seric finis(); 89297Seric } 90297Seric } 919337Seric e->e_to = NULL; 92297Seric 93297Seric /* 9424942Seric ** Basic state machine. 9524942Seric ** 9624942Seric ** This machine runs through the following states: 9724942Seric ** 9824942Seric ** ESM_QUIET Errors have already been printed iff the 9924942Seric ** sender is local. 10024942Seric ** ESM_REPORT Report directly to the sender's terminal. 10124942Seric ** ESM_MAIL Mail response to the sender. 10224942Seric ** ESM_DEADLETTER Save response in ~/dead.letter. 10324942Seric ** ESM_POSTMASTER Mail response to the postmaster. 10424942Seric ** ESM_PANIC Save response anywhere possible. 105297Seric */ 106297Seric 10724942Seric /* determine starting state */ 10858734Seric switch (e->e_errormode) 109297Seric { 11024942Seric case EM_WRITE: 11124942Seric state = ESM_REPORT; 11224942Seric break; 11324942Seric 11424942Seric case EM_BERKNET: 11524942Seric /* mail back, but return o.k. exit status */ 116401Seric ExitStat = EX_OK; 11724942Seric 11824942Seric /* fall through.... */ 11924942Seric 12024942Seric case EM_MAIL: 12124942Seric state = ESM_MAIL; 12224942Seric break; 12324942Seric 12424942Seric case EM_PRINT: 12524979Seric case '\0': 12624942Seric state = ESM_QUIET; 12724942Seric break; 12824942Seric 12924942Seric case EM_QUIET: 13024942Seric /* no need to return anything at all */ 13124942Seric return; 13224979Seric 13324979Seric default: 13458734Seric syserr("554 savemail: bogus errormode x%x\n", e->e_errormode); 13524979Seric state = ESM_MAIL; 13624979Seric break; 137297Seric } 138297Seric 13959094Seric /* if this is already an error response, send to postmaster */ 14059094Seric if (bitset(EF_RESPONSE, e->e_flags)) 14159094Seric { 14259094Seric if (e->e_parent != NULL && 14359094Seric bitset(EF_RESPONSE, e->e_parent->e_flags)) 14459094Seric { 14559094Seric /* got an error sending a response -- can it */ 14659094Seric return; 14759094Seric } 14859094Seric state = ESM_POSTMASTER; 14959094Seric } 15059094Seric 15124942Seric while (state != ESM_DONE) 152297Seric { 15324979Seric if (tTd(6, 5)) 15424979Seric printf(" state %d\n", state); 15524979Seric 15624942Seric switch (state) 157297Seric { 15824979Seric case ESM_QUIET: 15924979Seric if (e->e_from.q_mailer == LocalMailer) 16024979Seric state = ESM_DEADLETTER; 16124979Seric else 16224979Seric state = ESM_MAIL; 16324979Seric break; 16424979Seric 16524942Seric case ESM_REPORT: 16624942Seric 16724942Seric /* 16824942Seric ** If the user is still logged in on the same terminal, 16924942Seric ** then write the error messages back to hir (sic). 17024942Seric */ 17124942Seric 17224942Seric p = ttypath(); 17324942Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 17424942Seric { 17524942Seric state = ESM_MAIL; 17624942Seric break; 17724942Seric } 17824942Seric 17958050Seric expand("\201n", buf, &buf[sizeof buf - 1], e); 1809375Seric printf("\r\nMessage from %s...\r\n", buf); 1819375Seric printf("Errors occurred while sending mail.\r\n"); 1829542Seric if (e->e_xfp != NULL) 1839375Seric { 1849542Seric (void) fflush(e->e_xfp); 18524942Seric fp = fopen(queuename(e, 'x'), "r"); 1869375Seric } 1879375Seric else 18824942Seric fp = NULL; 18924942Seric if (fp == NULL) 1909375Seric { 1919337Seric syserr("Cannot open %s", queuename(e, 'x')); 1929375Seric printf("Transcript of session is unavailable.\r\n"); 1939375Seric } 1949375Seric else 1959375Seric { 1969375Seric printf("Transcript follows:\r\n"); 19724942Seric while (fgets(buf, sizeof buf, fp) != NULL && 1989375Seric !ferror(stdout)) 1999375Seric fputs(buf, stdout); 20058680Seric (void) xfclose(fp, "savemail transcript", e->e_id); 2019375Seric } 20224942Seric printf("Original message will be saved in dead.letter.\r\n"); 20324942Seric state = ESM_DEADLETTER; 20424942Seric break; 205297Seric 20624942Seric case ESM_MAIL: 20724942Seric /* 20824942Seric ** If mailing back, do it. 20924942Seric ** Throw away all further output. Don't alias, 21024942Seric ** since this could cause loops, e.g., if joe 21124942Seric ** mails to joe@x, and for some reason the network 21224942Seric ** for @x is down, then the response gets sent to 21324942Seric ** joe@x, which gives a response, etc. Also force 21424942Seric ** the mail to be delivered even if a version of 21524942Seric ** it has already been sent to the sender. 21663841Seric ** 21763841Seric ** If this is a configuration or local software 21863841Seric ** error, send to the local postmaster as well, 21963841Seric ** since the originator can't do anything 22063841Seric ** about it anyway. Note that this is a full 22163841Seric ** copy of the message (intentionally) so that 22263841Seric ** the Postmaster can forward things along. 22324942Seric */ 224297Seric 22563841Seric if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE) 22663841Seric { 22763841Seric (void) sendtolist("postmaster", 22864284Seric NULLADDR, &e->e_errorqueue, e); 22963841Seric } 23058680Seric if (strcmp(e->e_from.q_paddr, "<>") != 0) 23163841Seric { 23258680Seric (void) sendtolist(e->e_from.q_paddr, 23364284Seric NULLADDR, &e->e_errorqueue, e); 23463841Seric } 23558680Seric 23663841Seric /* 23763841Seric ** Deliver a non-delivery report to the 23863841Seric ** Postmaster-designate (not necessarily 23963841Seric ** Postmaster). This does not include the 24063841Seric ** body of the message, for privacy reasons. 24163841Seric ** You really shouldn't need this. 24263841Seric */ 24363841Seric 24463849Seric e->e_flags |= EF_PM_NOTIFY; 24558178Seric 24658680Seric q = e->e_errorqueue; 24758680Seric if (q == NULL) 24858680Seric { 24958680Seric /* this is an error-error */ 25058680Seric state = ESM_POSTMASTER; 25158680Seric break; 25258680Seric } 25358966Seric if (returntosender(e->e_message, 25458680Seric q, (e->e_class >= 0), e) == 0) 25558680Seric { 25658680Seric state = ESM_DONE; 25758680Seric break; 25858680Seric } 25924981Seric 26058680Seric /* didn't work -- return to postmaster */ 26158680Seric state = ESM_POSTMASTER; 26258680Seric break; 26358306Seric 26458680Seric case ESM_POSTMASTER: 26558680Seric /* 26658680Seric ** Similar to previous case, but to system postmaster. 26758680Seric */ 26858680Seric 26959432Seric q = NULL; 27059432Seric if (sendtolist("postmaster", NULL, &q, e) <= 0) 27124942Seric { 27258680Seric syserr("553 cannot parse postmaster!"); 27358680Seric ExitStat = EX_SOFTWARE; 27458680Seric state = ESM_USRTMP; 27558680Seric break; 27624942Seric } 27758966Seric if (returntosender(e->e_message, 27857438Seric q, (e->e_class >= 0), e) == 0) 27924942Seric { 28024942Seric state = ESM_DONE; 28124942Seric break; 28224942Seric } 283297Seric 28458680Seric /* didn't work -- last resort */ 28558680Seric state = ESM_USRTMP; 28624942Seric break; 287297Seric 28824942Seric case ESM_DEADLETTER: 28924942Seric /* 29024942Seric ** Save the message in dead.letter. 29124942Seric ** If we weren't mailing back, and the user is 29224942Seric ** local, we should save the message in 29324942Seric ** ~/dead.letter so that the poor person doesn't 29424942Seric ** have to type it over again -- and we all know 29524942Seric ** what poor typists UNIX users are. 29624942Seric */ 2975315Seric 29824942Seric p = NULL; 29924942Seric if (e->e_from.q_mailer == LocalMailer) 30024942Seric { 30124942Seric if (e->e_from.q_home != NULL) 30224942Seric p = e->e_from.q_home; 30324942Seric else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 30424942Seric p = pw->pw_dir; 30524942Seric } 30624942Seric if (p == NULL) 30724942Seric { 30858865Seric /* no local directory */ 30924942Seric state = ESM_MAIL; 31024942Seric break; 31124942Seric } 31224942Seric if (e->e_dfp != NULL) 31324942Seric { 31424942Seric bool oldverb = Verbose; 31524942Seric 31624942Seric /* we have a home directory; open dead.letter */ 31724942Seric define('z', p, e); 31858050Seric expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e); 31924942Seric Verbose = TRUE; 32058151Seric message("Saving message in %s", buf); 32124942Seric Verbose = oldverb; 32224942Seric e->e_to = buf; 32324942Seric q = NULL; 32458082Seric (void) sendtolist(buf, &e->e_from, &q, e); 32564354Seric if (q != NULL && 32664354Seric !bitset(QBADADDR, q->q_flags) && 32764354Seric deliver(e, q) == 0) 32824942Seric state = ESM_DONE; 32924942Seric else 33024942Seric state = ESM_MAIL; 33124942Seric } 33225569Seric else 33325569Seric { 33425569Seric /* no data file -- try mailing back */ 33525569Seric state = ESM_MAIL; 33625569Seric } 33724942Seric break; 33824942Seric 33924942Seric case ESM_USRTMP: 34024942Seric /* 34124942Seric ** Log the mail in /usr/tmp/dead.letter. 34224942Seric */ 34324942Seric 34457438Seric if (e->e_class < 0) 34557438Seric { 34657438Seric state = ESM_DONE; 34757438Seric break; 34857438Seric } 34957438Seric 35059745Seric fp = dfopen("/usr/tmp/dead.letter", 35159745Seric O_WRONLY|O_CREAT|O_APPEND, FileMode); 35224942Seric if (fp == NULL) 35324942Seric { 35424942Seric state = ESM_PANIC; 35524942Seric break; 35624942Seric } 35724942Seric 35858010Seric putfromline(fp, FileMailer, e); 35958010Seric (*e->e_puthdr)(fp, FileMailer, e); 36058010Seric putline("\n", fp, FileMailer); 36159730Seric (*e->e_putbody)(fp, FileMailer, e, NULL); 36258010Seric putline("\n", fp, FileMailer); 36324942Seric (void) fflush(fp); 36424942Seric state = ferror(fp) ? ESM_PANIC : ESM_DONE; 36558680Seric (void) xfclose(fp, "savemail", "/usr/tmp/dead.letter"); 36624942Seric break; 36724942Seric 36824942Seric default: 36958151Seric syserr("554 savemail: unknown state %d", state); 37024942Seric 37124942Seric /* fall through ... */ 37224942Seric 37324942Seric case ESM_PANIC: 37424942Seric /* leave the locked queue & transcript files around */ 37558151Seric syserr("554 savemail: cannot save rejected email anywhere"); 37624942Seric exit(EX_SOFTWARE); 37724942Seric } 378297Seric } 379297Seric } 380297Seric /* 3814633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 3824633Seric ** 3834633Seric ** Parameters: 3844633Seric ** msg -- the explanatory message. 38516479Seric ** returnq -- the queue of people to send the message to. 3865984Seric ** sendbody -- if TRUE, also send back the body of the 3875984Seric ** message; otherwise just send the header. 38855012Seric ** e -- the current envelope. 3894633Seric ** 3904633Seric ** Returns: 3914633Seric ** zero -- if everything went ok. 3924633Seric ** else -- some error. 3934633Seric ** 3944633Seric ** Side Effects: 3954633Seric ** Returns the current message to the sender via 3964633Seric ** mail. 3974633Seric */ 3984633Seric 3995984Seric static bool SendBody; 4004633Seric 4017045Seric #define MAXRETURNS 6 /* max depth of returning messages */ 40258559Seric #define ERRORFUDGE 100 /* nominal size of error message text */ 4037045Seric 40455012Seric returntosender(msg, returnq, sendbody, e) 4054633Seric char *msg; 40616479Seric ADDRESS *returnq; 4075984Seric bool sendbody; 40855012Seric register ENVELOPE *e; 4094633Seric { 4104633Seric char buf[MAXNAME]; 4116978Seric extern putheader(), errbody(); 4126978Seric register ENVELOPE *ee; 41358680Seric ENVELOPE *oldcur = CurEnv; 4146978Seric ENVELOPE errenvelope; 4157045Seric static int returndepth; 4169375Seric register ADDRESS *q; 4174633Seric 41860010Seric if (returnq == NULL) 41960010Seric return (-1); 42060010Seric 42158966Seric if (msg == NULL) 42258966Seric msg = "Unable to deliver mail"; 42358966Seric 4247676Seric if (tTd(6, 1)) 4257287Seric { 42658680Seric printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=", 42755012Seric msg, returndepth, e); 42816479Seric printaddr(returnq, TRUE); 4297287Seric } 4307287Seric 4317045Seric if (++returndepth >= MAXRETURNS) 4327045Seric { 4337045Seric if (returndepth != MAXRETURNS) 43458151Seric syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr); 4357045Seric /* don't "unrecurse" and fake a clean exit */ 4367045Seric /* returndepth--; */ 4377045Seric return (0); 4387045Seric } 4397045Seric 4405984Seric SendBody = sendbody; 44158680Seric define('g', e->e_from.q_paddr, e); 44258179Seric ee = newenvelope(&errenvelope, e); 44358050Seric define('a', "\201b", ee); 44459057Seric define('r', "internal", ee); 44559057Seric define('s', "localhost", ee); 44659057Seric define('_', "localhost", ee); 4476978Seric ee->e_puthdr = putheader; 4486978Seric ee->e_putbody = errbody; 44964120Seric ee->e_flags |= EF_RESPONSE|EF_METOO; 45055012Seric if (!bitset(EF_OLDSTYLE, e->e_flags)) 45145155Seric ee->e_flags &= ~EF_OLDSTYLE; 45216479Seric ee->e_sendqueue = returnq; 45364655Seric ee->e_msgsize = ERRORFUDGE; 45464655Seric if (!NoReturn) 45564655Seric ee->e_msgsize += e->e_msgsize; 4569542Seric openxscript(ee); 45716479Seric for (q = returnq; q != NULL; q = q->q_next) 4589375Seric { 45959989Seric if (bitset(QBADADDR, q->q_flags)) 46058559Seric continue; 46158559Seric 46259989Seric if (!bitset(QDONTSEND, q->q_flags)) 46359989Seric ee->e_nrcpts++; 46458559Seric 46558144Seric if (!DontPruneRoutes && pruneroute(q->q_paddr)) 46664284Seric parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e); 46758144Seric 4689375Seric if (q->q_alias == NULL) 46959580Seric addheader("To", q->q_paddr, ee); 4709375Seric } 47124942Seric 47257642Seric # ifdef LOG 47358020Seric if (LogLevel > 5) 47457642Seric syslog(LOG_INFO, "%s: %s: return to sender: %s", 47557642Seric e->e_id, ee->e_id, msg); 47657642Seric # endif 47757642Seric 47810845Seric (void) sprintf(buf, "Returned mail: %s", msg); 47959580Seric addheader("Subject", buf, ee); 48059730Seric if (SendMIMEErrors) 48159730Seric { 48259987Seric addheader("MIME-Version", "1.0", ee); 48359730Seric (void) sprintf(buf, "%s.%ld/%s", 48459730Seric ee->e_id, curtime(), MyHostName); 48559730Seric ee->e_msgboundary = newstr(buf); 48659730Seric (void) sprintf(buf, "multipart/mixed; boundary=\"%s\"", 48759730Seric ee->e_msgboundary); 48859730Seric addheader("Content-Type", buf, ee); 48959730Seric } 4904633Seric 4914633Seric /* fake up an address header for the from person */ 49258050Seric expand("\201n", buf, &buf[sizeof buf - 1], e); 49364284Seric if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL) 4944633Seric { 49558151Seric syserr("553 Can't parse myself!"); 4964633Seric ExitStat = EX_SOFTWARE; 4977045Seric returndepth--; 4984633Seric return (-1); 4994633Seric } 50058704Seric ee->e_sender = ee->e_from.q_paddr; 5015984Seric 5026978Seric /* push state into submessage */ 5036978Seric CurEnv = ee; 50458050Seric define('f', "\201n", ee); 5059375Seric define('x', "Mail Delivery Subsystem", ee); 50658929Seric eatheader(ee, TRUE); 5075984Seric 50863753Seric /* mark statistics */ 50964284Seric markstats(ee, NULLADDR); 51063753Seric 5116978Seric /* actually deliver the error message */ 51214876Seric sendall(ee, SM_DEFAULT); 5136978Seric 5146978Seric /* restore state */ 5157811Seric dropenvelope(ee); 51658680Seric CurEnv = oldcur; 5177045Seric returndepth--; 5186978Seric 5197045Seric /* should check for delivery errors here */ 5204633Seric return (0); 5214633Seric } 5224633Seric /* 5236978Seric ** ERRBODY -- output the body of an error message. 5246978Seric ** 5256978Seric ** Typically this is a copy of the transcript plus a copy of the 5266978Seric ** original offending message. 5276978Seric ** 528297Seric ** Parameters: 529297Seric ** fp -- the output file. 53010170Seric ** m -- the mailer to output to. 5319542Seric ** e -- the envelope we are working in. 532297Seric ** 533297Seric ** Returns: 534297Seric ** none 535297Seric ** 536297Seric ** Side Effects: 5376978Seric ** Outputs the body of an error message. 538297Seric */ 539297Seric 54010170Seric errbody(fp, m, e) 541297Seric register FILE *fp; 5424318Seric register struct mailer *m; 5439542Seric register ENVELOPE *e; 544297Seric { 5456978Seric register FILE *xfile; 54659082Seric char *p; 54759082Seric register ADDRESS *q; 54859082Seric bool printheader; 5493189Seric char buf[MAXLINE]; 550297Seric 55158680Seric if (e->e_parent == NULL) 55258680Seric { 55358680Seric syserr("errbody: null parent"); 55458680Seric putline(" ----- Original message lost -----\n", fp, m); 55558680Seric return; 55658680Seric } 55758680Seric 5589057Seric /* 55959730Seric ** Output MIME header. 56059730Seric */ 56159730Seric 56259730Seric if (e->e_msgboundary != NULL) 56359730Seric { 56459730Seric putline("This is a MIME-encapsulated message", fp, m); 56559730Seric putline("", fp, m); 56659730Seric (void) sprintf(buf, "--%s", e->e_msgboundary); 56759730Seric putline(buf, fp, m); 56859730Seric putline("", fp, m); 56959730Seric } 57059730Seric 57159730Seric /* 57263852Seric ** Output introductory information. 57363852Seric */ 57463852Seric 575*64718Seric for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 576*64718Seric if (bitset(QBADADDR, q->q_flags)) 577*64718Seric break; 578*64718Seric if (q == NULL) 579*64718Seric { 580*64718Seric putline(" **********************************************", 581*64718Seric fp, m); 582*64718Seric putline(" ** THIS IS A WARNING MESSAGE ONLY **", 583*64718Seric fp, m); 584*64718Seric putline(" ** YOU DO NOT NEED TO RESEND YOUR MESSAGE **", 585*64718Seric fp, m); 586*64718Seric putline(" **********************************************", 587*64718Seric fp, m); 588*64718Seric putline("", fp, m); 589*64718Seric } 590*64718Seric sprintf(buf, "The original message was received at %s", 591*64718Seric arpadate(ctime(&e->e_parent->e_ctime))); 59263852Seric putline(buf, fp, m); 59364025Seric expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent); 59463852Seric putline(buf, fp, m); 59563852Seric putline("", fp, m); 59663852Seric 59763852Seric /* 59855372Seric ** Output error message header (if specified and available). 59955372Seric */ 60055372Seric 60155372Seric if (ErrMsgFile != NULL) 60255372Seric { 60355372Seric if (*ErrMsgFile == '/') 60455372Seric { 60555372Seric xfile = fopen(ErrMsgFile, "r"); 60655372Seric if (xfile != NULL) 60755372Seric { 60855372Seric while (fgets(buf, sizeof buf, xfile) != NULL) 60955425Seric { 61055425Seric expand(buf, buf, &buf[sizeof buf - 1], e); 61155372Seric putline(buf, fp, m); 61255425Seric } 61355372Seric (void) fclose(xfile); 61459082Seric putline("\n", fp, m); 61555372Seric } 61655372Seric } 61755372Seric else 61855372Seric { 61955425Seric expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e); 62055425Seric putline(buf, fp, m); 62159730Seric putline("", fp, m); 62255372Seric } 62355372Seric } 62455372Seric 62555372Seric /* 62659082Seric ** Output message introduction 62759082Seric */ 62859082Seric 62959082Seric printheader = TRUE; 63059082Seric for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 63159082Seric { 63263787Seric if (bitset(QBADADDR|QREPORT, q->q_flags)) 63359082Seric { 63459082Seric if (printheader) 63559082Seric { 63663787Seric putline(" ----- The following addresses had delivery problems -----", 63759082Seric fp, m); 63859082Seric printheader = FALSE; 63959082Seric } 64063849Seric strcpy(buf, q->q_paddr); 64163787Seric if (bitset(QBADADDR, q->q_flags)) 64263849Seric strcat(buf, " (unrecoverable error)"); 64363787Seric else 64463849Seric strcat(buf, " (transient failure)"); 64563787Seric putline(buf, fp, m); 64663849Seric if (q->q_alias != NULL) 64763849Seric { 64863849Seric strcpy(buf, " (expanded from: "); 64963849Seric strcat(buf, q->q_alias->q_paddr); 65063849Seric strcat(buf, ")"); 65163849Seric putline(buf, fp, m); 65263849Seric } 65359082Seric } 65459082Seric } 65559082Seric if (!printheader) 65659082Seric putline("\n", fp, m); 65759082Seric 65859082Seric /* 6599057Seric ** Output transcript of errors 6609057Seric */ 6619057Seric 6624086Seric (void) fflush(stdout); 6639542Seric p = queuename(e->e_parent, 'x'); 6649337Seric if ((xfile = fopen(p, "r")) == NULL) 6659057Seric { 6669337Seric syserr("Cannot open %s", p); 66759082Seric putline(" ----- Transcript of session is unavailable -----\n", fp, m); 6689057Seric } 6699057Seric else 6709057Seric { 67158680Seric putline(" ----- Transcript of session follows -----\n", fp, m); 6729542Seric if (e->e_xfp != NULL) 6739542Seric (void) fflush(e->e_xfp); 6749057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 67510170Seric putline(buf, fp, m); 67658680Seric (void) xfclose(xfile, "errbody xscript", p); 6779057Seric } 678297Seric errno = 0; 6794318Seric 6804318Seric /* 6814318Seric ** Output text of original message 6824318Seric */ 6834318Seric 6844289Seric if (NoReturn) 68558665Seric SendBody = FALSE; 68659730Seric putline("", fp, m); 68758680Seric if (e->e_parent->e_df != NULL) 6884199Seric { 6895984Seric if (SendBody) 69063852Seric putline(" ----- Original message follows -----\n", fp, m); 6915984Seric else 69259987Seric putline(" ----- Message header follows -----\n", fp, m); 69359730Seric (void) fflush(fp); 69459730Seric 69559730Seric if (e->e_msgboundary != NULL) 6965984Seric { 69759730Seric putline("", fp, m); 69859730Seric (void) sprintf(buf, "--%s", e->e_msgboundary); 69959730Seric putline(buf, fp, m); 70059987Seric putline("Content-Type: message/rfc822", fp, m); 70159730Seric putline("", fp, m); 7025984Seric } 70359730Seric putheader(fp, m, e->e_parent); 70459730Seric putline("", fp, m); 70559730Seric if (SendBody) 70659730Seric putbody(fp, m, e->e_parent, e->e_msgboundary); 70759987Seric else 70860010Seric putline(" ----- Message body suppressed -----", fp, m); 7094199Seric } 7104199Seric else 71110170Seric { 71210170Seric putline(" ----- No message was collected -----\n", fp, m); 71310170Seric } 7144318Seric 71560010Seric if (e->e_msgboundary != NULL) 71660010Seric { 71760010Seric putline("", fp, m); 71860010Seric (void) sprintf(buf, "--%s--", e->e_msgboundary); 71960010Seric putline(buf, fp, m); 72060010Seric } 72159730Seric putline("", fp, m); 72259730Seric 7234318Seric /* 7244318Seric ** Cleanup and exit 7254318Seric */ 7264318Seric 727297Seric if (errno != 0) 7286978Seric syserr("errbody: I/O error"); 729297Seric } 73058144Seric /* 73158144Seric ** PRUNEROUTE -- prune an RFC-822 source route 73258144Seric ** 73358144Seric ** Trims down a source route to the last internet-registered hop. 73458144Seric ** This is encouraged by RFC 1123 section 5.3.3. 73558144Seric ** 73658144Seric ** Parameters: 73758144Seric ** addr -- the address 73858144Seric ** 73958144Seric ** Returns: 74058144Seric ** TRUE -- address was modified 74158144Seric ** FALSE -- address could not be pruned 74258144Seric ** 74358144Seric ** Side Effects: 74458144Seric ** modifies addr in-place 74558144Seric */ 74658144Seric 74758144Seric pruneroute(addr) 74858144Seric char *addr; 74958144Seric { 75058144Seric #ifdef NAMED_BIND 75158144Seric char *start, *at, *comma; 75258144Seric char c; 75358144Seric int rcode; 75458144Seric char hostbuf[BUFSIZ]; 75558144Seric char *mxhosts[MAXMXHOSTS + 1]; 75658144Seric 75758144Seric /* check to see if this is really a route-addr */ 75858144Seric if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>') 75958144Seric return FALSE; 76058144Seric start = strchr(addr, ':'); 76158144Seric at = strrchr(addr, '@'); 76258144Seric if (start == NULL || at == NULL || at < start) 76358144Seric return FALSE; 76458144Seric 76558144Seric /* slice off the angle brackets */ 76658144Seric strcpy(hostbuf, at + 1); 76758144Seric hostbuf[strlen(hostbuf) - 1] = '\0'; 76858144Seric 76958144Seric while (start) 77058144Seric { 77159273Seric if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0) 77258144Seric { 77358144Seric strcpy(addr + 1, start + 1); 77458144Seric return TRUE; 77558144Seric } 77658144Seric c = *start; 77758144Seric *start = '\0'; 77858144Seric comma = strrchr(addr, ','); 77958144Seric if (comma && comma[1] == '@') 78058144Seric strcpy(hostbuf, comma + 2); 78158144Seric else 78258144Seric comma = 0; 78358144Seric *start = c; 78458144Seric start = comma; 78558144Seric } 78658144Seric #endif 78758144Seric return FALSE; 78858144Seric } 789