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