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