125a99e2eSeric # include <signal.h> 254aa2b0fSeric # include <errno.h> 3b20b3270Seric # include "sendmail.h" 4c77d1c25Seric # include <sys/stat.h> 525a99e2eSeric 6*63780dbdSeric SCCSID(@(#)deliver.c 3.135 11/28/82); 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 */ 318*63780dbdSeric 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 351*63780dbdSeric if (rcode == EX_OK) 352*63780dbdSeric { 353588cad61Seric /* send the recipient list */ 354*63780dbdSeric tobuf[0] = '\0'; 355588cad61Seric for (to = tochain; to != NULL; to = to->q_tchain) 356588cad61Seric { 357588cad61Seric int i; 358588cad61Seric 359*63780dbdSeric e->e_to = to->q_paddr; 360588cad61Seric i = smtprcpt(to); 361588cad61Seric if (i != EX_OK) 362588cad61Seric { 363588cad61Seric if (i == EX_TEMPFAIL) 364588cad61Seric to->q_flags |= QQUEUEUP; 365588cad61Seric else 366588cad61Seric to->q_flags |= QBADADDR; 367*63780dbdSeric giveresponse(i, m); 368*63780dbdSeric } 369*63780dbdSeric else 370*63780dbdSeric { 371*63780dbdSeric strcat(tobuf, ","); 372*63780dbdSeric strcat(tobuf, to->q_paddr); 373588cad61Seric } 374588cad61Seric } 375588cad61Seric 376*63780dbdSeric /* now send the data */ 377*63780dbdSeric if (tobuf[0] == '\0') 378*63780dbdSeric e->e_to = NULL; 379*63780dbdSeric else 380*63780dbdSeric { 381*63780dbdSeric e->e_to = tobuf + 1; 382588cad61Seric rcode = smtpfinish(m, e); 383*63780dbdSeric } 384*63780dbdSeric 385*63780dbdSeric /* now close the connection */ 386*63780dbdSeric smtpquit(pv[0]); 387*63780dbdSeric } 388c579ef51Seric } 389c579ef51Seric else 3902c7e1b8dSeric # endif SMTP 391588cad61Seric rcode = sendoff(e, m, pv, ctladdr); 3925dfc646bSeric 393c77d1c25Seric /* 394*63780dbdSeric ** Do final status disposal. 395*63780dbdSeric ** 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 400*63780dbdSeric if (tobuf[0] != '\0') 401*63780dbdSeric giveresponse(rcode, m); 402*63780dbdSeric if (rcode != EX_OK) 403c77d1c25Seric { 404772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 405*63780dbdSeric { 406*63780dbdSeric if (rcode == EX_TEMPFAIL) 407c77d1c25Seric to->q_flags |= QQUEUEUP; 408*63780dbdSeric else 409*63780dbdSeric to->q_flags |= QBADADDR; 410*63780dbdSeric } 411c77d1c25Seric } 412c77d1c25Seric 41335490626Seric errno = 0; 414588cad61Seric define('g', (char *) NULL, e); 4155826d9d3Seric return (rcode); 41625a99e2eSeric } 4175dfc646bSeric /* 41832d19d43Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 41932d19d43Seric ** 42032d19d43Seric ** This MUST be a macro, since after a vfork we are running 42132d19d43Seric ** two processes on the same stack!!! 42232d19d43Seric ** 42332d19d43Seric ** Parameters: 42432d19d43Seric ** none. 42532d19d43Seric ** 42632d19d43Seric ** Returns: 42732d19d43Seric ** From a macro??? You've got to be kidding! 42832d19d43Seric ** 42932d19d43Seric ** Side Effects: 43032d19d43Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 43132d19d43Seric ** pid of child in parent, zero in child. 43232d19d43Seric ** -1 on unrecoverable error. 43332d19d43Seric ** 43432d19d43Seric ** Notes: 43532d19d43Seric ** I'm awfully sorry this looks so awful. That's 43632d19d43Seric ** vfork for you..... 43732d19d43Seric */ 43832d19d43Seric 43932d19d43Seric # define NFORKTRIES 5 4404300ddf0Seric # ifdef VMUNIX 44132d19d43Seric # define XFORK vfork 4424300ddf0Seric # else VMUNIX 44332d19d43Seric # define XFORK fork 4444300ddf0Seric # endif VMUNIX 44532d19d43Seric 44632d19d43Seric # define DOFORK(fORKfN) \ 44732d19d43Seric {\ 44832d19d43Seric register int i;\ 44932d19d43Seric \ 45032d19d43Seric for (i = NFORKTRIES; i-- > 0; )\ 45132d19d43Seric {\ 45232d19d43Seric pid = fORKfN();\ 45332d19d43Seric if (pid >= 0)\ 45432d19d43Seric break;\ 4555e663df1Seric sleep(NFORKTRIES - i);\ 45632d19d43Seric }\ 45732d19d43Seric } 45832d19d43Seric /* 4592ed72599Seric ** DOFORK -- simple fork interface to DOFORK. 4602ed72599Seric ** 4612ed72599Seric ** Parameters: 4622ed72599Seric ** none. 4632ed72599Seric ** 4642ed72599Seric ** Returns: 4652ed72599Seric ** pid of child in parent. 4662ed72599Seric ** zero in child. 4672ed72599Seric ** -1 on error. 4682ed72599Seric ** 4692ed72599Seric ** Side Effects: 4702ed72599Seric ** returns twice, once in parent and once in child. 4712ed72599Seric */ 4722ed72599Seric 4732ed72599Seric dofork() 4742ed72599Seric { 4752ed72599Seric register int pid; 4762ed72599Seric 4772ed72599Seric DOFORK(fork); 4782ed72599Seric return (pid); 4792ed72599Seric } 4802ed72599Seric /* 4815dfc646bSeric ** SENDOFF -- send off call to mailer & collect response. 4825dfc646bSeric ** 4835dfc646bSeric ** Parameters: 484588cad61Seric ** e -- the envelope to mail. 4855dfc646bSeric ** m -- mailer descriptor. 4865dfc646bSeric ** pvp -- parameter vector to send to it. 4876259796dSeric ** ctladdr -- an address pointer controlling the 4886259796dSeric ** user/groupid etc. of the mailer. 4895dfc646bSeric ** 4905dfc646bSeric ** Returns: 4915dfc646bSeric ** exit status of mailer. 4925dfc646bSeric ** 4935dfc646bSeric ** Side Effects: 4945dfc646bSeric ** none. 4955dfc646bSeric */ 4965dfc646bSeric 497588cad61Seric sendoff(e, m, pvp, ctladdr) 498588cad61Seric register ENVELOPE *e; 499588cad61Seric MAILER *m; 5005dfc646bSeric char **pvp; 5016259796dSeric ADDRESS *ctladdr; 5025dfc646bSeric { 503c579ef51Seric auto FILE *mfile; 504c579ef51Seric auto FILE *rfile; 5055dfc646bSeric register int i; 506c579ef51Seric int pid; 507c579ef51Seric 508c579ef51Seric /* 509c579ef51Seric ** Create connection to mailer. 510c579ef51Seric */ 511c579ef51Seric 512c579ef51Seric pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 513c579ef51Seric if (pid < 0) 514c579ef51Seric return (-1); 515c579ef51Seric 516c579ef51Seric /* 517c579ef51Seric ** Format and send message. 518588cad61Seric ** We ignore broken pipes so that the mailer need not read 519588cad61Seric ** its input if it is not convenient to do so (e.g., on 520588cad61Seric ** some error). 521c579ef51Seric */ 522c579ef51Seric 523c579ef51Seric (void) signal(SIGPIPE, SIG_IGN); 52451552439Seric putfromline(mfile, m); 525588cad61Seric (*e->e_puthdr)(mfile, m, e); 52651552439Seric fprintf(mfile, "\n"); 527588cad61Seric (*e->e_putbody)(mfile, m, FALSE); 528c579ef51Seric (void) fclose(mfile); 529c579ef51Seric 530c579ef51Seric i = endmailer(pid, pvp[0]); 531bc6e2962Seric 532bc6e2962Seric /* arrange a return receipt if requested */ 533588cad61Seric if (e->e_receiptto != NULL && bitset(M_LOCAL, m->m_flags)) 534bc6e2962Seric { 535588cad61Seric e->e_flags |= EF_SENDRECEIPT; 536bc6e2962Seric /* do we want to send back more info? */ 537bc6e2962Seric } 538bc6e2962Seric 539c579ef51Seric return (i); 540c579ef51Seric } 541c579ef51Seric /* 542c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 543c579ef51Seric ** 544c579ef51Seric ** We should never get fatal errors (e.g., segmentation 545c579ef51Seric ** violation), so we report those specially. For other 546c579ef51Seric ** errors, we choose a status message (into statmsg), 547c579ef51Seric ** and if it represents an error, we print it. 548c579ef51Seric ** 549c579ef51Seric ** Parameters: 550c579ef51Seric ** pid -- pid of mailer. 551c579ef51Seric ** name -- name of mailer (for error messages). 552c579ef51Seric ** 553c579ef51Seric ** Returns: 554c579ef51Seric ** exit code of mailer. 555c579ef51Seric ** 556c579ef51Seric ** Side Effects: 557c579ef51Seric ** none. 558c579ef51Seric */ 559c579ef51Seric 560c579ef51Seric endmailer(pid, name) 561c579ef51Seric int pid; 562c579ef51Seric char *name; 563c579ef51Seric { 564588cad61Seric int st; 565c579ef51Seric 56633db8731Seric /* in the IPC case there is nothing to wait for */ 56733db8731Seric if (pid == 0) 56833db8731Seric return (EX_OK); 56933db8731Seric 57033db8731Seric /* wait for the mailer process to die and collect status */ 571588cad61Seric st = waitfor(pid); 572588cad61Seric if (st == -1) 57378de67c1Seric { 574588cad61Seric syserr("endmailer %s: wait", name); 575588cad61Seric return (EX_SOFTWARE); 576c579ef51Seric } 57733db8731Seric 57833db8731Seric /* see if it died a horrid death */ 579c579ef51Seric if ((st & 0377) != 0) 580c579ef51Seric { 581588cad61Seric syserr("endmailer %s: stat %o", name, st); 582c579ef51Seric ExitStat = EX_UNAVAILABLE; 583588cad61Seric return (EX_UNAVAILABLE); 584c579ef51Seric } 58533db8731Seric 58633db8731Seric /* normal death -- return status */ 587588cad61Seric st = (st >> 8) & 0377; 588588cad61Seric return (st); 589c579ef51Seric } 590c579ef51Seric /* 591c579ef51Seric ** OPENMAILER -- open connection to mailer. 592c579ef51Seric ** 593c579ef51Seric ** Parameters: 594c579ef51Seric ** m -- mailer descriptor. 595c579ef51Seric ** pvp -- parameter vector to pass to mailer. 596c579ef51Seric ** ctladdr -- controlling address for user. 597c579ef51Seric ** clever -- create a full duplex connection. 598c579ef51Seric ** pmfile -- pointer to mfile (to mailer) connection. 599c579ef51Seric ** prfile -- pointer to rfile (from mailer) connection. 600c579ef51Seric ** 601c579ef51Seric ** Returns: 60233db8731Seric ** pid of mailer ( > 0 ). 603c579ef51Seric ** -1 on error. 60433db8731Seric ** zero on an IPC connection. 605c579ef51Seric ** 606c579ef51Seric ** Side Effects: 607c579ef51Seric ** creates a mailer in a subprocess. 608c579ef51Seric */ 609c579ef51Seric 610c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 611588cad61Seric MAILER *m; 612c579ef51Seric char **pvp; 613c579ef51Seric ADDRESS *ctladdr; 614c579ef51Seric bool clever; 615c579ef51Seric FILE **pmfile; 616c579ef51Seric FILE **prfile; 617c579ef51Seric { 6185dfc646bSeric int pid; 619f8952a83Seric int mpvect[2]; 620c579ef51Seric int rpvect[2]; 6215dfc646bSeric FILE *mfile; 622c579ef51Seric FILE *rfile; 6235dfc646bSeric extern FILE *fdopen(); 6245dfc646bSeric 6255dfc646bSeric # ifdef DEBUG 6266ef48975Seric if (tTd(11, 1)) 6275dfc646bSeric { 6288c57e552Seric printf("openmailer:"); 6295dfc646bSeric printav(pvp); 6305dfc646bSeric } 6315dfc646bSeric # endif DEBUG 63235490626Seric errno = 0; 6335dfc646bSeric 63433db8731Seric /* 63533db8731Seric ** Deal with the special case of mail handled through an IPC 63633db8731Seric ** connection. 63733db8731Seric ** In this case we don't actually fork. We must be 63833db8731Seric ** running SMTP for this to work. We will return a 63933db8731Seric ** zero pid to indicate that we are running IPC. 64033db8731Seric */ 64133db8731Seric 64233db8731Seric if (strcmp(m->m_mailer, "[IPC]") == 0) 64333db8731Seric { 644588cad61Seric #ifdef DAEMON 64533db8731Seric register int i; 6461277f9a8Seric register u_short port; 64733db8731Seric 64833db8731Seric if (!clever) 64933db8731Seric syserr("non-clever IPC"); 65093b6e3cfSeric if (pvp[2] != NULL) 6511277f9a8Seric port = atoi(pvp[2]); 65293b6e3cfSeric else 6531277f9a8Seric port = 0; 6541277f9a8Seric i = makeconnection(pvp[1], port, pmfile, prfile); 65533db8731Seric if (i != EX_OK) 656ed854c7bSeric { 657ed854c7bSeric ExitStat = i; 65833db8731Seric return (-1); 659ed854c7bSeric } 66033db8731Seric else 66133db8731Seric return (0); 662588cad61Seric #else DAEMON 663588cad61Seric syserr("openmailer: no IPC"); 664588cad61Seric return (-1); 66533db8731Seric #endif DAEMON 666588cad61Seric } 66733db8731Seric 6686328bdf7Seric /* create a pipe to shove the mail through */ 669f8952a83Seric if (pipe(mpvect) < 0) 67025a99e2eSeric { 671588cad61Seric syserr("openmailer: pipe (to mailer)"); 67225a99e2eSeric return (-1); 67325a99e2eSeric } 674c579ef51Seric 6752c7e1b8dSeric #ifdef SMTP 676c579ef51Seric /* if this mailer speaks smtp, create a return pipe */ 677c579ef51Seric if (clever && pipe(rpvect) < 0) 678c579ef51Seric { 679588cad61Seric syserr("openmailer: pipe (from mailer)"); 680c579ef51Seric (void) close(mpvect[0]); 681c579ef51Seric (void) close(mpvect[1]); 682c579ef51Seric return (-1); 683c579ef51Seric } 6842c7e1b8dSeric #endif SMTP 685c579ef51Seric 68633db8731Seric /* 68733db8731Seric ** Actually fork the mailer process. 68833db8731Seric ** DOFORK is clever about retrying. 68933db8731Seric */ 69033db8731Seric 691b254bcb6Seric if (Xscript != NULL) 6926ef48975Seric (void) fflush(Xscript); /* for debugging */ 693588cad61Seric (void) fflush(stdout); 69432d19d43Seric DOFORK(XFORK); 695f129ec7dSeric /* pid is set by DOFORK */ 69625a99e2eSeric if (pid < 0) 69725a99e2eSeric { 69833db8731Seric /* failure */ 699588cad61Seric syserr("openmailer: cannot fork"); 700f8952a83Seric (void) close(mpvect[0]); 701f8952a83Seric (void) close(mpvect[1]); 702588cad61Seric #ifdef SMTP 703c579ef51Seric if (clever) 704c579ef51Seric { 705c579ef51Seric (void) close(rpvect[0]); 706c579ef51Seric (void) close(rpvect[1]); 707c579ef51Seric } 708588cad61Seric #endif SMTP 70925a99e2eSeric return (-1); 71025a99e2eSeric } 71125a99e2eSeric else if (pid == 0) 71225a99e2eSeric { 71325a99e2eSeric /* child -- set up input & exec mailer */ 71403ab8e55Seric /* make diagnostic output be standard output */ 7158f0e7860Seric (void) signal(SIGINT, SIG_IGN); 7168f0e7860Seric (void) signal(SIGHUP, SIG_IGN); 7170984da9fSeric (void) signal(SIGTERM, SIG_DFL); 718f8952a83Seric 719f8952a83Seric /* arrange to filter standard & diag output of command */ 720c579ef51Seric if (clever) 721c579ef51Seric { 722c579ef51Seric (void) close(rpvect[0]); 723c579ef51Seric (void) close(1); 724c579ef51Seric (void) dup(rpvect[1]); 725c579ef51Seric (void) close(rpvect[1]); 726c579ef51Seric } 727276723a8Seric else if (OpMode == MD_SMTP || HoldErrs) 728f8952a83Seric { 729588cad61Seric /* put mailer output in transcript */ 730f8952a83Seric (void) close(1); 7319df2a963Seric (void) dup(fileno(Xscript)); 732f8952a83Seric } 733db8841e9Seric (void) close(2); 734db8841e9Seric (void) dup(1); 735f8952a83Seric 736f8952a83Seric /* arrange to get standard input */ 737f8952a83Seric (void) close(mpvect[1]); 738db8841e9Seric (void) close(0); 739f8952a83Seric if (dup(mpvect[0]) < 0) 74025a99e2eSeric { 74125a99e2eSeric syserr("Cannot dup to zero!"); 742a590b978Seric _exit(EX_OSERR); 74325a99e2eSeric } 744f8952a83Seric (void) close(mpvect[0]); 7452a6e0786Seric if (!bitset(M_RESTR, m->m_flags)) 7460984da9fSeric { 747e36b99e2Seric if (ctladdr->q_uid == 0) 748e36b99e2Seric { 749e36b99e2Seric (void) setgid(DefGid); 750e36b99e2Seric (void) setuid(DefUid); 751e36b99e2Seric } 752e36b99e2Seric else 75369f29479Seric { 754e36b99e2Seric (void) setgid(ctladdr->q_gid); 755e36b99e2Seric (void) setuid(ctladdr->q_uid); 75669f29479Seric } 7570984da9fSeric } 758588cad61Seric 759e374fd72Seric /* 760e374fd72Seric ** We have to be careful with vfork - we can't mung up the 761e374fd72Seric ** memory but we don't want the mailer to inherit any extra 762e374fd72Seric ** open files. Chances are the mailer won't 763e374fd72Seric ** care about an extra file, but then again you never know. 764e374fd72Seric ** Actually, we would like to close(fileno(pwf)), but it's 765e374fd72Seric ** declared static so we can't. But if we fclose(pwf), which 766e374fd72Seric ** is what endpwent does, it closes it in the parent too and 767e374fd72Seric ** the next getpwnam will be slower. If you have a weird 768e374fd72Seric ** mailer that chokes on the extra file you should do the 7694300ddf0Seric ** endpwent(). -MRH 770e374fd72Seric ** 771e374fd72Seric ** Similar comments apply to log. However, openlog is 772e374fd72Seric ** clever enough to set the FIOCLEX mode on the file, 773e374fd72Seric ** so it will be closed automatically on the exec. 774e374fd72Seric */ 775e374fd72Seric 776588cad61Seric closeall(); 77733db8731Seric 77833db8731Seric /* try to execute the mailer */ 77925a99e2eSeric execv(m->m_mailer, pvp); 78033db8731Seric 78125a99e2eSeric /* syserr fails because log is closed */ 78225a99e2eSeric /* syserr("Cannot exec %s", m->m_mailer); */ 78332d19d43Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 784db8841e9Seric (void) fflush(stdout); 785a590b978Seric _exit(EX_UNAVAILABLE); 78625a99e2eSeric } 78725a99e2eSeric 788f8952a83Seric /* 789c579ef51Seric ** Set up return value. 790f8952a83Seric */ 791f8952a83Seric 792f8952a83Seric (void) close(mpvect[0]); 793f8952a83Seric mfile = fdopen(mpvect[1], "w"); 794c579ef51Seric if (clever) 79525a99e2eSeric { 796c579ef51Seric (void) close(rpvect[1]); 797c579ef51Seric rfile = fdopen(rpvect[0], "r"); 79825a99e2eSeric } 799c579ef51Seric 800c579ef51Seric *pmfile = mfile; 801c579ef51Seric *prfile = rfile; 802c579ef51Seric 803c579ef51Seric return (pid); 80425a99e2eSeric } 80525a99e2eSeric /* 80625a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 80725a99e2eSeric ** 80825a99e2eSeric ** Parameters: 80925a99e2eSeric ** stat -- the status code from the mailer (high byte 81025a99e2eSeric ** only; core dumps must have been taken care of 81125a99e2eSeric ** already). 81225a99e2eSeric ** m -- the mailer descriptor for this mailer. 81325a99e2eSeric ** 81425a99e2eSeric ** Returns: 815db8841e9Seric ** none. 81625a99e2eSeric ** 81725a99e2eSeric ** Side Effects: 818c1f9df2cSeric ** Errors may be incremented. 81925a99e2eSeric ** ExitStat may be set. 82025a99e2eSeric */ 82125a99e2eSeric 822588cad61Seric /*ARGSUSED*/ 823588cad61Seric giveresponse(stat, m) 82425a99e2eSeric int stat; 825588cad61Seric register MAILER *m; 82625a99e2eSeric { 82725a99e2eSeric register char *statmsg; 82825a99e2eSeric extern char *SysExMsg[]; 82925a99e2eSeric register int i; 83025a99e2eSeric extern int N_SysEx; 83129dd97a3Seric char buf[30]; 83225a99e2eSeric 83313bbc08cSeric /* 83413bbc08cSeric ** Compute status message from code. 83513bbc08cSeric */ 83613bbc08cSeric 83725a99e2eSeric i = stat - EX__BASE; 838588cad61Seric if (stat == 0) 839588cad61Seric statmsg = "250 Sent"; 840588cad61Seric else if (i < 0 || i > N_SysEx) 841588cad61Seric { 842588cad61Seric (void) sprintf(buf, "554 unknown mailer error %d", stat); 843588cad61Seric stat = EX_UNAVAILABLE; 844588cad61Seric statmsg = buf; 845588cad61Seric } 84625a99e2eSeric else 84725a99e2eSeric statmsg = SysExMsg[i]; 848588cad61Seric 849588cad61Seric /* 850588cad61Seric ** Print the message as appropriate 851588cad61Seric */ 852588cad61Seric 85325a99e2eSeric if (stat == 0) 8545826d9d3Seric message(Arpa_Info, &statmsg[4]); 855c77d1c25Seric else if (stat == EX_TEMPFAIL) 856eb238f8cSeric message(Arpa_Info, "deferred"); 85725a99e2eSeric else 85825a99e2eSeric { 859c1f9df2cSeric Errors++; 8605826d9d3Seric usrerr(statmsg); 86125a99e2eSeric } 86225a99e2eSeric 86325a99e2eSeric /* 86425a99e2eSeric ** Final cleanup. 86525a99e2eSeric ** Log a record of the transaction. Compute the new 86625a99e2eSeric ** ExitStat -- if we already had an error, stick with 86725a99e2eSeric ** that. 86825a99e2eSeric */ 86925a99e2eSeric 87061f5a1d4Seric if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2)) 871eb238f8cSeric logdelivery(&statmsg[4]); 872eb238f8cSeric 873eb238f8cSeric if (stat != EX_TEMPFAIL) 874eb238f8cSeric setstat(stat); 875eb238f8cSeric } 876eb238f8cSeric /* 877eb238f8cSeric ** LOGDELIVERY -- log the delivery in the system log 878eb238f8cSeric ** 879eb238f8cSeric ** Parameters: 880eb238f8cSeric ** stat -- the message to print for the status 881eb238f8cSeric ** 882eb238f8cSeric ** Returns: 883eb238f8cSeric ** none 884eb238f8cSeric ** 885eb238f8cSeric ** Side Effects: 886eb238f8cSeric ** none 887eb238f8cSeric */ 888eb238f8cSeric 889eb238f8cSeric logdelivery(stat) 890eb238f8cSeric char *stat; 8915cf56be3Seric { 8925cf56be3Seric extern char *pintvl(); 8935cf56be3Seric 894eb238f8cSeric # ifdef LOG 8955cf56be3Seric syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id, 896eb238f8cSeric CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), stat); 89725a99e2eSeric # endif LOG 89825a99e2eSeric } 89925a99e2eSeric /* 90051552439Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 90125a99e2eSeric ** 90251552439Seric ** This can be made an arbitrary message separator by changing $l 90351552439Seric ** 90451552439Seric ** One of the ugliest hacks seen by human eyes is 90551552439Seric ** contained herein: UUCP wants those stupid 906588cad61Seric ** "emote from <host>" lines. Why oh why does a 90751552439Seric ** well-meaning programmer such as myself have to 90851552439Seric ** deal with this kind of antique garbage???? 90925a99e2eSeric ** 91025a99e2eSeric ** Parameters: 91151552439Seric ** fp -- the file to output to. 91251552439Seric ** m -- the mailer describing this entry. 91325a99e2eSeric ** 91425a99e2eSeric ** Returns: 91551552439Seric ** none 91625a99e2eSeric ** 91725a99e2eSeric ** Side Effects: 91851552439Seric ** outputs some text to fp. 91925a99e2eSeric */ 92025a99e2eSeric 92151552439Seric putfromline(fp, m) 92251552439Seric register FILE *fp; 92351552439Seric register MAILER *m; 92425a99e2eSeric { 92551552439Seric char buf[MAXLINE]; 92625a99e2eSeric 92751552439Seric if (bitset(M_NHDR, m->m_flags)) 92851552439Seric return; 92913bbc08cSeric 9302c7e1b8dSeric # ifdef UGLYUUCP 931a49f24c0Seric if (bitset(M_UGLYUUCP, m->m_flags)) 93274b6e67bSeric { 93374b6e67bSeric extern char *macvalue(); 9348c57e552Seric char *sys = macvalue('g', CurEnv); 93574b6e67bSeric char *bang = index(sys, '!'); 93674b6e67bSeric 93774b6e67bSeric if (bang == NULL) 93874b6e67bSeric syserr("No ! in UUCP! (%s)", sys); 93974b6e67bSeric else 940588cad61Seric { 94174b6e67bSeric *bang = '\0'; 942518ad6b6Seric expand("From $f $d remote from $g\n", buf, 94351552439Seric &buf[sizeof buf - 1], CurEnv); 94474b6e67bSeric *bang = '!'; 94574b6e67bSeric } 946588cad61Seric } 947a36e30c9Seric else 9482c7e1b8dSeric # endif UGLYUUCP 94951552439Seric expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv); 950d90b8d00Seric putline(buf, fp, bitset(M_FULLSMTP, m->m_flags)); 951bc6e2962Seric } 952bc6e2962Seric /* 95351552439Seric ** PUTBODY -- put the body of a message. 95451552439Seric ** 95551552439Seric ** Parameters: 95651552439Seric ** fp -- file to output onto. 95751552439Seric ** m -- a mailer descriptor. 95851552439Seric ** xdot -- if set, use SMTP hidden dot algorithm. 95951552439Seric ** 96051552439Seric ** Returns: 96151552439Seric ** none. 96251552439Seric ** 96351552439Seric ** Side Effects: 96451552439Seric ** The message is written onto fp. 96551552439Seric */ 96651552439Seric 96751552439Seric putbody(fp, m, xdot) 96851552439Seric FILE *fp; 969588cad61Seric MAILER *m; 97051552439Seric bool xdot; 97151552439Seric { 97251552439Seric char buf[MAXLINE + 1]; 973d90b8d00Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 97451552439Seric 97551552439Seric /* 97651552439Seric ** Output the body of the message 97751552439Seric */ 97851552439Seric 97951552439Seric if (TempFile != NULL) 98051552439Seric { 98151552439Seric rewind(TempFile); 98251552439Seric buf[0] = '.'; 98351552439Seric while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL) 984d90b8d00Seric putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp); 98551552439Seric 98651552439Seric if (ferror(TempFile)) 98751552439Seric { 98851552439Seric syserr("putbody: read error"); 98951552439Seric ExitStat = EX_IOERR; 99051552439Seric } 99151552439Seric } 99251552439Seric 99351552439Seric (void) fflush(fp); 99451552439Seric if (ferror(fp) && errno != EPIPE) 99551552439Seric { 99651552439Seric syserr("putbody: write error"); 99751552439Seric ExitStat = EX_IOERR; 99851552439Seric } 99951552439Seric errno = 0; 100025a99e2eSeric } 100125a99e2eSeric /* 100225a99e2eSeric ** MAILFILE -- Send a message to a file. 100325a99e2eSeric ** 1004f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 1005f129ec7dSeric ** bits, sendmail will try to become the owner of that file 1006f129ec7dSeric ** rather than the real user. Obviously, this only works if 1007f129ec7dSeric ** sendmail runs as root. 1008f129ec7dSeric ** 1009588cad61Seric ** This could be done as a subordinate mailer, except that it 1010588cad61Seric ** is used implicitly to save messages in ~/dead.letter. We 1011588cad61Seric ** view this as being sufficiently important as to include it 1012588cad61Seric ** here. For example, if the system is dying, we shouldn't have 1013588cad61Seric ** to create another process plus some pipes to save the message. 1014588cad61Seric ** 101525a99e2eSeric ** Parameters: 101625a99e2eSeric ** filename -- the name of the file to send to. 10176259796dSeric ** ctladdr -- the controlling address header -- includes 10186259796dSeric ** the userid/groupid to be when sending. 101925a99e2eSeric ** 102025a99e2eSeric ** Returns: 102125a99e2eSeric ** The exit code associated with the operation. 102225a99e2eSeric ** 102325a99e2eSeric ** Side Effects: 102425a99e2eSeric ** none. 102525a99e2eSeric */ 102625a99e2eSeric 10276259796dSeric mailfile(filename, ctladdr) 102825a99e2eSeric char *filename; 10296259796dSeric ADDRESS *ctladdr; 103025a99e2eSeric { 103125a99e2eSeric register FILE *f; 103232d19d43Seric register int pid; 103325a99e2eSeric 103432d19d43Seric /* 103532d19d43Seric ** Fork so we can change permissions here. 103632d19d43Seric ** Note that we MUST use fork, not vfork, because of 103732d19d43Seric ** the complications of calling subroutines, etc. 103832d19d43Seric */ 103932d19d43Seric 104032d19d43Seric DOFORK(fork); 104132d19d43Seric 104232d19d43Seric if (pid < 0) 104332d19d43Seric return (EX_OSERR); 104432d19d43Seric else if (pid == 0) 104532d19d43Seric { 104632d19d43Seric /* child -- actually write to file */ 1047f129ec7dSeric struct stat stb; 1048f129ec7dSeric 10490984da9fSeric (void) signal(SIGINT, SIG_DFL); 10500984da9fSeric (void) signal(SIGHUP, SIG_DFL); 10510984da9fSeric (void) signal(SIGTERM, SIG_DFL); 1052f129ec7dSeric umask(OldUmask); 1053f129ec7dSeric if (stat(filename, &stb) < 0) 1054e6e1265fSeric stb.st_mode = 0666; 1055f129ec7dSeric if (bitset(0111, stb.st_mode)) 1056f129ec7dSeric exit(EX_CANTCREAT); 105703827b5fSeric if (ctladdr == NULL) 10587a941e7aSeric ctladdr = &CurEnv->e_from; 1059f129ec7dSeric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 1060e36b99e2Seric { 1061e36b99e2Seric if (ctladdr->q_uid == 0) 1062e36b99e2Seric (void) setgid(DefGid); 1063e36b99e2Seric else 10646259796dSeric (void) setgid(ctladdr->q_gid); 1065e36b99e2Seric } 1066f129ec7dSeric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 1067e36b99e2Seric { 1068e36b99e2Seric if (ctladdr->q_uid == 0) 1069e36b99e2Seric (void) setuid(DefUid); 1070e36b99e2Seric else 10716259796dSeric (void) setuid(ctladdr->q_uid); 1072e36b99e2Seric } 107327628d59Seric f = dfopen(filename, "a"); 107425a99e2eSeric if (f == NULL) 107532d19d43Seric exit(EX_CANTCREAT); 107625a99e2eSeric 1077588cad61Seric putfromline(f, ProgMailer); 1078588cad61Seric (*CurEnv->e_puthdr)(f, ProgMailer, CurEnv); 1079d90b8d00Seric fputs("\n", f); 1080588cad61Seric (*CurEnv->e_putbody)(f, ProgMailer, FALSE); 108125a99e2eSeric fputs("\n", f); 1082db8841e9Seric (void) fclose(f); 108332d19d43Seric (void) fflush(stdout); 1084e36b99e2Seric 108527628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 1086c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 108732d19d43Seric exit(EX_OK); 108813bbc08cSeric /*NOTREACHED*/ 108932d19d43Seric } 109032d19d43Seric else 109132d19d43Seric { 109232d19d43Seric /* parent -- wait for exit status */ 1093588cad61Seric int st; 109432d19d43Seric 1095588cad61Seric st = waitfor(pid); 1096588cad61Seric if ((st & 0377) != 0) 1097588cad61Seric return (EX_UNAVAILABLE); 1098588cad61Seric else 1099588cad61Seric return ((st >> 8) & 0377); 110032d19d43Seric } 110125a99e2eSeric } 1102ea4dc939Seric /* 1103ea4dc939Seric ** SENDALL -- actually send all the messages. 1104ea4dc939Seric ** 1105ea4dc939Seric ** Parameters: 11060c52a0b3Seric ** e -- the envelope to send. 1107276723a8Seric ** mode -- the delivery mode to use. 1108ea4dc939Seric ** 1109ea4dc939Seric ** Returns: 1110ea4dc939Seric ** none. 1111ea4dc939Seric ** 1112ea4dc939Seric ** Side Effects: 1113ea4dc939Seric ** Scans the send lists and sends everything it finds. 11140c52a0b3Seric ** Delivers any appropriate error messages. 1115276723a8Seric ** If we are running in a non-interactive mode, takes the 1116276723a8Seric ** appropriate action. 1117ea4dc939Seric */ 1118ea4dc939Seric 1119276723a8Seric sendall(e, mode) 11200c52a0b3Seric ENVELOPE *e; 1121276723a8Seric char mode; 1122ea4dc939Seric { 1123e77e673fSeric register ADDRESS *q; 112414a8ed7aSeric bool oldverbose; 1125276723a8Seric int pid; 1126ea4dc939Seric 1127772e6e50Seric #ifdef DEBUG 1128df864a8fSeric if (tTd(13, 1)) 1129772e6e50Seric { 1130276723a8Seric printf("\nSENDALL: mode %c, sendqueue:\n", mode); 11310c52a0b3Seric printaddr(e->e_sendqueue, TRUE); 1132772e6e50Seric } 1133772e6e50Seric #endif DEBUG 1134ea4dc939Seric 11350c52a0b3Seric /* 1136276723a8Seric ** Do any preprocessing necessary for the mode we are running. 1137588cad61Seric ** Check to make sure the hop count is reasonable. 1138588cad61Seric ** Delete sends to the sender in mailing lists. 1139276723a8Seric */ 1140276723a8Seric 1141588cad61Seric CurEnv = e; 1142276723a8Seric 1143588cad61Seric if (e->e_hopcount > MAXHOP) 1144276723a8Seric { 1145588cad61Seric syserr("sendall: too many hops (%d max)", MAXHOP); 1146588cad61Seric return; 1147588cad61Seric } 1148588cad61Seric 1149588cad61Seric if (!MeToo) 1150276723a8Seric { 1151588cad61Seric e->e_from.q_flags |= QDONTSEND; 1152588cad61Seric recipient(&e->e_from, &e->e_sendqueue); 1153276723a8Seric } 1154588cad61Seric 1155588cad61Seric # ifdef QUEUE 1156b254bcb6Seric if ((mode == SM_QUEUE || mode == SM_FORK || 1157b254bcb6Seric (mode != SM_VERIFY && SuperSafe)) && 1158b254bcb6Seric !bitset(EF_INQUEUE, e->e_flags)) 1159588cad61Seric queueup(e, TRUE, mode == SM_QUEUE); 1160276723a8Seric #endif QUEUE 1161276723a8Seric 1162276723a8Seric oldverbose = Verbose; 1163276723a8Seric switch (mode) 1164276723a8Seric { 1165276723a8Seric case SM_VERIFY: 1166276723a8Seric Verbose = TRUE; 1167276723a8Seric break; 1168276723a8Seric 1169276723a8Seric case SM_QUEUE: 1170b254bcb6Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 1171276723a8Seric return; 1172276723a8Seric 1173276723a8Seric case SM_FORK: 1174b254bcb6Seric if (Xscript != NULL) 1175a6fce3d8Seric (void) fflush(Xscript); 1176276723a8Seric pid = fork(); 1177276723a8Seric if (pid < 0) 1178276723a8Seric { 1179276723a8Seric mode = SM_DELIVER; 1180276723a8Seric break; 1181276723a8Seric } 1182276723a8Seric else if (pid > 0) 1183a6fce3d8Seric { 1184a6fce3d8Seric /* be sure we leave the temp files to our child */ 1185b254bcb6Seric e->e_id = e->e_df = NULL; 1186276723a8Seric return; 1187a6fce3d8Seric } 1188276723a8Seric 1189276723a8Seric /* double fork to avoid zombies */ 1190276723a8Seric if (fork() > 0) 1191276723a8Seric exit(EX_OK); 1192276723a8Seric 1193a6fce3d8Seric /* be sure we are immune from the terminal */ 1194e2e30df7Seric disconnect(); 1195a6fce3d8Seric 1196276723a8Seric break; 1197276723a8Seric } 1198276723a8Seric 1199276723a8Seric /* 12000c52a0b3Seric ** Run through the list and send everything. 12010c52a0b3Seric */ 12020c52a0b3Seric 12030c52a0b3Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1204ea4dc939Seric { 1205276723a8Seric if (mode == SM_VERIFY) 1206ea4dc939Seric { 1207a6fce3d8Seric e->e_to = q->q_paddr; 1208e77e673fSeric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1209ea4dc939Seric message(Arpa_Info, "deliverable"); 1210ea4dc939Seric } 1211ea4dc939Seric else 1212588cad61Seric (void) deliver(e, q); 1213ea4dc939Seric } 121414a8ed7aSeric Verbose = oldverbose; 12150c52a0b3Seric 12160c52a0b3Seric /* 12170c52a0b3Seric ** Now run through and check for errors. 12180c52a0b3Seric */ 12190c52a0b3Seric 1220276723a8Seric if (mode == SM_VERIFY) 12210c52a0b3Seric return; 12220c52a0b3Seric 12230c52a0b3Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 12240c52a0b3Seric { 12250c52a0b3Seric register ADDRESS *qq; 12260c52a0b3Seric 1227df864a8fSeric # ifdef DEBUG 1228df864a8fSeric if (tTd(13, 3)) 1229df864a8fSeric { 1230df864a8fSeric printf("Checking "); 1231df864a8fSeric printaddr(q, FALSE); 1232df864a8fSeric } 1233df864a8fSeric # endif DEBUG 1234df864a8fSeric 1235b254bcb6Seric /* only send errors if the message failed */ 1236b254bcb6Seric if (!bitset(QBADADDR, q->q_flags)) 1237b254bcb6Seric continue; 12380c52a0b3Seric 12390c52a0b3Seric /* we have an address that failed -- find the parent */ 12400c52a0b3Seric for (qq = q; qq != NULL; qq = qq->q_alias) 12410c52a0b3Seric { 12420c52a0b3Seric char obuf[MAXNAME + 6]; 12430c52a0b3Seric extern char *aliaslookup(); 12440c52a0b3Seric 12450c52a0b3Seric /* we can only have owners for local addresses */ 12460c52a0b3Seric if (!bitset(M_LOCAL, qq->q_mailer->m_flags)) 12470c52a0b3Seric continue; 12480c52a0b3Seric 12490c52a0b3Seric /* see if the owner list exists */ 12500c52a0b3Seric (void) strcpy(obuf, "owner-"); 1251cec031e3Seric if (strncmp(qq->q_user, "owner-", 6) == 0) 1252cec031e3Seric (void) strcat(obuf, "owner"); 1253cec031e3Seric else 12540c52a0b3Seric (void) strcat(obuf, qq->q_user); 12550c52a0b3Seric if (aliaslookup(obuf) == NULL) 12560c52a0b3Seric continue; 12570c52a0b3Seric 1258df864a8fSeric # ifdef DEBUG 1259df864a8fSeric if (tTd(13, 4)) 1260df864a8fSeric printf("Errors to %s\n", obuf); 1261df864a8fSeric # endif DEBUG 1262df864a8fSeric 12630c52a0b3Seric /* owner list exists -- add it to the error queue */ 1264588cad61Seric sendto(obuf, (ADDRESS *) NULL, &e->e_errorqueue); 1265588cad61Seric ErrorMode == EM_MAIL; 12660c52a0b3Seric break; 12670c52a0b3Seric } 12680c52a0b3Seric 12690c52a0b3Seric /* if we did not find an owner, send to the sender */ 12707455aa0bSeric if (qq == NULL && bitset(QBADADDR, q->q_flags)) 1271656d6d13Seric sendto(e->e_from.q_paddr, qq, &e->e_errorqueue); 12720c52a0b3Seric } 1273276723a8Seric 1274276723a8Seric if (mode == SM_FORK) 1275276723a8Seric finis(); 12760c52a0b3Seric } 1277