125a99e2eSeric # include <signal.h> 254aa2b0fSeric # include <errno.h> 3b20b3270Seric # include "sendmail.h" 4c77d1c25Seric # include <sys/stat.h> 525a99e2eSeric 6*83b7ddc9Seric SCCSID(@(#)deliver.c 3.139 01/03/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 */ 402a6e0786Seric extern bool checkcompat(); 415dfc646bSeric char *pv[MAXPV+1]; 424b22ea87Seric char tobuf[MAXLINE-50]; /* text line of to people */ 435dfc646bSeric char buf[MAXNAME]; 446259796dSeric ADDRESS *ctladdr; 456259796dSeric extern ADDRESS *getctladdr(); 4678442df3Seric char tfrombuf[MAXNAME]; /* translated from person */ 4778442df3Seric extern char **prescan(); 48c77d1c25Seric register ADDRESS *to = firstto; 49c579ef51Seric bool clever = FALSE; /* running user smtp to this mailer */ 50772e6e50Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 515826d9d3Seric register int rcode; /* response code */ 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 76317680d6Seric if (NoConnect && !QueueRun && bitset(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); 10678442df3Seric mvp = prescan(buf, '\0'); 107588cad61Seric rewrite(mvp, 3); 108588cad61Seric rewrite(mvp, 1); 1093bea8136Seric rewrite(mvp, m->m_s_rwset); 11078442df3Seric cataddr(mvp, tfrombuf, sizeof tfrombuf); 11178442df3Seric 112588cad61Seric define('g', tfrombuf, e); /* translated sender address */ 113588cad61Seric define('h', host, e); /* to host */ 1145dfc646bSeric Errors = 0; 1155dfc646bSeric pvp = pv; 1165dfc646bSeric *pvp++ = m->m_argv[0]; 1175dfc646bSeric 1185dfc646bSeric /* insert -f or -r flag as appropriate */ 1195dfc646bSeric if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag) 1205dfc646bSeric { 1215dfc646bSeric if (bitset(M_FOPT, m->m_flags)) 1225dfc646bSeric *pvp++ = "-f"; 1235dfc646bSeric else 1245dfc646bSeric *pvp++ = "-r"; 125588cad61Seric expand("$g", buf, &buf[sizeof buf - 1], e); 1265dfc646bSeric *pvp++ = newstr(buf); 1275dfc646bSeric } 1285dfc646bSeric 1295dfc646bSeric /* 1305dfc646bSeric ** Append the other fixed parts of the argv. These run 1315dfc646bSeric ** up to the first entry containing "$u". There can only 1325dfc646bSeric ** be one of these, and there are only a few more slots 1335dfc646bSeric ** in the pv after it. 1345dfc646bSeric */ 1355dfc646bSeric 1365dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 1375dfc646bSeric { 1385dfc646bSeric while ((p = index(p, '$')) != NULL) 1395dfc646bSeric if (*++p == 'u') 1405dfc646bSeric break; 1415dfc646bSeric if (p != NULL) 1425dfc646bSeric break; 1435dfc646bSeric 1445dfc646bSeric /* this entry is safe -- go ahead and process it */ 145588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 1465dfc646bSeric *pvp++ = newstr(buf); 1475dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 1485dfc646bSeric { 1495dfc646bSeric syserr("Too many parameters to %s before $u", pv[0]); 1505dfc646bSeric return (-1); 1515dfc646bSeric } 1525dfc646bSeric } 153c579ef51Seric 15433db8731Seric /* 15533db8731Seric ** If we have no substitution for the user name in the argument 15633db8731Seric ** list, we know that we must supply the names otherwise -- and 15733db8731Seric ** SMTP is the answer!! 15833db8731Seric */ 15933db8731Seric 1605dfc646bSeric if (*mvp == NULL) 161c579ef51Seric { 162c579ef51Seric /* running SMTP */ 1632c7e1b8dSeric # ifdef SMTP 164c579ef51Seric clever = TRUE; 165c579ef51Seric *pvp = NULL; 1662c7e1b8dSeric # else SMTP 16733db8731Seric /* oops! we don't implement SMTP */ 1682c7e1b8dSeric syserr("SMTP style mailer"); 1692c7e1b8dSeric return (EX_SOFTWARE); 1702c7e1b8dSeric # endif SMTP 171c579ef51Seric } 1725dfc646bSeric 1735dfc646bSeric /* 1745dfc646bSeric ** At this point *mvp points to the argument with $u. We 1755dfc646bSeric ** run through our address list and append all the addresses 1765dfc646bSeric ** we can. If we run out of space, do not fret! We can 1775dfc646bSeric ** always send another copy later. 1785dfc646bSeric */ 1795dfc646bSeric 1805dfc646bSeric tobuf[0] = '\0'; 181588cad61Seric e->e_to = tobuf; 1826259796dSeric ctladdr = NULL; 1835dfc646bSeric for (; to != NULL; to = to->q_next) 1845dfc646bSeric { 1855dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 186bea22b26Seric if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags)) 1875dfc646bSeric break; 1885dfc646bSeric 1895dfc646bSeric /* if already sent or not for this host, don't send */ 190da2935e1Seric if (bitset(QDONTSEND, to->q_flags) || 191da2935e1Seric strcmp(to->q_host, host) != 0 || 192da2935e1Seric to->q_mailer != firstto->q_mailer) 1935dfc646bSeric continue; 1946259796dSeric 1954b22ea87Seric /* avoid overflowing tobuf */ 196588cad61Seric if (sizeof tobuf - (strlen(to->q_paddr) + strlen(tobuf) + 2) < 0) 1974b22ea87Seric break; 1984b22ea87Seric 199772e6e50Seric # ifdef DEBUG 2006ef48975Seric if (tTd(10, 1)) 201772e6e50Seric { 202772e6e50Seric printf("\nsend to "); 203772e6e50Seric printaddr(to, FALSE); 204772e6e50Seric } 205772e6e50Seric # endif DEBUG 206772e6e50Seric 2076259796dSeric /* compute effective uid/gid when sending */ 2087da1035fSeric if (to->q_mailer == ProgMailer) 2096259796dSeric ctladdr = getctladdr(to); 2106259796dSeric 2115dfc646bSeric user = to->q_user; 212588cad61Seric e->e_to = to->q_paddr; 2135dfc646bSeric to->q_flags |= QDONTSEND; 2145dfc646bSeric 2155dfc646bSeric /* 2165dfc646bSeric ** Check to see that these people are allowed to 2175dfc646bSeric ** talk to each other. 2182a6e0786Seric */ 2192a6e0786Seric 2202a6e0786Seric if (!checkcompat(to)) 2215dfc646bSeric { 222588cad61Seric giveresponse(EX_UNAVAILABLE, m); 2235dfc646bSeric continue; 2245dfc646bSeric } 2252a6e0786Seric 2262a6e0786Seric /* 2279ec9501bSeric ** Strip quote bits from names if the mailer is dumb 2289ec9501bSeric ** about them. 22925a99e2eSeric */ 23025a99e2eSeric 2312a6e0786Seric if (bitset(M_STRIPQ, m->m_flags)) 23225a99e2eSeric { 2339ec9501bSeric stripquotes(user, TRUE); 2349ec9501bSeric stripquotes(host, TRUE); 2359ec9501bSeric } 2369ec9501bSeric else 2379ec9501bSeric { 2389ec9501bSeric stripquotes(user, FALSE); 2399ec9501bSeric stripquotes(host, FALSE); 24025a99e2eSeric } 24125a99e2eSeric 242cdb828c5Seric /* hack attack -- delivermail compatibility */ 243cdb828c5Seric if (m == ProgMailer && *user == '|') 244cdb828c5Seric user++; 245cdb828c5Seric 24625a99e2eSeric /* 2473efaed6eSeric ** If an error message has already been given, don't 2483efaed6eSeric ** bother to send to this address. 2493efaed6eSeric ** 2503efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 2513efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 2523efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 2533efaed6eSeric */ 2543efaed6eSeric 2556cae517dSeric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 2563efaed6eSeric continue; 2573efaed6eSeric 258f2fec898Seric /* save statistics.... */ 259588cad61Seric markstats(e, to); 260f2fec898Seric 2613efaed6eSeric /* 26225a99e2eSeric ** See if this user name is "special". 26325a99e2eSeric ** If the user name has a slash in it, assume that this 26451552439Seric ** is a file -- send it off without further ado. Note 26551552439Seric ** that this type of addresses is not processed along 26651552439Seric ** with the others, so we fudge on the To person. 26725a99e2eSeric */ 26825a99e2eSeric 2697da1035fSeric if (m == LocalMailer) 27025a99e2eSeric { 271a49f24c0Seric if (user[0] == '/') 27225a99e2eSeric { 2735826d9d3Seric rcode = mailfile(user, getctladdr(to)); 274588cad61Seric giveresponse(rcode, m); 2755dfc646bSeric continue; 27625a99e2eSeric } 27725a99e2eSeric } 27825a99e2eSeric 27913bbc08cSeric /* 28013bbc08cSeric ** Address is verified -- add this user to mailer 28113bbc08cSeric ** argv, and add it to the print list of recipients. 28213bbc08cSeric */ 28313bbc08cSeric 284508daeccSeric /* link together the chain of recipients */ 285508daeccSeric to->q_tchain = tochain; 286508daeccSeric tochain = to; 287508daeccSeric 2885dfc646bSeric /* create list of users for error messages */ 289db8841e9Seric (void) strcat(tobuf, ","); 290db8841e9Seric (void) strcat(tobuf, to->q_paddr); 291588cad61Seric define('u', user, e); /* to user */ 292588cad61Seric define('z', to->q_home, e); /* user's home */ 2935dfc646bSeric 294c579ef51Seric /* 295508daeccSeric ** Expand out this user into argument list. 296c579ef51Seric */ 297c579ef51Seric 298508daeccSeric if (!clever) 299c579ef51Seric { 300588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 3015dfc646bSeric *pvp++ = newstr(buf); 3025dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 3035dfc646bSeric { 3045dfc646bSeric /* allow some space for trailing parms */ 3055dfc646bSeric break; 3065dfc646bSeric } 3075dfc646bSeric } 308c579ef51Seric } 3095dfc646bSeric 310145b49b1Seric /* see if any addresses still exist */ 311145b49b1Seric if (tobuf[0] == '\0') 312c579ef51Seric { 313588cad61Seric define('g', (char *) NULL, e); 314145b49b1Seric return (0); 315c579ef51Seric } 316145b49b1Seric 3175dfc646bSeric /* print out messages as full list */ 31863780dbdSeric e->e_to = tobuf + 1; 3195dfc646bSeric 3205dfc646bSeric /* 3215dfc646bSeric ** Fill out any parameters after the $u parameter. 3225dfc646bSeric */ 3235dfc646bSeric 324c579ef51Seric while (!clever && *++mvp != NULL) 3255dfc646bSeric { 326588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 3275dfc646bSeric *pvp++ = newstr(buf); 3285dfc646bSeric if (pvp >= &pv[MAXPV]) 3295dfc646bSeric syserr("deliver: pv overflow after $u for %s", pv[0]); 3305dfc646bSeric } 3315dfc646bSeric *pvp++ = NULL; 3325dfc646bSeric 33325a99e2eSeric /* 33425a99e2eSeric ** Call the mailer. 3356328bdf7Seric ** The argument vector gets built, pipes 33625a99e2eSeric ** are created as necessary, and we fork & exec as 3376328bdf7Seric ** appropriate. 338c579ef51Seric ** If we are running SMTP, we just need to clean up. 33925a99e2eSeric */ 34025a99e2eSeric 341588cad61Seric message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name); 342588cad61Seric 3436259796dSeric if (ctladdr == NULL) 344588cad61Seric ctladdr = &e->e_from; 3452c7e1b8dSeric # ifdef SMTP 346c579ef51Seric if (clever) 347c579ef51Seric { 348588cad61Seric /* send the initial SMTP protocol */ 349588cad61Seric rcode = smtpinit(m, pv, (ADDRESS *) NULL); 350588cad61Seric 35163780dbdSeric if (rcode == EX_OK) 35263780dbdSeric { 353588cad61Seric /* send the recipient list */ 35463780dbdSeric tobuf[0] = '\0'; 355588cad61Seric for (to = tochain; to != NULL; to = to->q_tchain) 356588cad61Seric { 357588cad61Seric int i; 358588cad61Seric 35963780dbdSeric e->e_to = to->q_paddr; 360588cad61Seric i = smtprcpt(to); 361588cad61Seric if (i != EX_OK) 362588cad61Seric { 363*83b7ddc9Seric markfailure(e, to, i); 36463780dbdSeric giveresponse(i, m); 36563780dbdSeric } 36663780dbdSeric else 36763780dbdSeric { 36863780dbdSeric strcat(tobuf, ","); 36963780dbdSeric strcat(tobuf, to->q_paddr); 370588cad61Seric } 371588cad61Seric } 372588cad61Seric 37363780dbdSeric /* now send the data */ 37463780dbdSeric if (tobuf[0] == '\0') 37563780dbdSeric e->e_to = NULL; 37663780dbdSeric else 37763780dbdSeric { 37863780dbdSeric e->e_to = tobuf + 1; 379588cad61Seric rcode = smtpfinish(m, e); 38063780dbdSeric } 38163780dbdSeric 38263780dbdSeric /* now close the connection */ 38363780dbdSeric smtpquit(pv[0]); 38463780dbdSeric } 385c579ef51Seric } 386c579ef51Seric else 3872c7e1b8dSeric # endif SMTP 388ddf9b743Seric rcode = sendoff(e, m, pv, ctladdr, FALSE); 3895dfc646bSeric 390c77d1c25Seric /* 39163780dbdSeric ** Do final status disposal. 39263780dbdSeric ** We check for something in tobuf for the SMTP case. 393c77d1c25Seric ** If we got a temporary failure, arrange to queue the 394c77d1c25Seric ** addressees. 395c77d1c25Seric */ 396c77d1c25Seric 39763780dbdSeric if (tobuf[0] != '\0') 39863780dbdSeric giveresponse(rcode, m); 39963780dbdSeric if (rcode != EX_OK) 400c77d1c25Seric { 401772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 402*83b7ddc9Seric markfailure(e, to, rcode); 403c77d1c25Seric } 404c77d1c25Seric 40535490626Seric errno = 0; 406588cad61Seric define('g', (char *) NULL, e); 4075826d9d3Seric return (rcode); 40825a99e2eSeric } 4095dfc646bSeric /* 410*83b7ddc9Seric ** MARKFAILURE -- mark a failure on a specific address. 411*83b7ddc9Seric ** 412*83b7ddc9Seric ** Parameters: 413*83b7ddc9Seric ** e -- the envelope we are sending. 414*83b7ddc9Seric ** q -- the address to mark. 415*83b7ddc9Seric ** rcode -- the code signifying the particular failure. 416*83b7ddc9Seric ** 417*83b7ddc9Seric ** Returns: 418*83b7ddc9Seric ** none. 419*83b7ddc9Seric ** 420*83b7ddc9Seric ** Side Effects: 421*83b7ddc9Seric ** marks the address (and possibly the envelope) with the 422*83b7ddc9Seric ** failure so that an error will be returned or 423*83b7ddc9Seric ** the message will be queued, as appropriate. 424*83b7ddc9Seric */ 425*83b7ddc9Seric 426*83b7ddc9Seric markfailure(e, q, rcode) 427*83b7ddc9Seric register ENVELOPE *e; 428*83b7ddc9Seric register ADDRESS *q; 429*83b7ddc9Seric int rcode; 430*83b7ddc9Seric { 431*83b7ddc9Seric if (rcode == EX_OK) 432*83b7ddc9Seric return; 433*83b7ddc9Seric else if (rcode != EX_TEMPFAIL) 434*83b7ddc9Seric q->q_flags |= QBADADDR; 435*83b7ddc9Seric else if (curtime() > e->e_ctime + TimeOut) 436*83b7ddc9Seric { 437*83b7ddc9Seric extern char *pintvl(); 438*83b7ddc9Seric 439*83b7ddc9Seric if (!bitset(EF_TIMEOUT, e->e_flags)) 440*83b7ddc9Seric message(Arpa_Info, "Cannot send message for %s", 441*83b7ddc9Seric pintvl(TimeOut, FALSE)); 442*83b7ddc9Seric q->q_flags |= QBADADDR; 443*83b7ddc9Seric e->e_flags |= EF_TIMEOUT; 444*83b7ddc9Seric } 445*83b7ddc9Seric else 446*83b7ddc9Seric q->q_flags |= QQUEUEUP; 447*83b7ddc9Seric } 448*83b7ddc9Seric /* 44932d19d43Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 45032d19d43Seric ** 45132d19d43Seric ** This MUST be a macro, since after a vfork we are running 45232d19d43Seric ** two processes on the same stack!!! 45332d19d43Seric ** 45432d19d43Seric ** Parameters: 45532d19d43Seric ** none. 45632d19d43Seric ** 45732d19d43Seric ** Returns: 45832d19d43Seric ** From a macro??? You've got to be kidding! 45932d19d43Seric ** 46032d19d43Seric ** Side Effects: 46132d19d43Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 46232d19d43Seric ** pid of child in parent, zero in child. 46332d19d43Seric ** -1 on unrecoverable error. 46432d19d43Seric ** 46532d19d43Seric ** Notes: 46632d19d43Seric ** I'm awfully sorry this looks so awful. That's 46732d19d43Seric ** vfork for you..... 46832d19d43Seric */ 46932d19d43Seric 47032d19d43Seric # define NFORKTRIES 5 4714300ddf0Seric # ifdef VMUNIX 47232d19d43Seric # define XFORK vfork 4734300ddf0Seric # else VMUNIX 47432d19d43Seric # define XFORK fork 4754300ddf0Seric # endif VMUNIX 47632d19d43Seric 47732d19d43Seric # define DOFORK(fORKfN) \ 47832d19d43Seric {\ 47932d19d43Seric register int i;\ 48032d19d43Seric \ 48132d19d43Seric for (i = NFORKTRIES; i-- > 0; )\ 48232d19d43Seric {\ 48332d19d43Seric pid = fORKfN();\ 48432d19d43Seric if (pid >= 0)\ 48532d19d43Seric break;\ 4865e663df1Seric sleep(NFORKTRIES - i);\ 48732d19d43Seric }\ 48832d19d43Seric } 48932d19d43Seric /* 4902ed72599Seric ** DOFORK -- simple fork interface to DOFORK. 4912ed72599Seric ** 4922ed72599Seric ** Parameters: 4932ed72599Seric ** none. 4942ed72599Seric ** 4952ed72599Seric ** Returns: 4962ed72599Seric ** pid of child in parent. 4972ed72599Seric ** zero in child. 4982ed72599Seric ** -1 on error. 4992ed72599Seric ** 5002ed72599Seric ** Side Effects: 5012ed72599Seric ** returns twice, once in parent and once in child. 5022ed72599Seric */ 5032ed72599Seric 5042ed72599Seric dofork() 5052ed72599Seric { 5062ed72599Seric register int pid; 5072ed72599Seric 5082ed72599Seric DOFORK(fork); 5092ed72599Seric return (pid); 5102ed72599Seric } 5112ed72599Seric /* 5125dfc646bSeric ** SENDOFF -- send off call to mailer & collect response. 5135dfc646bSeric ** 5145dfc646bSeric ** Parameters: 515588cad61Seric ** e -- the envelope to mail. 5165dfc646bSeric ** m -- mailer descriptor. 5175dfc646bSeric ** pvp -- parameter vector to send to it. 5186259796dSeric ** ctladdr -- an address pointer controlling the 5196259796dSeric ** user/groupid etc. of the mailer. 520ddf9b743Seric ** crlf -- set if we want CRLF on the end of lines. 5215dfc646bSeric ** 5225dfc646bSeric ** Returns: 5235dfc646bSeric ** exit status of mailer. 5245dfc646bSeric ** 5255dfc646bSeric ** Side Effects: 5265dfc646bSeric ** none. 5275dfc646bSeric */ 5285dfc646bSeric 529ddf9b743Seric sendoff(e, m, pvp, ctladdr, crlf) 530588cad61Seric register ENVELOPE *e; 531588cad61Seric MAILER *m; 5325dfc646bSeric char **pvp; 5336259796dSeric ADDRESS *ctladdr; 534ddf9b743Seric bool crlf; 5355dfc646bSeric { 536c579ef51Seric auto FILE *mfile; 537c579ef51Seric auto FILE *rfile; 5385dfc646bSeric register int i; 539c579ef51Seric int pid; 540c579ef51Seric 541c579ef51Seric /* 542c579ef51Seric ** Create connection to mailer. 543c579ef51Seric */ 544c579ef51Seric 545c579ef51Seric pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 546c579ef51Seric if (pid < 0) 547c579ef51Seric return (-1); 548c579ef51Seric 549c579ef51Seric /* 550c579ef51Seric ** Format and send message. 551588cad61Seric ** We ignore broken pipes so that the mailer need not read 552588cad61Seric ** its input if it is not convenient to do so (e.g., on 553588cad61Seric ** some error). 554c579ef51Seric */ 555c579ef51Seric 556c579ef51Seric (void) signal(SIGPIPE, SIG_IGN); 557ddf9b743Seric putfromline(mfile, m, crlf); 558ddf9b743Seric (*e->e_puthdr)(mfile, m, e, crlf); 55951552439Seric fprintf(mfile, "\n"); 560ddf9b743Seric (*e->e_putbody)(mfile, m, FALSE, e, crlf); 561c579ef51Seric (void) fclose(mfile); 562c579ef51Seric 563c579ef51Seric i = endmailer(pid, pvp[0]); 564bc6e2962Seric 565bc6e2962Seric /* arrange a return receipt if requested */ 566588cad61Seric if (e->e_receiptto != NULL && bitset(M_LOCAL, m->m_flags)) 567bc6e2962Seric { 568588cad61Seric e->e_flags |= EF_SENDRECEIPT; 569bc6e2962Seric /* do we want to send back more info? */ 570bc6e2962Seric } 571bc6e2962Seric 572c579ef51Seric return (i); 573c579ef51Seric } 574c579ef51Seric /* 575c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 576c579ef51Seric ** 577c579ef51Seric ** We should never get fatal errors (e.g., segmentation 578c579ef51Seric ** violation), so we report those specially. For other 579c579ef51Seric ** errors, we choose a status message (into statmsg), 580c579ef51Seric ** and if it represents an error, we print it. 581c579ef51Seric ** 582c579ef51Seric ** Parameters: 583c579ef51Seric ** pid -- pid of mailer. 584c579ef51Seric ** name -- name of mailer (for error messages). 585c579ef51Seric ** 586c579ef51Seric ** Returns: 587c579ef51Seric ** exit code of mailer. 588c579ef51Seric ** 589c579ef51Seric ** Side Effects: 590c579ef51Seric ** none. 591c579ef51Seric */ 592c579ef51Seric 593c579ef51Seric endmailer(pid, name) 594c579ef51Seric int pid; 595c579ef51Seric char *name; 596c579ef51Seric { 597588cad61Seric int st; 598c579ef51Seric 59933db8731Seric /* in the IPC case there is nothing to wait for */ 60033db8731Seric if (pid == 0) 60133db8731Seric return (EX_OK); 60233db8731Seric 60333db8731Seric /* wait for the mailer process to die and collect status */ 604588cad61Seric st = waitfor(pid); 605588cad61Seric if (st == -1) 60678de67c1Seric { 607588cad61Seric syserr("endmailer %s: wait", name); 608588cad61Seric return (EX_SOFTWARE); 609c579ef51Seric } 61033db8731Seric 61133db8731Seric /* see if it died a horrid death */ 612c579ef51Seric if ((st & 0377) != 0) 613c579ef51Seric { 614588cad61Seric syserr("endmailer %s: stat %o", name, st); 615c579ef51Seric ExitStat = EX_UNAVAILABLE; 616588cad61Seric return (EX_UNAVAILABLE); 617c579ef51Seric } 61833db8731Seric 61933db8731Seric /* normal death -- return status */ 620588cad61Seric st = (st >> 8) & 0377; 621588cad61Seric return (st); 622c579ef51Seric } 623c579ef51Seric /* 624c579ef51Seric ** OPENMAILER -- open connection to mailer. 625c579ef51Seric ** 626c579ef51Seric ** Parameters: 627c579ef51Seric ** m -- mailer descriptor. 628c579ef51Seric ** pvp -- parameter vector to pass to mailer. 629c579ef51Seric ** ctladdr -- controlling address for user. 630c579ef51Seric ** clever -- create a full duplex connection. 631c579ef51Seric ** pmfile -- pointer to mfile (to mailer) connection. 632c579ef51Seric ** prfile -- pointer to rfile (from mailer) connection. 633c579ef51Seric ** 634c579ef51Seric ** Returns: 63533db8731Seric ** pid of mailer ( > 0 ). 636c579ef51Seric ** -1 on error. 63733db8731Seric ** zero on an IPC connection. 638c579ef51Seric ** 639c579ef51Seric ** Side Effects: 640c579ef51Seric ** creates a mailer in a subprocess. 641c579ef51Seric */ 642c579ef51Seric 643c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 644588cad61Seric MAILER *m; 645c579ef51Seric char **pvp; 646c579ef51Seric ADDRESS *ctladdr; 647c579ef51Seric bool clever; 648c579ef51Seric FILE **pmfile; 649c579ef51Seric FILE **prfile; 650c579ef51Seric { 6515dfc646bSeric int pid; 652f8952a83Seric int mpvect[2]; 653c579ef51Seric int rpvect[2]; 6545dfc646bSeric FILE *mfile; 655c579ef51Seric FILE *rfile; 6565dfc646bSeric extern FILE *fdopen(); 6575dfc646bSeric 6585dfc646bSeric # ifdef DEBUG 6596ef48975Seric if (tTd(11, 1)) 6605dfc646bSeric { 6618c57e552Seric printf("openmailer:"); 6625dfc646bSeric printav(pvp); 6635dfc646bSeric } 6645dfc646bSeric # endif DEBUG 66535490626Seric errno = 0; 6665dfc646bSeric 66733db8731Seric /* 66833db8731Seric ** Deal with the special case of mail handled through an IPC 66933db8731Seric ** connection. 67033db8731Seric ** In this case we don't actually fork. We must be 67133db8731Seric ** running SMTP for this to work. We will return a 67233db8731Seric ** zero pid to indicate that we are running IPC. 67333db8731Seric */ 67433db8731Seric 67533db8731Seric if (strcmp(m->m_mailer, "[IPC]") == 0) 67633db8731Seric { 677588cad61Seric #ifdef DAEMON 67833db8731Seric register int i; 6791277f9a8Seric register u_short port; 68033db8731Seric 68133db8731Seric if (!clever) 68233db8731Seric syserr("non-clever IPC"); 68393b6e3cfSeric if (pvp[2] != NULL) 6841277f9a8Seric port = atoi(pvp[2]); 68593b6e3cfSeric else 6861277f9a8Seric port = 0; 6871277f9a8Seric i = makeconnection(pvp[1], port, pmfile, prfile); 68833db8731Seric if (i != EX_OK) 689ed854c7bSeric { 690ed854c7bSeric ExitStat = i; 69133db8731Seric return (-1); 692ed854c7bSeric } 69333db8731Seric else 69433db8731Seric return (0); 695588cad61Seric #else DAEMON 696588cad61Seric syserr("openmailer: no IPC"); 697588cad61Seric return (-1); 69833db8731Seric #endif DAEMON 699588cad61Seric } 70033db8731Seric 7016328bdf7Seric /* create a pipe to shove the mail through */ 702f8952a83Seric if (pipe(mpvect) < 0) 70325a99e2eSeric { 704588cad61Seric syserr("openmailer: pipe (to mailer)"); 70525a99e2eSeric return (-1); 70625a99e2eSeric } 707c579ef51Seric 7082c7e1b8dSeric #ifdef SMTP 709c579ef51Seric /* if this mailer speaks smtp, create a return pipe */ 710c579ef51Seric if (clever && pipe(rpvect) < 0) 711c579ef51Seric { 712588cad61Seric syserr("openmailer: pipe (from mailer)"); 713c579ef51Seric (void) close(mpvect[0]); 714c579ef51Seric (void) close(mpvect[1]); 715c579ef51Seric return (-1); 716c579ef51Seric } 7172c7e1b8dSeric #endif SMTP 718c579ef51Seric 71933db8731Seric /* 72033db8731Seric ** Actually fork the mailer process. 72133db8731Seric ** DOFORK is clever about retrying. 72233db8731Seric */ 72333db8731Seric 7249a6a5f55Seric if (CurEnv->e_xfp != NULL) 7259a6a5f55Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 726588cad61Seric (void) fflush(stdout); 72732d19d43Seric DOFORK(XFORK); 728f129ec7dSeric /* pid is set by DOFORK */ 72925a99e2eSeric if (pid < 0) 73025a99e2eSeric { 73133db8731Seric /* failure */ 732588cad61Seric syserr("openmailer: cannot fork"); 733f8952a83Seric (void) close(mpvect[0]); 734f8952a83Seric (void) close(mpvect[1]); 735588cad61Seric #ifdef SMTP 736c579ef51Seric if (clever) 737c579ef51Seric { 738c579ef51Seric (void) close(rpvect[0]); 739c579ef51Seric (void) close(rpvect[1]); 740c579ef51Seric } 741588cad61Seric #endif SMTP 74225a99e2eSeric return (-1); 74325a99e2eSeric } 74425a99e2eSeric else if (pid == 0) 74525a99e2eSeric { 74625a99e2eSeric /* child -- set up input & exec mailer */ 74703ab8e55Seric /* make diagnostic output be standard output */ 7488f0e7860Seric (void) signal(SIGINT, SIG_IGN); 7498f0e7860Seric (void) signal(SIGHUP, SIG_IGN); 7500984da9fSeric (void) signal(SIGTERM, SIG_DFL); 751f8952a83Seric 752f8952a83Seric /* arrange to filter standard & diag output of command */ 753c579ef51Seric if (clever) 754c579ef51Seric { 755c579ef51Seric (void) close(rpvect[0]); 756c579ef51Seric (void) close(1); 757c579ef51Seric (void) dup(rpvect[1]); 758c579ef51Seric (void) close(rpvect[1]); 759c579ef51Seric } 760276723a8Seric else if (OpMode == MD_SMTP || HoldErrs) 761f8952a83Seric { 762588cad61Seric /* put mailer output in transcript */ 763f8952a83Seric (void) close(1); 7649a6a5f55Seric (void) dup(fileno(CurEnv->e_xfp)); 765f8952a83Seric } 766db8841e9Seric (void) close(2); 767db8841e9Seric (void) dup(1); 768f8952a83Seric 769f8952a83Seric /* arrange to get standard input */ 770f8952a83Seric (void) close(mpvect[1]); 771db8841e9Seric (void) close(0); 772f8952a83Seric if (dup(mpvect[0]) < 0) 77325a99e2eSeric { 77425a99e2eSeric syserr("Cannot dup to zero!"); 775a590b978Seric _exit(EX_OSERR); 77625a99e2eSeric } 777f8952a83Seric (void) close(mpvect[0]); 7782a6e0786Seric if (!bitset(M_RESTR, m->m_flags)) 7790984da9fSeric { 780e36b99e2Seric if (ctladdr->q_uid == 0) 781e36b99e2Seric { 782e36b99e2Seric (void) setgid(DefGid); 783e36b99e2Seric (void) setuid(DefUid); 784e36b99e2Seric } 785e36b99e2Seric else 78669f29479Seric { 787e36b99e2Seric (void) setgid(ctladdr->q_gid); 788e36b99e2Seric (void) setuid(ctladdr->q_uid); 78969f29479Seric } 7900984da9fSeric } 791588cad61Seric 792e374fd72Seric /* 793e374fd72Seric ** We have to be careful with vfork - we can't mung up the 794e374fd72Seric ** memory but we don't want the mailer to inherit any extra 795e374fd72Seric ** open files. Chances are the mailer won't 796e374fd72Seric ** care about an extra file, but then again you never know. 797e374fd72Seric ** Actually, we would like to close(fileno(pwf)), but it's 798e374fd72Seric ** declared static so we can't. But if we fclose(pwf), which 799e374fd72Seric ** is what endpwent does, it closes it in the parent too and 800e374fd72Seric ** the next getpwnam will be slower. If you have a weird 801e374fd72Seric ** mailer that chokes on the extra file you should do the 8024300ddf0Seric ** endpwent(). -MRH 803e374fd72Seric ** 804e374fd72Seric ** Similar comments apply to log. However, openlog is 805e374fd72Seric ** clever enough to set the FIOCLEX mode on the file, 806e374fd72Seric ** so it will be closed automatically on the exec. 807e374fd72Seric */ 808e374fd72Seric 809588cad61Seric closeall(); 81033db8731Seric 81133db8731Seric /* try to execute the mailer */ 81225a99e2eSeric execv(m->m_mailer, pvp); 81333db8731Seric 81425a99e2eSeric /* syserr fails because log is closed */ 81525a99e2eSeric /* syserr("Cannot exec %s", m->m_mailer); */ 81632d19d43Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 817db8841e9Seric (void) fflush(stdout); 818a590b978Seric _exit(EX_UNAVAILABLE); 81925a99e2eSeric } 82025a99e2eSeric 821f8952a83Seric /* 822c579ef51Seric ** Set up return value. 823f8952a83Seric */ 824f8952a83Seric 825f8952a83Seric (void) close(mpvect[0]); 826f8952a83Seric mfile = fdopen(mpvect[1], "w"); 827c579ef51Seric if (clever) 82825a99e2eSeric { 829c579ef51Seric (void) close(rpvect[1]); 830c579ef51Seric rfile = fdopen(rpvect[0], "r"); 83125a99e2eSeric } 832c579ef51Seric 833c579ef51Seric *pmfile = mfile; 834c579ef51Seric *prfile = rfile; 835c579ef51Seric 836c579ef51Seric return (pid); 83725a99e2eSeric } 83825a99e2eSeric /* 83925a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 84025a99e2eSeric ** 84125a99e2eSeric ** Parameters: 84225a99e2eSeric ** stat -- the status code from the mailer (high byte 84325a99e2eSeric ** only; core dumps must have been taken care of 84425a99e2eSeric ** already). 84525a99e2eSeric ** m -- the mailer descriptor for this mailer. 84625a99e2eSeric ** 84725a99e2eSeric ** Returns: 848db8841e9Seric ** none. 84925a99e2eSeric ** 85025a99e2eSeric ** Side Effects: 851c1f9df2cSeric ** Errors may be incremented. 85225a99e2eSeric ** ExitStat may be set. 85325a99e2eSeric */ 85425a99e2eSeric 855588cad61Seric /*ARGSUSED*/ 856588cad61Seric giveresponse(stat, m) 85725a99e2eSeric int stat; 858588cad61Seric register MAILER *m; 85925a99e2eSeric { 86025a99e2eSeric register char *statmsg; 86125a99e2eSeric extern char *SysExMsg[]; 86225a99e2eSeric register int i; 86325a99e2eSeric extern int N_SysEx; 86429dd97a3Seric char buf[30]; 86525a99e2eSeric 86613bbc08cSeric /* 86713bbc08cSeric ** Compute status message from code. 86813bbc08cSeric */ 86913bbc08cSeric 87025a99e2eSeric i = stat - EX__BASE; 871588cad61Seric if (stat == 0) 872588cad61Seric statmsg = "250 Sent"; 873588cad61Seric else if (i < 0 || i > N_SysEx) 874588cad61Seric { 875588cad61Seric (void) sprintf(buf, "554 unknown mailer error %d", stat); 876588cad61Seric stat = EX_UNAVAILABLE; 877588cad61Seric statmsg = buf; 878588cad61Seric } 87925a99e2eSeric else 88025a99e2eSeric statmsg = SysExMsg[i]; 881588cad61Seric 882588cad61Seric /* 883588cad61Seric ** Print the message as appropriate 884588cad61Seric */ 885588cad61Seric 88625a99e2eSeric if (stat == 0) 8875826d9d3Seric message(Arpa_Info, &statmsg[4]); 888c77d1c25Seric else if (stat == EX_TEMPFAIL) 889eb238f8cSeric message(Arpa_Info, "deferred"); 89025a99e2eSeric else 89125a99e2eSeric { 892c1f9df2cSeric Errors++; 8935826d9d3Seric usrerr(statmsg); 89425a99e2eSeric } 89525a99e2eSeric 89625a99e2eSeric /* 89725a99e2eSeric ** Final cleanup. 89825a99e2eSeric ** Log a record of the transaction. Compute the new 89925a99e2eSeric ** ExitStat -- if we already had an error, stick with 90025a99e2eSeric ** that. 90125a99e2eSeric */ 90225a99e2eSeric 90361f5a1d4Seric if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2)) 904eb238f8cSeric logdelivery(&statmsg[4]); 905eb238f8cSeric 906eb238f8cSeric if (stat != EX_TEMPFAIL) 907eb238f8cSeric setstat(stat); 908eb238f8cSeric } 909eb238f8cSeric /* 910eb238f8cSeric ** LOGDELIVERY -- log the delivery in the system log 911eb238f8cSeric ** 912eb238f8cSeric ** Parameters: 913eb238f8cSeric ** stat -- the message to print for the status 914eb238f8cSeric ** 915eb238f8cSeric ** Returns: 916eb238f8cSeric ** none 917eb238f8cSeric ** 918eb238f8cSeric ** Side Effects: 919eb238f8cSeric ** none 920eb238f8cSeric */ 921eb238f8cSeric 922eb238f8cSeric logdelivery(stat) 923eb238f8cSeric char *stat; 9245cf56be3Seric { 9255cf56be3Seric extern char *pintvl(); 9265cf56be3Seric 927eb238f8cSeric # ifdef LOG 9285cf56be3Seric syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id, 929eb238f8cSeric CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), stat); 93025a99e2eSeric # endif LOG 93125a99e2eSeric } 93225a99e2eSeric /* 93351552439Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 93425a99e2eSeric ** 93551552439Seric ** This can be made an arbitrary message separator by changing $l 93651552439Seric ** 93751552439Seric ** One of the ugliest hacks seen by human eyes is 93851552439Seric ** contained herein: UUCP wants those stupid 939588cad61Seric ** "emote from <host>" lines. Why oh why does a 94051552439Seric ** well-meaning programmer such as myself have to 94151552439Seric ** deal with this kind of antique garbage???? 94225a99e2eSeric ** 94325a99e2eSeric ** Parameters: 94451552439Seric ** fp -- the file to output to. 94551552439Seric ** m -- the mailer describing this entry. 946ddf9b743Seric ** crlf -- set if we want a CRLF at the end of the line. 94725a99e2eSeric ** 94825a99e2eSeric ** Returns: 94951552439Seric ** none 95025a99e2eSeric ** 95125a99e2eSeric ** Side Effects: 95251552439Seric ** outputs some text to fp. 95325a99e2eSeric */ 95425a99e2eSeric 955ddf9b743Seric putfromline(fp, m, crlf) 95651552439Seric register FILE *fp; 95751552439Seric register MAILER *m; 95825a99e2eSeric { 95951552439Seric char buf[MAXLINE]; 96025a99e2eSeric 96151552439Seric if (bitset(M_NHDR, m->m_flags)) 96251552439Seric return; 96313bbc08cSeric 9642c7e1b8dSeric # ifdef UGLYUUCP 965a49f24c0Seric if (bitset(M_UGLYUUCP, m->m_flags)) 96674b6e67bSeric { 96774b6e67bSeric extern char *macvalue(); 9688c57e552Seric char *sys = macvalue('g', CurEnv); 96974b6e67bSeric char *bang = index(sys, '!'); 97074b6e67bSeric 97174b6e67bSeric if (bang == NULL) 97274b6e67bSeric syserr("No ! in UUCP! (%s)", sys); 97374b6e67bSeric else 974588cad61Seric { 97574b6e67bSeric *bang = '\0'; 976518ad6b6Seric expand("From $f $d remote from $g\n", buf, 97751552439Seric &buf[sizeof buf - 1], CurEnv); 97874b6e67bSeric *bang = '!'; 97974b6e67bSeric } 980588cad61Seric } 981a36e30c9Seric else 9822c7e1b8dSeric # endif UGLYUUCP 98351552439Seric expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv); 984ddf9b743Seric putline(buf, fp, crlf, bitset(M_FULLSMTP, m->m_flags)); 985bc6e2962Seric } 986bc6e2962Seric /* 98751552439Seric ** PUTBODY -- put the body of a message. 98851552439Seric ** 98951552439Seric ** Parameters: 99051552439Seric ** fp -- file to output onto. 99151552439Seric ** m -- a mailer descriptor. 99251552439Seric ** xdot -- if set, use SMTP hidden dot algorithm. 9939a6a5f55Seric ** e -- the envelope to put out. 99451552439Seric ** 99551552439Seric ** Returns: 99651552439Seric ** none. 99751552439Seric ** 99851552439Seric ** Side Effects: 99951552439Seric ** The message is written onto fp. 100051552439Seric */ 100151552439Seric 1002ddf9b743Seric putbody(fp, m, xdot, e, crlf) 100351552439Seric FILE *fp; 1004588cad61Seric MAILER *m; 100551552439Seric bool xdot; 10069a6a5f55Seric register ENVELOPE *e; 100751552439Seric { 100851552439Seric char buf[MAXLINE + 1]; 1009d90b8d00Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 101051552439Seric 101151552439Seric /* 101251552439Seric ** Output the body of the message 101351552439Seric */ 101451552439Seric 10159a6a5f55Seric if (e->e_dfp == NULL) 101651552439Seric { 10179a6a5f55Seric if (e->e_df != NULL) 10189a6a5f55Seric { 10199a6a5f55Seric e->e_dfp = fopen(e->e_df, "r"); 10209a6a5f55Seric if (e->e_dfp == NULL) 10219a6a5f55Seric syserr("Cannot open %s", e->e_df); 10229a6a5f55Seric } 10239a6a5f55Seric else 1024ddf9b743Seric putline("<<< No Message Collected >>>", fp, crlf, fullsmtp); 10259a6a5f55Seric } 10269a6a5f55Seric if (e->e_dfp != NULL) 10279a6a5f55Seric { 10289a6a5f55Seric rewind(e->e_dfp); 102951552439Seric buf[0] = '.'; 10309a6a5f55Seric while (!ferror(fp) && 10319a6a5f55Seric fgets(&buf[1], sizeof buf - 1, e->e_dfp) != NULL) 10329a6a5f55Seric { 1033ddf9b743Seric putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, crlf, fullsmtp); 10349a6a5f55Seric } 103551552439Seric 10369a6a5f55Seric if (ferror(e->e_dfp)) 103751552439Seric { 103851552439Seric syserr("putbody: read error"); 103951552439Seric ExitStat = EX_IOERR; 104051552439Seric } 104151552439Seric } 104251552439Seric 104351552439Seric (void) fflush(fp); 104451552439Seric if (ferror(fp) && errno != EPIPE) 104551552439Seric { 104651552439Seric syserr("putbody: write error"); 104751552439Seric ExitStat = EX_IOERR; 104851552439Seric } 104951552439Seric errno = 0; 105025a99e2eSeric } 105125a99e2eSeric /* 105225a99e2eSeric ** MAILFILE -- Send a message to a file. 105325a99e2eSeric ** 1054f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 1055f129ec7dSeric ** bits, sendmail will try to become the owner of that file 1056f129ec7dSeric ** rather than the real user. Obviously, this only works if 1057f129ec7dSeric ** sendmail runs as root. 1058f129ec7dSeric ** 1059588cad61Seric ** This could be done as a subordinate mailer, except that it 1060588cad61Seric ** is used implicitly to save messages in ~/dead.letter. We 1061588cad61Seric ** view this as being sufficiently important as to include it 1062588cad61Seric ** here. For example, if the system is dying, we shouldn't have 1063588cad61Seric ** to create another process plus some pipes to save the message. 1064588cad61Seric ** 106525a99e2eSeric ** Parameters: 106625a99e2eSeric ** filename -- the name of the file to send to. 10676259796dSeric ** ctladdr -- the controlling address header -- includes 10686259796dSeric ** the userid/groupid to be when sending. 106925a99e2eSeric ** 107025a99e2eSeric ** Returns: 107125a99e2eSeric ** The exit code associated with the operation. 107225a99e2eSeric ** 107325a99e2eSeric ** Side Effects: 107425a99e2eSeric ** none. 107525a99e2eSeric */ 107625a99e2eSeric 10776259796dSeric mailfile(filename, ctladdr) 107825a99e2eSeric char *filename; 10796259796dSeric ADDRESS *ctladdr; 108025a99e2eSeric { 108125a99e2eSeric register FILE *f; 108232d19d43Seric register int pid; 108325a99e2eSeric 108432d19d43Seric /* 108532d19d43Seric ** Fork so we can change permissions here. 108632d19d43Seric ** Note that we MUST use fork, not vfork, because of 108732d19d43Seric ** the complications of calling subroutines, etc. 108832d19d43Seric */ 108932d19d43Seric 109032d19d43Seric DOFORK(fork); 109132d19d43Seric 109232d19d43Seric if (pid < 0) 109332d19d43Seric return (EX_OSERR); 109432d19d43Seric else if (pid == 0) 109532d19d43Seric { 109632d19d43Seric /* child -- actually write to file */ 1097f129ec7dSeric struct stat stb; 1098f129ec7dSeric 10990984da9fSeric (void) signal(SIGINT, SIG_DFL); 11000984da9fSeric (void) signal(SIGHUP, SIG_DFL); 11010984da9fSeric (void) signal(SIGTERM, SIG_DFL); 1102f129ec7dSeric umask(OldUmask); 1103f129ec7dSeric if (stat(filename, &stb) < 0) 1104e6e1265fSeric stb.st_mode = 0666; 1105f129ec7dSeric if (bitset(0111, stb.st_mode)) 1106f129ec7dSeric exit(EX_CANTCREAT); 110703827b5fSeric if (ctladdr == NULL) 11087a941e7aSeric ctladdr = &CurEnv->e_from; 1109f129ec7dSeric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 1110e36b99e2Seric { 1111e36b99e2Seric if (ctladdr->q_uid == 0) 1112e36b99e2Seric (void) setgid(DefGid); 1113e36b99e2Seric else 11146259796dSeric (void) setgid(ctladdr->q_gid); 1115e36b99e2Seric } 1116f129ec7dSeric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 1117e36b99e2Seric { 1118e36b99e2Seric if (ctladdr->q_uid == 0) 1119e36b99e2Seric (void) setuid(DefUid); 1120e36b99e2Seric else 11216259796dSeric (void) setuid(ctladdr->q_uid); 1122e36b99e2Seric } 112327628d59Seric f = dfopen(filename, "a"); 112425a99e2eSeric if (f == NULL) 112532d19d43Seric exit(EX_CANTCREAT); 112625a99e2eSeric 1127ddf9b743Seric putfromline(f, ProgMailer, FALSE); 1128ddf9b743Seric (*CurEnv->e_puthdr)(f, ProgMailer, CurEnv, FALSE); 1129d90b8d00Seric fputs("\n", f); 1130ddf9b743Seric (*CurEnv->e_putbody)(f, ProgMailer, FALSE, CurEnv, FALSE); 113125a99e2eSeric fputs("\n", f); 1132db8841e9Seric (void) fclose(f); 113332d19d43Seric (void) fflush(stdout); 1134e36b99e2Seric 113527628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 1136c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 113732d19d43Seric exit(EX_OK); 113813bbc08cSeric /*NOTREACHED*/ 113932d19d43Seric } 114032d19d43Seric else 114132d19d43Seric { 114232d19d43Seric /* parent -- wait for exit status */ 1143588cad61Seric int st; 114432d19d43Seric 1145588cad61Seric st = waitfor(pid); 1146588cad61Seric if ((st & 0377) != 0) 1147588cad61Seric return (EX_UNAVAILABLE); 1148588cad61Seric else 1149588cad61Seric return ((st >> 8) & 0377); 115032d19d43Seric } 115125a99e2eSeric } 1152ea4dc939Seric /* 1153ea4dc939Seric ** SENDALL -- actually send all the messages. 1154ea4dc939Seric ** 1155ea4dc939Seric ** Parameters: 11560c52a0b3Seric ** e -- the envelope to send. 1157276723a8Seric ** mode -- the delivery mode to use. 1158ea4dc939Seric ** 1159ea4dc939Seric ** Returns: 1160ea4dc939Seric ** none. 1161ea4dc939Seric ** 1162ea4dc939Seric ** Side Effects: 1163ea4dc939Seric ** Scans the send lists and sends everything it finds. 11640c52a0b3Seric ** Delivers any appropriate error messages. 1165276723a8Seric ** If we are running in a non-interactive mode, takes the 1166276723a8Seric ** appropriate action. 1167ea4dc939Seric */ 1168ea4dc939Seric 1169276723a8Seric sendall(e, mode) 11700c52a0b3Seric ENVELOPE *e; 1171276723a8Seric char mode; 1172ea4dc939Seric { 1173e77e673fSeric register ADDRESS *q; 117414a8ed7aSeric bool oldverbose; 1175276723a8Seric int pid; 1176ea4dc939Seric 1177772e6e50Seric #ifdef DEBUG 1178df864a8fSeric if (tTd(13, 1)) 1179772e6e50Seric { 1180276723a8Seric printf("\nSENDALL: mode %c, sendqueue:\n", mode); 11810c52a0b3Seric printaddr(e->e_sendqueue, TRUE); 1182772e6e50Seric } 1183772e6e50Seric #endif DEBUG 1184ea4dc939Seric 11850c52a0b3Seric /* 1186276723a8Seric ** Do any preprocessing necessary for the mode we are running. 1187588cad61Seric ** Check to make sure the hop count is reasonable. 1188588cad61Seric ** Delete sends to the sender in mailing lists. 1189276723a8Seric */ 1190276723a8Seric 1191588cad61Seric CurEnv = e; 1192276723a8Seric 1193588cad61Seric if (e->e_hopcount > MAXHOP) 1194276723a8Seric { 1195588cad61Seric syserr("sendall: too many hops (%d max)", MAXHOP); 1196588cad61Seric return; 1197588cad61Seric } 1198588cad61Seric 1199588cad61Seric if (!MeToo) 1200276723a8Seric { 1201588cad61Seric e->e_from.q_flags |= QDONTSEND; 1202588cad61Seric recipient(&e->e_from, &e->e_sendqueue); 1203276723a8Seric } 1204588cad61Seric 1205588cad61Seric # ifdef QUEUE 1206b254bcb6Seric if ((mode == SM_QUEUE || mode == SM_FORK || 1207b254bcb6Seric (mode != SM_VERIFY && SuperSafe)) && 1208b254bcb6Seric !bitset(EF_INQUEUE, e->e_flags)) 1209588cad61Seric queueup(e, TRUE, mode == SM_QUEUE); 1210276723a8Seric #endif QUEUE 1211276723a8Seric 1212276723a8Seric oldverbose = Verbose; 1213276723a8Seric switch (mode) 1214276723a8Seric { 1215276723a8Seric case SM_VERIFY: 1216276723a8Seric Verbose = TRUE; 1217276723a8Seric break; 1218276723a8Seric 1219276723a8Seric case SM_QUEUE: 1220b254bcb6Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 1221276723a8Seric return; 1222276723a8Seric 1223276723a8Seric case SM_FORK: 12249a6a5f55Seric if (e->e_xfp != NULL) 12259a6a5f55Seric (void) fflush(e->e_xfp); 1226276723a8Seric pid = fork(); 1227276723a8Seric if (pid < 0) 1228276723a8Seric { 1229276723a8Seric mode = SM_DELIVER; 1230276723a8Seric break; 1231276723a8Seric } 1232276723a8Seric else if (pid > 0) 1233a6fce3d8Seric { 1234a6fce3d8Seric /* be sure we leave the temp files to our child */ 1235b254bcb6Seric e->e_id = e->e_df = NULL; 1236276723a8Seric return; 1237a6fce3d8Seric } 1238276723a8Seric 1239276723a8Seric /* double fork to avoid zombies */ 1240276723a8Seric if (fork() > 0) 1241276723a8Seric exit(EX_OK); 1242276723a8Seric 1243a6fce3d8Seric /* be sure we are immune from the terminal */ 1244e2e30df7Seric disconnect(); 1245a6fce3d8Seric 1246276723a8Seric break; 1247276723a8Seric } 1248276723a8Seric 1249276723a8Seric /* 12500c52a0b3Seric ** Run through the list and send everything. 12510c52a0b3Seric */ 12520c52a0b3Seric 12530c52a0b3Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1254ea4dc939Seric { 1255276723a8Seric if (mode == SM_VERIFY) 1256ea4dc939Seric { 1257a6fce3d8Seric e->e_to = q->q_paddr; 1258e77e673fSeric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1259ea4dc939Seric message(Arpa_Info, "deliverable"); 1260ea4dc939Seric } 1261ea4dc939Seric else 1262588cad61Seric (void) deliver(e, q); 1263ea4dc939Seric } 126414a8ed7aSeric Verbose = oldverbose; 12650c52a0b3Seric 12660c52a0b3Seric /* 12670c52a0b3Seric ** Now run through and check for errors. 12680c52a0b3Seric */ 12690c52a0b3Seric 1270276723a8Seric if (mode == SM_VERIFY) 12710c52a0b3Seric return; 12720c52a0b3Seric 12730c52a0b3Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 12740c52a0b3Seric { 12750c52a0b3Seric register ADDRESS *qq; 12760c52a0b3Seric 1277df864a8fSeric # ifdef DEBUG 1278df864a8fSeric if (tTd(13, 3)) 1279df864a8fSeric { 1280df864a8fSeric printf("Checking "); 1281df864a8fSeric printaddr(q, FALSE); 1282df864a8fSeric } 1283df864a8fSeric # endif DEBUG 1284df864a8fSeric 1285b254bcb6Seric /* only send errors if the message failed */ 1286b254bcb6Seric if (!bitset(QBADADDR, q->q_flags)) 1287b254bcb6Seric continue; 12880c52a0b3Seric 12890c52a0b3Seric /* we have an address that failed -- find the parent */ 12900c52a0b3Seric for (qq = q; qq != NULL; qq = qq->q_alias) 12910c52a0b3Seric { 12920c52a0b3Seric char obuf[MAXNAME + 6]; 12930c52a0b3Seric extern char *aliaslookup(); 12940c52a0b3Seric 12950c52a0b3Seric /* we can only have owners for local addresses */ 12960c52a0b3Seric if (!bitset(M_LOCAL, qq->q_mailer->m_flags)) 12970c52a0b3Seric continue; 12980c52a0b3Seric 12990c52a0b3Seric /* see if the owner list exists */ 13000c52a0b3Seric (void) strcpy(obuf, "owner-"); 1301cec031e3Seric if (strncmp(qq->q_user, "owner-", 6) == 0) 1302cec031e3Seric (void) strcat(obuf, "owner"); 1303cec031e3Seric else 13040c52a0b3Seric (void) strcat(obuf, qq->q_user); 13050c52a0b3Seric if (aliaslookup(obuf) == NULL) 13060c52a0b3Seric continue; 13070c52a0b3Seric 1308df864a8fSeric # ifdef DEBUG 1309df864a8fSeric if (tTd(13, 4)) 1310df864a8fSeric printf("Errors to %s\n", obuf); 1311df864a8fSeric # endif DEBUG 1312df864a8fSeric 13130c52a0b3Seric /* owner list exists -- add it to the error queue */ 1314e3e4ed86Seric sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue); 1315588cad61Seric ErrorMode == EM_MAIL; 13160c52a0b3Seric break; 13170c52a0b3Seric } 13180c52a0b3Seric 13190c52a0b3Seric /* if we did not find an owner, send to the sender */ 13207455aa0bSeric if (qq == NULL && bitset(QBADADDR, q->q_flags)) 1321e3e4ed86Seric sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue); 13220c52a0b3Seric } 1323276723a8Seric 1324276723a8Seric if (mode == SM_FORK) 1325276723a8Seric finis(); 13260c52a0b3Seric } 1327