/
commands.c
3012 lines (2773 loc) · 70.8 KB
/
commands.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1988, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if 0
#ifndef lint
static const char sccsid[] = "@(#)commands.c 8.4 (Berkeley) 5/30/95";
#endif
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/un.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <netdb.h>
#include <pwd.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/telnet.h>
#include <arpa/inet.h>
#include "general.h"
#include "ring.h"
#include "externs.h"
#include "defines.h"
#include "types.h"
#include "misc.h"
#ifdef AUTHENTICATION
#include <libtelnet/auth.h>
#endif
#ifdef ENCRYPTION
#include <libtelnet/encrypt.h>
#endif
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 256
#endif
typedef int (*intrtn_t)(int, char **);
#ifdef AUTHENTICATION
extern int auth_togdebug(int);
#endif
#ifdef ENCRYPTION
extern int EncryptAutoEnc(int);
extern int EncryptAutoDec(int);
extern int EncryptDebug(int);
extern int EncryptVerbose(int);
#endif /* ENCRYPTION */
#if defined(IPPROTO_IP) && defined(IP_TOS)
int tos = -1;
#endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
char *hostname;
static char _hostname[MAXHOSTNAMELEN];
static int help(int, char **);
static int call(intrtn_t, ...);
static void cmdrc(char *, char *);
#ifdef INET6
static int switch_af(struct addrinfo **);
#endif
static int togglehelp(void);
static int send_tncmd(void (*)(int, int), const char *, char *);
static int setmod(int);
static int clearmode(int);
static int modehelp(void);
static int sourceroute(struct addrinfo *, char *, unsigned char **, int *, int *, int *);
typedef struct {
const char *name; /* command name */
const char *help; /* help string (NULL for no help) */
int (*handler)(int, char **); /* routine which executes command */
int needconnect; /* Do we need to be connected to execute? */
} Command;
static char line[256];
static char saveline[256];
static int margc;
static char *margv[20];
#ifdef OPIE
#include <sys/wait.h>
#define PATH_OPIEKEY "/usr/bin/opiekey"
static int
opie_calc(int argc, char *argv[])
{
int status;
if(argc != 3) {
printf("%s sequence challenge\n", argv[0]);
return (0);
}
switch(fork()) {
case 0:
execv(PATH_OPIEKEY, argv);
exit (1);
case -1:
perror("fork");
break;
default:
(void) wait(&status);
if (WIFEXITED(status))
return (WEXITSTATUS(status));
}
return (0);
}
#endif
static void
makeargv(void)
{
char *cp, *cp2, c;
char **argp = margv;
margc = 0;
cp = line;
if (*cp == '!') { /* Special case shell escape */
strcpy(saveline, line); /* save for shell command */
*argp++ = strdup("!"); /* No room in string to get this */
margc++;
cp++;
}
while ((c = *cp)) {
int inquote = 0;
while (isspace(c))
c = *++cp;
if (c == '\0')
break;
*argp++ = cp;
margc += 1;
for (cp2 = cp; c != '\0'; c = *++cp) {
if (inquote) {
if (c == inquote) {
inquote = 0;
continue;
}
} else {
if (c == '\\') {
if ((c = *++cp) == '\0')
break;
} else if (c == '"') {
inquote = '"';
continue;
} else if (c == '\'') {
inquote = '\'';
continue;
} else if (isspace(c))
break;
}
*cp2++ = c;
}
*cp2 = '\0';
if (c == '\0')
break;
cp++;
}
*argp++ = 0;
}
/*
* Make a character string into a number.
*
* Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
*/
static int
special(char *s)
{
char c;
char b;
switch (*s) {
case '^':
b = *++s;
if (b == '?') {
c = b | 0x40; /* DEL */
} else {
c = b & 0x1f;
}
break;
default:
c = *s;
break;
}
return c;
}
/*
* Construct a control character sequence
* for a special character.
*/
static const char *
control(cc_t c)
{
static char buf[5];
/*
* The only way I could get the Sun 3.5 compiler
* to shut up about
* if ((unsigned int)c >= 0x80)
* was to assign "c" to an unsigned int variable...
* Arggg....
*/
unsigned int uic = (unsigned int)c;
if (uic == 0x7f)
return ("^?");
if (c == (cc_t)_POSIX_VDISABLE) {
return "off";
}
if (uic >= 0x80) {
buf[0] = '\\';
buf[1] = ((c>>6)&07) + '0';
buf[2] = ((c>>3)&07) + '0';
buf[3] = (c&07) + '0';
buf[4] = 0;
} else if (uic >= 0x20) {
buf[0] = c;
buf[1] = 0;
} else {
buf[0] = '^';
buf[1] = '@'+c;
buf[2] = 0;
}
return (buf);
}
/*
* The following are data structures and routines for
* the "send" command.
*
*/
struct sendlist {
const char *name; /* How user refers to it (case independent) */
const char *help; /* Help information (0 ==> no help) */
int needconnect; /* Need to be connected */
int narg; /* Number of arguments */
int (*handler)(char *, ...); /* Routine to perform (for special ops) */
int nbyte; /* Number of bytes to send this command */
int what; /* Character to be sent (<0 ==> special) */
};
static int
send_esc(void),
send_help(void),
send_docmd(char *),
send_dontcmd(char *),
send_willcmd(char *),
send_wontcmd(char *);
static struct sendlist Sendlist[] = {
{ "ao", "Send Telnet Abort output", 1, 0, NULL, 2, AO },
{ "ayt", "Send Telnet 'Are You There'", 1, 0, NULL, 2, AYT },
{ "brk", "Send Telnet Break", 1, 0, NULL, 2, BREAK },
{ "break", NULL, 1, 0, NULL, 2, BREAK },
{ "ec", "Send Telnet Erase Character", 1, 0, NULL, 2, EC },
{ "el", "Send Telnet Erase Line", 1, 0, NULL, 2, EL },
{ "escape", "Send current escape character",1, 0, (int (*)(char *, ...))send_esc, 1, 0 },
{ "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL, 2, GA },
{ "ip", "Send Telnet Interrupt Process",1, 0, NULL, 2, IP },
{ "intp", NULL, 1, 0, NULL, 2, IP },
{ "interrupt", NULL, 1, 0, NULL, 2, IP },
{ "intr", NULL, 1, 0, NULL, 2, IP },
{ "nop", "Send Telnet 'No operation'", 1, 0, NULL, 2, NOP },
{ "eor", "Send Telnet 'End of Record'", 1, 0, NULL, 2, EOR },
{ "abort", "Send Telnet 'Abort Process'", 1, 0, NULL, 2, ABORT },
{ "susp", "Send Telnet 'Suspend Process'",1, 0, NULL, 2, SUSP },
{ "eof", "Send Telnet End of File Character", 1, 0, NULL, 2, xEOF },
{ "synch", "Perform Telnet 'Synch operation'", 1, 0, (int (*)(char *, ...))dosynch, 2, 0 },
{ "getstatus", "Send request for STATUS", 1, 0, (int (*)(char *, ...))get_status, 6, 0 },
{ "?", "Display send options", 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
{ "help", NULL, 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
{ "do", NULL, 0, 1, (int (*)(char *, ...))send_docmd, 3, 0 },
{ "dont", NULL, 0, 1, (int (*)(char *, ...))send_dontcmd, 3, 0 },
{ "will", NULL, 0, 1, (int (*)(char *, ...))send_willcmd, 3, 0 },
{ "wont", NULL, 0, 1, (int (*)(char *, ...))send_wontcmd, 3, 0 },
{ NULL, NULL, 0, 0, NULL, 0, 0 }
};
#define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
sizeof(struct sendlist)))
static int
sendcmd(int argc, char *argv[])
{
int count; /* how many bytes we are going to need to send */
int i;
struct sendlist *s; /* pointer to current command */
int success = 0;
int needconnect = 0;
if (argc < 2) {
printf("need at least one argument for 'send' command\n");
printf("'send ?' for help\n");
return 0;
}
/*
* First, validate all the send arguments.
* In addition, we see how much space we are going to need, and
* whether or not we will be doing a "SYNCH" operation (which
* flushes the network queue).
*/
count = 0;
for (i = 1; i < argc; i++) {
s = GETSEND(argv[i]);
if (s == 0) {
printf("Unknown send argument '%s'\n'send ?' for help.\n",
argv[i]);
return 0;
} else if (Ambiguous((void *)s)) {
printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
argv[i]);
return 0;
}
if (i + s->narg >= argc) {
fprintf(stderr,
"Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
return 0;
}
count += s->nbyte;
if ((void *)s->handler == (void *)send_help) {
send_help();
return 0;
}
i += s->narg;
needconnect += s->needconnect;
}
if (!connected && needconnect) {
printf("?Need to be connected first.\n");
printf("'send ?' for help\n");
return 0;
}
/* Now, do we have enough room? */
if (NETROOM() < count) {
printf("There is not enough room in the buffer TO the network\n");
printf("to process your request. Nothing will be done.\n");
printf("('send synch' will throw away most data in the network\n");
printf("buffer, if this might help.)\n");
return 0;
}
/* OK, they are all OK, now go through again and actually send */
count = 0;
for (i = 1; i < argc; i++) {
if ((s = GETSEND(argv[i])) == 0) {
fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
quit();
/*NOTREACHED*/
}
if (s->handler) {
count++;
success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
(s->narg > 1) ? argv[i+2] : 0);
i += s->narg;
} else {
NET2ADD(IAC, s->what);
printoption("SENT", IAC, s->what);
}
}
return (count == success);
}
static int
send_esc(void)
{
NETADD(escape);
return 1;
}
static int
send_docmd(char *name)
{
return(send_tncmd(send_do, "do", name));
}
static int
send_dontcmd(name)
char *name;
{
return(send_tncmd(send_dont, "dont", name));
}
static int
send_willcmd(char *name)
{
return(send_tncmd(send_will, "will", name));
}
static int
send_wontcmd(char *name)
{
return(send_tncmd(send_wont, "wont", name));
}
static int
send_tncmd(void (*func)(int, int), const char *cmd, char *name)
{
char **cpp;
extern char *telopts[];
int val = 0;
if (isprefix(name, "help") || isprefix(name, "?")) {
int col, len;
printf("usage: send %s <value|option>\n", cmd);
printf("\"value\" must be from 0 to 255\n");
printf("Valid options are:\n\t");
col = 8;
for (cpp = telopts; *cpp; cpp++) {
len = strlen(*cpp) + 3;
if (col + len > 65) {
printf("\n\t");
col = 8;
}
printf(" \"%s\"", *cpp);
col += len;
}
printf("\n");
return 0;
}
cpp = (char **)genget(name, telopts, sizeof(char *));
if (Ambiguous(cpp)) {
fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
name, cmd);
return 0;
}
if (cpp) {
val = cpp - telopts;
} else {
char *cp = name;
while (*cp >= '0' && *cp <= '9') {
val *= 10;
val += *cp - '0';
cp++;
}
if (*cp != 0) {
fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
name, cmd);
return 0;
} else if (val < 0 || val > 255) {
fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
name, cmd);
return 0;
}
}
if (!connected) {
printf("?Need to be connected first.\n");
return 0;
}
(*func)(val, 1);
return 1;
}
static int
send_help(void)
{
struct sendlist *s; /* pointer to current command */
for (s = Sendlist; s->name; s++) {
if (s->help)
printf("%-15s %s\n", s->name, s->help);
}
return(0);
}
/*
* The following are the routines and data structures referred
* to by the arguments to the "toggle" command.
*/
static int
lclchars(void)
{
donelclchars = 1;
return 1;
}
static int
togdebug(void)
{
#ifndef NOT43
if (net > 0 &&
(SetSockOpt(net, SOL_SOCKET, SO_DEBUG, telnet_debug)) < 0) {
perror("setsockopt (SO_DEBUG)");
}
#else /* NOT43 */
if (telnet_debug) {
if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0)
perror("setsockopt (SO_DEBUG)");
} else
printf("Cannot turn off socket debugging\n");
#endif /* NOT43 */
return 1;
}
static int
togcrlf(void)
{
if (crlf) {
printf("Will send carriage returns as telnet <CR><LF>.\n");
} else {
printf("Will send carriage returns as telnet <CR><NUL>.\n");
}
return 1;
}
int binmode;
static int
togbinary(int val)
{
donebinarytoggle = 1;
if (val >= 0) {
binmode = val;
} else {
if (my_want_state_is_will(TELOPT_BINARY) &&
my_want_state_is_do(TELOPT_BINARY)) {
binmode = 1;
} else if (my_want_state_is_wont(TELOPT_BINARY) &&
my_want_state_is_dont(TELOPT_BINARY)) {
binmode = 0;
}
val = binmode ? 0 : 1;
}
if (val == 1) {
if (my_want_state_is_will(TELOPT_BINARY) &&
my_want_state_is_do(TELOPT_BINARY)) {
printf("Already operating in binary mode with remote host.\n");
} else {
printf("Negotiating binary mode with remote host.\n");
tel_enter_binary(3);
}
} else {
if (my_want_state_is_wont(TELOPT_BINARY) &&
my_want_state_is_dont(TELOPT_BINARY)) {
printf("Already in network ascii mode with remote host.\n");
} else {
printf("Negotiating network ascii mode with remote host.\n");
tel_leave_binary(3);
}
}
return 1;
}
static int
togrbinary(int val)
{
donebinarytoggle = 1;
if (val == -1)
val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
if (val == 1) {
if (my_want_state_is_do(TELOPT_BINARY)) {
printf("Already receiving in binary mode.\n");
} else {
printf("Negotiating binary mode on input.\n");
tel_enter_binary(1);
}
} else {
if (my_want_state_is_dont(TELOPT_BINARY)) {
printf("Already receiving in network ascii mode.\n");
} else {
printf("Negotiating network ascii mode on input.\n");
tel_leave_binary(1);
}
}
return 1;
}
static int
togxbinary(int val)
{
donebinarytoggle = 1;
if (val == -1)
val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
if (val == 1) {
if (my_want_state_is_will(TELOPT_BINARY)) {
printf("Already transmitting in binary mode.\n");
} else {
printf("Negotiating binary mode on output.\n");
tel_enter_binary(2);
}
} else {
if (my_want_state_is_wont(TELOPT_BINARY)) {
printf("Already transmitting in network ascii mode.\n");
} else {
printf("Negotiating network ascii mode on output.\n");
tel_leave_binary(2);
}
}
return 1;
}
struct togglelist {
const char *name; /* name of toggle */
const char *help; /* help message */
int (*handler)(int); /* routine to do actual setting */
int *variable;
const char *actionexplanation;
};
static struct togglelist Togglelist[] = {
{ "autoflush",
"flushing of output when sending interrupt characters",
0,
&autoflush,
"flush output when sending interrupt characters" },
{ "autosynch",
"automatic sending of interrupt characters in urgent mode",
0,
&autosynch,
"send interrupt characters in urgent mode" },
#ifdef AUTHENTICATION
{ "autologin",
"automatic sending of login and/or authentication info",
0,
&autologin,
"send login name and/or authentication information" },
{ "authdebug",
"Toggle authentication debugging",
auth_togdebug,
0,
"print authentication debugging information" },
#endif
#ifdef ENCRYPTION
{ "autoencrypt",
"automatic encryption of data stream",
EncryptAutoEnc,
0,
"automatically encrypt output" },
{ "autodecrypt",
"automatic decryption of data stream",
EncryptAutoDec,
0,
"automatically decrypt input" },
{ "verbose_encrypt",
"Toggle verbose encryption output",
EncryptVerbose,
0,
"print verbose encryption output" },
{ "encdebug",
"Toggle encryption debugging",
EncryptDebug,
0,
"print encryption debugging information" },
#endif /* ENCRYPTION */
{ "skiprc",
"don't read ~/.telnetrc file",
0,
&skiprc,
"skip reading of ~/.telnetrc file" },
{ "binary",
"sending and receiving of binary data",
togbinary,
0,
0 },
{ "inbinary",
"receiving of binary data",
togrbinary,
0,
0 },
{ "outbinary",
"sending of binary data",
togxbinary,
0,
0 },
{ "crlf",
"sending carriage returns as telnet <CR><LF>",
(int (*)(int))togcrlf,
&crlf,
0 },
{ "crmod",
"mapping of received carriage returns",
0,
&crmod,
"map carriage return on output" },
{ "localchars",
"local recognition of certain control characters",
(int (*)(int))lclchars,
&localchars,
"recognize certain control characters" },
{ " ", "", NULL, NULL, NULL }, /* empty line */
{ "debug",
"debugging",
(int (*)(int))togdebug,
&telnet_debug,
"turn on socket level debugging" },
{ "netdata",
"printing of hexadecimal network data (debugging)",
0,
&netdata,
"print hexadecimal representation of network traffic" },
{ "prettydump",
"output of \"netdata\" to user readable format (debugging)",
0,
&prettydump,
"print user readable output for \"netdata\"" },
{ "options",
"viewing of options processing (debugging)",
0,
&showoptions,
"show option processing" },
{ "termdata",
"(debugging) toggle printing of hexadecimal terminal data",
0,
&termdata,
"print hexadecimal representation of terminal traffic" },
{ "?",
NULL,
(int (*)(int))togglehelp,
NULL,
NULL },
{ NULL, NULL, NULL, NULL, NULL },
{ "help",
NULL,
(int (*)(int))togglehelp,
NULL,
NULL },
{ NULL, NULL, NULL, NULL, NULL }
};
static int
togglehelp(void)
{
struct togglelist *c;
for (c = Togglelist; c->name; c++) {
if (c->help) {
if (*c->help)
printf("%-15s toggle %s\n", c->name, c->help);
else
printf("\n");
}
}
printf("\n");
printf("%-15s %s\n", "?", "display help information");
return 0;
}
static void
settogglehelp(int set)
{
struct togglelist *c;
for (c = Togglelist; c->name; c++) {
if (c->help) {
if (*c->help)
printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
c->help);
else
printf("\n");
}
}
}
#define GETTOGGLE(name) (struct togglelist *) \
genget(name, (char **) Togglelist, sizeof(struct togglelist))
static int
toggle(int argc, char *argv[])
{
int retval = 1;
char *name;
struct togglelist *c;
if (argc < 2) {
fprintf(stderr,
"Need an argument to 'toggle' command. 'toggle ?' for help.\n");
return 0;
}
argc--;
argv++;
while (argc--) {
name = *argv++;
c = GETTOGGLE(name);
if (Ambiguous((void *)c)) {
fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
name);
return 0;
} else if (c == 0) {
fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
name);
return 0;
} else {
if (c->variable) {
*c->variable = !*c->variable; /* invert it */
if (c->actionexplanation) {
printf("%s %s.\n", *c->variable? "Will" : "Won't",
c->actionexplanation);
}
}
if (c->handler) {
retval &= (*c->handler)(-1);
}
}
}
return retval;
}
/*
* The following perform the "set" command.
*/
#ifdef USE_TERMIO
struct termio new_tc = { 0, 0, 0, 0, {}, 0, 0 };
#endif
struct setlist {
const char *name; /* name */
const char *help; /* help information */
void (*handler)(char *);
cc_t *charp; /* where it is located at */
};
static struct setlist Setlist[] = {
#ifdef KLUDGELINEMODE
{ "echo", "character to toggle local echoing on/off", NULL, &echoc },
#endif
{ "escape", "character to escape back to telnet command mode", NULL, &escape },
{ "rlogin", "rlogin escape character", 0, &rlogin },
{ "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
{ " ", "", NULL, NULL },
{ " ", "The following need 'localchars' to be toggled true", NULL, NULL },
{ "flushoutput", "character to cause an Abort Output", NULL, termFlushCharp },
{ "interrupt", "character to cause an Interrupt Process", NULL, termIntCharp },
{ "quit", "character to cause an Abort process", NULL, termQuitCharp },
{ "eof", "character to cause an EOF ", NULL, termEofCharp },
{ " ", "", NULL, NULL },
{ " ", "The following are for local editing in linemode", NULL, NULL },
{ "erase", "character to use to erase a character", NULL, termEraseCharp },
{ "kill", "character to use to erase a line", NULL, termKillCharp },
{ "lnext", "character to use for literal next", NULL, termLiteralNextCharp },
{ "susp", "character to cause a Suspend Process", NULL, termSuspCharp },
{ "reprint", "character to use for line reprint", NULL, termRprntCharp },
{ "worderase", "character to use to erase a word", NULL, termWerasCharp },
{ "start", "character to use for XON", NULL, termStartCharp },
{ "stop", "character to use for XOFF", NULL, termStopCharp },
{ "forw1", "alternate end of line character", NULL, termForw1Charp },
{ "forw2", "alternate end of line character", NULL, termForw2Charp },
{ "ayt", "alternate AYT character", NULL, termAytCharp },
{ "baudrate", "set remote baud rate", DoBaudRate, ComPortBaudRate },
{ NULL, NULL, NULL, NULL }
};
static struct setlist *
getset(char *name)
{
return (struct setlist *)
genget(name, (char **) Setlist, sizeof(struct setlist));
}
void
set_escape_char(char *s)
{
if (rlogin != _POSIX_VDISABLE) {
rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
printf("Telnet rlogin escape character is '%s'.\n",
control(rlogin));
} else {
escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
printf("Telnet escape character is '%s'.\n", control(escape));
}
}
static int
setcmd(int argc, char *argv[])
{
int value;
struct setlist *ct;
struct togglelist *c;
if (argc < 2 || argc > 3) {
printf("Format is 'set Name Value'\n'set ?' for help.\n");
return 0;
}
if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
for (ct = Setlist; ct->name; ct++)
printf("%-15s %s\n", ct->name, ct->help);
printf("\n");
settogglehelp(1);
printf("%-15s %s\n", "?", "display help information");
return 0;
}
ct = getset(argv[1]);
if (ct == 0) {
c = GETTOGGLE(argv[1]);
if (c == 0) {
fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
argv[1]);
return 0;
} else if (Ambiguous((void *)c)) {
fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
argv[1]);
return 0;
}
if (c->variable) {
if ((argc == 2) || (strcmp("on", argv[2]) == 0))
*c->variable = 1;
else if (strcmp("off", argv[2]) == 0)
*c->variable = 0;
else {
printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
return 0;
}
if (c->actionexplanation) {
printf("%s %s.\n", *c->variable? "Will" : "Won't",
c->actionexplanation);
}
}
if (c->handler)
(*c->handler)(1);
} else if (argc != 3) {
printf("Format is 'set Name Value'\n'set ?' for help.\n");
return 0;
} else if (Ambiguous((void *)ct)) {
fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
argv[1]);
return 0;
} else if (ct->handler) {
(*ct->handler)(argv[2]);
printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
} else {
if (strcmp("off", argv[2])) {
value = special(argv[2]);
} else {
value = _POSIX_VDISABLE;
}
*(ct->charp) = (cc_t)value;
printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
}
slc_check();
return 1;
}
static int
unsetcmd(int argc, char *argv[])
{
struct setlist *ct;
struct togglelist *c;
char *name;