1294Seric # include <signal.h> 24123Seric # include <errno.h> 34327Seric # include <sys/types.h> 44327Seric # include <sys/stat.h> 53310Seric # include "sendmail.h" 6294Seric # ifdef LOG 72774Seric # include <syslog.h> 8294Seric # endif LOG 9294Seric 10*4596Seric static char SccsId[] = "@(#)deliver.c 3.49 10/23/81"; 11405Seric 12294Seric /* 134315Seric ** DELIVER -- Deliver a message to a list of addresses. 14294Seric ** 154315Seric ** This routine delivers to everyone on the same host as the 164315Seric ** user on the head of the list. It is clever about mailers 174315Seric ** that don't handle multiple users. It is NOT guaranteed 184315Seric ** that it will deliver to all these addresses however -- so 194315Seric ** deliver should be called once for each address on the 204315Seric ** list. 214315Seric ** 22294Seric ** Parameters: 234315Seric ** to -- head of the address list to deliver to. 24294Seric ** editfcn -- if non-NULL, we want to call this function 25294Seric ** to output the letter (instead of just out- 26294Seric ** putting it raw). 27294Seric ** 28294Seric ** Returns: 29294Seric ** zero -- successfully delivered. 30294Seric ** else -- some failure, see ExitStat for more info. 31294Seric ** 32294Seric ** Side Effects: 33294Seric ** The standard input is passed off to someone. 34294Seric */ 35294Seric 36294Seric deliver(to, editfcn) 372968Seric ADDRESS *to; 38294Seric int (*editfcn)(); 39294Seric { 404452Seric char *host; /* host being sent to */ 414452Seric char *user; /* user being sent to */ 42294Seric char **pvp; 433233Seric register char **mvp; 443233Seric register char *p; 454452Seric register struct mailer *m; /* mailer for this recipient */ 46294Seric register int i; 472898Seric extern putmessage(); 482968Seric extern bool checkcompat(); 493233Seric char *pv[MAXPV+1]; 504452Seric char tobuf[MAXLINE]; /* text line of to people */ 513233Seric char buf[MAXNAME]; 524397Seric ADDRESS *ctladdr; 534397Seric extern ADDRESS *getctladdr(); 544452Seric char tfrombuf[MAXNAME]; /* translated from person */ 554452Seric extern char **prescan(); 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", 64*4596Seric 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 76*4596Seric 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 */ 156*4596Seric 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.... */ 207*4596Seric Stat.stat_nt[to->q_mailer->m_mno]++; 208*4596Seric 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 220*4596Seric 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 2854488Seric errno = 0; 2863233Seric return (i); 2873233Seric } 2883233Seric /* 2894214Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 2904214Seric ** 2914214Seric ** This MUST be a macro, since after a vfork we are running 2924214Seric ** two processes on the same stack!!! 2934214Seric ** 2944214Seric ** Parameters: 2954214Seric ** none. 2964214Seric ** 2974214Seric ** Returns: 2984214Seric ** From a macro??? You've got to be kidding! 2994214Seric ** 3004214Seric ** Side Effects: 3014214Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 3024214Seric ** pid of child in parent, zero in child. 3034214Seric ** -1 on unrecoverable error. 3044214Seric ** 3054214Seric ** Notes: 3064214Seric ** I'm awfully sorry this looks so awful. That's 3074214Seric ** vfork for you..... 3084214Seric */ 3094214Seric 3104214Seric # define NFORKTRIES 5 3114214Seric # ifdef VFORK 3124214Seric # define XFORK vfork 3134214Seric # else VFORK 3144214Seric # define XFORK fork 3154214Seric # endif VFORK 3164214Seric 3174214Seric # define DOFORK(fORKfN) \ 3184214Seric {\ 3194214Seric register int i;\ 3204214Seric \ 3214214Seric for (i = NFORKTRIES; i-- > 0; )\ 3224214Seric {\ 3234214Seric pid = fORKfN();\ 3244214Seric if (pid >= 0)\ 3254214Seric break;\ 3264214Seric sleep((unsigned) NFORKTRIES - i);\ 3274214Seric }\ 3284214Seric } 3294214Seric /* 3303233Seric ** SENDOFF -- send off call to mailer & collect response. 3313233Seric ** 3323233Seric ** Parameters: 3333233Seric ** m -- mailer descriptor. 3343233Seric ** pvp -- parameter vector to send to it. 3353233Seric ** editfcn -- function to pipe it through. 3364397Seric ** ctladdr -- an address pointer controlling the 3374397Seric ** user/groupid etc. of the mailer. 3383233Seric ** 3393233Seric ** Returns: 3403233Seric ** exit status of mailer. 3413233Seric ** 3423233Seric ** Side Effects: 3433233Seric ** none. 3443233Seric */ 3453233Seric 3464397Seric sendoff(m, pvp, editfcn, ctladdr) 3473233Seric struct mailer *m; 3483233Seric char **pvp; 3493233Seric int (*editfcn)(); 3504397Seric ADDRESS *ctladdr; 3513233Seric { 3523233Seric auto int st; 3533233Seric register int i; 3543233Seric int pid; 3553233Seric int pvect[2]; 3563233Seric FILE *mfile; 3573233Seric extern putmessage(); 3583233Seric extern FILE *fdopen(); 3593233Seric 3603233Seric # ifdef DEBUG 3613233Seric if (Debug) 362294Seric { 3633233Seric printf("Sendoff:\n"); 3643233Seric printav(pvp); 365294Seric } 3663233Seric # endif DEBUG 3674488Seric errno = 0; 3683233Seric 3692898Seric /* create a pipe to shove the mail through */ 3702898Seric if (pipe(pvect) < 0) 371294Seric { 372294Seric syserr("pipe"); 373294Seric return (-1); 374294Seric } 3754214Seric DOFORK(XFORK); 3764327Seric /* pid is set by DOFORK */ 377294Seric if (pid < 0) 378294Seric { 379294Seric syserr("Cannot fork"); 3804082Seric (void) close(pvect[0]); 3814082Seric (void) close(pvect[1]); 382294Seric return (-1); 383294Seric } 384294Seric else if (pid == 0) 385294Seric { 386294Seric /* child -- set up input & exec mailer */ 3871621Seric /* make diagnostic output be standard output */ 3884477Seric (void) signal(SIGINT, SIG_IGN); 3894477Seric (void) signal(SIGHUP, SIG_IGN); 3904215Seric (void) signal(SIGTERM, SIG_DFL); 3914082Seric (void) close(2); 3924082Seric (void) dup(1); 3934082Seric (void) close(0); 3942898Seric if (dup(pvect[0]) < 0) 395294Seric { 3962898Seric syserr("Cannot dup to zero!"); 3972898Seric _exit(EX_OSERR); 398294Seric } 3994082Seric (void) close(pvect[0]); 4004082Seric (void) close(pvect[1]); 4012968Seric if (!bitset(M_RESTR, m->m_flags)) 4024215Seric { 4034417Seric if (ctladdr->q_uid == 0) 4044417Seric { 4054417Seric extern int DefUid, DefGid; 4064415Seric 4074417Seric (void) setgid(DefGid); 4084417Seric (void) setuid(DefUid); 4094417Seric } 4104417Seric else 4114415Seric { 4124417Seric (void) setgid(ctladdr->q_gid); 4134417Seric (void) setuid(ctladdr->q_uid); 4144415Seric } 4154215Seric } 4162774Seric # ifndef VFORK 4172774Seric /* 4182774Seric ** We have to be careful with vfork - we can't mung up the 4192774Seric ** memory but we don't want the mailer to inherit any extra 4202774Seric ** open files. Chances are the mailer won't 4212774Seric ** care about an extra file, but then again you never know. 4222774Seric ** Actually, we would like to close(fileno(pwf)), but it's 4232774Seric ** declared static so we can't. But if we fclose(pwf), which 4242774Seric ** is what endpwent does, it closes it in the parent too and 4252774Seric ** the next getpwnam will be slower. If you have a weird 4262774Seric ** mailer that chokes on the extra file you should do the 4272774Seric ** endpwent(). 4282774Seric ** 4292774Seric ** Similar comments apply to log. However, openlog is 4302774Seric ** clever enough to set the FIOCLEX mode on the file, 4312774Seric ** so it will be closed automatically on the exec. 4322774Seric */ 4332774Seric 4342774Seric endpwent(); 435294Seric # ifdef LOG 4362089Seric closelog(); 437294Seric # endif LOG 4382774Seric # endif VFORK 439294Seric execv(m->m_mailer, pvp); 440294Seric /* syserr fails because log is closed */ 441294Seric /* syserr("Cannot exec %s", m->m_mailer); */ 4424214Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 4434082Seric (void) fflush(stdout); 4441619Seric _exit(EX_UNAVAILABLE); 445294Seric } 446294Seric 4472898Seric /* write out message to mailer */ 4484082Seric (void) close(pvect[0]); 4494123Seric (void) signal(SIGPIPE, SIG_IGN); 4502898Seric mfile = fdopen(pvect[1], "w"); 4512898Seric if (editfcn == NULL) 4522898Seric editfcn = putmessage; 4532898Seric (*editfcn)(mfile, m); 4544082Seric (void) fclose(mfile); 455294Seric 456294Seric /* 457294Seric ** Wait for child to die and report status. 458294Seric ** We should never get fatal errors (e.g., segmentation 459294Seric ** violation), so we report those specially. For other 460294Seric ** errors, we choose a status message (into statmsg), 461294Seric ** and if it represents an error, we print it. 462294Seric */ 463294Seric 464294Seric while ((i = wait(&st)) > 0 && i != pid) 465294Seric continue; 466294Seric if (i < 0) 467294Seric { 468294Seric syserr("wait"); 469294Seric return (-1); 470294Seric } 471294Seric if ((st & 0377) != 0) 472294Seric { 473294Seric syserr("%s: stat %o", pvp[0], st); 4741597Seric ExitStat = EX_UNAVAILABLE; 475294Seric return (-1); 476294Seric } 477294Seric i = (st >> 8) & 0377; 4782343Seric giveresponse(i, TRUE, m); 479294Seric return (i); 480294Seric } 481294Seric /* 482294Seric ** GIVERESPONSE -- Interpret an error response from a mailer 483294Seric ** 484294Seric ** Parameters: 485294Seric ** stat -- the status code from the mailer (high byte 486294Seric ** only; core dumps must have been taken care of 487294Seric ** already). 488294Seric ** force -- if set, force an error message output, even 489294Seric ** if the mailer seems to like to print its own 490294Seric ** messages. 491294Seric ** m -- the mailer descriptor for this mailer. 492294Seric ** 493294Seric ** Returns: 4944082Seric ** none. 495294Seric ** 496294Seric ** Side Effects: 4971518Seric ** Errors may be incremented. 498294Seric ** ExitStat may be set. 499294Seric */ 500294Seric 501294Seric giveresponse(stat, force, m) 502294Seric int stat; 503294Seric int force; 504294Seric register struct mailer *m; 505294Seric { 506294Seric register char *statmsg; 507294Seric extern char *SysExMsg[]; 508294Seric register int i; 509294Seric extern int N_SysEx; 5101624Seric char buf[30]; 511294Seric 5124315Seric /* 5134315Seric ** Compute status message from code. 5144315Seric */ 5154315Seric 516294Seric i = stat - EX__BASE; 517294Seric if (i < 0 || i > N_SysEx) 518294Seric statmsg = NULL; 519294Seric else 520294Seric statmsg = SysExMsg[i]; 521294Seric if (stat == 0) 5224065Seric { 5234194Seric if (bitset(M_LOCAL, m->m_flags)) 5244161Seric statmsg = "delivered"; 5254161Seric else 5264161Seric statmsg = "queued"; 5274065Seric if (Verbose) 5284166Seric message(Arpa_Info, statmsg); 5294065Seric } 530294Seric else 531294Seric { 5321518Seric Errors++; 533294Seric if (statmsg == NULL && m->m_badstat != 0) 534294Seric { 535294Seric stat = m->m_badstat; 536294Seric i = stat - EX__BASE; 537294Seric # ifdef DEBUG 538294Seric if (i < 0 || i >= N_SysEx) 539294Seric syserr("Bad m_badstat %d", stat); 540294Seric else 541294Seric # endif DEBUG 542294Seric statmsg = SysExMsg[i]; 543294Seric } 544294Seric if (statmsg == NULL) 545294Seric usrerr("unknown mailer response %d", stat); 5464065Seric else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 547294Seric usrerr("%s", statmsg); 548294Seric } 549294Seric 550294Seric /* 551294Seric ** Final cleanup. 552294Seric ** Log a record of the transaction. Compute the new 553294Seric ** ExitStat -- if we already had an error, stick with 554294Seric ** that. 555294Seric */ 556294Seric 5571624Seric if (statmsg == NULL) 5581624Seric { 5594082Seric (void) sprintf(buf, "error %d", stat); 5601624Seric statmsg = buf; 5611624Seric } 5621624Seric 563294Seric # ifdef LOG 5642774Seric syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg); 565294Seric # endif LOG 5661389Seric setstat(stat); 567294Seric } 568294Seric /* 5692898Seric ** PUTMESSAGE -- output a message to the final mailer. 570294Seric ** 5712898Seric ** This routine takes care of recreating the header from the 5722898Seric ** in-core copy, etc. 573294Seric ** 574294Seric ** Parameters: 5752898Seric ** fp -- file to output onto. 5762898Seric ** m -- a mailer descriptor. 577294Seric ** 578294Seric ** Returns: 5792898Seric ** none. 580294Seric ** 581294Seric ** Side Effects: 5822898Seric ** The message is written onto fp. 583294Seric */ 584294Seric 5852898Seric putmessage(fp, m) 5862898Seric FILE *fp; 5872898Seric struct mailer *m; 588294Seric { 5892898Seric char buf[BUFSIZ]; 5902898Seric register int i; 5914315Seric register HDR *h; 5922898Seric extern char *arpadate(); 5932898Seric bool anyheader = FALSE; 5943044Seric extern char *capitalize(); 5954370Seric extern char *hvalue(); 5964370Seric extern bool samefrom(); 5974447Seric char *of_line; 598294Seric 5994315Seric /* 6004315Seric ** Output "From" line unless supressed 6014315Seric */ 6024315Seric 6033186Seric if (!bitset(M_NHDR, m->m_flags)) 6044205Seric { 6054205Seric (void) expand("$l", buf, &buf[sizeof buf - 1]); 6064205Seric fprintf(fp, "%s\n", buf); 6074205Seric } 6083186Seric 6094315Seric /* 6104315Seric ** Output all header lines 6114315Seric */ 6124315Seric 6134447Seric of_line = hvalue("original-from"); 6142898Seric for (h = Header; h != NULL; h = h->h_link) 6151828Seric { 6164315Seric register char *p; 6174370Seric char *origfrom = OrigFrom; 6184447Seric bool nooutput; 6194315Seric 6204447Seric nooutput = FALSE; 6213389Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 6224209Seric { 6234209Seric p = ")><("; /* can't happen (I hope) */ 6244447Seric nooutput = TRUE; 6254209Seric } 6264447Seric 6274447Seric /* use From: line from message if generated is the same */ 6284370Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL && 6294447Seric strcmp(m->m_from, "$f") == 0 && of_line == NULL) 6303385Seric { 6314370Seric p = origfrom; 6324370Seric origfrom = NULL; 6334370Seric } 6344370Seric else if (bitset(H_DEFAULT, h->h_flags)) 6354370Seric { 6364082Seric (void) expand(h->h_value, buf, &buf[sizeof buf]); 6373385Seric p = buf; 6383385Seric } 6392898Seric else 6403385Seric p = h->h_value; 6414447Seric if (p == NULL || *p == '\0') 6423389Seric continue; 6434209Seric 6444209Seric /* hack, hack -- output Original-From field if different */ 6454447Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL) 6464209Seric { 6474447Seric /* output new Original-From line if needed */ 6484447Seric if (of_line == NULL && !samefrom(p, origfrom)) 6494447Seric { 6504447Seric fprintf(fp, "Original-From: %s\n", origfrom); 6514447Seric anyheader = TRUE; 6524447Seric } 6534447Seric if (of_line != NULL && !nooutput && samefrom(p, of_line)) 6544447Seric { 6554447Seric /* delete Original-From: line if redundant */ 6564447Seric p = of_line; 6574447Seric of_line = NULL; 6584447Seric } 6594447Seric } 6604447Seric else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL) 6614447Seric nooutput = TRUE; 6624447Seric 6634447Seric /* finally, output the header line */ 6644447Seric if (!nooutput) 6654447Seric { 6664447Seric fprintf(fp, "%s: %s\n", capitalize(h->h_field), p); 6674447Seric h->h_flags |= H_USED; 6684370Seric anyheader = TRUE; 6694209Seric } 6702898Seric } 6712898Seric if (anyheader) 6722898Seric fprintf(fp, "\n"); 6732898Seric 6744315Seric /* 6754315Seric ** Output the body of the message 6764315Seric */ 6774315Seric 6784452Seric if (TempFile != NULL) 6794452Seric { 6804452Seric rewind(TempFile); 6814452Seric while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, TempFile)) > 0) 6824452Seric (void) fwrite(buf, 1, i, fp); 6832898Seric 6844452Seric if (ferror(TempFile)) 6854452Seric { 6864452Seric syserr("putmessage: read error"); 6874452Seric setstat(EX_IOERR); 6884452Seric } 6894452Seric } 6904452Seric 6914452Seric fflush(fp); 6924123Seric if (ferror(fp) && errno != EPIPE) 693294Seric { 6942898Seric syserr("putmessage: write error"); 695294Seric setstat(EX_IOERR); 696294Seric } 6974123Seric errno = 0; 698294Seric } 699294Seric /* 7004370Seric ** SAMEFROM -- tell if two text addresses represent the same from address. 7014370Seric ** 7024370Seric ** Parameters: 7034370Seric ** ifrom -- internally generated form of from address. 7044370Seric ** efrom -- external form of from address. 7054370Seric ** 7064370Seric ** Returns: 7074370Seric ** TRUE -- if they convey the same info. 7084370Seric ** FALSE -- if any information has been lost. 7094370Seric ** 7104370Seric ** Side Effects: 7114370Seric ** none. 7124370Seric */ 7134370Seric 7144370Seric bool 7154370Seric samefrom(ifrom, efrom) 7164370Seric char *ifrom; 7174370Seric char *efrom; 7184370Seric { 7194447Seric register char *p; 7204447Seric char buf[MAXNAME + 4]; 7214447Seric 7224447Seric # ifdef DEBUG 7234447Seric if (Debug > 7) 7244447Seric printf("samefrom(%s,%s)-->", ifrom, efrom); 7254447Seric # endif DEBUG 7264447Seric if (strcmp(ifrom, efrom) == 0) 7274447Seric goto success; 7284447Seric p = index(ifrom, '@'); 7294447Seric if (p == NULL) 7304447Seric goto failure; 7314447Seric *p = '\0'; 7324447Seric strcpy(buf, ifrom); 7334447Seric strcat(buf, " at "); 7344447Seric *p++ = '@'; 7354447Seric strcat(buf, p); 7364447Seric if (strcmp(buf, efrom) == 0) 7374447Seric goto success; 7384447Seric 7394447Seric failure: 7404447Seric # ifdef DEBUG 7414447Seric if (Debug > 7) 7424447Seric printf("FALSE\n"); 7434447Seric # endif DEBUG 7444447Seric return (FALSE); 7454447Seric 7464447Seric success: 7474447Seric # ifdef DEBUG 7484447Seric if (Debug > 7) 7494447Seric printf("TRUE\n"); 7504447Seric # endif DEBUG 7514447Seric return (TRUE); 7524370Seric } 7534370Seric /* 754294Seric ** MAILFILE -- Send a message to a file. 755294Seric ** 7564327Seric ** If the file has the setuid/setgid bits set, but NO execute 7574327Seric ** bits, sendmail will try to become the owner of that file 7584327Seric ** rather than the real user. Obviously, this only works if 7594327Seric ** sendmail runs as root. 7604327Seric ** 761294Seric ** Parameters: 762294Seric ** filename -- the name of the file to send to. 7634397Seric ** ctladdr -- the controlling address header -- includes 7644397Seric ** the userid/groupid to be when sending. 765294Seric ** 766294Seric ** Returns: 767294Seric ** The exit code associated with the operation. 768294Seric ** 769294Seric ** Side Effects: 770294Seric ** none. 771294Seric */ 772294Seric 7734397Seric mailfile(filename, ctladdr) 774294Seric char *filename; 7754397Seric ADDRESS *ctladdr; 776294Seric { 777294Seric register FILE *f; 7784214Seric register int pid; 779294Seric 7804214Seric /* 7814214Seric ** Fork so we can change permissions here. 7824214Seric ** Note that we MUST use fork, not vfork, because of 7834214Seric ** the complications of calling subroutines, etc. 7844214Seric */ 7854067Seric 7864214Seric DOFORK(fork); 7874214Seric 7884214Seric if (pid < 0) 7894214Seric return (EX_OSERR); 7904214Seric else if (pid == 0) 7914214Seric { 7924214Seric /* child -- actually write to file */ 7934327Seric struct stat stb; 7944417Seric extern int DefUid, DefGid; 7954327Seric 7964215Seric (void) signal(SIGINT, SIG_DFL); 7974215Seric (void) signal(SIGHUP, SIG_DFL); 7984215Seric (void) signal(SIGTERM, SIG_DFL); 7994327Seric umask(OldUmask); 8004327Seric if (stat(filename, &stb) < 0) 8014431Seric stb.st_mode = 0666; 8024327Seric if (bitset(0111, stb.st_mode)) 8034327Seric exit(EX_CANTCREAT); 8044401Seric if (ctladdr == NULL) 8054401Seric ctladdr = &From; 8064327Seric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 8074417Seric { 8084417Seric if (ctladdr->q_uid == 0) 8094417Seric (void) setgid(DefGid); 8104417Seric else 8114417Seric (void) setgid(ctladdr->q_gid); 8124417Seric } 8134327Seric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 8144417Seric { 8154417Seric if (ctladdr->q_uid == 0) 8164417Seric (void) setuid(DefUid); 8174417Seric else 8184417Seric (void) setuid(ctladdr->q_uid); 8194417Seric } 8204214Seric f = fopen(filename, "a"); 8214214Seric if (f == NULL) 8224214Seric exit(EX_CANTCREAT); 8234214Seric 8244214Seric putmessage(f, Mailer[1]); 8254214Seric fputs("\n", f); 8264214Seric (void) fclose(f); 8274214Seric (void) fflush(stdout); 8284417Seric 8294417Seric /* reset ISUID & ISGID bits */ 8304417Seric (void) chmod(filename, stb.st_mode); 8314214Seric exit(EX_OK); 8324315Seric /*NOTREACHED*/ 8334214Seric } 8344214Seric else 8354214Seric { 8364214Seric /* parent -- wait for exit status */ 8374214Seric register int i; 8384214Seric auto int stat; 8394214Seric 8404214Seric while ((i = wait(&stat)) != pid) 8414214Seric { 8424214Seric if (i < 0) 8434214Seric { 8444214Seric stat = EX_OSERR << 8; 8454214Seric break; 8464214Seric } 8474214Seric } 8484215Seric if ((stat & 0377) != 0) 8494215Seric stat = EX_UNAVAILABLE << 8; 8504214Seric return ((stat >> 8) & 0377); 8514214Seric } 852294Seric } 8534550Seric /* 8544550Seric ** SENDALL -- actually send all the messages. 8554550Seric ** 8564550Seric ** Parameters: 8574550Seric ** verifyonly -- if set, only give verification messages. 8584550Seric ** 8594550Seric ** Returns: 8604550Seric ** none. 8614550Seric ** 8624550Seric ** Side Effects: 8634550Seric ** Scans the send lists and sends everything it finds. 8644550Seric */ 8654550Seric 8664550Seric sendall(verifyonly) 8674550Seric bool verifyonly; 8684550Seric { 8694550Seric register int i; 8704550Seric typedef int (*fnptr)(); 8714550Seric 8724550Seric for (i = 0; Mailer[i] != NULL; i++) 8734550Seric { 8744550Seric ADDRESS *q; 8754550Seric 8764550Seric for (q = Mailer[i]->m_sendq; q != NULL; q = q->q_next) 8774550Seric { 8784550Seric if (verifyonly) 8794550Seric { 8804550Seric To = q->q_paddr; 8814550Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 8824550Seric { 883*4596Seric if (bitset(M_LOCAL, q->q_mailer->m_flags)) 8844550Seric message(Arpa_Info, "deliverable"); 8854550Seric else 8864550Seric message(Arpa_Info, "queueable"); 8874550Seric } 8884550Seric } 8894550Seric else 8904550Seric (void) deliver(q, (fnptr) NULL); 8914550Seric } 8924550Seric } 8934550Seric } 894