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