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.39 (Berkeley) 11/09/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) setuid(DefUid); 1132 } 1133 else 1134 { 1135 (void) initgroups(ctladdr->q_ruser? 1136 ctladdr->q_ruser: ctladdr->q_user, 1137 ctladdr->q_gid); 1138 (void) setuid(ctladdr->q_uid); 1139 } 1140 } 1141 1142 if (tTd(11, 2)) 1143 printf("openmailer: running as r/euid=%d/%d\n", 1144 getuid(), geteuid()); 1145 1146 /* move into some "safe" directory */ 1147 if (m->m_execdir != NULL) 1148 { 1149 char *p, *q; 1150 char buf[MAXLINE]; 1151 1152 for (p = m->m_execdir; p != NULL; p = q) 1153 { 1154 q = strchr(p, ':'); 1155 if (q != NULL) 1156 *q = '\0'; 1157 expand(p, buf, &buf[sizeof buf] - 1, e); 1158 if (q != NULL) 1159 *q++ = ':'; 1160 if (tTd(11, 20)) 1161 printf("openmailer: trydir %s\n", 1162 buf); 1163 if (buf[0] != '\0' && chdir(buf) >= 0) 1164 break; 1165 } 1166 } 1167 1168 /* arrange to filter std & diag output of command */ 1169 if (clever) 1170 { 1171 (void) close(rpvect[0]); 1172 if (dup2(rpvect[1], STDOUT_FILENO) < 0) 1173 { 1174 syserr("%s... openmailer(%s): cannot dup pipe %d for stdout", 1175 e->e_to, m->m_name, rpvect[1]); 1176 _exit(EX_OSERR); 1177 } 1178 (void) close(rpvect[1]); 1179 } 1180 else if (OpMode == MD_SMTP || HoldErrs) 1181 { 1182 /* put mailer output in transcript */ 1183 if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0) 1184 { 1185 syserr("%s... openmailer(%s): cannot dup xscript %d for stdout", 1186 e->e_to, m->m_name, 1187 fileno(e->e_xfp)); 1188 _exit(EX_OSERR); 1189 } 1190 } 1191 if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) 1192 { 1193 syserr("%s... openmailer(%s): cannot dup stdout for stderr", 1194 e->e_to, m->m_name); 1195 _exit(EX_OSERR); 1196 } 1197 1198 /* arrange to get standard input */ 1199 (void) close(mpvect[1]); 1200 if (dup2(mpvect[0], STDIN_FILENO) < 0) 1201 { 1202 syserr("%s... openmailer(%s): cannot dup pipe %d for stdin", 1203 e->e_to, m->m_name, mpvect[0]); 1204 _exit(EX_OSERR); 1205 } 1206 (void) close(mpvect[0]); 1207 1208 /* arrange for all the files to be closed */ 1209 for (i = 3; i < DtableSize; i++) 1210 { 1211 register int j; 1212 1213 if ((j = fcntl(i, F_GETFD, 0)) != -1) 1214 (void) fcntl(i, F_SETFD, j | 1); 1215 } 1216 1217 /* set up the mailer environment */ 1218 i = 0; 1219 env[i++] = "AGENT=sendmail"; 1220 for (ep = environ; *ep != NULL; ep++) 1221 { 1222 if (strncmp(*ep, "TZ=", 3) == 0) 1223 env[i++] = *ep; 1224 } 1225 env[i++] = NULL; 1226 1227 /* try to execute the mailer */ 1228 execve(m->m_mailer, pv, env); 1229 saveerrno = errno; 1230 syserr("Cannot exec %s", m->m_mailer); 1231 if (m == LocalMailer || transienterror(saveerrno)) 1232 _exit(EX_OSERR); 1233 _exit(EX_UNAVAILABLE); 1234 } 1235 1236 /* 1237 ** Set up return value. 1238 */ 1239 1240 mci = (MCI *) xalloc(sizeof *mci); 1241 bzero((char *) mci, sizeof *mci); 1242 mci->mci_mailer = m; 1243 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 1244 mci->mci_pid = pid; 1245 (void) close(mpvect[0]); 1246 mci->mci_out = fdopen(mpvect[1], "w"); 1247 if (mci->mci_out == NULL) 1248 { 1249 syserr("deliver: cannot create mailer output channel, fd=%d", 1250 mpvect[1]); 1251 (void) close(mpvect[1]); 1252 if (clever) 1253 { 1254 (void) close(rpvect[0]); 1255 (void) close(rpvect[1]); 1256 } 1257 rcode = EX_OSERR; 1258 goto give_up; 1259 } 1260 if (clever) 1261 { 1262 (void) close(rpvect[1]); 1263 mci->mci_in = fdopen(rpvect[0], "r"); 1264 if (mci->mci_in == NULL) 1265 { 1266 syserr("deliver: cannot create mailer input channel, fd=%d", 1267 mpvect[1]); 1268 (void) close(rpvect[0]); 1269 fclose(mci->mci_out); 1270 mci->mci_out = NULL; 1271 rcode = EX_OSERR; 1272 goto give_up; 1273 } 1274 } 1275 else 1276 { 1277 mci->mci_flags |= MCIF_TEMP; 1278 mci->mci_in = NULL; 1279 } 1280 } 1281 1282 /* 1283 ** If we are in SMTP opening state, send initial protocol. 1284 */ 1285 1286 if (clever && mci->mci_state != MCIS_CLOSED) 1287 { 1288 smtpinit(m, mci, e); 1289 } 1290 if (tTd(11, 1)) 1291 { 1292 printf("openmailer: "); 1293 mci_dump(mci, FALSE); 1294 } 1295 1296 if (mci->mci_state != MCIS_OPEN) 1297 { 1298 /* couldn't open the mailer */ 1299 rcode = mci->mci_exitstat; 1300 errno = mci->mci_errno; 1301 #ifdef NAMED_BIND 1302 h_errno = mci->mci_herrno; 1303 #endif 1304 if (rcode == EX_OK) 1305 { 1306 /* shouldn't happen */ 1307 syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 1308 rcode, mci->mci_state, firstsig); 1309 rcode = EX_SOFTWARE; 1310 } 1311 else if (rcode == EX_TEMPFAIL && *curhost != '\0') 1312 { 1313 /* try next MX site */ 1314 goto tryhost; 1315 } 1316 } 1317 else if (!clever) 1318 { 1319 /* 1320 ** Format and send message. 1321 */ 1322 1323 putfromline(mci->mci_out, m, e); 1324 (*e->e_puthdr)(mci->mci_out, m, e); 1325 putline("\n", mci->mci_out, m); 1326 (*e->e_putbody)(mci->mci_out, m, e, NULL); 1327 1328 /* get the exit status */ 1329 rcode = endmailer(mci, e, pv); 1330 } 1331 else 1332 #ifdef SMTP 1333 { 1334 /* 1335 ** Send the MAIL FROM: protocol 1336 */ 1337 1338 rcode = smtpmailfrom(m, mci, e); 1339 if (rcode == EX_OK) 1340 { 1341 register char *t = tobuf; 1342 register int i; 1343 1344 /* send the recipient list */ 1345 tobuf[0] = '\0'; 1346 for (to = tochain; to != NULL; to = to->q_tchain) 1347 { 1348 e->e_to = to->q_paddr; 1349 if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 1350 { 1351 markfailure(e, to, i); 1352 giveresponse(i, m, mci, ctladdr, e); 1353 } 1354 else 1355 { 1356 *t++ = ','; 1357 for (p = to->q_paddr; *p; *t++ = *p++) 1358 continue; 1359 *t = '\0'; 1360 } 1361 } 1362 1363 /* now send the data */ 1364 if (tobuf[0] == '\0') 1365 { 1366 rcode = EX_OK; 1367 e->e_to = NULL; 1368 if (bitset(MCIF_CACHED, mci->mci_flags)) 1369 smtprset(m, mci, e); 1370 } 1371 else 1372 { 1373 e->e_to = tobuf + 1; 1374 rcode = smtpdata(m, mci, e); 1375 } 1376 1377 /* now close the connection */ 1378 if (!bitset(MCIF_CACHED, mci->mci_flags)) 1379 smtpquit(m, mci, e); 1380 } 1381 if (rcode != EX_OK && *curhost != '\0') 1382 { 1383 /* try next MX site */ 1384 goto tryhost; 1385 } 1386 } 1387 #else /* not SMTP */ 1388 { 1389 syserr("554 deliver: need SMTP compiled to use clever mailer"); 1390 rcode = EX_CONFIG; 1391 goto give_up; 1392 } 1393 #endif /* SMTP */ 1394 #ifdef NAMED_BIND 1395 if (ConfigLevel < 2) 1396 _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 1397 #endif 1398 1399 /* arrange a return receipt if requested */ 1400 if (rcode == EX_OK && e->e_receiptto != NULL && 1401 bitnset(M_LOCALMAILER, m->m_flags)) 1402 { 1403 e->e_flags |= EF_SENDRECEIPT; 1404 /* do we want to send back more info? */ 1405 } 1406 1407 /* 1408 ** Do final status disposal. 1409 ** We check for something in tobuf for the SMTP case. 1410 ** If we got a temporary failure, arrange to queue the 1411 ** addressees. 1412 */ 1413 1414 give_up: 1415 if (tobuf[0] != '\0') 1416 giveresponse(rcode, m, mci, ctladdr, e); 1417 for (to = tochain; to != NULL; to = to->q_tchain) 1418 { 1419 if (rcode != EX_OK) 1420 markfailure(e, to, rcode); 1421 else 1422 { 1423 to->q_flags |= QSENT; 1424 e->e_nsent++; 1425 if (e->e_receiptto != NULL && 1426 bitnset(M_LOCALMAILER, m->m_flags)) 1427 { 1428 fprintf(e->e_xfp, "%s... Successfully delivered\n", 1429 to->q_paddr); 1430 } 1431 } 1432 } 1433 1434 /* 1435 ** Restore state and return. 1436 */ 1437 1438 #ifdef XDEBUG 1439 { 1440 char wbuf[MAXLINE]; 1441 1442 /* make absolutely certain 0, 1, and 2 are in use */ 1443 sprintf(wbuf, "%s... end of deliver(%s)", 1444 e->e_to == NULL ? "NO-TO-LIST" : e->e_to, 1445 m->m_name); 1446 checkfd012(wbuf); 1447 } 1448 #endif 1449 1450 errno = 0; 1451 define('g', (char *) NULL, e); 1452 return (rcode); 1453 } 1454 /* 1455 ** MARKFAILURE -- mark a failure on a specific address. 1456 ** 1457 ** Parameters: 1458 ** e -- the envelope we are sending. 1459 ** q -- the address to mark. 1460 ** rcode -- the code signifying the particular failure. 1461 ** 1462 ** Returns: 1463 ** none. 1464 ** 1465 ** Side Effects: 1466 ** marks the address (and possibly the envelope) with the 1467 ** failure so that an error will be returned or 1468 ** the message will be queued, as appropriate. 1469 */ 1470 1471 markfailure(e, q, rcode) 1472 register ENVELOPE *e; 1473 register ADDRESS *q; 1474 int rcode; 1475 { 1476 char buf[MAXLINE]; 1477 1478 if (rcode == EX_OK) 1479 return; 1480 else if (rcode == EX_TEMPFAIL) 1481 q->q_flags |= QQUEUEUP; 1482 else if (rcode != EX_IOERR && rcode != EX_OSERR) 1483 q->q_flags |= QBADADDR; 1484 } 1485 /* 1486 ** ENDMAILER -- Wait for mailer to terminate. 1487 ** 1488 ** We should never get fatal errors (e.g., segmentation 1489 ** violation), so we report those specially. For other 1490 ** errors, we choose a status message (into statmsg), 1491 ** and if it represents an error, we print it. 1492 ** 1493 ** Parameters: 1494 ** pid -- pid of mailer. 1495 ** e -- the current envelope. 1496 ** pv -- the parameter vector that invoked the mailer 1497 ** (for error messages). 1498 ** 1499 ** Returns: 1500 ** exit code of mailer. 1501 ** 1502 ** Side Effects: 1503 ** none. 1504 */ 1505 1506 endmailer(mci, e, pv) 1507 register MCI *mci; 1508 register ENVELOPE *e; 1509 char **pv; 1510 { 1511 int st; 1512 1513 /* close any connections */ 1514 if (mci->mci_in != NULL) 1515 (void) xfclose(mci->mci_in, pv[0], "mci_in"); 1516 if (mci->mci_out != NULL) 1517 (void) xfclose(mci->mci_out, pv[0], "mci_out"); 1518 mci->mci_in = mci->mci_out = NULL; 1519 mci->mci_state = MCIS_CLOSED; 1520 1521 /* in the IPC case there is nothing to wait for */ 1522 if (mci->mci_pid == 0) 1523 return (EX_OK); 1524 1525 /* wait for the mailer process to die and collect status */ 1526 st = waitfor(mci->mci_pid); 1527 if (st == -1) 1528 { 1529 syserr("endmailer %s: wait", pv[0]); 1530 return (EX_SOFTWARE); 1531 } 1532 1533 if (WIFEXITED(st)) 1534 { 1535 /* normal death -- return status */ 1536 return (WEXITSTATUS(st)); 1537 } 1538 1539 /* it died a horrid death */ 1540 syserr("mailer %s died with signal %o", pv[0], st); 1541 1542 /* log the arguments */ 1543 if (e->e_xfp != NULL) 1544 { 1545 register char **av; 1546 1547 fprintf(e->e_xfp, "Arguments:"); 1548 for (av = pv; *av != NULL; av++) 1549 fprintf(e->e_xfp, " %s", *av); 1550 fprintf(e->e_xfp, "\n"); 1551 } 1552 1553 ExitStat = EX_TEMPFAIL; 1554 return (EX_TEMPFAIL); 1555 } 1556 /* 1557 ** GIVERESPONSE -- Interpret an error response from a mailer 1558 ** 1559 ** Parameters: 1560 ** stat -- the status code from the mailer (high byte 1561 ** only; core dumps must have been taken care of 1562 ** already). 1563 ** m -- the mailer info for this mailer. 1564 ** mci -- the mailer connection info -- can be NULL if the 1565 ** response is given before the connection is made. 1566 ** ctladdr -- the controlling address for the recipient 1567 ** address(es). 1568 ** e -- the current envelope. 1569 ** 1570 ** Returns: 1571 ** none. 1572 ** 1573 ** Side Effects: 1574 ** Errors may be incremented. 1575 ** ExitStat may be set. 1576 */ 1577 1578 giveresponse(stat, m, mci, ctladdr, e) 1579 int stat; 1580 register MAILER *m; 1581 register MCI *mci; 1582 ADDRESS *ctladdr; 1583 ENVELOPE *e; 1584 { 1585 register const char *statmsg; 1586 extern char *SysExMsg[]; 1587 register int i; 1588 extern int N_SysEx; 1589 char buf[MAXLINE]; 1590 1591 /* 1592 ** Compute status message from code. 1593 */ 1594 1595 i = stat - EX__BASE; 1596 if (stat == 0) 1597 { 1598 statmsg = "250 Sent"; 1599 if (e->e_statmsg != NULL) 1600 { 1601 (void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg); 1602 statmsg = buf; 1603 } 1604 } 1605 else if (i < 0 || i > N_SysEx) 1606 { 1607 (void) sprintf(buf, "554 unknown mailer error %d", stat); 1608 stat = EX_UNAVAILABLE; 1609 statmsg = buf; 1610 } 1611 else if (stat == EX_TEMPFAIL) 1612 { 1613 (void) strcpy(buf, SysExMsg[i] + 1); 1614 #ifdef NAMED_BIND 1615 if (h_errno == TRY_AGAIN) 1616 statmsg = errstring(h_errno+E_DNSBASE); 1617 else 1618 #endif 1619 { 1620 if (errno != 0) 1621 statmsg = errstring(errno); 1622 else 1623 { 1624 #ifdef SMTP 1625 extern char SmtpError[]; 1626 1627 statmsg = SmtpError; 1628 #else /* SMTP */ 1629 statmsg = NULL; 1630 #endif /* SMTP */ 1631 } 1632 } 1633 if (statmsg != NULL && statmsg[0] != '\0') 1634 { 1635 (void) strcat(buf, ": "); 1636 (void) strcat(buf, statmsg); 1637 } 1638 statmsg = buf; 1639 } 1640 #ifdef NAMED_BIND 1641 else if (stat == EX_NOHOST && h_errno != 0) 1642 { 1643 statmsg = errstring(h_errno + E_DNSBASE); 1644 (void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg); 1645 statmsg = buf; 1646 } 1647 #endif 1648 else 1649 { 1650 statmsg = SysExMsg[i]; 1651 if (*statmsg++ == ':') 1652 { 1653 (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 1654 statmsg = buf; 1655 } 1656 } 1657 1658 /* 1659 ** Print the message as appropriate 1660 */ 1661 1662 if (stat == EX_OK || stat == EX_TEMPFAIL) 1663 { 1664 extern char MsgBuf[]; 1665 1666 message(&statmsg[4], errstring(errno)); 1667 if (stat == EX_TEMPFAIL && e->e_xfp != NULL) 1668 fprintf(e->e_xfp, "%s\n", &MsgBuf[4]); 1669 } 1670 else 1671 { 1672 Errors++; 1673 usrerr(statmsg, errstring(errno)); 1674 } 1675 1676 /* 1677 ** Final cleanup. 1678 ** Log a record of the transaction. Compute the new 1679 ** ExitStat -- if we already had an error, stick with 1680 ** that. 1681 */ 1682 1683 if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 1684 logdelivery(m, mci, &statmsg[4], ctladdr, e); 1685 1686 if (stat != EX_TEMPFAIL) 1687 setstat(stat); 1688 if (stat != EX_OK) 1689 { 1690 if (e->e_message != NULL) 1691 free(e->e_message); 1692 e->e_message = newstr(&statmsg[4]); 1693 } 1694 errno = 0; 1695 #ifdef NAMED_BIND 1696 h_errno = 0; 1697 #endif 1698 } 1699 /* 1700 ** LOGDELIVERY -- log the delivery in the system log 1701 ** 1702 ** Parameters: 1703 ** m -- the mailer info. Can be NULL for initial queue. 1704 ** mci -- the mailer connection info -- can be NULL if the 1705 ** log is occuring when no connection is active. 1706 ** stat -- the message to print for the status. 1707 ** ctladdr -- the controlling address for the to list. 1708 ** e -- the current envelope. 1709 ** 1710 ** Returns: 1711 ** none 1712 ** 1713 ** Side Effects: 1714 ** none 1715 */ 1716 1717 logdelivery(m, mci, stat, ctladdr, e) 1718 MAILER *m; 1719 register MCI *mci; 1720 char *stat; 1721 ADDRESS *ctladdr; 1722 register ENVELOPE *e; 1723 { 1724 # ifdef LOG 1725 register char *bp; 1726 char buf[512]; 1727 1728 bp = buf; 1729 if (ctladdr != NULL) 1730 { 1731 strcpy(bp, ", ctladdr="); 1732 strcat(bp, ctladdr->q_paddr); 1733 bp += strlen(bp); 1734 if (bitset(QGOODUID, ctladdr->q_flags)) 1735 { 1736 (void) sprintf(bp, " (%d/%d)", 1737 ctladdr->q_uid, ctladdr->q_gid); 1738 bp += strlen(bp); 1739 } 1740 } 1741 1742 (void) sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 1743 bp += strlen(bp); 1744 1745 if (m != NULL) 1746 { 1747 (void) strcpy(bp, ", mailer="); 1748 (void) strcat(bp, m->m_name); 1749 bp += strlen(bp); 1750 } 1751 1752 if (mci != NULL && mci->mci_host != NULL) 1753 { 1754 # ifdef DAEMON 1755 extern SOCKADDR CurHostAddr; 1756 # endif 1757 1758 (void) strcpy(bp, ", relay="); 1759 (void) strcat(bp, mci->mci_host); 1760 1761 # ifdef DAEMON 1762 (void) strcat(bp, " ("); 1763 (void) strcat(bp, anynet_ntoa(&CurHostAddr)); 1764 (void) strcat(bp, ")"); 1765 # endif 1766 } 1767 else 1768 { 1769 char *p = macvalue('h', e); 1770 1771 if (p != NULL && p[0] != '\0') 1772 { 1773 (void) strcpy(bp, ", relay="); 1774 (void) strcat(bp, p); 1775 } 1776 } 1777 1778 syslog(LOG_INFO, "%s: to=%s%s, stat=%s", 1779 e->e_id, e->e_to, buf, stat); 1780 # endif /* LOG */ 1781 } 1782 /* 1783 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 1784 ** 1785 ** This can be made an arbitrary message separator by changing $l 1786 ** 1787 ** One of the ugliest hacks seen by human eyes is contained herein: 1788 ** UUCP wants those stupid "remote from <host>" lines. Why oh why 1789 ** does a well-meaning programmer such as myself have to deal with 1790 ** this kind of antique garbage???? 1791 ** 1792 ** Parameters: 1793 ** fp -- the file to output to. 1794 ** m -- the mailer describing this entry. 1795 ** 1796 ** Returns: 1797 ** none 1798 ** 1799 ** Side Effects: 1800 ** outputs some text to fp. 1801 */ 1802 1803 putfromline(fp, m, e) 1804 register FILE *fp; 1805 register MAILER *m; 1806 ENVELOPE *e; 1807 { 1808 char *template = "\201l\n"; 1809 char buf[MAXLINE]; 1810 1811 if (bitnset(M_NHDR, m->m_flags)) 1812 return; 1813 1814 # ifdef UGLYUUCP 1815 if (bitnset(M_UGLYUUCP, m->m_flags)) 1816 { 1817 char *bang; 1818 char xbuf[MAXLINE]; 1819 1820 expand("\201g", buf, &buf[sizeof buf - 1], e); 1821 bang = strchr(buf, '!'); 1822 if (bang == NULL) 1823 { 1824 errno = 0; 1825 syserr("554 No ! in UUCP From address! (%s given)", buf); 1826 } 1827 else 1828 { 1829 *bang++ = '\0'; 1830 (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 1831 template = xbuf; 1832 } 1833 } 1834 # endif /* UGLYUUCP */ 1835 expand(template, buf, &buf[sizeof buf - 1], e); 1836 putline(buf, fp, m); 1837 } 1838 /* 1839 ** PUTBODY -- put the body of a message. 1840 ** 1841 ** Parameters: 1842 ** fp -- file to output onto. 1843 ** m -- a mailer descriptor to control output format. 1844 ** e -- the envelope to put out. 1845 ** separator -- if non-NULL, a message separator that must 1846 ** not be permitted in the resulting message. 1847 ** 1848 ** Returns: 1849 ** none. 1850 ** 1851 ** Side Effects: 1852 ** The message is written onto fp. 1853 */ 1854 1855 putbody(fp, m, e, separator) 1856 FILE *fp; 1857 MAILER *m; 1858 register ENVELOPE *e; 1859 char *separator; 1860 { 1861 char buf[MAXLINE]; 1862 1863 /* 1864 ** Output the body of the message 1865 */ 1866 1867 if (e->e_dfp == NULL) 1868 { 1869 if (e->e_df != NULL) 1870 { 1871 e->e_dfp = fopen(e->e_df, "r"); 1872 if (e->e_dfp == NULL) 1873 syserr("putbody: Cannot open %s for %s from %s", 1874 e->e_df, e->e_to, e->e_from.q_paddr); 1875 } 1876 else 1877 putline("<<< No Message Collected >>>", fp, m); 1878 } 1879 if (e->e_dfp != NULL) 1880 { 1881 rewind(e->e_dfp); 1882 while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 1883 { 1884 if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1885 strncmp(buf, "From ", 5) == 0) 1886 (void) putc('>', fp); 1887 if (buf[0] == '-' && buf[1] == '-' && separator != NULL) 1888 { 1889 /* possible separator */ 1890 int sl = strlen(separator); 1891 1892 if (strncmp(&buf[2], separator, sl) == 0) 1893 (void) putc(' ', fp); 1894 } 1895 putline(buf, fp, m); 1896 } 1897 1898 if (ferror(e->e_dfp)) 1899 { 1900 syserr("putbody: %s: read error", e->e_df); 1901 ExitStat = EX_IOERR; 1902 } 1903 } 1904 1905 /* some mailers want extra blank line at end of message */ 1906 if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n') 1907 putline("", fp, m); 1908 1909 (void) fflush(fp); 1910 if (ferror(fp) && errno != EPIPE) 1911 { 1912 syserr("putbody: write error"); 1913 ExitStat = EX_IOERR; 1914 } 1915 errno = 0; 1916 } 1917 /* 1918 ** MAILFILE -- Send a message to a file. 1919 ** 1920 ** If the file has the setuid/setgid bits set, but NO execute 1921 ** bits, sendmail will try to become the owner of that file 1922 ** rather than the real user. Obviously, this only works if 1923 ** sendmail runs as root. 1924 ** 1925 ** This could be done as a subordinate mailer, except that it 1926 ** is used implicitly to save messages in ~/dead.letter. We 1927 ** view this as being sufficiently important as to include it 1928 ** here. For example, if the system is dying, we shouldn't have 1929 ** to create another process plus some pipes to save the message. 1930 ** 1931 ** Parameters: 1932 ** filename -- the name of the file to send to. 1933 ** ctladdr -- the controlling address header -- includes 1934 ** the userid/groupid to be when sending. 1935 ** 1936 ** Returns: 1937 ** The exit code associated with the operation. 1938 ** 1939 ** Side Effects: 1940 ** none. 1941 */ 1942 1943 mailfile(filename, ctladdr, e) 1944 char *filename; 1945 ADDRESS *ctladdr; 1946 register ENVELOPE *e; 1947 { 1948 register FILE *f; 1949 register int pid; 1950 int mode; 1951 1952 if (tTd(11, 1)) 1953 { 1954 printf("mailfile %s\n ctladdr=", filename); 1955 printaddr(ctladdr, FALSE); 1956 } 1957 1958 if (e->e_xfp != NULL) 1959 fflush(e->e_xfp); 1960 1961 /* 1962 ** Fork so we can change permissions here. 1963 ** Note that we MUST use fork, not vfork, because of 1964 ** the complications of calling subroutines, etc. 1965 */ 1966 1967 DOFORK(fork); 1968 1969 if (pid < 0) 1970 return (EX_OSERR); 1971 else if (pid == 0) 1972 { 1973 /* child -- actually write to file */ 1974 struct stat stb; 1975 1976 (void) setsignal(SIGINT, SIG_DFL); 1977 (void) setsignal(SIGHUP, SIG_DFL); 1978 (void) setsignal(SIGTERM, SIG_DFL); 1979 (void) umask(OldUmask); 1980 1981 if (stat(filename, &stb) < 0) 1982 stb.st_mode = FileMode; 1983 mode = stb.st_mode; 1984 1985 /* limit the errors to those actually caused in the child */ 1986 errno = 0; 1987 ExitStat = EX_OK; 1988 1989 if (bitset(0111, stb.st_mode)) 1990 exit(EX_CANTCREAT); 1991 if (ctladdr != NULL) 1992 { 1993 /* ignore setuid and setgid bits */ 1994 mode &= ~(S_ISGID|S_ISUID); 1995 } 1996 1997 /* we have to open the dfile BEFORE setuid */ 1998 if (e->e_dfp == NULL && e->e_df != NULL) 1999 { 2000 e->e_dfp = fopen(e->e_df, "r"); 2001 if (e->e_dfp == NULL) 2002 { 2003 syserr("mailfile: Cannot open %s for %s from %s", 2004 e->e_df, e->e_to, e->e_from.q_paddr); 2005 } 2006 } 2007 2008 if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 2009 { 2010 if (ctladdr == NULL || ctladdr->q_uid == 0) 2011 { 2012 (void) initgroups(DefUser, DefGid); 2013 } 2014 else 2015 { 2016 (void) initgroups(ctladdr->q_ruser ? 2017 ctladdr->q_ruser : ctladdr->q_user, 2018 ctladdr->q_gid); 2019 } 2020 } 2021 if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 2022 { 2023 if (ctladdr == NULL || ctladdr->q_uid == 0) 2024 (void) setuid(DefUid); 2025 else 2026 (void) setuid(ctladdr->q_uid); 2027 } 2028 FileName = filename; 2029 LineNumber = 0; 2030 f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode); 2031 if (f == NULL) 2032 { 2033 message("554 cannot open: %s", errstring(errno)); 2034 exit(EX_CANTCREAT); 2035 } 2036 2037 putfromline(f, FileMailer, e); 2038 (*e->e_puthdr)(f, FileMailer, e); 2039 putline("\n", f, FileMailer); 2040 (*e->e_putbody)(f, FileMailer, e, NULL); 2041 putline("\n", f, FileMailer); 2042 if (ferror(f)) 2043 { 2044 message("451 I/O error: %s", errstring(errno)); 2045 setstat(EX_IOERR); 2046 } 2047 (void) xfclose(f, "mailfile", filename); 2048 (void) fflush(stdout); 2049 2050 /* reset ISUID & ISGID bits for paranoid systems */ 2051 (void) chmod(filename, (int) stb.st_mode); 2052 exit(ExitStat); 2053 /*NOTREACHED*/ 2054 } 2055 else 2056 { 2057 /* parent -- wait for exit status */ 2058 int st; 2059 2060 st = waitfor(pid); 2061 if (WIFEXITED(st)) 2062 return (WEXITSTATUS(st)); 2063 else 2064 { 2065 syserr("child died on signal %d", st); 2066 return (EX_UNAVAILABLE); 2067 } 2068 /*NOTREACHED*/ 2069 } 2070 } 2071 /* 2072 ** HOSTSIGNATURE -- return the "signature" for a host. 2073 ** 2074 ** The signature describes how we are going to send this -- it 2075 ** can be just the hostname (for non-Internet hosts) or can be 2076 ** an ordered list of MX hosts. 2077 ** 2078 ** Parameters: 2079 ** m -- the mailer describing this host. 2080 ** host -- the host name. 2081 ** e -- the current envelope. 2082 ** 2083 ** Returns: 2084 ** The signature for this host. 2085 ** 2086 ** Side Effects: 2087 ** Can tweak the symbol table. 2088 */ 2089 2090 char * 2091 hostsignature(m, host, e) 2092 register MAILER *m; 2093 char *host; 2094 ENVELOPE *e; 2095 { 2096 register char *p; 2097 register STAB *s; 2098 int i; 2099 int len; 2100 #ifdef NAMED_BIND 2101 int nmx; 2102 auto int rcode; 2103 char *hp; 2104 char *endp; 2105 int oldoptions; 2106 char *mxhosts[MAXMXHOSTS + 1]; 2107 #endif 2108 2109 /* 2110 ** Check to see if this uses IPC -- if not, it can't have MX records. 2111 */ 2112 2113 p = m->m_mailer; 2114 if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 2115 { 2116 /* just an ordinary mailer */ 2117 return host; 2118 } 2119 2120 /* 2121 ** Look it up in the symbol table. 2122 */ 2123 2124 s = stab(host, ST_HOSTSIG, ST_ENTER); 2125 if (s->s_hostsig != NULL) 2126 return s->s_hostsig; 2127 2128 /* 2129 ** Not already there -- create a signature. 2130 */ 2131 2132 #ifdef NAMED_BIND 2133 if (ConfigLevel < 2) 2134 { 2135 oldoptions = _res.options; 2136 _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 2137 } 2138 2139 for (hp = host; hp != NULL; hp = endp) 2140 { 2141 endp = strchr(hp, ':'); 2142 if (endp != NULL) 2143 *endp = '\0'; 2144 2145 nmx = getmxrr(hp, mxhosts, TRUE, &rcode); 2146 2147 if (nmx <= 0) 2148 { 2149 register MCI *mci; 2150 2151 /* update the connection info for this host */ 2152 mci = mci_get(hp, m); 2153 mci->mci_exitstat = rcode; 2154 mci->mci_errno = errno; 2155 #ifdef NAMED_BIND 2156 mci->mci_herrno = h_errno; 2157 #endif 2158 2159 /* and return the original host name as the signature */ 2160 nmx = 1; 2161 mxhosts[0] = hp; 2162 } 2163 2164 len = 0; 2165 for (i = 0; i < nmx; i++) 2166 { 2167 len += strlen(mxhosts[i]) + 1; 2168 } 2169 if (s->s_hostsig != NULL) 2170 len += strlen(s->s_hostsig) + 1; 2171 p = xalloc(len); 2172 if (s->s_hostsig != NULL) 2173 { 2174 (void) strcpy(p, s->s_hostsig); 2175 free(s->s_hostsig); 2176 s->s_hostsig = p; 2177 p += strlen(p); 2178 *p++ = ':'; 2179 } 2180 else 2181 s->s_hostsig = p; 2182 for (i = 0; i < nmx; i++) 2183 { 2184 if (i != 0) 2185 *p++ = ':'; 2186 strcpy(p, mxhosts[i]); 2187 p += strlen(p); 2188 } 2189 if (endp != NULL) 2190 *endp++ = ':'; 2191 } 2192 makelower(s->s_hostsig); 2193 if (ConfigLevel < 2) 2194 _res.options = oldoptions; 2195 #else 2196 /* not using BIND -- the signature is just the host name */ 2197 s->s_hostsig = host; 2198 #endif 2199 if (tTd(17, 1)) 2200 printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 2201 return s->s_hostsig; 2202 } 2203