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