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 6.47 (Berkeley) 03/18/93"; 11 #endif /* not lint */ 12 13 #include "sendmail.h" 14 #include <signal.h> 15 #include <sys/stat.h> 16 #include <netdb.h> 17 #include <errno.h> 18 #ifdef NAMED_BIND 19 #include <arpa/nameser.h> 20 #include <resolv.h> 21 #endif 22 23 /* 24 ** DELIVER -- Deliver a message to a list of addresses. 25 ** 26 ** This routine delivers to everyone on the same host as the 27 ** user on the head of the list. It is clever about mailers 28 ** that don't handle multiple users. It is NOT guaranteed 29 ** that it will deliver to all these addresses however -- so 30 ** deliver should be called once for each address on the 31 ** list. 32 ** 33 ** Parameters: 34 ** e -- the envelope to deliver. 35 ** firstto -- head of the address list to deliver to. 36 ** 37 ** Returns: 38 ** zero -- successfully delivered. 39 ** else -- some failure, see ExitStat for more info. 40 ** 41 ** Side Effects: 42 ** The standard input is passed off to someone. 43 */ 44 45 deliver(e, firstto) 46 register ENVELOPE *e; 47 ADDRESS *firstto; 48 { 49 char *host; /* host being sent to */ 50 char *user; /* user being sent to */ 51 char **pvp; 52 register char **mvp; 53 register char *p; 54 register MAILER *m; /* mailer for this recipient */ 55 ADDRESS *ctladdr; 56 register MCI *mci; 57 register ADDRESS *to = firstto; 58 bool clever = FALSE; /* running user smtp to this mailer */ 59 ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 60 int rcode; /* response code */ 61 char *firstsig; /* signature of firstto */ 62 char *pv[MAXPV+1]; 63 char tobuf[TOBUFSIZE]; /* text line of to people */ 64 char buf[MAXNAME]; 65 char rpathbuf[MAXNAME]; /* translated return path */ 66 extern int checkcompat(); 67 extern ADDRESS *getctladdr(); 68 extern char *remotename(); 69 extern MCI *openmailer(); 70 extern char *hostsignature(); 71 72 errno = 0; 73 if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, 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|QBADADDR|QQUEUEUP, to->q_flags) || 107 to->q_mailer != m) 108 continue; 109 to->q_flags |= QQUEUEUP|QDONTSEND; 110 e->e_to = to->q_paddr; 111 message("queued"); 112 if (LogLevel > 8) 113 logdelivery(m, NULL, "queued", e); 114 } 115 e->e_to = NULL; 116 return (0); 117 } 118 119 /* 120 ** Do initial argv setup. 121 ** Insert the mailer name. Notice that $x expansion is 122 ** NOT done on the mailer name. Then, if the mailer has 123 ** a picky -f flag, we insert it as appropriate. This 124 ** code does not check for 'pv' overflow; this places a 125 ** manifest lower limit of 4 for MAXPV. 126 ** The from address rewrite is expected to make 127 ** the address relative to the other end. 128 */ 129 130 /* rewrite from address, using rewriting rules */ 131 (void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m, TRUE, FALSE, 132 TRUE, FALSE, e)); 133 define('g', rpathbuf, e); /* translated return path */ 134 define('h', host, e); /* to host */ 135 Errors = 0; 136 pvp = pv; 137 *pvp++ = m->m_argv[0]; 138 139 /* insert -f or -r flag as appropriate */ 140 if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 141 { 142 if (bitnset(M_FOPT, m->m_flags)) 143 *pvp++ = "-f"; 144 else 145 *pvp++ = "-r"; 146 *pvp++ = newstr(rpathbuf); 147 } 148 149 /* 150 ** Append the other fixed parts of the argv. These run 151 ** up to the first entry containing "$u". There can only 152 ** be one of these, and there are only a few more slots 153 ** in the pv after it. 154 */ 155 156 for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 157 { 158 /* can't use strchr here because of sign extension problems */ 159 while (*p != '\0') 160 { 161 if ((*p++ & 0377) == MACROEXPAND) 162 { 163 if (*p == 'u') 164 break; 165 } 166 } 167 168 if (*p != '\0') 169 break; 170 171 /* this entry is safe -- go ahead and process it */ 172 expand(*mvp, buf, &buf[sizeof buf - 1], e); 173 *pvp++ = newstr(buf); 174 if (pvp >= &pv[MAXPV - 3]) 175 { 176 syserr("554 Too many parameters to %s before $u", pv[0]); 177 return (-1); 178 } 179 } 180 181 /* 182 ** If we have no substitution for the user name in the argument 183 ** list, we know that we must supply the names otherwise -- and 184 ** SMTP is the answer!! 185 */ 186 187 if (*mvp == NULL) 188 { 189 /* running SMTP */ 190 # ifdef SMTP 191 clever = TRUE; 192 *pvp = NULL; 193 # else /* SMTP */ 194 /* oops! we don't implement SMTP */ 195 syserr("554 SMTP style mailer"); 196 return (EX_SOFTWARE); 197 # endif /* SMTP */ 198 } 199 200 /* 201 ** At this point *mvp points to the argument with $u. We 202 ** run through our address list and append all the addresses 203 ** we can. If we run out of space, do not fret! We can 204 ** always send another copy later. 205 */ 206 207 tobuf[0] = '\0'; 208 e->e_to = tobuf; 209 ctladdr = NULL; 210 firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e); 211 for (; to != NULL; to = to->q_next) 212 { 213 /* avoid sending multiple recipients to dumb mailers */ 214 if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 215 break; 216 217 /* if already sent or not for this host, don't send */ 218 if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 219 to->q_mailer != firstto->q_mailer || 220 strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0) 221 continue; 222 223 /* avoid overflowing tobuf */ 224 if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 225 break; 226 227 if (tTd(10, 1)) 228 { 229 printf("\nsend to "); 230 printaddr(to, FALSE); 231 } 232 233 /* compute effective uid/gid when sending */ 234 if (to->q_mailer == ProgMailer) 235 ctladdr = getctladdr(to); 236 237 user = to->q_user; 238 e->e_to = to->q_paddr; 239 if (tTd(10, 5)) 240 { 241 printf("deliver: QDONTSEND "); 242 printaddr(to, FALSE); 243 } 244 to->q_flags |= QDONTSEND; 245 246 /* 247 ** Check to see that these people are allowed to 248 ** talk to each other. 249 */ 250 251 if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 252 { 253 NoReturn = TRUE; 254 usrerr("552 Message is too large; %ld bytes max", m->m_maxsize); 255 giveresponse(EX_UNAVAILABLE, m, NULL, e); 256 continue; 257 } 258 rcode = checkcompat(to, e); 259 if (rcode != EX_OK) 260 { 261 giveresponse(rcode, m, NULL, e); 262 continue; 263 } 264 265 /* 266 ** Strip quote bits from names if the mailer is dumb 267 ** about them. 268 */ 269 270 if (bitnset(M_STRIPQ, m->m_flags)) 271 { 272 stripquotes(user); 273 stripquotes(host); 274 } 275 276 /* hack attack -- delivermail compatibility */ 277 if (m == ProgMailer && *user == '|') 278 user++; 279 280 /* 281 ** If an error message has already been given, don't 282 ** bother to send to this address. 283 ** 284 ** >>>>>>>>>> This clause assumes that the local mailer 285 ** >> NOTE >> cannot do any further aliasing; that 286 ** >>>>>>>>>> function is subsumed by sendmail. 287 */ 288 289 if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 290 continue; 291 292 /* save statistics.... */ 293 markstats(e, to); 294 295 /* 296 ** See if this user name is "special". 297 ** If the user name has a slash in it, assume that this 298 ** is a file -- send it off without further ado. Note 299 ** that this type of addresses is not processed along 300 ** with the others, so we fudge on the To person. 301 */ 302 303 if (m == FileMailer) 304 { 305 rcode = mailfile(user, getctladdr(to), e); 306 giveresponse(rcode, m, NULL, e); 307 if (rcode == EX_OK) 308 to->q_flags |= QSENT; 309 continue; 310 } 311 312 /* 313 ** Address is verified -- add this user to mailer 314 ** argv, and add it to the print list of recipients. 315 */ 316 317 /* link together the chain of recipients */ 318 to->q_tchain = tochain; 319 tochain = to; 320 321 /* create list of users for error messages */ 322 (void) strcat(tobuf, ","); 323 (void) strcat(tobuf, to->q_paddr); 324 define('u', user, e); /* to user */ 325 define('z', to->q_home, e); /* user's home */ 326 327 /* 328 ** Expand out this user into argument list. 329 */ 330 331 if (!clever) 332 { 333 expand(*mvp, buf, &buf[sizeof buf - 1], e); 334 *pvp++ = newstr(buf); 335 if (pvp >= &pv[MAXPV - 2]) 336 { 337 /* allow some space for trailing parms */ 338 break; 339 } 340 } 341 } 342 343 /* see if any addresses still exist */ 344 if (tobuf[0] == '\0') 345 { 346 define('g', (char *) NULL, e); 347 return (0); 348 } 349 350 /* print out messages as full list */ 351 e->e_to = tobuf + 1; 352 353 /* 354 ** Fill out any parameters after the $u parameter. 355 */ 356 357 while (!clever && *++mvp != NULL) 358 { 359 expand(*mvp, buf, &buf[sizeof buf - 1], e); 360 *pvp++ = newstr(buf); 361 if (pvp >= &pv[MAXPV]) 362 syserr("554 deliver: pv overflow after $u for %s", pv[0]); 363 } 364 *pvp++ = NULL; 365 366 /* 367 ** Call the mailer. 368 ** The argument vector gets built, pipes 369 ** are created as necessary, and we fork & exec as 370 ** appropriate. 371 ** If we are running SMTP, we just need to clean up. 372 */ 373 374 if (ctladdr == NULL && m != ProgMailer) 375 ctladdr = &e->e_from; 376 #ifdef NAMED_BIND 377 if (ConfigLevel < 2) 378 _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 379 #endif 380 mci = openmailer(m, pv, ctladdr, clever, e); 381 if (mci == NULL) 382 { 383 /* catastrophic error */ 384 rcode = EX_OSERR; 385 goto give_up; 386 } 387 else if (mci->mci_state != MCIS_OPEN) 388 { 389 /* couldn't open the mailer */ 390 rcode = mci->mci_exitstat; 391 errno = mci->mci_errno; 392 if (rcode == EX_OK) 393 { 394 /* shouldn't happen */ 395 syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 396 rcode, mci->mci_state, firstsig); 397 rcode = EX_SOFTWARE; 398 } 399 } 400 else if (!clever) 401 { 402 /* 403 ** Format and send message. 404 */ 405 406 putfromline(mci->mci_out, m, e); 407 (*e->e_puthdr)(mci->mci_out, m, e); 408 putline("\n", mci->mci_out, m); 409 (*e->e_putbody)(mci->mci_out, m, e); 410 411 /* get the exit status */ 412 rcode = endmailer(mci, pv[0]); 413 } 414 else 415 #ifdef SMTP 416 { 417 /* 418 ** Send the MAIL FROM: protocol 419 */ 420 421 rcode = smtpmailfrom(m, mci, e); 422 if (rcode == EX_OK) 423 { 424 register char *t = tobuf; 425 register int i; 426 427 /* send the recipient list */ 428 tobuf[0] = '\0'; 429 for (to = tochain; to != NULL; to = to->q_tchain) 430 { 431 e->e_to = to->q_paddr; 432 if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 433 { 434 markfailure(e, to, i); 435 giveresponse(i, m, mci, e); 436 } 437 else 438 { 439 *t++ = ','; 440 for (p = to->q_paddr; *p; *t++ = *p++) 441 continue; 442 } 443 } 444 445 /* now send the data */ 446 if (tobuf[0] == '\0') 447 { 448 e->e_to = NULL; 449 if (bitset(MCIF_CACHED, mci->mci_flags)) 450 smtprset(m, mci, e); 451 } 452 else 453 { 454 e->e_to = tobuf + 1; 455 rcode = smtpdata(m, mci, e); 456 } 457 458 /* now close the connection */ 459 if (!bitset(MCIF_CACHED, mci->mci_flags)) 460 smtpquit(m, mci, e); 461 } 462 } 463 #else /* not SMTP */ 464 { 465 syserr("554 deliver: need SMTP compiled to use clever mailer"); 466 rcode = EX_CONFIG; 467 goto give_up; 468 } 469 #endif /* SMTP */ 470 #ifdef NAMED_BIND 471 if (ConfigLevel < 2) 472 _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 473 #endif 474 475 /* arrange a return receipt if requested */ 476 if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags)) 477 { 478 e->e_flags |= EF_SENDRECEIPT; 479 /* do we want to send back more info? */ 480 } 481 482 /* 483 ** Do final status disposal. 484 ** We check for something in tobuf for the SMTP case. 485 ** If we got a temporary failure, arrange to queue the 486 ** addressees. 487 */ 488 489 give_up: 490 if (tobuf[0] != '\0') 491 giveresponse(rcode, m, mci, e); 492 for (to = tochain; to != NULL; to = to->q_tchain) 493 { 494 if (rcode != EX_OK) 495 markfailure(e, to, rcode); 496 else 497 { 498 to->q_flags |= QSENT; 499 e->e_nsent++; 500 } 501 } 502 503 /* 504 ** Restore state and return. 505 */ 506 507 errno = 0; 508 define('g', (char *) NULL, e); 509 return (rcode); 510 } 511 /* 512 ** MARKFAILURE -- mark a failure on a specific address. 513 ** 514 ** Parameters: 515 ** e -- the envelope we are sending. 516 ** q -- the address to mark. 517 ** rcode -- the code signifying the particular failure. 518 ** 519 ** Returns: 520 ** none. 521 ** 522 ** Side Effects: 523 ** marks the address (and possibly the envelope) with the 524 ** failure so that an error will be returned or 525 ** the message will be queued, as appropriate. 526 */ 527 528 markfailure(e, q, rcode) 529 register ENVELOPE *e; 530 register ADDRESS *q; 531 int rcode; 532 { 533 if (rcode == EX_OK) 534 return; 535 else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR) 536 q->q_flags |= QBADADDR; 537 else if (curtime() > e->e_ctime + TimeOut) 538 { 539 extern char *pintvl(); 540 char buf[MAXLINE]; 541 542 if (!bitset(EF_TIMEOUT, e->e_flags)) 543 { 544 (void) sprintf(buf, "Cannot send message for %s", 545 pintvl(TimeOut, FALSE)); 546 if (e->e_message != NULL) 547 free(e->e_message); 548 e->e_message = newstr(buf); 549 message(buf); 550 } 551 q->q_flags |= QBADADDR; 552 e->e_flags |= EF_TIMEOUT; 553 fprintf(e->e_xfp, "421 %s... Message timed out\n", q->q_paddr); 554 } 555 else 556 q->q_flags |= QQUEUEUP; 557 } 558 /* 559 ** DOFORK -- do a fork, retrying a couple of times on failure. 560 ** 561 ** This MUST be a macro, since after a vfork we are running 562 ** two processes on the same stack!!! 563 ** 564 ** Parameters: 565 ** none. 566 ** 567 ** Returns: 568 ** From a macro??? You've got to be kidding! 569 ** 570 ** Side Effects: 571 ** Modifies the ==> LOCAL <== variable 'pid', leaving: 572 ** pid of child in parent, zero in child. 573 ** -1 on unrecoverable error. 574 ** 575 ** Notes: 576 ** I'm awfully sorry this looks so awful. That's 577 ** vfork for you..... 578 */ 579 580 # define NFORKTRIES 5 581 582 # ifndef FORK 583 # define FORK fork 584 # endif 585 586 # define DOFORK(fORKfN) \ 587 {\ 588 register int i;\ 589 \ 590 for (i = NFORKTRIES; --i >= 0; )\ 591 {\ 592 pid = fORKfN();\ 593 if (pid >= 0)\ 594 break;\ 595 if (i > 0)\ 596 sleep((unsigned) NFORKTRIES - i);\ 597 }\ 598 } 599 /* 600 ** DOFORK -- simple fork interface to DOFORK. 601 ** 602 ** Parameters: 603 ** none. 604 ** 605 ** Returns: 606 ** pid of child in parent. 607 ** zero in child. 608 ** -1 on error. 609 ** 610 ** Side Effects: 611 ** returns twice, once in parent and once in child. 612 */ 613 614 dofork() 615 { 616 register int pid; 617 618 DOFORK(fork); 619 return (pid); 620 } 621 /* 622 ** ENDMAILER -- Wait for mailer to terminate. 623 ** 624 ** We should never get fatal errors (e.g., segmentation 625 ** violation), so we report those specially. For other 626 ** errors, we choose a status message (into statmsg), 627 ** and if it represents an error, we print it. 628 ** 629 ** Parameters: 630 ** pid -- pid of mailer. 631 ** name -- name of mailer (for error messages). 632 ** 633 ** Returns: 634 ** exit code of mailer. 635 ** 636 ** Side Effects: 637 ** none. 638 */ 639 640 endmailer(mci, name) 641 register MCI *mci; 642 char *name; 643 { 644 int st; 645 646 /* close any connections */ 647 if (mci->mci_in != NULL) 648 (void) xfclose(mci->mci_in, name, "mci_in"); 649 if (mci->mci_out != NULL) 650 (void) xfclose(mci->mci_out, name, "mci_out"); 651 mci->mci_in = mci->mci_out = NULL; 652 mci->mci_state = MCIS_CLOSED; 653 654 /* in the IPC case there is nothing to wait for */ 655 if (mci->mci_pid == 0) 656 return (EX_OK); 657 658 /* wait for the mailer process to die and collect status */ 659 st = waitfor(mci->mci_pid); 660 if (st == -1) 661 { 662 syserr("endmailer %s: wait", name); 663 return (EX_SOFTWARE); 664 } 665 666 /* see if it died a horrid death */ 667 if ((st & 0377) != 0) 668 { 669 syserr("mailer %s died with signal %o", name, st); 670 ExitStat = EX_TEMPFAIL; 671 return (EX_TEMPFAIL); 672 } 673 674 /* normal death -- return status */ 675 st = (st >> 8) & 0377; 676 return (st); 677 } 678 /* 679 ** OPENMAILER -- open connection to mailer. 680 ** 681 ** Parameters: 682 ** m -- mailer descriptor. 683 ** pvp -- parameter vector to pass to mailer. 684 ** ctladdr -- controlling address for user. 685 ** clever -- create a full duplex connection. 686 ** 687 ** Returns: 688 ** The mail connection info struct for this connection. 689 ** NULL on failure. 690 ** 691 ** Side Effects: 692 ** creates a mailer in a subprocess. 693 */ 694 695 MCI * 696 openmailer(m, pvp, ctladdr, clever, e) 697 MAILER *m; 698 char **pvp; 699 ADDRESS *ctladdr; 700 bool clever; 701 ENVELOPE *e; 702 { 703 int pid; 704 register MCI *mci; 705 int mpvect[2]; 706 int rpvect[2]; 707 extern FILE *fdopen(); 708 709 if (tTd(11, 1)) 710 { 711 printf("openmailer:"); 712 printav(pvp); 713 } 714 errno = 0; 715 716 CurHostName = m->m_mailer; 717 718 /* 719 ** Deal with the special case of mail handled through an IPC 720 ** connection. 721 ** In this case we don't actually fork. We must be 722 ** running SMTP for this to work. We will return a 723 ** zero pid to indicate that we are running IPC. 724 ** We also handle a debug version that just talks to stdin/out. 725 */ 726 727 /* check for Local Person Communication -- not for mortals!!! */ 728 if (strcmp(m->m_mailer, "[LPC]") == 0) 729 { 730 mci = (MCI *) xalloc(sizeof *mci); 731 bzero((char *) mci, sizeof *mci); 732 mci->mci_in = stdin; 733 mci->mci_out = stdout; 734 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 735 mci->mci_mailer = m; 736 } 737 else if (strcmp(m->m_mailer, "[IPC]") == 0 || 738 strcmp(m->m_mailer, "[TCP]") == 0) 739 { 740 #ifdef DAEMON 741 register int i; 742 register u_short port; 743 char *curhost; 744 extern MCI *mci_get(); 745 extern char *hostsignature(); 746 747 CurHostName = pvp[1]; 748 curhost = hostsignature(m, pvp[1], e); 749 750 if (curhost == NULL || curhost[0] == '\0') 751 { 752 syserr("null signature"); 753 return NULL; 754 } 755 756 if (!clever) 757 { 758 syserr("554 non-clever IPC"); 759 return NULL; 760 } 761 if (pvp[2] != NULL) 762 port = atoi(pvp[2]); 763 else 764 port = 0; 765 while (*curhost != '\0') 766 { 767 register char *p; 768 static char hostbuf[MAXNAME]; 769 770 /* pull the next host from the signature */ 771 p = strchr(curhost, ':'); 772 if (p == NULL) 773 p = &curhost[strlen(curhost)]; 774 strncpy(hostbuf, curhost, p - curhost); 775 hostbuf[p - curhost] = '\0'; 776 if (*p != '\0') 777 p++; 778 curhost = p; 779 780 /* see if we already know that this host is fried */ 781 CurHostName = hostbuf; 782 mci = mci_get(hostbuf, m); 783 if (mci->mci_state != MCIS_CLOSED) 784 { 785 if (tTd(11, 1)) 786 { 787 printf("openmailer: "); 788 mci_dump(mci); 789 } 790 CurHostName = mci->mci_host; 791 return mci; 792 } 793 mci->mci_mailer = m; 794 if (mci->mci_exitstat != EX_OK) 795 continue; 796 797 /* try the connection */ 798 setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 799 message("Connecting to %s (%s)...", 800 hostbuf, m->m_name); 801 i = makeconnection(hostbuf, port, mci, 802 bitnset(M_SECURE_PORT, m->m_flags)); 803 mci->mci_exitstat = i; 804 mci->mci_errno = errno; 805 if (i == EX_OK) 806 { 807 mci->mci_state = MCIS_OPENING; 808 mci_cache(mci); 809 break; 810 } 811 else if (tTd(11, 1)) 812 printf("openmailer: makeconnection => stat=%d, errno=%d\n", 813 i, errno); 814 815 816 /* enter status of this host */ 817 setstat(i); 818 } 819 mci->mci_pid = 0; 820 #else /* no DAEMON */ 821 syserr("554 openmailer: no IPC"); 822 if (tTd(11, 1)) 823 printf("openmailer: NULL\n"); 824 return NULL; 825 #endif /* DAEMON */ 826 } 827 else 828 { 829 /* create a pipe to shove the mail through */ 830 if (pipe(mpvect) < 0) 831 { 832 syserr("openmailer: pipe (to mailer)"); 833 if (tTd(11, 1)) 834 printf("openmailer: NULL\n"); 835 return NULL; 836 } 837 838 /* if this mailer speaks smtp, create a return pipe */ 839 if (clever && pipe(rpvect) < 0) 840 { 841 syserr("openmailer: pipe (from mailer)"); 842 (void) close(mpvect[0]); 843 (void) close(mpvect[1]); 844 if (tTd(11, 1)) 845 printf("openmailer: NULL\n"); 846 return NULL; 847 } 848 849 /* 850 ** Actually fork the mailer process. 851 ** DOFORK is clever about retrying. 852 ** 853 ** Dispose of SIGCHLD signal catchers that may be laying 854 ** around so that endmail will get it. 855 */ 856 857 if (e->e_xfp != NULL) 858 (void) fflush(e->e_xfp); /* for debugging */ 859 (void) fflush(stdout); 860 # ifdef SIGCHLD 861 (void) signal(SIGCHLD, SIG_DFL); 862 # endif /* SIGCHLD */ 863 DOFORK(FORK); 864 /* pid is set by DOFORK */ 865 if (pid < 0) 866 { 867 /* failure */ 868 syserr("openmailer: cannot fork"); 869 (void) close(mpvect[0]); 870 (void) close(mpvect[1]); 871 if (clever) 872 { 873 (void) close(rpvect[0]); 874 (void) close(rpvect[1]); 875 } 876 if (tTd(11, 1)) 877 printf("openmailer: NULL\n"); 878 return NULL; 879 } 880 else if (pid == 0) 881 { 882 int i; 883 int saveerrno; 884 char **ep; 885 char *env[MAXUSERENVIRON]; 886 extern char **environ; 887 extern int DtableSize; 888 889 /* child -- set up input & exec mailer */ 890 /* make diagnostic output be standard output */ 891 (void) signal(SIGINT, SIG_IGN); 892 (void) signal(SIGHUP, SIG_IGN); 893 (void) signal(SIGTERM, SIG_DFL); 894 895 /* close any other cached connections */ 896 mci_flush(FALSE, mci); 897 898 /* arrange to filter std & diag output of command */ 899 if (clever) 900 { 901 (void) close(rpvect[0]); 902 (void) close(1); 903 (void) dup(rpvect[1]); 904 (void) close(rpvect[1]); 905 } 906 else if (OpMode == MD_SMTP || HoldErrs) 907 { 908 /* put mailer output in transcript */ 909 (void) close(1); 910 (void) dup(fileno(e->e_xfp)); 911 } 912 (void) close(2); 913 (void) dup(1); 914 915 /* arrange to get standard input */ 916 (void) close(mpvect[1]); 917 (void) close(0); 918 if (dup(mpvect[0]) < 0) 919 { 920 syserr("Cannot dup to zero!"); 921 _exit(EX_OSERR); 922 } 923 (void) close(mpvect[0]); 924 if (!bitnset(M_RESTR, m->m_flags)) 925 { 926 if (ctladdr == NULL || ctladdr->q_uid == 0) 927 { 928 (void) setgid(DefGid); 929 (void) initgroups(DefUser, DefGid); 930 (void) setuid(DefUid); 931 } 932 else 933 { 934 (void) setgid(ctladdr->q_gid); 935 (void) initgroups(ctladdr->q_ruser? 936 ctladdr->q_ruser: ctladdr->q_user, 937 ctladdr->q_gid); 938 (void) setuid(ctladdr->q_uid); 939 } 940 } 941 942 /* arrange for all the files to be closed */ 943 for (i = 3; i < DtableSize; i++) 944 { 945 register int j; 946 if ((j = fcntl(i, F_GETFD, 0)) != -1) 947 (void)fcntl(i, F_SETFD, j|1); 948 } 949 950 /* set up the mailer environment */ 951 i = 0; 952 env[i++] = "AGENT=sendmail"; 953 for (ep = environ; *ep != NULL; ep++) 954 { 955 if (strncmp(*ep, "TZ=", 3) == 0) 956 env[i++] = *ep; 957 } 958 env[i++] = NULL; 959 960 /* try to execute the mailer */ 961 execve(m->m_mailer, pvp, env); 962 saveerrno = errno; 963 syserr("Cannot exec %s", m->m_mailer); 964 if (m == LocalMailer) 965 _exit(EX_TEMPFAIL); 966 if (transienterror(saveerrno)) 967 _exit(EX_TEMPFAIL); 968 _exit(EX_UNAVAILABLE); 969 } 970 971 /* 972 ** Set up return value. 973 */ 974 975 mci = (MCI *) xalloc(sizeof *mci); 976 bzero((char *) mci, sizeof *mci); 977 mci->mci_mailer = m; 978 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 979 mci->mci_pid = pid; 980 (void) close(mpvect[0]); 981 mci->mci_out = fdopen(mpvect[1], "w"); 982 if (clever) 983 { 984 (void) close(rpvect[1]); 985 mci->mci_in = fdopen(rpvect[0], "r"); 986 } 987 else 988 { 989 mci->mci_flags |= MCIF_TEMP; 990 mci->mci_in = NULL; 991 } 992 } 993 994 /* 995 ** If we are in SMTP opening state, send initial protocol. 996 */ 997 998 if (clever && mci->mci_state != MCIS_CLOSED) 999 { 1000 smtpinit(m, mci, e); 1001 } 1002 if (tTd(11, 1)) 1003 { 1004 printf("openmailer: "); 1005 mci_dump(mci); 1006 } 1007 1008 return mci; 1009 } 1010 /* 1011 ** GIVERESPONSE -- Interpret an error response from a mailer 1012 ** 1013 ** Parameters: 1014 ** stat -- the status code from the mailer (high byte 1015 ** only; core dumps must have been taken care of 1016 ** already). 1017 ** m -- the mailer info for this mailer. 1018 ** mci -- the mailer connection info -- can be NULL if the 1019 ** response is given before the connection is made. 1020 ** e -- the current envelope. 1021 ** 1022 ** Returns: 1023 ** none. 1024 ** 1025 ** Side Effects: 1026 ** Errors may be incremented. 1027 ** ExitStat may be set. 1028 */ 1029 1030 giveresponse(stat, m, mci, e) 1031 int stat; 1032 register MAILER *m; 1033 register MCI *mci; 1034 ENVELOPE *e; 1035 { 1036 register char *statmsg; 1037 extern char *SysExMsg[]; 1038 register int i; 1039 extern int N_SysEx; 1040 #ifdef NAMED_BIND 1041 extern int h_errno; 1042 #endif 1043 char buf[MAXLINE]; 1044 extern char *errstring(); 1045 1046 /* 1047 ** Compute status message from code. 1048 */ 1049 1050 i = stat - EX__BASE; 1051 if (stat == 0) 1052 statmsg = "250 Sent"; 1053 else if (i < 0 || i > N_SysEx) 1054 { 1055 (void) sprintf(buf, "554 unknown mailer error %d", stat); 1056 stat = EX_UNAVAILABLE; 1057 statmsg = buf; 1058 } 1059 else if (stat == EX_TEMPFAIL) 1060 { 1061 (void) strcpy(buf, SysExMsg[i] + 1); 1062 #ifdef NAMED_BIND 1063 if (h_errno == TRY_AGAIN) 1064 statmsg = errstring(h_errno+MAX_ERRNO); 1065 else 1066 #endif 1067 { 1068 if (errno != 0) 1069 statmsg = errstring(errno); 1070 else 1071 { 1072 #ifdef SMTP 1073 extern char SmtpError[]; 1074 1075 statmsg = SmtpError; 1076 #else /* SMTP */ 1077 statmsg = NULL; 1078 #endif /* SMTP */ 1079 } 1080 } 1081 if (statmsg != NULL && statmsg[0] != '\0') 1082 { 1083 (void) strcat(buf, ": "); 1084 (void) strcat(buf, statmsg); 1085 } 1086 statmsg = buf; 1087 } 1088 else 1089 { 1090 statmsg = SysExMsg[i]; 1091 if (*statmsg++ == ':') 1092 { 1093 (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 1094 statmsg = buf; 1095 } 1096 } 1097 1098 /* 1099 ** Print the message as appropriate 1100 */ 1101 1102 if (stat == EX_OK || stat == EX_TEMPFAIL) 1103 message(&statmsg[4], errstring(errno)); 1104 else 1105 { 1106 Errors++; 1107 usrerr(statmsg, errstring(errno)); 1108 } 1109 1110 /* 1111 ** Final cleanup. 1112 ** Log a record of the transaction. Compute the new 1113 ** ExitStat -- if we already had an error, stick with 1114 ** that. 1115 */ 1116 1117 if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 1118 logdelivery(m, mci, &statmsg[4], e); 1119 1120 if (stat != EX_TEMPFAIL) 1121 setstat(stat); 1122 if (stat != EX_OK) 1123 { 1124 if (e->e_message != NULL) 1125 free(e->e_message); 1126 e->e_message = newstr(&statmsg[4]); 1127 } 1128 errno = 0; 1129 #ifdef NAMED_BIND 1130 h_errno = 0; 1131 #endif 1132 } 1133 /* 1134 ** LOGDELIVERY -- log the delivery in the system log 1135 ** 1136 ** Parameters: 1137 ** m -- the mailer info. Can be NULL for initial queue. 1138 ** mci -- the mailer connection info -- can be NULL if the 1139 ** log is occuring when no connection is active. 1140 ** stat -- the message to print for the status. 1141 ** e -- the current envelope. 1142 ** 1143 ** Returns: 1144 ** none 1145 ** 1146 ** Side Effects: 1147 ** none 1148 */ 1149 1150 logdelivery(m, mci, stat, e) 1151 MAILER *m; 1152 register MCI *mci; 1153 char *stat; 1154 register ENVELOPE *e; 1155 { 1156 # ifdef LOG 1157 char *curhost; 1158 char buf[512]; 1159 extern char *pintvl(); 1160 extern char *macvalue(); 1161 1162 (void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 1163 1164 if (m != NULL) 1165 { 1166 (void) strcat(buf, ", mailer="); 1167 (void) strcat(buf, m->m_name); 1168 } 1169 1170 if (mci != NULL && mci->mci_host != NULL) 1171 { 1172 # ifdef DAEMON 1173 extern struct sockaddr_in CurHostAddr; 1174 extern char *inet_ntoa(); 1175 # endif 1176 1177 (void) strcat(buf, ", relay="); 1178 (void) strcat(buf, mci->mci_host); 1179 1180 # ifdef DAEMON 1181 (void) strcat(buf, " ("); 1182 (void) strcat(buf, inet_ntoa(CurHostAddr.sin_addr)); 1183 (void) strcat(buf, ")"); 1184 # endif 1185 } 1186 else 1187 { 1188 char *p = macvalue('h', e); 1189 1190 if (p != NULL && p[0] != '\0') 1191 { 1192 (void) strcat(buf, ", relay="); 1193 (void) strcat(buf, p); 1194 } 1195 } 1196 1197 syslog(LOG_INFO, "%s: to=%s, %s, stat=%s", 1198 e->e_id, e->e_to, buf, stat); 1199 # endif /* LOG */ 1200 } 1201 /* 1202 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 1203 ** 1204 ** This can be made an arbitrary message separator by changing $l 1205 ** 1206 ** One of the ugliest hacks seen by human eyes is contained herein: 1207 ** UUCP wants those stupid "remote from <host>" lines. Why oh why 1208 ** does a well-meaning programmer such as myself have to deal with 1209 ** this kind of antique garbage???? 1210 ** 1211 ** Parameters: 1212 ** fp -- the file to output to. 1213 ** m -- the mailer describing this entry. 1214 ** 1215 ** Returns: 1216 ** none 1217 ** 1218 ** Side Effects: 1219 ** outputs some text to fp. 1220 */ 1221 1222 putfromline(fp, m, e) 1223 register FILE *fp; 1224 register MAILER *m; 1225 ENVELOPE *e; 1226 { 1227 char *template = "\201l\n"; 1228 char buf[MAXLINE]; 1229 1230 if (bitnset(M_NHDR, m->m_flags)) 1231 return; 1232 1233 # ifdef UGLYUUCP 1234 if (bitnset(M_UGLYUUCP, m->m_flags)) 1235 { 1236 char *bang; 1237 char xbuf[MAXLINE]; 1238 1239 expand("\201g", buf, &buf[sizeof buf - 1], e); 1240 bang = strchr(buf, '!'); 1241 if (bang == NULL) 1242 syserr("554 No ! in UUCP! (%s)", buf); 1243 else 1244 { 1245 *bang++ = '\0'; 1246 (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 1247 template = xbuf; 1248 } 1249 } 1250 # endif /* UGLYUUCP */ 1251 expand(template, buf, &buf[sizeof buf - 1], e); 1252 putline(buf, fp, m); 1253 } 1254 /* 1255 ** PUTBODY -- put the body of a message. 1256 ** 1257 ** Parameters: 1258 ** fp -- file to output onto. 1259 ** m -- a mailer descriptor to control output format. 1260 ** e -- the envelope to put out. 1261 ** 1262 ** Returns: 1263 ** none. 1264 ** 1265 ** Side Effects: 1266 ** The message is written onto fp. 1267 */ 1268 1269 putbody(fp, m, e) 1270 FILE *fp; 1271 MAILER *m; 1272 register ENVELOPE *e; 1273 { 1274 char buf[MAXLINE]; 1275 1276 /* 1277 ** Output the body of the message 1278 */ 1279 1280 if (e->e_dfp == NULL) 1281 { 1282 if (e->e_df != NULL) 1283 { 1284 e->e_dfp = fopen(e->e_df, "r"); 1285 if (e->e_dfp == NULL) 1286 syserr("putbody: Cannot open %s for %s from %s", 1287 e->e_df, e->e_to, e->e_from); 1288 } 1289 else 1290 putline("<<< No Message Collected >>>", fp, m); 1291 } 1292 if (e->e_dfp != NULL) 1293 { 1294 rewind(e->e_dfp); 1295 while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 1296 { 1297 if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1298 strncmp(buf, "From ", 5) == 0) 1299 (void) putc('>', fp); 1300 putline(buf, fp, m); 1301 } 1302 1303 if (ferror(e->e_dfp)) 1304 { 1305 syserr("putbody: read error"); 1306 ExitStat = EX_IOERR; 1307 } 1308 } 1309 1310 (void) fflush(fp); 1311 if (ferror(fp) && errno != EPIPE) 1312 { 1313 syserr("putbody: write error"); 1314 ExitStat = EX_IOERR; 1315 } 1316 errno = 0; 1317 } 1318 /* 1319 ** MAILFILE -- Send a message to a file. 1320 ** 1321 ** If the file has the setuid/setgid bits set, but NO execute 1322 ** bits, sendmail will try to become the owner of that file 1323 ** rather than the real user. Obviously, this only works if 1324 ** sendmail runs as root. 1325 ** 1326 ** This could be done as a subordinate mailer, except that it 1327 ** is used implicitly to save messages in ~/dead.letter. We 1328 ** view this as being sufficiently important as to include it 1329 ** here. For example, if the system is dying, we shouldn't have 1330 ** to create another process plus some pipes to save the message. 1331 ** 1332 ** Parameters: 1333 ** filename -- the name of the file to send to. 1334 ** ctladdr -- the controlling address header -- includes 1335 ** the userid/groupid to be when sending. 1336 ** 1337 ** Returns: 1338 ** The exit code associated with the operation. 1339 ** 1340 ** Side Effects: 1341 ** none. 1342 */ 1343 1344 mailfile(filename, ctladdr, e) 1345 char *filename; 1346 ADDRESS *ctladdr; 1347 register ENVELOPE *e; 1348 { 1349 register FILE *f; 1350 register int pid; 1351 int mode; 1352 1353 /* 1354 ** Fork so we can change permissions here. 1355 ** Note that we MUST use fork, not vfork, because of 1356 ** the complications of calling subroutines, etc. 1357 */ 1358 1359 DOFORK(fork); 1360 1361 if (pid < 0) 1362 return (EX_OSERR); 1363 else if (pid == 0) 1364 { 1365 /* child -- actually write to file */ 1366 struct stat stb; 1367 1368 (void) signal(SIGINT, SIG_DFL); 1369 (void) signal(SIGHUP, SIG_DFL); 1370 (void) signal(SIGTERM, SIG_DFL); 1371 (void) umask(OldUmask); 1372 1373 if (stat(filename, &stb) < 0) 1374 stb.st_mode = 0666; 1375 mode = stb.st_mode; 1376 1377 /* limit the errors to those actually caused in the child */ 1378 errno = 0; 1379 ExitStat = EX_OK; 1380 1381 if (bitset(0111, stb.st_mode)) 1382 exit(EX_CANTCREAT); 1383 if (ctladdr == NULL) 1384 ctladdr = &e->e_from; 1385 else 1386 { 1387 /* ignore setuid and setgid bits */ 1388 mode &= ~(S_ISGID|S_ISUID); 1389 } 1390 1391 /* we have to open the dfile BEFORE setuid */ 1392 if (e->e_dfp == NULL && e->e_df != NULL) 1393 { 1394 e->e_dfp = fopen(e->e_df, "r"); 1395 if (e->e_dfp == NULL) 1396 { 1397 syserr("mailfile: Cannot open %s for %s from %s", 1398 e->e_df, e->e_to, e->e_from); 1399 } 1400 } 1401 1402 if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 1403 { 1404 if (ctladdr->q_uid == 0) 1405 { 1406 (void) setgid(DefGid); 1407 (void) initgroups(DefUser, DefGid); 1408 } 1409 else 1410 { 1411 (void) setgid(ctladdr->q_gid); 1412 (void) initgroups(ctladdr->q_ruser ? 1413 ctladdr->q_ruser : ctladdr->q_user, 1414 ctladdr->q_gid); 1415 } 1416 } 1417 if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 1418 { 1419 if (ctladdr->q_uid == 0) 1420 (void) setuid(DefUid); 1421 else 1422 (void) setuid(ctladdr->q_uid); 1423 } 1424 FileName = filename; 1425 LineNumber = 0; 1426 f = dfopen(filename, "a"); 1427 if (f == NULL) 1428 { 1429 message("554 cannot open"); 1430 exit(EX_CANTCREAT); 1431 } 1432 1433 putfromline(f, ProgMailer, e); 1434 (*e->e_puthdr)(f, ProgMailer, e); 1435 putline("\n", f, ProgMailer); 1436 (*e->e_putbody)(f, ProgMailer, e); 1437 putline("\n", f, ProgMailer); 1438 if (ferror(f)) 1439 { 1440 message("451 I/O error"); 1441 setstat(EX_IOERR); 1442 } 1443 (void) xfclose(f, "mailfile", filename); 1444 (void) fflush(stdout); 1445 1446 /* reset ISUID & ISGID bits for paranoid systems */ 1447 (void) chmod(filename, (int) stb.st_mode); 1448 exit(ExitStat); 1449 /*NOTREACHED*/ 1450 } 1451 else 1452 { 1453 /* parent -- wait for exit status */ 1454 int st; 1455 1456 st = waitfor(pid); 1457 if ((st & 0377) != 0) 1458 return (EX_UNAVAILABLE); 1459 else 1460 return ((st >> 8) & 0377); 1461 /*NOTREACHED*/ 1462 } 1463 } 1464 /* 1465 ** SENDALL -- actually send all the messages. 1466 ** 1467 ** Parameters: 1468 ** e -- the envelope to send. 1469 ** mode -- the delivery mode to use. If SM_DEFAULT, use 1470 ** the current SendMode. 1471 ** 1472 ** Returns: 1473 ** none. 1474 ** 1475 ** Side Effects: 1476 ** Scans the send lists and sends everything it finds. 1477 ** Delivers any appropriate error messages. 1478 ** If we are running in a non-interactive mode, takes the 1479 ** appropriate action. 1480 */ 1481 1482 sendall(e, mode) 1483 ENVELOPE *e; 1484 char mode; 1485 { 1486 register ADDRESS *q; 1487 char *owner; 1488 int otherowners; 1489 ENVELOPE *splitenv = NULL; 1490 1491 /* determine actual delivery mode */ 1492 if (mode == SM_DEFAULT) 1493 { 1494 extern bool shouldqueue(); 1495 1496 mode = SendMode; 1497 if (mode != SM_VERIFY && 1498 shouldqueue(e->e_msgpriority, e->e_ctime)) 1499 mode = SM_QUEUE; 1500 } 1501 1502 if (tTd(13, 1)) 1503 { 1504 printf("\nSENDALL: mode %c, e_from ", mode); 1505 printaddr(&e->e_from, FALSE); 1506 printf("sendqueue:\n"); 1507 printaddr(e->e_sendqueue, TRUE); 1508 } 1509 1510 /* 1511 ** Do any preprocessing necessary for the mode we are running. 1512 ** Check to make sure the hop count is reasonable. 1513 ** Delete sends to the sender in mailing lists. 1514 */ 1515 1516 CurEnv = e; 1517 1518 if (e->e_hopcount > MaxHopCount) 1519 { 1520 errno = 0; 1521 syserr("554 too many hops %d (%d max): from %s, to %s", 1522 e->e_hopcount, MaxHopCount, e->e_from.q_paddr, 1523 e->e_sendqueue->q_paddr); 1524 return; 1525 } 1526 1527 if (!MeToo) 1528 { 1529 extern ADDRESS *recipient(); 1530 1531 if (tTd(13, 5)) 1532 { 1533 printf("sendall: QDONTSEND "); 1534 printaddr(&e->e_from, FALSE); 1535 } 1536 e->e_from.q_flags |= QDONTSEND; 1537 (void) recipient(&e->e_from, &e->e_sendqueue, e); 1538 } 1539 1540 /* 1541 ** Handle alias owners. 1542 ** 1543 ** We scan up the q_alias chain looking for owners. 1544 ** We discard owners that are the same as the return path. 1545 */ 1546 1547 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1548 { 1549 register struct address *a; 1550 1551 for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias) 1552 continue; 1553 if (a != NULL) 1554 q->q_owner = a->q_owner; 1555 1556 if (q->q_owner != NULL && 1557 !bitset(QDONTSEND, q->q_flags) && 1558 strcmp(q->q_owner, e->e_from.q_paddr) == 0) 1559 q->q_owner = NULL; 1560 } 1561 1562 owner = ""; 1563 otherowners = 1; 1564 while (owner != NULL && otherowners > 0) 1565 { 1566 owner = NULL; 1567 otherowners = 0; 1568 1569 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1570 { 1571 if (bitset(QDONTSEND, q->q_flags)) 1572 continue; 1573 1574 if (q->q_owner != NULL) 1575 { 1576 if (owner == NULL) 1577 owner = q->q_owner; 1578 else if (owner != q->q_owner) 1579 { 1580 if (strcmp(owner, q->q_owner) == 0) 1581 { 1582 /* make future comparisons cheap */ 1583 q->q_owner = owner; 1584 } 1585 else 1586 { 1587 otherowners++; 1588 } 1589 owner = q->q_owner; 1590 } 1591 } 1592 else 1593 { 1594 otherowners++; 1595 } 1596 } 1597 1598 if (owner != NULL && otherowners > 0) 1599 { 1600 register ENVELOPE *ee; 1601 extern HDR *copyheader(); 1602 extern ADDRESS *copyqueue(); 1603 1604 /* 1605 ** Split this envelope into two. 1606 */ 1607 1608 ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE)); 1609 *ee = *e; 1610 ee->e_id = NULL; 1611 (void) queuename(ee, '\0'); 1612 1613 if (tTd(13, 1)) 1614 printf("sendall: split %s into %s\n", 1615 e->e_id, ee->e_id); 1616 1617 ee->e_header = copyheader(e->e_header); 1618 ee->e_sendqueue = copyqueue(e->e_sendqueue); 1619 ee->e_errorqueue = copyqueue(e->e_errorqueue); 1620 ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE); 1621 setsender(owner, ee, NULL, TRUE); 1622 if (tTd(13, 5)) 1623 { 1624 printf("sendall(split): QDONTSEND "); 1625 printaddr(&ee->e_from, FALSE); 1626 } 1627 ee->e_from.q_flags |= QDONTSEND; 1628 ee->e_dfp = NULL; 1629 ee->e_xfp = NULL; 1630 ee->e_lockfp = NULL; 1631 ee->e_df = NULL; 1632 ee->e_sibling = splitenv; 1633 splitenv = ee; 1634 1635 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1636 if (q->q_owner == owner) 1637 q->q_flags |= QDONTSEND; 1638 for (q = ee->e_sendqueue; q != NULL; q = q->q_next) 1639 if (q->q_owner != owner) 1640 q->q_flags |= QDONTSEND; 1641 1642 if (e->e_df != NULL && mode != SM_VERIFY) 1643 { 1644 ee->e_dfp = NULL; 1645 ee->e_df = newstr(queuename(ee, 'd')); 1646 if (link(e->e_df, ee->e_df) < 0) 1647 { 1648 syserr("sendall: link(%s, %s)", 1649 e->e_df, ee->e_df); 1650 } 1651 } 1652 1653 if (mode != SM_VERIFY) 1654 { 1655 char xfbuf1[20], xfbuf2[20]; 1656 1657 (void) strcpy(xfbuf1, queuename(e, 'x')); 1658 (void) strcpy(xfbuf2, queuename(ee, 'x')); 1659 if (link(xfbuf1, xfbuf2) < 0) 1660 { 1661 syserr("sendall: link(%s, %s)", 1662 xfbuf1, xfbuf2); 1663 } 1664 } 1665 } 1666 } 1667 1668 if (owner != NULL) 1669 { 1670 setsender(owner, e, NULL, TRUE); 1671 if (tTd(13, 5)) 1672 { 1673 printf("sendall(owner): QDONTSEND "); 1674 printaddr(&e->e_from, FALSE); 1675 } 1676 e->e_from.q_flags |= QDONTSEND; 1677 } 1678 1679 # ifdef QUEUE 1680 if ((mode == SM_QUEUE || mode == SM_FORK || 1681 (mode != SM_VERIFY && SuperSafe)) && 1682 !bitset(EF_INQUEUE, e->e_flags)) 1683 queueup(e, TRUE, mode == SM_QUEUE); 1684 #endif /* QUEUE */ 1685 1686 if (splitenv != NULL) 1687 { 1688 register ENVELOPE *ee; 1689 1690 if (tTd(13, 1)) 1691 { 1692 printf("\nsendall: Split queue; remaining queue:\n"); 1693 printaddr(e->e_sendqueue, TRUE); 1694 } 1695 1696 for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 1697 { 1698 CurEnv = ee; 1699 sendenvelope(ee, mode); 1700 } 1701 1702 CurEnv = e; 1703 } 1704 sendenvelope(e, mode); 1705 1706 for (; splitenv != NULL; splitenv = splitenv->e_sibling) 1707 dropenvelope(splitenv); 1708 } 1709 1710 sendenvelope(e, mode) 1711 register ENVELOPE *e; 1712 char mode; 1713 { 1714 bool oldverbose; 1715 int pid; 1716 register ADDRESS *q; 1717 #ifdef LOCKF 1718 struct flock lfd; 1719 #endif 1720 1721 oldverbose = Verbose; 1722 switch (mode) 1723 { 1724 case SM_VERIFY: 1725 Verbose = TRUE; 1726 break; 1727 1728 case SM_QUEUE: 1729 queueonly: 1730 e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 1731 return; 1732 1733 case SM_FORK: 1734 if (e->e_xfp != NULL) 1735 (void) fflush(e->e_xfp); 1736 1737 # ifdef LOCKF 1738 /* 1739 ** Since lockf has the interesting semantic that the 1740 ** lock is lost when we fork, we have to risk losing 1741 ** the lock here by closing before the fork, and then 1742 ** trying to get it back in the child. 1743 */ 1744 1745 if (e->e_lockfp != NULL) 1746 { 1747 (void) xfclose(e->e_lockfp, "sendenvelope", "lockfp"); 1748 e->e_lockfp = NULL; 1749 } 1750 # endif /* LOCKF */ 1751 1752 pid = fork(); 1753 if (pid < 0) 1754 { 1755 goto queueonly; 1756 } 1757 else if (pid > 0) 1758 { 1759 /* be sure we leave the temp files to our child */ 1760 e->e_id = e->e_df = NULL; 1761 # ifndef LOCKF 1762 if (e->e_lockfp != NULL) 1763 { 1764 (void) xfclose(e->e_lockfp, "sendenvelope", "lockfp"); 1765 e->e_lockfp = NULL; 1766 } 1767 # endif 1768 1769 /* close any random open files in the envelope */ 1770 if (e->e_dfp != NULL) 1771 { 1772 (void) xfclose(e->e_dfp, "sendenvelope", "dfp"); 1773 e->e_dfp = NULL; 1774 } 1775 if (e->e_xfp != NULL) 1776 { 1777 (void) xfclose(e->e_xfp, "sendenvelope", "xfp"); 1778 e->e_xfp = NULL; 1779 } 1780 return; 1781 } 1782 1783 /* double fork to avoid zombies */ 1784 if (fork() > 0) 1785 exit(EX_OK); 1786 1787 /* be sure we are immune from the terminal */ 1788 disconnect(FALSE, e); 1789 1790 # ifdef LOCKF 1791 /* 1792 ** Now try to get our lock back. 1793 */ 1794 1795 lfd.l_type = F_WRLCK; 1796 lfd.l_whence = lfd.l_start = lfd.l_len = 0; 1797 e->e_lockfp = fopen(queuename(e, 'q'), "r+"); 1798 if (e->e_lockfp == NULL || 1799 fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0) 1800 { 1801 /* oops.... lost it */ 1802 # ifdef LOG 1803 if (LogLevel > 29) 1804 syslog(LOG_NOTICE, "%s: lost lock: %m", 1805 e->e_id); 1806 # endif /* LOG */ 1807 exit(EX_OK); 1808 } 1809 # endif /* LOCKF */ 1810 1811 /* 1812 ** Close any cached connections. 1813 ** 1814 ** We don't send the QUIT protocol because the parent 1815 ** still knows about the connection. 1816 ** 1817 ** This should only happen when delivering an error 1818 ** message. 1819 */ 1820 1821 mci_flush(FALSE, NULL); 1822 1823 break; 1824 } 1825 1826 /* 1827 ** Run through the list and send everything. 1828 */ 1829 1830 e->e_nsent = 0; 1831 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1832 { 1833 if (mode == SM_VERIFY) 1834 { 1835 e->e_to = q->q_paddr; 1836 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1837 message("deliverable"); 1838 } 1839 else if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1840 { 1841 # ifdef QUEUE 1842 /* 1843 ** Checkpoint the send list every few addresses 1844 */ 1845 1846 if (e->e_nsent >= CheckpointInterval) 1847 { 1848 queueup(e, TRUE, FALSE); 1849 e->e_nsent = 0; 1850 } 1851 # endif /* QUEUE */ 1852 (void) deliver(e, q); 1853 } 1854 } 1855 Verbose = oldverbose; 1856 1857 /* 1858 ** Now run through and check for errors. 1859 */ 1860 1861 if (mode == SM_VERIFY) 1862 { 1863 return; 1864 } 1865 1866 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1867 { 1868 if (tTd(13, 3)) 1869 { 1870 printf("Checking "); 1871 printaddr(q, FALSE); 1872 } 1873 1874 /* only send errors if the message failed */ 1875 if (!bitset(QBADADDR, q->q_flags)) 1876 continue; 1877 1878 e->e_flags |= EF_FATALERRS; 1879 1880 if (q->q_owner == NULL && strcmp(e->e_from.q_paddr, "<>") != 0) 1881 (void) sendtolist(e->e_from.q_paddr, NULL, 1882 &e->e_errorqueue, e); 1883 } 1884 1885 if (mode == SM_FORK) 1886 finis(); 1887 } 1888 /* 1889 ** HOSTSIGNATURE -- return the "signature" for a host. 1890 ** 1891 ** The signature describes how we are going to send this -- it 1892 ** can be just the hostname (for non-Internet hosts) or can be 1893 ** an ordered list of MX hosts. 1894 ** 1895 ** Parameters: 1896 ** m -- the mailer describing this host. 1897 ** host -- the host name. 1898 ** e -- the current envelope. 1899 ** 1900 ** Returns: 1901 ** The signature for this host. 1902 ** 1903 ** Side Effects: 1904 ** Can tweak the symbol table. 1905 */ 1906 1907 char * 1908 hostsignature(m, host, e) 1909 register MAILER *m; 1910 char *host; 1911 ENVELOPE *e; 1912 { 1913 register char *p; 1914 register STAB *s; 1915 int i; 1916 int len; 1917 #ifdef NAMED_BIND 1918 int nmx; 1919 auto int rcode; 1920 char *mxhosts[MAXMXHOSTS + 1]; 1921 static char myhostbuf[MAXNAME]; 1922 #endif 1923 1924 /* 1925 ** Check to see if this uses IPC -- if not, it can't have MX records. 1926 */ 1927 1928 p = m->m_mailer; 1929 if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 1930 { 1931 /* just an ordinary mailer */ 1932 return host; 1933 } 1934 1935 /* 1936 ** If it is a numeric address, just return it. 1937 */ 1938 1939 if (host[0] == '[') 1940 return host; 1941 1942 /* 1943 ** Look it up in the symbol table. 1944 */ 1945 1946 s = stab(host, ST_HOSTSIG, ST_ENTER); 1947 if (s->s_hostsig != NULL) 1948 return s->s_hostsig; 1949 1950 /* 1951 ** Not already there -- create a signature. 1952 */ 1953 1954 #ifdef NAMED_BIND 1955 if (myhostbuf[0] == '\0') 1956 expand("\201j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e); 1957 1958 nmx = getmxrr(host, mxhosts, myhostbuf, &rcode); 1959 1960 if (nmx <= 0) 1961 { 1962 register MCI *mci; 1963 extern int errno; 1964 extern MCI *mci_get(); 1965 1966 /* update the connection info for this host */ 1967 mci = mci_get(host, m); 1968 mci->mci_exitstat = rcode; 1969 mci->mci_errno = errno; 1970 1971 /* and return the original host name as the signature */ 1972 s->s_hostsig = host; 1973 return host; 1974 } 1975 1976 len = 0; 1977 for (i = 0; i < nmx; i++) 1978 { 1979 len += strlen(mxhosts[i]) + 1; 1980 } 1981 s->s_hostsig = p = xalloc(len); 1982 for (i = 0; i < nmx; i++) 1983 { 1984 if (i != 0) 1985 *p++ = ':'; 1986 strcpy(p, mxhosts[i]); 1987 p += strlen(p); 1988 } 1989 makelower(s->s_hostsig); 1990 #else 1991 /* not using BIND -- the signature is just the host name */ 1992 s->s_hostsig = host; 1993 #endif 1994 if (tTd(17, 1)) 1995 printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 1996 return s->s_hostsig; 1997 } 1998