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*64792Seric static char sccsid[] = "@(#)savemail.c 8.17 (Berkeley) 10/31/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 73297Seric /* 74297Seric ** In the unhappy event we don't know who to return the mail 75297Seric ** to, make someone up. 76297Seric */ 77297Seric 789337Seric if (e->e_from.q_paddr == NULL) 79297Seric { 8058733Seric e->e_sender = "Postmaster"; 8164284Seric if (parseaddr(e->e_sender, &e->e_from, 8264284Seric RF_COPYPARSE|RF_SENDERADDR, '\0', NULL, e) == NULL) 83297Seric { 8458733Seric syserr("553 Cannot parse Postmaster!"); 85297Seric ExitStat = EX_SOFTWARE; 86297Seric finis(); 87297Seric } 88297Seric } 899337Seric e->e_to = NULL; 90297Seric 91297Seric /* 9224942Seric ** Basic state machine. 9324942Seric ** 9424942Seric ** This machine runs through the following states: 9524942Seric ** 9624942Seric ** ESM_QUIET Errors have already been printed iff the 9724942Seric ** sender is local. 9824942Seric ** ESM_REPORT Report directly to the sender's terminal. 9924942Seric ** ESM_MAIL Mail response to the sender. 10024942Seric ** ESM_DEADLETTER Save response in ~/dead.letter. 10124942Seric ** ESM_POSTMASTER Mail response to the postmaster. 10224942Seric ** ESM_PANIC Save response anywhere possible. 103297Seric */ 104297Seric 10524942Seric /* determine starting state */ 10658734Seric switch (e->e_errormode) 107297Seric { 10824942Seric case EM_WRITE: 10924942Seric state = ESM_REPORT; 11024942Seric break; 11124942Seric 11224942Seric case EM_BERKNET: 11324942Seric /* mail back, but return o.k. exit status */ 114401Seric ExitStat = EX_OK; 11524942Seric 11624942Seric /* fall through.... */ 11724942Seric 11824942Seric case EM_MAIL: 11924942Seric state = ESM_MAIL; 12024942Seric break; 12124942Seric 12224942Seric case EM_PRINT: 12324979Seric case '\0': 12424942Seric state = ESM_QUIET; 12524942Seric break; 12624942Seric 12724942Seric case EM_QUIET: 12824942Seric /* no need to return anything at all */ 12924942Seric return; 13024979Seric 13124979Seric default: 13258734Seric syserr("554 savemail: bogus errormode x%x\n", e->e_errormode); 13324979Seric state = ESM_MAIL; 13424979Seric break; 135297Seric } 136297Seric 13759094Seric /* if this is already an error response, send to postmaster */ 13859094Seric if (bitset(EF_RESPONSE, e->e_flags)) 13959094Seric { 14059094Seric if (e->e_parent != NULL && 14159094Seric bitset(EF_RESPONSE, e->e_parent->e_flags)) 14259094Seric { 14359094Seric /* got an error sending a response -- can it */ 14459094Seric return; 14559094Seric } 14659094Seric state = ESM_POSTMASTER; 14759094Seric } 14859094Seric 14924942Seric while (state != ESM_DONE) 150297Seric { 15124979Seric if (tTd(6, 5)) 15224979Seric printf(" state %d\n", state); 15324979Seric 15424942Seric switch (state) 155297Seric { 15624979Seric case ESM_QUIET: 15724979Seric if (e->e_from.q_mailer == LocalMailer) 15824979Seric state = ESM_DEADLETTER; 15924979Seric else 16024979Seric state = ESM_MAIL; 16124979Seric break; 16224979Seric 16324942Seric case ESM_REPORT: 16424942Seric 16524942Seric /* 16624942Seric ** If the user is still logged in on the same terminal, 16724942Seric ** then write the error messages back to hir (sic). 16824942Seric */ 16924942Seric 17024942Seric p = ttypath(); 17124942Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 17224942Seric { 17324942Seric state = ESM_MAIL; 17424942Seric break; 17524942Seric } 17624942Seric 17758050Seric expand("\201n", buf, &buf[sizeof buf - 1], e); 1789375Seric printf("\r\nMessage from %s...\r\n", buf); 1799375Seric printf("Errors occurred while sending mail.\r\n"); 1809542Seric if (e->e_xfp != NULL) 1819375Seric { 1829542Seric (void) fflush(e->e_xfp); 18324942Seric fp = fopen(queuename(e, 'x'), "r"); 1849375Seric } 1859375Seric else 18624942Seric fp = NULL; 18724942Seric if (fp == NULL) 1889375Seric { 1899337Seric syserr("Cannot open %s", queuename(e, 'x')); 1909375Seric printf("Transcript of session is unavailable.\r\n"); 1919375Seric } 1929375Seric else 1939375Seric { 1949375Seric printf("Transcript follows:\r\n"); 19524942Seric while (fgets(buf, sizeof buf, fp) != NULL && 1969375Seric !ferror(stdout)) 1979375Seric fputs(buf, stdout); 19858680Seric (void) xfclose(fp, "savemail transcript", e->e_id); 1999375Seric } 20024942Seric printf("Original message will be saved in dead.letter.\r\n"); 20124942Seric state = ESM_DEADLETTER; 20224942Seric break; 203297Seric 20424942Seric case ESM_MAIL: 20524942Seric /* 20624942Seric ** If mailing back, do it. 20724942Seric ** Throw away all further output. Don't alias, 20824942Seric ** since this could cause loops, e.g., if joe 20924942Seric ** mails to joe@x, and for some reason the network 21024942Seric ** for @x is down, then the response gets sent to 21124942Seric ** joe@x, which gives a response, etc. Also force 21224942Seric ** the mail to be delivered even if a version of 21324942Seric ** it has already been sent to the sender. 21463841Seric ** 21563841Seric ** If this is a configuration or local software 21663841Seric ** error, send to the local postmaster as well, 21763841Seric ** since the originator can't do anything 21863841Seric ** about it anyway. Note that this is a full 21963841Seric ** copy of the message (intentionally) so that 22063841Seric ** the Postmaster can forward things along. 22124942Seric */ 222297Seric 22363841Seric if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE) 22463841Seric { 22563841Seric (void) sendtolist("postmaster", 22664284Seric NULLADDR, &e->e_errorqueue, e); 22763841Seric } 22858680Seric if (strcmp(e->e_from.q_paddr, "<>") != 0) 22963841Seric { 23058680Seric (void) sendtolist(e->e_from.q_paddr, 23164284Seric NULLADDR, &e->e_errorqueue, e); 23263841Seric } 23358680Seric 23463841Seric /* 23563841Seric ** Deliver a non-delivery report to the 23663841Seric ** Postmaster-designate (not necessarily 23763841Seric ** Postmaster). This does not include the 23863841Seric ** body of the message, for privacy reasons. 23963841Seric ** You really shouldn't need this. 24063841Seric */ 24163841Seric 24263849Seric e->e_flags |= EF_PM_NOTIFY; 24358178Seric 244*64792Seric /* check to see if there are any good addresses */ 245*64792Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 246*64792Seric if (!bitset(QBADADDR|QDONTSEND, q->q_flags)) 247*64792Seric break; 24858680Seric if (q == NULL) 24958680Seric { 25058680Seric /* this is an error-error */ 25158680Seric state = ESM_POSTMASTER; 25258680Seric break; 25358680Seric } 25458966Seric if (returntosender(e->e_message, 25558680Seric q, (e->e_class >= 0), e) == 0) 25658680Seric { 25758680Seric state = ESM_DONE; 25858680Seric break; 25958680Seric } 26024981Seric 26158680Seric /* didn't work -- return to postmaster */ 26258680Seric state = ESM_POSTMASTER; 26358680Seric break; 26458306Seric 26558680Seric case ESM_POSTMASTER: 26658680Seric /* 26758680Seric ** Similar to previous case, but to system postmaster. 26858680Seric */ 26958680Seric 27059432Seric q = NULL; 27159432Seric if (sendtolist("postmaster", NULL, &q, e) <= 0) 27224942Seric { 27358680Seric syserr("553 cannot parse postmaster!"); 27458680Seric ExitStat = EX_SOFTWARE; 27558680Seric state = ESM_USRTMP; 27658680Seric break; 27724942Seric } 27858966Seric if (returntosender(e->e_message, 27957438Seric q, (e->e_class >= 0), e) == 0) 28024942Seric { 28124942Seric state = ESM_DONE; 28224942Seric break; 28324942Seric } 284297Seric 28558680Seric /* didn't work -- last resort */ 28658680Seric state = ESM_USRTMP; 28724942Seric break; 288297Seric 28924942Seric case ESM_DEADLETTER: 29024942Seric /* 29124942Seric ** Save the message in dead.letter. 29224942Seric ** If we weren't mailing back, and the user is 29324942Seric ** local, we should save the message in 29424942Seric ** ~/dead.letter so that the poor person doesn't 29524942Seric ** have to type it over again -- and we all know 29624942Seric ** what poor typists UNIX users are. 29724942Seric */ 2985315Seric 29924942Seric p = NULL; 30024942Seric if (e->e_from.q_mailer == LocalMailer) 30124942Seric { 30224942Seric if (e->e_from.q_home != NULL) 30324942Seric p = e->e_from.q_home; 30424942Seric else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 30524942Seric p = pw->pw_dir; 30624942Seric } 30724942Seric if (p == NULL) 30824942Seric { 30958865Seric /* no local directory */ 31024942Seric state = ESM_MAIL; 31124942Seric break; 31224942Seric } 31324942Seric if (e->e_dfp != NULL) 31424942Seric { 31524942Seric bool oldverb = Verbose; 31624942Seric 31724942Seric /* we have a home directory; open dead.letter */ 31824942Seric define('z', p, e); 31958050Seric expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e); 32024942Seric Verbose = TRUE; 32158151Seric message("Saving message in %s", buf); 32224942Seric Verbose = oldverb; 32324942Seric e->e_to = buf; 32424942Seric q = NULL; 32558082Seric (void) sendtolist(buf, &e->e_from, &q, e); 32664354Seric if (q != NULL && 32764354Seric !bitset(QBADADDR, q->q_flags) && 32864354Seric deliver(e, q) == 0) 32924942Seric state = ESM_DONE; 33024942Seric else 33124942Seric state = ESM_MAIL; 33224942Seric } 33325569Seric else 33425569Seric { 33525569Seric /* no data file -- try mailing back */ 33625569Seric state = ESM_MAIL; 33725569Seric } 33824942Seric break; 33924942Seric 34024942Seric case ESM_USRTMP: 34124942Seric /* 34224942Seric ** Log the mail in /usr/tmp/dead.letter. 34324942Seric */ 34424942Seric 34557438Seric if (e->e_class < 0) 34657438Seric { 34757438Seric state = ESM_DONE; 34857438Seric break; 34957438Seric } 35057438Seric 35159745Seric fp = dfopen("/usr/tmp/dead.letter", 35259745Seric O_WRONLY|O_CREAT|O_APPEND, FileMode); 35324942Seric if (fp == NULL) 35424942Seric { 35524942Seric state = ESM_PANIC; 35624942Seric break; 35724942Seric } 35824942Seric 35958010Seric putfromline(fp, FileMailer, e); 36058010Seric (*e->e_puthdr)(fp, FileMailer, e); 36158010Seric putline("\n", fp, FileMailer); 36259730Seric (*e->e_putbody)(fp, FileMailer, e, NULL); 36358010Seric putline("\n", fp, FileMailer); 36424942Seric (void) fflush(fp); 36524942Seric state = ferror(fp) ? ESM_PANIC : ESM_DONE; 36658680Seric (void) xfclose(fp, "savemail", "/usr/tmp/dead.letter"); 36724942Seric break; 36824942Seric 36924942Seric default: 37058151Seric syserr("554 savemail: unknown state %d", state); 37124942Seric 37224942Seric /* fall through ... */ 37324942Seric 37424942Seric case ESM_PANIC: 37524942Seric /* leave the locked queue & transcript files around */ 37658151Seric syserr("554 savemail: cannot save rejected email anywhere"); 37724942Seric exit(EX_SOFTWARE); 37824942Seric } 379297Seric } 380297Seric } 381297Seric /* 3824633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 3834633Seric ** 3844633Seric ** Parameters: 3854633Seric ** msg -- the explanatory message. 38616479Seric ** returnq -- the queue of people to send the message to. 3875984Seric ** sendbody -- if TRUE, also send back the body of the 3885984Seric ** message; otherwise just send the header. 38955012Seric ** e -- the current envelope. 3904633Seric ** 3914633Seric ** Returns: 3924633Seric ** zero -- if everything went ok. 3934633Seric ** else -- some error. 3944633Seric ** 3954633Seric ** Side Effects: 3964633Seric ** Returns the current message to the sender via 3974633Seric ** mail. 3984633Seric */ 3994633Seric 4005984Seric static bool SendBody; 4014633Seric 4027045Seric #define MAXRETURNS 6 /* max depth of returning messages */ 40358559Seric #define ERRORFUDGE 100 /* nominal size of error message text */ 4047045Seric 40555012Seric returntosender(msg, returnq, sendbody, e) 4064633Seric char *msg; 40716479Seric ADDRESS *returnq; 4085984Seric bool sendbody; 40955012Seric register ENVELOPE *e; 4104633Seric { 4114633Seric char buf[MAXNAME]; 4126978Seric extern putheader(), errbody(); 4136978Seric register ENVELOPE *ee; 41458680Seric ENVELOPE *oldcur = CurEnv; 4156978Seric ENVELOPE errenvelope; 4167045Seric static int returndepth; 4179375Seric register ADDRESS *q; 4184633Seric 41960010Seric if (returnq == NULL) 42060010Seric return (-1); 42160010Seric 42258966Seric if (msg == NULL) 42358966Seric msg = "Unable to deliver mail"; 42458966Seric 4257676Seric if (tTd(6, 1)) 4267287Seric { 42758680Seric printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=", 42855012Seric msg, returndepth, e); 42916479Seric printaddr(returnq, TRUE); 4307287Seric } 4317287Seric 4327045Seric if (++returndepth >= MAXRETURNS) 4337045Seric { 4347045Seric if (returndepth != MAXRETURNS) 43558151Seric syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr); 4367045Seric /* don't "unrecurse" and fake a clean exit */ 4377045Seric /* returndepth--; */ 4387045Seric return (0); 4397045Seric } 4407045Seric 4415984Seric SendBody = sendbody; 44258680Seric define('g', e->e_from.q_paddr, e); 44358179Seric ee = newenvelope(&errenvelope, e); 44458050Seric define('a', "\201b", ee); 44559057Seric define('r', "internal", ee); 44659057Seric define('s', "localhost", ee); 44759057Seric define('_', "localhost", ee); 4486978Seric ee->e_puthdr = putheader; 4496978Seric ee->e_putbody = errbody; 45064120Seric ee->e_flags |= EF_RESPONSE|EF_METOO; 45155012Seric if (!bitset(EF_OLDSTYLE, e->e_flags)) 45245155Seric ee->e_flags &= ~EF_OLDSTYLE; 45316479Seric ee->e_sendqueue = returnq; 45464655Seric ee->e_msgsize = ERRORFUDGE; 45564655Seric if (!NoReturn) 45664655Seric ee->e_msgsize += e->e_msgsize; 45764737Seric initsys(ee); 45816479Seric for (q = returnq; q != NULL; q = q->q_next) 4599375Seric { 46059989Seric if (bitset(QBADADDR, q->q_flags)) 46158559Seric continue; 46258559Seric 46359989Seric if (!bitset(QDONTSEND, q->q_flags)) 46459989Seric ee->e_nrcpts++; 46558559Seric 46658144Seric if (!DontPruneRoutes && pruneroute(q->q_paddr)) 46764284Seric parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e); 46858144Seric 4699375Seric if (q->q_alias == NULL) 47059580Seric addheader("To", q->q_paddr, ee); 4719375Seric } 47224942Seric 47357642Seric # ifdef LOG 47458020Seric if (LogLevel > 5) 47557642Seric syslog(LOG_INFO, "%s: %s: return to sender: %s", 47657642Seric e->e_id, ee->e_id, msg); 47757642Seric # endif 47857642Seric 47910845Seric (void) sprintf(buf, "Returned mail: %s", msg); 48059580Seric addheader("Subject", buf, ee); 48159730Seric if (SendMIMEErrors) 48259730Seric { 48359987Seric addheader("MIME-Version", "1.0", ee); 48459730Seric (void) sprintf(buf, "%s.%ld/%s", 48559730Seric ee->e_id, curtime(), MyHostName); 48659730Seric ee->e_msgboundary = newstr(buf); 48759730Seric (void) sprintf(buf, "multipart/mixed; boundary=\"%s\"", 48859730Seric ee->e_msgboundary); 48959730Seric addheader("Content-Type", buf, ee); 49059730Seric } 4914633Seric 4924633Seric /* fake up an address header for the from person */ 49358050Seric expand("\201n", buf, &buf[sizeof buf - 1], e); 49464284Seric if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL) 4954633Seric { 49658151Seric syserr("553 Can't parse myself!"); 4974633Seric ExitStat = EX_SOFTWARE; 4987045Seric returndepth--; 4994633Seric return (-1); 5004633Seric } 50158704Seric ee->e_sender = ee->e_from.q_paddr; 5025984Seric 5036978Seric /* push state into submessage */ 5046978Seric CurEnv = ee; 50558050Seric define('f', "\201n", ee); 5069375Seric define('x', "Mail Delivery Subsystem", ee); 50758929Seric eatheader(ee, TRUE); 5085984Seric 50963753Seric /* mark statistics */ 51064284Seric markstats(ee, NULLADDR); 51163753Seric 5126978Seric /* actually deliver the error message */ 51314876Seric sendall(ee, SM_DEFAULT); 5146978Seric 5156978Seric /* restore state */ 5167811Seric dropenvelope(ee); 51758680Seric CurEnv = oldcur; 5187045Seric returndepth--; 5196978Seric 5207045Seric /* should check for delivery errors here */ 5214633Seric return (0); 5224633Seric } 5234633Seric /* 5246978Seric ** ERRBODY -- output the body of an error message. 5256978Seric ** 5266978Seric ** Typically this is a copy of the transcript plus a copy of the 5276978Seric ** original offending message. 5286978Seric ** 529297Seric ** Parameters: 530297Seric ** fp -- the output file. 53110170Seric ** m -- the mailer to output to. 5329542Seric ** e -- the envelope we are working in. 533297Seric ** 534297Seric ** Returns: 535297Seric ** none 536297Seric ** 537297Seric ** Side Effects: 5386978Seric ** Outputs the body of an error message. 539297Seric */ 540297Seric 54110170Seric errbody(fp, m, e) 542297Seric register FILE *fp; 5434318Seric register struct mailer *m; 5449542Seric register ENVELOPE *e; 545297Seric { 5466978Seric register FILE *xfile; 54759082Seric char *p; 54859082Seric register ADDRESS *q; 54959082Seric bool printheader; 5503189Seric char buf[MAXLINE]; 551297Seric 55258680Seric if (e->e_parent == NULL) 55358680Seric { 55458680Seric syserr("errbody: null parent"); 55558680Seric putline(" ----- Original message lost -----\n", fp, m); 55658680Seric return; 55758680Seric } 55858680Seric 5599057Seric /* 56059730Seric ** Output MIME header. 56159730Seric */ 56259730Seric 56359730Seric if (e->e_msgboundary != NULL) 56459730Seric { 56559730Seric putline("This is a MIME-encapsulated message", fp, m); 56659730Seric putline("", fp, m); 56759730Seric (void) sprintf(buf, "--%s", e->e_msgboundary); 56859730Seric putline(buf, fp, m); 56959730Seric putline("", fp, m); 57059730Seric } 57159730Seric 57259730Seric /* 57363852Seric ** Output introductory information. 57463852Seric */ 57563852Seric 57664718Seric for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 57764718Seric if (bitset(QBADADDR, q->q_flags)) 57864718Seric break; 57964726Seric if (q == NULL && !bitset(EF_FATALERRS, e->e_parent->e_flags)) 58064718Seric { 58164718Seric putline(" **********************************************", 58264718Seric fp, m); 58364718Seric putline(" ** THIS IS A WARNING MESSAGE ONLY **", 58464718Seric fp, m); 58564718Seric putline(" ** YOU DO NOT NEED TO RESEND YOUR MESSAGE **", 58664718Seric fp, m); 58764718Seric putline(" **********************************************", 58864718Seric fp, m); 58964718Seric putline("", fp, m); 59064718Seric } 59164718Seric sprintf(buf, "The original message was received at %s", 59264718Seric arpadate(ctime(&e->e_parent->e_ctime))); 59363852Seric putline(buf, fp, m); 59464025Seric expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent); 59563852Seric putline(buf, fp, m); 59663852Seric putline("", fp, m); 59763852Seric 59863852Seric /* 59955372Seric ** Output error message header (if specified and available). 60055372Seric */ 60155372Seric 60255372Seric if (ErrMsgFile != NULL) 60355372Seric { 60455372Seric if (*ErrMsgFile == '/') 60555372Seric { 60655372Seric xfile = fopen(ErrMsgFile, "r"); 60755372Seric if (xfile != NULL) 60855372Seric { 60955372Seric while (fgets(buf, sizeof buf, xfile) != NULL) 61055425Seric { 61155425Seric expand(buf, buf, &buf[sizeof buf - 1], e); 61255372Seric putline(buf, fp, m); 61355425Seric } 61455372Seric (void) fclose(xfile); 61559082Seric putline("\n", fp, m); 61655372Seric } 61755372Seric } 61855372Seric else 61955372Seric { 62055425Seric expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e); 62155425Seric putline(buf, fp, m); 62259730Seric putline("", fp, m); 62355372Seric } 62455372Seric } 62555372Seric 62655372Seric /* 62759082Seric ** Output message introduction 62859082Seric */ 62959082Seric 63059082Seric printheader = TRUE; 63159082Seric for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 63259082Seric { 63363787Seric if (bitset(QBADADDR|QREPORT, q->q_flags)) 63459082Seric { 63559082Seric if (printheader) 63659082Seric { 63763787Seric putline(" ----- The following addresses had delivery problems -----", 63859082Seric fp, m); 63959082Seric printheader = FALSE; 64059082Seric } 64163849Seric strcpy(buf, q->q_paddr); 64263787Seric if (bitset(QBADADDR, q->q_flags)) 64363849Seric strcat(buf, " (unrecoverable error)"); 64463787Seric else 64563849Seric strcat(buf, " (transient failure)"); 64663787Seric putline(buf, fp, m); 64763849Seric if (q->q_alias != NULL) 64863849Seric { 64963849Seric strcpy(buf, " (expanded from: "); 65063849Seric strcat(buf, q->q_alias->q_paddr); 65163849Seric strcat(buf, ")"); 65263849Seric putline(buf, fp, m); 65363849Seric } 65459082Seric } 65559082Seric } 65659082Seric if (!printheader) 65759082Seric putline("\n", fp, m); 65859082Seric 65959082Seric /* 6609057Seric ** Output transcript of errors 6619057Seric */ 6629057Seric 6634086Seric (void) fflush(stdout); 6649542Seric p = queuename(e->e_parent, 'x'); 6659337Seric if ((xfile = fopen(p, "r")) == NULL) 6669057Seric { 6679337Seric syserr("Cannot open %s", p); 66859082Seric putline(" ----- Transcript of session is unavailable -----\n", fp, m); 6699057Seric } 6709057Seric else 6719057Seric { 67258680Seric putline(" ----- Transcript of session follows -----\n", fp, m); 6739542Seric if (e->e_xfp != NULL) 6749542Seric (void) fflush(e->e_xfp); 6759057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 67610170Seric putline(buf, fp, m); 67758680Seric (void) xfclose(xfile, "errbody xscript", p); 6789057Seric } 679297Seric errno = 0; 6804318Seric 6814318Seric /* 6824318Seric ** Output text of original message 6834318Seric */ 6844318Seric 6854289Seric if (NoReturn) 68658665Seric SendBody = FALSE; 68759730Seric putline("", fp, m); 68858680Seric if (e->e_parent->e_df != NULL) 6894199Seric { 6905984Seric if (SendBody) 69163852Seric putline(" ----- Original message follows -----\n", fp, m); 6925984Seric else 69359987Seric putline(" ----- Message header follows -----\n", fp, m); 69459730Seric (void) fflush(fp); 69559730Seric 69659730Seric if (e->e_msgboundary != NULL) 6975984Seric { 69859730Seric putline("", fp, m); 69959730Seric (void) sprintf(buf, "--%s", e->e_msgboundary); 70059730Seric putline(buf, fp, m); 70159987Seric putline("Content-Type: message/rfc822", fp, m); 70259730Seric putline("", fp, m); 7035984Seric } 70459730Seric putheader(fp, m, e->e_parent); 70559730Seric putline("", fp, m); 70659730Seric if (SendBody) 70759730Seric putbody(fp, m, e->e_parent, e->e_msgboundary); 70859987Seric else 70960010Seric putline(" ----- Message body suppressed -----", fp, m); 7104199Seric } 7114199Seric else 71210170Seric { 71310170Seric putline(" ----- No message was collected -----\n", fp, m); 71410170Seric } 7154318Seric 71660010Seric if (e->e_msgboundary != NULL) 71760010Seric { 71860010Seric putline("", fp, m); 71960010Seric (void) sprintf(buf, "--%s--", e->e_msgboundary); 72060010Seric putline(buf, fp, m); 72160010Seric } 72259730Seric putline("", fp, m); 72359730Seric 7244318Seric /* 7254318Seric ** Cleanup and exit 7264318Seric */ 7274318Seric 728297Seric if (errno != 0) 7296978Seric syserr("errbody: I/O error"); 730297Seric } 73158144Seric /* 73258144Seric ** PRUNEROUTE -- prune an RFC-822 source route 73358144Seric ** 73458144Seric ** Trims down a source route to the last internet-registered hop. 73558144Seric ** This is encouraged by RFC 1123 section 5.3.3. 73658144Seric ** 73758144Seric ** Parameters: 73858144Seric ** addr -- the address 73958144Seric ** 74058144Seric ** Returns: 74158144Seric ** TRUE -- address was modified 74258144Seric ** FALSE -- address could not be pruned 74358144Seric ** 74458144Seric ** Side Effects: 74558144Seric ** modifies addr in-place 74658144Seric */ 74758144Seric 74858144Seric pruneroute(addr) 74958144Seric char *addr; 75058144Seric { 75158144Seric #ifdef NAMED_BIND 75258144Seric char *start, *at, *comma; 75358144Seric char c; 75458144Seric int rcode; 75558144Seric char hostbuf[BUFSIZ]; 75658144Seric char *mxhosts[MAXMXHOSTS + 1]; 75758144Seric 75858144Seric /* check to see if this is really a route-addr */ 75958144Seric if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>') 76058144Seric return FALSE; 76158144Seric start = strchr(addr, ':'); 76258144Seric at = strrchr(addr, '@'); 76358144Seric if (start == NULL || at == NULL || at < start) 76458144Seric return FALSE; 76558144Seric 76658144Seric /* slice off the angle brackets */ 76758144Seric strcpy(hostbuf, at + 1); 76858144Seric hostbuf[strlen(hostbuf) - 1] = '\0'; 76958144Seric 77058144Seric while (start) 77158144Seric { 77259273Seric if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0) 77358144Seric { 77458144Seric strcpy(addr + 1, start + 1); 77558144Seric return TRUE; 77658144Seric } 77758144Seric c = *start; 77858144Seric *start = '\0'; 77958144Seric comma = strrchr(addr, ','); 78058144Seric if (comma && comma[1] == '@') 78158144Seric strcpy(hostbuf, comma + 2); 78258144Seric else 78358144Seric comma = 0; 78458144Seric *start = c; 78558144Seric start = comma; 78658144Seric } 78758144Seric #endif 78858144Seric return FALSE; 78958144Seric } 790