1294Seric # include <signal.h> 24123Seric # include <errno.h> 34621Seric # include "sendmail.h" 44327Seric # include <sys/stat.h> 5294Seric # ifdef LOG 62774Seric # include <syslog.h> 7294Seric # endif LOG 8294Seric 9*7202Seric SCCSID(@(#)deliver.c 3.87 06/16/82); 10405Seric 11294Seric /* 124315Seric ** DELIVER -- Deliver a message to a list of addresses. 13294Seric ** 144315Seric ** This routine delivers to everyone on the same host as the 154315Seric ** user on the head of the list. It is clever about mailers 164315Seric ** that don't handle multiple users. It is NOT guaranteed 174315Seric ** that it will deliver to all these addresses however -- so 184315Seric ** deliver should be called once for each address on the 194315Seric ** list. 204315Seric ** 21294Seric ** Parameters: 224621Seric ** firstto -- head of the address list to deliver to. 23294Seric ** 24294Seric ** Returns: 25294Seric ** zero -- successfully delivered. 26294Seric ** else -- some failure, see ExitStat for more info. 27294Seric ** 28294Seric ** Side Effects: 29294Seric ** The standard input is passed off to someone. 30294Seric */ 31294Seric 326974Seric deliver(firstto) 334621Seric ADDRESS *firstto; 34294Seric { 354452Seric char *host; /* host being sent to */ 364452Seric char *user; /* user being sent to */ 37294Seric char **pvp; 383233Seric register char **mvp; 393233Seric register char *p; 404452Seric register struct mailer *m; /* mailer for this recipient */ 41294Seric register int i; 422968Seric extern bool checkcompat(); 433233Seric char *pv[MAXPV+1]; 444452Seric char tobuf[MAXLINE]; /* text line of to people */ 453233Seric char buf[MAXNAME]; 464397Seric ADDRESS *ctladdr; 474397Seric extern ADDRESS *getctladdr(); 484452Seric char tfrombuf[MAXNAME]; /* translated from person */ 494452Seric extern char **prescan(); 504621Seric register ADDRESS *to = firstto; 514863Seric bool clever = FALSE; /* running user smtp to this mailer */ 524863Seric bool tempfail = FALSE; 535032Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 547052Seric bool notopen = TRUE; /* set if connection not quite open */ 55294Seric 564488Seric errno = 0; 577052Seric if (bitset(QDONTSEND, to->q_flags)) 583233Seric return (0); 59294Seric 606974Seric m = to->q_mailer; 616974Seric host = to->q_host; 626974Seric 63294Seric # ifdef DEBUG 64294Seric if (Debug) 653233Seric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 666974Seric m->m_mno, host, to->q_user); 67294Seric # endif DEBUG 68294Seric 69294Seric /* 705903Seric ** If this mailer is expensive, and if we don't want to make 715903Seric ** connections now, just mark these addresses and return. 725903Seric ** This is useful if we want to batch connections to 735903Seric ** reduce load. This will cause the messages to be 745903Seric ** queued up, and a daemon will come along to send the 755903Seric ** messages later. 765903Seric ** This should be on a per-mailer basis. 775903Seric */ 785903Seric 795903Seric if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags)) 805903Seric { 815903Seric for (; to != NULL; to = to->q_next) 825903Seric if (!bitset(QDONTSEND, to->q_flags)) 835903Seric to->q_flags |= QQUEUEUP|QDONTSEND; 845903Seric return (0); 855903Seric } 865903Seric 875903Seric /* 883233Seric ** Do initial argv setup. 893233Seric ** Insert the mailer name. Notice that $x expansion is 903233Seric ** NOT done on the mailer name. Then, if the mailer has 913233Seric ** a picky -f flag, we insert it as appropriate. This 923233Seric ** code does not check for 'pv' overflow; this places a 933233Seric ** manifest lower limit of 4 for MAXPV. 945903Seric ** We rewrite the from address here, being careful 955903Seric ** to also rewrite it again using ruleset 2 to 965903Seric ** eliminate redundancies. 972968Seric */ 982968Seric 994452Seric /* rewrite from address, using rewriting rules */ 1006974Seric expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv); 1014452Seric mvp = prescan(buf, '\0'); 1024452Seric if (mvp == NULL) 1034452Seric { 1044452Seric syserr("bad mailer from translate \"%s\"", buf); 1054452Seric return (EX_SOFTWARE); 1064452Seric } 1074452Seric rewrite(mvp, 2); 1084452Seric cataddr(mvp, tfrombuf, sizeof tfrombuf); 1094452Seric 1104452Seric define('g', tfrombuf); /* translated sender address */ 1113233Seric define('h', host); /* to host */ 1123233Seric Errors = 0; 1133233Seric pvp = pv; 1143233Seric *pvp++ = m->m_argv[0]; 1152968Seric 1163233Seric /* insert -f or -r flag as appropriate */ 1173233Seric if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag) 1183233Seric { 1193233Seric if (bitset(M_FOPT, m->m_flags)) 1203233Seric *pvp++ = "-f"; 1213233Seric else 1223233Seric *pvp++ = "-r"; 1236974Seric expand("$g", buf, &buf[sizeof buf - 1], CurEnv); 1243233Seric *pvp++ = newstr(buf); 1253233Seric } 126294Seric 127294Seric /* 1283233Seric ** Append the other fixed parts of the argv. These run 1293233Seric ** up to the first entry containing "$u". There can only 1303233Seric ** be one of these, and there are only a few more slots 1313233Seric ** in the pv after it. 132294Seric */ 133294Seric 1343233Seric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 135294Seric { 1363233Seric while ((p = index(p, '$')) != NULL) 1373233Seric if (*++p == 'u') 1383233Seric break; 1393233Seric if (p != NULL) 1403233Seric break; 1413233Seric 1423233Seric /* this entry is safe -- go ahead and process it */ 1436974Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 1443233Seric *pvp++ = newstr(buf); 1453233Seric if (pvp >= &pv[MAXPV - 3]) 1463233Seric { 1473233Seric syserr("Too many parameters to %s before $u", pv[0]); 1483233Seric return (-1); 1493233Seric } 150294Seric } 1514863Seric 1526038Seric /* 1536038Seric ** If we have no substitution for the user name in the argument 1546038Seric ** list, we know that we must supply the names otherwise -- and 1556038Seric ** SMTP is the answer!! 1566038Seric */ 1576038Seric 1583233Seric if (*mvp == NULL) 1594863Seric { 1604863Seric /* running SMTP */ 1615179Seric # ifdef SMTP 1624863Seric clever = TRUE; 1634863Seric *pvp = NULL; 1645179Seric # else SMTP 1656038Seric /* oops! we don't implement SMTP */ 1665179Seric syserr("SMTP style mailer"); 1675179Seric return (EX_SOFTWARE); 1685179Seric # endif SMTP 1694863Seric } 170294Seric 171294Seric /* 1723233Seric ** At this point *mvp points to the argument with $u. We 1733233Seric ** run through our address list and append all the addresses 1743233Seric ** we can. If we run out of space, do not fret! We can 1753233Seric ** always send another copy later. 176294Seric */ 177294Seric 1783233Seric tobuf[0] = '\0'; 1796900Seric CurEnv->e_to = tobuf; 1804397Seric ctladdr = NULL; 1813233Seric for (; to != NULL; to = to->q_next) 182294Seric { 1833233Seric /* avoid sending multiple recipients to dumb mailers */ 1844382Seric if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags)) 1853233Seric break; 1863233Seric 1873233Seric /* if already sent or not for this host, don't send */ 1887052Seric if (bitset(QDONTSEND, to->q_flags) || 1897052Seric strcmp(to->q_host, host) != 0 || 1907052Seric to->q_mailer != firstto->q_mailer) 1913233Seric continue; 1924397Seric 1935032Seric # ifdef DEBUG 1945032Seric if (Debug) 1955032Seric { 1965032Seric printf("\nsend to "); 1975032Seric printaddr(to, FALSE); 1985032Seric } 1995032Seric # endif DEBUG 2005032Seric 2014397Seric /* compute effective uid/gid when sending */ 2024596Seric if (to->q_mailer == ProgMailer) 2034397Seric ctladdr = getctladdr(to); 2044397Seric 2053233Seric user = to->q_user; 2066900Seric CurEnv->e_to = to->q_paddr; 2073233Seric to->q_flags |= QDONTSEND; 2084863Seric if (tempfail) 2095032Seric { 2104863Seric to->q_flags |= QQUEUEUP; 2115032Seric continue; 2125032Seric } 2133233Seric 2143233Seric /* 2153233Seric ** Check to see that these people are allowed to 2163233Seric ** talk to each other. 2173233Seric */ 2183233Seric 2193233Seric if (!checkcompat(to)) 220294Seric { 2213233Seric giveresponse(EX_UNAVAILABLE, TRUE, m); 2223233Seric continue; 223294Seric } 2243233Seric 2253233Seric /* 2264099Seric ** Strip quote bits from names if the mailer is dumb 2274099Seric ** about them. 2283233Seric */ 2293233Seric 2303233Seric if (bitset(M_STRIPQ, m->m_flags)) 231294Seric { 2324099Seric stripquotes(user, TRUE); 2334099Seric stripquotes(host, TRUE); 2343233Seric } 2354099Seric else 2364099Seric { 2374099Seric stripquotes(user, FALSE); 2384099Seric stripquotes(host, FALSE); 2394099Seric } 2403233Seric 2413233Seric /* 2427052Seric ** Do initial connection setup if needed. 2437052Seric */ 2447052Seric 2457052Seric if (notopen) 2467052Seric { 2477052Seric message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name); 2487052Seric # ifdef SMTP 2497052Seric if (clever) 2507052Seric { 2517052Seric /* send the initial SMTP protocol */ 2527052Seric i = smtpinit(m, pv, (ADDRESS *) NULL); 2537052Seric # ifdef QUEUE 2547052Seric if (i == EX_TEMPFAIL) 2557052Seric tempfail = TRUE; 2567052Seric # endif QUEUE 2577052Seric } 2587052Seric # ifdef SMTP 2597052Seric notopen = FALSE; 2607052Seric } 2617052Seric 2627052Seric /* 2636059Seric ** Pass it to the other host if we are running SMTP. 2646059Seric */ 2656059Seric 2666059Seric if (clever) 2676059Seric { 2686059Seric # ifdef SMTP 2696059Seric i = smtprcpt(to); 2706059Seric if (i != EX_OK) 2716059Seric { 2726059Seric # ifdef QUEUE 2736059Seric if (i == EX_TEMPFAIL) 2746059Seric to->q_flags |= QQUEUEUP; 2756059Seric else 2766059Seric # endif QUEUE 2776059Seric { 2786059Seric to->q_flags |= QBADADDR; 2796059Seric giveresponse(i, TRUE, m); 2806059Seric } 2816059Seric } 2826059Seric # else SMTP 2836059Seric syserr("trying to be clever"); 2846059Seric # endif SMTP 2856059Seric } 2866059Seric 2876059Seric /* 2884161Seric ** If an error message has already been given, don't 2894161Seric ** bother to send to this address. 2904161Seric ** 2914161Seric ** >>>>>>>>>> This clause assumes that the local mailer 2924161Seric ** >> NOTE >> cannot do any further aliasing; that 2934161Seric ** >>>>>>>>>> function is subsumed by sendmail. 2944161Seric */ 2954161Seric 2964161Seric if (bitset(QBADADDR, to->q_flags)) 2974161Seric continue; 2984161Seric 2994283Seric /* save statistics.... */ 3004596Seric Stat.stat_nt[to->q_mailer->m_mno]++; 3016900Seric Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize); 3024283Seric 3034161Seric /* 3043233Seric ** See if this user name is "special". 3053233Seric ** If the user name has a slash in it, assume that this 3066974Seric ** is a file -- send it off without further ado. Note 3076974Seric ** that this type of addresses is not processed along 3086974Seric ** with the others, so we fudge on the To person. 3093233Seric */ 3103233Seric 3114596Seric if (m == LocalMailer) 3123233Seric { 3135599Seric if (user[0] == '/') 314294Seric { 3154397Seric i = mailfile(user, getctladdr(to)); 316294Seric giveresponse(i, TRUE, m); 3173233Seric continue; 318294Seric } 319294Seric } 3203233Seric 3214315Seric /* 3224315Seric ** Address is verified -- add this user to mailer 3234315Seric ** argv, and add it to the print list of recipients. 3244315Seric */ 3254315Seric 3266059Seric /* link together the chain of recipients */ 3276272Seric to->q_tchain = tochain; 3286272Seric tochain = to; 3296059Seric 3303233Seric /* create list of users for error messages */ 3313233Seric if (tobuf[0] != '\0') 3324082Seric (void) strcat(tobuf, ","); 3334082Seric (void) strcat(tobuf, to->q_paddr); 3343233Seric define('u', user); /* to user */ 3354078Seric define('z', to->q_home); /* user's home */ 3363233Seric 3374863Seric /* 3386059Seric ** Expand out this user into argument list. 3394863Seric */ 3404863Seric 3416059Seric if (!clever) 3423233Seric { 3436974Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 3444863Seric *pvp++ = newstr(buf); 3454863Seric if (pvp >= &pv[MAXPV - 2]) 3464863Seric { 3474863Seric /* allow some space for trailing parms */ 3484863Seric break; 3494863Seric } 3504863Seric } 351294Seric } 352294Seric 3534067Seric /* see if any addresses still exist */ 3544067Seric if (tobuf[0] == '\0') 3554863Seric { 3565179Seric # ifdef SMTP 3574863Seric if (clever) 3584863Seric smtpquit(pv[0]); 3595179Seric # endif SMTP 3607003Seric define('g', (char *) NULL); 3614067Seric return (0); 3624863Seric } 3634067Seric 3643233Seric /* print out messages as full list */ 3656900Seric CurEnv->e_to = tobuf; 3663233Seric 367294Seric /* 3683233Seric ** Fill out any parameters after the $u parameter. 369294Seric */ 370294Seric 3714863Seric while (!clever && *++mvp != NULL) 372294Seric { 3736974Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 3743233Seric *pvp++ = newstr(buf); 3753233Seric if (pvp >= &pv[MAXPV]) 3763233Seric syserr("deliver: pv overflow after $u for %s", pv[0]); 377294Seric } 3783233Seric *pvp++ = NULL; 379294Seric 380294Seric /* 381294Seric ** Call the mailer. 3822898Seric ** The argument vector gets built, pipes 383294Seric ** are created as necessary, and we fork & exec as 3842898Seric ** appropriate. 3854863Seric ** If we are running SMTP, we just need to clean up. 386294Seric */ 387294Seric 3884397Seric if (ctladdr == NULL) 3896900Seric ctladdr = &CurEnv->e_from; 3905179Seric # ifdef SMTP 3914863Seric if (clever) 3924863Seric { 3936974Seric i = smtpfinish(m, CurEnv); 3944863Seric smtpquit(pv[0]); 3954863Seric } 3964863Seric else 3975179Seric # endif SMTP 3986974Seric i = sendoff(m, pv, ctladdr); 3993233Seric 4004621Seric /* 4014621Seric ** If we got a temporary failure, arrange to queue the 4024621Seric ** addressees. 4034621Seric */ 4044621Seric 4055179Seric # ifdef QUEUE 4064621Seric if (i == EX_TEMPFAIL) 4074621Seric { 4085032Seric for (to = tochain; to != NULL; to = to->q_tchain) 4094621Seric to->q_flags |= QQUEUEUP; 4104621Seric } 4115179Seric # endif QUEUE 4124621Seric 4134488Seric errno = 0; 4147003Seric define('g', (char *) NULL); 4153233Seric return (i); 4163233Seric } 4173233Seric /* 4184214Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 4194214Seric ** 4204214Seric ** This MUST be a macro, since after a vfork we are running 4214214Seric ** two processes on the same stack!!! 4224214Seric ** 4234214Seric ** Parameters: 4244214Seric ** none. 4254214Seric ** 4264214Seric ** Returns: 4274214Seric ** From a macro??? You've got to be kidding! 4284214Seric ** 4294214Seric ** Side Effects: 4304214Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 4314214Seric ** pid of child in parent, zero in child. 4324214Seric ** -1 on unrecoverable error. 4334214Seric ** 4344214Seric ** Notes: 4354214Seric ** I'm awfully sorry this looks so awful. That's 4364214Seric ** vfork for you..... 4374214Seric */ 4384214Seric 4394214Seric # define NFORKTRIES 5 4404214Seric # ifdef VFORK 4414214Seric # define XFORK vfork 4424214Seric # else VFORK 4434214Seric # define XFORK fork 4444214Seric # endif VFORK 4454214Seric 4464214Seric # define DOFORK(fORKfN) \ 4474214Seric {\ 4484214Seric register int i;\ 4494214Seric \ 4504214Seric for (i = NFORKTRIES; i-- > 0; )\ 4514214Seric {\ 4524214Seric pid = fORKfN();\ 4534214Seric if (pid >= 0)\ 4544214Seric break;\ 4557003Seric sleep(NFORKTRIES - i);\ 4564214Seric }\ 4574214Seric } 4584214Seric /* 4594637Seric ** DOFORK -- simple fork interface to DOFORK. 4604637Seric ** 4614637Seric ** Parameters: 4624637Seric ** none. 4634637Seric ** 4644637Seric ** Returns: 4654637Seric ** pid of child in parent. 4664637Seric ** zero in child. 4674637Seric ** -1 on error. 4684637Seric ** 4694637Seric ** Side Effects: 4704637Seric ** returns twice, once in parent and once in child. 4714637Seric */ 4724637Seric 4734637Seric dofork() 4744637Seric { 4754637Seric register int pid; 4764637Seric 4774637Seric DOFORK(fork); 4784637Seric return (pid); 4794637Seric } 4804637Seric /* 4813233Seric ** SENDOFF -- send off call to mailer & collect response. 4823233Seric ** 4833233Seric ** Parameters: 4843233Seric ** m -- mailer descriptor. 4853233Seric ** pvp -- parameter vector to send to it. 4864397Seric ** ctladdr -- an address pointer controlling the 4874397Seric ** user/groupid etc. of the mailer. 4883233Seric ** 4893233Seric ** Returns: 4903233Seric ** exit status of mailer. 4913233Seric ** 4923233Seric ** Side Effects: 4933233Seric ** none. 4943233Seric */ 4953233Seric 4966974Seric sendoff(m, pvp, ctladdr) 4973233Seric struct mailer *m; 4983233Seric char **pvp; 4994397Seric ADDRESS *ctladdr; 5003233Seric { 5014863Seric auto FILE *mfile; 5024863Seric auto FILE *rfile; 5033233Seric register int i; 5043233Seric int pid; 5054863Seric 5064863Seric /* 5074863Seric ** Create connection to mailer. 5084863Seric */ 5094863Seric 5104863Seric pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 5114863Seric if (pid < 0) 5124863Seric return (-1); 5134863Seric 5144863Seric /* 5154863Seric ** Format and send message. 5164863Seric */ 5174863Seric 5184863Seric (void) signal(SIGPIPE, SIG_IGN); 5196974Seric putfromline(mfile, m); 5206974Seric (*CurEnv->e_puthdr)(mfile, m, CurEnv); 5216974Seric fprintf(mfile, "\n"); 5226974Seric (*CurEnv->e_putbody)(mfile, m, FALSE); 5234863Seric (void) fclose(mfile); 5244863Seric 5254863Seric i = endmailer(pid, pvp[0]); 5264863Seric giveresponse(i, TRUE, m); 5275981Seric 5285981Seric /* arrange a return receipt if requested */ 5296900Seric if (CurEnv->e_retreceipt && bitset(M_LOCAL, m->m_flags) && i == EX_OK) 5305981Seric { 5316900Seric CurEnv->e_sendreceipt = TRUE; 5326900Seric fprintf(Xscript, "%s... successfully delivered\n", CurEnv->e_to); 5335981Seric /* do we want to send back more info? */ 5345981Seric } 5355981Seric 5364863Seric return (i); 5374863Seric } 5384863Seric /* 5394863Seric ** ENDMAILER -- Wait for mailer to terminate. 5404863Seric ** 5414863Seric ** We should never get fatal errors (e.g., segmentation 5424863Seric ** violation), so we report those specially. For other 5434863Seric ** errors, we choose a status message (into statmsg), 5444863Seric ** and if it represents an error, we print it. 5454863Seric ** 5464863Seric ** Parameters: 5474863Seric ** pid -- pid of mailer. 5484863Seric ** name -- name of mailer (for error messages). 5494863Seric ** 5504863Seric ** Returns: 5514863Seric ** exit code of mailer. 5524863Seric ** 5534863Seric ** Side Effects: 5544863Seric ** none. 5554863Seric */ 5564863Seric 5574863Seric endmailer(pid, name) 5584863Seric int pid; 5594863Seric char *name; 5604863Seric { 5614863Seric register int i; 5624863Seric auto int st; 5634863Seric 5646038Seric /* in the IPC case there is nothing to wait for */ 5656038Seric if (pid == 0) 5666038Seric return (EX_OK); 5676038Seric 5686038Seric /* wait for the mailer process to die and collect status */ 5694863Seric while ((i = wait(&st)) > 0 && i != pid) 5704863Seric continue; 5714863Seric if (i < 0) 5724863Seric { 5734863Seric syserr("wait"); 5744863Seric return (-1); 5754863Seric } 5766038Seric 5776038Seric /* see if it died a horrid death */ 5784863Seric if ((st & 0377) != 0) 5794863Seric { 5804863Seric syserr("%s: stat %o", name, st); 5814863Seric ExitStat = EX_UNAVAILABLE; 5824863Seric return (-1); 5834863Seric } 5846038Seric 5856038Seric /* normal death -- return status */ 5864863Seric i = (st >> 8) & 0377; 5874863Seric return (i); 5884863Seric } 5894863Seric /* 5904863Seric ** OPENMAILER -- open connection to mailer. 5914863Seric ** 5924863Seric ** Parameters: 5934863Seric ** m -- mailer descriptor. 5944863Seric ** pvp -- parameter vector to pass to mailer. 5954863Seric ** ctladdr -- controlling address for user. 5964863Seric ** clever -- create a full duplex connection. 5974863Seric ** pmfile -- pointer to mfile (to mailer) connection. 5984863Seric ** prfile -- pointer to rfile (from mailer) connection. 5994863Seric ** 6004863Seric ** Returns: 6016038Seric ** pid of mailer ( > 0 ). 6024863Seric ** -1 on error. 6036038Seric ** zero on an IPC connection. 6044863Seric ** 6054863Seric ** Side Effects: 6064863Seric ** creates a mailer in a subprocess. 6074863Seric */ 6084863Seric 6094863Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 6104863Seric struct mailer *m; 6114863Seric char **pvp; 6124863Seric ADDRESS *ctladdr; 6134863Seric bool clever; 6144863Seric FILE **pmfile; 6154863Seric FILE **prfile; 6164863Seric { 6174863Seric int pid; 6184709Seric int mpvect[2]; 6194863Seric int rpvect[2]; 6203233Seric FILE *mfile; 6214863Seric FILE *rfile; 6223233Seric extern FILE *fdopen(); 6233233Seric 6243233Seric # ifdef DEBUG 6253233Seric if (Debug) 626294Seric { 6274863Seric printf("openmailer:\n"); 6283233Seric printav(pvp); 629294Seric } 6303233Seric # endif DEBUG 6314488Seric errno = 0; 6323233Seric 6336038Seric # ifdef DAEMON 6346038Seric /* 6356038Seric ** Deal with the special case of mail handled through an IPC 6366038Seric ** connection. 6376038Seric ** In this case we don't actually fork. We must be 6386038Seric ** running SMTP for this to work. We will return a 6396038Seric ** zero pid to indicate that we are running IPC. 6406038Seric */ 6416038Seric 6426038Seric if (strcmp(m->m_mailer, "[IPC]") == 0) 6436038Seric { 6446038Seric register int i; 6456038Seric 6466038Seric if (!clever) 6476038Seric syserr("non-clever IPC"); 6486632Seric if (pvp[2] != NULL) 6496632Seric i = atoi(pvp[2]); 6506632Seric else 6516632Seric i = 0; 6526632Seric i = makeconnection(pvp[1], i, pmfile, prfile); 6536038Seric if (i != EX_OK) 6546047Seric { 6556047Seric ExitStat = i; 6566038Seric return (-1); 6576047Seric } 6586038Seric else 6596038Seric return (0); 6606038Seric } 6616038Seric # endif DAEMON 6626038Seric 6632898Seric /* create a pipe to shove the mail through */ 6644709Seric if (pipe(mpvect) < 0) 665294Seric { 6664863Seric syserr("pipe (to mailer)"); 667294Seric return (-1); 668294Seric } 6694863Seric 6705179Seric # ifdef SMTP 6714863Seric /* if this mailer speaks smtp, create a return pipe */ 6724863Seric if (clever && pipe(rpvect) < 0) 6734863Seric { 6744863Seric syserr("pipe (from mailer)"); 6754863Seric (void) close(mpvect[0]); 6764863Seric (void) close(mpvect[1]); 6774863Seric return (-1); 6784863Seric } 6795179Seric # endif SMTP 6804863Seric 6816038Seric /* 6826038Seric ** Actually fork the mailer process. 6836038Seric ** DOFORK is clever about retrying. 6846038Seric */ 6856038Seric 6864214Seric DOFORK(XFORK); 6874327Seric /* pid is set by DOFORK */ 688294Seric if (pid < 0) 689294Seric { 6906038Seric /* failure */ 691294Seric syserr("Cannot fork"); 6924709Seric (void) close(mpvect[0]); 6934709Seric (void) close(mpvect[1]); 6944863Seric if (clever) 6954863Seric { 6964863Seric (void) close(rpvect[0]); 6974863Seric (void) close(rpvect[1]); 6984863Seric } 699294Seric return (-1); 700294Seric } 701294Seric else if (pid == 0) 702294Seric { 703294Seric /* child -- set up input & exec mailer */ 7041621Seric /* make diagnostic output be standard output */ 7054477Seric (void) signal(SIGINT, SIG_IGN); 7064477Seric (void) signal(SIGHUP, SIG_IGN); 7074215Seric (void) signal(SIGTERM, SIG_DFL); 7084709Seric 7094709Seric /* arrange to filter standard & diag output of command */ 7104863Seric if (clever) 7114709Seric { 7124863Seric (void) close(rpvect[0]); 7134709Seric (void) close(1); 7144863Seric (void) dup(rpvect[1]); 7154863Seric (void) close(rpvect[1]); 7164863Seric } 7174863Seric else if (OutChannel != stdout) 7184863Seric { 7194863Seric (void) close(1); 7204709Seric (void) dup(fileno(OutChannel)); 7214709Seric } 7224082Seric (void) close(2); 7234082Seric (void) dup(1); 7244709Seric 7254709Seric /* arrange to get standard input */ 7264709Seric (void) close(mpvect[1]); 7274082Seric (void) close(0); 7284709Seric if (dup(mpvect[0]) < 0) 729294Seric { 7302898Seric syserr("Cannot dup to zero!"); 7312898Seric _exit(EX_OSERR); 732294Seric } 7334709Seric (void) close(mpvect[0]); 7342968Seric if (!bitset(M_RESTR, m->m_flags)) 7354215Seric { 7364417Seric if (ctladdr->q_uid == 0) 7374417Seric { 7384417Seric (void) setgid(DefGid); 7394417Seric (void) setuid(DefUid); 7404417Seric } 7414417Seric else 7424415Seric { 7434417Seric (void) setgid(ctladdr->q_gid); 7444417Seric (void) setuid(ctladdr->q_uid); 7454415Seric } 7464215Seric } 7472774Seric # ifndef VFORK 7482774Seric /* 7492774Seric ** We have to be careful with vfork - we can't mung up the 7502774Seric ** memory but we don't want the mailer to inherit any extra 7512774Seric ** open files. Chances are the mailer won't 7522774Seric ** care about an extra file, but then again you never know. 7532774Seric ** Actually, we would like to close(fileno(pwf)), but it's 7542774Seric ** declared static so we can't. But if we fclose(pwf), which 7552774Seric ** is what endpwent does, it closes it in the parent too and 7562774Seric ** the next getpwnam will be slower. If you have a weird 7572774Seric ** mailer that chokes on the extra file you should do the 7582774Seric ** endpwent(). 7592774Seric ** 7602774Seric ** Similar comments apply to log. However, openlog is 7612774Seric ** clever enough to set the FIOCLEX mode on the file, 7622774Seric ** so it will be closed automatically on the exec. 7632774Seric */ 7642774Seric 7652774Seric endpwent(); 766294Seric # ifdef LOG 7672089Seric closelog(); 768294Seric # endif LOG 7692774Seric # endif VFORK 7706038Seric 7716038Seric /* try to execute the mailer */ 772294Seric execv(m->m_mailer, pvp); 7736038Seric 774294Seric /* syserr fails because log is closed */ 775294Seric /* syserr("Cannot exec %s", m->m_mailer); */ 7764214Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 7774082Seric (void) fflush(stdout); 7781619Seric _exit(EX_UNAVAILABLE); 779294Seric } 780294Seric 7814709Seric /* 7824863Seric ** Set up return value. 7834709Seric */ 7844709Seric 7854709Seric (void) close(mpvect[0]); 7864709Seric mfile = fdopen(mpvect[1], "w"); 7874863Seric if (clever) 7884863Seric { 7894863Seric (void) close(rpvect[1]); 7904863Seric rfile = fdopen(rpvect[0], "r"); 7914863Seric } 792294Seric 7934863Seric *pmfile = mfile; 7944863Seric *prfile = rfile; 795294Seric 7964863Seric return (pid); 797294Seric } 798294Seric /* 799294Seric ** GIVERESPONSE -- Interpret an error response from a mailer 800294Seric ** 801294Seric ** Parameters: 802294Seric ** stat -- the status code from the mailer (high byte 803294Seric ** only; core dumps must have been taken care of 804294Seric ** already). 805294Seric ** force -- if set, force an error message output, even 806294Seric ** if the mailer seems to like to print its own 807294Seric ** messages. 808294Seric ** m -- the mailer descriptor for this mailer. 809294Seric ** 810294Seric ** Returns: 8114082Seric ** none. 812294Seric ** 813294Seric ** Side Effects: 8141518Seric ** Errors may be incremented. 815294Seric ** ExitStat may be set. 816294Seric */ 817294Seric 818294Seric giveresponse(stat, force, m) 819294Seric int stat; 820294Seric int force; 821294Seric register struct mailer *m; 822294Seric { 823294Seric register char *statmsg; 824294Seric extern char *SysExMsg[]; 825294Seric register int i; 826294Seric extern int N_SysEx; 8271624Seric char buf[30]; 828294Seric 8294315Seric /* 8304315Seric ** Compute status message from code. 8314315Seric */ 8324315Seric 833294Seric i = stat - EX__BASE; 834294Seric if (i < 0 || i > N_SysEx) 835294Seric statmsg = NULL; 836294Seric else 837294Seric statmsg = SysExMsg[i]; 838294Seric if (stat == 0) 8394065Seric { 8404194Seric if (bitset(M_LOCAL, m->m_flags)) 8414161Seric statmsg = "delivered"; 8424161Seric else 8434161Seric statmsg = "queued"; 8447052Seric message(Arpa_Info, statmsg); 8454065Seric } 8465179Seric # ifdef QUEUE 8474621Seric else if (stat == EX_TEMPFAIL) 8484621Seric { 8497052Seric message(Arpa_Info, "transmission deferred"); 8504621Seric } 8515179Seric # endif QUEUE 852294Seric else 853294Seric { 8541518Seric Errors++; 8556047Seric FatalErrors = TRUE; 856294Seric if (statmsg == NULL && m->m_badstat != 0) 857294Seric { 858294Seric stat = m->m_badstat; 859294Seric i = stat - EX__BASE; 860294Seric # ifdef DEBUG 861294Seric if (i < 0 || i >= N_SysEx) 862294Seric syserr("Bad m_badstat %d", stat); 863294Seric else 864294Seric # endif DEBUG 865294Seric statmsg = SysExMsg[i]; 866294Seric } 867294Seric if (statmsg == NULL) 868294Seric usrerr("unknown mailer response %d", stat); 8694065Seric else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 870294Seric usrerr("%s", statmsg); 871294Seric } 872294Seric 873294Seric /* 874294Seric ** Final cleanup. 875294Seric ** Log a record of the transaction. Compute the new 876294Seric ** ExitStat -- if we already had an error, stick with 877294Seric ** that. 878294Seric */ 879294Seric 8801624Seric if (statmsg == NULL) 8811624Seric { 8824082Seric (void) sprintf(buf, "error %d", stat); 8831624Seric statmsg = buf; 8841624Seric } 8851624Seric 886294Seric # ifdef LOG 8876900Seric syslog(LOG_INFO, "%s->%s: %ld: %s", CurEnv->e_from.q_paddr, CurEnv->e_to, CurEnv->e_msgsize, statmsg); 888294Seric # endif LOG 8895179Seric # ifdef QUEUE 8904621Seric if (stat != EX_TEMPFAIL) 8915179Seric # endif QUEUE 8924621Seric setstat(stat); 893294Seric } 894294Seric /* 8956974Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 896294Seric ** 8976974Seric ** This can be made an arbitrary message separator by changing $l 898294Seric ** 8996974Seric ** One of the ugliest hacks seen by human eyes is 9006974Seric ** contained herein: UUCP wants those stupid 9016974Seric ** "remote from <host>" lines. Why oh why does a 9026974Seric ** well-meaning programmer such as myself have to 9036974Seric ** deal with this kind of antique garbage???? 9046974Seric ** 905294Seric ** Parameters: 9066974Seric ** fp -- the file to output to. 9076974Seric ** m -- the mailer describing this entry. 908294Seric ** 909294Seric ** Returns: 9106974Seric ** none 911294Seric ** 912294Seric ** Side Effects: 9136974Seric ** outputs some text to fp. 914294Seric */ 915294Seric 9166974Seric putfromline(fp, m) 9176974Seric register FILE *fp; 9186974Seric register MAILER *m; 919294Seric { 9206974Seric char buf[MAXLINE]; 921294Seric 9226974Seric if (bitset(M_NHDR, m->m_flags)) 9236974Seric return; 9244315Seric 9256974Seric # ifdef UGLYUUCP 9266974Seric if (bitset(M_UGLYUUCP, m->m_flags)) 9274205Seric { 9286974Seric extern char *macvalue(); 9296974Seric char *sys = macvalue('g'); 9306974Seric char *bang = index(sys, '!'); 9316041Seric 9326974Seric if (bang == NULL) 9336974Seric syserr("No ! in UUCP! (%s)", sys); 9345099Seric else 9356974Seric *bang = '\0'; 9366974Seric expand("From $f $d remote from $g", buf, 9376974Seric &buf[sizeof buf - 1], CurEnv); 9386974Seric *bang = '!'; 9396974Seric } 9406974Seric else 9415179Seric # endif UGLYUUCP 9426974Seric expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv); 9437123Seric putline(buf, fp, bitset(M_FULLSMTP, m->m_flags)); 9445981Seric } 9455981Seric /* 9466974Seric ** PUTHEADER -- put the header part of a message from the in-core copy 9475981Seric ** 9485981Seric ** Parameters: 9495981Seric ** fp -- file to put it on. 9505981Seric ** m -- mailer to use. 9516974Seric ** e -- envelope to use. 9525981Seric ** 9535981Seric ** Returns: 9545981Seric ** none. 9555981Seric ** 9565981Seric ** Side Effects: 9575981Seric ** none. 9585981Seric */ 9595981Seric 9606974Seric putheader(fp, m, e) 9615981Seric register FILE *fp; 9625981Seric register struct mailer *m; 9636974Seric register ENVELOPE *e; 9645981Seric { 9655981Seric char buf[BUFSIZ]; 9665981Seric register HDR *h; 9675981Seric extern char *arpadate(); 9685981Seric extern char *capitalize(); 9695981Seric extern char *hvalue(); 9705981Seric extern bool samefrom(); 9715981Seric char *of_line; 9727123Seric char obuf[MAXLINE]; 9737123Seric register char *obp; 9747123Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 9755981Seric 9764447Seric of_line = hvalue("original-from"); 9776974Seric for (h = e->e_header; h != NULL; h = h->h_link) 9781828Seric { 9794315Seric register char *p; 9806974Seric char *origfrom = e->e_origfrom; 9814447Seric bool nooutput; 9824315Seric 9834447Seric nooutput = FALSE; 9843389Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 9854447Seric nooutput = TRUE; 9864447Seric 9874447Seric /* use From: line from message if generated is the same */ 9884370Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL && 9894447Seric strcmp(m->m_from, "$f") == 0 && of_line == NULL) 9903385Seric { 9914370Seric p = origfrom; 9924370Seric origfrom = NULL; 9934370Seric } 9944370Seric else if (bitset(H_DEFAULT, h->h_flags)) 9954370Seric { 9966974Seric expand(h->h_value, buf, &buf[sizeof buf], e); 9973385Seric p = buf; 9983385Seric } 9995913Seric else if (bitset(H_ADDR, h->h_flags)) 10005913Seric { 10015913Seric register int opos; 10025913Seric bool firstone = TRUE; 10035913Seric 10045913Seric /* 10055913Seric ** Output the address list translated by the 10065913Seric ** mailer and with commas. 10075913Seric */ 10085913Seric 10095913Seric p = h->h_value; 10105913Seric if (p == NULL || *p == '\0' || nooutput) 10115913Seric continue; 10127123Seric obp = obuf; 10137123Seric sprintf(obp, "%s: ", capitalize(h->h_field)); 10145913Seric opos = strlen(h->h_field) + 2; 10157123Seric obp += opos; 10165913Seric while (*p != '\0') 10175913Seric { 10185913Seric register char *name = p; 10195913Seric extern char *remotename(); 10205913Seric char savechar; 10215913Seric 10225913Seric /* find the end of the name */ 10235936Seric while (*p != '\0' && *p != ',') 10245936Seric { 10255936Seric extern bool isatword(); 10265936Seric char *oldp; 10275936Seric 10286974Seric if (!e->e_oldstyle || !isspace(*p)) 10295936Seric { 10305936Seric p++; 10315936Seric continue; 10325936Seric } 10335936Seric oldp = p; 10345936Seric while (*p != '\0' && isspace(*p)) 10355936Seric p++; 10365936Seric if (*p != '@' && !isatword(p)) 10375936Seric { 10385936Seric p = oldp; 10395936Seric break; 10405936Seric } 10415936Seric p += *p == '@' ? 1 : 2; 10425936Seric while (*p != '\0' && isspace(*p)) 10435936Seric p++; 10445936Seric } 10455913Seric savechar = *p; 10465913Seric *p = '\0'; 10475913Seric 10485913Seric /* translate the name to be relative */ 10496820Seric name = remotename(name, m, FALSE); 10505913Seric if (*name == '\0') 10515913Seric continue; 10525913Seric 10535913Seric /* output the name with nice formatting */ 10545913Seric opos += strlen(name); 10555913Seric if (!firstone) 10565913Seric opos += 2; 10575913Seric if (opos > 78 && !firstone) 10585913Seric { 10597123Seric (void) sprintf(obp, ",\n"); 10607123Seric putline(obuf, fp, fullsmtp); 10617123Seric obp = obuf; 10627123Seric (void) sprintf(obp, " "); 10637123Seric obp += strlen(obp); 10645916Seric opos = 8 + strlen(name); 10655913Seric } 10665913Seric else if (!firstone) 10677123Seric { 10687123Seric (void) sprintf(obp, ", "); 10697123Seric obp += 2; 10707123Seric } 10717123Seric (void) sprintf(obp, "%s", name); 10727123Seric obp += strlen(obp); 10735913Seric firstone = FALSE; 10745913Seric 10755913Seric /* clean up the source string */ 10765913Seric *p = savechar; 10775913Seric while (*p != '\0' && (isspace(*p) || *p == ',')) 10785913Seric p++; 10795913Seric } 10807123Seric strcpy(obp, "\n"); 1081*7202Seric putline(obuf, fp, fullsmtp); 10825913Seric nooutput = TRUE; 10835913Seric } 10842898Seric else 10853385Seric p = h->h_value; 10864447Seric if (p == NULL || *p == '\0') 10873389Seric continue; 10884209Seric 10894209Seric /* hack, hack -- output Original-From field if different */ 10904447Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL) 10914209Seric { 10924447Seric /* output new Original-From line if needed */ 10934447Seric if (of_line == NULL && !samefrom(p, origfrom)) 10947123Seric { 10957123Seric (void) sprintf(obuf, "Original-From: %s\n", origfrom); 10967123Seric putline(obuf, fp, fullsmtp); 10977123Seric } 10984447Seric if (of_line != NULL && !nooutput && samefrom(p, of_line)) 10994447Seric { 11004447Seric /* delete Original-From: line if redundant */ 11014447Seric p = of_line; 11024447Seric of_line = NULL; 11034447Seric } 11044447Seric } 11054447Seric else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL) 11064447Seric nooutput = TRUE; 11074447Seric 11084447Seric /* finally, output the header line */ 11094447Seric if (!nooutput) 11104447Seric { 11117123Seric (void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p); 11127123Seric putline(obuf, fp, fullsmtp); 11134447Seric h->h_flags |= H_USED; 11144209Seric } 11152898Seric } 1116294Seric } 1117294Seric /* 11186974Seric ** PUTBODY -- put the body of a message. 11196974Seric ** 11206974Seric ** Parameters: 11216974Seric ** fp -- file to output onto. 11226974Seric ** m -- a mailer descriptor. 11236974Seric ** xdot -- if set, use SMTP hidden dot algorithm. 11246974Seric ** 11256974Seric ** Returns: 11266974Seric ** none. 11276974Seric ** 11286974Seric ** Side Effects: 11296974Seric ** The message is written onto fp. 11306974Seric */ 11316974Seric 11326974Seric putbody(fp, m, xdot) 11336974Seric FILE *fp; 11346974Seric struct mailer *m; 11356974Seric bool xdot; 11366974Seric { 11376974Seric char buf[MAXLINE + 1]; 11387123Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 11396974Seric 11406974Seric /* 11416974Seric ** Output the body of the message 11426974Seric */ 11436974Seric 11447003Seric #ifdef lint 11457003Seric /* m will be needed later for complete smtp emulation */ 11467003Seric if (m == NULL) 11477003Seric return; 11487003Seric #endif lint 11497003Seric 11506974Seric if (TempFile != NULL) 11516974Seric { 11526974Seric rewind(TempFile); 11536974Seric buf[0] = '.'; 11546974Seric while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL) 11557123Seric putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp); 11566974Seric 11576974Seric if (ferror(TempFile)) 11586974Seric { 11596974Seric syserr("putbody: read error"); 11606974Seric ExitStat = EX_IOERR; 11616974Seric } 11626974Seric } 11636974Seric 11646974Seric (void) fflush(fp); 11656974Seric if (ferror(fp) && errno != EPIPE) 11666974Seric { 11676974Seric syserr("putbody: write error"); 11686974Seric ExitStat = EX_IOERR; 11696974Seric } 11706974Seric errno = 0; 11716974Seric } 11726974Seric /* 11735936Seric ** ISATWORD -- tell if the word we are pointing to is "at". 11745936Seric ** 11755936Seric ** Parameters: 11765936Seric ** p -- word to check. 11775936Seric ** 11785936Seric ** Returns: 11795936Seric ** TRUE -- if p is the word at. 11805936Seric ** FALSE -- otherwise. 11815936Seric ** 11825936Seric ** Side Effects: 11835936Seric ** none. 11845936Seric */ 11855936Seric 11865936Seric bool 11875936Seric isatword(p) 11885936Seric register char *p; 11895936Seric { 11905936Seric extern char lower(); 11915936Seric 11925936Seric if (lower(p[0]) == 'a' && lower(p[1]) == 't' && 11935936Seric p[2] != '\0' && isspace(p[2])) 11945936Seric return (TRUE); 11955936Seric return (FALSE); 11965936Seric } 11975936Seric /* 11985913Seric ** REMOTENAME -- return the name relative to the current mailer 11995913Seric ** 12005913Seric ** Parameters: 12015913Seric ** name -- the name to translate. 12026820Seric ** force -- if set, forces rewriting even if the mailer 12036820Seric ** does not request it. Used for rewriting 12046820Seric ** sender addresses. 12055913Seric ** 12065913Seric ** Returns: 12075913Seric ** the text string representing this address relative to 12085913Seric ** the receiving mailer. 12095913Seric ** 12105913Seric ** Side Effects: 12115913Seric ** none. 12125913Seric ** 12135913Seric ** Warnings: 12145913Seric ** The text string returned is tucked away locally; 12155913Seric ** copy it if you intend to save it. 12165913Seric */ 12175913Seric 12185913Seric char * 12196820Seric remotename(name, m, force) 12205913Seric char *name; 12215916Seric struct mailer *m; 12226820Seric bool force; 12235913Seric { 12245913Seric static char buf[MAXNAME]; 12255913Seric char lbuf[MAXNAME]; 12265913Seric extern char *macvalue(); 12275913Seric char *oldf = macvalue('f'); 12285916Seric char *oldx = macvalue('x'); 12295916Seric char *oldg = macvalue('g'); 12305913Seric extern char **prescan(); 12315913Seric register char **pvp; 12325916Seric extern char *getxpart(); 12337003Seric extern ADDRESS *buildaddr(); 12345913Seric 12355913Seric /* 12366820Seric ** See if this mailer wants the name to be rewritten. There are 12376820Seric ** many problems here, owing to the standards for doing replies. 12386820Seric ** In general, these names should only be rewritten if we are 12396820Seric ** sending to another host that runs sendmail. 12406820Seric */ 12416820Seric 12426820Seric if (!bitset(M_RELRCPT, m->m_flags) && !force) 12437003Seric return (name); 12446820Seric 12456820Seric /* 12465913Seric ** Do general rewriting of name. 12475913Seric ** This will also take care of doing global name translation. 12485913Seric */ 12495913Seric 12505916Seric define('x', getxpart(name)); 12515913Seric pvp = prescan(name, '\0'); 12527048Seric rewrite(pvp, 1); 12537048Seric rewrite(pvp, 3); 12547048Seric if (**pvp == CANONNET) 12555913Seric { 12567048Seric /* oops... resolved to something */ 12577048Seric return (name); 12585913Seric } 12597048Seric cataddr(pvp, lbuf, sizeof lbuf); 12605913Seric 12615913Seric /* make the name relative to the receiving mailer */ 12625913Seric define('f', lbuf); 12636974Seric expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv); 12645913Seric 12655913Seric /* rewrite to get rid of garbage we added in the expand above */ 12665913Seric pvp = prescan(buf, '\0'); 12675913Seric rewrite(pvp, 2); 12685916Seric cataddr(pvp, lbuf, sizeof lbuf); 12695913Seric 12705916Seric /* now add any comment info we had before back */ 12715916Seric define('g', lbuf); 12726974Seric expand("$q", buf, &buf[sizeof buf - 1], CurEnv); 12735916Seric 12745913Seric define('f', oldf); 12755916Seric define('g', oldg); 12765916Seric define('x', oldx); 12775913Seric 12785913Seric # ifdef DEBUG 12795913Seric if (Debug > 0) 12805913Seric printf("remotename(%s) => `%s'\n", name, buf); 12815913Seric # endif DEBUG 12825913Seric return (buf); 12835913Seric } 12845913Seric /* 12854370Seric ** SAMEFROM -- tell if two text addresses represent the same from address. 12864370Seric ** 12874370Seric ** Parameters: 12884370Seric ** ifrom -- internally generated form of from address. 12894370Seric ** efrom -- external form of from address. 12904370Seric ** 12914370Seric ** Returns: 12924370Seric ** TRUE -- if they convey the same info. 12934370Seric ** FALSE -- if any information has been lost. 12944370Seric ** 12954370Seric ** Side Effects: 12964370Seric ** none. 12974370Seric */ 12984370Seric 12994370Seric bool 13004370Seric samefrom(ifrom, efrom) 13014370Seric char *ifrom; 13024370Seric char *efrom; 13034370Seric { 13044447Seric register char *p; 13054447Seric char buf[MAXNAME + 4]; 13064447Seric 13074447Seric # ifdef DEBUG 13084447Seric if (Debug > 7) 13094447Seric printf("samefrom(%s,%s)-->", ifrom, efrom); 13104447Seric # endif DEBUG 13114447Seric if (strcmp(ifrom, efrom) == 0) 13124447Seric goto success; 13134447Seric p = index(ifrom, '@'); 13144447Seric if (p == NULL) 13154447Seric goto failure; 13164447Seric *p = '\0'; 13177003Seric (void) strcpy(buf, ifrom); 13187003Seric (void) strcat(buf, " at "); 13194447Seric *p++ = '@'; 13207003Seric (void) strcat(buf, p); 13214447Seric if (strcmp(buf, efrom) == 0) 13224447Seric goto success; 13234447Seric 13244447Seric failure: 13254447Seric # ifdef DEBUG 13264447Seric if (Debug > 7) 13274447Seric printf("FALSE\n"); 13284447Seric # endif DEBUG 13294447Seric return (FALSE); 13304447Seric 13314447Seric success: 13324447Seric # ifdef DEBUG 13334447Seric if (Debug > 7) 13344447Seric printf("TRUE\n"); 13354447Seric # endif DEBUG 13364447Seric return (TRUE); 13374370Seric } 13384370Seric /* 1339294Seric ** MAILFILE -- Send a message to a file. 1340294Seric ** 13414327Seric ** If the file has the setuid/setgid bits set, but NO execute 13424327Seric ** bits, sendmail will try to become the owner of that file 13434327Seric ** rather than the real user. Obviously, this only works if 13444327Seric ** sendmail runs as root. 13454327Seric ** 1346294Seric ** Parameters: 1347294Seric ** filename -- the name of the file to send to. 13484397Seric ** ctladdr -- the controlling address header -- includes 13494397Seric ** the userid/groupid to be when sending. 1350294Seric ** 1351294Seric ** Returns: 1352294Seric ** The exit code associated with the operation. 1353294Seric ** 1354294Seric ** Side Effects: 1355294Seric ** none. 1356294Seric */ 1357294Seric 13584397Seric mailfile(filename, ctladdr) 1359294Seric char *filename; 13604397Seric ADDRESS *ctladdr; 1361294Seric { 1362294Seric register FILE *f; 13634214Seric register int pid; 1364294Seric 13654214Seric /* 13664214Seric ** Fork so we can change permissions here. 13674214Seric ** Note that we MUST use fork, not vfork, because of 13684214Seric ** the complications of calling subroutines, etc. 13694214Seric */ 13704067Seric 13714214Seric DOFORK(fork); 13724214Seric 13734214Seric if (pid < 0) 13744214Seric return (EX_OSERR); 13754214Seric else if (pid == 0) 13764214Seric { 13774214Seric /* child -- actually write to file */ 13784327Seric struct stat stb; 13794327Seric 13804215Seric (void) signal(SIGINT, SIG_DFL); 13814215Seric (void) signal(SIGHUP, SIG_DFL); 13824215Seric (void) signal(SIGTERM, SIG_DFL); 13834327Seric umask(OldUmask); 13844327Seric if (stat(filename, &stb) < 0) 13854431Seric stb.st_mode = 0666; 13864327Seric if (bitset(0111, stb.st_mode)) 13874327Seric exit(EX_CANTCREAT); 13884401Seric if (ctladdr == NULL) 13896900Seric ctladdr = &CurEnv->e_from; 13904327Seric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 13914417Seric { 13924417Seric if (ctladdr->q_uid == 0) 13934417Seric (void) setgid(DefGid); 13944417Seric else 13954417Seric (void) setgid(ctladdr->q_gid); 13964417Seric } 13974327Seric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 13984417Seric { 13994417Seric if (ctladdr->q_uid == 0) 14004417Seric (void) setuid(DefUid); 14014417Seric else 14024417Seric (void) setuid(ctladdr->q_uid); 14034417Seric } 14046887Seric f = dfopen(filename, "a"); 14054214Seric if (f == NULL) 14064214Seric exit(EX_CANTCREAT); 14074214Seric 14086974Seric putfromline(f, Mailer[1]); 14096974Seric (*CurEnv->e_puthdr)(f, Mailer[1], CurEnv); 14107123Seric fputs("\n", f); 14116974Seric (*CurEnv->e_putbody)(f, Mailer[1], FALSE); 14124214Seric fputs("\n", f); 14134214Seric (void) fclose(f); 14144214Seric (void) fflush(stdout); 14154417Seric 14166887Seric /* reset ISUID & ISGID bits for paranoid systems */ 14174621Seric (void) chmod(filename, (int) stb.st_mode); 14184214Seric exit(EX_OK); 14194315Seric /*NOTREACHED*/ 14204214Seric } 14214214Seric else 14224214Seric { 14234214Seric /* parent -- wait for exit status */ 14244214Seric register int i; 14254214Seric auto int stat; 14264214Seric 14274214Seric while ((i = wait(&stat)) != pid) 14284214Seric { 14294214Seric if (i < 0) 14304214Seric { 14314214Seric stat = EX_OSERR << 8; 14324214Seric break; 14334214Seric } 14344214Seric } 14354215Seric if ((stat & 0377) != 0) 14364215Seric stat = EX_UNAVAILABLE << 8; 14374214Seric return ((stat >> 8) & 0377); 14384214Seric } 1439294Seric } 14404550Seric /* 14414550Seric ** SENDALL -- actually send all the messages. 14424550Seric ** 14434550Seric ** Parameters: 14447043Seric ** e -- the envelope to send. 14454550Seric ** verifyonly -- if set, only give verification messages. 14464550Seric ** 14474550Seric ** Returns: 14484550Seric ** none. 14494550Seric ** 14504550Seric ** Side Effects: 14514550Seric ** Scans the send lists and sends everything it finds. 14527043Seric ** Delivers any appropriate error messages. 14534550Seric */ 14544550Seric 14557043Seric sendall(e, verifyonly) 14567043Seric ENVELOPE *e; 14574550Seric bool verifyonly; 14584550Seric { 14595008Seric register ADDRESS *q; 14604550Seric 14615032Seric # ifdef DEBUG 14625032Seric if (Debug > 1) 14635032Seric { 14646900Seric printf("\nSend Queue:\n"); 14657043Seric printaddr(e->e_sendqueue, TRUE); 14665032Seric } 14675032Seric # endif DEBUG 14685008Seric 14697043Seric /* 14707043Seric ** Run through the list and send everything. 14717043Seric */ 14727043Seric 14737043Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14744550Seric { 14755008Seric if (verifyonly) 14764550Seric { 14776900Seric CurEnv->e_to = q->q_paddr; 14785008Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 14797052Seric message(Arpa_Info, "deliverable"); 14804550Seric } 14815008Seric else 14826974Seric (void) deliver(q); 14834550Seric } 14847043Seric 14857043Seric /* 14867043Seric ** Now run through and check for errors. 14877043Seric */ 14887043Seric 14897043Seric if (verifyonly) 14907043Seric return; 14917043Seric 14927043Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14937043Seric { 14947043Seric register ADDRESS *qq; 14957043Seric 14967043Seric if (bitset(QQUEUEUP, q->q_flags)) 14977043Seric e->e_queueup = TRUE; 14987043Seric if (!bitset(QBADADDR, q->q_flags)) 14997043Seric continue; 15007043Seric 15017043Seric /* we have an address that failed -- find the parent */ 15027043Seric for (qq = q; qq != NULL; qq = qq->q_alias) 15037043Seric { 15047043Seric char obuf[MAXNAME + 6]; 15057043Seric extern char *aliaslookup(); 15067043Seric 15077043Seric /* we can only have owners for local addresses */ 15087043Seric if (!bitset(M_LOCAL, qq->q_mailer->m_flags)) 15097043Seric continue; 15107043Seric 15117043Seric /* see if the owner list exists */ 15127043Seric (void) strcpy(obuf, "owner-"); 15137047Seric if (strncmp(qq->q_user, "owner-", 6) == 0) 15147047Seric (void) strcat(obuf, "owner"); 15157047Seric else 15167047Seric (void) strcat(obuf, qq->q_user); 15177043Seric if (aliaslookup(obuf) == NULL) 15187043Seric continue; 15197043Seric 15207043Seric /* owner list exists -- add it to the error queue */ 15217043Seric qq->q_flags &= ~QPRIMARY; 15227043Seric sendto(obuf, 1, qq, &e->e_errorqueue); 15237043Seric MailBack = TRUE; 15247043Seric break; 15257043Seric } 15267043Seric 15277043Seric /* if we did not find an owner, send to the sender */ 15287043Seric if (qq == NULL) 15297043Seric sendto(e->e_from.q_paddr, 1, qq, &e->e_errorqueue); 15307043Seric } 15314550Seric } 15327043Seric /* 15337043Seric ** CHECKERRORS -- check a queue of addresses and process errors. 15347043Seric ** 15357043Seric ** Parameters: 15367043Seric ** e -- the envelope to check. 15377043Seric ** 15387043Seric ** Returns: 15397043Seric ** none. 15407043Seric ** 15417043Seric ** Side Effects: 15427043Seric ** Arranges to queue all tempfailed messages in q 15437043Seric ** or deliver error responses. 15447043Seric */ 15457043Seric 15467043Seric checkerrors(e) 15477043Seric register ENVELOPE *e; 15487043Seric { 15497043Seric # ifdef DEBUG 15507043Seric if (Debug > 0) 15517043Seric { 15527043Seric printf("\ncheckerrors: errorqueue:\n"); 15537043Seric printaddr(e->e_errorqueue, TRUE); 15547043Seric } 15557043Seric # endif DEBUG 15567043Seric 15577043Seric /* mail back the transcript on errors */ 15587043Seric if (FatalErrors) 15597043Seric savemail(); 15607043Seric 15617043Seric /* queue up anything laying around */ 15627043Seric if (e->e_queueup) 15637043Seric { 15647043Seric # ifdef QUEUE 15657043Seric queueup(e, FALSE); 15667043Seric # else QUEUE 15677043Seric syserr("finis: trying to queue %s", e->e_df); 15687043Seric # endif QUEUE 15697043Seric } 15707043Seric } 1571