1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)defs.h	8.1 (Berkeley) 6/4/93
34  */
35 
36 /*
37  * Telnet server defines
38  */
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/ioctl.h>
42 
43 #ifndef	BSD
44 # define	BSD 43
45 #endif
46 
47 #if	defined(CRAY) && !defined(LINEMODE)
48 # define SYSV_TERMIO
49 # define LINEMODE
50 # define KLUDGELINEMODE
51 # define DIAGNOSTICS
52 # if defined(UNICOS50) && !defined(UNICOS5)
53 #  define UNICOS5
54 # endif
55 # if !defined(UNICOS5)
56 #  define BFTPDAEMON
57 #  define HAS_IP_TOS
58 # endif
59 #endif /* CRAY */
60 #if defined(UNICOS5) && !defined(NO_SETSID)
61 # define NO_SETSID
62 #endif
63 
64 #if defined(PRINTOPTIONS) && defined(DIAGNOSTICS)
65 #define TELOPTS
66 #define TELCMDS
67 #define	SLC_NAMES
68 #endif
69 
70 #if	defined(SYSV_TERMIO) && !defined(USE_TERMIO)
71 # define	USE_TERMIO
72 #endif
73 
74 #include <sys/socket.h>
75 #ifndef	CRAY
76 #include <sys/wait.h>
77 #endif	/* CRAY */
78 #include <fcntl.h>
79 #include <sys/file.h>
80 #include <sys/stat.h>
81 
82 #if TIME_WITH_SYS_TIME
83 # include <sys/time.h>
84 # include <time.h>
85 #else
86 # if HAVE_SYS_TIME_H
87 #  include <sys/time.h>
88 # else
89 #  include <time.h>
90 # endif
91 #endif
92 
93 #include <sys/ioctl.h>
94 #ifdef	HAVE_SYS_FILIO_H
95 #include <sys/filio.h>
96 #endif
97 
98 #include <netinet/in.h>
99 
100 #include <arpa/telnet.h>
101 
102 /* for socklen() */
103 #include "socket-utils.h"
104 
105 #include <stdio.h>
106 #ifdef	__STDC__
107 #include <stdlib.h>
108 #endif
109 #include <signal.h>
110 #include <errno.h>
111 #include <netdb.h>
112 #include <syslog.h>
113 #ifndef	LOG_DAEMON
114 #define	LOG_DAEMON	0
115 #endif
116 #ifndef	LOG_ODELAY
117 #define	LOG_ODELAY	0
118 #endif
119 #include <ctype.h>
120 #ifndef HAVE_STRING_H
121 #include <strings.h>
122 #else
123 #include <string.h>
124 #endif
125 
126 #ifndef	USE_TERMIO
127 #include <sgtty.h>
128 #else
129 # ifdef	SYSV_TERMIO
130 # include <termio.h>
131 # else
132 # include <termios.h>
133 # endif
134 #endif
135 #if !defined(USE_TERMIO) || defined(NO_CC_T)
136 typedef unsigned char cc_t;
137 #endif
138 
139 #ifdef	__STDC__
140 #include <unistd.h>
141 #endif
142 
143 #ifndef _POSIX_VDISABLE
144 # ifdef VDISABLE
145 #  define _POSIX_VDISABLE VDISABLE
146 # else
147 #  define _POSIX_VDISABLE ((unsigned char)'\377')
148 # endif
149 #endif
150 
151 
152 #ifdef	CRAY
153 # ifdef	CRAY1
154 # include <sys/pty.h>
155 #  ifndef FD_ZERO
156 # include <sys/select.h>
157 #  endif /* FD_ZERO */
158 # endif	/* CRAY1 */
159 
160 #ifdef HAVE_MEMORY_H
161 #include <memory.h>
162 #endif
163 #endif	/* CRAY */
164 
165 #ifdef __hpux
166 #include <sys/ptyio.h>
167 #endif
168 
169 #if defined(__alpha) && defined(STREAMSPTY)
170 #undef STREAMSPTY
171 #endif
172 
173 #if	!defined(TIOCSCTTY) && defined(TCSETCTTY)
174 # define	TIOCSCTTY TCSETCTTY
175 #endif
176 
177 #ifndef	FD_SET
178 #ifndef	HAVE_fd_set
179 typedef struct fd_set { int fds_bits[1]; } fd_set;
180 #endif
181 
182 #define	FD_SET(n, p)	((p)->fds_bits[0] |= (1<<(n)))
183 #define	FD_CLR(n, p)	((p)->fds_bits[0] &= ~(1<<(n)))
184 #define	FD_ISSET(n, p)	((p)->fds_bits[0] & (1<<(n)))
185 #define FD_ZERO(p)	((p)->fds_bits[0] = 0)
186 #endif	/* FD_SET */
187 
188 /*
189  * I/O data buffers defines
190  */
191 #define	NETSLOP	64
192 #ifdef CRAY
193 #undef BUFSIZ
194 #define BUFSIZ  2048
195 #endif
196 
197 #define	NIACCUM(c)	{   *netip++ = c; \
198 			    ncc++; \
199 			}
200 
201 /* clock manipulations */
202 #define	settimer(x)	(clocks.x = ++clocks.system)
203 #define	sequenceIs(x,y)	(clocks.x < clocks.y)
204 
205 /*
206  * Linemode support states, in decreasing order of importance
207  */
208 #define REAL_LINEMODE	0x04
209 #define KLUDGE_OK	0x03
210 #define	NO_AUTOKLUDGE	0x02
211 #define KLUDGE_LINEMODE	0x01
212 #define NO_LINEMODE	0x00
213 
214 /*
215  * Structures of information for each special character function.
216  */
217 typedef struct {
218 	unsigned char	flag;		/* the flags for this function */
219 	cc_t		val;		/* the value of the special character */
220 } slcent, *Slcent;
221 
222 typedef struct {
223 	slcent		defset;		/* the default settings */
224 	slcent		current;	/* the current settings */
225 	cc_t		*sptr;		/* a pointer to the char in */
226 					/* system data structures */
227 } slcfun, *Slcfun;
228 
229 #ifdef DIAGNOSTICS
230 /*
231  * Diagnostics capabilities
232  */
233 #define	TD_REPORT	0x01	/* Report operations to client */
234 #define TD_EXERCISE	0x02	/* Exercise client's implementation */
235 #define TD_NETDATA	0x04	/* Display received data stream */
236 #define TD_PTYDATA	0x08	/* Display data passed to pty */
237 #define	TD_OPTIONS	0x10	/* Report just telnet options */
238 #endif /* DIAGNOSTICS */
239 
240 /*
241  * We keep track of each side of the option negotiation.
242  */
243 
244 #define	MY_STATE_WILL		0x01
245 #define	MY_WANT_STATE_WILL	0x02
246 #define	MY_STATE_DO		0x04
247 #define	MY_WANT_STATE_DO	0x08
248 
249 /*
250  * Macros to check the current state of things
251  */
252 
253 #define	my_state_is_do(opt)		(options[opt]&MY_STATE_DO)
254 #define	my_state_is_will(opt)		(options[opt]&MY_STATE_WILL)
255 #define my_want_state_is_do(opt)	(options[opt]&MY_WANT_STATE_DO)
256 #define my_want_state_is_will(opt)	(options[opt]&MY_WANT_STATE_WILL)
257 
258 #define	my_state_is_dont(opt)		(!my_state_is_do(opt))
259 #define	my_state_is_wont(opt)		(!my_state_is_will(opt))
260 #define my_want_state_is_dont(opt)	(!my_want_state_is_do(opt))
261 #define my_want_state_is_wont(opt)	(!my_want_state_is_will(opt))
262 
263 #define	set_my_state_do(opt)		(options[opt] |= MY_STATE_DO)
264 #define	set_my_state_will(opt)		(options[opt] |= MY_STATE_WILL)
265 #define	set_my_want_state_do(opt)	(options[opt] |= MY_WANT_STATE_DO)
266 #define	set_my_want_state_will(opt)	(options[opt] |= MY_WANT_STATE_WILL)
267 
268 #define	set_my_state_dont(opt)		(options[opt] &= ~MY_STATE_DO)
269 #define	set_my_state_wont(opt)		(options[opt] &= ~MY_STATE_WILL)
270 #define	set_my_want_state_dont(opt)	(options[opt] &= ~MY_WANT_STATE_DO)
271 #define	set_my_want_state_wont(opt)	(options[opt] &= ~MY_WANT_STATE_WILL)
272 
273 /*
274  * Tricky code here.  What we want to know is if the MY_STATE_WILL
275  * and MY_WANT_STATE_WILL bits have the same value.  Since the two
276  * bits are adjacent, a little arithmatic will show that by adding
277  * in the lower bit, the upper bit will be set if the two bits were
278  * different, and clear if they were the same.
279  */
280 #define my_will_wont_is_changing(opt) \
281 			((options[opt]+MY_STATE_WILL) & MY_WANT_STATE_WILL)
282 
283 #define my_do_dont_is_changing(opt) \
284 			((options[opt]+MY_STATE_DO) & MY_WANT_STATE_DO)
285 
286 /*
287  * Make everything symetrical
288  */
289 
290 #define	HIS_STATE_WILL			MY_STATE_DO
291 #define	HIS_WANT_STATE_WILL		MY_WANT_STATE_DO
292 #define HIS_STATE_DO			MY_STATE_WILL
293 #define HIS_WANT_STATE_DO		MY_WANT_STATE_WILL
294 
295 #define	his_state_is_do			my_state_is_will
296 #define	his_state_is_will		my_state_is_do
297 #define his_want_state_is_do		my_want_state_is_will
298 #define his_want_state_is_will		my_want_state_is_do
299 
300 #define	his_state_is_dont		my_state_is_wont
301 #define	his_state_is_wont		my_state_is_dont
302 #define his_want_state_is_dont		my_want_state_is_wont
303 #define his_want_state_is_wont		my_want_state_is_dont
304 
305 #define	set_his_state_do		set_my_state_will
306 #define	set_his_state_will		set_my_state_do
307 #define	set_his_want_state_do		set_my_want_state_will
308 #define	set_his_want_state_will		set_my_want_state_do
309 
310 #define	set_his_state_dont		set_my_state_wont
311 #define	set_his_state_wont		set_my_state_dont
312 #define	set_his_want_state_dont		set_my_want_state_wont
313 #define	set_his_want_state_wont		set_my_want_state_dont
314 
315 #define his_will_wont_is_changing	my_do_dont_is_changing
316 #define his_do_dont_is_changing		my_will_wont_is_changing
317 
318 extern char	*line;
319 
320 #ifdef	ENCRYPTION
321 extern void	(*encrypt_output) (unsigned char *, int);
322 extern int	(*decrypt_input) (int);
323 #endif	/* ENCRYPTION */
324