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