Debian Codegraph @

telnetd/telnetd.h

raw
1 /*
2 Copyright (C) 1996-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 #include <sys/types.h>
20 #include <sys/time.h>
21 #include <sys/stat.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <netdb.h>
28 #include <unistd.h>
29 #include <signal.h>
30 #include <errno.h>
31 #include <syslog.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <strings.h>
35 #ifdef HAVE_SYS_FILIO_H
36 # include <sys/filio.h>
37 #endif
38 #ifdef HAVE_SYS_TTY_H
39 # include <sys/tty.h>
40 #endif
41 #ifdef HAVE_SYS_PTYVAR_H
42 # include <sys/ptyvar.h>
43 #endif
44 #if defined HAVE_STROPTS_H && !defined HAVE_IOCTL
45 # include <stropts.h>
46 #endif
47 #include <sys/ioctl.h>
48
49 #include <arpa/telnet.h>
50
51 #include <libtelnet/auth.h>
52 #include <libtelnet/encrypt.h>
53
54 #include <termios.h>
55
56 #define obstack_chunk_alloc malloc
57 #define obstack_chunk_free free
58 #include <obstack.h>
59
60 #include "gl_hash_set.h"
61 #include "gl_xset.h"
62 #include "hashcode-string1.h"
63 #include "xalloc.h"
64
65 #ifndef HAVE_CC_T
66 typedef unsigned char cc_t;
67 #endif
68
69 struct telnetd_clocks
70 {
71 int system; /* what the current time is */
72 int echotoggle; /* last time user entered echo character */
73 int modenegotiated; /* last time operating mode negotiated */
74 int didnetreceive; /* last time we read data from network */
75 int ttypesubopt; /* ttype subopt is received */
76 int tspeedsubopt; /* tspeed subopt is received */
77 int environsubopt; /* environ subopt is received */
78 int oenvironsubopt; /* old environ subopt is received */
79 int xdisplocsubopt; /* xdisploc subopt is received */
80 int baseline; /* time started to do timed action */
81 int gotDM; /* when did we last see a data mark */
82 };
83 #define settimer(x) (clocks.x = ++clocks.system)
84 #define sequenceIs(x,y) (clocks.x < clocks.y)
85
86 /*
87 * Structures of information for each special character function.
88 */
89 typedef struct
90 {
91 unsigned char flag; /* the flags for this function */
92 cc_t val; /* the value of the special character */
93 } slcent, *Slcent;
94
95 typedef struct
96 {
97 slcent defset; /* the default settings */
98 slcent current; /* the current settings */
99 cc_t *sptr; /* a pointer to the char in */
100 /* system data structures */
101 } slcfun, *Slcfun;
102
103 #ifdef HAVE_UNAME
104 /* Prefix and suffix if the IM string can be generated from uname. */
105 # define UNAME_IM_PREFIX "\r\n"
106 # define UNAME_IM_SUFFIX " (%l) (%t)\r\n\r\n"
107 #else /* ! HAVE_UNAME */
108 # define UNAME_IM_PREFIX "\r\n"
109 # define UNAME_IM_SUFFIX "\r\n"
110 #endif
111
112 /* ********************* */
113 /* State machine */
114 /*
115 * We keep track of each side of the option negotiation.
116 */
117
118 #define MY_STATE_WILL 0x01
119 #define MY_WANT_STATE_WILL 0x02
120 #define MY_STATE_DO 0x04
121 #define MY_WANT_STATE_DO 0x08
122
123 /*
124 * Macros to check the current state of things
125 */
126
127 #define my_state_is_do(opt) (options[opt]&MY_STATE_DO)
128 #define my_state_is_will(opt) (options[opt]&MY_STATE_WILL)
129 #define my_want_state_is_do(opt) (options[opt]&MY_WANT_STATE_DO)
130 #define my_want_state_is_will(opt) (options[opt]&MY_WANT_STATE_WILL)
131
132 #define my_state_is_dont(opt) (!my_state_is_do(opt))
133 #define my_state_is_wont(opt) (!my_state_is_will(opt))
134 #define my_want_state_is_dont(opt) (!my_want_state_is_do(opt))
135 #define my_want_state_is_wont(opt) (!my_want_state_is_will(opt))
136
137 #define set_my_state_do(opt) (options[opt] |= MY_STATE_DO)
138 #define set_my_state_will(opt) (options[opt] |= MY_STATE_WILL)
139 #define set_my_want_state_do(opt) (options[opt] |= MY_WANT_STATE_DO)
140 #define set_my_want_state_will(opt) (options[opt] |= MY_WANT_STATE_WILL)
141
142 #define set_my_state_dont(opt) (options[opt] &= ~MY_STATE_DO)
143 #define set_my_state_wont(opt) (options[opt] &= ~MY_STATE_WILL)
144 #define set_my_want_state_dont(opt) (options[opt] &= ~MY_WANT_STATE_DO)
145 #define set_my_want_state_wont(opt) (options[opt] &= ~MY_WANT_STATE_WILL)
146
147 /*
148 * Tricky code here. What we want to know is if the MY_STATE_WILL
149 * and MY_WANT_STATE_WILL bits have the same value. Since the two
150 * bits are adjacent, a little arithmetic will show that by adding
151 * in the lower bit, the upper bit will be set if the two bits were
152 * different, and clear if they were the same.
153 */
154 #define my_will_wont_is_changing(opt) \
155 ((options[opt]+MY_STATE_WILL) & MY_WANT_STATE_WILL)
156
157 #define my_do_dont_is_changing(opt) \
158 ((options[opt]+MY_STATE_DO) & MY_WANT_STATE_DO)
159
160 /*
161 * Make everything symmetrical
162 */
163
164 #define HIS_STATE_WILL MY_STATE_DO
165 #define HIS_WANT_STATE_WILL MY_WANT_STATE_DO
166 #define HIS_STATE_DO MY_STATE_WILL
167 #define HIS_WANT_STATE_DO MY_WANT_STATE_WILL
168
169 #define his_state_is_do my_state_is_will
170 #define his_state_is_will my_state_is_do
171 #define his_want_state_is_do my_want_state_is_will
172 #define his_want_state_is_will my_want_state_is_do
173
174 #define his_state_is_dont my_state_is_wont
175 #define his_state_is_wont my_state_is_dont
176 #define his_want_state_is_dont my_want_state_is_wont
177 #define his_want_state_is_wont my_want_state_is_dont
178
179 #define set_his_state_do set_my_state_will
180 #define set_his_state_will set_my_state_do
181 #define set_his_want_state_do set_my_want_state_will
182 #define set_his_want_state_will set_my_want_state_do
183
184 #define set_his_state_dont set_my_state_wont
185 #define set_his_state_wont set_my_state_dont
186 #define set_his_want_state_dont set_my_want_state_wont
187 #define set_his_want_state_wont set_my_want_state_dont
188
189 #define his_will_wont_is_changing my_do_dont_is_changing
190 #define his_do_dont_is_changing my_will_wont_is_changing
191
192 /* ******* */
193
194 void fatal (int f, char *msg);
195
196 /*
197 * Linemode support states, in decreasing order of importance
198 */
199 #define REAL_LINEMODE 0x04
200 #define KLUDGE_OK 0x03
201 #define NO_AUTOKLUDGE 0x02
202 #define KLUDGE_LINEMODE 0x01
203 #define NO_LINEMODE 0x00
204
205 #include <arpa/telnet.h>
206
207 #define NETSLOP 64
208
209 #define ttloop(c) while (c) io_drain ()
210
211 /* External variables */
212 extern char options[256];
213 extern char do_dont_resp[256];
214 extern char will_wont_resp[256];
215 extern int linemode; /* linemode on/off */
216 extern int uselinemode; /* what linemode to use (on/off) */
217 extern int editmode; /* edit modes in use */
218 extern int useeditmode; /* edit modes to use */
219 extern int alwayslinemode; /* command line option */
220 extern int lmodetype; /* Client support for linemode */
221 extern int flowmode; /* current flow control state */
222 extern int restartany; /* restart output on any character state */
223 extern int diagnostic; /* telnet diagnostic capabilities */
224 #if defined AUTHENTICATION
225 extern int auth_level;
226 extern int autologin;
227 #endif
228
229 extern slcfun slctab[NSLC + 1]; /* slc mapping table */
230
231 extern char *terminaltype;
232 extern char *remote_hostname;
233 extern char *local_hostname;
234 extern char *login_invocation;
235 extern char *user_name;
236
237 extern int pty, net;
238 extern int SYNCHing; /* we are in TELNET SYNCH mode */
239 extern struct telnetd_clocks clocks;
240 extern gl_set_t accept_env_set;
241 extern char line[];
242
243 extern char *xstrdup (const char *);
244 extern int argcv_get (const char *command, const char *delim,
245 int *argc, char ***argv);
246
247 void io_setup (void);
248 int net_has_data (void);
249 int net_get_char (int peek);
250 void set_neturg (void);
251 int net_output_data (const char *format, ...);
252 int net_output_datalen (const void *buf, size_t l);
253 int net_buffer_level (void);
254 void io_drain (void);
255
256 int stilloob (int s);
257 void ptyflush (void);
258 char *nextitem (char *current, const char *endp);
259 void netclear (void);
260 void netflush (void);
261
262 int pty_buffer_is_full (void);
263 void pty_output_byte (int c);
264 void pty_output_datalen (const void *data, size_t len);
265 int pty_buffer_level (void);
266
267 /* TTY functions */
268 extern void init_termbuf (void);
269 extern void set_termbuf (void);
270 extern int spcset (int func, cc_t * valp, cc_t ** valpp);
271 extern void term_send_eof (void);
272 extern int term_change_eof (void);
273
274 extern void tty_binaryin (int);
275 extern void tty_binaryout (int);
276 extern int tty_flowmode (void);
277 extern int tty_restartany (void);
278 extern int tty_isbinaryin (void);
279 extern int tty_isbinaryout (void);
280 extern int tty_iscrnl (void);
281 extern int tty_isecho (void);
282 extern int tty_isediting (void);
283 extern int tty_islitecho (void);
284 extern int tty_isnewmap (void);
285 extern int tty_israw (void);
286 extern int tty_issofttab (void);
287 extern int tty_istrapsig (void);
288 extern int tty_linemode (void);
289 extern void tty_rspeed (int);
290 extern void tty_setecho (int);
291 extern void tty_setedit (int);
292 extern void tty_setlinemode (int);
293 extern void tty_setlitecho (int);
294 extern void tty_setsig (int);
295 extern void tty_setsofttab (int);
296 extern void tty_tspeed (int);
297
298 extern char *expand_line (const char *fmt);
299
300 /* FIXME */
301 extern void _termstat (void);
302 extern void add_slc (char func, char flag, cc_t val);
303 extern void check_slc (void);
304 extern void change_slc (char func, char flag, cc_t val);
305
306 extern void cleanup (int);
307 extern void clientstat (int, int, int);
308 extern void copy_termbuf (void);
309 extern void deferslc (void);
310 extern void defer_terminit (void);
311 extern void do_opt_slc (unsigned char *, int);
312 extern void dooption (int);
313 extern void dontoption (int);
314 extern void edithost (char *, char *);
315 extern void fatal (int, char *);
316 extern void fatalperror (int, char *);
317 extern void get_slc_defaults (void);
318 extern void localstat (void);
319 extern void flowstat (void);
320 extern void netclear (void);
321
322 extern void send_do (int, int);
323 extern void send_dont (int, int);
324 extern void send_slc (void);
325 extern void send_status (void);
326 extern void send_will (int, int);
327 extern void send_wont (int, int);
328 extern void set_termbuf (void);
329 extern void start_login (char *, int, char *);
330 extern void start_slc (int);
331 extern void start_slave (char *, int, char *);
332
333 extern void suboption (void);
334 extern void telrcv (void);
335
336 extern int end_slc (unsigned char **);
337 extern int spcset (int, cc_t *, cc_t **);
338 extern int stilloob (int);
339 extern int terminit (void);
340 extern int termstat (void);
341 extern void willoption (int);
342 extern void wontoption (int);
343
344 #ifdef ENCRYPTION
345 extern void (*encrypt_output) (unsigned char *, int);
346 extern int (*decrypt_input) (int);
347 #endif /* ENCRYPTION */
348
349 extern int startslave (char *host, int autologin, char *autoname);
350 extern int getterminaltype (char *user_name, size_t len);
351 extern int net_input_level (void);
352 extern int net_output_level (void);
353 extern int net_read (void);
354 extern int net_buffer_is_full (void);
355 extern void net_output_byte (int c);
356 extern int pty_input_level (void);
357 extern int pty_read (void);
358 extern int pty_output_level (void);
359 extern int pty_get_char (int peek);
360 extern int pty_input_putback (const char *str, size_t len);
361
362 extern int terminaltypeok (char *s);