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