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*27628d59Seric SCCSID(@(#)deliver.c 3.78 05/20/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 ** editfcn -- if non-NULL, we want to call this function 2425a99e2eSeric ** to output the letter (instead of just out- 2525a99e2eSeric ** putting it raw). 2625a99e2eSeric ** 2725a99e2eSeric ** Returns: 2825a99e2eSeric ** zero -- successfully delivered. 2925a99e2eSeric ** else -- some failure, see ExitStat for more info. 3025a99e2eSeric ** 3125a99e2eSeric ** Side Effects: 3225a99e2eSeric ** The standard input is passed off to someone. 3325a99e2eSeric */ 3425a99e2eSeric 35c77d1c25Seric deliver(firstto, editfcn) 36c77d1c25Seric ADDRESS *firstto; 3725a99e2eSeric int (*editfcn)(); 3825a99e2eSeric { 3978442df3Seric char *host; /* host being sent to */ 4078442df3Seric char *user; /* user being sent to */ 4125a99e2eSeric char **pvp; 425dfc646bSeric register char **mvp; 4325a99e2eSeric register char *p; 4478442df3Seric register struct mailer *m; /* mailer for this recipient */ 455dfc646bSeric register int i; 466328bdf7Seric extern putmessage(); 472a6e0786Seric extern bool checkcompat(); 485dfc646bSeric char *pv[MAXPV+1]; 4978442df3Seric char tobuf[MAXLINE]; /* text line of to people */ 505dfc646bSeric char buf[MAXNAME]; 516259796dSeric ADDRESS *ctladdr; 526259796dSeric extern ADDRESS *getctladdr(); 5378442df3Seric char tfrombuf[MAXNAME]; /* translated from person */ 5478442df3Seric extern char **prescan(); 55c77d1c25Seric register ADDRESS *to = firstto; 56c579ef51Seric bool clever = FALSE; /* running user smtp to this mailer */ 57c579ef51Seric bool tempfail = FALSE; 58772e6e50Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 5925a99e2eSeric 6035490626Seric errno = 0; 61e77e673fSeric if (!ForceMail && bitset(QDONTSEND, to->q_flags)) 625dfc646bSeric return (0); 6325a99e2eSeric 6425a99e2eSeric # ifdef DEBUG 6525a99e2eSeric if (Debug) 665dfc646bSeric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 677da1035fSeric to->q_mailer->m_mno, to->q_host, to->q_user); 6825a99e2eSeric # endif DEBUG 6925a99e2eSeric 70f3dbc832Seric m = to->q_mailer; 71f3dbc832Seric host = to->q_host; 72f3dbc832Seric 73f3dbc832Seric /* 74f3dbc832Seric ** If this mailer is expensive, and if we don't want to make 75f3dbc832Seric ** connections now, just mark these addresses and return. 76f3dbc832Seric ** This is useful if we want to batch connections to 77f3dbc832Seric ** reduce load. This will cause the messages to be 78f3dbc832Seric ** queued up, and a daemon will come along to send the 79f3dbc832Seric ** messages later. 80f3dbc832Seric ** This should be on a per-mailer basis. 81f3dbc832Seric */ 82f3dbc832Seric 83f3dbc832Seric if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags)) 84f3dbc832Seric { 85f3dbc832Seric QueueUp = TRUE; 86f3dbc832Seric for (; to != NULL; to = to->q_next) 87f3dbc832Seric if (!bitset(QDONTSEND, to->q_flags)) 88f3dbc832Seric to->q_flags |= QQUEUEUP|QDONTSEND; 89f3dbc832Seric return (0); 90f3dbc832Seric } 91f3dbc832Seric 9225a99e2eSeric /* 935dfc646bSeric ** Do initial argv setup. 945dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 955dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 965dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 975dfc646bSeric ** code does not check for 'pv' overflow; this places a 985dfc646bSeric ** manifest lower limit of 4 for MAXPV. 99f3dbc832Seric ** We rewrite the from address here, being careful 100f3dbc832Seric ** to also rewrite it again using ruleset 2 to 101f3dbc832Seric ** eliminate redundancies. 1025dfc646bSeric */ 1035dfc646bSeric 10478442df3Seric /* rewrite from address, using rewriting rules */ 10578442df3Seric (void) expand(m->m_from, buf, &buf[sizeof buf - 1]); 10678442df3Seric mvp = prescan(buf, '\0'); 10778442df3Seric if (mvp == NULL) 10878442df3Seric { 10978442df3Seric syserr("bad mailer from translate \"%s\"", buf); 11078442df3Seric return (EX_SOFTWARE); 11178442df3Seric } 11278442df3Seric rewrite(mvp, 2); 11378442df3Seric cataddr(mvp, tfrombuf, sizeof tfrombuf); 11478442df3Seric 11578442df3Seric define('g', tfrombuf); /* translated sender address */ 1165dfc646bSeric define('h', host); /* to host */ 1175dfc646bSeric Errors = 0; 1185dfc646bSeric pvp = pv; 1195dfc646bSeric *pvp++ = m->m_argv[0]; 1205dfc646bSeric 1215dfc646bSeric /* insert -f or -r flag as appropriate */ 1225dfc646bSeric if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag) 1235dfc646bSeric { 1245dfc646bSeric if (bitset(M_FOPT, m->m_flags)) 1255dfc646bSeric *pvp++ = "-f"; 1265dfc646bSeric else 1275dfc646bSeric *pvp++ = "-r"; 128db8841e9Seric (void) expand("$g", buf, &buf[sizeof buf - 1]); 1295dfc646bSeric *pvp++ = newstr(buf); 1305dfc646bSeric } 1315dfc646bSeric 1325dfc646bSeric /* 1335dfc646bSeric ** Append the other fixed parts of the argv. These run 1345dfc646bSeric ** up to the first entry containing "$u". There can only 1355dfc646bSeric ** be one of these, and there are only a few more slots 1365dfc646bSeric ** in the pv after it. 1375dfc646bSeric */ 1385dfc646bSeric 1395dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 1405dfc646bSeric { 1415dfc646bSeric while ((p = index(p, '$')) != NULL) 1425dfc646bSeric if (*++p == 'u') 1435dfc646bSeric break; 1445dfc646bSeric if (p != NULL) 1455dfc646bSeric break; 1465dfc646bSeric 1475dfc646bSeric /* this entry is safe -- go ahead and process it */ 148db8841e9Seric (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 1495dfc646bSeric *pvp++ = newstr(buf); 1505dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 1515dfc646bSeric { 1525dfc646bSeric syserr("Too many parameters to %s before $u", pv[0]); 1535dfc646bSeric return (-1); 1545dfc646bSeric } 1555dfc646bSeric } 156c579ef51Seric 15733db8731Seric /* 15833db8731Seric ** If we have no substitution for the user name in the argument 15933db8731Seric ** list, we know that we must supply the names otherwise -- and 16033db8731Seric ** SMTP is the answer!! 16133db8731Seric */ 16233db8731Seric 1635dfc646bSeric if (*mvp == NULL) 164c579ef51Seric { 165c579ef51Seric /* running SMTP */ 1662c7e1b8dSeric # ifdef SMTP 167c579ef51Seric clever = TRUE; 168c579ef51Seric *pvp = NULL; 16933db8731Seric 17033db8731Seric /* send the initial SMTP protocol */ 171ed854c7bSeric smtpinit(m, pv, (ADDRESS *) NULL); 1722c7e1b8dSeric # ifdef QUEUE 173c579ef51Seric if (i == EX_TEMPFAIL) 174c579ef51Seric { 175c579ef51Seric QueueUp = TRUE; 176c579ef51Seric tempfail = TRUE; 177c579ef51Seric } 1782c7e1b8dSeric # endif QUEUE 1792c7e1b8dSeric # else SMTP 18033db8731Seric /* oops! we don't implement SMTP */ 1812c7e1b8dSeric syserr("SMTP style mailer"); 1822c7e1b8dSeric return (EX_SOFTWARE); 1832c7e1b8dSeric # endif SMTP 184c579ef51Seric } 1855dfc646bSeric 1865dfc646bSeric /* 1875dfc646bSeric ** At this point *mvp points to the argument with $u. We 1885dfc646bSeric ** run through our address list and append all the addresses 1895dfc646bSeric ** we can. If we run out of space, do not fret! We can 1905dfc646bSeric ** always send another copy later. 1915dfc646bSeric */ 1925dfc646bSeric 1935dfc646bSeric tobuf[0] = '\0'; 1945dfc646bSeric To = tobuf; 1956259796dSeric ctladdr = NULL; 1965dfc646bSeric for (; to != NULL; to = to->q_next) 1975dfc646bSeric { 1985dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 199bea22b26Seric if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags)) 2005dfc646bSeric break; 2015dfc646bSeric 2025dfc646bSeric /* if already sent or not for this host, don't send */ 203e77e673fSeric if ((!ForceMail && bitset(QDONTSEND, to->q_flags)) || 204e77e673fSeric strcmp(to->q_host, host) != 0 || to->q_mailer != firstto->q_mailer) 2055dfc646bSeric continue; 2066259796dSeric 207772e6e50Seric # ifdef DEBUG 208772e6e50Seric if (Debug) 209772e6e50Seric { 210772e6e50Seric printf("\nsend to "); 211772e6e50Seric printaddr(to, FALSE); 212772e6e50Seric } 213772e6e50Seric # endif DEBUG 214772e6e50Seric 2156259796dSeric /* compute effective uid/gid when sending */ 2167da1035fSeric if (to->q_mailer == ProgMailer) 2176259796dSeric ctladdr = getctladdr(to); 2186259796dSeric 2195dfc646bSeric user = to->q_user; 2205dfc646bSeric To = to->q_paddr; 2215dfc646bSeric to->q_flags |= QDONTSEND; 222c579ef51Seric if (tempfail) 223772e6e50Seric { 224c579ef51Seric to->q_flags |= QQUEUEUP; 225772e6e50Seric continue; 226772e6e50Seric } 2275dfc646bSeric 2285dfc646bSeric /* 2295dfc646bSeric ** Check to see that these people are allowed to 2305dfc646bSeric ** talk to each other. 2312a6e0786Seric */ 2322a6e0786Seric 2332a6e0786Seric if (!checkcompat(to)) 2345dfc646bSeric { 2355dfc646bSeric giveresponse(EX_UNAVAILABLE, TRUE, m); 2365dfc646bSeric continue; 2375dfc646bSeric } 2382a6e0786Seric 2392a6e0786Seric /* 2409ec9501bSeric ** Strip quote bits from names if the mailer is dumb 2419ec9501bSeric ** about them. 24225a99e2eSeric */ 24325a99e2eSeric 2442a6e0786Seric if (bitset(M_STRIPQ, m->m_flags)) 24525a99e2eSeric { 2469ec9501bSeric stripquotes(user, TRUE); 2479ec9501bSeric stripquotes(host, TRUE); 2489ec9501bSeric } 2499ec9501bSeric else 2509ec9501bSeric { 2519ec9501bSeric stripquotes(user, FALSE); 2529ec9501bSeric stripquotes(host, FALSE); 25325a99e2eSeric } 25425a99e2eSeric 25525a99e2eSeric /* 256508daeccSeric ** Pass it to the other host if we are running SMTP. 257508daeccSeric */ 258508daeccSeric 259508daeccSeric if (clever) 260508daeccSeric { 261508daeccSeric # ifdef SMTP 262508daeccSeric i = smtprcpt(to); 263508daeccSeric if (i != EX_OK) 264508daeccSeric { 265508daeccSeric # ifdef QUEUE 266508daeccSeric if (i == EX_TEMPFAIL) 267508daeccSeric { 268508daeccSeric QueueUp = TRUE; 269508daeccSeric to->q_flags |= QQUEUEUP; 270508daeccSeric } 271508daeccSeric else 272508daeccSeric # endif QUEUE 273508daeccSeric { 274508daeccSeric to->q_flags |= QBADADDR; 275508daeccSeric giveresponse(i, TRUE, m); 276508daeccSeric } 277508daeccSeric } 278508daeccSeric # else SMTP 279508daeccSeric syserr("trying to be clever"); 280508daeccSeric # endif SMTP 281508daeccSeric } 282508daeccSeric 283508daeccSeric /* 2843efaed6eSeric ** If an error message has already been given, don't 2853efaed6eSeric ** bother to send to this address. 2863efaed6eSeric ** 2873efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 2883efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 2893efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 2903efaed6eSeric */ 2913efaed6eSeric 2923efaed6eSeric if (bitset(QBADADDR, to->q_flags)) 2933efaed6eSeric continue; 2943efaed6eSeric 295f2fec898Seric /* save statistics.... */ 2967da1035fSeric Stat.stat_nt[to->q_mailer->m_mno]++; 2977da1035fSeric Stat.stat_bt[to->q_mailer->m_mno] += kbytes(MsgSize); 298f2fec898Seric 2993efaed6eSeric /* 30025a99e2eSeric ** See if this user name is "special". 30125a99e2eSeric ** If the user name has a slash in it, assume that this 30225a99e2eSeric ** is a file -- send it off without further ado. 30325a99e2eSeric ** Note that this means that editfcn's will not 3045dfc646bSeric ** be applied to the message. Also note that 3055dfc646bSeric ** this type of addresses is not processed along 3065dfc646bSeric ** with the others, so we fudge on the To person. 30725a99e2eSeric */ 30825a99e2eSeric 3097da1035fSeric if (m == LocalMailer) 31025a99e2eSeric { 311a49f24c0Seric if (user[0] == '/') 31225a99e2eSeric { 3136259796dSeric i = mailfile(user, getctladdr(to)); 31425a99e2eSeric giveresponse(i, TRUE, m); 3155dfc646bSeric continue; 31625a99e2eSeric } 31725a99e2eSeric } 31825a99e2eSeric 31913bbc08cSeric /* 32013bbc08cSeric ** Address is verified -- add this user to mailer 32113bbc08cSeric ** argv, and add it to the print list of recipients. 32213bbc08cSeric */ 32313bbc08cSeric 324508daeccSeric /* link together the chain of recipients */ 325508daeccSeric to->q_tchain = tochain; 326508daeccSeric tochain = to; 327508daeccSeric 3285dfc646bSeric /* create list of users for error messages */ 3295dfc646bSeric if (tobuf[0] != '\0') 330db8841e9Seric (void) strcat(tobuf, ","); 331db8841e9Seric (void) strcat(tobuf, to->q_paddr); 3325dfc646bSeric define('u', user); /* to user */ 333c2567733Seric define('z', to->q_home); /* user's home */ 3345dfc646bSeric 335c579ef51Seric /* 336508daeccSeric ** Expand out this user into argument list. 337c579ef51Seric */ 338c579ef51Seric 339508daeccSeric if (!clever) 340c579ef51Seric { 341d4ccc802Seric (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 3425dfc646bSeric *pvp++ = newstr(buf); 3435dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 3445dfc646bSeric { 3455dfc646bSeric /* allow some space for trailing parms */ 3465dfc646bSeric break; 3475dfc646bSeric } 3485dfc646bSeric } 349c579ef51Seric } 3505dfc646bSeric 351145b49b1Seric /* see if any addresses still exist */ 352145b49b1Seric if (tobuf[0] == '\0') 353c579ef51Seric { 3542c7e1b8dSeric # ifdef SMTP 355c579ef51Seric if (clever) 356c579ef51Seric smtpquit(pv[0]); 3572c7e1b8dSeric # endif SMTP 358145b49b1Seric return (0); 359c579ef51Seric } 360145b49b1Seric 3615dfc646bSeric /* print out messages as full list */ 3625dfc646bSeric To = tobuf; 3635dfc646bSeric 3645dfc646bSeric /* 3655dfc646bSeric ** Fill out any parameters after the $u parameter. 3665dfc646bSeric */ 3675dfc646bSeric 368c579ef51Seric while (!clever && *++mvp != NULL) 3695dfc646bSeric { 370db8841e9Seric (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 3715dfc646bSeric *pvp++ = newstr(buf); 3725dfc646bSeric if (pvp >= &pv[MAXPV]) 3735dfc646bSeric syserr("deliver: pv overflow after $u for %s", pv[0]); 3745dfc646bSeric } 3755dfc646bSeric *pvp++ = NULL; 3765dfc646bSeric 37725a99e2eSeric /* 37825a99e2eSeric ** Call the mailer. 3796328bdf7Seric ** The argument vector gets built, pipes 38025a99e2eSeric ** are created as necessary, and we fork & exec as 3816328bdf7Seric ** appropriate. 382c579ef51Seric ** If we are running SMTP, we just need to clean up. 38325a99e2eSeric */ 38425a99e2eSeric 3855dfc646bSeric if (editfcn == NULL) 3865dfc646bSeric editfcn = putmessage; 3876259796dSeric if (ctladdr == NULL) 3886259796dSeric ctladdr = &From; 3892c7e1b8dSeric # ifdef SMTP 390c579ef51Seric if (clever) 391c579ef51Seric { 392c579ef51Seric i = smtpfinish(m, editfcn); 393c579ef51Seric smtpquit(pv[0]); 394c579ef51Seric } 395c579ef51Seric else 3962c7e1b8dSeric # endif SMTP 3976259796dSeric i = sendoff(m, pv, editfcn, ctladdr); 3985dfc646bSeric 399c77d1c25Seric /* 400c77d1c25Seric ** If we got a temporary failure, arrange to queue the 401c77d1c25Seric ** addressees. 402c77d1c25Seric */ 403c77d1c25Seric 4042c7e1b8dSeric # ifdef QUEUE 405c77d1c25Seric if (i == EX_TEMPFAIL) 406c77d1c25Seric { 407c77d1c25Seric QueueUp = TRUE; 408772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 409c77d1c25Seric to->q_flags |= QQUEUEUP; 410c77d1c25Seric } 4112c7e1b8dSeric # endif QUEUE 412c77d1c25Seric 41335490626Seric errno = 0; 4145dfc646bSeric return (i); 41525a99e2eSeric } 4165dfc646bSeric /* 41732d19d43Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 41832d19d43Seric ** 41932d19d43Seric ** This MUST be a macro, since after a vfork we are running 42032d19d43Seric ** two processes on the same stack!!! 42132d19d43Seric ** 42232d19d43Seric ** Parameters: 42332d19d43Seric ** none. 42432d19d43Seric ** 42532d19d43Seric ** Returns: 42632d19d43Seric ** From a macro??? You've got to be kidding! 42732d19d43Seric ** 42832d19d43Seric ** Side Effects: 42932d19d43Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 43032d19d43Seric ** pid of child in parent, zero in child. 43132d19d43Seric ** -1 on unrecoverable error. 43232d19d43Seric ** 43332d19d43Seric ** Notes: 43432d19d43Seric ** I'm awfully sorry this looks so awful. That's 43532d19d43Seric ** vfork for you..... 43632d19d43Seric */ 43732d19d43Seric 43832d19d43Seric # define NFORKTRIES 5 43932d19d43Seric # ifdef VFORK 44032d19d43Seric # define XFORK vfork 44132d19d43Seric # else VFORK 44232d19d43Seric # define XFORK fork 44332d19d43Seric # endif VFORK 44432d19d43Seric 44532d19d43Seric # define DOFORK(fORKfN) \ 44632d19d43Seric {\ 44732d19d43Seric register int i;\ 44832d19d43Seric \ 44932d19d43Seric for (i = NFORKTRIES; i-- > 0; )\ 45032d19d43Seric {\ 45132d19d43Seric pid = fORKfN();\ 45232d19d43Seric if (pid >= 0)\ 45332d19d43Seric break;\ 45432d19d43Seric sleep((unsigned) NFORKTRIES - i);\ 45532d19d43Seric }\ 45632d19d43Seric } 45732d19d43Seric /* 4582ed72599Seric ** DOFORK -- simple fork interface to DOFORK. 4592ed72599Seric ** 4602ed72599Seric ** Parameters: 4612ed72599Seric ** none. 4622ed72599Seric ** 4632ed72599Seric ** Returns: 4642ed72599Seric ** pid of child in parent. 4652ed72599Seric ** zero in child. 4662ed72599Seric ** -1 on error. 4672ed72599Seric ** 4682ed72599Seric ** Side Effects: 4692ed72599Seric ** returns twice, once in parent and once in child. 4702ed72599Seric */ 4712ed72599Seric 4722ed72599Seric dofork() 4732ed72599Seric { 4742ed72599Seric register int pid; 4752ed72599Seric 4762ed72599Seric DOFORK(fork); 4772ed72599Seric return (pid); 4782ed72599Seric } 4792ed72599Seric /* 4805dfc646bSeric ** SENDOFF -- send off call to mailer & collect response. 4815dfc646bSeric ** 4825dfc646bSeric ** Parameters: 4835dfc646bSeric ** m -- mailer descriptor. 4845dfc646bSeric ** pvp -- parameter vector to send to it. 4855dfc646bSeric ** editfcn -- function to pipe it through. 4866259796dSeric ** ctladdr -- an address pointer controlling the 4876259796dSeric ** user/groupid etc. of the mailer. 4885dfc646bSeric ** 4895dfc646bSeric ** Returns: 4905dfc646bSeric ** exit status of mailer. 4915dfc646bSeric ** 4925dfc646bSeric ** Side Effects: 4935dfc646bSeric ** none. 4945dfc646bSeric */ 4955dfc646bSeric 4966259796dSeric sendoff(m, pvp, editfcn, ctladdr) 4975dfc646bSeric struct mailer *m; 4985dfc646bSeric char **pvp; 4995dfc646bSeric int (*editfcn)(); 5006259796dSeric ADDRESS *ctladdr; 5015dfc646bSeric { 502c579ef51Seric auto FILE *mfile; 503c579ef51Seric auto FILE *rfile; 5045dfc646bSeric register int i; 505c579ef51Seric extern putmessage(); 506c579ef51Seric int pid; 507c579ef51Seric 508c579ef51Seric /* 509c579ef51Seric ** Create connection to mailer. 510c579ef51Seric */ 511c579ef51Seric 512c579ef51Seric pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 513c579ef51Seric if (pid < 0) 514c579ef51Seric return (-1); 515c579ef51Seric 516c579ef51Seric /* 517c579ef51Seric ** Format and send message. 518c579ef51Seric */ 519c579ef51Seric 520c579ef51Seric (void) signal(SIGPIPE, SIG_IGN); 521c579ef51Seric if (editfcn == NULL) 522c579ef51Seric editfcn = putmessage; 523c579ef51Seric 524c579ef51Seric (*editfcn)(mfile, m, FALSE); 525c579ef51Seric (void) fclose(mfile); 526c579ef51Seric 527c579ef51Seric i = endmailer(pid, pvp[0]); 528c579ef51Seric giveresponse(i, TRUE, m); 529bc6e2962Seric 530bc6e2962Seric /* arrange a return receipt if requested */ 531bc6e2962Seric if (RetReceipt && bitset(M_LOCAL, m->m_flags) && i == EX_OK) 532bc6e2962Seric { 533bc6e2962Seric SendReceipt = TRUE; 534bc6e2962Seric fprintf(Xscript, "%s... successfully delivered\n", To); 535bc6e2962Seric /* do we want to send back more info? */ 536bc6e2962Seric } 537bc6e2962Seric 538c579ef51Seric return (i); 539c579ef51Seric } 540c579ef51Seric /* 541c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 542c579ef51Seric ** 543c579ef51Seric ** We should never get fatal errors (e.g., segmentation 544c579ef51Seric ** violation), so we report those specially. For other 545c579ef51Seric ** errors, we choose a status message (into statmsg), 546c579ef51Seric ** and if it represents an error, we print it. 547c579ef51Seric ** 548c579ef51Seric ** Parameters: 549c579ef51Seric ** pid -- pid of mailer. 550c579ef51Seric ** name -- name of mailer (for error messages). 551c579ef51Seric ** 552c579ef51Seric ** Returns: 553c579ef51Seric ** exit code of mailer. 554c579ef51Seric ** 555c579ef51Seric ** Side Effects: 556c579ef51Seric ** none. 557c579ef51Seric */ 558c579ef51Seric 559c579ef51Seric endmailer(pid, name) 560c579ef51Seric int pid; 561c579ef51Seric char *name; 562c579ef51Seric { 563c579ef51Seric register int i; 564c579ef51Seric auto int st; 565c579ef51Seric 56633db8731Seric /* in the IPC case there is nothing to wait for */ 56733db8731Seric if (pid == 0) 56833db8731Seric return (EX_OK); 56933db8731Seric 57033db8731Seric /* wait for the mailer process to die and collect status */ 571c579ef51Seric while ((i = wait(&st)) > 0 && i != pid) 572c579ef51Seric continue; 573c579ef51Seric if (i < 0) 574c579ef51Seric { 575c579ef51Seric syserr("wait"); 576c579ef51Seric return (-1); 577c579ef51Seric } 57833db8731Seric 57933db8731Seric /* see if it died a horrid death */ 580c579ef51Seric if ((st & 0377) != 0) 581c579ef51Seric { 582c579ef51Seric syserr("%s: stat %o", name, st); 583c579ef51Seric ExitStat = EX_UNAVAILABLE; 584c579ef51Seric return (-1); 585c579ef51Seric } 58633db8731Seric 58733db8731Seric /* normal death -- return status */ 588c579ef51Seric i = (st >> 8) & 0377; 589c579ef51Seric return (i); 590c579ef51Seric } 591c579ef51Seric /* 592c579ef51Seric ** OPENMAILER -- open connection to mailer. 593c579ef51Seric ** 594c579ef51Seric ** Parameters: 595c579ef51Seric ** m -- mailer descriptor. 596c579ef51Seric ** pvp -- parameter vector to pass to mailer. 597c579ef51Seric ** ctladdr -- controlling address for user. 598c579ef51Seric ** clever -- create a full duplex connection. 599c579ef51Seric ** pmfile -- pointer to mfile (to mailer) connection. 600c579ef51Seric ** prfile -- pointer to rfile (from mailer) connection. 601c579ef51Seric ** 602c579ef51Seric ** Returns: 60333db8731Seric ** pid of mailer ( > 0 ). 604c579ef51Seric ** -1 on error. 60533db8731Seric ** zero on an IPC connection. 606c579ef51Seric ** 607c579ef51Seric ** Side Effects: 608c579ef51Seric ** creates a mailer in a subprocess. 609c579ef51Seric */ 610c579ef51Seric 611c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 612c579ef51Seric struct mailer *m; 613c579ef51Seric char **pvp; 614c579ef51Seric ADDRESS *ctladdr; 615c579ef51Seric bool clever; 616c579ef51Seric FILE **pmfile; 617c579ef51Seric FILE **prfile; 618c579ef51Seric { 6195dfc646bSeric int pid; 620f8952a83Seric int mpvect[2]; 621c579ef51Seric int rpvect[2]; 6225dfc646bSeric FILE *mfile; 623c579ef51Seric FILE *rfile; 6245dfc646bSeric extern FILE *fdopen(); 6255dfc646bSeric 6265dfc646bSeric # ifdef DEBUG 6275dfc646bSeric if (Debug) 6285dfc646bSeric { 629c579ef51Seric printf("openmailer:\n"); 6305dfc646bSeric printav(pvp); 6315dfc646bSeric } 6325dfc646bSeric # endif DEBUG 63335490626Seric errno = 0; 6345dfc646bSeric 63533db8731Seric # ifdef DAEMON 63633db8731Seric /* 63733db8731Seric ** Deal with the special case of mail handled through an IPC 63833db8731Seric ** connection. 63933db8731Seric ** In this case we don't actually fork. We must be 64033db8731Seric ** running SMTP for this to work. We will return a 64133db8731Seric ** zero pid to indicate that we are running IPC. 64233db8731Seric */ 64333db8731Seric 64433db8731Seric if (strcmp(m->m_mailer, "[IPC]") == 0) 64533db8731Seric { 64633db8731Seric register int i; 64733db8731Seric 64833db8731Seric if (!clever) 64933db8731Seric syserr("non-clever IPC"); 65093b6e3cfSeric if (pvp[2] != NULL) 65193b6e3cfSeric i = atoi(pvp[2]); 65293b6e3cfSeric else 65393b6e3cfSeric i = 0; 65493b6e3cfSeric i = makeconnection(pvp[1], i, pmfile, prfile); 65533db8731Seric if (i != EX_OK) 656ed854c7bSeric { 657ed854c7bSeric ExitStat = i; 65833db8731Seric return (-1); 659ed854c7bSeric } 66033db8731Seric else 66133db8731Seric return (0); 66233db8731Seric } 66333db8731Seric # endif DAEMON 66433db8731Seric 6656328bdf7Seric /* create a pipe to shove the mail through */ 666f8952a83Seric if (pipe(mpvect) < 0) 66725a99e2eSeric { 668c579ef51Seric syserr("pipe (to mailer)"); 66925a99e2eSeric return (-1); 67025a99e2eSeric } 671c579ef51Seric 6722c7e1b8dSeric # ifdef SMTP 673c579ef51Seric /* if this mailer speaks smtp, create a return pipe */ 674c579ef51Seric if (clever && pipe(rpvect) < 0) 675c579ef51Seric { 676c579ef51Seric syserr("pipe (from mailer)"); 677c579ef51Seric (void) close(mpvect[0]); 678c579ef51Seric (void) close(mpvect[1]); 679c579ef51Seric return (-1); 680c579ef51Seric } 6812c7e1b8dSeric # endif SMTP 682c579ef51Seric 68333db8731Seric /* 68433db8731Seric ** Actually fork the mailer process. 68533db8731Seric ** DOFORK is clever about retrying. 68633db8731Seric */ 68733db8731Seric 68832d19d43Seric DOFORK(XFORK); 689f129ec7dSeric /* pid is set by DOFORK */ 69025a99e2eSeric if (pid < 0) 69125a99e2eSeric { 69233db8731Seric /* failure */ 69325a99e2eSeric syserr("Cannot fork"); 694f8952a83Seric (void) close(mpvect[0]); 695f8952a83Seric (void) close(mpvect[1]); 696c579ef51Seric if (clever) 697c579ef51Seric { 698c579ef51Seric (void) close(rpvect[0]); 699c579ef51Seric (void) close(rpvect[1]); 700c579ef51Seric } 70125a99e2eSeric return (-1); 70225a99e2eSeric } 70325a99e2eSeric else if (pid == 0) 70425a99e2eSeric { 70525a99e2eSeric /* child -- set up input & exec mailer */ 70603ab8e55Seric /* make diagnostic output be standard output */ 7078f0e7860Seric (void) signal(SIGINT, SIG_IGN); 7088f0e7860Seric (void) signal(SIGHUP, SIG_IGN); 7090984da9fSeric (void) signal(SIGTERM, SIG_DFL); 710f8952a83Seric 711f8952a83Seric /* arrange to filter standard & diag output of command */ 712c579ef51Seric if (clever) 713c579ef51Seric { 714c579ef51Seric (void) close(rpvect[0]); 715c579ef51Seric (void) close(1); 716c579ef51Seric (void) dup(rpvect[1]); 717c579ef51Seric (void) close(rpvect[1]); 718c579ef51Seric } 719c579ef51Seric else if (OutChannel != stdout) 720f8952a83Seric { 721f8952a83Seric (void) close(1); 722f8952a83Seric (void) dup(fileno(OutChannel)); 723f8952a83Seric } 724db8841e9Seric (void) close(2); 725db8841e9Seric (void) dup(1); 726f8952a83Seric 727f8952a83Seric /* arrange to get standard input */ 728f8952a83Seric (void) close(mpvect[1]); 729db8841e9Seric (void) close(0); 730f8952a83Seric if (dup(mpvect[0]) < 0) 73125a99e2eSeric { 73225a99e2eSeric syserr("Cannot dup to zero!"); 733a590b978Seric _exit(EX_OSERR); 73425a99e2eSeric } 735f8952a83Seric (void) close(mpvect[0]); 7362a6e0786Seric if (!bitset(M_RESTR, m->m_flags)) 7370984da9fSeric { 738e36b99e2Seric if (ctladdr->q_uid == 0) 739e36b99e2Seric { 740e36b99e2Seric (void) setgid(DefGid); 741e36b99e2Seric (void) setuid(DefUid); 742e36b99e2Seric } 743e36b99e2Seric else 74469f29479Seric { 745e36b99e2Seric (void) setgid(ctladdr->q_gid); 746e36b99e2Seric (void) setuid(ctladdr->q_uid); 74769f29479Seric } 7480984da9fSeric } 749e374fd72Seric # ifndef VFORK 750e374fd72Seric /* 751e374fd72Seric ** We have to be careful with vfork - we can't mung up the 752e374fd72Seric ** memory but we don't want the mailer to inherit any extra 753e374fd72Seric ** open files. Chances are the mailer won't 754e374fd72Seric ** care about an extra file, but then again you never know. 755e374fd72Seric ** Actually, we would like to close(fileno(pwf)), but it's 756e374fd72Seric ** declared static so we can't. But if we fclose(pwf), which 757e374fd72Seric ** is what endpwent does, it closes it in the parent too and 758e374fd72Seric ** the next getpwnam will be slower. If you have a weird 759e374fd72Seric ** mailer that chokes on the extra file you should do the 760e374fd72Seric ** endpwent(). 761e374fd72Seric ** 762e374fd72Seric ** Similar comments apply to log. However, openlog is 763e374fd72Seric ** clever enough to set the FIOCLEX mode on the file, 764e374fd72Seric ** so it will be closed automatically on the exec. 765e374fd72Seric */ 766e374fd72Seric 767e374fd72Seric endpwent(); 76825a99e2eSeric # ifdef LOG 769f9fe028fSeric closelog(); 77025a99e2eSeric # endif LOG 771e374fd72Seric # endif VFORK 77233db8731Seric 77333db8731Seric /* try to execute the mailer */ 77425a99e2eSeric execv(m->m_mailer, pvp); 77533db8731Seric 77625a99e2eSeric /* syserr fails because log is closed */ 77725a99e2eSeric /* syserr("Cannot exec %s", m->m_mailer); */ 77832d19d43Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 779db8841e9Seric (void) fflush(stdout); 780a590b978Seric _exit(EX_UNAVAILABLE); 78125a99e2eSeric } 78225a99e2eSeric 783f8952a83Seric /* 784c579ef51Seric ** Set up return value. 785f8952a83Seric */ 786f8952a83Seric 787f8952a83Seric (void) close(mpvect[0]); 788f8952a83Seric mfile = fdopen(mpvect[1], "w"); 789c579ef51Seric if (clever) 79025a99e2eSeric { 791c579ef51Seric (void) close(rpvect[1]); 792c579ef51Seric rfile = fdopen(rpvect[0], "r"); 79325a99e2eSeric } 794c579ef51Seric 795c579ef51Seric *pmfile = mfile; 796c579ef51Seric *prfile = rfile; 797c579ef51Seric 798c579ef51Seric return (pid); 79925a99e2eSeric } 80025a99e2eSeric /* 80125a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 80225a99e2eSeric ** 80325a99e2eSeric ** Parameters: 80425a99e2eSeric ** stat -- the status code from the mailer (high byte 80525a99e2eSeric ** only; core dumps must have been taken care of 80625a99e2eSeric ** already). 80725a99e2eSeric ** force -- if set, force an error message output, even 80825a99e2eSeric ** if the mailer seems to like to print its own 80925a99e2eSeric ** messages. 81025a99e2eSeric ** m -- the mailer descriptor for this mailer. 81125a99e2eSeric ** 81225a99e2eSeric ** Returns: 813db8841e9Seric ** none. 81425a99e2eSeric ** 81525a99e2eSeric ** Side Effects: 816c1f9df2cSeric ** Errors may be incremented. 81725a99e2eSeric ** ExitStat may be set. 81825a99e2eSeric */ 81925a99e2eSeric 82025a99e2eSeric giveresponse(stat, force, m) 82125a99e2eSeric int stat; 82225a99e2eSeric int force; 82325a99e2eSeric register struct mailer *m; 82425a99e2eSeric { 82525a99e2eSeric register char *statmsg; 82625a99e2eSeric extern char *SysExMsg[]; 82725a99e2eSeric register int i; 82825a99e2eSeric extern int N_SysEx; 82929dd97a3Seric char buf[30]; 83025a99e2eSeric 83113bbc08cSeric /* 83213bbc08cSeric ** Compute status message from code. 83313bbc08cSeric */ 83413bbc08cSeric 83525a99e2eSeric i = stat - EX__BASE; 83625a99e2eSeric if (i < 0 || i > N_SysEx) 83725a99e2eSeric statmsg = NULL; 83825a99e2eSeric else 83925a99e2eSeric statmsg = SysExMsg[i]; 84025a99e2eSeric if (stat == 0) 841c38ba59cSeric { 8426cbfa739Seric if (bitset(M_LOCAL, m->m_flags)) 8433efaed6eSeric statmsg = "delivered"; 8443efaed6eSeric else 8453efaed6eSeric statmsg = "queued"; 846c38ba59cSeric if (Verbose) 847544026c5Seric message(Arpa_Info, statmsg); 848c38ba59cSeric } 8492c7e1b8dSeric # ifdef QUEUE 850c77d1c25Seric else if (stat == EX_TEMPFAIL) 851c77d1c25Seric { 852c77d1c25Seric if (Verbose) 853c77d1c25Seric message(Arpa_Info, "transmission deferred"); 854c77d1c25Seric } 8552c7e1b8dSeric # endif QUEUE 85625a99e2eSeric else 85725a99e2eSeric { 858c1f9df2cSeric Errors++; 859ed854c7bSeric FatalErrors = TRUE; 86025a99e2eSeric if (statmsg == NULL && m->m_badstat != 0) 86125a99e2eSeric { 86225a99e2eSeric stat = m->m_badstat; 86325a99e2eSeric i = stat - EX__BASE; 86425a99e2eSeric # ifdef DEBUG 86525a99e2eSeric if (i < 0 || i >= N_SysEx) 86625a99e2eSeric syserr("Bad m_badstat %d", stat); 86725a99e2eSeric else 86825a99e2eSeric # endif DEBUG 86925a99e2eSeric statmsg = SysExMsg[i]; 87025a99e2eSeric } 87125a99e2eSeric if (statmsg == NULL) 87225a99e2eSeric usrerr("unknown mailer response %d", stat); 873c38ba59cSeric else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 87425a99e2eSeric usrerr("%s", statmsg); 87525a99e2eSeric } 87625a99e2eSeric 87725a99e2eSeric /* 87825a99e2eSeric ** Final cleanup. 87925a99e2eSeric ** Log a record of the transaction. Compute the new 88025a99e2eSeric ** ExitStat -- if we already had an error, stick with 88125a99e2eSeric ** that. 88225a99e2eSeric */ 88325a99e2eSeric 88425a99e2eSeric if (statmsg == NULL) 88529dd97a3Seric { 886db8841e9Seric (void) sprintf(buf, "error %d", stat); 88729dd97a3Seric statmsg = buf; 88829dd97a3Seric } 88929dd97a3Seric 89029dd97a3Seric # ifdef LOG 891e374fd72Seric syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg); 89225a99e2eSeric # endif LOG 8932c7e1b8dSeric # ifdef QUEUE 894c77d1c25Seric if (stat != EX_TEMPFAIL) 8952c7e1b8dSeric # endif QUEUE 896243921eeSeric setstat(stat); 89725a99e2eSeric } 89825a99e2eSeric /* 8996328bdf7Seric ** PUTMESSAGE -- output a message to the final mailer. 90025a99e2eSeric ** 9016328bdf7Seric ** This routine takes care of recreating the header from the 9026328bdf7Seric ** in-core copy, etc. 90325a99e2eSeric ** 90425a99e2eSeric ** Parameters: 9056328bdf7Seric ** fp -- file to output onto. 9066328bdf7Seric ** m -- a mailer descriptor. 907c579ef51Seric ** xdot -- if set, hide lines beginning with dot. 90825a99e2eSeric ** 90925a99e2eSeric ** Returns: 9106328bdf7Seric ** none. 91125a99e2eSeric ** 91225a99e2eSeric ** Side Effects: 9136328bdf7Seric ** The message is written onto fp. 91425a99e2eSeric */ 91525a99e2eSeric 916c579ef51Seric putmessage(fp, m, xdot) 9176328bdf7Seric FILE *fp; 9186328bdf7Seric struct mailer *m; 919c579ef51Seric bool xdot; 92025a99e2eSeric { 9216328bdf7Seric char buf[BUFSIZ]; 92225a99e2eSeric 92313bbc08cSeric /* 92413bbc08cSeric ** Output "From" line unless supressed 925a36e30c9Seric ** 926a36e30c9Seric ** >>>>>>>>>> One of the ugliest hacks seen by human eyes is 927a36e30c9Seric ** >>>>>>>>>> contained herein: UUCP wants those stupid 92874b6e67bSeric ** >> NOTE >> "remote from <host>" lines. Why oh why does a 92974b6e67bSeric ** >>>>>>>>>> well-meaning programmer such as myself have to 930a36e30c9Seric ** >>>>>>>>>> deal with this kind of antique garbage???? 93113bbc08cSeric */ 93213bbc08cSeric 93340e4ab56Seric if (!bitset(M_NHDR, m->m_flags)) 9341412cc7cSeric { 9352c7e1b8dSeric # ifdef UGLYUUCP 936a49f24c0Seric if (bitset(M_UGLYUUCP, m->m_flags)) 93774b6e67bSeric { 93874b6e67bSeric extern char *macvalue(); 93974b6e67bSeric char *sys = macvalue('g'); 94074b6e67bSeric char *bang = index(sys, '!'); 94174b6e67bSeric 94274b6e67bSeric if (bang == NULL) 94374b6e67bSeric syserr("No ! in UUCP! (%s)", sys); 94474b6e67bSeric else 94574b6e67bSeric *bang = '\0'; 94674b6e67bSeric (void) expand("From $f $d remote from $g", buf, 947a36e30c9Seric &buf[sizeof buf - 1]); 94874b6e67bSeric *bang = '!'; 94974b6e67bSeric } 950a36e30c9Seric else 9512c7e1b8dSeric # endif UGLYUUCP 9521412cc7cSeric (void) expand("$l", buf, &buf[sizeof buf - 1]); 9531412cc7cSeric fprintf(fp, "%s\n", buf); 9541412cc7cSeric } 95540e4ab56Seric 95613bbc08cSeric /* 95713bbc08cSeric ** Output all header lines 95813bbc08cSeric */ 95913bbc08cSeric 960bc6e2962Seric putheader(fp, m); 961bc6e2962Seric 962bc6e2962Seric /* 963bc6e2962Seric ** Output the body of the message 964bc6e2962Seric */ 965bc6e2962Seric 966bc6e2962Seric if (TempFile != NULL) 967bc6e2962Seric { 968bc6e2962Seric rewind(TempFile); 969bc6e2962Seric while (!ferror(fp) && fgets(buf, sizeof buf, TempFile) != NULL) 970bc6e2962Seric fprintf(fp, "%s%s", xdot && buf[0] == '.' ? "." : "", buf); 971bc6e2962Seric 972bc6e2962Seric if (ferror(TempFile)) 973bc6e2962Seric { 974bc6e2962Seric syserr("putmessage: read error"); 975ed854c7bSeric ExitStat = EX_IOERR; 976bc6e2962Seric } 977bc6e2962Seric } 978bc6e2962Seric 979bc6e2962Seric (void) fflush(fp); 980bc6e2962Seric if (ferror(fp) && errno != EPIPE) 981bc6e2962Seric { 982bc6e2962Seric syserr("putmessage: write error"); 983ed854c7bSeric ExitStat = EX_IOERR; 984bc6e2962Seric } 985bc6e2962Seric errno = 0; 986bc6e2962Seric } 987bc6e2962Seric /* 988bc6e2962Seric ** PUTHEADER -- put the header part of a message 989bc6e2962Seric ** 990bc6e2962Seric ** Parameters: 991bc6e2962Seric ** fp -- file to put it on. 992bc6e2962Seric ** m -- mailer to use. 993bc6e2962Seric ** 994bc6e2962Seric ** Returns: 995bc6e2962Seric ** none. 996bc6e2962Seric ** 997bc6e2962Seric ** Side Effects: 998bc6e2962Seric ** none. 999bc6e2962Seric */ 1000bc6e2962Seric 1001bc6e2962Seric putheader(fp, m) 1002bc6e2962Seric register FILE *fp; 1003bc6e2962Seric register struct mailer *m; 1004bc6e2962Seric { 1005bc6e2962Seric char buf[BUFSIZ]; 1006bc6e2962Seric register HDR *h; 1007bc6e2962Seric extern char *arpadate(); 1008bc6e2962Seric bool anyheader = FALSE; 1009bc6e2962Seric extern char *capitalize(); 1010bc6e2962Seric extern char *hvalue(); 1011bc6e2962Seric extern bool samefrom(); 1012bc6e2962Seric char *of_line; 1013bc6e2962Seric 1014894de7daSeric of_line = hvalue("original-from"); 10156328bdf7Seric for (h = Header; h != NULL; h = h->h_link) 10166328bdf7Seric { 101713bbc08cSeric register char *p; 101887bd9a02Seric char *origfrom = OrigFrom; 1019894de7daSeric bool nooutput; 102013bbc08cSeric 1021894de7daSeric nooutput = FALSE; 10229e9163a0Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 1023894de7daSeric nooutput = TRUE; 1024894de7daSeric 1025894de7daSeric /* use From: line from message if generated is the same */ 102687bd9a02Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL && 1027894de7daSeric strcmp(m->m_from, "$f") == 0 && of_line == NULL) 102887bd9a02Seric { 102987bd9a02Seric p = origfrom; 103087bd9a02Seric origfrom = NULL; 103187bd9a02Seric } 103287bd9a02Seric else if (bitset(H_DEFAULT, h->h_flags)) 10334ae18d0eSeric { 1034db8841e9Seric (void) expand(h->h_value, buf, &buf[sizeof buf]); 10354ae18d0eSeric p = buf; 10364ae18d0eSeric } 10378c2bf25bSeric else if (bitset(H_ADDR, h->h_flags)) 10388c2bf25bSeric { 10398c2bf25bSeric register int opos; 10408c2bf25bSeric bool firstone = TRUE; 10418c2bf25bSeric 10428c2bf25bSeric /* 10438c2bf25bSeric ** Output the address list translated by the 10448c2bf25bSeric ** mailer and with commas. 10458c2bf25bSeric */ 10468c2bf25bSeric 10478c2bf25bSeric p = h->h_value; 10488c2bf25bSeric if (p == NULL || *p == '\0' || nooutput) 10498c2bf25bSeric continue; 10508c2bf25bSeric fprintf(fp, "%s: ", capitalize(h->h_field)); 10518c2bf25bSeric opos = strlen(h->h_field) + 2; 10528c2bf25bSeric while (*p != '\0') 10538c2bf25bSeric { 10548c2bf25bSeric register char *name = p; 10558c2bf25bSeric extern char *remotename(); 10568c2bf25bSeric char savechar; 10578c2bf25bSeric 10588c2bf25bSeric /* find the end of the name */ 1059bc28c57aSeric while (*p != '\0' && *p != ',') 1060bc28c57aSeric { 1061bc28c57aSeric extern bool isatword(); 1062bc28c57aSeric char *oldp; 1063bc28c57aSeric 1064bc28c57aSeric if (!OldStyle || !isspace(*p)) 1065bc28c57aSeric { 10668c2bf25bSeric p++; 1067bc28c57aSeric continue; 1068bc28c57aSeric } 1069bc28c57aSeric oldp = p; 1070bc28c57aSeric while (*p != '\0' && isspace(*p)) 1071bc28c57aSeric p++; 1072bc28c57aSeric if (*p != '@' && !isatword(p)) 1073bc28c57aSeric { 1074bc28c57aSeric p = oldp; 1075bc28c57aSeric break; 1076bc28c57aSeric } 1077bc28c57aSeric p += *p == '@' ? 1 : 2; 1078bc28c57aSeric while (*p != '\0' && isspace(*p)) 1079bc28c57aSeric p++; 1080bc28c57aSeric } 10818c2bf25bSeric savechar = *p; 10828c2bf25bSeric *p = '\0'; 10838c2bf25bSeric 10848c2bf25bSeric /* translate the name to be relative */ 1085f60462a7Seric name = remotename(name, m, FALSE); 10868c2bf25bSeric if (*name == '\0') 10878c2bf25bSeric continue; 10888c2bf25bSeric 10898c2bf25bSeric /* output the name with nice formatting */ 10908c2bf25bSeric opos += strlen(name); 10918c2bf25bSeric if (!firstone) 10928c2bf25bSeric opos += 2; 10938c2bf25bSeric if (opos > 78 && !firstone) 10948c2bf25bSeric { 10958c2bf25bSeric fprintf(fp, ",\n "); 1096bc213ac6Seric opos = 8 + strlen(name); 10978c2bf25bSeric } 10988c2bf25bSeric else if (!firstone) 10998c2bf25bSeric fprintf(fp, ", "); 11008c2bf25bSeric fprintf(fp, "%s", name); 11018c2bf25bSeric firstone = FALSE; 11028c2bf25bSeric 11038c2bf25bSeric /* clean up the source string */ 11048c2bf25bSeric *p = savechar; 11058c2bf25bSeric while (*p != '\0' && (isspace(*p) || *p == ',')) 11068c2bf25bSeric p++; 11078c2bf25bSeric } 11088c2bf25bSeric fprintf(fp, "\n"); 11098c2bf25bSeric nooutput = TRUE; 11108c2bf25bSeric } 11114ae18d0eSeric else 11124ae18d0eSeric p = h->h_value; 1113894de7daSeric if (p == NULL || *p == '\0') 11149e9163a0Seric continue; 11155a0dcb5fSeric 11165a0dcb5fSeric /* hack, hack -- output Original-From field if different */ 1117894de7daSeric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL) 1118894de7daSeric { 1119894de7daSeric /* output new Original-From line if needed */ 1120894de7daSeric if (of_line == NULL && !samefrom(p, origfrom)) 11215a0dcb5fSeric { 112287bd9a02Seric fprintf(fp, "Original-From: %s\n", origfrom); 112387bd9a02Seric anyheader = TRUE; 11245a0dcb5fSeric } 1125894de7daSeric if (of_line != NULL && !nooutput && samefrom(p, of_line)) 1126894de7daSeric { 1127894de7daSeric /* delete Original-From: line if redundant */ 1128894de7daSeric p = of_line; 1129894de7daSeric of_line = NULL; 1130894de7daSeric } 1131894de7daSeric } 1132894de7daSeric else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL) 1133894de7daSeric nooutput = TRUE; 1134894de7daSeric 1135894de7daSeric /* finally, output the header line */ 1136894de7daSeric if (!nooutput) 1137894de7daSeric { 1138894de7daSeric fprintf(fp, "%s: %s\n", capitalize(h->h_field), p); 1139894de7daSeric h->h_flags |= H_USED; 1140894de7daSeric anyheader = TRUE; 1141894de7daSeric } 11426328bdf7Seric } 11436328bdf7Seric if (anyheader) 11446328bdf7Seric fprintf(fp, "\n"); 114525a99e2eSeric } 114625a99e2eSeric /* 1147bc28c57aSeric ** ISATWORD -- tell if the word we are pointing to is "at". 1148bc28c57aSeric ** 1149bc28c57aSeric ** Parameters: 1150bc28c57aSeric ** p -- word to check. 1151bc28c57aSeric ** 1152bc28c57aSeric ** Returns: 1153bc28c57aSeric ** TRUE -- if p is the word at. 1154bc28c57aSeric ** FALSE -- otherwise. 1155bc28c57aSeric ** 1156bc28c57aSeric ** Side Effects: 1157bc28c57aSeric ** none. 1158bc28c57aSeric */ 1159bc28c57aSeric 1160bc28c57aSeric bool 1161bc28c57aSeric isatword(p) 1162bc28c57aSeric register char *p; 1163bc28c57aSeric { 1164bc28c57aSeric extern char lower(); 1165bc28c57aSeric 1166bc28c57aSeric if (lower(p[0]) == 'a' && lower(p[1]) == 't' && 1167bc28c57aSeric p[2] != '\0' && isspace(p[2])) 1168bc28c57aSeric return (TRUE); 1169bc28c57aSeric return (FALSE); 1170bc28c57aSeric } 1171bc28c57aSeric /* 11728c2bf25bSeric ** REMOTENAME -- return the name relative to the current mailer 11738c2bf25bSeric ** 11748c2bf25bSeric ** Parameters: 11758c2bf25bSeric ** name -- the name to translate. 1176f60462a7Seric ** force -- if set, forces rewriting even if the mailer 1177f60462a7Seric ** does not request it. Used for rewriting 1178f60462a7Seric ** sender addresses. 11798c2bf25bSeric ** 11808c2bf25bSeric ** Returns: 11818c2bf25bSeric ** the text string representing this address relative to 11828c2bf25bSeric ** the receiving mailer. 11838c2bf25bSeric ** 11848c2bf25bSeric ** Side Effects: 11858c2bf25bSeric ** none. 11868c2bf25bSeric ** 11878c2bf25bSeric ** Warnings: 11888c2bf25bSeric ** The text string returned is tucked away locally; 11898c2bf25bSeric ** copy it if you intend to save it. 11908c2bf25bSeric */ 11918c2bf25bSeric 11928c2bf25bSeric char * 1193f60462a7Seric remotename(name, m, force) 11948c2bf25bSeric char *name; 1195bc213ac6Seric struct mailer *m; 1196f60462a7Seric bool force; 11978c2bf25bSeric { 11988c2bf25bSeric static char buf[MAXNAME]; 11998c2bf25bSeric char lbuf[MAXNAME]; 12008c2bf25bSeric extern char *macvalue(); 12018c2bf25bSeric char *oldf = macvalue('f'); 1202bc213ac6Seric char *oldx = macvalue('x'); 1203bc213ac6Seric char *oldg = macvalue('g'); 12048c2bf25bSeric extern char **prescan(); 12058c2bf25bSeric register char **pvp; 1206bc213ac6Seric extern char *getxpart(); 12078c2bf25bSeric 12088c2bf25bSeric /* 1209f60462a7Seric ** See if this mailer wants the name to be rewritten. There are 1210f60462a7Seric ** many problems here, owing to the standards for doing replies. 1211f60462a7Seric ** In general, these names should only be rewritten if we are 1212f60462a7Seric ** sending to another host that runs sendmail. 1213f60462a7Seric */ 1214f60462a7Seric 1215f60462a7Seric if (!bitset(M_RELRCPT, m->m_flags) && !force) 1216f60462a7Seric return; 1217f60462a7Seric 1218f60462a7Seric /* 12198c2bf25bSeric ** Do general rewriting of name. 12208c2bf25bSeric ** This will also take care of doing global name translation. 12218c2bf25bSeric */ 12228c2bf25bSeric 1223bc213ac6Seric define('x', getxpart(name)); 12248c2bf25bSeric pvp = prescan(name, '\0'); 12258c2bf25bSeric for (;;) 12268c2bf25bSeric { 12278c2bf25bSeric rewrite(pvp, 1); 12288c2bf25bSeric rewrite(pvp, 3); 12298c2bf25bSeric if (**pvp == CANONNET) 12308c2bf25bSeric { 12318c2bf25bSeric auto ADDRESS a; 12328c2bf25bSeric register char *p; 12338c2bf25bSeric extern char *hostalias(); 12348c2bf25bSeric 12358c2bf25bSeric /* oops... resolved to something */ 12368c2bf25bSeric if (buildaddr(pvp, &a) == NULL) 12378c2bf25bSeric return (name); 12388c2bf25bSeric p = hostalias(&a); 12398c2bf25bSeric if (p == NULL) 12408c2bf25bSeric return (name); 12418c2bf25bSeric pvp = prescan(p, '\0'); 12428c2bf25bSeric } 12438c2bf25bSeric else 12448c2bf25bSeric { 12458c2bf25bSeric cataddr(pvp, lbuf, sizeof lbuf); 12468c2bf25bSeric break; 12478c2bf25bSeric } 12488c2bf25bSeric } 12498c2bf25bSeric 12508c2bf25bSeric /* make the name relative to the receiving mailer */ 12518c2bf25bSeric define('f', lbuf); 1252bc213ac6Seric (void) expand(m->m_from, buf, &buf[sizeof buf - 1]); 12538c2bf25bSeric 12548c2bf25bSeric /* rewrite to get rid of garbage we added in the expand above */ 12558c2bf25bSeric pvp = prescan(buf, '\0'); 12568c2bf25bSeric rewrite(pvp, 2); 1257bc213ac6Seric cataddr(pvp, lbuf, sizeof lbuf); 1258bc213ac6Seric 1259bc213ac6Seric /* now add any comment info we had before back */ 1260bc213ac6Seric define('g', lbuf); 1261bc213ac6Seric (void) expand("$q", buf, &buf[sizeof buf - 1]); 12628c2bf25bSeric 12638c2bf25bSeric define('f', oldf); 1264bc213ac6Seric define('g', oldg); 1265bc213ac6Seric define('x', oldx); 12668c2bf25bSeric 12678c2bf25bSeric # ifdef DEBUG 12688c2bf25bSeric if (Debug > 0) 12698c2bf25bSeric printf("remotename(%s) => `%s'\n", name, buf); 12708c2bf25bSeric # endif DEBUG 12718c2bf25bSeric return (buf); 12728c2bf25bSeric } 12738c2bf25bSeric /* 127487bd9a02Seric ** SAMEFROM -- tell if two text addresses represent the same from address. 127587bd9a02Seric ** 127687bd9a02Seric ** Parameters: 127787bd9a02Seric ** ifrom -- internally generated form of from address. 127887bd9a02Seric ** efrom -- external form of from address. 127987bd9a02Seric ** 128087bd9a02Seric ** Returns: 128187bd9a02Seric ** TRUE -- if they convey the same info. 128287bd9a02Seric ** FALSE -- if any information has been lost. 128387bd9a02Seric ** 128487bd9a02Seric ** Side Effects: 128587bd9a02Seric ** none. 128687bd9a02Seric */ 128787bd9a02Seric 128887bd9a02Seric bool 128987bd9a02Seric samefrom(ifrom, efrom) 129087bd9a02Seric char *ifrom; 129187bd9a02Seric char *efrom; 129287bd9a02Seric { 1293894de7daSeric register char *p; 1294894de7daSeric char buf[MAXNAME + 4]; 1295894de7daSeric 1296894de7daSeric # ifdef DEBUG 1297894de7daSeric if (Debug > 7) 1298894de7daSeric printf("samefrom(%s,%s)-->", ifrom, efrom); 1299894de7daSeric # endif DEBUG 1300894de7daSeric if (strcmp(ifrom, efrom) == 0) 1301894de7daSeric goto success; 1302894de7daSeric p = index(ifrom, '@'); 1303894de7daSeric if (p == NULL) 1304894de7daSeric goto failure; 1305894de7daSeric *p = '\0'; 1306894de7daSeric strcpy(buf, ifrom); 1307894de7daSeric strcat(buf, " at "); 1308894de7daSeric *p++ = '@'; 1309894de7daSeric strcat(buf, p); 1310894de7daSeric if (strcmp(buf, efrom) == 0) 1311894de7daSeric goto success; 1312894de7daSeric 1313894de7daSeric failure: 1314894de7daSeric # ifdef DEBUG 1315894de7daSeric if (Debug > 7) 1316894de7daSeric printf("FALSE\n"); 1317894de7daSeric # endif DEBUG 1318894de7daSeric return (FALSE); 1319894de7daSeric 1320894de7daSeric success: 1321894de7daSeric # ifdef DEBUG 1322894de7daSeric if (Debug > 7) 1323894de7daSeric printf("TRUE\n"); 1324894de7daSeric # endif DEBUG 1325894de7daSeric return (TRUE); 132687bd9a02Seric } 132787bd9a02Seric /* 132825a99e2eSeric ** MAILFILE -- Send a message to a file. 132925a99e2eSeric ** 1330f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 1331f129ec7dSeric ** bits, sendmail will try to become the owner of that file 1332f129ec7dSeric ** rather than the real user. Obviously, this only works if 1333f129ec7dSeric ** sendmail runs as root. 1334f129ec7dSeric ** 133525a99e2eSeric ** Parameters: 133625a99e2eSeric ** filename -- the name of the file to send to. 13376259796dSeric ** ctladdr -- the controlling address header -- includes 13386259796dSeric ** the userid/groupid to be when sending. 133925a99e2eSeric ** 134025a99e2eSeric ** Returns: 134125a99e2eSeric ** The exit code associated with the operation. 134225a99e2eSeric ** 134325a99e2eSeric ** Side Effects: 134425a99e2eSeric ** none. 134525a99e2eSeric */ 134625a99e2eSeric 13476259796dSeric mailfile(filename, ctladdr) 134825a99e2eSeric char *filename; 13496259796dSeric ADDRESS *ctladdr; 135025a99e2eSeric { 135125a99e2eSeric register FILE *f; 135232d19d43Seric register int pid; 135325a99e2eSeric 135432d19d43Seric /* 135532d19d43Seric ** Fork so we can change permissions here. 135632d19d43Seric ** Note that we MUST use fork, not vfork, because of 135732d19d43Seric ** the complications of calling subroutines, etc. 135832d19d43Seric */ 135932d19d43Seric 136032d19d43Seric DOFORK(fork); 136132d19d43Seric 136232d19d43Seric if (pid < 0) 136332d19d43Seric return (EX_OSERR); 136432d19d43Seric else if (pid == 0) 136532d19d43Seric { 136632d19d43Seric /* child -- actually write to file */ 1367f129ec7dSeric struct stat stb; 1368f129ec7dSeric 13690984da9fSeric (void) signal(SIGINT, SIG_DFL); 13700984da9fSeric (void) signal(SIGHUP, SIG_DFL); 13710984da9fSeric (void) signal(SIGTERM, SIG_DFL); 1372f129ec7dSeric umask(OldUmask); 1373f129ec7dSeric if (stat(filename, &stb) < 0) 1374e6e1265fSeric stb.st_mode = 0666; 1375f129ec7dSeric if (bitset(0111, stb.st_mode)) 1376f129ec7dSeric exit(EX_CANTCREAT); 137703827b5fSeric if (ctladdr == NULL) 137803827b5fSeric ctladdr = &From; 1379f129ec7dSeric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 1380e36b99e2Seric { 1381e36b99e2Seric if (ctladdr->q_uid == 0) 1382e36b99e2Seric (void) setgid(DefGid); 1383e36b99e2Seric else 13846259796dSeric (void) setgid(ctladdr->q_gid); 1385e36b99e2Seric } 1386f129ec7dSeric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 1387e36b99e2Seric { 1388e36b99e2Seric if (ctladdr->q_uid == 0) 1389e36b99e2Seric (void) setuid(DefUid); 1390e36b99e2Seric else 13916259796dSeric (void) setuid(ctladdr->q_uid); 1392e36b99e2Seric } 1393*27628d59Seric f = dfopen(filename, "a"); 139425a99e2eSeric if (f == NULL) 139532d19d43Seric exit(EX_CANTCREAT); 139625a99e2eSeric 1397c579ef51Seric putmessage(f, Mailer[1], FALSE); 139825a99e2eSeric fputs("\n", f); 1399db8841e9Seric (void) fclose(f); 140032d19d43Seric (void) fflush(stdout); 1401e36b99e2Seric 1402*27628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 1403c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 140432d19d43Seric exit(EX_OK); 140513bbc08cSeric /*NOTREACHED*/ 140632d19d43Seric } 140732d19d43Seric else 140832d19d43Seric { 140932d19d43Seric /* parent -- wait for exit status */ 141032d19d43Seric register int i; 141132d19d43Seric auto int stat; 141232d19d43Seric 141332d19d43Seric while ((i = wait(&stat)) != pid) 141432d19d43Seric { 141532d19d43Seric if (i < 0) 141632d19d43Seric { 141732d19d43Seric stat = EX_OSERR << 8; 141832d19d43Seric break; 141932d19d43Seric } 142032d19d43Seric } 14210984da9fSeric if ((stat & 0377) != 0) 14220984da9fSeric stat = EX_UNAVAILABLE << 8; 142332d19d43Seric return ((stat >> 8) & 0377); 142432d19d43Seric } 142525a99e2eSeric } 1426ea4dc939Seric /* 1427ea4dc939Seric ** SENDALL -- actually send all the messages. 1428ea4dc939Seric ** 1429ea4dc939Seric ** Parameters: 1430ea4dc939Seric ** verifyonly -- if set, only give verification messages. 1431ea4dc939Seric ** 1432ea4dc939Seric ** Returns: 1433ea4dc939Seric ** none. 1434ea4dc939Seric ** 1435ea4dc939Seric ** Side Effects: 1436ea4dc939Seric ** Scans the send lists and sends everything it finds. 1437ea4dc939Seric */ 1438ea4dc939Seric 1439ea4dc939Seric sendall(verifyonly) 1440ea4dc939Seric bool verifyonly; 1441ea4dc939Seric { 1442e77e673fSeric register ADDRESS *q; 1443ea4dc939Seric typedef int (*fnptr)(); 1444ea4dc939Seric 1445772e6e50Seric # ifdef DEBUG 1446772e6e50Seric if (Debug > 1) 1447772e6e50Seric { 1448772e6e50Seric printf("\nSendQueue:\n"); 1449772e6e50Seric printaddr(SendQueue, TRUE); 1450772e6e50Seric } 1451772e6e50Seric # endif DEBUG 1452ea4dc939Seric 1453e77e673fSeric for (q = SendQueue; q != NULL; q = q->q_next) 1454ea4dc939Seric { 1455ea4dc939Seric if (verifyonly) 1456ea4dc939Seric { 1457ea4dc939Seric To = q->q_paddr; 1458e77e673fSeric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1459ea4dc939Seric { 14607da1035fSeric if (bitset(M_LOCAL, q->q_mailer->m_flags)) 1461ea4dc939Seric message(Arpa_Info, "deliverable"); 1462ea4dc939Seric else 1463ea4dc939Seric message(Arpa_Info, "queueable"); 1464ea4dc939Seric } 1465ea4dc939Seric } 1466ea4dc939Seric else 1467ea4dc939Seric (void) deliver(q, (fnptr) NULL); 1468ea4dc939Seric } 1469ea4dc939Seric } 1470