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