Debian Codegraph @

telnetd/slc.c

raw
1 /*
2 Copyright (C) 1993-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 /*
20 * Copyright (c) 1989, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 #include <config.h>
49
50 #include "telnetd.h"
51
52 /*
53 * local variables
54 */
55 static unsigned char *def_slcbuf = (unsigned char *) 0;
56 static int def_slclen = 0;
57 static int slcchange; /* change to slc is requested */
58 static unsigned char *slcptr; /* pointer into slc buffer */
59 static unsigned char slcbuf[NSLC * 6]; /* buffer for slc negotiation */
60
61 /*
62 * send_slc
63 *
64 * Write out the current special characters to the client.
65 */
66 void
67 send_slc (void)
68 {
69 int i;
70
71 /*
72 * Send out list of triplets of special characters
73 * to client. We only send info on the characters
74 * that are currently supported, except that editing
75 * characters without current support are conveyed
76 * using SLC_DEFAULT, thus urging the client to make
77 * a choice of his own. This conforms to RFC 1184,
78 * section 2.4, page 6.
79 */
80 for (i = 1; i <= NSLC; i++)
81 {
82 if ((slctab[i].current.flag & SLC_LEVELBITS) != SLC_NOSUPPORT)
83 add_slc ((unsigned char) i, slctab[i].current.flag,
84 slctab[i].current.val);
85 else
86 {
87 /* Urge the client to choose sensible editing characters. */
88 switch (i)
89 {
90 case SLC_EC:
91 case SLC_EL:
92 case SLC_EW:
93 case SLC_RP:
94 case SLC_LNEXT:
95 add_slc ((unsigned char) i,
96 (slctab[i].current.
97 flag & ~SLC_LEVELBITS) | SLC_DEFAULT, 0);
98 break;
99
100 default:
101 /* Be silent on other, unsupported characters. */
102 break;
103 }
104 }
105 }
106
107 } /* end of send_slc */
108
109 /*
110 * default_slc
111 *
112 * Set pty special characters to all the defaults.
113 */
114 void
115 default_slc (void)
116 {
117 int i;
118
119 for (i = 1; i <= NSLC; i++)
120 {
121 slctab[i].current.val = slctab[i].defset.val;
122 if (slctab[i].current.val == (cc_t) (_POSIX_VDISABLE))
123 slctab[i].current.flag = SLC_NOSUPPORT;
124 else
125 slctab[i].current.flag = slctab[i].defset.flag;
126 if (slctab[i].sptr)
127 {
128 *(slctab[i].sptr) = slctab[i].defset.val;
129 }
130 }
131 slcchange = 1;
132
133 } /* end of default_slc */
134
135 /*
136 * get_slc_defaults
137 *
138 * Initialize the slc mapping table.
139 */
140 void
141 get_slc_defaults (void)
142 {
143 int i;
144
145 init_termbuf ();
146
147 for (i = 1; i <= NSLC; i++)
148 {
149 slctab[i].defset.flag =
150 spcset (i, &slctab[i].defset.val, &slctab[i].sptr);
151 slctab[i].current.flag = SLC_NOSUPPORT;
152 slctab[i].current.val = 0;
153 }
154
155 } /* end of get_slc_defaults */
156
157 /*
158 * add_slc
159 *
160 * Add an slc triplet to the slc buffer.
161 */
162 void
163 add_slc (char func, char flag, cc_t val)
164 {
165 /* Do nothing if the entire triplet cannot fit in the buffer. */
166 if (slcbuf + sizeof slcbuf - slcptr <= 6)
167 return;
168
169 if ((*slcptr++ = (unsigned char) func) == 0xff)
170 *slcptr++ = 0xff;
171
172 if ((*slcptr++ = (unsigned char) flag) == 0xff)
173 *slcptr++ = 0xff;
174
175 if ((*slcptr++ = (unsigned char) val) == 0xff)
176 *slcptr++ = 0xff;
177
178 } /* end of add_slc */
179
180 /*
181 * start_slc
182 *
183 * Get ready to process incoming slc's and respond to them.
184 *
185 * The parameter getit is non-zero if it is necessary to grab a copy
186 * of the terminal control structures.
187 */
188 void
189 start_slc (int getit)
190 {
191
192 slcchange = 0;
193 if (getit)
194 init_termbuf ();
195 sprintf ((char *) slcbuf, "%c%c%c%c", IAC, SB, TELOPT_LINEMODE, LM_SLC);
196 slcptr = slcbuf + 4;
197
198 } /* end of start_slc */
199
200 /*
201 * end_slc
202 *
203 * Finish up the slc negotiation. If something to send, then send it.
204 */
205 int
206 end_slc (unsigned char **bufp)
207 {
208 int len;
209 void netflush (void);
210
211 /*
212 * If a change has occurred, store the new terminal control
213 * structures back to the terminal driver.
214 */
215 if (slcchange)
216 {
217 set_termbuf ();
218 }
219
220 /*
221 * If the pty state has not yet been fully processed and there is a
222 * deferred slc request from the client, then do not send any
223 * sort of slc negotiation now. We will respond to the client's
224 * request very soon.
225 */
226 if (def_slcbuf && (terminit () == 0))
227 {
228 return (0);
229 }
230
231 if (slcptr > (slcbuf + 4))
232 {
233 if (bufp)
234 {
235 *bufp = &slcbuf[4];
236 return (slcptr - slcbuf - 4);
237 }
238 else
239 {
240 sprintf ((char *) slcptr, "%c%c", IAC, SE);
241 slcptr += 2;
242 len = slcptr - slcbuf;
243 net_output_datalen (slcbuf, len);
244 netflush (); /* force it out immediately */
245 }
246 }
247 return (0);
248
249 } /* end of end_slc */
250
251 /*
252 * process_slc
253 *
254 * Figure out what to do about the client's slc
255 */
256 void
257 process_slc (unsigned char func, unsigned char flag, cc_t val)
258 {
259 int hislevel, mylevel, ack;
260
261 /*
262 * Ensure that we know something about this function
263 */
264 if (func > NSLC)
265 {
266 add_slc (func, SLC_NOSUPPORT, 0);
267 return;
268 }
269
270 /*
271 * Process the special case requests of 0 SLC_DEFAULT 0
272 * and 0 SLC_VARIABLE 0. Be a little forgiving here, don't
273 * worry about whether the value is actually 0 or not.
274 */
275 if (func == 0)
276 {
277 if ((flag = flag & SLC_LEVELBITS) == SLC_DEFAULT)
278 {
279 default_slc ();
280 send_slc ();
281 }
282 else if (flag == SLC_VARIABLE)
283 {
284 send_slc ();
285 }
286 return;
287 }
288
289 /*
290 * Appears to be a function that we know something about. So
291 * get on with it and see what we know.
292 */
293
294 hislevel = flag & SLC_LEVELBITS;
295 mylevel = slctab[func].current.flag & SLC_LEVELBITS;
296 ack = flag & SLC_ACK;
297 /*
298 * ignore the command if:
299 * the function value and level are the same as what we already have;
300 * or the level is the same and the ack bit is set
301 */
302 if (hislevel == mylevel && (val == slctab[func].current.val || ack))
303 {
304 return;
305 }
306 else if (ack)
307 {
308 /*
309 * If we get here, we got an ack, but the levels don't match.
310 * This shouldn't happen. If it does, it is probably because
311 * we have sent two requests to set a variable without getting
312 * a response between them, and this is the first response.
313 * So, ignore it, and wait for the next response.
314 */
315 return;
316 }
317 else
318 {
319 change_slc (func, flag, val);
320 }
321
322 } /* end of process_slc */
323
324 /*
325 * change_slc
326 *
327 * Process a request to change one of our special characters.
328 * Compare client's request with what we are capable of supporting.
329 */
330 void
331 change_slc (char func_c, char flag, cc_t val)
332 {
333 int func = func_c;
334 int hislevel, mylevel;
335
336 hislevel = flag & SLC_LEVELBITS;
337 mylevel = slctab[func].defset.flag & SLC_LEVELBITS;
338 /*
339 * If client is setting a function to NOSUPPORT
340 * or DEFAULT, then we can easily and directly
341 * accommodate the request.
342 */
343 if (hislevel == SLC_NOSUPPORT)
344 {
345 slctab[func].current.flag = flag;
346 slctab[func].current.val = (cc_t) _POSIX_VDISABLE;
347 flag |= SLC_ACK;
348 add_slc (func, flag, val);
349 return;
350 }
351 if (hislevel == SLC_DEFAULT)
352 {
353 /*
354 * Special case here. If client tells us to use
355 * the default on a function we don't support, then
356 * return NOSUPPORT instead of what we may have as a
357 * default level of DEFAULT.
358 */
359 if (mylevel == SLC_DEFAULT)
360 {
361 slctab[func].current.flag = SLC_NOSUPPORT;
362 }
363 else
364 {
365 slctab[func].current.flag = slctab[func].defset.flag;
366 }
367 slctab[func].current.val = slctab[func].defset.val;
368 add_slc (func, slctab[func].current.flag, slctab[func].current.val);
369 return;
370 }
371
372 /*
373 * Client wants us to change to a new value or he
374 * is telling us that he can't change to our value.
375 * Some of the slc's we support and can change,
376 * some we do support but can't change,
377 * and others we don't support at all.
378 * If we can change it then we have a pointer to
379 * the place to put the new value, so change it,
380 * otherwise, continue the negotiation.
381 */
382 if (slctab[func].sptr)
383 {
384 /*
385 * We can change this one.
386 */
387 slctab[func].current.val = val;
388 *(slctab[func].sptr) = val;
389 slctab[func].current.flag = flag;
390 flag |= SLC_ACK;
391 slcchange = 1;
392 add_slc (func, flag, val);
393 }
394 else
395 {
396 /*
397 * It is not possible for us to support this
398 * request as he asks.
399 *
400 * If our level is DEFAULT, then just ack whatever was
401 * sent.
402 *
403 * If he can't change and we can't change,
404 * then degenerate to NOSUPPORT.
405 *
406 * Otherwise we send our level back to him, (CANTCHANGE
407 * or NOSUPPORT) and if CANTCHANGE, send
408 * our value as well.
409 */
410 if (mylevel == SLC_DEFAULT)
411 {
412 slctab[func].current.flag = flag;
413 slctab[func].current.val = val;
414 flag |= SLC_ACK;
415 }
416 else if (hislevel == SLC_CANTCHANGE && mylevel == SLC_CANTCHANGE)
417 {
418 flag &= ~SLC_LEVELBITS;
419 flag |= SLC_NOSUPPORT;
420 slctab[func].current.flag = flag;
421 }
422 else
423 {
424 flag &= ~SLC_LEVELBITS;
425 flag |= mylevel;
426 slctab[func].current.flag = flag;
427 if (mylevel == SLC_CANTCHANGE)
428 {
429 slctab[func].current.val = slctab[func].defset.val;
430 val = slctab[func].current.val;
431 }
432 }
433 add_slc (func, flag, val);
434 }
435
436 } /* end of change_slc */
437
438 /*
439 * check_slc
440 *
441 * Check the special characters in use and notify the client if any have
442 * changed. Only those characters that are capable of being changed are
443 * likely to have changed. If a local change occurs, kick the support level
444 * and flags up to the defaults.
445 */
446 void
447 check_slc (void)
448 {
449 int i;
450
451 for (i = 1; i <= NSLC; i++)
452 {
453 if (i == SLC_EOF && term_change_eof ())
454 continue;
455 if (slctab[i].sptr && (*(slctab[i].sptr) != slctab[i].current.val))
456 {
457 slctab[i].current.val = *(slctab[i].sptr);
458 if (*(slctab[i].sptr) == (cc_t) _POSIX_VDISABLE)
459 slctab[i].current.flag = SLC_NOSUPPORT;
460 else
461 slctab[i].current.flag = slctab[i].defset.flag;
462 add_slc ((unsigned char) i, slctab[i].current.flag,
463 slctab[i].current.val);
464 }
465 }
466 } /* check_slc */
467
468 /*
469 * do_opt_slc
470 *
471 * Process an slc option buffer. Defer processing of incoming slc's
472 * until after the terminal state has been processed. Save the first slc
473 * request that comes along, but discard all others.
474 *
475 * ptr points to the beginning of the buffer, len is the length.
476 */
477 void
478 do_opt_slc (unsigned char *ptr, int len)
479 {
480 unsigned char func, flag;
481 cc_t val;
482 unsigned char *end = ptr + len;
483
484 if (terminit ())
485 { /* go ahead */
486 while (ptr < end)
487 {
488 func = *ptr++;
489 if (ptr >= end)
490 break;
491 flag = *ptr++;
492 if (ptr >= end)
493 break;
494 val = (cc_t) * ptr++;
495
496 process_slc (func, flag, val);
497
498 }
499 }
500 else
501 {
502 /*
503 * save this slc buffer if it is the first, otherwise dump
504 * it.
505 */
506 if (def_slcbuf == (unsigned char *) 0)
507 {
508 def_slclen = len;
509 def_slcbuf = (unsigned char *) malloc ((unsigned) len);
510 if (def_slcbuf == (unsigned char *) 0)
511 return; /* too bad */
512 memmove (def_slcbuf, ptr, len);
513 }
514 }
515
516 } /* end of do_opt_slc */
517
518 /*
519 * deferslc
520 *
521 * Do slc stuff that was deferred.
522 */
523 void
524 deferslc (void)
525 {
526 if (def_slcbuf)
527 {
528 start_slc (1);
529 do_opt_slc (def_slcbuf, def_slclen);
530 end_slc (0);
531 free (def_slcbuf);
532 def_slcbuf = (unsigned char *) 0;
533 def_slclen = 0;
534 }
535
536 } /* end of deferslc */