1 # include <signal.h> 2 # include <errno.h> 3 # include "sendmail.h" 4 # include <sys/stat.h> 5 6 SCCSID(@(#)deliver.c 3.131 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 extern bool samefrom(); 993 char obuf[MAXLINE]; 994 register char *obp; 995 bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 996 997 if (bitset(M_LOCAL, m->m_flags) && fullsmtp) 998 fprintf(fp, "Return-Path: <%s>\n", e->e_from); 999 for (h = e->e_header; h != NULL; h = h->h_link) 1000 { 1001 register char *p; 1002 1003 if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 1004 continue; 1005 1006 p = h->h_value; 1007 if (bitset(H_DEFAULT, h->h_flags)) 1008 { 1009 /* macro expand value if generated internally */ 1010 expand(p, buf, &buf[sizeof buf], e); 1011 p = buf; 1012 } 1013 if (p == NULL || *p == '\0') 1014 continue; 1015 1016 if (bitset(H_FROM|H_RCPT, h->h_flags)) 1017 { 1018 /* address field */ 1019 bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 1020 1021 if (bitset(H_FROM, h->h_flags)) 1022 oldstyle = FALSE; 1023 commaize(h, p, fp, oldstyle, m); 1024 } 1025 else 1026 { 1027 /* vanilla header line */ 1028 (void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p); 1029 putline(obuf, fp, fullsmtp); 1030 } 1031 h->h_flags |= H_USED; 1032 } 1033 } 1034 /* 1035 ** COMMAIZE -- output a header field, making a comma-translated list. 1036 ** 1037 ** Parameters: 1038 ** h -- the header field to output. 1039 ** p -- the value to put in it. 1040 ** fp -- file to put it to. 1041 ** oldstyle -- TRUE if this is an old style header. 1042 ** m -- a pointer to the mailer descriptor. If NULL, 1043 ** don't transform the name at all. 1044 ** 1045 ** Returns: 1046 ** none. 1047 ** 1048 ** Side Effects: 1049 ** outputs "p" to file "fp". 1050 */ 1051 1052 commaize(h, p, fp, oldstyle, m) 1053 register HDR *h; 1054 register char *p; 1055 FILE *fp; 1056 bool oldstyle; 1057 register MAILER *m; 1058 { 1059 register char *obp; 1060 int opos; 1061 bool fullsmtp = FALSE; 1062 bool firstone = TRUE; 1063 char obuf[MAXLINE]; 1064 1065 /* 1066 ** Output the address list translated by the 1067 ** mailer and with commas. 1068 */ 1069 1070 # ifdef DEBUG 1071 if (tTd(14, 2)) 1072 printf("commaize(%s: %s)\n", h->h_field, p); 1073 # endif DEBUG 1074 1075 if (m != NULL && bitset(M_FULLSMTP, m->m_flags)) 1076 fullsmtp = TRUE; 1077 1078 obp = obuf; 1079 (void) sprintf(obp, "%s: ", capitalize(h->h_field)); 1080 opos = strlen(h->h_field) + 2; 1081 obp += opos; 1082 1083 /* 1084 ** Run through the list of values. 1085 */ 1086 1087 while (*p != '\0') 1088 { 1089 register char *name; 1090 char savechar; 1091 extern char *remotename(); 1092 extern char *DelimChar; /* defined in prescan */ 1093 1094 /* 1095 ** Find the end of the name. New style names 1096 ** end with a comma, old style names end with 1097 ** a space character. However, spaces do not 1098 ** necessarily delimit an old-style name -- at 1099 ** signs mean keep going. 1100 */ 1101 1102 /* find end of name */ 1103 while (isspace(*p) || *p == ',') 1104 p++; 1105 name = p; 1106 for (;;) 1107 { 1108 char *oldp; 1109 extern bool isatword(); 1110 1111 (void) prescan(p, oldstyle ? ' ' : ','); 1112 p = DelimChar; 1113 1114 /* look to see if we have an at sign */ 1115 oldp = p; 1116 while (*p != '\0' && isspace(*p)) 1117 p++; 1118 1119 if (*p != '@' && !isatword(p)) 1120 { 1121 p = oldp; 1122 break; 1123 } 1124 p += *p == '@' ? 1 : 2; 1125 while (*p != '\0' && isspace(*p)) 1126 p++; 1127 } 1128 /* at the end of one complete name */ 1129 1130 /* strip off trailing white space */ 1131 while (p >= name && (isspace(*p) || *p == ',' || *p == '\0')) 1132 p--; 1133 if (++p == name) 1134 continue; 1135 savechar = *p; 1136 *p = '\0'; 1137 1138 /* translate the name to be relative */ 1139 if (m != NULL) 1140 name = remotename(name, m, bitset(H_FROM, h->h_flags)); 1141 if (*name == '\0') 1142 { 1143 *p = savechar; 1144 continue; 1145 } 1146 1147 /* output the name with nice formatting */ 1148 opos += qstrlen(name); 1149 if (!firstone) 1150 opos += 2; 1151 if (opos > 78 && !firstone) 1152 { 1153 (void) sprintf(obp, ",\n"); 1154 putline(obuf, fp, fullsmtp); 1155 obp = obuf; 1156 (void) sprintf(obp, " "); 1157 obp += strlen(obp); 1158 opos = 8 + strlen(name); 1159 } 1160 else if (!firstone) 1161 { 1162 (void) sprintf(obp, ", "); 1163 obp += 2; 1164 } 1165 1166 /* strip off quote bits as we output */ 1167 while (*name != '\0') 1168 { 1169 if (bitset(0200, *name)) 1170 *obp++ = '\\'; 1171 *obp++ = *name++ & ~0200; 1172 } 1173 firstone = FALSE; 1174 *p = savechar; 1175 } 1176 (void) strcpy(obp, "\n"); 1177 putline(obuf, fp, fullsmtp); 1178 } 1179 /* 1180 ** PUTBODY -- put the body of a message. 1181 ** 1182 ** Parameters: 1183 ** fp -- file to output onto. 1184 ** m -- a mailer descriptor. 1185 ** xdot -- if set, use SMTP hidden dot algorithm. 1186 ** 1187 ** Returns: 1188 ** none. 1189 ** 1190 ** Side Effects: 1191 ** The message is written onto fp. 1192 */ 1193 1194 putbody(fp, m, xdot) 1195 FILE *fp; 1196 struct mailer *m; 1197 bool xdot; 1198 { 1199 char buf[MAXLINE + 1]; 1200 bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 1201 1202 /* 1203 ** Output the body of the message 1204 */ 1205 1206 #ifdef lint 1207 /* m will be needed later for complete smtp emulation */ 1208 if (m == NULL) 1209 return; 1210 #endif lint 1211 1212 if (TempFile != NULL) 1213 { 1214 rewind(TempFile); 1215 buf[0] = '.'; 1216 while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL) 1217 putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp); 1218 1219 if (ferror(TempFile)) 1220 { 1221 syserr("putbody: read error"); 1222 ExitStat = EX_IOERR; 1223 } 1224 } 1225 1226 (void) fflush(fp); 1227 if (ferror(fp) && errno != EPIPE) 1228 { 1229 syserr("putbody: write error"); 1230 ExitStat = EX_IOERR; 1231 } 1232 errno = 0; 1233 } 1234 /* 1235 ** ISATWORD -- tell if the word we are pointing to is "at". 1236 ** 1237 ** Parameters: 1238 ** p -- word to check. 1239 ** 1240 ** Returns: 1241 ** TRUE -- if p is the word at. 1242 ** FALSE -- otherwise. 1243 ** 1244 ** Side Effects: 1245 ** none. 1246 */ 1247 1248 bool 1249 isatword(p) 1250 register char *p; 1251 { 1252 extern char lower(); 1253 1254 if (lower(p[0]) == 'a' && lower(p[1]) == 't' && 1255 p[2] != '\0' && isspace(p[2])) 1256 return (TRUE); 1257 return (FALSE); 1258 } 1259 /* 1260 ** SAMEFROM -- tell if two text addresses represent the same from address. 1261 ** 1262 ** Parameters: 1263 ** ifrom -- internally generated form of from address. 1264 ** efrom -- external form of from address. 1265 ** 1266 ** Returns: 1267 ** TRUE -- if they convey the same info. 1268 ** FALSE -- if any information has been lost. 1269 ** 1270 ** Side Effects: 1271 ** none. 1272 */ 1273 1274 bool 1275 samefrom(ifrom, efrom) 1276 char *ifrom; 1277 char *efrom; 1278 { 1279 register char *p; 1280 char buf[MAXNAME + 4]; 1281 1282 # ifdef DEBUG 1283 if (tTd(3, 8)) 1284 printf("samefrom(%s,%s)-->", ifrom, efrom); 1285 # endif DEBUG 1286 if (strcmp(ifrom, efrom) == 0) 1287 goto success; 1288 p = index(ifrom, '@'); 1289 if (p == NULL) 1290 goto failure; 1291 *p = '\0'; 1292 (void) strcpy(buf, ifrom); 1293 (void) strcat(buf, " at "); 1294 *p++ = '@'; 1295 (void) strcat(buf, p); 1296 if (strcmp(buf, efrom) == 0) 1297 goto success; 1298 1299 failure: 1300 # ifdef DEBUG 1301 if (tTd(3, 8)) 1302 printf("FALSE\n"); 1303 # endif DEBUG 1304 return (FALSE); 1305 1306 success: 1307 # ifdef DEBUG 1308 if (tTd(3, 8)) 1309 printf("TRUE\n"); 1310 # endif DEBUG 1311 return (TRUE); 1312 } 1313 /* 1314 ** MAILFILE -- Send a message to a file. 1315 ** 1316 ** If the file has the setuid/setgid bits set, but NO execute 1317 ** bits, sendmail will try to become the owner of that file 1318 ** rather than the real user. Obviously, this only works if 1319 ** sendmail runs as root. 1320 ** 1321 ** Parameters: 1322 ** filename -- the name of the file to send to. 1323 ** ctladdr -- the controlling address header -- includes 1324 ** the userid/groupid to be when sending. 1325 ** 1326 ** Returns: 1327 ** The exit code associated with the operation. 1328 ** 1329 ** Side Effects: 1330 ** none. 1331 */ 1332 1333 mailfile(filename, ctladdr) 1334 char *filename; 1335 ADDRESS *ctladdr; 1336 { 1337 register FILE *f; 1338 register int pid; 1339 1340 /* 1341 ** Fork so we can change permissions here. 1342 ** Note that we MUST use fork, not vfork, because of 1343 ** the complications of calling subroutines, etc. 1344 */ 1345 1346 DOFORK(fork); 1347 1348 if (pid < 0) 1349 return (EX_OSERR); 1350 else if (pid == 0) 1351 { 1352 /* child -- actually write to file */ 1353 struct stat stb; 1354 1355 (void) signal(SIGINT, SIG_DFL); 1356 (void) signal(SIGHUP, SIG_DFL); 1357 (void) signal(SIGTERM, SIG_DFL); 1358 umask(OldUmask); 1359 if (stat(filename, &stb) < 0) 1360 stb.st_mode = 0666; 1361 if (bitset(0111, stb.st_mode)) 1362 exit(EX_CANTCREAT); 1363 if (ctladdr == NULL) 1364 ctladdr = &CurEnv->e_from; 1365 if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 1366 { 1367 if (ctladdr->q_uid == 0) 1368 (void) setgid(DefGid); 1369 else 1370 (void) setgid(ctladdr->q_gid); 1371 } 1372 if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 1373 { 1374 if (ctladdr->q_uid == 0) 1375 (void) setuid(DefUid); 1376 else 1377 (void) setuid(ctladdr->q_uid); 1378 } 1379 f = dfopen(filename, "a"); 1380 if (f == NULL) 1381 exit(EX_CANTCREAT); 1382 1383 putfromline(f, Mailer[1]); 1384 (*CurEnv->e_puthdr)(f, Mailer[1], CurEnv); 1385 fputs("\n", f); 1386 (*CurEnv->e_putbody)(f, Mailer[1], FALSE); 1387 fputs("\n", f); 1388 (void) fclose(f); 1389 (void) fflush(stdout); 1390 1391 /* reset ISUID & ISGID bits for paranoid systems */ 1392 (void) chmod(filename, (int) stb.st_mode); 1393 exit(EX_OK); 1394 /*NOTREACHED*/ 1395 } 1396 else 1397 { 1398 /* parent -- wait for exit status */ 1399 register int i; 1400 auto int stat; 1401 1402 while ((i = wait(&stat)) != pid) 1403 { 1404 if (i < 0) 1405 { 1406 stat = EX_OSERR << 8; 1407 break; 1408 } 1409 } 1410 if ((stat & 0377) != 0) 1411 stat = EX_UNAVAILABLE << 8; 1412 return ((stat >> 8) & 0377); 1413 } 1414 } 1415 /* 1416 ** SENDALL -- actually send all the messages. 1417 ** 1418 ** Parameters: 1419 ** e -- the envelope to send. 1420 ** mode -- the delivery mode to use. 1421 ** 1422 ** Returns: 1423 ** none. 1424 ** 1425 ** Side Effects: 1426 ** Scans the send lists and sends everything it finds. 1427 ** Delivers any appropriate error messages. 1428 ** If we are running in a non-interactive mode, takes the 1429 ** appropriate action. 1430 */ 1431 1432 sendall(e, mode) 1433 ENVELOPE *e; 1434 char mode; 1435 { 1436 register ADDRESS *q; 1437 bool oldverbose; 1438 int pid; 1439 1440 # ifdef DEBUG 1441 if (tTd(13, 1)) 1442 { 1443 printf("\nSENDALL: mode %c, sendqueue:\n", mode); 1444 printaddr(e->e_sendqueue, TRUE); 1445 } 1446 # endif DEBUG 1447 1448 /* 1449 ** Do any preprocessing necessary for the mode we are running. 1450 */ 1451 1452 # ifdef QUEUE 1453 if (mode == SM_QUEUE) 1454 { 1455 register ADDRESS *q; 1456 1457 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1458 { 1459 if (!bitset(QDONTSEND, q->q_flags)) 1460 { 1461 e->e_to = q->q_paddr; 1462 message(Arpa_Info, "queued"); 1463 if (LogLevel > 4) 1464 logdelivery("queued"); 1465 } 1466 e->e_to = NULL; 1467 } 1468 } 1469 if ((mode == SM_QUEUE || mode == SM_FORK || 1470 (mode != SM_VERIFY && SuperSafe)) && 1471 !bitset(EF_INQUEUE, e->e_flags)) 1472 queueup(e, TRUE); 1473 #endif QUEUE 1474 1475 oldverbose = Verbose; 1476 switch (mode) 1477 { 1478 case SM_VERIFY: 1479 Verbose = TRUE; 1480 break; 1481 1482 case SM_QUEUE: 1483 e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 1484 return; 1485 1486 case SM_FORK: 1487 if (Xscript != NULL) 1488 (void) fflush(Xscript); 1489 pid = fork(); 1490 if (pid < 0) 1491 { 1492 mode = SM_DELIVER; 1493 break; 1494 } 1495 else if (pid > 0) 1496 { 1497 /* be sure we leave the temp files to our child */ 1498 e->e_id = e->e_df = NULL; 1499 return; 1500 } 1501 1502 /* double fork to avoid zombies */ 1503 if (fork() > 0) 1504 exit(EX_OK); 1505 1506 /* be sure we are immune from the terminal */ 1507 disconnect(TRUE); 1508 1509 break; 1510 } 1511 1512 /* 1513 ** Run through the list and send everything. 1514 */ 1515 1516 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1517 { 1518 if (mode == SM_VERIFY) 1519 { 1520 e->e_to = q->q_paddr; 1521 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1522 message(Arpa_Info, "deliverable"); 1523 } 1524 else 1525 (void) deliver(q); 1526 } 1527 Verbose = oldverbose; 1528 1529 /* 1530 ** Now run through and check for errors. 1531 */ 1532 1533 if (mode == SM_VERIFY) 1534 return; 1535 1536 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1537 { 1538 register ADDRESS *qq; 1539 1540 # ifdef DEBUG 1541 if (tTd(13, 3)) 1542 { 1543 printf("Checking "); 1544 printaddr(q, FALSE); 1545 } 1546 # endif DEBUG 1547 1548 /* only send errors if the message failed */ 1549 if (!bitset(QBADADDR, q->q_flags)) 1550 continue; 1551 1552 /* we have an address that failed -- find the parent */ 1553 for (qq = q; qq != NULL; qq = qq->q_alias) 1554 { 1555 char obuf[MAXNAME + 6]; 1556 extern char *aliaslookup(); 1557 1558 /* we can only have owners for local addresses */ 1559 if (!bitset(M_LOCAL, qq->q_mailer->m_flags)) 1560 continue; 1561 1562 /* see if the owner list exists */ 1563 (void) strcpy(obuf, "owner-"); 1564 if (strncmp(qq->q_user, "owner-", 6) == 0) 1565 (void) strcat(obuf, "owner"); 1566 else 1567 (void) strcat(obuf, qq->q_user); 1568 if (aliaslookup(obuf) == NULL) 1569 continue; 1570 1571 # ifdef DEBUG 1572 if (tTd(13, 4)) 1573 printf("Errors to %s\n", obuf); 1574 # endif DEBUG 1575 1576 /* owner list exists -- add it to the error queue */ 1577 qq->q_flags &= ~QPRIMARY; 1578 sendto(obuf, qq, &e->e_errorqueue); 1579 MailBack = TRUE; 1580 break; 1581 } 1582 1583 /* if we did not find an owner, send to the sender */ 1584 if (qq == NULL && bitset(QBADADDR, q->q_flags)) 1585 sendto(e->e_from.q_paddr, qq, &e->e_errorqueue); 1586 } 1587 1588 if (mode == SM_FORK) 1589 finis(); 1590 } 1591 /* 1592 ** CHECKERRORS -- check a queue of addresses and process errors. 1593 ** 1594 ** Parameters: 1595 ** e -- the envelope to check. 1596 ** 1597 ** Returns: 1598 ** none. 1599 ** 1600 ** Side Effects: 1601 ** Arranges to queue all tempfailed messages in q 1602 ** or deliver error responses. 1603 */ 1604 1605 checkerrors(e) 1606 register ENVELOPE *e; 1607 { 1608 register ADDRESS *q; 1609 1610 # ifdef DEBUG 1611 if (tTd(4, 1)) 1612 { 1613 printf("\ncheckerrors: e_flags %o, errorqueue:\n", e->e_flags); 1614 printaddr(e->e_errorqueue, TRUE); 1615 } 1616 # endif DEBUG 1617 1618 /* mail back the transcript on errors */ 1619 if (bitset(EF_FATALERRS, e->e_flags)) 1620 savemail(e); 1621 } 1622