14227346bSdist /* 20942ea6aSbostic * Copyright (c) 1983 Eric P. Allman 3e70a7521Sbostic * Copyright (c) 1988 Regents of the University of California. 4e70a7521Sbostic * All rights reserved. 5e70a7521Sbostic * 63bc94712Sbostic * %sccs.include.redist.c% 74227346bSdist */ 84227346bSdist 94227346bSdist #ifndef lint 10*1caa9a59Seric static char sccsid[] = "@(#)deliver.c 6.19 (Berkeley) 02/20/93"; 11e70a7521Sbostic #endif /* not lint */ 124227346bSdist 13fcde9cc8Sbostic #include "sendmail.h" 14911693bfSbostic #include <sys/signal.h> 15c77d1c25Seric #include <sys/stat.h> 16f28da541Smiriam #include <netdb.h> 17fccb3f7aSbostic #include <fcntl.h> 18911693bfSbostic #include <errno.h> 19134746fbSeric #ifdef NAMED_BIND 20912a731aSbostic #include <arpa/nameser.h> 21912a731aSbostic #include <resolv.h> 22134746fbSeric #endif 2325a99e2eSeric 2425a99e2eSeric /* 2513bbc08cSeric ** DELIVER -- Deliver a message to a list of addresses. 2613bbc08cSeric ** 2713bbc08cSeric ** This routine delivers to everyone on the same host as the 2813bbc08cSeric ** user on the head of the list. It is clever about mailers 2913bbc08cSeric ** that don't handle multiple users. It is NOT guaranteed 3013bbc08cSeric ** that it will deliver to all these addresses however -- so 3113bbc08cSeric ** deliver should be called once for each address on the 3213bbc08cSeric ** list. 3325a99e2eSeric ** 3425a99e2eSeric ** Parameters: 35588cad61Seric ** e -- the envelope to deliver. 36c77d1c25Seric ** firstto -- head of the address list to deliver to. 3725a99e2eSeric ** 3825a99e2eSeric ** Returns: 3925a99e2eSeric ** zero -- successfully delivered. 4025a99e2eSeric ** else -- some failure, see ExitStat for more info. 4125a99e2eSeric ** 4225a99e2eSeric ** Side Effects: 4325a99e2eSeric ** The standard input is passed off to someone. 4425a99e2eSeric */ 4525a99e2eSeric 46588cad61Seric deliver(e, firstto) 47588cad61Seric register ENVELOPE *e; 48c77d1c25Seric ADDRESS *firstto; 4925a99e2eSeric { 5078442df3Seric char *host; /* host being sent to */ 5178442df3Seric char *user; /* user being sent to */ 5225a99e2eSeric char **pvp; 535dfc646bSeric register char **mvp; 5425a99e2eSeric register char *p; 55588cad61Seric register MAILER *m; /* mailer for this recipient */ 566259796dSeric ADDRESS *ctladdr; 57b31e7f2bSeric register MCI *mci; 58c77d1c25Seric register ADDRESS *to = firstto; 59c579ef51Seric bool clever = FALSE; /* running user smtp to this mailer */ 60772e6e50Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 61911693bfSbostic int rcode; /* response code */ 62c23ed322Seric char *from; /* pointer to from person */ 63e103b48fSeric char *firstsig; /* signature of firstto */ 64ee6bf8dfSeric char *pv[MAXPV+1]; 65ee6bf8dfSeric char tobuf[MAXLINE-50]; /* text line of to people */ 66ee6bf8dfSeric char buf[MAXNAME]; 67ee6bf8dfSeric char tfrombuf[MAXNAME]; /* translated from person */ 68c23ed322Seric char rpathbuf[MAXNAME]; /* translated return path */ 69fabb3bd4Seric extern int checkcompat(); 70ee6bf8dfSeric extern ADDRESS *getctladdr(); 71ee6bf8dfSeric extern char *remotename(); 72b31e7f2bSeric extern MCI *openmailer(); 73e103b48fSeric extern char *hostsignature(); 7425a99e2eSeric 7535490626Seric errno = 0; 76da2935e1Seric if (bitset(QDONTSEND, to->q_flags)) 775dfc646bSeric return (0); 7825a99e2eSeric 79134746fbSeric #ifdef NAMED_BIND 80912a731aSbostic /* unless interactive, try twice, over a minute */ 81912a731aSbostic if (OpMode == MD_DAEMON || OpMode == MD_SMTP) { 82912a731aSbostic _res.retrans = 30; 83912a731aSbostic _res.retry = 2; 84912a731aSbostic } 85d4bd8f0eSbostic #endif 86912a731aSbostic 8751552439Seric m = to->q_mailer; 8851552439Seric host = to->q_host; 8951552439Seric 906ef48975Seric if (tTd(10, 1)) 915dfc646bSeric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 9251552439Seric m->m_mno, host, to->q_user); 93f3dbc832Seric 94f3dbc832Seric /* 95f3dbc832Seric ** If this mailer is expensive, and if we don't want to make 96f3dbc832Seric ** connections now, just mark these addresses and return. 97f3dbc832Seric ** This is useful if we want to batch connections to 98f3dbc832Seric ** reduce load. This will cause the messages to be 99f3dbc832Seric ** queued up, and a daemon will come along to send the 100f3dbc832Seric ** messages later. 101f3dbc832Seric ** This should be on a per-mailer basis. 102f3dbc832Seric */ 103f3dbc832Seric 10457fc6f17Seric if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) && 105317680d6Seric !Verbose) 106f3dbc832Seric { 107f3dbc832Seric for (; to != NULL; to = to->q_next) 108f4560e80Seric { 109f4560e80Seric if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m) 110f4560e80Seric continue; 111f3dbc832Seric to->q_flags |= QQUEUEUP|QDONTSEND; 112588cad61Seric e->e_to = to->q_paddr; 113eb238f8cSeric message(Arpa_Info, "queued"); 1142f624c86Seric if (LogLevel > 8) 115b31e7f2bSeric logdelivery("queued", e); 116f4560e80Seric } 117588cad61Seric e->e_to = NULL; 118f3dbc832Seric return (0); 119f3dbc832Seric } 120f3dbc832Seric 12125a99e2eSeric /* 1225dfc646bSeric ** Do initial argv setup. 1235dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 1245dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 1255dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 1265dfc646bSeric ** code does not check for 'pv' overflow; this places a 1275dfc646bSeric ** manifest lower limit of 4 for MAXPV. 1283bea8136Seric ** The from address rewrite is expected to make 1293bea8136Seric ** the address relative to the other end. 1305dfc646bSeric */ 1315dfc646bSeric 13278442df3Seric /* rewrite from address, using rewriting rules */ 1332f624c86Seric (void) strcpy(rpathbuf, remotename(e->e_returnpath, m, TRUE, FALSE, 1342f624c86Seric TRUE, e)); 135c23ed322Seric if (e->e_returnpath == e->e_sender) 136c23ed322Seric { 137c23ed322Seric from = rpathbuf; 138c23ed322Seric } 139c23ed322Seric else 140c23ed322Seric { 1412f624c86Seric (void) strcpy(tfrombuf, remotename(e->e_sender, m, TRUE, FALSE, 1422f624c86Seric TRUE, e)); 143c23ed322Seric from = tfrombuf; 144c23ed322Seric } 14578442df3Seric 146c23ed322Seric define('f', e->e_returnpath, e); /* raw return path */ 147c23ed322Seric define('<', rpathbuf, e); /* translated return path */ 148c23ed322Seric define('g', from, e); /* translated sender */ 149588cad61Seric define('h', host, e); /* to host */ 1505dfc646bSeric Errors = 0; 1515dfc646bSeric pvp = pv; 1525dfc646bSeric *pvp++ = m->m_argv[0]; 1535dfc646bSeric 1545dfc646bSeric /* insert -f or -r flag as appropriate */ 15557fc6f17Seric if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 1565dfc646bSeric { 15757fc6f17Seric if (bitnset(M_FOPT, m->m_flags)) 1585dfc646bSeric *pvp++ = "-f"; 1595dfc646bSeric else 1605dfc646bSeric *pvp++ = "-r"; 161c23ed322Seric *pvp++ = newstr(rpathbuf); 1625dfc646bSeric } 1635dfc646bSeric 1645dfc646bSeric /* 1655dfc646bSeric ** Append the other fixed parts of the argv. These run 1665dfc646bSeric ** up to the first entry containing "$u". There can only 1675dfc646bSeric ** be one of these, and there are only a few more slots 1685dfc646bSeric ** in the pv after it. 1695dfc646bSeric */ 1705dfc646bSeric 1715dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 1725dfc646bSeric { 1732bc47524Seric /* can't use strchr here because of sign extension problems */ 1742bc47524Seric while (*p != '\0') 1752bc47524Seric { 1762bc47524Seric if ((*p++ & 0377) == MACROEXPAND) 1772bc47524Seric { 1782bc47524Seric if (*p == 'u') 1795dfc646bSeric break; 1802bc47524Seric } 1812bc47524Seric } 1822bc47524Seric 1832bc47524Seric if (*p != '\0') 1845dfc646bSeric break; 1855dfc646bSeric 1865dfc646bSeric /* this entry is safe -- go ahead and process it */ 187588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 1885dfc646bSeric *pvp++ = newstr(buf); 1895dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 1905dfc646bSeric { 1915dfc646bSeric syserr("Too many parameters to %s before $u", pv[0]); 1925dfc646bSeric return (-1); 1935dfc646bSeric } 1945dfc646bSeric } 195c579ef51Seric 19633db8731Seric /* 19733db8731Seric ** If we have no substitution for the user name in the argument 19833db8731Seric ** list, we know that we must supply the names otherwise -- and 19933db8731Seric ** SMTP is the answer!! 20033db8731Seric */ 20133db8731Seric 2025dfc646bSeric if (*mvp == NULL) 203c579ef51Seric { 204c579ef51Seric /* running SMTP */ 2052c7e1b8dSeric # ifdef SMTP 206c579ef51Seric clever = TRUE; 207c579ef51Seric *pvp = NULL; 2086c2c3107Seric # else /* SMTP */ 20933db8731Seric /* oops! we don't implement SMTP */ 2102c7e1b8dSeric syserr("SMTP style mailer"); 2112c7e1b8dSeric return (EX_SOFTWARE); 2126c2c3107Seric # endif /* SMTP */ 213c579ef51Seric } 2145dfc646bSeric 2155dfc646bSeric /* 2165dfc646bSeric ** At this point *mvp points to the argument with $u. We 2175dfc646bSeric ** run through our address list and append all the addresses 2185dfc646bSeric ** we can. If we run out of space, do not fret! We can 2195dfc646bSeric ** always send another copy later. 2205dfc646bSeric */ 2215dfc646bSeric 2225dfc646bSeric tobuf[0] = '\0'; 223588cad61Seric e->e_to = tobuf; 2246259796dSeric ctladdr = NULL; 225e103b48fSeric firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e); 2265dfc646bSeric for (; to != NULL; to = to->q_next) 2275dfc646bSeric { 2285dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 22957fc6f17Seric if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 2305dfc646bSeric break; 2315dfc646bSeric 2325dfc646bSeric /* if already sent or not for this host, don't send */ 233da2935e1Seric if (bitset(QDONTSEND, to->q_flags) || 234e103b48fSeric to->q_mailer != firstto->q_mailer || 235e103b48fSeric strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0) 2365dfc646bSeric continue; 2376259796dSeric 2384b22ea87Seric /* avoid overflowing tobuf */ 239aa50a568Sbostic if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 2404b22ea87Seric break; 2414b22ea87Seric 2426ef48975Seric if (tTd(10, 1)) 243772e6e50Seric { 244772e6e50Seric printf("\nsend to "); 245772e6e50Seric printaddr(to, FALSE); 246772e6e50Seric } 247772e6e50Seric 2486259796dSeric /* compute effective uid/gid when sending */ 2497da1035fSeric if (to->q_mailer == ProgMailer) 2506259796dSeric ctladdr = getctladdr(to); 2516259796dSeric 2525dfc646bSeric user = to->q_user; 253588cad61Seric e->e_to = to->q_paddr; 2545dfc646bSeric to->q_flags |= QDONTSEND; 25575f1ade9Seric if (tTd(10, 5)) 25675f1ade9Seric { 25775f1ade9Seric printf("deliver: QDONTSEND "); 25875f1ade9Seric printaddr(to, FALSE); 25975f1ade9Seric } 2605dfc646bSeric 2615dfc646bSeric /* 2625dfc646bSeric ** Check to see that these people are allowed to 2635dfc646bSeric ** talk to each other. 2642a6e0786Seric */ 2652a6e0786Seric 26669582d2fSeric if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 26769582d2fSeric { 26869582d2fSeric NoReturn = TRUE; 269672bec4aSeric usrerr("Message is too large; %ld bytes max", m->m_maxsize); 27069582d2fSeric giveresponse(EX_UNAVAILABLE, m, e); 27169582d2fSeric continue; 27269582d2fSeric } 273fabb3bd4Seric rcode = checkcompat(to, e); 2741793c9c5Seric if (rcode != EX_OK) 2755dfc646bSeric { 2761793c9c5Seric giveresponse(rcode, m, e); 2775dfc646bSeric continue; 2785dfc646bSeric } 2792a6e0786Seric 2802a6e0786Seric /* 2819ec9501bSeric ** Strip quote bits from names if the mailer is dumb 2829ec9501bSeric ** about them. 28325a99e2eSeric */ 28425a99e2eSeric 28557fc6f17Seric if (bitnset(M_STRIPQ, m->m_flags)) 28625a99e2eSeric { 2871d8f1806Seric stripquotes(user); 2881d8f1806Seric stripquotes(host); 28925a99e2eSeric } 29025a99e2eSeric 291cdb828c5Seric /* hack attack -- delivermail compatibility */ 292cdb828c5Seric if (m == ProgMailer && *user == '|') 293cdb828c5Seric user++; 294cdb828c5Seric 29525a99e2eSeric /* 2963efaed6eSeric ** If an error message has already been given, don't 2973efaed6eSeric ** bother to send to this address. 2983efaed6eSeric ** 2993efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 3003efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 3013efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 3023efaed6eSeric */ 3033efaed6eSeric 3046cae517dSeric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 3053efaed6eSeric continue; 3063efaed6eSeric 307f2fec898Seric /* save statistics.... */ 308588cad61Seric markstats(e, to); 309f2fec898Seric 3103efaed6eSeric /* 31125a99e2eSeric ** See if this user name is "special". 31225a99e2eSeric ** If the user name has a slash in it, assume that this 31351552439Seric ** is a file -- send it off without further ado. Note 31451552439Seric ** that this type of addresses is not processed along 31551552439Seric ** with the others, so we fudge on the To person. 31625a99e2eSeric */ 31725a99e2eSeric 3182c017f8dSeric if (m == FileMailer) 31925a99e2eSeric { 320b31e7f2bSeric rcode = mailfile(user, getctladdr(to), e); 321198d9be0Seric giveresponse(rcode, m, e); 322dde5acadSeric if (rcode == EX_OK) 323dde5acadSeric to->q_flags |= QSENT; 3245dfc646bSeric continue; 32525a99e2eSeric } 32625a99e2eSeric 32713bbc08cSeric /* 32813bbc08cSeric ** Address is verified -- add this user to mailer 32913bbc08cSeric ** argv, and add it to the print list of recipients. 33013bbc08cSeric */ 33113bbc08cSeric 332508daeccSeric /* link together the chain of recipients */ 333508daeccSeric to->q_tchain = tochain; 334508daeccSeric tochain = to; 335508daeccSeric 3365dfc646bSeric /* create list of users for error messages */ 337db8841e9Seric (void) strcat(tobuf, ","); 338db8841e9Seric (void) strcat(tobuf, to->q_paddr); 339588cad61Seric define('u', user, e); /* to user */ 340588cad61Seric define('z', to->q_home, e); /* user's home */ 3415dfc646bSeric 342c579ef51Seric /* 343508daeccSeric ** Expand out this user into argument list. 344c579ef51Seric */ 345c579ef51Seric 346508daeccSeric if (!clever) 347c579ef51Seric { 348588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 3495dfc646bSeric *pvp++ = newstr(buf); 3505dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 3515dfc646bSeric { 3525dfc646bSeric /* allow some space for trailing parms */ 3535dfc646bSeric break; 3545dfc646bSeric } 3555dfc646bSeric } 356c579ef51Seric } 3575dfc646bSeric 358145b49b1Seric /* see if any addresses still exist */ 359145b49b1Seric if (tobuf[0] == '\0') 360c579ef51Seric { 361588cad61Seric define('g', (char *) NULL, e); 362c23ed322Seric define('<', (char *) NULL, e); 363145b49b1Seric return (0); 364c579ef51Seric } 365145b49b1Seric 3665dfc646bSeric /* print out messages as full list */ 36763780dbdSeric e->e_to = tobuf + 1; 3685dfc646bSeric 3695dfc646bSeric /* 3705dfc646bSeric ** Fill out any parameters after the $u parameter. 3715dfc646bSeric */ 3725dfc646bSeric 373c579ef51Seric while (!clever && *++mvp != NULL) 3745dfc646bSeric { 375588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 3765dfc646bSeric *pvp++ = newstr(buf); 3775dfc646bSeric if (pvp >= &pv[MAXPV]) 3785dfc646bSeric syserr("deliver: pv overflow after $u for %s", pv[0]); 3795dfc646bSeric } 3805dfc646bSeric *pvp++ = NULL; 3815dfc646bSeric 38225a99e2eSeric /* 38325a99e2eSeric ** Call the mailer. 3846328bdf7Seric ** The argument vector gets built, pipes 38525a99e2eSeric ** are created as necessary, and we fork & exec as 3866328bdf7Seric ** appropriate. 387c579ef51Seric ** If we are running SMTP, we just need to clean up. 38825a99e2eSeric */ 38925a99e2eSeric 39086b26461Seric if (ctladdr == NULL) 39186b26461Seric ctladdr = &e->e_from; 392134746fbSeric #ifdef NAMED_BIND 3932bcc6d2dSeric if (ConfigLevel < 2) 394912a731aSbostic _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 395134746fbSeric #endif 396b31e7f2bSeric mci = openmailer(m, pv, ctladdr, clever, e); 397b31e7f2bSeric if (mci == NULL) 398134746fbSeric { 399b31e7f2bSeric /* catastrophic error */ 400845e533cSeric rcode = EX_OSERR; 401b31e7f2bSeric goto give_up; 402b31e7f2bSeric } 403b31e7f2bSeric else if (mci->mci_state != MCIS_OPEN) 404b31e7f2bSeric { 405b31e7f2bSeric /* couldn't open the mailer */ 406b31e7f2bSeric rcode = mci->mci_exitstat; 4072a6bc25bSeric errno = mci->mci_errno; 408b31e7f2bSeric if (rcode == EX_OK) 409b31e7f2bSeric { 410b31e7f2bSeric /* shouldn't happen */ 4116b0f339dSeric syserr("deliver: rcode=%d, mci_state=%d, sig=%s", 4126b0f339dSeric rcode, mci->mci_state, firstsig); 413b31e7f2bSeric rcode = EX_SOFTWARE; 414b31e7f2bSeric } 415b31e7f2bSeric } 416b31e7f2bSeric else if (!clever) 417b31e7f2bSeric { 418b31e7f2bSeric /* 419b31e7f2bSeric ** Format and send message. 420b31e7f2bSeric */ 42115d084d5Seric 422b31e7f2bSeric putfromline(mci->mci_out, m, e); 423b31e7f2bSeric (*e->e_puthdr)(mci->mci_out, m, e); 424b31e7f2bSeric putline("\n", mci->mci_out, m); 425b31e7f2bSeric (*e->e_putbody)(mci->mci_out, m, e); 426b31e7f2bSeric 427b31e7f2bSeric /* get the exit status */ 428b31e7f2bSeric rcode = endmailer(mci, pv[0]); 429134746fbSeric } 430134746fbSeric else 431b31e7f2bSeric #ifdef SMTP 432134746fbSeric { 433b31e7f2bSeric /* 434b31e7f2bSeric ** Send the MAIL FROM: protocol 435b31e7f2bSeric */ 43615d084d5Seric 437b31e7f2bSeric rcode = smtpmailfrom(m, mci, e); 438b31e7f2bSeric if (rcode == EX_OK) 43975889e88Seric { 440ded0d3daSkarels register char *t = tobuf; 441ded0d3daSkarels register int i; 442ded0d3daSkarels 443588cad61Seric /* send the recipient list */ 44463780dbdSeric tobuf[0] = '\0'; 44575889e88Seric for (to = tochain; to != NULL; to = to->q_tchain) 44675889e88Seric { 44763780dbdSeric e->e_to = to->q_paddr; 44815d084d5Seric if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 44975889e88Seric { 45083b7ddc9Seric markfailure(e, to, i); 451198d9be0Seric giveresponse(i, m, e); 45263780dbdSeric } 45375889e88Seric else 45475889e88Seric { 455911693bfSbostic *t++ = ','; 456b31e7f2bSeric for (p = to->q_paddr; *p; *t++ = *p++) 457b31e7f2bSeric continue; 458588cad61Seric } 459588cad61Seric } 460588cad61Seric 46163780dbdSeric /* now send the data */ 46263780dbdSeric if (tobuf[0] == '\0') 463b31e7f2bSeric { 46463780dbdSeric e->e_to = NULL; 465b31e7f2bSeric if (bitset(MCIF_CACHED, mci->mci_flags)) 466b31e7f2bSeric smtprset(m, mci, e); 467b31e7f2bSeric } 46875889e88Seric else 46975889e88Seric { 47063780dbdSeric e->e_to = tobuf + 1; 47175889e88Seric rcode = smtpdata(m, mci, e); 47263780dbdSeric } 47363780dbdSeric 47463780dbdSeric /* now close the connection */ 475b31e7f2bSeric if (!bitset(MCIF_CACHED, mci->mci_flags)) 47615d084d5Seric smtpquit(m, mci, e); 47763780dbdSeric } 478c579ef51Seric } 479b31e7f2bSeric #else /* not SMTP */ 480a05b3449Sbostic { 481b31e7f2bSeric syserr("deliver: need SMTP compiled to use clever mailer"); 482845e533cSeric rcode = EX_CONFIG; 483b31e7f2bSeric goto give_up; 484a05b3449Sbostic } 485b31e7f2bSeric #endif /* SMTP */ 486134746fbSeric #ifdef NAMED_BIND 4872bcc6d2dSeric if (ConfigLevel < 2) 488912a731aSbostic _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 489134746fbSeric #endif 4905dfc646bSeric 491b31e7f2bSeric /* arrange a return receipt if requested */ 492b31e7f2bSeric if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags)) 493b31e7f2bSeric { 494b31e7f2bSeric e->e_flags |= EF_SENDRECEIPT; 495b31e7f2bSeric /* do we want to send back more info? */ 496b31e7f2bSeric } 497b31e7f2bSeric 498c77d1c25Seric /* 49963780dbdSeric ** Do final status disposal. 50063780dbdSeric ** We check for something in tobuf for the SMTP case. 501c77d1c25Seric ** If we got a temporary failure, arrange to queue the 502c77d1c25Seric ** addressees. 503c77d1c25Seric */ 504c77d1c25Seric 505b31e7f2bSeric give_up: 50663780dbdSeric if (tobuf[0] != '\0') 507198d9be0Seric giveresponse(rcode, m, e); 508772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 509b31e7f2bSeric { 510dde5acadSeric if (rcode != EX_OK) 51183b7ddc9Seric markfailure(e, to, rcode); 512dde5acadSeric else 513655518ecSeric { 514dde5acadSeric to->q_flags |= QSENT; 515655518ecSeric e->e_nsent++; 516655518ecSeric } 517b31e7f2bSeric } 518b31e7f2bSeric 519b31e7f2bSeric /* 520b31e7f2bSeric ** Restore state and return. 521b31e7f2bSeric */ 522c77d1c25Seric 52335490626Seric errno = 0; 524588cad61Seric define('g', (char *) NULL, e); 525c23ed322Seric define('<', (char *) NULL, e); 5265826d9d3Seric return (rcode); 52725a99e2eSeric } 5285dfc646bSeric /* 52983b7ddc9Seric ** MARKFAILURE -- mark a failure on a specific address. 53083b7ddc9Seric ** 53183b7ddc9Seric ** Parameters: 53283b7ddc9Seric ** e -- the envelope we are sending. 53383b7ddc9Seric ** q -- the address to mark. 53483b7ddc9Seric ** rcode -- the code signifying the particular failure. 53583b7ddc9Seric ** 53683b7ddc9Seric ** Returns: 53783b7ddc9Seric ** none. 53883b7ddc9Seric ** 53983b7ddc9Seric ** Side Effects: 54083b7ddc9Seric ** marks the address (and possibly the envelope) with the 54183b7ddc9Seric ** failure so that an error will be returned or 54283b7ddc9Seric ** the message will be queued, as appropriate. 54383b7ddc9Seric */ 54483b7ddc9Seric 54583b7ddc9Seric markfailure(e, q, rcode) 54683b7ddc9Seric register ENVELOPE *e; 54783b7ddc9Seric register ADDRESS *q; 54883b7ddc9Seric int rcode; 54983b7ddc9Seric { 55083b7ddc9Seric if (rcode == EX_OK) 55183b7ddc9Seric return; 552ef137175Sbostic else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR) 55383b7ddc9Seric q->q_flags |= QBADADDR; 55483b7ddc9Seric else if (curtime() > e->e_ctime + TimeOut) 55583b7ddc9Seric { 55683b7ddc9Seric extern char *pintvl(); 557198d9be0Seric char buf[MAXLINE]; 55883b7ddc9Seric 55983b7ddc9Seric if (!bitset(EF_TIMEOUT, e->e_flags)) 560198d9be0Seric { 561198d9be0Seric (void) sprintf(buf, "Cannot send message for %s", 56283b7ddc9Seric pintvl(TimeOut, FALSE)); 563198d9be0Seric if (e->e_message != NULL) 564198d9be0Seric free(e->e_message); 565198d9be0Seric e->e_message = newstr(buf); 566198d9be0Seric message(Arpa_Info, buf); 567198d9be0Seric } 56883b7ddc9Seric q->q_flags |= QBADADDR; 56983b7ddc9Seric e->e_flags |= EF_TIMEOUT; 570c13b5dfcSeric fprintf(e->e_xfp, "421 %s... Message timed out\n", q->q_paddr); 57183b7ddc9Seric } 57283b7ddc9Seric else 57383b7ddc9Seric q->q_flags |= QQUEUEUP; 57483b7ddc9Seric } 57583b7ddc9Seric /* 57632d19d43Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 57732d19d43Seric ** 57832d19d43Seric ** This MUST be a macro, since after a vfork we are running 57932d19d43Seric ** two processes on the same stack!!! 58032d19d43Seric ** 58132d19d43Seric ** Parameters: 58232d19d43Seric ** none. 58332d19d43Seric ** 58432d19d43Seric ** Returns: 58532d19d43Seric ** From a macro??? You've got to be kidding! 58632d19d43Seric ** 58732d19d43Seric ** Side Effects: 58832d19d43Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 58932d19d43Seric ** pid of child in parent, zero in child. 59032d19d43Seric ** -1 on unrecoverable error. 59132d19d43Seric ** 59232d19d43Seric ** Notes: 59332d19d43Seric ** I'm awfully sorry this looks so awful. That's 59432d19d43Seric ** vfork for you..... 59532d19d43Seric */ 59632d19d43Seric 59732d19d43Seric # define NFORKTRIES 5 59884f7cd1bSeric 59984f7cd1bSeric # ifndef FORK 60084f7cd1bSeric # define FORK fork 60184f7cd1bSeric # endif 60232d19d43Seric 60332d19d43Seric # define DOFORK(fORKfN) \ 60432d19d43Seric {\ 60532d19d43Seric register int i;\ 60632d19d43Seric \ 60711799049Seric for (i = NFORKTRIES; --i >= 0; )\ 60832d19d43Seric {\ 60932d19d43Seric pid = fORKfN();\ 61032d19d43Seric if (pid >= 0)\ 61132d19d43Seric break;\ 61211799049Seric if (i > 0)\ 6136c4635f6Seric sleep((unsigned) NFORKTRIES - i);\ 61432d19d43Seric }\ 61532d19d43Seric } 61632d19d43Seric /* 6172ed72599Seric ** DOFORK -- simple fork interface to DOFORK. 6182ed72599Seric ** 6192ed72599Seric ** Parameters: 6202ed72599Seric ** none. 6212ed72599Seric ** 6222ed72599Seric ** Returns: 6232ed72599Seric ** pid of child in parent. 6242ed72599Seric ** zero in child. 6252ed72599Seric ** -1 on error. 6262ed72599Seric ** 6272ed72599Seric ** Side Effects: 6282ed72599Seric ** returns twice, once in parent and once in child. 6292ed72599Seric */ 6302ed72599Seric 6312ed72599Seric dofork() 6322ed72599Seric { 6332ed72599Seric register int pid; 6342ed72599Seric 6352ed72599Seric DOFORK(fork); 6362ed72599Seric return (pid); 6372ed72599Seric } 6382ed72599Seric /* 639c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 640c579ef51Seric ** 641c579ef51Seric ** We should never get fatal errors (e.g., segmentation 642c579ef51Seric ** violation), so we report those specially. For other 643c579ef51Seric ** errors, we choose a status message (into statmsg), 644c579ef51Seric ** and if it represents an error, we print it. 645c579ef51Seric ** 646c579ef51Seric ** Parameters: 647c579ef51Seric ** pid -- pid of mailer. 648c579ef51Seric ** name -- name of mailer (for error messages). 649c579ef51Seric ** 650c579ef51Seric ** Returns: 651c579ef51Seric ** exit code of mailer. 652c579ef51Seric ** 653c579ef51Seric ** Side Effects: 654c579ef51Seric ** none. 655c579ef51Seric */ 656c579ef51Seric 65775889e88Seric endmailer(mci, name) 658b31e7f2bSeric register MCI *mci; 659c579ef51Seric char *name; 660c579ef51Seric { 661588cad61Seric int st; 662c579ef51Seric 66375889e88Seric /* close any connections */ 66475889e88Seric if (mci->mci_in != NULL) 66575889e88Seric (void) fclose(mci->mci_in); 66675889e88Seric if (mci->mci_out != NULL) 66775889e88Seric (void) fclose(mci->mci_out); 66875889e88Seric mci->mci_in = mci->mci_out = NULL; 66975889e88Seric mci->mci_state = MCIS_CLOSED; 67075889e88Seric 67133db8731Seric /* in the IPC case there is nothing to wait for */ 67275889e88Seric if (mci->mci_pid == 0) 67333db8731Seric return (EX_OK); 67433db8731Seric 67533db8731Seric /* wait for the mailer process to die and collect status */ 67675889e88Seric st = waitfor(mci->mci_pid); 677588cad61Seric if (st == -1) 67878de67c1Seric { 679588cad61Seric syserr("endmailer %s: wait", name); 680588cad61Seric return (EX_SOFTWARE); 681c579ef51Seric } 68233db8731Seric 68333db8731Seric /* see if it died a horrid death */ 684c579ef51Seric if ((st & 0377) != 0) 685c579ef51Seric { 6865f73204aSeric syserr("mailer %s died with signal %o", name, st); 6875f73204aSeric ExitStat = EX_TEMPFAIL; 6885f73204aSeric return (EX_TEMPFAIL); 689c579ef51Seric } 69033db8731Seric 69133db8731Seric /* normal death -- return status */ 692588cad61Seric st = (st >> 8) & 0377; 693588cad61Seric return (st); 694c579ef51Seric } 695c579ef51Seric /* 696c579ef51Seric ** OPENMAILER -- open connection to mailer. 697c579ef51Seric ** 698c579ef51Seric ** Parameters: 699c579ef51Seric ** m -- mailer descriptor. 700c579ef51Seric ** pvp -- parameter vector to pass to mailer. 701c579ef51Seric ** ctladdr -- controlling address for user. 702c579ef51Seric ** clever -- create a full duplex connection. 703c579ef51Seric ** 704c579ef51Seric ** Returns: 70575889e88Seric ** The mail connection info struct for this connection. 70675889e88Seric ** NULL on failure. 707c579ef51Seric ** 708c579ef51Seric ** Side Effects: 709c579ef51Seric ** creates a mailer in a subprocess. 710c579ef51Seric */ 711c579ef51Seric 712b31e7f2bSeric MCI * 713b31e7f2bSeric openmailer(m, pvp, ctladdr, clever, e) 714588cad61Seric MAILER *m; 715c579ef51Seric char **pvp; 716c579ef51Seric ADDRESS *ctladdr; 717c579ef51Seric bool clever; 718b31e7f2bSeric ENVELOPE *e; 719c579ef51Seric { 7205dfc646bSeric int pid; 721b31e7f2bSeric register MCI *mci; 722f8952a83Seric int mpvect[2]; 723c579ef51Seric int rpvect[2]; 7245dfc646bSeric extern FILE *fdopen(); 7255dfc646bSeric 7266ef48975Seric if (tTd(11, 1)) 7275dfc646bSeric { 7288c57e552Seric printf("openmailer:"); 7295dfc646bSeric printav(pvp); 7305dfc646bSeric } 73135490626Seric errno = 0; 7325dfc646bSeric 733ef66a9d0Seric CurHostName = m->m_mailer; 734ef66a9d0Seric 73533db8731Seric /* 73633db8731Seric ** Deal with the special case of mail handled through an IPC 73733db8731Seric ** connection. 73833db8731Seric ** In this case we don't actually fork. We must be 73933db8731Seric ** running SMTP for this to work. We will return a 74033db8731Seric ** zero pid to indicate that we are running IPC. 741e7c1bd78Seric ** We also handle a debug version that just talks to stdin/out. 74233db8731Seric */ 74333db8731Seric 744e7c1bd78Seric /* check for Local Person Communication -- not for mortals!!! */ 745e7c1bd78Seric if (strcmp(m->m_mailer, "[LPC]") == 0) 746e7c1bd78Seric { 747b31e7f2bSeric mci = (MCI *) xalloc(sizeof *mci); 7482a087e90Seric bzero((char *) mci, sizeof *mci); 74975889e88Seric mci->mci_in = stdin; 75075889e88Seric mci->mci_out = stdout; 751b31e7f2bSeric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 75215d084d5Seric mci->mci_mailer = m; 753e7c1bd78Seric } 754b31e7f2bSeric else if (strcmp(m->m_mailer, "[IPC]") == 0 || 755914346b1Seric strcmp(m->m_mailer, "[TCP]") == 0) 75633db8731Seric { 75784f7cd1bSeric #ifdef DAEMON 758e103b48fSeric register int i; 7591277f9a8Seric register u_short port; 760e103b48fSeric char *curhost; 761b31e7f2bSeric extern MCI *mci_get(); 762e103b48fSeric extern char *hostsignature(); 76333db8731Seric 764ef66a9d0Seric CurHostName = pvp[1]; 765e103b48fSeric curhost = hostsignature(m, pvp[1], e); 766b31e7f2bSeric 76733db8731Seric if (!clever) 76833db8731Seric syserr("non-clever IPC"); 76993b6e3cfSeric if (pvp[2] != NULL) 7701277f9a8Seric port = atoi(pvp[2]); 77193b6e3cfSeric else 7721277f9a8Seric port = 0; 773e103b48fSeric while (*curhost != '\0') 774ebc61751Sbloom { 775e103b48fSeric register char *p; 776e103b48fSeric char hostbuf[MAXNAME]; 777e103b48fSeric 778e103b48fSeric /* pull the next host from the signature */ 779e103b48fSeric p = strchr(curhost, ':'); 780e103b48fSeric if (p == NULL) 781e103b48fSeric p = &curhost[strlen(curhost)]; 782e103b48fSeric strncpy(hostbuf, curhost, p - curhost); 783e103b48fSeric hostbuf[p - curhost] = '\0'; 784e103b48fSeric if (*p != '\0') 785e103b48fSeric p++; 786e103b48fSeric curhost = p; 787e103b48fSeric 78845a8316eSeric /* see if we already know that this host is fried */ 789e103b48fSeric CurHostName = hostbuf; 790e103b48fSeric mci = mci_get(hostbuf, m); 791b31e7f2bSeric if (mci->mci_state != MCIS_CLOSED) 7921fa7c2ebSeric { 7931fa7c2ebSeric if (tTd(11, 1)) 7941fa7c2ebSeric { 7951fa7c2ebSeric printf("openmailer: "); 7961fa7c2ebSeric mci_dump(mci); 7971fa7c2ebSeric } 79867088a9dSeric CurHostName = mci->mci_host; 79975889e88Seric return mci; 8001fa7c2ebSeric } 80115d084d5Seric mci->mci_mailer = m; 802b31e7f2bSeric if (mci->mci_exitstat != EX_OK) 803b31e7f2bSeric continue; 804b31e7f2bSeric 80575889e88Seric /* try the connection */ 806e103b48fSeric setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 807914346b1Seric message(Arpa_Info, "Connecting to %s (%s)...", 808e103b48fSeric hostbuf, m->m_name); 809e103b48fSeric i = makeconnection(hostbuf, port, mci, 810914346b1Seric bitnset(M_SECURE_PORT, m->m_flags)); 81175889e88Seric mci->mci_exitstat = i; 81275889e88Seric mci->mci_errno = errno; 81375889e88Seric if (i == EX_OK) 814b31e7f2bSeric { 815b31e7f2bSeric mci->mci_state = MCIS_OPENING; 816b31e7f2bSeric mci_cache(mci); 817b31e7f2bSeric break; 818b31e7f2bSeric } 819b31e7f2bSeric else if (tTd(11, 1)) 820b31e7f2bSeric printf("openmailer: makeconnection => stat=%d, errno=%d\n", 821b31e7f2bSeric i, errno); 822b31e7f2bSeric 82375889e88Seric 8245f73204aSeric /* enter status of this host */ 82575889e88Seric setstat(i); 826ed854c7bSeric } 8272a087e90Seric mci->mci_pid = 0; 828b31e7f2bSeric #else /* no DAEMON */ 829588cad61Seric syserr("openmailer: no IPC"); 8301fa7c2ebSeric if (tTd(11, 1)) 8311fa7c2ebSeric printf("openmailer: NULL\n"); 83275889e88Seric return NULL; 833b31e7f2bSeric #endif /* DAEMON */ 834588cad61Seric } 835b31e7f2bSeric else 836b31e7f2bSeric { 8376328bdf7Seric /* create a pipe to shove the mail through */ 838f8952a83Seric if (pipe(mpvect) < 0) 83925a99e2eSeric { 840588cad61Seric syserr("openmailer: pipe (to mailer)"); 8411fa7c2ebSeric if (tTd(11, 1)) 8421fa7c2ebSeric printf("openmailer: NULL\n"); 84375889e88Seric return NULL; 84425a99e2eSeric } 845c579ef51Seric 846c579ef51Seric /* if this mailer speaks smtp, create a return pipe */ 847c579ef51Seric if (clever && pipe(rpvect) < 0) 848c579ef51Seric { 849588cad61Seric syserr("openmailer: pipe (from mailer)"); 850c579ef51Seric (void) close(mpvect[0]); 851c579ef51Seric (void) close(mpvect[1]); 8521fa7c2ebSeric if (tTd(11, 1)) 8531fa7c2ebSeric printf("openmailer: NULL\n"); 85475889e88Seric return NULL; 855c579ef51Seric } 856c579ef51Seric 85733db8731Seric /* 85833db8731Seric ** Actually fork the mailer process. 85933db8731Seric ** DOFORK is clever about retrying. 8606984bfddSeric ** 8616984bfddSeric ** Dispose of SIGCHLD signal catchers that may be laying 8626984bfddSeric ** around so that endmail will get it. 86333db8731Seric */ 86433db8731Seric 865b31e7f2bSeric if (e->e_xfp != NULL) 866b31e7f2bSeric (void) fflush(e->e_xfp); /* for debugging */ 867588cad61Seric (void) fflush(stdout); 8686984bfddSeric # ifdef SIGCHLD 8696984bfddSeric (void) signal(SIGCHLD, SIG_DFL); 8706c2c3107Seric # endif /* SIGCHLD */ 87184f7cd1bSeric DOFORK(FORK); 872f129ec7dSeric /* pid is set by DOFORK */ 87325a99e2eSeric if (pid < 0) 87425a99e2eSeric { 87533db8731Seric /* failure */ 876588cad61Seric syserr("openmailer: cannot fork"); 877f8952a83Seric (void) close(mpvect[0]); 878f8952a83Seric (void) close(mpvect[1]); 879c579ef51Seric if (clever) 880c579ef51Seric { 881c579ef51Seric (void) close(rpvect[0]); 882c579ef51Seric (void) close(rpvect[1]); 883c579ef51Seric } 8841fa7c2ebSeric if (tTd(11, 1)) 8851fa7c2ebSeric printf("openmailer: NULL\n"); 88675889e88Seric return NULL; 88725a99e2eSeric } 88825a99e2eSeric else if (pid == 0) 88925a99e2eSeric { 89013088b9fSeric int i; 891dafbc479Seric int saveerrno; 892d7a0480eSeric char *env[2]; 8935f73204aSeric extern int DtableSize; 89413088b9fSeric 89525a99e2eSeric /* child -- set up input & exec mailer */ 89603ab8e55Seric /* make diagnostic output be standard output */ 8978f0e7860Seric (void) signal(SIGINT, SIG_IGN); 8988f0e7860Seric (void) signal(SIGHUP, SIG_IGN); 8990984da9fSeric (void) signal(SIGTERM, SIG_DFL); 900f8952a83Seric 901*1caa9a59Seric /* close any other cached connections */ 902*1caa9a59Seric mci_flush(FALSE, mci); 903*1caa9a59Seric 904b31e7f2bSeric /* arrange to filter std & diag output of command */ 905c579ef51Seric if (clever) 906c579ef51Seric { 907c579ef51Seric (void) close(rpvect[0]); 908c579ef51Seric (void) close(1); 909c579ef51Seric (void) dup(rpvect[1]); 910c579ef51Seric (void) close(rpvect[1]); 911c579ef51Seric } 912276723a8Seric else if (OpMode == MD_SMTP || HoldErrs) 913f8952a83Seric { 914588cad61Seric /* put mailer output in transcript */ 915f8952a83Seric (void) close(1); 916b31e7f2bSeric (void) dup(fileno(e->e_xfp)); 917f8952a83Seric } 918db8841e9Seric (void) close(2); 919db8841e9Seric (void) dup(1); 920f8952a83Seric 921f8952a83Seric /* arrange to get standard input */ 922f8952a83Seric (void) close(mpvect[1]); 923db8841e9Seric (void) close(0); 924f8952a83Seric if (dup(mpvect[0]) < 0) 92525a99e2eSeric { 92625a99e2eSeric syserr("Cannot dup to zero!"); 927a590b978Seric _exit(EX_OSERR); 92825a99e2eSeric } 929f8952a83Seric (void) close(mpvect[0]); 93057fc6f17Seric if (!bitnset(M_RESTR, m->m_flags)) 9310984da9fSeric { 93253e3fa05Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 933e36b99e2Seric { 934e36b99e2Seric (void) setgid(DefGid); 935898a126bSbostic (void) initgroups(DefUser, DefGid); 936e36b99e2Seric (void) setuid(DefUid); 937e36b99e2Seric } 938e36b99e2Seric else 93969f29479Seric { 940e36b99e2Seric (void) setgid(ctladdr->q_gid); 941898a126bSbostic (void) initgroups(ctladdr->q_ruser? 942898a126bSbostic ctladdr->q_ruser: ctladdr->q_user, 943898a126bSbostic ctladdr->q_gid); 944e36b99e2Seric (void) setuid(ctladdr->q_uid); 94569f29479Seric } 9460984da9fSeric } 947588cad61Seric 94813088b9fSeric /* arrange for all the files to be closed */ 949b31e7f2bSeric for (i = 3; i < DtableSize; i++) 950b31e7f2bSeric { 951fccb3f7aSbostic register int j; 952fccb3f7aSbostic if ((j = fcntl(i, F_GETFD, 0)) != -1) 953fccb3f7aSbostic (void)fcntl(i, F_SETFD, j|1); 954fccb3f7aSbostic } 95533db8731Seric 95633db8731Seric /* try to execute the mailer */ 957d7a0480eSeric env[0] = "AGENT=sendmail"; 958d7a0480eSeric env[1] = NULL; 959d7a0480eSeric execve(m->m_mailer, pvp, env); 960dafbc479Seric saveerrno = errno; 96113088b9fSeric syserr("Cannot exec %s", m->m_mailer); 96295b76e4bSeric if (m == LocalMailer) 96355f33c03Seric _exit(EX_TEMPFAIL); 964dafbc479Seric switch (saveerrno) 96595b76e4bSeric { 96695b76e4bSeric case EIO: 96795b76e4bSeric case EAGAIN: 96895b76e4bSeric case ENOMEM: 96995b76e4bSeric # ifdef EPROCLIM 97095b76e4bSeric case EPROCLIM: 97195b76e4bSeric # endif 97295b76e4bSeric _exit(EX_TEMPFAIL); 97395b76e4bSeric } 974a590b978Seric _exit(EX_UNAVAILABLE); 97525a99e2eSeric } 97625a99e2eSeric 977f8952a83Seric /* 978c579ef51Seric ** Set up return value. 979f8952a83Seric */ 980f8952a83Seric 981b31e7f2bSeric mci = (MCI *) xalloc(sizeof *mci); 9822a087e90Seric bzero((char *) mci, sizeof *mci); 98315d084d5Seric mci->mci_mailer = m; 984b31e7f2bSeric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 9852a087e90Seric mci->mci_pid = pid; 986f8952a83Seric (void) close(mpvect[0]); 98775889e88Seric mci->mci_out = fdopen(mpvect[1], "w"); 988c579ef51Seric if (clever) 98925a99e2eSeric { 990c579ef51Seric (void) close(rpvect[1]); 99175889e88Seric mci->mci_in = fdopen(rpvect[0], "r"); 99275889e88Seric } 99375889e88Seric else 99475889e88Seric { 99575889e88Seric mci->mci_flags |= MCIF_TEMP; 99675889e88Seric mci->mci_in = NULL; 99775889e88Seric } 998b31e7f2bSeric } 999b31e7f2bSeric 1000b31e7f2bSeric /* 1001b31e7f2bSeric ** If we are in SMTP opening state, send initial protocol. 1002b31e7f2bSeric */ 1003b31e7f2bSeric 1004b31e7f2bSeric if (clever && mci->mci_state != MCIS_CLOSED) 1005b31e7f2bSeric { 1006b31e7f2bSeric smtpinit(m, mci, e); 1007b31e7f2bSeric } 10081fa7c2ebSeric if (tTd(11, 1)) 10091fa7c2ebSeric { 10101fa7c2ebSeric printf("openmailer: "); 10111fa7c2ebSeric mci_dump(mci); 10121fa7c2ebSeric } 1013c579ef51Seric 101475889e88Seric return mci; 101525a99e2eSeric } 101625a99e2eSeric /* 101725a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 101825a99e2eSeric ** 101925a99e2eSeric ** Parameters: 102025a99e2eSeric ** stat -- the status code from the mailer (high byte 102125a99e2eSeric ** only; core dumps must have been taken care of 102225a99e2eSeric ** already). 102325a99e2eSeric ** m -- the mailer descriptor for this mailer. 102425a99e2eSeric ** 102525a99e2eSeric ** Returns: 1026db8841e9Seric ** none. 102725a99e2eSeric ** 102825a99e2eSeric ** Side Effects: 1029c1f9df2cSeric ** Errors may be incremented. 103025a99e2eSeric ** ExitStat may be set. 103125a99e2eSeric */ 103225a99e2eSeric 1033198d9be0Seric giveresponse(stat, m, e) 103425a99e2eSeric int stat; 1035588cad61Seric register MAILER *m; 1036198d9be0Seric ENVELOPE *e; 103725a99e2eSeric { 103825a99e2eSeric register char *statmsg; 103925a99e2eSeric extern char *SysExMsg[]; 104025a99e2eSeric register int i; 1041d4bd8f0eSbostic extern int N_SysEx; 1042d4bd8f0eSbostic #ifdef NAMED_BIND 1043d4bd8f0eSbostic extern int h_errno; 1044d4bd8f0eSbostic #endif 1045198d9be0Seric char buf[MAXLINE]; 104625a99e2eSeric 10477d1fc79dSeric #ifdef lint 10487d1fc79dSeric if (m == NULL) 10497d1fc79dSeric return; 10507d1fc79dSeric #endif lint 10517d1fc79dSeric 105213bbc08cSeric /* 105313bbc08cSeric ** Compute status message from code. 105413bbc08cSeric */ 105513bbc08cSeric 105625a99e2eSeric i = stat - EX__BASE; 1057588cad61Seric if (stat == 0) 1058588cad61Seric statmsg = "250 Sent"; 1059588cad61Seric else if (i < 0 || i > N_SysEx) 1060588cad61Seric { 1061588cad61Seric (void) sprintf(buf, "554 unknown mailer error %d", stat); 1062588cad61Seric stat = EX_UNAVAILABLE; 1063588cad61Seric statmsg = buf; 1064588cad61Seric } 1065198d9be0Seric else if (stat == EX_TEMPFAIL) 1066198d9be0Seric { 10678557d168Seric (void) strcpy(buf, SysExMsg[i]); 1068d4bd8f0eSbostic #ifdef NAMED_BIND 1069f28da541Smiriam if (h_errno == TRY_AGAIN) 1070f28da541Smiriam { 1071f28da541Smiriam extern char *errstring(); 1072f28da541Smiriam 1073f28da541Smiriam statmsg = errstring(h_errno+MAX_ERRNO); 1074f28da541Smiriam } 1075f28da541Smiriam else 1076d4bd8f0eSbostic #endif 1077f28da541Smiriam { 10788557d168Seric if (errno != 0) 1079198d9be0Seric { 108087c9b3e7Seric extern char *errstring(); 10818557d168Seric 1082d87e85f3Seric statmsg = errstring(errno); 1083d87e85f3Seric } 1084d87e85f3Seric else 1085d87e85f3Seric { 1086d87e85f3Seric #ifdef SMTP 1087d87e85f3Seric extern char SmtpError[]; 1088d87e85f3Seric 1089d87e85f3Seric statmsg = SmtpError; 10906c2c3107Seric #else /* SMTP */ 1091d87e85f3Seric statmsg = NULL; 10926c2c3107Seric #endif /* SMTP */ 1093d87e85f3Seric } 1094f28da541Smiriam } 1095d87e85f3Seric if (statmsg != NULL && statmsg[0] != '\0') 1096d87e85f3Seric { 109787c9b3e7Seric (void) strcat(buf, ": "); 1098d87e85f3Seric (void) strcat(buf, statmsg); 10998557d168Seric } 1100198d9be0Seric statmsg = buf; 1101198d9be0Seric } 110225a99e2eSeric else 1103d87e85f3Seric { 110425a99e2eSeric statmsg = SysExMsg[i]; 1105d87e85f3Seric } 1106588cad61Seric 1107588cad61Seric /* 1108588cad61Seric ** Print the message as appropriate 1109588cad61Seric */ 1110588cad61Seric 1111198d9be0Seric if (stat == EX_OK || stat == EX_TEMPFAIL) 11125826d9d3Seric message(Arpa_Info, &statmsg[4]); 111325a99e2eSeric else 111425a99e2eSeric { 1115c1f9df2cSeric Errors++; 11165826d9d3Seric usrerr(statmsg); 111725a99e2eSeric } 111825a99e2eSeric 111925a99e2eSeric /* 112025a99e2eSeric ** Final cleanup. 112125a99e2eSeric ** Log a record of the transaction. Compute the new 112225a99e2eSeric ** ExitStat -- if we already had an error, stick with 112325a99e2eSeric ** that. 112425a99e2eSeric */ 112525a99e2eSeric 11262f624c86Seric if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 1127b31e7f2bSeric logdelivery(&statmsg[4], e); 1128eb238f8cSeric 1129eb238f8cSeric if (stat != EX_TEMPFAIL) 1130eb238f8cSeric setstat(stat); 1131198d9be0Seric if (stat != EX_OK) 1132198d9be0Seric { 1133198d9be0Seric if (e->e_message != NULL) 1134198d9be0Seric free(e->e_message); 1135198d9be0Seric e->e_message = newstr(&statmsg[4]); 1136198d9be0Seric } 11378557d168Seric errno = 0; 1138d4bd8f0eSbostic #ifdef NAMED_BIND 1139f28da541Smiriam h_errno = 0; 1140d4bd8f0eSbostic #endif 1141eb238f8cSeric } 1142eb238f8cSeric /* 1143eb238f8cSeric ** LOGDELIVERY -- log the delivery in the system log 1144eb238f8cSeric ** 1145eb238f8cSeric ** Parameters: 1146eb238f8cSeric ** stat -- the message to print for the status 1147eb238f8cSeric ** 1148eb238f8cSeric ** Returns: 1149eb238f8cSeric ** none 1150eb238f8cSeric ** 1151eb238f8cSeric ** Side Effects: 1152eb238f8cSeric ** none 1153eb238f8cSeric */ 1154eb238f8cSeric 1155b31e7f2bSeric logdelivery(stat, e) 1156eb238f8cSeric char *stat; 1157b31e7f2bSeric register ENVELOPE *e; 11585cf56be3Seric { 11595cf56be3Seric extern char *pintvl(); 11605cf56be3Seric 1161eb238f8cSeric # ifdef LOG 1162b31e7f2bSeric syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", e->e_id, 1163b31e7f2bSeric e->e_to, pintvl(curtime() - e->e_ctime, TRUE), stat); 11646c2c3107Seric # endif /* LOG */ 116525a99e2eSeric } 116625a99e2eSeric /* 116751552439Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 116825a99e2eSeric ** 116951552439Seric ** This can be made an arbitrary message separator by changing $l 117051552439Seric ** 11719b6c17a6Seric ** One of the ugliest hacks seen by human eyes is contained herein: 11729b6c17a6Seric ** UUCP wants those stupid "remote from <host>" lines. Why oh why 11739b6c17a6Seric ** does a well-meaning programmer such as myself have to deal with 11749b6c17a6Seric ** this kind of antique garbage???? 117525a99e2eSeric ** 117625a99e2eSeric ** Parameters: 117751552439Seric ** fp -- the file to output to. 117851552439Seric ** m -- the mailer describing this entry. 117925a99e2eSeric ** 118025a99e2eSeric ** Returns: 118151552439Seric ** none 118225a99e2eSeric ** 118325a99e2eSeric ** Side Effects: 118451552439Seric ** outputs some text to fp. 118525a99e2eSeric */ 118625a99e2eSeric 1187b31e7f2bSeric putfromline(fp, m, e) 118851552439Seric register FILE *fp; 118951552439Seric register MAILER *m; 1190b31e7f2bSeric ENVELOPE *e; 119125a99e2eSeric { 11922bc47524Seric char *template = "\201l\n"; 119351552439Seric char buf[MAXLINE]; 119425a99e2eSeric 119557fc6f17Seric if (bitnset(M_NHDR, m->m_flags)) 119651552439Seric return; 119713bbc08cSeric 11982c7e1b8dSeric # ifdef UGLYUUCP 119957fc6f17Seric if (bitnset(M_UGLYUUCP, m->m_flags)) 120074b6e67bSeric { 1201ea09d6edSeric char *bang; 1202ea09d6edSeric char xbuf[MAXLINE]; 120374b6e67bSeric 12042bc47524Seric expand("\201<", buf, &buf[sizeof buf - 1], e); 12056c2c3107Seric bang = strchr(buf, '!'); 120674b6e67bSeric if (bang == NULL) 1207ea09d6edSeric syserr("No ! in UUCP! (%s)", buf); 120874b6e67bSeric else 1209588cad61Seric { 1210ea09d6edSeric *bang++ = '\0'; 12112bc47524Seric (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 1212ea09d6edSeric template = xbuf; 121374b6e67bSeric } 1214588cad61Seric } 12156c2c3107Seric # endif /* UGLYUUCP */ 1216b31e7f2bSeric expand(template, buf, &buf[sizeof buf - 1], e); 121777b52738Seric putline(buf, fp, m); 1218bc6e2962Seric } 1219bc6e2962Seric /* 122051552439Seric ** PUTBODY -- put the body of a message. 122151552439Seric ** 122251552439Seric ** Parameters: 122351552439Seric ** fp -- file to output onto. 122477b52738Seric ** m -- a mailer descriptor to control output format. 12259a6a5f55Seric ** e -- the envelope to put out. 122651552439Seric ** 122751552439Seric ** Returns: 122851552439Seric ** none. 122951552439Seric ** 123051552439Seric ** Side Effects: 123151552439Seric ** The message is written onto fp. 123251552439Seric */ 123351552439Seric 123477b52738Seric putbody(fp, m, e) 123551552439Seric FILE *fp; 1236588cad61Seric MAILER *m; 12379a6a5f55Seric register ENVELOPE *e; 123851552439Seric { 123977b52738Seric char buf[MAXLINE]; 124051552439Seric 124151552439Seric /* 124251552439Seric ** Output the body of the message 124351552439Seric */ 124451552439Seric 12459a6a5f55Seric if (e->e_dfp == NULL) 124651552439Seric { 12479a6a5f55Seric if (e->e_df != NULL) 12489a6a5f55Seric { 12499a6a5f55Seric e->e_dfp = fopen(e->e_df, "r"); 12509a6a5f55Seric if (e->e_dfp == NULL) 12518f9146b0Srick syserr("putbody: Cannot open %s for %s from %s", 12528f9146b0Srick e->e_df, e->e_to, e->e_from); 12539a6a5f55Seric } 12549a6a5f55Seric else 125577b52738Seric putline("<<< No Message Collected >>>", fp, m); 12569a6a5f55Seric } 12579a6a5f55Seric if (e->e_dfp != NULL) 12589a6a5f55Seric { 12599a6a5f55Seric rewind(e->e_dfp); 126077b52738Seric while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 126124fc8aeeSeric { 126224fc8aeeSeric if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1263d6fa2b58Sbostic strncmp(buf, "From ", 5) == 0) 12643462ad9eSeric (void) putc('>', fp); 126577b52738Seric putline(buf, fp, m); 126624fc8aeeSeric } 126751552439Seric 12689a6a5f55Seric if (ferror(e->e_dfp)) 126951552439Seric { 127051552439Seric syserr("putbody: read error"); 127151552439Seric ExitStat = EX_IOERR; 127251552439Seric } 127351552439Seric } 127451552439Seric 127551552439Seric (void) fflush(fp); 127651552439Seric if (ferror(fp) && errno != EPIPE) 127751552439Seric { 127851552439Seric syserr("putbody: write error"); 127951552439Seric ExitStat = EX_IOERR; 128051552439Seric } 128151552439Seric errno = 0; 128225a99e2eSeric } 128325a99e2eSeric /* 128425a99e2eSeric ** MAILFILE -- Send a message to a file. 128525a99e2eSeric ** 1286f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 1287f129ec7dSeric ** bits, sendmail will try to become the owner of that file 1288f129ec7dSeric ** rather than the real user. Obviously, this only works if 1289f129ec7dSeric ** sendmail runs as root. 1290f129ec7dSeric ** 1291588cad61Seric ** This could be done as a subordinate mailer, except that it 1292588cad61Seric ** is used implicitly to save messages in ~/dead.letter. We 1293588cad61Seric ** view this as being sufficiently important as to include it 1294588cad61Seric ** here. For example, if the system is dying, we shouldn't have 1295588cad61Seric ** to create another process plus some pipes to save the message. 1296588cad61Seric ** 129725a99e2eSeric ** Parameters: 129825a99e2eSeric ** filename -- the name of the file to send to. 12996259796dSeric ** ctladdr -- the controlling address header -- includes 13006259796dSeric ** the userid/groupid to be when sending. 130125a99e2eSeric ** 130225a99e2eSeric ** Returns: 130325a99e2eSeric ** The exit code associated with the operation. 130425a99e2eSeric ** 130525a99e2eSeric ** Side Effects: 130625a99e2eSeric ** none. 130725a99e2eSeric */ 130825a99e2eSeric 1309b31e7f2bSeric mailfile(filename, ctladdr, e) 131025a99e2eSeric char *filename; 13116259796dSeric ADDRESS *ctladdr; 1312b31e7f2bSeric register ENVELOPE *e; 131325a99e2eSeric { 131425a99e2eSeric register FILE *f; 131532d19d43Seric register int pid; 131615d084d5Seric int mode; 131725a99e2eSeric 131832d19d43Seric /* 131932d19d43Seric ** Fork so we can change permissions here. 132032d19d43Seric ** Note that we MUST use fork, not vfork, because of 132132d19d43Seric ** the complications of calling subroutines, etc. 132232d19d43Seric */ 132332d19d43Seric 132432d19d43Seric DOFORK(fork); 132532d19d43Seric 132632d19d43Seric if (pid < 0) 132732d19d43Seric return (EX_OSERR); 132832d19d43Seric else if (pid == 0) 132932d19d43Seric { 133032d19d43Seric /* child -- actually write to file */ 1331f129ec7dSeric struct stat stb; 1332f129ec7dSeric 13330984da9fSeric (void) signal(SIGINT, SIG_DFL); 13340984da9fSeric (void) signal(SIGHUP, SIG_DFL); 13350984da9fSeric (void) signal(SIGTERM, SIG_DFL); 13363462ad9eSeric (void) umask(OldUmask); 133795f16dc0Seric 1338f129ec7dSeric if (stat(filename, &stb) < 0) 1339e6e1265fSeric stb.st_mode = 0666; 134015d084d5Seric mode = stb.st_mode; 134195f16dc0Seric 134295f16dc0Seric /* limit the errors to those actually caused in the child */ 134395f16dc0Seric errno = 0; 134495f16dc0Seric ExitStat = EX_OK; 134595f16dc0Seric 1346f129ec7dSeric if (bitset(0111, stb.st_mode)) 1347f129ec7dSeric exit(EX_CANTCREAT); 134803827b5fSeric if (ctladdr == NULL) 13498f9146b0Srick ctladdr = &e->e_from; 135015d084d5Seric else 135115d084d5Seric { 135215d084d5Seric /* ignore setuid and setgid bits */ 135315d084d5Seric mode &= ~(S_ISGID|S_ISUID); 135415d084d5Seric } 135515d084d5Seric 13568f9146b0Srick /* we have to open the dfile BEFORE setuid */ 13578f9146b0Srick if (e->e_dfp == NULL && e->e_df != NULL) 13588f9146b0Srick { 13598f9146b0Srick e->e_dfp = fopen(e->e_df, "r"); 136095f16dc0Seric if (e->e_dfp == NULL) 136195f16dc0Seric { 13628f9146b0Srick syserr("mailfile: Cannot open %s for %s from %s", 13638f9146b0Srick e->e_df, e->e_to, e->e_from); 13648f9146b0Srick } 13658f9146b0Srick } 13668f9146b0Srick 136715d084d5Seric if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 1368e36b99e2Seric { 136995f16dc0Seric if (ctladdr->q_uid == 0) 137095f16dc0Seric { 1371e36b99e2Seric (void) setgid(DefGid); 1372898a126bSbostic (void) initgroups(DefUser, DefGid); 137395f16dc0Seric } 137495f16dc0Seric else 137595f16dc0Seric { 13766259796dSeric (void) setgid(ctladdr->q_gid); 1377898a126bSbostic (void) initgroups(ctladdr->q_ruser ? 1378898a126bSbostic ctladdr->q_ruser : ctladdr->q_user, 1379898a126bSbostic ctladdr->q_gid); 1380898a126bSbostic } 1381e36b99e2Seric } 138215d084d5Seric if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 1383e36b99e2Seric { 1384e36b99e2Seric if (ctladdr->q_uid == 0) 1385e36b99e2Seric (void) setuid(DefUid); 1386e36b99e2Seric else 13876259796dSeric (void) setuid(ctladdr->q_uid); 1388e36b99e2Seric } 138995f16dc0Seric FileName = filename; 139095f16dc0Seric LineNumber = 0; 139127628d59Seric f = dfopen(filename, "a"); 139225a99e2eSeric if (f == NULL) 139395f16dc0Seric { 139495f16dc0Seric message("cannot open"); 139532d19d43Seric exit(EX_CANTCREAT); 139695f16dc0Seric } 139725a99e2eSeric 1398b31e7f2bSeric putfromline(f, ProgMailer, e); 1399b843d759Seric (*e->e_puthdr)(f, ProgMailer, e); 140077b52738Seric putline("\n", f, ProgMailer); 1401b843d759Seric (*e->e_putbody)(f, ProgMailer, e); 140277b52738Seric putline("\n", f, ProgMailer); 140395f16dc0Seric if (ferror(f)) 140495f16dc0Seric { 140595f16dc0Seric message("I/O error"); 140695f16dc0Seric setstat(EX_IOERR); 140795f16dc0Seric } 1408db8841e9Seric (void) fclose(f); 140932d19d43Seric (void) fflush(stdout); 1410e36b99e2Seric 141127628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 1412c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 141395f16dc0Seric exit(ExitStat); 141413bbc08cSeric /*NOTREACHED*/ 141532d19d43Seric } 141632d19d43Seric else 141732d19d43Seric { 141832d19d43Seric /* parent -- wait for exit status */ 1419588cad61Seric int st; 142032d19d43Seric 1421588cad61Seric st = waitfor(pid); 1422588cad61Seric if ((st & 0377) != 0) 1423588cad61Seric return (EX_UNAVAILABLE); 1424588cad61Seric else 1425588cad61Seric return ((st >> 8) & 0377); 14268f9146b0Srick /*NOTREACHED*/ 142732d19d43Seric } 142825a99e2eSeric } 1429ea4dc939Seric /* 1430ea4dc939Seric ** SENDALL -- actually send all the messages. 1431ea4dc939Seric ** 1432ea4dc939Seric ** Parameters: 14330c52a0b3Seric ** e -- the envelope to send. 14347b95031aSeric ** mode -- the delivery mode to use. If SM_DEFAULT, use 14357b95031aSeric ** the current SendMode. 1436ea4dc939Seric ** 1437ea4dc939Seric ** Returns: 1438ea4dc939Seric ** none. 1439ea4dc939Seric ** 1440ea4dc939Seric ** Side Effects: 1441ea4dc939Seric ** Scans the send lists and sends everything it finds. 14420c52a0b3Seric ** Delivers any appropriate error messages. 1443276723a8Seric ** If we are running in a non-interactive mode, takes the 1444276723a8Seric ** appropriate action. 1445ea4dc939Seric */ 1446ea4dc939Seric 1447276723a8Seric sendall(e, mode) 14480c52a0b3Seric ENVELOPE *e; 1449276723a8Seric char mode; 1450ea4dc939Seric { 1451e77e673fSeric register ADDRESS *q; 145214a8ed7aSeric bool oldverbose; 1453276723a8Seric int pid; 14541c265a00Seric # ifdef LOCKF 14551c265a00Seric struct flock lfd; 14561c265a00Seric # endif 1457ea4dc939Seric 14587b95031aSeric /* determine actual delivery mode */ 14597b95031aSeric if (mode == SM_DEFAULT) 14607b95031aSeric { 14615f73204aSeric extern bool shouldqueue(); 14627b95031aSeric 146355e150dbSeric if (shouldqueue(e->e_msgpriority, e->e_ctime)) 14647b95031aSeric mode = SM_QUEUE; 14657b95031aSeric else 14667b95031aSeric mode = SendMode; 14677b95031aSeric } 14687b95031aSeric 1469df864a8fSeric if (tTd(13, 1)) 1470772e6e50Seric { 1471276723a8Seric printf("\nSENDALL: mode %c, sendqueue:\n", mode); 14720c52a0b3Seric printaddr(e->e_sendqueue, TRUE); 1473772e6e50Seric } 1474ea4dc939Seric 14750c52a0b3Seric /* 1476276723a8Seric ** Do any preprocessing necessary for the mode we are running. 1477588cad61Seric ** Check to make sure the hop count is reasonable. 1478588cad61Seric ** Delete sends to the sender in mailing lists. 1479276723a8Seric */ 1480276723a8Seric 1481588cad61Seric CurEnv = e; 1482276723a8Seric 14831d57450bSeric if (e->e_hopcount > MaxHopCount) 1484276723a8Seric { 14858f9146b0Srick errno = 0; 14868f9146b0Srick syserr("sendall: too many hops %d (%d max): from %s, to %s", 1487655518ecSeric e->e_hopcount, MaxHopCount, e->e_from.q_paddr, 1488655518ecSeric e->e_sendqueue->q_paddr); 1489588cad61Seric return; 1490588cad61Seric } 1491588cad61Seric 1492588cad61Seric if (!MeToo) 1493276723a8Seric { 1494f3d6c55cSeric extern ADDRESS *recipient(); 1495f3d6c55cSeric 1496588cad61Seric e->e_from.q_flags |= QDONTSEND; 149775f1ade9Seric if (tTd(13, 5)) 149875f1ade9Seric { 149975f1ade9Seric printf("sendall: QDONTSEND "); 150075f1ade9Seric printaddr(&e->e_from, FALSE); 150175f1ade9Seric } 1502b843d759Seric (void) recipient(&e->e_from, &e->e_sendqueue, e); 1503276723a8Seric } 1504588cad61Seric 1505588cad61Seric # ifdef QUEUE 1506b254bcb6Seric if ((mode == SM_QUEUE || mode == SM_FORK || 1507b254bcb6Seric (mode != SM_VERIFY && SuperSafe)) && 1508b254bcb6Seric !bitset(EF_INQUEUE, e->e_flags)) 1509e020b127Seric queueup(e, TRUE, mode == SM_QUEUE); 15106c2c3107Seric #endif /* QUEUE */ 1511276723a8Seric 1512276723a8Seric oldverbose = Verbose; 1513276723a8Seric switch (mode) 1514276723a8Seric { 1515276723a8Seric case SM_VERIFY: 1516276723a8Seric Verbose = TRUE; 1517276723a8Seric break; 1518276723a8Seric 1519276723a8Seric case SM_QUEUE: 1520c7f5410dSeric queueonly: 1521b254bcb6Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 1522276723a8Seric return; 1523276723a8Seric 1524276723a8Seric case SM_FORK: 15259a6a5f55Seric if (e->e_xfp != NULL) 15269a6a5f55Seric (void) fflush(e->e_xfp); 1527c7f5410dSeric 1528c7f5410dSeric # ifdef LOCKF 1529c7f5410dSeric /* 1530c7f5410dSeric ** Since lockf has the interesting semantic that the 15311c265a00Seric ** lock is lost when we fork, we have to risk losing 15321c265a00Seric ** the lock here by closing before the fork, and then 15331c265a00Seric ** trying to get it back in the child. 1534c7f5410dSeric */ 1535c7f5410dSeric 1536e020b127Seric if (e->e_lockfp != NULL) 1537c7f5410dSeric { 1538e020b127Seric (void) fclose(e->e_lockfp); 1539e020b127Seric e->e_lockfp = NULL; 1540c7f5410dSeric } 1541c7f5410dSeric # endif /* LOCKF */ 1542c7f5410dSeric 1543276723a8Seric pid = fork(); 1544276723a8Seric if (pid < 0) 1545276723a8Seric { 1546c7f5410dSeric goto queueonly; 1547276723a8Seric } 1548276723a8Seric else if (pid > 0) 1549a6fce3d8Seric { 1550a6fce3d8Seric /* be sure we leave the temp files to our child */ 1551b254bcb6Seric e->e_id = e->e_df = NULL; 1552c7f5410dSeric # ifndef LOCKF 1553e020b127Seric if (e->e_lockfp != NULL) 155475f1ade9Seric { 1555e020b127Seric (void) fclose(e->e_lockfp); 155675f1ade9Seric e->e_lockfp = NULL; 155775f1ade9Seric } 1558c7f5410dSeric # endif 155975f1ade9Seric 156075f1ade9Seric /* close any random open files in the envelope */ 156175f1ade9Seric if (e->e_dfp != NULL) 156275f1ade9Seric { 156375f1ade9Seric (void) fclose(e->e_dfp); 156475f1ade9Seric e->e_dfp = NULL; 156575f1ade9Seric } 156675f1ade9Seric if (e->e_xfp != NULL) 156775f1ade9Seric { 156875f1ade9Seric (void) fclose(e->e_xfp); 156975f1ade9Seric e->e_xfp = NULL; 157075f1ade9Seric } 1571276723a8Seric return; 1572a6fce3d8Seric } 1573276723a8Seric 1574276723a8Seric /* double fork to avoid zombies */ 1575276723a8Seric if (fork() > 0) 1576276723a8Seric exit(EX_OK); 1577276723a8Seric 1578a6fce3d8Seric /* be sure we are immune from the terminal */ 1579769e215aSeric disconnect(FALSE); 1580a6fce3d8Seric 1581e6720d51Seric # ifdef LOCKF 1582e6720d51Seric /* 1583c7f5410dSeric ** Now try to get our lock back. 1584e6720d51Seric */ 1585e6720d51Seric 15861c265a00Seric lfd.l_type = F_WRLCK; 15871c265a00Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 1588e020b127Seric e->e_lockfp = fopen(queuename(e, 'q'), "r+"); 1589e020b127Seric if (e->e_lockfp == NULL || 15901c265a00Seric fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0) 1591e6720d51Seric { 1592e6720d51Seric /* oops.... lost it */ 1593e6720d51Seric # ifdef LOG 15942f624c86Seric if (LogLevel > 29) 1595c7f5410dSeric syslog(LOG_NOTICE, "%s: lost lock: %m", 1596b31e7f2bSeric e->e_id); 1597e6720d51Seric # endif /* LOG */ 1598e6720d51Seric exit(EX_OK); 1599e6720d51Seric } 1600e6720d51Seric # endif /* LOCKF */ 1601e6720d51Seric 1602*1caa9a59Seric /* 1603*1caa9a59Seric ** Close any cached connections. 1604*1caa9a59Seric ** 1605*1caa9a59Seric ** We don't send the QUIT protocol because the parent 1606*1caa9a59Seric ** still knows about the connection. 1607*1caa9a59Seric ** 1608*1caa9a59Seric ** This should only happen when delivering an error 1609*1caa9a59Seric ** message. 1610*1caa9a59Seric */ 1611*1caa9a59Seric 1612*1caa9a59Seric mci_flush(FALSE, NULL); 1613*1caa9a59Seric 1614276723a8Seric break; 1615276723a8Seric } 1616276723a8Seric 1617276723a8Seric /* 16180c52a0b3Seric ** Run through the list and send everything. 16190c52a0b3Seric */ 16200c52a0b3Seric 1621655518ecSeric e->e_nsent = 0; 16220c52a0b3Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1623ea4dc939Seric { 1624276723a8Seric if (mode == SM_VERIFY) 1625ea4dc939Seric { 1626a6fce3d8Seric e->e_to = q->q_paddr; 1627e77e673fSeric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1628ea4dc939Seric message(Arpa_Info, "deliverable"); 1629ea4dc939Seric } 1630dde5acadSeric else if (!bitset(QDONTSEND, q->q_flags)) 1631dde5acadSeric { 1632de1179f5Seric # ifdef QUEUE 1633dde5acadSeric /* 1634dde5acadSeric ** Checkpoint the send list every few addresses 1635dde5acadSeric */ 1636dde5acadSeric 1637655518ecSeric if (e->e_nsent >= CheckpointInterval) 1638dde5acadSeric { 1639e020b127Seric queueup(e, TRUE, FALSE); 1640655518ecSeric e->e_nsent = 0; 1641dde5acadSeric } 1642de1179f5Seric # endif /* QUEUE */ 1643655518ecSeric (void) deliver(e, q); 1644dde5acadSeric } 1645ea4dc939Seric } 164614a8ed7aSeric Verbose = oldverbose; 16470c52a0b3Seric 16480c52a0b3Seric /* 16490c52a0b3Seric ** Now run through and check for errors. 16500c52a0b3Seric */ 16510c52a0b3Seric 1652e020b127Seric if (mode == SM_VERIFY) 16530c52a0b3Seric return; 16540c52a0b3Seric 16550c52a0b3Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 16560c52a0b3Seric { 16570c52a0b3Seric register ADDRESS *qq; 16580c52a0b3Seric 1659df864a8fSeric if (tTd(13, 3)) 1660df864a8fSeric { 1661df864a8fSeric printf("Checking "); 1662df864a8fSeric printaddr(q, FALSE); 1663df864a8fSeric } 1664df864a8fSeric 1665b254bcb6Seric /* only send errors if the message failed */ 1666b254bcb6Seric if (!bitset(QBADADDR, q->q_flags)) 1667b254bcb6Seric continue; 16680c52a0b3Seric 16690c52a0b3Seric /* we have an address that failed -- find the parent */ 16700c52a0b3Seric for (qq = q; qq != NULL; qq = qq->q_alias) 16710c52a0b3Seric { 16720c52a0b3Seric char obuf[MAXNAME + 6]; 16730c52a0b3Seric extern char *aliaslookup(); 16740c52a0b3Seric 16750c52a0b3Seric /* we can only have owners for local addresses */ 167657fc6f17Seric if (!bitnset(M_LOCAL, qq->q_mailer->m_flags)) 16770c52a0b3Seric continue; 16780c52a0b3Seric 16790c52a0b3Seric /* see if the owner list exists */ 16800c52a0b3Seric (void) strcpy(obuf, "owner-"); 1681cec031e3Seric if (strncmp(qq->q_user, "owner-", 6) == 0) 1682cec031e3Seric (void) strcat(obuf, "owner"); 1683cec031e3Seric else 16840c52a0b3Seric (void) strcat(obuf, qq->q_user); 168555e150dbSeric if (!bitnset(M_USR_UPPER, qq->q_mailer->m_flags)) 1686ef137175Sbostic makelower(obuf); 16870c52a0b3Seric if (aliaslookup(obuf) == NULL) 16880c52a0b3Seric continue; 16890c52a0b3Seric 1690df864a8fSeric if (tTd(13, 4)) 1691df864a8fSeric printf("Errors to %s\n", obuf); 1692df864a8fSeric 16930c52a0b3Seric /* owner list exists -- add it to the error queue */ 1694*1caa9a59Seric (void) sendtolist(obuf, (ADDRESS *) NULL, 1695*1caa9a59Seric &e->e_errorqueue, e); 169655e150dbSeric 169755e150dbSeric /* and set the return path to point to it */ 169855e150dbSeric e->e_returnpath = newstr(obuf); 169955e150dbSeric 170053e3fa05Seric ErrorMode = EM_MAIL; 17010c52a0b3Seric break; 17020c52a0b3Seric } 17030c52a0b3Seric 17040c52a0b3Seric /* if we did not find an owner, send to the sender */ 17057455aa0bSeric if (qq == NULL && bitset(QBADADDR, q->q_flags)) 1706*1caa9a59Seric (void) sendtolist(e->e_from.q_paddr, qq, 1707*1caa9a59Seric &e->e_errorqueue, e); 17080c52a0b3Seric } 1709276723a8Seric 1710276723a8Seric if (mode == SM_FORK) 1711276723a8Seric finis(); 17120c52a0b3Seric } 1713e103b48fSeric /* 1714e103b48fSeric ** HOSTSIGNATURE -- return the "signature" for a host. 1715e103b48fSeric ** 1716e103b48fSeric ** The signature describes how we are going to send this -- it 1717e103b48fSeric ** can be just the hostname (for non-Internet hosts) or can be 1718e103b48fSeric ** an ordered list of MX hosts. 1719e103b48fSeric ** 1720e103b48fSeric ** Parameters: 1721e103b48fSeric ** m -- the mailer describing this host. 1722e103b48fSeric ** host -- the host name. 1723e103b48fSeric ** e -- the current envelope. 1724e103b48fSeric ** 1725e103b48fSeric ** Returns: 1726e103b48fSeric ** The signature for this host. 1727e103b48fSeric ** 1728e103b48fSeric ** Side Effects: 1729e103b48fSeric ** Can tweak the symbol table. 1730e103b48fSeric */ 1731e103b48fSeric 1732e103b48fSeric char * 1733e103b48fSeric hostsignature(m, host, e) 1734e103b48fSeric register MAILER *m; 1735e103b48fSeric char *host; 1736e103b48fSeric ENVELOPE *e; 1737e103b48fSeric { 1738e103b48fSeric register char *p; 1739e103b48fSeric register STAB *s; 1740e103b48fSeric int i; 1741e103b48fSeric int len; 1742e103b48fSeric #ifdef NAMED_BIND 1743e103b48fSeric int nmx; 1744e103b48fSeric auto int rcode; 1745e103b48fSeric char *mxhosts[MAXMXHOSTS + 1]; 1746e103b48fSeric static char myhostbuf[MAXNAME]; 1747e103b48fSeric #endif 1748e103b48fSeric 1749e103b48fSeric /* 1750e103b48fSeric ** Check to see if this uses IPC -- if not, it can't have MX records. 1751e103b48fSeric */ 1752e103b48fSeric 1753e103b48fSeric p = m->m_mailer; 1754e103b48fSeric if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 1755e103b48fSeric { 1756e103b48fSeric /* just an ordinary mailer */ 1757e103b48fSeric return host; 1758e103b48fSeric } 1759e103b48fSeric 1760e103b48fSeric /* 1761e103b48fSeric ** If it is a numeric address, just return it. 1762e103b48fSeric */ 1763e103b48fSeric 1764e103b48fSeric if (host[0] == '[') 1765e103b48fSeric return host; 1766e103b48fSeric 1767e103b48fSeric /* 1768e103b48fSeric ** Look it up in the symbol table. 1769e103b48fSeric */ 1770e103b48fSeric 1771e103b48fSeric s = stab(host, ST_HOSTSIG, ST_ENTER); 1772e103b48fSeric if (s->s_hostsig != NULL) 1773e103b48fSeric return s->s_hostsig; 1774e103b48fSeric 1775e103b48fSeric /* 1776e103b48fSeric ** Not already there -- create a signature. 1777e103b48fSeric */ 1778e103b48fSeric 1779e103b48fSeric #ifdef NAMED_BIND 1780e103b48fSeric if (myhostbuf[0] == '\0') 17812bc47524Seric expand("\201j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e); 1782e103b48fSeric 1783e103b48fSeric nmx = getmxrr(host, mxhosts, myhostbuf, &rcode); 1784e103b48fSeric if (nmx <= 0) 1785e103b48fSeric { 1786e103b48fSeric register MCI *mci; 1787e103b48fSeric extern int errno; 1788e103b48fSeric extern MCI *mci_get(); 1789e103b48fSeric 1790e103b48fSeric /* update the connection info for this host */ 1791e103b48fSeric mci = mci_get(host, m); 1792e103b48fSeric mci->mci_exitstat = rcode; 1793e103b48fSeric mci->mci_errno = errno; 1794e103b48fSeric 1795e103b48fSeric /* and return the original host name as the signature */ 1796e103b48fSeric s->s_hostsig = host; 1797e103b48fSeric return host; 1798e103b48fSeric } 1799e103b48fSeric 1800e103b48fSeric len = 0; 1801e103b48fSeric for (i = 0; i < nmx; i++) 1802e103b48fSeric { 1803e103b48fSeric len += strlen(mxhosts[i]) + 1; 1804e103b48fSeric } 1805e103b48fSeric s->s_hostsig = p = xalloc(len); 1806e103b48fSeric for (i = 0; i < nmx; i++) 1807e103b48fSeric { 1808e103b48fSeric if (i != 0) 1809e103b48fSeric *p++ = ':'; 1810e103b48fSeric strcpy(p, mxhosts[i]); 1811e103b48fSeric p += strlen(p); 1812e103b48fSeric } 1813e103b48fSeric makelower(s->s_hostsig); 1814e103b48fSeric #else 1815e103b48fSeric /* not using BIND -- the signature is just the host name */ 1816e103b48fSeric s->s_hostsig = host; 1817e103b48fSeric #endif 1818e103b48fSeric if (tTd(17, 1)) 1819e103b48fSeric printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 1820e103b48fSeric return s->s_hostsig; 1821e103b48fSeric } 1822