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*508daeccSeric SCCSID(@(#)deliver.c 3.73 03/06/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 /* 256*508daeccSeric ** Pass it to the other host if we are running SMTP. 257*508daeccSeric */ 258*508daeccSeric 259*508daeccSeric if (clever) 260*508daeccSeric { 261*508daeccSeric # ifdef SMTP 262*508daeccSeric i = smtprcpt(to); 263*508daeccSeric if (i != EX_OK) 264*508daeccSeric { 265*508daeccSeric # ifdef QUEUE 266*508daeccSeric if (i == EX_TEMPFAIL) 267*508daeccSeric { 268*508daeccSeric QueueUp = TRUE; 269*508daeccSeric to->q_flags |= QQUEUEUP; 270*508daeccSeric } 271*508daeccSeric else 272*508daeccSeric # endif QUEUE 273*508daeccSeric { 274*508daeccSeric to->q_flags |= QBADADDR; 275*508daeccSeric giveresponse(i, TRUE, m); 276*508daeccSeric } 277*508daeccSeric } 278*508daeccSeric # else SMTP 279*508daeccSeric syserr("trying to be clever"); 280*508daeccSeric # endif SMTP 281*508daeccSeric } 282*508daeccSeric 283*508daeccSeric /* 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 324*508daeccSeric /* link together the chain of recipients */ 325*508daeccSeric if (!bitset(QDONTSEND, to->q_flags)) 326*508daeccSeric { 327*508daeccSeric to->q_tchain = tochain; 328*508daeccSeric tochain = to; 329*508daeccSeric } 330*508daeccSeric 3315dfc646bSeric /* create list of users for error messages */ 3325dfc646bSeric if (tobuf[0] != '\0') 333db8841e9Seric (void) strcat(tobuf, ","); 334db8841e9Seric (void) strcat(tobuf, to->q_paddr); 3355dfc646bSeric define('u', user); /* to user */ 336c2567733Seric define('z', to->q_home); /* user's home */ 3375dfc646bSeric 338c579ef51Seric /* 339*508daeccSeric ** Expand out this user into argument list. 340c579ef51Seric */ 341c579ef51Seric 342*508daeccSeric if (!clever) 343c579ef51Seric { 344d4ccc802Seric (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 3455dfc646bSeric *pvp++ = newstr(buf); 3465dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 3475dfc646bSeric { 3485dfc646bSeric /* allow some space for trailing parms */ 3495dfc646bSeric break; 3505dfc646bSeric } 3515dfc646bSeric } 352c579ef51Seric } 3535dfc646bSeric 354145b49b1Seric /* see if any addresses still exist */ 355145b49b1Seric if (tobuf[0] == '\0') 356c579ef51Seric { 3572c7e1b8dSeric # ifdef SMTP 358c579ef51Seric if (clever) 359c579ef51Seric smtpquit(pv[0]); 3602c7e1b8dSeric # endif SMTP 361145b49b1Seric return (0); 362c579ef51Seric } 363145b49b1Seric 3645dfc646bSeric /* print out messages as full list */ 3655dfc646bSeric To = tobuf; 3665dfc646bSeric 3675dfc646bSeric /* 3685dfc646bSeric ** Fill out any parameters after the $u parameter. 3695dfc646bSeric */ 3705dfc646bSeric 371c579ef51Seric while (!clever && *++mvp != NULL) 3725dfc646bSeric { 373db8841e9Seric (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 3745dfc646bSeric *pvp++ = newstr(buf); 3755dfc646bSeric if (pvp >= &pv[MAXPV]) 3765dfc646bSeric syserr("deliver: pv overflow after $u for %s", pv[0]); 3775dfc646bSeric } 3785dfc646bSeric *pvp++ = NULL; 3795dfc646bSeric 38025a99e2eSeric /* 38125a99e2eSeric ** Call the mailer. 3826328bdf7Seric ** The argument vector gets built, pipes 38325a99e2eSeric ** are created as necessary, and we fork & exec as 3846328bdf7Seric ** appropriate. 385c579ef51Seric ** If we are running SMTP, we just need to clean up. 38625a99e2eSeric */ 38725a99e2eSeric 3885dfc646bSeric if (editfcn == NULL) 3895dfc646bSeric editfcn = putmessage; 3906259796dSeric if (ctladdr == NULL) 3916259796dSeric ctladdr = &From; 3922c7e1b8dSeric # ifdef SMTP 393c579ef51Seric if (clever) 394c579ef51Seric { 395c579ef51Seric i = smtpfinish(m, editfcn); 396c579ef51Seric smtpquit(pv[0]); 397c579ef51Seric } 398c579ef51Seric else 3992c7e1b8dSeric # endif SMTP 4006259796dSeric i = sendoff(m, pv, editfcn, ctladdr); 4015dfc646bSeric 402c77d1c25Seric /* 403c77d1c25Seric ** If we got a temporary failure, arrange to queue the 404c77d1c25Seric ** addressees. 405c77d1c25Seric */ 406c77d1c25Seric 4072c7e1b8dSeric # ifdef QUEUE 408c77d1c25Seric if (i == EX_TEMPFAIL) 409c77d1c25Seric { 410c77d1c25Seric QueueUp = TRUE; 411772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 412c77d1c25Seric to->q_flags |= QQUEUEUP; 413c77d1c25Seric } 4142c7e1b8dSeric # endif QUEUE 415c77d1c25Seric 41635490626Seric errno = 0; 4175dfc646bSeric return (i); 41825a99e2eSeric } 4195dfc646bSeric /* 42032d19d43Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 42132d19d43Seric ** 42232d19d43Seric ** This MUST be a macro, since after a vfork we are running 42332d19d43Seric ** two processes on the same stack!!! 42432d19d43Seric ** 42532d19d43Seric ** Parameters: 42632d19d43Seric ** none. 42732d19d43Seric ** 42832d19d43Seric ** Returns: 42932d19d43Seric ** From a macro??? You've got to be kidding! 43032d19d43Seric ** 43132d19d43Seric ** Side Effects: 43232d19d43Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 43332d19d43Seric ** pid of child in parent, zero in child. 43432d19d43Seric ** -1 on unrecoverable error. 43532d19d43Seric ** 43632d19d43Seric ** Notes: 43732d19d43Seric ** I'm awfully sorry this looks so awful. That's 43832d19d43Seric ** vfork for you..... 43932d19d43Seric */ 44032d19d43Seric 44132d19d43Seric # define NFORKTRIES 5 44232d19d43Seric # ifdef VFORK 44332d19d43Seric # define XFORK vfork 44432d19d43Seric # else VFORK 44532d19d43Seric # define XFORK fork 44632d19d43Seric # endif VFORK 44732d19d43Seric 44832d19d43Seric # define DOFORK(fORKfN) \ 44932d19d43Seric {\ 45032d19d43Seric register int i;\ 45132d19d43Seric \ 45232d19d43Seric for (i = NFORKTRIES; i-- > 0; )\ 45332d19d43Seric {\ 45432d19d43Seric pid = fORKfN();\ 45532d19d43Seric if (pid >= 0)\ 45632d19d43Seric break;\ 45732d19d43Seric sleep((unsigned) NFORKTRIES - i);\ 45832d19d43Seric }\ 45932d19d43Seric } 46032d19d43Seric /* 4612ed72599Seric ** DOFORK -- simple fork interface to DOFORK. 4622ed72599Seric ** 4632ed72599Seric ** Parameters: 4642ed72599Seric ** none. 4652ed72599Seric ** 4662ed72599Seric ** Returns: 4672ed72599Seric ** pid of child in parent. 4682ed72599Seric ** zero in child. 4692ed72599Seric ** -1 on error. 4702ed72599Seric ** 4712ed72599Seric ** Side Effects: 4722ed72599Seric ** returns twice, once in parent and once in child. 4732ed72599Seric */ 4742ed72599Seric 4752ed72599Seric dofork() 4762ed72599Seric { 4772ed72599Seric register int pid; 4782ed72599Seric 4792ed72599Seric DOFORK(fork); 4802ed72599Seric return (pid); 4812ed72599Seric } 4822ed72599Seric /* 4835dfc646bSeric ** SENDOFF -- send off call to mailer & collect response. 4845dfc646bSeric ** 4855dfc646bSeric ** Parameters: 4865dfc646bSeric ** m -- mailer descriptor. 4875dfc646bSeric ** pvp -- parameter vector to send to it. 4885dfc646bSeric ** editfcn -- function to pipe it through. 4896259796dSeric ** ctladdr -- an address pointer controlling the 4906259796dSeric ** user/groupid etc. of the mailer. 4915dfc646bSeric ** 4925dfc646bSeric ** Returns: 4935dfc646bSeric ** exit status of mailer. 4945dfc646bSeric ** 4955dfc646bSeric ** Side Effects: 4965dfc646bSeric ** none. 4975dfc646bSeric */ 4985dfc646bSeric 4996259796dSeric sendoff(m, pvp, editfcn, ctladdr) 5005dfc646bSeric struct mailer *m; 5015dfc646bSeric char **pvp; 5025dfc646bSeric int (*editfcn)(); 5036259796dSeric ADDRESS *ctladdr; 5045dfc646bSeric { 505c579ef51Seric auto FILE *mfile; 506c579ef51Seric auto FILE *rfile; 5075dfc646bSeric register int i; 508c579ef51Seric extern putmessage(); 509c579ef51Seric int pid; 510c579ef51Seric 511c579ef51Seric /* 512c579ef51Seric ** Create connection to mailer. 513c579ef51Seric */ 514c579ef51Seric 515c579ef51Seric pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 516c579ef51Seric if (pid < 0) 517c579ef51Seric return (-1); 518c579ef51Seric 519c579ef51Seric /* 520c579ef51Seric ** Format and send message. 521c579ef51Seric */ 522c579ef51Seric 523c579ef51Seric (void) signal(SIGPIPE, SIG_IGN); 524c579ef51Seric if (editfcn == NULL) 525c579ef51Seric editfcn = putmessage; 526c579ef51Seric 527c579ef51Seric (*editfcn)(mfile, m, FALSE); 528c579ef51Seric (void) fclose(mfile); 529c579ef51Seric 530c579ef51Seric i = endmailer(pid, pvp[0]); 531c579ef51Seric giveresponse(i, TRUE, m); 532bc6e2962Seric 533bc6e2962Seric /* arrange a return receipt if requested */ 534bc6e2962Seric if (RetReceipt && bitset(M_LOCAL, m->m_flags) && i == EX_OK) 535bc6e2962Seric { 536bc6e2962Seric SendReceipt = TRUE; 537bc6e2962Seric fprintf(Xscript, "%s... successfully delivered\n", To); 538bc6e2962Seric /* do we want to send back more info? */ 539bc6e2962Seric } 540bc6e2962Seric 541c579ef51Seric return (i); 542c579ef51Seric } 543c579ef51Seric /* 544c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 545c579ef51Seric ** 546c579ef51Seric ** We should never get fatal errors (e.g., segmentation 547c579ef51Seric ** violation), so we report those specially. For other 548c579ef51Seric ** errors, we choose a status message (into statmsg), 549c579ef51Seric ** and if it represents an error, we print it. 550c579ef51Seric ** 551c579ef51Seric ** Parameters: 552c579ef51Seric ** pid -- pid of mailer. 553c579ef51Seric ** name -- name of mailer (for error messages). 554c579ef51Seric ** 555c579ef51Seric ** Returns: 556c579ef51Seric ** exit code of mailer. 557c579ef51Seric ** 558c579ef51Seric ** Side Effects: 559c579ef51Seric ** none. 560c579ef51Seric */ 561c579ef51Seric 562c579ef51Seric endmailer(pid, name) 563c579ef51Seric int pid; 564c579ef51Seric char *name; 565c579ef51Seric { 566c579ef51Seric register int i; 567c579ef51Seric auto int st; 568c579ef51Seric 56933db8731Seric /* in the IPC case there is nothing to wait for */ 57033db8731Seric if (pid == 0) 57133db8731Seric return (EX_OK); 57233db8731Seric 57333db8731Seric /* wait for the mailer process to die and collect status */ 574c579ef51Seric while ((i = wait(&st)) > 0 && i != pid) 575c579ef51Seric continue; 576c579ef51Seric if (i < 0) 577c579ef51Seric { 578c579ef51Seric syserr("wait"); 579c579ef51Seric return (-1); 580c579ef51Seric } 58133db8731Seric 58233db8731Seric /* see if it died a horrid death */ 583c579ef51Seric if ((st & 0377) != 0) 584c579ef51Seric { 585c579ef51Seric syserr("%s: stat %o", name, st); 586c579ef51Seric ExitStat = EX_UNAVAILABLE; 587c579ef51Seric return (-1); 588c579ef51Seric } 58933db8731Seric 59033db8731Seric /* normal death -- return status */ 591c579ef51Seric i = (st >> 8) & 0377; 592c579ef51Seric return (i); 593c579ef51Seric } 594c579ef51Seric /* 595c579ef51Seric ** OPENMAILER -- open connection to mailer. 596c579ef51Seric ** 597c579ef51Seric ** Parameters: 598c579ef51Seric ** m -- mailer descriptor. 599c579ef51Seric ** pvp -- parameter vector to pass to mailer. 600c579ef51Seric ** ctladdr -- controlling address for user. 601c579ef51Seric ** clever -- create a full duplex connection. 602c579ef51Seric ** pmfile -- pointer to mfile (to mailer) connection. 603c579ef51Seric ** prfile -- pointer to rfile (from mailer) connection. 604c579ef51Seric ** 605c579ef51Seric ** Returns: 60633db8731Seric ** pid of mailer ( > 0 ). 607c579ef51Seric ** -1 on error. 60833db8731Seric ** zero on an IPC connection. 609c579ef51Seric ** 610c579ef51Seric ** Side Effects: 611c579ef51Seric ** creates a mailer in a subprocess. 612c579ef51Seric */ 613c579ef51Seric 614c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 615c579ef51Seric struct mailer *m; 616c579ef51Seric char **pvp; 617c579ef51Seric ADDRESS *ctladdr; 618c579ef51Seric bool clever; 619c579ef51Seric FILE **pmfile; 620c579ef51Seric FILE **prfile; 621c579ef51Seric { 6225dfc646bSeric int pid; 623f8952a83Seric int mpvect[2]; 624c579ef51Seric int rpvect[2]; 6255dfc646bSeric FILE *mfile; 626c579ef51Seric FILE *rfile; 6275dfc646bSeric extern FILE *fdopen(); 6285dfc646bSeric 6295dfc646bSeric # ifdef DEBUG 6305dfc646bSeric if (Debug) 6315dfc646bSeric { 632c579ef51Seric printf("openmailer:\n"); 6335dfc646bSeric printav(pvp); 6345dfc646bSeric } 6355dfc646bSeric # endif DEBUG 63635490626Seric errno = 0; 6375dfc646bSeric 63833db8731Seric # ifdef DAEMON 63933db8731Seric /* 64033db8731Seric ** Deal with the special case of mail handled through an IPC 64133db8731Seric ** connection. 64233db8731Seric ** In this case we don't actually fork. We must be 64333db8731Seric ** running SMTP for this to work. We will return a 64433db8731Seric ** zero pid to indicate that we are running IPC. 64533db8731Seric */ 64633db8731Seric 64733db8731Seric if (strcmp(m->m_mailer, "[IPC]") == 0) 64833db8731Seric { 64933db8731Seric register int i; 65033db8731Seric 65133db8731Seric if (!clever) 65233db8731Seric syserr("non-clever IPC"); 65333db8731Seric i = makeconnection(pvp[1], pmfile, prfile); 65433db8731Seric if (i != EX_OK) 655ed854c7bSeric { 656ed854c7bSeric ExitStat = i; 65733db8731Seric return (-1); 658ed854c7bSeric } 65933db8731Seric else 66033db8731Seric return (0); 66133db8731Seric } 66233db8731Seric # endif DAEMON 66333db8731Seric 6646328bdf7Seric /* create a pipe to shove the mail through */ 665f8952a83Seric if (pipe(mpvect) < 0) 66625a99e2eSeric { 667c579ef51Seric syserr("pipe (to mailer)"); 66825a99e2eSeric return (-1); 66925a99e2eSeric } 670c579ef51Seric 6712c7e1b8dSeric # ifdef SMTP 672c579ef51Seric /* if this mailer speaks smtp, create a return pipe */ 673c579ef51Seric if (clever && pipe(rpvect) < 0) 674c579ef51Seric { 675c579ef51Seric syserr("pipe (from mailer)"); 676c579ef51Seric (void) close(mpvect[0]); 677c579ef51Seric (void) close(mpvect[1]); 678c579ef51Seric return (-1); 679c579ef51Seric } 6802c7e1b8dSeric # endif SMTP 681c579ef51Seric 68233db8731Seric /* 68333db8731Seric ** Actually fork the mailer process. 68433db8731Seric ** DOFORK is clever about retrying. 68533db8731Seric */ 68633db8731Seric 68732d19d43Seric DOFORK(XFORK); 688f129ec7dSeric /* pid is set by DOFORK */ 68925a99e2eSeric if (pid < 0) 69025a99e2eSeric { 69133db8731Seric /* failure */ 69225a99e2eSeric syserr("Cannot fork"); 693f8952a83Seric (void) close(mpvect[0]); 694f8952a83Seric (void) close(mpvect[1]); 695c579ef51Seric if (clever) 696c579ef51Seric { 697c579ef51Seric (void) close(rpvect[0]); 698c579ef51Seric (void) close(rpvect[1]); 699c579ef51Seric } 70025a99e2eSeric return (-1); 70125a99e2eSeric } 70225a99e2eSeric else if (pid == 0) 70325a99e2eSeric { 70425a99e2eSeric /* child -- set up input & exec mailer */ 70503ab8e55Seric /* make diagnostic output be standard output */ 7068f0e7860Seric (void) signal(SIGINT, SIG_IGN); 7078f0e7860Seric (void) signal(SIGHUP, SIG_IGN); 7080984da9fSeric (void) signal(SIGTERM, SIG_DFL); 709f8952a83Seric 710f8952a83Seric /* arrange to filter standard & diag output of command */ 711c579ef51Seric if (clever) 712c579ef51Seric { 713c579ef51Seric (void) close(rpvect[0]); 714c579ef51Seric (void) close(1); 715c579ef51Seric (void) dup(rpvect[1]); 716c579ef51Seric (void) close(rpvect[1]); 717c579ef51Seric } 718c579ef51Seric else if (OutChannel != stdout) 719f8952a83Seric { 720f8952a83Seric (void) close(1); 721f8952a83Seric (void) dup(fileno(OutChannel)); 722f8952a83Seric } 723db8841e9Seric (void) close(2); 724db8841e9Seric (void) dup(1); 725f8952a83Seric 726f8952a83Seric /* arrange to get standard input */ 727f8952a83Seric (void) close(mpvect[1]); 728db8841e9Seric (void) close(0); 729f8952a83Seric if (dup(mpvect[0]) < 0) 73025a99e2eSeric { 73125a99e2eSeric syserr("Cannot dup to zero!"); 732a590b978Seric _exit(EX_OSERR); 73325a99e2eSeric } 734f8952a83Seric (void) close(mpvect[0]); 7352a6e0786Seric if (!bitset(M_RESTR, m->m_flags)) 7360984da9fSeric { 737e36b99e2Seric if (ctladdr->q_uid == 0) 738e36b99e2Seric { 739e36b99e2Seric (void) setgid(DefGid); 740e36b99e2Seric (void) setuid(DefUid); 741e36b99e2Seric } 742e36b99e2Seric else 74369f29479Seric { 744e36b99e2Seric (void) setgid(ctladdr->q_gid); 745e36b99e2Seric (void) setuid(ctladdr->q_uid); 74669f29479Seric } 7470984da9fSeric } 748e374fd72Seric # ifndef VFORK 749e374fd72Seric /* 750e374fd72Seric ** We have to be careful with vfork - we can't mung up the 751e374fd72Seric ** memory but we don't want the mailer to inherit any extra 752e374fd72Seric ** open files. Chances are the mailer won't 753e374fd72Seric ** care about an extra file, but then again you never know. 754e374fd72Seric ** Actually, we would like to close(fileno(pwf)), but it's 755e374fd72Seric ** declared static so we can't. But if we fclose(pwf), which 756e374fd72Seric ** is what endpwent does, it closes it in the parent too and 757e374fd72Seric ** the next getpwnam will be slower. If you have a weird 758e374fd72Seric ** mailer that chokes on the extra file you should do the 759e374fd72Seric ** endpwent(). 760e374fd72Seric ** 761e374fd72Seric ** Similar comments apply to log. However, openlog is 762e374fd72Seric ** clever enough to set the FIOCLEX mode on the file, 763e374fd72Seric ** so it will be closed automatically on the exec. 764e374fd72Seric */ 765e374fd72Seric 766e374fd72Seric endpwent(); 76725a99e2eSeric # ifdef LOG 768f9fe028fSeric closelog(); 76925a99e2eSeric # endif LOG 770e374fd72Seric # endif VFORK 77133db8731Seric 77233db8731Seric /* try to execute the mailer */ 77325a99e2eSeric execv(m->m_mailer, pvp); 77433db8731Seric 77525a99e2eSeric /* syserr fails because log is closed */ 77625a99e2eSeric /* syserr("Cannot exec %s", m->m_mailer); */ 77732d19d43Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 778db8841e9Seric (void) fflush(stdout); 779a590b978Seric _exit(EX_UNAVAILABLE); 78025a99e2eSeric } 78125a99e2eSeric 782f8952a83Seric /* 783c579ef51Seric ** Set up return value. 784f8952a83Seric */ 785f8952a83Seric 786f8952a83Seric (void) close(mpvect[0]); 787f8952a83Seric mfile = fdopen(mpvect[1], "w"); 788c579ef51Seric if (clever) 78925a99e2eSeric { 790c579ef51Seric (void) close(rpvect[1]); 791c579ef51Seric rfile = fdopen(rpvect[0], "r"); 79225a99e2eSeric } 793c579ef51Seric 794c579ef51Seric *pmfile = mfile; 795c579ef51Seric *prfile = rfile; 796c579ef51Seric 797c579ef51Seric return (pid); 79825a99e2eSeric } 79925a99e2eSeric /* 80025a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 80125a99e2eSeric ** 80225a99e2eSeric ** Parameters: 80325a99e2eSeric ** stat -- the status code from the mailer (high byte 80425a99e2eSeric ** only; core dumps must have been taken care of 80525a99e2eSeric ** already). 80625a99e2eSeric ** force -- if set, force an error message output, even 80725a99e2eSeric ** if the mailer seems to like to print its own 80825a99e2eSeric ** messages. 80925a99e2eSeric ** m -- the mailer descriptor for this mailer. 81025a99e2eSeric ** 81125a99e2eSeric ** Returns: 812db8841e9Seric ** none. 81325a99e2eSeric ** 81425a99e2eSeric ** Side Effects: 815c1f9df2cSeric ** Errors may be incremented. 81625a99e2eSeric ** ExitStat may be set. 81725a99e2eSeric */ 81825a99e2eSeric 81925a99e2eSeric giveresponse(stat, force, m) 82025a99e2eSeric int stat; 82125a99e2eSeric int force; 82225a99e2eSeric register struct mailer *m; 82325a99e2eSeric { 82425a99e2eSeric register char *statmsg; 82525a99e2eSeric extern char *SysExMsg[]; 82625a99e2eSeric register int i; 82725a99e2eSeric extern int N_SysEx; 82829dd97a3Seric char buf[30]; 82925a99e2eSeric 83013bbc08cSeric /* 83113bbc08cSeric ** Compute status message from code. 83213bbc08cSeric */ 83313bbc08cSeric 83425a99e2eSeric i = stat - EX__BASE; 83525a99e2eSeric if (i < 0 || i > N_SysEx) 83625a99e2eSeric statmsg = NULL; 83725a99e2eSeric else 83825a99e2eSeric statmsg = SysExMsg[i]; 83925a99e2eSeric if (stat == 0) 840c38ba59cSeric { 8416cbfa739Seric if (bitset(M_LOCAL, m->m_flags)) 8423efaed6eSeric statmsg = "delivered"; 8433efaed6eSeric else 8443efaed6eSeric statmsg = "queued"; 845c38ba59cSeric if (Verbose) 846544026c5Seric message(Arpa_Info, statmsg); 847c38ba59cSeric } 8482c7e1b8dSeric # ifdef QUEUE 849c77d1c25Seric else if (stat == EX_TEMPFAIL) 850c77d1c25Seric { 851c77d1c25Seric if (Verbose) 852c77d1c25Seric message(Arpa_Info, "transmission deferred"); 853c77d1c25Seric } 8542c7e1b8dSeric # endif QUEUE 85525a99e2eSeric else 85625a99e2eSeric { 857c1f9df2cSeric Errors++; 858ed854c7bSeric FatalErrors = TRUE; 85925a99e2eSeric if (statmsg == NULL && m->m_badstat != 0) 86025a99e2eSeric { 86125a99e2eSeric stat = m->m_badstat; 86225a99e2eSeric i = stat - EX__BASE; 86325a99e2eSeric # ifdef DEBUG 86425a99e2eSeric if (i < 0 || i >= N_SysEx) 86525a99e2eSeric syserr("Bad m_badstat %d", stat); 86625a99e2eSeric else 86725a99e2eSeric # endif DEBUG 86825a99e2eSeric statmsg = SysExMsg[i]; 86925a99e2eSeric } 87025a99e2eSeric if (statmsg == NULL) 87125a99e2eSeric usrerr("unknown mailer response %d", stat); 872c38ba59cSeric else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 87325a99e2eSeric usrerr("%s", statmsg); 87425a99e2eSeric } 87525a99e2eSeric 87625a99e2eSeric /* 87725a99e2eSeric ** Final cleanup. 87825a99e2eSeric ** Log a record of the transaction. Compute the new 87925a99e2eSeric ** ExitStat -- if we already had an error, stick with 88025a99e2eSeric ** that. 88125a99e2eSeric */ 88225a99e2eSeric 88325a99e2eSeric if (statmsg == NULL) 88429dd97a3Seric { 885db8841e9Seric (void) sprintf(buf, "error %d", stat); 88629dd97a3Seric statmsg = buf; 88729dd97a3Seric } 88829dd97a3Seric 88929dd97a3Seric # ifdef LOG 890e374fd72Seric syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg); 89125a99e2eSeric # endif LOG 8922c7e1b8dSeric # ifdef QUEUE 893c77d1c25Seric if (stat != EX_TEMPFAIL) 8942c7e1b8dSeric # endif QUEUE 895243921eeSeric setstat(stat); 89625a99e2eSeric } 89725a99e2eSeric /* 8986328bdf7Seric ** PUTMESSAGE -- output a message to the final mailer. 89925a99e2eSeric ** 9006328bdf7Seric ** This routine takes care of recreating the header from the 9016328bdf7Seric ** in-core copy, etc. 90225a99e2eSeric ** 90325a99e2eSeric ** Parameters: 9046328bdf7Seric ** fp -- file to output onto. 9056328bdf7Seric ** m -- a mailer descriptor. 906c579ef51Seric ** xdot -- if set, hide lines beginning with dot. 90725a99e2eSeric ** 90825a99e2eSeric ** Returns: 9096328bdf7Seric ** none. 91025a99e2eSeric ** 91125a99e2eSeric ** Side Effects: 9126328bdf7Seric ** The message is written onto fp. 91325a99e2eSeric */ 91425a99e2eSeric 915c579ef51Seric putmessage(fp, m, xdot) 9166328bdf7Seric FILE *fp; 9176328bdf7Seric struct mailer *m; 918c579ef51Seric bool xdot; 91925a99e2eSeric { 9206328bdf7Seric char buf[BUFSIZ]; 92125a99e2eSeric 92213bbc08cSeric /* 92313bbc08cSeric ** Output "From" line unless supressed 924a36e30c9Seric ** 925a36e30c9Seric ** >>>>>>>>>> One of the ugliest hacks seen by human eyes is 926a36e30c9Seric ** >>>>>>>>>> contained herein: UUCP wants those stupid 92774b6e67bSeric ** >> NOTE >> "remote from <host>" lines. Why oh why does a 92874b6e67bSeric ** >>>>>>>>>> well-meaning programmer such as myself have to 929a36e30c9Seric ** >>>>>>>>>> deal with this kind of antique garbage???? 93013bbc08cSeric */ 93113bbc08cSeric 93240e4ab56Seric if (!bitset(M_NHDR, m->m_flags)) 9331412cc7cSeric { 9342c7e1b8dSeric # ifdef UGLYUUCP 935a49f24c0Seric if (bitset(M_UGLYUUCP, m->m_flags)) 93674b6e67bSeric { 93774b6e67bSeric extern char *macvalue(); 93874b6e67bSeric char *sys = macvalue('g'); 93974b6e67bSeric char *bang = index(sys, '!'); 94074b6e67bSeric 94174b6e67bSeric if (bang == NULL) 94274b6e67bSeric syserr("No ! in UUCP! (%s)", sys); 94374b6e67bSeric else 94474b6e67bSeric *bang = '\0'; 94574b6e67bSeric (void) expand("From $f $d remote from $g", buf, 946a36e30c9Seric &buf[sizeof buf - 1]); 94774b6e67bSeric *bang = '!'; 94874b6e67bSeric } 949a36e30c9Seric else 9502c7e1b8dSeric # endif UGLYUUCP 9511412cc7cSeric (void) expand("$l", buf, &buf[sizeof buf - 1]); 9521412cc7cSeric fprintf(fp, "%s\n", buf); 9531412cc7cSeric } 95440e4ab56Seric 95513bbc08cSeric /* 95613bbc08cSeric ** Output all header lines 95713bbc08cSeric */ 95813bbc08cSeric 959bc6e2962Seric putheader(fp, m); 960bc6e2962Seric 961bc6e2962Seric /* 962bc6e2962Seric ** Output the body of the message 963bc6e2962Seric */ 964bc6e2962Seric 965bc6e2962Seric if (TempFile != NULL) 966bc6e2962Seric { 967bc6e2962Seric rewind(TempFile); 968bc6e2962Seric while (!ferror(fp) && fgets(buf, sizeof buf, TempFile) != NULL) 969bc6e2962Seric fprintf(fp, "%s%s", xdot && buf[0] == '.' ? "." : "", buf); 970bc6e2962Seric 971bc6e2962Seric if (ferror(TempFile)) 972bc6e2962Seric { 973bc6e2962Seric syserr("putmessage: read error"); 974ed854c7bSeric ExitStat = EX_IOERR; 975bc6e2962Seric } 976bc6e2962Seric } 977bc6e2962Seric 978bc6e2962Seric (void) fflush(fp); 979bc6e2962Seric if (ferror(fp) && errno != EPIPE) 980bc6e2962Seric { 981bc6e2962Seric syserr("putmessage: write error"); 982ed854c7bSeric ExitStat = EX_IOERR; 983bc6e2962Seric } 984bc6e2962Seric errno = 0; 985bc6e2962Seric } 986bc6e2962Seric /* 987bc6e2962Seric ** PUTHEADER -- put the header part of a message 988bc6e2962Seric ** 989bc6e2962Seric ** Parameters: 990bc6e2962Seric ** fp -- file to put it on. 991bc6e2962Seric ** m -- mailer to use. 992bc6e2962Seric ** 993bc6e2962Seric ** Returns: 994bc6e2962Seric ** none. 995bc6e2962Seric ** 996bc6e2962Seric ** Side Effects: 997bc6e2962Seric ** none. 998bc6e2962Seric */ 999bc6e2962Seric 1000bc6e2962Seric putheader(fp, m) 1001bc6e2962Seric register FILE *fp; 1002bc6e2962Seric register struct mailer *m; 1003bc6e2962Seric { 1004bc6e2962Seric char buf[BUFSIZ]; 1005bc6e2962Seric register HDR *h; 1006bc6e2962Seric extern char *arpadate(); 1007bc6e2962Seric bool anyheader = FALSE; 1008bc6e2962Seric extern char *capitalize(); 1009bc6e2962Seric extern char *hvalue(); 1010bc6e2962Seric extern bool samefrom(); 1011bc6e2962Seric char *of_line; 1012bc6e2962Seric 1013894de7daSeric of_line = hvalue("original-from"); 10146328bdf7Seric for (h = Header; h != NULL; h = h->h_link) 10156328bdf7Seric { 101613bbc08cSeric register char *p; 101787bd9a02Seric char *origfrom = OrigFrom; 1018894de7daSeric bool nooutput; 101913bbc08cSeric 1020894de7daSeric nooutput = FALSE; 10219e9163a0Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 1022894de7daSeric nooutput = TRUE; 1023894de7daSeric 1024894de7daSeric /* use From: line from message if generated is the same */ 102587bd9a02Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL && 1026894de7daSeric strcmp(m->m_from, "$f") == 0 && of_line == NULL) 102787bd9a02Seric { 102887bd9a02Seric p = origfrom; 102987bd9a02Seric origfrom = NULL; 103087bd9a02Seric } 103187bd9a02Seric else if (bitset(H_DEFAULT, h->h_flags)) 10324ae18d0eSeric { 1033db8841e9Seric (void) expand(h->h_value, buf, &buf[sizeof buf]); 10344ae18d0eSeric p = buf; 10354ae18d0eSeric } 10368c2bf25bSeric else if (bitset(H_ADDR, h->h_flags)) 10378c2bf25bSeric { 10388c2bf25bSeric register int opos; 10398c2bf25bSeric bool firstone = TRUE; 10408c2bf25bSeric 10418c2bf25bSeric /* 10428c2bf25bSeric ** Output the address list translated by the 10438c2bf25bSeric ** mailer and with commas. 10448c2bf25bSeric */ 10458c2bf25bSeric 10468c2bf25bSeric p = h->h_value; 10478c2bf25bSeric if (p == NULL || *p == '\0' || nooutput) 10488c2bf25bSeric continue; 10498c2bf25bSeric fprintf(fp, "%s: ", capitalize(h->h_field)); 10508c2bf25bSeric opos = strlen(h->h_field) + 2; 10518c2bf25bSeric while (*p != '\0') 10528c2bf25bSeric { 10538c2bf25bSeric register char *name = p; 10548c2bf25bSeric extern char *remotename(); 10558c2bf25bSeric char savechar; 10568c2bf25bSeric 10578c2bf25bSeric /* find the end of the name */ 1058bc28c57aSeric while (*p != '\0' && *p != ',') 1059bc28c57aSeric { 1060bc28c57aSeric extern bool isatword(); 1061bc28c57aSeric char *oldp; 1062bc28c57aSeric 1063bc28c57aSeric if (!OldStyle || !isspace(*p)) 1064bc28c57aSeric { 10658c2bf25bSeric p++; 1066bc28c57aSeric continue; 1067bc28c57aSeric } 1068bc28c57aSeric oldp = p; 1069bc28c57aSeric while (*p != '\0' && isspace(*p)) 1070bc28c57aSeric p++; 1071bc28c57aSeric if (*p != '@' && !isatword(p)) 1072bc28c57aSeric { 1073bc28c57aSeric p = oldp; 1074bc28c57aSeric break; 1075bc28c57aSeric } 1076bc28c57aSeric p += *p == '@' ? 1 : 2; 1077bc28c57aSeric while (*p != '\0' && isspace(*p)) 1078bc28c57aSeric p++; 1079bc28c57aSeric } 10808c2bf25bSeric savechar = *p; 10818c2bf25bSeric *p = '\0'; 10828c2bf25bSeric 10838c2bf25bSeric /* translate the name to be relative */ 1084bc213ac6Seric name = remotename(name, m); 10858c2bf25bSeric if (*name == '\0') 10868c2bf25bSeric continue; 10878c2bf25bSeric 10888c2bf25bSeric /* output the name with nice formatting */ 10898c2bf25bSeric opos += strlen(name); 10908c2bf25bSeric if (!firstone) 10918c2bf25bSeric opos += 2; 10928c2bf25bSeric if (opos > 78 && !firstone) 10938c2bf25bSeric { 10948c2bf25bSeric fprintf(fp, ",\n "); 1095bc213ac6Seric opos = 8 + strlen(name); 10968c2bf25bSeric } 10978c2bf25bSeric else if (!firstone) 10988c2bf25bSeric fprintf(fp, ", "); 10998c2bf25bSeric fprintf(fp, "%s", name); 11008c2bf25bSeric firstone = FALSE; 11018c2bf25bSeric 11028c2bf25bSeric /* clean up the source string */ 11038c2bf25bSeric *p = savechar; 11048c2bf25bSeric while (*p != '\0' && (isspace(*p) || *p == ',')) 11058c2bf25bSeric p++; 11068c2bf25bSeric } 11078c2bf25bSeric fprintf(fp, "\n"); 11088c2bf25bSeric nooutput = TRUE; 11098c2bf25bSeric } 11104ae18d0eSeric else 11114ae18d0eSeric p = h->h_value; 1112894de7daSeric if (p == NULL || *p == '\0') 11139e9163a0Seric continue; 11145a0dcb5fSeric 11155a0dcb5fSeric /* hack, hack -- output Original-From field if different */ 1116894de7daSeric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL) 1117894de7daSeric { 1118894de7daSeric /* output new Original-From line if needed */ 1119894de7daSeric if (of_line == NULL && !samefrom(p, origfrom)) 11205a0dcb5fSeric { 112187bd9a02Seric fprintf(fp, "Original-From: %s\n", origfrom); 112287bd9a02Seric anyheader = TRUE; 11235a0dcb5fSeric } 1124894de7daSeric if (of_line != NULL && !nooutput && samefrom(p, of_line)) 1125894de7daSeric { 1126894de7daSeric /* delete Original-From: line if redundant */ 1127894de7daSeric p = of_line; 1128894de7daSeric of_line = NULL; 1129894de7daSeric } 1130894de7daSeric } 1131894de7daSeric else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL) 1132894de7daSeric nooutput = TRUE; 1133894de7daSeric 1134894de7daSeric /* finally, output the header line */ 1135894de7daSeric if (!nooutput) 1136894de7daSeric { 1137894de7daSeric fprintf(fp, "%s: %s\n", capitalize(h->h_field), p); 1138894de7daSeric h->h_flags |= H_USED; 1139894de7daSeric anyheader = TRUE; 1140894de7daSeric } 11416328bdf7Seric } 11426328bdf7Seric if (anyheader) 11436328bdf7Seric fprintf(fp, "\n"); 114425a99e2eSeric } 114525a99e2eSeric /* 1146bc28c57aSeric ** ISATWORD -- tell if the word we are pointing to is "at". 1147bc28c57aSeric ** 1148bc28c57aSeric ** Parameters: 1149bc28c57aSeric ** p -- word to check. 1150bc28c57aSeric ** 1151bc28c57aSeric ** Returns: 1152bc28c57aSeric ** TRUE -- if p is the word at. 1153bc28c57aSeric ** FALSE -- otherwise. 1154bc28c57aSeric ** 1155bc28c57aSeric ** Side Effects: 1156bc28c57aSeric ** none. 1157bc28c57aSeric */ 1158bc28c57aSeric 1159bc28c57aSeric bool 1160bc28c57aSeric isatword(p) 1161bc28c57aSeric register char *p; 1162bc28c57aSeric { 1163bc28c57aSeric extern char lower(); 1164bc28c57aSeric 1165bc28c57aSeric if (lower(p[0]) == 'a' && lower(p[1]) == 't' && 1166bc28c57aSeric p[2] != '\0' && isspace(p[2])) 1167bc28c57aSeric return (TRUE); 1168bc28c57aSeric return (FALSE); 1169bc28c57aSeric } 1170bc28c57aSeric /* 11718c2bf25bSeric ** REMOTENAME -- return the name relative to the current mailer 11728c2bf25bSeric ** 11738c2bf25bSeric ** Parameters: 11748c2bf25bSeric ** name -- the name to translate. 11758c2bf25bSeric ** 11768c2bf25bSeric ** Returns: 11778c2bf25bSeric ** the text string representing this address relative to 11788c2bf25bSeric ** the receiving mailer. 11798c2bf25bSeric ** 11808c2bf25bSeric ** Side Effects: 11818c2bf25bSeric ** none. 11828c2bf25bSeric ** 11838c2bf25bSeric ** Warnings: 11848c2bf25bSeric ** The text string returned is tucked away locally; 11858c2bf25bSeric ** copy it if you intend to save it. 11868c2bf25bSeric */ 11878c2bf25bSeric 11888c2bf25bSeric char * 1189bc213ac6Seric remotename(name, m) 11908c2bf25bSeric char *name; 1191bc213ac6Seric struct mailer *m; 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 /* 12048c2bf25bSeric ** Do general rewriting of name. 12058c2bf25bSeric ** This will also take care of doing global name translation. 12068c2bf25bSeric */ 12078c2bf25bSeric 1208bc213ac6Seric define('x', getxpart(name)); 12098c2bf25bSeric pvp = prescan(name, '\0'); 12108c2bf25bSeric for (;;) 12118c2bf25bSeric { 12128c2bf25bSeric rewrite(pvp, 1); 12138c2bf25bSeric rewrite(pvp, 3); 12148c2bf25bSeric if (**pvp == CANONNET) 12158c2bf25bSeric { 12168c2bf25bSeric auto ADDRESS a; 12178c2bf25bSeric register char *p; 12188c2bf25bSeric extern char *hostalias(); 12198c2bf25bSeric 12208c2bf25bSeric /* oops... resolved to something */ 12218c2bf25bSeric if (buildaddr(pvp, &a) == NULL) 12228c2bf25bSeric return (name); 12238c2bf25bSeric p = hostalias(&a); 12248c2bf25bSeric if (p == NULL) 12258c2bf25bSeric return (name); 12268c2bf25bSeric pvp = prescan(p, '\0'); 12278c2bf25bSeric } 12288c2bf25bSeric else 12298c2bf25bSeric { 12308c2bf25bSeric cataddr(pvp, lbuf, sizeof lbuf); 12318c2bf25bSeric break; 12328c2bf25bSeric } 12338c2bf25bSeric } 12348c2bf25bSeric 12358c2bf25bSeric /* make the name relative to the receiving mailer */ 12368c2bf25bSeric define('f', lbuf); 1237bc213ac6Seric (void) expand(m->m_from, buf, &buf[sizeof buf - 1]); 12388c2bf25bSeric 12398c2bf25bSeric /* rewrite to get rid of garbage we added in the expand above */ 12408c2bf25bSeric pvp = prescan(buf, '\0'); 12418c2bf25bSeric rewrite(pvp, 2); 1242bc213ac6Seric cataddr(pvp, lbuf, sizeof lbuf); 1243bc213ac6Seric 1244bc213ac6Seric /* now add any comment info we had before back */ 1245bc213ac6Seric define('g', lbuf); 1246bc213ac6Seric (void) expand("$q", buf, &buf[sizeof buf - 1]); 12478c2bf25bSeric 12488c2bf25bSeric define('f', oldf); 1249bc213ac6Seric define('g', oldg); 1250bc213ac6Seric define('x', oldx); 12518c2bf25bSeric 12528c2bf25bSeric # ifdef DEBUG 12538c2bf25bSeric if (Debug > 0) 12548c2bf25bSeric printf("remotename(%s) => `%s'\n", name, buf); 12558c2bf25bSeric # endif DEBUG 12568c2bf25bSeric return (buf); 12578c2bf25bSeric } 12588c2bf25bSeric /* 125987bd9a02Seric ** SAMEFROM -- tell if two text addresses represent the same from address. 126087bd9a02Seric ** 126187bd9a02Seric ** Parameters: 126287bd9a02Seric ** ifrom -- internally generated form of from address. 126387bd9a02Seric ** efrom -- external form of from address. 126487bd9a02Seric ** 126587bd9a02Seric ** Returns: 126687bd9a02Seric ** TRUE -- if they convey the same info. 126787bd9a02Seric ** FALSE -- if any information has been lost. 126887bd9a02Seric ** 126987bd9a02Seric ** Side Effects: 127087bd9a02Seric ** none. 127187bd9a02Seric */ 127287bd9a02Seric 127387bd9a02Seric bool 127487bd9a02Seric samefrom(ifrom, efrom) 127587bd9a02Seric char *ifrom; 127687bd9a02Seric char *efrom; 127787bd9a02Seric { 1278894de7daSeric register char *p; 1279894de7daSeric char buf[MAXNAME + 4]; 1280894de7daSeric 1281894de7daSeric # ifdef DEBUG 1282894de7daSeric if (Debug > 7) 1283894de7daSeric printf("samefrom(%s,%s)-->", ifrom, efrom); 1284894de7daSeric # endif DEBUG 1285894de7daSeric if (strcmp(ifrom, efrom) == 0) 1286894de7daSeric goto success; 1287894de7daSeric p = index(ifrom, '@'); 1288894de7daSeric if (p == NULL) 1289894de7daSeric goto failure; 1290894de7daSeric *p = '\0'; 1291894de7daSeric strcpy(buf, ifrom); 1292894de7daSeric strcat(buf, " at "); 1293894de7daSeric *p++ = '@'; 1294894de7daSeric strcat(buf, p); 1295894de7daSeric if (strcmp(buf, efrom) == 0) 1296894de7daSeric goto success; 1297894de7daSeric 1298894de7daSeric failure: 1299894de7daSeric # ifdef DEBUG 1300894de7daSeric if (Debug > 7) 1301894de7daSeric printf("FALSE\n"); 1302894de7daSeric # endif DEBUG 1303894de7daSeric return (FALSE); 1304894de7daSeric 1305894de7daSeric success: 1306894de7daSeric # ifdef DEBUG 1307894de7daSeric if (Debug > 7) 1308894de7daSeric printf("TRUE\n"); 1309894de7daSeric # endif DEBUG 1310894de7daSeric return (TRUE); 131187bd9a02Seric } 131287bd9a02Seric /* 131325a99e2eSeric ** MAILFILE -- Send a message to a file. 131425a99e2eSeric ** 1315f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 1316f129ec7dSeric ** bits, sendmail will try to become the owner of that file 1317f129ec7dSeric ** rather than the real user. Obviously, this only works if 1318f129ec7dSeric ** sendmail runs as root. 1319f129ec7dSeric ** 132025a99e2eSeric ** Parameters: 132125a99e2eSeric ** filename -- the name of the file to send to. 13226259796dSeric ** ctladdr -- the controlling address header -- includes 13236259796dSeric ** the userid/groupid to be when sending. 132425a99e2eSeric ** 132525a99e2eSeric ** Returns: 132625a99e2eSeric ** The exit code associated with the operation. 132725a99e2eSeric ** 132825a99e2eSeric ** Side Effects: 132925a99e2eSeric ** none. 133025a99e2eSeric */ 133125a99e2eSeric 13326259796dSeric mailfile(filename, ctladdr) 133325a99e2eSeric char *filename; 13346259796dSeric ADDRESS *ctladdr; 133525a99e2eSeric { 133625a99e2eSeric register FILE *f; 133732d19d43Seric register int pid; 133825a99e2eSeric 133932d19d43Seric /* 134032d19d43Seric ** Fork so we can change permissions here. 134132d19d43Seric ** Note that we MUST use fork, not vfork, because of 134232d19d43Seric ** the complications of calling subroutines, etc. 134332d19d43Seric */ 134432d19d43Seric 134532d19d43Seric DOFORK(fork); 134632d19d43Seric 134732d19d43Seric if (pid < 0) 134832d19d43Seric return (EX_OSERR); 134932d19d43Seric else if (pid == 0) 135032d19d43Seric { 135132d19d43Seric /* child -- actually write to file */ 1352f129ec7dSeric struct stat stb; 1353f129ec7dSeric 13540984da9fSeric (void) signal(SIGINT, SIG_DFL); 13550984da9fSeric (void) signal(SIGHUP, SIG_DFL); 13560984da9fSeric (void) signal(SIGTERM, SIG_DFL); 1357f129ec7dSeric umask(OldUmask); 1358f129ec7dSeric if (stat(filename, &stb) < 0) 1359e6e1265fSeric stb.st_mode = 0666; 1360f129ec7dSeric if (bitset(0111, stb.st_mode)) 1361f129ec7dSeric exit(EX_CANTCREAT); 136203827b5fSeric if (ctladdr == NULL) 136303827b5fSeric ctladdr = &From; 1364f129ec7dSeric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 1365e36b99e2Seric { 1366e36b99e2Seric if (ctladdr->q_uid == 0) 1367e36b99e2Seric (void) setgid(DefGid); 1368e36b99e2Seric else 13696259796dSeric (void) setgid(ctladdr->q_gid); 1370e36b99e2Seric } 1371f129ec7dSeric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 1372e36b99e2Seric { 1373e36b99e2Seric if (ctladdr->q_uid == 0) 1374e36b99e2Seric (void) setuid(DefUid); 1375e36b99e2Seric else 13766259796dSeric (void) setuid(ctladdr->q_uid); 1377e36b99e2Seric } 137825a99e2eSeric f = fopen(filename, "a"); 137925a99e2eSeric if (f == NULL) 138032d19d43Seric exit(EX_CANTCREAT); 138125a99e2eSeric 1382c579ef51Seric putmessage(f, Mailer[1], FALSE); 138325a99e2eSeric fputs("\n", f); 1384db8841e9Seric (void) fclose(f); 138532d19d43Seric (void) fflush(stdout); 1386e36b99e2Seric 1387e36b99e2Seric /* reset ISUID & ISGID bits */ 1388c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 138932d19d43Seric exit(EX_OK); 139013bbc08cSeric /*NOTREACHED*/ 139132d19d43Seric } 139232d19d43Seric else 139332d19d43Seric { 139432d19d43Seric /* parent -- wait for exit status */ 139532d19d43Seric register int i; 139632d19d43Seric auto int stat; 139732d19d43Seric 139832d19d43Seric while ((i = wait(&stat)) != pid) 139932d19d43Seric { 140032d19d43Seric if (i < 0) 140132d19d43Seric { 140232d19d43Seric stat = EX_OSERR << 8; 140332d19d43Seric break; 140432d19d43Seric } 140532d19d43Seric } 14060984da9fSeric if ((stat & 0377) != 0) 14070984da9fSeric stat = EX_UNAVAILABLE << 8; 140832d19d43Seric return ((stat >> 8) & 0377); 140932d19d43Seric } 141025a99e2eSeric } 1411ea4dc939Seric /* 1412ea4dc939Seric ** SENDALL -- actually send all the messages. 1413ea4dc939Seric ** 1414ea4dc939Seric ** Parameters: 1415ea4dc939Seric ** verifyonly -- if set, only give verification messages. 1416ea4dc939Seric ** 1417ea4dc939Seric ** Returns: 1418ea4dc939Seric ** none. 1419ea4dc939Seric ** 1420ea4dc939Seric ** Side Effects: 1421ea4dc939Seric ** Scans the send lists and sends everything it finds. 1422ea4dc939Seric */ 1423ea4dc939Seric 1424ea4dc939Seric sendall(verifyonly) 1425ea4dc939Seric bool verifyonly; 1426ea4dc939Seric { 1427e77e673fSeric register ADDRESS *q; 1428ea4dc939Seric typedef int (*fnptr)(); 1429ea4dc939Seric 1430772e6e50Seric # ifdef DEBUG 1431772e6e50Seric if (Debug > 1) 1432772e6e50Seric { 1433772e6e50Seric printf("\nSendQueue:\n"); 1434772e6e50Seric printaddr(SendQueue, TRUE); 1435772e6e50Seric } 1436772e6e50Seric # endif DEBUG 1437ea4dc939Seric 1438e77e673fSeric for (q = SendQueue; q != NULL; q = q->q_next) 1439ea4dc939Seric { 1440ea4dc939Seric if (verifyonly) 1441ea4dc939Seric { 1442ea4dc939Seric To = q->q_paddr; 1443e77e673fSeric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1444ea4dc939Seric { 14457da1035fSeric if (bitset(M_LOCAL, q->q_mailer->m_flags)) 1446ea4dc939Seric message(Arpa_Info, "deliverable"); 1447ea4dc939Seric else 1448ea4dc939Seric message(Arpa_Info, "queueable"); 1449ea4dc939Seric } 1450ea4dc939Seric } 1451ea4dc939Seric else 1452ea4dc939Seric (void) deliver(q, (fnptr) NULL); 1453ea4dc939Seric } 1454ea4dc939Seric } 1455