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