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