1 /* $NetBSD: telnet.c,v 1.36 2012/01/10 13:49:32 christos Exp $ */
4 * Copyright (c) 1988, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)telnet.c 8.4 (Berkeley) 5/30/95";
37 __RCSID("$NetBSD: telnet.c,v 1.36 2012/01/10 13:49:32 christos Exp $");
41 #include <sys/param.h>
46 /* By the way, we need to include curses.h before telnet.h since,
47 * among other things, telnet.h #defines 'DO', which is a variable
48 * declared in curses.h.
51 #include <arpa/telnet.h>
61 #include <libtelnet/misc.h>
63 #include <libtelnet/auth.h>
66 #include <libtelnet/encrypt.h>
69 #define strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x))
71 static unsigned char subbuffer
[SUBBUFSIZE
],
72 *subpointer
, *subend
; /* buffer for sub-options */
73 #define SB_CLEAR() subpointer = subbuffer;
74 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
75 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
76 *subpointer++ = (c); \
79 #define SB_GET() ((*subpointer++)&0xff)
80 #define SB_PEEK() ((*subpointer)&0xff)
81 #define SB_EOF() (subpointer >= subend)
82 #define SB_LEN() (subend - subpointer)
84 char options
[256]; /* The combined options */
85 char do_dont_resp
[256];
86 char will_wont_resp
[256];
90 autologin
= 0, /* Autologin anyone? */
94 In3270
, /* Are we in 3270 mode? */
95 ISend
, /* trying to send network data in */
98 netdata
, /* Print out network data flow */
99 crlf
, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
101 noasynchtty
= 0,/* User specified "-noasynch" on command line */
102 noasynchnet
= 0,/* User specified "-noasynch" on command line */
103 askedSGA
= 0, /* We have talked about suppress go ahead */
104 #endif /* defined(TN3270) */
106 SYNCHing
, /* we are in TELNET SYNCH mode */
107 flushout
, /* flush output */
108 autoflush
= 0, /* flush output when interrupting? */
109 autosynch
, /* send interrupt characters with SYNCH? */
110 localflow
, /* we handle flow control locally */
111 restartany
, /* if flow control enabled, restart on any character */
112 localchars
, /* we recognize interrupt/quit */
113 donelclchars
, /* the user has set "localchars" */
114 donebinarytoggle
, /* the user has put us in binary */
115 dontlecho
, /* do we suppress local echoing right now? */
117 doaddrlookup
= 1, /* do a reverse address lookup? */
124 #ifdef KLUDGELINEMODE
129 * Telnet receiver states for fsm
138 #define TS_SB 7 /* sub-option collection */
139 #define TS_SE 8 /* looking for sub-option end */
141 static int telrcv_state
;
143 unsigned char telopt_environ
= TELOPT_NEW_ENVIRON
;
145 # define telopt_environ TELOPT_NEW_ENVIRON
148 jmp_buf toplevel
= { 0 };
153 #ifdef KLUDGELINEMODE
154 int kludgelinemode
= 1;
157 static void dooption(int);
158 static void dontoption(int);
159 static void suboption(void);
160 static int telsnd(void);
161 static void netclear(void);
162 static void doflush(void);
165 * The following are some clocks used to decide how to interpret
166 * the relationship between various variables.
172 Modelist modelist
[] = {
173 { "telnet command mode", COMMAND_LINE
},
174 { "character-at-a-time mode", 0 },
175 { "character-at-a-time mode (local echo)", LOCAL_ECHO
|LOCAL_CHARS
},
176 { "line-by-line mode (remote echo)", LINE
| LOCAL_CHARS
},
177 { "line-by-line mode", LINE
| LOCAL_ECHO
| LOCAL_CHARS
},
178 { "line-by-line mode (local echoing suppressed)", LINE
| LOCAL_CHARS
},
185 * Initialize telnet environment.
196 connected
= In3270
= ISend
= localflow
= donebinarytoggle
= 0;
197 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
198 auth_encrypt_connect(connected
);
199 #endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
204 /* Don't change NetTrace */
206 escape
= CONTROL(']');
207 rlogin
= _POSIX_VDISABLE
;
208 #ifdef KLUDGELINEMODE
209 echoc
= CONTROL('E');
213 telrcv_state
= TS_DATA
;
222 printring(Ring
*ring
, char *format
, ...)
226 char buffer
[100]; /* where things go */
231 va_start(ap
, format
);
235 while ((i
= *format
++) != 0) {
240 *ptr
++ = va_arg(ap
, int);
243 string
= va_arg(ap
, char *);
244 ring_supply_data(ring
, buffer
, ptr
-buffer
);
245 ring_supply_data(ring
, string
, strlen(string
));
249 ExitString("printring: trailing %%.\n", 1);
252 ExitString("printring: unknown format character.\n", 1);
260 ring_supply_data(ring
, buffer
, ptr
-buffer
);
265 * These routines are in charge of sending option negotiations
268 * The basic idea is that we send the negotiation if either side
269 * is in disagreement as to what the current state should be.
273 send_do(int c
, int init
)
276 if (((do_dont_resp
[c
] == 0) && my_state_is_do(c
)) ||
277 my_want_state_is_do(c
))
279 set_my_want_state_do(c
);
284 printoption("SENT", DO
, c
);
288 send_dont(int c
, int init
)
291 if (((do_dont_resp
[c
] == 0) && my_state_is_dont(c
)) ||
292 my_want_state_is_dont(c
))
294 set_my_want_state_dont(c
);
299 printoption("SENT", DONT
, c
);
303 send_will(int c
, int init
)
306 if (((will_wont_resp
[c
] == 0) && my_state_is_will(c
)) ||
307 my_want_state_is_will(c
))
309 set_my_want_state_will(c
);
314 printoption("SENT", WILL
, c
);
318 send_wont(int c
, int init
)
321 if (((will_wont_resp
[c
] == 0) && my_state_is_wont(c
)) ||
322 my_want_state_is_wont(c
))
324 set_my_want_state_wont(c
);
329 printoption("SENT", WONT
, c
);
334 willoption(int option
)
336 int new_state_ok
= 0;
338 if (do_dont_resp
[option
]) {
339 --do_dont_resp
[option
];
340 if (do_dont_resp
[option
] && my_state_is_do(option
))
341 --do_dont_resp
[option
];
344 if ((do_dont_resp
[option
] == 0) && my_want_state_is_dont(option
)) {
351 * The following is a pain in the rear-end.
352 * Various IBM servers (some versions of Wiscnet,
353 * possibly Fibronics/Spartacus, and who knows who
354 * else) will NOT allow us to send "DO SGA" too early
355 * in the setup proceedings. On the other hand,
356 * 4.2 servers (telnetd) won't set SGA correctly.
357 * So, we are stuck. Empirically (but, based on
358 * a VERY small sample), the IBM servers don't send
359 * out anything about ECHO, so we postpone our sending
360 * "DO SGA" until we see "WILL ECHO" (which 4.2 servers
366 if (my_want_state_is_dont(TELOPT_SGA
))
367 send_do(TELOPT_SGA
, 1);
372 #endif /* defined(TN3270) */
375 settimer(modenegotiated
);
378 #ifdef AUTHENTICATION
379 case TELOPT_AUTHENTICATION
:
382 #endif /* ENCRYPTION */
391 * Special case for TM. If we get back a WILL,
392 * pretend we got back a WONT.
394 set_my_want_state_dont(option
);
395 set_my_state_dont(option
);
396 return; /* Never reply to TM will's/wont's */
398 case TELOPT_LINEMODE
:
404 set_my_want_state_do(option
);
406 setconnmode(0); /* possibly set new tty mode */
408 do_dont_resp
[option
]++;
409 send_dont(option
, 0);
412 set_my_state_do(option
);
414 if (option
== TELOPT_ENCRYPT
)
415 encrypt_send_support();
416 #endif /* ENCRYPTION */
420 wontoption(int option
)
422 if (do_dont_resp
[option
]) {
423 --do_dont_resp
[option
];
424 if (do_dont_resp
[option
] && my_state_is_dont(option
))
425 --do_dont_resp
[option
];
428 if ((do_dont_resp
[option
] == 0) && my_want_state_is_do(option
)) {
432 #ifdef KLUDGELINEMODE
439 settimer(modenegotiated
);
445 set_my_want_state_dont(option
);
446 set_my_state_dont(option
);
447 return; /* Never reply to TM will's/wont's */
452 set_my_want_state_dont(option
);
453 if (my_state_is_do(option
))
454 send_dont(option
, 0);
455 setconnmode(0); /* Set new tty mode */
456 } else if (option
== TELOPT_TM
) {
458 * Special case for TM.
462 set_my_want_state_dont(option
);
464 set_my_state_dont(option
);
470 int new_state_ok
= 0;
472 if (will_wont_resp
[option
]) {
473 --will_wont_resp
[option
];
474 if (will_wont_resp
[option
] && my_state_is_will(option
))
475 --will_wont_resp
[option
];
478 if (will_wont_resp
[option
] == 0) {
479 if (my_want_state_is_wont(option
)) {
485 * Special case for TM. We send a WILL, but pretend
488 send_will(option
, 0);
489 set_my_want_state_wont(TELOPT_TM
);
490 set_my_state_wont(TELOPT_TM
);
494 case TELOPT_EOR
: /* end of record */
495 # endif /* defined(TN3270) */
496 case TELOPT_BINARY
: /* binary mode */
497 case TELOPT_NAWS
: /* window size */
498 case TELOPT_TSPEED
: /* terminal speed */
499 case TELOPT_LFLOW
: /* local flow control */
500 case TELOPT_TTYPE
: /* terminal type option */
501 case TELOPT_SGA
: /* no big deal */
503 case TELOPT_ENCRYPT
: /* encryption variable option */
504 #endif /* ENCRYPTION */
508 case TELOPT_NEW_ENVIRON
: /* New environment variable option */
510 if (my_state_is_will(TELOPT_OLD_ENVIRON
))
511 send_wont(TELOPT_OLD_ENVIRON
, 1); /* turn off the old */
513 case TELOPT_OLD_ENVIRON
: /* Old environment variable option */
514 if (my_state_is_will(TELOPT_NEW_ENVIRON
))
515 break; /* Don't enable if new one is in use! */
517 telopt_environ
= option
;
522 #ifdef AUTHENTICATION
523 case TELOPT_AUTHENTICATION
:
529 case TELOPT_XDISPLOC
: /* X Display location */
530 if (env_getvalue((const unsigned char *)"DISPLAY"))
534 case TELOPT_LINEMODE
:
535 #ifdef KLUDGELINEMODE
537 send_do(TELOPT_SGA
, 1);
539 set_my_want_state_will(TELOPT_LINEMODE
);
540 send_will(option
, 0);
541 set_my_state_will(TELOPT_LINEMODE
);
545 case TELOPT_ECHO
: /* We're never going to echo... */
551 set_my_want_state_will(option
);
552 send_will(option
, 0);
553 setconnmode(0); /* Set new tty mode */
555 will_wont_resp
[option
]++;
556 send_wont(option
, 0);
560 * Handle options that need more things done after the
561 * other side has acknowledged the option.
564 case TELOPT_LINEMODE
:
565 #ifdef KLUDGELINEMODE
567 send_do(TELOPT_SGA
, 1);
569 set_my_state_will(option
);
571 send_do(TELOPT_SGA
, 0);
576 set_my_state_will(option
);
580 dontoption(int option
)
583 if (will_wont_resp
[option
]) {
584 --will_wont_resp
[option
];
585 if (will_wont_resp
[option
] && my_state_is_wont(option
))
586 --will_wont_resp
[option
];
589 if ((will_wont_resp
[option
] == 0) && my_want_state_is_will(option
)) {
591 case TELOPT_LINEMODE
:
592 linemode
= 0; /* put us back to the default state */
595 case TELOPT_NEW_ENVIRON
:
597 * The new environ option wasn't recognized, try
600 send_will(TELOPT_OLD_ENVIRON
, 1);
601 telopt_environ
= TELOPT_OLD_ENVIRON
;
605 /* we always accept a DONT */
606 set_my_want_state_wont(option
);
607 if (my_state_is_will(option
))
608 send_wont(option
, 0);
609 setconnmode(0); /* Set new tty mode */
611 set_my_state_wont(option
);
615 * Given a buffer returned by tgetent(), this routine will turn
616 * the pipe separated list of names in the buffer into an array
617 * of pointers to null terminated names. We toss out any bad,
618 * duplicate, or verbose names (names with spaces).
621 static char name_unknown
[] = "UNKNOWN";
622 static char *unknown
[] = { 0, 0 };
625 mklist(char *buf
, char *name
)
628 char c
, *cp
, **argvp
, *cp2
, **argv
, **avt
;
631 if ((int)strlen(name
) > 40) {
633 unknown
[0] = name_unknown
;
639 unknown
[0] = name_unknown
;
641 * Count up the number of names.
643 for (n
= 1, cp
= buf
; *cp
&& *cp
!= ':'; cp
++) {
648 * Allocate an array to put the name pointers into
650 argv
= (char **)malloc((n
+3)*sizeof(char *));
655 * Fill up the array of pointers to names.
660 for (cp
= cp2
= buf
; (c
= *cp
); cp
++) {
661 if (c
== '|' || c
== ':') {
664 * Skip entries that have spaces or are over 40
665 * characters long. If this is our environment
666 * name, then put it up front. Otherwise, as
667 * long as this is not a duplicate name (case
668 * insensitive) add it to the list.
670 if (n
|| (cp
- cp2
> 41))
672 else if (name
&& (strncasecmp(name
, cp2
, cp
-cp2
) == 0))
674 else if (is_unique(cp2
, argv
+1, argvp
))
679 * Skip multiple delimiters. Reset cp2 to
680 * the beginning of the next name. Reset n,
681 * the flag for names with spaces.
683 while ((c
= *cp
) == '|')
689 * Skip entries with spaces or non-ascii values.
690 * Convert lower case letters to upper case.
692 if ((c
== ' ') || !isascii(c
))
694 else if (islower((unsigned char)c
))
695 *cp
= toupper((unsigned char)c
);
699 * Check for an old V6 2 character name. If the second
700 * name points to the beginning of the buffer, and is
701 * only 2 characters long, move it to the end of the array.
703 if ((argv
[1] == buf
) && (strlen(argv
[1]) == 2)) {
705 for (avt
= &argv
[1]; avt
< argvp
; avt
++)
711 * Duplicate last name, for TTYPE option, and null
712 * terminate the array. If we didn't find a match on
713 * our terminal name, put that name at the beginning.
724 for (avt
= argv
; avt
< argvp
; avt
++)
735 is_unique(char *name
, char **as
, char **ae
)
740 n
= strlen(name
) + 1;
741 for (ap
= as
; ap
< ae
; ap
++)
742 if (strncasecmp(*ap
, name
, n
) == 0)
752 setupterm(char *tname
, int fd
, int *errp
)
754 char zz
[1024], *zz_ptr
;
755 char *ext_tc
, *newptr
;
758 if ((termbuf
= malloc(1024)) == NULL
)
761 if (tgetent(termbuf
, tname
) == 1) {
762 /* check for ZZ capability, indicating termcap truncated */
764 if (tgetstr("ZZ", &zz_ptr
) != NULL
) {
765 /* it was, fish back the full termcap */
766 sscanf(zz
, "%p", &ext_tc
);
767 len
= strlen(ext_tc
) + 1;
768 if ((newptr
= realloc(termbuf
, len
)) == NULL
)
771 memcpy(newptr
, ext_tc
, len
);
785 #define termbuf ttytype
786 extern char ttytype
[];
789 int resettermname
= 1;
795 static char **tnamep
= 0;
801 if (tnamep
&& tnamep
!= unknown
)
803 if ((tname
= (char *)env_getvalue((const unsigned char *)"TERM")) &&
804 (setupterm(tname
, 1, &err
) == 0)) {
805 tnamep
= mklist(termbuf
, tname
);
807 if (tname
&& ((int)strlen(tname
) <= 40)) {
811 unknown
[0] = name_unknown
;
823 * Look at the sub-option buffer, and try to be helpful to the other
826 * Currently we recognize:
828 * Terminal type, send request.
829 * Terminal speed (send request).
830 * Local flow control (is request).
837 unsigned char subchar
;
839 printsub('<', subbuffer
, SB_LEN()+2);
840 switch (subchar
= SB_GET()) {
842 if (my_want_state_is_wont(TELOPT_TTYPE
))
844 if (SB_EOF() || SB_GET() != TELQUAL_SEND
) {
848 unsigned char temp
[50];
852 if (tn3270_ttype()) {
855 #endif /* defined(TN3270) */
856 name
= gettermname();
857 len
= strlen(name
) + 4 + 2;
858 if (len
< NETROOM()) {
859 sprintf((char *)temp
, "%c%c%c%c%s%c%c", IAC
, SB
, TELOPT_TTYPE
,
860 TELQUAL_IS
, name
, IAC
, SE
);
861 ring_supply_data(&netoring
, temp
, len
);
862 printsub('>', &temp
[2], len
-2);
864 ExitString("No room in buffer for terminal type.\n", 1);
870 if (my_want_state_is_wont(TELOPT_TSPEED
))
874 if (SB_GET() == TELQUAL_SEND
) {
876 unsigned char temp
[50];
879 TerminalSpeeds(&isp
, &osp
);
881 sprintf((char *)temp
, "%c%c%c%c%ld,%ld%c%c", IAC
, SB
, TELOPT_TSPEED
,
882 TELQUAL_IS
, osp
, isp
, IAC
, SE
);
883 len
= strlen((char *)temp
+4) + 4; /* temp[3] is 0 ... */
885 if (len
< NETROOM()) {
886 ring_supply_data(&netoring
, temp
, len
);
887 printsub('>', temp
+2, len
- 2);
889 /*@*/ else printf("lm_will: not enough room in buffer\n");
893 if (my_want_state_is_wont(TELOPT_LFLOW
))
898 case LFLOW_RESTART_ANY
:
901 case LFLOW_RESTART_XON
:
917 case TELOPT_LINEMODE
:
918 if (my_want_state_is_wont(TELOPT_LINEMODE
))
924 lm_will(subpointer
, SB_LEN());
927 lm_wont(subpointer
, SB_LEN());
930 lm_do(subpointer
, SB_LEN());
933 lm_dont(subpointer
, SB_LEN());
936 slc(subpointer
, SB_LEN());
939 lm_mode(subpointer
, SB_LEN(), 0);
947 case TELOPT_OLD_ENVIRON
:
949 case TELOPT_NEW_ENVIRON
:
955 if (my_want_state_is_dont(subchar
))
959 if (my_want_state_is_wont(subchar
)) {
966 env_opt(subpointer
, SB_LEN());
969 case TELOPT_XDISPLOC
:
970 if (my_want_state_is_wont(TELOPT_XDISPLOC
))
974 if (SB_GET() == TELQUAL_SEND
) {
975 unsigned char temp
[50], *dp
;
978 if ((dp
= env_getvalue((const unsigned char *)"DISPLAY")) == NULL
) {
980 * Something happened, we no longer have a DISPLAY
981 * variable. So, turn off the option.
983 send_wont(TELOPT_XDISPLOC
, 1);
986 sprintf((char *)temp
, "%c%c%c%c%s%c%c", IAC
, SB
, TELOPT_XDISPLOC
,
987 TELQUAL_IS
, dp
, IAC
, SE
);
988 len
= strlen((char *)temp
+4) + 4; /* temp[3] is 0 ... */
990 if (len
< NETROOM()) {
991 ring_supply_data(&netoring
, temp
, len
);
992 printsub('>', temp
+2, len
- 2);
994 /*@*/ else printf("lm_will: not enough room in buffer\n");
998 #ifdef AUTHENTICATION
999 case TELOPT_AUTHENTICATION
: {
1006 if (my_want_state_is_dont(TELOPT_AUTHENTICATION
))
1008 auth_is(subpointer
, SB_LEN());
1011 if (my_want_state_is_wont(TELOPT_AUTHENTICATION
))
1013 auth_send(subpointer
, SB_LEN());
1016 if (my_want_state_is_wont(TELOPT_AUTHENTICATION
))
1018 auth_reply(subpointer
, SB_LEN());
1021 if (my_want_state_is_dont(TELOPT_AUTHENTICATION
))
1023 auth_name(subpointer
, SB_LEN());
1030 case TELOPT_ENCRYPT
:
1035 if (my_want_state_is_dont(TELOPT_ENCRYPT
))
1037 encrypt_start(subpointer
, SB_LEN());
1040 if (my_want_state_is_dont(TELOPT_ENCRYPT
))
1044 case ENCRYPT_SUPPORT
:
1045 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1047 encrypt_support(subpointer
, SB_LEN());
1049 case ENCRYPT_REQSTART
:
1050 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1052 encrypt_request_start(subpointer
, SB_LEN());
1054 case ENCRYPT_REQEND
:
1055 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1058 * We can always send an REQEND so that we cannot
1059 * get stuck encrypting. We should only get this
1060 * if we have been able to get in the correct mode
1063 encrypt_request_end();
1066 if (my_want_state_is_dont(TELOPT_ENCRYPT
))
1068 encrypt_is(subpointer
, SB_LEN());
1071 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1073 encrypt_reply(subpointer
, SB_LEN());
1075 case ENCRYPT_ENC_KEYID
:
1076 if (my_want_state_is_dont(TELOPT_ENCRYPT
))
1078 encrypt_enc_keyid(subpointer
, SB_LEN());
1080 case ENCRYPT_DEC_KEYID
:
1081 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1083 encrypt_dec_keyid(subpointer
, SB_LEN());
1089 #endif /* ENCRYPTION */
1095 static unsigned char str_lm
[] = { IAC
, SB
, TELOPT_LINEMODE
, 0, 0, IAC
, SE
};
1098 lm_will(unsigned char *cmd
, int len
)
1101 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
1105 case LM_FORWARDMASK
: /* We shouldn't ever get this... */
1109 if ((size_t)NETROOM() > sizeof(str_lm
)) {
1110 ring_supply_data(&netoring
, str_lm
, sizeof(str_lm
));
1111 printsub('>', &str_lm
[2], sizeof(str_lm
)-2);
1113 /*@*/ else printf("lm_will: not enough room in buffer\n");
1119 lm_wont(unsigned char *cmd
, int len
)
1122 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
1126 case LM_FORWARDMASK
: /* We shouldn't ever get this... */
1128 /* We are always DONT, so don't respond */
1134 lm_do(unsigned char *cmd
, int len
)
1137 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1141 case LM_FORWARDMASK
:
1145 if ((size_t)NETROOM() > sizeof(str_lm
)) {
1146 ring_supply_data(&netoring
, str_lm
, sizeof(str_lm
));
1147 printsub('>', &str_lm
[2], sizeof(str_lm
)-2);
1149 /*@*/ else printf("lm_do: not enough room in buffer\n");
1155 lm_dont(unsigned char *cmd
, int len
)
1158 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1162 case LM_FORWARDMASK
:
1164 /* we are always WONT, so don't respond */
1169 static unsigned char str_lm_mode
[] = {
1170 IAC
, SB
, TELOPT_LINEMODE
, LM_MODE
, 0, IAC
, SE
1174 lm_mode(unsigned char *cmd
, int len
, int init
)
1178 if ((linemode
&MODE_MASK
&~MODE_ACK
) == *cmd
)
1182 linemode
= *cmd
&(MODE_MASK
&~MODE_ACK
);
1183 str_lm_mode
[4] = linemode
;
1185 str_lm_mode
[4] |= MODE_ACK
;
1186 if ((size_t)NETROOM() > sizeof(str_lm_mode
)) {
1187 ring_supply_data(&netoring
, str_lm_mode
, sizeof(str_lm_mode
));
1188 printsub('>', &str_lm_mode
[2], sizeof(str_lm_mode
)-2);
1190 /*@*/ else printf("lm_mode: not enough room in buffer\n");
1191 setconnmode(0); /* set changed mode */
1198 * Handle special character suboption of LINEMODE.
1204 char flags
; /* Current flags & level */
1205 char mylevel
; /* Maximum level & flags */
1208 #define SLC_IMPORT 0
1209 #define SLC_EXPORT 1
1210 #define SLC_RVALUE 2
1211 static int slc_mode
= SLC_EXPORT
;
1219 for (spcp
= spc_data
; spcp
< &spc_data
[NSLC
+1]; spcp
++) {
1222 spcp
->flags
= spcp
->mylevel
= SLC_NOSUPPORT
;
1225 #define initfunc(func, flags) { \
1226 spcp = &spc_data[func]; \
1227 if ((spcp->valp = tcval(func)) != NULL){ \
1228 spcp->val = *spcp->valp; \
1229 spcp->mylevel = SLC_VARIABLE|flags; \
1232 spcp->mylevel = SLC_DEFAULT; \
1236 initfunc(SLC_SYNCH
, 0);
1238 initfunc(SLC_AO
, 0);
1239 initfunc(SLC_AYT
, 0);
1241 initfunc(SLC_ABORT
, SLC_FLUSHIN
|SLC_FLUSHOUT
);
1242 initfunc(SLC_EOF
, 0);
1243 initfunc(SLC_SUSP
, SLC_FLUSHIN
);
1244 initfunc(SLC_EC
, 0);
1245 initfunc(SLC_EL
, 0);
1246 initfunc(SLC_EW
, 0);
1247 initfunc(SLC_RP
, 0);
1248 initfunc(SLC_LNEXT
, 0);
1249 initfunc(SLC_XON
, 0);
1250 initfunc(SLC_XOFF
, 0);
1251 initfunc(SLC_FORW1
, 0);
1252 initfunc(SLC_FORW2
, 0);
1255 initfunc(SLC_IP
, SLC_FLUSHIN
|SLC_FLUSHOUT
);
1258 if (slc_mode
== SLC_EXPORT
)
1268 printf("Special characters are %s values\n",
1269 slc_mode
== SLC_IMPORT
? "remote default" :
1270 slc_mode
== SLC_EXPORT
? "local" :
1275 slc_mode_export(int n
)
1277 slc_mode
= SLC_EXPORT
;
1278 if (my_state_is_will(TELOPT_LINEMODE
))
1283 slc_mode_import(int def
)
1285 slc_mode
= def
? SLC_IMPORT
: SLC_RVALUE
;
1286 if (my_state_is_will(TELOPT_LINEMODE
))
1290 unsigned char slc_import_val
[] = {
1291 IAC
, SB
, TELOPT_LINEMODE
, LM_SLC
, 0, SLC_VARIABLE
, 0, IAC
, SE
1293 unsigned char slc_import_def
[] = {
1294 IAC
, SB
, TELOPT_LINEMODE
, LM_SLC
, 0, SLC_DEFAULT
, 0, IAC
, SE
1300 if ((size_t)NETROOM() > sizeof(slc_import_val
)) {
1302 ring_supply_data(&netoring
, slc_import_def
, sizeof(slc_import_def
));
1303 printsub('>', &slc_import_def
[2], sizeof(slc_import_def
)-2);
1305 ring_supply_data(&netoring
, slc_import_val
, sizeof(slc_import_val
));
1306 printsub('>', &slc_import_val
[2], sizeof(slc_import_val
)-2);
1309 /*@*/ else printf("slc_import: not enough room\n");
1317 TerminalDefaultChars();
1320 for (spcp
= &spc_data
[1]; spcp
< &spc_data
[NSLC
+1]; spcp
++) {
1321 if (spcp
->mylevel
!= SLC_NOSUPPORT
) {
1322 if (spcp
->val
== (cc_t
)(_POSIX_VDISABLE
))
1323 spcp
->flags
= SLC_NOSUPPORT
;
1325 spcp
->flags
= spcp
->mylevel
;
1327 spcp
->val
= *spcp
->valp
;
1328 slc_add_reply(spcp
- spc_data
, spcp
->flags
, spcp
->val
);
1333 setconnmode(1); /* Make sure the character values are set */
1337 slc(unsigned char *cp
, int len
)
1344 for (; len
>= 3; len
-=3, cp
+=3) {
1346 func
= cp
[SLC_FUNC
];
1350 * Client side: always ignore 0 function.
1355 if ((cp
[SLC_FLAGS
] & SLC_LEVELBITS
) != SLC_NOSUPPORT
)
1356 slc_add_reply(func
, SLC_NOSUPPORT
, 0);
1360 spcp
= &spc_data
[func
];
1362 level
= cp
[SLC_FLAGS
]&(SLC_LEVELBITS
|SLC_ACK
);
1364 if ((cp
[SLC_VALUE
] == (unsigned char)spcp
->val
) &&
1365 ((level
&SLC_LEVELBITS
) == (spcp
->flags
&SLC_LEVELBITS
))) {
1369 if (level
== (SLC_DEFAULT
|SLC_ACK
)) {
1371 * This is an error condition, the SLC_ACK
1372 * bit should never be set for the SLC_DEFAULT
1373 * level. Our best guess to recover is to
1374 * ignore the SLC_ACK bit.
1376 cp
[SLC_FLAGS
] &= ~SLC_ACK
;
1379 if (level
== ((spcp
->flags
&SLC_LEVELBITS
)|SLC_ACK
)) {
1380 spcp
->val
= (cc_t
)cp
[SLC_VALUE
];
1381 spcp
->flags
= cp
[SLC_FLAGS
]; /* include SLC_ACK */
1387 if (level
<= (spcp
->mylevel
&SLC_LEVELBITS
)) {
1388 spcp
->flags
= cp
[SLC_FLAGS
]|SLC_ACK
;
1389 spcp
->val
= (cc_t
)cp
[SLC_VALUE
];
1391 if (level
== SLC_DEFAULT
) {
1392 if ((spcp
->mylevel
&SLC_LEVELBITS
) != SLC_DEFAULT
)
1393 spcp
->flags
= spcp
->mylevel
;
1395 spcp
->flags
= SLC_NOSUPPORT
;
1397 slc_add_reply(func
, spcp
->flags
, spcp
->val
);
1401 setconnmode(1); /* set the new character values */
1410 for (spcp
= &spc_data
[1]; spcp
< &spc_data
[NSLC
+1]; spcp
++) {
1411 if (spcp
->valp
&& spcp
->val
!= *spcp
->valp
) {
1412 spcp
->val
= *spcp
->valp
;
1413 if (spcp
->val
== (cc_t
)(_POSIX_VDISABLE
))
1414 spcp
->flags
= SLC_NOSUPPORT
;
1416 spcp
->flags
= spcp
->mylevel
;
1417 slc_add_reply(spcp
- spc_data
, spcp
->flags
, spcp
->val
);
1425 unsigned char slc_reply
[128];
1426 unsigned char *slc_replyp
;
1429 slc_start_reply(void)
1431 slc_replyp
= slc_reply
;
1432 *slc_replyp
++ = IAC
;
1434 *slc_replyp
++ = TELOPT_LINEMODE
;
1435 *slc_replyp
++ = LM_SLC
;
1439 slc_add_reply(unsigned int func
, unsigned int flags
, cc_t value
)
1441 if ((size_t)(slc_replyp
- slc_reply
) + 6 > sizeof(slc_reply
))
1443 if ((*slc_replyp
++ = func
) == IAC
)
1444 *slc_replyp
++ = IAC
;
1445 if ((*slc_replyp
++ = flags
) == IAC
)
1446 *slc_replyp
++ = IAC
;
1447 if ((*slc_replyp
++ = (unsigned char)value
) == IAC
)
1448 *slc_replyp
++ = IAC
;
1456 len
= slc_replyp
- slc_reply
;
1457 if (len
<= 4 || ((size_t)len
+ 2 > sizeof(slc_reply
)))
1459 *slc_replyp
++ = IAC
;
1462 if (NETROOM() > len
) {
1463 ring_supply_data(&netoring
, slc_reply
, slc_replyp
- slc_reply
);
1464 printsub('>', &slc_reply
[2], slc_replyp
- slc_reply
- 2);
1466 /*@*/else printf("slc_end_reply: not enough room\n");
1473 int need_update
= 0;
1475 for (spcp
= &spc_data
[1]; spcp
< &spc_data
[NSLC
+1]; spcp
++) {
1476 if (!(spcp
->flags
&SLC_ACK
))
1478 spcp
->flags
&= ~SLC_ACK
;
1479 if (spcp
->valp
&& (*spcp
->valp
!= spcp
->val
)) {
1480 *spcp
->valp
= spcp
->val
;
1484 return(need_update
);
1490 * Earlier version of telnet/telnetd from the BSD code had
1491 * the definitions of VALUE and VAR reversed. To ensure
1492 * maximum interoperability, we assume that the server is
1493 * an older BSD server, until proven otherwise. The newer
1494 * BSD servers should be able to handle either definition,
1495 * so it is better to use the wrong values if we don't
1496 * know what type of server it is.
1499 int old_env_var
= OLD_ENV_VAR
;
1500 int old_env_value
= OLD_ENV_VALUE
;
1502 # define old_env_var OLD_ENV_VAR
1503 # define old_env_value OLD_ENV_VALUE
1508 env_opt(unsigned char *buf
, int len
)
1510 unsigned char *ep
= 0, *epc
= 0;
1513 switch(buf
[0]&0xff) {
1518 } else for (i
= 1; i
< len
; i
++) {
1519 switch (buf
[i
]&0xff) {
1523 if (telopt_environ
== TELOPT_OLD_ENVIRON
1525 /* Server has the same definitions */
1526 old_env_var
= OLD_ENV_VAR
;
1527 old_env_value
= OLD_ENV_VALUE
;
1533 * Although OLD_ENV_VALUE is not legal, we will
1534 * still recognize it, just in case it is an
1535 * old server that has VAR & VALUE mixed up...
1546 ep
= epc
= &buf
[i
+1];
1566 /* Ignore for now. We shouldn't get it anyway. */
1574 #define OPT_REPLY_SIZE 256
1575 unsigned char *opt_reply
;
1576 unsigned char *opt_replyp
;
1577 unsigned char *opt_replyend
;
1585 p
= (unsigned char *)realloc(opt_reply
, OPT_REPLY_SIZE
);
1589 p
= (unsigned char *)malloc(OPT_REPLY_SIZE
);
1591 if (opt_reply
== NULL
) {
1592 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1593 opt_reply
= opt_replyp
= opt_replyend
= NULL
;
1596 opt_replyp
= opt_reply
;
1597 opt_replyend
= opt_reply
+ OPT_REPLY_SIZE
;
1598 *opt_replyp
++ = IAC
;
1600 *opt_replyp
++ = telopt_environ
;
1601 *opt_replyp
++ = TELQUAL_IS
;
1605 env_opt_start_info(void)
1609 opt_replyp
[-1] = TELQUAL_INFO
;
1613 env_opt_add(unsigned char *ep
)
1615 unsigned char *vp
, c
;
1616 unsigned int len
, olen
, elen
;
1618 if (opt_reply
== NULL
) /*XXX*/
1621 if (ep
== NULL
|| *ep
== '\0') {
1622 /* Send user defined variables first. */
1624 while ((ep
= env_default(0, 0)) != NULL
)
1627 /* Now add the list of well know variables. */
1629 while ((ep
= env_default(0, 1)) != NULL
)
1633 vp
= env_getvalue(ep
);
1634 elen
= 2 * (vp
? strlen((char *)vp
) : 0) +
1635 2 * strlen((char *)ep
) + 6;
1636 if ((unsigned int)(opt_replyend
- opt_replyp
) < elen
)
1639 len
= opt_replyend
- opt_reply
+ elen
;
1640 olen
= opt_replyp
- opt_reply
;
1641 p
= (unsigned char *)realloc(opt_reply
, len
);
1645 if (opt_reply
== NULL
) {
1646 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1647 opt_reply
= opt_replyp
= opt_replyend
= NULL
;
1650 opt_replyp
= opt_reply
+ olen
;
1651 opt_replyend
= opt_reply
+ len
;
1653 if (opt_welldefined(ep
))
1655 if (telopt_environ
== TELOPT_OLD_ENVIRON
)
1656 *opt_replyp
++ = old_env_var
;
1659 *opt_replyp
++ = NEW_ENV_VAR
;
1661 *opt_replyp
++ = ENV_USERVAR
;
1663 while ((c
= *ep
++) != '\0') {
1666 *opt_replyp
++ = IAC
;
1672 *opt_replyp
++ = ENV_ESC
;
1677 if ((ep
= vp
) != NULL
) {
1679 if (telopt_environ
== TELOPT_OLD_ENVIRON
)
1680 *opt_replyp
++ = old_env_value
;
1683 *opt_replyp
++ = NEW_ENV_VALUE
;
1691 opt_welldefined(const char *ep
)
1693 if ((strcmp(ep
, "USER") == 0) ||
1694 (strcmp(ep
, "DISPLAY") == 0) ||
1695 (strcmp(ep
, "PRINTER") == 0) ||
1696 (strcmp(ep
, "SYSTEMTYPE") == 0) ||
1697 (strcmp(ep
, "JOB") == 0) ||
1698 (strcmp(ep
, "ACCT") == 0))
1703 env_opt_end(int emptyok
)
1707 len
= opt_replyp
- opt_reply
+ 2;
1708 if (emptyok
|| len
> 6) {
1709 *opt_replyp
++ = IAC
;
1711 if (NETROOM() > len
) {
1712 ring_supply_data(&netoring
, opt_reply
, len
);
1713 printsub('>', &opt_reply
[2], len
- 2);
1715 /*@*/ else printf("slc_end_reply: not enough room\n");
1719 opt_reply
= opt_replyp
= opt_replyend
= NULL
;
1730 unsigned char *sbp
= NULL
;
1732 int returnValue
= 0;
1736 while (TTYROOM() > 2) {
1739 ring_consumed(&netiring
, count
);
1743 sbp
= netiring
.consume
;
1744 scc
= ring_full_consecutive(&netiring
);
1746 /* No more data coming in */
1751 c
= *sbp
++ & 0xff, scc
--; count
++;
1754 c
= (*decrypt_input
)(c
);
1755 #endif /* ENCRYPTION */
1757 switch (telrcv_state
) {
1760 telrcv_state
= TS_DATA
;
1762 break; /* Ignore \0 after CR */
1764 else if ((c
== '\n') && my_want_state_is_dont(TELOPT_ECHO
) && !crmod
) {
1768 /* Else, fall through */
1772 telrcv_state
= TS_IAC
;
1775 # if defined(TN3270)
1779 c
= *sbp
++ & 0377, scc
--; count
++;
1782 c
= (*decrypt_input
)(c
);
1783 #endif /* ENCRYPTION */
1785 telrcv_state
= TS_IAC
;
1791 # endif /* defined(TN3270) */
1793 * The 'crmod' hack (see following) is needed
1794 * since we can't * set CRMOD on output only.
1795 * Machines like MULTICS like to send \r without
1796 * \n; since we must turn off CRMOD to get proper
1797 * input, the mapping is done here (sigh).
1799 if ((c
== '\r') && my_want_state_is_dont(TELOPT_BINARY
)) {
1804 c
= (*decrypt_input
)(c
);
1805 #endif /* ENCRYPTION */
1807 sbp
++, scc
--; count
++;
1810 } else if (my_want_state_is_dont(TELOPT_ECHO
) &&
1812 sbp
++, scc
--; count
++;
1817 (*decrypt_input
)(-1);
1818 #endif /* ENCRYPTION */
1826 telrcv_state
= TS_CR
;
1842 telrcv_state
= TS_WILL
;
1846 telrcv_state
= TS_WONT
;
1850 telrcv_state
= TS_DO
;
1854 telrcv_state
= TS_DONT
;
1859 * We may have missed an urgent notification,
1860 * so make sure we flush whatever is in the
1863 printoption("RCVD", IAC
, DM
);
1866 SYNCHing
= stilloob();
1872 telrcv_state
= TS_SB
;
1875 # if defined(TN3270)
1878 if (Ibackp
== Ifrontp
) {
1879 Ibackp
= Ifrontp
= Ibuf
;
1880 ISend
= 0; /* should have been! */
1882 Ibackp
+= DataFromNetwork(Ibackp
, Ifrontp
-Ibackp
, 1);
1886 printoption("RCVD", IAC
, EOR
);
1888 # endif /* defined(TN3270) */
1891 # if !defined(TN3270)
1893 # else /* !defined(TN3270) */
1899 # endif /* !defined(TN3270) */
1905 printoption("RCVD", IAC
, c
);
1908 telrcv_state
= TS_DATA
;
1912 printoption("RCVD", WILL
, c
);
1915 telrcv_state
= TS_DATA
;
1919 printoption("RCVD", WONT
, c
);
1922 telrcv_state
= TS_DATA
;
1926 printoption("RCVD", DO
, c
);
1929 if (c
== TELOPT_NAWS
) {
1931 } else if (c
== TELOPT_LFLOW
) {
1936 telrcv_state
= TS_DATA
;
1940 printoption("RCVD", DONT
, c
);
1943 setconnmode(0); /* set new tty mode (maybe) */
1945 telrcv_state
= TS_DATA
;
1950 telrcv_state
= TS_SE
;
1960 * This is an error. We only expect to get
1961 * "IAC IAC" or "IAC SE". Several things may
1962 * have happened. An IAC was not doubled, the
1963 * IAC SE was left off, or another option got
1964 * inserted into the suboption are all possibilities.
1965 * If we assume that the IAC was not doubled,
1966 * and really the IAC SE was left off, we could
1967 * get into an infinite loop here. So, instead,
1968 * we terminate the suboption, and process the
1969 * partial suboption if we can.
1976 printoption("In SUBOPTION processing, RCVD", IAC
, c
);
1977 suboption(); /* handle sub-option */
1979 telrcv_state
= TS_IAC
;
1983 telrcv_state
= TS_SB
;
1989 suboption(); /* handle sub-option */
1991 telrcv_state
= TS_DATA
;
1996 ring_consumed(&netiring
, count
);
1997 return returnValue
||count
;
2000 static int bol
= 1, local
= 0;
2008 command(0, "z\n", 2);
2019 int returnValue
= 0;
2020 unsigned char *tbp
= NULL
;
2024 while (NETROOM() > 2) {
2030 ring_consumed(&ttyiring
, count
);
2034 tbp
= ttyiring
.consume
;
2035 tcc
= ring_full_consecutive(&ttyiring
);
2040 c
= *tbp
++ & 0xff, sc
= strip(c
), tcc
--; count
++;
2041 if (rlogin
!= _POSIX_VDISABLE
) {
2050 if (sc
== '.' || c
== termEofChar
) {
2052 command(0, "close\n", 6);
2055 if (sc
== termSuspChar
) {
2057 command(0, "z\n", 2);
2061 command(0, (char *)tbp
, tcc
);
2075 if ((sc
== '\n') || (sc
== '\r'))
2077 } else if (sc
== escape
&& escape
!= _POSIX_VDISABLE
) {
2079 * Double escape is a pass through of a single escape character.
2081 if (tcc
&& strip(*tbp
) == escape
) {
2087 command(0, (char *)tbp
, tcc
);
2096 #ifdef KLUDGELINEMODE
2097 if (kludgelinemode
&& (globalmode
&MODE_EDIT
) && (sc
== echoc
)) {
2098 if (tcc
> 0 && strip(*tbp
) == echoc
) {
2099 tcc
--; tbp
++; count
++;
2101 dontlecho
= !dontlecho
;
2102 settimer(echotoggle
);
2109 if (sc
!= _POSIX_VDISABLE
&& MODE_LOCAL_CHARS(globalmode
)) {
2110 if (TerminalSpecialChars(sc
) == 0) {
2115 if (my_want_state_is_wont(TELOPT_BINARY
)) {
2119 * If we are in CRMOD mode (\r ==> \n)
2120 * on our local machine, then probably
2121 * a newline (unix) is CRLF (TELNET).
2123 if (MODE_LOCAL_CHARS(globalmode
)) {
2127 bol
= flushline
= 1;
2131 NET2ADD('\r', '\0');
2133 NET2ADD('\r', '\n');
2135 bol
= flushline
= 1;
2144 } else if (c
== IAC
) {
2151 ring_consumed(&ttyiring
, count
);
2152 return returnValue
||count
; /* Non-zero if we did anything */
2158 * Try to do something.
2160 * If we do something useful, return 1; else return 0.
2166 Scheduler(int block
) /* should we block in the select ? */
2168 /* One wants to be a bit careful about setting returnValue
2169 * to one, since a one implies we did some useful work,
2170 * and therefore probably won't be called to block next
2171 * time (TN3270 mode only).
2174 int netin
, netout
, netex
, ttyin
, ttyout
;
2176 /* Decide which rings should be processed */
2178 netout
= ring_full_count(&netoring
) &&
2180 (my_want_state_is_wont(TELOPT_LINEMODE
)
2181 #ifdef KLUDGELINEMODE
2182 && (!kludgelinemode
|| my_want_state_is_do(TELOPT_SGA
))
2185 my_want_state_is_will(TELOPT_BINARY
));
2186 ttyout
= ring_full_count(&ttyoring
);
2189 ttyin
= ring_empty_count(&ttyiring
) && (clienteof
== 0) && (shell_active
== 0);
2190 #else /* defined(TN3270) */
2191 ttyin
= ring_empty_count(&ttyiring
) && (clienteof
== 0);
2192 #endif /* defined(TN3270) */
2195 netin
= ring_empty_count(&netiring
);
2196 # else /* !defined(TN3270) */
2197 netin
= !ISend
&& ring_empty_count(&netiring
);
2198 # endif /* !defined(TN3270) */
2202 /* If we have seen a signal recently, reset things */
2206 (void) signal(SIGIO
, inputAvailable
);
2208 #endif /* defined(TN3270) */
2210 /* Call to system code to process rings */
2212 returnValue
= process_rings(netin
, netout
, netex
, ttyin
, ttyout
, !block
);
2214 /* Now, look at the input rings, looking for work to do. */
2216 if (ring_full_count(&ttyiring
)) {
2217 # if defined(TN3270)
2221 c
= DataFromTerminal(ttyiring
.consume
,
2222 ring_full_consecutive(&ttyiring
));
2225 ring_consumed(&ttyiring
, c
);
2228 # endif /* defined(TN3270) */
2229 returnValue
|= telsnd();
2230 # if defined(TN3270)
2232 # endif /* defined(TN3270) */
2235 if (ring_full_count(&netiring
)) {
2236 # if !defined(TN3270)
2237 returnValue
|= telrcv();
2238 # else /* !defined(TN3270) */
2239 returnValue
= Push3270();
2240 # endif /* !defined(TN3270) */
2246 * Select from tty and network...
2249 telnet(const char *user
)
2253 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
2255 static char local_host
[MAXHOSTNAMELEN
+ 1] = { 0 };
2257 if (!local_host
[0]) {
2258 gethostname(local_host
, sizeof(local_host
));
2259 local_host
[sizeof(local_host
)-1] = 0;
2261 auth_encrypt_init(local_host
, hostname
, "TELNET", 0);
2262 auth_encrypt_user(user
);
2264 #endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
2265 # if !defined(TN3270)
2267 #ifdef AUTHENTICATION
2269 send_will(TELOPT_AUTHENTICATION
, 1);
2272 send_do(TELOPT_ENCRYPT
, 1);
2273 send_will(TELOPT_ENCRYPT
, 1);
2274 #endif /* ENCRYPTION */
2275 send_do(TELOPT_SGA
, 1);
2276 send_will(TELOPT_TTYPE
, 1);
2277 send_will(TELOPT_NAWS
, 1);
2278 send_will(TELOPT_TSPEED
, 1);
2279 send_will(TELOPT_LFLOW
, 1);
2280 send_will(TELOPT_LINEMODE
, 1);
2281 send_will(TELOPT_NEW_ENVIRON
, 1);
2282 send_do(TELOPT_STATUS
, 1);
2283 if (env_getvalue((const unsigned char *)"DISPLAY"))
2284 send_will(TELOPT_XDISPLOC
, 1);
2286 tel_enter_binary(eight
);
2288 # endif /* !defined(TN3270) */
2290 # if !defined(TN3270)
2294 while ((schedValue
= Scheduler(0)) != 0) {
2295 if (schedValue
== -1) {
2301 if (Scheduler(1) == -1) {
2306 # else /* !defined(TN3270) */
2310 while (!In3270
&& !shell_active
) {
2311 if (Scheduler(1) == -1) {
2317 while ((schedValue
= Scheduler(0)) != 0) {
2318 if (schedValue
== -1) {
2323 /* If there is data waiting to go out to terminal, don't
2324 * schedule any more data for the terminal.
2326 if (ring_full_count(&ttyoring
)) {
2330 if (shell_continue() == 0) {
2333 } else if (In3270
) {
2334 schedValue
= DoTerminalOutput();
2337 if (schedValue
&& (shell_active
== 0)) {
2338 if (Scheduler(1) == -1) {
2344 # endif /* !defined(TN3270) */
2347 #if 0 /* XXX - this not being in is a bug */
2351 * Return the address of the next "item" in the TELNET data
2352 * stream. This will be the address of the next character if
2353 * the current address is a user data character, or it will
2354 * be the address of the character following the TELNET command
2355 * if the current address is a TELNET IAC ("I Am a Command")
2360 nextitem(char *current
)
2362 if ((*current
&0xff) != IAC
) {
2365 switch (*(current
+1)&0xff) {
2371 case SB
: /* loop forever looking for the SE */
2373 char *look
= current
+2;
2376 if ((*look
++&0xff) == IAC
) {
2377 if ((*look
++&0xff) == SE
) {
2392 * We are about to do a TELNET SYNCH operation. Clear
2393 * the path to the network.
2395 * Things are a bit tricky since we may have sent the first
2396 * byte or so of a previous TELNET command into the network.
2397 * So, we have to scan the network buffer from the beginning
2398 * until we are up to where we want to be.
2400 * A side effect of what we do, just to keep things
2401 * simple, is to clear the urgent data pointer. The principal
2402 * caller should be setting the urgent data pointer AFTER calling
2410 char *thisitem
, *next
;
2412 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
2413 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
2417 while ((next
= nextitem(thisitem
)) <= netobuf
.send
) {
2421 /* Now, thisitem is first before/at boundary. */
2423 good
= netobuf
; /* where the good bytes go */
2425 while (netoring
.add
> thisitem
) {
2426 if (wewant(thisitem
)) {
2431 next
= nextitem(next
);
2432 } while (wewant(next
) && (nfrontp
> next
));
2433 length
= next
-thisitem
;
2434 memmove(good
, thisitem
, length
);
2438 thisitem
= nextitem(thisitem
);
2446 * These routines add various telnet commands to the data stream.
2456 (void) ttyflush(1); /* Flush/drop output */
2457 /* do printoption AFTER flush, otherwise the output gets tossed... */
2458 printoption("SENT", DO
, TELOPT_TM
);
2465 printoption("SENT", IAC
, AO
);
2476 printoption("SENT", IAC
, EL
);
2483 printoption("SENT", IAC
, EC
);
2490 netclear(); /* clear the path to the network */
2494 printoption("SENT", IAC
, DM
);
2498 int want_status_response
= 0;
2503 unsigned char tmp
[16];
2506 if (my_want_state_is_dont(TELOPT_STATUS
)) {
2507 printf("Remote side does not support STATUS option\n");
2514 *cp
++ = TELOPT_STATUS
;
2515 *cp
++ = TELQUAL_SEND
;
2518 if (NETROOM() >= cp
- tmp
) {
2519 ring_supply_data(&netoring
, tmp
, cp
-tmp
);
2520 printsub('>', tmp
+2, cp
- tmp
- 2);
2522 ++want_status_response
;
2530 printoption("SENT", IAC
, IP
);
2543 NET2ADD(IAC
, BREAK
);
2544 printoption("SENT", IAC
, BREAK
);
2557 NET2ADD(IAC
, ABORT
);
2558 printoption("SENT", IAC
, ABORT
);
2572 printoption("SENT", IAC
, SUSP
);
2586 printoption("SENT", IAC
, xEOF
);
2593 printoption("SENT", IAC
, AYT
);
2597 * Send a window size update to the remote system.
2604 unsigned char tmp
[16];
2607 if (my_state_is_wont(TELOPT_NAWS
))
2610 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2611 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2613 if (TerminalWindowSize(&rows
, &cols
) == 0) { /* Failed */
2621 *cp
++ = TELOPT_NAWS
;
2626 if (NETROOM() >= cp
- tmp
) {
2627 ring_supply_data(&netoring
, tmp
, cp
-tmp
);
2628 printsub('>', tmp
+2, cp
- tmp
- 2);
2633 tel_enter_binary(int rw
)
2636 send_do(TELOPT_BINARY
, 1);
2638 send_will(TELOPT_BINARY
, 1);
2642 tel_leave_binary(int rw
)
2645 send_dont(TELOPT_BINARY
, 1);
2647 send_wont(TELOPT_BINARY
, 1);