1294Seric # include <signal.h> 24123Seric # include <errno.h> 34621Seric # include "sendmail.h" 44327Seric # include <sys/stat.h> 5294Seric 6*8093Seric SCCSID(@(#)deliver.c 3.110 09/06/82); 7405Seric 8294Seric /* 94315Seric ** DELIVER -- Deliver a message to a list of addresses. 10294Seric ** 114315Seric ** This routine delivers to everyone on the same host as the 124315Seric ** user on the head of the list. It is clever about mailers 134315Seric ** that don't handle multiple users. It is NOT guaranteed 144315Seric ** that it will deliver to all these addresses however -- so 154315Seric ** deliver should be called once for each address on the 164315Seric ** list. 174315Seric ** 18294Seric ** Parameters: 194621Seric ** firstto -- head of the address list to deliver to. 20294Seric ** 21294Seric ** Returns: 22294Seric ** zero -- successfully delivered. 23294Seric ** else -- some failure, see ExitStat for more info. 24294Seric ** 25294Seric ** Side Effects: 26294Seric ** The standard input is passed off to someone. 27294Seric */ 28294Seric 296974Seric deliver(firstto) 304621Seric ADDRESS *firstto; 31294Seric { 324452Seric char *host; /* host being sent to */ 334452Seric char *user; /* user being sent to */ 34294Seric char **pvp; 353233Seric register char **mvp; 363233Seric register char *p; 374452Seric register struct mailer *m; /* mailer for this recipient */ 382968Seric extern bool checkcompat(); 393233Seric char *pv[MAXPV+1]; 404452Seric char tobuf[MAXLINE]; /* text line of to people */ 413233Seric char buf[MAXNAME]; 424397Seric ADDRESS *ctladdr; 434397Seric extern ADDRESS *getctladdr(); 444452Seric char tfrombuf[MAXNAME]; /* translated from person */ 454452Seric extern char **prescan(); 464621Seric register ADDRESS *to = firstto; 474863Seric bool clever = FALSE; /* running user smtp to this mailer */ 485032Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 497052Seric bool notopen = TRUE; /* set if connection not quite open */ 508003Seric register int rcode; /* response code */ 51294Seric 524488Seric errno = 0; 537052Seric if (bitset(QDONTSEND, to->q_flags)) 543233Seric return (0); 55294Seric 566974Seric m = to->q_mailer; 576974Seric host = to->q_host; 586974Seric 59294Seric # ifdef DEBUG 607672Seric if (tTd(10, 1)) 613233Seric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 626974Seric m->m_mno, host, to->q_user); 63294Seric # endif DEBUG 64294Seric 65294Seric /* 665903Seric ** If this mailer is expensive, and if we don't want to make 675903Seric ** connections now, just mark these addresses and return. 685903Seric ** This is useful if we want to batch connections to 695903Seric ** reduce load. This will cause the messages to be 705903Seric ** queued up, and a daemon will come along to send the 715903Seric ** messages later. 725903Seric ** This should be on a per-mailer basis. 735903Seric */ 745903Seric 755903Seric if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags)) 765903Seric { 775903Seric for (; to != NULL; to = to->q_next) 787875Seric if (!bitset(QDONTSEND, to->q_flags) && 797875Seric to->q_mailer == firstto->q_mailer) 805903Seric to->q_flags |= QQUEUEUP|QDONTSEND; 815903Seric return (0); 825903Seric } 835903Seric 845903Seric /* 853233Seric ** Do initial argv setup. 863233Seric ** Insert the mailer name. Notice that $x expansion is 873233Seric ** NOT done on the mailer name. Then, if the mailer has 883233Seric ** a picky -f flag, we insert it as appropriate. This 893233Seric ** code does not check for 'pv' overflow; this places a 903233Seric ** manifest lower limit of 4 for MAXPV. 918062Seric ** The from address rewrite is expected to make 928062Seric ** the address relative to the other end. 932968Seric */ 942968Seric 954452Seric /* rewrite from address, using rewriting rules */ 968062Seric expand("$f", buf, &buf[sizeof buf - 1], CurEnv); 974452Seric mvp = prescan(buf, '\0'); 988062Seric rewrite(mvp, m->m_s_rwset); 994452Seric cataddr(mvp, tfrombuf, sizeof tfrombuf); 1004452Seric 1014452Seric define('g', tfrombuf); /* translated sender address */ 1023233Seric define('h', host); /* to host */ 1033233Seric Errors = 0; 1043233Seric pvp = pv; 1053233Seric *pvp++ = m->m_argv[0]; 1062968Seric 1073233Seric /* insert -f or -r flag as appropriate */ 1083233Seric if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag) 1093233Seric { 1103233Seric if (bitset(M_FOPT, m->m_flags)) 1113233Seric *pvp++ = "-f"; 1123233Seric else 1133233Seric *pvp++ = "-r"; 1146974Seric expand("$g", buf, &buf[sizeof buf - 1], CurEnv); 1153233Seric *pvp++ = newstr(buf); 1163233Seric } 117294Seric 118294Seric /* 1193233Seric ** Append the other fixed parts of the argv. These run 1203233Seric ** up to the first entry containing "$u". There can only 1213233Seric ** be one of these, and there are only a few more slots 1223233Seric ** in the pv after it. 123294Seric */ 124294Seric 1253233Seric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 126294Seric { 1273233Seric while ((p = index(p, '$')) != NULL) 1283233Seric if (*++p == 'u') 1293233Seric break; 1303233Seric if (p != NULL) 1313233Seric break; 1323233Seric 1333233Seric /* this entry is safe -- go ahead and process it */ 1346974Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 1353233Seric *pvp++ = newstr(buf); 1363233Seric if (pvp >= &pv[MAXPV - 3]) 1373233Seric { 1383233Seric syserr("Too many parameters to %s before $u", pv[0]); 1393233Seric return (-1); 1403233Seric } 141294Seric } 1424863Seric 1436038Seric /* 1446038Seric ** If we have no substitution for the user name in the argument 1456038Seric ** list, we know that we must supply the names otherwise -- and 1466038Seric ** SMTP is the answer!! 1476038Seric */ 1486038Seric 1493233Seric if (*mvp == NULL) 1504863Seric { 1514863Seric /* running SMTP */ 1525179Seric # ifdef SMTP 1534863Seric clever = TRUE; 1544863Seric *pvp = NULL; 1555179Seric # else SMTP 1566038Seric /* oops! we don't implement SMTP */ 1575179Seric syserr("SMTP style mailer"); 1585179Seric return (EX_SOFTWARE); 1595179Seric # endif SMTP 1604863Seric } 161294Seric 162294Seric /* 1633233Seric ** At this point *mvp points to the argument with $u. We 1643233Seric ** run through our address list and append all the addresses 1653233Seric ** we can. If we run out of space, do not fret! We can 1663233Seric ** always send another copy later. 167294Seric */ 168294Seric 1693233Seric tobuf[0] = '\0'; 1706900Seric CurEnv->e_to = tobuf; 1714397Seric ctladdr = NULL; 1723233Seric for (; to != NULL; to = to->q_next) 173294Seric { 1743233Seric /* avoid sending multiple recipients to dumb mailers */ 1754382Seric if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags)) 1763233Seric break; 1773233Seric 1783233Seric /* if already sent or not for this host, don't send */ 1797052Seric if (bitset(QDONTSEND, to->q_flags) || 1807052Seric strcmp(to->q_host, host) != 0 || 1817052Seric to->q_mailer != firstto->q_mailer) 1823233Seric continue; 1834397Seric 1845032Seric # ifdef DEBUG 1857672Seric if (tTd(10, 1)) 1865032Seric { 1875032Seric printf("\nsend to "); 1885032Seric printaddr(to, FALSE); 1895032Seric } 1905032Seric # endif DEBUG 1915032Seric 1924397Seric /* compute effective uid/gid when sending */ 1934596Seric if (to->q_mailer == ProgMailer) 1944397Seric ctladdr = getctladdr(to); 1954397Seric 1963233Seric user = to->q_user; 1976900Seric CurEnv->e_to = to->q_paddr; 1983233Seric to->q_flags |= QDONTSEND; 1993233Seric 2003233Seric /* 2013233Seric ** Check to see that these people are allowed to 2023233Seric ** talk to each other. 2033233Seric */ 2043233Seric 2053233Seric if (!checkcompat(to)) 206294Seric { 2073233Seric giveresponse(EX_UNAVAILABLE, TRUE, m); 2083233Seric continue; 209294Seric } 2103233Seric 2113233Seric /* 2124099Seric ** Strip quote bits from names if the mailer is dumb 2134099Seric ** about them. 2143233Seric */ 2153233Seric 2163233Seric if (bitset(M_STRIPQ, m->m_flags)) 217294Seric { 2184099Seric stripquotes(user, TRUE); 2194099Seric stripquotes(host, TRUE); 2203233Seric } 2214099Seric else 2224099Seric { 2234099Seric stripquotes(user, FALSE); 2244099Seric stripquotes(host, FALSE); 2254099Seric } 2263233Seric 2273233Seric /* 2287052Seric ** Do initial connection setup if needed. 2297052Seric */ 2307052Seric 2317052Seric if (notopen) 2327052Seric { 2337052Seric message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name); 2347052Seric # ifdef SMTP 2357052Seric if (clever) 2367052Seric { 2377052Seric /* send the initial SMTP protocol */ 2388003Seric rcode = smtpinit(m, pv, (ADDRESS *) NULL); 2397052Seric } 2407052Seric # ifdef SMTP 2417052Seric notopen = FALSE; 2427052Seric } 2437052Seric 2447052Seric /* 2456059Seric ** Pass it to the other host if we are running SMTP. 2466059Seric */ 2476059Seric 2486059Seric if (clever) 2496059Seric { 2506059Seric # ifdef SMTP 2518003Seric if (rcode == EX_OK) 2528003Seric rcode = smtprcpt(to); 2538003Seric if (rcode != EX_OK) 2546059Seric { 2558003Seric if (rcode == EX_TEMPFAIL) 2566059Seric to->q_flags |= QQUEUEUP; 2576059Seric else 2586059Seric to->q_flags |= QBADADDR; 2598003Seric giveresponse(rcode, TRUE, m); 2606059Seric } 2616059Seric # else SMTP 2626059Seric syserr("trying to be clever"); 2636059Seric # endif SMTP 2646059Seric } 2656059Seric 2666059Seric /* 2674161Seric ** If an error message has already been given, don't 2684161Seric ** bother to send to this address. 2694161Seric ** 2704161Seric ** >>>>>>>>>> This clause assumes that the local mailer 2714161Seric ** >> NOTE >> cannot do any further aliasing; that 2724161Seric ** >>>>>>>>>> function is subsumed by sendmail. 2734161Seric */ 2744161Seric 2757293Seric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 2764161Seric continue; 2774161Seric 2784283Seric /* save statistics.... */ 2794596Seric Stat.stat_nt[to->q_mailer->m_mno]++; 2806900Seric Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize); 2814283Seric 2824161Seric /* 2833233Seric ** See if this user name is "special". 2843233Seric ** If the user name has a slash in it, assume that this 2856974Seric ** is a file -- send it off without further ado. Note 2866974Seric ** that this type of addresses is not processed along 2876974Seric ** with the others, so we fudge on the To person. 2883233Seric */ 2893233Seric 2904596Seric if (m == LocalMailer) 2913233Seric { 2925599Seric if (user[0] == '/') 293294Seric { 2948003Seric rcode = mailfile(user, getctladdr(to)); 2958003Seric giveresponse(rcode, TRUE, m); 2963233Seric continue; 297294Seric } 298294Seric } 2993233Seric 3004315Seric /* 3014315Seric ** Address is verified -- add this user to mailer 3024315Seric ** argv, and add it to the print list of recipients. 3034315Seric */ 3044315Seric 3056059Seric /* link together the chain of recipients */ 3066272Seric to->q_tchain = tochain; 3076272Seric tochain = to; 3086059Seric 3093233Seric /* create list of users for error messages */ 3103233Seric if (tobuf[0] != '\0') 3114082Seric (void) strcat(tobuf, ","); 3124082Seric (void) strcat(tobuf, to->q_paddr); 3133233Seric define('u', user); /* to user */ 3144078Seric define('z', to->q_home); /* user's home */ 3153233Seric 3164863Seric /* 3176059Seric ** Expand out this user into argument list. 3184863Seric */ 3194863Seric 3206059Seric if (!clever) 3213233Seric { 3226974Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 3234863Seric *pvp++ = newstr(buf); 3244863Seric if (pvp >= &pv[MAXPV - 2]) 3254863Seric { 3264863Seric /* allow some space for trailing parms */ 3274863Seric break; 3284863Seric } 3294863Seric } 330294Seric } 331294Seric 3324067Seric /* see if any addresses still exist */ 3334067Seric if (tobuf[0] == '\0') 3344863Seric { 3355179Seric # ifdef SMTP 3364863Seric if (clever) 3377228Seric smtpquit(pv[0], FALSE); 3385179Seric # endif SMTP 3397003Seric define('g', (char *) NULL); 3404067Seric return (0); 3414863Seric } 3424067Seric 3433233Seric /* print out messages as full list */ 3446900Seric CurEnv->e_to = tobuf; 3453233Seric 346294Seric /* 3473233Seric ** Fill out any parameters after the $u parameter. 348294Seric */ 349294Seric 3504863Seric while (!clever && *++mvp != NULL) 351294Seric { 3526974Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 3533233Seric *pvp++ = newstr(buf); 3543233Seric if (pvp >= &pv[MAXPV]) 3553233Seric syserr("deliver: pv overflow after $u for %s", pv[0]); 356294Seric } 3573233Seric *pvp++ = NULL; 358294Seric 359294Seric /* 360294Seric ** Call the mailer. 3612898Seric ** The argument vector gets built, pipes 362294Seric ** are created as necessary, and we fork & exec as 3632898Seric ** appropriate. 3644863Seric ** If we are running SMTP, we just need to clean up. 365294Seric */ 366294Seric 3674397Seric if (ctladdr == NULL) 3686900Seric ctladdr = &CurEnv->e_from; 3695179Seric # ifdef SMTP 3704863Seric if (clever) 3714863Seric { 3728003Seric rcode = smtpfinish(m, CurEnv); 3738003Seric if (rcode != EX_OK) 3748003Seric giveresponse(rcode, TRUE, m); 3758003Seric smtpquit(pv[0], rcode == EX_OK); 3764863Seric } 3774863Seric else 3785179Seric # endif SMTP 3798003Seric rcode = sendoff(m, pv, ctladdr); 3803233Seric 3814621Seric /* 3824621Seric ** If we got a temporary failure, arrange to queue the 3834621Seric ** addressees. 3844621Seric */ 3854621Seric 3868003Seric if (rcode == EX_TEMPFAIL) 3874621Seric { 3885032Seric for (to = tochain; to != NULL; to = to->q_tchain) 3894621Seric to->q_flags |= QQUEUEUP; 3904621Seric } 3914621Seric 3924488Seric errno = 0; 3937003Seric define('g', (char *) NULL); 3948003Seric return (rcode); 3953233Seric } 3963233Seric /* 3974214Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 3984214Seric ** 3994214Seric ** This MUST be a macro, since after a vfork we are running 4004214Seric ** two processes on the same stack!!! 4014214Seric ** 4024214Seric ** Parameters: 4034214Seric ** none. 4044214Seric ** 4054214Seric ** Returns: 4064214Seric ** From a macro??? You've got to be kidding! 4074214Seric ** 4084214Seric ** Side Effects: 4094214Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 4104214Seric ** pid of child in parent, zero in child. 4114214Seric ** -1 on unrecoverable error. 4124214Seric ** 4134214Seric ** Notes: 4144214Seric ** I'm awfully sorry this looks so awful. That's 4154214Seric ** vfork for you..... 4164214Seric */ 4174214Seric 4184214Seric # define NFORKTRIES 5 4194214Seric # ifdef VFORK 4204214Seric # define XFORK vfork 4214214Seric # else VFORK 4224214Seric # define XFORK fork 4234214Seric # endif VFORK 4244214Seric 4254214Seric # define DOFORK(fORKfN) \ 4264214Seric {\ 4274214Seric register int i;\ 4284214Seric \ 4294214Seric for (i = NFORKTRIES; i-- > 0; )\ 4304214Seric {\ 4314214Seric pid = fORKfN();\ 4324214Seric if (pid >= 0)\ 4334214Seric break;\ 4347003Seric sleep(NFORKTRIES - i);\ 4354214Seric }\ 4364214Seric } 4374214Seric /* 4384637Seric ** DOFORK -- simple fork interface to DOFORK. 4394637Seric ** 4404637Seric ** Parameters: 4414637Seric ** none. 4424637Seric ** 4434637Seric ** Returns: 4444637Seric ** pid of child in parent. 4454637Seric ** zero in child. 4464637Seric ** -1 on error. 4474637Seric ** 4484637Seric ** Side Effects: 4494637Seric ** returns twice, once in parent and once in child. 4504637Seric */ 4514637Seric 4524637Seric dofork() 4534637Seric { 4544637Seric register int pid; 4554637Seric 4564637Seric DOFORK(fork); 4574637Seric return (pid); 4584637Seric } 4594637Seric /* 4603233Seric ** SENDOFF -- send off call to mailer & collect response. 4613233Seric ** 4623233Seric ** Parameters: 4633233Seric ** m -- mailer descriptor. 4643233Seric ** pvp -- parameter vector to send to it. 4654397Seric ** ctladdr -- an address pointer controlling the 4664397Seric ** user/groupid etc. of the mailer. 4673233Seric ** 4683233Seric ** Returns: 4693233Seric ** exit status of mailer. 4703233Seric ** 4713233Seric ** Side Effects: 4723233Seric ** none. 4733233Seric */ 4743233Seric 4756974Seric sendoff(m, pvp, ctladdr) 4763233Seric struct mailer *m; 4773233Seric char **pvp; 4784397Seric ADDRESS *ctladdr; 4793233Seric { 4804863Seric auto FILE *mfile; 4814863Seric auto FILE *rfile; 4823233Seric register int i; 4833233Seric int pid; 4844863Seric 4854863Seric /* 4864863Seric ** Create connection to mailer. 4874863Seric */ 4884863Seric 4894863Seric pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 4904863Seric if (pid < 0) 4914863Seric return (-1); 4924863Seric 4934863Seric /* 4944863Seric ** Format and send message. 4954863Seric */ 4964863Seric 4974863Seric (void) signal(SIGPIPE, SIG_IGN); 4986974Seric putfromline(mfile, m); 4996974Seric (*CurEnv->e_puthdr)(mfile, m, CurEnv); 5006974Seric fprintf(mfile, "\n"); 5016974Seric (*CurEnv->e_putbody)(mfile, m, FALSE); 5024863Seric (void) fclose(mfile); 5034863Seric 5044863Seric i = endmailer(pid, pvp[0]); 5054863Seric giveresponse(i, TRUE, m); 5065981Seric 5075981Seric /* arrange a return receipt if requested */ 5088062Seric if (CurEnv->e_receiptto != NULL && bitset(M_LOCAL, m->m_flags)) 5095981Seric { 5106900Seric CurEnv->e_sendreceipt = TRUE; 5118062Seric if (ExitStat == EX_OK) 5128062Seric fprintf(Xscript, "%s... successfully delivered\n", 5138062Seric CurEnv->e_to); 5145981Seric /* do we want to send back more info? */ 5155981Seric } 5165981Seric 5174863Seric return (i); 5184863Seric } 5194863Seric /* 5204863Seric ** ENDMAILER -- Wait for mailer to terminate. 5214863Seric ** 5224863Seric ** We should never get fatal errors (e.g., segmentation 5234863Seric ** violation), so we report those specially. For other 5244863Seric ** errors, we choose a status message (into statmsg), 5254863Seric ** and if it represents an error, we print it. 5264863Seric ** 5274863Seric ** Parameters: 5284863Seric ** pid -- pid of mailer. 5294863Seric ** name -- name of mailer (for error messages). 5304863Seric ** 5314863Seric ** Returns: 5324863Seric ** exit code of mailer. 5334863Seric ** 5344863Seric ** Side Effects: 5354863Seric ** none. 5364863Seric */ 5374863Seric 5384863Seric endmailer(pid, name) 5394863Seric int pid; 5404863Seric char *name; 5414863Seric { 5424863Seric register int i; 5434863Seric auto int st; 5444863Seric 5456038Seric /* in the IPC case there is nothing to wait for */ 5466038Seric if (pid == 0) 5476038Seric return (EX_OK); 5486038Seric 5496038Seric /* wait for the mailer process to die and collect status */ 5504863Seric while ((i = wait(&st)) > 0 && i != pid) 5514863Seric continue; 5524863Seric if (i < 0) 5534863Seric { 5544863Seric syserr("wait"); 5554863Seric return (-1); 5564863Seric } 5576038Seric 5586038Seric /* see if it died a horrid death */ 5594863Seric if ((st & 0377) != 0) 5604863Seric { 5614863Seric syserr("%s: stat %o", name, st); 5624863Seric ExitStat = EX_UNAVAILABLE; 5634863Seric return (-1); 5644863Seric } 5656038Seric 5666038Seric /* normal death -- return status */ 5674863Seric i = (st >> 8) & 0377; 5684863Seric return (i); 5694863Seric } 5704863Seric /* 5714863Seric ** OPENMAILER -- open connection to mailer. 5724863Seric ** 5734863Seric ** Parameters: 5744863Seric ** m -- mailer descriptor. 5754863Seric ** pvp -- parameter vector to pass to mailer. 5764863Seric ** ctladdr -- controlling address for user. 5774863Seric ** clever -- create a full duplex connection. 5784863Seric ** pmfile -- pointer to mfile (to mailer) connection. 5794863Seric ** prfile -- pointer to rfile (from mailer) connection. 5804863Seric ** 5814863Seric ** Returns: 5826038Seric ** pid of mailer ( > 0 ). 5834863Seric ** -1 on error. 5846038Seric ** zero on an IPC connection. 5854863Seric ** 5864863Seric ** Side Effects: 5874863Seric ** creates a mailer in a subprocess. 5884863Seric */ 5894863Seric 5904863Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 5914863Seric struct mailer *m; 5924863Seric char **pvp; 5934863Seric ADDRESS *ctladdr; 5944863Seric bool clever; 5954863Seric FILE **pmfile; 5964863Seric FILE **prfile; 5974863Seric { 5984863Seric int pid; 5994709Seric int mpvect[2]; 6004863Seric int rpvect[2]; 6013233Seric FILE *mfile; 6024863Seric FILE *rfile; 6033233Seric extern FILE *fdopen(); 6043233Seric 6053233Seric # ifdef DEBUG 6067672Seric if (tTd(11, 1)) 607294Seric { 6084863Seric printf("openmailer:\n"); 6093233Seric printav(pvp); 610294Seric } 6113233Seric # endif DEBUG 6124488Seric errno = 0; 6133233Seric 6146038Seric # ifdef DAEMON 6156038Seric /* 6166038Seric ** Deal with the special case of mail handled through an IPC 6176038Seric ** connection. 6186038Seric ** In this case we don't actually fork. We must be 6196038Seric ** running SMTP for this to work. We will return a 6206038Seric ** zero pid to indicate that we are running IPC. 6216038Seric */ 6226038Seric 6236038Seric if (strcmp(m->m_mailer, "[IPC]") == 0) 6246038Seric { 6256038Seric register int i; 6267285Seric register u_short port; 6276038Seric 6286038Seric if (!clever) 6296038Seric syserr("non-clever IPC"); 6306632Seric if (pvp[2] != NULL) 6317285Seric port = atoi(pvp[2]); 6326632Seric else 6337285Seric port = 0; 6347285Seric i = makeconnection(pvp[1], port, pmfile, prfile); 6356038Seric if (i != EX_OK) 6366047Seric { 6376047Seric ExitStat = i; 6386038Seric return (-1); 6396047Seric } 6406038Seric else 6416038Seric return (0); 6426038Seric } 6436038Seric # endif DAEMON 6446038Seric 6452898Seric /* create a pipe to shove the mail through */ 6464709Seric if (pipe(mpvect) < 0) 647294Seric { 6484863Seric syserr("pipe (to mailer)"); 649294Seric return (-1); 650294Seric } 6514863Seric 6525179Seric # ifdef SMTP 6534863Seric /* if this mailer speaks smtp, create a return pipe */ 6544863Seric if (clever && pipe(rpvect) < 0) 6554863Seric { 6564863Seric syserr("pipe (from mailer)"); 6574863Seric (void) close(mpvect[0]); 6584863Seric (void) close(mpvect[1]); 6594863Seric return (-1); 6604863Seric } 6615179Seric # endif SMTP 6624863Seric 6636038Seric /* 6646038Seric ** Actually fork the mailer process. 6656038Seric ** DOFORK is clever about retrying. 6666038Seric */ 6676038Seric 6687672Seric (void) fflush(Xscript); /* for debugging */ 6694214Seric DOFORK(XFORK); 6704327Seric /* pid is set by DOFORK */ 671294Seric if (pid < 0) 672294Seric { 6736038Seric /* failure */ 674294Seric syserr("Cannot fork"); 6754709Seric (void) close(mpvect[0]); 6764709Seric (void) close(mpvect[1]); 6774863Seric if (clever) 6784863Seric { 6794863Seric (void) close(rpvect[0]); 6804863Seric (void) close(rpvect[1]); 6814863Seric } 682294Seric return (-1); 683294Seric } 684294Seric else if (pid == 0) 685294Seric { 686294Seric /* child -- set up input & exec mailer */ 6871621Seric /* make diagnostic output be standard output */ 6884477Seric (void) signal(SIGINT, SIG_IGN); 6894477Seric (void) signal(SIGHUP, SIG_IGN); 6904215Seric (void) signal(SIGTERM, SIG_DFL); 6914709Seric 6924709Seric /* arrange to filter standard & diag output of command */ 6934863Seric if (clever) 6944709Seric { 6954863Seric (void) close(rpvect[0]); 6964709Seric (void) close(1); 6974863Seric (void) dup(rpvect[1]); 6984863Seric (void) close(rpvect[1]); 6994863Seric } 7004863Seric else if (OutChannel != stdout) 7014863Seric { 7024863Seric (void) close(1); 7034709Seric (void) dup(fileno(OutChannel)); 7044709Seric } 7054082Seric (void) close(2); 7064082Seric (void) dup(1); 7074709Seric 7084709Seric /* arrange to get standard input */ 7094709Seric (void) close(mpvect[1]); 7104082Seric (void) close(0); 7114709Seric if (dup(mpvect[0]) < 0) 712294Seric { 7132898Seric syserr("Cannot dup to zero!"); 7142898Seric _exit(EX_OSERR); 715294Seric } 7164709Seric (void) close(mpvect[0]); 7172968Seric if (!bitset(M_RESTR, m->m_flags)) 7184215Seric { 7194417Seric if (ctladdr->q_uid == 0) 7204417Seric { 7214417Seric (void) setgid(DefGid); 7224417Seric (void) setuid(DefUid); 7234417Seric } 7244417Seric else 7254415Seric { 7264417Seric (void) setgid(ctladdr->q_gid); 7274417Seric (void) setuid(ctladdr->q_uid); 7284415Seric } 7294215Seric } 7302774Seric # ifndef VFORK 7312774Seric /* 7322774Seric ** We have to be careful with vfork - we can't mung up the 7332774Seric ** memory but we don't want the mailer to inherit any extra 7342774Seric ** open files. Chances are the mailer won't 7352774Seric ** care about an extra file, but then again you never know. 7362774Seric ** Actually, we would like to close(fileno(pwf)), but it's 7372774Seric ** declared static so we can't. But if we fclose(pwf), which 7382774Seric ** is what endpwent does, it closes it in the parent too and 7392774Seric ** the next getpwnam will be slower. If you have a weird 7402774Seric ** mailer that chokes on the extra file you should do the 7412774Seric ** endpwent(). 7422774Seric ** 7432774Seric ** Similar comments apply to log. However, openlog is 7442774Seric ** clever enough to set the FIOCLEX mode on the file, 7452774Seric ** so it will be closed automatically on the exec. 7462774Seric */ 7472774Seric 7482774Seric endpwent(); 749294Seric # ifdef LOG 7502089Seric closelog(); 751294Seric # endif LOG 7522774Seric # endif VFORK 7536038Seric 7546038Seric /* try to execute the mailer */ 755294Seric execv(m->m_mailer, pvp); 7566038Seric 757294Seric /* syserr fails because log is closed */ 758294Seric /* syserr("Cannot exec %s", m->m_mailer); */ 7594214Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 7604082Seric (void) fflush(stdout); 7611619Seric _exit(EX_UNAVAILABLE); 762294Seric } 763294Seric 7644709Seric /* 7654863Seric ** Set up return value. 7664709Seric */ 7674709Seric 7684709Seric (void) close(mpvect[0]); 7694709Seric mfile = fdopen(mpvect[1], "w"); 7704863Seric if (clever) 7714863Seric { 7724863Seric (void) close(rpvect[1]); 7734863Seric rfile = fdopen(rpvect[0], "r"); 7744863Seric } 775294Seric 7764863Seric *pmfile = mfile; 7774863Seric *prfile = rfile; 778294Seric 7794863Seric return (pid); 780294Seric } 781294Seric /* 782294Seric ** GIVERESPONSE -- Interpret an error response from a mailer 783294Seric ** 784294Seric ** Parameters: 785294Seric ** stat -- the status code from the mailer (high byte 786294Seric ** only; core dumps must have been taken care of 787294Seric ** already). 788294Seric ** force -- if set, force an error message output, even 789294Seric ** if the mailer seems to like to print its own 790294Seric ** messages. 791294Seric ** m -- the mailer descriptor for this mailer. 792294Seric ** 793294Seric ** Returns: 7944082Seric ** none. 795294Seric ** 796294Seric ** Side Effects: 7971518Seric ** Errors may be incremented. 798294Seric ** ExitStat may be set. 799294Seric */ 800294Seric 801294Seric giveresponse(stat, force, m) 802294Seric int stat; 8037228Seric bool force; 804294Seric register struct mailer *m; 805294Seric { 806294Seric register char *statmsg; 807294Seric extern char *SysExMsg[]; 808294Seric register int i; 809294Seric extern int N_SysEx; 8101624Seric char buf[30]; 811294Seric 8124315Seric /* 8134315Seric ** Compute status message from code. 8144315Seric */ 8154315Seric 816294Seric i = stat - EX__BASE; 817294Seric if (i < 0 || i > N_SysEx) 818294Seric statmsg = NULL; 819294Seric else 820294Seric statmsg = SysExMsg[i]; 821294Seric if (stat == 0) 8224065Seric { 8238003Seric statmsg = "250 sent"; 8248003Seric message(Arpa_Info, &statmsg[4]); 8254065Seric } 8264621Seric else if (stat == EX_TEMPFAIL) 8274621Seric { 8287875Seric message(Arpa_Info, "deferred"); 8294621Seric } 830294Seric else 831294Seric { 8321518Seric Errors++; 8336047Seric FatalErrors = TRUE; 834294Seric if (statmsg == NULL && m->m_badstat != 0) 835294Seric { 836294Seric stat = m->m_badstat; 837294Seric i = stat - EX__BASE; 838294Seric # ifdef DEBUG 839294Seric if (i < 0 || i >= N_SysEx) 840294Seric syserr("Bad m_badstat %d", stat); 841294Seric else 842294Seric # endif DEBUG 8437344Seric statmsg = SysExMsg[i]; 844294Seric } 845294Seric if (statmsg == NULL) 846294Seric usrerr("unknown mailer response %d", stat); 8474065Seric else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 8488003Seric usrerr(statmsg); 8497344Seric else 8508003Seric fprintf(Xscript, "%s\n", &statmsg[4]); 851294Seric } 852294Seric 853294Seric /* 854294Seric ** Final cleanup. 855294Seric ** Log a record of the transaction. Compute the new 856294Seric ** ExitStat -- if we already had an error, stick with 857294Seric ** that. 858294Seric */ 859294Seric 8601624Seric if (statmsg == NULL) 8611624Seric { 8628003Seric (void) sprintf(buf, "554 error %d", stat); 8631624Seric statmsg = buf; 8641624Seric } 8651624Seric 866294Seric # ifdef LOG 8677680Seric if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2)) 8687858Seric { 8697858Seric extern char *pintvl(); 8707858Seric 8717858Seric syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id, 8727883Seric CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), 8738003Seric &statmsg[4]); 8747858Seric } 875294Seric # endif LOG 8764621Seric if (stat != EX_TEMPFAIL) 8774621Seric setstat(stat); 878294Seric } 879294Seric /* 8806974Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 881294Seric ** 8826974Seric ** This can be made an arbitrary message separator by changing $l 883294Seric ** 8846974Seric ** One of the ugliest hacks seen by human eyes is 8856974Seric ** contained herein: UUCP wants those stupid 8866974Seric ** "remote from <host>" lines. Why oh why does a 8876974Seric ** well-meaning programmer such as myself have to 8886974Seric ** deal with this kind of antique garbage???? 8896974Seric ** 890294Seric ** Parameters: 8916974Seric ** fp -- the file to output to. 8926974Seric ** m -- the mailer describing this entry. 893294Seric ** 894294Seric ** Returns: 8956974Seric ** none 896294Seric ** 897294Seric ** Side Effects: 8986974Seric ** outputs some text to fp. 899294Seric */ 900294Seric 9016974Seric putfromline(fp, m) 9026974Seric register FILE *fp; 9036974Seric register MAILER *m; 904294Seric { 9056974Seric char buf[MAXLINE]; 906294Seric 9076974Seric if (bitset(M_NHDR, m->m_flags)) 9086974Seric return; 9094315Seric 9106974Seric # ifdef UGLYUUCP 9116974Seric if (bitset(M_UGLYUUCP, m->m_flags)) 9124205Seric { 9136974Seric extern char *macvalue(); 9146974Seric char *sys = macvalue('g'); 9156974Seric char *bang = index(sys, '!'); 9166041Seric 9176974Seric if (bang == NULL) 9186974Seric syserr("No ! in UUCP! (%s)", sys); 9195099Seric else 9206974Seric *bang = '\0'; 9217232Seric expand("From $f $d remote from $g\n", buf, 9226974Seric &buf[sizeof buf - 1], CurEnv); 9236974Seric *bang = '!'; 9246974Seric } 9256974Seric else 9265179Seric # endif UGLYUUCP 9276974Seric expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv); 9287123Seric putline(buf, fp, bitset(M_FULLSMTP, m->m_flags)); 9295981Seric } 9305981Seric /* 9316974Seric ** PUTHEADER -- put the header part of a message from the in-core copy 9325981Seric ** 9335981Seric ** Parameters: 9345981Seric ** fp -- file to put it on. 9355981Seric ** m -- mailer to use. 9366974Seric ** e -- envelope to use. 9375981Seric ** 9385981Seric ** Returns: 9395981Seric ** none. 9405981Seric ** 9415981Seric ** Side Effects: 9425981Seric ** none. 9435981Seric */ 9445981Seric 9456974Seric putheader(fp, m, e) 9465981Seric register FILE *fp; 9475981Seric register struct mailer *m; 9486974Seric register ENVELOPE *e; 9495981Seric { 9505981Seric char buf[BUFSIZ]; 9515981Seric register HDR *h; 9525981Seric extern char *arpadate(); 9535981Seric extern char *capitalize(); 9545981Seric extern char *hvalue(); 9555981Seric extern bool samefrom(); 9567123Seric char obuf[MAXLINE]; 9577123Seric register char *obp; 9587123Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 9595981Seric 9606974Seric for (h = e->e_header; h != NULL; h = h->h_link) 9611828Seric { 9624315Seric register char *p; 9634315Seric 9643389Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 9658062Seric continue; 9664447Seric 9677756Seric p = h->h_value; 9688062Seric if (bitset(H_DEFAULT, h->h_flags)) 9693385Seric { 9707666Seric /* macro expand value if generated internally */ 9718062Seric expand(p, buf, &buf[sizeof buf], e); 9723385Seric p = buf; 9733385Seric } 9744447Seric if (p == NULL || *p == '\0') 9753389Seric continue; 9764209Seric 9778062Seric if (bitset(H_FROM|H_RCPT, h->h_flags)) 9784209Seric { 9798062Seric /* address field */ 980*8093Seric bool oldstyle = e->e_oldstyle; 981*8093Seric 982*8093Seric if (bitset(H_FROM, h->h_flags)) 983*8093Seric oldstyle = FALSE; 984*8093Seric commaize(h, p, fp, oldstyle, m); 9854447Seric } 9868062Seric else 9874447Seric { 9888062Seric /* vanilla header line */ 9897123Seric (void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p); 9907123Seric putline(obuf, fp, fullsmtp); 9914209Seric } 9928062Seric h->h_flags |= H_USED; 9932898Seric } 994294Seric } 995294Seric /* 9967756Seric ** COMMAIZE -- output a header field, making a comma-translated list. 9977756Seric ** 9987756Seric ** Parameters: 9998062Seric ** h -- the header field to output. 10008062Seric ** p -- the value to put in it. 10017756Seric ** fp -- file to put it to. 10027756Seric ** oldstyle -- TRUE if this is an old style header. 10037756Seric ** m -- a pointer to the mailer descriptor. 10047756Seric ** 10057756Seric ** Returns: 10067756Seric ** none. 10077756Seric ** 10087756Seric ** Side Effects: 10097756Seric ** outputs "p" to file "fp". 10107756Seric */ 10117756Seric 10128062Seric commaize(h, p, fp, oldstyle, m) 10138062Seric register HDR *h; 10147756Seric register char *p; 10157756Seric FILE *fp; 10167756Seric bool oldstyle; 10178062Seric register MAILER *m; 10187756Seric { 10198062Seric register char *obp; 10208062Seric int opos; 10218062Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 10227756Seric bool firstone = TRUE; 10237756Seric char obuf[MAXLINE]; 10247756Seric 10257756Seric /* 10267756Seric ** Output the address list translated by the 10277756Seric ** mailer and with commas. 10287756Seric */ 10297756Seric 10307756Seric # ifdef DEBUG 10317756Seric if (tTd(14, 2)) 10328062Seric printf("commaize(%s: %s)\n", h->h_field, p); 10337756Seric # endif DEBUG 10347756Seric 10357756Seric obp = obuf; 10368062Seric (void) sprintf(obp, "%s: ", capitalize(h->h_field)); 10378062Seric opos = strlen(h->h_field) + 2; 10387756Seric obp += opos; 10397756Seric 10407756Seric /* 10417756Seric ** Run through the list of values. 10427756Seric */ 10437756Seric 10447756Seric while (*p != '\0') 10457756Seric { 10467756Seric register char *name; 10477756Seric char savechar; 10488062Seric extern char *remotename(); 10498077Seric extern char *DelimChar; /* defined in prescan */ 10507756Seric 10517756Seric /* 10527756Seric ** Find the end of the name. New style names 10537756Seric ** end with a comma, old style names end with 10547756Seric ** a space character. However, spaces do not 10557756Seric ** necessarily delimit an old-style name -- at 10567756Seric ** signs mean keep going. 10577756Seric */ 10587756Seric 10598077Seric /* find end of name */ 10608077Seric while (isspace(*p) || *p == ',') 10617756Seric p++; 10627756Seric name = p; 10638077Seric for (;;) 10647756Seric { 10658077Seric char *oldp; 10667756Seric extern bool isatword(); 10677756Seric 10688091Seric (void) prescan(p, oldstyle ? ' ' : ','); 10698077Seric p = DelimChar; 10707756Seric 10717756Seric /* look to see if we have an at sign */ 10727756Seric oldp = p; 10737756Seric while (*p != '\0' && isspace(*p)) 10747756Seric p++; 10757756Seric 10767756Seric if (*p != '@' && !isatword(p)) 10777756Seric { 10787756Seric p = oldp; 10797756Seric break; 10807756Seric } 10817756Seric p += *p == '@' ? 1 : 2; 10827756Seric while (*p != '\0' && isspace(*p)) 10837756Seric p++; 10847756Seric } 10857756Seric /* at the end of one complete name */ 10867756Seric 10877756Seric /* strip off trailing white space */ 10887756Seric while (p >= name && (isspace(*p) || *p == ',' || *p == '\0')) 10897756Seric p--; 10907756Seric if (++p == name) 10917756Seric continue; 10927756Seric savechar = *p; 10937756Seric *p = '\0'; 10947756Seric 10957756Seric /* translate the name to be relative */ 10968062Seric name = remotename(name, m, bitset(H_FROM, h->h_flags)); 10977756Seric if (*name == '\0') 10987756Seric { 10997756Seric *p = savechar; 11007756Seric continue; 11017756Seric } 11027756Seric 11037756Seric /* output the name with nice formatting */ 11047756Seric opos += strlen(name); 11057756Seric if (!firstone) 11067756Seric opos += 2; 11077756Seric if (opos > 78 && !firstone) 11087756Seric { 11097756Seric (void) sprintf(obp, ",\n"); 11107756Seric putline(obuf, fp, fullsmtp); 11117756Seric obp = obuf; 11127756Seric (void) sprintf(obp, " "); 11137756Seric obp += strlen(obp); 11147756Seric opos = 8 + strlen(name); 11157756Seric } 11167756Seric else if (!firstone) 11177756Seric { 11187756Seric (void) sprintf(obp, ", "); 11197756Seric obp += 2; 11207756Seric } 11217756Seric (void) sprintf(obp, "%s", name); 11227756Seric obp += strlen(obp); 11237756Seric firstone = FALSE; 11247756Seric *p = savechar; 11257756Seric } 11267756Seric (void) strcpy(obp, "\n"); 11277756Seric putline(obuf, fp, fullsmtp); 11287756Seric } 11297756Seric /* 11306974Seric ** PUTBODY -- put the body of a message. 11316974Seric ** 11326974Seric ** Parameters: 11336974Seric ** fp -- file to output onto. 11346974Seric ** m -- a mailer descriptor. 11356974Seric ** xdot -- if set, use SMTP hidden dot algorithm. 11366974Seric ** 11376974Seric ** Returns: 11386974Seric ** none. 11396974Seric ** 11406974Seric ** Side Effects: 11416974Seric ** The message is written onto fp. 11426974Seric */ 11436974Seric 11446974Seric putbody(fp, m, xdot) 11456974Seric FILE *fp; 11466974Seric struct mailer *m; 11476974Seric bool xdot; 11486974Seric { 11496974Seric char buf[MAXLINE + 1]; 11507123Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 11516974Seric 11526974Seric /* 11536974Seric ** Output the body of the message 11546974Seric */ 11556974Seric 11567003Seric #ifdef lint 11577003Seric /* m will be needed later for complete smtp emulation */ 11587003Seric if (m == NULL) 11597003Seric return; 11607003Seric #endif lint 11617003Seric 11626974Seric if (TempFile != NULL) 11636974Seric { 11646974Seric rewind(TempFile); 11656974Seric buf[0] = '.'; 11666974Seric while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL) 11677123Seric putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp); 11686974Seric 11696974Seric if (ferror(TempFile)) 11706974Seric { 11716974Seric syserr("putbody: read error"); 11726974Seric ExitStat = EX_IOERR; 11736974Seric } 11746974Seric } 11756974Seric 11766974Seric (void) fflush(fp); 11776974Seric if (ferror(fp) && errno != EPIPE) 11786974Seric { 11796974Seric syserr("putbody: write error"); 11806974Seric ExitStat = EX_IOERR; 11816974Seric } 11826974Seric errno = 0; 11836974Seric } 11846974Seric /* 11855936Seric ** ISATWORD -- tell if the word we are pointing to is "at". 11865936Seric ** 11875936Seric ** Parameters: 11885936Seric ** p -- word to check. 11895936Seric ** 11905936Seric ** Returns: 11915936Seric ** TRUE -- if p is the word at. 11925936Seric ** FALSE -- otherwise. 11935936Seric ** 11945936Seric ** Side Effects: 11955936Seric ** none. 11965936Seric */ 11975936Seric 11985936Seric bool 11995936Seric isatword(p) 12005936Seric register char *p; 12015936Seric { 12025936Seric extern char lower(); 12035936Seric 12045936Seric if (lower(p[0]) == 'a' && lower(p[1]) == 't' && 12055936Seric p[2] != '\0' && isspace(p[2])) 12065936Seric return (TRUE); 12075936Seric return (FALSE); 12085936Seric } 12095936Seric /* 12104370Seric ** SAMEFROM -- tell if two text addresses represent the same from address. 12114370Seric ** 12124370Seric ** Parameters: 12134370Seric ** ifrom -- internally generated form of from address. 12144370Seric ** efrom -- external form of from address. 12154370Seric ** 12164370Seric ** Returns: 12174370Seric ** TRUE -- if they convey the same info. 12184370Seric ** FALSE -- if any information has been lost. 12194370Seric ** 12204370Seric ** Side Effects: 12214370Seric ** none. 12224370Seric */ 12234370Seric 12244370Seric bool 12254370Seric samefrom(ifrom, efrom) 12264370Seric char *ifrom; 12274370Seric char *efrom; 12284370Seric { 12294447Seric register char *p; 12304447Seric char buf[MAXNAME + 4]; 12314447Seric 12324447Seric # ifdef DEBUG 12337672Seric if (tTd(3, 8)) 12344447Seric printf("samefrom(%s,%s)-->", ifrom, efrom); 12354447Seric # endif DEBUG 12364447Seric if (strcmp(ifrom, efrom) == 0) 12374447Seric goto success; 12384447Seric p = index(ifrom, '@'); 12394447Seric if (p == NULL) 12404447Seric goto failure; 12414447Seric *p = '\0'; 12427003Seric (void) strcpy(buf, ifrom); 12437003Seric (void) strcat(buf, " at "); 12444447Seric *p++ = '@'; 12457003Seric (void) strcat(buf, p); 12464447Seric if (strcmp(buf, efrom) == 0) 12474447Seric goto success; 12484447Seric 12494447Seric failure: 12504447Seric # ifdef DEBUG 12517672Seric if (tTd(3, 8)) 12524447Seric printf("FALSE\n"); 12534447Seric # endif DEBUG 12544447Seric return (FALSE); 12554447Seric 12564447Seric success: 12574447Seric # ifdef DEBUG 12587672Seric if (tTd(3, 8)) 12594447Seric printf("TRUE\n"); 12604447Seric # endif DEBUG 12614447Seric return (TRUE); 12624370Seric } 12634370Seric /* 1264294Seric ** MAILFILE -- Send a message to a file. 1265294Seric ** 12664327Seric ** If the file has the setuid/setgid bits set, but NO execute 12674327Seric ** bits, sendmail will try to become the owner of that file 12684327Seric ** rather than the real user. Obviously, this only works if 12694327Seric ** sendmail runs as root. 12704327Seric ** 1271294Seric ** Parameters: 1272294Seric ** filename -- the name of the file to send to. 12734397Seric ** ctladdr -- the controlling address header -- includes 12744397Seric ** the userid/groupid to be when sending. 1275294Seric ** 1276294Seric ** Returns: 1277294Seric ** The exit code associated with the operation. 1278294Seric ** 1279294Seric ** Side Effects: 1280294Seric ** none. 1281294Seric */ 1282294Seric 12834397Seric mailfile(filename, ctladdr) 1284294Seric char *filename; 12854397Seric ADDRESS *ctladdr; 1286294Seric { 1287294Seric register FILE *f; 12884214Seric register int pid; 1289294Seric 12904214Seric /* 12914214Seric ** Fork so we can change permissions here. 12924214Seric ** Note that we MUST use fork, not vfork, because of 12934214Seric ** the complications of calling subroutines, etc. 12944214Seric */ 12954067Seric 12964214Seric DOFORK(fork); 12974214Seric 12984214Seric if (pid < 0) 12994214Seric return (EX_OSERR); 13004214Seric else if (pid == 0) 13014214Seric { 13024214Seric /* child -- actually write to file */ 13034327Seric struct stat stb; 13044327Seric 13054215Seric (void) signal(SIGINT, SIG_DFL); 13064215Seric (void) signal(SIGHUP, SIG_DFL); 13074215Seric (void) signal(SIGTERM, SIG_DFL); 13084327Seric umask(OldUmask); 13094327Seric if (stat(filename, &stb) < 0) 13104431Seric stb.st_mode = 0666; 13114327Seric if (bitset(0111, stb.st_mode)) 13124327Seric exit(EX_CANTCREAT); 13134401Seric if (ctladdr == NULL) 13146900Seric ctladdr = &CurEnv->e_from; 13154327Seric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 13164417Seric { 13174417Seric if (ctladdr->q_uid == 0) 13184417Seric (void) setgid(DefGid); 13194417Seric else 13204417Seric (void) setgid(ctladdr->q_gid); 13214417Seric } 13224327Seric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 13234417Seric { 13244417Seric if (ctladdr->q_uid == 0) 13254417Seric (void) setuid(DefUid); 13264417Seric else 13274417Seric (void) setuid(ctladdr->q_uid); 13284417Seric } 13296887Seric f = dfopen(filename, "a"); 13304214Seric if (f == NULL) 13314214Seric exit(EX_CANTCREAT); 13324214Seric 13336974Seric putfromline(f, Mailer[1]); 13346974Seric (*CurEnv->e_puthdr)(f, Mailer[1], CurEnv); 13357123Seric fputs("\n", f); 13366974Seric (*CurEnv->e_putbody)(f, Mailer[1], FALSE); 13374214Seric fputs("\n", f); 13384214Seric (void) fclose(f); 13394214Seric (void) fflush(stdout); 13404417Seric 13416887Seric /* reset ISUID & ISGID bits for paranoid systems */ 13424621Seric (void) chmod(filename, (int) stb.st_mode); 13434214Seric exit(EX_OK); 13444315Seric /*NOTREACHED*/ 13454214Seric } 13464214Seric else 13474214Seric { 13484214Seric /* parent -- wait for exit status */ 13494214Seric register int i; 13504214Seric auto int stat; 13514214Seric 13524214Seric while ((i = wait(&stat)) != pid) 13534214Seric { 13544214Seric if (i < 0) 13554214Seric { 13564214Seric stat = EX_OSERR << 8; 13574214Seric break; 13584214Seric } 13594214Seric } 13604215Seric if ((stat & 0377) != 0) 13614215Seric stat = EX_UNAVAILABLE << 8; 13624214Seric return ((stat >> 8) & 0377); 13634214Seric } 1364294Seric } 13654550Seric /* 13664550Seric ** SENDALL -- actually send all the messages. 13674550Seric ** 13684550Seric ** Parameters: 13697043Seric ** e -- the envelope to send. 13704550Seric ** verifyonly -- if set, only give verification messages. 13714550Seric ** 13724550Seric ** Returns: 13734550Seric ** none. 13744550Seric ** 13754550Seric ** Side Effects: 13764550Seric ** Scans the send lists and sends everything it finds. 13777043Seric ** Delivers any appropriate error messages. 13784550Seric */ 13794550Seric 13807043Seric sendall(e, verifyonly) 13817043Seric ENVELOPE *e; 13824550Seric bool verifyonly; 13834550Seric { 13845008Seric register ADDRESS *q; 13857779Seric bool oldverbose; 13864550Seric 13875032Seric # ifdef DEBUG 13887672Seric if (tTd(13, 2)) 13895032Seric { 13906900Seric printf("\nSend Queue:\n"); 13917043Seric printaddr(e->e_sendqueue, TRUE); 13925032Seric } 13935032Seric # endif DEBUG 13945008Seric 13957043Seric /* 13967043Seric ** Run through the list and send everything. 13977043Seric */ 13987043Seric 13997779Seric oldverbose = Verbose; 14007779Seric if (verifyonly) 14017779Seric Verbose = TRUE; 14027043Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14034550Seric { 14045008Seric if (verifyonly) 14054550Seric { 14066900Seric CurEnv->e_to = q->q_paddr; 14075008Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 14087052Seric message(Arpa_Info, "deliverable"); 14094550Seric } 14105008Seric else 14116974Seric (void) deliver(q); 14124550Seric } 14137779Seric Verbose = oldverbose; 14147043Seric 14157043Seric /* 14167043Seric ** Now run through and check for errors. 14177043Seric */ 14187043Seric 14197043Seric if (verifyonly) 14207043Seric return; 14217043Seric 14227043Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14237043Seric { 14247043Seric register ADDRESS *qq; 14257043Seric 14267043Seric if (bitset(QQUEUEUP, q->q_flags)) 14277043Seric e->e_queueup = TRUE; 14287043Seric if (!bitset(QBADADDR, q->q_flags)) 14297043Seric continue; 14307043Seric 14317043Seric /* we have an address that failed -- find the parent */ 14327043Seric for (qq = q; qq != NULL; qq = qq->q_alias) 14337043Seric { 14347043Seric char obuf[MAXNAME + 6]; 14357043Seric extern char *aliaslookup(); 14367043Seric 14377043Seric /* we can only have owners for local addresses */ 14387043Seric if (!bitset(M_LOCAL, qq->q_mailer->m_flags)) 14397043Seric continue; 14407043Seric 14417043Seric /* see if the owner list exists */ 14427043Seric (void) strcpy(obuf, "owner-"); 14437047Seric if (strncmp(qq->q_user, "owner-", 6) == 0) 14447047Seric (void) strcat(obuf, "owner"); 14457047Seric else 14467047Seric (void) strcat(obuf, qq->q_user); 14477043Seric if (aliaslookup(obuf) == NULL) 14487043Seric continue; 14497043Seric 14507043Seric /* owner list exists -- add it to the error queue */ 14517043Seric qq->q_flags &= ~QPRIMARY; 14528077Seric sendto(obuf, qq, &e->e_errorqueue); 14537043Seric MailBack = TRUE; 14547043Seric break; 14557043Seric } 14567043Seric 14577043Seric /* if we did not find an owner, send to the sender */ 14587043Seric if (qq == NULL) 14598077Seric sendto(e->e_from.q_paddr, qq, &e->e_errorqueue); 14607043Seric } 14614550Seric } 14627043Seric /* 14637043Seric ** CHECKERRORS -- check a queue of addresses and process errors. 14647043Seric ** 14657043Seric ** Parameters: 14667043Seric ** e -- the envelope to check. 14677043Seric ** 14687043Seric ** Returns: 14697043Seric ** none. 14707043Seric ** 14717043Seric ** Side Effects: 14727043Seric ** Arranges to queue all tempfailed messages in q 14737043Seric ** or deliver error responses. 14747043Seric */ 14757043Seric 14767043Seric checkerrors(e) 14777043Seric register ENVELOPE *e; 14787043Seric { 14797808Seric register ADDRESS *q; 14807808Seric 14817043Seric # ifdef DEBUG 14827672Seric if (tTd(4, 1)) 14837043Seric { 14847371Seric printf("\ncheckerrors: FatalErrors %d, errorqueue:\n"); 14857043Seric printaddr(e->e_errorqueue, TRUE); 14867043Seric } 14877043Seric # endif DEBUG 14887043Seric 14897043Seric /* mail back the transcript on errors */ 14907043Seric if (FatalErrors) 14917043Seric savemail(); 14927043Seric 14937043Seric /* queue up anything laying around */ 14947858Seric if (e->e_dontqueue) 14957858Seric return; 14967808Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14977043Seric { 14987808Seric if (bitset(QQUEUEUP, q->q_flags)) 14997808Seric { 15007043Seric # ifdef QUEUE 15017808Seric queueup(e, FALSE); 15027043Seric # else QUEUE 15037808Seric syserr("checkerrors: trying to queue %s", e->e_df); 15047043Seric # endif QUEUE 15057808Seric break; 15067808Seric } 15077043Seric } 15087043Seric } 1509