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.35 (Berkeley) 10/21/93"; 11 #endif /* not lint */ 12 13 #include "sendmail.h" 14 #include <netdb.h> 15 #include <errno.h> 16 #ifdef NAMED_BIND 17 #include <arpa/nameser.h> 18 #include <resolv.h> 19 20 extern int h_errno; 21 #endif 22 23 /* 24 ** SENDALL -- actually send all the messages. 25 ** 26 ** Parameters: 27 ** e -- the envelope to send. 28 ** mode -- the delivery mode to use. If SM_DEFAULT, use 29 ** the current e->e_sendmode. 30 ** 31 ** Returns: 32 ** none. 33 ** 34 ** Side Effects: 35 ** Scans the send lists and sends everything it finds. 36 ** Delivers any appropriate error messages. 37 ** If we are running in a non-interactive mode, takes the 38 ** appropriate action. 39 */ 40 41 sendall(e, mode) 42 ENVELOPE *e; 43 char mode; 44 { 45 register ADDRESS *q; 46 char *owner; 47 int otherowners; 48 register ENVELOPE *ee; 49 ENVELOPE *splitenv = NULL; 50 bool announcequeueup; 51 52 /* 53 ** If we have had global, fatal errors, don't bother sending 54 ** the message at all if we are in SMTP mode. Local errors 55 ** (e.g., a single address failing) will still cause the other 56 ** addresses to be sent. 57 */ 58 59 if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP) 60 { 61 e->e_flags |= EF_CLRQUEUE; 62 return; 63 } 64 65 /* determine actual delivery mode */ 66 if (mode == SM_DEFAULT) 67 { 68 mode = e->e_sendmode; 69 if (mode != SM_VERIFY && 70 shouldqueue(e->e_msgpriority, e->e_ctime)) 71 mode = SM_QUEUE; 72 announcequeueup = mode == SM_QUEUE; 73 } 74 else 75 announcequeueup = FALSE; 76 77 if (tTd(13, 1)) 78 { 79 printf("\n===== SENDALL: mode %c, id %s, e_from ", 80 mode, e->e_id); 81 printaddr(&e->e_from, FALSE); 82 printf("sendqueue:\n"); 83 printaddr(e->e_sendqueue, TRUE); 84 } 85 86 /* 87 ** Do any preprocessing necessary for the mode we are running. 88 ** Check to make sure the hop count is reasonable. 89 ** Delete sends to the sender in mailing lists. 90 */ 91 92 CurEnv = e; 93 94 if (e->e_hopcount > MaxHopCount) 95 { 96 errno = 0; 97 syserr("554 too many hops %d (%d max): from %s via %s, to %s", 98 e->e_hopcount, MaxHopCount, e->e_from.q_paddr, 99 RealHostName, e->e_sendqueue->q_paddr); 100 return; 101 } 102 103 /* 104 ** Do sender deletion. 105 ** 106 ** If the sender has the QQUEUEUP flag set, skip this. 107 ** This can happen if the name server is hosed when you 108 ** are trying to send mail. The result is that the sender 109 ** is instantiated in the queue as a recipient. 110 */ 111 112 if (!bitset(EF_METOO, e->e_flags) && 113 !bitset(QQUEUEUP, e->e_from.q_flags)) 114 { 115 if (tTd(13, 5)) 116 { 117 printf("sendall: QDONTSEND "); 118 printaddr(&e->e_from, FALSE); 119 } 120 e->e_from.q_flags |= QDONTSEND; 121 (void) recipient(&e->e_from, &e->e_sendqueue, e); 122 } 123 124 /* 125 ** Handle alias owners. 126 ** 127 ** We scan up the q_alias chain looking for owners. 128 ** We discard owners that are the same as the return path. 129 */ 130 131 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 132 { 133 register struct address *a; 134 135 for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias) 136 continue; 137 if (a != NULL) 138 q->q_owner = a->q_owner; 139 140 if (q->q_owner != NULL && 141 !bitset(QDONTSEND, q->q_flags) && 142 strcmp(q->q_owner, e->e_from.q_paddr) == 0) 143 q->q_owner = NULL; 144 } 145 146 owner = ""; 147 otherowners = 1; 148 while (owner != NULL && otherowners > 0) 149 { 150 owner = NULL; 151 otherowners = 0; 152 153 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 154 { 155 if (bitset(QDONTSEND, q->q_flags)) 156 continue; 157 158 if (q->q_owner != NULL) 159 { 160 if (owner == NULL) 161 owner = q->q_owner; 162 else if (owner != q->q_owner) 163 { 164 if (strcmp(owner, q->q_owner) == 0) 165 { 166 /* make future comparisons cheap */ 167 q->q_owner = owner; 168 } 169 else 170 { 171 otherowners++; 172 } 173 owner = q->q_owner; 174 } 175 } 176 else 177 { 178 otherowners++; 179 } 180 } 181 182 if (owner != NULL && otherowners > 0) 183 { 184 extern HDR *copyheader(); 185 extern ADDRESS *copyqueue(); 186 187 /* 188 ** Split this envelope into two. 189 */ 190 191 ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE)); 192 *ee = *e; 193 ee->e_id = NULL; 194 (void) queuename(ee, '\0'); 195 196 if (tTd(13, 1)) 197 printf("sendall: split %s into %s\n", 198 e->e_id, ee->e_id); 199 200 ee->e_header = copyheader(e->e_header); 201 ee->e_sendqueue = copyqueue(e->e_sendqueue); 202 ee->e_errorqueue = copyqueue(e->e_errorqueue); 203 ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS); 204 setsender(owner, ee, NULL, TRUE); 205 if (tTd(13, 5)) 206 { 207 printf("sendall(split): QDONTSEND "); 208 printaddr(&ee->e_from, FALSE); 209 } 210 ee->e_from.q_flags |= QDONTSEND; 211 ee->e_dfp = NULL; 212 ee->e_xfp = NULL; 213 ee->e_lockfp = NULL; 214 ee->e_df = NULL; 215 ee->e_errormode = EM_MAIL; 216 ee->e_sibling = splitenv; 217 splitenv = ee; 218 219 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 220 if (q->q_owner == owner) 221 q->q_flags |= QDONTSEND; 222 for (q = ee->e_sendqueue; q != NULL; q = q->q_next) 223 if (q->q_owner != owner) 224 q->q_flags |= QDONTSEND; 225 226 if (e->e_df != NULL && mode != SM_VERIFY) 227 { 228 ee->e_dfp = NULL; 229 ee->e_df = queuename(ee, 'd'); 230 ee->e_df = newstr(ee->e_df); 231 if (link(e->e_df, ee->e_df) < 0) 232 { 233 syserr("sendall: link(%s, %s)", 234 e->e_df, ee->e_df); 235 } 236 } 237 238 if (mode != SM_VERIFY) 239 openxscript(ee); 240 #ifdef LOG 241 if (LogLevel > 4) 242 syslog(LOG_INFO, "%s: clone %s", 243 ee->e_id, e->e_id); 244 #endif 245 } 246 } 247 248 if (owner != NULL) 249 { 250 setsender(owner, e, NULL, TRUE); 251 if (tTd(13, 5)) 252 { 253 printf("sendall(owner): QDONTSEND "); 254 printaddr(&e->e_from, FALSE); 255 } 256 e->e_from.q_flags |= QDONTSEND; 257 e->e_errormode = EM_MAIL; 258 } 259 260 # ifdef QUEUE 261 if ((mode == SM_QUEUE || mode == SM_FORK || 262 (mode != SM_VERIFY && SuperSafe)) && 263 !bitset(EF_INQUEUE, e->e_flags)) 264 { 265 /* be sure everything is instantiated in the queue */ 266 queueup(e, TRUE, announcequeueup); 267 for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 268 queueup(ee, TRUE, announcequeueup); 269 } 270 #endif /* QUEUE */ 271 272 if (splitenv != NULL) 273 { 274 if (tTd(13, 1)) 275 { 276 printf("\nsendall: Split queue; remaining queue:\n"); 277 printaddr(e->e_sendqueue, TRUE); 278 } 279 280 for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 281 { 282 CurEnv = ee; 283 sendenvelope(ee, mode); 284 } 285 286 CurEnv = e; 287 } 288 sendenvelope(e, mode); 289 290 for (; splitenv != NULL; splitenv = splitenv->e_sibling) 291 dropenvelope(splitenv); 292 } 293 294 sendenvelope(e, mode) 295 register ENVELOPE *e; 296 char mode; 297 { 298 bool oldverbose; 299 int pid; 300 register ADDRESS *q; 301 char *qf; 302 char *id; 303 304 /* 305 ** If we have had global, fatal errors, don't bother sending 306 ** the message at all if we are in SMTP mode. Local errors 307 ** (e.g., a single address failing) will still cause the other 308 ** addresses to be sent. 309 */ 310 311 if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP) 312 { 313 e->e_flags |= EF_CLRQUEUE; 314 return; 315 } 316 317 oldverbose = Verbose; 318 switch (mode) 319 { 320 case SM_VERIFY: 321 Verbose = TRUE; 322 break; 323 324 case SM_QUEUE: 325 queueonly: 326 e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 327 return; 328 329 case SM_FORK: 330 if (e->e_xfp != NULL) 331 (void) fflush(e->e_xfp); 332 333 # ifndef HASFLOCK 334 /* 335 ** Since fcntl locking has the interesting semantic that 336 ** the lock is owned by a process, not by an open file 337 ** descriptor, we have to flush this to the queue, and 338 ** then restart from scratch in the child. 339 */ 340 341 /* save id for future use */ 342 id = e->e_id; 343 344 /* now drop the envelope in the parent */ 345 e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 346 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", 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, 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, 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 rcode = mailfile(user, getctladdr(to), e); 810 giveresponse(rcode, m, NULL, e); 811 if (rcode == EX_OK) 812 to->q_flags |= QSENT; 813 continue; 814 } 815 816 /* 817 ** Address is verified -- add this user to mailer 818 ** argv, and add it to the print list of recipients. 819 */ 820 821 /* link together the chain of recipients */ 822 to->q_tchain = tochain; 823 tochain = to; 824 825 /* create list of users for error messages */ 826 (void) strcat(tobuf, ","); 827 (void) strcat(tobuf, to->q_paddr); 828 define('u', user, e); /* to user */ 829 p = to->q_home; 830 if (p == NULL && ctladdr != NULL) 831 p = ctladdr->q_home; 832 define('z', p, e); /* user's home */ 833 834 /* 835 ** Expand out this user into argument list. 836 */ 837 838 if (!clever) 839 { 840 expand(*mvp, buf, &buf[sizeof buf - 1], e); 841 *pvp++ = newstr(buf); 842 if (pvp >= &pv[MAXPV - 2]) 843 { 844 /* allow some space for trailing parms */ 845 break; 846 } 847 } 848 } 849 850 /* see if any addresses still exist */ 851 if (tobuf[0] == '\0') 852 { 853 define('g', (char *) NULL, e); 854 return (0); 855 } 856 857 /* print out messages as full list */ 858 e->e_to = tobuf + 1; 859 860 /* 861 ** Fill out any parameters after the $u parameter. 862 */ 863 864 while (!clever && *++mvp != NULL) 865 { 866 expand(*mvp, buf, &buf[sizeof buf - 1], e); 867 *pvp++ = newstr(buf); 868 if (pvp >= &pv[MAXPV]) 869 syserr("554 deliver: pv overflow after $u for %s", pv[0]); 870 } 871 *pvp++ = NULL; 872 873 /* 874 ** Call the mailer. 875 ** The argument vector gets built, pipes 876 ** are created as necessary, and we fork & exec as 877 ** appropriate. 878 ** If we are running SMTP, we just need to clean up. 879 */ 880 881 if (ctladdr == NULL && m != ProgMailer) 882 ctladdr = &e->e_from; 883 #ifdef NAMED_BIND 884 if (ConfigLevel < 2) 885 _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 886 #endif 887 888 if (tTd(11, 1)) 889 { 890 printf("openmailer:"); 891 printav(pv); 892 } 893 errno = 0; 894 895 CurHostName = m->m_mailer; 896 897 /* 898 ** Deal with the special case of mail handled through an IPC 899 ** connection. 900 ** In this case we don't actually fork. We must be 901 ** running SMTP for this to work. We will return a 902 ** zero pid to indicate that we are running IPC. 903 ** We also handle a debug version that just talks to stdin/out. 904 */ 905 906 curhost = NULL; 907 SmtpPhase = NULL; 908 mci = NULL; 909 910 #ifdef XDEBUG 911 { 912 char wbuf[MAXLINE]; 913 914 /* make absolutely certain 0, 1, and 2 are in use */ 915 sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name); 916 checkfd012(wbuf); 917 } 918 #endif 919 920 /* check for Local Person Communication -- not for mortals!!! */ 921 if (strcmp(m->m_mailer, "[LPC]") == 0) 922 { 923 mci = (MCI *) xalloc(sizeof *mci); 924 bzero((char *) mci, sizeof *mci); 925 mci->mci_in = stdin; 926 mci->mci_out = stdout; 927 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 928 mci->mci_mailer = m; 929 } 930 else if (strcmp(m->m_mailer, "[IPC]") == 0 || 931 strcmp(m->m_mailer, "[TCP]") == 0) 932 { 933 #ifdef DAEMON 934 register int i; 935 register u_short port; 936 937 CurHostName = pv[1]; 938 curhost = hostsignature(m, pv[1], e); 939 940 if (curhost == NULL || curhost[0] == '\0') 941 { 942 syserr("null signature"); 943 rcode = EX_OSERR; 944 goto give_up; 945 } 946 947 if (!clever) 948 { 949 syserr("554 non-clever IPC"); 950 rcode = EX_CONFIG; 951 goto give_up; 952 } 953 if (pv[2] != NULL) 954 port = atoi(pv[2]); 955 else 956 port = 0; 957 tryhost: 958 while (*curhost != '\0') 959 { 960 register char *p; 961 static char hostbuf[MAXNAME]; 962 963 /* pull the next host from the signature */ 964 p = strchr(curhost, ':'); 965 if (p == NULL) 966 p = &curhost[strlen(curhost)]; 967 if (p == curhost) 968 { 969 syserr("deliver: null host name in signature"); 970 curhost++; 971 continue; 972 } 973 strncpy(hostbuf, curhost, p - curhost); 974 hostbuf[p - curhost] = '\0'; 975 if (*p != '\0') 976 p++; 977 curhost = p; 978 979 /* see if we already know that this host is fried */ 980 CurHostName = hostbuf; 981 mci = mci_get(hostbuf, m); 982 if (mci->mci_state != MCIS_CLOSED) 983 { 984 if (tTd(11, 1)) 985 { 986 printf("openmailer: "); 987 mci_dump(mci, FALSE); 988 } 989 CurHostName = mci->mci_host; 990 break; 991 } 992 mci->mci_mailer = m; 993 if (mci->mci_exitstat != EX_OK) 994 continue; 995 996 /* try the connection */ 997 setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 998 message("Connecting to %s (%s)...", 999 hostbuf, m->m_name); 1000 i = makeconnection(hostbuf, port, mci, 1001 bitnset(M_SECURE_PORT, m->m_flags)); 1002 mci->mci_exitstat = i; 1003 mci->mci_errno = errno; 1004 #ifdef NAMED_BIND 1005 mci->mci_herrno = h_errno; 1006 #endif 1007 if (i == EX_OK) 1008 { 1009 mci->mci_state = MCIS_OPENING; 1010 mci_cache(mci); 1011 if (TrafficLogFile != NULL) 1012 fprintf(TrafficLogFile, "%05d == CONNECT %s\n", 1013 getpid(), hostbuf); 1014 break; 1015 } 1016 else if (tTd(11, 1)) 1017 printf("openmailer: makeconnection => stat=%d, errno=%d\n", 1018 i, errno); 1019 1020 /* enter status of this host */ 1021 setstat(i); 1022 1023 /* should print some message here for -v mode */ 1024 } 1025 if (mci == NULL) 1026 { 1027 syserr("deliver: no host name"); 1028 rcode = EX_OSERR; 1029 goto give_up; 1030 } 1031 mci->mci_pid = 0; 1032 #else /* no DAEMON */ 1033 syserr("554 openmailer: no IPC"); 1034 if (tTd(11, 1)) 1035 printf("openmailer: NULL\n"); 1036 rcode = EX_UNAVAILABLE; 1037 goto give_up; 1038 #endif /* DAEMON */ 1039 } 1040 else 1041 { 1042 if (TrafficLogFile != NULL) 1043 { 1044 char **av; 1045 1046 fprintf(TrafficLogFile, "%05d === EXEC", getpid()); 1047 for (av = pv; *av != NULL; av++) 1048 fprintf(TrafficLogFile, " %s", *av); 1049 fprintf(TrafficLogFile, "\n"); 1050 } 1051 1052 /* create a pipe to shove the mail through */ 1053 if (pipe(mpvect) < 0) 1054 { 1055 syserr("%s... openmailer(%s): pipe (to mailer)", 1056 e->e_to, m->m_name); 1057 if (tTd(11, 1)) 1058 printf("openmailer: NULL\n"); 1059 rcode = EX_OSERR; 1060 goto give_up; 1061 } 1062 1063 /* if this mailer speaks smtp, create a return pipe */ 1064 if (clever && pipe(rpvect) < 0) 1065 { 1066 syserr("%s... openmailer(%s): pipe (from mailer)", 1067 e->e_to, m->m_name); 1068 (void) close(mpvect[0]); 1069 (void) close(mpvect[1]); 1070 if (tTd(11, 1)) 1071 printf("openmailer: NULL\n"); 1072 rcode = EX_OSERR; 1073 goto give_up; 1074 } 1075 1076 /* 1077 ** Actually fork the mailer process. 1078 ** DOFORK is clever about retrying. 1079 ** 1080 ** Dispose of SIGCHLD signal catchers that may be laying 1081 ** around so that endmail will get it. 1082 */ 1083 1084 if (e->e_xfp != NULL) 1085 (void) fflush(e->e_xfp); /* for debugging */ 1086 (void) fflush(stdout); 1087 # ifdef SIGCHLD 1088 (void) setsignal(SIGCHLD, SIG_DFL); 1089 # endif /* SIGCHLD */ 1090 DOFORK(FORK); 1091 /* pid is set by DOFORK */ 1092 if (pid < 0) 1093 { 1094 /* failure */ 1095 syserr("%s... openmailer(%s): cannot fork", 1096 e->e_to, m->m_name); 1097 (void) close(mpvect[0]); 1098 (void) close(mpvect[1]); 1099 if (clever) 1100 { 1101 (void) close(rpvect[0]); 1102 (void) close(rpvect[1]); 1103 } 1104 if (tTd(11, 1)) 1105 printf("openmailer: NULL\n"); 1106 rcode = EX_OSERR; 1107 goto give_up; 1108 } 1109 else if (pid == 0) 1110 { 1111 int i; 1112 int saveerrno; 1113 char **ep; 1114 char *env[MAXUSERENVIRON]; 1115 extern char **environ; 1116 extern int DtableSize; 1117 1118 /* child -- set up input & exec mailer */ 1119 (void) setsignal(SIGINT, SIG_IGN); 1120 (void) setsignal(SIGHUP, SIG_IGN); 1121 (void) setsignal(SIGTERM, SIG_DFL); 1122 1123 /* close any other cached connections */ 1124 mci_flush(FALSE, mci); 1125 1126 /* reset user and group */ 1127 if (!bitnset(M_RESTR, m->m_flags)) 1128 { 1129 if (ctladdr == NULL || ctladdr->q_uid == 0) 1130 { 1131 (void) initgroups(DefUser, DefGid); 1132 (void) setuid(DefUid); 1133 } 1134 else 1135 { 1136 (void) initgroups(ctladdr->q_ruser? 1137 ctladdr->q_ruser: ctladdr->q_user, 1138 ctladdr->q_gid); 1139 (void) setuid(ctladdr->q_uid); 1140 } 1141 } 1142 1143 if (tTd(11, 2)) 1144 printf("openmailer: running as r/euid=%d/%d\n", 1145 getuid(), geteuid()); 1146 1147 /* move into some "safe" directory */ 1148 if (m->m_execdir != NULL) 1149 { 1150 char *p, *q; 1151 char buf[MAXLINE]; 1152 1153 for (p = m->m_execdir; p != NULL; p = q) 1154 { 1155 q = strchr(p, ':'); 1156 if (q != NULL) 1157 *q = '\0'; 1158 expand(p, buf, &buf[sizeof buf] - 1, e); 1159 if (q != NULL) 1160 *q++ = ':'; 1161 if (tTd(11, 20)) 1162 printf("openmailer: trydir %s\n", 1163 buf); 1164 if (buf[0] != '\0' && chdir(buf) >= 0) 1165 break; 1166 } 1167 } 1168 1169 /* arrange to filter std & diag output of command */ 1170 if (clever) 1171 { 1172 (void) close(rpvect[0]); 1173 if (dup2(rpvect[1], STDOUT_FILENO) < 0) 1174 { 1175 syserr("%s... openmailer(%s): cannot dup pipe %d for stdout", 1176 e->e_to, m->m_name, rpvect[1]); 1177 _exit(EX_OSERR); 1178 } 1179 (void) close(rpvect[1]); 1180 } 1181 else if (OpMode == MD_SMTP || HoldErrs) 1182 { 1183 /* put mailer output in transcript */ 1184 if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0) 1185 { 1186 syserr("%s... openmailer(%s): cannot dup xscript %d for stdout", 1187 e->e_to, m->m_name, 1188 fileno(e->e_xfp)); 1189 _exit(EX_OSERR); 1190 } 1191 } 1192 if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) 1193 { 1194 syserr("%s... openmailer(%s): cannot dup stdout for stderr", 1195 e->e_to, m->m_name); 1196 _exit(EX_OSERR); 1197 } 1198 1199 /* arrange to get standard input */ 1200 (void) close(mpvect[1]); 1201 if (dup2(mpvect[0], STDIN_FILENO) < 0) 1202 { 1203 syserr("%s... openmailer(%s): cannot dup pipe %d for stdin", 1204 e->e_to, m->m_name, mpvect[0]); 1205 _exit(EX_OSERR); 1206 } 1207 (void) close(mpvect[0]); 1208 1209 /* arrange for all the files to be closed */ 1210 for (i = 3; i < DtableSize; i++) 1211 { 1212 register int j; 1213 1214 if ((j = fcntl(i, F_GETFD, 0)) != -1) 1215 (void) fcntl(i, F_SETFD, j | 1); 1216 } 1217 1218 /* set up the mailer environment */ 1219 i = 0; 1220 env[i++] = "AGENT=sendmail"; 1221 for (ep = environ; *ep != NULL; ep++) 1222 { 1223 if (strncmp(*ep, "TZ=", 3) == 0) 1224 env[i++] = *ep; 1225 } 1226 env[i++] = NULL; 1227 1228 /* try to execute the mailer */ 1229 execve(m->m_mailer, pv, env); 1230 saveerrno = errno; 1231 syserr("Cannot exec %s", m->m_mailer); 1232 if (m == LocalMailer || transienterror(saveerrno)) 1233 _exit(EX_OSERR); 1234 _exit(EX_UNAVAILABLE); 1235 } 1236 1237 /* 1238 ** Set up return value. 1239 */ 1240 1241 mci = (MCI *) xalloc(sizeof *mci); 1242 bzero((char *) mci, sizeof *mci); 1243 mci->mci_mailer = m; 1244 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 1245 mci->mci_pid = pid; 1246 (void) close(mpvect[0]); 1247 mci->mci_out = fdopen(mpvect[1], "w"); 1248 if (mci->mci_out == NULL) 1249 { 1250 syserr("deliver: cannot create mailer output channel, fd=%d", 1251 mpvect[1]); 1252 (void) close(mpvect[1]); 1253 if (clever) 1254 { 1255 (void) close(rpvect[0]); 1256 (void) close(rpvect[1]); 1257 } 1258 rcode = EX_OSERR; 1259 goto give_up; 1260 } 1261 if (clever) 1262 { 1263 (void) close(rpvect[1]); 1264 mci->mci_in = fdopen(rpvect[0], "r"); 1265 if (mci->mci_in == NULL) 1266 { 1267 syserr("deliver: cannot create mailer input channel, fd=%d", 1268 mpvect[1]); 1269 (void) close(rpvect[0]); 1270 fclose(mci->mci_out); 1271 mci->mci_out = NULL; 1272 rcode = EX_OSERR; 1273 goto give_up; 1274 } 1275 } 1276 else 1277 { 1278 mci->mci_flags |= MCIF_TEMP; 1279 mci->mci_in = NULL; 1280 } 1281 } 1282 1283 /* 1284 ** If we are in SMTP opening state, send initial protocol. 1285 */ 1286 1287 if (clever && mci->mci_state != MCIS_CLOSED) 1288 { 1289 smtpinit(m, mci, e); 1290 } 1291 if (tTd(11, 1)) 1292 { 1293 printf("openmailer: "); 1294 mci_dump(mci, FALSE); 1295 } 1296 1297 if (mci->mci_state != MCIS_OPEN) 1298 { 1299 /* couldn't open the mailer */ 1300 rcode = mci->mci_exitstat; 1301 errno = mci->mci_errno; 1302 #ifdef NAMED_BIND 1303 h_errno = mci->mci_herrno; 1304 #endif 1305 if (rcode == EX_OK) 1306 { 1307 /* shouldn't happen */ 1308 syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 1309 rcode, mci->mci_state, firstsig); 1310 rcode = EX_SOFTWARE; 1311 } 1312 else if (rcode == EX_TEMPFAIL && *curhost != '\0') 1313 { 1314 /* try next MX site */ 1315 goto tryhost; 1316 } 1317 } 1318 else if (!clever) 1319 { 1320 /* 1321 ** Format and send message. 1322 */ 1323 1324 putfromline(mci->mci_out, m, e); 1325 (*e->e_puthdr)(mci->mci_out, m, e); 1326 putline("\n", mci->mci_out, m); 1327 (*e->e_putbody)(mci->mci_out, m, e, NULL); 1328 1329 /* get the exit status */ 1330 rcode = endmailer(mci, e, pv); 1331 } 1332 else 1333 #ifdef SMTP 1334 { 1335 /* 1336 ** Send the MAIL FROM: protocol 1337 */ 1338 1339 rcode = smtpmailfrom(m, mci, e); 1340 if (rcode == EX_OK) 1341 { 1342 register char *t = tobuf; 1343 register int i; 1344 1345 /* send the recipient list */ 1346 tobuf[0] = '\0'; 1347 for (to = tochain; to != NULL; to = to->q_tchain) 1348 { 1349 e->e_to = to->q_paddr; 1350 if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 1351 { 1352 markfailure(e, to, i); 1353 giveresponse(i, m, mci, e); 1354 } 1355 else 1356 { 1357 *t++ = ','; 1358 for (p = to->q_paddr; *p; *t++ = *p++) 1359 continue; 1360 *t = '\0'; 1361 } 1362 } 1363 1364 /* now send the data */ 1365 if (tobuf[0] == '\0') 1366 { 1367 rcode = EX_OK; 1368 e->e_to = NULL; 1369 if (bitset(MCIF_CACHED, mci->mci_flags)) 1370 smtprset(m, mci, e); 1371 } 1372 else 1373 { 1374 e->e_to = tobuf + 1; 1375 rcode = smtpdata(m, mci, e); 1376 } 1377 1378 /* now close the connection */ 1379 if (!bitset(MCIF_CACHED, mci->mci_flags)) 1380 smtpquit(m, mci, e); 1381 } 1382 if (rcode != EX_OK && *curhost != '\0') 1383 { 1384 /* try next MX site */ 1385 goto tryhost; 1386 } 1387 } 1388 #else /* not SMTP */ 1389 { 1390 syserr("554 deliver: need SMTP compiled to use clever mailer"); 1391 rcode = EX_CONFIG; 1392 goto give_up; 1393 } 1394 #endif /* SMTP */ 1395 #ifdef NAMED_BIND 1396 if (ConfigLevel < 2) 1397 _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 1398 #endif 1399 1400 /* arrange a return receipt if requested */ 1401 if (rcode == EX_OK && e->e_receiptto != NULL && 1402 bitnset(M_LOCALMAILER, m->m_flags)) 1403 { 1404 e->e_flags |= EF_SENDRECEIPT; 1405 /* do we want to send back more info? */ 1406 } 1407 1408 /* 1409 ** Do final status disposal. 1410 ** We check for something in tobuf for the SMTP case. 1411 ** If we got a temporary failure, arrange to queue the 1412 ** addressees. 1413 */ 1414 1415 give_up: 1416 if (tobuf[0] != '\0') 1417 giveresponse(rcode, m, mci, e); 1418 for (to = tochain; to != NULL; to = to->q_tchain) 1419 { 1420 if (rcode != EX_OK) 1421 markfailure(e, to, rcode); 1422 else 1423 { 1424 to->q_flags |= QSENT; 1425 e->e_nsent++; 1426 if (e->e_receiptto != NULL && 1427 bitnset(M_LOCALMAILER, m->m_flags)) 1428 { 1429 fprintf(e->e_xfp, "%s... Successfully delivered\n", 1430 to->q_paddr); 1431 } 1432 } 1433 } 1434 1435 /* 1436 ** Restore state and return. 1437 */ 1438 1439 #ifdef XDEBUG 1440 { 1441 char wbuf[MAXLINE]; 1442 1443 /* make absolutely certain 0, 1, and 2 are in use */ 1444 sprintf(wbuf, "%s... end of deliver(%s)", 1445 e->e_to == NULL ? "NO-TO-LIST" : e->e_to, 1446 m->m_name); 1447 checkfd012(wbuf); 1448 } 1449 #endif 1450 1451 errno = 0; 1452 define('g', (char *) NULL, e); 1453 return (rcode); 1454 } 1455 /* 1456 ** MARKFAILURE -- mark a failure on a specific address. 1457 ** 1458 ** Parameters: 1459 ** e -- the envelope we are sending. 1460 ** q -- the address to mark. 1461 ** rcode -- the code signifying the particular failure. 1462 ** 1463 ** Returns: 1464 ** none. 1465 ** 1466 ** Side Effects: 1467 ** marks the address (and possibly the envelope) with the 1468 ** failure so that an error will be returned or 1469 ** the message will be queued, as appropriate. 1470 */ 1471 1472 markfailure(e, q, rcode) 1473 register ENVELOPE *e; 1474 register ADDRESS *q; 1475 int rcode; 1476 { 1477 char buf[MAXLINE]; 1478 1479 if (rcode == EX_OK) 1480 return; 1481 else if (rcode == EX_TEMPFAIL) 1482 q->q_flags |= QQUEUEUP; 1483 else if (rcode != EX_IOERR && rcode != EX_OSERR) 1484 q->q_flags |= QBADADDR; 1485 } 1486 /* 1487 ** ENDMAILER -- Wait for mailer to terminate. 1488 ** 1489 ** We should never get fatal errors (e.g., segmentation 1490 ** violation), so we report those specially. For other 1491 ** errors, we choose a status message (into statmsg), 1492 ** and if it represents an error, we print it. 1493 ** 1494 ** Parameters: 1495 ** pid -- pid of mailer. 1496 ** e -- the current envelope. 1497 ** pv -- the parameter vector that invoked the mailer 1498 ** (for error messages). 1499 ** 1500 ** Returns: 1501 ** exit code of mailer. 1502 ** 1503 ** Side Effects: 1504 ** none. 1505 */ 1506 1507 endmailer(mci, e, pv) 1508 register MCI *mci; 1509 register ENVELOPE *e; 1510 char **pv; 1511 { 1512 int st; 1513 1514 /* close any connections */ 1515 if (mci->mci_in != NULL) 1516 (void) xfclose(mci->mci_in, pv[0], "mci_in"); 1517 if (mci->mci_out != NULL) 1518 (void) xfclose(mci->mci_out, pv[0], "mci_out"); 1519 mci->mci_in = mci->mci_out = NULL; 1520 mci->mci_state = MCIS_CLOSED; 1521 1522 /* in the IPC case there is nothing to wait for */ 1523 if (mci->mci_pid == 0) 1524 return (EX_OK); 1525 1526 /* wait for the mailer process to die and collect status */ 1527 st = waitfor(mci->mci_pid); 1528 if (st == -1) 1529 { 1530 syserr("endmailer %s: wait", pv[0]); 1531 return (EX_SOFTWARE); 1532 } 1533 1534 if (WIFEXITED(st)) 1535 { 1536 /* normal death -- return status */ 1537 return (WEXITSTATUS(st)); 1538 } 1539 1540 /* it died a horrid death */ 1541 syserr("mailer %s died with signal %o", pv[0], st); 1542 1543 /* log the arguments */ 1544 if (e->e_xfp != NULL) 1545 { 1546 register char **av; 1547 1548 fprintf(e->e_xfp, "Arguments:"); 1549 for (av = pv; *av != NULL; av++) 1550 fprintf(e->e_xfp, " %s", *av); 1551 fprintf(e->e_xfp, "\n"); 1552 } 1553 1554 ExitStat = EX_TEMPFAIL; 1555 return (EX_TEMPFAIL); 1556 } 1557 /* 1558 ** GIVERESPONSE -- Interpret an error response from a mailer 1559 ** 1560 ** Parameters: 1561 ** stat -- the status code from the mailer (high byte 1562 ** only; core dumps must have been taken care of 1563 ** already). 1564 ** m -- the mailer info for this mailer. 1565 ** mci -- the mailer connection info -- can be NULL if the 1566 ** response is given before the connection is made. 1567 ** e -- the current envelope. 1568 ** 1569 ** Returns: 1570 ** none. 1571 ** 1572 ** Side Effects: 1573 ** Errors may be incremented. 1574 ** ExitStat may be set. 1575 */ 1576 1577 giveresponse(stat, m, mci, e) 1578 int stat; 1579 register MAILER *m; 1580 register MCI *mci; 1581 ENVELOPE *e; 1582 { 1583 register const char *statmsg; 1584 extern char *SysExMsg[]; 1585 register int i; 1586 extern int N_SysEx; 1587 char buf[MAXLINE]; 1588 1589 /* 1590 ** Compute status message from code. 1591 */ 1592 1593 i = stat - EX__BASE; 1594 if (stat == 0) 1595 { 1596 statmsg = "250 Sent"; 1597 if (e->e_statmsg != NULL) 1598 { 1599 (void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg); 1600 statmsg = buf; 1601 } 1602 } 1603 else if (i < 0 || i > N_SysEx) 1604 { 1605 (void) sprintf(buf, "554 unknown mailer error %d", stat); 1606 stat = EX_UNAVAILABLE; 1607 statmsg = buf; 1608 } 1609 else if (stat == EX_TEMPFAIL) 1610 { 1611 (void) strcpy(buf, SysExMsg[i] + 1); 1612 #ifdef NAMED_BIND 1613 if (h_errno == TRY_AGAIN) 1614 statmsg = errstring(h_errno+E_DNSBASE); 1615 else 1616 #endif 1617 { 1618 if (errno != 0) 1619 statmsg = errstring(errno); 1620 else 1621 { 1622 #ifdef SMTP 1623 extern char SmtpError[]; 1624 1625 statmsg = SmtpError; 1626 #else /* SMTP */ 1627 statmsg = NULL; 1628 #endif /* SMTP */ 1629 } 1630 } 1631 if (statmsg != NULL && statmsg[0] != '\0') 1632 { 1633 (void) strcat(buf, ": "); 1634 (void) strcat(buf, statmsg); 1635 } 1636 statmsg = buf; 1637 } 1638 #ifdef NAMED_BIND 1639 else if (stat == EX_NOHOST && h_errno != 0) 1640 { 1641 statmsg = errstring(h_errno + E_DNSBASE); 1642 (void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg); 1643 statmsg = buf; 1644 } 1645 #endif 1646 else 1647 { 1648 statmsg = SysExMsg[i]; 1649 if (*statmsg++ == ':') 1650 { 1651 (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 1652 statmsg = buf; 1653 } 1654 } 1655 1656 /* 1657 ** Print the message as appropriate 1658 */ 1659 1660 if (stat == EX_OK || stat == EX_TEMPFAIL) 1661 { 1662 extern char MsgBuf[]; 1663 1664 message(&statmsg[4], errstring(errno)); 1665 if (stat == EX_TEMPFAIL && e->e_xfp != NULL) 1666 fprintf(e->e_xfp, "%s\n", &MsgBuf[4]); 1667 } 1668 else 1669 { 1670 Errors++; 1671 usrerr(statmsg, errstring(errno)); 1672 } 1673 1674 /* 1675 ** Final cleanup. 1676 ** Log a record of the transaction. Compute the new 1677 ** ExitStat -- if we already had an error, stick with 1678 ** that. 1679 */ 1680 1681 if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 1682 logdelivery(m, mci, &statmsg[4], e); 1683 1684 if (stat != EX_TEMPFAIL) 1685 setstat(stat); 1686 if (stat != EX_OK) 1687 { 1688 if (e->e_message != NULL) 1689 free(e->e_message); 1690 e->e_message = newstr(&statmsg[4]); 1691 } 1692 errno = 0; 1693 #ifdef NAMED_BIND 1694 h_errno = 0; 1695 #endif 1696 } 1697 /* 1698 ** LOGDELIVERY -- log the delivery in the system log 1699 ** 1700 ** Parameters: 1701 ** m -- the mailer info. Can be NULL for initial queue. 1702 ** mci -- the mailer connection info -- can be NULL if the 1703 ** log is occuring when no connection is active. 1704 ** stat -- the message to print for the status. 1705 ** e -- the current envelope. 1706 ** 1707 ** Returns: 1708 ** none 1709 ** 1710 ** Side Effects: 1711 ** none 1712 */ 1713 1714 logdelivery(m, mci, stat, e) 1715 MAILER *m; 1716 register MCI *mci; 1717 char *stat; 1718 register ENVELOPE *e; 1719 { 1720 # ifdef LOG 1721 char buf[512]; 1722 1723 (void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 1724 1725 if (m != NULL) 1726 { 1727 (void) strcat(buf, ", mailer="); 1728 (void) strcat(buf, m->m_name); 1729 } 1730 1731 if (mci != NULL && mci->mci_host != NULL) 1732 { 1733 # ifdef DAEMON 1734 extern SOCKADDR CurHostAddr; 1735 # endif 1736 1737 (void) strcat(buf, ", relay="); 1738 (void) strcat(buf, mci->mci_host); 1739 1740 # ifdef DAEMON 1741 (void) strcat(buf, " ("); 1742 (void) strcat(buf, anynet_ntoa(&CurHostAddr)); 1743 (void) strcat(buf, ")"); 1744 # endif 1745 } 1746 else 1747 { 1748 char *p = macvalue('h', e); 1749 1750 if (p != NULL && p[0] != '\0') 1751 { 1752 (void) strcat(buf, ", relay="); 1753 (void) strcat(buf, p); 1754 } 1755 } 1756 1757 syslog(LOG_INFO, "%s: to=%s, %s, stat=%s", 1758 e->e_id, e->e_to, buf, stat); 1759 # endif /* LOG */ 1760 } 1761 /* 1762 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 1763 ** 1764 ** This can be made an arbitrary message separator by changing $l 1765 ** 1766 ** One of the ugliest hacks seen by human eyes is contained herein: 1767 ** UUCP wants those stupid "remote from <host>" lines. Why oh why 1768 ** does a well-meaning programmer such as myself have to deal with 1769 ** this kind of antique garbage???? 1770 ** 1771 ** Parameters: 1772 ** fp -- the file to output to. 1773 ** m -- the mailer describing this entry. 1774 ** 1775 ** Returns: 1776 ** none 1777 ** 1778 ** Side Effects: 1779 ** outputs some text to fp. 1780 */ 1781 1782 putfromline(fp, m, e) 1783 register FILE *fp; 1784 register MAILER *m; 1785 ENVELOPE *e; 1786 { 1787 char *template = "\201l\n"; 1788 char buf[MAXLINE]; 1789 1790 if (bitnset(M_NHDR, m->m_flags)) 1791 return; 1792 1793 # ifdef UGLYUUCP 1794 if (bitnset(M_UGLYUUCP, m->m_flags)) 1795 { 1796 char *bang; 1797 char xbuf[MAXLINE]; 1798 1799 expand("\201g", buf, &buf[sizeof buf - 1], e); 1800 bang = strchr(buf, '!'); 1801 if (bang == NULL) 1802 { 1803 errno = 0; 1804 syserr("554 No ! in UUCP From address! (%s given)", buf); 1805 } 1806 else 1807 { 1808 *bang++ = '\0'; 1809 (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 1810 template = xbuf; 1811 } 1812 } 1813 # endif /* UGLYUUCP */ 1814 expand(template, buf, &buf[sizeof buf - 1], e); 1815 putline(buf, fp, m); 1816 } 1817 /* 1818 ** PUTBODY -- put the body of a message. 1819 ** 1820 ** Parameters: 1821 ** fp -- file to output onto. 1822 ** m -- a mailer descriptor to control output format. 1823 ** e -- the envelope to put out. 1824 ** separator -- if non-NULL, a message separator that must 1825 ** not be permitted in the resulting message. 1826 ** 1827 ** Returns: 1828 ** none. 1829 ** 1830 ** Side Effects: 1831 ** The message is written onto fp. 1832 */ 1833 1834 putbody(fp, m, e, separator) 1835 FILE *fp; 1836 MAILER *m; 1837 register ENVELOPE *e; 1838 char *separator; 1839 { 1840 char buf[MAXLINE]; 1841 1842 /* 1843 ** Output the body of the message 1844 */ 1845 1846 if (e->e_dfp == NULL) 1847 { 1848 if (e->e_df != NULL) 1849 { 1850 e->e_dfp = fopen(e->e_df, "r"); 1851 if (e->e_dfp == NULL) 1852 syserr("putbody: Cannot open %s for %s from %s", 1853 e->e_df, e->e_to, e->e_from.q_paddr); 1854 } 1855 else 1856 putline("<<< No Message Collected >>>", fp, m); 1857 } 1858 if (e->e_dfp != NULL) 1859 { 1860 rewind(e->e_dfp); 1861 while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 1862 { 1863 if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1864 strncmp(buf, "From ", 5) == 0) 1865 (void) putc('>', fp); 1866 if (buf[0] == '-' && buf[1] == '-' && separator != NULL) 1867 { 1868 /* possible separator */ 1869 int sl = strlen(separator); 1870 1871 if (strncmp(&buf[2], separator, sl) == 0) 1872 (void) putc(' ', fp); 1873 } 1874 putline(buf, fp, m); 1875 } 1876 1877 if (ferror(e->e_dfp)) 1878 { 1879 syserr("putbody: %s: read error", e->e_df); 1880 ExitStat = EX_IOERR; 1881 } 1882 } 1883 1884 /* some mailers want extra blank line at end of message */ 1885 if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n') 1886 putline("", fp, m); 1887 1888 (void) fflush(fp); 1889 if (ferror(fp) && errno != EPIPE) 1890 { 1891 syserr("putbody: write error"); 1892 ExitStat = EX_IOERR; 1893 } 1894 errno = 0; 1895 } 1896 /* 1897 ** MAILFILE -- Send a message to a file. 1898 ** 1899 ** If the file has the setuid/setgid bits set, but NO execute 1900 ** bits, sendmail will try to become the owner of that file 1901 ** rather than the real user. Obviously, this only works if 1902 ** sendmail runs as root. 1903 ** 1904 ** This could be done as a subordinate mailer, except that it 1905 ** is used implicitly to save messages in ~/dead.letter. We 1906 ** view this as being sufficiently important as to include it 1907 ** here. For example, if the system is dying, we shouldn't have 1908 ** to create another process plus some pipes to save the message. 1909 ** 1910 ** Parameters: 1911 ** filename -- the name of the file to send to. 1912 ** ctladdr -- the controlling address header -- includes 1913 ** the userid/groupid to be when sending. 1914 ** 1915 ** Returns: 1916 ** The exit code associated with the operation. 1917 ** 1918 ** Side Effects: 1919 ** none. 1920 */ 1921 1922 mailfile(filename, ctladdr, e) 1923 char *filename; 1924 ADDRESS *ctladdr; 1925 register ENVELOPE *e; 1926 { 1927 register FILE *f; 1928 register int pid; 1929 int mode; 1930 1931 if (tTd(11, 1)) 1932 { 1933 printf("mailfile %s\n ctladdr=", filename); 1934 printaddr(ctladdr, FALSE); 1935 } 1936 1937 if (e->e_xfp != NULL) 1938 fflush(e->e_xfp); 1939 1940 /* 1941 ** Fork so we can change permissions here. 1942 ** Note that we MUST use fork, not vfork, because of 1943 ** the complications of calling subroutines, etc. 1944 */ 1945 1946 DOFORK(fork); 1947 1948 if (pid < 0) 1949 return (EX_OSERR); 1950 else if (pid == 0) 1951 { 1952 /* child -- actually write to file */ 1953 struct stat stb; 1954 1955 (void) setsignal(SIGINT, SIG_DFL); 1956 (void) setsignal(SIGHUP, SIG_DFL); 1957 (void) setsignal(SIGTERM, SIG_DFL); 1958 (void) umask(OldUmask); 1959 1960 if (stat(filename, &stb) < 0) 1961 stb.st_mode = FileMode; 1962 mode = stb.st_mode; 1963 1964 /* limit the errors to those actually caused in the child */ 1965 errno = 0; 1966 ExitStat = EX_OK; 1967 1968 if (bitset(0111, stb.st_mode)) 1969 exit(EX_CANTCREAT); 1970 if (ctladdr == NULL) 1971 ctladdr = &e->e_from; 1972 else 1973 { 1974 /* ignore setuid and setgid bits */ 1975 mode &= ~(S_ISGID|S_ISUID); 1976 } 1977 1978 /* we have to open the dfile BEFORE setuid */ 1979 if (e->e_dfp == NULL && e->e_df != NULL) 1980 { 1981 e->e_dfp = fopen(e->e_df, "r"); 1982 if (e->e_dfp == NULL) 1983 { 1984 syserr("mailfile: Cannot open %s for %s from %s", 1985 e->e_df, e->e_to, e->e_from.q_paddr); 1986 } 1987 } 1988 1989 if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 1990 { 1991 if (ctladdr->q_uid == 0) 1992 { 1993 (void) initgroups(DefUser, DefGid); 1994 } 1995 else 1996 { 1997 (void) initgroups(ctladdr->q_ruser ? 1998 ctladdr->q_ruser : ctladdr->q_user, 1999 ctladdr->q_gid); 2000 } 2001 } 2002 if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 2003 { 2004 if (ctladdr->q_uid == 0) 2005 (void) setuid(DefUid); 2006 else 2007 (void) setuid(ctladdr->q_uid); 2008 } 2009 FileName = filename; 2010 LineNumber = 0; 2011 f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode); 2012 if (f == NULL) 2013 { 2014 message("554 cannot open: %s", errstring(errno)); 2015 exit(EX_CANTCREAT); 2016 } 2017 2018 putfromline(f, FileMailer, e); 2019 (*e->e_puthdr)(f, FileMailer, e); 2020 putline("\n", f, FileMailer); 2021 (*e->e_putbody)(f, FileMailer, e, NULL); 2022 putline("\n", f, FileMailer); 2023 if (ferror(f)) 2024 { 2025 message("451 I/O error: %s", errstring(errno)); 2026 setstat(EX_IOERR); 2027 } 2028 (void) xfclose(f, "mailfile", filename); 2029 (void) fflush(stdout); 2030 2031 /* reset ISUID & ISGID bits for paranoid systems */ 2032 (void) chmod(filename, (int) stb.st_mode); 2033 exit(ExitStat); 2034 /*NOTREACHED*/ 2035 } 2036 else 2037 { 2038 /* parent -- wait for exit status */ 2039 int st; 2040 2041 st = waitfor(pid); 2042 if (WIFEXITED(st)) 2043 return (WEXITSTATUS(st)); 2044 else 2045 { 2046 syserr("child died on signal %d", st); 2047 return (EX_UNAVAILABLE); 2048 } 2049 /*NOTREACHED*/ 2050 } 2051 } 2052 /* 2053 ** HOSTSIGNATURE -- return the "signature" for a host. 2054 ** 2055 ** The signature describes how we are going to send this -- it 2056 ** can be just the hostname (for non-Internet hosts) or can be 2057 ** an ordered list of MX hosts. 2058 ** 2059 ** Parameters: 2060 ** m -- the mailer describing this host. 2061 ** host -- the host name. 2062 ** e -- the current envelope. 2063 ** 2064 ** Returns: 2065 ** The signature for this host. 2066 ** 2067 ** Side Effects: 2068 ** Can tweak the symbol table. 2069 */ 2070 2071 char * 2072 hostsignature(m, host, e) 2073 register MAILER *m; 2074 char *host; 2075 ENVELOPE *e; 2076 { 2077 register char *p; 2078 register STAB *s; 2079 int i; 2080 int len; 2081 #ifdef NAMED_BIND 2082 int nmx; 2083 auto int rcode; 2084 char *hp; 2085 char *endp; 2086 int oldoptions; 2087 char *mxhosts[MAXMXHOSTS + 1]; 2088 #endif 2089 2090 /* 2091 ** Check to see if this uses IPC -- if not, it can't have MX records. 2092 */ 2093 2094 p = m->m_mailer; 2095 if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 2096 { 2097 /* just an ordinary mailer */ 2098 return host; 2099 } 2100 2101 /* 2102 ** Look it up in the symbol table. 2103 */ 2104 2105 s = stab(host, ST_HOSTSIG, ST_ENTER); 2106 if (s->s_hostsig != NULL) 2107 return s->s_hostsig; 2108 2109 /* 2110 ** Not already there -- create a signature. 2111 */ 2112 2113 #ifdef NAMED_BIND 2114 if (ConfigLevel < 2) 2115 { 2116 oldoptions = _res.options; 2117 _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 2118 } 2119 2120 for (hp = host; hp != NULL; hp = endp) 2121 { 2122 endp = strchr(hp, ':'); 2123 if (endp != NULL) 2124 *endp = '\0'; 2125 2126 nmx = getmxrr(hp, mxhosts, TRUE, &rcode); 2127 2128 if (nmx <= 0) 2129 { 2130 register MCI *mci; 2131 extern int errno; 2132 2133 /* update the connection info for this host */ 2134 mci = mci_get(hp, m); 2135 mci->mci_exitstat = rcode; 2136 mci->mci_errno = errno; 2137 #ifdef NAMED_BIND 2138 mci->mci_herrno = h_errno; 2139 #endif 2140 2141 /* and return the original host name as the signature */ 2142 nmx = 1; 2143 mxhosts[0] = hp; 2144 } 2145 2146 len = 0; 2147 for (i = 0; i < nmx; i++) 2148 { 2149 len += strlen(mxhosts[i]) + 1; 2150 } 2151 if (s->s_hostsig != NULL) 2152 len += strlen(s->s_hostsig) + 1; 2153 p = xalloc(len); 2154 if (s->s_hostsig != NULL) 2155 { 2156 (void) strcpy(p, s->s_hostsig); 2157 free(s->s_hostsig); 2158 s->s_hostsig = p; 2159 p += strlen(p); 2160 *p++ = ':'; 2161 } 2162 else 2163 s->s_hostsig = p; 2164 for (i = 0; i < nmx; i++) 2165 { 2166 if (i != 0) 2167 *p++ = ':'; 2168 strcpy(p, mxhosts[i]); 2169 p += strlen(p); 2170 } 2171 if (endp != NULL) 2172 *endp++ = ':'; 2173 } 2174 makelower(s->s_hostsig); 2175 if (ConfigLevel < 2) 2176 _res.options = oldoptions; 2177 #else 2178 /* not using BIND -- the signature is just the host name */ 2179 s->s_hostsig = host; 2180 #endif 2181 if (tTd(17, 1)) 2182 printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 2183 return s->s_hostsig; 2184 } 2185