home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckv200.zip / ckctel.c < prev    next >
C/C++ Source or Header  |  2001-11-17  |  282KB  |  8,441 lines

  1. char *cktelv = "Telnet support, 8.0.240, 17 Nov 2001";
  2. #define CKCTEL_C
  3.  
  4. int sstelnet = 0;                       /* Do server-side Telnet negotiation */
  5.  
  6. /*  C K C T E L  --  Telnet support  */
  7.  
  8. /*
  9.   Authors:
  10.     Telnet protocol by Frank da Cruz and Jeffrey Altman.
  11.     Telnet Forward X by Jeffrey Altman
  12.     Telnet START_TLS support by Jeffrey Altman
  13.     Telnet AUTH and ENCRYPT support by Jeffrey Altman
  14.     Telnet COMPORT support by Jeffrey Altman
  15.     Telnet NEW-ENVIRONMENT support by Jeffrey Altman
  16.     Telnet NAWS support by Frank da Cruz and Jeffrey Altman
  17.     Telnet TERMTYPE support by Jeffrey Altman
  18.     Telnet KERMIT support by Jeffrey Altman
  19.     Other contributions as indicated in the code.
  20.  
  21.   Copyright (C) 1985, 2001,
  22.     Trustees of Columbia University in the City of New York.
  23.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  24.     copyright text in the ckcmai.c module for disclaimer and permissions.
  25. */
  26.  
  27. /*
  28.   NOTE TO CONTRIBUTORS: This file, and all the other shared (ckc and cku)
  29.   C-Kermit source files, must be compatible with C preprocessors that support
  30.   only #ifdef, #else, #endif, #define, and #undef.  Please do not use #if,
  31.   logical operators, or other preprocessor features in this module.  Also,
  32.   don't use any ANSI C constructs except within #ifdef CK_ANSIC..#endif.
  33. */
  34.  
  35. #include "ckcsym.h"
  36. #include "ckcdeb.h"
  37.  
  38. #ifdef TNCODE
  39. #include "ckcker.h"
  40. #define TELCMDS                         /* to define name array */
  41. #define TELOPTS                         /* to define name array */
  42. #define SLC_NAMES                       /* to define name array */
  43. #define ENCRYPT_NAMES
  44. #define AUTH_NAMES
  45. #define TELOPT_STATES
  46. #define TELOPT_MODES
  47. #define TNC_NAMES
  48. #include "ckcnet.h"
  49. #include "ckctel.h"
  50. #ifdef CK_SSL
  51. #include "ck_ssl.h"
  52. #endif /* CK_SSL */
  53.  
  54. #ifndef NOTERM
  55. #ifdef OS2                              /* For terminal type name string */
  56. #include "ckuusr.h"
  57. #ifndef NT
  58. #include <os2.h>
  59. #undef COMMENT
  60. #endif /* NT */
  61. #include "ckocon.h"
  62. extern int tt_type, max_tt;
  63. extern struct tt_info_rec tt_info[];
  64. extern char ttname[];
  65. #endif /* OS2 */
  66. #endif /* NOTERM */
  67.  
  68. #ifdef OS2
  69. #include <assert.h>
  70. #ifdef NT
  71. #include <setjmpex.h>
  72. #else /* NT */
  73. #include <setjmp.h>
  74. #endif /* NT */
  75. #include <signal.h>
  76. #include "ckcsig.h"
  77. #endif /* OS2 */
  78.  
  79. #ifdef CK_NAWS                          /* Negotiate About Window Size */
  80. #ifdef RLOGCODE
  81. _PROTOTYP( int rlog_naws, (void) );
  82. #endif /* RLOGCODE */
  83. #endif /* CK_NAWS */
  84.  
  85. int tn_init = 0;                        /* Telnet protocol initialized flag */
  86. int tn_begun = 0;                       /* Telnet protocol started flag */
  87. static int tn_first = 1;                /* First time init flag */
  88. extern int tn_exit;                     /* Exit on disconnect */
  89. extern int inserver;                    /* Running as IKSD */
  90. char *tn_term = NULL;                   /* Terminal type override */
  91.  
  92. #ifdef CK_SNDLOC
  93. char *tn_loc = NULL;                    /* Location override */
  94. #endif /* CK_SNDLOC */
  95. int tn_nlm = TNL_CRLF;                  /* Telnet CR -> CR LF mode */
  96. int tn_b_nlm = TNL_CR;                  /* Telnet Binary CR RAW mode */
  97. int tn_b_meu = 0;                       /* Telnet Binary ME means U too */
  98. int tn_b_ume = 0;                       /* Telnet Binary U means ME too */
  99. int tn_wait_flg = 1;                    /* Telnet Wait for Negotiations */
  100. int tn_infinite = 0;                    /* Telnet Bug Infinite-Loop-Check */
  101. int tn_rem_echo = 1;                    /* We will echo if WILL ECHO */
  102. int tn_b_xfer = 0;                      /* Telnet Binary for Xfers? */
  103. int tn_sb_bug = 1;                      /* Telnet BUG - SB w/o WILL or DO */
  104. int tn_no_encrypt_xfer = 0;             /* Turn off Telnet Encrypt? */
  105. int tn_delay_sb = 1;                    /* Delay SBs until safe */
  106. int tn_auth_how = TN_AUTH_HOW_ANY;
  107. int tn_auth_enc = TN_AUTH_ENC_ANY;
  108. int tn_deb = 0;                         /* Telnet Debug mode */
  109. int tn_sfu = 0;                /* Microsoft SFU compatibility */
  110. #ifdef CK_FORWARD_X
  111. char * tn_fwdx_xauthority = NULL;       /* Xauthority File */
  112. #endif /* CK_FORWARD_X */
  113.  
  114. #ifdef OS2
  115. int ttnum = -1;                         /* Last Telnet Terminal Type sent */
  116. int ttnumend = 0;                       /* Has end of list been found */
  117. #endif /* OS2 */
  118.  
  119. char tn_msg[TN_MSG_LEN];                /* Telnet data can be rather long */
  120. char hexbuf[TN_MSG_LEN];
  121.  
  122. /*
  123.   In order to prevent an infinite telnet negotiation loop we maintain a
  124.   count of the number of times the same telnet negotiation message is
  125.   sent. When this count hits MAXTNCNT, we do not send any more of the
  126.   message. The count is stored in the tncnts[][] array.
  127.  
  128.   The tncnts[][] array is indexed by negotiation option (SUPPRESS GO AHEAD,
  129.   TERMINAL TYPE, NAWS, etc. - see the tnopts[] array) and the four
  130.   negotiation message types (WILL, WONT, DO, DONT).  All telnet negotiations
  131.   are kept track of in this way.
  132.  
  133.   The count for a message is zeroed when the "opposite" message is sent.
  134.   WILL is the opposite of WONT, and DO is the opposite of DONT.
  135.   For example sending "WILL SGA" increments tncnts[TELOPT_SGA][0]
  136.   and zeroes tncnts[TELOPT_SGA][1].
  137.  
  138.   The code that does this is in tn_sopt().
  139.  
  140.   rogersh@fsj.co.jp, 18/3/1995
  141.  
  142.   8/16/1998 - with the recent rewrite of the telnet state machine I don't
  143.   think this code is necessary anymore.  However, it can't do any harm so
  144.   I am leaving it in.    - Jeff
  145.  
  146.   12/28/1998 - all references to tncnts[] must be done with TELOPT_INDEX(opt)
  147.   because the Telnet option list is no longer contiguous.  We also must
  148.   allocate NTELOPTS + 1 because the TELOPT_INDEX() macro returns NTELOPTS
  149.   for an invalid option number.
  150. */
  151.  
  152. #define MAXTNCNT 4      /* Permits 4 intermediate telnet firewalls/gateways */
  153.  
  154. char tncnts[NTELOPTS+1][4];             /* Counts */
  155. char tnopps[4] = { 1,0,3,2 };           /* Opposites */
  156.  
  157. #ifdef CK_ENVIRONMENT
  158. #ifdef CK_FORWARD_X
  159. #define TSBUFSIZ 2056
  160. #else /* CK_FORWARD_X */
  161. #define TSBUFSIZ 1024
  162. #endif /* CK_FORWARD_X */
  163. char tn_env_acct[64];
  164. char tn_env_disp[64];
  165. char tn_env_job[64];
  166. char tn_env_prnt[64];
  167. char tn_env_sys[64];
  168. char * tn_env_uservar[8][2];
  169. int tn_env_flg = 1;
  170. #else /* CK_ENVIRONMENT */
  171. #define TSBUFSIZ 41
  172. int tn_env_flg = 0;
  173. #endif /* CK_ENVIRONMENT */
  174.  
  175. #ifndef NOSIGWINCH
  176. #ifdef CK_NAWS                          /* Window size business */
  177. #ifdef UNIX
  178. #include <signal.h>
  179. #endif /* UNIX */
  180. #endif /* CK_NAWS */
  181. #endif /* NOSIGWINCH */
  182.  
  183. CHAR sb[TSBUFSIZ];            /* Buffer for subnegotiations */
  184.  
  185. int tn_duplex = 1;                      /* Local echo */
  186.  
  187. extern char uidbuf[];                   /* User ID buffer */
  188. extern int quiet, ttnet, ttnproto, debses, what, duplex, oldplex, local;
  189. extern int seslog, sessft, whyclosed;
  190. #ifdef OS2
  191. #ifndef NOTERM
  192. extern int tt_rows[], tt_cols[];
  193. extern int tt_status[VNUM];
  194. extern int scrninitialized[];
  195. #endif /* NOTERM */
  196. #else /* OS2 */
  197. extern int tt_rows, tt_cols;            /* Everybody has this */
  198. #endif /* OS2 */
  199. extern int cmd_cols, cmd_rows;
  200. extern char namecopy[];
  201. extern char myipaddr[];             /* Global copy of my IP address */
  202.  
  203. int sw_armed = 0;                       /* SIGWINCH armed flag */
  204.  
  205. #ifndef NOSIGWINCH
  206. #ifdef CK_NAWS                          /* Window size business */
  207. #ifdef SIGWINCH
  208. #ifdef UNIX
  209.  
  210. SIGTYP
  211. winchh(foo) int foo; {
  212.     int x = 0;
  213. #ifdef CK_TTYFD
  214. #ifndef VMS
  215.     extern int ttyfd;
  216. #endif /* VMS */
  217. #endif /* CK_TTYFD */
  218.     debug(F100,"SIGWINCH caught","",0);
  219.     signal(SIGWINCH,winchh);            /* Re-arm the signal */
  220. #ifdef CK_TTYFD
  221.     if
  222. #ifdef VMS
  223.       (vmsttyfd() == -1)
  224. #else
  225.       (ttyfd == -1)
  226. #endif /* VMS */
  227. #else
  228.       (!local)
  229. #endif /* CK_TTYFD */
  230.     return;
  231.  
  232.     x = ttgwsiz();                      /* Get new window size */
  233. /*
  234.   This should be OK.  It might seem that sending this from
  235.   interrupt level could interfere with another TELNET IAC string
  236.   that was in the process of being sent.  But we always send
  237.   TELNET strings with a single write(), which should prevent mixups.
  238. */
  239.     if (x > 0 && tt_rows > 0 && tt_cols > 0) {
  240.         tn_snaws();
  241. #ifdef RLOGCODE
  242.         rlog_naws();
  243. #endif /* RLOGCODE */
  244.     }
  245.     return;
  246. }
  247. #endif /* UNIX */
  248. #endif /* SIGWINCH */
  249. #endif /* CK_NAWS */
  250. #endif /* NOSIGWINCH */
  251.  
  252. #ifndef TELOPT_MACRO
  253. int
  254. telopt_index(opt) int opt; {
  255.     if (opt >= 0 && opt <= TELOPT_STDERR)
  256.       return(opt);
  257.     else if (opt >= TELOPT_PRAGMA_LOGON && opt <= TELOPT_PRAGMA_HEARTBEAT)
  258.       return(opt-88);
  259.     else if (opt == TELOPT_IBM_SAK)
  260.       return(opt-147);
  261.     else
  262.       return(NTELOPTS);
  263. }
  264.  
  265. int
  266. telopt_ok(opt) int opt; {
  267.     return((opt >= TELOPT_BINARY && opt <= TELOPT_STDERR) ||
  268.            (opt >= TELOPT_PRAGMA_LOGON && opt <= TELOPT_PRAGMA_HEARTBEAT) ||
  269.            (opt == TELOPT_IBM_SAK));
  270. }
  271.  
  272. CHAR *
  273. telopt(opt) int opt; {
  274.     if (telopt_ok(opt))
  275.       return((CHAR *)telopts[telopt_index(opt)]);
  276.     else
  277.       return((CHAR *)"UNKNOWN");
  278. }
  279.  
  280. int
  281. telopt_mode_ok(opt) int opt; {
  282.     return((unsigned int)(opt) <= TN_NG_MU);
  283. }
  284.  
  285. CHAR *
  286. telopt_mode(opt) int opt; {
  287.     if (telopt_mode_ok(opt))
  288.       return((CHAR *)telopt_modes[opt-TN_NG_RF]);
  289.     else
  290.       return((CHAR *)"UNKNOWN");
  291. }
  292. #endif /* TELOPT_MACRO */
  293.  
  294. static int
  295. tn_outst(notquiet) int notquiet; {
  296.     int outstanding = 0;
  297.     int x = 0;
  298. #ifdef CK_ENCRYPTION
  299.     int e = 0;
  300.     int d = 0;
  301. #endif /* CK_ENCRYPTION */
  302.  
  303.     if (tn_wait_flg) {
  304.         for (x = TELOPT_FIRST; x <= TELOPT_LAST; x++) {
  305.             if (TELOPT_OK(x)) {
  306.                 if (TELOPT_UNANSWERED_WILL(x)) {
  307.                     if ( notquiet )
  308.                       printf("?Telnet waiting for response to WILL %s\r\n",
  309.                              TELOPT(x));
  310.                     debug(F111,"tn_outst","unanswered WILL",x);
  311.                     outstanding = 1;
  312.                     if ( !notquiet )
  313.                       break;
  314.                 }
  315.                 if (TELOPT_UNANSWERED_DO(x)) {
  316.                     if ( notquiet )
  317.                       printf("?Telnet waiting for response to DO %s\r\n",
  318.                              TELOPT(x));
  319.                     debug(F111,"tn_outst","unanswered DO",x);
  320.                     outstanding = 1;
  321.                     if ( !notquiet )
  322.                       break;
  323.                 }
  324.                 if (TELOPT_UNANSWERED_WONT(x)) {
  325.                     if ( notquiet )
  326.                       printf("?Telnet waiting for response to WONT %s\r\n",
  327.                              TELOPT(x));
  328.                     debug(F111,"tn_outst","unanswered WONT",x);
  329.                     outstanding = 1;
  330.                     if ( !notquiet )
  331.                       break;
  332.                 }
  333.                 if (TELOPT_UNANSWERED_DONT(x)) {
  334.                     if ( notquiet )
  335.                       printf("?Telnet waiting for response to DONT %s\r\n",
  336.                              TELOPT(x));
  337.                     debug(F111,"tn_outst","unanswered DONT",x);
  338.                     outstanding = 1;
  339.                     if ( !notquiet )
  340.                       break;
  341.                 }
  342.                 if (TELOPT_UNANSWERED_SB(x)) {
  343.                     if ( notquiet )
  344.                       printf("?Telnet waiting for response to SB %s\r\n",
  345.                              TELOPT(x));
  346.                     debug(F111,"tn_outst","unanswered SB",x);
  347.                     outstanding = 1;
  348.                     if ( !notquiet )
  349.                       break;
  350.                 }
  351.             }
  352.         }
  353. #ifdef CK_AUTHENTICATION
  354.     if (ck_tn_auth_in_progress()) {
  355.         if (TELOPT_ME(TELOPT_AUTHENTICATION)) {
  356.         if (notquiet)
  357.           printf("?Telnet waiting for WILL %s subnegotiation\r\n",
  358.              TELOPT(TELOPT_AUTHENTICATION));
  359.         debug(F111,
  360.               "tn_outst",
  361.               "ME authentication in progress",
  362.               TELOPT_AUTHENTICATION
  363.               );
  364.         outstanding = 1;
  365.         } else if (TELOPT_U(TELOPT_AUTHENTICATION)) {
  366.         if (notquiet)
  367.           printf("?Telnet waiting for DO %s subnegotiation\r\n",
  368.              TELOPT(TELOPT_AUTHENTICATION));
  369.         debug(F111,
  370.               "tn_outst",
  371.               "U authentication in progress",
  372.               TELOPT_AUTHENTICATION
  373.               );
  374.         outstanding = 1;
  375.         }
  376.     }
  377. #endif /* CK_AUTHENTICATION */
  378. #ifdef CK_ENCRYPTION
  379.         if (!outstanding) {
  380.             e = ck_tn_encrypting();
  381.             d = ck_tn_decrypting();
  382.             if (TELOPT_ME(TELOPT_ENCRYPTION)) {
  383.                 if (TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop && e ||
  384.                     !TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop && !e
  385.                     ) {
  386.                     if ( notquiet )
  387.                       printf("?Telnet waiting for WILL %s subnegotiation\r\n",
  388.                              TELOPT(TELOPT_ENCRYPTION));
  389.                     debug(F111,
  390.                           "tn_outst",
  391.                           "encryption mode switch",
  392.                           TELOPT_ENCRYPTION
  393.                           );
  394.                     outstanding = 1;
  395.                 }
  396.             }
  397.             if (TELOPT_U(TELOPT_ENCRYPTION)) {
  398.                 if (TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop && d ||
  399.                     !TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop && !d
  400.                     ) {
  401.                     if ( notquiet )
  402.                       printf("?Telnet waiting for DO %s subnegotiation\r\n",
  403.                              TELOPT(TELOPT_ENCRYPTION));
  404.                     debug(F111,
  405.                           "tn_outst",
  406.                           "decryption mode switch",
  407.                            TELOPT_ENCRYPTION
  408.                           );
  409.                     outstanding = 1;
  410.                 }
  411.             }
  412.         }
  413. #endif /* CK_ENCRYPTION */
  414.     } /* if (tn_wait_flg) */
  415.  
  416. #ifdef IKS_OPTION
  417.     /* Even if we are not waiting for Telnet options we must wait for */
  418.     /* Kermit Telnet Subnegotiations if we have sent a request to the */
  419.     /* other guy.  Otherwise we will get out of sync.                 */
  420.     if (!outstanding) {
  421.         if (TELOPT_U(TELOPT_KERMIT) &&
  422.             (TELOPT_SB(TELOPT_KERMIT).kermit.me_req_start ||
  423.              TELOPT_SB(TELOPT_KERMIT).kermit.me_req_stop ||
  424.              !TELOPT_SB(TELOPT_KERMIT).kermit.sop)
  425.             ) {
  426.             if ( notquiet )
  427.               printf("?Telnet waiting for SB %s negotiation\r\n",
  428.                      TELOPT(TELOPT_KERMIT));
  429.             debug(F111,"tn_outst","U kermit in progress",TELOPT_KERMIT);
  430.             outstanding = 1;
  431.         }
  432.     }
  433. #endif /* IKS_OPTION */
  434.  
  435. #ifdef TN_COMPORT
  436.     if (!outstanding) {
  437.         if (TELOPT_ME(TELOPT_COMPORT) &&
  438.              TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb) {
  439.             if (notquiet)
  440.               printf("?Telnet waiting for SB %s negotiation\r\n",
  441.                      TELOPT(TELOPT_COMPORT));
  442.             debug(F111,"tn_outst","ComPort SB in progress",TELOPT_COMPORT);
  443.             outstanding = 1;
  444.         }
  445.     }
  446. #endif /* TN_COMPORT */
  447.     return(outstanding);
  448. }
  449.  
  450. int
  451. istncomport() {
  452. #ifdef TN_COMPORT
  453.     if (!local) return(0);
  454.     if (ttnet != NET_TCPB) return(0);
  455.     if (ttnproto != NP_TELNET) return(0);
  456.     if (TELOPT_ME(TELOPT_COMPORT))
  457.       return(1);
  458.     else
  459. #endif /* TN_COMPORT */
  460.       return(0);
  461. }
  462.  
  463. /* tn_wait() -- Wait for response to Telnet negotiation. */
  464. /*
  465.   Wait for up to <timeout> seconds for the response to arrive.
  466.   Place all non-telnet data into Telnet Wait Buffer.
  467.   If response does arrive return 1, else return 0.
  468. */
  469. #ifndef TN_WAIT_BUF_SZ
  470. #define TN_WAIT_BUF_SZ 4096
  471. #endif /* TN_WAIT_BUF_SZ */
  472. static char tn_wait_buf[TN_WAIT_BUF_SZ];
  473. static int  tn_wait_idx = 0;
  474. #ifndef TN_TIMEOUT
  475. #define TN_TIMEOUT 120
  476. #endif /* TN_TIMEOUT */
  477. static int tn_wait_tmo = TN_TIMEOUT;
  478.  
  479. #ifdef CKSPINNER
  480. VOID
  481. prtwait(state) int state; {
  482.     switch (state % 4) {
  483.       case 0:
  484.         printf("/");
  485.         break;
  486.       case 1:
  487.         printf("-");
  488.         break;
  489.       case 2:
  490.         printf("\\");
  491.         break;
  492.       case 3:
  493.         printf("|");
  494.         break;
  495.     }
  496. }
  497. #endif /* CKSPINNER */
  498.  
  499. static int nflag = 0;
  500.  
  501. int
  502. #ifdef CK_ANSIC
  503. tn_wait(char * where)
  504. #else
  505. tn_wait(where) char * where;
  506. #endif /* CK_ANSIC */
  507. /* tn_wait */ {
  508.     extern int ckxech, local;
  509.     int ch = 0, count = 0;
  510. #ifndef NOHINTS
  511.     int nohintgiven = 1;
  512.     extern int hints;
  513. #endif /* NOHINTS */
  514.     int outstanding;
  515.  
  516.     rtimer();
  517.  
  518.     debug(F110,"tn_wait waiting for",where,0);
  519.     tn_wait_tmo = TN_TIMEOUT;
  520.     debug(F111,"tn_wait","timeout",tn_wait_tmo);
  521.     outstanding = tn_outst(0);
  522.  
  523.     /* The following is meant to be !(||).  We only want to return */
  524.     /* immediately if both the tn_wait_flg && tn_outst() are false */
  525.     if (!(outstanding || tn_wait_flg))  /* If no need to wait */
  526.       return(1);                        /* Don't. */
  527.  
  528.     if (tn_deb || debses) tn_debug("<wait for outstanding negotiations>");
  529.  
  530. #ifdef CKSPINNER
  531.     if (!sstelnet && !quiet)
  532.         prtwait(0);
  533. #endif /* CKSPINNER */
  534.  
  535.     /* Wait up to TN_TIMEOUT sec for responses to outstanding telnet negs */
  536.     do {
  537. #ifdef NTSIG
  538.         ck_ih();
  539. #endif /* NTSIG */
  540.         ch = ttinc(1);
  541.         if (ch == -1) {                 /* Timed out */
  542.             if (!sstelnet && !quiet) {  /* Let user know... */
  543. #ifdef CKSPINNER
  544.                 printf("\b");
  545.                 prtwait(gtimer());
  546. #else
  547.                 if (nflag == 0) {
  548.                     printf(" Negotiations.");
  549.                     nflag++;
  550.                 }
  551.                 if (nflag > 0) {
  552.                     printf(".");
  553.                     nflag++;
  554.                     fflush(stdout);
  555.                 }
  556. #endif /* CKSPINNER */
  557.             }
  558.         } else if (ch < -1) {
  559.             printf("\r\n?Connection closed by peer.\r\n");
  560.             if (tn_deb || debses) tn_debug("<connection closed by peer>");
  561.             return(-1);
  562.         } else
  563.         switch (ch) {
  564.           case IAC:
  565. #ifdef CKSPINNER
  566.             if (!sstelnet && !quiet)
  567.               printf("\b");
  568. #endif /* CKSPINNER */
  569.             ch = tn_doop((CHAR)(ch & 0xff),inserver?ckxech:duplex,ttinc);
  570. #ifdef CKSPINNER
  571.             if (!sstelnet && !quiet) {
  572.                 prtwait(gtimer());
  573.             }
  574. #endif /* CKSPINNER */
  575.             debug(F101,"tn_wait tn_doop","",ch);
  576.             switch (ch) {
  577.               case 1:
  578.                 duplex = 1;             /* Turn on echoing */
  579.                 if (inserver)
  580.                   ckxech = 1;
  581.                 break;
  582.               case 2:
  583.                 duplex = 0;             /* Turn off echoing */
  584.                 if (inserver)
  585.                   ckxech = 0;
  586.                 break;
  587.               case 3:
  588.                 tn_wait_buf[tn_wait_idx++] = IAC;
  589.                 break;
  590.               case 4:                   /* IKS event */
  591.               case 6:                   /* Logout */
  592.                 break;
  593.               case -1:
  594.                 printf("?Telnet Option negotiation error.\n");
  595.                 if (tn_deb || debses)
  596.                   tn_debug("<Telnet Option negotiation error>");
  597.                 return(-1);
  598.               case -2:
  599.                 printf("?Connection closed by peer.\n");
  600.                 if (tn_deb || debses) tn_debug("<Connection closed by peer>");
  601.                 return(-2);
  602.               default:
  603.                 if (ch < 0) {
  604.                   if (tn_deb || debses) tn_debug("<Unknown connection error>");
  605.                   return(ch);
  606.                 }
  607.             } /* switch */
  608.             break;
  609.           default:
  610. #ifdef CKSPINNER
  611.             if (!sstelnet && !quiet) {
  612.                 printf("\b");
  613.                 prtwait(gtimer());
  614.             }
  615. #endif /* CKSPINNER */
  616.             tn_wait_buf[tn_wait_idx++] = (CHAR)(ch & 0xff);
  617.         } /* switch */
  618.  
  619.         outstanding = tn_outst(0);
  620.  
  621.         if ( outstanding && ch != IAC ) {
  622.             int timer = gtimer();
  623.             if ( timer > tn_wait_tmo ) {
  624.                 if (!sstelnet) {
  625.                     printf(
  626.                     "\r\n?Telnet Protocol Timeout - connection closed\r\n");
  627.                     if (tn_deb || debses)
  628.                         tn_debug(
  629.                         "<telnet protocol timeout - connection closed>");
  630.                     tn_outst(1);
  631.                 }
  632.                 /* if we do not close the connection, then we will block */
  633.                 /* the next time we hit a wait.  and if we don't we will */
  634.                 /* do the wrong thing if the host sends 0xFF and does    */
  635.                 /* not intend it to be an IAC.                           */
  636.                 ttclos(0);
  637.                 whyclosed = WC_TELOPT;
  638.                 return(-1);
  639.             }
  640. #ifndef NOHINTS
  641.             else if ( hints && timer > 30 && nohintgiven && !inserver ) {
  642. #ifdef CKSPINNER
  643.                 printf("\b");
  644. #else /* CKSPINNER */
  645.                 printf("\r\n");
  646. #endif /* CKSPINNER */
  647.       printf("*************************\r\n");
  648.         printf("The Telnet %s is not sending required responses.\r\n\r\n",
  649.                 sstelnet?"client":"server");
  650.       tn_outst(1);
  651.       printf("\nYou can continue to wait or you can cancel with Ctrl-C.\r\n");
  652.       printf("In case the Telnet server never responds as required,\r\n");
  653.       printf("you can try connecting to this host with TELNET /NOWAIT.\r\n");
  654.       printf("Use SET HINTS OFF to suppress further hints.\r\n");
  655.       printf("*************************\r\n");
  656.       nohintgiven = 0;
  657.         }
  658. #endif /* NOHINTS */
  659.         }
  660.     } while ((tn_wait_idx < TN_WAIT_BUF_SZ) &&
  661.               (outstanding && (count = ttchk()) >= 0) /* (count not used) */
  662.               );
  663.  
  664.     if (tn_wait_idx == TN_WAIT_BUF_SZ) {
  665.       if (tn_deb || debses) tn_debug("<Telnet Wait Buffer filled>");
  666.       return(0);
  667.     }
  668.  
  669.     if (!sstelnet && !quiet) {
  670. #ifdef CKSPINNER
  671.         printf("\b \b");
  672. #else
  673.         if (nflag > 0) {
  674.             printf(" (OK)\n");
  675.             nflag = -1;
  676.         }
  677. #endif /* CKSPINNER */
  678.     }
  679.     if (tn_deb || debses) tn_debug("<no outstanding negotiations>");
  680.     return(0);
  681. }
  682.  
  683. /* Push data from the Telnet Wait Buffer into the I/O Queue */
  684. /* Return 1 on success                                      */
  685.  
  686. int
  687. tn_push() {
  688. #ifdef NETLEBUF
  689.     extern int tt_push_inited;
  690. #endif /* NETLEBUF */
  691.     if (tn_wait_idx) {
  692.         hexdump((CHAR *)"tn_push",tn_wait_buf,tn_wait_idx);
  693. #ifdef NETLEBUF
  694.         if (!tt_push_inited)            /* Local handling */
  695.           le_init();
  696.         le_puts((CHAR *)tn_wait_buf,tn_wait_idx);
  697. #else                                   /* External handling... */
  698. #ifdef OS2                              /* K95 has its own way */
  699.         le_puts((CHAR *)tn_wait_buf,tn_wait_idx);
  700. #else
  701. #ifdef TTLEBUF                          /* UNIX, etc */
  702.         le_puts((CHAR *)tn_wait_buf,tn_wait_idx);
  703. #else
  704. /*
  705.   If you see this message in AOS/VS, OS-9, VOS, etc, you need to copy
  706.   the #ifdef TTLEBUF..#endif code from ckutio.c to the corresponding
  707.   places in your ck?tio.c module.
  708. */
  709.         printf("tn_push called but not implemented - data lost.\n");
  710. #endif /* TTLEBUF */
  711. #endif /* OS2 */
  712. #endif /* NETLEBUF */
  713.         tn_wait_idx = 0;
  714.     }
  715.     tn_wait_tmo = TN_TIMEOUT;           /* Reset wait timer stats */
  716.     return(1);
  717. }
  718.  
  719. /*  T N _ S O P T  */
  720. /*
  721.    Sends a telnet option, avoids loops.
  722.    Returns 1 if command was sent, 0 if not, -1 on error.
  723. */
  724. int
  725. tn_sopt(cmd,opt) int cmd, opt; {        /* TELNET SEND OPTION */
  726.     CHAR buf[5];
  727.  
  728.     if (ttnet != NET_TCPB) return(-1);  /* Must be TCP/IP */
  729.     if (ttnproto != NP_TELNET) return(-1); /* Must be telnet protocol */
  730.     if (!TELCMD_OK(cmd)) return(-1);
  731.     if (TELOPT_OK(opt)) {
  732.         if (cmd == DO && TELOPT_UNANSWERED_DO(opt)) return(0);
  733.         if (cmd == WILL && TELOPT_UNANSWERED_WILL(opt)) return(0);
  734.         if (cmd == DONT && TELOPT_UNANSWERED_DONT(opt)) return(0);
  735.         if (cmd == WONT && TELOPT_UNANSWERED_WONT(opt)) return(0);
  736.     }
  737. #ifdef CK_SSL
  738.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  739.         return(0);
  740.     }
  741. #endif /* CK_SSL */
  742.  
  743.     if (cmd == DO && opt == TELOPT_AUTHENTICATION)
  744.       buf[0] = 0;
  745.  
  746.     if (tn_infinite && TELOPT_OK(opt)) { /* See comment above about   */
  747.         int index = TELOPT_INDEX(opt);   /* preventing infinite loops */
  748.         int m = cmd - WILL;
  749.  
  750.         if (tncnts[index][m] > MAXTNCNT) {
  751.             if (tn_deb || debses || deblog) {
  752.                 ckmakmsg(tn_msg,TN_MSG_LEN,
  753.                            "TELNET negotiation loop ",
  754.                            TELCMD(cmd), " ",
  755.                            TELOPT(opt));
  756.                 debug(F101,tn_msg,"",opt);
  757.                 if (tn_deb || debses) tn_debug(tn_msg);
  758.             }
  759.             return(0);
  760.         }
  761.         tncnts[index][m]++;
  762.         tncnts[index][tnopps[m]] = 0;
  763.     }
  764.     buf[0] = (CHAR) IAC;
  765.     buf[1] = (CHAR) (cmd & 0xff);
  766.     buf[2] = (CHAR) (opt & 0xff);
  767.     buf[3] = (CHAR) 0;
  768.     if ((tn_deb || debses || deblog) && cmd != SB) {
  769.         ckmakmsg(tn_msg,TN_MSG_LEN,"TELNET SENT ",TELCMD(cmd)," ",
  770.                   TELOPT(opt));
  771.         debug(F101,tn_msg,"",opt);
  772.     }
  773.  
  774.     if (ttol(buf,3) < 3) {
  775.         return(-1);
  776.     }
  777.  
  778.     /* Only display the command if it was actually sent. */
  779.     if ((tn_deb || debses) && cmd != SB) tn_debug(tn_msg);
  780.  
  781.     if (TELOPT_OK(opt)) {
  782.         if (cmd == DONT && TELOPT_UNANSWERED_DO(opt))
  783.           TELOPT_UNANSWERED_DO(opt) = 0;
  784.         if (cmd == WONT && TELOPT_UNANSWERED_WILL(opt))
  785.           TELOPT_UNANSWERED_WILL(opt) = 0;
  786.         if (cmd == DO && TELOPT_UNANSWERED_DONT(opt))
  787.           TELOPT_UNANSWERED_DONT(opt) = 0;
  788.         if (cmd == WILL && TELOPT_UNANSWERED_WONT(opt))
  789.           TELOPT_UNANSWERED_WONT(opt) = 0;
  790.     }
  791.     return(1);
  792. }
  793.  
  794. /* Send a telnet sub-option */
  795. /* Returns 1 if command was sent, 0 if not, -1 on error */
  796.  
  797. int
  798. tn_ssbopt(opt,sub,data,len) int opt, sub; CHAR * data; int len; {
  799.     CHAR buf[256];
  800.     int n,m;
  801.  
  802.     if (ttnet != NET_TCPB) return(0);   /* Must be TCP/IP */
  803.     if (ttnproto != NP_TELNET) return(0); /* Must be telnet protocol */
  804.     if (!TELOPT_OK(opt)) return(-1);
  805.     if (len < 0 || len > 250) {
  806.         debug(F111,"Unable to Send TELNET SB - data too long","len",len);
  807.         return(-1);                     /* Data too long */
  808.     }
  809. #ifdef CK_SSL
  810.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  811.         if (ttchk() < 0)
  812.           return(-1);
  813.         else
  814.           return(1);
  815.     }
  816. #endif /* CK_SSL */
  817.  
  818.     if (!data) len = 0;
  819.  
  820.     buf[0] = (CHAR) IAC;
  821.     buf[1] = (CHAR) (SB & 0xff);
  822.     buf[2] = (CHAR) (opt & 0xff);
  823.     buf[3] = (CHAR) (sub & 0xff);
  824.     if (data && len > 0) {
  825.         memcpy(&buf[4],data,len);
  826.     }
  827.     buf[4+len] = (CHAR) IAC;
  828.     buf[5+len] = (CHAR) SE;
  829.  
  830.     if (tn_deb || debses || deblog) {
  831.         if (opt == TELOPT_START_TLS && sub == 1)
  832.           ckmakmsg(tn_msg,TN_MSG_LEN,"TELNET SENT SB ",
  833.                     TELOPT(opt)," FOLLOWS IAC SE",NULL);
  834.         else if (opt == TELOPT_TTYPE && sub == 1)
  835.           ckmakmsg(tn_msg,TN_MSG_LEN,"TELNET SENT SB ", TELOPT(opt),
  836.                     " SEND IAC SE",NULL);
  837.         else if (opt == TELOPT_TTYPE && sub == 0)
  838.           ckmakxmsg(tn_msg,TN_MSG_LEN,"TELNET SENT SB ",TELOPT(opt)," IS ",
  839.             (char *)data," IAC SE",NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  840.         else if (opt == TELOPT_NEWENVIRON) {
  841.             int i, quote;
  842.             ckmakmsg(tn_msg,TN_MSG_LEN,"TELNET SENT SB ",
  843.                      TELOPT(TELOPT_NEWENVIRON)," ",
  844.                      sub == TELQUAL_SEND ? "SEND" :
  845.                      sub == TELQUAL_IS ? "IS" :
  846.                      sub == TELQUAL_INFO ?"INFO" : "UNKNOWN" );
  847.             for (i = 0, quote = 0; i < len; i++) {
  848.                 if (quote) {
  849.                     sprintf(hexbuf,"%02x",data[i]); /* safe but ugly */
  850.                     ckstrncat(tn_msg,hexbuf,TN_MSG_LEN);
  851.                     quote = 0;
  852.                 } else {
  853.                     switch (data[i]) {
  854.                       case TEL_ENV_USERVAR:
  855.                         ckstrncat(tn_msg," USERVAR ",TN_MSG_LEN);
  856.                         break;
  857.                       case TEL_ENV_VAR:
  858.                         ckstrncat(tn_msg," VAR ",TN_MSG_LEN);
  859.                         break;
  860.                       case TEL_ENV_VALUE:
  861.                         ckstrncat(tn_msg," VALUE ",TN_MSG_LEN);
  862.                         break;
  863.                       case TEL_ENV_ESC:
  864.                         ckstrncat(tn_msg," ESC ",TN_MSG_LEN);
  865.                         quote = 1;
  866.                         break;
  867.                       case IAC:
  868.                         ckstrncat(tn_msg," IAC ",TN_MSG_LEN);
  869.                         break;
  870.                       default:
  871.                         sprintf(hexbuf,"%c",data[i]); /* safe but ugly */
  872.                         ckstrncat(tn_msg,hexbuf,TN_MSG_LEN);
  873.                     }
  874.                 }
  875.             }
  876.             ckstrncat(tn_msg," IAC SE",TN_MSG_LEN);
  877.         } else {
  878.             sprintf(hexbuf,"%02x",sub);             /* safe but ugly */
  879.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  880.                       "TELNET SENT SB ",TELOPT(opt),
  881.               " ",
  882.               hexbuf,
  883.               " <data> IAC SE",
  884.                        NULL,NULL,NULL,NULL,NULL,NULL,NULL
  885.               );
  886.         }
  887.         debug(F101,tn_msg,"",opt);
  888.         if (tn_deb || debses) tn_debug(tn_msg);
  889.     }
  890.     if (ttol(buf,6+len) < 6+len)
  891.       return(-1);
  892.     return(1);
  893. }
  894.  
  895. /*
  896.   tn_flui() -- Processes all waiting data for Telnet commands.
  897.   All non-Telnet data is to be stored into the Telnet Wait Buffer.
  898.   Returns 1 on success.
  899. */
  900. int
  901. tn_flui() {
  902.     extern int ckxech;
  903.     int x = 0;
  904.  
  905.     /* Wait up to 5 sec for responses to outstanding telnet negotiations */
  906.     while (x >= 0 && ttchk() > 0  && tn_wait_idx < TN_WAIT_BUF_SZ) {
  907.         x = ttinc(1);
  908.         switch (x) {
  909.           case IAC:
  910.             x = tn_doop((CHAR)(x & 0xff),inserver?ckxech:duplex,ttinc);
  911.             debug(F101,"tn_flui tn_doop","",x);
  912.             switch (x) {
  913.               case 1:                   /* Turn on echoing */
  914.                 duplex = 1;
  915.                 if (inserver)
  916.                   ckxech = 1;
  917.                 break;
  918.               case 2:                   /* Turn off echoing */
  919.                 duplex = 0;
  920.                 if (inserver)
  921.                   ckxech = 0;
  922.                 break;
  923.               case 3:
  924.                 tn_wait_buf[tn_wait_idx++] = IAC;
  925.                 break;
  926.               case 4:                   /* IKS event */
  927.               case 6:                   /* Logout */
  928.                 break;
  929.             }
  930.             break;
  931.           default:
  932.             if (x >= 0)
  933.               tn_wait_buf[tn_wait_idx++] = x;
  934.         }
  935.     }
  936.     return(1);
  937. }
  938.  
  939. unsigned char *
  940. tn_get_display()
  941. {
  942.     char * disp = NULL;
  943.     static char tmploc[256];
  944.  
  945.     /* Must compute the DISPLAY string we are going to send to the host */
  946.     /* If one is not assigned, do not send a string unless the user has */
  947.     /* explicitedly requested we try to send one via X-Display Location */
  948.     /* But do not send a string at all if FORWARD_X is in use.          */
  949.  
  950.     debug(F110,"tn_get_display() myipaddr",myipaddr,0);
  951. #ifdef CK_ENVIRONMENT
  952.     debug(F110,"tn_get_display() tn_env_disp",tn_env_disp,0);
  953.     if (tn_env_disp[0]) {
  954.         int colon = ckindex(":",tn_env_disp,0,0,1);
  955.         if ( !colon ) {
  956.             ckmakmsg(tmploc,256,myipaddr,":",tn_env_disp,NULL);
  957.             disp = tmploc;
  958.         } else if ( ckindex("localhost:",tn_env_disp,0,0,0) ||
  959.                     ckindex("unix:",tn_env_disp,0,0,0) ||
  960.                     ckindex("127.0.0.1:",tn_env_disp,0,0,0) ||
  961.                     ckindex("0:",tn_env_disp,0,0,0) ||
  962.                     tn_env_disp[0] == ':' ) {
  963.             ckmakmsg(tmploc,256,myipaddr,":",&tn_env_disp[colon],NULL);
  964.             disp = tmploc;
  965.         } else
  966.             disp = tn_env_disp;
  967.     }
  968.     else
  969. #endif /* CK_ENVIRONMENT */
  970.         if (TELOPT_ME(TELOPT_XDISPLOC) ||
  971.               TELOPT_U(TELOPT_FORWARD_X)) {
  972.         ckmakmsg(tmploc,256,myipaddr,":0.0",NULL,NULL);
  973.         disp = tmploc;
  974.     }
  975.     debug(F110,"tn_get_display() returns",disp,0);
  976.     return((CHAR *)disp);
  977. }
  978.  
  979. #ifdef CK_FORWARD_X
  980. static Xauth fake_xauth = {0,0,NULL,0,NULL,0,NULL,0,NULL};
  981. static Xauth *real_xauth=NULL;
  982.  
  983. /*
  984.  * Author:  Jim Fulton, MIT X Consortium
  985.  *
  986.  * fwdx_parse_displayname -
  987.  * display a display string up into its component parts
  988.  */
  989. #ifdef UNIX
  990. #define UNIX_CONNECTION "unix"
  991. #define UNIX_CONNECTION_LENGTH 4
  992. #endif
  993.  
  994. /*
  995.  * private utility routines
  996.  */
  997.  
  998. static int
  999. #ifdef CK_ANSIC
  1000. XmuGetHostname (char *buf, int maxlen)
  1001. #else
  1002. XmuGetHostname (buf, maxlen)
  1003.     char *buf;
  1004.     int maxlen;
  1005. #endif /* CK_ANSIC */
  1006. {
  1007.     int len;
  1008.  
  1009. #ifdef NEED_UTSNAME
  1010.     /*
  1011.      * same host name crock as in server and xinit.
  1012.      */
  1013.     struct utsname name;
  1014.  
  1015.     uname (&name);
  1016.     len = strlen (name.nodename);
  1017.     if (len >= maxlen) len = maxlen - 1;
  1018.     strncpy (buf, name.nodename, len);
  1019.     buf[len] = '\0';
  1020. #else
  1021.     buf[0] = '\0';
  1022.     (void) gethostname (buf, maxlen);
  1023.     buf [maxlen - 1] = '\0';
  1024.     len = strlen(buf);
  1025. #endif /* hpux */
  1026.     return len;
  1027. }
  1028.  
  1029. static char *
  1030. #ifdef CK_ANSIC
  1031. copystring (char *src, int len)
  1032. #else
  1033. copystring (src, len)
  1034.     char *src;
  1035.     int len;
  1036. #endif /* CK_ANSIC */
  1037. {
  1038.     char *cp;
  1039.  
  1040.     if (!src && len != 0) return NULL;
  1041.     cp = malloc (len + 1);
  1042.     if (cp) {
  1043.     if (src) strncpy (cp, src, len);
  1044.     cp[len] = '\0';
  1045.     }
  1046.     return cp;
  1047. }
  1048.  
  1049. static char *
  1050. #ifdef CK_ANSIC
  1051. get_local_hostname (char *buf, int maxlen)
  1052. #else
  1053. get_local_hostname (buf, maxlen)
  1054.     char *buf;
  1055.     int maxlen;
  1056. #endif
  1057. {
  1058.     buf[0] = '\0';
  1059.     (void) XmuGetHostname (buf, maxlen);
  1060.     return (buf[0] ? buf : NULL);
  1061. }
  1062.  
  1063. #ifndef UNIX
  1064. static char *
  1065. copyhostname ()
  1066. {
  1067.     char buf[256];
  1068.  
  1069.     return (get_local_hostname (buf, sizeof buf) ?
  1070.         copystring (buf, strlen (buf)) : NULL);
  1071. }
  1072. #endif
  1073.  
  1074.  
  1075. int
  1076. #ifdef CK_ANSIC
  1077. fwdx_parse_displayname (char *displayname, int *familyp, char **hostp,
  1078.                         int *dpynump, int *scrnump, char **restp)
  1079. #else
  1080. fwdx_parse_displayname (displayname, familyp, hostp, dpynump, scrnump, restp)
  1081.     char *displayname;
  1082.     int *familyp;            /* return */
  1083.     char **hostp;            /* return */
  1084.     int *dpynump, *scrnump;        /* return */
  1085.     char **restp;            /* return */
  1086. #endif /* CK_ANSIC */
  1087. {
  1088.     char *ptr;                /* work variables */
  1089.     int len;                /* work variable */
  1090.     int family = -1;            /* value to be returned */
  1091.     char *host = NULL;            /* must free if set and error return */
  1092.     int dpynum = -1;            /* value to be returned */
  1093.     int scrnum = 0;            /* value to be returned */
  1094.     char *rest = NULL;            /* must free if set and error return */
  1095.     int dnet = 0;            /* if 1 then using DECnet */
  1096.  
  1097.                     /* check the name */
  1098.     if (!displayname || !displayname[0])
  1099.         return 0;
  1100.                     /* must have at least :number */
  1101.     ptr = (char *)strchr(displayname, ':');
  1102.     if (!ptr || !ptr[1]) return 0;
  1103.     if (ptr[1] == ':') {
  1104.     if (ptr[2] == '\0') return 0;
  1105.     dnet = 1;
  1106.     }
  1107.  
  1108.     /*
  1109.      * get the host string; if none is given, use the most effiecient path
  1110.      */
  1111.  
  1112.     len = (ptr - displayname);    /* length of host name */
  1113.     if (len == 0) {            /* choose most efficient path */
  1114. #ifdef UNIX
  1115.     host = copystring (UNIX_CONNECTION, UNIX_CONNECTION_LENGTH);
  1116.     family = FamilyLocal;
  1117. #else
  1118.     if (dnet) {
  1119.         host = copystring ("0", 1);
  1120.         family = FamilyDECnet;
  1121.     } else {
  1122.         host = copyhostname ();
  1123.         family = FamilyInternet;
  1124.     }
  1125. #endif
  1126.     } else {
  1127.     host = copystring (displayname, len);
  1128.     if (dnet) {
  1129.         family = dnet;
  1130.     } else {
  1131. #ifdef UNIX
  1132.         if (host && strcmp (host, UNIX_CONNECTION) == 0)
  1133.           family = FamilyLocal;
  1134.         else
  1135. #endif
  1136.           family = FamilyInternet;
  1137.     }
  1138.     }
  1139.  
  1140.     if (!host) return 0;
  1141.  
  1142.  
  1143.     /*
  1144.      * get the display number; we know that there is something after the
  1145.      * colon (or colons) from above.  note that host is now set and must
  1146.      * be freed if there is an error.
  1147.      */
  1148.  
  1149.     if (dnet) ptr++;            /* skip the extra DECnet colon */
  1150.     ptr++;                /* move to start of display num */
  1151.     {
  1152.     register char *cp;
  1153.  
  1154.     for (cp = ptr; *cp && isascii(*cp) && isdigit(*cp); cp++) ;
  1155.     len = (cp - ptr);
  1156.                     /* check present and valid follow */
  1157.     if (len == 0 || (*cp && *cp != '.')) {
  1158.         free (host);
  1159.         return 0;
  1160.     }
  1161.  
  1162.     dpynum = atoi (ptr);        /* it will handle num. as well */
  1163.     ptr = cp;
  1164.     }
  1165.  
  1166.     /*
  1167.      * now get screen number if given; ptr may point to nul at this point
  1168.      */
  1169.     if (ptr[0] == '.') {
  1170.     register char *cp;
  1171.  
  1172.     ptr++;
  1173.     for (cp = ptr; *cp && isascii(*cp) && isdigit(*cp); cp++) ;
  1174.     len = (cp - ptr);
  1175.     if (len == 0 || (*cp && *cp != '.')) {    /* all prop name */
  1176.         free (host);
  1177.         return 0;
  1178.     }
  1179.  
  1180.     scrnum = atoi (ptr);        /* it will handle num. as well */
  1181.     ptr = cp;
  1182.     }
  1183.  
  1184.     /*
  1185.      * and finally, get any additional stuff that might be following the
  1186.      * the screen number; ptr must point to a period if there is anything
  1187.      */
  1188.  
  1189.     if (ptr[0] == '.') {
  1190.     ptr++;
  1191.     len = strlen (ptr);
  1192.     if (len > 0) {
  1193.         rest = copystring (ptr, len);
  1194.         if (!rest) {
  1195.         free (host);
  1196.         return 1;
  1197.         }
  1198.     }
  1199.     }
  1200.  
  1201.     /*
  1202.      * and we are done!
  1203.      */
  1204.  
  1205.     if ( familyp )
  1206.         *familyp = family;
  1207.     if ( hostp )
  1208.         *hostp = host;
  1209.     else
  1210.         free(host);
  1211.     if ( dpynump )
  1212.         *dpynump = dpynum;
  1213.     if ( scrnump )
  1214.         *scrnump = scrnum;
  1215.     if ( restp )
  1216.         *restp = rest;
  1217.     else
  1218.         free(rest);
  1219.     return 1;
  1220. }
  1221.  
  1222.  
  1223. int
  1224. #ifdef CK_ANSIC
  1225. fwdx_tn_sb( unsigned char * sb, int n )
  1226. #else
  1227. fwdx_tn_sb( sb, n ) unsigned char * sb; int n;
  1228. #endif /* CK_ANSIC */
  1229. {
  1230.     unsigned short hchannel, nchannel;
  1231.     unsigned char * p;
  1232.     int i;
  1233.     int rc = -1;
  1234.  
  1235.     /* check to ensure we have negotiated Forward X */
  1236.     if ( sstelnet && !TELOPT_ME(TELOPT_FORWARD_X) ||
  1237.          !sstelnet && !TELOPT_U(TELOPT_FORWARD_X) ) {
  1238.         debug(F100,"fwdx_tn_sb() not negotiated","",0);
  1239.         return(0);
  1240.     }
  1241.  
  1242. #ifdef CK_SSL
  1243.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1244.         return(0);
  1245.     }
  1246. #endif /* CK_SSL */
  1247.  
  1248.     switch (sb[0]) {
  1249.     case FWDX_SCREEN:
  1250.         if (sstelnet && n == 4)
  1251.             rc = fwdx_create_listen_socket(sb[1]);
  1252.         break;
  1253.     case FWDX_OPEN:
  1254.         if ( !sstelnet && n >= 5 ) {
  1255.             p = (unsigned char *) &nchannel;
  1256.             i = 1;
  1257.             p[0] = sb[i++];
  1258.             if ( p[0] == IAC )
  1259.                 i++;
  1260.             p[1] = sb[i++];
  1261.             if ( p[0] == IAC )
  1262.                 i++;
  1263.             hchannel = ntohs(nchannel);
  1264.             rc = fwdx_open_client_channel(hchannel);
  1265.             if ( rc < 0 ) {
  1266.                 /* Failed; Send CLOSE channel */
  1267.                 fwdx_send_close(hchannel);
  1268.                 rc = 0;         /* Do not consider this to be a telnet error */
  1269.             }
  1270. #ifdef NT
  1271.             if ( !TELOPT_SB(TELOPT_FORWARD_X).forward_x.thread_started ) {
  1272.                 ckThreadBegin( &fwdx_thread,32655, 0, FALSE, 0 ) ;
  1273.                 TELOPT_SB(TELOPT_FORWARD_X).forward_x.thread_started = 1;
  1274.             }
  1275. #endif /* NT */
  1276.         }
  1277.         break;
  1278.     case FWDX_CLOSE:
  1279.         p = (unsigned char *) &nchannel;
  1280.         i = 1;
  1281.         p[0] = sb[i++];
  1282.         if ( p[0] == IAC )
  1283.             i++;
  1284.         p[1] = sb[i++];
  1285.         if ( p[0] == IAC )
  1286.             i++;
  1287.         hchannel = ntohs(nchannel);
  1288.         fwdx_close_channel(hchannel);
  1289.         rc = 0; /* no errors when closing */
  1290.         break;
  1291.     case FWDX_DATA:
  1292.     p = (unsigned char *) &nchannel;
  1293.         i = 1;
  1294.         p[0] = sb[i++];
  1295.         if ( p[0] == IAC )
  1296.             i++;
  1297.         p[1] = sb[i++];
  1298.         if ( p[0] == IAC )
  1299.             i++;
  1300.  
  1301.         hchannel = ntohs(nchannel);
  1302.         rc = fwdx_send_xauth_to_xserver(hchannel,(char *)&sb[3],n-5);
  1303.         if ( rc >= 0 && n-5-rc > 0) {
  1304.             rc = fwdx_write_data_to_channel(hchannel,(char *)&sb[3+rc],n-5-rc);
  1305.             if ( rc < 0 ) {
  1306.                 /* Failed; Send CLOSE channel */
  1307.                 rc = fwdx_send_close(hchannel);
  1308.             }
  1309.         }
  1310.         break;
  1311.     case FWDX_OPTIONS:
  1312.         if ( sstelnet ) {
  1313. #ifndef FWDX_SERVER
  1314.             rc = 0;
  1315. #else
  1316.             rc = fwdx_server_accept_options((char*)&sb[2],n-3);
  1317. #endif
  1318.         } else {
  1319.             rc = fwdx_client_reply_options((char *)&sb[2],n-3);
  1320.             if ( rc >= 0 ) {
  1321.                 rc = tn_sndfwdx();
  1322.             }
  1323.         }
  1324.         break;
  1325.     case FWDX_OPT_DATA:
  1326.         switch ( sb[1] ) {
  1327.         default:
  1328.             rc = 0;             /* we don't recognize, not an error */
  1329.         }
  1330.         break;
  1331.  
  1332.     case FWDX_XOFF:
  1333.     case FWDX_XON:
  1334.         if ( !sstelnet ) {
  1335.             p = (unsigned char *) &nchannel;
  1336.             i = 1;
  1337.             p[0] = sb[i++];
  1338.             if ( p[0] == IAC )
  1339.                 i++;
  1340.             p[1] = sb[i++];
  1341.             if ( p[0] == IAC )
  1342.                 i++;
  1343.  
  1344.             hchannel = ntohs(nchannel);
  1345.             TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[hchannel].suspend =
  1346.                 (sb[0] == FWDX_XOFF);
  1347.             rc = 0;
  1348.         }
  1349.         break;
  1350.     }
  1351.  
  1352.     return(rc < 0 ? -1 : 0);
  1353. }
  1354.  
  1355. int
  1356. #ifdef CK_ANSIC
  1357. fwdx_send_xauth_to_xserver(int channel, unsigned char * data, int len)
  1358. #else
  1359. fwdx_send_xauth_to_xserver(channel, data, len)
  1360.     int channel; unsigned char * data; int len;
  1361. #endif /* CK_ANSIC */
  1362. {
  1363.     int name_len, data_len, i;
  1364.  
  1365.     for (i = 0; i < MAXFWDX ; i++) {
  1366.         if (TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].id == channel)
  1367.             break;
  1368.     }
  1369.     if ( i == MAXFWDX )
  1370.         goto auth_err;
  1371.  
  1372.     if (!TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].need_to_send_xauth)
  1373.         return(0);
  1374.  
  1375.     if (len < 12)
  1376.         goto auth_err;
  1377.  
  1378.     /* Parse the lengths of variable-length fields. */
  1379.     if (data[0] == 0x42) {        /* byte order MSB first. */
  1380.         /* Xauth packets appear to always have this format */
  1381.         if ( data[1] != 0x00 ||
  1382.              data[2] != 0x00 ||
  1383.              data[3] != 0x0B ||
  1384.              data[4] != 0x00 ||
  1385.              data[5] != 0x00 )
  1386.             goto auth_err;
  1387.  
  1388.         name_len = (data[6] << 8) + data[7];
  1389.         data_len = (data[8] << 8) + data[9];
  1390.     } else if (data[0] == 0x6c) {    /* Byte order LSB first. */
  1391.         /* Xauth packets appear to always have this format */
  1392.         if ( data[1] != 0x00 ||
  1393.              data[2] != 0x0B ||
  1394.              data[3] != 0x00 ||
  1395.              data[4] != 0x00 ||
  1396.              data[5] != 0x00 )
  1397.             goto auth_err;
  1398.  
  1399.         name_len = data[6] + (data[7] << 8);
  1400.         data_len = data[8] + (data[9] << 8);
  1401.     } else {
  1402.         /* bad byte order byte */
  1403.         goto auth_err;
  1404.     }
  1405.  
  1406.     /* Check if the whole packet is in buffer. */
  1407.     if (len < 12 + ((name_len + 3) & ~3) + ((data_len + 3) & ~3))
  1408.         goto auth_err;
  1409.     /* If the Telnet Server allows a real Xauth message to be sent */
  1410.     /* Then let the message be processed by the Xserver.           */
  1411.     if (name_len + data_len > 0) {
  1412.        TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].need_to_send_xauth = 0;
  1413.        return(0);
  1414.     }
  1415.     else
  1416.     /* If an empty Xauth message was received.  We are going to   */
  1417.     /* send our own Xauth message using the real Xauth data.  And */
  1418.     /* then send any other data in the buffer.                    */
  1419.     {
  1420.         int c, err, dpynum, scrnum, family, sb_len;
  1421.         char *display, *host = NULL, *rest = NULL;
  1422.         unsigned char *sb, *p;
  1423.  
  1424.         /* parse the local DISPLAY env var */
  1425.         display = getenv("DISPLAY");
  1426.         if ( !display )
  1427.             display = "127.0.0.1:0.0";
  1428.  
  1429.         if (fwdx_parse_displayname(display,
  1430.                    &family, &host, &dpynum, &scrnum, &rest)) {
  1431.             char * disp_no = ckitoa(dpynum);    /* should be unsigned */
  1432.             if (family == FamilyLocal) {
  1433.                 /* call with address = "<local host name>" */
  1434.                 char address[300] = "localhost";
  1435.                 gethostname(address, sizeof(address) - 1);
  1436.                 real_xauth = XauGetAuthByAddr(family,
  1437.                           strlen(address),
  1438.                           address,
  1439.                           strlen(disp_no),
  1440.                           disp_no, 0, NULL);
  1441.             }
  1442.             else if (family == FamilyInternet) {
  1443.                 /* call with address = 4 bytes numeric ip addr (MSB) */
  1444.                 struct hostent *hi;
  1445.                 if (hi = gethostbyname(host))
  1446.                     real_xauth = XauGetAuthByAddr(family, 4,
  1447.                           hi->h_addr, strlen(disp_no),
  1448.                           disp_no, 0, NULL);
  1449.             }
  1450.         }
  1451.         if (host) free(host);
  1452.         if (rest) free(rest);
  1453.         if (!real_xauth) {
  1454.     TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].need_to_send_xauth = 0;
  1455.             return(0);
  1456.         }
  1457.  
  1458.         if (!strncmp(real_xauth->name,
  1459.              "MIT-MAGIC-COOKIE-1",
  1460.              real_xauth->name_length)) {
  1461.             char msg[64];
  1462.  
  1463.             name_len = real_xauth->name_length;
  1464.             data_len = 16;
  1465.  
  1466.             if ( data[0] == 0x42 ) {
  1467.                 msg[0] = 0x42; /* MSB order */
  1468.                 msg[1] = msg[2] = 0;
  1469.                 msg[3] = 0x0B;
  1470.                 msg[4] = msg[5] = 0;
  1471.                 msg[6] = (name_len >> 8);
  1472.                 msg[7] = (name_len & 0xFF);
  1473.                 msg[8] = (data_len >> 8);
  1474.                 msg[9] = (data_len & 0xFF);
  1475.             } else {
  1476.                 msg[0] = 0x6c; /* LSB order */
  1477.                 msg[1] = 0;
  1478.                 msg[2] = 0x0B;
  1479.                 msg[3] = msg[4] = msg[5] = 0;
  1480.                 msg[6] = (name_len & 0xFF);
  1481.                 msg[7] = (name_len >> 8);
  1482.                 msg[8] = (data_len & 0xFF);
  1483.                 msg[9] = (data_len >> 8);
  1484.             }
  1485.             msg[10] = msg[11] = 0;
  1486.             memcpy(&msg[12],real_xauth->name,18);
  1487.             msg[30] = msg[31] = 0;
  1488.             memcpy(&msg[32],real_xauth->data,16);
  1489.  
  1490.             if (fwdx_write_data_to_channel(channel,(char *)msg,48) < 0) {
  1491.   TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].need_to_send_xauth = 0;
  1492.                 return(-1);
  1493.             } else {
  1494.   TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].need_to_send_xauth = 0;
  1495.                 return(12);
  1496.             }
  1497.         } else {
  1498.   TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].need_to_send_xauth = 0;
  1499.             return(0);        /* we do not know how to handle this type yet */
  1500.         }
  1501.     }
  1502.  
  1503.   auth_err:
  1504.     return(-1);
  1505. }
  1506.  
  1507.  
  1508. #ifdef COMMENT
  1509. int
  1510. #ifdef CK_ANSIC
  1511. fwdx_authorize_channel(int channel, unsigned char * data, int len)
  1512. #else
  1513. fwdx_authorize_channel(channel, data, len)
  1514.     int channel; unsigned char * data; int len;
  1515. #endif /* CK_ANSIC */
  1516. {
  1517.     /* XXX maybe we should have some retry handling if not the whole first
  1518.     * authorization packet arrives complete
  1519.     */
  1520.     if ( !TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[channel].authorized ) {
  1521.         int name_len, data_len;
  1522.  
  1523.         if (len < 12)
  1524.             goto auth_err;
  1525.  
  1526.         /* Parse the lengths of variable-length fields. */
  1527.         if (data[0] == 0x42) {        /* byte order MSB first. */
  1528.             /* Xauth packets appear to always have this format */
  1529.             if ( data[1] != 0x00 ||
  1530.                  data[2] != 0x00 ||
  1531.                  data[3] != 0x0B ||
  1532.                  data[4] != 0x00 ||
  1533.                  data[5] != 0x00 )
  1534.                 goto auth_err;
  1535.  
  1536.             name_len = (data[6] << 8) + data[7];
  1537.             data_len = (data[8] << 8) + data[9];
  1538.         } else if (data[0] == 0x6c) {    /* Byte order LSB first. */
  1539.             /* Xauth packets appear to always have this format */
  1540.             if ( data[1] != 0x00 ||
  1541.                  data[2] != 0x0B ||
  1542.                  data[3] != 0x00 ||
  1543.                  data[4] != 0x00 ||
  1544.                  data[5] != 0x00 )
  1545.                 goto auth_err;
  1546.  
  1547.             name_len = data[6] + (data[7] << 8);
  1548.             data_len = data[8] + (data[9] << 8);
  1549.         } else {
  1550.             /* bad byte order byte */
  1551.             goto auth_err;
  1552.         }
  1553.         /* Check if authentication protocol matches. */
  1554.         if (name_len != fake_xauth.name_length ||
  1555.              memcmp(data + 12, fake_xauth.name, name_len) != 0) {
  1556.             /* connection uses different authentication protocol */
  1557.             goto auth_err;
  1558.         }
  1559.         /* Check if authentication data matches our fake data. */
  1560.         if (data_len != fake_xauth.data_length ||
  1561.              memcmp(data + 12 + ((name_len + 3) & ~3),
  1562.                      fake_xauth.data, fake_xauth.data_length) != 0) {
  1563.             /* auth data does not match fake data */
  1564.             goto auth_err;
  1565.         }
  1566.         /* substitute the fake data with real data if we have any */
  1567.         if (real_xauth && real_xauth->data)
  1568.             memcpy(data + 12 + ((name_len + 3) & ~3),
  1569.            real_xauth->data, data_len);
  1570.  
  1571.         TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[channel].authorized = 1;
  1572.     }
  1573.     return(0);
  1574.   auth_err:
  1575.     return(-1);
  1576. }
  1577. #endif /* COMMENT */
  1578.  
  1579. int
  1580. #ifdef CK_ANSIC
  1581. fwdx_send_close(int channel)
  1582. #else
  1583. fwdx_send_close(channel) int channel;
  1584. #endif /* CK_ANSIC */
  1585. {
  1586.     unsigned short nchannel;
  1587.     int i;
  1588.     CHAR * p;
  1589.  
  1590. #ifdef CK_SSL
  1591.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1592.         return(0);
  1593.     }
  1594. #endif /* CK_SSL */
  1595.  
  1596.     nchannel = htons(channel);
  1597.     p = (unsigned char *) &nchannel;
  1598.  
  1599.     i = 0;
  1600.     sb[i++] = (CHAR) IAC;               /* I Am a Command */
  1601.     sb[i++] = (CHAR) SB;                /* Subnegotiation */
  1602.     sb[i++] = TELOPT_FORWARD_X;         /* Forward X */
  1603.     sb[i++] = FWDX_CLOSE;               /* Open */
  1604.     sb[i++] = p[0];                     /* First Byte of Channel */
  1605.     if ( p[0] == IAC )
  1606.         sb[i++] = IAC;
  1607.     sb[i++] = p[1];                     /* Second Byte of Channel */
  1608.     if ( p[1] == IAC )
  1609.         sb[i++] = IAC;
  1610.     sb[i++] = (CHAR) IAC;               /* End of Subnegotiation */
  1611.     sb[i++] = (CHAR) SE;                /* marked by IAC SE */
  1612.     if (ttol((CHAR *)sb,i) < 0) {       /* Send it. */
  1613.         return(-1);
  1614.     }
  1615. #ifdef DEBUG
  1616.     if (deblog || tn_deb || debses) {
  1617.         ckmakxmsg(tn_msg,TN_MSG_LEN,"TELNET SENT SB ",TELOPT(TELOPT_FORWARD_X),
  1618.                  " CLOSE CHANNEL=",ckitoa(channel)," IAC SE",
  1619.                   NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  1620.         debug(F100,tn_msg,"",0);
  1621.         if (tn_deb || debses) tn_debug(tn_msg);
  1622.     }
  1623. #endif /* DEBUG */
  1624.     return(0);
  1625. }
  1626.  
  1627. int
  1628. #ifdef CK_ANSIC
  1629. fwdx_send_open(int channel)
  1630. #else /* CK_ANSIC */
  1631. fwdx_send_open(channel) int channel;
  1632. #endif /* CK_ANSIC */
  1633. {
  1634.     unsigned short nchannel;
  1635.     int i;
  1636.     CHAR * p;
  1637.  
  1638. #ifdef CK_SSL
  1639.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1640.         return(0);
  1641.     }
  1642. #endif /* CK_SSL */
  1643.  
  1644.     nchannel = htons(channel);
  1645.     p = (unsigned char *) &nchannel;
  1646.  
  1647.     i = 0;
  1648.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  1649.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  1650.     sb[i++] = TELOPT_FORWARD_X;           /* Forward X */
  1651.     sb[i++] = FWDX_OPEN;                  /* Open */
  1652.     sb[i++] = p[0];                       /* First Byte of Channel */
  1653.     if ( p[0] == IAC )
  1654.         sb[i++] = IAC;
  1655.     sb[i++] = p[1];                       /* Second Byte of Channel */
  1656.     if ( p[1] == IAC )
  1657.         sb[i++] = IAC;
  1658.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  1659.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  1660.     if (ttol((CHAR *)sb,i) < 0) {         /* Send it. */
  1661.         return(-1);
  1662.     }
  1663. #ifdef DEBUG
  1664.     if (deblog || tn_deb || debses) {
  1665.         ckmakxmsg(tn_msg,TN_MSG_LEN,"TELNET SENT SB ",TELOPT(TELOPT_FORWARD_X),
  1666.                   " OPEN CHANNEL=",ckitoa(channel)," IAC SE",
  1667.                   NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  1668.         debug(F100,tn_msg,"",0);
  1669.         if (tn_deb || debses) tn_debug(tn_msg);
  1670.     }
  1671. #endif /* DEBUG */
  1672.     return(0);
  1673. }
  1674.  
  1675. int
  1676. #ifdef CK_ANSIC
  1677. fwdx_client_reply_options(char *opts, int n)
  1678. #else
  1679. fwdx_client_reply_options(opts, n) char *opts; int n;
  1680. #endif /* CK_ANSIC */
  1681. {
  1682.     int i,j;
  1683.  
  1684. #ifdef CK_SSL
  1685.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1686.         return(0);
  1687.     }
  1688. #endif /* CK_SSL */
  1689.  
  1690.     i = 0;
  1691.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  1692.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  1693.     sb[i++] = TELOPT_FORWARD_X;           /* Forward X */
  1694.     sb[i++] = FWDX_OPTIONS;               /* Options */
  1695.  
  1696.     /* Look for the options we recognize and will support for this session */
  1697.     /* and reply with their bytes set                                      */
  1698.     for (j=0; j<n; j++,i++) {
  1699.         sb[i] = FWDX_OPT_NONE;          /* Add zero byte - no options */
  1700. #ifdef COMMENT
  1701.         /* If we had any options to support, this is how we would do it */
  1702.         if ( j == 0 ) {
  1703.             if (opts[j] & FWDX_OPT_XXXX) {
  1704.                 /* set flag to remember option is in use */
  1705.                 flag = 1;
  1706.                 sb[i] |= FWDX_OPT_XXXX;
  1707.             }
  1708.     }
  1709. #endif /* COMMENT */
  1710.     }
  1711.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  1712.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  1713.     if (ttol((CHAR *)sb,i) < 0) {         /* Send it. */
  1714.         return(-1);
  1715.     }
  1716. #ifdef DEBUG
  1717.     if (deblog || tn_deb || debses) {
  1718.         ckmakxmsg(tn_msg,TN_MSG_LEN,"TELNET SENT SB ",TELOPT(TELOPT_FORWARD_X),
  1719.                   " OPTIONS ",ckctox(sb[4],1)," IAC SE",
  1720.                   NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  1721.         debug(F100,tn_msg,"",0);
  1722.         if (tn_deb || debses) tn_debug(tn_msg);
  1723.     }
  1724. #endif /* DEBUG */
  1725.     return(0);
  1726. }
  1727.  
  1728.  
  1729. int
  1730. fwdx_send_options() {
  1731.     int i;
  1732.  
  1733. #ifdef CK_SSL
  1734.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1735.         return(0);
  1736.     }
  1737. #endif /* CK_SSL */
  1738.  
  1739.     i = 0;
  1740.     sb[i++] = (CHAR) IAC;               /* I Am a Command */
  1741.     sb[i++] = (CHAR) SB;                /* Subnegotiation */
  1742.     sb[i++] = TELOPT_FORWARD_X;         /* Forward X */
  1743.     sb[i++] = FWDX_OPTIONS;             /* Options */
  1744.     sb[i]   = FWDX_OPT_NONE;
  1745.     /* activate options here */
  1746.     i++;
  1747.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  1748.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  1749.     if (ttol((CHAR *)sb,i) < 0) {         /* Send it. */
  1750.         return(-1);
  1751.     }
  1752. #ifdef DEBUG
  1753.     if (deblog || tn_deb || debses) {
  1754.         ckmakmsg(tn_msg,TN_MSG_LEN,"TELNET SENT SB ",TELOPT(TELOPT_FORWARD_X),
  1755.                   " OPTIONS 00 IAC SE",NULL);
  1756.         debug(F100,tn_msg,"",0);
  1757.         if (tn_deb || debses) tn_debug(tn_msg);
  1758.     }
  1759. #endif /* DEBUG */
  1760.     return(0);
  1761. }
  1762.  
  1763. int
  1764. #ifdef CK_ANSIC
  1765. fwdx_send_data_from_channel(int channel, char * data, int len)
  1766. #else
  1767. fwdx_send_data_from_channel(channel, data, len)
  1768.     int channel; char * data; int len;
  1769. #endif
  1770. {
  1771.     extern char tn_msg[];
  1772.     unsigned short nchannel;
  1773.     static CHAR sb[2048];
  1774.     CHAR * p;
  1775.     int i, j;
  1776.     unsigned int tmp;
  1777.  
  1778.     debug(F111,"fwdx_send_data_from_channel()","channel",channel);
  1779.  
  1780. #ifdef CK_SSL
  1781.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1782.         return(0);
  1783.     }
  1784. #endif /* CK_SSL */
  1785.  
  1786.     nchannel = htons(channel);
  1787.     p = (unsigned char *) &nchannel;
  1788.  
  1789.     j = 0;
  1790.     sb[j++] = (CHAR) IAC;                 /* I Am a Command */
  1791.     sb[j++] = (CHAR) SB;                  /* Subnegotiation */
  1792.     sb[j++] = TELOPT_FORWARD_X;           /* Forward X */
  1793.     sb[j++] = FWDX_DATA;                  /* Data */
  1794.     sb[j++] = p[0];                       /* First Byte of Channel */
  1795.     if ( p[0] == IAC )
  1796.         sb[j++] = IAC;
  1797.     sb[j++] = p[1];                       /* Second Byte of Channel */
  1798.     if ( p[1] == IAC )
  1799.         sb[j++] = IAC;
  1800.  
  1801.     for (i = 0; i < len; i++) {
  1802.         tmp = (unsigned int)data[i];
  1803.         if ( tmp == IAC ) {
  1804.             sb[j++] = IAC;
  1805.             sb[j++] = IAC;
  1806.         } else {
  1807.             sb[j++] = tmp;
  1808.         }
  1809.         if ( j >= 2045 && (i < len-1) ) {
  1810.             sb[j++] = (CHAR) IAC;                 /* End of Subnegotiation */
  1811.             sb[j++] = (CHAR) SE;                  /* marked by IAC SE */
  1812.             if ( ttol(sb,j) < 0 ) {
  1813.                 debug(F110,"fwdx_send_data_from_channel()","ttol() failed",0);
  1814.                 return(-1);
  1815.             }
  1816. #ifdef DEBUG
  1817.             if (deblog || tn_deb || debses) {
  1818.                 ckmakxmsg( tn_msg,TN_MSG_LEN,"TELNET SENT SB ",
  1819.                            TELOPT(TELOPT_FORWARD_X),
  1820.                            " DATA CHANNEL=",ckitoa(channel)," ",
  1821.                            NULL,NULL,NULL,NULL,NULL,NULL,NULL );
  1822.                 tn_hex(tn_msg,TN_MSG_LEN,&sb[6],j-8);
  1823.                 ckstrncat(tn_msg," IAC SE",TN_MSG_LEN);
  1824.                 debug(F100,tn_msg,"",0);
  1825.                 if (tn_deb || debses) tn_debug(tn_msg);
  1826.             }
  1827. #endif /* DEBUG */
  1828.  
  1829.             sb[0] = (CHAR) IAC;                 /* I Am a Command */
  1830.             sb[1] = (CHAR) SB;                  /* Subnegotiation */
  1831.             sb[2] = TELOPT_FORWARD_X;           /* Forward X */
  1832.             sb[3] = FWDX_DATA;                  /* Data */
  1833.             sb[4] = p[0];
  1834.             sb[5] = p[1];
  1835.  
  1836.             j = 6;
  1837.         }
  1838.     }
  1839.  
  1840.     sb[j++] = (CHAR) IAC;                 /* End of Subnegotiation */
  1841.     sb[j++] = (CHAR) SE;                  /* marked by IAC SE */
  1842.     if ( ttol(sb,j) < 0 ) {
  1843.         debug(F110,"fwdx_send_data_from_channel()","ttol() failed",0);
  1844.         return(-1);
  1845.     }
  1846.  
  1847. #ifdef DEBUG
  1848.     if (deblog || tn_deb || debses) {
  1849.         ckmakxmsg( tn_msg,TN_MSG_LEN,
  1850.                    "TELNET SENT SB ",TELOPT(TELOPT_FORWARD_X),
  1851.                    " DATA ",ckctox(p[0],1)," ",ckctox(p[1],1)," ",
  1852.                    NULL,NULL,NULL,NULL,NULL);
  1853.         tn_hex(tn_msg,TN_MSG_LEN,&sb[6],j-8);
  1854.         ckstrncat(tn_msg," IAC SE",TN_MSG_LEN);
  1855.         debug(F100,tn_msg,"",0);
  1856.         if (tn_deb || debses) tn_debug(tn_msg);
  1857.     }
  1858. #endif /* DEBUG */
  1859.  
  1860.     return(0);
  1861. }
  1862.  
  1863. static unsigned char *
  1864. #ifdef CK_ANSIC
  1865. fwdx_add_quoted_twobyte(unsigned char *p, unsigned short twobyte)
  1866. #else
  1867. fwdx_add_quoted_twobyte(p, twobyte)
  1868.     unsigned char *p; unsigned short twobyte;
  1869. #endif /* CK_ANSIC */
  1870. /* adds the IAC quoted (MSB) representation of 'channel' at buffer pointer 'p',
  1871.  * returning pointer to new buffer position. NO OVERFLOW CHECK!
  1872.  */
  1873. {
  1874.     *p++ = (unsigned char)((twobyte >> 8) & 0xFF);
  1875.     if (*(p - 1) == 0xFF)
  1876.         *p++ = 0xFF;
  1877.     *p++ = (unsigned char)(twobyte & 0xFF);
  1878.     if (*(p - 1) == 0xFF)
  1879.         *p++ = 0xFF;
  1880.     return p;
  1881. }
  1882.  
  1883. int
  1884. #ifdef CK_ANSIC
  1885. fwdx_create_fake_xauth(char *name, int name_len, int data_len)
  1886. #else
  1887. fwdx_create_fake_xauth(name, name_len, data_len)
  1888.     char *name; int name_len; int data_len;
  1889. #endif /* CK_ANSIC */
  1890. {
  1891.     char stackdata[256];
  1892.     unsigned int c, n;
  1893.  
  1894.     if (!name_len || !data_len)
  1895.         return 1;
  1896.     fake_xauth.name = malloc(name_len);
  1897.     fake_xauth.data = malloc(data_len);
  1898.     if (!fake_xauth.name || !fake_xauth.data)
  1899.         return 2;
  1900.     fake_xauth.name_length = name_len;
  1901.     memcpy(fake_xauth.name, name, name_len);
  1902.     fake_xauth.data_length = data_len;
  1903.  
  1904.     /* try to make a random unsigned int to feed srand() */
  1905.     c = time(NULL);
  1906.     c *= getpid();
  1907.     for (n = 0; n < sizeof(stackdata); n++)
  1908.         c += stackdata[n];
  1909.     srand((unsigned int)c);
  1910.     for (c = 0; c < data_len; c++)
  1911.         fake_xauth.data[c] = (unsigned char)rand();
  1912.     return 0;
  1913. }
  1914.  
  1915. #ifdef COMMENT
  1916. /* No longer used */
  1917. int
  1918. fwdx_send_xauth(void)
  1919. {
  1920.     int c, err, dpynum, family, sb_len;
  1921.     char *display, *host = NULL;
  1922.     unsigned char *sb, *p;
  1923.  
  1924.     /* parse the local DISPLAY env var */
  1925.     if (!(display = tn_get_display()))
  1926.         return (-1);
  1927.     if (fwdx_parse_displayname(display, &family, &host, &dpynum, NULL, NULL)) {
  1928.     char * disp_no = ckitoa(dpynum);
  1929.     if (family == FamilyLocal) {
  1930.         /* call with address = "<local host name>" */
  1931.         char address[300] = "localhost";
  1932.         gethostname(address, sizeof(address) - 1);
  1933.         real_xauth = XauGetAuthByAddr(family,
  1934.                       strlen(address),
  1935.                       address,
  1936.                       strlen(disp_no),
  1937.                       disp_no, 0, NULL
  1938.                       );
  1939.     }
  1940.     else if (family == FamilyInternet) {
  1941.         /* call with address = 4 bytes numeric ip addr (MSB) */
  1942.             struct hostent *hi;
  1943.             if (hi = gethostbyname(host))
  1944.             real_xauth = XauGetAuthByAddr(family, 4,
  1945.                           hi->h_addr,
  1946.                           strlen(disp_no),
  1947.                           disp_no, 0, NULL
  1948.                           );
  1949.     }
  1950.     }
  1951.     if (host) {
  1952.         free(host);
  1953.         host = NULL;
  1954.     }
  1955.     if (real_xauth)
  1956.         err = fwdx_create_fake_xauth(real_xauth->name,
  1957.                      real_xauth->name_length,
  1958.                      real_xauth->data_length
  1959.                      );
  1960.     else
  1961.       err = fwdx_create_fake_xauth("MIT-MAGIC-COOKIE-1",
  1962.                    strlen("MIT-MAGIC-COOKIE-1"), 16);
  1963.     if (err)
  1964.         return(-1);
  1965.  
  1966.     /* allocate memory for the SB block, alloc for worst case              */
  1967.     /* the following sprintf() calls are safe due to length checking       */
  1968.     /* buffer is twice as big as the input just in case every byte was IAC */
  1969.     sb_len = 5 + 2 + 2 + fake_xauth.name_length + fake_xauth.data_length + 2;
  1970.     if (!(sb = malloc(2 * sb_len)))
  1971.         return(-1);
  1972.     p = sb;
  1973.     sprintf(p, "%c%c%c%c%c", IAC, SB, TELOPT_FORWARD_X,
  1974.         FWDX_OPT_DATA, FWDX_OPT_XAUTH);
  1975.     p += 5;
  1976.     p = fwdx_add_quoted_twobyte(p, fake_xauth.name_length);
  1977.     p = fwdx_add_quoted_twobyte(p, fake_xauth.data_length);
  1978.     for (c = 0; c < fake_xauth.name_length; c++) {
  1979.         *p++ = fake_xauth.name[c];
  1980.         if ((unsigned char)fake_xauth.name[c] == 0xFF)
  1981.         *p++ = 0xFF;
  1982.     }
  1983.     for (c = 0; c < fake_xauth.data_length; c++) {
  1984.         *p++ = fake_xauth.data[c];
  1985.         if ((unsigned char)fake_xauth.data[c] == 0xFF)
  1986.         *p++ = 0xFF;
  1987.     }
  1988.     sprintf(p, "%c%c", IAC, SE);
  1989.     p += 2;
  1990.  
  1991.     /* Add Telnet Debug info here */
  1992.     if ( ttol(sb,p-sb) < 0 ) {
  1993.         debug(F110,"fwdx_send_xauth()","ttol() failed",0);
  1994.         return(-1);
  1995.     }
  1996.  
  1997. #ifdef DEBUG
  1998.     if (deblog || tn_deb || debses) {
  1999.         sprintf(tn_msg,"TELNET SENT SB %s OPTION_DATA XAUTH ",
  2000.                  TELOPT(TELOPT_FORWARD_X));
  2001.         tn_hex(tn_msg,TN_MSG_LEN,&sb[5],(p-sb)-7);
  2002.         ckstrncat(tn_msg," IAC SE",TN_MSG_LEN);
  2003.         debug(F100,tn_msg,"",0);
  2004.         if (tn_deb || debses) tn_debug(tn_msg);
  2005.     }
  2006. #endif /* DEBUG */
  2007.  
  2008.     free(sb);
  2009.     return(0);
  2010. }
  2011. #endif /* COMMENT */
  2012. #ifdef FWDX_SERVER
  2013. /* Only if we ever become a server - not yet ported to Kermit   */
  2014. /* And even so most of this code does not belong in this module */
  2015.  
  2016. int
  2017. fwdx_write_xauthfile(void)
  2018. {
  2019.     int dpynum, scrnum, family;
  2020.     char myhost[300], *host, *rest = NULL;
  2021.     FILE *file;
  2022.     struct sockaddr_in saddr;
  2023.     struct hostent *hi;
  2024.  
  2025.     if (!fwdx_display && !fwdx_xauthfile)
  2026.         return 1;
  2027.     if (!parse_displayname(fwdx_display,
  2028.                &family, &host, &dpynum, &scrnum, &rest))
  2029.         return 2;
  2030.     if (rest) free(rest);
  2031.     if (host) free(host);
  2032.     if (family != FamilyInternet)
  2033.         return 3; /* every thing but FamilyInternet is unexpected */
  2034.  
  2035.     /* X connections to localhost:1 is actually treated as local unix sockets,
  2036.      * see the 'xauth' man page.
  2037.      */
  2038.     xauth.family = FamilyLocal;
  2039.     if (gethostname(myhost, sizeof(myhost) - 1))
  2040.         return 5;
  2041.     xauth.address_length = strlen(myhost);
  2042.     if (!(xauth.address = malloc(xauth.address_length)))
  2043.         return 5;
  2044.     memcpy(xauth.address, myhost, xauth.address_length);
  2045.  
  2046.     /* the display number is written as a string, not numeric */
  2047.     if (!(xauth.number = malloc(6)))
  2048.         return 6;
  2049.     snprintf(xauth.number, 5, "%u", dpynum);
  2050.     xauth.number_length = strlen(xauth.number);
  2051.     if (!(file = fopen(fwdx_xauthfile, "wb")))
  2052.         return 7;
  2053.     if (!XauWriteAuth(file, &xauth))
  2054.         return 8;
  2055.     fclose(file);
  2056.     setenv("XAUTHORITY", fwdx_xauthfile, 1);
  2057.     return 0;
  2058. }
  2059.  
  2060. int
  2061. fwdx_setup_xauth(unsigned char *sp, int len)
  2062. /* called with 'len' xauth bytes, starting at 'sp'
  2063.  * the data format is: <uint16 name_length> <uint16 data_length> <name> <data>
  2064.  */
  2065. {
  2066.     int xauthfd;
  2067.  
  2068.     if (!fwdx_options[FWDX_OPT_XAUTH])
  2069.         return 1;
  2070.     if (len < 4)
  2071.         return 2;
  2072.  
  2073.     /* setup the xauth struct */
  2074.     xauth.name_length = (sp[0] << 8) + sp[1];
  2075.     xauth.data_length = (sp[2] << 8) + sp[3];
  2076.     if (len != 4 + xauth.name_length + xauth.data_length)
  2077.         return 3;
  2078.     xauth.name = malloc(xauth.name_length);
  2079.     xauth.data = malloc(xauth.data_length);
  2080.     if (!xauth.name || !xauth.data)
  2081.         return 4;
  2082.     memcpy(xauth.name, sp + 4, xauth.name_length);
  2083.     memcpy(xauth.data, sp + 4 + xauth.name_length, xauth.data_length);
  2084.  
  2085.     /* Setup to always have a local .Xauthority. */
  2086.     fwdx_xauthfile = malloc(MAXPATHLEN+1);
  2087.     snprintf(fwdx_xauthfile, MAXPATHLEN, "/tmp/XauthXXXXXX");
  2088.     if ((xauthfd = mkstemp(fwdx_xauthfile)) != -1)
  2089.         /* we change file ownership later, when we know who is to be owner! */
  2090.     close(xauthfd);
  2091.     else {
  2092.     free(fwdx_xauthfile);
  2093.     fwdx_xauthfile = NULL;
  2094.     return 5;
  2095.     }
  2096. /* Must have the subshell's new DISPLAY env var to write xauth to xauthfile */
  2097.     if (fwdx_display)
  2098.         if (fwdx_write_xauthfile())
  2099.         return 6;
  2100.  
  2101.     return 0;
  2102. }
  2103.  
  2104. void fwdx_set_xauthfile_owner(int uid)
  2105. {
  2106.     struct passwd *pwd;
  2107.  
  2108.     if (!fwdx_xauthfile || !(pwd = getpwuid(uid)))
  2109.         return;
  2110.     chown(fwdx_xauthfile, pwd->pw_uid, pwd->pw_gid);
  2111. }
  2112.  
  2113. int
  2114. fwdx_server_accept_options(unsigned char *sp, int len)
  2115. /* called with 'len' option bytes, starting at 'sp' */
  2116. {
  2117.     int c;
  2118.  
  2119.     for (c = 0; c < len-2; c++) {
  2120.         if (c == 0) {
  2121.             if (sp[c] & FWDX_OPT_XAUTH)
  2122.                 flag = 1;
  2123.         }
  2124.     }
  2125.     return(0);
  2126. }
  2127. #endif /* FWDX_SERVER */
  2128. #endif /* CK_FORWARD_X */
  2129.  
  2130. #ifdef IKS_OPTION
  2131. /*
  2132.   iks_wait() -- Wait for an IKS subnegotiation response.
  2133.   sb - is either KERMIT_REQ_START or KERMIT_REQ_STOP depending on the desired
  2134.        state of the peer's Kermit server.
  2135.   flushok - specifies whether it is ok to throw away non-Telnet data
  2136.        if so, then we call ttflui() instead of tn_flui().
  2137.   Returns:
  2138.    1 if the desired state is achieved or if it is unknown.
  2139.    0 if the desired state is not achieved.
  2140. */
  2141. int
  2142. #ifdef CK_ANSIC
  2143. iks_wait(int sb, int flushok)
  2144. #else /* CK_ANSIC */
  2145. iks_wait(sb,flushok) int sb; int flushok;
  2146. #endif /* CK_ANSIC */
  2147. {
  2148.     int tn_wait_save = tn_wait_flg;
  2149.     int x;
  2150.  
  2151.     if (TELOPT_U(TELOPT_KERMIT)) {
  2152.         switch (sb) {
  2153.           case KERMIT_REQ_START:
  2154.             debug(F111,
  2155.                   "iks_wait KERMIT_REQ_START",
  2156.                   "u_start",
  2157.                   TELOPT_SB(TELOPT_KERMIT).kermit.u_start
  2158.                   );
  2159.             tn_siks(KERMIT_REQ_START);
  2160.             tn_wait_flg = 1;            /* Kermit Option MUST wait */
  2161.             do {
  2162.                 if (flushok)
  2163.                   tn_wait_idx = 0;
  2164.                 x = tn_wait("iks_wait() me_iks_req_start");
  2165.             } while (x == 0 && flushok && tn_wait_idx == TN_WAIT_BUF_SZ);
  2166.             tn_wait_flg = tn_wait_save;
  2167.             if (flushok)
  2168.               tn_wait_idx = 0;
  2169.         if (tn_wait_idx == TN_WAIT_BUF_SZ) {
  2170.         /*
  2171.          * We are attempting to start a kermit server on the peer
  2172.          * the most likely reason is because we want to perform a
  2173.          * file transfer.  But there is a huge amount of non telnet
  2174.          * negotiation data coming in and so we have not been able
  2175.          * to find the response.  So we will lie and assume that
  2176.          * response is 'yes'.  The worse that will happen is that
  2177.          * a RESP_STOP is received after we enter protocol mode.
  2178.          * And the protocol operation will be canceled.
  2179.          */
  2180.         tn_push();
  2181.         return(1);
  2182.         } else {
  2183.         tn_push();
  2184.         return(TELOPT_SB(TELOPT_KERMIT).kermit.u_start);
  2185.         }
  2186.           case KERMIT_REQ_STOP:
  2187.             debug(F111,
  2188.                   "iks_wait KERMIT_REQ_STOP",
  2189.                   "u_start",
  2190.                   TELOPT_SB(TELOPT_KERMIT).kermit.u_start
  2191.                   );
  2192.             tn_siks(KERMIT_REQ_STOP);
  2193.             tn_wait_flg = 1;            /* Kermit Option MUST wait */
  2194.             do {
  2195.                 if (flushok)
  2196.                   tn_wait_idx = 0;
  2197.                 x = tn_wait("iks_wait() me_iks_req_stop");
  2198.             } while (x == 0 && flushok && tn_wait_idx == TN_WAIT_BUF_SZ);
  2199.             tn_wait_flg = tn_wait_save;
  2200.             if (flushok)
  2201.               tn_wait_idx = 0;
  2202.  
  2203.         if (tn_wait_idx == TN_WAIT_BUF_SZ) {
  2204.         /*
  2205.          * We are attempting to stop a kermit server on the peer
  2206.          * the most likely reason being that we want to enter
  2207.          * CONNECT mode.  But there is a huge amount of non telnet
  2208.          * negotiation data coming in and so we have not been able
  2209.          * to find the response.  So we will lie and assume that
  2210.          * the answer is 'yes' and allow the CONNECT command to
  2211.          * succeed.  The worst that happens is that CONNECT mode
  2212.          * swallows the incoming data displaying it to the user
  2213.          * and then it resumes Kermit client mode.
  2214.          */
  2215.         tn_push();
  2216.         return(1);
  2217.         } else {
  2218.         tn_push();
  2219.         return(!TELOPT_SB(TELOPT_KERMIT).kermit.u_start);
  2220.         }
  2221.         }
  2222.         tn_push();
  2223.     }
  2224.     return(1);
  2225. }
  2226.  
  2227. int
  2228. #ifdef CK_ANSIC
  2229. iks_tn_sb(CHAR * sb, int n)
  2230. #else
  2231. iks_tn_sb(sb, n) CHAR * sb; int n;
  2232. #endif /* CK_ANSIC */
  2233. {
  2234.     extern int server;
  2235. #ifdef NOICP
  2236.     extern int autodl;
  2237.     int inautodl = 0, cmdadl = 1;
  2238. #else
  2239. #ifdef CK_AUTODL
  2240.     extern int autodl, inautodl, cmdadl;
  2241. #endif /* CK_AUTODL */
  2242. #endif /* NOICP */
  2243.     switch (sb[0]) {
  2244.       case KERMIT_START:                /* START */
  2245.         TELOPT_SB(TELOPT_KERMIT).kermit.u_start = 1;
  2246.         return(4);
  2247.  
  2248.       case KERMIT_STOP:                 /* STOP */
  2249.         TELOPT_SB(TELOPT_KERMIT).kermit.u_start = 0;
  2250.         return(4);
  2251.  
  2252.       case KERMIT_REQ_START:            /* REQ-START */
  2253. #ifndef NOXFER
  2254.         if (inserver) {
  2255. #ifdef CK_AUTODL
  2256.             cmdadl = 1;                 /* Turn on packet detection */
  2257. #endif /* CK_AUTODL */
  2258.             TELOPT_SB(TELOPT_KERMIT).kermit.me_start = 1;
  2259.             tn_siks(KERMIT_RESP_START);
  2260.         } else if (TELOPT_SB(TELOPT_KERMIT).kermit.me_start) {
  2261.             tn_siks(KERMIT_RESP_START);
  2262.         } else {
  2263. #ifdef CK_AUTODL
  2264.             if ((local && what == W_CONNECT && autodl) ||
  2265.                 (local && what != W_CONNECT && inautodl)
  2266.                 )
  2267.               tn_siks(KERMIT_RESP_START); /* STOP */
  2268.             else
  2269. #endif /* CK_AUTODL */
  2270.               tn_siks(KERMIT_RESP_STOP);
  2271.         }
  2272. #else /* NOXFER */
  2273.           tn_siks(KERMIT_RESP_STOP);
  2274. #endif /* NOXFER */
  2275.         return(4);
  2276.  
  2277.       case KERMIT_REQ_STOP:             /* REQ-STOP */
  2278.         /* The protocol requires that the request be responded to */
  2279.         /* either by changing states or by reporting the current  */
  2280.         /* state.  */
  2281.  
  2282.         /* We need to provide the user some way of dictating what */
  2283.         /* the policies should be.  For instance, if we are in    */
  2284.         /* CONNECT mode with autodownload ON and we get a REQ-STOP*/
  2285.         /* what should the proper response be?                    */
  2286. #ifndef NOXFER
  2287.         if (inserver
  2288. #ifdef CK_AUTODL
  2289.             || !local && cmdadl
  2290. #endif /* CK_AUTODL */
  2291.             ) {
  2292. #ifdef CK_AUTODL
  2293.             cmdadl = 0;                 /* Turn off packet detection */
  2294. #endif /* CK_AUTODL */
  2295.             tn_siks(KERMIT_RESP_STOP);
  2296.         } else if (server) {
  2297.             extern int en_fin;
  2298.             if (en_fin) {               /* If the server is allowed to stop */
  2299.                 tn_siks(KERMIT_RESP_STOP);
  2300.             } else {                    /* We are not allowed to stop */
  2301.                 tn_siks(KERMIT_RESP_START);
  2302.             }
  2303. #ifdef CK_AUTODL
  2304.         } else if ((local && what == W_CONNECT && autodl) ||
  2305.                    (local && what != W_CONNECT && inautodl)
  2306.                    ) {
  2307.             /* If we are a pseudo-server and the other side requests */
  2308.             /* that we stop, tell then that we have even though we   */
  2309.             /* have not.  Otherwise, the other side might refuse to  */
  2310.             /* enter SERVER mode.                                    */
  2311.  
  2312.             tn_siks(KERMIT_RESP_STOP);  /* STOP */
  2313. #endif /* CK_AUTODL */
  2314.  
  2315.         } else
  2316. #endif /* NOXFER */
  2317.     {
  2318.             /* If we are not currently in any mode that accepts */
  2319.             /* Kermit packets then of course report that we are */
  2320.             /* not being a Kermit server.                       */
  2321.  
  2322.             tn_siks(KERMIT_RESP_STOP);  /* STOP */
  2323.         }
  2324.         return(4);
  2325.  
  2326.       case KERMIT_SOP: {                /* SOP */
  2327. #ifndef NOXFER
  2328.           extern CHAR stchr;            /* Incoming SOP character */
  2329.           stchr = sb[1];
  2330. #endif /* NOXFER */
  2331.           TELOPT_SB(TELOPT_KERMIT).kermit.sop = 1;
  2332.           return(4);
  2333.       }
  2334.  
  2335.       case KERMIT_RESP_START:           /* START */
  2336.         TELOPT_SB(TELOPT_KERMIT).kermit.u_start = 1;
  2337.         if (TELOPT_SB(TELOPT_KERMIT).kermit.me_req_start) {
  2338.             TELOPT_SB(TELOPT_KERMIT).kermit.me_req_start = 0;
  2339.         } else if (TELOPT_SB(TELOPT_KERMIT).kermit.me_req_stop) {
  2340.             /* If we have issued a request to stop a Kermit Server */
  2341.             /* and the response is Start, then we must report this */
  2342.             /* to the caller.                                      */
  2343.             TELOPT_SB(TELOPT_KERMIT).kermit.me_req_stop = 0;
  2344.         }
  2345.         return(4);
  2346.  
  2347.       case KERMIT_RESP_STOP:            /* STOP */
  2348.         TELOPT_SB(TELOPT_KERMIT).kermit.u_start = 0;
  2349.         if (TELOPT_SB(TELOPT_KERMIT).kermit.me_req_start) {
  2350.             TELOPT_SB(TELOPT_KERMIT).kermit.me_req_start = 0;
  2351.             /* If we have issued a request to start a Kermit Server */
  2352.             /* and the response is Stop, then we must report this   */
  2353.             /* to the caller.                                       */
  2354.         } else if (TELOPT_SB(TELOPT_KERMIT).kermit.me_req_stop) {
  2355.             TELOPT_SB(TELOPT_KERMIT).kermit.me_req_stop = 0;
  2356.         }
  2357.         return(4);
  2358.  
  2359.       default:
  2360.         return(0);
  2361.  
  2362.     } /* switch (sb[0]) */
  2363. }
  2364. #endif /* IKS_OPTION */
  2365.  
  2366. /* Initialize telnet settings - set default values for ME and U modes */
  2367. int
  2368. tn_set_modes() {
  2369.     int opt,cmd;
  2370. #ifdef CK_FORWARD_X
  2371.     int x;
  2372. #endif /* CK_FORWARD_X */
  2373. #ifdef CK_ENVIRONMENT
  2374.     {
  2375.         int i,j;
  2376.         for (i = 0; i < 8; i++) {
  2377.             tn_env_uservar[i][0] = NULL;
  2378.             tn_env_uservar[i][1] = NULL;
  2379.         }
  2380.     }
  2381. #endif /* CK_ENVIRONMENT */
  2382.  
  2383.     /* initialize all options to refuse in both directions */
  2384.     for (opt = 0; opt < NTELOPTS; opt++) {
  2385.         TELOPT_ME(opt) = 0;
  2386.         TELOPT_U(opt)  = 0;
  2387.         TELOPT_UNANSWERED_WILL(opt) = 0;
  2388.         TELOPT_UNANSWERED_DO(opt)   = 0;
  2389.         TELOPT_UNANSWERED_WONT(opt) = 0;
  2390.         TELOPT_UNANSWERED_DONT(opt)   = 0;
  2391.         TELOPT_UNANSWERED_SB(opt)   = 0;
  2392.         TELOPT_ME_MODE(opt) = TN_NG_RF;
  2393.         TELOPT_U_MODE(opt) = TN_NG_RF;
  2394.         TELOPT_DEF_S_ME_MODE(opt) = TN_NG_RF;
  2395.         TELOPT_DEF_S_U_MODE(opt) = TN_NG_RF;
  2396.         TELOPT_DEF_C_ME_MODE(opt) = TN_NG_RF;
  2397.         TELOPT_DEF_C_U_MODE(opt) = TN_NG_RF;
  2398.         for (cmd = 0; cmd < 4; cmd ++)
  2399.           tncnts[TELOPT_INDEX(opt)][cmd] = 0;
  2400.     }
  2401. #ifdef IKS_OPTION
  2402.     TELOPT_SB(TELOPT_KERMIT).kermit.me_start = 0;
  2403.     TELOPT_SB(TELOPT_KERMIT).kermit.u_start = 0;
  2404.     TELOPT_SB(TELOPT_KERMIT).kermit.me_req_start = 0;
  2405.     TELOPT_SB(TELOPT_KERMIT).kermit.me_req_stop = 0;
  2406.     TELOPT_SB(TELOPT_KERMIT).kermit.sop = 0;
  2407. #endif /* IKS_OPTION */
  2408.  
  2409. #ifdef CK_ENCRYPTION
  2410.     TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 0;
  2411. #endif /* CK_ENCRYPTION */
  2412.  
  2413. #ifdef  CK_NAWS
  2414.     TELOPT_SB(TELOPT_NAWS).naws.x = 0;
  2415.     TELOPT_SB(TELOPT_NAWS).naws.y = 0;
  2416. #endif /* CK_NAWS */
  2417.  
  2418. #ifdef CK_SSL
  2419.     TELOPT_SB(TELOPT_START_TLS).start_tls.u_follows = 0;
  2420.     TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows = 0;
  2421. #endif /* CK_SSL */
  2422.  
  2423.     /* Now set the ones we want to accept to the proper values */
  2424.     TELOPT_DEF_S_ME_MODE(TELOPT_SGA) = TN_NG_RQ;
  2425.     TELOPT_DEF_S_U_MODE(TELOPT_SGA) = TN_NG_RQ;
  2426.     TELOPT_DEF_C_ME_MODE(TELOPT_SGA) = TN_NG_AC;
  2427.     TELOPT_DEF_C_U_MODE(TELOPT_SGA) = TN_NG_AC;
  2428.  
  2429.     TELOPT_DEF_S_ME_MODE(TELOPT_BINARY) = TN_NG_AC;
  2430.     TELOPT_DEF_S_U_MODE(TELOPT_BINARY) = TN_NG_AC;
  2431.     TELOPT_DEF_C_ME_MODE(TELOPT_BINARY) = TN_NG_AC;
  2432.     TELOPT_DEF_C_U_MODE(TELOPT_BINARY) = TN_NG_AC;
  2433.  
  2434.     TELOPT_DEF_S_ME_MODE(TELOPT_LOGOUT) = TN_NG_AC;
  2435.     TELOPT_DEF_S_U_MODE(TELOPT_LOGOUT) = TN_NG_AC;
  2436.     TELOPT_DEF_C_ME_MODE(TELOPT_LOGOUT) = TN_NG_AC;
  2437.     TELOPT_DEF_C_U_MODE(TELOPT_LOGOUT) = TN_NG_AC;
  2438.  
  2439. #ifdef IKS_OPTION
  2440.     TELOPT_DEF_S_ME_MODE(TELOPT_KERMIT) = TN_NG_RQ;
  2441.     TELOPT_DEF_S_U_MODE(TELOPT_KERMIT) = TN_NG_RQ;
  2442.     TELOPT_DEF_C_ME_MODE(TELOPT_KERMIT) = TN_NG_RQ;
  2443.     TELOPT_DEF_C_U_MODE(TELOPT_KERMIT) = TN_NG_RQ;
  2444. #endif /* IKS_OPTION */
  2445.  
  2446. #ifdef CK_ENCRYPTION
  2447.     TELOPT_DEF_S_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RQ;
  2448.     TELOPT_DEF_S_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RQ;
  2449.     TELOPT_DEF_C_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RQ;
  2450.     TELOPT_DEF_C_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RQ;
  2451. #endif /* CK_ENCRYPTION */
  2452.  
  2453.     TELOPT_DEF_S_ME_MODE(TELOPT_ECHO) = TN_NG_RQ;
  2454. #ifdef IKSD
  2455.     if ( !inserver )
  2456. #endif /* IKSD */
  2457.       TELOPT_DEF_S_U_MODE(TELOPT_TTYPE) = TN_NG_RQ;
  2458.  
  2459. #ifdef CK_ENVIRONMENT
  2460.     TELOPT_DEF_S_U_MODE(TELOPT_NEWENVIRON) = TN_NG_RQ;
  2461. #endif /* CK_ENVIRONMENT */
  2462.  
  2463. #ifdef CK_AUTHENTICATION
  2464.     TELOPT_DEF_S_U_MODE(TELOPT_AUTHENTICATION) = TN_NG_RQ;
  2465. #endif /* CK_AUTHENTICATION */
  2466.  
  2467. #ifdef CK_SSL
  2468.     if (ck_ssleay_is_installed()) {
  2469.         TELOPT_DEF_S_U_MODE(TELOPT_START_TLS) = TN_NG_RQ;
  2470.         TELOPT_DEF_C_ME_MODE(TELOPT_START_TLS) = TN_NG_AC;
  2471.     }
  2472. #endif /* CK_SSL */
  2473.  
  2474. #ifdef CK_NAWS
  2475.     TELOPT_DEF_S_U_MODE(TELOPT_NAWS) = TN_NG_RQ;
  2476. #endif /* CK_NAWS */
  2477.  
  2478.     TELOPT_DEF_C_U_MODE(TELOPT_ECHO) = TN_NG_AC;
  2479.     TELOPT_DEF_C_ME_MODE(TELOPT_TTYPE) = TN_NG_RQ;
  2480.  
  2481. #ifdef CK_ENVIRONMENT
  2482.     TELOPT_DEF_C_ME_MODE(TELOPT_NEWENVIRON) = TN_NG_RQ;
  2483. #endif /* CK_ENVIRONMENT */
  2484.  
  2485. #ifdef CK_AUTHENTICATION
  2486.     TELOPT_DEF_C_ME_MODE(TELOPT_AUTHENTICATION) = TN_NG_RQ;
  2487. #endif /* CK_AUTHENTICATION */
  2488.  
  2489. #ifdef CK_NAWS
  2490.     TELOPT_DEF_C_ME_MODE(TELOPT_NAWS) = TN_NG_RQ;
  2491. #endif /* CK_NAWS */
  2492.  
  2493. #ifdef CK_SNDLOC
  2494.     TELOPT_DEF_C_ME_MODE(TELOPT_SNDLOC) = TN_NG_RQ;
  2495. #endif /* CK_SNDLOC */
  2496.  
  2497. #ifdef CK_FORWARD_X
  2498.     TELOPT_DEF_C_U_MODE(TELOPT_FORWARD_X) = TN_NG_AC;
  2499.     TELOPT_SB(TELOPT_FORWARD_X).forward_x.listen_socket = -1;
  2500.     for (x = 0; x < MAXFWDX; x++) {
  2501.        TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].fd = -1;
  2502.        TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].id = -1;
  2503.        TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].need_to_send_xauth = 0;
  2504.        TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].suspend = 0;
  2505.     }
  2506. #endif /* CK_FORWARD_X */
  2507.  
  2508. #ifdef TN_COMPORT
  2509.     TELOPT_DEF_C_ME_MODE(TELOPT_COMPORT) = TN_NG_RQ;
  2510. #endif /* TN_COMPORT */
  2511.  
  2512.     /* Set the initial values for currently known mode */
  2513.     for (opt = TELOPT_FIRST; opt <= TELOPT_LAST; opt++) {
  2514.         if (TELOPT_OK(opt)) {
  2515.             TELOPT_ME_MODE(opt) = sstelnet ?
  2516.               TELOPT_DEF_S_ME_MODE(opt) :
  2517.                 TELOPT_DEF_C_ME_MODE(opt);
  2518.             TELOPT_U_MODE(opt) = sstelnet ?
  2519.               TELOPT_DEF_S_U_MODE(opt) :
  2520.                 TELOPT_DEF_C_U_MODE(opt);
  2521.         }
  2522.     }
  2523.     return(1);
  2524. }
  2525.  
  2526.  
  2527. /* Send Delayed Subnegotiations */
  2528.  
  2529. VOID
  2530. tn_sdsb() {
  2531.     if (TELOPT_SB(TELOPT_TTYPE).term.need_to_send) {
  2532.         tn_sttyp();
  2533.         TELOPT_SB(TELOPT_TTYPE).term.need_to_send = 0;
  2534.     }
  2535. #ifdef CK_ENVIRONMENT
  2536.     if (TELOPT_SB(TELOPT_NEWENVIRON).env.need_to_send &&
  2537.         TELOPT_SB(TELOPT_NEWENVIRON).env.str) {
  2538.         tn_snenv((CHAR *)TELOPT_SB(TELOPT_NEWENVIRON).env.str,
  2539.                  TELOPT_SB(TELOPT_NEWENVIRON).env.len);
  2540.         free(TELOPT_SB(TELOPT_NEWENVIRON).env.str);
  2541.         TELOPT_SB(TELOPT_NEWENVIRON).env.str=NULL;
  2542.         TELOPT_SB(TELOPT_NEWENVIRON).env.len=0;
  2543.         TELOPT_SB(TELOPT_NEWENVIRON).env.need_to_send = 0;
  2544.     }
  2545. #ifdef CK_XDISPLOC
  2546.     if (TELOPT_SB(TELOPT_XDISPLOC).xdisp.need_to_send) {
  2547.         tn_sxdisploc();
  2548.         TELOPT_SB(TELOPT_XDISPLOC).xdisp.need_to_send = 0;
  2549.     }
  2550. #endif /* CK_XDISPLOC */
  2551. #endif /* CK_ENVIRONMENT */
  2552. #ifdef CK_NAWS
  2553.     if (TELOPT_SB(TELOPT_NAWS).naws.need_to_send) {
  2554.         tn_snaws();
  2555.         TELOPT_SB(TELOPT_NAWS).naws.need_to_send = 0;
  2556.     }
  2557. #endif /* CK_NAWS */
  2558. #ifdef CK_SNDLOC
  2559.     if (TELOPT_SB(TELOPT_SNDLOC).sndloc.need_to_send) {
  2560.         tn_sndloc();
  2561.         TELOPT_SB(TELOPT_SNDLOC).sndloc.need_to_send = 0;
  2562.     }
  2563. #endif /* CK_SNDLOC */
  2564. #ifdef CK_FORWARD_X
  2565.     if (TELOPT_SB(TELOPT_FORWARD_X).forward_x.need_to_send) {
  2566.         if ( sstelnet )
  2567.             fwdx_send_options();
  2568.         TELOPT_SB(TELOPT_FORWARD_X).forward_x.need_to_send = 0;
  2569.     }
  2570. #endif /* CK_FORWARD_X */
  2571. #ifdef TN_COMPORT
  2572.     if (TELOPT_SB(TELOPT_COMPORT).comport.need_to_send) {
  2573.         tn_sndcomport();
  2574.         TELOPT_SB(TELOPT_COMPORT).comport.need_to_send = 0;
  2575.     }
  2576. #endif /* TN_COMPORT */
  2577.  
  2578. }
  2579.  
  2580. int
  2581. tn_reset() {
  2582.     int x,opt,cmd;
  2583.  
  2584.     tn_wait_idx = 0;                    /* Clear the tn_push() buffer */
  2585.     tn_wait_tmo = TN_TIMEOUT;           /* Reset wait timer stats */
  2586.  
  2587.     nflag = 0;
  2588.  
  2589.     /* Reset the TELNET OPTIONS counts */
  2590.     for (opt = TELOPT_FIRST; opt <= TELOPT_LAST; opt++) {
  2591.         if (TELOPT_OK(opt)) {
  2592.             TELOPT_ME(opt) = 0;
  2593.             TELOPT_U(opt)  = 0;
  2594.             TELOPT_UNANSWERED_WILL(opt) = 0;
  2595.             TELOPT_UNANSWERED_DO(opt)   = 0;
  2596.             TELOPT_UNANSWERED_WONT(opt) = 0;
  2597.             TELOPT_UNANSWERED_DONT(opt)   = 0;
  2598.             TELOPT_UNANSWERED_SB(opt)   = 0;
  2599.             TELOPT_ME_MODE(opt) = sstelnet ?
  2600.               TELOPT_DEF_S_ME_MODE(opt) :
  2601.                 TELOPT_DEF_C_ME_MODE(opt);
  2602.             TELOPT_U_MODE(opt) = sstelnet ?
  2603.               TELOPT_DEF_S_U_MODE(opt) :
  2604.                 TELOPT_DEF_C_U_MODE(opt);
  2605.  
  2606. #ifdef DEBUG
  2607.             if (deblog) {
  2608.                 switch (TELOPT_ME_MODE(opt)) {
  2609.                   case TN_NG_RF:
  2610.                     debug(F110,"tn_ini ME REFUSE ",TELOPT(opt),0);
  2611.                     break;
  2612.                   case TN_NG_AC:
  2613.                     debug(F110,"tn_ini ME ACCEPT ",TELOPT(opt),0);
  2614.                     break;
  2615.                   case TN_NG_RQ:
  2616.                     debug(F110,"tn_ini ME REQUEST",TELOPT(opt),0);
  2617.                     break;
  2618.                   case TN_NG_MU:
  2619.                     debug(F110,"tn_ini ME REQUIRE",TELOPT(opt),0);
  2620.                     break;
  2621.                 }
  2622.                 switch (TELOPT_U_MODE(opt)) {
  2623.                   case TN_NG_RF:
  2624.                     debug(F110,"tn_ini U  REFUSE ",TELOPT(opt),0);
  2625.                     break;
  2626.                   case TN_NG_AC:
  2627.                     debug(F110,"tn_ini U  ACCEPT ",TELOPT(opt),0);
  2628.                     break;
  2629.                   case TN_NG_RQ:
  2630.                     debug(F110,"tn_ini U  REQUEST",TELOPT(opt),0);
  2631.                     break;
  2632.                   case TN_NG_MU:
  2633.                     debug(F110,"tn_ini U  REQUIRE",TELOPT(opt),0);
  2634.                     break;
  2635.                 }
  2636.             }
  2637. #endif /* DEBUG */
  2638.             for (cmd = 0; cmd < 4; cmd ++)
  2639.               tncnts[TELOPT_INDEX(opt)][cmd] = 0;
  2640.         }
  2641.     }
  2642. #ifdef CK_ENVIRONMENT
  2643.     if (!tn_env_flg) {
  2644.         TELOPT_ME_MODE(TELOPT_NEWENVIRON) = TN_NG_RF;
  2645.         TELOPT_U_MODE(TELOPT_NEWENVIRON) = TN_NG_RF;
  2646.     }
  2647. #endif /* CK_ENVIRONMENT */
  2648. #ifdef CK_SNDLOC
  2649.     if (!tn_loc)
  2650.         TELOPT_DEF_C_ME_MODE(TELOPT_SNDLOC) = TN_NG_RF;
  2651. #endif /* CK_SNDLOC */
  2652. #ifdef IKS_OPTION
  2653.     TELOPT_SB(TELOPT_KERMIT).kermit.me_start = 0;
  2654.     TELOPT_SB(TELOPT_KERMIT).kermit.u_start = 0;
  2655.     TELOPT_SB(TELOPT_KERMIT).kermit.me_req_start = 0;
  2656.     TELOPT_SB(TELOPT_KERMIT).kermit.me_req_stop = 0;
  2657.     TELOPT_SB(TELOPT_KERMIT).kermit.sop = 0;
  2658. #endif /* IKS_OPTION */
  2659. #ifdef CK_ENCRYPTION
  2660.     TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 0;
  2661.     TELOPT_SB(TELOPT_ENCRYPTION).encrypt.need_to_send = 0;
  2662. #endif /* CK_ENCRYPTION */
  2663. #ifdef  CK_NAWS
  2664.     TELOPT_SB(TELOPT_NAWS).naws.need_to_send = 0;
  2665.     TELOPT_SB(TELOPT_NAWS).naws.x = 0;
  2666.     TELOPT_SB(TELOPT_NAWS).naws.y = 0;
  2667. #endif /* CK_NAWS */
  2668.     TELOPT_SB(TELOPT_TTYPE).term.need_to_send = 0;
  2669.     TELOPT_SB(TELOPT_TTYPE).term.type[0] = '\0';
  2670. #ifdef CK_ENVIRONMENT
  2671.     TELOPT_SB(TELOPT_NEWENVIRON).env.need_to_send = 0;
  2672.     if (tn_first)
  2673.         TELOPT_SB(TELOPT_NEWENVIRON).env.str=NULL;
  2674.     else if (TELOPT_SB(TELOPT_NEWENVIRON).env.str) {
  2675.         free(TELOPT_SB(TELOPT_NEWENVIRON).env.str);
  2676.         TELOPT_SB(TELOPT_NEWENVIRON).env.str=NULL;
  2677.     }
  2678.     TELOPT_SB(TELOPT_NEWENVIRON).env.len=0;
  2679. #ifdef CK_XDISPLOC
  2680.     TELOPT_SB(TELOPT_XDISPLOC).xdisp.need_to_send = 0;
  2681. #endif /* CK_XDISPLOC */
  2682. #endif /* CK_ENVIRONMENT */
  2683. #ifdef CK_SNDLOC
  2684.     TELOPT_SB(TELOPT_SNDLOC).sndloc.need_to_send = 0;
  2685. #endif /* CK_SNDLOC */
  2686. #ifdef CK_FORWARD_X
  2687.     TELOPT_SB(TELOPT_FORWARD_X).forward_x.need_to_send = 0;
  2688.     TELOPT_SB(TELOPT_FORWARD_X).forward_x.listen_socket = -1;
  2689.     for (x = 0; x < MAXFWDX; x++) {
  2690.        TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].fd = -1;
  2691.        TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].id = -1;
  2692.        TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].need_to_send_xauth = 0;
  2693.        TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].suspend = 0;
  2694.     }
  2695.     /* Reset Xauth data */
  2696.     if ( real_xauth ) {
  2697.         XauDisposeAuth(real_xauth);
  2698.         real_xauth = NULL;
  2699.     }
  2700.     if ( fake_xauth.name )
  2701.         free(fake_xauth.name);
  2702.     if ( fake_xauth.data )
  2703.         free(fake_xauth.data);
  2704.     if ( fake_xauth.address )
  2705.         free(fake_xauth.address);
  2706.     if ( fake_xauth.number )
  2707.         free(fake_xauth.number);
  2708.     memset(&fake_xauth,0,sizeof(fake_xauth));
  2709. #ifdef NT
  2710.     TELOPT_SB(TELOPT_FORWARD_X).forward_x.thread_started = 0;
  2711. #endif /* NT */
  2712. #endif /* CK_FORWARD_X */
  2713. #ifdef CK_SSL
  2714.     if (tls_only_flag || ssl_only_flag) {
  2715.         TELOPT_ME_MODE(TELOPT_START_TLS) = TN_NG_RF;
  2716.         TELOPT_U_MODE(TELOPT_START_TLS) = TN_NG_RF;
  2717.     }
  2718.     TELOPT_SB(TELOPT_START_TLS).start_tls.u_follows = 0;
  2719.     TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows = 0;
  2720. #endif /* CK_SSL */
  2721.  
  2722. #ifdef CK_ENCRYPTION
  2723.     if (!ck_crypt_is_installed()
  2724. #ifdef CK_SSL
  2725.         || tls_only_flag || ssl_only_flag
  2726. #endif /* CK_SSL */
  2727.         ) {
  2728.         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2729.         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2730.     }
  2731. #endif /* CK_ENCRYPTION */
  2732.  
  2733. #ifdef TN_COMPORT
  2734.     TELOPT_SB(TELOPT_COMPORT).comport.need_to_send = 0;
  2735.     TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 0;
  2736.     tnc_init();
  2737. #endif /* TN_COMPORT */
  2738.  
  2739.     tn_first = 0;                       /* No longer the first time init */
  2740.  
  2741. #ifdef OS2
  2742.     ttnum = -1;                         /* Reset TermType negotiation */
  2743.     ttnumend = 0;
  2744. #endif /* OS2 */
  2745.  
  2746.     return(0);
  2747. }
  2748.  
  2749. int
  2750. tn_start() {
  2751.     int wait, x, opt;
  2752.  
  2753.     if (tn_init && tn_begun)
  2754.         return(0);
  2755.     tn_begun = 1;
  2756.  
  2757.     debug(F111,"tn_start","sstelnet",sstelnet);
  2758.     wait = 0;
  2759.     if (tn_duplex)  {
  2760.         oldplex = duplex;               /* save old duplex value */
  2761.         duplex = 1;                     /* and set to half duplex for telnet */
  2762.     }
  2763. #ifdef CK_SSL
  2764.     if (!TELOPT_ME(TELOPT_START_TLS) &&
  2765.         TELOPT_ME_MODE(TELOPT_START_TLS) >= TN_NG_RQ) {
  2766.         if (tn_sopt(WILL, TELOPT_START_TLS) < 0)
  2767.           return(-1);
  2768.         TELOPT_UNANSWERED_WILL(TELOPT_START_TLS) = 1;
  2769.         wait = 1;
  2770.     }
  2771.     if (!TELOPT_U(TELOPT_START_TLS) &&
  2772.         TELOPT_U_MODE(TELOPT_START_TLS) >= TN_NG_RQ) {
  2773.         if (tn_sopt(DO, TELOPT_START_TLS) < 0)
  2774.           return(-1);
  2775.         TELOPT_UNANSWERED_DO(TELOPT_START_TLS) = 1;
  2776.         wait = 1;
  2777.     }
  2778. #ifdef COMMENT
  2779. /*
  2780.   We can put off waiting for this until after we have requested AUTH.  The
  2781.   next draft will specify how the WILL side is to decide between these
  2782.   conflicting options.
  2783. */
  2784.     if (wait) {
  2785.         if (tn_wait("start_tls") < 0) {
  2786.             tn_push();
  2787.             return(-1);
  2788.         }
  2789.         wait = 0;
  2790.     }
  2791. #endif /* COMMENT */
  2792. #endif /* CK_SSL */
  2793.  
  2794. #ifdef CK_AUTHENTICATION
  2795.     debug(F110,"tn_ini() CK_AUTHENTICATION","",0);
  2796.     if (tn_init)                /* tn_ini() might be called recursively */
  2797.       return(0);
  2798.     if (!TELOPT_ME(TELOPT_AUTHENTICATION) &&
  2799.         TELOPT_ME_MODE(TELOPT_AUTHENTICATION) >= TN_NG_RQ) {
  2800.         if (tn_sopt(WILL, TELOPT_AUTHENTICATION) < 0)
  2801.           return(-1);
  2802.         TELOPT_UNANSWERED_WILL(TELOPT_AUTHENTICATION) = 1;
  2803.         wait = 1;
  2804.     }
  2805.     if (!TELOPT_U(TELOPT_AUTHENTICATION) &&
  2806.         TELOPT_U_MODE(TELOPT_AUTHENTICATION) >= TN_NG_RQ) {
  2807.         if (tn_sopt(DO, TELOPT_AUTHENTICATION) < 0)
  2808.           return(-1);
  2809.         TELOPT_UNANSWERED_DO(TELOPT_AUTHENTICATION) = 1;
  2810.         wait = 1;
  2811.     }
  2812. #ifdef CK_ENCRYPTION
  2813.     if (TELOPT_U_MODE(TELOPT_AUTHENTICATION) == TN_NG_RF &&
  2814.          TELOPT_ME_MODE(TELOPT_AUTHENTICATION) == TN_NG_RF) {
  2815.         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2816.         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2817.     }
  2818. #endif /* CK_ENCRYPTION */
  2819. #endif /* CK_AUTHENTICATION */
  2820.  
  2821. #ifdef CK_NAWS
  2822. #ifndef NOLOCAL
  2823.     debug(F110,"tn_ini() CK_NAWS !NOLOCAL","",0);
  2824.     if (!sstelnet) {
  2825.         /* Console terminal screen rows and columns */
  2826. #ifdef OS2
  2827.         debug(F101,
  2828.               "tn_ini tt_rows 1",
  2829.               "",
  2830.               VscrnGetHeight(VTERM)-(tt_status[VTERM]?1:0)
  2831.               );
  2832.         debug(F101,"tn_ini tt_cols 1","",VscrnGetWidth(VTERM));
  2833.         /* Not known yet */
  2834.         if (VscrnGetWidth(VTERM) < 0 ||
  2835.             VscrnGetHeight(VTERM)-(tt_status[VTERM]?1:0) < 0) {
  2836.             ttgwsiz();                  /* Try to find out */
  2837.         }
  2838.         debug(F101,
  2839.               "tn_ini tt_rows 2",
  2840.               "",
  2841.               VscrnGetHeight(VTERM)-(tt_status[VTERM]?1:0)
  2842.               );
  2843.         debug(F101,"tn_ini tt_cols 2","",VscrnGetWidth(VTERM));
  2844.         /* Now do we know? */
  2845.         if (VscrnGetWidth(VTERM) > 0 &&
  2846.             VscrnGetHeight(VTERM)-(tt_status[VTERM]?1:0) > 0) {
  2847.             if (!TELOPT_ME(TELOPT_NAWS) &&
  2848.                 TELOPT_ME_MODE(TELOPT_NAWS) >= TN_NG_RQ) {
  2849.                 if (tn_sopt(WILL, TELOPT_NAWS) < 0)
  2850.                   return(-1);
  2851.                 TELOPT_UNANSWERED_WILL(TELOPT_NAWS) = 1;
  2852.                 wait = 1;
  2853.             }
  2854.         }
  2855. #else /* OS2 */
  2856.         debug(F101,"tn_ini tt_rows 1","",tt_rows);
  2857.         debug(F101,"tn_ini tt_cols 1","",tt_cols);
  2858.         if (tt_rows < 0 || tt_cols < 0) { /* Not known yet */
  2859.             ttgwsiz();                  /* Try to find out */
  2860.         }
  2861.         debug(F101,"tn_ini tt_rows 2","",tt_rows);
  2862.         debug(F101,"tn_ini tt_cols 2","",tt_cols);
  2863.         if (tt_rows > 0 && tt_cols > 0) { /* Now do we know? */
  2864.             if (!TELOPT_ME(TELOPT_NAWS) &&
  2865.                 TELOPT_ME_MODE(TELOPT_NAWS) >= TN_NG_RQ) {
  2866.                 if (tn_sopt(WILL, TELOPT_NAWS) < 0)
  2867.                   return(-1);
  2868.                 TELOPT_UNANSWERED_WILL(TELOPT_NAWS) = 1;
  2869.                 wait = 1;
  2870.             }
  2871.         }
  2872. #endif /* OS2 */
  2873.     } else
  2874. #endif /* NOLOCAL */
  2875.     {
  2876.         if (!TELOPT_U(TELOPT_NAWS) &&
  2877.             TELOPT_U_MODE(TELOPT_NAWS) >= TN_NG_RQ) {
  2878.             if (tn_sopt(DO, TELOPT_NAWS) < 0)
  2879.               return(-1);
  2880.             TELOPT_UNANSWERED_DO(TELOPT_NAWS) = 1;
  2881.             wait = 1;
  2882.         }
  2883.     }
  2884. #endif /* CK_NAWS */
  2885.  
  2886.     if (!TELOPT_ME(TELOPT_SGA) &&
  2887.         TELOPT_ME_MODE(TELOPT_SGA) >= TN_NG_RQ) {
  2888.         if (tn_sopt(WILL, TELOPT_SGA) < 0)
  2889.           return(-1);
  2890.         TELOPT_UNANSWERED_WILL(TELOPT_SGA) = 1;
  2891.         wait = 1;
  2892.     }
  2893.     if (!TELOPT_U(TELOPT_SGA) &&
  2894.         TELOPT_U_MODE(TELOPT_SGA) >= TN_NG_RQ) {
  2895.         if (tn_sopt(DO, TELOPT_SGA) < 0)
  2896.           return(-1);
  2897.         TELOPT_UNANSWERED_DO(TELOPT_SGA) = 1;
  2898.         wait = 1;
  2899.     }
  2900.     if (!tn_duplex) {
  2901.         if (!TELOPT_U(TELOPT_ECHO) &&
  2902.             TELOPT_U_MODE(TELOPT_ECHO) >= TN_NG_RQ) {
  2903.             if (tn_sopt(DO, TELOPT_ECHO) < 0)
  2904.               return(-1);
  2905.             TELOPT_UNANSWERED_DO(TELOPT_ECHO) = 1;
  2906.             wait = 1;
  2907.         }
  2908.     }
  2909.     if (!TELOPT_ME(TELOPT_ECHO) &&
  2910.         TELOPT_ME_MODE(TELOPT_ECHO) >= TN_NG_RQ) {
  2911.         if (tn_sopt(WILL, TELOPT_ECHO) < 0)
  2912.           return(-1);
  2913.         TELOPT_UNANSWERED_WILL(TELOPT_ECHO) = 1;
  2914.         wait = 1;
  2915.     }
  2916.  
  2917.     debug(F100,"tn_ini about to send WILL TTYPE if requested","",0);
  2918. /*
  2919.   Talking to TELNET port, so send WILL TERMINAL TYPE and DO SGA.
  2920.   Also send WILL NAWS if we know our screen dimensions.
  2921. */
  2922.     if (!TELOPT_ME(TELOPT_TTYPE) &&
  2923.         TELOPT_ME_MODE(TELOPT_TTYPE) >= TN_NG_RQ) {
  2924.         if ((x = tn_sopt(WILL,TELOPT_TTYPE)) < 0) {
  2925.             debug(F101,"tn_ini tn_sopt WILL TTYPE failed","",x);
  2926.             return(-1);
  2927.         }
  2928.         TELOPT_UNANSWERED_WILL(TELOPT_TTYPE) = 1;
  2929.         wait = 1;
  2930.         debug(F100,"tn_ini sent WILL TTYPE ok","",0);
  2931.     }
  2932.     if (!TELOPT_U(TELOPT_TTYPE) &&
  2933.         TELOPT_U_MODE(TELOPT_TTYPE) >= TN_NG_RQ) {
  2934.         if ((x = tn_sopt(DO,TELOPT_TTYPE)) < 0) {
  2935.             debug(F101,"tn_ini tn_sopt DO TTYPE failed","",x);
  2936.             return(-1);
  2937.         }
  2938.         TELOPT_UNANSWERED_DO(TELOPT_TTYPE) = 1;
  2939.         wait = 1;
  2940.         debug(F100,"tn_ini sent DO TTYPE ok","",0);
  2941.     }
  2942.     if (!TELOPT_ME(TELOPT_BINARY) &&
  2943.         TELOPT_ME_MODE(TELOPT_BINARY) >= TN_NG_RQ) {
  2944.         if (tn_sopt(WILL, TELOPT_BINARY) < 0)
  2945.           return(-1);
  2946.         TELOPT_UNANSWERED_WILL(TELOPT_BINARY) = 1;
  2947.         wait = 1;
  2948.     }
  2949.     if (!TELOPT_U(TELOPT_BINARY) &&
  2950.         TELOPT_U_MODE(TELOPT_BINARY) >= TN_NG_RQ) {
  2951.         if (tn_sopt(DO, TELOPT_BINARY) < 0)
  2952.           return(-1);
  2953.         TELOPT_UNANSWERED_DO(TELOPT_BINARY) = 1;
  2954.         wait = 1;
  2955.     }
  2956. #ifdef CK_SNDLOC
  2957.     if (tn_loc) {
  2958.         if (!TELOPT_ME(TELOPT_SNDLOC) &&
  2959.             TELOPT_ME_MODE(TELOPT_SNDLOC) >= TN_NG_RQ) {
  2960.             if (tn_sopt(WILL, TELOPT_SNDLOC) < 0)
  2961.               return(-1);
  2962.             TELOPT_UNANSWERED_WILL(TELOPT_SNDLOC) = 1;
  2963.             wait = 1;
  2964.         }
  2965.     }
  2966. #endif /* CK_SNDLOC */
  2967. #ifdef CK_ENVIRONMENT
  2968. #ifdef CK_FORWARD_X
  2969.     if (!TELOPT_U(TELOPT_FORWARD_X) &&
  2970.          TELOPT_U_MODE(TELOPT_FORWARD_X) >= TN_NG_RQ) {
  2971.         if (tn_sopt(WILL, TELOPT_FORWARD_X) < 0)
  2972.             return(-1);
  2973.         TELOPT_UNANSWERED_WILL(TELOPT_FORWARD_X) = 1;
  2974.         wait = 1;
  2975.     }
  2976. #endif /* FORWARD_X */
  2977. #ifdef CK_XDISPLOC
  2978.     if (!TELOPT_ME(TELOPT_XDISPLOC) &&
  2979.          TELOPT_ME_MODE(TELOPT_XDISPLOC) >= TN_NG_RQ) {
  2980.         if (tn_sopt(WILL, TELOPT_XDISPLOC) < 0)
  2981.             return(-1);
  2982.         TELOPT_UNANSWERED_WILL(TELOPT_XDISPLOC) = 1;
  2983.         wait = 1;
  2984.     }
  2985. #endif /* CK_XDISPLOC */
  2986.     /* Will send terminal environment. */
  2987.     if (!TELOPT_ME(TELOPT_NEWENVIRON) &&
  2988.         TELOPT_ME_MODE(TELOPT_NEWENVIRON) >= TN_NG_RQ) {
  2989.         if (tn_sopt(WILL, TELOPT_NEWENVIRON) < 0)
  2990.           return(-1);
  2991.         TELOPT_UNANSWERED_WILL(TELOPT_NEWENVIRON) = 1;
  2992.         wait = 1;
  2993.     }
  2994.     if (!TELOPT_U(TELOPT_NEWENVIRON) &&
  2995.         TELOPT_U_MODE(TELOPT_NEWENVIRON) >= TN_NG_RQ) {
  2996.         if (tn_sopt(DO, TELOPT_NEWENVIRON) < 0)
  2997.           return(-1);
  2998.         TELOPT_UNANSWERED_DO(TELOPT_NEWENVIRON) = 1;
  2999.         wait = 1;
  3000.     }
  3001. #endif /* CK_ENVIRONMENT */
  3002.  
  3003.     /* Take care of any other telnet options that require handling. */
  3004.  
  3005.     for (opt = TELOPT_FIRST; opt <= TELOPT_LAST; opt++) {
  3006.         switch (opt) {
  3007.           case TELOPT_AUTHENTICATION:
  3008.           case TELOPT_ENCRYPTION:
  3009.           case TELOPT_TTYPE:
  3010.           case TELOPT_NAWS:
  3011.           case TELOPT_BINARY:
  3012.           case TELOPT_NEWENVIRON:
  3013.           case TELOPT_SNDLOC:
  3014.           case TELOPT_XDISPLOC:
  3015.           case TELOPT_SGA:
  3016.           case TELOPT_ECHO:
  3017.           case TELOPT_KERMIT:
  3018.           case TELOPT_START_TLS:
  3019.           case TELOPT_FORWARD_X:
  3020.                 break;
  3021.             break;
  3022.           default:
  3023.             if (TELOPT_OK(opt)) {
  3024.                 if (!TELOPT_ME(opt) &&
  3025.                     TELOPT_ME_MODE(opt) >= TN_NG_RQ) {
  3026.                     if (tn_sopt(WILL, opt) < 0)
  3027.                       return(-1);
  3028.                     TELOPT_UNANSWERED_WILL(opt) = 1;
  3029.                     wait = 1;
  3030.                 }
  3031.                 if (!TELOPT_U(opt) &&
  3032.                     TELOPT_U_MODE(opt) >= TN_NG_RQ) {
  3033.                     if (tn_sopt(DO, opt) < 0)
  3034.                       return(-1);
  3035.                     TELOPT_UNANSWERED_DO(opt) = 1;
  3036.                     wait = 1;
  3037.                 }
  3038.             }
  3039.         }
  3040.     }
  3041.     if (wait) {
  3042.         if (tn_wait("pre-encrypt") < 0) {
  3043.             tn_push();
  3044.             return(-1);
  3045.         }
  3046.         wait = 0;
  3047.     }
  3048.  
  3049. #ifdef CK_ENCRYPTION
  3050.     if (tn_init)                /* tn_ini() may be called recursively */
  3051.       return(0);
  3052.  
  3053.     if (!TELOPT_ME(TELOPT_ENCRYPTION) &&
  3054.         TELOPT_ME_MODE(TELOPT_ENCRYPTION) >= TN_NG_RQ) {
  3055.         if (tn_sopt(WILL, TELOPT_ENCRYPTION) < 0)
  3056.           return(-1);
  3057.         TELOPT_UNANSWERED_WILL(TELOPT_ENCRYPTION) = 1;
  3058.         wait = 1;
  3059.     }
  3060.     if (!TELOPT_U(TELOPT_ENCRYPTION) &&
  3061.         TELOPT_U_MODE(TELOPT_ENCRYPTION) >= TN_NG_RQ) {
  3062.         if (tn_sopt(DO, TELOPT_ENCRYPTION) < 0)
  3063.           return(-1);
  3064.         TELOPT_UNANSWERED_DO(TELOPT_ENCRYPTION) = 1;
  3065.         wait = 1;
  3066.     }
  3067.  
  3068.     /* If we are going to encrypt, we want to do it before we send any more */
  3069.     /* data, especially the terminal type and environment variables.        */
  3070.     if (wait) {
  3071.         if (tn_wait("post-encrypt") < 0) {
  3072.             tn_push();
  3073.             return(-1);
  3074.         }
  3075.         wait = 0;
  3076.     }
  3077. #endif /* CK_ENCRYPTION */
  3078.  
  3079.     tn_sdsb();
  3080.  
  3081.     if (tn_init)                   /* tn_ini() may be called recursively */
  3082.         return(0);
  3083.  
  3084. #ifdef IKS_OPTION
  3085.     /* Kermit Server negotiation must go last */
  3086.     /* Send U before ME */
  3087.  
  3088.     if (!TELOPT_U(TELOPT_KERMIT) &&
  3089.         TELOPT_U_MODE(TELOPT_KERMIT) >= TN_NG_RQ) {
  3090.         if (tn_sopt(DO, TELOPT_KERMIT) < 0)
  3091.           return(-1);
  3092.         TELOPT_UNANSWERED_DO(TELOPT_KERMIT) = 1;
  3093.         wait = 1;
  3094.     }
  3095.     if (!TELOPT_ME(TELOPT_KERMIT) &&
  3096.         TELOPT_ME_MODE(TELOPT_KERMIT) >= TN_NG_RQ) {
  3097.         if (tn_sopt(WILL, TELOPT_KERMIT) < 0)
  3098.           return(-1);
  3099.         TELOPT_UNANSWERED_WILL(TELOPT_KERMIT) = 1;
  3100.         wait = 1;
  3101.     }
  3102. #endif /* IKS_OPTION */
  3103.  
  3104.     if (wait) {
  3105.         if (tn_wait("end of telnet negotiations") < 0) {
  3106.             tn_push();
  3107.             return(-1);
  3108.         }
  3109.         wait = 0;
  3110.     }
  3111.  
  3112.     tn_sdsb();                          /* Send delayed subnegotiations */
  3113.     tn_push();
  3114.     return(0);
  3115. }
  3116.  
  3117. /* Start a telnet connection. */
  3118. /* Returns -1 on error, 0 if nothing happens, 1 if init msgs sent ok */
  3119.  
  3120. int
  3121. tn_ini() {
  3122.     int x;
  3123.  
  3124.     debug(F101,"tn_ini ttnproto","",ttnproto);
  3125.     debug(F101,"tn_ini tn_init","",tn_init);
  3126.  
  3127.     if (ttnet != NET_TCPB)              /* Make sure connection is TCP/IP */
  3128.       return(0);
  3129.     if (tn_init)                        /* Have we done this already? */
  3130.       return(0);                        /* Don't do it again. */
  3131.  
  3132.     tn_reset();                         /* Reset telnet parameters */
  3133.     tn_begun = 0;                       /* Reset; will be set by tn_start() */
  3134.  
  3135.     switch ( ttnproto ) {
  3136.       case NP_RLOGIN:
  3137.       case NP_K4LOGIN:
  3138.       case NP_EK4LOGIN:
  3139.       case NP_K5LOGIN:
  3140.       case NP_EK5LOGIN:
  3141.       case NP_K5U2U:
  3142.         tn_init = 1;
  3143.     debug(F100,"tn_ini telnet negotiations ignored","tn_init",tn_init);
  3144.         return(0);
  3145.       case NP_NONE:
  3146.       case NP_SSL:
  3147.       case NP_TLS:             /* If not talking to a telnet port, */
  3148.         ttnproto = NP_TELNET;           /* pretend it's telnet anyway, */
  3149.         oldplex = duplex;               /* save old duplex value */
  3150.         duplex = 1;                     /* and set to half duplex for telnet */
  3151.         tn_wait("tn_ini - waiting to see if telnet negotiations were sent");
  3152.         debug(F100,"tn_ini skipping telnet negotiations","",0);
  3153.         return(0);
  3154.       case NP_TCPRAW:            /* Raw socket requested. */
  3155.     debug(F100,"tn_ini telnet negotiations ignored","tn_init",tn_init);
  3156.         return(0);
  3157.       case NP_KERMIT:            /* switching to Telnet protocol */
  3158.       case NP_SSL_TELNET:
  3159.       case NP_TLS_TELNET:
  3160.         debug(F101,"tn_ini switching from XXX to Telnet","",ttnproto);
  3161.         ttnproto = NP_TELNET;
  3162.     /* fall through */
  3163.       default:
  3164.     /* We are already using a variation on Telnet protocol */
  3165.     ;
  3166.     }
  3167.  
  3168.     x = tn_start();
  3169.     tn_init = 1;                        /* Remember successful completion. */
  3170.  
  3171.     /* Don't send anything else! */
  3172.     debug(F101,"tn_ini duplex","",duplex);
  3173.     debug(F101,"tn_ini done, tn_init","",tn_init);
  3174.     return(x);
  3175. }
  3176.  
  3177. int
  3178. #ifdef CK_ANSIC
  3179. tn_hex(CHAR * buf, int buflen, CHAR * data, int datalen)
  3180. #else /* CK_ANSIC */
  3181. tn_hex(buf, buflen, data, datalen)
  3182.     CHAR * buf;
  3183.     int buflen;
  3184.     CHAR * data;
  3185.     int datalen;
  3186. #endif /* CK_ANSIC */
  3187. {
  3188.     int i = 0, j = 0, k = 0;
  3189.     CHAR tmp[8];
  3190. #ifdef COMMENT
  3191.     int was_hex = 1;
  3192.  
  3193.     for (k=0; k < datalen; k++) {
  3194.         if (data[k] < 32 || data[k] >= 127) {
  3195.             sprintf(tmp,"%s%02X ",was_hex?"":"\" ",data[k]);
  3196.             was_hex = 1;
  3197.         } else {
  3198.             sprintf(tmp,"%s%c",was_hex?"\"":"",data[k]);
  3199.             was_hex = 0;
  3200.         }
  3201.         ckstrncat((char *)buf,tmp,buflen);
  3202.     }
  3203.     if (!was_hex)
  3204.         ckstrncat((char *)buf,"\" ",buflen);
  3205. #else /* COMMENT */
  3206.     if (datalen <= 0 || data == NULL)
  3207.         return(0);
  3208.  
  3209.     for (i = 0; i < datalen; i++) {
  3210.         ckstrncat((char *)buf,"\r\n  ",buflen);
  3211.         for (j = 0 ; (j < 16); j++) {
  3212.             if ((i + j) < datalen)
  3213.           sprintf((char *)tmp,
  3214.               "%s%02x ",
  3215.               (j == 8 ? "| " : ""),
  3216.               (char) data[i + j]
  3217.               );
  3218.             else
  3219.           sprintf((char *)tmp,
  3220.               "%s   ",
  3221.               (j == 8 ? "| " : "")
  3222.               );
  3223.             ckstrncat((char *)buf,(char *)tmp,buflen);
  3224.         }
  3225.         ckstrncat((char *)buf," ",buflen);
  3226.         for (k = 0; (k < 16) && ((i + k) < datalen); k++) {
  3227.             sprintf((char *)tmp,
  3228.                      "%s%c",
  3229.                      (k == 8 ? " " : ""),
  3230.                      isprint((char)(data[i+k])) ? data[i+k] : '.'
  3231.                      );
  3232.             ckstrncat((char *)buf,(char *)tmp,buflen);
  3233.         }
  3234.         i += j - 1;
  3235.     } /* end for */
  3236.     ckstrncat((char *)buf,"\r\n  ",buflen);
  3237. #endif /* COMMENT */
  3238.     return(strlen((char *)buf));
  3239. }
  3240.  
  3241. VOID
  3242. tn_debug(s) char *s; {
  3243. #ifdef NOLOCAL
  3244.     return;
  3245. #else /* NOLOCAL */
  3246. #ifdef OS2
  3247.     void cwrite(unsigned short);
  3248.     char *p = s;
  3249.     _PROTOTYP (void os2bold, (void));
  3250.     extern int tt_type_mode;
  3251. #endif /* OS2 */
  3252.  
  3253.     if (!(tn_deb || debses))
  3254.       return;
  3255.     debug(F111,"tn_debug",s,what);
  3256. #ifdef OS2
  3257.     if ( what == W_COMMAND || what == W_NOTHING ) {
  3258.         extern unsigned char colorcmd;
  3259.         colorcmd ^= 0x8 ;
  3260.         printf("%s\r\n",s);
  3261.         colorcmd ^= 0x8 ;
  3262.     }
  3263.     if (!scrninitialized[VTERM]) {
  3264.         USHORT x,y;
  3265.         checkscreenmode();
  3266.         GetCurPos(&y, &x);
  3267.         SaveCmdMode(x+1,y+1);
  3268.         scrninit();
  3269.         RestoreCmdMode();
  3270.     }
  3271.  
  3272.     if ( ISVTNT(tt_type_mode) && ttnum != -1 && !debses )
  3273.         return;
  3274.  
  3275.     RequestVscrnMutex( VTERM, SEM_INDEFINITE_WAIT ) ;
  3276.  
  3277.     os2bold();                          /* Toggle boldness */
  3278.     while (*p)
  3279.       cwrite((CHAR) *p++);              /* Go boldly ... */
  3280.     os2bold();                          /* Toggle boldness back */
  3281.     if (debses) {
  3282.         debses = 0;
  3283.         cwrite((CHAR) '\015');
  3284.         cwrite((CHAR) '\012');
  3285.         debses = 1;
  3286.     } else {
  3287.         cwrite((CHAR) '\015');
  3288.         cwrite((CHAR) '\012');
  3289.     }
  3290.     ReleaseVscrnMutex(VTERM) ;
  3291. #else
  3292.     if (what != W_CONNECT && what != W_COMMAND && what != W_NOTHING)
  3293.       return;                           /* CONNECT/command must be active */
  3294.     conoll(s);
  3295. #endif /* OS2 */
  3296. #endif /* NOLOCAL */
  3297. }
  3298.  
  3299. /*
  3300.   Process in-band Telnet negotiation characters from the remote host.
  3301.   Call with the telnet IAC character and the current duplex setting
  3302.   (0 = remote echo, 1 = local echo), and a pointer to a function to call
  3303.   to read more characters.  Returns:
  3304.     6 if DO LOGOUT was received and accepted
  3305.     5 if the Kermit start of packet character has changed
  3306.     4 if state of remote Internet Kermit Service has changed
  3307.     3 if a quoted IAC was received
  3308.     2 if local echo must be changed to remote
  3309.     1 if remote echo must be changed to local
  3310.     0 if nothing happens or no action necessary
  3311.    -1 on failure (= internal or i/o error)
  3312. */
  3313. #ifdef IKS_OPTION
  3314. int
  3315. tn_siks(cmd) int cmd; {         /* TELNET SEND IKS SUB */
  3316.     CHAR buf[8];
  3317. #ifndef NOXFER
  3318.     extern CHAR mystch;                 /* Outgoing Start of Packet Char */
  3319. #else
  3320.     CHAR mystch = '\1';
  3321. #endif /* NOXFER */
  3322.     int n,m;
  3323.  
  3324.     if (ttnet != NET_TCPB) return(0);   /* Must be TCP/IP */
  3325.     if (ttnproto != NP_TELNET) return(0); /* Must be telnet protocol */
  3326.     if (cmd < KERMIT_START || cmd > KERMIT_RESP_STOP) /* Illegal subcommand */
  3327.       return(-1);
  3328.  
  3329. #ifdef CK_SSL
  3330.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  3331.         return(0);
  3332.     }
  3333. #endif /* CK_SSL */
  3334.     if (cmd == KERMIT_START || cmd == KERMIT_RESP_START) {
  3335.         TELOPT_SB(TELOPT_KERMIT).kermit.me_start = 1;
  3336.     } else if (cmd == KERMIT_STOP || cmd == KERMIT_RESP_STOP) {
  3337.         TELOPT_SB(TELOPT_KERMIT).kermit.me_start = 0;
  3338.     } else if (cmd == KERMIT_REQ_STOP)
  3339.       TELOPT_SB(TELOPT_KERMIT).kermit.me_req_stop = 1;
  3340.     else if (cmd == KERMIT_REQ_START)
  3341.       TELOPT_SB(TELOPT_KERMIT).kermit.me_req_start = 1;
  3342.  
  3343.     if (cmd == KERMIT_SOP) {
  3344.         buf[0] = (CHAR) IAC;
  3345.         buf[1] = (CHAR) SB;
  3346.         buf[2] = (CHAR) TELOPT_KERMIT;
  3347.         buf[3] = (CHAR) (cmd & 0xff);
  3348.         buf[4] = (CHAR) mystch;
  3349.         buf[5] = (CHAR) IAC;
  3350.         buf[6] = (CHAR) SE;
  3351.         buf[7] = (CHAR) 0;
  3352.         if (tn_deb || debses || deblog) {
  3353.             ckmakmsg( tn_msg,TN_MSG_LEN,"TELNET SENT SB KERMIT SOP ",
  3354.                       ckctox(mystch,1)," IAC SE",NULL);
  3355.             debug(F101,tn_msg,"",cmd);
  3356.             if (tn_deb || debses) tn_debug(tn_msg);
  3357.         }
  3358.         if (ttol(buf,7) < 7)
  3359.           return(-1);
  3360.     } else {
  3361.         buf[0] = (CHAR) IAC;
  3362.         buf[1] = (CHAR) SB;
  3363.         buf[2] = (CHAR) TELOPT_KERMIT;
  3364.         buf[3] = (CHAR) (cmd & 0xff);
  3365.         buf[4] = (CHAR) IAC;
  3366.         buf[5] = (CHAR) SE;
  3367.         buf[6] = (CHAR) 0;
  3368.         if (tn_deb || debses || deblog) {
  3369.             char * s = 0;
  3370.             switch (cmd) {
  3371.               case KERMIT_START: s = "START"; break;
  3372.               case KERMIT_STOP: s = "STOP"; break;
  3373.               case KERMIT_REQ_START: s = "REQ-START"; break;
  3374.               case KERMIT_REQ_STOP: s = "REQ-STOP"; break;
  3375.               case KERMIT_RESP_START: s = "RESP-START"; break;
  3376.               case KERMIT_RESP_STOP:  s = "RESP-STOP"; break;
  3377.             }
  3378.             ckmakmsg( tn_msg,TN_MSG_LEN,
  3379.                       "TELNET SENT SB kermit ",s," IAC SE",NULL);
  3380.             debug(F101,tn_msg,"",cmd);
  3381.             if (tn_deb || debses) tn_debug(tn_msg);
  3382.         }
  3383.         if (ttol(buf,6) < 6)
  3384.           return(-1);
  3385.     }
  3386.     return(1);
  3387. }
  3388. #endif /* IKS_OPTION */
  3389.  
  3390. /* tn_sb() performs Telnet Subnegotiation Parsing and Debugging */
  3391. /* returns <= 0 on error, 1 on success */
  3392. /* the length returned includes the IAC SE bytes */
  3393.  
  3394. int
  3395. #ifdef CK_ANSIC                         /* TELNET SB */
  3396. tn_sb( int opt, int * len, int (*fn)(int) )
  3397. #else
  3398. tn_sb( opt, len, fn ) int opt; int * len; int (*fn)();
  3399. #endif /* CK_ANSIC */
  3400. /* tn_sb */ {
  3401.     int c, x, y, n, m, flag;
  3402.     debug(F100,"Entering tn_sb()","",0);
  3403.     *len = 0;                   /* Initialize Len to 0 */
  3404.     n = flag = 0;               /* Flag for when done reading SB */
  3405.     while (n < TSBUFSIZ) {      /* Loop looking for IAC SE */
  3406.         if ((y = (*fn)(0)) < 0) /* Read a byte */
  3407.           return(y);
  3408.         y &= 0xff;              /* Make sure it's just 8 bits. */
  3409.         sb[n++] = (char) y;     /* Deposit in buffer. */
  3410.         if (seslog && sessft == XYFT_D) { /* Take care of session log */
  3411.             logchar((char) y);
  3412.         }
  3413.         if (y == IAC) {         /* If this is an IAC                */
  3414.             if (flag) {         /* If previous char was IAC         */
  3415.                 n--;            /* it's quoted, keep one IAC        */
  3416.                 flag = 0;       /* and turn off the flag.           */
  3417.             } else flag = 1;    /* Otherwise set the flag.          */
  3418.         } else if (flag) {      /* Something else following IAC     */
  3419.             if (y == SE)        /* If not SE, it's a protocol error */
  3420.               break;
  3421.             else if (y == DONT) { /* Used DONT instead of SE */
  3422.                 debug(F100,
  3423.                       "TELNET Subnegotiation error - used DONT instead of SE!",
  3424.                       ""
  3425.                       ,0
  3426.                       );
  3427.                 if (tn_deb || debses)
  3428.                   tn_debug(
  3429.                      "TELNET Subnegotiation error - used DONT instead of SE!");
  3430.                 flag = 3;
  3431.                 break;
  3432.             } else {            /* Other protocol error */
  3433.                 flag = 0;
  3434.                 break;
  3435.             }
  3436.         }
  3437.  
  3438. #ifdef CK_FORWARD_X
  3439.         if ( opt == TELOPT_FORWARD_X && sb[0] == FWDX_DATA &&
  3440.              n == (TSBUFSIZ-4) && !flag ) {
  3441.             /* do not let the buffer over flow */
  3442.             /* write the data to the channel and continue processing */
  3443.             /* the incoming data until IAC SE is reached. */
  3444.             sb[n++] = IAC;
  3445.             sb[n++] = SE;
  3446.             if ( deblog || tn_deb || debses ) {
  3447.                 int i;
  3448.                 ckmakmsg( tn_msg,TN_MSG_LEN,
  3449.                           "TELNET RCVD SB ",TELOPT(opt)," DATA ",NULL);
  3450.                 tn_hex(tn_msg,TN_MSG_LEN,&sb[1],n-3);
  3451.                 if (flag == 2)
  3452.                     ckstrncat(tn_msg," SE",TN_MSG_LEN);
  3453.                 else if (flag == 3)
  3454.                     ckstrncat(tn_msg," IAC DONT",TN_MSG_LEN);
  3455.                 else
  3456.                     ckstrncat(tn_msg," IAC SE",TN_MSG_LEN);
  3457.                 debug(F100,tn_msg,"",0);
  3458.                 if (tn_deb || debses)
  3459.                     tn_debug(tn_msg);
  3460.             }
  3461.  
  3462.             if ( fwdx_tn_sb(sb,n) < 0 )
  3463.                 return(0);
  3464.  
  3465.             /* reset leave the msg type and channel number in place */
  3466.             n = 3;
  3467.         }
  3468. #endif /* CK_FORWARD_X */
  3469.     }
  3470.     debug(F111,"tn_sb end of while loop","flag",flag);
  3471.     if (!flag) {                        /* Make sure we got a valid SB */
  3472.         debug(F111, "TELNET Subnegotiation prematurely broken","opt",opt);
  3473.         if (tn_deb || debses)
  3474.           tn_debug("TELNET Subnegotiation prematurely broken");
  3475.         /* Was -1 but that would be an I/O error, so absorb it and go on. */
  3476.         return(0);
  3477.     }
  3478.     if (deblog || tn_deb || debses) {
  3479.         int i;
  3480.         char * s[16];
  3481.         for (i = 0; i < 16; i++)
  3482.           s[i] = "";
  3483.         if (opt == TELOPT_NAWS) {
  3484.             i = 0;
  3485.         } else {
  3486.             i = 1;
  3487.             s[0] = "UNKNOWN";
  3488.  
  3489.             switch (sb[0]) {
  3490.               case 0:
  3491.                 if (opt == TELOPT_FORWARD_X)
  3492.                   s[0] = "SCREEN";
  3493.                 else if (opt == TELOPT_KERMIT)
  3494.                   s[0] = "START";
  3495.                 else if (opt == TELOPT_LFLOW)
  3496.                   s[0] = "OFF";
  3497.                 else if (opt == TELOPT_COMPORT)
  3498.                   s[0] = "SIGNATURE";
  3499.                 else
  3500.                   s[0] = "IS";
  3501.                 if (opt == TELOPT_ENCRYPTION) {
  3502.                     i++;
  3503.                     if (sb[1] < ENCTYPE_CNT) {
  3504.                         s[1] = enctype_names[sb[1]];
  3505.                         i++;
  3506.                         switch(sb[2]) {
  3507.                           case 1:
  3508.                             s[2] = "FB64_IV";
  3509.                             break;
  3510.                           case 2:
  3511.                             s[2] = "FB64_IV_OK";
  3512.                             break;
  3513.                           case 3:
  3514.                             s[2] = "FB64_IV_BAD";
  3515.                             break;
  3516.                           case 4:
  3517.                             s[2] = "FB64_CHALLENGE";
  3518.                             break;
  3519.                           case 5:
  3520.                             s[2] = "FB64_RESPONSE";
  3521.                             break;
  3522.                         }
  3523.                     } else {
  3524.                         s[1] = "UNKNOWN";
  3525.                     }
  3526.                 }
  3527.                 if (opt == TELOPT_AUTHENTICATION) {
  3528.                     i += 2;
  3529.                     s[1] = AUTHTYPE_NAME(sb[1]);
  3530.                     s[2] = AUTHMODE_NAME(sb[2]);
  3531.                     if (sb[1]) {
  3532.                         i++;
  3533.                         switch (sb[3]) {
  3534.                           case 0:
  3535.                             switch (sb[1]) {
  3536.                               case AUTHTYPE_NTLM:
  3537.                                 s[3] = "NTLM_AUTH";
  3538.                                 break;
  3539.                               default:
  3540.                                 s[3] = "AUTH";
  3541.                             }
  3542.                             break;
  3543.                           case 1:
  3544.                             switch (sb[1]) {
  3545.                               case AUTHTYPE_SSL:
  3546.                                 s[3] = "START";
  3547.                                 break;
  3548.                               case AUTHTYPE_NTLM:
  3549.                                 s[3] = "NTLM_CHALLENGE";
  3550.                                 break;
  3551.                               default:
  3552.                                 s[3] = "REJECT";
  3553.                             }
  3554.                             break;
  3555.                           case 2:
  3556.                             switch (sb[1]) {
  3557.                               case AUTHTYPE_NTLM:
  3558.                                 s[3] = "NTLM_RESPONSE";
  3559.                                 break;
  3560.                               default:
  3561.                                 s[3] = "ACCEPT";
  3562.                             }
  3563.                             break;
  3564.                           case 3:
  3565.                             switch (sb[1]) {
  3566.                               case AUTHTYPE_NTLM:
  3567.                                 s[3] = "NTLM_ACCEPT";
  3568.                                 break;
  3569.                               case 1:   /* KERBEROS_v4 */
  3570.                               case 5:   /* SRP */
  3571.                                 s[3] = "CHALLENGE";
  3572.                                 break;
  3573.                               case 2:   /* KERBEROS_v5 */
  3574.                                 s[3] = "RESPONSE";
  3575.                                 break;
  3576.                               case AUTHTYPE_SSL:
  3577.                                 s[3] = "REJECT";
  3578.                                 break;
  3579.                             }
  3580.                             break;
  3581.                           case 4:
  3582.                             switch (sb[1]) {
  3583.                               case AUTHTYPE_NTLM:
  3584.                                 s[3] = "NTLM_REJECT";
  3585.                                 break;
  3586.                               case 1:   /* KERBEROS_V4 */
  3587.                               case 5:   /* SRP */
  3588.                                 s[3] = "RESPONSE";
  3589.                                 break;
  3590.                               case 2:   /* KERBEROS_V5 */
  3591.                                 s[3] = "FORWARD";
  3592.                                 break;
  3593.                             }
  3594.                             break;
  3595.                           case 5:
  3596.                             switch (sb[1]) {
  3597.                               case 5:   /* SRP */
  3598.                                 s[3] = "FORWARD";
  3599.                                 break;
  3600.                               case 2:   /* KERBEROS_V5 */
  3601.                                 s[3] = "FORWARD_ACCEPT";
  3602.                                 break;
  3603.                             }
  3604.                             break;
  3605.                           case 6:
  3606.                             switch (sb[1]) {
  3607.                               case 5:   /* SRP */
  3608.                                 s[3] = "FORWARD_ACCEPT";
  3609.                                 break;
  3610.                               case 2: /* KERBEROS_V5 */
  3611.                                 s[3] = "FORWARD_REJECT";
  3612.                                 break;
  3613.                             }
  3614.                             break;
  3615.                           case 7:
  3616.                             switch (sb[1]) {
  3617.                               case 5:   /* SRP */
  3618.                                 s[3] = "FORWARD_REJECT";
  3619.                                 break;
  3620.                               case 2: /* KERBEROS_V5 */
  3621.                                 s[3] = "TLS_VERIFY";
  3622.                                 break;
  3623.                               }
  3624.                             break;
  3625.                           case 8:
  3626.                             switch (sb[1]) {
  3627.                               case 5: /* SRP */
  3628.                                 s[3] = "EXP";
  3629.                                 break;
  3630.                             }
  3631.                             break;
  3632.                           case 9:
  3633.                             switch (sb[1]) {
  3634.                               case 5: /* SRP */
  3635.                                 s[3] = "PARAMS";
  3636.                                 break;
  3637.                             }
  3638.                             break;
  3639.                         }
  3640.                     }
  3641.                 }
  3642.                 break;
  3643.               case 1:
  3644.                 switch (opt) {
  3645.                   case TELOPT_FORWARD_X:
  3646.                     s[0] = "OPEN";
  3647.                     break;
  3648.                   case TELOPT_LFLOW:
  3649.                     s[0] = "ON";
  3650.                     break;
  3651.                   case TELOPT_KERMIT:
  3652.                     s[0] = "STOP";
  3653.                     break;
  3654.                   case TELOPT_COMPORT:
  3655.                     s[0] = "SET-BAUDRATE";
  3656.                       break;
  3657.                   case TELOPT_AUTHENTICATION:
  3658.                     s[0] = "SEND";
  3659.                     hexbuf[0] = '\0';
  3660.                     for (; i < n-2; i += 2) {
  3661.                         if ( AUTHTYPE_NAME_OK(sb[i]) &&
  3662.                              AUTHMODE_NAME_OK(sb[i]))
  3663.                             ckmakmsg( tn_msg, TN_MSG_LEN,
  3664.                                       AUTHTYPE_NAME(sb[i])," ",
  3665.                                       AUTHMODE_NAME(sb[i+1])," "
  3666.                                       );
  3667.                         else
  3668.               ckmakxmsg(tn_msg, TN_MSG_LEN,
  3669.                     AUTHTYPE_NAME(sb[i]),
  3670.                     "=",
  3671.                     ckitoa(sb[i]),
  3672.                     " ",
  3673.                     AUTHMODE_NAME(sb[i+1]),
  3674.                     "=",
  3675.                     ckitoa(sb[i+1]),
  3676.                     " ",
  3677.                     NULL,NULL,NULL,NULL
  3678.                     );
  3679.                         ckstrncat(hexbuf,tn_msg,sizeof(hexbuf));
  3680.                     }
  3681.                     s[1] = hexbuf;
  3682.                     break;
  3683.  
  3684.                   case TELOPT_ENCRYPTION:
  3685.                     s[0] = "SUPPORT";
  3686.                     while (i < n-2) {
  3687.                         s[i] = enctype_names[sb[i]];
  3688.                         i++;
  3689.                     }
  3690.                     break;
  3691.  
  3692.                   case TELOPT_START_TLS:
  3693.                     s[0] = "FOLLOWS";
  3694.                     break;
  3695.                   default:
  3696.                     s[0] = "SEND";
  3697.                 }
  3698.                 break;
  3699.  
  3700.               case 2:
  3701.                 switch (opt) {
  3702.                 case TELOPT_FORWARD_X:
  3703.                     s[0] = "CLOSE";
  3704.                     break;
  3705.                   case TELOPT_LFLOW:
  3706.                     s[0] = "RESTART-ANY";
  3707.                     break;
  3708.                   case TELOPT_KERMIT:
  3709.                     s[0] = "REQ-START";
  3710.                     break;
  3711.                   case TELOPT_COMPORT:
  3712.                     s[0] = "SET-DATASIZE";
  3713.                     break;
  3714.                   case TELOPT_NEWENVIRON:
  3715.                     s[0] = "INFO";
  3716.                     break;
  3717.                   case TELOPT_AUTHENTICATION:
  3718.                     s[0] = "REPLY";
  3719.                     i=4;
  3720.                     s[1] = AUTHTYPE_NAME(sb[1]);
  3721.                     s[2] = AUTHMODE_NAME(sb[2]);
  3722.                     switch (sb[3]) {
  3723.                       case 0:
  3724.                         switch (sb[1]) {
  3725.                           case AUTHTYPE_NTLM:
  3726.                             s[3] = "NTLM_AUTH";
  3727.                             break;
  3728.                           default:
  3729.                             s[3] = "AUTH";
  3730.                         }
  3731.                         break;
  3732.                       case 1:
  3733.                         switch (sb[1]) {
  3734.                           case AUTHTYPE_NTLM:
  3735.                             s[3] = "NTLM_CHALLENGE";
  3736.                             break;
  3737.                           default:
  3738.                             s[3] = "REJECT";
  3739.                         }
  3740.                         break;
  3741.                       case 2:
  3742.                         switch (sb[1]) {
  3743.                           case AUTHTYPE_NTLM:
  3744.                             s[3] = "NTLM_RESPONSE";
  3745.                             break;
  3746.                           default:
  3747.                             s[3] = "ACCEPT";
  3748.                         }
  3749.                         break;
  3750.                       case 3:
  3751.                         switch (sb[1]) {
  3752.                           case AUTHTYPE_NTLM:
  3753.                             s[3] = "NTLM_ACCEPT";
  3754.                             break;
  3755.                           case AUTHTYPE_KERBEROS_V4:
  3756.                           case AUTHTYPE_SRP:
  3757.                             s[3] = "CHALLENGE";
  3758.                             break;
  3759.                           case AUTHTYPE_KERBEROS_V5:
  3760.                             s[3] = "RESPONSE";
  3761.                             break;
  3762.                         }
  3763.                         break;
  3764.                       case 4:
  3765.                         switch (sb[1]) {
  3766.                           case AUTHTYPE_NTLM:
  3767.                             s[3] = "NTLM_REJECT";
  3768.                             break;
  3769.                           case AUTHTYPE_KERBEROS_V4:
  3770.                           case AUTHTYPE_SRP:
  3771.                             s[3] = "RESPONSE";
  3772.                             break;
  3773.                           case AUTHTYPE_KERBEROS_V5:
  3774.                             s[3] = "FORWARD";
  3775.                             break;
  3776.                         }
  3777.                         break;
  3778.                       case 5:
  3779.                         switch (sb[1]) {
  3780.                           case AUTHTYPE_SRP:
  3781.                             s[3] = "FORWARD";
  3782.                             break;
  3783.                           case AUTHTYPE_KERBEROS_V5:
  3784.                             s[3] = "FORWARD_ACCEPT";
  3785.                             break;
  3786.                         }
  3787.                         break;
  3788.                       case 6:
  3789.                         switch (sb[1]) {
  3790.                           case AUTHTYPE_SRP:
  3791.                             s[3] = "FORWARD_ACCEPT";
  3792.                             break;
  3793.                           case AUTHTYPE_KERBEROS_V5:
  3794.                             s[3] = "FORWARD_REJECT";
  3795.                             break;
  3796.                         }
  3797.                         break;
  3798.                       case 7:
  3799.                         switch (sb[1]) {
  3800.                           case AUTHTYPE_SRP:
  3801.                             s[3] = "FORWARD_REJECT";
  3802.                             break;
  3803.                           case AUTHTYPE_KERBEROS_V5:
  3804.                             s[3] = "TLS_VERIFY";
  3805.                             break;
  3806.                         }
  3807.                         break;
  3808.                       case 8:
  3809.                         switch (sb[1]) {
  3810.                           case AUTHTYPE_SRP:
  3811.                             s[3] = "EXP";
  3812.                             break;
  3813.                         }
  3814.                         break;
  3815.                       case 9:
  3816.                         switch (sb[1]) {
  3817.                           case AUTHTYPE_SRP:
  3818.                             s[3] = "PARAMS";
  3819.                             break;
  3820.                         }
  3821.                         break;
  3822.                     }
  3823.                     break;
  3824.                   case TELOPT_ENCRYPTION:
  3825.                     s[0] = "REPLY";
  3826.                     s[1] = enctype_names[sb[1]];
  3827.                     i++;
  3828.                     switch (sb[2]) {
  3829.                       case 1:
  3830.                         i++;
  3831.                         s[2] = "FB64_IV";
  3832.                         break;
  3833.                       case 2:
  3834.                         i++;
  3835.                         s[2] = "FB64_IV_OK";
  3836.                         break;
  3837.                       case 3:
  3838.                         i++;
  3839.                         s[2] = "FB64_IV_BAD";
  3840.                         break;
  3841.                       case 4:
  3842.                         i++;
  3843.                         s[2] = "FB64_CHALLENGE";
  3844.                         break;
  3845.                       case 5:
  3846.                         i++;
  3847.                         s[2] = "FB64_RESPONSE";
  3848.                         break;
  3849.                     }
  3850.                     break;
  3851.                 }
  3852.                 break;
  3853.               case 3:
  3854.                 switch (opt) {
  3855.                   case TELOPT_FORWARD_X:
  3856.                     s[0] = "DATA";
  3857.                     break;
  3858.                   case TELOPT_LFLOW:
  3859.                     s[0] = "RESTART-XON";
  3860.                     break;
  3861.                   case TELOPT_KERMIT:
  3862.                     s[0] = "REQ-STOP";
  3863.                     break;
  3864.                   case TELOPT_COMPORT:
  3865.                       s[0] = "SET-PARITY";
  3866.                       break;
  3867.                   case TELOPT_AUTHENTICATION:
  3868.                     s[0] = "NAME";
  3869.                     break;
  3870.                   case TELOPT_ENCRYPTION:
  3871.                     s[0] = "START";
  3872.                     break;
  3873.                 }
  3874.                 break;
  3875.               case 4:
  3876.                 switch (opt) {
  3877.                   case TELOPT_FORWARD_X:
  3878.                     s[0] = "OPTIONS";
  3879.                     break;
  3880.                   case TELOPT_KERMIT:
  3881.                     s[0] = "SOP";
  3882.                     break;
  3883.                   case TELOPT_COMPORT:
  3884.                       s[0] = "SET-STOPSIZE";
  3885.                       break;
  3886.                   case TELOPT_ENCRYPTION:
  3887.                     s[0] = "END";
  3888.                     break;
  3889.                 }
  3890.                 break;
  3891.               case 5:
  3892.                 switch (opt) {
  3893.                 case TELOPT_FORWARD_X:
  3894.                     s[0] = "OPTION_DATA";
  3895.                     break;
  3896.                 case TELOPT_ENCRYPTION:
  3897.                     s[0] = "REQUEST-START";
  3898.                     break;
  3899.                 case TELOPT_COMPORT:
  3900.                     s[0] = "SET-CONTROL";
  3901.                     break;
  3902.                 }
  3903.                 break;
  3904.               case 6:
  3905.                 switch (opt) {
  3906.                   case TELOPT_FORWARD_X:
  3907.                     s[0] = "XOFF";
  3908.                     break;
  3909.                   case TELOPT_ENCRYPTION:
  3910.                     s[0] = "REQUEST-END";
  3911.                     break;
  3912.                   case TELOPT_COMPORT:
  3913.                       s[0] = "NOTIFY-LINESTATE";
  3914.                       break;
  3915.                   }
  3916.                 break;
  3917.               case 7:
  3918.                 switch (opt) {
  3919.                   case TELOPT_FORWARD_X:
  3920.                     s[0] = "XON";
  3921.                     break;
  3922.                   case TELOPT_ENCRYPTION:
  3923.                     s[0] = "ENC-KEYID";
  3924.                     break;
  3925.                   case TELOPT_COMPORT:
  3926.                       s[0] = "NOTIFY-MODEMSTATE";
  3927.                       break;
  3928.                   }
  3929.                 break;
  3930.               case 8:
  3931.                 switch (opt) {
  3932.                   case TELOPT_KERMIT:
  3933.                     s[0] = "RESP-START";
  3934.                     break;
  3935.                   case TELOPT_ENCRYPTION:
  3936.                     s[0] = "DEC-KEYID";
  3937.                     break;
  3938.                   case TELOPT_COMPORT:
  3939.                       s[0] = "FLOWCONTROL-SUSPEND";
  3940.                       break;
  3941.                   }
  3942.                 break;
  3943.               case 9:
  3944.                 switch (opt) {
  3945.                   case TELOPT_KERMIT:
  3946.                     s[0] = "RESP-STOP";
  3947.                     break;
  3948.                   case TELOPT_COMPORT:
  3949.                       s[0] = "FLOWCONTROL-RESUME";
  3950.                       break;
  3951.                   }
  3952.                 break;
  3953.               case 10:
  3954.                   switch (opt) {
  3955.                   case TELOPT_COMPORT:
  3956.                     s[0] = "SET-LINESTATE-MASK";
  3957.                     break;
  3958.                   }
  3959.                   break;
  3960.             case 11:
  3961.                   switch (opt) {
  3962.                   case TELOPT_COMPORT:
  3963.                       s[0] = "SET-MODEMSTATE-MASK";
  3964.                       break;
  3965.                   }
  3966.                   break;
  3967.             case 12:
  3968.                   switch (opt) {
  3969.                   case TELOPT_COMPORT:
  3970.                       s[0] = "PURGE-DATA";
  3971.                       break;
  3972.                   }
  3973.                   break;
  3974.  
  3975.  
  3976.             case 100:
  3977.                   switch (opt) {
  3978.                   case TELOPT_COMPORT:
  3979.                       s[0] = "S_SIGNATURE";
  3980.                       break;
  3981.                   }
  3982.                   break;
  3983.             case 101:
  3984.                   switch (opt) {
  3985.                   case TELOPT_COMPORT:
  3986.                       s[0] = "S_SET-BAUDRATE";
  3987.                       break;
  3988.                   }
  3989.                   break;
  3990.             case 102:
  3991.                   switch (opt) {
  3992.                   case TELOPT_COMPORT:
  3993.                       s[0] = "S_SET-DATASIZE";
  3994.                       break;
  3995.                   }
  3996.                   break;
  3997.             case 103:
  3998.                   switch (opt) {
  3999.                   case TELOPT_COMPORT:
  4000.                       s[0] = "S_SET-PARITY";
  4001.                       break;
  4002.                   }
  4003.                   break;
  4004.             case 104:
  4005.                   switch (opt) {
  4006.                   case TELOPT_COMPORT:
  4007.                       s[0] = "S_SET-STOPSIZE";
  4008.                       break;
  4009.                   }
  4010.                   break;
  4011.             case 105:
  4012.                   switch (opt) {
  4013.                   case TELOPT_COMPORT:
  4014.                       s[0] = "S_SET-CONTROL";
  4015.                       break;
  4016.                   }
  4017.                   break;
  4018.             case 106:
  4019.                   switch (opt) {
  4020.                   case TELOPT_COMPORT:
  4021.                       s[0] = "S_NOTIFY-LINESTATE";
  4022.                       break;
  4023.                   }
  4024.                   break;
  4025.             case 107:
  4026.                   switch (opt) {
  4027.                   case TELOPT_COMPORT:
  4028.                       s[0] = "S_NOTIFY-MODEMSTATE";
  4029.                       break;
  4030.                   }
  4031.                   break;
  4032.             case 108:
  4033.                   switch (opt) {
  4034.                   case TELOPT_COMPORT:
  4035.                       s[0] = "S_FLOWCONTROL-SUSPEND";
  4036.                       break;
  4037.                   }
  4038.                   break;
  4039.             case 109:
  4040.                   switch (opt) {
  4041.                   case TELOPT_COMPORT:
  4042.                       s[0] = "S_FLOWCONTROL-RESUME";
  4043.                       break;
  4044.                   }
  4045.                   break;
  4046.             case 110:
  4047.                   switch (opt) {
  4048.                   case TELOPT_COMPORT:
  4049.                       s[0] = "S_SET-LINESTATE-MASK";
  4050.                       break;
  4051.                   }
  4052.                   break;
  4053.             case 111:
  4054.                   switch (opt) {
  4055.                   case TELOPT_COMPORT:
  4056.                       s[0] = "S_SET-MODEMSTATE-MASK";
  4057.                       break;
  4058.                   }
  4059.                   break;
  4060.             case 112:
  4061.                   switch (opt) {
  4062.                   case TELOPT_COMPORT:
  4063.                       s[0] = "S_PURGE-DATA";
  4064.                       break;
  4065.                   }
  4066.                   break;
  4067.               }
  4068.         }
  4069. #ifdef M_XENIX
  4070.         {
  4071.           int len, param, param_len;
  4072.           ckmakmsg( tn_msg, TN_MSG_LEN,
  4073.                     "TELNET RCVD SB ",
  4074.                     TELOPT(opt)," ",NULL);
  4075.           len = strlen(tn_msg);
  4076.           for (param = 0; param <= 15; param++) {
  4077.             param_len = strlen(s[param]);
  4078.             if (param_len > 0) {
  4079.               strcpy(&tn_msg[len], s[param]);
  4080.               len += param_len;
  4081.               tn_msg[len++] = ' ';
  4082.             }
  4083.           }
  4084.           tn_msg[len] = '\0';
  4085.         }
  4086. #else /* M_XENIX */
  4087.         ckmakxmsg(tn_msg,TN_MSG_LEN,"TELNET RCVD SB ",TELOPT(opt)," ",
  4088.           NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  4089.         {
  4090.             int i;
  4091.             for (i = 0; i <= 15; i++) {
  4092.                 if (s[i][0]) {
  4093.                     ckstrncat(tn_msg,s[i],TN_MSG_LEN);
  4094.                     ckstrncat(tn_msg," ",TN_MSG_LEN);
  4095.                 }
  4096.             }
  4097.         }
  4098. #endif /* M_XENIX */
  4099.         tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&sb[i],n-2-i);
  4100.         if (flag == 2)
  4101.           ckstrncat(tn_msg," SE",TN_MSG_LEN);
  4102.         else if (flag == 3)
  4103.           ckstrncat(tn_msg," IAC DONT",TN_MSG_LEN);
  4104.         else
  4105.           ckstrncat(tn_msg," IAC SE",TN_MSG_LEN);
  4106.         debug(F100,tn_msg,"",0);
  4107.         if (tn_deb || debses)
  4108.           tn_debug(tn_msg);
  4109.     }
  4110.     *len = n;           /* return length */
  4111.     return(1);          /* success */
  4112. }
  4113.  
  4114. static char rows_buf[16] = { 0, 0 }; /* LINES Environment variable */
  4115. static char cols_buf[16] = { 0, 0 }; /* COLUMNS Enviornment variable */
  4116. static char term_buf[64] = { 0, 0 }; /* TERM Environment variable */
  4117.  
  4118. #ifdef CK_CURSES
  4119. #ifndef VMS
  4120. #ifndef COHERENT
  4121. _PROTOTYP(int tgetent,(char *, char *));
  4122. #endif /* COHERENT */
  4123. #else
  4124. #ifdef __DECC
  4125. _PROTOTYP(int tgetent,(char *, char *));
  4126. #endif /* __DECC */
  4127. #endif /* VMS */
  4128. extern char * trmbuf;                   /* Real curses */
  4129. #endif /* CK_CURSES */
  4130.  
  4131. #ifdef CK_ENCRYPTION
  4132. static int
  4133. tn_no_encrypt()
  4134. {
  4135.     /* Prevent Encryption from being negotiated */
  4136.     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  4137.     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  4138.  
  4139.     /* Cancel any negotiation that might have started */
  4140.     ck_tn_enc_stop();
  4141.  
  4142.     if (TELOPT_ME(TELOPT_ENCRYPTION) ||
  4143.          TELOPT_UNANSWERED_WILL(TELOPT_ENCRYPTION)) {
  4144.         TELOPT_ME(TELOPT_ENCRYPTION) = 0;
  4145.         if (tn_sopt(WONT,TELOPT_ENCRYPTION) < 0)
  4146.             return(-1);
  4147.         TELOPT_UNANSWERED_WONT(TELOPT_ENCRYPTION) = 1;
  4148.     }
  4149.     if (TELOPT_U(TELOPT_ENCRYPTION) ||
  4150.          TELOPT_UNANSWERED_DO(TELOPT_ENCRYPTION)) {
  4151.         TELOPT_U(TELOPT_ENCRYPTION) = 0;
  4152.         if (tn_sopt(DONT,TELOPT_ENCRYPTION) < 0)
  4153.             return(-1);
  4154.         TELOPT_UNANSWERED_DONT(TELOPT_ENCRYPTION) = 1;
  4155.     }
  4156.     return(0);
  4157. }
  4158. #endif /* CK_ENCRYPTION */
  4159.  
  4160. /* The following note came from the old SGA negotiation code.  This should */
  4161. /* no longer be necessary with the New Telnet negotiation state machine.   */
  4162. /*
  4163.   Note: The following is proper behavior, and required for talking to the
  4164.   Apertus interface to the NOTIS library system, e.g. at Iowa State U:
  4165.   scholar.iastate.edu.  Without this reply, the server hangs forever.  This
  4166.   code should not be loop-inducing, since C-Kermit never sends WILL SGA as
  4167.   an initial bid, so if DO SGA comes, it is never an ACK.
  4168. */
  4169. /*
  4170.   Return values:
  4171.   -1 = Telnet Opton negotiation error
  4172.   -2 = Connection closed by peer
  4173.   -3 = Connection closed by us
  4174.   0  = Success
  4175.   1  = Echoing on
  4176.   2  = Echoing off
  4177.   3  = Quoted IAC
  4178.   4  = IKS Event
  4179.   5  = (unassigned)
  4180.   6  = Logout
  4181. */
  4182.  
  4183. static int
  4184. #ifdef CK_ANSIC                         /* TELNET DO OPTION */
  4185. tn_xdoop(CHAR z, int echo, int (*fn)(int))
  4186. #else
  4187. tn_xdoop(z, echo, fn) CHAR z; int echo; int (*fn)();
  4188. #endif /* CK_ANSIC */
  4189. /* tn_xdoop */ {
  4190.     int c, x, y, n, m;
  4191. #ifdef IKS_OPTION
  4192.     extern int server;
  4193. #ifdef NOICP
  4194.     extern int autodl;
  4195.     int inautodl = 0, cmdadl = 1;
  4196. #else
  4197. #ifdef CK_AUTODL
  4198.     extern int autodl, inautodl, cmdadl;
  4199. #endif /* CK_AUTODL */
  4200. #endif /* NOICP */
  4201. #endif /* IKS_OPTION */
  4202.  
  4203.  
  4204. /* Have IAC, read command character. */
  4205.  
  4206.     while ((c = (*fn)(0)) == -1);       /* Read command character */
  4207.     if (c < 0)
  4208.       return(c);
  4209.     c &= 0xFF;                          /* Strip high bits */
  4210.  
  4211.     if (!TELCMD_OK(c)) {
  4212.         ckmakmsg(tn_msg,TN_MSG_LEN,"TELNET RCVD UNKNOWN (",ckitoa(c),")",NULL);
  4213.         debug(F101,tn_msg,"",c);
  4214.         if (tn_deb || debses) tn_debug(tn_msg);
  4215.         return(0);
  4216.     }
  4217.     if (ttnproto == NP_NONE) {
  4218.         debug(F100,"tn_doop discovered a Telnet command",
  4219.               "ttnproto = NP_TELNET",0);
  4220.         ttnproto = NP_TELNET;
  4221.     }
  4222.     if (seslog && sessft == XYFT_D) {   /* Copy to session log, if any. */
  4223.         logchar((char)z);
  4224.         logchar((char)c);
  4225.     }
  4226.  
  4227.     if (c == (CHAR) IAC)                /* Quoted IAC */
  4228.       return(3);
  4229.  
  4230.     if (c < SB) {                       /* Other command with no arguments. */
  4231.         if (deblog || tn_deb || debses) {
  4232.             ckmakmsg(tn_msg,TN_MSG_LEN,"TELNET RCVD ",TELCMD(c),NULL,NULL);
  4233.             debug(F101,tn_msg,"",c);
  4234.             if (tn_deb || debses) tn_debug(tn_msg);
  4235.         }
  4236.         switch (c) {                    /* What we would like to do here    */
  4237.           case TN_GA:                   /* Is substitute ASCII characters   */
  4238.             break;                      /* for the Telnet Command so that   */
  4239.           case TN_EL:                   /* the command may be processed by  */
  4240.             break;                      /* either the internal emulator or  */
  4241.           case TN_EC:                   /* by the superior process or shell */
  4242.             break;
  4243.           case TN_AYT:
  4244.             ttol((CHAR *)"[Yes]\015\012",7);
  4245.             break;
  4246.           case TN_AO:
  4247. #ifdef BETADEBUG
  4248.             bleep(BP_NOTE);
  4249. #endif /* BETADEBUG */
  4250.             break;
  4251.           case TN_IP:
  4252.             break;
  4253.           case BREAK:
  4254.             break;
  4255.           case TN_DM:
  4256.             break;
  4257.           case TN_NOP:
  4258.             break;
  4259.           case SE:
  4260.             break;
  4261.           case TN_EOR:
  4262.             break;
  4263.           case TN_ABORT:
  4264.             break;
  4265.           case TN_SUSP:
  4266.             break;
  4267.           case TN_EOF:
  4268.             break;
  4269.           case TN_SAK:
  4270.             break;
  4271.         }
  4272.         return(0);
  4273.     }
  4274.  
  4275. /* SB, WILL, WONT, DO, or DONT need more bytes... */
  4276.  
  4277.     if ((x = (*fn)(0)) < 0)             /* Get the option. */
  4278.       return(x);
  4279.     x &= 0xff;                          /* Trim to 8 bits. */
  4280.  
  4281.     if (seslog && sessft == XYFT_D) {   /* Session log */
  4282.         logchar((char) x);
  4283.     }
  4284.     if ((deblog || tn_deb || debses) && c != SB) {
  4285.         ckmakmsg(tn_msg,TN_MSG_LEN,"TELNET RCVD ",TELCMD(c)," ",TELOPT(x));
  4286.         debug(F101,tn_msg,"",x);
  4287.         if (tn_deb || debses) tn_debug(tn_msg);
  4288.     }
  4289.  
  4290.     /* Now handle the command */
  4291.     switch (c) {
  4292.       case WILL:
  4293. #ifdef CK_SSL
  4294.         if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  4295.             return(0);
  4296. #endif /* CK_SSL */
  4297. #ifdef CK_FORWARD_X
  4298.           if (x == TELOPT_FORWARD_X) {
  4299.               if (!fwdx_server_avail() || !(
  4300. #ifdef CK_SSL
  4301.                  (ssl_active_flag || tls_active_flag)
  4302. #else /* CK_SSL */
  4303.                  0
  4304. #endif /* CK_SSL */
  4305.                  ||
  4306. #ifdef CK_ENCRYPTION
  4307.                  (ck_tn_encrypting() && ck_tn_decrypting())
  4308. #else /* CK_ENCRYPTION */
  4309.                  0
  4310. #endif /* CK_ENCRYPTION */
  4311.                  )) {
  4312.                   TELOPT_U_MODE(TELOPT_FORWARD_X) = TN_NG_RF;
  4313.                   TELOPT_ME_MODE(TELOPT_FORWARD_X) = TN_NG_RF;
  4314.               }
  4315.           }
  4316. #endif /* CK_FORWARD_X */
  4317.         if (!TELOPT_OK(x) || TELOPT_U_MODE(x) == TN_NG_RF) {
  4318.             if (tn_sopt(DONT,x) < 0)
  4319.               return(-1);
  4320.             if (TELOPT_UNANSWERED_DO(x))
  4321.                 TELOPT_UNANSWERED_DO(x) = 0;
  4322.         } else if (!TELOPT_U(x)) {
  4323.             if (!TELOPT_UNANSWERED_DO(x)) {
  4324.                 if (tn_sopt(DO,x) < 0)
  4325.                   return -1;
  4326.             }
  4327.             if (TELOPT_UNANSWERED_DO(x))
  4328.                 TELOPT_UNANSWERED_DO(x) = 0;
  4329.             TELOPT_U(x) = 1;
  4330.  
  4331.             switch (x) {
  4332. #ifdef CK_SSL
  4333.               case TELOPT_START_TLS:
  4334.                 /*
  4335.                    If my proposal is accepted, at this point the Telnet
  4336.                    protocol is turned off and a TLS negotiation takes
  4337.                    place.
  4338.  
  4339.                    Start by sending SB START_TLS FOLLOWS  to signal
  4340.                    we are ready.  Wait for the peer to send the same
  4341.                    and then start the TLS negotiation.
  4342.  
  4343.                    If the TLS negotiation succeeds we call tn_ini()
  4344.                    again to reset the telnet state machine and restart
  4345.                    the negotiation process over the now secure link.
  4346.  
  4347.                    If the TLS negotiation fails, we call ttclos()
  4348.                    to terminate the connection.
  4349.  
  4350.                    Only the server should receive a WILL START_TLS
  4351.                  */
  4352.                 tn_ssbopt(TELOPT_START_TLS,1,NULL,0);
  4353.                 TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows = 1;
  4354.                 break;
  4355. #endif /* CK_SSL */
  4356.  
  4357. #ifdef CK_AUTHENTICATION
  4358.               case TELOPT_AUTHENTICATION: {
  4359.                   /* We now have to perform a SB SEND to identify the  */
  4360.                   /* supported authentication types to the other side. */
  4361.                   extern int authentication_version;
  4362.                   authentication_version = AUTHTYPE_AUTO;
  4363.                   ck_tn_auth_request();
  4364.                   break;
  4365.               }
  4366. #endif /* CK_AUTHENTICATION */
  4367. #ifdef CK_ENCRYPTION
  4368.               case TELOPT_ENCRYPTION:
  4369.                 if (!(TELOPT_ME(TELOPT_AUTHENTICATION) ||
  4370.                       TELOPT_U(TELOPT_AUTHENTICATION))
  4371.                     ) {
  4372.                     if (tn_sopt(DONT,x) < 0)
  4373.                       return(-1);
  4374.                     TELOPT_U(x) = 0;
  4375.                 } else {
  4376.                     if (ck_tn_auth_in_progress()) {
  4377.                         TELOPT_SB(TELOPT_ENCRYPTION).encrypt.need_to_send = 1;
  4378.                     } else {
  4379.                         /* Perform subnegotiation */
  4380.                         ck_encrypt_send_support();
  4381.                     }
  4382.                     if (!(TELOPT_ME(x) || TELOPT_UNANSWERED_WILL(x))
  4383.                         && TELOPT_ME_MODE(x) != TN_NG_RF) {
  4384.                         if (tn_sopt(WILL, x) < 0)
  4385.                           return(-1);
  4386.                         TELOPT_UNANSWERED_WILL(x) = 1;
  4387.                     }
  4388.                 }
  4389.                 break;
  4390. #endif /* CK_ENCRYPTION */
  4391. #ifdef IKS_OPTION
  4392.               case TELOPT_KERMIT:
  4393.                 if (!TELOPT_ME(x)) {
  4394.                     /* Tell the other side what Start of Packet Character */
  4395.                     tn_siks(KERMIT_SOP); /* SOP */
  4396.  
  4397.                     if (!TELOPT_UNANSWERED_WILL(x) &&
  4398.                         TELOPT_ME_MODE(x) != TN_NG_RF) {
  4399.                         if (tn_sopt(WILL, x) < 0)
  4400.                           return(-1);
  4401.                         TELOPT_UNANSWERED_WILL(x) = 1;
  4402.                     }
  4403.                 }
  4404.                 break;
  4405. #endif /* IKS_OPTION */
  4406.               case TELOPT_BINARY:
  4407.                 if (!TELOPT_ME(x)) {
  4408.                     if (!TELOPT_UNANSWERED_WILL(x) &&
  4409.                         TELOPT_ME_MODE(x) >= TN_NG_RQ) {
  4410.                         if (tn_sopt(WILL, x) < 0)
  4411.                           return(-1);
  4412.                         TELOPT_UNANSWERED_WILL(x) = 1;
  4413.                     }
  4414.                 }
  4415.                 break;
  4416.               case TELOPT_ECHO:
  4417.                 if (echo) {
  4418.                     if (TELOPT_UNANSWERED_DO(x))
  4419.                       TELOPT_UNANSWERED_DO(x) = 0;
  4420.                     return(2);
  4421.                 }
  4422.                 break;
  4423.               case TELOPT_TTYPE:
  4424.                 /* SB TTYPE SEND */
  4425.                 tn_ssbopt(TELOPT_TTYPE,TELQUAL_SEND,NULL,0);
  4426.                 TELOPT_UNANSWERED_SB(TELOPT_TTYPE)=1;
  4427.                 break;
  4428. #ifdef CK_ENVIRONMENT
  4429.               case TELOPT_NEWENVIRON:   /* SB NEW-ENVIRON SEND */
  4430.         {
  4431.                   char request[6];      /* request it */
  4432.           sprintf(request,"%cUSER",TEL_ENV_VAR);        /* safe */
  4433.                   tn_ssbopt(TELOPT_NEWENVIRON,TELQUAL_SEND,request,
  4434.                             strlen(request));
  4435.                   TELOPT_UNANSWERED_SB(TELOPT_NEWENVIRON)=1;
  4436.                 }
  4437.                 break;
  4438. #endif /* CK_ENVIRONMENT */
  4439.             } /* switch */
  4440.         } else {
  4441.             if (TELOPT_UNANSWERED_DO(x))
  4442.                 TELOPT_UNANSWERED_DO(x) = 0;
  4443.         }
  4444.         break;
  4445.       case WONT:
  4446. #ifdef CK_SSL
  4447.         if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  4448.             return(0);
  4449. #endif /* CK_SSL */
  4450.         if (!TELOPT_OK(x) || TELOPT_U(x) || TELOPT_UNANSWERED_DO(x)) {
  4451.             /* David Borman says we should not respond DONT when */
  4452.             /* the WONT is a response to a DO that we sent.   */
  4453.             if (!(TELOPT_UNANSWERED_DO(x) || TELOPT_UNANSWERED_DONT(x)))
  4454.               if (tn_sopt(DONT,x) < 0)
  4455.                 return(-1);
  4456.             if (TELOPT_UNANSWERED_DONT(x))
  4457.                 TELOPT_UNANSWERED_DONT(x) = 0;
  4458.             if (TELOPT_UNANSWERED_DO(x))
  4459.                 TELOPT_UNANSWERED_DO(x) = 0;
  4460.             if (TELOPT_U(x)) {
  4461.                 TELOPT_U(x) = 0;
  4462.             }
  4463.             switch(x) {
  4464. #ifdef CK_SSL
  4465.             case TELOPT_START_TLS:
  4466.                 if (sstelnet && TELOPT_U_MODE(x) == TN_NG_MU) {
  4467.                     printf("Telnet Start-TLS refused.\n");
  4468.                     ttclos(0);
  4469.                     whyclosed = WC_TELOPT;
  4470.                     return(-3);
  4471.                 }
  4472.                 break;
  4473. #endif /* CK_SSL */
  4474. #ifdef CK_AUTHENTICATION
  4475.               case TELOPT_AUTHENTICATION:
  4476.                 if (sstelnet && TELOPT_U_MODE(x) == TN_NG_MU) {
  4477.                     printf("Telnet authentication refused.\n");
  4478.                     ttclos(0);
  4479.                     whyclosed = WC_TELOPT;
  4480.                     return(-3);
  4481.                 } else if (TELOPT_U_MODE(x) == TN_NG_RQ) {
  4482.                     TELOPT_U_MODE(x) = TN_NG_AC;
  4483.                 }
  4484.                 if (ck_tn_auth_in_progress())
  4485.                   printf("Telnet authentication refused.\n");
  4486. #ifdef CK_ENCRYPTION
  4487.                 if (sstelnet) {
  4488.                     if (tn_no_encrypt()<0)
  4489.                         return(-1);
  4490.                 }
  4491. #endif /* CK_ENCRYPTION */
  4492.                 break;
  4493. #endif /* CK_AUTHENTICATION */
  4494. #ifdef CK_ENCRYPTION
  4495.               case TELOPT_ENCRYPTION:
  4496.                 ck_tn_enc_stop();
  4497.                 break;
  4498. #endif /* CK_ENCRYPTION */
  4499. #ifdef IKS_OPTION
  4500.               case TELOPT_KERMIT:
  4501.                 TELOPT_SB(x).kermit.u_start = 0;
  4502.                 TELOPT_SB(x).kermit.me_req_start = 0;
  4503.                 TELOPT_SB(x).kermit.me_req_stop = 0;
  4504.                 break;
  4505. #endif /* IKS_OPTION */
  4506.               case TELOPT_NAWS: {
  4507.                   /* The client does not support NAWS. */
  4508.                   /* Assume a height of 24 and a width of 80 */
  4509.                   if (sstelnet
  4510. #ifdef IKSD
  4511.                    || inserver
  4512. #endif /* IKSD */
  4513.                    ) {
  4514.                       int w = 80, h = 24;
  4515. #ifndef NOLOCAL
  4516.                       if (tcp_incoming) {
  4517. #ifdef OS2
  4518.                           tt_cols[VTERM] = w;
  4519.                           tt_rows[VTERM] = h;
  4520.                           VscrnSetWidth(VTERM, w);
  4521.                           VscrnSetHeight(VTERM, h+(tt_status[VTERM]?1:0));
  4522. #else /* OS2 */
  4523.                           tt_cols = w;
  4524.                           tt_rows = h;
  4525. #endif /* OS2 */
  4526.                       } else {
  4527. #ifdef OS2
  4528.                           tt_cols[VCMD] = w;
  4529.                           tt_rows[VCMD] = h;
  4530.                           VscrnSetWidth(VCMD, w);
  4531.                           VscrnSetHeight(VCMD, h);
  4532. #endif /* OS2 */
  4533.                           cmd_cols = w;
  4534.                           cmd_rows = h;
  4535.                       }
  4536. #else /* NOLOCAL */
  4537.                       cmd_cols = w;
  4538.                       cmd_rows = h;
  4539. #endif /* NOLOCAL */
  4540.                       /* Add LINES and COLUMNS to the environment */
  4541.                       ckmakmsg((char *)rows_buf,16,"LINES=",ckitoa(h),
  4542.                                 NULL,NULL);
  4543.                       ckmakmsg((char *)cols_buf,16,"COLUMNS=",ckitoa(w),
  4544.                                 NULL,NULL);
  4545. #ifdef OS2ORUNIX
  4546. #ifndef NOPUTENV
  4547.                       putenv(rows_buf);
  4548.                       putenv(cols_buf);
  4549. #endif /* NOPUTENV */
  4550. #endif /* OS2ORUNIX */
  4551.                   }
  4552.                   break;
  4553.               }
  4554.               case TELOPT_ECHO:
  4555.                 if (!echo) {
  4556.                     if (TELOPT_UNANSWERED_DO(x))
  4557.                       TELOPT_UNANSWERED_DO(x) = 0;
  4558.                     return(1);
  4559.                 }
  4560.                 break;
  4561.             }
  4562.         } else {
  4563.             if (TELOPT_UNANSWERED_DONT(x))
  4564.                 TELOPT_UNANSWERED_DONT(x) = 0;
  4565.             if (TELOPT_UNANSWERED_DO(x))
  4566.                 TELOPT_UNANSWERED_DO(x) = 0;
  4567.         }
  4568.         if (TELOPT_U_MODE(x) == TN_NG_MU) {
  4569.             ckmakmsg( tn_msg,TN_MSG_LEN,
  4570.                       "Peer refuses TELNET DO ",TELOPT(x),
  4571.                       " negotiations - terminating connection",NULL
  4572.                     );
  4573.             debug(F100,tn_msg,"",0);
  4574.             if (tn_deb || debses) tn_debug(tn_msg);
  4575.             printf("%s\n",tn_msg);
  4576.             ttclos(0);
  4577.             whyclosed = WC_TELOPT;
  4578.             return(-3);
  4579.         }
  4580. #ifdef COMMENT
  4581.         if (x == TELOPT_ECHO && !echo) /* Special handling for echo */
  4582.           return(1);                   /* because we allow 'duplex' */
  4583. #endif /* COMMENT */
  4584.         break;
  4585.  
  4586.       case DO:
  4587. #ifdef CK_SSL
  4588.         if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  4589.             return(0);
  4590. #endif /* CK_SSL */
  4591.         if (!TELOPT_OK(x) || TELOPT_ME_MODE(x) == TN_NG_RF) {
  4592.             if (tn_sopt(WONT,x) < 0)
  4593.               return(-1);
  4594.             if (TELOPT_UNANSWERED_WILL(x))
  4595.                 TELOPT_UNANSWERED_WILL(x) = 0;
  4596.         } else if (!TELOPT_ME(x)) {
  4597.             if (!TELOPT_UNANSWERED_WILL(x)) {
  4598.                 if (tn_sopt(WILL,x) < 0)
  4599.                   return(-1);
  4600.             }
  4601.             if (TELOPT_UNANSWERED_WILL(x))
  4602.                 TELOPT_UNANSWERED_WILL(x) = 0;
  4603.             TELOPT_ME(x) = 1;
  4604.  
  4605.             switch (x) {
  4606. #ifdef CK_SSL
  4607.               case TELOPT_START_TLS:
  4608.                 /*
  4609.                    If my proposal is accepted at this point the Telnet
  4610.                    protocol is turned off and a TLS negotiation takes
  4611.                    place.
  4612.  
  4613.                    Start by sending SB START_TLS FOLLOWS  to signal
  4614.                    we are ready.  Wait for the peer to send the same
  4615.                    and then start the TLS negotiation.
  4616.  
  4617.                    If the TLS negotiation succeeds we call tn_ini()
  4618.                    again to reset the telnet state machine and restart
  4619.                    the negotiation process over the now secure link.
  4620.  
  4621.                    If the TLS negotiation fails, we call ttclos()
  4622.                    to terminate the connection.  Then we set the
  4623.                    U_MODE and ME_MODE for TELOPT_START_TLS to REFUSE
  4624.                    and then call ttopen() to create a new connection
  4625.                    to the same host but this time do not attempt
  4626.                    TLS security.
  4627.  
  4628.                    Only the client should receive DO START_TLS.
  4629.                 */
  4630.                 tn_ssbopt(TELOPT_START_TLS,1,NULL,0);
  4631.                 TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows = 1;
  4632.                 break;
  4633. #endif /* CK_SSL */
  4634.  
  4635. #ifdef CK_AUTHENTICATION
  4636.               case TELOPT_AUTHENTICATION: {
  4637.                   /* We don't know what authentication we are using yet */
  4638.                   /* but it is not NULL until a failure is detected so */
  4639.                   /* use AUTO in the meantime. */
  4640.                   extern int authentication_version;
  4641.                   authentication_version = AUTHTYPE_AUTO;
  4642.                   break;
  4643.               }
  4644. #endif /* CK_AUTHENTICATION */
  4645. #ifdef CK_ENCRYPTION
  4646.               case TELOPT_ENCRYPTION:
  4647.                 if (!(TELOPT_ME(TELOPT_AUTHENTICATION) ||
  4648.                       TELOPT_U(TELOPT_AUTHENTICATION))
  4649.                     ) {
  4650.                     if (tn_sopt(WONT,x) < 0)
  4651.                       return(-1);
  4652.                     TELOPT_ME(x) = 0;
  4653.                 } else {
  4654.                     if (!(TELOPT_U(x) || TELOPT_UNANSWERED_DO(x))
  4655.                         && TELOPT_U_MODE(x) != TN_NG_RF) {
  4656.                         if (tn_sopt(DO, x) < 0)
  4657.                           return(-1);
  4658.                         TELOPT_UNANSWERED_DO(x) = 1;
  4659.                     }
  4660.                 }
  4661.                 break;
  4662. #endif /* CK_ENCRYPTION */
  4663. #ifdef IKS_OPTION
  4664.               case TELOPT_KERMIT:
  4665. /* If currently processing Kermit server packets, must tell the other side */
  4666.  
  4667.                 debug(F111,"tn_doop","what",what);
  4668.                 debug(F111,"tn_doop","server",server);
  4669. #ifdef CK_AUTODL
  4670.                 debug(F111,"tn_doop","autodl",autodl);
  4671.                 debug(F111,"tn_doop","inautodl",inautodl);
  4672.                 debug(F111,"tn_doop","cmdadl",cmdadl);
  4673. #endif /* CK_AUTODL */
  4674.  
  4675.                 if (server
  4676. #ifdef CK_AUTODL
  4677.                     || (local && ((what == W_CONNECT && autodl) ||
  4678.                                   (what != W_CONNECT && inautodl)))
  4679.                     || (!local && cmdadl)
  4680. #endif /* CK_AUTODL */
  4681.                     ) {
  4682.                     tn_siks(KERMIT_START);      /* START */
  4683.                 }
  4684.                 if (!TELOPT_U(x)) {
  4685.                     /* Tell the other side what Start of Packet Character */
  4686.                     tn_siks(KERMIT_SOP);             /* SOP */
  4687.                     if (!TELOPT_UNANSWERED_DO(x) &&
  4688.                         TELOPT_U_MODE(x) != TN_NG_RF) {
  4689.                         if (tn_sopt(DO, x) < 0)
  4690.                           return(-1);
  4691.                         TELOPT_UNANSWERED_DO(x) = 1;
  4692.                     }
  4693.                 }
  4694.                 break;
  4695. #endif /* IKS_OPTION */
  4696.  
  4697.               case TELOPT_BINARY:
  4698.                 if (!TELOPT_U(x)) {
  4699.                     if (!TELOPT_UNANSWERED_DO(x) &&
  4700.                         TELOPT_U_MODE(x) >= TN_NG_RQ) {
  4701.                         if (tn_sopt(DO, x) < 0)
  4702.                           return(-1);
  4703.                         TELOPT_UNANSWERED_DO(x) = 1;
  4704.                     }
  4705.                 }
  4706.                 break;
  4707.               case TELOPT_NAWS:
  4708. #ifdef CK_NAWS
  4709. #ifndef NOSIGWINCH
  4710. #ifdef SIGWINCH
  4711. #ifdef UNIX
  4712.                 if (sw_armed++ < 1) {   /* Catch window-size changes. */
  4713.                     debug(F100,"tn_doop arming SIGWINCH","",0);
  4714.                     signal(SIGWINCH,winchh);
  4715.                 }
  4716. #else
  4717.                 debug(F100,"SIGWINCH defined but not used","",0);
  4718. #endif /* UNIX */
  4719. #else  /* SIGWINCH */
  4720.                 debug(F100,"SIGWINCH not defined","",0);
  4721. #endif /* SIGWINCH */
  4722. #else  /* NOSIGWINCH */
  4723.                 debug(F100,"NOSIGWINCH defined","",0);
  4724. #endif /* NOSIGWINCH */
  4725.                 if ( !tn_delay_sb || !tn_outst(0) || tn_init ) {
  4726.                     if (tn_snaws() < 0)
  4727.                         return(-1);
  4728.                 } else {
  4729.                     TELOPT_SB(TELOPT_NAWS).naws.need_to_send = 1;
  4730.                 }
  4731. #endif /* CK_NAWS */
  4732.                 break;
  4733.               case TELOPT_LOGOUT:
  4734.                 ttclos(0);              /* And then hangup */
  4735.                 whyclosed = WC_TELOPT;
  4736. #ifdef IKSD
  4737.                 if (inserver
  4738. #ifndef NOLOCAL
  4739.                     && !local
  4740. #endif /* NOLOCAL */
  4741.                     )
  4742.                   doexit(GOOD_EXIT,0);
  4743. #endif /* IKSD */
  4744.                 if (TELOPT_UNANSWERED_WILL(x))
  4745.                   TELOPT_UNANSWERED_WILL(x) = 0;
  4746.                 return(6);
  4747. #ifdef CK_SNDLOC
  4748.                case TELOPT_SNDLOC:
  4749.                   if ( !tn_delay_sb || !tn_outst(0) || tn_init ) {
  4750.                       if (tn_sndloc() < 0)
  4751.                           return(-1);
  4752.                   } else {
  4753.                       TELOPT_SB(TELOPT_SNDLOC).sndloc.need_to_send = 1;
  4754.                   }
  4755.                   break;
  4756. #endif /* CK_SNDLOC */
  4757. #ifdef CK_FORWARD_X
  4758.                case TELOPT_FORWARD_X:
  4759.                   if ( !tn_delay_sb || !tn_outst(0) || tn_init ) {
  4760.                       if (fwdx_send_options() < 0) {
  4761.                           if (tn_sopt(DONT,x) < 0)
  4762.                               return(-1);
  4763.                           TELOPT_UNANSWERED_DONT(x) = 1;
  4764.                       }
  4765.                   } else {
  4766.                       TELOPT_SB(TELOPT_FORWARD_X).forward_x.need_to_send = 1;
  4767.                   }
  4768.                   break;
  4769. #endif /* CK_FORWARD_X */
  4770. #ifdef TN_COMPORT
  4771.               case TELOPT_COMPORT: {
  4772.                 extern int reliable;
  4773.         if (!tn_delay_sb || !tn_outst(0) || tn_init) {
  4774.             if (tn_sndcomport() < 0)
  4775.               return(-1);
  4776.         } else {
  4777.             TELOPT_SB(TELOPT_COMPORT).comport.need_to_send = 1;
  4778.         }
  4779.                 /* Telnet -> Serial -> ??? is not a reliable connection. */
  4780.                 reliable = SET_OFF;
  4781.         break;
  4782.               }
  4783. #endif /* TN_COMPORT */
  4784.             } /* switch */
  4785.         } else {
  4786.           if (TELOPT_UNANSWERED_WILL(x))
  4787.             TELOPT_UNANSWERED_WILL(x) = 0;
  4788.         }
  4789.         break;
  4790.  
  4791.       case DONT:
  4792. #ifdef CK_SSL
  4793.         if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  4794.             return(0);
  4795. #endif /* CK_SSL */
  4796.         if (!TELOPT_OK(x) || TELOPT_ME(x) || TELOPT_UNANSWERED_WILL(x)) {
  4797.             /* David Borman says we should not respond DONT when */
  4798.             /* the WONT is a response to a WILL that we sent. */
  4799.             if (!(TELOPT_UNANSWERED_WILL(x) || TELOPT_UNANSWERED_WONT(x)))
  4800.               if (tn_sopt(WONT,x) < 0)
  4801.                 return(-1);
  4802.  
  4803.             if (TELOPT_UNANSWERED_WILL(x))
  4804.                 TELOPT_UNANSWERED_WILL(x) = 0;
  4805.             if (TELOPT_UNANSWERED_WONT(x))
  4806.                 TELOPT_UNANSWERED_WONT(x) = 0;
  4807.             if (TELOPT_ME(x))
  4808.               TELOPT_ME(x) = 0;
  4809.  
  4810.             switch (x) {
  4811. #ifdef CK_SSL
  4812.             case TELOPT_START_TLS:
  4813.                 if (!sstelnet && TELOPT_ME_MODE(x) == TN_NG_MU) {
  4814.                     printf("Telnet Start-TLS refused.\n");
  4815.                     ttclos(0);
  4816.                     whyclosed = WC_TELOPT;
  4817.                     return(-3);
  4818.                 }
  4819.                 break;
  4820. #endif /* CK_SSL */
  4821. #ifdef CK_AUTHENTICATION
  4822.               case TELOPT_AUTHENTICATION:
  4823.                 if (!sstelnet && TELOPT_ME_MODE(x) == TN_NG_MU) {
  4824. #ifdef CK_SSL
  4825.                     if (tls_active_flag) {
  4826.                         TELOPT_ME_MODE(x) = TN_NG_AC;
  4827.                         break;
  4828.                     } else
  4829. #endif /* CK_SSL */
  4830.                     {
  4831.                         printf("Telnet authentication refused.\n");
  4832.                         ttclos(0);
  4833.                         whyclosed = WC_TELOPT;
  4834.                         return(-3);
  4835.                     }
  4836.                 } else if (TELOPT_ME_MODE(x) == TN_NG_RQ) {
  4837.                     TELOPT_ME_MODE(x) = TN_NG_AC;
  4838.                 }
  4839.                 if (ck_tn_auth_in_progress())
  4840.                   printf("Telnet authentication refused.\n");
  4841. #ifdef CK_ENCRYPTION
  4842.                 if (!sstelnet) {
  4843.                     if (tn_no_encrypt()<0)
  4844.                         return(-1);
  4845.                 }
  4846. #endif /* CK_ENCRYPTION */
  4847.                 break;
  4848. #endif /* CK_AUTHENTICATION */
  4849.               case TELOPT_ENCRYPTION:
  4850. #ifdef CK_ENCRYPTION
  4851.                 ck_tn_enc_stop();
  4852. #endif /* CK_ENCRYPTION */
  4853.                 break;
  4854.               case TELOPT_KERMIT:
  4855. #ifdef IKS_OPTION
  4856.                 TELOPT_SB(x).kermit.me_start = 0;
  4857. #endif /* IKS_OPTION */
  4858.                 break;
  4859.               default:
  4860.                 break;
  4861.             } /* switch */
  4862.         } else {
  4863.           if (TELOPT_UNANSWERED_WILL(x))
  4864.               TELOPT_UNANSWERED_WILL(x) = 0;
  4865.           if (TELOPT_UNANSWERED_WONT(x))
  4866.               TELOPT_UNANSWERED_WONT(x) = 0;
  4867.         }
  4868.         if (TELOPT_ME_MODE(x) == TN_NG_MU) {
  4869.             ckmakmsg( tn_msg,TN_MSG_LEN,
  4870.                       "Peer refuses TELNET WILL ",TELOPT(x),
  4871.                       " negotiations - terminating connection",
  4872.                       NULL
  4873.                       );
  4874.             debug(F100,tn_msg,"",0);
  4875.             if (tn_deb || debses) tn_debug(tn_msg);
  4876.             printf("%s\n",tn_msg);
  4877.             ttclos(0);
  4878.             whyclosed = WC_TELOPT;
  4879.             return(-3);
  4880.         }
  4881.         break;
  4882.       case SB:
  4883.         if ((y = tn_sb(x,&n,fn)) <= 0)
  4884.           return(y);
  4885.  
  4886. #ifdef CK_SSL
  4887.         /* Do not process subnegotiations other than START_TLS after we */
  4888.         /* have agreed to begin the TLS negotiation sequence.           */
  4889.         if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows &&
  4890.              x != TELOPT_START_TLS)
  4891.             break;
  4892. #endif /* CK_SSL */
  4893.  
  4894.         if (!TELOPT_OK(x)) {
  4895.             hexdump("unknown telnet subnegotiation",sb,n);
  4896.             break;
  4897.         } else if ( !(TELOPT_ME(x) || TELOPT_U(x)) ) {
  4898.             hexdump("telnet option not negotiated",sb,n);
  4899.             if (!tn_sb_bug)
  4900.                 break;
  4901.             if (TELOPT_UNANSWERED_WILL(x)) {
  4902.                 TELOPT_UNANSWERED_WILL(x) = 0;
  4903.                 TELOPT_U(x) = 1;
  4904.                 ckmakmsg(tn_msg,TN_MSG_LEN,
  4905.                          "TELNET DO ",TELOPT(x),
  4906.                          "(implied by receipt of SB - protocol error ignored)",
  4907.                          NULL
  4908.                          );
  4909.                 debug(F100,tn_msg,"",0);
  4910.                 if (tn_deb || debses) tn_debug(tn_msg);
  4911.             }
  4912.             if (TELOPT_UNANSWERED_DO(x)) {
  4913.                 TELOPT_UNANSWERED_DO(x) = 0;
  4914.                 TELOPT_ME(x) = 1;
  4915.                 ckmakmsg(tn_msg,TN_MSG_LEN,"TELNET WILL ",TELOPT(x),
  4916.                         " (implied by receipt of SB - protocol error ignored)",
  4917.                         NULL);
  4918.                 debug(F100,tn_msg,"",0);
  4919.                 if (tn_deb || debses) tn_debug(tn_msg);
  4920.              }
  4921.         }
  4922.  
  4923.         TELOPT_UNANSWERED_SB(x)=0;
  4924.         switch (x) {
  4925. #ifdef CK_FORWARD_X
  4926.           case TELOPT_FORWARD_X:
  4927.             return(fwdx_tn_sb(sb, n));
  4928. #endif /* CK_FORWARD_X */
  4929. #ifdef CK_SSL
  4930.           case TELOPT_START_TLS: {
  4931.               /*
  4932.                  the other side is saying SB START_TLS FOLLOWS
  4933.                  the incoming channel is now ready for starting the
  4934.                  TLS negotiation.
  4935.                  */
  4936.               int def_tls_u_mode, def_tls_me_mode;
  4937.               int def_enc_u_mode, def_enc_me_mode;
  4938.               int rc = 0;
  4939.  
  4940.               if (sb[0] != 1) {
  4941.                   break;
  4942.               }
  4943.  
  4944.               TELOPT_SB(TELOPT_START_TLS).start_tls.u_follows = 1;
  4945.               /* Preserve the default modes and make sure we will */
  4946.               /* refuse START_TLS when we retry. */
  4947.               if (sstelnet) {
  4948.                   def_tls_u_mode = TELOPT_DEF_S_U_MODE(TELOPT_START_TLS);
  4949.                   def_tls_me_mode = TELOPT_DEF_S_ME_MODE(TELOPT_START_TLS);
  4950.                   TELOPT_DEF_S_U_MODE(TELOPT_START_TLS) = TN_NG_RF;
  4951.                   TELOPT_DEF_S_ME_MODE(TELOPT_START_TLS)= TN_NG_RF;
  4952. #ifdef CK_ENCRYPTION
  4953.                   def_enc_u_mode = TELOPT_DEF_S_U_MODE(TELOPT_ENCRYPTION);
  4954.                   def_enc_me_mode = TELOPT_DEF_S_ME_MODE(TELOPT_ENCRYPTION);
  4955.                   TELOPT_DEF_S_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  4956.                   TELOPT_DEF_S_ME_MODE(TELOPT_ENCRYPTION)= TN_NG_RF;
  4957. #endif /* CK_ENCRYPTION */
  4958.               } else {
  4959.                   def_tls_u_mode = TELOPT_DEF_C_U_MODE(TELOPT_START_TLS);
  4960.                   def_tls_me_mode = TELOPT_DEF_C_ME_MODE(TELOPT_START_TLS);
  4961.                   TELOPT_DEF_C_U_MODE(TELOPT_START_TLS) = TN_NG_RF;
  4962.                   TELOPT_DEF_C_ME_MODE(TELOPT_START_TLS)= TN_NG_RF;
  4963. #ifdef CK_ENCRYPTION
  4964.                   def_enc_u_mode = TELOPT_DEF_C_U_MODE(TELOPT_ENCRYPTION);
  4965.                   def_enc_me_mode = TELOPT_DEF_C_ME_MODE(TELOPT_ENCRYPTION);
  4966.                   TELOPT_DEF_C_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  4967.                   TELOPT_DEF_C_ME_MODE(TELOPT_ENCRYPTION)= TN_NG_RF;
  4968. #endif /* CK_ENCRYPTION */
  4969.               }
  4970.               /* Negotiate TLS */
  4971.               ttnproto = NP_TLS;
  4972.               tn_init = 0;
  4973.               tn_begun = 0;
  4974.               if (ck_tn_tls_negotiate()<0) {
  4975.                   /* we failed.  disconnect and if we are the client */
  4976.                   /* then reconnect and try without START_TLS.       */
  4977.                   extern char * line;
  4978.                   int x = -1;
  4979.                   extern int mdmtyp;
  4980.  
  4981.                   if (sstelnet) {
  4982.                       printf("TLS failed:  Disconnecting.\n");
  4983.                       TELOPT_DEF_S_U_MODE(TELOPT_START_TLS)  = def_tls_u_mode;
  4984.                       TELOPT_DEF_S_ME_MODE(TELOPT_START_TLS) = def_tls_me_mode;
  4985. #ifdef CK_ENCRYPTION
  4986.                      TELOPT_DEF_S_U_MODE(TELOPT_ENCRYPTION)  = def_enc_u_mode;
  4987.                      TELOPT_DEF_S_ME_MODE(TELOPT_ENCRYPTION) = def_enc_me_mode;
  4988. #endif /* CK_ENCRYPTION */
  4989.                       ttclos(0);
  4990.                       whyclosed = WC_TELOPT;
  4991.                       ttnproto = NP_TELNET;
  4992.                       rc = -3;
  4993.                   } else {
  4994. #ifndef NOLOCAL
  4995.                       extern tls_norestore;
  4996. #endif /* NOLOCAL */
  4997.                       printf("TLS failed:  Disconnecting...\n");
  4998. #ifdef CK_ENCRYPTION
  4999.                      TELOPT_DEF_C_U_MODE(TELOPT_ENCRYPTION)  = def_enc_u_mode;
  5000.                      TELOPT_DEF_C_ME_MODE(TELOPT_ENCRYPTION) = def_enc_me_mode;
  5001. #endif /* CK_ENCRYPTION */
  5002.                       /* if START_TLS is not REQUIRED, then retry without it */
  5003.                       if ( def_tls_me_mode != TN_NG_MU ) {
  5004. #ifndef NOLOCAL
  5005.                           tls_norestore = 1;
  5006. #endif /* NOLOCAL */
  5007.                           ttclos(0);
  5008.                           whyclosed = WC_TELOPT;
  5009. #ifndef NOLOCAL
  5010.                           tls_norestore = 0;
  5011. #endif /* NOLOCAL */
  5012.                           ttnproto = NP_TELNET;
  5013.                           printf("Reconnecting without TLS.\n");
  5014.                           sleep(2);
  5015.                           if (ttopen(line,&x,mdmtyp,0)<0)
  5016.                               rc = -3;
  5017.                       } else {
  5018.                           TELOPT_DEF_C_U_MODE(TELOPT_START_TLS) =
  5019.                             def_tls_u_mode;
  5020.                           TELOPT_DEF_C_ME_MODE(TELOPT_START_TLS) =
  5021.                             def_tls_me_mode;
  5022.                           ttclos(0);
  5023.                           whyclosed = WC_TELOPT;
  5024.                           ttnproto = NP_TELNET;
  5025.                           rc = -3;
  5026.                       }
  5027.                   }
  5028.               } else {
  5029.                   /* we succeeded.  restart telnet negotiations from */
  5030.                   /* the beginning.  However, if we have received a  */
  5031.                   /* client certificate and we are a server, then do */
  5032.                   /* not offer TELOPT_AUTH.                          */
  5033.                   if ( ck_tn_auth_valid() == AUTH_VALID ) {
  5034.                       TELOPT_DEF_S_U_MODE(TELOPT_AUTHENTICATION) = TN_NG_AC;
  5035.                       TELOPT_DEF_S_ME_MODE(TELOPT_AUTHENTICATION)= TN_NG_AC;
  5036.                   }
  5037.                   ttnproto = NP_TELNET;
  5038.                   if (tn_ini() < 0)
  5039.                     if (ttchk() < 0)
  5040.                       rc = -1;
  5041.               }
  5042.               /* Restore the default modes */
  5043.               if (sstelnet) {
  5044.                   TELOPT_DEF_S_U_MODE(TELOPT_START_TLS)  = def_tls_u_mode;
  5045.                   TELOPT_DEF_S_ME_MODE(TELOPT_START_TLS) = def_tls_me_mode;
  5046. #ifdef CK_ENCRYPTION
  5047.                   TELOPT_DEF_S_U_MODE(TELOPT_ENCRYPTION)  = def_enc_u_mode;
  5048.                   TELOPT_DEF_S_ME_MODE(TELOPT_ENCRYPTION) = def_enc_me_mode;
  5049. #endif /* CK_ENCRYPTION */
  5050.               } else {
  5051.                   TELOPT_DEF_C_U_MODE(TELOPT_START_TLS)  = def_tls_u_mode;
  5052.                   TELOPT_DEF_C_ME_MODE(TELOPT_START_TLS) = def_tls_me_mode;
  5053. #ifdef CK_ENCRYPTION
  5054.                   TELOPT_DEF_C_U_MODE(TELOPT_ENCRYPTION)  = def_enc_u_mode;
  5055.                   TELOPT_DEF_C_ME_MODE(TELOPT_ENCRYPTION) = def_enc_me_mode;
  5056. #endif /* CK_ENCRYPTION */
  5057.               }
  5058.               return(rc);
  5059.           }
  5060. #endif /* CK_SSL */
  5061. #ifdef CK_AUTHENTICATION
  5062.           case TELOPT_AUTHENTICATION:
  5063.             if (ck_tn_sb_auth(sb,n) < 0) {
  5064.                 if (sstelnet && TELOPT_U_MODE(x) == TN_NG_MU) {
  5065.                     ttclos(0);
  5066.                     whyclosed = WC_TELOPT;
  5067.                     return(-3);
  5068.                 } else if (!sstelnet && TELOPT_ME_MODE(x) == TN_NG_MU) {
  5069.                     ttclos(0);
  5070.                     whyclosed = WC_TELOPT;
  5071.                     return(-3);
  5072.                 } else {
  5073.                     if (TELOPT_ME_MODE(x) == TN_NG_RQ)
  5074.                       TELOPT_ME_MODE(x) = TN_NG_AC;
  5075.                     if (TELOPT_U_MODE(x) == TN_NG_RQ)
  5076.                       TELOPT_U_MODE(x) = TN_NG_AC;
  5077.                 }
  5078.                 if (TELOPT_ME(x)) {
  5079.                     TELOPT_ME(x) = 0;
  5080.                     if (tn_sopt(WONT,x) < 0)
  5081.                       return(-1);
  5082.                 }
  5083.                 if (TELOPT_U(x)) {
  5084.                     TELOPT_U(x) = 0;
  5085.                     if (tn_sopt(DONT,x) < 0)
  5086.                       return(-1);
  5087.                 }
  5088. #ifdef CK_ENCRYPTION
  5089.                 if (tn_no_encrypt()<0)
  5090.                     return(-1);
  5091. #endif /* CK_ENCRYPTION */
  5092.             } else {
  5093. #ifdef CK_ENCRYPTION
  5094.                 if (!ck_tn_auth_in_progress()) { /* we are finished */
  5095.                     if (ck_tn_authenticated() == AUTHTYPE_SSL) {
  5096.                         /* TLS was successful.  Disable ENCRYPTION */
  5097.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  5098.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  5099.                     }
  5100.                     if (TELOPT_SB(TELOPT_ENCRYPTION).encrypt.need_to_send) {
  5101.                         ck_encrypt_send_support();
  5102.                         TELOPT_SB(TELOPT_ENCRYPTION).encrypt.need_to_send = 0;
  5103.                     }
  5104.                 }
  5105. #endif /* CK_ENCRYPTION */
  5106.             }
  5107.             break;
  5108. #endif /* CK_AUTHENTICATION */
  5109. #ifdef CK_ENCRYPTION
  5110.           case TELOPT_ENCRYPTION:
  5111.             if (ck_tn_sb_encrypt(sb, n) < 0) {
  5112.                 if (TELOPT_U_MODE(x) == TN_NG_MU ||
  5113.                     TELOPT_ME_MODE(x) == TN_NG_MU)
  5114.                   {
  5115.                       ttclos(0);
  5116.                       whyclosed = WC_TELOPT;
  5117.                       return(-3);
  5118.                 } else {
  5119.                     if (TELOPT_ME_MODE(x) == TN_NG_RQ)
  5120.                       TELOPT_ME_MODE(x) = TN_NG_AC;
  5121.                     if (TELOPT_U_MODE(x) == TN_NG_RQ)
  5122.                       TELOPT_U_MODE(x) = TN_NG_AC;
  5123.                 }
  5124.                 if (TELOPT_ME(x)) {
  5125.                     TELOPT_ME(x) = 0;
  5126.                     if (tn_sopt(WONT,x) < 0)
  5127.                       return(-1);
  5128.                 }
  5129.                 if (TELOPT_U(x)) {
  5130.                     TELOPT_U(x) = 0;
  5131.                     if (tn_sopt(DONT,x) < 0)
  5132.                       return(-1);
  5133.                 }
  5134.             }
  5135.             break;
  5136. #endif /* CK_ENCRYPTION */
  5137. #ifdef IKS_OPTION
  5138.           case TELOPT_KERMIT:
  5139.             return(iks_tn_sb(sb, n-2));
  5140. #endif /* IKS_OPTION */
  5141. #ifdef TN_COMPORT
  5142.           case TELOPT_COMPORT:
  5143.             return(tnc_tn_sb(sb, n-2));
  5144. #endif /* TN_COMPORT */
  5145.           case TELOPT_TTYPE:
  5146.             switch (sb[0]) {
  5147.               case TELQUAL_SEND:        /* SEND terminal type? */
  5148.                 if ( !tn_delay_sb || !tn_outst(0) || tn_init ) {
  5149.                     if (tn_sttyp() < 0) /* Yes, so send it. */
  5150.                         return(-1);
  5151.                 } else {
  5152.                     TELOPT_SB(TELOPT_TTYPE).term.need_to_send = 1;
  5153.                 }
  5154.                 break;
  5155.               case TELQUAL_IS: {        /* IS terminal type? */
  5156.                   /* IS terminal type -- remote gave us its current type */
  5157.                   int i = 0;
  5158. #ifndef OS2
  5159.                   CHAR oldterm[64], *p;
  5160. #endif /* OS2 */
  5161.                   /* Isolate the specified terminal type string */
  5162.                   while (sb[i++] != IAC) {
  5163.                       if (i == 40 ||    /* max len of term string - RFC */
  5164.                           sb[i] == IAC) {
  5165.                           sb[i] = '\0';
  5166.                           break;
  5167.                       }
  5168.                   }
  5169. #ifdef OS2
  5170. #ifndef NOTERM
  5171.                   strupr(&(sb[1]));     /* Upper case it */
  5172.                   for (i = 0; i <= max_tt; i++) { /* find it in our list */
  5173.                       if (!strcmp(&(sb[1]),tt_info[i].x_name)
  5174.                           && i != TT_VTNT) /* can't support VTNT as server */
  5175.                         {
  5176.                           /* Set terminal type to the one chosen */
  5177.                           if (i != tt_type)
  5178.                             settermtype(i,0);
  5179.                           break;
  5180.                       }
  5181.                   }
  5182.                   if (i > max_tt &&
  5183.                       strcmp(&(sb[1]),TELOPT_SB(TELOPT_TTYPE).term.type)) {
  5184.                       /* Couldn't find the specified term type */
  5185.                       sb[40] = '\0';
  5186.                       strcpy(TELOPT_SB(TELOPT_TTYPE).term.type,&(sb[1]));
  5187.                       /* SB TTYPE SEND */
  5188.                       tn_ssbopt(TELOPT_TTYPE,TELQUAL_SEND,NULL,0);
  5189.                       TELOPT_UNANSWERED_SB(TELOPT_TTYPE)=1;
  5190.                   }
  5191. #endif /* NOTERM */
  5192. #else /* OS2 */
  5193.                   p = (CHAR *) getenv("TERM");
  5194.                   if (p)
  5195.                     ckstrncpy((char *)oldterm,(char *)p,63);
  5196.                   else
  5197.                     oldterm[0] = '\0';
  5198.                   cklower((char *)&(sb[1])); /* Lower case new term */
  5199.                   ckmakmsg(term_buf,64,"TERM=",(char *)&(sb[1]),NULL,NULL);
  5200. #ifdef OS2ORUNIX
  5201. #ifndef NOPUTENV
  5202.                   putenv(term_buf);
  5203. #endif /* NOPUTENV */
  5204. #endif /* OS2ORUNIX */
  5205. #ifdef CK_CURSES
  5206. #ifndef MYCURSES
  5207. #ifndef COHERENT
  5208.                   if (trmbuf) {
  5209.                       if (tgetent(trmbuf,(char *)&sb[1]) < 1) {
  5210.                           /* Unsupported terminal.  If new and old terminal */
  5211.                           /* types do not match, ask for another type. */
  5212.                           if (strcmp((char *)oldterm,(char *)&sb[1])) {
  5213.                               /* SB TTYPE SEND */
  5214.                               tn_ssbopt(TELOPT_TTYPE,TELQUAL_SEND,NULL,0);
  5215.                               TELOPT_UNANSWERED_SB(TELOPT_TTYPE)=1;
  5216.                           }
  5217.                       }
  5218.                   }
  5219. #endif /* COHERENT */
  5220. #endif /* MYCURSES */
  5221. #endif /* CK_CURSES */
  5222. #endif /* OS2 */
  5223.               }
  5224.             }
  5225.             break;
  5226. #ifdef CK_ENVIRONMENT
  5227. #ifdef CK_XDISPLOC
  5228.           case TELOPT_XDISPLOC:         /* Send X-Display Location */
  5229.             if (sb[0] == TELQUAL_SEND) {/* SEND X-Display Loc? */
  5230.                 if ( !tn_delay_sb || !tn_outst(0) || tn_init ) {
  5231.                     if (tn_sxdisploc() < 0)     /* Yes, so send it. */
  5232.                         return(-1);
  5233.                 } else {
  5234.                     TELOPT_SB(TELOPT_XDISPLOC).xdisp.need_to_send = 1;
  5235.                 }
  5236.             }
  5237.             /* IS -- X Display Location (not supported) */
  5238.             else if (sb[0] == TELQUAL_IS) {
  5239.                 int i = 0;
  5240.                 /* Isolate the specified X-display string */
  5241.                 while (sb[i++] != IAC) {
  5242.                     if (i >= TSBUFSIZ)
  5243.                       return (-1);
  5244.                     if (sb[i] == IAC) {
  5245.                         sb[i] = '\0';
  5246.                         break;
  5247.                     }
  5248.                 }
  5249.                 debug(F110,"TELNET SB XDISPLOC IS",&sb[1],0);
  5250.             }
  5251.             break;
  5252. #endif /* CK_XDISPLOC */
  5253. #endif /* CK_ENVIRONMENT */
  5254.           case TELOPT_NAWS:
  5255.               if (sstelnet
  5256. #ifdef IKSD
  5257.                    || inserver
  5258. #endif /* IKSD */
  5259.                    ) {
  5260.                   int w = 0, h = 0;
  5261.                   int i = 0;
  5262.                   /* At this point sb[] should contain width and height */
  5263.                   if (sb[i] == IAC) i++;
  5264.                   w = (sb[i++] << 8);   /* save upper height */
  5265.                   if (sb[i] == IAC) i++;
  5266.                   w += sb[i++];         /* save the width */
  5267.                   if (sb[i] == IAC) i++;
  5268.                   h = (sb[i++] << 8);   /* save upper height */
  5269.                   if (sb[i] == IAC) i++;
  5270.                   h += sb[i++];
  5271.                   debug(F111,"tn_doop NAWS SB","width",w);
  5272.                   debug(F111,"tn_doop NAWS SB","height",h);
  5273.  
  5274.                   if (w == 0)
  5275.                     w = 80;
  5276.                   if (h == 0)
  5277.                     h = 24;
  5278. #ifndef NOLOCAL
  5279.                   if (tcp_incoming || inserver) {
  5280. #ifdef OS2
  5281.                       tt_cols[VTERM] = w;
  5282.                       tt_rows[VTERM] = h;
  5283.                       VscrnSetWidth(VTERM, w);
  5284.                       VscrnSetHeight(VTERM, h+(tt_status[VTERM]?1:0));
  5285. #ifdef IKSD
  5286.                       if (inserver) {
  5287.                           cmd_cols = tt_cols[VCMD] = w;
  5288.                           cmd_rows = tt_rows[VCMD] = h;
  5289.                           VscrnSetWidth(VCMD, w);
  5290.                           VscrnSetHeight(VCMD, h);
  5291.                       }
  5292. #endif /* IKSD */
  5293. #else /* OS2 */
  5294.                       tt_cols = w;
  5295.                       tt_rows = h;
  5296. #endif /* OS2 */
  5297.                   } else {
  5298. #ifdef OS2
  5299.                       tt_cols[VCMD] = w;
  5300.                       tt_rows[VCMD] = h;
  5301.                       VscrnSetWidth(VCMD, w);
  5302.                       VscrnSetHeight(VCMD, h);
  5303. #endif /* OS2 */
  5304.                       cmd_cols = w;
  5305.                       cmd_rows = h;
  5306.                   }
  5307. #else /* NOLOCAL */
  5308.                   cmd_cols = w;
  5309.                   cmd_rows = h;
  5310. #endif /* NOLOCAL */
  5311.  
  5312.                   /* Add LINES and COLUMNS to the environment */
  5313.                   ckmakmsg((char *)rows_buf,16,"LINES=",ckitoa(h),NULL,NULL);
  5314.                   ckmakmsg((char *)cols_buf,16,"COLUMNS=",ckitoa(w),NULL,NULL);
  5315. #ifdef OS2ORUNIX
  5316. #ifndef NOPUTENV
  5317.                   putenv(rows_buf);
  5318.                   putenv(cols_buf);
  5319. #endif /* NOPUTENV */
  5320. #endif /* OS2ORUNIX */
  5321.               }
  5322.               break;
  5323. #ifdef CK_ENVIRONMENT
  5324.           case TELOPT_NEWENVIRON:
  5325.             switch (sb[0]) {
  5326.               case TELQUAL_IS:                  /* IS */
  5327.               case TELQUAL_INFO:                /* INFO */
  5328.                 if (sb[0] == TELQUAL_IS)
  5329.                   debug(F101,"tn_doop NEW-ENV SB IS","",n-3);
  5330.                 else
  5331.                   debug(F101,"tn_doop NEW-ENV SB INFO","",n-3);
  5332.                 if (sstelnet || inserver) { /* Yes, receive it. */
  5333.                     if (tn_rnenv((CHAR *)&sb[1],n-3) < 0)
  5334.                       return(-1);
  5335.                 }
  5336.                 break;
  5337.               case TELQUAL_SEND:        /* SEND */
  5338.                 if ( sstelnet || inserver )         /* ignore if server */
  5339.                     break;
  5340.                 /* We need to take the sb[] and build a structure */
  5341.                 /* containing all of the variables and types that */
  5342.                 /* we are supposed to keep track of and send to   */
  5343.                 /* the host, then call tn_snenv().                  */
  5344.                 /* Or we can punt ...                               */
  5345.                 if ( !tn_delay_sb || !tn_outst(0) || tn_init ) {
  5346.                   if (tn_snenv((CHAR *)&sb[1],n-3) < 0) /* Yes, send it. */
  5347.                      return(-1);
  5348.                 } else {
  5349. #ifndef VMS
  5350.                   CHAR * xxx;
  5351.                   xxx = (CHAR *) malloc(n-1);
  5352. #else
  5353.                   unsigned char * xxx;
  5354.                   xxx = (unsigned char *) malloc(n-1);
  5355. #endif /* VMS */
  5356.                   /* Postpone sending until end of tn_ini() */
  5357.                   TELOPT_SB(TELOPT_NEWENVIRON).env.str = xxx;
  5358.                   if (TELOPT_SB(TELOPT_NEWENVIRON).env.str) {
  5359.                   memcpy((char *)TELOPT_SB(TELOPT_NEWENVIRON).env.str,
  5360.                             (char *)&sb[1],n-3);
  5361.                   TELOPT_SB(TELOPT_NEWENVIRON).env.str[n-3] = IAC;
  5362.                   TELOPT_SB(TELOPT_NEWENVIRON).env.str[n-2] = '\0';
  5363.                   TELOPT_SB(TELOPT_NEWENVIRON).env.len = n-3;
  5364.                   TELOPT_SB(TELOPT_NEWENVIRON).env.need_to_send = 1;
  5365.                   }
  5366.                 }
  5367.                 break;
  5368.               }
  5369.               break;
  5370. #endif /* CK_ENVIRONMENT */
  5371. #ifdef CK_SNDLOC
  5372.           case TELOPT_SNDLOC: {
  5373.               if ( deblog ) {
  5374.                   sb[n-2] = '\0';
  5375.                   debug(F110,"TELNET Send-Location",sb,0);
  5376.               }
  5377.               break;
  5378.           }
  5379. #endif /* CK_SNDLOC */
  5380.           } /* switch */
  5381.         break;
  5382.     }
  5383.     return(0);
  5384. }
  5385.  
  5386. int
  5387. #ifdef CK_ANSIC                         /* TELNET DO OPTION */
  5388. tn_doop(CHAR z, int echo, int (*fn)(int))
  5389. #else
  5390. tn_doop(z, echo, fn) CHAR z; int echo; int (*fn)();
  5391. #endif /* CK_ANSIC */
  5392. /* tn_doop */ {
  5393.     int x=0, y=0;
  5394.  
  5395.     if (z != (CHAR) IAC) {
  5396.         debug(F101,"tn_doop bad call","",z);
  5397.         return(-1);
  5398.     }
  5399.     if (ttnet != NET_TCPB)              /* Check network type */
  5400.       return(0);
  5401.     if (ttnproto != NP_TELNET &&
  5402.          ttnproto != NP_NONE)           /* Check protocol */
  5403.         return(0);
  5404.  
  5405.     x = tn_xdoop(z,echo,fn);
  5406.     if (x >= 0 && !tn_begun) {
  5407.         y = tn_start();
  5408.     }
  5409.     return(y < 0 ? y : x);
  5410. }
  5411.  
  5412. #ifdef CK_ENVIRONMENT
  5413.  
  5414. /* Telnet receive new environment */
  5415. /* Returns -1 on error, 0 if nothing happens, 1 on success */
  5416. /* In order for this code to work, sb[len] == IAC          */
  5417. /* We currently only support the USER environment variable */
  5418.  
  5419. int
  5420. #ifdef CK_ANSIC
  5421. tn_rnenv(CHAR * sb, int len)
  5422. #else
  5423. tn_rnenv(sb, len) CHAR * sb; int len;
  5424. #endif /* CK_ANSIC */
  5425. /* tn_rnenv */ {                        /* Receive new environment */
  5426.     char varname[17];
  5427.     char value[65];
  5428.     char * reply = 0, * s = 0;
  5429.     int i,j,k,n;                                /* Worker. */
  5430.     int type = 0; /* 0 for NONE, 1 for VAR, 2 for USERVAR, */
  5431.                   /* 3 for VALUE in progress */
  5432.  
  5433.     if (ttnet != NET_TCPB) return(0);
  5434.     if (ttnproto != NP_TELNET) return(0);
  5435.     if (sb == NULL) return(-1);
  5436.  
  5437.     if (len == 0) return(1);
  5438.  
  5439.     /*
  5440.     Pairs of <type> [VAR=0, VALUE=1, ESC=2, USERVAR=3] <value> "unterminated"
  5441.     follow here until done...
  5442.     */
  5443.     for (i = 0, j = 0, k = 0, type = 0, varname[0]= '\0'; i <= len; i++) {
  5444.         switch (sb[i]) {
  5445.         case TEL_ENV_VAR:               /* VAR */
  5446.         case TEL_ENV_USERVAR:           /* USERVAR */
  5447.         case IAC:                       /* End of the list */
  5448.             switch (type) {
  5449.           case 0:            /* Nothing in progress */
  5450.                 /* If we get IAC only, then that means there were */
  5451.                 /* no environment variables to send.  we are done */
  5452.                 if (j == 0 && sb[i] == IAC)
  5453.                     return(1);
  5454.           case 1:            /* VAR in progress */
  5455.           case 2:            /* USERVAR in progress */
  5456.           case 3:            /* VALUE in progress */
  5457.                 value[k] = '\0';
  5458.                 varname[j] = '\0';
  5459.                 debug(F111,"tn_rnenv varname",varname,type);
  5460.                 debug(F111,"tn_rnenv value",value,type);
  5461.                 if (!strcmp(varname,"USER")) {
  5462. #ifdef CK_AUTHENTICATION
  5463.                     if (ck_tn_auth_valid() != AUTH_VALID) {
  5464.                         extern char szUserNameRequested[];
  5465.                         debug(F100,"tn_rnenv != AUTH_VALID","",0);
  5466.                         ckstrncpy(szUserNameRequested,value,UIDBUFLEN);
  5467.                         ckstrncpy(uidbuf,value,UIDBUFLEN);
  5468. #ifdef CK_SSL
  5469.                         if (ssl_active_flag) {
  5470.                             if ( tls_is_user_valid(ssl_con, uidbuf) ) {
  5471.                                 extern char szUserNameAuthenticated[];
  5472.                                 ckstrncpy(szUserNameAuthenticated,uidbuf,
  5473.                                            UIDBUFLEN);
  5474.                                 auth_finished(AUTH_VALID);
  5475.                             }
  5476.                         } else if (tls_active_flag) {
  5477.                             if ( tls_is_user_valid(tls_con, uidbuf) ) {
  5478.                                 extern char szUserNameAuthenticated[];
  5479.                                 ckstrncpy(szUserNameAuthenticated,uidbuf,
  5480.                                            UIDBUFLEN);
  5481.                                 auth_finished(AUTH_VALID);
  5482.                             }
  5483.                         }
  5484. #endif /* CK_SSL */
  5485.                     } else {    /* AUTH_VALID */
  5486.                         int x = 0;
  5487.                         debug(F110,"tn_rnenv AUTH_VALID uidbuf",uidbuf,0);
  5488.  
  5489. #ifdef OS2
  5490.                         x = ckstrcmp(value,uidbuf,-1,0); /* case insensitive */
  5491. #ifdef NT
  5492.                         /* NTLM authentication returns names of the form */
  5493.                         /* DOMAIN\user.  We need to check to see of the  */
  5494.                         /* USER VAR contains a domain name or not.  If   */
  5495.                         /* not, then we do not want to change state if   */
  5496.                         /* the uidbuf matches the USER VAR when the      */
  5497.                         /* DOMAIN is ignored.                            */
  5498.                         if ( x && ck_tn_authenticated() == AUTHTYPE_NTLM ) {
  5499.                             char * s1=NULL, * s2=NULL;
  5500.                             int    len1, len2, i;
  5501.  
  5502.                             len1 = strlen(value);
  5503.                             for ( i=len1-1 ; i>=0 ; i--) {
  5504.                                 if ( value[i] == '\\' ) {
  5505.                                     s1 = &value[i+1];   /* DOMAIN found */
  5506.                                     break;
  5507.                                 }
  5508.                             }
  5509.  
  5510.                             if ( s1 == NULL ) {
  5511.                                 len2 = strlen(uidbuf);
  5512.                                 for ( i=len2-1 ; i>=0 ; i--) {
  5513.                                     if ( uidbuf[i] == '\\' ) {
  5514.                                         s2 = &uidbuf[i+1];   /* DOMAIN found */
  5515.                                         break;
  5516.                                     }
  5517.                                 }
  5518.  
  5519.                                 if ( s2 )
  5520.                                     x = ckstrcmp(value,s2,-1,0);
  5521.                             }
  5522.                         }
  5523. #endif /* NT */
  5524. #else /* OS2 */
  5525.                         x = ckstrcmp(value,uidbuf,-1,1); /* case sensitive */
  5526. #endif /* OS2 */
  5527.                         if ( x ) {
  5528.                             extern char szUserNameRequested[];
  5529.                             ckstrncpy(uidbuf,value,UIDBUFLEN);
  5530.                             ckstrncpy(szUserNameRequested,value,UIDBUFLEN);
  5531.                             auth_finished(AUTH_USER);
  5532. #ifdef CK_SSL
  5533.                             if (ssl_active_flag || tls_active_flag) {
  5534.                                 if ( tls_is_user_valid(ssl_con, uidbuf) )
  5535.                                     auth_finished(AUTH_VALID);
  5536.                             }
  5537. #endif /* CK_SSL */
  5538.                         }
  5539.                     }
  5540. #else /* CK_AUTHENTICATION */
  5541.                     ckstrncpy(uidbuf,value,UIDBUFLEN);
  5542. #endif /* CK_AUTHENTICATION */
  5543.                 }
  5544.                 break;
  5545.             }
  5546.             varname[0] = '\0';
  5547.             value[0] = '\0';
  5548.             j = 0;
  5549.             k = 0;
  5550.             type = (sb[i] == TEL_ENV_USERVAR ? 2 :      /* USERVAR */
  5551.                     sb[i] == TEL_ENV_VAR ? 1 :  /* VAR */
  5552.                      0
  5553.                      );
  5554.             break;
  5555.         case TEL_ENV_VALUE: /* VALUE */
  5556.             if ( type == 1 || type == 2 )
  5557.                 type = 3;
  5558.             break;
  5559.         case TEL_ENV_ESC:       /* ESC */
  5560.             /* Take next character literally */
  5561.             if ( ++i >= len )
  5562.                 break;
  5563.             /* otherwise, fallthrough so byte will be added to string. */
  5564.         default:
  5565.             switch (type) {
  5566.             case 1:     /* VAR in progress */
  5567.             case 2:     /* USERVAR in progress */
  5568.                 if ( j < 16 )
  5569.                     varname[j++] = sb[i];
  5570.                 break;
  5571.             case 3:
  5572.                 if ( k < 64 )
  5573.                     value[k++] = sb[i];
  5574.                 break;
  5575.             }
  5576.         }
  5577.     }
  5578.     return(0);
  5579. }
  5580.  
  5581. /* These are for Microsoft SFU version 2 Telnet Server */
  5582. #define SFUTLNTVER "SFUTLNTVER"
  5583. #define SFUTLNTMODE "SFUTLNTMODE"
  5584. #define SFUTLNTVER_VALUE  "2"
  5585. #define SFUTLNTMODE_VALUE "console"  /* The other value is "stream" */
  5586.  
  5587. /* Telnet send new environment */
  5588. /* Returns -1 on error, 0 if nothing happens, 1 on success */
  5589. /* In order for this code to work, sb[len] == IAC          */
  5590.  
  5591. int
  5592. #ifdef CK_ANSIC
  5593. tn_snenv(CHAR * sb, int len)
  5594. #else
  5595. tn_snenv(sb, len) CHAR * sb; int len;
  5596. #endif /* CK_ANSIC */
  5597. /* tn_snenv */ {                        /* Send new environment */
  5598.     char varname[16];
  5599.     char * reply = 0, * s = 0;
  5600.     int i,j,n;                          /* Worker. */
  5601.     int type = 0;       /* 0 for NONE, 1 for VAR, 2 for USERVAR in progress */
  5602.     extern int ck_lcname;
  5603.     char localuidbuf[UIDBUFLEN];
  5604.     char * uu = uidbuf;
  5605.     char * disp = NULL;
  5606.  
  5607.     if (ttnet != NET_TCPB) return(0);
  5608.     if (ttnproto != NP_TELNET) return(0);
  5609.     if (!sb) return(-1);
  5610.  
  5611. #ifdef CK_SSL
  5612.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  5613.         return(0);
  5614.     }
  5615. #endif /* CK_SSL */
  5616.  
  5617. #ifdef CK_FORWARD_X
  5618.     if (TELOPT_U(TELOPT_FORWARD_X)) {
  5619.         disp = NULL;
  5620.     } else
  5621. #endif /* CK_FORWARD_X */
  5622.         disp = (char *)tn_get_display();
  5623.  
  5624.     if (ck_lcname) {
  5625.         ckstrncpy(localuidbuf,uidbuf,UIDBUFLEN);
  5626.         cklower(localuidbuf);
  5627.         uu = localuidbuf;
  5628.     }
  5629.  
  5630.     hexdump((CHAR *)"tn_snenv sb[]",sb,len);
  5631.     debug(F110,"tn_snenv uidbuf",uidbuf,0);
  5632.     debug(F110,"tn_snenv localuidbuf",localuidbuf,0);
  5633.     debug(F110,"tn_snenv tn_env_sys",tn_env_sys,0);
  5634.     debug(F110,"tn_snenv tn_env_disp",tn_env_disp,0);
  5635.     debug(F110,"tn_snenv disp",disp,0);
  5636.  
  5637.     /* First determine the size of the buffer we will need */
  5638.     for (i = 0, j = 0, n = 0, type = 0, varname[0]= '\0'; i <= len; i++) {
  5639.         switch (sb[i]) {
  5640.           case TEL_ENV_VAR:             /* VAR */
  5641.           case TEL_ENV_USERVAR:         /* USERVAR */
  5642.     case IAC:                     /* End of the list */
  5643.             switch (type) {
  5644.         case 0:                   /* Nothing in progress */
  5645.                 /* If we get IAC only, then that means send all */
  5646.                 /* VAR and USERVAR.                             */
  5647.                 if (!(j == 0 && sb[i] == IAC))
  5648.                   break;
  5649.         case 1:                   /* VAR in progress */
  5650.                 varname[j] = '\0' ;
  5651.                 if (!varname[0]) {      /* Send All */
  5652.                     if (uu[0])
  5653.                       n += strlen(uu) + 4 + 2;
  5654.                     if (tn_env_job[0])
  5655.                       n += strlen(tn_env_job) + 3 + 2;
  5656.                     if (tn_env_acct[0])
  5657.                       n += strlen(tn_env_acct) + 4 + 2;
  5658.                     if (tn_env_prnt[0])
  5659.                       n += strlen(tn_env_prnt) + 7 + 2;
  5660.                     if (tn_env_sys[0])
  5661.                       n += strlen(tn_env_sys) + 10 + 2;
  5662.                     if (disp)
  5663.                       n += strlen(disp) + 7 + 2;
  5664.                 } else if (!strcmp(varname,"USER") && uu[0])
  5665.                   n += strlen(uu) + 4 + 2;
  5666.                 else if (!strcmp(varname,"JOB") && tn_env_job[0])
  5667.                   n += strlen(tn_env_job) + 3 + 2;
  5668.                 else if (!strcmp(varname,"ACCT") && tn_env_acct[0])
  5669.                   n += strlen(tn_env_acct) + 4 + 2;
  5670.                 else if (!strcmp(varname,"PRINTER") && tn_env_prnt[0])
  5671.                   n += strlen(tn_env_prnt) + 7 + 2;
  5672.                 else if (!strcmp(varname,"SYSTEMTYPE") && tn_env_sys[0])
  5673.                   n += strlen(tn_env_sys) + 10 + 2;
  5674.                 else if (!strcmp(varname,"DISPLAY") && disp)
  5675.                   n += strlen(disp) + 7 + 2;
  5676.                 /* If we get IAC only, then that means send all */
  5677.                 /* VAR and USERVAR.                             */
  5678.           if (!(j == 0 && sb[i] == IAC))
  5679.               break;
  5680.         case 2:                   /* USERVAR in progress */
  5681.         varname[j] = '\0' ;
  5682.         if (!varname[0]) {      /* Send All */
  5683.                     int x;
  5684.                     for ( x=0 ; x<8 ; x++ ) {
  5685.                         if ( tn_env_uservar[x][0] &&
  5686.                              tn_env_uservar[x][1] )
  5687.                             n += strlen(tn_env_uservar[x][0])
  5688.                                 + strlen(tn_env_uservar[x][1]) + 2;
  5689.                     }
  5690.                     if ( tn_sfu ) {
  5691.                         /* For compatibility with Microsoft Telnet Server */
  5692.                         n += strlen(SFUTLNTVER) + strlen(SFUTLNTVER_VALUE) + 2;
  5693.                         n += strlen(SFUTLNTMODE) +
  5694.               strlen(SFUTLNTMODE_VALUE) + 2;
  5695.                     }
  5696. #ifdef CK_SNDLOC
  5697.                     if ( tn_loc && tn_loc[0] )
  5698.                         n += strlen("LOCATION") + strlen(tn_loc) + 2;
  5699. #endif /* CK_SNDLOC */
  5700.                 }
  5701.                 else if (tn_sfu && !strcmp(varname,SFUTLNTVER))
  5702.                     n += strlen(SFUTLNTVER) + strlen(SFUTLNTVER_VALUE) + 2;
  5703.                 else if (tn_sfu && !strcmp(varname,SFUTLNTMODE))
  5704.                     n += strlen(SFUTLNTMODE) + strlen(SFUTLNTMODE_VALUE) + 2;
  5705. #ifdef CK_SNDLOC
  5706.                 else if ( tn_loc && tn_loc[0] && !strcmp(varname,"LOCATION"))
  5707.                     n += strlen("LOCATION") + strlen(tn_loc) + 2;
  5708. #endif /* CK_SNDLOC */
  5709.                 else {
  5710.                     int x;
  5711.                     for ( x=0 ; x<8 ; x++ ) {
  5712.                         if ( tn_env_uservar[x][0] &&
  5713.                              tn_env_uservar[x][1] &&
  5714.                              !strcmp(varname,tn_env_uservar[x][0]))
  5715.                             n += strlen(tn_env_uservar[x][0])
  5716.                                 + strlen(tn_env_uservar[x][1]) + 2;
  5717.                     }
  5718.                 }
  5719.         break;
  5720.             }
  5721.             varname[0] = '\0';
  5722.             j = 0;
  5723.             type = (sb[i] == TEL_ENV_USERVAR ? 2 :      /* USERVAR */
  5724.                     sb[i] == TEL_ENV_VAR ? 1 :          /* VAR */
  5725.                     0
  5726.                    );
  5727.             break;
  5728.           case TEL_ENV_VALUE:           /* VALUE */
  5729.             /* Protocol Error */
  5730.             debug(F100, "TELNET Subnegotiation error - VALUE in SEND", "",0);
  5731.             if (tn_deb || debses)
  5732.               tn_debug("TELNET Subnegotiation error - VALUE in SEND");
  5733.             return(0);
  5734.           case TEL_ENV_ESC:     /* ESC */
  5735.             if (++i >= len)
  5736.               break;
  5737.           default:
  5738.             if (j < 16 )
  5739.               varname[j++] = sb[i];
  5740.         }
  5741.     }
  5742.     reply = malloc(n + 16);              /* Leave room for IAC stuff */
  5743.     if (!reply) {
  5744.         debug(F100, "TELNET Subnegotiation error - malloc failed", "",0);
  5745.         if (tn_deb || debses)
  5746.           tn_debug("TELNET Subnegotiation error - malloc failed");
  5747.  
  5748.         /* Send a return packet with no variables so that the host */
  5749.         /* may continue with additional negotiations               */
  5750.         if (tn_ssbopt(TELOPT_NEWENVIRON,TELQUAL_IS,"",0) < 0)
  5751.           return(-1);
  5752.         return(0);
  5753.     }
  5754.  
  5755.     /* Now construct the real reply */
  5756.  
  5757.     n = 0;                              /* Start at beginning of buffer */
  5758. /*
  5759.   Pairs of <type> [VAR=0, VALUE=1, ESC=2, USERVAR=3] <value> "unterminated"
  5760.   follow here until done...
  5761. */
  5762.     for (i = 0, j = 0, type = 0, varname[0]= '\0'; i <= len; i++) {
  5763.         switch (sb[i]) {
  5764.           case TEL_ENV_VAR:             /* VAR */
  5765.           case TEL_ENV_USERVAR:         /* USERVAR */
  5766.           case IAC:                     /* End of the list */
  5767.             switch (type) {
  5768.               case 0:                   /* Nothing in progress */
  5769.                 /* If we get IAC only, then that means send all */
  5770.                 /* VAR and USERVAR.                             */
  5771.                 if (!(j == 0 && sb[i] == IAC))
  5772.                   break;
  5773.               case 1:                   /* VAR in progress */
  5774.                 varname[j] = '\0';
  5775.                 if (!varname[0]) {
  5776.                     /* Send All */
  5777.                     if (uu[0]) {
  5778.                         reply[n] = TEL_ENV_VAR; /* VAR */
  5779.                         strcpy(&reply[n+1],"USER");
  5780.                         reply[n+5] = TEL_ENV_VALUE;             /* VALUE */
  5781.                         strcpy(&reply[n+6],uu);
  5782.                         n += strlen(uu) + 4 + 2;
  5783.                     }
  5784.                     if (tn_env_job[0]) {
  5785.                         reply[n] = TEL_ENV_VAR; /* VAR */
  5786.                         strcpy(&reply[n+1],"JOB");
  5787.                         reply[n+4] = TEL_ENV_VALUE;     /* VALUE */
  5788.                         strcpy(&reply[n+5],tn_env_job);
  5789.                         n += strlen(tn_env_job) + 3 + 2;
  5790.                     }
  5791.                     if (tn_env_acct[0]) {
  5792.                         reply[n] = TEL_ENV_VAR; /* VAR */
  5793.                         strcpy(&reply[n+1],"ACCT");
  5794.                         reply[n+5] = TEL_ENV_VALUE;     /* VALUE */
  5795.                         strcpy(&reply[n+6],tn_env_acct);
  5796.                         n += strlen(tn_env_acct) + 4 + 2;
  5797.                     }
  5798.                     if (tn_env_prnt[0]) {
  5799.                         reply[n] = TEL_ENV_VAR; /* VAR */
  5800.                         strcpy(&reply[n+1],"PRINTER");
  5801.                         reply[n+8] = TEL_ENV_VALUE;     /* VALUE */
  5802.                         strcpy(&reply[n+9],tn_env_prnt);
  5803.                         n += strlen(tn_env_prnt) + 7 + 2;
  5804.                     }
  5805.                     if (tn_env_sys[0]) {
  5806.                         reply[n] = TEL_ENV_VAR; /* VAR */
  5807.                         strcpy(&reply[n+1],"SYSTEMTYPE");
  5808.                         reply[n+11] = TEL_ENV_VALUE; /* VALUE */
  5809.                         strcpy(&reply[n+12],tn_env_sys);
  5810.                         n += strlen(tn_env_sys) + 10 + 2;
  5811.                     }
  5812.                     if (disp) {
  5813.                         reply[n] = TEL_ENV_VAR; /* VAR */
  5814.                         strcpy(&reply[n+1],"DISPLAY");
  5815.                         reply[n+8] = TEL_ENV_VALUE;     /* VALUE */
  5816.                         strcpy(&reply[n+9],disp);
  5817.                         n += strlen(disp) + 7 + 2;
  5818.                     }
  5819.                 } else if (!strcmp(varname,"USER") && uu[0]) {
  5820.                     reply[n] = TEL_ENV_VAR;     /* VAR */
  5821.                     strcpy(&reply[n+1],"USER");
  5822.                     reply[n+5] = TEL_ENV_VALUE; /* VALUE */
  5823.                     strcpy(&reply[n+6],uu);
  5824.                     n += strlen(uu) + 4 + 2;
  5825.                 } else if (!strcmp(varname,"JOB") && tn_env_job[0]) {
  5826.                     reply[n] = TEL_ENV_VAR;     /* VAR */
  5827.                     strcpy(&reply[n+1],"JOB");
  5828.                     reply[n+4] = TEL_ENV_VALUE; /* VALUE */
  5829.                     strcpy(&reply[n+5],tn_env_job);
  5830.                     n += strlen(tn_env_job) + 3 + 2;
  5831.                 } else if (!strcmp(varname,"ACCT") && tn_env_acct[0]) {
  5832.                     reply[n] = TEL_ENV_VAR;     /* VAR */
  5833.                     strcpy(&reply[n+1],"ACCT");
  5834.                     reply[n+5] = TEL_ENV_VALUE; /* VALUE */
  5835.                     strcpy(&reply[n+6],tn_env_acct);
  5836.                     n += strlen(tn_env_acct) + 4 + 2;
  5837.                 } else if (!strcmp(varname,"PRINTER") && tn_env_prnt[0]) {
  5838.                     reply[n] = TEL_ENV_VAR;     /* VAR */
  5839.                     strcpy(&reply[n+1],"PRINTER");
  5840.                     reply[n+8] = TEL_ENV_VALUE; /* VALUE */
  5841.                     strcpy(&reply[n+9],tn_env_prnt);
  5842.                     n += strlen(tn_env_prnt) + 7 + 2;
  5843.                 } else if (!strcmp(varname,"SYSTEMTYPE") && tn_env_sys[0]) {
  5844.                     reply[n] = TEL_ENV_VAR;     /* VAR */
  5845.                     strcpy(&reply[n+1],"SYSTEMTYPE");
  5846.                     reply[n+11] = TEL_ENV_VALUE;        /* VALUE */
  5847.                     strcpy(&reply[n+12],tn_env_sys);
  5848.                     n += strlen(tn_env_sys) + 10 + 2;
  5849.                 } else if (!strcmp(varname,"DISPLAY") && disp) {
  5850.                     reply[n] = TEL_ENV_VAR;     /* VAR */
  5851.                     strcpy(&reply[n+1],"DISPLAY");
  5852.                     reply[n+8] = TEL_ENV_VALUE; /* VALUE */
  5853.                     strcpy(&reply[n+9],disp);
  5854.                     n += strlen(disp) + 7 + 2;
  5855.                 }
  5856.           /* If we get IAC only, then that means send all */
  5857.           /* VAR and USERVAR.                             */
  5858.           if (!(j == 0 && sb[i] == IAC))
  5859.               break;
  5860.             case 2:     /* USERVAR in progress */
  5861.           varname[j] = '\0';
  5862.           if (!varname[0]) {
  5863.               /* Send All */
  5864.                       int x,y;
  5865.                       for ( x=0 ; x<8 ; x++ ) {
  5866.                           if ( tn_env_uservar[x][0] &&
  5867.                                tn_env_uservar[x][1] ) {
  5868.                               reply[n] = TEL_ENV_USERVAR;     /* VAR */
  5869.                               y = strlen(tn_env_uservar[x][0]);
  5870.                               strcpy(&reply[n+1],tn_env_uservar[x][0]);
  5871.                               reply[n+y+1] = TEL_ENV_VALUE; /* VALUE */
  5872.                               strcpy(&reply[n+y+2],tn_env_uservar[x][1]);
  5873.                               n += y+strlen(tn_env_uservar[x][1])+2;
  5874.                           }
  5875.                       }
  5876.                       if ( tn_sfu ) {
  5877.                           /* Compatibility with Microsoft Telnet Server */
  5878.                           reply[n] = TEL_ENV_USERVAR;     /* VAR */
  5879.                           strcpy(&reply[n+1],SFUTLNTVER);
  5880.                           reply[n+11] = TEL_ENV_VALUE; /* VALUE */
  5881.                           strcpy(&reply[n+12],SFUTLNTVER_VALUE);
  5882.                           n += strlen(SFUTLNTVER)+strlen(SFUTLNTVER_VALUE)+2;
  5883.  
  5884.                           reply[n] = TEL_ENV_USERVAR;     /* VAR */
  5885.                           strcpy(&reply[n+1],SFUTLNTMODE);
  5886.                           reply[n+12] = TEL_ENV_VALUE; /* VALUE */
  5887.                           strcpy(&reply[n+13],SFUTLNTMODE_VALUE);
  5888.                           n += strlen(SFUTLNTMODE)+strlen(SFUTLNTMODE_VALUE)+2;
  5889.                       }
  5890.                       if (tn_loc && tn_loc[0]) {
  5891.                           reply[n] = TEL_ENV_USERVAR;     /* VAR */
  5892.                           strcpy(&reply[n+1],"LOCATION");
  5893.                           reply[n+9] = TEL_ENV_VALUE; /* VALUE */
  5894.                           strcpy(&reply[n+10],tn_loc);
  5895.                           n += strlen("LOCATION") + strlen(tn_loc) + 2;
  5896.                       }
  5897.           }  else if (tn_sfu && !strcmp(varname,SFUTLNTVER)) {
  5898.               reply[n] = TEL_ENV_USERVAR;     /* VAR */
  5899.               strcpy(&reply[n+1],SFUTLNTVER);
  5900.               reply[n+11] = TEL_ENV_VALUE; /* VALUE */
  5901.               strcpy(&reply[n+12],SFUTLNTVER_VALUE);
  5902.               n += strlen(SFUTLNTVER) + strlen(SFUTLNTVER_VALUE) + 2;
  5903.           }  else if (tn_sfu && !strcmp(varname,SFUTLNTMODE)) {
  5904.               reply[n] = TEL_ENV_USERVAR;     /* VAR */
  5905.               strcpy(&reply[n+1],SFUTLNTMODE);
  5906.               reply[n+12] = TEL_ENV_VALUE; /* VALUE */
  5907.               strcpy(&reply[n+13],SFUTLNTMODE_VALUE);
  5908.               n += strlen(SFUTLNTMODE) + strlen(SFUTLNTMODE_VALUE) + 2;
  5909.           }
  5910. #ifdef CK_SNDLOC
  5911.                   else if (tn_loc && tn_loc[0] && !strcmp(varname,"LOCATION")){
  5912.               reply[n] = TEL_ENV_USERVAR;     /* VAR */
  5913.               strcpy(&reply[n+1],"LOCATION");
  5914.               reply[n+9] = TEL_ENV_VALUE; /* VALUE */
  5915.               strcpy(&reply[n+10],tn_loc);
  5916.               n += strlen("LOCATION") + strlen(tn_loc) + 2;
  5917.                   }
  5918. #endif /* CK_SNDLOC */
  5919.                   else {
  5920.                       int x,y;
  5921.                       for ( x=0 ; x<8 ; x++ ) {
  5922.                           if ( tn_env_uservar[x][0] &&
  5923.                                tn_env_uservar[x][1] &&
  5924.                                !strcmp(varname,tn_env_uservar[x][0])) {
  5925.                               reply[n] = TEL_ENV_USERVAR;     /* VAR */
  5926.                               y = strlen(tn_env_uservar[x][0]);
  5927.                               strcpy(&reply[n+1],tn_env_uservar[x][0]);
  5928.                               reply[n+y+1] = TEL_ENV_VALUE; /* VALUE */
  5929.                               strcpy(&reply[n+y+2],tn_env_uservar[x][1]);
  5930.                               n += y+strlen(tn_env_uservar[x][1])+2;
  5931.                           }
  5932.                       }
  5933.                   }
  5934.                 break;
  5935.             }
  5936.             varname[0] = '\0';
  5937.             j = 0;
  5938.             type = (sb[i] == TEL_ENV_USERVAR ? 2 :      /* USERVAR */
  5939.                     sb[i] == TEL_ENV_VAR ? 1 :  /* VAR */
  5940.                     0
  5941.                    );
  5942.             break;
  5943.           case TEL_ENV_VALUE: /* VALUE */
  5944.             /* Protocol Error */
  5945.             debug(F100, "TELNET Subnegotiation error - VALUE in SEND", "",0);
  5946.             if (tn_deb || debses)
  5947.               tn_debug("TELNET Subnegotiation error - VALUE in SEND");
  5948.             return(0);  /* Was -1 but that would be taken as */
  5949.                         /* an I/O error, so absorb it and go on. */
  5950.           case TEL_ENV_ESC:     /* ESC */
  5951.             /* Not sure what this for.  Quote next character? */
  5952.             break;
  5953.           default:
  5954.             varname[j++] = sb[i];
  5955.         }
  5956.     }
  5957.     if (tn_ssbopt(TELOPT_NEWENVIRON,TELQUAL_IS,reply,n) < 0) {
  5958.         free(reply);
  5959.         return(-1);
  5960.     }
  5961.     free(reply);
  5962.     return(1);
  5963. }
  5964. #endif /* CK_ENVIRONMENT */
  5965.  
  5966. /* Telnet send terminal type */
  5967. /* Returns -1 on error, 0 if nothing happens, 1 if type sent successfully */
  5968.  
  5969. int
  5970. tn_sttyp() {                            /* Send telnet terminal type. */
  5971.     char *ttn;                          /* Name of terminal type. */
  5972. #ifdef OS2
  5973.     static int alias = -1;              /* which alias are we using ? */
  5974.     int settype = 0;
  5975. #endif /* OS2 */
  5976.     int i;                              /* Worker. */
  5977.     int tntermflg = 0;
  5978.  
  5979.     if (ttnet != NET_TCPB) return(0);
  5980.     if (ttnproto != NP_TELNET) return(0);
  5981.  
  5982.     if (!TELOPT_ME(TELOPT_TTYPE)) return(0);
  5983.  
  5984. #ifdef CK_SSL
  5985.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  5986.         return(0);
  5987.     }
  5988. #endif /* CK_SSL */
  5989.     ttn = NULL;
  5990.  
  5991. #ifndef NOTERM
  5992. #ifdef OS2
  5993.     if (!tn_term) {
  5994.         if (ttnum == -1) {
  5995.             ttnum = tt_type;
  5996.             settype = 0;
  5997.             alias = -1;
  5998.         } else if (ttnumend) {
  5999.             ttnumend = 0;
  6000.             settype = 0;
  6001.         } else {
  6002.             if (tt_info[tt_type].x_aliases[++alias] == NULL)  {
  6003.                 if (--tt_type < 0)
  6004.                   tt_type = max_tt;
  6005.                 if (ttnum == tt_type)
  6006.                   ttnumend = 1;
  6007.                 settype = 1;
  6008.                 alias = -1;
  6009.             }
  6010.         }
  6011.         if (tt_type >= 0 && tt_type <= max_tt) {
  6012.             if (alias == -1)
  6013.               ttn = tt_info[tt_type].x_name;
  6014.             else
  6015.               ttn = tt_info[tt_type].x_aliases[alias];
  6016.         } else
  6017.           ttn = NULL;
  6018.     }
  6019.     else settype = 0;
  6020. #endif /* OS2 */
  6021. #endif /* NOTERM */
  6022.  
  6023.     if (tn_term) {                      /* Terminal type override? */
  6024.         debug(F110,"tn_sttyp",tn_term,0);
  6025.         if (*tn_term) {
  6026.             ttn = tn_term;
  6027.             tntermflg = 1;
  6028.         }
  6029.     } else debug(F100,"tn_sttyp no term override","",0);
  6030.  
  6031. #ifndef datageneral
  6032.     if (!ttn) {                         /* If no override, */
  6033.         ttn = getenv("TERM");           /* get it from the environment. */
  6034.     }
  6035. #endif /* datageneral */
  6036.     if ((ttn == ((char *)0)) || ((int)strlen(ttn) >= TSBUFSIZ))
  6037.       ttn = "UNKNOWN";
  6038.     sb[0] = (CHAR) IAC;                 /* I Am a Command */
  6039.     sb[1] = (CHAR) SB;                  /* Subnegotiation */
  6040.     sb[2] = TELOPT_TTYPE;               /* Terminal Type */
  6041.     sb[3] = (CHAR) 0;                   /* Is... */
  6042.     for (i = 4; *ttn; ttn++,i++) {      /* Copy and uppercase it */
  6043. #ifdef VMS
  6044.         if (!tntermflg && *ttn == '-' &&
  6045.             (!strcmp(ttn,"-80") || !strcmp(ttn,"-132")))
  6046.           break;
  6047.         else
  6048. #endif /* VMS */
  6049.         sb[i] = (char) ((!tntermflg && islower(*ttn)) ? toupper(*ttn) : *ttn);
  6050.     }
  6051.     ttn = (char *)sb;                   /* Point back to beginning */
  6052.     if (deblog || tn_deb || debses) {
  6053.         sb[i] = '\0';                   /* For debugging */
  6054.         ckmakxmsg(tn_msg,TN_MSG_LEN,"TELNET SENT SB ",
  6055.                  TELOPT(TELOPT_TTYPE)," IS ",(char *)sb+4," IAC SE",
  6056.                  NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  6057.         debug(F100,tn_msg,"",0);
  6058.         if (tn_deb || debses) tn_debug(tn_msg);
  6059.     }
  6060.     sb[i++] = (CHAR) IAC;               /* End of Subnegotiation */
  6061.     sb[i++] = (CHAR) SE;                /* marked by IAC SE */
  6062.     if (ttol((CHAR *)sb,i) < 0)         /* Send it. */
  6063.       return(-1);
  6064. #ifndef NOTERM
  6065. #ifdef OS2
  6066.     if (settype)
  6067.         settermtype(tt_type,0);
  6068.     else {
  6069.         ipadl25();
  6070.         VscrnIsDirty(VTERM);
  6071.     }
  6072. #endif /* OS2 */
  6073. #endif /* NOTERM */
  6074.     return(1);
  6075. }
  6076.  
  6077. #ifdef CK_ENVIRONMENT
  6078. #ifdef CK_XDISPLOC
  6079.  
  6080. /* Telnet send xdisplay location */
  6081. /* Returns -1 on error, 0 if nothing happens, 1 if type sent successfully */
  6082.  
  6083. int
  6084. tn_sxdisploc() {                        /* Send telnet X display location. */
  6085.     char * disp=NULL;
  6086.     int i;
  6087.  
  6088.     if (ttnet != NET_TCPB) return(0);
  6089.     if (ttnproto != NP_TELNET) return(0);
  6090.  
  6091.     if (!TELOPT_ME(TELOPT_XDISPLOC)) return(0);
  6092.  
  6093. #ifdef CK_SSL
  6094.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  6095.         return(0);
  6096.     }
  6097. #endif /* CK_SSL */
  6098.  
  6099. #ifdef CK_FORWARD_X
  6100.     if (TELOPT_U(TELOPT_FORWARD_X)) {
  6101.         disp = NULL;
  6102.     } else
  6103. #endif /* CK_FORWARD_X */
  6104.         disp = (char *)tn_get_display();
  6105.     debug(F110,"tn_sxdisploc",disp,0);
  6106.  
  6107.     if (!disp) {
  6108.         /* Can't do both, send WONT */
  6109.         if (tn_sopt(WONT,TELOPT_XDISPLOC) < 0)
  6110.             return(-1);
  6111.         TELOPT_UNANSWERED_WONT(TELOPT_XDISPLOC) = 1;
  6112.         return(0);
  6113.     }
  6114.  
  6115.     sb[0] = (CHAR) IAC;                 /* I Am a Command */
  6116.     sb[1] = (CHAR) SB;                  /* Subnegotiation */
  6117.     sb[2] = TELOPT_XDISPLOC;            /* X-Display Location */
  6118.     sb[3] = (CHAR) 0;                   /* Is... */
  6119.     for (i = 4; *disp; disp++,i++) {      /* Copy and uppercase it */
  6120.         sb[i] = (char) *disp;
  6121.     }
  6122.     if (deblog || tn_deb || debses) {
  6123.         sb[i] = '\0';                   /* For debugging */
  6124.         ckmakxmsg( tn_msg,TN_MSG_LEN,
  6125.                   "TELNET SENT SB ",TELOPT(TELOPT_XDISPLOC),
  6126.                   " IS ",(char *)sb+4," IAC SE",
  6127.           NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  6128.         debug(F100,tn_msg,"",0);
  6129.         if (tn_deb || debses) tn_debug(tn_msg);
  6130.     }
  6131.     sb[i++] = (CHAR) IAC;               /* End of Subnegotiation */
  6132.     sb[i++] = (CHAR) SE;                /* marked by IAC SE */
  6133.     if (ttol((CHAR *)sb,i) < 0)         /* Send it. */
  6134.       return(-1);
  6135.     return(1);
  6136. }
  6137. #endif /* CK_XDISPLOC */
  6138. #endif /* CK_ENVIRONMENT */
  6139.  
  6140. #ifdef CK_FORWARD_X
  6141. int
  6142. tn_sndfwdx() {                          /* Send Fwd X Screen number to host */
  6143.     unsigned char screen = 0;
  6144.     char * disp;
  6145.     int i;
  6146.  
  6147.     if (!TELOPT_U(TELOPT_FORWARD_X)) return(0);
  6148. #ifdef CK_SSL
  6149.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  6150.         return(0);
  6151.     }
  6152. #endif /* CK_SSL */
  6153.  
  6154.     /*
  6155.      * The format of the DISPLAY variable is [<host>:]<display>[.<screen>]
  6156.      * where <host> is an optional DNS name or ip address with a default of
  6157.      * the localhost; the screen defaults to 0
  6158.      */
  6159.  
  6160.     disp = tn_get_display();
  6161.     if (disp) {
  6162.         int colon,dot;
  6163.         colon = ckindex(":",disp,0,0,1);
  6164.         dot   = ckindex(".",&disp[colon],0,0,1);
  6165.  
  6166.         if ( dot ) {
  6167.             screen = atoi(&disp[colon+dot]);
  6168.         }
  6169.     } else {
  6170.         screen = 0;
  6171.     }
  6172.  
  6173.     i = 0;
  6174.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  6175.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  6176.     sb[i++] = TELOPT_FORWARD_X;           /* Forward X */
  6177.     sb[i++] = FWDX_SCREEN;                /* Screen */
  6178.     sb[i++] = screen;
  6179.     if ( screen == IAC )
  6180.         sb[i++] = IAC;
  6181.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  6182.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  6183.     if (ttol((CHAR *)sb,i) < 0)         /* Send it. */
  6184.       return(-1);
  6185. #ifdef DEBUG
  6186.     if (deblog || tn_deb || debses) {
  6187.         ckmakxmsg( tn_msg,TN_MSG_LEN,
  6188.                    "TELNET SENT SB ",TELOPT(TELOPT_FORWARD_X),
  6189.                    " SCREEN ",ckctox(screen,1)," IAC SE",
  6190.                    NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  6191.         debug(F100,tn_msg,"",0);
  6192.         if (tn_deb || debses) tn_debug(tn_msg);
  6193.     }
  6194. #endif /* DEBUG */
  6195.     return(0);
  6196. }
  6197. #endif /* CK_FORWARD_X */
  6198.  
  6199. #ifdef CK_SNDLOC
  6200. int
  6201. tn_sndloc() {                           /* Send location. */
  6202.     int i;                              /* Worker. */
  6203.     char *ttloc;
  6204.  
  6205.     if (!TELOPT_ME(TELOPT_SNDLOC)) return(0);
  6206.  
  6207. #ifdef CK_SSL
  6208.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  6209.         return(0);
  6210.     }
  6211. #endif /* CK_SSL */
  6212.     ttloc = (tn_loc ? tn_loc : "");     /* In case we are being called even */
  6213.                                         /* though there is no location. */
  6214.     sb[0] = (CHAR) IAC;                 /* I Am a Command */
  6215.     sb[1] = (CHAR) SB;                  /* Subnegotiation */
  6216.     sb[2] = TELOPT_SNDLOC;              /* Location */
  6217.     for (i = 3; *ttloc && i < TSBUFSIZ; ttloc++,i++) /* Copy it */
  6218.       sb[i] = (char) *ttloc;
  6219.     sb[i++] = (CHAR) IAC;               /* End of Subnegotiation */
  6220.     sb[i++] = (CHAR) SE;                /* marked by IAC SE */
  6221.     if (ttol((CHAR *)sb,i) < 0)         /* Send it. */
  6222.       return(-1);
  6223.     sb[i-2] = '\0';                     /* For debugging */
  6224. #ifdef DEBUG
  6225.     if (deblog || tn_deb || debses) {
  6226.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  6227.           "TELNET SENT SB ",TELOPT(TELOPT_SNDLOC)," ",(char *)sb+3,
  6228.           " IAC SE", NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  6229.         debug(F100,tn_msg,"",0);
  6230.         if (tn_deb || debses) tn_debug(tn_msg);
  6231.     }
  6232. #endif /* DEBUG */
  6233.     return(0);
  6234. }
  6235. #endif /* CK_SNDLOC */
  6236.  
  6237. #ifdef CK_NAWS                  /*  NAWS = Negotiate About Window Size  */
  6238. int
  6239. tn_snaws() {                    /*  Send terminal width and height, RFC 1073 */
  6240. #ifndef NOLOCAL
  6241.     int i = 0;
  6242. #ifdef OS2
  6243.     int x = VscrnGetWidth(VTERM),
  6244.     y = VscrnGetHeight(VTERM) - (tt_status[VTERM] ? 1 : 0);
  6245. #else /* OS2 */
  6246.     int x = tt_cols, y = tt_rows;
  6247. #endif /* OS2 */
  6248.  
  6249.     if (ttnet != NET_TCPB) return(0);
  6250.     if (ttnproto != NP_TELNET) return(0);
  6251.     if (!TELOPT_ME(TELOPT_NAWS)) return(0);
  6252.  
  6253. #ifdef CK_SSL
  6254.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  6255.         return(0);
  6256.     }
  6257. #endif /* CK_SSL */
  6258.     if (x < 0) x = 0;
  6259.     if (y < 0) y = 0;
  6260.  
  6261.     if (x == TELOPT_SB(TELOPT_NAWS).naws.x && /* Only send if changed */
  6262.         y == TELOPT_SB(TELOPT_NAWS).naws.y
  6263.         )
  6264.       return(0);
  6265.     TELOPT_SB(TELOPT_NAWS).naws.x = x;  /* Remember the size     */
  6266.     TELOPT_SB(TELOPT_NAWS).naws.y = y;
  6267.  
  6268.     sb[i++] = (CHAR) IAC;               /* Send the subnegotiation */
  6269.     sb[i++] = (CHAR) SB;
  6270.     sb[i++] = TELOPT_NAWS;
  6271.     sb[i++] = (CHAR) (x >> 8) & 0xff;
  6272.     if ((CHAR) sb[i-1] == (CHAR) IAC)   /* IAC in data must be doubled */
  6273.       sb[i++] = (CHAR) IAC;
  6274.     sb[i++] = (CHAR) (x & 0xff);
  6275.     if ((CHAR) sb[i-1] == (CHAR) IAC)
  6276.       sb[i++] = (CHAR) IAC;
  6277.     sb[i++] = (CHAR) (y >> 8) & 0xff;
  6278.     if ((CHAR) sb[i-1] == (CHAR) IAC)
  6279.       sb[i++] = (CHAR) IAC;
  6280.     sb[i++] = (CHAR) (y & 0xff);
  6281.     if ((CHAR) sb[i-1] == (CHAR) IAC)
  6282.       sb[i++] = (CHAR) IAC;
  6283.     sb[i++] = (CHAR) IAC;
  6284.     sb[i++] = (CHAR) SE;
  6285.     if (deblog || tn_deb || debses) {
  6286.         ckmakxmsg(tn_msg,TN_MSG_LEN,"TELNET SENT SB NAWS ",
  6287.                   ckitoa(x)," ",ckitoa(y)," IAC SE",
  6288.                    NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  6289.         debug(F100,tn_msg,"",0);
  6290.         if (tn_deb || debses) tn_debug(tn_msg);
  6291.     }
  6292.     if (ttol((CHAR *)sb,i) < 0)         /* Send it. */
  6293.       return(-1);
  6294. #endif /* NOLOCAL */
  6295.     return (0);
  6296. }
  6297. #endif /* CK_NAWS */
  6298.  
  6299. #ifdef TN_COMPORT
  6300. static char * tnc_signature = NULL;
  6301. static int tnc_ls_mask = 0;
  6302. static int tnc_ls = 0;
  6303. static int tnc_ms_mask = 255;
  6304. static int tnc_ms = 0;
  6305. static int tnc_oflow = 0;
  6306. static int tnc_iflow = 0;
  6307. static int tnc_bps = 0;
  6308. static int tnc_datasize = 0;
  6309. static int tnc_parity = 0;
  6310. static int tnc_stopbit = 0;
  6311. static int tnc_break = 0;
  6312. static int tnc_dtr = 0;
  6313. static int tnc_rts = 0;
  6314. static int tnc_suspend_xmit = 0;
  6315. static int tnc_bps_index = -1;
  6316.  
  6317. int
  6318. #ifdef CK_ANSIC
  6319. tnc_init(void)
  6320. #else /* CK_ANSIC */
  6321. tnc_init()
  6322. #endif /* CK_ANSIC */
  6323. /* tnc_init */ {
  6324.     debug(F100,"tnc_init","",0);
  6325.  
  6326.     if (tnc_signature) {
  6327.         free(tnc_signature);
  6328.         tnc_signature = NULL;
  6329.     }
  6330.     tnc_ls_mask = 0;
  6331.     tnc_ls = 0;
  6332.     tnc_ms_mask = 255;
  6333.     tnc_ms = 0;
  6334.     tnc_oflow = 0;
  6335.     tnc_iflow = 0;
  6336.     tnc_bps = 0;
  6337.     tnc_datasize = 0;
  6338.     tnc_parity = 0;
  6339.     tnc_stopbit = 0;
  6340.     tnc_break = 0;
  6341.     tnc_dtr = 0;
  6342.     tnc_rts = 0;
  6343.     tnc_suspend_xmit = 0;
  6344.     tnc_bps_index = -1;
  6345.     return(0);
  6346. }
  6347.  
  6348. int
  6349. #ifdef CK_ANSIC
  6350. tn_sndcomport(void)
  6351. #else /* CK_ANSIC */
  6352. tn_sndcomport()
  6353. #endif /* CK_ANSIC */
  6354. /* tn_sndcomport */ {
  6355.     int baud, datasize, parity, stopsize, oflow, iflow;
  6356.     CONST char * signature;
  6357.  
  6358.     debug(F100,"tnc_sndcomport","",0);
  6359.     signature = tnc_get_signature();
  6360.     baud = tnc_get_baud();
  6361.     datasize = tnc_get_datasize();
  6362.     parity = tnc_get_parity();
  6363.     stopsize = tnc_get_stopsize();
  6364.     oflow = tnc_get_oflow();
  6365.     iflow = tnc_get_iflow();
  6366.     tnc_set_ls_mask(255);
  6367.     tnc_set_ms_mask(255);
  6368.     return(0);
  6369. }
  6370.  
  6371. int
  6372. #ifdef CK_ANSIC
  6373. tnc_wait(char * msg)
  6374. #else /* CK_ANSIC */
  6375. tnc_wait(msg) char * msg;
  6376. #endif /* CK_ANSIC */
  6377. /* tnc_wait */ {
  6378.     int rc, tn_wait_save = tn_wait_flg;
  6379.     debug(F110,"tnc_wait","begin",0);
  6380.     TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 1;
  6381.     tn_wait_flg = 1;
  6382.     rc = tn_wait(msg);
  6383.     debug(F110,"tnc_wait","end",0);
  6384.     tn_wait_flg = tn_wait_save;
  6385.     return(rc);
  6386. }
  6387.  
  6388. /* Returns -1 on error, 0 on success */
  6389. /* In order for this code to work, sb[len] == IAC          */
  6390.  
  6391. int
  6392. #ifdef CK_ANSIC
  6393. tnc_tn_sb(CHAR * sb, int len)
  6394. #else
  6395. tnc_tn_sb(sb, len) CHAR * sb; int len;
  6396. #endif /* CK_ANSIC */
  6397. /* tnc_tn_sb */ {
  6398.     if (ttnet != NET_TCPB) return(0);
  6399.     if (ttnproto != NP_TELNET) return(0);
  6400.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  6401.  
  6402.     if (!sb) return(-1);
  6403.  
  6404. #ifdef CK_SSL
  6405.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  6406.         return(0);
  6407.     }
  6408. #endif /* CK_SSL */
  6409.  
  6410.     switch (sb[0]) {
  6411.       case TNC_C2S_SIGNATURE:
  6412.       case TNC_S2C_SIGNATURE:
  6413.         debug(F111,"tnc_tn_sb","signature",len);
  6414.         if (len == 1) {
  6415.         tnc_send_signature("Kermit Telnet Com Port Option");
  6416.         } else {
  6417.             TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 0;
  6418.             if (tnc_signature)
  6419.           free(tnc_signature);
  6420.             tnc_signature = malloc(len);
  6421.             if (tnc_signature) {
  6422.                 memcpy(tnc_signature,&sb[1],len-1);
  6423.                 tnc_signature[len-1] = '\0';
  6424.             }
  6425.         }
  6426.         break;
  6427.  
  6428.       case TNC_C2S_SET_BAUDRATE:
  6429.       case TNC_S2C_SET_BAUDRATE: {
  6430.       long baudrate;
  6431.       char * br = (char *)&baudrate;
  6432.       TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 0;
  6433.           if (len == 2) {
  6434.             /* Actual behavior of the Access Server... */
  6435.             debug(F111,"tnc_tn_sb","baudrate index",sb[1]);
  6436.             tnc_bps_index = 1;
  6437.             switch (sb[1]) {
  6438.             case TNC_BPS_300:
  6439.               tnc_bps = 300;
  6440.               break;
  6441.             case TNC_BPS_600:
  6442.               tnc_bps = 600;
  6443.               break;
  6444.         case TNC_BPS_1200:
  6445.           tnc_bps = 1200;
  6446.           break;
  6447.         case TNC_BPS_2400:
  6448.           tnc_bps = 2400;
  6449.           break;
  6450.         case TNC_BPS_4800:
  6451.           tnc_bps = 4800;
  6452.           break;
  6453.         case TNC_BPS_9600:
  6454.           tnc_bps = 9600;
  6455.           break;
  6456.         case TNC_BPS_14400:
  6457.           tnc_bps = 14400;
  6458.           break;
  6459.         case TNC_BPS_19200:
  6460.           tnc_bps = 19200;
  6461.           break;
  6462.         case TNC_BPS_28800:
  6463.           tnc_bps = 28800;
  6464.           break;
  6465.         case TNC_BPS_38400:
  6466.           tnc_bps = 38400;
  6467.           break;
  6468.         case TNC_BPS_57600:
  6469.           tnc_bps = 57600;
  6470.           break;
  6471.         case TNC_BPS_115200:
  6472.           tnc_bps = 115200;
  6473.           break;
  6474.         case TNC_BPS_230400:
  6475.           tnc_bps = 230400;
  6476.           break;
  6477.         case TNC_BPS_460800:
  6478.           tnc_bps = 460800;
  6479.           break;
  6480.         default:
  6481.           tnc_bps = -1;
  6482.         }
  6483.           } else if (len == 5) {
  6484.             /* This section attempts to follow RFC 2217 */
  6485.               tnc_bps_index = 0;
  6486.               br[0] = sb[1];
  6487.               br[1] = sb[2];
  6488.               br[2] = sb[3];
  6489.               br[3] = sb[4];
  6490. #ifdef datageneral
  6491.           /* AOS/VS doesn't have ntohl() but MV's are big-endian */
  6492.               tnc_bps = baudrate;
  6493. #else
  6494.               tnc_bps = ntohl(baudrate);
  6495. #endif /* datageneral */
  6496.               debug(F111,"tnc_tn_sb","baudrate rfc",tnc_bps);
  6497.           } else {
  6498.               debug(F111,"tnc_tn_sb","baudrate invalid len",len);
  6499.               return(-1);
  6500.           }
  6501.       break;
  6502.       }
  6503.       case TNC_C2S_SET_DATASIZE:
  6504.       case TNC_S2C_SET_DATASIZE:
  6505.         TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 0;
  6506.         if (len < 2)
  6507.       return(-1);
  6508.         tnc_datasize = sb[1];
  6509.         debug(F111,"tnc_tn_sb","datasize",sb[1]);
  6510.         break;
  6511.  
  6512.       case TNC_C2S_SET_PARITY:
  6513.       case TNC_S2C_SET_PARITY:
  6514.         TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 0;
  6515.         if (len < 2)
  6516.       return(-1);
  6517.         tnc_parity = sb[1];
  6518.         debug(F111,"tnc_tn_sb","parity",sb[1]);
  6519.         break;
  6520.  
  6521.       case TNC_C2S_SET_STOPSIZE:
  6522.       case TNC_S2C_SET_STOPSIZE:
  6523.         TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 0;
  6524.         if (len < 2)
  6525.       return(-1);
  6526.         tnc_stopbit = sb[1];
  6527.         debug(F111,"tnc_tn_sb","stopsize",sb[1]);
  6528.         break;
  6529.  
  6530.       case TNC_C2S_SET_CONTROL:
  6531.       case TNC_S2C_SET_CONTROL:
  6532.         if (len < 2) {
  6533.             TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 0;
  6534.             return(-1);
  6535.         }
  6536.  
  6537.         /* This line should be removed when testing is complete. */
  6538.         TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 0;
  6539.  
  6540.         switch ( sb[1] ) {
  6541.       case TNC_CTL_OFLOW_REQUEST:
  6542.             /* determine local outbound flow control and send to peer */
  6543.             debug(F110,"tnc_tn_sb","oflow request",0);
  6544.             break;
  6545.       case TNC_CTL_OFLOW_NONE:
  6546.       case TNC_CTL_OFLOW_XON_XOFF:
  6547.       case TNC_CTL_OFLOW_RTS_CTS:
  6548.       case TNC_CTL_OFLOW_DCD:
  6549.       case TNC_CTL_OFLOW_DSR:
  6550.             TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 0;
  6551.             tnc_oflow = sb[1];
  6552.             debug(F111,"tnc_tn_sb","oflow",sb[1]);
  6553.             break;
  6554.       case TNC_CTL_BREAK_REQUEST:
  6555.             /* determine local break state and send to peer */
  6556.             debug(F110,"tnc_tn_sb","break request",0);
  6557.             break;
  6558.       case TNC_CTL_BREAK_ON:
  6559.             TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 0;
  6560.             tnc_break = 1;
  6561.             debug(F110,"tnc_tn_sb","break on",0);
  6562.             break;
  6563.  
  6564.       case TNC_CTL_BREAK_OFF:
  6565.             TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 0;
  6566.             tnc_break = 0;
  6567.             debug(F110,"tnc_tn_sb","break off",0);
  6568.             break;
  6569.  
  6570.       case TNC_CTL_DTR_REQUEST:
  6571.             /* determine local dtr state and send to peer */
  6572.             debug(F110,"tnc_tn_sb","dtr request",0);
  6573.             break;
  6574.  
  6575.       case TNC_CTL_DTR_ON:
  6576.             TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 0;
  6577.             tnc_dtr = 1;
  6578.             debug(F110,"tnc_tn_sb","dtr on",0);
  6579.             break;
  6580.  
  6581.       case TNC_CTL_DTR_OFF:
  6582.             TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 0;
  6583.             tnc_dtr = 0;
  6584.             debug(F110,"tnc_tn_sb","dtr off",0);
  6585.             break;
  6586.  
  6587.       case TNC_CTL_RTS_REQUEST:
  6588.             /* determine local rts state and send to peer */
  6589.             debug(F110,"tnc_tn_sb","rts request",0);
  6590.             break;
  6591.  
  6592.       case TNC_CTL_RTS_ON:
  6593.             TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 0;
  6594.             tnc_rts = 1;
  6595.             debug(F110,"tnc_tn_sb","rts on",0);
  6596.             break;
  6597.  
  6598.       case TNC_CTL_RTS_OFF:
  6599.             TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 0;
  6600.             tnc_rts = 0;
  6601.             debug(F110,"tnc_tn_sb","rts off",0);
  6602.             break;
  6603.  
  6604.       case TNC_CTL_IFLOW_REQUEST:
  6605.             /* determine local inbound flow control and send to peer */
  6606.             debug(F110,"tnc_tn_sb","iflow request",0);
  6607.             break;
  6608.  
  6609.       case TNC_CTL_IFLOW_NONE:
  6610.       case TNC_CTL_IFLOW_XON_XOFF:
  6611.       case TNC_CTL_IFLOW_RTS_CTS:
  6612.       case TNC_CTL_IFLOW_DTR:
  6613.             TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb = 0;
  6614.             tnc_iflow = sb[1];
  6615.             debug(F111,"tnc_tn_sb","iflow",sb[1]);
  6616.             break;
  6617.       default:
  6618.             return(-1);
  6619.         }
  6620.         break;
  6621.  
  6622.       case TNC_C2S_NOTIFY_LINESTATE:
  6623.       case TNC_S2C_SEND_LS:
  6624.         if (len < 2)
  6625.       return(-1);
  6626.         tnc_ls = sb[1];
  6627.         debug(F111,"tnc_tn_sb","linestate",sb[1]);
  6628.         if (tn_deb || debses) {
  6629.             if (tnc_ls & TNC_MS_DATA_READY )
  6630.           tn_debug("  ComPort Linestate Data Ready");
  6631.             if (tnc_ls & TNC_MS_OVERRUN_ERROR )
  6632.           tn_debug("  ComPort Linestate Overrun Error");
  6633.             if (tnc_ls & TNC_MS_PARITY_ERROR )
  6634.           tn_debug("  ComPort Linestate Parity Error");
  6635.             if (tnc_ls & TNC_MS_FRAME_ERROR )
  6636.           tn_debug("  ComPort Linestate Framing Error");
  6637.             if (tnc_ls & TNC_MS_BREAK_ERROR )
  6638.           tn_debug("  ComPort Linestate Break Detect Error");
  6639.             if (tnc_ls & TNC_MS_HR_EMPTY )
  6640.           tn_debug("  ComPort Linestate Holding Register Empty");
  6641.             if (tnc_ls & TNC_MS_SR_EMPTY )
  6642.           tn_debug("  ComPort Linestate Shift Register Empty");
  6643.             if (tnc_ls & TNC_MS_TIMEOUT_ERROR )
  6644.           tn_debug("  ComPort Linestate Timeout Error");
  6645.         }
  6646.         break;
  6647.  
  6648.       case TNC_C2S_NOTIFY_MODEMSTATE:
  6649.       case TNC_S2C_SEND_MS:
  6650.         if (len < 2)
  6651.       return(-1);
  6652.         tnc_ms = sb[1];
  6653.         debug(F111,"tnc_tn_sb","modemstate",sb[1]);
  6654.         if (tn_deb || debses) {
  6655.             if (tnc_ms & TNC_MS_CTS_DELTA )
  6656.           tn_debug("  ComPort Modemstate CTS State Change");
  6657.             if (tnc_ms & TNC_MS_DSR_DELTA )
  6658.           tn_debug("  ComPort Modemstate DSR State Change");
  6659.             if (tnc_ms &
  6660.         TNC_MS_EDGE_RING )
  6661.           tn_debug("  ComPort Modemstate Trailing Edge Ring Detector On");
  6662.             else
  6663.           tn_debug("  ComPort Modemstate Trailing Edge Ring Detector Off");
  6664.             if (tnc_ms & TNC_MS_RLSD_DELTA )
  6665.           tn_debug("  ComPort Modemstate RLSD State Change");
  6666.             if (tnc_ms & TNC_MS_CTS_SIG )
  6667.           tn_debug("  ComPort Modemstate CTS Signal On");
  6668.             else
  6669.           tn_debug("  ComPort Modemstate CTS Signal Off");
  6670.             if (tnc_ms & TNC_MS_DSR_SIG )
  6671.           tn_debug("  ComPort Modemstate DSR Signal On");
  6672.             else
  6673.           tn_debug("  ComPort Modemstate DSR Signal Off");
  6674.             if (tnc_ms & TNC_MS_RI_SIG )
  6675.           tn_debug("  ComPort Modemstate Ring Indicator On");
  6676.             else
  6677.           tn_debug("  ComPort Modemstate Ring Indicator Off");
  6678.             if (tnc_ms & TNC_MS_RLSD_SIG )
  6679.           tn_debug("  ComPort Modemstate RLSD Signal On");
  6680.             else
  6681.           tn_debug("  ComPort Modemstate RLSD Signal Off");
  6682.         }
  6683.         break;
  6684.  
  6685.       case TNC_C2S_FLOW_SUSPEND:
  6686.       case TNC_S2C_FLOW_SUSPEND:
  6687.         debug(F110,"tnc_tn_sb","flow suspend",0);
  6688.         tnc_suspend_xmit = 1;
  6689.         break;
  6690.  
  6691.       case TNC_C2S_FLOW_RESUME:
  6692.       case TNC_S2C_FLOW_RESUME:
  6693.         debug(F110,"tnc_tn_sb","flow resume",0);
  6694.         tnc_suspend_xmit = 0;
  6695.         break;
  6696.  
  6697.       case TNC_C2S_SET_LS_MASK:
  6698.       case TNC_S2C_SET_LS_MASK:
  6699.         if (len < 2)
  6700.       return(-1);
  6701.         debug(F111,"tnc_tn_sb","linestate mask",sb[1]);
  6702.         tnc_ls_mask = sb[1];
  6703.         break;
  6704.  
  6705.       case TNC_C2S_SET_MS_MASK:
  6706.       case TNC_S2C_SET_MS_MASK:
  6707.         if (len < 2)
  6708.       return(-1);
  6709.         debug(F111,"tnc_tn_sb","modemstate mask",sb[1]);
  6710.         tnc_ls_mask = sb[1];
  6711.         break;
  6712.  
  6713.       case TNC_C2S_PURGE:
  6714.       case TNC_S2C_PURGE:
  6715.         if (len < 2)
  6716.       return(-1);
  6717.         debug(F111,"tnc_tn_sb","purge",sb[1]);
  6718.         switch ( sb[1] ) {
  6719.       case TNC_PURGE_RECEIVE:
  6720.       case TNC_PURGE_TRANSMIT:
  6721.       case TNC_PURGE_BOTH:
  6722.             /* purge local buffers */
  6723.             break;
  6724.       default:
  6725.             return(-1);
  6726.         }
  6727.         break;
  6728.       default:
  6729.         return(-1);
  6730.     }
  6731.     return(0);
  6732. }
  6733.  
  6734. CONST char *
  6735. #ifdef CK_ANSIC
  6736. tnc_get_signature(void)
  6737. #else /* CK_ANSIC */
  6738. tnc_get_signature()
  6739. #endif /* CK_ANSIC */
  6740. /* tnc_get_signature */ {
  6741.     /* send IAC SB COM-PORT SIGNATURE IAC SE */
  6742.     /* wait for response */
  6743.     int i = 0;
  6744.  
  6745.     if (ttnet != NET_TCPB) return(NULL);
  6746.     if (ttnproto != NP_TELNET) return(NULL);
  6747.  
  6748.     if (!TELOPT_ME(TELOPT_COMPORT)) return(NULL);
  6749.  
  6750. #ifdef CK_SSL
  6751.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  6752.         return(NULL);
  6753.     }
  6754. #endif /* CK_SSL */
  6755.  
  6756.     sb[i++] = (CHAR) IAC;        /* I Am a Command */
  6757.     sb[i++] = (CHAR) SB;        /* Subnegotiation */
  6758.     sb[i++] = TELOPT_COMPORT;        /* ComPort */
  6759.     sb[i++] = TNC_C2S_SIGNATURE;    /* Signature */
  6760.     sb[i++] = (CHAR) IAC;               /* End of Subnegotiation */
  6761.     sb[i++] = (CHAR) SE;                /* marked by IAC SE */
  6762.     if (ttol((CHAR *)sb,i) < 0)         /* Send it. */
  6763.       return(NULL);
  6764. #ifdef DEBUG
  6765.     if (deblog || tn_deb || debses) {
  6766.         ckmakmsg(tn_msg,TN_MSG_LEN,
  6767.          "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  6768.          " SIGNATURE IAC SE", NULL);
  6769.         debug(F100,tn_msg,"",0);
  6770.         if (tn_deb || debses) tn_debug(tn_msg);
  6771.     }
  6772. #endif /* DEBUG */
  6773.  
  6774.     if (tnc_wait("comport signature request") < 0) {
  6775.         tn_push();
  6776.         return(NULL);
  6777.     }
  6778.     debug(F110,"tnc_get_signature",tnc_signature,0);
  6779.     return(tnc_signature);
  6780. }
  6781.  
  6782. int
  6783. #ifdef CK_ANSIC
  6784. tnc_send_signature(char * signature)
  6785. #else /* CK_ANSIC */
  6786. tnc_send_signature(signature) char * signature;
  6787. #endif /* CK_ANSIC */
  6788. /* tnc_send_signature */ {
  6789.     /* send IAC SB COM-PORT SIGNATURE <text> IAC SE */
  6790.     int i = 0, j = 0;
  6791.  
  6792.     debug(F110,"tnc_send_signature",signature,0);
  6793.  
  6794.     if (!signature || !signature[0])
  6795.       return(0);
  6796.  
  6797.     if (ttnet != NET_TCPB) return(0);
  6798.     if (ttnproto != NP_TELNET) return(0);
  6799.  
  6800.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  6801.  
  6802. #ifdef CK_SSL
  6803.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  6804.         return(0);
  6805.     }
  6806. #endif /* CK_SSL */
  6807.  
  6808.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  6809.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  6810.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  6811.     sb[i++] = TNC_C2S_SIGNATURE;      /* Signature */
  6812.     for (; signature[j]; i++,j++)
  6813.       sb[i] = signature[j];
  6814.     sb[i++] = (CHAR) IAC;               /* End of Subnegotiation */
  6815.     sb[i++] = (CHAR) SE;                /* marked by IAC SE */
  6816.     if (ttol((CHAR *)sb,i) < 0)         /* Send it. */
  6817.       return(-1);
  6818. #ifdef DEBUG
  6819.     if (deblog || tn_deb || debses) {
  6820.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  6821.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  6822.           " SIGNATURE ", signature, " IAC SE", NULL,
  6823.           NULL,NULL,NULL,NULL,NULL,NULL);
  6824.         debug(F100,tn_msg,"",0);
  6825.         if (tn_deb || debses) tn_debug(tn_msg);
  6826.     }
  6827. #endif /* DEBUG */
  6828.     return(0);
  6829. }
  6830.  
  6831. int
  6832. #ifdef CK_ANSIC
  6833. tnc_set_baud( long baud )
  6834. #else /* CK_ANSIC */
  6835. tnc_set_baud(baud) long baud;
  6836. #endif /* CK_ANSIC */
  6837. /* tnc_set_baud */ {
  6838.     /* send IAC SB COM-PORT SET-BAUD <value(4)> IAC SE  */
  6839.     /* wait for response */
  6840.     /* 0 is used to request the current baud rate and */
  6841.     /* may not be sent by this func */
  6842.     /* return new host value */
  6843.  
  6844.     /*
  6845.      *   the above comes from the RFC.  But that is not what I am seeing
  6846.      *   instead I appear to be seeing to following:
  6847.      *
  6848.      *      Value               Baud
  6849.      *          1               ?
  6850.      *          2               ?
  6851.      *          3               300
  6852.      *          4               600
  6853.      *          5               1200
  6854.      *          6               2400
  6855.      *          7               4800      ?
  6856.      *          8               9600
  6857.      *          9                         ?
  6858.      *          10              19200     ?
  6859.      *          11                        ?
  6860.      *          12              38400
  6861.      *          13              57600     ?
  6862.      *          14              115200
  6863.      *          15              230400    ?
  6864.      *          16              460800    ?
  6865.      */
  6866.  
  6867.     int i = 0;
  6868. #ifdef datageneral
  6869.     /* AOS/VS doesn't have htonl() but MV's are big-endian */
  6870.     long net_baud = baud;
  6871. #else
  6872.     long net_baud = htonl(baud);
  6873. #endif /* datageneral */
  6874.     CHAR b;
  6875.  
  6876.     debug(F111,"tnc_set_baud","begin",baud);
  6877.  
  6878.     if (ttnet != NET_TCPB) return(0);
  6879.     if (ttnproto != NP_TELNET) return(0);
  6880.  
  6881.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  6882.  
  6883. #ifdef CK_SSL
  6884.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  6885.         return(0);
  6886.     }
  6887. #endif /* CK_SSL */
  6888.  
  6889.     if (baud <= 0)
  6890.         return(0);
  6891.  
  6892.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  6893.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  6894.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  6895.     sb[i++] = TNC_C2S_SET_BAUDRATE;   /* Set Baud Rate */
  6896.  
  6897.     if (tnc_bps_index) {
  6898.         /* IOS Access Server */
  6899.         if (baud <= 300)
  6900.             b = TNC_BPS_300;
  6901.         else if (baud <= 600)
  6902.             b = TNC_BPS_600;
  6903.         else if (baud <= 1200)
  6904.             b = TNC_BPS_1200;
  6905.         else if (baud <= 2400)
  6906.             b = TNC_BPS_2400;
  6907.         else if (baud <= 4800)
  6908.             b = TNC_BPS_4800;
  6909.         else if (baud <= 9600)
  6910.             b = TNC_BPS_9600;
  6911.         else if (baud <= 14400)
  6912.             b = TNC_BPS_14400;
  6913.         else if (baud <= 19200)
  6914.             b = TNC_BPS_19200;
  6915.         else if (baud <= 28800)
  6916.             b = TNC_BPS_28800;
  6917.         else if (baud <= 38400)
  6918.             b = TNC_BPS_38400;
  6919.         else if (baud <= 57600)
  6920.             b = TNC_BPS_57600;
  6921.         else if (baud <= 115200)
  6922.             b = TNC_BPS_115200;
  6923.         else if (baud <= 230400)
  6924.             b = TNC_BPS_230400;
  6925.         else
  6926.             b = TNC_BPS_460800;
  6927.         sb[i++] = b;
  6928.     } else {
  6929.         /* RFC 2217 */
  6930.         sb[i++] = ((char *)&net_baud)[0];
  6931.         sb[i++] = ((char *)&net_baud)[1];
  6932.         sb[i++] = ((char *)&net_baud)[2];
  6933.         sb[i++] = ((char *)&net_baud)[3];
  6934.     }
  6935.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  6936.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  6937.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  6938.       return(-1);
  6939. #ifdef DEBUG
  6940.     if (deblog || tn_deb || debses) {
  6941.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  6942.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  6943.           " SET-BAUD-RATE ", ckltoa(baud)," IAC SE", NULL,
  6944.           NULL,NULL,NULL,NULL,NULL,NULL);
  6945.         debug(F100,tn_msg,"",0);
  6946.         if (tn_deb || debses) tn_debug(tn_msg);
  6947.     }
  6948. #endif /* DEBUG */
  6949.  
  6950.     if (tnc_wait("comport set baud rate") < 0) {
  6951.         tn_push();
  6952.         return(-1);
  6953.     }
  6954.     debug(F111,"tnc_set_baud","end",tnc_bps);
  6955.     return(tnc_bps);
  6956. }
  6957.  
  6958. int
  6959. #ifdef CK_ANSIC
  6960. tnc_get_baud(void)
  6961. #else /* CK_ANSIC */
  6962. tnc_get_baud()
  6963. #endif /* CK_ANSIC */
  6964. /* tnc_get_baud */ {
  6965.     /* send IAC SB COM-PORT SET-BAUD <value(4)=0> IAC SE  */
  6966.     /* wait for response */
  6967.     int i = 0;
  6968.  
  6969.     debug(F110,"tnc_get_baud","begin",0);
  6970.  
  6971.     if (ttnet != NET_TCPB) return(0);
  6972.     if (ttnproto != NP_TELNET) return(0);
  6973.  
  6974.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  6975.  
  6976. #ifdef CK_SSL
  6977.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  6978.         return(0);
  6979.     }
  6980. #endif /* CK_SSL */
  6981.  
  6982.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  6983.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  6984.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  6985.     sb[i++] = TNC_C2S_SET_BAUDRATE;   /* Set Baud Rate */
  6986.  
  6987.     if (tnc_bps_index > 0) {
  6988.         /* Access Server */
  6989.         sb[i++] = 0;
  6990.     } else {
  6991.         /* RFC 2217 */
  6992.         sb[i++] = 0;
  6993.         sb[i++] = 0;
  6994.         sb[i++] = 0;
  6995.         sb[i++] = 0;
  6996.     }
  6997.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  6998.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  6999.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7000.       return(-1);
  7001. #ifdef DEBUG
  7002.     if (deblog || tn_deb || debses) {
  7003.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7004.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7005.           " SET-BAUD-RATE ", ckltoa(0)," IAC SE", NULL,
  7006.           NULL,NULL,NULL,NULL,NULL,NULL);
  7007.         debug(F100,tn_msg,"",0);
  7008.         if (tn_deb || debses) tn_debug(tn_msg);
  7009.     }
  7010. #endif /* DEBUG */
  7011.  
  7012.     if (tnc_wait("comport get baud rate") < 0) {
  7013.         tn_push();
  7014.         return(-1);
  7015.     }
  7016.     debug(F111,"tnc_get_baud","end",tnc_bps);
  7017.     return(tnc_bps);
  7018. }
  7019.  
  7020. int
  7021. #ifdef CK_ANSIC
  7022. tnc_set_datasize(int datasize)
  7023. #else /* CK_ANSIC */
  7024. tnc_set_datasize(datasize) int datasize;
  7025. #endif /* CK_ANSIC */
  7026. /* tnc_set_datasize */ {
  7027.     /* IAC SB COM-PORT SET_DATASIZE <value(1)> IAC SE */
  7028.     /* Valid <value>s are 5 through 8 */
  7029.     /* Wait for response */
  7030.     /* return new host value */
  7031.  
  7032.     int i = 0;
  7033.  
  7034.     debug(F111,"tnc_set_datasize","begin",datasize);
  7035.  
  7036.     if (ttnet != NET_TCPB) return(0);
  7037.     if (ttnproto != NP_TELNET) return(0);
  7038.  
  7039.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7040.  
  7041. #ifdef CK_SSL
  7042.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7043.         return(0);
  7044.     }
  7045. #endif /* CK_SSL */
  7046.  
  7047.     if ( !(datasize >= 5 && datasize <= 8) )
  7048.         return(0);
  7049.  
  7050.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7051.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7052.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7053.     sb[i++] = TNC_C2S_SET_DATASIZE;   /* Set DataSize */
  7054.     sb[i++] = (unsigned char)(datasize & 0xFF);
  7055.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7056.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7057.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7058.       return(-1);
  7059. #ifdef DEBUG
  7060.     if (deblog || tn_deb || debses) {
  7061.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7062.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7063.           " SET-DATASIZE ", ckitoa(datasize)," IAC SE", NULL,
  7064.           NULL,NULL,NULL,NULL,NULL,NULL);
  7065.         debug(F100,tn_msg,"",0);
  7066.         if (tn_deb || debses) tn_debug(tn_msg);
  7067.     }
  7068. #endif /* DEBUG */
  7069.  
  7070.     if (tnc_wait("comport set datasize") < 0) {
  7071.         tn_push();
  7072.         return(-1);
  7073.     }
  7074.     debug(F111,"tnc_set_datasize","end",tnc_datasize);
  7075.     return(tnc_datasize);
  7076. }
  7077.  
  7078. int
  7079. #ifdef CK_ANSIC
  7080. tnc_get_datasize(void)
  7081. #else /* CK_ANSIC */
  7082. tnc_get_datasize()
  7083. #endif /* CK_ANSIC */
  7084. /* tnc_get_datasize */ {
  7085.     /* IAC SB COM-PORT SET_DATASIZE <value(1)=0> IAC SE */
  7086.     /* Wait for response */
  7087.     int i = 0;
  7088.  
  7089.     debug(F110,"tnc_get_datasize","begin",0);
  7090.  
  7091.     if (ttnet != NET_TCPB) return(0);
  7092.     if (ttnproto != NP_TELNET) return(0);
  7093.  
  7094.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7095.  
  7096. #ifdef CK_SSL
  7097.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7098.         return(0);
  7099.     }
  7100. #endif /* CK_SSL */
  7101.  
  7102.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7103.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7104.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7105.     sb[i++] = TNC_C2S_SET_DATASIZE;   /* Set DataSize */
  7106.     sb[i++] = 0;
  7107.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7108.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7109.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7110.       return(-1);
  7111. #ifdef DEBUG
  7112.     if (deblog || tn_deb || debses) {
  7113.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7114.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7115.           " SET-DATASIZE ", ckltoa(0)," IAC SE", NULL,
  7116.           NULL,NULL,NULL,NULL,NULL,NULL);
  7117.         debug(F100,tn_msg,"",0);
  7118.         if (tn_deb || debses) tn_debug(tn_msg);
  7119.     }
  7120. #endif /* DEBUG */
  7121.  
  7122.     if (tnc_wait("comport get datasize") < 0) {
  7123.         tn_push();
  7124.         return(-1);
  7125.     }
  7126.     debug(F111,"tnc_get_datasize","end",tnc_datasize);
  7127.     return(tnc_datasize);
  7128. }
  7129.  
  7130. int
  7131. #ifdef CK_ANSIC
  7132. tnc_set_parity(int parity)
  7133. #else /* CK_ANSIC */
  7134. tnc_set_parity(parity) int parity;
  7135. #endif /* CK_ANSIC */
  7136. /* tnc_set_parity */ {
  7137.     /* IAC SB COM-PORT SET_PARITY <value(1)> IAC SE */
  7138.     /*        Value     Parity
  7139.      *          1       None
  7140.      *          2       Odd
  7141.      *          3       Even
  7142.      *          4       Mark
  7143.      *          5       Space
  7144.      */
  7145.     /* Wait for response.  Return new host value. */
  7146.     int i = 0;
  7147.  
  7148.     debug(F110,"tnc_set_parity","begin",parity);
  7149.  
  7150.     if (ttnet != NET_TCPB) return(0);
  7151.     if (ttnproto != NP_TELNET) return(0);
  7152.  
  7153.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7154.  
  7155. #ifdef CK_SSL
  7156.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7157.         return(0);
  7158.     }
  7159. #endif /* CK_SSL */
  7160.  
  7161.     if ( !(parity >= 1 && parity <= 5) )
  7162.         return(0);
  7163.  
  7164.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7165.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7166.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7167.     sb[i++] = TNC_C2S_SET_PARITY;     /* Set Parity */
  7168.     sb[i++] = (unsigned char)(parity & 0xFF);
  7169.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7170.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7171.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7172.       return(-1);
  7173. #ifdef DEBUG
  7174.     if (deblog || tn_deb || debses) {
  7175.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7176.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7177.           " SET-PARITY ", ckitoa(parity)," IAC SE", NULL,
  7178.           NULL,NULL,NULL,NULL,NULL,NULL);
  7179.         debug(F100,tn_msg,"",0);
  7180.         if (tn_deb || debses) tn_debug(tn_msg);
  7181.     }
  7182. #endif /* DEBUG */
  7183.  
  7184.     if (tnc_wait("comport set parity") < 0) {
  7185.         tn_push();
  7186.         return(-1);
  7187.     }
  7188.     debug(F111,"tnc_set_parity","end",tnc_parity);
  7189.     return(tnc_parity);
  7190. }
  7191.  
  7192. int
  7193. #ifdef CK_ANSIC
  7194. tnc_get_parity(void)
  7195. #else /* CK_ANSIC */
  7196. tnc_get_parity()
  7197. #endif /* CK_ANSIC */
  7198. /* tnc_get_parity */ {
  7199.     /* IAC SB COM-PORT SET_PARITY <value(1)=0> IAC SE */
  7200.     /* wait for response */
  7201.     int i = 0;
  7202.  
  7203.     debug(F110,"tnc_get_parity","begin",0);
  7204.     if (ttnet != NET_TCPB) return(0);
  7205.     if (ttnproto != NP_TELNET) return(0);
  7206.  
  7207.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7208.  
  7209. #ifdef CK_SSL
  7210.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7211.         return(0);
  7212.     }
  7213. #endif /* CK_SSL */
  7214.  
  7215.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7216.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7217.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7218.     sb[i++] = TNC_C2S_SET_PARITY;     /* Set Parity */
  7219.     sb[i++] = 0;
  7220.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7221.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7222.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7223.       return(-1);
  7224. #ifdef DEBUG
  7225.     if (deblog || tn_deb || debses) {
  7226.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7227.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7228.           " SET-PARITY ", ckitoa(0)," IAC SE", NULL,
  7229.           NULL,NULL,NULL,NULL,NULL,NULL);
  7230.         debug(F100,tn_msg,"",0);
  7231.         if (tn_deb || debses) tn_debug(tn_msg);
  7232.     }
  7233. #endif /* DEBUG */
  7234.  
  7235.     if (tnc_wait("comport get parity") < 0) {
  7236.         tn_push();
  7237.         return(-1);
  7238.     }
  7239.     debug(F111,"tnc_get_parity","end",tnc_parity);
  7240.     return(tnc_parity);
  7241. }
  7242.  
  7243. int
  7244. #ifdef CK_ANSIC
  7245. tnc_set_stopsize(int stopsize)
  7246. #else /* CK_ANSIC */
  7247. tnc_set_stopsize(stopsize) int stopsize;
  7248. #endif /* CK_ANSIC */
  7249. /* tnc_set_stopsize */ {
  7250.     /* IAC SB COM-PORT SET_STOPSIZE <value(1)> IAC SE */
  7251.     /*        Value     Stop Bit Size
  7252.      *          1       1
  7253.      *          2       2
  7254.      *          3       1.5
  7255.      */
  7256.     /* Wait for response.  Return new host value. */
  7257.     int i = 0;
  7258.  
  7259.     debug(F111,"tnc_set_stopsize","begin",stopsize);
  7260.     if (ttnet != NET_TCPB) return(0);
  7261.     if (ttnproto != NP_TELNET) return(0);
  7262.  
  7263.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7264.  
  7265. #ifdef CK_SSL
  7266.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7267.         return(0);
  7268.     }
  7269. #endif /* CK_SSL */
  7270.  
  7271.     if (!(stopsize >= 1 && stopsize <= 3) )
  7272.       return(0);
  7273.  
  7274.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7275.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7276.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7277.     sb[i++] = TNC_C2S_SET_STOPSIZE;   /* Set Stop Bits */
  7278.     sb[i++] = (unsigned char)(stopsize & 0xFF);
  7279.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7280.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7281.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7282.       return(-1);
  7283. #ifdef DEBUG
  7284.     if (deblog || tn_deb || debses) {
  7285.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7286.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7287.           " SET-STOPSIZE ", ckitoa(stopsize)," IAC SE", NULL,
  7288.           NULL,NULL,NULL,NULL,NULL,NULL);
  7289.         debug(F100,tn_msg,"",0);
  7290.         if (tn_deb || debses) tn_debug(tn_msg);
  7291.     }
  7292. #endif /* DEBUG */
  7293.  
  7294.     if (tnc_wait("comport set stopsize") < 0) {
  7295.         tn_push();
  7296.         return(-1);
  7297.     }
  7298.     debug(F111,"tnc_set_stopsize","end",tnc_stopbit);
  7299.     return(tnc_stopbit);
  7300. }
  7301.  
  7302. int
  7303. #ifdef CK_ANSIC
  7304. tnc_get_stopsize(void)
  7305. #else /* CK_ANSIC */
  7306. tnc_get_stopsize()
  7307. #endif /* CK_ANSIC */
  7308. /* tnc_get_stopsize */ {
  7309.     /* IAC SB COM-PORT SET_STOPSIZE <value(1)=0> IAC SE */
  7310.     /* Wait for response */
  7311.     int i = 0;
  7312.  
  7313.     debug(F110,"tnc_get_stopsize","begin",0);
  7314.     if (ttnet != NET_TCPB) return(0);
  7315.     if (ttnproto != NP_TELNET) return(0);
  7316.  
  7317.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7318.  
  7319. #ifdef CK_SSL
  7320.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7321.         return(0);
  7322.     }
  7323. #endif /* CK_SSL */
  7324.  
  7325.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7326.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7327.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7328.     sb[i++] = TNC_C2S_SET_STOPSIZE;   /* Set Stop Bits */
  7329.     sb[i++] = 0;
  7330.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7331.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7332.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7333.       return(-1);
  7334. #ifdef DEBUG
  7335.     if (deblog || tn_deb || debses) {
  7336.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7337.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7338.           " SET-STOPSIZE ", ckitoa(0)," IAC SE", NULL,
  7339.           NULL,NULL,NULL,NULL,NULL,NULL);
  7340.         debug(F100,tn_msg,"",0);
  7341.         if (tn_deb || debses) tn_debug(tn_msg);
  7342.     }
  7343. #endif /* DEBUG */
  7344.  
  7345.     if (tnc_wait("comport set stopsize") < 0) {
  7346.         tn_push();
  7347.         return(-1);
  7348.     }
  7349.     debug(F111,"tnc_get_stopsize","end",tnc_stopbit);
  7350.     return(tnc_stopbit);
  7351. }
  7352.  
  7353. int
  7354. #ifdef CK_ANSIC
  7355. tnc_set_oflow(int control)
  7356. #else /* CK_ANSIC */
  7357. tnc_set_oflow(control) int control;
  7358. #endif /* CK_ANSIC */
  7359. /* tnc_set_oflow */ {
  7360.     /* IAC SB COM_PORT SET_CONTROL <value(1)> IAC SE */
  7361.     /*        Value     Flow Control
  7362.      *          1       No Flow Control
  7363.      *          2       Xon/Xoff
  7364.      *          3       Rts/Cts
  7365.      *         17       DCD
  7366.      *         19       DSR
  7367.      */
  7368.     /* wait for response, return new host value. */
  7369.     int i = 0;
  7370.  
  7371.     debug(F111,"tnc_set_oflow","begin",control);
  7372.     if (ttnet != NET_TCPB) return(0);
  7373.     if (ttnproto != NP_TELNET) return(0);
  7374.  
  7375.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7376.  
  7377. #ifdef CK_SSL
  7378.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7379.         return(0);
  7380.     }
  7381. #endif /* CK_SSL */
  7382.  
  7383.     if (control != 1 && control != 2 && control != 3 &&
  7384.     control != 17 && control != 19)
  7385.       return(0);
  7386.  
  7387.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7388.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7389.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7390.     sb[i++] = TNC_C2S_SET_CONTROL;    /* Set Control */
  7391.     sb[i++] = (unsigned char)(control & 0xFF);
  7392.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7393.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7394.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7395.       return(-1);
  7396. #ifdef DEBUG
  7397.     if (deblog || tn_deb || debses) {
  7398.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7399.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7400.           " SET-CONTROL ", ckitoa(control)," IAC SE", NULL,
  7401.           NULL,NULL,NULL,NULL,NULL,NULL);
  7402.         debug(F100,tn_msg,"",0);
  7403.         if (tn_deb || debses) tn_debug(tn_msg);
  7404.     }
  7405. #endif /* DEBUG */
  7406.  
  7407.     if (tnc_wait("comport set outbound flow control") < 0) {
  7408.         tn_push();
  7409.         return(-1);
  7410.     }
  7411.     debug(F111,"tnc_set_oflow","end",tnc_oflow);
  7412.     return(tnc_oflow);
  7413. }
  7414.  
  7415. int
  7416. #ifdef CK_ANSIC
  7417. tnc_get_oflow(void)
  7418. #else /* CK_ANSIC */
  7419. tnc_get_oflow()
  7420. #endif /* CK_ANSIC */
  7421. /* tnc_get_oflow */ {
  7422.     /* IAC SB COM_PORT SET_CONTROL <value(1)=0> IAC SE */
  7423.     /* wait for response */
  7424.     int i = 0;
  7425.  
  7426.     debug(F110,"tnc_get_oflow","begin",0);
  7427.     if (ttnet != NET_TCPB) return(0);
  7428.     if (ttnproto != NP_TELNET) return(0);
  7429.  
  7430.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7431.  
  7432. #ifdef CK_SSL
  7433.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7434.         return(0);
  7435.     }
  7436. #endif /* CK_SSL */
  7437.  
  7438.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7439.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7440.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7441.     sb[i++] = TNC_C2S_SET_CONTROL;    /* Set Control */
  7442.     sb[i++] = TNC_CTL_OFLOW_REQUEST;
  7443.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7444.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7445.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7446.       return(-1);
  7447. #ifdef DEBUG
  7448.     if (deblog || tn_deb || debses) {
  7449.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7450.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7451.           " SET-CONTROL ",
  7452.                    ckitoa(TNC_CTL_OFLOW_REQUEST),
  7453.                    " IAC SE", NULL,
  7454.           NULL,NULL,NULL,NULL,NULL,NULL);
  7455.         debug(F100,tn_msg,"",0);
  7456.         if (tn_deb || debses) tn_debug(tn_msg);
  7457.     }
  7458. #endif /* DEBUG */
  7459.  
  7460.     if (tnc_wait("comport get outbound flow control") < 0) {
  7461.         tn_push();
  7462.         return(-1);
  7463.     }
  7464.     debug(F111,"tnc_get_oflow","end",tnc_oflow);
  7465.     return(tnc_oflow);
  7466. }
  7467.  
  7468. int
  7469. #ifdef CK_ANSIC
  7470. tnc_set_iflow(int control)
  7471. #else /* CK_ANSIC */
  7472. tnc_set_iflow(control) int control;
  7473. #endif /* CK_ANSIC */
  7474. /* tnc_set_iflow */ {
  7475.     /* IAC SB COM_PORT SET_CONTROL <value(1)> IAC SE */
  7476.     /*        Value     Flow Control
  7477.      *         14       No Flow Control
  7478.      *         15       Xon/Xoff
  7479.      *         16       Rts/Cts
  7480.      *         18       DTR
  7481.      */
  7482.     /* wait for response, return new host value. */
  7483.     int i = 0;
  7484.  
  7485.     debug(F111,"tnc_set_iflow","begin",control);
  7486.     if (ttnet != NET_TCPB) return(0);
  7487.     if (ttnproto != NP_TELNET) return(0);
  7488.  
  7489.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7490.  
  7491. #ifdef CK_SSL
  7492.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7493.         return(0);
  7494.     }
  7495. #endif /* CK_SSL */
  7496.  
  7497.     if (control != 14 && control != 15 && control != 16 && control != 18)
  7498.       return(0);
  7499.  
  7500.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7501.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7502.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7503.     sb[i++] = TNC_C2S_SET_CONTROL;    /* Set Control */
  7504.     sb[i++] = (unsigned char)(control & 0xFF);
  7505.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7506.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7507.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7508.       return(-1);
  7509. #ifdef DEBUG
  7510.     if (deblog || tn_deb || debses) {
  7511.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7512.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7513.           " SET-CONTROL ", ckitoa(control)," IAC SE", NULL,
  7514.           NULL,NULL,NULL,NULL,NULL,NULL);
  7515.         debug(F100,tn_msg,"",0);
  7516.         if (tn_deb || debses) tn_debug(tn_msg);
  7517.     }
  7518. #endif /* DEBUG */
  7519.  
  7520.     if (tnc_wait("comport set inbound flow control") < 0) {
  7521.         tn_push();
  7522.         return(-1);
  7523.     }
  7524.     debug(F111,"tnc_set_iflow","end",tnc_iflow);
  7525.     return(tnc_iflow);
  7526. }
  7527.  
  7528. int
  7529. #ifdef CK_ANSIC
  7530. tnc_get_iflow(void)
  7531. #else /* CK_ANSIC */
  7532. tnc_get_iflow()
  7533. #endif /* CK_ANSIC */
  7534. /* tnc_get_iflow */ {
  7535.     /* IAC SB COM_PORT SET_CONTROL <value(1)=13> IAC SE */
  7536.     /* wait for response */
  7537.     int i = 0;
  7538.  
  7539.     debug(F110,"tnc_get_iflow","begin",0);
  7540.     if (ttnet != NET_TCPB) return(0);
  7541.     if (ttnproto != NP_TELNET) return(0);
  7542.  
  7543.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7544.  
  7545. #ifdef CK_SSL
  7546.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7547.         return(0);
  7548.     }
  7549. #endif /* CK_SSL */
  7550.  
  7551.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7552.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7553.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7554.     sb[i++] = TNC_C2S_SET_CONTROL;    /* Set Control */
  7555.     sb[i++] = TNC_CTL_IFLOW_REQUEST;
  7556.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7557.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7558.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7559.       return(-1);
  7560. #ifdef DEBUG
  7561.     if (deblog || tn_deb || debses) {
  7562.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7563.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7564.           " SET-CONTROL ",
  7565.           ckitoa(TNC_CTL_IFLOW_REQUEST),
  7566.           " IAC SE", NULL,
  7567.           NULL,NULL,NULL,NULL,NULL,NULL);
  7568.         debug(F100,tn_msg,"",0);
  7569.         if (tn_deb || debses) tn_debug(tn_msg);
  7570.     }
  7571. #endif /* DEBUG */
  7572.  
  7573.     if (tnc_wait("comport get inbound flow control") < 0) {
  7574.         tn_push();
  7575.         return(-1);
  7576.     }
  7577.     debug(F111,"tnc_get_iflow","end",tnc_iflow);
  7578.     return(tnc_iflow);
  7579. }
  7580.  
  7581. int
  7582. #ifdef CK_ANSIC
  7583. tnc_set_break_state(int onoff)
  7584. #else /* CK_ANSIC */
  7585. tnc_set_break_state(onoff) int onoff;
  7586. #endif /* CK_ANSIC */
  7587. /* tnc_set_break_state */ {
  7588.     /* IAC SB COM_PORT SET_CONTROL <value(1)> IAC SE */
  7589.     /*        Value     Break State
  7590.      *          5       On
  7591.      *          6       Off
  7592.      */
  7593.     /* wait for response, return new host value. */
  7594.     int i = 0;
  7595.  
  7596.     debug(F111,"tnc_set_break_state","begin",onoff);
  7597.     if (ttnet != NET_TCPB) return(0);
  7598.     if (ttnproto != NP_TELNET) return(0);
  7599.  
  7600.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7601.  
  7602. #ifdef CK_SSL
  7603.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7604.         return(0);
  7605.     }
  7606. #endif /* CK_SSL */
  7607.  
  7608.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7609.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7610.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7611.     sb[i++] = TNC_C2S_SET_CONTROL;    /* Set Control */
  7612.     sb[i++] = onoff ?
  7613.       TNC_CTL_BREAK_ON : TNC_CTL_BREAK_OFF;
  7614.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7615.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7616.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7617.       return(-1);
  7618. #ifdef DEBUG
  7619.     if (deblog || tn_deb || debses) {
  7620.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7621.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7622.           " SET-CONTROL ",
  7623.           onoff ? "BREAK-ON" : "BREAK-OFF",
  7624.           " IAC SE", NULL,
  7625.           NULL,NULL,NULL,NULL,NULL,NULL);
  7626.         debug(F100,tn_msg,"",0);
  7627.         if (tn_deb || debses) tn_debug(tn_msg);
  7628.     }
  7629. #endif /* DEBUG */
  7630.  
  7631.     if (tnc_wait("comport set break state") < 0) {
  7632.         tn_push();
  7633.         return(-1);
  7634.     }
  7635.     debug(F111,"tnc_set_break_state","end",tnc_break);
  7636.     return(tnc_break);
  7637. }
  7638.  
  7639. int
  7640. #ifdef CK_ANSIC
  7641. tnc_get_break_state(void)
  7642. #else /* CK_ANSIC */
  7643. tnc_get_break_state()
  7644. #endif /* CK_ANSIC */
  7645. /* tnc_get_break_state */ {
  7646.     /* IAC SB COM_PORT SET_CONTROL <value(1)=4> IAC SE */
  7647.     /* wait for response */
  7648.     int i = 0;
  7649.  
  7650.     debug(F110,"tnc_get_break_state","begin",0);
  7651.     if (ttnet != NET_TCPB) return(0);
  7652.     if (ttnproto != NP_TELNET) return(0);
  7653.  
  7654.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7655.  
  7656. #ifdef CK_SSL
  7657.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7658.         return(0);
  7659.     }
  7660. #endif /* CK_SSL */
  7661.  
  7662.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7663.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7664.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7665.     sb[i++] = TNC_C2S_SET_CONTROL;    /* Set Control */
  7666.     sb[i++] = TNC_CTL_BREAK_REQUEST;
  7667.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7668.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7669.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7670.       return(-1);
  7671. #ifdef DEBUG
  7672.     if (deblog || tn_deb || debses) {
  7673.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7674.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7675.           " SET-CONTROL ",
  7676.           "BREAK-REQUEST",
  7677.           " IAC SE", NULL,
  7678.           NULL,NULL,NULL,NULL,NULL,NULL);
  7679.         debug(F100,tn_msg,"",0);
  7680.         if (tn_deb || debses) tn_debug(tn_msg);
  7681.     }
  7682. #endif /* DEBUG */
  7683.  
  7684.     if (tnc_wait("comport get break state") < 0) {
  7685.         tn_push();
  7686.         return(-1);
  7687.     }
  7688.     debug(F111,"tnc_get_break_state","end",tnc_break);
  7689.     return(tnc_break);
  7690. }
  7691.  
  7692. int
  7693. #ifdef CK_ANSIC
  7694. tnc_set_dtr_state(int onoff)
  7695. #else /* CK_ANSIC */
  7696. tnc_set_dtr_state(onoff) int onoff;
  7697. #endif /* CK_ANSIC */
  7698. /* tnc_set_dtr_state */ {
  7699.     /* IAC SB COM_PORT SET_CONTROL <value(1)> IAC SE */
  7700.     /*        Value     Dtr State
  7701.      *          8       On
  7702.      *          9       Off
  7703.      */
  7704.     /* wait for response, return new host value. */
  7705.     int i = 0;
  7706.  
  7707.     debug(F111,"tnc_set_dtr_state","begin",onoff);
  7708.     if (ttnet != NET_TCPB) return(0);
  7709.     if (ttnproto != NP_TELNET) return(0);
  7710.  
  7711.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7712.  
  7713. #ifdef CK_SSL
  7714.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7715.         return(0);
  7716.     }
  7717. #endif /* CK_SSL */
  7718.  
  7719.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7720.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7721.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7722.     sb[i++] = TNC_C2S_SET_CONTROL;    /* Set Control */
  7723.     sb[i++] = onoff ?
  7724.         TNC_CTL_DTR_ON : TNC_CTL_DTR_OFF;
  7725.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7726.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7727.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7728.       return(-1);
  7729. #ifdef DEBUG
  7730.     if (deblog || tn_deb || debses) {
  7731.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7732.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7733.           " SET-CONTROL ",
  7734.           onoff ? "DTR-ON" : "DTR-OFF",
  7735.           " IAC SE", NULL,
  7736.           NULL,NULL,NULL,NULL,NULL,NULL);
  7737.         debug(F100,tn_msg,"",0);
  7738.         if (tn_deb || debses) tn_debug(tn_msg);
  7739.     }
  7740. #endif /* DEBUG */
  7741.  
  7742.     if (tnc_wait("comport set dtr state") < 0) {
  7743.         tn_push();
  7744.         return(-1);
  7745.     }
  7746.     debug(F111,"tnc_set_dtr_state","end",tnc_dtr);
  7747.     return(tnc_dtr);
  7748. }
  7749.  
  7750. int
  7751. #ifdef CK_ANSIC
  7752. tnc_get_dtr_state(void)
  7753. #else /* CK_ANSIC */
  7754. tnc_get_dtr_state()
  7755. #endif /* CK_ANSIC */
  7756. /* tnc_get_dtr_state */ {
  7757.     /* IAC SB COM_PORT SET_CONTROL <value(1)=7> IAC SE */
  7758.     /* wait for response */
  7759.     int i = 0;
  7760.  
  7761.     debug(F110,"tnc_get_dtr_state","begin",0);
  7762.     if (ttnet != NET_TCPB) return(0);
  7763.     if (ttnproto != NP_TELNET) return(0);
  7764.  
  7765.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7766.  
  7767. #ifdef CK_SSL
  7768.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7769.         return(0);
  7770.     }
  7771. #endif /* CK_SSL */
  7772.  
  7773.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7774.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7775.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7776.     sb[i++] = TNC_C2S_SET_CONTROL;    /* Set Control */
  7777.     sb[i++] = TNC_CTL_DTR_REQUEST;
  7778.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7779.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7780.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7781.       return(-1);
  7782. #ifdef DEBUG
  7783.     if (deblog || tn_deb || debses) {
  7784.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7785.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7786.           " SET-CONTROL ",
  7787.                   "DTR-REQUEST",
  7788.           " IAC SE", NULL,
  7789.           NULL,NULL,NULL,NULL,NULL,NULL);
  7790.         debug(F100,tn_msg,"",0);
  7791.         if (tn_deb || debses) tn_debug(tn_msg);
  7792.     }
  7793. #endif /* DEBUG */
  7794.  
  7795.     if (tnc_wait("comport get dtr state") < 0) {
  7796.         tn_push();
  7797.         return(-1);
  7798.     }
  7799.     debug(F111,"tnc_get_dtr_state","end",tnc_dtr);
  7800.     return(tnc_dtr);
  7801. }
  7802.  
  7803. int
  7804. #ifdef CK_ANSIC
  7805. tnc_set_rts_state(int onoff)
  7806. #else /* CK_ANSIC */
  7807. tnc_set_rts_state(onoff) int onoff;
  7808. #endif /* CK_ANSIC */
  7809. /* tnc_set_rts_state */ {
  7810.     /* IAC SB COM_PORT SET_CONTROL <value(1)> IAC SE */
  7811.     /*        Value     Rts State
  7812.      *          5       On
  7813.      *          6       Off
  7814.      */
  7815.     /* wait for response, return new host value. */
  7816.     int i = 0;
  7817.  
  7818.     debug(F111,"tnc_set_rts_state","begin",onoff);
  7819.     if (ttnet != NET_TCPB) return(0);
  7820.     if (ttnproto != NP_TELNET) return(0);
  7821.  
  7822.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7823.  
  7824. #ifdef CK_SSL
  7825.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7826.         return(0);
  7827.     }
  7828. #endif /* CK_SSL */
  7829.  
  7830.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7831.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7832.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7833.     sb[i++] = TNC_C2S_SET_CONTROL;    /* Set Control */
  7834.     sb[i++] = onoff ?
  7835.       TNC_CTL_RTS_ON : TNC_CTL_RTS_OFF;
  7836.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7837.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7838.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7839.       return(-1);
  7840. #ifdef DEBUG
  7841.     if (deblog || tn_deb || debses) {
  7842.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7843.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7844.           " SET-CONTROL ",
  7845.           onoff ? "RTS-ON" : "RTS-OFF",
  7846.           " IAC SE", NULL,
  7847.           NULL,NULL,NULL,NULL,NULL,NULL);
  7848.         debug(F100,tn_msg,"",0);
  7849.         if (tn_deb || debses) tn_debug(tn_msg);
  7850.     }
  7851. #endif /* DEBUG */
  7852.  
  7853.     if (tnc_wait("comport set rts state") < 0) {
  7854.         tn_push();
  7855.         return(-1);
  7856.     }
  7857.     debug(F111,"tnc_set_rts_state","end",tnc_rts);
  7858.     return(tnc_rts);
  7859. }
  7860.  
  7861. int
  7862. #ifdef CK_ANSIC
  7863. tnc_get_rts_state(void)
  7864. #else /* CK_ANSIC */
  7865. tnc_get_rts_state()
  7866. #endif /* CK_ANSIC */
  7867. /* tnc_get_rts_state */ {
  7868.     /* IAC SB COM_PORT SET_CONTROL <value(1)=10> IAC SE */
  7869.     /* wait for response */
  7870.     int i = 0;
  7871.  
  7872.     debug(F110,"tnc_get_rts_state","begin",0);
  7873.     if (ttnet != NET_TCPB) return(0);
  7874.     if (ttnproto != NP_TELNET) return(0);
  7875.  
  7876.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7877.  
  7878. #ifdef CK_SSL
  7879.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7880.         return(0);
  7881.     }
  7882. #endif /* CK_SSL */
  7883.  
  7884.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7885.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7886.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7887.     sb[i++] = TNC_C2S_SET_CONTROL;    /* Set Control */
  7888.     sb[i++] = TNC_CTL_RTS_REQUEST;
  7889.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7890.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7891.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7892.       return(-1);
  7893. #ifdef DEBUG
  7894.     if (deblog || tn_deb || debses) {
  7895.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7896.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7897.           " SET-CONTROL ",
  7898.           "RTS-REQUEST",
  7899.           " IAC SE", NULL,
  7900.           NULL,NULL,NULL,NULL,NULL,NULL);
  7901.         debug(F100,tn_msg,"",0);
  7902.         if (tn_deb || debses) tn_debug(tn_msg);
  7903.     }
  7904. #endif /* DEBUG */
  7905.  
  7906.     if (tnc_wait("comport get rts state") < 0) {
  7907.         tn_push();
  7908.         return(-1);
  7909.     }
  7910.     debug(F111,"tnc_get_rts_state","end",tnc_rts);
  7911.     return(tnc_rts);
  7912. }
  7913.  
  7914. int
  7915. #ifdef CK_ANSIC
  7916. tnc_set_ls_mask(int mask)
  7917. #else /* CK_ANSIC */
  7918. tnc_set_ls_mask(mask) int mask;
  7919. #endif /* CK_ANSIC */
  7920. /* tnc_set_ls_mask */ {
  7921.     /* IAC SB COM_PORT SET_LINESTATE_MASK <value(1)> IAC SE */
  7922.     /*        Bit       Meaning
  7923.      *          0       Data Ready
  7924.      *          1       Overrun Error
  7925.      *          2       Parity Error
  7926.      *          3       Framing Error
  7927.      *          4       Break Detect Error
  7928.      *          5       Transfer Holding Register Empty
  7929.      *          6       Transfer Shift Register Empty
  7930.      *          7       Timeout Error
  7931.      */
  7932.     int i = 0;
  7933.  
  7934.     debug(F111,"tnc_set_ls_mask","begin",mask);
  7935.     if (ttnet != NET_TCPB) return(0);
  7936.     if (ttnproto != NP_TELNET) return(0);
  7937.  
  7938.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  7939.  
  7940. #ifdef CK_SSL
  7941.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  7942.         return(0);
  7943.     }
  7944. #endif /* CK_SSL */
  7945.  
  7946.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  7947.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  7948.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  7949.     sb[i++] = TNC_C2S_SET_LS_MASK;
  7950.     sb[i++] = (unsigned char)(mask & 0xFF);
  7951.     if (sb[i-1] == IAC )
  7952.       sb[i++] = IAC;
  7953.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  7954.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  7955.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  7956.       return(-1);
  7957. #ifdef DEBUG
  7958.     if (deblog || tn_deb || debses) {
  7959.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  7960.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  7961.           " SET-LINESTATE-MASK ",
  7962.           ckitoa(mask & 0xFF),
  7963.           " IAC SE", NULL,
  7964.           NULL,NULL,NULL,NULL,NULL,NULL);
  7965.         debug(F100,tn_msg,"",0);
  7966.         if (tn_deb || debses) tn_debug(tn_msg);
  7967.     }
  7968. #endif /* DEBUG */
  7969.  
  7970.     tnc_ls_mask = mask;
  7971.     debug(F111,"tnc_set_ls_mask","end",tnc_ls_mask);
  7972.     return(0);
  7973. }
  7974.  
  7975. int
  7976. #ifdef CK_ANSIC
  7977. tnc_get_ls_mask(void)
  7978. #else /* CK_ANSIC */
  7979. tnc_get_ls_mask()
  7980. #endif /* CK_ANSIC */
  7981. /* tnc_get_ls_mask */ {
  7982.     debug(F101,"tnc_get_ls_mask","",tnc_ls_mask);
  7983.     return(tnc_ls_mask);
  7984. }
  7985.  
  7986. int
  7987. #ifdef CK_ANSIC
  7988. tnc_get_ls(void)
  7989. #else /* CK_ANSIC */
  7990. tnc_get_ls()
  7991. #endif /* CK_ANSIC */
  7992. /* tnc_get_ls */ {
  7993.     int ls = tnc_ls;
  7994.     debug(F111,"tnc_get_ls","begin",tnc_ls);
  7995.     tnc_ls = 0;
  7996.     debug(F111,"tnc_get_ls","end",tnc_ls);
  7997.     return(ls);
  7998. }
  7999.  
  8000. int
  8001. #ifdef CK_ANSIC
  8002. tnc_set_ms_mask(int mask)
  8003. #else /* CK_ANSIC */
  8004. tnc_set_ms_mask(mask) int mask;
  8005. #endif /* CK_ANSIC */
  8006. /* tnc_set_ms_mask */ {
  8007.     /* IAC SB COM_PORT SET_MODEMSTATE_MASK <value(1)> IAC SE */
  8008.     /*        Bit       Meaning
  8009.      *          0       Delta Clear To Send
  8010.      *          1       Delta Data Set Ready
  8011.      *          2       Trailing Edge Ring Detector
  8012.      *          3       Delta Receive Line Signal (Carrier) Detect
  8013.      *          4       Clear To Send Signal State
  8014.      *          5       Data-Set-Ready Signal State
  8015.      *          6       Ring Indicator
  8016.      *          7       Receive Line Signal (Carrier) Detect
  8017.      */
  8018.  
  8019.     int i = 0;
  8020.  
  8021.     debug(F111,"tnc_set_ms_mask","begin",mask);
  8022.     if (ttnet != NET_TCPB) return(0);
  8023.     if (ttnproto != NP_TELNET) return(0);
  8024.  
  8025.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  8026.  
  8027. #ifdef CK_SSL
  8028.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  8029.         return(0);
  8030.     }
  8031. #endif /* CK_SSL */
  8032.  
  8033.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  8034.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  8035.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  8036.     sb[i++] = TNC_C2S_SET_MS_MASK;
  8037.     sb[i++] = (unsigned char)(mask & 0xFF);
  8038.     if (sb[i-1] == IAC )
  8039.       sb[i++] = IAC;
  8040.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  8041.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  8042.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  8043.       return(-1);
  8044. #ifdef DEBUG
  8045.     if (deblog || tn_deb || debses) {
  8046.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  8047.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  8048.           " SET-MODEMSTATE-MASK ",
  8049.           ckitoa(mask & 0xFF),
  8050.           " IAC SE", NULL,
  8051.           NULL,NULL,NULL,NULL,NULL,NULL);
  8052.         debug(F100,tn_msg,"",0);
  8053.         if (tn_deb || debses) tn_debug(tn_msg);
  8054.     }
  8055. #endif /* DEBUG */
  8056.  
  8057.     tnc_ms_mask = mask;
  8058.     debug(F111,"tnc_set_ms_mask","end",tnc_ms_mask);
  8059.     return(0);
  8060. }
  8061.  
  8062. int
  8063. #ifdef CK_ANSIC
  8064. tnc_get_ms_mask(void)
  8065. #else /* CK_ANSIC */
  8066. tnc_get_ms_mask()
  8067. #endif /* CK_ANSIC */
  8068. /* tnc_get_ms_mask */ {
  8069.     debug(F101,"tnc_get_gs_mask","",tnc_ms_mask);
  8070.     return(tnc_ms_mask);
  8071. }
  8072.  
  8073. int
  8074. #ifdef CK_ANSIC
  8075. tnc_get_ms(void)
  8076. #else /* CK_ANSIC */
  8077. tnc_get_ms()
  8078. #endif /* CK_ANSIC */
  8079. /* tnc_get_ms */ {
  8080.     int ms = tnc_ms;
  8081.     debug(F111,"tnc_get_ms","begin",tnc_ms);
  8082.     tnc_ms = 0;
  8083.     debug(F111,"tnc_get_ms_mask","end",tnc_ms);
  8084.     return(ms);
  8085. }
  8086.  
  8087. int
  8088. #ifdef CK_ANSIC
  8089. tnc_send_purge_data(int mode)
  8090. #else /* CK_ANSIC */
  8091. tnc_send_purge_data(mode) int mode;
  8092. #endif /* CK_ANSIC */
  8093. /* tnc_send_purge_data */ {
  8094.     /* IAC SB COM_PORT PURGE_DATA <value(1)> IAC SE */
  8095.     /*        Value     Meaning
  8096.      *          1       Purge access server receive data buffer
  8097.      *          2       Purge access server transmit data buffer
  8098.      *          3       Purge access server receive and transmit data buffers
  8099.      */
  8100.     /* No response */
  8101.     int i = 0;
  8102.  
  8103.     debug(F111,"tnc_send_purge_data","begin",mode);
  8104.     if (ttnet != NET_TCPB) return(0);
  8105.     if (ttnproto != NP_TELNET) return(0);
  8106.  
  8107.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  8108.  
  8109. #ifdef CK_SSL
  8110.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  8111.         return(0);
  8112.     }
  8113. #endif /* CK_SSL */
  8114.  
  8115.     if ( !(mode >= 1 && mode <= 3) )
  8116.         return(0);
  8117.  
  8118.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  8119.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  8120.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  8121.     sb[i++] = TNC_C2S_PURGE;
  8122.     sb[i++] = (unsigned char)(mode & 0xFF);
  8123.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  8124.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  8125.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  8126.       return(-1);
  8127. #ifdef DEBUG
  8128.     if (deblog || tn_deb || debses) {
  8129.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  8130.           "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  8131.           " PURGE-DATA ",
  8132.           ckitoa(mode & 0xFF),
  8133.           " IAC SE", NULL,
  8134.           NULL,NULL,NULL,NULL,NULL,NULL);
  8135.         debug(F100,tn_msg,"",0);
  8136.         if (tn_deb || debses) tn_debug(tn_msg);
  8137.     }
  8138. #endif /* DEBUG */
  8139.     debug(F110,"tnc_send_purge_data","end",0);
  8140.     return(0);
  8141. }
  8142.  
  8143. int
  8144. #ifdef CK_ANSIC
  8145. tnc_flow_suspended(void)
  8146. #else /* CK_ANSIC */
  8147. tnc_flow_suspended()
  8148. #endif /* CK_ANSIC */
  8149. /* tnc_flow_suspended */ {
  8150.     debug(F111,"tnc_flow_suspended","",tnc_suspend_xmit);
  8151.     return(tnc_suspend_xmit);
  8152. }
  8153.  
  8154. int
  8155. #ifdef CK_ANSIC
  8156. tnc_suspend_flow(void)
  8157. #else /* CK_ANSIC */
  8158. tnc_suspend_flow()
  8159. #endif /* CK_ANSIC */
  8160. /* tnc_suspend_flow */ {
  8161.     /* IAC SB COM_PORT FLOWCONTROL_SUSPEND IAC SE */
  8162.     int i = 0;
  8163.  
  8164.     debug(F110,"tnc_suspend_flow","begin",0);
  8165.     if (ttnet != NET_TCPB) return(0);
  8166.     if (ttnproto != NP_TELNET) return(0);
  8167.  
  8168.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  8169.  
  8170. #ifdef CK_SSL
  8171.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  8172.         return(0);
  8173.     }
  8174. #endif /* CK_SSL */
  8175.  
  8176.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  8177.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  8178.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  8179.     sb[i++] = TNC_C2S_FLOW_SUSPEND;
  8180.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  8181.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  8182.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  8183.       return(-1);
  8184. #ifdef DEBUG
  8185.     if (deblog || tn_deb || debses) {
  8186.         ckmakmsg(tn_msg,TN_MSG_LEN,
  8187.          "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  8188.          " FLOWCONTROL-SUSPEND IAC SE", NULL);
  8189.         debug(F100,tn_msg,"",0);
  8190.         if (tn_deb || debses) tn_debug(tn_msg);
  8191.     }
  8192. #endif /* DEBUG */
  8193.     debug(F110,"tnc_suspend_flow","end",0);
  8194.     return(0);
  8195. }
  8196.  
  8197. int
  8198. #ifdef CK_ANSIC
  8199. tnc_resume_flow(void)
  8200. #else /* CK_ANSIC */
  8201. tnc_resume_flow()
  8202. #endif /* CK_ANSIC */
  8203. /* tnc_resume_flow */ {
  8204.     /* IAC SB COM_PORT FLOWCONTROL_RESUME IAC SE */
  8205.     int i = 0;
  8206.  
  8207.     debug(F110,"tnc_resume_flow","begin",0);
  8208.     if (ttnet != NET_TCPB) return(0);
  8209.     if (ttnproto != NP_TELNET) return(0);
  8210.  
  8211.     if (!TELOPT_ME(TELOPT_COMPORT)) return(0);
  8212.  
  8213. #ifdef CK_SSL
  8214.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  8215.         return(0);
  8216.     }
  8217. #endif /* CK_SSL */
  8218.  
  8219.     sb[i++] = (CHAR) IAC;                 /* I Am a Command */
  8220.     sb[i++] = (CHAR) SB;                  /* Subnegotiation */
  8221.     sb[i++] = TELOPT_COMPORT;             /* ComPort */
  8222.     sb[i++] = TNC_C2S_FLOW_RESUME;
  8223.     sb[i++] = (CHAR) IAC;                 /* End of Subnegotiation */
  8224.     sb[i++] = (CHAR) SE;                  /* marked by IAC SE */
  8225.     if (ttol((CHAR *)sb,i) < 0)           /* Send it. */
  8226.       return(-1);
  8227. #ifdef DEBUG
  8228.     if (deblog || tn_deb || debses) {
  8229.         ckmakmsg(tn_msg,TN_MSG_LEN,
  8230.          "TELNET SENT SB ",TELOPT(TELOPT_COMPORT),
  8231.          " FLOWCONTROL-RESUME IAC SE", NULL);
  8232.         debug(F100,tn_msg,"",0);
  8233.         if (tn_deb || debses) tn_debug(tn_msg);
  8234.     }
  8235. #endif /* DEBUG */
  8236.     debug(F110,"tnc_resume_flow","end",0);
  8237.     return(0);
  8238. }
  8239.  
  8240. int
  8241. #ifdef CK_ANSIC
  8242. tnsetflow(int nflow)
  8243. #else
  8244. tnsetflow(nflow) int nflow;
  8245. #endif /* CK_ANSIC */
  8246. /* tnsetflow */ {
  8247.  
  8248.     int rc = -1;
  8249.  
  8250.     debug(F111,"tnsetflow","begin",nflow);
  8251.     if (ttnet != NET_TCPB || ttnproto != NP_TELNET)
  8252.         return(-1);
  8253.  
  8254.     if (TELOPT_ME(TELOPT_COMPORT)) {
  8255.         switch(nflow) {
  8256.       case FLO_XONX:
  8257.             rc = tnc_set_oflow(
  8258.          TNC_CTL_OFLOW_XON_XOFF
  8259.          );
  8260.             if (rc >= 0)
  8261.           rc = tnc_set_iflow(
  8262.                    TNC_CTL_IFLOW_XON_XOFF
  8263.                    );
  8264.             break;
  8265.       case FLO_RTSC:
  8266.             rc = tnc_set_oflow(
  8267.                  TNC_CTL_OFLOW_RTS_CTS
  8268.                  );
  8269.             if (rc >= 0)
  8270.           rc = tnc_set_iflow(
  8271.                    TNC_CTL_IFLOW_RTS_CTS
  8272.                    );
  8273.             break;
  8274.       case FLO_KEEP:
  8275.             /* leave things exactly as they are */
  8276.             rc = 0;
  8277.             break;
  8278.       case FLO_NONE:
  8279.           case FLO_DIAL:  /* dialing hack */
  8280.           case FLO_DIAX:  /* cancel dialing hack */
  8281.             rc = tnc_set_oflow(
  8282.                  TNC_CTL_OFLOW_NONE
  8283.                  );
  8284.             if (rc >= 0)
  8285.           rc = tnc_set_iflow(
  8286.                    TNC_CTL_IFLOW_NONE
  8287.                    );
  8288.             break;
  8289.           case FLO_DTRC:
  8290.           case FLO_ETXA:
  8291.           case FLO_STRG:
  8292.           case FLO_DTRT:
  8293.           default:
  8294.             /* not supported */
  8295.             rc = -1;
  8296.             break;
  8297.         }
  8298.     }
  8299.     debug(F111,"tnsetflow","end",rc);
  8300.     return(rc >= 0 ? 0 : -1);
  8301. }
  8302.  
  8303. int
  8304. #ifdef CK_ANSIC
  8305. tnsettings(int par, int stop)
  8306. #else
  8307. tnsettings(par, stop) int par, stop;
  8308. #endif /* CK_ANSIC */
  8309. /* tnsettings */ {
  8310.     int rc = -1;
  8311.     int datasize = 0;
  8312.     extern int hwparity;
  8313.  
  8314.     debug(F111,"tnsettings begin","par",par);
  8315.     debug(F111,"tnsettings begin","stop",stop);
  8316.     if (ttnet != NET_TCPB || ttnproto != NP_TELNET)
  8317.       return(-1);
  8318.  
  8319.     datasize = par ? TNC_DS_7 : TNC_DS_8;
  8320.     if (!par) par = hwparity;
  8321.  
  8322.     if (TELOPT_ME(TELOPT_COMPORT)) {
  8323.         switch (par) {
  8324.       case 'e':
  8325.             rc = tnc_set_parity(TNC_PAR_EVEN);
  8326.             if (rc >= 0)
  8327.           rc = tnc_set_datasize(datasize);
  8328.             break;
  8329.       case 'o':
  8330.             rc = tnc_set_parity(TNC_PAR_ODD);
  8331.             if (rc >= 0)
  8332.           rc = tnc_set_datasize(datasize);
  8333.             break;
  8334.       case 'm':
  8335.             rc = tnc_set_parity(TNC_PAR_MARK);
  8336.             if (rc >= 0)
  8337.           rc = tnc_set_datasize(datasize);
  8338.             break;
  8339.       case 's':
  8340.             rc = tnc_set_parity(TNC_PAR_SPACE);
  8341.             if (rc >= 0)
  8342.           rc = tnc_set_datasize(datasize);
  8343.             break;
  8344.       case 0:
  8345.       case 'n':
  8346.             rc = tnc_set_parity(TNC_PAR_NONE);
  8347.             if (rc >= 0)
  8348.           rc = tnc_set_datasize(datasize);
  8349.         break;
  8350.       default:
  8351.         /* no change */
  8352.             rc = 0;
  8353.         }
  8354.         switch(stop) {
  8355.       case 2:
  8356.             if (rc >= 0)
  8357.           rc = tnc_set_stopsize(TNC_SB_2);
  8358.             break;
  8359.       case 1:
  8360.             if (rc >= 0)
  8361.           rc = tnc_set_stopsize(TNC_SB_1);
  8362.             break;
  8363.       default:
  8364.             /* no change */
  8365.             if (rc >= 0)
  8366.           rc = 0;
  8367.         }
  8368.     }
  8369.     debug(F111,"tnsettings","end",rc);
  8370.     return((rc >= 0) ? 0 : -1);
  8371. }
  8372.  
  8373. /*  T N G M D M  --  Telnet Get modem signals  */
  8374. /*
  8375.  Looks for the modem signals CTS, DSR, and CTS, and returns those that are
  8376.  on in as its return value, in a bit mask as described for ttwmdm.
  8377.  Returns:
  8378.   -3 Not implemented
  8379.   -2 if the line does not have modem control
  8380.   -1 on error.
  8381.   >= 0 on success, with a bit mask containing the modem signals that are on.
  8382. */
  8383. int
  8384. #ifdef CK_ANSIC
  8385. tngmdm(void)
  8386. #else
  8387. tngmdm()
  8388. #endif /* CK_ANSIC */
  8389. /* tngmdm */ {
  8390.  
  8391.     int rc = -1;
  8392.  
  8393.     debug(F110,"tngmdm","begin",0);
  8394.     if (ttnet != NET_TCPB || ttnproto != NP_TELNET)
  8395.       return(-1);
  8396.  
  8397.     if (TELOPT_ME(TELOPT_COMPORT)) {
  8398.         int modemstate = tnc_get_ms();
  8399.         int modem = 0;
  8400.         if (modemstate & TNC_MS_CTS_SIG)
  8401.       modem |= BM_CTS;
  8402.         if (modemstate & TNC_MS_DSR_SIG)
  8403.       modem |= BM_DSR;
  8404.         if (modemstate & TNC_MS_RI_SIG)
  8405.       modem |= BM_RNG;
  8406.         if (modemstate & TNC_MS_RLSD_SIG)
  8407.       modem |= BM_DCD;
  8408.         debug(F111,"tngmdm","end",modem);
  8409.         return(modem);
  8410.     } else {
  8411.         debug(F111,"tngmdm","end",-2);
  8412.         return(-2);
  8413.     }
  8414. }
  8415.  
  8416. int
  8417. #ifdef CK_ANSIC
  8418. tnsndb(long wait)
  8419. #else
  8420. tnsndb(wait) long wait;
  8421. #endif /* CK_ANSIC */
  8422. /* tnsndb */ {
  8423.     int rc = -1;
  8424.  
  8425.     debug(F111,"tnsndb","begin",wait);
  8426.     if (ttnet != NET_TCPB || ttnproto != NP_TELNET)
  8427.       return(-1);
  8428.  
  8429.     if (TELOPT_ME(TELOPT_COMPORT)) {
  8430.         rc  = tnc_set_break_state(1);
  8431.         if (rc >= 0) {
  8432.             msleep(wait);                         /* ZZZzzz */
  8433.             rc = tnc_set_break_state(0);
  8434.         }
  8435.     }
  8436.     debug(F111,"tnsndb","end",rc);
  8437.     return((rc >= 0) ? 0 : -1);
  8438. }
  8439. #endif /* TN_COMPORT */
  8440. #endif /* TNCODE */
  8441.