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