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