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