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.26 (Berkeley) 09/26/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 define('z', to->q_home, e); /* user's home */ 826 827 /* 828 ** Expand out this user into argument list. 829 */ 830 831 if (!clever) 832 { 833 expand(*mvp, buf, &buf[sizeof buf - 1], e); 834 *pvp++ = newstr(buf); 835 if (pvp >= &pv[MAXPV - 2]) 836 { 837 /* allow some space for trailing parms */ 838 break; 839 } 840 } 841 } 842 843 /* see if any addresses still exist */ 844 if (tobuf[0] == '\0') 845 { 846 define('g', (char *) NULL, e); 847 return (0); 848 } 849 850 /* print out messages as full list */ 851 e->e_to = tobuf + 1; 852 853 /* 854 ** Fill out any parameters after the $u parameter. 855 */ 856 857 while (!clever && *++mvp != NULL) 858 { 859 expand(*mvp, buf, &buf[sizeof buf - 1], e); 860 *pvp++ = newstr(buf); 861 if (pvp >= &pv[MAXPV]) 862 syserr("554 deliver: pv overflow after $u for %s", pv[0]); 863 } 864 *pvp++ = NULL; 865 866 /* 867 ** Call the mailer. 868 ** The argument vector gets built, pipes 869 ** are created as necessary, and we fork & exec as 870 ** appropriate. 871 ** If we are running SMTP, we just need to clean up. 872 */ 873 874 if (ctladdr == NULL && m != ProgMailer) 875 ctladdr = &e->e_from; 876 #ifdef NAMED_BIND 877 if (ConfigLevel < 2) 878 _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 879 #endif 880 881 if (tTd(11, 1)) 882 { 883 printf("openmailer:"); 884 printav(pv); 885 } 886 errno = 0; 887 888 CurHostName = m->m_mailer; 889 890 /* 891 ** Deal with the special case of mail handled through an IPC 892 ** connection. 893 ** In this case we don't actually fork. We must be 894 ** running SMTP for this to work. We will return a 895 ** zero pid to indicate that we are running IPC. 896 ** We also handle a debug version that just talks to stdin/out. 897 */ 898 899 curhost = NULL; 900 SmtpPhase = NULL; 901 902 #ifdef XDEBUG 903 { 904 char wbuf[MAXLINE]; 905 906 /* make absolutely certain 0, 1, and 2 are in use */ 907 sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name); 908 checkfd012(wbuf); 909 } 910 #endif 911 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 930 CurHostName = pv[1]; 931 curhost = hostsignature(m, pv[1], e); 932 933 if (curhost == NULL || curhost[0] == '\0') 934 { 935 syserr("null signature"); 936 rcode = EX_OSERR; 937 goto give_up; 938 } 939 940 if (!clever) 941 { 942 syserr("554 non-clever IPC"); 943 rcode = EX_OSERR; 944 goto give_up; 945 } 946 if (pv[2] != NULL) 947 port = atoi(pv[2]); 948 else 949 port = 0; 950 tryhost: 951 mci = NULL; 952 while (*curhost != '\0') 953 { 954 register char *p; 955 static char hostbuf[MAXNAME]; 956 957 mci = NULL; 958 959 /* pull the next host from the signature */ 960 p = strchr(curhost, ':'); 961 if (p == NULL) 962 p = &curhost[strlen(curhost)]; 963 strncpy(hostbuf, curhost, p - curhost); 964 hostbuf[p - curhost] = '\0'; 965 if (*p != '\0') 966 p++; 967 curhost = p; 968 969 /* see if we already know that this host is fried */ 970 CurHostName = hostbuf; 971 mci = mci_get(hostbuf, m); 972 if (mci->mci_state != MCIS_CLOSED) 973 { 974 if (tTd(11, 1)) 975 { 976 printf("openmailer: "); 977 mci_dump(mci); 978 } 979 CurHostName = mci->mci_host; 980 break; 981 } 982 mci->mci_mailer = m; 983 if (mci->mci_exitstat != EX_OK) 984 continue; 985 986 /* try the connection */ 987 setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 988 message("Connecting to %s (%s)...", 989 hostbuf, m->m_name); 990 i = makeconnection(hostbuf, port, mci, 991 bitnset(M_SECURE_PORT, m->m_flags)); 992 mci->mci_exitstat = i; 993 mci->mci_errno = errno; 994 #ifdef NAMED_BIND 995 mci->mci_herrno = h_errno; 996 #endif 997 if (i == EX_OK) 998 { 999 mci->mci_state = MCIS_OPENING; 1000 mci_cache(mci); 1001 if (TrafficLogFile != NULL) 1002 fprintf(TrafficLogFile, "%05d == CONNECT %s\n", 1003 getpid(), hostbuf); 1004 break; 1005 } 1006 else if (tTd(11, 1)) 1007 printf("openmailer: makeconnection => stat=%d, errno=%d\n", 1008 i, errno); 1009 1010 1011 /* enter status of this host */ 1012 setstat(i); 1013 } 1014 mci->mci_pid = 0; 1015 #else /* no DAEMON */ 1016 syserr("554 openmailer: no IPC"); 1017 if (tTd(11, 1)) 1018 printf("openmailer: NULL\n"); 1019 return NULL; 1020 #endif /* DAEMON */ 1021 } 1022 else 1023 { 1024 if (TrafficLogFile != NULL) 1025 { 1026 char **av; 1027 1028 fprintf(TrafficLogFile, "%05d === EXEC", getpid()); 1029 for (av = pv; *av != NULL; av++) 1030 fprintf(TrafficLogFile, " %s", *av); 1031 fprintf(TrafficLogFile, "\n"); 1032 } 1033 1034 /* create a pipe to shove the mail through */ 1035 if (pipe(mpvect) < 0) 1036 { 1037 syserr("%s... openmailer(%s): pipe (to mailer)", 1038 e->e_to, m->m_name); 1039 if (tTd(11, 1)) 1040 printf("openmailer: NULL\n"); 1041 rcode = EX_OSERR; 1042 goto give_up; 1043 } 1044 1045 /* if this mailer speaks smtp, create a return pipe */ 1046 if (clever && pipe(rpvect) < 0) 1047 { 1048 syserr("%s... openmailer(%s): pipe (from mailer)", 1049 e->e_to, m->m_name); 1050 (void) close(mpvect[0]); 1051 (void) close(mpvect[1]); 1052 if (tTd(11, 1)) 1053 printf("openmailer: NULL\n"); 1054 rcode = EX_OSERR; 1055 goto give_up; 1056 } 1057 1058 /* 1059 ** Actually fork the mailer process. 1060 ** DOFORK is clever about retrying. 1061 ** 1062 ** Dispose of SIGCHLD signal catchers that may be laying 1063 ** around so that endmail will get it. 1064 */ 1065 1066 if (e->e_xfp != NULL) 1067 (void) fflush(e->e_xfp); /* for debugging */ 1068 (void) fflush(stdout); 1069 # ifdef SIGCHLD 1070 (void) setsignal(SIGCHLD, SIG_DFL); 1071 # endif /* SIGCHLD */ 1072 DOFORK(FORK); 1073 /* pid is set by DOFORK */ 1074 if (pid < 0) 1075 { 1076 /* failure */ 1077 syserr("%s... openmailer(%s): cannot fork", 1078 e->e_to, m->m_name); 1079 (void) close(mpvect[0]); 1080 (void) close(mpvect[1]); 1081 if (clever) 1082 { 1083 (void) close(rpvect[0]); 1084 (void) close(rpvect[1]); 1085 } 1086 if (tTd(11, 1)) 1087 printf("openmailer: NULL\n"); 1088 rcode = EX_OSERR; 1089 goto give_up; 1090 } 1091 else if (pid == 0) 1092 { 1093 int i; 1094 int saveerrno; 1095 char **ep; 1096 char *env[MAXUSERENVIRON]; 1097 extern char **environ; 1098 extern int DtableSize; 1099 1100 /* child -- set up input & exec mailer */ 1101 (void) setsignal(SIGINT, SIG_IGN); 1102 (void) setsignal(SIGHUP, SIG_IGN); 1103 (void) setsignal(SIGTERM, SIG_DFL); 1104 1105 /* close any other cached connections */ 1106 mci_flush(FALSE, mci); 1107 1108 /* reset user and group */ 1109 if (!bitnset(M_RESTR, m->m_flags)) 1110 { 1111 if (ctladdr == NULL || ctladdr->q_uid == 0) 1112 { 1113 (void) initgroups(DefUser, DefGid); 1114 (void) setuid(DefUid); 1115 } 1116 else 1117 { 1118 (void) initgroups(ctladdr->q_ruser? 1119 ctladdr->q_ruser: ctladdr->q_user, 1120 ctladdr->q_gid); 1121 (void) setuid(ctladdr->q_uid); 1122 } 1123 } 1124 1125 if (tTd(11, 2)) 1126 printf("openmailer: running as r/euid=%d/%d\n", 1127 getuid(), geteuid()); 1128 1129 /* move into some "safe" directory */ 1130 if (m->m_execdir != NULL) 1131 { 1132 char *p, *q; 1133 char buf[MAXLINE]; 1134 1135 for (p = m->m_execdir; p != NULL; p = q) 1136 { 1137 q = strchr(p, ':'); 1138 if (q != NULL) 1139 *q = '\0'; 1140 expand(p, buf, &buf[sizeof buf] - 1, e); 1141 if (q != NULL) 1142 *q++ = ':'; 1143 if (tTd(11, 20)) 1144 printf("openmailer: trydir %s\n", 1145 buf); 1146 if (buf[0] != '\0' && chdir(buf) >= 0) 1147 break; 1148 } 1149 } 1150 1151 /* arrange to filter std & diag output of command */ 1152 if (clever) 1153 { 1154 (void) close(rpvect[0]); 1155 if (dup2(rpvect[1], STDOUT_FILENO) < 0) 1156 { 1157 syserr("%s... openmailer(%s): cannot dup pipe %d for stdout", 1158 e->e_to, m->m_name, rpvect[1]); 1159 _exit(EX_OSERR); 1160 } 1161 (void) close(rpvect[1]); 1162 } 1163 else if (OpMode == MD_SMTP || HoldErrs) 1164 { 1165 /* put mailer output in transcript */ 1166 if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0) 1167 { 1168 syserr("%s... openmailer(%s): cannot dup xscript %d for stdout", 1169 e->e_to, m->m_name, 1170 fileno(e->e_xfp)); 1171 _exit(EX_OSERR); 1172 } 1173 } 1174 if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) 1175 { 1176 syserr("%s... openmailer(%s): cannot dup stdout for stderr", 1177 e->e_to, m->m_name); 1178 _exit(EX_OSERR); 1179 } 1180 1181 /* arrange to get standard input */ 1182 (void) close(mpvect[1]); 1183 if (dup2(mpvect[0], STDIN_FILENO) < 0) 1184 { 1185 syserr("%s... openmailer(%s): cannot dup pipe %d for stdin", 1186 e->e_to, m->m_name, mpvect[0]); 1187 _exit(EX_OSERR); 1188 } 1189 (void) close(mpvect[0]); 1190 1191 /* arrange for all the files to be closed */ 1192 for (i = 3; i < DtableSize; i++) 1193 { 1194 register int j; 1195 1196 if ((j = fcntl(i, F_GETFD, 0)) != -1) 1197 (void) fcntl(i, F_SETFD, j | 1); 1198 } 1199 1200 /* set up the mailer environment */ 1201 i = 0; 1202 env[i++] = "AGENT=sendmail"; 1203 for (ep = environ; *ep != NULL; ep++) 1204 { 1205 if (strncmp(*ep, "TZ=", 3) == 0) 1206 env[i++] = *ep; 1207 } 1208 env[i++] = NULL; 1209 1210 /* try to execute the mailer */ 1211 execve(m->m_mailer, pv, env); 1212 saveerrno = errno; 1213 syserr("Cannot exec %s", m->m_mailer); 1214 if (m == LocalMailer || transienterror(saveerrno)) 1215 _exit(EX_OSERR); 1216 _exit(EX_UNAVAILABLE); 1217 } 1218 1219 /* 1220 ** Set up return value. 1221 */ 1222 1223 mci = (MCI *) xalloc(sizeof *mci); 1224 bzero((char *) mci, sizeof *mci); 1225 mci->mci_mailer = m; 1226 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 1227 mci->mci_pid = pid; 1228 (void) close(mpvect[0]); 1229 mci->mci_out = fdopen(mpvect[1], "w"); 1230 if (clever) 1231 { 1232 (void) close(rpvect[1]); 1233 mci->mci_in = fdopen(rpvect[0], "r"); 1234 } 1235 else 1236 { 1237 mci->mci_flags |= MCIF_TEMP; 1238 mci->mci_in = NULL; 1239 } 1240 } 1241 1242 /* 1243 ** If we are in SMTP opening state, send initial protocol. 1244 */ 1245 1246 if (clever && mci->mci_state != MCIS_CLOSED) 1247 { 1248 smtpinit(m, mci, e); 1249 } 1250 if (tTd(11, 1)) 1251 { 1252 printf("openmailer: "); 1253 mci_dump(mci); 1254 } 1255 1256 if (mci->mci_state != MCIS_OPEN) 1257 { 1258 /* couldn't open the mailer */ 1259 rcode = mci->mci_exitstat; 1260 errno = mci->mci_errno; 1261 #ifdef NAMED_BIND 1262 h_errno = mci->mci_herrno; 1263 #endif 1264 if (rcode == EX_OK) 1265 { 1266 /* shouldn't happen */ 1267 syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 1268 rcode, mci->mci_state, firstsig); 1269 rcode = EX_SOFTWARE; 1270 } 1271 else if (rcode == EX_TEMPFAIL && *curhost != '\0') 1272 { 1273 /* try next MX site */ 1274 goto tryhost; 1275 } 1276 } 1277 else if (!clever) 1278 { 1279 /* 1280 ** Format and send message. 1281 */ 1282 1283 putfromline(mci->mci_out, m, e); 1284 (*e->e_puthdr)(mci->mci_out, m, e); 1285 putline("\n", mci->mci_out, m); 1286 (*e->e_putbody)(mci->mci_out, m, e, NULL); 1287 1288 /* get the exit status */ 1289 rcode = endmailer(mci, e, pv); 1290 } 1291 else 1292 #ifdef SMTP 1293 { 1294 /* 1295 ** Send the MAIL FROM: protocol 1296 */ 1297 1298 rcode = smtpmailfrom(m, mci, e); 1299 if (rcode == EX_OK) 1300 { 1301 register char *t = tobuf; 1302 register int i; 1303 1304 /* send the recipient list */ 1305 tobuf[0] = '\0'; 1306 for (to = tochain; to != NULL; to = to->q_tchain) 1307 { 1308 e->e_to = to->q_paddr; 1309 if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 1310 { 1311 markfailure(e, to, i); 1312 giveresponse(i, m, mci, e); 1313 } 1314 else 1315 { 1316 *t++ = ','; 1317 for (p = to->q_paddr; *p; *t++ = *p++) 1318 continue; 1319 *t = '\0'; 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 #ifdef XDEBUG 1392 { 1393 char wbuf[MAXLINE]; 1394 1395 /* make absolutely certain 0, 1, and 2 are in use */ 1396 sprintf(wbuf, "%s... end of deliver(%s)", 1397 e->e_to == NULL ? "NO-TO-LIST" : e->e_to, 1398 m->m_name); 1399 checkfd012(wbuf); 1400 } 1401 #endif 1402 1403 errno = 0; 1404 define('g', (char *) NULL, e); 1405 return (rcode); 1406 } 1407 /* 1408 ** MARKFAILURE -- mark a failure on a specific address. 1409 ** 1410 ** Parameters: 1411 ** e -- the envelope we are sending. 1412 ** q -- the address to mark. 1413 ** rcode -- the code signifying the particular failure. 1414 ** 1415 ** Returns: 1416 ** none. 1417 ** 1418 ** Side Effects: 1419 ** marks the address (and possibly the envelope) with the 1420 ** failure so that an error will be returned or 1421 ** the message will be queued, as appropriate. 1422 */ 1423 1424 markfailure(e, q, rcode) 1425 register ENVELOPE *e; 1426 register ADDRESS *q; 1427 int rcode; 1428 { 1429 char buf[MAXLINE]; 1430 1431 if (rcode == EX_OK) 1432 return; 1433 else if (rcode == EX_TEMPFAIL) 1434 q->q_flags |= QQUEUEUP; 1435 else if (rcode != EX_IOERR && rcode != EX_OSERR) 1436 q->q_flags |= QBADADDR; 1437 } 1438 /* 1439 ** ENDMAILER -- Wait for mailer to terminate. 1440 ** 1441 ** We should never get fatal errors (e.g., segmentation 1442 ** violation), so we report those specially. For other 1443 ** errors, we choose a status message (into statmsg), 1444 ** and if it represents an error, we print it. 1445 ** 1446 ** Parameters: 1447 ** pid -- pid of mailer. 1448 ** e -- the current envelope. 1449 ** pv -- the parameter vector that invoked the mailer 1450 ** (for error messages). 1451 ** 1452 ** Returns: 1453 ** exit code of mailer. 1454 ** 1455 ** Side Effects: 1456 ** none. 1457 */ 1458 1459 endmailer(mci, e, pv) 1460 register MCI *mci; 1461 register ENVELOPE *e; 1462 char **pv; 1463 { 1464 int st; 1465 1466 /* close any connections */ 1467 if (mci->mci_in != NULL) 1468 (void) xfclose(mci->mci_in, pv[0], "mci_in"); 1469 if (mci->mci_out != NULL) 1470 (void) xfclose(mci->mci_out, pv[0], "mci_out"); 1471 mci->mci_in = mci->mci_out = NULL; 1472 mci->mci_state = MCIS_CLOSED; 1473 1474 /* in the IPC case there is nothing to wait for */ 1475 if (mci->mci_pid == 0) 1476 return (EX_OK); 1477 1478 /* wait for the mailer process to die and collect status */ 1479 st = waitfor(mci->mci_pid); 1480 if (st == -1) 1481 { 1482 syserr("endmailer %s: wait", pv[0]); 1483 return (EX_SOFTWARE); 1484 } 1485 1486 if (WIFEXITED(st)) 1487 { 1488 /* normal death -- return status */ 1489 return (WEXITSTATUS(st)); 1490 } 1491 1492 /* it died a horrid death */ 1493 syserr("mailer %s died with signal %o", pv[0], st); 1494 1495 /* log the arguments */ 1496 if (e->e_xfp != NULL) 1497 { 1498 register char **av; 1499 1500 fprintf(e->e_xfp, "Arguments:"); 1501 for (av = pv; *av != NULL; av++) 1502 fprintf(e->e_xfp, " %s", *av); 1503 fprintf(e->e_xfp, "\n"); 1504 } 1505 1506 ExitStat = EX_TEMPFAIL; 1507 return (EX_TEMPFAIL); 1508 } 1509 /* 1510 ** GIVERESPONSE -- Interpret an error response from a mailer 1511 ** 1512 ** Parameters: 1513 ** stat -- the status code from the mailer (high byte 1514 ** only; core dumps must have been taken care of 1515 ** already). 1516 ** m -- the mailer info for this mailer. 1517 ** mci -- the mailer connection info -- can be NULL if the 1518 ** response is given before the connection is made. 1519 ** e -- the current envelope. 1520 ** 1521 ** Returns: 1522 ** none. 1523 ** 1524 ** Side Effects: 1525 ** Errors may be incremented. 1526 ** ExitStat may be set. 1527 */ 1528 1529 giveresponse(stat, m, mci, e) 1530 int stat; 1531 register MAILER *m; 1532 register MCI *mci; 1533 ENVELOPE *e; 1534 { 1535 register const char *statmsg; 1536 extern char *SysExMsg[]; 1537 register int i; 1538 extern int N_SysEx; 1539 char buf[MAXLINE]; 1540 1541 /* 1542 ** Compute status message from code. 1543 */ 1544 1545 i = stat - EX__BASE; 1546 if (stat == 0) 1547 { 1548 statmsg = "250 Sent"; 1549 if (e->e_statmsg != NULL) 1550 { 1551 (void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg); 1552 statmsg = buf; 1553 } 1554 } 1555 else if (i < 0 || i > N_SysEx) 1556 { 1557 (void) sprintf(buf, "554 unknown mailer error %d", stat); 1558 stat = EX_UNAVAILABLE; 1559 statmsg = buf; 1560 } 1561 else if (stat == EX_TEMPFAIL) 1562 { 1563 (void) strcpy(buf, SysExMsg[i] + 1); 1564 #ifdef NAMED_BIND 1565 if (h_errno == TRY_AGAIN) 1566 statmsg = errstring(h_errno+E_DNSBASE); 1567 else 1568 #endif 1569 { 1570 if (errno != 0) 1571 statmsg = errstring(errno); 1572 else 1573 { 1574 #ifdef SMTP 1575 extern char SmtpError[]; 1576 1577 statmsg = SmtpError; 1578 #else /* SMTP */ 1579 statmsg = NULL; 1580 #endif /* SMTP */ 1581 } 1582 } 1583 if (statmsg != NULL && statmsg[0] != '\0') 1584 { 1585 (void) strcat(buf, ": "); 1586 (void) strcat(buf, statmsg); 1587 } 1588 statmsg = buf; 1589 } 1590 #ifdef NAMED_BIND 1591 else if (stat == EX_NOHOST && h_errno != 0) 1592 { 1593 statmsg = errstring(h_errno + E_DNSBASE); 1594 (void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg); 1595 statmsg = buf; 1596 } 1597 #endif 1598 else 1599 { 1600 statmsg = SysExMsg[i]; 1601 if (*statmsg++ == ':') 1602 { 1603 (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 1604 statmsg = buf; 1605 } 1606 } 1607 1608 /* 1609 ** Print the message as appropriate 1610 */ 1611 1612 if (stat == EX_OK || stat == EX_TEMPFAIL) 1613 message(&statmsg[4], errstring(errno)); 1614 else 1615 { 1616 Errors++; 1617 usrerr(statmsg, errstring(errno)); 1618 } 1619 1620 /* 1621 ** Final cleanup. 1622 ** Log a record of the transaction. Compute the new 1623 ** ExitStat -- if we already had an error, stick with 1624 ** that. 1625 */ 1626 1627 if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 1628 logdelivery(m, mci, &statmsg[4], e); 1629 1630 if (stat != EX_TEMPFAIL) 1631 setstat(stat); 1632 if (stat != EX_OK) 1633 { 1634 if (e->e_message != NULL) 1635 free(e->e_message); 1636 e->e_message = newstr(&statmsg[4]); 1637 } 1638 errno = 0; 1639 #ifdef NAMED_BIND 1640 h_errno = 0; 1641 #endif 1642 } 1643 /* 1644 ** LOGDELIVERY -- log the delivery in the system log 1645 ** 1646 ** Parameters: 1647 ** m -- the mailer info. Can be NULL for initial queue. 1648 ** mci -- the mailer connection info -- can be NULL if the 1649 ** log is occuring when no connection is active. 1650 ** stat -- the message to print for the status. 1651 ** e -- the current envelope. 1652 ** 1653 ** Returns: 1654 ** none 1655 ** 1656 ** Side Effects: 1657 ** none 1658 */ 1659 1660 logdelivery(m, mci, stat, e) 1661 MAILER *m; 1662 register MCI *mci; 1663 char *stat; 1664 register ENVELOPE *e; 1665 { 1666 # ifdef LOG 1667 char buf[512]; 1668 1669 (void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 1670 1671 if (m != NULL) 1672 { 1673 (void) strcat(buf, ", mailer="); 1674 (void) strcat(buf, m->m_name); 1675 } 1676 1677 if (mci != NULL && mci->mci_host != NULL) 1678 { 1679 # ifdef DAEMON 1680 extern SOCKADDR CurHostAddr; 1681 # endif 1682 1683 (void) strcat(buf, ", relay="); 1684 (void) strcat(buf, mci->mci_host); 1685 1686 # ifdef DAEMON 1687 (void) strcat(buf, " ("); 1688 (void) strcat(buf, anynet_ntoa(&CurHostAddr)); 1689 (void) strcat(buf, ")"); 1690 # endif 1691 } 1692 else 1693 { 1694 char *p = macvalue('h', e); 1695 1696 if (p != NULL && p[0] != '\0') 1697 { 1698 (void) strcat(buf, ", relay="); 1699 (void) strcat(buf, p); 1700 } 1701 } 1702 1703 syslog(LOG_INFO, "%s: to=%s, %s, stat=%s", 1704 e->e_id, e->e_to, buf, stat); 1705 # endif /* LOG */ 1706 } 1707 /* 1708 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 1709 ** 1710 ** This can be made an arbitrary message separator by changing $l 1711 ** 1712 ** One of the ugliest hacks seen by human eyes is contained herein: 1713 ** UUCP wants those stupid "remote from <host>" lines. Why oh why 1714 ** does a well-meaning programmer such as myself have to deal with 1715 ** this kind of antique garbage???? 1716 ** 1717 ** Parameters: 1718 ** fp -- the file to output to. 1719 ** m -- the mailer describing this entry. 1720 ** 1721 ** Returns: 1722 ** none 1723 ** 1724 ** Side Effects: 1725 ** outputs some text to fp. 1726 */ 1727 1728 putfromline(fp, m, e) 1729 register FILE *fp; 1730 register MAILER *m; 1731 ENVELOPE *e; 1732 { 1733 char *template = "\201l\n"; 1734 char buf[MAXLINE]; 1735 1736 if (bitnset(M_NHDR, m->m_flags)) 1737 return; 1738 1739 # ifdef UGLYUUCP 1740 if (bitnset(M_UGLYUUCP, m->m_flags)) 1741 { 1742 char *bang; 1743 char xbuf[MAXLINE]; 1744 1745 expand("\201g", buf, &buf[sizeof buf - 1], e); 1746 bang = strchr(buf, '!'); 1747 if (bang == NULL) 1748 { 1749 errno = 0; 1750 syserr("554 No ! in UUCP From address! (%s given)", buf); 1751 } 1752 else 1753 { 1754 *bang++ = '\0'; 1755 (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 1756 template = xbuf; 1757 } 1758 } 1759 # endif /* UGLYUUCP */ 1760 expand(template, buf, &buf[sizeof buf - 1], e); 1761 putline(buf, fp, m); 1762 } 1763 /* 1764 ** PUTBODY -- put the body of a message. 1765 ** 1766 ** Parameters: 1767 ** fp -- file to output onto. 1768 ** m -- a mailer descriptor to control output format. 1769 ** e -- the envelope to put out. 1770 ** separator -- if non-NULL, a message separator that must 1771 ** not be permitted in the resulting message. 1772 ** 1773 ** Returns: 1774 ** none. 1775 ** 1776 ** Side Effects: 1777 ** The message is written onto fp. 1778 */ 1779 1780 putbody(fp, m, e, separator) 1781 FILE *fp; 1782 MAILER *m; 1783 register ENVELOPE *e; 1784 char *separator; 1785 { 1786 char buf[MAXLINE]; 1787 1788 /* 1789 ** Output the body of the message 1790 */ 1791 1792 if (e->e_dfp == NULL) 1793 { 1794 if (e->e_df != NULL) 1795 { 1796 e->e_dfp = fopen(e->e_df, "r"); 1797 if (e->e_dfp == NULL) 1798 syserr("putbody: Cannot open %s for %s from %s", 1799 e->e_df, e->e_to, e->e_from.q_paddr); 1800 } 1801 else 1802 putline("<<< No Message Collected >>>", fp, m); 1803 } 1804 if (e->e_dfp != NULL) 1805 { 1806 rewind(e->e_dfp); 1807 while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 1808 { 1809 if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1810 strncmp(buf, "From ", 5) == 0) 1811 (void) putc('>', fp); 1812 if (buf[0] == '-' && buf[1] == '-' && separator != NULL) 1813 { 1814 /* possible separator */ 1815 int sl = strlen(separator); 1816 1817 if (strncmp(&buf[2], separator, sl) == 0) 1818 (void) putc(' ', fp); 1819 } 1820 putline(buf, fp, m); 1821 } 1822 1823 if (ferror(e->e_dfp)) 1824 { 1825 syserr("putbody: read error"); 1826 ExitStat = EX_IOERR; 1827 } 1828 } 1829 1830 /* some mailers want extra blank line at end of message */ 1831 if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n') 1832 putline("", fp, m); 1833 1834 (void) fflush(fp); 1835 if (ferror(fp) && errno != EPIPE) 1836 { 1837 syserr("putbody: write error"); 1838 ExitStat = EX_IOERR; 1839 } 1840 errno = 0; 1841 } 1842 /* 1843 ** MAILFILE -- Send a message to a file. 1844 ** 1845 ** If the file has the setuid/setgid bits set, but NO execute 1846 ** bits, sendmail will try to become the owner of that file 1847 ** rather than the real user. Obviously, this only works if 1848 ** sendmail runs as root. 1849 ** 1850 ** This could be done as a subordinate mailer, except that it 1851 ** is used implicitly to save messages in ~/dead.letter. We 1852 ** view this as being sufficiently important as to include it 1853 ** here. For example, if the system is dying, we shouldn't have 1854 ** to create another process plus some pipes to save the message. 1855 ** 1856 ** Parameters: 1857 ** filename -- the name of the file to send to. 1858 ** ctladdr -- the controlling address header -- includes 1859 ** the userid/groupid to be when sending. 1860 ** 1861 ** Returns: 1862 ** The exit code associated with the operation. 1863 ** 1864 ** Side Effects: 1865 ** none. 1866 */ 1867 1868 mailfile(filename, ctladdr, e) 1869 char *filename; 1870 ADDRESS *ctladdr; 1871 register ENVELOPE *e; 1872 { 1873 register FILE *f; 1874 register int pid; 1875 int mode; 1876 1877 if (tTd(11, 1)) 1878 { 1879 printf("mailfile %s\n ctladdr=", filename); 1880 printaddr(ctladdr, FALSE); 1881 } 1882 1883 if (e->e_xfp != NULL) 1884 fflush(e->e_xfp); 1885 1886 /* 1887 ** Fork so we can change permissions here. 1888 ** Note that we MUST use fork, not vfork, because of 1889 ** the complications of calling subroutines, etc. 1890 */ 1891 1892 DOFORK(fork); 1893 1894 if (pid < 0) 1895 return (EX_OSERR); 1896 else if (pid == 0) 1897 { 1898 /* child -- actually write to file */ 1899 struct stat stb; 1900 1901 (void) setsignal(SIGINT, SIG_DFL); 1902 (void) setsignal(SIGHUP, SIG_DFL); 1903 (void) setsignal(SIGTERM, SIG_DFL); 1904 (void) umask(OldUmask); 1905 1906 if (stat(filename, &stb) < 0) 1907 stb.st_mode = FileMode; 1908 mode = stb.st_mode; 1909 1910 /* limit the errors to those actually caused in the child */ 1911 errno = 0; 1912 ExitStat = EX_OK; 1913 1914 if (bitset(0111, stb.st_mode)) 1915 exit(EX_CANTCREAT); 1916 if (ctladdr == NULL) 1917 ctladdr = &e->e_from; 1918 else 1919 { 1920 /* ignore setuid and setgid bits */ 1921 mode &= ~(S_ISGID|S_ISUID); 1922 } 1923 1924 /* we have to open the dfile BEFORE setuid */ 1925 if (e->e_dfp == NULL && e->e_df != NULL) 1926 { 1927 e->e_dfp = fopen(e->e_df, "r"); 1928 if (e->e_dfp == NULL) 1929 { 1930 syserr("mailfile: Cannot open %s for %s from %s", 1931 e->e_df, e->e_to, e->e_from.q_paddr); 1932 } 1933 } 1934 1935 if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 1936 { 1937 if (ctladdr->q_uid == 0) 1938 { 1939 (void) initgroups(DefUser, DefGid); 1940 } 1941 else 1942 { 1943 (void) initgroups(ctladdr->q_ruser ? 1944 ctladdr->q_ruser : ctladdr->q_user, 1945 ctladdr->q_gid); 1946 } 1947 } 1948 if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 1949 { 1950 if (ctladdr->q_uid == 0) 1951 (void) setuid(DefUid); 1952 else 1953 (void) setuid(ctladdr->q_uid); 1954 } 1955 FileName = filename; 1956 LineNumber = 0; 1957 f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode); 1958 if (f == NULL) 1959 { 1960 message("554 cannot open: %s", errstring(errno)); 1961 exit(EX_CANTCREAT); 1962 } 1963 1964 putfromline(f, FileMailer, e); 1965 (*e->e_puthdr)(f, FileMailer, e); 1966 putline("\n", f, FileMailer); 1967 (*e->e_putbody)(f, FileMailer, e, NULL); 1968 putline("\n", f, FileMailer); 1969 if (ferror(f)) 1970 { 1971 message("451 I/O error: %s", errstring(errno)); 1972 setstat(EX_IOERR); 1973 } 1974 (void) xfclose(f, "mailfile", filename); 1975 (void) fflush(stdout); 1976 1977 /* reset ISUID & ISGID bits for paranoid systems */ 1978 (void) chmod(filename, (int) stb.st_mode); 1979 exit(ExitStat); 1980 /*NOTREACHED*/ 1981 } 1982 else 1983 { 1984 /* parent -- wait for exit status */ 1985 int st; 1986 1987 st = waitfor(pid); 1988 if (WIFEXITED(st)) 1989 return (WEXITSTATUS(st)); 1990 else 1991 { 1992 syserr("child died on signal %d", st); 1993 return (EX_UNAVAILABLE); 1994 } 1995 /*NOTREACHED*/ 1996 } 1997 } 1998 /* 1999 ** HOSTSIGNATURE -- return the "signature" for a host. 2000 ** 2001 ** The signature describes how we are going to send this -- it 2002 ** can be just the hostname (for non-Internet hosts) or can be 2003 ** an ordered list of MX hosts. 2004 ** 2005 ** Parameters: 2006 ** m -- the mailer describing this host. 2007 ** host -- the host name. 2008 ** e -- the current envelope. 2009 ** 2010 ** Returns: 2011 ** The signature for this host. 2012 ** 2013 ** Side Effects: 2014 ** Can tweak the symbol table. 2015 */ 2016 2017 char * 2018 hostsignature(m, host, e) 2019 register MAILER *m; 2020 char *host; 2021 ENVELOPE *e; 2022 { 2023 register char *p; 2024 register STAB *s; 2025 int i; 2026 int len; 2027 #ifdef NAMED_BIND 2028 int nmx; 2029 auto int rcode; 2030 char *hp; 2031 char *endp; 2032 int oldoptions; 2033 char *mxhosts[MAXMXHOSTS + 1]; 2034 #endif 2035 2036 /* 2037 ** Check to see if this uses IPC -- if not, it can't have MX records. 2038 */ 2039 2040 p = m->m_mailer; 2041 if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 2042 { 2043 /* just an ordinary mailer */ 2044 return host; 2045 } 2046 2047 /* 2048 ** If it is a numeric address, just return it. 2049 */ 2050 2051 if (host[0] == '[') 2052 return host; 2053 2054 /* 2055 ** Look it up in the symbol table. 2056 */ 2057 2058 s = stab(host, ST_HOSTSIG, ST_ENTER); 2059 if (s->s_hostsig != NULL) 2060 return s->s_hostsig; 2061 2062 /* 2063 ** Not already there -- create a signature. 2064 */ 2065 2066 #ifdef NAMED_BIND 2067 if (ConfigLevel < 2) 2068 { 2069 oldoptions = _res.options; 2070 _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 2071 } 2072 2073 for (hp = host; hp != NULL; hp = endp) 2074 { 2075 endp = strchr(hp, ':'); 2076 if (endp != NULL) 2077 *endp = '\0'; 2078 2079 nmx = getmxrr(hp, mxhosts, TRUE, &rcode); 2080 2081 if (nmx <= 0) 2082 { 2083 register MCI *mci; 2084 extern int errno; 2085 2086 /* update the connection info for this host */ 2087 mci = mci_get(hp, m); 2088 mci->mci_exitstat = rcode; 2089 mci->mci_errno = errno; 2090 #ifdef NAMED_BIND 2091 mci->mci_herrno = h_errno; 2092 #endif 2093 2094 /* and return the original host name as the signature */ 2095 nmx = 1; 2096 mxhosts[0] = hp; 2097 } 2098 2099 len = 0; 2100 for (i = 0; i < nmx; i++) 2101 { 2102 len += strlen(mxhosts[i]) + 1; 2103 } 2104 if (s->s_hostsig != NULL) 2105 len += strlen(s->s_hostsig) + 1; 2106 p = xalloc(len); 2107 if (s->s_hostsig != NULL) 2108 { 2109 (void) strcpy(p, s->s_hostsig); 2110 free(s->s_hostsig); 2111 s->s_hostsig = p; 2112 p += strlen(p); 2113 *p++ = ':'; 2114 } 2115 else 2116 s->s_hostsig = p; 2117 for (i = 0; i < nmx; i++) 2118 { 2119 if (i != 0) 2120 *p++ = ':'; 2121 strcpy(p, mxhosts[i]); 2122 p += strlen(p); 2123 } 2124 if (endp != NULL) 2125 *endp++ = ':'; 2126 } 2127 makelower(s->s_hostsig); 2128 if (ConfigLevel < 2) 2129 _res.options = oldoptions; 2130 #else 2131 /* not using BIND -- the signature is just the host name */ 2132 s->s_hostsig = host; 2133 #endif 2134 if (tTd(17, 1)) 2135 printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 2136 return s->s_hostsig; 2137 } 2138