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*64284Seric static char sccsid[] = "@(#)savemail.c 8.10 (Berkeley) 08/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 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"; 83*64284Seric if (parseaddr(e->e_sender, &e->e_from, 84*64284Seric 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", 228*64284Seric NULLADDR, &e->e_errorqueue, e); 22963841Seric } 23058680Seric if (strcmp(e->e_from.q_paddr, "<>") != 0) 23163841Seric { 23258680Seric (void) sendtolist(e->e_from.q_paddr, 233*64284Seric 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); 32524942Seric if (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; 45158559Seric ee->e_msgsize = e->e_msgsize + ERRORFUDGE; 4529542Seric openxscript(ee); 45316479Seric for (q = returnq; q != NULL; q = q->q_next) 4549375Seric { 45559989Seric if (bitset(QBADADDR, q->q_flags)) 45658559Seric continue; 45758559Seric 45859989Seric if (!bitset(QDONTSEND, q->q_flags)) 45959989Seric ee->e_nrcpts++; 46058559Seric 46158144Seric if (!DontPruneRoutes && pruneroute(q->q_paddr)) 462*64284Seric parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e); 46358144Seric 4649375Seric if (q->q_alias == NULL) 46559580Seric addheader("To", q->q_paddr, ee); 4669375Seric } 46724942Seric 46857642Seric # ifdef LOG 46958020Seric if (LogLevel > 5) 47057642Seric syslog(LOG_INFO, "%s: %s: return to sender: %s", 47157642Seric e->e_id, ee->e_id, msg); 47257642Seric # endif 47357642Seric 47410845Seric (void) sprintf(buf, "Returned mail: %s", msg); 47559580Seric addheader("Subject", buf, ee); 47659730Seric if (SendMIMEErrors) 47759730Seric { 47859987Seric addheader("MIME-Version", "1.0", ee); 47959730Seric (void) sprintf(buf, "%s.%ld/%s", 48059730Seric ee->e_id, curtime(), MyHostName); 48159730Seric ee->e_msgboundary = newstr(buf); 48259730Seric (void) sprintf(buf, "multipart/mixed; boundary=\"%s\"", 48359730Seric ee->e_msgboundary); 48459730Seric addheader("Content-Type", buf, ee); 48559730Seric } 4864633Seric 4874633Seric /* fake up an address header for the from person */ 48858050Seric expand("\201n", buf, &buf[sizeof buf - 1], e); 489*64284Seric if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL) 4904633Seric { 49158151Seric syserr("553 Can't parse myself!"); 4924633Seric ExitStat = EX_SOFTWARE; 4937045Seric returndepth--; 4944633Seric return (-1); 4954633Seric } 49658704Seric ee->e_sender = ee->e_from.q_paddr; 4975984Seric 4986978Seric /* push state into submessage */ 4996978Seric CurEnv = ee; 50058050Seric define('f', "\201n", ee); 5019375Seric define('x', "Mail Delivery Subsystem", ee); 50258929Seric eatheader(ee, TRUE); 5035984Seric 50463753Seric /* mark statistics */ 505*64284Seric markstats(ee, NULLADDR); 50663753Seric 5076978Seric /* actually deliver the error message */ 50814876Seric sendall(ee, SM_DEFAULT); 5096978Seric 5106978Seric /* restore state */ 5117811Seric dropenvelope(ee); 51258680Seric CurEnv = oldcur; 5137045Seric returndepth--; 5146978Seric 5157045Seric /* should check for delivery errors here */ 5164633Seric return (0); 5174633Seric } 5184633Seric /* 5196978Seric ** ERRBODY -- output the body of an error message. 5206978Seric ** 5216978Seric ** Typically this is a copy of the transcript plus a copy of the 5226978Seric ** original offending message. 5236978Seric ** 524297Seric ** Parameters: 525297Seric ** fp -- the output file. 52610170Seric ** m -- the mailer to output to. 5279542Seric ** e -- the envelope we are working in. 528297Seric ** 529297Seric ** Returns: 530297Seric ** none 531297Seric ** 532297Seric ** Side Effects: 5336978Seric ** Outputs the body of an error message. 534297Seric */ 535297Seric 53610170Seric errbody(fp, m, e) 537297Seric register FILE *fp; 5384318Seric register struct mailer *m; 5399542Seric register ENVELOPE *e; 540297Seric { 5416978Seric register FILE *xfile; 54259082Seric char *p; 54359082Seric register ADDRESS *q; 54459082Seric bool printheader; 5453189Seric char buf[MAXLINE]; 546297Seric 54758680Seric if (e->e_parent == NULL) 54858680Seric { 54958680Seric syserr("errbody: null parent"); 55058680Seric putline(" ----- Original message lost -----\n", fp, m); 55158680Seric return; 55258680Seric } 55358680Seric 5549057Seric /* 55559730Seric ** Output MIME header. 55659730Seric */ 55759730Seric 55859730Seric if (e->e_msgboundary != NULL) 55959730Seric { 56059730Seric putline("This is a MIME-encapsulated message", fp, m); 56159730Seric putline("", fp, m); 56259730Seric (void) sprintf(buf, "--%s", e->e_msgboundary); 56359730Seric putline(buf, fp, m); 56459730Seric putline("", fp, m); 56559730Seric } 56659730Seric 56759730Seric /* 56863852Seric ** Output introductory information. 56963852Seric */ 57063852Seric 57163852Seric sprintf(buf, "The original message was received at %s", arpadate(NULL)); 57263852Seric putline(buf, fp, m); 57364025Seric expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent); 57463852Seric putline(buf, fp, m); 57563852Seric putline("", fp, m); 57663852Seric 57763852Seric /* 57855372Seric ** Output error message header (if specified and available). 57955372Seric */ 58055372Seric 58155372Seric if (ErrMsgFile != NULL) 58255372Seric { 58355372Seric if (*ErrMsgFile == '/') 58455372Seric { 58555372Seric xfile = fopen(ErrMsgFile, "r"); 58655372Seric if (xfile != NULL) 58755372Seric { 58855372Seric while (fgets(buf, sizeof buf, xfile) != NULL) 58955425Seric { 59055425Seric expand(buf, buf, &buf[sizeof buf - 1], e); 59155372Seric putline(buf, fp, m); 59255425Seric } 59355372Seric (void) fclose(xfile); 59459082Seric putline("\n", fp, m); 59555372Seric } 59655372Seric } 59755372Seric else 59855372Seric { 59955425Seric expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e); 60055425Seric putline(buf, fp, m); 60159730Seric putline("", fp, m); 60255372Seric } 60355372Seric } 60455372Seric 60555372Seric /* 60659082Seric ** Output message introduction 60759082Seric */ 60859082Seric 60959082Seric printheader = TRUE; 61059082Seric for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 61159082Seric { 61263787Seric if (bitset(QBADADDR|QREPORT, q->q_flags)) 61359082Seric { 61459082Seric if (printheader) 61559082Seric { 61663787Seric putline(" ----- The following addresses had delivery problems -----", 61759082Seric fp, m); 61859082Seric printheader = FALSE; 61959082Seric } 62063849Seric strcpy(buf, q->q_paddr); 62163787Seric if (bitset(QBADADDR, q->q_flags)) 62263849Seric strcat(buf, " (unrecoverable error)"); 62363787Seric else 62463849Seric strcat(buf, " (transient failure)"); 62563787Seric putline(buf, fp, m); 62663849Seric if (q->q_alias != NULL) 62763849Seric { 62863849Seric strcpy(buf, " (expanded from: "); 62963849Seric strcat(buf, q->q_alias->q_paddr); 63063849Seric strcat(buf, ")"); 63163849Seric putline(buf, fp, m); 63263849Seric } 63359082Seric } 63459082Seric } 63559082Seric if (!printheader) 63659082Seric putline("\n", fp, m); 63759082Seric 63859082Seric /* 6399057Seric ** Output transcript of errors 6409057Seric */ 6419057Seric 6424086Seric (void) fflush(stdout); 6439542Seric p = queuename(e->e_parent, 'x'); 6449337Seric if ((xfile = fopen(p, "r")) == NULL) 6459057Seric { 6469337Seric syserr("Cannot open %s", p); 64759082Seric putline(" ----- Transcript of session is unavailable -----\n", fp, m); 6489057Seric } 6499057Seric else 6509057Seric { 65158680Seric putline(" ----- Transcript of session follows -----\n", fp, m); 6529542Seric if (e->e_xfp != NULL) 6539542Seric (void) fflush(e->e_xfp); 6549057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 65510170Seric putline(buf, fp, m); 65658680Seric (void) xfclose(xfile, "errbody xscript", p); 6579057Seric } 658297Seric errno = 0; 6594318Seric 6604318Seric /* 6614318Seric ** Output text of original message 6624318Seric */ 6634318Seric 6644289Seric if (NoReturn) 66558665Seric SendBody = FALSE; 66659730Seric putline("", fp, m); 66758680Seric if (e->e_parent->e_df != NULL) 6684199Seric { 6695984Seric if (SendBody) 67063852Seric putline(" ----- Original message follows -----\n", fp, m); 6715984Seric else 67259987Seric putline(" ----- Message header follows -----\n", fp, m); 67359730Seric (void) fflush(fp); 67459730Seric 67559730Seric if (e->e_msgboundary != NULL) 6765984Seric { 67759730Seric putline("", fp, m); 67859730Seric (void) sprintf(buf, "--%s", e->e_msgboundary); 67959730Seric putline(buf, fp, m); 68059987Seric putline("Content-Type: message/rfc822", fp, m); 68159730Seric putline("", fp, m); 6825984Seric } 68359730Seric putheader(fp, m, e->e_parent); 68459730Seric putline("", fp, m); 68559730Seric if (SendBody) 68659730Seric putbody(fp, m, e->e_parent, e->e_msgboundary); 68759987Seric else 68860010Seric putline(" ----- Message body suppressed -----", fp, m); 6894199Seric } 6904199Seric else 69110170Seric { 69210170Seric putline(" ----- No message was collected -----\n", fp, m); 69310170Seric } 6944318Seric 69560010Seric if (e->e_msgboundary != NULL) 69660010Seric { 69760010Seric putline("", fp, m); 69860010Seric (void) sprintf(buf, "--%s--", e->e_msgboundary); 69960010Seric putline(buf, fp, m); 70060010Seric } 70159730Seric putline("", fp, m); 70259730Seric 7034318Seric /* 7044318Seric ** Cleanup and exit 7054318Seric */ 7064318Seric 707297Seric if (errno != 0) 7086978Seric syserr("errbody: I/O error"); 709297Seric } 71058144Seric /* 71158144Seric ** PRUNEROUTE -- prune an RFC-822 source route 71258144Seric ** 71358144Seric ** Trims down a source route to the last internet-registered hop. 71458144Seric ** This is encouraged by RFC 1123 section 5.3.3. 71558144Seric ** 71658144Seric ** Parameters: 71758144Seric ** addr -- the address 71858144Seric ** 71958144Seric ** Returns: 72058144Seric ** TRUE -- address was modified 72158144Seric ** FALSE -- address could not be pruned 72258144Seric ** 72358144Seric ** Side Effects: 72458144Seric ** modifies addr in-place 72558144Seric */ 72658144Seric 72758144Seric pruneroute(addr) 72858144Seric char *addr; 72958144Seric { 73058144Seric #ifdef NAMED_BIND 73158144Seric char *start, *at, *comma; 73258144Seric char c; 73358144Seric int rcode; 73458144Seric char hostbuf[BUFSIZ]; 73558144Seric char *mxhosts[MAXMXHOSTS + 1]; 73658144Seric 73758144Seric /* check to see if this is really a route-addr */ 73858144Seric if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>') 73958144Seric return FALSE; 74058144Seric start = strchr(addr, ':'); 74158144Seric at = strrchr(addr, '@'); 74258144Seric if (start == NULL || at == NULL || at < start) 74358144Seric return FALSE; 74458144Seric 74558144Seric /* slice off the angle brackets */ 74658144Seric strcpy(hostbuf, at + 1); 74758144Seric hostbuf[strlen(hostbuf) - 1] = '\0'; 74858144Seric 74958144Seric while (start) 75058144Seric { 75159273Seric if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0) 75258144Seric { 75358144Seric strcpy(addr + 1, start + 1); 75458144Seric return TRUE; 75558144Seric } 75658144Seric c = *start; 75758144Seric *start = '\0'; 75858144Seric comma = strrchr(addr, ','); 75958144Seric if (comma && comma[1] == '@') 76058144Seric strcpy(hostbuf, comma + 2); 76158144Seric else 76258144Seric comma = 0; 76358144Seric *start = c; 76458144Seric start = comma; 76558144Seric } 76658144Seric #endif 76758144Seric return FALSE; 76858144Seric } 769