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