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