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*b31e7f2bSeric static char sccsid[] = "@(#)deliver.c 5.57 (Berkeley) 07/11/92"; 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 2012eec323Sbostic #include <sys/param.h> 21912a731aSbostic #include <arpa/nameser.h> 22912a731aSbostic #include <resolv.h> 23134746fbSeric #endif 2425a99e2eSeric 2525a99e2eSeric /* 2613bbc08cSeric ** DELIVER -- Deliver a message to a list of addresses. 2713bbc08cSeric ** 2813bbc08cSeric ** This routine delivers to everyone on the same host as the 2913bbc08cSeric ** user on the head of the list. It is clever about mailers 3013bbc08cSeric ** that don't handle multiple users. It is NOT guaranteed 3113bbc08cSeric ** that it will deliver to all these addresses however -- so 3213bbc08cSeric ** deliver should be called once for each address on the 3313bbc08cSeric ** list. 3425a99e2eSeric ** 3525a99e2eSeric ** Parameters: 36588cad61Seric ** e -- the envelope to deliver. 37c77d1c25Seric ** firstto -- head of the address list to deliver to. 3825a99e2eSeric ** 3925a99e2eSeric ** Returns: 4025a99e2eSeric ** zero -- successfully delivered. 4125a99e2eSeric ** else -- some failure, see ExitStat for more info. 4225a99e2eSeric ** 4325a99e2eSeric ** Side Effects: 4425a99e2eSeric ** The standard input is passed off to someone. 4525a99e2eSeric */ 4625a99e2eSeric 47588cad61Seric deliver(e, firstto) 48588cad61Seric register ENVELOPE *e; 49c77d1c25Seric ADDRESS *firstto; 5025a99e2eSeric { 5178442df3Seric char *host; /* host being sent to */ 5278442df3Seric char *user; /* user being sent to */ 5325a99e2eSeric char **pvp; 545dfc646bSeric register char **mvp; 5525a99e2eSeric register char *p; 56588cad61Seric register MAILER *m; /* mailer for this recipient */ 576259796dSeric ADDRESS *ctladdr; 58*b31e7f2bSeric register MCI *mci; 59c77d1c25Seric register ADDRESS *to = firstto; 60c579ef51Seric bool clever = FALSE; /* running user smtp to this mailer */ 61772e6e50Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 62911693bfSbostic int rcode; /* response code */ 63c23ed322Seric char *from; /* pointer to from person */ 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 */ 69ee6bf8dfSeric extern bool checkcompat(); 70ee6bf8dfSeric extern ADDRESS *getctladdr(); 71ee6bf8dfSeric extern char *remotename(); 72*b31e7f2bSeric extern MCI *openmailer(); 7325a99e2eSeric 7435490626Seric errno = 0; 75da2935e1Seric if (bitset(QDONTSEND, to->q_flags)) 765dfc646bSeric return (0); 7725a99e2eSeric 78134746fbSeric #ifdef NAMED_BIND 79912a731aSbostic /* unless interactive, try twice, over a minute */ 80912a731aSbostic if (OpMode == MD_DAEMON || OpMode == MD_SMTP) { 81912a731aSbostic _res.retrans = 30; 82912a731aSbostic _res.retry = 2; 83912a731aSbostic } 84d4bd8f0eSbostic #endif 85912a731aSbostic 8651552439Seric m = to->q_mailer; 8751552439Seric host = to->q_host; 8851552439Seric 896ef48975Seric if (tTd(10, 1)) 905dfc646bSeric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 9151552439Seric m->m_mno, host, to->q_user); 92f3dbc832Seric 93f3dbc832Seric /* 94f3dbc832Seric ** If this mailer is expensive, and if we don't want to make 95f3dbc832Seric ** connections now, just mark these addresses and return. 96f3dbc832Seric ** This is useful if we want to batch connections to 97f3dbc832Seric ** reduce load. This will cause the messages to be 98f3dbc832Seric ** queued up, and a daemon will come along to send the 99f3dbc832Seric ** messages later. 100f3dbc832Seric ** This should be on a per-mailer basis. 101f3dbc832Seric */ 102f3dbc832Seric 10357fc6f17Seric if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) && 104317680d6Seric !Verbose) 105f3dbc832Seric { 106f3dbc832Seric for (; to != NULL; to = to->q_next) 107f4560e80Seric { 108f4560e80Seric if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m) 109f4560e80Seric continue; 110f3dbc832Seric to->q_flags |= QQUEUEUP|QDONTSEND; 111588cad61Seric e->e_to = to->q_paddr; 112eb238f8cSeric message(Arpa_Info, "queued"); 113eb238f8cSeric if (LogLevel > 4) 114*b31e7f2bSeric logdelivery("queued", e); 115f4560e80Seric } 116588cad61Seric e->e_to = NULL; 117f3dbc832Seric return (0); 118f3dbc832Seric } 119f3dbc832Seric 12025a99e2eSeric /* 1215dfc646bSeric ** Do initial argv setup. 1225dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 1235dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 1245dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 1255dfc646bSeric ** code does not check for 'pv' overflow; this places a 1265dfc646bSeric ** manifest lower limit of 4 for MAXPV. 1273bea8136Seric ** The from address rewrite is expected to make 1283bea8136Seric ** the address relative to the other end. 1295dfc646bSeric */ 1305dfc646bSeric 13178442df3Seric /* rewrite from address, using rewriting rules */ 132c23ed322Seric (void) strcpy(rpathbuf, remotename(e->e_returnpath, m, TRUE, TRUE)); 133c23ed322Seric if (e->e_returnpath == e->e_sender) 134c23ed322Seric { 135c23ed322Seric from = rpathbuf; 136c23ed322Seric } 137c23ed322Seric else 138c23ed322Seric { 139c23ed322Seric (void) strcpy(tfrombuf, remotename(e->e_sender, m, TRUE, TRUE)); 140c23ed322Seric from = tfrombuf; 141c23ed322Seric } 14278442df3Seric 143c23ed322Seric define('f', e->e_returnpath, e); /* raw return path */ 144c23ed322Seric define('<', rpathbuf, e); /* translated return path */ 145c23ed322Seric define('g', from, e); /* translated sender */ 146588cad61Seric define('h', host, e); /* to host */ 1475dfc646bSeric Errors = 0; 1485dfc646bSeric pvp = pv; 1495dfc646bSeric *pvp++ = m->m_argv[0]; 1505dfc646bSeric 1515dfc646bSeric /* insert -f or -r flag as appropriate */ 15257fc6f17Seric if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 1535dfc646bSeric { 15457fc6f17Seric if (bitnset(M_FOPT, m->m_flags)) 1555dfc646bSeric *pvp++ = "-f"; 1565dfc646bSeric else 1575dfc646bSeric *pvp++ = "-r"; 158c23ed322Seric *pvp++ = newstr(rpathbuf); 1595dfc646bSeric } 1605dfc646bSeric 1615dfc646bSeric /* 1625dfc646bSeric ** Append the other fixed parts of the argv. These run 1635dfc646bSeric ** up to the first entry containing "$u". There can only 1645dfc646bSeric ** be one of these, and there are only a few more slots 1655dfc646bSeric ** in the pv after it. 1665dfc646bSeric */ 1675dfc646bSeric 1685dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 1695dfc646bSeric { 1709b6c17a6Seric while ((p = index(p, '\001')) != NULL) 1715dfc646bSeric if (*++p == 'u') 1725dfc646bSeric break; 1735dfc646bSeric if (p != NULL) 1745dfc646bSeric break; 1755dfc646bSeric 1765dfc646bSeric /* this entry is safe -- go ahead and process it */ 177588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 1785dfc646bSeric *pvp++ = newstr(buf); 1795dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 1805dfc646bSeric { 1815dfc646bSeric syserr("Too many parameters to %s before $u", pv[0]); 1825dfc646bSeric return (-1); 1835dfc646bSeric } 1845dfc646bSeric } 185c579ef51Seric 18633db8731Seric /* 18733db8731Seric ** If we have no substitution for the user name in the argument 18833db8731Seric ** list, we know that we must supply the names otherwise -- and 18933db8731Seric ** SMTP is the answer!! 19033db8731Seric */ 19133db8731Seric 1925dfc646bSeric if (*mvp == NULL) 193c579ef51Seric { 194c579ef51Seric /* running SMTP */ 1952c7e1b8dSeric # ifdef SMTP 196c579ef51Seric clever = TRUE; 197c579ef51Seric *pvp = NULL; 1982c7e1b8dSeric # else SMTP 19933db8731Seric /* oops! we don't implement SMTP */ 2002c7e1b8dSeric syserr("SMTP style mailer"); 2012c7e1b8dSeric return (EX_SOFTWARE); 2022c7e1b8dSeric # endif SMTP 203c579ef51Seric } 2045dfc646bSeric 2055dfc646bSeric /* 2065dfc646bSeric ** At this point *mvp points to the argument with $u. We 2075dfc646bSeric ** run through our address list and append all the addresses 2085dfc646bSeric ** we can. If we run out of space, do not fret! We can 2095dfc646bSeric ** always send another copy later. 2105dfc646bSeric */ 2115dfc646bSeric 2125dfc646bSeric tobuf[0] = '\0'; 213588cad61Seric e->e_to = tobuf; 2146259796dSeric ctladdr = NULL; 2155dfc646bSeric for (; to != NULL; to = to->q_next) 2165dfc646bSeric { 2175dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 21857fc6f17Seric if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 2195dfc646bSeric break; 2205dfc646bSeric 2215dfc646bSeric /* if already sent or not for this host, don't send */ 222da2935e1Seric if (bitset(QDONTSEND, to->q_flags) || 223da2935e1Seric strcmp(to->q_host, host) != 0 || 224da2935e1Seric to->q_mailer != firstto->q_mailer) 2255dfc646bSeric continue; 2266259796dSeric 2274b22ea87Seric /* avoid overflowing tobuf */ 228aa50a568Sbostic if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 2294b22ea87Seric break; 2304b22ea87Seric 2316ef48975Seric if (tTd(10, 1)) 232772e6e50Seric { 233772e6e50Seric printf("\nsend to "); 234772e6e50Seric printaddr(to, FALSE); 235772e6e50Seric } 236772e6e50Seric 2376259796dSeric /* compute effective uid/gid when sending */ 2387da1035fSeric if (to->q_mailer == ProgMailer) 2396259796dSeric ctladdr = getctladdr(to); 2406259796dSeric 2415dfc646bSeric user = to->q_user; 242588cad61Seric e->e_to = to->q_paddr; 2435dfc646bSeric to->q_flags |= QDONTSEND; 2445dfc646bSeric 2455dfc646bSeric /* 2465dfc646bSeric ** Check to see that these people are allowed to 2475dfc646bSeric ** talk to each other. 2482a6e0786Seric */ 2492a6e0786Seric 25069582d2fSeric if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 25169582d2fSeric { 25269582d2fSeric NoReturn = TRUE; 253672bec4aSeric usrerr("Message is too large; %ld bytes max", m->m_maxsize); 25469582d2fSeric giveresponse(EX_UNAVAILABLE, m, e); 25569582d2fSeric continue; 25669582d2fSeric } 2572a6e0786Seric if (!checkcompat(to)) 2585dfc646bSeric { 259198d9be0Seric giveresponse(EX_UNAVAILABLE, m, e); 2605dfc646bSeric continue; 2615dfc646bSeric } 2622a6e0786Seric 2632a6e0786Seric /* 2649ec9501bSeric ** Strip quote bits from names if the mailer is dumb 2659ec9501bSeric ** about them. 26625a99e2eSeric */ 26725a99e2eSeric 26857fc6f17Seric if (bitnset(M_STRIPQ, m->m_flags)) 26925a99e2eSeric { 2709ec9501bSeric stripquotes(user, TRUE); 2719ec9501bSeric stripquotes(host, TRUE); 2729ec9501bSeric } 2739ec9501bSeric else 2749ec9501bSeric { 2759ec9501bSeric stripquotes(user, FALSE); 2769ec9501bSeric stripquotes(host, FALSE); 27725a99e2eSeric } 27825a99e2eSeric 279cdb828c5Seric /* hack attack -- delivermail compatibility */ 280cdb828c5Seric if (m == ProgMailer && *user == '|') 281cdb828c5Seric user++; 282cdb828c5Seric 28325a99e2eSeric /* 2843efaed6eSeric ** If an error message has already been given, don't 2853efaed6eSeric ** bother to send to this address. 2863efaed6eSeric ** 2873efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 2883efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 2893efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 2903efaed6eSeric */ 2913efaed6eSeric 2926cae517dSeric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 2933efaed6eSeric continue; 2943efaed6eSeric 295f2fec898Seric /* save statistics.... */ 296588cad61Seric markstats(e, to); 297f2fec898Seric 2983efaed6eSeric /* 29925a99e2eSeric ** See if this user name is "special". 30025a99e2eSeric ** If the user name has a slash in it, assume that this 30151552439Seric ** is a file -- send it off without further ado. Note 30251552439Seric ** that this type of addresses is not processed along 30351552439Seric ** with the others, so we fudge on the To person. 30425a99e2eSeric */ 30525a99e2eSeric 3067da1035fSeric if (m == LocalMailer) 30725a99e2eSeric { 308a49f24c0Seric if (user[0] == '/') 30925a99e2eSeric { 310*b31e7f2bSeric rcode = mailfile(user, getctladdr(to), e); 311198d9be0Seric giveresponse(rcode, m, e); 312dde5acadSeric if (rcode == EX_OK) 313dde5acadSeric to->q_flags |= QSENT; 3145dfc646bSeric continue; 31525a99e2eSeric } 31625a99e2eSeric } 31725a99e2eSeric 31813bbc08cSeric /* 31913bbc08cSeric ** Address is verified -- add this user to mailer 32013bbc08cSeric ** argv, and add it to the print list of recipients. 32113bbc08cSeric */ 32213bbc08cSeric 323508daeccSeric /* link together the chain of recipients */ 324508daeccSeric to->q_tchain = tochain; 325508daeccSeric tochain = to; 326508daeccSeric 3275dfc646bSeric /* create list of users for error messages */ 328db8841e9Seric (void) strcat(tobuf, ","); 329db8841e9Seric (void) strcat(tobuf, to->q_paddr); 330588cad61Seric define('u', user, e); /* to user */ 331588cad61Seric define('z', to->q_home, e); /* user's home */ 3325dfc646bSeric 333c579ef51Seric /* 334508daeccSeric ** Expand out this user into argument list. 335c579ef51Seric */ 336c579ef51Seric 337508daeccSeric if (!clever) 338c579ef51Seric { 339588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 3405dfc646bSeric *pvp++ = newstr(buf); 3415dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 3425dfc646bSeric { 3435dfc646bSeric /* allow some space for trailing parms */ 3445dfc646bSeric break; 3455dfc646bSeric } 3465dfc646bSeric } 347c579ef51Seric } 3485dfc646bSeric 349145b49b1Seric /* see if any addresses still exist */ 350145b49b1Seric if (tobuf[0] == '\0') 351c579ef51Seric { 352588cad61Seric define('g', (char *) NULL, e); 353c23ed322Seric define('<', (char *) NULL, e); 354145b49b1Seric return (0); 355c579ef51Seric } 356145b49b1Seric 3575dfc646bSeric /* print out messages as full list */ 35863780dbdSeric e->e_to = tobuf + 1; 3595dfc646bSeric 3605dfc646bSeric /* 3615dfc646bSeric ** Fill out any parameters after the $u parameter. 3625dfc646bSeric */ 3635dfc646bSeric 364c579ef51Seric while (!clever && *++mvp != NULL) 3655dfc646bSeric { 366588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 3675dfc646bSeric *pvp++ = newstr(buf); 3685dfc646bSeric if (pvp >= &pv[MAXPV]) 3695dfc646bSeric syserr("deliver: pv overflow after $u for %s", pv[0]); 3705dfc646bSeric } 3715dfc646bSeric *pvp++ = NULL; 3725dfc646bSeric 37325a99e2eSeric /* 37425a99e2eSeric ** Call the mailer. 3756328bdf7Seric ** The argument vector gets built, pipes 37625a99e2eSeric ** are created as necessary, and we fork & exec as 3776328bdf7Seric ** appropriate. 378c579ef51Seric ** If we are running SMTP, we just need to clean up. 37925a99e2eSeric */ 38025a99e2eSeric 38186b26461Seric if (ctladdr == NULL) 38286b26461Seric ctladdr = &e->e_from; 383134746fbSeric #ifdef NAMED_BIND 3842bcc6d2dSeric if (ConfigLevel < 2) 385912a731aSbostic _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 386134746fbSeric #endif 387*b31e7f2bSeric mci = openmailer(m, pv, ctladdr, clever, e); 388*b31e7f2bSeric if (mci == NULL) 389134746fbSeric { 390*b31e7f2bSeric /* catastrophic error */ 391*b31e7f2bSeric rcode = -1; 392*b31e7f2bSeric goto give_up; 393*b31e7f2bSeric } 394*b31e7f2bSeric else if (mci->mci_state != MCIS_OPEN) 395*b31e7f2bSeric { 396*b31e7f2bSeric /* couldn't open the mailer */ 397*b31e7f2bSeric rcode = mci->mci_exitstat; 398*b31e7f2bSeric if (rcode == EX_OK) 399*b31e7f2bSeric { 400*b31e7f2bSeric /* shouldn't happen */ 401*b31e7f2bSeric rcode = EX_SOFTWARE; 402*b31e7f2bSeric } 403*b31e7f2bSeric } 404*b31e7f2bSeric else if (!clever) 405*b31e7f2bSeric { 406*b31e7f2bSeric /* 407*b31e7f2bSeric ** Format and send message. 408*b31e7f2bSeric */ 40915d084d5Seric 410*b31e7f2bSeric putfromline(mci->mci_out, m, e); 411*b31e7f2bSeric (*e->e_puthdr)(mci->mci_out, m, e); 412*b31e7f2bSeric putline("\n", mci->mci_out, m); 413*b31e7f2bSeric (*e->e_putbody)(mci->mci_out, m, e); 414*b31e7f2bSeric 415*b31e7f2bSeric /* get the exit status */ 416*b31e7f2bSeric rcode = endmailer(mci, pv[0]); 417134746fbSeric } 418134746fbSeric else 419*b31e7f2bSeric #ifdef SMTP 420134746fbSeric { 421*b31e7f2bSeric /* 422*b31e7f2bSeric ** Send the MAIL FROM: protocol 423*b31e7f2bSeric */ 42415d084d5Seric 425*b31e7f2bSeric rcode = smtpmailfrom(m, mci, e); 426*b31e7f2bSeric if (rcode == EX_OK) 42775889e88Seric { 428ded0d3daSkarels register char *t = tobuf; 429ded0d3daSkarels register int i; 430ded0d3daSkarels 431588cad61Seric /* send the recipient list */ 43263780dbdSeric tobuf[0] = '\0'; 43375889e88Seric for (to = tochain; to != NULL; to = to->q_tchain) 43475889e88Seric { 43563780dbdSeric e->e_to = to->q_paddr; 43615d084d5Seric if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 43775889e88Seric { 43883b7ddc9Seric markfailure(e, to, i); 439198d9be0Seric giveresponse(i, m, e); 44063780dbdSeric } 44175889e88Seric else 44275889e88Seric { 443911693bfSbostic *t++ = ','; 444*b31e7f2bSeric for (p = to->q_paddr; *p; *t++ = *p++) 445*b31e7f2bSeric continue; 446588cad61Seric } 447588cad61Seric } 448588cad61Seric 44963780dbdSeric /* now send the data */ 45063780dbdSeric if (tobuf[0] == '\0') 451*b31e7f2bSeric { 45263780dbdSeric e->e_to = NULL; 453*b31e7f2bSeric if (bitset(MCIF_CACHED, mci->mci_flags)) 454*b31e7f2bSeric smtprset(m, mci, e); 455*b31e7f2bSeric } 45675889e88Seric else 45775889e88Seric { 45863780dbdSeric e->e_to = tobuf + 1; 45975889e88Seric rcode = smtpdata(m, mci, e); 46063780dbdSeric } 46163780dbdSeric 46263780dbdSeric /* now close the connection */ 463*b31e7f2bSeric if (!bitset(MCIF_CACHED, mci->mci_flags)) 46415d084d5Seric smtpquit(m, mci, e); 46563780dbdSeric } 466c579ef51Seric } 467*b31e7f2bSeric #else /* not SMTP */ 468a05b3449Sbostic { 469*b31e7f2bSeric syserr("deliver: need SMTP compiled to use clever mailer"); 470*b31e7f2bSeric rcode = -1; 471*b31e7f2bSeric goto give_up; 472a05b3449Sbostic } 473*b31e7f2bSeric #endif /* SMTP */ 474134746fbSeric #ifdef NAMED_BIND 4752bcc6d2dSeric if (ConfigLevel < 2) 476912a731aSbostic _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 477134746fbSeric #endif 4785dfc646bSeric 479*b31e7f2bSeric /* arrange a return receipt if requested */ 480*b31e7f2bSeric if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags)) 481*b31e7f2bSeric { 482*b31e7f2bSeric e->e_flags |= EF_SENDRECEIPT; 483*b31e7f2bSeric /* do we want to send back more info? */ 484*b31e7f2bSeric } 485*b31e7f2bSeric 486c77d1c25Seric /* 48763780dbdSeric ** Do final status disposal. 48863780dbdSeric ** We check for something in tobuf for the SMTP case. 489c77d1c25Seric ** If we got a temporary failure, arrange to queue the 490c77d1c25Seric ** addressees. 491c77d1c25Seric */ 492c77d1c25Seric 493*b31e7f2bSeric give_up: 49463780dbdSeric if (tobuf[0] != '\0') 495198d9be0Seric giveresponse(rcode, m, e); 496772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 497*b31e7f2bSeric { 498dde5acadSeric if (rcode != EX_OK) 49983b7ddc9Seric markfailure(e, to, rcode); 500dde5acadSeric else 501dde5acadSeric to->q_flags |= QSENT; 502*b31e7f2bSeric } 503*b31e7f2bSeric 504*b31e7f2bSeric /* 505*b31e7f2bSeric ** Restore state and return. 506*b31e7f2bSeric */ 507c77d1c25Seric 50835490626Seric errno = 0; 509588cad61Seric define('g', (char *) NULL, e); 510c23ed322Seric define('<', (char *) NULL, e); 5115826d9d3Seric return (rcode); 51225a99e2eSeric } 5135dfc646bSeric /* 51483b7ddc9Seric ** MARKFAILURE -- mark a failure on a specific address. 51583b7ddc9Seric ** 51683b7ddc9Seric ** Parameters: 51783b7ddc9Seric ** e -- the envelope we are sending. 51883b7ddc9Seric ** q -- the address to mark. 51983b7ddc9Seric ** rcode -- the code signifying the particular failure. 52083b7ddc9Seric ** 52183b7ddc9Seric ** Returns: 52283b7ddc9Seric ** none. 52383b7ddc9Seric ** 52483b7ddc9Seric ** Side Effects: 52583b7ddc9Seric ** marks the address (and possibly the envelope) with the 52683b7ddc9Seric ** failure so that an error will be returned or 52783b7ddc9Seric ** the message will be queued, as appropriate. 52883b7ddc9Seric */ 52983b7ddc9Seric 53083b7ddc9Seric markfailure(e, q, rcode) 53183b7ddc9Seric register ENVELOPE *e; 53283b7ddc9Seric register ADDRESS *q; 53383b7ddc9Seric int rcode; 53483b7ddc9Seric { 53583b7ddc9Seric if (rcode == EX_OK) 53683b7ddc9Seric return; 537ef137175Sbostic else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR) 53883b7ddc9Seric q->q_flags |= QBADADDR; 53983b7ddc9Seric else if (curtime() > e->e_ctime + TimeOut) 54083b7ddc9Seric { 54183b7ddc9Seric extern char *pintvl(); 542198d9be0Seric char buf[MAXLINE]; 54383b7ddc9Seric 54483b7ddc9Seric if (!bitset(EF_TIMEOUT, e->e_flags)) 545198d9be0Seric { 546198d9be0Seric (void) sprintf(buf, "Cannot send message for %s", 54783b7ddc9Seric pintvl(TimeOut, FALSE)); 548198d9be0Seric if (e->e_message != NULL) 549198d9be0Seric free(e->e_message); 550198d9be0Seric e->e_message = newstr(buf); 551198d9be0Seric message(Arpa_Info, buf); 552198d9be0Seric } 55383b7ddc9Seric q->q_flags |= QBADADDR; 55483b7ddc9Seric e->e_flags |= EF_TIMEOUT; 55583b7ddc9Seric } 55683b7ddc9Seric else 55783b7ddc9Seric q->q_flags |= QQUEUEUP; 55883b7ddc9Seric } 55983b7ddc9Seric /* 56032d19d43Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 56132d19d43Seric ** 56232d19d43Seric ** This MUST be a macro, since after a vfork we are running 56332d19d43Seric ** two processes on the same stack!!! 56432d19d43Seric ** 56532d19d43Seric ** Parameters: 56632d19d43Seric ** none. 56732d19d43Seric ** 56832d19d43Seric ** Returns: 56932d19d43Seric ** From a macro??? You've got to be kidding! 57032d19d43Seric ** 57132d19d43Seric ** Side Effects: 57232d19d43Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 57332d19d43Seric ** pid of child in parent, zero in child. 57432d19d43Seric ** -1 on unrecoverable error. 57532d19d43Seric ** 57632d19d43Seric ** Notes: 57732d19d43Seric ** I'm awfully sorry this looks so awful. That's 57832d19d43Seric ** vfork for you..... 57932d19d43Seric */ 58032d19d43Seric 58132d19d43Seric # define NFORKTRIES 5 58284f7cd1bSeric 58384f7cd1bSeric # ifndef FORK 58484f7cd1bSeric # define FORK fork 58584f7cd1bSeric # endif 58632d19d43Seric 58732d19d43Seric # define DOFORK(fORKfN) \ 58832d19d43Seric {\ 58932d19d43Seric register int i;\ 59032d19d43Seric \ 59111799049Seric for (i = NFORKTRIES; --i >= 0; )\ 59232d19d43Seric {\ 59332d19d43Seric pid = fORKfN();\ 59432d19d43Seric if (pid >= 0)\ 59532d19d43Seric break;\ 59611799049Seric if (i > 0)\ 5976c4635f6Seric sleep((unsigned) NFORKTRIES - i);\ 59832d19d43Seric }\ 59932d19d43Seric } 60032d19d43Seric /* 6012ed72599Seric ** DOFORK -- simple fork interface to DOFORK. 6022ed72599Seric ** 6032ed72599Seric ** Parameters: 6042ed72599Seric ** none. 6052ed72599Seric ** 6062ed72599Seric ** Returns: 6072ed72599Seric ** pid of child in parent. 6082ed72599Seric ** zero in child. 6092ed72599Seric ** -1 on error. 6102ed72599Seric ** 6112ed72599Seric ** Side Effects: 6122ed72599Seric ** returns twice, once in parent and once in child. 6132ed72599Seric */ 6142ed72599Seric 6152ed72599Seric dofork() 6162ed72599Seric { 6172ed72599Seric register int pid; 6182ed72599Seric 6192ed72599Seric DOFORK(fork); 6202ed72599Seric return (pid); 6212ed72599Seric } 6222ed72599Seric /* 623c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 624c579ef51Seric ** 625c579ef51Seric ** We should never get fatal errors (e.g., segmentation 626c579ef51Seric ** violation), so we report those specially. For other 627c579ef51Seric ** errors, we choose a status message (into statmsg), 628c579ef51Seric ** and if it represents an error, we print it. 629c579ef51Seric ** 630c579ef51Seric ** Parameters: 631c579ef51Seric ** pid -- pid of mailer. 632c579ef51Seric ** name -- name of mailer (for error messages). 633c579ef51Seric ** 634c579ef51Seric ** Returns: 635c579ef51Seric ** exit code of mailer. 636c579ef51Seric ** 637c579ef51Seric ** Side Effects: 638c579ef51Seric ** none. 639c579ef51Seric */ 640c579ef51Seric 64175889e88Seric endmailer(mci, name) 642*b31e7f2bSeric register MCI *mci; 643c579ef51Seric char *name; 644c579ef51Seric { 645588cad61Seric int st; 646c579ef51Seric 64775889e88Seric /* close any connections */ 64875889e88Seric if (mci->mci_in != NULL) 64975889e88Seric (void) fclose(mci->mci_in); 65075889e88Seric if (mci->mci_out != NULL) 65175889e88Seric (void) fclose(mci->mci_out); 65275889e88Seric mci->mci_in = mci->mci_out = NULL; 65375889e88Seric mci->mci_state = MCIS_CLOSED; 65475889e88Seric 65533db8731Seric /* in the IPC case there is nothing to wait for */ 65675889e88Seric if (mci->mci_pid == 0) 65733db8731Seric return (EX_OK); 65833db8731Seric 65933db8731Seric /* wait for the mailer process to die and collect status */ 66075889e88Seric st = waitfor(mci->mci_pid); 661588cad61Seric if (st == -1) 66278de67c1Seric { 663588cad61Seric syserr("endmailer %s: wait", name); 664588cad61Seric return (EX_SOFTWARE); 665c579ef51Seric } 66633db8731Seric 66733db8731Seric /* see if it died a horrid death */ 668c579ef51Seric if ((st & 0377) != 0) 669c579ef51Seric { 6705f73204aSeric syserr("mailer %s died with signal %o", name, st); 6715f73204aSeric ExitStat = EX_TEMPFAIL; 6725f73204aSeric return (EX_TEMPFAIL); 673c579ef51Seric } 67433db8731Seric 67533db8731Seric /* normal death -- return status */ 676588cad61Seric st = (st >> 8) & 0377; 677588cad61Seric return (st); 678c579ef51Seric } 679c579ef51Seric /* 680c579ef51Seric ** OPENMAILER -- open connection to mailer. 681c579ef51Seric ** 682c579ef51Seric ** Parameters: 683c579ef51Seric ** m -- mailer descriptor. 684c579ef51Seric ** pvp -- parameter vector to pass to mailer. 685c579ef51Seric ** ctladdr -- controlling address for user. 686c579ef51Seric ** clever -- create a full duplex connection. 687c579ef51Seric ** 688c579ef51Seric ** Returns: 68975889e88Seric ** The mail connection info struct for this connection. 69075889e88Seric ** NULL on failure. 691c579ef51Seric ** 692c579ef51Seric ** Side Effects: 693c579ef51Seric ** creates a mailer in a subprocess. 694c579ef51Seric */ 695c579ef51Seric 696*b31e7f2bSeric MCI * 697*b31e7f2bSeric openmailer(m, pvp, ctladdr, clever, e) 698588cad61Seric MAILER *m; 699c579ef51Seric char **pvp; 700c579ef51Seric ADDRESS *ctladdr; 701c579ef51Seric bool clever; 702*b31e7f2bSeric ENVELOPE *e; 703c579ef51Seric { 7045dfc646bSeric int pid; 705*b31e7f2bSeric register MCI *mci; 706f8952a83Seric int mpvect[2]; 707c579ef51Seric int rpvect[2]; 7085dfc646bSeric extern FILE *fdopen(); 7095dfc646bSeric 7106ef48975Seric if (tTd(11, 1)) 7115dfc646bSeric { 7128c57e552Seric printf("openmailer:"); 7135dfc646bSeric printav(pvp); 7145dfc646bSeric } 71535490626Seric errno = 0; 7165dfc646bSeric 717ef66a9d0Seric CurHostName = m->m_mailer; 718ef66a9d0Seric 71933db8731Seric /* 72033db8731Seric ** Deal with the special case of mail handled through an IPC 72133db8731Seric ** connection. 72233db8731Seric ** In this case we don't actually fork. We must be 72333db8731Seric ** running SMTP for this to work. We will return a 72433db8731Seric ** zero pid to indicate that we are running IPC. 725e7c1bd78Seric ** We also handle a debug version that just talks to stdin/out. 72633db8731Seric */ 72733db8731Seric 728e7c1bd78Seric /* check for Local Person Communication -- not for mortals!!! */ 729e7c1bd78Seric if (strcmp(m->m_mailer, "[LPC]") == 0) 730e7c1bd78Seric { 731*b31e7f2bSeric mci = (MCI *) xalloc(sizeof *mci); 73275889e88Seric mci->mci_in = stdin; 73375889e88Seric mci->mci_out = stdout; 73475889e88Seric mci->mci_pid = 0; 735*b31e7f2bSeric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 73615d084d5Seric mci->mci_mailer = m; 737*b31e7f2bSeric mci->mci_flags = 0; 738e7c1bd78Seric } 739*b31e7f2bSeric else if (strcmp(m->m_mailer, "[IPC]") == 0 || 740914346b1Seric strcmp(m->m_mailer, "[TCP]") == 0) 74133db8731Seric { 74284f7cd1bSeric #ifdef DAEMON 7435f73204aSeric register STAB *st; 7445f73204aSeric extern STAB *stab(); 745ebc61751Sbloom register int i, j; 7461277f9a8Seric register u_short port; 747*b31e7f2bSeric int nmx; 748*b31e7f2bSeric char *mxhosts[MAXMXHOSTS + 1]; 749*b31e7f2bSeric extern MCI *mci_get(); 75033db8731Seric 751ef66a9d0Seric CurHostName = pvp[1]; 752*b31e7f2bSeric #ifdef NAMED_BIND 753*b31e7f2bSeric if (CurHostName != NULL && CurHostName[0] != '\0' && 754*b31e7f2bSeric CurHostName[0] != '[') 755*b31e7f2bSeric { 756*b31e7f2bSeric int rcode; 757*b31e7f2bSeric char buf[MAXNAME]; 758*b31e7f2bSeric 759*b31e7f2bSeric expand("\001j", buf, &buf[sizeof(buf) - 1], e); 760*b31e7f2bSeric nmx = getmxrr(CurHostName, mxhosts, buf, &rcode); 761*b31e7f2bSeric if (nmx < 0) 762*b31e7f2bSeric { 763*b31e7f2bSeric mci = mci_get(CurHostName, m); 764*b31e7f2bSeric mci->mci_exitstat = rcode; 765*b31e7f2bSeric mci->mci_errno = errno; 766*b31e7f2bSeric } 767*b31e7f2bSeric } 768*b31e7f2bSeric else 769*b31e7f2bSeric #endif 770*b31e7f2bSeric { 771*b31e7f2bSeric nmx = 1; 772*b31e7f2bSeric mxhosts[0] = CurHostName; 773*b31e7f2bSeric } 774*b31e7f2bSeric 77533db8731Seric if (!clever) 77633db8731Seric syserr("non-clever IPC"); 77793b6e3cfSeric if (pvp[2] != NULL) 7781277f9a8Seric port = atoi(pvp[2]); 77993b6e3cfSeric else 7801277f9a8Seric port = 0; 781*b31e7f2bSeric for (j = 0; j < nmx; j++) 782ebc61751Sbloom { 78345a8316eSeric /* see if we already know that this host is fried */ 784*b31e7f2bSeric CurHostName = mxhosts[j]; 785*b31e7f2bSeric mci = mci_get(CurHostName, m); 786*b31e7f2bSeric if (mci->mci_state != MCIS_CLOSED) 78775889e88Seric return mci; 78815d084d5Seric mci->mci_mailer = m; 789*b31e7f2bSeric if (mci->mci_exitstat != EX_OK) 790*b31e7f2bSeric continue; 791*b31e7f2bSeric 79275889e88Seric /* try the connection */ 793*b31e7f2bSeric setproctitle("%s %s: %s", e->e_id, mxhosts[1], "user open"); 794914346b1Seric message(Arpa_Info, "Connecting to %s (%s)...", 795*b31e7f2bSeric mxhosts[j], m->m_name); 796*b31e7f2bSeric i = makeconnection(mxhosts[j], port, mci, 797914346b1Seric bitnset(M_SECURE_PORT, m->m_flags)); 79875889e88Seric mci->mci_exitstat = i; 79975889e88Seric mci->mci_errno = errno; 80075889e88Seric if (i == EX_OK) 801*b31e7f2bSeric { 802*b31e7f2bSeric mci->mci_state = MCIS_OPENING; 803*b31e7f2bSeric mci_cache(mci); 804*b31e7f2bSeric break; 805*b31e7f2bSeric } 806*b31e7f2bSeric else if (tTd(11, 1)) 807*b31e7f2bSeric printf("openmailer: makeconnection => stat=%d, errno=%d\n", 808*b31e7f2bSeric i, errno); 809*b31e7f2bSeric 81075889e88Seric 8115f73204aSeric /* enter status of this host */ 81275889e88Seric setstat(i); 813ed854c7bSeric } 814*b31e7f2bSeric #else /* no DAEMON */ 815588cad61Seric syserr("openmailer: no IPC"); 81675889e88Seric return NULL; 817*b31e7f2bSeric #endif /* DAEMON */ 818588cad61Seric } 819*b31e7f2bSeric else 820*b31e7f2bSeric { 8216328bdf7Seric /* create a pipe to shove the mail through */ 822f8952a83Seric if (pipe(mpvect) < 0) 82325a99e2eSeric { 824588cad61Seric syserr("openmailer: pipe (to mailer)"); 82575889e88Seric return NULL; 82625a99e2eSeric } 827c579ef51Seric 828c579ef51Seric /* if this mailer speaks smtp, create a return pipe */ 829c579ef51Seric if (clever && pipe(rpvect) < 0) 830c579ef51Seric { 831588cad61Seric syserr("openmailer: pipe (from mailer)"); 832c579ef51Seric (void) close(mpvect[0]); 833c579ef51Seric (void) close(mpvect[1]); 83475889e88Seric return NULL; 835c579ef51Seric } 836c579ef51Seric 83733db8731Seric /* 83833db8731Seric ** Actually fork the mailer process. 83933db8731Seric ** DOFORK is clever about retrying. 8406984bfddSeric ** 8416984bfddSeric ** Dispose of SIGCHLD signal catchers that may be laying 8426984bfddSeric ** around so that endmail will get it. 84333db8731Seric */ 84433db8731Seric 845*b31e7f2bSeric if (e->e_xfp != NULL) 846*b31e7f2bSeric (void) fflush(e->e_xfp); /* for debugging */ 847588cad61Seric (void) fflush(stdout); 8486984bfddSeric # ifdef SIGCHLD 8496984bfddSeric (void) signal(SIGCHLD, SIG_DFL); 8506984bfddSeric # endif SIGCHLD 85184f7cd1bSeric DOFORK(FORK); 852f129ec7dSeric /* pid is set by DOFORK */ 85325a99e2eSeric if (pid < 0) 85425a99e2eSeric { 85533db8731Seric /* failure */ 856588cad61Seric syserr("openmailer: cannot fork"); 857f8952a83Seric (void) close(mpvect[0]); 858f8952a83Seric (void) close(mpvect[1]); 859c579ef51Seric if (clever) 860c579ef51Seric { 861c579ef51Seric (void) close(rpvect[0]); 862c579ef51Seric (void) close(rpvect[1]); 863c579ef51Seric } 86475889e88Seric return NULL; 86525a99e2eSeric } 86625a99e2eSeric else if (pid == 0) 86725a99e2eSeric { 86813088b9fSeric int i; 8695f73204aSeric extern int DtableSize; 87013088b9fSeric 87125a99e2eSeric /* child -- set up input & exec mailer */ 87203ab8e55Seric /* make diagnostic output be standard output */ 8738f0e7860Seric (void) signal(SIGINT, SIG_IGN); 8748f0e7860Seric (void) signal(SIGHUP, SIG_IGN); 8750984da9fSeric (void) signal(SIGTERM, SIG_DFL); 876f8952a83Seric 877*b31e7f2bSeric /* arrange to filter std & diag output of command */ 878c579ef51Seric if (clever) 879c579ef51Seric { 880c579ef51Seric (void) close(rpvect[0]); 881c579ef51Seric (void) close(1); 882c579ef51Seric (void) dup(rpvect[1]); 883c579ef51Seric (void) close(rpvect[1]); 884c579ef51Seric } 885276723a8Seric else if (OpMode == MD_SMTP || HoldErrs) 886f8952a83Seric { 887588cad61Seric /* put mailer output in transcript */ 888f8952a83Seric (void) close(1); 889*b31e7f2bSeric (void) dup(fileno(e->e_xfp)); 890f8952a83Seric } 891db8841e9Seric (void) close(2); 892db8841e9Seric (void) dup(1); 893f8952a83Seric 894f8952a83Seric /* arrange to get standard input */ 895f8952a83Seric (void) close(mpvect[1]); 896db8841e9Seric (void) close(0); 897f8952a83Seric if (dup(mpvect[0]) < 0) 89825a99e2eSeric { 89925a99e2eSeric syserr("Cannot dup to zero!"); 900a590b978Seric _exit(EX_OSERR); 90125a99e2eSeric } 902f8952a83Seric (void) close(mpvect[0]); 90357fc6f17Seric if (!bitnset(M_RESTR, m->m_flags)) 9040984da9fSeric { 90553e3fa05Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 906e36b99e2Seric { 907e36b99e2Seric (void) setgid(DefGid); 908898a126bSbostic (void) initgroups(DefUser, DefGid); 909e36b99e2Seric (void) setuid(DefUid); 910e36b99e2Seric } 911e36b99e2Seric else 91269f29479Seric { 913e36b99e2Seric (void) setgid(ctladdr->q_gid); 914898a126bSbostic (void) initgroups(ctladdr->q_ruser? 915898a126bSbostic ctladdr->q_ruser: ctladdr->q_user, 916898a126bSbostic ctladdr->q_gid); 917e36b99e2Seric (void) setuid(ctladdr->q_uid); 91869f29479Seric } 9190984da9fSeric } 920588cad61Seric 92113088b9fSeric /* arrange for all the files to be closed */ 922*b31e7f2bSeric for (i = 3; i < DtableSize; i++) 923*b31e7f2bSeric { 924fccb3f7aSbostic register int j; 925fccb3f7aSbostic if ((j = fcntl(i, F_GETFD, 0)) != -1) 926fccb3f7aSbostic (void)fcntl(i, F_SETFD, j|1); 927fccb3f7aSbostic } 92833db8731Seric 92933db8731Seric /* try to execute the mailer */ 9305df317aaSeric execve(m->m_mailer, pvp, UserEnviron); 93113088b9fSeric syserr("Cannot exec %s", m->m_mailer); 93295b76e4bSeric if (m == LocalMailer) 93355f33c03Seric _exit(EX_TEMPFAIL); 93495b76e4bSeric switch (errno) 93595b76e4bSeric { 93695b76e4bSeric case EIO: 93795b76e4bSeric case EAGAIN: 93895b76e4bSeric case ENOMEM: 93995b76e4bSeric # ifdef EPROCLIM 94095b76e4bSeric case EPROCLIM: 94195b76e4bSeric # endif 94295b76e4bSeric _exit(EX_TEMPFAIL); 94395b76e4bSeric } 944a590b978Seric _exit(EX_UNAVAILABLE); 94525a99e2eSeric } 94625a99e2eSeric 947f8952a83Seric /* 948c579ef51Seric ** Set up return value. 949f8952a83Seric */ 950f8952a83Seric 951*b31e7f2bSeric mci = (MCI *) xalloc(sizeof *mci); 95215d084d5Seric mci->mci_mailer = m; 953*b31e7f2bSeric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 954f8952a83Seric (void) close(mpvect[0]); 95575889e88Seric mci->mci_out = fdopen(mpvect[1], "w"); 956c579ef51Seric if (clever) 95725a99e2eSeric { 958c579ef51Seric (void) close(rpvect[1]); 95975889e88Seric mci->mci_in = fdopen(rpvect[0], "r"); 96075889e88Seric } 96175889e88Seric else 96275889e88Seric { 96375889e88Seric mci->mci_flags |= MCIF_TEMP; 96475889e88Seric mci->mci_in = NULL; 96575889e88Seric } 966*b31e7f2bSeric } 967*b31e7f2bSeric 968*b31e7f2bSeric /* 969*b31e7f2bSeric ** If we are in SMTP opening state, send initial protocol. 970*b31e7f2bSeric */ 971*b31e7f2bSeric 972*b31e7f2bSeric if (clever && mci->mci_state != MCIS_CLOSED) 973*b31e7f2bSeric { 974*b31e7f2bSeric smtpinit(m, mci, e); 975*b31e7f2bSeric } 976c579ef51Seric 97775889e88Seric return mci; 97825a99e2eSeric } 97925a99e2eSeric /* 98025a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 98125a99e2eSeric ** 98225a99e2eSeric ** Parameters: 98325a99e2eSeric ** stat -- the status code from the mailer (high byte 98425a99e2eSeric ** only; core dumps must have been taken care of 98525a99e2eSeric ** already). 98625a99e2eSeric ** m -- the mailer descriptor for this mailer. 98725a99e2eSeric ** 98825a99e2eSeric ** Returns: 989db8841e9Seric ** none. 99025a99e2eSeric ** 99125a99e2eSeric ** Side Effects: 992c1f9df2cSeric ** Errors may be incremented. 99325a99e2eSeric ** ExitStat may be set. 99425a99e2eSeric */ 99525a99e2eSeric 996198d9be0Seric giveresponse(stat, m, e) 99725a99e2eSeric int stat; 998588cad61Seric register MAILER *m; 999198d9be0Seric ENVELOPE *e; 100025a99e2eSeric { 100125a99e2eSeric register char *statmsg; 100225a99e2eSeric extern char *SysExMsg[]; 100325a99e2eSeric register int i; 1004d4bd8f0eSbostic extern int N_SysEx; 1005d4bd8f0eSbostic #ifdef NAMED_BIND 1006d4bd8f0eSbostic extern int h_errno; 1007d4bd8f0eSbostic #endif 1008198d9be0Seric char buf[MAXLINE]; 100925a99e2eSeric 10107d1fc79dSeric #ifdef lint 10117d1fc79dSeric if (m == NULL) 10127d1fc79dSeric return; 10137d1fc79dSeric #endif lint 10147d1fc79dSeric 101513bbc08cSeric /* 101613bbc08cSeric ** Compute status message from code. 101713bbc08cSeric */ 101813bbc08cSeric 101925a99e2eSeric i = stat - EX__BASE; 1020588cad61Seric if (stat == 0) 1021588cad61Seric statmsg = "250 Sent"; 1022588cad61Seric else if (i < 0 || i > N_SysEx) 1023588cad61Seric { 1024588cad61Seric (void) sprintf(buf, "554 unknown mailer error %d", stat); 1025588cad61Seric stat = EX_UNAVAILABLE; 1026588cad61Seric statmsg = buf; 1027588cad61Seric } 1028198d9be0Seric else if (stat == EX_TEMPFAIL) 1029198d9be0Seric { 10308557d168Seric (void) strcpy(buf, SysExMsg[i]); 1031d4bd8f0eSbostic #ifdef NAMED_BIND 1032f28da541Smiriam if (h_errno == TRY_AGAIN) 1033f28da541Smiriam { 1034f28da541Smiriam extern char *errstring(); 1035f28da541Smiriam 1036f28da541Smiriam statmsg = errstring(h_errno+MAX_ERRNO); 1037f28da541Smiriam } 1038f28da541Smiriam else 1039d4bd8f0eSbostic #endif 1040f28da541Smiriam { 10418557d168Seric if (errno != 0) 1042198d9be0Seric { 104387c9b3e7Seric extern char *errstring(); 10448557d168Seric 1045d87e85f3Seric statmsg = errstring(errno); 1046d87e85f3Seric } 1047d87e85f3Seric else 1048d87e85f3Seric { 1049d87e85f3Seric #ifdef SMTP 1050d87e85f3Seric extern char SmtpError[]; 1051d87e85f3Seric 1052d87e85f3Seric statmsg = SmtpError; 1053d87e85f3Seric #else SMTP 1054d87e85f3Seric statmsg = NULL; 1055d87e85f3Seric #endif SMTP 1056d87e85f3Seric } 1057f28da541Smiriam } 1058d87e85f3Seric if (statmsg != NULL && statmsg[0] != '\0') 1059d87e85f3Seric { 106087c9b3e7Seric (void) strcat(buf, ": "); 1061d87e85f3Seric (void) strcat(buf, statmsg); 10628557d168Seric } 1063198d9be0Seric statmsg = buf; 1064198d9be0Seric } 106525a99e2eSeric else 1066d87e85f3Seric { 106725a99e2eSeric statmsg = SysExMsg[i]; 1068d87e85f3Seric } 1069588cad61Seric 1070588cad61Seric /* 1071588cad61Seric ** Print the message as appropriate 1072588cad61Seric */ 1073588cad61Seric 1074198d9be0Seric if (stat == EX_OK || stat == EX_TEMPFAIL) 10755826d9d3Seric message(Arpa_Info, &statmsg[4]); 107625a99e2eSeric else 107725a99e2eSeric { 1078c1f9df2cSeric Errors++; 10795826d9d3Seric usrerr(statmsg); 108025a99e2eSeric } 108125a99e2eSeric 108225a99e2eSeric /* 108325a99e2eSeric ** Final cleanup. 108425a99e2eSeric ** Log a record of the transaction. Compute the new 108525a99e2eSeric ** ExitStat -- if we already had an error, stick with 108625a99e2eSeric ** that. 108725a99e2eSeric */ 108825a99e2eSeric 108961f5a1d4Seric if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2)) 1090*b31e7f2bSeric logdelivery(&statmsg[4], e); 1091eb238f8cSeric 1092eb238f8cSeric if (stat != EX_TEMPFAIL) 1093eb238f8cSeric setstat(stat); 1094198d9be0Seric if (stat != EX_OK) 1095198d9be0Seric { 1096198d9be0Seric if (e->e_message != NULL) 1097198d9be0Seric free(e->e_message); 1098198d9be0Seric e->e_message = newstr(&statmsg[4]); 1099198d9be0Seric } 11008557d168Seric errno = 0; 1101d4bd8f0eSbostic #ifdef NAMED_BIND 1102f28da541Smiriam h_errno = 0; 1103d4bd8f0eSbostic #endif 1104eb238f8cSeric } 1105eb238f8cSeric /* 1106eb238f8cSeric ** LOGDELIVERY -- log the delivery in the system log 1107eb238f8cSeric ** 1108eb238f8cSeric ** Parameters: 1109eb238f8cSeric ** stat -- the message to print for the status 1110eb238f8cSeric ** 1111eb238f8cSeric ** Returns: 1112eb238f8cSeric ** none 1113eb238f8cSeric ** 1114eb238f8cSeric ** Side Effects: 1115eb238f8cSeric ** none 1116eb238f8cSeric */ 1117eb238f8cSeric 1118*b31e7f2bSeric logdelivery(stat, e) 1119eb238f8cSeric char *stat; 1120*b31e7f2bSeric register ENVELOPE *e; 11215cf56be3Seric { 11225cf56be3Seric extern char *pintvl(); 11235cf56be3Seric 1124eb238f8cSeric # ifdef LOG 1125*b31e7f2bSeric syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", e->e_id, 1126*b31e7f2bSeric e->e_to, pintvl(curtime() - e->e_ctime, TRUE), stat); 112725a99e2eSeric # endif LOG 112825a99e2eSeric } 112925a99e2eSeric /* 113051552439Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 113125a99e2eSeric ** 113251552439Seric ** This can be made an arbitrary message separator by changing $l 113351552439Seric ** 11349b6c17a6Seric ** One of the ugliest hacks seen by human eyes is contained herein: 11359b6c17a6Seric ** UUCP wants those stupid "remote from <host>" lines. Why oh why 11369b6c17a6Seric ** does a well-meaning programmer such as myself have to deal with 11379b6c17a6Seric ** this kind of antique garbage???? 113825a99e2eSeric ** 113925a99e2eSeric ** Parameters: 114051552439Seric ** fp -- the file to output to. 114151552439Seric ** m -- the mailer describing this entry. 114225a99e2eSeric ** 114325a99e2eSeric ** Returns: 114451552439Seric ** none 114525a99e2eSeric ** 114625a99e2eSeric ** Side Effects: 114751552439Seric ** outputs some text to fp. 114825a99e2eSeric */ 114925a99e2eSeric 1150*b31e7f2bSeric putfromline(fp, m, e) 115151552439Seric register FILE *fp; 115251552439Seric register MAILER *m; 1153*b31e7f2bSeric ENVELOPE *e; 115425a99e2eSeric { 11559b6c17a6Seric char *template = "\001l\n"; 115651552439Seric char buf[MAXLINE]; 115725a99e2eSeric 115857fc6f17Seric if (bitnset(M_NHDR, m->m_flags)) 115951552439Seric return; 116013bbc08cSeric 11612c7e1b8dSeric # ifdef UGLYUUCP 116257fc6f17Seric if (bitnset(M_UGLYUUCP, m->m_flags)) 116374b6e67bSeric { 1164ea09d6edSeric char *bang; 1165ea09d6edSeric char xbuf[MAXLINE]; 116674b6e67bSeric 1167*b31e7f2bSeric expand("\001<", buf, &buf[sizeof buf - 1], e); 1168ea09d6edSeric bang = index(buf, '!'); 116974b6e67bSeric if (bang == NULL) 1170ea09d6edSeric syserr("No ! in UUCP! (%s)", buf); 117174b6e67bSeric else 1172588cad61Seric { 1173ea09d6edSeric *bang++ = '\0'; 11749b6c17a6Seric (void) sprintf(xbuf, "From %s \001d remote from %s\n", bang, buf); 1175ea09d6edSeric template = xbuf; 117674b6e67bSeric } 1177588cad61Seric } 11782c7e1b8dSeric # endif UGLYUUCP 1179*b31e7f2bSeric expand(template, buf, &buf[sizeof buf - 1], e); 118077b52738Seric putline(buf, fp, m); 1181bc6e2962Seric } 1182bc6e2962Seric /* 118351552439Seric ** PUTBODY -- put the body of a message. 118451552439Seric ** 118551552439Seric ** Parameters: 118651552439Seric ** fp -- file to output onto. 118777b52738Seric ** m -- a mailer descriptor to control output format. 11889a6a5f55Seric ** e -- the envelope to put out. 118951552439Seric ** 119051552439Seric ** Returns: 119151552439Seric ** none. 119251552439Seric ** 119351552439Seric ** Side Effects: 119451552439Seric ** The message is written onto fp. 119551552439Seric */ 119651552439Seric 119777b52738Seric putbody(fp, m, e) 119851552439Seric FILE *fp; 1199588cad61Seric MAILER *m; 12009a6a5f55Seric register ENVELOPE *e; 120151552439Seric { 120277b52738Seric char buf[MAXLINE]; 120351552439Seric 120451552439Seric /* 120551552439Seric ** Output the body of the message 120651552439Seric */ 120751552439Seric 12089a6a5f55Seric if (e->e_dfp == NULL) 120951552439Seric { 12109a6a5f55Seric if (e->e_df != NULL) 12119a6a5f55Seric { 12129a6a5f55Seric e->e_dfp = fopen(e->e_df, "r"); 12139a6a5f55Seric if (e->e_dfp == NULL) 12148f9146b0Srick syserr("putbody: Cannot open %s for %s from %s", 12158f9146b0Srick e->e_df, e->e_to, e->e_from); 12169a6a5f55Seric } 12179a6a5f55Seric else 121877b52738Seric putline("<<< No Message Collected >>>", fp, m); 12199a6a5f55Seric } 12209a6a5f55Seric if (e->e_dfp != NULL) 12219a6a5f55Seric { 12229a6a5f55Seric rewind(e->e_dfp); 122377b52738Seric while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 122424fc8aeeSeric { 122524fc8aeeSeric if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1226d6fa2b58Sbostic strncmp(buf, "From ", 5) == 0) 12273462ad9eSeric (void) putc('>', fp); 122877b52738Seric putline(buf, fp, m); 122924fc8aeeSeric } 123051552439Seric 12319a6a5f55Seric if (ferror(e->e_dfp)) 123251552439Seric { 123351552439Seric syserr("putbody: read error"); 123451552439Seric ExitStat = EX_IOERR; 123551552439Seric } 123651552439Seric } 123751552439Seric 123851552439Seric (void) fflush(fp); 123951552439Seric if (ferror(fp) && errno != EPIPE) 124051552439Seric { 124151552439Seric syserr("putbody: write error"); 124251552439Seric ExitStat = EX_IOERR; 124351552439Seric } 124451552439Seric errno = 0; 124525a99e2eSeric } 124625a99e2eSeric /* 124725a99e2eSeric ** MAILFILE -- Send a message to a file. 124825a99e2eSeric ** 1249f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 1250f129ec7dSeric ** bits, sendmail will try to become the owner of that file 1251f129ec7dSeric ** rather than the real user. Obviously, this only works if 1252f129ec7dSeric ** sendmail runs as root. 1253f129ec7dSeric ** 1254588cad61Seric ** This could be done as a subordinate mailer, except that it 1255588cad61Seric ** is used implicitly to save messages in ~/dead.letter. We 1256588cad61Seric ** view this as being sufficiently important as to include it 1257588cad61Seric ** here. For example, if the system is dying, we shouldn't have 1258588cad61Seric ** to create another process plus some pipes to save the message. 1259588cad61Seric ** 126025a99e2eSeric ** Parameters: 126125a99e2eSeric ** filename -- the name of the file to send to. 12626259796dSeric ** ctladdr -- the controlling address header -- includes 12636259796dSeric ** the userid/groupid to be when sending. 126425a99e2eSeric ** 126525a99e2eSeric ** Returns: 126625a99e2eSeric ** The exit code associated with the operation. 126725a99e2eSeric ** 126825a99e2eSeric ** Side Effects: 126925a99e2eSeric ** none. 127025a99e2eSeric */ 127125a99e2eSeric 1272*b31e7f2bSeric mailfile(filename, ctladdr, e) 127325a99e2eSeric char *filename; 12746259796dSeric ADDRESS *ctladdr; 1275*b31e7f2bSeric register ENVELOPE *e; 127625a99e2eSeric { 127725a99e2eSeric register FILE *f; 127832d19d43Seric register int pid; 127915d084d5Seric int mode; 128025a99e2eSeric 128132d19d43Seric /* 128232d19d43Seric ** Fork so we can change permissions here. 128332d19d43Seric ** Note that we MUST use fork, not vfork, because of 128432d19d43Seric ** the complications of calling subroutines, etc. 128532d19d43Seric */ 128632d19d43Seric 128732d19d43Seric DOFORK(fork); 128832d19d43Seric 128932d19d43Seric if (pid < 0) 129032d19d43Seric return (EX_OSERR); 129132d19d43Seric else if (pid == 0) 129232d19d43Seric { 129332d19d43Seric /* child -- actually write to file */ 1294f129ec7dSeric struct stat stb; 1295f129ec7dSeric 12960984da9fSeric (void) signal(SIGINT, SIG_DFL); 12970984da9fSeric (void) signal(SIGHUP, SIG_DFL); 12980984da9fSeric (void) signal(SIGTERM, SIG_DFL); 12993462ad9eSeric (void) umask(OldUmask); 130095f16dc0Seric 1301f129ec7dSeric if (stat(filename, &stb) < 0) 1302e6e1265fSeric stb.st_mode = 0666; 130315d084d5Seric mode = stb.st_mode; 130495f16dc0Seric 130595f16dc0Seric /* limit the errors to those actually caused in the child */ 130695f16dc0Seric errno = 0; 130795f16dc0Seric ExitStat = EX_OK; 130895f16dc0Seric 1309f129ec7dSeric if (bitset(0111, stb.st_mode)) 1310f129ec7dSeric exit(EX_CANTCREAT); 131103827b5fSeric if (ctladdr == NULL) 13128f9146b0Srick ctladdr = &e->e_from; 131315d084d5Seric else 131415d084d5Seric { 131515d084d5Seric /* ignore setuid and setgid bits */ 131615d084d5Seric mode &= ~(S_ISGID|S_ISUID); 131715d084d5Seric } 131815d084d5Seric 13198f9146b0Srick /* we have to open the dfile BEFORE setuid */ 13208f9146b0Srick if (e->e_dfp == NULL && e->e_df != NULL) 13218f9146b0Srick { 13228f9146b0Srick e->e_dfp = fopen(e->e_df, "r"); 132395f16dc0Seric if (e->e_dfp == NULL) 132495f16dc0Seric { 13258f9146b0Srick syserr("mailfile: Cannot open %s for %s from %s", 13268f9146b0Srick e->e_df, e->e_to, e->e_from); 13278f9146b0Srick } 13288f9146b0Srick } 13298f9146b0Srick 133015d084d5Seric if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 1331e36b99e2Seric { 133295f16dc0Seric if (ctladdr->q_uid == 0) 133395f16dc0Seric { 1334e36b99e2Seric (void) setgid(DefGid); 1335898a126bSbostic (void) initgroups(DefUser, DefGid); 133695f16dc0Seric } 133795f16dc0Seric else 133895f16dc0Seric { 13396259796dSeric (void) setgid(ctladdr->q_gid); 1340898a126bSbostic (void) initgroups(ctladdr->q_ruser ? 1341898a126bSbostic ctladdr->q_ruser : ctladdr->q_user, 1342898a126bSbostic ctladdr->q_gid); 1343898a126bSbostic } 1344e36b99e2Seric } 134515d084d5Seric if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 1346e36b99e2Seric { 1347e36b99e2Seric if (ctladdr->q_uid == 0) 1348e36b99e2Seric (void) setuid(DefUid); 1349e36b99e2Seric else 13506259796dSeric (void) setuid(ctladdr->q_uid); 1351e36b99e2Seric } 135295f16dc0Seric FileName = filename; 135395f16dc0Seric LineNumber = 0; 135427628d59Seric f = dfopen(filename, "a"); 135525a99e2eSeric if (f == NULL) 135695f16dc0Seric { 135795f16dc0Seric message("cannot open"); 135832d19d43Seric exit(EX_CANTCREAT); 135995f16dc0Seric } 136025a99e2eSeric 1361*b31e7f2bSeric putfromline(f, ProgMailer, e); 136277b52738Seric (*CurEnv->e_puthdr)(f, ProgMailer, CurEnv); 136377b52738Seric putline("\n", f, ProgMailer); 136477b52738Seric (*CurEnv->e_putbody)(f, ProgMailer, CurEnv); 136577b52738Seric putline("\n", f, ProgMailer); 136695f16dc0Seric if (ferror(f)) 136795f16dc0Seric { 136895f16dc0Seric message("I/O error"); 136995f16dc0Seric setstat(EX_IOERR); 137095f16dc0Seric } 1371db8841e9Seric (void) fclose(f); 137232d19d43Seric (void) fflush(stdout); 1373e36b99e2Seric 137427628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 1375c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 137695f16dc0Seric exit(ExitStat); 137713bbc08cSeric /*NOTREACHED*/ 137832d19d43Seric } 137932d19d43Seric else 138032d19d43Seric { 138132d19d43Seric /* parent -- wait for exit status */ 1382588cad61Seric int st; 138332d19d43Seric 1384588cad61Seric st = waitfor(pid); 1385588cad61Seric if ((st & 0377) != 0) 1386588cad61Seric return (EX_UNAVAILABLE); 1387588cad61Seric else 1388588cad61Seric return ((st >> 8) & 0377); 13898f9146b0Srick /*NOTREACHED*/ 139032d19d43Seric } 139125a99e2eSeric } 1392ea4dc939Seric /* 1393ea4dc939Seric ** SENDALL -- actually send all the messages. 1394ea4dc939Seric ** 1395ea4dc939Seric ** Parameters: 13960c52a0b3Seric ** e -- the envelope to send. 13977b95031aSeric ** mode -- the delivery mode to use. If SM_DEFAULT, use 13987b95031aSeric ** the current SendMode. 1399ea4dc939Seric ** 1400ea4dc939Seric ** Returns: 1401ea4dc939Seric ** none. 1402ea4dc939Seric ** 1403ea4dc939Seric ** Side Effects: 1404ea4dc939Seric ** Scans the send lists and sends everything it finds. 14050c52a0b3Seric ** Delivers any appropriate error messages. 1406276723a8Seric ** If we are running in a non-interactive mode, takes the 1407276723a8Seric ** appropriate action. 1408ea4dc939Seric */ 1409ea4dc939Seric 1410276723a8Seric sendall(e, mode) 14110c52a0b3Seric ENVELOPE *e; 1412276723a8Seric char mode; 1413ea4dc939Seric { 1414e77e673fSeric register ADDRESS *q; 141514a8ed7aSeric bool oldverbose; 1416276723a8Seric int pid; 1417dde5acadSeric int nsent; 14181c265a00Seric # ifdef LOCKF 14191c265a00Seric struct flock lfd; 14201c265a00Seric # endif 1421ea4dc939Seric 14227b95031aSeric /* determine actual delivery mode */ 14237b95031aSeric if (mode == SM_DEFAULT) 14247b95031aSeric { 14255f73204aSeric extern bool shouldqueue(); 14267b95031aSeric 14275f73204aSeric if (shouldqueue(e->e_msgpriority)) 14287b95031aSeric mode = SM_QUEUE; 14297b95031aSeric else 14307b95031aSeric mode = SendMode; 14317b95031aSeric } 14327b95031aSeric 1433df864a8fSeric if (tTd(13, 1)) 1434772e6e50Seric { 1435276723a8Seric printf("\nSENDALL: mode %c, sendqueue:\n", mode); 14360c52a0b3Seric printaddr(e->e_sendqueue, TRUE); 1437772e6e50Seric } 1438ea4dc939Seric 14390c52a0b3Seric /* 1440276723a8Seric ** Do any preprocessing necessary for the mode we are running. 1441588cad61Seric ** Check to make sure the hop count is reasonable. 1442588cad61Seric ** Delete sends to the sender in mailing lists. 1443276723a8Seric */ 1444276723a8Seric 1445588cad61Seric CurEnv = e; 1446276723a8Seric 14471d57450bSeric if (e->e_hopcount > MaxHopCount) 1448276723a8Seric { 14498f9146b0Srick errno = 0; 14508f9146b0Srick syserr("sendall: too many hops %d (%d max): from %s, to %s", 14511d57450bSeric e->e_hopcount, MaxHopCount, e->e_from, e->e_to); 1452588cad61Seric return; 1453588cad61Seric } 1454588cad61Seric 1455588cad61Seric if (!MeToo) 1456276723a8Seric { 1457f3d6c55cSeric extern ADDRESS *recipient(); 1458f3d6c55cSeric 1459588cad61Seric e->e_from.q_flags |= QDONTSEND; 1460f3d6c55cSeric (void) recipient(&e->e_from, &e->e_sendqueue); 1461276723a8Seric } 1462588cad61Seric 1463588cad61Seric # ifdef QUEUE 1464b254bcb6Seric if ((mode == SM_QUEUE || mode == SM_FORK || 1465b254bcb6Seric (mode != SM_VERIFY && SuperSafe)) && 1466b254bcb6Seric !bitset(EF_INQUEUE, e->e_flags)) 1467e020b127Seric queueup(e, TRUE, mode == SM_QUEUE); 1468276723a8Seric #endif QUEUE 1469276723a8Seric 1470276723a8Seric oldverbose = Verbose; 1471276723a8Seric switch (mode) 1472276723a8Seric { 1473276723a8Seric case SM_VERIFY: 1474276723a8Seric Verbose = TRUE; 1475276723a8Seric break; 1476276723a8Seric 1477276723a8Seric case SM_QUEUE: 1478c7f5410dSeric queueonly: 1479b254bcb6Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 1480276723a8Seric return; 1481276723a8Seric 1482276723a8Seric case SM_FORK: 14839a6a5f55Seric if (e->e_xfp != NULL) 14849a6a5f55Seric (void) fflush(e->e_xfp); 1485c7f5410dSeric 1486c7f5410dSeric # ifdef LOCKF 1487c7f5410dSeric /* 1488c7f5410dSeric ** Since lockf has the interesting semantic that the 14891c265a00Seric ** lock is lost when we fork, we have to risk losing 14901c265a00Seric ** the lock here by closing before the fork, and then 14911c265a00Seric ** trying to get it back in the child. 1492c7f5410dSeric */ 1493c7f5410dSeric 1494e020b127Seric if (e->e_lockfp != NULL) 1495c7f5410dSeric { 1496e020b127Seric (void) fclose(e->e_lockfp); 1497e020b127Seric e->e_lockfp = NULL; 1498c7f5410dSeric } 1499c7f5410dSeric # endif /* LOCKF */ 1500c7f5410dSeric 1501276723a8Seric pid = fork(); 1502276723a8Seric if (pid < 0) 1503276723a8Seric { 1504c7f5410dSeric goto queueonly; 1505276723a8Seric } 1506276723a8Seric else if (pid > 0) 1507a6fce3d8Seric { 1508a6fce3d8Seric /* be sure we leave the temp files to our child */ 1509b254bcb6Seric e->e_id = e->e_df = NULL; 1510c7f5410dSeric # ifndef LOCKF 1511e020b127Seric if (e->e_lockfp != NULL) 1512e020b127Seric (void) fclose(e->e_lockfp); 1513c7f5410dSeric # endif 1514276723a8Seric return; 1515a6fce3d8Seric } 1516276723a8Seric 1517276723a8Seric /* double fork to avoid zombies */ 1518276723a8Seric if (fork() > 0) 1519276723a8Seric exit(EX_OK); 1520276723a8Seric 1521a6fce3d8Seric /* be sure we are immune from the terminal */ 1522769e215aSeric disconnect(FALSE); 1523a6fce3d8Seric 1524e6720d51Seric # ifdef LOCKF 1525e6720d51Seric /* 1526c7f5410dSeric ** Now try to get our lock back. 1527e6720d51Seric */ 1528e6720d51Seric 15291c265a00Seric lfd.l_type = F_WRLCK; 15301c265a00Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 1531e020b127Seric e->e_lockfp = fopen(queuename(e, 'q'), "r+"); 1532e020b127Seric if (e->e_lockfp == NULL || 15331c265a00Seric fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0) 1534e6720d51Seric { 1535e6720d51Seric /* oops.... lost it */ 1536e6720d51Seric # ifdef LOG 1537e6720d51Seric if (LogLevel > 5) 1538c7f5410dSeric syslog(LOG_NOTICE, "%s: lost lock: %m", 1539*b31e7f2bSeric e->e_id); 1540e6720d51Seric # endif /* LOG */ 1541e6720d51Seric exit(EX_OK); 1542e6720d51Seric } 1543e6720d51Seric # endif /* LOCKF */ 1544e6720d51Seric 1545276723a8Seric break; 1546276723a8Seric } 1547276723a8Seric 1548276723a8Seric /* 15490c52a0b3Seric ** Run through the list and send everything. 15500c52a0b3Seric */ 15510c52a0b3Seric 1552dde5acadSeric nsent = 0; 15530c52a0b3Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1554ea4dc939Seric { 1555276723a8Seric if (mode == SM_VERIFY) 1556ea4dc939Seric { 1557a6fce3d8Seric e->e_to = q->q_paddr; 1558e77e673fSeric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1559ea4dc939Seric message(Arpa_Info, "deliverable"); 1560ea4dc939Seric } 1561dde5acadSeric else if (!bitset(QDONTSEND, q->q_flags)) 1562dde5acadSeric { 1563de1179f5Seric # ifdef QUEUE 1564dde5acadSeric /* 1565dde5acadSeric ** Checkpoint the send list every few addresses 1566dde5acadSeric */ 1567dde5acadSeric 1568dde5acadSeric if (nsent >= CheckpointInterval) 1569dde5acadSeric { 1570e020b127Seric queueup(e, TRUE, FALSE); 1571dde5acadSeric nsent = 0; 1572dde5acadSeric } 1573de1179f5Seric # endif /* QUEUE */ 1574dde5acadSeric if (deliver(e, q) == EX_OK) 1575dde5acadSeric nsent++; 1576dde5acadSeric } 1577ea4dc939Seric } 157814a8ed7aSeric Verbose = oldverbose; 15790c52a0b3Seric 15800c52a0b3Seric /* 15810c52a0b3Seric ** Now run through and check for errors. 15820c52a0b3Seric */ 15830c52a0b3Seric 1584e020b127Seric if (mode == SM_VERIFY) 15850c52a0b3Seric return; 15860c52a0b3Seric 15870c52a0b3Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 15880c52a0b3Seric { 15890c52a0b3Seric register ADDRESS *qq; 15900c52a0b3Seric 1591df864a8fSeric if (tTd(13, 3)) 1592df864a8fSeric { 1593df864a8fSeric printf("Checking "); 1594df864a8fSeric printaddr(q, FALSE); 1595df864a8fSeric } 1596df864a8fSeric 1597b254bcb6Seric /* only send errors if the message failed */ 1598b254bcb6Seric if (!bitset(QBADADDR, q->q_flags)) 1599b254bcb6Seric continue; 16000c52a0b3Seric 16010c52a0b3Seric /* we have an address that failed -- find the parent */ 16020c52a0b3Seric for (qq = q; qq != NULL; qq = qq->q_alias) 16030c52a0b3Seric { 16040c52a0b3Seric char obuf[MAXNAME + 6]; 16050c52a0b3Seric extern char *aliaslookup(); 16060c52a0b3Seric 16070c52a0b3Seric /* we can only have owners for local addresses */ 160857fc6f17Seric if (!bitnset(M_LOCAL, qq->q_mailer->m_flags)) 16090c52a0b3Seric continue; 16100c52a0b3Seric 16110c52a0b3Seric /* see if the owner list exists */ 16120c52a0b3Seric (void) strcpy(obuf, "owner-"); 1613cec031e3Seric if (strncmp(qq->q_user, "owner-", 6) == 0) 1614cec031e3Seric (void) strcat(obuf, "owner"); 1615cec031e3Seric else 16160c52a0b3Seric (void) strcat(obuf, qq->q_user); 1617ef137175Sbostic makelower(obuf); 16180c52a0b3Seric if (aliaslookup(obuf) == NULL) 16190c52a0b3Seric continue; 16200c52a0b3Seric 1621df864a8fSeric if (tTd(13, 4)) 1622df864a8fSeric printf("Errors to %s\n", obuf); 1623df864a8fSeric 16240c52a0b3Seric /* owner list exists -- add it to the error queue */ 1625e3e4ed86Seric sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue); 162653e3fa05Seric ErrorMode = EM_MAIL; 16270c52a0b3Seric break; 16280c52a0b3Seric } 16290c52a0b3Seric 16300c52a0b3Seric /* if we did not find an owner, send to the sender */ 16317455aa0bSeric if (qq == NULL && bitset(QBADADDR, q->q_flags)) 1632e3e4ed86Seric sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue); 16330c52a0b3Seric } 1634276723a8Seric 1635276723a8Seric if (mode == SM_FORK) 1636276723a8Seric finis(); 16370c52a0b3Seric } 1638