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.133 (Berkeley) 03/21/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 /* save id for future use */ 349 qid = e->e_id; 350 351 /* now drop the envelope in the parent */ 352 e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 353 dropenvelope(e); 354 355 /* and reacquire in the child */ 356 (void) dowork(qid, TRUE, FALSE, e); 357 358 return; 359 360 # else /* HASFLOCK */ 361 362 pid = fork(); 363 if (pid < 0) 364 { 365 goto queueonly; 366 } 367 else if (pid > 0) 368 { 369 /* be sure we leave the temp files to our child */ 370 /* can't call unlockqueue to avoid unlink of xfp */ 371 if (e->e_lockfp != NULL) 372 (void) xfclose(e->e_lockfp, "sendenvelope lockfp", e->e_id); 373 e->e_lockfp = NULL; 374 375 /* close any random open files in the envelope */ 376 closexscript(e); 377 if (e->e_dfp != NULL) 378 (void) xfclose(e->e_dfp, "sendenvelope dfp", e->e_id); 379 e->e_dfp = NULL; 380 e->e_id = NULL; 381 e->e_flags &= ~EF_HAS_DF; 382 383 /* catch intermediate zombie */ 384 (void) waitfor(pid); 385 return; 386 } 387 388 /* double fork to avoid zombies */ 389 pid = fork(); 390 if (pid > 0) 391 exit(EX_OK); 392 393 /* be sure we are immune from the terminal */ 394 disconnect(1, e); 395 396 /* prevent parent from waiting if there was an error */ 397 if (pid < 0) 398 { 399 e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 400 finis(); 401 } 402 403 /* 404 ** Close any cached connections. 405 ** 406 ** We don't send the QUIT protocol because the parent 407 ** still knows about the connection. 408 ** 409 ** This should only happen when delivering an error 410 ** message. 411 */ 412 413 mci_flush(FALSE, NULL); 414 415 # endif /* HASFLOCK */ 416 417 break; 418 } 419 420 if (splitenv != NULL) 421 { 422 if (tTd(13, 1)) 423 { 424 printf("\nsendall: Split queue; remaining queue:\n"); 425 printaddr(e->e_sendqueue, TRUE); 426 } 427 428 for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 429 { 430 CurEnv = ee; 431 if (mode != SM_VERIFY) 432 openxscript(ee); 433 sendenvelope(ee, mode); 434 dropenvelope(ee); 435 } 436 437 CurEnv = e; 438 } 439 sendenvelope(e, mode); 440 Verbose = oldverbose; 441 } 442 443 void 444 sendenvelope(e, mode) 445 register ENVELOPE *e; 446 char mode; 447 { 448 register ADDRESS *q; 449 bool didany; 450 451 /* 452 ** If we have had global, fatal errors, don't bother sending 453 ** the message at all if we are in SMTP mode. Local errors 454 ** (e.g., a single address failing) will still cause the other 455 ** addresses to be sent. 456 */ 457 458 if (bitset(EF_FATALERRS, e->e_flags) && 459 (OpMode == MD_SMTP || OpMode == MD_DAEMON)) 460 { 461 e->e_flags |= EF_CLRQUEUE; 462 return; 463 } 464 465 /* 466 ** Run through the list and send everything. 467 ** 468 ** Set EF_GLOBALERRS so that error messages during delivery 469 ** result in returned mail. 470 */ 471 472 e->e_nsent = 0; 473 e->e_flags |= EF_GLOBALERRS; 474 didany = FALSE; 475 476 /* now run through the queue */ 477 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 478 { 479 #ifdef XDEBUG 480 char wbuf[MAXNAME + 20]; 481 482 (void) sprintf(wbuf, "sendall(%s)", q->q_paddr); 483 checkfd012(wbuf); 484 #endif 485 if (mode == SM_VERIFY) 486 { 487 e->e_to = q->q_paddr; 488 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 489 { 490 if (q->q_host != NULL && q->q_host[0] != '\0') 491 message("deliverable: mailer %s, host %s, user %s", 492 q->q_mailer->m_name, 493 q->q_host, 494 q->q_user); 495 else 496 message("deliverable: mailer %s, user %s", 497 q->q_mailer->m_name, 498 q->q_user); 499 } 500 } 501 else if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 502 { 503 # ifdef QUEUE 504 /* 505 ** Checkpoint the send list every few addresses 506 */ 507 508 if (e->e_nsent >= CheckpointInterval) 509 { 510 queueup(e, TRUE, FALSE); 511 e->e_nsent = 0; 512 } 513 # endif /* QUEUE */ 514 (void) deliver(e, q); 515 didany = TRUE; 516 } 517 } 518 if (didany) 519 { 520 e->e_dtime = curtime(); 521 e->e_ntries++; 522 } 523 524 #ifdef XDEBUG 525 checkfd012("end of sendenvelope"); 526 #endif 527 528 if (mode == SM_FORK) 529 finis(); 530 } 531 /* 532 ** DOFORK -- do a fork, retrying a couple of times on failure. 533 ** 534 ** This MUST be a macro, since after a vfork we are running 535 ** two processes on the same stack!!! 536 ** 537 ** Parameters: 538 ** none. 539 ** 540 ** Returns: 541 ** From a macro??? You've got to be kidding! 542 ** 543 ** Side Effects: 544 ** Modifies the ==> LOCAL <== variable 'pid', leaving: 545 ** pid of child in parent, zero in child. 546 ** -1 on unrecoverable error. 547 ** 548 ** Notes: 549 ** I'm awfully sorry this looks so awful. That's 550 ** vfork for you..... 551 */ 552 553 # define NFORKTRIES 5 554 555 # ifndef FORK 556 # define FORK fork 557 # endif 558 559 # define DOFORK(fORKfN) \ 560 {\ 561 register int i;\ 562 \ 563 for (i = NFORKTRIES; --i >= 0; )\ 564 {\ 565 pid = fORKfN();\ 566 if (pid >= 0)\ 567 break;\ 568 if (i > 0)\ 569 sleep((unsigned) NFORKTRIES - i);\ 570 }\ 571 } 572 /* 573 ** DOFORK -- simple fork interface to DOFORK. 574 ** 575 ** Parameters: 576 ** none. 577 ** 578 ** Returns: 579 ** pid of child in parent. 580 ** zero in child. 581 ** -1 on error. 582 ** 583 ** Side Effects: 584 ** returns twice, once in parent and once in child. 585 */ 586 587 dofork() 588 { 589 register int pid = -1; 590 591 DOFORK(fork); 592 return (pid); 593 } 594 /* 595 ** DELIVER -- Deliver a message to a list of addresses. 596 ** 597 ** This routine delivers to everyone on the same host as the 598 ** user on the head of the list. It is clever about mailers 599 ** that don't handle multiple users. It is NOT guaranteed 600 ** that it will deliver to all these addresses however -- so 601 ** deliver should be called once for each address on the 602 ** list. 603 ** 604 ** Parameters: 605 ** e -- the envelope to deliver. 606 ** firstto -- head of the address list to deliver to. 607 ** 608 ** Returns: 609 ** zero -- successfully delivered. 610 ** else -- some failure, see ExitStat for more info. 611 ** 612 ** Side Effects: 613 ** The standard input is passed off to someone. 614 */ 615 616 int 617 deliver(e, firstto) 618 register ENVELOPE *e; 619 ADDRESS *firstto; 620 { 621 char *host; /* host being sent to */ 622 char *user; /* user being sent to */ 623 char **pvp; 624 register char **mvp; 625 register char *p; 626 register MAILER *m; /* mailer for this recipient */ 627 ADDRESS *ctladdr; 628 register MCI *mci; 629 register ADDRESS *to = firstto; 630 bool clever = FALSE; /* running user smtp to this mailer */ 631 ADDRESS *tochain = NULL; /* users chain in this mailer call */ 632 int rcode; /* response code */ 633 char *firstsig; /* signature of firstto */ 634 int pid = -1; 635 char *curhost; 636 time_t xstart; 637 int mpvect[2]; 638 int rpvect[2]; 639 char *pv[MAXPV+1]; 640 char tobuf[TOBUFSIZE]; /* text line of to people */ 641 char buf[MAXNAME + 1]; 642 char rpathbuf[MAXNAME + 1]; /* translated return path */ 643 extern int checkcompat(); 644 645 errno = 0; 646 if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags)) 647 return (0); 648 649 #if NAMED_BIND 650 /* unless interactive, try twice, over a minute */ 651 if (OpMode == MD_DAEMON || OpMode == MD_SMTP) 652 { 653 _res.retrans = 30; 654 _res.retry = 2; 655 } 656 #endif 657 658 m = to->q_mailer; 659 host = to->q_host; 660 CurEnv = e; /* just in case */ 661 e->e_statmsg = NULL; 662 SmtpError[0] = '\0'; 663 xstart = curtime(); 664 665 if (tTd(10, 1)) 666 printf("\n--deliver, id=%s, mailer=%s, host=`%s', first user=`%s'\n", 667 e->e_id, m->m_name, host, to->q_user); 668 if (tTd(10, 100)) 669 printopenfds(FALSE); 670 671 /* 672 ** Do initial argv setup. 673 ** Insert the mailer name. Notice that $x expansion is 674 ** NOT done on the mailer name. Then, if the mailer has 675 ** a picky -f flag, we insert it as appropriate. This 676 ** code does not check for 'pv' overflow; this places a 677 ** manifest lower limit of 4 for MAXPV. 678 ** The from address rewrite is expected to make 679 ** the address relative to the other end. 680 */ 681 682 /* rewrite from address, using rewriting rules */ 683 rcode = EX_OK; 684 if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags)) 685 p = e->e_sender; 686 else 687 p = e->e_from.q_paddr; 688 (void) strcpy(rpathbuf, remotename(p, m, 689 RF_SENDERADDR|RF_CANONICAL, 690 &rcode, e)); 691 define('g', rpathbuf, e); /* translated return path */ 692 define('h', host, e); /* to host */ 693 Errors = 0; 694 pvp = pv; 695 *pvp++ = m->m_argv[0]; 696 697 /* insert -f or -r flag as appropriate */ 698 if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 699 { 700 if (bitnset(M_FOPT, m->m_flags)) 701 *pvp++ = "-f"; 702 else 703 *pvp++ = "-r"; 704 *pvp++ = newstr(rpathbuf); 705 } 706 707 /* 708 ** Append the other fixed parts of the argv. These run 709 ** up to the first entry containing "$u". There can only 710 ** be one of these, and there are only a few more slots 711 ** in the pv after it. 712 */ 713 714 for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 715 { 716 /* can't use strchr here because of sign extension problems */ 717 while (*p != '\0') 718 { 719 if ((*p++ & 0377) == MACROEXPAND) 720 { 721 if (*p == 'u') 722 break; 723 } 724 } 725 726 if (*p != '\0') 727 break; 728 729 /* this entry is safe -- go ahead and process it */ 730 expand(*mvp, buf, sizeof buf, e); 731 *pvp++ = newstr(buf); 732 if (pvp >= &pv[MAXPV - 3]) 733 { 734 syserr("554 Too many parameters to %s before $u", pv[0]); 735 return (-1); 736 } 737 } 738 739 /* 740 ** If we have no substitution for the user name in the argument 741 ** list, we know that we must supply the names otherwise -- and 742 ** SMTP is the answer!! 743 */ 744 745 if (*mvp == NULL) 746 { 747 /* running SMTP */ 748 # ifdef SMTP 749 clever = TRUE; 750 *pvp = NULL; 751 # else /* SMTP */ 752 /* oops! we don't implement SMTP */ 753 syserr("554 SMTP style mailer not implemented"); 754 return (EX_SOFTWARE); 755 # endif /* SMTP */ 756 } 757 758 /* 759 ** At this point *mvp points to the argument with $u. We 760 ** run through our address list and append all the addresses 761 ** we can. If we run out of space, do not fret! We can 762 ** always send another copy later. 763 */ 764 765 tobuf[0] = '\0'; 766 e->e_to = tobuf; 767 ctladdr = NULL; 768 firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e); 769 for (; to != NULL; to = to->q_next) 770 { 771 /* avoid sending multiple recipients to dumb mailers */ 772 if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 773 break; 774 775 /* if already sent or not for this host, don't send */ 776 if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 777 to->q_mailer != firstto->q_mailer || 778 strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0) 779 continue; 780 781 /* avoid overflowing tobuf */ 782 if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 783 break; 784 785 if (tTd(10, 1)) 786 { 787 printf("\nsend to "); 788 printaddr(to, FALSE); 789 } 790 791 /* compute effective uid/gid when sending */ 792 if (bitnset(M_RUNASRCPT, to->q_mailer->m_flags)) 793 ctladdr = getctladdr(to); 794 795 if (tTd(10, 2)) 796 { 797 printf("ctladdr="); 798 printaddr(ctladdr, FALSE); 799 } 800 801 user = to->q_user; 802 e->e_to = to->q_paddr; 803 if (tTd(10, 5)) 804 { 805 printf("deliver: QDONTSEND "); 806 printaddr(to, FALSE); 807 } 808 to->q_flags |= QDONTSEND; 809 810 /* 811 ** Check to see that these people are allowed to 812 ** talk to each other. 813 */ 814 815 if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 816 { 817 e->e_flags |= EF_NO_BODY_RETN; 818 usrerr("552 Message is too large; %ld bytes max", m->m_maxsize); 819 giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, xstart, e); 820 continue; 821 } 822 #if NAMED_BIND 823 h_errno = 0; 824 #endif 825 rcode = checkcompat(to, e); 826 if (rcode != EX_OK) 827 { 828 markfailure(e, to, NULL, rcode); 829 giveresponse(rcode, m, NULL, ctladdr, xstart, e); 830 continue; 831 } 832 833 /* 834 ** Strip quote bits from names if the mailer is dumb 835 ** about them. 836 */ 837 838 if (bitnset(M_STRIPQ, m->m_flags)) 839 { 840 stripquotes(user); 841 stripquotes(host); 842 } 843 844 /* hack attack -- delivermail compatibility */ 845 if (m == ProgMailer && *user == '|') 846 user++; 847 848 /* 849 ** If an error message has already been given, don't 850 ** bother to send to this address. 851 ** 852 ** >>>>>>>>>> This clause assumes that the local mailer 853 ** >> NOTE >> cannot do any further aliasing; that 854 ** >>>>>>>>>> function is subsumed by sendmail. 855 */ 856 857 if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 858 continue; 859 860 /* save statistics.... */ 861 markstats(e, to); 862 863 /* 864 ** See if this user name is "special". 865 ** If the user name has a slash in it, assume that this 866 ** is a file -- send it off without further ado. Note 867 ** that this type of addresses is not processed along 868 ** with the others, so we fudge on the To person. 869 */ 870 871 if (m == FileMailer) 872 { 873 rcode = mailfile(user, ctladdr, e); 874 giveresponse(rcode, m, NULL, ctladdr, xstart, e); 875 e->e_nsent++; 876 if (rcode == EX_OK) 877 { 878 to->q_flags |= QSENT; 879 if (bitnset(M_LOCALMAILER, m->m_flags) && 880 (e->e_receiptto != NULL || 881 bitset(QPINGONSUCCESS, to->q_flags))) 882 { 883 to->q_flags |= QREPORT; 884 fprintf(e->e_xfp, "%s... Successfully delivered\n", 885 to->q_paddr); 886 } 887 } 888 to->q_statdate = curtime(); 889 continue; 890 } 891 892 /* 893 ** Address is verified -- add this user to mailer 894 ** argv, and add it to the print list of recipients. 895 */ 896 897 /* link together the chain of recipients */ 898 to->q_tchain = tochain; 899 tochain = to; 900 901 /* create list of users for error messages */ 902 (void) strcat(tobuf, ","); 903 (void) strcat(tobuf, to->q_paddr); 904 define('u', user, e); /* to user */ 905 p = to->q_home; 906 if (p == NULL && ctladdr != NULL) 907 p = ctladdr->q_home; 908 define('z', p, e); /* user's home */ 909 910 /* 911 ** Expand out this user into argument list. 912 */ 913 914 if (!clever) 915 { 916 expand(*mvp, buf, sizeof buf, e); 917 *pvp++ = newstr(buf); 918 if (pvp >= &pv[MAXPV - 2]) 919 { 920 /* allow some space for trailing parms */ 921 break; 922 } 923 } 924 } 925 926 /* see if any addresses still exist */ 927 if (tobuf[0] == '\0') 928 { 929 define('g', (char *) NULL, e); 930 return (0); 931 } 932 933 /* print out messages as full list */ 934 e->e_to = tobuf + 1; 935 936 /* 937 ** Fill out any parameters after the $u parameter. 938 */ 939 940 while (!clever && *++mvp != NULL) 941 { 942 expand(*mvp, buf, sizeof buf, e); 943 *pvp++ = newstr(buf); 944 if (pvp >= &pv[MAXPV]) 945 syserr("554 deliver: pv overflow after $u for %s", pv[0]); 946 } 947 *pvp++ = NULL; 948 949 /* 950 ** Call the mailer. 951 ** The argument vector gets built, pipes 952 ** are created as necessary, and we fork & exec as 953 ** appropriate. 954 ** If we are running SMTP, we just need to clean up. 955 */ 956 957 /*XXX this seems a bit wierd */ 958 if (ctladdr == NULL && m != ProgMailer && 959 bitset(QGOODUID, e->e_from.q_flags)) 960 ctladdr = &e->e_from; 961 962 #if NAMED_BIND 963 if (ConfigLevel < 2) 964 _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 965 #endif 966 967 if (tTd(11, 1)) 968 { 969 printf("openmailer:"); 970 printav(pv); 971 } 972 errno = 0; 973 #if NAMED_BIND 974 h_errno = 0; 975 #endif 976 977 CurHostName = NULL; 978 979 /* 980 ** Deal with the special case of mail handled through an IPC 981 ** connection. 982 ** In this case we don't actually fork. We must be 983 ** running SMTP for this to work. We will return a 984 ** zero pid to indicate that we are running IPC. 985 ** We also handle a debug version that just talks to stdin/out. 986 */ 987 988 curhost = NULL; 989 SmtpPhase = NULL; 990 mci = NULL; 991 992 #ifdef XDEBUG 993 { 994 char wbuf[MAXLINE]; 995 996 /* make absolutely certain 0, 1, and 2 are in use */ 997 sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name); 998 checkfd012(wbuf); 999 } 1000 #endif 1001 1002 /* check for 8-bit available */ 1003 if (bitset(EF_HAS8BIT, e->e_flags) && 1004 bitnset(M_7BITS, m->m_flags) && 1005 !bitset(MM_MIME8BIT, MimeMode)) 1006 { 1007 usrerr("554 Cannot send 8-bit data to 7-bit destination"); 1008 rcode = EX_DATAERR; 1009 goto give_up; 1010 } 1011 1012 /* check for Local Person Communication -- not for mortals!!! */ 1013 if (strcmp(m->m_mailer, "[LPC]") == 0) 1014 { 1015 mci = (MCI *) xalloc(sizeof *mci); 1016 bzero((char *) mci, sizeof *mci); 1017 mci->mci_in = stdin; 1018 mci->mci_out = stdout; 1019 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 1020 mci->mci_mailer = m; 1021 } 1022 else if (strcmp(m->m_mailer, "[IPC]") == 0 || 1023 strcmp(m->m_mailer, "[TCP]") == 0) 1024 { 1025 #ifdef DAEMON 1026 register int i; 1027 register u_short port = 0; 1028 1029 if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0') 1030 { 1031 syserr("null host name for %s mailer", m->m_mailer); 1032 rcode = EX_CONFIG; 1033 goto give_up; 1034 } 1035 1036 CurHostName = pv[1]; 1037 curhost = hostsignature(m, pv[1], e); 1038 1039 if (curhost == NULL || curhost[0] == '\0') 1040 { 1041 syserr("null host signature for %s", pv[1]); 1042 rcode = EX_CONFIG; 1043 goto give_up; 1044 } 1045 1046 if (!clever) 1047 { 1048 syserr("554 non-clever IPC"); 1049 rcode = EX_CONFIG; 1050 goto give_up; 1051 } 1052 if (pv[2] != NULL) 1053 port = atoi(pv[2]); 1054 tryhost: 1055 while (*curhost != '\0') 1056 { 1057 register char *p; 1058 static char hostbuf[MAXNAME + 1]; 1059 1060 /* pull the next host from the signature */ 1061 p = strchr(curhost, ':'); 1062 if (p == NULL) 1063 p = &curhost[strlen(curhost)]; 1064 if (p == curhost) 1065 { 1066 syserr("deliver: null host name in signature"); 1067 curhost++; 1068 continue; 1069 } 1070 strncpy(hostbuf, curhost, p - curhost); 1071 hostbuf[p - curhost] = '\0'; 1072 if (*p != '\0') 1073 p++; 1074 curhost = p; 1075 1076 /* see if we already know that this host is fried */ 1077 CurHostName = hostbuf; 1078 mci = mci_get(hostbuf, m); 1079 if (mci->mci_state != MCIS_CLOSED) 1080 { 1081 if (tTd(11, 1)) 1082 { 1083 printf("openmailer: "); 1084 mci_dump(mci, FALSE); 1085 } 1086 CurHostName = mci->mci_host; 1087 message("Using cached connection to %s via %s...", 1088 hostbuf, m->m_name); 1089 break; 1090 } 1091 mci->mci_mailer = m; 1092 if (mci->mci_exitstat != EX_OK) 1093 continue; 1094 1095 /* try the connection */ 1096 setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 1097 message("Connecting to %s via %s...", 1098 hostbuf, m->m_name); 1099 i = makeconnection(hostbuf, port, mci, 1100 bitnset(M_SECURE_PORT, m->m_flags)); 1101 mci->mci_exitstat = i; 1102 mci->mci_errno = errno; 1103 #if NAMED_BIND 1104 mci->mci_herrno = h_errno; 1105 #endif 1106 if (i == EX_OK) 1107 { 1108 mci->mci_state = MCIS_OPENING; 1109 mci_cache(mci); 1110 if (TrafficLogFile != NULL) 1111 fprintf(TrafficLogFile, "%05d == CONNECT %s\n", 1112 getpid(), hostbuf); 1113 break; 1114 } 1115 else if (tTd(11, 1)) 1116 printf("openmailer: makeconnection => stat=%d, errno=%d\n", 1117 i, errno); 1118 1119 /* enter status of this host */ 1120 setstat(i); 1121 1122 /* should print some message here for -v mode */ 1123 } 1124 if (mci == NULL) 1125 { 1126 syserr("deliver: no host name"); 1127 rcode = EX_OSERR; 1128 goto give_up; 1129 } 1130 mci->mci_pid = 0; 1131 #else /* no DAEMON */ 1132 syserr("554 openmailer: no IPC"); 1133 if (tTd(11, 1)) 1134 printf("openmailer: NULL\n"); 1135 rcode = EX_UNAVAILABLE; 1136 goto give_up; 1137 #endif /* DAEMON */ 1138 } 1139 else 1140 { 1141 /* flush any expired connections */ 1142 (void) mci_scan(NULL); 1143 1144 /* announce the connection to verbose listeners */ 1145 if (host == NULL || host[0] == '\0') 1146 message("Connecting to %s...", m->m_name); 1147 else 1148 message("Connecting to %s via %s...", host, m->m_name); 1149 if (TrafficLogFile != NULL) 1150 { 1151 char **av; 1152 1153 fprintf(TrafficLogFile, "%05d === EXEC", getpid()); 1154 for (av = pv; *av != NULL; av++) 1155 fprintf(TrafficLogFile, " %s", *av); 1156 fprintf(TrafficLogFile, "\n"); 1157 } 1158 1159 /* create a pipe to shove the mail through */ 1160 if (pipe(mpvect) < 0) 1161 { 1162 syserr("%s... openmailer(%s): pipe (to mailer)", 1163 e->e_to, m->m_name); 1164 if (tTd(11, 1)) 1165 printf("openmailer: NULL\n"); 1166 rcode = EX_OSERR; 1167 goto give_up; 1168 } 1169 1170 /* if this mailer speaks smtp, create a return pipe */ 1171 if (clever && pipe(rpvect) < 0) 1172 { 1173 syserr("%s... openmailer(%s): pipe (from mailer)", 1174 e->e_to, m->m_name); 1175 (void) close(mpvect[0]); 1176 (void) close(mpvect[1]); 1177 if (tTd(11, 1)) 1178 printf("openmailer: NULL\n"); 1179 rcode = EX_OSERR; 1180 goto give_up; 1181 } 1182 1183 /* 1184 ** Actually fork the mailer process. 1185 ** DOFORK is clever about retrying. 1186 ** 1187 ** Dispose of SIGCHLD signal catchers that may be laying 1188 ** around so that endmail will get it. 1189 */ 1190 1191 if (e->e_xfp != NULL) 1192 (void) fflush(e->e_xfp); /* for debugging */ 1193 (void) fflush(stdout); 1194 # ifdef SIGCHLD 1195 (void) setsignal(SIGCHLD, SIG_DFL); 1196 # endif /* SIGCHLD */ 1197 DOFORK(FORK); 1198 /* pid is set by DOFORK */ 1199 if (pid < 0) 1200 { 1201 /* failure */ 1202 syserr("%s... openmailer(%s): cannot fork", 1203 e->e_to, m->m_name); 1204 (void) close(mpvect[0]); 1205 (void) close(mpvect[1]); 1206 if (clever) 1207 { 1208 (void) close(rpvect[0]); 1209 (void) close(rpvect[1]); 1210 } 1211 if (tTd(11, 1)) 1212 printf("openmailer: NULL\n"); 1213 rcode = EX_OSERR; 1214 goto give_up; 1215 } 1216 else if (pid == 0) 1217 { 1218 int i; 1219 int saveerrno; 1220 char **ep; 1221 char *env[MAXUSERENVIRON]; 1222 extern char **environ; 1223 extern int DtableSize; 1224 1225 if (e->e_lockfp != NULL) 1226 { 1227 fclose(e->e_lockfp); 1228 e->e_lockfp = NULL; 1229 } 1230 1231 /* child -- set up input & exec mailer */ 1232 (void) setsignal(SIGINT, SIG_IGN); 1233 (void) setsignal(SIGHUP, SIG_IGN); 1234 (void) setsignal(SIGTERM, SIG_DFL); 1235 1236 /* reset user and group */ 1237 if (bitnset(M_SPECIFIC_UID, m->m_flags)) 1238 { 1239 (void) setgid(m->m_gid); 1240 (void) setuid(m->m_uid); 1241 } 1242 else if (ctladdr != NULL && ctladdr->q_uid != 0) 1243 { 1244 (void) initgroups(ctladdr->q_ruser? 1245 ctladdr->q_ruser: ctladdr->q_user, 1246 ctladdr->q_gid); 1247 (void) setgid(ctladdr->q_gid); 1248 (void) setuid(ctladdr->q_uid); 1249 } 1250 else 1251 { 1252 (void) initgroups(DefUser, DefGid); 1253 if (m->m_gid == 0) 1254 (void) setgid(DefGid); 1255 else 1256 (void) setgid(m->m_gid); 1257 if (m->m_uid == 0) 1258 (void) setuid(DefUid); 1259 else 1260 (void) setuid(m->m_uid); 1261 } 1262 1263 if (tTd(11, 2)) 1264 printf("openmailer: running as r/euid=%d/%d\n", 1265 getuid(), geteuid()); 1266 1267 /* move into some "safe" directory */ 1268 if (m->m_execdir != NULL) 1269 { 1270 char *p, *q; 1271 char buf[MAXLINE + 1]; 1272 1273 for (p = m->m_execdir; p != NULL; p = q) 1274 { 1275 q = strchr(p, ':'); 1276 if (q != NULL) 1277 *q = '\0'; 1278 expand(p, buf, sizeof buf, e); 1279 if (q != NULL) 1280 *q++ = ':'; 1281 if (tTd(11, 20)) 1282 printf("openmailer: trydir %s\n", 1283 buf); 1284 if (buf[0] != '\0' && chdir(buf) >= 0) 1285 break; 1286 } 1287 } 1288 1289 /* arrange to filter std & diag output of command */ 1290 if (clever) 1291 { 1292 (void) close(rpvect[0]); 1293 if (dup2(rpvect[1], STDOUT_FILENO) < 0) 1294 { 1295 syserr("%s... openmailer(%s): cannot dup pipe %d for stdout", 1296 e->e_to, m->m_name, rpvect[1]); 1297 _exit(EX_OSERR); 1298 } 1299 (void) close(rpvect[1]); 1300 } 1301 else if (OpMode == MD_SMTP || OpMode == MD_DAEMON || 1302 HoldErrs || DisConnected) 1303 { 1304 /* put mailer output in transcript */ 1305 if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0) 1306 { 1307 syserr("%s... openmailer(%s): cannot dup xscript %d for stdout", 1308 e->e_to, m->m_name, 1309 fileno(e->e_xfp)); 1310 _exit(EX_OSERR); 1311 } 1312 } 1313 if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) 1314 { 1315 syserr("%s... openmailer(%s): cannot dup stdout for stderr", 1316 e->e_to, m->m_name); 1317 _exit(EX_OSERR); 1318 } 1319 1320 /* arrange to get standard input */ 1321 (void) close(mpvect[1]); 1322 if (dup2(mpvect[0], STDIN_FILENO) < 0) 1323 { 1324 syserr("%s... openmailer(%s): cannot dup pipe %d for stdin", 1325 e->e_to, m->m_name, mpvect[0]); 1326 _exit(EX_OSERR); 1327 } 1328 (void) close(mpvect[0]); 1329 1330 /* arrange for all the files to be closed */ 1331 for (i = 3; i < DtableSize; i++) 1332 { 1333 register int j; 1334 1335 if ((j = fcntl(i, F_GETFD, 0)) != -1) 1336 (void) fcntl(i, F_SETFD, j | 1); 1337 } 1338 1339 /* 1340 ** Set up the mailer environment 1341 ** _FORCE_MAIL_LOCAL_ is DG-UX equiv of -d flag. 1342 ** TZ is timezone information. 1343 ** SYSTYPE is Apollo software sys type (required). 1344 ** ISP is Apollo hardware system type (required). 1345 */ 1346 1347 i = 0; 1348 env[i++] = "AGENT=sendmail"; 1349 env[i++] = "_FORCE_MAIL_LOCAL_=yes"; 1350 for (ep = environ; *ep != NULL; ep++) 1351 { 1352 if (strncmp(*ep, "TZ=", 3) == 0 || 1353 strncmp(*ep, "ISP=", 4) == 0 || 1354 strncmp(*ep, "SYSTYPE=", 8) == 0) 1355 env[i++] = *ep; 1356 } 1357 env[i] = NULL; 1358 1359 /* run disconnected from terminal */ 1360 (void) setsid(); 1361 1362 /* try to execute the mailer */ 1363 execve(m->m_mailer, pv, env); 1364 saveerrno = errno; 1365 syserr("Cannot exec %s", m->m_mailer); 1366 if (bitnset(M_LOCALMAILER, m->m_flags) || 1367 transienterror(saveerrno)) 1368 _exit(EX_OSERR); 1369 _exit(EX_UNAVAILABLE); 1370 } 1371 1372 /* 1373 ** Set up return value. 1374 */ 1375 1376 mci = (MCI *) xalloc(sizeof *mci); 1377 bzero((char *) mci, sizeof *mci); 1378 mci->mci_mailer = m; 1379 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 1380 mci->mci_pid = pid; 1381 (void) close(mpvect[0]); 1382 mci->mci_out = fdopen(mpvect[1], "w"); 1383 if (mci->mci_out == NULL) 1384 { 1385 syserr("deliver: cannot create mailer output channel, fd=%d", 1386 mpvect[1]); 1387 (void) close(mpvect[1]); 1388 if (clever) 1389 { 1390 (void) close(rpvect[0]); 1391 (void) close(rpvect[1]); 1392 } 1393 rcode = EX_OSERR; 1394 goto give_up; 1395 } 1396 if (clever) 1397 { 1398 (void) close(rpvect[1]); 1399 mci->mci_in = fdopen(rpvect[0], "r"); 1400 if (mci->mci_in == NULL) 1401 { 1402 syserr("deliver: cannot create mailer input channel, fd=%d", 1403 mpvect[1]); 1404 (void) close(rpvect[0]); 1405 fclose(mci->mci_out); 1406 mci->mci_out = NULL; 1407 rcode = EX_OSERR; 1408 goto give_up; 1409 } 1410 } 1411 else 1412 { 1413 mci->mci_flags |= MCIF_TEMP; 1414 mci->mci_in = NULL; 1415 } 1416 } 1417 1418 /* 1419 ** If we are in SMTP opening state, send initial protocol. 1420 */ 1421 1422 if (clever && mci->mci_state != MCIS_CLOSED) 1423 { 1424 smtpinit(m, mci, e); 1425 } 1426 1427 if (bitset(EF_HAS8BIT, e->e_flags) && bitnset(M_7BITS, m->m_flags)) 1428 mci->mci_flags |= MCIF_CVT8TO7; 1429 else 1430 mci->mci_flags &= ~MCIF_CVT8TO7; 1431 1432 if (tTd(11, 1)) 1433 { 1434 printf("openmailer: "); 1435 mci_dump(mci, FALSE); 1436 } 1437 1438 if (mci->mci_state != MCIS_OPEN) 1439 { 1440 /* couldn't open the mailer */ 1441 rcode = mci->mci_exitstat; 1442 errno = mci->mci_errno; 1443 #if NAMED_BIND 1444 h_errno = mci->mci_herrno; 1445 #endif 1446 if (rcode == EX_OK) 1447 { 1448 /* shouldn't happen */ 1449 syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 1450 rcode, mci->mci_state, firstsig); 1451 rcode = EX_SOFTWARE; 1452 } 1453 else if (curhost != NULL && *curhost != '\0') 1454 { 1455 /* try next MX site */ 1456 goto tryhost; 1457 } 1458 } 1459 else if (!clever) 1460 { 1461 /* 1462 ** Format and send message. 1463 */ 1464 1465 putfromline(mci, e); 1466 (*e->e_puthdr)(mci, e->e_header, e); 1467 (*e->e_putbody)(mci, e, NULL); 1468 1469 /* get the exit status */ 1470 rcode = endmailer(mci, e, pv); 1471 } 1472 else 1473 #ifdef SMTP 1474 { 1475 /* 1476 ** Send the MAIL FROM: protocol 1477 */ 1478 1479 rcode = smtpmailfrom(m, mci, e); 1480 if (rcode == EX_OK) 1481 { 1482 register char *t = tobuf; 1483 register int i; 1484 1485 /* send the recipient list */ 1486 tobuf[0] = '\0'; 1487 for (to = tochain; to != NULL; to = to->q_tchain) 1488 { 1489 e->e_to = to->q_paddr; 1490 if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 1491 { 1492 markfailure(e, to, mci, i); 1493 giveresponse(i, m, mci, ctladdr, xstart, e); 1494 } 1495 else 1496 { 1497 *t++ = ','; 1498 for (p = to->q_paddr; *p; *t++ = *p++) 1499 continue; 1500 *t = '\0'; 1501 } 1502 } 1503 1504 /* now send the data */ 1505 if (tobuf[0] == '\0') 1506 { 1507 rcode = EX_OK; 1508 e->e_to = NULL; 1509 if (bitset(MCIF_CACHED, mci->mci_flags)) 1510 smtprset(m, mci, e); 1511 } 1512 else 1513 { 1514 e->e_to = tobuf + 1; 1515 rcode = smtpdata(m, mci, e); 1516 } 1517 1518 /* now close the connection */ 1519 if (!bitset(MCIF_CACHED, mci->mci_flags)) 1520 smtpquit(m, mci, e); 1521 } 1522 if (rcode != EX_OK && curhost != NULL && *curhost != '\0') 1523 { 1524 /* try next MX site */ 1525 goto tryhost; 1526 } 1527 } 1528 #else /* not SMTP */ 1529 { 1530 syserr("554 deliver: need SMTP compiled to use clever mailer"); 1531 rcode = EX_CONFIG; 1532 goto give_up; 1533 } 1534 #endif /* SMTP */ 1535 #if NAMED_BIND 1536 if (ConfigLevel < 2) 1537 _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 1538 #endif 1539 1540 /* arrange a return receipt if requested */ 1541 if (rcode == EX_OK && e->e_receiptto != NULL && 1542 bitnset(M_LOCALMAILER, m->m_flags)) 1543 { 1544 e->e_flags |= EF_SENDRECEIPT; 1545 /* do we want to send back more info? */ 1546 } 1547 1548 /* 1549 ** Do final status disposal. 1550 ** We check for something in tobuf for the SMTP case. 1551 ** If we got a temporary failure, arrange to queue the 1552 ** addressees. 1553 */ 1554 1555 give_up: 1556 if (tobuf[0] != '\0') 1557 giveresponse(rcode, m, mci, ctladdr, xstart, e); 1558 for (to = tochain; to != NULL; to = to->q_tchain) 1559 { 1560 if (rcode != EX_OK) 1561 markfailure(e, to, mci, rcode); 1562 else if (!bitset(QBADADDR|QQUEUEUP, to->q_flags)) 1563 { 1564 to->q_flags |= QSENT; 1565 to->q_statdate = curtime(); 1566 e->e_nsent++; 1567 if (bitnset(M_LOCALMAILER, m->m_flags) && 1568 (e->e_receiptto != NULL || 1569 bitset(QPINGONSUCCESS, to->q_flags))) 1570 { 1571 to->q_flags |= QREPORT; 1572 fprintf(e->e_xfp, "%s... Successfully delivered\n", 1573 to->q_paddr); 1574 } 1575 else if (bitset(QPINGONSUCCESS, 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] != NULL) 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