1294Seric # include <signal.h> 24123Seric # include <errno.h> 34621Seric # include "sendmail.h" 44327Seric # include <sys/stat.h> 5294Seric # ifdef LOG 62774Seric # include <syslog.h> 7294Seric # endif LOG 8294Seric 9*5008Seric static char SccsId[] = "@(#)deliver.c 3.55 11/21/81"; 10405Seric 11294Seric /* 124315Seric ** DELIVER -- Deliver a message to a list of addresses. 13294Seric ** 144315Seric ** This routine delivers to everyone on the same host as the 154315Seric ** user on the head of the list. It is clever about mailers 164315Seric ** that don't handle multiple users. It is NOT guaranteed 174315Seric ** that it will deliver to all these addresses however -- so 184315Seric ** deliver should be called once for each address on the 194315Seric ** list. 204315Seric ** 21294Seric ** Parameters: 224621Seric ** firstto -- head of the address list to deliver to. 23294Seric ** editfcn -- if non-NULL, we want to call this function 24294Seric ** to output the letter (instead of just out- 25294Seric ** putting it raw). 26294Seric ** 27294Seric ** Returns: 28294Seric ** zero -- successfully delivered. 29294Seric ** else -- some failure, see ExitStat for more info. 30294Seric ** 31294Seric ** Side Effects: 32294Seric ** The standard input is passed off to someone. 33294Seric */ 34294Seric 354621Seric deliver(firstto, editfcn) 364621Seric ADDRESS *firstto; 37294Seric int (*editfcn)(); 38294Seric { 394452Seric char *host; /* host being sent to */ 404452Seric char *user; /* user being sent to */ 41294Seric char **pvp; 423233Seric register char **mvp; 433233Seric register char *p; 444452Seric register struct mailer *m; /* mailer for this recipient */ 45294Seric register int i; 462898Seric extern putmessage(); 472968Seric extern bool checkcompat(); 483233Seric char *pv[MAXPV+1]; 494452Seric char tobuf[MAXLINE]; /* text line of to people */ 503233Seric char buf[MAXNAME]; 514397Seric ADDRESS *ctladdr; 524397Seric extern ADDRESS *getctladdr(); 534452Seric char tfrombuf[MAXNAME]; /* translated from person */ 544452Seric extern char **prescan(); 554621Seric register ADDRESS *to = firstto; 564863Seric bool clever = FALSE; /* running user smtp to this mailer */ 574863Seric bool tempfail = FALSE; 58294Seric 594488Seric errno = 0; 60*5008Seric if (!ForceMail && bitset(QDONTSEND, to->q_flags)) 613233Seric return (0); 62294Seric 63294Seric # ifdef DEBUG 64294Seric if (Debug) 653233Seric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 664596Seric to->q_mailer->m_mno, to->q_host, to->q_user); 67294Seric # endif DEBUG 68294Seric 69294Seric /* 703233Seric ** Do initial argv setup. 713233Seric ** Insert the mailer name. Notice that $x expansion is 723233Seric ** NOT done on the mailer name. Then, if the mailer has 733233Seric ** a picky -f flag, we insert it as appropriate. This 743233Seric ** code does not check for 'pv' overflow; this places a 753233Seric ** manifest lower limit of 4 for MAXPV. 762968Seric */ 772968Seric 784596Seric m = to->q_mailer; 793233Seric host = to->q_host; 804452Seric 814452Seric /* rewrite from address, using rewriting rules */ 824452Seric (void) expand(m->m_from, buf, &buf[sizeof buf - 1]); 834452Seric mvp = prescan(buf, '\0'); 844452Seric if (mvp == NULL) 854452Seric { 864452Seric syserr("bad mailer from translate \"%s\"", buf); 874452Seric return (EX_SOFTWARE); 884452Seric } 894452Seric rewrite(mvp, 2); 904452Seric cataddr(mvp, tfrombuf, sizeof tfrombuf); 914452Seric 924452Seric define('g', tfrombuf); /* translated sender address */ 933233Seric define('h', host); /* to host */ 943233Seric Errors = 0; 953233Seric pvp = pv; 963233Seric *pvp++ = m->m_argv[0]; 972968Seric 983233Seric /* insert -f or -r flag as appropriate */ 993233Seric if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag) 1003233Seric { 1013233Seric if (bitset(M_FOPT, m->m_flags)) 1023233Seric *pvp++ = "-f"; 1033233Seric else 1043233Seric *pvp++ = "-r"; 1054082Seric (void) expand("$g", buf, &buf[sizeof buf - 1]); 1063233Seric *pvp++ = newstr(buf); 1073233Seric } 108294Seric 109294Seric /* 1103233Seric ** Append the other fixed parts of the argv. These run 1113233Seric ** up to the first entry containing "$u". There can only 1123233Seric ** be one of these, and there are only a few more slots 1133233Seric ** in the pv after it. 114294Seric */ 115294Seric 1163233Seric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 117294Seric { 1183233Seric while ((p = index(p, '$')) != NULL) 1193233Seric if (*++p == 'u') 1203233Seric break; 1213233Seric if (p != NULL) 1223233Seric break; 1233233Seric 1243233Seric /* this entry is safe -- go ahead and process it */ 1254082Seric (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 1263233Seric *pvp++ = newstr(buf); 1273233Seric if (pvp >= &pv[MAXPV - 3]) 1283233Seric { 1293233Seric syserr("Too many parameters to %s before $u", pv[0]); 1303233Seric return (-1); 1313233Seric } 132294Seric } 1334863Seric 1343233Seric if (*mvp == NULL) 1354863Seric { 1364863Seric /* running SMTP */ 1374863Seric clever = TRUE; 1384863Seric *pvp = NULL; 1394863Seric i = smtpinit(m, pv, (ADDRESS *) NULL); 1404863Seric giveresponse(i, TRUE, m); 1414863Seric if (i == EX_TEMPFAIL) 1424863Seric { 1434863Seric QueueUp = TRUE; 1444863Seric tempfail = TRUE; 1454863Seric } 1464863Seric } 147294Seric 148294Seric /* 1493233Seric ** At this point *mvp points to the argument with $u. We 1503233Seric ** run through our address list and append all the addresses 1513233Seric ** we can. If we run out of space, do not fret! We can 1523233Seric ** always send another copy later. 153294Seric */ 154294Seric 1553233Seric tobuf[0] = '\0'; 1563233Seric To = tobuf; 1574397Seric ctladdr = NULL; 1583233Seric for (; to != NULL; to = to->q_next) 159294Seric { 1603233Seric /* avoid sending multiple recipients to dumb mailers */ 1614382Seric if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags)) 1623233Seric break; 1633233Seric 1643233Seric /* if already sent or not for this host, don't send */ 165*5008Seric if ((!ForceMail && bitset(QDONTSEND, to->q_flags)) || 166*5008Seric strcmp(to->q_host, host) != 0 || to->q_mailer != firstto->q_mailer) 1673233Seric continue; 1684397Seric 1694397Seric /* compute effective uid/gid when sending */ 1704596Seric if (to->q_mailer == ProgMailer) 1714397Seric ctladdr = getctladdr(to); 1724397Seric 1733233Seric user = to->q_user; 1743233Seric To = to->q_paddr; 1753233Seric to->q_flags |= QDONTSEND; 1764863Seric if (tempfail) 1774863Seric to->q_flags |= QQUEUEUP; 1783233Seric # ifdef DEBUG 1793233Seric if (Debug) 1803233Seric printf(" send to `%s'\n", user); 1813233Seric # endif DEBUG 1823233Seric 1833233Seric /* 1843233Seric ** Check to see that these people are allowed to 1853233Seric ** talk to each other. 1863233Seric */ 1873233Seric 1883233Seric if (!checkcompat(to)) 189294Seric { 1903233Seric giveresponse(EX_UNAVAILABLE, TRUE, m); 1913233Seric continue; 192294Seric } 1933233Seric 1943233Seric /* 1954099Seric ** Strip quote bits from names if the mailer is dumb 1964099Seric ** about them. 1973233Seric */ 1983233Seric 1993233Seric if (bitset(M_STRIPQ, m->m_flags)) 200294Seric { 2014099Seric stripquotes(user, TRUE); 2024099Seric stripquotes(host, TRUE); 2033233Seric } 2044099Seric else 2054099Seric { 2064099Seric stripquotes(user, FALSE); 2074099Seric stripquotes(host, FALSE); 2084099Seric } 2093233Seric 2103233Seric /* 2114161Seric ** If an error message has already been given, don't 2124161Seric ** bother to send to this address. 2134161Seric ** 2144161Seric ** >>>>>>>>>> This clause assumes that the local mailer 2154161Seric ** >> NOTE >> cannot do any further aliasing; that 2164161Seric ** >>>>>>>>>> function is subsumed by sendmail. 2174161Seric */ 2184161Seric 2194161Seric if (bitset(QBADADDR, to->q_flags)) 2204161Seric continue; 2214161Seric 2224283Seric /* save statistics.... */ 2234596Seric Stat.stat_nt[to->q_mailer->m_mno]++; 2244596Seric Stat.stat_bt[to->q_mailer->m_mno] += kbytes(MsgSize); 2254283Seric 2264161Seric /* 2273233Seric ** See if this user name is "special". 2283233Seric ** If the user name has a slash in it, assume that this 2293233Seric ** is a file -- send it off without further ado. 2303233Seric ** Note that this means that editfcn's will not 2313233Seric ** be applied to the message. Also note that 2323233Seric ** this type of addresses is not processed along 2333233Seric ** with the others, so we fudge on the To person. 2343233Seric */ 2353233Seric 2364596Seric if (m == LocalMailer) 2373233Seric { 238294Seric if (index(user, '/') != NULL) 239294Seric { 2404397Seric i = mailfile(user, getctladdr(to)); 241294Seric giveresponse(i, TRUE, m); 2423233Seric continue; 243294Seric } 244294Seric } 2453233Seric 2464315Seric /* 2474315Seric ** Address is verified -- add this user to mailer 2484315Seric ** argv, and add it to the print list of recipients. 2494315Seric */ 2504315Seric 2513233Seric /* create list of users for error messages */ 2523233Seric if (tobuf[0] != '\0') 2534082Seric (void) strcat(tobuf, ","); 2544082Seric (void) strcat(tobuf, to->q_paddr); 2553233Seric define('u', user); /* to user */ 2564078Seric define('z', to->q_home); /* user's home */ 2573233Seric 2584863Seric /* 2594863Seric ** Expand out this user into argument list or 2604863Seric ** send it to our SMTP server. 2614863Seric */ 2624863Seric 2634863Seric if (clever) 2643233Seric { 2654975Seric i = smtprcpt(to); 2664863Seric if (i == EX_TEMPFAIL) 2674863Seric { 2684863Seric QueueUp = TRUE; 2694863Seric to->q_flags |= QQUEUEUP; 2704863Seric } 2714863Seric else if (i != EX_OK) 2724863Seric { 2734863Seric to->q_flags |= QBADADDR; 2744863Seric giveresponse(i, TRUE, m); 2754863Seric } 2763233Seric } 2774863Seric else 2784863Seric { 2794863Seric (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 2804863Seric *pvp++ = newstr(buf); 2814863Seric if (pvp >= &pv[MAXPV - 2]) 2824863Seric { 2834863Seric /* allow some space for trailing parms */ 2844863Seric break; 2854863Seric } 2864863Seric } 287294Seric } 288294Seric 2894067Seric /* see if any addresses still exist */ 2904067Seric if (tobuf[0] == '\0') 2914863Seric { 2924863Seric if (clever) 2934863Seric smtpquit(pv[0]); 2944067Seric return (0); 2954863Seric } 2964067Seric 2973233Seric /* print out messages as full list */ 2983233Seric To = tobuf; 2993233Seric 300294Seric /* 3013233Seric ** Fill out any parameters after the $u parameter. 302294Seric */ 303294Seric 3044863Seric while (!clever && *++mvp != NULL) 305294Seric { 3064082Seric (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 3073233Seric *pvp++ = newstr(buf); 3083233Seric if (pvp >= &pv[MAXPV]) 3093233Seric syserr("deliver: pv overflow after $u for %s", pv[0]); 310294Seric } 3113233Seric *pvp++ = NULL; 312294Seric 313294Seric /* 314294Seric ** Call the mailer. 3152898Seric ** The argument vector gets built, pipes 316294Seric ** are created as necessary, and we fork & exec as 3172898Seric ** appropriate. 3184863Seric ** If we are running SMTP, we just need to clean up. 319294Seric */ 320294Seric 3213233Seric if (editfcn == NULL) 3223233Seric editfcn = putmessage; 3234397Seric if (ctladdr == NULL) 3244397Seric ctladdr = &From; 3254863Seric if (clever) 3264863Seric { 3274863Seric i = smtpfinish(m, editfcn); 3284863Seric smtpquit(pv[0]); 3294863Seric } 3304863Seric else 3314863Seric i = sendoff(m, pv, editfcn, ctladdr); 3323233Seric 3334621Seric /* 3344621Seric ** If we got a temporary failure, arrange to queue the 3354621Seric ** addressees. 3364621Seric */ 3374621Seric 3384621Seric if (i == EX_TEMPFAIL) 3394621Seric { 3404621Seric QueueUp = TRUE; 3414621Seric for (to = firstto; to != NULL; to = to->q_next) 3424621Seric { 3434621Seric if (bitset(QBADADDR, to->q_flags)) 3444621Seric continue; 3454621Seric to->q_flags |= QQUEUEUP; 3464621Seric } 3474621Seric } 3484621Seric 3494488Seric errno = 0; 3503233Seric return (i); 3513233Seric } 3523233Seric /* 3534214Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 3544214Seric ** 3554214Seric ** This MUST be a macro, since after a vfork we are running 3564214Seric ** two processes on the same stack!!! 3574214Seric ** 3584214Seric ** Parameters: 3594214Seric ** none. 3604214Seric ** 3614214Seric ** Returns: 3624214Seric ** From a macro??? You've got to be kidding! 3634214Seric ** 3644214Seric ** Side Effects: 3654214Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 3664214Seric ** pid of child in parent, zero in child. 3674214Seric ** -1 on unrecoverable error. 3684214Seric ** 3694214Seric ** Notes: 3704214Seric ** I'm awfully sorry this looks so awful. That's 3714214Seric ** vfork for you..... 3724214Seric */ 3734214Seric 3744214Seric # define NFORKTRIES 5 3754214Seric # ifdef VFORK 3764214Seric # define XFORK vfork 3774214Seric # else VFORK 3784214Seric # define XFORK fork 3794214Seric # endif VFORK 3804214Seric 3814214Seric # define DOFORK(fORKfN) \ 3824214Seric {\ 3834214Seric register int i;\ 3844214Seric \ 3854214Seric for (i = NFORKTRIES; i-- > 0; )\ 3864214Seric {\ 3874214Seric pid = fORKfN();\ 3884214Seric if (pid >= 0)\ 3894214Seric break;\ 3904214Seric sleep((unsigned) NFORKTRIES - i);\ 3914214Seric }\ 3924214Seric } 3934214Seric /* 3944637Seric ** DOFORK -- simple fork interface to DOFORK. 3954637Seric ** 3964637Seric ** Parameters: 3974637Seric ** none. 3984637Seric ** 3994637Seric ** Returns: 4004637Seric ** pid of child in parent. 4014637Seric ** zero in child. 4024637Seric ** -1 on error. 4034637Seric ** 4044637Seric ** Side Effects: 4054637Seric ** returns twice, once in parent and once in child. 4064637Seric */ 4074637Seric 4084637Seric dofork() 4094637Seric { 4104637Seric register int pid; 4114637Seric 4124637Seric DOFORK(fork); 4134637Seric return (pid); 4144637Seric } 4154637Seric /* 4163233Seric ** SENDOFF -- send off call to mailer & collect response. 4173233Seric ** 4183233Seric ** Parameters: 4193233Seric ** m -- mailer descriptor. 4203233Seric ** pvp -- parameter vector to send to it. 4213233Seric ** editfcn -- function to pipe it through. 4224397Seric ** ctladdr -- an address pointer controlling the 4234397Seric ** user/groupid etc. of the mailer. 4243233Seric ** 4253233Seric ** Returns: 4263233Seric ** exit status of mailer. 4273233Seric ** 4283233Seric ** Side Effects: 4293233Seric ** none. 4303233Seric */ 4313233Seric 4324397Seric sendoff(m, pvp, editfcn, ctladdr) 4333233Seric struct mailer *m; 4343233Seric char **pvp; 4353233Seric int (*editfcn)(); 4364397Seric ADDRESS *ctladdr; 4373233Seric { 4384863Seric auto FILE *mfile; 4394863Seric auto FILE *rfile; 4403233Seric register int i; 4414863Seric extern putmessage(); 4423233Seric int pid; 4434863Seric 4444863Seric /* 4454863Seric ** Create connection to mailer. 4464863Seric */ 4474863Seric 4484863Seric pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 4494863Seric if (pid < 0) 4504863Seric return (-1); 4514863Seric 4524863Seric /* 4534863Seric ** Format and send message. 4544863Seric */ 4554863Seric 4564863Seric (void) signal(SIGPIPE, SIG_IGN); 4574863Seric if (editfcn == NULL) 4584863Seric editfcn = putmessage; 4594863Seric 4604863Seric (*editfcn)(mfile, m, FALSE); 4614863Seric (void) fclose(mfile); 4624863Seric 4634863Seric i = endmailer(pid, pvp[0]); 4644863Seric giveresponse(i, TRUE, m); 4654863Seric return (i); 4664863Seric } 4674863Seric /* 4684863Seric ** ENDMAILER -- Wait for mailer to terminate. 4694863Seric ** 4704863Seric ** We should never get fatal errors (e.g., segmentation 4714863Seric ** violation), so we report those specially. For other 4724863Seric ** errors, we choose a status message (into statmsg), 4734863Seric ** and if it represents an error, we print it. 4744863Seric ** 4754863Seric ** Parameters: 4764863Seric ** pid -- pid of mailer. 4774863Seric ** name -- name of mailer (for error messages). 4784863Seric ** 4794863Seric ** Returns: 4804863Seric ** exit code of mailer. 4814863Seric ** 4824863Seric ** Side Effects: 4834863Seric ** none. 4844863Seric */ 4854863Seric 4864863Seric endmailer(pid, name) 4874863Seric int pid; 4884863Seric char *name; 4894863Seric { 4904863Seric register int i; 4914863Seric auto int st; 4924863Seric 4934863Seric while ((i = wait(&st)) > 0 && i != pid) 4944863Seric continue; 4954863Seric if (i < 0) 4964863Seric { 4974863Seric syserr("wait"); 4984863Seric return (-1); 4994863Seric } 5004863Seric if ((st & 0377) != 0) 5014863Seric { 5024863Seric syserr("%s: stat %o", name, st); 5034863Seric ExitStat = EX_UNAVAILABLE; 5044863Seric return (-1); 5054863Seric } 5064863Seric i = (st >> 8) & 0377; 5074863Seric return (i); 5084863Seric } 5094863Seric /* 5104863Seric ** OPENMAILER -- open connection to mailer. 5114863Seric ** 5124863Seric ** Parameters: 5134863Seric ** m -- mailer descriptor. 5144863Seric ** pvp -- parameter vector to pass to mailer. 5154863Seric ** ctladdr -- controlling address for user. 5164863Seric ** clever -- create a full duplex connection. 5174863Seric ** pmfile -- pointer to mfile (to mailer) connection. 5184863Seric ** prfile -- pointer to rfile (from mailer) connection. 5194863Seric ** 5204863Seric ** Returns: 5214863Seric ** pid of mailer. 5224863Seric ** -1 on error. 5234863Seric ** 5244863Seric ** Side Effects: 5254863Seric ** creates a mailer in a subprocess. 5264863Seric */ 5274863Seric 5284863Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 5294863Seric struct mailer *m; 5304863Seric char **pvp; 5314863Seric ADDRESS *ctladdr; 5324863Seric bool clever; 5334863Seric FILE **pmfile; 5344863Seric FILE **prfile; 5354863Seric { 5364863Seric int pid; 5374709Seric int mpvect[2]; 5384863Seric int rpvect[2]; 5393233Seric FILE *mfile; 5404863Seric FILE *rfile; 5413233Seric extern FILE *fdopen(); 5423233Seric 5433233Seric # ifdef DEBUG 5443233Seric if (Debug) 545294Seric { 5464863Seric printf("openmailer:\n"); 5473233Seric printav(pvp); 548294Seric } 5493233Seric # endif DEBUG 5504488Seric errno = 0; 5513233Seric 5522898Seric /* create a pipe to shove the mail through */ 5534709Seric if (pipe(mpvect) < 0) 554294Seric { 5554863Seric syserr("pipe (to mailer)"); 556294Seric return (-1); 557294Seric } 5584863Seric 5594863Seric /* if this mailer speaks smtp, create a return pipe */ 5604863Seric if (clever && pipe(rpvect) < 0) 5614863Seric { 5624863Seric syserr("pipe (from mailer)"); 5634863Seric (void) close(mpvect[0]); 5644863Seric (void) close(mpvect[1]); 5654863Seric return (-1); 5664863Seric } 5674863Seric 5684214Seric DOFORK(XFORK); 5694327Seric /* pid is set by DOFORK */ 570294Seric if (pid < 0) 571294Seric { 572294Seric syserr("Cannot fork"); 5734709Seric (void) close(mpvect[0]); 5744709Seric (void) close(mpvect[1]); 5754863Seric if (clever) 5764863Seric { 5774863Seric (void) close(rpvect[0]); 5784863Seric (void) close(rpvect[1]); 5794863Seric } 580294Seric return (-1); 581294Seric } 582294Seric else if (pid == 0) 583294Seric { 584294Seric /* child -- set up input & exec mailer */ 5851621Seric /* make diagnostic output be standard output */ 5864477Seric (void) signal(SIGINT, SIG_IGN); 5874477Seric (void) signal(SIGHUP, SIG_IGN); 5884215Seric (void) signal(SIGTERM, SIG_DFL); 5894709Seric 5904709Seric /* arrange to filter standard & diag output of command */ 5914863Seric if (clever) 5924709Seric { 5934863Seric (void) close(rpvect[0]); 5944709Seric (void) close(1); 5954863Seric (void) dup(rpvect[1]); 5964863Seric (void) close(rpvect[1]); 5974863Seric } 5984863Seric else if (OutChannel != stdout) 5994863Seric { 6004863Seric (void) close(1); 6014709Seric (void) dup(fileno(OutChannel)); 6024709Seric } 6034082Seric (void) close(2); 6044082Seric (void) dup(1); 6054709Seric 6064709Seric /* arrange to get standard input */ 6074709Seric (void) close(mpvect[1]); 6084082Seric (void) close(0); 6094709Seric if (dup(mpvect[0]) < 0) 610294Seric { 6112898Seric syserr("Cannot dup to zero!"); 6122898Seric _exit(EX_OSERR); 613294Seric } 6144709Seric (void) close(mpvect[0]); 6152968Seric if (!bitset(M_RESTR, m->m_flags)) 6164215Seric { 6174417Seric if (ctladdr->q_uid == 0) 6184417Seric { 6194417Seric extern int DefUid, DefGid; 6204415Seric 6214417Seric (void) setgid(DefGid); 6224417Seric (void) setuid(DefUid); 6234417Seric } 6244417Seric else 6254415Seric { 6264417Seric (void) setgid(ctladdr->q_gid); 6274417Seric (void) setuid(ctladdr->q_uid); 6284415Seric } 6294215Seric } 6302774Seric # ifndef VFORK 6312774Seric /* 6322774Seric ** We have to be careful with vfork - we can't mung up the 6332774Seric ** memory but we don't want the mailer to inherit any extra 6342774Seric ** open files. Chances are the mailer won't 6352774Seric ** care about an extra file, but then again you never know. 6362774Seric ** Actually, we would like to close(fileno(pwf)), but it's 6372774Seric ** declared static so we can't. But if we fclose(pwf), which 6382774Seric ** is what endpwent does, it closes it in the parent too and 6392774Seric ** the next getpwnam will be slower. If you have a weird 6402774Seric ** mailer that chokes on the extra file you should do the 6412774Seric ** endpwent(). 6422774Seric ** 6432774Seric ** Similar comments apply to log. However, openlog is 6442774Seric ** clever enough to set the FIOCLEX mode on the file, 6452774Seric ** so it will be closed automatically on the exec. 6462774Seric */ 6472774Seric 6482774Seric endpwent(); 649294Seric # ifdef LOG 6502089Seric closelog(); 651294Seric # endif LOG 6522774Seric # endif VFORK 653294Seric execv(m->m_mailer, pvp); 654294Seric /* syserr fails because log is closed */ 655294Seric /* syserr("Cannot exec %s", m->m_mailer); */ 6564214Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 6574082Seric (void) fflush(stdout); 6581619Seric _exit(EX_UNAVAILABLE); 659294Seric } 660294Seric 6614709Seric /* 6624863Seric ** Set up return value. 6634709Seric */ 6644709Seric 6654709Seric (void) close(mpvect[0]); 6664709Seric mfile = fdopen(mpvect[1], "w"); 6674863Seric if (clever) 6684863Seric { 6694863Seric (void) close(rpvect[1]); 6704863Seric rfile = fdopen(rpvect[0], "r"); 6714863Seric } 672294Seric 6734863Seric *pmfile = mfile; 6744863Seric *prfile = rfile; 675294Seric 6764863Seric return (pid); 677294Seric } 678294Seric /* 679294Seric ** GIVERESPONSE -- Interpret an error response from a mailer 680294Seric ** 681294Seric ** Parameters: 682294Seric ** stat -- the status code from the mailer (high byte 683294Seric ** only; core dumps must have been taken care of 684294Seric ** already). 685294Seric ** force -- if set, force an error message output, even 686294Seric ** if the mailer seems to like to print its own 687294Seric ** messages. 688294Seric ** m -- the mailer descriptor for this mailer. 689294Seric ** 690294Seric ** Returns: 6914082Seric ** none. 692294Seric ** 693294Seric ** Side Effects: 6941518Seric ** Errors may be incremented. 695294Seric ** ExitStat may be set. 696294Seric */ 697294Seric 698294Seric giveresponse(stat, force, m) 699294Seric int stat; 700294Seric int force; 701294Seric register struct mailer *m; 702294Seric { 703294Seric register char *statmsg; 704294Seric extern char *SysExMsg[]; 705294Seric register int i; 706294Seric extern int N_SysEx; 7071624Seric char buf[30]; 708294Seric 7094315Seric /* 7104315Seric ** Compute status message from code. 7114315Seric */ 7124315Seric 713294Seric i = stat - EX__BASE; 714294Seric if (i < 0 || i > N_SysEx) 715294Seric statmsg = NULL; 716294Seric else 717294Seric statmsg = SysExMsg[i]; 718294Seric if (stat == 0) 7194065Seric { 7204194Seric if (bitset(M_LOCAL, m->m_flags)) 7214161Seric statmsg = "delivered"; 7224161Seric else 7234161Seric statmsg = "queued"; 7244065Seric if (Verbose) 7254166Seric message(Arpa_Info, statmsg); 7264065Seric } 7274621Seric else if (stat == EX_TEMPFAIL) 7284621Seric { 7294621Seric if (Verbose) 7304621Seric message(Arpa_Info, "transmission deferred"); 7314621Seric } 732294Seric else 733294Seric { 7341518Seric Errors++; 735294Seric if (statmsg == NULL && m->m_badstat != 0) 736294Seric { 737294Seric stat = m->m_badstat; 738294Seric i = stat - EX__BASE; 739294Seric # ifdef DEBUG 740294Seric if (i < 0 || i >= N_SysEx) 741294Seric syserr("Bad m_badstat %d", stat); 742294Seric else 743294Seric # endif DEBUG 744294Seric statmsg = SysExMsg[i]; 745294Seric } 746294Seric if (statmsg == NULL) 747294Seric usrerr("unknown mailer response %d", stat); 7484065Seric else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 749294Seric usrerr("%s", statmsg); 750294Seric } 751294Seric 752294Seric /* 753294Seric ** Final cleanup. 754294Seric ** Log a record of the transaction. Compute the new 755294Seric ** ExitStat -- if we already had an error, stick with 756294Seric ** that. 757294Seric */ 758294Seric 7591624Seric if (statmsg == NULL) 7601624Seric { 7614082Seric (void) sprintf(buf, "error %d", stat); 7621624Seric statmsg = buf; 7631624Seric } 7641624Seric 765294Seric # ifdef LOG 7662774Seric syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg); 767294Seric # endif LOG 7684621Seric if (stat != EX_TEMPFAIL) 7694621Seric setstat(stat); 770294Seric } 771294Seric /* 7722898Seric ** PUTMESSAGE -- output a message to the final mailer. 773294Seric ** 7742898Seric ** This routine takes care of recreating the header from the 7752898Seric ** in-core copy, etc. 776294Seric ** 777294Seric ** Parameters: 7782898Seric ** fp -- file to output onto. 7792898Seric ** m -- a mailer descriptor. 7804863Seric ** xdot -- if set, hide lines beginning with dot. 781294Seric ** 782294Seric ** Returns: 7832898Seric ** none. 784294Seric ** 785294Seric ** Side Effects: 7862898Seric ** The message is written onto fp. 787294Seric */ 788294Seric 7894863Seric putmessage(fp, m, xdot) 7902898Seric FILE *fp; 7912898Seric struct mailer *m; 7924863Seric bool xdot; 793294Seric { 7942898Seric char buf[BUFSIZ]; 7954315Seric register HDR *h; 7962898Seric extern char *arpadate(); 7972898Seric bool anyheader = FALSE; 7983044Seric extern char *capitalize(); 7994370Seric extern char *hvalue(); 8004370Seric extern bool samefrom(); 8014447Seric char *of_line; 802294Seric 8034315Seric /* 8044315Seric ** Output "From" line unless supressed 8054315Seric */ 8064315Seric 8073186Seric if (!bitset(M_NHDR, m->m_flags)) 8084205Seric { 8094205Seric (void) expand("$l", buf, &buf[sizeof buf - 1]); 8104205Seric fprintf(fp, "%s\n", buf); 8114205Seric } 8123186Seric 8134315Seric /* 8144315Seric ** Output all header lines 8154315Seric */ 8164315Seric 8174447Seric of_line = hvalue("original-from"); 8182898Seric for (h = Header; h != NULL; h = h->h_link) 8191828Seric { 8204315Seric register char *p; 8214370Seric char *origfrom = OrigFrom; 8224447Seric bool nooutput; 8234315Seric 8244447Seric nooutput = FALSE; 8253389Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 8264209Seric { 8274209Seric p = ")><("; /* can't happen (I hope) */ 8284447Seric nooutput = TRUE; 8294209Seric } 8304447Seric 8314447Seric /* use From: line from message if generated is the same */ 8324370Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL && 8334447Seric strcmp(m->m_from, "$f") == 0 && of_line == NULL) 8343385Seric { 8354370Seric p = origfrom; 8364370Seric origfrom = NULL; 8374370Seric } 8384370Seric else if (bitset(H_DEFAULT, h->h_flags)) 8394370Seric { 8404082Seric (void) expand(h->h_value, buf, &buf[sizeof buf]); 8413385Seric p = buf; 8423385Seric } 8432898Seric else 8443385Seric p = h->h_value; 8454447Seric if (p == NULL || *p == '\0') 8463389Seric continue; 8474209Seric 8484209Seric /* hack, hack -- output Original-From field if different */ 8494447Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL) 8504209Seric { 8514447Seric /* output new Original-From line if needed */ 8524447Seric if (of_line == NULL && !samefrom(p, origfrom)) 8534447Seric { 8544447Seric fprintf(fp, "Original-From: %s\n", origfrom); 8554447Seric anyheader = TRUE; 8564447Seric } 8574447Seric if (of_line != NULL && !nooutput && samefrom(p, of_line)) 8584447Seric { 8594447Seric /* delete Original-From: line if redundant */ 8604447Seric p = of_line; 8614447Seric of_line = NULL; 8624447Seric } 8634447Seric } 8644447Seric else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL) 8654447Seric nooutput = TRUE; 8664447Seric 8674447Seric /* finally, output the header line */ 8684447Seric if (!nooutput) 8694447Seric { 8704447Seric fprintf(fp, "%s: %s\n", capitalize(h->h_field), p); 8714447Seric h->h_flags |= H_USED; 8724370Seric anyheader = TRUE; 8734209Seric } 8742898Seric } 8752898Seric if (anyheader) 8762898Seric fprintf(fp, "\n"); 8772898Seric 8784315Seric /* 8794315Seric ** Output the body of the message 8804315Seric */ 8814315Seric 8824452Seric if (TempFile != NULL) 8834452Seric { 8844452Seric rewind(TempFile); 8854863Seric while (!ferror(fp) && fgets(buf, sizeof buf, TempFile) != NULL) 8864863Seric fprintf(fp, "%s%s", xdot && buf[0] == '.' ? "." : "", buf); 8872898Seric 8884452Seric if (ferror(TempFile)) 8894452Seric { 8904452Seric syserr("putmessage: read error"); 8914452Seric setstat(EX_IOERR); 8924452Seric } 8934452Seric } 8944452Seric 8954621Seric (void) fflush(fp); 8964123Seric if (ferror(fp) && errno != EPIPE) 897294Seric { 8982898Seric syserr("putmessage: write error"); 899294Seric setstat(EX_IOERR); 900294Seric } 9014123Seric errno = 0; 902294Seric } 903294Seric /* 9044370Seric ** SAMEFROM -- tell if two text addresses represent the same from address. 9054370Seric ** 9064370Seric ** Parameters: 9074370Seric ** ifrom -- internally generated form of from address. 9084370Seric ** efrom -- external form of from address. 9094370Seric ** 9104370Seric ** Returns: 9114370Seric ** TRUE -- if they convey the same info. 9124370Seric ** FALSE -- if any information has been lost. 9134370Seric ** 9144370Seric ** Side Effects: 9154370Seric ** none. 9164370Seric */ 9174370Seric 9184370Seric bool 9194370Seric samefrom(ifrom, efrom) 9204370Seric char *ifrom; 9214370Seric char *efrom; 9224370Seric { 9234447Seric register char *p; 9244447Seric char buf[MAXNAME + 4]; 9254447Seric 9264447Seric # ifdef DEBUG 9274447Seric if (Debug > 7) 9284447Seric printf("samefrom(%s,%s)-->", ifrom, efrom); 9294447Seric # endif DEBUG 9304447Seric if (strcmp(ifrom, efrom) == 0) 9314447Seric goto success; 9324447Seric p = index(ifrom, '@'); 9334447Seric if (p == NULL) 9344447Seric goto failure; 9354447Seric *p = '\0'; 9364447Seric strcpy(buf, ifrom); 9374447Seric strcat(buf, " at "); 9384447Seric *p++ = '@'; 9394447Seric strcat(buf, p); 9404447Seric if (strcmp(buf, efrom) == 0) 9414447Seric goto success; 9424447Seric 9434447Seric failure: 9444447Seric # ifdef DEBUG 9454447Seric if (Debug > 7) 9464447Seric printf("FALSE\n"); 9474447Seric # endif DEBUG 9484447Seric return (FALSE); 9494447Seric 9504447Seric success: 9514447Seric # ifdef DEBUG 9524447Seric if (Debug > 7) 9534447Seric printf("TRUE\n"); 9544447Seric # endif DEBUG 9554447Seric return (TRUE); 9564370Seric } 9574370Seric /* 958294Seric ** MAILFILE -- Send a message to a file. 959294Seric ** 9604327Seric ** If the file has the setuid/setgid bits set, but NO execute 9614327Seric ** bits, sendmail will try to become the owner of that file 9624327Seric ** rather than the real user. Obviously, this only works if 9634327Seric ** sendmail runs as root. 9644327Seric ** 965294Seric ** Parameters: 966294Seric ** filename -- the name of the file to send to. 9674397Seric ** ctladdr -- the controlling address header -- includes 9684397Seric ** the userid/groupid to be when sending. 969294Seric ** 970294Seric ** Returns: 971294Seric ** The exit code associated with the operation. 972294Seric ** 973294Seric ** Side Effects: 974294Seric ** none. 975294Seric */ 976294Seric 9774397Seric mailfile(filename, ctladdr) 978294Seric char *filename; 9794397Seric ADDRESS *ctladdr; 980294Seric { 981294Seric register FILE *f; 9824214Seric register int pid; 983294Seric 9844214Seric /* 9854214Seric ** Fork so we can change permissions here. 9864214Seric ** Note that we MUST use fork, not vfork, because of 9874214Seric ** the complications of calling subroutines, etc. 9884214Seric */ 9894067Seric 9904214Seric DOFORK(fork); 9914214Seric 9924214Seric if (pid < 0) 9934214Seric return (EX_OSERR); 9944214Seric else if (pid == 0) 9954214Seric { 9964214Seric /* child -- actually write to file */ 9974327Seric struct stat stb; 9984417Seric extern int DefUid, DefGid; 9994327Seric 10004215Seric (void) signal(SIGINT, SIG_DFL); 10014215Seric (void) signal(SIGHUP, SIG_DFL); 10024215Seric (void) signal(SIGTERM, SIG_DFL); 10034327Seric umask(OldUmask); 10044327Seric if (stat(filename, &stb) < 0) 10054431Seric stb.st_mode = 0666; 10064327Seric if (bitset(0111, stb.st_mode)) 10074327Seric exit(EX_CANTCREAT); 10084401Seric if (ctladdr == NULL) 10094401Seric ctladdr = &From; 10104327Seric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 10114417Seric { 10124417Seric if (ctladdr->q_uid == 0) 10134417Seric (void) setgid(DefGid); 10144417Seric else 10154417Seric (void) setgid(ctladdr->q_gid); 10164417Seric } 10174327Seric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 10184417Seric { 10194417Seric if (ctladdr->q_uid == 0) 10204417Seric (void) setuid(DefUid); 10214417Seric else 10224417Seric (void) setuid(ctladdr->q_uid); 10234417Seric } 10244214Seric f = fopen(filename, "a"); 10254214Seric if (f == NULL) 10264214Seric exit(EX_CANTCREAT); 10274214Seric 10284863Seric putmessage(f, Mailer[1], FALSE); 10294214Seric fputs("\n", f); 10304214Seric (void) fclose(f); 10314214Seric (void) fflush(stdout); 10324417Seric 10334417Seric /* reset ISUID & ISGID bits */ 10344621Seric (void) chmod(filename, (int) stb.st_mode); 10354214Seric exit(EX_OK); 10364315Seric /*NOTREACHED*/ 10374214Seric } 10384214Seric else 10394214Seric { 10404214Seric /* parent -- wait for exit status */ 10414214Seric register int i; 10424214Seric auto int stat; 10434214Seric 10444214Seric while ((i = wait(&stat)) != pid) 10454214Seric { 10464214Seric if (i < 0) 10474214Seric { 10484214Seric stat = EX_OSERR << 8; 10494214Seric break; 10504214Seric } 10514214Seric } 10524215Seric if ((stat & 0377) != 0) 10534215Seric stat = EX_UNAVAILABLE << 8; 10544214Seric return ((stat >> 8) & 0377); 10554214Seric } 1056294Seric } 10574550Seric /* 10584550Seric ** SENDALL -- actually send all the messages. 10594550Seric ** 10604550Seric ** Parameters: 10614550Seric ** verifyonly -- if set, only give verification messages. 10624550Seric ** 10634550Seric ** Returns: 10644550Seric ** none. 10654550Seric ** 10664550Seric ** Side Effects: 10674550Seric ** Scans the send lists and sends everything it finds. 10684550Seric */ 10694550Seric 10704550Seric sendall(verifyonly) 10714550Seric bool verifyonly; 10724550Seric { 1073*5008Seric register ADDRESS *q; 10744550Seric typedef int (*fnptr)(); 10754550Seric 1076*5008Seric 1077*5008Seric for (q = SendQueue; q != NULL; q = q->q_next) 10784550Seric { 1079*5008Seric if (verifyonly) 10804550Seric { 1081*5008Seric To = q->q_paddr; 1082*5008Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 10834550Seric { 1084*5008Seric if (bitset(M_LOCAL, q->q_mailer->m_flags)) 1085*5008Seric message(Arpa_Info, "deliverable"); 1086*5008Seric else 1087*5008Seric message(Arpa_Info, "queueable"); 10884550Seric } 10894550Seric } 1090*5008Seric else 1091*5008Seric (void) deliver(q, (fnptr) NULL); 10924550Seric } 10934550Seric } 1094