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*67880Seric static char sccsid[] = "@(#)savemail.c 8.36 (Berkeley) 11/04/94"; 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 4665174Seric # ifndef _PATH_VARTMP 4765174Seric # define _PATH_VARTMP "/usr/tmp/" 4865174Seric # endif 4924942Seric 5065174Seric 519337Seric savemail(e) 529337Seric register ENVELOPE *e; 53297Seric { 54297Seric register struct passwd *pw; 5524942Seric register FILE *fp; 5624942Seric int state; 5759290Seric auto ADDRESS *q = NULL; 5865870Seric register char *p; 5965870Seric MCI mcibuf; 60297Seric char buf[MAXLINE+1]; 61297Seric extern struct passwd *getpwnam(); 62297Seric extern char *ttypath(); 635846Seric typedef int (*fnptr)(); 6464945Seric extern bool writable(); 65297Seric 667676Seric if (tTd(6, 1)) 6758680Seric { 6866317Seric printf("\nsavemail, errormode = %c, id = %s, ExitStat = %d\n e_from=", 6966317Seric e->e_errormode, e->e_id == NULL ? "NONE" : e->e_id, 7066317Seric ExitStat); 7158680Seric printaddr(&e->e_from, FALSE); 7258680Seric } 737361Seric 7459101Seric if (e->e_id == NULL) 7559101Seric { 7659101Seric /* can't return a message with no id */ 7759101Seric return; 7859101Seric } 7959101Seric 80297Seric /* 81297Seric ** In the unhappy event we don't know who to return the mail 82297Seric ** to, make someone up. 83297Seric */ 84297Seric 859337Seric if (e->e_from.q_paddr == NULL) 86297Seric { 8758733Seric e->e_sender = "Postmaster"; 8864284Seric if (parseaddr(e->e_sender, &e->e_from, 8964284Seric RF_COPYPARSE|RF_SENDERADDR, '\0', NULL, e) == NULL) 90297Seric { 9158733Seric syserr("553 Cannot parse Postmaster!"); 92297Seric ExitStat = EX_SOFTWARE; 93297Seric finis(); 94297Seric } 95297Seric } 969337Seric e->e_to = NULL; 97297Seric 98297Seric /* 9924942Seric ** Basic state machine. 10024942Seric ** 10124942Seric ** This machine runs through the following states: 10224942Seric ** 10324942Seric ** ESM_QUIET Errors have already been printed iff the 10424942Seric ** sender is local. 10524942Seric ** ESM_REPORT Report directly to the sender's terminal. 10624942Seric ** ESM_MAIL Mail response to the sender. 10724942Seric ** ESM_DEADLETTER Save response in ~/dead.letter. 10824942Seric ** ESM_POSTMASTER Mail response to the postmaster. 10924942Seric ** ESM_PANIC Save response anywhere possible. 110297Seric */ 111297Seric 11224942Seric /* determine starting state */ 11358734Seric switch (e->e_errormode) 114297Seric { 11524942Seric case EM_WRITE: 11624942Seric state = ESM_REPORT; 11724942Seric break; 11824942Seric 11924942Seric case EM_BERKNET: 12024942Seric /* mail back, but return o.k. exit status */ 121401Seric ExitStat = EX_OK; 12224942Seric 12324942Seric /* fall through.... */ 12424942Seric 12524942Seric case EM_MAIL: 12624942Seric state = ESM_MAIL; 12724942Seric break; 12824942Seric 12924942Seric case EM_PRINT: 13024979Seric case '\0': 13124942Seric state = ESM_QUIET; 13224942Seric break; 13324942Seric 13424942Seric case EM_QUIET: 13524942Seric /* no need to return anything at all */ 13624942Seric return; 13724979Seric 13824979Seric default: 13958734Seric syserr("554 savemail: bogus errormode x%x\n", e->e_errormode); 14024979Seric state = ESM_MAIL; 14124979Seric break; 142297Seric } 143297Seric 14459094Seric /* if this is already an error response, send to postmaster */ 14559094Seric if (bitset(EF_RESPONSE, e->e_flags)) 14659094Seric { 14759094Seric if (e->e_parent != NULL && 14859094Seric bitset(EF_RESPONSE, e->e_parent->e_flags)) 14959094Seric { 15059094Seric /* got an error sending a response -- can it */ 15159094Seric return; 15259094Seric } 15359094Seric state = ESM_POSTMASTER; 15459094Seric } 15559094Seric 15624942Seric while (state != ESM_DONE) 157297Seric { 15824979Seric if (tTd(6, 5)) 15924979Seric printf(" state %d\n", state); 16024979Seric 16124942Seric switch (state) 162297Seric { 16324979Seric case ESM_QUIET: 16467473Seric if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags)) 16524979Seric state = ESM_DEADLETTER; 16624979Seric else 16724979Seric state = ESM_MAIL; 16824979Seric break; 16924979Seric 17024942Seric case ESM_REPORT: 17124942Seric 17224942Seric /* 17324942Seric ** If the user is still logged in on the same terminal, 17424942Seric ** then write the error messages back to hir (sic). 17524942Seric */ 17624942Seric 17724942Seric p = ttypath(); 17824942Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 17924942Seric { 18024942Seric state = ESM_MAIL; 18124942Seric break; 18224942Seric } 18324942Seric 18458050Seric expand("\201n", buf, &buf[sizeof buf - 1], e); 1859375Seric printf("\r\nMessage from %s...\r\n", buf); 1869375Seric printf("Errors occurred while sending mail.\r\n"); 1879542Seric if (e->e_xfp != NULL) 1889375Seric { 1899542Seric (void) fflush(e->e_xfp); 19024942Seric fp = fopen(queuename(e, 'x'), "r"); 1919375Seric } 1929375Seric else 19324942Seric fp = NULL; 19424942Seric if (fp == NULL) 1959375Seric { 1969337Seric syserr("Cannot open %s", queuename(e, 'x')); 1979375Seric printf("Transcript of session is unavailable.\r\n"); 1989375Seric } 1999375Seric else 2009375Seric { 2019375Seric printf("Transcript follows:\r\n"); 20224942Seric while (fgets(buf, sizeof buf, fp) != NULL && 2039375Seric !ferror(stdout)) 2049375Seric fputs(buf, stdout); 20558680Seric (void) xfclose(fp, "savemail transcript", e->e_id); 2069375Seric } 20724942Seric printf("Original message will be saved in dead.letter.\r\n"); 20824942Seric state = ESM_DEADLETTER; 20924942Seric break; 210297Seric 21124942Seric case ESM_MAIL: 21224942Seric /* 21324942Seric ** If mailing back, do it. 21424942Seric ** Throw away all further output. Don't alias, 21524942Seric ** since this could cause loops, e.g., if joe 21624942Seric ** mails to joe@x, and for some reason the network 21724942Seric ** for @x is down, then the response gets sent to 21824942Seric ** joe@x, which gives a response, etc. Also force 21924942Seric ** the mail to be delivered even if a version of 22024942Seric ** it has already been sent to the sender. 22163841Seric ** 22263841Seric ** If this is a configuration or local software 22363841Seric ** error, send to the local postmaster as well, 22463841Seric ** since the originator can't do anything 22563841Seric ** about it anyway. Note that this is a full 22663841Seric ** copy of the message (intentionally) so that 22763841Seric ** the Postmaster can forward things along. 22824942Seric */ 229297Seric 23063841Seric if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE) 23163841Seric { 23263841Seric (void) sendtolist("postmaster", 23364284Seric NULLADDR, &e->e_errorqueue, e); 23463841Seric } 23558680Seric if (strcmp(e->e_from.q_paddr, "<>") != 0) 23663841Seric { 23758680Seric (void) sendtolist(e->e_from.q_paddr, 23864284Seric NULLADDR, &e->e_errorqueue, e); 23963841Seric } 24058680Seric 24163841Seric /* 24263841Seric ** Deliver a non-delivery report to the 24363841Seric ** Postmaster-designate (not necessarily 24463841Seric ** Postmaster). This does not include the 24563841Seric ** body of the message, for privacy reasons. 24663841Seric ** You really shouldn't need this. 24763841Seric */ 24863841Seric 24963849Seric e->e_flags |= EF_PM_NOTIFY; 25058178Seric 25164792Seric /* check to see if there are any good addresses */ 25264792Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 25364792Seric if (!bitset(QBADADDR|QDONTSEND, q->q_flags)) 25464792Seric break; 25558680Seric if (q == NULL) 25658680Seric { 25758680Seric /* this is an error-error */ 25858680Seric state = ESM_POSTMASTER; 25958680Seric break; 26058680Seric } 26166303Seric if (returntosender(e->e_message, e->e_errorqueue, 26266303Seric (e->e_class >= 0), e) == 0) 26358680Seric { 26458680Seric state = ESM_DONE; 26558680Seric break; 26658680Seric } 26724981Seric 26858680Seric /* didn't work -- return to postmaster */ 26958680Seric state = ESM_POSTMASTER; 27058680Seric break; 27158306Seric 27258680Seric case ESM_POSTMASTER: 27358680Seric /* 27458680Seric ** Similar to previous case, but to system postmaster. 27558680Seric */ 27658680Seric 27759432Seric q = NULL; 27859432Seric if (sendtolist("postmaster", NULL, &q, e) <= 0) 27924942Seric { 28058680Seric syserr("553 cannot parse postmaster!"); 28158680Seric ExitStat = EX_SOFTWARE; 28258680Seric state = ESM_USRTMP; 28358680Seric break; 28424942Seric } 28558966Seric if (returntosender(e->e_message, 28657438Seric q, (e->e_class >= 0), e) == 0) 28724942Seric { 28824942Seric state = ESM_DONE; 28924942Seric break; 29024942Seric } 291297Seric 29258680Seric /* didn't work -- last resort */ 29358680Seric state = ESM_USRTMP; 29424942Seric break; 295297Seric 29624942Seric case ESM_DEADLETTER: 29724942Seric /* 29824942Seric ** Save the message in dead.letter. 29924942Seric ** If we weren't mailing back, and the user is 30024942Seric ** local, we should save the message in 30124942Seric ** ~/dead.letter so that the poor person doesn't 30224942Seric ** have to type it over again -- and we all know 30324942Seric ** what poor typists UNIX users are. 30424942Seric */ 3055315Seric 30624942Seric p = NULL; 30767473Seric if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags)) 30824942Seric { 30924942Seric if (e->e_from.q_home != NULL) 31024942Seric p = e->e_from.q_home; 31124942Seric else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 31224942Seric p = pw->pw_dir; 31324942Seric } 31424942Seric if (p == NULL) 31524942Seric { 31658865Seric /* no local directory */ 31724942Seric state = ESM_MAIL; 31824942Seric break; 31924942Seric } 32024942Seric if (e->e_dfp != NULL) 32124942Seric { 32224942Seric bool oldverb = Verbose; 32324942Seric 32424942Seric /* we have a home directory; open dead.letter */ 32524942Seric define('z', p, e); 32658050Seric expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e); 32724942Seric Verbose = TRUE; 32858151Seric message("Saving message in %s", buf); 32924942Seric Verbose = oldverb; 33024942Seric e->e_to = buf; 33124942Seric q = NULL; 33258082Seric (void) sendtolist(buf, &e->e_from, &q, e); 33364354Seric if (q != NULL && 33464354Seric !bitset(QBADADDR, q->q_flags) && 33564354Seric deliver(e, q) == 0) 33624942Seric state = ESM_DONE; 33724942Seric else 33824942Seric state = ESM_MAIL; 33924942Seric } 34025569Seric else 34125569Seric { 34225569Seric /* no data file -- try mailing back */ 34325569Seric state = ESM_MAIL; 34425569Seric } 34524942Seric break; 34624942Seric 34724942Seric case ESM_USRTMP: 34824942Seric /* 34924942Seric ** Log the mail in /usr/tmp/dead.letter. 35024942Seric */ 35124942Seric 35257438Seric if (e->e_class < 0) 35357438Seric { 35457438Seric state = ESM_DONE; 35557438Seric break; 35657438Seric } 35757438Seric 35865174Seric strcpy(buf, _PATH_VARTMP); 35965174Seric strcat(buf, "dead.letter"); 36065112Seric if (!writable(buf, NULLADDR, SFF_NOSLINK)) 36164945Seric { 36264945Seric state = ESM_PANIC; 36364945Seric break; 36464945Seric } 36564945Seric fp = dfopen(buf, O_WRONLY|O_CREAT|O_APPEND, FileMode); 36624942Seric if (fp == NULL) 36724942Seric { 36824942Seric state = ESM_PANIC; 36924942Seric break; 37024942Seric } 37124942Seric 37265870Seric bzero(&mcibuf, sizeof mcibuf); 37365870Seric mcibuf.mci_out = fp; 37465870Seric mcibuf.mci_mailer = FileMailer; 37565870Seric if (bitnset(M_7BITS, FileMailer->m_flags)) 37665870Seric mcibuf.mci_flags |= MCIF_7BIT; 37765870Seric 37865870Seric putfromline(&mcibuf, e); 37967546Seric (*e->e_puthdr)(&mcibuf, e->e_header, e); 38065870Seric (*e->e_putbody)(&mcibuf, e, NULL); 38165870Seric putline("\n", &mcibuf); 38224942Seric (void) fflush(fp); 38324942Seric state = ferror(fp) ? ESM_PANIC : ESM_DONE; 38467809Seric (void) xfclose(fp, "savemail", buf); 38524942Seric break; 38624942Seric 38724942Seric default: 38858151Seric syserr("554 savemail: unknown state %d", state); 38924942Seric 39024942Seric /* fall through ... */ 39124942Seric 39224942Seric case ESM_PANIC: 39324942Seric /* leave the locked queue & transcript files around */ 39464949Seric syserr("!554 savemail: cannot save rejected email anywhere"); 39524942Seric } 396297Seric } 397297Seric } 398297Seric /* 3994633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 4004633Seric ** 4014633Seric ** Parameters: 4024633Seric ** msg -- the explanatory message. 40316479Seric ** returnq -- the queue of people to send the message to. 4045984Seric ** sendbody -- if TRUE, also send back the body of the 4055984Seric ** message; otherwise just send the header. 40655012Seric ** e -- the current envelope. 4074633Seric ** 4084633Seric ** Returns: 4094633Seric ** zero -- if everything went ok. 4104633Seric ** else -- some error. 4114633Seric ** 4124633Seric ** Side Effects: 4134633Seric ** Returns the current message to the sender via 4144633Seric ** mail. 4154633Seric */ 4164633Seric 4175984Seric static bool SendBody; 4184633Seric 4197045Seric #define MAXRETURNS 6 /* max depth of returning messages */ 42058559Seric #define ERRORFUDGE 100 /* nominal size of error message text */ 4217045Seric 42255012Seric returntosender(msg, returnq, sendbody, e) 4234633Seric char *msg; 42416479Seric ADDRESS *returnq; 4255984Seric bool sendbody; 42655012Seric register ENVELOPE *e; 4274633Seric { 4284633Seric char buf[MAXNAME]; 4296978Seric extern putheader(), errbody(); 4306978Seric register ENVELOPE *ee; 43158680Seric ENVELOPE *oldcur = CurEnv; 4326978Seric ENVELOPE errenvelope; 4337045Seric static int returndepth; 4349375Seric register ADDRESS *q; 4354633Seric 43660010Seric if (returnq == NULL) 43760010Seric return (-1); 43860010Seric 43958966Seric if (msg == NULL) 44058966Seric msg = "Unable to deliver mail"; 44158966Seric 4427676Seric if (tTd(6, 1)) 4437287Seric { 44458680Seric printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=", 44555012Seric msg, returndepth, e); 44616479Seric printaddr(returnq, TRUE); 4477287Seric } 4487287Seric 4497045Seric if (++returndepth >= MAXRETURNS) 4507045Seric { 4517045Seric if (returndepth != MAXRETURNS) 45258151Seric syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr); 4537045Seric /* don't "unrecurse" and fake a clean exit */ 4547045Seric /* returndepth--; */ 4557045Seric return (0); 4567045Seric } 4577045Seric 4585984Seric SendBody = sendbody; 45958680Seric define('g', e->e_from.q_paddr, e); 46064940Seric define('u', NULL, e); 461*67880Seric 462*67880Seric /* initialize error envelope */ 46358179Seric ee = newenvelope(&errenvelope, e); 46458050Seric define('a', "\201b", ee); 46559057Seric define('r', "internal", ee); 46659057Seric define('s', "localhost", ee); 46759057Seric define('_', "localhost", ee); 4686978Seric ee->e_puthdr = putheader; 4696978Seric ee->e_putbody = errbody; 47064120Seric ee->e_flags |= EF_RESPONSE|EF_METOO; 47155012Seric if (!bitset(EF_OLDSTYLE, e->e_flags)) 47245155Seric ee->e_flags &= ~EF_OLDSTYLE; 47316479Seric ee->e_sendqueue = returnq; 47464655Seric ee->e_msgsize = ERRORFUDGE; 47567473Seric if (!bitset(EF_NORETURN, e->e_flags)) 47664655Seric ee->e_msgsize += e->e_msgsize; 47764737Seric initsys(ee); 47816479Seric for (q = returnq; q != NULL; q = q->q_next) 4799375Seric { 48059989Seric if (bitset(QBADADDR, q->q_flags)) 48158559Seric continue; 48258559Seric 48359989Seric if (!bitset(QDONTSEND, q->q_flags)) 48459989Seric ee->e_nrcpts++; 48558559Seric 48658144Seric if (!DontPruneRoutes && pruneroute(q->q_paddr)) 48764284Seric parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e); 48858144Seric 4899375Seric if (q->q_alias == NULL) 49067546Seric addheader("To", q->q_paddr, &ee->e_header); 4919375Seric } 49224942Seric 49357642Seric # ifdef LOG 49458020Seric if (LogLevel > 5) 49565054Seric syslog(LOG_INFO, "%s: %s: return to sender: %s", 49657642Seric e->e_id, ee->e_id, msg); 49757642Seric # endif 49857642Seric 49967429Seric if (strncmp(msg, "Warning:", 8) == 0) 50067261Seric { 50167261Seric addheader("Subject", msg, ee); 50267546Seric addheader("Precedence", "autoreply warning-timeout", &ee->e_header); 50367261Seric } 50467429Seric else if (strcmp(msg, "Return receipt") == 0) 50567429Seric { 50667429Seric addheader("Subject", msg, ee); 50767546Seric addheader("Precedence", "autoreply return-receipt", &ee->e_header); 50867429Seric } 50967261Seric else 51067261Seric { 51167261Seric sprintf(buf, "Returned mail: %.*s", sizeof buf - 20, msg); 51267546Seric addheader("Subject", buf, &ee->e_header); 51367546Seric addheader("Precedence", "autoreply failure-message", &ee->e_header); 51467261Seric } 51559730Seric if (SendMIMEErrors) 51659730Seric { 51767546Seric addheader("MIME-Version", "1.0", &ee->e_header); 51859730Seric (void) sprintf(buf, "%s.%ld/%s", 51959730Seric ee->e_id, curtime(), MyHostName); 52059730Seric ee->e_msgboundary = newstr(buf); 521*67880Seric (void) sprintf(buf, 522*67880Seric "multipart/report; report-type=delivery-status; boundary=\"%s\"", 523*67880Seric ee->e_msgboundary); 52467546Seric addheader("Content-Type", buf, &ee->e_header); 52559730Seric } 5264633Seric 5274633Seric /* fake up an address header for the from person */ 52858050Seric expand("\201n", buf, &buf[sizeof buf - 1], e); 52964284Seric if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL) 5304633Seric { 53158151Seric syserr("553 Can't parse myself!"); 5324633Seric ExitStat = EX_SOFTWARE; 5337045Seric returndepth--; 5344633Seric return (-1); 5354633Seric } 53658704Seric ee->e_sender = ee->e_from.q_paddr; 5375984Seric 5386978Seric /* push state into submessage */ 5396978Seric CurEnv = ee; 54058050Seric define('f', "\201n", ee); 5419375Seric define('x', "Mail Delivery Subsystem", ee); 54258929Seric eatheader(ee, TRUE); 5435984Seric 54463753Seric /* mark statistics */ 54564284Seric markstats(ee, NULLADDR); 54663753Seric 5476978Seric /* actually deliver the error message */ 54814876Seric sendall(ee, SM_DEFAULT); 5496978Seric 5506978Seric /* restore state */ 5517811Seric dropenvelope(ee); 55258680Seric CurEnv = oldcur; 5537045Seric returndepth--; 5546978Seric 5557045Seric /* should check for delivery errors here */ 5564633Seric return (0); 5574633Seric } 5584633Seric /* 5596978Seric ** ERRBODY -- output the body of an error message. 5606978Seric ** 5616978Seric ** Typically this is a copy of the transcript plus a copy of the 5626978Seric ** original offending message. 5636978Seric ** 564297Seric ** Parameters: 56565870Seric ** mci -- the mailer connection information. 5669542Seric ** e -- the envelope we are working in. 56767546Seric ** separator -- any possible MIME separator. 568297Seric ** 569297Seric ** Returns: 570297Seric ** none 571297Seric ** 572297Seric ** Side Effects: 5736978Seric ** Outputs the body of an error message. 574297Seric */ 575297Seric 57667546Seric errbody(mci, e, separator) 57765870Seric register MCI *mci; 5789542Seric register ENVELOPE *e; 57967546Seric char *separator; 580297Seric { 5816978Seric register FILE *xfile; 58259082Seric char *p; 58359082Seric register ADDRESS *q; 58459082Seric bool printheader; 5853189Seric char buf[MAXLINE]; 586297Seric 58767546Seric if (bitset(MCIF_INHEADER, mci->mci_flags)) 58867546Seric { 58967546Seric putline("", mci); 59067546Seric mci->mci_flags &= ~MCIF_INHEADER; 59167546Seric } 59258680Seric if (e->e_parent == NULL) 59358680Seric { 59458680Seric syserr("errbody: null parent"); 59565870Seric putline(" ----- Original message lost -----\n", mci); 59658680Seric return; 59758680Seric } 59858680Seric 5999057Seric /* 60059730Seric ** Output MIME header. 60159730Seric */ 60259730Seric 60359730Seric if (e->e_msgboundary != NULL) 60459730Seric { 60565870Seric putline("This is a MIME-encapsulated message", mci); 60665870Seric putline("", mci); 60759730Seric (void) sprintf(buf, "--%s", e->e_msgboundary); 60865870Seric putline(buf, mci); 60965870Seric putline("", mci); 61059730Seric } 61159730Seric 61259730Seric /* 61363852Seric ** Output introductory information. 61463852Seric */ 61563852Seric 61664718Seric for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 61764718Seric if (bitset(QBADADDR, q->q_flags)) 61864718Seric break; 61965054Seric if (q == NULL && 62065054Seric !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags)) 62164718Seric { 62264718Seric putline(" **********************************************", 62365870Seric mci); 62464718Seric putline(" ** THIS IS A WARNING MESSAGE ONLY **", 62565870Seric mci); 62664718Seric putline(" ** YOU DO NOT NEED TO RESEND YOUR MESSAGE **", 62765870Seric mci); 62864718Seric putline(" **********************************************", 62965870Seric mci); 63065870Seric putline("", mci); 63164718Seric } 63264718Seric sprintf(buf, "The original message was received at %s", 63364718Seric arpadate(ctime(&e->e_parent->e_ctime))); 63465870Seric putline(buf, mci); 63564025Seric expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent); 63665870Seric putline(buf, mci); 63765870Seric putline("", mci); 63863852Seric 63963852Seric /* 64055372Seric ** Output error message header (if specified and available). 64155372Seric */ 64255372Seric 64367682Seric if (ErrMsgFile != NULL && !bitset(EF_SENDRECEIPT, e->e_parent->e_flags)) 64455372Seric { 64555372Seric if (*ErrMsgFile == '/') 64655372Seric { 64755372Seric xfile = fopen(ErrMsgFile, "r"); 64855372Seric if (xfile != NULL) 64955372Seric { 65055372Seric while (fgets(buf, sizeof buf, xfile) != NULL) 65155425Seric { 65255425Seric expand(buf, buf, &buf[sizeof buf - 1], e); 65365870Seric putline(buf, mci); 65455425Seric } 65555372Seric (void) fclose(xfile); 65665870Seric putline("\n", mci); 65755372Seric } 65855372Seric } 65955372Seric else 66055372Seric { 66155425Seric expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e); 66265870Seric putline(buf, mci); 66365870Seric putline("", mci); 66455372Seric } 66555372Seric } 66655372Seric 66755372Seric /* 66859082Seric ** Output message introduction 66959082Seric */ 67059082Seric 67159082Seric printheader = TRUE; 67259082Seric for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 67359082Seric { 67463787Seric if (bitset(QBADADDR|QREPORT, q->q_flags)) 67559082Seric { 67659082Seric if (printheader) 67759082Seric { 678*67880Seric putline(" ----- The following addresses have delivery notifications -----", 67965870Seric mci); 68059082Seric printheader = FALSE; 68159082Seric } 68263849Seric strcpy(buf, q->q_paddr); 68363787Seric if (bitset(QBADADDR, q->q_flags)) 68463849Seric strcat(buf, " (unrecoverable error)"); 685*67880Seric else if (bitset(QSENT, q->q_flags)) 686*67880Seric strcat(buf, " (successfully delivered)"); 68763787Seric else 68863849Seric strcat(buf, " (transient failure)"); 68965870Seric putline(buf, mci); 69063849Seric if (q->q_alias != NULL) 69163849Seric { 69263849Seric strcpy(buf, " (expanded from: "); 69363849Seric strcat(buf, q->q_alias->q_paddr); 69463849Seric strcat(buf, ")"); 69565870Seric putline(buf, mci); 69663849Seric } 69759082Seric } 69859082Seric } 69959082Seric if (!printheader) 70065870Seric putline("\n", mci); 70159082Seric 70259082Seric /* 7039057Seric ** Output transcript of errors 7049057Seric */ 7059057Seric 7064086Seric (void) fflush(stdout); 7079542Seric p = queuename(e->e_parent, 'x'); 7089337Seric if ((xfile = fopen(p, "r")) == NULL) 7099057Seric { 7109337Seric syserr("Cannot open %s", p); 71165870Seric putline(" ----- Transcript of session is unavailable -----\n", mci); 7129057Seric } 7139057Seric else 7149057Seric { 71565870Seric putline(" ----- Transcript of session follows -----\n", mci); 7169542Seric if (e->e_xfp != NULL) 7179542Seric (void) fflush(e->e_xfp); 7189057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 71965870Seric putline(buf, mci); 72058680Seric (void) xfclose(xfile, "errbody xscript", p); 7219057Seric } 722297Seric errno = 0; 7234318Seric 7244318Seric /* 725*67880Seric ** Output machine-readable version. 726*67880Seric */ 727*67880Seric 728*67880Seric if (e->e_msgboundary != NULL) 729*67880Seric { 730*67880Seric putline("", mci); 731*67880Seric (void) sprintf(buf, "--%s", e->e_msgboundary); 732*67880Seric putline(buf, mci); 733*67880Seric putline("Content-Type: message/delivery-status", mci); 734*67880Seric putline("", mci); 735*67880Seric 736*67880Seric /* 737*67880Seric ** Output per-message information. 738*67880Seric */ 739*67880Seric 740*67880Seric /* Original-MTS-Type: is optional */ 741*67880Seric 742*67880Seric /* original envelope id from MAIL FROM: line */ 743*67880Seric if (e->e_parent->e_envid != NULL) 744*67880Seric { 745*67880Seric (void) sprintf(buf, "Original-Envelope-Id: %s", 746*67880Seric e->e_parent->e_envid); 747*67880Seric putline(buf, mci); 748*67880Seric } 749*67880Seric 750*67880Seric /* Final-MTS-Type: is optional (always INET?) */ 751*67880Seric 752*67880Seric /* Final-MTA: seems silly -- this is in the From: line */ 753*67880Seric (void) sprintf(buf, "Final-MTA: %s", MyHostName); 754*67880Seric putline(buf, mci); 755*67880Seric 756*67880Seric /* 757*67880Seric ** Output per-address information. 758*67880Seric */ 759*67880Seric 760*67880Seric for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 761*67880Seric { 762*67880Seric register ADDRESS *r; 763*67880Seric 764*67880Seric if (!bitset(QBADADDR|QREPORT|QRELAYED, q->q_flags)) 765*67880Seric continue; 766*67880Seric putline("", mci); 767*67880Seric 768*67880Seric /* Rcpt: -- the name from the RCPT command */ 769*67880Seric for (r = q; r->q_alias != NULL; r = r->q_alias) 770*67880Seric continue; 771*67880Seric (void) sprintf(buf, "Rcpt: %s", r->q_paddr); 772*67880Seric putline(buf, mci); 773*67880Seric 774*67880Seric /* Action: -- what happened? */ 775*67880Seric if (bitset(QBADADDR, q->q_flags)) 776*67880Seric putline("Action: failed", mci); 777*67880Seric else if (bitset(QQUEUEUP, q->q_flags)) 778*67880Seric putline("Action: delayed", mci); 779*67880Seric else if (bitset(QRELAYED, q->q_flags)) 780*67880Seric putline("Action: relayed", mci); 781*67880Seric else 782*67880Seric putline("Action: delivered", mci); 783*67880Seric 784*67880Seric /* Status: -- what _really_ happened? */ 785*67880Seric strcpy(buf, "Status: "); 786*67880Seric if (q->q_status != NULL) 787*67880Seric strcat(buf, q->q_status); 788*67880Seric else if (bitset(QBADADDR, q->q_flags)) 789*67880Seric strcat(buf, "500"); 790*67880Seric else if (bitset(QQUEUEUP, q->q_flags)) 791*67880Seric strcat(buf, "400"); 792*67880Seric else if (bitset(QRELAYED, q->q_flags)) 793*67880Seric strcat(buf, "601"); 794*67880Seric else 795*67880Seric strcat(buf, "200"); 796*67880Seric putline(buf, mci); 797*67880Seric 798*67880Seric /* Date: -- fine granularity */ 799*67880Seric if (q->q_statdate == (time_t) 0L) 800*67880Seric q->q_statdate = curtime(); 801*67880Seric (void) sprintf(buf, "Date: %s", 802*67880Seric arpadate(ctime(&q->q_statdate))); 803*67880Seric putline(buf, mci); 804*67880Seric 805*67880Seric /* Final-Log-Id: -- why isn't this per-message? */ 806*67880Seric (void) sprintf(buf, "Final-Log-Id: %s", e->e_id); 807*67880Seric putline(buf, mci); 808*67880Seric 809*67880Seric /* Original-Rcpt: -- passed from on high */ 810*67880Seric if (q->q_orcpt != NULL) 811*67880Seric { 812*67880Seric (void) sprintf(buf, "Original-Rcpt: %s", 813*67880Seric q->q_orcpt); 814*67880Seric putline(buf, mci); 815*67880Seric } 816*67880Seric 817*67880Seric /* Final-Rcpt: -- if through alias */ 818*67880Seric if (q->q_alias != NULL) 819*67880Seric { 820*67880Seric (void) sprintf(buf, "Final-Rcpt: %s", 821*67880Seric q->q_paddr); 822*67880Seric putline(buf, mci); 823*67880Seric } 824*67880Seric 825*67880Seric /* Final-Status: -- same as Status? XXX */ 826*67880Seric 827*67880Seric /* Remote-MTS-Type: -- always INET? XXX */ 828*67880Seric 829*67880Seric /* Remote-MTA: -- who was I talking to? */ 830*67880Seric if (q->q_statmta != NULL) 831*67880Seric { 832*67880Seric (void) sprintf(buf, "Remote-MTA: %s", 833*67880Seric q->q_statmta); 834*67880Seric putline(buf, mci); 835*67880Seric } 836*67880Seric 837*67880Seric /* Remote-Rcpt: -- same as Final-Rcpt? XXX */ 838*67880Seric 839*67880Seric /* Remote-Status: -- same as Final-Status? XXX */ 840*67880Seric } 841*67880Seric } 842*67880Seric 843*67880Seric /* 8444318Seric ** Output text of original message 8454318Seric */ 8464318Seric 847*67880Seric if (bitset(EF_NORETURN, e->e_parent->e_flags)) 84858665Seric SendBody = FALSE; 84965870Seric putline("", mci); 85058680Seric if (e->e_parent->e_df != NULL) 8514199Seric { 852*67880Seric if (e->e_msgboundary == NULL) 853*67880Seric { 854*67880Seric if (SendBody) 855*67880Seric putline(" ----- Original message follows -----\n", mci); 856*67880Seric else 857*67880Seric putline(" ----- Message header follows -----\n", mci); 858*67880Seric (void) fflush(mci->mci_out); 859*67880Seric } 8605984Seric else 8615984Seric { 86265870Seric putline("", mci); 86359730Seric (void) sprintf(buf, "--%s", e->e_msgboundary); 86465870Seric putline(buf, mci); 86565870Seric putline("Content-Type: message/rfc822", mci); 86665870Seric putline("", mci); 8675984Seric } 86867546Seric putheader(mci, e->e_parent->e_header, e->e_parent); 86959730Seric if (SendBody) 87065870Seric putbody(mci, e->e_parent, e->e_msgboundary); 87159987Seric else 87267546Seric { 87367546Seric putline("", mci); 87465870Seric putline(" ----- Message body suppressed -----", mci); 87567546Seric } 8764199Seric } 8774199Seric else 87810170Seric { 87965870Seric putline(" ----- No message was collected -----\n", mci); 88010170Seric } 8814318Seric 88260010Seric if (e->e_msgboundary != NULL) 88360010Seric { 88465870Seric putline("", mci); 88560010Seric (void) sprintf(buf, "--%s--", e->e_msgboundary); 88665870Seric putline(buf, mci); 88760010Seric } 88865870Seric putline("", mci); 88959730Seric 8904318Seric /* 8914318Seric ** Cleanup and exit 8924318Seric */ 8934318Seric 894297Seric if (errno != 0) 8956978Seric syserr("errbody: I/O error"); 896297Seric } 89758144Seric /* 89858144Seric ** PRUNEROUTE -- prune an RFC-822 source route 89958144Seric ** 90058144Seric ** Trims down a source route to the last internet-registered hop. 90158144Seric ** This is encouraged by RFC 1123 section 5.3.3. 90258144Seric ** 90358144Seric ** Parameters: 90458144Seric ** addr -- the address 90558144Seric ** 90658144Seric ** Returns: 90758144Seric ** TRUE -- address was modified 90858144Seric ** FALSE -- address could not be pruned 90958144Seric ** 91058144Seric ** Side Effects: 91158144Seric ** modifies addr in-place 91258144Seric */ 91358144Seric 91458144Seric pruneroute(addr) 91558144Seric char *addr; 91658144Seric { 91766334Seric #if NAMED_BIND 91858144Seric char *start, *at, *comma; 91958144Seric char c; 92058144Seric int rcode; 92158144Seric char hostbuf[BUFSIZ]; 92258144Seric char *mxhosts[MAXMXHOSTS + 1]; 92358144Seric 92458144Seric /* check to see if this is really a route-addr */ 92558144Seric if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>') 92658144Seric return FALSE; 92758144Seric start = strchr(addr, ':'); 92858144Seric at = strrchr(addr, '@'); 92958144Seric if (start == NULL || at == NULL || at < start) 93058144Seric return FALSE; 93158144Seric 93258144Seric /* slice off the angle brackets */ 93358144Seric strcpy(hostbuf, at + 1); 93458144Seric hostbuf[strlen(hostbuf) - 1] = '\0'; 93558144Seric 93658144Seric while (start) 93758144Seric { 93859273Seric if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0) 93958144Seric { 94058144Seric strcpy(addr + 1, start + 1); 94158144Seric return TRUE; 94258144Seric } 94358144Seric c = *start; 94458144Seric *start = '\0'; 94558144Seric comma = strrchr(addr, ','); 94658144Seric if (comma && comma[1] == '@') 94758144Seric strcpy(hostbuf, comma + 2); 94858144Seric else 94958144Seric comma = 0; 95058144Seric *start = c; 95158144Seric start = comma; 95258144Seric } 95358144Seric #endif 95458144Seric return FALSE; 95558144Seric } 956