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