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.75 (Berkeley) 05/14/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 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 extern char *errstring(); 1572 1573 /* 1574 ** Compute status message from code. 1575 */ 1576 1577 i = stat - EX__BASE; 1578 if (stat == 0) 1579 { 1580 statmsg = "250 Sent"; 1581 if (e->e_statmsg != NULL) 1582 { 1583 (void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg); 1584 statmsg = buf; 1585 } 1586 } 1587 else if (i < 0 || i > N_SysEx) 1588 { 1589 (void) sprintf(buf, "554 unknown mailer error %d", stat); 1590 stat = EX_UNAVAILABLE; 1591 statmsg = buf; 1592 } 1593 else if (stat == EX_TEMPFAIL) 1594 { 1595 (void) strcpy(buf, SysExMsg[i] + 1); 1596 #ifdef NAMED_BIND 1597 if (h_errno == TRY_AGAIN) 1598 statmsg = errstring(h_errno+MAX_ERRNO); 1599 else 1600 #endif 1601 { 1602 if (errno != 0) 1603 statmsg = errstring(errno); 1604 else 1605 { 1606 #ifdef SMTP 1607 extern char SmtpError[]; 1608 1609 statmsg = SmtpError; 1610 #else /* SMTP */ 1611 statmsg = NULL; 1612 #endif /* SMTP */ 1613 } 1614 } 1615 if (statmsg != NULL && statmsg[0] != '\0') 1616 { 1617 (void) strcat(buf, ": "); 1618 (void) strcat(buf, statmsg); 1619 } 1620 statmsg = buf; 1621 } 1622 else 1623 { 1624 statmsg = SysExMsg[i]; 1625 if (*statmsg++ == ':') 1626 { 1627 (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 1628 statmsg = buf; 1629 } 1630 } 1631 1632 /* 1633 ** Print the message as appropriate 1634 */ 1635 1636 if (stat == EX_OK || stat == EX_TEMPFAIL) 1637 message(&statmsg[4], errstring(errno)); 1638 else 1639 { 1640 Errors++; 1641 usrerr(statmsg, errstring(errno)); 1642 } 1643 1644 /* 1645 ** Final cleanup. 1646 ** Log a record of the transaction. Compute the new 1647 ** ExitStat -- if we already had an error, stick with 1648 ** that. 1649 */ 1650 1651 if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 1652 logdelivery(m, mci, &statmsg[4], e); 1653 1654 if (stat != EX_TEMPFAIL) 1655 setstat(stat); 1656 if (stat != EX_OK) 1657 { 1658 if (e->e_message != NULL) 1659 free(e->e_message); 1660 e->e_message = newstr(&statmsg[4]); 1661 } 1662 errno = 0; 1663 #ifdef NAMED_BIND 1664 h_errno = 0; 1665 #endif 1666 } 1667 /* 1668 ** LOGDELIVERY -- log the delivery in the system log 1669 ** 1670 ** Parameters: 1671 ** m -- the mailer info. Can be NULL for initial queue. 1672 ** mci -- the mailer connection info -- can be NULL if the 1673 ** log is occuring when no connection is active. 1674 ** stat -- the message to print for the status. 1675 ** e -- the current envelope. 1676 ** 1677 ** Returns: 1678 ** none 1679 ** 1680 ** Side Effects: 1681 ** none 1682 */ 1683 1684 logdelivery(m, mci, stat, e) 1685 MAILER *m; 1686 register MCI *mci; 1687 char *stat; 1688 register ENVELOPE *e; 1689 { 1690 # ifdef LOG 1691 char *curhost; 1692 char buf[512]; 1693 extern char *pintvl(); 1694 extern char *macvalue(); 1695 1696 (void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 1697 1698 if (m != NULL) 1699 { 1700 (void) strcat(buf, ", mailer="); 1701 (void) strcat(buf, m->m_name); 1702 } 1703 1704 if (mci != NULL && mci->mci_host != NULL) 1705 { 1706 # ifdef DAEMON 1707 extern SOCKADDR CurHostAddr; 1708 extern char *anynet_ntoa(); 1709 # endif 1710 1711 (void) strcat(buf, ", relay="); 1712 (void) strcat(buf, mci->mci_host); 1713 1714 # ifdef DAEMON 1715 (void) strcat(buf, " ("); 1716 (void) strcat(buf, anynet_ntoa(&CurHostAddr)); 1717 (void) strcat(buf, ")"); 1718 # endif 1719 } 1720 else 1721 { 1722 char *p = macvalue('h', e); 1723 1724 if (p != NULL && p[0] != '\0') 1725 { 1726 (void) strcat(buf, ", relay="); 1727 (void) strcat(buf, p); 1728 } 1729 } 1730 1731 syslog(LOG_INFO, "%s: to=%s, %s, stat=%s", 1732 e->e_id, e->e_to, buf, stat); 1733 # endif /* LOG */ 1734 } 1735 /* 1736 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 1737 ** 1738 ** This can be made an arbitrary message separator by changing $l 1739 ** 1740 ** One of the ugliest hacks seen by human eyes is contained herein: 1741 ** UUCP wants those stupid "remote from <host>" lines. Why oh why 1742 ** does a well-meaning programmer such as myself have to deal with 1743 ** this kind of antique garbage???? 1744 ** 1745 ** Parameters: 1746 ** fp -- the file to output to. 1747 ** m -- the mailer describing this entry. 1748 ** 1749 ** Returns: 1750 ** none 1751 ** 1752 ** Side Effects: 1753 ** outputs some text to fp. 1754 */ 1755 1756 putfromline(fp, m, e) 1757 register FILE *fp; 1758 register MAILER *m; 1759 ENVELOPE *e; 1760 { 1761 char *template = "\201l\n"; 1762 char buf[MAXLINE]; 1763 1764 if (bitnset(M_NHDR, m->m_flags)) 1765 return; 1766 1767 # ifdef UGLYUUCP 1768 if (bitnset(M_UGLYUUCP, m->m_flags)) 1769 { 1770 char *bang; 1771 char xbuf[MAXLINE]; 1772 1773 expand("\201g", buf, &buf[sizeof buf - 1], e); 1774 bang = strchr(buf, '!'); 1775 if (bang == NULL) 1776 syserr("554 No ! in UUCP! (%s)", buf); 1777 else 1778 { 1779 *bang++ = '\0'; 1780 (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 1781 template = xbuf; 1782 } 1783 } 1784 # endif /* UGLYUUCP */ 1785 expand(template, buf, &buf[sizeof buf - 1], e); 1786 putline(buf, fp, m); 1787 } 1788 /* 1789 ** PUTBODY -- put the body of a message. 1790 ** 1791 ** Parameters: 1792 ** fp -- file to output onto. 1793 ** m -- a mailer descriptor to control output format. 1794 ** e -- the envelope to put out. 1795 ** separator -- if non-NULL, a message separator that must 1796 ** not be permitted in the resulting message. 1797 ** 1798 ** Returns: 1799 ** none. 1800 ** 1801 ** Side Effects: 1802 ** The message is written onto fp. 1803 */ 1804 1805 putbody(fp, m, e, separator) 1806 FILE *fp; 1807 MAILER *m; 1808 register ENVELOPE *e; 1809 char *separator; 1810 { 1811 char buf[MAXLINE]; 1812 1813 /* 1814 ** Output the body of the message 1815 */ 1816 1817 if (e->e_dfp == NULL) 1818 { 1819 if (e->e_df != NULL) 1820 { 1821 e->e_dfp = fopen(e->e_df, "r"); 1822 if (e->e_dfp == NULL) 1823 syserr("putbody: Cannot open %s for %s from %s", 1824 e->e_df, e->e_to, e->e_from); 1825 } 1826 else 1827 putline("<<< No Message Collected >>>", fp, m); 1828 } 1829 if (e->e_dfp != NULL) 1830 { 1831 rewind(e->e_dfp); 1832 while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 1833 { 1834 if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1835 strncmp(buf, "From ", 5) == 0) 1836 (void) putc('>', fp); 1837 if (buf[0] == '-' && buf[1] == '-' && separator != NULL) 1838 { 1839 /* possible separator */ 1840 int sl = strlen(separator); 1841 1842 if (strncmp(&buf[2], separator, sl) == 0) 1843 (void) putc(' ', fp); 1844 } 1845 putline(buf, fp, m); 1846 } 1847 1848 if (ferror(e->e_dfp)) 1849 { 1850 syserr("putbody: read error"); 1851 ExitStat = EX_IOERR; 1852 } 1853 } 1854 1855 /* some mailers want extra blank line at end of message */ 1856 if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n') 1857 putline("", fp, m); 1858 1859 (void) fflush(fp); 1860 if (ferror(fp) && errno != EPIPE) 1861 { 1862 syserr("putbody: write error"); 1863 ExitStat = EX_IOERR; 1864 } 1865 errno = 0; 1866 } 1867 /* 1868 ** MAILFILE -- Send a message to a file. 1869 ** 1870 ** If the file has the setuid/setgid bits set, but NO execute 1871 ** bits, sendmail will try to become the owner of that file 1872 ** rather than the real user. Obviously, this only works if 1873 ** sendmail runs as root. 1874 ** 1875 ** This could be done as a subordinate mailer, except that it 1876 ** is used implicitly to save messages in ~/dead.letter. We 1877 ** view this as being sufficiently important as to include it 1878 ** here. For example, if the system is dying, we shouldn't have 1879 ** to create another process plus some pipes to save the message. 1880 ** 1881 ** Parameters: 1882 ** filename -- the name of the file to send to. 1883 ** ctladdr -- the controlling address header -- includes 1884 ** the userid/groupid to be when sending. 1885 ** 1886 ** Returns: 1887 ** The exit code associated with the operation. 1888 ** 1889 ** Side Effects: 1890 ** none. 1891 */ 1892 1893 mailfile(filename, ctladdr, e) 1894 char *filename; 1895 ADDRESS *ctladdr; 1896 register ENVELOPE *e; 1897 { 1898 register FILE *f; 1899 register int pid; 1900 int mode; 1901 1902 if (tTd(11, 1)) 1903 { 1904 printf("mailfile %s\n ctladdr=", filename); 1905 printaddr(ctladdr, FALSE); 1906 } 1907 1908 /* 1909 ** Fork so we can change permissions here. 1910 ** Note that we MUST use fork, not vfork, because of 1911 ** the complications of calling subroutines, etc. 1912 */ 1913 1914 DOFORK(fork); 1915 1916 if (pid < 0) 1917 return (EX_OSERR); 1918 else if (pid == 0) 1919 { 1920 /* child -- actually write to file */ 1921 struct stat stb; 1922 1923 (void) signal(SIGINT, SIG_DFL); 1924 (void) signal(SIGHUP, SIG_DFL); 1925 (void) signal(SIGTERM, SIG_DFL); 1926 (void) umask(OldUmask); 1927 1928 if (stat(filename, &stb) < 0) 1929 stb.st_mode = FileMode; 1930 mode = stb.st_mode; 1931 1932 /* limit the errors to those actually caused in the child */ 1933 errno = 0; 1934 ExitStat = EX_OK; 1935 1936 if (bitset(0111, stb.st_mode)) 1937 exit(EX_CANTCREAT); 1938 if (ctladdr == NULL) 1939 ctladdr = &e->e_from; 1940 else 1941 { 1942 /* ignore setuid and setgid bits */ 1943 mode &= ~(S_ISGID|S_ISUID); 1944 } 1945 1946 /* we have to open the dfile BEFORE setuid */ 1947 if (e->e_dfp == NULL && e->e_df != NULL) 1948 { 1949 e->e_dfp = fopen(e->e_df, "r"); 1950 if (e->e_dfp == NULL) 1951 { 1952 syserr("mailfile: Cannot open %s for %s from %s", 1953 e->e_df, e->e_to, e->e_from); 1954 } 1955 } 1956 1957 if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 1958 { 1959 if (ctladdr->q_uid == 0) 1960 { 1961 (void) setgid(DefGid); 1962 (void) initgroups(DefUser, DefGid); 1963 } 1964 else 1965 { 1966 (void) setgid(ctladdr->q_gid); 1967 (void) initgroups(ctladdr->q_ruser ? 1968 ctladdr->q_ruser : ctladdr->q_user, 1969 ctladdr->q_gid); 1970 } 1971 } 1972 if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 1973 { 1974 if (ctladdr->q_uid == 0) 1975 (void) setuid(DefUid); 1976 else 1977 (void) setuid(ctladdr->q_uid); 1978 } 1979 FileName = filename; 1980 LineNumber = 0; 1981 f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode); 1982 if (f == NULL) 1983 { 1984 message("554 cannot open"); 1985 exit(EX_CANTCREAT); 1986 } 1987 1988 putfromline(f, FileMailer, e); 1989 (*e->e_puthdr)(f, FileMailer, e); 1990 putline("\n", f, FileMailer); 1991 (*e->e_putbody)(f, FileMailer, e, NULL); 1992 putline("\n", f, FileMailer); 1993 if (ferror(f)) 1994 { 1995 message("451 I/O error"); 1996 setstat(EX_IOERR); 1997 } 1998 (void) xfclose(f, "mailfile", filename); 1999 (void) fflush(stdout); 2000 2001 /* reset ISUID & ISGID bits for paranoid systems */ 2002 (void) chmod(filename, (int) stb.st_mode); 2003 exit(ExitStat); 2004 /*NOTREACHED*/ 2005 } 2006 else 2007 { 2008 /* parent -- wait for exit status */ 2009 int st; 2010 2011 st = waitfor(pid); 2012 if ((st & 0377) != 0) 2013 return (EX_UNAVAILABLE); 2014 else 2015 return ((st >> 8) & 0377); 2016 /*NOTREACHED*/ 2017 } 2018 } 2019 /* 2020 ** HOSTSIGNATURE -- return the "signature" for a host. 2021 ** 2022 ** The signature describes how we are going to send this -- it 2023 ** can be just the hostname (for non-Internet hosts) or can be 2024 ** an ordered list of MX hosts. 2025 ** 2026 ** Parameters: 2027 ** m -- the mailer describing this host. 2028 ** host -- the host name. 2029 ** e -- the current envelope. 2030 ** 2031 ** Returns: 2032 ** The signature for this host. 2033 ** 2034 ** Side Effects: 2035 ** Can tweak the symbol table. 2036 */ 2037 2038 char * 2039 hostsignature(m, host, e) 2040 register MAILER *m; 2041 char *host; 2042 ENVELOPE *e; 2043 { 2044 register char *p; 2045 register STAB *s; 2046 int i; 2047 int len; 2048 #ifdef NAMED_BIND 2049 int nmx; 2050 auto int rcode; 2051 char *hp; 2052 char *endp; 2053 int oldoptions; 2054 char *mxhosts[MAXMXHOSTS + 1]; 2055 #endif 2056 2057 /* 2058 ** Check to see if this uses IPC -- if not, it can't have MX records. 2059 */ 2060 2061 p = m->m_mailer; 2062 if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 2063 { 2064 /* just an ordinary mailer */ 2065 return host; 2066 } 2067 2068 /* 2069 ** If it is a numeric address, just return it. 2070 */ 2071 2072 if (host[0] == '[') 2073 return host; 2074 2075 /* 2076 ** Look it up in the symbol table. 2077 */ 2078 2079 s = stab(host, ST_HOSTSIG, ST_ENTER); 2080 if (s->s_hostsig != NULL) 2081 return s->s_hostsig; 2082 2083 /* 2084 ** Not already there -- create a signature. 2085 */ 2086 2087 #ifdef NAMED_BIND 2088 if (ConfigLevel < 2) 2089 { 2090 oldoptions = _res.options; 2091 _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 2092 } 2093 2094 for (hp = host; hp != NULL; hp = endp) 2095 { 2096 endp = strchr(hp, ':'); 2097 if (endp != NULL) 2098 *endp = '\0'; 2099 2100 nmx = getmxrr(hp, mxhosts, TRUE, &rcode); 2101 2102 if (nmx <= 0) 2103 { 2104 register MCI *mci; 2105 extern int errno; 2106 extern MCI *mci_get(); 2107 2108 /* update the connection info for this host */ 2109 mci = mci_get(hp, m); 2110 mci->mci_exitstat = rcode; 2111 mci->mci_errno = errno; 2112 2113 /* and return the original host name as the signature */ 2114 nmx = 1; 2115 mxhosts[0] = hp; 2116 } 2117 2118 len = 0; 2119 for (i = 0; i < nmx; i++) 2120 { 2121 len += strlen(mxhosts[i]) + 1; 2122 } 2123 if (s->s_hostsig != NULL) 2124 len += strlen(s->s_hostsig) + 1; 2125 p = xalloc(len); 2126 if (s->s_hostsig != NULL) 2127 { 2128 (void) strcpy(p, s->s_hostsig); 2129 free(s->s_hostsig); 2130 s->s_hostsig = p; 2131 p += strlen(p); 2132 *p++ = ':'; 2133 } 2134 else 2135 s->s_hostsig = p; 2136 for (i = 0; i < nmx; i++) 2137 { 2138 if (i != 0) 2139 *p++ = ':'; 2140 strcpy(p, mxhosts[i]); 2141 p += strlen(p); 2142 } 2143 if (endp != NULL) 2144 *endp++ = ':'; 2145 } 2146 makelower(s->s_hostsig); 2147 if (ConfigLevel < 2) 2148 _res.options = oldoptions; 2149 #else 2150 /* not using BIND -- the signature is just the host name */ 2151 s->s_hostsig = host; 2152 #endif 2153 if (tTd(17, 1)) 2154 printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 2155 return s->s_hostsig; 2156 } 2157