-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtelnet.c
427 lines (403 loc) · 8.31 KB
/
telnet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* Internet Telnet client
* Copyright 1991 Phil Karn, KA9Q
*/
#include "top.h"
#include "lib/std/stdio.h"
#ifdef __TURBOC__
#include <io.h>
#include <fcntl.h>
#endif
#include "lib/std/errno.h"
#include "global.h"
#include "net/core/mbuf.h"
#include "core/socket.h"
#include "telnet.h"
#include "core/session.h"
#include "core/proc.h"
#include "core/tty.h"
#include "commands.h"
#include "lib/util//cmdparse.h"
#include "lib/inet/netuser.h"
#include "net/inet/internet.h"
int Refuse_echo = 0;
int Tn_cr_mode = 0; /* if true turn <cr> to <cr-nul> */
int Topt = 0;
char *T_options[] = {
"Transmit Binary",
"Echo",
"",
"Suppress Go Ahead",
"",
"Status",
"Timing Mark"
};
static int keychar(int c);
int
dotopt(argc,argv,p)
int argc;
char *argv[];
void *p;
{
setbool(&Topt,"Telnet option tracing",argc,argv);
return 0;
}
/* Execute user telnet command */
int
dotelnet(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct session *sp;
struct ksockaddr_in fsocket;
int s;
/* Allocate a session descriptor */
if((sp = newsession(Cmdline,TELNET,1)) == NULL){
kprintf("Too many sessions\n");
return 1;
}
sp->inproc = keychar; /* Intercept ^C */
fsocket.sin_family = kAF_INET;
fsocket.sin_port = (argc < 3) ? IPPORT_TELNET : atoi(argv[2]);
if(SETSIG(kEABORT)){
keywait(NULL,1);
freesession(&sp);
return 1;
}
kprintf("Resolving %s...\n",argv[1]);
if((fsocket.sin_addr.s_addr = resolve(argv[1])) == 0){
kprintf(Badhost,argv[1]);
keywait(NULL,1);
freesession(&sp);
return 1;
}
if((s = ksocket(kAF_INET,kSOCK_STREAM,0)) == -1){
kprintf("Can't create socket\n");
keywait(NULL,1);
freesession(&sp);
return 1;
}
settos(s,LOW_DELAY);
sp->network = kfdopen(s,"r+t");
ksetvbuf(sp->network,NULL,_kIOLBF,kBUFSIZ);
return tel_connect(sp,(struct ksockaddr *)&fsocket,SOCKSIZE);
}
/* Generic interactive connect routine, used by Telnet, AX.25, NET/ROM */
int
tel_connect(sp,fsocket,len)
struct session *sp;
struct ksockaddr *fsocket;
int len;
{
struct telnet tn;
memset(&tn,0,sizeof(tn));
tn.eolmode = Tn_cr_mode;
tn.session = sp; /* Upward pointer */
sp->cb.telnet = &tn; /* Downward pointer */
kprintf("Trying %s...\n",psocket(fsocket));
if(kconnect(kfileno(sp->network),fsocket,len) == -1){
kperror("connect failed");
keywait(NULL,1);
freesession(&sp);
return 1;
}
kprintf("Connected\n");
sp->inproc = NULL; /* No longer respond to ^C */
tnrecv(&tn);
return 0;
}
/* Telnet input routine, common to both telnet and ttylink */
void
tnrecv(tn)
struct telnet *tn;
{
int c;
struct session *sp;
char *cp;
kFILE *network;
sp = tn->session;
network = sp->network;
/* Fork off the transmit process */
sp->proc1 = newproc("tel_out",1024,tel_output,0,tn,NULL,0);
/* Process input on the connection */
while((c = kgetc(network)) != kEOF){
if(c != IAC){
/* Ordinary character */
kputchar((char)c);
if(sp->record != NULL)
kputc(c,sp->record);
/* If we're likely to block on the next getc, flush */
if(kfrrdy(network) == 0)
kfflush(kstdout);
continue;
}
/* IAC received, get command sequence */
c = kgetc(network);
switch(c){
case WILL:
c = kgetc(network);
willopt(tn,c);
break;
case WONT:
c = kgetc(network);
wontopt(tn,c);
break;
case DO:
c = kgetc(network);
doopt(tn,c);
break;
case DONT:
c = kgetc(network);
dontopt(tn,c);
break;
case IAC: /* Escaped IAC */
kputchar(IAC);
if(sp->record != NULL)
kputc(IAC,sp->record);
break;
}
}
/* A close was received from the remote host.
* Notify the user, kill the output task and wait for a response
* from the user before freeing the session.
*/
kfmode(sp->output,STREAM_ASCII); /* Restore newline translation */
ksetvbuf(sp->output,NULL,_kIOLBF,kBUFSIZ);
cp = sockerr(kfileno(network));
kprintf("Closed: %s\n", cp != NULL ? cp : "EOF");
killproc(&sp->proc1);
kfclose(sp->network);
sp->network = NULL;
keywait(NULL,1);
freesession(&sp);
}
/* User telnet output task, started by user telnet command */
void
tel_output(unused,tn1,p)
int unused;
void *tn1;
void *p;
{
struct session *sp;
int c;
struct telnet *tn;
tn = (struct telnet *)tn1;
sp = tn->session;
/* Send whatever's typed on the terminal */
while((c = kgetc(sp->input)) != kEOF){
kputc(c,sp->network);
if(!tn->remote[TN_ECHO] && sp->record != NULL)
kputc(c,sp->record);
/* By default, output is transparent in remote echo mode.
* If eolmode is set, turn a cr into cr-null.
* This can only happen when in remote echo (raw) mode, since
* the tty driver normally maps \r to \n in cooked mode.
*/
if(c == '\r' && tn->eolmode)
kputc('\0',sp->network);
if(tn->remote[TN_ECHO])
kfflush(sp->network);
}
/* Make sure our parent doesn't try to kill us after we exit */
sp->proc1 = NULL;
}
int
doecho(argc,argv,p)
int argc;
char *argv[];
void *p;
{
if(argc < 2){
if(Refuse_echo)
kprintf("Refuse\n");
else
kprintf("Accept\n");
} else {
if(argv[1][0] == 'r')
Refuse_echo = 1;
else if(argv[1][0] == 'a')
Refuse_echo = 0;
else
return -1;
}
return 0;
}
/* set for unix end of line for remote echo mode telnet */
int
doeol(argc,argv,p)
int argc;
char *argv[];
void *p;
{
if(argc < 2){
if(Tn_cr_mode)
kprintf("null\n");
else
kprintf("standard\n");
} else {
if(argv[1][0] == 'n')
Tn_cr_mode = 1;
else if(argv[1][0] == 's')
Tn_cr_mode = 0;
else {
kprintf("Usage: %s [standard|null]\n",argv[0]);
return -1;
}
}
return 0;
}
/* The guts of the actual Telnet protocol: negotiating options */
void
willopt(tn,opt)
struct telnet *tn;
int opt;
{
int ack;
if(Topt){
kprintf("recv: will ");
if(opt <= NOPTIONS)
kprintf("%s\n",T_options[opt]);
else
kprintf("%u\n",opt);
}
switch(opt){
case TN_TRANSMIT_BINARY:
case TN_ECHO:
case TN_SUPPRESS_GA:
if(tn->remote[opt] == 1)
return; /* Already set, ignore to prevent loop */
if(opt == TN_ECHO){
if(Refuse_echo){
/* User doesn't want to accept */
ack = DONT;
break;
} else {
/* Put tty into raw mode */
tn->session->ttystate.edit = 0;
tn->session->ttystate.echo = 0;
kfmode(tn->session->network,STREAM_BINARY);
ksetvbuf(tn->session->network,NULL,_kIONBF,0);
kfmode(kstdout,STREAM_BINARY);
/* setvbuf(stdout,NULL,_kIONBF,0); */
}
}
tn->remote[opt] = 1;
ack = DO;
break;
default:
ack = DONT; /* We don't know what he's offering; refuse */
}
answer(tn,ack,opt);
}
void
wontopt(tn,opt)
struct telnet *tn;
int opt;
{
if(Topt){
kprintf("recv: wont ");
if(opt <= NOPTIONS)
kprintf("%s\n",T_options[opt]);
else
kprintf("%u\n",opt);
}
if(opt <= NOPTIONS){
if(tn->remote[opt] == 0)
return; /* Already clear, ignore to prevent loop */
tn->remote[opt] = 0;
if(opt == TN_ECHO){
/* Put tty into cooked mode */
tn->session->ttystate.edit = 1;
tn->session->ttystate.echo = 1;
kfmode(tn->session->network,STREAM_ASCII);
ksetvbuf(tn->session->network,NULL,_kIOLBF,kBUFSIZ);
kfmode(kstdout,STREAM_ASCII);
/* setvbuf(stdout,NULL,_kIOLBF,BUFSIZ); */
}
}
answer(tn,DONT,opt); /* Must always accept */
}
void
doopt(tn,opt)
struct telnet *tn;
int opt;
{
int ack;
if(Topt){
kprintf("recv: do ");
if(opt <= NOPTIONS)
kprintf("%s\n",T_options[opt]);
else
kprintf("%u\n",opt);
}
switch(opt){
case TN_SUPPRESS_GA:
if(tn->local[opt] == 1)
return; /* Already set, ignore to prevent loop */
tn->local[opt] = 1;
ack = WILL;
break;
default:
ack = WONT; /* Don't know what it is */
}
answer(tn,ack,opt);
}
void
dontopt(tn,opt)
struct telnet *tn;
int opt;
{
if(Topt){
kprintf("recv: dont ");
if(opt <= NOPTIONS)
kprintf("%s\n",T_options[opt]);
else
kprintf("%u\n",opt);
}
if(opt <= NOPTIONS){
if(tn->local[opt] == 0){
/* Already clear, ignore to prevent loop */
return;
}
tn->local[opt] = 0;
}
answer(tn,WONT,opt);
}
void
answer(tn,r1,r2)
struct telnet *tn;
int r1,r2;
{
if(Topt){
switch(r1){
case WILL:
kprintf("sent: will ");
break;
case WONT:
kprintf("sent: wont ");
break;
case DO:
kprintf("sent: do ");
break;
case DONT:
kprintf("sent: dont ");
break;
}
if(r2 <= NOPTIONS)
kprintf("%s\n",T_options[r2]);
else
kprintf("%u\n",r2);
}
kfprintf(tn->session->network,"%c%c%c",IAC,r1,r2);
kfflush(tn->session->network);
}
static int
keychar(c)
int c;
{
if(c != CTLC)
return 1; /* Ignore all but ^C */
kfprintf(Current->output,"^C\n");
alert(Current->proc,kEABORT);
return 0;
}