1 # include <signal.h> 2 # include <errno.h> 3 # include "sendmail.h" 4 # include <sys/stat.h> 5 6 SCCSID(@(#)deliver.c 3.111 09/08/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 if (i < 0 && errno == EINTR) 555 continue; 556 } while (i >= 0 && i != pid); 557 if (i < 0) 558 { 559 syserr("wait"); 560 return (-1); 561 } 562 563 /* see if it died a horrid death */ 564 if ((st & 0377) != 0) 565 { 566 syserr("%s: stat %o", name, st); 567 ExitStat = EX_UNAVAILABLE; 568 return (-1); 569 } 570 571 /* normal death -- return status */ 572 i = (st >> 8) & 0377; 573 return (i); 574 } 575 /* 576 ** OPENMAILER -- open connection to mailer. 577 ** 578 ** Parameters: 579 ** m -- mailer descriptor. 580 ** pvp -- parameter vector to pass to mailer. 581 ** ctladdr -- controlling address for user. 582 ** clever -- create a full duplex connection. 583 ** pmfile -- pointer to mfile (to mailer) connection. 584 ** prfile -- pointer to rfile (from mailer) connection. 585 ** 586 ** Returns: 587 ** pid of mailer ( > 0 ). 588 ** -1 on error. 589 ** zero on an IPC connection. 590 ** 591 ** Side Effects: 592 ** creates a mailer in a subprocess. 593 */ 594 595 openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 596 struct mailer *m; 597 char **pvp; 598 ADDRESS *ctladdr; 599 bool clever; 600 FILE **pmfile; 601 FILE **prfile; 602 { 603 int pid; 604 int mpvect[2]; 605 int rpvect[2]; 606 FILE *mfile; 607 FILE *rfile; 608 extern FILE *fdopen(); 609 610 # ifdef DEBUG 611 if (tTd(11, 1)) 612 { 613 printf("openmailer:\n"); 614 printav(pvp); 615 } 616 # endif DEBUG 617 errno = 0; 618 619 # ifdef DAEMON 620 /* 621 ** Deal with the special case of mail handled through an IPC 622 ** connection. 623 ** In this case we don't actually fork. We must be 624 ** running SMTP for this to work. We will return a 625 ** zero pid to indicate that we are running IPC. 626 */ 627 628 if (strcmp(m->m_mailer, "[IPC]") == 0) 629 { 630 register int i; 631 register u_short port; 632 633 if (!clever) 634 syserr("non-clever IPC"); 635 if (pvp[2] != NULL) 636 port = atoi(pvp[2]); 637 else 638 port = 0; 639 i = makeconnection(pvp[1], port, pmfile, prfile); 640 if (i != EX_OK) 641 { 642 ExitStat = i; 643 return (-1); 644 } 645 else 646 return (0); 647 } 648 # endif DAEMON 649 650 /* create a pipe to shove the mail through */ 651 if (pipe(mpvect) < 0) 652 { 653 syserr("pipe (to mailer)"); 654 return (-1); 655 } 656 657 # ifdef SMTP 658 /* if this mailer speaks smtp, create a return pipe */ 659 if (clever && pipe(rpvect) < 0) 660 { 661 syserr("pipe (from mailer)"); 662 (void) close(mpvect[0]); 663 (void) close(mpvect[1]); 664 return (-1); 665 } 666 # endif SMTP 667 668 /* 669 ** Actually fork the mailer process. 670 ** DOFORK is clever about retrying. 671 */ 672 673 (void) fflush(Xscript); /* for debugging */ 674 DOFORK(XFORK); 675 /* pid is set by DOFORK */ 676 if (pid < 0) 677 { 678 /* failure */ 679 syserr("Cannot fork"); 680 (void) close(mpvect[0]); 681 (void) close(mpvect[1]); 682 if (clever) 683 { 684 (void) close(rpvect[0]); 685 (void) close(rpvect[1]); 686 } 687 return (-1); 688 } 689 else if (pid == 0) 690 { 691 /* child -- set up input & exec mailer */ 692 /* make diagnostic output be standard output */ 693 (void) signal(SIGINT, SIG_IGN); 694 (void) signal(SIGHUP, SIG_IGN); 695 (void) signal(SIGTERM, SIG_DFL); 696 697 /* arrange to filter standard & diag output of command */ 698 if (clever) 699 { 700 (void) close(rpvect[0]); 701 (void) close(1); 702 (void) dup(rpvect[1]); 703 (void) close(rpvect[1]); 704 } 705 else if (OutChannel != stdout) 706 { 707 (void) close(1); 708 (void) dup(fileno(OutChannel)); 709 } 710 (void) close(2); 711 (void) dup(1); 712 713 /* arrange to get standard input */ 714 (void) close(mpvect[1]); 715 (void) close(0); 716 if (dup(mpvect[0]) < 0) 717 { 718 syserr("Cannot dup to zero!"); 719 _exit(EX_OSERR); 720 } 721 (void) close(mpvect[0]); 722 if (!bitset(M_RESTR, m->m_flags)) 723 { 724 if (ctladdr->q_uid == 0) 725 { 726 (void) setgid(DefGid); 727 (void) setuid(DefUid); 728 } 729 else 730 { 731 (void) setgid(ctladdr->q_gid); 732 (void) setuid(ctladdr->q_uid); 733 } 734 } 735 # ifndef VFORK 736 /* 737 ** We have to be careful with vfork - we can't mung up the 738 ** memory but we don't want the mailer to inherit any extra 739 ** open files. Chances are the mailer won't 740 ** care about an extra file, but then again you never know. 741 ** Actually, we would like to close(fileno(pwf)), but it's 742 ** declared static so we can't. But if we fclose(pwf), which 743 ** is what endpwent does, it closes it in the parent too and 744 ** the next getpwnam will be slower. If you have a weird 745 ** mailer that chokes on the extra file you should do the 746 ** endpwent(). 747 ** 748 ** Similar comments apply to log. However, openlog is 749 ** clever enough to set the FIOCLEX mode on the file, 750 ** so it will be closed automatically on the exec. 751 */ 752 753 endpwent(); 754 # ifdef LOG 755 closelog(); 756 # endif LOG 757 # endif VFORK 758 759 /* try to execute the mailer */ 760 execv(m->m_mailer, pvp); 761 762 /* syserr fails because log is closed */ 763 /* syserr("Cannot exec %s", m->m_mailer); */ 764 printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 765 (void) fflush(stdout); 766 _exit(EX_UNAVAILABLE); 767 } 768 769 /* 770 ** Set up return value. 771 */ 772 773 (void) close(mpvect[0]); 774 mfile = fdopen(mpvect[1], "w"); 775 if (clever) 776 { 777 (void) close(rpvect[1]); 778 rfile = fdopen(rpvect[0], "r"); 779 } 780 781 *pmfile = mfile; 782 *prfile = rfile; 783 784 return (pid); 785 } 786 /* 787 ** GIVERESPONSE -- Interpret an error response from a mailer 788 ** 789 ** Parameters: 790 ** stat -- the status code from the mailer (high byte 791 ** only; core dumps must have been taken care of 792 ** already). 793 ** force -- if set, force an error message output, even 794 ** if the mailer seems to like to print its own 795 ** messages. 796 ** m -- the mailer descriptor for this mailer. 797 ** 798 ** Returns: 799 ** none. 800 ** 801 ** Side Effects: 802 ** Errors may be incremented. 803 ** ExitStat may be set. 804 */ 805 806 giveresponse(stat, force, m) 807 int stat; 808 bool force; 809 register struct mailer *m; 810 { 811 register char *statmsg; 812 extern char *SysExMsg[]; 813 register int i; 814 extern int N_SysEx; 815 char buf[30]; 816 817 /* 818 ** Compute status message from code. 819 */ 820 821 i = stat - EX__BASE; 822 if (i < 0 || i > N_SysEx) 823 statmsg = NULL; 824 else 825 statmsg = SysExMsg[i]; 826 if (stat == 0) 827 { 828 statmsg = "250 sent"; 829 message(Arpa_Info, &statmsg[4]); 830 } 831 else if (stat == EX_TEMPFAIL) 832 { 833 message(Arpa_Info, "deferred"); 834 } 835 else 836 { 837 Errors++; 838 FatalErrors = TRUE; 839 if (statmsg == NULL && m->m_badstat != 0) 840 { 841 stat = m->m_badstat; 842 i = stat - EX__BASE; 843 # ifdef DEBUG 844 if (i < 0 || i >= N_SysEx) 845 syserr("Bad m_badstat %d", stat); 846 else 847 # endif DEBUG 848 statmsg = SysExMsg[i]; 849 } 850 if (statmsg == NULL) 851 usrerr("unknown mailer response %d", stat); 852 else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 853 usrerr(statmsg); 854 else 855 fprintf(Xscript, "%s\n", &statmsg[4]); 856 } 857 858 /* 859 ** Final cleanup. 860 ** Log a record of the transaction. Compute the new 861 ** ExitStat -- if we already had an error, stick with 862 ** that. 863 */ 864 865 if (statmsg == NULL) 866 { 867 (void) sprintf(buf, "554 error %d", stat); 868 statmsg = buf; 869 } 870 871 # ifdef LOG 872 if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2)) 873 { 874 extern char *pintvl(); 875 876 syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id, 877 CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), 878 &statmsg[4]); 879 } 880 # endif LOG 881 if (stat != EX_TEMPFAIL) 882 setstat(stat); 883 } 884 /* 885 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 886 ** 887 ** This can be made an arbitrary message separator by changing $l 888 ** 889 ** One of the ugliest hacks seen by human eyes is 890 ** contained herein: UUCP wants those stupid 891 ** "remote from <host>" lines. Why oh why does a 892 ** well-meaning programmer such as myself have to 893 ** deal with this kind of antique garbage???? 894 ** 895 ** Parameters: 896 ** fp -- the file to output to. 897 ** m -- the mailer describing this entry. 898 ** 899 ** Returns: 900 ** none 901 ** 902 ** Side Effects: 903 ** outputs some text to fp. 904 */ 905 906 putfromline(fp, m) 907 register FILE *fp; 908 register MAILER *m; 909 { 910 char buf[MAXLINE]; 911 912 if (bitset(M_NHDR, m->m_flags)) 913 return; 914 915 # ifdef UGLYUUCP 916 if (bitset(M_UGLYUUCP, m->m_flags)) 917 { 918 extern char *macvalue(); 919 char *sys = macvalue('g'); 920 char *bang = index(sys, '!'); 921 922 if (bang == NULL) 923 syserr("No ! in UUCP! (%s)", sys); 924 else 925 *bang = '\0'; 926 expand("From $f $d remote from $g\n", buf, 927 &buf[sizeof buf - 1], CurEnv); 928 *bang = '!'; 929 } 930 else 931 # endif UGLYUUCP 932 expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv); 933 putline(buf, fp, bitset(M_FULLSMTP, m->m_flags)); 934 } 935 /* 936 ** PUTHEADER -- put the header part of a message from the in-core copy 937 ** 938 ** Parameters: 939 ** fp -- file to put it on. 940 ** m -- mailer to use. 941 ** e -- envelope to use. 942 ** 943 ** Returns: 944 ** none. 945 ** 946 ** Side Effects: 947 ** none. 948 */ 949 950 putheader(fp, m, e) 951 register FILE *fp; 952 register struct mailer *m; 953 register ENVELOPE *e; 954 { 955 char buf[BUFSIZ]; 956 register HDR *h; 957 extern char *arpadate(); 958 extern char *capitalize(); 959 extern char *hvalue(); 960 extern bool samefrom(); 961 char obuf[MAXLINE]; 962 register char *obp; 963 bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 964 965 for (h = e->e_header; h != NULL; h = h->h_link) 966 { 967 register char *p; 968 969 if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 970 continue; 971 972 p = h->h_value; 973 if (bitset(H_DEFAULT, h->h_flags)) 974 { 975 /* macro expand value if generated internally */ 976 expand(p, buf, &buf[sizeof buf], e); 977 p = buf; 978 } 979 if (p == NULL || *p == '\0') 980 continue; 981 982 if (bitset(H_FROM|H_RCPT, h->h_flags)) 983 { 984 /* address field */ 985 bool oldstyle = e->e_oldstyle; 986 987 if (bitset(H_FROM, h->h_flags)) 988 oldstyle = FALSE; 989 commaize(h, p, fp, oldstyle, m); 990 } 991 else 992 { 993 /* vanilla header line */ 994 (void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p); 995 putline(obuf, fp, fullsmtp); 996 } 997 h->h_flags |= H_USED; 998 } 999 } 1000 /* 1001 ** COMMAIZE -- output a header field, making a comma-translated list. 1002 ** 1003 ** Parameters: 1004 ** h -- the header field to output. 1005 ** p -- the value to put in it. 1006 ** fp -- file to put it to. 1007 ** oldstyle -- TRUE if this is an old style header. 1008 ** m -- a pointer to the mailer descriptor. 1009 ** 1010 ** Returns: 1011 ** none. 1012 ** 1013 ** Side Effects: 1014 ** outputs "p" to file "fp". 1015 */ 1016 1017 commaize(h, p, fp, oldstyle, m) 1018 register HDR *h; 1019 register char *p; 1020 FILE *fp; 1021 bool oldstyle; 1022 register MAILER *m; 1023 { 1024 register char *obp; 1025 int opos; 1026 bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 1027 bool firstone = TRUE; 1028 char obuf[MAXLINE]; 1029 1030 /* 1031 ** Output the address list translated by the 1032 ** mailer and with commas. 1033 */ 1034 1035 # ifdef DEBUG 1036 if (tTd(14, 2)) 1037 printf("commaize(%s: %s)\n", h->h_field, p); 1038 # endif DEBUG 1039 1040 obp = obuf; 1041 (void) sprintf(obp, "%s: ", capitalize(h->h_field)); 1042 opos = strlen(h->h_field) + 2; 1043 obp += opos; 1044 1045 /* 1046 ** Run through the list of values. 1047 */ 1048 1049 while (*p != '\0') 1050 { 1051 register char *name; 1052 char savechar; 1053 extern char *remotename(); 1054 extern char *DelimChar; /* defined in prescan */ 1055 1056 /* 1057 ** Find the end of the name. New style names 1058 ** end with a comma, old style names end with 1059 ** a space character. However, spaces do not 1060 ** necessarily delimit an old-style name -- at 1061 ** signs mean keep going. 1062 */ 1063 1064 /* find end of name */ 1065 while (isspace(*p) || *p == ',') 1066 p++; 1067 name = p; 1068 for (;;) 1069 { 1070 char *oldp; 1071 extern bool isatword(); 1072 1073 (void) prescan(p, oldstyle ? ' ' : ','); 1074 p = DelimChar; 1075 1076 /* look to see if we have an at sign */ 1077 oldp = p; 1078 while (*p != '\0' && isspace(*p)) 1079 p++; 1080 1081 if (*p != '@' && !isatword(p)) 1082 { 1083 p = oldp; 1084 break; 1085 } 1086 p += *p == '@' ? 1 : 2; 1087 while (*p != '\0' && isspace(*p)) 1088 p++; 1089 } 1090 /* at the end of one complete name */ 1091 1092 /* strip off trailing white space */ 1093 while (p >= name && (isspace(*p) || *p == ',' || *p == '\0')) 1094 p--; 1095 if (++p == name) 1096 continue; 1097 savechar = *p; 1098 *p = '\0'; 1099 1100 /* translate the name to be relative */ 1101 name = remotename(name, m, bitset(H_FROM, h->h_flags)); 1102 if (*name == '\0') 1103 { 1104 *p = savechar; 1105 continue; 1106 } 1107 1108 /* output the name with nice formatting */ 1109 opos += strlen(name); 1110 if (!firstone) 1111 opos += 2; 1112 if (opos > 78 && !firstone) 1113 { 1114 (void) sprintf(obp, ",\n"); 1115 putline(obuf, fp, fullsmtp); 1116 obp = obuf; 1117 (void) sprintf(obp, " "); 1118 obp += strlen(obp); 1119 opos = 8 + strlen(name); 1120 } 1121 else if (!firstone) 1122 { 1123 (void) sprintf(obp, ", "); 1124 obp += 2; 1125 } 1126 (void) sprintf(obp, "%s", name); 1127 obp += strlen(obp); 1128 firstone = FALSE; 1129 *p = savechar; 1130 } 1131 (void) strcpy(obp, "\n"); 1132 putline(obuf, fp, fullsmtp); 1133 } 1134 /* 1135 ** PUTBODY -- put the body of a message. 1136 ** 1137 ** Parameters: 1138 ** fp -- file to output onto. 1139 ** m -- a mailer descriptor. 1140 ** xdot -- if set, use SMTP hidden dot algorithm. 1141 ** 1142 ** Returns: 1143 ** none. 1144 ** 1145 ** Side Effects: 1146 ** The message is written onto fp. 1147 */ 1148 1149 putbody(fp, m, xdot) 1150 FILE *fp; 1151 struct mailer *m; 1152 bool xdot; 1153 { 1154 char buf[MAXLINE + 1]; 1155 bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 1156 1157 /* 1158 ** Output the body of the message 1159 */ 1160 1161 #ifdef lint 1162 /* m will be needed later for complete smtp emulation */ 1163 if (m == NULL) 1164 return; 1165 #endif lint 1166 1167 if (TempFile != NULL) 1168 { 1169 rewind(TempFile); 1170 buf[0] = '.'; 1171 while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL) 1172 putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp); 1173 1174 if (ferror(TempFile)) 1175 { 1176 syserr("putbody: read error"); 1177 ExitStat = EX_IOERR; 1178 } 1179 } 1180 1181 (void) fflush(fp); 1182 if (ferror(fp) && errno != EPIPE) 1183 { 1184 syserr("putbody: write error"); 1185 ExitStat = EX_IOERR; 1186 } 1187 errno = 0; 1188 } 1189 /* 1190 ** ISATWORD -- tell if the word we are pointing to is "at". 1191 ** 1192 ** Parameters: 1193 ** p -- word to check. 1194 ** 1195 ** Returns: 1196 ** TRUE -- if p is the word at. 1197 ** FALSE -- otherwise. 1198 ** 1199 ** Side Effects: 1200 ** none. 1201 */ 1202 1203 bool 1204 isatword(p) 1205 register char *p; 1206 { 1207 extern char lower(); 1208 1209 if (lower(p[0]) == 'a' && lower(p[1]) == 't' && 1210 p[2] != '\0' && isspace(p[2])) 1211 return (TRUE); 1212 return (FALSE); 1213 } 1214 /* 1215 ** SAMEFROM -- tell if two text addresses represent the same from address. 1216 ** 1217 ** Parameters: 1218 ** ifrom -- internally generated form of from address. 1219 ** efrom -- external form of from address. 1220 ** 1221 ** Returns: 1222 ** TRUE -- if they convey the same info. 1223 ** FALSE -- if any information has been lost. 1224 ** 1225 ** Side Effects: 1226 ** none. 1227 */ 1228 1229 bool 1230 samefrom(ifrom, efrom) 1231 char *ifrom; 1232 char *efrom; 1233 { 1234 register char *p; 1235 char buf[MAXNAME + 4]; 1236 1237 # ifdef DEBUG 1238 if (tTd(3, 8)) 1239 printf("samefrom(%s,%s)-->", ifrom, efrom); 1240 # endif DEBUG 1241 if (strcmp(ifrom, efrom) == 0) 1242 goto success; 1243 p = index(ifrom, '@'); 1244 if (p == NULL) 1245 goto failure; 1246 *p = '\0'; 1247 (void) strcpy(buf, ifrom); 1248 (void) strcat(buf, " at "); 1249 *p++ = '@'; 1250 (void) strcat(buf, p); 1251 if (strcmp(buf, efrom) == 0) 1252 goto success; 1253 1254 failure: 1255 # ifdef DEBUG 1256 if (tTd(3, 8)) 1257 printf("FALSE\n"); 1258 # endif DEBUG 1259 return (FALSE); 1260 1261 success: 1262 # ifdef DEBUG 1263 if (tTd(3, 8)) 1264 printf("TRUE\n"); 1265 # endif DEBUG 1266 return (TRUE); 1267 } 1268 /* 1269 ** MAILFILE -- Send a message to a file. 1270 ** 1271 ** If the file has the setuid/setgid bits set, but NO execute 1272 ** bits, sendmail will try to become the owner of that file 1273 ** rather than the real user. Obviously, this only works if 1274 ** sendmail runs as root. 1275 ** 1276 ** Parameters: 1277 ** filename -- the name of the file to send to. 1278 ** ctladdr -- the controlling address header -- includes 1279 ** the userid/groupid to be when sending. 1280 ** 1281 ** Returns: 1282 ** The exit code associated with the operation. 1283 ** 1284 ** Side Effects: 1285 ** none. 1286 */ 1287 1288 mailfile(filename, ctladdr) 1289 char *filename; 1290 ADDRESS *ctladdr; 1291 { 1292 register FILE *f; 1293 register int pid; 1294 1295 /* 1296 ** Fork so we can change permissions here. 1297 ** Note that we MUST use fork, not vfork, because of 1298 ** the complications of calling subroutines, etc. 1299 */ 1300 1301 DOFORK(fork); 1302 1303 if (pid < 0) 1304 return (EX_OSERR); 1305 else if (pid == 0) 1306 { 1307 /* child -- actually write to file */ 1308 struct stat stb; 1309 1310 (void) signal(SIGINT, SIG_DFL); 1311 (void) signal(SIGHUP, SIG_DFL); 1312 (void) signal(SIGTERM, SIG_DFL); 1313 umask(OldUmask); 1314 if (stat(filename, &stb) < 0) 1315 stb.st_mode = 0666; 1316 if (bitset(0111, stb.st_mode)) 1317 exit(EX_CANTCREAT); 1318 if (ctladdr == NULL) 1319 ctladdr = &CurEnv->e_from; 1320 if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 1321 { 1322 if (ctladdr->q_uid == 0) 1323 (void) setgid(DefGid); 1324 else 1325 (void) setgid(ctladdr->q_gid); 1326 } 1327 if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 1328 { 1329 if (ctladdr->q_uid == 0) 1330 (void) setuid(DefUid); 1331 else 1332 (void) setuid(ctladdr->q_uid); 1333 } 1334 f = dfopen(filename, "a"); 1335 if (f == NULL) 1336 exit(EX_CANTCREAT); 1337 1338 putfromline(f, Mailer[1]); 1339 (*CurEnv->e_puthdr)(f, Mailer[1], CurEnv); 1340 fputs("\n", f); 1341 (*CurEnv->e_putbody)(f, Mailer[1], FALSE); 1342 fputs("\n", f); 1343 (void) fclose(f); 1344 (void) fflush(stdout); 1345 1346 /* reset ISUID & ISGID bits for paranoid systems */ 1347 (void) chmod(filename, (int) stb.st_mode); 1348 exit(EX_OK); 1349 /*NOTREACHED*/ 1350 } 1351 else 1352 { 1353 /* parent -- wait for exit status */ 1354 register int i; 1355 auto int stat; 1356 1357 while ((i = wait(&stat)) != pid) 1358 { 1359 if (i < 0) 1360 { 1361 stat = EX_OSERR << 8; 1362 break; 1363 } 1364 } 1365 if ((stat & 0377) != 0) 1366 stat = EX_UNAVAILABLE << 8; 1367 return ((stat >> 8) & 0377); 1368 } 1369 } 1370 /* 1371 ** SENDALL -- actually send all the messages. 1372 ** 1373 ** Parameters: 1374 ** e -- the envelope to send. 1375 ** verifyonly -- if set, only give verification messages. 1376 ** 1377 ** Returns: 1378 ** none. 1379 ** 1380 ** Side Effects: 1381 ** Scans the send lists and sends everything it finds. 1382 ** Delivers any appropriate error messages. 1383 */ 1384 1385 sendall(e, verifyonly) 1386 ENVELOPE *e; 1387 bool verifyonly; 1388 { 1389 register ADDRESS *q; 1390 bool oldverbose; 1391 1392 # ifdef DEBUG 1393 if (tTd(13, 2)) 1394 { 1395 printf("\nSend Queue:\n"); 1396 printaddr(e->e_sendqueue, TRUE); 1397 } 1398 # endif DEBUG 1399 1400 /* 1401 ** Run through the list and send everything. 1402 */ 1403 1404 oldverbose = Verbose; 1405 if (verifyonly) 1406 Verbose = TRUE; 1407 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1408 { 1409 if (verifyonly) 1410 { 1411 CurEnv->e_to = q->q_paddr; 1412 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1413 message(Arpa_Info, "deliverable"); 1414 } 1415 else 1416 (void) deliver(q); 1417 } 1418 Verbose = oldverbose; 1419 1420 /* 1421 ** Now run through and check for errors. 1422 */ 1423 1424 if (verifyonly) 1425 return; 1426 1427 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1428 { 1429 register ADDRESS *qq; 1430 1431 if (bitset(QQUEUEUP, q->q_flags)) 1432 e->e_queueup = TRUE; 1433 if (!bitset(QBADADDR, q->q_flags)) 1434 continue; 1435 1436 /* we have an address that failed -- find the parent */ 1437 for (qq = q; qq != NULL; qq = qq->q_alias) 1438 { 1439 char obuf[MAXNAME + 6]; 1440 extern char *aliaslookup(); 1441 1442 /* we can only have owners for local addresses */ 1443 if (!bitset(M_LOCAL, qq->q_mailer->m_flags)) 1444 continue; 1445 1446 /* see if the owner list exists */ 1447 (void) strcpy(obuf, "owner-"); 1448 if (strncmp(qq->q_user, "owner-", 6) == 0) 1449 (void) strcat(obuf, "owner"); 1450 else 1451 (void) strcat(obuf, qq->q_user); 1452 if (aliaslookup(obuf) == NULL) 1453 continue; 1454 1455 /* owner list exists -- add it to the error queue */ 1456 qq->q_flags &= ~QPRIMARY; 1457 sendto(obuf, qq, &e->e_errorqueue); 1458 MailBack = TRUE; 1459 break; 1460 } 1461 1462 /* if we did not find an owner, send to the sender */ 1463 if (qq == NULL) 1464 sendto(e->e_from.q_paddr, qq, &e->e_errorqueue); 1465 } 1466 } 1467 /* 1468 ** CHECKERRORS -- check a queue of addresses and process errors. 1469 ** 1470 ** Parameters: 1471 ** e -- the envelope to check. 1472 ** 1473 ** Returns: 1474 ** none. 1475 ** 1476 ** Side Effects: 1477 ** Arranges to queue all tempfailed messages in q 1478 ** or deliver error responses. 1479 */ 1480 1481 checkerrors(e) 1482 register ENVELOPE *e; 1483 { 1484 register ADDRESS *q; 1485 1486 # ifdef DEBUG 1487 if (tTd(4, 1)) 1488 { 1489 printf("\ncheckerrors: FatalErrors %d, errorqueue:\n"); 1490 printaddr(e->e_errorqueue, TRUE); 1491 } 1492 # endif DEBUG 1493 1494 /* mail back the transcript on errors */ 1495 if (FatalErrors) 1496 savemail(); 1497 1498 /* queue up anything laying around */ 1499 if (e->e_dontqueue) 1500 return; 1501 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1502 { 1503 if (bitset(QQUEUEUP, q->q_flags)) 1504 { 1505 # ifdef QUEUE 1506 queueup(e, FALSE); 1507 # else QUEUE 1508 syserr("checkerrors: trying to queue %s", e->e_df); 1509 # endif QUEUE 1510 break; 1511 } 1512 } 1513 } 1514