Annotation of src/usr.bin/msgs/msgs.c, Revision 1.30
1.30 ! djm 1: /* $OpenBSD: msgs.c,v 1.29 2004/10/02 04:14:39 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: msgs.c,v 1.7 1995/09/28 06:57:40 tls Exp $ */
3:
4: /*-
5: * Copyright (c) 1980, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.26 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #ifndef lint
34: static char copyright[] =
35: "@(#) Copyright (c) 1980, 1993\n\
36: The Regents of the University of California. All rights reserved.\n";
37: #endif /* not lint */
38:
39: #ifndef lint
40: #if 0
41: static char sccsid[] = "@(#)msgs.c 8.2 (Berkeley) 4/28/95";
42: #else
1.30 ! djm 43: static char rcsid[] = "$OpenBSD: msgs.c,v 1.29 2004/10/02 04:14:39 deraadt Exp $";
1.1 deraadt 44: #endif
45: #endif /* not lint */
46:
47: /*
48: * msgs - a user bulletin board program
49: *
50: * usage:
51: * msgs [fhlopqr] [[-]number] to read messages
52: * msgs -s to place messages
53: * msgs -c [-days] to clean up the bulletin board
54: *
55: * prompt commands are:
56: * y print message
57: * n flush message, go to next message
58: * q flush message, quit
59: * p print message, turn on 'pipe thru more' mode
60: * P print message, turn off 'pipe thru more' mode
61: * - reprint last message
62: * s[-][<num>] [<filename>] save message
63: * m[-][<num>] mail with message in temp mbox
64: * x exit without flushing this message
65: * <num> print message number <num>
66: */
67:
68: #define OBJECT /* will object to messages without Subjects */
69: #define REJECT /* will reject messages without Subjects
70: (OBJECT must be defined also) */
1.9 downsj 71: #undef UNBUFFERED /* use unbuffered output */
1.1 deraadt 72:
73: #include <sys/param.h>
74: #include <sys/ioctl.h>
75: #include <sys/stat.h>
76: #include <dirent.h>
77: #include <ctype.h>
78: #include <errno.h>
1.15 millert 79: #include <fcntl.h>
1.1 deraadt 80: #include <pwd.h>
81: #include <setjmp.h>
82: #include <signal.h>
83: #include <stdio.h>
84: #include <stdlib.h>
85: #include <string.h>
1.9 downsj 86: #include <term.h>
1.1 deraadt 87: #include <termios.h>
88: #include <time.h>
89: #include <unistd.h>
90: #include "pathnames.h"
91:
92: #define CMODE 0664 /* bounds file creation mode */
93: #define NO 0
94: #define YES 1
95: #define SUPERUSER 0 /* superuser uid */
96: #define DAEMON 1 /* daemon uid */
97: #define NLINES 24 /* default number of lines/crt screen */
98: #define NDAYS 21 /* default keep time for messages */
99: #define DAYS *24*60*60 /* seconds/day */
100: #define MSGSRC ".msgsrc" /* user's rc file */
101: #define BOUNDS "bounds" /* message bounds file */
102: #define NEXT "Next message? [yq]"
103: #define MORE "More? [ynq]"
104: #define NOMORE "(No more) [q] ?"
105:
106: typedef char bool;
107:
108: FILE *msgsrc;
109: FILE *newmsg;
110: char *sep = "-";
111: char inbuf[BUFSIZ];
1.8 deraadt 112: char fname[MAXPATHLEN];
113: char cmdbuf[MAXPATHLEN + MAXPATHLEN];
1.1 deraadt 114: char subj[128];
115: char from[128];
116: char date[128];
117: char *ptr;
118: char *in;
119: bool local;
120: bool ruptible;
121: bool totty;
122: bool seenfrom;
123: bool seensubj;
124: bool blankline;
125: bool printing = NO;
126: bool mailing = NO;
127: bool quitit = NO;
128: bool sending = NO;
129: bool intrpflg = NO;
130: bool restricted = NO;
131: int uid;
132: int msg;
133: int prevmsg;
134: int lct;
135: int nlines;
136: int Lpp = 0;
137: time_t t;
138: time_t keep;
139:
1.21 millert 140: void prmesg(int);
141: void onintr(int);
142: void onsusp(int);
143: int linecnt(FILE *);
1.24 deraadt 144: int next(char *, int);
1.21 millert 145: void ask(char *);
146: void gfrsub(FILE *);
147: char *nxtfld(char *);
1.1 deraadt 148:
149: /* option initialization */
150: bool hdrs = NO;
151: bool qopt = NO;
152: bool hush = NO;
153: bool send_msg = NO;
154: bool locomode = NO;
155: bool use_pager = NO;
156: bool clean = NO;
157: bool lastcmd = NO;
158: jmp_buf tstpbuf;
159:
1.9 downsj 160: int
1.27 deraadt 161: main(int argc, char *argv[])
1.1 deraadt 162: {
163: bool newrc, already;
164: int rcfirst = 0; /* first message to print (from .rc) */
165: int rcback = 0; /* amount to back off of rcfirst */
166: int firstmsg, nextmsg, lastmsg = 0;
167: int blast = 0;
168: FILE *bounds;
1.17 millert 169: char *cp;
1.1 deraadt 170:
171: #ifdef UNBUFFERED
172: setbuf(stdout, NULL);
173: #endif
174:
175: time(&t);
1.30 ! djm 176: uid = getuid();
! 177: if (setresuid(uid, uid, uid) == -1) {
! 178: perror("setresuid");
! 179: exit(1);
! 180: }
1.1 deraadt 181: ruptible = (signal(SIGINT, SIG_IGN) == SIG_DFL);
182: if (ruptible)
183: signal(SIGINT, SIG_DFL);
184:
185: argc--, argv++;
186: while (argc > 0) {
187: if (isdigit(argv[0][0])) { /* starting message # */
188: rcfirst = atoi(argv[0]);
1.29 deraadt 189: } else if (isdigit(argv[0][1])) { /* backward offset */
190: rcback = atoi(&(argv[0][1]));
191: } else {
1.1 deraadt 192: ptr = *argv;
1.29 deraadt 193: while (*ptr) {
194: switch (*ptr++) {
195: case '-':
196: break;
197: case 'c':
198: if (uid != SUPERUSER && uid != DAEMON) {
199: fprintf(stderr, "Sorry\n");
200: exit(1);
201: }
202: clean = YES;
203: break;
204: case 'f': /* silently */
205: hush = YES;
206: break;
207: case 'h': /* headers only */
208: hdrs = YES;
209: break;
210: case 'l': /* local msgs only */
211: locomode = YES;
212: break;
213: case 'o': /* option to save last message */
214: lastcmd = YES;
215: break;
216: case 'p': /* pipe thru 'more' during long msgs */
217: use_pager = YES;
218: break;
219: case 'q': /* query only */
220: qopt = YES;
221: break;
222: case 'r': /* restricted */
223: restricted = YES;
224: break;
225: case 's': /* sending TO msgs */
226: send_msg = YES;
227: break;
228: default:
229: fprintf(stderr,
230: "usage: msgs [fhlopqr] [[-]number]\n"
231: " msgs [-s]\n"
232: " msgs [-c [-days]]\n");
1.1 deraadt 233: exit(1);
234: }
235: }
236: }
237: argc--, argv++;
238: }
239:
240: /*
241: * determine current message bounds
242: */
1.5 millert 243: snprintf(fname, sizeof(fname), "%s/%s", _PATH_MSGS, BOUNDS);
1.1 deraadt 244: bounds = fopen(fname, "r");
245:
1.12 deraadt 246: if (bounds == NULL) {
1.13 aaron 247: if (errno == ENOENT) {
248: if ((bounds = fopen(fname, "w+")) == NULL) {
249: perror(fname);
250: exit(1);
251: }
252: fprintf(bounds, "1 0\n");
253: rewind(bounds);
1.29 deraadt 254: } else {
1.13 aaron 255: perror(fname);
256: exit(1);
257: }
1.1 deraadt 258: }
1.12 deraadt 259:
260: fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
261: fclose(bounds);
262: blast = lastmsg; /* save upper bound */
1.1 deraadt 263:
264: if (clean)
265: keep = t - (rcback? rcback : NDAYS) DAYS;
266:
267: if (clean || bounds == NULL) { /* relocate message bounds */
268: struct dirent *dp;
269: struct stat stbuf;
270: bool seenany = NO;
271: DIR *dirp;
272:
273: dirp = opendir(_PATH_MSGS);
274: if (dirp == NULL) {
275: perror(_PATH_MSGS);
276: exit(errno);
277: }
278: chmod(fname, CMODE);
279:
280: firstmsg = 32767;
281: lastmsg = 0;
282:
283: for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
1.19 mpech 284: int i = 0;
1.1 deraadt 285:
1.17 millert 286: cp = dp->d_name;
1.1 deraadt 287: if (dp->d_ino == 0)
288: continue;
289: if (dp->d_namlen == 0)
290: continue;
291:
292: if (clean)
1.5 millert 293: snprintf(inbuf, sizeof(inbuf), "%s/%s",
1.29 deraadt 294: _PATH_MSGS, cp);
1.1 deraadt 295:
296: while (isdigit(*cp))
297: i = i * 10 + *cp++ - '0';
298: if (*cp)
299: continue; /* not a message! */
300:
301: if (clean) {
302: if (stat(inbuf, &stbuf) != 0)
303: continue;
1.29 deraadt 304: if (stbuf.st_mtime < keep &&
305: stbuf.st_mode&S_IWRITE) {
1.1 deraadt 306: unlink(inbuf);
307: continue;
308: }
309: }
310:
311: if (i > lastmsg)
312: lastmsg = i;
313: if (i < firstmsg)
314: firstmsg = i;
315: seenany = YES;
316: }
317: closedir(dirp);
318:
319: if (!seenany) {
320: if (blast != 0) /* never lower the upper bound! */
321: lastmsg = blast;
322: firstmsg = lastmsg + 1;
1.29 deraadt 323: } else if (blast > lastmsg)
1.1 deraadt 324: lastmsg = blast;
325:
326: if (!send_msg) {
327: bounds = fopen(fname, "w");
328: if (bounds == NULL) {
329: perror(fname);
330: exit(errno);
331: }
332: chmod(fname, CMODE);
333: fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
334: fclose(bounds);
335: }
336: }
337:
338: if (send_msg) {
339: /*
340: * Send mode - place msgs in _PATH_MSGS
341: */
342: bounds = fopen(fname, "w");
343: if (bounds == NULL) {
344: perror(fname);
345: exit(errno);
346: }
347:
348: nextmsg = lastmsg + 1;
1.5 millert 349: snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, nextmsg);
1.1 deraadt 350: newmsg = fopen(fname, "w");
351: if (newmsg == NULL) {
352: perror(fname);
353: exit(errno);
354: }
355: chmod(fname, 0644);
356:
357: fprintf(bounds, "%d %d\n", firstmsg, nextmsg);
358: fclose(bounds);
359:
360: sending = YES;
361: if (ruptible)
362: signal(SIGINT, onintr);
363:
364: if (isatty(fileno(stdin))) {
365: ptr = getpwuid(uid)->pw_name;
366: printf("Message %d:\nFrom %s %sSubject: ",
1.29 deraadt 367: nextmsg, ptr, ctime(&t));
1.1 deraadt 368: fflush(stdout);
369: fgets(inbuf, sizeof inbuf, stdin);
370: putchar('\n');
371: fflush(stdout);
372: fprintf(newmsg, "From %s %sSubject: %s\n",
1.29 deraadt 373: ptr, ctime(&t), inbuf);
1.1 deraadt 374: blankline = seensubj = YES;
1.29 deraadt 375: } else
1.1 deraadt 376: blankline = seensubj = NO;
377: for (;;) {
378: fgets(inbuf, sizeof inbuf, stdin);
379: if (feof(stdin) || ferror(stdin))
380: break;
381: blankline = (blankline || (inbuf[0] == '\n'));
1.29 deraadt 382: seensubj = (seensubj ||
383: (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
1.1 deraadt 384: fputs(inbuf, newmsg);
385: }
386: #ifdef OBJECT
387: if (!seensubj) {
388: printf("NOTICE: Messages should have a Subject field!\n");
389: #ifdef REJECT
390: unlink(fname);
391: #endif
392: exit(1);
393: }
394: #endif
395: exit(ferror(stdin));
396: }
397: if (clean)
398: exit(0);
399:
400: /*
401: * prepare to display messages
402: */
403: totty = (isatty(fileno(stdout)) != 0);
404: use_pager = use_pager && totty;
405:
1.17 millert 406: if ((cp = getenv("HOME")) == NULL || *cp == '\0') {
407: fprintf(stderr, "Error, no home directory!\n");
408: exit(1);
409: }
410: snprintf(fname, sizeof(fname), "%s/%s", cp, MSGSRC);
1.1 deraadt 411: msgsrc = fopen(fname, "r");
412: if (msgsrc) {
413: newrc = NO;
1.11 deraadt 414: fscanf(msgsrc, "%d\n", &nextmsg);
415: fclose(msgsrc);
416: if (nextmsg > lastmsg+1) {
1.1 deraadt 417: printf("Warning: bounds have been reset (%d, %d)\n",
1.29 deraadt 418: firstmsg, lastmsg);
1.1 deraadt 419: truncate(fname, (off_t)0);
420: newrc = YES;
1.29 deraadt 421: } else if (!rcfirst)
1.1 deraadt 422: rcfirst = nextmsg - rcback;
1.29 deraadt 423: } else
1.11 deraadt 424: newrc = YES;
425: msgsrc = fopen(fname, "r+");
426: if (msgsrc == NULL)
427: msgsrc = fopen(fname, "w");
1.1 deraadt 428: if (msgsrc == NULL) {
429: perror(fname);
430: exit(errno);
431: }
432: if (rcfirst) {
433: if (rcfirst > lastmsg+1) {
434: printf("Warning: the last message is number %d.\n",
1.29 deraadt 435: lastmsg);
1.1 deraadt 436: rcfirst = nextmsg;
437: }
438: if (rcfirst > firstmsg)
439: firstmsg = rcfirst; /* don't set below first msg */
440: }
441: if (newrc) {
442: nextmsg = firstmsg;
1.29 deraadt 443: fseeko(msgsrc, (off_t)0, SEEK_SET);
1.1 deraadt 444: fprintf(msgsrc, "%d\n", nextmsg);
445: fflush(msgsrc);
446: }
447:
448: if (totty) {
449: struct winsize win;
450: if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1)
451: Lpp = win.ws_row;
452: if (Lpp <= 0) {
1.10 downsj 453: char *ttype = getenv("TERM");
454:
455: if (ttype != (char *)NULL) {
456: if (tgetent(NULL, ttype) <= 0
457: || (Lpp = tgetnum("li")) <= 0) {
458: Lpp = NLINES;
459: }
460: } else
1.1 deraadt 461: Lpp = NLINES;
462: }
463: }
464: Lpp -= 6; /* for headers, etc. */
465:
466: already = NO;
467: prevmsg = firstmsg;
468: printing = YES;
469: if (ruptible)
470: signal(SIGINT, onintr);
471:
472: /*
473: * Main program loop
474: */
475: for (msg = firstmsg; msg <= lastmsg; msg++) {
476:
1.5 millert 477: snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, msg);
1.1 deraadt 478: newmsg = fopen(fname, "r");
479: if (newmsg == NULL)
480: continue;
481:
482: gfrsub(newmsg); /* get From and Subject fields */
483: if (locomode && !local) {
484: fclose(newmsg);
485: continue;
486: }
487:
488: if (qopt) { /* This has to be located here */
489: printf("There are new messages.\n");
490: exit(0);
491: }
492:
493: if (already && !hdrs)
494: putchar('\n');
495:
496: /*
497: * Print header
498: */
499: if (totty)
500: signal(SIGTSTP, onsusp);
501: (void) setjmp(tstpbuf);
502: already = YES;
503: nlines = 2;
504: if (seenfrom) {
505: printf("Message %d:\nFrom %s %s", msg, from, date);
506: nlines++;
507: }
508: if (seensubj) {
509: printf("Subject: %s", subj);
510: nlines++;
1.29 deraadt 511: } else {
1.1 deraadt 512: if (seenfrom) {
513: putchar('\n');
514: nlines++;
515: }
1.29 deraadt 516: while (nlines < 6 &&
517: fgets(inbuf, sizeof inbuf, newmsg) &&
518: inbuf[0] != '\n') {
1.1 deraadt 519: fputs(inbuf, stdout);
520: nlines++;
521: }
522: }
523:
524: lct = linecnt(newmsg);
525: if (lct)
526: printf("(%d%slines) ", lct, seensubj? " " : " more ");
527:
528: if (hdrs) {
529: printf("\n-----\n");
530: fclose(newmsg);
531: continue;
532: }
533:
534: /*
535: * Ask user for command
536: */
537: if (totty)
538: ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
539: else
540: inbuf[0] = 'y';
541: if (totty)
542: signal(SIGTSTP, SIG_DFL);
543: cmnd:
544: in = inbuf;
545: switch (*in) {
1.29 deraadt 546: case 'x':
547: case 'X':
548: exit(0);
1.1 deraadt 549:
1.29 deraadt 550: case 'q':
551: case 'Q':
552: quitit = YES;
553: printf("--Postponed--\n");
554: exit(0);
555: /* intentional fall-thru */
556: case 'n':
557: case 'N':
558: if (msg >= nextmsg)
559: sep = "Flushed";
560: prevmsg = msg;
561: break;
1.1 deraadt 562:
1.29 deraadt 563: case 'p':
564: case 'P':
565: use_pager = (*in++ == 'p');
566: /* intentional fallthru */
567: case '\n':
568: case 'y':
569: default:
570: if (*in == '-') {
571: msg = prevmsg-1;
572: sep = "replay";
573: break;
574: }
575: if (isdigit(*in)) {
576: msg = next(in, sizeof inbuf);
577: sep = in;
578: break;
579: }
1.1 deraadt 580:
1.29 deraadt 581: prmesg(nlines + lct + (seensubj? 1 : 0));
582: prevmsg = msg;
1.1 deraadt 583: }
584:
585: printf("--%s--\n", sep);
586: sep = "-";
587: if (msg >= nextmsg) {
588: nextmsg = msg + 1;
1.29 deraadt 589: fseeko(msgsrc, (off_t)0, SEEK_SET);
1.1 deraadt 590: fprintf(msgsrc, "%d\n", nextmsg);
591: fflush(msgsrc);
592: }
593: if (newmsg)
594: fclose(newmsg);
595: if (quitit)
596: break;
597: }
598:
599: /*
600: * Make sure .rc file gets updated
601: */
602: if (--msg >= nextmsg) {
603: nextmsg = msg + 1;
1.29 deraadt 604: fseeko(msgsrc, (off_t)0, SEEK_SET);
1.1 deraadt 605: fprintf(msgsrc, "%d\n", nextmsg);
606: fflush(msgsrc);
607: }
608: if (already && !quitit && lastcmd && totty) {
609: /*
610: * save or reply to last message?
611: */
612: msg = prevmsg;
613: ask(NOMORE);
614: if (inbuf[0] == '-' || isdigit(inbuf[0]))
615: goto cmnd;
616: }
617: if (!(already || hush || qopt))
618: printf("No new messages.\n");
619: exit(0);
620: }
621:
1.9 downsj 622: void
1.27 deraadt 623: prmesg(int length)
1.1 deraadt 624: {
625: FILE *outf;
626: char *env_pager;
627:
628: if (use_pager && length > Lpp) {
629: signal(SIGPIPE, SIG_IGN);
630: signal(SIGQUIT, SIG_IGN);
1.14 pjanzen 631: if ((env_pager = getenv("PAGER")) == NULL || *env_pager == '\0') {
1.11 deraadt 632: snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp);
633: } else {
1.16 deraadt 634: snprintf(cmdbuf, sizeof(cmdbuf), "%s", env_pager);
1.11 deraadt 635: }
1.1 deraadt 636: outf = popen(cmdbuf, "w");
637: if (!outf)
638: outf = stdout;
639: else
640: setbuf(outf, (char *)NULL);
641: }
642: else
643: outf = stdout;
644:
645: if (seensubj)
646: putc('\n', outf);
647:
648: while (fgets(inbuf, sizeof inbuf, newmsg)) {
649: fputs(inbuf, outf);
650: if (ferror(outf)) {
651: clearerr(outf);
652: break;
653: }
654: }
655:
656: if (outf != stdout) {
657: pclose(outf);
658: signal(SIGPIPE, SIG_DFL);
659: signal(SIGQUIT, SIG_DFL);
1.29 deraadt 660: } else {
1.1 deraadt 661: fflush(stdout);
662: }
663:
664: /* trick to force wait on output */
665: tcdrain(fileno(stdout));
666: }
667:
1.29 deraadt 668: /* ARGSUSED */
1.1 deraadt 669: void
1.29 deraadt 670: onintr(int signo)
1.1 deraadt 671: {
1.29 deraadt 672: int save_errno = errno;
673:
1.1 deraadt 674: signal(SIGINT, onintr);
675: if (mailing)
676: unlink(fname);
677: if (sending) {
678: unlink(fname);
1.29 deraadt 679: write(STDOUT_FILENO, "--Killed--\n", strlen("--Killed--\n"));
680: _exit(1);
1.1 deraadt 681: }
682: if (printing) {
1.29 deraadt 683: write(STDOUT_FILENO, "\n", 1);
1.1 deraadt 684: if (hdrs)
1.29 deraadt 685: _exit(0);
1.1 deraadt 686: sep = "Interrupt";
687: if (newmsg)
1.29 deraadt 688: fseeko(newmsg, (off_t)0, SEEK_END);
1.1 deraadt 689: intrpflg = YES;
690: }
1.29 deraadt 691: errno = save_errno;
1.1 deraadt 692: }
693:
694: /*
695: * We have just gotten a susp. Suspend and prepare to resume.
696: */
1.29 deraadt 697: /* ARGSUSED */
1.1 deraadt 698: void
1.29 deraadt 699: onsusp(int signo)
1.1 deraadt 700: {
1.29 deraadt 701: int save_errno = errno;
1.18 millert 702: sigset_t emptyset;
703:
1.1 deraadt 704: signal(SIGTSTP, SIG_DFL);
1.18 millert 705: sigemptyset(&emptyset);
706: sigprocmask(SIG_SETMASK, &emptyset, NULL);
1.1 deraadt 707: kill(0, SIGTSTP);
708: signal(SIGTSTP, onsusp);
1.29 deraadt 709: errno = save_errno;
710:
1.1 deraadt 711: if (!mailing)
1.20 art 712: longjmp(tstpbuf, 1);
1.1 deraadt 713: }
714:
1.9 downsj 715: int
1.27 deraadt 716: linecnt(FILE *f)
1.1 deraadt 717: {
1.29 deraadt 718: off_t oldpos = ftello(f);
719:
1.1 deraadt 720: int l = 0;
721: char lbuf[BUFSIZ];
722:
723: while (fgets(lbuf, sizeof lbuf, f))
724: l++;
725: clearerr(f);
1.29 deraadt 726: fseeko(f, oldpos, SEEK_SET);
1.1 deraadt 727: return (l);
728: }
729:
1.9 downsj 730: int
1.27 deraadt 731: next(char *buf, int len)
1.1 deraadt 732: {
733: int i;
734: sscanf(buf, "%d", &i);
1.24 deraadt 735: snprintf(buf, len, "Goto %d", i);
1.1 deraadt 736: return(--i);
737: }
738:
1.9 downsj 739: void
1.27 deraadt 740: ask(char *prompt)
1.1 deraadt 741: {
742: char inch;
1.2 deraadt 743: int n, cmsg, fd;
1.1 deraadt 744: off_t oldpos;
745: FILE *cpfrom, *cpto;
746:
747: printf("%s ", prompt);
748: fflush(stdout);
749: intrpflg = NO;
750: (void) fgets(inbuf, sizeof inbuf, stdin);
751: if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n')
752: inbuf[n - 1] = '\0';
753: if (intrpflg)
754: inbuf[0] = 'x';
755:
756: /*
757: * Handle 'mail' and 'save' here.
758: */
1.11 deraadt 759: if (((inch = inbuf[0]) == 's' || inch == 'm') && !restricted) {
1.1 deraadt 760: if (inbuf[1] == '-')
761: cmsg = prevmsg;
762: else if (isdigit(inbuf[1]))
763: cmsg = atoi(&inbuf[1]);
764: else
765: cmsg = msg;
1.5 millert 766: snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, cmsg);
1.1 deraadt 767:
1.29 deraadt 768: oldpos = ftello(newmsg);
1.1 deraadt 769:
770: cpfrom = fopen(fname, "r");
771: if (!cpfrom) {
772: printf("Message %d not found\n", cmsg);
773: ask (prompt);
774: return;
775: }
776:
777: if (inch == 's') {
778: in = nxtfld(inbuf);
779: if (*in) {
1.22 ho 780: for (n=0;
1.29 deraadt 781: in[n] > ' ' && n < sizeof fname - 1;
782: n++) {
1.1 deraadt 783: fname[n] = in[n];
784: }
785: fname[n] = NULL;
786: }
787: else
1.25 deraadt 788: strlcpy(fname, "Messages", sizeof fname);
1.22 ho 789: fd = open(fname, O_RDWR|O_EXCL|O_CREAT|O_APPEND, 0666);
1.29 deraadt 790: } else {
1.25 deraadt 791: strlcpy(fname, _PATH_TMPFILE, sizeof fname);
1.7 deraadt 792: fd = mkstemp(fname);
793: if (fd != -1) {
794: snprintf(cmdbuf, sizeof(cmdbuf), _PATH_MAIL, fname);
795: mailing = YES;
796: }
797: }
798: if (fd == -1 || (cpto = fdopen(fd, "a")) == NULL) {
799: if (fd != -1)
1.2 deraadt 800: close(fd);
1.1 deraadt 801: perror(fname);
802: mailing = NO;
1.29 deraadt 803: fseeko(newmsg, oldpos, SEEK_SET);
1.1 deraadt 804: ask(prompt);
805: return;
806: }
807:
1.9 downsj 808: while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom)))
1.1 deraadt 809: fwrite(inbuf, 1, n, cpto);
810:
811: fclose(cpfrom);
812: fclose(cpto);
1.29 deraadt 813: fseeko(newmsg, oldpos, SEEK_SET); /* reposition current message */
1.1 deraadt 814: if (inch == 's')
815: printf("Message %d saved in \"%s\"\n", cmsg, fname);
816: else {
817: system(cmdbuf);
818: unlink(fname);
819: mailing = NO;
820: }
821: ask(prompt);
822: }
823: }
824:
1.9 downsj 825: void
1.27 deraadt 826: gfrsub(FILE *infile)
1.1 deraadt 827: {
828: off_t frompos;
829:
830: seensubj = seenfrom = NO;
831: local = YES;
832: subj[0] = from[0] = date[0] = NULL;
833:
834: /*
835: * Is this a normal message?
836: */
837: if (fgets(inbuf, sizeof inbuf, infile)) {
838: if (strncmp(inbuf, "From", 4)==0) {
839: /*
840: * expected form starts with From
841: */
842: seenfrom = YES;
1.29 deraadt 843: frompos = ftello(infile);
1.1 deraadt 844: ptr = from;
845: in = nxtfld(inbuf);
1.8 deraadt 846: if (*in) {
847: while (*in && *in > ' ' &&
848: ptr - from < sizeof from -1) {
849: if (*in == ':' || *in == '@' || *in == '!')
850: local = NO;
851: *ptr++ = *in++;
852: }
1.1 deraadt 853: }
854: *ptr = NULL;
855: if (*(in = nxtfld(in)))
856: strncpy(date, in, sizeof date);
857: else {
858: date[0] = '\n';
859: date[1] = NULL;
860: }
1.29 deraadt 861: } else {
1.1 deraadt 862: /*
863: * not the expected form
864: */
1.29 deraadt 865: fseeko(infile, (off_t)0, SEEK_SET);
1.1 deraadt 866: return;
867: }
1.29 deraadt 868: } else
1.1 deraadt 869: /*
870: * empty file ?
871: */
872: return;
873:
874: /*
875: * look for Subject line until EOF or a blank line
876: */
1.29 deraadt 877: while (fgets(inbuf, sizeof inbuf, infile) &&
878: !(blankline = (inbuf[0] == '\n'))) {
1.1 deraadt 879: /*
880: * extract Subject line
881: */
882: if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
883: seensubj = YES;
1.29 deraadt 884: frompos = ftello(infile);
1.1 deraadt 885: strncpy(subj, nxtfld(inbuf), sizeof subj);
886: }
887: }
888: if (!blankline)
889: /*
890: * ran into EOF
891: */
1.29 deraadt 892: fseeko(infile, frompos, SEEK_SET);
1.1 deraadt 893:
894: if (!seensubj)
895: /*
896: * for possible use with Mail
897: */
898: strncpy(subj, "(No Subject)\n", sizeof subj);
899: }
900:
901: char *
1.27 deraadt 902: nxtfld(char *s)
1.1 deraadt 903: {
1.29 deraadt 904: if (*s)
905: while (*s && *s > ' ')
906: s++; /* skip over this field */
907: if (*s)
908: while (*s && *s <= ' ')
909: s++; /* find start of next field */
1.1 deraadt 910: return (s);
911: }