125a99e2eSeric # include <signal.h> 254aa2b0fSeric # include <errno.h> 3*f129ec7dSeric # include <sys/types.h> 4*f129ec7dSeric # include <sys/stat.h> 5b20b3270Seric # include "sendmail.h" 625a99e2eSeric # ifdef LOG 7e374fd72Seric # include <syslog.h> 825a99e2eSeric # endif LOG 925a99e2eSeric 10*f129ec7dSeric static char SccsId[] = "@(#)deliver.c 3.36 09/07/81"; 11259cace7Seric 1225a99e2eSeric /* 1313bbc08cSeric ** DELIVER -- Deliver a message to a list of addresses. 1413bbc08cSeric ** 1513bbc08cSeric ** This routine delivers to everyone on the same host as the 1613bbc08cSeric ** user on the head of the list. It is clever about mailers 1713bbc08cSeric ** that don't handle multiple users. It is NOT guaranteed 1813bbc08cSeric ** that it will deliver to all these addresses however -- so 1913bbc08cSeric ** deliver should be called once for each address on the 2013bbc08cSeric ** list. 2125a99e2eSeric ** 2225a99e2eSeric ** Parameters: 2313bbc08cSeric ** to -- head of the address list to deliver to. 2425a99e2eSeric ** editfcn -- if non-NULL, we want to call this function 2525a99e2eSeric ** to output the letter (instead of just out- 2625a99e2eSeric ** putting it raw). 2725a99e2eSeric ** 2825a99e2eSeric ** Returns: 2925a99e2eSeric ** zero -- successfully delivered. 3025a99e2eSeric ** else -- some failure, see ExitStat for more info. 3125a99e2eSeric ** 3225a99e2eSeric ** Side Effects: 3325a99e2eSeric ** The standard input is passed off to someone. 3425a99e2eSeric */ 3525a99e2eSeric 3625a99e2eSeric deliver(to, editfcn) 372a6e0786Seric ADDRESS *to; 3825a99e2eSeric int (*editfcn)(); 3925a99e2eSeric { 4025a99e2eSeric char *host; 4125a99e2eSeric char *user; 4225a99e2eSeric char **pvp; 435dfc646bSeric register char **mvp; 4425a99e2eSeric register char *p; 455dfc646bSeric register struct mailer *m; 465dfc646bSeric register int i; 476328bdf7Seric extern putmessage(); 482a6e0786Seric extern bool checkcompat(); 495dfc646bSeric char *pv[MAXPV+1]; 505dfc646bSeric char tobuf[MAXLINE]; 515dfc646bSeric char buf[MAXNAME]; 525dfc646bSeric bool firstone; 5325a99e2eSeric 5406ca012aSeric if (!ForceMail && bitset(QDONTSEND, to->q_flags)) 555dfc646bSeric return (0); 5625a99e2eSeric 5725a99e2eSeric # ifdef DEBUG 5825a99e2eSeric if (Debug) 595dfc646bSeric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 605dfc646bSeric to->q_mailer, to->q_host, to->q_user); 6125a99e2eSeric # endif DEBUG 6225a99e2eSeric 6325a99e2eSeric /* 645dfc646bSeric ** Do initial argv setup. 655dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 665dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 675dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 685dfc646bSeric ** code does not check for 'pv' overflow; this places a 695dfc646bSeric ** manifest lower limit of 4 for MAXPV. 705dfc646bSeric */ 715dfc646bSeric 725dfc646bSeric m = Mailer[to->q_mailer]; 735dfc646bSeric host = to->q_host; 745dfc646bSeric define('g', m->m_from); /* translated from address */ 755dfc646bSeric define('h', host); /* to host */ 765dfc646bSeric Errors = 0; 775dfc646bSeric errno = 0; 785dfc646bSeric pvp = pv; 795dfc646bSeric *pvp++ = m->m_argv[0]; 805dfc646bSeric 815dfc646bSeric /* insert -f or -r flag as appropriate */ 825dfc646bSeric if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag) 835dfc646bSeric { 845dfc646bSeric if (bitset(M_FOPT, m->m_flags)) 855dfc646bSeric *pvp++ = "-f"; 865dfc646bSeric else 875dfc646bSeric *pvp++ = "-r"; 88db8841e9Seric (void) expand("$g", buf, &buf[sizeof buf - 1]); 895dfc646bSeric *pvp++ = newstr(buf); 905dfc646bSeric } 915dfc646bSeric 925dfc646bSeric /* 935dfc646bSeric ** Append the other fixed parts of the argv. These run 945dfc646bSeric ** up to the first entry containing "$u". There can only 955dfc646bSeric ** be one of these, and there are only a few more slots 965dfc646bSeric ** in the pv after it. 975dfc646bSeric */ 985dfc646bSeric 995dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 1005dfc646bSeric { 1015dfc646bSeric while ((p = index(p, '$')) != NULL) 1025dfc646bSeric if (*++p == 'u') 1035dfc646bSeric break; 1045dfc646bSeric if (p != NULL) 1055dfc646bSeric break; 1065dfc646bSeric 1075dfc646bSeric /* this entry is safe -- go ahead and process it */ 108db8841e9Seric (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 1095dfc646bSeric *pvp++ = newstr(buf); 1105dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 1115dfc646bSeric { 1125dfc646bSeric syserr("Too many parameters to %s before $u", pv[0]); 1135dfc646bSeric return (-1); 1145dfc646bSeric } 1155dfc646bSeric } 1165dfc646bSeric if (*mvp == NULL) 1175dfc646bSeric syserr("No $u in mailer argv for %s", pv[0]); 1185dfc646bSeric 1195dfc646bSeric /* 1205dfc646bSeric ** At this point *mvp points to the argument with $u. We 1215dfc646bSeric ** run through our address list and append all the addresses 1225dfc646bSeric ** we can. If we run out of space, do not fret! We can 1235dfc646bSeric ** always send another copy later. 1245dfc646bSeric */ 1255dfc646bSeric 1265dfc646bSeric tobuf[0] = '\0'; 1275dfc646bSeric firstone = TRUE; 1285dfc646bSeric To = tobuf; 1295dfc646bSeric for (; to != NULL; to = to->q_next) 1305dfc646bSeric { 1315dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 1325dfc646bSeric if (!firstone && !bitset(M_MUSER, m->m_flags)) 1335dfc646bSeric break; 1345dfc646bSeric 1355dfc646bSeric /* if already sent or not for this host, don't send */ 13606ca012aSeric if ((!ForceMail && bitset(QDONTSEND, to->q_flags)) || 13706ca012aSeric strcmp(to->q_host, host) != 0) 1385dfc646bSeric continue; 1395dfc646bSeric user = to->q_user; 1405dfc646bSeric To = to->q_paddr; 1415dfc646bSeric to->q_flags |= QDONTSEND; 1425dfc646bSeric # ifdef DEBUG 1435dfc646bSeric if (Debug) 1445dfc646bSeric printf(" send to `%s'\n", user); 1455dfc646bSeric # endif DEBUG 1465dfc646bSeric 1475dfc646bSeric /* 1485dfc646bSeric ** Check to see that these people are allowed to 1495dfc646bSeric ** talk to each other. 1502a6e0786Seric */ 1512a6e0786Seric 1522a6e0786Seric if (!checkcompat(to)) 1535dfc646bSeric { 1545dfc646bSeric giveresponse(EX_UNAVAILABLE, TRUE, m); 1555dfc646bSeric continue; 1565dfc646bSeric } 1572a6e0786Seric 1582a6e0786Seric /* 1599ec9501bSeric ** Strip quote bits from names if the mailer is dumb 1609ec9501bSeric ** about them. 16125a99e2eSeric */ 16225a99e2eSeric 1632a6e0786Seric if (bitset(M_STRIPQ, m->m_flags)) 16425a99e2eSeric { 1659ec9501bSeric stripquotes(user, TRUE); 1669ec9501bSeric stripquotes(host, TRUE); 1679ec9501bSeric } 1689ec9501bSeric else 1699ec9501bSeric { 1709ec9501bSeric stripquotes(user, FALSE); 1719ec9501bSeric stripquotes(host, FALSE); 17225a99e2eSeric } 17325a99e2eSeric 17425a99e2eSeric /* 1753efaed6eSeric ** If an error message has already been given, don't 1763efaed6eSeric ** bother to send to this address. 1773efaed6eSeric ** 1783efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 1793efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 1803efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 1813efaed6eSeric */ 1823efaed6eSeric 1833efaed6eSeric if (bitset(QBADADDR, to->q_flags)) 1843efaed6eSeric continue; 1853efaed6eSeric 186f2fec898Seric /* save statistics.... */ 187f2fec898Seric Stat.stat_nt[to->q_mailer]++; 188f2fec898Seric Stat.stat_bt[to->q_mailer] += kbytes(MsgSize); 189f2fec898Seric 1903efaed6eSeric /* 19125a99e2eSeric ** See if this user name is "special". 19225a99e2eSeric ** If the user name has a slash in it, assume that this 19325a99e2eSeric ** is a file -- send it off without further ado. 19425a99e2eSeric ** Note that this means that editfcn's will not 1955dfc646bSeric ** be applied to the message. Also note that 1965dfc646bSeric ** this type of addresses is not processed along 1975dfc646bSeric ** with the others, so we fudge on the To person. 19825a99e2eSeric */ 19925a99e2eSeric 2006cbfa739Seric if (m == Mailer[MN_LOCAL]) 20125a99e2eSeric { 20225a99e2eSeric if (index(user, '/') != NULL) 20325a99e2eSeric { 20425a99e2eSeric i = mailfile(user); 20525a99e2eSeric giveresponse(i, TRUE, m); 2065dfc646bSeric continue; 20725a99e2eSeric } 20825a99e2eSeric } 20925a99e2eSeric 21013bbc08cSeric /* 21113bbc08cSeric ** Address is verified -- add this user to mailer 21213bbc08cSeric ** argv, and add it to the print list of recipients. 21313bbc08cSeric */ 21413bbc08cSeric 2155dfc646bSeric /* create list of users for error messages */ 2165dfc646bSeric if (tobuf[0] != '\0') 217db8841e9Seric (void) strcat(tobuf, ","); 218db8841e9Seric (void) strcat(tobuf, to->q_paddr); 2195dfc646bSeric define('u', user); /* to user */ 220c2567733Seric define('z', to->q_home); /* user's home */ 2215dfc646bSeric 2225dfc646bSeric /* expand out this user */ 223d4ccc802Seric (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 2245dfc646bSeric *pvp++ = newstr(buf); 2255dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 2265dfc646bSeric { 2275dfc646bSeric /* allow some space for trailing parms */ 2285dfc646bSeric break; 2295dfc646bSeric } 2305dfc646bSeric } 2315dfc646bSeric 232145b49b1Seric /* see if any addresses still exist */ 233145b49b1Seric if (tobuf[0] == '\0') 234145b49b1Seric return (0); 235145b49b1Seric 2365dfc646bSeric /* print out messages as full list */ 2375dfc646bSeric To = tobuf; 2385dfc646bSeric 2395dfc646bSeric /* 2405dfc646bSeric ** Fill out any parameters after the $u parameter. 2415dfc646bSeric */ 2425dfc646bSeric 2435dfc646bSeric while (*++mvp != NULL) 2445dfc646bSeric { 245db8841e9Seric (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 2465dfc646bSeric *pvp++ = newstr(buf); 2475dfc646bSeric if (pvp >= &pv[MAXPV]) 2485dfc646bSeric syserr("deliver: pv overflow after $u for %s", pv[0]); 2495dfc646bSeric } 2505dfc646bSeric *pvp++ = NULL; 2515dfc646bSeric 25225a99e2eSeric /* 25325a99e2eSeric ** Call the mailer. 2546328bdf7Seric ** The argument vector gets built, pipes 25525a99e2eSeric ** are created as necessary, and we fork & exec as 2566328bdf7Seric ** appropriate. 25725a99e2eSeric */ 25825a99e2eSeric 2595dfc646bSeric if (editfcn == NULL) 2605dfc646bSeric editfcn = putmessage; 2615dfc646bSeric i = sendoff(m, pv, editfcn); 2625dfc646bSeric 2635dfc646bSeric return (i); 26425a99e2eSeric } 2655dfc646bSeric /* 26632d19d43Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 26732d19d43Seric ** 26832d19d43Seric ** This MUST be a macro, since after a vfork we are running 26932d19d43Seric ** two processes on the same stack!!! 27032d19d43Seric ** 27132d19d43Seric ** Parameters: 27232d19d43Seric ** none. 27332d19d43Seric ** 27432d19d43Seric ** Returns: 27532d19d43Seric ** From a macro??? You've got to be kidding! 27632d19d43Seric ** 27732d19d43Seric ** Side Effects: 27832d19d43Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 27932d19d43Seric ** pid of child in parent, zero in child. 28032d19d43Seric ** -1 on unrecoverable error. 28132d19d43Seric ** 28232d19d43Seric ** Notes: 28332d19d43Seric ** I'm awfully sorry this looks so awful. That's 28432d19d43Seric ** vfork for you..... 28532d19d43Seric */ 28632d19d43Seric 28732d19d43Seric # define NFORKTRIES 5 28832d19d43Seric # ifdef VFORK 28932d19d43Seric # define XFORK vfork 29032d19d43Seric # else VFORK 29132d19d43Seric # define XFORK fork 29232d19d43Seric # endif VFORK 29332d19d43Seric 29432d19d43Seric # define DOFORK(fORKfN) \ 29532d19d43Seric {\ 29632d19d43Seric register int i;\ 29732d19d43Seric \ 29832d19d43Seric for (i = NFORKTRIES; i-- > 0; )\ 29932d19d43Seric {\ 30032d19d43Seric pid = fORKfN();\ 30132d19d43Seric if (pid >= 0)\ 30232d19d43Seric break;\ 30332d19d43Seric sleep((unsigned) NFORKTRIES - i);\ 30432d19d43Seric }\ 30532d19d43Seric } 30632d19d43Seric /* 3075dfc646bSeric ** SENDOFF -- send off call to mailer & collect response. 3085dfc646bSeric ** 3095dfc646bSeric ** Parameters: 3105dfc646bSeric ** m -- mailer descriptor. 3115dfc646bSeric ** pvp -- parameter vector to send to it. 3125dfc646bSeric ** editfcn -- function to pipe it through. 3135dfc646bSeric ** 3145dfc646bSeric ** Returns: 3155dfc646bSeric ** exit status of mailer. 3165dfc646bSeric ** 3175dfc646bSeric ** Side Effects: 3185dfc646bSeric ** none. 3195dfc646bSeric */ 3205dfc646bSeric 3215dfc646bSeric sendoff(m, pvp, editfcn) 3225dfc646bSeric struct mailer *m; 3235dfc646bSeric char **pvp; 3245dfc646bSeric int (*editfcn)(); 3255dfc646bSeric { 3265dfc646bSeric auto int st; 3275dfc646bSeric register int i; 3285dfc646bSeric int pid; 3295dfc646bSeric int pvect[2]; 3305dfc646bSeric FILE *mfile; 3315dfc646bSeric extern putmessage(); 3325dfc646bSeric extern FILE *fdopen(); 3335dfc646bSeric 3345dfc646bSeric # ifdef DEBUG 3355dfc646bSeric if (Debug) 3365dfc646bSeric { 3375dfc646bSeric printf("Sendoff:\n"); 3385dfc646bSeric printav(pvp); 3395dfc646bSeric } 3405dfc646bSeric # endif DEBUG 3415dfc646bSeric 3426328bdf7Seric /* create a pipe to shove the mail through */ 3436328bdf7Seric if (pipe(pvect) < 0) 34425a99e2eSeric { 34525a99e2eSeric syserr("pipe"); 34625a99e2eSeric return (-1); 34725a99e2eSeric } 34832d19d43Seric DOFORK(XFORK); 349*f129ec7dSeric /* pid is set by DOFORK */ 35025a99e2eSeric if (pid < 0) 35125a99e2eSeric { 35225a99e2eSeric syserr("Cannot fork"); 353db8841e9Seric (void) close(pvect[0]); 354db8841e9Seric (void) close(pvect[1]); 35525a99e2eSeric return (-1); 35625a99e2eSeric } 35725a99e2eSeric else if (pid == 0) 35825a99e2eSeric { 35925a99e2eSeric /* child -- set up input & exec mailer */ 36003ab8e55Seric /* make diagnostic output be standard output */ 3610984da9fSeric (void) signal(SIGINT, SIG_DFL); 3620984da9fSeric (void) signal(SIGHUP, SIG_DFL); 3630984da9fSeric (void) signal(SIGTERM, SIG_DFL); 364db8841e9Seric (void) close(2); 365db8841e9Seric (void) dup(1); 366db8841e9Seric (void) close(0); 36725a99e2eSeric if (dup(pvect[0]) < 0) 36825a99e2eSeric { 36925a99e2eSeric syserr("Cannot dup to zero!"); 370a590b978Seric _exit(EX_OSERR); 37125a99e2eSeric } 372db8841e9Seric (void) close(pvect[0]); 373db8841e9Seric (void) close(pvect[1]); 3742a6e0786Seric if (!bitset(M_RESTR, m->m_flags)) 3750984da9fSeric { 376db8841e9Seric (void) setuid(getuid()); 3770984da9fSeric (void) setgid(getgid()); 3780984da9fSeric } 379e374fd72Seric # ifndef VFORK 380e374fd72Seric /* 381e374fd72Seric ** We have to be careful with vfork - we can't mung up the 382e374fd72Seric ** memory but we don't want the mailer to inherit any extra 383e374fd72Seric ** open files. Chances are the mailer won't 384e374fd72Seric ** care about an extra file, but then again you never know. 385e374fd72Seric ** Actually, we would like to close(fileno(pwf)), but it's 386e374fd72Seric ** declared static so we can't. But if we fclose(pwf), which 387e374fd72Seric ** is what endpwent does, it closes it in the parent too and 388e374fd72Seric ** the next getpwnam will be slower. If you have a weird 389e374fd72Seric ** mailer that chokes on the extra file you should do the 390e374fd72Seric ** endpwent(). 391e374fd72Seric ** 392e374fd72Seric ** Similar comments apply to log. However, openlog is 393e374fd72Seric ** clever enough to set the FIOCLEX mode on the file, 394e374fd72Seric ** so it will be closed automatically on the exec. 395e374fd72Seric */ 396e374fd72Seric 397e374fd72Seric endpwent(); 39825a99e2eSeric # ifdef LOG 399f9fe028fSeric closelog(); 40025a99e2eSeric # endif LOG 401e374fd72Seric # endif VFORK 40225a99e2eSeric execv(m->m_mailer, pvp); 40325a99e2eSeric /* syserr fails because log is closed */ 40425a99e2eSeric /* syserr("Cannot exec %s", m->m_mailer); */ 40532d19d43Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 406db8841e9Seric (void) fflush(stdout); 407a590b978Seric _exit(EX_UNAVAILABLE); 40825a99e2eSeric } 40925a99e2eSeric 4106328bdf7Seric /* write out message to mailer */ 411db8841e9Seric (void) close(pvect[0]); 41254aa2b0fSeric (void) signal(SIGPIPE, SIG_IGN); 41325a99e2eSeric mfile = fdopen(pvect[1], "w"); 4146328bdf7Seric if (editfcn == NULL) 4156328bdf7Seric editfcn = putmessage; 4166328bdf7Seric (*editfcn)(mfile, m); 417db8841e9Seric (void) fclose(mfile); 41825a99e2eSeric 41925a99e2eSeric /* 42025a99e2eSeric ** Wait for child to die and report status. 42125a99e2eSeric ** We should never get fatal errors (e.g., segmentation 42225a99e2eSeric ** violation), so we report those specially. For other 42325a99e2eSeric ** errors, we choose a status message (into statmsg), 42425a99e2eSeric ** and if it represents an error, we print it. 42525a99e2eSeric */ 42625a99e2eSeric 42725a99e2eSeric while ((i = wait(&st)) > 0 && i != pid) 42825a99e2eSeric continue; 42925a99e2eSeric if (i < 0) 43025a99e2eSeric { 43125a99e2eSeric syserr("wait"); 43225a99e2eSeric return (-1); 43325a99e2eSeric } 43425a99e2eSeric if ((st & 0377) != 0) 43525a99e2eSeric { 43625a99e2eSeric syserr("%s: stat %o", pvp[0], st); 437df2792f7Seric ExitStat = EX_UNAVAILABLE; 43825a99e2eSeric return (-1); 43925a99e2eSeric } 44025a99e2eSeric i = (st >> 8) & 0377; 4412ac087dbSeric giveresponse(i, TRUE, m); 44225a99e2eSeric return (i); 44325a99e2eSeric } 44425a99e2eSeric /* 44525a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 44625a99e2eSeric ** 44725a99e2eSeric ** Parameters: 44825a99e2eSeric ** stat -- the status code from the mailer (high byte 44925a99e2eSeric ** only; core dumps must have been taken care of 45025a99e2eSeric ** already). 45125a99e2eSeric ** force -- if set, force an error message output, even 45225a99e2eSeric ** if the mailer seems to like to print its own 45325a99e2eSeric ** messages. 45425a99e2eSeric ** m -- the mailer descriptor for this mailer. 45525a99e2eSeric ** 45625a99e2eSeric ** Returns: 457db8841e9Seric ** none. 45825a99e2eSeric ** 45925a99e2eSeric ** Side Effects: 460c1f9df2cSeric ** Errors may be incremented. 46125a99e2eSeric ** ExitStat may be set. 46225a99e2eSeric */ 46325a99e2eSeric 46425a99e2eSeric giveresponse(stat, force, m) 46525a99e2eSeric int stat; 46625a99e2eSeric int force; 46725a99e2eSeric register struct mailer *m; 46825a99e2eSeric { 46925a99e2eSeric register char *statmsg; 47025a99e2eSeric extern char *SysExMsg[]; 47125a99e2eSeric register int i; 47225a99e2eSeric extern int N_SysEx; 47329dd97a3Seric char buf[30]; 47425a99e2eSeric 47513bbc08cSeric /* 47613bbc08cSeric ** Compute status message from code. 47713bbc08cSeric */ 47813bbc08cSeric 47925a99e2eSeric i = stat - EX__BASE; 48025a99e2eSeric if (i < 0 || i > N_SysEx) 48125a99e2eSeric statmsg = NULL; 48225a99e2eSeric else 48325a99e2eSeric statmsg = SysExMsg[i]; 48425a99e2eSeric if (stat == 0) 485c38ba59cSeric { 4866cbfa739Seric if (bitset(M_LOCAL, m->m_flags)) 4873efaed6eSeric statmsg = "delivered"; 4883efaed6eSeric else 4893efaed6eSeric statmsg = "queued"; 490c38ba59cSeric if (Verbose) 491544026c5Seric message(Arpa_Info, statmsg); 492c38ba59cSeric } 49325a99e2eSeric else 49425a99e2eSeric { 495c1f9df2cSeric Errors++; 49625a99e2eSeric if (statmsg == NULL && m->m_badstat != 0) 49725a99e2eSeric { 49825a99e2eSeric stat = m->m_badstat; 49925a99e2eSeric i = stat - EX__BASE; 50025a99e2eSeric # ifdef DEBUG 50125a99e2eSeric if (i < 0 || i >= N_SysEx) 50225a99e2eSeric syserr("Bad m_badstat %d", stat); 50325a99e2eSeric else 50425a99e2eSeric # endif DEBUG 50525a99e2eSeric statmsg = SysExMsg[i]; 50625a99e2eSeric } 50725a99e2eSeric if (statmsg == NULL) 50825a99e2eSeric usrerr("unknown mailer response %d", stat); 509c38ba59cSeric else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 51025a99e2eSeric usrerr("%s", statmsg); 51125a99e2eSeric } 51225a99e2eSeric 51325a99e2eSeric /* 51425a99e2eSeric ** Final cleanup. 51525a99e2eSeric ** Log a record of the transaction. Compute the new 51625a99e2eSeric ** ExitStat -- if we already had an error, stick with 51725a99e2eSeric ** that. 51825a99e2eSeric */ 51925a99e2eSeric 52025a99e2eSeric if (statmsg == NULL) 52129dd97a3Seric { 522db8841e9Seric (void) sprintf(buf, "error %d", stat); 52329dd97a3Seric statmsg = buf; 52429dd97a3Seric } 52529dd97a3Seric 52629dd97a3Seric # ifdef LOG 527e374fd72Seric syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg); 52825a99e2eSeric # endif LOG 529243921eeSeric setstat(stat); 53025a99e2eSeric } 53125a99e2eSeric /* 5326328bdf7Seric ** PUTMESSAGE -- output a message to the final mailer. 53325a99e2eSeric ** 5346328bdf7Seric ** This routine takes care of recreating the header from the 5356328bdf7Seric ** in-core copy, etc. 53625a99e2eSeric ** 53725a99e2eSeric ** Parameters: 5386328bdf7Seric ** fp -- file to output onto. 5396328bdf7Seric ** m -- a mailer descriptor. 54025a99e2eSeric ** 54125a99e2eSeric ** Returns: 5426328bdf7Seric ** none. 54325a99e2eSeric ** 54425a99e2eSeric ** Side Effects: 5456328bdf7Seric ** The message is written onto fp. 54625a99e2eSeric */ 54725a99e2eSeric 5486328bdf7Seric putmessage(fp, m) 5496328bdf7Seric FILE *fp; 5506328bdf7Seric struct mailer *m; 55125a99e2eSeric { 5526328bdf7Seric char buf[BUFSIZ]; 5536328bdf7Seric register int i; 55413bbc08cSeric register HDR *h; 5556328bdf7Seric extern char *arpadate(); 5566328bdf7Seric bool anyheader = FALSE; 557e9ff65b0Seric extern char *capitalize(); 55825a99e2eSeric 55913bbc08cSeric /* 56013bbc08cSeric ** Output "From" line unless supressed 56113bbc08cSeric */ 56213bbc08cSeric 56340e4ab56Seric if (!bitset(M_NHDR, m->m_flags)) 5641412cc7cSeric { 5651412cc7cSeric (void) expand("$l", buf, &buf[sizeof buf - 1]); 5661412cc7cSeric fprintf(fp, "%s\n", buf); 5671412cc7cSeric } 56840e4ab56Seric 56913bbc08cSeric /* 57013bbc08cSeric ** Output all header lines 57113bbc08cSeric */ 57213bbc08cSeric 5736328bdf7Seric for (h = Header; h != NULL; h = h->h_link) 5746328bdf7Seric { 57513bbc08cSeric register char *p; 57613bbc08cSeric 5779e9163a0Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 5785a0dcb5fSeric { 5795a0dcb5fSeric p = ")><("; /* can't happen (I hope) */ 5805a0dcb5fSeric goto checkfrom; 5815a0dcb5fSeric } 5824ae18d0eSeric if (bitset(H_DEFAULT, h->h_flags)) 5834ae18d0eSeric { 584db8841e9Seric (void) expand(h->h_value, buf, &buf[sizeof buf]); 5854ae18d0eSeric p = buf; 5864ae18d0eSeric } 5874ae18d0eSeric else 5884ae18d0eSeric p = h->h_value; 5899e9163a0Seric if (*p == '\0') 5909e9163a0Seric continue; 5914ae18d0eSeric fprintf(fp, "%s: %s\n", capitalize(h->h_field), p); 5926328bdf7Seric h->h_flags |= H_USED; 5936328bdf7Seric anyheader = TRUE; 5945a0dcb5fSeric 5955a0dcb5fSeric /* hack, hack -- output Original-From field if different */ 5965a0dcb5fSeric checkfrom: 5975a0dcb5fSeric if (strcmp(h->h_field, "from") == 0) 5985a0dcb5fSeric { 5995a0dcb5fSeric extern char *hvalue(); 6005a0dcb5fSeric char *ofrom = hvalue("original-from"); 6015a0dcb5fSeric 6025a0dcb5fSeric if (ofrom != NULL && strcmp(p, ofrom) != 0) 6035a0dcb5fSeric fprintf(fp, "Original-From: %s\n", ofrom); 6045a0dcb5fSeric } 6056328bdf7Seric } 6066328bdf7Seric if (anyheader) 6076328bdf7Seric fprintf(fp, "\n"); 6086328bdf7Seric 60913bbc08cSeric /* 61013bbc08cSeric ** Output the body of the message 61113bbc08cSeric */ 61213bbc08cSeric 613b7902a1dSeric rewind(TempFile); 614b7902a1dSeric while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, TempFile)) > 0) 615db8841e9Seric (void) fwrite(buf, 1, i, fp); 6166328bdf7Seric 61754aa2b0fSeric if (ferror(fp) && errno != EPIPE) 61825a99e2eSeric { 6196328bdf7Seric syserr("putmessage: write error"); 62025a99e2eSeric setstat(EX_IOERR); 62125a99e2eSeric } 62254aa2b0fSeric errno = 0; 62325a99e2eSeric } 62425a99e2eSeric /* 62525a99e2eSeric ** MAILFILE -- Send a message to a file. 62625a99e2eSeric ** 627*f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 628*f129ec7dSeric ** bits, sendmail will try to become the owner of that file 629*f129ec7dSeric ** rather than the real user. Obviously, this only works if 630*f129ec7dSeric ** sendmail runs as root. 631*f129ec7dSeric ** 63225a99e2eSeric ** Parameters: 63325a99e2eSeric ** filename -- the name of the file to send to. 63425a99e2eSeric ** 63525a99e2eSeric ** Returns: 63625a99e2eSeric ** The exit code associated with the operation. 63725a99e2eSeric ** 63825a99e2eSeric ** Side Effects: 63925a99e2eSeric ** none. 64025a99e2eSeric */ 64125a99e2eSeric 64225a99e2eSeric mailfile(filename) 64325a99e2eSeric char *filename; 64425a99e2eSeric { 64525a99e2eSeric register FILE *f; 64632d19d43Seric register int pid; 64725a99e2eSeric 64832d19d43Seric /* 64932d19d43Seric ** Fork so we can change permissions here. 65032d19d43Seric ** Note that we MUST use fork, not vfork, because of 65132d19d43Seric ** the complications of calling subroutines, etc. 65232d19d43Seric */ 65332d19d43Seric 65432d19d43Seric DOFORK(fork); 65532d19d43Seric 65632d19d43Seric if (pid < 0) 65732d19d43Seric return (EX_OSERR); 65832d19d43Seric else if (pid == 0) 65932d19d43Seric { 66032d19d43Seric /* child -- actually write to file */ 661*f129ec7dSeric struct stat stb; 662*f129ec7dSeric 6630984da9fSeric (void) signal(SIGINT, SIG_DFL); 6640984da9fSeric (void) signal(SIGHUP, SIG_DFL); 6650984da9fSeric (void) signal(SIGTERM, SIG_DFL); 666*f129ec7dSeric umask(OldUmask); 667*f129ec7dSeric if (stat(filename, &stb) < 0) 668*f129ec7dSeric stb.st_mode = 0; 669*f129ec7dSeric if (bitset(0111, stb.st_mode)) 670*f129ec7dSeric exit(EX_CANTCREAT); 671*f129ec7dSeric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 672*f129ec7dSeric (void) setgid(getgid()); 673*f129ec7dSeric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 674*f129ec7dSeric (void) setuid(getuid()); 67525a99e2eSeric f = fopen(filename, "a"); 67625a99e2eSeric if (f == NULL) 67732d19d43Seric exit(EX_CANTCREAT); 67825a99e2eSeric 67940e4ab56Seric putmessage(f, Mailer[1]); 68025a99e2eSeric fputs("\n", f); 681db8841e9Seric (void) fclose(f); 68232d19d43Seric (void) fflush(stdout); 68332d19d43Seric exit(EX_OK); 68413bbc08cSeric /*NOTREACHED*/ 68532d19d43Seric } 68632d19d43Seric else 68732d19d43Seric { 68832d19d43Seric /* parent -- wait for exit status */ 68932d19d43Seric register int i; 69032d19d43Seric auto int stat; 69132d19d43Seric 69232d19d43Seric while ((i = wait(&stat)) != pid) 69332d19d43Seric { 69432d19d43Seric if (i < 0) 69532d19d43Seric { 69632d19d43Seric stat = EX_OSERR << 8; 69732d19d43Seric break; 69832d19d43Seric } 69932d19d43Seric } 7000984da9fSeric if ((stat & 0377) != 0) 7010984da9fSeric stat = EX_UNAVAILABLE << 8; 70232d19d43Seric return ((stat >> 8) & 0377); 70332d19d43Seric } 70425a99e2eSeric } 705