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