1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988 Regents of the University of California. 4 * All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #ifndef lint 10 static char sccsid[] = "@(#)deliver.c 5.54.1.1 (Berkeley) 02/26/92"; 11 #endif /* not lint */ 12 13 #include "sendmail.h" 14 #include <sys/signal.h> 15 #include <sys/stat.h> 16 #include <netdb.h> 17 #include <fcntl.h> 18 #include <errno.h> 19 #ifdef NAMED_BIND 20 #include <sys/param.h> 21 #include <arpa/nameser.h> 22 #include <resolv.h> 23 #endif 24 25 /* 26 ** DELIVER -- Deliver a message to a list of addresses. 27 ** 28 ** This routine delivers to everyone on the same host as the 29 ** user on the head of the list. It is clever about mailers 30 ** that don't handle multiple users. It is NOT guaranteed 31 ** that it will deliver to all these addresses however -- so 32 ** deliver should be called once for each address on the 33 ** list. 34 ** 35 ** Parameters: 36 ** e -- the envelope to deliver. 37 ** firstto -- head of the address list to deliver to. 38 ** 39 ** Returns: 40 ** zero -- successfully delivered. 41 ** else -- some failure, see ExitStat for more info. 42 ** 43 ** Side Effects: 44 ** The standard input is passed off to someone. 45 */ 46 47 deliver(e, firstto) 48 register ENVELOPE *e; 49 ADDRESS *firstto; 50 { 51 char *host; /* host being sent to */ 52 char *user; /* user being sent to */ 53 char **pvp; 54 register char **mvp; 55 register char *p; 56 register MAILER *m; /* mailer for this recipient */ 57 ADDRESS *ctladdr; 58 register ADDRESS *to = firstto; 59 bool clever = FALSE; /* running user smtp to this mailer */ 60 ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 61 int rcode; /* response code */ 62 char *from; /* pointer to from person */ 63 char *pv[MAXPV+1]; 64 char tobuf[MAXLINE-50]; /* text line of to people */ 65 char buf[MAXNAME]; 66 char tfrombuf[MAXNAME]; /* translated from person */ 67 char rpathbuf[MAXNAME]; /* translated return path */ 68 extern bool checkcompat(); 69 extern ADDRESS *getctladdr(); 70 extern char *remotename(); 71 72 errno = 0; 73 if (bitset(QDONTSEND, to->q_flags)) 74 return (0); 75 76 #ifdef NAMED_BIND 77 /* unless interactive, try twice, over a minute */ 78 if (OpMode == MD_DAEMON || OpMode == MD_SMTP) { 79 _res.retrans = 30; 80 _res.retry = 2; 81 } 82 #endif 83 84 m = to->q_mailer; 85 host = to->q_host; 86 87 if (tTd(10, 1)) 88 printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 89 m->m_mno, host, to->q_user); 90 91 /* 92 ** If this mailer is expensive, and if we don't want to make 93 ** connections now, just mark these addresses and return. 94 ** This is useful if we want to batch connections to 95 ** reduce load. This will cause the messages to be 96 ** queued up, and a daemon will come along to send the 97 ** messages later. 98 ** This should be on a per-mailer basis. 99 */ 100 101 if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) && 102 !Verbose) 103 { 104 for (; to != NULL; to = to->q_next) 105 { 106 if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m) 107 continue; 108 to->q_flags |= QQUEUEUP|QDONTSEND; 109 e->e_to = to->q_paddr; 110 message(Arpa_Info, "queued"); 111 if (LogLevel > 4) 112 logdelivery("queued"); 113 } 114 e->e_to = NULL; 115 return (0); 116 } 117 118 /* 119 ** Do initial argv setup. 120 ** Insert the mailer name. Notice that $x expansion is 121 ** NOT done on the mailer name. Then, if the mailer has 122 ** a picky -f flag, we insert it as appropriate. This 123 ** code does not check for 'pv' overflow; this places a 124 ** manifest lower limit of 4 for MAXPV. 125 ** The from address rewrite is expected to make 126 ** the address relative to the other end. 127 */ 128 129 /* rewrite from address, using rewriting rules */ 130 (void) strcpy(rpathbuf, remotename(e->e_returnpath, m, TRUE, TRUE)); 131 if (e->e_returnpath == e->e_sender) 132 { 133 from = rpathbuf; 134 } 135 else 136 { 137 (void) strcpy(tfrombuf, remotename(e->e_sender, m, TRUE, TRUE)); 138 from = tfrombuf; 139 } 140 141 define('f', e->e_returnpath, e); /* raw return path */ 142 define('<', rpathbuf, e); /* translated return path */ 143 define('g', from, e); /* translated sender */ 144 define('h', host, e); /* to host */ 145 Errors = 0; 146 pvp = pv; 147 *pvp++ = m->m_argv[0]; 148 149 /* insert -f or -r flag as appropriate */ 150 if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 151 { 152 if (bitnset(M_FOPT, m->m_flags)) 153 *pvp++ = "-f"; 154 else 155 *pvp++ = "-r"; 156 *pvp++ = newstr(rpathbuf); 157 } 158 159 /* 160 ** Append the other fixed parts of the argv. These run 161 ** up to the first entry containing "$u". There can only 162 ** be one of these, and there are only a few more slots 163 ** in the pv after it. 164 */ 165 166 for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 167 { 168 while ((p = index(p, '\001')) != NULL) 169 if (*++p == 'u') 170 break; 171 if (p != NULL) 172 break; 173 174 /* this entry is safe -- go ahead and process it */ 175 expand(*mvp, buf, &buf[sizeof buf - 1], e); 176 *pvp++ = newstr(buf); 177 if (pvp >= &pv[MAXPV - 3]) 178 { 179 syserr("Too many parameters to %s before $u", pv[0]); 180 return (-1); 181 } 182 } 183 184 /* 185 ** If we have no substitution for the user name in the argument 186 ** list, we know that we must supply the names otherwise -- and 187 ** SMTP is the answer!! 188 */ 189 190 if (*mvp == NULL) 191 { 192 /* running SMTP */ 193 # ifdef SMTP 194 clever = TRUE; 195 *pvp = NULL; 196 # else SMTP 197 /* oops! we don't implement SMTP */ 198 syserr("SMTP style mailer"); 199 return (EX_SOFTWARE); 200 # endif SMTP 201 } 202 203 /* 204 ** At this point *mvp points to the argument with $u. We 205 ** run through our address list and append all the addresses 206 ** we can. If we run out of space, do not fret! We can 207 ** always send another copy later. 208 */ 209 210 tobuf[0] = '\0'; 211 e->e_to = tobuf; 212 ctladdr = NULL; 213 for (; to != NULL; to = to->q_next) 214 { 215 /* avoid sending multiple recipients to dumb mailers */ 216 if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 217 break; 218 219 /* if already sent or not for this host, don't send */ 220 if (bitset(QDONTSEND, to->q_flags) || 221 strcmp(to->q_host, host) != 0 || 222 to->q_mailer != firstto->q_mailer) 223 continue; 224 225 /* avoid overflowing tobuf */ 226 if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 227 break; 228 229 if (tTd(10, 1)) 230 { 231 printf("\nsend to "); 232 printaddr(to, FALSE); 233 } 234 235 /* compute effective uid/gid when sending */ 236 if (to->q_mailer == ProgMailer) 237 ctladdr = getctladdr(to); 238 239 user = to->q_user; 240 e->e_to = to->q_paddr; 241 to->q_flags |= QDONTSEND; 242 243 /* 244 ** Check to see that these people are allowed to 245 ** talk to each other. 246 */ 247 248 if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 249 { 250 NoReturn = TRUE; 251 usrerr("Message is too large; %ld bytes max", m->m_maxsize); 252 giveresponse(EX_UNAVAILABLE, m, e); 253 continue; 254 } 255 if (!checkcompat(to)) 256 { 257 giveresponse(EX_UNAVAILABLE, m, e); 258 continue; 259 } 260 261 /* 262 ** Strip quote bits from names if the mailer is dumb 263 ** about them. 264 */ 265 266 if (bitnset(M_STRIPQ, m->m_flags)) 267 { 268 stripquotes(user, TRUE); 269 stripquotes(host, TRUE); 270 } 271 else 272 { 273 stripquotes(user, FALSE); 274 stripquotes(host, FALSE); 275 } 276 277 /* hack attack -- delivermail compatibility */ 278 if (m == ProgMailer && *user == '|') 279 user++; 280 281 /* 282 ** If an error message has already been given, don't 283 ** bother to send to this address. 284 ** 285 ** >>>>>>>>>> This clause assumes that the local mailer 286 ** >> NOTE >> cannot do any further aliasing; that 287 ** >>>>>>>>>> function is subsumed by sendmail. 288 */ 289 290 if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 291 continue; 292 293 /* save statistics.... */ 294 markstats(e, to); 295 296 /* 297 ** See if this user name is "special". 298 ** If the user name has a slash in it, assume that this 299 ** is a file -- send it off without further ado. Note 300 ** that this type of addresses is not processed along 301 ** with the others, so we fudge on the To person. 302 */ 303 304 if (m == LocalMailer) 305 { 306 if (user[0] == '/') 307 { 308 rcode = mailfile(user, getctladdr(to)); 309 giveresponse(rcode, m, e); 310 if (rcode == EX_OK) 311 to->q_flags |= QSENT; 312 continue; 313 } 314 } 315 316 /* 317 ** Address is verified -- add this user to mailer 318 ** argv, and add it to the print list of recipients. 319 */ 320 321 /* link together the chain of recipients */ 322 to->q_tchain = tochain; 323 tochain = to; 324 325 /* create list of users for error messages */ 326 (void) strcat(tobuf, ","); 327 (void) strcat(tobuf, to->q_paddr); 328 define('u', user, e); /* to user */ 329 define('z', to->q_home, e); /* user's home */ 330 331 /* 332 ** Expand out this user into argument list. 333 */ 334 335 if (!clever) 336 { 337 expand(*mvp, buf, &buf[sizeof buf - 1], e); 338 *pvp++ = newstr(buf); 339 if (pvp >= &pv[MAXPV - 2]) 340 { 341 /* allow some space for trailing parms */ 342 break; 343 } 344 } 345 } 346 347 /* see if any addresses still exist */ 348 if (tobuf[0] == '\0') 349 { 350 define('g', (char *) NULL, e); 351 define('<', (char *) NULL, e); 352 return (0); 353 } 354 355 /* print out messages as full list */ 356 e->e_to = tobuf + 1; 357 358 /* 359 ** Fill out any parameters after the $u parameter. 360 */ 361 362 while (!clever && *++mvp != NULL) 363 { 364 expand(*mvp, buf, &buf[sizeof buf - 1], e); 365 *pvp++ = newstr(buf); 366 if (pvp >= &pv[MAXPV]) 367 syserr("deliver: pv overflow after $u for %s", pv[0]); 368 } 369 *pvp++ = NULL; 370 371 /* 372 ** Call the mailer. 373 ** The argument vector gets built, pipes 374 ** are created as necessary, and we fork & exec as 375 ** appropriate. 376 ** If we are running SMTP, we just need to clean up. 377 */ 378 379 if (ctladdr == NULL) 380 ctladdr = &e->e_from; 381 #ifdef NAMED_BIND 382 if (ConfigLevel < 2) 383 _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 384 #endif 385 #ifdef SMTP 386 if (clever) 387 { 388 rcode = EX_OK; 389 #ifdef NAMED_BIND 390 if (host[0] && host[0] != '[') 391 { 392 expand("\001j", buf, &buf[sizeof(buf) - 1], e); 393 Nmx = getmxrr(host, MxHosts, buf, &rcode); 394 } 395 else 396 #endif 397 { 398 Nmx = 1; 399 MxHosts[0] = host; 400 } 401 if (Nmx >= 0) 402 { 403 message(Arpa_Info, "Connecting to %s (%s)...", 404 MxHosts[0], m->m_name); 405 if ((rcode = smtpinit(m, pv)) == EX_OK) { 406 register char *t = tobuf; 407 register int i; 408 409 /* send the recipient list */ 410 tobuf[0] = '\0'; 411 for (to = tochain; to; to = to->q_tchain) { 412 e->e_to = to->q_paddr; 413 if ((i = smtprcpt(to, m)) != EX_OK) { 414 markfailure(e, to, i); 415 giveresponse(i, m, e); 416 } 417 else { 418 *t++ = ','; 419 for (p = to->q_paddr; *p; *t++ = *p++); 420 } 421 } 422 423 /* now send the data */ 424 if (tobuf[0] == '\0') 425 e->e_to = NULL; 426 else { 427 e->e_to = tobuf + 1; 428 rcode = smtpdata(m, e); 429 } 430 431 /* now close the connection */ 432 smtpquit(m); 433 } 434 } 435 } 436 else 437 #endif /* SMTP */ 438 { 439 static int sendoff(); 440 441 message(Arpa_Info, "Connecting to %s (%s)...", host, m->m_name); 442 rcode = sendoff(e, m, pv, ctladdr); 443 } 444 #ifdef NAMED_BIND 445 if (ConfigLevel < 2) 446 _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 447 #endif 448 449 /* 450 ** Do final status disposal. 451 ** We check for something in tobuf for the SMTP case. 452 ** If we got a temporary failure, arrange to queue the 453 ** addressees. 454 */ 455 456 if (tobuf[0] != '\0') 457 giveresponse(rcode, m, e); 458 for (to = tochain; to != NULL; to = to->q_tchain) 459 if (rcode != EX_OK) 460 markfailure(e, to, rcode); 461 else 462 to->q_flags |= QSENT; 463 464 errno = 0; 465 define('g', (char *) NULL, e); 466 define('<', (char *) NULL, e); 467 return (rcode); 468 } 469 /* 470 ** MARKFAILURE -- mark a failure on a specific address. 471 ** 472 ** Parameters: 473 ** e -- the envelope we are sending. 474 ** q -- the address to mark. 475 ** rcode -- the code signifying the particular failure. 476 ** 477 ** Returns: 478 ** none. 479 ** 480 ** Side Effects: 481 ** marks the address (and possibly the envelope) with the 482 ** failure so that an error will be returned or 483 ** the message will be queued, as appropriate. 484 */ 485 486 markfailure(e, q, rcode) 487 register ENVELOPE *e; 488 register ADDRESS *q; 489 int rcode; 490 { 491 if (rcode == EX_OK) 492 return; 493 else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR) 494 q->q_flags |= QBADADDR; 495 else if (curtime() > e->e_ctime + TimeOut) 496 { 497 extern char *pintvl(); 498 char buf[MAXLINE]; 499 500 if (!bitset(EF_TIMEOUT, e->e_flags)) 501 { 502 (void) sprintf(buf, "Cannot send message for %s", 503 pintvl(TimeOut, FALSE)); 504 if (e->e_message != NULL) 505 free(e->e_message); 506 e->e_message = newstr(buf); 507 message(Arpa_Info, buf); 508 } 509 q->q_flags |= QBADADDR; 510 e->e_flags |= EF_TIMEOUT; 511 } 512 else 513 q->q_flags |= QQUEUEUP; 514 } 515 /* 516 ** DOFORK -- do a fork, retrying a couple of times on failure. 517 ** 518 ** This MUST be a macro, since after a vfork we are running 519 ** two processes on the same stack!!! 520 ** 521 ** Parameters: 522 ** none. 523 ** 524 ** Returns: 525 ** From a macro??? You've got to be kidding! 526 ** 527 ** Side Effects: 528 ** Modifies the ==> LOCAL <== variable 'pid', leaving: 529 ** pid of child in parent, zero in child. 530 ** -1 on unrecoverable error. 531 ** 532 ** Notes: 533 ** I'm awfully sorry this looks so awful. That's 534 ** vfork for you..... 535 */ 536 537 # define NFORKTRIES 5 538 539 # ifndef FORK 540 # define FORK fork 541 # endif 542 543 # define DOFORK(fORKfN) \ 544 {\ 545 register int i;\ 546 \ 547 for (i = NFORKTRIES; --i >= 0; )\ 548 {\ 549 pid = fORKfN();\ 550 if (pid >= 0)\ 551 break;\ 552 if (i > 0)\ 553 sleep((unsigned) NFORKTRIES - i);\ 554 }\ 555 } 556 /* 557 ** DOFORK -- simple fork interface to DOFORK. 558 ** 559 ** Parameters: 560 ** none. 561 ** 562 ** Returns: 563 ** pid of child in parent. 564 ** zero in child. 565 ** -1 on error. 566 ** 567 ** Side Effects: 568 ** returns twice, once in parent and once in child. 569 */ 570 571 dofork() 572 { 573 register int pid; 574 575 DOFORK(fork); 576 return (pid); 577 } 578 /* 579 ** SENDOFF -- send off call to mailer & collect response. 580 ** 581 ** Parameters: 582 ** e -- the envelope to mail. 583 ** m -- mailer descriptor. 584 ** pvp -- parameter vector to send to it. 585 ** ctladdr -- an address pointer controlling the 586 ** user/groupid etc. of the mailer. 587 ** 588 ** Returns: 589 ** exit status of mailer. 590 ** 591 ** Side Effects: 592 ** none. 593 */ 594 static 595 sendoff(e, m, pvp, ctladdr) 596 register ENVELOPE *e; 597 MAILER *m; 598 char **pvp; 599 ADDRESS *ctladdr; 600 { 601 register int i; 602 603 /* 604 ** Create connection to mailer. 605 */ 606 607 mci = openmailer(m, pvp, ctladdr, FALSE); 608 if (mci == NULL) 609 return (-1); 610 611 /* 612 ** Format and send message. 613 */ 614 615 putfromline(mci->mci_out, m); 616 (*e->e_puthdr)(mci->mci_out, m, e); 617 putline("\n", mci->mci_out, m); 618 (*e->e_putbody)(mci->mci_out, m, e); 619 620 i = endmailer(mci, pvp[0]); 621 622 /* arrange a return receipt if requested */ 623 if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags)) 624 { 625 e->e_flags |= EF_SENDRECEIPT; 626 /* do we want to send back more info? */ 627 } 628 629 return (i); 630 } 631 /* 632 ** ENDMAILER -- Wait for mailer to terminate. 633 ** 634 ** We should never get fatal errors (e.g., segmentation 635 ** violation), so we report those specially. For other 636 ** errors, we choose a status message (into statmsg), 637 ** and if it represents an error, we print it. 638 ** 639 ** Parameters: 640 ** pid -- pid of mailer. 641 ** name -- name of mailer (for error messages). 642 ** 643 ** Returns: 644 ** exit code of mailer. 645 ** 646 ** Side Effects: 647 ** none. 648 */ 649 650 endmailer(mci, name) 651 register MCONINFO *mci; 652 char *name; 653 { 654 int st; 655 656 /* close any connections */ 657 if (mci->mci_in != NULL) 658 (void) fclose(mci->mci_in); 659 if (mci->mci_out != NULL) 660 (void) fclose(mci->mci_out); 661 mci->mci_in = mci->mci_out = NULL; 662 mci->mci_state = MCIS_CLOSED; 663 if (bitset(MCIF_TEMP, mci->mci_flags)) 664 xfree(mci); 665 666 /* in the IPC case there is nothing to wait for */ 667 if (pid == 0) 668 return (EX_OK); 669 670 /* wait for the mailer process to die and collect status */ 671 st = waitfor(pid); 672 if (st == -1) 673 { 674 syserr("endmailer %s: wait", name); 675 return (EX_SOFTWARE); 676 } 677 678 /* see if it died a horrid death */ 679 if ((st & 0377) != 0) 680 { 681 syserr("mailer %s died with signal %o", name, st); 682 ExitStat = EX_TEMPFAIL; 683 return (EX_TEMPFAIL); 684 } 685 686 /* normal death -- return status */ 687 st = (st >> 8) & 0377; 688 return (st); 689 } 690 /* 691 ** OPENMAILER -- open connection to mailer. 692 ** 693 ** Parameters: 694 ** m -- mailer descriptor. 695 ** pvp -- parameter vector to pass to mailer. 696 ** ctladdr -- controlling address for user. 697 ** clever -- create a full duplex connection. 698 ** 699 ** Returns: 700 ** The mail connection info struct for this connection. 701 ** NULL on failure. 702 ** 703 ** Side Effects: 704 ** creates a mailer in a subprocess. 705 */ 706 707 MCONINFO * 708 openmailer(m, pvp, ctladdr, clever) 709 MAILER *m; 710 char **pvp; 711 ADDRESS *ctladdr; 712 bool clever; 713 { 714 int pid; 715 int mpvect[2]; 716 int rpvect[2]; 717 extern FILE *fdopen(); 718 719 if (tTd(11, 1)) 720 { 721 printf("openmailer:"); 722 printav(pvp); 723 } 724 errno = 0; 725 726 CurHostName = m->m_mailer; 727 728 /* 729 ** Deal with the special case of mail handled through an IPC 730 ** connection. 731 ** In this case we don't actually fork. We must be 732 ** running SMTP for this to work. We will return a 733 ** zero pid to indicate that we are running IPC. 734 ** We also handle a debug version that just talks to stdin/out. 735 */ 736 737 /* check for Local Person Communication -- not for mortals!!! */ 738 if (strcmp(m->m_mailer, "[LPC]") == 0) 739 { 740 mci = xalloc(sizeof *mci); 741 mci->mci_in = stdin; 742 mci->mci_out = stdout; 743 mci->mci_pid = 0; 744 mci->mci_state = MCIS_OPEN; 745 return mci; 746 } 747 748 if (strcmp(m->m_mailer, "[IPC]") == 0 || 749 strcmp(m->m_mailer, "[TCP]") == 0) 750 { 751 #ifdef DAEMON 752 register STAB *st; 753 extern STAB *stab(); 754 register int i, j; 755 register u_short port; 756 757 CurHostName = pvp[1]; 758 if (!clever) 759 syserr("non-clever IPC"); 760 if (pvp[2] != NULL) 761 port = atoi(pvp[2]); 762 else 763 port = 0; 764 for (j = 0; j < Nmx; j++) 765 { 766 /* see if we already know that this host is fried */ 767 CurHostName = MxHosts[j]; 768 st = stab(CurHostName, 769 ST_MCONINFO + m->m_mno, 770 ST_ENTER); 771 mci = &st->s_mci; 772 if (mci->mci_state != MCIS_CLOSED) 773 return mci; 774 if (mci->mci_exitstat == EX_OK) 775 { 776 /* try the connection */ 777 message(Arpa_Info, "Connecting to %s (%s)...", 778 MxHosts[j], m->m_name); 779 i = makeconnection(MxHosts[j], port, mci, 780 bitnset(M_SECURE_PORT, m->m_flags)); 781 mci->mci_exitstat = i; 782 mci->mci_errno = errno; 783 } 784 else 785 { 786 i = st->s_host.ho_exitstat; 787 errno = st->s_host.ho_errno; 788 } 789 if (i == EX_OK) 790 return mci; 791 792 /* enter status of this host */ 793 setstat(i); 794 } 795 return NULL; 796 #else DAEMON 797 syserr("openmailer: no IPC"); 798 return NULL; 799 #endif DAEMON 800 } 801 802 /* create a pipe to shove the mail through */ 803 if (pipe(mpvect) < 0) 804 { 805 syserr("openmailer: pipe (to mailer)"); 806 return NULL; 807 } 808 809 #ifdef SMTP 810 /* if this mailer speaks smtp, create a return pipe */ 811 if (clever && pipe(rpvect) < 0) 812 { 813 syserr("openmailer: pipe (from mailer)"); 814 (void) close(mpvect[0]); 815 (void) close(mpvect[1]); 816 return NULL; 817 } 818 #endif SMTP 819 820 /* 821 ** Actually fork the mailer process. 822 ** DOFORK is clever about retrying. 823 ** 824 ** Dispose of SIGCHLD signal catchers that may be laying 825 ** around so that endmail will get it. 826 */ 827 828 if (CurEnv->e_xfp != NULL) 829 (void) fflush(CurEnv->e_xfp); /* for debugging */ 830 (void) fflush(stdout); 831 # ifdef SIGCHLD 832 (void) signal(SIGCHLD, SIG_DFL); 833 # endif SIGCHLD 834 DOFORK(FORK); 835 /* pid is set by DOFORK */ 836 if (pid < 0) 837 { 838 /* failure */ 839 syserr("openmailer: cannot fork"); 840 (void) close(mpvect[0]); 841 (void) close(mpvect[1]); 842 #ifdef SMTP 843 if (clever) 844 { 845 (void) close(rpvect[0]); 846 (void) close(rpvect[1]); 847 } 848 #endif SMTP 849 return NULL; 850 } 851 else if (pid == 0) 852 { 853 int i; 854 extern int DtableSize; 855 856 /* child -- set up input & exec mailer */ 857 /* make diagnostic output be standard output */ 858 (void) signal(SIGINT, SIG_IGN); 859 (void) signal(SIGHUP, SIG_IGN); 860 (void) signal(SIGTERM, SIG_DFL); 861 862 /* arrange to filter standard & diag output of command */ 863 if (clever) 864 { 865 (void) close(rpvect[0]); 866 (void) close(1); 867 (void) dup(rpvect[1]); 868 (void) close(rpvect[1]); 869 } 870 else if (OpMode == MD_SMTP || HoldErrs) 871 { 872 /* put mailer output in transcript */ 873 (void) close(1); 874 (void) dup(fileno(CurEnv->e_xfp)); 875 } 876 (void) close(2); 877 (void) dup(1); 878 879 /* arrange to get standard input */ 880 (void) close(mpvect[1]); 881 (void) close(0); 882 if (dup(mpvect[0]) < 0) 883 { 884 syserr("Cannot dup to zero!"); 885 _exit(EX_OSERR); 886 } 887 (void) close(mpvect[0]); 888 if (!bitnset(M_RESTR, m->m_flags)) 889 { 890 if (ctladdr == NULL || ctladdr->q_uid == 0) 891 { 892 (void) setgid(DefGid); 893 (void) initgroups(DefUser, DefGid); 894 (void) setuid(DefUid); 895 } 896 else 897 { 898 (void) setgid(ctladdr->q_gid); 899 (void) initgroups(ctladdr->q_ruser? 900 ctladdr->q_ruser: ctladdr->q_user, 901 ctladdr->q_gid); 902 (void) setuid(ctladdr->q_uid); 903 } 904 } 905 906 /* arrange for all the files to be closed */ 907 for (i = 3; i < DtableSize; i++) { 908 register int j; 909 if ((j = fcntl(i, F_GETFD, 0)) != -1) 910 (void)fcntl(i, F_SETFD, j|1); 911 } 912 913 /* try to execute the mailer */ 914 execve(m->m_mailer, pvp, UserEnviron); 915 syserr("Cannot exec %s", m->m_mailer); 916 if (m == LocalMailer) 917 _exit(EX_TEMPFAIL); 918 switch (errno) 919 { 920 case EIO: 921 case EAGAIN: 922 case ENOMEM: 923 # ifdef EPROCLIM 924 case EPROCLIM: 925 # endif 926 _exit(EX_TEMPFAIL); 927 } 928 _exit(EX_UNAVAILABLE); 929 } 930 931 /* 932 ** Set up return value. 933 */ 934 935 mci = xalloc(sizeof *mci); 936 (void) close(mpvect[0]); 937 mci->mci_out = fdopen(mpvect[1], "w"); 938 if (clever) 939 { 940 (void) close(rpvect[1]); 941 mci->mci_in = fdopen(rpvect[0], "r"); 942 } 943 else 944 { 945 mci->mci_flags |= MCIF_TEMP; 946 mci->mci_in = NULL; 947 } 948 949 return mci; 950 } 951 /* 952 ** GIVERESPONSE -- Interpret an error response from a mailer 953 ** 954 ** Parameters: 955 ** stat -- the status code from the mailer (high byte 956 ** only; core dumps must have been taken care of 957 ** already). 958 ** m -- the mailer descriptor for this mailer. 959 ** 960 ** Returns: 961 ** none. 962 ** 963 ** Side Effects: 964 ** Errors may be incremented. 965 ** ExitStat may be set. 966 */ 967 968 giveresponse(stat, m, e) 969 int stat; 970 register MAILER *m; 971 ENVELOPE *e; 972 { 973 register char *statmsg; 974 extern char *SysExMsg[]; 975 register int i; 976 extern int N_SysEx; 977 #ifdef NAMED_BIND 978 extern int h_errno; 979 #endif 980 char buf[MAXLINE]; 981 982 #ifdef lint 983 if (m == NULL) 984 return; 985 #endif lint 986 987 /* 988 ** Compute status message from code. 989 */ 990 991 i = stat - EX__BASE; 992 if (stat == 0) 993 statmsg = "250 Sent"; 994 else if (i < 0 || i > N_SysEx) 995 { 996 (void) sprintf(buf, "554 unknown mailer error %d", stat); 997 stat = EX_UNAVAILABLE; 998 statmsg = buf; 999 } 1000 else if (stat == EX_TEMPFAIL) 1001 { 1002 (void) strcpy(buf, SysExMsg[i]); 1003 #ifdef NAMED_BIND 1004 if (h_errno == TRY_AGAIN) 1005 { 1006 extern char *errstring(); 1007 1008 statmsg = errstring(h_errno+MAX_ERRNO); 1009 } 1010 else 1011 #endif 1012 { 1013 if (errno != 0) 1014 { 1015 extern char *errstring(); 1016 1017 statmsg = errstring(errno); 1018 } 1019 else 1020 { 1021 #ifdef SMTP 1022 extern char SmtpError[]; 1023 1024 statmsg = SmtpError; 1025 #else SMTP 1026 statmsg = NULL; 1027 #endif SMTP 1028 } 1029 } 1030 if (statmsg != NULL && statmsg[0] != '\0') 1031 { 1032 (void) strcat(buf, ": "); 1033 (void) strcat(buf, statmsg); 1034 } 1035 statmsg = buf; 1036 } 1037 else 1038 { 1039 statmsg = SysExMsg[i]; 1040 } 1041 1042 /* 1043 ** Print the message as appropriate 1044 */ 1045 1046 if (stat == EX_OK || stat == EX_TEMPFAIL) 1047 message(Arpa_Info, &statmsg[4]); 1048 else 1049 { 1050 Errors++; 1051 usrerr(statmsg); 1052 } 1053 1054 /* 1055 ** Final cleanup. 1056 ** Log a record of the transaction. Compute the new 1057 ** ExitStat -- if we already had an error, stick with 1058 ** that. 1059 */ 1060 1061 if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2)) 1062 logdelivery(&statmsg[4]); 1063 1064 if (stat != EX_TEMPFAIL) 1065 setstat(stat); 1066 if (stat != EX_OK) 1067 { 1068 if (e->e_message != NULL) 1069 free(e->e_message); 1070 e->e_message = newstr(&statmsg[4]); 1071 } 1072 errno = 0; 1073 #ifdef NAMED_BIND 1074 h_errno = 0; 1075 #endif 1076 } 1077 /* 1078 ** LOGDELIVERY -- log the delivery in the system log 1079 ** 1080 ** Parameters: 1081 ** stat -- the message to print for the status 1082 ** 1083 ** Returns: 1084 ** none 1085 ** 1086 ** Side Effects: 1087 ** none 1088 */ 1089 1090 logdelivery(stat) 1091 char *stat; 1092 { 1093 extern char *pintvl(); 1094 1095 # ifdef LOG 1096 syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id, 1097 CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), stat); 1098 # endif LOG 1099 } 1100 /* 1101 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 1102 ** 1103 ** This can be made an arbitrary message separator by changing $l 1104 ** 1105 ** One of the ugliest hacks seen by human eyes is contained herein: 1106 ** UUCP wants those stupid "remote from <host>" lines. Why oh why 1107 ** does a well-meaning programmer such as myself have to deal with 1108 ** this kind of antique garbage???? 1109 ** 1110 ** Parameters: 1111 ** fp -- the file to output to. 1112 ** m -- the mailer describing this entry. 1113 ** 1114 ** Returns: 1115 ** none 1116 ** 1117 ** Side Effects: 1118 ** outputs some text to fp. 1119 */ 1120 1121 putfromline(fp, m) 1122 register FILE *fp; 1123 register MAILER *m; 1124 { 1125 char *template = "\001l\n"; 1126 char buf[MAXLINE]; 1127 1128 if (bitnset(M_NHDR, m->m_flags)) 1129 return; 1130 1131 # ifdef UGLYUUCP 1132 if (bitnset(M_UGLYUUCP, m->m_flags)) 1133 { 1134 char *bang; 1135 char xbuf[MAXLINE]; 1136 1137 expand("\001<", buf, &buf[sizeof buf - 1], CurEnv); 1138 bang = index(buf, '!'); 1139 if (bang == NULL) 1140 syserr("No ! in UUCP! (%s)", buf); 1141 else 1142 { 1143 *bang++ = '\0'; 1144 (void) sprintf(xbuf, "From %s \001d remote from %s\n", bang, buf); 1145 template = xbuf; 1146 } 1147 } 1148 # endif UGLYUUCP 1149 expand(template, buf, &buf[sizeof buf - 1], CurEnv); 1150 putline(buf, fp, m); 1151 } 1152 /* 1153 ** PUTBODY -- put the body of a message. 1154 ** 1155 ** Parameters: 1156 ** fp -- file to output onto. 1157 ** m -- a mailer descriptor to control output format. 1158 ** e -- the envelope to put out. 1159 ** 1160 ** Returns: 1161 ** none. 1162 ** 1163 ** Side Effects: 1164 ** The message is written onto fp. 1165 */ 1166 1167 putbody(fp, m, e) 1168 FILE *fp; 1169 MAILER *m; 1170 register ENVELOPE *e; 1171 { 1172 char buf[MAXLINE]; 1173 1174 /* 1175 ** Output the body of the message 1176 */ 1177 1178 if (e->e_dfp == NULL) 1179 { 1180 if (e->e_df != NULL) 1181 { 1182 e->e_dfp = fopen(e->e_df, "r"); 1183 if (e->e_dfp == NULL) 1184 syserr("putbody: Cannot open %s for %s from %s", 1185 e->e_df, e->e_to, e->e_from); 1186 } 1187 else 1188 putline("<<< No Message Collected >>>", fp, m); 1189 } 1190 if (e->e_dfp != NULL) 1191 { 1192 rewind(e->e_dfp); 1193 while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 1194 { 1195 if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1196 strncmp(buf, "From ", 5) == 0) 1197 (void) putc('>', fp); 1198 putline(buf, fp, m); 1199 } 1200 1201 if (ferror(e->e_dfp)) 1202 { 1203 syserr("putbody: read error"); 1204 ExitStat = EX_IOERR; 1205 } 1206 } 1207 1208 (void) fflush(fp); 1209 if (ferror(fp) && errno != EPIPE) 1210 { 1211 syserr("putbody: write error"); 1212 ExitStat = EX_IOERR; 1213 } 1214 errno = 0; 1215 } 1216 /* 1217 ** MAILFILE -- Send a message to a file. 1218 ** 1219 ** If the file has the setuid/setgid bits set, but NO execute 1220 ** bits, sendmail will try to become the owner of that file 1221 ** rather than the real user. Obviously, this only works if 1222 ** sendmail runs as root. 1223 ** 1224 ** This could be done as a subordinate mailer, except that it 1225 ** is used implicitly to save messages in ~/dead.letter. We 1226 ** view this as being sufficiently important as to include it 1227 ** here. For example, if the system is dying, we shouldn't have 1228 ** to create another process plus some pipes to save the message. 1229 ** 1230 ** Parameters: 1231 ** filename -- the name of the file to send to. 1232 ** ctladdr -- the controlling address header -- includes 1233 ** the userid/groupid to be when sending. 1234 ** 1235 ** Returns: 1236 ** The exit code associated with the operation. 1237 ** 1238 ** Side Effects: 1239 ** none. 1240 */ 1241 1242 mailfile(filename, ctladdr) 1243 char *filename; 1244 ADDRESS *ctladdr; 1245 { 1246 register FILE *f; 1247 register int pid; 1248 ENVELOPE *e = CurEnv; 1249 1250 /* 1251 ** Fork so we can change permissions here. 1252 ** Note that we MUST use fork, not vfork, because of 1253 ** the complications of calling subroutines, etc. 1254 */ 1255 1256 DOFORK(fork); 1257 1258 if (pid < 0) 1259 return (EX_OSERR); 1260 else if (pid == 0) 1261 { 1262 /* child -- actually write to file */ 1263 struct stat stb; 1264 1265 (void) signal(SIGINT, SIG_DFL); 1266 (void) signal(SIGHUP, SIG_DFL); 1267 (void) signal(SIGTERM, SIG_DFL); 1268 (void) umask(OldUmask); 1269 1270 if (stat(filename, &stb) < 0) 1271 stb.st_mode = 0666; 1272 1273 /* limit the errors to those actually caused in the child */ 1274 errno = 0; 1275 ExitStat = EX_OK; 1276 1277 if (bitset(0111, stb.st_mode)) 1278 exit(EX_CANTCREAT); 1279 if (ctladdr == NULL) 1280 ctladdr = &e->e_from; 1281 /* we have to open the dfile BEFORE setuid */ 1282 if (e->e_dfp == NULL && e->e_df != NULL) 1283 { 1284 e->e_dfp = fopen(e->e_df, "r"); 1285 if (e->e_dfp == NULL) 1286 { 1287 syserr("mailfile: Cannot open %s for %s from %s", 1288 e->e_df, e->e_to, e->e_from); 1289 } 1290 } 1291 1292 if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 1293 { 1294 if (ctladdr->q_uid == 0) 1295 { 1296 (void) setgid(DefGid); 1297 (void) initgroups(DefUser, DefGid); 1298 } 1299 else 1300 { 1301 (void) setgid(ctladdr->q_gid); 1302 (void) initgroups(ctladdr->q_ruser? 1303 ctladdr->q_ruser: ctladdr->q_user, 1304 ctladdr->q_gid); 1305 } 1306 } 1307 if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 1308 { 1309 if (ctladdr->q_uid == 0) 1310 (void) setuid(DefUid); 1311 else 1312 (void) setuid(ctladdr->q_uid); 1313 } 1314 FileName = filename; 1315 LineNumber = 0; 1316 f = dfopen(filename, "a"); 1317 if (f == NULL) 1318 { 1319 message("cannot open"); 1320 exit(EX_CANTCREAT); 1321 } 1322 1323 putfromline(f, ProgMailer); 1324 (*CurEnv->e_puthdr)(f, ProgMailer, CurEnv); 1325 putline("\n", f, ProgMailer); 1326 (*CurEnv->e_putbody)(f, ProgMailer, CurEnv); 1327 putline("\n", f, ProgMailer); 1328 if (ferror(f)) 1329 { 1330 message("I/O error"); 1331 setstat(EX_IOERR); 1332 } 1333 (void) fclose(f); 1334 (void) fflush(stdout); 1335 1336 /* reset ISUID & ISGID bits for paranoid systems */ 1337 (void) chmod(filename, (int) stb.st_mode); 1338 exit(ExitStat); 1339 /*NOTREACHED*/ 1340 } 1341 else 1342 { 1343 /* parent -- wait for exit status */ 1344 int st; 1345 1346 st = waitfor(pid); 1347 if ((st & 0377) != 0) 1348 return (EX_UNAVAILABLE); 1349 else 1350 return ((st >> 8) & 0377); 1351 /*NOTREACHED*/ 1352 } 1353 } 1354 /* 1355 ** SENDALL -- actually send all the messages. 1356 ** 1357 ** Parameters: 1358 ** e -- the envelope to send. 1359 ** mode -- the delivery mode to use. If SM_DEFAULT, use 1360 ** the current SendMode. 1361 ** 1362 ** Returns: 1363 ** none. 1364 ** 1365 ** Side Effects: 1366 ** Scans the send lists and sends everything it finds. 1367 ** Delivers any appropriate error messages. 1368 ** If we are running in a non-interactive mode, takes the 1369 ** appropriate action. 1370 */ 1371 1372 sendall(e, mode) 1373 ENVELOPE *e; 1374 char mode; 1375 { 1376 register ADDRESS *q; 1377 bool oldverbose; 1378 int pid; 1379 int nsent; 1380 # ifdef LOCKF 1381 struct flock lfd; 1382 # endif 1383 1384 /* determine actual delivery mode */ 1385 if (mode == SM_DEFAULT) 1386 { 1387 extern bool shouldqueue(); 1388 1389 if (shouldqueue(e->e_msgpriority)) 1390 mode = SM_QUEUE; 1391 else 1392 mode = SendMode; 1393 } 1394 1395 if (tTd(13, 1)) 1396 { 1397 printf("\nSENDALL: mode %c, sendqueue:\n", mode); 1398 printaddr(e->e_sendqueue, TRUE); 1399 } 1400 1401 /* 1402 ** Do any preprocessing necessary for the mode we are running. 1403 ** Check to make sure the hop count is reasonable. 1404 ** Delete sends to the sender in mailing lists. 1405 */ 1406 1407 CurEnv = e; 1408 1409 if (e->e_hopcount > MaxHopCount) 1410 { 1411 errno = 0; 1412 syserr("sendall: too many hops %d (%d max): from %s, to %s", 1413 e->e_hopcount, MaxHopCount, e->e_from, e->e_to); 1414 return; 1415 } 1416 1417 if (!MeToo) 1418 { 1419 extern ADDRESS *recipient(); 1420 1421 e->e_from.q_flags |= QDONTSEND; 1422 (void) recipient(&e->e_from, &e->e_sendqueue); 1423 } 1424 1425 # ifdef QUEUE 1426 if ((mode == SM_QUEUE || mode == SM_FORK || 1427 (mode != SM_VERIFY && SuperSafe)) && 1428 !bitset(EF_INQUEUE, e->e_flags)) 1429 queueup(e, TRUE, mode == SM_QUEUE); 1430 #endif QUEUE 1431 1432 oldverbose = Verbose; 1433 switch (mode) 1434 { 1435 case SM_VERIFY: 1436 Verbose = TRUE; 1437 break; 1438 1439 case SM_QUEUE: 1440 queueonly: 1441 e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 1442 return; 1443 1444 case SM_FORK: 1445 if (e->e_xfp != NULL) 1446 (void) fflush(e->e_xfp); 1447 1448 # ifdef LOCKF 1449 /* 1450 ** Since lockf has the interesting semantic that the 1451 ** lock is lost when we fork, we have to risk losing 1452 ** the lock here by closing before the fork, and then 1453 ** trying to get it back in the child. 1454 */ 1455 1456 if (e->e_lockfp != NULL) 1457 { 1458 (void) fclose(e->e_lockfp); 1459 e->e_lockfp = NULL; 1460 } 1461 # endif /* LOCKF */ 1462 1463 pid = fork(); 1464 if (pid < 0) 1465 { 1466 goto queueonly; 1467 } 1468 else if (pid > 0) 1469 { 1470 /* be sure we leave the temp files to our child */ 1471 e->e_id = e->e_df = NULL; 1472 # ifndef LOCKF 1473 if (e->e_lockfp != NULL) 1474 (void) fclose(e->e_lockfp); 1475 # endif 1476 return; 1477 } 1478 1479 /* double fork to avoid zombies */ 1480 if (fork() > 0) 1481 exit(EX_OK); 1482 1483 /* be sure we are immune from the terminal */ 1484 disconnect(FALSE); 1485 1486 # ifdef LOCKF 1487 /* 1488 ** Now try to get our lock back. 1489 */ 1490 1491 lfd.l_type = F_WRLCK; 1492 lfd.l_whence = lfd.l_start = lfd.l_len = 0; 1493 e->e_lockfp = fopen(queuename(e, 'q'), "r+"); 1494 if (e->e_lockfp == NULL || 1495 fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0) 1496 { 1497 /* oops.... lost it */ 1498 # ifdef LOG 1499 if (LogLevel > 5) 1500 syslog(LOG_NOTICE, "%s: lost lock: %m", 1501 CurEnv->e_id); 1502 # endif /* LOG */ 1503 exit(EX_OK); 1504 } 1505 # endif /* LOCKF */ 1506 1507 break; 1508 } 1509 1510 /* 1511 ** Run through the list and send everything. 1512 */ 1513 1514 nsent = 0; 1515 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1516 { 1517 if (mode == SM_VERIFY) 1518 { 1519 e->e_to = q->q_paddr; 1520 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1521 message(Arpa_Info, "deliverable"); 1522 } 1523 else if (!bitset(QDONTSEND, q->q_flags)) 1524 { 1525 # ifdef QUEUE 1526 /* 1527 ** Checkpoint the send list every few addresses 1528 */ 1529 1530 if (nsent >= CheckpointInterval) 1531 { 1532 queueup(e, TRUE, FALSE); 1533 nsent = 0; 1534 } 1535 # endif /* QUEUE */ 1536 if (deliver(e, q) == EX_OK) 1537 nsent++; 1538 } 1539 } 1540 Verbose = oldverbose; 1541 1542 /* 1543 ** Now run through and check for errors. 1544 */ 1545 1546 if (mode == SM_VERIFY) 1547 return; 1548 1549 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1550 { 1551 register ADDRESS *qq; 1552 1553 if (tTd(13, 3)) 1554 { 1555 printf("Checking "); 1556 printaddr(q, FALSE); 1557 } 1558 1559 /* only send errors if the message failed */ 1560 if (!bitset(QBADADDR, q->q_flags)) 1561 continue; 1562 1563 /* we have an address that failed -- find the parent */ 1564 for (qq = q; qq != NULL; qq = qq->q_alias) 1565 { 1566 char obuf[MAXNAME + 6]; 1567 extern char *aliaslookup(); 1568 1569 /* we can only have owners for local addresses */ 1570 if (!bitnset(M_LOCAL, qq->q_mailer->m_flags)) 1571 continue; 1572 1573 /* see if the owner list exists */ 1574 (void) strcpy(obuf, "owner-"); 1575 if (strncmp(qq->q_user, "owner-", 6) == 0) 1576 (void) strcat(obuf, "owner"); 1577 else 1578 (void) strcat(obuf, qq->q_user); 1579 makelower(obuf); 1580 if (aliaslookup(obuf) == NULL) 1581 continue; 1582 1583 if (tTd(13, 4)) 1584 printf("Errors to %s\n", obuf); 1585 1586 /* owner list exists -- add it to the error queue */ 1587 sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue); 1588 ErrorMode = EM_MAIL; 1589 break; 1590 } 1591 1592 /* if we did not find an owner, send to the sender */ 1593 if (qq == NULL && bitset(QBADADDR, q->q_flags)) 1594 sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue); 1595 } 1596 1597 if (mode == SM_FORK) 1598 finis(); 1599 } 1600