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