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*4709Seric static char SccsId[] = "@(#)deliver.c 3.52 10/31/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; 56294Seric 574488Seric errno = 0; 584311Seric if (!ForceMail && bitset(QDONTSEND, to->q_flags)) 593233Seric return (0); 60294Seric 61294Seric # ifdef DEBUG 62294Seric if (Debug) 633233Seric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 644596Seric to->q_mailer->m_mno, to->q_host, to->q_user); 65294Seric # endif DEBUG 66294Seric 67294Seric /* 683233Seric ** Do initial argv setup. 693233Seric ** Insert the mailer name. Notice that $x expansion is 703233Seric ** NOT done on the mailer name. Then, if the mailer has 713233Seric ** a picky -f flag, we insert it as appropriate. This 723233Seric ** code does not check for 'pv' overflow; this places a 733233Seric ** manifest lower limit of 4 for MAXPV. 742968Seric */ 752968Seric 764596Seric m = to->q_mailer; 773233Seric host = to->q_host; 784452Seric 794452Seric /* rewrite from address, using rewriting rules */ 804452Seric (void) expand(m->m_from, buf, &buf[sizeof buf - 1]); 814452Seric mvp = prescan(buf, '\0'); 824452Seric if (mvp == NULL) 834452Seric { 844452Seric syserr("bad mailer from translate \"%s\"", buf); 854452Seric return (EX_SOFTWARE); 864452Seric } 874452Seric rewrite(mvp, 2); 884452Seric cataddr(mvp, tfrombuf, sizeof tfrombuf); 894452Seric 904452Seric define('g', tfrombuf); /* translated sender address */ 913233Seric define('h', host); /* to host */ 923233Seric Errors = 0; 933233Seric pvp = pv; 943233Seric *pvp++ = m->m_argv[0]; 952968Seric 963233Seric /* insert -f or -r flag as appropriate */ 973233Seric if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag) 983233Seric { 993233Seric if (bitset(M_FOPT, m->m_flags)) 1003233Seric *pvp++ = "-f"; 1013233Seric else 1023233Seric *pvp++ = "-r"; 1034082Seric (void) expand("$g", buf, &buf[sizeof buf - 1]); 1043233Seric *pvp++ = newstr(buf); 1053233Seric } 106294Seric 107294Seric /* 1083233Seric ** Append the other fixed parts of the argv. These run 1093233Seric ** up to the first entry containing "$u". There can only 1103233Seric ** be one of these, and there are only a few more slots 1113233Seric ** in the pv after it. 112294Seric */ 113294Seric 1143233Seric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 115294Seric { 1163233Seric while ((p = index(p, '$')) != NULL) 1173233Seric if (*++p == 'u') 1183233Seric break; 1193233Seric if (p != NULL) 1203233Seric break; 1213233Seric 1223233Seric /* this entry is safe -- go ahead and process it */ 1234082Seric (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 1243233Seric *pvp++ = newstr(buf); 1253233Seric if (pvp >= &pv[MAXPV - 3]) 1263233Seric { 1273233Seric syserr("Too many parameters to %s before $u", pv[0]); 1283233Seric return (-1); 1293233Seric } 130294Seric } 1313233Seric if (*mvp == NULL) 1323233Seric syserr("No $u in mailer argv for %s", pv[0]); 133294Seric 134294Seric /* 1353233Seric ** At this point *mvp points to the argument with $u. We 1363233Seric ** run through our address list and append all the addresses 1373233Seric ** we can. If we run out of space, do not fret! We can 1383233Seric ** always send another copy later. 139294Seric */ 140294Seric 1413233Seric tobuf[0] = '\0'; 1423233Seric To = tobuf; 1434397Seric ctladdr = NULL; 1443233Seric for (; to != NULL; to = to->q_next) 145294Seric { 1463233Seric /* avoid sending multiple recipients to dumb mailers */ 1474382Seric if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags)) 1483233Seric break; 1493233Seric 1503233Seric /* if already sent or not for this host, don't send */ 1514311Seric if ((!ForceMail && bitset(QDONTSEND, to->q_flags)) || 1524311Seric strcmp(to->q_host, host) != 0) 1533233Seric continue; 1544397Seric 1554397Seric /* compute effective uid/gid when sending */ 1564596Seric if (to->q_mailer == ProgMailer) 1574397Seric ctladdr = getctladdr(to); 1584397Seric 1593233Seric user = to->q_user; 1603233Seric To = to->q_paddr; 1613233Seric to->q_flags |= QDONTSEND; 1623233Seric # ifdef DEBUG 1633233Seric if (Debug) 1643233Seric printf(" send to `%s'\n", user); 1653233Seric # endif DEBUG 1663233Seric 1673233Seric /* 1683233Seric ** Check to see that these people are allowed to 1693233Seric ** talk to each other. 1703233Seric */ 1713233Seric 1723233Seric if (!checkcompat(to)) 173294Seric { 1743233Seric giveresponse(EX_UNAVAILABLE, TRUE, m); 1753233Seric continue; 176294Seric } 1773233Seric 1783233Seric /* 1794099Seric ** Strip quote bits from names if the mailer is dumb 1804099Seric ** about them. 1813233Seric */ 1823233Seric 1833233Seric if (bitset(M_STRIPQ, m->m_flags)) 184294Seric { 1854099Seric stripquotes(user, TRUE); 1864099Seric stripquotes(host, TRUE); 1873233Seric } 1884099Seric else 1894099Seric { 1904099Seric stripquotes(user, FALSE); 1914099Seric stripquotes(host, FALSE); 1924099Seric } 1933233Seric 1943233Seric /* 1954161Seric ** If an error message has already been given, don't 1964161Seric ** bother to send to this address. 1974161Seric ** 1984161Seric ** >>>>>>>>>> This clause assumes that the local mailer 1994161Seric ** >> NOTE >> cannot do any further aliasing; that 2004161Seric ** >>>>>>>>>> function is subsumed by sendmail. 2014161Seric */ 2024161Seric 2034161Seric if (bitset(QBADADDR, to->q_flags)) 2044161Seric continue; 2054161Seric 2064283Seric /* save statistics.... */ 2074596Seric Stat.stat_nt[to->q_mailer->m_mno]++; 2084596Seric Stat.stat_bt[to->q_mailer->m_mno] += kbytes(MsgSize); 2094283Seric 2104161Seric /* 2113233Seric ** See if this user name is "special". 2123233Seric ** If the user name has a slash in it, assume that this 2133233Seric ** is a file -- send it off without further ado. 2143233Seric ** Note that this means that editfcn's will not 2153233Seric ** be applied to the message. Also note that 2163233Seric ** this type of addresses is not processed along 2173233Seric ** with the others, so we fudge on the To person. 2183233Seric */ 2193233Seric 2204596Seric if (m == LocalMailer) 2213233Seric { 222294Seric if (index(user, '/') != NULL) 223294Seric { 2244397Seric i = mailfile(user, getctladdr(to)); 225294Seric giveresponse(i, TRUE, m); 2263233Seric continue; 227294Seric } 228294Seric } 2293233Seric 2304315Seric /* 2314315Seric ** Address is verified -- add this user to mailer 2324315Seric ** argv, and add it to the print list of recipients. 2334315Seric */ 2344315Seric 2353233Seric /* create list of users for error messages */ 2363233Seric if (tobuf[0] != '\0') 2374082Seric (void) strcat(tobuf, ","); 2384082Seric (void) strcat(tobuf, to->q_paddr); 2393233Seric define('u', user); /* to user */ 2404078Seric define('z', to->q_home); /* user's home */ 2413233Seric 2423233Seric /* expand out this user */ 2434305Seric (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 2443233Seric *pvp++ = newstr(buf); 2453233Seric if (pvp >= &pv[MAXPV - 2]) 2463233Seric { 2473233Seric /* allow some space for trailing parms */ 2483233Seric break; 2493233Seric } 250294Seric } 251294Seric 2524067Seric /* see if any addresses still exist */ 2534067Seric if (tobuf[0] == '\0') 2544067Seric return (0); 2554067Seric 2563233Seric /* print out messages as full list */ 2573233Seric To = tobuf; 2583233Seric 259294Seric /* 2603233Seric ** Fill out any parameters after the $u parameter. 261294Seric */ 262294Seric 2633233Seric while (*++mvp != NULL) 264294Seric { 2654082Seric (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 2663233Seric *pvp++ = newstr(buf); 2673233Seric if (pvp >= &pv[MAXPV]) 2683233Seric syserr("deliver: pv overflow after $u for %s", pv[0]); 269294Seric } 2703233Seric *pvp++ = NULL; 271294Seric 272294Seric /* 273294Seric ** Call the mailer. 2742898Seric ** The argument vector gets built, pipes 275294Seric ** are created as necessary, and we fork & exec as 2762898Seric ** appropriate. 277294Seric */ 278294Seric 2793233Seric if (editfcn == NULL) 2803233Seric editfcn = putmessage; 2814397Seric if (ctladdr == NULL) 2824397Seric ctladdr = &From; 2834397Seric i = sendoff(m, pv, editfcn, ctladdr); 2843233Seric 2854621Seric /* 2864621Seric ** If we got a temporary failure, arrange to queue the 2874621Seric ** addressees. 2884621Seric */ 2894621Seric 2904621Seric if (i == EX_TEMPFAIL) 2914621Seric { 2924621Seric QueueUp = TRUE; 2934621Seric for (to = firstto; to != NULL; to = to->q_next) 2944621Seric { 2954621Seric if (bitset(QBADADDR, to->q_flags)) 2964621Seric continue; 2974621Seric to->q_flags |= QQUEUEUP; 2984621Seric } 2994621Seric } 3004621Seric 3014488Seric errno = 0; 3023233Seric return (i); 3033233Seric } 3043233Seric /* 3054214Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 3064214Seric ** 3074214Seric ** This MUST be a macro, since after a vfork we are running 3084214Seric ** two processes on the same stack!!! 3094214Seric ** 3104214Seric ** Parameters: 3114214Seric ** none. 3124214Seric ** 3134214Seric ** Returns: 3144214Seric ** From a macro??? You've got to be kidding! 3154214Seric ** 3164214Seric ** Side Effects: 3174214Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 3184214Seric ** pid of child in parent, zero in child. 3194214Seric ** -1 on unrecoverable error. 3204214Seric ** 3214214Seric ** Notes: 3224214Seric ** I'm awfully sorry this looks so awful. That's 3234214Seric ** vfork for you..... 3244214Seric */ 3254214Seric 3264214Seric # define NFORKTRIES 5 3274214Seric # ifdef VFORK 3284214Seric # define XFORK vfork 3294214Seric # else VFORK 3304214Seric # define XFORK fork 3314214Seric # endif VFORK 3324214Seric 3334214Seric # define DOFORK(fORKfN) \ 3344214Seric {\ 3354214Seric register int i;\ 3364214Seric \ 3374214Seric for (i = NFORKTRIES; i-- > 0; )\ 3384214Seric {\ 3394214Seric pid = fORKfN();\ 3404214Seric if (pid >= 0)\ 3414214Seric break;\ 3424214Seric sleep((unsigned) NFORKTRIES - i);\ 3434214Seric }\ 3444214Seric } 3454214Seric /* 3464637Seric ** DOFORK -- simple fork interface to DOFORK. 3474637Seric ** 3484637Seric ** Parameters: 3494637Seric ** none. 3504637Seric ** 3514637Seric ** Returns: 3524637Seric ** pid of child in parent. 3534637Seric ** zero in child. 3544637Seric ** -1 on error. 3554637Seric ** 3564637Seric ** Side Effects: 3574637Seric ** returns twice, once in parent and once in child. 3584637Seric */ 3594637Seric 3604637Seric dofork() 3614637Seric { 3624637Seric register int pid; 3634637Seric 3644637Seric DOFORK(fork); 3654637Seric return (pid); 3664637Seric } 3674637Seric /* 3683233Seric ** SENDOFF -- send off call to mailer & collect response. 3693233Seric ** 3703233Seric ** Parameters: 3713233Seric ** m -- mailer descriptor. 3723233Seric ** pvp -- parameter vector to send to it. 3733233Seric ** editfcn -- function to pipe it through. 3744397Seric ** ctladdr -- an address pointer controlling the 3754397Seric ** user/groupid etc. of the mailer. 3763233Seric ** 3773233Seric ** Returns: 3783233Seric ** exit status of mailer. 3793233Seric ** 3803233Seric ** Side Effects: 3813233Seric ** none. 3823233Seric */ 3833233Seric 3844397Seric sendoff(m, pvp, editfcn, ctladdr) 3853233Seric struct mailer *m; 3863233Seric char **pvp; 3873233Seric int (*editfcn)(); 3884397Seric ADDRESS *ctladdr; 3893233Seric { 3903233Seric auto int st; 3913233Seric register int i; 3923233Seric int pid; 393*4709Seric int mpvect[2]; 3943233Seric FILE *mfile; 3953233Seric extern putmessage(); 3963233Seric extern FILE *fdopen(); 3973233Seric 3983233Seric # ifdef DEBUG 3993233Seric if (Debug) 400294Seric { 4013233Seric printf("Sendoff:\n"); 4023233Seric printav(pvp); 403294Seric } 4043233Seric # endif DEBUG 4054488Seric errno = 0; 4063233Seric 4072898Seric /* create a pipe to shove the mail through */ 408*4709Seric if (pipe(mpvect) < 0) 409294Seric { 410294Seric syserr("pipe"); 411294Seric return (-1); 412294Seric } 4134214Seric DOFORK(XFORK); 4144327Seric /* pid is set by DOFORK */ 415294Seric if (pid < 0) 416294Seric { 417294Seric syserr("Cannot fork"); 418*4709Seric (void) close(mpvect[0]); 419*4709Seric (void) close(mpvect[1]); 420294Seric return (-1); 421294Seric } 422294Seric else if (pid == 0) 423294Seric { 424294Seric /* child -- set up input & exec mailer */ 4251621Seric /* make diagnostic output be standard output */ 4264477Seric (void) signal(SIGINT, SIG_IGN); 4274477Seric (void) signal(SIGHUP, SIG_IGN); 4284215Seric (void) signal(SIGTERM, SIG_DFL); 429*4709Seric 430*4709Seric /* arrange to filter standard & diag output of command */ 431*4709Seric if (OutChannel != stdout) 432*4709Seric { 433*4709Seric (void) close(1); 434*4709Seric (void) dup(fileno(OutChannel)); 435*4709Seric } 4364082Seric (void) close(2); 4374082Seric (void) dup(1); 438*4709Seric 439*4709Seric /* arrange to get standard input */ 440*4709Seric (void) close(mpvect[1]); 4414082Seric (void) close(0); 442*4709Seric if (dup(mpvect[0]) < 0) 443294Seric { 4442898Seric syserr("Cannot dup to zero!"); 4452898Seric _exit(EX_OSERR); 446294Seric } 447*4709Seric (void) close(mpvect[0]); 4482968Seric if (!bitset(M_RESTR, m->m_flags)) 4494215Seric { 4504417Seric if (ctladdr->q_uid == 0) 4514417Seric { 4524417Seric extern int DefUid, DefGid; 4534415Seric 4544417Seric (void) setgid(DefGid); 4554417Seric (void) setuid(DefUid); 4564417Seric } 4574417Seric else 4584415Seric { 4594417Seric (void) setgid(ctladdr->q_gid); 4604417Seric (void) setuid(ctladdr->q_uid); 4614415Seric } 4624215Seric } 4632774Seric # ifndef VFORK 4642774Seric /* 4652774Seric ** We have to be careful with vfork - we can't mung up the 4662774Seric ** memory but we don't want the mailer to inherit any extra 4672774Seric ** open files. Chances are the mailer won't 4682774Seric ** care about an extra file, but then again you never know. 4692774Seric ** Actually, we would like to close(fileno(pwf)), but it's 4702774Seric ** declared static so we can't. But if we fclose(pwf), which 4712774Seric ** is what endpwent does, it closes it in the parent too and 4722774Seric ** the next getpwnam will be slower. If you have a weird 4732774Seric ** mailer that chokes on the extra file you should do the 4742774Seric ** endpwent(). 4752774Seric ** 4762774Seric ** Similar comments apply to log. However, openlog is 4772774Seric ** clever enough to set the FIOCLEX mode on the file, 4782774Seric ** so it will be closed automatically on the exec. 4792774Seric */ 4802774Seric 4812774Seric endpwent(); 482294Seric # ifdef LOG 4832089Seric closelog(); 484294Seric # endif LOG 4852774Seric # endif VFORK 486294Seric execv(m->m_mailer, pvp); 487294Seric /* syserr fails because log is closed */ 488294Seric /* syserr("Cannot exec %s", m->m_mailer); */ 4894214Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 4904082Seric (void) fflush(stdout); 4911619Seric _exit(EX_UNAVAILABLE); 492294Seric } 493294Seric 494*4709Seric /* 495*4709Seric ** Format and write message to mailer. 496*4709Seric */ 497*4709Seric 498*4709Seric (void) close(mpvect[0]); 4994123Seric (void) signal(SIGPIPE, SIG_IGN); 500*4709Seric mfile = fdopen(mpvect[1], "w"); 5012898Seric if (editfcn == NULL) 5022898Seric editfcn = putmessage; 5032898Seric (*editfcn)(mfile, m); 5044082Seric (void) fclose(mfile); 505294Seric 506294Seric /* 507294Seric ** Wait for child to die and report status. 508294Seric ** We should never get fatal errors (e.g., segmentation 509294Seric ** violation), so we report those specially. For other 510294Seric ** errors, we choose a status message (into statmsg), 511294Seric ** and if it represents an error, we print it. 512294Seric */ 513294Seric 514294Seric while ((i = wait(&st)) > 0 && i != pid) 515294Seric continue; 516294Seric if (i < 0) 517294Seric { 518294Seric syserr("wait"); 519294Seric return (-1); 520294Seric } 521294Seric if ((st & 0377) != 0) 522294Seric { 523294Seric syserr("%s: stat %o", pvp[0], st); 5241597Seric ExitStat = EX_UNAVAILABLE; 525294Seric return (-1); 526294Seric } 527294Seric i = (st >> 8) & 0377; 5282343Seric giveresponse(i, TRUE, m); 529294Seric return (i); 530294Seric } 531294Seric /* 532294Seric ** GIVERESPONSE -- Interpret an error response from a mailer 533294Seric ** 534294Seric ** Parameters: 535294Seric ** stat -- the status code from the mailer (high byte 536294Seric ** only; core dumps must have been taken care of 537294Seric ** already). 538294Seric ** force -- if set, force an error message output, even 539294Seric ** if the mailer seems to like to print its own 540294Seric ** messages. 541294Seric ** m -- the mailer descriptor for this mailer. 542294Seric ** 543294Seric ** Returns: 5444082Seric ** none. 545294Seric ** 546294Seric ** Side Effects: 5471518Seric ** Errors may be incremented. 548294Seric ** ExitStat may be set. 549294Seric */ 550294Seric 551294Seric giveresponse(stat, force, m) 552294Seric int stat; 553294Seric int force; 554294Seric register struct mailer *m; 555294Seric { 556294Seric register char *statmsg; 557294Seric extern char *SysExMsg[]; 558294Seric register int i; 559294Seric extern int N_SysEx; 5601624Seric char buf[30]; 561294Seric 5624315Seric /* 5634315Seric ** Compute status message from code. 5644315Seric */ 5654315Seric 566294Seric i = stat - EX__BASE; 567294Seric if (i < 0 || i > N_SysEx) 568294Seric statmsg = NULL; 569294Seric else 570294Seric statmsg = SysExMsg[i]; 571294Seric if (stat == 0) 5724065Seric { 5734194Seric if (bitset(M_LOCAL, m->m_flags)) 5744161Seric statmsg = "delivered"; 5754161Seric else 5764161Seric statmsg = "queued"; 5774065Seric if (Verbose) 5784166Seric message(Arpa_Info, statmsg); 5794065Seric } 5804621Seric else if (stat == EX_TEMPFAIL) 5814621Seric { 5824621Seric if (Verbose) 5834621Seric message(Arpa_Info, "transmission deferred"); 5844621Seric } 585294Seric else 586294Seric { 5871518Seric Errors++; 588294Seric if (statmsg == NULL && m->m_badstat != 0) 589294Seric { 590294Seric stat = m->m_badstat; 591294Seric i = stat - EX__BASE; 592294Seric # ifdef DEBUG 593294Seric if (i < 0 || i >= N_SysEx) 594294Seric syserr("Bad m_badstat %d", stat); 595294Seric else 596294Seric # endif DEBUG 597294Seric statmsg = SysExMsg[i]; 598294Seric } 599294Seric if (statmsg == NULL) 600294Seric usrerr("unknown mailer response %d", stat); 6014065Seric else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 602294Seric usrerr("%s", statmsg); 603294Seric } 604294Seric 605294Seric /* 606294Seric ** Final cleanup. 607294Seric ** Log a record of the transaction. Compute the new 608294Seric ** ExitStat -- if we already had an error, stick with 609294Seric ** that. 610294Seric */ 611294Seric 6121624Seric if (statmsg == NULL) 6131624Seric { 6144082Seric (void) sprintf(buf, "error %d", stat); 6151624Seric statmsg = buf; 6161624Seric } 6171624Seric 618294Seric # ifdef LOG 6192774Seric syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg); 620294Seric # endif LOG 6214621Seric if (stat != EX_TEMPFAIL) 6224621Seric setstat(stat); 623294Seric } 624294Seric /* 6252898Seric ** PUTMESSAGE -- output a message to the final mailer. 626294Seric ** 6272898Seric ** This routine takes care of recreating the header from the 6282898Seric ** in-core copy, etc. 629294Seric ** 630294Seric ** Parameters: 6312898Seric ** fp -- file to output onto. 6322898Seric ** m -- a mailer descriptor. 633294Seric ** 634294Seric ** Returns: 6352898Seric ** none. 636294Seric ** 637294Seric ** Side Effects: 6382898Seric ** The message is written onto fp. 639294Seric */ 640294Seric 6412898Seric putmessage(fp, m) 6422898Seric FILE *fp; 6432898Seric struct mailer *m; 644294Seric { 6452898Seric char buf[BUFSIZ]; 6462898Seric register int i; 6474315Seric register HDR *h; 6482898Seric extern char *arpadate(); 6492898Seric bool anyheader = FALSE; 6503044Seric extern char *capitalize(); 6514370Seric extern char *hvalue(); 6524370Seric extern bool samefrom(); 6534447Seric char *of_line; 654294Seric 6554315Seric /* 6564315Seric ** Output "From" line unless supressed 6574315Seric */ 6584315Seric 6593186Seric if (!bitset(M_NHDR, m->m_flags)) 6604205Seric { 6614205Seric (void) expand("$l", buf, &buf[sizeof buf - 1]); 6624205Seric fprintf(fp, "%s\n", buf); 6634205Seric } 6643186Seric 6654315Seric /* 6664315Seric ** Output all header lines 6674315Seric */ 6684315Seric 6694447Seric of_line = hvalue("original-from"); 6702898Seric for (h = Header; h != NULL; h = h->h_link) 6711828Seric { 6724315Seric register char *p; 6734370Seric char *origfrom = OrigFrom; 6744447Seric bool nooutput; 6754315Seric 6764447Seric nooutput = FALSE; 6773389Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 6784209Seric { 6794209Seric p = ")><("; /* can't happen (I hope) */ 6804447Seric nooutput = TRUE; 6814209Seric } 6824447Seric 6834447Seric /* use From: line from message if generated is the same */ 6844370Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL && 6854447Seric strcmp(m->m_from, "$f") == 0 && of_line == NULL) 6863385Seric { 6874370Seric p = origfrom; 6884370Seric origfrom = NULL; 6894370Seric } 6904370Seric else if (bitset(H_DEFAULT, h->h_flags)) 6914370Seric { 6924082Seric (void) expand(h->h_value, buf, &buf[sizeof buf]); 6933385Seric p = buf; 6943385Seric } 6952898Seric else 6963385Seric p = h->h_value; 6974447Seric if (p == NULL || *p == '\0') 6983389Seric continue; 6994209Seric 7004209Seric /* hack, hack -- output Original-From field if different */ 7014447Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL) 7024209Seric { 7034447Seric /* output new Original-From line if needed */ 7044447Seric if (of_line == NULL && !samefrom(p, origfrom)) 7054447Seric { 7064447Seric fprintf(fp, "Original-From: %s\n", origfrom); 7074447Seric anyheader = TRUE; 7084447Seric } 7094447Seric if (of_line != NULL && !nooutput && samefrom(p, of_line)) 7104447Seric { 7114447Seric /* delete Original-From: line if redundant */ 7124447Seric p = of_line; 7134447Seric of_line = NULL; 7144447Seric } 7154447Seric } 7164447Seric else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL) 7174447Seric nooutput = TRUE; 7184447Seric 7194447Seric /* finally, output the header line */ 7204447Seric if (!nooutput) 7214447Seric { 7224447Seric fprintf(fp, "%s: %s\n", capitalize(h->h_field), p); 7234447Seric h->h_flags |= H_USED; 7244370Seric anyheader = TRUE; 7254209Seric } 7262898Seric } 7272898Seric if (anyheader) 7282898Seric fprintf(fp, "\n"); 7292898Seric 7304315Seric /* 7314315Seric ** Output the body of the message 7324315Seric */ 7334315Seric 7344452Seric if (TempFile != NULL) 7354452Seric { 7364452Seric rewind(TempFile); 7374452Seric while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, TempFile)) > 0) 7384452Seric (void) fwrite(buf, 1, i, fp); 7392898Seric 7404452Seric if (ferror(TempFile)) 7414452Seric { 7424452Seric syserr("putmessage: read error"); 7434452Seric setstat(EX_IOERR); 7444452Seric } 7454452Seric } 7464452Seric 7474621Seric (void) fflush(fp); 7484123Seric if (ferror(fp) && errno != EPIPE) 749294Seric { 7502898Seric syserr("putmessage: write error"); 751294Seric setstat(EX_IOERR); 752294Seric } 7534123Seric errno = 0; 754294Seric } 755294Seric /* 7564370Seric ** SAMEFROM -- tell if two text addresses represent the same from address. 7574370Seric ** 7584370Seric ** Parameters: 7594370Seric ** ifrom -- internally generated form of from address. 7604370Seric ** efrom -- external form of from address. 7614370Seric ** 7624370Seric ** Returns: 7634370Seric ** TRUE -- if they convey the same info. 7644370Seric ** FALSE -- if any information has been lost. 7654370Seric ** 7664370Seric ** Side Effects: 7674370Seric ** none. 7684370Seric */ 7694370Seric 7704370Seric bool 7714370Seric samefrom(ifrom, efrom) 7724370Seric char *ifrom; 7734370Seric char *efrom; 7744370Seric { 7754447Seric register char *p; 7764447Seric char buf[MAXNAME + 4]; 7774447Seric 7784447Seric # ifdef DEBUG 7794447Seric if (Debug > 7) 7804447Seric printf("samefrom(%s,%s)-->", ifrom, efrom); 7814447Seric # endif DEBUG 7824447Seric if (strcmp(ifrom, efrom) == 0) 7834447Seric goto success; 7844447Seric p = index(ifrom, '@'); 7854447Seric if (p == NULL) 7864447Seric goto failure; 7874447Seric *p = '\0'; 7884447Seric strcpy(buf, ifrom); 7894447Seric strcat(buf, " at "); 7904447Seric *p++ = '@'; 7914447Seric strcat(buf, p); 7924447Seric if (strcmp(buf, efrom) == 0) 7934447Seric goto success; 7944447Seric 7954447Seric failure: 7964447Seric # ifdef DEBUG 7974447Seric if (Debug > 7) 7984447Seric printf("FALSE\n"); 7994447Seric # endif DEBUG 8004447Seric return (FALSE); 8014447Seric 8024447Seric success: 8034447Seric # ifdef DEBUG 8044447Seric if (Debug > 7) 8054447Seric printf("TRUE\n"); 8064447Seric # endif DEBUG 8074447Seric return (TRUE); 8084370Seric } 8094370Seric /* 810294Seric ** MAILFILE -- Send a message to a file. 811294Seric ** 8124327Seric ** If the file has the setuid/setgid bits set, but NO execute 8134327Seric ** bits, sendmail will try to become the owner of that file 8144327Seric ** rather than the real user. Obviously, this only works if 8154327Seric ** sendmail runs as root. 8164327Seric ** 817294Seric ** Parameters: 818294Seric ** filename -- the name of the file to send to. 8194397Seric ** ctladdr -- the controlling address header -- includes 8204397Seric ** the userid/groupid to be when sending. 821294Seric ** 822294Seric ** Returns: 823294Seric ** The exit code associated with the operation. 824294Seric ** 825294Seric ** Side Effects: 826294Seric ** none. 827294Seric */ 828294Seric 8294397Seric mailfile(filename, ctladdr) 830294Seric char *filename; 8314397Seric ADDRESS *ctladdr; 832294Seric { 833294Seric register FILE *f; 8344214Seric register int pid; 835294Seric 8364214Seric /* 8374214Seric ** Fork so we can change permissions here. 8384214Seric ** Note that we MUST use fork, not vfork, because of 8394214Seric ** the complications of calling subroutines, etc. 8404214Seric */ 8414067Seric 8424214Seric DOFORK(fork); 8434214Seric 8444214Seric if (pid < 0) 8454214Seric return (EX_OSERR); 8464214Seric else if (pid == 0) 8474214Seric { 8484214Seric /* child -- actually write to file */ 8494327Seric struct stat stb; 8504417Seric extern int DefUid, DefGid; 8514327Seric 8524215Seric (void) signal(SIGINT, SIG_DFL); 8534215Seric (void) signal(SIGHUP, SIG_DFL); 8544215Seric (void) signal(SIGTERM, SIG_DFL); 8554327Seric umask(OldUmask); 8564327Seric if (stat(filename, &stb) < 0) 8574431Seric stb.st_mode = 0666; 8584327Seric if (bitset(0111, stb.st_mode)) 8594327Seric exit(EX_CANTCREAT); 8604401Seric if (ctladdr == NULL) 8614401Seric ctladdr = &From; 8624327Seric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 8634417Seric { 8644417Seric if (ctladdr->q_uid == 0) 8654417Seric (void) setgid(DefGid); 8664417Seric else 8674417Seric (void) setgid(ctladdr->q_gid); 8684417Seric } 8694327Seric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 8704417Seric { 8714417Seric if (ctladdr->q_uid == 0) 8724417Seric (void) setuid(DefUid); 8734417Seric else 8744417Seric (void) setuid(ctladdr->q_uid); 8754417Seric } 8764214Seric f = fopen(filename, "a"); 8774214Seric if (f == NULL) 8784214Seric exit(EX_CANTCREAT); 8794214Seric 8804214Seric putmessage(f, Mailer[1]); 8814214Seric fputs("\n", f); 8824214Seric (void) fclose(f); 8834214Seric (void) fflush(stdout); 8844417Seric 8854417Seric /* reset ISUID & ISGID bits */ 8864621Seric (void) chmod(filename, (int) stb.st_mode); 8874214Seric exit(EX_OK); 8884315Seric /*NOTREACHED*/ 8894214Seric } 8904214Seric else 8914214Seric { 8924214Seric /* parent -- wait for exit status */ 8934214Seric register int i; 8944214Seric auto int stat; 8954214Seric 8964214Seric while ((i = wait(&stat)) != pid) 8974214Seric { 8984214Seric if (i < 0) 8994214Seric { 9004214Seric stat = EX_OSERR << 8; 9014214Seric break; 9024214Seric } 9034214Seric } 9044215Seric if ((stat & 0377) != 0) 9054215Seric stat = EX_UNAVAILABLE << 8; 9064214Seric return ((stat >> 8) & 0377); 9074214Seric } 908294Seric } 9094550Seric /* 9104550Seric ** SENDALL -- actually send all the messages. 9114550Seric ** 9124550Seric ** Parameters: 9134550Seric ** verifyonly -- if set, only give verification messages. 9144550Seric ** 9154550Seric ** Returns: 9164550Seric ** none. 9174550Seric ** 9184550Seric ** Side Effects: 9194550Seric ** Scans the send lists and sends everything it finds. 9204550Seric */ 9214550Seric 9224550Seric sendall(verifyonly) 9234550Seric bool verifyonly; 9244550Seric { 9254550Seric register int i; 9264550Seric typedef int (*fnptr)(); 9274550Seric 9284550Seric for (i = 0; Mailer[i] != NULL; i++) 9294550Seric { 9304550Seric ADDRESS *q; 9314550Seric 9324550Seric for (q = Mailer[i]->m_sendq; q != NULL; q = q->q_next) 9334550Seric { 9344550Seric if (verifyonly) 9354550Seric { 9364550Seric To = q->q_paddr; 9374550Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 9384550Seric { 9394596Seric if (bitset(M_LOCAL, q->q_mailer->m_flags)) 9404550Seric message(Arpa_Info, "deliverable"); 9414550Seric else 9424550Seric message(Arpa_Info, "queueable"); 9434550Seric } 9444550Seric } 9454550Seric else 9464550Seric (void) deliver(q, (fnptr) NULL); 9474550Seric } 9484550Seric } 9494550Seric } 950