125a99e2eSeric # include <signal.h> 254aa2b0fSeric # include <errno.h> 3b20b3270Seric # include "sendmail.h" 4c77d1c25Seric # include <sys/stat.h> 525a99e2eSeric 6*ea09d6edSeric SCCSID(@(#)deliver.c 3.152 05/04/83); 7259cace7Seric 825a99e2eSeric /* 913bbc08cSeric ** DELIVER -- Deliver a message to a list of addresses. 1013bbc08cSeric ** 1113bbc08cSeric ** This routine delivers to everyone on the same host as the 1213bbc08cSeric ** user on the head of the list. It is clever about mailers 1313bbc08cSeric ** that don't handle multiple users. It is NOT guaranteed 1413bbc08cSeric ** that it will deliver to all these addresses however -- so 1513bbc08cSeric ** deliver should be called once for each address on the 1613bbc08cSeric ** list. 1725a99e2eSeric ** 1825a99e2eSeric ** Parameters: 19588cad61Seric ** e -- the envelope to deliver. 20c77d1c25Seric ** firstto -- head of the address list to deliver to. 2125a99e2eSeric ** 2225a99e2eSeric ** Returns: 2325a99e2eSeric ** zero -- successfully delivered. 2425a99e2eSeric ** else -- some failure, see ExitStat for more info. 2525a99e2eSeric ** 2625a99e2eSeric ** Side Effects: 2725a99e2eSeric ** The standard input is passed off to someone. 2825a99e2eSeric */ 2925a99e2eSeric 30588cad61Seric deliver(e, firstto) 31588cad61Seric register ENVELOPE *e; 32c77d1c25Seric ADDRESS *firstto; 3325a99e2eSeric { 3478442df3Seric char *host; /* host being sent to */ 3578442df3Seric char *user; /* user being sent to */ 3625a99e2eSeric char **pvp; 375dfc646bSeric register char **mvp; 3825a99e2eSeric register char *p; 39588cad61Seric register MAILER *m; /* mailer for this recipient */ 406259796dSeric ADDRESS *ctladdr; 41c77d1c25Seric register ADDRESS *to = firstto; 42c579ef51Seric bool clever = FALSE; /* running user smtp to this mailer */ 43772e6e50Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 445826d9d3Seric register int rcode; /* response code */ 45ee6bf8dfSeric char *pv[MAXPV+1]; 46ee6bf8dfSeric char tobuf[MAXLINE-50]; /* text line of to people */ 47ee6bf8dfSeric char buf[MAXNAME]; 48ee6bf8dfSeric char tfrombuf[MAXNAME]; /* translated from person */ 49ee6bf8dfSeric extern bool checkcompat(); 50ee6bf8dfSeric extern ADDRESS *getctladdr(); 51ee6bf8dfSeric extern char *remotename(); 5225a99e2eSeric 5335490626Seric errno = 0; 54da2935e1Seric if (bitset(QDONTSEND, to->q_flags)) 555dfc646bSeric return (0); 5625a99e2eSeric 5751552439Seric m = to->q_mailer; 5851552439Seric host = to->q_host; 5951552439Seric 6025a99e2eSeric # ifdef DEBUG 616ef48975Seric if (tTd(10, 1)) 625dfc646bSeric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 6351552439Seric m->m_mno, host, to->q_user); 6425a99e2eSeric # endif DEBUG 65f3dbc832Seric 66f3dbc832Seric /* 67f3dbc832Seric ** If this mailer is expensive, and if we don't want to make 68f3dbc832Seric ** connections now, just mark these addresses and return. 69f3dbc832Seric ** This is useful if we want to batch connections to 70f3dbc832Seric ** reduce load. This will cause the messages to be 71f3dbc832Seric ** queued up, and a daemon will come along to send the 72f3dbc832Seric ** messages later. 73f3dbc832Seric ** This should be on a per-mailer basis. 74f3dbc832Seric */ 75f3dbc832Seric 7657fc6f17Seric if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) && 77317680d6Seric !Verbose) 78f3dbc832Seric { 79f3dbc832Seric for (; to != NULL; to = to->q_next) 80f4560e80Seric { 81f4560e80Seric if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m) 82f4560e80Seric continue; 83f3dbc832Seric to->q_flags |= QQUEUEUP|QDONTSEND; 84588cad61Seric e->e_to = to->q_paddr; 85eb238f8cSeric message(Arpa_Info, "queued"); 86eb238f8cSeric if (LogLevel > 4) 87eb238f8cSeric logdelivery("queued"); 88f4560e80Seric } 89588cad61Seric e->e_to = NULL; 90f3dbc832Seric return (0); 91f3dbc832Seric } 92f3dbc832Seric 9325a99e2eSeric /* 945dfc646bSeric ** Do initial argv setup. 955dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 965dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 975dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 985dfc646bSeric ** code does not check for 'pv' overflow; this places a 995dfc646bSeric ** manifest lower limit of 4 for MAXPV. 1003bea8136Seric ** The from address rewrite is expected to make 1013bea8136Seric ** the address relative to the other end. 1025dfc646bSeric */ 1035dfc646bSeric 10478442df3Seric /* rewrite from address, using rewriting rules */ 105588cad61Seric expand("$f", buf, &buf[sizeof buf - 1], e); 106ee6bf8dfSeric (void) strcpy(tfrombuf, remotename(buf, m, TRUE, TRUE)); 10778442df3Seric 108588cad61Seric define('g', tfrombuf, e); /* translated sender address */ 109588cad61Seric define('h', host, e); /* to host */ 1105dfc646bSeric Errors = 0; 1115dfc646bSeric pvp = pv; 1125dfc646bSeric *pvp++ = m->m_argv[0]; 1135dfc646bSeric 1145dfc646bSeric /* insert -f or -r flag as appropriate */ 11557fc6f17Seric if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 1165dfc646bSeric { 11757fc6f17Seric if (bitnset(M_FOPT, m->m_flags)) 1185dfc646bSeric *pvp++ = "-f"; 1195dfc646bSeric else 1205dfc646bSeric *pvp++ = "-r"; 121588cad61Seric expand("$g", buf, &buf[sizeof buf - 1], e); 1225dfc646bSeric *pvp++ = newstr(buf); 1235dfc646bSeric } 1245dfc646bSeric 1255dfc646bSeric /* 1265dfc646bSeric ** Append the other fixed parts of the argv. These run 1275dfc646bSeric ** up to the first entry containing "$u". There can only 1285dfc646bSeric ** be one of these, and there are only a few more slots 1295dfc646bSeric ** in the pv after it. 1305dfc646bSeric */ 1315dfc646bSeric 1325dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 1335dfc646bSeric { 1345dfc646bSeric while ((p = index(p, '$')) != NULL) 1355dfc646bSeric if (*++p == 'u') 1365dfc646bSeric break; 1375dfc646bSeric if (p != NULL) 1385dfc646bSeric break; 1395dfc646bSeric 1405dfc646bSeric /* this entry is safe -- go ahead and process it */ 141588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 1425dfc646bSeric *pvp++ = newstr(buf); 1435dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 1445dfc646bSeric { 1455dfc646bSeric syserr("Too many parameters to %s before $u", pv[0]); 1465dfc646bSeric return (-1); 1475dfc646bSeric } 1485dfc646bSeric } 149c579ef51Seric 15033db8731Seric /* 15133db8731Seric ** If we have no substitution for the user name in the argument 15233db8731Seric ** list, we know that we must supply the names otherwise -- and 15333db8731Seric ** SMTP is the answer!! 15433db8731Seric */ 15533db8731Seric 1565dfc646bSeric if (*mvp == NULL) 157c579ef51Seric { 158c579ef51Seric /* running SMTP */ 1592c7e1b8dSeric # ifdef SMTP 160c579ef51Seric clever = TRUE; 161c579ef51Seric *pvp = NULL; 1622c7e1b8dSeric # else SMTP 16333db8731Seric /* oops! we don't implement SMTP */ 1642c7e1b8dSeric syserr("SMTP style mailer"); 1652c7e1b8dSeric return (EX_SOFTWARE); 1662c7e1b8dSeric # endif SMTP 167c579ef51Seric } 1685dfc646bSeric 1695dfc646bSeric /* 1705dfc646bSeric ** At this point *mvp points to the argument with $u. We 1715dfc646bSeric ** run through our address list and append all the addresses 1725dfc646bSeric ** we can. If we run out of space, do not fret! We can 1735dfc646bSeric ** always send another copy later. 1745dfc646bSeric */ 1755dfc646bSeric 1765dfc646bSeric tobuf[0] = '\0'; 177588cad61Seric e->e_to = tobuf; 1786259796dSeric ctladdr = NULL; 1795dfc646bSeric for (; to != NULL; to = to->q_next) 1805dfc646bSeric { 1815dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 18257fc6f17Seric if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 1835dfc646bSeric break; 1845dfc646bSeric 1855dfc646bSeric /* if already sent or not for this host, don't send */ 186da2935e1Seric if (bitset(QDONTSEND, to->q_flags) || 187da2935e1Seric strcmp(to->q_host, host) != 0 || 188da2935e1Seric to->q_mailer != firstto->q_mailer) 1895dfc646bSeric continue; 1906259796dSeric 1914b22ea87Seric /* avoid overflowing tobuf */ 192588cad61Seric if (sizeof tobuf - (strlen(to->q_paddr) + strlen(tobuf) + 2) < 0) 1934b22ea87Seric break; 1944b22ea87Seric 195772e6e50Seric # ifdef DEBUG 1966ef48975Seric if (tTd(10, 1)) 197772e6e50Seric { 198772e6e50Seric printf("\nsend to "); 199772e6e50Seric printaddr(to, FALSE); 200772e6e50Seric } 201772e6e50Seric # endif DEBUG 202772e6e50Seric 2036259796dSeric /* compute effective uid/gid when sending */ 2047da1035fSeric if (to->q_mailer == ProgMailer) 2056259796dSeric ctladdr = getctladdr(to); 2066259796dSeric 2075dfc646bSeric user = to->q_user; 208588cad61Seric e->e_to = to->q_paddr; 2095dfc646bSeric to->q_flags |= QDONTSEND; 2105dfc646bSeric 2115dfc646bSeric /* 2125dfc646bSeric ** Check to see that these people are allowed to 2135dfc646bSeric ** talk to each other. 2142a6e0786Seric */ 2152a6e0786Seric 21669582d2fSeric if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 21769582d2fSeric { 21869582d2fSeric usrerr("Message is too large; %ld bytes max", m->m_maxsize); 21969582d2fSeric NoReturn = TRUE; 22069582d2fSeric giveresponse(EX_UNAVAILABLE, m, e); 22169582d2fSeric continue; 22269582d2fSeric } 2232a6e0786Seric if (!checkcompat(to)) 2245dfc646bSeric { 225198d9be0Seric giveresponse(EX_UNAVAILABLE, m, e); 2265dfc646bSeric continue; 2275dfc646bSeric } 2282a6e0786Seric 2292a6e0786Seric /* 2309ec9501bSeric ** Strip quote bits from names if the mailer is dumb 2319ec9501bSeric ** about them. 23225a99e2eSeric */ 23325a99e2eSeric 23457fc6f17Seric if (bitnset(M_STRIPQ, m->m_flags)) 23525a99e2eSeric { 2369ec9501bSeric stripquotes(user, TRUE); 2379ec9501bSeric stripquotes(host, TRUE); 2389ec9501bSeric } 2399ec9501bSeric else 2409ec9501bSeric { 2419ec9501bSeric stripquotes(user, FALSE); 2429ec9501bSeric stripquotes(host, FALSE); 24325a99e2eSeric } 24425a99e2eSeric 245cdb828c5Seric /* hack attack -- delivermail compatibility */ 246cdb828c5Seric if (m == ProgMailer && *user == '|') 247cdb828c5Seric user++; 248cdb828c5Seric 24925a99e2eSeric /* 2503efaed6eSeric ** If an error message has already been given, don't 2513efaed6eSeric ** bother to send to this address. 2523efaed6eSeric ** 2533efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 2543efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 2553efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 2563efaed6eSeric */ 2573efaed6eSeric 2586cae517dSeric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 2593efaed6eSeric continue; 2603efaed6eSeric 261f2fec898Seric /* save statistics.... */ 262588cad61Seric markstats(e, to); 263f2fec898Seric 2643efaed6eSeric /* 26525a99e2eSeric ** See if this user name is "special". 26625a99e2eSeric ** If the user name has a slash in it, assume that this 26751552439Seric ** is a file -- send it off without further ado. Note 26851552439Seric ** that this type of addresses is not processed along 26951552439Seric ** with the others, so we fudge on the To person. 27025a99e2eSeric */ 27125a99e2eSeric 2727da1035fSeric if (m == LocalMailer) 27325a99e2eSeric { 274a49f24c0Seric if (user[0] == '/') 27525a99e2eSeric { 2765826d9d3Seric rcode = mailfile(user, getctladdr(to)); 277198d9be0Seric giveresponse(rcode, m, e); 2785dfc646bSeric continue; 27925a99e2eSeric } 28025a99e2eSeric } 28125a99e2eSeric 28213bbc08cSeric /* 28313bbc08cSeric ** Address is verified -- add this user to mailer 28413bbc08cSeric ** argv, and add it to the print list of recipients. 28513bbc08cSeric */ 28613bbc08cSeric 287508daeccSeric /* link together the chain of recipients */ 288508daeccSeric to->q_tchain = tochain; 289508daeccSeric tochain = to; 290508daeccSeric 2915dfc646bSeric /* create list of users for error messages */ 292db8841e9Seric (void) strcat(tobuf, ","); 293db8841e9Seric (void) strcat(tobuf, to->q_paddr); 294588cad61Seric define('u', user, e); /* to user */ 295588cad61Seric define('z', to->q_home, e); /* user's home */ 2965dfc646bSeric 297c579ef51Seric /* 298508daeccSeric ** Expand out this user into argument list. 299c579ef51Seric */ 300c579ef51Seric 301508daeccSeric if (!clever) 302c579ef51Seric { 303588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 3045dfc646bSeric *pvp++ = newstr(buf); 3055dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 3065dfc646bSeric { 3075dfc646bSeric /* allow some space for trailing parms */ 3085dfc646bSeric break; 3095dfc646bSeric } 3105dfc646bSeric } 311c579ef51Seric } 3125dfc646bSeric 313145b49b1Seric /* see if any addresses still exist */ 314145b49b1Seric if (tobuf[0] == '\0') 315c579ef51Seric { 316588cad61Seric define('g', (char *) NULL, e); 317145b49b1Seric return (0); 318c579ef51Seric } 319145b49b1Seric 3205dfc646bSeric /* print out messages as full list */ 32163780dbdSeric e->e_to = tobuf + 1; 3225dfc646bSeric 3235dfc646bSeric /* 3245dfc646bSeric ** Fill out any parameters after the $u parameter. 3255dfc646bSeric */ 3265dfc646bSeric 327c579ef51Seric while (!clever && *++mvp != NULL) 3285dfc646bSeric { 329588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 3305dfc646bSeric *pvp++ = newstr(buf); 3315dfc646bSeric if (pvp >= &pv[MAXPV]) 3325dfc646bSeric syserr("deliver: pv overflow after $u for %s", pv[0]); 3335dfc646bSeric } 3345dfc646bSeric *pvp++ = NULL; 3355dfc646bSeric 33625a99e2eSeric /* 33725a99e2eSeric ** Call the mailer. 3386328bdf7Seric ** The argument vector gets built, pipes 33925a99e2eSeric ** are created as necessary, and we fork & exec as 3406328bdf7Seric ** appropriate. 341c579ef51Seric ** If we are running SMTP, we just need to clean up. 34225a99e2eSeric */ 34325a99e2eSeric 344588cad61Seric message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name); 345588cad61Seric 3466259796dSeric if (ctladdr == NULL) 347588cad61Seric ctladdr = &e->e_from; 3482c7e1b8dSeric # ifdef SMTP 349c579ef51Seric if (clever) 350c579ef51Seric { 351588cad61Seric /* send the initial SMTP protocol */ 35277b52738Seric rcode = smtpinit(m, pv); 353588cad61Seric 35463780dbdSeric if (rcode == EX_OK) 35563780dbdSeric { 356588cad61Seric /* send the recipient list */ 35763780dbdSeric tobuf[0] = '\0'; 358588cad61Seric for (to = tochain; to != NULL; to = to->q_tchain) 359588cad61Seric { 360588cad61Seric int i; 361588cad61Seric 36263780dbdSeric e->e_to = to->q_paddr; 36377b52738Seric i = smtprcpt(to, m); 364588cad61Seric if (i != EX_OK) 365588cad61Seric { 36683b7ddc9Seric markfailure(e, to, i); 367198d9be0Seric giveresponse(i, m, e); 36863780dbdSeric } 36963780dbdSeric else 37063780dbdSeric { 37163780dbdSeric strcat(tobuf, ","); 37263780dbdSeric strcat(tobuf, to->q_paddr); 373588cad61Seric } 374588cad61Seric } 375588cad61Seric 37663780dbdSeric /* now send the data */ 37763780dbdSeric if (tobuf[0] == '\0') 37863780dbdSeric e->e_to = NULL; 37963780dbdSeric else 38063780dbdSeric { 38163780dbdSeric e->e_to = tobuf + 1; 38277b52738Seric rcode = smtpdata(m, e); 38363780dbdSeric } 38463780dbdSeric 38563780dbdSeric /* now close the connection */ 38677b52738Seric smtpquit(pv[0], m); 38763780dbdSeric } 388c579ef51Seric } 389c579ef51Seric else 3902c7e1b8dSeric # endif SMTP 39177b52738Seric rcode = sendoff(e, m, pv, ctladdr); 3925dfc646bSeric 393c77d1c25Seric /* 39463780dbdSeric ** Do final status disposal. 39563780dbdSeric ** We check for something in tobuf for the SMTP case. 396c77d1c25Seric ** If we got a temporary failure, arrange to queue the 397c77d1c25Seric ** addressees. 398c77d1c25Seric */ 399c77d1c25Seric 40063780dbdSeric if (tobuf[0] != '\0') 401198d9be0Seric giveresponse(rcode, m, e); 40263780dbdSeric if (rcode != EX_OK) 403c77d1c25Seric { 404772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 40583b7ddc9Seric markfailure(e, to, rcode); 406c77d1c25Seric } 407c77d1c25Seric 40835490626Seric errno = 0; 409588cad61Seric define('g', (char *) NULL, e); 4105826d9d3Seric return (rcode); 41125a99e2eSeric } 4125dfc646bSeric /* 41383b7ddc9Seric ** MARKFAILURE -- mark a failure on a specific address. 41483b7ddc9Seric ** 41583b7ddc9Seric ** Parameters: 41683b7ddc9Seric ** e -- the envelope we are sending. 41783b7ddc9Seric ** q -- the address to mark. 41883b7ddc9Seric ** rcode -- the code signifying the particular failure. 41983b7ddc9Seric ** 42083b7ddc9Seric ** Returns: 42183b7ddc9Seric ** none. 42283b7ddc9Seric ** 42383b7ddc9Seric ** Side Effects: 42483b7ddc9Seric ** marks the address (and possibly the envelope) with the 42583b7ddc9Seric ** failure so that an error will be returned or 42683b7ddc9Seric ** the message will be queued, as appropriate. 42783b7ddc9Seric */ 42883b7ddc9Seric 42983b7ddc9Seric markfailure(e, q, rcode) 43083b7ddc9Seric register ENVELOPE *e; 43183b7ddc9Seric register ADDRESS *q; 43283b7ddc9Seric int rcode; 43383b7ddc9Seric { 43483b7ddc9Seric if (rcode == EX_OK) 43583b7ddc9Seric return; 43683b7ddc9Seric else if (rcode != EX_TEMPFAIL) 43783b7ddc9Seric q->q_flags |= QBADADDR; 43883b7ddc9Seric else if (curtime() > e->e_ctime + TimeOut) 43983b7ddc9Seric { 44083b7ddc9Seric extern char *pintvl(); 441198d9be0Seric char buf[MAXLINE]; 44283b7ddc9Seric 44383b7ddc9Seric if (!bitset(EF_TIMEOUT, e->e_flags)) 444198d9be0Seric { 445198d9be0Seric (void) sprintf(buf, "Cannot send message for %s", 44683b7ddc9Seric pintvl(TimeOut, FALSE)); 447198d9be0Seric if (e->e_message != NULL) 448198d9be0Seric free(e->e_message); 449198d9be0Seric e->e_message = newstr(buf); 450198d9be0Seric message(Arpa_Info, buf); 451198d9be0Seric } 45283b7ddc9Seric q->q_flags |= QBADADDR; 45383b7ddc9Seric e->e_flags |= EF_TIMEOUT; 45483b7ddc9Seric } 45583b7ddc9Seric else 45683b7ddc9Seric q->q_flags |= QQUEUEUP; 45783b7ddc9Seric } 45883b7ddc9Seric /* 45932d19d43Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 46032d19d43Seric ** 46132d19d43Seric ** This MUST be a macro, since after a vfork we are running 46232d19d43Seric ** two processes on the same stack!!! 46332d19d43Seric ** 46432d19d43Seric ** Parameters: 46532d19d43Seric ** none. 46632d19d43Seric ** 46732d19d43Seric ** Returns: 46832d19d43Seric ** From a macro??? You've got to be kidding! 46932d19d43Seric ** 47032d19d43Seric ** Side Effects: 47132d19d43Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 47232d19d43Seric ** pid of child in parent, zero in child. 47332d19d43Seric ** -1 on unrecoverable error. 47432d19d43Seric ** 47532d19d43Seric ** Notes: 47632d19d43Seric ** I'm awfully sorry this looks so awful. That's 47732d19d43Seric ** vfork for you..... 47832d19d43Seric */ 47932d19d43Seric 48032d19d43Seric # define NFORKTRIES 5 4814300ddf0Seric # ifdef VMUNIX 48232d19d43Seric # define XFORK vfork 4834300ddf0Seric # else VMUNIX 48432d19d43Seric # define XFORK fork 4854300ddf0Seric # endif VMUNIX 48632d19d43Seric 48732d19d43Seric # define DOFORK(fORKfN) \ 48832d19d43Seric {\ 48932d19d43Seric register int i;\ 49032d19d43Seric \ 49132d19d43Seric for (i = NFORKTRIES; i-- > 0; )\ 49232d19d43Seric {\ 49332d19d43Seric pid = fORKfN();\ 49432d19d43Seric if (pid >= 0)\ 49532d19d43Seric break;\ 4965e663df1Seric sleep(NFORKTRIES - i);\ 49732d19d43Seric }\ 49832d19d43Seric } 49932d19d43Seric /* 5002ed72599Seric ** DOFORK -- simple fork interface to DOFORK. 5012ed72599Seric ** 5022ed72599Seric ** Parameters: 5032ed72599Seric ** none. 5042ed72599Seric ** 5052ed72599Seric ** Returns: 5062ed72599Seric ** pid of child in parent. 5072ed72599Seric ** zero in child. 5082ed72599Seric ** -1 on error. 5092ed72599Seric ** 5102ed72599Seric ** Side Effects: 5112ed72599Seric ** returns twice, once in parent and once in child. 5122ed72599Seric */ 5132ed72599Seric 5142ed72599Seric dofork() 5152ed72599Seric { 5162ed72599Seric register int pid; 5172ed72599Seric 5182ed72599Seric DOFORK(fork); 5192ed72599Seric return (pid); 5202ed72599Seric } 5212ed72599Seric /* 5225dfc646bSeric ** SENDOFF -- send off call to mailer & collect response. 5235dfc646bSeric ** 5245dfc646bSeric ** Parameters: 525588cad61Seric ** e -- the envelope to mail. 5265dfc646bSeric ** m -- mailer descriptor. 5275dfc646bSeric ** pvp -- parameter vector to send to it. 5286259796dSeric ** ctladdr -- an address pointer controlling the 5296259796dSeric ** user/groupid etc. of the mailer. 5305dfc646bSeric ** 5315dfc646bSeric ** Returns: 5325dfc646bSeric ** exit status of mailer. 5335dfc646bSeric ** 5345dfc646bSeric ** Side Effects: 5355dfc646bSeric ** none. 5365dfc646bSeric */ 5375dfc646bSeric 53877b52738Seric sendoff(e, m, pvp, ctladdr) 539588cad61Seric register ENVELOPE *e; 540588cad61Seric MAILER *m; 5415dfc646bSeric char **pvp; 5426259796dSeric ADDRESS *ctladdr; 5435dfc646bSeric { 544c579ef51Seric auto FILE *mfile; 545c579ef51Seric auto FILE *rfile; 5465dfc646bSeric register int i; 547c579ef51Seric int pid; 548c579ef51Seric 549c579ef51Seric /* 550c579ef51Seric ** Create connection to mailer. 551c579ef51Seric */ 552c579ef51Seric 553c579ef51Seric pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 554c579ef51Seric if (pid < 0) 555c579ef51Seric return (-1); 556c579ef51Seric 557c579ef51Seric /* 558c579ef51Seric ** Format and send message. 559c579ef51Seric */ 560c579ef51Seric 56177b52738Seric putfromline(mfile, m); 56277b52738Seric (*e->e_puthdr)(mfile, m, e); 56377b52738Seric putline("\n", mfile, m); 56477b52738Seric (*e->e_putbody)(mfile, m, e); 565c579ef51Seric (void) fclose(mfile); 566c579ef51Seric 567c579ef51Seric i = endmailer(pid, pvp[0]); 568bc6e2962Seric 569bc6e2962Seric /* arrange a return receipt if requested */ 57057fc6f17Seric if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags)) 571bc6e2962Seric { 572588cad61Seric e->e_flags |= EF_SENDRECEIPT; 573bc6e2962Seric /* do we want to send back more info? */ 574bc6e2962Seric } 575bc6e2962Seric 576c579ef51Seric return (i); 577c579ef51Seric } 578c579ef51Seric /* 579c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 580c579ef51Seric ** 581c579ef51Seric ** We should never get fatal errors (e.g., segmentation 582c579ef51Seric ** violation), so we report those specially. For other 583c579ef51Seric ** errors, we choose a status message (into statmsg), 584c579ef51Seric ** and if it represents an error, we print it. 585c579ef51Seric ** 586c579ef51Seric ** Parameters: 587c579ef51Seric ** pid -- pid of mailer. 588c579ef51Seric ** name -- name of mailer (for error messages). 589c579ef51Seric ** 590c579ef51Seric ** Returns: 591c579ef51Seric ** exit code of mailer. 592c579ef51Seric ** 593c579ef51Seric ** Side Effects: 594c579ef51Seric ** none. 595c579ef51Seric */ 596c579ef51Seric 597c579ef51Seric endmailer(pid, name) 598c579ef51Seric int pid; 599c579ef51Seric char *name; 600c579ef51Seric { 601588cad61Seric int st; 602c579ef51Seric 60333db8731Seric /* in the IPC case there is nothing to wait for */ 60433db8731Seric if (pid == 0) 60533db8731Seric return (EX_OK); 60633db8731Seric 60733db8731Seric /* wait for the mailer process to die and collect status */ 608588cad61Seric st = waitfor(pid); 609588cad61Seric if (st == -1) 61078de67c1Seric { 611588cad61Seric syserr("endmailer %s: wait", name); 612588cad61Seric return (EX_SOFTWARE); 613c579ef51Seric } 61433db8731Seric 61533db8731Seric /* see if it died a horrid death */ 616c579ef51Seric if ((st & 0377) != 0) 617c579ef51Seric { 618588cad61Seric syserr("endmailer %s: stat %o", name, st); 619c579ef51Seric ExitStat = EX_UNAVAILABLE; 620588cad61Seric return (EX_UNAVAILABLE); 621c579ef51Seric } 62233db8731Seric 62333db8731Seric /* normal death -- return status */ 624588cad61Seric st = (st >> 8) & 0377; 625588cad61Seric return (st); 626c579ef51Seric } 627c579ef51Seric /* 628c579ef51Seric ** OPENMAILER -- open connection to mailer. 629c579ef51Seric ** 630c579ef51Seric ** Parameters: 631c579ef51Seric ** m -- mailer descriptor. 632c579ef51Seric ** pvp -- parameter vector to pass to mailer. 633c579ef51Seric ** ctladdr -- controlling address for user. 634c579ef51Seric ** clever -- create a full duplex connection. 635c579ef51Seric ** pmfile -- pointer to mfile (to mailer) connection. 636c579ef51Seric ** prfile -- pointer to rfile (from mailer) connection. 637c579ef51Seric ** 638c579ef51Seric ** Returns: 63933db8731Seric ** pid of mailer ( > 0 ). 640c579ef51Seric ** -1 on error. 64133db8731Seric ** zero on an IPC connection. 642c579ef51Seric ** 643c579ef51Seric ** Side Effects: 644c579ef51Seric ** creates a mailer in a subprocess. 645c579ef51Seric */ 646c579ef51Seric 647c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 648588cad61Seric MAILER *m; 649c579ef51Seric char **pvp; 650c579ef51Seric ADDRESS *ctladdr; 651c579ef51Seric bool clever; 652c579ef51Seric FILE **pmfile; 653c579ef51Seric FILE **prfile; 654c579ef51Seric { 6555dfc646bSeric int pid; 656f8952a83Seric int mpvect[2]; 657c579ef51Seric int rpvect[2]; 6585dfc646bSeric FILE *mfile; 659c579ef51Seric FILE *rfile; 6605dfc646bSeric extern FILE *fdopen(); 6615dfc646bSeric 6625dfc646bSeric # ifdef DEBUG 6636ef48975Seric if (tTd(11, 1)) 6645dfc646bSeric { 6658c57e552Seric printf("openmailer:"); 6665dfc646bSeric printav(pvp); 6675dfc646bSeric } 6685dfc646bSeric # endif DEBUG 66935490626Seric errno = 0; 6705dfc646bSeric 67133db8731Seric /* 67233db8731Seric ** Deal with the special case of mail handled through an IPC 67333db8731Seric ** connection. 67433db8731Seric ** In this case we don't actually fork. We must be 67533db8731Seric ** running SMTP for this to work. We will return a 67633db8731Seric ** zero pid to indicate that we are running IPC. 677e7c1bd78Seric ** We also handle a debug version that just talks to stdin/out. 67833db8731Seric */ 67933db8731Seric 680e7c1bd78Seric #ifdef DEBUG 681e7c1bd78Seric /* check for Local Person Communication -- not for mortals!!! */ 682e7c1bd78Seric if (strcmp(m->m_mailer, "[LPC]") == 0) 683e7c1bd78Seric { 684e7c1bd78Seric *pmfile = stdout; 685e7c1bd78Seric *prfile = stdin; 686e7c1bd78Seric return (0); 687e7c1bd78Seric } 688e7c1bd78Seric #endif DEBUG 689e7c1bd78Seric 69033db8731Seric if (strcmp(m->m_mailer, "[IPC]") == 0) 69133db8731Seric { 692588cad61Seric #ifdef DAEMON 69333db8731Seric register int i; 6941277f9a8Seric register u_short port; 69533db8731Seric 69633db8731Seric if (!clever) 69733db8731Seric syserr("non-clever IPC"); 69893b6e3cfSeric if (pvp[2] != NULL) 6991277f9a8Seric port = atoi(pvp[2]); 70093b6e3cfSeric else 7011277f9a8Seric port = 0; 7021277f9a8Seric i = makeconnection(pvp[1], port, pmfile, prfile); 70333db8731Seric if (i != EX_OK) 704ed854c7bSeric { 705ed854c7bSeric ExitStat = i; 70633db8731Seric return (-1); 707ed854c7bSeric } 70833db8731Seric else 70933db8731Seric return (0); 710588cad61Seric #else DAEMON 711588cad61Seric syserr("openmailer: no IPC"); 712588cad61Seric return (-1); 71333db8731Seric #endif DAEMON 714588cad61Seric } 71533db8731Seric 7166328bdf7Seric /* create a pipe to shove the mail through */ 717f8952a83Seric if (pipe(mpvect) < 0) 71825a99e2eSeric { 719588cad61Seric syserr("openmailer: pipe (to mailer)"); 72025a99e2eSeric return (-1); 72125a99e2eSeric } 722c579ef51Seric 7232c7e1b8dSeric #ifdef SMTP 724c579ef51Seric /* if this mailer speaks smtp, create a return pipe */ 725c579ef51Seric if (clever && pipe(rpvect) < 0) 726c579ef51Seric { 727588cad61Seric syserr("openmailer: pipe (from mailer)"); 728c579ef51Seric (void) close(mpvect[0]); 729c579ef51Seric (void) close(mpvect[1]); 730c579ef51Seric return (-1); 731c579ef51Seric } 7322c7e1b8dSeric #endif SMTP 733c579ef51Seric 73433db8731Seric /* 73533db8731Seric ** Actually fork the mailer process. 73633db8731Seric ** DOFORK is clever about retrying. 73733db8731Seric */ 73833db8731Seric 7399a6a5f55Seric if (CurEnv->e_xfp != NULL) 7409a6a5f55Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 741588cad61Seric (void) fflush(stdout); 74232d19d43Seric DOFORK(XFORK); 743f129ec7dSeric /* pid is set by DOFORK */ 74425a99e2eSeric if (pid < 0) 74525a99e2eSeric { 74633db8731Seric /* failure */ 747588cad61Seric syserr("openmailer: cannot fork"); 748f8952a83Seric (void) close(mpvect[0]); 749f8952a83Seric (void) close(mpvect[1]); 750588cad61Seric #ifdef SMTP 751c579ef51Seric if (clever) 752c579ef51Seric { 753c579ef51Seric (void) close(rpvect[0]); 754c579ef51Seric (void) close(rpvect[1]); 755c579ef51Seric } 756588cad61Seric #endif SMTP 75725a99e2eSeric return (-1); 75825a99e2eSeric } 75925a99e2eSeric else if (pid == 0) 76025a99e2eSeric { 76125a99e2eSeric /* child -- set up input & exec mailer */ 76203ab8e55Seric /* make diagnostic output be standard output */ 7638f0e7860Seric (void) signal(SIGINT, SIG_IGN); 7648f0e7860Seric (void) signal(SIGHUP, SIG_IGN); 7650984da9fSeric (void) signal(SIGTERM, SIG_DFL); 766f8952a83Seric 767f8952a83Seric /* arrange to filter standard & diag output of command */ 768c579ef51Seric if (clever) 769c579ef51Seric { 770c579ef51Seric (void) close(rpvect[0]); 771c579ef51Seric (void) close(1); 772c579ef51Seric (void) dup(rpvect[1]); 773c579ef51Seric (void) close(rpvect[1]); 774c579ef51Seric } 775276723a8Seric else if (OpMode == MD_SMTP || HoldErrs) 776f8952a83Seric { 777588cad61Seric /* put mailer output in transcript */ 778f8952a83Seric (void) close(1); 7799a6a5f55Seric (void) dup(fileno(CurEnv->e_xfp)); 780f8952a83Seric } 781db8841e9Seric (void) close(2); 782db8841e9Seric (void) dup(1); 783f8952a83Seric 784f8952a83Seric /* arrange to get standard input */ 785f8952a83Seric (void) close(mpvect[1]); 786db8841e9Seric (void) close(0); 787f8952a83Seric if (dup(mpvect[0]) < 0) 78825a99e2eSeric { 78925a99e2eSeric syserr("Cannot dup to zero!"); 790a590b978Seric _exit(EX_OSERR); 79125a99e2eSeric } 792f8952a83Seric (void) close(mpvect[0]); 79357fc6f17Seric if (!bitnset(M_RESTR, m->m_flags)) 7940984da9fSeric { 795e36b99e2Seric if (ctladdr->q_uid == 0) 796e36b99e2Seric { 797e36b99e2Seric (void) setgid(DefGid); 798e36b99e2Seric (void) setuid(DefUid); 799e36b99e2Seric } 800e36b99e2Seric else 80169f29479Seric { 802e36b99e2Seric (void) setgid(ctladdr->q_gid); 803e36b99e2Seric (void) setuid(ctladdr->q_uid); 80469f29479Seric } 8050984da9fSeric } 806588cad61Seric 807e374fd72Seric /* 808e374fd72Seric ** We have to be careful with vfork - we can't mung up the 809e374fd72Seric ** memory but we don't want the mailer to inherit any extra 810e374fd72Seric ** open files. Chances are the mailer won't 811e374fd72Seric ** care about an extra file, but then again you never know. 812e374fd72Seric ** Actually, we would like to close(fileno(pwf)), but it's 813e374fd72Seric ** declared static so we can't. But if we fclose(pwf), which 814e374fd72Seric ** is what endpwent does, it closes it in the parent too and 815e374fd72Seric ** the next getpwnam will be slower. If you have a weird 816e374fd72Seric ** mailer that chokes on the extra file you should do the 8174300ddf0Seric ** endpwent(). -MRH 818e374fd72Seric ** 819e374fd72Seric ** Similar comments apply to log. However, openlog is 820e374fd72Seric ** clever enough to set the FIOCLEX mode on the file, 821e374fd72Seric ** so it will be closed automatically on the exec. 822e374fd72Seric */ 823e374fd72Seric 824588cad61Seric closeall(); 82533db8731Seric 82633db8731Seric /* try to execute the mailer */ 82725a99e2eSeric execv(m->m_mailer, pvp); 82833db8731Seric 82925a99e2eSeric /* syserr fails because log is closed */ 83025a99e2eSeric /* syserr("Cannot exec %s", m->m_mailer); */ 83132d19d43Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 832db8841e9Seric (void) fflush(stdout); 833a590b978Seric _exit(EX_UNAVAILABLE); 83425a99e2eSeric } 83525a99e2eSeric 836f8952a83Seric /* 837c579ef51Seric ** Set up return value. 838f8952a83Seric */ 839f8952a83Seric 840f8952a83Seric (void) close(mpvect[0]); 841f8952a83Seric mfile = fdopen(mpvect[1], "w"); 842c579ef51Seric if (clever) 84325a99e2eSeric { 844c579ef51Seric (void) close(rpvect[1]); 845c579ef51Seric rfile = fdopen(rpvect[0], "r"); 84625a99e2eSeric } 847c579ef51Seric 848c579ef51Seric *pmfile = mfile; 849c579ef51Seric *prfile = rfile; 850c579ef51Seric 851c579ef51Seric return (pid); 85225a99e2eSeric } 85325a99e2eSeric /* 85425a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 85525a99e2eSeric ** 85625a99e2eSeric ** Parameters: 85725a99e2eSeric ** stat -- the status code from the mailer (high byte 85825a99e2eSeric ** only; core dumps must have been taken care of 85925a99e2eSeric ** already). 86025a99e2eSeric ** m -- the mailer descriptor for this mailer. 86125a99e2eSeric ** 86225a99e2eSeric ** Returns: 863db8841e9Seric ** none. 86425a99e2eSeric ** 86525a99e2eSeric ** Side Effects: 866c1f9df2cSeric ** Errors may be incremented. 86725a99e2eSeric ** ExitStat may be set. 86825a99e2eSeric */ 86925a99e2eSeric 870198d9be0Seric giveresponse(stat, m, e) 87125a99e2eSeric int stat; 872588cad61Seric register MAILER *m; 873198d9be0Seric ENVELOPE *e; 87425a99e2eSeric { 87525a99e2eSeric register char *statmsg; 87625a99e2eSeric extern char *SysExMsg[]; 87725a99e2eSeric register int i; 87825a99e2eSeric extern int N_SysEx; 879198d9be0Seric char buf[MAXLINE]; 88025a99e2eSeric 8817d1fc79dSeric #ifdef lint 8827d1fc79dSeric if (m == NULL) 8837d1fc79dSeric return; 8847d1fc79dSeric #endif lint 8857d1fc79dSeric 88613bbc08cSeric /* 88713bbc08cSeric ** Compute status message from code. 88813bbc08cSeric */ 88913bbc08cSeric 89025a99e2eSeric i = stat - EX__BASE; 891588cad61Seric if (stat == 0) 892588cad61Seric statmsg = "250 Sent"; 893588cad61Seric else if (i < 0 || i > N_SysEx) 894588cad61Seric { 895588cad61Seric (void) sprintf(buf, "554 unknown mailer error %d", stat); 896588cad61Seric stat = EX_UNAVAILABLE; 897588cad61Seric statmsg = buf; 898588cad61Seric } 899198d9be0Seric else if (stat == EX_TEMPFAIL) 900198d9be0Seric { 901198d9be0Seric extern char *sys_errlist[]; 902198d9be0Seric extern int sys_nerr; 903198d9be0Seric 9048557d168Seric (void) strcpy(buf, SysExMsg[i]); 9058557d168Seric if (errno != 0) 906198d9be0Seric { 9078557d168Seric (void) strcat(buf, ": "); 9088557d168Seric if (errno > 0 && errno < sys_nerr) 909198d9be0Seric (void) strcat(buf, sys_errlist[errno]); 910198d9be0Seric else 9118557d168Seric { 9128557d168Seric char xbuf[30]; 9138557d168Seric 9148557d168Seric (void) sprintf(xbuf, "Error %d", errno); 9158557d168Seric (void) strcat(buf, xbuf); 9168557d168Seric } 9178557d168Seric } 918198d9be0Seric statmsg = buf; 919198d9be0Seric } 92025a99e2eSeric else 92125a99e2eSeric statmsg = SysExMsg[i]; 922588cad61Seric 923588cad61Seric /* 924588cad61Seric ** Print the message as appropriate 925588cad61Seric */ 926588cad61Seric 927198d9be0Seric if (stat == EX_OK || stat == EX_TEMPFAIL) 9285826d9d3Seric message(Arpa_Info, &statmsg[4]); 92925a99e2eSeric else 93025a99e2eSeric { 931c1f9df2cSeric Errors++; 9325826d9d3Seric usrerr(statmsg); 93325a99e2eSeric } 93425a99e2eSeric 93525a99e2eSeric /* 93625a99e2eSeric ** Final cleanup. 93725a99e2eSeric ** Log a record of the transaction. Compute the new 93825a99e2eSeric ** ExitStat -- if we already had an error, stick with 93925a99e2eSeric ** that. 94025a99e2eSeric */ 94125a99e2eSeric 94261f5a1d4Seric if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2)) 943eb238f8cSeric logdelivery(&statmsg[4]); 944eb238f8cSeric 945eb238f8cSeric if (stat != EX_TEMPFAIL) 946eb238f8cSeric setstat(stat); 947198d9be0Seric if (stat != EX_OK) 948198d9be0Seric { 949198d9be0Seric if (e->e_message != NULL) 950198d9be0Seric free(e->e_message); 951198d9be0Seric e->e_message = newstr(&statmsg[4]); 952198d9be0Seric } 9538557d168Seric errno = 0; 954eb238f8cSeric } 955eb238f8cSeric /* 956eb238f8cSeric ** LOGDELIVERY -- log the delivery in the system log 957eb238f8cSeric ** 958eb238f8cSeric ** Parameters: 959eb238f8cSeric ** stat -- the message to print for the status 960eb238f8cSeric ** 961eb238f8cSeric ** Returns: 962eb238f8cSeric ** none 963eb238f8cSeric ** 964eb238f8cSeric ** Side Effects: 965eb238f8cSeric ** none 966eb238f8cSeric */ 967eb238f8cSeric 968eb238f8cSeric logdelivery(stat) 969eb238f8cSeric char *stat; 9705cf56be3Seric { 9715cf56be3Seric extern char *pintvl(); 9725cf56be3Seric 973eb238f8cSeric # ifdef LOG 9745cf56be3Seric syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id, 975eb238f8cSeric CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), stat); 97625a99e2eSeric # endif LOG 97725a99e2eSeric } 97825a99e2eSeric /* 97951552439Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 98025a99e2eSeric ** 98151552439Seric ** This can be made an arbitrary message separator by changing $l 98251552439Seric ** 98351552439Seric ** One of the ugliest hacks seen by human eyes is 98451552439Seric ** contained herein: UUCP wants those stupid 985588cad61Seric ** "emote from <host>" lines. Why oh why does a 98651552439Seric ** well-meaning programmer such as myself have to 98751552439Seric ** deal with this kind of antique garbage???? 98825a99e2eSeric ** 98925a99e2eSeric ** Parameters: 99051552439Seric ** fp -- the file to output to. 99151552439Seric ** m -- the mailer describing this entry. 99225a99e2eSeric ** 99325a99e2eSeric ** Returns: 99451552439Seric ** none 99525a99e2eSeric ** 99625a99e2eSeric ** Side Effects: 99751552439Seric ** outputs some text to fp. 99825a99e2eSeric */ 99925a99e2eSeric 100077b52738Seric putfromline(fp, m) 100151552439Seric register FILE *fp; 100251552439Seric register MAILER *m; 100325a99e2eSeric { 1004*ea09d6edSeric char *template = "$l\n"; 100551552439Seric char buf[MAXLINE]; 100625a99e2eSeric 100757fc6f17Seric if (bitnset(M_NHDR, m->m_flags)) 100851552439Seric return; 100913bbc08cSeric 10102c7e1b8dSeric # ifdef UGLYUUCP 101157fc6f17Seric if (bitnset(M_UGLYUUCP, m->m_flags)) 101274b6e67bSeric { 1013*ea09d6edSeric char *bang; 1014*ea09d6edSeric char xbuf[MAXLINE]; 101574b6e67bSeric 1016*ea09d6edSeric expand("$g", buf, &buf[sizeof buf - 1], CurEnv); 1017*ea09d6edSeric bang = index(buf, '!'); 101874b6e67bSeric if (bang == NULL) 1019*ea09d6edSeric syserr("No ! in UUCP! (%s)", buf); 102074b6e67bSeric else 1021588cad61Seric { 1022*ea09d6edSeric *bang++ = '\0'; 1023*ea09d6edSeric (void) sprintf(xbuf, "From %s $d remote from %s\n", buf, bang); 1024*ea09d6edSeric template = xbuf; 102574b6e67bSeric } 1026588cad61Seric } 10272c7e1b8dSeric # endif UGLYUUCP 1028*ea09d6edSeric expand(template, buf, &buf[sizeof buf - 1], CurEnv); 102977b52738Seric putline(buf, fp, m); 1030bc6e2962Seric } 1031bc6e2962Seric /* 103251552439Seric ** PUTBODY -- put the body of a message. 103351552439Seric ** 103451552439Seric ** Parameters: 103551552439Seric ** fp -- file to output onto. 103677b52738Seric ** m -- a mailer descriptor to control output format. 10379a6a5f55Seric ** e -- the envelope to put out. 103851552439Seric ** 103951552439Seric ** Returns: 104051552439Seric ** none. 104151552439Seric ** 104251552439Seric ** Side Effects: 104351552439Seric ** The message is written onto fp. 104451552439Seric */ 104551552439Seric 104677b52738Seric putbody(fp, m, e) 104751552439Seric FILE *fp; 1048588cad61Seric MAILER *m; 10499a6a5f55Seric register ENVELOPE *e; 105051552439Seric { 105177b52738Seric char buf[MAXLINE]; 105251552439Seric 105351552439Seric /* 105451552439Seric ** Output the body of the message 105551552439Seric */ 105651552439Seric 10579a6a5f55Seric if (e->e_dfp == NULL) 105851552439Seric { 10599a6a5f55Seric if (e->e_df != NULL) 10609a6a5f55Seric { 10619a6a5f55Seric e->e_dfp = fopen(e->e_df, "r"); 10629a6a5f55Seric if (e->e_dfp == NULL) 10639a6a5f55Seric syserr("Cannot open %s", e->e_df); 10649a6a5f55Seric } 10659a6a5f55Seric else 106677b52738Seric putline("<<< No Message Collected >>>", fp, m); 10679a6a5f55Seric } 10689a6a5f55Seric if (e->e_dfp != NULL) 10699a6a5f55Seric { 10709a6a5f55Seric rewind(e->e_dfp); 107177b52738Seric while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 107277b52738Seric putline(buf, fp, m); 107351552439Seric 10749a6a5f55Seric if (ferror(e->e_dfp)) 107551552439Seric { 107651552439Seric syserr("putbody: read error"); 107751552439Seric ExitStat = EX_IOERR; 107851552439Seric } 107951552439Seric } 108051552439Seric 108151552439Seric (void) fflush(fp); 108251552439Seric if (ferror(fp) && errno != EPIPE) 108351552439Seric { 108451552439Seric syserr("putbody: write error"); 108551552439Seric ExitStat = EX_IOERR; 108651552439Seric } 108751552439Seric errno = 0; 108825a99e2eSeric } 108925a99e2eSeric /* 109025a99e2eSeric ** MAILFILE -- Send a message to a file. 109125a99e2eSeric ** 1092f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 1093f129ec7dSeric ** bits, sendmail will try to become the owner of that file 1094f129ec7dSeric ** rather than the real user. Obviously, this only works if 1095f129ec7dSeric ** sendmail runs as root. 1096f129ec7dSeric ** 1097588cad61Seric ** This could be done as a subordinate mailer, except that it 1098588cad61Seric ** is used implicitly to save messages in ~/dead.letter. We 1099588cad61Seric ** view this as being sufficiently important as to include it 1100588cad61Seric ** here. For example, if the system is dying, we shouldn't have 1101588cad61Seric ** to create another process plus some pipes to save the message. 1102588cad61Seric ** 110325a99e2eSeric ** Parameters: 110425a99e2eSeric ** filename -- the name of the file to send to. 11056259796dSeric ** ctladdr -- the controlling address header -- includes 11066259796dSeric ** the userid/groupid to be when sending. 110725a99e2eSeric ** 110825a99e2eSeric ** Returns: 110925a99e2eSeric ** The exit code associated with the operation. 111025a99e2eSeric ** 111125a99e2eSeric ** Side Effects: 111225a99e2eSeric ** none. 111325a99e2eSeric */ 111425a99e2eSeric 11156259796dSeric mailfile(filename, ctladdr) 111625a99e2eSeric char *filename; 11176259796dSeric ADDRESS *ctladdr; 111825a99e2eSeric { 111925a99e2eSeric register FILE *f; 112032d19d43Seric register int pid; 112125a99e2eSeric 112232d19d43Seric /* 112332d19d43Seric ** Fork so we can change permissions here. 112432d19d43Seric ** Note that we MUST use fork, not vfork, because of 112532d19d43Seric ** the complications of calling subroutines, etc. 112632d19d43Seric */ 112732d19d43Seric 112832d19d43Seric DOFORK(fork); 112932d19d43Seric 113032d19d43Seric if (pid < 0) 113132d19d43Seric return (EX_OSERR); 113232d19d43Seric else if (pid == 0) 113332d19d43Seric { 113432d19d43Seric /* child -- actually write to file */ 1135f129ec7dSeric struct stat stb; 1136f129ec7dSeric 11370984da9fSeric (void) signal(SIGINT, SIG_DFL); 11380984da9fSeric (void) signal(SIGHUP, SIG_DFL); 11390984da9fSeric (void) signal(SIGTERM, SIG_DFL); 1140f129ec7dSeric umask(OldUmask); 1141f129ec7dSeric if (stat(filename, &stb) < 0) 114224447f54Seric { 114324447f54Seric errno = 0; 1144e6e1265fSeric stb.st_mode = 0666; 114524447f54Seric } 1146f129ec7dSeric if (bitset(0111, stb.st_mode)) 1147f129ec7dSeric exit(EX_CANTCREAT); 114803827b5fSeric if (ctladdr == NULL) 11497a941e7aSeric ctladdr = &CurEnv->e_from; 1150f129ec7dSeric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 1151e36b99e2Seric { 1152e36b99e2Seric if (ctladdr->q_uid == 0) 1153e36b99e2Seric (void) setgid(DefGid); 1154e36b99e2Seric else 11556259796dSeric (void) setgid(ctladdr->q_gid); 1156e36b99e2Seric } 1157f129ec7dSeric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 1158e36b99e2Seric { 1159e36b99e2Seric if (ctladdr->q_uid == 0) 1160e36b99e2Seric (void) setuid(DefUid); 1161e36b99e2Seric else 11626259796dSeric (void) setuid(ctladdr->q_uid); 1163e36b99e2Seric } 116427628d59Seric f = dfopen(filename, "a"); 116525a99e2eSeric if (f == NULL) 116632d19d43Seric exit(EX_CANTCREAT); 116725a99e2eSeric 116877b52738Seric putfromline(f, ProgMailer); 116977b52738Seric (*CurEnv->e_puthdr)(f, ProgMailer, CurEnv); 117077b52738Seric putline("\n", f, ProgMailer); 117177b52738Seric (*CurEnv->e_putbody)(f, ProgMailer, CurEnv); 117277b52738Seric putline("\n", f, ProgMailer); 1173db8841e9Seric (void) fclose(f); 117432d19d43Seric (void) fflush(stdout); 1175e36b99e2Seric 117627628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 1177c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 117832d19d43Seric exit(EX_OK); 117913bbc08cSeric /*NOTREACHED*/ 118032d19d43Seric } 118132d19d43Seric else 118232d19d43Seric { 118332d19d43Seric /* parent -- wait for exit status */ 1184588cad61Seric int st; 118532d19d43Seric 1186588cad61Seric st = waitfor(pid); 1187588cad61Seric if ((st & 0377) != 0) 1188588cad61Seric return (EX_UNAVAILABLE); 1189588cad61Seric else 1190588cad61Seric return ((st >> 8) & 0377); 119132d19d43Seric } 119225a99e2eSeric } 1193ea4dc939Seric /* 1194ea4dc939Seric ** SENDALL -- actually send all the messages. 1195ea4dc939Seric ** 1196ea4dc939Seric ** Parameters: 11970c52a0b3Seric ** e -- the envelope to send. 1198276723a8Seric ** mode -- the delivery mode to use. 1199ea4dc939Seric ** 1200ea4dc939Seric ** Returns: 1201ea4dc939Seric ** none. 1202ea4dc939Seric ** 1203ea4dc939Seric ** Side Effects: 1204ea4dc939Seric ** Scans the send lists and sends everything it finds. 12050c52a0b3Seric ** Delivers any appropriate error messages. 1206276723a8Seric ** If we are running in a non-interactive mode, takes the 1207276723a8Seric ** appropriate action. 1208ea4dc939Seric */ 1209ea4dc939Seric 1210276723a8Seric sendall(e, mode) 12110c52a0b3Seric ENVELOPE *e; 1212276723a8Seric char mode; 1213ea4dc939Seric { 1214e77e673fSeric register ADDRESS *q; 121514a8ed7aSeric bool oldverbose; 1216276723a8Seric int pid; 1217ea4dc939Seric 1218772e6e50Seric #ifdef DEBUG 1219df864a8fSeric if (tTd(13, 1)) 1220772e6e50Seric { 1221276723a8Seric printf("\nSENDALL: mode %c, sendqueue:\n", mode); 12220c52a0b3Seric printaddr(e->e_sendqueue, TRUE); 1223772e6e50Seric } 1224772e6e50Seric #endif DEBUG 1225ea4dc939Seric 12260c52a0b3Seric /* 1227276723a8Seric ** Do any preprocessing necessary for the mode we are running. 1228588cad61Seric ** Check to make sure the hop count is reasonable. 1229588cad61Seric ** Delete sends to the sender in mailing lists. 1230276723a8Seric */ 1231276723a8Seric 1232588cad61Seric CurEnv = e; 1233276723a8Seric 1234588cad61Seric if (e->e_hopcount > MAXHOP) 1235276723a8Seric { 1236588cad61Seric syserr("sendall: too many hops (%d max)", MAXHOP); 1237588cad61Seric return; 1238588cad61Seric } 1239588cad61Seric 1240588cad61Seric if (!MeToo) 1241276723a8Seric { 1242588cad61Seric e->e_from.q_flags |= QDONTSEND; 1243588cad61Seric recipient(&e->e_from, &e->e_sendqueue); 1244276723a8Seric } 1245588cad61Seric 1246588cad61Seric # ifdef QUEUE 1247b254bcb6Seric if ((mode == SM_QUEUE || mode == SM_FORK || 1248b254bcb6Seric (mode != SM_VERIFY && SuperSafe)) && 1249b254bcb6Seric !bitset(EF_INQUEUE, e->e_flags)) 1250588cad61Seric queueup(e, TRUE, mode == SM_QUEUE); 1251276723a8Seric #endif QUEUE 1252276723a8Seric 1253276723a8Seric oldverbose = Verbose; 1254276723a8Seric switch (mode) 1255276723a8Seric { 1256276723a8Seric case SM_VERIFY: 1257276723a8Seric Verbose = TRUE; 1258276723a8Seric break; 1259276723a8Seric 1260276723a8Seric case SM_QUEUE: 1261b254bcb6Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 1262276723a8Seric return; 1263276723a8Seric 1264276723a8Seric case SM_FORK: 12659a6a5f55Seric if (e->e_xfp != NULL) 12669a6a5f55Seric (void) fflush(e->e_xfp); 1267276723a8Seric pid = fork(); 1268276723a8Seric if (pid < 0) 1269276723a8Seric { 1270276723a8Seric mode = SM_DELIVER; 1271276723a8Seric break; 1272276723a8Seric } 1273276723a8Seric else if (pid > 0) 1274a6fce3d8Seric { 1275a6fce3d8Seric /* be sure we leave the temp files to our child */ 1276b254bcb6Seric e->e_id = e->e_df = NULL; 1277276723a8Seric return; 1278a6fce3d8Seric } 1279276723a8Seric 1280276723a8Seric /* double fork to avoid zombies */ 1281276723a8Seric if (fork() > 0) 1282276723a8Seric exit(EX_OK); 1283276723a8Seric 1284a6fce3d8Seric /* be sure we are immune from the terminal */ 1285769e215aSeric disconnect(FALSE); 1286a6fce3d8Seric 1287276723a8Seric break; 1288276723a8Seric } 1289276723a8Seric 1290276723a8Seric /* 12910c52a0b3Seric ** Run through the list and send everything. 12920c52a0b3Seric */ 12930c52a0b3Seric 12940c52a0b3Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1295ea4dc939Seric { 1296276723a8Seric if (mode == SM_VERIFY) 1297ea4dc939Seric { 1298a6fce3d8Seric e->e_to = q->q_paddr; 1299e77e673fSeric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1300ea4dc939Seric message(Arpa_Info, "deliverable"); 1301ea4dc939Seric } 1302ea4dc939Seric else 1303588cad61Seric (void) deliver(e, q); 1304ea4dc939Seric } 130514a8ed7aSeric Verbose = oldverbose; 13060c52a0b3Seric 13070c52a0b3Seric /* 13080c52a0b3Seric ** Now run through and check for errors. 13090c52a0b3Seric */ 13100c52a0b3Seric 1311276723a8Seric if (mode == SM_VERIFY) 13120c52a0b3Seric return; 13130c52a0b3Seric 13140c52a0b3Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 13150c52a0b3Seric { 13160c52a0b3Seric register ADDRESS *qq; 13170c52a0b3Seric 1318df864a8fSeric # ifdef DEBUG 1319df864a8fSeric if (tTd(13, 3)) 1320df864a8fSeric { 1321df864a8fSeric printf("Checking "); 1322df864a8fSeric printaddr(q, FALSE); 1323df864a8fSeric } 1324df864a8fSeric # endif DEBUG 1325df864a8fSeric 1326b254bcb6Seric /* only send errors if the message failed */ 1327b254bcb6Seric if (!bitset(QBADADDR, q->q_flags)) 1328b254bcb6Seric continue; 13290c52a0b3Seric 13300c52a0b3Seric /* we have an address that failed -- find the parent */ 13310c52a0b3Seric for (qq = q; qq != NULL; qq = qq->q_alias) 13320c52a0b3Seric { 13330c52a0b3Seric char obuf[MAXNAME + 6]; 13340c52a0b3Seric extern char *aliaslookup(); 13350c52a0b3Seric 13360c52a0b3Seric /* we can only have owners for local addresses */ 133757fc6f17Seric if (!bitnset(M_LOCAL, qq->q_mailer->m_flags)) 13380c52a0b3Seric continue; 13390c52a0b3Seric 13400c52a0b3Seric /* see if the owner list exists */ 13410c52a0b3Seric (void) strcpy(obuf, "owner-"); 1342cec031e3Seric if (strncmp(qq->q_user, "owner-", 6) == 0) 1343cec031e3Seric (void) strcat(obuf, "owner"); 1344cec031e3Seric else 13450c52a0b3Seric (void) strcat(obuf, qq->q_user); 13460c52a0b3Seric if (aliaslookup(obuf) == NULL) 13470c52a0b3Seric continue; 13480c52a0b3Seric 1349df864a8fSeric # ifdef DEBUG 1350df864a8fSeric if (tTd(13, 4)) 1351df864a8fSeric printf("Errors to %s\n", obuf); 1352df864a8fSeric # endif DEBUG 1353df864a8fSeric 13540c52a0b3Seric /* owner list exists -- add it to the error queue */ 1355e3e4ed86Seric sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue); 1356588cad61Seric ErrorMode == EM_MAIL; 13570c52a0b3Seric break; 13580c52a0b3Seric } 13590c52a0b3Seric 13600c52a0b3Seric /* if we did not find an owner, send to the sender */ 13617455aa0bSeric if (qq == NULL && bitset(QBADADDR, q->q_flags)) 1362e3e4ed86Seric sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue); 13630c52a0b3Seric } 1364276723a8Seric 1365276723a8Seric if (mode == SM_FORK) 1366276723a8Seric finis(); 13670c52a0b3Seric } 1368