1 /* 2 * Copyright (c) 1998 Sendmail, Inc. All rights reserved. 3 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved. 4 * Copyright (c) 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * By using this file, you agree to the terms and conditions set 8 * forth in the LICENSE file which can be found at the top level of 9 * the sendmail distribution. 10 * 11 */ 12 13 #ifndef lint 14 static char sccsid[] = "@(#)deliver.c 8.353 (Berkeley) 6/30/98"; 15 #endif /* not lint */ 16 17 #include "sendmail.h" 18 #include <errno.h> 19 #include <grp.h> 20 #if NAMED_BIND 21 #include <resolv.h> 22 #endif 23 24 #if HASSETUSERCONTEXT 25 # include <login_cap.h> 26 #endif 27 28 #if SMTP 29 extern char SmtpError[]; 30 #endif 31 32 /* 33 ** SENDALL -- actually send all the messages. 34 ** 35 ** Parameters: 36 ** e -- the envelope to send. 37 ** mode -- the delivery mode to use. If SM_DEFAULT, use 38 ** the current e->e_sendmode. 39 ** 40 ** Returns: 41 ** none. 42 ** 43 ** Side Effects: 44 ** Scans the send lists and sends everything it finds. 45 ** Delivers any appropriate error messages. 46 ** If we are running in a non-interactive mode, takes the 47 ** appropriate action. 48 */ 49 50 void 51 sendall(e, mode) 52 ENVELOPE *e; 53 int mode; 54 { 55 register ADDRESS *q; 56 char *owner; 57 int otherowners; 58 register ENVELOPE *ee; 59 ENVELOPE *splitenv = NULL; 60 int oldverbose = Verbose; 61 bool somedeliveries = FALSE, expensive = FALSE; 62 pid_t pid; 63 void sendenvelope __P((ENVELOPE *, int)); 64 65 /* 66 ** If this message is to be discarded, don't bother sending 67 ** the message at all. 68 */ 69 70 if (bitset(EF_DISCARD, e->e_flags)) 71 { 72 if (tTd(13, 1)) 73 printf("sendall: discarding id %s\n", e->e_id); 74 e->e_flags |= EF_CLRQUEUE; 75 if (LogLevel > 4) 76 sm_syslog(LOG_INFO, e->e_id, "discarded"); 77 markstats(e, NULL, TRUE); 78 return; 79 } 80 81 /* 82 ** If we have had global, fatal errors, don't bother sending 83 ** the message at all if we are in SMTP mode. Local errors 84 ** (e.g., a single address failing) will still cause the other 85 ** addresses to be sent. 86 */ 87 88 if (bitset(EF_FATALERRS, e->e_flags) && 89 (OpMode == MD_SMTP || OpMode == MD_DAEMON)) 90 { 91 e->e_flags |= EF_CLRQUEUE; 92 return; 93 } 94 95 /* determine actual delivery mode */ 96 if (mode == SM_DEFAULT) 97 { 98 mode = e->e_sendmode; 99 if (mode != SM_VERIFY && mode != SM_DEFER && 100 shouldqueue(e->e_msgpriority, e->e_ctime)) 101 mode = SM_QUEUE; 102 } 103 104 if (tTd(13, 1)) 105 { 106 extern void printenvflags __P((ENVELOPE *)); 107 108 printf("\n===== SENDALL: mode %c, id %s, e_from ", 109 mode, e->e_id); 110 printaddr(&e->e_from, FALSE); 111 printf("\te_flags = "); 112 printenvflags(e); 113 printf("sendqueue:\n"); 114 printaddr(e->e_sendqueue, TRUE); 115 } 116 117 /* 118 ** Do any preprocessing necessary for the mode we are running. 119 ** Check to make sure the hop count is reasonable. 120 ** Delete sends to the sender in mailing lists. 121 */ 122 123 CurEnv = e; 124 if (tTd(62, 1)) 125 checkfds(NULL); 126 127 if (e->e_hopcount > MaxHopCount) 128 { 129 errno = 0; 130 #if QUEUE 131 queueup(e, mode == SM_QUEUE || mode == SM_DEFER); 132 #endif 133 e->e_flags |= EF_FATALERRS|EF_PM_NOTIFY|EF_CLRQUEUE; 134 syserr("554 Too many hops %d (%d max): from %s via %s, to %s", 135 e->e_hopcount, MaxHopCount, e->e_from.q_paddr, 136 RealHostName == NULL ? "localhost" : RealHostName, 137 e->e_sendqueue->q_paddr); 138 e->e_sendqueue->q_status = "5.4.6"; 139 return; 140 } 141 142 /* 143 ** Do sender deletion. 144 ** 145 ** If the sender has the QQUEUEUP flag set, skip this. 146 ** This can happen if the name server is hosed when you 147 ** are trying to send mail. The result is that the sender 148 ** is instantiated in the queue as a recipient. 149 */ 150 151 if (!bitset(EF_METOO, e->e_flags) && 152 !bitset(QQUEUEUP, e->e_from.q_flags)) 153 { 154 if (tTd(13, 5)) 155 { 156 printf("sendall: QDONTSEND "); 157 printaddr(&e->e_from, FALSE); 158 } 159 e->e_from.q_flags |= QDONTSEND; 160 (void) recipient(&e->e_from, &e->e_sendqueue, 0, e); 161 } 162 163 /* 164 ** Handle alias owners. 165 ** 166 ** We scan up the q_alias chain looking for owners. 167 ** We discard owners that are the same as the return path. 168 */ 169 170 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 171 { 172 register struct address *a; 173 174 for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias) 175 continue; 176 if (a != NULL) 177 q->q_owner = a->q_owner; 178 179 if (q->q_owner != NULL && 180 !bitset(QDONTSEND, q->q_flags) && 181 strcmp(q->q_owner, e->e_from.q_paddr) == 0) 182 q->q_owner = NULL; 183 } 184 185 if (tTd(13, 25)) 186 { 187 printf("\nAfter first owner pass, sendq =\n"); 188 printaddr(e->e_sendqueue, TRUE); 189 } 190 191 owner = ""; 192 otherowners = 1; 193 while (owner != NULL && otherowners > 0) 194 { 195 if (tTd(13, 28)) 196 printf("owner = \"%s\", otherowners = %d\n", 197 owner, otherowners); 198 owner = NULL; 199 otherowners = bitset(EF_SENDRECEIPT, e->e_flags) ? 1 : 0; 200 201 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 202 { 203 if (tTd(13, 30)) 204 { 205 printf("Checking "); 206 printaddr(q, FALSE); 207 } 208 if (bitset(QDONTSEND, q->q_flags)) 209 { 210 if (tTd(13, 30)) 211 printf(" ... QDONTSEND\n"); 212 continue; 213 } 214 if (tTd(13, 29) && !tTd(13, 30)) 215 { 216 printf("Checking "); 217 printaddr(q, FALSE); 218 } 219 220 if (q->q_owner != NULL) 221 { 222 if (owner == NULL) 223 { 224 if (tTd(13, 40)) 225 printf(" ... First owner = \"%s\"\n", 226 q->q_owner); 227 owner = q->q_owner; 228 } 229 else if (owner != q->q_owner) 230 { 231 if (strcmp(owner, q->q_owner) == 0) 232 { 233 if (tTd(13, 40)) 234 printf(" ... Same owner = \"%s\"\n", 235 owner); 236 237 /* make future comparisons cheap */ 238 q->q_owner = owner; 239 } 240 else 241 { 242 if (tTd(13, 40)) 243 printf(" ... Another owner \"%s\"\n", 244 q->q_owner); 245 otherowners++; 246 } 247 owner = q->q_owner; 248 } 249 else if (tTd(13, 40)) 250 printf(" ... Same owner = \"%s\"\n", 251 owner); 252 } 253 else 254 { 255 if (tTd(13, 40)) 256 printf(" ... Null owner\n"); 257 otherowners++; 258 } 259 260 /* 261 ** If this mailer is expensive, and if we don't 262 ** want to make connections now, just mark these 263 ** addresses and return. This is useful if we 264 ** want to batch connections to reduce load. This 265 ** will cause the messages to be queued up, and a 266 ** daemon will come along to send the messages later. 267 */ 268 269 if (bitset(QBADADDR|QQUEUEUP, q->q_flags)) 270 { 271 if (tTd(13, 30)) 272 printf(" ... QBADADDR|QQUEUEUP\n"); 273 continue; 274 } 275 if (NoConnect && !Verbose && 276 bitnset(M_EXPENSIVE, q->q_mailer->m_flags)) 277 { 278 if (tTd(13, 30)) 279 printf(" ... expensive\n"); 280 q->q_flags |= QQUEUEUP; 281 expensive = TRUE; 282 } 283 else 284 { 285 if (tTd(13, 30)) 286 printf(" ... deliverable\n"); 287 somedeliveries = TRUE; 288 } 289 } 290 291 if (owner != NULL && otherowners > 0) 292 { 293 extern HDR *copyheader __P((HDR *)); 294 extern ADDRESS *copyqueue __P((ADDRESS *)); 295 extern void dup_queue_file __P((ENVELOPE *, ENVELOPE *, int)); 296 297 /* 298 ** Split this envelope into two. 299 */ 300 301 ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE)); 302 *ee = *e; 303 ee->e_id = NULL; 304 (void) queuename(ee, '\0'); 305 306 if (tTd(13, 1)) 307 printf("sendall: split %s into %s, owner = \"%s\", otherowners = %d\n", 308 e->e_id, ee->e_id, owner, otherowners); 309 310 ee->e_header = copyheader(e->e_header); 311 ee->e_sendqueue = copyqueue(e->e_sendqueue); 312 ee->e_errorqueue = copyqueue(e->e_errorqueue); 313 ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS|EF_SENDRECEIPT|EF_RET_PARAM); 314 ee->e_flags |= EF_NORECEIPT; 315 setsender(owner, ee, NULL, '\0', TRUE); 316 if (tTd(13, 5)) 317 { 318 printf("sendall(split): QDONTSEND "); 319 printaddr(&ee->e_from, FALSE); 320 } 321 ee->e_from.q_flags |= QDONTSEND; 322 ee->e_dfp = NULL; 323 ee->e_xfp = NULL; 324 ee->e_errormode = EM_MAIL; 325 ee->e_sibling = splitenv; 326 splitenv = ee; 327 328 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 329 { 330 if (q->q_owner == owner) 331 { 332 q->q_flags |= QDONTSEND; 333 q->q_flags &= ~(QQUEUEUP|QBADADDR); 334 if (tTd(13, 6)) 335 printf("\t... stripping %s from original envelope\n", 336 q->q_paddr); 337 } 338 } 339 for (q = ee->e_sendqueue; q != NULL; q = q->q_next) 340 { 341 if (q->q_owner != owner) 342 { 343 q->q_flags |= QDONTSEND; 344 q->q_flags &= ~(QQUEUEUP|QBADADDR); 345 if (tTd(13, 6)) 346 printf("\t... dropping %s from cloned envelope\n", 347 q->q_paddr); 348 } 349 else 350 { 351 /* clear DSN parameters */ 352 q->q_flags &= ~(QHASNOTIFY|Q_PINGFLAGS); 353 q->q_flags |= DefaultNotify & ~QPINGONSUCCESS; 354 if (tTd(13, 6)) 355 printf("\t... moving %s to cloned envelope\n", 356 q->q_paddr); 357 } 358 } 359 360 if (mode != SM_VERIFY && bitset(EF_HAS_DF, e->e_flags)) 361 dup_queue_file(e, ee, 'd'); 362 openxscript(ee); 363 if (LogLevel > 4) 364 sm_syslog(LOG_INFO, ee->e_id, 365 "clone %s, owner=%s", 366 e->e_id, owner); 367 } 368 } 369 370 if (owner != NULL) 371 { 372 setsender(owner, e, NULL, '\0', TRUE); 373 if (tTd(13, 5)) 374 { 375 printf("sendall(owner): QDONTSEND "); 376 printaddr(&e->e_from, FALSE); 377 } 378 e->e_from.q_flags |= QDONTSEND; 379 e->e_errormode = EM_MAIL; 380 e->e_flags |= EF_NORECEIPT; 381 e->e_flags &= ~EF_FATALERRS; 382 } 383 384 /* if nothing to be delivered, just queue up everything */ 385 if (!somedeliveries && mode != SM_QUEUE && mode != SM_DEFER && 386 mode != SM_VERIFY) 387 { 388 if (tTd(13, 29)) 389 printf("No deliveries: auto-queuing\n"); 390 mode = SM_QUEUE; 391 392 /* treat this as a delivery in terms of counting tries */ 393 e->e_dtime = curtime(); 394 if (!expensive) 395 e->e_ntries++; 396 for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 397 { 398 ee->e_dtime = curtime(); 399 if (!expensive) 400 ee->e_ntries++; 401 } 402 } 403 404 # if QUEUE 405 if ((mode == SM_QUEUE || mode == SM_DEFER || mode == SM_FORK || 406 (mode != SM_VERIFY && SuperSafe)) && 407 (!bitset(EF_INQUEUE, e->e_flags) || splitenv != NULL)) 408 { 409 /* be sure everything is instantiated in the queue */ 410 queueup(e, mode == SM_QUEUE || mode == SM_DEFER); 411 for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 412 queueup(ee, mode == SM_QUEUE || mode == SM_DEFER); 413 } 414 #endif /* QUEUE */ 415 416 if (tTd(62, 10)) 417 checkfds("after envelope splitting"); 418 419 /* 420 ** If we belong in background, fork now. 421 */ 422 423 if (tTd(13, 20)) 424 { 425 printf("sendall: final mode = %c\n", mode); 426 if (tTd(13, 21)) 427 { 428 printf("\n================ Final Send Queue(s) =====================\n"); 429 printf("\n *** Envelope %s, e_from=%s ***\n", 430 e->e_id, e->e_from.q_paddr); 431 printaddr(e->e_sendqueue, TRUE); 432 for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 433 { 434 printf("\n *** Envelope %s, e_from=%s ***\n", 435 ee->e_id, ee->e_from.q_paddr); 436 printaddr(ee->e_sendqueue, TRUE); 437 } 438 printf("==========================================================\n\n"); 439 } 440 } 441 switch (mode) 442 { 443 case SM_VERIFY: 444 Verbose = 2; 445 break; 446 447 case SM_QUEUE: 448 case SM_DEFER: 449 # if HASFLOCK 450 queueonly: 451 # endif 452 if (e->e_nrcpts > 0) 453 e->e_flags |= EF_INQUEUE; 454 dropenvelope(e, splitenv != NULL); 455 for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 456 { 457 if (ee->e_nrcpts > 0) 458 ee->e_flags |= EF_INQUEUE; 459 dropenvelope(ee, FALSE); 460 } 461 return; 462 463 case SM_FORK: 464 if (e->e_xfp != NULL) 465 (void) fflush(e->e_xfp); 466 467 # if !HASFLOCK 468 /* 469 ** Since fcntl locking has the interesting semantic that 470 ** the lock is owned by a process, not by an open file 471 ** descriptor, we have to flush this to the queue, and 472 ** then restart from scratch in the child. 473 */ 474 475 { 476 /* save id for future use */ 477 char *qid = e->e_id; 478 479 /* now drop the envelope in the parent */ 480 e->e_flags |= EF_INQUEUE; 481 dropenvelope(e, splitenv != NULL); 482 483 /* arrange to reacquire lock after fork */ 484 e->e_id = qid; 485 } 486 487 for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 488 { 489 /* save id for future use */ 490 char *qid = ee->e_id; 491 492 /* drop envelope in parent */ 493 ee->e_flags |= EF_INQUEUE; 494 dropenvelope(ee, FALSE); 495 496 /* and save qid for reacquisition */ 497 ee->e_id = qid; 498 } 499 500 # endif /* !HASFLOCK */ 501 502 pid = fork(); 503 if (pid < 0) 504 { 505 # if HASFLOCK 506 goto queueonly; 507 # else 508 e->e_id = NULL; 509 for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 510 ee->e_id = NULL; 511 return; 512 # endif /* HASFLOCK */ 513 } 514 else if (pid > 0) 515 { 516 # if HASFLOCK 517 /* be sure we leave the temp files to our child */ 518 /* close any random open files in the envelope */ 519 closexscript(e); 520 if (e->e_dfp != NULL) 521 (void) xfclose(e->e_dfp, "sendenvelope dfp", e->e_id); 522 e->e_dfp = NULL; 523 e->e_flags &= ~EF_HAS_DF; 524 525 /* can't call unlockqueue to avoid unlink of xfp */ 526 if (e->e_lockfp != NULL) 527 (void) xfclose(e->e_lockfp, "sendenvelope lockfp", e->e_id); 528 e->e_lockfp = NULL; 529 # endif 530 531 /* make sure the parent doesn't own the envelope */ 532 e->e_id = NULL; 533 534 /* catch intermediate zombie */ 535 (void) waitfor(pid); 536 return; 537 } 538 539 /* double fork to avoid zombies */ 540 pid = fork(); 541 if (pid > 0) 542 exit(EX_OK); 543 544 /* be sure we are immune from the terminal */ 545 disconnect(2, e); 546 547 /* prevent parent from waiting if there was an error */ 548 if (pid < 0) 549 { 550 # if HASFLOCK 551 e->e_flags |= EF_INQUEUE; 552 # else 553 e->e_id = NULL; 554 # endif /* HASFLOCK */ 555 finis(); 556 } 557 558 /* be sure to give error messages in child */ 559 QuickAbort = FALSE; 560 561 /* 562 ** Close any cached connections. 563 ** 564 ** We don't send the QUIT protocol because the parent 565 ** still knows about the connection. 566 ** 567 ** This should only happen when delivering an error 568 ** message. 569 */ 570 571 mci_flush(FALSE, NULL); 572 573 # if HASFLOCK 574 break; 575 # else 576 577 /* 578 ** Now reacquire and run the various queue files. 579 */ 580 581 for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 582 { 583 ENVELOPE *sibling = ee->e_sibling; 584 585 (void) dowork(ee->e_id, FALSE, FALSE, ee); 586 ee->e_sibling = sibling; 587 } 588 (void) dowork(e->e_id, FALSE, FALSE, e); 589 finis(); 590 # endif /* !HASFLOCK */ 591 } 592 593 sendenvelope(e, mode); 594 dropenvelope(e, TRUE); 595 for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 596 { 597 CurEnv = ee; 598 if (mode != SM_VERIFY) 599 openxscript(ee); 600 sendenvelope(ee, mode); 601 dropenvelope(ee, TRUE); 602 } 603 CurEnv = e; 604 605 Verbose = oldverbose; 606 if (mode == SM_FORK) 607 finis(); 608 } 609 610 void 611 sendenvelope(e, mode) 612 register ENVELOPE *e; 613 int mode; 614 { 615 register ADDRESS *q; 616 bool didany; 617 618 if (tTd(13, 10)) 619 printf("sendenvelope(%s) e_flags=0x%lx\n", 620 e->e_id == NULL ? "[NOQUEUE]" : e->e_id, 621 e->e_flags); 622 if (LogLevel > 80) 623 sm_syslog(LOG_DEBUG, e->e_id, 624 "sendenvelope, flags=0x%x", 625 e->e_flags); 626 627 /* 628 ** If we have had global, fatal errors, don't bother sending 629 ** the message at all if we are in SMTP mode. Local errors 630 ** (e.g., a single address failing) will still cause the other 631 ** addresses to be sent. 632 */ 633 634 if (bitset(EF_FATALERRS, e->e_flags) && 635 (OpMode == MD_SMTP || OpMode == MD_DAEMON)) 636 { 637 e->e_flags |= EF_CLRQUEUE; 638 return; 639 } 640 641 /* 642 ** Run through the list and send everything. 643 ** 644 ** Set EF_GLOBALERRS so that error messages during delivery 645 ** result in returned mail. 646 */ 647 648 e->e_nsent = 0; 649 e->e_flags |= EF_GLOBALERRS; 650 define(macid("{envid}", NULL), e->e_envid, e); 651 define(macid("{bodytype}", NULL), e->e_bodytype, e); 652 didany = FALSE; 653 654 /* now run through the queue */ 655 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 656 { 657 #if XDEBUG 658 char wbuf[MAXNAME + 20]; 659 660 (void) snprintf(wbuf, sizeof wbuf, "sendall(%.*s)", 661 MAXNAME, q->q_paddr); 662 checkfd012(wbuf); 663 #endif 664 if (mode == SM_VERIFY) 665 { 666 e->e_to = q->q_paddr; 667 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 668 { 669 if (q->q_host != NULL && q->q_host[0] != '\0') 670 message("deliverable: mailer %s, host %s, user %s", 671 q->q_mailer->m_name, 672 q->q_host, 673 q->q_user); 674 else 675 message("deliverable: mailer %s, user %s", 676 q->q_mailer->m_name, 677 q->q_user); 678 } 679 } 680 else if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 681 { 682 extern int deliver __P((ENVELOPE *, ADDRESS *)); 683 684 # if QUEUE 685 /* 686 ** Checkpoint the send list every few addresses 687 */ 688 689 if (e->e_nsent >= CheckpointInterval) 690 { 691 queueup(e, FALSE); 692 e->e_nsent = 0; 693 } 694 # endif /* QUEUE */ 695 (void) deliver(e, q); 696 didany = TRUE; 697 } 698 } 699 if (didany) 700 { 701 e->e_dtime = curtime(); 702 e->e_ntries++; 703 } 704 705 #if XDEBUG 706 checkfd012("end of sendenvelope"); 707 #endif 708 } 709 /* 710 ** DUP_QUEUE_FILE -- duplicate a queue file into a split queue 711 ** 712 ** Parameters: 713 ** e -- the existing envelope 714 ** ee -- the new envelope 715 ** type -- the queue file type (e.g., 'd') 716 ** 717 ** Returns: 718 ** none 719 */ 720 721 void 722 dup_queue_file(e, ee, type) 723 struct envelope *e, *ee; 724 int type; 725 { 726 char f1buf[MAXQFNAME], f2buf[MAXQFNAME]; 727 728 ee->e_dfp = NULL; 729 ee->e_xfp = NULL; 730 snprintf(f1buf, sizeof f1buf, "%s", queuename(e, type)); 731 snprintf(f2buf, sizeof f2buf, "%s", queuename(ee, type)); 732 if (link(f1buf, f2buf) < 0) 733 { 734 int saverrno = errno; 735 736 syserr("sendall: link(%s, %s)", f1buf, f2buf); 737 if (saverrno == EEXIST) 738 { 739 if (unlink(f2buf) < 0) 740 { 741 syserr("!sendall: unlink(%s): permanent", 742 f2buf); 743 /*NOTREACHED*/ 744 } 745 if (link(f1buf, f2buf) < 0) 746 { 747 syserr("!sendall: link(%s, %s): permanent", 748 f1buf, f2buf); 749 /*NOTREACHED*/ 750 } 751 } 752 } 753 } 754 /* 755 ** DOFORK -- do a fork, retrying a couple of times on failure. 756 ** 757 ** This MUST be a macro, since after a vfork we are running 758 ** two processes on the same stack!!! 759 ** 760 ** Parameters: 761 ** none. 762 ** 763 ** Returns: 764 ** From a macro??? You've got to be kidding! 765 ** 766 ** Side Effects: 767 ** Modifies the ==> LOCAL <== variable 'pid', leaving: 768 ** pid of child in parent, zero in child. 769 ** -1 on unrecoverable error. 770 ** 771 ** Notes: 772 ** I'm awfully sorry this looks so awful. That's 773 ** vfork for you..... 774 */ 775 776 # define NFORKTRIES 5 777 778 # ifndef FORK 779 # define FORK fork 780 # endif 781 782 # define DOFORK(fORKfN) \ 783 {\ 784 register int i;\ 785 \ 786 for (i = NFORKTRIES; --i >= 0; )\ 787 {\ 788 pid = fORKfN();\ 789 if (pid >= 0)\ 790 break;\ 791 if (i > 0)\ 792 sleep((unsigned) NFORKTRIES - i);\ 793 }\ 794 } 795 /* 796 ** DOFORK -- simple fork interface to DOFORK. 797 ** 798 ** Parameters: 799 ** none. 800 ** 801 ** Returns: 802 ** pid of child in parent. 803 ** zero in child. 804 ** -1 on error. 805 ** 806 ** Side Effects: 807 ** returns twice, once in parent and once in child. 808 */ 809 810 int 811 dofork() 812 { 813 register pid_t pid = -1; 814 815 DOFORK(fork); 816 return (pid); 817 } 818 /* 819 ** DELIVER -- Deliver a message to a list of addresses. 820 ** 821 ** This routine delivers to everyone on the same host as the 822 ** user on the head of the list. It is clever about mailers 823 ** that don't handle multiple users. It is NOT guaranteed 824 ** that it will deliver to all these addresses however -- so 825 ** deliver should be called once for each address on the 826 ** list. 827 ** 828 ** Parameters: 829 ** e -- the envelope to deliver. 830 ** firstto -- head of the address list to deliver to. 831 ** 832 ** Returns: 833 ** zero -- successfully delivered. 834 ** else -- some failure, see ExitStat for more info. 835 ** 836 ** Side Effects: 837 ** The standard input is passed off to someone. 838 */ 839 840 #ifndef NO_UID 841 # define NO_UID -1 842 #endif 843 #ifndef NO_GID 844 # define NO_GID -1 845 #endif 846 847 int 848 deliver(e, firstto) 849 register ENVELOPE *e; 850 ADDRESS *firstto; 851 { 852 char *host; /* host being sent to */ 853 char *user; /* user being sent to */ 854 char **pvp; 855 register char **mvp; 856 register char *p; 857 register MAILER *m; /* mailer for this recipient */ 858 ADDRESS *volatile ctladdr; 859 ADDRESS *volatile contextaddr = NULL; 860 register MCI *volatile mci; 861 register ADDRESS *to = firstto; 862 volatile bool clever = FALSE; /* running user smtp to this mailer */ 863 ADDRESS *volatile tochain = NULL; /* users chain in this mailer call */ 864 int rcode; /* response code */ 865 int lmtp_rcode = EX_OK; 866 char *firstsig; /* signature of firstto */ 867 pid_t pid = -1; 868 char *volatile curhost; 869 register u_short port = 0; 870 time_t xstart; 871 bool suidwarn; 872 bool anyok; /* at least one address was OK */ 873 bool goodmxfound = FALSE; /* at least one MX was OK */ 874 int mpvect[2]; 875 int rpvect[2]; 876 char *pv[MAXPV+1]; 877 char tobuf[TOBUFSIZE]; /* text line of to people */ 878 char buf[MAXNAME + 1]; 879 char rpathbuf[MAXNAME + 1]; /* translated return path */ 880 extern int checkcompat __P((ADDRESS *, ENVELOPE *)); 881 extern void markfailure __P((ENVELOPE *, ADDRESS *, MCI *, int)); 882 883 errno = 0; 884 if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags)) 885 return (0); 886 887 suidwarn = geteuid() == 0; 888 889 #if NAMED_BIND 890 /* unless interactive, try twice, over a minute */ 891 if (OpMode == MD_DAEMON || OpMode == MD_SMTP) 892 { 893 _res.retrans = 30; 894 _res.retry = 2; 895 } 896 #endif 897 898 m = to->q_mailer; 899 host = to->q_host; 900 CurEnv = e; /* just in case */ 901 e->e_statmsg = NULL; 902 #if SMTP 903 SmtpError[0] = '\0'; 904 #endif 905 xstart = curtime(); 906 907 if (tTd(10, 1)) 908 printf("\n--deliver, id=%s, mailer=%s, host=`%s', first user=`%s'\n", 909 e->e_id, m->m_name, host, to->q_user); 910 if (tTd(10, 100)) 911 printopenfds(FALSE); 912 913 /* 914 ** Clear $&{client_*} macros if this is a bounce message to 915 ** prevent rejection by check_compat ruleset. 916 */ 917 918 if (bitset(EF_RESPONSE, e->e_flags)) 919 { 920 define(macid("{client_name}", NULL), "", e); 921 define(macid("{client_addr}", NULL), "", e); 922 define(macid("{client_port}", NULL), "", e); 923 } 924 925 /* 926 ** Do initial argv setup. 927 ** Insert the mailer name. Notice that $x expansion is 928 ** NOT done on the mailer name. Then, if the mailer has 929 ** a picky -f flag, we insert it as appropriate. This 930 ** code does not check for 'pv' overflow; this places a 931 ** manifest lower limit of 4 for MAXPV. 932 ** The from address rewrite is expected to make 933 ** the address relative to the other end. 934 */ 935 936 /* rewrite from address, using rewriting rules */ 937 rcode = EX_OK; 938 if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags)) 939 p = e->e_sender; 940 else 941 p = e->e_from.q_paddr; 942 p = remotename(p, m, RF_SENDERADDR|RF_CANONICAL, &rcode, e); 943 if (strlen(p) >= (SIZE_T) sizeof rpathbuf) 944 { 945 p = shortenstring(p, MAXSHORTSTR); 946 syserr("remotename: huge return %s", p); 947 } 948 snprintf(rpathbuf, sizeof rpathbuf, "%s", p); 949 define('g', rpathbuf, e); /* translated return path */ 950 define('h', host, e); /* to host */ 951 Errors = 0; 952 pvp = pv; 953 *pvp++ = m->m_argv[0]; 954 955 /* insert -f or -r flag as appropriate */ 956 if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 957 { 958 if (bitnset(M_FOPT, m->m_flags)) 959 *pvp++ = "-f"; 960 else 961 *pvp++ = "-r"; 962 *pvp++ = newstr(rpathbuf); 963 } 964 965 /* 966 ** Append the other fixed parts of the argv. These run 967 ** up to the first entry containing "$u". There can only 968 ** be one of these, and there are only a few more slots 969 ** in the pv after it. 970 */ 971 972 for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 973 { 974 /* can't use strchr here because of sign extension problems */ 975 while (*p != '\0') 976 { 977 if ((*p++ & 0377) == MACROEXPAND) 978 { 979 if (*p == 'u') 980 break; 981 } 982 } 983 984 if (*p != '\0') 985 break; 986 987 /* this entry is safe -- go ahead and process it */ 988 expand(*mvp, buf, sizeof buf, e); 989 *pvp++ = newstr(buf); 990 if (pvp >= &pv[MAXPV - 3]) 991 { 992 syserr("554 Too many parameters to %s before $u", pv[0]); 993 return (-1); 994 } 995 } 996 997 /* 998 ** If we have no substitution for the user name in the argument 999 ** list, we know that we must supply the names otherwise -- and 1000 ** SMTP is the answer!! 1001 */ 1002 1003 if (*mvp == NULL) 1004 { 1005 /* running SMTP */ 1006 # if SMTP 1007 clever = TRUE; 1008 *pvp = NULL; 1009 # else /* SMTP */ 1010 /* oops! we don't implement SMTP */ 1011 syserr("554 SMTP style mailer not implemented"); 1012 return (EX_SOFTWARE); 1013 # endif /* SMTP */ 1014 } 1015 1016 /* 1017 ** At this point *mvp points to the argument with $u. We 1018 ** run through our address list and append all the addresses 1019 ** we can. If we run out of space, do not fret! We can 1020 ** always send another copy later. 1021 */ 1022 1023 tobuf[0] = '\0'; 1024 e->e_to = tobuf; 1025 ctladdr = NULL; 1026 firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e); 1027 for (; to != NULL; to = to->q_next) 1028 { 1029 /* avoid sending multiple recipients to dumb mailers */ 1030 if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 1031 break; 1032 1033 /* if already sent or not for this host, don't send */ 1034 if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 1035 to->q_mailer != firstto->q_mailer || 1036 strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0) 1037 continue; 1038 1039 /* avoid overflowing tobuf */ 1040 if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 1041 break; 1042 1043 if (tTd(10, 1)) 1044 { 1045 printf("\nsend to "); 1046 printaddr(to, FALSE); 1047 } 1048 1049 /* compute effective uid/gid when sending */ 1050 if (bitnset(M_RUNASRCPT, to->q_mailer->m_flags)) 1051 contextaddr = ctladdr = getctladdr(to); 1052 1053 if (tTd(10, 2)) 1054 { 1055 printf("ctladdr="); 1056 printaddr(ctladdr, FALSE); 1057 } 1058 1059 user = to->q_user; 1060 e->e_to = to->q_paddr; 1061 if (tTd(10, 5)) 1062 { 1063 printf("deliver: QDONTSEND "); 1064 printaddr(to, FALSE); 1065 } 1066 to->q_flags |= QDONTSEND; 1067 1068 /* 1069 ** Check to see that these people are allowed to 1070 ** talk to each other. 1071 */ 1072 1073 if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 1074 { 1075 e->e_flags |= EF_NO_BODY_RETN; 1076 if (bitnset(M_LOCALMAILER, to->q_mailer->m_flags)) 1077 to->q_status = "5.2.3"; 1078 else 1079 to->q_status = "5.3.4"; 1080 usrerr("552 Message is too large; %ld bytes max", m->m_maxsize); 1081 markfailure(e, to, NULL, EX_UNAVAILABLE); 1082 giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, xstart, e); 1083 continue; 1084 } 1085 #if NAMED_BIND 1086 h_errno = 0; 1087 #endif 1088 1089 /* do config file checking of compatibility */ 1090 rcode = rscheck("check_compat", 1091 e->e_from.q_paddr, to->q_paddr, e); 1092 if (rcode == EX_OK) 1093 { 1094 /* do in-code checking */ 1095 rcode = checkcompat(to, e); 1096 } 1097 if (rcode != EX_OK) 1098 { 1099 markfailure(e, to, NULL, rcode); 1100 giveresponse(rcode, m, NULL, ctladdr, xstart, e); 1101 continue; 1102 } 1103 1104 /* 1105 ** Strip quote bits from names if the mailer is dumb 1106 ** about them. 1107 */ 1108 1109 if (bitnset(M_STRIPQ, m->m_flags)) 1110 { 1111 stripquotes(user); 1112 stripquotes(host); 1113 } 1114 1115 /* hack attack -- delivermail compatibility */ 1116 if (m == ProgMailer && *user == '|') 1117 user++; 1118 1119 /* 1120 ** If an error message has already been given, don't 1121 ** bother to send to this address. 1122 ** 1123 ** >>>>>>>>>> This clause assumes that the local mailer 1124 ** >> NOTE >> cannot do any further aliasing; that 1125 ** >>>>>>>>>> function is subsumed by sendmail. 1126 */ 1127 1128 if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 1129 continue; 1130 1131 /* 1132 ** See if this user name is "special". 1133 ** If the user name has a slash in it, assume that this 1134 ** is a file -- send it off without further ado. Note 1135 ** that this type of addresses is not processed along 1136 ** with the others, so we fudge on the To person. 1137 */ 1138 1139 if (strcmp(m->m_mailer, "[FILE]") == 0) 1140 { 1141 define('u', user, e); /* to user */ 1142 p = to->q_home; 1143 if (p == NULL && ctladdr != NULL) 1144 p = ctladdr->q_home; 1145 define('z', p, e); /* user's home */ 1146 expand(m->m_argv[1], buf, sizeof buf, e); 1147 if (strlen(buf) > 0) 1148 rcode = mailfile(buf, m, ctladdr, SFF_CREAT, e); 1149 else 1150 { 1151 syserr("empty filename specification for mailer %s", 1152 m->m_name); 1153 rcode = EX_CONFIG; 1154 } 1155 giveresponse(rcode, m, NULL, ctladdr, xstart, e); 1156 markfailure(e, to, NULL, rcode); 1157 e->e_nsent++; 1158 if (rcode == EX_OK) 1159 { 1160 to->q_flags |= QSENT; 1161 if (bitnset(M_LOCALMAILER, m->m_flags) && 1162 bitset(QPINGONSUCCESS, to->q_flags)) 1163 { 1164 to->q_flags |= QDELIVERED; 1165 to->q_status = "2.1.5"; 1166 fprintf(e->e_xfp, "%s... Successfully delivered\n", 1167 to->q_paddr); 1168 } 1169 } 1170 to->q_statdate = curtime(); 1171 markstats(e, to, FALSE); 1172 continue; 1173 } 1174 1175 /* 1176 ** Address is verified -- add this user to mailer 1177 ** argv, and add it to the print list of recipients. 1178 */ 1179 1180 /* link together the chain of recipients */ 1181 to->q_tchain = tochain; 1182 tochain = to; 1183 1184 /* create list of users for error messages */ 1185 (void) strcat(tobuf, ","); 1186 (void) strcat(tobuf, to->q_paddr); 1187 define('u', user, e); /* to user */ 1188 p = to->q_home; 1189 if (p == NULL && ctladdr != NULL) 1190 p = ctladdr->q_home; 1191 define('z', p, e); /* user's home */ 1192 1193 /* 1194 ** Expand out this user into argument list. 1195 */ 1196 1197 if (!clever) 1198 { 1199 expand(*mvp, buf, sizeof buf, e); 1200 *pvp++ = newstr(buf); 1201 if (pvp >= &pv[MAXPV - 2]) 1202 { 1203 /* allow some space for trailing parms */ 1204 break; 1205 } 1206 } 1207 } 1208 1209 /* see if any addresses still exist */ 1210 if (tobuf[0] == '\0') 1211 { 1212 define('g', (char *) NULL, e); 1213 return (0); 1214 } 1215 1216 /* print out messages as full list */ 1217 e->e_to = tobuf + 1; 1218 1219 /* 1220 ** Fill out any parameters after the $u parameter. 1221 */ 1222 1223 while (!clever && *++mvp != NULL) 1224 { 1225 expand(*mvp, buf, sizeof buf, e); 1226 *pvp++ = newstr(buf); 1227 if (pvp >= &pv[MAXPV]) 1228 syserr("554 deliver: pv overflow after $u for %s", pv[0]); 1229 } 1230 *pvp++ = NULL; 1231 1232 /* 1233 ** Call the mailer. 1234 ** The argument vector gets built, pipes 1235 ** are created as necessary, and we fork & exec as 1236 ** appropriate. 1237 ** If we are running SMTP, we just need to clean up. 1238 */ 1239 1240 /*XXX this seems a bit wierd */ 1241 if (ctladdr == NULL && m != ProgMailer && m != FileMailer && 1242 bitset(QGOODUID, e->e_from.q_flags)) 1243 ctladdr = &e->e_from; 1244 1245 #if NAMED_BIND 1246 if (ConfigLevel < 2) 1247 _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 1248 #endif 1249 1250 if (tTd(11, 1)) 1251 { 1252 printf("openmailer:"); 1253 printav(pv); 1254 } 1255 errno = 0; 1256 #if NAMED_BIND 1257 h_errno = 0; 1258 #endif 1259 1260 CurHostName = NULL; 1261 1262 /* 1263 ** Deal with the special case of mail handled through an IPC 1264 ** connection. 1265 ** In this case we don't actually fork. We must be 1266 ** running SMTP for this to work. We will return a 1267 ** zero pid to indicate that we are running IPC. 1268 ** We also handle a debug version that just talks to stdin/out. 1269 */ 1270 1271 curhost = NULL; 1272 SmtpPhase = NULL; 1273 mci = NULL; 1274 1275 #if XDEBUG 1276 { 1277 char wbuf[MAXLINE]; 1278 1279 /* make absolutely certain 0, 1, and 2 are in use */ 1280 snprintf(wbuf, sizeof wbuf, "%s... openmailer(%s)", 1281 shortenstring(e->e_to, MAXSHORTSTR), m->m_name); 1282 checkfd012(wbuf); 1283 } 1284 #endif 1285 1286 /* check for 8-bit available */ 1287 if (bitset(EF_HAS8BIT, e->e_flags) && 1288 bitnset(M_7BITS, m->m_flags) && 1289 (bitset(EF_DONT_MIME, e->e_flags) || 1290 !(bitset(MM_MIME8BIT, MimeMode) || 1291 (bitset(EF_IS_MIME, e->e_flags) && 1292 bitset(MM_CVTMIME, MimeMode))))) 1293 { 1294 usrerr("554 Cannot send 8-bit data to 7-bit destination"); 1295 rcode = EX_DATAERR; 1296 e->e_status = "5.6.3"; 1297 goto give_up; 1298 } 1299 1300 if (tTd(62, 8)) 1301 checkfds("before delivery"); 1302 1303 /* check for Local Person Communication -- not for mortals!!! */ 1304 if (strcmp(m->m_mailer, "[LPC]") == 0) 1305 { 1306 mci = (MCI *) xalloc(sizeof *mci); 1307 bzero((char *) mci, sizeof *mci); 1308 mci->mci_in = stdin; 1309 mci->mci_out = stdout; 1310 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 1311 mci->mci_mailer = m; 1312 } 1313 else if (strcmp(m->m_mailer, "[IPC]") == 0 || 1314 strcmp(m->m_mailer, "[TCP]") == 0) 1315 { 1316 #if DAEMON 1317 register int i; 1318 1319 if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0') 1320 { 1321 syserr("null host name for %s mailer", m->m_mailer); 1322 rcode = EX_CONFIG; 1323 goto give_up; 1324 } 1325 1326 CurHostName = pv[1]; 1327 curhost = hostsignature(m, pv[1], e); 1328 1329 if (curhost == NULL || curhost[0] == '\0') 1330 { 1331 syserr("null host signature for %s", pv[1]); 1332 rcode = EX_CONFIG; 1333 goto give_up; 1334 } 1335 1336 if (!clever) 1337 { 1338 syserr("554 non-clever IPC"); 1339 rcode = EX_CONFIG; 1340 goto give_up; 1341 } 1342 if (pv[2] != NULL) 1343 { 1344 port = htons(atoi(pv[2])); 1345 if (port == 0) 1346 { 1347 struct servent *sp = getservbyname(pv[2], "tcp"); 1348 1349 if (sp == NULL) 1350 syserr("Service %s unknown", pv[2]); 1351 else 1352 port = sp->s_port; 1353 } 1354 } 1355 tryhost: 1356 while (*curhost != '\0') 1357 { 1358 static char hostbuf[MAXNAME + 1]; 1359 extern int makeconnection __P((char *, u_short, MCI *, ENVELOPE *)); 1360 1361 /* pull the next host from the signature */ 1362 p = strchr(curhost, ':'); 1363 if (p == NULL) 1364 p = (char *) &curhost[strlen(curhost)]; 1365 if (p == curhost) 1366 { 1367 syserr("deliver: null host name in signature"); 1368 curhost++; 1369 continue; 1370 } 1371 i = p - curhost; 1372 if (i >= sizeof hostbuf) 1373 i = sizeof hostbuf - 1; 1374 strncpy(hostbuf, curhost, i); 1375 hostbuf[i] = '\0'; 1376 if (*p != '\0') 1377 p++; 1378 curhost = p; 1379 1380 /* see if we already know that this host is fried */ 1381 CurHostName = hostbuf; 1382 mci = mci_get(hostbuf, m); 1383 if (mci->mci_state != MCIS_CLOSED) 1384 { 1385 if (tTd(11, 1)) 1386 { 1387 printf("openmailer: "); 1388 mci_dump(mci, FALSE); 1389 } 1390 CurHostName = mci->mci_host; 1391 message("Using cached %sSMTP connection to %s via %s...", 1392 bitset(MCIF_ESMTP, mci->mci_flags) ? "E" : "", 1393 hostbuf, m->m_name); 1394 break; 1395 } 1396 mci->mci_mailer = m; 1397 if (mci->mci_exitstat != EX_OK) 1398 { 1399 if (mci->mci_exitstat == EX_TEMPFAIL) 1400 goodmxfound = TRUE; 1401 continue; 1402 } 1403 1404 if (mci_lock_host(mci) != EX_OK) 1405 { 1406 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL); 1407 goodmxfound = TRUE; 1408 continue; 1409 } 1410 1411 /* try the connection */ 1412 setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 1413 if (port == 0) 1414 message("Connecting to %s via %s...", 1415 hostbuf, m->m_name); 1416 else 1417 message("Connecting to %s port %d via %s...", 1418 hostbuf, ntohs(port), m->m_name); 1419 i = makeconnection(hostbuf, port, mci, e); 1420 mci->mci_lastuse = curtime(); 1421 mci->mci_exitstat = i; 1422 mci->mci_errno = errno; 1423 #if NAMED_BIND 1424 mci->mci_herrno = h_errno; 1425 #endif 1426 if (i == EX_OK) 1427 { 1428 goodmxfound = TRUE; 1429 mci->mci_state = MCIS_OPENING; 1430 mci_cache(mci); 1431 if (TrafficLogFile != NULL) 1432 fprintf(TrafficLogFile, "%05d === CONNECT %s\n", 1433 (int) getpid(), hostbuf); 1434 break; 1435 } 1436 else 1437 { 1438 if (tTd(11, 1)) 1439 printf("openmailer: makeconnection => stat=%d, errno=%d\n", 1440 i, errno); 1441 if (i == EX_TEMPFAIL) 1442 goodmxfound = TRUE; 1443 mci_unlock_host(mci); 1444 } 1445 1446 /* enter status of this host */ 1447 setstat(i); 1448 1449 /* should print some message here for -v mode */ 1450 } 1451 if (mci == NULL) 1452 { 1453 syserr("deliver: no host name"); 1454 rcode = EX_SOFTWARE; 1455 goto give_up; 1456 } 1457 mci->mci_pid = 0; 1458 #else /* no DAEMON */ 1459 syserr("554 openmailer: no IPC"); 1460 if (tTd(11, 1)) 1461 printf("openmailer: NULL\n"); 1462 rcode = EX_UNAVAILABLE; 1463 goto give_up; 1464 #endif /* DAEMON */ 1465 } 1466 else 1467 { 1468 /* flush any expired connections */ 1469 (void) mci_scan(NULL); 1470 mci = NULL; 1471 1472 #if SMTP 1473 if (bitnset(M_LMTP, m->m_flags)) 1474 { 1475 /* try to get a cached connection */ 1476 mci = mci_get(m->m_name, m); 1477 if (mci->mci_host == NULL) 1478 mci->mci_host = m->m_name; 1479 CurHostName = mci->mci_host; 1480 if (mci->mci_state != MCIS_CLOSED) 1481 { 1482 message("Using cached LMTP connection for %s...", 1483 m->m_name); 1484 goto do_transfer; 1485 } 1486 } 1487 #endif 1488 1489 /* announce the connection to verbose listeners */ 1490 if (host == NULL || host[0] == '\0') 1491 message("Connecting to %s...", m->m_name); 1492 else 1493 message("Connecting to %s via %s...", host, m->m_name); 1494 if (TrafficLogFile != NULL) 1495 { 1496 char **av; 1497 1498 fprintf(TrafficLogFile, "%05d === EXEC", (int) getpid()); 1499 for (av = pv; *av != NULL; av++) 1500 fprintf(TrafficLogFile, " %s", *av); 1501 fprintf(TrafficLogFile, "\n"); 1502 } 1503 1504 #if XDEBUG 1505 checkfd012("before creating mail pipe"); 1506 #endif 1507 1508 /* create a pipe to shove the mail through */ 1509 if (pipe(mpvect) < 0) 1510 { 1511 syserr("%s... openmailer(%s): pipe (to mailer)", 1512 shortenstring(e->e_to, MAXSHORTSTR), m->m_name); 1513 if (tTd(11, 1)) 1514 printf("openmailer: NULL\n"); 1515 rcode = EX_OSERR; 1516 goto give_up; 1517 } 1518 1519 #if XDEBUG 1520 /* make sure we didn't get one of the standard I/O files */ 1521 if (mpvect[0] < 3 || mpvect[1] < 3) 1522 { 1523 syserr("%s... openmailer(%s): bogus mpvect %d %d", 1524 shortenstring(e->e_to, MAXSHORTSTR), m->m_name, 1525 mpvect[0], mpvect[1]); 1526 printopenfds(TRUE); 1527 if (tTd(11, 1)) 1528 printf("openmailer: NULL\n"); 1529 rcode = EX_OSERR; 1530 goto give_up; 1531 } 1532 1533 /* make sure system call isn't dead meat */ 1534 checkfdopen(mpvect[0], "mpvect[0]"); 1535 checkfdopen(mpvect[1], "mpvect[1]"); 1536 if (mpvect[0] == mpvect[1] || 1537 (e->e_lockfp != NULL && 1538 (mpvect[0] == fileno(e->e_lockfp) || 1539 mpvect[1] == fileno(e->e_lockfp)))) 1540 { 1541 if (e->e_lockfp == NULL) 1542 syserr("%s... openmailer(%s): overlapping mpvect %d %d", 1543 shortenstring(e->e_to, MAXSHORTSTR), 1544 m->m_name, mpvect[0], mpvect[1]); 1545 else 1546 syserr("%s... openmailer(%s): overlapping mpvect %d %d, lockfp = %d", 1547 shortenstring(e->e_to, MAXSHORTSTR), 1548 m->m_name, mpvect[0], mpvect[1], 1549 fileno(e->e_lockfp)); 1550 } 1551 #endif 1552 1553 /* if this mailer speaks smtp, create a return pipe */ 1554 #if SMTP 1555 if (clever) 1556 { 1557 if (pipe(rpvect) < 0) 1558 { 1559 syserr("%s... openmailer(%s): pipe (from mailer)", 1560 shortenstring(e->e_to, MAXSHORTSTR), 1561 m->m_name); 1562 (void) close(mpvect[0]); 1563 (void) close(mpvect[1]); 1564 if (tTd(11, 1)) 1565 printf("openmailer: NULL\n"); 1566 rcode = EX_OSERR; 1567 goto give_up; 1568 } 1569 # if XDEBUG 1570 checkfdopen(rpvect[0], "rpvect[0]"); 1571 checkfdopen(rpvect[1], "rpvect[1]"); 1572 # endif 1573 } 1574 #endif 1575 1576 /* 1577 ** Actually fork the mailer process. 1578 ** DOFORK is clever about retrying. 1579 ** 1580 ** Dispose of SIGCHLD signal catchers that may be laying 1581 ** around so that endmail will get it. 1582 */ 1583 1584 if (e->e_xfp != NULL) 1585 (void) fflush(e->e_xfp); /* for debugging */ 1586 (void) fflush(stdout); 1587 (void) setsignal(SIGCHLD, SIG_DFL); 1588 DOFORK(FORK); 1589 /* pid is set by DOFORK */ 1590 if (pid < 0) 1591 { 1592 /* failure */ 1593 syserr("%s... openmailer(%s): cannot fork", 1594 shortenstring(e->e_to, MAXSHORTSTR), m->m_name); 1595 (void) close(mpvect[0]); 1596 (void) close(mpvect[1]); 1597 #if SMTP 1598 if (clever) 1599 { 1600 (void) close(rpvect[0]); 1601 (void) close(rpvect[1]); 1602 } 1603 #endif 1604 if (tTd(11, 1)) 1605 printf("openmailer: NULL\n"); 1606 rcode = EX_OSERR; 1607 goto give_up; 1608 } 1609 else if (pid == 0) 1610 { 1611 int i; 1612 int saveerrno; 1613 int new_euid = NO_UID; 1614 int new_ruid = NO_UID; 1615 int new_gid = NO_GID; 1616 struct stat stb; 1617 extern int DtableSize; 1618 1619 if (e->e_lockfp != NULL) 1620 (void) close(fileno(e->e_lockfp)); 1621 1622 /* child -- set up input & exec mailer */ 1623 (void) setsignal(SIGINT, SIG_IGN); 1624 (void) setsignal(SIGHUP, SIG_IGN); 1625 (void) setsignal(SIGTERM, SIG_DFL); 1626 1627 if (m != FileMailer || stat(tochain->q_user, &stb) < 0) 1628 stb.st_mode = 0; 1629 1630 #if HASSETUSERCONTEXT 1631 /* 1632 ** Set user resources. 1633 */ 1634 1635 if (contextaddr != NULL) 1636 { 1637 struct passwd *pwd; 1638 1639 if (contextaddr->q_ruser != NULL) 1640 pwd = sm_getpwnam(contextaddr->q_ruser); 1641 else 1642 pwd = sm_getpwnam(contextaddr->q_user); 1643 if (pwd != NULL) 1644 (void) setusercontext(NULL, 1645 pwd, pwd->pw_uid, 1646 LOGIN_SETRESOURCES|LOGIN_SETPRIORITY); 1647 } 1648 #endif 1649 1650 /* tweak niceness */ 1651 if (m->m_nice != 0) 1652 nice(m->m_nice); 1653 1654 /* reset group id */ 1655 if (bitnset(M_SPECIFIC_UID, m->m_flags)) 1656 new_gid = m->m_gid; 1657 else if (bitset(S_ISGID, stb.st_mode)) 1658 new_gid = stb.st_gid; 1659 else if (ctladdr != NULL && ctladdr->q_gid != 0) 1660 { 1661 if (!DontInitGroups) 1662 { 1663 char *u = ctladdr->q_ruser; 1664 1665 if (u == NULL) 1666 u = ctladdr->q_user; 1667 1668 if (initgroups(u, ctladdr->q_gid) == -1 && suidwarn) 1669 syserr("openmailer: initgroups(%s, %d) failed", 1670 u, ctladdr->q_gid); 1671 } 1672 else 1673 { 1674 GIDSET_T gidset[1]; 1675 1676 gidset[0] = ctladdr->q_gid; 1677 if (setgroups(1, gidset) == -1 && suidwarn) 1678 syserr("openmailer: setgroups() failed"); 1679 } 1680 new_gid = ctladdr->q_gid; 1681 } 1682 else 1683 { 1684 if (!DontInitGroups) 1685 { 1686 if (initgroups(DefUser, DefGid) == -1 && suidwarn) 1687 syserr("openmailer: initgroups(%s, %d) failed", 1688 DefUser, DefGid); 1689 } 1690 else 1691 { 1692 GIDSET_T gidset[1]; 1693 1694 gidset[0] = DefGid; 1695 if (setgroups(1, gidset) == -1 && suidwarn) 1696 syserr("openmailer: setgroups() failed"); 1697 } 1698 if (m->m_gid == 0) 1699 new_gid = DefGid; 1700 else 1701 new_gid = m->m_gid; 1702 } 1703 if (new_gid != NO_GID && setgid(new_gid) < 0 && suidwarn) 1704 syserr("openmailer: setgid(%ld) failed", 1705 (long) new_gid); 1706 1707 /* reset user id */ 1708 endpwent(); 1709 if (bitnset(M_SPECIFIC_UID, m->m_flags)) 1710 new_euid = m->m_uid; 1711 else if (bitset(S_ISUID, stb.st_mode)) 1712 new_ruid = stb.st_uid; 1713 else if (ctladdr != NULL && ctladdr->q_uid != 0) 1714 new_ruid = ctladdr->q_uid; 1715 else if (m->m_uid != 0) 1716 new_ruid = m->m_uid; 1717 else 1718 new_ruid = DefUid; 1719 if (new_euid != NO_UID) 1720 { 1721 vendor_set_uid(new_euid); 1722 #if USESETEUID 1723 if (seteuid(new_euid) < 0 && suidwarn) 1724 syserr("openmailer: seteuid(%ld) failed", 1725 (long) new_euid); 1726 #else 1727 # if HASSETREUID 1728 if (setreuid(new_ruid, new_euid) < 0 && suidwarn) 1729 syserr("openmailer: setreuid(%ld, %ld) failed", 1730 (long) new_ruid, (long) new_euid); 1731 # else 1732 if (new_euid != geteuid() && setuid(new_euid) < 0 && suidwarn) 1733 syserr("openmailer: setuid(%ld) failed", 1734 (long) new_euid); 1735 # endif 1736 #endif 1737 } 1738 else if (new_ruid != NO_UID) 1739 { 1740 vendor_set_uid(new_ruid); 1741 if (setuid(new_ruid) < 0 && suidwarn) 1742 syserr("openmailer: setuid(%ld) failed", 1743 (long) new_ruid); 1744 } 1745 1746 if (tTd(11, 2)) 1747 printf("openmailer: running as r/euid=%d/%d\n", 1748 (int) getuid(), (int) geteuid()); 1749 1750 /* move into some "safe" directory */ 1751 if (m->m_execdir != NULL) 1752 { 1753 char *q; 1754 char buf[MAXLINE + 1]; 1755 1756 for (p = m->m_execdir; p != NULL; p = q) 1757 { 1758 q = strchr(p, ':'); 1759 if (q != NULL) 1760 *q = '\0'; 1761 expand(p, buf, sizeof buf, e); 1762 if (q != NULL) 1763 *q++ = ':'; 1764 if (tTd(11, 20)) 1765 printf("openmailer: trydir %s\n", 1766 buf); 1767 if (buf[0] != '\0' && chdir(buf) >= 0) 1768 break; 1769 } 1770 } 1771 1772 /* arrange to filter std & diag output of command */ 1773 #if SMTP 1774 if (clever) 1775 { 1776 (void) close(rpvect[0]); 1777 if (dup2(rpvect[1], STDOUT_FILENO) < 0) 1778 { 1779 syserr("%s... openmailer(%s): cannot dup pipe %d for stdout", 1780 shortenstring(e->e_to, MAXSHORTSTR), 1781 m->m_name, rpvect[1]); 1782 _exit(EX_OSERR); 1783 } 1784 (void) close(rpvect[1]); 1785 } 1786 else 1787 { 1788 /* put mailer output in transcript */ 1789 if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0) 1790 { 1791 syserr("%s... openmailer(%s): cannot dup xscript %d for stdout", 1792 shortenstring(e->e_to, MAXSHORTSTR), 1793 m->m_name, fileno(e->e_xfp)); 1794 _exit(EX_OSERR); 1795 } 1796 } 1797 #endif 1798 if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) 1799 { 1800 syserr("%s... openmailer(%s): cannot dup stdout for stderr", 1801 shortenstring(e->e_to, MAXSHORTSTR), 1802 m->m_name); 1803 _exit(EX_OSERR); 1804 } 1805 1806 /* arrange to get standard input */ 1807 (void) close(mpvect[1]); 1808 if (dup2(mpvect[0], STDIN_FILENO) < 0) 1809 { 1810 syserr("%s... openmailer(%s): cannot dup pipe %d for stdin", 1811 shortenstring(e->e_to, MAXSHORTSTR), 1812 m->m_name, mpvect[0]); 1813 _exit(EX_OSERR); 1814 } 1815 (void) close(mpvect[0]); 1816 1817 /* arrange for all the files to be closed */ 1818 for (i = 3; i < DtableSize; i++) 1819 { 1820 register int j; 1821 1822 if ((j = fcntl(i, F_GETFD, 0)) != -1) 1823 (void) fcntl(i, F_SETFD, j | 1); 1824 } 1825 1826 /* run disconnected from terminal */ 1827 (void) setsid(); 1828 1829 /* try to execute the mailer */ 1830 execve(m->m_mailer, (ARGV_T) pv, (ARGV_T) UserEnviron); 1831 saveerrno = errno; 1832 syserr("Cannot exec %s", m->m_mailer); 1833 if (bitnset(M_LOCALMAILER, m->m_flags) || 1834 transienterror(saveerrno)) 1835 _exit(EX_OSERR); 1836 _exit(EX_UNAVAILABLE); 1837 } 1838 1839 /* 1840 ** Set up return value. 1841 */ 1842 1843 if (mci == NULL) 1844 { 1845 mci = (MCI *) xalloc(sizeof *mci); 1846 bzero((char *) mci, sizeof *mci); 1847 } 1848 mci->mci_mailer = m; 1849 if (clever) 1850 { 1851 mci->mci_state = MCIS_OPENING; 1852 mci_cache(mci); 1853 } 1854 else 1855 { 1856 mci->mci_state = MCIS_OPEN; 1857 } 1858 mci->mci_pid = pid; 1859 (void) close(mpvect[0]); 1860 mci->mci_out = fdopen(mpvect[1], "w"); 1861 if (mci->mci_out == NULL) 1862 { 1863 syserr("deliver: cannot create mailer output channel, fd=%d", 1864 mpvect[1]); 1865 (void) close(mpvect[1]); 1866 #if SMTP 1867 if (clever) 1868 { 1869 (void) close(rpvect[0]); 1870 (void) close(rpvect[1]); 1871 } 1872 #endif 1873 rcode = EX_OSERR; 1874 goto give_up; 1875 } 1876 #if SMTP 1877 if (clever) 1878 { 1879 (void) close(rpvect[1]); 1880 mci->mci_in = fdopen(rpvect[0], "r"); 1881 if (mci->mci_in == NULL) 1882 { 1883 syserr("deliver: cannot create mailer input channel, fd=%d", 1884 mpvect[1]); 1885 (void) close(rpvect[0]); 1886 fclose(mci->mci_out); 1887 mci->mci_out = NULL; 1888 rcode = EX_OSERR; 1889 goto give_up; 1890 } 1891 } 1892 else 1893 #endif 1894 { 1895 mci->mci_flags |= MCIF_TEMP; 1896 mci->mci_in = NULL; 1897 } 1898 } 1899 1900 /* 1901 ** If we are in SMTP opening state, send initial protocol. 1902 */ 1903 1904 if (bitnset(M_7BITS, m->m_flags) && 1905 (!clever || mci->mci_state == MCIS_OPENING)) 1906 mci->mci_flags |= MCIF_7BIT; 1907 #if SMTP 1908 if (clever && mci->mci_state != MCIS_CLOSED) 1909 { 1910 extern void smtpinit __P((MAILER *, MCI *, ENVELOPE *)); 1911 1912 smtpinit(m, mci, e); 1913 } 1914 #endif 1915 1916 do_transfer: 1917 /* clear out per-message flags from connection structure */ 1918 mci->mci_flags &= ~(MCIF_CVT7TO8|MCIF_CVT8TO7); 1919 1920 if (bitset(EF_HAS8BIT, e->e_flags) && 1921 !bitset(EF_DONT_MIME, e->e_flags) && 1922 bitnset(M_7BITS, m->m_flags)) 1923 mci->mci_flags |= MCIF_CVT8TO7; 1924 1925 #if MIME7TO8 1926 if (bitnset(M_MAKE8BIT, m->m_flags) && 1927 !bitset(MCIF_7BIT, mci->mci_flags) && 1928 (p = hvalue("Content-Transfer-Encoding", e->e_header)) != NULL && 1929 (strcasecmp(p, "quoted-printable") == 0 || 1930 strcasecmp(p, "base64") == 0) && 1931 (p = hvalue("Content-Type", e->e_header)) != NULL) 1932 { 1933 /* may want to convert 7 -> 8 */ 1934 /* XXX should really parse it here -- and use a class XXX */ 1935 if (strncasecmp(p, "text/plain", 10) == 0 && 1936 (p[10] == '\0' || p[10] == ' ' || p[10] == ';')) 1937 mci->mci_flags |= MCIF_CVT7TO8; 1938 } 1939 #endif 1940 1941 if (tTd(11, 1)) 1942 { 1943 printf("openmailer: "); 1944 mci_dump(mci, FALSE); 1945 } 1946 1947 if (mci->mci_state != MCIS_OPEN) 1948 { 1949 /* couldn't open the mailer */ 1950 rcode = mci->mci_exitstat; 1951 errno = mci->mci_errno; 1952 #if NAMED_BIND 1953 h_errno = mci->mci_herrno; 1954 #endif 1955 if (rcode == EX_OK) 1956 { 1957 /* shouldn't happen */ 1958 syserr("554 deliver: mci=%lx rcode=%d errno=%d state=%d sig=%s", 1959 (long) mci, rcode, errno, mci->mci_state, 1960 firstsig); 1961 mci_dump_all(TRUE); 1962 rcode = EX_SOFTWARE; 1963 } 1964 #if DAEMON 1965 else if (curhost != NULL && *curhost != '\0') 1966 { 1967 /* try next MX site */ 1968 goto tryhost; 1969 } 1970 #endif 1971 } 1972 else if (!clever) 1973 { 1974 /* 1975 ** Format and send message. 1976 */ 1977 1978 mci->mci_contentlen = 0; 1979 putfromline(mci, e); 1980 (*e->e_puthdr)(mci, e->e_header, e); 1981 (*e->e_putbody)(mci, e, NULL); 1982 1983 /* get the exit status */ 1984 rcode = endmailer(mci, e, pv); 1985 } 1986 else 1987 #if SMTP 1988 { 1989 extern int smtpmailfrom __P((MAILER *, MCI *, ENVELOPE *)); 1990 extern int smtprcpt __P((ADDRESS *, MAILER *, MCI *, ENVELOPE *)); 1991 extern int smtpdata __P((MAILER *, MCI *, ENVELOPE *)); 1992 1993 /* 1994 ** Send the MAIL FROM: protocol 1995 */ 1996 1997 rcode = smtpmailfrom(m, mci, e); 1998 if (rcode == EX_OK) 1999 { 2000 register char *t = tobuf; 2001 register int i; 2002 2003 /* send the recipient list */ 2004 tobuf[0] = '\0'; 2005 for (to = tochain; to != NULL; to = to->q_tchain) 2006 { 2007 e->e_to = to->q_paddr; 2008 if (strlen(to->q_paddr) + (t - tobuf) + 2 > sizeof tobuf) 2009 { 2010 /* not enough room */ 2011 continue; 2012 } 2013 else if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 2014 { 2015 markfailure(e, to, mci, i); 2016 giveresponse(i, m, mci, ctladdr, xstart, e); 2017 } 2018 else 2019 { 2020 *t++ = ','; 2021 for (p = to->q_paddr; *p; *t++ = *p++) 2022 continue; 2023 *t = '\0'; 2024 } 2025 } 2026 2027 /* now send the data */ 2028 if (tobuf[0] == '\0') 2029 { 2030 rcode = EX_OK; 2031 e->e_to = NULL; 2032 if (bitset(MCIF_CACHED, mci->mci_flags)) 2033 smtprset(m, mci, e); 2034 } 2035 else 2036 { 2037 e->e_to = tobuf + 1; 2038 rcode = smtpdata(m, mci, e); 2039 } 2040 } 2041 # if DAEMON 2042 if (rcode == EX_TEMPFAIL && curhost != NULL && *curhost != '\0') 2043 { 2044 /* try next MX site */ 2045 goto tryhost; 2046 } 2047 # endif 2048 } 2049 #else /* not SMTP */ 2050 { 2051 syserr("554 deliver: need SMTP compiled to use clever mailer"); 2052 rcode = EX_CONFIG; 2053 goto give_up; 2054 } 2055 #endif /* SMTP */ 2056 #if NAMED_BIND 2057 if (ConfigLevel < 2) 2058 _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 2059 #endif 2060 2061 if (tTd(62, 1)) 2062 checkfds("after delivery"); 2063 2064 /* 2065 ** Do final status disposal. 2066 ** We check for something in tobuf for the SMTP case. 2067 ** If we got a temporary failure, arrange to queue the 2068 ** addressees. 2069 */ 2070 2071 give_up: 2072 #if SMTP 2073 if (bitnset(M_LMTP, m->m_flags)) 2074 { 2075 lmtp_rcode = rcode; 2076 tobuf[0] = '\0'; 2077 anyok = FALSE; 2078 } 2079 else 2080 #endif 2081 anyok = rcode == EX_OK; 2082 2083 for (to = tochain; to != NULL; to = to->q_tchain) 2084 { 2085 /* see if address already marked */ 2086 if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 2087 continue; 2088 2089 #if SMTP 2090 /* if running LMTP, get the status for each address */ 2091 if (bitnset(M_LMTP, m->m_flags)) 2092 { 2093 extern int smtpgetstat __P((MAILER *, MCI *, ENVELOPE *)); 2094 2095 if (lmtp_rcode == EX_OK) 2096 rcode = smtpgetstat(m, mci, e); 2097 if (rcode == EX_OK) 2098 { 2099 if (strlen(to->q_paddr) + strlen(tobuf) + 2 >= sizeof tobuf) 2100 { 2101 syserr("LMTP tobuf overflow"); 2102 } 2103 else 2104 { 2105 strcat(tobuf, ","); 2106 strcat(tobuf, to->q_paddr); 2107 } 2108 anyok = TRUE; 2109 } 2110 else 2111 { 2112 e->e_to = to->q_paddr; 2113 markfailure(e, to, mci, rcode); 2114 giveresponse(rcode, m, mci, ctladdr, xstart, e); 2115 e->e_to = tobuf + 1; 2116 continue; 2117 } 2118 } 2119 else 2120 #endif 2121 { 2122 /* mark bad addresses */ 2123 if (rcode != EX_OK) 2124 { 2125 if (goodmxfound && rcode == EX_NOHOST) 2126 rcode = EX_TEMPFAIL; 2127 markfailure(e, to, mci, rcode); 2128 continue; 2129 } 2130 } 2131 2132 /* successful delivery */ 2133 to->q_flags |= QSENT; 2134 to->q_statdate = curtime(); 2135 e->e_nsent++; 2136 if (bitnset(M_LOCALMAILER, m->m_flags) && 2137 bitset(QPINGONSUCCESS, to->q_flags)) 2138 { 2139 to->q_flags |= QDELIVERED; 2140 to->q_status = "2.1.5"; 2141 fprintf(e->e_xfp, "%s... Successfully delivered\n", 2142 to->q_paddr); 2143 } 2144 else if (bitset(QPINGONSUCCESS, to->q_flags) && 2145 bitset(QPRIMARY, to->q_flags) && 2146 !bitset(MCIF_DSN, mci->mci_flags)) 2147 { 2148 to->q_flags |= QRELAYED; 2149 fprintf(e->e_xfp, "%s... relayed; expect no further notifications\n", 2150 to->q_paddr); 2151 } 2152 } 2153 2154 #if SMTP 2155 if (bitnset(M_LMTP, m->m_flags)) 2156 { 2157 /* 2158 ** Global information applies to the last recipient only; 2159 ** clear it out to avoid bogus errors. 2160 */ 2161 2162 rcode = EX_OK; 2163 e->e_statmsg = NULL; 2164 2165 /* reset the mci state for the next transaction */ 2166 if (mci != NULL && mci->mci_state == MCIS_ACTIVE) 2167 mci->mci_state = MCIS_OPEN; 2168 } 2169 #endif 2170 2171 if (tobuf[0] != '\0') 2172 giveresponse(rcode, m, mci, ctladdr, xstart, e); 2173 if (anyok) 2174 markstats(e, tochain, FALSE); 2175 mci_store_persistent(mci); 2176 2177 #if SMTP 2178 /* now close the connection */ 2179 if (clever && mci != NULL && mci->mci_state != MCIS_CLOSED && 2180 !bitset(MCIF_CACHED, mci->mci_flags)) 2181 smtpquit(m, mci, e); 2182 #endif 2183 2184 /* 2185 ** Restore state and return. 2186 */ 2187 2188 #if XDEBUG 2189 { 2190 char wbuf[MAXLINE]; 2191 2192 /* make absolutely certain 0, 1, and 2 are in use */ 2193 snprintf(wbuf, sizeof wbuf, "%s... end of deliver(%s)", 2194 e->e_to == NULL ? "NO-TO-LIST" 2195 : shortenstring(e->e_to, MAXSHORTSTR), 2196 m->m_name); 2197 checkfd012(wbuf); 2198 } 2199 #endif 2200 2201 errno = 0; 2202 define('g', (char *) NULL, e); 2203 return (rcode); 2204 } 2205 /* 2206 ** MARKFAILURE -- mark a failure on a specific address. 2207 ** 2208 ** Parameters: 2209 ** e -- the envelope we are sending. 2210 ** q -- the address to mark. 2211 ** mci -- mailer connection information. 2212 ** rcode -- the code signifying the particular failure. 2213 ** 2214 ** Returns: 2215 ** none. 2216 ** 2217 ** Side Effects: 2218 ** marks the address (and possibly the envelope) with the 2219 ** failure so that an error will be returned or 2220 ** the message will be queued, as appropriate. 2221 */ 2222 2223 void 2224 markfailure(e, q, mci, rcode) 2225 register ENVELOPE *e; 2226 register ADDRESS *q; 2227 register MCI *mci; 2228 int rcode; 2229 { 2230 char *stat = NULL; 2231 2232 switch (rcode) 2233 { 2234 case EX_OK: 2235 break; 2236 2237 case EX_TEMPFAIL: 2238 case EX_IOERR: 2239 case EX_OSERR: 2240 q->q_flags |= QQUEUEUP; 2241 q->q_flags &= ~QDONTSEND; 2242 break; 2243 2244 default: 2245 q->q_flags |= QBADADDR; 2246 break; 2247 } 2248 2249 /* find most specific error code possible */ 2250 if (mci != NULL && mci->mci_status != NULL) 2251 { 2252 q->q_status = mci->mci_status; 2253 if (mci->mci_rstatus != NULL) 2254 q->q_rstatus = newstr(mci->mci_rstatus); 2255 else 2256 q->q_rstatus = NULL; 2257 } 2258 else if (e->e_status != NULL) 2259 { 2260 q->q_status = e->e_status; 2261 q->q_rstatus = NULL; 2262 } 2263 else 2264 { 2265 switch (rcode) 2266 { 2267 case EX_USAGE: 2268 stat = "5.5.4"; 2269 break; 2270 2271 case EX_DATAERR: 2272 stat = "5.5.2"; 2273 break; 2274 2275 case EX_NOUSER: 2276 stat = "5.1.1"; 2277 break; 2278 2279 case EX_NOHOST: 2280 stat = "5.1.2"; 2281 break; 2282 2283 case EX_NOINPUT: 2284 case EX_CANTCREAT: 2285 case EX_NOPERM: 2286 stat = "5.3.0"; 2287 break; 2288 2289 case EX_UNAVAILABLE: 2290 case EX_SOFTWARE: 2291 case EX_OSFILE: 2292 case EX_PROTOCOL: 2293 case EX_CONFIG: 2294 stat = "5.5.0"; 2295 break; 2296 2297 case EX_OSERR: 2298 case EX_IOERR: 2299 stat = "4.5.0"; 2300 break; 2301 2302 case EX_TEMPFAIL: 2303 stat = "4.2.0"; 2304 break; 2305 } 2306 if (stat != NULL) 2307 q->q_status = stat; 2308 } 2309 2310 q->q_statdate = curtime(); 2311 if (CurHostName != NULL && CurHostName[0] != '\0') 2312 q->q_statmta = newstr(CurHostName); 2313 if (rcode != EX_OK && q->q_rstatus == NULL && 2314 q->q_mailer != NULL && q->q_mailer->m_diagtype != NULL && 2315 strcasecmp(q->q_mailer->m_diagtype, "UNIX") == 0) 2316 { 2317 char buf[30]; 2318 2319 (void) snprintf(buf, sizeof buf, "%d", rcode); 2320 q->q_rstatus = newstr(buf); 2321 } 2322 } 2323 /* 2324 ** ENDMAILER -- Wait for mailer to terminate. 2325 ** 2326 ** We should never get fatal errors (e.g., segmentation 2327 ** violation), so we report those specially. For other 2328 ** errors, we choose a status message (into statmsg), 2329 ** and if it represents an error, we print it. 2330 ** 2331 ** Parameters: 2332 ** pid -- pid of mailer. 2333 ** e -- the current envelope. 2334 ** pv -- the parameter vector that invoked the mailer 2335 ** (for error messages). 2336 ** 2337 ** Returns: 2338 ** exit code of mailer. 2339 ** 2340 ** Side Effects: 2341 ** none. 2342 */ 2343 2344 int 2345 endmailer(mci, e, pv) 2346 register MCI *mci; 2347 register ENVELOPE *e; 2348 char **pv; 2349 { 2350 int st; 2351 2352 mci_unlock_host(mci); 2353 2354 /* close any connections */ 2355 if (mci->mci_in != NULL) 2356 (void) xfclose(mci->mci_in, mci->mci_mailer->m_name, "mci_in"); 2357 if (mci->mci_out != NULL) 2358 (void) xfclose(mci->mci_out, mci->mci_mailer->m_name, "mci_out"); 2359 mci->mci_in = mci->mci_out = NULL; 2360 mci->mci_state = MCIS_CLOSED; 2361 2362 /* in the IPC case there is nothing to wait for */ 2363 if (mci->mci_pid == 0) 2364 return (EX_OK); 2365 2366 #if _FFR_TIMEOUT_WAIT 2367 put a timeout around the wait 2368 #endif 2369 2370 /* wait for the mailer process to die and collect status */ 2371 st = waitfor(mci->mci_pid); 2372 if (st == -1) 2373 { 2374 syserr("endmailer %s: wait", mci->mci_mailer->m_name); 2375 return (EX_SOFTWARE); 2376 } 2377 2378 if (WIFEXITED(st)) 2379 { 2380 /* normal death -- return status */ 2381 return (WEXITSTATUS(st)); 2382 } 2383 2384 /* it died a horrid death */ 2385 syserr("451 mailer %s died with signal %o", 2386 mci->mci_mailer->m_name, st); 2387 2388 /* log the arguments */ 2389 if (pv != NULL && e->e_xfp != NULL) 2390 { 2391 register char **av; 2392 2393 fprintf(e->e_xfp, "Arguments:"); 2394 for (av = pv; *av != NULL; av++) 2395 fprintf(e->e_xfp, " %s", *av); 2396 fprintf(e->e_xfp, "\n"); 2397 } 2398 2399 ExitStat = EX_TEMPFAIL; 2400 return (EX_TEMPFAIL); 2401 } 2402 /* 2403 ** GIVERESPONSE -- Interpret an error response from a mailer 2404 ** 2405 ** Parameters: 2406 ** stat -- the status code from the mailer (high byte 2407 ** only; core dumps must have been taken care of 2408 ** already). 2409 ** m -- the mailer info for this mailer. 2410 ** mci -- the mailer connection info -- can be NULL if the 2411 ** response is given before the connection is made. 2412 ** ctladdr -- the controlling address for the recipient 2413 ** address(es). 2414 ** xstart -- the transaction start time, for computing 2415 ** transaction delays. 2416 ** e -- the current envelope. 2417 ** 2418 ** Returns: 2419 ** none. 2420 ** 2421 ** Side Effects: 2422 ** Errors may be incremented. 2423 ** ExitStat may be set. 2424 */ 2425 2426 void 2427 giveresponse(stat, m, mci, ctladdr, xstart, e) 2428 int stat; 2429 register MAILER *m; 2430 register MCI *mci; 2431 ADDRESS *ctladdr; 2432 time_t xstart; 2433 ENVELOPE *e; 2434 { 2435 register const char *statmsg; 2436 extern char *SysExMsg[]; 2437 register int i; 2438 extern int N_SysEx; 2439 char buf[MAXLINE]; 2440 2441 if (e == NULL) 2442 syserr("giveresponse: null envelope"); 2443 2444 /* 2445 ** Compute status message from code. 2446 */ 2447 2448 i = stat - EX__BASE; 2449 if (stat == 0) 2450 { 2451 statmsg = "250 Sent"; 2452 if (e->e_statmsg != NULL) 2453 { 2454 (void) snprintf(buf, sizeof buf, "%s (%s)", 2455 statmsg, shortenstring(e->e_statmsg, 403)); 2456 statmsg = buf; 2457 } 2458 } 2459 else if (i < 0 || i >= N_SysEx) 2460 { 2461 (void) snprintf(buf, sizeof buf, "554 unknown mailer error %d", 2462 stat); 2463 stat = EX_UNAVAILABLE; 2464 statmsg = buf; 2465 } 2466 else if (stat == EX_TEMPFAIL) 2467 { 2468 char *bp = buf; 2469 2470 snprintf(bp, SPACELEFT(buf, bp), "%s", SysExMsg[i] + 1); 2471 bp += strlen(bp); 2472 #if NAMED_BIND 2473 if (h_errno == TRY_AGAIN) 2474 statmsg = errstring(h_errno+E_DNSBASE); 2475 else 2476 #endif 2477 { 2478 if (errno != 0) 2479 statmsg = errstring(errno); 2480 else 2481 { 2482 #if SMTP 2483 statmsg = SmtpError; 2484 #else /* SMTP */ 2485 statmsg = NULL; 2486 #endif /* SMTP */ 2487 } 2488 } 2489 if (statmsg != NULL && statmsg[0] != '\0') 2490 snprintf(bp, SPACELEFT(buf, bp), ": %s", statmsg); 2491 statmsg = buf; 2492 } 2493 #if NAMED_BIND 2494 else if (stat == EX_NOHOST && h_errno != 0) 2495 { 2496 statmsg = errstring(h_errno + E_DNSBASE); 2497 (void) snprintf(buf, sizeof buf, "%s (%s)", 2498 SysExMsg[i] + 1, statmsg); 2499 statmsg = buf; 2500 } 2501 #endif 2502 else 2503 { 2504 statmsg = SysExMsg[i]; 2505 if (*statmsg++ == ':' && errno != 0) 2506 { 2507 (void) snprintf(buf, sizeof buf, "%s: %s", 2508 statmsg, errstring(errno)); 2509 statmsg = buf; 2510 } 2511 } 2512 2513 /* 2514 ** Print the message as appropriate 2515 */ 2516 2517 if (stat == EX_OK || stat == EX_TEMPFAIL) 2518 { 2519 extern char MsgBuf[]; 2520 2521 message("%s", &statmsg[4]); 2522 if (stat == EX_TEMPFAIL && e->e_xfp != NULL) 2523 fprintf(e->e_xfp, "%s\n", &MsgBuf[4]); 2524 } 2525 else 2526 { 2527 char mbuf[8]; 2528 2529 Errors++; 2530 snprintf(mbuf, sizeof mbuf, "%.3s %%s", statmsg); 2531 usrerr(mbuf, &statmsg[4]); 2532 } 2533 2534 /* 2535 ** Final cleanup. 2536 ** Log a record of the transaction. Compute the new 2537 ** ExitStat -- if we already had an error, stick with 2538 ** that. 2539 */ 2540 2541 if (OpMode != MD_VERIFY && !bitset(EF_VRFYONLY, e->e_flags) && 2542 LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 2543 logdelivery(m, mci, &statmsg[4], ctladdr, xstart, e); 2544 2545 if (tTd(11, 2)) 2546 printf("giveresponse: stat=%d, e->e_message=%s\n", 2547 stat, e->e_message == NULL ? "<NULL>" : e->e_message); 2548 2549 if (stat != EX_TEMPFAIL) 2550 setstat(stat); 2551 if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL)) 2552 { 2553 if (e->e_message != NULL) 2554 free(e->e_message); 2555 e->e_message = newstr(&statmsg[4]); 2556 } 2557 errno = 0; 2558 #if NAMED_BIND 2559 h_errno = 0; 2560 #endif 2561 } 2562 /* 2563 ** LOGDELIVERY -- log the delivery in the system log 2564 ** 2565 ** Care is taken to avoid logging lines that are too long, because 2566 ** some versions of syslog have an unfortunate proclivity for core 2567 ** dumping. This is a hack, to be sure, that is at best empirical. 2568 ** 2569 ** Parameters: 2570 ** m -- the mailer info. Can be NULL for initial queue. 2571 ** mci -- the mailer connection info -- can be NULL if the 2572 ** log is occuring when no connection is active. 2573 ** stat -- the message to print for the status. 2574 ** ctladdr -- the controlling address for the to list. 2575 ** xstart -- the transaction start time, used for 2576 ** computing transaction delay. 2577 ** e -- the current envelope. 2578 ** 2579 ** Returns: 2580 ** none 2581 ** 2582 ** Side Effects: 2583 ** none 2584 */ 2585 2586 void 2587 logdelivery(m, mci, stat, ctladdr, xstart, e) 2588 MAILER *m; 2589 register MCI *mci; 2590 const char *stat; 2591 ADDRESS *ctladdr; 2592 time_t xstart; 2593 register ENVELOPE *e; 2594 { 2595 register char *bp; 2596 register char *p; 2597 int l; 2598 char buf[1024]; 2599 2600 # if (SYSLOG_BUFSIZE) >= 256 2601 /* ctladdr: max 106 bytes */ 2602 bp = buf; 2603 if (ctladdr != NULL) 2604 { 2605 snprintf(bp, SPACELEFT(buf, bp), ", ctladdr=%s", 2606 shortenstring(ctladdr->q_paddr, 83)); 2607 bp += strlen(bp); 2608 if (bitset(QGOODUID, ctladdr->q_flags)) 2609 { 2610 (void) snprintf(bp, SPACELEFT(buf, bp), " (%d/%d)", 2611 ctladdr->q_uid, ctladdr->q_gid); 2612 bp += strlen(bp); 2613 } 2614 } 2615 2616 /* delay & xdelay: max 41 bytes */ 2617 snprintf(bp, SPACELEFT(buf, bp), ", delay=%s", 2618 pintvl(curtime() - e->e_ctime, TRUE)); 2619 bp += strlen(bp); 2620 2621 if (xstart != (time_t) 0) 2622 { 2623 snprintf(bp, SPACELEFT(buf, bp), ", xdelay=%s", 2624 pintvl(curtime() - xstart, TRUE)); 2625 bp += strlen(bp); 2626 } 2627 2628 /* mailer: assume about 19 bytes (max 10 byte mailer name) */ 2629 if (m != NULL) 2630 { 2631 snprintf(bp, SPACELEFT(buf, bp), ", mailer=%s", m->m_name); 2632 bp += strlen(bp); 2633 } 2634 2635 /* relay: max 66 bytes for IPv4 addresses */ 2636 if (mci != NULL && mci->mci_host != NULL) 2637 { 2638 # if DAEMON 2639 extern SOCKADDR CurHostAddr; 2640 # endif 2641 2642 snprintf(bp, SPACELEFT(buf, bp), ", relay=%s", 2643 shortenstring(mci->mci_host, 40)); 2644 bp += strlen(bp); 2645 2646 # if DAEMON 2647 if (CurHostAddr.sa.sa_family != 0) 2648 { 2649 snprintf(bp, SPACELEFT(buf, bp), " [%s]", 2650 anynet_ntoa(&CurHostAddr)); 2651 } 2652 # endif 2653 } 2654 else if (strcmp(stat, "queued") != 0) 2655 { 2656 p = macvalue('h', e); 2657 if (p != NULL && p[0] != '\0') 2658 { 2659 snprintf(bp, SPACELEFT(buf, bp), ", relay=%s", 2660 shortenstring(p, 40)); 2661 } 2662 } 2663 bp += strlen(bp); 2664 2665 #define STATLEN (((SYSLOG_BUFSIZE) - 100) / 4) 2666 #if (STATLEN) < 63 2667 # undef STATLEN 2668 # define STATLEN 63 2669 #endif 2670 #if (STATLEN) > 203 2671 # undef STATLEN 2672 # define STATLEN 203 2673 #endif 2674 2675 /* stat: max 210 bytes */ 2676 if ((bp - buf) > (sizeof buf - ((STATLEN) + 20))) 2677 { 2678 /* desperation move -- truncate data */ 2679 bp = buf + sizeof buf - ((STATLEN) + 17); 2680 strcpy(bp, "..."); 2681 bp += 3; 2682 } 2683 2684 (void) strcpy(bp, ", stat="); 2685 bp += strlen(bp); 2686 2687 (void) strcpy(bp, shortenstring(stat, (STATLEN))); 2688 2689 /* id, to: max 13 + TOBUFSIZE bytes */ 2690 l = SYSLOG_BUFSIZE - 100 - strlen(buf); 2691 p = e->e_to; 2692 while (strlen(p) >= (SIZE_T) l) 2693 { 2694 register char *q = strchr(p + l, ','); 2695 2696 if (q == NULL) 2697 break; 2698 sm_syslog(LOG_INFO, e->e_id, 2699 "to=%.*s [more]%s", 2700 ++q - p, p, buf); 2701 p = q; 2702 } 2703 sm_syslog(LOG_INFO, e->e_id, "to=%s%s", p, buf); 2704 2705 # else /* we have a very short log buffer size */ 2706 2707 l = SYSLOG_BUFSIZE - 85; 2708 p = e->e_to; 2709 while (strlen(p) >= (SIZE_T) l) 2710 { 2711 register char *q = strchr(p + l, ','); 2712 2713 if (q == NULL) 2714 break; 2715 sm_syslog(LOG_INFO, e->e_id, 2716 "to=%.*s [more]", 2717 ++q - p, p); 2718 p = q; 2719 } 2720 sm_syslog(LOG_INFO, e->e_id, "to=%s", p); 2721 2722 if (ctladdr != NULL) 2723 { 2724 bp = buf; 2725 snprintf(bp, SPACELEFT(buf, bp), "ctladdr=%s", 2726 shortenstring(ctladdr->q_paddr, 83)); 2727 bp += strlen(bp); 2728 if (bitset(QGOODUID, ctladdr->q_flags)) 2729 { 2730 (void) snprintf(bp, SPACELEFT(buf, bp), " (%d/%d)", 2731 ctladdr->q_uid, ctladdr->q_gid); 2732 bp += strlen(bp); 2733 } 2734 sm_syslog(LOG_INFO, e->e_id, "%s", buf); 2735 } 2736 bp = buf; 2737 snprintf(bp, SPACELEFT(buf, bp), "delay=%s", 2738 pintvl(curtime() - e->e_ctime, TRUE)); 2739 bp += strlen(bp); 2740 if (xstart != (time_t) 0) 2741 { 2742 snprintf(bp, SPACELEFT(buf, bp), ", xdelay=%s", 2743 pintvl(curtime() - xstart, TRUE)); 2744 bp += strlen(bp); 2745 } 2746 2747 if (m != NULL) 2748 { 2749 snprintf(bp, SPACELEFT(buf, bp), ", mailer=%s", m->m_name); 2750 bp += strlen(bp); 2751 } 2752 sm_syslog(LOG_INFO, e->e_id, "%.1000s", buf); 2753 2754 buf[0] = '\0'; 2755 bp = buf; 2756 if (mci != NULL && mci->mci_host != NULL) 2757 { 2758 # if DAEMON 2759 extern SOCKADDR CurHostAddr; 2760 # endif 2761 2762 snprintf(bp, SPACELEFT(buf, bp), "relay=%.100s", mci->mci_host); 2763 bp += strlen(bp); 2764 2765 # if DAEMON 2766 if (CurHostAddr.sa.sa_family != 0) 2767 snprintf(bp, SPACELEFT(buf, bp), " [%.100s]", 2768 anynet_ntoa(&CurHostAddr)); 2769 # endif 2770 } 2771 else if (strcmp(stat, "queued") != 0) 2772 { 2773 p = macvalue('h', e); 2774 if (p != NULL && p[0] != '\0') 2775 snprintf(buf, sizeof buf, "relay=%.100s", p); 2776 } 2777 if (buf[0] != '\0') 2778 sm_syslog(LOG_INFO, e->e_id, "%.1000s", buf); 2779 2780 sm_syslog(LOG_INFO, e->e_id, "stat=%s", shortenstring(stat, 63)); 2781 # endif /* short log buffer */ 2782 } 2783 /* 2784 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 2785 ** 2786 ** This can be made an arbitrary message separator by changing $l 2787 ** 2788 ** One of the ugliest hacks seen by human eyes is contained herein: 2789 ** UUCP wants those stupid "remote from <host>" lines. Why oh why 2790 ** does a well-meaning programmer such as myself have to deal with 2791 ** this kind of antique garbage???? 2792 ** 2793 ** Parameters: 2794 ** mci -- the connection information. 2795 ** e -- the envelope. 2796 ** 2797 ** Returns: 2798 ** none 2799 ** 2800 ** Side Effects: 2801 ** outputs some text to fp. 2802 */ 2803 2804 void 2805 putfromline(mci, e) 2806 register MCI *mci; 2807 ENVELOPE *e; 2808 { 2809 char *template = UnixFromLine; 2810 char buf[MAXLINE]; 2811 char xbuf[MAXLINE]; 2812 2813 if (bitnset(M_NHDR, mci->mci_mailer->m_flags)) 2814 return; 2815 2816 mci->mci_flags |= MCIF_INHEADER; 2817 2818 if (bitnset(M_UGLYUUCP, mci->mci_mailer->m_flags)) 2819 { 2820 char *bang; 2821 2822 expand("\201g", buf, sizeof buf, e); 2823 bang = strchr(buf, '!'); 2824 if (bang == NULL) 2825 { 2826 char *at; 2827 char hname[MAXNAME]; 2828 2829 /* 2830 ** If we can construct a UUCP path, do so 2831 */ 2832 2833 at = strrchr(buf, '@'); 2834 if (at == NULL) 2835 { 2836 expand( "\201k", hname, sizeof hname, e); 2837 at = hname; 2838 } 2839 else 2840 *at++ = '\0'; 2841 (void) snprintf(xbuf, sizeof xbuf, 2842 "From %.800s \201d remote from %.100s\n", 2843 buf, at); 2844 } 2845 else 2846 { 2847 *bang++ = '\0'; 2848 (void) snprintf(xbuf, sizeof xbuf, 2849 "From %.800s \201d remote from %.100s\n", 2850 bang, buf); 2851 template = xbuf; 2852 } 2853 } 2854 expand(template, buf, sizeof buf, e); 2855 putxline(buf, strlen(buf), mci, PXLF_HEADER); 2856 } 2857 /* 2858 ** PUTBODY -- put the body of a message. 2859 ** 2860 ** Parameters: 2861 ** mci -- the connection information. 2862 ** e -- the envelope to put out. 2863 ** separator -- if non-NULL, a message separator that must 2864 ** not be permitted in the resulting message. 2865 ** 2866 ** Returns: 2867 ** none. 2868 ** 2869 ** Side Effects: 2870 ** The message is written onto fp. 2871 */ 2872 2873 /* values for output state variable */ 2874 #define OS_HEAD 0 /* at beginning of line */ 2875 #define OS_CR 1 /* read a carriage return */ 2876 #define OS_INLINE 2 /* putting rest of line */ 2877 2878 void 2879 putbody(mci, e, separator) 2880 register MCI *mci; 2881 register ENVELOPE *e; 2882 char *separator; 2883 { 2884 char buf[MAXLINE]; 2885 2886 /* 2887 ** Output the body of the message 2888 */ 2889 2890 if (e->e_dfp == NULL && bitset(EF_HAS_DF, e->e_flags)) 2891 { 2892 char *df = queuename(e, 'd'); 2893 2894 e->e_dfp = fopen(df, "r"); 2895 if (e->e_dfp == NULL) 2896 syserr("putbody: Cannot open %s for %s from %s", 2897 df, e->e_to, e->e_from.q_paddr); 2898 } 2899 if (e->e_dfp == NULL) 2900 { 2901 if (bitset(MCIF_INHEADER, mci->mci_flags)) 2902 { 2903 putline("", mci); 2904 mci->mci_flags &= ~MCIF_INHEADER; 2905 } 2906 putline("<<< No Message Collected >>>", mci); 2907 goto endofmessage; 2908 } 2909 if (e->e_dfino == (ino_t) 0) 2910 { 2911 struct stat stbuf; 2912 2913 if (fstat(fileno(e->e_dfp), &stbuf) < 0) 2914 e->e_dfino = -1; 2915 else 2916 { 2917 e->e_dfdev = stbuf.st_dev; 2918 e->e_dfino = stbuf.st_ino; 2919 } 2920 } 2921 rewind(e->e_dfp); 2922 2923 #if MIME8TO7 2924 if (bitset(MCIF_CVT8TO7, mci->mci_flags)) 2925 { 2926 char *boundaries[MAXMIMENESTING + 1]; 2927 2928 /* 2929 ** Do 8 to 7 bit MIME conversion. 2930 */ 2931 2932 /* make sure it looks like a MIME message */ 2933 if (hvalue("MIME-Version", e->e_header) == NULL) 2934 putline("MIME-Version: 1.0", mci); 2935 2936 if (hvalue("Content-Type", e->e_header) == NULL) 2937 { 2938 snprintf(buf, sizeof buf, 2939 "Content-Type: text/plain; charset=%s", 2940 defcharset(e)); 2941 putline(buf, mci); 2942 } 2943 2944 /* now do the hard work */ 2945 boundaries[0] = NULL; 2946 mci->mci_flags |= MCIF_INHEADER; 2947 mime8to7(mci, e->e_header, e, boundaries, M87F_OUTER); 2948 } 2949 # if MIME7TO8 2950 else if (bitset(MCIF_CVT7TO8, mci->mci_flags)) 2951 { 2952 mime7to8(mci, e->e_header, e); 2953 } 2954 # endif 2955 else 2956 #endif 2957 { 2958 int ostate; 2959 register char *bp; 2960 register char *pbp; 2961 register int c; 2962 register char *xp; 2963 int padc; 2964 char *buflim; 2965 int pos = 0; 2966 size_t eol_len; 2967 char peekbuf[10]; 2968 2969 /* we can pass it through unmodified */ 2970 if (bitset(MCIF_INHEADER, mci->mci_flags)) 2971 { 2972 putline("", mci); 2973 mci->mci_flags &= ~MCIF_INHEADER; 2974 } 2975 2976 /* determine end of buffer; allow for short mailer lines */ 2977 buflim = &buf[sizeof buf - 1]; 2978 if (mci->mci_mailer->m_linelimit > 0 && 2979 mci->mci_mailer->m_linelimit < sizeof buf - 1) 2980 buflim = &buf[mci->mci_mailer->m_linelimit - 1]; 2981 eol_len = strlen(mci->mci_mailer->m_eol); 2982 2983 /* copy temp file to output with mapping */ 2984 ostate = OS_HEAD; 2985 bp = buf; 2986 pbp = peekbuf; 2987 while (!ferror(mci->mci_out)) 2988 { 2989 if (pbp > peekbuf) 2990 c = *--pbp; 2991 else if ((c = getc(e->e_dfp)) == EOF) 2992 break; 2993 if (bitset(MCIF_7BIT, mci->mci_flags)) 2994 c &= 0x7f; 2995 switch (ostate) 2996 { 2997 case OS_HEAD: 2998 #if _FFR_NONULLS 2999 if (c == '\0' && 3000 bitnset(M_NONULLS, mci->mci_mailer->m_flags)) 3001 break; 3002 #endif 3003 if (c != '\r' && c != '\n' && bp < buflim) 3004 { 3005 *bp++ = c; 3006 break; 3007 } 3008 3009 /* check beginning of line for special cases */ 3010 *bp = '\0'; 3011 pos = 0; 3012 padc = EOF; 3013 if (buf[0] == 'F' && 3014 bitnset(M_ESCFROM, mci->mci_mailer->m_flags) && 3015 strncmp(buf, "From ", 5) == 0) 3016 { 3017 padc = '>'; 3018 } 3019 if (buf[0] == '-' && buf[1] == '-' && 3020 separator != NULL) 3021 { 3022 /* possible separator */ 3023 int sl = strlen(separator); 3024 3025 if (strncmp(&buf[2], separator, sl) == 0) 3026 padc = ' '; 3027 } 3028 if (buf[0] == '.' && 3029 bitnset(M_XDOT, mci->mci_mailer->m_flags)) 3030 { 3031 padc = '.'; 3032 } 3033 3034 /* now copy out saved line */ 3035 if (TrafficLogFile != NULL) 3036 { 3037 fprintf(TrafficLogFile, "%05d >>> ", 3038 (int) getpid()); 3039 if (padc != EOF) 3040 putc(padc, TrafficLogFile); 3041 for (xp = buf; xp < bp; xp++) 3042 putc(*xp, TrafficLogFile); 3043 if (c == '\n') 3044 fputs(mci->mci_mailer->m_eol, 3045 TrafficLogFile); 3046 } 3047 if (padc != EOF) 3048 { 3049 putc(padc, mci->mci_out); 3050 mci->mci_contentlen++; 3051 pos++; 3052 } 3053 for (xp = buf; xp < bp; xp++) 3054 { 3055 putc(*xp, mci->mci_out); 3056 mci->mci_contentlen++; 3057 } 3058 if (c == '\n') 3059 { 3060 fputs(mci->mci_mailer->m_eol, 3061 mci->mci_out); 3062 mci->mci_contentlen += eol_len; 3063 pos = 0; 3064 } 3065 else 3066 { 3067 pos += bp - buf; 3068 if (c != '\r') 3069 *pbp++ = c; 3070 } 3071 bp = buf; 3072 3073 /* determine next state */ 3074 if (c == '\n') 3075 ostate = OS_HEAD; 3076 else if (c == '\r') 3077 ostate = OS_CR; 3078 else 3079 ostate = OS_INLINE; 3080 continue; 3081 3082 case OS_CR: 3083 if (c == '\n') 3084 { 3085 /* got CRLF */ 3086 fputs(mci->mci_mailer->m_eol, mci->mci_out); 3087 mci->mci_contentlen += eol_len; 3088 if (TrafficLogFile != NULL) 3089 { 3090 fputs(mci->mci_mailer->m_eol, 3091 TrafficLogFile); 3092 } 3093 ostate = OS_HEAD; 3094 continue; 3095 } 3096 3097 /* had a naked carriage return */ 3098 *pbp++ = c; 3099 c = '\r'; 3100 ostate = OS_INLINE; 3101 goto putch; 3102 3103 case OS_INLINE: 3104 if (c == '\r') 3105 { 3106 ostate = OS_CR; 3107 continue; 3108 } 3109 #if _FFR_NONULLS 3110 if (c == '\0' && 3111 bitnset(M_NONULLS, mci->mci_mailer->m_flags)) 3112 break; 3113 #endif 3114 putch: 3115 if (mci->mci_mailer->m_linelimit > 0 && 3116 pos > mci->mci_mailer->m_linelimit && 3117 c != '\n') 3118 { 3119 putc('!', mci->mci_out); 3120 mci->mci_contentlen++; 3121 fputs(mci->mci_mailer->m_eol, mci->mci_out); 3122 mci->mci_contentlen += eol_len; 3123 if (TrafficLogFile != NULL) 3124 { 3125 fprintf(TrafficLogFile, "!%s", 3126 mci->mci_mailer->m_eol); 3127 } 3128 ostate = OS_HEAD; 3129 *pbp++ = c; 3130 continue; 3131 } 3132 if (c == '\n') 3133 { 3134 if (TrafficLogFile != NULL) 3135 fputs(mci->mci_mailer->m_eol, 3136 TrafficLogFile); 3137 fputs(mci->mci_mailer->m_eol, mci->mci_out); 3138 mci->mci_contentlen += eol_len; 3139 pos = 0; 3140 ostate = OS_HEAD; 3141 } 3142 else 3143 { 3144 if (TrafficLogFile != NULL) 3145 putc(c, TrafficLogFile); 3146 putc(c, mci->mci_out); 3147 mci->mci_contentlen++; 3148 pos++; 3149 ostate = OS_INLINE; 3150 } 3151 break; 3152 } 3153 } 3154 3155 /* make sure we are at the beginning of a line */ 3156 if (bp > buf) 3157 { 3158 if (TrafficLogFile != NULL) 3159 { 3160 for (xp = buf; xp < bp; xp++) 3161 putc(*xp, TrafficLogFile); 3162 } 3163 for (xp = buf; xp < bp; xp++) 3164 { 3165 putc(*xp, mci->mci_out); 3166 mci->mci_contentlen++; 3167 } 3168 pos += bp - buf; 3169 } 3170 if (pos > 0) 3171 { 3172 if (TrafficLogFile != NULL) 3173 fputs(mci->mci_mailer->m_eol, TrafficLogFile); 3174 fputs(mci->mci_mailer->m_eol, mci->mci_out); 3175 mci->mci_contentlen += eol_len; 3176 } 3177 } 3178 3179 if (ferror(e->e_dfp)) 3180 { 3181 syserr("putbody: df%s: read error", e->e_id); 3182 ExitStat = EX_IOERR; 3183 } 3184 3185 endofmessage: 3186 /* some mailers want extra blank line at end of message */ 3187 if (bitnset(M_BLANKEND, mci->mci_mailer->m_flags) && 3188 buf[0] != '\0' && buf[0] != '\n') 3189 putline("", mci); 3190 3191 (void) fflush(mci->mci_out); 3192 if (ferror(mci->mci_out) && errno != EPIPE) 3193 { 3194 syserr("putbody: write error"); 3195 ExitStat = EX_IOERR; 3196 } 3197 errno = 0; 3198 } 3199 /* 3200 ** MAILFILE -- Send a message to a file. 3201 ** 3202 ** If the file has the setuid/setgid bits set, but NO execute 3203 ** bits, sendmail will try to become the owner of that file 3204 ** rather than the real user. Obviously, this only works if 3205 ** sendmail runs as root. 3206 ** 3207 ** This could be done as a subordinate mailer, except that it 3208 ** is used implicitly to save messages in ~/dead.letter. We 3209 ** view this as being sufficiently important as to include it 3210 ** here. For example, if the system is dying, we shouldn't have 3211 ** to create another process plus some pipes to save the message. 3212 ** 3213 ** Parameters: 3214 ** filename -- the name of the file to send to. 3215 ** mailer -- mailer definition for recipient -- if NULL, 3216 ** use FileMailer. 3217 ** ctladdr -- the controlling address header -- includes 3218 ** the userid/groupid to be when sending. 3219 ** sfflags -- flags for opening. 3220 ** e -- the current envelope. 3221 ** 3222 ** Returns: 3223 ** The exit code associated with the operation. 3224 ** 3225 ** Side Effects: 3226 ** none. 3227 */ 3228 3229 static jmp_buf CtxMailfileTimeout; 3230 static void mailfiletimeout __P((void)); 3231 3232 int 3233 mailfile(filename, mailer, ctladdr, sfflags, e) 3234 char *volatile filename; 3235 MAILER *volatile mailer; 3236 ADDRESS *ctladdr; 3237 volatile int sfflags; 3238 register ENVELOPE *e; 3239 { 3240 register FILE *f; 3241 register pid_t pid = -1; 3242 volatile int mode = ST_MODE_NOFILE; 3243 bool suidwarn = geteuid() == 0; 3244 char *p; 3245 EVENT *ev; 3246 3247 if (tTd(11, 1)) 3248 { 3249 printf("mailfile %s\n ctladdr=", filename); 3250 printaddr(ctladdr, FALSE); 3251 } 3252 3253 if (mailer == NULL) 3254 mailer = FileMailer; 3255 3256 if (e->e_xfp != NULL) 3257 fflush(e->e_xfp); 3258 3259 /* 3260 ** Special case /dev/null. This allows us to restrict file 3261 ** delivery to regular files only. 3262 */ 3263 3264 if (strcmp(filename, "/dev/null") == 0) 3265 return EX_OK; 3266 3267 /* check for 8-bit available */ 3268 if (bitset(EF_HAS8BIT, e->e_flags) && 3269 bitnset(M_7BITS, mailer->m_flags) && 3270 (bitset(EF_DONT_MIME, e->e_flags) || 3271 !(bitset(MM_MIME8BIT, MimeMode) || 3272 (bitset(EF_IS_MIME, e->e_flags) && 3273 bitset(MM_CVTMIME, MimeMode))))) 3274 { 3275 usrerr("554 Cannot send 8-bit data to 7-bit destination"); 3276 e->e_status = "5.6.3"; 3277 return(EX_DATAERR); 3278 } 3279 3280 /* 3281 ** Fork so we can change permissions here. 3282 ** Note that we MUST use fork, not vfork, because of 3283 ** the complications of calling subroutines, etc. 3284 */ 3285 3286 DOFORK(fork); 3287 3288 if (pid < 0) 3289 return (EX_OSERR); 3290 else if (pid == 0) 3291 { 3292 /* child -- actually write to file */ 3293 struct stat stb; 3294 MCI mcibuf; 3295 volatile int oflags = O_WRONLY|O_APPEND; 3296 3297 if (e->e_lockfp != NULL) 3298 (void) close(fileno(e->e_lockfp)); 3299 3300 (void) setsignal(SIGINT, SIG_DFL); 3301 (void) setsignal(SIGHUP, SIG_DFL); 3302 (void) setsignal(SIGTERM, SIG_DFL); 3303 (void) umask(OldUmask); 3304 e->e_to = filename; 3305 ExitStat = EX_OK; 3306 3307 if (setjmp(CtxMailfileTimeout) != 0) 3308 { 3309 exit(EX_TEMPFAIL); 3310 } 3311 3312 if (TimeOuts.to_fileopen > 0) 3313 ev = setevent(TimeOuts.to_fileopen, mailfiletimeout, 0); 3314 else 3315 ev = NULL; 3316 3317 #ifdef HASLSTAT 3318 if (lstat(filename, &stb) < 0) 3319 #else 3320 if (stat(filename, &stb) < 0) 3321 #endif 3322 { 3323 stb.st_mode = ST_MODE_NOFILE; 3324 mode = FileMode; 3325 oflags |= O_CREAT|O_EXCL; 3326 } 3327 else if (bitset(S_IXUSR|S_IXGRP|S_IXOTH, stb.st_mode) || 3328 (!bitset(DBS_FILEDELIVERYTOHARDLINK, DontBlameSendmail) && 3329 stb.st_nlink != 1) || 3330 (SafeFileEnv != NULL && !S_ISREG(stb.st_mode))) 3331 exit(EX_CANTCREAT); 3332 if (mode == ST_MODE_NOFILE) 3333 mode = stb.st_mode; 3334 3335 /* limit the errors to those actually caused in the child */ 3336 errno = 0; 3337 ExitStat = EX_OK; 3338 3339 if (ctladdr != NULL || bitset(SFF_RUNASREALUID, sfflags)) 3340 { 3341 /* ignore setuid and setgid bits */ 3342 mode &= ~(S_ISGID|S_ISUID); 3343 } 3344 3345 /* we have to open the dfile BEFORE setuid */ 3346 if (e->e_dfp == NULL && bitset(EF_HAS_DF, e->e_flags)) 3347 { 3348 char *df = queuename(e, 'd'); 3349 3350 e->e_dfp = fopen(df, "r"); 3351 if (e->e_dfp == NULL) 3352 { 3353 syserr("mailfile: Cannot open %s for %s from %s", 3354 df, e->e_to, e->e_from.q_paddr); 3355 } 3356 } 3357 3358 /* select a new user to run as */ 3359 if (!bitset(SFF_RUNASREALUID, sfflags)) 3360 { 3361 if (bitnset(M_SPECIFIC_UID, mailer->m_flags)) 3362 { 3363 RealUserName = NULL; 3364 RealUid = mailer->m_uid; 3365 } 3366 else if (bitset(S_ISUID, mode)) 3367 { 3368 RealUserName = NULL; 3369 RealUid = stb.st_uid; 3370 } 3371 else if (ctladdr != NULL && ctladdr->q_uid != 0) 3372 { 3373 if (ctladdr->q_ruser != NULL) 3374 RealUserName = ctladdr->q_ruser; 3375 else 3376 RealUserName = ctladdr->q_user; 3377 RealUid = ctladdr->q_uid; 3378 } 3379 else if (mailer != NULL && mailer->m_uid != 0) 3380 { 3381 RealUserName = DefUser; 3382 RealUid = mailer->m_uid; 3383 } 3384 else 3385 { 3386 RealUserName = DefUser; 3387 RealUid = DefUid; 3388 } 3389 3390 /* select a new group to run as */ 3391 if (bitnset(M_SPECIFIC_UID, mailer->m_flags)) 3392 RealGid = mailer->m_gid; 3393 else if (bitset(S_ISGID, mode)) 3394 RealGid = stb.st_gid; 3395 else if (ctladdr != NULL && ctladdr->q_uid != 0) 3396 RealGid = ctladdr->q_gid; 3397 else if (mailer != NULL && mailer->m_gid != 0) 3398 RealGid = mailer->m_gid; 3399 else 3400 RealGid = DefGid; 3401 } 3402 3403 /* last ditch */ 3404 if (!bitset(SFF_ROOTOK, sfflags)) 3405 { 3406 if (RealUid == 0) 3407 RealUid = DefUid; 3408 if (RealGid == 0) 3409 RealGid = DefGid; 3410 } 3411 3412 /* set group id list (needs /etc/group access) */ 3413 if (RealUserName != NULL && !DontInitGroups) 3414 { 3415 if (initgroups(RealUserName, RealGid) == -1 && suidwarn) 3416 syserr("mailfile: initgroups(%s, %d) failed", 3417 RealUserName, RealGid); 3418 } 3419 else 3420 { 3421 GIDSET_T gidset[1]; 3422 3423 gidset[0] = RealGid; 3424 if (setgroups(1, gidset) == -1 && suidwarn) 3425 syserr("mailfile: setgroups() failed"); 3426 } 3427 3428 /* if you have a safe environment, go into it */ 3429 if (SafeFileEnv != NULL && SafeFileEnv[0] != '\0') 3430 { 3431 int i; 3432 3433 if (chroot(SafeFileEnv) < 0) 3434 { 3435 syserr("mailfile: Cannot chroot(%s)", 3436 SafeFileEnv); 3437 exit(EX_CANTCREAT); 3438 } 3439 i = strlen(SafeFileEnv); 3440 if (strncmp(SafeFileEnv, filename, i) == 0) 3441 filename += i; 3442 } 3443 if (chdir("/") < 0) 3444 syserr("mailfile: cannot chdir(/)"); 3445 3446 /* now reset the group and user ids */ 3447 endpwent(); 3448 if (setgid(RealGid) < 0 && suidwarn) 3449 syserr("mailfile: setgid(%ld) failed", (long) RealGid); 3450 vendor_set_uid(RealUid); 3451 if (setuid(RealUid) < 0 && suidwarn) 3452 syserr("mailfile: setuid(%ld) failed", (long) RealUid); 3453 3454 /* move into some "safe" directory */ 3455 if (mailer->m_execdir != NULL) 3456 { 3457 char *q; 3458 char buf[MAXLINE + 1]; 3459 3460 for (p = mailer->m_execdir; p != NULL; p = q) 3461 { 3462 q = strchr(p, ':'); 3463 if (q != NULL) 3464 *q = '\0'; 3465 expand(p, buf, sizeof buf, e); 3466 if (q != NULL) 3467 *q++ = ':'; 3468 if (tTd(11, 20)) 3469 printf("mailfile: trydir %s\n", 3470 buf); 3471 if (buf[0] != '\0' && chdir(buf) >= 0) 3472 break; 3473 } 3474 } 3475 3476 sfflags |= SFF_NOPATHCHECK; 3477 if (!bitset(DBS_FILEDELIVERYTOSYMLINK, DontBlameSendmail)) 3478 sfflags |= SFF_NOSLINK; 3479 if (!bitset(DBS_FILEDELIVERYTOHARDLINK, DontBlameSendmail)) 3480 sfflags |= SFF_NOHLINK; 3481 sfflags &= ~SFF_OPENASROOT; 3482 f = safefopen(filename, oflags, FileMode, sfflags); 3483 if (f == NULL) 3484 { 3485 message("554 cannot open %s: %s", 3486 shortenstring(filename, MAXSHORTSTR), 3487 errstring(errno)); 3488 exit(EX_CANTCREAT); 3489 } 3490 if (filechanged(filename, fileno(f), &stb)) 3491 { 3492 message("554 file changed after open"); 3493 exit(EX_CANTCREAT); 3494 } 3495 if (fstat(fileno(f), &stb) < 0) 3496 { 3497 message("554 cannot fstat %s", errstring(errno)); 3498 exit(EX_CANTCREAT); 3499 } 3500 3501 if (ev != NULL) 3502 clrevent(ev); 3503 3504 bzero(&mcibuf, sizeof mcibuf); 3505 mcibuf.mci_mailer = mailer; 3506 mcibuf.mci_out = f; 3507 mcibuf.mci_contentlen = 0; 3508 if (bitnset(M_7BITS, mailer->m_flags)) 3509 mcibuf.mci_flags |= MCIF_7BIT; 3510 3511 /* clear out per-message flags from connection structure */ 3512 mcibuf.mci_flags &= ~(MCIF_CVT7TO8|MCIF_CVT8TO7); 3513 3514 if (bitset(EF_HAS8BIT, e->e_flags) && 3515 !bitset(EF_DONT_MIME, e->e_flags) && 3516 bitnset(M_7BITS, mailer->m_flags)) 3517 mcibuf.mci_flags |= MCIF_CVT8TO7; 3518 3519 #if MIME7TO8 3520 if (bitnset(M_MAKE8BIT, mailer->m_flags) && 3521 !bitset(MCIF_7BIT, mcibuf.mci_flags) && 3522 (p = hvalue("Content-Transfer-Encoding", e->e_header)) != NULL && 3523 (strcasecmp(p, "quoted-printable") == 0 || 3524 strcasecmp(p, "base64") == 0) && 3525 (p = hvalue("Content-Type", e->e_header)) != NULL) 3526 { 3527 /* may want to convert 7 -> 8 */ 3528 /* XXX should really parse it here -- and use a class XXX */ 3529 if (strncasecmp(p, "text/plain", 10) == 0 && 3530 (p[10] == '\0' || p[10] == ' ' || p[10] == ';')) 3531 mcibuf.mci_flags |= MCIF_CVT7TO8; 3532 } 3533 #endif 3534 3535 putfromline(&mcibuf, e); 3536 (*e->e_puthdr)(&mcibuf, e->e_header, e); 3537 (*e->e_putbody)(&mcibuf, e, NULL); 3538 putline("\n", &mcibuf); 3539 if (fflush(f) < 0 || ferror(f)) 3540 { 3541 message("451 I/O error: %s", errstring(errno)); 3542 setstat(EX_IOERR); 3543 } 3544 3545 /* reset ISUID & ISGID bits for paranoid systems */ 3546 #if HASFCHMOD 3547 (void) fchmod(fileno(f), (MODE_T) stb.st_mode); 3548 #else 3549 (void) chmod(filename, (MODE_T) stb.st_mode); 3550 #endif 3551 (void) xfclose(f, "mailfile", filename); 3552 (void) fflush(stdout); 3553 setuid(RealUid); 3554 exit(ExitStat); 3555 /*NOTREACHED*/ 3556 } 3557 else 3558 { 3559 /* parent -- wait for exit status */ 3560 int st; 3561 3562 st = waitfor(pid); 3563 if (st == -1) 3564 { 3565 syserr("mailfile: %s: wait", mailer->m_name); 3566 return (EX_SOFTWARE); 3567 } 3568 if (WIFEXITED(st)) 3569 return (WEXITSTATUS(st)); 3570 else 3571 { 3572 syserr("mailfile: %s: child died on signal %d", 3573 mailer->m_name, st); 3574 return (EX_UNAVAILABLE); 3575 } 3576 /*NOTREACHED*/ 3577 } 3578 return EX_UNAVAILABLE; /* avoid compiler warning on IRIX */ 3579 } 3580 3581 static void 3582 mailfiletimeout() 3583 { 3584 longjmp(CtxMailfileTimeout, 1); 3585 } 3586 /* 3587 ** HOSTSIGNATURE -- return the "signature" for a host. 3588 ** 3589 ** The signature describes how we are going to send this -- it 3590 ** can be just the hostname (for non-Internet hosts) or can be 3591 ** an ordered list of MX hosts. 3592 ** 3593 ** Parameters: 3594 ** m -- the mailer describing this host. 3595 ** host -- the host name. 3596 ** e -- the current envelope. 3597 ** 3598 ** Returns: 3599 ** The signature for this host. 3600 ** 3601 ** Side Effects: 3602 ** Can tweak the symbol table. 3603 */ 3604 3605 char * 3606 hostsignature(m, host, e) 3607 register MAILER *m; 3608 char *host; 3609 ENVELOPE *e; 3610 { 3611 register char *p; 3612 register STAB *s; 3613 int i; 3614 int len; 3615 #if NAMED_BIND 3616 int nmx; 3617 char *hp; 3618 char *endp; 3619 int oldoptions = _res.options; 3620 char *mxhosts[MAXMXHOSTS + 1]; 3621 #endif 3622 3623 /* 3624 ** Check to see if this uses IPC -- if not, it can't have MX records. 3625 */ 3626 3627 p = m->m_mailer; 3628 if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 3629 { 3630 /* just an ordinary mailer */ 3631 return host; 3632 } 3633 3634 /* 3635 ** Look it up in the symbol table. 3636 */ 3637 3638 s = stab(host, ST_HOSTSIG, ST_ENTER); 3639 if (s->s_hostsig != NULL) 3640 return s->s_hostsig; 3641 3642 /* 3643 ** Not already there -- create a signature. 3644 */ 3645 3646 #if NAMED_BIND 3647 if (ConfigLevel < 2) 3648 _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 3649 3650 for (hp = host; hp != NULL; hp = endp) 3651 { 3652 endp = strchr(hp, ':'); 3653 if (endp != NULL) 3654 *endp = '\0'; 3655 3656 if (bitnset(M_NOMX, m->m_flags)) 3657 { 3658 /* skip MX lookups */ 3659 nmx = 1; 3660 mxhosts[0] = hp; 3661 } 3662 else 3663 { 3664 auto int rcode; 3665 3666 nmx = getmxrr(hp, mxhosts, TRUE, &rcode); 3667 if (nmx <= 0) 3668 { 3669 register MCI *mci; 3670 3671 /* update the connection info for this host */ 3672 mci = mci_get(hp, m); 3673 mci->mci_errno = errno; 3674 mci->mci_herrno = h_errno; 3675 mci->mci_lastuse = curtime(); 3676 mci_setstat(mci, rcode, NULL, NULL); 3677 3678 /* use the original host name as signature */ 3679 nmx = 1; 3680 mxhosts[0] = hp; 3681 } 3682 } 3683 3684 len = 0; 3685 for (i = 0; i < nmx; i++) 3686 { 3687 len += strlen(mxhosts[i]) + 1; 3688 } 3689 if (s->s_hostsig != NULL) 3690 len += strlen(s->s_hostsig) + 1; 3691 p = xalloc(len); 3692 if (s->s_hostsig != NULL) 3693 { 3694 (void) strcpy(p, s->s_hostsig); 3695 free(s->s_hostsig); 3696 s->s_hostsig = p; 3697 p += strlen(p); 3698 *p++ = ':'; 3699 } 3700 else 3701 s->s_hostsig = p; 3702 for (i = 0; i < nmx; i++) 3703 { 3704 if (i != 0) 3705 *p++ = ':'; 3706 strcpy(p, mxhosts[i]); 3707 p += strlen(p); 3708 } 3709 if (endp != NULL) 3710 *endp++ = ':'; 3711 } 3712 makelower(s->s_hostsig); 3713 if (ConfigLevel < 2) 3714 _res.options = oldoptions; 3715 #else 3716 /* not using BIND -- the signature is just the host name */ 3717 s->s_hostsig = host; 3718 #endif 3719 if (tTd(17, 1)) 3720 printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 3721 return s->s_hostsig; 3722 } 3723