1 /*	$OpenBSD: tipout.c,v 1.12 2005/04/11 19:59:07 deraadt Exp $	*/
2 /*	$NetBSD: tipout.c,v 1.5 1996/12/29 10:34:12 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 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.
16  * 3. Neither the name of the University nor the names of its contributors
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 #if 0
35 static char sccsid[] = "@(#)tipout.c	8.1 (Berkeley) 6/6/93";
36 #endif
37 static const char rcsid[] = "$OpenBSD: tipout.c,v 1.12 2005/04/11 19:59:07 deraadt Exp $";
38 #endif /* not lint */
39 
40 #include "tip.h"
41 /*
42  * tip
43  *
44  * lower fork of tip -- handles passive side
45  *  reading from the remote host
46  */
47 
48 static	jmp_buf sigbuf;
49 
50 /*
51  * TIPOUT wait state routine --
52  *   sent by TIPIN when it wants to posses the remote host
53  */
54 void
intIOT()55 intIOT()
56 {
57 
58 	write(repdes[1],&ccc,1);
59 	read(fildes[0], &ccc,1);
60 	longjmp(sigbuf, 1);
61 }
62 
63 /*
64  * Scripting command interpreter --
65  *  accepts script file name over the pipe and acts accordingly
66  */
67 void
intEMT()68 intEMT()
69 {
70 	char c, line[256];
71 	char *pline = line;
72 	char reply;
73 
74 	read(fildes[0], &c, 1);
75 	while (c != '\n' && pline - line < sizeof(line)) {
76 		*pline++ = c;
77 		read(fildes[0], &c, 1);
78 	}
79 	*pline = '\0';
80 	if (boolean(value(SCRIPT)) && fscript != NULL)
81 		fclose(fscript);
82 	if (pline == line) {
83 		setboolean(value(SCRIPT), FALSE);
84 		reply = 'y';
85 	} else {
86 		if ((fscript = fopen(line, "a")) == NULL)
87 			reply = 'n';
88 		else {
89 			reply = 'y';
90 			setboolean(value(SCRIPT), TRUE);
91 		}
92 	}
93 	write(repdes[1], &reply, 1);
94 	longjmp(sigbuf, 1);
95 }
96 
97 void
intTERM(int signo)98 intTERM(int signo)
99 {
100 
101 	if (boolean(value(SCRIPT)) && fscript != NULL)
102 		fclose(fscript);
103 	if (signo && tipin_pid)
104 		kill(tipin_pid, signo);
105 	exit(0);
106 }
107 
108 void
intSYS()109 intSYS()
110 {
111 
112 	setboolean(value(BEAUTIFY), !boolean(value(BEAUTIFY)));
113 	longjmp(sigbuf, 1);
114 }
115 
116 /*
117  * ****TIPOUT   TIPOUT****
118  */
119 void
tipout()120 tipout()
121 {
122 	char buf[BUFSIZ];
123 	char *cp;
124 	int cnt;
125 	sigset_t mask, omask;
126 
127 	signal(SIGINT, SIG_IGN);
128 	signal(SIGQUIT, SIG_IGN);
129 	signal(SIGEMT, intEMT);		/* attention from TIPIN */
130 	signal(SIGTERM, intTERM);	/* time to go signal */
131 	signal(SIGIOT, intIOT);		/* scripting going on signal */
132 	signal(SIGHUP, intTERM);	/* for dial-ups */
133 	signal(SIGSYS, intSYS);		/* beautify toggle */
134 	(void) setjmp(sigbuf);
135 	sigprocmask(SIG_BLOCK, NULL, &omask);
136 	for (;;) {
137 		sigprocmask(SIG_SETMASK, &omask, NULL);
138 		cnt = read(FD, buf, BUFSIZ);
139 		if (cnt <= 0) {
140 			/* lost carrier */
141 			if (cnt < 0 && errno == EIO) {
142 				sigemptyset(&mask);
143 				sigaddset(&mask, SIGTERM);
144 				sigprocmask(SIG_BLOCK, &mask, NULL);
145 				intTERM(0);
146 				/*NOTREACHED*/
147 			}
148 			continue;
149 		}
150 		sigemptyset(&mask);
151 		sigaddset(&mask, SIGEMT);
152 		sigaddset(&mask, SIGTERM);
153 		sigaddset(&mask, SIGIOT);
154 		sigaddset(&mask, SIGSYS);
155 		sigprocmask(SIG_BLOCK, &mask, NULL);
156 		for (cp = buf; cp < buf + cnt; cp++)
157 			*cp &= STRIP_PAR;
158 		write(STDOUT_FILENO, buf, cnt);
159 		if (boolean(value(SCRIPT)) && fscript != NULL) {
160 			if (!boolean(value(BEAUTIFY))) {
161 				fwrite(buf, 1, cnt, fscript);
162 				continue;
163 			}
164 			for (cp = buf; cp < buf + cnt; cp++)
165 				if ((*cp >= ' ' && *cp <= '~') ||
166 				    any(*cp, value(EXCEPTIONS)))
167 					putc(*cp, fscript);
168 		}
169 	}
170 }
171