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