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