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