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.52 (Berkeley) 03/19/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 && !bitset(EF_QUEUERUN, e->e_flags) && 102 bitnset(M_EXPENSIVE, m->m_flags) && !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 char buf[MAXLINE]; 534 extern char *pintvl(); 535 536 if (rcode == EX_OK) 537 return; 538 else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR) 539 q->q_flags |= QBADADDR; 540 else if (curtime() > e->e_ctime + TimeOuts.to_q_return) 541 { 542 if (!bitset(EF_TIMEOUT, e->e_flags)) 543 { 544 (void) sprintf(buf, "Cannot send message for %s", 545 pintvl(TimeOuts.to_q_return, 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 { 557 q->q_flags |= QQUEUEUP; 558 if (TimeOuts.to_q_warning > 0 && 559 curtime() > e->e_ctime + TimeOuts.to_q_warning) 560 { 561 if (!bitset(EF_WARNING, e->e_flags) && 562 e->e_class >= 0) 563 { 564 (void) sprintf(buf, 565 "warning: cannot send message for %s", 566 pintvl(TimeOuts.to_q_warning, FALSE)); 567 if (e->e_message != NULL) 568 free(e->e_message); 569 e->e_message = newstr(buf); 570 message(buf); 571 e->e_flags |= EF_WARNING|EF_TIMEOUT; 572 } 573 fprintf(e->e_xfp, 574 "%s... Warning: message still undelivered after %s\n", 575 q->q_paddr, pintvl(TimeOuts.to_q_warning, FALSE)); 576 fprintf(e->e_xfp, "Will keep trying until message is %s old\n", 577 pintvl(TimeOuts.to_q_return, FALSE)); 578 } 579 } 580 } 581 /* 582 ** DOFORK -- do a fork, retrying a couple of times on failure. 583 ** 584 ** This MUST be a macro, since after a vfork we are running 585 ** two processes on the same stack!!! 586 ** 587 ** Parameters: 588 ** none. 589 ** 590 ** Returns: 591 ** From a macro??? You've got to be kidding! 592 ** 593 ** Side Effects: 594 ** Modifies the ==> LOCAL <== variable 'pid', leaving: 595 ** pid of child in parent, zero in child. 596 ** -1 on unrecoverable error. 597 ** 598 ** Notes: 599 ** I'm awfully sorry this looks so awful. That's 600 ** vfork for you..... 601 */ 602 603 # define NFORKTRIES 5 604 605 # ifndef FORK 606 # define FORK fork 607 # endif 608 609 # define DOFORK(fORKfN) \ 610 {\ 611 register int i;\ 612 \ 613 for (i = NFORKTRIES; --i >= 0; )\ 614 {\ 615 pid = fORKfN();\ 616 if (pid >= 0)\ 617 break;\ 618 if (i > 0)\ 619 sleep((unsigned) NFORKTRIES - i);\ 620 }\ 621 } 622 /* 623 ** DOFORK -- simple fork interface to DOFORK. 624 ** 625 ** Parameters: 626 ** none. 627 ** 628 ** Returns: 629 ** pid of child in parent. 630 ** zero in child. 631 ** -1 on error. 632 ** 633 ** Side Effects: 634 ** returns twice, once in parent and once in child. 635 */ 636 637 dofork() 638 { 639 register int pid; 640 641 DOFORK(fork); 642 return (pid); 643 } 644 /* 645 ** ENDMAILER -- Wait for mailer to terminate. 646 ** 647 ** We should never get fatal errors (e.g., segmentation 648 ** violation), so we report those specially. For other 649 ** errors, we choose a status message (into statmsg), 650 ** and if it represents an error, we print it. 651 ** 652 ** Parameters: 653 ** pid -- pid of mailer. 654 ** name -- name of mailer (for error messages). 655 ** 656 ** Returns: 657 ** exit code of mailer. 658 ** 659 ** Side Effects: 660 ** none. 661 */ 662 663 endmailer(mci, name) 664 register MCI *mci; 665 char *name; 666 { 667 int st; 668 669 /* close any connections */ 670 if (mci->mci_in != NULL) 671 (void) xfclose(mci->mci_in, name, "mci_in"); 672 if (mci->mci_out != NULL) 673 (void) xfclose(mci->mci_out, name, "mci_out"); 674 mci->mci_in = mci->mci_out = NULL; 675 mci->mci_state = MCIS_CLOSED; 676 677 /* in the IPC case there is nothing to wait for */ 678 if (mci->mci_pid == 0) 679 return (EX_OK); 680 681 /* wait for the mailer process to die and collect status */ 682 st = waitfor(mci->mci_pid); 683 if (st == -1) 684 { 685 syserr("endmailer %s: wait", name); 686 return (EX_SOFTWARE); 687 } 688 689 /* see if it died a horrid death */ 690 if ((st & 0377) != 0) 691 { 692 syserr("mailer %s died with signal %o", name, st); 693 ExitStat = EX_TEMPFAIL; 694 return (EX_TEMPFAIL); 695 } 696 697 /* normal death -- return status */ 698 st = (st >> 8) & 0377; 699 return (st); 700 } 701 /* 702 ** OPENMAILER -- open connection to mailer. 703 ** 704 ** Parameters: 705 ** m -- mailer descriptor. 706 ** pvp -- parameter vector to pass to mailer. 707 ** ctladdr -- controlling address for user. 708 ** clever -- create a full duplex connection. 709 ** 710 ** Returns: 711 ** The mail connection info struct for this connection. 712 ** NULL on failure. 713 ** 714 ** Side Effects: 715 ** creates a mailer in a subprocess. 716 */ 717 718 MCI * 719 openmailer(m, pvp, ctladdr, clever, e) 720 MAILER *m; 721 char **pvp; 722 ADDRESS *ctladdr; 723 bool clever; 724 ENVELOPE *e; 725 { 726 int pid; 727 register MCI *mci; 728 int mpvect[2]; 729 int rpvect[2]; 730 extern FILE *fdopen(); 731 732 if (tTd(11, 1)) 733 { 734 printf("openmailer:"); 735 printav(pvp); 736 } 737 errno = 0; 738 739 CurHostName = m->m_mailer; 740 741 /* 742 ** Deal with the special case of mail handled through an IPC 743 ** connection. 744 ** In this case we don't actually fork. We must be 745 ** running SMTP for this to work. We will return a 746 ** zero pid to indicate that we are running IPC. 747 ** We also handle a debug version that just talks to stdin/out. 748 */ 749 750 /* check for Local Person Communication -- not for mortals!!! */ 751 if (strcmp(m->m_mailer, "[LPC]") == 0) 752 { 753 mci = (MCI *) xalloc(sizeof *mci); 754 bzero((char *) mci, sizeof *mci); 755 mci->mci_in = stdin; 756 mci->mci_out = stdout; 757 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 758 mci->mci_mailer = m; 759 } 760 else if (strcmp(m->m_mailer, "[IPC]") == 0 || 761 strcmp(m->m_mailer, "[TCP]") == 0) 762 { 763 #ifdef DAEMON 764 register int i; 765 register u_short port; 766 char *curhost; 767 extern MCI *mci_get(); 768 extern char *hostsignature(); 769 770 CurHostName = pvp[1]; 771 curhost = hostsignature(m, pvp[1], e); 772 773 if (curhost == NULL || curhost[0] == '\0') 774 { 775 syserr("null signature"); 776 return NULL; 777 } 778 779 if (!clever) 780 { 781 syserr("554 non-clever IPC"); 782 return NULL; 783 } 784 if (pvp[2] != NULL) 785 port = atoi(pvp[2]); 786 else 787 port = 0; 788 while (*curhost != '\0') 789 { 790 register char *p; 791 static char hostbuf[MAXNAME]; 792 793 /* pull the next host from the signature */ 794 p = strchr(curhost, ':'); 795 if (p == NULL) 796 p = &curhost[strlen(curhost)]; 797 strncpy(hostbuf, curhost, p - curhost); 798 hostbuf[p - curhost] = '\0'; 799 if (*p != '\0') 800 p++; 801 curhost = p; 802 803 /* see if we already know that this host is fried */ 804 CurHostName = hostbuf; 805 mci = mci_get(hostbuf, m); 806 if (mci->mci_state != MCIS_CLOSED) 807 { 808 if (tTd(11, 1)) 809 { 810 printf("openmailer: "); 811 mci_dump(mci); 812 } 813 CurHostName = mci->mci_host; 814 return mci; 815 } 816 mci->mci_mailer = m; 817 if (mci->mci_exitstat != EX_OK) 818 continue; 819 820 /* try the connection */ 821 setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 822 message("Connecting to %s (%s)...", 823 hostbuf, m->m_name); 824 i = makeconnection(hostbuf, port, mci, 825 bitnset(M_SECURE_PORT, m->m_flags)); 826 mci->mci_exitstat = i; 827 mci->mci_errno = errno; 828 if (i == EX_OK) 829 { 830 mci->mci_state = MCIS_OPENING; 831 mci_cache(mci); 832 break; 833 } 834 else if (tTd(11, 1)) 835 printf("openmailer: makeconnection => stat=%d, errno=%d\n", 836 i, errno); 837 838 839 /* enter status of this host */ 840 setstat(i); 841 } 842 mci->mci_pid = 0; 843 #else /* no DAEMON */ 844 syserr("554 openmailer: no IPC"); 845 if (tTd(11, 1)) 846 printf("openmailer: NULL\n"); 847 return NULL; 848 #endif /* DAEMON */ 849 } 850 else 851 { 852 /* create a pipe to shove the mail through */ 853 if (pipe(mpvect) < 0) 854 { 855 syserr("openmailer: pipe (to mailer)"); 856 if (tTd(11, 1)) 857 printf("openmailer: NULL\n"); 858 return NULL; 859 } 860 861 /* if this mailer speaks smtp, create a return pipe */ 862 if (clever && pipe(rpvect) < 0) 863 { 864 syserr("openmailer: pipe (from mailer)"); 865 (void) close(mpvect[0]); 866 (void) close(mpvect[1]); 867 if (tTd(11, 1)) 868 printf("openmailer: NULL\n"); 869 return NULL; 870 } 871 872 /* 873 ** Actually fork the mailer process. 874 ** DOFORK is clever about retrying. 875 ** 876 ** Dispose of SIGCHLD signal catchers that may be laying 877 ** around so that endmail will get it. 878 */ 879 880 if (e->e_xfp != NULL) 881 (void) fflush(e->e_xfp); /* for debugging */ 882 (void) fflush(stdout); 883 # ifdef SIGCHLD 884 (void) signal(SIGCHLD, SIG_DFL); 885 # endif /* SIGCHLD */ 886 DOFORK(FORK); 887 /* pid is set by DOFORK */ 888 if (pid < 0) 889 { 890 /* failure */ 891 syserr("openmailer: cannot fork"); 892 (void) close(mpvect[0]); 893 (void) close(mpvect[1]); 894 if (clever) 895 { 896 (void) close(rpvect[0]); 897 (void) close(rpvect[1]); 898 } 899 if (tTd(11, 1)) 900 printf("openmailer: NULL\n"); 901 return NULL; 902 } 903 else if (pid == 0) 904 { 905 int i; 906 int saveerrno; 907 char **ep; 908 char *env[MAXUSERENVIRON]; 909 extern char **environ; 910 extern int DtableSize; 911 912 /* child -- set up input & exec mailer */ 913 /* make diagnostic output be standard output */ 914 (void) signal(SIGINT, SIG_IGN); 915 (void) signal(SIGHUP, SIG_IGN); 916 (void) signal(SIGTERM, SIG_DFL); 917 918 /* close any other cached connections */ 919 mci_flush(FALSE, mci); 920 921 /* arrange to filter std & diag output of command */ 922 if (clever) 923 { 924 (void) close(rpvect[0]); 925 (void) dup2(rpvect[1], STDOUT_FILENO); 926 (void) close(rpvect[1]); 927 } 928 else if (OpMode == MD_SMTP || HoldErrs) 929 { 930 /* put mailer output in transcript */ 931 (void) dup2(fileno(e->e_xfp), STDOUT_FILENO); 932 } 933 (void) dup2(STDOUT_FILENO, STDERR_FILENO); 934 935 /* arrange to get standard input */ 936 (void) close(mpvect[1]); 937 if (dup2(mpvect[0], STDIN_FILENO) < 0) 938 { 939 syserr("Cannot dup to zero!"); 940 _exit(EX_OSERR); 941 } 942 (void) close(mpvect[0]); 943 if (!bitnset(M_RESTR, m->m_flags)) 944 { 945 if (ctladdr == NULL || ctladdr->q_uid == 0) 946 { 947 (void) setgid(DefGid); 948 (void) initgroups(DefUser, DefGid); 949 (void) setuid(DefUid); 950 } 951 else 952 { 953 (void) setgid(ctladdr->q_gid); 954 (void) initgroups(ctladdr->q_ruser? 955 ctladdr->q_ruser: ctladdr->q_user, 956 ctladdr->q_gid); 957 (void) setuid(ctladdr->q_uid); 958 } 959 } 960 961 /* arrange for all the files to be closed */ 962 for (i = 3; i < DtableSize; i++) 963 { 964 register int j; 965 if ((j = fcntl(i, F_GETFD, 0)) != -1) 966 (void)fcntl(i, F_SETFD, j|1); 967 } 968 969 /* set up the mailer environment */ 970 i = 0; 971 env[i++] = "AGENT=sendmail"; 972 for (ep = environ; *ep != NULL; ep++) 973 { 974 if (strncmp(*ep, "TZ=", 3) == 0) 975 env[i++] = *ep; 976 } 977 env[i++] = NULL; 978 979 /* try to execute the mailer */ 980 execve(m->m_mailer, pvp, env); 981 saveerrno = errno; 982 syserr("Cannot exec %s", m->m_mailer); 983 if (m == LocalMailer) 984 _exit(EX_TEMPFAIL); 985 if (transienterror(saveerrno)) 986 _exit(EX_TEMPFAIL); 987 _exit(EX_UNAVAILABLE); 988 } 989 990 /* 991 ** Set up return value. 992 */ 993 994 mci = (MCI *) xalloc(sizeof *mci); 995 bzero((char *) mci, sizeof *mci); 996 mci->mci_mailer = m; 997 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 998 mci->mci_pid = pid; 999 (void) close(mpvect[0]); 1000 mci->mci_out = fdopen(mpvect[1], "w"); 1001 if (clever) 1002 { 1003 (void) close(rpvect[1]); 1004 mci->mci_in = fdopen(rpvect[0], "r"); 1005 } 1006 else 1007 { 1008 mci->mci_flags |= MCIF_TEMP; 1009 mci->mci_in = NULL; 1010 } 1011 } 1012 1013 /* 1014 ** If we are in SMTP opening state, send initial protocol. 1015 */ 1016 1017 if (clever && mci->mci_state != MCIS_CLOSED) 1018 { 1019 smtpinit(m, mci, e); 1020 } 1021 if (tTd(11, 1)) 1022 { 1023 printf("openmailer: "); 1024 mci_dump(mci); 1025 } 1026 1027 return mci; 1028 } 1029 /* 1030 ** GIVERESPONSE -- Interpret an error response from a mailer 1031 ** 1032 ** Parameters: 1033 ** stat -- the status code from the mailer (high byte 1034 ** only; core dumps must have been taken care of 1035 ** already). 1036 ** m -- the mailer info for this mailer. 1037 ** mci -- the mailer connection info -- can be NULL if the 1038 ** response is given before the connection is made. 1039 ** e -- the current envelope. 1040 ** 1041 ** Returns: 1042 ** none. 1043 ** 1044 ** Side Effects: 1045 ** Errors may be incremented. 1046 ** ExitStat may be set. 1047 */ 1048 1049 giveresponse(stat, m, mci, e) 1050 int stat; 1051 register MAILER *m; 1052 register MCI *mci; 1053 ENVELOPE *e; 1054 { 1055 register char *statmsg; 1056 extern char *SysExMsg[]; 1057 register int i; 1058 extern int N_SysEx; 1059 #ifdef NAMED_BIND 1060 extern int h_errno; 1061 #endif 1062 char buf[MAXLINE]; 1063 extern char *errstring(); 1064 1065 /* 1066 ** Compute status message from code. 1067 */ 1068 1069 i = stat - EX__BASE; 1070 if (stat == 0) 1071 statmsg = "250 Sent"; 1072 else if (i < 0 || i > N_SysEx) 1073 { 1074 (void) sprintf(buf, "554 unknown mailer error %d", stat); 1075 stat = EX_UNAVAILABLE; 1076 statmsg = buf; 1077 } 1078 else if (stat == EX_TEMPFAIL) 1079 { 1080 (void) strcpy(buf, SysExMsg[i] + 1); 1081 #ifdef NAMED_BIND 1082 if (h_errno == TRY_AGAIN) 1083 statmsg = errstring(h_errno+MAX_ERRNO); 1084 else 1085 #endif 1086 { 1087 if (errno != 0) 1088 statmsg = errstring(errno); 1089 else 1090 { 1091 #ifdef SMTP 1092 extern char SmtpError[]; 1093 1094 statmsg = SmtpError; 1095 #else /* SMTP */ 1096 statmsg = NULL; 1097 #endif /* SMTP */ 1098 } 1099 } 1100 if (statmsg != NULL && statmsg[0] != '\0') 1101 { 1102 (void) strcat(buf, ": "); 1103 (void) strcat(buf, statmsg); 1104 } 1105 statmsg = buf; 1106 } 1107 else 1108 { 1109 statmsg = SysExMsg[i]; 1110 if (*statmsg++ == ':') 1111 { 1112 (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 1113 statmsg = buf; 1114 } 1115 } 1116 1117 /* 1118 ** Print the message as appropriate 1119 */ 1120 1121 if (stat == EX_OK || stat == EX_TEMPFAIL) 1122 message(&statmsg[4], errstring(errno)); 1123 else 1124 { 1125 Errors++; 1126 usrerr(statmsg, errstring(errno)); 1127 } 1128 1129 /* 1130 ** Final cleanup. 1131 ** Log a record of the transaction. Compute the new 1132 ** ExitStat -- if we already had an error, stick with 1133 ** that. 1134 */ 1135 1136 if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 1137 logdelivery(m, mci, &statmsg[4], e); 1138 1139 if (stat != EX_TEMPFAIL) 1140 setstat(stat); 1141 if (stat != EX_OK) 1142 { 1143 if (e->e_message != NULL) 1144 free(e->e_message); 1145 e->e_message = newstr(&statmsg[4]); 1146 } 1147 errno = 0; 1148 #ifdef NAMED_BIND 1149 h_errno = 0; 1150 #endif 1151 } 1152 /* 1153 ** LOGDELIVERY -- log the delivery in the system log 1154 ** 1155 ** Parameters: 1156 ** m -- the mailer info. Can be NULL for initial queue. 1157 ** mci -- the mailer connection info -- can be NULL if the 1158 ** log is occuring when no connection is active. 1159 ** stat -- the message to print for the status. 1160 ** e -- the current envelope. 1161 ** 1162 ** Returns: 1163 ** none 1164 ** 1165 ** Side Effects: 1166 ** none 1167 */ 1168 1169 logdelivery(m, mci, stat, e) 1170 MAILER *m; 1171 register MCI *mci; 1172 char *stat; 1173 register ENVELOPE *e; 1174 { 1175 # ifdef LOG 1176 char *curhost; 1177 char buf[512]; 1178 extern char *pintvl(); 1179 extern char *macvalue(); 1180 1181 (void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 1182 1183 if (m != NULL) 1184 { 1185 (void) strcat(buf, ", mailer="); 1186 (void) strcat(buf, m->m_name); 1187 } 1188 1189 if (mci != NULL && mci->mci_host != NULL) 1190 { 1191 # ifdef DAEMON 1192 extern struct sockaddr_in CurHostAddr; 1193 extern char *inet_ntoa(); 1194 # endif 1195 1196 (void) strcat(buf, ", relay="); 1197 (void) strcat(buf, mci->mci_host); 1198 1199 # ifdef DAEMON 1200 (void) strcat(buf, " ("); 1201 (void) strcat(buf, inet_ntoa(CurHostAddr.sin_addr)); 1202 (void) strcat(buf, ")"); 1203 # endif 1204 } 1205 else 1206 { 1207 char *p = macvalue('h', e); 1208 1209 if (p != NULL && p[0] != '\0') 1210 { 1211 (void) strcat(buf, ", relay="); 1212 (void) strcat(buf, p); 1213 } 1214 } 1215 1216 syslog(LOG_INFO, "%s: to=%s, %s, stat=%s", 1217 e->e_id, e->e_to, buf, stat); 1218 # endif /* LOG */ 1219 } 1220 /* 1221 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 1222 ** 1223 ** This can be made an arbitrary message separator by changing $l 1224 ** 1225 ** One of the ugliest hacks seen by human eyes is contained herein: 1226 ** UUCP wants those stupid "remote from <host>" lines. Why oh why 1227 ** does a well-meaning programmer such as myself have to deal with 1228 ** this kind of antique garbage???? 1229 ** 1230 ** Parameters: 1231 ** fp -- the file to output to. 1232 ** m -- the mailer describing this entry. 1233 ** 1234 ** Returns: 1235 ** none 1236 ** 1237 ** Side Effects: 1238 ** outputs some text to fp. 1239 */ 1240 1241 putfromline(fp, m, e) 1242 register FILE *fp; 1243 register MAILER *m; 1244 ENVELOPE *e; 1245 { 1246 char *template = "\201l\n"; 1247 char buf[MAXLINE]; 1248 1249 if (bitnset(M_NHDR, m->m_flags)) 1250 return; 1251 1252 # ifdef UGLYUUCP 1253 if (bitnset(M_UGLYUUCP, m->m_flags)) 1254 { 1255 char *bang; 1256 char xbuf[MAXLINE]; 1257 1258 expand("\201g", buf, &buf[sizeof buf - 1], e); 1259 bang = strchr(buf, '!'); 1260 if (bang == NULL) 1261 syserr("554 No ! in UUCP! (%s)", buf); 1262 else 1263 { 1264 *bang++ = '\0'; 1265 (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 1266 template = xbuf; 1267 } 1268 } 1269 # endif /* UGLYUUCP */ 1270 expand(template, buf, &buf[sizeof buf - 1], e); 1271 putline(buf, fp, m); 1272 } 1273 /* 1274 ** PUTBODY -- put the body of a message. 1275 ** 1276 ** Parameters: 1277 ** fp -- file to output onto. 1278 ** m -- a mailer descriptor to control output format. 1279 ** e -- the envelope to put out. 1280 ** 1281 ** Returns: 1282 ** none. 1283 ** 1284 ** Side Effects: 1285 ** The message is written onto fp. 1286 */ 1287 1288 putbody(fp, m, e) 1289 FILE *fp; 1290 MAILER *m; 1291 register ENVELOPE *e; 1292 { 1293 char buf[MAXLINE]; 1294 1295 /* 1296 ** Output the body of the message 1297 */ 1298 1299 if (e->e_dfp == NULL) 1300 { 1301 if (e->e_df != NULL) 1302 { 1303 e->e_dfp = fopen(e->e_df, "r"); 1304 if (e->e_dfp == NULL) 1305 syserr("putbody: Cannot open %s for %s from %s", 1306 e->e_df, e->e_to, e->e_from); 1307 } 1308 else 1309 putline("<<< No Message Collected >>>", fp, m); 1310 } 1311 if (e->e_dfp != NULL) 1312 { 1313 rewind(e->e_dfp); 1314 while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 1315 { 1316 if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1317 strncmp(buf, "From ", 5) == 0) 1318 (void) putc('>', fp); 1319 putline(buf, fp, m); 1320 } 1321 1322 if (ferror(e->e_dfp)) 1323 { 1324 syserr("putbody: read error"); 1325 ExitStat = EX_IOERR; 1326 } 1327 } 1328 1329 (void) fflush(fp); 1330 if (ferror(fp) && errno != EPIPE) 1331 { 1332 syserr("putbody: write error"); 1333 ExitStat = EX_IOERR; 1334 } 1335 errno = 0; 1336 } 1337 /* 1338 ** MAILFILE -- Send a message to a file. 1339 ** 1340 ** If the file has the setuid/setgid bits set, but NO execute 1341 ** bits, sendmail will try to become the owner of that file 1342 ** rather than the real user. Obviously, this only works if 1343 ** sendmail runs as root. 1344 ** 1345 ** This could be done as a subordinate mailer, except that it 1346 ** is used implicitly to save messages in ~/dead.letter. We 1347 ** view this as being sufficiently important as to include it 1348 ** here. For example, if the system is dying, we shouldn't have 1349 ** to create another process plus some pipes to save the message. 1350 ** 1351 ** Parameters: 1352 ** filename -- the name of the file to send to. 1353 ** ctladdr -- the controlling address header -- includes 1354 ** the userid/groupid to be when sending. 1355 ** 1356 ** Returns: 1357 ** The exit code associated with the operation. 1358 ** 1359 ** Side Effects: 1360 ** none. 1361 */ 1362 1363 mailfile(filename, ctladdr, e) 1364 char *filename; 1365 ADDRESS *ctladdr; 1366 register ENVELOPE *e; 1367 { 1368 register FILE *f; 1369 register int pid; 1370 int mode; 1371 1372 /* 1373 ** Fork so we can change permissions here. 1374 ** Note that we MUST use fork, not vfork, because of 1375 ** the complications of calling subroutines, etc. 1376 */ 1377 1378 DOFORK(fork); 1379 1380 if (pid < 0) 1381 return (EX_OSERR); 1382 else if (pid == 0) 1383 { 1384 /* child -- actually write to file */ 1385 struct stat stb; 1386 1387 (void) signal(SIGINT, SIG_DFL); 1388 (void) signal(SIGHUP, SIG_DFL); 1389 (void) signal(SIGTERM, SIG_DFL); 1390 (void) umask(OldUmask); 1391 1392 if (stat(filename, &stb) < 0) 1393 stb.st_mode = 0666; 1394 mode = stb.st_mode; 1395 1396 /* limit the errors to those actually caused in the child */ 1397 errno = 0; 1398 ExitStat = EX_OK; 1399 1400 if (bitset(0111, stb.st_mode)) 1401 exit(EX_CANTCREAT); 1402 if (ctladdr == NULL) 1403 ctladdr = &e->e_from; 1404 else 1405 { 1406 /* ignore setuid and setgid bits */ 1407 mode &= ~(S_ISGID|S_ISUID); 1408 } 1409 1410 /* we have to open the dfile BEFORE setuid */ 1411 if (e->e_dfp == NULL && e->e_df != NULL) 1412 { 1413 e->e_dfp = fopen(e->e_df, "r"); 1414 if (e->e_dfp == NULL) 1415 { 1416 syserr("mailfile: Cannot open %s for %s from %s", 1417 e->e_df, e->e_to, e->e_from); 1418 } 1419 } 1420 1421 if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 1422 { 1423 if (ctladdr->q_uid == 0) 1424 { 1425 (void) setgid(DefGid); 1426 (void) initgroups(DefUser, DefGid); 1427 } 1428 else 1429 { 1430 (void) setgid(ctladdr->q_gid); 1431 (void) initgroups(ctladdr->q_ruser ? 1432 ctladdr->q_ruser : ctladdr->q_user, 1433 ctladdr->q_gid); 1434 } 1435 } 1436 if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 1437 { 1438 if (ctladdr->q_uid == 0) 1439 (void) setuid(DefUid); 1440 else 1441 (void) setuid(ctladdr->q_uid); 1442 } 1443 FileName = filename; 1444 LineNumber = 0; 1445 f = dfopen(filename, "a"); 1446 if (f == NULL) 1447 { 1448 message("554 cannot open"); 1449 exit(EX_CANTCREAT); 1450 } 1451 1452 putfromline(f, ProgMailer, e); 1453 (*e->e_puthdr)(f, ProgMailer, e); 1454 putline("\n", f, ProgMailer); 1455 (*e->e_putbody)(f, ProgMailer, e); 1456 putline("\n", f, ProgMailer); 1457 if (ferror(f)) 1458 { 1459 message("451 I/O error"); 1460 setstat(EX_IOERR); 1461 } 1462 (void) xfclose(f, "mailfile", filename); 1463 (void) fflush(stdout); 1464 1465 /* reset ISUID & ISGID bits for paranoid systems */ 1466 (void) chmod(filename, (int) stb.st_mode); 1467 exit(ExitStat); 1468 /*NOTREACHED*/ 1469 } 1470 else 1471 { 1472 /* parent -- wait for exit status */ 1473 int st; 1474 1475 st = waitfor(pid); 1476 if ((st & 0377) != 0) 1477 return (EX_UNAVAILABLE); 1478 else 1479 return ((st >> 8) & 0377); 1480 /*NOTREACHED*/ 1481 } 1482 } 1483 /* 1484 ** SENDALL -- actually send all the messages. 1485 ** 1486 ** Parameters: 1487 ** e -- the envelope to send. 1488 ** mode -- the delivery mode to use. If SM_DEFAULT, use 1489 ** the current e->e_sendmode. 1490 ** 1491 ** Returns: 1492 ** none. 1493 ** 1494 ** Side Effects: 1495 ** Scans the send lists and sends everything it finds. 1496 ** Delivers any appropriate error messages. 1497 ** If we are running in a non-interactive mode, takes the 1498 ** appropriate action. 1499 */ 1500 1501 sendall(e, mode) 1502 ENVELOPE *e; 1503 char mode; 1504 { 1505 register ADDRESS *q; 1506 char *owner; 1507 int otherowners; 1508 register ENVELOPE *ee; 1509 ENVELOPE *splitenv = NULL; 1510 1511 /* determine actual delivery mode */ 1512 if (mode == SM_DEFAULT) 1513 { 1514 extern bool shouldqueue(); 1515 1516 mode = e->e_sendmode; 1517 if (mode != SM_VERIFY && 1518 shouldqueue(e->e_msgpriority, e->e_ctime)) 1519 mode = SM_QUEUE; 1520 } 1521 1522 if (tTd(13, 1)) 1523 { 1524 printf("\nSENDALL: mode %c, e_from ", mode); 1525 printaddr(&e->e_from, FALSE); 1526 printf("sendqueue:\n"); 1527 printaddr(e->e_sendqueue, TRUE); 1528 } 1529 1530 /* 1531 ** Do any preprocessing necessary for the mode we are running. 1532 ** Check to make sure the hop count is reasonable. 1533 ** Delete sends to the sender in mailing lists. 1534 */ 1535 1536 CurEnv = e; 1537 1538 if (e->e_hopcount > MaxHopCount) 1539 { 1540 errno = 0; 1541 syserr("554 too many hops %d (%d max): from %s, to %s", 1542 e->e_hopcount, MaxHopCount, e->e_from.q_paddr, 1543 e->e_sendqueue->q_paddr); 1544 return; 1545 } 1546 1547 if (!MeToo) 1548 { 1549 extern ADDRESS *recipient(); 1550 1551 if (tTd(13, 5)) 1552 { 1553 printf("sendall: QDONTSEND "); 1554 printaddr(&e->e_from, FALSE); 1555 } 1556 e->e_from.q_flags |= QDONTSEND; 1557 (void) recipient(&e->e_from, &e->e_sendqueue, e); 1558 } 1559 1560 /* 1561 ** Handle alias owners. 1562 ** 1563 ** We scan up the q_alias chain looking for owners. 1564 ** We discard owners that are the same as the return path. 1565 */ 1566 1567 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1568 { 1569 register struct address *a; 1570 1571 for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias) 1572 continue; 1573 if (a != NULL) 1574 q->q_owner = a->q_owner; 1575 1576 if (q->q_owner != NULL && 1577 !bitset(QDONTSEND, q->q_flags) && 1578 strcmp(q->q_owner, e->e_from.q_paddr) == 0) 1579 q->q_owner = NULL; 1580 } 1581 1582 owner = ""; 1583 otherowners = 1; 1584 while (owner != NULL && otherowners > 0) 1585 { 1586 owner = NULL; 1587 otherowners = 0; 1588 1589 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1590 { 1591 if (bitset(QDONTSEND, q->q_flags)) 1592 continue; 1593 1594 if (q->q_owner != NULL) 1595 { 1596 if (owner == NULL) 1597 owner = q->q_owner; 1598 else if (owner != q->q_owner) 1599 { 1600 if (strcmp(owner, q->q_owner) == 0) 1601 { 1602 /* make future comparisons cheap */ 1603 q->q_owner = owner; 1604 } 1605 else 1606 { 1607 otherowners++; 1608 } 1609 owner = q->q_owner; 1610 } 1611 } 1612 else 1613 { 1614 otherowners++; 1615 } 1616 } 1617 1618 if (owner != NULL && otherowners > 0) 1619 { 1620 extern HDR *copyheader(); 1621 extern ADDRESS *copyqueue(); 1622 1623 /* 1624 ** Split this envelope into two. 1625 */ 1626 1627 ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE)); 1628 *ee = *e; 1629 ee->e_id = NULL; 1630 (void) queuename(ee, '\0'); 1631 1632 if (tTd(13, 1)) 1633 printf("sendall: split %s into %s\n", 1634 e->e_id, ee->e_id); 1635 1636 ee->e_header = copyheader(e->e_header); 1637 ee->e_sendqueue = copyqueue(e->e_sendqueue); 1638 ee->e_errorqueue = copyqueue(e->e_errorqueue); 1639 ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE); 1640 setsender(owner, ee, NULL, TRUE); 1641 if (tTd(13, 5)) 1642 { 1643 printf("sendall(split): QDONTSEND "); 1644 printaddr(&ee->e_from, FALSE); 1645 } 1646 ee->e_from.q_flags |= QDONTSEND; 1647 ee->e_dfp = NULL; 1648 ee->e_xfp = NULL; 1649 ee->e_lockfp = NULL; 1650 ee->e_df = NULL; 1651 ee->e_errormode = EM_MAIL; 1652 ee->e_sibling = splitenv; 1653 splitenv = ee; 1654 1655 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1656 if (q->q_owner == owner) 1657 q->q_flags |= QDONTSEND; 1658 for (q = ee->e_sendqueue; q != NULL; q = q->q_next) 1659 if (q->q_owner != owner) 1660 q->q_flags |= QDONTSEND; 1661 1662 if (e->e_df != NULL && mode != SM_VERIFY) 1663 { 1664 ee->e_dfp = NULL; 1665 ee->e_df = newstr(queuename(ee, 'd')); 1666 if (link(e->e_df, ee->e_df) < 0) 1667 { 1668 syserr("sendall: link(%s, %s)", 1669 e->e_df, ee->e_df); 1670 } 1671 } 1672 1673 if (mode != SM_VERIFY) 1674 { 1675 char xfbuf1[20], xfbuf2[20]; 1676 1677 (void) strcpy(xfbuf1, queuename(e, 'x')); 1678 (void) strcpy(xfbuf2, queuename(ee, 'x')); 1679 if (link(xfbuf1, xfbuf2) < 0) 1680 { 1681 syserr("sendall: link(%s, %s)", 1682 xfbuf1, xfbuf2); 1683 } 1684 } 1685 #ifdef LOG 1686 if (LogLevel > 4) 1687 syslog(LOG_INFO, "%s: clone %s", 1688 ee->e_id, e->e_id); 1689 #endif 1690 } 1691 } 1692 1693 if (owner != NULL) 1694 { 1695 setsender(owner, e, NULL, TRUE); 1696 if (tTd(13, 5)) 1697 { 1698 printf("sendall(owner): QDONTSEND "); 1699 printaddr(&e->e_from, FALSE); 1700 } 1701 e->e_from.q_flags |= QDONTSEND; 1702 e->e_errormode = EM_MAIL; 1703 } 1704 1705 # ifdef QUEUE 1706 if ((mode == SM_QUEUE || mode == SM_FORK || 1707 (mode != SM_VERIFY && SuperSafe)) && 1708 !bitset(EF_INQUEUE, e->e_flags)) 1709 { 1710 /* be sure everything is instantiated in the queue */ 1711 queueup(e, TRUE, mode == SM_QUEUE); 1712 for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 1713 queueup(ee, TRUE, mode == SM_QUEUE); 1714 } 1715 #endif /* QUEUE */ 1716 1717 if (splitenv != NULL) 1718 { 1719 if (tTd(13, 1)) 1720 { 1721 printf("\nsendall: Split queue; remaining queue:\n"); 1722 printaddr(e->e_sendqueue, TRUE); 1723 } 1724 1725 for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 1726 { 1727 CurEnv = ee; 1728 sendenvelope(ee, mode); 1729 } 1730 1731 CurEnv = e; 1732 } 1733 sendenvelope(e, mode); 1734 1735 for (; splitenv != NULL; splitenv = splitenv->e_sibling) 1736 dropenvelope(splitenv); 1737 } 1738 1739 sendenvelope(e, mode) 1740 register ENVELOPE *e; 1741 char mode; 1742 { 1743 bool oldverbose; 1744 int pid; 1745 register ADDRESS *q; 1746 #ifdef LOCKF 1747 struct flock lfd; 1748 #endif 1749 1750 oldverbose = Verbose; 1751 switch (mode) 1752 { 1753 case SM_VERIFY: 1754 Verbose = TRUE; 1755 break; 1756 1757 case SM_QUEUE: 1758 queueonly: 1759 e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 1760 return; 1761 1762 case SM_FORK: 1763 if (e->e_xfp != NULL) 1764 (void) fflush(e->e_xfp); 1765 1766 # ifdef LOCKF 1767 /* 1768 ** Since lockf has the interesting semantic that the 1769 ** lock is lost when we fork, we have to risk losing 1770 ** the lock here by closing before the fork, and then 1771 ** trying to get it back in the child. 1772 */ 1773 1774 if (e->e_lockfp != NULL) 1775 { 1776 (void) xfclose(e->e_lockfp, "sendenvelope", "lockfp"); 1777 e->e_lockfp = NULL; 1778 } 1779 # endif /* LOCKF */ 1780 1781 pid = fork(); 1782 if (pid < 0) 1783 { 1784 goto queueonly; 1785 } 1786 else if (pid > 0) 1787 { 1788 /* be sure we leave the temp files to our child */ 1789 e->e_id = e->e_df = NULL; 1790 # ifndef LOCKF 1791 if (e->e_lockfp != NULL) 1792 { 1793 (void) xfclose(e->e_lockfp, "sendenvelope", "lockfp"); 1794 e->e_lockfp = NULL; 1795 } 1796 # endif 1797 1798 /* close any random open files in the envelope */ 1799 if (e->e_dfp != NULL) 1800 { 1801 (void) xfclose(e->e_dfp, "sendenvelope", "dfp"); 1802 e->e_dfp = NULL; 1803 } 1804 if (e->e_xfp != NULL) 1805 { 1806 (void) xfclose(e->e_xfp, "sendenvelope", "xfp"); 1807 e->e_xfp = NULL; 1808 } 1809 return; 1810 } 1811 1812 /* double fork to avoid zombies */ 1813 if (fork() > 0) 1814 exit(EX_OK); 1815 1816 /* be sure we are immune from the terminal */ 1817 disconnect(FALSE, e); 1818 1819 # ifdef LOCKF 1820 /* 1821 ** Now try to get our lock back. 1822 */ 1823 1824 lfd.l_type = F_WRLCK; 1825 lfd.l_whence = lfd.l_start = lfd.l_len = 0; 1826 e->e_lockfp = fopen(queuename(e, 'q'), "r+"); 1827 if (e->e_lockfp == NULL || 1828 fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0) 1829 { 1830 /* oops.... lost it */ 1831 if (tTd(13, 1)) 1832 printf("sendenvelope: %s lost lock: lockfp=%x, %s\n", 1833 e->e_id, e->e_lockfp, errstring(errno)); 1834 1835 # ifdef LOG 1836 if (LogLevel > 29) 1837 syslog(LOG_NOTICE, "%s: lost lock: %m", 1838 e->e_id); 1839 # endif /* LOG */ 1840 exit(EX_OK); 1841 } 1842 # endif /* LOCKF */ 1843 1844 /* 1845 ** Close any cached connections. 1846 ** 1847 ** We don't send the QUIT protocol because the parent 1848 ** still knows about the connection. 1849 ** 1850 ** This should only happen when delivering an error 1851 ** message. 1852 */ 1853 1854 mci_flush(FALSE, NULL); 1855 1856 break; 1857 } 1858 1859 /* 1860 ** Run through the list and send everything. 1861 */ 1862 1863 e->e_nsent = 0; 1864 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1865 { 1866 if (mode == SM_VERIFY) 1867 { 1868 e->e_to = q->q_paddr; 1869 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1870 message("deliverable"); 1871 } 1872 else if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1873 { 1874 # ifdef QUEUE 1875 /* 1876 ** Checkpoint the send list every few addresses 1877 */ 1878 1879 if (e->e_nsent >= CheckpointInterval) 1880 { 1881 queueup(e, TRUE, FALSE); 1882 e->e_nsent = 0; 1883 } 1884 # endif /* QUEUE */ 1885 (void) deliver(e, q); 1886 } 1887 } 1888 Verbose = oldverbose; 1889 1890 /* 1891 ** Now run through and check for errors. 1892 */ 1893 1894 if (mode == SM_VERIFY) 1895 { 1896 return; 1897 } 1898 1899 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1900 { 1901 if (tTd(13, 3)) 1902 { 1903 printf("Checking "); 1904 printaddr(q, FALSE); 1905 } 1906 1907 /* only send errors if the message failed */ 1908 if (!bitset(QBADADDR, q->q_flags)) 1909 continue; 1910 1911 e->e_flags |= EF_FATALERRS; 1912 1913 if (q->q_owner == NULL && strcmp(e->e_from.q_paddr, "<>") != 0) 1914 (void) sendtolist(e->e_from.q_paddr, NULL, 1915 &e->e_errorqueue, e); 1916 } 1917 1918 if (mode == SM_FORK) 1919 finis(); 1920 } 1921 /* 1922 ** HOSTSIGNATURE -- return the "signature" for a host. 1923 ** 1924 ** The signature describes how we are going to send this -- it 1925 ** can be just the hostname (for non-Internet hosts) or can be 1926 ** an ordered list of MX hosts. 1927 ** 1928 ** Parameters: 1929 ** m -- the mailer describing this host. 1930 ** host -- the host name. 1931 ** e -- the current envelope. 1932 ** 1933 ** Returns: 1934 ** The signature for this host. 1935 ** 1936 ** Side Effects: 1937 ** Can tweak the symbol table. 1938 */ 1939 1940 char * 1941 hostsignature(m, host, e) 1942 register MAILER *m; 1943 char *host; 1944 ENVELOPE *e; 1945 { 1946 register char *p; 1947 register STAB *s; 1948 int i; 1949 int len; 1950 #ifdef NAMED_BIND 1951 int nmx; 1952 auto int rcode; 1953 char *mxhosts[MAXMXHOSTS + 1]; 1954 static char myhostbuf[MAXNAME]; 1955 #endif 1956 1957 /* 1958 ** Check to see if this uses IPC -- if not, it can't have MX records. 1959 */ 1960 1961 p = m->m_mailer; 1962 if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 1963 { 1964 /* just an ordinary mailer */ 1965 return host; 1966 } 1967 1968 /* 1969 ** If it is a numeric address, just return it. 1970 */ 1971 1972 if (host[0] == '[') 1973 return host; 1974 1975 /* 1976 ** Look it up in the symbol table. 1977 */ 1978 1979 s = stab(host, ST_HOSTSIG, ST_ENTER); 1980 if (s->s_hostsig != NULL) 1981 return s->s_hostsig; 1982 1983 /* 1984 ** Not already there -- create a signature. 1985 */ 1986 1987 #ifdef NAMED_BIND 1988 if (myhostbuf[0] == '\0') 1989 expand("\201j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e); 1990 1991 nmx = getmxrr(host, mxhosts, myhostbuf, &rcode); 1992 1993 if (nmx <= 0) 1994 { 1995 register MCI *mci; 1996 extern int errno; 1997 extern MCI *mci_get(); 1998 1999 /* update the connection info for this host */ 2000 mci = mci_get(host, m); 2001 mci->mci_exitstat = rcode; 2002 mci->mci_errno = errno; 2003 2004 /* and return the original host name as the signature */ 2005 s->s_hostsig = host; 2006 return host; 2007 } 2008 2009 len = 0; 2010 for (i = 0; i < nmx; i++) 2011 { 2012 len += strlen(mxhosts[i]) + 1; 2013 } 2014 s->s_hostsig = p = xalloc(len); 2015 for (i = 0; i < nmx; i++) 2016 { 2017 if (i != 0) 2018 *p++ = ':'; 2019 strcpy(p, mxhosts[i]); 2020 p += strlen(p); 2021 } 2022 makelower(s->s_hostsig); 2023 #else 2024 /* not using BIND -- the signature is just the host name */ 2025 s->s_hostsig = host; 2026 #endif 2027 if (tTd(17, 1)) 2028 printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 2029 return s->s_hostsig; 2030 } 2031