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*68490Seric static char sccsid[] = "@(#)savemail.c 8.52 (Berkeley) 03/05/95"; 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. 2667981Seric ** sendbody -- if TRUE, also send back the body of the 2767981Seric ** message; otherwise just send the header. 28297Seric ** 29297Seric ** Returns: 30297Seric ** none 31297Seric ** 32297Seric ** Side Effects: 33297Seric ** Saves the letter, by writing or mailing it back to the 34297Seric ** sender, or by putting it in dead.letter in her home 35297Seric ** directory. 36297Seric */ 37297Seric 3824942Seric /* defines for state machine */ 3924942Seric # define ESM_REPORT 0 /* report to sender's terminal */ 4024942Seric # define ESM_MAIL 1 /* mail back to sender */ 4124942Seric # define ESM_QUIET 2 /* messages have already been returned */ 4224942Seric # define ESM_DEADLETTER 3 /* save in ~/dead.letter */ 4324942Seric # define ESM_POSTMASTER 4 /* return to postmaster */ 4424942Seric # define ESM_USRTMP 5 /* save in /usr/tmp/dead.letter */ 4524942Seric # define ESM_PANIC 6 /* leave the locked queue/transcript files */ 4624942Seric # define ESM_DONE 7 /* the message is successfully delivered */ 4724942Seric 4865174Seric # ifndef _PATH_VARTMP 4965174Seric # define _PATH_VARTMP "/usr/tmp/" 5065174Seric # endif 5124942Seric 5265174Seric 5367981Seric savemail(e, sendbody) 549337Seric register ENVELOPE *e; 5567981Seric bool sendbody; 56297Seric { 57297Seric register struct passwd *pw; 5824942Seric register FILE *fp; 5924942Seric int state; 6059290Seric auto ADDRESS *q = NULL; 6165870Seric register char *p; 6265870Seric MCI mcibuf; 63297Seric char buf[MAXLINE+1]; 64297Seric extern struct passwd *getpwnam(); 65297Seric extern char *ttypath(); 665846Seric typedef int (*fnptr)(); 6764945Seric extern bool writable(); 68297Seric 697676Seric if (tTd(6, 1)) 7058680Seric { 7166317Seric printf("\nsavemail, errormode = %c, id = %s, ExitStat = %d\n e_from=", 7266317Seric e->e_errormode, e->e_id == NULL ? "NONE" : e->e_id, 7366317Seric ExitStat); 7458680Seric printaddr(&e->e_from, FALSE); 7558680Seric } 767361Seric 7759101Seric if (e->e_id == NULL) 7859101Seric { 7959101Seric /* can't return a message with no id */ 8059101Seric return; 8159101Seric } 8259101Seric 83297Seric /* 84297Seric ** In the unhappy event we don't know who to return the mail 85297Seric ** to, make someone up. 86297Seric */ 87297Seric 889337Seric if (e->e_from.q_paddr == NULL) 89297Seric { 9058733Seric e->e_sender = "Postmaster"; 9164284Seric if (parseaddr(e->e_sender, &e->e_from, 9264284Seric RF_COPYPARSE|RF_SENDERADDR, '\0', NULL, e) == NULL) 93297Seric { 9458733Seric syserr("553 Cannot parse Postmaster!"); 95297Seric ExitStat = EX_SOFTWARE; 96297Seric finis(); 97297Seric } 98297Seric } 999337Seric e->e_to = NULL; 100297Seric 101297Seric /* 10224942Seric ** Basic state machine. 10324942Seric ** 10424942Seric ** This machine runs through the following states: 10524942Seric ** 10624942Seric ** ESM_QUIET Errors have already been printed iff the 10724942Seric ** sender is local. 10824942Seric ** ESM_REPORT Report directly to the sender's terminal. 10924942Seric ** ESM_MAIL Mail response to the sender. 11024942Seric ** ESM_DEADLETTER Save response in ~/dead.letter. 11124942Seric ** ESM_POSTMASTER Mail response to the postmaster. 11224942Seric ** ESM_PANIC Save response anywhere possible. 113297Seric */ 114297Seric 11524942Seric /* determine starting state */ 11658734Seric switch (e->e_errormode) 117297Seric { 11824942Seric case EM_WRITE: 11924942Seric state = ESM_REPORT; 12024942Seric break; 12124942Seric 12224942Seric case EM_BERKNET: 12324942Seric /* mail back, but return o.k. exit status */ 124401Seric ExitStat = EX_OK; 12524942Seric 12624942Seric /* fall through.... */ 12724942Seric 12824942Seric case EM_MAIL: 12924942Seric state = ESM_MAIL; 13024942Seric break; 13124942Seric 13224942Seric case EM_PRINT: 13324979Seric case '\0': 13424942Seric state = ESM_QUIET; 13524942Seric break; 13624942Seric 13724942Seric case EM_QUIET: 13824942Seric /* no need to return anything at all */ 13924942Seric return; 14024979Seric 14124979Seric default: 14258734Seric syserr("554 savemail: bogus errormode x%x\n", e->e_errormode); 14324979Seric state = ESM_MAIL; 14424979Seric break; 145297Seric } 146297Seric 14759094Seric /* if this is already an error response, send to postmaster */ 14859094Seric if (bitset(EF_RESPONSE, e->e_flags)) 14959094Seric { 15059094Seric if (e->e_parent != NULL && 15159094Seric bitset(EF_RESPONSE, e->e_parent->e_flags)) 15259094Seric { 15359094Seric /* got an error sending a response -- can it */ 15459094Seric return; 15559094Seric } 15659094Seric state = ESM_POSTMASTER; 15759094Seric } 15859094Seric 15924942Seric while (state != ESM_DONE) 160297Seric { 16124979Seric if (tTd(6, 5)) 16224979Seric printf(" state %d\n", state); 16324979Seric 16424942Seric switch (state) 165297Seric { 16624979Seric case ESM_QUIET: 16767473Seric if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags)) 16824979Seric state = ESM_DEADLETTER; 16924979Seric else 17024979Seric state = ESM_MAIL; 17124979Seric break; 17224979Seric 17324942Seric case ESM_REPORT: 17424942Seric 17524942Seric /* 17624942Seric ** If the user is still logged in on the same terminal, 17724942Seric ** then write the error messages back to hir (sic). 17824942Seric */ 17924942Seric 18024942Seric p = ttypath(); 18124942Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 18224942Seric { 18324942Seric state = ESM_MAIL; 18424942Seric break; 18524942Seric } 18624942Seric 18758050Seric expand("\201n", buf, &buf[sizeof buf - 1], e); 1889375Seric printf("\r\nMessage from %s...\r\n", buf); 1899375Seric printf("Errors occurred while sending mail.\r\n"); 1909542Seric if (e->e_xfp != NULL) 1919375Seric { 1929542Seric (void) fflush(e->e_xfp); 19324942Seric fp = fopen(queuename(e, 'x'), "r"); 1949375Seric } 1959375Seric else 19624942Seric fp = NULL; 19724942Seric if (fp == NULL) 1989375Seric { 1999337Seric syserr("Cannot open %s", queuename(e, 'x')); 2009375Seric printf("Transcript of session is unavailable.\r\n"); 2019375Seric } 2029375Seric else 2039375Seric { 2049375Seric printf("Transcript follows:\r\n"); 20524942Seric while (fgets(buf, sizeof buf, fp) != NULL && 2069375Seric !ferror(stdout)) 2079375Seric fputs(buf, stdout); 20858680Seric (void) xfclose(fp, "savemail transcript", e->e_id); 2099375Seric } 21024942Seric printf("Original message will be saved in dead.letter.\r\n"); 21124942Seric state = ESM_DEADLETTER; 21224942Seric break; 213297Seric 21424942Seric case ESM_MAIL: 21524942Seric /* 21624942Seric ** If mailing back, do it. 21724942Seric ** Throw away all further output. Don't alias, 21824942Seric ** since this could cause loops, e.g., if joe 21924942Seric ** mails to joe@x, and for some reason the network 22024942Seric ** for @x is down, then the response gets sent to 22124942Seric ** joe@x, which gives a response, etc. Also force 22224942Seric ** the mail to be delivered even if a version of 22324942Seric ** it has already been sent to the sender. 22463841Seric ** 22563841Seric ** If this is a configuration or local software 22663841Seric ** error, send to the local postmaster as well, 22763841Seric ** since the originator can't do anything 22863841Seric ** about it anyway. Note that this is a full 22963841Seric ** copy of the message (intentionally) so that 23063841Seric ** the Postmaster can forward things along. 23124942Seric */ 232297Seric 23363841Seric if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE) 23463841Seric { 23563841Seric (void) sendtolist("postmaster", 23667982Seric NULLADDR, &e->e_errorqueue, 0, e); 23763841Seric } 23867939Seric if (!emptyaddr(&e->e_from)) 23963841Seric { 24058680Seric (void) sendtolist(e->e_from.q_paddr, 24167982Seric NULLADDR, &e->e_errorqueue, 0, e); 24263841Seric } 24358680Seric 24463841Seric /* 24563841Seric ** Deliver a non-delivery report to the 24663841Seric ** Postmaster-designate (not necessarily 24763841Seric ** Postmaster). This does not include the 24863841Seric ** body of the message, for privacy reasons. 24963841Seric ** You really shouldn't need this. 25063841Seric */ 25163841Seric 25263849Seric e->e_flags |= EF_PM_NOTIFY; 25358178Seric 25464792Seric /* check to see if there are any good addresses */ 25564792Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 25664792Seric if (!bitset(QBADADDR|QDONTSEND, q->q_flags)) 25764792Seric break; 25858680Seric if (q == NULL) 25958680Seric { 26058680Seric /* this is an error-error */ 26158680Seric state = ESM_POSTMASTER; 26258680Seric break; 26358680Seric } 26466303Seric if (returntosender(e->e_message, e->e_errorqueue, 26567981Seric sendbody, e) == 0) 26658680Seric { 26758680Seric state = ESM_DONE; 26858680Seric break; 26958680Seric } 27024981Seric 27158680Seric /* didn't work -- return to postmaster */ 27258680Seric state = ESM_POSTMASTER; 27358680Seric break; 27458306Seric 27558680Seric case ESM_POSTMASTER: 27658680Seric /* 27758680Seric ** Similar to previous case, but to system postmaster. 27858680Seric */ 27958680Seric 28059432Seric q = NULL; 28167982Seric if (sendtolist("postmaster", NULL, &q, 0, e) <= 0) 28224942Seric { 28358680Seric syserr("553 cannot parse postmaster!"); 28458680Seric ExitStat = EX_SOFTWARE; 28558680Seric state = ESM_USRTMP; 28658680Seric break; 28724942Seric } 28867981Seric if (returntosender(e->e_message, q, sendbody, e) == 0) 28924942Seric { 29024942Seric state = ESM_DONE; 29124942Seric break; 29224942Seric } 293297Seric 29458680Seric /* didn't work -- last resort */ 29558680Seric state = ESM_USRTMP; 29624942Seric break; 297297Seric 29824942Seric case ESM_DEADLETTER: 29924942Seric /* 30024942Seric ** Save the message in dead.letter. 30124942Seric ** If we weren't mailing back, and the user is 30224942Seric ** local, we should save the message in 30324942Seric ** ~/dead.letter so that the poor person doesn't 30424942Seric ** have to type it over again -- and we all know 30524942Seric ** what poor typists UNIX users are. 30624942Seric */ 3075315Seric 30824942Seric p = NULL; 30967473Seric if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags)) 31024942Seric { 31124942Seric if (e->e_from.q_home != NULL) 31224942Seric p = e->e_from.q_home; 31324942Seric else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 31424942Seric p = pw->pw_dir; 31524942Seric } 31624942Seric if (p == NULL) 31724942Seric { 31858865Seric /* no local directory */ 31924942Seric state = ESM_MAIL; 32024942Seric break; 32124942Seric } 32224942Seric if (e->e_dfp != NULL) 32324942Seric { 32424942Seric bool oldverb = Verbose; 32524942Seric 32624942Seric /* we have a home directory; open dead.letter */ 32724942Seric define('z', p, e); 32858050Seric expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e); 32924942Seric Verbose = TRUE; 33058151Seric message("Saving message in %s", buf); 33124942Seric Verbose = oldverb; 33224942Seric e->e_to = buf; 33324942Seric q = NULL; 33467982Seric (void) sendtolist(buf, &e->e_from, &q, 0, e); 33564354Seric if (q != NULL && 33664354Seric !bitset(QBADADDR, q->q_flags) && 33764354Seric deliver(e, q) == 0) 33824942Seric state = ESM_DONE; 33924942Seric else 34024942Seric state = ESM_MAIL; 34124942Seric } 34225569Seric else 34325569Seric { 34425569Seric /* no data file -- try mailing back */ 34525569Seric state = ESM_MAIL; 34625569Seric } 34724942Seric break; 34824942Seric 34924942Seric case ESM_USRTMP: 35024942Seric /* 35124942Seric ** Log the mail in /usr/tmp/dead.letter. 35224942Seric */ 35324942Seric 35457438Seric if (e->e_class < 0) 35557438Seric { 35657438Seric state = ESM_DONE; 35757438Seric break; 35857438Seric } 35957438Seric 360*68490Seric if (SafeFileEnv != NULL && SafeFileEnv[0] != '\0') 361*68490Seric { 362*68490Seric state = ESM_PANIC; 363*68490Seric break; 364*68490Seric } 365*68490Seric 36665174Seric strcpy(buf, _PATH_VARTMP); 36765174Seric strcat(buf, "dead.letter"); 36865112Seric if (!writable(buf, NULLADDR, SFF_NOSLINK)) 36964945Seric { 37064945Seric state = ESM_PANIC; 37164945Seric break; 37264945Seric } 37364945Seric fp = dfopen(buf, O_WRONLY|O_CREAT|O_APPEND, FileMode); 37424942Seric if (fp == NULL) 37524942Seric { 37624942Seric state = ESM_PANIC; 37724942Seric break; 37824942Seric } 37924942Seric 38065870Seric bzero(&mcibuf, sizeof mcibuf); 38165870Seric mcibuf.mci_out = fp; 38265870Seric mcibuf.mci_mailer = FileMailer; 38365870Seric if (bitnset(M_7BITS, FileMailer->m_flags)) 38465870Seric mcibuf.mci_flags |= MCIF_7BIT; 38565870Seric 38665870Seric putfromline(&mcibuf, e); 38768228Seric (*e->e_puthdr)(&mcibuf, e->e_header, e); 38868228Seric (*e->e_putbody)(&mcibuf, e, NULL); 38965870Seric putline("\n", &mcibuf); 39024942Seric (void) fflush(fp); 39124942Seric state = ferror(fp) ? ESM_PANIC : ESM_DONE; 39267809Seric (void) xfclose(fp, "savemail", buf); 39324942Seric break; 39424942Seric 39524942Seric default: 39658151Seric syserr("554 savemail: unknown state %d", state); 39724942Seric 39824942Seric /* fall through ... */ 39924942Seric 40024942Seric case ESM_PANIC: 40124942Seric /* leave the locked queue & transcript files around */ 402*68490Seric loseqfile(e, "savemail panic"); 40364949Seric syserr("!554 savemail: cannot save rejected email anywhere"); 40424942Seric } 405297Seric } 406297Seric } 407297Seric /* 4084633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 4094633Seric ** 4104633Seric ** Parameters: 4114633Seric ** msg -- the explanatory message. 41216479Seric ** returnq -- the queue of people to send the message to. 4135984Seric ** sendbody -- if TRUE, also send back the body of the 4145984Seric ** message; otherwise just send the header. 41555012Seric ** e -- the current envelope. 4164633Seric ** 4174633Seric ** Returns: 4184633Seric ** zero -- if everything went ok. 4194633Seric ** else -- some error. 4204633Seric ** 4214633Seric ** Side Effects: 4224633Seric ** Returns the current message to the sender via 4234633Seric ** mail. 4244633Seric */ 4254633Seric 4265984Seric static bool SendBody; 4274633Seric 4287045Seric #define MAXRETURNS 6 /* max depth of returning messages */ 42958559Seric #define ERRORFUDGE 100 /* nominal size of error message text */ 4307045Seric 43155012Seric returntosender(msg, returnq, sendbody, e) 4324633Seric char *msg; 43316479Seric ADDRESS *returnq; 4345984Seric bool sendbody; 43555012Seric register ENVELOPE *e; 4364633Seric { 4374633Seric char buf[MAXNAME]; 4386978Seric extern putheader(), errbody(); 4396978Seric register ENVELOPE *ee; 44058680Seric ENVELOPE *oldcur = CurEnv; 4416978Seric ENVELOPE errenvelope; 4427045Seric static int returndepth; 4439375Seric register ADDRESS *q; 44468228Seric char *p; 4454633Seric 44660010Seric if (returnq == NULL) 44760010Seric return (-1); 44860010Seric 44958966Seric if (msg == NULL) 45058966Seric msg = "Unable to deliver mail"; 45158966Seric 4527676Seric if (tTd(6, 1)) 4537287Seric { 45467987Seric printf("\n*** Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=", 45555012Seric msg, returndepth, e); 45616479Seric printaddr(returnq, TRUE); 45767987Seric if (tTd(6, 20)) 45867987Seric { 45967987Seric printf("Sendq="); 46067987Seric printaddr(e->e_sendqueue, TRUE); 46167987Seric } 4627287Seric } 4637287Seric 4647045Seric if (++returndepth >= MAXRETURNS) 4657045Seric { 4667045Seric if (returndepth != MAXRETURNS) 46758151Seric syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr); 4687045Seric /* don't "unrecurse" and fake a clean exit */ 4697045Seric /* returndepth--; */ 4707045Seric return (0); 4717045Seric } 4727045Seric 4735984Seric SendBody = sendbody; 47458680Seric define('g', e->e_from.q_paddr, e); 47564940Seric define('u', NULL, e); 47667880Seric 47767880Seric /* initialize error envelope */ 47858179Seric ee = newenvelope(&errenvelope, e); 47958050Seric define('a', "\201b", ee); 48059057Seric define('r', "internal", ee); 48159057Seric define('s', "localhost", ee); 48259057Seric define('_', "localhost", ee); 4836978Seric ee->e_puthdr = putheader; 4846978Seric ee->e_putbody = errbody; 48564120Seric ee->e_flags |= EF_RESPONSE|EF_METOO; 48655012Seric if (!bitset(EF_OLDSTYLE, e->e_flags)) 48745155Seric ee->e_flags &= ~EF_OLDSTYLE; 48816479Seric ee->e_sendqueue = returnq; 48964655Seric ee->e_msgsize = ERRORFUDGE; 49067473Seric if (!bitset(EF_NORETURN, e->e_flags)) 49164655Seric ee->e_msgsize += e->e_msgsize; 49264737Seric initsys(ee); 49316479Seric for (q = returnq; q != NULL; q = q->q_next) 4949375Seric { 49559989Seric if (bitset(QBADADDR, q->q_flags)) 49658559Seric continue; 49758559Seric 49868141Seric if (!DontPruneRoutes && pruneroute(q->q_paddr)) 49968141Seric { 50068141Seric register ADDRESS *p; 50168141Seric 50268141Seric parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e); 50368141Seric for (p = returnq; p != NULL; p = p->q_next) 50468141Seric { 50568141Seric if (p != q && sameaddr(p, q)) 50668141Seric q->q_flags |= QDONTSEND; 50768141Seric } 50868141Seric } 50968141Seric 51059989Seric if (!bitset(QDONTSEND, q->q_flags)) 51159989Seric ee->e_nrcpts++; 51258559Seric 5139375Seric if (q->q_alias == NULL) 51467546Seric addheader("To", q->q_paddr, &ee->e_header); 5159375Seric } 51624942Seric 51757642Seric # ifdef LOG 51858020Seric if (LogLevel > 5) 51965054Seric syslog(LOG_INFO, "%s: %s: return to sender: %s", 52057642Seric e->e_id, ee->e_id, msg); 52157642Seric # endif 52257642Seric 52368003Seric if (SendMIMEErrors) 52467261Seric { 52568003Seric addheader("MIME-Version", "1.0", &ee->e_header); 52668003Seric (void) sprintf(buf, "%s.%ld/%s", 52768003Seric ee->e_id, curtime(), MyHostName); 52868003Seric ee->e_msgboundary = newstr(buf); 52968003Seric (void) sprintf(buf, 53068028Seric #ifdef DSN 53168085Seric "multipart/report; report-type=X-delivery-status-1; boundary=\"%s\"", 53268028Seric #else 53368028Seric "multipart/mixed; boundary=\"%s\"", 53468028Seric #endif 53568003Seric ee->e_msgboundary); 53668003Seric addheader("Content-Type", buf, &ee->e_header); 53768003Seric } 53868228Seric if (strncmp(msg, "Warning:", 8) == 0) 53968003Seric { 54067261Seric addheader("Subject", msg, ee); 54168228Seric p = "warning-timeout"; 54267261Seric } 54367429Seric else if (strcmp(msg, "Return receipt") == 0) 54467429Seric { 54567429Seric addheader("Subject", msg, ee); 54668228Seric p = "return-receipt"; 54767429Seric } 54867261Seric else 54967261Seric { 55067261Seric sprintf(buf, "Returned mail: %.*s", sizeof buf - 20, msg); 55167546Seric addheader("Subject", buf, &ee->e_header); 55268228Seric p = "failure"; 55367261Seric } 55468228Seric (void) sprintf(buf, "auto-generated (%s)", p); 55568228Seric addheader("Auto-Submitted", buf, &ee->e_header); 5564633Seric 5574633Seric /* fake up an address header for the from person */ 55858050Seric expand("\201n", buf, &buf[sizeof buf - 1], e); 55964284Seric if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL) 5604633Seric { 56158151Seric syserr("553 Can't parse myself!"); 5624633Seric ExitStat = EX_SOFTWARE; 5637045Seric returndepth--; 5644633Seric return (-1); 5654633Seric } 56658704Seric ee->e_sender = ee->e_from.q_paddr; 5675984Seric 5686978Seric /* push state into submessage */ 5696978Seric CurEnv = ee; 57058050Seric define('f', "\201n", ee); 5719375Seric define('x', "Mail Delivery Subsystem", ee); 57258929Seric eatheader(ee, TRUE); 5735984Seric 57463753Seric /* mark statistics */ 57564284Seric markstats(ee, NULLADDR); 57663753Seric 5776978Seric /* actually deliver the error message */ 57814876Seric sendall(ee, SM_DEFAULT); 5796978Seric 5806978Seric /* restore state */ 5817811Seric dropenvelope(ee); 58258680Seric CurEnv = oldcur; 5837045Seric returndepth--; 5846978Seric 5857045Seric /* should check for delivery errors here */ 5864633Seric return (0); 5874633Seric } 5884633Seric /* 5896978Seric ** ERRBODY -- output the body of an error message. 5906978Seric ** 5916978Seric ** Typically this is a copy of the transcript plus a copy of the 5926978Seric ** original offending message. 5936978Seric ** 594297Seric ** Parameters: 59565870Seric ** mci -- the mailer connection information. 5969542Seric ** e -- the envelope we are working in. 59767546Seric ** separator -- any possible MIME separator. 59867936Seric ** flags -- to modify the behaviour. 599297Seric ** 600297Seric ** Returns: 601297Seric ** none 602297Seric ** 603297Seric ** Side Effects: 6046978Seric ** Outputs the body of an error message. 605297Seric */ 606297Seric 60768228Seric errbody(mci, e, separator) 60865870Seric register MCI *mci; 6099542Seric register ENVELOPE *e; 61067546Seric char *separator; 611297Seric { 6126978Seric register FILE *xfile; 61359082Seric char *p; 61459082Seric register ADDRESS *q; 61559082Seric bool printheader; 6163189Seric char buf[MAXLINE]; 61768228Seric extern char *xtextify(); 618297Seric 61967546Seric if (bitset(MCIF_INHEADER, mci->mci_flags)) 62067546Seric { 62167546Seric putline("", mci); 62267546Seric mci->mci_flags &= ~MCIF_INHEADER; 62367546Seric } 62458680Seric if (e->e_parent == NULL) 62558680Seric { 62658680Seric syserr("errbody: null parent"); 62765870Seric putline(" ----- Original message lost -----\n", mci); 62858680Seric return; 62958680Seric } 63058680Seric 6319057Seric /* 63259730Seric ** Output MIME header. 63359730Seric */ 63459730Seric 63559730Seric if (e->e_msgboundary != NULL) 63659730Seric { 63765870Seric putline("This is a MIME-encapsulated message", mci); 63865870Seric putline("", mci); 63959730Seric (void) sprintf(buf, "--%s", e->e_msgboundary); 64065870Seric putline(buf, mci); 64165870Seric putline("", mci); 64259730Seric } 64359730Seric 64459730Seric /* 64563852Seric ** Output introductory information. 64663852Seric */ 64763852Seric 64864718Seric for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 64964718Seric if (bitset(QBADADDR, q->q_flags)) 65064718Seric break; 65165054Seric if (q == NULL && 65265054Seric !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags)) 65364718Seric { 65464718Seric putline(" **********************************************", 65565870Seric mci); 65664718Seric putline(" ** THIS IS A WARNING MESSAGE ONLY **", 65765870Seric mci); 65864718Seric putline(" ** YOU DO NOT NEED TO RESEND YOUR MESSAGE **", 65965870Seric mci); 66064718Seric putline(" **********************************************", 66165870Seric mci); 66265870Seric putline("", mci); 66364718Seric } 66464718Seric sprintf(buf, "The original message was received at %s", 66564718Seric arpadate(ctime(&e->e_parent->e_ctime))); 66665870Seric putline(buf, mci); 66764025Seric expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent); 66865870Seric putline(buf, mci); 66965870Seric putline("", mci); 67063852Seric 67163852Seric /* 67255372Seric ** Output error message header (if specified and available). 67355372Seric */ 67455372Seric 67567682Seric if (ErrMsgFile != NULL && !bitset(EF_SENDRECEIPT, e->e_parent->e_flags)) 67655372Seric { 67755372Seric if (*ErrMsgFile == '/') 67855372Seric { 67955372Seric xfile = fopen(ErrMsgFile, "r"); 68055372Seric if (xfile != NULL) 68155372Seric { 68255372Seric while (fgets(buf, sizeof buf, xfile) != NULL) 68355425Seric { 68455425Seric expand(buf, buf, &buf[sizeof buf - 1], e); 68565870Seric putline(buf, mci); 68655425Seric } 68755372Seric (void) fclose(xfile); 68865870Seric putline("\n", mci); 68955372Seric } 69055372Seric } 69155372Seric else 69255372Seric { 69355425Seric expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e); 69465870Seric putline(buf, mci); 69565870Seric putline("", mci); 69655372Seric } 69755372Seric } 69855372Seric 69955372Seric /* 70059082Seric ** Output message introduction 70159082Seric */ 70259082Seric 70359082Seric printheader = TRUE; 70459082Seric for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 70559082Seric { 70667981Seric if (bitset(QBADADDR|QREPORT|QRELAYED, q->q_flags)) 70759082Seric { 70859082Seric if (printheader) 70959082Seric { 71067880Seric putline(" ----- The following addresses have delivery notifications -----", 71165870Seric mci); 71259082Seric printheader = FALSE; 71359082Seric } 71463849Seric strcpy(buf, q->q_paddr); 71563787Seric if (bitset(QBADADDR, q->q_flags)) 71663849Seric strcat(buf, " (unrecoverable error)"); 71767981Seric else if (bitset(QRELAYED, q->q_flags)) 71867981Seric strcat(buf, " (relayed to non-DSN-aware mailer)"); 71967880Seric else if (bitset(QSENT, q->q_flags)) 72067880Seric strcat(buf, " (successfully delivered)"); 72163787Seric else 72263849Seric strcat(buf, " (transient failure)"); 72365870Seric putline(buf, mci); 72463849Seric if (q->q_alias != NULL) 72563849Seric { 72663849Seric strcpy(buf, " (expanded from: "); 72763849Seric strcat(buf, q->q_alias->q_paddr); 72863849Seric strcat(buf, ")"); 72965870Seric putline(buf, mci); 73063849Seric } 73159082Seric } 73259082Seric } 73359082Seric if (!printheader) 73465870Seric putline("\n", mci); 73559082Seric 73659082Seric /* 7379057Seric ** Output transcript of errors 7389057Seric */ 7399057Seric 7404086Seric (void) fflush(stdout); 7419542Seric p = queuename(e->e_parent, 'x'); 7429337Seric if ((xfile = fopen(p, "r")) == NULL) 7439057Seric { 7449337Seric syserr("Cannot open %s", p); 74565870Seric putline(" ----- Transcript of session is unavailable -----\n", mci); 7469057Seric } 7479057Seric else 7489057Seric { 74965870Seric putline(" ----- Transcript of session follows -----\n", mci); 7509542Seric if (e->e_xfp != NULL) 7519542Seric (void) fflush(e->e_xfp); 7529057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 75365870Seric putline(buf, mci); 75458680Seric (void) xfclose(xfile, "errbody xscript", p); 7559057Seric } 756297Seric errno = 0; 7574318Seric 75868028Seric #ifdef DSN 7594318Seric /* 76067880Seric ** Output machine-readable version. 76167880Seric */ 76267880Seric 76367880Seric if (e->e_msgboundary != NULL) 76467880Seric { 76567880Seric putline("", mci); 76667880Seric (void) sprintf(buf, "--%s", e->e_msgboundary); 76767880Seric putline(buf, mci); 76868228Seric putline("Content-Type: message/X-delivery-status-2 (Draft of 20 January 1995)", mci); 76967880Seric putline("", mci); 77067880Seric 77167880Seric /* 77267880Seric ** Output per-message information. 77367880Seric */ 77467880Seric 77567880Seric /* original envelope id from MAIL FROM: line */ 77667880Seric if (e->e_parent->e_envid != NULL) 77767880Seric { 77867880Seric (void) sprintf(buf, "Original-Envelope-Id: %s", 77968228Seric xtextify(e->e_parent->e_envid)); 78067880Seric putline(buf, mci); 78167880Seric } 78267880Seric 78368228Seric /* Reporting-MTA: is us (required) */ 78468228Seric p = e->e_parent->e_from.q_mailer->m_mtatype; 78568228Seric if (p == NULL) 78668228Seric p = "dns"; 78768228Seric (void) sprintf(buf, "Reporting-MTA: %s; %s", p, MyHostName); 78867880Seric putline(buf, mci); 78967880Seric 79068228Seric /* Received-From-MTA: shows where we got this message from */ 79167990Seric if (RealHostName != NULL) 79267990Seric { 79368228Seric /* XXX use $s for type? */ 79468228Seric p = e->e_parent->e_from.q_mailer->m_mtatype; 79568228Seric if (p == NULL) 79668228Seric p = "dns"; 79768228Seric (void) sprintf(buf, "Received-From-MTA: %s; %s", 79868228Seric p, RealHostName); 79967990Seric putline(buf, mci); 80067990Seric } 80167963Seric 80267963Seric /* Arrival-Date: -- when it arrived here */ 80367963Seric (void) sprintf(buf, "Arrival-Date: %s", 80467963Seric arpadate(ctime(&e->e_parent->e_ctime))); 80567963Seric putline(buf, mci); 80667963Seric 80767880Seric /* 80867880Seric ** Output per-address information. 80967880Seric */ 81067880Seric 81167880Seric for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 81267880Seric { 81367880Seric register ADDRESS *r; 81467880Seric 81567880Seric if (!bitset(QBADADDR|QREPORT|QRELAYED, q->q_flags)) 81667880Seric continue; 81767880Seric putline("", mci); 81867880Seric 81968228Seric /* Original-Recipient: -- passed from on high */ 82068228Seric if (q->q_orcpt != NULL) 82168228Seric { 82268228Seric (void) sprintf(buf, "Original-Recipient: %s", 82368228Seric xtextify(q->q_orcpt)); 82468228Seric putline(buf, mci); 82568228Seric } 82668228Seric 82768228Seric /* Final-Recipient: -- the name from the RCPT command */ 82868228Seric p = e->e_parent->e_from.q_mailer->m_addrtype; 82968228Seric if (p == NULL) 83068228Seric p = "rfc822"; 83168228Seric for (r = q; r->q_alias != NULL; r = r->q_alias) 83268228Seric continue; 83368228Seric if (strchr(r->q_user, '@') == NULL) 83468228Seric (void) sprintf(buf, "Final-Recipient: %s; %s@%s", 83568228Seric p, xtextify(r->q_user), MyHostName); 83667998Seric else 83768228Seric (void) sprintf(buf, "Final-Recipient: %s; %s", 83868228Seric p, xtextify(r->q_user)); 83967880Seric putline(buf, mci); 84067880Seric 84167880Seric /* Action: -- what happened? */ 84267880Seric if (bitset(QBADADDR, q->q_flags)) 84368228Seric putline("Action: failure", mci); 84467880Seric else if (bitset(QQUEUEUP, q->q_flags)) 84567880Seric putline("Action: delayed", mci); 84667880Seric else if (bitset(QRELAYED, q->q_flags)) 84767880Seric putline("Action: relayed", mci); 84867880Seric else 84967880Seric putline("Action: delivered", mci); 85067880Seric 85167880Seric /* Status: -- what _really_ happened? */ 85267880Seric strcpy(buf, "Status: "); 85367880Seric if (q->q_status != NULL) 85467880Seric strcat(buf, q->q_status); 85567880Seric else if (bitset(QBADADDR, q->q_flags)) 85668228Seric strcat(buf, "5.0.0"); 85767880Seric else if (bitset(QQUEUEUP, q->q_flags)) 85868228Seric strcat(buf, "4.0.0"); 85967880Seric else if (bitset(QRELAYED, q->q_flags)) 86068228Seric strcat(buf, "6.0.1"); 86167880Seric else 86268228Seric strcat(buf, "2.0.0"); 86367880Seric putline(buf, mci); 86467880Seric 86568228Seric /* Remote-MTA: -- who was I talking to? */ 86668228Seric p = q->q_mailer->m_mtatype; 86768228Seric if (p == NULL) 86868228Seric p = "dns"; 86968228Seric (void) sprintf(buf, "Remote-MTA: %s; ", p); 87068228Seric if (q->q_statmta != NULL) 87168228Seric p = q->q_statmta; 87268228Seric else if (q->q_host != NULL) 87368228Seric p = q->q_host; 87468228Seric else 87568228Seric p = NULL; 87668228Seric if (p != NULL) 87768228Seric { 87868228Seric strcat(buf, p); 87968228Seric p = &buf[strlen(buf) - 1]; 88068228Seric if (*p == '.') 88168228Seric *p = '\0'; 88268228Seric putline(buf, mci); 88368228Seric } 88468228Seric 88568228Seric /* Diagnostic-Code: -- actual result from other end */ 88668228Seric if (q->q_rstatus != NULL) 88768228Seric { 88868228Seric p = q->q_mailer->m_diagtype; 88968228Seric if (p == NULL) 89068228Seric p = "smtp"; 89168228Seric (void) sprintf(buf, "Diagnostic-Code: %s; %s", 89268228Seric p, q->q_rstatus); 89368228Seric putline(buf, mci); 89468228Seric } 89568228Seric 89668228Seric /* Last-Attempt-Date: -- fine granularity */ 89767880Seric if (q->q_statdate == (time_t) 0L) 89867880Seric q->q_statdate = curtime(); 89968228Seric (void) sprintf(buf, "Last-Attempt-Date: %s", 90067880Seric arpadate(ctime(&q->q_statdate))); 90167880Seric putline(buf, mci); 90267880Seric 90367963Seric /* Expiry-Date: -- for delayed messages only */ 90467963Seric if (bitset(QQUEUEUP, q->q_flags) && 90567963Seric !bitset(QBADADDR, q->q_flags)) 90667963Seric { 90767963Seric time_t xdate; 90867963Seric 90967963Seric xdate = e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass]; 91067963Seric sprintf(buf, "Expiry-Date: %s", 91167963Seric arpadate(ctime(&xdate))); 91267963Seric putline(buf, mci); 91367963Seric } 91467880Seric } 91567880Seric } 91668028Seric #endif 91767880Seric 91867880Seric /* 9194318Seric ** Output text of original message 9204318Seric */ 9214318Seric 92267880Seric if (bitset(EF_NORETURN, e->e_parent->e_flags)) 92358665Seric SendBody = FALSE; 92465870Seric putline("", mci); 92558680Seric if (e->e_parent->e_df != NULL) 9264199Seric { 92767880Seric if (e->e_msgboundary == NULL) 92867880Seric { 92967880Seric if (SendBody) 93067880Seric putline(" ----- Original message follows -----\n", mci); 93167880Seric else 93267880Seric putline(" ----- Message header follows -----\n", mci); 93367880Seric (void) fflush(mci->mci_out); 93467880Seric } 9355984Seric else 9365984Seric { 93759730Seric (void) sprintf(buf, "--%s", e->e_msgboundary); 93865870Seric putline(buf, mci); 93968228Seric (void) sprintf(buf, "Content-Type: message/rfc822%s", 94068228Seric mci, SendBody ? "" : "-headers"); 94168228Seric putline(buf, mci); 9425984Seric } 94367981Seric putline("", mci); 94468228Seric putheader(mci, e->e_parent->e_header, e->e_parent); 94559730Seric if (SendBody) 94668228Seric putbody(mci, e->e_parent, e->e_msgboundary); 94768228Seric else if (e->e_msgboundary == NULL) 94867546Seric { 94967546Seric putline("", mci); 95065870Seric putline(" ----- Message body suppressed -----", mci); 95167546Seric } 9524199Seric } 95368228Seric else if (e->e_msgboundary == NULL) 95410170Seric { 95565870Seric putline(" ----- No message was collected -----\n", mci); 95610170Seric } 9574318Seric 95860010Seric if (e->e_msgboundary != NULL) 95960010Seric { 96065870Seric putline("", mci); 96160010Seric (void) sprintf(buf, "--%s--", e->e_msgboundary); 96265870Seric putline(buf, mci); 96360010Seric } 96465870Seric putline("", mci); 96559730Seric 9664318Seric /* 9674318Seric ** Cleanup and exit 9684318Seric */ 9694318Seric 970297Seric if (errno != 0) 9716978Seric syserr("errbody: I/O error"); 972297Seric } 97358144Seric /* 97468228Seric ** SMTPTODSN -- convert SMTP to DSN status code 97568228Seric ** 97668228Seric ** Parameters: 97768228Seric ** smtpstat -- the smtp status code (e.g., 550). 97868228Seric ** 97968228Seric ** Returns: 98068228Seric ** The DSN version of the status code. 98168228Seric */ 98268228Seric 98368228Seric char * 98468228Seric smtptodsn(smtpstat) 98568228Seric int smtpstat; 98668228Seric { 98768228Seric switch (smtpstat) 98868228Seric { 98968228Seric case 450: /* Req mail action not taken: mailbox unavailable */ 99068228Seric return "4.2.0"; 99168228Seric 99268228Seric case 451: /* Req action aborted: local error in processing */ 99368228Seric return "4.3.0"; 99468228Seric 99568228Seric case 452: /* Req action not taken: insufficient sys storage */ 99668228Seric return "4.3.1"; 99768228Seric 99868228Seric case 500: /* Syntax error, command unrecognized */ 99968228Seric return "5.5.2"; 100068228Seric 100168228Seric case 501: /* Syntax error in parameters or arguments */ 100268228Seric return "5.5.4"; 100368228Seric 100468228Seric case 502: /* Command not implemented */ 100568228Seric return "5.5.1"; 100668228Seric 100768228Seric case 503: /* Bad sequence of commands */ 100868228Seric return "5.5.1"; 100968228Seric 101068228Seric case 504: /* Command parameter not implemented */ 101168228Seric return "5.5.4"; 101268228Seric 101368228Seric case 550: /* Req mail action not taken: mailbox unavailable */ 101468228Seric return "5.2.0"; 101568228Seric 101668228Seric case 551: /* User not local; please try <...> */ 101768228Seric return "5.1.6"; 101868228Seric 101968228Seric case 552: /* Req mail action aborted: exceeded storage alloc */ 102068228Seric return "5.2.2"; 102168228Seric 102268228Seric case 553: /* Req action not taken: mailbox name not allowed */ 102368228Seric return "5.1.3"; 102468228Seric 102568228Seric case 554: /* Transaction failed */ 102668228Seric return "5.0.0"; 102768228Seric } 102868228Seric 102968228Seric if ((smtpstat / 100) == 2) 103068228Seric return "2.0.0"; 103168228Seric if ((smtpstat / 100) == 4) 103268228Seric return "4.0.0"; 103368228Seric return "5.0.0"; 103468228Seric } 103568228Seric /* 103668228Seric ** XTEXTIFY -- take regular text and turn it into DSN-style xtext 103768228Seric ** 103868228Seric ** Parameters: 103968228Seric ** t -- the text to convert. 104068228Seric ** 104168228Seric ** Returns: 104268228Seric ** The xtext-ified version of the same string. 104368228Seric */ 104468228Seric 104568228Seric char * 104668228Seric xtextify(t) 104768228Seric register char *t; 104868228Seric { 104968228Seric register char *p; 105068228Seric int l; 105168228Seric int nbogus; 105268228Seric static char *bp = NULL; 105368228Seric static int bplen = 0; 105468228Seric 105568228Seric /* figure out how long this xtext will have to be */ 105668228Seric nbogus = l = 0; 105768228Seric for (p = t; *p != '\0'; p++) 105868228Seric { 105968228Seric register int c = (*p & 0xff); 106068228Seric 106168228Seric /* ASCII dependence here -- this is the way the spec words it */ 106268228Seric if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(') 106368228Seric nbogus++; 106468228Seric l++; 106568228Seric } 106668228Seric if (nbogus == 0) 106768228Seric return t; 106868228Seric l += nbogus * 2 + 1; 106968228Seric 107068228Seric /* now allocate space if necessary for the new string */ 107168228Seric if (l > bplen) 107268228Seric { 107368228Seric if (bp != NULL) 107468228Seric free(bp); 107568228Seric bp = xalloc(l); 107668228Seric bplen = l; 107768228Seric } 107868228Seric 107968228Seric /* ok, copy the text with byte expansion */ 108068228Seric for (p = bp; *t != '\0'; ) 108168228Seric { 108268228Seric register int c = (*t++ & 0xff); 108368228Seric 108468228Seric /* ASCII dependence here -- this is the way the spec words it */ 108568228Seric if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(') 108668228Seric { 108768228Seric *p++ = '+'; 108868228Seric *p++ = "0123456789abcdef"[c >> 4]; 108968228Seric *p++ = "0123456789abcdef"[c & 0xf]; 109068228Seric } 109168228Seric else 109268228Seric *p++ = c; 109368228Seric } 109468228Seric *p = '\0'; 109568228Seric return bp; 109668228Seric } 109768228Seric /* 109858144Seric ** PRUNEROUTE -- prune an RFC-822 source route 109958144Seric ** 110058144Seric ** Trims down a source route to the last internet-registered hop. 110158144Seric ** This is encouraged by RFC 1123 section 5.3.3. 110258144Seric ** 110358144Seric ** Parameters: 110458144Seric ** addr -- the address 110558144Seric ** 110658144Seric ** Returns: 110758144Seric ** TRUE -- address was modified 110858144Seric ** FALSE -- address could not be pruned 110958144Seric ** 111058144Seric ** Side Effects: 111158144Seric ** modifies addr in-place 111258144Seric */ 111358144Seric 111458144Seric pruneroute(addr) 111558144Seric char *addr; 111658144Seric { 111766334Seric #if NAMED_BIND 111858144Seric char *start, *at, *comma; 111958144Seric char c; 112058144Seric int rcode; 112158144Seric char hostbuf[BUFSIZ]; 112258144Seric char *mxhosts[MAXMXHOSTS + 1]; 112358144Seric 112458144Seric /* check to see if this is really a route-addr */ 112558144Seric if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>') 112658144Seric return FALSE; 112758144Seric start = strchr(addr, ':'); 112858144Seric at = strrchr(addr, '@'); 112958144Seric if (start == NULL || at == NULL || at < start) 113058144Seric return FALSE; 113158144Seric 113258144Seric /* slice off the angle brackets */ 113358144Seric strcpy(hostbuf, at + 1); 113458144Seric hostbuf[strlen(hostbuf) - 1] = '\0'; 113558144Seric 113658144Seric while (start) 113758144Seric { 113859273Seric if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0) 113958144Seric { 114058144Seric strcpy(addr + 1, start + 1); 114158144Seric return TRUE; 114258144Seric } 114358144Seric c = *start; 114458144Seric *start = '\0'; 114558144Seric comma = strrchr(addr, ','); 114658144Seric if (comma && comma[1] == '@') 114758144Seric strcpy(hostbuf, comma + 2); 114858144Seric else 114958144Seric comma = 0; 115058144Seric *start = c; 115158144Seric start = comma; 115258144Seric } 115358144Seric #endif 115458144Seric return FALSE; 115558144Seric } 1156