xref: /haiku/src/bin/network/telnetd/slc.c (revision b55a57da7173b9af0432bd3e148d03f06161d036)
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 
34 #if 0
35 #ifndef lint
36 static const char sccsid[] = "@(#)slc.c	8.2 (Berkeley) 5/30/95";
37 #endif
38 #endif
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/contrib/telnet/telnetd/slc.c,v 1.9 2003/05/04 02:54:49 obrien Exp $");
41 
42 #include "telnetd.h"
43 
44 #ifdef	LINEMODE
45 /*
46  * local variables
47  */
48 static unsigned char	*def_slcbuf = (unsigned char *)0;
49 static int		def_slclen = 0;
50 static int		slcchange;	/* change to slc is requested */
51 static unsigned char	*slcptr;	/* pointer into slc buffer */
52 static unsigned char	slcbuf[NSLC*6];	/* buffer for slc negotiation */
53 
54 /*
55  * send_slc
56  *
57  * Write out the current special characters to the client.
58  */
59 void
60 send_slc(void)
61 {
62 	int i;
63 
64 	/*
65 	 * Send out list of triplets of special characters
66 	 * to client.  We only send info on the characters
67 	 * that are currently supported.
68 	 */
69 	for (i = 1; i <= NSLC; i++) {
70 		if ((slctab[i].defset.flag & SLC_LEVELBITS) == SLC_NOSUPPORT)
71 			continue;
72 		add_slc((unsigned char)i, slctab[i].current.flag,
73 							slctab[i].current.val);
74 	}
75 
76 }  /* end of send_slc */
77 
78 /*
79  * default_slc
80  *
81  * Set pty special characters to all the defaults.
82  */
83 static void
84 default_slc(void)
85 {
86 	int i;
87 
88 	for (i = 1; i <= NSLC; i++) {
89 		slctab[i].current.val = slctab[i].defset.val;
90 		if (slctab[i].current.val == (cc_t)(_POSIX_VDISABLE))
91 			slctab[i].current.flag = SLC_NOSUPPORT;
92 		else
93 			slctab[i].current.flag = slctab[i].defset.flag;
94 		if (slctab[i].sptr) {
95 			*(slctab[i].sptr) = slctab[i].defset.val;
96 		}
97 	}
98 	slcchange = 1;
99 
100 }  /* end of default_slc */
101 #endif	/* LINEMODE */
102 
103 /*
104  * get_slc_defaults
105  *
106  * Initialize the slc mapping table.
107  */
108 void
109 get_slc_defaults(void)
110 {
111 	int i;
112 
113 	init_termbuf();
114 
115 	for (i = 1; i <= NSLC; i++) {
116 		slctab[i].defset.flag =
117 			spcset(i, &slctab[i].defset.val, &slctab[i].sptr);
118 		slctab[i].current.flag = SLC_NOSUPPORT;
119 		slctab[i].current.val = 0;
120 	}
121 
122 }  /* end of get_slc_defaults */
123 
124 #ifdef	LINEMODE
125 /*
126  * add_slc
127  *
128  * Add an slc triplet to the slc buffer.
129  */
130 void
131 add_slc(char func, char flag, cc_t val)
132 {
133 
134 	if ((*slcptr++ = (unsigned char)func) == 0xff)
135 		*slcptr++ = 0xff;
136 
137 	if ((*slcptr++ = (unsigned char)flag) == 0xff)
138 		*slcptr++ = 0xff;
139 
140 	if ((*slcptr++ = (unsigned char)val) == 0xff)
141 		*slcptr++ = 0xff;
142 
143 }  /* end of add_slc */
144 
145 /*
146  * start_slc
147  *
148  * Get ready to process incoming slc's and respond to them.
149  *
150  * The parameter getit is non-zero if it is necessary to grab a copy
151  * of the terminal control structures.
152  */
153 void
154 start_slc(int getit)
155 {
156 
157 	slcchange = 0;
158 	if (getit)
159 		init_termbuf();
160 	(void) sprintf((char *)slcbuf, "%c%c%c%c",
161 					IAC, SB, TELOPT_LINEMODE, LM_SLC);
162 	slcptr = slcbuf + 4;
163 
164 }  /* end of start_slc */
165 
166 /*
167  * end_slc
168  *
169  * Finish up the slc negotiation.  If something to send, then send it.
170  */
171 int
172 end_slc(unsigned char **bufp)
173 {
174 	int len;
175 
176 	/*
177 	 * If a change has occured, store the new terminal control
178 	 * structures back to the terminal driver.
179 	 */
180 	if (slcchange) {
181 		set_termbuf();
182 	}
183 
184 	/*
185 	 * If the pty state has not yet been fully processed and there is a
186 	 * deferred slc request from the client, then do not send any
187 	 * sort of slc negotiation now.  We will respond to the client's
188 	 * request very soon.
189 	 */
190 	if (def_slcbuf && (terminit() == 0)) {
191 		return(0);
192 	}
193 
194 	if (slcptr > (slcbuf + 4)) {
195 		if (bufp) {
196 			*bufp = &slcbuf[4];
197 			return(slcptr - slcbuf - 4);
198 		} else {
199 			(void) sprintf((char *)slcptr, "%c%c", IAC, SE);
200 			slcptr += 2;
201 			len = slcptr - slcbuf;
202 			output_datalen(slcbuf, len);
203 			netflush();  /* force it out immediately */
204 			DIAG(TD_OPTIONS, printsub('>', slcbuf+2, len-2););
205 		}
206 	}
207 	return (0);
208 
209 }  /* end of end_slc */
210 
211 /*
212  * process_slc
213  *
214  * Figure out what to do about the client's slc
215  */
216 void
217 process_slc(unsigned char func, unsigned char flag, cc_t val)
218 {
219 	int hislevel, mylevel, ack;
220 
221 	/*
222 	 * Ensure that we know something about this function
223 	 */
224 	if (func > NSLC) {
225 		add_slc(func, SLC_NOSUPPORT, 0);
226 		return;
227 	}
228 
229 	/*
230 	 * Process the special case requests of 0 SLC_DEFAULT 0
231 	 * and 0 SLC_VARIABLE 0.  Be a little forgiving here, don't
232 	 * worry about whether the value is actually 0 or not.
233 	 */
234 	if (func == 0) {
235 		if ((flag = flag & SLC_LEVELBITS) == SLC_DEFAULT) {
236 			default_slc();
237 			send_slc();
238 		} else if (flag == SLC_VARIABLE) {
239 			send_slc();
240 		}
241 		return;
242 	}
243 
244 	/*
245 	 * Appears to be a function that we know something about.  So
246 	 * get on with it and see what we know.
247 	 */
248 
249 	hislevel = flag & SLC_LEVELBITS;
250 	mylevel = slctab[func].current.flag & SLC_LEVELBITS;
251 	ack = flag & SLC_ACK;
252 	/*
253 	 * ignore the command if:
254 	 * the function value and level are the same as what we already have;
255 	 * or the level is the same and the ack bit is set
256 	 */
257 	if (hislevel == mylevel && (val == slctab[func].current.val || ack)) {
258 		return;
259 	} else if (ack) {
260 		/*
261 		 * If we get here, we got an ack, but the levels don't match.
262 		 * This shouldn't happen.  If it does, it is probably because
263 		 * we have sent two requests to set a variable without getting
264 		 * a response between them, and this is the first response.
265 		 * So, ignore it, and wait for the next response.
266 		 */
267 		return;
268 	} else {
269 		change_slc(func, flag, val);
270 	}
271 
272 }  /* end of process_slc */
273 
274 /*
275  * change_slc
276  *
277  * Process a request to change one of our special characters.
278  * Compare client's request with what we are capable of supporting.
279  */
280 void
281 change_slc(char func, char flag, cc_t val)
282 {
283 	int hislevel, mylevel;
284 
285 	hislevel = flag & SLC_LEVELBITS;
286 	mylevel = slctab[(int)func].defset.flag & SLC_LEVELBITS;
287 	/*
288 	 * If client is setting a function to NOSUPPORT
289 	 * or DEFAULT, then we can easily and directly
290 	 * accomodate the request.
291 	 */
292 	if (hislevel == SLC_NOSUPPORT) {
293 		slctab[(int)func].current.flag = flag;
294 		slctab[(int)func].current.val = (cc_t)_POSIX_VDISABLE;
295 		flag |= SLC_ACK;
296 		add_slc(func, flag, val);
297 		return;
298 	}
299 	if (hislevel == SLC_DEFAULT) {
300 		/*
301 		 * Special case here.  If client tells us to use
302 		 * the default on a function we don't support, then
303 		 * return NOSUPPORT instead of what we may have as a
304 		 * default level of DEFAULT.
305 		 */
306 		if (mylevel == SLC_DEFAULT) {
307 			slctab[(int)func].current.flag = SLC_NOSUPPORT;
308 		} else {
309 			slctab[(int)func].current.flag = slctab[(int)func].defset.flag;
310 		}
311 		slctab[(int)func].current.val = slctab[(int)func].defset.val;
312 		add_slc(func, slctab[(int)func].current.flag,
313 						slctab[(int)func].current.val);
314 		return;
315 	}
316 
317 	/*
318 	 * Client wants us to change to a new value or he
319 	 * is telling us that he can't change to our value.
320 	 * Some of the slc's we support and can change,
321 	 * some we do support but can't change,
322 	 * and others we don't support at all.
323 	 * If we can change it then we have a pointer to
324 	 * the place to put the new value, so change it,
325 	 * otherwise, continue the negotiation.
326 	 */
327 	if (slctab[(int)func].sptr) {
328 		/*
329 		 * We can change this one.
330 		 */
331 		slctab[(int)func].current.val = val;
332 		*(slctab[(int)func].sptr) = val;
333 		slctab[(int)func].current.flag = flag;
334 		flag |= SLC_ACK;
335 		slcchange = 1;
336 		add_slc(func, flag, val);
337 	} else {
338 		/*
339 		* It is not possible for us to support this
340 		* request as he asks.
341 		*
342 		* If our level is DEFAULT, then just ack whatever was
343 		* sent.
344 		*
345 		* If he can't change and we can't change,
346 		* then degenerate to NOSUPPORT.
347 		*
348 		* Otherwise we send our level back to him, (CANTCHANGE
349 		* or NOSUPPORT) and if CANTCHANGE, send
350 		* our value as well.
351 		*/
352 		if (mylevel == SLC_DEFAULT) {
353 			slctab[(int)func].current.flag = flag;
354 			slctab[(int)func].current.val = val;
355 			flag |= SLC_ACK;
356 		} else if (hislevel == SLC_CANTCHANGE &&
357 				    mylevel == SLC_CANTCHANGE) {
358 			flag &= ~SLC_LEVELBITS;
359 			flag |= SLC_NOSUPPORT;
360 			slctab[(int)func].current.flag = flag;
361 		} else {
362 			flag &= ~SLC_LEVELBITS;
363 			flag |= mylevel;
364 			slctab[(int)func].current.flag = flag;
365 			if (mylevel == SLC_CANTCHANGE) {
366 				slctab[(int)func].current.val =
367 					slctab[(int)func].defset.val;
368 				val = slctab[(int)func].current.val;
369 			}
370 		}
371 		add_slc(func, flag, val);
372 	}
373 
374 }  /* end of change_slc */
375 
376 #if	defined(USE_TERMIO) && (VEOF == VMIN)
377 cc_t oldeofc = '\004';
378 #endif
379 
380 /*
381  * check_slc
382  *
383  * Check the special characters in use and notify the client if any have
384  * changed.  Only those characters that are capable of being changed are
385  * likely to have changed.  If a local change occurs, kick the support level
386  * and flags up to the defaults.
387  */
388 void
389 check_slc(void)
390 {
391 	int i;
392 
393 	for (i = 1; i <= NSLC; i++) {
394 #if	defined(USE_TERMIO) && (VEOF == VMIN)
395 		/*
396 		 * In a perfect world this would be a neat little
397 		 * function.  But in this world, we should not notify
398 		 * client of changes to the VEOF char when
399 		 * ICANON is off, because it is not representing
400 		 * a special character.
401 		 */
402 		if (i == SLC_EOF) {
403 			if (!tty_isediting())
404 				continue;
405 			else if (slctab[i].sptr)
406 				oldeofc = *(slctab[i].sptr);
407 		}
408 #endif	/* defined(USE_TERMIO) && defined(SYSV_TERMIO) */
409 		if (slctab[i].sptr &&
410 				(*(slctab[i].sptr) != slctab[i].current.val)) {
411 			slctab[i].current.val = *(slctab[i].sptr);
412 			if (*(slctab[i].sptr) == (cc_t)_POSIX_VDISABLE)
413 				slctab[i].current.flag = SLC_NOSUPPORT;
414 			else
415 				slctab[i].current.flag = slctab[i].defset.flag;
416 			add_slc((unsigned char)i, slctab[i].current.flag,
417 						slctab[i].current.val);
418 		}
419 	}
420 }  /* check_slc */
421 
422 /*
423  * do_opt_slc
424  *
425  * Process an slc option buffer.  Defer processing of incoming slc's
426  * until after the terminal state has been processed.  Save the first slc
427  * request that comes along, but discard all others.
428  *
429  * ptr points to the beginning of the buffer, len is the length.
430  */
431 void
432 do_opt_slc(unsigned char *ptr, int len)
433 {
434 	unsigned char func, flag;
435 	cc_t val;
436 	unsigned char *end = ptr + len;
437 
438 	if (terminit()) {  /* go ahead */
439 		while (ptr < end) {
440 			func = *ptr++;
441 			if (ptr >= end) break;
442 			flag = *ptr++;
443 			if (ptr >= end) break;
444 			val = (cc_t)*ptr++;
445 
446 			process_slc(func, flag, val);
447 
448 		}
449 	} else {
450 		/*
451 		 * save this slc buffer if it is the first, otherwise dump
452 		 * it.
453 		 */
454 		if (def_slcbuf == (unsigned char *)0) {
455 			def_slclen = len;
456 			def_slcbuf = (unsigned char *)malloc((unsigned)len);
457 			if (def_slcbuf == (unsigned char *)0)
458 				return;  /* too bad */
459 			memmove(def_slcbuf, ptr, len);
460 		}
461 	}
462 
463 }  /* end of do_opt_slc */
464 
465 /*
466  * deferslc
467  *
468  * Do slc stuff that was deferred.
469  */
470 void
471 deferslc(void)
472 {
473 	if (def_slcbuf) {
474 		start_slc(1);
475 		do_opt_slc(def_slcbuf, def_slclen);
476 		(void) end_slc(0);
477 		free(def_slcbuf);
478 		def_slcbuf = (unsigned char *)0;
479 		def_slclen = 0;
480 	}
481 
482 }  /* end of deferslc */
483 
484 #endif	/* LINEMODE */
485