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