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