1 /* $NetBSD: collect.c,v 1.47 2013/06/28 17:36:18 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.47 2013/06/28 17:36:18 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 <assert.h> 49 #include <util.h> 50 51 #include "rcv.h" 52 #include "extern.h" 53 #include "format.h" 54 #ifdef MIME_SUPPORT 55 #include "mime.h" 56 #endif 57 #include "sig.h" 58 #include "thread.h" 59 60 61 /* 62 * Read a message from standard input and return a read file to it 63 * or NULL on error. 64 */ 65 66 /* 67 * The following hokiness with global variables is so that on 68 * receipt of an interrupt signal, the partial message can be salted 69 * away on dead.letter. 70 */ 71 static FILE *collf; /* File for saving away */ 72 static int hadintr; /* Have seen one SIGINT so far */ 73 74 static jmp_buf abort_jmpbuf; /* To end collection with error */ 75 static jmp_buf reset_jmpbuf; /* To get back to work */ 76 static int reset_on_stop; /* To do job control longjmp. */ 77 78 /* 79 * Write a file, ex-like if f set. 80 */ 81 static int 82 exwrite(const char name[], FILE *fp, int f) 83 { 84 FILE *of; 85 int c; 86 long cc; 87 int lc; 88 struct stat junk; 89 90 if (f) { 91 (void)printf("\"%s\" ", name); 92 (void)fflush(stdout); 93 } 94 if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) { 95 if (!f) 96 (void)fprintf(stderr, "%s: ", name); 97 (void)fprintf(stderr, "File exists\n"); 98 return -1; 99 } 100 if ((of = Fopen(name, "we")) == NULL) { 101 warn("%s", name); 102 return -1; 103 } 104 lc = 0; 105 cc = 0; 106 while ((c = getc(fp)) != EOF) { 107 cc++; 108 if (c == '\n') 109 lc++; 110 (void)putc(c, of); 111 if (ferror(of)) { 112 warn("%s", name); 113 (void)Fclose(of); 114 return -1; 115 } 116 } 117 (void)Fclose(of); 118 (void)printf("%d/%ld\n", lc, cc); 119 (void)fflush(stdout); 120 return 0; 121 } 122 123 /* 124 * Edit the message being collected on fp. 125 * On return, make the edit file the new temp file. 126 */ 127 static void 128 mesedit(FILE *fp, int c) 129 { 130 struct sigaction osa; 131 sigset_t oset; 132 FILE *nf; 133 134 sig_check(); 135 (void)sig_ignore(SIGINT, &osa, &oset); 136 nf = run_editor(fp, (off_t)-1, c, 0); 137 if (nf != NULL) { 138 (void)fseek(nf, 0L, 2); 139 collf = nf; 140 (void)Fclose(fp); 141 } 142 (void)sig_restore(SIGINT, &osa, &oset); 143 sig_check(); 144 } 145 146 /* 147 * Pipe the message through the command. 148 * Old message is on stdin of command; 149 * New message collected from stdout. 150 * Sh -c must return 0 to accept the new message. 151 */ 152 static void 153 mespipe(FILE *fp, char cmd[]) 154 { 155 FILE *nf; 156 struct sigaction osa; 157 sigset_t oset; 158 const char *shellcmd; 159 int fd; 160 char tempname[PATHSIZE]; 161 162 sig_check(); 163 (void)sig_ignore(SIGINT, &osa, &oset); 164 165 (void)snprintf(tempname, sizeof(tempname), 166 "%s/mail.ReXXXXXXXXXX", tmpdir); 167 if ((fd = mkstemp(tempname)) == -1 || 168 (nf = Fdopen(fd, "we+")) == NULL) { 169 if (fd != -1) 170 (void)close(fd); 171 warn("%s", tempname); 172 goto out; 173 } 174 (void)unlink(tempname); 175 /* 176 * stdin = current message. 177 * stdout = new message. 178 */ 179 if ((shellcmd = value(ENAME_SHELL)) == NULL) 180 shellcmd = _PATH_CSHELL; 181 if (run_command(shellcmd, 182 NULL, fileno(fp), fileno(nf), "-c", cmd, NULL) < 0) { 183 (void)Fclose(nf); 184 goto out; 185 } 186 if (fsize(nf) == 0) { 187 (void)fprintf(stderr, "No bytes from \"%s\" !?\n", cmd); 188 (void)Fclose(nf); 189 goto out; 190 } 191 /* 192 * Take new files. 193 */ 194 (void)fseek(nf, 0L, 2); 195 collf = nf; 196 (void)Fclose(fp); 197 out: 198 (void)sig_restore(SIGINT, &osa, &oset); 199 sig_check(); 200 } 201 202 /* 203 * Interpolate the named messages into the current 204 * message, preceding each line with a tab. 205 * Return a count of the number of characters now in 206 * the message, or -1 if an error is encountered writing 207 * the message temporary. The flag argument is 'm' if we 208 * should shift over and 'f' if not. 209 */ 210 static int 211 interpolate(char ms[], FILE *fp, char *fn, int f) 212 { 213 int *msgvec; 214 struct ignoretab *ig; 215 const char *tabst; 216 #ifdef MIME_SUPPORT 217 struct mime_info *mip; 218 int retval; 219 #endif 220 msgvec = salloc((get_msgCount() + 1) * sizeof(*msgvec)); 221 if (msgvec == NULL) 222 return 0; 223 if (getmsglist(ms, msgvec, 0) < 0) 224 return 0; 225 if (*msgvec == 0) { 226 *msgvec = first(0, MMNORM); 227 if (*msgvec == 0) { 228 (void)printf("No appropriate messages\n"); 229 return 0; 230 } 231 msgvec[1] = 0; 232 } 233 if (f == 'f' || f == 'F') 234 tabst = NULL; 235 else if ((tabst = value(ENAME_INDENTPREFIX)) == NULL) 236 tabst = "\t"; 237 ig = isupper(f) ? NULL : ignore; 238 (void)printf("Interpolating:"); 239 for (/*EMPTY*/; *msgvec != 0; msgvec++) { 240 struct message *mp; 241 char *fmtstr; 242 243 mp = get_message(*msgvec); 244 touch(mp); 245 (void)printf(" %d", *msgvec); 246 (void)fflush(stdout); /* flush stdout and the above */ 247 248 if (tabst && (fmtstr = value(ENAME_INDENT_PREAMBLE)) != NULL) 249 fmsgprintf(collf, fmtstr, mp); 250 #ifdef MIME_SUPPORT 251 mip = NULL; 252 if (value(ENAME_MIME_DECODE_MSG)) { 253 if ((tabst == NULL && value(ENAME_MIME_DECODE_INSERT)) || 254 (tabst != NULL && value(ENAME_MIME_DECODE_QUOTE))) 255 mip = mime_decode_open(mp); 256 } 257 retval = mime_sendmessage(mp, fp, ig, tabst, mip); 258 mime_decode_close(mip); 259 if (retval < 0) 260 #else 261 if (sendmessage(mp, fp, ig, tabst, NULL) < 0) 262 #endif 263 { 264 warn("%s", fn); 265 return -1; 266 } 267 if (tabst && (fmtstr = value(ENAME_INDENT_POSTSCRIPT)) != NULL) 268 fmsgprintf(collf, fmtstr, mp); 269 } 270 (void)printf("\n"); 271 return 0; 272 } 273 274 /* 275 * Append the contents of the file to the end of the deadletter file. 276 */ 277 PUBLIC void 278 savedeadletter(FILE *fp) 279 { 280 FILE *dbuf; 281 mode_t m; 282 int c; 283 const char *cp; 284 285 if (fsize(fp) == 0) 286 return; 287 cp = getdeadletter(); 288 m = umask(077); 289 dbuf = Fopen(cp, "ae"); 290 (void)umask(m); 291 if (dbuf == NULL) 292 return; 293 (void)printf("Saving message body to `%s'.\n", cp); 294 while ((c = getc(fp)) != EOF) 295 (void)putc(c, dbuf); 296 (void)Fclose(dbuf); 297 rewind(fp); 298 } 299 300 /* 301 * On interrupt, come here to save the partial message in ~/dead.letter. 302 * Then jump out of the collection loop. 303 */ 304 static void 305 coll_int(int signo) 306 { 307 sig_t o = signal(SIGINT, SIG_IGN); 308 309 /* 310 * the control flow is subtle, because we can be called from ~q. 311 */ 312 if (!hadintr) { 313 if (value(ENAME_IGNORE) != NULL) { 314 (void)puts("@"); 315 (void)fflush(stdout); 316 clearerr(stdin); 317 return; 318 } 319 hadintr = 1; 320 longjmp(reset_jmpbuf, signo); 321 } 322 if (collf) { 323 rewind(collf); 324 if (value(ENAME_NOSAVE) == NULL) 325 savedeadletter(collf); 326 } 327 signal(SIGINT, o); 328 longjmp(abort_jmpbuf, signo); 329 } 330 331 /*ARGSUSED*/ 332 __dead static void 333 coll_hup(int signo __unused) 334 { 335 336 rewind(collf); 337 savedeadletter(collf); 338 /* 339 * Let's pretend nobody else wants to clean up, 340 * a true statement at this time. 341 */ 342 exit(EXIT_FAILURE); 343 } 344 345 /* 346 * Print (continue) when continued after ^Z. 347 */ 348 static void 349 coll_stop(int signo) 350 { 351 352 if (reset_on_stop) { 353 reset_on_stop = 0; 354 hadintr = 0; 355 longjmp(reset_jmpbuf, signo); 356 } 357 } 358 359 PUBLIC FILE * 360 collect(struct header *hp, int printheaders) 361 { 362 sig_t volatile old_sigint = sig_current(SIGINT); 363 sig_t volatile old_sighup = sig_current(SIGHUP); 364 sig_t volatile old_sigtstp = sig_current(SIGTSTP); 365 sig_t volatile old_sigttin = sig_current(SIGTTIN); 366 sig_t volatile old_sigttou = sig_current(SIGTTOU); 367 FILE *fbuf; 368 int lc, cc; 369 int c, fd, t; 370 char linebuf[LINESIZE]; 371 const char *cp; 372 char tempname[PATHSIZE]; 373 char mailtempname[PATHSIZE]; 374 int eofcount; 375 int longline; 376 int lastlong, rc; /* So we don't make 2 or more lines 377 out of a long input line. */ 378 379 /* The following are declared volatile to avoid longjmp clobbering. */ 380 char volatile getsub; 381 int volatile escape; 382 383 (void)memset(mailtempname, 0, sizeof(mailtempname)); 384 collf = NULL; 385 386 if (setjmp(abort_jmpbuf) || setjmp(reset_jmpbuf)) { 387 (void)rm(mailtempname); 388 goto err; 389 } 390 sig_check(); 391 392 sig_hold(); 393 old_sigint = sig_signal(SIGINT, coll_int); 394 old_sighup = sig_signal(SIGHUP, coll_hup); 395 old_sigtstp = sig_signal(SIGTSTP, coll_stop); 396 old_sigttin = sig_signal(SIGTTIN, coll_stop); 397 old_sigttou = sig_signal(SIGTTOU, coll_stop); 398 sig_release(); 399 400 noreset++; 401 (void)snprintf(mailtempname, sizeof(mailtempname), 402 "%s/mail.RsXXXXXXXXXX", tmpdir); 403 if ((fd = mkstemp(mailtempname)) == -1 || 404 (collf = Fdopen(fd, "we+")) == NULL) { 405 if (fd != -1) 406 (void)close(fd); 407 warn("%s", mailtempname); 408 goto err; 409 } 410 (void)rm(mailtempname); 411 412 /* 413 * If we are going to prompt for a subject, 414 * refrain from printing a newline after 415 * the headers (since some people mind). 416 */ 417 t = GTO | GSUBJECT | GCC | GNL | GSMOPTS; 418 getsub = 0; 419 if (hp->h_subject == NULL && value(ENAME_INTERACTIVE) != NULL && 420 (value(ENAME_ASK) != NULL || value(ENAME_ASKSUB) != NULL)) { 421 t &= ~GNL; 422 getsub++; 423 } 424 if (printheaders) { 425 (void)puthead(hp, stdout, t); 426 (void)fflush(stdout); 427 } 428 if ((cp = value(ENAME_ESCAPE)) != NULL) 429 escape = *cp; 430 else 431 escape = ESCAPE; 432 hadintr = 0; 433 if (setjmp(reset_jmpbuf) == 0) { 434 if (getsub) 435 (void)grabh(hp, GSUBJECT); 436 } else { 437 /* 438 * Come here for printing the after-signal message. 439 * Duplicate messages won't be printed because 440 * the write is aborted if we get a SIGTTOU. 441 */ 442 cont: 443 if (hadintr) { 444 (void)fflush(stdout); 445 (void)fprintf(stderr, 446 "\n(Interrupt -- one more to kill letter)\n"); 447 } else { 448 (void)printf("(continue)\n"); 449 (void)fflush(stdout); 450 } 451 } 452 eofcount = 0; /* reset after possible longjmp */ 453 longline = 0; /* reset after possible longjmp */ 454 for (;;) { 455 reset_on_stop = 1; 456 c = readline(stdin, linebuf, LINESIZE, reset_on_stop); 457 reset_on_stop = 0; 458 459 if (c < 0) { 460 char *p; 461 462 if (value(ENAME_INTERACTIVE) != NULL && 463 (p = value(ENAME_IGNOREEOF)) != NULL && 464 ++eofcount < (*p == 0 ? 25 : atoi(p))) { 465 (void)printf("Use \".\" to terminate letter\n"); 466 continue; 467 } 468 break; 469 } 470 lastlong = longline; 471 longline = c == LINESIZE - 1; 472 eofcount = 0; 473 hadintr = 0; 474 if (linebuf[0] == '.' && linebuf[1] == '\0' && 475 value(ENAME_INTERACTIVE) != NULL && !lastlong && 476 (value(ENAME_DOT) != NULL || value(ENAME_IGNOREEOF) != NULL)) 477 break; 478 if (linebuf[0] != escape || value(ENAME_INTERACTIVE) == NULL || 479 lastlong) { 480 if (putline(collf, linebuf, !longline) < 0) 481 goto err; 482 continue; 483 } 484 c = linebuf[1]; 485 switch (c) { 486 default: 487 /* 488 * On double escape, just send the single one. 489 * Otherwise, it's an error. 490 */ 491 if (c == escape) { 492 if (putline(collf, &linebuf[1], !longline) < 0) 493 goto err; 494 else 495 break; 496 } 497 (void)printf("Unknown tilde escape.\n"); 498 break; 499 #ifdef MIME_SUPPORT 500 case '@': 501 hp->h_attach = mime_attach_files(hp->h_attach, &linebuf[2]); 502 break; 503 #endif 504 case 'C': 505 /* 506 * Dump core. 507 */ 508 (void)core(NULL); 509 break; 510 case '!': 511 /* 512 * Shell escape, send the balance of the 513 * line to sh -c. 514 */ 515 (void)shell(&linebuf[2]); 516 break; 517 case ':': 518 case '_': 519 /* 520 * Escape to command mode, but be nice! 521 */ 522 (void)execute(&linebuf[2], ec_composing); 523 goto cont; 524 case '.': 525 /* 526 * Simulate end of file on input. 527 */ 528 goto out; 529 case 'q': 530 /* 531 * Force a quit of sending mail. 532 * Act like an interrupt happened. 533 */ 534 hadintr++; 535 coll_int(SIGINT); 536 exit(1); 537 /*NOTREACHED*/ 538 539 case 'x': /* exit, do not save in dead.letter */ 540 goto err; 541 542 case 'h': 543 /* 544 * Grab a bunch of headers. 545 */ 546 (void)grabh(hp, GTO | GSUBJECT | GCC | GBCC | GSMOPTS); 547 goto cont; 548 case 't': 549 /* 550 * Add to the To list. 551 */ 552 hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO)); 553 break; 554 case 's': 555 /* 556 * Set the Subject list. 557 */ 558 cp = skip_WSP(&linebuf[2]); 559 hp->h_subject = savestr(cp); 560 break; 561 case 'c': 562 /* 563 * Add to the CC list. 564 */ 565 hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC)); 566 break; 567 case 'b': 568 /* 569 * Add stuff to blind carbon copies list. 570 */ 571 hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC)); 572 break; 573 case 'i': 574 case 'A': 575 case 'a': 576 /* 577 * Insert named variable in message 578 */ 579 580 switch(c) { 581 case 'i': 582 cp = skip_WSP(&linebuf[2]); 583 break; 584 case 'a': 585 cp = "sign"; 586 break; 587 case 'A': 588 cp = "Sign"; 589 break; 590 default: 591 goto err; 592 } 593 594 if (*cp && (cp = value(cp)) != NULL) { 595 (void)printf("%s\n", cp); 596 if (putline(collf, cp, 1) < 0) 597 goto err; 598 } 599 600 break; 601 602 case 'd': 603 (void)strcpy(linebuf + 2, getdeadletter()); 604 /* FALLTHROUGH */ 605 case 'r': 606 case '<': 607 /* 608 * Invoke a file: 609 * Search for the file name, 610 * then open it and copy the contents to collf. 611 */ 612 cp = skip_WSP(&linebuf[2]); 613 if (*cp == '\0') { 614 (void)printf("Interpolate what file?\n"); 615 break; 616 } 617 618 cp = expand(cp); 619 if (cp == NULL) 620 break; 621 622 if (*cp == '!') { /* insert stdout of command */ 623 const char *shellcmd; 624 int nullfd; 625 int rc2; 626 627 if ((nullfd = open("/dev/null", O_RDONLY, 0)) == -1) { 628 warn("/dev/null"); 629 break; 630 } 631 632 (void)snprintf(tempname, sizeof(tempname), 633 "%s/mail.ReXXXXXXXXXX", tmpdir); 634 if ((fd = mkstemp(tempname)) == -1 || 635 (fbuf = Fdopen(fd, "we+")) == NULL) { 636 if (fd != -1) 637 (void)close(fd); 638 warn("%s", tempname); 639 break; 640 } 641 (void)unlink(tempname); 642 643 if ((shellcmd = value(ENAME_SHELL)) == NULL) 644 shellcmd = _PATH_CSHELL; 645 646 rc2 = run_command(shellcmd, NULL, nullfd, fileno(fbuf), "-c", cp + 1, NULL); 647 648 (void)close(nullfd); 649 650 if (rc2 < 0) { 651 (void)Fclose(fbuf); 652 break; 653 } 654 655 if (fsize(fbuf) == 0) { 656 (void)fprintf(stderr, "No bytes from command \"%s\"\n", cp + 1); 657 (void)Fclose(fbuf); 658 break; 659 } 660 661 rewind(fbuf); 662 } 663 else if (isdir(cp)) { 664 (void)printf("%s: Directory\n", cp); 665 break; 666 } 667 else if ((fbuf = Fopen(cp, "re")) == NULL) { 668 warn("%s", cp); 669 break; 670 } 671 (void)printf("\"%s\" ", cp); 672 (void)fflush(stdout); 673 lc = 0; 674 cc = 0; 675 while ((rc = readline(fbuf, linebuf, LINESIZE, 0)) >= 0) { 676 if (rc != LINESIZE-1) lc++; 677 if ((t = putline(collf, linebuf, 678 rc != LINESIZE-1)) < 0) { 679 (void)Fclose(fbuf); 680 goto err; 681 } 682 cc += t; 683 } 684 (void)Fclose(fbuf); 685 (void)printf("%d/%d\n", lc, cc); 686 break; 687 case 'w': 688 /* 689 * Write the message on a file. 690 */ 691 cp = skip_WSP(&linebuf[2]); 692 if (*cp == '\0') { 693 (void)fprintf(stderr, "Write what file!?\n"); 694 break; 695 } 696 if ((cp = expand(cp)) == NULL) 697 break; 698 rewind(collf); 699 (void)exwrite(cp, collf, 1); 700 break; 701 case 'm': 702 case 'M': 703 case 'f': 704 case 'F': 705 /* 706 * Interpolate the named messages, if we 707 * are in receiving mail mode. Does the 708 * standard list processing garbage. 709 * If ~f is given, we don't shift over. 710 */ 711 if (interpolate(linebuf + 2, collf, mailtempname, c) < 0) 712 goto err; 713 goto cont; 714 case '?': 715 cathelp(_PATH_TILDE); 716 break; 717 case 'p': 718 /* 719 * Print out the current state of the 720 * message without altering anything. 721 */ 722 rewind(collf); 723 (void)printf("-------\nMessage contains:\n"); 724 (void)puthead(hp, stdout, 725 GTO | GSUBJECT | GCC | GBCC | GSMOPTS | GNL); 726 while ((t = getc(collf)) != EOF) 727 (void)putchar(t); 728 goto cont; 729 case '|': 730 /* 731 * Pipe message through command. 732 * Collect output as new message. 733 */ 734 rewind(collf); 735 mespipe(collf, &linebuf[2]); 736 goto cont; 737 case 'v': 738 case 'e': 739 /* 740 * Edit the current message. 741 * 'e' means to use EDITOR 742 * 'v' means to use VISUAL 743 */ 744 rewind(collf); 745 mesedit(collf, c); 746 goto cont; 747 } 748 } 749 goto out; 750 err: 751 if (collf != NULL) { 752 (void)Fclose(collf); 753 collf = NULL; 754 } 755 out: 756 if (collf != NULL) 757 rewind(collf); 758 noreset--; 759 760 sig_hold(); 761 (void)sig_signal(SIGINT, old_sigint); 762 (void)sig_signal(SIGHUP, old_sighup); 763 (void)sig_signal(SIGTSTP, old_sigtstp); 764 (void)sig_signal(SIGTTIN, old_sigttin); 765 (void)sig_signal(SIGTTOU, old_sigttou); 766 sig_release(); 767 768 sig_check(); 769 return collf; 770 } 771