-
Notifications
You must be signed in to change notification settings - Fork 48
/
cli_new.c
2355 lines (2102 loc) · 61.4 KB
/
cli_new.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
/* Workaround for features present in current kernel but
missing from old includes (glibc) in Debian Squeeze */
#ifndef O_CLOEXEC
#define O_CLOEXEC 02000000
#endif
#ifndef F_DUPFD_CLOEXEC
#define F_DUPFD_CLOEXEC 0x406
#endif
#include <sys/wait.h>
#include <dirent.h>
#include <limits.h>
#include <stdarg.h>
#include "cli_val.h"
#include "cli_parse.h"
#include <regex.h>
#include <errno.h>
#include <time.h>
#include <utime.h>
#include "cli_objects.h"
#include "cli_val_engine.h"
#include "cstore/cstore-c.h"
/* Defines: */
#define EXE_STRING_DELTA 512
#define PATH_DELTA 1000
#define ENDS_ALLOC 20
/* mcd vs. ccd location
change when m_root changed */
#define PATH_CM_LOCATION 25
#define VAR_REF_MARKER "$VAR("
#define VAR_REF_MARKER_LEN 5
#define VAR_REF_SELF_MARKER "$VAR(@)"
#define VAR_REF_SELF_MARKER_LEN 7
/* Global vars: */
vtw_path m_path, t_path;
void *var_ref_handle = NULL;
/* Local vars: */
static vtw_node *vtw_free_nodes; /* linked via left */
static int cond1[TOP_COND] ={5, 0,-1,-1, 0, 1, 0, 0};
static int cond2[TOP_COND] ={5, 0, 1,-1,-1, 1, 1, 0};
static char const *cond_formats[DOMAIN_TYPE] =
{
0,
"%u", /* INT_TYPE */
"%u.%u.%u.%u", /* IPV4_TYPE */
"%u.%u.%u.%u/%u", /* IPV4NET_TYPE */
"%x:%x:%x:%x:%x:%x:%x:%x", /* IPV6NET */
"%x:%x:%x:%x:%x:%x:%x:%x/%u", /* IPV6NET_TYPE */
"%x:%x:%x:%x:%x:%x" /* MACADDR_TYPE */
};
static int cond_format_lens[DOMAIN_TYPE] =
{
0,
1, /* INT_TYPE */
4, /* IPV4_TYPE */
5, /* IPV4NET_TYPE */
16, /* IPV6_TYPE */
17, /* IPV6NET_TYPE */
6 /* MACADDR_TYPE */
};
static int cli_val_len;
static char *cli_val_ptr;
static char *exe_string;
static int exe_string_len;
static int node_cnt;
static int free_node_cnt;
static boolean in_validate_val;
static valstruct validate_value_val; /* value being validated
to be used as $(@) */
/* Local function declarations: */
static void touch(void);
static int check_comp(vtw_node *cur);
static boolean check_syn_func(vtw_node *cur,const char *prepend_msg,boolean format, const char* func,int line);
#define check_syn(cur,msg_buf,format) check_syn_func((cur),(msg_buf),(format),__FUNCTION__,__LINE__)
static void copy_path(vtw_path *to, vtw_path *from);
static int eval_va(valstruct *res, vtw_node *node);
static int expand_string(char *p);
static void free_node(vtw_node *node);
static void free_node_tree(vtw_node *node);
void free_path(vtw_path *path);
static void free_string(char *str);
static vtw_node * get_node(void);
static void scan_ipv6(char *val, unsigned int *parts);
static int set_reference_environment(const char* var_reference,
clind_path_ref *n_cfg_path,
clind_path_ref *n_tmpl_path,
clind_path_ref *n_cmd_path,
int active);
/*************************************************
GLOBAL FUNCTIONS
***************************************************/
const char *cli_operation_name = NULL;
void bye(const char *msg, ...)
{
va_list ap;
OUTPUT_USER("%s failed\n",
(cli_operation_name) ? cli_operation_name : "Operation");
va_start(ap, msg);
vprintf(msg, ap);
printf("\n");
va_end(ap);
exit(1);
}
int mkdir_p(const char *path)
{
if (mkdir(path, 0777) == 0)
return 0;
if (errno != ENOENT)
return -1;
char *tmp = strdup(path);
if (tmp == NULL) {
errno = ENOMEM;
return -1;
}
char *slash = strrchr(tmp, '/');
if (slash == NULL)
return -1;
*slash = '\0';
/* recurse to make missing piece of path */
int ret = mkdir_p(tmp);
if (ret == 0)
ret = mkdir(path, 0777);
free(tmp);
return ret;
}
static void
touch_dir(const char *dp)
{
struct stat statbuf;
if (lstat(dp, &statbuf) < 0) {
if (errno != ENOENT)
bye("can't access directory: %s (%s)", dp, strerror(errno));
if (mkdir_p(dp) < 0)
bye("can't make directory: %s (%s)", dp, strerror(errno));
} else {
if(!S_ISDIR(statbuf.st_mode))
bye("directory %s expected, found file", dp);
utime(dp, NULL);
}
}
/*****************************************************
add_val:
verify that the types are the same;
if first valstruct is single value, convert it
into multivalue;
add the value of second to the list of first;
*****************************************************/
void add_val(valstruct *first, valstruct *second)
{
assert (first->free_me && second->free_me);
assert(second->cnt == 0);
if (first->cnt%MULTI_ALLOC == 0) {
/* convert into multivalue */
first->vals = my_realloc(first->vals, (first->cnt + MULTI_ALLOC) *
sizeof(char *), "add_value 1");
first->val_types = my_realloc(first->val_types,(first->cnt + MULTI_ALLOC) *
sizeof(vtw_type_e), "add_value 2");
if (first->cnt == 0) { /* single value - convert */
first->vals[0] = first->val;
first->val_types[0] = first->val_type;
first->cnt = 1;
first->val = NULL;
}
}
second->free_me = FALSE; /* we took its string */
first->vals[first->cnt] = second->val;
first->val_types[first->cnt] = second->val_type;
++first->cnt;
}
/*****************************************************
append - append node to the tail of list
*****************************************************/
void append(vtw_list *l, vtw_node *n, int aux)
{
vtw_node *lnode;
lnode = make_node(LIST_OP, n, NULL);
lnode->vtw_node_aux = aux;
if(l->vtw_list_tail) {
assert(l->vtw_list_tail->vtw_node_right == NULL);
l->vtw_list_tail->vtw_node_right = lnode;
} else {
assert(l->vtw_list_head == NULL);
l->vtw_list_head = lnode;
}
l->vtw_list_tail = lnode;
}
/* this recursive function walks the specified "vtw_node" tree representing
* "syntax" actions and looks for the first "in" action on a "self ref"
* specified as follows in template:
* $VAR(@) in "val1", "val2", ...
*
* if found, the corresponding valstruct is returned. this is used by the
* completion mechanism to get such "allowed" values specified by "syntax".
*/
const valstruct *
get_syntax_self_in_valstruct(const vtw_node *vnode)
{
const valstruct *ret = NULL;
if (!vnode) {
return NULL;
}
if (vnode->vtw_node_oper == COND_OP && vnode->vtw_node_aux == IN_COND
&& vnode->vtw_node_left && vnode->vtw_node_right) {
vtw_node *ln = vnode->vtw_node_left;
vtw_node *rn = vnode->vtw_node_right;
if (ln->vtw_node_oper == VAR_OP && ln->vtw_node_string
&& strncmp(VAR_REF_SELF_MARKER, ln->vtw_node_string,
VAR_REF_SELF_MARKER_LEN) == 0
&& rn->vtw_node_oper == VAL_OP) {
// found a matching syntax action. return valstruct.
return &(rn->vtw_node_val);
}
}
// this node does not match. walk down.
ret = get_syntax_self_in_valstruct(vnode->vtw_node_left);
if (!ret) {
ret = get_syntax_self_in_valstruct(vnode->vtw_node_right);
}
return ret;
}
/* execute specified command and return output in specified buffer as
* a null-terminated string. return number of characters in the output
* or -1 if failed.
*
* NOTE: NO attempt is made to ensure the security of the specified command.
* in other words, *DO NOT* use a user-supplied string as the command
* for this function.
*/
int
get_shell_command_output(const char *cmd, char *buf, unsigned int buf_size)
{
int ret = -1;
FILE *cmd_in = NULL;
size_t cnt = 0;
if (!buf || !buf_size) {
return -1;
}
if (!(cmd_in = popen(cmd, "r"))) {
return -1;
}
cnt = fread(buf, 1, buf_size - 1, cmd_in);
if (cnt == (buf_size - 1) || feof(cmd_in)) {
/* buffer full or got the whole output. null terminate */
buf[cnt] = 0;
ret = cnt;
}
if (pclose(cmd_in) == -1) {
ret = -1;
}
return ret;
}
#define LOCK_FILE "/opt/vyatta/config/.lock"
#define COMMIT_CMD "/opt/vyatta/sbin/my_commit"
void
release_config_lock(void)
{
unlink(LOCK_FILE);
/* error ignored */
}
/* try to clean up orphaned lock file. return -1 if failed */
static int
try_lock_cleanup(void)
{
char buf[128];
char proc[128];
FILE *f = NULL;
int ret = -1;
struct stat statb;
do {
/* get the proc entry */
if ((f = fopen(LOCK_FILE, "r")) == NULL) {
break;
}
if (fgets(proc, 128, f) == NULL) {
break;
}
/* read the proc entry */
if (stat(proc, &statb) == -1) {
if (errno == ENOENT) {
/* proc entry doesn't exist. can clean up the lock now */
ret = 0;
break;
}
}
fclose(f);
if ((f = fopen(proc, "r")) == NULL) {
/* can't open proc entry. assume we can't clean up */
break;
}
if (fgets(buf, 128, f) == NULL) {
/* can't read proc entry. assume we can't clean up */
break;
}
/* check if the process is commit */
if (strcmp(buf, COMMIT_CMD) == 0) {
/* it is commit. can't clean up */
break;
}
/* can clean up the lock */
ret = 0;
} while (0);
if (f) {
fclose(f);
}
if (ret == 0) {
unlink(LOCK_FILE);
if (stat(LOCK_FILE, &statb) != -1 || errno != ENOENT) {
/* proc entry still exists. cleanup failed */
ret = -1;
}
}
return ret;
}
static int
create_lock_file(int try_cleanup)
{
int fd = -1;
int i = 0;
struct timespec req;
#define LOCK_WAIT_TIME 2
#define LOCK_NUM_RETRIES 5
req.tv_sec = LOCK_WAIT_TIME;
req.tv_nsec = 0;
fd = open(LOCK_FILE, O_WRONLY | O_CREAT | O_EXCL, 0660);
if (fd == -1) {
for (i = 0; i < LOCK_NUM_RETRIES; i++) {
nanosleep(&req, NULL);
fd = open(LOCK_FILE, O_WRONLY | O_CREAT | O_EXCL, 0660);
if (fd >= 0) {
break;
}
}
}
if (fd == -1 && try_cleanup) {
if (try_lock_cleanup() != -1) {
/* cleanup succeeded */
fd = create_lock_file(0);
}
}
return fd;
}
int
get_config_lock(void)
{
int fd = -1;
FILE *lfile = NULL;
int ret = -1;
do {
/* create lock file */
fd = create_lock_file(1);
if (fd == -1) {
break;
}
/* write pid into lock file */
if ((lfile = fdopen(fd, "w")) == NULL) {
break;
}
if (fprintf(lfile, "/proc/%u/cmdline", getpid()) < 0) {
break;
}
/* fclose also closes fd */
if (fclose(lfile) != 0) {
break;
}
/* clean up on exit */
if (atexit(release_config_lock) != 0) {
break;
}
ret = 0;
} while (0);
if (ret == -1) {
if (lfile) {
fclose(lfile);
} else if (fd != -1) {
close(fd);
}
//code below will cause a concurrent commit to release lock on failure, and
//therefore, allow the concurrent to proceed the second time.
//lock can be forced via commit flag instead.
// release_config_lock();
}
return ret;
}
/* returns FALSE if execution returns non-null,
returns TRUE if every excution returns NULL
*/
boolean
execute_list(vtw_node *cur, const vtw_def *def, const char *prepend_msg)
{
boolean ret;
int status;
set_in_exec(TRUE);
status = char2val(def, get_at_string(), &validate_value_val);
if (status) return FALSE;
// XXX emulate original impl for "error" location
ret = check_syn(cur, prepend_msg,
(getenv("VYATTA_OUTPUT_ERROR_LOCATION") != NULL));
free_val(&validate_value_val);
set_in_exec(FALSE);
return ret;
}
/*****************************************************
make_node - create a node with oper, left, and right
*****************************************************/
vtw_node * make_node(vtw_oper_e oper, vtw_node *left,
vtw_node *right)
{
vtw_node *ret ;
ret = get_node();
ret->vtw_node_oper = oper;
ret->vtw_node_left = left;
ret->vtw_node_right = right;
ret->vtw_node_string = NULL;
ret->vtw_node_aux = 0;
return ret;
}
vtw_node *make_str_node0(char *str, vtw_oper_e op)
{
vtw_node *ret;
ret = make_node(op, NULL, NULL);
ret->vtw_node_string = str;
ret->vtw_node_type = TEXT_TYPE;
return ret;
}
/*****************************************************
make_str_node - create a VAL_OP node with str
*****************************************************/
vtw_node *make_str_node(char *str)
{
return make_str_node0(str, VAL_OP);
}
/*****************************************************
make_var_node - create a VAR_OP node with str
*****************************************************/
vtw_node *make_var_node(char *str)
{
return make_str_node0(str, VAR_OP);
}
/*****************************************************
make_val_node - create a VAl_OP node with str
*****************************************************/
vtw_node *make_val_node(valstruct *val)
{
vtw_node *ret;
assert(val->free_me);
ret = make_node(VAL_OP, NULL, NULL);
ret->vtw_node_val = *val;
val->free_me = FALSE;
return ret;
}
valstruct str2val(char *cp)
{
valstruct ret;
memset(&ret, 0, sizeof(ret));
ret.val_type = TEXT_TYPE;
ret.val = cp;
ret.free_me = TRUE;
return ret;
}
/****************************************************
STATIC FUNCTIONS
****************************************************/
int char2val_notext(const vtw_def *def, int my_type, int my_type2,
char *value, valstruct **valp, char *buf);
int char2val_text(const vtw_def *def, char *value, valstruct **valp);
/**************************************************
char2val:
convert string into valstruct verifying the type
according to def
****************************************************/
int char2val(const vtw_def *def, char *value, valstruct *valp)
{
int my_type = def->def_type;
int my_type2 = def->def_type2;
memset(valp, 0, sizeof (*valp));
/*
normal:
A) single text type
B) single non-text type
new:
C) Text plus non-text (NOT SUPPORTED)!
D) 2 non-text (double up first loop)
//so perhaps split the below into two functions, text and non-text
*/
if (my_type != TEXT_TYPE &&
my_type != ERROR_TYPE) {
//since this is the restricted type
//we'll either do two calls to this
//or one call to this as text
//currently fails to handle mixed text + non-text case...
char buf1[2048];
if (char2val_notext(def,my_type,my_type2,value,&valp,buf1) != 0) {
OUTPUT_USER("%s", buf1);
return -1; //only single definition
}
return 0;
}
else {
return char2val_text(def,value,&valp);
}
}
//non-text type processing block
int char2val_notext(const vtw_def *def, int my_type, int my_type2,
char *value, valstruct **valpp, char *err_buf)
{
valstruct *valp = *valpp;
int token;
boolean first = TRUE;
cli_val_len = strlen(value);
cli_val_ptr = value;
char type_buf[256];
if (my_type2 != ERROR_TYPE) {
sprintf(type_buf,"%s or %s",type_to_name(my_type),type_to_name(my_type2));
}
else {
sprintf(type_buf,"%s",type_to_name(my_type));
}
while(1) {
token = yy_cli_val_lex();
if (token != VALUE) {
if (first || token){
if (def->def_type_help){
set_at_string(value);
(void)expand_string(def->def_type_help);
sprintf(err_buf, "%s\n", exe_string);
} else {
printf("Wrong type of value in %s, need %s\n",
m_path.path_buf + m_path.print_offset, type_buf);
token = yy_cli_val_lex();
sprintf(err_buf, "\"%s\" is not a valid value of type \"%s\"\n",
value, type_buf);
}
return -1;
}
return 0;
}
if (my_type != get_cli_value_ptr()->val_type &&
(my_type2 != ERROR_TYPE && my_type2 != get_cli_value_ptr()->val_type)) {
if (def->def_type_help){
set_at_string(value);
(void)expand_string(def->def_type_help);
sprintf(err_buf, "%s\n", exe_string);
} else {
printf("Wrong type of value in %s, need %s\n",
m_path.path_buf + m_path.print_offset, type_buf);
token = yy_cli_val_lex();
sprintf(err_buf, "\"%s\" is not a valid value of type \"%s\"\n",
value, type_buf);
}
my_free(get_cli_value_ptr()->val);
if (first) {
return -1;
}
return 0;
}
if (first) {
*valp = *get_cli_value_ptr();
get_cli_value_ptr()->free_me = FALSE;
first = FALSE;
} else {
if (def->multi)
add_val(valp, get_cli_value_ptr());
else {
printf("Unexpected multivalue in %s\n", m_path.path);
free_val(get_cli_value_ptr());
}
}
token = yy_cli_val_lex();
if (!token) {
return 0;
}
if (token != EOL) {
token = yy_cli_val_lex();
sprintf(err_buf, "\"%s\" is not a valid value\n", value);
printf("Badly formed value in %s\n", m_path.path + m_path.print_offset);
if (token == VALUE) {
my_free(get_cli_value_ptr()->val);
}
return -1;
}
}
return 0;
}
int char2val_text(const vtw_def *def, char *value, valstruct **valpp)
{
valstruct *valp = *valpp;
char *endp, *cp;
int linecnt, cnt;
//PROCESSING IF TYPE IS TEXT TYPE
valp->val_type = TEXT_TYPE;
valp->val_types = NULL;
valp->free_me = TRUE;
/* count lines */
linecnt = 0;
for (cp = value; *cp; ++cp)
if (*cp == '\n')
++linecnt;
if (cp != value && cp[-1] != '\n')
++linecnt; /* last non empty non \n terminated string */
if (linecnt == 0) /* one empty non terminated string */
linecnt = 1;
if (linecnt == 1) {
valp->val=my_strdup(value, "char2val 1");
/*truncate '\n' etc */
endp = strchr(valp->val, '\n');
if (endp)
*endp = 0;
} else {
valp->cnt = linecnt;
cnt = (linecnt + MULTI_ALLOC - 1) / MULTI_ALLOC;
cnt *= MULTI_ALLOC;
valp->vals = my_malloc(cnt * sizeof(char *), "char2val 2");
valp->val_types = my_malloc(cnt * sizeof(vtw_type_e), "char2val 3");
int i;
for (i=0;i<cnt;++i) {
valp->val_types[i] = ERROR_TYPE;
}
for(cp = value, cnt = 0; cnt < linecnt; ++cnt) {
endp = strchr(cp, '\n');
if (endp)
*endp = 0;
valp->vals[cnt]=my_strdup(cp, "char2val 3");
if (endp) {
*endp = '\n';
cp = endp + 1;
} else {
/* non '\n' terinated string, must be last line, we are done */
++cnt;
assert(cnt == linecnt);
break;
}
}
}
return 0;
}
/****************************************************
val_comp:
compare two values per cond
returns result of comparison
****************************************************/
static boolean
val_cmp(const valstruct *left, const valstruct *right, vtw_cond_e cond)
{
unsigned int left_parts[9], right_parts[9];
vtw_type_e val_type;
int parts_num, lstop, rstop, lcur, rcur;
char const *format;
char *lval, *rval;
int ret=0, step=0, res=0;
val_type = left->val_type;
if (left->cnt)
lstop = left->cnt;
else
lstop = 1;
if (right->cnt)
rstop = right->cnt;
else
rstop = 1;
for(lcur = 0; lcur < lstop; ++lcur) {
if (!lcur && !left->cnt)
lval = left->val;
else
lval = left->vals[lcur];
for(rcur = 0; rcur < rstop; ++rcur) {
if (!rcur && !right->cnt)
rval = right->val;
else
rval = right->vals[rcur];
//don't bother comparing if these are different types.
if ((rcur || right->cnt)
&& right->val_types != NULL
&& right->val_types[rcur] != ERROR_TYPE) {
if (right->val_types[rcur] != val_type) {
continue;
}
}
parts_num = 0;
switch (val_type) {
case IPV6_TYPE:
parts_num = 8;
goto ipv6_common;
case IPV6NET_TYPE:
parts_num = 9;
ipv6_common:
scan_ipv6(lval,left_parts);
scan_ipv6(rval,right_parts);
break;
case IPV4_TYPE:
case IPV4NET_TYPE:
case MACADDR_TYPE:
case INT_TYPE:
format = cond_formats[val_type];
parts_num = cond_format_lens[val_type];
(void) sscanf(lval, format, left_parts, left_parts+1,
left_parts+2, left_parts+3, left_parts+4,
left_parts+5);
if ((rcur || right->cnt)
&& right->val_types != NULL
&& right->val_types[rcur] != ERROR_TYPE) {
format = cond_formats[right->val_types[rcur]];
}
(void) sscanf(rval, format, right_parts, right_parts+1,
right_parts+2, right_parts+3, right_parts+4,
right_parts+5);
break;
case TEXT_TYPE:
case BOOL_TYPE:
res = strcmp(lval, rval);
goto done_comp;
default:
bye("Unknown value in switch on line %d\n", __LINE__);
}
/* here to do a multistep int compare */
for (step = 0; step < parts_num; ++ step) {
if (left_parts[step] > right_parts[step]) {
res = 1;
break; /* no reason to continue checking other steps */
}
if (left_parts[step] < right_parts[step]) {
res = -1;
break; /* no reason to continue checking other steps */
}
res = 0;
}
done_comp:
if (res > 0)
res = 1;
else if (res < 0)
res = -1;
ret = ((res == cond1[cond]) ||
(res == cond2[cond]));
if (ret && cond == IN_COND) {
/* one success is enough for right cycle
in case of IN_COND, continue left cycle */
break;
}
if (!ret && cond != IN_COND)
/* one failure is enough in cases
other than IN_COND - go out */
return ret;
/* in all other cases:
(fail & IN_COND) or (success & !IN_COND)
contniue checking; */
}
}
return ret;
}
/****************************************************
check_comp:
evaluate comparison node.
returns boolean value of result
****************************************************/
static boolean check_comp(vtw_node *cur)
{
int ret;
int status;
valstruct left, right;
memset(&left, 0 , sizeof(left));
memset(&right, 0 , sizeof(right));
ret = FALSE; /* in case of status */
status = eval_va(&left, cur->vtw_node_left);
DPRINT("check_comp left status=%d type=%d cnt=%d val=[%s]\n",
status, left.val_type, left.cnt, left.val);
if (status)
goto free_and_return;
status = eval_va(&right, cur->vtw_node_right);
DPRINT("check_comp right status=%d type=%d cnt=%d val=[%s]\n",
status, right.val_type, right.cnt, right.val);
if (status)
goto free_and_return;
if(left.val_type != right.val_type) {
printf("Different types in comparison\n");
goto free_and_return;
}
ret = val_cmp(&left, &right,cur->vtw_node_aux);
free_and_return:
if (left.free_me)
free_val(&left);
if (right.free_me)
free_val(&right);
return ret;
}
/******************
Change value of var in the file
*****************/
static int write_value_to_file(const char* var_path,const char* value) {
if(var_path && value) {
{
/*Build directory, if necessary:*/
clind_path_ref var_dir_path=clind_path_construct(var_path);
if(!var_dir_path) bye("Can not construct path %s", var_path);
else {
char* end = clind_path_pop_string(var_dir_path);
if(!end || strcmp(end,VAL_NAME)) {
bye("Wrong end of path: %s (%s)", end,var_path);
}
free(end);end=NULL;
touch();
touch_dir(clind_path_get_path_string(var_dir_path));
clind_path_destruct(&var_dir_path);
}
}
{
/*Write to file*/
FILE* fp = fopen(var_path, "w");
if(!fp) bye("Can not open value file %s", var_path);
if (fputs(value, fp) < 0 || fputc('\n',fp) < 0)
bye("Error writing file %s", var_path);
fclose(fp);
}
}
return 0;
}
static int change_var_value(const char* var_reference,const char* value, int active_dir) {
int ret=-1;
if(var_reference && value) {
if (var_ref_handle) {
/* handle is set => we are in cstore operation. */
if (cstore_set_var_ref(var_ref_handle, var_reference, value,
active_dir)) {
ret = 0;
}
} else {
/* legacy usage */
char* var_path=NULL;
clind_path_ref n_cfg_path=NULL;
clind_path_ref n_tmpl_path=NULL;
clind_path_ref n_cmd_path=NULL;
if(set_reference_environment(var_reference,
&n_cfg_path,
&n_tmpl_path,
&n_cmd_path,
active_dir)==0) {
clind_val cv;
memset(&cv,0,sizeof(cv));
if(clind_config_engine_apply_command_path(n_cfg_path,
n_tmpl_path,
n_cmd_path,
FALSE,
&cv,
get_tdirp(),
TRUE,
active_dir)==0) {
var_path=cv.value;
}
}
if(n_cfg_path) clind_path_destruct(&n_cfg_path);
if(n_tmpl_path) clind_path_destruct(&n_tmpl_path);
if(n_cmd_path) clind_path_destruct(&n_cmd_path);
if(var_path) {
ret=write_value_to_file(var_path,value);
free(var_path);
}
}
}
return ret;
}
static int system_out(char *command, const char *prepend_msg, boolean eloc);
/****************************************************
check_syn:
evaluate syntax tree;
returns TRUE if all checks are OK,
returns FALSE if check fails.
****************************************************/
static boolean check_syn_func(vtw_node *cur, const char *prepend_msg,
boolean format, const char* func, int line)
{
int status;
int ret;
int ii;
switch(cur->vtw_node_oper) {
case LIST_OP:
ret = TRUE;
if (is_in_commit() || !cur->vtw_node_aux) {
ret = check_syn(cur->vtw_node_left,prepend_msg,format);
}
if (!ret || !cur->vtw_node_right) /* or no right operand */
return ret;
return check_syn(cur->vtw_node_right,prepend_msg,format);
case HELP_OP:
ret = check_syn(cur->vtw_node_left,prepend_msg,format);
if (ret <= 0){
if (expand_string(cur->vtw_node_right->vtw_node_string) == VTWERR_OK) {