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