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*64726Seric static char sccsid[] = "@(#)savemail.c 8.15 (Berkeley) 10/17/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 24458680Seric q = e->e_errorqueue; 24558680Seric if (q == NULL) 24658680Seric { 24758680Seric /* this is an error-error */ 24858680Seric state = ESM_POSTMASTER; 24958680Seric break; 25058680Seric } 25158966Seric if (returntosender(e->e_message, 25258680Seric q, (e->e_class >= 0), e) == 0) 25358680Seric { 25458680Seric state = ESM_DONE; 25558680Seric break; 25658680Seric } 25724981Seric 25858680Seric /* didn't work -- return to postmaster */ 25958680Seric state = ESM_POSTMASTER; 26058680Seric break; 26158306Seric 26258680Seric case ESM_POSTMASTER: 26358680Seric /* 26458680Seric ** Similar to previous case, but to system postmaster. 26558680Seric */ 26658680Seric 26759432Seric q = NULL; 26859432Seric if (sendtolist("postmaster", NULL, &q, e) <= 0) 26924942Seric { 27058680Seric syserr("553 cannot parse postmaster!"); 27158680Seric ExitStat = EX_SOFTWARE; 27258680Seric state = ESM_USRTMP; 27358680Seric break; 27424942Seric } 27558966Seric if (returntosender(e->e_message, 27657438Seric q, (e->e_class >= 0), e) == 0) 27724942Seric { 27824942Seric state = ESM_DONE; 27924942Seric break; 28024942Seric } 281297Seric 28258680Seric /* didn't work -- last resort */ 28358680Seric state = ESM_USRTMP; 28424942Seric break; 285297Seric 28624942Seric case ESM_DEADLETTER: 28724942Seric /* 28824942Seric ** Save the message in dead.letter. 28924942Seric ** If we weren't mailing back, and the user is 29024942Seric ** local, we should save the message in 29124942Seric ** ~/dead.letter so that the poor person doesn't 29224942Seric ** have to type it over again -- and we all know 29324942Seric ** what poor typists UNIX users are. 29424942Seric */ 2955315Seric 29624942Seric p = NULL; 29724942Seric if (e->e_from.q_mailer == LocalMailer) 29824942Seric { 29924942Seric if (e->e_from.q_home != NULL) 30024942Seric p = e->e_from.q_home; 30124942Seric else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 30224942Seric p = pw->pw_dir; 30324942Seric } 30424942Seric if (p == NULL) 30524942Seric { 30658865Seric /* no local directory */ 30724942Seric state = ESM_MAIL; 30824942Seric break; 30924942Seric } 31024942Seric if (e->e_dfp != NULL) 31124942Seric { 31224942Seric bool oldverb = Verbose; 31324942Seric 31424942Seric /* we have a home directory; open dead.letter */ 31524942Seric define('z', p, e); 31658050Seric expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e); 31724942Seric Verbose = TRUE; 31858151Seric message("Saving message in %s", buf); 31924942Seric Verbose = oldverb; 32024942Seric e->e_to = buf; 32124942Seric q = NULL; 32258082Seric (void) sendtolist(buf, &e->e_from, &q, e); 32364354Seric if (q != NULL && 32464354Seric !bitset(QBADADDR, q->q_flags) && 32564354Seric deliver(e, q) == 0) 32624942Seric state = ESM_DONE; 32724942Seric else 32824942Seric state = ESM_MAIL; 32924942Seric } 33025569Seric else 33125569Seric { 33225569Seric /* no data file -- try mailing back */ 33325569Seric state = ESM_MAIL; 33425569Seric } 33524942Seric break; 33624942Seric 33724942Seric case ESM_USRTMP: 33824942Seric /* 33924942Seric ** Log the mail in /usr/tmp/dead.letter. 34024942Seric */ 34124942Seric 34257438Seric if (e->e_class < 0) 34357438Seric { 34457438Seric state = ESM_DONE; 34557438Seric break; 34657438Seric } 34757438Seric 34859745Seric fp = dfopen("/usr/tmp/dead.letter", 34959745Seric O_WRONLY|O_CREAT|O_APPEND, FileMode); 35024942Seric if (fp == NULL) 35124942Seric { 35224942Seric state = ESM_PANIC; 35324942Seric break; 35424942Seric } 35524942Seric 35658010Seric putfromline(fp, FileMailer, e); 35758010Seric (*e->e_puthdr)(fp, FileMailer, e); 35858010Seric putline("\n", fp, FileMailer); 35959730Seric (*e->e_putbody)(fp, FileMailer, e, NULL); 36058010Seric putline("\n", fp, FileMailer); 36124942Seric (void) fflush(fp); 36224942Seric state = ferror(fp) ? ESM_PANIC : ESM_DONE; 36358680Seric (void) xfclose(fp, "savemail", "/usr/tmp/dead.letter"); 36424942Seric break; 36524942Seric 36624942Seric default: 36758151Seric syserr("554 savemail: unknown state %d", state); 36824942Seric 36924942Seric /* fall through ... */ 37024942Seric 37124942Seric case ESM_PANIC: 37224942Seric /* leave the locked queue & transcript files around */ 37358151Seric syserr("554 savemail: cannot save rejected email anywhere"); 37424942Seric exit(EX_SOFTWARE); 37524942Seric } 376297Seric } 377297Seric } 378297Seric /* 3794633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 3804633Seric ** 3814633Seric ** Parameters: 3824633Seric ** msg -- the explanatory message. 38316479Seric ** returnq -- the queue of people to send the message to. 3845984Seric ** sendbody -- if TRUE, also send back the body of the 3855984Seric ** message; otherwise just send the header. 38655012Seric ** e -- the current envelope. 3874633Seric ** 3884633Seric ** Returns: 3894633Seric ** zero -- if everything went ok. 3904633Seric ** else -- some error. 3914633Seric ** 3924633Seric ** Side Effects: 3934633Seric ** Returns the current message to the sender via 3944633Seric ** mail. 3954633Seric */ 3964633Seric 3975984Seric static bool SendBody; 3984633Seric 3997045Seric #define MAXRETURNS 6 /* max depth of returning messages */ 40058559Seric #define ERRORFUDGE 100 /* nominal size of error message text */ 4017045Seric 40255012Seric returntosender(msg, returnq, sendbody, e) 4034633Seric char *msg; 40416479Seric ADDRESS *returnq; 4055984Seric bool sendbody; 40655012Seric register ENVELOPE *e; 4074633Seric { 4084633Seric char buf[MAXNAME]; 4096978Seric extern putheader(), errbody(); 4106978Seric register ENVELOPE *ee; 41158680Seric ENVELOPE *oldcur = CurEnv; 4126978Seric ENVELOPE errenvelope; 4137045Seric static int returndepth; 4149375Seric register ADDRESS *q; 4154633Seric 41660010Seric if (returnq == NULL) 41760010Seric return (-1); 41860010Seric 41958966Seric if (msg == NULL) 42058966Seric msg = "Unable to deliver mail"; 42158966Seric 4227676Seric if (tTd(6, 1)) 4237287Seric { 42458680Seric printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=", 42555012Seric msg, returndepth, e); 42616479Seric printaddr(returnq, TRUE); 4277287Seric } 4287287Seric 4297045Seric if (++returndepth >= MAXRETURNS) 4307045Seric { 4317045Seric if (returndepth != MAXRETURNS) 43258151Seric syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr); 4337045Seric /* don't "unrecurse" and fake a clean exit */ 4347045Seric /* returndepth--; */ 4357045Seric return (0); 4367045Seric } 4377045Seric 4385984Seric SendBody = sendbody; 43958680Seric define('g', e->e_from.q_paddr, e); 44058179Seric ee = newenvelope(&errenvelope, e); 44158050Seric define('a', "\201b", ee); 44259057Seric define('r', "internal", ee); 44359057Seric define('s', "localhost", ee); 44459057Seric define('_', "localhost", ee); 4456978Seric ee->e_puthdr = putheader; 4466978Seric ee->e_putbody = errbody; 44764120Seric ee->e_flags |= EF_RESPONSE|EF_METOO; 44855012Seric if (!bitset(EF_OLDSTYLE, e->e_flags)) 44945155Seric ee->e_flags &= ~EF_OLDSTYLE; 45016479Seric ee->e_sendqueue = returnq; 45164655Seric ee->e_msgsize = ERRORFUDGE; 45264655Seric if (!NoReturn) 45364655Seric ee->e_msgsize += e->e_msgsize; 4549542Seric openxscript(ee); 45516479Seric for (q = returnq; q != NULL; q = q->q_next) 4569375Seric { 45759989Seric if (bitset(QBADADDR, q->q_flags)) 45858559Seric continue; 45958559Seric 46059989Seric if (!bitset(QDONTSEND, q->q_flags)) 46159989Seric ee->e_nrcpts++; 46258559Seric 46358144Seric if (!DontPruneRoutes && pruneroute(q->q_paddr)) 46464284Seric parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e); 46558144Seric 4669375Seric if (q->q_alias == NULL) 46759580Seric addheader("To", q->q_paddr, ee); 4689375Seric } 46924942Seric 47057642Seric # ifdef LOG 47158020Seric if (LogLevel > 5) 47257642Seric syslog(LOG_INFO, "%s: %s: return to sender: %s", 47357642Seric e->e_id, ee->e_id, msg); 47457642Seric # endif 47557642Seric 47610845Seric (void) sprintf(buf, "Returned mail: %s", msg); 47759580Seric addheader("Subject", buf, ee); 47859730Seric if (SendMIMEErrors) 47959730Seric { 48059987Seric addheader("MIME-Version", "1.0", ee); 48159730Seric (void) sprintf(buf, "%s.%ld/%s", 48259730Seric ee->e_id, curtime(), MyHostName); 48359730Seric ee->e_msgboundary = newstr(buf); 48459730Seric (void) sprintf(buf, "multipart/mixed; boundary=\"%s\"", 48559730Seric ee->e_msgboundary); 48659730Seric addheader("Content-Type", buf, ee); 48759730Seric } 4884633Seric 4894633Seric /* fake up an address header for the from person */ 49058050Seric expand("\201n", buf, &buf[sizeof buf - 1], e); 49164284Seric if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL) 4924633Seric { 49358151Seric syserr("553 Can't parse myself!"); 4944633Seric ExitStat = EX_SOFTWARE; 4957045Seric returndepth--; 4964633Seric return (-1); 4974633Seric } 49858704Seric ee->e_sender = ee->e_from.q_paddr; 4995984Seric 5006978Seric /* push state into submessage */ 5016978Seric CurEnv = ee; 50258050Seric define('f', "\201n", ee); 5039375Seric define('x', "Mail Delivery Subsystem", ee); 50458929Seric eatheader(ee, TRUE); 5055984Seric 50663753Seric /* mark statistics */ 50764284Seric markstats(ee, NULLADDR); 50863753Seric 5096978Seric /* actually deliver the error message */ 51014876Seric sendall(ee, SM_DEFAULT); 5116978Seric 5126978Seric /* restore state */ 5137811Seric dropenvelope(ee); 51458680Seric CurEnv = oldcur; 5157045Seric returndepth--; 5166978Seric 5177045Seric /* should check for delivery errors here */ 5184633Seric return (0); 5194633Seric } 5204633Seric /* 5216978Seric ** ERRBODY -- output the body of an error message. 5226978Seric ** 5236978Seric ** Typically this is a copy of the transcript plus a copy of the 5246978Seric ** original offending message. 5256978Seric ** 526297Seric ** Parameters: 527297Seric ** fp -- the output file. 52810170Seric ** m -- the mailer to output to. 5299542Seric ** e -- the envelope we are working in. 530297Seric ** 531297Seric ** Returns: 532297Seric ** none 533297Seric ** 534297Seric ** Side Effects: 5356978Seric ** Outputs the body of an error message. 536297Seric */ 537297Seric 53810170Seric errbody(fp, m, e) 539297Seric register FILE *fp; 5404318Seric register struct mailer *m; 5419542Seric register ENVELOPE *e; 542297Seric { 5436978Seric register FILE *xfile; 54459082Seric char *p; 54559082Seric register ADDRESS *q; 54659082Seric bool printheader; 5473189Seric char buf[MAXLINE]; 548297Seric 54958680Seric if (e->e_parent == NULL) 55058680Seric { 55158680Seric syserr("errbody: null parent"); 55258680Seric putline(" ----- Original message lost -----\n", fp, m); 55358680Seric return; 55458680Seric } 55558680Seric 5569057Seric /* 55759730Seric ** Output MIME header. 55859730Seric */ 55959730Seric 56059730Seric if (e->e_msgboundary != NULL) 56159730Seric { 56259730Seric putline("This is a MIME-encapsulated message", fp, m); 56359730Seric putline("", fp, m); 56459730Seric (void) sprintf(buf, "--%s", e->e_msgboundary); 56559730Seric putline(buf, fp, m); 56659730Seric putline("", fp, m); 56759730Seric } 56859730Seric 56959730Seric /* 57063852Seric ** Output introductory information. 57163852Seric */ 57263852Seric 57364718Seric for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 57464718Seric if (bitset(QBADADDR, q->q_flags)) 57564718Seric break; 576*64726Seric if (q == NULL && !bitset(EF_FATALERRS, e->e_parent->e_flags)) 57764718Seric { 57864718Seric putline(" **********************************************", 57964718Seric fp, m); 58064718Seric putline(" ** THIS IS A WARNING MESSAGE ONLY **", 58164718Seric fp, m); 58264718Seric putline(" ** YOU DO NOT NEED TO RESEND YOUR MESSAGE **", 58364718Seric fp, m); 58464718Seric putline(" **********************************************", 58564718Seric fp, m); 58664718Seric putline("", fp, m); 58764718Seric } 58864718Seric sprintf(buf, "The original message was received at %s", 58964718Seric arpadate(ctime(&e->e_parent->e_ctime))); 59063852Seric putline(buf, fp, m); 59164025Seric expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent); 59263852Seric putline(buf, fp, m); 59363852Seric putline("", fp, m); 59463852Seric 59563852Seric /* 59655372Seric ** Output error message header (if specified and available). 59755372Seric */ 59855372Seric 59955372Seric if (ErrMsgFile != NULL) 60055372Seric { 60155372Seric if (*ErrMsgFile == '/') 60255372Seric { 60355372Seric xfile = fopen(ErrMsgFile, "r"); 60455372Seric if (xfile != NULL) 60555372Seric { 60655372Seric while (fgets(buf, sizeof buf, xfile) != NULL) 60755425Seric { 60855425Seric expand(buf, buf, &buf[sizeof buf - 1], e); 60955372Seric putline(buf, fp, m); 61055425Seric } 61155372Seric (void) fclose(xfile); 61259082Seric putline("\n", fp, m); 61355372Seric } 61455372Seric } 61555372Seric else 61655372Seric { 61755425Seric expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e); 61855425Seric putline(buf, fp, m); 61959730Seric putline("", fp, m); 62055372Seric } 62155372Seric } 62255372Seric 62355372Seric /* 62459082Seric ** Output message introduction 62559082Seric */ 62659082Seric 62759082Seric printheader = TRUE; 62859082Seric for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 62959082Seric { 63063787Seric if (bitset(QBADADDR|QREPORT, q->q_flags)) 63159082Seric { 63259082Seric if (printheader) 63359082Seric { 63463787Seric putline(" ----- The following addresses had delivery problems -----", 63559082Seric fp, m); 63659082Seric printheader = FALSE; 63759082Seric } 63863849Seric strcpy(buf, q->q_paddr); 63963787Seric if (bitset(QBADADDR, q->q_flags)) 64063849Seric strcat(buf, " (unrecoverable error)"); 64163787Seric else 64263849Seric strcat(buf, " (transient failure)"); 64363787Seric putline(buf, fp, m); 64463849Seric if (q->q_alias != NULL) 64563849Seric { 64663849Seric strcpy(buf, " (expanded from: "); 64763849Seric strcat(buf, q->q_alias->q_paddr); 64863849Seric strcat(buf, ")"); 64963849Seric putline(buf, fp, m); 65063849Seric } 65159082Seric } 65259082Seric } 65359082Seric if (!printheader) 65459082Seric putline("\n", fp, m); 65559082Seric 65659082Seric /* 6579057Seric ** Output transcript of errors 6589057Seric */ 6599057Seric 6604086Seric (void) fflush(stdout); 6619542Seric p = queuename(e->e_parent, 'x'); 6629337Seric if ((xfile = fopen(p, "r")) == NULL) 6639057Seric { 6649337Seric syserr("Cannot open %s", p); 66559082Seric putline(" ----- Transcript of session is unavailable -----\n", fp, m); 6669057Seric } 6679057Seric else 6689057Seric { 66958680Seric putline(" ----- Transcript of session follows -----\n", fp, m); 6709542Seric if (e->e_xfp != NULL) 6719542Seric (void) fflush(e->e_xfp); 6729057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 67310170Seric putline(buf, fp, m); 67458680Seric (void) xfclose(xfile, "errbody xscript", p); 6759057Seric } 676297Seric errno = 0; 6774318Seric 6784318Seric /* 6794318Seric ** Output text of original message 6804318Seric */ 6814318Seric 6824289Seric if (NoReturn) 68358665Seric SendBody = FALSE; 68459730Seric putline("", fp, m); 68558680Seric if (e->e_parent->e_df != NULL) 6864199Seric { 6875984Seric if (SendBody) 68863852Seric putline(" ----- Original message follows -----\n", fp, m); 6895984Seric else 69059987Seric putline(" ----- Message header follows -----\n", fp, m); 69159730Seric (void) fflush(fp); 69259730Seric 69359730Seric if (e->e_msgboundary != NULL) 6945984Seric { 69559730Seric putline("", fp, m); 69659730Seric (void) sprintf(buf, "--%s", e->e_msgboundary); 69759730Seric putline(buf, fp, m); 69859987Seric putline("Content-Type: message/rfc822", fp, m); 69959730Seric putline("", fp, m); 7005984Seric } 70159730Seric putheader(fp, m, e->e_parent); 70259730Seric putline("", fp, m); 70359730Seric if (SendBody) 70459730Seric putbody(fp, m, e->e_parent, e->e_msgboundary); 70559987Seric else 70660010Seric putline(" ----- Message body suppressed -----", fp, m); 7074199Seric } 7084199Seric else 70910170Seric { 71010170Seric putline(" ----- No message was collected -----\n", fp, m); 71110170Seric } 7124318Seric 71360010Seric if (e->e_msgboundary != NULL) 71460010Seric { 71560010Seric putline("", fp, m); 71660010Seric (void) sprintf(buf, "--%s--", e->e_msgboundary); 71760010Seric putline(buf, fp, m); 71860010Seric } 71959730Seric putline("", fp, m); 72059730Seric 7214318Seric /* 7224318Seric ** Cleanup and exit 7234318Seric */ 7244318Seric 725297Seric if (errno != 0) 7266978Seric syserr("errbody: I/O error"); 727297Seric } 72858144Seric /* 72958144Seric ** PRUNEROUTE -- prune an RFC-822 source route 73058144Seric ** 73158144Seric ** Trims down a source route to the last internet-registered hop. 73258144Seric ** This is encouraged by RFC 1123 section 5.3.3. 73358144Seric ** 73458144Seric ** Parameters: 73558144Seric ** addr -- the address 73658144Seric ** 73758144Seric ** Returns: 73858144Seric ** TRUE -- address was modified 73958144Seric ** FALSE -- address could not be pruned 74058144Seric ** 74158144Seric ** Side Effects: 74258144Seric ** modifies addr in-place 74358144Seric */ 74458144Seric 74558144Seric pruneroute(addr) 74658144Seric char *addr; 74758144Seric { 74858144Seric #ifdef NAMED_BIND 74958144Seric char *start, *at, *comma; 75058144Seric char c; 75158144Seric int rcode; 75258144Seric char hostbuf[BUFSIZ]; 75358144Seric char *mxhosts[MAXMXHOSTS + 1]; 75458144Seric 75558144Seric /* check to see if this is really a route-addr */ 75658144Seric if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>') 75758144Seric return FALSE; 75858144Seric start = strchr(addr, ':'); 75958144Seric at = strrchr(addr, '@'); 76058144Seric if (start == NULL || at == NULL || at < start) 76158144Seric return FALSE; 76258144Seric 76358144Seric /* slice off the angle brackets */ 76458144Seric strcpy(hostbuf, at + 1); 76558144Seric hostbuf[strlen(hostbuf) - 1] = '\0'; 76658144Seric 76758144Seric while (start) 76858144Seric { 76959273Seric if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0) 77058144Seric { 77158144Seric strcpy(addr + 1, start + 1); 77258144Seric return TRUE; 77358144Seric } 77458144Seric c = *start; 77558144Seric *start = '\0'; 77658144Seric comma = strrchr(addr, ','); 77758144Seric if (comma && comma[1] == '@') 77858144Seric strcpy(hostbuf, comma + 2); 77958144Seric else 78058144Seric comma = 0; 78158144Seric *start = c; 78258144Seric start = comma; 78358144Seric } 78458144Seric #endif 78558144Seric return FALSE; 78658144Seric } 787