Annotation of src/usr.bin/msgs/msgs.c, Revision 1.31
1.31 ! moritz 1: /* $OpenBSD: msgs.c,v 1.30 2005/07/04 01:54:10 djm 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.31 ! moritz 43: static char rcsid[] = "$OpenBSD: msgs.c,v 1.30 2005/07/04 01:54:10 djm 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))) {
1.31 ! moritz 365: struct passwd *pw;
! 366:
! 367: if ((pw = getpwuid(uid)) == NULL) {
! 368: perror("getpwuid");
! 369: exit(1);
! 370: }
1.1 deraadt 371: printf("Message %d:\nFrom %s %sSubject: ",
1.31 ! moritz 372: nextmsg, pw->pw_name, ctime(&t));
1.1 deraadt 373: fflush(stdout);
374: fgets(inbuf, sizeof inbuf, stdin);
375: putchar('\n');
376: fflush(stdout);
377: fprintf(newmsg, "From %s %sSubject: %s\n",
1.31 ! moritz 378: pw->pw_name, ctime(&t), inbuf);
1.1 deraadt 379: blankline = seensubj = YES;
1.29 deraadt 380: } else
1.1 deraadt 381: blankline = seensubj = NO;
382: for (;;) {
383: fgets(inbuf, sizeof inbuf, stdin);
384: if (feof(stdin) || ferror(stdin))
385: break;
386: blankline = (blankline || (inbuf[0] == '\n'));
1.29 deraadt 387: seensubj = (seensubj ||
388: (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
1.1 deraadt 389: fputs(inbuf, newmsg);
390: }
391: #ifdef OBJECT
392: if (!seensubj) {
393: printf("NOTICE: Messages should have a Subject field!\n");
394: #ifdef REJECT
395: unlink(fname);
396: #endif
397: exit(1);
398: }
399: #endif
400: exit(ferror(stdin));
401: }
402: if (clean)
403: exit(0);
404:
405: /*
406: * prepare to display messages
407: */
408: totty = (isatty(fileno(stdout)) != 0);
409: use_pager = use_pager && totty;
410:
1.17 millert 411: if ((cp = getenv("HOME")) == NULL || *cp == '\0') {
412: fprintf(stderr, "Error, no home directory!\n");
413: exit(1);
414: }
415: snprintf(fname, sizeof(fname), "%s/%s", cp, MSGSRC);
1.1 deraadt 416: msgsrc = fopen(fname, "r");
417: if (msgsrc) {
418: newrc = NO;
1.11 deraadt 419: fscanf(msgsrc, "%d\n", &nextmsg);
420: fclose(msgsrc);
421: if (nextmsg > lastmsg+1) {
1.1 deraadt 422: printf("Warning: bounds have been reset (%d, %d)\n",
1.29 deraadt 423: firstmsg, lastmsg);
1.1 deraadt 424: truncate(fname, (off_t)0);
425: newrc = YES;
1.29 deraadt 426: } else if (!rcfirst)
1.1 deraadt 427: rcfirst = nextmsg - rcback;
1.29 deraadt 428: } else
1.11 deraadt 429: newrc = YES;
430: msgsrc = fopen(fname, "r+");
431: if (msgsrc == NULL)
432: msgsrc = fopen(fname, "w");
1.1 deraadt 433: if (msgsrc == NULL) {
434: perror(fname);
435: exit(errno);
436: }
437: if (rcfirst) {
438: if (rcfirst > lastmsg+1) {
439: printf("Warning: the last message is number %d.\n",
1.29 deraadt 440: lastmsg);
1.1 deraadt 441: rcfirst = nextmsg;
442: }
443: if (rcfirst > firstmsg)
444: firstmsg = rcfirst; /* don't set below first msg */
445: }
446: if (newrc) {
447: nextmsg = firstmsg;
1.29 deraadt 448: fseeko(msgsrc, (off_t)0, SEEK_SET);
1.1 deraadt 449: fprintf(msgsrc, "%d\n", nextmsg);
450: fflush(msgsrc);
451: }
452:
453: if (totty) {
454: struct winsize win;
455: if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1)
456: Lpp = win.ws_row;
457: if (Lpp <= 0) {
1.10 downsj 458: char *ttype = getenv("TERM");
459:
460: if (ttype != (char *)NULL) {
461: if (tgetent(NULL, ttype) <= 0
462: || (Lpp = tgetnum("li")) <= 0) {
463: Lpp = NLINES;
464: }
465: } else
1.1 deraadt 466: Lpp = NLINES;
467: }
468: }
469: Lpp -= 6; /* for headers, etc. */
470:
471: already = NO;
472: prevmsg = firstmsg;
473: printing = YES;
474: if (ruptible)
475: signal(SIGINT, onintr);
476:
477: /*
478: * Main program loop
479: */
480: for (msg = firstmsg; msg <= lastmsg; msg++) {
481:
1.5 millert 482: snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, msg);
1.1 deraadt 483: newmsg = fopen(fname, "r");
484: if (newmsg == NULL)
485: continue;
486:
487: gfrsub(newmsg); /* get From and Subject fields */
488: if (locomode && !local) {
489: fclose(newmsg);
490: continue;
491: }
492:
493: if (qopt) { /* This has to be located here */
494: printf("There are new messages.\n");
495: exit(0);
496: }
497:
498: if (already && !hdrs)
499: putchar('\n');
500:
501: /*
502: * Print header
503: */
504: if (totty)
505: signal(SIGTSTP, onsusp);
506: (void) setjmp(tstpbuf);
507: already = YES;
508: nlines = 2;
509: if (seenfrom) {
510: printf("Message %d:\nFrom %s %s", msg, from, date);
511: nlines++;
512: }
513: if (seensubj) {
514: printf("Subject: %s", subj);
515: nlines++;
1.29 deraadt 516: } else {
1.1 deraadt 517: if (seenfrom) {
518: putchar('\n');
519: nlines++;
520: }
1.29 deraadt 521: while (nlines < 6 &&
522: fgets(inbuf, sizeof inbuf, newmsg) &&
523: inbuf[0] != '\n') {
1.1 deraadt 524: fputs(inbuf, stdout);
525: nlines++;
526: }
527: }
528:
529: lct = linecnt(newmsg);
530: if (lct)
531: printf("(%d%slines) ", lct, seensubj? " " : " more ");
532:
533: if (hdrs) {
534: printf("\n-----\n");
535: fclose(newmsg);
536: continue;
537: }
538:
539: /*
540: * Ask user for command
541: */
542: if (totty)
543: ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
544: else
545: inbuf[0] = 'y';
546: if (totty)
547: signal(SIGTSTP, SIG_DFL);
548: cmnd:
549: in = inbuf;
550: switch (*in) {
1.29 deraadt 551: case 'x':
552: case 'X':
553: exit(0);
1.1 deraadt 554:
1.29 deraadt 555: case 'q':
556: case 'Q':
557: quitit = YES;
558: printf("--Postponed--\n");
559: exit(0);
560: /* intentional fall-thru */
561: case 'n':
562: case 'N':
563: if (msg >= nextmsg)
564: sep = "Flushed";
565: prevmsg = msg;
566: break;
1.1 deraadt 567:
1.29 deraadt 568: case 'p':
569: case 'P':
570: use_pager = (*in++ == 'p');
571: /* intentional fallthru */
572: case '\n':
573: case 'y':
574: default:
575: if (*in == '-') {
576: msg = prevmsg-1;
577: sep = "replay";
578: break;
579: }
580: if (isdigit(*in)) {
581: msg = next(in, sizeof inbuf);
582: sep = in;
583: break;
584: }
1.1 deraadt 585:
1.29 deraadt 586: prmesg(nlines + lct + (seensubj? 1 : 0));
587: prevmsg = msg;
1.1 deraadt 588: }
589:
590: printf("--%s--\n", sep);
591: sep = "-";
592: if (msg >= nextmsg) {
593: nextmsg = msg + 1;
1.29 deraadt 594: fseeko(msgsrc, (off_t)0, SEEK_SET);
1.1 deraadt 595: fprintf(msgsrc, "%d\n", nextmsg);
596: fflush(msgsrc);
597: }
598: if (newmsg)
599: fclose(newmsg);
600: if (quitit)
601: break;
602: }
603:
604: /*
605: * Make sure .rc file gets updated
606: */
607: if (--msg >= nextmsg) {
608: nextmsg = msg + 1;
1.29 deraadt 609: fseeko(msgsrc, (off_t)0, SEEK_SET);
1.1 deraadt 610: fprintf(msgsrc, "%d\n", nextmsg);
611: fflush(msgsrc);
612: }
613: if (already && !quitit && lastcmd && totty) {
614: /*
615: * save or reply to last message?
616: */
617: msg = prevmsg;
618: ask(NOMORE);
619: if (inbuf[0] == '-' || isdigit(inbuf[0]))
620: goto cmnd;
621: }
622: if (!(already || hush || qopt))
623: printf("No new messages.\n");
624: exit(0);
625: }
626:
1.9 downsj 627: void
1.27 deraadt 628: prmesg(int length)
1.1 deraadt 629: {
630: FILE *outf;
631: char *env_pager;
632:
633: if (use_pager && length > Lpp) {
634: signal(SIGPIPE, SIG_IGN);
635: signal(SIGQUIT, SIG_IGN);
1.14 pjanzen 636: if ((env_pager = getenv("PAGER")) == NULL || *env_pager == '\0') {
1.11 deraadt 637: snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp);
638: } else {
1.16 deraadt 639: snprintf(cmdbuf, sizeof(cmdbuf), "%s", env_pager);
1.11 deraadt 640: }
1.1 deraadt 641: outf = popen(cmdbuf, "w");
642: if (!outf)
643: outf = stdout;
644: else
645: setbuf(outf, (char *)NULL);
646: }
647: else
648: outf = stdout;
649:
650: if (seensubj)
651: putc('\n', outf);
652:
653: while (fgets(inbuf, sizeof inbuf, newmsg)) {
654: fputs(inbuf, outf);
655: if (ferror(outf)) {
656: clearerr(outf);
657: break;
658: }
659: }
660:
661: if (outf != stdout) {
662: pclose(outf);
663: signal(SIGPIPE, SIG_DFL);
664: signal(SIGQUIT, SIG_DFL);
1.29 deraadt 665: } else {
1.1 deraadt 666: fflush(stdout);
667: }
668:
669: /* trick to force wait on output */
670: tcdrain(fileno(stdout));
671: }
672:
1.29 deraadt 673: /* ARGSUSED */
1.1 deraadt 674: void
1.29 deraadt 675: onintr(int signo)
1.1 deraadt 676: {
1.29 deraadt 677: int save_errno = errno;
678:
1.1 deraadt 679: signal(SIGINT, onintr);
680: if (mailing)
681: unlink(fname);
682: if (sending) {
683: unlink(fname);
1.29 deraadt 684: write(STDOUT_FILENO, "--Killed--\n", strlen("--Killed--\n"));
685: _exit(1);
1.1 deraadt 686: }
687: if (printing) {
1.29 deraadt 688: write(STDOUT_FILENO, "\n", 1);
1.1 deraadt 689: if (hdrs)
1.29 deraadt 690: _exit(0);
1.1 deraadt 691: sep = "Interrupt";
692: if (newmsg)
1.29 deraadt 693: fseeko(newmsg, (off_t)0, SEEK_END);
1.1 deraadt 694: intrpflg = YES;
695: }
1.29 deraadt 696: errno = save_errno;
1.1 deraadt 697: }
698:
699: /*
700: * We have just gotten a susp. Suspend and prepare to resume.
701: */
1.29 deraadt 702: /* ARGSUSED */
1.1 deraadt 703: void
1.29 deraadt 704: onsusp(int signo)
1.1 deraadt 705: {
1.29 deraadt 706: int save_errno = errno;
1.18 millert 707: sigset_t emptyset;
708:
1.1 deraadt 709: signal(SIGTSTP, SIG_DFL);
1.18 millert 710: sigemptyset(&emptyset);
711: sigprocmask(SIG_SETMASK, &emptyset, NULL);
1.1 deraadt 712: kill(0, SIGTSTP);
713: signal(SIGTSTP, onsusp);
1.29 deraadt 714: errno = save_errno;
715:
1.1 deraadt 716: if (!mailing)
1.20 art 717: longjmp(tstpbuf, 1);
1.1 deraadt 718: }
719:
1.9 downsj 720: int
1.27 deraadt 721: linecnt(FILE *f)
1.1 deraadt 722: {
1.29 deraadt 723: off_t oldpos = ftello(f);
724:
1.1 deraadt 725: int l = 0;
726: char lbuf[BUFSIZ];
727:
728: while (fgets(lbuf, sizeof lbuf, f))
729: l++;
730: clearerr(f);
1.29 deraadt 731: fseeko(f, oldpos, SEEK_SET);
1.1 deraadt 732: return (l);
733: }
734:
1.9 downsj 735: int
1.27 deraadt 736: next(char *buf, int len)
1.1 deraadt 737: {
738: int i;
739: sscanf(buf, "%d", &i);
1.24 deraadt 740: snprintf(buf, len, "Goto %d", i);
1.1 deraadt 741: return(--i);
742: }
743:
1.9 downsj 744: void
1.27 deraadt 745: ask(char *prompt)
1.1 deraadt 746: {
747: char inch;
1.2 deraadt 748: int n, cmsg, fd;
1.1 deraadt 749: off_t oldpos;
750: FILE *cpfrom, *cpto;
751:
752: printf("%s ", prompt);
753: fflush(stdout);
754: intrpflg = NO;
755: (void) fgets(inbuf, sizeof inbuf, stdin);
756: if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n')
757: inbuf[n - 1] = '\0';
758: if (intrpflg)
759: inbuf[0] = 'x';
760:
761: /*
762: * Handle 'mail' and 'save' here.
763: */
1.11 deraadt 764: if (((inch = inbuf[0]) == 's' || inch == 'm') && !restricted) {
1.1 deraadt 765: if (inbuf[1] == '-')
766: cmsg = prevmsg;
767: else if (isdigit(inbuf[1]))
768: cmsg = atoi(&inbuf[1]);
769: else
770: cmsg = msg;
1.5 millert 771: snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, cmsg);
1.1 deraadt 772:
1.29 deraadt 773: oldpos = ftello(newmsg);
1.1 deraadt 774:
775: cpfrom = fopen(fname, "r");
776: if (!cpfrom) {
777: printf("Message %d not found\n", cmsg);
778: ask (prompt);
779: return;
780: }
781:
782: if (inch == 's') {
783: in = nxtfld(inbuf);
784: if (*in) {
1.22 ho 785: for (n=0;
1.29 deraadt 786: in[n] > ' ' && n < sizeof fname - 1;
787: n++) {
1.1 deraadt 788: fname[n] = in[n];
789: }
790: fname[n] = NULL;
791: }
792: else
1.25 deraadt 793: strlcpy(fname, "Messages", sizeof fname);
1.22 ho 794: fd = open(fname, O_RDWR|O_EXCL|O_CREAT|O_APPEND, 0666);
1.29 deraadt 795: } else {
1.25 deraadt 796: strlcpy(fname, _PATH_TMPFILE, sizeof fname);
1.7 deraadt 797: fd = mkstemp(fname);
798: if (fd != -1) {
799: snprintf(cmdbuf, sizeof(cmdbuf), _PATH_MAIL, fname);
800: mailing = YES;
801: }
802: }
803: if (fd == -1 || (cpto = fdopen(fd, "a")) == NULL) {
804: if (fd != -1)
1.2 deraadt 805: close(fd);
1.1 deraadt 806: perror(fname);
807: mailing = NO;
1.29 deraadt 808: fseeko(newmsg, oldpos, SEEK_SET);
1.1 deraadt 809: ask(prompt);
810: return;
811: }
812:
1.9 downsj 813: while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom)))
1.1 deraadt 814: fwrite(inbuf, 1, n, cpto);
815:
816: fclose(cpfrom);
817: fclose(cpto);
1.29 deraadt 818: fseeko(newmsg, oldpos, SEEK_SET); /* reposition current message */
1.1 deraadt 819: if (inch == 's')
820: printf("Message %d saved in \"%s\"\n", cmsg, fname);
821: else {
822: system(cmdbuf);
823: unlink(fname);
824: mailing = NO;
825: }
826: ask(prompt);
827: }
828: }
829:
1.9 downsj 830: void
1.27 deraadt 831: gfrsub(FILE *infile)
1.1 deraadt 832: {
833: off_t frompos;
834:
835: seensubj = seenfrom = NO;
836: local = YES;
837: subj[0] = from[0] = date[0] = NULL;
838:
839: /*
840: * Is this a normal message?
841: */
842: if (fgets(inbuf, sizeof inbuf, infile)) {
843: if (strncmp(inbuf, "From", 4)==0) {
844: /*
845: * expected form starts with From
846: */
847: seenfrom = YES;
1.29 deraadt 848: frompos = ftello(infile);
1.1 deraadt 849: ptr = from;
850: in = nxtfld(inbuf);
1.8 deraadt 851: if (*in) {
852: while (*in && *in > ' ' &&
853: ptr - from < sizeof from -1) {
854: if (*in == ':' || *in == '@' || *in == '!')
855: local = NO;
856: *ptr++ = *in++;
857: }
1.1 deraadt 858: }
859: *ptr = NULL;
860: if (*(in = nxtfld(in)))
861: strncpy(date, in, sizeof date);
862: else {
863: date[0] = '\n';
864: date[1] = NULL;
865: }
1.29 deraadt 866: } else {
1.1 deraadt 867: /*
868: * not the expected form
869: */
1.29 deraadt 870: fseeko(infile, (off_t)0, SEEK_SET);
1.1 deraadt 871: return;
872: }
1.29 deraadt 873: } else
1.1 deraadt 874: /*
875: * empty file ?
876: */
877: return;
878:
879: /*
880: * look for Subject line until EOF or a blank line
881: */
1.29 deraadt 882: while (fgets(inbuf, sizeof inbuf, infile) &&
883: !(blankline = (inbuf[0] == '\n'))) {
1.1 deraadt 884: /*
885: * extract Subject line
886: */
887: if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
888: seensubj = YES;
1.29 deraadt 889: frompos = ftello(infile);
1.1 deraadt 890: strncpy(subj, nxtfld(inbuf), sizeof subj);
891: }
892: }
893: if (!blankline)
894: /*
895: * ran into EOF
896: */
1.29 deraadt 897: fseeko(infile, frompos, SEEK_SET);
1.1 deraadt 898:
899: if (!seensubj)
900: /*
901: * for possible use with Mail
902: */
903: strncpy(subj, "(No Subject)\n", sizeof subj);
904: }
905:
906: char *
1.27 deraadt 907: nxtfld(char *s)
1.1 deraadt 908: {
1.29 deraadt 909: if (*s)
910: while (*s && *s > ' ')
911: s++; /* skip over this field */
912: if (*s)
913: while (*s && *s <= ' ')
914: s++; /* find start of next field */
1.1 deraadt 915: return (s);
916: }