1294Seric # include <signal.h> 24123Seric # include <errno.h> 34621Seric # include "sendmail.h" 44327Seric # include <sys/stat.h> 5294Seric 6*8178Seric SCCSID(@(#)deliver.c 3.113 09/12/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 */ 5508127Seric do 5518127Seric { 5528127Seric errno = 0; 5538127Seric i = wait(&st); 5548133Seric } while ((i >= 0 && i != pid) || errno == EINTR); 5554863Seric if (i < 0) 5564863Seric { 5574863Seric syserr("wait"); 5584863Seric return (-1); 5594863Seric } 5606038Seric 5616038Seric /* see if it died a horrid death */ 5624863Seric if ((st & 0377) != 0) 5634863Seric { 5644863Seric syserr("%s: stat %o", name, st); 5654863Seric ExitStat = EX_UNAVAILABLE; 5664863Seric return (-1); 5674863Seric } 5686038Seric 5696038Seric /* normal death -- return status */ 5704863Seric i = (st >> 8) & 0377; 5714863Seric return (i); 5724863Seric } 5734863Seric /* 5744863Seric ** OPENMAILER -- open connection to mailer. 5754863Seric ** 5764863Seric ** Parameters: 5774863Seric ** m -- mailer descriptor. 5784863Seric ** pvp -- parameter vector to pass to mailer. 5794863Seric ** ctladdr -- controlling address for user. 5804863Seric ** clever -- create a full duplex connection. 5814863Seric ** pmfile -- pointer to mfile (to mailer) connection. 5824863Seric ** prfile -- pointer to rfile (from mailer) connection. 5834863Seric ** 5844863Seric ** Returns: 5856038Seric ** pid of mailer ( > 0 ). 5864863Seric ** -1 on error. 5876038Seric ** zero on an IPC connection. 5884863Seric ** 5894863Seric ** Side Effects: 5904863Seric ** creates a mailer in a subprocess. 5914863Seric */ 5924863Seric 5934863Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 5944863Seric struct mailer *m; 5954863Seric char **pvp; 5964863Seric ADDRESS *ctladdr; 5974863Seric bool clever; 5984863Seric FILE **pmfile; 5994863Seric FILE **prfile; 6004863Seric { 6014863Seric int pid; 6024709Seric int mpvect[2]; 6034863Seric int rpvect[2]; 6043233Seric FILE *mfile; 6054863Seric FILE *rfile; 6063233Seric extern FILE *fdopen(); 6073233Seric 6083233Seric # ifdef DEBUG 6097672Seric if (tTd(11, 1)) 610294Seric { 611*8178Seric printf("openmailer:"); 6123233Seric printav(pvp); 613294Seric } 6143233Seric # endif DEBUG 6154488Seric errno = 0; 6163233Seric 6176038Seric # ifdef DAEMON 6186038Seric /* 6196038Seric ** Deal with the special case of mail handled through an IPC 6206038Seric ** connection. 6216038Seric ** In this case we don't actually fork. We must be 6226038Seric ** running SMTP for this to work. We will return a 6236038Seric ** zero pid to indicate that we are running IPC. 6246038Seric */ 6256038Seric 6266038Seric if (strcmp(m->m_mailer, "[IPC]") == 0) 6276038Seric { 6286038Seric register int i; 6297285Seric register u_short port; 6306038Seric 6316038Seric if (!clever) 6326038Seric syserr("non-clever IPC"); 6336632Seric if (pvp[2] != NULL) 6347285Seric port = atoi(pvp[2]); 6356632Seric else 6367285Seric port = 0; 6377285Seric i = makeconnection(pvp[1], port, pmfile, prfile); 6386038Seric if (i != EX_OK) 6396047Seric { 6406047Seric ExitStat = i; 6416038Seric return (-1); 6426047Seric } 6436038Seric else 6446038Seric return (0); 6456038Seric } 6466038Seric # endif DAEMON 6476038Seric 6482898Seric /* create a pipe to shove the mail through */ 6494709Seric if (pipe(mpvect) < 0) 650294Seric { 6514863Seric syserr("pipe (to mailer)"); 652294Seric return (-1); 653294Seric } 6544863Seric 6555179Seric # ifdef SMTP 6564863Seric /* if this mailer speaks smtp, create a return pipe */ 6574863Seric if (clever && pipe(rpvect) < 0) 6584863Seric { 6594863Seric syserr("pipe (from mailer)"); 6604863Seric (void) close(mpvect[0]); 6614863Seric (void) close(mpvect[1]); 6624863Seric return (-1); 6634863Seric } 6645179Seric # endif SMTP 6654863Seric 6666038Seric /* 6676038Seric ** Actually fork the mailer process. 6686038Seric ** DOFORK is clever about retrying. 6696038Seric */ 6706038Seric 6717672Seric (void) fflush(Xscript); /* for debugging */ 6724214Seric DOFORK(XFORK); 6734327Seric /* pid is set by DOFORK */ 674294Seric if (pid < 0) 675294Seric { 6766038Seric /* failure */ 677294Seric syserr("Cannot fork"); 6784709Seric (void) close(mpvect[0]); 6794709Seric (void) close(mpvect[1]); 6804863Seric if (clever) 6814863Seric { 6824863Seric (void) close(rpvect[0]); 6834863Seric (void) close(rpvect[1]); 6844863Seric } 685294Seric return (-1); 686294Seric } 687294Seric else if (pid == 0) 688294Seric { 689294Seric /* child -- set up input & exec mailer */ 6901621Seric /* make diagnostic output be standard output */ 6914477Seric (void) signal(SIGINT, SIG_IGN); 6924477Seric (void) signal(SIGHUP, SIG_IGN); 6934215Seric (void) signal(SIGTERM, SIG_DFL); 6944709Seric 6954709Seric /* arrange to filter standard & diag output of command */ 6964863Seric if (clever) 6974709Seric { 6984863Seric (void) close(rpvect[0]); 6994709Seric (void) close(1); 7004863Seric (void) dup(rpvect[1]); 7014863Seric (void) close(rpvect[1]); 7024863Seric } 7034863Seric else if (OutChannel != stdout) 7044863Seric { 7054863Seric (void) close(1); 7064709Seric (void) dup(fileno(OutChannel)); 7074709Seric } 7084082Seric (void) close(2); 7094082Seric (void) dup(1); 7104709Seric 7114709Seric /* arrange to get standard input */ 7124709Seric (void) close(mpvect[1]); 7134082Seric (void) close(0); 7144709Seric if (dup(mpvect[0]) < 0) 715294Seric { 7162898Seric syserr("Cannot dup to zero!"); 7172898Seric _exit(EX_OSERR); 718294Seric } 7194709Seric (void) close(mpvect[0]); 7202968Seric if (!bitset(M_RESTR, m->m_flags)) 7214215Seric { 7224417Seric if (ctladdr->q_uid == 0) 7234417Seric { 7244417Seric (void) setgid(DefGid); 7254417Seric (void) setuid(DefUid); 7264417Seric } 7274417Seric else 7284415Seric { 7294417Seric (void) setgid(ctladdr->q_gid); 7304417Seric (void) setuid(ctladdr->q_uid); 7314415Seric } 7324215Seric } 7332774Seric # ifndef VFORK 7342774Seric /* 7352774Seric ** We have to be careful with vfork - we can't mung up the 7362774Seric ** memory but we don't want the mailer to inherit any extra 7372774Seric ** open files. Chances are the mailer won't 7382774Seric ** care about an extra file, but then again you never know. 7392774Seric ** Actually, we would like to close(fileno(pwf)), but it's 7402774Seric ** declared static so we can't. But if we fclose(pwf), which 7412774Seric ** is what endpwent does, it closes it in the parent too and 7422774Seric ** the next getpwnam will be slower. If you have a weird 7432774Seric ** mailer that chokes on the extra file you should do the 7442774Seric ** endpwent(). 7452774Seric ** 7462774Seric ** Similar comments apply to log. However, openlog is 7472774Seric ** clever enough to set the FIOCLEX mode on the file, 7482774Seric ** so it will be closed automatically on the exec. 7492774Seric */ 7502774Seric 7512774Seric endpwent(); 752294Seric # ifdef LOG 7532089Seric closelog(); 754294Seric # endif LOG 7552774Seric # endif VFORK 7566038Seric 7576038Seric /* try to execute the mailer */ 758294Seric execv(m->m_mailer, pvp); 7596038Seric 760294Seric /* syserr fails because log is closed */ 761294Seric /* syserr("Cannot exec %s", m->m_mailer); */ 7624214Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 7634082Seric (void) fflush(stdout); 7641619Seric _exit(EX_UNAVAILABLE); 765294Seric } 766294Seric 7674709Seric /* 7684863Seric ** Set up return value. 7694709Seric */ 7704709Seric 7714709Seric (void) close(mpvect[0]); 7724709Seric mfile = fdopen(mpvect[1], "w"); 7734863Seric if (clever) 7744863Seric { 7754863Seric (void) close(rpvect[1]); 7764863Seric rfile = fdopen(rpvect[0], "r"); 7774863Seric } 778294Seric 7794863Seric *pmfile = mfile; 7804863Seric *prfile = rfile; 781294Seric 7824863Seric return (pid); 783294Seric } 784294Seric /* 785294Seric ** GIVERESPONSE -- Interpret an error response from a mailer 786294Seric ** 787294Seric ** Parameters: 788294Seric ** stat -- the status code from the mailer (high byte 789294Seric ** only; core dumps must have been taken care of 790294Seric ** already). 791294Seric ** force -- if set, force an error message output, even 792294Seric ** if the mailer seems to like to print its own 793294Seric ** messages. 794294Seric ** m -- the mailer descriptor for this mailer. 795294Seric ** 796294Seric ** Returns: 7974082Seric ** none. 798294Seric ** 799294Seric ** Side Effects: 8001518Seric ** Errors may be incremented. 801294Seric ** ExitStat may be set. 802294Seric */ 803294Seric 804294Seric giveresponse(stat, force, m) 805294Seric int stat; 8067228Seric bool force; 807294Seric register struct mailer *m; 808294Seric { 809294Seric register char *statmsg; 810294Seric extern char *SysExMsg[]; 811294Seric register int i; 812294Seric extern int N_SysEx; 8131624Seric char buf[30]; 814294Seric 8154315Seric /* 8164315Seric ** Compute status message from code. 8174315Seric */ 8184315Seric 819294Seric i = stat - EX__BASE; 820294Seric if (i < 0 || i > N_SysEx) 821294Seric statmsg = NULL; 822294Seric else 823294Seric statmsg = SysExMsg[i]; 824294Seric if (stat == 0) 8254065Seric { 8268003Seric statmsg = "250 sent"; 8278003Seric message(Arpa_Info, &statmsg[4]); 8284065Seric } 8294621Seric else if (stat == EX_TEMPFAIL) 8304621Seric { 8317875Seric message(Arpa_Info, "deferred"); 8324621Seric } 833294Seric else 834294Seric { 8351518Seric Errors++; 8366047Seric FatalErrors = TRUE; 837294Seric if (statmsg == NULL && m->m_badstat != 0) 838294Seric { 839294Seric stat = m->m_badstat; 840294Seric i = stat - EX__BASE; 841294Seric # ifdef DEBUG 842294Seric if (i < 0 || i >= N_SysEx) 843294Seric syserr("Bad m_badstat %d", stat); 844294Seric else 845294Seric # endif DEBUG 8467344Seric statmsg = SysExMsg[i]; 847294Seric } 848294Seric if (statmsg == NULL) 849294Seric usrerr("unknown mailer response %d", stat); 8504065Seric else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 8518003Seric usrerr(statmsg); 8527344Seric else 8538003Seric fprintf(Xscript, "%s\n", &statmsg[4]); 854294Seric } 855294Seric 856294Seric /* 857294Seric ** Final cleanup. 858294Seric ** Log a record of the transaction. Compute the new 859294Seric ** ExitStat -- if we already had an error, stick with 860294Seric ** that. 861294Seric */ 862294Seric 8631624Seric if (statmsg == NULL) 8641624Seric { 8658003Seric (void) sprintf(buf, "554 error %d", stat); 8661624Seric statmsg = buf; 8671624Seric } 8681624Seric 869294Seric # ifdef LOG 8707680Seric if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2)) 8717858Seric { 8727858Seric extern char *pintvl(); 8737858Seric 8747858Seric syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id, 8757883Seric CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), 8768003Seric &statmsg[4]); 8777858Seric } 878294Seric # endif LOG 8794621Seric if (stat != EX_TEMPFAIL) 8804621Seric setstat(stat); 881294Seric } 882294Seric /* 8836974Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 884294Seric ** 8856974Seric ** This can be made an arbitrary message separator by changing $l 886294Seric ** 8876974Seric ** One of the ugliest hacks seen by human eyes is 8886974Seric ** contained herein: UUCP wants those stupid 8896974Seric ** "remote from <host>" lines. Why oh why does a 8906974Seric ** well-meaning programmer such as myself have to 8916974Seric ** deal with this kind of antique garbage???? 8926974Seric ** 893294Seric ** Parameters: 8946974Seric ** fp -- the file to output to. 8956974Seric ** m -- the mailer describing this entry. 896294Seric ** 897294Seric ** Returns: 8986974Seric ** none 899294Seric ** 900294Seric ** Side Effects: 9016974Seric ** outputs some text to fp. 902294Seric */ 903294Seric 9046974Seric putfromline(fp, m) 9056974Seric register FILE *fp; 9066974Seric register MAILER *m; 907294Seric { 9086974Seric char buf[MAXLINE]; 909294Seric 9106974Seric if (bitset(M_NHDR, m->m_flags)) 9116974Seric return; 9124315Seric 9136974Seric # ifdef UGLYUUCP 9146974Seric if (bitset(M_UGLYUUCP, m->m_flags)) 9154205Seric { 9166974Seric extern char *macvalue(); 917*8178Seric char *sys = macvalue('g', CurEnv); 9186974Seric char *bang = index(sys, '!'); 9196041Seric 9206974Seric if (bang == NULL) 9216974Seric syserr("No ! in UUCP! (%s)", sys); 9225099Seric else 9236974Seric *bang = '\0'; 9247232Seric expand("From $f $d remote from $g\n", buf, 9256974Seric &buf[sizeof buf - 1], CurEnv); 9266974Seric *bang = '!'; 9276974Seric } 9286974Seric else 9295179Seric # endif UGLYUUCP 9306974Seric expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv); 9317123Seric putline(buf, fp, bitset(M_FULLSMTP, m->m_flags)); 9325981Seric } 9335981Seric /* 9346974Seric ** PUTHEADER -- put the header part of a message from the in-core copy 9355981Seric ** 9365981Seric ** Parameters: 9375981Seric ** fp -- file to put it on. 9385981Seric ** m -- mailer to use. 9396974Seric ** e -- envelope to use. 9405981Seric ** 9415981Seric ** Returns: 9425981Seric ** none. 9435981Seric ** 9445981Seric ** Side Effects: 9455981Seric ** none. 9465981Seric */ 9475981Seric 9486974Seric putheader(fp, m, e) 9495981Seric register FILE *fp; 9505981Seric register struct mailer *m; 9516974Seric register ENVELOPE *e; 9525981Seric { 9535981Seric char buf[BUFSIZ]; 9545981Seric register HDR *h; 9555981Seric extern char *arpadate(); 9565981Seric extern char *capitalize(); 9575981Seric extern bool samefrom(); 9587123Seric char obuf[MAXLINE]; 9597123Seric register char *obp; 9607123Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 9615981Seric 9626974Seric for (h = e->e_header; h != NULL; h = h->h_link) 9631828Seric { 9644315Seric register char *p; 9654315Seric 9663389Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 9678062Seric continue; 9684447Seric 9697756Seric p = h->h_value; 9708062Seric if (bitset(H_DEFAULT, h->h_flags)) 9713385Seric { 9727666Seric /* macro expand value if generated internally */ 9738062Seric expand(p, buf, &buf[sizeof buf], e); 9743385Seric p = buf; 9753385Seric } 9764447Seric if (p == NULL || *p == '\0') 9773389Seric continue; 9784209Seric 9798062Seric if (bitset(H_FROM|H_RCPT, h->h_flags)) 9804209Seric { 9818062Seric /* address field */ 9828093Seric bool oldstyle = e->e_oldstyle; 9838093Seric 9848093Seric if (bitset(H_FROM, h->h_flags)) 9858093Seric oldstyle = FALSE; 9868093Seric commaize(h, p, fp, oldstyle, m); 9874447Seric } 9888062Seric else 9894447Seric { 9908062Seric /* vanilla header line */ 9917123Seric (void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p); 9927123Seric putline(obuf, fp, fullsmtp); 9934209Seric } 9948062Seric h->h_flags |= H_USED; 9952898Seric } 996294Seric } 997294Seric /* 9987756Seric ** COMMAIZE -- output a header field, making a comma-translated list. 9997756Seric ** 10007756Seric ** Parameters: 10018062Seric ** h -- the header field to output. 10028062Seric ** p -- the value to put in it. 10037756Seric ** fp -- file to put it to. 10047756Seric ** oldstyle -- TRUE if this is an old style header. 10057756Seric ** m -- a pointer to the mailer descriptor. 10067756Seric ** 10077756Seric ** Returns: 10087756Seric ** none. 10097756Seric ** 10107756Seric ** Side Effects: 10117756Seric ** outputs "p" to file "fp". 10127756Seric */ 10137756Seric 10148062Seric commaize(h, p, fp, oldstyle, m) 10158062Seric register HDR *h; 10167756Seric register char *p; 10177756Seric FILE *fp; 10187756Seric bool oldstyle; 10198062Seric register MAILER *m; 10207756Seric { 10218062Seric register char *obp; 10228062Seric int opos; 10238062Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 10247756Seric bool firstone = TRUE; 10257756Seric char obuf[MAXLINE]; 10267756Seric 10277756Seric /* 10287756Seric ** Output the address list translated by the 10297756Seric ** mailer and with commas. 10307756Seric */ 10317756Seric 10327756Seric # ifdef DEBUG 10337756Seric if (tTd(14, 2)) 10348062Seric printf("commaize(%s: %s)\n", h->h_field, p); 10357756Seric # endif DEBUG 10367756Seric 10377756Seric obp = obuf; 10388062Seric (void) sprintf(obp, "%s: ", capitalize(h->h_field)); 10398062Seric opos = strlen(h->h_field) + 2; 10407756Seric obp += opos; 10417756Seric 10427756Seric /* 10437756Seric ** Run through the list of values. 10447756Seric */ 10457756Seric 10467756Seric while (*p != '\0') 10477756Seric { 10487756Seric register char *name; 10497756Seric char savechar; 10508062Seric extern char *remotename(); 10518077Seric extern char *DelimChar; /* defined in prescan */ 10527756Seric 10537756Seric /* 10547756Seric ** Find the end of the name. New style names 10557756Seric ** end with a comma, old style names end with 10567756Seric ** a space character. However, spaces do not 10577756Seric ** necessarily delimit an old-style name -- at 10587756Seric ** signs mean keep going. 10597756Seric */ 10607756Seric 10618077Seric /* find end of name */ 10628077Seric while (isspace(*p) || *p == ',') 10637756Seric p++; 10647756Seric name = p; 10658077Seric for (;;) 10667756Seric { 10678077Seric char *oldp; 10687756Seric extern bool isatword(); 10697756Seric 10708091Seric (void) prescan(p, oldstyle ? ' ' : ','); 10718077Seric p = DelimChar; 10727756Seric 10737756Seric /* look to see if we have an at sign */ 10747756Seric oldp = p; 10757756Seric while (*p != '\0' && isspace(*p)) 10767756Seric p++; 10777756Seric 10787756Seric if (*p != '@' && !isatword(p)) 10797756Seric { 10807756Seric p = oldp; 10817756Seric break; 10827756Seric } 10837756Seric p += *p == '@' ? 1 : 2; 10847756Seric while (*p != '\0' && isspace(*p)) 10857756Seric p++; 10867756Seric } 10877756Seric /* at the end of one complete name */ 10887756Seric 10897756Seric /* strip off trailing white space */ 10907756Seric while (p >= name && (isspace(*p) || *p == ',' || *p == '\0')) 10917756Seric p--; 10927756Seric if (++p == name) 10937756Seric continue; 10947756Seric savechar = *p; 10957756Seric *p = '\0'; 10967756Seric 10977756Seric /* translate the name to be relative */ 10988062Seric name = remotename(name, m, bitset(H_FROM, h->h_flags)); 10997756Seric if (*name == '\0') 11007756Seric { 11017756Seric *p = savechar; 11027756Seric continue; 11037756Seric } 11047756Seric 11057756Seric /* output the name with nice formatting */ 11067756Seric opos += strlen(name); 11077756Seric if (!firstone) 11087756Seric opos += 2; 11097756Seric if (opos > 78 && !firstone) 11107756Seric { 11117756Seric (void) sprintf(obp, ",\n"); 11127756Seric putline(obuf, fp, fullsmtp); 11137756Seric obp = obuf; 11147756Seric (void) sprintf(obp, " "); 11157756Seric obp += strlen(obp); 11167756Seric opos = 8 + strlen(name); 11177756Seric } 11187756Seric else if (!firstone) 11197756Seric { 11207756Seric (void) sprintf(obp, ", "); 11217756Seric obp += 2; 11227756Seric } 11237756Seric (void) sprintf(obp, "%s", name); 11247756Seric obp += strlen(obp); 11257756Seric firstone = FALSE; 11267756Seric *p = savechar; 11277756Seric } 11287756Seric (void) strcpy(obp, "\n"); 11297756Seric putline(obuf, fp, fullsmtp); 11307756Seric } 11317756Seric /* 11326974Seric ** PUTBODY -- put the body of a message. 11336974Seric ** 11346974Seric ** Parameters: 11356974Seric ** fp -- file to output onto. 11366974Seric ** m -- a mailer descriptor. 11376974Seric ** xdot -- if set, use SMTP hidden dot algorithm. 11386974Seric ** 11396974Seric ** Returns: 11406974Seric ** none. 11416974Seric ** 11426974Seric ** Side Effects: 11436974Seric ** The message is written onto fp. 11446974Seric */ 11456974Seric 11466974Seric putbody(fp, m, xdot) 11476974Seric FILE *fp; 11486974Seric struct mailer *m; 11496974Seric bool xdot; 11506974Seric { 11516974Seric char buf[MAXLINE + 1]; 11527123Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 11536974Seric 11546974Seric /* 11556974Seric ** Output the body of the message 11566974Seric */ 11576974Seric 11587003Seric #ifdef lint 11597003Seric /* m will be needed later for complete smtp emulation */ 11607003Seric if (m == NULL) 11617003Seric return; 11627003Seric #endif lint 11637003Seric 11646974Seric if (TempFile != NULL) 11656974Seric { 11666974Seric rewind(TempFile); 11676974Seric buf[0] = '.'; 11686974Seric while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL) 11697123Seric putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp); 11706974Seric 11716974Seric if (ferror(TempFile)) 11726974Seric { 11736974Seric syserr("putbody: read error"); 11746974Seric ExitStat = EX_IOERR; 11756974Seric } 11766974Seric } 11776974Seric 11786974Seric (void) fflush(fp); 11796974Seric if (ferror(fp) && errno != EPIPE) 11806974Seric { 11816974Seric syserr("putbody: write error"); 11826974Seric ExitStat = EX_IOERR; 11836974Seric } 11846974Seric errno = 0; 11856974Seric } 11866974Seric /* 11875936Seric ** ISATWORD -- tell if the word we are pointing to is "at". 11885936Seric ** 11895936Seric ** Parameters: 11905936Seric ** p -- word to check. 11915936Seric ** 11925936Seric ** Returns: 11935936Seric ** TRUE -- if p is the word at. 11945936Seric ** FALSE -- otherwise. 11955936Seric ** 11965936Seric ** Side Effects: 11975936Seric ** none. 11985936Seric */ 11995936Seric 12005936Seric bool 12015936Seric isatword(p) 12025936Seric register char *p; 12035936Seric { 12045936Seric extern char lower(); 12055936Seric 12065936Seric if (lower(p[0]) == 'a' && lower(p[1]) == 't' && 12075936Seric p[2] != '\0' && isspace(p[2])) 12085936Seric return (TRUE); 12095936Seric return (FALSE); 12105936Seric } 12115936Seric /* 12124370Seric ** SAMEFROM -- tell if two text addresses represent the same from address. 12134370Seric ** 12144370Seric ** Parameters: 12154370Seric ** ifrom -- internally generated form of from address. 12164370Seric ** efrom -- external form of from address. 12174370Seric ** 12184370Seric ** Returns: 12194370Seric ** TRUE -- if they convey the same info. 12204370Seric ** FALSE -- if any information has been lost. 12214370Seric ** 12224370Seric ** Side Effects: 12234370Seric ** none. 12244370Seric */ 12254370Seric 12264370Seric bool 12274370Seric samefrom(ifrom, efrom) 12284370Seric char *ifrom; 12294370Seric char *efrom; 12304370Seric { 12314447Seric register char *p; 12324447Seric char buf[MAXNAME + 4]; 12334447Seric 12344447Seric # ifdef DEBUG 12357672Seric if (tTd(3, 8)) 12364447Seric printf("samefrom(%s,%s)-->", ifrom, efrom); 12374447Seric # endif DEBUG 12384447Seric if (strcmp(ifrom, efrom) == 0) 12394447Seric goto success; 12404447Seric p = index(ifrom, '@'); 12414447Seric if (p == NULL) 12424447Seric goto failure; 12434447Seric *p = '\0'; 12447003Seric (void) strcpy(buf, ifrom); 12457003Seric (void) strcat(buf, " at "); 12464447Seric *p++ = '@'; 12477003Seric (void) strcat(buf, p); 12484447Seric if (strcmp(buf, efrom) == 0) 12494447Seric goto success; 12504447Seric 12514447Seric failure: 12524447Seric # ifdef DEBUG 12537672Seric if (tTd(3, 8)) 12544447Seric printf("FALSE\n"); 12554447Seric # endif DEBUG 12564447Seric return (FALSE); 12574447Seric 12584447Seric success: 12594447Seric # ifdef DEBUG 12607672Seric if (tTd(3, 8)) 12614447Seric printf("TRUE\n"); 12624447Seric # endif DEBUG 12634447Seric return (TRUE); 12644370Seric } 12654370Seric /* 1266294Seric ** MAILFILE -- Send a message to a file. 1267294Seric ** 12684327Seric ** If the file has the setuid/setgid bits set, but NO execute 12694327Seric ** bits, sendmail will try to become the owner of that file 12704327Seric ** rather than the real user. Obviously, this only works if 12714327Seric ** sendmail runs as root. 12724327Seric ** 1273294Seric ** Parameters: 1274294Seric ** filename -- the name of the file to send to. 12754397Seric ** ctladdr -- the controlling address header -- includes 12764397Seric ** the userid/groupid to be when sending. 1277294Seric ** 1278294Seric ** Returns: 1279294Seric ** The exit code associated with the operation. 1280294Seric ** 1281294Seric ** Side Effects: 1282294Seric ** none. 1283294Seric */ 1284294Seric 12854397Seric mailfile(filename, ctladdr) 1286294Seric char *filename; 12874397Seric ADDRESS *ctladdr; 1288294Seric { 1289294Seric register FILE *f; 12904214Seric register int pid; 1291294Seric 12924214Seric /* 12934214Seric ** Fork so we can change permissions here. 12944214Seric ** Note that we MUST use fork, not vfork, because of 12954214Seric ** the complications of calling subroutines, etc. 12964214Seric */ 12974067Seric 12984214Seric DOFORK(fork); 12994214Seric 13004214Seric if (pid < 0) 13014214Seric return (EX_OSERR); 13024214Seric else if (pid == 0) 13034214Seric { 13044214Seric /* child -- actually write to file */ 13054327Seric struct stat stb; 13064327Seric 13074215Seric (void) signal(SIGINT, SIG_DFL); 13084215Seric (void) signal(SIGHUP, SIG_DFL); 13094215Seric (void) signal(SIGTERM, SIG_DFL); 13104327Seric umask(OldUmask); 13114327Seric if (stat(filename, &stb) < 0) 13124431Seric stb.st_mode = 0666; 13134327Seric if (bitset(0111, stb.st_mode)) 13144327Seric exit(EX_CANTCREAT); 13154401Seric if (ctladdr == NULL) 13166900Seric ctladdr = &CurEnv->e_from; 13174327Seric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 13184417Seric { 13194417Seric if (ctladdr->q_uid == 0) 13204417Seric (void) setgid(DefGid); 13214417Seric else 13224417Seric (void) setgid(ctladdr->q_gid); 13234417Seric } 13244327Seric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 13254417Seric { 13264417Seric if (ctladdr->q_uid == 0) 13274417Seric (void) setuid(DefUid); 13284417Seric else 13294417Seric (void) setuid(ctladdr->q_uid); 13304417Seric } 13316887Seric f = dfopen(filename, "a"); 13324214Seric if (f == NULL) 13334214Seric exit(EX_CANTCREAT); 13344214Seric 13356974Seric putfromline(f, Mailer[1]); 13366974Seric (*CurEnv->e_puthdr)(f, Mailer[1], CurEnv); 13377123Seric fputs("\n", f); 13386974Seric (*CurEnv->e_putbody)(f, Mailer[1], FALSE); 13394214Seric fputs("\n", f); 13404214Seric (void) fclose(f); 13414214Seric (void) fflush(stdout); 13424417Seric 13436887Seric /* reset ISUID & ISGID bits for paranoid systems */ 13444621Seric (void) chmod(filename, (int) stb.st_mode); 13454214Seric exit(EX_OK); 13464315Seric /*NOTREACHED*/ 13474214Seric } 13484214Seric else 13494214Seric { 13504214Seric /* parent -- wait for exit status */ 13514214Seric register int i; 13524214Seric auto int stat; 13534214Seric 13544214Seric while ((i = wait(&stat)) != pid) 13554214Seric { 13564214Seric if (i < 0) 13574214Seric { 13584214Seric stat = EX_OSERR << 8; 13594214Seric break; 13604214Seric } 13614214Seric } 13624215Seric if ((stat & 0377) != 0) 13634215Seric stat = EX_UNAVAILABLE << 8; 13644214Seric return ((stat >> 8) & 0377); 13654214Seric } 1366294Seric } 13674550Seric /* 13684550Seric ** SENDALL -- actually send all the messages. 13694550Seric ** 13704550Seric ** Parameters: 13717043Seric ** e -- the envelope to send. 13724550Seric ** verifyonly -- if set, only give verification messages. 13734550Seric ** 13744550Seric ** Returns: 13754550Seric ** none. 13764550Seric ** 13774550Seric ** Side Effects: 13784550Seric ** Scans the send lists and sends everything it finds. 13797043Seric ** Delivers any appropriate error messages. 13804550Seric */ 13814550Seric 13827043Seric sendall(e, verifyonly) 13837043Seric ENVELOPE *e; 13844550Seric bool verifyonly; 13854550Seric { 13865008Seric register ADDRESS *q; 13877779Seric bool oldverbose; 13884550Seric 13895032Seric # ifdef DEBUG 13907672Seric if (tTd(13, 2)) 13915032Seric { 13926900Seric printf("\nSend Queue:\n"); 13937043Seric printaddr(e->e_sendqueue, TRUE); 13945032Seric } 13955032Seric # endif DEBUG 13965008Seric 13977043Seric /* 13987043Seric ** Run through the list and send everything. 13997043Seric */ 14007043Seric 14017779Seric oldverbose = Verbose; 14027779Seric if (verifyonly) 14037779Seric Verbose = TRUE; 14047043Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14054550Seric { 14065008Seric if (verifyonly) 14074550Seric { 14086900Seric CurEnv->e_to = q->q_paddr; 14095008Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 14107052Seric message(Arpa_Info, "deliverable"); 14114550Seric } 14125008Seric else 14136974Seric (void) deliver(q); 14144550Seric } 14157779Seric Verbose = oldverbose; 14167043Seric 14177043Seric /* 14187043Seric ** Now run through and check for errors. 14197043Seric */ 14207043Seric 14217043Seric if (verifyonly) 14227043Seric return; 14237043Seric 14247043Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14257043Seric { 14267043Seric register ADDRESS *qq; 14277043Seric 14287043Seric if (bitset(QQUEUEUP, q->q_flags)) 14297043Seric e->e_queueup = TRUE; 14307043Seric if (!bitset(QBADADDR, q->q_flags)) 14317043Seric continue; 14327043Seric 14337043Seric /* we have an address that failed -- find the parent */ 14347043Seric for (qq = q; qq != NULL; qq = qq->q_alias) 14357043Seric { 14367043Seric char obuf[MAXNAME + 6]; 14377043Seric extern char *aliaslookup(); 14387043Seric 14397043Seric /* we can only have owners for local addresses */ 14407043Seric if (!bitset(M_LOCAL, qq->q_mailer->m_flags)) 14417043Seric continue; 14427043Seric 14437043Seric /* see if the owner list exists */ 14447043Seric (void) strcpy(obuf, "owner-"); 14457047Seric if (strncmp(qq->q_user, "owner-", 6) == 0) 14467047Seric (void) strcat(obuf, "owner"); 14477047Seric else 14487047Seric (void) strcat(obuf, qq->q_user); 14497043Seric if (aliaslookup(obuf) == NULL) 14507043Seric continue; 14517043Seric 14527043Seric /* owner list exists -- add it to the error queue */ 14537043Seric qq->q_flags &= ~QPRIMARY; 14548077Seric sendto(obuf, qq, &e->e_errorqueue); 14557043Seric MailBack = TRUE; 14567043Seric break; 14577043Seric } 14587043Seric 14597043Seric /* if we did not find an owner, send to the sender */ 14607043Seric if (qq == NULL) 14618077Seric sendto(e->e_from.q_paddr, qq, &e->e_errorqueue); 14627043Seric } 14634550Seric } 14647043Seric /* 14657043Seric ** CHECKERRORS -- check a queue of addresses and process errors. 14667043Seric ** 14677043Seric ** Parameters: 14687043Seric ** e -- the envelope to check. 14697043Seric ** 14707043Seric ** Returns: 14717043Seric ** none. 14727043Seric ** 14737043Seric ** Side Effects: 14747043Seric ** Arranges to queue all tempfailed messages in q 14757043Seric ** or deliver error responses. 14767043Seric */ 14777043Seric 14787043Seric checkerrors(e) 14797043Seric register ENVELOPE *e; 14807043Seric { 14817808Seric register ADDRESS *q; 14827808Seric 14837043Seric # ifdef DEBUG 14847672Seric if (tTd(4, 1)) 14857043Seric { 14867371Seric printf("\ncheckerrors: FatalErrors %d, errorqueue:\n"); 14877043Seric printaddr(e->e_errorqueue, TRUE); 14887043Seric } 14897043Seric # endif DEBUG 14907043Seric 14917043Seric /* mail back the transcript on errors */ 14927043Seric if (FatalErrors) 14937043Seric savemail(); 14947043Seric 14957043Seric /* queue up anything laying around */ 14967858Seric if (e->e_dontqueue) 14977858Seric return; 14987808Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14997043Seric { 15007808Seric if (bitset(QQUEUEUP, q->q_flags)) 15017808Seric { 15027043Seric # ifdef QUEUE 15037808Seric queueup(e, FALSE); 15047043Seric # else QUEUE 15057808Seric syserr("checkerrors: trying to queue %s", e->e_df); 15067043Seric # endif QUEUE 15077808Seric break; 15087808Seric } 15097043Seric } 15107043Seric } 1511