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