1294Seric # include <signal.h> 24123Seric # include <errno.h> 34621Seric # include "sendmail.h" 44327Seric # include <sys/stat.h> 5294Seric 6*8003Seric SCCSID(@(#)deliver.c 3.106 08/31/82); 7405Seric 8294Seric /* 94315Seric ** DELIVER -- Deliver a message to a list of addresses. 10294Seric ** 114315Seric ** This routine delivers to everyone on the same host as the 124315Seric ** user on the head of the list. It is clever about mailers 134315Seric ** that don't handle multiple users. It is NOT guaranteed 144315Seric ** that it will deliver to all these addresses however -- so 154315Seric ** deliver should be called once for each address on the 164315Seric ** list. 174315Seric ** 18294Seric ** Parameters: 194621Seric ** firstto -- head of the address list to deliver to. 20294Seric ** 21294Seric ** Returns: 22294Seric ** zero -- successfully delivered. 23294Seric ** else -- some failure, see ExitStat for more info. 24294Seric ** 25294Seric ** Side Effects: 26294Seric ** The standard input is passed off to someone. 27294Seric */ 28294Seric 296974Seric deliver(firstto) 304621Seric ADDRESS *firstto; 31294Seric { 324452Seric char *host; /* host being sent to */ 334452Seric char *user; /* user being sent to */ 34294Seric char **pvp; 353233Seric register char **mvp; 363233Seric register char *p; 374452Seric register struct mailer *m; /* mailer for this recipient */ 382968Seric extern bool checkcompat(); 393233Seric char *pv[MAXPV+1]; 404452Seric char tobuf[MAXLINE]; /* text line of to people */ 413233Seric char buf[MAXNAME]; 424397Seric ADDRESS *ctladdr; 434397Seric extern ADDRESS *getctladdr(); 444452Seric char tfrombuf[MAXNAME]; /* translated from person */ 454452Seric extern char **prescan(); 464621Seric register ADDRESS *to = firstto; 474863Seric bool clever = FALSE; /* running user smtp to this mailer */ 485032Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 497052Seric bool notopen = TRUE; /* set if connection not quite open */ 50*8003Seric register int rcode; /* response code */ 51294Seric 524488Seric errno = 0; 537052Seric if (bitset(QDONTSEND, to->q_flags)) 543233Seric return (0); 55294Seric 566974Seric m = to->q_mailer; 576974Seric host = to->q_host; 586974Seric 59294Seric # ifdef DEBUG 607672Seric if (tTd(10, 1)) 613233Seric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 626974Seric m->m_mno, host, to->q_user); 63294Seric # endif DEBUG 64294Seric 65294Seric /* 665903Seric ** If this mailer is expensive, and if we don't want to make 675903Seric ** connections now, just mark these addresses and return. 685903Seric ** This is useful if we want to batch connections to 695903Seric ** reduce load. This will cause the messages to be 705903Seric ** queued up, and a daemon will come along to send the 715903Seric ** messages later. 725903Seric ** This should be on a per-mailer basis. 735903Seric */ 745903Seric 755903Seric if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags)) 765903Seric { 775903Seric for (; to != NULL; to = to->q_next) 787875Seric if (!bitset(QDONTSEND, to->q_flags) && 797875Seric to->q_mailer == firstto->q_mailer) 805903Seric to->q_flags |= QQUEUEUP|QDONTSEND; 815903Seric return (0); 825903Seric } 835903Seric 845903Seric /* 853233Seric ** Do initial argv setup. 863233Seric ** Insert the mailer name. Notice that $x expansion is 873233Seric ** NOT done on the mailer name. Then, if the mailer has 883233Seric ** a picky -f flag, we insert it as appropriate. This 893233Seric ** code does not check for 'pv' overflow; this places a 903233Seric ** manifest lower limit of 4 for MAXPV. 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 */ 244*8003Seric rcode = 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 257*8003Seric if (rcode == EX_OK) 258*8003Seric rcode = smtprcpt(to); 259*8003Seric if (rcode != EX_OK) 2606059Seric { 261*8003Seric if (rcode == EX_TEMPFAIL) 2626059Seric to->q_flags |= QQUEUEUP; 2636059Seric else 2646059Seric to->q_flags |= QBADADDR; 265*8003Seric giveresponse(rcode, TRUE, m); 2666059Seric } 2676059Seric # else SMTP 2686059Seric syserr("trying to be clever"); 2696059Seric # endif SMTP 2706059Seric } 2716059Seric 2726059Seric /* 2734161Seric ** If an error message has already been given, don't 2744161Seric ** bother to send to this address. 2754161Seric ** 2764161Seric ** >>>>>>>>>> This clause assumes that the local mailer 2774161Seric ** >> NOTE >> cannot do any further aliasing; that 2784161Seric ** >>>>>>>>>> function is subsumed by sendmail. 2794161Seric */ 2804161Seric 2817293Seric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 2824161Seric continue; 2834161Seric 2844283Seric /* save statistics.... */ 2854596Seric Stat.stat_nt[to->q_mailer->m_mno]++; 2866900Seric Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize); 2874283Seric 2884161Seric /* 2893233Seric ** See if this user name is "special". 2903233Seric ** If the user name has a slash in it, assume that this 2916974Seric ** is a file -- send it off without further ado. Note 2926974Seric ** that this type of addresses is not processed along 2936974Seric ** with the others, so we fudge on the To person. 2943233Seric */ 2953233Seric 2964596Seric if (m == LocalMailer) 2973233Seric { 2985599Seric if (user[0] == '/') 299294Seric { 300*8003Seric rcode = mailfile(user, getctladdr(to)); 301*8003Seric giveresponse(rcode, TRUE, m); 3023233Seric continue; 303294Seric } 304294Seric } 3053233Seric 3064315Seric /* 3074315Seric ** Address is verified -- add this user to mailer 3084315Seric ** argv, and add it to the print list of recipients. 3094315Seric */ 3104315Seric 3116059Seric /* link together the chain of recipients */ 3126272Seric to->q_tchain = tochain; 3136272Seric tochain = to; 3146059Seric 3153233Seric /* create list of users for error messages */ 3163233Seric if (tobuf[0] != '\0') 3174082Seric (void) strcat(tobuf, ","); 3184082Seric (void) strcat(tobuf, to->q_paddr); 3193233Seric define('u', user); /* to user */ 3204078Seric define('z', to->q_home); /* user's home */ 3213233Seric 3224863Seric /* 3236059Seric ** Expand out this user into argument list. 3244863Seric */ 3254863Seric 3266059Seric if (!clever) 3273233Seric { 3286974Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 3294863Seric *pvp++ = newstr(buf); 3304863Seric if (pvp >= &pv[MAXPV - 2]) 3314863Seric { 3324863Seric /* allow some space for trailing parms */ 3334863Seric break; 3344863Seric } 3354863Seric } 336294Seric } 337294Seric 3384067Seric /* see if any addresses still exist */ 3394067Seric if (tobuf[0] == '\0') 3404863Seric { 3415179Seric # ifdef SMTP 3424863Seric if (clever) 3437228Seric smtpquit(pv[0], FALSE); 3445179Seric # endif SMTP 3457003Seric define('g', (char *) NULL); 3464067Seric return (0); 3474863Seric } 3484067Seric 3493233Seric /* print out messages as full list */ 3506900Seric CurEnv->e_to = tobuf; 3513233Seric 352294Seric /* 3533233Seric ** Fill out any parameters after the $u parameter. 354294Seric */ 355294Seric 3564863Seric while (!clever && *++mvp != NULL) 357294Seric { 3586974Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 3593233Seric *pvp++ = newstr(buf); 3603233Seric if (pvp >= &pv[MAXPV]) 3613233Seric syserr("deliver: pv overflow after $u for %s", pv[0]); 362294Seric } 3633233Seric *pvp++ = NULL; 364294Seric 365294Seric /* 366294Seric ** Call the mailer. 3672898Seric ** The argument vector gets built, pipes 368294Seric ** are created as necessary, and we fork & exec as 3692898Seric ** appropriate. 3704863Seric ** If we are running SMTP, we just need to clean up. 371294Seric */ 372294Seric 3734397Seric if (ctladdr == NULL) 3746900Seric ctladdr = &CurEnv->e_from; 3755179Seric # ifdef SMTP 3764863Seric if (clever) 3774863Seric { 378*8003Seric rcode = smtpfinish(m, CurEnv); 379*8003Seric if (rcode != EX_OK) 380*8003Seric giveresponse(rcode, TRUE, m); 381*8003Seric smtpquit(pv[0], rcode == EX_OK); 3824863Seric } 3834863Seric else 3845179Seric # endif SMTP 385*8003Seric rcode = sendoff(m, pv, ctladdr); 3863233Seric 3874621Seric /* 3884621Seric ** If we got a temporary failure, arrange to queue the 3894621Seric ** addressees. 3904621Seric */ 3914621Seric 392*8003Seric if (rcode == EX_TEMPFAIL) 3934621Seric { 3945032Seric for (to = tochain; to != NULL; to = to->q_tchain) 3954621Seric to->q_flags |= QQUEUEUP; 3964621Seric } 3974621Seric 3984488Seric errno = 0; 3997003Seric define('g', (char *) NULL); 400*8003Seric return (rcode); 4013233Seric } 4023233Seric /* 4034214Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 4044214Seric ** 4054214Seric ** This MUST be a macro, since after a vfork we are running 4064214Seric ** two processes on the same stack!!! 4074214Seric ** 4084214Seric ** Parameters: 4094214Seric ** none. 4104214Seric ** 4114214Seric ** Returns: 4124214Seric ** From a macro??? You've got to be kidding! 4134214Seric ** 4144214Seric ** Side Effects: 4154214Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 4164214Seric ** pid of child in parent, zero in child. 4174214Seric ** -1 on unrecoverable error. 4184214Seric ** 4194214Seric ** Notes: 4204214Seric ** I'm awfully sorry this looks so awful. That's 4214214Seric ** vfork for you..... 4224214Seric */ 4234214Seric 4244214Seric # define NFORKTRIES 5 4254214Seric # ifdef VFORK 4264214Seric # define XFORK vfork 4274214Seric # else VFORK 4284214Seric # define XFORK fork 4294214Seric # endif VFORK 4304214Seric 4314214Seric # define DOFORK(fORKfN) \ 4324214Seric {\ 4334214Seric register int i;\ 4344214Seric \ 4354214Seric for (i = NFORKTRIES; i-- > 0; )\ 4364214Seric {\ 4374214Seric pid = fORKfN();\ 4384214Seric if (pid >= 0)\ 4394214Seric break;\ 4407003Seric sleep(NFORKTRIES - i);\ 4414214Seric }\ 4424214Seric } 4434214Seric /* 4444637Seric ** DOFORK -- simple fork interface to DOFORK. 4454637Seric ** 4464637Seric ** Parameters: 4474637Seric ** none. 4484637Seric ** 4494637Seric ** Returns: 4504637Seric ** pid of child in parent. 4514637Seric ** zero in child. 4524637Seric ** -1 on error. 4534637Seric ** 4544637Seric ** Side Effects: 4554637Seric ** returns twice, once in parent and once in child. 4564637Seric */ 4574637Seric 4584637Seric dofork() 4594637Seric { 4604637Seric register int pid; 4614637Seric 4624637Seric DOFORK(fork); 4634637Seric return (pid); 4644637Seric } 4654637Seric /* 4663233Seric ** SENDOFF -- send off call to mailer & collect response. 4673233Seric ** 4683233Seric ** Parameters: 4693233Seric ** m -- mailer descriptor. 4703233Seric ** pvp -- parameter vector to send to it. 4714397Seric ** ctladdr -- an address pointer controlling the 4724397Seric ** user/groupid etc. of the mailer. 4733233Seric ** 4743233Seric ** Returns: 4753233Seric ** exit status of mailer. 4763233Seric ** 4773233Seric ** Side Effects: 4783233Seric ** none. 4793233Seric */ 4803233Seric 4816974Seric sendoff(m, pvp, ctladdr) 4823233Seric struct mailer *m; 4833233Seric char **pvp; 4844397Seric ADDRESS *ctladdr; 4853233Seric { 4864863Seric auto FILE *mfile; 4874863Seric auto FILE *rfile; 4883233Seric register int i; 4893233Seric int pid; 4904863Seric 4914863Seric /* 4924863Seric ** Create connection to mailer. 4934863Seric */ 4944863Seric 4954863Seric pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 4964863Seric if (pid < 0) 4974863Seric return (-1); 4984863Seric 4994863Seric /* 5004863Seric ** Format and send message. 5014863Seric */ 5024863Seric 5034863Seric (void) signal(SIGPIPE, SIG_IGN); 5046974Seric putfromline(mfile, m); 5056974Seric (*CurEnv->e_puthdr)(mfile, m, CurEnv); 5066974Seric fprintf(mfile, "\n"); 5076974Seric (*CurEnv->e_putbody)(mfile, m, FALSE); 5084863Seric (void) fclose(mfile); 5094863Seric 5104863Seric i = endmailer(pid, pvp[0]); 5114863Seric giveresponse(i, TRUE, m); 5125981Seric 5135981Seric /* arrange a return receipt if requested */ 5146900Seric if (CurEnv->e_retreceipt && bitset(M_LOCAL, m->m_flags) && i == EX_OK) 5155981Seric { 5166900Seric CurEnv->e_sendreceipt = TRUE; 5176900Seric fprintf(Xscript, "%s... successfully delivered\n", CurEnv->e_to); 5185981Seric /* do we want to send back more info? */ 5195981Seric } 5205981Seric 5214863Seric return (i); 5224863Seric } 5234863Seric /* 5244863Seric ** ENDMAILER -- Wait for mailer to terminate. 5254863Seric ** 5264863Seric ** We should never get fatal errors (e.g., segmentation 5274863Seric ** violation), so we report those specially. For other 5284863Seric ** errors, we choose a status message (into statmsg), 5294863Seric ** and if it represents an error, we print it. 5304863Seric ** 5314863Seric ** Parameters: 5324863Seric ** pid -- pid of mailer. 5334863Seric ** name -- name of mailer (for error messages). 5344863Seric ** 5354863Seric ** Returns: 5364863Seric ** exit code of mailer. 5374863Seric ** 5384863Seric ** Side Effects: 5394863Seric ** none. 5404863Seric */ 5414863Seric 5424863Seric endmailer(pid, name) 5434863Seric int pid; 5444863Seric char *name; 5454863Seric { 5464863Seric register int i; 5474863Seric auto int st; 5484863Seric 5496038Seric /* in the IPC case there is nothing to wait for */ 5506038Seric if (pid == 0) 5516038Seric return (EX_OK); 5526038Seric 5536038Seric /* wait for the mailer process to die and collect status */ 5544863Seric while ((i = wait(&st)) > 0 && i != pid) 5554863Seric continue; 5564863Seric if (i < 0) 5574863Seric { 5584863Seric syserr("wait"); 5594863Seric return (-1); 5604863Seric } 5616038Seric 5626038Seric /* see if it died a horrid death */ 5634863Seric if ((st & 0377) != 0) 5644863Seric { 5654863Seric syserr("%s: stat %o", name, st); 5664863Seric ExitStat = EX_UNAVAILABLE; 5674863Seric return (-1); 5684863Seric } 5696038Seric 5706038Seric /* normal death -- return status */ 5714863Seric i = (st >> 8) & 0377; 5724863Seric return (i); 5734863Seric } 5744863Seric /* 5754863Seric ** OPENMAILER -- open connection to mailer. 5764863Seric ** 5774863Seric ** Parameters: 5784863Seric ** m -- mailer descriptor. 5794863Seric ** pvp -- parameter vector to pass to mailer. 5804863Seric ** ctladdr -- controlling address for user. 5814863Seric ** clever -- create a full duplex connection. 5824863Seric ** pmfile -- pointer to mfile (to mailer) connection. 5834863Seric ** prfile -- pointer to rfile (from mailer) connection. 5844863Seric ** 5854863Seric ** Returns: 5866038Seric ** pid of mailer ( > 0 ). 5874863Seric ** -1 on error. 5886038Seric ** zero on an IPC connection. 5894863Seric ** 5904863Seric ** Side Effects: 5914863Seric ** creates a mailer in a subprocess. 5924863Seric */ 5934863Seric 5944863Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 5954863Seric struct mailer *m; 5964863Seric char **pvp; 5974863Seric ADDRESS *ctladdr; 5984863Seric bool clever; 5994863Seric FILE **pmfile; 6004863Seric FILE **prfile; 6014863Seric { 6024863Seric int pid; 6034709Seric int mpvect[2]; 6044863Seric int rpvect[2]; 6053233Seric FILE *mfile; 6064863Seric FILE *rfile; 6073233Seric extern FILE *fdopen(); 6083233Seric 6093233Seric # ifdef DEBUG 6107672Seric if (tTd(11, 1)) 611294Seric { 6124863Seric printf("openmailer:\n"); 6133233Seric printav(pvp); 614294Seric } 6153233Seric # endif DEBUG 6164488Seric errno = 0; 6173233Seric 6186038Seric # ifdef DAEMON 6196038Seric /* 6206038Seric ** Deal with the special case of mail handled through an IPC 6216038Seric ** connection. 6226038Seric ** In this case we don't actually fork. We must be 6236038Seric ** running SMTP for this to work. We will return a 6246038Seric ** zero pid to indicate that we are running IPC. 6256038Seric */ 6266038Seric 6276038Seric if (strcmp(m->m_mailer, "[IPC]") == 0) 6286038Seric { 6296038Seric register int i; 6307285Seric register u_short port; 6316038Seric 6326038Seric if (!clever) 6336038Seric syserr("non-clever IPC"); 6346632Seric if (pvp[2] != NULL) 6357285Seric port = atoi(pvp[2]); 6366632Seric else 6377285Seric port = 0; 6387285Seric i = makeconnection(pvp[1], port, pmfile, prfile); 6396038Seric if (i != EX_OK) 6406047Seric { 6416047Seric ExitStat = i; 6426038Seric return (-1); 6436047Seric } 6446038Seric else 6456038Seric return (0); 6466038Seric } 6476038Seric # endif DAEMON 6486038Seric 6492898Seric /* create a pipe to shove the mail through */ 6504709Seric if (pipe(mpvect) < 0) 651294Seric { 6524863Seric syserr("pipe (to mailer)"); 653294Seric return (-1); 654294Seric } 6554863Seric 6565179Seric # ifdef SMTP 6574863Seric /* if this mailer speaks smtp, create a return pipe */ 6584863Seric if (clever && pipe(rpvect) < 0) 6594863Seric { 6604863Seric syserr("pipe (from mailer)"); 6614863Seric (void) close(mpvect[0]); 6624863Seric (void) close(mpvect[1]); 6634863Seric return (-1); 6644863Seric } 6655179Seric # endif SMTP 6664863Seric 6676038Seric /* 6686038Seric ** Actually fork the mailer process. 6696038Seric ** DOFORK is clever about retrying. 6706038Seric */ 6716038Seric 6727672Seric (void) fflush(Xscript); /* for debugging */ 6734214Seric DOFORK(XFORK); 6744327Seric /* pid is set by DOFORK */ 675294Seric if (pid < 0) 676294Seric { 6776038Seric /* failure */ 678294Seric syserr("Cannot fork"); 6794709Seric (void) close(mpvect[0]); 6804709Seric (void) close(mpvect[1]); 6814863Seric if (clever) 6824863Seric { 6834863Seric (void) close(rpvect[0]); 6844863Seric (void) close(rpvect[1]); 6854863Seric } 686294Seric return (-1); 687294Seric } 688294Seric else if (pid == 0) 689294Seric { 690294Seric /* child -- set up input & exec mailer */ 6911621Seric /* make diagnostic output be standard output */ 6924477Seric (void) signal(SIGINT, SIG_IGN); 6934477Seric (void) signal(SIGHUP, SIG_IGN); 6944215Seric (void) signal(SIGTERM, SIG_DFL); 6954709Seric 6964709Seric /* arrange to filter standard & diag output of command */ 6974863Seric if (clever) 6984709Seric { 6994863Seric (void) close(rpvect[0]); 7004709Seric (void) close(1); 7014863Seric (void) dup(rpvect[1]); 7024863Seric (void) close(rpvect[1]); 7034863Seric } 7044863Seric else if (OutChannel != stdout) 7054863Seric { 7064863Seric (void) close(1); 7074709Seric (void) dup(fileno(OutChannel)); 7084709Seric } 7094082Seric (void) close(2); 7104082Seric (void) dup(1); 7114709Seric 7124709Seric /* arrange to get standard input */ 7134709Seric (void) close(mpvect[1]); 7144082Seric (void) close(0); 7154709Seric if (dup(mpvect[0]) < 0) 716294Seric { 7172898Seric syserr("Cannot dup to zero!"); 7182898Seric _exit(EX_OSERR); 719294Seric } 7204709Seric (void) close(mpvect[0]); 7212968Seric if (!bitset(M_RESTR, m->m_flags)) 7224215Seric { 7234417Seric if (ctladdr->q_uid == 0) 7244417Seric { 7254417Seric (void) setgid(DefGid); 7264417Seric (void) setuid(DefUid); 7274417Seric } 7284417Seric else 7294415Seric { 7304417Seric (void) setgid(ctladdr->q_gid); 7314417Seric (void) setuid(ctladdr->q_uid); 7324415Seric } 7334215Seric } 7342774Seric # ifndef VFORK 7352774Seric /* 7362774Seric ** We have to be careful with vfork - we can't mung up the 7372774Seric ** memory but we don't want the mailer to inherit any extra 7382774Seric ** open files. Chances are the mailer won't 7392774Seric ** care about an extra file, but then again you never know. 7402774Seric ** Actually, we would like to close(fileno(pwf)), but it's 7412774Seric ** declared static so we can't. But if we fclose(pwf), which 7422774Seric ** is what endpwent does, it closes it in the parent too and 7432774Seric ** the next getpwnam will be slower. If you have a weird 7442774Seric ** mailer that chokes on the extra file you should do the 7452774Seric ** endpwent(). 7462774Seric ** 7472774Seric ** Similar comments apply to log. However, openlog is 7482774Seric ** clever enough to set the FIOCLEX mode on the file, 7492774Seric ** so it will be closed automatically on the exec. 7502774Seric */ 7512774Seric 7522774Seric endpwent(); 753294Seric # ifdef LOG 7542089Seric closelog(); 755294Seric # endif LOG 7562774Seric # endif VFORK 7576038Seric 7586038Seric /* try to execute the mailer */ 759294Seric execv(m->m_mailer, pvp); 7606038Seric 761294Seric /* syserr fails because log is closed */ 762294Seric /* syserr("Cannot exec %s", m->m_mailer); */ 7634214Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 7644082Seric (void) fflush(stdout); 7651619Seric _exit(EX_UNAVAILABLE); 766294Seric } 767294Seric 7684709Seric /* 7694863Seric ** Set up return value. 7704709Seric */ 7714709Seric 7724709Seric (void) close(mpvect[0]); 7734709Seric mfile = fdopen(mpvect[1], "w"); 7744863Seric if (clever) 7754863Seric { 7764863Seric (void) close(rpvect[1]); 7774863Seric rfile = fdopen(rpvect[0], "r"); 7784863Seric } 779294Seric 7804863Seric *pmfile = mfile; 7814863Seric *prfile = rfile; 782294Seric 7834863Seric return (pid); 784294Seric } 785294Seric /* 786294Seric ** GIVERESPONSE -- Interpret an error response from a mailer 787294Seric ** 788294Seric ** Parameters: 789294Seric ** stat -- the status code from the mailer (high byte 790294Seric ** only; core dumps must have been taken care of 791294Seric ** already). 792294Seric ** force -- if set, force an error message output, even 793294Seric ** if the mailer seems to like to print its own 794294Seric ** messages. 795294Seric ** m -- the mailer descriptor for this mailer. 796294Seric ** 797294Seric ** Returns: 7984082Seric ** none. 799294Seric ** 800294Seric ** Side Effects: 8011518Seric ** Errors may be incremented. 802294Seric ** ExitStat may be set. 803294Seric */ 804294Seric 805294Seric giveresponse(stat, force, m) 806294Seric int stat; 8077228Seric bool force; 808294Seric register struct mailer *m; 809294Seric { 810294Seric register char *statmsg; 811294Seric extern char *SysExMsg[]; 812294Seric register int i; 813294Seric extern int N_SysEx; 8141624Seric char buf[30]; 815294Seric 8164315Seric /* 8174315Seric ** Compute status message from code. 8184315Seric */ 8194315Seric 820294Seric i = stat - EX__BASE; 821294Seric if (i < 0 || i > N_SysEx) 822294Seric statmsg = NULL; 823294Seric else 824294Seric statmsg = SysExMsg[i]; 825294Seric if (stat == 0) 8264065Seric { 827*8003Seric statmsg = "250 sent"; 828*8003Seric message(Arpa_Info, &statmsg[4]); 8294065Seric } 8304621Seric else if (stat == EX_TEMPFAIL) 8314621Seric { 8327875Seric message(Arpa_Info, "deferred"); 8334621Seric } 834294Seric else 835294Seric { 8361518Seric Errors++; 8376047Seric FatalErrors = TRUE; 838294Seric if (statmsg == NULL && m->m_badstat != 0) 839294Seric { 840294Seric stat = m->m_badstat; 841294Seric i = stat - EX__BASE; 842294Seric # ifdef DEBUG 843294Seric if (i < 0 || i >= N_SysEx) 844294Seric syserr("Bad m_badstat %d", stat); 845294Seric else 846294Seric # endif DEBUG 8477344Seric statmsg = SysExMsg[i]; 848294Seric } 849294Seric if (statmsg == NULL) 850294Seric usrerr("unknown mailer response %d", stat); 8514065Seric else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 852*8003Seric usrerr(statmsg); 8537344Seric else 854*8003Seric fprintf(Xscript, "%s\n", &statmsg[4]); 855294Seric } 856294Seric 857294Seric /* 858294Seric ** Final cleanup. 859294Seric ** Log a record of the transaction. Compute the new 860294Seric ** ExitStat -- if we already had an error, stick with 861294Seric ** that. 862294Seric */ 863294Seric 8641624Seric if (statmsg == NULL) 8651624Seric { 866*8003Seric (void) sprintf(buf, "554 error %d", stat); 8671624Seric statmsg = buf; 8681624Seric } 8691624Seric 870294Seric # ifdef LOG 8717680Seric if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2)) 8727858Seric { 8737858Seric extern char *pintvl(); 8747858Seric 8757858Seric syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id, 8767883Seric CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), 877*8003Seric &statmsg[4]); 8787858Seric } 879294Seric # endif LOG 8804621Seric if (stat != EX_TEMPFAIL) 8814621Seric setstat(stat); 882294Seric } 883294Seric /* 8846974Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 885294Seric ** 8866974Seric ** This can be made an arbitrary message separator by changing $l 887294Seric ** 8886974Seric ** One of the ugliest hacks seen by human eyes is 8896974Seric ** contained herein: UUCP wants those stupid 8906974Seric ** "remote from <host>" lines. Why oh why does a 8916974Seric ** well-meaning programmer such as myself have to 8926974Seric ** deal with this kind of antique garbage???? 8936974Seric ** 894294Seric ** Parameters: 8956974Seric ** fp -- the file to output to. 8966974Seric ** m -- the mailer describing this entry. 897294Seric ** 898294Seric ** Returns: 8996974Seric ** none 900294Seric ** 901294Seric ** Side Effects: 9026974Seric ** outputs some text to fp. 903294Seric */ 904294Seric 9056974Seric putfromline(fp, m) 9066974Seric register FILE *fp; 9076974Seric register MAILER *m; 908294Seric { 9096974Seric char buf[MAXLINE]; 910294Seric 9116974Seric if (bitset(M_NHDR, m->m_flags)) 9126974Seric return; 9134315Seric 9146974Seric # ifdef UGLYUUCP 9156974Seric if (bitset(M_UGLYUUCP, m->m_flags)) 9164205Seric { 9176974Seric extern char *macvalue(); 9186974Seric char *sys = macvalue('g'); 9196974Seric char *bang = index(sys, '!'); 9206041Seric 9216974Seric if (bang == NULL) 9226974Seric syserr("No ! in UUCP! (%s)", sys); 9235099Seric else 9246974Seric *bang = '\0'; 9257232Seric expand("From $f $d remote from $g\n", buf, 9266974Seric &buf[sizeof buf - 1], CurEnv); 9276974Seric *bang = '!'; 9286974Seric } 9296974Seric else 9305179Seric # endif UGLYUUCP 9316974Seric expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv); 9327123Seric putline(buf, fp, bitset(M_FULLSMTP, m->m_flags)); 9335981Seric } 9345981Seric /* 9356974Seric ** PUTHEADER -- put the header part of a message from the in-core copy 9365981Seric ** 9375981Seric ** Parameters: 9385981Seric ** fp -- file to put it on. 9395981Seric ** m -- mailer to use. 9406974Seric ** e -- envelope to use. 9415981Seric ** 9425981Seric ** Returns: 9435981Seric ** none. 9445981Seric ** 9455981Seric ** Side Effects: 9465981Seric ** none. 9475981Seric */ 9485981Seric 9496974Seric putheader(fp, m, e) 9505981Seric register FILE *fp; 9515981Seric register struct mailer *m; 9526974Seric register ENVELOPE *e; 9535981Seric { 9545981Seric char buf[BUFSIZ]; 9555981Seric register HDR *h; 9565981Seric extern char *arpadate(); 9575981Seric extern char *capitalize(); 9585981Seric extern char *hvalue(); 9595981Seric extern bool samefrom(); 9605981Seric char *of_line; 9617123Seric char obuf[MAXLINE]; 9627123Seric register char *obp; 9637123Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 9645981Seric 9654447Seric of_line = hvalue("original-from"); 9666974Seric for (h = e->e_header; h != NULL; h = h->h_link) 9671828Seric { 9684315Seric register char *p; 9696974Seric char *origfrom = e->e_origfrom; 9704447Seric bool nooutput; 9714315Seric 9724447Seric nooutput = FALSE; 9733389Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 9744447Seric nooutput = TRUE; 9754447Seric 9764447Seric /* use From: line from message if generated is the same */ 9777756Seric p = h->h_value; 9784370Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL && 9794447Seric strcmp(m->m_from, "$f") == 0 && of_line == NULL) 9803385Seric { 9814370Seric p = origfrom; 9824370Seric origfrom = NULL; 9834370Seric } 9844370Seric else if (bitset(H_DEFAULT, h->h_flags)) 9854370Seric { 9867666Seric /* macro expand value if generated internally */ 9876974Seric expand(h->h_value, buf, &buf[sizeof buf], e); 9883385Seric p = buf; 9893385Seric } 9905913Seric else if (bitset(H_ADDR, h->h_flags)) 9915913Seric { 9925913Seric if (p == NULL || *p == '\0' || nooutput) 9935913Seric continue; 9947756Seric commaize(p, capitalize(h->h_field), fp, e->e_oldstyle, m); 9955913Seric nooutput = TRUE; 9965913Seric } 9974447Seric if (p == NULL || *p == '\0') 9983389Seric continue; 9994209Seric 10004209Seric /* hack, hack -- output Original-From field if different */ 10014447Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL) 10024209Seric { 10034447Seric /* output new Original-From line if needed */ 10044447Seric if (of_line == NULL && !samefrom(p, origfrom)) 10057123Seric { 10067123Seric (void) sprintf(obuf, "Original-From: %s\n", origfrom); 10077123Seric putline(obuf, fp, fullsmtp); 10087123Seric } 10094447Seric if (of_line != NULL && !nooutput && samefrom(p, of_line)) 10104447Seric { 10114447Seric /* delete Original-From: line if redundant */ 10124447Seric p = of_line; 10134447Seric of_line = NULL; 10144447Seric } 10154447Seric } 10164447Seric else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL) 10174447Seric nooutput = TRUE; 10184447Seric 10194447Seric /* finally, output the header line */ 10204447Seric if (!nooutput) 10214447Seric { 10227123Seric (void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p); 10237123Seric putline(obuf, fp, fullsmtp); 10244447Seric h->h_flags |= H_USED; 10254209Seric } 10262898Seric } 1027294Seric } 1028294Seric /* 10297756Seric ** COMMAIZE -- output a header field, making a comma-translated list. 10307756Seric ** 10317756Seric ** Parameters: 10327756Seric ** p -- the field to output. 10337756Seric ** tag -- the tag to associate with it. 10347756Seric ** fp -- file to put it to. 10357756Seric ** oldstyle -- TRUE if this is an old style header. 10367756Seric ** m -- a pointer to the mailer descriptor. 10377756Seric ** 10387756Seric ** Returns: 10397756Seric ** none. 10407756Seric ** 10417756Seric ** Side Effects: 10427756Seric ** outputs "p" to file "fp". 10437756Seric */ 10447756Seric 10457756Seric commaize(p, tag, fp, oldstyle, m) 10467756Seric register char *p; 10477756Seric char *tag; 10487756Seric FILE *fp; 10497756Seric bool oldstyle; 10507756Seric MAILER *m; 10517756Seric { 10527756Seric register int opos; 10537756Seric bool firstone = TRUE; 10547756Seric char obuf[MAXLINE]; 10557756Seric register char *obp; 10567756Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 10577756Seric 10587756Seric /* 10597756Seric ** Output the address list translated by the 10607756Seric ** mailer and with commas. 10617756Seric */ 10627756Seric 10637756Seric # ifdef DEBUG 10647756Seric if (tTd(14, 2)) 10657756Seric printf("commaize(%s: %s)\n", tag, p); 10667756Seric # endif DEBUG 10677756Seric 10687756Seric obp = obuf; 10697756Seric (void) sprintf(obp, "%s: ", tag); 10707756Seric opos = strlen(tag) + 2; 10717756Seric obp += opos; 10727756Seric 10737756Seric /* 10747756Seric ** Run through the list of values. 10757756Seric */ 10767756Seric 10777756Seric while (*p != '\0') 10787756Seric { 10797756Seric register char *name; 10807756Seric extern char *remotename(); 10817756Seric char savechar; 10827973Seric int commentlevel; 10837973Seric bool inquote; 10847756Seric 10857756Seric /* 10867756Seric ** Find the end of the name. New style names 10877756Seric ** end with a comma, old style names end with 10887756Seric ** a space character. However, spaces do not 10897756Seric ** necessarily delimit an old-style name -- at 10907756Seric ** signs mean keep going. 10917756Seric */ 10927756Seric 10937756Seric /* clean up the leading trash in source */ 10947756Seric while (*p != '\0' && (isspace(*p) || *p == ',')) 10957756Seric p++; 10967756Seric name = p; 10977756Seric 10987756Seric /* find end of name */ 10997973Seric commentlevel = 0; 11007973Seric inquote = FALSE; 11017973Seric while (*p != '\0' && (*p != ',' || commentlevel > 0 || inquote)) 11027756Seric { 11037756Seric extern bool isatword(); 11047756Seric char *oldp; 11057756Seric 11067973Seric if (*p == '(') 11077973Seric commentlevel++; 11087973Seric else if (*p == ')' && commentlevel > 0) 11097973Seric commentlevel--; 11107973Seric else if (*p == '"') 11117973Seric inquote = !inquote; 11127756Seric if (!oldstyle || !isspace(*p)) 11137756Seric { 11147756Seric p++; 11157756Seric continue; 11167756Seric } 11177756Seric 11187756Seric /* look to see if we have an at sign */ 11197756Seric oldp = p; 11207756Seric while (*p != '\0' && isspace(*p)) 11217756Seric p++; 11227756Seric 11237756Seric if (*p != '@' && !isatword(p)) 11247756Seric { 11257756Seric p = oldp; 11267756Seric break; 11277756Seric } 11287756Seric p += *p == '@' ? 1 : 2; 11297756Seric while (*p != '\0' && isspace(*p)) 11307756Seric p++; 11317756Seric } 11327756Seric /* at the end of one complete name */ 11337756Seric 11347756Seric /* strip off trailing white space */ 11357756Seric while (p >= name && (isspace(*p) || *p == ',' || *p == '\0')) 11367756Seric p--; 11377756Seric if (++p == name) 11387756Seric continue; 11397756Seric savechar = *p; 11407756Seric *p = '\0'; 11417756Seric 11427756Seric /* translate the name to be relative */ 11437756Seric name = remotename(name, m, FALSE); 11447756Seric if (*name == '\0') 11457756Seric { 11467756Seric *p = savechar; 11477756Seric continue; 11487756Seric } 11497756Seric 11507756Seric /* output the name with nice formatting */ 11517756Seric opos += strlen(name); 11527756Seric if (!firstone) 11537756Seric opos += 2; 11547756Seric if (opos > 78 && !firstone) 11557756Seric { 11567756Seric (void) sprintf(obp, ",\n"); 11577756Seric putline(obuf, fp, fullsmtp); 11587756Seric obp = obuf; 11597756Seric (void) sprintf(obp, " "); 11607756Seric obp += strlen(obp); 11617756Seric opos = 8 + strlen(name); 11627756Seric } 11637756Seric else if (!firstone) 11647756Seric { 11657756Seric (void) sprintf(obp, ", "); 11667756Seric obp += 2; 11677756Seric } 11687756Seric (void) sprintf(obp, "%s", name); 11697756Seric obp += strlen(obp); 11707756Seric firstone = FALSE; 11717756Seric *p = savechar; 11727756Seric } 11737756Seric (void) strcpy(obp, "\n"); 11747756Seric putline(obuf, fp, fullsmtp); 11757756Seric } 11767756Seric /* 11776974Seric ** PUTBODY -- put the body of a message. 11786974Seric ** 11796974Seric ** Parameters: 11806974Seric ** fp -- file to output onto. 11816974Seric ** m -- a mailer descriptor. 11826974Seric ** xdot -- if set, use SMTP hidden dot algorithm. 11836974Seric ** 11846974Seric ** Returns: 11856974Seric ** none. 11866974Seric ** 11876974Seric ** Side Effects: 11886974Seric ** The message is written onto fp. 11896974Seric */ 11906974Seric 11916974Seric putbody(fp, m, xdot) 11926974Seric FILE *fp; 11936974Seric struct mailer *m; 11946974Seric bool xdot; 11956974Seric { 11966974Seric char buf[MAXLINE + 1]; 11977123Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 11986974Seric 11996974Seric /* 12006974Seric ** Output the body of the message 12016974Seric */ 12026974Seric 12037003Seric #ifdef lint 12047003Seric /* m will be needed later for complete smtp emulation */ 12057003Seric if (m == NULL) 12067003Seric return; 12077003Seric #endif lint 12087003Seric 12096974Seric if (TempFile != NULL) 12106974Seric { 12116974Seric rewind(TempFile); 12126974Seric buf[0] = '.'; 12136974Seric while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL) 12147123Seric putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp); 12156974Seric 12166974Seric if (ferror(TempFile)) 12176974Seric { 12186974Seric syserr("putbody: read error"); 12196974Seric ExitStat = EX_IOERR; 12206974Seric } 12216974Seric } 12226974Seric 12236974Seric (void) fflush(fp); 12246974Seric if (ferror(fp) && errno != EPIPE) 12256974Seric { 12266974Seric syserr("putbody: write error"); 12276974Seric ExitStat = EX_IOERR; 12286974Seric } 12296974Seric errno = 0; 12306974Seric } 12316974Seric /* 12325936Seric ** ISATWORD -- tell if the word we are pointing to is "at". 12335936Seric ** 12345936Seric ** Parameters: 12355936Seric ** p -- word to check. 12365936Seric ** 12375936Seric ** Returns: 12385936Seric ** TRUE -- if p is the word at. 12395936Seric ** FALSE -- otherwise. 12405936Seric ** 12415936Seric ** Side Effects: 12425936Seric ** none. 12435936Seric */ 12445936Seric 12455936Seric bool 12465936Seric isatword(p) 12475936Seric register char *p; 12485936Seric { 12495936Seric extern char lower(); 12505936Seric 12515936Seric if (lower(p[0]) == 'a' && lower(p[1]) == 't' && 12525936Seric p[2] != '\0' && isspace(p[2])) 12535936Seric return (TRUE); 12545936Seric return (FALSE); 12555936Seric } 12565936Seric /* 12574370Seric ** SAMEFROM -- tell if two text addresses represent the same from address. 12584370Seric ** 12594370Seric ** Parameters: 12604370Seric ** ifrom -- internally generated form of from address. 12614370Seric ** efrom -- external form of from address. 12624370Seric ** 12634370Seric ** Returns: 12644370Seric ** TRUE -- if they convey the same info. 12654370Seric ** FALSE -- if any information has been lost. 12664370Seric ** 12674370Seric ** Side Effects: 12684370Seric ** none. 12694370Seric */ 12704370Seric 12714370Seric bool 12724370Seric samefrom(ifrom, efrom) 12734370Seric char *ifrom; 12744370Seric char *efrom; 12754370Seric { 12764447Seric register char *p; 12774447Seric char buf[MAXNAME + 4]; 12784447Seric 12794447Seric # ifdef DEBUG 12807672Seric if (tTd(3, 8)) 12814447Seric printf("samefrom(%s,%s)-->", ifrom, efrom); 12824447Seric # endif DEBUG 12834447Seric if (strcmp(ifrom, efrom) == 0) 12844447Seric goto success; 12854447Seric p = index(ifrom, '@'); 12864447Seric if (p == NULL) 12874447Seric goto failure; 12884447Seric *p = '\0'; 12897003Seric (void) strcpy(buf, ifrom); 12907003Seric (void) strcat(buf, " at "); 12914447Seric *p++ = '@'; 12927003Seric (void) strcat(buf, p); 12934447Seric if (strcmp(buf, efrom) == 0) 12944447Seric goto success; 12954447Seric 12964447Seric failure: 12974447Seric # ifdef DEBUG 12987672Seric if (tTd(3, 8)) 12994447Seric printf("FALSE\n"); 13004447Seric # endif DEBUG 13014447Seric return (FALSE); 13024447Seric 13034447Seric success: 13044447Seric # ifdef DEBUG 13057672Seric if (tTd(3, 8)) 13064447Seric printf("TRUE\n"); 13074447Seric # endif DEBUG 13084447Seric return (TRUE); 13094370Seric } 13104370Seric /* 1311294Seric ** MAILFILE -- Send a message to a file. 1312294Seric ** 13134327Seric ** If the file has the setuid/setgid bits set, but NO execute 13144327Seric ** bits, sendmail will try to become the owner of that file 13154327Seric ** rather than the real user. Obviously, this only works if 13164327Seric ** sendmail runs as root. 13174327Seric ** 1318294Seric ** Parameters: 1319294Seric ** filename -- the name of the file to send to. 13204397Seric ** ctladdr -- the controlling address header -- includes 13214397Seric ** the userid/groupid to be when sending. 1322294Seric ** 1323294Seric ** Returns: 1324294Seric ** The exit code associated with the operation. 1325294Seric ** 1326294Seric ** Side Effects: 1327294Seric ** none. 1328294Seric */ 1329294Seric 13304397Seric mailfile(filename, ctladdr) 1331294Seric char *filename; 13324397Seric ADDRESS *ctladdr; 1333294Seric { 1334294Seric register FILE *f; 13354214Seric register int pid; 1336294Seric 13374214Seric /* 13384214Seric ** Fork so we can change permissions here. 13394214Seric ** Note that we MUST use fork, not vfork, because of 13404214Seric ** the complications of calling subroutines, etc. 13414214Seric */ 13424067Seric 13434214Seric DOFORK(fork); 13444214Seric 13454214Seric if (pid < 0) 13464214Seric return (EX_OSERR); 13474214Seric else if (pid == 0) 13484214Seric { 13494214Seric /* child -- actually write to file */ 13504327Seric struct stat stb; 13514327Seric 13524215Seric (void) signal(SIGINT, SIG_DFL); 13534215Seric (void) signal(SIGHUP, SIG_DFL); 13544215Seric (void) signal(SIGTERM, SIG_DFL); 13554327Seric umask(OldUmask); 13564327Seric if (stat(filename, &stb) < 0) 13574431Seric stb.st_mode = 0666; 13584327Seric if (bitset(0111, stb.st_mode)) 13594327Seric exit(EX_CANTCREAT); 13604401Seric if (ctladdr == NULL) 13616900Seric ctladdr = &CurEnv->e_from; 13624327Seric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 13634417Seric { 13644417Seric if (ctladdr->q_uid == 0) 13654417Seric (void) setgid(DefGid); 13664417Seric else 13674417Seric (void) setgid(ctladdr->q_gid); 13684417Seric } 13694327Seric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 13704417Seric { 13714417Seric if (ctladdr->q_uid == 0) 13724417Seric (void) setuid(DefUid); 13734417Seric else 13744417Seric (void) setuid(ctladdr->q_uid); 13754417Seric } 13766887Seric f = dfopen(filename, "a"); 13774214Seric if (f == NULL) 13784214Seric exit(EX_CANTCREAT); 13794214Seric 13806974Seric putfromline(f, Mailer[1]); 13816974Seric (*CurEnv->e_puthdr)(f, Mailer[1], CurEnv); 13827123Seric fputs("\n", f); 13836974Seric (*CurEnv->e_putbody)(f, Mailer[1], FALSE); 13844214Seric fputs("\n", f); 13854214Seric (void) fclose(f); 13864214Seric (void) fflush(stdout); 13874417Seric 13886887Seric /* reset ISUID & ISGID bits for paranoid systems */ 13894621Seric (void) chmod(filename, (int) stb.st_mode); 13904214Seric exit(EX_OK); 13914315Seric /*NOTREACHED*/ 13924214Seric } 13934214Seric else 13944214Seric { 13954214Seric /* parent -- wait for exit status */ 13964214Seric register int i; 13974214Seric auto int stat; 13984214Seric 13994214Seric while ((i = wait(&stat)) != pid) 14004214Seric { 14014214Seric if (i < 0) 14024214Seric { 14034214Seric stat = EX_OSERR << 8; 14044214Seric break; 14054214Seric } 14064214Seric } 14074215Seric if ((stat & 0377) != 0) 14084215Seric stat = EX_UNAVAILABLE << 8; 14094214Seric return ((stat >> 8) & 0377); 14104214Seric } 1411294Seric } 14124550Seric /* 14134550Seric ** SENDALL -- actually send all the messages. 14144550Seric ** 14154550Seric ** Parameters: 14167043Seric ** e -- the envelope to send. 14174550Seric ** verifyonly -- if set, only give verification messages. 14184550Seric ** 14194550Seric ** Returns: 14204550Seric ** none. 14214550Seric ** 14224550Seric ** Side Effects: 14234550Seric ** Scans the send lists and sends everything it finds. 14247043Seric ** Delivers any appropriate error messages. 14254550Seric */ 14264550Seric 14277043Seric sendall(e, verifyonly) 14287043Seric ENVELOPE *e; 14294550Seric bool verifyonly; 14304550Seric { 14315008Seric register ADDRESS *q; 14327779Seric bool oldverbose; 14334550Seric 14345032Seric # ifdef DEBUG 14357672Seric if (tTd(13, 2)) 14365032Seric { 14376900Seric printf("\nSend Queue:\n"); 14387043Seric printaddr(e->e_sendqueue, TRUE); 14395032Seric } 14405032Seric # endif DEBUG 14415008Seric 14427043Seric /* 14437043Seric ** Run through the list and send everything. 14447043Seric */ 14457043Seric 14467779Seric oldverbose = Verbose; 14477779Seric if (verifyonly) 14487779Seric Verbose = TRUE; 14497043Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14504550Seric { 14515008Seric if (verifyonly) 14524550Seric { 14536900Seric CurEnv->e_to = q->q_paddr; 14545008Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 14557052Seric message(Arpa_Info, "deliverable"); 14564550Seric } 14575008Seric else 14586974Seric (void) deliver(q); 14594550Seric } 14607779Seric Verbose = oldverbose; 14617043Seric 14627043Seric /* 14637043Seric ** Now run through and check for errors. 14647043Seric */ 14657043Seric 14667043Seric if (verifyonly) 14677043Seric return; 14687043Seric 14697043Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14707043Seric { 14717043Seric register ADDRESS *qq; 14727043Seric 14737043Seric if (bitset(QQUEUEUP, q->q_flags)) 14747043Seric e->e_queueup = TRUE; 14757043Seric if (!bitset(QBADADDR, q->q_flags)) 14767043Seric continue; 14777043Seric 14787043Seric /* we have an address that failed -- find the parent */ 14797043Seric for (qq = q; qq != NULL; qq = qq->q_alias) 14807043Seric { 14817043Seric char obuf[MAXNAME + 6]; 14827043Seric extern char *aliaslookup(); 14837043Seric 14847043Seric /* we can only have owners for local addresses */ 14857043Seric if (!bitset(M_LOCAL, qq->q_mailer->m_flags)) 14867043Seric continue; 14877043Seric 14887043Seric /* see if the owner list exists */ 14897043Seric (void) strcpy(obuf, "owner-"); 14907047Seric if (strncmp(qq->q_user, "owner-", 6) == 0) 14917047Seric (void) strcat(obuf, "owner"); 14927047Seric else 14937047Seric (void) strcat(obuf, qq->q_user); 14947043Seric if (aliaslookup(obuf) == NULL) 14957043Seric continue; 14967043Seric 14977043Seric /* owner list exists -- add it to the error queue */ 14987043Seric qq->q_flags &= ~QPRIMARY; 14997043Seric sendto(obuf, 1, qq, &e->e_errorqueue); 15007043Seric MailBack = TRUE; 15017043Seric break; 15027043Seric } 15037043Seric 15047043Seric /* if we did not find an owner, send to the sender */ 15057043Seric if (qq == NULL) 15067043Seric sendto(e->e_from.q_paddr, 1, qq, &e->e_errorqueue); 15077043Seric } 15084550Seric } 15097043Seric /* 15107043Seric ** CHECKERRORS -- check a queue of addresses and process errors. 15117043Seric ** 15127043Seric ** Parameters: 15137043Seric ** e -- the envelope to check. 15147043Seric ** 15157043Seric ** Returns: 15167043Seric ** none. 15177043Seric ** 15187043Seric ** Side Effects: 15197043Seric ** Arranges to queue all tempfailed messages in q 15207043Seric ** or deliver error responses. 15217043Seric */ 15227043Seric 15237043Seric checkerrors(e) 15247043Seric register ENVELOPE *e; 15257043Seric { 15267808Seric register ADDRESS *q; 15277808Seric 15287043Seric # ifdef DEBUG 15297672Seric if (tTd(4, 1)) 15307043Seric { 15317371Seric printf("\ncheckerrors: FatalErrors %d, errorqueue:\n"); 15327043Seric printaddr(e->e_errorqueue, TRUE); 15337043Seric } 15347043Seric # endif DEBUG 15357043Seric 15367043Seric /* mail back the transcript on errors */ 15377043Seric if (FatalErrors) 15387043Seric savemail(); 15397043Seric 15407043Seric /* queue up anything laying around */ 15417858Seric if (e->e_dontqueue) 15427858Seric return; 15437808Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 15447043Seric { 15457808Seric if (bitset(QQUEUEUP, q->q_flags)) 15467808Seric { 15477043Seric # ifdef QUEUE 15487808Seric queueup(e, FALSE); 15497043Seric # else QUEUE 15507808Seric syserr("checkerrors: trying to queue %s", e->e_df); 15517043Seric # endif QUEUE 15527808Seric break; 15537808Seric } 15547043Seric } 15557043Seric } 1556