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