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