forked from dparrish/libcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libcli.c
3633 lines (3098 loc) · 116 KB
/
libcli.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
// vim:sw=2 tw=120 et
#ifdef WIN32
#include <windows.h>
#include <winsock2.h>
#endif
#define _GNU_SOURCE
#include <errno.h>
#include <memory.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#if !defined(__APPLE__) && !defined(__FreeBSD__)
#include <malloc.h>
#endif
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifndef WIN32
#include <regex.h>
#endif
#if defined(LIBCLI_USE_POLL) && !defined(WIN32)
#include <poll.h>
#define CLI_SOCKET_WAIT_PERROR "poll"
#else
#define CLI_SOCKET_WAIT_PERROR "select"
#endif
#include "libcli.h"
#ifdef __GNUC__
#define UNUSED(d) d __attribute__((unused))
#else
#define UNUSED(d) d
#endif
#define MATCH_REGEX 1
#define MATCH_INVERT 2
#ifdef WIN32
// Stupid windows has multiple namespaces for filedescriptors, with different read/write functions required for each ..
int read(int fd, void *buf, unsigned int count) {
return recv(fd, buf, count, 0);
}
int write(int fd, const void *buf, unsigned int count) {
return send(fd, buf, count, 0);
}
int vasprintf(char **strp, const char *fmt, va_list args) {
int size;
va_list argCopy;
// Do initial vsnprintf on a copy of the va_list
va_copy(argCopy, args);
size = vsnprintf(NULL, 0, fmt, argCopy);
va_end(argCopy);
if ((*strp = malloc(size + 1)) == NULL) {
return -1;
}
size = vsnprintf(*strp, size + 1, fmt, args);
return size;
}
int asprintf(char **strp, const char *fmt, ...) {
va_list args;
int size;
va_start(args, fmt);
size = vasprintf(strp, fmt, args);
va_end(args);
return size;
}
int fprintf(FILE *stream, const char *fmt, ...) {
va_list args;
int size;
char *buf;
va_start(args, fmt);
size = vasprintf(&buf, fmt, args);
if (size < 0) {
goto out;
}
size = write(stream->_file, buf, size);
free(buf);
out:
va_end(args);
return size;
}
// Dummy definitions to allow compilation on Windows
int regex_dummy() {
return 0;
};
#define regfree(...) regex_dummy()
#define regexec(...) regex_dummy()
#define regcomp(...) regex_dummy()
#define regex_t int
#define REG_NOSUB 0
#define REG_EXTENDED 0
#define REG_ICASE 0
#endif
enum cli_states {
STATE_LOGIN,
STATE_PASSWORD,
STATE_NORMAL,
STATE_ENABLE_PASSWORD,
STATE_ENABLE,
};
struct unp {
char *username;
char *password;
struct unp *next;
};
struct cli_filter_cmds {
const char *cmd;
const char *help;
};
// Free and zero (to avoid double-free)
#define free_z(p) \
do { \
if (p) { \
free(p); \
(p) = 0; \
} \
} while (0)
// Forward defines of *INTERNAL* library function as static here
static int cli_search_flags_validator(struct cli_def *cli, const char *word, const char *value);
static int cli_match_filter_init(struct cli_def *cli, int argc, char **argv, struct cli_filter *filt);
static int cli_range_filter_init(struct cli_def *cli, int argc, char **argv, struct cli_filter *filt);
static int cli_count_filter_init(struct cli_def *cli, int argc, char **argv, struct cli_filter *filt);
static int cli_match_filter(struct cli_def *cli, const char *string, void *data);
static int cli_range_filter(struct cli_def *cli, const char *string, void *data);
static int cli_count_filter(struct cli_def *cli, const char *string, void *data);
static void cli_int_parse_optargs(struct cli_def *cli, struct cli_pipeline_stage *stage, struct cli_command *cmd,
char lastchar, struct cli_comphelp *comphelp);
static int cli_int_enter_buildmode(struct cli_def *cli, struct cli_pipeline_stage *stage, char *mode_text);
static char *cli_int_buildmode_extend_cmdline(char *, char *word);
static void cli_int_free_buildmode(struct cli_def *cli);
static void cli_free_command(struct cli_def *cli, struct cli_command *cmd);
static int cli_int_unregister_command_core(struct cli_def *cli, const char *command, int command_type);
static int cli_int_unregister_buildmode_command(struct cli_def *cli, const char *command) __attribute__((unused));
static struct cli_command *cli_int_register_buildmode_command(struct cli_def *cli, struct cli_command *parent,
const char *command,
int (*callback)(struct cli_def *cli, const char *,
char **, int),
int flags, int privilege, int mode, const char *help);
static void cli_int_buildmode_reset_unset_help(struct cli_def *cli);
static int cli_int_buildmode_cmd_cback(struct cli_def *cli, const char *command, char *argv[], int argc);
static int cli_int_buildmode_flag_cback(struct cli_def *cli, const char *command, char *argv[], int argc);
static int cli_int_buildmode_flag_multiple_cback(struct cli_def *cli, const char *command, char *argv[], int argc);
static int cli_int_buildmode_cancel_cback(struct cli_def *cli, const char *command, char *argv[], int argc);
static int cli_int_buildmode_execute_cback(struct cli_def *cli, const char *command, char *argv[], int argc);
static int cli_int_buildmode_show_cback(struct cli_def *cli, const char *command, char *argv[], int argc);
static int cli_int_buildmode_unset_cback(struct cli_def *cli, const char *command, char *argv[], int argc);
static int cli_int_buildmode_unset_completor(struct cli_def *cli, const char *name, const char *word,
struct cli_comphelp *comphelp);
static int cli_int_buildmode_unset_validator(struct cli_def *cli, const char *name, const char *value);
static int cli_int_execute_buildmode(struct cli_def *cli);
static void cli_int_free_found_optargs(struct cli_optarg_pair **optarg_pair);
static void cli_int_unset_optarg_value(struct cli_def *cli, const char *name);
static struct cli_pipeline *cli_int_generate_pipeline(struct cli_def *cli, const char *command);
static int cli_int_validate_pipeline(struct cli_def *cli, struct cli_pipeline *pipeline);
static int cli_int_execute_pipeline(struct cli_def *cli, struct cli_pipeline *pipeline);
inline void cli_int_show_pipeline(struct cli_def *cli, struct cli_pipeline *pipeline);
static void cli_int_free_pipeline(struct cli_pipeline *pipeline);
static struct cli_command *cli_register_command_core(struct cli_def *cli, struct cli_command *parent,
struct cli_command *c);
static void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *comphelp);
static int cli_socket_wait(int sockfd, struct timeval *tm);
static char DELIM_OPT_START[] = "[";
static char DELIM_OPT_END[] = "]";
static char DELIM_ARG_START[] = "<";
static char DELIM_ARG_END[] = ">";
static char DELIM_NONE[] = "";
static ssize_t _write(int fd, const void *buf, size_t count) {
size_t written = 0;
ssize_t thisTime = 0;
while (count != written) {
thisTime = write(fd, (char *)buf + written, count - written);
if (thisTime == -1) {
if (errno == EINTR)
continue;
else
return -1;
}
written += thisTime;
}
return written;
}
char *cli_int_command_name(struct cli_def *cli, struct cli_command *command) {
char *name;
char *o;
if (command->full_command_name) {
free(command->full_command_name);
command->full_command_name = NULL;
}
if (!(name = calloc(1, 1))) return NULL;
while (command) {
o = name;
if (asprintf(&name, "%s%s%s", command->command, *o ? " " : "", o) == -1) {
fprintf(stderr, "Couldn't allocate memory for command_name: %s", strerror(errno));
free(o);
return NULL;
}
command = command->parent;
free(o);
}
return name;
}
char *cli_command_name(struct cli_def *cli, struct cli_command *command) {
return command->full_command_name;
}
void cli_set_auth_callback(struct cli_def *cli, int (*auth_callback)(const char *, const char *)) {
cli->auth_callback = auth_callback;
}
void cli_set_enable_callback(struct cli_def *cli, int (*enable_callback)(const char *)) {
cli->enable_callback = enable_callback;
}
void cli_allow_user(struct cli_def *cli, const char *username, const char *password) {
struct unp *u, *n;
if (!(n = malloc(sizeof(struct unp)))) {
fprintf(stderr, "Couldn't allocate memory for user: %s", strerror(errno));
return;
}
if (!(n->username = strdup(username))) {
fprintf(stderr, "Couldn't allocate memory for username: %s", strerror(errno));
free(n);
return;
}
if (!(n->password = strdup(password))) {
fprintf(stderr, "Couldn't allocate memory for password: %s", strerror(errno));
free(n->username);
free(n);
return;
}
n->next = NULL;
if (!cli->users) {
cli->users = n;
} else {
for (u = cli->users; u && u->next; u = u->next)
;
if (u) u->next = n;
}
}
void cli_allow_enable(struct cli_def *cli, const char *password) {
free_z(cli->enable_password);
if (!(cli->enable_password = strdup(password))) {
fprintf(stderr, "Couldn't allocate memory for enable password: %s", strerror(errno));
}
}
void cli_deny_user(struct cli_def *cli, const char *username) {
struct unp *u, *p = NULL;
if (!cli->users) return;
for (u = cli->users; u; u = u->next) {
if (strcmp(username, u->username) == 0) {
if (p)
p->next = u->next;
else
cli->users = u->next;
free(u->username);
free(u->password);
free(u);
break;
}
p = u;
}
}
void cli_set_banner(struct cli_def *cli, const char *banner) {
free_z(cli->banner);
if (banner && *banner) cli->banner = strdup(banner);
}
void cli_set_hostname(struct cli_def *cli, const char *hostname) {
free_z(cli->hostname);
if (hostname && *hostname) cli->hostname = strdup(hostname);
}
void cli_set_promptchar(struct cli_def *cli, const char *promptchar) {
free_z(cli->promptchar);
cli->promptchar = strdup(promptchar);
}
static int cli_build_shortest(struct cli_def *cli, struct cli_command *commands) {
struct cli_command *c, *p;
char *cp, *pp;
unsigned len;
for (c = commands; c; c = c->next) {
c->unique_len = strlen(c->command);
if ((c->mode != MODE_ANY && c->mode != cli->mode) || c->privilege > cli->privilege) continue;
c->unique_len = 1;
for (p = commands; p; p = p->next) {
if (c == p) continue;
if (c->command_type != p->command_type) continue;
if ((p->mode != MODE_ANY && p->mode != cli->mode) || p->privilege > cli->privilege) continue;
cp = c->command;
pp = p->command;
len = 1;
while (*cp && *pp && *cp++ == *pp++) len++;
if (len > c->unique_len) c->unique_len = len;
}
if (c->children) cli_build_shortest(cli, c->children);
}
return CLI_OK;
}
int cli_set_privilege(struct cli_def *cli, int priv) {
int old = cli->privilege;
cli->privilege = priv;
if (priv != old) {
cli_set_promptchar(cli, priv == PRIVILEGE_PRIVILEGED ? "# " : "> ");
cli_build_shortest(cli, cli->commands);
}
return old;
}
void cli_set_modestring(struct cli_def *cli, const char *modestring) {
free_z(cli->modestring);
if (modestring) cli->modestring = strdup(modestring);
}
int cli_set_configmode(struct cli_def *cli, int mode, const char *config_desc) {
int old = cli->mode;
cli->mode = mode;
if (mode != old) {
if (!cli->mode) {
// Not config mode
cli_set_modestring(cli, NULL);
} else if (config_desc && *config_desc) {
char string[64];
snprintf(string, sizeof(string), "(config-%s)", config_desc);
cli_set_modestring(cli, string);
} else {
cli_set_modestring(cli, "(config)");
}
cli_build_shortest(cli, cli->commands);
}
return old;
}
struct cli_command *cli_register_command_core(struct cli_def *cli, struct cli_command *parent, struct cli_command *c) {
struct cli_command *p = NULL;
if (!c) return NULL;
c->parent = parent;
/* Go build the 'full command name' now that told it who its parent is.
* If this fails, clean it up and return a NULL w/o proceeding.
*/
if (!(c->full_command_name = cli_int_command_name(cli, c))) {
cli_free_command(cli, c);
return NULL;
}
/*
* Figure out we have a chain, or would be the first element on it.
* If we'd be the first element, assign as such.
* Otherwise find the lead element so we can trace it below.
*/
if (parent) {
if (!parent->children) {
parent->children = c;
} else {
p = parent->children;
}
} else {
if (!cli->commands) {
cli->commands = c;
} else {
p = cli->commands;
}
}
/*
* If we have a chain (p is not null), run down to the last element and place this command at the end
*/
for (; p && p->next; p = p->next)
;
if (p) {
p->next = c;
c->previous = p;
}
return c;
}
struct cli_command *cli_register_command(struct cli_def *cli, struct cli_command *parent, const char *command,
int (*callback)(struct cli_def *cli, const char *, char **, int),
int privilege, int mode, const char *help) {
struct cli_command *c;
if (!command) return NULL;
if (!(c = calloc(sizeof(struct cli_command), 1))) return NULL;
c->command_type = CLI_REGULAR_COMMAND;
c->callback = callback;
c->next = NULL;
if (!(c->command = strdup(command))) {
free(c);
return NULL;
}
c->privilege = privilege;
c->mode = mode;
if (help && !(c->help = strdup(help))) {
free(c->command);
free(c);
return NULL;
}
return cli_register_command_core(cli, parent, c);
}
static void cli_free_command(struct cli_def *cli, struct cli_command *cmd) {
struct cli_command *c, *p;
for (c = cmd->children; c;) {
p = c->next;
cli_free_command(cli, c);
c = p;
}
free(cmd->command);
if (cmd->help) free(cmd->help);
if (cmd->optargs) cli_unregister_all_optarg(cmd);
if (cmd->full_command_name) free(cmd->full_command_name);
/*
* Ok, update the pointers of anyone who pointed to us.
* We have 3 pointers to worry about - parent, previous, and next.
* We don't have to worry about children since they've been cleared above.
* If both cli->command points to us we need to update cli->command to point to whatever command is 'next'.
* Otherwise ensure that any item before/behind us points around us.
*
* Important - there is no provision for deleting a discrete subcommand.
* For example, suppose we define foo, then bar with foo as the parent, then baz with bar as the parent. We cannot
* delete 'bar' and have a new chain of foo -> baz.
* The above freeing of children prevents this in the first place.
*/
if (cmd == cli->commands) {
cli->commands = cmd->next;
if (cmd->next) {
cmd->next->parent = NULL;
cmd->next->previous = NULL;
}
} else {
if (cmd->previous) {
cmd->previous->next = cmd->next;
}
if (cmd->next) {
cmd->next->previous = cmd->previous;
}
}
free(cmd);
}
int cli_int_unregister_command_core(struct cli_def *cli, const char *command, int command_type) {
struct cli_command *c, *p = NULL;
if (!command) return -1;
if (!cli->commands) return CLI_OK;
for (c = cli->commands; c;) {
p = c->next;
if (strcmp(c->command, command) == 0 && c->command_type == command_type) {
cli_free_command(cli, c);
return CLI_OK;
}
c = p;
}
return CLI_OK;
}
int cli_unregister_command(struct cli_def *cli, const char *command) {
return cli_int_unregister_command_core(cli, command, CLI_REGULAR_COMMAND);
}
int cli_show_help(struct cli_def *cli, struct cli_command *c) {
struct cli_command *p;
for (p = c; p; p = p->next) {
if (p->command && p->callback && cli->privilege >= p->privilege && (p->mode == cli->mode || p->mode == MODE_ANY)) {
cli_error(cli, " %-20s %s", cli_command_name(cli, p), (p->help != NULL ? p->help : ""));
}
if (p->children) cli_show_help(cli, p->children);
}
return CLI_OK;
}
int cli_enable(struct cli_def *cli, UNUSED(const char *command), UNUSED(char *argv[]), UNUSED(int argc)) {
if (cli->privilege == PRIVILEGE_PRIVILEGED) return CLI_OK;
if (!cli->enable_password && !cli->enable_callback) {
// No password required, set privilege immediately.
cli_set_privilege(cli, PRIVILEGE_PRIVILEGED);
cli_set_configmode(cli, MODE_EXEC, NULL);
} else {
// Require password entry
cli->state = STATE_ENABLE_PASSWORD;
}
return CLI_OK;
}
int cli_disable(struct cli_def *cli, UNUSED(const char *command), UNUSED(char *argv[]), UNUSED(int argc)) {
cli_set_privilege(cli, PRIVILEGE_UNPRIVILEGED);
cli_set_configmode(cli, MODE_EXEC, NULL);
return CLI_OK;
}
int cli_help(struct cli_def *cli, UNUSED(const char *command), UNUSED(char *argv[]), UNUSED(int argc)) {
cli_error(cli, "\nCommands available:");
cli_show_help(cli, cli->commands);
return CLI_OK;
}
int cli_history(struct cli_def *cli, UNUSED(const char *command), UNUSED(char *argv[]), UNUSED(int argc)) {
int i;
cli_error(cli, "\nCommand history:");
for (i = 0; i < MAX_HISTORY; i++) {
if (cli->history[i]) cli_error(cli, "%3d. %s", i, cli->history[i]);
}
return CLI_OK;
}
int cli_quit(struct cli_def *cli, UNUSED(const char *command), UNUSED(char *argv[]), UNUSED(int argc)) {
cli_set_privilege(cli, PRIVILEGE_UNPRIVILEGED);
cli_set_configmode(cli, MODE_EXEC, NULL);
return CLI_QUIT;
}
int cli_exit(struct cli_def *cli, const char *command, char *argv[], int argc) {
if (cli->mode == MODE_EXEC) return cli_quit(cli, command, argv, argc);
if (cli->mode > MODE_CONFIG)
cli_set_configmode(cli, MODE_CONFIG, NULL);
else
cli_set_configmode(cli, MODE_EXEC, NULL);
cli->service = NULL;
return CLI_OK;
}
int cli_int_idle_timeout(struct cli_def *cli) {
cli_print(cli, "Idle timeout");
return CLI_QUIT;
}
int cli_int_configure_terminal(struct cli_def *cli, UNUSED(const char *command), UNUSED(char *argv[]),
UNUSED(int argc)) {
cli_set_configmode(cli, MODE_CONFIG, NULL);
return CLI_OK;
}
struct cli_def *cli_init() {
struct cli_def *cli;
struct cli_command *c;
if (!(cli = calloc(sizeof(struct cli_def), 1))) return 0;
cli->buf_size = 1024;
if (!(cli->buffer = calloc(cli->buf_size, 1))) {
cli_done(cli);
return 0;
}
cli->telnet_protocol = 1;
cli_register_command(cli, 0, "help", cli_help, PRIVILEGE_UNPRIVILEGED, MODE_ANY, "Show available commands");
cli_register_command(cli, 0, "quit", cli_quit, PRIVILEGE_UNPRIVILEGED, MODE_ANY, "Disconnect");
cli_register_command(cli, 0, "logout", cli_quit, PRIVILEGE_UNPRIVILEGED, MODE_ANY, "Disconnect");
cli_register_command(cli, 0, "exit", cli_exit, PRIVILEGE_UNPRIVILEGED, MODE_ANY, "Exit from current mode");
cli_register_command(cli, 0, "history", cli_history, PRIVILEGE_UNPRIVILEGED, MODE_ANY,
"Show a list of previously run commands");
cli_register_command(cli, 0, "enable", cli_enable, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Turn on privileged commands");
cli_register_command(cli, 0, "disable", cli_disable, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Turn off privileged commands");
c = cli_register_command(cli, 0, "configure", 0, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Enter configuration mode");
if (!c) {
cli_done(cli);
return 0;
}
cli_register_command(cli, c, "terminal", cli_int_configure_terminal, PRIVILEGE_PRIVILEGED, MODE_EXEC,
"Conlfigure from the terminal");
// And now the built in filters
c = cli_register_filter(cli, "begin", cli_range_filter_init, cli_range_filter, PRIVILEGE_UNPRIVILEGED, MODE_ANY,
"Begin with lines that match");
if (!c) {
cli_done(cli);
return 0;
}
cli_register_optarg(c, "range_start", CLI_CMD_ARGUMENT | CLI_CMD_REMAINDER_OF_LINE, PRIVILEGE_UNPRIVILEGED, MODE_ANY,
"Begin showing lines that match", NULL, NULL, NULL);
c = cli_register_filter(cli, "between", cli_range_filter_init, cli_range_filter, PRIVILEGE_UNPRIVILEGED, MODE_ANY,
"Between lines that match");
if (!c) {
cli_done(cli);
return 0;
}
cli_register_optarg(c, "range_start", CLI_CMD_ARGUMENT, PRIVILEGE_UNPRIVILEGED, MODE_ANY,
"Begin showing lines that match", NULL, NULL, NULL);
cli_register_optarg(c, "range_end", CLI_CMD_ARGUMENT | CLI_CMD_REMAINDER_OF_LINE, PRIVILEGE_UNPRIVILEGED, MODE_ANY,
"Stop showing lines that match", NULL, NULL, NULL);
cli_register_filter(cli, "count", cli_count_filter_init, cli_count_filter, PRIVILEGE_UNPRIVILEGED, MODE_ANY,
"Count of lines");
c = cli_register_filter(cli, "exclude", cli_match_filter_init, cli_match_filter, PRIVILEGE_UNPRIVILEGED, MODE_ANY,
"Exclude lines that match");
if (!c) {
cli_done(cli);
return 0;
}
cli_register_optarg(c, "search_pattern", CLI_CMD_ARGUMENT | CLI_CMD_REMAINDER_OF_LINE, PRIVILEGE_UNPRIVILEGED,
MODE_ANY, "Search pattern", NULL, NULL, NULL);
c = cli_register_filter(cli, "include", cli_match_filter_init, cli_match_filter, PRIVILEGE_UNPRIVILEGED, MODE_ANY,
"Include lines that match");
if (!c) {
cli_done(cli);
return 0;
}
cli_register_optarg(c, "search_pattern", CLI_CMD_ARGUMENT | CLI_CMD_REMAINDER_OF_LINE, PRIVILEGE_UNPRIVILEGED,
MODE_ANY, "Search pattern", NULL, NULL, NULL);
c = cli_register_filter(cli, "grep", cli_match_filter_init, cli_match_filter, PRIVILEGE_UNPRIVILEGED, MODE_ANY,
"Include lines that match regex (options: -v, -i, -e)");
if (!c) {
cli_done(cli);
return 0;
}
cli_register_optarg(c, "search_flags", CLI_CMD_HYPHENATED_OPTION, PRIVILEGE_UNPRIVILEGED, MODE_ANY,
"Search flags (-[ivx]", NULL, cli_search_flags_validator, NULL);
cli_register_optarg(c, "search_pattern", CLI_CMD_ARGUMENT | CLI_CMD_REMAINDER_OF_LINE, PRIVILEGE_UNPRIVILEGED,
MODE_ANY, "Search pattern", NULL, NULL, NULL);
c = cli_register_filter(cli, "egrep", cli_match_filter_init, cli_match_filter, PRIVILEGE_UNPRIVILEGED, MODE_ANY,
"Include lines that match extended regex");
if (!c) {
cli_done(cli);
return 0;
}
cli_register_optarg(c, "search_flags", CLI_CMD_HYPHENATED_OPTION, PRIVILEGE_UNPRIVILEGED, MODE_ANY,
"Search flags (-[ivx]", NULL, cli_search_flags_validator, NULL);
cli_register_optarg(c, "search_pattern", CLI_CMD_ARGUMENT | CLI_CMD_REMAINDER_OF_LINE, PRIVILEGE_UNPRIVILEGED,
MODE_ANY, "Search pattern", NULL, NULL, NULL);
cli->privilege = cli->mode = -1;
cli_set_privilege(cli, PRIVILEGE_UNPRIVILEGED);
cli_set_configmode(cli, MODE_EXEC, 0);
// Default to 1 second timeout intervals
cli->timeout_tm.tv_sec = 1;
cli->timeout_tm.tv_usec = 0;
// Set default idle timeout callback, but no timeout
cli_set_idle_timeout_callback(cli, 0, cli_int_idle_timeout);
return cli;
}
void cli_unregister_tree(struct cli_def *cli, struct cli_command *command, int command_type) {
struct cli_command *c, *p = NULL;
if (!command) command = cli->commands;
for (c = command; c;) {
p = c->next;
if (c->command_type == command_type || command_type == CLI_ANY_COMMAND) {
if (c == cli->commands) cli->commands = c->next;
// Unregister all child commands
cli_free_command(cli, c);
}
c = p;
}
}
void cli_unregister_all(struct cli_def *cli, struct cli_command *command) {
cli_unregister_tree(cli, command, CLI_REGULAR_COMMAND);
}
int cli_done(struct cli_def *cli) {
if (!cli) return CLI_OK;
struct unp *u = cli->users, *n;
cli_free_history(cli);
// Free all users
while (u) {
if (u->username) free(u->username);
if (u->password) free(u->password);
n = u->next;
free(u);
u = n;
}
if (cli->buildmode) cli_int_free_buildmode(cli);
cli_unregister_tree(cli, cli->commands, CLI_ANY_COMMAND);
free_z(cli->promptchar);
free_z(cli->modestring);
free_z(cli->banner);
free_z(cli->promptchar);
free_z(cli->hostname);
free_z(cli->buffer);
free_z(cli);
return CLI_OK;
}
static int cli_add_history(struct cli_def *cli, const char *cmd) {
int i;
for (i = 0; i < MAX_HISTORY; i++) {
if (!cli->history[i]) {
if (i == 0 || strcasecmp(cli->history[i - 1], cmd))
if (!(cli->history[i] = strdup(cmd))) return CLI_ERROR;
return CLI_OK;
}
}
// No space found, drop one off the beginning of the list
free(cli->history[0]);
for (i = 0; i < MAX_HISTORY - 1; i++) cli->history[i] = cli->history[i + 1];
if (!(cli->history[MAX_HISTORY - 1] = strdup(cmd))) return CLI_ERROR;
return CLI_OK;
}
void cli_free_history(struct cli_def *cli) {
int i;
for (i = 0; i < MAX_HISTORY; i++) {
if (cli->history[i]) free_z(cli->history[i]);
}
}
static char *cli_int_return_newword(const char *start, const char *end) {
int len = end - start;
char *to = NULL;
char *newword = NULL;
// allocate space (including terminal NULL, then go through and deal with escaping characters as we copy them
if (!(newword = calloc(len + 1, 1))) return 0;
to = newword;
while (start != end) {
if (*start == '\\')
start++;
else
*to++ = *start++;
}
return newword;
}
static int cli_parse_line(const char *line, char *words[], int max_words) {
int nwords = 0;
const char *p = line;
const char *word_start = 0;
int inquote = 0;
while (*p) {
if (!isspace(*p)) {
word_start = p;
break;
}
p++;
}
while (nwords < max_words - 1) {
if (*p == '\\' && *(p + 1)) {
p += 2;
}
/*
* a 'word' terminates at:
* - end-of-string, whitespace (if not inside quotes)
* - start of quoted section (if word_start != NULL)
* - end of a quoted section
* - whitespace/pipe unless inside quotes
*/
if (!*p || *p == inquote || (word_start && !inquote && (isspace(*p) || *p == '|'))) {
// if we have a word start, extract from there to this character dealing with escapes
if (word_start) {
if (!(words[nwords++] = cli_int_return_newword(word_start, p))) return 0;
}
// now figure out how to proceed
// if at end_of_string we're done
if (!*p) break;
// found matching quote, eat it
if (inquote) p++; // Skip over trailing quote if we have one
inquote = 0;
word_start = 0;
} else if (!inquote && (*p == '"' || *p == '\'')) {
if (word_start && word_start != p) {
if (!(words[nwords++] = cli_int_return_newword(word_start, p))) return 0;
}
inquote = *p++;
word_start = p;
} else {
if (!word_start) {
if (*p == '|') {
if (!(words[nwords++] = strdup("|"))) return 0;
} else if (!isspace(*p))
word_start = p;
}
p++;
}
}
return nwords;
}
static char *join_words(int argc, char **argv) {
char *p;
int len = 0;
int i;
for (i = 0; i < argc; i++) {
if (i) len += 1;
len += strlen(argv[i]);
}
p = malloc(len + 1);
if (!p) return NULL;
p[0] = 0;
for (i = 0; i < argc; i++) {
if (i) strcat(p, " ");
strcat(p, argv[i]);
}
return p;
}
int cli_run_command(struct cli_def *cli, const char *command) {
int rc = CLI_ERROR;
struct cli_pipeline *pipeline;
// Split command into pipeline stages
pipeline = cli_int_generate_pipeline(cli, command);
// cli_int_validate_pipeline will deal with buildmode command setup, and return CLI_BUILDMODE_START if found.
if (pipeline) rc = cli_int_validate_pipeline(cli, pipeline);
if (rc == CLI_OK) {
rc = cli_int_execute_pipeline(cli, pipeline);
}
cli_int_free_pipeline(pipeline);
return rc;
}
void cli_get_completions(struct cli_def *cli, const char *command, char lastchar, struct cli_comphelp *comphelp) {
struct cli_command *c = NULL;
struct cli_command *n = NULL;
int i;
int command_type;
struct cli_pipeline *pipeline = NULL;
struct cli_pipeline_stage *stage;
char *delim_start = DELIM_NONE;
char *delim_end = DELIM_NONE;
if (!(pipeline = cli_int_generate_pipeline(cli, command))) goto out;
stage = &pipeline->stage[pipeline->num_stages - 1];
// Check to see if either *no* input, or if the lastchar is a tab.
if ((!stage->words[0] || (command[strlen(command) - 1] == ' ')) && (stage->words[stage->num_words - 1]))
stage->num_words++;
if (cli->buildmode)
command_type = CLI_BUILDMODE_COMMAND;
else if (pipeline->num_stages == 1)
command_type = CLI_REGULAR_COMMAND;
else
command_type = CLI_FILTER_COMMAND;
for (c = cli->commands, i = 0; c && i < stage->num_words; c = n) {
char *strptr = NULL;
char *nameptr = NULL;
n = c->next;
if (c->command_type != command_type) continue;
if (cli->privilege < c->privilege) continue;
if (c->mode != cli->mode && c->mode != MODE_ANY) continue;
if (stage->words[i] && strncasecmp(c->command, stage->words[i], strlen(stage->words[i]))) continue;
// Special case for 'buildmode' - skip if the argument for this command was seen, unless MULTIPLE flag is set
if (cli->buildmode) {
struct cli_optarg *optarg;
for (optarg = cli->buildmode->command->optargs; optarg; optarg = optarg->next) {
if (!strcmp(optarg->name, c->command)) break;
}
if (optarg && cli_find_optarg_value(cli, optarg->name, NULL) && !(optarg->flags & (CLI_CMD_OPTION_MULTIPLE)))
continue;
}
if (i < stage->num_words - 1) {
if (stage->words[i] && (strlen(stage->words[i]) < c->unique_len) && strcmp(stage->words[i], c->command)) continue;
n = c->children;
// If we have no more children, we've matched the *command* - remember this
if (!c->children) break;
i++;
continue;
}
if (lastchar == '?') {
delim_start = DELIM_NONE;
delim_end = DELIM_NONE;
// Note that buildmode commands need to see if that command is some optinal value
if (command_type == CLI_BUILDMODE_COMMAND) {
if (c->flags & (CLI_CMD_OPTIONAL_FLAG | CLI_CMD_OPTIONAL_ARGUMENT)) {
delim_start = DELIM_OPT_START;
delim_end = DELIM_OPT_END;
}
}
if (asprintf(&nameptr, "%s%s%s", delim_start, c->command, delim_end) != -1) {
if (asprintf(&strptr, " %s", nameptr) != -1) {
cli_int_wrap_help_line(strptr, c->help, comphelp);
free_z(strptr);
}
free(nameptr);
}
} else {
cli_add_comphelp_entry(comphelp, c->command);
}
}
out:
if (c) {
// Advance past first word of stage
i++;
stage->command = c;
stage->first_unmatched = i;
if (c->optargs) {
cli_int_parse_optargs(cli, stage, c, lastchar, comphelp);
} else if (lastchar == '?') {
// Special case for getting help with no defined optargs....
comphelp->num_entries = -1;
}
if (stage->status) {
// if we had an error here we need to redraw the commandline
cli_reprompt(cli);
}
}
cli_int_free_pipeline(pipeline);
}
static void cli_clear_line(int sockfd, char *cmd, int l, int cursor) {
// Use cmd as our buffer, and overwrite contents as needed.
// Backspace to beginning