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