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