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*4452Seric static char SccsId[] = "@(#)deliver.c 3.45 10/02/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 { 40*4452Seric char *host; /* host being sent to */ 41*4452Seric char *user; /* user being sent to */ 42294Seric char **pvp; 433233Seric register char **mvp; 443233Seric register char *p; 45*4452Seric register struct mailer *m; /* mailer for this recipient */ 46294Seric register int i; 472898Seric extern putmessage(); 482968Seric extern bool checkcompat(); 493233Seric char *pv[MAXPV+1]; 50*4452Seric char tobuf[MAXLINE]; /* text line of to people */ 513233Seric char buf[MAXNAME]; 524397Seric ADDRESS *ctladdr; 534397Seric extern ADDRESS *getctladdr(); 54*4452Seric char tfrombuf[MAXNAME]; /* translated from person */ 55*4452Seric extern char **prescan(); 56294Seric 574311Seric if (!ForceMail && bitset(QDONTSEND, to->q_flags)) 583233Seric return (0); 59294Seric 60294Seric # ifdef DEBUG 61294Seric if (Debug) 623233Seric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 633233Seric to->q_mailer, to->q_host, to->q_user); 64294Seric # endif DEBUG 65294Seric 66294Seric /* 673233Seric ** Do initial argv setup. 683233Seric ** Insert the mailer name. Notice that $x expansion is 693233Seric ** NOT done on the mailer name. Then, if the mailer has 703233Seric ** a picky -f flag, we insert it as appropriate. This 713233Seric ** code does not check for 'pv' overflow; this places a 723233Seric ** manifest lower limit of 4 for MAXPV. 732968Seric */ 742968Seric 753233Seric m = Mailer[to->q_mailer]; 763233Seric host = to->q_host; 77*4452Seric 78*4452Seric /* rewrite from address, using rewriting rules */ 79*4452Seric (void) expand(m->m_from, buf, &buf[sizeof buf - 1]); 80*4452Seric mvp = prescan(buf, '\0'); 81*4452Seric if (mvp == NULL) 82*4452Seric { 83*4452Seric syserr("bad mailer from translate \"%s\"", buf); 84*4452Seric return (EX_SOFTWARE); 85*4452Seric } 86*4452Seric rewrite(mvp, 2); 87*4452Seric cataddr(mvp, tfrombuf, sizeof tfrombuf); 88*4452Seric 89*4452Seric define('g', tfrombuf); /* translated sender address */ 903233Seric define('h', host); /* to host */ 913233Seric Errors = 0; 923233Seric errno = 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 */ 1564397Seric if (to->q_mailer == MN_PROG) 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.... */ 2074283Seric Stat.stat_nt[to->q_mailer]++; 2084283Seric Stat.stat_bt[to->q_mailer] += 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 2204194Seric if (m == Mailer[MN_LOCAL]) 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 2853233Seric return (i); 2863233Seric } 2873233Seric /* 2884214Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 2894214Seric ** 2904214Seric ** This MUST be a macro, since after a vfork we are running 2914214Seric ** two processes on the same stack!!! 2924214Seric ** 2934214Seric ** Parameters: 2944214Seric ** none. 2954214Seric ** 2964214Seric ** Returns: 2974214Seric ** From a macro??? You've got to be kidding! 2984214Seric ** 2994214Seric ** Side Effects: 3004214Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 3014214Seric ** pid of child in parent, zero in child. 3024214Seric ** -1 on unrecoverable error. 3034214Seric ** 3044214Seric ** Notes: 3054214Seric ** I'm awfully sorry this looks so awful. That's 3064214Seric ** vfork for you..... 3074214Seric */ 3084214Seric 3094214Seric # define NFORKTRIES 5 3104214Seric # ifdef VFORK 3114214Seric # define XFORK vfork 3124214Seric # else VFORK 3134214Seric # define XFORK fork 3144214Seric # endif VFORK 3154214Seric 3164214Seric # define DOFORK(fORKfN) \ 3174214Seric {\ 3184214Seric register int i;\ 3194214Seric \ 3204214Seric for (i = NFORKTRIES; i-- > 0; )\ 3214214Seric {\ 3224214Seric pid = fORKfN();\ 3234214Seric if (pid >= 0)\ 3244214Seric break;\ 3254214Seric sleep((unsigned) NFORKTRIES - i);\ 3264214Seric }\ 3274214Seric } 3284214Seric /* 3293233Seric ** SENDOFF -- send off call to mailer & collect response. 3303233Seric ** 3313233Seric ** Parameters: 3323233Seric ** m -- mailer descriptor. 3333233Seric ** pvp -- parameter vector to send to it. 3343233Seric ** editfcn -- function to pipe it through. 3354397Seric ** ctladdr -- an address pointer controlling the 3364397Seric ** user/groupid etc. of the mailer. 3373233Seric ** 3383233Seric ** Returns: 3393233Seric ** exit status of mailer. 3403233Seric ** 3413233Seric ** Side Effects: 3423233Seric ** none. 3433233Seric */ 3443233Seric 3454397Seric sendoff(m, pvp, editfcn, ctladdr) 3463233Seric struct mailer *m; 3473233Seric char **pvp; 3483233Seric int (*editfcn)(); 3494397Seric ADDRESS *ctladdr; 3503233Seric { 3513233Seric auto int st; 3523233Seric register int i; 3533233Seric int pid; 3543233Seric int pvect[2]; 3553233Seric FILE *mfile; 3563233Seric extern putmessage(); 3573233Seric extern FILE *fdopen(); 3583233Seric 3593233Seric # ifdef DEBUG 3603233Seric if (Debug) 361294Seric { 3623233Seric printf("Sendoff:\n"); 3633233Seric printav(pvp); 364294Seric } 3653233Seric # endif DEBUG 3663233Seric 3672898Seric /* create a pipe to shove the mail through */ 3682898Seric if (pipe(pvect) < 0) 369294Seric { 370294Seric syserr("pipe"); 371294Seric return (-1); 372294Seric } 3734214Seric DOFORK(XFORK); 3744327Seric /* pid is set by DOFORK */ 375294Seric if (pid < 0) 376294Seric { 377294Seric syserr("Cannot fork"); 3784082Seric (void) close(pvect[0]); 3794082Seric (void) close(pvect[1]); 380294Seric return (-1); 381294Seric } 382294Seric else if (pid == 0) 383294Seric { 384294Seric /* child -- set up input & exec mailer */ 3851621Seric /* make diagnostic output be standard output */ 3864215Seric (void) signal(SIGINT, SIG_DFL); 3874215Seric (void) signal(SIGHUP, SIG_DFL); 3884215Seric (void) signal(SIGTERM, SIG_DFL); 3894082Seric (void) close(2); 3904082Seric (void) dup(1); 3914082Seric (void) close(0); 3922898Seric if (dup(pvect[0]) < 0) 393294Seric { 3942898Seric syserr("Cannot dup to zero!"); 3952898Seric _exit(EX_OSERR); 396294Seric } 3974082Seric (void) close(pvect[0]); 3984082Seric (void) close(pvect[1]); 3992968Seric if (!bitset(M_RESTR, m->m_flags)) 4004215Seric { 4014417Seric if (ctladdr->q_uid == 0) 4024417Seric { 4034417Seric extern int DefUid, DefGid; 4044415Seric 4054417Seric (void) setgid(DefGid); 4064417Seric (void) setuid(DefUid); 4074417Seric } 4084417Seric else 4094415Seric { 4104417Seric (void) setgid(ctladdr->q_gid); 4114417Seric (void) setuid(ctladdr->q_uid); 4124415Seric } 4134215Seric } 4142774Seric # ifndef VFORK 4152774Seric /* 4162774Seric ** We have to be careful with vfork - we can't mung up the 4172774Seric ** memory but we don't want the mailer to inherit any extra 4182774Seric ** open files. Chances are the mailer won't 4192774Seric ** care about an extra file, but then again you never know. 4202774Seric ** Actually, we would like to close(fileno(pwf)), but it's 4212774Seric ** declared static so we can't. But if we fclose(pwf), which 4222774Seric ** is what endpwent does, it closes it in the parent too and 4232774Seric ** the next getpwnam will be slower. If you have a weird 4242774Seric ** mailer that chokes on the extra file you should do the 4252774Seric ** endpwent(). 4262774Seric ** 4272774Seric ** Similar comments apply to log. However, openlog is 4282774Seric ** clever enough to set the FIOCLEX mode on the file, 4292774Seric ** so it will be closed automatically on the exec. 4302774Seric */ 4312774Seric 4322774Seric endpwent(); 433294Seric # ifdef LOG 4342089Seric closelog(); 435294Seric # endif LOG 4362774Seric # endif VFORK 437294Seric execv(m->m_mailer, pvp); 438294Seric /* syserr fails because log is closed */ 439294Seric /* syserr("Cannot exec %s", m->m_mailer); */ 4404214Seric printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 4414082Seric (void) fflush(stdout); 4421619Seric _exit(EX_UNAVAILABLE); 443294Seric } 444294Seric 4452898Seric /* write out message to mailer */ 4464082Seric (void) close(pvect[0]); 4474123Seric (void) signal(SIGPIPE, SIG_IGN); 4482898Seric mfile = fdopen(pvect[1], "w"); 4492898Seric if (editfcn == NULL) 4502898Seric editfcn = putmessage; 4512898Seric (*editfcn)(mfile, m); 4524082Seric (void) fclose(mfile); 453294Seric 454294Seric /* 455294Seric ** Wait for child to die and report status. 456294Seric ** We should never get fatal errors (e.g., segmentation 457294Seric ** violation), so we report those specially. For other 458294Seric ** errors, we choose a status message (into statmsg), 459294Seric ** and if it represents an error, we print it. 460294Seric */ 461294Seric 462294Seric while ((i = wait(&st)) > 0 && i != pid) 463294Seric continue; 464294Seric if (i < 0) 465294Seric { 466294Seric syserr("wait"); 467294Seric return (-1); 468294Seric } 469294Seric if ((st & 0377) != 0) 470294Seric { 471294Seric syserr("%s: stat %o", pvp[0], st); 4721597Seric ExitStat = EX_UNAVAILABLE; 473294Seric return (-1); 474294Seric } 475294Seric i = (st >> 8) & 0377; 4762343Seric giveresponse(i, TRUE, m); 477294Seric return (i); 478294Seric } 479294Seric /* 480294Seric ** GIVERESPONSE -- Interpret an error response from a mailer 481294Seric ** 482294Seric ** Parameters: 483294Seric ** stat -- the status code from the mailer (high byte 484294Seric ** only; core dumps must have been taken care of 485294Seric ** already). 486294Seric ** force -- if set, force an error message output, even 487294Seric ** if the mailer seems to like to print its own 488294Seric ** messages. 489294Seric ** m -- the mailer descriptor for this mailer. 490294Seric ** 491294Seric ** Returns: 4924082Seric ** none. 493294Seric ** 494294Seric ** Side Effects: 4951518Seric ** Errors may be incremented. 496294Seric ** ExitStat may be set. 497294Seric */ 498294Seric 499294Seric giveresponse(stat, force, m) 500294Seric int stat; 501294Seric int force; 502294Seric register struct mailer *m; 503294Seric { 504294Seric register char *statmsg; 505294Seric extern char *SysExMsg[]; 506294Seric register int i; 507294Seric extern int N_SysEx; 5081624Seric char buf[30]; 509294Seric 5104315Seric /* 5114315Seric ** Compute status message from code. 5124315Seric */ 5134315Seric 514294Seric i = stat - EX__BASE; 515294Seric if (i < 0 || i > N_SysEx) 516294Seric statmsg = NULL; 517294Seric else 518294Seric statmsg = SysExMsg[i]; 519294Seric if (stat == 0) 5204065Seric { 5214194Seric if (bitset(M_LOCAL, m->m_flags)) 5224161Seric statmsg = "delivered"; 5234161Seric else 5244161Seric statmsg = "queued"; 5254065Seric if (Verbose) 5264166Seric message(Arpa_Info, statmsg); 5274065Seric } 528294Seric else 529294Seric { 5301518Seric Errors++; 531294Seric if (statmsg == NULL && m->m_badstat != 0) 532294Seric { 533294Seric stat = m->m_badstat; 534294Seric i = stat - EX__BASE; 535294Seric # ifdef DEBUG 536294Seric if (i < 0 || i >= N_SysEx) 537294Seric syserr("Bad m_badstat %d", stat); 538294Seric else 539294Seric # endif DEBUG 540294Seric statmsg = SysExMsg[i]; 541294Seric } 542294Seric if (statmsg == NULL) 543294Seric usrerr("unknown mailer response %d", stat); 5444065Seric else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 545294Seric usrerr("%s", statmsg); 546294Seric } 547294Seric 548294Seric /* 549294Seric ** Final cleanup. 550294Seric ** Log a record of the transaction. Compute the new 551294Seric ** ExitStat -- if we already had an error, stick with 552294Seric ** that. 553294Seric */ 554294Seric 5551624Seric if (statmsg == NULL) 5561624Seric { 5574082Seric (void) sprintf(buf, "error %d", stat); 5581624Seric statmsg = buf; 5591624Seric } 5601624Seric 561294Seric # ifdef LOG 5622774Seric syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg); 563294Seric # endif LOG 5641389Seric setstat(stat); 565294Seric } 566294Seric /* 5672898Seric ** PUTMESSAGE -- output a message to the final mailer. 568294Seric ** 5692898Seric ** This routine takes care of recreating the header from the 5702898Seric ** in-core copy, etc. 571294Seric ** 572294Seric ** Parameters: 5732898Seric ** fp -- file to output onto. 5742898Seric ** m -- a mailer descriptor. 575294Seric ** 576294Seric ** Returns: 5772898Seric ** none. 578294Seric ** 579294Seric ** Side Effects: 5802898Seric ** The message is written onto fp. 581294Seric */ 582294Seric 5832898Seric putmessage(fp, m) 5842898Seric FILE *fp; 5852898Seric struct mailer *m; 586294Seric { 5872898Seric char buf[BUFSIZ]; 5882898Seric register int i; 5894315Seric register HDR *h; 5902898Seric extern char *arpadate(); 5912898Seric bool anyheader = FALSE; 5923044Seric extern char *capitalize(); 5934370Seric extern char *hvalue(); 5944370Seric extern bool samefrom(); 5954447Seric char *of_line; 596294Seric 5974315Seric /* 5984315Seric ** Output "From" line unless supressed 5994315Seric */ 6004315Seric 6013186Seric if (!bitset(M_NHDR, m->m_flags)) 6024205Seric { 6034205Seric (void) expand("$l", buf, &buf[sizeof buf - 1]); 6044205Seric fprintf(fp, "%s\n", buf); 6054205Seric } 6063186Seric 6074315Seric /* 6084315Seric ** Output all header lines 6094315Seric */ 6104315Seric 6114447Seric of_line = hvalue("original-from"); 6122898Seric for (h = Header; h != NULL; h = h->h_link) 6131828Seric { 6144315Seric register char *p; 6154370Seric char *origfrom = OrigFrom; 6164447Seric bool nooutput; 6174315Seric 6184447Seric nooutput = FALSE; 6193389Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 6204209Seric { 6214209Seric p = ")><("; /* can't happen (I hope) */ 6224447Seric nooutput = TRUE; 6234209Seric } 6244447Seric 6254447Seric /* use From: line from message if generated is the same */ 6264370Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL && 6274447Seric strcmp(m->m_from, "$f") == 0 && of_line == NULL) 6283385Seric { 6294370Seric p = origfrom; 6304370Seric origfrom = NULL; 6314370Seric } 6324370Seric else if (bitset(H_DEFAULT, h->h_flags)) 6334370Seric { 6344082Seric (void) expand(h->h_value, buf, &buf[sizeof buf]); 6353385Seric p = buf; 6363385Seric } 6372898Seric else 6383385Seric p = h->h_value; 6394447Seric if (p == NULL || *p == '\0') 6403389Seric continue; 6414209Seric 6424209Seric /* hack, hack -- output Original-From field if different */ 6434447Seric if (strcmp(h->h_field, "from") == 0 && origfrom != NULL) 6444209Seric { 6454447Seric /* output new Original-From line if needed */ 6464447Seric if (of_line == NULL && !samefrom(p, origfrom)) 6474447Seric { 6484447Seric fprintf(fp, "Original-From: %s\n", origfrom); 6494447Seric anyheader = TRUE; 6504447Seric } 6514447Seric if (of_line != NULL && !nooutput && samefrom(p, of_line)) 6524447Seric { 6534447Seric /* delete Original-From: line if redundant */ 6544447Seric p = of_line; 6554447Seric of_line = NULL; 6564447Seric } 6574447Seric } 6584447Seric else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL) 6594447Seric nooutput = TRUE; 6604447Seric 6614447Seric /* finally, output the header line */ 6624447Seric if (!nooutput) 6634447Seric { 6644447Seric fprintf(fp, "%s: %s\n", capitalize(h->h_field), p); 6654447Seric h->h_flags |= H_USED; 6664370Seric anyheader = TRUE; 6674209Seric } 6682898Seric } 6692898Seric if (anyheader) 6702898Seric fprintf(fp, "\n"); 6712898Seric 6724315Seric /* 6734315Seric ** Output the body of the message 6744315Seric */ 6754315Seric 676*4452Seric if (TempFile != NULL) 677*4452Seric { 678*4452Seric rewind(TempFile); 679*4452Seric while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, TempFile)) > 0) 680*4452Seric (void) fwrite(buf, 1, i, fp); 6812898Seric 682*4452Seric if (ferror(TempFile)) 683*4452Seric { 684*4452Seric syserr("putmessage: read error"); 685*4452Seric setstat(EX_IOERR); 686*4452Seric } 687*4452Seric } 688*4452Seric 689*4452Seric fflush(fp); 6904123Seric if (ferror(fp) && errno != EPIPE) 691294Seric { 6922898Seric syserr("putmessage: write error"); 693294Seric setstat(EX_IOERR); 694294Seric } 6954123Seric errno = 0; 696294Seric } 697294Seric /* 6984370Seric ** SAMEFROM -- tell if two text addresses represent the same from address. 6994370Seric ** 7004370Seric ** Parameters: 7014370Seric ** ifrom -- internally generated form of from address. 7024370Seric ** efrom -- external form of from address. 7034370Seric ** 7044370Seric ** Returns: 7054370Seric ** TRUE -- if they convey the same info. 7064370Seric ** FALSE -- if any information has been lost. 7074370Seric ** 7084370Seric ** Side Effects: 7094370Seric ** none. 7104370Seric */ 7114370Seric 7124370Seric bool 7134370Seric samefrom(ifrom, efrom) 7144370Seric char *ifrom; 7154370Seric char *efrom; 7164370Seric { 7174447Seric register char *p; 7184447Seric char buf[MAXNAME + 4]; 7194447Seric 7204447Seric # ifdef DEBUG 7214447Seric if (Debug > 7) 7224447Seric printf("samefrom(%s,%s)-->", ifrom, efrom); 7234447Seric # endif DEBUG 7244447Seric if (strcmp(ifrom, efrom) == 0) 7254447Seric goto success; 7264447Seric p = index(ifrom, '@'); 7274447Seric if (p == NULL) 7284447Seric goto failure; 7294447Seric *p = '\0'; 7304447Seric strcpy(buf, ifrom); 7314447Seric strcat(buf, " at "); 7324447Seric *p++ = '@'; 7334447Seric strcat(buf, p); 7344447Seric if (strcmp(buf, efrom) == 0) 7354447Seric goto success; 7364447Seric 7374447Seric failure: 7384447Seric # ifdef DEBUG 7394447Seric if (Debug > 7) 7404447Seric printf("FALSE\n"); 7414447Seric # endif DEBUG 7424447Seric return (FALSE); 7434447Seric 7444447Seric success: 7454447Seric # ifdef DEBUG 7464447Seric if (Debug > 7) 7474447Seric printf("TRUE\n"); 7484447Seric # endif DEBUG 7494447Seric return (TRUE); 7504370Seric } 7514370Seric /* 752294Seric ** MAILFILE -- Send a message to a file. 753294Seric ** 7544327Seric ** If the file has the setuid/setgid bits set, but NO execute 7554327Seric ** bits, sendmail will try to become the owner of that file 7564327Seric ** rather than the real user. Obviously, this only works if 7574327Seric ** sendmail runs as root. 7584327Seric ** 759294Seric ** Parameters: 760294Seric ** filename -- the name of the file to send to. 7614397Seric ** ctladdr -- the controlling address header -- includes 7624397Seric ** the userid/groupid to be when sending. 763294Seric ** 764294Seric ** Returns: 765294Seric ** The exit code associated with the operation. 766294Seric ** 767294Seric ** Side Effects: 768294Seric ** none. 769294Seric */ 770294Seric 7714397Seric mailfile(filename, ctladdr) 772294Seric char *filename; 7734397Seric ADDRESS *ctladdr; 774294Seric { 775294Seric register FILE *f; 7764214Seric register int pid; 777294Seric 7784214Seric /* 7794214Seric ** Fork so we can change permissions here. 7804214Seric ** Note that we MUST use fork, not vfork, because of 7814214Seric ** the complications of calling subroutines, etc. 7824214Seric */ 7834067Seric 7844214Seric DOFORK(fork); 7854214Seric 7864214Seric if (pid < 0) 7874214Seric return (EX_OSERR); 7884214Seric else if (pid == 0) 7894214Seric { 7904214Seric /* child -- actually write to file */ 7914327Seric struct stat stb; 7924417Seric extern int DefUid, DefGid; 7934327Seric 7944215Seric (void) signal(SIGINT, SIG_DFL); 7954215Seric (void) signal(SIGHUP, SIG_DFL); 7964215Seric (void) signal(SIGTERM, SIG_DFL); 7974327Seric umask(OldUmask); 7984327Seric if (stat(filename, &stb) < 0) 7994431Seric stb.st_mode = 0666; 8004327Seric if (bitset(0111, stb.st_mode)) 8014327Seric exit(EX_CANTCREAT); 8024401Seric if (ctladdr == NULL) 8034401Seric ctladdr = &From; 8044327Seric if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 8054417Seric { 8064417Seric if (ctladdr->q_uid == 0) 8074417Seric (void) setgid(DefGid); 8084417Seric else 8094417Seric (void) setgid(ctladdr->q_gid); 8104417Seric } 8114327Seric if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 8124417Seric { 8134417Seric if (ctladdr->q_uid == 0) 8144417Seric (void) setuid(DefUid); 8154417Seric else 8164417Seric (void) setuid(ctladdr->q_uid); 8174417Seric } 8184214Seric f = fopen(filename, "a"); 8194214Seric if (f == NULL) 8204214Seric exit(EX_CANTCREAT); 8214214Seric 8224214Seric putmessage(f, Mailer[1]); 8234214Seric fputs("\n", f); 8244214Seric (void) fclose(f); 8254214Seric (void) fflush(stdout); 8264417Seric 8274417Seric /* reset ISUID & ISGID bits */ 8284417Seric (void) chmod(filename, stb.st_mode); 8294214Seric exit(EX_OK); 8304315Seric /*NOTREACHED*/ 8314214Seric } 8324214Seric else 8334214Seric { 8344214Seric /* parent -- wait for exit status */ 8354214Seric register int i; 8364214Seric auto int stat; 8374214Seric 8384214Seric while ((i = wait(&stat)) != pid) 8394214Seric { 8404214Seric if (i < 0) 8414214Seric { 8424214Seric stat = EX_OSERR << 8; 8434214Seric break; 8444214Seric } 8454214Seric } 8464215Seric if ((stat & 0377) != 0) 8474215Seric stat = EX_UNAVAILABLE << 8; 8484214Seric return ((stat >> 8) & 0377); 8494214Seric } 850294Seric } 851