1294Seric # include <signal.h> 24123Seric # include <errno.h> 34621Seric # include "sendmail.h" 44327Seric # include <sys/stat.h> 5294Seric 6*8428Seric SCCSID(@(#)deliver.c 3.119 10/09/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]; 408225Seric char tobuf[MAXLINE-50]; /* 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 75*8428Seric if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags) && 76*8428Seric !Verbose) 775903Seric { 785903Seric for (; to != NULL; to = to->q_next) 797875Seric if (!bitset(QDONTSEND, to->q_flags) && 807875Seric to->q_mailer == firstto->q_mailer) 815903Seric to->q_flags |= QQUEUEUP|QDONTSEND; 825903Seric return (0); 835903Seric } 845903Seric 855903Seric /* 863233Seric ** Do initial argv setup. 873233Seric ** Insert the mailer name. Notice that $x expansion is 883233Seric ** NOT done on the mailer name. Then, if the mailer has 893233Seric ** a picky -f flag, we insert it as appropriate. This 903233Seric ** code does not check for 'pv' overflow; this places a 913233Seric ** manifest lower limit of 4 for MAXPV. 928062Seric ** The from address rewrite is expected to make 938062Seric ** the address relative to the other end. 942968Seric */ 952968Seric 964452Seric /* rewrite from address, using rewriting rules */ 978062Seric expand("$f", buf, &buf[sizeof buf - 1], CurEnv); 984452Seric mvp = prescan(buf, '\0'); 998062Seric rewrite(mvp, m->m_s_rwset); 1004452Seric cataddr(mvp, tfrombuf, sizeof tfrombuf); 1014452Seric 1024452Seric define('g', tfrombuf); /* translated sender address */ 1033233Seric define('h', host); /* to host */ 1043233Seric Errors = 0; 1053233Seric pvp = pv; 1063233Seric *pvp++ = m->m_argv[0]; 1072968Seric 1083233Seric /* insert -f or -r flag as appropriate */ 1093233Seric if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag) 1103233Seric { 1113233Seric if (bitset(M_FOPT, m->m_flags)) 1123233Seric *pvp++ = "-f"; 1133233Seric else 1143233Seric *pvp++ = "-r"; 1156974Seric expand("$g", buf, &buf[sizeof buf - 1], CurEnv); 1163233Seric *pvp++ = newstr(buf); 1173233Seric } 118294Seric 119294Seric /* 1203233Seric ** Append the other fixed parts of the argv. These run 1213233Seric ** up to the first entry containing "$u". There can only 1223233Seric ** be one of these, and there are only a few more slots 1233233Seric ** in the pv after it. 124294Seric */ 125294Seric 1263233Seric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 127294Seric { 1283233Seric while ((p = index(p, '$')) != NULL) 1293233Seric if (*++p == 'u') 1303233Seric break; 1313233Seric if (p != NULL) 1323233Seric break; 1333233Seric 1343233Seric /* this entry is safe -- go ahead and process it */ 1356974Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 1363233Seric *pvp++ = newstr(buf); 1373233Seric if (pvp >= &pv[MAXPV - 3]) 1383233Seric { 1393233Seric syserr("Too many parameters to %s before $u", pv[0]); 1403233Seric return (-1); 1413233Seric } 142294Seric } 1434863Seric 1446038Seric /* 1456038Seric ** If we have no substitution for the user name in the argument 1466038Seric ** list, we know that we must supply the names otherwise -- and 1476038Seric ** SMTP is the answer!! 1486038Seric */ 1496038Seric 1503233Seric if (*mvp == NULL) 1514863Seric { 1524863Seric /* running SMTP */ 1535179Seric # ifdef SMTP 1544863Seric clever = TRUE; 1554863Seric *pvp = NULL; 1565179Seric # else SMTP 1576038Seric /* oops! we don't implement SMTP */ 1585179Seric syserr("SMTP style mailer"); 1595179Seric return (EX_SOFTWARE); 1605179Seric # endif SMTP 1614863Seric } 162294Seric 163294Seric /* 1643233Seric ** At this point *mvp points to the argument with $u. We 1653233Seric ** run through our address list and append all the addresses 1663233Seric ** we can. If we run out of space, do not fret! We can 1673233Seric ** always send another copy later. 168294Seric */ 169294Seric 1703233Seric tobuf[0] = '\0'; 1716900Seric CurEnv->e_to = tobuf; 1724397Seric ctladdr = NULL; 1733233Seric for (; to != NULL; to = to->q_next) 174294Seric { 1753233Seric /* avoid sending multiple recipients to dumb mailers */ 1764382Seric if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags)) 1773233Seric break; 1783233Seric 1793233Seric /* if already sent or not for this host, don't send */ 1807052Seric if (bitset(QDONTSEND, to->q_flags) || 1817052Seric strcmp(to->q_host, host) != 0 || 1827052Seric to->q_mailer != firstto->q_mailer) 1833233Seric continue; 1844397Seric 1858225Seric /* avoid overflowing tobuf */ 1868225Seric if (sizeof tobuf - (strlen(to->q_paddr) + strlen(tobuf) + 1) < 0) 1878225Seric break; 1888225Seric 1895032Seric # ifdef DEBUG 1907672Seric if (tTd(10, 1)) 1915032Seric { 1925032Seric printf("\nsend to "); 1935032Seric printaddr(to, FALSE); 1945032Seric } 1955032Seric # endif DEBUG 1965032Seric 1974397Seric /* compute effective uid/gid when sending */ 1984596Seric if (to->q_mailer == ProgMailer) 1994397Seric ctladdr = getctladdr(to); 2004397Seric 2013233Seric user = to->q_user; 2026900Seric CurEnv->e_to = to->q_paddr; 2033233Seric to->q_flags |= QDONTSEND; 2043233Seric 2053233Seric /* 2063233Seric ** Check to see that these people are allowed to 2073233Seric ** talk to each other. 2083233Seric */ 2093233Seric 2103233Seric if (!checkcompat(to)) 211294Seric { 2123233Seric giveresponse(EX_UNAVAILABLE, TRUE, m); 2133233Seric continue; 214294Seric } 2153233Seric 2163233Seric /* 2174099Seric ** Strip quote bits from names if the mailer is dumb 2184099Seric ** about them. 2193233Seric */ 2203233Seric 2213233Seric if (bitset(M_STRIPQ, m->m_flags)) 222294Seric { 2234099Seric stripquotes(user, TRUE); 2244099Seric stripquotes(host, TRUE); 2253233Seric } 2264099Seric else 2274099Seric { 2284099Seric stripquotes(user, FALSE); 2294099Seric stripquotes(host, FALSE); 2304099Seric } 2313233Seric 2323233Seric /* 2337052Seric ** Do initial connection setup if needed. 2347052Seric */ 2357052Seric 2367052Seric if (notopen) 2377052Seric { 2387052Seric message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name); 2397052Seric # ifdef SMTP 2407052Seric if (clever) 2417052Seric { 2427052Seric /* send the initial SMTP protocol */ 2438003Seric rcode = smtpinit(m, pv, (ADDRESS *) NULL); 2447052Seric } 2457052Seric # ifdef SMTP 2467052Seric notopen = FALSE; 2477052Seric } 2487052Seric 2497052Seric /* 2506059Seric ** Pass it to the other host if we are running SMTP. 2516059Seric */ 2526059Seric 2536059Seric if (clever) 2546059Seric { 2556059Seric # ifdef SMTP 2568003Seric if (rcode == EX_OK) 2578003Seric rcode = smtprcpt(to); 2588003Seric if (rcode != EX_OK) 2596059Seric { 2608003Seric if (rcode == EX_TEMPFAIL) 2616059Seric to->q_flags |= QQUEUEUP; 2626059Seric else 2636059Seric to->q_flags |= QBADADDR; 2648003Seric giveresponse(rcode, TRUE, m); 2656059Seric } 2666059Seric # else SMTP 2676059Seric syserr("trying to be clever"); 2686059Seric # endif SMTP 2696059Seric } 2706059Seric 2716059Seric /* 2724161Seric ** If an error message has already been given, don't 2734161Seric ** bother to send to this address. 2744161Seric ** 2754161Seric ** >>>>>>>>>> This clause assumes that the local mailer 2764161Seric ** >> NOTE >> cannot do any further aliasing; that 2774161Seric ** >>>>>>>>>> function is subsumed by sendmail. 2784161Seric */ 2794161Seric 2807293Seric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 2814161Seric continue; 2824161Seric 2834283Seric /* save statistics.... */ 2844596Seric Stat.stat_nt[to->q_mailer->m_mno]++; 2856900Seric Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize); 2864283Seric 2874161Seric /* 2883233Seric ** See if this user name is "special". 2893233Seric ** If the user name has a slash in it, assume that this 2906974Seric ** is a file -- send it off without further ado. Note 2916974Seric ** that this type of addresses is not processed along 2926974Seric ** with the others, so we fudge on the To person. 2933233Seric */ 2943233Seric 2954596Seric if (m == LocalMailer) 2963233Seric { 2975599Seric if (user[0] == '/') 298294Seric { 2998003Seric rcode = mailfile(user, getctladdr(to)); 3008003Seric giveresponse(rcode, TRUE, m); 3013233Seric continue; 302294Seric } 303294Seric } 3043233Seric 3054315Seric /* 3064315Seric ** Address is verified -- add this user to mailer 3074315Seric ** argv, and add it to the print list of recipients. 3084315Seric */ 3094315Seric 3106059Seric /* link together the chain of recipients */ 3116272Seric to->q_tchain = tochain; 3126272Seric tochain = to; 3136059Seric 3143233Seric /* create list of users for error messages */ 3153233Seric if (tobuf[0] != '\0') 3164082Seric (void) strcat(tobuf, ","); 3174082Seric (void) strcat(tobuf, to->q_paddr); 3183233Seric define('u', user); /* to user */ 3194078Seric define('z', to->q_home); /* user's home */ 3203233Seric 3214863Seric /* 3226059Seric ** Expand out this user into argument list. 3234863Seric */ 3244863Seric 3256059Seric if (!clever) 3263233Seric { 3276974Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 3284863Seric *pvp++ = newstr(buf); 3294863Seric if (pvp >= &pv[MAXPV - 2]) 3304863Seric { 3314863Seric /* allow some space for trailing parms */ 3324863Seric break; 3334863Seric } 3344863Seric } 335294Seric } 336294Seric 3374067Seric /* see if any addresses still exist */ 3384067Seric if (tobuf[0] == '\0') 3394863Seric { 3405179Seric # ifdef SMTP 3414863Seric if (clever) 3427228Seric smtpquit(pv[0], FALSE); 3435179Seric # endif SMTP 3447003Seric define('g', (char *) NULL); 3454067Seric return (0); 3464863Seric } 3474067Seric 3483233Seric /* print out messages as full list */ 3496900Seric CurEnv->e_to = tobuf; 3503233Seric 351294Seric /* 3523233Seric ** Fill out any parameters after the $u parameter. 353294Seric */ 354294Seric 3554863Seric while (!clever && *++mvp != NULL) 356294Seric { 3576974Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 3583233Seric *pvp++ = newstr(buf); 3593233Seric if (pvp >= &pv[MAXPV]) 3603233Seric syserr("deliver: pv overflow after $u for %s", pv[0]); 361294Seric } 3623233Seric *pvp++ = NULL; 363294Seric 364294Seric /* 365294Seric ** Call the mailer. 3662898Seric ** The argument vector gets built, pipes 367294Seric ** are created as necessary, and we fork & exec as 3682898Seric ** appropriate. 3694863Seric ** If we are running SMTP, we just need to clean up. 370294Seric */ 371294Seric 3724397Seric if (ctladdr == NULL) 3736900Seric ctladdr = &CurEnv->e_from; 3745179Seric # ifdef SMTP 3754863Seric if (clever) 3764863Seric { 3778003Seric rcode = smtpfinish(m, CurEnv); 3788003Seric if (rcode != EX_OK) 3798003Seric giveresponse(rcode, TRUE, m); 3808003Seric smtpquit(pv[0], rcode == EX_OK); 3814863Seric } 3824863Seric else 3835179Seric # endif SMTP 3848003Seric rcode = sendoff(m, pv, ctladdr); 3853233Seric 3864621Seric /* 3874621Seric ** If we got a temporary failure, arrange to queue the 3884621Seric ** addressees. 3894621Seric */ 3904621Seric 3918003Seric if (rcode == EX_TEMPFAIL) 3924621Seric { 3935032Seric for (to = tochain; to != NULL; to = to->q_tchain) 3944621Seric to->q_flags |= QQUEUEUP; 3954621Seric } 3964621Seric 3974488Seric errno = 0; 3987003Seric define('g', (char *) NULL); 3998003Seric return (rcode); 4003233Seric } 4013233Seric /* 4024214Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 4034214Seric ** 4044214Seric ** This MUST be a macro, since after a vfork we are running 4054214Seric ** two processes on the same stack!!! 4064214Seric ** 4074214Seric ** Parameters: 4084214Seric ** none. 4094214Seric ** 4104214Seric ** Returns: 4114214Seric ** From a macro??? You've got to be kidding! 4124214Seric ** 4134214Seric ** Side Effects: 4144214Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 4154214Seric ** pid of child in parent, zero in child. 4164214Seric ** -1 on unrecoverable error. 4174214Seric ** 4184214Seric ** Notes: 4194214Seric ** I'm awfully sorry this looks so awful. That's 4204214Seric ** vfork for you..... 4214214Seric */ 4224214Seric 4234214Seric # define NFORKTRIES 5 4244214Seric # ifdef VFORK 4254214Seric # define XFORK vfork 4264214Seric # else VFORK 4274214Seric # define XFORK fork 4284214Seric # endif VFORK 4294214Seric 4304214Seric # define DOFORK(fORKfN) \ 4314214Seric {\ 4324214Seric register int i;\ 4334214Seric \ 4344214Seric for (i = NFORKTRIES; i-- > 0; )\ 4354214Seric {\ 4364214Seric pid = fORKfN();\ 4374214Seric if (pid >= 0)\ 4384214Seric break;\ 4397003Seric sleep(NFORKTRIES - i);\ 4404214Seric }\ 4414214Seric } 4424214Seric /* 4434637Seric ** DOFORK -- simple fork interface to DOFORK. 4444637Seric ** 4454637Seric ** Parameters: 4464637Seric ** none. 4474637Seric ** 4484637Seric ** Returns: 4494637Seric ** pid of child in parent. 4504637Seric ** zero in child. 4514637Seric ** -1 on error. 4524637Seric ** 4534637Seric ** Side Effects: 4544637Seric ** returns twice, once in parent and once in child. 4554637Seric */ 4564637Seric 4574637Seric dofork() 4584637Seric { 4594637Seric register int pid; 4604637Seric 4614637Seric DOFORK(fork); 4624637Seric return (pid); 4634637Seric } 4644637Seric /* 4653233Seric ** SENDOFF -- send off call to mailer & collect response. 4663233Seric ** 4673233Seric ** Parameters: 4683233Seric ** m -- mailer descriptor. 4693233Seric ** pvp -- parameter vector to send to it. 4704397Seric ** ctladdr -- an address pointer controlling the 4714397Seric ** user/groupid etc. of the mailer. 4723233Seric ** 4733233Seric ** Returns: 4743233Seric ** exit status of mailer. 4753233Seric ** 4763233Seric ** Side Effects: 4773233Seric ** none. 4783233Seric */ 4793233Seric 4806974Seric sendoff(m, pvp, ctladdr) 4813233Seric struct mailer *m; 4823233Seric char **pvp; 4834397Seric ADDRESS *ctladdr; 4843233Seric { 4854863Seric auto FILE *mfile; 4864863Seric auto FILE *rfile; 4873233Seric register int i; 4883233Seric int pid; 4894863Seric 4904863Seric /* 4914863Seric ** Create connection to mailer. 4924863Seric */ 4934863Seric 4944863Seric pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 4954863Seric if (pid < 0) 4964863Seric return (-1); 4974863Seric 4984863Seric /* 4994863Seric ** Format and send message. 5004863Seric */ 5014863Seric 5024863Seric (void) signal(SIGPIPE, SIG_IGN); 5036974Seric putfromline(mfile, m); 5046974Seric (*CurEnv->e_puthdr)(mfile, m, CurEnv); 5056974Seric fprintf(mfile, "\n"); 5066974Seric (*CurEnv->e_putbody)(mfile, m, FALSE); 5074863Seric (void) fclose(mfile); 5084863Seric 5094863Seric i = endmailer(pid, pvp[0]); 5104863Seric giveresponse(i, TRUE, m); 5115981Seric 5125981Seric /* arrange a return receipt if requested */ 5138062Seric if (CurEnv->e_receiptto != NULL && bitset(M_LOCAL, m->m_flags)) 5145981Seric { 5156900Seric CurEnv->e_sendreceipt = TRUE; 5168062Seric if (ExitStat == EX_OK) 5178062Seric fprintf(Xscript, "%s... successfully delivered\n", 5188062Seric CurEnv->e_to); 5195981Seric /* do we want to send back more info? */ 5205981Seric } 5215981Seric 5224863Seric return (i); 5234863Seric } 5244863Seric /* 5254863Seric ** ENDMAILER -- Wait for mailer to terminate. 5264863Seric ** 5274863Seric ** We should never get fatal errors (e.g., segmentation 5284863Seric ** violation), so we report those specially. For other 5294863Seric ** errors, we choose a status message (into statmsg), 5304863Seric ** and if it represents an error, we print it. 5314863Seric ** 5324863Seric ** Parameters: 5334863Seric ** pid -- pid of mailer. 5344863Seric ** name -- name of mailer (for error messages). 5354863Seric ** 5364863Seric ** Returns: 5374863Seric ** exit code of mailer. 5384863Seric ** 5394863Seric ** Side Effects: 5404863Seric ** none. 5414863Seric */ 5424863Seric 5434863Seric endmailer(pid, name) 5444863Seric int pid; 5454863Seric char *name; 5464863Seric { 5474863Seric register int i; 5484863Seric auto int st; 5494863Seric 5506038Seric /* in the IPC case there is nothing to wait for */ 5516038Seric if (pid == 0) 5526038Seric return (EX_OK); 5536038Seric 5546038Seric /* wait for the mailer process to die and collect status */ 5558127Seric do 5568127Seric { 5578127Seric errno = 0; 5588127Seric i = wait(&st); 5598133Seric } while ((i >= 0 && i != pid) || errno == EINTR); 5604863Seric if (i < 0) 5614863Seric { 5624863Seric syserr("wait"); 5634863Seric return (-1); 5644863Seric } 5656038Seric 5666038Seric /* see if it died a horrid death */ 5674863Seric if ((st & 0377) != 0) 5684863Seric { 5694863Seric syserr("%s: stat %o", name, st); 5704863Seric ExitStat = EX_UNAVAILABLE; 5714863Seric return (-1); 5724863Seric } 5736038Seric 5746038Seric /* normal death -- return status */ 5754863Seric i = (st >> 8) & 0377; 5764863Seric return (i); 5774863Seric } 5784863Seric /* 5794863Seric ** OPENMAILER -- open connection to mailer. 5804863Seric ** 5814863Seric ** Parameters: 5824863Seric ** m -- mailer descriptor. 5834863Seric ** pvp -- parameter vector to pass to mailer. 5844863Seric ** ctladdr -- controlling address for user. 5854863Seric ** clever -- create a full duplex connection. 5864863Seric ** pmfile -- pointer to mfile (to mailer) connection. 5874863Seric ** prfile -- pointer to rfile (from mailer) connection. 5884863Seric ** 5894863Seric ** Returns: 5906038Seric ** pid of mailer ( > 0 ). 5914863Seric ** -1 on error. 5926038Seric ** zero on an IPC connection. 5934863Seric ** 5944863Seric ** Side Effects: 5954863Seric ** creates a mailer in a subprocess. 5964863Seric */ 5974863Seric 5984863Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 5994863Seric struct mailer *m; 6004863Seric char **pvp; 6014863Seric ADDRESS *ctladdr; 6024863Seric bool clever; 6034863Seric FILE **pmfile; 6044863Seric FILE **prfile; 6054863Seric { 6064863Seric int pid; 6074709Seric int mpvect[2]; 6084863Seric int rpvect[2]; 6093233Seric FILE *mfile; 6104863Seric FILE *rfile; 6113233Seric extern FILE *fdopen(); 6123233Seric 6133233Seric # ifdef DEBUG 6147672Seric if (tTd(11, 1)) 615294Seric { 6168178Seric printf("openmailer:"); 6173233Seric printav(pvp); 618294Seric } 6193233Seric # endif DEBUG 6204488Seric errno = 0; 6213233Seric 6226038Seric # ifdef DAEMON 6236038Seric /* 6246038Seric ** Deal with the special case of mail handled through an IPC 6256038Seric ** connection. 6266038Seric ** In this case we don't actually fork. We must be 6276038Seric ** running SMTP for this to work. We will return a 6286038Seric ** zero pid to indicate that we are running IPC. 6296038Seric */ 6306038Seric 6316038Seric if (strcmp(m->m_mailer, "[IPC]") == 0) 6326038Seric { 6336038Seric register int i; 6347285Seric register u_short port; 6356038Seric 6366038Seric if (!clever) 6376038Seric syserr("non-clever IPC"); 6386632Seric if (pvp[2] != NULL) 6397285Seric port = atoi(pvp[2]); 6406632Seric else 6417285Seric port = 0; 6427285Seric i = makeconnection(pvp[1], port, pmfile, prfile); 6436038Seric if (i != EX_OK) 6446047Seric { 6456047Seric ExitStat = i; 6466038Seric return (-1); 6476047Seric } 6486038Seric else 6496038Seric return (0); 6506038Seric } 6516038Seric # endif DAEMON 6526038Seric 6532898Seric /* create a pipe to shove the mail through */ 6544709Seric if (pipe(mpvect) < 0) 655294Seric { 6564863Seric syserr("pipe (to mailer)"); 657294Seric return (-1); 658294Seric } 6594863Seric 6605179Seric # ifdef SMTP 6614863Seric /* if this mailer speaks smtp, create a return pipe */ 6624863Seric if (clever && pipe(rpvect) < 0) 6634863Seric { 6644863Seric syserr("pipe (from mailer)"); 6654863Seric (void) close(mpvect[0]); 6664863Seric (void) close(mpvect[1]); 6674863Seric return (-1); 6684863Seric } 6695179Seric # endif SMTP 6704863Seric 6716038Seric /* 6726038Seric ** Actually fork the mailer process. 6736038Seric ** DOFORK is clever about retrying. 6746038Seric */ 6756038Seric 6767672Seric (void) fflush(Xscript); /* for debugging */ 6774214Seric DOFORK(XFORK); 6784327Seric /* pid is set by DOFORK */ 679294Seric if (pid < 0) 680294Seric { 6816038Seric /* failure */ 682294Seric syserr("Cannot fork"); 6834709Seric (void) close(mpvect[0]); 6844709Seric (void) close(mpvect[1]); 6854863Seric if (clever) 6864863Seric { 6874863Seric (void) close(rpvect[0]); 6884863Seric (void) close(rpvect[1]); 6894863Seric } 690294Seric return (-1); 691294Seric } 692294Seric else if (pid == 0) 693294Seric { 694294Seric /* child -- set up input & exec mailer */ 6951621Seric /* make diagnostic output be standard output */ 6964477Seric (void) signal(SIGINT, SIG_IGN); 6974477Seric (void) signal(SIGHUP, SIG_IGN); 6984215Seric (void) signal(SIGTERM, SIG_DFL); 6994709Seric 7004709Seric /* arrange to filter standard & diag output of command */ 7014863Seric if (clever) 7024709Seric { 7034863Seric (void) close(rpvect[0]); 7044709Seric (void) close(1); 7054863Seric (void) dup(rpvect[1]); 7064863Seric (void) close(rpvect[1]); 7074863Seric } 7084863Seric else if (OutChannel != stdout) 7094863Seric { 7104863Seric (void) close(1); 7114709Seric (void) dup(fileno(OutChannel)); 7124709Seric } 7134082Seric (void) close(2); 7144082Seric (void) dup(1); 7154709Seric 7164709Seric /* arrange to get standard input */ 7174709Seric (void) close(mpvect[1]); 7184082Seric (void) close(0); 7194709Seric if (dup(mpvect[0]) < 0) 720294Seric { 7212898Seric syserr("Cannot dup to zero!"); 7222898Seric _exit(EX_OSERR); 723294Seric } 7244709Seric (void) close(mpvect[0]); 7252968Seric if (!bitset(M_RESTR, m->m_flags)) 7264215Seric { 7274417Seric if (ctladdr->q_uid == 0) 7284417Seric { 7294417Seric (void) setgid(DefGid); 7304417Seric (void) setuid(DefUid); 7314417Seric } 7324417Seric else 7334415Seric { 7344417Seric (void) setgid(ctladdr->q_gid); 7354417Seric (void) setuid(ctladdr->q_uid); 7364415Seric } 7374215Seric } 7382774Seric # ifndef VFORK 7392774Seric /* 7402774Seric ** We have to be careful with vfork - we can't mung up the 7412774Seric ** memory but we don't want the mailer to inherit any extra 7422774Seric ** open files. Chances are the mailer won't 7432774Seric ** care about an extra file, but then again you never know. 7442774Seric ** Actually, we would like to close(fileno(pwf)), but it's 7452774Seric ** declared static so we can't. But if we fclose(pwf), which 7462774Seric ** is what endpwent does, it closes it in the parent too and 7472774Seric ** the next getpwnam will be slower. If you have a weird 7482774Seric ** mailer that chokes on the extra file you should do the 7492774Seric ** endpwent(). 7502774Seric ** 7512774Seric ** Similar comments apply to log. However, openlog is 7522774Seric ** clever enough to set the FIOCLEX mode on the file, 7532774Seric ** so it will be closed automatically on the exec. 7542774Seric */ 7552774Seric 7562774Seric endpwent(); 757294Seric # ifdef LOG 7582089Seric closelog(); 759294Seric # endif LOG 7602774Seric # endif VFORK 7616038Seric 7626038Seric /* try to execute the mailer */ 763294Seric execv(m->m_mailer, pvp); 7646038Seric 765294Seric /* syserr fails because log is closed */ 766294Seric /* syserr("Cannot exec %s", m->m_mailer); */ 7674214Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 7684082Seric (void) fflush(stdout); 7691619Seric _exit(EX_UNAVAILABLE); 770294Seric } 771294Seric 7724709Seric /* 7734863Seric ** Set up return value. 7744709Seric */ 7754709Seric 7764709Seric (void) close(mpvect[0]); 7774709Seric mfile = fdopen(mpvect[1], "w"); 7784863Seric if (clever) 7794863Seric { 7804863Seric (void) close(rpvect[1]); 7814863Seric rfile = fdopen(rpvect[0], "r"); 7824863Seric } 783294Seric 7844863Seric *pmfile = mfile; 7854863Seric *prfile = rfile; 786294Seric 7874863Seric return (pid); 788294Seric } 789294Seric /* 790294Seric ** GIVERESPONSE -- Interpret an error response from a mailer 791294Seric ** 792294Seric ** Parameters: 793294Seric ** stat -- the status code from the mailer (high byte 794294Seric ** only; core dumps must have been taken care of 795294Seric ** already). 796294Seric ** force -- if set, force an error message output, even 797294Seric ** if the mailer seems to like to print its own 798294Seric ** messages. 799294Seric ** m -- the mailer descriptor for this mailer. 800294Seric ** 801294Seric ** Returns: 8024082Seric ** none. 803294Seric ** 804294Seric ** Side Effects: 8051518Seric ** Errors may be incremented. 806294Seric ** ExitStat may be set. 807294Seric */ 808294Seric 809294Seric giveresponse(stat, force, m) 810294Seric int stat; 8117228Seric bool force; 812294Seric register struct mailer *m; 813294Seric { 814294Seric register char *statmsg; 815294Seric extern char *SysExMsg[]; 816294Seric register int i; 817294Seric extern int N_SysEx; 8181624Seric char buf[30]; 819294Seric 8204315Seric /* 8214315Seric ** Compute status message from code. 8224315Seric */ 8234315Seric 824294Seric i = stat - EX__BASE; 825294Seric if (i < 0 || i > N_SysEx) 826294Seric statmsg = NULL; 827294Seric else 828294Seric statmsg = SysExMsg[i]; 829294Seric if (stat == 0) 8304065Seric { 8318003Seric statmsg = "250 sent"; 8328003Seric message(Arpa_Info, &statmsg[4]); 8334065Seric } 8344621Seric else if (stat == EX_TEMPFAIL) 8354621Seric { 8367875Seric message(Arpa_Info, "deferred"); 8374621Seric } 838294Seric else 839294Seric { 8401518Seric Errors++; 8416047Seric FatalErrors = TRUE; 842294Seric if (statmsg == NULL && m->m_badstat != 0) 843294Seric { 844294Seric stat = m->m_badstat; 845294Seric i = stat - EX__BASE; 846294Seric # ifdef DEBUG 847294Seric if (i < 0 || i >= N_SysEx) 848294Seric syserr("Bad m_badstat %d", stat); 849294Seric else 850294Seric # endif DEBUG 8517344Seric statmsg = SysExMsg[i]; 852294Seric } 853294Seric if (statmsg == NULL) 854294Seric usrerr("unknown mailer response %d", stat); 8554065Seric else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 8568003Seric usrerr(statmsg); 8577344Seric else 8588003Seric fprintf(Xscript, "%s\n", &statmsg[4]); 859294Seric } 860294Seric 861294Seric /* 862294Seric ** Final cleanup. 863294Seric ** Log a record of the transaction. Compute the new 864294Seric ** ExitStat -- if we already had an error, stick with 865294Seric ** that. 866294Seric */ 867294Seric 8681624Seric if (statmsg == NULL) 8691624Seric { 8708003Seric (void) sprintf(buf, "554 error %d", stat); 8711624Seric statmsg = buf; 8721624Seric } 8731624Seric 874294Seric # ifdef LOG 8757680Seric if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2)) 8767858Seric { 8777858Seric extern char *pintvl(); 8787858Seric 8797858Seric syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id, 8807883Seric CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), 8818003Seric &statmsg[4]); 8827858Seric } 883294Seric # endif LOG 8844621Seric if (stat != EX_TEMPFAIL) 8854621Seric setstat(stat); 886294Seric } 887294Seric /* 8886974Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 889294Seric ** 8906974Seric ** This can be made an arbitrary message separator by changing $l 891294Seric ** 8926974Seric ** One of the ugliest hacks seen by human eyes is 8936974Seric ** contained herein: UUCP wants those stupid 8946974Seric ** "remote from <host>" lines. Why oh why does a 8956974Seric ** well-meaning programmer such as myself have to 8966974Seric ** deal with this kind of antique garbage???? 8976974Seric ** 898294Seric ** Parameters: 8996974Seric ** fp -- the file to output to. 9006974Seric ** m -- the mailer describing this entry. 901294Seric ** 902294Seric ** Returns: 9036974Seric ** none 904294Seric ** 905294Seric ** Side Effects: 9066974Seric ** outputs some text to fp. 907294Seric */ 908294Seric 9096974Seric putfromline(fp, m) 9106974Seric register FILE *fp; 9116974Seric register MAILER *m; 912294Seric { 9136974Seric char buf[MAXLINE]; 914294Seric 9156974Seric if (bitset(M_NHDR, m->m_flags)) 9166974Seric return; 9174315Seric 9186974Seric # ifdef UGLYUUCP 9196974Seric if (bitset(M_UGLYUUCP, m->m_flags)) 9204205Seric { 9216974Seric extern char *macvalue(); 9228178Seric char *sys = macvalue('g', CurEnv); 9236974Seric char *bang = index(sys, '!'); 9246041Seric 9256974Seric if (bang == NULL) 9266974Seric syserr("No ! in UUCP! (%s)", sys); 9275099Seric else 9286974Seric *bang = '\0'; 9297232Seric expand("From $f $d remote from $g\n", buf, 9306974Seric &buf[sizeof buf - 1], CurEnv); 9316974Seric *bang = '!'; 9326974Seric } 9336974Seric else 9345179Seric # endif UGLYUUCP 9356974Seric expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv); 9367123Seric putline(buf, fp, bitset(M_FULLSMTP, m->m_flags)); 9375981Seric } 9385981Seric /* 9396974Seric ** PUTHEADER -- put the header part of a message from the in-core copy 9405981Seric ** 9415981Seric ** Parameters: 9425981Seric ** fp -- file to put it on. 9435981Seric ** m -- mailer to use. 9446974Seric ** e -- envelope to use. 9455981Seric ** 9465981Seric ** Returns: 9475981Seric ** none. 9485981Seric ** 9495981Seric ** Side Effects: 9505981Seric ** none. 9515981Seric */ 9525981Seric 9536974Seric putheader(fp, m, e) 9545981Seric register FILE *fp; 9555981Seric register struct mailer *m; 9566974Seric register ENVELOPE *e; 9575981Seric { 9585981Seric char buf[BUFSIZ]; 9595981Seric register HDR *h; 9605981Seric extern char *arpadate(); 9615981Seric extern char *capitalize(); 9625981Seric extern bool samefrom(); 9637123Seric char obuf[MAXLINE]; 9647123Seric register char *obp; 9657123Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 9665981Seric 9676974Seric for (h = e->e_header; h != NULL; h = h->h_link) 9681828Seric { 9694315Seric register char *p; 9704315Seric 9713389Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 9728062Seric continue; 9734447Seric 9747756Seric p = h->h_value; 9758062Seric if (bitset(H_DEFAULT, h->h_flags)) 9763385Seric { 9777666Seric /* macro expand value if generated internally */ 9788062Seric expand(p, buf, &buf[sizeof buf], e); 9793385Seric p = buf; 9803385Seric } 9814447Seric if (p == NULL || *p == '\0') 9823389Seric continue; 9834209Seric 9848062Seric if (bitset(H_FROM|H_RCPT, h->h_flags)) 9854209Seric { 9868062Seric /* address field */ 9878093Seric bool oldstyle = e->e_oldstyle; 9888093Seric 9898093Seric if (bitset(H_FROM, h->h_flags)) 9908093Seric oldstyle = FALSE; 9918093Seric commaize(h, p, fp, oldstyle, m); 9924447Seric } 9938062Seric else 9944447Seric { 9958062Seric /* vanilla header line */ 9967123Seric (void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p); 9977123Seric putline(obuf, fp, fullsmtp); 9984209Seric } 9998062Seric h->h_flags |= H_USED; 10002898Seric } 1001294Seric } 1002294Seric /* 10037756Seric ** COMMAIZE -- output a header field, making a comma-translated list. 10047756Seric ** 10057756Seric ** Parameters: 10068062Seric ** h -- the header field to output. 10078062Seric ** p -- the value to put in it. 10087756Seric ** fp -- file to put it to. 10097756Seric ** oldstyle -- TRUE if this is an old style header. 10108235Seric ** m -- a pointer to the mailer descriptor. If NULL, 10118235Seric ** don't transform the name at all. 10127756Seric ** 10137756Seric ** Returns: 10147756Seric ** none. 10157756Seric ** 10167756Seric ** Side Effects: 10177756Seric ** outputs "p" to file "fp". 10187756Seric */ 10197756Seric 10208062Seric commaize(h, p, fp, oldstyle, m) 10218062Seric register HDR *h; 10227756Seric register char *p; 10237756Seric FILE *fp; 10247756Seric bool oldstyle; 10258062Seric register MAILER *m; 10267756Seric { 10278062Seric register char *obp; 10288062Seric int opos; 10298235Seric bool fullsmtp = FALSE; 10307756Seric bool firstone = TRUE; 10317756Seric char obuf[MAXLINE]; 10327756Seric 10337756Seric /* 10347756Seric ** Output the address list translated by the 10357756Seric ** mailer and with commas. 10367756Seric */ 10377756Seric 10387756Seric # ifdef DEBUG 10397756Seric if (tTd(14, 2)) 10408062Seric printf("commaize(%s: %s)\n", h->h_field, p); 10417756Seric # endif DEBUG 10427756Seric 10438235Seric if (m != NULL && bitset(M_FULLSMTP, m->m_flags)) 10448235Seric fullsmtp = TRUE; 10458235Seric 10467756Seric obp = obuf; 10478062Seric (void) sprintf(obp, "%s: ", capitalize(h->h_field)); 10488062Seric opos = strlen(h->h_field) + 2; 10497756Seric obp += opos; 10507756Seric 10517756Seric /* 10527756Seric ** Run through the list of values. 10537756Seric */ 10547756Seric 10557756Seric while (*p != '\0') 10567756Seric { 10577756Seric register char *name; 10587756Seric char savechar; 10598062Seric extern char *remotename(); 10608077Seric extern char *DelimChar; /* defined in prescan */ 10617756Seric 10627756Seric /* 10637756Seric ** Find the end of the name. New style names 10647756Seric ** end with a comma, old style names end with 10657756Seric ** a space character. However, spaces do not 10667756Seric ** necessarily delimit an old-style name -- at 10677756Seric ** signs mean keep going. 10687756Seric */ 10697756Seric 10708077Seric /* find end of name */ 10718077Seric while (isspace(*p) || *p == ',') 10727756Seric p++; 10737756Seric name = p; 10748077Seric for (;;) 10757756Seric { 10768077Seric char *oldp; 10777756Seric extern bool isatword(); 10787756Seric 10798091Seric (void) prescan(p, oldstyle ? ' ' : ','); 10808077Seric p = DelimChar; 10817756Seric 10827756Seric /* look to see if we have an at sign */ 10837756Seric oldp = p; 10847756Seric while (*p != '\0' && isspace(*p)) 10857756Seric p++; 10867756Seric 10877756Seric if (*p != '@' && !isatword(p)) 10887756Seric { 10897756Seric p = oldp; 10907756Seric break; 10917756Seric } 10927756Seric p += *p == '@' ? 1 : 2; 10937756Seric while (*p != '\0' && isspace(*p)) 10947756Seric p++; 10957756Seric } 10967756Seric /* at the end of one complete name */ 10977756Seric 10987756Seric /* strip off trailing white space */ 10997756Seric while (p >= name && (isspace(*p) || *p == ',' || *p == '\0')) 11007756Seric p--; 11017756Seric if (++p == name) 11027756Seric continue; 11037756Seric savechar = *p; 11047756Seric *p = '\0'; 11057756Seric 11067756Seric /* translate the name to be relative */ 11078235Seric if (m != NULL) 11088235Seric name = remotename(name, m, bitset(H_FROM, h->h_flags)); 11097756Seric if (*name == '\0') 11107756Seric { 11117756Seric *p = savechar; 11127756Seric continue; 11137756Seric } 11147756Seric 11157756Seric /* output the name with nice formatting */ 11167756Seric opos += strlen(name); 11177756Seric if (!firstone) 11187756Seric opos += 2; 11197756Seric if (opos > 78 && !firstone) 11207756Seric { 11217756Seric (void) sprintf(obp, ",\n"); 11227756Seric putline(obuf, fp, fullsmtp); 11237756Seric obp = obuf; 11247756Seric (void) sprintf(obp, " "); 11257756Seric obp += strlen(obp); 11267756Seric opos = 8 + strlen(name); 11277756Seric } 11287756Seric else if (!firstone) 11297756Seric { 11307756Seric (void) sprintf(obp, ", "); 11317756Seric obp += 2; 11327756Seric } 11337756Seric (void) sprintf(obp, "%s", name); 11347756Seric obp += strlen(obp); 11357756Seric firstone = FALSE; 11367756Seric *p = savechar; 11377756Seric } 11387756Seric (void) strcpy(obp, "\n"); 11397756Seric putline(obuf, fp, fullsmtp); 11407756Seric } 11417756Seric /* 11426974Seric ** PUTBODY -- put the body of a message. 11436974Seric ** 11446974Seric ** Parameters: 11456974Seric ** fp -- file to output onto. 11466974Seric ** m -- a mailer descriptor. 11476974Seric ** xdot -- if set, use SMTP hidden dot algorithm. 11486974Seric ** 11496974Seric ** Returns: 11506974Seric ** none. 11516974Seric ** 11526974Seric ** Side Effects: 11536974Seric ** The message is written onto fp. 11546974Seric */ 11556974Seric 11566974Seric putbody(fp, m, xdot) 11576974Seric FILE *fp; 11586974Seric struct mailer *m; 11596974Seric bool xdot; 11606974Seric { 11616974Seric char buf[MAXLINE + 1]; 11627123Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 11636974Seric 11646974Seric /* 11656974Seric ** Output the body of the message 11666974Seric */ 11676974Seric 11687003Seric #ifdef lint 11697003Seric /* m will be needed later for complete smtp emulation */ 11707003Seric if (m == NULL) 11717003Seric return; 11727003Seric #endif lint 11737003Seric 11746974Seric if (TempFile != NULL) 11756974Seric { 11766974Seric rewind(TempFile); 11776974Seric buf[0] = '.'; 11786974Seric while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL) 11797123Seric putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp); 11806974Seric 11816974Seric if (ferror(TempFile)) 11826974Seric { 11836974Seric syserr("putbody: read error"); 11846974Seric ExitStat = EX_IOERR; 11856974Seric } 11866974Seric } 11876974Seric 11886974Seric (void) fflush(fp); 11896974Seric if (ferror(fp) && errno != EPIPE) 11906974Seric { 11916974Seric syserr("putbody: write error"); 11926974Seric ExitStat = EX_IOERR; 11936974Seric } 11946974Seric errno = 0; 11956974Seric } 11966974Seric /* 11975936Seric ** ISATWORD -- tell if the word we are pointing to is "at". 11985936Seric ** 11995936Seric ** Parameters: 12005936Seric ** p -- word to check. 12015936Seric ** 12025936Seric ** Returns: 12035936Seric ** TRUE -- if p is the word at. 12045936Seric ** FALSE -- otherwise. 12055936Seric ** 12065936Seric ** Side Effects: 12075936Seric ** none. 12085936Seric */ 12095936Seric 12105936Seric bool 12115936Seric isatword(p) 12125936Seric register char *p; 12135936Seric { 12145936Seric extern char lower(); 12155936Seric 12165936Seric if (lower(p[0]) == 'a' && lower(p[1]) == 't' && 12175936Seric p[2] != '\0' && isspace(p[2])) 12185936Seric return (TRUE); 12195936Seric return (FALSE); 12205936Seric } 12215936Seric /* 12224370Seric ** SAMEFROM -- tell if two text addresses represent the same from address. 12234370Seric ** 12244370Seric ** Parameters: 12254370Seric ** ifrom -- internally generated form of from address. 12264370Seric ** efrom -- external form of from address. 12274370Seric ** 12284370Seric ** Returns: 12294370Seric ** TRUE -- if they convey the same info. 12304370Seric ** FALSE -- if any information has been lost. 12314370Seric ** 12324370Seric ** Side Effects: 12334370Seric ** none. 12344370Seric */ 12354370Seric 12364370Seric bool 12374370Seric samefrom(ifrom, efrom) 12384370Seric char *ifrom; 12394370Seric char *efrom; 12404370Seric { 12414447Seric register char *p; 12424447Seric char buf[MAXNAME + 4]; 12434447Seric 12444447Seric # ifdef DEBUG 12457672Seric if (tTd(3, 8)) 12464447Seric printf("samefrom(%s,%s)-->", ifrom, efrom); 12474447Seric # endif DEBUG 12484447Seric if (strcmp(ifrom, efrom) == 0) 12494447Seric goto success; 12504447Seric p = index(ifrom, '@'); 12514447Seric if (p == NULL) 12524447Seric goto failure; 12534447Seric *p = '\0'; 12547003Seric (void) strcpy(buf, ifrom); 12557003Seric (void) strcat(buf, " at "); 12564447Seric *p++ = '@'; 12577003Seric (void) strcat(buf, p); 12584447Seric if (strcmp(buf, efrom) == 0) 12594447Seric goto success; 12604447Seric 12614447Seric failure: 12624447Seric # ifdef DEBUG 12637672Seric if (tTd(3, 8)) 12644447Seric printf("FALSE\n"); 12654447Seric # endif DEBUG 12664447Seric return (FALSE); 12674447Seric 12684447Seric success: 12694447Seric # ifdef DEBUG 12707672Seric if (tTd(3, 8)) 12714447Seric printf("TRUE\n"); 12724447Seric # endif DEBUG 12734447Seric return (TRUE); 12744370Seric } 12754370Seric /* 1276294Seric ** MAILFILE -- Send a message to a file. 1277294Seric ** 12784327Seric ** If the file has the setuid/setgid bits set, but NO execute 12794327Seric ** bits, sendmail will try to become the owner of that file 12804327Seric ** rather than the real user. Obviously, this only works if 12814327Seric ** sendmail runs as root. 12824327Seric ** 1283294Seric ** Parameters: 1284294Seric ** filename -- the name of the file to send to. 12854397Seric ** ctladdr -- the controlling address header -- includes 12864397Seric ** the userid/groupid to be when sending. 1287294Seric ** 1288294Seric ** Returns: 1289294Seric ** The exit code associated with the operation. 1290294Seric ** 1291294Seric ** Side Effects: 1292294Seric ** none. 1293294Seric */ 1294294Seric 12954397Seric mailfile(filename, ctladdr) 1296294Seric char *filename; 12974397Seric ADDRESS *ctladdr; 1298294Seric { 1299294Seric register FILE *f; 13004214Seric register int pid; 1301294Seric 13024214Seric /* 13034214Seric ** Fork so we can change permissions here. 13044214Seric ** Note that we MUST use fork, not vfork, because of 13054214Seric ** the complications of calling subroutines, etc. 13064214Seric */ 13074067Seric 13084214Seric DOFORK(fork); 13094214Seric 13104214Seric if (pid < 0) 13114214Seric return (EX_OSERR); 13124214Seric else if (pid == 0) 13134214Seric { 13144214Seric /* child -- actually write to file */ 13154327Seric struct stat stb; 13164327Seric 13174215Seric (void) signal(SIGINT, SIG_DFL); 13184215Seric (void) signal(SIGHUP, SIG_DFL); 13194215Seric (void) signal(SIGTERM, SIG_DFL); 13204327Seric umask(OldUmask); 13214327Seric if (stat(filename, &stb) < 0) 13224431Seric stb.st_mode = 0666; 13234327Seric if (bitset(0111, stb.st_mode)) 13244327Seric exit(EX_CANTCREAT); 13254401Seric if (ctladdr == NULL) 13266900Seric ctladdr = &CurEnv->e_from; 13274327Seric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 13284417Seric { 13294417Seric if (ctladdr->q_uid == 0) 13304417Seric (void) setgid(DefGid); 13314417Seric else 13324417Seric (void) setgid(ctladdr->q_gid); 13334417Seric } 13344327Seric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 13354417Seric { 13364417Seric if (ctladdr->q_uid == 0) 13374417Seric (void) setuid(DefUid); 13384417Seric else 13394417Seric (void) setuid(ctladdr->q_uid); 13404417Seric } 13416887Seric f = dfopen(filename, "a"); 13424214Seric if (f == NULL) 13434214Seric exit(EX_CANTCREAT); 13444214Seric 13456974Seric putfromline(f, Mailer[1]); 13466974Seric (*CurEnv->e_puthdr)(f, Mailer[1], CurEnv); 13477123Seric fputs("\n", f); 13486974Seric (*CurEnv->e_putbody)(f, Mailer[1], FALSE); 13494214Seric fputs("\n", f); 13504214Seric (void) fclose(f); 13514214Seric (void) fflush(stdout); 13524417Seric 13536887Seric /* reset ISUID & ISGID bits for paranoid systems */ 13544621Seric (void) chmod(filename, (int) stb.st_mode); 13554214Seric exit(EX_OK); 13564315Seric /*NOTREACHED*/ 13574214Seric } 13584214Seric else 13594214Seric { 13604214Seric /* parent -- wait for exit status */ 13614214Seric register int i; 13624214Seric auto int stat; 13634214Seric 13644214Seric while ((i = wait(&stat)) != pid) 13654214Seric { 13664214Seric if (i < 0) 13674214Seric { 13684214Seric stat = EX_OSERR << 8; 13694214Seric break; 13704214Seric } 13714214Seric } 13724215Seric if ((stat & 0377) != 0) 13734215Seric stat = EX_UNAVAILABLE << 8; 13744214Seric return ((stat >> 8) & 0377); 13754214Seric } 1376294Seric } 13774550Seric /* 13784550Seric ** SENDALL -- actually send all the messages. 13794550Seric ** 13804550Seric ** Parameters: 13817043Seric ** e -- the envelope to send. 13824550Seric ** verifyonly -- if set, only give verification messages. 13834550Seric ** 13844550Seric ** Returns: 13854550Seric ** none. 13864550Seric ** 13874550Seric ** Side Effects: 13884550Seric ** Scans the send lists and sends everything it finds. 13897043Seric ** Delivers any appropriate error messages. 13904550Seric */ 13914550Seric 13927043Seric sendall(e, verifyonly) 13937043Seric ENVELOPE *e; 13944550Seric bool verifyonly; 13954550Seric { 13965008Seric register ADDRESS *q; 13977779Seric bool oldverbose; 13984550Seric 13995032Seric # ifdef DEBUG 14008248Seric if (tTd(13, 1)) 14015032Seric { 14028248Seric printf("\nSENDALL: verify %d, sendqueue:\n"); 14037043Seric printaddr(e->e_sendqueue, TRUE); 14045032Seric } 14055032Seric # endif DEBUG 14065008Seric 14077043Seric /* 14087043Seric ** Run through the list and send everything. 14097043Seric */ 14107043Seric 14117779Seric oldverbose = Verbose; 14127779Seric if (verifyonly) 14137779Seric Verbose = TRUE; 14147043Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14154550Seric { 14165008Seric if (verifyonly) 14174550Seric { 14186900Seric CurEnv->e_to = q->q_paddr; 14195008Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 14207052Seric message(Arpa_Info, "deliverable"); 14214550Seric } 14225008Seric else 14236974Seric (void) deliver(q); 14244550Seric } 14257779Seric Verbose = oldverbose; 14267043Seric 14277043Seric /* 14287043Seric ** Now run through and check for errors. 14297043Seric */ 14307043Seric 14317043Seric if (verifyonly) 14327043Seric return; 14337043Seric 14347043Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14357043Seric { 14367043Seric register ADDRESS *qq; 14377043Seric 14388248Seric # ifdef DEBUG 14398248Seric if (tTd(13, 3)) 14408248Seric { 14418248Seric printf("Checking "); 14428248Seric printaddr(q, FALSE); 14438248Seric } 14448248Seric # endif DEBUG 14458248Seric 14467043Seric if (bitset(QQUEUEUP, q->q_flags)) 14477043Seric e->e_queueup = TRUE; 14487043Seric 14497043Seric /* we have an address that failed -- find the parent */ 14507043Seric for (qq = q; qq != NULL; qq = qq->q_alias) 14517043Seric { 14527043Seric char obuf[MAXNAME + 6]; 14537043Seric extern char *aliaslookup(); 14547043Seric 14557043Seric /* we can only have owners for local addresses */ 14567043Seric if (!bitset(M_LOCAL, qq->q_mailer->m_flags)) 14577043Seric continue; 14587043Seric 14597043Seric /* see if the owner list exists */ 14607043Seric (void) strcpy(obuf, "owner-"); 14617047Seric if (strncmp(qq->q_user, "owner-", 6) == 0) 14627047Seric (void) strcat(obuf, "owner"); 14637047Seric else 14647047Seric (void) strcat(obuf, qq->q_user); 14657043Seric if (aliaslookup(obuf) == NULL) 14667043Seric continue; 14677043Seric 14688248Seric # ifdef DEBUG 14698248Seric if (tTd(13, 4)) 14708248Seric printf("Errors to %s\n", obuf); 14718248Seric # endif DEBUG 14728248Seric 14738248Seric /* add in an errors-to field */ 14748248Seric /* ugh... must happen before delivery..... 14758248Seric addheader("errors-to", newstr(obuf), e); 14768248Seric .... i guess this should go in sendto */ 14778248Seric 14788248Seric /* only send errors if the message failed */ 14798248Seric if (!bitset(QBADADDR, q->q_flags)) 14808248Seric break; 14818248Seric 14827043Seric /* owner list exists -- add it to the error queue */ 14837043Seric qq->q_flags &= ~QPRIMARY; 14848077Seric sendto(obuf, qq, &e->e_errorqueue); 14857043Seric MailBack = TRUE; 14867043Seric break; 14877043Seric } 14887043Seric 14897043Seric /* if we did not find an owner, send to the sender */ 14908426Seric if (qq == NULL && bitset(QBADADDR, q->q_flags)) 14918077Seric sendto(e->e_from.q_paddr, qq, &e->e_errorqueue); 14927043Seric } 14934550Seric } 14947043Seric /* 14957043Seric ** CHECKERRORS -- check a queue of addresses and process errors. 14967043Seric ** 14977043Seric ** Parameters: 14987043Seric ** e -- the envelope to check. 14997043Seric ** 15007043Seric ** Returns: 15017043Seric ** none. 15027043Seric ** 15037043Seric ** Side Effects: 15047043Seric ** Arranges to queue all tempfailed messages in q 15057043Seric ** or deliver error responses. 15067043Seric */ 15077043Seric 15087043Seric checkerrors(e) 15097043Seric register ENVELOPE *e; 15107043Seric { 15117808Seric register ADDRESS *q; 15127808Seric 15137043Seric # ifdef DEBUG 15147672Seric if (tTd(4, 1)) 15157043Seric { 15167371Seric printf("\ncheckerrors: FatalErrors %d, errorqueue:\n"); 15177043Seric printaddr(e->e_errorqueue, TRUE); 15187043Seric } 15197043Seric # endif DEBUG 15207043Seric 15217043Seric /* mail back the transcript on errors */ 15227043Seric if (FatalErrors) 15237043Seric savemail(); 15247043Seric 15257043Seric /* queue up anything laying around */ 15267858Seric if (e->e_dontqueue) 15277858Seric return; 15287808Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 15297043Seric { 15307808Seric if (bitset(QQUEUEUP, q->q_flags)) 15317808Seric { 15327043Seric # ifdef QUEUE 15337808Seric queueup(e, FALSE); 15348262Seric e->e_df = e->e_qf = NULL; 15358262Seric e->e_dontqueue = TRUE; 15367043Seric # else QUEUE 15377808Seric syserr("checkerrors: trying to queue %s", e->e_df); 15387043Seric # endif QUEUE 15397808Seric break; 15407808Seric } 15417043Seric } 15427043Seric } 1543