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.105 (Berkeley) 11/08/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 to->q_statdate = curtime(); 855 continue; 856 } 857 858 /* 859 ** Address is verified -- add this user to mailer 860 ** argv, and add it to the print list of recipients. 861 */ 862 863 /* link together the chain of recipients */ 864 to->q_tchain = tochain; 865 tochain = to; 866 867 /* create list of users for error messages */ 868 (void) strcat(tobuf, ","); 869 (void) strcat(tobuf, to->q_paddr); 870 define('u', user, e); /* to user */ 871 p = to->q_home; 872 if (p == NULL && ctladdr != NULL) 873 p = ctladdr->q_home; 874 define('z', p, e); /* user's home */ 875 876 /* 877 ** Expand out this user into argument list. 878 */ 879 880 if (!clever) 881 { 882 expand(*mvp, buf, &buf[sizeof buf - 1], e); 883 *pvp++ = newstr(buf); 884 if (pvp >= &pv[MAXPV - 2]) 885 { 886 /* allow some space for trailing parms */ 887 break; 888 } 889 } 890 } 891 892 /* see if any addresses still exist */ 893 if (tobuf[0] == '\0') 894 { 895 define('g', (char *) NULL, e); 896 return (0); 897 } 898 899 /* print out messages as full list */ 900 e->e_to = tobuf + 1; 901 902 /* 903 ** Fill out any parameters after the $u parameter. 904 */ 905 906 while (!clever && *++mvp != NULL) 907 { 908 expand(*mvp, buf, &buf[sizeof buf - 1], e); 909 *pvp++ = newstr(buf); 910 if (pvp >= &pv[MAXPV]) 911 syserr("554 deliver: pv overflow after $u for %s", pv[0]); 912 } 913 *pvp++ = NULL; 914 915 /* 916 ** Call the mailer. 917 ** The argument vector gets built, pipes 918 ** are created as necessary, and we fork & exec as 919 ** appropriate. 920 ** If we are running SMTP, we just need to clean up. 921 */ 922 923 /*XXX this seems a bit wierd */ 924 if (ctladdr == NULL && m != ProgMailer && 925 bitset(QGOODUID, e->e_from.q_flags)) 926 ctladdr = &e->e_from; 927 928 #if NAMED_BIND 929 if (ConfigLevel < 2) 930 _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 931 #endif 932 933 if (tTd(11, 1)) 934 { 935 printf("openmailer:"); 936 printav(pv); 937 } 938 errno = 0; 939 #if NAMED_BIND 940 h_errno = 0; 941 #endif 942 943 CurHostName = m->m_mailer; 944 945 /* 946 ** Deal with the special case of mail handled through an IPC 947 ** connection. 948 ** In this case we don't actually fork. We must be 949 ** running SMTP for this to work. We will return a 950 ** zero pid to indicate that we are running IPC. 951 ** We also handle a debug version that just talks to stdin/out. 952 */ 953 954 curhost = NULL; 955 SmtpPhase = NULL; 956 mci = NULL; 957 958 #ifdef XDEBUG 959 { 960 char wbuf[MAXLINE]; 961 962 /* make absolutely certain 0, 1, and 2 are in use */ 963 sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name); 964 checkfd012(wbuf); 965 } 966 #endif 967 968 /* check for 8-bit available */ 969 if (bitset(EF_HAS8BIT, e->e_flags) && 970 bitnset(M_7BITS, m->m_flags) && 971 !bitset(MM_MIME8BIT, MimeMode)) 972 { 973 usrerr("554 Cannot send 8-bit data to 7-bit destination"); 974 rcode = EX_DATAERR; 975 goto give_up; 976 } 977 978 /* check for Local Person Communication -- not for mortals!!! */ 979 if (strcmp(m->m_mailer, "[LPC]") == 0) 980 { 981 mci = (MCI *) xalloc(sizeof *mci); 982 bzero((char *) mci, sizeof *mci); 983 mci->mci_in = stdin; 984 mci->mci_out = stdout; 985 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 986 mci->mci_mailer = m; 987 } 988 else if (strcmp(m->m_mailer, "[IPC]") == 0 || 989 strcmp(m->m_mailer, "[TCP]") == 0) 990 { 991 #ifdef DAEMON 992 register int i; 993 register u_short port; 994 995 if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0') 996 { 997 syserr("null host name for %s mailer", m->m_mailer); 998 rcode = EX_CONFIG; 999 goto give_up; 1000 } 1001 1002 CurHostName = pv[1]; 1003 curhost = hostsignature(m, pv[1], e); 1004 1005 if (curhost == NULL || curhost[0] == '\0') 1006 { 1007 syserr("null host signature for %s", pv[1]); 1008 rcode = EX_CONFIG; 1009 goto give_up; 1010 } 1011 1012 if (!clever) 1013 { 1014 syserr("554 non-clever IPC"); 1015 rcode = EX_CONFIG; 1016 goto give_up; 1017 } 1018 if (pv[2] != NULL) 1019 port = atoi(pv[2]); 1020 else 1021 port = 0; 1022 tryhost: 1023 while (*curhost != '\0') 1024 { 1025 register char *p; 1026 static char hostbuf[MAXNAME]; 1027 1028 /* pull the next host from the signature */ 1029 p = strchr(curhost, ':'); 1030 if (p == NULL) 1031 p = &curhost[strlen(curhost)]; 1032 if (p == curhost) 1033 { 1034 syserr("deliver: null host name in signature"); 1035 curhost++; 1036 continue; 1037 } 1038 strncpy(hostbuf, curhost, p - curhost); 1039 hostbuf[p - curhost] = '\0'; 1040 if (*p != '\0') 1041 p++; 1042 curhost = p; 1043 1044 /* see if we already know that this host is fried */ 1045 CurHostName = hostbuf; 1046 mci = mci_get(hostbuf, m); 1047 if (mci->mci_state != MCIS_CLOSED) 1048 { 1049 if (tTd(11, 1)) 1050 { 1051 printf("openmailer: "); 1052 mci_dump(mci, FALSE); 1053 } 1054 CurHostName = mci->mci_host; 1055 message("Using cached connection to %s via %s...", 1056 hostbuf, m->m_name); 1057 break; 1058 } 1059 mci->mci_mailer = m; 1060 if (mci->mci_exitstat != EX_OK) 1061 continue; 1062 1063 /* try the connection */ 1064 setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 1065 message("Connecting to %s via %s...", 1066 hostbuf, m->m_name); 1067 i = makeconnection(hostbuf, port, mci, 1068 bitnset(M_SECURE_PORT, m->m_flags)); 1069 mci->mci_exitstat = i; 1070 mci->mci_errno = errno; 1071 #if NAMED_BIND 1072 mci->mci_herrno = h_errno; 1073 #endif 1074 if (i == EX_OK) 1075 { 1076 mci->mci_state = MCIS_OPENING; 1077 mci_cache(mci); 1078 if (TrafficLogFile != NULL) 1079 fprintf(TrafficLogFile, "%05d == CONNECT %s\n", 1080 getpid(), hostbuf); 1081 break; 1082 } 1083 else if (tTd(11, 1)) 1084 printf("openmailer: makeconnection => stat=%d, errno=%d\n", 1085 i, errno); 1086 1087 /* enter status of this host */ 1088 setstat(i); 1089 1090 /* should print some message here for -v mode */ 1091 } 1092 if (mci == NULL) 1093 { 1094 syserr("deliver: no host name"); 1095 rcode = EX_OSERR; 1096 goto give_up; 1097 } 1098 mci->mci_pid = 0; 1099 #else /* no DAEMON */ 1100 syserr("554 openmailer: no IPC"); 1101 if (tTd(11, 1)) 1102 printf("openmailer: NULL\n"); 1103 rcode = EX_UNAVAILABLE; 1104 goto give_up; 1105 #endif /* DAEMON */ 1106 } 1107 else 1108 { 1109 /* flush any expired connections */ 1110 (void) mci_scan(NULL); 1111 1112 /* announce the connection to verbose listeners */ 1113 if (host == NULL || host[0] == '\0') 1114 message("Connecting to %s...", m->m_name); 1115 else 1116 message("Connecting to %s via %s...", host, m->m_name); 1117 if (TrafficLogFile != NULL) 1118 { 1119 char **av; 1120 1121 fprintf(TrafficLogFile, "%05d === EXEC", getpid()); 1122 for (av = pv; *av != NULL; av++) 1123 fprintf(TrafficLogFile, " %s", *av); 1124 fprintf(TrafficLogFile, "\n"); 1125 } 1126 1127 /* create a pipe to shove the mail through */ 1128 if (pipe(mpvect) < 0) 1129 { 1130 syserr("%s... openmailer(%s): pipe (to mailer)", 1131 e->e_to, m->m_name); 1132 if (tTd(11, 1)) 1133 printf("openmailer: NULL\n"); 1134 rcode = EX_OSERR; 1135 goto give_up; 1136 } 1137 1138 /* if this mailer speaks smtp, create a return pipe */ 1139 if (clever && pipe(rpvect) < 0) 1140 { 1141 syserr("%s... openmailer(%s): pipe (from mailer)", 1142 e->e_to, m->m_name); 1143 (void) close(mpvect[0]); 1144 (void) close(mpvect[1]); 1145 if (tTd(11, 1)) 1146 printf("openmailer: NULL\n"); 1147 rcode = EX_OSERR; 1148 goto give_up; 1149 } 1150 1151 /* 1152 ** Actually fork the mailer process. 1153 ** DOFORK is clever about retrying. 1154 ** 1155 ** Dispose of SIGCHLD signal catchers that may be laying 1156 ** around so that endmail will get it. 1157 */ 1158 1159 if (e->e_xfp != NULL) 1160 (void) fflush(e->e_xfp); /* for debugging */ 1161 (void) fflush(stdout); 1162 # ifdef SIGCHLD 1163 (void) setsignal(SIGCHLD, SIG_DFL); 1164 # endif /* SIGCHLD */ 1165 DOFORK(FORK); 1166 /* pid is set by DOFORK */ 1167 if (pid < 0) 1168 { 1169 /* failure */ 1170 syserr("%s... openmailer(%s): cannot fork", 1171 e->e_to, m->m_name); 1172 (void) close(mpvect[0]); 1173 (void) close(mpvect[1]); 1174 if (clever) 1175 { 1176 (void) close(rpvect[0]); 1177 (void) close(rpvect[1]); 1178 } 1179 if (tTd(11, 1)) 1180 printf("openmailer: NULL\n"); 1181 rcode = EX_OSERR; 1182 goto give_up; 1183 } 1184 else if (pid == 0) 1185 { 1186 int i; 1187 int saveerrno; 1188 char **ep; 1189 char *env[MAXUSERENVIRON]; 1190 extern char **environ; 1191 extern int DtableSize; 1192 1193 /* child -- set up input & exec mailer */ 1194 (void) setsignal(SIGINT, SIG_IGN); 1195 (void) setsignal(SIGHUP, SIG_IGN); 1196 (void) setsignal(SIGTERM, SIG_DFL); 1197 1198 /* reset user and group */ 1199 if (bitnset(M_SPECIFIC_UID, m->m_flags)) 1200 { 1201 (void) setgid(m->m_gid); 1202 (void) setuid(m->m_uid); 1203 } 1204 else if (ctladdr != NULL && ctladdr->q_uid != 0) 1205 { 1206 (void) initgroups(ctladdr->q_ruser? 1207 ctladdr->q_ruser: ctladdr->q_user, 1208 ctladdr->q_gid); 1209 (void) setgid(ctladdr->q_gid); 1210 (void) setuid(ctladdr->q_uid); 1211 } 1212 else 1213 { 1214 (void) initgroups(DefUser, DefGid); 1215 if (m->m_gid == 0) 1216 (void) setgid(DefGid); 1217 else 1218 (void) setgid(m->m_gid); 1219 if (m->m_uid == 0) 1220 (void) setuid(DefUid); 1221 else 1222 (void) setuid(m->m_uid); 1223 } 1224 1225 if (tTd(11, 2)) 1226 printf("openmailer: running as r/euid=%d/%d\n", 1227 getuid(), geteuid()); 1228 1229 /* move into some "safe" directory */ 1230 if (m->m_execdir != NULL) 1231 { 1232 char *p, *q; 1233 char buf[MAXLINE]; 1234 1235 for (p = m->m_execdir; p != NULL; p = q) 1236 { 1237 q = strchr(p, ':'); 1238 if (q != NULL) 1239 *q = '\0'; 1240 expand(p, buf, &buf[sizeof buf] - 1, e); 1241 if (q != NULL) 1242 *q++ = ':'; 1243 if (tTd(11, 20)) 1244 printf("openmailer: trydir %s\n", 1245 buf); 1246 if (buf[0] != '\0' && chdir(buf) >= 0) 1247 break; 1248 } 1249 } 1250 1251 /* arrange to filter std & diag output of command */ 1252 if (clever) 1253 { 1254 (void) close(rpvect[0]); 1255 if (dup2(rpvect[1], STDOUT_FILENO) < 0) 1256 { 1257 syserr("%s... openmailer(%s): cannot dup pipe %d for stdout", 1258 e->e_to, m->m_name, rpvect[1]); 1259 _exit(EX_OSERR); 1260 } 1261 (void) close(rpvect[1]); 1262 } 1263 else if (OpMode == MD_SMTP || OpMode == MD_DAEMON || 1264 HoldErrs || DisConnected) 1265 { 1266 /* put mailer output in transcript */ 1267 if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0) 1268 { 1269 syserr("%s... openmailer(%s): cannot dup xscript %d for stdout", 1270 e->e_to, m->m_name, 1271 fileno(e->e_xfp)); 1272 _exit(EX_OSERR); 1273 } 1274 } 1275 if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) 1276 { 1277 syserr("%s... openmailer(%s): cannot dup stdout for stderr", 1278 e->e_to, m->m_name); 1279 _exit(EX_OSERR); 1280 } 1281 1282 /* arrange to get standard input */ 1283 (void) close(mpvect[1]); 1284 if (dup2(mpvect[0], STDIN_FILENO) < 0) 1285 { 1286 syserr("%s... openmailer(%s): cannot dup pipe %d for stdin", 1287 e->e_to, m->m_name, mpvect[0]); 1288 _exit(EX_OSERR); 1289 } 1290 (void) close(mpvect[0]); 1291 1292 /* arrange for all the files to be closed */ 1293 for (i = 3; i < DtableSize; i++) 1294 { 1295 register int j; 1296 1297 if ((j = fcntl(i, F_GETFD, 0)) != -1) 1298 (void) fcntl(i, F_SETFD, j | 1); 1299 } 1300 1301 /* 1302 ** Set up the mailer environment 1303 ** _FORCE_MAIL_LOCAL_ is DG-UX equiv of -d flag. 1304 ** TZ is timezone information. 1305 ** SYSTYPE is Apollo software sys type (required). 1306 ** ISP is Apollo hardware system type (required). 1307 */ 1308 1309 i = 0; 1310 env[i++] = "AGENT=sendmail"; 1311 env[i++] = "_FORCE_MAIL_LOCAL_=yes"; 1312 for (ep = environ; *ep != NULL; ep++) 1313 { 1314 if (strncmp(*ep, "TZ=", 3) == 0 || 1315 strncmp(*ep, "ISP=", 4) == 0 || 1316 strncmp(*ep, "SYSTYPE=", 8) == 0) 1317 env[i++] = *ep; 1318 } 1319 env[i] = NULL; 1320 1321 /* run disconnected from terminal */ 1322 (void) setsid(); 1323 1324 /* try to execute the mailer */ 1325 execve(m->m_mailer, pv, env); 1326 saveerrno = errno; 1327 syserr("Cannot exec %s", m->m_mailer); 1328 if (bitnset(M_LOCALMAILER, m->m_flags) || 1329 transienterror(saveerrno)) 1330 _exit(EX_OSERR); 1331 _exit(EX_UNAVAILABLE); 1332 } 1333 1334 /* 1335 ** Set up return value. 1336 */ 1337 1338 mci = (MCI *) xalloc(sizeof *mci); 1339 bzero((char *) mci, sizeof *mci); 1340 mci->mci_mailer = m; 1341 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 1342 mci->mci_pid = pid; 1343 (void) close(mpvect[0]); 1344 mci->mci_out = fdopen(mpvect[1], "w"); 1345 if (mci->mci_out == NULL) 1346 { 1347 syserr("deliver: cannot create mailer output channel, fd=%d", 1348 mpvect[1]); 1349 (void) close(mpvect[1]); 1350 if (clever) 1351 { 1352 (void) close(rpvect[0]); 1353 (void) close(rpvect[1]); 1354 } 1355 rcode = EX_OSERR; 1356 goto give_up; 1357 } 1358 if (clever) 1359 { 1360 (void) close(rpvect[1]); 1361 mci->mci_in = fdopen(rpvect[0], "r"); 1362 if (mci->mci_in == NULL) 1363 { 1364 syserr("deliver: cannot create mailer input channel, fd=%d", 1365 mpvect[1]); 1366 (void) close(rpvect[0]); 1367 fclose(mci->mci_out); 1368 mci->mci_out = NULL; 1369 rcode = EX_OSERR; 1370 goto give_up; 1371 } 1372 } 1373 else 1374 { 1375 mci->mci_flags |= MCIF_TEMP; 1376 mci->mci_in = NULL; 1377 } 1378 } 1379 1380 /* 1381 ** If we are in SMTP opening state, send initial protocol. 1382 */ 1383 1384 if (clever && mci->mci_state != MCIS_CLOSED) 1385 { 1386 smtpinit(m, mci, e); 1387 } 1388 1389 if (bitset(EF_HAS8BIT, e->e_flags) && bitnset(M_7BITS, m->m_flags)) 1390 mci->mci_flags |= MCIF_CVT8TO7; 1391 else 1392 mci->mci_flags &= ~MCIF_CVT8TO7; 1393 1394 if (tTd(11, 1)) 1395 { 1396 printf("openmailer: "); 1397 mci_dump(mci, FALSE); 1398 } 1399 1400 if (mci->mci_state != MCIS_OPEN) 1401 { 1402 /* couldn't open the mailer */ 1403 rcode = mci->mci_exitstat; 1404 errno = mci->mci_errno; 1405 #if NAMED_BIND 1406 h_errno = mci->mci_herrno; 1407 #endif 1408 if (rcode == EX_OK) 1409 { 1410 /* shouldn't happen */ 1411 syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 1412 rcode, mci->mci_state, firstsig); 1413 rcode = EX_SOFTWARE; 1414 } 1415 else if (rcode == EX_TEMPFAIL && curhost != NULL && *curhost != '\0') 1416 { 1417 /* try next MX site */ 1418 goto tryhost; 1419 } 1420 } 1421 else if (!clever) 1422 { 1423 /* 1424 ** Format and send message. 1425 */ 1426 1427 putfromline(mci, e); 1428 (*e->e_puthdr)(mci, e->e_header, e); 1429 (*e->e_putbody)(mci, e, NULL); 1430 1431 /* get the exit status */ 1432 rcode = endmailer(mci, e, pv); 1433 } 1434 else 1435 #ifdef SMTP 1436 { 1437 /* 1438 ** Send the MAIL FROM: protocol 1439 */ 1440 1441 rcode = smtpmailfrom(m, mci, e); 1442 if (rcode == EX_OK) 1443 { 1444 register char *t = tobuf; 1445 register int i; 1446 1447 /* send the recipient list */ 1448 tobuf[0] = '\0'; 1449 for (to = tochain; to != NULL; to = to->q_tchain) 1450 { 1451 e->e_to = to->q_paddr; 1452 if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 1453 { 1454 markfailure(e, to, i); 1455 giveresponse(i, m, mci, ctladdr, e); 1456 } 1457 else 1458 { 1459 *t++ = ','; 1460 for (p = to->q_paddr; *p; *t++ = *p++) 1461 continue; 1462 *t = '\0'; 1463 } 1464 } 1465 1466 /* now send the data */ 1467 if (tobuf[0] == '\0') 1468 { 1469 rcode = EX_OK; 1470 e->e_to = NULL; 1471 if (bitset(MCIF_CACHED, mci->mci_flags)) 1472 smtprset(m, mci, e); 1473 } 1474 else 1475 { 1476 e->e_to = tobuf + 1; 1477 rcode = smtpdata(m, mci, e); 1478 } 1479 1480 /* now close the connection */ 1481 if (!bitset(MCIF_CACHED, mci->mci_flags)) 1482 smtpquit(m, mci, e); 1483 } 1484 if (rcode != EX_OK && curhost != NULL && *curhost != '\0') 1485 { 1486 /* try next MX site */ 1487 goto tryhost; 1488 } 1489 } 1490 #else /* not SMTP */ 1491 { 1492 syserr("554 deliver: need SMTP compiled to use clever mailer"); 1493 rcode = EX_CONFIG; 1494 goto give_up; 1495 } 1496 #endif /* SMTP */ 1497 #if NAMED_BIND 1498 if (ConfigLevel < 2) 1499 _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 1500 #endif 1501 1502 /* arrange a return receipt if requested */ 1503 if (rcode == EX_OK && e->e_receiptto != NULL && 1504 bitnset(M_LOCALMAILER, m->m_flags)) 1505 { 1506 e->e_flags |= EF_SENDRECEIPT; 1507 /* do we want to send back more info? */ 1508 } 1509 1510 /* 1511 ** Do final status disposal. 1512 ** We check for something in tobuf for the SMTP case. 1513 ** If we got a temporary failure, arrange to queue the 1514 ** addressees. 1515 */ 1516 1517 give_up: 1518 if (tobuf[0] != '\0') 1519 giveresponse(rcode, m, mci, ctladdr, e); 1520 for (to = tochain; to != NULL; to = to->q_tchain) 1521 { 1522 if (rcode != EX_OK) 1523 markfailure(e, to, rcode); 1524 else 1525 { 1526 to->q_flags |= QSENT; 1527 e->e_nsent++; 1528 if (bitnset(M_LOCALMAILER, m->m_flags) && 1529 (e->e_receiptto != NULL || 1530 bitset(QPINGONSUCCESS, to->q_flags))) 1531 { 1532 to->q_flags |= QREPORT; 1533 fprintf(e->e_xfp, "%s... Successfully delivered\n", 1534 to->q_paddr); 1535 } 1536 } 1537 } 1538 1539 /* 1540 ** Restore state and return. 1541 */ 1542 1543 #ifdef XDEBUG 1544 { 1545 char wbuf[MAXLINE]; 1546 1547 /* make absolutely certain 0, 1, and 2 are in use */ 1548 sprintf(wbuf, "%s... end of deliver(%s)", 1549 e->e_to == NULL ? "NO-TO-LIST" : e->e_to, 1550 m->m_name); 1551 checkfd012(wbuf); 1552 } 1553 #endif 1554 1555 errno = 0; 1556 define('g', (char *) NULL, e); 1557 return (rcode); 1558 } 1559 /* 1560 ** MARKFAILURE -- mark a failure on a specific address. 1561 ** 1562 ** Parameters: 1563 ** e -- the envelope we are sending. 1564 ** q -- the address to mark. 1565 ** rcode -- the code signifying the particular failure. 1566 ** 1567 ** Returns: 1568 ** none. 1569 ** 1570 ** Side Effects: 1571 ** marks the address (and possibly the envelope) with the 1572 ** failure so that an error will be returned or 1573 ** the message will be queued, as appropriate. 1574 */ 1575 1576 markfailure(e, q, rcode) 1577 register ENVELOPE *e; 1578 register ADDRESS *q; 1579 int rcode; 1580 { 1581 char buf[MAXLINE]; 1582 1583 switch (rcode) 1584 { 1585 case EX_OK: 1586 break; 1587 1588 case EX_TEMPFAIL: 1589 case EX_IOERR: 1590 case EX_OSERR: 1591 q->q_flags |= QQUEUEUP; 1592 break; 1593 1594 default: 1595 q->q_flags |= QBADADDR; 1596 break; 1597 } 1598 q->q_statdate = curtime(); 1599 q->q_statmta = newstr(CurHostName); 1600 } 1601 /* 1602 ** ENDMAILER -- Wait for mailer to terminate. 1603 ** 1604 ** We should never get fatal errors (e.g., segmentation 1605 ** violation), so we report those specially. For other 1606 ** errors, we choose a status message (into statmsg), 1607 ** and if it represents an error, we print it. 1608 ** 1609 ** Parameters: 1610 ** pid -- pid of mailer. 1611 ** e -- the current envelope. 1612 ** pv -- the parameter vector that invoked the mailer 1613 ** (for error messages). 1614 ** 1615 ** Returns: 1616 ** exit code of mailer. 1617 ** 1618 ** Side Effects: 1619 ** none. 1620 */ 1621 1622 endmailer(mci, e, pv) 1623 register MCI *mci; 1624 register ENVELOPE *e; 1625 char **pv; 1626 { 1627 int st; 1628 1629 /* close any connections */ 1630 if (mci->mci_in != NULL) 1631 (void) xfclose(mci->mci_in, mci->mci_mailer->m_name, "mci_in"); 1632 if (mci->mci_out != NULL) 1633 (void) xfclose(mci->mci_out, mci->mci_mailer->m_name, "mci_out"); 1634 mci->mci_in = mci->mci_out = NULL; 1635 mci->mci_state = MCIS_CLOSED; 1636 1637 /* in the IPC case there is nothing to wait for */ 1638 if (mci->mci_pid == 0) 1639 return (EX_OK); 1640 1641 /* wait for the mailer process to die and collect status */ 1642 st = waitfor(mci->mci_pid); 1643 if (st == -1) 1644 { 1645 syserr("endmailer %s: wait", pv[0]); 1646 return (EX_SOFTWARE); 1647 } 1648 1649 if (WIFEXITED(st)) 1650 { 1651 /* normal death -- return status */ 1652 return (WEXITSTATUS(st)); 1653 } 1654 1655 /* it died a horrid death */ 1656 syserr("451 mailer %s died with signal %o", 1657 mci->mci_mailer->m_name, st); 1658 1659 /* log the arguments */ 1660 if (pv != NULL && e->e_xfp != NULL) 1661 { 1662 register char **av; 1663 1664 fprintf(e->e_xfp, "Arguments:"); 1665 for (av = pv; *av != NULL; av++) 1666 fprintf(e->e_xfp, " %s", *av); 1667 fprintf(e->e_xfp, "\n"); 1668 } 1669 1670 ExitStat = EX_TEMPFAIL; 1671 return (EX_TEMPFAIL); 1672 } 1673 /* 1674 ** GIVERESPONSE -- Interpret an error response from a mailer 1675 ** 1676 ** Parameters: 1677 ** stat -- the status code from the mailer (high byte 1678 ** only; core dumps must have been taken care of 1679 ** already). 1680 ** m -- the mailer info for this mailer. 1681 ** mci -- the mailer connection info -- can be NULL if the 1682 ** response is given before the connection is made. 1683 ** ctladdr -- the controlling address for the recipient 1684 ** address(es). 1685 ** e -- the current envelope. 1686 ** 1687 ** Returns: 1688 ** none. 1689 ** 1690 ** Side Effects: 1691 ** Errors may be incremented. 1692 ** ExitStat may be set. 1693 */ 1694 1695 giveresponse(stat, m, mci, ctladdr, e) 1696 int stat; 1697 register MAILER *m; 1698 register MCI *mci; 1699 ADDRESS *ctladdr; 1700 ENVELOPE *e; 1701 { 1702 register const char *statmsg; 1703 extern char *SysExMsg[]; 1704 register int i; 1705 extern int N_SysEx; 1706 char buf[MAXLINE]; 1707 1708 /* 1709 ** Compute status message from code. 1710 */ 1711 1712 i = stat - EX__BASE; 1713 if (stat == 0) 1714 { 1715 statmsg = "250 Sent"; 1716 if (e->e_statmsg != NULL) 1717 { 1718 (void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg); 1719 statmsg = buf; 1720 } 1721 } 1722 else if (i < 0 || i > N_SysEx) 1723 { 1724 (void) sprintf(buf, "554 unknown mailer error %d", stat); 1725 stat = EX_UNAVAILABLE; 1726 statmsg = buf; 1727 } 1728 else if (stat == EX_TEMPFAIL) 1729 { 1730 (void) strcpy(buf, SysExMsg[i] + 1); 1731 #if NAMED_BIND 1732 if (h_errno == TRY_AGAIN) 1733 statmsg = errstring(h_errno+E_DNSBASE); 1734 else 1735 #endif 1736 { 1737 if (errno != 0) 1738 statmsg = errstring(errno); 1739 else 1740 { 1741 #ifdef SMTP 1742 statmsg = SmtpError; 1743 #else /* SMTP */ 1744 statmsg = NULL; 1745 #endif /* SMTP */ 1746 } 1747 } 1748 if (statmsg != NULL && statmsg[0] != '\0') 1749 { 1750 (void) strcat(buf, ": "); 1751 (void) strcat(buf, statmsg); 1752 } 1753 statmsg = buf; 1754 } 1755 #if NAMED_BIND 1756 else if (stat == EX_NOHOST && h_errno != 0) 1757 { 1758 statmsg = errstring(h_errno + E_DNSBASE); 1759 (void) sprintf(buf, "%s (%s)", SysExMsg[i] + 1, statmsg); 1760 statmsg = buf; 1761 } 1762 #endif 1763 else 1764 { 1765 statmsg = SysExMsg[i]; 1766 if (*statmsg++ == ':') 1767 { 1768 (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 1769 statmsg = buf; 1770 } 1771 } 1772 1773 /* 1774 ** Print the message as appropriate 1775 */ 1776 1777 if (stat == EX_OK || stat == EX_TEMPFAIL) 1778 { 1779 extern char MsgBuf[]; 1780 1781 message("%s", &statmsg[4]); 1782 if (stat == EX_TEMPFAIL && e->e_xfp != NULL) 1783 fprintf(e->e_xfp, "%s\n", &MsgBuf[4]); 1784 } 1785 else 1786 { 1787 char mbuf[8]; 1788 1789 Errors++; 1790 sprintf(mbuf, "%.3s %%s", statmsg); 1791 usrerr(mbuf, &statmsg[4]); 1792 } 1793 1794 /* 1795 ** Final cleanup. 1796 ** Log a record of the transaction. Compute the new 1797 ** ExitStat -- if we already had an error, stick with 1798 ** that. 1799 */ 1800 1801 if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 1802 logdelivery(m, mci, &statmsg[4], ctladdr, e); 1803 1804 if (tTd(11, 2)) 1805 printf("giveresponse: stat=%d, e->e_message=%s\n", 1806 stat, e->e_message); 1807 1808 if (stat != EX_TEMPFAIL) 1809 setstat(stat); 1810 if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL)) 1811 { 1812 if (e->e_message != NULL) 1813 free(e->e_message); 1814 e->e_message = newstr(&statmsg[4]); 1815 } 1816 errno = 0; 1817 #if NAMED_BIND 1818 h_errno = 0; 1819 #endif 1820 } 1821 /* 1822 ** LOGDELIVERY -- log the delivery in the system log 1823 ** 1824 ** Care is taken to avoid logging lines that are too long, because 1825 ** some versions of syslog have an unfortunate proclivity for core 1826 ** dumping. This is a hack, to be sure, that is at best empirical. 1827 ** 1828 ** Parameters: 1829 ** m -- the mailer info. Can be NULL for initial queue. 1830 ** mci -- the mailer connection info -- can be NULL if the 1831 ** log is occuring when no connection is active. 1832 ** stat -- the message to print for the status. 1833 ** ctladdr -- the controlling address for the to list. 1834 ** e -- the current envelope. 1835 ** 1836 ** Returns: 1837 ** none 1838 ** 1839 ** Side Effects: 1840 ** none 1841 */ 1842 1843 logdelivery(m, mci, stat, ctladdr, e) 1844 MAILER *m; 1845 register MCI *mci; 1846 char *stat; 1847 ADDRESS *ctladdr; 1848 register ENVELOPE *e; 1849 { 1850 # ifdef LOG 1851 register char *bp; 1852 register char *p; 1853 int l; 1854 char buf[512]; 1855 1856 # if (SYSLOG_BUFSIZE) >= 256 1857 bp = buf; 1858 if (ctladdr != NULL) 1859 { 1860 strcpy(bp, ", ctladdr="); 1861 strcat(bp, shortenstring(ctladdr->q_paddr, 83)); 1862 bp += strlen(bp); 1863 if (bitset(QGOODUID, ctladdr->q_flags)) 1864 { 1865 (void) sprintf(bp, " (%d/%d)", 1866 ctladdr->q_uid, ctladdr->q_gid); 1867 bp += strlen(bp); 1868 } 1869 } 1870 1871 (void) sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 1872 bp += strlen(bp); 1873 1874 if (m != NULL) 1875 { 1876 (void) strcpy(bp, ", mailer="); 1877 (void) strcat(bp, m->m_name); 1878 bp += strlen(bp); 1879 } 1880 1881 if (mci != NULL && mci->mci_host != NULL) 1882 { 1883 # ifdef DAEMON 1884 extern SOCKADDR CurHostAddr; 1885 # endif 1886 1887 (void) strcpy(bp, ", relay="); 1888 (void) strcat(bp, mci->mci_host); 1889 1890 # ifdef DAEMON 1891 (void) strcat(bp, " ["); 1892 (void) strcat(bp, anynet_ntoa(&CurHostAddr)); 1893 (void) strcat(bp, "]"); 1894 # endif 1895 } 1896 else if (strcmp(stat, "queued") != 0) 1897 { 1898 char *p = macvalue('h', e); 1899 1900 if (p != NULL && p[0] != '\0') 1901 { 1902 (void) strcpy(bp, ", relay="); 1903 (void) strcat(bp, p); 1904 } 1905 } 1906 bp += strlen(bp); 1907 1908 #define STATLEN (((SYSLOG_BUFSIZE) - 100) / 4) 1909 #if (STATLEN) < 63 1910 # undef STATLEN 1911 # define STATLEN 63 1912 #endif 1913 #if (STATLEN) > 203 1914 # undef STATLEN 1915 # define STATLEN 203 1916 #endif 1917 1918 if ((bp - buf) > (sizeof buf - ((STATLEN) + 20))) 1919 { 1920 /* desperation move -- truncate data */ 1921 bp = buf + sizeof buf - ((STATLEN) + 17); 1922 strcpy(bp, "..."); 1923 bp += 3; 1924 } 1925 1926 (void) strcpy(bp, ", stat="); 1927 bp += strlen(bp); 1928 1929 (void) strcpy(bp, shortenstring(stat, (STATLEN))); 1930 1931 l = SYSLOG_BUFSIZE - 100 - strlen(buf); 1932 p = e->e_to; 1933 while (strlen(p) >= l) 1934 { 1935 register char *q = strchr(p + l, ','); 1936 1937 if (q == NULL) 1938 break; 1939 syslog(LOG_INFO, "%s: to=%.*s [more]%s", 1940 e->e_id, ++q - p, p, buf); 1941 p = q; 1942 } 1943 syslog(LOG_INFO, "%s: to=%s%s", e->e_id, p, buf); 1944 1945 # else /* we have a very short log buffer size */ 1946 1947 l = SYSLOG_BUFSIZE - 85; 1948 p = e->e_to; 1949 while (strlen(p) >= l) 1950 { 1951 register char *q = strchr(p + l, ','); 1952 1953 if (q == NULL) 1954 break; 1955 syslog(LOG_INFO, "%s: to=%.*s [more]", 1956 e->e_id, ++q - p, p); 1957 p = q; 1958 } 1959 syslog(LOG_INFO, "%s: to=%s", e->e_id, p); 1960 1961 if (ctladdr != NULL) 1962 { 1963 bp = buf; 1964 strcpy(buf, "ctladdr="); 1965 bp += strlen(buf); 1966 strcpy(bp, shortenstring(ctladdr->q_paddr, 83)); 1967 bp += strlen(buf); 1968 if (bitset(QGOODUID, ctladdr->q_flags)) 1969 { 1970 (void) sprintf(bp, " (%d/%d)", 1971 ctladdr->q_uid, ctladdr->q_gid); 1972 bp += strlen(bp); 1973 } 1974 syslog(LOG_INFO, "%s: %s", e->e_id, buf); 1975 } 1976 bp = buf; 1977 sprintf(bp, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 1978 bp += strlen(bp); 1979 1980 if (m != NULL) 1981 { 1982 sprintf(bp, ", mailer=%s", m->m_name); 1983 bp += strlen(bp); 1984 } 1985 syslog(LOG_INFO, "%s: %s", e->e_id, buf); 1986 1987 buf[0] = '\0'; 1988 if (mci != NULL && mci->mci_host != NULL) 1989 { 1990 # ifdef DAEMON 1991 extern SOCKADDR CurHostAddr; 1992 # endif 1993 1994 sprintf(buf, "relay=%s", mci->mci_host); 1995 1996 # ifdef DAEMON 1997 (void) strcat(buf, " ["); 1998 (void) strcat(buf, anynet_ntoa(&CurHostAddr)); 1999 (void) strcat(buf, "]"); 2000 # endif 2001 } 2002 else if (strcmp(stat, "queued") != 0) 2003 { 2004 char *p = macvalue('h', e); 2005 2006 if (p != NULL && p[0] != '\0') 2007 sprintf(buf, "relay=%s", p); 2008 } 2009 if (buf[0] != '\0') 2010 syslog(LOG_INFO, "%s: %s", e->e_id, buf); 2011 2012 syslog(LOG_INFO, "%s: stat=%s", e->e_id, shortenstring(stat, 63)); 2013 # endif /* short log buffer */ 2014 # endif /* LOG */ 2015 } 2016 /* 2017 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 2018 ** 2019 ** This can be made an arbitrary message separator by changing $l 2020 ** 2021 ** One of the ugliest hacks seen by human eyes is contained herein: 2022 ** UUCP wants those stupid "remote from <host>" lines. Why oh why 2023 ** does a well-meaning programmer such as myself have to deal with 2024 ** this kind of antique garbage???? 2025 ** 2026 ** Parameters: 2027 ** mci -- the connection information. 2028 ** e -- the envelope. 2029 ** 2030 ** Returns: 2031 ** none 2032 ** 2033 ** Side Effects: 2034 ** outputs some text to fp. 2035 */ 2036 2037 putfromline(mci, e) 2038 register MCI *mci; 2039 ENVELOPE *e; 2040 { 2041 char *template = "\201l\n"; 2042 char buf[MAXLINE]; 2043 2044 if (bitnset(M_NHDR, mci->mci_mailer->m_flags)) 2045 return; 2046 2047 # ifdef UGLYUUCP 2048 if (bitnset(M_UGLYUUCP, mci->mci_mailer->m_flags)) 2049 { 2050 char *bang; 2051 char xbuf[MAXLINE]; 2052 2053 expand("\201g", buf, &buf[sizeof buf - 1], e); 2054 bang = strchr(buf, '!'); 2055 if (bang == NULL) 2056 { 2057 errno = 0; 2058 syserr("554 No ! in UUCP From address! (%s given)", buf); 2059 } 2060 else 2061 { 2062 *bang++ = '\0'; 2063 (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 2064 template = xbuf; 2065 } 2066 } 2067 # endif /* UGLYUUCP */ 2068 expand(template, buf, &buf[sizeof buf - 1], e); 2069 putline(buf, mci); 2070 } 2071 /* 2072 ** PUTBODY -- put the body of a message. 2073 ** 2074 ** Parameters: 2075 ** mci -- the connection information. 2076 ** e -- the envelope to put out. 2077 ** separator -- if non-NULL, a message separator that must 2078 ** not be permitted in the resulting message. 2079 ** 2080 ** Returns: 2081 ** none. 2082 ** 2083 ** Side Effects: 2084 ** The message is written onto fp. 2085 */ 2086 2087 /* values for output state variable */ 2088 #define OS_HEAD 0 /* at beginning of line */ 2089 #define OS_CR 1 /* read a carriage return */ 2090 #define OS_INLINE 2 /* putting rest of line */ 2091 2092 putbody(mci, e, separator) 2093 register MCI *mci; 2094 register ENVELOPE *e; 2095 char *separator; 2096 { 2097 char buf[MAXLINE]; 2098 2099 /* 2100 ** Output the body of the message 2101 */ 2102 2103 if (e->e_dfp == NULL) 2104 { 2105 if (e->e_df != NULL) 2106 { 2107 e->e_dfp = fopen(e->e_df, "r"); 2108 if (e->e_dfp == NULL) 2109 syserr("putbody: Cannot open %s for %s from %s", 2110 e->e_df, e->e_to, e->e_from.q_paddr); 2111 } 2112 else 2113 { 2114 if (bitset(MCIF_INHEADER, mci->mci_flags)) 2115 { 2116 putline("", mci); 2117 mci->mci_flags &= ~MCIF_INHEADER; 2118 } 2119 putline("<<< No Message Collected >>>", mci); 2120 goto endofmessage; 2121 } 2122 } 2123 if (e->e_dfp != NULL && e->e_dfino == (ino_t) 0) 2124 { 2125 struct stat stbuf; 2126 2127 if (fstat(fileno(e->e_dfp), &stbuf) < 0) 2128 e->e_dfino = -1; 2129 else 2130 { 2131 e->e_dfdev = stbuf.st_dev; 2132 e->e_dfino = stbuf.st_ino; 2133 } 2134 } 2135 rewind(e->e_dfp); 2136 2137 if (bitset(MCIF_CVT8TO7, mci->mci_flags)) 2138 { 2139 /* 2140 ** Do 8 to 7 bit MIME conversion. 2141 */ 2142 2143 /* make sure it looks like a MIME message */ 2144 if (hvalue("MIME-Version", e->e_header) == NULL) 2145 putline("MIME-Version: 1.0", mci); 2146 2147 if (hvalue("Content-Type", e->e_header) == NULL) 2148 { 2149 sprintf(buf, "Content-Type: text/plain; charset=%s", 2150 defcharset(e)); 2151 putline(buf, mci); 2152 } 2153 2154 /* now do the hard work */ 2155 mime8to7(mci, e->e_header, e, NULL); 2156 } 2157 else 2158 { 2159 int ostate; 2160 register char *bp; 2161 register char *pbp; 2162 register int c; 2163 int padc; 2164 char *buflim; 2165 int pos; 2166 char peekbuf[10]; 2167 2168 /* we can pass it through unmodified */ 2169 if (bitset(MCIF_INHEADER, mci->mci_flags)) 2170 { 2171 putline("", mci); 2172 mci->mci_flags &= ~MCIF_INHEADER; 2173 } 2174 2175 /* determine end of buffer; allow for short mailer lines */ 2176 buflim = &buf[sizeof buf - 1]; 2177 if (mci->mci_mailer->m_linelimit > 0 && 2178 mci->mci_mailer->m_linelimit < sizeof buf - 1) 2179 buflim = &buf[mci->mci_mailer->m_linelimit - 1]; 2180 2181 /* copy temp file to output with mapping */ 2182 ostate = OS_HEAD; 2183 bp = buf; 2184 pbp = peekbuf; 2185 while (!ferror(mci->mci_out)) 2186 { 2187 register char *xp; 2188 2189 if (pbp > peekbuf) 2190 c = *--pbp; 2191 else if ((c = fgetc(e->e_dfp)) == EOF) 2192 break; 2193 if (bitset(MCIF_7BIT, mci->mci_flags)) 2194 c &= 0x7f; 2195 switch (ostate) 2196 { 2197 case OS_HEAD: 2198 if (c != '\r' && c != '\n' && bp < buflim) 2199 { 2200 *bp++ = c; 2201 break; 2202 } 2203 2204 /* check beginning of line for special cases */ 2205 *bp = '\0'; 2206 pos = 0; 2207 padc = EOF; 2208 if (buf[0] == 'F' && 2209 bitnset(M_ESCFROM, mci->mci_mailer->m_flags) && 2210 strncmp(buf, "From ", 5) == 0) 2211 { 2212 padc = '>'; 2213 } 2214 if (buf[0] == '-' && buf[1] == '-' && 2215 separator != NULL) 2216 { 2217 /* possible separator */ 2218 int sl = strlen(separator); 2219 2220 if (strncmp(&buf[2], separator, sl) == 0) 2221 padc = ' '; 2222 } 2223 if (buf[0] == '.' && 2224 bitnset(M_XDOT, mci->mci_mailer->m_flags)) 2225 { 2226 padc = '.'; 2227 } 2228 2229 /* now copy out saved line */ 2230 if (TrafficLogFile != NULL) 2231 { 2232 fprintf(TrafficLogFile, "%05d >>> ", getpid()); 2233 if (padc != EOF) 2234 fputc(padc, TrafficLogFile); 2235 for (xp = buf; xp < bp; xp++) 2236 fputc(*xp, TrafficLogFile); 2237 if (c == '\n') 2238 fputs(mci->mci_mailer->m_eol, 2239 TrafficLogFile); 2240 } 2241 if (padc != EOF) 2242 { 2243 fputc(padc, mci->mci_out); 2244 pos++; 2245 } 2246 for (xp = buf; xp < bp; xp++) 2247 fputc(*xp, mci->mci_out); 2248 if (c == '\n') 2249 { 2250 fputs(mci->mci_mailer->m_eol, 2251 mci->mci_out); 2252 pos = 0; 2253 } 2254 else 2255 { 2256 pos += bp - buf; 2257 if (c != '\r') 2258 *pbp++ = c; 2259 } 2260 bp = buf; 2261 2262 /* determine next state */ 2263 if (c == '\n') 2264 ostate = OS_HEAD; 2265 else if (c == '\r') 2266 ostate = OS_CR; 2267 else 2268 ostate = OS_INLINE; 2269 continue; 2270 2271 case OS_CR: 2272 if (c == '\n') 2273 { 2274 /* got CRLF */ 2275 fputs(mci->mci_mailer->m_eol, mci->mci_out); 2276 if (TrafficLogFile != NULL) 2277 { 2278 fputs(mci->mci_mailer->m_eol, 2279 TrafficLogFile); 2280 } 2281 ostate = OS_HEAD; 2282 continue; 2283 } 2284 2285 /* had a naked carriage return */ 2286 *pbp++ = c; 2287 c = '\r'; 2288 goto putch; 2289 2290 case OS_INLINE: 2291 if (c == '\r') 2292 { 2293 ostate = OS_CR; 2294 continue; 2295 } 2296 putch: 2297 if (mci->mci_mailer->m_linelimit > 0 && 2298 pos > mci->mci_mailer->m_linelimit && 2299 c != '\n') 2300 { 2301 putc('!', mci->mci_out); 2302 fputs(mci->mci_mailer->m_eol, mci->mci_out); 2303 if (TrafficLogFile != NULL) 2304 { 2305 fprintf(TrafficLogFile, "!%s", 2306 mci->mci_mailer->m_eol); 2307 } 2308 ostate = OS_HEAD; 2309 *pbp++ = c; 2310 continue; 2311 } 2312 if (TrafficLogFile != NULL) 2313 fputc(c, TrafficLogFile); 2314 putc(c, mci->mci_out); 2315 pos++; 2316 ostate = c == '\n' ? OS_HEAD : OS_INLINE; 2317 break; 2318 } 2319 } 2320 } 2321 2322 if (ferror(e->e_dfp)) 2323 { 2324 syserr("putbody: %s: read error", e->e_df); 2325 ExitStat = EX_IOERR; 2326 } 2327 2328 endofmessage: 2329 /* some mailers want extra blank line at end of message */ 2330 if (bitnset(M_BLANKEND, mci->mci_mailer->m_flags) && 2331 buf[0] != '\0' && buf[0] != '\n') 2332 putline("", mci); 2333 2334 (void) fflush(mci->mci_out); 2335 if (ferror(mci->mci_out) && errno != EPIPE) 2336 { 2337 syserr("putbody: write error"); 2338 ExitStat = EX_IOERR; 2339 } 2340 errno = 0; 2341 } 2342 /* 2343 ** MAILFILE -- Send a message to a file. 2344 ** 2345 ** If the file has the setuid/setgid bits set, but NO execute 2346 ** bits, sendmail will try to become the owner of that file 2347 ** rather than the real user. Obviously, this only works if 2348 ** sendmail runs as root. 2349 ** 2350 ** This could be done as a subordinate mailer, except that it 2351 ** is used implicitly to save messages in ~/dead.letter. We 2352 ** view this as being sufficiently important as to include it 2353 ** here. For example, if the system is dying, we shouldn't have 2354 ** to create another process plus some pipes to save the message. 2355 ** 2356 ** Parameters: 2357 ** filename -- the name of the file to send to. 2358 ** ctladdr -- the controlling address header -- includes 2359 ** the userid/groupid to be when sending. 2360 ** 2361 ** Returns: 2362 ** The exit code associated with the operation. 2363 ** 2364 ** Side Effects: 2365 ** none. 2366 */ 2367 2368 mailfile(filename, ctladdr, e) 2369 char *filename; 2370 ADDRESS *ctladdr; 2371 register ENVELOPE *e; 2372 { 2373 register FILE *f; 2374 register int pid; 2375 int mode; 2376 2377 if (tTd(11, 1)) 2378 { 2379 printf("mailfile %s\n ctladdr=", filename); 2380 printaddr(ctladdr, FALSE); 2381 } 2382 2383 if (e->e_xfp != NULL) 2384 fflush(e->e_xfp); 2385 2386 /* 2387 ** Fork so we can change permissions here. 2388 ** Note that we MUST use fork, not vfork, because of 2389 ** the complications of calling subroutines, etc. 2390 */ 2391 2392 DOFORK(fork); 2393 2394 if (pid < 0) 2395 return (EX_OSERR); 2396 else if (pid == 0) 2397 { 2398 /* child -- actually write to file */ 2399 struct stat stb; 2400 MCI mcibuf; 2401 2402 (void) setsignal(SIGINT, SIG_DFL); 2403 (void) setsignal(SIGHUP, SIG_DFL); 2404 (void) setsignal(SIGTERM, SIG_DFL); 2405 (void) umask(OldUmask); 2406 2407 if (stat(filename, &stb) < 0) 2408 stb.st_mode = FileMode; 2409 mode = stb.st_mode; 2410 2411 /* limit the errors to those actually caused in the child */ 2412 errno = 0; 2413 ExitStat = EX_OK; 2414 2415 if (bitset(0111, stb.st_mode)) 2416 exit(EX_CANTCREAT); 2417 if (ctladdr != NULL) 2418 { 2419 /* ignore setuid and setgid bits */ 2420 mode &= ~(S_ISGID|S_ISUID); 2421 } 2422 2423 /* we have to open the dfile BEFORE setuid */ 2424 if (e->e_dfp == NULL && e->e_df != NULL) 2425 { 2426 e->e_dfp = fopen(e->e_df, "r"); 2427 if (e->e_dfp == NULL) 2428 { 2429 syserr("mailfile: Cannot open %s for %s from %s", 2430 e->e_df, e->e_to, e->e_from.q_paddr); 2431 } 2432 } 2433 2434 if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 2435 { 2436 if (ctladdr == NULL || ctladdr->q_uid == 0) 2437 { 2438 (void) initgroups(DefUser, DefGid); 2439 } 2440 else 2441 { 2442 (void) initgroups(ctladdr->q_ruser ? 2443 ctladdr->q_ruser : ctladdr->q_user, 2444 ctladdr->q_gid); 2445 } 2446 } 2447 if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 2448 { 2449 if (ctladdr == NULL || ctladdr->q_uid == 0) 2450 (void) setuid(DefUid); 2451 else 2452 (void) setuid(ctladdr->q_uid); 2453 } 2454 FileName = filename; 2455 LineNumber = 0; 2456 f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode); 2457 if (f == NULL) 2458 { 2459 message("554 cannot open: %s", errstring(errno)); 2460 exit(EX_CANTCREAT); 2461 } 2462 2463 bzero(&mcibuf, sizeof mcibuf); 2464 mcibuf.mci_mailer = FileMailer; 2465 mcibuf.mci_out = f; 2466 if (bitnset(M_7BITS, FileMailer->m_flags)) 2467 mcibuf.mci_flags |= MCIF_7BIT; 2468 2469 putfromline(&mcibuf, e); 2470 (*e->e_puthdr)(&mcibuf, e->e_header, e); 2471 (*e->e_putbody)(&mcibuf, e, NULL); 2472 putline("\n", &mcibuf); 2473 if (ferror(f)) 2474 { 2475 message("451 I/O error: %s", errstring(errno)); 2476 setstat(EX_IOERR); 2477 } 2478 (void) xfclose(f, "mailfile", filename); 2479 (void) fflush(stdout); 2480 2481 /* reset ISUID & ISGID bits for paranoid systems */ 2482 (void) chmod(filename, (int) stb.st_mode); 2483 exit(ExitStat); 2484 /*NOTREACHED*/ 2485 } 2486 else 2487 { 2488 /* parent -- wait for exit status */ 2489 int st; 2490 2491 st = waitfor(pid); 2492 if (WIFEXITED(st)) 2493 return (WEXITSTATUS(st)); 2494 else 2495 { 2496 syserr("child died on signal %d", st); 2497 return (EX_UNAVAILABLE); 2498 } 2499 /*NOTREACHED*/ 2500 } 2501 } 2502 /* 2503 ** HOSTSIGNATURE -- return the "signature" for a host. 2504 ** 2505 ** The signature describes how we are going to send this -- it 2506 ** can be just the hostname (for non-Internet hosts) or can be 2507 ** an ordered list of MX hosts. 2508 ** 2509 ** Parameters: 2510 ** m -- the mailer describing this host. 2511 ** host -- the host name. 2512 ** e -- the current envelope. 2513 ** 2514 ** Returns: 2515 ** The signature for this host. 2516 ** 2517 ** Side Effects: 2518 ** Can tweak the symbol table. 2519 */ 2520 2521 char * 2522 hostsignature(m, host, e) 2523 register MAILER *m; 2524 char *host; 2525 ENVELOPE *e; 2526 { 2527 register char *p; 2528 register STAB *s; 2529 int i; 2530 int len; 2531 #if NAMED_BIND 2532 int nmx; 2533 auto int rcode; 2534 char *hp; 2535 char *endp; 2536 int oldoptions; 2537 char *mxhosts[MAXMXHOSTS + 1]; 2538 #endif 2539 2540 /* 2541 ** Check to see if this uses IPC -- if not, it can't have MX records. 2542 */ 2543 2544 p = m->m_mailer; 2545 if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 2546 { 2547 /* just an ordinary mailer */ 2548 return host; 2549 } 2550 2551 /* 2552 ** Look it up in the symbol table. 2553 */ 2554 2555 s = stab(host, ST_HOSTSIG, ST_ENTER); 2556 if (s->s_hostsig != NULL) 2557 return s->s_hostsig; 2558 2559 /* 2560 ** Not already there -- create a signature. 2561 */ 2562 2563 #if NAMED_BIND 2564 if (ConfigLevel < 2) 2565 { 2566 oldoptions = _res.options; 2567 _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 2568 } 2569 2570 for (hp = host; hp != NULL; hp = endp) 2571 { 2572 endp = strchr(hp, ':'); 2573 if (endp != NULL) 2574 *endp = '\0'; 2575 2576 nmx = getmxrr(hp, mxhosts, TRUE, &rcode); 2577 2578 if (nmx <= 0) 2579 { 2580 register MCI *mci; 2581 2582 /* update the connection info for this host */ 2583 mci = mci_get(hp, m); 2584 mci->mci_exitstat = rcode; 2585 mci->mci_errno = errno; 2586 #if NAMED_BIND 2587 mci->mci_herrno = h_errno; 2588 #endif 2589 2590 /* and return the original host name as the signature */ 2591 nmx = 1; 2592 mxhosts[0] = hp; 2593 } 2594 2595 len = 0; 2596 for (i = 0; i < nmx; i++) 2597 { 2598 len += strlen(mxhosts[i]) + 1; 2599 } 2600 if (s->s_hostsig != NULL) 2601 len += strlen(s->s_hostsig) + 1; 2602 p = xalloc(len); 2603 if (s->s_hostsig != NULL) 2604 { 2605 (void) strcpy(p, s->s_hostsig); 2606 free(s->s_hostsig); 2607 s->s_hostsig = p; 2608 p += strlen(p); 2609 *p++ = ':'; 2610 } 2611 else 2612 s->s_hostsig = p; 2613 for (i = 0; i < nmx; i++) 2614 { 2615 if (i != 0) 2616 *p++ = ':'; 2617 strcpy(p, mxhosts[i]); 2618 p += strlen(p); 2619 } 2620 if (endp != NULL) 2621 *endp++ = ':'; 2622 } 2623 makelower(s->s_hostsig); 2624 if (ConfigLevel < 2) 2625 _res.options = oldoptions; 2626 #else 2627 /* not using BIND -- the signature is just the host name */ 2628 s->s_hostsig = host; 2629 #endif 2630 if (tTd(17, 1)) 2631 printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 2632 return s->s_hostsig; 2633 } 2634 /* 2635 ** SETSTATUS -- set the address status for return messages 2636 ** 2637 ** Parameters: 2638 ** a -- the address to set. 2639 ** msg -- the text of the message, which must be in standard 2640 ** SMTP form (3 digits, a space, and a message). 2641 ** 2642 ** Returns: 2643 ** none. 2644 */ 2645 2646 setstatus(a, msg) 2647 register ADDRESS *a; 2648 char *msg; 2649 { 2650 char buf[MAXLINE]; 2651 2652 if (a->q_status != NULL) 2653 free(a->q_status); 2654 if (strlen(msg) > 4) 2655 { 2656 register char *p, *q; 2657 int parenlev = 0; 2658 2659 strncpy(buf, msg, 4); 2660 p = &buf[4]; 2661 *p++ = '('; 2662 for (q = &msg[4]; *q != NULL; q++) 2663 { 2664 switch (*q) 2665 { 2666 case '(': 2667 parenlev++; 2668 break; 2669 2670 case ')': 2671 if (parenlev > 0) 2672 parenlev--; 2673 else 2674 *p++ = '\\'; 2675 break; 2676 } 2677 *p++ = *q; 2678 } 2679 while (parenlev-- >= 0) 2680 *p++ = ')'; 2681 *p++ = '\0'; 2682 msg = buf; 2683 } 2684 a->q_status = newstr(msg); 2685 } 2686