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*81161401Seric static char sccsid[] = "@(#)deliver.c 6.33 (Berkeley) 03/01/93"; 11e70a7521Sbostic #endif /* not lint */ 124227346bSdist 13fcde9cc8Sbostic #include "sendmail.h" 14a8c080f0Seric #include <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; 76c6e18ac5Seric if (bitset(QDONTSEND|QQUEUEUP, 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 { 109c6e18ac5Seric if (bitset(QDONTSEND|QQUEUEUP, to->q_flags) || 110c6e18ac5Seric to->q_mailer != m) 111f4560e80Seric continue; 112f3dbc832Seric to->q_flags |= QQUEUEUP|QDONTSEND; 113588cad61Seric e->e_to = to->q_paddr; 11408b25121Seric message("queued"); 1152f624c86Seric if (LogLevel > 8) 116*81161401Seric logdelivery(m, NULL, "queued", e); 117f4560e80Seric } 118588cad61Seric e->e_to = NULL; 119f3dbc832Seric return (0); 120f3dbc832Seric } 121f3dbc832Seric 12225a99e2eSeric /* 1235dfc646bSeric ** Do initial argv setup. 1245dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 1255dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 1265dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 1275dfc646bSeric ** code does not check for 'pv' overflow; this places a 1285dfc646bSeric ** manifest lower limit of 4 for MAXPV. 1293bea8136Seric ** The from address rewrite is expected to make 1303bea8136Seric ** the address relative to the other end. 1315dfc646bSeric */ 1325dfc646bSeric 13378442df3Seric /* rewrite from address, using rewriting rules */ 1342f624c86Seric (void) strcpy(rpathbuf, remotename(e->e_returnpath, m, TRUE, FALSE, 1352f624c86Seric TRUE, e)); 136c23ed322Seric if (e->e_returnpath == e->e_sender) 137c23ed322Seric { 138c23ed322Seric from = rpathbuf; 139c23ed322Seric } 140c23ed322Seric else 141c23ed322Seric { 1422f624c86Seric (void) strcpy(tfrombuf, remotename(e->e_sender, m, TRUE, FALSE, 1432f624c86Seric TRUE, e)); 144c23ed322Seric from = tfrombuf; 145c23ed322Seric } 14678442df3Seric 147c23ed322Seric define('f', e->e_returnpath, e); /* raw return path */ 148c23ed322Seric define('<', rpathbuf, e); /* translated return path */ 149c23ed322Seric define('g', from, e); /* translated sender */ 150588cad61Seric define('h', host, e); /* to host */ 1515dfc646bSeric Errors = 0; 1525dfc646bSeric pvp = pv; 1535dfc646bSeric *pvp++ = m->m_argv[0]; 1545dfc646bSeric 1555dfc646bSeric /* insert -f or -r flag as appropriate */ 15657fc6f17Seric if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 1575dfc646bSeric { 15857fc6f17Seric if (bitnset(M_FOPT, m->m_flags)) 1595dfc646bSeric *pvp++ = "-f"; 1605dfc646bSeric else 1615dfc646bSeric *pvp++ = "-r"; 162c23ed322Seric *pvp++ = newstr(rpathbuf); 1635dfc646bSeric } 1645dfc646bSeric 1655dfc646bSeric /* 1665dfc646bSeric ** Append the other fixed parts of the argv. These run 1675dfc646bSeric ** up to the first entry containing "$u". There can only 1685dfc646bSeric ** be one of these, and there are only a few more slots 1695dfc646bSeric ** in the pv after it. 1705dfc646bSeric */ 1715dfc646bSeric 1725dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 1735dfc646bSeric { 1742bc47524Seric /* can't use strchr here because of sign extension problems */ 1752bc47524Seric while (*p != '\0') 1762bc47524Seric { 1772bc47524Seric if ((*p++ & 0377) == MACROEXPAND) 1782bc47524Seric { 1792bc47524Seric if (*p == 'u') 1805dfc646bSeric break; 1812bc47524Seric } 1822bc47524Seric } 1832bc47524Seric 1842bc47524Seric if (*p != '\0') 1855dfc646bSeric break; 1865dfc646bSeric 1875dfc646bSeric /* this entry is safe -- go ahead and process it */ 188588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 1895dfc646bSeric *pvp++ = newstr(buf); 1905dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 1915dfc646bSeric { 19208b25121Seric syserr("554 Too many parameters to %s before $u", pv[0]); 1935dfc646bSeric return (-1); 1945dfc646bSeric } 1955dfc646bSeric } 196c579ef51Seric 19733db8731Seric /* 19833db8731Seric ** If we have no substitution for the user name in the argument 19933db8731Seric ** list, we know that we must supply the names otherwise -- and 20033db8731Seric ** SMTP is the answer!! 20133db8731Seric */ 20233db8731Seric 2035dfc646bSeric if (*mvp == NULL) 204c579ef51Seric { 205c579ef51Seric /* running SMTP */ 2062c7e1b8dSeric # ifdef SMTP 207c579ef51Seric clever = TRUE; 208c579ef51Seric *pvp = NULL; 2096c2c3107Seric # else /* SMTP */ 21033db8731Seric /* oops! we don't implement SMTP */ 21108b25121Seric syserr("554 SMTP style mailer"); 2122c7e1b8dSeric return (EX_SOFTWARE); 2136c2c3107Seric # endif /* SMTP */ 214c579ef51Seric } 2155dfc646bSeric 2165dfc646bSeric /* 2175dfc646bSeric ** At this point *mvp points to the argument with $u. We 2185dfc646bSeric ** run through our address list and append all the addresses 2195dfc646bSeric ** we can. If we run out of space, do not fret! We can 2205dfc646bSeric ** always send another copy later. 2215dfc646bSeric */ 2225dfc646bSeric 2235dfc646bSeric tobuf[0] = '\0'; 224588cad61Seric e->e_to = tobuf; 2256259796dSeric ctladdr = NULL; 226e103b48fSeric firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e); 2275dfc646bSeric for (; to != NULL; to = to->q_next) 2285dfc646bSeric { 2295dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 23057fc6f17Seric if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 2315dfc646bSeric break; 2325dfc646bSeric 2335dfc646bSeric /* if already sent or not for this host, don't send */ 234c6e18ac5Seric if (bitset(QDONTSEND|QQUEUEUP, to->q_flags) || 235e103b48fSeric to->q_mailer != firstto->q_mailer || 236e103b48fSeric strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0) 2375dfc646bSeric continue; 2386259796dSeric 2394b22ea87Seric /* avoid overflowing tobuf */ 240aa50a568Sbostic if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 2414b22ea87Seric break; 2424b22ea87Seric 2436ef48975Seric if (tTd(10, 1)) 244772e6e50Seric { 245772e6e50Seric printf("\nsend to "); 246772e6e50Seric printaddr(to, FALSE); 247772e6e50Seric } 248772e6e50Seric 2496259796dSeric /* compute effective uid/gid when sending */ 2507da1035fSeric if (to->q_mailer == ProgMailer) 2516259796dSeric ctladdr = getctladdr(to); 2526259796dSeric 2535dfc646bSeric user = to->q_user; 254588cad61Seric e->e_to = to->q_paddr; 2555dfc646bSeric to->q_flags |= QDONTSEND; 25675f1ade9Seric if (tTd(10, 5)) 25775f1ade9Seric { 25875f1ade9Seric printf("deliver: QDONTSEND "); 25975f1ade9Seric printaddr(to, FALSE); 26075f1ade9Seric } 2615dfc646bSeric 2625dfc646bSeric /* 2635dfc646bSeric ** Check to see that these people are allowed to 2645dfc646bSeric ** talk to each other. 2652a6e0786Seric */ 2662a6e0786Seric 26769582d2fSeric if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 26869582d2fSeric { 26969582d2fSeric NoReturn = TRUE; 27008b25121Seric usrerr("552 Message is too large; %ld bytes max", m->m_maxsize); 271*81161401Seric giveresponse(EX_UNAVAILABLE, m, NULL, e); 27269582d2fSeric continue; 27369582d2fSeric } 274fabb3bd4Seric rcode = checkcompat(to, e); 2751793c9c5Seric if (rcode != EX_OK) 2765dfc646bSeric { 277*81161401Seric giveresponse(rcode, m, NULL, e); 2785dfc646bSeric continue; 2795dfc646bSeric } 2802a6e0786Seric 2812a6e0786Seric /* 2829ec9501bSeric ** Strip quote bits from names if the mailer is dumb 2839ec9501bSeric ** about them. 28425a99e2eSeric */ 28525a99e2eSeric 28657fc6f17Seric if (bitnset(M_STRIPQ, m->m_flags)) 28725a99e2eSeric { 2881d8f1806Seric stripquotes(user); 2891d8f1806Seric stripquotes(host); 29025a99e2eSeric } 29125a99e2eSeric 292cdb828c5Seric /* hack attack -- delivermail compatibility */ 293cdb828c5Seric if (m == ProgMailer && *user == '|') 294cdb828c5Seric user++; 295cdb828c5Seric 29625a99e2eSeric /* 2973efaed6eSeric ** If an error message has already been given, don't 2983efaed6eSeric ** bother to send to this address. 2993efaed6eSeric ** 3003efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 3013efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 3023efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 3033efaed6eSeric */ 3043efaed6eSeric 3056cae517dSeric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 3063efaed6eSeric continue; 3073efaed6eSeric 308f2fec898Seric /* save statistics.... */ 309588cad61Seric markstats(e, to); 310f2fec898Seric 3113efaed6eSeric /* 31225a99e2eSeric ** See if this user name is "special". 31325a99e2eSeric ** If the user name has a slash in it, assume that this 31451552439Seric ** is a file -- send it off without further ado. Note 31551552439Seric ** that this type of addresses is not processed along 31651552439Seric ** with the others, so we fudge on the To person. 31725a99e2eSeric */ 31825a99e2eSeric 3192c017f8dSeric if (m == FileMailer) 32025a99e2eSeric { 321b31e7f2bSeric rcode = mailfile(user, getctladdr(to), e); 322*81161401Seric giveresponse(rcode, m, NULL, e); 323dde5acadSeric if (rcode == EX_OK) 324dde5acadSeric to->q_flags |= QSENT; 3255dfc646bSeric continue; 32625a99e2eSeric } 32725a99e2eSeric 32813bbc08cSeric /* 32913bbc08cSeric ** Address is verified -- add this user to mailer 33013bbc08cSeric ** argv, and add it to the print list of recipients. 33113bbc08cSeric */ 33213bbc08cSeric 333508daeccSeric /* link together the chain of recipients */ 334508daeccSeric to->q_tchain = tochain; 335508daeccSeric tochain = to; 336508daeccSeric 3375dfc646bSeric /* create list of users for error messages */ 338db8841e9Seric (void) strcat(tobuf, ","); 339db8841e9Seric (void) strcat(tobuf, to->q_paddr); 340588cad61Seric define('u', user, e); /* to user */ 341588cad61Seric define('z', to->q_home, e); /* user's home */ 3425dfc646bSeric 343c579ef51Seric /* 344508daeccSeric ** Expand out this user into argument list. 345c579ef51Seric */ 346c579ef51Seric 347508daeccSeric if (!clever) 348c579ef51Seric { 349588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 3505dfc646bSeric *pvp++ = newstr(buf); 3515dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 3525dfc646bSeric { 3535dfc646bSeric /* allow some space for trailing parms */ 3545dfc646bSeric break; 3555dfc646bSeric } 3565dfc646bSeric } 357c579ef51Seric } 3585dfc646bSeric 359145b49b1Seric /* see if any addresses still exist */ 360145b49b1Seric if (tobuf[0] == '\0') 361c579ef51Seric { 362588cad61Seric define('g', (char *) NULL, e); 363c23ed322Seric define('<', (char *) NULL, e); 364145b49b1Seric return (0); 365c579ef51Seric } 366145b49b1Seric 3675dfc646bSeric /* print out messages as full list */ 36863780dbdSeric e->e_to = tobuf + 1; 3695dfc646bSeric 3705dfc646bSeric /* 3715dfc646bSeric ** Fill out any parameters after the $u parameter. 3725dfc646bSeric */ 3735dfc646bSeric 374c579ef51Seric while (!clever && *++mvp != NULL) 3755dfc646bSeric { 376588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 3775dfc646bSeric *pvp++ = newstr(buf); 3785dfc646bSeric if (pvp >= &pv[MAXPV]) 37908b25121Seric syserr("554 deliver: pv overflow after $u for %s", pv[0]); 3805dfc646bSeric } 3815dfc646bSeric *pvp++ = NULL; 3825dfc646bSeric 38325a99e2eSeric /* 38425a99e2eSeric ** Call the mailer. 3856328bdf7Seric ** The argument vector gets built, pipes 38625a99e2eSeric ** are created as necessary, and we fork & exec as 3876328bdf7Seric ** appropriate. 388c579ef51Seric ** If we are running SMTP, we just need to clean up. 38925a99e2eSeric */ 39025a99e2eSeric 3917ee87e5fSeric if (ctladdr == NULL && m != ProgMailer) 39286b26461Seric ctladdr = &e->e_from; 393134746fbSeric #ifdef NAMED_BIND 3942bcc6d2dSeric if (ConfigLevel < 2) 395912a731aSbostic _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 396134746fbSeric #endif 397b31e7f2bSeric mci = openmailer(m, pv, ctladdr, clever, e); 398b31e7f2bSeric if (mci == NULL) 399134746fbSeric { 400b31e7f2bSeric /* catastrophic error */ 401845e533cSeric rcode = EX_OSERR; 402b31e7f2bSeric goto give_up; 403b31e7f2bSeric } 404b31e7f2bSeric else if (mci->mci_state != MCIS_OPEN) 405b31e7f2bSeric { 406b31e7f2bSeric /* couldn't open the mailer */ 407b31e7f2bSeric rcode = mci->mci_exitstat; 4082a6bc25bSeric errno = mci->mci_errno; 409b31e7f2bSeric if (rcode == EX_OK) 410b31e7f2bSeric { 411b31e7f2bSeric /* shouldn't happen */ 41208b25121Seric syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 4136b0f339dSeric rcode, mci->mci_state, firstsig); 414b31e7f2bSeric rcode = EX_SOFTWARE; 415b31e7f2bSeric } 416b31e7f2bSeric } 417b31e7f2bSeric else if (!clever) 418b31e7f2bSeric { 419b31e7f2bSeric /* 420b31e7f2bSeric ** Format and send message. 421b31e7f2bSeric */ 42215d084d5Seric 423b31e7f2bSeric putfromline(mci->mci_out, m, e); 424b31e7f2bSeric (*e->e_puthdr)(mci->mci_out, m, e); 425b31e7f2bSeric putline("\n", mci->mci_out, m); 426b31e7f2bSeric (*e->e_putbody)(mci->mci_out, m, e); 427b31e7f2bSeric 428b31e7f2bSeric /* get the exit status */ 429b31e7f2bSeric rcode = endmailer(mci, pv[0]); 430134746fbSeric } 431134746fbSeric else 432b31e7f2bSeric #ifdef SMTP 433134746fbSeric { 434b31e7f2bSeric /* 435b31e7f2bSeric ** Send the MAIL FROM: protocol 436b31e7f2bSeric */ 43715d084d5Seric 438b31e7f2bSeric rcode = smtpmailfrom(m, mci, e); 439b31e7f2bSeric if (rcode == EX_OK) 44075889e88Seric { 441ded0d3daSkarels register char *t = tobuf; 442ded0d3daSkarels register int i; 443ded0d3daSkarels 444588cad61Seric /* send the recipient list */ 44563780dbdSeric tobuf[0] = '\0'; 44675889e88Seric for (to = tochain; to != NULL; to = to->q_tchain) 44775889e88Seric { 44863780dbdSeric e->e_to = to->q_paddr; 44915d084d5Seric if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 45075889e88Seric { 45183b7ddc9Seric markfailure(e, to, i); 452*81161401Seric giveresponse(i, m, mci, e); 45363780dbdSeric } 45475889e88Seric else 45575889e88Seric { 456911693bfSbostic *t++ = ','; 457b31e7f2bSeric for (p = to->q_paddr; *p; *t++ = *p++) 458b31e7f2bSeric continue; 459588cad61Seric } 460588cad61Seric } 461588cad61Seric 46263780dbdSeric /* now send the data */ 46363780dbdSeric if (tobuf[0] == '\0') 464b31e7f2bSeric { 46563780dbdSeric e->e_to = NULL; 466b31e7f2bSeric if (bitset(MCIF_CACHED, mci->mci_flags)) 467b31e7f2bSeric smtprset(m, mci, e); 468b31e7f2bSeric } 46975889e88Seric else 47075889e88Seric { 47163780dbdSeric e->e_to = tobuf + 1; 47275889e88Seric rcode = smtpdata(m, mci, e); 47363780dbdSeric } 47463780dbdSeric 47563780dbdSeric /* now close the connection */ 476b31e7f2bSeric if (!bitset(MCIF_CACHED, mci->mci_flags)) 47715d084d5Seric smtpquit(m, mci, e); 47863780dbdSeric } 479c579ef51Seric } 480b31e7f2bSeric #else /* not SMTP */ 481a05b3449Sbostic { 48208b25121Seric syserr("554 deliver: need SMTP compiled to use clever mailer"); 483845e533cSeric rcode = EX_CONFIG; 484b31e7f2bSeric goto give_up; 485a05b3449Sbostic } 486b31e7f2bSeric #endif /* SMTP */ 487134746fbSeric #ifdef NAMED_BIND 4882bcc6d2dSeric if (ConfigLevel < 2) 489912a731aSbostic _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 490134746fbSeric #endif 4915dfc646bSeric 492b31e7f2bSeric /* arrange a return receipt if requested */ 4938c13bf07Seric if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags)) 494b31e7f2bSeric { 495b31e7f2bSeric e->e_flags |= EF_SENDRECEIPT; 496b31e7f2bSeric /* do we want to send back more info? */ 497b31e7f2bSeric } 498b31e7f2bSeric 499c77d1c25Seric /* 50063780dbdSeric ** Do final status disposal. 50163780dbdSeric ** We check for something in tobuf for the SMTP case. 502c77d1c25Seric ** If we got a temporary failure, arrange to queue the 503c77d1c25Seric ** addressees. 504c77d1c25Seric */ 505c77d1c25Seric 506b31e7f2bSeric give_up: 50763780dbdSeric if (tobuf[0] != '\0') 508*81161401Seric giveresponse(rcode, m, mci, e); 509772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 510b31e7f2bSeric { 511dde5acadSeric if (rcode != EX_OK) 51283b7ddc9Seric markfailure(e, to, rcode); 513dde5acadSeric else 514655518ecSeric { 515dde5acadSeric to->q_flags |= QSENT; 516655518ecSeric e->e_nsent++; 517655518ecSeric } 518b31e7f2bSeric } 519b31e7f2bSeric 520b31e7f2bSeric /* 521b31e7f2bSeric ** Restore state and return. 522b31e7f2bSeric */ 523c77d1c25Seric 52435490626Seric errno = 0; 525588cad61Seric define('g', (char *) NULL, e); 526c23ed322Seric define('<', (char *) NULL, e); 5275826d9d3Seric return (rcode); 52825a99e2eSeric } 5295dfc646bSeric /* 53083b7ddc9Seric ** MARKFAILURE -- mark a failure on a specific address. 53183b7ddc9Seric ** 53283b7ddc9Seric ** Parameters: 53383b7ddc9Seric ** e -- the envelope we are sending. 53483b7ddc9Seric ** q -- the address to mark. 53583b7ddc9Seric ** rcode -- the code signifying the particular failure. 53683b7ddc9Seric ** 53783b7ddc9Seric ** Returns: 53883b7ddc9Seric ** none. 53983b7ddc9Seric ** 54083b7ddc9Seric ** Side Effects: 54183b7ddc9Seric ** marks the address (and possibly the envelope) with the 54283b7ddc9Seric ** failure so that an error will be returned or 54383b7ddc9Seric ** the message will be queued, as appropriate. 54483b7ddc9Seric */ 54583b7ddc9Seric 54683b7ddc9Seric markfailure(e, q, rcode) 54783b7ddc9Seric register ENVELOPE *e; 54883b7ddc9Seric register ADDRESS *q; 54983b7ddc9Seric int rcode; 55083b7ddc9Seric { 55183b7ddc9Seric if (rcode == EX_OK) 55283b7ddc9Seric return; 553ef137175Sbostic else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR) 55483b7ddc9Seric q->q_flags |= QBADADDR; 55583b7ddc9Seric else if (curtime() > e->e_ctime + TimeOut) 55683b7ddc9Seric { 55783b7ddc9Seric extern char *pintvl(); 558198d9be0Seric char buf[MAXLINE]; 55983b7ddc9Seric 56083b7ddc9Seric if (!bitset(EF_TIMEOUT, e->e_flags)) 561198d9be0Seric { 562198d9be0Seric (void) sprintf(buf, "Cannot send message for %s", 56383b7ddc9Seric pintvl(TimeOut, FALSE)); 564198d9be0Seric if (e->e_message != NULL) 565198d9be0Seric free(e->e_message); 566198d9be0Seric e->e_message = newstr(buf); 56708b25121Seric message(buf); 568198d9be0Seric } 56983b7ddc9Seric q->q_flags |= QBADADDR; 57083b7ddc9Seric e->e_flags |= EF_TIMEOUT; 571c13b5dfcSeric fprintf(e->e_xfp, "421 %s... Message timed out\n", q->q_paddr); 57283b7ddc9Seric } 57383b7ddc9Seric else 57483b7ddc9Seric q->q_flags |= QQUEUEUP; 57583b7ddc9Seric } 57683b7ddc9Seric /* 57732d19d43Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 57832d19d43Seric ** 57932d19d43Seric ** This MUST be a macro, since after a vfork we are running 58032d19d43Seric ** two processes on the same stack!!! 58132d19d43Seric ** 58232d19d43Seric ** Parameters: 58332d19d43Seric ** none. 58432d19d43Seric ** 58532d19d43Seric ** Returns: 58632d19d43Seric ** From a macro??? You've got to be kidding! 58732d19d43Seric ** 58832d19d43Seric ** Side Effects: 58932d19d43Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 59032d19d43Seric ** pid of child in parent, zero in child. 59132d19d43Seric ** -1 on unrecoverable error. 59232d19d43Seric ** 59332d19d43Seric ** Notes: 59432d19d43Seric ** I'm awfully sorry this looks so awful. That's 59532d19d43Seric ** vfork for you..... 59632d19d43Seric */ 59732d19d43Seric 59832d19d43Seric # define NFORKTRIES 5 59984f7cd1bSeric 60084f7cd1bSeric # ifndef FORK 60184f7cd1bSeric # define FORK fork 60284f7cd1bSeric # endif 60332d19d43Seric 60432d19d43Seric # define DOFORK(fORKfN) \ 60532d19d43Seric {\ 60632d19d43Seric register int i;\ 60732d19d43Seric \ 60811799049Seric for (i = NFORKTRIES; --i >= 0; )\ 60932d19d43Seric {\ 61032d19d43Seric pid = fORKfN();\ 61132d19d43Seric if (pid >= 0)\ 61232d19d43Seric break;\ 61311799049Seric if (i > 0)\ 6146c4635f6Seric sleep((unsigned) NFORKTRIES - i);\ 61532d19d43Seric }\ 61632d19d43Seric } 61732d19d43Seric /* 6182ed72599Seric ** DOFORK -- simple fork interface to DOFORK. 6192ed72599Seric ** 6202ed72599Seric ** Parameters: 6212ed72599Seric ** none. 6222ed72599Seric ** 6232ed72599Seric ** Returns: 6242ed72599Seric ** pid of child in parent. 6252ed72599Seric ** zero in child. 6262ed72599Seric ** -1 on error. 6272ed72599Seric ** 6282ed72599Seric ** Side Effects: 6292ed72599Seric ** returns twice, once in parent and once in child. 6302ed72599Seric */ 6312ed72599Seric 6322ed72599Seric dofork() 6332ed72599Seric { 6342ed72599Seric register int pid; 6352ed72599Seric 6362ed72599Seric DOFORK(fork); 6372ed72599Seric return (pid); 6382ed72599Seric } 6392ed72599Seric /* 640c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 641c579ef51Seric ** 642c579ef51Seric ** We should never get fatal errors (e.g., segmentation 643c579ef51Seric ** violation), so we report those specially. For other 644c579ef51Seric ** errors, we choose a status message (into statmsg), 645c579ef51Seric ** and if it represents an error, we print it. 646c579ef51Seric ** 647c579ef51Seric ** Parameters: 648c579ef51Seric ** pid -- pid of mailer. 649c579ef51Seric ** name -- name of mailer (for error messages). 650c579ef51Seric ** 651c579ef51Seric ** Returns: 652c579ef51Seric ** exit code of mailer. 653c579ef51Seric ** 654c579ef51Seric ** Side Effects: 655c579ef51Seric ** none. 656c579ef51Seric */ 657c579ef51Seric 65875889e88Seric endmailer(mci, name) 659b31e7f2bSeric register MCI *mci; 660c579ef51Seric char *name; 661c579ef51Seric { 662588cad61Seric int st; 663c579ef51Seric 66475889e88Seric /* close any connections */ 66575889e88Seric if (mci->mci_in != NULL) 66675889e88Seric (void) fclose(mci->mci_in); 66775889e88Seric if (mci->mci_out != NULL) 66875889e88Seric (void) fclose(mci->mci_out); 66975889e88Seric mci->mci_in = mci->mci_out = NULL; 67075889e88Seric mci->mci_state = MCIS_CLOSED; 67175889e88Seric 67233db8731Seric /* in the IPC case there is nothing to wait for */ 67375889e88Seric if (mci->mci_pid == 0) 67433db8731Seric return (EX_OK); 67533db8731Seric 67633db8731Seric /* wait for the mailer process to die and collect status */ 67775889e88Seric st = waitfor(mci->mci_pid); 678588cad61Seric if (st == -1) 67978de67c1Seric { 680588cad61Seric syserr("endmailer %s: wait", name); 681588cad61Seric return (EX_SOFTWARE); 682c579ef51Seric } 68333db8731Seric 68433db8731Seric /* see if it died a horrid death */ 685c579ef51Seric if ((st & 0377) != 0) 686c579ef51Seric { 6875f73204aSeric syserr("mailer %s died with signal %o", name, st); 6885f73204aSeric ExitStat = EX_TEMPFAIL; 6895f73204aSeric return (EX_TEMPFAIL); 690c579ef51Seric } 69133db8731Seric 69233db8731Seric /* normal death -- return status */ 693588cad61Seric st = (st >> 8) & 0377; 694588cad61Seric return (st); 695c579ef51Seric } 696c579ef51Seric /* 697c579ef51Seric ** OPENMAILER -- open connection to mailer. 698c579ef51Seric ** 699c579ef51Seric ** Parameters: 700c579ef51Seric ** m -- mailer descriptor. 701c579ef51Seric ** pvp -- parameter vector to pass to mailer. 702c579ef51Seric ** ctladdr -- controlling address for user. 703c579ef51Seric ** clever -- create a full duplex connection. 704c579ef51Seric ** 705c579ef51Seric ** Returns: 70675889e88Seric ** The mail connection info struct for this connection. 70775889e88Seric ** NULL on failure. 708c579ef51Seric ** 709c579ef51Seric ** Side Effects: 710c579ef51Seric ** creates a mailer in a subprocess. 711c579ef51Seric */ 712c579ef51Seric 713b31e7f2bSeric MCI * 714b31e7f2bSeric openmailer(m, pvp, ctladdr, clever, e) 715588cad61Seric MAILER *m; 716c579ef51Seric char **pvp; 717c579ef51Seric ADDRESS *ctladdr; 718c579ef51Seric bool clever; 719b31e7f2bSeric ENVELOPE *e; 720c579ef51Seric { 7215dfc646bSeric int pid; 722b31e7f2bSeric register MCI *mci; 723f8952a83Seric int mpvect[2]; 724c579ef51Seric int rpvect[2]; 7255dfc646bSeric extern FILE *fdopen(); 7265dfc646bSeric 7276ef48975Seric if (tTd(11, 1)) 7285dfc646bSeric { 7298c57e552Seric printf("openmailer:"); 7305dfc646bSeric printav(pvp); 7315dfc646bSeric } 73235490626Seric errno = 0; 7335dfc646bSeric 734ef66a9d0Seric CurHostName = m->m_mailer; 735ef66a9d0Seric 73633db8731Seric /* 73733db8731Seric ** Deal with the special case of mail handled through an IPC 73833db8731Seric ** connection. 73933db8731Seric ** In this case we don't actually fork. We must be 74033db8731Seric ** running SMTP for this to work. We will return a 74133db8731Seric ** zero pid to indicate that we are running IPC. 742e7c1bd78Seric ** We also handle a debug version that just talks to stdin/out. 74333db8731Seric */ 74433db8731Seric 745e7c1bd78Seric /* check for Local Person Communication -- not for mortals!!! */ 746e7c1bd78Seric if (strcmp(m->m_mailer, "[LPC]") == 0) 747e7c1bd78Seric { 748b31e7f2bSeric mci = (MCI *) xalloc(sizeof *mci); 7492a087e90Seric bzero((char *) mci, sizeof *mci); 75075889e88Seric mci->mci_in = stdin; 75175889e88Seric mci->mci_out = stdout; 752b31e7f2bSeric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 75315d084d5Seric mci->mci_mailer = m; 754e7c1bd78Seric } 755b31e7f2bSeric else if (strcmp(m->m_mailer, "[IPC]") == 0 || 756914346b1Seric strcmp(m->m_mailer, "[TCP]") == 0) 75733db8731Seric { 75884f7cd1bSeric #ifdef DAEMON 759e103b48fSeric register int i; 7601277f9a8Seric register u_short port; 761e103b48fSeric char *curhost; 762b31e7f2bSeric extern MCI *mci_get(); 763e103b48fSeric extern char *hostsignature(); 76433db8731Seric 765ef66a9d0Seric CurHostName = pvp[1]; 766e103b48fSeric curhost = hostsignature(m, pvp[1], e); 767b31e7f2bSeric 76833db8731Seric if (!clever) 76908b25121Seric syserr("554 non-clever IPC"); 77093b6e3cfSeric if (pvp[2] != NULL) 7711277f9a8Seric port = atoi(pvp[2]); 77293b6e3cfSeric else 7731277f9a8Seric port = 0; 774e103b48fSeric while (*curhost != '\0') 775ebc61751Sbloom { 776e103b48fSeric register char *p; 777e103b48fSeric char hostbuf[MAXNAME]; 778e103b48fSeric 779e103b48fSeric /* pull the next host from the signature */ 780e103b48fSeric p = strchr(curhost, ':'); 781e103b48fSeric if (p == NULL) 782e103b48fSeric p = &curhost[strlen(curhost)]; 783e103b48fSeric strncpy(hostbuf, curhost, p - curhost); 784e103b48fSeric hostbuf[p - curhost] = '\0'; 785e103b48fSeric if (*p != '\0') 786e103b48fSeric p++; 787e103b48fSeric curhost = p; 788e103b48fSeric 78945a8316eSeric /* see if we already know that this host is fried */ 790e103b48fSeric CurHostName = hostbuf; 791e103b48fSeric mci = mci_get(hostbuf, m); 792b31e7f2bSeric if (mci->mci_state != MCIS_CLOSED) 7931fa7c2ebSeric { 7941fa7c2ebSeric if (tTd(11, 1)) 7951fa7c2ebSeric { 7961fa7c2ebSeric printf("openmailer: "); 7971fa7c2ebSeric mci_dump(mci); 7981fa7c2ebSeric } 79967088a9dSeric CurHostName = mci->mci_host; 80075889e88Seric return mci; 8011fa7c2ebSeric } 80215d084d5Seric mci->mci_mailer = m; 803b31e7f2bSeric if (mci->mci_exitstat != EX_OK) 804b31e7f2bSeric continue; 805b31e7f2bSeric 80675889e88Seric /* try the connection */ 807e103b48fSeric setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 80808b25121Seric message("Connecting to %s (%s)...", 809e103b48fSeric hostbuf, m->m_name); 810e103b48fSeric i = makeconnection(hostbuf, port, mci, 811914346b1Seric bitnset(M_SECURE_PORT, m->m_flags)); 81275889e88Seric mci->mci_exitstat = i; 81375889e88Seric mci->mci_errno = errno; 81475889e88Seric if (i == EX_OK) 815b31e7f2bSeric { 816b31e7f2bSeric mci->mci_state = MCIS_OPENING; 817b31e7f2bSeric mci_cache(mci); 818b31e7f2bSeric break; 819b31e7f2bSeric } 820b31e7f2bSeric else if (tTd(11, 1)) 821b31e7f2bSeric printf("openmailer: makeconnection => stat=%d, errno=%d\n", 822b31e7f2bSeric i, errno); 823b31e7f2bSeric 82475889e88Seric 8255f73204aSeric /* enter status of this host */ 82675889e88Seric setstat(i); 827ed854c7bSeric } 8282a087e90Seric mci->mci_pid = 0; 829b31e7f2bSeric #else /* no DAEMON */ 83008b25121Seric syserr("554 openmailer: no IPC"); 8311fa7c2ebSeric if (tTd(11, 1)) 8321fa7c2ebSeric printf("openmailer: NULL\n"); 83375889e88Seric return NULL; 834b31e7f2bSeric #endif /* DAEMON */ 835588cad61Seric } 836b31e7f2bSeric else 837b31e7f2bSeric { 8386328bdf7Seric /* create a pipe to shove the mail through */ 839f8952a83Seric if (pipe(mpvect) < 0) 84025a99e2eSeric { 841588cad61Seric syserr("openmailer: pipe (to mailer)"); 8421fa7c2ebSeric if (tTd(11, 1)) 8431fa7c2ebSeric printf("openmailer: NULL\n"); 84475889e88Seric return NULL; 84525a99e2eSeric } 846c579ef51Seric 847c579ef51Seric /* if this mailer speaks smtp, create a return pipe */ 848c579ef51Seric if (clever && pipe(rpvect) < 0) 849c579ef51Seric { 850588cad61Seric syserr("openmailer: pipe (from mailer)"); 851c579ef51Seric (void) close(mpvect[0]); 852c579ef51Seric (void) close(mpvect[1]); 8531fa7c2ebSeric if (tTd(11, 1)) 8541fa7c2ebSeric printf("openmailer: NULL\n"); 85575889e88Seric return NULL; 856c579ef51Seric } 857c579ef51Seric 85833db8731Seric /* 85933db8731Seric ** Actually fork the mailer process. 86033db8731Seric ** DOFORK is clever about retrying. 8616984bfddSeric ** 8626984bfddSeric ** Dispose of SIGCHLD signal catchers that may be laying 8636984bfddSeric ** around so that endmail will get it. 86433db8731Seric */ 86533db8731Seric 866b31e7f2bSeric if (e->e_xfp != NULL) 867b31e7f2bSeric (void) fflush(e->e_xfp); /* for debugging */ 868588cad61Seric (void) fflush(stdout); 8696984bfddSeric # ifdef SIGCHLD 8706984bfddSeric (void) signal(SIGCHLD, SIG_DFL); 8716c2c3107Seric # endif /* SIGCHLD */ 87284f7cd1bSeric DOFORK(FORK); 873f129ec7dSeric /* pid is set by DOFORK */ 87425a99e2eSeric if (pid < 0) 87525a99e2eSeric { 87633db8731Seric /* failure */ 877588cad61Seric syserr("openmailer: cannot fork"); 878f8952a83Seric (void) close(mpvect[0]); 879f8952a83Seric (void) close(mpvect[1]); 880c579ef51Seric if (clever) 881c579ef51Seric { 882c579ef51Seric (void) close(rpvect[0]); 883c579ef51Seric (void) close(rpvect[1]); 884c579ef51Seric } 8851fa7c2ebSeric if (tTd(11, 1)) 8861fa7c2ebSeric printf("openmailer: NULL\n"); 88775889e88Seric return NULL; 88825a99e2eSeric } 88925a99e2eSeric else if (pid == 0) 89025a99e2eSeric { 89113088b9fSeric int i; 892dafbc479Seric int saveerrno; 893d7a0480eSeric char *env[2]; 8945f73204aSeric extern int DtableSize; 89513088b9fSeric 89625a99e2eSeric /* child -- set up input & exec mailer */ 89703ab8e55Seric /* make diagnostic output be standard output */ 8988f0e7860Seric (void) signal(SIGINT, SIG_IGN); 8998f0e7860Seric (void) signal(SIGHUP, SIG_IGN); 9000984da9fSeric (void) signal(SIGTERM, SIG_DFL); 901f8952a83Seric 9021caa9a59Seric /* close any other cached connections */ 9031caa9a59Seric mci_flush(FALSE, mci); 9041caa9a59Seric 905b31e7f2bSeric /* arrange to filter std & diag output of command */ 906c579ef51Seric if (clever) 907c579ef51Seric { 908c579ef51Seric (void) close(rpvect[0]); 909c579ef51Seric (void) close(1); 910c579ef51Seric (void) dup(rpvect[1]); 911c579ef51Seric (void) close(rpvect[1]); 912c579ef51Seric } 913276723a8Seric else if (OpMode == MD_SMTP || HoldErrs) 914f8952a83Seric { 915588cad61Seric /* put mailer output in transcript */ 916f8952a83Seric (void) close(1); 917b31e7f2bSeric (void) dup(fileno(e->e_xfp)); 918f8952a83Seric } 919db8841e9Seric (void) close(2); 920db8841e9Seric (void) dup(1); 921f8952a83Seric 922f8952a83Seric /* arrange to get standard input */ 923f8952a83Seric (void) close(mpvect[1]); 924db8841e9Seric (void) close(0); 925f8952a83Seric if (dup(mpvect[0]) < 0) 92625a99e2eSeric { 92725a99e2eSeric syserr("Cannot dup to zero!"); 928a590b978Seric _exit(EX_OSERR); 92925a99e2eSeric } 930f8952a83Seric (void) close(mpvect[0]); 93157fc6f17Seric if (!bitnset(M_RESTR, m->m_flags)) 9320984da9fSeric { 93353e3fa05Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 934e36b99e2Seric { 935e36b99e2Seric (void) setgid(DefGid); 936898a126bSbostic (void) initgroups(DefUser, DefGid); 937e36b99e2Seric (void) setuid(DefUid); 938e36b99e2Seric } 939e36b99e2Seric else 94069f29479Seric { 941e36b99e2Seric (void) setgid(ctladdr->q_gid); 942898a126bSbostic (void) initgroups(ctladdr->q_ruser? 943898a126bSbostic ctladdr->q_ruser: ctladdr->q_user, 944898a126bSbostic ctladdr->q_gid); 945e36b99e2Seric (void) setuid(ctladdr->q_uid); 94669f29479Seric } 9470984da9fSeric } 948588cad61Seric 94913088b9fSeric /* arrange for all the files to be closed */ 950b31e7f2bSeric for (i = 3; i < DtableSize; i++) 951b31e7f2bSeric { 952fccb3f7aSbostic register int j; 953fccb3f7aSbostic if ((j = fcntl(i, F_GETFD, 0)) != -1) 954fccb3f7aSbostic (void)fcntl(i, F_SETFD, j|1); 955fccb3f7aSbostic } 95633db8731Seric 95733db8731Seric /* try to execute the mailer */ 958d7a0480eSeric env[0] = "AGENT=sendmail"; 959d7a0480eSeric env[1] = NULL; 960d7a0480eSeric execve(m->m_mailer, pvp, env); 961dafbc479Seric saveerrno = errno; 96213088b9fSeric syserr("Cannot exec %s", m->m_mailer); 96395b76e4bSeric if (m == LocalMailer) 96455f33c03Seric _exit(EX_TEMPFAIL); 965dafbc479Seric switch (saveerrno) 96695b76e4bSeric { 96795b76e4bSeric case EIO: 96895b76e4bSeric case EAGAIN: 96995b76e4bSeric case ENOMEM: 97095b76e4bSeric # ifdef EPROCLIM 97195b76e4bSeric case EPROCLIM: 97295b76e4bSeric # endif 9731ab55cd7Seric # ifdef ETIMEDOUT 9741ab55cd7Seric case ETIMEDOUT: 9751ab55cd7Seric # endif 97695b76e4bSeric _exit(EX_TEMPFAIL); 97795b76e4bSeric } 978a590b978Seric _exit(EX_UNAVAILABLE); 97925a99e2eSeric } 98025a99e2eSeric 981f8952a83Seric /* 982c579ef51Seric ** Set up return value. 983f8952a83Seric */ 984f8952a83Seric 985b31e7f2bSeric mci = (MCI *) xalloc(sizeof *mci); 9862a087e90Seric bzero((char *) mci, sizeof *mci); 98715d084d5Seric mci->mci_mailer = m; 988b31e7f2bSeric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 9892a087e90Seric mci->mci_pid = pid; 990f8952a83Seric (void) close(mpvect[0]); 99175889e88Seric mci->mci_out = fdopen(mpvect[1], "w"); 992c579ef51Seric if (clever) 99325a99e2eSeric { 994c579ef51Seric (void) close(rpvect[1]); 99575889e88Seric mci->mci_in = fdopen(rpvect[0], "r"); 99675889e88Seric } 99775889e88Seric else 99875889e88Seric { 99975889e88Seric mci->mci_flags |= MCIF_TEMP; 100075889e88Seric mci->mci_in = NULL; 100175889e88Seric } 1002b31e7f2bSeric } 1003b31e7f2bSeric 1004b31e7f2bSeric /* 1005b31e7f2bSeric ** If we are in SMTP opening state, send initial protocol. 1006b31e7f2bSeric */ 1007b31e7f2bSeric 1008b31e7f2bSeric if (clever && mci->mci_state != MCIS_CLOSED) 1009b31e7f2bSeric { 1010b31e7f2bSeric smtpinit(m, mci, e); 1011b31e7f2bSeric } 10121fa7c2ebSeric if (tTd(11, 1)) 10131fa7c2ebSeric { 10141fa7c2ebSeric printf("openmailer: "); 10151fa7c2ebSeric mci_dump(mci); 10161fa7c2ebSeric } 1017c579ef51Seric 101875889e88Seric return mci; 101925a99e2eSeric } 102025a99e2eSeric /* 102125a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 102225a99e2eSeric ** 102325a99e2eSeric ** Parameters: 102425a99e2eSeric ** stat -- the status code from the mailer (high byte 102525a99e2eSeric ** only; core dumps must have been taken care of 102625a99e2eSeric ** already). 1027*81161401Seric ** m -- the mailer info for this mailer. 1028*81161401Seric ** mci -- the mailer connection info -- can be NULL if the 1029*81161401Seric ** response is given before the connection is made. 1030*81161401Seric ** e -- the current envelope. 103125a99e2eSeric ** 103225a99e2eSeric ** Returns: 1033db8841e9Seric ** none. 103425a99e2eSeric ** 103525a99e2eSeric ** Side Effects: 1036c1f9df2cSeric ** Errors may be incremented. 103725a99e2eSeric ** ExitStat may be set. 103825a99e2eSeric */ 103925a99e2eSeric 1040*81161401Seric giveresponse(stat, m, mci, e) 104125a99e2eSeric int stat; 1042588cad61Seric register MAILER *m; 1043*81161401Seric register MCI *mci; 1044198d9be0Seric ENVELOPE *e; 104525a99e2eSeric { 104625a99e2eSeric register char *statmsg; 104725a99e2eSeric extern char *SysExMsg[]; 104825a99e2eSeric register int i; 1049d4bd8f0eSbostic extern int N_SysEx; 1050d4bd8f0eSbostic #ifdef NAMED_BIND 1051d4bd8f0eSbostic extern int h_errno; 1052d4bd8f0eSbostic #endif 1053198d9be0Seric char buf[MAXLINE]; 105425a99e2eSeric 105513bbc08cSeric /* 105613bbc08cSeric ** Compute status message from code. 105713bbc08cSeric */ 105813bbc08cSeric 105925a99e2eSeric i = stat - EX__BASE; 1060588cad61Seric if (stat == 0) 1061588cad61Seric statmsg = "250 Sent"; 1062588cad61Seric else if (i < 0 || i > N_SysEx) 1063588cad61Seric { 1064588cad61Seric (void) sprintf(buf, "554 unknown mailer error %d", stat); 1065588cad61Seric stat = EX_UNAVAILABLE; 1066588cad61Seric statmsg = buf; 1067588cad61Seric } 1068198d9be0Seric else if (stat == EX_TEMPFAIL) 1069198d9be0Seric { 10708557d168Seric (void) strcpy(buf, SysExMsg[i]); 1071d4bd8f0eSbostic #ifdef NAMED_BIND 1072f28da541Smiriam if (h_errno == TRY_AGAIN) 1073f28da541Smiriam { 1074f28da541Smiriam extern char *errstring(); 1075f28da541Smiriam 1076f28da541Smiriam statmsg = errstring(h_errno+MAX_ERRNO); 1077f28da541Smiriam } 1078f28da541Smiriam else 1079d4bd8f0eSbostic #endif 1080f28da541Smiriam { 10818557d168Seric if (errno != 0) 1082198d9be0Seric { 108387c9b3e7Seric extern char *errstring(); 10848557d168Seric 1085d87e85f3Seric statmsg = errstring(errno); 1086d87e85f3Seric } 1087d87e85f3Seric else 1088d87e85f3Seric { 1089d87e85f3Seric #ifdef SMTP 1090d87e85f3Seric extern char SmtpError[]; 1091d87e85f3Seric 1092d87e85f3Seric statmsg = SmtpError; 10936c2c3107Seric #else /* SMTP */ 1094d87e85f3Seric statmsg = NULL; 10956c2c3107Seric #endif /* SMTP */ 1096d87e85f3Seric } 1097f28da541Smiriam } 1098d87e85f3Seric if (statmsg != NULL && statmsg[0] != '\0') 1099d87e85f3Seric { 110087c9b3e7Seric (void) strcat(buf, ": "); 1101d87e85f3Seric (void) strcat(buf, statmsg); 11028557d168Seric } 1103198d9be0Seric statmsg = buf; 1104198d9be0Seric } 110525a99e2eSeric else 1106d87e85f3Seric { 110725a99e2eSeric statmsg = SysExMsg[i]; 1108d87e85f3Seric } 1109588cad61Seric 1110588cad61Seric /* 1111588cad61Seric ** Print the message as appropriate 1112588cad61Seric */ 1113588cad61Seric 1114198d9be0Seric if (stat == EX_OK || stat == EX_TEMPFAIL) 111508b25121Seric message(&statmsg[4]); 111625a99e2eSeric else 111725a99e2eSeric { 1118c1f9df2cSeric Errors++; 11195826d9d3Seric usrerr(statmsg); 112025a99e2eSeric } 112125a99e2eSeric 112225a99e2eSeric /* 112325a99e2eSeric ** Final cleanup. 112425a99e2eSeric ** Log a record of the transaction. Compute the new 112525a99e2eSeric ** ExitStat -- if we already had an error, stick with 112625a99e2eSeric ** that. 112725a99e2eSeric */ 112825a99e2eSeric 11292f624c86Seric if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 1130*81161401Seric logdelivery(m, mci, &statmsg[4], e); 1131eb238f8cSeric 1132eb238f8cSeric if (stat != EX_TEMPFAIL) 1133eb238f8cSeric setstat(stat); 1134198d9be0Seric if (stat != EX_OK) 1135198d9be0Seric { 1136198d9be0Seric if (e->e_message != NULL) 1137198d9be0Seric free(e->e_message); 1138198d9be0Seric e->e_message = newstr(&statmsg[4]); 1139198d9be0Seric } 11408557d168Seric errno = 0; 1141d4bd8f0eSbostic #ifdef NAMED_BIND 1142f28da541Smiriam h_errno = 0; 1143d4bd8f0eSbostic #endif 1144eb238f8cSeric } 1145eb238f8cSeric /* 1146eb238f8cSeric ** LOGDELIVERY -- log the delivery in the system log 1147eb238f8cSeric ** 1148eb238f8cSeric ** Parameters: 1149*81161401Seric ** m -- the mailer info. Can be NULL for initial queue. 1150*81161401Seric ** mci -- the mailer connection info -- can be NULL if the 1151*81161401Seric ** log is occuring when no connection is active. 1152*81161401Seric ** stat -- the message to print for the status. 1153*81161401Seric ** e -- the current envelope. 1154eb238f8cSeric ** 1155eb238f8cSeric ** Returns: 1156eb238f8cSeric ** none 1157eb238f8cSeric ** 1158eb238f8cSeric ** Side Effects: 1159eb238f8cSeric ** none 1160eb238f8cSeric */ 1161eb238f8cSeric 1162*81161401Seric logdelivery(m, mci, stat, e) 1163*81161401Seric MAILER *m; 1164*81161401Seric register MCI *mci; 1165eb238f8cSeric char *stat; 1166b31e7f2bSeric register ENVELOPE *e; 11675cf56be3Seric { 116871ff6caaSeric char *delay; 1169*81161401Seric char *curhost; 11705cf56be3Seric extern char *pintvl(); 11715cf56be3Seric 1172eb238f8cSeric # ifdef LOG 1173*81161401Seric if (mci != NULL && mci->mci_host != NULL) 1174*81161401Seric curhost = mci->mci_host; 1175*81161401Seric else 1176*81161401Seric curhost = CurHostName; 1177*81161401Seric 117871ff6caaSeric delay = pintvl(curtime() - e->e_ctime, TRUE); 1179*81161401Seric if (strcmp(stat, "Sent") != 0 || m == NULL || curhost == NULL || 1180*81161401Seric strcmp(curhost, "localhost") == 0) 118171ff6caaSeric { 118271ff6caaSeric syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", 118371ff6caaSeric e->e_id, e->e_to, delay, stat); 118471ff6caaSeric } 118571ff6caaSeric else 118671ff6caaSeric { 1187320e0d1cSeric char *p1, *p2; 118871ff6caaSeric extern char *macvalue(); 118971ff6caaSeric 1190*81161401Seric if (curhost[0] == '/') 119171ff6caaSeric { 1192320e0d1cSeric p1 = macvalue('h', e); 1193320e0d1cSeric if (p1 == NULL || p1[0] == '\0') 1194320e0d1cSeric p1 = "local"; 1195*81161401Seric p2 = curhost; 119671ff6caaSeric } 119771ff6caaSeric # ifdef DAEMON 119871ff6caaSeric else 119971ff6caaSeric { 120071ff6caaSeric extern struct sockaddr_in CurHostAddr; 120171ff6caaSeric extern char *inet_ntoa(); 120271ff6caaSeric 1203*81161401Seric p1 = curhost; 1204320e0d1cSeric p2 = inet_ntoa(CurHostAddr.sin_addr); 120571ff6caaSeric } 120671ff6caaSeric # endif 1207*81161401Seric syslog(LOG_INFO, "%s: to=%s, delay=%s, mailer=%s, stat=Sent to %s (%s)", 1208*81161401Seric e->e_id, e->e_to, delay, m->m_name, p1, p2); 120971ff6caaSeric } 12106c2c3107Seric # endif /* LOG */ 121125a99e2eSeric } 121225a99e2eSeric /* 121351552439Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 121425a99e2eSeric ** 121551552439Seric ** This can be made an arbitrary message separator by changing $l 121651552439Seric ** 12179b6c17a6Seric ** One of the ugliest hacks seen by human eyes is contained herein: 12189b6c17a6Seric ** UUCP wants those stupid "remote from <host>" lines. Why oh why 12199b6c17a6Seric ** does a well-meaning programmer such as myself have to deal with 12209b6c17a6Seric ** this kind of antique garbage???? 122125a99e2eSeric ** 122225a99e2eSeric ** Parameters: 122351552439Seric ** fp -- the file to output to. 122451552439Seric ** m -- the mailer describing this entry. 122525a99e2eSeric ** 122625a99e2eSeric ** Returns: 122751552439Seric ** none 122825a99e2eSeric ** 122925a99e2eSeric ** Side Effects: 123051552439Seric ** outputs some text to fp. 123125a99e2eSeric */ 123225a99e2eSeric 1233b31e7f2bSeric putfromline(fp, m, e) 123451552439Seric register FILE *fp; 123551552439Seric register MAILER *m; 1236b31e7f2bSeric ENVELOPE *e; 123725a99e2eSeric { 12382bc47524Seric char *template = "\201l\n"; 123951552439Seric char buf[MAXLINE]; 124025a99e2eSeric 124157fc6f17Seric if (bitnset(M_NHDR, m->m_flags)) 124251552439Seric return; 124313bbc08cSeric 12442c7e1b8dSeric # ifdef UGLYUUCP 124557fc6f17Seric if (bitnset(M_UGLYUUCP, m->m_flags)) 124674b6e67bSeric { 1247ea09d6edSeric char *bang; 1248ea09d6edSeric char xbuf[MAXLINE]; 124974b6e67bSeric 12502bc47524Seric expand("\201<", buf, &buf[sizeof buf - 1], e); 12516c2c3107Seric bang = strchr(buf, '!'); 125274b6e67bSeric if (bang == NULL) 125308b25121Seric syserr("554 No ! in UUCP! (%s)", buf); 125474b6e67bSeric else 1255588cad61Seric { 1256ea09d6edSeric *bang++ = '\0'; 12572bc47524Seric (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 1258ea09d6edSeric template = xbuf; 125974b6e67bSeric } 1260588cad61Seric } 12616c2c3107Seric # endif /* UGLYUUCP */ 1262b31e7f2bSeric expand(template, buf, &buf[sizeof buf - 1], e); 126377b52738Seric putline(buf, fp, m); 1264bc6e2962Seric } 1265bc6e2962Seric /* 126651552439Seric ** PUTBODY -- put the body of a message. 126751552439Seric ** 126851552439Seric ** Parameters: 126951552439Seric ** fp -- file to output onto. 127077b52738Seric ** m -- a mailer descriptor to control output format. 12719a6a5f55Seric ** e -- the envelope to put out. 127251552439Seric ** 127351552439Seric ** Returns: 127451552439Seric ** none. 127551552439Seric ** 127651552439Seric ** Side Effects: 127751552439Seric ** The message is written onto fp. 127851552439Seric */ 127951552439Seric 128077b52738Seric putbody(fp, m, e) 128151552439Seric FILE *fp; 1282588cad61Seric MAILER *m; 12839a6a5f55Seric register ENVELOPE *e; 128451552439Seric { 128577b52738Seric char buf[MAXLINE]; 128651552439Seric 128751552439Seric /* 128851552439Seric ** Output the body of the message 128951552439Seric */ 129051552439Seric 12919a6a5f55Seric if (e->e_dfp == NULL) 129251552439Seric { 12939a6a5f55Seric if (e->e_df != NULL) 12949a6a5f55Seric { 12959a6a5f55Seric e->e_dfp = fopen(e->e_df, "r"); 12969a6a5f55Seric if (e->e_dfp == NULL) 12978f9146b0Srick syserr("putbody: Cannot open %s for %s from %s", 12988f9146b0Srick e->e_df, e->e_to, e->e_from); 12999a6a5f55Seric } 13009a6a5f55Seric else 130177b52738Seric putline("<<< No Message Collected >>>", fp, m); 13029a6a5f55Seric } 13039a6a5f55Seric if (e->e_dfp != NULL) 13049a6a5f55Seric { 13059a6a5f55Seric rewind(e->e_dfp); 130677b52738Seric while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 130724fc8aeeSeric { 130824fc8aeeSeric if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1309d6fa2b58Sbostic strncmp(buf, "From ", 5) == 0) 13103462ad9eSeric (void) putc('>', fp); 131177b52738Seric putline(buf, fp, m); 131224fc8aeeSeric } 131351552439Seric 13149a6a5f55Seric if (ferror(e->e_dfp)) 131551552439Seric { 131651552439Seric syserr("putbody: read error"); 131751552439Seric ExitStat = EX_IOERR; 131851552439Seric } 131951552439Seric } 132051552439Seric 132151552439Seric (void) fflush(fp); 132251552439Seric if (ferror(fp) && errno != EPIPE) 132351552439Seric { 132451552439Seric syserr("putbody: write error"); 132551552439Seric ExitStat = EX_IOERR; 132651552439Seric } 132751552439Seric errno = 0; 132825a99e2eSeric } 132925a99e2eSeric /* 13309836bea2Seric ** PARENTBODY -- put the body of the parent of a message. 13319836bea2Seric ** 13329836bea2Seric ** Parameters: 13339836bea2Seric ** fp -- file to output onto. 13349836bea2Seric ** m -- a mailer descriptor to control output format. 13359836bea2Seric ** e -- the envelope whose parent to put out. 13369836bea2Seric ** 13379836bea2Seric ** Returns: 13389836bea2Seric ** none. 13399836bea2Seric ** 13409836bea2Seric ** Side Effects: 13419836bea2Seric ** The message is written onto fp. 13429836bea2Seric */ 13439836bea2Seric 13449836bea2Seric parentbody(fp, m, e) 13459836bea2Seric FILE *fp; 13469836bea2Seric MAILER *m; 13479836bea2Seric register ENVELOPE *e; 13489836bea2Seric { 13499836bea2Seric putbody(fp, m, e->e_parent); 13509836bea2Seric } 13519836bea2Seric /* 135225a99e2eSeric ** MAILFILE -- Send a message to a file. 135325a99e2eSeric ** 1354f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 1355f129ec7dSeric ** bits, sendmail will try to become the owner of that file 1356f129ec7dSeric ** rather than the real user. Obviously, this only works if 1357f129ec7dSeric ** sendmail runs as root. 1358f129ec7dSeric ** 1359588cad61Seric ** This could be done as a subordinate mailer, except that it 1360588cad61Seric ** is used implicitly to save messages in ~/dead.letter. We 1361588cad61Seric ** view this as being sufficiently important as to include it 1362588cad61Seric ** here. For example, if the system is dying, we shouldn't have 1363588cad61Seric ** to create another process plus some pipes to save the message. 1364588cad61Seric ** 136525a99e2eSeric ** Parameters: 136625a99e2eSeric ** filename -- the name of the file to send to. 13676259796dSeric ** ctladdr -- the controlling address header -- includes 13686259796dSeric ** the userid/groupid to be when sending. 136925a99e2eSeric ** 137025a99e2eSeric ** Returns: 137125a99e2eSeric ** The exit code associated with the operation. 137225a99e2eSeric ** 137325a99e2eSeric ** Side Effects: 137425a99e2eSeric ** none. 137525a99e2eSeric */ 137625a99e2eSeric 1377b31e7f2bSeric mailfile(filename, ctladdr, e) 137825a99e2eSeric char *filename; 13796259796dSeric ADDRESS *ctladdr; 1380b31e7f2bSeric register ENVELOPE *e; 138125a99e2eSeric { 138225a99e2eSeric register FILE *f; 138332d19d43Seric register int pid; 138415d084d5Seric int mode; 138525a99e2eSeric 138632d19d43Seric /* 138732d19d43Seric ** Fork so we can change permissions here. 138832d19d43Seric ** Note that we MUST use fork, not vfork, because of 138932d19d43Seric ** the complications of calling subroutines, etc. 139032d19d43Seric */ 139132d19d43Seric 139232d19d43Seric DOFORK(fork); 139332d19d43Seric 139432d19d43Seric if (pid < 0) 139532d19d43Seric return (EX_OSERR); 139632d19d43Seric else if (pid == 0) 139732d19d43Seric { 139832d19d43Seric /* child -- actually write to file */ 1399f129ec7dSeric struct stat stb; 1400f129ec7dSeric 14010984da9fSeric (void) signal(SIGINT, SIG_DFL); 14020984da9fSeric (void) signal(SIGHUP, SIG_DFL); 14030984da9fSeric (void) signal(SIGTERM, SIG_DFL); 14043462ad9eSeric (void) umask(OldUmask); 140595f16dc0Seric 1406f129ec7dSeric if (stat(filename, &stb) < 0) 1407e6e1265fSeric stb.st_mode = 0666; 140815d084d5Seric mode = stb.st_mode; 140995f16dc0Seric 141095f16dc0Seric /* limit the errors to those actually caused in the child */ 141195f16dc0Seric errno = 0; 141295f16dc0Seric ExitStat = EX_OK; 141395f16dc0Seric 1414f129ec7dSeric if (bitset(0111, stb.st_mode)) 1415f129ec7dSeric exit(EX_CANTCREAT); 141603827b5fSeric if (ctladdr == NULL) 14178f9146b0Srick ctladdr = &e->e_from; 141815d084d5Seric else 141915d084d5Seric { 142015d084d5Seric /* ignore setuid and setgid bits */ 142115d084d5Seric mode &= ~(S_ISGID|S_ISUID); 142215d084d5Seric } 142315d084d5Seric 14248f9146b0Srick /* we have to open the dfile BEFORE setuid */ 14258f9146b0Srick if (e->e_dfp == NULL && e->e_df != NULL) 14268f9146b0Srick { 14278f9146b0Srick e->e_dfp = fopen(e->e_df, "r"); 142895f16dc0Seric if (e->e_dfp == NULL) 142995f16dc0Seric { 14308f9146b0Srick syserr("mailfile: Cannot open %s for %s from %s", 14318f9146b0Srick e->e_df, e->e_to, e->e_from); 14328f9146b0Srick } 14338f9146b0Srick } 14348f9146b0Srick 143515d084d5Seric if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 1436e36b99e2Seric { 143795f16dc0Seric if (ctladdr->q_uid == 0) 143895f16dc0Seric { 1439e36b99e2Seric (void) setgid(DefGid); 1440898a126bSbostic (void) initgroups(DefUser, DefGid); 144195f16dc0Seric } 144295f16dc0Seric else 144395f16dc0Seric { 14446259796dSeric (void) setgid(ctladdr->q_gid); 1445898a126bSbostic (void) initgroups(ctladdr->q_ruser ? 1446898a126bSbostic ctladdr->q_ruser : ctladdr->q_user, 1447898a126bSbostic ctladdr->q_gid); 1448898a126bSbostic } 1449e36b99e2Seric } 145015d084d5Seric if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 1451e36b99e2Seric { 1452e36b99e2Seric if (ctladdr->q_uid == 0) 1453e36b99e2Seric (void) setuid(DefUid); 1454e36b99e2Seric else 14556259796dSeric (void) setuid(ctladdr->q_uid); 1456e36b99e2Seric } 145795f16dc0Seric FileName = filename; 145895f16dc0Seric LineNumber = 0; 145927628d59Seric f = dfopen(filename, "a"); 146025a99e2eSeric if (f == NULL) 146195f16dc0Seric { 146208b25121Seric message("554 cannot open"); 146332d19d43Seric exit(EX_CANTCREAT); 146495f16dc0Seric } 146525a99e2eSeric 1466b31e7f2bSeric putfromline(f, ProgMailer, e); 1467b843d759Seric (*e->e_puthdr)(f, ProgMailer, e); 146877b52738Seric putline("\n", f, ProgMailer); 1469b843d759Seric (*e->e_putbody)(f, ProgMailer, e); 147077b52738Seric putline("\n", f, ProgMailer); 147195f16dc0Seric if (ferror(f)) 147295f16dc0Seric { 147308b25121Seric message("451 I/O error"); 147495f16dc0Seric setstat(EX_IOERR); 147595f16dc0Seric } 1476db8841e9Seric (void) fclose(f); 147732d19d43Seric (void) fflush(stdout); 1478e36b99e2Seric 147927628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 1480c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 148195f16dc0Seric exit(ExitStat); 148213bbc08cSeric /*NOTREACHED*/ 148332d19d43Seric } 148432d19d43Seric else 148532d19d43Seric { 148632d19d43Seric /* parent -- wait for exit status */ 1487588cad61Seric int st; 148832d19d43Seric 1489588cad61Seric st = waitfor(pid); 1490588cad61Seric if ((st & 0377) != 0) 1491588cad61Seric return (EX_UNAVAILABLE); 1492588cad61Seric else 1493588cad61Seric return ((st >> 8) & 0377); 14948f9146b0Srick /*NOTREACHED*/ 149532d19d43Seric } 149625a99e2eSeric } 1497ea4dc939Seric /* 1498ea4dc939Seric ** SENDALL -- actually send all the messages. 1499ea4dc939Seric ** 1500ea4dc939Seric ** Parameters: 15010c52a0b3Seric ** e -- the envelope to send. 15027b95031aSeric ** mode -- the delivery mode to use. If SM_DEFAULT, use 15037b95031aSeric ** the current SendMode. 1504ea4dc939Seric ** 1505ea4dc939Seric ** Returns: 1506ea4dc939Seric ** none. 1507ea4dc939Seric ** 1508ea4dc939Seric ** Side Effects: 1509ea4dc939Seric ** Scans the send lists and sends everything it finds. 15100c52a0b3Seric ** Delivers any appropriate error messages. 1511276723a8Seric ** If we are running in a non-interactive mode, takes the 1512276723a8Seric ** appropriate action. 1513ea4dc939Seric */ 1514ea4dc939Seric 1515276723a8Seric sendall(e, mode) 15160c52a0b3Seric ENVELOPE *e; 1517276723a8Seric char mode; 1518ea4dc939Seric { 1519e77e673fSeric register ADDRESS *q; 152014a8ed7aSeric bool oldverbose; 1521276723a8Seric int pid; 15229836bea2Seric char *owner; 15239836bea2Seric int otherowners; 15249836bea2Seric ENVELOPE *splitenv = NULL; 15251c265a00Seric # ifdef LOCKF 15261c265a00Seric struct flock lfd; 15271c265a00Seric # endif 1528ea4dc939Seric 15297b95031aSeric /* determine actual delivery mode */ 15307b95031aSeric if (mode == SM_DEFAULT) 15317b95031aSeric { 15325f73204aSeric extern bool shouldqueue(); 15337b95031aSeric 15347b95031aSeric mode = SendMode; 15353f324333Seric if (mode != SM_VERIFY && 15363f324333Seric shouldqueue(e->e_msgpriority, e->e_ctime)) 15373f324333Seric mode = SM_QUEUE; 15387b95031aSeric } 15397b95031aSeric 1540df864a8fSeric if (tTd(13, 1)) 1541772e6e50Seric { 1542276723a8Seric printf("\nSENDALL: mode %c, sendqueue:\n", mode); 15430c52a0b3Seric printaddr(e->e_sendqueue, TRUE); 1544772e6e50Seric } 1545ea4dc939Seric 15460c52a0b3Seric /* 1547276723a8Seric ** Do any preprocessing necessary for the mode we are running. 1548588cad61Seric ** Check to make sure the hop count is reasonable. 1549588cad61Seric ** Delete sends to the sender in mailing lists. 1550276723a8Seric */ 1551276723a8Seric 1552588cad61Seric CurEnv = e; 1553276723a8Seric 15541d57450bSeric if (e->e_hopcount > MaxHopCount) 1555276723a8Seric { 15568f9146b0Srick errno = 0; 155708b25121Seric syserr("554 too many hops %d (%d max): from %s, to %s", 1558655518ecSeric e->e_hopcount, MaxHopCount, e->e_from.q_paddr, 1559655518ecSeric e->e_sendqueue->q_paddr); 1560588cad61Seric return; 1561588cad61Seric } 1562588cad61Seric 1563588cad61Seric if (!MeToo) 1564276723a8Seric { 1565f3d6c55cSeric extern ADDRESS *recipient(); 1566f3d6c55cSeric 1567588cad61Seric e->e_from.q_flags |= QDONTSEND; 156875f1ade9Seric if (tTd(13, 5)) 156975f1ade9Seric { 157075f1ade9Seric printf("sendall: QDONTSEND "); 157175f1ade9Seric printaddr(&e->e_from, FALSE); 157275f1ade9Seric } 1573b843d759Seric (void) recipient(&e->e_from, &e->e_sendqueue, e); 1574276723a8Seric } 1575588cad61Seric 15769836bea2Seric /* 15779836bea2Seric ** Handle alias owners. 15789836bea2Seric ** 15799836bea2Seric ** We scan up the q_alias chain looking for owners. 15809836bea2Seric ** We discard owners that are the same as the return path. 15819836bea2Seric */ 15829836bea2Seric 15839836bea2Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 15849836bea2Seric { 15859836bea2Seric register struct address *a; 15869836bea2Seric 15879836bea2Seric for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias) 15889836bea2Seric continue; 15899836bea2Seric if (a != NULL) 15909836bea2Seric q->q_owner = a->q_owner; 15919836bea2Seric 15929836bea2Seric if (q->q_owner != NULL && !bitset(QDONTSEND, q->q_flags) && 15939836bea2Seric strcmp(q->q_owner, e->e_returnpath) == 0) 15949836bea2Seric q->q_owner = NULL; 15959836bea2Seric } 15969836bea2Seric 15979836bea2Seric owner = ""; 15989836bea2Seric otherowners = 1; 15999836bea2Seric while (owner != NULL && otherowners > 0) 16009836bea2Seric { 16019836bea2Seric owner = NULL; 16029836bea2Seric otherowners = 0; 16039836bea2Seric 16049836bea2Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 16059836bea2Seric { 16069836bea2Seric if (bitset(QDONTSEND, q->q_flags)) 16079836bea2Seric continue; 16089836bea2Seric 16099836bea2Seric if (q->q_owner != NULL) 16109836bea2Seric { 16119836bea2Seric if (owner == NULL) 16129836bea2Seric owner = q->q_owner; 16139836bea2Seric else if (owner != q->q_owner) 16149836bea2Seric { 16159836bea2Seric if (strcmp(owner, q->q_owner) == 0) 16169836bea2Seric { 16179836bea2Seric /* make future comparisons cheap */ 16189836bea2Seric q->q_owner = owner; 16199836bea2Seric } 16209836bea2Seric else 16219836bea2Seric { 16229836bea2Seric otherowners++; 16239836bea2Seric } 16249836bea2Seric owner = q->q_owner; 16259836bea2Seric } 16269836bea2Seric } 16279836bea2Seric else 16289836bea2Seric { 16299836bea2Seric otherowners++; 16309836bea2Seric } 16319836bea2Seric } 16329836bea2Seric 16339836bea2Seric if (owner != NULL && otherowners > 0) 16349836bea2Seric { 16359836bea2Seric register ENVELOPE *ee; 16369836bea2Seric extern HDR *copyheader(); 16379836bea2Seric extern ADDRESS *copyqueue(); 16389836bea2Seric 16399836bea2Seric ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE)); 16409836bea2Seric STRUCTCOPY(*e, *ee); 16419836bea2Seric ee->e_id = NULL; 16429836bea2Seric ee->e_parent = e; 16439836bea2Seric ee->e_header = copyheader(e->e_header); 16449836bea2Seric ee->e_sendqueue = copyqueue(e->e_sendqueue); 16459836bea2Seric ee->e_errorqueue = copyqueue(e->e_errorqueue); 16469836bea2Seric ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE); 16479836bea2Seric ee->e_returnpath = owner; 16489836bea2Seric ee->e_putbody = parentbody; 16499836bea2Seric ee->e_sibling = splitenv; 16509836bea2Seric splitenv = ee; 16519836bea2Seric 16529836bea2Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 16539836bea2Seric if (q->q_owner == owner) 16549836bea2Seric q->q_flags |= QDONTSEND; 16559836bea2Seric for (q = ee->e_sendqueue; q != NULL; q = q->q_next) 16569836bea2Seric if (q->q_owner != owner) 16579836bea2Seric q->q_flags |= QDONTSEND; 16589836bea2Seric 16599836bea2Seric if (e->e_df != NULL && mode != SM_VERIFY) 16609836bea2Seric { 16619836bea2Seric ee->e_dfp = NULL; 1662239b3abaSeric ee->e_df = newstr(queuename(ee, 'd')); 16639836bea2Seric if (link(e->e_df, ee->e_df) < 0) 16649836bea2Seric { 16659836bea2Seric syserr("sendall: link(%s, %s)", 16669836bea2Seric e->e_df, ee->e_df); 16679836bea2Seric } 16689836bea2Seric } 16699836bea2Seric } 16709836bea2Seric } 16719836bea2Seric 16729836bea2Seric if (owner != NULL) 16739836bea2Seric e->e_returnpath = owner; 16749836bea2Seric 1675588cad61Seric # ifdef QUEUE 1676b254bcb6Seric if ((mode == SM_QUEUE || mode == SM_FORK || 1677b254bcb6Seric (mode != SM_VERIFY && SuperSafe)) && 1678b254bcb6Seric !bitset(EF_INQUEUE, e->e_flags)) 1679e020b127Seric queueup(e, TRUE, mode == SM_QUEUE); 16806c2c3107Seric #endif /* QUEUE */ 1681276723a8Seric 16829836bea2Seric if (splitenv != NULL) 16839836bea2Seric { 16849836bea2Seric if (tTd(13, 1)) 16859836bea2Seric { 16869836bea2Seric printf("\nsendall: Split queue; remaining queue:\n"); 16879836bea2Seric printaddr(e->e_sendqueue, TRUE); 16889836bea2Seric } 16899836bea2Seric 16909836bea2Seric while (splitenv != NULL) 16919836bea2Seric { 16929836bea2Seric sendall(splitenv, mode); 16939836bea2Seric splitenv = splitenv->e_sibling; 16949836bea2Seric } 16959836bea2Seric 16969836bea2Seric CurEnv = e; 16979836bea2Seric } 16989836bea2Seric 1699276723a8Seric oldverbose = Verbose; 1700276723a8Seric switch (mode) 1701276723a8Seric { 1702276723a8Seric case SM_VERIFY: 1703276723a8Seric Verbose = TRUE; 1704276723a8Seric break; 1705276723a8Seric 1706276723a8Seric case SM_QUEUE: 1707c7f5410dSeric queueonly: 1708b254bcb6Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 1709276723a8Seric return; 1710276723a8Seric 1711276723a8Seric case SM_FORK: 17129a6a5f55Seric if (e->e_xfp != NULL) 17139a6a5f55Seric (void) fflush(e->e_xfp); 1714c7f5410dSeric 1715c7f5410dSeric # ifdef LOCKF 1716c7f5410dSeric /* 1717c7f5410dSeric ** Since lockf has the interesting semantic that the 17181c265a00Seric ** lock is lost when we fork, we have to risk losing 17191c265a00Seric ** the lock here by closing before the fork, and then 17201c265a00Seric ** trying to get it back in the child. 1721c7f5410dSeric */ 1722c7f5410dSeric 1723e020b127Seric if (e->e_lockfp != NULL) 1724c7f5410dSeric { 1725e020b127Seric (void) fclose(e->e_lockfp); 1726e020b127Seric e->e_lockfp = NULL; 1727c7f5410dSeric } 1728c7f5410dSeric # endif /* LOCKF */ 1729c7f5410dSeric 1730276723a8Seric pid = fork(); 1731276723a8Seric if (pid < 0) 1732276723a8Seric { 1733c7f5410dSeric goto queueonly; 1734276723a8Seric } 1735276723a8Seric else if (pid > 0) 1736a6fce3d8Seric { 1737a6fce3d8Seric /* be sure we leave the temp files to our child */ 1738b254bcb6Seric e->e_id = e->e_df = NULL; 1739c7f5410dSeric # ifndef LOCKF 1740e020b127Seric if (e->e_lockfp != NULL) 174175f1ade9Seric { 1742e020b127Seric (void) fclose(e->e_lockfp); 174375f1ade9Seric e->e_lockfp = NULL; 174475f1ade9Seric } 1745c7f5410dSeric # endif 174675f1ade9Seric 174775f1ade9Seric /* close any random open files in the envelope */ 174875f1ade9Seric if (e->e_dfp != NULL) 174975f1ade9Seric { 175075f1ade9Seric (void) fclose(e->e_dfp); 175175f1ade9Seric e->e_dfp = NULL; 175275f1ade9Seric } 175375f1ade9Seric if (e->e_xfp != NULL) 175475f1ade9Seric { 175575f1ade9Seric (void) fclose(e->e_xfp); 175675f1ade9Seric e->e_xfp = NULL; 175775f1ade9Seric } 1758276723a8Seric return; 1759a6fce3d8Seric } 1760276723a8Seric 1761276723a8Seric /* double fork to avoid zombies */ 1762276723a8Seric if (fork() > 0) 1763276723a8Seric exit(EX_OK); 1764276723a8Seric 1765a6fce3d8Seric /* be sure we are immune from the terminal */ 1766769e215aSeric disconnect(FALSE); 1767a6fce3d8Seric 1768e6720d51Seric # ifdef LOCKF 1769e6720d51Seric /* 1770c7f5410dSeric ** Now try to get our lock back. 1771e6720d51Seric */ 1772e6720d51Seric 17731c265a00Seric lfd.l_type = F_WRLCK; 17741c265a00Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 1775e020b127Seric e->e_lockfp = fopen(queuename(e, 'q'), "r+"); 1776e020b127Seric if (e->e_lockfp == NULL || 17771c265a00Seric fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0) 1778e6720d51Seric { 1779e6720d51Seric /* oops.... lost it */ 1780e6720d51Seric # ifdef LOG 17812f624c86Seric if (LogLevel > 29) 1782c7f5410dSeric syslog(LOG_NOTICE, "%s: lost lock: %m", 1783b31e7f2bSeric e->e_id); 1784e6720d51Seric # endif /* LOG */ 1785e6720d51Seric exit(EX_OK); 1786e6720d51Seric } 1787e6720d51Seric # endif /* LOCKF */ 1788e6720d51Seric 17891caa9a59Seric /* 17901caa9a59Seric ** Close any cached connections. 17911caa9a59Seric ** 17921caa9a59Seric ** We don't send the QUIT protocol because the parent 17931caa9a59Seric ** still knows about the connection. 17941caa9a59Seric ** 17951caa9a59Seric ** This should only happen when delivering an error 17961caa9a59Seric ** message. 17971caa9a59Seric */ 17981caa9a59Seric 17991caa9a59Seric mci_flush(FALSE, NULL); 18001caa9a59Seric 1801276723a8Seric break; 1802276723a8Seric } 1803276723a8Seric 1804276723a8Seric /* 18050c52a0b3Seric ** Run through the list and send everything. 18060c52a0b3Seric */ 18070c52a0b3Seric 1808655518ecSeric e->e_nsent = 0; 18090c52a0b3Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1810ea4dc939Seric { 1811276723a8Seric if (mode == SM_VERIFY) 1812ea4dc939Seric { 1813a6fce3d8Seric e->e_to = q->q_paddr; 1814e77e673fSeric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 181508b25121Seric message("deliverable"); 1816ea4dc939Seric } 1817dde5acadSeric else if (!bitset(QDONTSEND, q->q_flags)) 1818dde5acadSeric { 1819de1179f5Seric # ifdef QUEUE 1820dde5acadSeric /* 1821dde5acadSeric ** Checkpoint the send list every few addresses 1822dde5acadSeric */ 1823dde5acadSeric 1824655518ecSeric if (e->e_nsent >= CheckpointInterval) 1825dde5acadSeric { 1826e020b127Seric queueup(e, TRUE, FALSE); 1827655518ecSeric e->e_nsent = 0; 1828dde5acadSeric } 1829de1179f5Seric # endif /* QUEUE */ 1830655518ecSeric (void) deliver(e, q); 1831dde5acadSeric } 1832ea4dc939Seric } 183314a8ed7aSeric Verbose = oldverbose; 18340c52a0b3Seric 18350c52a0b3Seric /* 18360c52a0b3Seric ** Now run through and check for errors. 18370c52a0b3Seric */ 18380c52a0b3Seric 1839e020b127Seric if (mode == SM_VERIFY) 18400c52a0b3Seric return; 18410c52a0b3Seric 18420c52a0b3Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 18430c52a0b3Seric { 1844df864a8fSeric if (tTd(13, 3)) 1845df864a8fSeric { 1846df864a8fSeric printf("Checking "); 1847df864a8fSeric printaddr(q, FALSE); 1848df864a8fSeric } 1849df864a8fSeric 1850b254bcb6Seric /* only send errors if the message failed */ 1851b254bcb6Seric if (!bitset(QBADADDR, q->q_flags)) 1852b254bcb6Seric continue; 18530c52a0b3Seric 1854b8043ddbSeric if (q->q_owner == NULL && strcmp(e->e_from.q_paddr, "<>") != 0) 18559836bea2Seric (void) sendtolist(e->e_from.q_paddr, NULL, 18561caa9a59Seric &e->e_errorqueue, e); 18570c52a0b3Seric } 1858276723a8Seric 1859276723a8Seric if (mode == SM_FORK) 1860276723a8Seric finis(); 18610c52a0b3Seric } 1862e103b48fSeric /* 1863e103b48fSeric ** HOSTSIGNATURE -- return the "signature" for a host. 1864e103b48fSeric ** 1865e103b48fSeric ** The signature describes how we are going to send this -- it 1866e103b48fSeric ** can be just the hostname (for non-Internet hosts) or can be 1867e103b48fSeric ** an ordered list of MX hosts. 1868e103b48fSeric ** 1869e103b48fSeric ** Parameters: 1870e103b48fSeric ** m -- the mailer describing this host. 1871e103b48fSeric ** host -- the host name. 1872e103b48fSeric ** e -- the current envelope. 1873e103b48fSeric ** 1874e103b48fSeric ** Returns: 1875e103b48fSeric ** The signature for this host. 1876e103b48fSeric ** 1877e103b48fSeric ** Side Effects: 1878e103b48fSeric ** Can tweak the symbol table. 1879e103b48fSeric */ 1880e103b48fSeric 1881e103b48fSeric char * 1882e103b48fSeric hostsignature(m, host, e) 1883e103b48fSeric register MAILER *m; 1884e103b48fSeric char *host; 1885e103b48fSeric ENVELOPE *e; 1886e103b48fSeric { 1887e103b48fSeric register char *p; 1888e103b48fSeric register STAB *s; 1889e103b48fSeric int i; 1890e103b48fSeric int len; 1891e103b48fSeric #ifdef NAMED_BIND 1892e103b48fSeric int nmx; 1893e103b48fSeric auto int rcode; 1894e103b48fSeric char *mxhosts[MAXMXHOSTS + 1]; 1895e103b48fSeric static char myhostbuf[MAXNAME]; 1896e103b48fSeric #endif 1897e103b48fSeric 1898e103b48fSeric /* 1899e103b48fSeric ** Check to see if this uses IPC -- if not, it can't have MX records. 1900e103b48fSeric */ 1901e103b48fSeric 1902e103b48fSeric p = m->m_mailer; 1903e103b48fSeric if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 1904e103b48fSeric { 1905e103b48fSeric /* just an ordinary mailer */ 1906e103b48fSeric return host; 1907e103b48fSeric } 1908e103b48fSeric 1909e103b48fSeric /* 1910e103b48fSeric ** If it is a numeric address, just return it. 1911e103b48fSeric */ 1912e103b48fSeric 1913e103b48fSeric if (host[0] == '[') 1914e103b48fSeric return host; 1915e103b48fSeric 1916e103b48fSeric /* 1917e103b48fSeric ** Look it up in the symbol table. 1918e103b48fSeric */ 1919e103b48fSeric 1920e103b48fSeric s = stab(host, ST_HOSTSIG, ST_ENTER); 1921e103b48fSeric if (s->s_hostsig != NULL) 1922e103b48fSeric return s->s_hostsig; 1923e103b48fSeric 1924e103b48fSeric /* 1925e103b48fSeric ** Not already there -- create a signature. 1926e103b48fSeric */ 1927e103b48fSeric 1928e103b48fSeric #ifdef NAMED_BIND 1929e103b48fSeric if (myhostbuf[0] == '\0') 19302bc47524Seric expand("\201j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e); 1931e103b48fSeric 1932e103b48fSeric nmx = getmxrr(host, mxhosts, myhostbuf, &rcode); 1933e103b48fSeric if (nmx <= 0) 1934e103b48fSeric { 1935e103b48fSeric register MCI *mci; 1936e103b48fSeric extern int errno; 1937e103b48fSeric extern MCI *mci_get(); 1938e103b48fSeric 1939e103b48fSeric /* update the connection info for this host */ 1940e103b48fSeric mci = mci_get(host, m); 1941e103b48fSeric mci->mci_exitstat = rcode; 1942e103b48fSeric mci->mci_errno = errno; 1943e103b48fSeric 1944e103b48fSeric /* and return the original host name as the signature */ 1945e103b48fSeric s->s_hostsig = host; 1946e103b48fSeric return host; 1947e103b48fSeric } 1948e103b48fSeric 1949e103b48fSeric len = 0; 1950e103b48fSeric for (i = 0; i < nmx; i++) 1951e103b48fSeric { 1952e103b48fSeric len += strlen(mxhosts[i]) + 1; 1953e103b48fSeric } 1954e103b48fSeric s->s_hostsig = p = xalloc(len); 1955e103b48fSeric for (i = 0; i < nmx; i++) 1956e103b48fSeric { 1957e103b48fSeric if (i != 0) 1958e103b48fSeric *p++ = ':'; 1959e103b48fSeric strcpy(p, mxhosts[i]); 1960e103b48fSeric p += strlen(p); 1961e103b48fSeric } 1962e103b48fSeric makelower(s->s_hostsig); 1963e103b48fSeric #else 1964e103b48fSeric /* not using BIND -- the signature is just the host name */ 1965e103b48fSeric s->s_hostsig = host; 1966e103b48fSeric #endif 1967e103b48fSeric if (tTd(17, 1)) 1968e103b48fSeric printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 1969e103b48fSeric return s->s_hostsig; 1970e103b48fSeric } 1971