forked from yellowman/nsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.c
2178 lines (1943 loc) · 53.6 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) 2002-2008 Chris Cappuccio <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* 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. 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/fcntl.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <sys/reboot.h>
#include <sys/sockio.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/route.h>
#include <limits.h>
#include <util.h>
#include <pwd.h>
#include "editing.h"
#include "stringlist.h"
#include "externs.h"
#include "sysctl.h"
char prompt[128];
char line[1024];
char saveline[1024];
int margc;
char hname[HSIZE];
static char hbuf[MAXHOSTNAMELEN]; /* host name */
static char ifname[IFNAMSIZ]; /* interface name */
struct intlist *whichlist;
#define NARGS sizeof(line)/2 /* max arguments in char line[] */
char *margv[NARGS]; /* argv storage */
size_t cursor_argc; /* location of cursor in margv */
size_t cursor_argo; /* offset of cursor margv[cursor_argc] */
pid_t child;
static int quit(void);
static int disable(void);
static int doverbose(int, char**);
static int doediting(int, char**);
int rtable(int, char**);
int group(int, char**);
static int nsh_setrtable(int);
static int pr_routes(int, char **);
static int pr_routes6(int, char **);
static int pr_arp(int, char **);
static int pr_sadb(int, char **);
static int pr_kernel(int, char **);
static int pr_prot1(int, char **);
static int pr_dhcp(int, char **);
static int pr_conf(int, char **);
static int pr_s_conf(int, char **);
static int show_hostname(int, char **);
static int wr_startup(void);
static int wr_conf(char *);
static int show_help(int, char **);
static int sysctlhelp(int, char **, char **, int);
static int flush_pf(char *);
static int flush_help(void);
static int flush_line(char *);
static int flush_ip_routes(void);
static int flush_arp_cache(void);
static int flush_history(void);
static int int_help(void);
static int el_burrito(EditLine *, int, char **);
static int hostname(int, char **);
static int help(int, char**);
static int shell(int, char*[]);
static int ping(int, char*[]);
static int ping6(int, char*[]);
static int traceroute(int, char*[]);
static int traceroute6(int, char*[]);
static int ssh(int, char*[]);
static int telnet(int, char*[]);
void p_argv(int, char **);
static int notvalid(void);
static int nreboot(void);
static int halt(void);
static Command *getcmd(char *);
static void pf_stats(void);
static void sigalarm(int);
#include "commands.h"
void sigalarm(int blahfart)
{
if (child != -1) {
kill(child, SIGKILL);
}
}
/*
* Quit command
*/
int
quit(void)
{
printf("%% Session terminated.\n");
exit(0);
return 0;
}
/*
* Data structures and routines for the "show" command.
*/
Menu showlist[] = {
{ "hostname", "Router hostname", CMPL0 0, 0, 0, 0, show_hostname },
{ "interface", "Interface config", CMPL(i) 0, 0, 0, 1, show_int },
{ "route", "IPv4 route table or route lookup", CMPL0 0, 0, 0, 1, pr_routes },
{ "route6", "IPv6 route table or route lookup", CMPL0 0, 0, 0, 1, pr_routes6 },
{ "sadb", "Security Association Database", CMPL0 0, 0, 0, 0, pr_sadb },
{ "arp", "ARP table", CMPL0 0, 0, 0, 1, pr_arp },
{ "kernel", "Kernel statistics", CMPL(ta) (char **)stts, sizeof(struct stt), 0, 1, pr_kernel },
{ "bgp", "BGP information", CMPL(ta) (char **)bgcs, sizeof(struct prot1), 0, 4, pr_prot1 },
{ "ospf", "OSPF information", CMPL(ta) (char **)oscs, sizeof(struct prot1), 0, 3, pr_prot1 },
{ "ospf6", "OSPF6 information", CMPL(ta) (char **)os6cs, sizeof(struct prot1), 0, 3, pr_prot1 },
{ "rip", "RIP information", CMPL(ta) (char **)rics, sizeof(struct prot1), 0, 3, pr_prot1 },
{ "ldp", "LDP information", CMPL(ta) (char **)lics, sizeof(struct prot1), 0, 3, pr_prot1 },
{ "ike", "IKE information", CMPL(ta) (char **)ikcs, sizeof(struct prot1), 0, 3, pr_prot1 },
{ "dvmrp", "DVMRP information", CMPL(ta) (char **)dvcs, sizeof(struct prot1), 0, 2, pr_prot1 },
{ "relay", "Relay server", CMPL(ta) (char **)rlcs, sizeof(struct prot1), 0, 1, pr_prot1 },
{ "dhcp", "DHCP server", CMPL(ta) (char **)dhcs, sizeof(struct prot1), 0, 1, pr_dhcp },
{ "smtp", "SMTP server", CMPL(ta) (char **)smcs, sizeof(struct prot1), 0, 1, pr_prot1 },
{ "ldap", "LDAP server", CMPL(ta) (char **)ldcs, sizeof(struct prot1), 0, 1, pr_prot1 },
{ "monitor", "Monitor routing/arp table changes", CMPL0 0, 0, 0, 0, monitor },
{ "version", "Software information", CMPL0 0, 0, 0, 0, version },
{ "users", "System users", CMPL0 0, 0, 0, 0, who },
{ "running-config", "Operating configuration", CMPL0 0, 0, 0, 0, pr_conf },
{ "startup-config", "Startup configuration", CMPL0 0, 0, 0, 0, pr_s_conf },
{ "?", "Options", CMPL0 0, 0, 0, 0, show_help },
{ "help", 0, CMPL0 0, 0, 0, 0, show_help },
{ 0, 0, 0, 0, 0 }
};
static int
showcmd(int argc, char **argv)
{
Menu *s; /* pointer to current command */
int success = 0;
if (argc < 2) {
show_help(argc, argv);
return 0;
}
/*
* Validate show argument
*/
s = (Menu *) genget(argv[1], (char **) showlist, sizeof(Menu));
if (s == 0) {
printf("%% Invalid argument %s\n", argv[1]);
return 0;
} else if (Ambiguous(s)) {
printf("%% Ambiguous argument %s\n", argv[1]);
return 0;
}
if (((s->minarg + 2) > argc) || ((s->maxarg + 2) < argc)) {
printf("%% Wrong number of argument%s to 'show %s' command"
" (min %i, max %i)\n", argc <= 2 ? "" : "s", s->name,
s->minarg, s->maxarg);
return 0;
}
if (s->handler) /* As if there was something else we do ? */
success = (*s->handler)(argc, argv);
return(success);
}
static int
show_help(int argc, char **argv)
{
Menu *s; /* pointer to current command */
u_int z = 0;
printf("%% Commands may be abbreviated.\n");
printf("%% 'show' commands are:\n\n");
for (s = showlist; s->name; s++) {
if (strlen(s->name) > z)
z = strlen(s->name);
}
for (s = showlist; s->name; s++) {
if (s->help)
printf(" %-*s %s\n", z, s->name, s->help);
}
return 0;
}
/*
* Data structures and routines for the "ip" command.
*/
Menu iptab[] = {
{ "carp", "Allow CARP", CMPL0 0, 0, 0, 0, ipsysctl },
{ "carp-log", "CARP Logging Priority", CMPL0 0, 0, 0, 1, ipsysctl },
{ "carp-preempt", "CARP Virtual Host Preemption", CMPL0 0, 0, 0, 0, ipsysctl },
{ "forwarding", "Enable IPv4 Forwarding", CMPL0 0, 0, 0, 0, ipsysctl },
{ "mforwarding", "Enable IPv4 Multicast Forwarding", CMPL0 0, 0, 0, 0, ipsysctl },
{ "ipip", "Allow IP-in-IP Encapsulation", CMPL0 0, 0, 0, 0, ipsysctl },
{ "gre", "Allow Generic Route Encapsulation", CMPL0 0, 0, 0, 0, ipsysctl },
{ "wccp", "Allow Web Cache Control Protocol", CMPL0 0, 0, 0, 0, ipsysctl },
{ "mobileip", "Allow Mobile IP Encapsulation", CMPL0 0, 0, 0, 0, ipsysctl },
{ "etherip", "Allow Ether-IP Encapsulation", CMPL0 0, 0, 0, 0, ipsysctl },
{ "ipcomp", "Allow IP Compression", CMPL0 0, 0, 0, 0, ipsysctl },
{ "esp", "Allow Encapsulated Security Payload", CMPL0 0, 0, 0, 0, ipsysctl },
{ "esp-udpencap","Allow ESP encapsulation within UDP", CMPL0 0, 0, 0, 0, ipsysctl },
{ "esp-udpencap-port","UDP port number for encapsulation", CMPL0 0, 0, 0, 1, ipsysctl },
{ "ah", "Allow Authentication Header", CMPL0 0, 0, 0, 0, ipsysctl },
{ "sourceroute", "Process Loose/Strict Source Route Options", CMPL0 0, 0, 0, 0, ipsysctl },
{ "encdebug", "Enable if_enc debugging", CMPL0 0, 0, 0, 0, ipsysctl },
{ "send-redirects", "Send ICMP redirects", CMPL0 0, 0, 0, 0, ipsysctl },
{ "ifq-maxlen", "IP IFQ maxlen", CMPL0 0, 0, 0, 1, ipsysctl },
{ "directed-broadcast", "Allow directed broadcasts", CMPL0 0, 0, 0, 0, ipsysctl },
{ "multipath", "Multipath routing", CMPL0 0, 0, 0, 0, ipsysctl },
#ifdef notyet
{ "default-mtu", "Default interface MTU", CMPL0 0, 0, 1, 1, ipsysctl },
#endif
{ "default-ttl", "Set Default IP packet TTL", CMPL0 0, 0, 1, 1, ipsysctl },
{ "?", "Options", CMPL0 0, 0, 0, 0, sysctlhelp },
{ 0, 0, 0, 0, 0, 0, 0, 0 }
};
Menu ip6tab[] = {
{ "forwarding", "Enable IPv6 Forwarding", CMPL0 0, 0, 0, 0, ipsysctl },
{ "mforwarding", "Enable IPv6 Multicast Forwarding", CMPL0 0, 0, 0, 0, ipsysctl },
{ "multipath", "Multipath routing", CMPL0 0, 0, 0, 0, ipsysctl },
{ "v6only", "IPv6 only", CMPL0 0, 0, 0, 0, ipsysctl },
{ "maxifprefixes", "Max if IPv6 Prefixes", CMPL0 0, 0, 0, 0, ipsysctl },
{ "maxifdefrouters", "Max if IPv6 Def Routers", CMPL0 0, 0, 0, 0, ipsysctl },
{ "maxdynroutes", "Max IPv6 Dyn Routes", CMPL0 0, 0, 0, 0, ipsysctl },
{ "accept-rtadv", "Accept Router Advertisements", CMPL0 0, 0, 0, 0, ipsysctl },
{ "?", "Help", CMPL0 0, 0, 0, 0, sysctlhelp },
{ 0, 0, 0, 0, 0, 0, 0, 0 }
};
Menu mplstab[] = {
{ "ttl", "MPLS ttl", CMPL0 0, 0, 0, 1, ipsysctl },
{ "ifq-maxlen", "MPLS IFQ maxlen", CMPL0 0, 0, 0, 1, ipsysctl },
{ "mapttl-ip", "MPLS mapttl IPv4", CMPL0 0, 0, 0, 1, ipsysctl },
{ "mapttl-ip6", "MPLS mapttl IPv6", CMPL0 0, 0, 0, 1, ipsysctl },
{ "?", "Help", CMPL0 0, 0, 0, 0, sysctlhelp },
{ 0, 0, 0, 0, 0, 0, 0, 0 }
};
Menu ddbtab[] = {
{ "panic", "DDB panic", CMPL0 0, 0, 0, 1, ipsysctl },
{ "console", "DDB console", CMPL0 0, 0, 0, 1, ipsysctl },
{ "log", "DDB log", CMPL0 0, 0, 0, 1, ipsysctl },
{ "?", "Help", CMPL0 0, 0, 0, 0, sysctlhelp },
{ 0, 0, 0, 0, 0, 0, 0, 0 }
};
Menu pipextab[] = {
{ "enable", "PIPEX enable", CMPL0 0, 0, 0, 1, ipsysctl },
{ "?", "Help", CMPL0 0, 0, 0, 0, sysctlhelp },
{ 0, 0, 0, 0, 0, 0, 0, 0 }
};
static int
ipcmd(int argc, char **argv)
{
Menu *i; /* pointer to current command */
struct sysctltab *stab;
int set, success = 0;
if (NO_ARG(argv[0])) {
argv++;
argc--;
set = 0;
} else
set = 1;
/*
* Find ourself in the great divide
*/
stab = (struct sysctltab *)genget(argv[0], (char **)sysctls,
sizeof(struct sysctltab));
if (stab == 0) {
printf("%% Invalid argument %s\n", argv[0]);
return 0;
} else if (Ambiguous(stab)) {
printf("%% Ambiguous argument %s\n", argv[0]);
return 0;
}
if (argc < 2) {
sysctlhelp(0, NULL, NULL, stab->pf);
return 0;
}
/*
* Validate ip argument
*/
i = (Menu *)genget(argv[1], (char **)stab->table, sizeof(Menu));
if (i == 0) {
printf("%% Invalid argument %s\n", argv[1]);
return 0;
} else if (Ambiguous(i)) {
printf("%% Ambiguous argument %s\n", argv[1]);
return 0;
}
if (((i->minarg + 2) > argc) || ((i->maxarg + 2) < argc)) {
printf("%% Wrong argument%s to '%s %s' command.\n",
argv[0], argc <= 2 ? "" : "s", i->name);
return 0;
}
if (i->handler)
success = (*i->handler)(set, argv[1],
(i->maxarg > 0) ? argv[2] : 0, stab->pf);
return(success);
}
static int
sysctlhelp(int unused1, char **unused2, char **unused3, int type)
{
Menu *i = NULL, *j = NULL; /* pointer to current command */
char *prefix = NULL;
u_int z = 0;
struct sysctltab *stab;
for (stab = sysctls; stab->name != NULL; stab++)
if (stab->pf == type) {
prefix = stab->name;
i = j = stab->table;
break;
}
if (stab->pf != type) {
printf("%% table lookup failed (%d)\n", type);
return 0;
}
printf("%% Commands may be abbreviated.\n");
printf("%% '%s' commands are:\n\n", prefix);
for (; i && i->name; i++) {
if (strlen(i->name) > z)
z = strlen(i->name);
}
for (; j && j->name; j++) {
if (j->help)
printf(" %-*s %s\n", z, j->name, j->help);
}
return 0;
}
/*
* Data structures and routines for the "flush" command.
*/
Menu flushlist[] = {
{ "routes", "IP routes", CMPL0 0, 0, 0, 0, flush_ip_routes },
{ "arp", "ARP cache", CMPL0 0, 0, 0, 0, flush_arp_cache },
{ "line", "Active user", CMPL0 0, 0, 1, 1, flush_line },
{ "bridge-dyn", "Dynamically learned bridge addresses", CMPL0 0, 0, 1, 1, flush_bridgedyn },
{ "bridge-all", "Dynamic and static bridge addresses", CMPL0 0, 0, 1, 1, flush_bridgeall },
{ "bridge-rule", "Layer 2 filter rules for a bridge member port", CMPL0 0, 0, 2, 2, flush_bridgerule },
{ "pf", "pf NAT/filter/queue rules, states, tables", CMPL(t) (char**)fpfs, sizeof(struct fpf), 0, 1, flush_pf },
{ "history", "Command history", CMPL0 0, 0, 0, 0, flush_history },
{ "?", "Options", CMPL0 0, 0, 0, 0, flush_help },
{ "help", 0, CMPL0 0, 0, 0, 0, flush_help },
{ 0, 0, 0, 0, 0 }
};
static int
flushcmd(int argc, char **argv)
{
Menu *f;
if (argc < 2) {
flush_help();
return 0;
}
/*
* Validate flush argument
*/
f = (Menu *) genget(argv[1], (char **)flushlist, sizeof(Menu));
if (f == 0) {
printf("%% Invalid argument %s\n", argv[1]);
return 0;
} else if (Ambiguous(f)) {
printf("%% Ambiguous argument %s\n", argv[1]);
return 0;
}
if (((f->minarg + 2) > argc) || ((f->maxarg + 2) < argc)) {
printf("%% Wrong argument%s to 'flush %s' command.\n",
argc <= 2 ? "" : "s", f->name);
return 0;
}
if (f->handler)
(*f->handler)((f->maxarg > 0) ? argv[2] : 0,
(f->maxarg > 1) ? argv[3] : 0);
return(1);
}
static int
flush_line(char *line)
{
char *argv[] = { PKILL, "-9", "-t", line, '\0' };
cmdargs(PKILL, argv);
return (1);
}
static int
flush_help(void)
{
Menu *f;
u_int z = 0;
printf("%% Commands may be abbreviated.\n");
printf("%% 'flush' commands are:\n\n");
for (f = flushlist; f->name; f++) {
if (strlen(f->name) > z)
z = strlen(f->name);
}
for (f = flushlist; f->name; f++) {
if (f->help)
printf(" %-*s %s\n", z, f->name, f->help);
}
return 0;
}
/*
* Data structures and routines for the interface configuration mode
*/
struct intlist Intlist[] = {
/* Interface mode commands */
{ "ip", "IP address and other parameters", CMPL0 0, 0, intip },
{ "alias", "Additional IP addresses and other parameters", CMPL0 0, 0, intip },
{ "description", "Interface description", CMPL0 0, 0, intdesc },
{ "group", "Interface group", CMPL0 0, 0, intgroup },
{ "rdomain", "Interface routing domain", CMPL0 0, 0, intrdomain },
{ "rtlabel", "Interface route labels", CMPL0 0, 0, intrtlabel },
{ "priority", "Interface priority", CMPL0 0, 0, intmetric },
{ "mtu", "Set Maximum Transmission Unit", CMPL0 0, 0, intmtu },
{ "metric", "Set routing metric", CMPL0 0, 0, intmetric },
{ "link", "Set link level options", CMPL0 0, 0, intlink },
{ "arp", "Set Address Resolution Protocol", CMPL0 0, 0, intflags },
{ "label", "Set MPLS Label", CMPL0 0, 0, intlabel },
{ "lladdr", "Set Link Level (MAC) Address", CMPL0 0, 0, intlladdr },
{ "nwid", "802.11 network ID", CMPL0 0, 0, intnwid },
{ "nwkey", "802.11 network key", CMPL0 0, 0, intnwkey },
{ "powersave", "802.11 powersaving mode", CMPL0 0, 0, intpowersave },
{ "txpower", "802.11 transmit power", CMPL0 0, 0, inttxpower },
{ "bssid", "802.11 bss id", CMPL0 0, 0, intbssid },
{ "media", "Media type", CMPL0 0, 0, intmedia },
{ "mediaopt", "Media options", CMPL0 0, 0, intmediaopt },
{ "auth", "PPP authentication", CMPL0 0, 0, intsppp },
{ "peer", "PPP peer authentication", CMPL0 0, 0, intsppp },
{ "pppoe", "PPPoE settings", CMPL0 0, 0, intpppoe },
#ifdef INET6
{ "vltime", "IPv6 valid lifetime", CMPL0 0, 0, intvltime },
{ "pltime", "IPv6 preferred lifetime", CMPL0 0, 0, intpltime },
{ "anycast", "IPv6 anycast address bit", CMPL0 0, 0, intanycast },
{ "tentative", "IPv6 tentative address bit", CMPL0 0, 0, inttentative },
{ "eui64", "IPv6 automatic interface index", CMPL0 0, 0, inteui64 },
#endif
{ "tunnel", "Source/destination for GIF/GRE tunnel",CMPL0 0, 0, inttunnel },
{ "keepalive", "GRE tunnel keepalive", CMPL0 0, 0, intkeepalive },
{ "syncdev", "PFsync control message interface", CMPL(i) 0, 0, intsyncdev },
{ "syncpeer", "PFsync peer address", CMPL0 0, 0, intsyncpeer },
{ "maxupd", "PFsync max updates, defer first packet", CMPL0 0, 0, intmaxupd },
{ "vhid", "CARP virtual host ID", CMPL0 0, 0, intcarp },
{ "advbase", "CARP advertisement interval", CMPL0 0, 0, intcarp },
{ "advskew", "CARP advertisement skew", CMPL0 0, 0, intcarp },
{ "carppass", "CARP passphrase", CMPL0 0, 0, intcpass },
{ "carpdev", "CARP device", CMPL(i) 0, 0, intcdev },
{ "carpnode", "CARP additional vhid/advskew", CMPL0 0, 0, intcnode },
{ "carppeer", "CARP peer", CMPL0 0, 0, intcarp },
{ "balancing", "CARP balancing mode", CMPL0 0, 0, intcarp },
{ "pflow", "pflow data export", CMPL0 0, 0, intpflow },
{ "vlan", "802.1Q vlan tag and parent", CMPL0 0, 0, intvlan },
{ "timeslots", "TDM timeslots", CMPL0 0, 0, inttimeslot },
{ "debug", "Driver dependent debugging", CMPL0 0, 0, intflags },
{ "dhcrelay", "DHCP Relay Agent", CMPL0 0, 0, intdhcrelay },
{ "wol", "Wake On LAN", CMPL0 0, 0, intxflags },
{ "mpls", "MPLS", CMPL0 0, 0, intxflags },
{ "inet6", "IPv6", CMPL0 0, 0, intxflags },
{ "rtsol", "IPv6 router solicitation request", CMPL0 0, 0, intrtd },
{ "rtadvd", "IPv6 router advertisement service", CMPL0 0, 0, intrtd },
{ "autoconfprivacy", "IPv6 Autoconfigurable address", CMPL0 0, 0, intxflags },
{ "trunkport", "Add child interface(s) to trunk", CMPL0 0, 0, inttrunkport },
{ "trunkproto", "Define trunkproto", CMPL0 0, 0, inttrunkproto },
{ "shutdown", "Shutdown interface", CMPL0 0, 0, intflags },
{ "?", "Options", CMPL0 0, 0, int_help },
{ "help", 0, CMPL0 0, 0, int_help },
{ 0, 0, 0, 0, 0 }
};
struct intlist Bridgelist[] = {
/* Bridge mode commands */
{ "description", "Bridge description", CMPL0 0, 0, intdesc },
{ "member", "Bridge member(s)", CMPL(i) 0, 0, brport },
{ "span", "Bridge spanning port(s)", CMPL(i) 0, 0, brport },
{ "blocknonip", "Block non-IP traffic forwarding on member(s)", CMPL0 0, 0, brport },
{ "discover", "Mark member(s) as discovery port(s)", CMPL0 0, 0, brport },
{ "learning", "Mark member(s) as learning port(s)", CMPL0 0, 0, brport },
{ "stp", "Enable 802.1D spanning tree protocol on member(s)", CMPL0 0, 0, brport },
{ "maxaddr", "Maximum address cache size", CMPL0 0, 0, brval },
{ "timeout", "Address cache timeout", CMPL0 0, 0, brval },
{ "maxage", "Time for 802.1D configuration to remain valid", CMPL0 0, 0, brval },
{ "fwddelay", "Time before bridge begins forwarding packets", CMPL0 0, 0, brval },
{ "hellotime", "802.1D configuration packet broadcast interval", CMPL0 0, 0, brval },
{ "priority", "Spanning priority for all members on an 802.1D bridge",CMPL0 0, 0, brval },
{ "rule", "Bridge layer 2 filtering rules", CMPL0 0, 0, brrule },
{ "static", "Static bridge address entry", CMPL0 0, 0, brstatic },
{ "ifpriority", "Spanning priority of a member on an 802.1D bridge", CMPL0 0, 0, brpri },
{ "ifcost", "Spanning tree path cost of a member on 802.1D bridge", CMPL0 0, 0, brpri },
{ "link", "Set link level options", CMPL0 0, 0, intlink },
{ "shutdown", "Shutdown bridge", CMPL0 0, 0, intflags },
/* Help commands */
{ "?", "Options", CMPL0 0, 0, int_help },
{ "help", 0, CMPL0 0, 0, int_help },
{ 0, 0, 0, 0, 0, 0 }
};
/*
* command handler for interface and bridge modes
*
* acts as a loop for human keyboard user, and as a one time command
* lookup for rcfile -c or -i usage
*
* if a function returns to interface() with a 1, interface() will break
* the user back to command() mode.
*/
static int
interface(int argc, char **argv, char *modhvar)
{
u_int num;
int ifs, set = 1;
char *tmp;
struct intlist *i; /* pointer to current command */
struct ifreq ifr;
if (!modhvar) {
if (NO_ARG(argv[0])) {
argv++;
argc--;
set = 0;
}
if (argc != 2) {
printf("%% [no] interface <interface name>\n");
return(0);
}
tmp = argv[1];
} else {
/* called from cmdrc(), processing config file rules only */
if (argc == 2 && strcmp(modhvar, argv[1]) == 0) {
/* do-nothing */
return(0);
}
tmp = modhvar;
}
if (strlen(tmp) > IFNAMSIZ-1) {
printf("%% interface name too long\n");
return(0);
}
ifname[IFNAMSIZ-1] = '\0';
strlcpy(ifname, tmp, IFNAMSIZ);
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
ifs = socket(AF_INET, SOCK_DGRAM, 0);
if (ifs < 0) {
printf("%% socket failed: %s\n", strerror(errno));
return(1);
}
if (!is_valid_ifname(ifname)) {
if (set == 0) {
printf("%% interface %s not found\n", ifname);
close(ifs);
return(0);
}
if (ioctl(ifs, SIOCIFCREATE, &ifr) == -1) {
if (errno == EINVAL)
printf("%% interface %s not found\n", ifname);
else
printf("%% unable to create interface %s: %s\n",
ifname, strerror(errno));
close(ifs);
return(0);
}
}
if (set == 0) {
if (ioctl(ifs, SIOCIFDESTROY, &ifr) == -1) {
printf("%% unable to remove interface %s: %s\n",
ifname, strerror(errno));
} else {
/* remove interface routes? */
}
close(ifs);
return(0);
}
if (is_bridge(ifs, ifname)) {
/* whichlist also used by help, command completion code */
whichlist = Bridgelist;
bridge = 1;
} else {
whichlist = Intlist;
bridge = 0;
}
if (modhvar) {
/* direct rcfile -i or -c initialization */
char *argp;
if (argc - 1 > NARGS)
argc = NARGS;
if (argv[0] == 0)
return(0);
if (NO_ARG(argv[0]))
argp = argv[1];
else
argp = argv[0];
i = (struct intlist *) genget(argp, (char **)
whichlist, sizeof(struct intlist));
if (Ambiguous(i)) {
printf("%% Ambiguous command\n");
} else if (i == 0) {
printf("%% Invalid command\n");
} else {
int save_cli_rtable = cli_rtable;
cli_rtable = 0;
((*i->handler) (ifname, ifs, argc, argv));
cli_rtable = save_cli_rtable;
}
return(0);
}
/* human at the keyboard */
for (;;) {
char *margp;
if (!editing) {
/* command line editing disabled */
printf("%s", iprompt());
if (fgets(line, sizeof(line), stdin) == NULL) {
if (feof(stdin) || ferror(stdin)) {
printf("\n");
close(ifs);
return(0);
}
break;
}
} else {
const char *buf;
cursor_pos = NULL;
if ((buf = el_gets(eli, &num)) == NULL || num == 0)
break;
if (buf[--num] == '\n') {
if (num == 0)
break;
}
if (num >= sizeof(line)) {
printf("%% Input exceeds permitted length\n");
break;
}
memcpy(line, buf, (size_t)num);
line[num] = '\0';
history(histi, &ev, H_ENTER, buf);
}
if (line[0] == 0)
break;
makeargv();
if (margv[0] == 0)
break;
if (NO_ARG(margv[0]))
margp = margv[1];
else
margp = margv[0];
i = (struct intlist *) genget(margp, (char **)
whichlist, sizeof(struct intlist));
if (Ambiguous(i)) {
printf("%% Ambiguous command\n");
} else if (i == 0) {
int val = 1;
if (editing)
val = el_burrito(eli, margc, margv);
if (val)
printf("%% Invalid command\n");
} else {
int save_cli_rtable = cli_rtable;
cli_rtable = 0;
if ((*i->handler) (ifname, ifs, margc, margv)) {
cli_rtable = save_cli_rtable;
break;
}
cli_rtable = save_cli_rtable;
}
}
close(ifs);
return(0);
}
static int
int_help(void)
{
struct intlist *i; /* pointer to current command */
u_int z = 0;
printf("%% Commands may be abbreviated.\n");
printf("%% Press enter at a prompt to leave %s configuration mode.\n",
bridge ? "bridge" : "interface");
printf("%% %s configuration commands are:\n\n",
bridge ? "Bridge" : "Interface");
for (i = whichlist; i->name; i++) {
if (strlen(i->name) > z)
z = strlen(i->name);
}
for (i = whichlist; i->name; i++) {
if (i->help)
printf(" %-*s %s\n", z, i->name, i->help);
}
return 0;
}
/*
* Data structures and routines for the main CLI
*/
static char
hostnamehelp[] = "Set system hostname",
interfacehelp[] = "Modify interface parameters",
rtablehelp[] = "Routing table switch",
grouphelp[] = "Modify group attributes",
arphelp[] = "Static ARP set",
#ifdef notyet
parphelp[] = "Proxy ARP set",
#endif
pfhelp[] = "Packet filter control",
ospfhelp[] = "OSPF control",
ospf6help[] = "OSPF6 control",
bgphelp[] = "BGP control",
riphelp[] = "RIP control",
ldphelp[] = "LDP control",
relayhelp[] = "Relay control",
ipsechelp[] = "IPsec IKEv1 control",
ikehelp[] = "IPsec IKEv2 control",
rtadvhelp[] = "Router advertisement control",
dvmrphelp[] = "DVMRP control",
sasynchelp[] = "SA synchronization control",
dhcphelp[] = "DHCP server control",
snmphelp[] = "SNMP server control",
smtphelp[] = "SMTP server control",
ldaphelp[] = "LDAP server control",
sshdhelp[] = "SSH server control",
ntphelp[] = "NTP synchronization control",
nppphelp[] = "PPP server control",
ifstatehelp[] = "ifstate server control",
ftpproxyhelp[] ="ftp-proxy server control",
tftpproxyhelp[] ="tftp-proxy server control",
tftphelp[] = "TFTP server control",
dnshelp[] = "DNS rule control",
inethelp[] = "Inet super-server control",
bridgehelp[] = "Modify bridge parameters",
showhelp[] = "Show system information",
iphelp[] = "Set IP networking parameters",
ip6help[] = "Set IPv6 networking parameters",
mplshelp[] = "Set MPLS network parameters",
ddbhelp[] = "Set DDB parameters",
pipexhelp[] = "Set PIPEX parameters",
flushhelp[] = "Flush system tables",
enablehelp[] = "Enable privileged mode",
disablehelp[] = "Disable privileged mode",
routehelp[] = "Add a host or network route",
pinghelp[] = "Send IPv4 ICMP echo request",
ping6help[] = "Send IPv6 ICMP echo request",
tracerthelp[] = "Print the route to IPv4 host",
tracert6help[] ="Print the route to IPv6 host",
sshhelp[] = "SSH connection to remote host",
telnethelp[] = "Telnet connection to remote host",
quithelp[] = "Close current connection",
verbosehelp[] = "Set verbose diagnostics",
editinghelp[] = "Set command line editing",
whohelp[] = "Display system users",
shellhelp[] = "Invoke a subshell",
savehelp[] = "Save the current configuration",
nreboothelp[] = "Reboot the system",
halthelp[] = "Halt the system",
helphelp[] = "Print help information";
/*
* Primary commands, will be included in help output
*/
#define ssctl sizeof(struct ctl)
Command cmdtab[] = {
{ "hostname", hostnamehelp, CMPL0 0, 0, hostname, 1, 0, 0 },
{ "interface", interfacehelp, CMPL(i) 0, 0, interface, 1, 1, 1 },
{ "rtable", rtablehelp, CMPL0 0, 0, rtable, 0, 1, 2 },
{ "group", grouphelp, CMPL0 0, 0, group, 1, 1, 0 },
{ "arp", arphelp, CMPL0 0, 0, arpset, 1, 1, 0 },
#ifdef notyet
{ "proxy-arp", parphelp, CMPL0 0, 0, arpset, 1, 1, 0 },
#endif
{ "bridge", bridgehelp, CMPL(i) 0, 0, interface, 1, 0, 1 },
{ "show", showhelp, CMPL(ta) (char **)showlist, sizeof(Menu), showcmd, 0, 0, 0 },
{ "ip", iphelp, CMPL(ta) (char **)iptab, sizeof(Menu), ipcmd, 1, 1, 0 },
{ "ip6", ip6help, CMPL(ta) (char **)ip6tab, sizeof(Menu), ipcmd, 1, 1, 0 },
{ "mpls", mplshelp, CMPL(ta) (char **)mplstab, sizeof(Menu), ipcmd, 1, 1, 0 },
{ "ddb", ddbhelp, CMPL(ta) (char **)ddbtab, sizeof(Menu), ipcmd, 1, 1, 0 },
{ "pipex", pipexhelp, CMPL(ta) (char **)pipextab, sizeof(Menu), ipcmd, 1, 1, 0 },
{ "flush", flushhelp, CMPL(ta) (char **)flushlist, sizeof(Menu), flushcmd, 1, 0, 0 },
{ "enable", enablehelp, CMPL0 0, 0, enable, 0, 0, 0 },
{ "disable", disablehelp, CMPL0 0, 0, disable, 1, 0, 0 },
{ "route", routehelp, CMPL0 0, 0, route, 1, 1, 0 },
{ "pf", pfhelp, CMPL(t) (char **)ctl_pf, ssctl, ctlhandler, 1, 0, 1 },
{ "ospf", ospfhelp, CMPL(t) (char **)ctl_ospf, ssctl, ctlhandler, 1, 0, 1 },
{ "ospf6", ospf6help, CMPL(t) (char **)ctl_ospf6, ssctl, ctlhandler, 1, 0, 1 },
{ "bgp", bgphelp, CMPL(t) (char **)ctl_bgp, ssctl, ctlhandler, 1, 0, 1 },
{ "rip", riphelp, CMPL(t) (char **)ctl_rip, ssctl, ctlhandler, 1, 0, 1 },
{ "ldp", ldphelp, CMPL(t) (char **)ctl_ldp, ssctl, ctlhandler, 1, 0, 1 },
{ "relay", relayhelp, CMPL(t) (char **)ctl_relay, ssctl, ctlhandler, 1, 0, 1 },
{ "ipsec", ipsechelp, CMPL(t) (char **)ctl_ipsec, ssctl, ctlhandler, 1, 0, 1 },
{ "ike", ikehelp, CMPL(t) (char **)ctl_ike, ssctl, ctlhandler, 1, 0, 1 },
{ "dvmrp", dvmrphelp, CMPL(t) (char **)ctl_dvmrp, ssctl, ctlhandler, 1, 0, 1 },
{ "rtadv", rtadvhelp, CMPL(t) (char **)ctl_rtadv, ssctl, ctlhandler, 1, 0, 1 },
{ "sasync", sasynchelp, CMPL(t) (char **)ctl_sasync, ssctl, ctlhandler, 1, 0, 1 },
{ "dhcp", dhcphelp, CMPL(t) (char **)ctl_dhcp, ssctl, ctlhandler, 1, 0, 1 },
{ "snmp", snmphelp, CMPL(t) (char **)ctl_snmp, ssctl, ctlhandler, 1, 0, 1 },
{ "ldap", ldaphelp, CMPL(t) (char **)ctl_ldap, ssctl, ctlhandler, 1, 0, 1 },
{ "smtp", smtphelp, CMPL(t) (char **)ctl_smtp, ssctl, ctlhandler, 1, 0, 1 },
{ "sshd", sshdhelp, CMPL(t) (char **)ctl_sshd, ssctl, ctlhandler, 1, 0, 1 },
{ "ntp", ntphelp, CMPL(t) (char **)ctl_ntp, ssctl, ctlhandler, 1, 0, 1 },
{ "nppp", nppphelp, CMPL(t) (char **)ctl_nppp, ssctl, ctlhandler, 1, 0, 1 },
{ "ifstate", ifstatehelp, CMPL(t) (char **)ctl_ifstate, ssctl, ctlhandler, 1, 0, 1 },
{ "ftp-proxy", ftpproxyhelp, CMPL(t) (char **)ctl_ftpproxy, ssctl, ctlhandler, 1, 0, 1 },
{ "tftp-proxy", tftpproxyhelp, CMPL(t) (char **)ctl_tftpproxy, ssctl, ctlhandler, 1, 0, 1 },
{ "tftp", tftphelp, CMPL(t) (char **)ctl_tftp, ssctl, ctlhandler, 1, 0, 1 },
{ "dns", dnshelp, CMPL(t) (char **)ctl_dns, ssctl, ctlhandler, 1, 0, 1 },
{ "inet", inethelp, CMPL(t) (char **)ctl_inet, ssctl, ctlhandler, 1, 0, 1 },
{ "ping", pinghelp, CMPL0 0, 0, ping, 0, 0, 0 },
{ "ping6", ping6help, CMPL0 0, 0, ping6, 0, 0, 0 },
{ "traceroute", tracerthelp, CMPL0 0, 0, traceroute, 0, 0, 0 },
{ "traceroute6", tracert6help, CMPL0 0, 0, traceroute6, 0, 0, 0 },
{ "ssh", sshhelp, CMPL0 0, 0, ssh, 0, 0, 0 },
{ "telnet", telnethelp, CMPL0 0, 0, telnet, 0, 0, 0 },
{ "reboot", nreboothelp, CMPL0 0, 0, nreboot, 1, 0, 0 },
{ "halt", halthelp, CMPL0 0, 0, halt, 1, 0, 0 },
{ "write-config", savehelp, CMPL0 0, 0, wr_startup, 1, 0, 0 },
{ "verbose", verbosehelp, CMPL0 0, 0, doverbose, 0, 1, 0 },
{ "editing", editinghelp, CMPL0 0, 0, doediting, 0, 1, 0 },
{ "who", whohelp, CMPL0 0, 0, who, 0, 0, 0 },
{ "!", shellhelp, CMPL0 0, 0, shell, 1, 0, 0 },
{ "?", helphelp, CMPL(C) 0, 0, help, 0, 0, 0 },
{ "quit", quithelp, CMPL0 0, 0, quit, 0, 0, 0 },
{ "help", 0, CMPL(C) 0, 0, help, 0, 0, 0 },
{ 0, 0, CMPL0 0, 0, 0, 0, 0, 0 }
};
/*
* These commands escape ambiguous check and help listings
*/
static Command cmdtab2[] = {
{ "config", 0, CMPL0 0, 0, notvalid, 0, 0, 0 },
{ 0, 0, CMPL0 0, 0, 0, 0, 0, 0 }
};
Command *
getcmd(char *name)
{
Command *cm;
if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
return cm;
return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
}
void
makeargv()
{
char *cp, *cp2, *base, c;
char **argp = margv;
margc = 0;
cp = line;
if (*cp == '!') { /* Special case shell escape */
/* save for shell command */
strlcpy(saveline, line, sizeof(saveline));
*argp++ = "!"; /* 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;
cursor_argc = margc += 1;
base = cp;
for (cursor_argo = 0, cp2 = cp; c != '\0';
cursor_argo = (cp + 1) - base, 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)) {
cursor_argo = 0;
break;
}
}
*cp2++ = c;
}
*cp2 = '\0';
if (c == '\0') {
cursor_argc--;
break;
}
cp++;
}
*argp++ = 0;