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