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