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.149 (Berkeley) 04/25/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 dofork() 600 { 601 register int pid = -1; 602 603 DOFORK(fork); 604 return (pid); 605 } 606 /* 607 ** DELIVER -- Deliver a message to a list of addresses. 608 ** 609 ** This routine delivers to everyone on the same host as the 610 ** user on the head of the list. It is clever about mailers 611 ** that don't handle multiple users. It is NOT guaranteed 612 ** that it will deliver to all these addresses however -- so 613 ** deliver should be called once for each address on the 614 ** list. 615 ** 616 ** Parameters: 617 ** e -- the envelope to deliver. 618 ** firstto -- head of the address list to deliver to. 619 ** 620 ** Returns: 621 ** zero -- successfully delivered. 622 ** else -- some failure, see ExitStat for more info. 623 ** 624 ** Side Effects: 625 ** The standard input is passed off to someone. 626 */ 627 628 int 629 deliver(e, firstto) 630 register ENVELOPE *e; 631 ADDRESS *firstto; 632 { 633 char *host; /* host being sent to */ 634 char *user; /* user being sent to */ 635 char **pvp; 636 register char **mvp; 637 register char *p; 638 register MAILER *m; /* mailer for this recipient */ 639 ADDRESS *ctladdr; 640 register MCI *mci; 641 register ADDRESS *to = firstto; 642 bool clever = FALSE; /* running user smtp to this mailer */ 643 ADDRESS *tochain = NULL; /* users chain in this mailer call */ 644 int rcode; /* response code */ 645 char *firstsig; /* signature of firstto */ 646 int pid = -1; 647 char *curhost; 648 time_t xstart; 649 int mpvect[2]; 650 int rpvect[2]; 651 char *pv[MAXPV+1]; 652 char tobuf[TOBUFSIZE]; /* text line of to people */ 653 char buf[MAXNAME + 1]; 654 char rpathbuf[MAXNAME + 1]; /* translated return path */ 655 extern int checkcompat(); 656 657 errno = 0; 658 if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags)) 659 return (0); 660 661 #if NAMED_BIND 662 /* unless interactive, try twice, over a minute */ 663 if (OpMode == MD_DAEMON || OpMode == MD_SMTP) 664 { 665 _res.retrans = 30; 666 _res.retry = 2; 667 } 668 #endif 669 670 m = to->q_mailer; 671 host = to->q_host; 672 CurEnv = e; /* just in case */ 673 e->e_statmsg = NULL; 674 SmtpError[0] = '\0'; 675 xstart = curtime(); 676 677 if (tTd(10, 1)) 678 printf("\n--deliver, id=%s, mailer=%s, host=`%s', first user=`%s'\n", 679 e->e_id, m->m_name, host, to->q_user); 680 if (tTd(10, 100)) 681 printopenfds(FALSE); 682 683 /* 684 ** Do initial argv setup. 685 ** Insert the mailer name. Notice that $x expansion is 686 ** NOT done on the mailer name. Then, if the mailer has 687 ** a picky -f flag, we insert it as appropriate. This 688 ** code does not check for 'pv' overflow; this places a 689 ** manifest lower limit of 4 for MAXPV. 690 ** The from address rewrite is expected to make 691 ** the address relative to the other end. 692 */ 693 694 /* rewrite from address, using rewriting rules */ 695 rcode = EX_OK; 696 if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags)) 697 p = e->e_sender; 698 else 699 p = e->e_from.q_paddr; 700 (void) strcpy(rpathbuf, remotename(p, m, 701 RF_SENDERADDR|RF_CANONICAL, 702 &rcode, e)); 703 define('g', rpathbuf, e); /* translated return path */ 704 define('h', host, e); /* to host */ 705 Errors = 0; 706 pvp = pv; 707 *pvp++ = m->m_argv[0]; 708 709 /* insert -f or -r flag as appropriate */ 710 if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 711 { 712 if (bitnset(M_FOPT, m->m_flags)) 713 *pvp++ = "-f"; 714 else 715 *pvp++ = "-r"; 716 *pvp++ = newstr(rpathbuf); 717 } 718 719 /* 720 ** Append the other fixed parts of the argv. These run 721 ** up to the first entry containing "$u". There can only 722 ** be one of these, and there are only a few more slots 723 ** in the pv after it. 724 */ 725 726 for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 727 { 728 /* can't use strchr here because of sign extension problems */ 729 while (*p != '\0') 730 { 731 if ((*p++ & 0377) == MACROEXPAND) 732 { 733 if (*p == 'u') 734 break; 735 } 736 } 737 738 if (*p != '\0') 739 break; 740 741 /* this entry is safe -- go ahead and process it */ 742 expand(*mvp, buf, sizeof buf, e); 743 *pvp++ = newstr(buf); 744 if (pvp >= &pv[MAXPV - 3]) 745 { 746 syserr("554 Too many parameters to %s before $u", pv[0]); 747 return (-1); 748 } 749 } 750 751 /* 752 ** If we have no substitution for the user name in the argument 753 ** list, we know that we must supply the names otherwise -- and 754 ** SMTP is the answer!! 755 */ 756 757 if (*mvp == NULL) 758 { 759 /* running SMTP */ 760 # ifdef SMTP 761 clever = TRUE; 762 *pvp = NULL; 763 # else /* SMTP */ 764 /* oops! we don't implement SMTP */ 765 syserr("554 SMTP style mailer not implemented"); 766 return (EX_SOFTWARE); 767 # endif /* SMTP */ 768 } 769 770 /* 771 ** At this point *mvp points to the argument with $u. We 772 ** run through our address list and append all the addresses 773 ** we can. If we run out of space, do not fret! We can 774 ** always send another copy later. 775 */ 776 777 tobuf[0] = '\0'; 778 e->e_to = tobuf; 779 ctladdr = NULL; 780 firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e); 781 for (; to != NULL; to = to->q_next) 782 { 783 /* avoid sending multiple recipients to dumb mailers */ 784 if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 785 break; 786 787 /* if already sent or not for this host, don't send */ 788 if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 789 to->q_mailer != firstto->q_mailer || 790 strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0) 791 continue; 792 793 /* avoid overflowing tobuf */ 794 if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 795 break; 796 797 if (tTd(10, 1)) 798 { 799 printf("\nsend to "); 800 printaddr(to, FALSE); 801 } 802 803 /* compute effective uid/gid when sending */ 804 if (bitnset(M_RUNASRCPT, to->q_mailer->m_flags)) 805 ctladdr = getctladdr(to); 806 807 if (tTd(10, 2)) 808 { 809 printf("ctladdr="); 810 printaddr(ctladdr, FALSE); 811 } 812 813 user = to->q_user; 814 e->e_to = to->q_paddr; 815 if (tTd(10, 5)) 816 { 817 printf("deliver: QDONTSEND "); 818 printaddr(to, FALSE); 819 } 820 to->q_flags |= QDONTSEND; 821 822 /* 823 ** Check to see that these people are allowed to 824 ** talk to each other. 825 */ 826 827 if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 828 { 829 e->e_flags |= EF_NO_BODY_RETN; 830 mci->mci_status = "5.2.3"; 831 usrerr("552 Message is too large; %ld bytes max", m->m_maxsize); 832 giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, xstart, e); 833 continue; 834 } 835 #if NAMED_BIND 836 h_errno = 0; 837 #endif 838 rcode = checkcompat(to, e); 839 if (rcode != EX_OK) 840 { 841 markfailure(e, to, NULL, rcode); 842 giveresponse(rcode, m, NULL, ctladdr, xstart, e); 843 continue; 844 } 845 846 /* 847 ** Strip quote bits from names if the mailer is dumb 848 ** about them. 849 */ 850 851 if (bitnset(M_STRIPQ, m->m_flags)) 852 { 853 stripquotes(user); 854 stripquotes(host); 855 } 856 857 /* hack attack -- delivermail compatibility */ 858 if (m == ProgMailer && *user == '|') 859 user++; 860 861 /* 862 ** If an error message has already been given, don't 863 ** bother to send to this address. 864 ** 865 ** >>>>>>>>>> This clause assumes that the local mailer 866 ** >> NOTE >> cannot do any further aliasing; that 867 ** >>>>>>>>>> function is subsumed by sendmail. 868 */ 869 870 if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 871 continue; 872 873 /* save statistics.... */ 874 markstats(e, to); 875 876 /* 877 ** See if this user name is "special". 878 ** If the user name has a slash in it, assume that this 879 ** is a file -- send it off without further ado. Note 880 ** that this type of addresses is not processed along 881 ** with the others, so we fudge on the To person. 882 */ 883 884 if (strcmp(m->m_mailer, "[FILE]") == 0) 885 { 886 rcode = mailfile(user, ctladdr, e); 887 giveresponse(rcode, m, NULL, ctladdr, xstart, e); 888 e->e_nsent++; 889 if (rcode == EX_OK) 890 { 891 to->q_flags |= QSENT; 892 if (bitnset(M_LOCALMAILER, m->m_flags) && 893 (e->e_receiptto != NULL || 894 bitset(QPINGONSUCCESS, to->q_flags))) 895 { 896 to->q_flags |= QDELIVERED; 897 to->q_status = "2.1.5"; 898 fprintf(e->e_xfp, "%s... Successfully delivered\n", 899 to->q_paddr); 900 } 901 } 902 to->q_statdate = curtime(); 903 continue; 904 } 905 906 /* 907 ** Address is verified -- add this user to mailer 908 ** argv, and add it to the print list of recipients. 909 */ 910 911 /* link together the chain of recipients */ 912 to->q_tchain = tochain; 913 tochain = to; 914 915 /* create list of users for error messages */ 916 (void) strcat(tobuf, ","); 917 (void) strcat(tobuf, to->q_paddr); 918 define('u', user, e); /* to user */ 919 p = to->q_home; 920 if (p == NULL && ctladdr != NULL) 921 p = ctladdr->q_home; 922 define('z', p, e); /* user's home */ 923 924 /* 925 ** Expand out this user into argument list. 926 */ 927 928 if (!clever) 929 { 930 expand(*mvp, buf, sizeof buf, e); 931 *pvp++ = newstr(buf); 932 if (pvp >= &pv[MAXPV - 2]) 933 { 934 /* allow some space for trailing parms */ 935 break; 936 } 937 } 938 } 939 940 /* see if any addresses still exist */ 941 if (tobuf[0] == '\0') 942 { 943 define('g', (char *) NULL, e); 944 return (0); 945 } 946 947 /* print out messages as full list */ 948 e->e_to = tobuf + 1; 949 950 /* 951 ** Fill out any parameters after the $u parameter. 952 */ 953 954 while (!clever && *++mvp != NULL) 955 { 956 expand(*mvp, buf, sizeof buf, e); 957 *pvp++ = newstr(buf); 958 if (pvp >= &pv[MAXPV]) 959 syserr("554 deliver: pv overflow after $u for %s", pv[0]); 960 } 961 *pvp++ = NULL; 962 963 /* 964 ** Call the mailer. 965 ** The argument vector gets built, pipes 966 ** are created as necessary, and we fork & exec as 967 ** appropriate. 968 ** If we are running SMTP, we just need to clean up. 969 */ 970 971 /*XXX this seems a bit wierd */ 972 if (ctladdr == NULL && m != ProgMailer && 973 bitset(QGOODUID, e->e_from.q_flags)) 974 ctladdr = &e->e_from; 975 976 #if NAMED_BIND 977 if (ConfigLevel < 2) 978 _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 979 #endif 980 981 if (tTd(11, 1)) 982 { 983 printf("openmailer:"); 984 printav(pv); 985 } 986 errno = 0; 987 #if NAMED_BIND 988 h_errno = 0; 989 #endif 990 991 CurHostName = NULL; 992 993 /* 994 ** Deal with the special case of mail handled through an IPC 995 ** connection. 996 ** In this case we don't actually fork. We must be 997 ** running SMTP for this to work. We will return a 998 ** zero pid to indicate that we are running IPC. 999 ** We also handle a debug version that just talks to stdin/out. 1000 */ 1001 1002 curhost = NULL; 1003 SmtpPhase = NULL; 1004 mci = NULL; 1005 1006 #ifdef XDEBUG 1007 { 1008 char wbuf[MAXLINE]; 1009 1010 /* make absolutely certain 0, 1, and 2 are in use */ 1011 sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name); 1012 checkfd012(wbuf); 1013 } 1014 #endif 1015 1016 /* check for 8-bit available */ 1017 if (bitset(EF_HAS8BIT, e->e_flags) && 1018 bitnset(M_7BITS, m->m_flags) && 1019 !bitset(MM_MIME8BIT, MimeMode)) 1020 { 1021 usrerr("554 Cannot send 8-bit data to 7-bit destination"); 1022 rcode = EX_DATAERR; 1023 e->e_status = "5.6.3"; 1024 goto give_up; 1025 } 1026 1027 /* check for Local Person Communication -- not for mortals!!! */ 1028 if (strcmp(m->m_mailer, "[LPC]") == 0) 1029 { 1030 mci = (MCI *) xalloc(sizeof *mci); 1031 bzero((char *) mci, sizeof *mci); 1032 mci->mci_in = stdin; 1033 mci->mci_out = stdout; 1034 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 1035 mci->mci_mailer = m; 1036 } 1037 else if (strcmp(m->m_mailer, "[IPC]") == 0 || 1038 strcmp(m->m_mailer, "[TCP]") == 0) 1039 { 1040 #ifdef DAEMON 1041 register int i; 1042 register u_short port = 0; 1043 1044 if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0') 1045 { 1046 syserr("null host name for %s mailer", m->m_mailer); 1047 rcode = EX_CONFIG; 1048 goto give_up; 1049 } 1050 1051 CurHostName = pv[1]; 1052 curhost = hostsignature(m, pv[1], e); 1053 1054 if (curhost == NULL || curhost[0] == '\0') 1055 { 1056 syserr("null host signature for %s", pv[1]); 1057 rcode = EX_CONFIG; 1058 goto give_up; 1059 } 1060 1061 if (!clever) 1062 { 1063 syserr("554 non-clever IPC"); 1064 rcode = EX_CONFIG; 1065 goto give_up; 1066 } 1067 if (pv[2] != NULL) 1068 port = atoi(pv[2]); 1069 tryhost: 1070 while (*curhost != '\0') 1071 { 1072 register char *p; 1073 static char hostbuf[MAXNAME + 1]; 1074 1075 /* pull the next host from the signature */ 1076 p = strchr(curhost, ':'); 1077 if (p == NULL) 1078 p = &curhost[strlen(curhost)]; 1079 if (p == curhost) 1080 { 1081 syserr("deliver: null host name in signature"); 1082 curhost++; 1083 continue; 1084 } 1085 strncpy(hostbuf, curhost, p - curhost); 1086 hostbuf[p - curhost] = '\0'; 1087 if (*p != '\0') 1088 p++; 1089 curhost = p; 1090 1091 /* see if we already know that this host is fried */ 1092 CurHostName = hostbuf; 1093 mci = mci_get(hostbuf, m); 1094 if (mci->mci_state != MCIS_CLOSED) 1095 { 1096 if (tTd(11, 1)) 1097 { 1098 printf("openmailer: "); 1099 mci_dump(mci, FALSE); 1100 } 1101 CurHostName = mci->mci_host; 1102 message("Using cached connection to %s via %s...", 1103 hostbuf, m->m_name); 1104 break; 1105 } 1106 mci->mci_mailer = m; 1107 if (mci->mci_exitstat != EX_OK) 1108 continue; 1109 1110 /* try the connection */ 1111 setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 1112 message("Connecting to %s via %s...", 1113 hostbuf, m->m_name); 1114 i = makeconnection(hostbuf, port, mci, 1115 bitnset(M_SECURE_PORT, m->m_flags)); 1116 mci->mci_exitstat = i; 1117 mci->mci_errno = errno; 1118 #if NAMED_BIND 1119 mci->mci_herrno = h_errno; 1120 #endif 1121 if (i == EX_OK) 1122 { 1123 mci->mci_state = MCIS_OPENING; 1124 mci_cache(mci); 1125 if (TrafficLogFile != NULL) 1126 fprintf(TrafficLogFile, "%05d == CONNECT %s\n", 1127 getpid(), hostbuf); 1128 break; 1129 } 1130 else if (tTd(11, 1)) 1131 printf("openmailer: makeconnection => stat=%d, errno=%d\n", 1132 i, errno); 1133 1134 /* enter status of this host */ 1135 setstat(i); 1136 1137 /* should print some message here for -v mode */ 1138 } 1139 if (mci == NULL) 1140 { 1141 syserr("deliver: no host name"); 1142 rcode = EX_OSERR; 1143 goto give_up; 1144 } 1145 mci->mci_pid = 0; 1146 #else /* no DAEMON */ 1147 syserr("554 openmailer: no IPC"); 1148 if (tTd(11, 1)) 1149 printf("openmailer: NULL\n"); 1150 rcode = EX_UNAVAILABLE; 1151 goto give_up; 1152 #endif /* DAEMON */ 1153 } 1154 else 1155 { 1156 /* flush any expired connections */ 1157 (void) mci_scan(NULL); 1158 1159 /* announce the connection to verbose listeners */ 1160 if (host == NULL || host[0] == '\0') 1161 message("Connecting to %s...", m->m_name); 1162 else 1163 message("Connecting to %s via %s...", host, m->m_name); 1164 if (TrafficLogFile != NULL) 1165 { 1166 char **av; 1167 1168 fprintf(TrafficLogFile, "%05d === EXEC", getpid()); 1169 for (av = pv; *av != NULL; av++) 1170 fprintf(TrafficLogFile, " %s", *av); 1171 fprintf(TrafficLogFile, "\n"); 1172 } 1173 1174 /* create a pipe to shove the mail through */ 1175 if (pipe(mpvect) < 0) 1176 { 1177 syserr("%s... openmailer(%s): pipe (to mailer)", 1178 e->e_to, m->m_name); 1179 if (tTd(11, 1)) 1180 printf("openmailer: NULL\n"); 1181 rcode = EX_OSERR; 1182 goto give_up; 1183 } 1184 1185 /* if this mailer speaks smtp, create a return pipe */ 1186 if (clever && pipe(rpvect) < 0) 1187 { 1188 syserr("%s... openmailer(%s): pipe (from mailer)", 1189 e->e_to, m->m_name); 1190 (void) close(mpvect[0]); 1191 (void) close(mpvect[1]); 1192 if (tTd(11, 1)) 1193 printf("openmailer: NULL\n"); 1194 rcode = EX_OSERR; 1195 goto give_up; 1196 } 1197 1198 /* 1199 ** Actually fork the mailer process. 1200 ** DOFORK is clever about retrying. 1201 ** 1202 ** Dispose of SIGCHLD signal catchers that may be laying 1203 ** around so that endmail will get it. 1204 */ 1205 1206 if (e->e_xfp != NULL) 1207 (void) fflush(e->e_xfp); /* for debugging */ 1208 (void) fflush(stdout); 1209 # ifdef SIGCHLD 1210 (void) setsignal(SIGCHLD, SIG_DFL); 1211 # endif /* SIGCHLD */ 1212 DOFORK(FORK); 1213 /* pid is set by DOFORK */ 1214 if (pid < 0) 1215 { 1216 /* failure */ 1217 syserr("%s... openmailer(%s): cannot fork", 1218 e->e_to, m->m_name); 1219 (void) close(mpvect[0]); 1220 (void) close(mpvect[1]); 1221 if (clever) 1222 { 1223 (void) close(rpvect[0]); 1224 (void) close(rpvect[1]); 1225 } 1226 if (tTd(11, 1)) 1227 printf("openmailer: NULL\n"); 1228 rcode = EX_OSERR; 1229 goto give_up; 1230 } 1231 else if (pid == 0) 1232 { 1233 int i; 1234 int saveerrno; 1235 char **ep; 1236 char *env[MAXUSERENVIRON]; 1237 extern char **environ; 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 /* tweak niceness */ 1249 if (m->m_nice != 0) 1250 nice(m->m_nice); 1251 1252 /* reset user and group */ 1253 if (bitnset(M_SPECIFIC_UID, m->m_flags)) 1254 { 1255 (void) setgid(m->m_gid); 1256 (void) setuid(m->m_uid); 1257 } 1258 else if (ctladdr != NULL && ctladdr->q_uid != 0) 1259 { 1260 (void) initgroups(ctladdr->q_ruser? 1261 ctladdr->q_ruser: ctladdr->q_user, 1262 ctladdr->q_gid); 1263 (void) setgid(ctladdr->q_gid); 1264 (void) setuid(ctladdr->q_uid); 1265 } 1266 else 1267 { 1268 (void) initgroups(DefUser, DefGid); 1269 if (m->m_gid == 0) 1270 (void) setgid(DefGid); 1271 else 1272 (void) setgid(m->m_gid); 1273 if (m->m_uid == 0) 1274 (void) setuid(DefUid); 1275 else 1276 (void) setuid(m->m_uid); 1277 } 1278 1279 if (tTd(11, 2)) 1280 printf("openmailer: running as r/euid=%d/%d\n", 1281 getuid(), geteuid()); 1282 1283 /* move into some "safe" directory */ 1284 if (m->m_execdir != NULL) 1285 { 1286 char *p, *q; 1287 char buf[MAXLINE + 1]; 1288 1289 for (p = m->m_execdir; p != NULL; p = q) 1290 { 1291 q = strchr(p, ':'); 1292 if (q != NULL) 1293 *q = '\0'; 1294 expand(p, buf, sizeof buf, e); 1295 if (q != NULL) 1296 *q++ = ':'; 1297 if (tTd(11, 20)) 1298 printf("openmailer: trydir %s\n", 1299 buf); 1300 if (buf[0] != '\0' && chdir(buf) >= 0) 1301 break; 1302 } 1303 } 1304 1305 /* arrange to filter std & diag output of command */ 1306 if (clever) 1307 { 1308 (void) close(rpvect[0]); 1309 if (dup2(rpvect[1], STDOUT_FILENO) < 0) 1310 { 1311 syserr("%s... openmailer(%s): cannot dup pipe %d for stdout", 1312 e->e_to, m->m_name, rpvect[1]); 1313 _exit(EX_OSERR); 1314 } 1315 (void) close(rpvect[1]); 1316 } 1317 else if (OpMode == MD_SMTP || OpMode == MD_DAEMON || 1318 HoldErrs || DisConnected) 1319 { 1320 /* put mailer output in transcript */ 1321 if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0) 1322 { 1323 syserr("%s... openmailer(%s): cannot dup xscript %d for stdout", 1324 e->e_to, m->m_name, 1325 fileno(e->e_xfp)); 1326 _exit(EX_OSERR); 1327 } 1328 } 1329 if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) 1330 { 1331 syserr("%s... openmailer(%s): cannot dup stdout for stderr", 1332 e->e_to, m->m_name); 1333 _exit(EX_OSERR); 1334 } 1335 1336 /* arrange to get standard input */ 1337 (void) close(mpvect[1]); 1338 if (dup2(mpvect[0], STDIN_FILENO) < 0) 1339 { 1340 syserr("%s... openmailer(%s): cannot dup pipe %d for stdin", 1341 e->e_to, m->m_name, mpvect[0]); 1342 _exit(EX_OSERR); 1343 } 1344 (void) close(mpvect[0]); 1345 1346 /* arrange for all the files to be closed */ 1347 for (i = 3; i < DtableSize; i++) 1348 { 1349 register int j; 1350 1351 if ((j = fcntl(i, F_GETFD, 0)) != -1) 1352 (void) fcntl(i, F_SETFD, j | 1); 1353 } 1354 1355 /* 1356 ** Set up the mailer environment 1357 ** _FORCE_MAIL_LOCAL_ is DG-UX equiv of -d flag. 1358 ** TZ is timezone information. 1359 ** SYSTYPE is Apollo software sys type (required). 1360 ** ISP is Apollo hardware system type (required). 1361 */ 1362 1363 i = 0; 1364 env[i++] = "AGENT=sendmail"; 1365 env[i++] = "_FORCE_MAIL_LOCAL_=yes"; 1366 for (ep = environ; *ep != NULL; ep++) 1367 { 1368 if (strncmp(*ep, "TZ=", 3) == 0 || 1369 strncmp(*ep, "ISP=", 4) == 0 || 1370 strncmp(*ep, "SYSTYPE=", 8) == 0) 1371 env[i++] = *ep; 1372 } 1373 env[i] = NULL; 1374 1375 /* run disconnected from terminal */ 1376 (void) setsid(); 1377 1378 /* try to execute the mailer */ 1379 execve(m->m_mailer, (ARGV_T) pv, (ARGV_T) env); 1380 saveerrno = errno; 1381 syserr("Cannot exec %s", m->m_mailer); 1382 if (bitnset(M_LOCALMAILER, m->m_flags) || 1383 transienterror(saveerrno)) 1384 _exit(EX_OSERR); 1385 _exit(EX_UNAVAILABLE); 1386 } 1387 1388 /* 1389 ** Set up return value. 1390 */ 1391 1392 mci = (MCI *) xalloc(sizeof *mci); 1393 bzero((char *) mci, sizeof *mci); 1394 mci->mci_mailer = m; 1395 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 1396 mci->mci_pid = pid; 1397 (void) close(mpvect[0]); 1398 mci->mci_out = fdopen(mpvect[1], "w"); 1399 if (mci->mci_out == NULL) 1400 { 1401 syserr("deliver: cannot create mailer output channel, fd=%d", 1402 mpvect[1]); 1403 (void) close(mpvect[1]); 1404 if (clever) 1405 { 1406 (void) close(rpvect[0]); 1407 (void) close(rpvect[1]); 1408 } 1409 rcode = EX_OSERR; 1410 goto give_up; 1411 } 1412 if (clever) 1413 { 1414 (void) close(rpvect[1]); 1415 mci->mci_in = fdopen(rpvect[0], "r"); 1416 if (mci->mci_in == NULL) 1417 { 1418 syserr("deliver: cannot create mailer input channel, fd=%d", 1419 mpvect[1]); 1420 (void) close(rpvect[0]); 1421 fclose(mci->mci_out); 1422 mci->mci_out = NULL; 1423 rcode = EX_OSERR; 1424 goto give_up; 1425 } 1426 } 1427 else 1428 { 1429 mci->mci_flags |= MCIF_TEMP; 1430 mci->mci_in = NULL; 1431 } 1432 } 1433 1434 /* 1435 ** If we are in SMTP opening state, send initial protocol. 1436 */ 1437 1438 if (clever && mci->mci_state != MCIS_CLOSED) 1439 { 1440 smtpinit(m, mci, e); 1441 } 1442 1443 if (bitset(EF_HAS8BIT, e->e_flags) && bitnset(M_7BITS, m->m_flags)) 1444 mci->mci_flags |= MCIF_CVT8TO7; 1445 else 1446 mci->mci_flags &= ~MCIF_CVT8TO7; 1447 1448 if (tTd(11, 1)) 1449 { 1450 printf("openmailer: "); 1451 mci_dump(mci, FALSE); 1452 } 1453 1454 if (mci->mci_state != MCIS_OPEN) 1455 { 1456 /* couldn't open the mailer */ 1457 rcode = mci->mci_exitstat; 1458 errno = mci->mci_errno; 1459 #if NAMED_BIND 1460 h_errno = mci->mci_herrno; 1461 #endif 1462 if (rcode == EX_OK) 1463 { 1464 /* shouldn't happen */ 1465 syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 1466 rcode, mci->mci_state, firstsig); 1467 rcode = EX_SOFTWARE; 1468 } 1469 else if (curhost != NULL && *curhost != '\0') 1470 { 1471 /* try next MX site */ 1472 goto tryhost; 1473 } 1474 } 1475 else if (!clever) 1476 { 1477 /* 1478 ** Format and send message. 1479 */ 1480 1481 putfromline(mci, e); 1482 (*e->e_puthdr)(mci, e->e_header, e); 1483 (*e->e_putbody)(mci, e, NULL); 1484 1485 /* get the exit status */ 1486 rcode = endmailer(mci, e, pv); 1487 } 1488 else 1489 #ifdef SMTP 1490 { 1491 /* 1492 ** Send the MAIL FROM: protocol 1493 */ 1494 1495 rcode = smtpmailfrom(m, mci, e); 1496 if (rcode == EX_OK) 1497 { 1498 register char *t = tobuf; 1499 register int i; 1500 1501 /* send the recipient list */ 1502 tobuf[0] = '\0'; 1503 for (to = tochain; to != NULL; to = to->q_tchain) 1504 { 1505 e->e_to = to->q_paddr; 1506 if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 1507 { 1508 markfailure(e, to, mci, i); 1509 giveresponse(i, m, mci, ctladdr, xstart, e); 1510 } 1511 else 1512 { 1513 *t++ = ','; 1514 for (p = to->q_paddr; *p; *t++ = *p++) 1515 continue; 1516 *t = '\0'; 1517 } 1518 } 1519 1520 /* now send the data */ 1521 if (tobuf[0] == '\0') 1522 { 1523 rcode = EX_OK; 1524 e->e_to = NULL; 1525 if (bitset(MCIF_CACHED, mci->mci_flags)) 1526 smtprset(m, mci, e); 1527 } 1528 else 1529 { 1530 e->e_to = tobuf + 1; 1531 rcode = smtpdata(m, mci, e); 1532 } 1533 1534 /* now close the connection */ 1535 if (!bitset(MCIF_CACHED, mci->mci_flags)) 1536 smtpquit(m, mci, e); 1537 } 1538 if (rcode != EX_OK && curhost != NULL && *curhost != '\0') 1539 { 1540 /* try next MX site */ 1541 goto tryhost; 1542 } 1543 } 1544 #else /* not SMTP */ 1545 { 1546 syserr("554 deliver: need SMTP compiled to use clever mailer"); 1547 rcode = EX_CONFIG; 1548 goto give_up; 1549 } 1550 #endif /* SMTP */ 1551 #if NAMED_BIND 1552 if (ConfigLevel < 2) 1553 _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 1554 #endif 1555 1556 /* arrange a return receipt if requested */ 1557 if (rcode == EX_OK && e->e_receiptto != NULL && 1558 bitnset(M_LOCALMAILER, m->m_flags)) 1559 { 1560 e->e_flags |= EF_SENDRECEIPT; 1561 /* do we want to send back more info? */ 1562 } 1563 1564 /* 1565 ** Do final status disposal. 1566 ** We check for something in tobuf for the SMTP case. 1567 ** If we got a temporary failure, arrange to queue the 1568 ** addressees. 1569 */ 1570 1571 give_up: 1572 if (tobuf[0] != '\0') 1573 giveresponse(rcode, m, mci, ctladdr, xstart, e); 1574 for (to = tochain; to != NULL; to = to->q_tchain) 1575 { 1576 if (rcode != EX_OK) 1577 markfailure(e, to, mci, rcode); 1578 else if (!bitset(QBADADDR|QQUEUEUP, to->q_flags)) 1579 { 1580 to->q_flags |= QSENT; 1581 to->q_statdate = curtime(); 1582 e->e_nsent++; 1583 if (bitnset(M_LOCALMAILER, m->m_flags) && 1584 (e->e_receiptto != NULL || 1585 bitset(QPINGONSUCCESS, to->q_flags))) 1586 { 1587 to->q_flags |= QDELIVERED; 1588 to->q_status = "2.1.5"; 1589 fprintf(e->e_xfp, "%s... Successfully delivered\n", 1590 to->q_paddr); 1591 } 1592 else if (bitset(QPINGONSUCCESS, to->q_flags) && 1593 bitset(QPRIMARY, to->q_flags) && 1594 !bitset(MCIF_DSN, mci->mci_flags)) 1595 { 1596 to->q_flags |= QRELAYED; 1597 fprintf(e->e_xfp, "%s... relayed; expect no further notifications\n", 1598 to->q_paddr); 1599 } 1600 } 1601 } 1602 1603 /* 1604 ** Restore state and return. 1605 */ 1606 1607 #ifdef XDEBUG 1608 { 1609 char wbuf[MAXLINE]; 1610 1611 /* make absolutely certain 0, 1, and 2 are in use */ 1612 sprintf(wbuf, "%s... end of deliver(%s)", 1613 e->e_to == NULL ? "NO-TO-LIST" : e->e_to, 1614 m->m_name); 1615 checkfd012(wbuf); 1616 } 1617 #endif 1618 1619 errno = 0; 1620 define('g', (char *) NULL, e); 1621 return (rcode); 1622 } 1623 /* 1624 ** MARKFAILURE -- mark a failure on a specific address. 1625 ** 1626 ** Parameters: 1627 ** e -- the envelope we are sending. 1628 ** q -- the address to mark. 1629 ** mci -- mailer connection information. 1630 ** rcode -- the code signifying the particular failure. 1631 ** 1632 ** Returns: 1633 ** none. 1634 ** 1635 ** Side Effects: 1636 ** marks the address (and possibly the envelope) with the 1637 ** failure so that an error will be returned or 1638 ** the message will be queued, as appropriate. 1639 */ 1640 1641 markfailure(e, q, mci, rcode) 1642 register ENVELOPE *e; 1643 register ADDRESS *q; 1644 register MCI *mci; 1645 int rcode; 1646 { 1647 char *stat = NULL; 1648 1649 switch (rcode) 1650 { 1651 case EX_OK: 1652 break; 1653 1654 case EX_TEMPFAIL: 1655 case EX_IOERR: 1656 case EX_OSERR: 1657 q->q_flags |= QQUEUEUP; 1658 break; 1659 1660 default: 1661 q->q_flags |= QBADADDR; 1662 break; 1663 } 1664 1665 /* find most specific error code possible */ 1666 if (q->q_status == NULL && mci != NULL) 1667 q->q_status = mci->mci_status; 1668 if (q->q_status == NULL) 1669 q->q_status = e->e_status; 1670 if (q->q_status == NULL) 1671 { 1672 switch (rcode) 1673 { 1674 case EX_USAGE: 1675 stat = "5.5.4"; 1676 break; 1677 1678 case EX_DATAERR: 1679 stat = "5.5.2"; 1680 break; 1681 1682 case EX_NOUSER: 1683 stat = "5.1.1"; 1684 break; 1685 1686 case EX_NOHOST: 1687 stat = "5.1.2"; 1688 break; 1689 1690 case EX_NOINPUT: 1691 case EX_CANTCREAT: 1692 case EX_NOPERM: 1693 stat = "5.3.0"; 1694 break; 1695 1696 case EX_UNAVAILABLE: 1697 case EX_SOFTWARE: 1698 case EX_OSFILE: 1699 case EX_PROTOCOL: 1700 case EX_CONFIG: 1701 stat = "5.5.0"; 1702 break; 1703 1704 case EX_OSERR: 1705 case EX_IOERR: 1706 stat = "4.5.0"; 1707 break; 1708 1709 case EX_TEMPFAIL: 1710 stat = "4.2.0"; 1711 break; 1712 } 1713 if (stat != NULL) 1714 q->q_status = stat; 1715 } 1716 1717 q->q_statdate = curtime(); 1718 if (CurHostName != NULL && CurHostName[0] != '\0') 1719 q->q_statmta = newstr(CurHostName); 1720 if (rcode != EX_OK && q->q_rstatus == NULL) 1721 { 1722 char buf[30]; 1723 1724 (void) sprintf(buf, "%d", rcode); 1725 q->q_rstatus = newstr(buf); 1726 } 1727 } 1728 /* 1729 ** ENDMAILER -- Wait for mailer to terminate. 1730 ** 1731 ** We should never get fatal errors (e.g., segmentation 1732 ** violation), so we report those specially. For other 1733 ** errors, we choose a status message (into statmsg), 1734 ** and if it represents an error, we print it. 1735 ** 1736 ** Parameters: 1737 ** pid -- pid of mailer. 1738 ** e -- the current envelope. 1739 ** pv -- the parameter vector that invoked the mailer 1740 ** (for error messages). 1741 ** 1742 ** Returns: 1743 ** exit code of mailer. 1744 ** 1745 ** Side Effects: 1746 ** none. 1747 */ 1748 1749 endmailer(mci, e, pv) 1750 register MCI *mci; 1751 register ENVELOPE *e; 1752 char **pv; 1753 { 1754 int st; 1755 1756 /* close any connections */ 1757 if (mci->mci_in != NULL) 1758 (void) xfclose(mci->mci_in, mci->mci_mailer->m_name, "mci_in"); 1759 if (mci->mci_out != NULL) 1760 (void) xfclose(mci->mci_out, mci->mci_mailer->m_name, "mci_out"); 1761 mci->mci_in = mci->mci_out = NULL; 1762 mci->mci_state = MCIS_CLOSED; 1763 1764 /* in the IPC case there is nothing to wait for */ 1765 if (mci->mci_pid == 0) 1766 return (EX_OK); 1767 1768 /* wait for the mailer process to die and collect status */ 1769 st = waitfor(mci->mci_pid); 1770 if (st == -1) 1771 { 1772 syserr("endmailer %s: wait", pv[0]); 1773 return (EX_SOFTWARE); 1774 } 1775 1776 if (WIFEXITED(st)) 1777 { 1778 /* normal death -- return status */ 1779 return (WEXITSTATUS(st)); 1780 } 1781 1782 /* it died a horrid death */ 1783 syserr("451 mailer %s died with signal %o", 1784 mci->mci_mailer->m_name, st); 1785 1786 /* log the arguments */ 1787 if (pv != NULL && e->e_xfp != NULL) 1788 { 1789 register char **av; 1790 1791 fprintf(e->e_xfp, "Arguments:"); 1792 for (av = pv; *av != NULL; av++) 1793 fprintf(e->e_xfp, " %s", *av); 1794 fprintf(e->e_xfp, "\n"); 1795 } 1796 1797 ExitStat = EX_TEMPFAIL; 1798 return (EX_TEMPFAIL); 1799 } 1800 /* 1801 ** GIVERESPONSE -- Interpret an error response from a mailer 1802 ** 1803 ** Parameters: 1804 ** stat -- the status code from the mailer (high byte 1805 ** only; core dumps must have been taken care of 1806 ** already). 1807 ** m -- the mailer info for this mailer. 1808 ** mci -- the mailer connection info -- can be NULL if the 1809 ** response is given before the connection is made. 1810 ** ctladdr -- the controlling address for the recipient 1811 ** address(es). 1812 ** xstart -- the transaction start time, for computing 1813 ** transaction delays. 1814 ** e -- the current envelope. 1815 ** 1816 ** Returns: 1817 ** none. 1818 ** 1819 ** Side Effects: 1820 ** Errors may be incremented. 1821 ** ExitStat may be set. 1822 */ 1823 1824 giveresponse(stat, m, mci, ctladdr, xstart, e) 1825 int stat; 1826 register MAILER *m; 1827 register MCI *mci; 1828 ADDRESS *ctladdr; 1829 time_t xstart; 1830 ENVELOPE *e; 1831 { 1832 register const char *statmsg; 1833 extern char *SysExMsg[]; 1834 register int i; 1835 extern int N_SysEx; 1836 char buf[MAXLINE]; 1837 1838 /* 1839 ** Compute status message from code. 1840 */ 1841 1842 i = stat - EX__BASE; 1843 if (stat == 0) 1844 { 1845 statmsg = "250 Sent"; 1846 if (e->e_statmsg != NULL) 1847 { 1848 (void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg); 1849 statmsg = buf; 1850 } 1851 } 1852 else if (i < 0 || i > N_SysEx) 1853 { 1854 (void) sprintf(buf, "554 unknown mailer error %d", stat); 1855 stat = EX_UNAVAILABLE; 1856 statmsg = buf; 1857 } 1858 else if (stat == EX_TEMPFAIL) 1859 { 1860 (void) strcpy(buf, SysExMsg[i] + 1); 1861 #if NAMED_BIND 1862 if (h_errno == TRY_AGAIN) 1863 statmsg = errstring(h_errno+E_DNSBASE); 1864 else 1865 #endif 1866 { 1867 if (errno != 0) 1868 statmsg = errstring(errno); 1869 else 1870 { 1871 #ifdef SMTP 1872 statmsg = SmtpError; 1873 #else /* SMTP */ 1874 statmsg = NULL; 1875 #endif /* SMTP */ 1876 } 1877 } 1878 if (statmsg != NULL && statmsg[0] != '\0') 1879 { 1880 (void) strcat(buf, ": "); 1881 (void) strcat(buf, statmsg); 1882 } 1883 statmsg = buf; 1884 } 1885 #if NAMED_BIND 1886 else if (stat == EX_NOHOST && h_errno != 0) 1887 { 1888 statmsg = errstring(h_errno + E_DNSBASE); 1889 (void) sprintf(buf, "%s (%s)", SysExMsg[i] + 1, statmsg); 1890 statmsg = buf; 1891 } 1892 #endif 1893 else 1894 { 1895 statmsg = SysExMsg[i]; 1896 if (*statmsg++ == ':') 1897 { 1898 (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 1899 statmsg = buf; 1900 } 1901 } 1902 1903 /* 1904 ** Print the message as appropriate 1905 */ 1906 1907 if (stat == EX_OK || stat == EX_TEMPFAIL) 1908 { 1909 extern char MsgBuf[]; 1910 1911 message("%s", &statmsg[4]); 1912 if (stat == EX_TEMPFAIL && e->e_xfp != NULL) 1913 fprintf(e->e_xfp, "%s\n", &MsgBuf[4]); 1914 } 1915 else 1916 { 1917 char mbuf[8]; 1918 1919 Errors++; 1920 sprintf(mbuf, "%.3s %%s", statmsg); 1921 usrerr(mbuf, &statmsg[4]); 1922 } 1923 1924 /* 1925 ** Final cleanup. 1926 ** Log a record of the transaction. Compute the new 1927 ** ExitStat -- if we already had an error, stick with 1928 ** that. 1929 */ 1930 1931 if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 1932 logdelivery(m, mci, &statmsg[4], ctladdr, xstart, e); 1933 1934 if (tTd(11, 2)) 1935 printf("giveresponse: stat=%d, e->e_message=%s\n", 1936 stat, e->e_message == NULL ? "<NULL>" : e->e_message); 1937 1938 if (stat != EX_TEMPFAIL) 1939 setstat(stat); 1940 if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL)) 1941 { 1942 if (e->e_message != NULL) 1943 free(e->e_message); 1944 e->e_message = newstr(&statmsg[4]); 1945 } 1946 errno = 0; 1947 #if NAMED_BIND 1948 h_errno = 0; 1949 #endif 1950 } 1951 /* 1952 ** LOGDELIVERY -- log the delivery in the system log 1953 ** 1954 ** Care is taken to avoid logging lines that are too long, because 1955 ** some versions of syslog have an unfortunate proclivity for core 1956 ** dumping. This is a hack, to be sure, that is at best empirical. 1957 ** 1958 ** Parameters: 1959 ** m -- the mailer info. Can be NULL for initial queue. 1960 ** mci -- the mailer connection info -- can be NULL if the 1961 ** log is occuring when no connection is active. 1962 ** stat -- the message to print for the status. 1963 ** ctladdr -- the controlling address for the to list. 1964 ** xstart -- the transaction start time, used for 1965 ** computing transaction delay. 1966 ** e -- the current envelope. 1967 ** 1968 ** Returns: 1969 ** none 1970 ** 1971 ** Side Effects: 1972 ** none 1973 */ 1974 1975 logdelivery(m, mci, stat, ctladdr, xstart, e) 1976 MAILER *m; 1977 register MCI *mci; 1978 char *stat; 1979 ADDRESS *ctladdr; 1980 time_t xstart; 1981 register ENVELOPE *e; 1982 { 1983 # ifdef LOG 1984 register char *bp; 1985 register char *p; 1986 int l; 1987 char buf[512]; 1988 1989 # if (SYSLOG_BUFSIZE) >= 256 1990 bp = buf; 1991 if (ctladdr != NULL) 1992 { 1993 strcpy(bp, ", ctladdr="); 1994 strcat(bp, shortenstring(ctladdr->q_paddr, 83)); 1995 bp += strlen(bp); 1996 if (bitset(QGOODUID, ctladdr->q_flags)) 1997 { 1998 (void) sprintf(bp, " (%d/%d)", 1999 ctladdr->q_uid, ctladdr->q_gid); 2000 bp += strlen(bp); 2001 } 2002 } 2003 2004 sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 2005 bp += strlen(bp); 2006 2007 if (xstart != (time_t) 0) 2008 { 2009 sprintf(bp, ", xdelay=%s", pintvl(curtime() - xstart, TRUE)); 2010 bp += strlen(bp); 2011 } 2012 2013 if (m != NULL) 2014 { 2015 (void) strcpy(bp, ", mailer="); 2016 (void) strcat(bp, m->m_name); 2017 bp += strlen(bp); 2018 } 2019 2020 if (mci != NULL && mci->mci_host != NULL) 2021 { 2022 # ifdef DAEMON 2023 extern SOCKADDR CurHostAddr; 2024 # endif 2025 2026 (void) strcpy(bp, ", relay="); 2027 (void) strcat(bp, mci->mci_host); 2028 2029 # ifdef DAEMON 2030 (void) strcat(bp, " ["); 2031 (void) strcat(bp, anynet_ntoa(&CurHostAddr)); 2032 (void) strcat(bp, "]"); 2033 # endif 2034 } 2035 else if (strcmp(stat, "queued") != 0) 2036 { 2037 char *p = macvalue('h', e); 2038 2039 if (p != NULL && p[0] != '\0') 2040 { 2041 (void) strcpy(bp, ", relay="); 2042 (void) strcat(bp, p); 2043 } 2044 } 2045 bp += strlen(bp); 2046 2047 #define STATLEN (((SYSLOG_BUFSIZE) - 100) / 4) 2048 #if (STATLEN) < 63 2049 # undef STATLEN 2050 # define STATLEN 63 2051 #endif 2052 #if (STATLEN) > 203 2053 # undef STATLEN 2054 # define STATLEN 203 2055 #endif 2056 2057 if ((bp - buf) > (sizeof buf - ((STATLEN) + 20))) 2058 { 2059 /* desperation move -- truncate data */ 2060 bp = buf + sizeof buf - ((STATLEN) + 17); 2061 strcpy(bp, "..."); 2062 bp += 3; 2063 } 2064 2065 (void) strcpy(bp, ", stat="); 2066 bp += strlen(bp); 2067 2068 (void) strcpy(bp, shortenstring(stat, (STATLEN))); 2069 2070 l = SYSLOG_BUFSIZE - 100 - strlen(buf); 2071 p = e->e_to; 2072 while (strlen(p) >= (SIZE_T) l) 2073 { 2074 register char *q = strchr(p + l, ','); 2075 2076 if (q == NULL) 2077 break; 2078 syslog(LOG_INFO, "%s: to=%.*s [more]%s", 2079 e->e_id, ++q - p, p, buf); 2080 p = q; 2081 } 2082 syslog(LOG_INFO, "%s: to=%s%s", e->e_id, p, buf); 2083 2084 # else /* we have a very short log buffer size */ 2085 2086 l = SYSLOG_BUFSIZE - 85; 2087 p = e->e_to; 2088 while (strlen(p) >= l) 2089 { 2090 register char *q = strchr(p + l, ','); 2091 2092 if (q == NULL) 2093 break; 2094 syslog(LOG_INFO, "%s: to=%.*s [more]", 2095 e->e_id, ++q - p, p); 2096 p = q; 2097 } 2098 syslog(LOG_INFO, "%s: to=%s", e->e_id, p); 2099 2100 if (ctladdr != NULL) 2101 { 2102 bp = buf; 2103 strcpy(buf, "ctladdr="); 2104 bp += strlen(buf); 2105 strcpy(bp, shortenstring(ctladdr->q_paddr, 83)); 2106 bp += strlen(buf); 2107 if (bitset(QGOODUID, ctladdr->q_flags)) 2108 { 2109 (void) sprintf(bp, " (%d/%d)", 2110 ctladdr->q_uid, ctladdr->q_gid); 2111 bp += strlen(bp); 2112 } 2113 syslog(LOG_INFO, "%s: %s", e->e_id, buf); 2114 } 2115 bp = buf; 2116 sprintf(bp, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 2117 bp += strlen(bp); 2118 if (xstart != (time_t) 0) 2119 { 2120 sprintf(bp, ", xdelay=%s", pintvl(curtime() - xstart, TRUE)); 2121 bp += strlen(bp); 2122 } 2123 2124 if (m != NULL) 2125 { 2126 sprintf(bp, ", mailer=%s", m->m_name); 2127 bp += strlen(bp); 2128 } 2129 syslog(LOG_INFO, "%s: %s", e->e_id, buf); 2130 2131 buf[0] = '\0'; 2132 if (mci != NULL && mci->mci_host != NULL) 2133 { 2134 # ifdef DAEMON 2135 extern SOCKADDR CurHostAddr; 2136 # endif 2137 2138 sprintf(buf, "relay=%s", mci->mci_host); 2139 2140 # ifdef DAEMON 2141 (void) strcat(buf, " ["); 2142 (void) strcat(buf, anynet_ntoa(&CurHostAddr)); 2143 (void) strcat(buf, "]"); 2144 # endif 2145 } 2146 else if (strcmp(stat, "queued") != 0) 2147 { 2148 char *p = macvalue('h', e); 2149 2150 if (p != NULL && p[0] != '\0') 2151 sprintf(buf, "relay=%s", p); 2152 } 2153 if (buf[0] != '\0') 2154 syslog(LOG_INFO, "%s: %s", e->e_id, buf); 2155 2156 syslog(LOG_INFO, "%s: stat=%s", e->e_id, shortenstring(stat, 63)); 2157 # endif /* short log buffer */ 2158 # endif /* LOG */ 2159 } 2160 /* 2161 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 2162 ** 2163 ** This can be made an arbitrary message separator by changing $l 2164 ** 2165 ** One of the ugliest hacks seen by human eyes is contained herein: 2166 ** UUCP wants those stupid "remote from <host>" lines. Why oh why 2167 ** does a well-meaning programmer such as myself have to deal with 2168 ** this kind of antique garbage???? 2169 ** 2170 ** Parameters: 2171 ** mci -- the connection information. 2172 ** e -- the envelope. 2173 ** 2174 ** Returns: 2175 ** none 2176 ** 2177 ** Side Effects: 2178 ** outputs some text to fp. 2179 */ 2180 2181 putfromline(mci, e) 2182 register MCI *mci; 2183 ENVELOPE *e; 2184 { 2185 char *template = "\201l\n"; 2186 char buf[MAXLINE]; 2187 2188 if (bitnset(M_NHDR, mci->mci_mailer->m_flags)) 2189 return; 2190 2191 # ifdef UGLYUUCP 2192 if (bitnset(M_UGLYUUCP, mci->mci_mailer->m_flags)) 2193 { 2194 char *bang; 2195 char xbuf[MAXLINE]; 2196 2197 expand("\201g", buf, sizeof buf, e); 2198 bang = strchr(buf, '!'); 2199 if (bang == NULL) 2200 { 2201 errno = 0; 2202 syserr("554 No ! in UUCP From address! (%s given)", buf); 2203 } 2204 else 2205 { 2206 *bang++ = '\0'; 2207 (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 2208 template = xbuf; 2209 } 2210 } 2211 # endif /* UGLYUUCP */ 2212 expand(template, buf, sizeof buf, e); 2213 putxline(buf, mci, PXLF_NOTHINGSPECIAL); 2214 } 2215 /* 2216 ** PUTBODY -- put the body of a message. 2217 ** 2218 ** Parameters: 2219 ** mci -- the connection information. 2220 ** e -- the envelope to put out. 2221 ** separator -- if non-NULL, a message separator that must 2222 ** not be permitted in the resulting message. 2223 ** 2224 ** Returns: 2225 ** none. 2226 ** 2227 ** Side Effects: 2228 ** The message is written onto fp. 2229 */ 2230 2231 /* values for output state variable */ 2232 #define OS_HEAD 0 /* at beginning of line */ 2233 #define OS_CR 1 /* read a carriage return */ 2234 #define OS_INLINE 2 /* putting rest of line */ 2235 2236 putbody(mci, e, separator) 2237 register MCI *mci; 2238 register ENVELOPE *e; 2239 char *separator; 2240 { 2241 char buf[MAXLINE]; 2242 2243 /* 2244 ** Output the body of the message 2245 */ 2246 2247 if (e->e_dfp == NULL && bitset(EF_HAS_DF, e->e_flags)) 2248 { 2249 char *df = queuename(e, 'd'); 2250 2251 e->e_dfp = fopen(df, "r"); 2252 if (e->e_dfp == NULL) 2253 syserr("putbody: Cannot open %s for %s from %s", 2254 df, e->e_to, e->e_from.q_paddr); 2255 } 2256 if (e->e_dfp == NULL) 2257 { 2258 if (bitset(MCIF_INHEADER, mci->mci_flags)) 2259 { 2260 putline("", mci); 2261 mci->mci_flags &= ~MCIF_INHEADER; 2262 } 2263 putline("<<< No Message Collected >>>", mci); 2264 goto endofmessage; 2265 } 2266 if (e->e_dfino == (ino_t) 0) 2267 { 2268 struct stat stbuf; 2269 2270 if (fstat(fileno(e->e_dfp), &stbuf) < 0) 2271 e->e_dfino = -1; 2272 else 2273 { 2274 e->e_dfdev = stbuf.st_dev; 2275 e->e_dfino = stbuf.st_ino; 2276 } 2277 } 2278 rewind(e->e_dfp); 2279 2280 if (bitset(MCIF_CVT8TO7, mci->mci_flags)) 2281 { 2282 char *boundaries[MAXMIMENESTING + 1]; 2283 2284 /* 2285 ** Do 8 to 7 bit MIME conversion. 2286 */ 2287 2288 /* make sure it looks like a MIME message */ 2289 if (hvalue("MIME-Version", e->e_header) == NULL) 2290 putline("MIME-Version: 1.0", mci); 2291 2292 if (hvalue("Content-Type", e->e_header) == NULL) 2293 { 2294 sprintf(buf, "Content-Type: text/plain; charset=%s", 2295 defcharset(e)); 2296 putline(buf, mci); 2297 } 2298 2299 /* now do the hard work */ 2300 boundaries[0] = NULL; 2301 mime8to7(mci, e->e_header, e, boundaries, M87F_OUTER); 2302 } 2303 else 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 mailfile(filename, ctladdr, e) 2515 char *filename; 2516 ADDRESS *ctladdr; 2517 register ENVELOPE *e; 2518 { 2519 register FILE *f; 2520 register int pid = -1; 2521 int mode; 2522 2523 if (tTd(11, 1)) 2524 { 2525 printf("mailfile %s\n ctladdr=", filename); 2526 printaddr(ctladdr, FALSE); 2527 } 2528 2529 if (e->e_xfp != NULL) 2530 fflush(e->e_xfp); 2531 2532 /* 2533 ** Fork so we can change permissions here. 2534 ** Note that we MUST use fork, not vfork, because of 2535 ** the complications of calling subroutines, etc. 2536 */ 2537 2538 DOFORK(fork); 2539 2540 if (pid < 0) 2541 return (EX_OSERR); 2542 else if (pid == 0) 2543 { 2544 /* child -- actually write to file */ 2545 struct stat stb; 2546 struct stat fsb; 2547 MCI mcibuf; 2548 int oflags = O_WRONLY|O_APPEND; 2549 2550 if (e->e_lockfp != NULL) 2551 (void) close(fileno(e->e_lockfp)); 2552 2553 (void) setsignal(SIGINT, SIG_DFL); 2554 (void) setsignal(SIGHUP, SIG_DFL); 2555 (void) setsignal(SIGTERM, SIG_DFL); 2556 (void) umask(OldUmask); 2557 2558 #ifdef HASLSTAT 2559 if ((SafeFileEnv != NULL ? lstat(filename, &stb) 2560 : stat(filename, &stb)) < 0) 2561 #else 2562 if (stat(filename, &stb) < 0) 2563 #endif 2564 { 2565 stb.st_mode = FileMode; 2566 oflags |= O_CREAT|O_EXCL; 2567 } 2568 else if (bitset(0111, stb.st_mode) || stb.st_nlink != 1 || 2569 (SafeFileEnv != NULL && !S_ISREG(stb.st_mode))) 2570 exit(EX_CANTCREAT); 2571 mode = stb.st_mode; 2572 2573 /* limit the errors to those actually caused in the child */ 2574 errno = 0; 2575 ExitStat = EX_OK; 2576 2577 if (ctladdr != NULL) 2578 { 2579 /* ignore setuid and setgid bits */ 2580 mode &= ~(S_ISGID|S_ISUID); 2581 } 2582 2583 /* we have to open the dfile BEFORE setuid */ 2584 if (e->e_dfp == NULL && bitset(EF_HAS_DF, e->e_flags)) 2585 { 2586 char *df = queuename(e, 'd'); 2587 2588 e->e_dfp = fopen(df, "r"); 2589 if (e->e_dfp == NULL) 2590 { 2591 syserr("mailfile: Cannot open %s for %s from %s", 2592 df, e->e_to, e->e_from.q_paddr); 2593 } 2594 } 2595 2596 if (SafeFileEnv != NULL && SafeFileEnv[0] != '\0') 2597 { 2598 int i; 2599 2600 if (chroot(SafeFileEnv) < 0) 2601 { 2602 syserr("mailfile: Cannot chroot(%s)", 2603 SafeFileEnv); 2604 exit(EX_CANTCREAT); 2605 } 2606 i = strlen(SafeFileEnv); 2607 if (strncmp(SafeFileEnv, filename, i) == 0) 2608 filename += i; 2609 } 2610 if (chdir("/") < 0) 2611 syserr("mailfile: cannot chdir(/)"); 2612 2613 if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 2614 { 2615 if (ctladdr != NULL && ctladdr->q_uid != 0) 2616 (void) initgroups(ctladdr->q_ruser ? 2617 ctladdr->q_ruser : ctladdr->q_user, 2618 ctladdr->q_gid); 2619 else if (FileMailer != NULL && FileMailer->m_gid != 0) 2620 (void) initgroups(DefUser, FileMailer->m_gid); 2621 else 2622 (void) initgroups(DefUser, DefGid); 2623 } 2624 if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 2625 { 2626 if (ctladdr != NULL && ctladdr->q_uid != 0) 2627 (void) setuid(ctladdr->q_uid); 2628 else if (FileMailer != NULL && FileMailer->m_uid != 0) 2629 (void) setuid(FileMailer->m_uid); 2630 else 2631 (void) setuid(DefUid); 2632 } 2633 FileName = filename; 2634 LineNumber = 0; 2635 f = dfopen(filename, oflags, FileMode); 2636 if (f == NULL) 2637 { 2638 message("554 cannot open: %s", errstring(errno)); 2639 exit(EX_CANTCREAT); 2640 } 2641 if (fstat(fileno(f), &fsb) < 0 || 2642 (!bitset(O_CREAT, oflags) && 2643 (stb.st_nlink != fsb.st_nlink || 2644 stb.st_dev != fsb.st_dev || 2645 stb.st_ino != fsb.st_ino || 2646 stb.st_uid != fsb.st_uid))) 2647 { 2648 message("554 cannot write: file changed after open"); 2649 exit(EX_CANTCREAT); 2650 } 2651 2652 bzero(&mcibuf, sizeof mcibuf); 2653 mcibuf.mci_mailer = FileMailer; 2654 mcibuf.mci_out = f; 2655 if (bitnset(M_7BITS, FileMailer->m_flags)) 2656 mcibuf.mci_flags |= MCIF_7BIT; 2657 2658 putfromline(&mcibuf, e); 2659 (*e->e_puthdr)(&mcibuf, e->e_header, e); 2660 (*e->e_putbody)(&mcibuf, e, NULL); 2661 putline("\n", &mcibuf); 2662 if (ferror(f)) 2663 { 2664 message("451 I/O error: %s", errstring(errno)); 2665 setstat(EX_IOERR); 2666 } 2667 (void) xfclose(f, "mailfile", filename); 2668 (void) fflush(stdout); 2669 2670 /* reset ISUID & ISGID bits for paranoid systems */ 2671 (void) chmod(filename, (int) stb.st_mode); 2672 exit(ExitStat); 2673 /*NOTREACHED*/ 2674 } 2675 else 2676 { 2677 /* parent -- wait for exit status */ 2678 int st; 2679 2680 st = waitfor(pid); 2681 if (WIFEXITED(st)) 2682 return (WEXITSTATUS(st)); 2683 else 2684 { 2685 syserr("child died on signal %d", st); 2686 return (EX_UNAVAILABLE); 2687 } 2688 /*NOTREACHED*/ 2689 } 2690 } 2691 /* 2692 ** HOSTSIGNATURE -- return the "signature" for a host. 2693 ** 2694 ** The signature describes how we are going to send this -- it 2695 ** can be just the hostname (for non-Internet hosts) or can be 2696 ** an ordered list of MX hosts. 2697 ** 2698 ** Parameters: 2699 ** m -- the mailer describing this host. 2700 ** host -- the host name. 2701 ** e -- the current envelope. 2702 ** 2703 ** Returns: 2704 ** The signature for this host. 2705 ** 2706 ** Side Effects: 2707 ** Can tweak the symbol table. 2708 */ 2709 2710 char * 2711 hostsignature(m, host, e) 2712 register MAILER *m; 2713 char *host; 2714 ENVELOPE *e; 2715 { 2716 register char *p; 2717 register STAB *s; 2718 int i; 2719 int len; 2720 #if NAMED_BIND 2721 int nmx; 2722 auto int rcode; 2723 char *hp; 2724 char *endp; 2725 int oldoptions = _res.options; 2726 char *mxhosts[MAXMXHOSTS + 1]; 2727 #endif 2728 2729 /* 2730 ** Check to see if this uses IPC -- if not, it can't have MX records. 2731 */ 2732 2733 p = m->m_mailer; 2734 if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 2735 { 2736 /* just an ordinary mailer */ 2737 return host; 2738 } 2739 2740 /* 2741 ** Look it up in the symbol table. 2742 */ 2743 2744 s = stab(host, ST_HOSTSIG, ST_ENTER); 2745 if (s->s_hostsig != NULL) 2746 return s->s_hostsig; 2747 2748 /* 2749 ** Not already there -- create a signature. 2750 */ 2751 2752 #if NAMED_BIND 2753 if (ConfigLevel < 2) 2754 _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 2755 2756 for (hp = host; hp != NULL; hp = endp) 2757 { 2758 endp = strchr(hp, ':'); 2759 if (endp != NULL) 2760 *endp = '\0'; 2761 2762 nmx = getmxrr(hp, mxhosts, TRUE, &rcode); 2763 2764 if (nmx <= 0) 2765 { 2766 register MCI *mci; 2767 2768 /* update the connection info for this host */ 2769 mci = mci_get(hp, m); 2770 mci->mci_exitstat = rcode; 2771 mci->mci_errno = errno; 2772 mci->mci_herrno = h_errno; 2773 2774 /* and return the original host name as the signature */ 2775 nmx = 1; 2776 mxhosts[0] = hp; 2777 } 2778 2779 len = 0; 2780 for (i = 0; i < nmx; i++) 2781 { 2782 len += strlen(mxhosts[i]) + 1; 2783 } 2784 if (s->s_hostsig != NULL) 2785 len += strlen(s->s_hostsig) + 1; 2786 p = xalloc(len); 2787 if (s->s_hostsig != NULL) 2788 { 2789 (void) strcpy(p, s->s_hostsig); 2790 free(s->s_hostsig); 2791 s->s_hostsig = p; 2792 p += strlen(p); 2793 *p++ = ':'; 2794 } 2795 else 2796 s->s_hostsig = p; 2797 for (i = 0; i < nmx; i++) 2798 { 2799 if (i != 0) 2800 *p++ = ':'; 2801 strcpy(p, mxhosts[i]); 2802 p += strlen(p); 2803 } 2804 if (endp != NULL) 2805 *endp++ = ':'; 2806 } 2807 makelower(s->s_hostsig); 2808 if (ConfigLevel < 2) 2809 _res.options = oldoptions; 2810 #else 2811 /* not using BIND -- the signature is just the host name */ 2812 s->s_hostsig = host; 2813 #endif 2814 if (tTd(17, 1)) 2815 printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 2816 return s->s_hostsig; 2817 } 2818