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