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