1 /* $NetBSD: collect.c,v 1.33 2006/09/18 19:46:21 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)collect.c 8.2 (Berkeley) 4/19/94"; 36 #else 37 __RCSID("$NetBSD: collect.c,v 1.33 2006/09/18 19:46:21 christos Exp $"); 38 #endif 39 #endif /* not lint */ 40 41 /* 42 * Mail -- a mail program 43 * 44 * Collect input from standard input, handling 45 * ~ escapes. 46 */ 47 48 #include "rcv.h" 49 #include "extern.h" 50 51 52 /* 53 * Read a message from standard input and return a read file to it 54 * or NULL on error. 55 */ 56 57 /* 58 * The following hokiness with global variables is so that on 59 * receipt of an interrupt signal, the partial message can be salted 60 * away on dead.letter. 61 */ 62 63 static sig_t saveint; /* Previous SIGINT value */ 64 static sig_t savehup; /* Previous SIGHUP value */ 65 static sig_t savetstp; /* Previous SIGTSTP value */ 66 static sig_t savettou; /* Previous SIGTTOU value */ 67 static sig_t savettin; /* Previous SIGTTIN value */ 68 static FILE *collf; /* File for saving away */ 69 static int hadintr; /* Have seen one SIGINT so far */ 70 71 static jmp_buf colljmp; /* To get back to work */ 72 static int colljmp_p; /* whether to long jump */ 73 static jmp_buf collabort; /* To end collection with error */ 74 75 #if 0 76 static void show_name(const char *prefix, struct name *np) 77 { 78 int i = 0; 79 80 for (/* EMPTY */; np ; np = np->n_flink) { 81 printf("%s[%d]: %s\n", prefix, i, np->n_name); 82 i++; 83 } 84 } 85 86 void show_header(struct header *hp); 87 void show_header(struct header *hp) 88 { 89 show_name("TO", hp->h_to); 90 printf("SUBJECT: %s\n", hp->h_subject); 91 show_name("CC", hp->h_cc); 92 show_name("BCC", hp->h_bcc); 93 show_name("SMOPTS", hp->h_smopts); 94 } 95 #endif 96 97 98 FILE * 99 collect(struct header *hp, int printheaders) 100 { 101 FILE *fbuf; 102 int lc, cc, escape, eofcount; 103 int c, fd, t; 104 char linebuf[LINESIZE]; 105 const char *cp; 106 char getsub; 107 char tempname[PATHSIZE]; 108 char mailtempname[PATHSIZE]; 109 sigset_t nset; 110 int longline, lastlong, rc; /* So we don't make 2 or more lines 111 out of a long input line. */ 112 #if __GNUC__ 113 /* Avoid longjmp clobbering */ 114 (void)&escape; 115 (void)&eofcount; 116 (void)&getsub; 117 (void)&longline; 118 #endif 119 120 (void)memset(mailtempname, 0, sizeof(mailtempname)); 121 collf = NULL; 122 /* 123 * Start catching signals from here, but we're still die on interrupts 124 * until we're in the main loop. 125 */ 126 (void)sigemptyset(&nset); 127 (void)sigaddset(&nset, SIGINT); 128 (void)sigaddset(&nset, SIGHUP); 129 (void)sigprocmask(SIG_BLOCK, &nset, NULL); 130 if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN) 131 (void)signal(SIGINT, collint); 132 if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN) 133 (void)signal(SIGHUP, collhup); 134 savetstp = signal(SIGTSTP, collstop); 135 savettou = signal(SIGTTOU, collstop); 136 savettin = signal(SIGTTIN, collstop); 137 if (setjmp(collabort) || setjmp(colljmp)) { 138 (void)rm(mailtempname); 139 goto err; 140 } 141 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL); 142 143 noreset++; 144 (void)snprintf(mailtempname, sizeof(mailtempname), 145 "%s/mail.RsXXXXXXXXXX", tmpdir); 146 if ((fd = mkstemp(mailtempname)) == -1 || 147 (collf = Fdopen(fd, "w+")) == NULL) { 148 if (fd != -1) 149 (void)close(fd); 150 warn("%s", mailtempname); 151 goto err; 152 } 153 (void)rm(mailtempname); 154 155 /* 156 * If we are going to prompt for a subject, 157 * refrain from printing a newline after 158 * the headers (since some people mind). 159 */ 160 t = GTO|GSUBJECT|GCC|GNL|GSMOPTS; 161 getsub = 0; 162 if (hp->h_subject == NULL && value("interactive") != NULL && 163 (value("ask") != NULL || value("asksub") != NULL)) 164 t &= ~GNL, getsub++; 165 if (printheaders) { 166 (void)puthead(hp, stdout, t); 167 (void)fflush(stdout); 168 } 169 if ((cp = value("escape")) != NULL) 170 escape = *cp; 171 else 172 escape = ESCAPE; 173 eofcount = 0; 174 hadintr = 0; 175 lastlong = 0; 176 longline = 0; 177 178 if (!setjmp(colljmp)) { 179 if (getsub) 180 (void)grabh(hp, GSUBJECT); 181 } else { 182 /* 183 * Come here for printing the after-signal message. 184 * Duplicate messages won't be printed because 185 * the write is aborted if we get a SIGTTOU. 186 */ 187 cont: 188 if (hadintr) { 189 (void)fflush(stdout); 190 (void)fprintf(stderr, 191 "\n(Interrupt -- one more to kill letter)\n"); 192 } else { 193 (void)printf("(continue)\n"); 194 (void)fflush(stdout); 195 } 196 } 197 for (;;) { 198 colljmp_p = 1; 199 c = readline(stdin, linebuf, LINESIZE); 200 colljmp_p = 0; 201 #ifdef USE_READLINE 202 if (c < 0) { 203 char *p; 204 if (value("interactive") != NULL && 205 (p = value("ignoreeof")) != NULL && 206 ++eofcount < (*p == 0 ? 25 : atoi(p))) { 207 (void)printf("Use \".\" to terminate letter\n"); 208 continue; 209 } 210 break; 211 } 212 #else 213 if (c < 0) { 214 if (value("interactive") != NULL && 215 value("ignoreeof") != NULL && ++eofcount < 25) { 216 (void)printf("Use \".\" to terminate letter\n"); 217 continue; 218 } 219 break; 220 } 221 #endif 222 lastlong = longline; 223 longline = c == LINESIZE-1; 224 eofcount = 0; 225 hadintr = 0; 226 if (linebuf[0] == '.' && linebuf[1] == '\0' && 227 value("interactive") != NULL && !lastlong && 228 (value("dot") != NULL || value("ignoreeof") != NULL)) 229 break; 230 if (linebuf[0] != escape || value("interactive") == NULL || 231 lastlong) { 232 if (putline(collf, linebuf, !longline) < 0) 233 goto err; 234 continue; 235 } 236 c = linebuf[1]; 237 switch (c) { 238 default: 239 /* 240 * On double escape, just send the single one. 241 * Otherwise, it's an error. 242 */ 243 if (c == escape) { 244 if (putline(collf, &linebuf[1], !longline) < 0) 245 goto err; 246 else 247 break; 248 } 249 (void)printf("Unknown tilde escape.\n"); 250 break; 251 case 'C': 252 /* 253 * Dump core. 254 */ 255 (void)core(NULL); 256 break; 257 case '!': 258 /* 259 * Shell escape, send the balance of the 260 * line to sh -c. 261 */ 262 (void)shell(&linebuf[2]); 263 break; 264 case ':': 265 case '_': 266 /* 267 * Escape to command mode, but be nice! 268 */ 269 (void)execute(&linebuf[2], 1); 270 goto cont; 271 case '.': 272 /* 273 * Simulate end of file on input. 274 */ 275 goto out; 276 case 'q': 277 /* 278 * Force a quit of sending mail. 279 * Act like an interrupt happened. 280 */ 281 hadintr++; 282 collint(SIGINT); 283 exit(1); 284 /*NOTREACHED*/ 285 286 case 'x': /* exit, do not save in dead.letter */ 287 goto err; 288 289 case 'h': 290 /* 291 * Grab a bunch of headers. 292 */ 293 (void)grabh(hp, GTO|GSUBJECT|GCC|GBCC|GSMOPTS); 294 goto cont; 295 case 't': 296 /* 297 * Add to the To list. 298 */ 299 hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO)); 300 break; 301 case 's': 302 /* 303 * Set the Subject list. 304 */ 305 cp = &linebuf[2]; 306 while (isspace((unsigned char)*cp)) 307 cp++; 308 hp->h_subject = savestr(cp); 309 break; 310 case 'c': 311 /* 312 * Add to the CC list. 313 */ 314 hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC)); 315 break; 316 case 'b': 317 /* 318 * Add stuff to blind carbon copies list. 319 */ 320 hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC)); 321 break; 322 case 'i': 323 case 'A': 324 case 'a': 325 /* 326 * Insert named variable in message 327 */ 328 329 switch(c) { 330 case 'i': 331 cp = &linebuf[2]; 332 while(isspace((unsigned char) *cp)) 333 cp++; 334 335 break; 336 case 'a': 337 cp = "sign"; 338 break; 339 case 'A': 340 cp = "Sign"; 341 break; 342 default: 343 goto err; 344 } 345 346 if(*cp && (cp = value(cp)) != NULL) { 347 (void)printf("%s\n", cp); 348 if(putline(collf, cp, 1) < 0) 349 goto err; 350 } 351 352 break; 353 354 case 'd': 355 (void)strcpy(linebuf + 2, getdeadletter()); 356 /* FALLTHROUGH */ 357 case 'r': 358 case '<': 359 /* 360 * Invoke a file: 361 * Search for the file name, 362 * then open it and copy the contents to collf. 363 */ 364 cp = &linebuf[2]; 365 while (isspace((unsigned char)*cp)) 366 cp++; 367 if (*cp == '\0') { 368 (void)printf("Interpolate what file?\n"); 369 break; 370 } 371 372 cp = expand(cp); 373 if (cp == NULL) 374 break; 375 376 if (*cp == '!') { /* insert stdout of command */ 377 const char *shellcmd; 378 int nullfd; 379 int rc2; 380 381 if((nullfd = open("/dev/null", O_RDONLY, 0)) == -1) { 382 warn("/dev/null"); 383 break; 384 } 385 386 (void)snprintf(tempname, sizeof(tempname), 387 "%s/mail.ReXXXXXXXXXX", tmpdir); 388 if ((fd = mkstemp(tempname)) == -1 || 389 (fbuf = Fdopen(fd, "w+")) == NULL) { 390 if (fd != -1) 391 (void)close(fd); 392 warn("%s", tempname); 393 break; 394 } 395 (void)unlink(tempname); 396 397 if ((shellcmd = value("SHELL")) == NULL) 398 shellcmd = _PATH_CSHELL; 399 400 rc2 = run_command(shellcmd, 0, nullfd, fileno(fbuf), "-c", cp+1, NULL); 401 402 (void)close(nullfd); 403 404 if (rc2 < 0) { 405 (void)Fclose(fbuf); 406 break; 407 } 408 409 if (fsize(fbuf) == 0) { 410 (void)fprintf(stderr, "No bytes from command \"%s\"\n", cp+1); 411 (void)Fclose(fbuf); 412 break; 413 } 414 415 rewind(fbuf); 416 } 417 else if (isdir(cp)) { 418 (void)printf("%s: Directory\n", cp); 419 break; 420 } 421 else if ((fbuf = Fopen(cp, "r")) == NULL) { 422 warn("%s", cp); 423 break; 424 } 425 (void)printf("\"%s\" ", cp); 426 (void)fflush(stdout); 427 lc = 0; 428 cc = 0; 429 while ((rc = readline(fbuf, linebuf, LINESIZE)) >= 0) { 430 if (rc != LINESIZE-1) lc++; 431 if ((t = putline(collf, linebuf, 432 rc != LINESIZE-1)) < 0) { 433 (void)Fclose(fbuf); 434 goto err; 435 } 436 cc += t; 437 } 438 (void)Fclose(fbuf); 439 (void)printf("%d/%d\n", lc, cc); 440 break; 441 case 'w': 442 /* 443 * Write the message on a file. 444 */ 445 cp = &linebuf[2]; 446 while (*cp == ' ' || *cp == '\t') 447 cp++; 448 if (*cp == '\0') { 449 (void)fprintf(stderr, "Write what file!?\n"); 450 break; 451 } 452 if ((cp = expand(cp)) == NULL) 453 break; 454 rewind(collf); 455 (void)exwrite(cp, collf, 1); 456 break; 457 case 'm': 458 case 'M': 459 case 'f': 460 case 'F': 461 /* 462 * Interpolate the named messages, if we 463 * are in receiving mail mode. Does the 464 * standard list processing garbage. 465 * If ~f is given, we don't shift over. 466 */ 467 if (forward(linebuf + 2, collf, mailtempname, c) < 0) 468 goto err; 469 goto cont; 470 case '?': 471 if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) { 472 warn(_PATH_TILDE); 473 break; 474 } 475 while ((t = getc(fbuf)) != EOF) 476 (void)putchar(t); 477 (void)Fclose(fbuf); 478 break; 479 case 'p': 480 /* 481 * Print out the current state of the 482 * message without altering anything. 483 */ 484 rewind(collf); 485 (void)printf("-------\nMessage contains:\n"); 486 (void)puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL); 487 while ((t = getc(collf)) != EOF) 488 (void)putchar(t); 489 goto cont; 490 case '|': 491 /* 492 * Pipe message through command. 493 * Collect output as new message. 494 */ 495 rewind(collf); 496 mespipe(collf, &linebuf[2]); 497 goto cont; 498 case 'v': 499 case 'e': 500 /* 501 * Edit the current message. 502 * 'e' means to use EDITOR 503 * 'v' means to use VISUAL 504 */ 505 rewind(collf); 506 mesedit(collf, c); 507 goto cont; 508 } 509 } 510 goto out; 511 err: 512 if (collf != NULL) { 513 (void)Fclose(collf); 514 collf = NULL; 515 } 516 out: 517 if (collf != NULL) 518 rewind(collf); 519 noreset--; 520 (void)sigprocmask(SIG_BLOCK, &nset, NULL); 521 (void)signal(SIGINT, saveint); 522 (void)signal(SIGHUP, savehup); 523 (void)signal(SIGTSTP, savetstp); 524 (void)signal(SIGTTOU, savettou); 525 (void)signal(SIGTTIN, savettin); 526 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL); 527 return collf; 528 } 529 530 /* 531 * Write a file, ex-like if f set. 532 */ 533 int 534 exwrite(const char name[], FILE *fp, int f) 535 { 536 FILE *of; 537 int c; 538 long cc; 539 int lc; 540 struct stat junk; 541 542 if (f) { 543 (void)printf("\"%s\" ", name); 544 (void)fflush(stdout); 545 } 546 if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) { 547 if (!f) 548 (void)fprintf(stderr, "%s: ", name); 549 (void)fprintf(stderr, "File exists\n"); 550 return(-1); 551 } 552 if ((of = Fopen(name, "w")) == NULL) { 553 warn("%s", name); 554 return(-1); 555 } 556 lc = 0; 557 cc = 0; 558 while ((c = getc(fp)) != EOF) { 559 cc++; 560 if (c == '\n') 561 lc++; 562 (void)putc(c, of); 563 if (ferror(of)) { 564 warn("%s", name); 565 (void)Fclose(of); 566 return(-1); 567 } 568 } 569 (void)Fclose(of); 570 (void)printf("%d/%ld\n", lc, cc); 571 (void)fflush(stdout); 572 return(0); 573 } 574 575 /* 576 * Edit the message being collected on fp. 577 * On return, make the edit file the new temp file. 578 */ 579 void 580 mesedit(FILE *fp, int c) 581 { 582 sig_t sigint = signal(SIGINT, SIG_IGN); 583 FILE *nf = run_editor(fp, (off_t)-1, c, 0); 584 585 if (nf != NULL) { 586 (void)fseek(nf, 0L, 2); 587 collf = nf; 588 (void)Fclose(fp); 589 } 590 (void)signal(SIGINT, sigint); 591 } 592 593 /* 594 * Pipe the message through the command. 595 * Old message is on stdin of command; 596 * New message collected from stdout. 597 * Sh -c must return 0 to accept the new message. 598 */ 599 void 600 mespipe(FILE *fp, char cmd[]) 601 { 602 FILE *nf; 603 sig_t sigint = signal(SIGINT, SIG_IGN); 604 const char *shellcmd; 605 int fd; 606 char tempname[PATHSIZE]; 607 608 (void)snprintf(tempname, sizeof(tempname), 609 "%s/mail.ReXXXXXXXXXX", tmpdir); 610 if ((fd = mkstemp(tempname)) == -1 || 611 (nf = Fdopen(fd, "w+")) == NULL) { 612 if (fd != -1) 613 (void)close(fd); 614 warn("%s", tempname); 615 goto out; 616 } 617 (void)unlink(tempname); 618 /* 619 * stdin = current message. 620 * stdout = new message. 621 */ 622 if ((shellcmd = value("SHELL")) == NULL) 623 shellcmd = _PATH_CSHELL; 624 if (run_command(shellcmd, 625 0, fileno(fp), fileno(nf), "-c", cmd, NULL) < 0) { 626 (void)Fclose(nf); 627 goto out; 628 } 629 if (fsize(nf) == 0) { 630 (void)fprintf(stderr, "No bytes from \"%s\" !?\n", cmd); 631 (void)Fclose(nf); 632 goto out; 633 } 634 /* 635 * Take new files. 636 */ 637 (void)fseek(nf, 0L, 2); 638 collf = nf; 639 (void)Fclose(fp); 640 out: 641 (void)signal(SIGINT, sigint); 642 } 643 644 /* 645 * Interpolate the named messages into the current 646 * message, preceding each line with a tab. 647 * Return a count of the number of characters now in 648 * the message, or -1 if an error is encountered writing 649 * the message temporary. The flag argument is 'm' if we 650 * should shift over and 'f' if not. 651 */ 652 int 653 forward(char ms[], FILE *fp, char *fn, int f) 654 { 655 int *msgvec; 656 struct ignoretab *ig; 657 const char *tabst; 658 659 msgvec = salloc((msgCount+1) * sizeof *msgvec); 660 if (msgvec == NULL) 661 return(0); 662 if (getmsglist(ms, msgvec, 0) < 0) 663 return(0); 664 if (*msgvec == 0) { 665 *msgvec = first(0, MMNORM); 666 if (*msgvec == 0) { 667 (void)printf("No appropriate messages\n"); 668 return(0); 669 } 670 msgvec[1] = 0; 671 } 672 if (f == 'f' || f == 'F') 673 tabst = NULL; 674 else if ((tabst = value("indentprefix")) == NULL) 675 tabst = "\t"; 676 ig = isupper(f) ? NULL : ignore; 677 (void)printf("Interpolating:"); 678 for (; *msgvec != 0; msgvec++) { 679 struct message *mp = message + *msgvec - 1; 680 681 touch(mp); 682 (void)printf(" %d", *msgvec); 683 if (sendmessage(mp, fp, ig, tabst) < 0) { 684 warn("%s", fn); 685 return(-1); 686 } 687 } 688 (void)printf("\n"); 689 return(0); 690 } 691 692 /* 693 * Print (continue) when continued after ^Z. 694 */ 695 /*ARGSUSED*/ 696 void 697 collstop(int s) 698 { 699 sig_t old_action = signal(s, SIG_DFL); 700 sigset_t nset; 701 702 (void)sigemptyset(&nset); 703 (void)sigaddset(&nset, s); 704 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL); 705 (void)kill(0, s); 706 (void)sigprocmask(SIG_BLOCK, &nset, NULL); 707 (void)signal(s, old_action); 708 if (colljmp_p) { 709 colljmp_p = 0; 710 hadintr = 0; 711 longjmp(colljmp, 1); 712 } 713 } 714 715 /* 716 * On interrupt, come here to save the partial message in ~/dead.letter. 717 * Then jump out of the collection loop. 718 */ 719 /*ARGSUSED*/ 720 void 721 collint(int s) 722 { 723 /* 724 * the control flow is subtle, because we can be called from ~q. 725 */ 726 if (!hadintr) { 727 if (value("ignore") != NULL) { 728 (void)puts("@"); 729 (void)fflush(stdout); 730 clearerr(stdin); 731 return; 732 } 733 hadintr = 1; 734 longjmp(colljmp, 1); 735 } 736 rewind(collf); 737 if (value("nosave") == NULL) 738 savedeadletter(collf); 739 longjmp(collabort, 1); 740 } 741 742 /*ARGSUSED*/ 743 void 744 collhup(int s) 745 { 746 rewind(collf); 747 savedeadletter(collf); 748 /* 749 * Let's pretend nobody else wants to clean up, 750 * a true statement at this time. 751 */ 752 exit(1); 753 } 754 755 void 756 savedeadletter(FILE *fp) 757 { 758 FILE *dbuf; 759 mode_t m; 760 int c; 761 const char *cp; 762 763 if (fsize(fp) == 0) 764 return; 765 cp = getdeadletter(); 766 m = umask(077); 767 dbuf = Fopen(cp, "a"); 768 (void)umask(m); 769 if (dbuf == NULL) 770 return; 771 while ((c = getc(fp)) != EOF) 772 (void)putc(c, dbuf); 773 (void)Fclose(dbuf); 774 rewind(fp); 775 } 776