1294Seric # include <signal.h> 24123Seric # include <errno.h> 34621Seric # include "sendmail.h" 44327Seric # include <sys/stat.h> 5294Seric 6*7875Seric SCCSID(@(#)deliver.c 3.102 08/24/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 */ 38294Seric register int i; 392968Seric extern bool checkcompat(); 403233Seric char *pv[MAXPV+1]; 414452Seric char tobuf[MAXLINE]; /* text line of to people */ 423233Seric char buf[MAXNAME]; 434397Seric ADDRESS *ctladdr; 444397Seric extern ADDRESS *getctladdr(); 454452Seric char tfrombuf[MAXNAME]; /* translated from person */ 464452Seric extern char **prescan(); 474621Seric register ADDRESS *to = firstto; 484863Seric bool clever = FALSE; /* running user smtp to this mailer */ 495032Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 507052Seric bool notopen = TRUE; /* set if connection not quite open */ 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) 78*7875Seric if (!bitset(QDONTSEND, to->q_flags) && 79*7875Seric 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. 915903Seric ** We rewrite the from address here, being careful 925903Seric ** to also rewrite it again using ruleset 2 to 935903Seric ** eliminate redundancies. 942968Seric */ 952968Seric 964452Seric /* rewrite from address, using rewriting rules */ 976974Seric expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv); 984452Seric mvp = prescan(buf, '\0'); 994452Seric if (mvp == NULL) 1004452Seric { 1014452Seric syserr("bad mailer from translate \"%s\"", buf); 1024452Seric return (EX_SOFTWARE); 1034452Seric } 1044452Seric rewrite(mvp, 2); 1054452Seric cataddr(mvp, tfrombuf, sizeof tfrombuf); 1064452Seric 1074452Seric define('g', tfrombuf); /* translated sender address */ 1083233Seric define('h', host); /* to host */ 1093233Seric Errors = 0; 1103233Seric pvp = pv; 1113233Seric *pvp++ = m->m_argv[0]; 1122968Seric 1133233Seric /* insert -f or -r flag as appropriate */ 1143233Seric if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag) 1153233Seric { 1163233Seric if (bitset(M_FOPT, m->m_flags)) 1173233Seric *pvp++ = "-f"; 1183233Seric else 1193233Seric *pvp++ = "-r"; 1206974Seric expand("$g", buf, &buf[sizeof buf - 1], CurEnv); 1213233Seric *pvp++ = newstr(buf); 1223233Seric } 123294Seric 124294Seric /* 1253233Seric ** Append the other fixed parts of the argv. These run 1263233Seric ** up to the first entry containing "$u". There can only 1273233Seric ** be one of these, and there are only a few more slots 1283233Seric ** in the pv after it. 129294Seric */ 130294Seric 1313233Seric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 132294Seric { 1333233Seric while ((p = index(p, '$')) != NULL) 1343233Seric if (*++p == 'u') 1353233Seric break; 1363233Seric if (p != NULL) 1373233Seric break; 1383233Seric 1393233Seric /* this entry is safe -- go ahead and process it */ 1406974Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 1413233Seric *pvp++ = newstr(buf); 1423233Seric if (pvp >= &pv[MAXPV - 3]) 1433233Seric { 1443233Seric syserr("Too many parameters to %s before $u", pv[0]); 1453233Seric return (-1); 1463233Seric } 147294Seric } 1484863Seric 1496038Seric /* 1506038Seric ** If we have no substitution for the user name in the argument 1516038Seric ** list, we know that we must supply the names otherwise -- and 1526038Seric ** SMTP is the answer!! 1536038Seric */ 1546038Seric 1553233Seric if (*mvp == NULL) 1564863Seric { 1574863Seric /* running SMTP */ 1585179Seric # ifdef SMTP 1594863Seric clever = TRUE; 1604863Seric *pvp = NULL; 1615179Seric # else SMTP 1626038Seric /* oops! we don't implement SMTP */ 1635179Seric syserr("SMTP style mailer"); 1645179Seric return (EX_SOFTWARE); 1655179Seric # endif SMTP 1664863Seric } 167294Seric 168294Seric /* 1693233Seric ** At this point *mvp points to the argument with $u. We 1703233Seric ** run through our address list and append all the addresses 1713233Seric ** we can. If we run out of space, do not fret! We can 1723233Seric ** always send another copy later. 173294Seric */ 174294Seric 1753233Seric tobuf[0] = '\0'; 1766900Seric CurEnv->e_to = tobuf; 1774397Seric ctladdr = NULL; 1783233Seric for (; to != NULL; to = to->q_next) 179294Seric { 1803233Seric /* avoid sending multiple recipients to dumb mailers */ 1814382Seric if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags)) 1823233Seric break; 1833233Seric 1843233Seric /* if already sent or not for this host, don't send */ 1857052Seric if (bitset(QDONTSEND, to->q_flags) || 1867052Seric strcmp(to->q_host, host) != 0 || 1877052Seric to->q_mailer != firstto->q_mailer) 1883233Seric continue; 1894397Seric 1905032Seric # ifdef DEBUG 1917672Seric if (tTd(10, 1)) 1925032Seric { 1935032Seric printf("\nsend to "); 1945032Seric printaddr(to, FALSE); 1955032Seric } 1965032Seric # endif DEBUG 1975032Seric 1984397Seric /* compute effective uid/gid when sending */ 1994596Seric if (to->q_mailer == ProgMailer) 2004397Seric ctladdr = getctladdr(to); 2014397Seric 2023233Seric user = to->q_user; 2036900Seric CurEnv->e_to = to->q_paddr; 2043233Seric to->q_flags |= QDONTSEND; 2053233Seric 2063233Seric /* 2073233Seric ** Check to see that these people are allowed to 2083233Seric ** talk to each other. 2093233Seric */ 2103233Seric 2113233Seric if (!checkcompat(to)) 212294Seric { 2133233Seric giveresponse(EX_UNAVAILABLE, TRUE, m); 2143233Seric continue; 215294Seric } 2163233Seric 2173233Seric /* 2184099Seric ** Strip quote bits from names if the mailer is dumb 2194099Seric ** about them. 2203233Seric */ 2213233Seric 2223233Seric if (bitset(M_STRIPQ, m->m_flags)) 223294Seric { 2244099Seric stripquotes(user, TRUE); 2254099Seric stripquotes(host, TRUE); 2263233Seric } 2274099Seric else 2284099Seric { 2294099Seric stripquotes(user, FALSE); 2304099Seric stripquotes(host, FALSE); 2314099Seric } 2323233Seric 2333233Seric /* 2347052Seric ** Do initial connection setup if needed. 2357052Seric */ 2367052Seric 2377052Seric if (notopen) 2387052Seric { 2397052Seric message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name); 2407052Seric # ifdef SMTP 2417052Seric if (clever) 2427052Seric { 2437052Seric /* send the initial SMTP protocol */ 2447052Seric i = smtpinit(m, pv, (ADDRESS *) NULL); 2457052Seric } 2467052Seric # ifdef SMTP 2477052Seric notopen = FALSE; 2487052Seric } 2497052Seric 2507052Seric /* 2516059Seric ** Pass it to the other host if we are running SMTP. 2526059Seric */ 2536059Seric 2546059Seric if (clever) 2556059Seric { 2566059Seric # ifdef SMTP 2576059Seric i = smtprcpt(to); 2586059Seric if (i != EX_OK) 2596059Seric { 2606059Seric # ifdef QUEUE 2616059Seric if (i == EX_TEMPFAIL) 2626059Seric to->q_flags |= QQUEUEUP; 2636059Seric else 2646059Seric # endif QUEUE 2656059Seric to->q_flags |= QBADADDR; 2667293Seric giveresponse(i, TRUE, m); 2676059Seric } 2686059Seric # else SMTP 2696059Seric syserr("trying to be clever"); 2706059Seric # endif SMTP 2716059Seric } 2726059Seric 2736059Seric /* 2744161Seric ** If an error message has already been given, don't 2754161Seric ** bother to send to this address. 2764161Seric ** 2774161Seric ** >>>>>>>>>> This clause assumes that the local mailer 2784161Seric ** >> NOTE >> cannot do any further aliasing; that 2794161Seric ** >>>>>>>>>> function is subsumed by sendmail. 2804161Seric */ 2814161Seric 2827293Seric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 2834161Seric continue; 2844161Seric 2854283Seric /* save statistics.... */ 2864596Seric Stat.stat_nt[to->q_mailer->m_mno]++; 2876900Seric Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize); 2884283Seric 2894161Seric /* 2903233Seric ** See if this user name is "special". 2913233Seric ** If the user name has a slash in it, assume that this 2926974Seric ** is a file -- send it off without further ado. Note 2936974Seric ** that this type of addresses is not processed along 2946974Seric ** with the others, so we fudge on the To person. 2953233Seric */ 2963233Seric 2974596Seric if (m == LocalMailer) 2983233Seric { 2995599Seric if (user[0] == '/') 300294Seric { 3014397Seric i = mailfile(user, getctladdr(to)); 302294Seric giveresponse(i, TRUE, m); 3033233Seric continue; 304294Seric } 305294Seric } 3063233Seric 3074315Seric /* 3084315Seric ** Address is verified -- add this user to mailer 3094315Seric ** argv, and add it to the print list of recipients. 3104315Seric */ 3114315Seric 3126059Seric /* link together the chain of recipients */ 3136272Seric to->q_tchain = tochain; 3146272Seric tochain = to; 3156059Seric 3163233Seric /* create list of users for error messages */ 3173233Seric if (tobuf[0] != '\0') 3184082Seric (void) strcat(tobuf, ","); 3194082Seric (void) strcat(tobuf, to->q_paddr); 3203233Seric define('u', user); /* to user */ 3214078Seric define('z', to->q_home); /* user's home */ 3223233Seric 3234863Seric /* 3246059Seric ** Expand out this user into argument list. 3254863Seric */ 3264863Seric 3276059Seric if (!clever) 3283233Seric { 3296974Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 3304863Seric *pvp++ = newstr(buf); 3314863Seric if (pvp >= &pv[MAXPV - 2]) 3324863Seric { 3334863Seric /* allow some space for trailing parms */ 3344863Seric break; 3354863Seric } 3364863Seric } 337294Seric } 338294Seric 3394067Seric /* see if any addresses still exist */ 3404067Seric if (tobuf[0] == '\0') 3414863Seric { 3425179Seric # ifdef SMTP 3434863Seric if (clever) 3447228Seric smtpquit(pv[0], FALSE); 3455179Seric # endif SMTP 3467003Seric define('g', (char *) NULL); 3474067Seric return (0); 3484863Seric } 3494067Seric 3503233Seric /* print out messages as full list */ 3516900Seric CurEnv->e_to = tobuf; 3523233Seric 353294Seric /* 3543233Seric ** Fill out any parameters after the $u parameter. 355294Seric */ 356294Seric 3574863Seric while (!clever && *++mvp != NULL) 358294Seric { 3596974Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 3603233Seric *pvp++ = newstr(buf); 3613233Seric if (pvp >= &pv[MAXPV]) 3623233Seric syserr("deliver: pv overflow after $u for %s", pv[0]); 363294Seric } 3643233Seric *pvp++ = NULL; 365294Seric 366294Seric /* 367294Seric ** Call the mailer. 3682898Seric ** The argument vector gets built, pipes 369294Seric ** are created as necessary, and we fork & exec as 3702898Seric ** appropriate. 3714863Seric ** If we are running SMTP, we just need to clean up. 372294Seric */ 373294Seric 3744397Seric if (ctladdr == NULL) 3756900Seric ctladdr = &CurEnv->e_from; 3765179Seric # ifdef SMTP 3774863Seric if (clever) 3784863Seric { 3796974Seric i = smtpfinish(m, CurEnv); 3807228Seric smtpquit(pv[0], TRUE); 3814863Seric } 3824863Seric else 3835179Seric # endif SMTP 3846974Seric i = sendoff(m, pv, ctladdr); 3853233Seric 3864621Seric /* 3874621Seric ** If we got a temporary failure, arrange to queue the 3884621Seric ** addressees. 3894621Seric */ 3904621Seric 3915179Seric # ifdef QUEUE 3924621Seric if (i == EX_TEMPFAIL) 3934621Seric { 3945032Seric for (to = tochain; to != NULL; to = to->q_tchain) 3954621Seric to->q_flags |= QQUEUEUP; 3964621Seric } 3975179Seric # endif QUEUE 3984621Seric 3994488Seric errno = 0; 4007003Seric define('g', (char *) NULL); 4013233Seric return (i); 4023233Seric } 4033233Seric /* 4044214Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 4054214Seric ** 4064214Seric ** This MUST be a macro, since after a vfork we are running 4074214Seric ** two processes on the same stack!!! 4084214Seric ** 4094214Seric ** Parameters: 4104214Seric ** none. 4114214Seric ** 4124214Seric ** Returns: 4134214Seric ** From a macro??? You've got to be kidding! 4144214Seric ** 4154214Seric ** Side Effects: 4164214Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 4174214Seric ** pid of child in parent, zero in child. 4184214Seric ** -1 on unrecoverable error. 4194214Seric ** 4204214Seric ** Notes: 4214214Seric ** I'm awfully sorry this looks so awful. That's 4224214Seric ** vfork for you..... 4234214Seric */ 4244214Seric 4254214Seric # define NFORKTRIES 5 4264214Seric # ifdef VFORK 4274214Seric # define XFORK vfork 4284214Seric # else VFORK 4294214Seric # define XFORK fork 4304214Seric # endif VFORK 4314214Seric 4324214Seric # define DOFORK(fORKfN) \ 4334214Seric {\ 4344214Seric register int i;\ 4354214Seric \ 4364214Seric for (i = NFORKTRIES; i-- > 0; )\ 4374214Seric {\ 4384214Seric pid = fORKfN();\ 4394214Seric if (pid >= 0)\ 4404214Seric break;\ 4417003Seric sleep(NFORKTRIES - i);\ 4424214Seric }\ 4434214Seric } 4444214Seric /* 4454637Seric ** DOFORK -- simple fork interface to DOFORK. 4464637Seric ** 4474637Seric ** Parameters: 4484637Seric ** none. 4494637Seric ** 4504637Seric ** Returns: 4514637Seric ** pid of child in parent. 4524637Seric ** zero in child. 4534637Seric ** -1 on error. 4544637Seric ** 4554637Seric ** Side Effects: 4564637Seric ** returns twice, once in parent and once in child. 4574637Seric */ 4584637Seric 4594637Seric dofork() 4604637Seric { 4614637Seric register int pid; 4624637Seric 4634637Seric DOFORK(fork); 4644637Seric return (pid); 4654637Seric } 4664637Seric /* 4673233Seric ** SENDOFF -- send off call to mailer & collect response. 4683233Seric ** 4693233Seric ** Parameters: 4703233Seric ** m -- mailer descriptor. 4713233Seric ** pvp -- parameter vector to send to it. 4724397Seric ** ctladdr -- an address pointer controlling the 4734397Seric ** user/groupid etc. of the mailer. 4743233Seric ** 4753233Seric ** Returns: 4763233Seric ** exit status of mailer. 4773233Seric ** 4783233Seric ** Side Effects: 4793233Seric ** none. 4803233Seric */ 4813233Seric 4826974Seric sendoff(m, pvp, ctladdr) 4833233Seric struct mailer *m; 4843233Seric char **pvp; 4854397Seric ADDRESS *ctladdr; 4863233Seric { 4874863Seric auto FILE *mfile; 4884863Seric auto FILE *rfile; 4893233Seric register int i; 4903233Seric int pid; 4914863Seric 4924863Seric /* 4934863Seric ** Create connection to mailer. 4944863Seric */ 4954863Seric 4964863Seric pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 4974863Seric if (pid < 0) 4984863Seric return (-1); 4994863Seric 5004863Seric /* 5014863Seric ** Format and send message. 5024863Seric */ 5034863Seric 5044863Seric (void) signal(SIGPIPE, SIG_IGN); 5056974Seric putfromline(mfile, m); 5066974Seric (*CurEnv->e_puthdr)(mfile, m, CurEnv); 5076974Seric fprintf(mfile, "\n"); 5086974Seric (*CurEnv->e_putbody)(mfile, m, FALSE); 5094863Seric (void) fclose(mfile); 5104863Seric 5114863Seric i = endmailer(pid, pvp[0]); 5124863Seric giveresponse(i, TRUE, m); 5135981Seric 5145981Seric /* arrange a return receipt if requested */ 5156900Seric if (CurEnv->e_retreceipt && bitset(M_LOCAL, m->m_flags) && i == EX_OK) 5165981Seric { 5176900Seric CurEnv->e_sendreceipt = TRUE; 5186900Seric fprintf(Xscript, "%s... successfully delivered\n", 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 */ 5554863Seric while ((i = wait(&st)) > 0 && i != pid) 5564863Seric continue; 5574863Seric if (i < 0) 5584863Seric { 5594863Seric syserr("wait"); 5604863Seric return (-1); 5614863Seric } 5626038Seric 5636038Seric /* see if it died a horrid death */ 5644863Seric if ((st & 0377) != 0) 5654863Seric { 5664863Seric syserr("%s: stat %o", name, st); 5674863Seric ExitStat = EX_UNAVAILABLE; 5684863Seric return (-1); 5694863Seric } 5706038Seric 5716038Seric /* normal death -- return status */ 5724863Seric i = (st >> 8) & 0377; 5734863Seric return (i); 5744863Seric } 5754863Seric /* 5764863Seric ** OPENMAILER -- open connection to mailer. 5774863Seric ** 5784863Seric ** Parameters: 5794863Seric ** m -- mailer descriptor. 5804863Seric ** pvp -- parameter vector to pass to mailer. 5814863Seric ** ctladdr -- controlling address for user. 5824863Seric ** clever -- create a full duplex connection. 5834863Seric ** pmfile -- pointer to mfile (to mailer) connection. 5844863Seric ** prfile -- pointer to rfile (from mailer) connection. 5854863Seric ** 5864863Seric ** Returns: 5876038Seric ** pid of mailer ( > 0 ). 5884863Seric ** -1 on error. 5896038Seric ** zero on an IPC connection. 5904863Seric ** 5914863Seric ** Side Effects: 5924863Seric ** creates a mailer in a subprocess. 5934863Seric */ 5944863Seric 5954863Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 5964863Seric struct mailer *m; 5974863Seric char **pvp; 5984863Seric ADDRESS *ctladdr; 5994863Seric bool clever; 6004863Seric FILE **pmfile; 6014863Seric FILE **prfile; 6024863Seric { 6034863Seric int pid; 6044709Seric int mpvect[2]; 6054863Seric int rpvect[2]; 6063233Seric FILE *mfile; 6074863Seric FILE *rfile; 6083233Seric extern FILE *fdopen(); 6093233Seric 6103233Seric # ifdef DEBUG 6117672Seric if (tTd(11, 1)) 612294Seric { 6134863Seric printf("openmailer:\n"); 6143233Seric printav(pvp); 615294Seric } 6163233Seric # endif DEBUG 6174488Seric errno = 0; 6183233Seric 6196038Seric # ifdef DAEMON 6206038Seric /* 6216038Seric ** Deal with the special case of mail handled through an IPC 6226038Seric ** connection. 6236038Seric ** In this case we don't actually fork. We must be 6246038Seric ** running SMTP for this to work. We will return a 6256038Seric ** zero pid to indicate that we are running IPC. 6266038Seric */ 6276038Seric 6286038Seric if (strcmp(m->m_mailer, "[IPC]") == 0) 6296038Seric { 6306038Seric register int i; 6317285Seric register u_short port; 6326038Seric 6336038Seric if (!clever) 6346038Seric syserr("non-clever IPC"); 6356632Seric if (pvp[2] != NULL) 6367285Seric port = atoi(pvp[2]); 6376632Seric else 6387285Seric port = 0; 6397285Seric i = makeconnection(pvp[1], port, pmfile, prfile); 6406038Seric if (i != EX_OK) 6416047Seric { 6426047Seric ExitStat = i; 6436038Seric return (-1); 6446047Seric } 6456038Seric else 6466038Seric return (0); 6476038Seric } 6486038Seric # endif DAEMON 6496038Seric 6502898Seric /* create a pipe to shove the mail through */ 6514709Seric if (pipe(mpvect) < 0) 652294Seric { 6534863Seric syserr("pipe (to mailer)"); 654294Seric return (-1); 655294Seric } 6564863Seric 6575179Seric # ifdef SMTP 6584863Seric /* if this mailer speaks smtp, create a return pipe */ 6594863Seric if (clever && pipe(rpvect) < 0) 6604863Seric { 6614863Seric syserr("pipe (from mailer)"); 6624863Seric (void) close(mpvect[0]); 6634863Seric (void) close(mpvect[1]); 6644863Seric return (-1); 6654863Seric } 6665179Seric # endif SMTP 6674863Seric 6686038Seric /* 6696038Seric ** Actually fork the mailer process. 6706038Seric ** DOFORK is clever about retrying. 6716038Seric */ 6726038Seric 6737672Seric (void) fflush(Xscript); /* for debugging */ 6744214Seric DOFORK(XFORK); 6754327Seric /* pid is set by DOFORK */ 676294Seric if (pid < 0) 677294Seric { 6786038Seric /* failure */ 679294Seric syserr("Cannot fork"); 6804709Seric (void) close(mpvect[0]); 6814709Seric (void) close(mpvect[1]); 6824863Seric if (clever) 6834863Seric { 6844863Seric (void) close(rpvect[0]); 6854863Seric (void) close(rpvect[1]); 6864863Seric } 687294Seric return (-1); 688294Seric } 689294Seric else if (pid == 0) 690294Seric { 691294Seric /* child -- set up input & exec mailer */ 6921621Seric /* make diagnostic output be standard output */ 6934477Seric (void) signal(SIGINT, SIG_IGN); 6944477Seric (void) signal(SIGHUP, SIG_IGN); 6954215Seric (void) signal(SIGTERM, SIG_DFL); 6964709Seric 6974709Seric /* arrange to filter standard & diag output of command */ 6984863Seric if (clever) 6994709Seric { 7004863Seric (void) close(rpvect[0]); 7014709Seric (void) close(1); 7024863Seric (void) dup(rpvect[1]); 7034863Seric (void) close(rpvect[1]); 7044863Seric } 7054863Seric else if (OutChannel != stdout) 7064863Seric { 7074863Seric (void) close(1); 7084709Seric (void) dup(fileno(OutChannel)); 7094709Seric } 7104082Seric (void) close(2); 7114082Seric (void) dup(1); 7124709Seric 7134709Seric /* arrange to get standard input */ 7144709Seric (void) close(mpvect[1]); 7154082Seric (void) close(0); 7164709Seric if (dup(mpvect[0]) < 0) 717294Seric { 7182898Seric syserr("Cannot dup to zero!"); 7192898Seric _exit(EX_OSERR); 720294Seric } 7214709Seric (void) close(mpvect[0]); 7222968Seric if (!bitset(M_RESTR, m->m_flags)) 7234215Seric { 7244417Seric if (ctladdr->q_uid == 0) 7254417Seric { 7264417Seric (void) setgid(DefGid); 7274417Seric (void) setuid(DefUid); 7284417Seric } 7294417Seric else 7304415Seric { 7314417Seric (void) setgid(ctladdr->q_gid); 7324417Seric (void) setuid(ctladdr->q_uid); 7334415Seric } 7344215Seric } 7352774Seric # ifndef VFORK 7362774Seric /* 7372774Seric ** We have to be careful with vfork - we can't mung up the 7382774Seric ** memory but we don't want the mailer to inherit any extra 7392774Seric ** open files. Chances are the mailer won't 7402774Seric ** care about an extra file, but then again you never know. 7412774Seric ** Actually, we would like to close(fileno(pwf)), but it's 7422774Seric ** declared static so we can't. But if we fclose(pwf), which 7432774Seric ** is what endpwent does, it closes it in the parent too and 7442774Seric ** the next getpwnam will be slower. If you have a weird 7452774Seric ** mailer that chokes on the extra file you should do the 7462774Seric ** endpwent(). 7472774Seric ** 7482774Seric ** Similar comments apply to log. However, openlog is 7492774Seric ** clever enough to set the FIOCLEX mode on the file, 7502774Seric ** so it will be closed automatically on the exec. 7512774Seric */ 7522774Seric 7532774Seric endpwent(); 754294Seric # ifdef LOG 7552089Seric closelog(); 756294Seric # endif LOG 7572774Seric # endif VFORK 7586038Seric 7596038Seric /* try to execute the mailer */ 760294Seric execv(m->m_mailer, pvp); 7616038Seric 762294Seric /* syserr fails because log is closed */ 763294Seric /* syserr("Cannot exec %s", m->m_mailer); */ 7644214Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 7654082Seric (void) fflush(stdout); 7661619Seric _exit(EX_UNAVAILABLE); 767294Seric } 768294Seric 7694709Seric /* 7704863Seric ** Set up return value. 7714709Seric */ 7724709Seric 7734709Seric (void) close(mpvect[0]); 7744709Seric mfile = fdopen(mpvect[1], "w"); 7754863Seric if (clever) 7764863Seric { 7774863Seric (void) close(rpvect[1]); 7784863Seric rfile = fdopen(rpvect[0], "r"); 7794863Seric } 780294Seric 7814863Seric *pmfile = mfile; 7824863Seric *prfile = rfile; 783294Seric 7844863Seric return (pid); 785294Seric } 786294Seric /* 787294Seric ** GIVERESPONSE -- Interpret an error response from a mailer 788294Seric ** 789294Seric ** Parameters: 790294Seric ** stat -- the status code from the mailer (high byte 791294Seric ** only; core dumps must have been taken care of 792294Seric ** already). 793294Seric ** force -- if set, force an error message output, even 794294Seric ** if the mailer seems to like to print its own 795294Seric ** messages. 796294Seric ** m -- the mailer descriptor for this mailer. 797294Seric ** 798294Seric ** Returns: 7994082Seric ** none. 800294Seric ** 801294Seric ** Side Effects: 8021518Seric ** Errors may be incremented. 803294Seric ** ExitStat may be set. 804294Seric */ 805294Seric 806294Seric giveresponse(stat, force, m) 807294Seric int stat; 8087228Seric bool force; 809294Seric register struct mailer *m; 810294Seric { 811294Seric register char *statmsg; 812294Seric extern char *SysExMsg[]; 813294Seric register int i; 814294Seric extern int N_SysEx; 8151624Seric char buf[30]; 816294Seric 8174315Seric /* 8184315Seric ** Compute status message from code. 8194315Seric */ 8204315Seric 821294Seric i = stat - EX__BASE; 822294Seric if (i < 0 || i > N_SysEx) 823294Seric statmsg = NULL; 824294Seric else 825294Seric statmsg = SysExMsg[i]; 826294Seric if (stat == 0) 8274065Seric { 828*7875Seric statmsg = "sent"; 8297052Seric message(Arpa_Info, statmsg); 8304065Seric } 8315179Seric # ifdef QUEUE 8324621Seric else if (stat == EX_TEMPFAIL) 8334621Seric { 834*7875Seric message(Arpa_Info, "deferred"); 8354621Seric } 8365179Seric # endif QUEUE 837294Seric else 838294Seric { 8391518Seric Errors++; 8406047Seric FatalErrors = TRUE; 841294Seric if (statmsg == NULL && m->m_badstat != 0) 842294Seric { 843294Seric stat = m->m_badstat; 844294Seric i = stat - EX__BASE; 845294Seric # ifdef DEBUG 846294Seric if (i < 0 || i >= N_SysEx) 847294Seric syserr("Bad m_badstat %d", stat); 848294Seric else 849294Seric # endif DEBUG 8507344Seric statmsg = SysExMsg[i]; 851294Seric } 852294Seric if (statmsg == NULL) 853294Seric usrerr("unknown mailer response %d", stat); 8544065Seric else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 855294Seric usrerr("%s", statmsg); 8567344Seric else 8577344Seric fprintf(Xscript, "%s\n", statmsg); 858294Seric } 859294Seric 860294Seric /* 861294Seric ** Final cleanup. 862294Seric ** Log a record of the transaction. Compute the new 863294Seric ** ExitStat -- if we already had an error, stick with 864294Seric ** that. 865294Seric */ 866294Seric 8671624Seric if (statmsg == NULL) 8681624Seric { 8694082Seric (void) sprintf(buf, "error %d", stat); 8701624Seric statmsg = buf; 8711624Seric } 8721624Seric 873294Seric # ifdef LOG 8747680Seric if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2)) 8757858Seric { 8767858Seric extern char *pintvl(); 8777858Seric 8787858Seric syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id, 8797858Seric CurEnv->e_to, pintvl(CurTime - CurEnv->e_ctime, TRUE), 8807858Seric statmsg); 8817858Seric } 882294Seric # endif LOG 8835179Seric # ifdef QUEUE 8844621Seric if (stat != EX_TEMPFAIL) 8855179Seric # endif QUEUE 8864621Seric setstat(stat); 887294Seric } 888294Seric /* 8896974Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 890294Seric ** 8916974Seric ** This can be made an arbitrary message separator by changing $l 892294Seric ** 8936974Seric ** One of the ugliest hacks seen by human eyes is 8946974Seric ** contained herein: UUCP wants those stupid 8956974Seric ** "remote from <host>" lines. Why oh why does a 8966974Seric ** well-meaning programmer such as myself have to 8976974Seric ** deal with this kind of antique garbage???? 8986974Seric ** 899294Seric ** Parameters: 9006974Seric ** fp -- the file to output to. 9016974Seric ** m -- the mailer describing this entry. 902294Seric ** 903294Seric ** Returns: 9046974Seric ** none 905294Seric ** 906294Seric ** Side Effects: 9076974Seric ** outputs some text to fp. 908294Seric */ 909294Seric 9106974Seric putfromline(fp, m) 9116974Seric register FILE *fp; 9126974Seric register MAILER *m; 913294Seric { 9146974Seric char buf[MAXLINE]; 915294Seric 9166974Seric if (bitset(M_NHDR, m->m_flags)) 9176974Seric return; 9184315Seric 9196974Seric # ifdef UGLYUUCP 9206974Seric if (bitset(M_UGLYUUCP, m->m_flags)) 9214205Seric { 9226974Seric extern char *macvalue(); 9236974Seric char *sys = macvalue('g'); 9246974Seric char *bang = index(sys, '!'); 9256041Seric 9266974Seric if (bang == NULL) 9276974Seric syserr("No ! in UUCP! (%s)", sys); 9285099Seric else 9296974Seric *bang = '\0'; 9307232Seric expand("From $f $d remote from $g\n", buf, 9316974Seric &buf[sizeof buf - 1], CurEnv); 9326974Seric *bang = '!'; 9336974Seric } 9346974Seric else 9355179Seric # endif UGLYUUCP 9366974Seric expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv); 9377123Seric putline(buf, fp, bitset(M_FULLSMTP, m->m_flags)); 9385981Seric } 9395981Seric /* 9406974Seric ** PUTHEADER -- put the header part of a message from the in-core copy 9415981Seric ** 9425981Seric ** Parameters: 9435981Seric ** fp -- file to put it on. 9445981Seric ** m -- mailer to use. 9456974Seric ** e -- envelope to use. 9465981Seric ** 9475981Seric ** Returns: 9485981Seric ** none. 9495981Seric ** 9505981Seric ** Side Effects: 9515981Seric ** none. 9525981Seric */ 9535981Seric 9546974Seric putheader(fp, m, e) 9555981Seric register FILE *fp; 9565981Seric register struct mailer *m; 9576974Seric register ENVELOPE *e; 9585981Seric { 9595981Seric char buf[BUFSIZ]; 9605981Seric register HDR *h; 9615981Seric extern char *arpadate(); 9625981Seric extern char *capitalize(); 9635981Seric extern char *hvalue(); 9645981Seric extern bool samefrom(); 9655981Seric char *of_line; 9667123Seric char obuf[MAXLINE]; 9677123Seric register char *obp; 9687123Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 9695981Seric 9704447Seric of_line = hvalue("original-from"); 9716974Seric for (h = e->e_header; h != NULL; h = h->h_link) 9721828Seric { 9734315Seric register char *p; 9746974Seric char *origfrom = e->e_origfrom; 9754447Seric bool nooutput; 9764315Seric 9774447Seric nooutput = FALSE; 9783389Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 9794447Seric nooutput = TRUE; 9804447Seric 9814447Seric /* use From: line from message if generated is the same */ 9827756Seric p = h->h_value; 9834370Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL && 9844447Seric strcmp(m->m_from, "$f") == 0 && of_line == NULL) 9853385Seric { 9864370Seric p = origfrom; 9874370Seric origfrom = NULL; 9884370Seric } 9894370Seric else if (bitset(H_DEFAULT, h->h_flags)) 9904370Seric { 9917666Seric /* macro expand value if generated internally */ 9926974Seric expand(h->h_value, buf, &buf[sizeof buf], e); 9933385Seric p = buf; 9943385Seric } 9955913Seric else if (bitset(H_ADDR, h->h_flags)) 9965913Seric { 9975913Seric if (p == NULL || *p == '\0' || nooutput) 9985913Seric continue; 9997756Seric commaize(p, capitalize(h->h_field), fp, e->e_oldstyle, m); 10005913Seric nooutput = TRUE; 10015913Seric } 10024447Seric if (p == NULL || *p == '\0') 10033389Seric continue; 10044209Seric 10054209Seric /* hack, hack -- output Original-From field if different */ 10064447Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL) 10074209Seric { 10084447Seric /* output new Original-From line if needed */ 10094447Seric if (of_line == NULL && !samefrom(p, origfrom)) 10107123Seric { 10117123Seric (void) sprintf(obuf, "Original-From: %s\n", origfrom); 10127123Seric putline(obuf, fp, fullsmtp); 10137123Seric } 10144447Seric if (of_line != NULL && !nooutput && samefrom(p, of_line)) 10154447Seric { 10164447Seric /* delete Original-From: line if redundant */ 10174447Seric p = of_line; 10184447Seric of_line = NULL; 10194447Seric } 10204447Seric } 10214447Seric else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL) 10224447Seric nooutput = TRUE; 10234447Seric 10244447Seric /* finally, output the header line */ 10254447Seric if (!nooutput) 10264447Seric { 10277123Seric (void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p); 10287123Seric putline(obuf, fp, fullsmtp); 10294447Seric h->h_flags |= H_USED; 10304209Seric } 10312898Seric } 1032294Seric } 1033294Seric /* 10347756Seric ** COMMAIZE -- output a header field, making a comma-translated list. 10357756Seric ** 10367756Seric ** Parameters: 10377756Seric ** p -- the field to output. 10387756Seric ** tag -- the tag to associate with it. 10397756Seric ** fp -- file to put it to. 10407756Seric ** oldstyle -- TRUE if this is an old style header. 10417756Seric ** m -- a pointer to the mailer descriptor. 10427756Seric ** 10437756Seric ** Returns: 10447756Seric ** none. 10457756Seric ** 10467756Seric ** Side Effects: 10477756Seric ** outputs "p" to file "fp". 10487756Seric */ 10497756Seric 10507756Seric commaize(p, tag, fp, oldstyle, m) 10517756Seric register char *p; 10527756Seric char *tag; 10537756Seric FILE *fp; 10547756Seric bool oldstyle; 10557756Seric MAILER *m; 10567756Seric { 10577756Seric register int opos; 10587756Seric bool firstone = TRUE; 10597756Seric char obuf[MAXLINE]; 10607756Seric register char *obp; 10617756Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 10627756Seric 10637756Seric /* 10647756Seric ** Output the address list translated by the 10657756Seric ** mailer and with commas. 10667756Seric */ 10677756Seric 10687756Seric # ifdef DEBUG 10697756Seric if (tTd(14, 2)) 10707756Seric printf("commaize(%s: %s)\n", tag, p); 10717756Seric # endif DEBUG 10727756Seric 10737756Seric obp = obuf; 10747756Seric (void) sprintf(obp, "%s: ", tag); 10757756Seric opos = strlen(tag) + 2; 10767756Seric obp += opos; 10777756Seric 10787756Seric /* 10797756Seric ** Run through the list of values. 10807756Seric */ 10817756Seric 10827756Seric while (*p != '\0') 10837756Seric { 10847756Seric register char *name; 10857756Seric extern char *remotename(); 10867756Seric char savechar; 10877756Seric 10887756Seric /* 10897756Seric ** Find the end of the name. New style names 10907756Seric ** end with a comma, old style names end with 10917756Seric ** a space character. However, spaces do not 10927756Seric ** necessarily delimit an old-style name -- at 10937756Seric ** signs mean keep going. 10947756Seric */ 10957756Seric 10967756Seric /* clean up the leading trash in source */ 10977756Seric while (*p != '\0' && (isspace(*p) || *p == ',')) 10987756Seric p++; 10997756Seric name = p; 11007756Seric 11017756Seric /* find end of name */ 11027756Seric while (*p != '\0' && *p != ',') 11037756Seric { 11047756Seric extern bool isatword(); 11057756Seric char *oldp; 11067756Seric 11077756Seric if (!oldstyle || !isspace(*p)) 11087756Seric { 11097756Seric p++; 11107756Seric continue; 11117756Seric } 11127756Seric 11137756Seric /* look to see if we have an at sign */ 11147756Seric oldp = p; 11157756Seric while (*p != '\0' && isspace(*p)) 11167756Seric p++; 11177756Seric 11187756Seric if (*p != '@' && !isatword(p)) 11197756Seric { 11207756Seric p = oldp; 11217756Seric break; 11227756Seric } 11237756Seric p += *p == '@' ? 1 : 2; 11247756Seric while (*p != '\0' && isspace(*p)) 11257756Seric p++; 11267756Seric } 11277756Seric /* at the end of one complete name */ 11287756Seric 11297756Seric /* strip off trailing white space */ 11307756Seric while (p >= name && (isspace(*p) || *p == ',' || *p == '\0')) 11317756Seric p--; 11327756Seric if (++p == name) 11337756Seric continue; 11347756Seric savechar = *p; 11357756Seric *p = '\0'; 11367756Seric 11377756Seric /* translate the name to be relative */ 11387756Seric name = remotename(name, m, FALSE); 11397756Seric if (*name == '\0') 11407756Seric { 11417756Seric *p = savechar; 11427756Seric continue; 11437756Seric } 11447756Seric 11457756Seric /* output the name with nice formatting */ 11467756Seric opos += strlen(name); 11477756Seric if (!firstone) 11487756Seric opos += 2; 11497756Seric if (opos > 78 && !firstone) 11507756Seric { 11517756Seric (void) sprintf(obp, ",\n"); 11527756Seric putline(obuf, fp, fullsmtp); 11537756Seric obp = obuf; 11547756Seric (void) sprintf(obp, " "); 11557756Seric obp += strlen(obp); 11567756Seric opos = 8 + strlen(name); 11577756Seric } 11587756Seric else if (!firstone) 11597756Seric { 11607756Seric (void) sprintf(obp, ", "); 11617756Seric obp += 2; 11627756Seric } 11637756Seric (void) sprintf(obp, "%s", name); 11647756Seric obp += strlen(obp); 11657756Seric firstone = FALSE; 11667756Seric *p = savechar; 11677756Seric } 11687756Seric (void) strcpy(obp, "\n"); 11697756Seric putline(obuf, fp, fullsmtp); 11707756Seric } 11717756Seric /* 11726974Seric ** PUTBODY -- put the body of a message. 11736974Seric ** 11746974Seric ** Parameters: 11756974Seric ** fp -- file to output onto. 11766974Seric ** m -- a mailer descriptor. 11776974Seric ** xdot -- if set, use SMTP hidden dot algorithm. 11786974Seric ** 11796974Seric ** Returns: 11806974Seric ** none. 11816974Seric ** 11826974Seric ** Side Effects: 11836974Seric ** The message is written onto fp. 11846974Seric */ 11856974Seric 11866974Seric putbody(fp, m, xdot) 11876974Seric FILE *fp; 11886974Seric struct mailer *m; 11896974Seric bool xdot; 11906974Seric { 11916974Seric char buf[MAXLINE + 1]; 11927123Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 11936974Seric 11946974Seric /* 11956974Seric ** Output the body of the message 11966974Seric */ 11976974Seric 11987003Seric #ifdef lint 11997003Seric /* m will be needed later for complete smtp emulation */ 12007003Seric if (m == NULL) 12017003Seric return; 12027003Seric #endif lint 12037003Seric 12046974Seric if (TempFile != NULL) 12056974Seric { 12066974Seric rewind(TempFile); 12076974Seric buf[0] = '.'; 12086974Seric while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL) 12097123Seric putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp); 12106974Seric 12116974Seric if (ferror(TempFile)) 12126974Seric { 12136974Seric syserr("putbody: read error"); 12146974Seric ExitStat = EX_IOERR; 12156974Seric } 12166974Seric } 12176974Seric 12186974Seric (void) fflush(fp); 12196974Seric if (ferror(fp) && errno != EPIPE) 12206974Seric { 12216974Seric syserr("putbody: write error"); 12226974Seric ExitStat = EX_IOERR; 12236974Seric } 12246974Seric errno = 0; 12256974Seric } 12266974Seric /* 12275936Seric ** ISATWORD -- tell if the word we are pointing to is "at". 12285936Seric ** 12295936Seric ** Parameters: 12305936Seric ** p -- word to check. 12315936Seric ** 12325936Seric ** Returns: 12335936Seric ** TRUE -- if p is the word at. 12345936Seric ** FALSE -- otherwise. 12355936Seric ** 12365936Seric ** Side Effects: 12375936Seric ** none. 12385936Seric */ 12395936Seric 12405936Seric bool 12415936Seric isatword(p) 12425936Seric register char *p; 12435936Seric { 12445936Seric extern char lower(); 12455936Seric 12465936Seric if (lower(p[0]) == 'a' && lower(p[1]) == 't' && 12475936Seric p[2] != '\0' && isspace(p[2])) 12485936Seric return (TRUE); 12495936Seric return (FALSE); 12505936Seric } 12515936Seric /* 12524370Seric ** SAMEFROM -- tell if two text addresses represent the same from address. 12534370Seric ** 12544370Seric ** Parameters: 12554370Seric ** ifrom -- internally generated form of from address. 12564370Seric ** efrom -- external form of from address. 12574370Seric ** 12584370Seric ** Returns: 12594370Seric ** TRUE -- if they convey the same info. 12604370Seric ** FALSE -- if any information has been lost. 12614370Seric ** 12624370Seric ** Side Effects: 12634370Seric ** none. 12644370Seric */ 12654370Seric 12664370Seric bool 12674370Seric samefrom(ifrom, efrom) 12684370Seric char *ifrom; 12694370Seric char *efrom; 12704370Seric { 12714447Seric register char *p; 12724447Seric char buf[MAXNAME + 4]; 12734447Seric 12744447Seric # ifdef DEBUG 12757672Seric if (tTd(3, 8)) 12764447Seric printf("samefrom(%s,%s)-->", ifrom, efrom); 12774447Seric # endif DEBUG 12784447Seric if (strcmp(ifrom, efrom) == 0) 12794447Seric goto success; 12804447Seric p = index(ifrom, '@'); 12814447Seric if (p == NULL) 12824447Seric goto failure; 12834447Seric *p = '\0'; 12847003Seric (void) strcpy(buf, ifrom); 12857003Seric (void) strcat(buf, " at "); 12864447Seric *p++ = '@'; 12877003Seric (void) strcat(buf, p); 12884447Seric if (strcmp(buf, efrom) == 0) 12894447Seric goto success; 12904447Seric 12914447Seric failure: 12924447Seric # ifdef DEBUG 12937672Seric if (tTd(3, 8)) 12944447Seric printf("FALSE\n"); 12954447Seric # endif DEBUG 12964447Seric return (FALSE); 12974447Seric 12984447Seric success: 12994447Seric # ifdef DEBUG 13007672Seric if (tTd(3, 8)) 13014447Seric printf("TRUE\n"); 13024447Seric # endif DEBUG 13034447Seric return (TRUE); 13044370Seric } 13054370Seric /* 1306294Seric ** MAILFILE -- Send a message to a file. 1307294Seric ** 13084327Seric ** If the file has the setuid/setgid bits set, but NO execute 13094327Seric ** bits, sendmail will try to become the owner of that file 13104327Seric ** rather than the real user. Obviously, this only works if 13114327Seric ** sendmail runs as root. 13124327Seric ** 1313294Seric ** Parameters: 1314294Seric ** filename -- the name of the file to send to. 13154397Seric ** ctladdr -- the controlling address header -- includes 13164397Seric ** the userid/groupid to be when sending. 1317294Seric ** 1318294Seric ** Returns: 1319294Seric ** The exit code associated with the operation. 1320294Seric ** 1321294Seric ** Side Effects: 1322294Seric ** none. 1323294Seric */ 1324294Seric 13254397Seric mailfile(filename, ctladdr) 1326294Seric char *filename; 13274397Seric ADDRESS *ctladdr; 1328294Seric { 1329294Seric register FILE *f; 13304214Seric register int pid; 1331294Seric 13324214Seric /* 13334214Seric ** Fork so we can change permissions here. 13344214Seric ** Note that we MUST use fork, not vfork, because of 13354214Seric ** the complications of calling subroutines, etc. 13364214Seric */ 13374067Seric 13384214Seric DOFORK(fork); 13394214Seric 13404214Seric if (pid < 0) 13414214Seric return (EX_OSERR); 13424214Seric else if (pid == 0) 13434214Seric { 13444214Seric /* child -- actually write to file */ 13454327Seric struct stat stb; 13464327Seric 13474215Seric (void) signal(SIGINT, SIG_DFL); 13484215Seric (void) signal(SIGHUP, SIG_DFL); 13494215Seric (void) signal(SIGTERM, SIG_DFL); 13504327Seric umask(OldUmask); 13514327Seric if (stat(filename, &stb) < 0) 13524431Seric stb.st_mode = 0666; 13534327Seric if (bitset(0111, stb.st_mode)) 13544327Seric exit(EX_CANTCREAT); 13554401Seric if (ctladdr == NULL) 13566900Seric ctladdr = &CurEnv->e_from; 13574327Seric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 13584417Seric { 13594417Seric if (ctladdr->q_uid == 0) 13604417Seric (void) setgid(DefGid); 13614417Seric else 13624417Seric (void) setgid(ctladdr->q_gid); 13634417Seric } 13644327Seric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 13654417Seric { 13664417Seric if (ctladdr->q_uid == 0) 13674417Seric (void) setuid(DefUid); 13684417Seric else 13694417Seric (void) setuid(ctladdr->q_uid); 13704417Seric } 13716887Seric f = dfopen(filename, "a"); 13724214Seric if (f == NULL) 13734214Seric exit(EX_CANTCREAT); 13744214Seric 13756974Seric putfromline(f, Mailer[1]); 13766974Seric (*CurEnv->e_puthdr)(f, Mailer[1], CurEnv); 13777123Seric fputs("\n", f); 13786974Seric (*CurEnv->e_putbody)(f, Mailer[1], FALSE); 13794214Seric fputs("\n", f); 13804214Seric (void) fclose(f); 13814214Seric (void) fflush(stdout); 13824417Seric 13836887Seric /* reset ISUID & ISGID bits for paranoid systems */ 13844621Seric (void) chmod(filename, (int) stb.st_mode); 13854214Seric exit(EX_OK); 13864315Seric /*NOTREACHED*/ 13874214Seric } 13884214Seric else 13894214Seric { 13904214Seric /* parent -- wait for exit status */ 13914214Seric register int i; 13924214Seric auto int stat; 13934214Seric 13944214Seric while ((i = wait(&stat)) != pid) 13954214Seric { 13964214Seric if (i < 0) 13974214Seric { 13984214Seric stat = EX_OSERR << 8; 13994214Seric break; 14004214Seric } 14014214Seric } 14024215Seric if ((stat & 0377) != 0) 14034215Seric stat = EX_UNAVAILABLE << 8; 14044214Seric return ((stat >> 8) & 0377); 14054214Seric } 1406294Seric } 14074550Seric /* 14084550Seric ** SENDALL -- actually send all the messages. 14094550Seric ** 14104550Seric ** Parameters: 14117043Seric ** e -- the envelope to send. 14124550Seric ** verifyonly -- if set, only give verification messages. 14134550Seric ** 14144550Seric ** Returns: 14154550Seric ** none. 14164550Seric ** 14174550Seric ** Side Effects: 14184550Seric ** Scans the send lists and sends everything it finds. 14197043Seric ** Delivers any appropriate error messages. 14204550Seric */ 14214550Seric 14227043Seric sendall(e, verifyonly) 14237043Seric ENVELOPE *e; 14244550Seric bool verifyonly; 14254550Seric { 14265008Seric register ADDRESS *q; 14277779Seric bool oldverbose; 14284550Seric 14295032Seric # ifdef DEBUG 14307672Seric if (tTd(13, 2)) 14315032Seric { 14326900Seric printf("\nSend Queue:\n"); 14337043Seric printaddr(e->e_sendqueue, TRUE); 14345032Seric } 14355032Seric # endif DEBUG 14365008Seric 14377043Seric /* 14387043Seric ** Run through the list and send everything. 14397043Seric */ 14407043Seric 14417779Seric oldverbose = Verbose; 14427779Seric if (verifyonly) 14437779Seric Verbose = TRUE; 14447043Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14454550Seric { 14465008Seric if (verifyonly) 14474550Seric { 14486900Seric CurEnv->e_to = q->q_paddr; 14495008Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 14507052Seric message(Arpa_Info, "deliverable"); 14514550Seric } 14525008Seric else 14536974Seric (void) deliver(q); 14544550Seric } 14557779Seric Verbose = oldverbose; 14567043Seric 14577043Seric /* 14587043Seric ** Now run through and check for errors. 14597043Seric */ 14607043Seric 14617043Seric if (verifyonly) 14627043Seric return; 14637043Seric 14647043Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14657043Seric { 14667043Seric register ADDRESS *qq; 14677043Seric 14687043Seric if (bitset(QQUEUEUP, q->q_flags)) 14697043Seric e->e_queueup = TRUE; 14707043Seric if (!bitset(QBADADDR, q->q_flags)) 14717043Seric continue; 14727043Seric 14737043Seric /* we have an address that failed -- find the parent */ 14747043Seric for (qq = q; qq != NULL; qq = qq->q_alias) 14757043Seric { 14767043Seric char obuf[MAXNAME + 6]; 14777043Seric extern char *aliaslookup(); 14787043Seric 14797043Seric /* we can only have owners for local addresses */ 14807043Seric if (!bitset(M_LOCAL, qq->q_mailer->m_flags)) 14817043Seric continue; 14827043Seric 14837043Seric /* see if the owner list exists */ 14847043Seric (void) strcpy(obuf, "owner-"); 14857047Seric if (strncmp(qq->q_user, "owner-", 6) == 0) 14867047Seric (void) strcat(obuf, "owner"); 14877047Seric else 14887047Seric (void) strcat(obuf, qq->q_user); 14897043Seric if (aliaslookup(obuf) == NULL) 14907043Seric continue; 14917043Seric 14927043Seric /* owner list exists -- add it to the error queue */ 14937043Seric qq->q_flags &= ~QPRIMARY; 14947043Seric sendto(obuf, 1, qq, &e->e_errorqueue); 14957043Seric MailBack = TRUE; 14967043Seric break; 14977043Seric } 14987043Seric 14997043Seric /* if we did not find an owner, send to the sender */ 15007043Seric if (qq == NULL) 15017043Seric sendto(e->e_from.q_paddr, 1, qq, &e->e_errorqueue); 15027043Seric } 15034550Seric } 15047043Seric /* 15057043Seric ** CHECKERRORS -- check a queue of addresses and process errors. 15067043Seric ** 15077043Seric ** Parameters: 15087043Seric ** e -- the envelope to check. 15097043Seric ** 15107043Seric ** Returns: 15117043Seric ** none. 15127043Seric ** 15137043Seric ** Side Effects: 15147043Seric ** Arranges to queue all tempfailed messages in q 15157043Seric ** or deliver error responses. 15167043Seric */ 15177043Seric 15187043Seric checkerrors(e) 15197043Seric register ENVELOPE *e; 15207043Seric { 15217808Seric register ADDRESS *q; 15227808Seric 15237043Seric # ifdef DEBUG 15247672Seric if (tTd(4, 1)) 15257043Seric { 15267371Seric printf("\ncheckerrors: FatalErrors %d, errorqueue:\n"); 15277043Seric printaddr(e->e_errorqueue, TRUE); 15287043Seric } 15297043Seric # endif DEBUG 15307043Seric 15317043Seric /* mail back the transcript on errors */ 15327043Seric if (FatalErrors) 15337043Seric savemail(); 15347043Seric 15357043Seric /* queue up anything laying around */ 15367858Seric if (e->e_dontqueue) 15377858Seric return; 15387808Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 15397043Seric { 15407808Seric if (bitset(QQUEUEUP, q->q_flags)) 15417808Seric { 15427043Seric # ifdef QUEUE 15437808Seric queueup(e, FALSE); 15447043Seric # else QUEUE 15457808Seric syserr("checkerrors: trying to queue %s", e->e_df); 15467043Seric # endif QUEUE 15477808Seric break; 15487808Seric } 15497043Seric } 15507043Seric } 1551