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