1 # include <stdio.h> 2 # include <pwd.h> 3 # include <signal.h> 4 # include "dlvrmail.h" 5 # ifdef LOG 6 # include <log.h> 7 # endif LOG 8 9 static char SccsId[] = "@(#)deliver.c 2.5 02/05/81"; 10 11 /* 12 ** DELIVER -- Deliver a message to a particular address. 13 ** 14 ** Algorithm: 15 ** Compute receiving network (i.e., mailer), host, & user. 16 ** If local, see if this is really a program name. 17 ** Build argument for the mailer. 18 ** Create pipe through edit fcn if appropriate. 19 ** Fork. 20 ** Child: call mailer 21 ** Parent: call editfcn if specified. 22 ** Wait for mailer to finish. 23 ** Interpret exit status. 24 ** 25 ** Parameters: 26 ** to -- the address to deliver the message to. 27 ** editfcn -- if non-NULL, we want to call this function 28 ** to output the letter (instead of just out- 29 ** putting it raw). 30 ** 31 ** Returns: 32 ** zero -- successfully delivered. 33 ** else -- some failure, see ExitStat for more info. 34 ** 35 ** Side Effects: 36 ** The standard input is passed off to someone. 37 ** 38 ** WARNING: 39 ** The standard input is shared amongst all children, 40 ** including the file pointer. It is critical that the 41 ** parent waits for the child to finish before forking 42 ** another child. 43 ** 44 ** Called By: 45 ** main 46 ** savemail 47 ** 48 ** Files: 49 ** standard input -- must be opened to the message to 50 ** deliver. 51 */ 52 53 deliver(to, editfcn) 54 addrq *to; 55 int (*editfcn)(); 56 { 57 register struct mailer *m; 58 char *host; 59 char *user; 60 extern struct passwd *getpwnam(); 61 char **pvp; 62 extern char **buildargv(); 63 auto int st; 64 register int i; 65 register char *p; 66 int pid; 67 int pvect[2]; 68 extern FILE *fdopen(); 69 extern int errno; 70 FILE *mfile; 71 extern putheader(); 72 extern pipesig(); 73 extern bool GotHdr; 74 extern char *index(); 75 76 /* 77 ** Compute receiving mailer, host, and to addreses. 78 ** Do some initialization first. To is the to address 79 ** for error messages. 80 */ 81 82 To = to->q_paddr; 83 m = to->q_mailer; 84 user = to->q_user; 85 host = to->q_host; 86 Errors = 0; 87 errno = 0; 88 # ifdef DEBUG 89 if (Debug) 90 printf("deliver(%s [%d, `%s', `%s'])\n", To, m, host, user); 91 # endif DEBUG 92 93 /* 94 ** Remove quote bits from user/host. 95 */ 96 97 for (p = user; (*p++ &= 0177) != '\0'; ) 98 continue; 99 if (host != NULL) 100 for (p = host; (*p++ &= 0177) != '\0'; ) 101 continue; 102 103 /* 104 ** Strip quote bits from names if the mailer wants it. 105 */ 106 107 if (flagset(M_STRIPQ, m->m_flags)) 108 { 109 stripquotes(user); 110 stripquotes(host); 111 } 112 113 /* 114 ** See if this user name is "special". 115 ** If the user is a program, diddle with the mailer spec. 116 ** If the user name has a slash in it, assume that this 117 ** is a file -- send it off without further ado. 118 ** Note that this means that editfcn's will not 119 ** be applied to the message. 120 */ 121 122 if (m == &Mailer[0]) 123 { 124 if (*user == '|') 125 { 126 user++; 127 m = &Mailer[1]; 128 } 129 else 130 { 131 if (index(user, '/') != NULL) 132 { 133 i = mailfile(user); 134 giveresponse(i, TRUE, m); 135 return (i); 136 } 137 } 138 } 139 140 /* 141 ** See if the user exists. 142 ** Strictly, this is only needed to print a pretty 143 ** error message. 144 ** 145 ** >>>>>>>>>> This clause assumes that the local mailer 146 ** >> NOTE >> cannot do any further aliasing; that 147 ** >>>>>>>>>> function is subsumed by delivermail. 148 */ 149 150 if (m == &Mailer[0]) 151 { 152 if (getpwnam(user) == NULL) 153 { 154 giveresponse(EX_NOUSER, TRUE, m); 155 return (EX_NOUSER); 156 } 157 } 158 159 /* 160 ** If the mailer wants a From line, insert a new editfcn. 161 */ 162 163 if (flagset(M_HDR, m->m_flags) && editfcn == NULL && (!GotHdr || flagset(M_FHDR, m->m_flags))) 164 editfcn = putheader; 165 166 /* 167 ** Call the mailer. 168 ** The argument vector gets built, pipes through 'editfcn' 169 ** are created as necessary, and we fork & exec as 170 ** appropriate. In the parent, we call 'editfcn'. 171 */ 172 173 pvp = buildargv(m->m_argv, m->m_flags, host, user, From.q_paddr); 174 if (pvp == NULL) 175 { 176 usrerr("name too long"); 177 return (-1); 178 } 179 rewind(stdin); 180 181 /* create a pipe if we will need one */ 182 if (editfcn != NULL && pipe(pvect) < 0) 183 { 184 syserr("pipe"); 185 return (-1); 186 } 187 # ifdef VFORK 188 pid = vfork(); 189 # else 190 pid = fork(); 191 # endif 192 if (pid < 0) 193 { 194 syserr("Cannot fork"); 195 if (editfcn != NULL) 196 { 197 close(pvect[0]); 198 close(pvect[1]); 199 } 200 return (-1); 201 } 202 else if (pid == 0) 203 { 204 /* child -- set up input & exec mailer */ 205 /* make diagnostic output be standard output */ 206 close(2); 207 dup(1); 208 signal(SIGINT, SIG_IGN); 209 if (editfcn != NULL) 210 { 211 close(0); 212 if (dup(pvect[0]) < 0) 213 { 214 syserr("Cannot dup to zero!"); 215 _exit(EX_OSERR); 216 } 217 close(pvect[0]); 218 close(pvect[1]); 219 } 220 if (!flagset(M_RESTR, m->m_flags)) 221 setuid(getuid()); 222 # ifdef LOG 223 closelog(); 224 # endif LOG 225 # ifndef VFORK 226 /* 227 * We have to be careful with vfork - we can't mung up the 228 * memory but we don't want the mailer to inherit any extra 229 * open files. Chances are the mailer won't 230 * care about an extra file, but then again you never know. 231 * Actually, we would like to close(fileno(pwf)), but it's 232 * declared static so we can't. But if we fclose(pwf), which 233 * is what endpwent does, it closes it in the parent too and 234 * the next getpwnam will be slower. If you have a weird mailer 235 * that chokes on the extra file you should do the endpwent(). 236 */ 237 endpwent(); 238 # endif 239 execv(m->m_mailer, pvp); 240 /* syserr fails because log is closed */ 241 /* syserr("Cannot exec %s", m->m_mailer); */ 242 printf("Cannot exec %s\n", m->m_mailer); 243 fflush(stdout); 244 _exit(EX_UNAVAILABLE); 245 } 246 247 /* arrange to write out header message if error */ 248 if (editfcn != NULL) 249 { 250 close(pvect[0]); 251 signal(SIGPIPE, pipesig); 252 mfile = fdopen(pvect[1], "w"); 253 (*editfcn)(mfile); 254 fclose(mfile); 255 } 256 257 /* 258 ** Wait for child to die and report status. 259 ** We should never get fatal errors (e.g., segmentation 260 ** violation), so we report those specially. For other 261 ** errors, we choose a status message (into statmsg), 262 ** and if it represents an error, we print it. 263 */ 264 265 while ((i = wait(&st)) > 0 && i != pid) 266 continue; 267 if (i < 0) 268 { 269 syserr("wait"); 270 return (-1); 271 } 272 if ((st & 0377) != 0) 273 { 274 syserr("%s: stat %o", pvp[0], st); 275 ExitStat = EX_UNAVAILABLE; 276 return (-1); 277 } 278 i = (st >> 8) & 0377; 279 giveresponse(i, TRUE, m); 280 return (i); 281 } 282 /* 283 ** GIVERESPONSE -- Interpret an error response from a mailer 284 ** 285 ** Parameters: 286 ** stat -- the status code from the mailer (high byte 287 ** only; core dumps must have been taken care of 288 ** already). 289 ** force -- if set, force an error message output, even 290 ** if the mailer seems to like to print its own 291 ** messages. 292 ** m -- the mailer descriptor for this mailer. 293 ** 294 ** Returns: 295 ** none. 296 ** 297 ** Side Effects: 298 ** Errors may be incremented. 299 ** ExitStat may be set. 300 ** 301 ** Called By: 302 ** deliver 303 */ 304 305 giveresponse(stat, force, m) 306 int stat; 307 int force; 308 register struct mailer *m; 309 { 310 register char *statmsg; 311 extern char *SysExMsg[]; 312 register int i; 313 extern int N_SysEx; 314 extern long MsgSize; 315 char buf[30]; 316 317 i = stat - EX__BASE; 318 if (i < 0 || i > N_SysEx) 319 statmsg = NULL; 320 else 321 statmsg = SysExMsg[i]; 322 if (stat == 0) 323 statmsg = "ok"; 324 else 325 { 326 Errors++; 327 if (statmsg == NULL && m->m_badstat != 0) 328 { 329 stat = m->m_badstat; 330 i = stat - EX__BASE; 331 # ifdef DEBUG 332 if (i < 0 || i >= N_SysEx) 333 syserr("Bad m_badstat %d", stat); 334 else 335 # endif DEBUG 336 statmsg = SysExMsg[i]; 337 } 338 if (statmsg == NULL) 339 usrerr("unknown mailer response %d", stat); 340 else if (force || !flagset(M_QUIET, m->m_flags)) 341 usrerr("%s", statmsg); 342 } 343 344 /* 345 ** Final cleanup. 346 ** Log a record of the transaction. Compute the new 347 ** ExitStat -- if we already had an error, stick with 348 ** that. 349 */ 350 351 if (statmsg == NULL) 352 { 353 sprintf(buf, "error %d", stat); 354 statmsg = buf; 355 } 356 357 # ifdef LOG 358 logmsg(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg); 359 # endif LOG 360 setstat(stat); 361 return (stat); 362 } 363 /* 364 ** PUTHEADER -- insert the From header into some mail 365 ** 366 ** For mailers such as 'msgs' that want the header inserted 367 ** into the mail, this edit filter inserts the From line and 368 ** then passes the rest of the message through. 369 ** 370 ** Parameters: 371 ** fp -- the file pointer for the output. 372 ** 373 ** Returns: 374 ** none 375 ** 376 ** Side Effects: 377 ** Puts a "From" line in UNIX format, and then 378 ** outputs the rest of the message. 379 ** 380 ** Called By: 381 ** deliver 382 */ 383 384 putheader(fp) 385 register FILE *fp; 386 { 387 char buf[MAXLINE + 1]; 388 long tim; 389 extern char *ctime(); 390 register char *p; 391 extern char *index(); 392 393 /* output the header part */ 394 fgets(buf, sizeof buf, stdin); 395 if (strncmp(buf, "From ", 5) != 0 || (p = index(&buf[5], ' ')) == NULL) 396 { 397 time(&tim); 398 fprintf(fp, "From %s %s", From.q_paddr, ctime(&tim)); 399 fputs(buf, fp); 400 } 401 else 402 fprintf(fp, "From %s %s", From.q_paddr, &p[1]); 403 404 /* output the body */ 405 while (!ferror(fp) && fgets(buf, sizeof buf, stdin) != NULL) 406 fputs(buf, fp); 407 if (ferror(fp)) 408 { 409 syserr("putheader: write error"); 410 setstat(EX_IOERR); 411 } 412 } 413 /* 414 ** PIPESIG -- Handle broken pipe signals 415 ** 416 ** This just logs an error. 417 ** 418 ** Parameters: 419 ** none 420 ** 421 ** Returns: 422 ** none 423 ** 424 ** Side Effects: 425 ** logs an error message. 426 */ 427 428 pipesig() 429 { 430 syserr("Broken pipe"); 431 signal(SIGPIPE, SIG_IGN); 432 } 433 /* 434 ** SENDTO -- Designate a send list. 435 ** 436 ** The parameter is a comma-separated list of people to send to. 437 ** This routine arranges to send to all of them. 438 ** 439 ** Parameters: 440 ** list -- the send list. 441 ** copyf -- the copy flag; passed to parse. 442 ** 443 ** Returns: 444 ** none 445 ** 446 ** Side Effects: 447 ** none. 448 ** 449 ** Called By: 450 ** main 451 ** alias 452 */ 453 454 sendto(list, copyf) 455 char *list; 456 int copyf; 457 { 458 register char *p; 459 register char *q; 460 register char c; 461 addrq *a; 462 extern addrq *parse(); 463 bool more; 464 465 /* more keeps track of what the previous delimiter was */ 466 more = TRUE; 467 for (p = list; more; ) 468 { 469 /* find the end of this address */ 470 q = p; 471 while ((c = *p++) != '\0' && c != ',' && c != '\n') 472 continue; 473 more = c != '\0'; 474 *--p = '\0'; 475 if (more) 476 p++; 477 478 /* parse the address */ 479 if ((a = parse(q, (addrq *) NULL, copyf)) == NULL) 480 continue; 481 482 /* arrange to send to this person */ 483 recipient(a, &SendQ); 484 } 485 To = NULL; 486 } 487 /* 488 ** RECIPIENT -- Designate a message recipient 489 ** 490 ** Saves the named person for future mailing. 491 ** 492 ** Designates a person as a recipient. This routine 493 ** does the initial parsing, and checks to see if 494 ** this person has already received the mail. 495 ** It also supresses local network names and turns them into 496 ** local names. 497 ** 498 ** Parameters: 499 ** a -- the (preparsed) address header for the recipient. 500 ** targetq -- the queue to add the name to. 501 ** 502 ** Returns: 503 ** none. 504 ** 505 ** Side Effects: 506 ** none. 507 ** 508 ** Called By: 509 ** sendto 510 ** main 511 */ 512 513 recipient(a, targetq) 514 register addrq *a; 515 addrq *targetq; 516 { 517 register addrq *q; 518 register struct mailer *m; 519 register char **pvp; 520 extern char *xalloc(); 521 extern bool forward(); 522 extern int errno; 523 extern bool sameaddr(); 524 525 To = a->q_paddr; 526 m = a->q_mailer; 527 errno = 0; 528 # ifdef DEBUG 529 if (Debug) 530 printf("recipient(%s)\n", To); 531 # endif DEBUG 532 533 /* 534 ** Look up this person in the recipient list. If they 535 ** are there already, return, otherwise continue. 536 */ 537 538 if (!ForceMail) 539 { 540 for (q = &SendQ; (q = nxtinq(q)) != NULL; ) 541 if (sameaddr(q, a, FALSE)) 542 { 543 # ifdef DEBUG 544 if (Debug) 545 printf("(%s in SendQ)\n", a->q_paddr); 546 # endif DEBUG 547 return; 548 } 549 for (q = &AliasQ; (q = nxtinq(q)) != NULL; ) 550 if (sameaddr(q, a, FALSE)) 551 { 552 # ifdef DEBUG 553 if (Debug) 554 printf("(%s in AliasQ)\n", a->q_paddr); 555 # endif DEBUG 556 return; 557 } 558 } 559 560 /* 561 ** See if the user wants hir mail forwarded. 562 ** `Forward' must do the forwarding recursively. 563 */ 564 565 if (m == &Mailer[0] && !NoAlias && targetq == &SendQ && forward(a)) 566 return; 567 568 /* 569 ** Put the user onto the target queue. 570 */ 571 572 if (targetq != NULL) 573 { 574 putonq(a, targetq); 575 } 576 577 return; 578 } 579 /* 580 ** BUILDARGV -- Build an argument vector for a mail server. 581 ** 582 ** Using a template defined in config.c, an argv is built. 583 ** The format of the template is already a vector. The 584 ** items of this vector are copied, unless a dollar sign 585 ** is encountered. In this case, the next character 586 ** specifies something else to copy in. These can be 587 ** $f The from address. 588 ** $h The host. 589 ** $u The user. 590 ** $c The hop count. 591 ** The vector is built in a local buffer. A pointer to 592 ** the static argv is returned. 593 ** 594 ** Parameters: 595 ** tmplt -- a template for an argument vector. 596 ** flags -- the flags for this server. 597 ** host -- the host name to send to. 598 ** user -- the user name to send to. 599 ** from -- the person this mail is from. 600 ** 601 ** Returns: 602 ** A pointer to an argv. 603 ** 604 ** Side Effects: 605 ** none 606 ** 607 ** WARNING: 608 ** Since the argv is staticly allocated, any subsequent 609 ** calls will clobber the old argv. 610 ** 611 ** Called By: 612 ** deliver 613 */ 614 615 char ** 616 buildargv(tmplt, flags, host, user, from) 617 char **tmplt; 618 int flags; 619 char *host; 620 char *user; 621 char *from; 622 { 623 register char *p; 624 register char *q; 625 static char *pv[MAXPV+1]; 626 char **pvp; 627 char **mvp; 628 static char buf[512]; 629 register char *bp; 630 char pbuf[30]; 631 632 /* 633 ** Do initial argv setup. 634 ** Insert the mailer name. Notice that $x expansion is 635 ** NOT done on the mailer name. Then, if the mailer has 636 ** a picky -f flag, we insert it as appropriate. This 637 ** code does not check for 'pv' overflow; this places a 638 ** manifest lower limit of 4 for MAXPV. 639 */ 640 641 pvp = pv; 642 bp = buf; 643 644 *pvp++ = tmplt[0]; 645 646 /* insert -f or -r flag as appropriate */ 647 if (flagset(M_FOPT|M_ROPT, flags) && FromFlag) 648 { 649 if (flagset(M_FOPT, flags)) 650 *pvp++ = "-f"; 651 else 652 *pvp++ = "-r"; 653 *pvp++ = From.q_paddr; 654 } 655 656 /* 657 ** Build the rest of argv. 658 ** For each prototype parameter, the prototype is 659 ** scanned character at a time. If a dollar-sign is 660 ** found, 'q' is set to the appropriate expansion, 661 ** otherwise it is null. Then either the string 662 ** pointed to by q, or the original character, is 663 ** interpolated into the buffer. Buffer overflow is 664 ** checked. 665 */ 666 667 for (mvp = tmplt; (p = *++mvp) != NULL; ) 668 { 669 if (pvp >= &pv[MAXPV]) 670 { 671 syserr("Too many parameters to %s", pv[0]); 672 return (NULL); 673 } 674 *pvp++ = bp; 675 for (; *p != '\0'; p++) 676 { 677 /* q will be the interpolated quantity */ 678 q = NULL; 679 if (*p == '$') 680 { 681 switch (*++p) 682 { 683 case 'f': /* from person */ 684 q = from; 685 break; 686 687 case 'u': /* user */ 688 q = user; 689 break; 690 691 case 'h': /* host */ 692 q = host; 693 break; 694 695 case 'c': /* hop count */ 696 sprintf(pbuf, "%d", HopCount); 697 q = pbuf; 698 break; 699 } 700 } 701 702 /* 703 ** Interpolate q or output one character 704 ** Strip quote bits as we proceed..... 705 */ 706 707 if (q != NULL) 708 { 709 while (bp < &buf[sizeof buf - 1] && (*bp++ = *q++) != '\0') 710 continue; 711 bp--; 712 } 713 else if (bp < &buf[sizeof buf - 1]) 714 *bp++ = *p; 715 } 716 *bp++ = '\0'; 717 if (bp >= &buf[sizeof buf - 1]) 718 return (NULL); 719 } 720 *pvp = NULL; 721 722 # ifdef DEBUG 723 if (Debug) 724 { 725 printf("Interpolated argv is:\n"); 726 for (mvp = pv; *mvp != NULL; mvp++) 727 printf("\t%s\n", *mvp); 728 } 729 # endif DEBUG 730 731 return (pv); 732 } 733 /* 734 ** MAILFILE -- Send a message to a file. 735 ** 736 ** Parameters: 737 ** filename -- the name of the file to send to. 738 ** 739 ** Returns: 740 ** The exit code associated with the operation. 741 ** 742 ** Side Effects: 743 ** none. 744 ** 745 ** Called By: 746 ** deliver 747 */ 748 749 mailfile(filename) 750 char *filename; 751 { 752 char buf[MAXLINE]; 753 register FILE *f; 754 auto long tim; 755 extern char *ctime(); 756 757 f = fopen(filename, "a"); 758 if (f == NULL) 759 return (EX_CANTCREAT); 760 761 /* output the timestamp */ 762 time(&tim); 763 fprintf(f, "From %s %s", From.q_paddr, ctime(&tim)); 764 rewind(stdin); 765 while (fgets(buf, sizeof buf, stdin) != NULL) 766 { 767 fputs(buf, f); 768 if (ferror(f)) 769 { 770 fclose(f); 771 return (EX_IOERR); 772 } 773 } 774 fputs("\n", f); 775 fclose(f); 776 return (EX_OK); 777 } 778