14227346bSdist /* 2*0942ea6aSbostic * Copyright (c) 1983 Eric P. Allman 3e70a7521Sbostic * Copyright (c) 1988 Regents of the University of California. 4e70a7521Sbostic * All rights reserved. 5e70a7521Sbostic * 6e70a7521Sbostic * Redistribution and use in source and binary forms are permitted 7*0942ea6aSbostic * provided that the above copyright notice and this paragraph are 8*0942ea6aSbostic * duplicated in all such forms and that any documentation, 9*0942ea6aSbostic * advertising materials, and other materials related to such 10*0942ea6aSbostic * distribution and use acknowledge that the software was developed 11*0942ea6aSbostic * by the University of California, Berkeley. The name of the 12*0942ea6aSbostic * University may not be used to endorse or promote products derived 13*0942ea6aSbostic * from this software without specific prior written permission. 14*0942ea6aSbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 15*0942ea6aSbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 16*0942ea6aSbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 174227346bSdist */ 184227346bSdist 194227346bSdist #ifndef lint 20*0942ea6aSbostic static char sccsid[] = "@(#)deliver.c 5.22 (Berkeley) 06/30/88"; 21e70a7521Sbostic #endif /* not lint */ 224227346bSdist 23911693bfSbostic #include <sendmail.h> 24911693bfSbostic #include <sys/signal.h> 25c77d1c25Seric #include <sys/stat.h> 266d458631Sbostic #include <sys/ioctl.h> 27f28da541Smiriam #include <netdb.h> 28911693bfSbostic #include <errno.h> 29912a731aSbostic #include <arpa/nameser.h> 30912a731aSbostic #include <resolv.h> 3125a99e2eSeric 3225a99e2eSeric /* 3313bbc08cSeric ** DELIVER -- Deliver a message to a list of addresses. 3413bbc08cSeric ** 3513bbc08cSeric ** This routine delivers to everyone on the same host as the 3613bbc08cSeric ** user on the head of the list. It is clever about mailers 3713bbc08cSeric ** that don't handle multiple users. It is NOT guaranteed 3813bbc08cSeric ** that it will deliver to all these addresses however -- so 3913bbc08cSeric ** deliver should be called once for each address on the 4013bbc08cSeric ** list. 4125a99e2eSeric ** 4225a99e2eSeric ** Parameters: 43588cad61Seric ** e -- the envelope to deliver. 44c77d1c25Seric ** firstto -- head of the address list to deliver to. 4525a99e2eSeric ** 4625a99e2eSeric ** Returns: 4725a99e2eSeric ** zero -- successfully delivered. 4825a99e2eSeric ** else -- some failure, see ExitStat for more info. 4925a99e2eSeric ** 5025a99e2eSeric ** Side Effects: 5125a99e2eSeric ** The standard input is passed off to someone. 5225a99e2eSeric */ 5325a99e2eSeric 54588cad61Seric deliver(e, firstto) 55588cad61Seric register ENVELOPE *e; 56c77d1c25Seric ADDRESS *firstto; 5725a99e2eSeric { 5878442df3Seric char *host; /* host being sent to */ 5978442df3Seric char *user; /* user being sent to */ 6025a99e2eSeric char **pvp; 615dfc646bSeric register char **mvp; 6225a99e2eSeric register char *p; 63588cad61Seric register MAILER *m; /* mailer for this recipient */ 646259796dSeric ADDRESS *ctladdr; 65c77d1c25Seric register ADDRESS *to = firstto; 66c579ef51Seric bool clever = FALSE; /* running user smtp to this mailer */ 67772e6e50Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 68911693bfSbostic int rcode; /* response code */ 69ee6bf8dfSeric char *pv[MAXPV+1]; 70ee6bf8dfSeric char tobuf[MAXLINE-50]; /* text line of to people */ 71ee6bf8dfSeric char buf[MAXNAME]; 72ee6bf8dfSeric char tfrombuf[MAXNAME]; /* translated from person */ 73ee6bf8dfSeric extern bool checkcompat(); 74ee6bf8dfSeric extern ADDRESS *getctladdr(); 75ee6bf8dfSeric extern char *remotename(); 7625a99e2eSeric 7735490626Seric errno = 0; 78da2935e1Seric if (bitset(QDONTSEND, to->q_flags)) 795dfc646bSeric return (0); 8025a99e2eSeric 81912a731aSbostic /* unless interactive, try twice, over a minute */ 82912a731aSbostic if (OpMode == MD_DAEMON || OpMode == MD_SMTP) { 83912a731aSbostic _res.retrans = 30; 84912a731aSbostic _res.retry = 2; 85912a731aSbostic } 86912a731aSbostic 8751552439Seric m = to->q_mailer; 8851552439Seric host = to->q_host; 8951552439Seric 9025a99e2eSeric # ifdef DEBUG 916ef48975Seric if (tTd(10, 1)) 925dfc646bSeric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 9351552439Seric m->m_mno, host, to->q_user); 9425a99e2eSeric # endif DEBUG 95f3dbc832Seric 96f3dbc832Seric /* 97f3dbc832Seric ** If this mailer is expensive, and if we don't want to make 98f3dbc832Seric ** connections now, just mark these addresses and return. 99f3dbc832Seric ** This is useful if we want to batch connections to 100f3dbc832Seric ** reduce load. This will cause the messages to be 101f3dbc832Seric ** queued up, and a daemon will come along to send the 102f3dbc832Seric ** messages later. 103f3dbc832Seric ** This should be on a per-mailer basis. 104f3dbc832Seric */ 105f3dbc832Seric 10657fc6f17Seric if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) && 107317680d6Seric !Verbose) 108f3dbc832Seric { 109f3dbc832Seric for (; to != NULL; to = to->q_next) 110f4560e80Seric { 111f4560e80Seric if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m) 112f4560e80Seric continue; 113f3dbc832Seric to->q_flags |= QQUEUEUP|QDONTSEND; 114588cad61Seric e->e_to = to->q_paddr; 115eb238f8cSeric message(Arpa_Info, "queued"); 116eb238f8cSeric if (LogLevel > 4) 117eb238f8cSeric logdelivery("queued"); 118f4560e80Seric } 119588cad61Seric e->e_to = NULL; 120f3dbc832Seric return (0); 121f3dbc832Seric } 122f3dbc832Seric 12325a99e2eSeric /* 1245dfc646bSeric ** Do initial argv setup. 1255dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 1265dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 1275dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 1285dfc646bSeric ** code does not check for 'pv' overflow; this places a 1295dfc646bSeric ** manifest lower limit of 4 for MAXPV. 1303bea8136Seric ** The from address rewrite is expected to make 1313bea8136Seric ** the address relative to the other end. 1325dfc646bSeric */ 1335dfc646bSeric 13478442df3Seric /* rewrite from address, using rewriting rules */ 1359b6c17a6Seric expand("\001f", buf, &buf[sizeof buf - 1], e); 136ee6bf8dfSeric (void) strcpy(tfrombuf, remotename(buf, m, TRUE, TRUE)); 13778442df3Seric 138588cad61Seric define('g', tfrombuf, e); /* translated sender address */ 139588cad61Seric define('h', host, e); /* to host */ 1405dfc646bSeric Errors = 0; 1415dfc646bSeric pvp = pv; 1425dfc646bSeric *pvp++ = m->m_argv[0]; 1435dfc646bSeric 1445dfc646bSeric /* insert -f or -r flag as appropriate */ 14557fc6f17Seric if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 1465dfc646bSeric { 14757fc6f17Seric if (bitnset(M_FOPT, m->m_flags)) 1485dfc646bSeric *pvp++ = "-f"; 1495dfc646bSeric else 1505dfc646bSeric *pvp++ = "-r"; 1519b6c17a6Seric expand("\001g", buf, &buf[sizeof buf - 1], e); 1525dfc646bSeric *pvp++ = newstr(buf); 1535dfc646bSeric } 1545dfc646bSeric 1555dfc646bSeric /* 1565dfc646bSeric ** Append the other fixed parts of the argv. These run 1575dfc646bSeric ** up to the first entry containing "$u". There can only 1585dfc646bSeric ** be one of these, and there are only a few more slots 1595dfc646bSeric ** in the pv after it. 1605dfc646bSeric */ 1615dfc646bSeric 1625dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 1635dfc646bSeric { 1649b6c17a6Seric while ((p = index(p, '\001')) != NULL) 1655dfc646bSeric if (*++p == 'u') 1665dfc646bSeric break; 1675dfc646bSeric if (p != NULL) 1685dfc646bSeric break; 1695dfc646bSeric 1705dfc646bSeric /* this entry is safe -- go ahead and process it */ 171588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 1725dfc646bSeric *pvp++ = newstr(buf); 1735dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 1745dfc646bSeric { 1755dfc646bSeric syserr("Too many parameters to %s before $u", pv[0]); 1765dfc646bSeric return (-1); 1775dfc646bSeric } 1785dfc646bSeric } 179c579ef51Seric 18033db8731Seric /* 18133db8731Seric ** If we have no substitution for the user name in the argument 18233db8731Seric ** list, we know that we must supply the names otherwise -- and 18333db8731Seric ** SMTP is the answer!! 18433db8731Seric */ 18533db8731Seric 1865dfc646bSeric if (*mvp == NULL) 187c579ef51Seric { 188c579ef51Seric /* running SMTP */ 1892c7e1b8dSeric # ifdef SMTP 190c579ef51Seric clever = TRUE; 191c579ef51Seric *pvp = NULL; 1922c7e1b8dSeric # else SMTP 19333db8731Seric /* oops! we don't implement SMTP */ 1942c7e1b8dSeric syserr("SMTP style mailer"); 1952c7e1b8dSeric return (EX_SOFTWARE); 1962c7e1b8dSeric # endif SMTP 197c579ef51Seric } 1985dfc646bSeric 1995dfc646bSeric /* 2005dfc646bSeric ** At this point *mvp points to the argument with $u. We 2015dfc646bSeric ** run through our address list and append all the addresses 2025dfc646bSeric ** we can. If we run out of space, do not fret! We can 2035dfc646bSeric ** always send another copy later. 2045dfc646bSeric */ 2055dfc646bSeric 2065dfc646bSeric tobuf[0] = '\0'; 207588cad61Seric e->e_to = tobuf; 2086259796dSeric ctladdr = NULL; 2095dfc646bSeric for (; to != NULL; to = to->q_next) 2105dfc646bSeric { 2115dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 21257fc6f17Seric if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 2135dfc646bSeric break; 2145dfc646bSeric 2155dfc646bSeric /* if already sent or not for this host, don't send */ 216da2935e1Seric if (bitset(QDONTSEND, to->q_flags) || 217da2935e1Seric strcmp(to->q_host, host) != 0 || 218da2935e1Seric to->q_mailer != firstto->q_mailer) 2195dfc646bSeric continue; 2206259796dSeric 2214b22ea87Seric /* avoid overflowing tobuf */ 222588cad61Seric if (sizeof tobuf - (strlen(to->q_paddr) + strlen(tobuf) + 2) < 0) 2234b22ea87Seric break; 2244b22ea87Seric 225772e6e50Seric # ifdef DEBUG 2266ef48975Seric if (tTd(10, 1)) 227772e6e50Seric { 228772e6e50Seric printf("\nsend to "); 229772e6e50Seric printaddr(to, FALSE); 230772e6e50Seric } 231772e6e50Seric # endif DEBUG 232772e6e50Seric 2336259796dSeric /* compute effective uid/gid when sending */ 2347da1035fSeric if (to->q_mailer == ProgMailer) 2356259796dSeric ctladdr = getctladdr(to); 2366259796dSeric 2375dfc646bSeric user = to->q_user; 238588cad61Seric e->e_to = to->q_paddr; 2395dfc646bSeric to->q_flags |= QDONTSEND; 2405dfc646bSeric 2415dfc646bSeric /* 2425dfc646bSeric ** Check to see that these people are allowed to 2435dfc646bSeric ** talk to each other. 2442a6e0786Seric */ 2452a6e0786Seric 24669582d2fSeric if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 24769582d2fSeric { 24869582d2fSeric NoReturn = TRUE; 249672bec4aSeric usrerr("Message is too large; %ld bytes max", m->m_maxsize); 25069582d2fSeric giveresponse(EX_UNAVAILABLE, m, e); 25169582d2fSeric continue; 25269582d2fSeric } 2532a6e0786Seric if (!checkcompat(to)) 2545dfc646bSeric { 255198d9be0Seric giveresponse(EX_UNAVAILABLE, m, e); 2565dfc646bSeric continue; 2575dfc646bSeric } 2582a6e0786Seric 2592a6e0786Seric /* 2609ec9501bSeric ** Strip quote bits from names if the mailer is dumb 2619ec9501bSeric ** about them. 26225a99e2eSeric */ 26325a99e2eSeric 26457fc6f17Seric if (bitnset(M_STRIPQ, m->m_flags)) 26525a99e2eSeric { 2669ec9501bSeric stripquotes(user, TRUE); 2679ec9501bSeric stripquotes(host, TRUE); 2689ec9501bSeric } 2699ec9501bSeric else 2709ec9501bSeric { 2719ec9501bSeric stripquotes(user, FALSE); 2729ec9501bSeric stripquotes(host, FALSE); 27325a99e2eSeric } 27425a99e2eSeric 275cdb828c5Seric /* hack attack -- delivermail compatibility */ 276cdb828c5Seric if (m == ProgMailer && *user == '|') 277cdb828c5Seric user++; 278cdb828c5Seric 27925a99e2eSeric /* 2803efaed6eSeric ** If an error message has already been given, don't 2813efaed6eSeric ** bother to send to this address. 2823efaed6eSeric ** 2833efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 2843efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 2853efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 2863efaed6eSeric */ 2873efaed6eSeric 2886cae517dSeric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 2893efaed6eSeric continue; 2903efaed6eSeric 291f2fec898Seric /* save statistics.... */ 292588cad61Seric markstats(e, to); 293f2fec898Seric 2943efaed6eSeric /* 29525a99e2eSeric ** See if this user name is "special". 29625a99e2eSeric ** If the user name has a slash in it, assume that this 29751552439Seric ** is a file -- send it off without further ado. Note 29851552439Seric ** that this type of addresses is not processed along 29951552439Seric ** with the others, so we fudge on the To person. 30025a99e2eSeric */ 30125a99e2eSeric 3027da1035fSeric if (m == LocalMailer) 30325a99e2eSeric { 304a49f24c0Seric if (user[0] == '/') 30525a99e2eSeric { 3065826d9d3Seric rcode = mailfile(user, getctladdr(to)); 307198d9be0Seric giveresponse(rcode, m, e); 3085dfc646bSeric continue; 30925a99e2eSeric } 31025a99e2eSeric } 31125a99e2eSeric 31213bbc08cSeric /* 31313bbc08cSeric ** Address is verified -- add this user to mailer 31413bbc08cSeric ** argv, and add it to the print list of recipients. 31513bbc08cSeric */ 31613bbc08cSeric 317508daeccSeric /* link together the chain of recipients */ 318508daeccSeric to->q_tchain = tochain; 319508daeccSeric tochain = to; 320508daeccSeric 3215dfc646bSeric /* create list of users for error messages */ 322db8841e9Seric (void) strcat(tobuf, ","); 323db8841e9Seric (void) strcat(tobuf, to->q_paddr); 324588cad61Seric define('u', user, e); /* to user */ 325588cad61Seric define('z', to->q_home, e); /* user's home */ 3265dfc646bSeric 327c579ef51Seric /* 328508daeccSeric ** Expand out this user into argument list. 329c579ef51Seric */ 330c579ef51Seric 331508daeccSeric if (!clever) 332c579ef51Seric { 333588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 3345dfc646bSeric *pvp++ = newstr(buf); 3355dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 3365dfc646bSeric { 3375dfc646bSeric /* allow some space for trailing parms */ 3385dfc646bSeric break; 3395dfc646bSeric } 3405dfc646bSeric } 341c579ef51Seric } 3425dfc646bSeric 343145b49b1Seric /* see if any addresses still exist */ 344145b49b1Seric if (tobuf[0] == '\0') 345c579ef51Seric { 346588cad61Seric define('g', (char *) NULL, e); 347145b49b1Seric return (0); 348c579ef51Seric } 349145b49b1Seric 3505dfc646bSeric /* print out messages as full list */ 35163780dbdSeric e->e_to = tobuf + 1; 3525dfc646bSeric 3535dfc646bSeric /* 3545dfc646bSeric ** Fill out any parameters after the $u parameter. 3555dfc646bSeric */ 3565dfc646bSeric 357c579ef51Seric while (!clever && *++mvp != NULL) 3585dfc646bSeric { 359588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 3605dfc646bSeric *pvp++ = newstr(buf); 3615dfc646bSeric if (pvp >= &pv[MAXPV]) 3625dfc646bSeric syserr("deliver: pv overflow after $u for %s", pv[0]); 3635dfc646bSeric } 3645dfc646bSeric *pvp++ = NULL; 3655dfc646bSeric 36625a99e2eSeric /* 36725a99e2eSeric ** Call the mailer. 3686328bdf7Seric ** The argument vector gets built, pipes 36925a99e2eSeric ** are created as necessary, and we fork & exec as 3706328bdf7Seric ** appropriate. 371c579ef51Seric ** If we are running SMTP, we just need to clean up. 37225a99e2eSeric */ 37325a99e2eSeric 3746259796dSeric if (ctladdr == NULL) 375588cad61Seric ctladdr = &e->e_from; 376912a731aSbostic _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 3772c7e1b8dSeric #ifdef SMTP 378911693bfSbostic if (clever) { 379911693bfSbostic expand("\001w", buf, &buf[sizeof(buf) - 1], e); 380911693bfSbostic rcode = EX_OK; 381911693bfSbostic if (host[0] == '[') { 38260bcc2d9Sbostic Nmx = 1; 38360bcc2d9Sbostic MxHosts[0] = host; 384912a731aSbostic } else 385912a731aSbostic Nmx = getmxrr(host, MxHosts, buf, &rcode); 386912a731aSbostic if (Nmx >= 0) { 387912a731aSbostic message(Arpa_Info, "Connecting to %s (%s)...", 388912a731aSbostic MxHosts[0], m->m_name); 389912a731aSbostic if ((rcode = smtpinit(m, pv)) == EX_OK) { 390588cad61Seric /* send the recipient list */ 39163780dbdSeric tobuf[0] = '\0'; 392911693bfSbostic for (to = tochain; to; to = to->q_tchain) { 393911693bfSbostic register int i; 394911693bfSbostic register char *t = tobuf; 395588cad61Seric 39663780dbdSeric e->e_to = to->q_paddr; 397912a731aSbostic if ((i = smtprcpt(to, m)) != EX_OK) { 39883b7ddc9Seric markfailure(e, to, i); 399198d9be0Seric giveresponse(i, m, e); 40063780dbdSeric } 401911693bfSbostic else { 402911693bfSbostic *t++ = ','; 403911693bfSbostic for (p = to->q_paddr; *p; *t++ = *p++); 404588cad61Seric } 405588cad61Seric } 406588cad61Seric 40763780dbdSeric /* now send the data */ 40863780dbdSeric if (tobuf[0] == '\0') 40963780dbdSeric e->e_to = NULL; 410911693bfSbostic else { 41163780dbdSeric e->e_to = tobuf + 1; 41277b52738Seric rcode = smtpdata(m, e); 41363780dbdSeric } 41463780dbdSeric 41563780dbdSeric /* now close the connection */ 416a294c4b0Seric smtpquit(m); 41763780dbdSeric } 418c579ef51Seric } 419912a731aSbostic } 420c579ef51Seric else 421911693bfSbostic #endif /* SMTP */ 422a05b3449Sbostic { 423912a731aSbostic message(Arpa_Info, "Connecting to %s (%s)...", host, m->m_name); 42477b52738Seric rcode = sendoff(e, m, pv, ctladdr); 425a05b3449Sbostic } 426912a731aSbostic _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 4275dfc646bSeric 428c77d1c25Seric /* 42963780dbdSeric ** Do final status disposal. 43063780dbdSeric ** We check for something in tobuf for the SMTP case. 431c77d1c25Seric ** If we got a temporary failure, arrange to queue the 432c77d1c25Seric ** addressees. 433c77d1c25Seric */ 434c77d1c25Seric 43563780dbdSeric if (tobuf[0] != '\0') 436198d9be0Seric giveresponse(rcode, m, e); 43763780dbdSeric if (rcode != EX_OK) 438772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 43983b7ddc9Seric markfailure(e, to, rcode); 440c77d1c25Seric 44135490626Seric errno = 0; 442588cad61Seric define('g', (char *) NULL, e); 4435826d9d3Seric return (rcode); 44425a99e2eSeric } 4455dfc646bSeric /* 44683b7ddc9Seric ** MARKFAILURE -- mark a failure on a specific address. 44783b7ddc9Seric ** 44883b7ddc9Seric ** Parameters: 44983b7ddc9Seric ** e -- the envelope we are sending. 45083b7ddc9Seric ** q -- the address to mark. 45183b7ddc9Seric ** rcode -- the code signifying the particular failure. 45283b7ddc9Seric ** 45383b7ddc9Seric ** Returns: 45483b7ddc9Seric ** none. 45583b7ddc9Seric ** 45683b7ddc9Seric ** Side Effects: 45783b7ddc9Seric ** marks the address (and possibly the envelope) with the 45883b7ddc9Seric ** failure so that an error will be returned or 45983b7ddc9Seric ** the message will be queued, as appropriate. 46083b7ddc9Seric */ 46183b7ddc9Seric 46283b7ddc9Seric markfailure(e, q, rcode) 46383b7ddc9Seric register ENVELOPE *e; 46483b7ddc9Seric register ADDRESS *q; 46583b7ddc9Seric int rcode; 46683b7ddc9Seric { 46783b7ddc9Seric if (rcode == EX_OK) 46883b7ddc9Seric return; 46983b7ddc9Seric else if (rcode != EX_TEMPFAIL) 47083b7ddc9Seric q->q_flags |= QBADADDR; 47183b7ddc9Seric else if (curtime() > e->e_ctime + TimeOut) 47283b7ddc9Seric { 47383b7ddc9Seric extern char *pintvl(); 474198d9be0Seric char buf[MAXLINE]; 47583b7ddc9Seric 47683b7ddc9Seric if (!bitset(EF_TIMEOUT, e->e_flags)) 477198d9be0Seric { 478198d9be0Seric (void) sprintf(buf, "Cannot send message for %s", 47983b7ddc9Seric pintvl(TimeOut, FALSE)); 480198d9be0Seric if (e->e_message != NULL) 481198d9be0Seric free(e->e_message); 482198d9be0Seric e->e_message = newstr(buf); 483198d9be0Seric message(Arpa_Info, buf); 484198d9be0Seric } 48583b7ddc9Seric q->q_flags |= QBADADDR; 48683b7ddc9Seric e->e_flags |= EF_TIMEOUT; 48783b7ddc9Seric } 48883b7ddc9Seric else 48983b7ddc9Seric q->q_flags |= QQUEUEUP; 49083b7ddc9Seric } 49183b7ddc9Seric /* 49232d19d43Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 49332d19d43Seric ** 49432d19d43Seric ** This MUST be a macro, since after a vfork we are running 49532d19d43Seric ** two processes on the same stack!!! 49632d19d43Seric ** 49732d19d43Seric ** Parameters: 49832d19d43Seric ** none. 49932d19d43Seric ** 50032d19d43Seric ** Returns: 50132d19d43Seric ** From a macro??? You've got to be kidding! 50232d19d43Seric ** 50332d19d43Seric ** Side Effects: 50432d19d43Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 50532d19d43Seric ** pid of child in parent, zero in child. 50632d19d43Seric ** -1 on unrecoverable error. 50732d19d43Seric ** 50832d19d43Seric ** Notes: 50932d19d43Seric ** I'm awfully sorry this looks so awful. That's 51032d19d43Seric ** vfork for you..... 51132d19d43Seric */ 51232d19d43Seric 51332d19d43Seric # define NFORKTRIES 5 5144300ddf0Seric # ifdef VMUNIX 51532d19d43Seric # define XFORK vfork 5164300ddf0Seric # else VMUNIX 51732d19d43Seric # define XFORK fork 5184300ddf0Seric # endif VMUNIX 51932d19d43Seric 52032d19d43Seric # define DOFORK(fORKfN) \ 52132d19d43Seric {\ 52232d19d43Seric register int i;\ 52332d19d43Seric \ 52411799049Seric for (i = NFORKTRIES; --i >= 0; )\ 52532d19d43Seric {\ 52632d19d43Seric pid = fORKfN();\ 52732d19d43Seric if (pid >= 0)\ 52832d19d43Seric break;\ 52911799049Seric if (i > 0)\ 5306c4635f6Seric sleep((unsigned) NFORKTRIES - i);\ 53132d19d43Seric }\ 53232d19d43Seric } 53332d19d43Seric /* 5342ed72599Seric ** DOFORK -- simple fork interface to DOFORK. 5352ed72599Seric ** 5362ed72599Seric ** Parameters: 5372ed72599Seric ** none. 5382ed72599Seric ** 5392ed72599Seric ** Returns: 5402ed72599Seric ** pid of child in parent. 5412ed72599Seric ** zero in child. 5422ed72599Seric ** -1 on error. 5432ed72599Seric ** 5442ed72599Seric ** Side Effects: 5452ed72599Seric ** returns twice, once in parent and once in child. 5462ed72599Seric */ 5472ed72599Seric 5482ed72599Seric dofork() 5492ed72599Seric { 5502ed72599Seric register int pid; 5512ed72599Seric 5522ed72599Seric DOFORK(fork); 5532ed72599Seric return (pid); 5542ed72599Seric } 5552ed72599Seric /* 5565dfc646bSeric ** SENDOFF -- send off call to mailer & collect response. 5575dfc646bSeric ** 5585dfc646bSeric ** Parameters: 559588cad61Seric ** e -- the envelope to mail. 5605dfc646bSeric ** m -- mailer descriptor. 5615dfc646bSeric ** pvp -- parameter vector to send to it. 5626259796dSeric ** ctladdr -- an address pointer controlling the 5636259796dSeric ** user/groupid etc. of the mailer. 5645dfc646bSeric ** 5655dfc646bSeric ** Returns: 5665dfc646bSeric ** exit status of mailer. 5675dfc646bSeric ** 5685dfc646bSeric ** Side Effects: 5695dfc646bSeric ** none. 5705dfc646bSeric */ 571912a731aSbostic static 57277b52738Seric sendoff(e, m, pvp, ctladdr) 573588cad61Seric register ENVELOPE *e; 574588cad61Seric MAILER *m; 5755dfc646bSeric char **pvp; 5766259796dSeric ADDRESS *ctladdr; 5775dfc646bSeric { 578c579ef51Seric auto FILE *mfile; 579c579ef51Seric auto FILE *rfile; 5805dfc646bSeric register int i; 581c579ef51Seric int pid; 582c579ef51Seric 583c579ef51Seric /* 584c579ef51Seric ** Create connection to mailer. 585c579ef51Seric */ 586c579ef51Seric 587c579ef51Seric pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 588c579ef51Seric if (pid < 0) 589c579ef51Seric return (-1); 590c579ef51Seric 591c579ef51Seric /* 592c579ef51Seric ** Format and send message. 593c579ef51Seric */ 594c579ef51Seric 59577b52738Seric putfromline(mfile, m); 59677b52738Seric (*e->e_puthdr)(mfile, m, e); 59777b52738Seric putline("\n", mfile, m); 59877b52738Seric (*e->e_putbody)(mfile, m, e); 599c579ef51Seric (void) fclose(mfile); 600c579ef51Seric 601c579ef51Seric i = endmailer(pid, pvp[0]); 602bc6e2962Seric 603bc6e2962Seric /* arrange a return receipt if requested */ 60457fc6f17Seric if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags)) 605bc6e2962Seric { 606588cad61Seric e->e_flags |= EF_SENDRECEIPT; 607bc6e2962Seric /* do we want to send back more info? */ 608bc6e2962Seric } 609bc6e2962Seric 610c579ef51Seric return (i); 611c579ef51Seric } 612c579ef51Seric /* 613c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 614c579ef51Seric ** 615c579ef51Seric ** We should never get fatal errors (e.g., segmentation 616c579ef51Seric ** violation), so we report those specially. For other 617c579ef51Seric ** errors, we choose a status message (into statmsg), 618c579ef51Seric ** and if it represents an error, we print it. 619c579ef51Seric ** 620c579ef51Seric ** Parameters: 621c579ef51Seric ** pid -- pid of mailer. 622c579ef51Seric ** name -- name of mailer (for error messages). 623c579ef51Seric ** 624c579ef51Seric ** Returns: 625c579ef51Seric ** exit code of mailer. 626c579ef51Seric ** 627c579ef51Seric ** Side Effects: 628c579ef51Seric ** none. 629c579ef51Seric */ 630c579ef51Seric 631c579ef51Seric endmailer(pid, name) 632c579ef51Seric int pid; 633c579ef51Seric char *name; 634c579ef51Seric { 635588cad61Seric int st; 636c579ef51Seric 63733db8731Seric /* in the IPC case there is nothing to wait for */ 63833db8731Seric if (pid == 0) 63933db8731Seric return (EX_OK); 64033db8731Seric 64133db8731Seric /* wait for the mailer process to die and collect status */ 642588cad61Seric st = waitfor(pid); 643588cad61Seric if (st == -1) 64478de67c1Seric { 645588cad61Seric syserr("endmailer %s: wait", name); 646588cad61Seric return (EX_SOFTWARE); 647c579ef51Seric } 64833db8731Seric 64933db8731Seric /* see if it died a horrid death */ 650c579ef51Seric if ((st & 0377) != 0) 651c579ef51Seric { 6525f73204aSeric syserr("mailer %s died with signal %o", name, st); 6535f73204aSeric ExitStat = EX_TEMPFAIL; 6545f73204aSeric return (EX_TEMPFAIL); 655c579ef51Seric } 65633db8731Seric 65733db8731Seric /* normal death -- return status */ 658588cad61Seric st = (st >> 8) & 0377; 659588cad61Seric return (st); 660c579ef51Seric } 661c579ef51Seric /* 662c579ef51Seric ** OPENMAILER -- open connection to mailer. 663c579ef51Seric ** 664c579ef51Seric ** Parameters: 665c579ef51Seric ** m -- mailer descriptor. 666c579ef51Seric ** pvp -- parameter vector to pass to mailer. 667c579ef51Seric ** ctladdr -- controlling address for user. 668c579ef51Seric ** clever -- create a full duplex connection. 669c579ef51Seric ** pmfile -- pointer to mfile (to mailer) connection. 670c579ef51Seric ** prfile -- pointer to rfile (from mailer) connection. 671c579ef51Seric ** 672c579ef51Seric ** Returns: 67333db8731Seric ** pid of mailer ( > 0 ). 674c579ef51Seric ** -1 on error. 67533db8731Seric ** zero on an IPC connection. 676c579ef51Seric ** 677c579ef51Seric ** Side Effects: 678c579ef51Seric ** creates a mailer in a subprocess. 679c579ef51Seric */ 680c579ef51Seric 681c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 682588cad61Seric MAILER *m; 683c579ef51Seric char **pvp; 684c579ef51Seric ADDRESS *ctladdr; 685c579ef51Seric bool clever; 686c579ef51Seric FILE **pmfile; 687c579ef51Seric FILE **prfile; 688c579ef51Seric { 6895dfc646bSeric int pid; 690f8952a83Seric int mpvect[2]; 691c579ef51Seric int rpvect[2]; 6925dfc646bSeric FILE *mfile; 693c579ef51Seric FILE *rfile; 6945dfc646bSeric extern FILE *fdopen(); 6955dfc646bSeric 6965dfc646bSeric # ifdef DEBUG 6976ef48975Seric if (tTd(11, 1)) 6985dfc646bSeric { 6998c57e552Seric printf("openmailer:"); 7005dfc646bSeric printav(pvp); 7015dfc646bSeric } 7025dfc646bSeric # endif DEBUG 70335490626Seric errno = 0; 7045dfc646bSeric 705ef66a9d0Seric CurHostName = m->m_mailer; 706ef66a9d0Seric 70733db8731Seric /* 70833db8731Seric ** Deal with the special case of mail handled through an IPC 70933db8731Seric ** connection. 71033db8731Seric ** In this case we don't actually fork. We must be 71133db8731Seric ** running SMTP for this to work. We will return a 71233db8731Seric ** zero pid to indicate that we are running IPC. 713e7c1bd78Seric ** We also handle a debug version that just talks to stdin/out. 71433db8731Seric */ 71533db8731Seric 716e7c1bd78Seric #ifdef DEBUG 717e7c1bd78Seric /* check for Local Person Communication -- not for mortals!!! */ 718e7c1bd78Seric if (strcmp(m->m_mailer, "[LPC]") == 0) 719e7c1bd78Seric { 720e7c1bd78Seric *pmfile = stdout; 721e7c1bd78Seric *prfile = stdin; 722e7c1bd78Seric return (0); 723e7c1bd78Seric } 724e7c1bd78Seric #endif DEBUG 725e7c1bd78Seric 72633db8731Seric if (strcmp(m->m_mailer, "[IPC]") == 0) 72733db8731Seric { 7285f73204aSeric #ifdef HOSTINFO 7295f73204aSeric register STAB *st; 7305f73204aSeric extern STAB *stab(); 7315f73204aSeric #endif HOSTINFO 732588cad61Seric #ifdef DAEMON 733ebc61751Sbloom register int i, j; 7341277f9a8Seric register u_short port; 73533db8731Seric 736ef66a9d0Seric CurHostName = pvp[1]; 73733db8731Seric if (!clever) 73833db8731Seric syserr("non-clever IPC"); 73993b6e3cfSeric if (pvp[2] != NULL) 7401277f9a8Seric port = atoi(pvp[2]); 74193b6e3cfSeric else 7421277f9a8Seric port = 0; 743f1853fd7Seric for (j = 0; j < Nmx; j++) 744ebc61751Sbloom { 745f1853fd7Seric CurHostName = MxHosts[j]; 7465f73204aSeric #ifdef HOSTINFO 7475f73204aSeric /* see if we have already determined that this host is fried */ 748f1853fd7Seric st = stab(MxHosts[j], ST_HOST, ST_FIND); 749912a731aSbostic if (st == NULL || st->s_host.ho_exitstat == EX_OK) { 750912a731aSbostic if (j > 1) 751912a731aSbostic message(Arpa_Info, 752912a731aSbostic "Connecting to %s (%s)...", 753912a731aSbostic MxHosts[j], m->m_name); 754f1853fd7Seric i = makeconnection(MxHosts[j], port, pmfile, prfile); 755912a731aSbostic } 7565f73204aSeric else 757ef66a9d0Seric { 7585f73204aSeric i = st->s_host.ho_exitstat; 759ef66a9d0Seric errno = st->s_host.ho_errno; 760ef66a9d0Seric } 7615f73204aSeric #else HOSTINFO 762f1853fd7Seric i = makeconnection(MxHosts[j], port, pmfile, prfile); 7635f73204aSeric #endif HOSTINFO 76433db8731Seric if (i != EX_OK) 765ed854c7bSeric { 7665f73204aSeric #ifdef HOSTINFO 7675f73204aSeric /* enter status of this host */ 7685f73204aSeric if (st == NULL) 769f1853fd7Seric st = stab(MxHosts[j], ST_HOST, ST_ENTER); 7705f73204aSeric st->s_host.ho_exitstat = i; 7715f73204aSeric st->s_host.ho_errno = errno; 7725f73204aSeric #endif HOSTINFO 773ed854c7bSeric ExitStat = i; 774ebc61751Sbloom continue; 775ed854c7bSeric } 77633db8731Seric else 77733db8731Seric return (0); 778ebc61751Sbloom } 779ebc61751Sbloom return (-1); 780588cad61Seric #else DAEMON 781588cad61Seric syserr("openmailer: no IPC"); 782588cad61Seric return (-1); 78333db8731Seric #endif DAEMON 784588cad61Seric } 78533db8731Seric 7866328bdf7Seric /* create a pipe to shove the mail through */ 787f8952a83Seric if (pipe(mpvect) < 0) 78825a99e2eSeric { 789588cad61Seric syserr("openmailer: pipe (to mailer)"); 79025a99e2eSeric return (-1); 79125a99e2eSeric } 792c579ef51Seric 7932c7e1b8dSeric #ifdef SMTP 794c579ef51Seric /* if this mailer speaks smtp, create a return pipe */ 795c579ef51Seric if (clever && pipe(rpvect) < 0) 796c579ef51Seric { 797588cad61Seric syserr("openmailer: pipe (from mailer)"); 798c579ef51Seric (void) close(mpvect[0]); 799c579ef51Seric (void) close(mpvect[1]); 800c579ef51Seric return (-1); 801c579ef51Seric } 8022c7e1b8dSeric #endif SMTP 803c579ef51Seric 80433db8731Seric /* 80533db8731Seric ** Actually fork the mailer process. 80633db8731Seric ** DOFORK is clever about retrying. 8076984bfddSeric ** 8086984bfddSeric ** Dispose of SIGCHLD signal catchers that may be laying 8096984bfddSeric ** around so that endmail will get it. 81033db8731Seric */ 81133db8731Seric 8129a6a5f55Seric if (CurEnv->e_xfp != NULL) 8139a6a5f55Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 814588cad61Seric (void) fflush(stdout); 8156984bfddSeric # ifdef SIGCHLD 8166984bfddSeric (void) signal(SIGCHLD, SIG_DFL); 8176984bfddSeric # endif SIGCHLD 81832d19d43Seric DOFORK(XFORK); 819f129ec7dSeric /* pid is set by DOFORK */ 82025a99e2eSeric if (pid < 0) 82125a99e2eSeric { 82233db8731Seric /* failure */ 823588cad61Seric syserr("openmailer: cannot fork"); 824f8952a83Seric (void) close(mpvect[0]); 825f8952a83Seric (void) close(mpvect[1]); 826588cad61Seric #ifdef SMTP 827c579ef51Seric if (clever) 828c579ef51Seric { 829c579ef51Seric (void) close(rpvect[0]); 830c579ef51Seric (void) close(rpvect[1]); 831c579ef51Seric } 832588cad61Seric #endif SMTP 83325a99e2eSeric return (-1); 83425a99e2eSeric } 83525a99e2eSeric else if (pid == 0) 83625a99e2eSeric { 83713088b9fSeric int i; 8385f73204aSeric extern int DtableSize; 83913088b9fSeric 84025a99e2eSeric /* child -- set up input & exec mailer */ 84103ab8e55Seric /* make diagnostic output be standard output */ 8428f0e7860Seric (void) signal(SIGINT, SIG_IGN); 8438f0e7860Seric (void) signal(SIGHUP, SIG_IGN); 8440984da9fSeric (void) signal(SIGTERM, SIG_DFL); 845f8952a83Seric 846f8952a83Seric /* arrange to filter standard & diag output of command */ 847c579ef51Seric if (clever) 848c579ef51Seric { 849c579ef51Seric (void) close(rpvect[0]); 850c579ef51Seric (void) close(1); 851c579ef51Seric (void) dup(rpvect[1]); 852c579ef51Seric (void) close(rpvect[1]); 853c579ef51Seric } 854276723a8Seric else if (OpMode == MD_SMTP || HoldErrs) 855f8952a83Seric { 856588cad61Seric /* put mailer output in transcript */ 857f8952a83Seric (void) close(1); 8589a6a5f55Seric (void) dup(fileno(CurEnv->e_xfp)); 859f8952a83Seric } 860db8841e9Seric (void) close(2); 861db8841e9Seric (void) dup(1); 862f8952a83Seric 863f8952a83Seric /* arrange to get standard input */ 864f8952a83Seric (void) close(mpvect[1]); 865db8841e9Seric (void) close(0); 866f8952a83Seric if (dup(mpvect[0]) < 0) 86725a99e2eSeric { 86825a99e2eSeric syserr("Cannot dup to zero!"); 869a590b978Seric _exit(EX_OSERR); 87025a99e2eSeric } 871f8952a83Seric (void) close(mpvect[0]); 87257fc6f17Seric if (!bitnset(M_RESTR, m->m_flags)) 8730984da9fSeric { 87453e3fa05Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 875e36b99e2Seric { 876e36b99e2Seric (void) setgid(DefGid); 877e36b99e2Seric (void) setuid(DefUid); 878e36b99e2Seric } 879e36b99e2Seric else 88069f29479Seric { 881e36b99e2Seric (void) setgid(ctladdr->q_gid); 882e36b99e2Seric (void) setuid(ctladdr->q_uid); 88369f29479Seric } 8840984da9fSeric } 885588cad61Seric 88613088b9fSeric /* arrange for all the files to be closed */ 8875f73204aSeric for (i = 3; i < DtableSize; i++) 88813088b9fSeric (void) ioctl(i, FIOCLEX, 0); 88933db8731Seric 89033db8731Seric /* try to execute the mailer */ 8915df317aaSeric execve(m->m_mailer, pvp, UserEnviron); 89213088b9fSeric syserr("Cannot exec %s", m->m_mailer); 8935f73204aSeric if (m == LocalMailer || errno == EIO || errno == EAGAIN || 8945f73204aSeric errno == ENOMEM || errno == EPROCLIM) 89555f33c03Seric _exit(EX_TEMPFAIL); 89655f33c03Seric else 897a590b978Seric _exit(EX_UNAVAILABLE); 89825a99e2eSeric } 89925a99e2eSeric 900f8952a83Seric /* 901c579ef51Seric ** Set up return value. 902f8952a83Seric */ 903f8952a83Seric 904f8952a83Seric (void) close(mpvect[0]); 905f8952a83Seric mfile = fdopen(mpvect[1], "w"); 906c579ef51Seric if (clever) 90725a99e2eSeric { 908c579ef51Seric (void) close(rpvect[1]); 909c579ef51Seric rfile = fdopen(rpvect[0], "r"); 91025a99e2eSeric } 911c579ef51Seric 912c579ef51Seric *pmfile = mfile; 913c579ef51Seric *prfile = rfile; 914c579ef51Seric 915c579ef51Seric return (pid); 91625a99e2eSeric } 91725a99e2eSeric /* 91825a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 91925a99e2eSeric ** 92025a99e2eSeric ** Parameters: 92125a99e2eSeric ** stat -- the status code from the mailer (high byte 92225a99e2eSeric ** only; core dumps must have been taken care of 92325a99e2eSeric ** already). 92425a99e2eSeric ** m -- the mailer descriptor for this mailer. 92525a99e2eSeric ** 92625a99e2eSeric ** Returns: 927db8841e9Seric ** none. 92825a99e2eSeric ** 92925a99e2eSeric ** Side Effects: 930c1f9df2cSeric ** Errors may be incremented. 93125a99e2eSeric ** ExitStat may be set. 93225a99e2eSeric */ 93325a99e2eSeric 934198d9be0Seric giveresponse(stat, m, e) 93525a99e2eSeric int stat; 936588cad61Seric register MAILER *m; 937198d9be0Seric ENVELOPE *e; 93825a99e2eSeric { 93925a99e2eSeric register char *statmsg; 94025a99e2eSeric extern char *SysExMsg[]; 94125a99e2eSeric register int i; 9428f22b66bSbostic extern int N_SysEx, h_errno; 943198d9be0Seric char buf[MAXLINE]; 94425a99e2eSeric 9457d1fc79dSeric #ifdef lint 9467d1fc79dSeric if (m == NULL) 9477d1fc79dSeric return; 9487d1fc79dSeric #endif lint 9497d1fc79dSeric 95013bbc08cSeric /* 95113bbc08cSeric ** Compute status message from code. 95213bbc08cSeric */ 95313bbc08cSeric 95425a99e2eSeric i = stat - EX__BASE; 955588cad61Seric if (stat == 0) 956588cad61Seric statmsg = "250 Sent"; 957588cad61Seric else if (i < 0 || i > N_SysEx) 958588cad61Seric { 959588cad61Seric (void) sprintf(buf, "554 unknown mailer error %d", stat); 960588cad61Seric stat = EX_UNAVAILABLE; 961588cad61Seric statmsg = buf; 962588cad61Seric } 963198d9be0Seric else if (stat == EX_TEMPFAIL) 964198d9be0Seric { 9658557d168Seric (void) strcpy(buf, SysExMsg[i]); 966f28da541Smiriam if (h_errno == TRY_AGAIN) 967f28da541Smiriam { 968f28da541Smiriam extern char *errstring(); 969f28da541Smiriam 970f28da541Smiriam statmsg = errstring(h_errno+MAX_ERRNO); 971f28da541Smiriam } 972f28da541Smiriam else 973f28da541Smiriam { 9748557d168Seric if (errno != 0) 975198d9be0Seric { 97687c9b3e7Seric extern char *errstring(); 9778557d168Seric 978d87e85f3Seric statmsg = errstring(errno); 979d87e85f3Seric } 980d87e85f3Seric else 981d87e85f3Seric { 982d87e85f3Seric #ifdef SMTP 983d87e85f3Seric extern char SmtpError[]; 984d87e85f3Seric 985d87e85f3Seric statmsg = SmtpError; 986d87e85f3Seric #else SMTP 987d87e85f3Seric statmsg = NULL; 988d87e85f3Seric #endif SMTP 989d87e85f3Seric } 990f28da541Smiriam } 991d87e85f3Seric if (statmsg != NULL && statmsg[0] != '\0') 992d87e85f3Seric { 99387c9b3e7Seric (void) strcat(buf, ": "); 994d87e85f3Seric (void) strcat(buf, statmsg); 9958557d168Seric } 996198d9be0Seric statmsg = buf; 997198d9be0Seric } 99825a99e2eSeric else 999d87e85f3Seric { 100025a99e2eSeric statmsg = SysExMsg[i]; 1001d87e85f3Seric } 1002588cad61Seric 1003588cad61Seric /* 1004588cad61Seric ** Print the message as appropriate 1005588cad61Seric */ 1006588cad61Seric 1007198d9be0Seric if (stat == EX_OK || stat == EX_TEMPFAIL) 10085826d9d3Seric message(Arpa_Info, &statmsg[4]); 100925a99e2eSeric else 101025a99e2eSeric { 1011c1f9df2cSeric Errors++; 10125826d9d3Seric usrerr(statmsg); 101325a99e2eSeric } 101425a99e2eSeric 101525a99e2eSeric /* 101625a99e2eSeric ** Final cleanup. 101725a99e2eSeric ** Log a record of the transaction. Compute the new 101825a99e2eSeric ** ExitStat -- if we already had an error, stick with 101925a99e2eSeric ** that. 102025a99e2eSeric */ 102125a99e2eSeric 102261f5a1d4Seric if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2)) 1023eb238f8cSeric logdelivery(&statmsg[4]); 1024eb238f8cSeric 1025eb238f8cSeric if (stat != EX_TEMPFAIL) 1026eb238f8cSeric setstat(stat); 1027198d9be0Seric if (stat != EX_OK) 1028198d9be0Seric { 1029198d9be0Seric if (e->e_message != NULL) 1030198d9be0Seric free(e->e_message); 1031198d9be0Seric e->e_message = newstr(&statmsg[4]); 1032198d9be0Seric } 10338557d168Seric errno = 0; 1034f28da541Smiriam h_errno = 0; 1035eb238f8cSeric } 1036eb238f8cSeric /* 1037eb238f8cSeric ** LOGDELIVERY -- log the delivery in the system log 1038eb238f8cSeric ** 1039eb238f8cSeric ** Parameters: 1040eb238f8cSeric ** stat -- the message to print for the status 1041eb238f8cSeric ** 1042eb238f8cSeric ** Returns: 1043eb238f8cSeric ** none 1044eb238f8cSeric ** 1045eb238f8cSeric ** Side Effects: 1046eb238f8cSeric ** none 1047eb238f8cSeric */ 1048eb238f8cSeric 1049eb238f8cSeric logdelivery(stat) 1050eb238f8cSeric char *stat; 10515cf56be3Seric { 10525cf56be3Seric extern char *pintvl(); 10535cf56be3Seric 1054eb238f8cSeric # ifdef LOG 10555cf56be3Seric syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id, 1056eb238f8cSeric CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), stat); 105725a99e2eSeric # endif LOG 105825a99e2eSeric } 105925a99e2eSeric /* 106051552439Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 106125a99e2eSeric ** 106251552439Seric ** This can be made an arbitrary message separator by changing $l 106351552439Seric ** 10649b6c17a6Seric ** One of the ugliest hacks seen by human eyes is contained herein: 10659b6c17a6Seric ** UUCP wants those stupid "remote from <host>" lines. Why oh why 10669b6c17a6Seric ** does a well-meaning programmer such as myself have to deal with 10679b6c17a6Seric ** this kind of antique garbage???? 106825a99e2eSeric ** 106925a99e2eSeric ** Parameters: 107051552439Seric ** fp -- the file to output to. 107151552439Seric ** m -- the mailer describing this entry. 107225a99e2eSeric ** 107325a99e2eSeric ** Returns: 107451552439Seric ** none 107525a99e2eSeric ** 107625a99e2eSeric ** Side Effects: 107751552439Seric ** outputs some text to fp. 107825a99e2eSeric */ 107925a99e2eSeric 108077b52738Seric putfromline(fp, m) 108151552439Seric register FILE *fp; 108251552439Seric register MAILER *m; 108325a99e2eSeric { 10849b6c17a6Seric char *template = "\001l\n"; 108551552439Seric char buf[MAXLINE]; 108625a99e2eSeric 108757fc6f17Seric if (bitnset(M_NHDR, m->m_flags)) 108851552439Seric return; 108913bbc08cSeric 10902c7e1b8dSeric # ifdef UGLYUUCP 109157fc6f17Seric if (bitnset(M_UGLYUUCP, m->m_flags)) 109274b6e67bSeric { 1093ea09d6edSeric char *bang; 1094ea09d6edSeric char xbuf[MAXLINE]; 109574b6e67bSeric 10969b6c17a6Seric expand("\001g", buf, &buf[sizeof buf - 1], CurEnv); 1097ea09d6edSeric bang = index(buf, '!'); 109874b6e67bSeric if (bang == NULL) 1099ea09d6edSeric syserr("No ! in UUCP! (%s)", buf); 110074b6e67bSeric else 1101588cad61Seric { 1102ea09d6edSeric *bang++ = '\0'; 11039b6c17a6Seric (void) sprintf(xbuf, "From %s \001d remote from %s\n", bang, buf); 1104ea09d6edSeric template = xbuf; 110574b6e67bSeric } 1106588cad61Seric } 11072c7e1b8dSeric # endif UGLYUUCP 1108ea09d6edSeric expand(template, buf, &buf[sizeof buf - 1], CurEnv); 110977b52738Seric putline(buf, fp, m); 1110bc6e2962Seric } 1111bc6e2962Seric /* 111251552439Seric ** PUTBODY -- put the body of a message. 111351552439Seric ** 111451552439Seric ** Parameters: 111551552439Seric ** fp -- file to output onto. 111677b52738Seric ** m -- a mailer descriptor to control output format. 11179a6a5f55Seric ** e -- the envelope to put out. 111851552439Seric ** 111951552439Seric ** Returns: 112051552439Seric ** none. 112151552439Seric ** 112251552439Seric ** Side Effects: 112351552439Seric ** The message is written onto fp. 112451552439Seric */ 112551552439Seric 112677b52738Seric putbody(fp, m, e) 112751552439Seric FILE *fp; 1128588cad61Seric MAILER *m; 11299a6a5f55Seric register ENVELOPE *e; 113051552439Seric { 113177b52738Seric char buf[MAXLINE]; 113251552439Seric 113351552439Seric /* 113451552439Seric ** Output the body of the message 113551552439Seric */ 113651552439Seric 11379a6a5f55Seric if (e->e_dfp == NULL) 113851552439Seric { 11399a6a5f55Seric if (e->e_df != NULL) 11409a6a5f55Seric { 11419a6a5f55Seric e->e_dfp = fopen(e->e_df, "r"); 11429a6a5f55Seric if (e->e_dfp == NULL) 11439a6a5f55Seric syserr("Cannot open %s", e->e_df); 11449a6a5f55Seric } 11459a6a5f55Seric else 114677b52738Seric putline("<<< No Message Collected >>>", fp, m); 11479a6a5f55Seric } 11489a6a5f55Seric if (e->e_dfp != NULL) 11499a6a5f55Seric { 11509a6a5f55Seric rewind(e->e_dfp); 115177b52738Seric while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 115224fc8aeeSeric { 115324fc8aeeSeric if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 115424fc8aeeSeric strncmp(buf, "From", 4) == 0) 11553462ad9eSeric (void) putc('>', fp); 115677b52738Seric putline(buf, fp, m); 115724fc8aeeSeric } 115851552439Seric 11599a6a5f55Seric if (ferror(e->e_dfp)) 116051552439Seric { 116151552439Seric syserr("putbody: read error"); 116251552439Seric ExitStat = EX_IOERR; 116351552439Seric } 116451552439Seric } 116551552439Seric 116651552439Seric (void) fflush(fp); 116751552439Seric if (ferror(fp) && errno != EPIPE) 116851552439Seric { 116951552439Seric syserr("putbody: write error"); 117051552439Seric ExitStat = EX_IOERR; 117151552439Seric } 117251552439Seric errno = 0; 117325a99e2eSeric } 117425a99e2eSeric /* 117525a99e2eSeric ** MAILFILE -- Send a message to a file. 117625a99e2eSeric ** 1177f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 1178f129ec7dSeric ** bits, sendmail will try to become the owner of that file 1179f129ec7dSeric ** rather than the real user. Obviously, this only works if 1180f129ec7dSeric ** sendmail runs as root. 1181f129ec7dSeric ** 1182588cad61Seric ** This could be done as a subordinate mailer, except that it 1183588cad61Seric ** is used implicitly to save messages in ~/dead.letter. We 1184588cad61Seric ** view this as being sufficiently important as to include it 1185588cad61Seric ** here. For example, if the system is dying, we shouldn't have 1186588cad61Seric ** to create another process plus some pipes to save the message. 1187588cad61Seric ** 118825a99e2eSeric ** Parameters: 118925a99e2eSeric ** filename -- the name of the file to send to. 11906259796dSeric ** ctladdr -- the controlling address header -- includes 11916259796dSeric ** the userid/groupid to be when sending. 119225a99e2eSeric ** 119325a99e2eSeric ** Returns: 119425a99e2eSeric ** The exit code associated with the operation. 119525a99e2eSeric ** 119625a99e2eSeric ** Side Effects: 119725a99e2eSeric ** none. 119825a99e2eSeric */ 119925a99e2eSeric 12006259796dSeric mailfile(filename, ctladdr) 120125a99e2eSeric char *filename; 12026259796dSeric ADDRESS *ctladdr; 120325a99e2eSeric { 120425a99e2eSeric register FILE *f; 120532d19d43Seric register int pid; 120625a99e2eSeric 120732d19d43Seric /* 120832d19d43Seric ** Fork so we can change permissions here. 120932d19d43Seric ** Note that we MUST use fork, not vfork, because of 121032d19d43Seric ** the complications of calling subroutines, etc. 121132d19d43Seric */ 121232d19d43Seric 121332d19d43Seric DOFORK(fork); 121432d19d43Seric 121532d19d43Seric if (pid < 0) 121632d19d43Seric return (EX_OSERR); 121732d19d43Seric else if (pid == 0) 121832d19d43Seric { 121932d19d43Seric /* child -- actually write to file */ 1220f129ec7dSeric struct stat stb; 1221f129ec7dSeric 12220984da9fSeric (void) signal(SIGINT, SIG_DFL); 12230984da9fSeric (void) signal(SIGHUP, SIG_DFL); 12240984da9fSeric (void) signal(SIGTERM, SIG_DFL); 12253462ad9eSeric (void) umask(OldUmask); 1226f129ec7dSeric if (stat(filename, &stb) < 0) 122724447f54Seric { 122824447f54Seric errno = 0; 1229e6e1265fSeric stb.st_mode = 0666; 123024447f54Seric } 1231f129ec7dSeric if (bitset(0111, stb.st_mode)) 1232f129ec7dSeric exit(EX_CANTCREAT); 123303827b5fSeric if (ctladdr == NULL) 12347a941e7aSeric ctladdr = &CurEnv->e_from; 1235f129ec7dSeric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 1236e36b99e2Seric { 1237e36b99e2Seric if (ctladdr->q_uid == 0) 1238e36b99e2Seric (void) setgid(DefGid); 1239e36b99e2Seric else 12406259796dSeric (void) setgid(ctladdr->q_gid); 1241e36b99e2Seric } 1242f129ec7dSeric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 1243e36b99e2Seric { 1244e36b99e2Seric if (ctladdr->q_uid == 0) 1245e36b99e2Seric (void) setuid(DefUid); 1246e36b99e2Seric else 12476259796dSeric (void) setuid(ctladdr->q_uid); 1248e36b99e2Seric } 124927628d59Seric f = dfopen(filename, "a"); 125025a99e2eSeric if (f == NULL) 125132d19d43Seric exit(EX_CANTCREAT); 125225a99e2eSeric 125377b52738Seric putfromline(f, ProgMailer); 125477b52738Seric (*CurEnv->e_puthdr)(f, ProgMailer, CurEnv); 125577b52738Seric putline("\n", f, ProgMailer); 125677b52738Seric (*CurEnv->e_putbody)(f, ProgMailer, CurEnv); 125777b52738Seric putline("\n", f, ProgMailer); 1258db8841e9Seric (void) fclose(f); 125932d19d43Seric (void) fflush(stdout); 1260e36b99e2Seric 126127628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 1262c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 126332d19d43Seric exit(EX_OK); 126413bbc08cSeric /*NOTREACHED*/ 126532d19d43Seric } 126632d19d43Seric else 126732d19d43Seric { 126832d19d43Seric /* parent -- wait for exit status */ 1269588cad61Seric int st; 127032d19d43Seric 1271588cad61Seric st = waitfor(pid); 1272588cad61Seric if ((st & 0377) != 0) 1273588cad61Seric return (EX_UNAVAILABLE); 1274588cad61Seric else 1275588cad61Seric return ((st >> 8) & 0377); 127632d19d43Seric } 127725a99e2eSeric } 1278ea4dc939Seric /* 1279ea4dc939Seric ** SENDALL -- actually send all the messages. 1280ea4dc939Seric ** 1281ea4dc939Seric ** Parameters: 12820c52a0b3Seric ** e -- the envelope to send. 12837b95031aSeric ** mode -- the delivery mode to use. If SM_DEFAULT, use 12847b95031aSeric ** the current SendMode. 1285ea4dc939Seric ** 1286ea4dc939Seric ** Returns: 1287ea4dc939Seric ** none. 1288ea4dc939Seric ** 1289ea4dc939Seric ** Side Effects: 1290ea4dc939Seric ** Scans the send lists and sends everything it finds. 12910c52a0b3Seric ** Delivers any appropriate error messages. 1292276723a8Seric ** If we are running in a non-interactive mode, takes the 1293276723a8Seric ** appropriate action. 1294ea4dc939Seric */ 1295ea4dc939Seric 1296276723a8Seric sendall(e, mode) 12970c52a0b3Seric ENVELOPE *e; 1298276723a8Seric char mode; 1299ea4dc939Seric { 1300e77e673fSeric register ADDRESS *q; 130114a8ed7aSeric bool oldverbose; 1302276723a8Seric int pid; 1303ea4dc939Seric 13047b95031aSeric /* determine actual delivery mode */ 13057b95031aSeric if (mode == SM_DEFAULT) 13067b95031aSeric { 13075f73204aSeric extern bool shouldqueue(); 13087b95031aSeric 13095f73204aSeric if (shouldqueue(e->e_msgpriority)) 13107b95031aSeric mode = SM_QUEUE; 13117b95031aSeric else 13127b95031aSeric mode = SendMode; 13137b95031aSeric } 13147b95031aSeric 1315772e6e50Seric #ifdef DEBUG 1316df864a8fSeric if (tTd(13, 1)) 1317772e6e50Seric { 1318276723a8Seric printf("\nSENDALL: mode %c, sendqueue:\n", mode); 13190c52a0b3Seric printaddr(e->e_sendqueue, TRUE); 1320772e6e50Seric } 1321772e6e50Seric #endif DEBUG 1322ea4dc939Seric 13230c52a0b3Seric /* 1324276723a8Seric ** Do any preprocessing necessary for the mode we are running. 1325588cad61Seric ** Check to make sure the hop count is reasonable. 1326588cad61Seric ** Delete sends to the sender in mailing lists. 1327276723a8Seric */ 1328276723a8Seric 1329588cad61Seric CurEnv = e; 1330276723a8Seric 1331588cad61Seric if (e->e_hopcount > MAXHOP) 1332276723a8Seric { 1333588cad61Seric syserr("sendall: too many hops (%d max)", MAXHOP); 1334588cad61Seric return; 1335588cad61Seric } 1336588cad61Seric 1337588cad61Seric if (!MeToo) 1338276723a8Seric { 1339f3d6c55cSeric extern ADDRESS *recipient(); 1340f3d6c55cSeric 1341588cad61Seric e->e_from.q_flags |= QDONTSEND; 1342f3d6c55cSeric (void) recipient(&e->e_from, &e->e_sendqueue); 1343276723a8Seric } 1344588cad61Seric 1345588cad61Seric # ifdef QUEUE 1346b254bcb6Seric if ((mode == SM_QUEUE || mode == SM_FORK || 1347b254bcb6Seric (mode != SM_VERIFY && SuperSafe)) && 1348b254bcb6Seric !bitset(EF_INQUEUE, e->e_flags)) 1349588cad61Seric queueup(e, TRUE, mode == SM_QUEUE); 1350276723a8Seric #endif QUEUE 1351276723a8Seric 1352276723a8Seric oldverbose = Verbose; 1353276723a8Seric switch (mode) 1354276723a8Seric { 1355276723a8Seric case SM_VERIFY: 1356276723a8Seric Verbose = TRUE; 1357276723a8Seric break; 1358276723a8Seric 1359276723a8Seric case SM_QUEUE: 1360b254bcb6Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 1361276723a8Seric return; 1362276723a8Seric 1363276723a8Seric case SM_FORK: 13649a6a5f55Seric if (e->e_xfp != NULL) 13659a6a5f55Seric (void) fflush(e->e_xfp); 1366276723a8Seric pid = fork(); 1367276723a8Seric if (pid < 0) 1368276723a8Seric { 1369276723a8Seric mode = SM_DELIVER; 1370276723a8Seric break; 1371276723a8Seric } 1372276723a8Seric else if (pid > 0) 1373a6fce3d8Seric { 1374a6fce3d8Seric /* be sure we leave the temp files to our child */ 1375b254bcb6Seric e->e_id = e->e_df = NULL; 1376276723a8Seric return; 1377a6fce3d8Seric } 1378276723a8Seric 1379276723a8Seric /* double fork to avoid zombies */ 1380276723a8Seric if (fork() > 0) 1381276723a8Seric exit(EX_OK); 1382276723a8Seric 1383a6fce3d8Seric /* be sure we are immune from the terminal */ 1384769e215aSeric disconnect(FALSE); 1385a6fce3d8Seric 1386276723a8Seric break; 1387276723a8Seric } 1388276723a8Seric 1389276723a8Seric /* 13900c52a0b3Seric ** Run through the list and send everything. 13910c52a0b3Seric */ 13920c52a0b3Seric 13930c52a0b3Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1394ea4dc939Seric { 1395276723a8Seric if (mode == SM_VERIFY) 1396ea4dc939Seric { 1397a6fce3d8Seric e->e_to = q->q_paddr; 1398e77e673fSeric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1399ea4dc939Seric message(Arpa_Info, "deliverable"); 1400ea4dc939Seric } 1401ea4dc939Seric else 1402588cad61Seric (void) deliver(e, q); 1403ea4dc939Seric } 140414a8ed7aSeric Verbose = oldverbose; 14050c52a0b3Seric 14060c52a0b3Seric /* 14070c52a0b3Seric ** Now run through and check for errors. 14080c52a0b3Seric */ 14090c52a0b3Seric 1410276723a8Seric if (mode == SM_VERIFY) 14110c52a0b3Seric return; 14120c52a0b3Seric 14130c52a0b3Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14140c52a0b3Seric { 14150c52a0b3Seric register ADDRESS *qq; 14160c52a0b3Seric 1417df864a8fSeric # ifdef DEBUG 1418df864a8fSeric if (tTd(13, 3)) 1419df864a8fSeric { 1420df864a8fSeric printf("Checking "); 1421df864a8fSeric printaddr(q, FALSE); 1422df864a8fSeric } 1423df864a8fSeric # endif DEBUG 1424df864a8fSeric 1425b254bcb6Seric /* only send errors if the message failed */ 1426b254bcb6Seric if (!bitset(QBADADDR, q->q_flags)) 1427b254bcb6Seric continue; 14280c52a0b3Seric 14290c52a0b3Seric /* we have an address that failed -- find the parent */ 14300c52a0b3Seric for (qq = q; qq != NULL; qq = qq->q_alias) 14310c52a0b3Seric { 14320c52a0b3Seric char obuf[MAXNAME + 6]; 14330c52a0b3Seric extern char *aliaslookup(); 14340c52a0b3Seric 14350c52a0b3Seric /* we can only have owners for local addresses */ 143657fc6f17Seric if (!bitnset(M_LOCAL, qq->q_mailer->m_flags)) 14370c52a0b3Seric continue; 14380c52a0b3Seric 14390c52a0b3Seric /* see if the owner list exists */ 14400c52a0b3Seric (void) strcpy(obuf, "owner-"); 1441cec031e3Seric if (strncmp(qq->q_user, "owner-", 6) == 0) 1442cec031e3Seric (void) strcat(obuf, "owner"); 1443cec031e3Seric else 14440c52a0b3Seric (void) strcat(obuf, qq->q_user); 14450c52a0b3Seric if (aliaslookup(obuf) == NULL) 14460c52a0b3Seric continue; 14470c52a0b3Seric 1448df864a8fSeric # ifdef DEBUG 1449df864a8fSeric if (tTd(13, 4)) 1450df864a8fSeric printf("Errors to %s\n", obuf); 1451df864a8fSeric # endif DEBUG 1452df864a8fSeric 14530c52a0b3Seric /* owner list exists -- add it to the error queue */ 1454e3e4ed86Seric sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue); 145553e3fa05Seric ErrorMode = EM_MAIL; 14560c52a0b3Seric break; 14570c52a0b3Seric } 14580c52a0b3Seric 14590c52a0b3Seric /* if we did not find an owner, send to the sender */ 14607455aa0bSeric if (qq == NULL && bitset(QBADADDR, q->q_flags)) 1461e3e4ed86Seric sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue); 14620c52a0b3Seric } 1463276723a8Seric 1464276723a8Seric if (mode == SM_FORK) 1465276723a8Seric finis(); 14660c52a0b3Seric } 1467