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