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