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