1 # include <signal.h> 2 # include <errno.h> 3 # include "sendmail.h" 4 # include <sys/stat.h> 5 6 SCCSID(@(#)deliver.c 4.9 08/11/84); 7 8 /* 9 ** DELIVER -- Deliver a message to a list of addresses. 10 ** 11 ** This routine delivers to everyone on the same host as the 12 ** user on the head of the list. It is clever about mailers 13 ** that don't handle multiple users. It is NOT guaranteed 14 ** that it will deliver to all these addresses however -- so 15 ** deliver should be called once for each address on the 16 ** list. 17 ** 18 ** Parameters: 19 ** e -- the envelope to deliver. 20 ** firstto -- head of the address list to deliver to. 21 ** 22 ** Returns: 23 ** zero -- successfully delivered. 24 ** else -- some failure, see ExitStat for more info. 25 ** 26 ** Side Effects: 27 ** The standard input is passed off to someone. 28 */ 29 30 deliver(e, firstto) 31 register ENVELOPE *e; 32 ADDRESS *firstto; 33 { 34 char *host; /* host being sent to */ 35 char *user; /* user being sent to */ 36 char **pvp; 37 register char **mvp; 38 register char *p; 39 register MAILER *m; /* mailer for this recipient */ 40 ADDRESS *ctladdr; 41 register ADDRESS *to = firstto; 42 bool clever = FALSE; /* running user smtp to this mailer */ 43 ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 44 register int rcode; /* response code */ 45 char *pv[MAXPV+1]; 46 char tobuf[MAXLINE-50]; /* text line of to people */ 47 char buf[MAXNAME]; 48 char tfrombuf[MAXNAME]; /* translated from person */ 49 extern bool checkcompat(); 50 extern ADDRESS *getctladdr(); 51 extern char *remotename(); 52 53 errno = 0; 54 if (bitset(QDONTSEND, to->q_flags)) 55 return (0); 56 57 m = to->q_mailer; 58 host = to->q_host; 59 60 # ifdef DEBUG 61 if (tTd(10, 1)) 62 printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 63 m->m_mno, host, to->q_user); 64 # endif DEBUG 65 66 /* 67 ** If this mailer is expensive, and if we don't want to make 68 ** connections now, just mark these addresses and return. 69 ** This is useful if we want to batch connections to 70 ** reduce load. This will cause the messages to be 71 ** queued up, and a daemon will come along to send the 72 ** messages later. 73 ** This should be on a per-mailer basis. 74 */ 75 76 if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) && 77 !Verbose) 78 { 79 for (; to != NULL; to = to->q_next) 80 { 81 if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m) 82 continue; 83 to->q_flags |= QQUEUEUP|QDONTSEND; 84 e->e_to = to->q_paddr; 85 message(Arpa_Info, "queued"); 86 if (LogLevel > 4) 87 logdelivery("queued"); 88 } 89 e->e_to = NULL; 90 return (0); 91 } 92 93 /* 94 ** Do initial argv setup. 95 ** Insert the mailer name. Notice that $x expansion is 96 ** NOT done on the mailer name. Then, if the mailer has 97 ** a picky -f flag, we insert it as appropriate. This 98 ** code does not check for 'pv' overflow; this places a 99 ** manifest lower limit of 4 for MAXPV. 100 ** The from address rewrite is expected to make 101 ** the address relative to the other end. 102 */ 103 104 /* rewrite from address, using rewriting rules */ 105 expand("\001f", buf, &buf[sizeof buf - 1], e); 106 (void) strcpy(tfrombuf, remotename(buf, m, TRUE, TRUE)); 107 108 define('g', tfrombuf, e); /* translated sender address */ 109 define('h', host, e); /* to host */ 110 Errors = 0; 111 pvp = pv; 112 *pvp++ = m->m_argv[0]; 113 114 /* insert -f or -r flag as appropriate */ 115 if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 116 { 117 if (bitnset(M_FOPT, m->m_flags)) 118 *pvp++ = "-f"; 119 else 120 *pvp++ = "-r"; 121 expand("\001g", buf, &buf[sizeof buf - 1], e); 122 *pvp++ = newstr(buf); 123 } 124 125 /* 126 ** Append the other fixed parts of the argv. These run 127 ** up to the first entry containing "$u". There can only 128 ** be one of these, and there are only a few more slots 129 ** in the pv after it. 130 */ 131 132 for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 133 { 134 while ((p = index(p, '\001')) != NULL) 135 if (*++p == 'u') 136 break; 137 if (p != NULL) 138 break; 139 140 /* this entry is safe -- go ahead and process it */ 141 expand(*mvp, buf, &buf[sizeof buf - 1], e); 142 *pvp++ = newstr(buf); 143 if (pvp >= &pv[MAXPV - 3]) 144 { 145 syserr("Too many parameters to %s before $u", pv[0]); 146 return (-1); 147 } 148 } 149 150 /* 151 ** If we have no substitution for the user name in the argument 152 ** list, we know that we must supply the names otherwise -- and 153 ** SMTP is the answer!! 154 */ 155 156 if (*mvp == NULL) 157 { 158 /* running SMTP */ 159 # ifdef SMTP 160 clever = TRUE; 161 *pvp = NULL; 162 # else SMTP 163 /* oops! we don't implement SMTP */ 164 syserr("SMTP style mailer"); 165 return (EX_SOFTWARE); 166 # endif SMTP 167 } 168 169 /* 170 ** At this point *mvp points to the argument with $u. We 171 ** run through our address list and append all the addresses 172 ** we can. If we run out of space, do not fret! We can 173 ** always send another copy later. 174 */ 175 176 tobuf[0] = '\0'; 177 e->e_to = tobuf; 178 ctladdr = NULL; 179 for (; to != NULL; to = to->q_next) 180 { 181 /* avoid sending multiple recipients to dumb mailers */ 182 if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 183 break; 184 185 /* if already sent or not for this host, don't send */ 186 if (bitset(QDONTSEND, to->q_flags) || 187 strcmp(to->q_host, host) != 0 || 188 to->q_mailer != firstto->q_mailer) 189 continue; 190 191 /* avoid overflowing tobuf */ 192 if (sizeof tobuf - (strlen(to->q_paddr) + strlen(tobuf) + 2) < 0) 193 break; 194 195 # ifdef DEBUG 196 if (tTd(10, 1)) 197 { 198 printf("\nsend to "); 199 printaddr(to, FALSE); 200 } 201 # endif DEBUG 202 203 /* compute effective uid/gid when sending */ 204 if (to->q_mailer == ProgMailer) 205 ctladdr = getctladdr(to); 206 207 user = to->q_user; 208 e->e_to = to->q_paddr; 209 to->q_flags |= QDONTSEND; 210 211 /* 212 ** Check to see that these people are allowed to 213 ** talk to each other. 214 */ 215 216 if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 217 { 218 usrerr("Message is too large; %ld bytes max", m->m_maxsize); 219 NoReturn = TRUE; 220 giveresponse(EX_UNAVAILABLE, m, e); 221 continue; 222 } 223 if (!checkcompat(to)) 224 { 225 giveresponse(EX_UNAVAILABLE, m, e); 226 continue; 227 } 228 229 /* 230 ** Strip quote bits from names if the mailer is dumb 231 ** about them. 232 */ 233 234 if (bitnset(M_STRIPQ, m->m_flags)) 235 { 236 stripquotes(user, TRUE); 237 stripquotes(host, TRUE); 238 } 239 else 240 { 241 stripquotes(user, FALSE); 242 stripquotes(host, FALSE); 243 } 244 245 /* hack attack -- delivermail compatibility */ 246 if (m == ProgMailer && *user == '|') 247 user++; 248 249 /* 250 ** If an error message has already been given, don't 251 ** bother to send to this address. 252 ** 253 ** >>>>>>>>>> This clause assumes that the local mailer 254 ** >> NOTE >> cannot do any further aliasing; that 255 ** >>>>>>>>>> function is subsumed by sendmail. 256 */ 257 258 if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 259 continue; 260 261 /* save statistics.... */ 262 markstats(e, to); 263 264 /* 265 ** See if this user name is "special". 266 ** If the user name has a slash in it, assume that this 267 ** is a file -- send it off without further ado. Note 268 ** that this type of addresses is not processed along 269 ** with the others, so we fudge on the To person. 270 */ 271 272 if (m == LocalMailer) 273 { 274 if (user[0] == '/') 275 { 276 rcode = mailfile(user, getctladdr(to)); 277 giveresponse(rcode, m, e); 278 continue; 279 } 280 } 281 282 /* 283 ** Address is verified -- add this user to mailer 284 ** argv, and add it to the print list of recipients. 285 */ 286 287 /* link together the chain of recipients */ 288 to->q_tchain = tochain; 289 tochain = to; 290 291 /* create list of users for error messages */ 292 (void) strcat(tobuf, ","); 293 (void) strcat(tobuf, to->q_paddr); 294 define('u', user, e); /* to user */ 295 define('z', to->q_home, e); /* user's home */ 296 297 /* 298 ** Expand out this user into argument list. 299 */ 300 301 if (!clever) 302 { 303 expand(*mvp, buf, &buf[sizeof buf - 1], e); 304 *pvp++ = newstr(buf); 305 if (pvp >= &pv[MAXPV - 2]) 306 { 307 /* allow some space for trailing parms */ 308 break; 309 } 310 } 311 } 312 313 /* see if any addresses still exist */ 314 if (tobuf[0] == '\0') 315 { 316 define('g', (char *) NULL, e); 317 return (0); 318 } 319 320 /* print out messages as full list */ 321 e->e_to = tobuf + 1; 322 323 /* 324 ** Fill out any parameters after the $u parameter. 325 */ 326 327 while (!clever && *++mvp != NULL) 328 { 329 expand(*mvp, buf, &buf[sizeof buf - 1], e); 330 *pvp++ = newstr(buf); 331 if (pvp >= &pv[MAXPV]) 332 syserr("deliver: pv overflow after $u for %s", pv[0]); 333 } 334 *pvp++ = NULL; 335 336 /* 337 ** Call the mailer. 338 ** The argument vector gets built, pipes 339 ** are created as necessary, and we fork & exec as 340 ** appropriate. 341 ** If we are running SMTP, we just need to clean up. 342 */ 343 344 message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name); 345 346 if (ctladdr == NULL) 347 ctladdr = &e->e_from; 348 # ifdef SMTP 349 if (clever) 350 { 351 /* send the initial SMTP protocol */ 352 rcode = smtpinit(m, pv); 353 354 if (rcode == EX_OK) 355 { 356 /* send the recipient list */ 357 tobuf[0] = '\0'; 358 for (to = tochain; to != NULL; to = to->q_tchain) 359 { 360 int i; 361 362 e->e_to = to->q_paddr; 363 i = smtprcpt(to, m); 364 if (i != EX_OK) 365 { 366 markfailure(e, to, i); 367 giveresponse(i, m, e); 368 } 369 else 370 { 371 strcat(tobuf, ","); 372 strcat(tobuf, to->q_paddr); 373 } 374 } 375 376 /* now send the data */ 377 if (tobuf[0] == '\0') 378 e->e_to = NULL; 379 else 380 { 381 e->e_to = tobuf + 1; 382 rcode = smtpdata(m, e); 383 } 384 385 /* now close the connection */ 386 smtpquit(m); 387 } 388 } 389 else 390 # endif SMTP 391 rcode = sendoff(e, m, pv, ctladdr); 392 393 /* 394 ** Do final status disposal. 395 ** We check for something in tobuf for the SMTP case. 396 ** If we got a temporary failure, arrange to queue the 397 ** addressees. 398 */ 399 400 if (tobuf[0] != '\0') 401 giveresponse(rcode, m, e); 402 if (rcode != EX_OK) 403 { 404 for (to = tochain; to != NULL; to = to->q_tchain) 405 markfailure(e, to, rcode); 406 } 407 408 errno = 0; 409 define('g', (char *) NULL, e); 410 return (rcode); 411 } 412 /* 413 ** MARKFAILURE -- mark a failure on a specific address. 414 ** 415 ** Parameters: 416 ** e -- the envelope we are sending. 417 ** q -- the address to mark. 418 ** rcode -- the code signifying the particular failure. 419 ** 420 ** Returns: 421 ** none. 422 ** 423 ** Side Effects: 424 ** marks the address (and possibly the envelope) with the 425 ** failure so that an error will be returned or 426 ** the message will be queued, as appropriate. 427 */ 428 429 markfailure(e, q, rcode) 430 register ENVELOPE *e; 431 register ADDRESS *q; 432 int rcode; 433 { 434 if (rcode == EX_OK) 435 return; 436 else if (rcode != EX_TEMPFAIL) 437 q->q_flags |= QBADADDR; 438 else if (curtime() > e->e_ctime + TimeOut) 439 { 440 extern char *pintvl(); 441 char buf[MAXLINE]; 442 443 if (!bitset(EF_TIMEOUT, e->e_flags)) 444 { 445 (void) sprintf(buf, "Cannot send message for %s", 446 pintvl(TimeOut, FALSE)); 447 if (e->e_message != NULL) 448 free(e->e_message); 449 e->e_message = newstr(buf); 450 message(Arpa_Info, buf); 451 } 452 q->q_flags |= QBADADDR; 453 e->e_flags |= EF_TIMEOUT; 454 } 455 else 456 q->q_flags |= QQUEUEUP; 457 } 458 /* 459 ** DOFORK -- do a fork, retrying a couple of times on failure. 460 ** 461 ** This MUST be a macro, since after a vfork we are running 462 ** two processes on the same stack!!! 463 ** 464 ** Parameters: 465 ** none. 466 ** 467 ** Returns: 468 ** From a macro??? You've got to be kidding! 469 ** 470 ** Side Effects: 471 ** Modifies the ==> LOCAL <== variable 'pid', leaving: 472 ** pid of child in parent, zero in child. 473 ** -1 on unrecoverable error. 474 ** 475 ** Notes: 476 ** I'm awfully sorry this looks so awful. That's 477 ** vfork for you..... 478 */ 479 480 # define NFORKTRIES 5 481 # ifdef VMUNIX 482 # define XFORK vfork 483 # else VMUNIX 484 # define XFORK fork 485 # endif VMUNIX 486 487 # define DOFORK(fORKfN) \ 488 {\ 489 register int i;\ 490 \ 491 for (i = NFORKTRIES; i-- > 0; )\ 492 {\ 493 pid = fORKfN();\ 494 if (pid >= 0)\ 495 break;\ 496 sleep(NFORKTRIES - i);\ 497 }\ 498 } 499 /* 500 ** DOFORK -- simple fork interface to DOFORK. 501 ** 502 ** Parameters: 503 ** none. 504 ** 505 ** Returns: 506 ** pid of child in parent. 507 ** zero in child. 508 ** -1 on error. 509 ** 510 ** Side Effects: 511 ** returns twice, once in parent and once in child. 512 */ 513 514 dofork() 515 { 516 register int pid; 517 518 DOFORK(fork); 519 return (pid); 520 } 521 /* 522 ** SENDOFF -- send off call to mailer & collect response. 523 ** 524 ** Parameters: 525 ** e -- the envelope to mail. 526 ** m -- mailer descriptor. 527 ** pvp -- parameter vector to send to it. 528 ** ctladdr -- an address pointer controlling the 529 ** user/groupid etc. of the mailer. 530 ** 531 ** Returns: 532 ** exit status of mailer. 533 ** 534 ** Side Effects: 535 ** none. 536 */ 537 538 sendoff(e, m, pvp, ctladdr) 539 register ENVELOPE *e; 540 MAILER *m; 541 char **pvp; 542 ADDRESS *ctladdr; 543 { 544 auto FILE *mfile; 545 auto FILE *rfile; 546 register int i; 547 int pid; 548 549 /* 550 ** Create connection to mailer. 551 */ 552 553 pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 554 if (pid < 0) 555 return (-1); 556 557 /* 558 ** Format and send message. 559 */ 560 561 putfromline(mfile, m); 562 (*e->e_puthdr)(mfile, m, e); 563 putline("\n", mfile, m); 564 (*e->e_putbody)(mfile, m, e); 565 (void) fclose(mfile); 566 567 i = endmailer(pid, pvp[0]); 568 569 /* arrange a return receipt if requested */ 570 if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags)) 571 { 572 e->e_flags |= EF_SENDRECEIPT; 573 /* do we want to send back more info? */ 574 } 575 576 return (i); 577 } 578 /* 579 ** ENDMAILER -- Wait for mailer to terminate. 580 ** 581 ** We should never get fatal errors (e.g., segmentation 582 ** violation), so we report those specially. For other 583 ** errors, we choose a status message (into statmsg), 584 ** and if it represents an error, we print it. 585 ** 586 ** Parameters: 587 ** pid -- pid of mailer. 588 ** name -- name of mailer (for error messages). 589 ** 590 ** Returns: 591 ** exit code of mailer. 592 ** 593 ** Side Effects: 594 ** none. 595 */ 596 597 endmailer(pid, name) 598 int pid; 599 char *name; 600 { 601 int st; 602 603 /* in the IPC case there is nothing to wait for */ 604 if (pid == 0) 605 return (EX_OK); 606 607 /* wait for the mailer process to die and collect status */ 608 st = waitfor(pid); 609 if (st == -1) 610 { 611 syserr("endmailer %s: wait", name); 612 return (EX_SOFTWARE); 613 } 614 615 /* see if it died a horrid death */ 616 if ((st & 0377) != 0) 617 { 618 syserr("endmailer %s: stat %o", name, st); 619 ExitStat = EX_UNAVAILABLE; 620 return (EX_UNAVAILABLE); 621 } 622 623 /* normal death -- return status */ 624 st = (st >> 8) & 0377; 625 return (st); 626 } 627 /* 628 ** OPENMAILER -- open connection to mailer. 629 ** 630 ** Parameters: 631 ** m -- mailer descriptor. 632 ** pvp -- parameter vector to pass to mailer. 633 ** ctladdr -- controlling address for user. 634 ** clever -- create a full duplex connection. 635 ** pmfile -- pointer to mfile (to mailer) connection. 636 ** prfile -- pointer to rfile (from mailer) connection. 637 ** 638 ** Returns: 639 ** pid of mailer ( > 0 ). 640 ** -1 on error. 641 ** zero on an IPC connection. 642 ** 643 ** Side Effects: 644 ** creates a mailer in a subprocess. 645 */ 646 647 openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 648 MAILER *m; 649 char **pvp; 650 ADDRESS *ctladdr; 651 bool clever; 652 FILE **pmfile; 653 FILE **prfile; 654 { 655 int pid; 656 int mpvect[2]; 657 int rpvect[2]; 658 FILE *mfile; 659 FILE *rfile; 660 extern FILE *fdopen(); 661 662 # ifdef DEBUG 663 if (tTd(11, 1)) 664 { 665 printf("openmailer:"); 666 printav(pvp); 667 } 668 # endif DEBUG 669 errno = 0; 670 671 /* 672 ** Deal with the special case of mail handled through an IPC 673 ** connection. 674 ** In this case we don't actually fork. We must be 675 ** running SMTP for this to work. We will return a 676 ** zero pid to indicate that we are running IPC. 677 ** We also handle a debug version that just talks to stdin/out. 678 */ 679 680 #ifdef DEBUG 681 /* check for Local Person Communication -- not for mortals!!! */ 682 if (strcmp(m->m_mailer, "[LPC]") == 0) 683 { 684 *pmfile = stdout; 685 *prfile = stdin; 686 return (0); 687 } 688 #endif DEBUG 689 690 if (strcmp(m->m_mailer, "[IPC]") == 0) 691 { 692 #ifdef DAEMON 693 register int i; 694 register u_short port; 695 696 if (!clever) 697 syserr("non-clever IPC"); 698 if (pvp[2] != NULL) 699 port = atoi(pvp[2]); 700 else 701 port = 0; 702 i = makeconnection(pvp[1], port, pmfile, prfile); 703 if (i != EX_OK) 704 { 705 ExitStat = i; 706 return (-1); 707 } 708 else 709 return (0); 710 #else DAEMON 711 syserr("openmailer: no IPC"); 712 return (-1); 713 #endif DAEMON 714 } 715 716 /* create a pipe to shove the mail through */ 717 if (pipe(mpvect) < 0) 718 { 719 syserr("openmailer: pipe (to mailer)"); 720 return (-1); 721 } 722 723 #ifdef SMTP 724 /* if this mailer speaks smtp, create a return pipe */ 725 if (clever && pipe(rpvect) < 0) 726 { 727 syserr("openmailer: pipe (from mailer)"); 728 (void) close(mpvect[0]); 729 (void) close(mpvect[1]); 730 return (-1); 731 } 732 #endif SMTP 733 734 /* 735 ** Actually fork the mailer process. 736 ** DOFORK is clever about retrying. 737 */ 738 739 if (CurEnv->e_xfp != NULL) 740 (void) fflush(CurEnv->e_xfp); /* for debugging */ 741 (void) fflush(stdout); 742 DOFORK(XFORK); 743 /* pid is set by DOFORK */ 744 if (pid < 0) 745 { 746 /* failure */ 747 syserr("openmailer: cannot fork"); 748 (void) close(mpvect[0]); 749 (void) close(mpvect[1]); 750 #ifdef SMTP 751 if (clever) 752 { 753 (void) close(rpvect[0]); 754 (void) close(rpvect[1]); 755 } 756 #endif SMTP 757 return (-1); 758 } 759 else if (pid == 0) 760 { 761 int i; 762 763 /* child -- set up input & exec mailer */ 764 /* make diagnostic output be standard output */ 765 (void) signal(SIGINT, SIG_IGN); 766 (void) signal(SIGHUP, SIG_IGN); 767 (void) signal(SIGTERM, SIG_DFL); 768 769 /* arrange to filter standard & diag output of command */ 770 if (clever) 771 { 772 (void) close(rpvect[0]); 773 (void) close(1); 774 (void) dup(rpvect[1]); 775 (void) close(rpvect[1]); 776 } 777 else if (OpMode == MD_SMTP || HoldErrs) 778 { 779 /* put mailer output in transcript */ 780 (void) close(1); 781 (void) dup(fileno(CurEnv->e_xfp)); 782 } 783 (void) close(2); 784 (void) dup(1); 785 786 /* arrange to get standard input */ 787 (void) close(mpvect[1]); 788 (void) close(0); 789 if (dup(mpvect[0]) < 0) 790 { 791 syserr("Cannot dup to zero!"); 792 _exit(EX_OSERR); 793 } 794 (void) close(mpvect[0]); 795 if (!bitnset(M_RESTR, m->m_flags)) 796 { 797 if (ctladdr == NULL || ctladdr->q_uid == 0) 798 { 799 (void) setgid(DefGid); 800 (void) setuid(DefUid); 801 } 802 else 803 { 804 (void) setgid(ctladdr->q_gid); 805 (void) setuid(ctladdr->q_uid); 806 } 807 } 808 809 /* arrange for all the files to be closed */ 810 for (i = 3; i < 50; i++) 811 #ifdef FIOCLEX 812 (void) ioctl(i, FIOCLEX, 0); 813 #else FIOCLEX 814 (void) close(i); 815 #endif FIOCLEX 816 817 /* try to execute the mailer */ 818 execv(m->m_mailer, pvp); 819 820 #ifdef FIOCLEX 821 syserr("Cannot exec %s", m->m_mailer); 822 #else FIOCLEX 823 printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 824 (void) fflush(stdout); 825 #endif FIOCLEX 826 if (m == LocalMailer) 827 _exit(EX_TEMPFAIL); 828 else 829 _exit(EX_UNAVAILABLE); 830 } 831 832 /* 833 ** Set up return value. 834 */ 835 836 (void) close(mpvect[0]); 837 mfile = fdopen(mpvect[1], "w"); 838 if (clever) 839 { 840 (void) close(rpvect[1]); 841 rfile = fdopen(rpvect[0], "r"); 842 } 843 844 *pmfile = mfile; 845 *prfile = rfile; 846 847 return (pid); 848 } 849 /* 850 ** GIVERESPONSE -- Interpret an error response from a mailer 851 ** 852 ** Parameters: 853 ** stat -- the status code from the mailer (high byte 854 ** only; core dumps must have been taken care of 855 ** already). 856 ** m -- the mailer descriptor for this mailer. 857 ** 858 ** Returns: 859 ** none. 860 ** 861 ** Side Effects: 862 ** Errors may be incremented. 863 ** ExitStat may be set. 864 */ 865 866 giveresponse(stat, m, e) 867 int stat; 868 register MAILER *m; 869 ENVELOPE *e; 870 { 871 register char *statmsg; 872 extern char *SysExMsg[]; 873 register int i; 874 extern int N_SysEx; 875 char buf[MAXLINE]; 876 877 #ifdef lint 878 if (m == NULL) 879 return; 880 #endif lint 881 882 /* 883 ** Compute status message from code. 884 */ 885 886 i = stat - EX__BASE; 887 if (stat == 0) 888 statmsg = "250 Sent"; 889 else if (i < 0 || i > N_SysEx) 890 { 891 (void) sprintf(buf, "554 unknown mailer error %d", stat); 892 stat = EX_UNAVAILABLE; 893 statmsg = buf; 894 } 895 else if (stat == EX_TEMPFAIL) 896 { 897 (void) strcpy(buf, SysExMsg[i]); 898 if (errno != 0) 899 { 900 extern char *errstring(); 901 902 (void) strcat(buf, ": "); 903 (void) strcat(buf, errstring(errno)); 904 } 905 statmsg = buf; 906 } 907 else 908 statmsg = SysExMsg[i]; 909 910 /* 911 ** Print the message as appropriate 912 */ 913 914 if (stat == EX_OK || stat == EX_TEMPFAIL) 915 message(Arpa_Info, &statmsg[4]); 916 else 917 { 918 Errors++; 919 usrerr(statmsg); 920 } 921 922 /* 923 ** Final cleanup. 924 ** Log a record of the transaction. Compute the new 925 ** ExitStat -- if we already had an error, stick with 926 ** that. 927 */ 928 929 if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2)) 930 logdelivery(&statmsg[4]); 931 932 if (stat != EX_TEMPFAIL) 933 setstat(stat); 934 if (stat != EX_OK) 935 { 936 if (e->e_message != NULL) 937 free(e->e_message); 938 e->e_message = newstr(&statmsg[4]); 939 } 940 errno = 0; 941 } 942 /* 943 ** LOGDELIVERY -- log the delivery in the system log 944 ** 945 ** Parameters: 946 ** stat -- the message to print for the status 947 ** 948 ** Returns: 949 ** none 950 ** 951 ** Side Effects: 952 ** none 953 */ 954 955 logdelivery(stat) 956 char *stat; 957 { 958 extern char *pintvl(); 959 960 # ifdef LOG 961 syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id, 962 CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), stat); 963 # endif LOG 964 } 965 /* 966 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 967 ** 968 ** This can be made an arbitrary message separator by changing $l 969 ** 970 ** One of the ugliest hacks seen by human eyes is contained herein: 971 ** UUCP wants those stupid "remote from <host>" lines. Why oh why 972 ** does a well-meaning programmer such as myself have to deal with 973 ** this kind of antique garbage???? 974 ** 975 ** Parameters: 976 ** fp -- the file to output to. 977 ** m -- the mailer describing this entry. 978 ** 979 ** Returns: 980 ** none 981 ** 982 ** Side Effects: 983 ** outputs some text to fp. 984 */ 985 986 putfromline(fp, m) 987 register FILE *fp; 988 register MAILER *m; 989 { 990 char *template = "\001l\n"; 991 char buf[MAXLINE]; 992 993 if (bitnset(M_NHDR, m->m_flags)) 994 return; 995 996 # ifdef UGLYUUCP 997 if (bitnset(M_UGLYUUCP, m->m_flags)) 998 { 999 char *bang; 1000 char xbuf[MAXLINE]; 1001 1002 expand("\001g", buf, &buf[sizeof buf - 1], CurEnv); 1003 bang = index(buf, '!'); 1004 if (bang == NULL) 1005 syserr("No ! in UUCP! (%s)", buf); 1006 else 1007 { 1008 *bang++ = '\0'; 1009 (void) sprintf(xbuf, "From %s \001d remote from %s\n", bang, buf); 1010 template = xbuf; 1011 } 1012 } 1013 # endif UGLYUUCP 1014 expand(template, buf, &buf[sizeof buf - 1], CurEnv); 1015 putline(buf, fp, m); 1016 } 1017 /* 1018 ** PUTBODY -- put the body of a message. 1019 ** 1020 ** Parameters: 1021 ** fp -- file to output onto. 1022 ** m -- a mailer descriptor to control output format. 1023 ** e -- the envelope to put out. 1024 ** 1025 ** Returns: 1026 ** none. 1027 ** 1028 ** Side Effects: 1029 ** The message is written onto fp. 1030 */ 1031 1032 putbody(fp, m, e) 1033 FILE *fp; 1034 MAILER *m; 1035 register ENVELOPE *e; 1036 { 1037 char buf[MAXLINE]; 1038 1039 /* 1040 ** Output the body of the message 1041 */ 1042 1043 if (e->e_dfp == NULL) 1044 { 1045 if (e->e_df != NULL) 1046 { 1047 e->e_dfp = fopen(e->e_df, "r"); 1048 if (e->e_dfp == NULL) 1049 syserr("Cannot open %s", e->e_df); 1050 } 1051 else 1052 putline("<<< No Message Collected >>>", fp, m); 1053 } 1054 if (e->e_dfp != NULL) 1055 { 1056 rewind(e->e_dfp); 1057 while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 1058 { 1059 if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1060 strncmp(buf, "From", 4) == 0) 1061 putc('>', fp); 1062 putline(buf, fp, m); 1063 } 1064 1065 if (ferror(e->e_dfp)) 1066 { 1067 syserr("putbody: read error"); 1068 ExitStat = EX_IOERR; 1069 } 1070 } 1071 1072 (void) fflush(fp); 1073 if (ferror(fp) && errno != EPIPE) 1074 { 1075 syserr("putbody: write error"); 1076 ExitStat = EX_IOERR; 1077 } 1078 errno = 0; 1079 } 1080 /* 1081 ** MAILFILE -- Send a message to a file. 1082 ** 1083 ** If the file has the setuid/setgid bits set, but NO execute 1084 ** bits, sendmail will try to become the owner of that file 1085 ** rather than the real user. Obviously, this only works if 1086 ** sendmail runs as root. 1087 ** 1088 ** This could be done as a subordinate mailer, except that it 1089 ** is used implicitly to save messages in ~/dead.letter. We 1090 ** view this as being sufficiently important as to include it 1091 ** here. For example, if the system is dying, we shouldn't have 1092 ** to create another process plus some pipes to save the message. 1093 ** 1094 ** Parameters: 1095 ** filename -- the name of the file to send to. 1096 ** ctladdr -- the controlling address header -- includes 1097 ** the userid/groupid to be when sending. 1098 ** 1099 ** Returns: 1100 ** The exit code associated with the operation. 1101 ** 1102 ** Side Effects: 1103 ** none. 1104 */ 1105 1106 mailfile(filename, ctladdr) 1107 char *filename; 1108 ADDRESS *ctladdr; 1109 { 1110 register FILE *f; 1111 register int pid; 1112 1113 /* 1114 ** Fork so we can change permissions here. 1115 ** Note that we MUST use fork, not vfork, because of 1116 ** the complications of calling subroutines, etc. 1117 */ 1118 1119 DOFORK(fork); 1120 1121 if (pid < 0) 1122 return (EX_OSERR); 1123 else if (pid == 0) 1124 { 1125 /* child -- actually write to file */ 1126 struct stat stb; 1127 1128 (void) signal(SIGINT, SIG_DFL); 1129 (void) signal(SIGHUP, SIG_DFL); 1130 (void) signal(SIGTERM, SIG_DFL); 1131 umask(OldUmask); 1132 if (stat(filename, &stb) < 0) 1133 { 1134 errno = 0; 1135 stb.st_mode = 0666; 1136 } 1137 if (bitset(0111, stb.st_mode)) 1138 exit(EX_CANTCREAT); 1139 if (ctladdr == NULL) 1140 ctladdr = &CurEnv->e_from; 1141 if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 1142 { 1143 if (ctladdr->q_uid == 0) 1144 (void) setgid(DefGid); 1145 else 1146 (void) setgid(ctladdr->q_gid); 1147 } 1148 if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 1149 { 1150 if (ctladdr->q_uid == 0) 1151 (void) setuid(DefUid); 1152 else 1153 (void) setuid(ctladdr->q_uid); 1154 } 1155 f = dfopen(filename, "a"); 1156 if (f == NULL) 1157 exit(EX_CANTCREAT); 1158 1159 putfromline(f, ProgMailer); 1160 (*CurEnv->e_puthdr)(f, ProgMailer, CurEnv); 1161 putline("\n", f, ProgMailer); 1162 (*CurEnv->e_putbody)(f, ProgMailer, CurEnv); 1163 putline("\n", f, ProgMailer); 1164 (void) fclose(f); 1165 (void) fflush(stdout); 1166 1167 /* reset ISUID & ISGID bits for paranoid systems */ 1168 (void) chmod(filename, (int) stb.st_mode); 1169 exit(EX_OK); 1170 /*NOTREACHED*/ 1171 } 1172 else 1173 { 1174 /* parent -- wait for exit status */ 1175 int st; 1176 1177 st = waitfor(pid); 1178 if ((st & 0377) != 0) 1179 return (EX_UNAVAILABLE); 1180 else 1181 return ((st >> 8) & 0377); 1182 } 1183 } 1184 /* 1185 ** SENDALL -- actually send all the messages. 1186 ** 1187 ** Parameters: 1188 ** e -- the envelope to send. 1189 ** mode -- the delivery mode to use. If SM_DEFAULT, use 1190 ** the current SendMode. 1191 ** 1192 ** Returns: 1193 ** none. 1194 ** 1195 ** Side Effects: 1196 ** Scans the send lists and sends everything it finds. 1197 ** Delivers any appropriate error messages. 1198 ** If we are running in a non-interactive mode, takes the 1199 ** appropriate action. 1200 */ 1201 1202 sendall(e, mode) 1203 ENVELOPE *e; 1204 char mode; 1205 { 1206 register ADDRESS *q; 1207 bool oldverbose; 1208 int pid; 1209 1210 /* determine actual delivery mode */ 1211 if (mode == SM_DEFAULT) 1212 { 1213 extern int QueueLA; 1214 1215 if (getla() > QueueLA) 1216 mode = SM_QUEUE; 1217 else 1218 mode = SendMode; 1219 } 1220 1221 #ifdef DEBUG 1222 if (tTd(13, 1)) 1223 { 1224 printf("\nSENDALL: mode %c, sendqueue:\n", mode); 1225 printaddr(e->e_sendqueue, TRUE); 1226 } 1227 #endif DEBUG 1228 1229 /* 1230 ** Do any preprocessing necessary for the mode we are running. 1231 ** Check to make sure the hop count is reasonable. 1232 ** Delete sends to the sender in mailing lists. 1233 */ 1234 1235 CurEnv = e; 1236 1237 if (e->e_hopcount > MAXHOP) 1238 { 1239 syserr("sendall: too many hops (%d max)", MAXHOP); 1240 return; 1241 } 1242 1243 if (!MeToo) 1244 { 1245 extern ADDRESS *recipient(); 1246 1247 e->e_from.q_flags |= QDONTSEND; 1248 (void) recipient(&e->e_from, &e->e_sendqueue); 1249 } 1250 1251 # ifdef QUEUE 1252 if ((mode == SM_QUEUE || mode == SM_FORK || 1253 (mode != SM_VERIFY && SuperSafe)) && 1254 !bitset(EF_INQUEUE, e->e_flags)) 1255 queueup(e, TRUE, mode == SM_QUEUE); 1256 #endif QUEUE 1257 1258 oldverbose = Verbose; 1259 switch (mode) 1260 { 1261 case SM_VERIFY: 1262 Verbose = TRUE; 1263 break; 1264 1265 case SM_QUEUE: 1266 e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 1267 return; 1268 1269 case SM_FORK: 1270 if (e->e_xfp != NULL) 1271 (void) fflush(e->e_xfp); 1272 pid = fork(); 1273 if (pid < 0) 1274 { 1275 mode = SM_DELIVER; 1276 break; 1277 } 1278 else if (pid > 0) 1279 { 1280 /* be sure we leave the temp files to our child */ 1281 e->e_id = e->e_df = NULL; 1282 return; 1283 } 1284 1285 /* double fork to avoid zombies */ 1286 if (fork() > 0) 1287 exit(EX_OK); 1288 1289 /* be sure we are immune from the terminal */ 1290 disconnect(FALSE); 1291 1292 break; 1293 } 1294 1295 /* 1296 ** Run through the list and send everything. 1297 */ 1298 1299 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1300 { 1301 if (mode == SM_VERIFY) 1302 { 1303 e->e_to = q->q_paddr; 1304 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1305 message(Arpa_Info, "deliverable"); 1306 } 1307 else 1308 (void) deliver(e, q); 1309 } 1310 Verbose = oldverbose; 1311 1312 /* 1313 ** Now run through and check for errors. 1314 */ 1315 1316 if (mode == SM_VERIFY) 1317 return; 1318 1319 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1320 { 1321 register ADDRESS *qq; 1322 1323 # ifdef DEBUG 1324 if (tTd(13, 3)) 1325 { 1326 printf("Checking "); 1327 printaddr(q, FALSE); 1328 } 1329 # endif DEBUG 1330 1331 /* only send errors if the message failed */ 1332 if (!bitset(QBADADDR, q->q_flags)) 1333 continue; 1334 1335 /* we have an address that failed -- find the parent */ 1336 for (qq = q; qq != NULL; qq = qq->q_alias) 1337 { 1338 char obuf[MAXNAME + 6]; 1339 extern char *aliaslookup(); 1340 1341 /* we can only have owners for local addresses */ 1342 if (!bitnset(M_LOCAL, qq->q_mailer->m_flags)) 1343 continue; 1344 1345 /* see if the owner list exists */ 1346 (void) strcpy(obuf, "owner-"); 1347 if (strncmp(qq->q_user, "owner-", 6) == 0) 1348 (void) strcat(obuf, "owner"); 1349 else 1350 (void) strcat(obuf, qq->q_user); 1351 if (aliaslookup(obuf) == NULL) 1352 continue; 1353 1354 # ifdef DEBUG 1355 if (tTd(13, 4)) 1356 printf("Errors to %s\n", obuf); 1357 # endif DEBUG 1358 1359 /* owner list exists -- add it to the error queue */ 1360 sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue); 1361 ErrorMode = EM_MAIL; 1362 break; 1363 } 1364 1365 /* if we did not find an owner, send to the sender */ 1366 if (qq == NULL && bitset(QBADADDR, q->q_flags)) 1367 sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue); 1368 } 1369 1370 if (mode == SM_FORK) 1371 finis(); 1372 } 1373