125a99e2eSeric # include <signal.h> 254aa2b0fSeric # include <errno.h> 3b20b3270Seric # include "sendmail.h" 4c77d1c25Seric # include <sys/stat.h> 525a99e2eSeric # ifdef LOG 6e374fd72Seric # include <syslog.h> 725a99e2eSeric # endif LOG 825a99e2eSeric 9*51552439Seric SCCSID(@(#)deliver.c 3.79.1.1 05/29/82); 10259cace7Seric 1125a99e2eSeric /* 1213bbc08cSeric ** DELIVER -- Deliver a message to a list of addresses. 1313bbc08cSeric ** 1413bbc08cSeric ** This routine delivers to everyone on the same host as the 1513bbc08cSeric ** user on the head of the list. It is clever about mailers 1613bbc08cSeric ** that don't handle multiple users. It is NOT guaranteed 1713bbc08cSeric ** that it will deliver to all these addresses however -- so 1813bbc08cSeric ** deliver should be called once for each address on the 1913bbc08cSeric ** list. 2025a99e2eSeric ** 2125a99e2eSeric ** Parameters: 22c77d1c25Seric ** firstto -- head of the address list to deliver to. 2325a99e2eSeric ** 2425a99e2eSeric ** Returns: 2525a99e2eSeric ** zero -- successfully delivered. 2625a99e2eSeric ** else -- some failure, see ExitStat for more info. 2725a99e2eSeric ** 2825a99e2eSeric ** Side Effects: 2925a99e2eSeric ** The standard input is passed off to someone. 3025a99e2eSeric */ 3125a99e2eSeric 32*51552439Seric deliver(firstto) 33c77d1c25Seric ADDRESS *firstto; 3425a99e2eSeric { 3578442df3Seric char *host; /* host being sent to */ 3678442df3Seric char *user; /* user being sent to */ 3725a99e2eSeric char **pvp; 385dfc646bSeric register char **mvp; 3925a99e2eSeric register char *p; 4078442df3Seric register struct mailer *m; /* mailer for this recipient */ 415dfc646bSeric register int i; 422a6e0786Seric extern bool checkcompat(); 435dfc646bSeric char *pv[MAXPV+1]; 4478442df3Seric char tobuf[MAXLINE]; /* text line of to people */ 455dfc646bSeric char buf[MAXNAME]; 466259796dSeric ADDRESS *ctladdr; 476259796dSeric extern ADDRESS *getctladdr(); 4878442df3Seric char tfrombuf[MAXNAME]; /* translated from person */ 4978442df3Seric extern char **prescan(); 50c77d1c25Seric register ADDRESS *to = firstto; 51c579ef51Seric bool clever = FALSE; /* running user smtp to this mailer */ 52c579ef51Seric bool tempfail = FALSE; 53772e6e50Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 5425a99e2eSeric 5535490626Seric errno = 0; 56e77e673fSeric if (!ForceMail && bitset(QDONTSEND, to->q_flags)) 575dfc646bSeric return (0); 5825a99e2eSeric 59*51552439Seric m = to->q_mailer; 60*51552439Seric host = to->q_host; 61*51552439Seric 6225a99e2eSeric # ifdef DEBUG 6325a99e2eSeric if (Debug) 645dfc646bSeric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 65*51552439Seric m->m_mno, host, to->q_user); 6625a99e2eSeric # endif DEBUG 67*51552439Seric if (Verbose) 68*51552439Seric message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name); 69f3dbc832Seric 70f3dbc832Seric /* 71f3dbc832Seric ** If this mailer is expensive, and if we don't want to make 72f3dbc832Seric ** connections now, just mark these addresses and return. 73f3dbc832Seric ** This is useful if we want to batch connections to 74f3dbc832Seric ** reduce load. This will cause the messages to be 75f3dbc832Seric ** queued up, and a daemon will come along to send the 76f3dbc832Seric ** messages later. 77f3dbc832Seric ** This should be on a per-mailer basis. 78f3dbc832Seric */ 79f3dbc832Seric 80f3dbc832Seric if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags)) 81f3dbc832Seric { 827a941e7aSeric CurEnv->e_queueup = TRUE; 83f3dbc832Seric for (; to != NULL; to = to->q_next) 84f3dbc832Seric if (!bitset(QDONTSEND, to->q_flags)) 85f3dbc832Seric to->q_flags |= QQUEUEUP|QDONTSEND; 86f3dbc832Seric return (0); 87f3dbc832Seric } 88f3dbc832Seric 8925a99e2eSeric /* 905dfc646bSeric ** Do initial argv setup. 915dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 925dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 935dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 945dfc646bSeric ** code does not check for 'pv' overflow; this places a 955dfc646bSeric ** manifest lower limit of 4 for MAXPV. 96f3dbc832Seric ** We rewrite the from address here, being careful 97f3dbc832Seric ** to also rewrite it again using ruleset 2 to 98f3dbc832Seric ** eliminate redundancies. 995dfc646bSeric */ 1005dfc646bSeric 10178442df3Seric /* rewrite from address, using rewriting rules */ 102*51552439Seric expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv); 10378442df3Seric mvp = prescan(buf, '\0'); 10478442df3Seric if (mvp == NULL) 10578442df3Seric { 10678442df3Seric syserr("bad mailer from translate \"%s\"", buf); 10778442df3Seric return (EX_SOFTWARE); 10878442df3Seric } 10978442df3Seric rewrite(mvp, 2); 11078442df3Seric cataddr(mvp, tfrombuf, sizeof tfrombuf); 11178442df3Seric 11278442df3Seric define('g', tfrombuf); /* translated sender address */ 1135dfc646bSeric define('h', host); /* to host */ 1145dfc646bSeric Errors = 0; 1155dfc646bSeric pvp = pv; 1165dfc646bSeric *pvp++ = m->m_argv[0]; 1175dfc646bSeric 1185dfc646bSeric /* insert -f or -r flag as appropriate */ 1195dfc646bSeric if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag) 1205dfc646bSeric { 1215dfc646bSeric if (bitset(M_FOPT, m->m_flags)) 1225dfc646bSeric *pvp++ = "-f"; 1235dfc646bSeric else 1245dfc646bSeric *pvp++ = "-r"; 125*51552439Seric expand("$g", buf, &buf[sizeof buf - 1], CurEnv); 1265dfc646bSeric *pvp++ = newstr(buf); 1275dfc646bSeric } 1285dfc646bSeric 1295dfc646bSeric /* 1305dfc646bSeric ** Append the other fixed parts of the argv. These run 1315dfc646bSeric ** up to the first entry containing "$u". There can only 1325dfc646bSeric ** be one of these, and there are only a few more slots 1335dfc646bSeric ** in the pv after it. 1345dfc646bSeric */ 1355dfc646bSeric 1365dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 1375dfc646bSeric { 1385dfc646bSeric while ((p = index(p, '$')) != NULL) 1395dfc646bSeric if (*++p == 'u') 1405dfc646bSeric break; 1415dfc646bSeric if (p != NULL) 1425dfc646bSeric break; 1435dfc646bSeric 1445dfc646bSeric /* this entry is safe -- go ahead and process it */ 145*51552439Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 1465dfc646bSeric *pvp++ = newstr(buf); 1475dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 1485dfc646bSeric { 1495dfc646bSeric syserr("Too many parameters to %s before $u", pv[0]); 1505dfc646bSeric return (-1); 1515dfc646bSeric } 1525dfc646bSeric } 153c579ef51Seric 15433db8731Seric /* 15533db8731Seric ** If we have no substitution for the user name in the argument 15633db8731Seric ** list, we know that we must supply the names otherwise -- and 15733db8731Seric ** SMTP is the answer!! 15833db8731Seric */ 15933db8731Seric 1605dfc646bSeric if (*mvp == NULL) 161c579ef51Seric { 162c579ef51Seric /* running SMTP */ 1632c7e1b8dSeric # ifdef SMTP 164c579ef51Seric clever = TRUE; 165c579ef51Seric *pvp = NULL; 16633db8731Seric 16733db8731Seric /* send the initial SMTP protocol */ 168ed854c7bSeric smtpinit(m, pv, (ADDRESS *) NULL); 1692c7e1b8dSeric # ifdef QUEUE 170c579ef51Seric if (i == EX_TEMPFAIL) 171c579ef51Seric { 1727a941e7aSeric CurEnv->e_queueup = TRUE; 173c579ef51Seric tempfail = TRUE; 174c579ef51Seric } 1752c7e1b8dSeric # endif QUEUE 1762c7e1b8dSeric # else SMTP 17733db8731Seric /* oops! we don't implement SMTP */ 1782c7e1b8dSeric syserr("SMTP style mailer"); 1792c7e1b8dSeric return (EX_SOFTWARE); 1802c7e1b8dSeric # endif SMTP 181c579ef51Seric } 1825dfc646bSeric 1835dfc646bSeric /* 1845dfc646bSeric ** At this point *mvp points to the argument with $u. We 1855dfc646bSeric ** run through our address list and append all the addresses 1865dfc646bSeric ** we can. If we run out of space, do not fret! We can 1875dfc646bSeric ** always send another copy later. 1885dfc646bSeric */ 1895dfc646bSeric 1905dfc646bSeric tobuf[0] = '\0'; 1917a941e7aSeric CurEnv->e_to = tobuf; 1926259796dSeric ctladdr = NULL; 1935dfc646bSeric for (; to != NULL; to = to->q_next) 1945dfc646bSeric { 1955dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 196bea22b26Seric if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags)) 1975dfc646bSeric break; 1985dfc646bSeric 1995dfc646bSeric /* if already sent or not for this host, don't send */ 200e77e673fSeric if ((!ForceMail && bitset(QDONTSEND, to->q_flags)) || 201e77e673fSeric strcmp(to->q_host, host) != 0 || to->q_mailer != firstto->q_mailer) 2025dfc646bSeric continue; 2036259796dSeric 204772e6e50Seric # ifdef DEBUG 205772e6e50Seric if (Debug) 206772e6e50Seric { 207772e6e50Seric printf("\nsend to "); 208772e6e50Seric printaddr(to, FALSE); 209772e6e50Seric } 210772e6e50Seric # endif DEBUG 211772e6e50Seric 2126259796dSeric /* compute effective uid/gid when sending */ 2137da1035fSeric if (to->q_mailer == ProgMailer) 2146259796dSeric ctladdr = getctladdr(to); 2156259796dSeric 2165dfc646bSeric user = to->q_user; 2177a941e7aSeric CurEnv->e_to = to->q_paddr; 2185dfc646bSeric to->q_flags |= QDONTSEND; 219c579ef51Seric if (tempfail) 220772e6e50Seric { 221c579ef51Seric to->q_flags |= QQUEUEUP; 222772e6e50Seric continue; 223772e6e50Seric } 2245dfc646bSeric 2255dfc646bSeric /* 2265dfc646bSeric ** Check to see that these people are allowed to 2275dfc646bSeric ** talk to each other. 2282a6e0786Seric */ 2292a6e0786Seric 2302a6e0786Seric if (!checkcompat(to)) 2315dfc646bSeric { 2325dfc646bSeric giveresponse(EX_UNAVAILABLE, TRUE, m); 2335dfc646bSeric continue; 2345dfc646bSeric } 2352a6e0786Seric 2362a6e0786Seric /* 2379ec9501bSeric ** Strip quote bits from names if the mailer is dumb 2389ec9501bSeric ** about them. 23925a99e2eSeric */ 24025a99e2eSeric 2412a6e0786Seric if (bitset(M_STRIPQ, m->m_flags)) 24225a99e2eSeric { 2439ec9501bSeric stripquotes(user, TRUE); 2449ec9501bSeric stripquotes(host, TRUE); 2459ec9501bSeric } 2469ec9501bSeric else 2479ec9501bSeric { 2489ec9501bSeric stripquotes(user, FALSE); 2499ec9501bSeric stripquotes(host, FALSE); 25025a99e2eSeric } 25125a99e2eSeric 25225a99e2eSeric /* 253508daeccSeric ** Pass it to the other host if we are running SMTP. 254508daeccSeric */ 255508daeccSeric 256508daeccSeric if (clever) 257508daeccSeric { 258508daeccSeric # ifdef SMTP 259508daeccSeric i = smtprcpt(to); 260508daeccSeric if (i != EX_OK) 261508daeccSeric { 262508daeccSeric # ifdef QUEUE 263508daeccSeric if (i == EX_TEMPFAIL) 264508daeccSeric { 2657a941e7aSeric CurEnv->e_queueup = TRUE; 266508daeccSeric to->q_flags |= QQUEUEUP; 267508daeccSeric } 268508daeccSeric else 269508daeccSeric # endif QUEUE 270508daeccSeric { 271508daeccSeric to->q_flags |= QBADADDR; 272508daeccSeric giveresponse(i, TRUE, m); 273508daeccSeric } 274508daeccSeric } 275508daeccSeric # else SMTP 276508daeccSeric syserr("trying to be clever"); 277508daeccSeric # endif SMTP 278508daeccSeric } 279508daeccSeric 280508daeccSeric /* 2813efaed6eSeric ** If an error message has already been given, don't 2823efaed6eSeric ** bother to send to this address. 2833efaed6eSeric ** 2843efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 2853efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 2863efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 2873efaed6eSeric */ 2883efaed6eSeric 2893efaed6eSeric if (bitset(QBADADDR, to->q_flags)) 2903efaed6eSeric continue; 2913efaed6eSeric 292f2fec898Seric /* save statistics.... */ 2937da1035fSeric Stat.stat_nt[to->q_mailer->m_mno]++; 2947a941e7aSeric Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize); 295f2fec898Seric 2963efaed6eSeric /* 29725a99e2eSeric ** See if this user name is "special". 29825a99e2eSeric ** If the user name has a slash in it, assume that this 299*51552439Seric ** is a file -- send it off without further ado. Note 300*51552439Seric ** that this type of addresses is not processed along 301*51552439Seric ** with the others, so we fudge on the To person. 30225a99e2eSeric */ 30325a99e2eSeric 3047da1035fSeric if (m == LocalMailer) 30525a99e2eSeric { 306a49f24c0Seric if (user[0] == '/') 30725a99e2eSeric { 3086259796dSeric i = mailfile(user, getctladdr(to)); 30925a99e2eSeric giveresponse(i, TRUE, m); 3105dfc646bSeric continue; 31125a99e2eSeric } 31225a99e2eSeric } 31325a99e2eSeric 31413bbc08cSeric /* 31513bbc08cSeric ** Address is verified -- add this user to mailer 31613bbc08cSeric ** argv, and add it to the print list of recipients. 31713bbc08cSeric */ 31813bbc08cSeric 319508daeccSeric /* link together the chain of recipients */ 320508daeccSeric to->q_tchain = tochain; 321508daeccSeric tochain = to; 322508daeccSeric 3235dfc646bSeric /* create list of users for error messages */ 3245dfc646bSeric if (tobuf[0] != '\0') 325db8841e9Seric (void) strcat(tobuf, ","); 326db8841e9Seric (void) strcat(tobuf, to->q_paddr); 3275dfc646bSeric define('u', user); /* to user */ 328c2567733Seric define('z', to->q_home); /* user's home */ 3295dfc646bSeric 330c579ef51Seric /* 331508daeccSeric ** Expand out this user into argument list. 332c579ef51Seric */ 333c579ef51Seric 334508daeccSeric if (!clever) 335c579ef51Seric { 336*51552439Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 3375dfc646bSeric *pvp++ = newstr(buf); 3385dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 3395dfc646bSeric { 3405dfc646bSeric /* allow some space for trailing parms */ 3415dfc646bSeric break; 3425dfc646bSeric } 3435dfc646bSeric } 344c579ef51Seric } 3455dfc646bSeric 346145b49b1Seric /* see if any addresses still exist */ 347145b49b1Seric if (tobuf[0] == '\0') 348c579ef51Seric { 3492c7e1b8dSeric # ifdef SMTP 350c579ef51Seric if (clever) 351c579ef51Seric smtpquit(pv[0]); 3522c7e1b8dSeric # endif SMTP 353145b49b1Seric return (0); 354c579ef51Seric } 355145b49b1Seric 3565dfc646bSeric /* print out messages as full list */ 3577a941e7aSeric CurEnv->e_to = tobuf; 3585dfc646bSeric 3595dfc646bSeric /* 3605dfc646bSeric ** Fill out any parameters after the $u parameter. 3615dfc646bSeric */ 3625dfc646bSeric 363c579ef51Seric while (!clever && *++mvp != NULL) 3645dfc646bSeric { 365*51552439Seric expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 3665dfc646bSeric *pvp++ = newstr(buf); 3675dfc646bSeric if (pvp >= &pv[MAXPV]) 3685dfc646bSeric syserr("deliver: pv overflow after $u for %s", pv[0]); 3695dfc646bSeric } 3705dfc646bSeric *pvp++ = NULL; 3715dfc646bSeric 37225a99e2eSeric /* 37325a99e2eSeric ** Call the mailer. 3746328bdf7Seric ** The argument vector gets built, pipes 37525a99e2eSeric ** are created as necessary, and we fork & exec as 3766328bdf7Seric ** appropriate. 377c579ef51Seric ** If we are running SMTP, we just need to clean up. 37825a99e2eSeric */ 37925a99e2eSeric 3806259796dSeric if (ctladdr == NULL) 3817a941e7aSeric ctladdr = &CurEnv->e_from; 3822c7e1b8dSeric # ifdef SMTP 383c579ef51Seric if (clever) 384c579ef51Seric { 385*51552439Seric i = smtpfinish(m, CurEnv); 386c579ef51Seric smtpquit(pv[0]); 387c579ef51Seric } 388c579ef51Seric else 3892c7e1b8dSeric # endif SMTP 390*51552439Seric i = sendoff(m, pv, ctladdr); 3915dfc646bSeric 392c77d1c25Seric /* 393c77d1c25Seric ** If we got a temporary failure, arrange to queue the 394c77d1c25Seric ** addressees. 395c77d1c25Seric */ 396c77d1c25Seric 3972c7e1b8dSeric # ifdef QUEUE 398c77d1c25Seric if (i == EX_TEMPFAIL) 399c77d1c25Seric { 4007a941e7aSeric CurEnv->e_queueup = TRUE; 401772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 402c77d1c25Seric to->q_flags |= QQUEUEUP; 403c77d1c25Seric } 4042c7e1b8dSeric # endif QUEUE 405c77d1c25Seric 40635490626Seric errno = 0; 4075dfc646bSeric return (i); 40825a99e2eSeric } 4095dfc646bSeric /* 41032d19d43Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 41132d19d43Seric ** 41232d19d43Seric ** This MUST be a macro, since after a vfork we are running 41332d19d43Seric ** two processes on the same stack!!! 41432d19d43Seric ** 41532d19d43Seric ** Parameters: 41632d19d43Seric ** none. 41732d19d43Seric ** 41832d19d43Seric ** Returns: 41932d19d43Seric ** From a macro??? You've got to be kidding! 42032d19d43Seric ** 42132d19d43Seric ** Side Effects: 42232d19d43Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 42332d19d43Seric ** pid of child in parent, zero in child. 42432d19d43Seric ** -1 on unrecoverable error. 42532d19d43Seric ** 42632d19d43Seric ** Notes: 42732d19d43Seric ** I'm awfully sorry this looks so awful. That's 42832d19d43Seric ** vfork for you..... 42932d19d43Seric */ 43032d19d43Seric 43132d19d43Seric # define NFORKTRIES 5 43232d19d43Seric # ifdef VFORK 43332d19d43Seric # define XFORK vfork 43432d19d43Seric # else VFORK 43532d19d43Seric # define XFORK fork 43632d19d43Seric # endif VFORK 43732d19d43Seric 43832d19d43Seric # define DOFORK(fORKfN) \ 43932d19d43Seric {\ 44032d19d43Seric register int i;\ 44132d19d43Seric \ 44232d19d43Seric for (i = NFORKTRIES; i-- > 0; )\ 44332d19d43Seric {\ 44432d19d43Seric pid = fORKfN();\ 44532d19d43Seric if (pid >= 0)\ 44632d19d43Seric break;\ 44732d19d43Seric sleep((unsigned) NFORKTRIES - i);\ 44832d19d43Seric }\ 44932d19d43Seric } 45032d19d43Seric /* 4512ed72599Seric ** DOFORK -- simple fork interface to DOFORK. 4522ed72599Seric ** 4532ed72599Seric ** Parameters: 4542ed72599Seric ** none. 4552ed72599Seric ** 4562ed72599Seric ** Returns: 4572ed72599Seric ** pid of child in parent. 4582ed72599Seric ** zero in child. 4592ed72599Seric ** -1 on error. 4602ed72599Seric ** 4612ed72599Seric ** Side Effects: 4622ed72599Seric ** returns twice, once in parent and once in child. 4632ed72599Seric */ 4642ed72599Seric 4652ed72599Seric dofork() 4662ed72599Seric { 4672ed72599Seric register int pid; 4682ed72599Seric 4692ed72599Seric DOFORK(fork); 4702ed72599Seric return (pid); 4712ed72599Seric } 4722ed72599Seric /* 4735dfc646bSeric ** SENDOFF -- send off call to mailer & collect response. 4745dfc646bSeric ** 4755dfc646bSeric ** Parameters: 4765dfc646bSeric ** m -- mailer descriptor. 4775dfc646bSeric ** pvp -- parameter vector to send to it. 4786259796dSeric ** ctladdr -- an address pointer controlling the 4796259796dSeric ** user/groupid etc. of the mailer. 4805dfc646bSeric ** 4815dfc646bSeric ** Returns: 4825dfc646bSeric ** exit status of mailer. 4835dfc646bSeric ** 4845dfc646bSeric ** Side Effects: 4855dfc646bSeric ** none. 4865dfc646bSeric */ 4875dfc646bSeric 488*51552439Seric sendoff(m, pvp, ctladdr) 4895dfc646bSeric struct mailer *m; 4905dfc646bSeric char **pvp; 4916259796dSeric ADDRESS *ctladdr; 4925dfc646bSeric { 493c579ef51Seric auto FILE *mfile; 494c579ef51Seric auto FILE *rfile; 4955dfc646bSeric register int i; 496c579ef51Seric int pid; 497c579ef51Seric 498c579ef51Seric /* 499c579ef51Seric ** Create connection to mailer. 500c579ef51Seric */ 501c579ef51Seric 502c579ef51Seric pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 503c579ef51Seric if (pid < 0) 504c579ef51Seric return (-1); 505c579ef51Seric 506c579ef51Seric /* 507c579ef51Seric ** Format and send message. 508c579ef51Seric */ 509c579ef51Seric 510c579ef51Seric (void) signal(SIGPIPE, SIG_IGN); 511*51552439Seric putfromline(mfile, m); 512*51552439Seric (*CurEnv->e_puthdr)(mfile, m, CurEnv); 513*51552439Seric fprintf(mfile, "\n"); 514*51552439Seric (*CurEnv->e_putbody)(mfile, m, FALSE); 515c579ef51Seric (void) fclose(mfile); 516c579ef51Seric 517c579ef51Seric i = endmailer(pid, pvp[0]); 518c579ef51Seric giveresponse(i, TRUE, m); 519bc6e2962Seric 520bc6e2962Seric /* arrange a return receipt if requested */ 5217a941e7aSeric if (CurEnv->e_retreceipt && bitset(M_LOCAL, m->m_flags) && i == EX_OK) 522bc6e2962Seric { 5237a941e7aSeric CurEnv->e_sendreceipt = TRUE; 5247a941e7aSeric fprintf(Xscript, "%s... successfully delivered\n", CurEnv->e_to); 525bc6e2962Seric /* do we want to send back more info? */ 526bc6e2962Seric } 527bc6e2962Seric 528c579ef51Seric return (i); 529c579ef51Seric } 530c579ef51Seric /* 531c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 532c579ef51Seric ** 533c579ef51Seric ** We should never get fatal errors (e.g., segmentation 534c579ef51Seric ** violation), so we report those specially. For other 535c579ef51Seric ** errors, we choose a status message (into statmsg), 536c579ef51Seric ** and if it represents an error, we print it. 537c579ef51Seric ** 538c579ef51Seric ** Parameters: 539c579ef51Seric ** pid -- pid of mailer. 540c579ef51Seric ** name -- name of mailer (for error messages). 541c579ef51Seric ** 542c579ef51Seric ** Returns: 543c579ef51Seric ** exit code of mailer. 544c579ef51Seric ** 545c579ef51Seric ** Side Effects: 546c579ef51Seric ** none. 547c579ef51Seric */ 548c579ef51Seric 549c579ef51Seric endmailer(pid, name) 550c579ef51Seric int pid; 551c579ef51Seric char *name; 552c579ef51Seric { 553c579ef51Seric register int i; 554c579ef51Seric auto int st; 555c579ef51Seric 55633db8731Seric /* in the IPC case there is nothing to wait for */ 55733db8731Seric if (pid == 0) 55833db8731Seric return (EX_OK); 55933db8731Seric 56033db8731Seric /* wait for the mailer process to die and collect status */ 561c579ef51Seric while ((i = wait(&st)) > 0 && i != pid) 562c579ef51Seric continue; 563c579ef51Seric if (i < 0) 564c579ef51Seric { 565c579ef51Seric syserr("wait"); 566c579ef51Seric return (-1); 567c579ef51Seric } 56833db8731Seric 56933db8731Seric /* see if it died a horrid death */ 570c579ef51Seric if ((st & 0377) != 0) 571c579ef51Seric { 572c579ef51Seric syserr("%s: stat %o", name, st); 573c579ef51Seric ExitStat = EX_UNAVAILABLE; 574c579ef51Seric return (-1); 575c579ef51Seric } 57633db8731Seric 57733db8731Seric /* normal death -- return status */ 578c579ef51Seric i = (st >> 8) & 0377; 579c579ef51Seric return (i); 580c579ef51Seric } 581c579ef51Seric /* 582c579ef51Seric ** OPENMAILER -- open connection to mailer. 583c579ef51Seric ** 584c579ef51Seric ** Parameters: 585c579ef51Seric ** m -- mailer descriptor. 586c579ef51Seric ** pvp -- parameter vector to pass to mailer. 587c579ef51Seric ** ctladdr -- controlling address for user. 588c579ef51Seric ** clever -- create a full duplex connection. 589c579ef51Seric ** pmfile -- pointer to mfile (to mailer) connection. 590c579ef51Seric ** prfile -- pointer to rfile (from mailer) connection. 591c579ef51Seric ** 592c579ef51Seric ** Returns: 59333db8731Seric ** pid of mailer ( > 0 ). 594c579ef51Seric ** -1 on error. 59533db8731Seric ** zero on an IPC connection. 596c579ef51Seric ** 597c579ef51Seric ** Side Effects: 598c579ef51Seric ** creates a mailer in a subprocess. 599c579ef51Seric */ 600c579ef51Seric 601c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 602c579ef51Seric struct mailer *m; 603c579ef51Seric char **pvp; 604c579ef51Seric ADDRESS *ctladdr; 605c579ef51Seric bool clever; 606c579ef51Seric FILE **pmfile; 607c579ef51Seric FILE **prfile; 608c579ef51Seric { 6095dfc646bSeric int pid; 610f8952a83Seric int mpvect[2]; 611c579ef51Seric int rpvect[2]; 6125dfc646bSeric FILE *mfile; 613c579ef51Seric FILE *rfile; 6145dfc646bSeric extern FILE *fdopen(); 6155dfc646bSeric 6165dfc646bSeric # ifdef DEBUG 6175dfc646bSeric if (Debug) 6185dfc646bSeric { 619c579ef51Seric printf("openmailer:\n"); 6205dfc646bSeric printav(pvp); 6215dfc646bSeric } 6225dfc646bSeric # endif DEBUG 62335490626Seric errno = 0; 6245dfc646bSeric 62533db8731Seric # ifdef DAEMON 62633db8731Seric /* 62733db8731Seric ** Deal with the special case of mail handled through an IPC 62833db8731Seric ** connection. 62933db8731Seric ** In this case we don't actually fork. We must be 63033db8731Seric ** running SMTP for this to work. We will return a 63133db8731Seric ** zero pid to indicate that we are running IPC. 63233db8731Seric */ 63333db8731Seric 63433db8731Seric if (strcmp(m->m_mailer, "[IPC]") == 0) 63533db8731Seric { 63633db8731Seric register int i; 63733db8731Seric 63833db8731Seric if (!clever) 63933db8731Seric syserr("non-clever IPC"); 64093b6e3cfSeric if (pvp[2] != NULL) 64193b6e3cfSeric i = atoi(pvp[2]); 64293b6e3cfSeric else 64393b6e3cfSeric i = 0; 64493b6e3cfSeric i = makeconnection(pvp[1], i, pmfile, prfile); 64533db8731Seric if (i != EX_OK) 646ed854c7bSeric { 647ed854c7bSeric ExitStat = i; 64833db8731Seric return (-1); 649ed854c7bSeric } 65033db8731Seric else 65133db8731Seric return (0); 65233db8731Seric } 65333db8731Seric # endif DAEMON 65433db8731Seric 6556328bdf7Seric /* create a pipe to shove the mail through */ 656f8952a83Seric if (pipe(mpvect) < 0) 65725a99e2eSeric { 658c579ef51Seric syserr("pipe (to mailer)"); 65925a99e2eSeric return (-1); 66025a99e2eSeric } 661c579ef51Seric 6622c7e1b8dSeric # ifdef SMTP 663c579ef51Seric /* if this mailer speaks smtp, create a return pipe */ 664c579ef51Seric if (clever && pipe(rpvect) < 0) 665c579ef51Seric { 666c579ef51Seric syserr("pipe (from mailer)"); 667c579ef51Seric (void) close(mpvect[0]); 668c579ef51Seric (void) close(mpvect[1]); 669c579ef51Seric return (-1); 670c579ef51Seric } 6712c7e1b8dSeric # endif SMTP 672c579ef51Seric 67333db8731Seric /* 67433db8731Seric ** Actually fork the mailer process. 67533db8731Seric ** DOFORK is clever about retrying. 67633db8731Seric */ 67733db8731Seric 67832d19d43Seric DOFORK(XFORK); 679f129ec7dSeric /* pid is set by DOFORK */ 68025a99e2eSeric if (pid < 0) 68125a99e2eSeric { 68233db8731Seric /* failure */ 68325a99e2eSeric syserr("Cannot fork"); 684f8952a83Seric (void) close(mpvect[0]); 685f8952a83Seric (void) close(mpvect[1]); 686c579ef51Seric if (clever) 687c579ef51Seric { 688c579ef51Seric (void) close(rpvect[0]); 689c579ef51Seric (void) close(rpvect[1]); 690c579ef51Seric } 69125a99e2eSeric return (-1); 69225a99e2eSeric } 69325a99e2eSeric else if (pid == 0) 69425a99e2eSeric { 69525a99e2eSeric /* child -- set up input & exec mailer */ 69603ab8e55Seric /* make diagnostic output be standard output */ 6978f0e7860Seric (void) signal(SIGINT, SIG_IGN); 6988f0e7860Seric (void) signal(SIGHUP, SIG_IGN); 6990984da9fSeric (void) signal(SIGTERM, SIG_DFL); 700f8952a83Seric 701f8952a83Seric /* arrange to filter standard & diag output of command */ 702c579ef51Seric if (clever) 703c579ef51Seric { 704c579ef51Seric (void) close(rpvect[0]); 705c579ef51Seric (void) close(1); 706c579ef51Seric (void) dup(rpvect[1]); 707c579ef51Seric (void) close(rpvect[1]); 708c579ef51Seric } 709c579ef51Seric else if (OutChannel != stdout) 710f8952a83Seric { 711f8952a83Seric (void) close(1); 712f8952a83Seric (void) dup(fileno(OutChannel)); 713f8952a83Seric } 714db8841e9Seric (void) close(2); 715db8841e9Seric (void) dup(1); 716f8952a83Seric 717f8952a83Seric /* arrange to get standard input */ 718f8952a83Seric (void) close(mpvect[1]); 719db8841e9Seric (void) close(0); 720f8952a83Seric if (dup(mpvect[0]) < 0) 72125a99e2eSeric { 72225a99e2eSeric syserr("Cannot dup to zero!"); 723a590b978Seric _exit(EX_OSERR); 72425a99e2eSeric } 725f8952a83Seric (void) close(mpvect[0]); 7262a6e0786Seric if (!bitset(M_RESTR, m->m_flags)) 7270984da9fSeric { 728e36b99e2Seric if (ctladdr->q_uid == 0) 729e36b99e2Seric { 730e36b99e2Seric (void) setgid(DefGid); 731e36b99e2Seric (void) setuid(DefUid); 732e36b99e2Seric } 733e36b99e2Seric else 73469f29479Seric { 735e36b99e2Seric (void) setgid(ctladdr->q_gid); 736e36b99e2Seric (void) setuid(ctladdr->q_uid); 73769f29479Seric } 7380984da9fSeric } 739e374fd72Seric # ifndef VFORK 740e374fd72Seric /* 741e374fd72Seric ** We have to be careful with vfork - we can't mung up the 742e374fd72Seric ** memory but we don't want the mailer to inherit any extra 743e374fd72Seric ** open files. Chances are the mailer won't 744e374fd72Seric ** care about an extra file, but then again you never know. 745e374fd72Seric ** Actually, we would like to close(fileno(pwf)), but it's 746e374fd72Seric ** declared static so we can't. But if we fclose(pwf), which 747e374fd72Seric ** is what endpwent does, it closes it in the parent too and 748e374fd72Seric ** the next getpwnam will be slower. If you have a weird 749e374fd72Seric ** mailer that chokes on the extra file you should do the 750e374fd72Seric ** endpwent(). 751e374fd72Seric ** 752e374fd72Seric ** Similar comments apply to log. However, openlog is 753e374fd72Seric ** clever enough to set the FIOCLEX mode on the file, 754e374fd72Seric ** so it will be closed automatically on the exec. 755e374fd72Seric */ 756e374fd72Seric 757e374fd72Seric endpwent(); 75825a99e2eSeric # ifdef LOG 759f9fe028fSeric closelog(); 76025a99e2eSeric # endif LOG 761e374fd72Seric # endif VFORK 76233db8731Seric 76333db8731Seric /* try to execute the mailer */ 76425a99e2eSeric execv(m->m_mailer, pvp); 76533db8731Seric 76625a99e2eSeric /* syserr fails because log is closed */ 76725a99e2eSeric /* syserr("Cannot exec %s", m->m_mailer); */ 76832d19d43Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 769db8841e9Seric (void) fflush(stdout); 770a590b978Seric _exit(EX_UNAVAILABLE); 77125a99e2eSeric } 77225a99e2eSeric 773f8952a83Seric /* 774c579ef51Seric ** Set up return value. 775f8952a83Seric */ 776f8952a83Seric 777f8952a83Seric (void) close(mpvect[0]); 778f8952a83Seric mfile = fdopen(mpvect[1], "w"); 779c579ef51Seric if (clever) 78025a99e2eSeric { 781c579ef51Seric (void) close(rpvect[1]); 782c579ef51Seric rfile = fdopen(rpvect[0], "r"); 78325a99e2eSeric } 784c579ef51Seric 785c579ef51Seric *pmfile = mfile; 786c579ef51Seric *prfile = rfile; 787c579ef51Seric 788c579ef51Seric return (pid); 78925a99e2eSeric } 79025a99e2eSeric /* 79125a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 79225a99e2eSeric ** 79325a99e2eSeric ** Parameters: 79425a99e2eSeric ** stat -- the status code from the mailer (high byte 79525a99e2eSeric ** only; core dumps must have been taken care of 79625a99e2eSeric ** already). 79725a99e2eSeric ** force -- if set, force an error message output, even 79825a99e2eSeric ** if the mailer seems to like to print its own 79925a99e2eSeric ** messages. 80025a99e2eSeric ** m -- the mailer descriptor for this mailer. 80125a99e2eSeric ** 80225a99e2eSeric ** Returns: 803db8841e9Seric ** none. 80425a99e2eSeric ** 80525a99e2eSeric ** Side Effects: 806c1f9df2cSeric ** Errors may be incremented. 80725a99e2eSeric ** ExitStat may be set. 80825a99e2eSeric */ 80925a99e2eSeric 81025a99e2eSeric giveresponse(stat, force, m) 81125a99e2eSeric int stat; 81225a99e2eSeric int force; 81325a99e2eSeric register struct mailer *m; 81425a99e2eSeric { 81525a99e2eSeric register char *statmsg; 81625a99e2eSeric extern char *SysExMsg[]; 81725a99e2eSeric register int i; 81825a99e2eSeric extern int N_SysEx; 81929dd97a3Seric char buf[30]; 82025a99e2eSeric 82113bbc08cSeric /* 82213bbc08cSeric ** Compute status message from code. 82313bbc08cSeric */ 82413bbc08cSeric 82525a99e2eSeric i = stat - EX__BASE; 82625a99e2eSeric if (i < 0 || i > N_SysEx) 82725a99e2eSeric statmsg = NULL; 82825a99e2eSeric else 82925a99e2eSeric statmsg = SysExMsg[i]; 83025a99e2eSeric if (stat == 0) 831c38ba59cSeric { 8326cbfa739Seric if (bitset(M_LOCAL, m->m_flags)) 8333efaed6eSeric statmsg = "delivered"; 8343efaed6eSeric else 8353efaed6eSeric statmsg = "queued"; 836c38ba59cSeric if (Verbose) 837544026c5Seric message(Arpa_Info, statmsg); 838c38ba59cSeric } 8392c7e1b8dSeric # ifdef QUEUE 840c77d1c25Seric else if (stat == EX_TEMPFAIL) 841c77d1c25Seric { 842c77d1c25Seric if (Verbose) 843c77d1c25Seric message(Arpa_Info, "transmission deferred"); 844c77d1c25Seric } 8452c7e1b8dSeric # endif QUEUE 84625a99e2eSeric else 84725a99e2eSeric { 848c1f9df2cSeric Errors++; 849ed854c7bSeric FatalErrors = TRUE; 85025a99e2eSeric if (statmsg == NULL && m->m_badstat != 0) 85125a99e2eSeric { 85225a99e2eSeric stat = m->m_badstat; 85325a99e2eSeric i = stat - EX__BASE; 85425a99e2eSeric # ifdef DEBUG 85525a99e2eSeric if (i < 0 || i >= N_SysEx) 85625a99e2eSeric syserr("Bad m_badstat %d", stat); 85725a99e2eSeric else 85825a99e2eSeric # endif DEBUG 85925a99e2eSeric statmsg = SysExMsg[i]; 86025a99e2eSeric } 86125a99e2eSeric if (statmsg == NULL) 86225a99e2eSeric usrerr("unknown mailer response %d", stat); 863c38ba59cSeric else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 86425a99e2eSeric usrerr("%s", statmsg); 86525a99e2eSeric } 86625a99e2eSeric 86725a99e2eSeric /* 86825a99e2eSeric ** Final cleanup. 86925a99e2eSeric ** Log a record of the transaction. Compute the new 87025a99e2eSeric ** ExitStat -- if we already had an error, stick with 87125a99e2eSeric ** that. 87225a99e2eSeric */ 87325a99e2eSeric 87425a99e2eSeric if (statmsg == NULL) 87529dd97a3Seric { 876db8841e9Seric (void) sprintf(buf, "error %d", stat); 87729dd97a3Seric statmsg = buf; 87829dd97a3Seric } 87929dd97a3Seric 88029dd97a3Seric # ifdef LOG 8817a941e7aSeric syslog(LOG_INFO, "%s->%s: %ld: %s", CurEnv->e_from.q_paddr, CurEnv->e_to, CurEnv->e_msgsize, statmsg); 88225a99e2eSeric # endif LOG 8832c7e1b8dSeric # ifdef QUEUE 884c77d1c25Seric if (stat != EX_TEMPFAIL) 8852c7e1b8dSeric # endif QUEUE 886243921eeSeric setstat(stat); 88725a99e2eSeric } 88825a99e2eSeric /* 889*51552439Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 89025a99e2eSeric ** 891*51552439Seric ** This can be made an arbitrary message separator by changing $l 892*51552439Seric ** 893*51552439Seric ** One of the ugliest hacks seen by human eyes is 894*51552439Seric ** contained herein: UUCP wants those stupid 895*51552439Seric ** "remote from <host>" lines. Why oh why does a 896*51552439Seric ** well-meaning programmer such as myself have to 897*51552439Seric ** deal with this kind of antique garbage???? 89825a99e2eSeric ** 89925a99e2eSeric ** Parameters: 900*51552439Seric ** fp -- the file to output to. 901*51552439Seric ** m -- the mailer describing this entry. 90225a99e2eSeric ** 90325a99e2eSeric ** Returns: 904*51552439Seric ** none 90525a99e2eSeric ** 90625a99e2eSeric ** Side Effects: 907*51552439Seric ** outputs some text to fp. 90825a99e2eSeric */ 90925a99e2eSeric 910*51552439Seric putfromline(fp, m) 911*51552439Seric register FILE *fp; 912*51552439Seric register MAILER *m; 91325a99e2eSeric { 914*51552439Seric char buf[MAXLINE]; 91525a99e2eSeric 916*51552439Seric if (bitset(M_NHDR, m->m_flags)) 917*51552439Seric return; 91813bbc08cSeric 9192c7e1b8dSeric # ifdef UGLYUUCP 920a49f24c0Seric if (bitset(M_UGLYUUCP, m->m_flags)) 92174b6e67bSeric { 92274b6e67bSeric extern char *macvalue(); 92374b6e67bSeric char *sys = macvalue('g'); 92474b6e67bSeric char *bang = index(sys, '!'); 92574b6e67bSeric 92674b6e67bSeric if (bang == NULL) 92774b6e67bSeric syserr("No ! in UUCP! (%s)", sys); 92874b6e67bSeric else 92974b6e67bSeric *bang = '\0'; 930*51552439Seric expand("From $f $d remote from $g", buf, 931*51552439Seric &buf[sizeof buf - 1], CurEnv); 93274b6e67bSeric *bang = '!'; 93374b6e67bSeric } 934a36e30c9Seric else 9352c7e1b8dSeric # endif UGLYUUCP 936*51552439Seric expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv); 937*51552439Seric fputs(buf, fp); 938bc6e2962Seric } 939bc6e2962Seric /* 940*51552439Seric ** PUTHEADER -- put the header part of a message from the in-core copy 941bc6e2962Seric ** 942bc6e2962Seric ** Parameters: 943bc6e2962Seric ** fp -- file to put it on. 944bc6e2962Seric ** m -- mailer to use. 945*51552439Seric ** e -- envelope to use. 946bc6e2962Seric ** 947bc6e2962Seric ** Returns: 948bc6e2962Seric ** none. 949bc6e2962Seric ** 950bc6e2962Seric ** Side Effects: 951bc6e2962Seric ** none. 952bc6e2962Seric */ 953bc6e2962Seric 954*51552439Seric putheader(fp, m, e) 955bc6e2962Seric register FILE *fp; 956bc6e2962Seric register struct mailer *m; 957*51552439Seric register ENVELOPE *e; 958bc6e2962Seric { 959bc6e2962Seric char buf[BUFSIZ]; 960bc6e2962Seric register HDR *h; 961bc6e2962Seric extern char *arpadate(); 962bc6e2962Seric extern char *capitalize(); 963bc6e2962Seric extern char *hvalue(); 964bc6e2962Seric extern bool samefrom(); 965bc6e2962Seric char *of_line; 966bc6e2962Seric 967894de7daSeric of_line = hvalue("original-from"); 968*51552439Seric for (h = e->e_header; h != NULL; h = h->h_link) 9696328bdf7Seric { 97013bbc08cSeric register char *p; 971*51552439Seric char *origfrom = e->e_origfrom; 972894de7daSeric bool nooutput; 97313bbc08cSeric 974894de7daSeric nooutput = FALSE; 9759e9163a0Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 976894de7daSeric nooutput = TRUE; 977894de7daSeric 978894de7daSeric /* use From: line from message if generated is the same */ 97987bd9a02Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL && 980894de7daSeric strcmp(m->m_from, "$f") == 0 && of_line == NULL) 98187bd9a02Seric { 98287bd9a02Seric p = origfrom; 98387bd9a02Seric origfrom = NULL; 98487bd9a02Seric } 98587bd9a02Seric else if (bitset(H_DEFAULT, h->h_flags)) 9864ae18d0eSeric { 987*51552439Seric expand(h->h_value, buf, &buf[sizeof buf], e); 9884ae18d0eSeric p = buf; 9894ae18d0eSeric } 9908c2bf25bSeric else if (bitset(H_ADDR, h->h_flags)) 9918c2bf25bSeric { 9928c2bf25bSeric register int opos; 9938c2bf25bSeric bool firstone = TRUE; 9948c2bf25bSeric 9958c2bf25bSeric /* 9968c2bf25bSeric ** Output the address list translated by the 9978c2bf25bSeric ** mailer and with commas. 9988c2bf25bSeric */ 9998c2bf25bSeric 10008c2bf25bSeric p = h->h_value; 10018c2bf25bSeric if (p == NULL || *p == '\0' || nooutput) 10028c2bf25bSeric continue; 10038c2bf25bSeric fprintf(fp, "%s: ", capitalize(h->h_field)); 10048c2bf25bSeric opos = strlen(h->h_field) + 2; 10058c2bf25bSeric while (*p != '\0') 10068c2bf25bSeric { 10078c2bf25bSeric register char *name = p; 10088c2bf25bSeric extern char *remotename(); 10098c2bf25bSeric char savechar; 10108c2bf25bSeric 10118c2bf25bSeric /* find the end of the name */ 1012bc28c57aSeric while (*p != '\0' && *p != ',') 1013bc28c57aSeric { 1014bc28c57aSeric extern bool isatword(); 1015bc28c57aSeric char *oldp; 1016bc28c57aSeric 1017*51552439Seric if (!e->e_oldstyle || !isspace(*p)) 1018bc28c57aSeric { 10198c2bf25bSeric p++; 1020bc28c57aSeric continue; 1021bc28c57aSeric } 1022bc28c57aSeric oldp = p; 1023bc28c57aSeric while (*p != '\0' && isspace(*p)) 1024bc28c57aSeric p++; 1025bc28c57aSeric if (*p != '@' && !isatword(p)) 1026bc28c57aSeric { 1027bc28c57aSeric p = oldp; 1028bc28c57aSeric break; 1029bc28c57aSeric } 1030bc28c57aSeric p += *p == '@' ? 1 : 2; 1031bc28c57aSeric while (*p != '\0' && isspace(*p)) 1032bc28c57aSeric p++; 1033bc28c57aSeric } 10348c2bf25bSeric savechar = *p; 10358c2bf25bSeric *p = '\0'; 10368c2bf25bSeric 10378c2bf25bSeric /* translate the name to be relative */ 1038f60462a7Seric name = remotename(name, m, FALSE); 10398c2bf25bSeric if (*name == '\0') 10408c2bf25bSeric continue; 10418c2bf25bSeric 10428c2bf25bSeric /* output the name with nice formatting */ 10438c2bf25bSeric opos += strlen(name); 10448c2bf25bSeric if (!firstone) 10458c2bf25bSeric opos += 2; 10468c2bf25bSeric if (opos > 78 && !firstone) 10478c2bf25bSeric { 10488c2bf25bSeric fprintf(fp, ",\n "); 1049bc213ac6Seric opos = 8 + strlen(name); 10508c2bf25bSeric } 10518c2bf25bSeric else if (!firstone) 10528c2bf25bSeric fprintf(fp, ", "); 10538c2bf25bSeric fprintf(fp, "%s", name); 10548c2bf25bSeric firstone = FALSE; 10558c2bf25bSeric 10568c2bf25bSeric /* clean up the source string */ 10578c2bf25bSeric *p = savechar; 10588c2bf25bSeric while (*p != '\0' && (isspace(*p) || *p == ',')) 10598c2bf25bSeric p++; 10608c2bf25bSeric } 10618c2bf25bSeric fprintf(fp, "\n"); 10628c2bf25bSeric nooutput = TRUE; 10638c2bf25bSeric } 10644ae18d0eSeric else 10654ae18d0eSeric p = h->h_value; 1066894de7daSeric if (p == NULL || *p == '\0') 10679e9163a0Seric continue; 10685a0dcb5fSeric 10695a0dcb5fSeric /* hack, hack -- output Original-From field if different */ 1070894de7daSeric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL) 1071894de7daSeric { 1072894de7daSeric /* output new Original-From line if needed */ 1073894de7daSeric if (of_line == NULL && !samefrom(p, origfrom)) 107487bd9a02Seric fprintf(fp, "Original-From: %s\n", origfrom); 1075894de7daSeric if (of_line != NULL && !nooutput && samefrom(p, of_line)) 1076894de7daSeric { 1077894de7daSeric /* delete Original-From: line if redundant */ 1078894de7daSeric p = of_line; 1079894de7daSeric of_line = NULL; 1080894de7daSeric } 1081894de7daSeric } 1082894de7daSeric else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL) 1083894de7daSeric nooutput = TRUE; 1084894de7daSeric 1085894de7daSeric /* finally, output the header line */ 1086894de7daSeric if (!nooutput) 1087894de7daSeric { 1088894de7daSeric fprintf(fp, "%s: %s\n", capitalize(h->h_field), p); 1089894de7daSeric h->h_flags |= H_USED; 1090894de7daSeric } 10916328bdf7Seric } 1092*51552439Seric } 1093*51552439Seric /* 1094*51552439Seric ** PUTBODY -- put the body of a message. 1095*51552439Seric ** 1096*51552439Seric ** Parameters: 1097*51552439Seric ** fp -- file to output onto. 1098*51552439Seric ** m -- a mailer descriptor. 1099*51552439Seric ** xdot -- if set, use SMTP hidden dot algorithm. 1100*51552439Seric ** 1101*51552439Seric ** Returns: 1102*51552439Seric ** none. 1103*51552439Seric ** 1104*51552439Seric ** Side Effects: 1105*51552439Seric ** The message is written onto fp. 1106*51552439Seric */ 1107*51552439Seric 1108*51552439Seric putbody(fp, m, xdot) 1109*51552439Seric FILE *fp; 1110*51552439Seric struct mailer *m; 1111*51552439Seric bool xdot; 1112*51552439Seric { 1113*51552439Seric char buf[MAXLINE + 1]; 1114*51552439Seric 1115*51552439Seric /* 1116*51552439Seric ** Output the body of the message 1117*51552439Seric */ 1118*51552439Seric 1119*51552439Seric if (TempFile != NULL) 1120*51552439Seric { 1121*51552439Seric rewind(TempFile); 1122*51552439Seric buf[0] = '.'; 1123*51552439Seric while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL) 1124*51552439Seric fputs((xdot && buf[1] == '.') ? buf : &buf[1], fp); 1125*51552439Seric 1126*51552439Seric if (ferror(TempFile)) 1127*51552439Seric { 1128*51552439Seric syserr("putbody: read error"); 1129*51552439Seric ExitStat = EX_IOERR; 1130*51552439Seric } 1131*51552439Seric } 1132*51552439Seric 1133*51552439Seric (void) fflush(fp); 1134*51552439Seric if (ferror(fp) && errno != EPIPE) 1135*51552439Seric { 1136*51552439Seric syserr("putbody: write error"); 1137*51552439Seric ExitStat = EX_IOERR; 1138*51552439Seric } 1139*51552439Seric errno = 0; 114025a99e2eSeric } 114125a99e2eSeric /* 1142bc28c57aSeric ** ISATWORD -- tell if the word we are pointing to is "at". 1143bc28c57aSeric ** 1144bc28c57aSeric ** Parameters: 1145bc28c57aSeric ** p -- word to check. 1146bc28c57aSeric ** 1147bc28c57aSeric ** Returns: 1148bc28c57aSeric ** TRUE -- if p is the word at. 1149bc28c57aSeric ** FALSE -- otherwise. 1150bc28c57aSeric ** 1151bc28c57aSeric ** Side Effects: 1152bc28c57aSeric ** none. 1153bc28c57aSeric */ 1154bc28c57aSeric 1155bc28c57aSeric bool 1156bc28c57aSeric isatword(p) 1157bc28c57aSeric register char *p; 1158bc28c57aSeric { 1159bc28c57aSeric extern char lower(); 1160bc28c57aSeric 1161bc28c57aSeric if (lower(p[0]) == 'a' && lower(p[1]) == 't' && 1162bc28c57aSeric p[2] != '\0' && isspace(p[2])) 1163bc28c57aSeric return (TRUE); 1164bc28c57aSeric return (FALSE); 1165bc28c57aSeric } 1166bc28c57aSeric /* 11678c2bf25bSeric ** REMOTENAME -- return the name relative to the current mailer 11688c2bf25bSeric ** 11698c2bf25bSeric ** Parameters: 11708c2bf25bSeric ** name -- the name to translate. 1171f60462a7Seric ** force -- if set, forces rewriting even if the mailer 1172f60462a7Seric ** does not request it. Used for rewriting 1173f60462a7Seric ** sender addresses. 11748c2bf25bSeric ** 11758c2bf25bSeric ** Returns: 11768c2bf25bSeric ** the text string representing this address relative to 11778c2bf25bSeric ** the receiving mailer. 11788c2bf25bSeric ** 11798c2bf25bSeric ** Side Effects: 11808c2bf25bSeric ** none. 11818c2bf25bSeric ** 11828c2bf25bSeric ** Warnings: 11838c2bf25bSeric ** The text string returned is tucked away locally; 11848c2bf25bSeric ** copy it if you intend to save it. 11858c2bf25bSeric */ 11868c2bf25bSeric 11878c2bf25bSeric char * 1188f60462a7Seric remotename(name, m, force) 11898c2bf25bSeric char *name; 1190bc213ac6Seric struct mailer *m; 1191f60462a7Seric bool force; 11928c2bf25bSeric { 11938c2bf25bSeric static char buf[MAXNAME]; 11948c2bf25bSeric char lbuf[MAXNAME]; 11958c2bf25bSeric extern char *macvalue(); 11968c2bf25bSeric char *oldf = macvalue('f'); 1197bc213ac6Seric char *oldx = macvalue('x'); 1198bc213ac6Seric char *oldg = macvalue('g'); 11998c2bf25bSeric extern char **prescan(); 12008c2bf25bSeric register char **pvp; 1201bc213ac6Seric extern char *getxpart(); 12028c2bf25bSeric 12038c2bf25bSeric /* 1204f60462a7Seric ** See if this mailer wants the name to be rewritten. There are 1205f60462a7Seric ** many problems here, owing to the standards for doing replies. 1206f60462a7Seric ** In general, these names should only be rewritten if we are 1207f60462a7Seric ** sending to another host that runs sendmail. 1208f60462a7Seric */ 1209f60462a7Seric 1210f60462a7Seric if (!bitset(M_RELRCPT, m->m_flags) && !force) 1211f60462a7Seric return; 1212f60462a7Seric 1213f60462a7Seric /* 12148c2bf25bSeric ** Do general rewriting of name. 12158c2bf25bSeric ** This will also take care of doing global name translation. 12168c2bf25bSeric */ 12178c2bf25bSeric 1218bc213ac6Seric define('x', getxpart(name)); 12198c2bf25bSeric pvp = prescan(name, '\0'); 12208c2bf25bSeric for (;;) 12218c2bf25bSeric { 12228c2bf25bSeric rewrite(pvp, 1); 12238c2bf25bSeric rewrite(pvp, 3); 12248c2bf25bSeric if (**pvp == CANONNET) 12258c2bf25bSeric { 12268c2bf25bSeric auto ADDRESS a; 12278c2bf25bSeric register char *p; 12288c2bf25bSeric extern char *hostalias(); 12298c2bf25bSeric 12308c2bf25bSeric /* oops... resolved to something */ 12318c2bf25bSeric if (buildaddr(pvp, &a) == NULL) 12328c2bf25bSeric return (name); 12338c2bf25bSeric p = hostalias(&a); 12348c2bf25bSeric if (p == NULL) 12358c2bf25bSeric return (name); 12368c2bf25bSeric pvp = prescan(p, '\0'); 12378c2bf25bSeric } 12388c2bf25bSeric else 12398c2bf25bSeric { 12408c2bf25bSeric cataddr(pvp, lbuf, sizeof lbuf); 12418c2bf25bSeric break; 12428c2bf25bSeric } 12438c2bf25bSeric } 12448c2bf25bSeric 12458c2bf25bSeric /* make the name relative to the receiving mailer */ 12468c2bf25bSeric define('f', lbuf); 1247*51552439Seric expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv); 12488c2bf25bSeric 12498c2bf25bSeric /* rewrite to get rid of garbage we added in the expand above */ 12508c2bf25bSeric pvp = prescan(buf, '\0'); 12518c2bf25bSeric rewrite(pvp, 2); 1252bc213ac6Seric cataddr(pvp, lbuf, sizeof lbuf); 1253bc213ac6Seric 1254bc213ac6Seric /* now add any comment info we had before back */ 1255bc213ac6Seric define('g', lbuf); 1256*51552439Seric expand("$q", buf, &buf[sizeof buf - 1], CurEnv); 12578c2bf25bSeric 12588c2bf25bSeric define('f', oldf); 1259bc213ac6Seric define('g', oldg); 1260bc213ac6Seric define('x', oldx); 12618c2bf25bSeric 12628c2bf25bSeric # ifdef DEBUG 12638c2bf25bSeric if (Debug > 0) 12648c2bf25bSeric printf("remotename(%s) => `%s'\n", name, buf); 12658c2bf25bSeric # endif DEBUG 12668c2bf25bSeric return (buf); 12678c2bf25bSeric } 12688c2bf25bSeric /* 126987bd9a02Seric ** SAMEFROM -- tell if two text addresses represent the same from address. 127087bd9a02Seric ** 127187bd9a02Seric ** Parameters: 127287bd9a02Seric ** ifrom -- internally generated form of from address. 127387bd9a02Seric ** efrom -- external form of from address. 127487bd9a02Seric ** 127587bd9a02Seric ** Returns: 127687bd9a02Seric ** TRUE -- if they convey the same info. 127787bd9a02Seric ** FALSE -- if any information has been lost. 127887bd9a02Seric ** 127987bd9a02Seric ** Side Effects: 128087bd9a02Seric ** none. 128187bd9a02Seric */ 128287bd9a02Seric 128387bd9a02Seric bool 128487bd9a02Seric samefrom(ifrom, efrom) 128587bd9a02Seric char *ifrom; 128687bd9a02Seric char *efrom; 128787bd9a02Seric { 1288894de7daSeric register char *p; 1289894de7daSeric char buf[MAXNAME + 4]; 1290894de7daSeric 1291894de7daSeric # ifdef DEBUG 1292894de7daSeric if (Debug > 7) 1293894de7daSeric printf("samefrom(%s,%s)-->", ifrom, efrom); 1294894de7daSeric # endif DEBUG 1295894de7daSeric if (strcmp(ifrom, efrom) == 0) 1296894de7daSeric goto success; 1297894de7daSeric p = index(ifrom, '@'); 1298894de7daSeric if (p == NULL) 1299894de7daSeric goto failure; 1300894de7daSeric *p = '\0'; 1301894de7daSeric strcpy(buf, ifrom); 1302894de7daSeric strcat(buf, " at "); 1303894de7daSeric *p++ = '@'; 1304894de7daSeric strcat(buf, p); 1305894de7daSeric if (strcmp(buf, efrom) == 0) 1306894de7daSeric goto success; 1307894de7daSeric 1308894de7daSeric failure: 1309894de7daSeric # ifdef DEBUG 1310894de7daSeric if (Debug > 7) 1311894de7daSeric printf("FALSE\n"); 1312894de7daSeric # endif DEBUG 1313894de7daSeric return (FALSE); 1314894de7daSeric 1315894de7daSeric success: 1316894de7daSeric # ifdef DEBUG 1317894de7daSeric if (Debug > 7) 1318894de7daSeric printf("TRUE\n"); 1319894de7daSeric # endif DEBUG 1320894de7daSeric return (TRUE); 132187bd9a02Seric } 132287bd9a02Seric /* 132325a99e2eSeric ** MAILFILE -- Send a message to a file. 132425a99e2eSeric ** 1325f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 1326f129ec7dSeric ** bits, sendmail will try to become the owner of that file 1327f129ec7dSeric ** rather than the real user. Obviously, this only works if 1328f129ec7dSeric ** sendmail runs as root. 1329f129ec7dSeric ** 133025a99e2eSeric ** Parameters: 133125a99e2eSeric ** filename -- the name of the file to send to. 13326259796dSeric ** ctladdr -- the controlling address header -- includes 13336259796dSeric ** the userid/groupid to be when sending. 133425a99e2eSeric ** 133525a99e2eSeric ** Returns: 133625a99e2eSeric ** The exit code associated with the operation. 133725a99e2eSeric ** 133825a99e2eSeric ** Side Effects: 133925a99e2eSeric ** none. 134025a99e2eSeric */ 134125a99e2eSeric 13426259796dSeric mailfile(filename, ctladdr) 134325a99e2eSeric char *filename; 13446259796dSeric ADDRESS *ctladdr; 134525a99e2eSeric { 134625a99e2eSeric register FILE *f; 134732d19d43Seric register int pid; 134825a99e2eSeric 134932d19d43Seric /* 135032d19d43Seric ** Fork so we can change permissions here. 135132d19d43Seric ** Note that we MUST use fork, not vfork, because of 135232d19d43Seric ** the complications of calling subroutines, etc. 135332d19d43Seric */ 135432d19d43Seric 135532d19d43Seric DOFORK(fork); 135632d19d43Seric 135732d19d43Seric if (pid < 0) 135832d19d43Seric return (EX_OSERR); 135932d19d43Seric else if (pid == 0) 136032d19d43Seric { 136132d19d43Seric /* child -- actually write to file */ 1362f129ec7dSeric struct stat stb; 1363f129ec7dSeric 13640984da9fSeric (void) signal(SIGINT, SIG_DFL); 13650984da9fSeric (void) signal(SIGHUP, SIG_DFL); 13660984da9fSeric (void) signal(SIGTERM, SIG_DFL); 1367f129ec7dSeric umask(OldUmask); 1368f129ec7dSeric if (stat(filename, &stb) < 0) 1369e6e1265fSeric stb.st_mode = 0666; 1370f129ec7dSeric if (bitset(0111, stb.st_mode)) 1371f129ec7dSeric exit(EX_CANTCREAT); 137203827b5fSeric if (ctladdr == NULL) 13737a941e7aSeric ctladdr = &CurEnv->e_from; 1374f129ec7dSeric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 1375e36b99e2Seric { 1376e36b99e2Seric if (ctladdr->q_uid == 0) 1377e36b99e2Seric (void) setgid(DefGid); 1378e36b99e2Seric else 13796259796dSeric (void) setgid(ctladdr->q_gid); 1380e36b99e2Seric } 1381f129ec7dSeric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 1382e36b99e2Seric { 1383e36b99e2Seric if (ctladdr->q_uid == 0) 1384e36b99e2Seric (void) setuid(DefUid); 1385e36b99e2Seric else 13866259796dSeric (void) setuid(ctladdr->q_uid); 1387e36b99e2Seric } 138827628d59Seric f = dfopen(filename, "a"); 138925a99e2eSeric if (f == NULL) 139032d19d43Seric exit(EX_CANTCREAT); 139125a99e2eSeric 1392*51552439Seric putfromline(f, Mailer[1]); 1393*51552439Seric (*CurEnv->e_puthdr)(f, Mailer[1], CurEnv); 1394*51552439Seric fprintf(f, "\n"); 1395*51552439Seric (*CurEnv->e_putbody)(f, Mailer[1], FALSE); 139625a99e2eSeric fputs("\n", f); 1397db8841e9Seric (void) fclose(f); 139832d19d43Seric (void) fflush(stdout); 1399e36b99e2Seric 140027628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 1401c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 140232d19d43Seric exit(EX_OK); 140313bbc08cSeric /*NOTREACHED*/ 140432d19d43Seric } 140532d19d43Seric else 140632d19d43Seric { 140732d19d43Seric /* parent -- wait for exit status */ 140832d19d43Seric register int i; 140932d19d43Seric auto int stat; 141032d19d43Seric 141132d19d43Seric while ((i = wait(&stat)) != pid) 141232d19d43Seric { 141332d19d43Seric if (i < 0) 141432d19d43Seric { 141532d19d43Seric stat = EX_OSERR << 8; 141632d19d43Seric break; 141732d19d43Seric } 141832d19d43Seric } 14190984da9fSeric if ((stat & 0377) != 0) 14200984da9fSeric stat = EX_UNAVAILABLE << 8; 142132d19d43Seric return ((stat >> 8) & 0377); 142232d19d43Seric } 142325a99e2eSeric } 1424ea4dc939Seric /* 1425ea4dc939Seric ** SENDALL -- actually send all the messages. 1426ea4dc939Seric ** 1427ea4dc939Seric ** Parameters: 1428ea4dc939Seric ** verifyonly -- if set, only give verification messages. 1429ea4dc939Seric ** 1430ea4dc939Seric ** Returns: 1431ea4dc939Seric ** none. 1432ea4dc939Seric ** 1433ea4dc939Seric ** Side Effects: 1434ea4dc939Seric ** Scans the send lists and sends everything it finds. 1435ea4dc939Seric */ 1436ea4dc939Seric 1437ea4dc939Seric sendall(verifyonly) 1438ea4dc939Seric bool verifyonly; 1439ea4dc939Seric { 1440e77e673fSeric register ADDRESS *q; 1441ea4dc939Seric typedef int (*fnptr)(); 1442ea4dc939Seric 1443772e6e50Seric # ifdef DEBUG 1444772e6e50Seric if (Debug > 1) 1445772e6e50Seric { 1446772e6e50Seric printf("\nSend Queue:\n"); 14477a941e7aSeric printaddr(CurEnv->e_sendqueue, TRUE); 1448772e6e50Seric } 1449772e6e50Seric # endif DEBUG 1450ea4dc939Seric 14517a941e7aSeric for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next) 1452ea4dc939Seric { 1453ea4dc939Seric if (verifyonly) 1454ea4dc939Seric { 14557a941e7aSeric CurEnv->e_to = q->q_paddr; 1456e77e673fSeric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1457ea4dc939Seric { 14587da1035fSeric if (bitset(M_LOCAL, q->q_mailer->m_flags)) 1459ea4dc939Seric message(Arpa_Info, "deliverable"); 1460ea4dc939Seric else 1461ea4dc939Seric message(Arpa_Info, "queueable"); 1462ea4dc939Seric } 1463ea4dc939Seric } 1464ea4dc939Seric else 1465*51552439Seric (void) deliver(q); 1466ea4dc939Seric } 1467ea4dc939Seric } 1468