129088Smarkm/* 229088Smarkm * Copyright (c) 1988, 1990, 1993 329088Smarkm * The Regents of the University of California. All rights reserved. 429088Smarkm * 529088Smarkm * Redistribution and use in source and binary forms, with or without 629088Smarkm * modification, are permitted provided that the following conditions 729088Smarkm * are met: 829088Smarkm * 1. Redistributions of source code must retain the above copyright 929088Smarkm * notice, this list of conditions and the following disclaimer. 1029088Smarkm * 2. Redistributions in binary form must reproduce the above copyright 1129088Smarkm * notice, this list of conditions and the following disclaimer in the 1229088Smarkm * documentation and/or other materials provided with the distribution. 1329088Smarkm * 3. All advertising materials mentioning features or use of this software 1429088Smarkm * must display the following acknowledgement: 1529088Smarkm * This product includes software developed by the University of 1629088Smarkm * California, Berkeley and its contributors. 1729088Smarkm * 4. Neither the name of the University nor the names of its contributors 1829088Smarkm * may be used to endorse or promote products derived from this software 1929088Smarkm * without specific prior written permission. 2029088Smarkm * 2129088Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2229088Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2329088Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2429088Smarkm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2529088Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2629088Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2729088Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2829088Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2929088Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3029088Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3129088Smarkm * SUCH DAMAGE. 3229088Smarkm */ 3329088Smarkm 34114630Sobrien#if 0 3529088Smarkm#ifndef lint 3629181Smarkmstatic const char sccsid[] = "@(#)telnet.c 8.4 (Berkeley) 5/30/95"; 3763248Speter#endif 38114630Sobrien#endif 39114630Sobrien#include <sys/cdefs.h> 40114630Sobrien__FBSDID("$FreeBSD$"); 4129088Smarkm 4229088Smarkm#include <sys/types.h> 4329088Smarkm 4429088Smarkm/* By the way, we need to include curses.h before telnet.h since, 4529088Smarkm * among other things, telnet.h #defines 'DO', which is a variable 4629088Smarkm * declared in curses.h. 4729088Smarkm */ 4829088Smarkm 4929088Smarkm#include <ctype.h> 5087139Smarkm#include <curses.h> 5187139Smarkm#include <signal.h> 5229181Smarkm#include <stdlib.h> 5387139Smarkm#include <term.h> 5429181Smarkm#include <unistd.h> 5587139Smarkm#include <arpa/telnet.h> 5629181Smarkm 5729088Smarkm#include "ring.h" 5829088Smarkm 5929088Smarkm#include "defines.h" 6029088Smarkm#include "externs.h" 6129088Smarkm#include "types.h" 6229088Smarkm#include "general.h" 6329088Smarkm 6487139Smarkm#ifdef AUTHENTICATION 6529181Smarkm#include <libtelnet/auth.h> 6629181Smarkm#endif 6787139Smarkm#ifdef ENCRYPTION 6829181Smarkm#include <libtelnet/encrypt.h> 6929181Smarkm#endif 7029181Smarkm#include <libtelnet/misc.h> 7129088Smarkm 7229088Smarkm#define strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x)) 7329088Smarkm 7429088Smarkmstatic unsigned char subbuffer[SUBBUFSIZE], 7529088Smarkm *subpointer, *subend; /* buffer for sub-options */ 7629088Smarkm#define SB_CLEAR() subpointer = subbuffer; 7729088Smarkm#define SB_TERM() { subend = subpointer; SB_CLEAR(); } 7829088Smarkm#define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \ 7929088Smarkm *subpointer++ = (c); \ 8029088Smarkm } 8129088Smarkm 8229088Smarkm#define SB_GET() ((*subpointer++)&0xff) 8329088Smarkm#define SB_PEEK() ((*subpointer)&0xff) 8429088Smarkm#define SB_EOF() (subpointer >= subend) 8529088Smarkm#define SB_LEN() (subend - subpointer) 8629088Smarkm 8729088Smarkmchar options[256]; /* The combined options */ 8829088Smarkmchar do_dont_resp[256]; 8929088Smarkmchar will_wont_resp[256]; 9029088Smarkm 9129088Smarkmint 9229088Smarkm eight = 0, 9329088Smarkm autologin = 0, /* Autologin anyone? */ 9429088Smarkm skiprc = 0, 9529088Smarkm connected, 9629088Smarkm showoptions, 9729088Smarkm ISend, /* trying to send network data in */ 98114911Smarkm telnet_debug = 0, 9929088Smarkm crmod, 10029088Smarkm netdata, /* Print out network data flow */ 10129088Smarkm crlf, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */ 10229088Smarkm telnetport, 10329088Smarkm SYNCHing, /* we are in TELNET SYNCH mode */ 10429088Smarkm flushout, /* flush output */ 10529088Smarkm autoflush = 0, /* flush output when interrupting? */ 10629088Smarkm autosynch, /* send interrupt characters with SYNCH? */ 10729088Smarkm localflow, /* we handle flow control locally */ 10829088Smarkm restartany, /* if flow control enabled, restart on any character */ 10929088Smarkm localchars, /* we recognize interrupt/quit */ 11029088Smarkm donelclchars, /* the user has set "localchars" */ 11129088Smarkm donebinarytoggle, /* the user has put us in binary */ 11229088Smarkm dontlecho, /* do we suppress local echoing right now? */ 11329181Smarkm globalmode, 11447973Sru doaddrlookup = 1, /* do a reverse address lookup? */ 11529181Smarkm clienteof = 0; 11629088Smarkm 11729088Smarkmchar *prompt = 0; 11881965Smarkm#ifdef ENCRYPTION 11976616Speterchar *line; /* hack around breakage in sra.c :-( !! */ 12081965Smarkm#endif 12129088Smarkm 12229088Smarkmcc_t escape; 12329088Smarkmcc_t rlogin; 12429088Smarkm#ifdef KLUDGELINEMODE 12529088Smarkmcc_t echoc; 12629088Smarkm#endif 12729088Smarkm 12829088Smarkm/* 12929088Smarkm * Telnet receiver states for fsm 13029088Smarkm */ 13129088Smarkm#define TS_DATA 0 13229088Smarkm#define TS_IAC 1 13329088Smarkm#define TS_WILL 2 13429088Smarkm#define TS_WONT 3 13529088Smarkm#define TS_DO 4 13629088Smarkm#define TS_DONT 5 13729088Smarkm#define TS_CR 6 13829088Smarkm#define TS_SB 7 /* sub-option collection */ 13929088Smarkm#define TS_SE 8 /* looking for sub-option end */ 14029088Smarkm 14129088Smarkmstatic int telrcv_state; 14229088Smarkm#ifdef OLD_ENVIRON 14329088Smarkmunsigned char telopt_environ = TELOPT_NEW_ENVIRON; 14429088Smarkm#else 14529088Smarkm# define telopt_environ TELOPT_NEW_ENVIRON 14629088Smarkm#endif 14729088Smarkm 14887139Smarkmjmp_buf toplevel; 14929088Smarkm 15029088Smarkmint flushline; 15129088Smarkmint linemode; 15229088Smarkm 15329088Smarkm#ifdef KLUDGELINEMODE 15429088Smarkmint kludgelinemode = 1; 15529088Smarkm#endif 15629088Smarkm 15787139Smarkmstatic int is_unique(char *, char **, char **); 15887139Smarkm 15929088Smarkm/* 16029088Smarkm * The following are some clocks used to decide how to interpret 16129088Smarkm * the relationship between various variables. 16229088Smarkm */ 16329088Smarkm 16429088SmarkmClocks clocks; 16529088Smarkm 16629088Smarkm/* 16729088Smarkm * Initialize telnet environment. 16829088Smarkm */ 16929088Smarkm 17087139Smarkmvoid 17187139Smarkminit_telnet(void) 17229088Smarkm{ 17329088Smarkm env_init(); 17429088Smarkm 17529088Smarkm SB_CLEAR(); 17629088Smarkm ClearArray(options); 17729088Smarkm 17887139Smarkm connected = ISend = localflow = donebinarytoggle = 0; 17987139Smarkm#ifdef AUTHENTICATION 18087139Smarkm#ifdef ENCRYPTION 18129088Smarkm auth_encrypt_connect(connected); 18287139Smarkm#endif 18387139Smarkm#endif 18429088Smarkm restartany = -1; 18529088Smarkm 18629088Smarkm SYNCHing = 0; 18729088Smarkm 18829088Smarkm /* Don't change NetTrace */ 18929088Smarkm 19029088Smarkm escape = CONTROL(']'); 19129088Smarkm rlogin = _POSIX_VDISABLE; 19229088Smarkm#ifdef KLUDGELINEMODE 19329088Smarkm echoc = CONTROL('E'); 19429088Smarkm#endif 19529088Smarkm 19629088Smarkm flushline = 1; 19729088Smarkm telrcv_state = TS_DATA; 19829088Smarkm} 19929088Smarkm 20029088Smarkm 20129088Smarkm/* 20229088Smarkm * These routines are in charge of sending option negotiations 20329088Smarkm * to the other side. 20429088Smarkm * 20529088Smarkm * The basic idea is that we send the negotiation if either side 20629088Smarkm * is in disagreement as to what the current state should be. 20729088Smarkm */ 20829088Smarkm 20987139Smarkmvoid 21087139Smarkmsend_do(int c, int init) 21129088Smarkm{ 21229088Smarkm if (init) { 21329088Smarkm if (((do_dont_resp[c] == 0) && my_state_is_do(c)) || 21429088Smarkm my_want_state_is_do(c)) 21529088Smarkm return; 21629088Smarkm set_my_want_state_do(c); 21729088Smarkm do_dont_resp[c]++; 21829088Smarkm } 219142790Stobez if (telnetport < 0) 220142790Stobez return; 22129088Smarkm NET2ADD(IAC, DO); 22229088Smarkm NETADD(c); 22329088Smarkm printoption("SENT", DO, c); 22429088Smarkm} 22529088Smarkm 22687139Smarkmvoid 22787139Smarkmsend_dont(int c, int init) 22829088Smarkm{ 22929088Smarkm if (init) { 23029088Smarkm if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) || 23129088Smarkm my_want_state_is_dont(c)) 23229088Smarkm return; 23329088Smarkm set_my_want_state_dont(c); 23429088Smarkm do_dont_resp[c]++; 23529088Smarkm } 236142790Stobez if (telnetport < 0) 237142790Stobez return; 23829088Smarkm NET2ADD(IAC, DONT); 23929088Smarkm NETADD(c); 24029088Smarkm printoption("SENT", DONT, c); 24129088Smarkm} 24229088Smarkm 24387139Smarkmvoid 24487139Smarkmsend_will(int c, int init) 24529088Smarkm{ 24629088Smarkm if (init) { 24729088Smarkm if (((will_wont_resp[c] == 0) && my_state_is_will(c)) || 24829088Smarkm my_want_state_is_will(c)) 24929088Smarkm return; 25029088Smarkm set_my_want_state_will(c); 25129088Smarkm will_wont_resp[c]++; 25229088Smarkm } 253142790Stobez if (telnetport < 0) 254142790Stobez return; 25529088Smarkm NET2ADD(IAC, WILL); 25629088Smarkm NETADD(c); 25729088Smarkm printoption("SENT", WILL, c); 25829088Smarkm} 25929088Smarkm 26087139Smarkmvoid 26187139Smarkmsend_wont(int c, int init) 26229088Smarkm{ 26329088Smarkm if (init) { 26429088Smarkm if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) || 26529088Smarkm my_want_state_is_wont(c)) 26629088Smarkm return; 26729088Smarkm set_my_want_state_wont(c); 26829088Smarkm will_wont_resp[c]++; 26929088Smarkm } 270142790Stobez if (telnetport < 0) 271142790Stobez return; 27229088Smarkm NET2ADD(IAC, WONT); 27329088Smarkm NETADD(c); 27429088Smarkm printoption("SENT", WONT, c); 27529088Smarkm} 27629088Smarkm 27787139Smarkmvoid 27887139Smarkmwilloption(int option) 27929088Smarkm{ 28029088Smarkm int new_state_ok = 0; 28129088Smarkm 28229088Smarkm if (do_dont_resp[option]) { 28329088Smarkm --do_dont_resp[option]; 28429088Smarkm if (do_dont_resp[option] && my_state_is_do(option)) 28529088Smarkm --do_dont_resp[option]; 28629088Smarkm } 28729088Smarkm 28829088Smarkm if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) { 28929088Smarkm 29029088Smarkm switch (option) { 29129088Smarkm 29229088Smarkm case TELOPT_ECHO: 29329088Smarkm case TELOPT_BINARY: 29429088Smarkm case TELOPT_SGA: 29529088Smarkm settimer(modenegotiated); 296103955Smarkm /* FALLTHROUGH */ 29729088Smarkm case TELOPT_STATUS: 29887139Smarkm#ifdef AUTHENTICATION 29929088Smarkm case TELOPT_AUTHENTICATION: 30029088Smarkm#endif 30129088Smarkm#ifdef ENCRYPTION 30229088Smarkm case TELOPT_ENCRYPT: 30329088Smarkm#endif /* ENCRYPTION */ 30429088Smarkm new_state_ok = 1; 30529088Smarkm break; 30629088Smarkm 30729088Smarkm case TELOPT_TM: 30829088Smarkm if (flushout) 30929088Smarkm flushout = 0; 31029088Smarkm /* 31129088Smarkm * Special case for TM. If we get back a WILL, 31229088Smarkm * pretend we got back a WONT. 31329088Smarkm */ 31429088Smarkm set_my_want_state_dont(option); 31529088Smarkm set_my_state_dont(option); 31629088Smarkm return; /* Never reply to TM will's/wont's */ 31729088Smarkm 31829088Smarkm case TELOPT_LINEMODE: 31929088Smarkm default: 32029088Smarkm break; 32129088Smarkm } 32229088Smarkm 32329088Smarkm if (new_state_ok) { 32429088Smarkm set_my_want_state_do(option); 32529088Smarkm send_do(option, 0); 32629088Smarkm setconnmode(0); /* possibly set new tty mode */ 32729088Smarkm } else { 32829088Smarkm do_dont_resp[option]++; 32929088Smarkm send_dont(option, 0); 33029088Smarkm } 33129088Smarkm } 33229088Smarkm set_my_state_do(option); 33329088Smarkm#ifdef ENCRYPTION 33429088Smarkm if (option == TELOPT_ENCRYPT) 33529088Smarkm encrypt_send_support(); 33629088Smarkm#endif /* ENCRYPTION */ 33729088Smarkm} 33829088Smarkm 33987139Smarkmvoid 34087139Smarkmwontoption(int option) 34129088Smarkm{ 34229088Smarkm if (do_dont_resp[option]) { 34329088Smarkm --do_dont_resp[option]; 34429088Smarkm if (do_dont_resp[option] && my_state_is_dont(option)) 34529088Smarkm --do_dont_resp[option]; 34629088Smarkm } 34729088Smarkm 34829088Smarkm if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) { 34929088Smarkm 35029088Smarkm switch (option) { 35129088Smarkm 35229088Smarkm#ifdef KLUDGELINEMODE 35329088Smarkm case TELOPT_SGA: 35429088Smarkm if (!kludgelinemode) 35529088Smarkm break; 356103955Smarkm /* FALLTHROUGH */ 35729088Smarkm#endif 35829088Smarkm case TELOPT_ECHO: 35929088Smarkm settimer(modenegotiated); 36029088Smarkm break; 36129088Smarkm 36229088Smarkm case TELOPT_TM: 36329088Smarkm if (flushout) 36429088Smarkm flushout = 0; 36529088Smarkm set_my_want_state_dont(option); 36629088Smarkm set_my_state_dont(option); 36729088Smarkm return; /* Never reply to TM will's/wont's */ 36829088Smarkm 36929088Smarkm default: 37029088Smarkm break; 37129088Smarkm } 37229088Smarkm set_my_want_state_dont(option); 37329088Smarkm if (my_state_is_do(option)) 37429088Smarkm send_dont(option, 0); 37529088Smarkm setconnmode(0); /* Set new tty mode */ 37629088Smarkm } else if (option == TELOPT_TM) { 37729088Smarkm /* 37829088Smarkm * Special case for TM. 37929088Smarkm */ 38029088Smarkm if (flushout) 38129088Smarkm flushout = 0; 38229088Smarkm set_my_want_state_dont(option); 38329088Smarkm } 38429088Smarkm set_my_state_dont(option); 38529088Smarkm} 38629088Smarkm 38787139Smarkmstatic void 38887139Smarkmdooption(int option) 38929088Smarkm{ 39029088Smarkm int new_state_ok = 0; 39129088Smarkm 39229088Smarkm if (will_wont_resp[option]) { 39329088Smarkm --will_wont_resp[option]; 39429088Smarkm if (will_wont_resp[option] && my_state_is_will(option)) 39529088Smarkm --will_wont_resp[option]; 39629088Smarkm } 39729088Smarkm 39829088Smarkm if (will_wont_resp[option] == 0) { 39929088Smarkm if (my_want_state_is_wont(option)) { 40029088Smarkm 40129088Smarkm switch (option) { 40229088Smarkm 40329088Smarkm case TELOPT_TM: 40429088Smarkm /* 40529088Smarkm * Special case for TM. We send a WILL, but pretend 40629088Smarkm * we sent WONT. 40729088Smarkm */ 40829088Smarkm send_will(option, 0); 40929088Smarkm set_my_want_state_wont(TELOPT_TM); 41029088Smarkm set_my_state_wont(TELOPT_TM); 41129088Smarkm return; 41229088Smarkm 41329088Smarkm case TELOPT_BINARY: /* binary mode */ 41429088Smarkm case TELOPT_NAWS: /* window size */ 41529088Smarkm case TELOPT_TSPEED: /* terminal speed */ 41629088Smarkm case TELOPT_LFLOW: /* local flow control */ 41729088Smarkm case TELOPT_TTYPE: /* terminal type option */ 41829088Smarkm case TELOPT_SGA: /* no big deal */ 41929088Smarkm#ifdef ENCRYPTION 42029088Smarkm case TELOPT_ENCRYPT: /* encryption variable option */ 42129088Smarkm#endif /* ENCRYPTION */ 42229088Smarkm new_state_ok = 1; 42329088Smarkm break; 42429088Smarkm 42529088Smarkm case TELOPT_NEW_ENVIRON: /* New environment variable option */ 42629088Smarkm#ifdef OLD_ENVIRON 42729088Smarkm if (my_state_is_will(TELOPT_OLD_ENVIRON)) 42829088Smarkm send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */ 42929088Smarkm goto env_common; 43029088Smarkm case TELOPT_OLD_ENVIRON: /* Old environment variable option */ 43129088Smarkm if (my_state_is_will(TELOPT_NEW_ENVIRON)) 43229088Smarkm break; /* Don't enable if new one is in use! */ 43329088Smarkm env_common: 43429088Smarkm telopt_environ = option; 43529088Smarkm#endif 43629088Smarkm new_state_ok = 1; 43729088Smarkm break; 43829088Smarkm 43987139Smarkm#ifdef AUTHENTICATION 44029088Smarkm case TELOPT_AUTHENTICATION: 44129088Smarkm if (autologin) 44229088Smarkm new_state_ok = 1; 44329088Smarkm break; 44429088Smarkm#endif 44529088Smarkm 44629088Smarkm case TELOPT_XDISPLOC: /* X Display location */ 44787139Smarkm if (env_getvalue("DISPLAY")) 44829088Smarkm new_state_ok = 1; 44929088Smarkm break; 45029088Smarkm 45129088Smarkm case TELOPT_LINEMODE: 45229088Smarkm#ifdef KLUDGELINEMODE 45329088Smarkm kludgelinemode = 0; 45429088Smarkm send_do(TELOPT_SGA, 1); 45529088Smarkm#endif 45629088Smarkm set_my_want_state_will(TELOPT_LINEMODE); 45729088Smarkm send_will(option, 0); 45829088Smarkm set_my_state_will(TELOPT_LINEMODE); 45929088Smarkm slc_init(); 46029088Smarkm return; 46129088Smarkm 46229088Smarkm case TELOPT_ECHO: /* We're never going to echo... */ 46329088Smarkm default: 46429088Smarkm break; 46529088Smarkm } 46629088Smarkm 46729088Smarkm if (new_state_ok) { 46829088Smarkm set_my_want_state_will(option); 46929088Smarkm send_will(option, 0); 47029088Smarkm setconnmode(0); /* Set new tty mode */ 47129088Smarkm } else { 47229088Smarkm will_wont_resp[option]++; 47329088Smarkm send_wont(option, 0); 47429088Smarkm } 47529088Smarkm } else { 47629088Smarkm /* 47729088Smarkm * Handle options that need more things done after the 47829088Smarkm * other side has acknowledged the option. 47929088Smarkm */ 48029088Smarkm switch (option) { 48129088Smarkm case TELOPT_LINEMODE: 48229088Smarkm#ifdef KLUDGELINEMODE 48329088Smarkm kludgelinemode = 0; 48429088Smarkm send_do(TELOPT_SGA, 1); 48529088Smarkm#endif 48629088Smarkm set_my_state_will(option); 48729088Smarkm slc_init(); 48829088Smarkm send_do(TELOPT_SGA, 0); 48929088Smarkm return; 49029088Smarkm } 49129088Smarkm } 49229088Smarkm } 49329088Smarkm set_my_state_will(option); 49429088Smarkm} 49529088Smarkm 49687139Smarkmstatic void 49787139Smarkmdontoption(int option) 49829088Smarkm{ 49929088Smarkm 50029088Smarkm if (will_wont_resp[option]) { 50129088Smarkm --will_wont_resp[option]; 50229088Smarkm if (will_wont_resp[option] && my_state_is_wont(option)) 50329088Smarkm --will_wont_resp[option]; 50429088Smarkm } 50529088Smarkm 50629088Smarkm if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) { 50729088Smarkm switch (option) { 50829088Smarkm case TELOPT_LINEMODE: 50929088Smarkm linemode = 0; /* put us back to the default state */ 51029088Smarkm break; 51129088Smarkm#ifdef OLD_ENVIRON 51229088Smarkm case TELOPT_NEW_ENVIRON: 51329088Smarkm /* 51429088Smarkm * The new environ option wasn't recognized, try 51529088Smarkm * the old one. 51629088Smarkm */ 51729088Smarkm send_will(TELOPT_OLD_ENVIRON, 1); 51829088Smarkm telopt_environ = TELOPT_OLD_ENVIRON; 51929088Smarkm break; 52029088Smarkm#endif 52129088Smarkm } 52229088Smarkm /* we always accept a DONT */ 52329088Smarkm set_my_want_state_wont(option); 52429088Smarkm if (my_state_is_will(option)) 52529088Smarkm send_wont(option, 0); 52629088Smarkm setconnmode(0); /* Set new tty mode */ 52729088Smarkm } 52829088Smarkm set_my_state_wont(option); 52929088Smarkm} 53029088Smarkm 53129088Smarkm/* 53229088Smarkm * Given a buffer returned by tgetent(), this routine will turn 53372089Sasmodai * the pipe separated list of names in the buffer into an array 53429088Smarkm * of pointers to null terminated names. We toss out any bad, 53529088Smarkm * duplicate, or verbose names (names with spaces). 53629088Smarkm */ 53729088Smarkm 53887139Smarkmstatic const char *name_unknown = "UNKNOWN"; 53987139Smarkmstatic const char *unknown[] = { NULL, NULL }; 54029088Smarkm 54187139Smarkmstatic const char ** 54287139Smarkmmklist(char *buf, char *name) 54329088Smarkm{ 54487139Smarkm int n; 54587139Smarkm char c, *cp, **argvp, *cp2, **argv, **avt; 54629088Smarkm 54729088Smarkm if (name) { 54881965Smarkm if (strlen(name) > 40) { 54929088Smarkm name = 0; 55029088Smarkm unknown[0] = name_unknown; 55129088Smarkm } else { 55229088Smarkm unknown[0] = name; 55329088Smarkm upcase(name); 55429088Smarkm } 55529088Smarkm } else 55629088Smarkm unknown[0] = name_unknown; 55729088Smarkm /* 55829088Smarkm * Count up the number of names. 55929088Smarkm */ 56029088Smarkm for (n = 1, cp = buf; *cp && *cp != ':'; cp++) { 56129088Smarkm if (*cp == '|') 56229088Smarkm n++; 56329088Smarkm } 56429088Smarkm /* 56529088Smarkm * Allocate an array to put the name pointers into 56629088Smarkm */ 56729088Smarkm argv = (char **)malloc((n+3)*sizeof(char *)); 56829088Smarkm if (argv == 0) 56929088Smarkm return(unknown); 57029088Smarkm 57129088Smarkm /* 57229088Smarkm * Fill up the array of pointers to names. 57329088Smarkm */ 57429088Smarkm *argv = 0; 57529088Smarkm argvp = argv+1; 57629088Smarkm n = 0; 57729088Smarkm for (cp = cp2 = buf; (c = *cp); cp++) { 57829088Smarkm if (c == '|' || c == ':') { 57929088Smarkm *cp++ = '\0'; 58029088Smarkm /* 58129088Smarkm * Skip entries that have spaces or are over 40 58229088Smarkm * characters long. If this is our environment 58329088Smarkm * name, then put it up front. Otherwise, as 58429088Smarkm * long as this is not a duplicate name (case 58529088Smarkm * insensitive) add it to the list. 58629088Smarkm */ 58729088Smarkm if (n || (cp - cp2 > 41)) 58829088Smarkm ; 58929088Smarkm else if (name && (strncasecmp(name, cp2, cp-cp2) == 0)) 59029088Smarkm *argv = cp2; 59129088Smarkm else if (is_unique(cp2, argv+1, argvp)) 59229088Smarkm *argvp++ = cp2; 59329088Smarkm if (c == ':') 59429088Smarkm break; 59529088Smarkm /* 59629088Smarkm * Skip multiple delimiters. Reset cp2 to 59729088Smarkm * the beginning of the next name. Reset n, 59829088Smarkm * the flag for names with spaces. 59929088Smarkm */ 60029088Smarkm while ((c = *cp) == '|') 60129088Smarkm cp++; 60229088Smarkm cp2 = cp; 60329088Smarkm n = 0; 60429088Smarkm } 60529088Smarkm /* 60629088Smarkm * Skip entries with spaces or non-ascii values. 60729088Smarkm * Convert lower case letters to upper case. 60829088Smarkm */ 60929088Smarkm if ((c == ' ') || !isascii(c)) 61029088Smarkm n = 1; 61129088Smarkm else if (islower(c)) 61229088Smarkm *cp = toupper(c); 61329088Smarkm } 61429088Smarkm 61529088Smarkm /* 61629088Smarkm * Check for an old V6 2 character name. If the second 61729088Smarkm * name points to the beginning of the buffer, and is 61829088Smarkm * only 2 characters long, move it to the end of the array. 61929088Smarkm */ 62029088Smarkm if ((argv[1] == buf) && (strlen(argv[1]) == 2)) { 62129088Smarkm --argvp; 62229088Smarkm for (avt = &argv[1]; avt < argvp; avt++) 62329088Smarkm *avt = *(avt+1); 62429088Smarkm *argvp++ = buf; 62529088Smarkm } 62629088Smarkm 62729088Smarkm /* 62829088Smarkm * Duplicate last name, for TTYPE option, and null 62929088Smarkm * terminate the array. If we didn't find a match on 63029088Smarkm * our terminal name, put that name at the beginning. 63129088Smarkm */ 63229088Smarkm cp = *(argvp-1); 63329088Smarkm *argvp++ = cp; 63429088Smarkm *argvp = 0; 63529088Smarkm 63629088Smarkm if (*argv == 0) { 63729088Smarkm if (name) 63829088Smarkm *argv = name; 63929088Smarkm else { 64029088Smarkm --argvp; 64129088Smarkm for (avt = argv; avt < argvp; avt++) 64229088Smarkm *avt = *(avt+1); 64329088Smarkm } 64429088Smarkm } 64529088Smarkm if (*argv) 64687139Smarkm return((const char **)argv); 64729088Smarkm else 64829088Smarkm return(unknown); 64929088Smarkm} 65029088Smarkm 65187139Smarkmstatic int 65287139Smarkmis_unique(char *name, char **as, char **ae) 65329088Smarkm{ 65487139Smarkm char **ap; 65587139Smarkm int n; 65629088Smarkm 65729088Smarkm n = strlen(name) + 1; 65829088Smarkm for (ap = as; ap < ae; ap++) 65929088Smarkm if (strncasecmp(*ap, name, n) == 0) 66029088Smarkm return(0); 66129088Smarkm return (1); 66229088Smarkm} 66329088Smarkm 66429088Smarkm#ifdef TERMCAP 66529088Smarkmchar termbuf[1024]; 66629088Smarkm 66787139Smarkm/*ARGSUSED*/ 66887139Smarkmstatic int 66987139Smarkmsetupterm(char *tname, int fd, int *errp) 67029088Smarkm{ 67129088Smarkm if (tgetent(termbuf, tname) == 1) { 67229088Smarkm termbuf[1023] = '\0'; 67329088Smarkm if (errp) 67429088Smarkm *errp = 1; 67529088Smarkm return(0); 67629088Smarkm } 67729088Smarkm if (errp) 67829088Smarkm *errp = 0; 67929088Smarkm return(-1); 68029088Smarkm} 68129088Smarkm#else 68229088Smarkm#define termbuf ttytype 68329088Smarkmextern char ttytype[]; 68429088Smarkm#endif 68529088Smarkm 68629088Smarkmint resettermname = 1; 68729088Smarkm 68887139Smarkmstatic const char * 68987139Smarkmgettermname(void) 69029088Smarkm{ 69129088Smarkm char *tname; 69287139Smarkm static const char **tnamep = 0; 69387139Smarkm static const char **next; 69429088Smarkm int err; 69529088Smarkm 69629088Smarkm if (resettermname) { 69729088Smarkm resettermname = 0; 69829088Smarkm if (tnamep && tnamep != unknown) 69929088Smarkm free(tnamep); 70087139Smarkm if ((tname = env_getvalue("TERM")) && 70129088Smarkm (setupterm(tname, 1, &err) == 0)) { 70229088Smarkm tnamep = mklist(termbuf, tname); 70329088Smarkm } else { 70481965Smarkm if (tname && (strlen(tname) <= 40)) { 70529088Smarkm unknown[0] = tname; 70629088Smarkm upcase(tname); 70729088Smarkm } else 70829088Smarkm unknown[0] = name_unknown; 70929088Smarkm tnamep = unknown; 71029088Smarkm } 71129088Smarkm next = tnamep; 71229088Smarkm } 71329088Smarkm if (*next == 0) 71429088Smarkm next = tnamep; 71529088Smarkm return(*next++); 71629088Smarkm} 71729088Smarkm/* 71829088Smarkm * suboption() 71929088Smarkm * 72029088Smarkm * Look at the sub-option buffer, and try to be helpful to the other 72129088Smarkm * side. 72229088Smarkm * 72329088Smarkm * Currently we recognize: 72429088Smarkm * 72529088Smarkm * Terminal type, send request. 72629088Smarkm * Terminal speed (send request). 72729088Smarkm * Local flow control (is request). 72829088Smarkm * Linemode 72929088Smarkm */ 73029088Smarkm 73187139Smarkmstatic void 73287139Smarkmsuboption(void) 73329088Smarkm{ 73429088Smarkm unsigned char subchar; 73529088Smarkm 73629088Smarkm printsub('<', subbuffer, SB_LEN()+2); 73729088Smarkm switch (subchar = SB_GET()) { 73829088Smarkm case TELOPT_TTYPE: 73929088Smarkm if (my_want_state_is_wont(TELOPT_TTYPE)) 74029088Smarkm return; 74129088Smarkm if (SB_EOF() || SB_GET() != TELQUAL_SEND) { 74229088Smarkm return; 74329088Smarkm } else { 74487139Smarkm const char *name; 74529088Smarkm unsigned char temp[50]; 74629088Smarkm int len; 74729088Smarkm 74829088Smarkm name = gettermname(); 74929088Smarkm len = strlen(name) + 4 + 2; 75029088Smarkm if (len < NETROOM()) { 75187139Smarkm sprintf(temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE, 75229088Smarkm TELQUAL_IS, name, IAC, SE); 75329088Smarkm ring_supply_data(&netoring, temp, len); 75429088Smarkm printsub('>', &temp[2], len-2); 75529088Smarkm } else { 75629088Smarkm ExitString("No room in buffer for terminal type.\n", 1); 75729088Smarkm /*NOTREACHED*/ 75829088Smarkm } 75929088Smarkm } 76029088Smarkm break; 76129088Smarkm case TELOPT_TSPEED: 76229088Smarkm if (my_want_state_is_wont(TELOPT_TSPEED)) 76329088Smarkm return; 76429088Smarkm if (SB_EOF()) 76529088Smarkm return; 76629088Smarkm if (SB_GET() == TELQUAL_SEND) { 76729088Smarkm long ospeed, ispeed; 76829088Smarkm unsigned char temp[50]; 76929088Smarkm int len; 77029088Smarkm 77129088Smarkm TerminalSpeeds(&ispeed, &ospeed); 77229088Smarkm 77329181Smarkm sprintf((char *)temp, "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED, 77429088Smarkm TELQUAL_IS, ospeed, ispeed, IAC, SE); 77529088Smarkm len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */ 77629088Smarkm 77729088Smarkm if (len < NETROOM()) { 77829088Smarkm ring_supply_data(&netoring, temp, len); 77929088Smarkm printsub('>', temp+2, len - 2); 78029088Smarkm } 78129088Smarkm/*@*/ else printf("lm_will: not enough room in buffer\n"); 78229088Smarkm } 78329088Smarkm break; 78429088Smarkm case TELOPT_LFLOW: 78529088Smarkm if (my_want_state_is_wont(TELOPT_LFLOW)) 78629088Smarkm return; 78729088Smarkm if (SB_EOF()) 78829088Smarkm return; 78929088Smarkm switch(SB_GET()) { 79029088Smarkm case LFLOW_RESTART_ANY: 79129088Smarkm restartany = 1; 79229088Smarkm break; 79329088Smarkm case LFLOW_RESTART_XON: 79429088Smarkm restartany = 0; 79529088Smarkm break; 79629088Smarkm case LFLOW_ON: 79729088Smarkm localflow = 1; 79829088Smarkm break; 79929088Smarkm case LFLOW_OFF: 80029088Smarkm localflow = 0; 80129088Smarkm break; 80229088Smarkm default: 80329088Smarkm return; 80429088Smarkm } 80529088Smarkm setcommandmode(); 80629088Smarkm setconnmode(0); 80729088Smarkm break; 80829088Smarkm 80929088Smarkm case TELOPT_LINEMODE: 81029088Smarkm if (my_want_state_is_wont(TELOPT_LINEMODE)) 81129088Smarkm return; 81229088Smarkm if (SB_EOF()) 81329088Smarkm return; 81429088Smarkm switch (SB_GET()) { 81529088Smarkm case WILL: 81629088Smarkm lm_will(subpointer, SB_LEN()); 81729088Smarkm break; 81829088Smarkm case WONT: 81929088Smarkm lm_wont(subpointer, SB_LEN()); 82029088Smarkm break; 82129088Smarkm case DO: 82229088Smarkm lm_do(subpointer, SB_LEN()); 82329088Smarkm break; 82429088Smarkm case DONT: 82529088Smarkm lm_dont(subpointer, SB_LEN()); 82629088Smarkm break; 82729088Smarkm case LM_SLC: 82829088Smarkm slc(subpointer, SB_LEN()); 82929088Smarkm break; 83029088Smarkm case LM_MODE: 83129088Smarkm lm_mode(subpointer, SB_LEN(), 0); 83229088Smarkm break; 83329088Smarkm default: 83429088Smarkm break; 83529088Smarkm } 83629088Smarkm break; 83729088Smarkm 83829088Smarkm#ifdef OLD_ENVIRON 83929088Smarkm case TELOPT_OLD_ENVIRON: 84029088Smarkm#endif 84129088Smarkm case TELOPT_NEW_ENVIRON: 84229088Smarkm if (SB_EOF()) 84329088Smarkm return; 84429088Smarkm switch(SB_PEEK()) { 84529088Smarkm case TELQUAL_IS: 84629088Smarkm case TELQUAL_INFO: 84729088Smarkm if (my_want_state_is_dont(subchar)) 84829088Smarkm return; 84929088Smarkm break; 85029088Smarkm case TELQUAL_SEND: 85129088Smarkm if (my_want_state_is_wont(subchar)) { 85229088Smarkm return; 85329088Smarkm } 85429088Smarkm break; 85529088Smarkm default: 85629088Smarkm return; 85729088Smarkm } 85829088Smarkm env_opt(subpointer, SB_LEN()); 85929088Smarkm break; 86029088Smarkm 86129088Smarkm case TELOPT_XDISPLOC: 86229088Smarkm if (my_want_state_is_wont(TELOPT_XDISPLOC)) 86329088Smarkm return; 86429088Smarkm if (SB_EOF()) 86529088Smarkm return; 86629088Smarkm if (SB_GET() == TELQUAL_SEND) { 86729088Smarkm unsigned char temp[50], *dp; 86829088Smarkm int len; 86929088Smarkm 87087139Smarkm if ((dp = env_getvalue("DISPLAY")) == NULL || 87167827Skris strlen(dp) > sizeof(temp) - 7) { 87229088Smarkm /* 87329088Smarkm * Something happened, we no longer have a DISPLAY 87467827Skris * variable. Or it is too long. So, turn off the option. 87529088Smarkm */ 87629088Smarkm send_wont(TELOPT_XDISPLOC, 1); 87729088Smarkm break; 87829088Smarkm } 87987139Smarkm snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB, 88067827Skris TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE); 88129088Smarkm len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */ 88229088Smarkm 88329088Smarkm if (len < NETROOM()) { 88429088Smarkm ring_supply_data(&netoring, temp, len); 88529088Smarkm printsub('>', temp+2, len - 2); 88629088Smarkm } 88729088Smarkm/*@*/ else printf("lm_will: not enough room in buffer\n"); 88829088Smarkm } 88929088Smarkm break; 89029088Smarkm 89187139Smarkm#ifdef AUTHENTICATION 89229088Smarkm case TELOPT_AUTHENTICATION: { 89329088Smarkm if (!autologin) 89429088Smarkm break; 89529088Smarkm if (SB_EOF()) 89629088Smarkm return; 89729088Smarkm switch(SB_GET()) { 89829088Smarkm case TELQUAL_IS: 89929088Smarkm if (my_want_state_is_dont(TELOPT_AUTHENTICATION)) 90029088Smarkm return; 90129088Smarkm auth_is(subpointer, SB_LEN()); 90229088Smarkm break; 90329088Smarkm case TELQUAL_SEND: 90429088Smarkm if (my_want_state_is_wont(TELOPT_AUTHENTICATION)) 90529088Smarkm return; 90629088Smarkm auth_send(subpointer, SB_LEN()); 90729088Smarkm break; 90829088Smarkm case TELQUAL_REPLY: 90929088Smarkm if (my_want_state_is_wont(TELOPT_AUTHENTICATION)) 91029088Smarkm return; 91129088Smarkm auth_reply(subpointer, SB_LEN()); 91229088Smarkm break; 91329088Smarkm case TELQUAL_NAME: 91429088Smarkm if (my_want_state_is_dont(TELOPT_AUTHENTICATION)) 91529088Smarkm return; 91629088Smarkm auth_name(subpointer, SB_LEN()); 91729088Smarkm break; 91829088Smarkm } 91929088Smarkm } 92029088Smarkm break; 92129088Smarkm#endif 92229088Smarkm#ifdef ENCRYPTION 92329088Smarkm case TELOPT_ENCRYPT: 92429088Smarkm if (SB_EOF()) 92529088Smarkm return; 92629088Smarkm switch(SB_GET()) { 92729088Smarkm case ENCRYPT_START: 92829088Smarkm if (my_want_state_is_dont(TELOPT_ENCRYPT)) 92929088Smarkm return; 93029088Smarkm encrypt_start(subpointer, SB_LEN()); 93129088Smarkm break; 93229088Smarkm case ENCRYPT_END: 93329088Smarkm if (my_want_state_is_dont(TELOPT_ENCRYPT)) 93429088Smarkm return; 93529088Smarkm encrypt_end(); 93629088Smarkm break; 93729088Smarkm case ENCRYPT_SUPPORT: 93829088Smarkm if (my_want_state_is_wont(TELOPT_ENCRYPT)) 93929088Smarkm return; 94029088Smarkm encrypt_support(subpointer, SB_LEN()); 94129088Smarkm break; 94229088Smarkm case ENCRYPT_REQSTART: 94329088Smarkm if (my_want_state_is_wont(TELOPT_ENCRYPT)) 94429088Smarkm return; 94529088Smarkm encrypt_request_start(subpointer, SB_LEN()); 94629088Smarkm break; 94729088Smarkm case ENCRYPT_REQEND: 94829088Smarkm if (my_want_state_is_wont(TELOPT_ENCRYPT)) 94929088Smarkm return; 95029088Smarkm /* 95129088Smarkm * We can always send an REQEND so that we cannot 95229088Smarkm * get stuck encrypting. We should only get this 95329088Smarkm * if we have been able to get in the correct mode 95429088Smarkm * anyhow. 95529088Smarkm */ 95629088Smarkm encrypt_request_end(); 95729088Smarkm break; 95829088Smarkm case ENCRYPT_IS: 95929088Smarkm if (my_want_state_is_dont(TELOPT_ENCRYPT)) 96029088Smarkm return; 96129088Smarkm encrypt_is(subpointer, SB_LEN()); 96229088Smarkm break; 96329088Smarkm case ENCRYPT_REPLY: 96429088Smarkm if (my_want_state_is_wont(TELOPT_ENCRYPT)) 96529088Smarkm return; 96629088Smarkm encrypt_reply(subpointer, SB_LEN()); 96729088Smarkm break; 96829088Smarkm case ENCRYPT_ENC_KEYID: 96929088Smarkm if (my_want_state_is_dont(TELOPT_ENCRYPT)) 97029088Smarkm return; 97129088Smarkm encrypt_enc_keyid(subpointer, SB_LEN()); 97229088Smarkm break; 97329088Smarkm case ENCRYPT_DEC_KEYID: 97429088Smarkm if (my_want_state_is_wont(TELOPT_ENCRYPT)) 97529088Smarkm return; 97629088Smarkm encrypt_dec_keyid(subpointer, SB_LEN()); 97729088Smarkm break; 97829088Smarkm default: 97929088Smarkm break; 98029088Smarkm } 98129088Smarkm break; 98229088Smarkm#endif /* ENCRYPTION */ 98329088Smarkm default: 98429088Smarkm break; 98529088Smarkm } 98629088Smarkm} 98729088Smarkm 98829088Smarkmstatic unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE }; 98929088Smarkm 99087139Smarkmvoid 99187139Smarkmlm_will(unsigned char *cmd, int len) 99229088Smarkm{ 99329088Smarkm if (len < 1) { 99429088Smarkm/*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */ 99529088Smarkm return; 99629088Smarkm } 99729088Smarkm switch(cmd[0]) { 99829088Smarkm case LM_FORWARDMASK: /* We shouldn't ever get this... */ 99929088Smarkm default: 100029088Smarkm str_lm[3] = DONT; 100129088Smarkm str_lm[4] = cmd[0]; 100287139Smarkm if (NETROOM() > (int)sizeof(str_lm)) { 100329088Smarkm ring_supply_data(&netoring, str_lm, sizeof(str_lm)); 100429088Smarkm printsub('>', &str_lm[2], sizeof(str_lm)-2); 100529088Smarkm } 100629088Smarkm/*@*/ else printf("lm_will: not enough room in buffer\n"); 100729088Smarkm break; 100829088Smarkm } 100929088Smarkm} 101029088Smarkm 101187139Smarkmvoid 101287139Smarkmlm_wont(unsigned char *cmd, int len) 101329088Smarkm{ 101429088Smarkm if (len < 1) { 101529088Smarkm/*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */ 101629088Smarkm return; 101729088Smarkm } 101829088Smarkm switch(cmd[0]) { 101929088Smarkm case LM_FORWARDMASK: /* We shouldn't ever get this... */ 102029088Smarkm default: 102129088Smarkm /* We are always DONT, so don't respond */ 102229088Smarkm return; 102329088Smarkm } 102429088Smarkm} 102529088Smarkm 102687139Smarkmvoid 102787139Smarkmlm_do(unsigned char *cmd, int len) 102829088Smarkm{ 102929088Smarkm if (len < 1) { 103029088Smarkm/*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */ 103129088Smarkm return; 103229088Smarkm } 103329088Smarkm switch(cmd[0]) { 103429088Smarkm case LM_FORWARDMASK: 103529088Smarkm default: 103629088Smarkm str_lm[3] = WONT; 103729088Smarkm str_lm[4] = cmd[0]; 103887139Smarkm if (NETROOM() > (int)sizeof(str_lm)) { 103929088Smarkm ring_supply_data(&netoring, str_lm, sizeof(str_lm)); 104029088Smarkm printsub('>', &str_lm[2], sizeof(str_lm)-2); 104129088Smarkm } 104229088Smarkm/*@*/ else printf("lm_do: not enough room in buffer\n"); 104329088Smarkm break; 104429088Smarkm } 104529088Smarkm} 104629088Smarkm 104787139Smarkmvoid 104887139Smarkmlm_dont(unsigned char *cmd, int len) 104929088Smarkm{ 105029088Smarkm if (len < 1) { 105129088Smarkm/*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */ 105229088Smarkm return; 105329088Smarkm } 105429088Smarkm switch(cmd[0]) { 105529088Smarkm case LM_FORWARDMASK: 105629088Smarkm default: 105729088Smarkm /* we are always WONT, so don't respond */ 105829088Smarkm break; 105929088Smarkm } 106029088Smarkm} 106129088Smarkm 106229088Smarkmstatic unsigned char str_lm_mode[] = { 106329088Smarkm IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE 106429088Smarkm}; 106529088Smarkm 106687139Smarkmvoid 106787139Smarkmlm_mode(unsigned char *cmd, int len, int init) 106829088Smarkm{ 106929088Smarkm if (len != 1) 107029088Smarkm return; 107129088Smarkm if ((linemode&MODE_MASK&~MODE_ACK) == *cmd) 107229088Smarkm return; 107329088Smarkm if (*cmd&MODE_ACK) 107429088Smarkm return; 107529088Smarkm linemode = *cmd&(MODE_MASK&~MODE_ACK); 107629088Smarkm str_lm_mode[4] = linemode; 107729088Smarkm if (!init) 107829088Smarkm str_lm_mode[4] |= MODE_ACK; 107987139Smarkm if (NETROOM() > (int)sizeof(str_lm_mode)) { 108029088Smarkm ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode)); 108129088Smarkm printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2); 108229088Smarkm } 108329088Smarkm/*@*/ else printf("lm_mode: not enough room in buffer\n"); 108429088Smarkm setconnmode(0); /* set changed mode */ 108529088Smarkm} 108629088Smarkm 108729088Smarkm 108829088Smarkm 108929088Smarkm/* 109029088Smarkm * slc() 109129088Smarkm * Handle special character suboption of LINEMODE. 109229088Smarkm */ 109329088Smarkm 109429088Smarkmstruct spc { 109529088Smarkm cc_t val; 109629088Smarkm cc_t *valp; 109729088Smarkm char flags; /* Current flags & level */ 109829088Smarkm char mylevel; /* Maximum level & flags */ 109929088Smarkm} spc_data[NSLC+1]; 110029088Smarkm 110129088Smarkm#define SLC_IMPORT 0 110229088Smarkm#define SLC_EXPORT 1 110329088Smarkm#define SLC_RVALUE 2 110429088Smarkmstatic int slc_mode = SLC_EXPORT; 110529088Smarkm 110687139Smarkmvoid 110787139Smarkmslc_init(void) 110829088Smarkm{ 110987139Smarkm struct spc *spcp; 111029088Smarkm 111129088Smarkm localchars = 1; 111229088Smarkm for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) { 111329088Smarkm spcp->val = 0; 111429088Smarkm spcp->valp = 0; 111529088Smarkm spcp->flags = spcp->mylevel = SLC_NOSUPPORT; 111629088Smarkm } 111729088Smarkm 111829088Smarkm#define initfunc(func, flags) { \ 111929088Smarkm spcp = &spc_data[func]; \ 112029181Smarkm if ((spcp->valp = tcval(func))) { \ 112129088Smarkm spcp->val = *spcp->valp; \ 112229088Smarkm spcp->mylevel = SLC_VARIABLE|flags; \ 112329088Smarkm } else { \ 112429088Smarkm spcp->val = 0; \ 112529088Smarkm spcp->mylevel = SLC_DEFAULT; \ 112629088Smarkm } \ 112729088Smarkm } 112829088Smarkm 112929088Smarkm initfunc(SLC_SYNCH, 0); 113029088Smarkm /* No BRK */ 113129088Smarkm initfunc(SLC_AO, 0); 113229088Smarkm initfunc(SLC_AYT, 0); 113329088Smarkm /* No EOR */ 113429088Smarkm initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT); 113529088Smarkm initfunc(SLC_EOF, 0); 113629088Smarkm#ifndef SYSV_TERMIO 113729088Smarkm initfunc(SLC_SUSP, SLC_FLUSHIN); 113829088Smarkm#endif 113929088Smarkm initfunc(SLC_EC, 0); 114029088Smarkm initfunc(SLC_EL, 0); 114129088Smarkm#ifndef SYSV_TERMIO 114229088Smarkm initfunc(SLC_EW, 0); 114329088Smarkm initfunc(SLC_RP, 0); 114429088Smarkm initfunc(SLC_LNEXT, 0); 114529088Smarkm#endif 114629088Smarkm initfunc(SLC_XON, 0); 114729088Smarkm initfunc(SLC_XOFF, 0); 114829088Smarkm#ifdef SYSV_TERMIO 114929088Smarkm spc_data[SLC_XON].mylevel = SLC_CANTCHANGE; 115029088Smarkm spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE; 115129088Smarkm#endif 115229088Smarkm initfunc(SLC_FORW1, 0); 115329088Smarkm#ifdef USE_TERMIO 115429088Smarkm initfunc(SLC_FORW2, 0); 115529088Smarkm /* No FORW2 */ 115629088Smarkm#endif 115729088Smarkm 115829088Smarkm initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT); 115929088Smarkm#undef initfunc 116029088Smarkm 116129088Smarkm if (slc_mode == SLC_EXPORT) 116229088Smarkm slc_export(); 116329088Smarkm else 116429088Smarkm slc_import(1); 116529088Smarkm 116629088Smarkm} 116729088Smarkm 116887139Smarkmvoid 116987139Smarkmslcstate(void) 117029088Smarkm{ 117129088Smarkm printf("Special characters are %s values\n", 117229088Smarkm slc_mode == SLC_IMPORT ? "remote default" : 117329088Smarkm slc_mode == SLC_EXPORT ? "local" : 117429088Smarkm "remote"); 117529088Smarkm} 117629088Smarkm 117787139Smarkmvoid 117887139Smarkmslc_mode_export(void) 117929088Smarkm{ 118029088Smarkm slc_mode = SLC_EXPORT; 118129088Smarkm if (my_state_is_will(TELOPT_LINEMODE)) 118229088Smarkm slc_export(); 118329088Smarkm} 118429088Smarkm 118587139Smarkmvoid 118687139Smarkmslc_mode_import(int def) 118729088Smarkm{ 118829088Smarkm slc_mode = def ? SLC_IMPORT : SLC_RVALUE; 118929088Smarkm if (my_state_is_will(TELOPT_LINEMODE)) 119029088Smarkm slc_import(def); 119129088Smarkm} 119229088Smarkm 119329088Smarkmunsigned char slc_import_val[] = { 119429088Smarkm IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE 119529088Smarkm}; 119629088Smarkmunsigned char slc_import_def[] = { 119729088Smarkm IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE 119829088Smarkm}; 119929088Smarkm 120087139Smarkmvoid 120187139Smarkmslc_import(int def) 120229088Smarkm{ 120387139Smarkm if (NETROOM() > (int)sizeof(slc_import_val)) { 120429088Smarkm if (def) { 120529088Smarkm ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def)); 120629088Smarkm printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2); 120729088Smarkm } else { 120829088Smarkm ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val)); 120929088Smarkm printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2); 121029088Smarkm } 121129088Smarkm } 121229088Smarkm/*@*/ else printf("slc_import: not enough room\n"); 121329088Smarkm} 121429088Smarkm 121587139Smarkmvoid 121687139Smarkmslc_export(void) 121729088Smarkm{ 121887139Smarkm struct spc *spcp; 121929088Smarkm 122029088Smarkm TerminalDefaultChars(); 122129088Smarkm 122229088Smarkm slc_start_reply(); 122329088Smarkm for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) { 122429088Smarkm if (spcp->mylevel != SLC_NOSUPPORT) { 122529088Smarkm if (spcp->val == (cc_t)(_POSIX_VDISABLE)) 122629088Smarkm spcp->flags = SLC_NOSUPPORT; 122729088Smarkm else 122829088Smarkm spcp->flags = spcp->mylevel; 122929088Smarkm if (spcp->valp) 123029088Smarkm spcp->val = *spcp->valp; 123129088Smarkm slc_add_reply(spcp - spc_data, spcp->flags, spcp->val); 123229088Smarkm } 123329088Smarkm } 123429088Smarkm slc_end_reply(); 123529088Smarkm (void)slc_update(); 123629088Smarkm setconnmode(1); /* Make sure the character values are set */ 123729088Smarkm} 123829088Smarkm 123987139Smarkmvoid 124087139Smarkmslc(unsigned char *cp, int len) 124129088Smarkm{ 124287139Smarkm struct spc *spcp; 124387139Smarkm int func,level; 124429088Smarkm 124529088Smarkm slc_start_reply(); 124629088Smarkm 124729088Smarkm for (; len >= 3; len -=3, cp +=3) { 124829088Smarkm 124929088Smarkm func = cp[SLC_FUNC]; 125029088Smarkm 125129088Smarkm if (func == 0) { 125229088Smarkm /* 125329088Smarkm * Client side: always ignore 0 function. 125429088Smarkm */ 125529088Smarkm continue; 125629088Smarkm } 125729088Smarkm if (func > NSLC) { 125829088Smarkm if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT) 125929088Smarkm slc_add_reply(func, SLC_NOSUPPORT, 0); 126029088Smarkm continue; 126129088Smarkm } 126229088Smarkm 126329088Smarkm spcp = &spc_data[func]; 126429088Smarkm 126529088Smarkm level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK); 126629088Smarkm 126729088Smarkm if ((cp[SLC_VALUE] == (unsigned char)spcp->val) && 126829088Smarkm ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) { 126929088Smarkm continue; 127029088Smarkm } 127129088Smarkm 127229088Smarkm if (level == (SLC_DEFAULT|SLC_ACK)) { 127329088Smarkm /* 127429088Smarkm * This is an error condition, the SLC_ACK 127529088Smarkm * bit should never be set for the SLC_DEFAULT 127629088Smarkm * level. Our best guess to recover is to 127729088Smarkm * ignore the SLC_ACK bit. 127829088Smarkm */ 127929088Smarkm cp[SLC_FLAGS] &= ~SLC_ACK; 128029088Smarkm } 128129088Smarkm 128229088Smarkm if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) { 128329088Smarkm spcp->val = (cc_t)cp[SLC_VALUE]; 128429088Smarkm spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */ 128529088Smarkm continue; 128629088Smarkm } 128729088Smarkm 128829088Smarkm level &= ~SLC_ACK; 128929088Smarkm 129029088Smarkm if (level <= (spcp->mylevel&SLC_LEVELBITS)) { 129129088Smarkm spcp->flags = cp[SLC_FLAGS]|SLC_ACK; 129229088Smarkm spcp->val = (cc_t)cp[SLC_VALUE]; 129329088Smarkm } 129429088Smarkm if (level == SLC_DEFAULT) { 129529088Smarkm if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT) 129629088Smarkm spcp->flags = spcp->mylevel; 129729088Smarkm else 129829088Smarkm spcp->flags = SLC_NOSUPPORT; 129929088Smarkm } 130029088Smarkm slc_add_reply(func, spcp->flags, spcp->val); 130129088Smarkm } 130229088Smarkm slc_end_reply(); 130329088Smarkm if (slc_update()) 130429088Smarkm setconnmode(1); /* set the new character values */ 130529088Smarkm} 130629088Smarkm 130787139Smarkmvoid 130887139Smarkmslc_check(void) 130929088Smarkm{ 131087139Smarkm struct spc *spcp; 131129088Smarkm 131229088Smarkm slc_start_reply(); 131329088Smarkm for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) { 131429088Smarkm if (spcp->valp && spcp->val != *spcp->valp) { 131529088Smarkm spcp->val = *spcp->valp; 131629088Smarkm if (spcp->val == (cc_t)(_POSIX_VDISABLE)) 131729088Smarkm spcp->flags = SLC_NOSUPPORT; 131829088Smarkm else 131929088Smarkm spcp->flags = spcp->mylevel; 132029088Smarkm slc_add_reply(spcp - spc_data, spcp->flags, spcp->val); 132129088Smarkm } 132229088Smarkm } 132329088Smarkm slc_end_reply(); 132429088Smarkm setconnmode(1); 132529088Smarkm} 132629088Smarkm 132729088Smarkmunsigned char slc_reply[128]; 1328144231Snectarunsigned char const * const slc_reply_eom = &slc_reply[sizeof(slc_reply)]; 132929088Smarkmunsigned char *slc_replyp; 133029088Smarkm 133187139Smarkmvoid 133287139Smarkmslc_start_reply(void) 133329088Smarkm{ 133429088Smarkm slc_replyp = slc_reply; 133529088Smarkm *slc_replyp++ = IAC; 133629088Smarkm *slc_replyp++ = SB; 133729088Smarkm *slc_replyp++ = TELOPT_LINEMODE; 133829088Smarkm *slc_replyp++ = LM_SLC; 133929088Smarkm} 134029088Smarkm 134187139Smarkmvoid 134287139Smarkmslc_add_reply(unsigned char func, unsigned char flags, cc_t value) 134329088Smarkm{ 1344144231Snectar /* A sequence of up to 6 bytes my be written for this member of the SLC 1345144231Snectar * suboption list by this function. The end of negotiation command, 1346144231Snectar * which is written by slc_end_reply(), will require 2 additional 1347144231Snectar * bytes. Do not proceed unless there is sufficient space for these 1348144231Snectar * items. 1349144231Snectar */ 1350144231Snectar if (&slc_replyp[6+2] > slc_reply_eom) 1351144231Snectar return; 135229088Smarkm if ((*slc_replyp++ = func) == IAC) 135329088Smarkm *slc_replyp++ = IAC; 135429088Smarkm if ((*slc_replyp++ = flags) == IAC) 135529088Smarkm *slc_replyp++ = IAC; 135629088Smarkm if ((*slc_replyp++ = (unsigned char)value) == IAC) 135729088Smarkm *slc_replyp++ = IAC; 135829088Smarkm} 135929088Smarkm 136087139Smarkmvoid 136187139Smarkmslc_end_reply(void) 136229088Smarkm{ 136387139Smarkm int len; 136429088Smarkm 1365144231Snectar /* The end of negotiation command requires 2 bytes. */ 1366144231Snectar if (&slc_replyp[2] > slc_reply_eom) 1367144231Snectar return; 136829088Smarkm *slc_replyp++ = IAC; 136929088Smarkm *slc_replyp++ = SE; 137029088Smarkm len = slc_replyp - slc_reply; 137129088Smarkm if (len <= 6) 137229088Smarkm return; 137329088Smarkm if (NETROOM() > len) { 137429088Smarkm ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply); 137529088Smarkm printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2); 137629088Smarkm } 137729088Smarkm/*@*/else printf("slc_end_reply: not enough room\n"); 137829088Smarkm} 137929088Smarkm 138087139Smarkmint 138187139Smarkmslc_update(void) 138229088Smarkm{ 138387139Smarkm struct spc *spcp; 138429088Smarkm int need_update = 0; 138529088Smarkm 138629088Smarkm for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) { 138729088Smarkm if (!(spcp->flags&SLC_ACK)) 138829088Smarkm continue; 138929088Smarkm spcp->flags &= ~SLC_ACK; 139029088Smarkm if (spcp->valp && (*spcp->valp != spcp->val)) { 139129088Smarkm *spcp->valp = spcp->val; 139229088Smarkm need_update = 1; 139329088Smarkm } 139429088Smarkm } 139529088Smarkm return(need_update); 139629088Smarkm} 139729088Smarkm 139829088Smarkm#ifdef OLD_ENVIRON 139929088Smarkm# ifdef ENV_HACK 140029088Smarkm/* 140129088Smarkm * Earlier version of telnet/telnetd from the BSD code had 140229088Smarkm * the definitions of VALUE and VAR reversed. To ensure 140329088Smarkm * maximum interoperability, we assume that the server is 140429088Smarkm * an older BSD server, until proven otherwise. The newer 140529088Smarkm * BSD servers should be able to handle either definition, 140629088Smarkm * so it is better to use the wrong values if we don't 140729088Smarkm * know what type of server it is. 140829088Smarkm */ 140929088Smarkmint env_auto = 1; 141029088Smarkmint old_env_var = OLD_ENV_VAR; 141129088Smarkmint old_env_value = OLD_ENV_VALUE; 141229088Smarkm# else 141329088Smarkm# define old_env_var OLD_ENV_VAR 141429088Smarkm# define old_env_value OLD_ENV_VALUE 141529088Smarkm# endif 141629088Smarkm#endif 141729088Smarkm 141887139Smarkmvoid 141987139Smarkmenv_opt(unsigned char *buf, int len) 142029088Smarkm{ 142187139Smarkm unsigned char *ep = 0, *epc = 0; 142287139Smarkm int i; 142329088Smarkm 142429088Smarkm switch(buf[0]&0xff) { 142529088Smarkm case TELQUAL_SEND: 142629088Smarkm env_opt_start(); 142729088Smarkm if (len == 1) { 142829088Smarkm env_opt_add(NULL); 142929088Smarkm } else for (i = 1; i < len; i++) { 143029088Smarkm switch (buf[i]&0xff) { 143129088Smarkm#ifdef OLD_ENVIRON 143229088Smarkm case OLD_ENV_VAR: 143329088Smarkm# ifdef ENV_HACK 143429088Smarkm if (telopt_environ == TELOPT_OLD_ENVIRON 143529088Smarkm && env_auto) { 143629088Smarkm /* Server has the same definitions */ 143729088Smarkm old_env_var = OLD_ENV_VAR; 143829088Smarkm old_env_value = OLD_ENV_VALUE; 143929088Smarkm } 1440103955Smarkm /* FALLTHROUGH */ 144129088Smarkm# endif 144229088Smarkm case OLD_ENV_VALUE: 144329088Smarkm /* 144429088Smarkm * Although OLD_ENV_VALUE is not legal, we will 144529088Smarkm * still recognize it, just in case it is an 144629088Smarkm * old server that has VAR & VALUE mixed up... 144729088Smarkm */ 1448103955Smarkm /* FALLTHROUGH */ 144929088Smarkm#else 145029088Smarkm case NEW_ENV_VAR: 145129088Smarkm#endif 145229088Smarkm case ENV_USERVAR: 145329088Smarkm if (ep) { 145429088Smarkm *epc = 0; 145529088Smarkm env_opt_add(ep); 145629088Smarkm } 145729088Smarkm ep = epc = &buf[i+1]; 145829088Smarkm break; 145929088Smarkm case ENV_ESC: 146029088Smarkm i++; 1461103955Smarkm /*FALLTHROUGH*/ 146229088Smarkm default: 146329088Smarkm if (epc) 146429088Smarkm *epc++ = buf[i]; 146529088Smarkm break; 146629088Smarkm } 146729088Smarkm } 146829088Smarkm if (ep) { 146929088Smarkm *epc = 0; 147029088Smarkm env_opt_add(ep); 147129088Smarkm } 147229088Smarkm env_opt_end(1); 147329088Smarkm break; 147429088Smarkm 147529088Smarkm case TELQUAL_IS: 147629088Smarkm case TELQUAL_INFO: 147729088Smarkm /* Ignore for now. We shouldn't get it anyway. */ 147829088Smarkm break; 147929088Smarkm 148029088Smarkm default: 148129088Smarkm break; 148229088Smarkm } 148329088Smarkm} 148429088Smarkm 1485144231Snectar#define OPT_REPLY_SIZE (2 * SUBBUFSIZE) 1486144231Snectarunsigned char *opt_reply = NULL; 148729088Smarkmunsigned char *opt_replyp; 148829088Smarkmunsigned char *opt_replyend; 148929088Smarkm 149087139Smarkmvoid 149187139Smarkmenv_opt_start(void) 149229088Smarkm{ 149329088Smarkm if (opt_reply) 149429088Smarkm opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE); 149529088Smarkm else 149629088Smarkm opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE); 149729088Smarkm if (opt_reply == NULL) { 149829088Smarkm/*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n"); 149929088Smarkm opt_reply = opt_replyp = opt_replyend = NULL; 150029088Smarkm return; 150129088Smarkm } 150229088Smarkm opt_replyp = opt_reply; 150329088Smarkm opt_replyend = opt_reply + OPT_REPLY_SIZE; 150429088Smarkm *opt_replyp++ = IAC; 150529088Smarkm *opt_replyp++ = SB; 150629088Smarkm *opt_replyp++ = telopt_environ; 150729088Smarkm *opt_replyp++ = TELQUAL_IS; 150829088Smarkm} 150929088Smarkm 151087139Smarkmvoid 151187139Smarkmenv_opt_start_info(void) 151229088Smarkm{ 151329088Smarkm env_opt_start(); 151429088Smarkm if (opt_replyp) 151529088Smarkm opt_replyp[-1] = TELQUAL_INFO; 151629088Smarkm} 151729088Smarkm 151887139Smarkmvoid 151987139Smarkmenv_opt_add(unsigned char *ep) 152029088Smarkm{ 152187139Smarkm unsigned char *vp, c; 152229088Smarkm 152329088Smarkm if (opt_reply == NULL) /*XXX*/ 152429088Smarkm return; /*XXX*/ 152529088Smarkm 152629088Smarkm if (ep == NULL || *ep == '\0') { 152729088Smarkm /* Send user defined variables first. */ 152829088Smarkm env_default(1, 0); 152929181Smarkm while ((ep = env_default(0, 0))) 153029088Smarkm env_opt_add(ep); 153129088Smarkm 153229088Smarkm /* Now add the list of well know variables. */ 153329088Smarkm env_default(1, 1); 153429181Smarkm while ((ep = env_default(0, 1))) 153529088Smarkm env_opt_add(ep); 153629088Smarkm return; 153729088Smarkm } 153829088Smarkm vp = env_getvalue(ep); 1539144231Snectar if (opt_replyp + (vp ? 2 * strlen((char *)vp) : 0) + 1540144231Snectar 2 * strlen((char *)ep) + 6 > opt_replyend) 1541144231Snectar { 154287139Smarkm int len; 154329088Smarkm opt_replyend += OPT_REPLY_SIZE; 154429088Smarkm len = opt_replyend - opt_reply; 154529088Smarkm opt_reply = (unsigned char *)realloc(opt_reply, len); 154629088Smarkm if (opt_reply == NULL) { 154729088Smarkm/*@*/ printf("env_opt_add: realloc() failed!!!\n"); 154829088Smarkm opt_reply = opt_replyp = opt_replyend = NULL; 154929088Smarkm return; 155029088Smarkm } 155129088Smarkm opt_replyp = opt_reply + len - (opt_replyend - opt_replyp); 155229088Smarkm opt_replyend = opt_reply + len; 155329088Smarkm } 155429088Smarkm if (opt_welldefined(ep)) 155529088Smarkm#ifdef OLD_ENVIRON 155629088Smarkm if (telopt_environ == TELOPT_OLD_ENVIRON) 155729088Smarkm *opt_replyp++ = old_env_var; 155829088Smarkm else 155929088Smarkm#endif 156029088Smarkm *opt_replyp++ = NEW_ENV_VAR; 156129088Smarkm else 156229088Smarkm *opt_replyp++ = ENV_USERVAR; 156329088Smarkm for (;;) { 156429181Smarkm while ((c = *ep++)) { 1565144231Snectar if (opt_replyp + (2 + 2) > opt_replyend) 1566144231Snectar return; 156729088Smarkm switch(c&0xff) { 156829088Smarkm case IAC: 156929088Smarkm *opt_replyp++ = IAC; 157029088Smarkm break; 157129088Smarkm case NEW_ENV_VAR: 157229088Smarkm case NEW_ENV_VALUE: 157329088Smarkm case ENV_ESC: 157429088Smarkm case ENV_USERVAR: 157529088Smarkm *opt_replyp++ = ENV_ESC; 157629088Smarkm break; 157729088Smarkm } 157829088Smarkm *opt_replyp++ = c; 157929088Smarkm } 158029181Smarkm if ((ep = vp)) { 1581144231Snectar if (opt_replyp + (1 + 2 + 2) > opt_replyend) 1582144231Snectar return; 158329088Smarkm#ifdef OLD_ENVIRON 158429088Smarkm if (telopt_environ == TELOPT_OLD_ENVIRON) 158529088Smarkm *opt_replyp++ = old_env_value; 158629088Smarkm else 158729088Smarkm#endif 158829088Smarkm *opt_replyp++ = NEW_ENV_VALUE; 158929088Smarkm vp = NULL; 159029088Smarkm } else 159129088Smarkm break; 159229088Smarkm } 159329088Smarkm} 159429088Smarkm 159587139Smarkmint 159687139Smarkmopt_welldefined(const char *ep) 159729088Smarkm{ 159829088Smarkm if ((strcmp(ep, "USER") == 0) || 159929088Smarkm (strcmp(ep, "DISPLAY") == 0) || 160029088Smarkm (strcmp(ep, "PRINTER") == 0) || 160129088Smarkm (strcmp(ep, "SYSTEMTYPE") == 0) || 160229088Smarkm (strcmp(ep, "JOB") == 0) || 160329088Smarkm (strcmp(ep, "ACCT") == 0)) 160429088Smarkm return(1); 160529088Smarkm return(0); 160629088Smarkm} 160787139Smarkm 160887139Smarkmvoid 160987139Smarkmenv_opt_end(int emptyok) 161029088Smarkm{ 161187139Smarkm int len; 161229088Smarkm 1613144231Snectar if (opt_replyp + 2 > opt_replyend) 1614144231Snectar return; 1615144231Snectar len = opt_replyp + 2 - opt_reply; 161629088Smarkm if (emptyok || len > 6) { 161729088Smarkm *opt_replyp++ = IAC; 161829088Smarkm *opt_replyp++ = SE; 161929088Smarkm if (NETROOM() > len) { 162029088Smarkm ring_supply_data(&netoring, opt_reply, len); 162129088Smarkm printsub('>', &opt_reply[2], len - 2); 162229088Smarkm } 162329088Smarkm/*@*/ else printf("slc_end_reply: not enough room\n"); 162429088Smarkm } 162529088Smarkm if (opt_reply) { 162629088Smarkm free(opt_reply); 162729088Smarkm opt_reply = opt_replyp = opt_replyend = NULL; 162829088Smarkm } 162929088Smarkm} 163029088Smarkm 163129088Smarkm 163229088Smarkm 163387139Smarkmint 163487139Smarkmtelrcv(void) 163529088Smarkm{ 163687139Smarkm int c; 163787139Smarkm int scc; 163887139Smarkm unsigned char *sbp; 163929088Smarkm int count; 164029088Smarkm int returnValue = 0; 164129088Smarkm 164229088Smarkm scc = 0; 164329088Smarkm count = 0; 164429088Smarkm while (TTYROOM() > 2) { 164529088Smarkm if (scc == 0) { 164629088Smarkm if (count) { 164729088Smarkm ring_consumed(&netiring, count); 164829088Smarkm returnValue = 1; 164929088Smarkm count = 0; 165029088Smarkm } 165129088Smarkm sbp = netiring.consume; 165229088Smarkm scc = ring_full_consecutive(&netiring); 165329088Smarkm if (scc == 0) { 165429088Smarkm /* No more data coming in */ 165529088Smarkm break; 165629088Smarkm } 165729088Smarkm } 165829088Smarkm 165929088Smarkm c = *sbp++ & 0xff, scc--; count++; 166029088Smarkm#ifdef ENCRYPTION 166129088Smarkm if (decrypt_input) 166229088Smarkm c = (*decrypt_input)(c); 166329088Smarkm#endif /* ENCRYPTION */ 166429088Smarkm 166529088Smarkm switch (telrcv_state) { 166629088Smarkm 166729088Smarkm case TS_CR: 166829088Smarkm telrcv_state = TS_DATA; 166929088Smarkm if (c == '\0') { 167029088Smarkm break; /* Ignore \0 after CR */ 167129088Smarkm } 167229088Smarkm else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) { 167329088Smarkm TTYADD(c); 167429088Smarkm break; 167529088Smarkm } 1676103955Smarkm /* FALLTHROUGH */ 167729088Smarkm 167829088Smarkm case TS_DATA: 1679142790Stobez if (c == IAC && telnetport >= 0) { 168029088Smarkm telrcv_state = TS_IAC; 168129088Smarkm break; 168229088Smarkm } 168329088Smarkm /* 168429088Smarkm * The 'crmod' hack (see following) is needed 168529088Smarkm * since we can't * set CRMOD on output only. 168629088Smarkm * Machines like MULTICS like to send \r without 168729088Smarkm * \n; since we must turn off CRMOD to get proper 168829088Smarkm * input, the mapping is done here (sigh). 168929088Smarkm */ 169029088Smarkm if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) { 169129088Smarkm if (scc > 0) { 169229088Smarkm c = *sbp&0xff; 169329088Smarkm#ifdef ENCRYPTION 169429088Smarkm if (decrypt_input) 169529088Smarkm c = (*decrypt_input)(c); 169629088Smarkm#endif /* ENCRYPTION */ 169729088Smarkm if (c == 0) { 169829088Smarkm sbp++, scc--; count++; 169929088Smarkm /* a "true" CR */ 170029088Smarkm TTYADD('\r'); 170129088Smarkm } else if (my_want_state_is_dont(TELOPT_ECHO) && 170229088Smarkm (c == '\n')) { 170329088Smarkm sbp++, scc--; count++; 170429088Smarkm TTYADD('\n'); 170529088Smarkm } else { 170629088Smarkm#ifdef ENCRYPTION 170729088Smarkm if (decrypt_input) 170829088Smarkm (*decrypt_input)(-1); 170929088Smarkm#endif /* ENCRYPTION */ 171029088Smarkm 171129088Smarkm TTYADD('\r'); 171229088Smarkm if (crmod) { 171329088Smarkm TTYADD('\n'); 171429088Smarkm } 171529088Smarkm } 171629088Smarkm } else { 171729088Smarkm telrcv_state = TS_CR; 171829088Smarkm TTYADD('\r'); 171929088Smarkm if (crmod) { 172029088Smarkm TTYADD('\n'); 172129088Smarkm } 172229088Smarkm } 172329088Smarkm } else { 172429088Smarkm TTYADD(c); 172529088Smarkm } 172629088Smarkm continue; 172729088Smarkm 172829088Smarkm case TS_IAC: 172929088Smarkmprocess_iac: 173029088Smarkm switch (c) { 173129088Smarkm 173229088Smarkm case WILL: 173329088Smarkm telrcv_state = TS_WILL; 173429088Smarkm continue; 173529088Smarkm 173629088Smarkm case WONT: 173729088Smarkm telrcv_state = TS_WONT; 173829088Smarkm continue; 173929088Smarkm 174029088Smarkm case DO: 174129088Smarkm telrcv_state = TS_DO; 174229088Smarkm continue; 174329088Smarkm 174429088Smarkm case DONT: 174529088Smarkm telrcv_state = TS_DONT; 174629088Smarkm continue; 174729088Smarkm 174829088Smarkm case DM: 174929088Smarkm /* 175029088Smarkm * We may have missed an urgent notification, 175129088Smarkm * so make sure we flush whatever is in the 175229088Smarkm * buffer currently. 175329088Smarkm */ 175429088Smarkm printoption("RCVD", IAC, DM); 175529088Smarkm SYNCHing = 1; 175629088Smarkm (void) ttyflush(1); 175729088Smarkm SYNCHing = stilloob(); 175829088Smarkm settimer(gotDM); 175929088Smarkm break; 176029088Smarkm 176129088Smarkm case SB: 176229088Smarkm SB_CLEAR(); 176329088Smarkm telrcv_state = TS_SB; 176429088Smarkm continue; 176529088Smarkm 176629088Smarkm case IAC: 176729088Smarkm TTYADD(IAC); 176829088Smarkm break; 176929088Smarkm 177029088Smarkm case NOP: 177129088Smarkm case GA: 177229088Smarkm default: 177329088Smarkm printoption("RCVD", IAC, c); 177429088Smarkm break; 177529088Smarkm } 177629088Smarkm telrcv_state = TS_DATA; 177729088Smarkm continue; 177829088Smarkm 177929088Smarkm case TS_WILL: 178029088Smarkm printoption("RCVD", WILL, c); 178129088Smarkm willoption(c); 178229088Smarkm telrcv_state = TS_DATA; 178329088Smarkm continue; 178429088Smarkm 178529088Smarkm case TS_WONT: 178629088Smarkm printoption("RCVD", WONT, c); 178729088Smarkm wontoption(c); 178829088Smarkm telrcv_state = TS_DATA; 178929088Smarkm continue; 179029088Smarkm 179129088Smarkm case TS_DO: 179229088Smarkm printoption("RCVD", DO, c); 179329088Smarkm dooption(c); 179429088Smarkm if (c == TELOPT_NAWS) { 179529088Smarkm sendnaws(); 179629088Smarkm } else if (c == TELOPT_LFLOW) { 179729088Smarkm localflow = 1; 179829088Smarkm setcommandmode(); 179929088Smarkm setconnmode(0); 180029088Smarkm } 180129088Smarkm telrcv_state = TS_DATA; 180229088Smarkm continue; 180329088Smarkm 180429088Smarkm case TS_DONT: 180529088Smarkm printoption("RCVD", DONT, c); 180629088Smarkm dontoption(c); 180729088Smarkm flushline = 1; 180829088Smarkm setconnmode(0); /* set new tty mode (maybe) */ 180929088Smarkm telrcv_state = TS_DATA; 181029088Smarkm continue; 181129088Smarkm 181229088Smarkm case TS_SB: 181329088Smarkm if (c == IAC) { 181429088Smarkm telrcv_state = TS_SE; 181529088Smarkm } else { 181629088Smarkm SB_ACCUM(c); 181729088Smarkm } 181829088Smarkm continue; 181929088Smarkm 182029088Smarkm case TS_SE: 182129088Smarkm if (c != SE) { 182229088Smarkm if (c != IAC) { 182329088Smarkm /* 182429088Smarkm * This is an error. We only expect to get 182529088Smarkm * "IAC IAC" or "IAC SE". Several things may 182629088Smarkm * have happend. An IAC was not doubled, the 182729088Smarkm * IAC SE was left off, or another option got 182829088Smarkm * inserted into the suboption are all possibilities. 182929088Smarkm * If we assume that the IAC was not doubled, 183029088Smarkm * and really the IAC SE was left off, we could 183129088Smarkm * get into an infinate loop here. So, instead, 183229088Smarkm * we terminate the suboption, and process the 183329088Smarkm * partial suboption if we can. 183429088Smarkm */ 183529088Smarkm SB_ACCUM(IAC); 183629088Smarkm SB_ACCUM(c); 183729088Smarkm subpointer -= 2; 183829088Smarkm SB_TERM(); 183929088Smarkm 184029088Smarkm printoption("In SUBOPTION processing, RCVD", IAC, c); 184129088Smarkm suboption(); /* handle sub-option */ 184229088Smarkm telrcv_state = TS_IAC; 184329088Smarkm goto process_iac; 184429088Smarkm } 184529088Smarkm SB_ACCUM(c); 184629088Smarkm telrcv_state = TS_SB; 184729088Smarkm } else { 184829088Smarkm SB_ACCUM(IAC); 184929088Smarkm SB_ACCUM(SE); 185029088Smarkm subpointer -= 2; 185129088Smarkm SB_TERM(); 185229088Smarkm suboption(); /* handle sub-option */ 185329088Smarkm telrcv_state = TS_DATA; 185429088Smarkm } 185529088Smarkm } 185629088Smarkm } 185729088Smarkm if (count) 185829088Smarkm ring_consumed(&netiring, count); 185929088Smarkm return returnValue||count; 186029088Smarkm} 186129088Smarkm 186229088Smarkmstatic int bol = 1, local = 0; 186329088Smarkm 186487139Smarkmint 186587139Smarkmrlogin_susp(void) 186629088Smarkm{ 186729088Smarkm if (local) { 186829088Smarkm local = 0; 186929088Smarkm bol = 1; 187029088Smarkm command(0, "z\n", 2); 187129088Smarkm return(1); 187229088Smarkm } 187329088Smarkm return(0); 187429088Smarkm} 187529088Smarkm 187687139Smarkmstatic int 187787139Smarkmtelsnd(void) 187829088Smarkm{ 187929088Smarkm int tcc; 188029088Smarkm int count; 188129088Smarkm int returnValue = 0; 188229088Smarkm unsigned char *tbp; 188329088Smarkm 188429088Smarkm tcc = 0; 188529088Smarkm count = 0; 188629088Smarkm while (NETROOM() > 2) { 188787139Smarkm int sc; 188887139Smarkm int c; 188929088Smarkm 189029088Smarkm if (tcc == 0) { 189129088Smarkm if (count) { 189229088Smarkm ring_consumed(&ttyiring, count); 189329088Smarkm returnValue = 1; 189429088Smarkm count = 0; 189529088Smarkm } 189629088Smarkm tbp = ttyiring.consume; 189729088Smarkm tcc = ring_full_consecutive(&ttyiring); 189829088Smarkm if (tcc == 0) { 189929088Smarkm break; 190029088Smarkm } 190129088Smarkm } 190229088Smarkm c = *tbp++ & 0xff, sc = strip(c), tcc--; count++; 190329088Smarkm if (rlogin != _POSIX_VDISABLE) { 190429088Smarkm if (bol) { 190529088Smarkm bol = 0; 190629088Smarkm if (sc == rlogin) { 190729088Smarkm local = 1; 190829088Smarkm continue; 190929088Smarkm } 191029088Smarkm } else if (local) { 191129088Smarkm local = 0; 191229088Smarkm if (sc == '.' || c == termEofChar) { 191329088Smarkm bol = 1; 191429088Smarkm command(0, "close\n", 6); 191529088Smarkm continue; 191629088Smarkm } 191729088Smarkm if (sc == termSuspChar) { 191829088Smarkm bol = 1; 191929088Smarkm command(0, "z\n", 2); 192029088Smarkm continue; 192129088Smarkm } 192229088Smarkm if (sc == escape) { 192387139Smarkm command(0, tbp, tcc); 192429088Smarkm bol = 1; 192529088Smarkm count += tcc; 192629088Smarkm tcc = 0; 192729088Smarkm flushline = 1; 192829088Smarkm break; 192929088Smarkm } 193029088Smarkm if (sc != rlogin) { 193129088Smarkm ++tcc; 193229088Smarkm --tbp; 193329088Smarkm --count; 193429088Smarkm c = sc = rlogin; 193529088Smarkm } 193629088Smarkm } 193729088Smarkm if ((sc == '\n') || (sc == '\r')) 193829088Smarkm bol = 1; 193947973Sru } else if (escape != _POSIX_VDISABLE && sc == escape) { 194029088Smarkm /* 194129088Smarkm * Double escape is a pass through of a single escape character. 194229088Smarkm */ 194329088Smarkm if (tcc && strip(*tbp) == escape) { 194429088Smarkm tbp++; 194529088Smarkm tcc--; 194629088Smarkm count++; 194729088Smarkm bol = 0; 194829088Smarkm } else { 194929088Smarkm command(0, (char *)tbp, tcc); 195029088Smarkm bol = 1; 195129088Smarkm count += tcc; 195229088Smarkm tcc = 0; 195329088Smarkm flushline = 1; 195429088Smarkm break; 195529088Smarkm } 195629088Smarkm } else 195729088Smarkm bol = 0; 195829088Smarkm#ifdef KLUDGELINEMODE 195929088Smarkm if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) { 196029088Smarkm if (tcc > 0 && strip(*tbp) == echoc) { 196129088Smarkm tcc--; tbp++; count++; 196229088Smarkm } else { 196329088Smarkm dontlecho = !dontlecho; 196429088Smarkm settimer(echotoggle); 196529088Smarkm setconnmode(0); 196629088Smarkm flushline = 1; 196729088Smarkm break; 196829088Smarkm } 196929088Smarkm } 197029088Smarkm#endif 197129088Smarkm if (MODE_LOCAL_CHARS(globalmode)) { 197229088Smarkm if (TerminalSpecialChars(sc) == 0) { 197329088Smarkm bol = 1; 197429088Smarkm break; 197529088Smarkm } 197629088Smarkm } 197729088Smarkm if (my_want_state_is_wont(TELOPT_BINARY)) { 197829088Smarkm switch (c) { 197929088Smarkm case '\n': 198029088Smarkm /* 198129088Smarkm * If we are in CRMOD mode (\r ==> \n) 198229088Smarkm * on our local machine, then probably 198329088Smarkm * a newline (unix) is CRLF (TELNET). 198429088Smarkm */ 198529088Smarkm if (MODE_LOCAL_CHARS(globalmode)) { 198629088Smarkm NETADD('\r'); 198729088Smarkm } 198829088Smarkm NETADD('\n'); 198929088Smarkm bol = flushline = 1; 199029088Smarkm break; 199129088Smarkm case '\r': 199229088Smarkm if (!crlf) { 199329088Smarkm NET2ADD('\r', '\0'); 199429088Smarkm } else { 199529088Smarkm NET2ADD('\r', '\n'); 199629088Smarkm } 199729088Smarkm bol = flushline = 1; 199829088Smarkm break; 199929088Smarkm case IAC: 200029088Smarkm NET2ADD(IAC, IAC); 200129088Smarkm break; 200229088Smarkm default: 200329088Smarkm NETADD(c); 200429088Smarkm break; 200529088Smarkm } 200629088Smarkm } else if (c == IAC) { 200729088Smarkm NET2ADD(IAC, IAC); 200829088Smarkm } else { 200929088Smarkm NETADD(c); 201029088Smarkm } 201129088Smarkm } 201229088Smarkm if (count) 201329088Smarkm ring_consumed(&ttyiring, count); 201429088Smarkm return returnValue||count; /* Non-zero if we did anything */ 201529088Smarkm} 201629088Smarkm 201729088Smarkm/* 201829088Smarkm * Scheduler() 201929088Smarkm * 202029088Smarkm * Try to do something. 202129088Smarkm * 202229088Smarkm * If we do something useful, return 1; else return 0. 202329088Smarkm * 202429088Smarkm */ 202529088Smarkm 202687139Smarkmstatic int 202787139SmarkmScheduler(int block) 202829088Smarkm{ 202929088Smarkm /* One wants to be a bit careful about setting returnValue 203029088Smarkm * to one, since a one implies we did some useful work, 203129088Smarkm * and therefore probably won't be called to block next 203229088Smarkm */ 203329088Smarkm int returnValue; 203429088Smarkm int netin, netout, netex, ttyin, ttyout; 203529088Smarkm 203629088Smarkm /* Decide which rings should be processed */ 203729088Smarkm 203829088Smarkm netout = ring_full_count(&netoring) && 203929088Smarkm (flushline || 204029088Smarkm (my_want_state_is_wont(TELOPT_LINEMODE) 204129088Smarkm#ifdef KLUDGELINEMODE 204229088Smarkm && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA)) 204329088Smarkm#endif 204429088Smarkm ) || 204529088Smarkm my_want_state_is_will(TELOPT_BINARY)); 204629088Smarkm ttyout = ring_full_count(&ttyoring); 204729088Smarkm 204829181Smarkm ttyin = ring_empty_count(&ttyiring) && (clienteof == 0); 204929088Smarkm 205029088Smarkm netin = !ISend && ring_empty_count(&netiring); 205129088Smarkm 205229088Smarkm netex = !SYNCHing; 205329088Smarkm 205429088Smarkm /* Call to system code to process rings */ 205529088Smarkm 205629088Smarkm returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block); 205729088Smarkm 205829088Smarkm /* Now, look at the input rings, looking for work to do. */ 205929088Smarkm 206029088Smarkm if (ring_full_count(&ttyiring)) { 206129088Smarkm returnValue |= telsnd(); 206229088Smarkm } 206329088Smarkm 206429088Smarkm if (ring_full_count(&netiring)) { 206529088Smarkm returnValue |= telrcv(); 206629088Smarkm } 206729088Smarkm return returnValue; 206829088Smarkm} 206929088Smarkm 207087139Smarkm#ifdef AUTHENTICATION 207187139Smarkm#define __unusedhere 207287139Smarkm#else 207387139Smarkm#define __unusedhere __unused 207487139Smarkm#endif 207529088Smarkm/* 207629088Smarkm * Select from tty and network... 207729088Smarkm */ 207887139Smarkmvoid 207987139Smarkmtelnet(char *user __unusedhere) 208029088Smarkm{ 208129088Smarkm sys_telnet_init(); 208229088Smarkm 208387139Smarkm#ifdef AUTHENTICATION 208487139Smarkm#ifdef ENCRYPTION 208529088Smarkm { 208629088Smarkm static char local_host[256] = { 0 }; 208729088Smarkm 208829088Smarkm if (!local_host[0]) { 208929088Smarkm gethostname(local_host, sizeof(local_host)); 209029088Smarkm local_host[sizeof(local_host)-1] = 0; 209129088Smarkm } 209229088Smarkm auth_encrypt_init(local_host, hostname, "TELNET", 0); 209329088Smarkm auth_encrypt_user(user); 209429088Smarkm } 209587139Smarkm#endif 209687139Smarkm#endif 2097142790Stobez if (telnetport > 0) { 209887139Smarkm#ifdef AUTHENTICATION 209929088Smarkm if (autologin) 210029088Smarkm send_will(TELOPT_AUTHENTICATION, 1); 210129088Smarkm#endif 210229088Smarkm#ifdef ENCRYPTION 210329088Smarkm send_do(TELOPT_ENCRYPT, 1); 210429088Smarkm send_will(TELOPT_ENCRYPT, 1); 210529088Smarkm#endif /* ENCRYPTION */ 210629088Smarkm send_do(TELOPT_SGA, 1); 210729088Smarkm send_will(TELOPT_TTYPE, 1); 210829088Smarkm send_will(TELOPT_NAWS, 1); 210929088Smarkm send_will(TELOPT_TSPEED, 1); 211029088Smarkm send_will(TELOPT_LFLOW, 1); 211129088Smarkm send_will(TELOPT_LINEMODE, 1); 211229088Smarkm send_will(TELOPT_NEW_ENVIRON, 1); 211329088Smarkm send_do(TELOPT_STATUS, 1); 211487139Smarkm if (env_getvalue("DISPLAY")) 211529088Smarkm send_will(TELOPT_XDISPLOC, 1); 211629088Smarkm if (eight) 211729088Smarkm tel_enter_binary(eight); 211829088Smarkm } 211929088Smarkm 212029088Smarkm for (;;) { 212129088Smarkm int schedValue; 212229088Smarkm 212329088Smarkm while ((schedValue = Scheduler(0)) != 0) { 212429088Smarkm if (schedValue == -1) { 212529088Smarkm setcommandmode(); 212629088Smarkm return; 212729088Smarkm } 212829088Smarkm } 212929088Smarkm 213029088Smarkm if (Scheduler(1) == -1) { 213129088Smarkm setcommandmode(); 213229088Smarkm return; 213329088Smarkm } 213429088Smarkm } 213529088Smarkm} 213629088Smarkm 213729088Smarkm#if 0 /* XXX - this not being in is a bug */ 213829088Smarkm/* 213929088Smarkm * nextitem() 214029088Smarkm * 214129088Smarkm * Return the address of the next "item" in the TELNET data 214229088Smarkm * stream. This will be the address of the next character if 214329088Smarkm * the current address is a user data character, or it will 214429088Smarkm * be the address of the character following the TELNET command 214529088Smarkm * if the current address is a TELNET IAC ("I Am a Command") 214629088Smarkm * character. 214729088Smarkm */ 214829088Smarkm 214987139Smarkmstatic char * 215087139Smarkmnextitem(char *current) 215129088Smarkm{ 215229088Smarkm if ((*current&0xff) != IAC) { 215329088Smarkm return current+1; 215429088Smarkm } 215529088Smarkm switch (*(current+1)&0xff) { 215629088Smarkm case DO: 215729088Smarkm case DONT: 215829088Smarkm case WILL: 215929088Smarkm case WONT: 216029088Smarkm return current+3; 216129088Smarkm case SB: /* loop forever looking for the SE */ 216229088Smarkm { 216387139Smarkm char *look = current+2; 216429088Smarkm 216529088Smarkm for (;;) { 216629088Smarkm if ((*look++&0xff) == IAC) { 216729088Smarkm if ((*look++&0xff) == SE) { 216829088Smarkm return look; 216929088Smarkm } 217029088Smarkm } 217129088Smarkm } 217229088Smarkm } 217329088Smarkm default: 217429088Smarkm return current+2; 217529088Smarkm } 217629088Smarkm} 217729088Smarkm#endif /* 0 */ 217829088Smarkm 217929088Smarkm/* 218029088Smarkm * netclear() 218129088Smarkm * 218229088Smarkm * We are about to do a TELNET SYNCH operation. Clear 218329088Smarkm * the path to the network. 218429088Smarkm * 218529088Smarkm * Things are a bit tricky since we may have sent the first 218629088Smarkm * byte or so of a previous TELNET command into the network. 218729088Smarkm * So, we have to scan the network buffer from the beginning 218829088Smarkm * until we are up to where we want to be. 218929088Smarkm * 219029088Smarkm * A side effect of what we do, just to keep things 219129088Smarkm * simple, is to clear the urgent data pointer. The principal 219229088Smarkm * caller should be setting the urgent data pointer AFTER calling 219329088Smarkm * us in any case. 219429088Smarkm */ 219529088Smarkm 219687139Smarkmstatic void 219787139Smarkmnetclear(void) 219829088Smarkm{ 219987139Smarkm /* Deleted */ 220029088Smarkm} 220129088Smarkm 220229088Smarkm/* 220329088Smarkm * These routines add various telnet commands to the data stream. 220429088Smarkm */ 220529088Smarkm 220687139Smarkmstatic void 220787139Smarkmdoflush(void) 220829088Smarkm{ 220929088Smarkm NET2ADD(IAC, DO); 221029088Smarkm NETADD(TELOPT_TM); 221129088Smarkm flushline = 1; 221229088Smarkm flushout = 1; 221329088Smarkm (void) ttyflush(1); /* Flush/drop output */ 221429088Smarkm /* do printoption AFTER flush, otherwise the output gets tossed... */ 221529088Smarkm printoption("SENT", DO, TELOPT_TM); 221629088Smarkm} 221729088Smarkm 221887139Smarkmvoid 221987139SmarkmxmitAO(void) 222029088Smarkm{ 222129088Smarkm NET2ADD(IAC, AO); 222229088Smarkm printoption("SENT", IAC, AO); 222329088Smarkm if (autoflush) { 222429088Smarkm doflush(); 222529088Smarkm } 222629088Smarkm} 222729088Smarkm 222887139Smarkmvoid 222987139SmarkmxmitEL(void) 223029088Smarkm{ 223129088Smarkm NET2ADD(IAC, EL); 223229088Smarkm printoption("SENT", IAC, EL); 223329088Smarkm} 223429088Smarkm 223587139Smarkmvoid 223687139SmarkmxmitEC(void) 223729088Smarkm{ 223829088Smarkm NET2ADD(IAC, EC); 223929088Smarkm printoption("SENT", IAC, EC); 224029088Smarkm} 224129088Smarkm 224287139Smarkmint 224387139Smarkmdosynch(char *ch __unused) 224429088Smarkm{ 224529088Smarkm netclear(); /* clear the path to the network */ 224629088Smarkm NETADD(IAC); 224729088Smarkm setneturg(); 224829088Smarkm NETADD(DM); 224929088Smarkm printoption("SENT", IAC, DM); 225029088Smarkm return 1; 225129088Smarkm} 225229088Smarkm 225329088Smarkmint want_status_response = 0; 225429088Smarkm 225587139Smarkmint 225687139Smarkmget_status(char *ch __unused) 225729088Smarkm{ 225829088Smarkm unsigned char tmp[16]; 225987139Smarkm unsigned char *cp; 226029088Smarkm 226129088Smarkm if (my_want_state_is_dont(TELOPT_STATUS)) { 226229088Smarkm printf("Remote side does not support STATUS option\n"); 226329088Smarkm return 0; 226429088Smarkm } 226529088Smarkm cp = tmp; 226629088Smarkm 226729088Smarkm *cp++ = IAC; 226829088Smarkm *cp++ = SB; 226929088Smarkm *cp++ = TELOPT_STATUS; 227029088Smarkm *cp++ = TELQUAL_SEND; 227129088Smarkm *cp++ = IAC; 227229088Smarkm *cp++ = SE; 227329088Smarkm if (NETROOM() >= cp - tmp) { 227429088Smarkm ring_supply_data(&netoring, tmp, cp-tmp); 227529088Smarkm printsub('>', tmp+2, cp - tmp - 2); 227629088Smarkm } 227729088Smarkm ++want_status_response; 227829088Smarkm return 1; 227929088Smarkm} 228029088Smarkm 228187139Smarkmvoid 228287139Smarkmintp(void) 228329088Smarkm{ 228429088Smarkm NET2ADD(IAC, IP); 228529088Smarkm printoption("SENT", IAC, IP); 228629088Smarkm flushline = 1; 228729088Smarkm if (autoflush) { 228829088Smarkm doflush(); 228929088Smarkm } 229029088Smarkm if (autosynch) { 229187139Smarkm dosynch(NULL); 229229088Smarkm } 229329088Smarkm} 229429088Smarkm 229587139Smarkmvoid 229687139Smarkmsendbrk(void) 229729088Smarkm{ 229829088Smarkm NET2ADD(IAC, BREAK); 229929088Smarkm printoption("SENT", IAC, BREAK); 230029088Smarkm flushline = 1; 230129088Smarkm if (autoflush) { 230229088Smarkm doflush(); 230329088Smarkm } 230429088Smarkm if (autosynch) { 230587139Smarkm dosynch(NULL); 230629088Smarkm } 230729088Smarkm} 230829088Smarkm 230987139Smarkmvoid 231087139Smarkmsendabort(void) 231129088Smarkm{ 231229088Smarkm NET2ADD(IAC, ABORT); 231329088Smarkm printoption("SENT", IAC, ABORT); 231429088Smarkm flushline = 1; 231529088Smarkm if (autoflush) { 231629088Smarkm doflush(); 231729088Smarkm } 231829088Smarkm if (autosynch) { 231987139Smarkm dosynch(NULL); 232029088Smarkm } 232129088Smarkm} 232229088Smarkm 232387139Smarkmvoid 232487139Smarkmsendsusp(void) 232529088Smarkm{ 232629088Smarkm NET2ADD(IAC, SUSP); 232729088Smarkm printoption("SENT", IAC, SUSP); 232829088Smarkm flushline = 1; 232929088Smarkm if (autoflush) { 233029088Smarkm doflush(); 233129088Smarkm } 233229088Smarkm if (autosynch) { 233387139Smarkm dosynch(NULL); 233429088Smarkm } 233529088Smarkm} 233629088Smarkm 233787139Smarkmvoid 233887139Smarkmsendeof(void) 233929088Smarkm{ 234029088Smarkm NET2ADD(IAC, xEOF); 234129088Smarkm printoption("SENT", IAC, xEOF); 234229088Smarkm} 234329088Smarkm 234487139Smarkmvoid 234587139Smarkmsendayt(void) 234629088Smarkm{ 234729088Smarkm NET2ADD(IAC, AYT); 234829088Smarkm printoption("SENT", IAC, AYT); 234929088Smarkm} 235029088Smarkm 235129088Smarkm/* 235229088Smarkm * Send a window size update to the remote system. 235329088Smarkm */ 235429088Smarkm 235587139Smarkmvoid 235687139Smarkmsendnaws(void) 235729088Smarkm{ 235829088Smarkm long rows, cols; 235929088Smarkm unsigned char tmp[16]; 236087139Smarkm unsigned char *cp; 236129088Smarkm 236229088Smarkm if (my_state_is_wont(TELOPT_NAWS)) 236329088Smarkm return; 236429088Smarkm 236529088Smarkm#define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \ 236629088Smarkm if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; } 236729088Smarkm 236829088Smarkm if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */ 236929088Smarkm return; 237029088Smarkm } 237129088Smarkm 237229088Smarkm cp = tmp; 237329088Smarkm 237429088Smarkm *cp++ = IAC; 237529088Smarkm *cp++ = SB; 237629088Smarkm *cp++ = TELOPT_NAWS; 237729088Smarkm PUTSHORT(cp, cols); 237829088Smarkm PUTSHORT(cp, rows); 237929088Smarkm *cp++ = IAC; 238029088Smarkm *cp++ = SE; 238129088Smarkm if (NETROOM() >= cp - tmp) { 238229088Smarkm ring_supply_data(&netoring, tmp, cp-tmp); 238329088Smarkm printsub('>', tmp+2, cp - tmp - 2); 238429088Smarkm } 238529088Smarkm} 238629088Smarkm 238787139Smarkmvoid 238887139Smarkmtel_enter_binary(int rw) 238929088Smarkm{ 239029088Smarkm if (rw&1) 239129088Smarkm send_do(TELOPT_BINARY, 1); 239229088Smarkm if (rw&2) 239329088Smarkm send_will(TELOPT_BINARY, 1); 239429088Smarkm} 239529088Smarkm 239687139Smarkmvoid 239787139Smarkmtel_leave_binary(int rw) 239829088Smarkm{ 239929088Smarkm if (rw&1) 240029088Smarkm send_dont(TELOPT_BINARY, 1); 240129088Smarkm if (rw&2) 240229088Smarkm send_wont(TELOPT_BINARY, 1); 240329088Smarkm} 2404