/
copy.c
1372 lines (1248 loc) · 39.7 KB
/
copy.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
/* copy.c:
*
* Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>,
* The Silver Hammer Group, Ltd.
* 2009-2020 Alexander Reimer <alex_raw@rambler.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
//add by snop to include definition of data type
#include "include/httpd.h"
#include "include/httpd_config.h"
//#include "include/httpd_sysconf.h"
#include "copy.h"
#include "parser.h"
#include "parse_CGI.h" //char *w_strtok(char **str, char mark); void DoCGI(FILE *out, char *filename)
#include "copy_CGI.h"
#include "copy_tbl.h" //parse_tbl() & free_tbl()
static char copybuf[16384];
#define BUFSIZE 128
#define DEBUG
#undef DEBUG
extern int TIMEOUT;
extern struct cfg_parse1 *cfg1;
struct cfg_parse1 **cfg_p = &cfg1;
/*is old! use parsestr instead!*/
/*char *strstrend( char *a, char *b) //check b string in a string and gives part of a-str where b is ending, case ignored
{
//b must not be NULL!
char *c;
char *d;
while (*a)
{
c = (char *) b;
d = (char *) a;
while (*c)
{
if (tolower(*c) != tolower(*d)) break;
if (!(*d)) return NULL;
c++;
d++;
}
if (!(*c))
return (char *) d;
a++;
}
return NULL;
}*/
void print_pstr(FILE *out, char *tmp){
char ch, *tmp1, *tmp2;
unsigned int i;
while(*tmp){
ch = *tmp;
if(*tmp == '/'){
tmp++;
if(*tmp == '?'){
tmp++;
char *m;
tmp1 = tmp;
tmp2 = NULL;
while(*tmp1){ // /?var/?
if(*tmp1 == '/' && *(tmp1+1) == '?'){
i = tmp1 - tmp + 1;
if((i > 1) && (i < 33)){
m = malloc(i);
if(m){
strmycpy(m, tmp, i);
tmp2 = get_var(NULL, m); //get_var and get_variable
free(m);
}
}
tmp=tmp1+2;
break;
}
tmp1++;
}
if(tmp2 && *tmp2){
unsigned long long size = strncpy_(NULL, tmp2, 0)+1; //this is max. size of tmp2-string
if(size > 1 && (m = malloc(size))){
strncpy_(m, tmp2, size);
fprintf(out, "%s", m);
free(m);
}
}
continue;
} else if(*tmp != '/') tmp--;
} else
if(*tmp == '\\'){
tmp++;
switch(*tmp){
case 't': ch = '\t'; break;
case 'n': ch = '\n'; break;
case '\"':
case '\\': ch = *tmp; break;
case '0': return;
}
}
putc(ch, out);
tmp++;
}
}
unsigned char *point[2]; //0 - place where is ch_zero by /]; 1 - end of string
unsigned char ch_zero = '\0';
unsigned long number = 0;
unsigned long value_ = 0;
unsigned long stack_ = 0;
unsigned char *parsestr1(unsigned char *d, unsigned char *c) //try identic strings!, "xxx*NULL" combination
{
unsigned char *tmp, *tmp2, *tmp3, ch;
unsigned int i;
unsigned long digi;
while (*c)
{
if(*c == '/' ){
c++;
switch (*c){
case '*':/* printf("d1:%s\n", d);*/
c++; while(1){tmp = parsestr1(d, c); if (tmp) return tmp; if(! *d) break; d++;} return NULL;//depend on continues parsing
//if * in b string -> some symbols between
// !a*(b+c) = !(a+!(b+c))-> /!/Ba/\/!/Bb/\c/\/E/E
case 'B': c++; tmp = NULL; tmp2 = c; i = 0;
while(*tmp2){
if(*tmp2 == '/' && *(tmp2+1) == '/') tmp2++; //tmp2+=2
else if(*tmp2 == '/' && *(tmp2+1) == 'B' && i <= 1024) i++; //if(/B)
else if(*tmp2 == '/' && *(tmp2+1) == 'E'){ //if(/E) -optional
if(i == 0){tmp = tmp2; break;}
else i--;
}
tmp2++;
}
//tmp - end of compare-string
//tmp2 - can be used,
while(1){ //not matched
tmp2 = parsestr1(d, c);
if(tmp2) break;
i = 0;
while(1){
if(*c == '/' && *(c+1) == '/') c++; //c+=2
else if(*c == '/' && *(c+1) == 'B' && i <= 1024) {i++; c++;}
else if(i != 0 && *c == '/' && *(c+1) == 'E'){ i--; c++;}
else if(i == 0 && *c == '/' && *(c+1) == '\\'){c = c + 2; break;} // if(/\)
else if((tmp && (c == tmp)) || (*c == '\0')) return NULL; //end of compare-strings - no matches - return NULL
c++;
}
}
//printf("str:%s d=%s c=%s\n", tmp2,d,c);
if(tmp && *tmp2) {
// return parsestr1(tmp2, tmp+2);}
d = tmp2; /*tmp2 is not always the end of string!! check it out*/
c = tmp + 2; continue;}
else return tmp2;
case '{': c++; ch = *c; c++; tmp = NULL; tmp2 = c; i = 0;
while(*tmp2){
if(*tmp2 == '/' && *(tmp2+1) == '/') tmp2++; //tmp2+=2
else if(*tmp2 == '/' && *(tmp2+1) == '{' && i <= 1024) i++; //if(/{)
else if(*tmp2 == '/' && *(tmp2+1) == '}'){
if(i == 0){tmp = tmp2 + 2; break;}
else i--;
}
tmp2++;
}
//tmp - end of compare-string
//tmp2 - can be used,
// /t/{*/*="/*/N\\N"/t/}-->
// /t/{-/*="/*/N\\N"/t/}-->
// | |
// c tmp
if(ch == '*'){// /{*..../} - 0 Times or more
while(1){
if(tmp){
tmp2 = parsestr1(d, tmp);
if(tmp2) return tmp2;
}
tmp2 = parsestr1(d, c);
if(tmp2 == NULL) return NULL;
if(*tmp2 == '\0') return tmp2;
d = tmp2;
}
}
if(ch == '-'){// /{-..../} - 1 Time or more
while(1){
tmp2 = parsestr1(d, c);
if(tmp2 == NULL) return NULL;
if(*tmp2 == '\0') return tmp2;
d = tmp2;
if(tmp){
tmp2 = parsestr1(d, tmp);
if(tmp2) return tmp2;
}
}
}
if(ch == 'R' || ch == 'r'){// /{R..../} - Repead 1 Time or more
i = 0; // /{r..../} - Repead 0 Time or more
while(1){
tmp2 = parsestr1(d, c);
if(tmp2 == NULL) break;
if(tmp2 == d) break; //no progress
d = tmp2; i = 1;
}
if(ch == 'R' && i == 0) return NULL;
c = tmp; continue;
}
if(ch == 'C'){// /{C..../} - Compare witch pointer is bigger
tmp2 = parsestr1(d, c);
if(tmp2 == NULL) return NULL;
tmp3 = parsestr1(d, tmp);
if(tmp3 == NULL) return NULL;
if(tmp3 > tmp2) return tmp3;
return NULL;
}
return NULL; //default, if not '*' and not '-'
case '(': c++; ch = *c; c++; tmp = c; i = 0;
while(1){
if(*tmp == '\0') return NULL;
else if(*tmp == '/' && *(tmp+1) == '/') tmp++; //tmp+=2
else if(*tmp == '/' && *(tmp+1) == '(' && i <= 1024) i++; //if( /( )
else if(*tmp == '/' && *(tmp+1) == ')'){ //if( /) )
if(i == 0){tmp = tmp + 2; break;}//think about!! this is /) before /: found
else i--;
}
else if(i == 0 && *tmp == '/' && *(tmp+1) == ':'){
tmp = tmp + 2;
break; //if( /) )
}
tmp++;
}
// in c - plus, and in tmp - minus
if(ch == 'S'){ ///(S.../:.../) - use Stack with !manual! inc- decrimintation
i = 0; tmp2 = d; tmp3 = d;
while(1){
tmp2 = parsestr1(d, c);
if(tmp2){
if(tmp2 != d) d = tmp2; //progress!!
else tmp2 = NULL;
}
if(i && stack_ == 0) break;
if(tmp2 == NULL && tmp3 == NULL) return NULL;
tmp3 = parsestr1(d, tmp);
if(tmp3){
if(tmp3 != d) d = tmp3; //progress!!
else tmp3 = NULL;
}
if(i && stack_ == 0) break;
if(tmp2 == NULL && tmp3 == NULL) return NULL;
i = 1;
}
} else return NULL;
// in d - end of parsed string
i = 0;
while(1){
if(*tmp == '\0') return NULL;
else if(*tmp == '/' && *(tmp+1) == '/') tmp++; //tmp+=2
else if(*tmp == '/' && *(tmp+1) == '(' && i <= 1024) i++; //if( /( )
else if(*tmp == '/' && *(tmp+1) == ')'){ //if( /) )
if(i == 0){c = tmp + 2; break;}
else i--;
}
tmp++;
}
continue;
case '\\':
case 'E':
case ':':
case ')':
case '}': point[1] = d;
return d;
/* set number to /n10n or stack to /n15s*/
case 'n': c++; tmp = c;
while(*tmp && *tmp != 'n' && *tmp != 's') tmp++;
digi = 0;
i = 0;
while(c[i] >= '0' && c[i] <= '9'){ digi = digi*10 + (c[i] - '0'); i++; if(i > 5) break;}
c = tmp;
if(*c == 'n') number = digi; //set number to digi
else if(*c == 's') stack_ = digi;
if(*c) c++; //if not end - increase c.
continue;
/* by /sn - set number = source */
/* by /sv - set value = source */
/* by /ss - set stack = source */
case 's': c++; ch = *c; i = 0;
digi = 0;
while(*d >= '0' && *d <= '9'){
if(i < 7){ digi = digi*10 + (*d - '0'); i++;}
d++;
}
if(i == 0) return NULL; //in *d no digits
if(ch == 'n'){ c++; number = digi; continue;}
else if(ch == 'v'){ c++; value_ = digi; continue;}
else if(ch == 's'){ c++; stack_ = digi; continue;}
else { c = c - 2; break;} //go back to c = /sv(n) and parse it
/* by /hn - set number = source (hex) */
/* by /hv - set value = source (hex) */
/* by /hs - set stack = source (hex) */
case 'h': digi = 0; ch = 0;
while(1){
i = *d;
if(i >= '0' && i <= '9') ch = '0';
else if(i >= 'A' && i <= 'F') ch = 'A' - 10;
else if(i >= 'a' && i <= 'f') ch = 'a' - 10;
else break;
digi = (digi<<4) + i - ch;
d++;
}
if(ch == 0) return NULL;
c++;
if(*c == 'n'){ c++; number = digi; continue;}
else if(*c == 'v'){ c++; value_ = digi; continue;}
else if(*c == 's'){ c++; stack_ = digi; continue;}
else { c = c - 2; break;} //go back to c = /hv(n) and parse it
/* by /iv<10v or /iv>5v or /in<45n or /in>24n or /is>0s - if not matches return NULL */
/* by /iv+10v or /iv-5v or /in-45n or /in+24n or /is+1s */
case 'i': c++; ch = *c;
if(ch != 'v' && ch != 'n' && ch != 's'){ c = c - 2; break;}
c++; i = *c;
if(i != '<' && i != '>' && i != '+' && i != '-'){ c = c - 3; break;}
c++; digi = 0;
while(*c){
if(*c >= '0' && *c <= '9'){ digi = digi*10 + (*c - '0'); c++; continue;}
else if(*c == ch) break;
else{ break;}
}
if(*c == ch){
if(i == '+'){
if(ch == 'v'){ value_ += digi;}
else if(ch == 'n'){ number += digi;}
else if(ch == 's'){ stack_ += digi;}
c++; continue;
}else if(i == '-'){
if(ch == 'v'){ value_ -= digi;}
else if(ch == 'n'){ number -= digi;}
else if(ch == 's'){ stack_ -= digi;}
c++; continue;
}
unsigned long val = 0;
if(ch == 'v'){ val = value_;}
else if(ch == 'n'){ val = number;}
else if(ch == 's'){ val = stack_;}
if(i == '>'){ if(val <= digi) return NULL;}
else if(i == '<'){ if(val >= digi) return NULL;}
c++; continue;
}else return NULL;
/* skip zero or one character*/
case '0': tmp = d; while(tmp <= d+1){tmp = parsestr1(tmp, c+1); if (tmp) return tmp; tmp++;} return NULL;
/* skip one symbol in d, exept \0 */
case '|': c++; if(*d == '\0') return NULL; d++; continue;
/* by x/.x. skip x-symbol 1 or more times */
/* by /.x. skip x-symbol all at once (0 or more times)*/
case '.': if(*c != *(c+2) || *(c+1)=='\0') return NULL; //error! - or make it with continue
c=c+1; while(*d == *c) d++; c=c+2; continue;
/* by /,x, skip x-symbol 0 or 1-time */
case ',': if(*c != *(c+2) || *(c+1)=='\0') return NULL; //error!
c=c+1; tmp = d; while((d <= tmp+1) && (*d == *c)) d++; c=c+2; continue; //used in *search[]
/* by /lxl get last x-symbol in string 1 or more times */
case 'l':
/* by /LxL get last x-symbol in string 0 or more times*/
case 'L': if(*c != *(c+2) || *(c+1)=='\0') return NULL; //error!
i=0;c=c+1; tmp=d; while(*tmp){if(*tmp == *c){ d=tmp+1;i=1;} tmp++;}
if(*(c+1)=='l'&&i==0)return NULL;c=c+2; continue; //used in serv-others_1.htm
/* by /NxN x-symbol is not in string */
case 'N': if(*c != *(c+2) || *(c+1)=='\0') return NULL; //error!
c=c+1; if(*c == *d) return NULL; c=c+2; d++; continue; //used in
/* skip all blanks or tabs or enters in d */
// case ' ': tmp = d; while(*d == ' ' || *d == '\t' || *d == '\n' || *d == '\r') d++; if(tmp == d) return NULL; c++; continue;
case ' ': while(*d == ' ' || *d == '\t' || *d == '\n' || *d == '\r') d++; c++; continue;
/* skip all blanks and tabs */
case 't': while(*d == ' ' || *d == '\t') d++; c++; continue;
/* /<xCHARS/> matches CHARS with d, continue if matches and return null if not */
case '<': c++;
switch(*c){
case '-': // /<-CHARS/> matches chars 1 or more times
case '*': // /<*CHARS/> matches chars zero or more times
c++; tmp = c; i=0; while((*c != '/'|| c[1] != '>') && *c){
if(*c == '\\'){
c++;
switch(*c){
case '\\':
case '-':
case '\"': ch = *c;break;
case 't': ch = '\t';break;
case 'n': ch = '\n';break;
default: c--; ch = *c; break;
}
} else {
ch = *c;
if(*(c+1) == '-' && *(c+2)){ //if /<-a-z/> --> (ch=a <= *d <= *(c+2)=z)
if((ch < *(c+2)) && (*d >= ch) && (*d <= *(c+2))){
do{if(*d == '\0') break; d++;}while((*d >= ch) && (*d <= *(c+2)));
i = 1;
if(*d){ c = tmp; continue;}
}
c = c + 3; continue;
}
}
if(ch==*d){ do{d++;}while(ch==*d);
c = tmp; i=1; continue;
}
c++;}
if(*(tmp-1)=='-' && i==0) return NULL;
if(*c == '/') c=c+2; continue;
case '0': // /<0CHARS/> matches chars zero or 1 time
case '1': // /<1CHARS/> matches chars one time
case 'N': // /<NCHARS/> all chars not matches one time
i = 0;tmp = c; c++; while((*c != '/'|| c[1] != '>') && *c){
if(*c == '\\'){
c++;
switch(*c){
case '\\':
case '-':
case '\"': ch = *c;break;
case 't': ch = '\t';break;
case 'n': ch = '\n';break;
case '0': ch = '\0';break;//check if *d == '\0'.
//In /* is set until zero, inclusive zero!
default: c--; ch = *c; break;
}
} else {
ch = *c;
if(*(c+1) == '-' && *(c+2)){ //if /<1a-z/> --> (ch=a <= *d <= *(c+2)=z)
if((ch < *(c+2)) && (*d >= ch) && (*d <= *(c+2))) i = 1;
c = c + 3; continue;
}
}
if(ch == *d) i = 1; c++;}
if(*(tmp)=='1' && i==0) return NULL;
if(*(tmp)=='N' && i==1) return NULL;
if(*c == '/') c=c+2; if(*d != '\0' && (*(tmp)=='1' || *(tmp)=='N' || (*(tmp)=='0' && i==1))) d++; continue;
default:
case '\0': c--; continue; // rest='<\0'
}
case '[': if(parsestr1(d, c+1)){ /*point[0] = d;*/ return d;} return NULL; //if [ in b so this pointer will be returned, and c is move forward. BEGINNofSTR
case ']':/* printf("d:%s\n", d);*/
tmp = parsestr1(d, c+1);
if(!tmp) return NULL;
//case tmp=d (*(c+1) = '/0')[ end braces in end of string]
if(tmp == d && *tmp){ tmp++; point[1] = tmp;}
/*if(*d) point[0] = d+1; else */
point[0] = d; // point[0] points on character to be zeroed
ch_zero = *d;
*d = '\0'; return tmp;//depend on continues(if /[ goes after /] - begin)
// make end of string hier!, check rest as (]...) and return next to end char.
/* second '/' charakter is to match */
case '/': break;
// case '\0': continue;//need to check it!!!!!!
/* end of string is to match */
case '\0': if(*d != '\0') return NULL; return d;
case '!': //Negotiation NEW
// \!\*ab -no more "ab" in rest of string
tmp = parsestr1(d, c+1);
if(tmp) return NULL;
else { point[1] = d; return d;}
case '?': // /?variable?/ - nicht eingebetet(simple)
case 'Q': // /Qvar?/ - eingebetet
ch = *c; //in ch: Q or ?
c++;
char *m;
tmp = c;
tmp2 = NULL;
while(*tmp){ // /?var?/ - is old, /?var/? - is new
if((*tmp == '?' && *(tmp+1) == '/') || (*tmp == '/' && *(tmp+1) == '?')){
i = tmp - c + 1;
if((i > 1) && (i < 33)){
m = malloc(i);
if(m){
strmycpy(m, c, i);
tmp2 = get_var(NULL, m); //get_var and get_variable
free(m);
}
}
c=tmp+2;
break;
}
tmp++;
}
if(tmp2 && *tmp2){
if(ch == '?'){
i = strlen(tmp2);
if(i && strncmp(d, tmp2, i)) return NULL;
d = d + i;
}else{
static int time = 0;
time++;
if(time < 10){
unsigned long long size = strncpy_(NULL, tmp2, 0)+1; //this is max. size of tmp2-string
if(size > 1 && (m = malloc(size))){
strncpy_(m, tmp2, size);
tmp = parsestr1(d, m);
free(m);
if(!tmp){time--; return NULL;}
d = tmp;
//if(*d != '\0') d++; //????????
}
}else printf("parsestr: max. counter\n");
time--;
}
}
else return NULL;
continue; //it's or c++ or c=tmp+2
case '-': if(*(c+1) == '-') { d=d-1; c=c+2;} // /-- -means d-1
continue;
case 'm': c++; ch = *c; c++; tmp2 = c;// /m-WARN: not matched!\0 -thisIsNotMatched!
while(*tmp2){
if(*tmp2 == '\\' && *(tmp2+1) == '\\') tmp2++; //tmp2 = tmp2 + 2
else if(*tmp2 == '\\' && *(tmp2+1) == '0'){ tmp2 = tmp2 + 2; break;} //string must have \\0 at the end
tmp2++; //and don't have /\\ or /}, /), /E, /:
}
if(ch == 'w') print_pstr(fdcr, c); //putout to WEB: /mw....\0 without matching!
tmp = parsestr1(d, tmp2);
if(ch == '-' && tmp == NULL) print_pstr(stderr, c);
if(ch == '+' && tmp != NULL) print_pstr(stderr, c);
if(ch == 'N' && tmp == NULL) print_pstr(fdcr, c); //if not mantched the rest - putout to WEB: /mN...\0
if(ch == 'W' && tmp != NULL) print_pstr(fdcr, c); //if matched the rest - putout to WEB: /mW....\0
return tmp;
// /mw..\0/mW..\0/?par/? == /mw..\0/?par/?/mW..\0
default: c--;
}
} else if(*c == '\\'){
c++;
switch(*c){
/* new string is to match */
case 'n': if(*d != '\n') return NULL; c++; d++; continue;
case 't': if(*d != '\t') return NULL; c++; d++; continue;
case 'r': if(*d != '\r') return NULL; c++; d++; continue;
/* \" string is to match */
case '\"': if(*d != '\"') return NULL; c++; d++; continue; //need to change it
/* \0 the same as "bla/"; if after is "bla\02" -> "bla\0" + number=2 */
case '0': if(*d != '\0') return NULL;
c++; int digi = 0; i = 0;
while(c[i] >= '0' && c[i] <= '9'){ digi = digi*10 + (c[i] - '0'); i++; if(i > 5) break;}
if(i) number = digi; //set number to digi
point[1] = d;//new 14.07.2019
return d;
case '\\': break;
default: c--;
}
}
// if (tolower(*c) != tolower(*d)) break;
if (*c != *d) return NULL;
if (!(*d)) {printf("PARSESTR1<<<<<<</n");break;} //not happens *d=*c=='\0'
c++;
d++;
}
point[1] = d;
return (char *) d;//ENDofSTR
}
char *parsestr1_( char *d, char *c){ //push and pop the pointers
char *tmp, *a, *b;
a = point[0];
b = point[1];
tmp = parsestr1(d, c);
point[0] = a;
point[1] = b;
return tmp;
}
char *parsestr2( struct parsestr *ptr, char *d, char *c){ //use the pointers in struct
char *tmp;
number = 0;
point[0] = NULL;
if(tmp = parsestr1(d, c) /*&& (ptr != NULL)*/){
ptr->ch = ch_zero;
ptr->num = number;
ptr->zero = point[0]; //if NULL -> restore_str is not made
ptr->end = point[1];
}
return tmp;
}
char *restore_str( struct parsestr *ptr){ //note: in parsestring MUST BE-> "/]"
if(ptr->zero){
*(ptr->zero) = ptr->ch;
}
return ptr->end;
}
int copy(FILE *read_f, FILE *write_f) //used by rename_ aswell
{
int n;
int wrote;
// alarm(TIMEOUT);
while ((n = fread(copybuf,1,sizeof(copybuf),read_f)) != 0) {
//// alarm(TIMEOUT);
wrote = fwrite(copybuf,n,1,write_f);
//// alarm(TIMEOUT);
if (wrote < 1)
return -1;
}
// alarm(0);
return 0;
}
int copy_file(char *file, FILE *out)
{
FILE *fp;
int n;
int wrote;
char *err_str="[ERR] File: %s cannot open\n";
if((fp=fopen(file,"r")) == NULL){
printf(err_str, file);
fprintf(out, err_str, file);
return 1;
}
else{
while ((n = fread(copybuf,1,sizeof(copybuf),fp)) != 0) {
wrote = fwrite(copybuf,n,1,out);
if (wrote < 1){
fclose(fp);
return 1;
}
}
fclose(fp);
}
return 0;
}
/*if file is *.inc - then parse it by handle_get, otherwise put out without parsing*/
int copy_file_include(char *file, FILE *out)
{
char *ptr = file;
int i = 0, ret = 0;
while(*ptr){
if(*ptr == '?'){
*ptr = '\0';
i = 1;
break;
}
ptr++;
}
if(i){
//here must be arg_handle for ptr+1
handle_arg(1, ptr+1);
}
// if(parsestr1_(file, "/l.linc/") || parsestr1_(file, "/l.lhtm/")){ //
if(parsestr1_(file, "/l.l/Binc\\0/\\htm\\0/\\")){ //like: include.inc or incude.htm
FILE *infile;
char *data;
struct stat stbuf;
if(stat(file, &stbuf) || (stbuf.st_size>1024*64) || !(infile = fopen(file,"r"))) {
fprintf(out, "Unable to open HTML file %s, %d\n", file, errno);
if(i){
*ptr = '?';
free_arg(1);
}
return 1;
}
data = (char *)malloc(stbuf.st_size+1);
if(data){
fread(data, stbuf.st_size, 1, infile);
data[stbuf.st_size] = '\0';
handle_get(data, out);
free(data);
} else {
fprintf(out, "ERR: Unable allocate memory.\n");
//ret = 1;//error alocate
}
fclose(infile);
} else ret = copy_file(file, out); //all others will only copied
if(i){
*ptr = '?';
free_arg(1);
}
return ret;
}
int copy_file_buf(char *file, char *buf, long long size)
{
FILE *fp;
int n;
int wrote;
char *err_str="[ERR] File: %s cannot open\n";
if(!size) return -1;
if((fp=fopen(file,"r")) == NULL){
printf(err_str, file);
return -1;
}
else{
// while ((n = fread(buf,1,size-1,fp)) != 0) {
// }
fread(buf,size-1,1,fp);
fclose(fp);
}
buf[size-1] = '\0';
return 0;
}
// find there is a input type is "radio" in this line and insert the value for displaying
//inline char *radio_value_insert(char *line, char *var_head, FILE *out)
char *radio_value_insert(char *line, char *var_head, FILE *out)
{
char *file_head1, *file_head2, *radio_var, *ptr;
if((radio_var=parsestr1(var_head,"/*value=\"")) && (file_head1=parsestr1(var_head,"/*name=\"")))
{
if(file_head1<radio_var){
file_head2=radio_var; //last pointer
ptr=file_head1; //neaher pointer
}else{
file_head2=file_head1; //last pointer
ptr=radio_var; //neaher pointer
}
file_head2=strchr(file_head2,'\"');
file_head2[0]='\0';
file_head2++;
fprintf(out,"%s\"", line);
ptr=strchr(ptr,'\"');
ptr[0]='\0';
ptr = get_cfg_value(NULL, file_head1, 0); //another ptr
if(ptr && (strcmp(ptr,radio_var)==0) )
fprintf(out,"checked");
return file_head2;
} else {
putc(*line, out); //put out "<" the rest will be made by handle_get
return (line+1);
}
}
//register new parameter called name, from value, size-lang.
//return 1=success and 0=false
int reg_par(char *name, char *value, long long size)
{
struct cfg_parse1 *ptr = cfg1;
while(ptr){
if(ptr->type == CFG_TMP && !strcmp(name, ptr->web_name)){
ptr->size = size;
ptr->value = value;
ptr->new_value = value;
return 1;
}
ptr = ptr->next;
}
return 0;
}
//inline void include_(char *var_head, FILE *out) //SSI made total brutal, no checks of file path, and include must be along in string of file!
void include_(char *var_head, FILE *out)
{
char *var_head2;
struct parsestr strct;
do{
if ((var_head2 = parsestr2(&strct, var_head,"par=\"/[/*/]\"/ "))!=NULL){ //<!--#include par="size:web_name:name:pattern" -->
/*collect parameters in a array*/
char *a, *ptr;
int i = 0, str_size, val_size;
struct cfg_parse1 *cfg_pointer;
*cfg_p = (struct cfg_parse1 *)malloc(sizeof(struct cfg_parse1));
cfg_pointer = *cfg_p;
if(cfg_pointer == NULL) continue;
ptr = var_head2;
while(i < 3){
a = w_strtok(&ptr, ':');
if(a)
switch(i){
case 0: val_size = atoi(a);
if(!val_size || val_size > 300) return; //size is in impassable range - so skip it all (whole parameter string will be skipped!)
a = ptr;
str_size = strlen(a)+1;
ptr = (char*)malloc(str_size + 2*val_size);//str = "[part of var_head2][value][new_value]"
if(ptr == NULL){
i = 5; continue;
}
strcpy(ptr, a);
cfg_pointer->type = CFG_PAR;
cfg_pointer->str = ptr;
cfg_pointer->size = val_size;
cfg_pointer->changed = 0;
cfg_pointer->saved = 0;
cfg_pointer->value = ptr + str_size;
// *(cfg_pointer->value) = '\0';
cfg_pointer->new_value = ptr + str_size + val_size;
// *(cfg_pointer->new_value) = '\0';
memset(cfg_pointer->value, 0, 2*val_size);//fill with zeros
cfg_pointer->name = NULL;
cfg_pointer->pattern = NULL;
cfg_pointer->web_name = NULL;
cfg_pointer->next = NULL;
break;
case 1: cfg_pointer->web_name = a; break;
case 2: cfg_pointer->name = a; cfg_pointer->pattern = ptr; break;
}
else {
printf("par=\"\" not full, broken by %d\n", i+1);
break;
}
i++;
}
if(i != 5){
printf("Collected parameter: %s:%s:%lld:%s\n", cfg_pointer->web_name, cfg_pointer->name, cfg_pointer->size, cfg_pointer->pattern);
cfg_p = &(cfg_pointer->next);
}else{
printf("ERROR allocate memory\n");
if(cfg_p && *cfg_p){
free(*cfg_p);
*cfg_p = NULL; //main criteria to abort moving in array.
}
}
} else if ((var_head2 = parsestr2(&strct, var_head,"readcfg/ "))!=NULL){ //<!--#include readcfg -->
/*start to fill parameters from config to collected array*/
ReadConfiguration();
} else if ((var_head2 = parsestr2(&strct, var_head,"area=\"/[/*/]\"/ "))!=NULL){ //<!--#include area="size:web_name" -->
/*collect parameters in a array*/
char *a, *ptr;
int i = 0, str_size;
long long val_size;
struct cfg_parse1 *cfg_pointer;
*cfg_p = (struct cfg_parse1 *)malloc(sizeof(struct cfg_parse1));
cfg_pointer = *cfg_p;
if(cfg_pointer == NULL) continue;
ptr = var_head2;
while(i < 2){
a = w_strtok(&ptr, ':');
if(a)
switch(i){
case 0: val_size = atoll(a);
if(val_size > (100 *1024)) return; //size is in impassable range - so skip it all (whole parameter string will be skipped!)
a = ptr;
str_size = strlen(a)+1;
ptr = (char*)malloc(str_size + val_size);//str = "[part of var_head2][value]"
if(ptr == NULL){
i = 5; continue;
}
strcpy(ptr, a);
cfg_pointer->type = (val_size != 0) ? CFG_AREA : CFG_TMP;
cfg_pointer->str = ptr;
cfg_pointer->size = val_size;
cfg_pointer->changed = 0;
cfg_pointer->saved = 0;
if(val_size != 0){//AREA="size:name"
cfg_pointer->value = ptr + str_size;
cfg_pointer->new_value = ptr + str_size;
memset(cfg_pointer->value, 0, val_size);//fill memory with 0
}else{//AREA="0:temp_name"
cfg_pointer->value = NULL;
cfg_pointer->new_value = NULL;
}
cfg_pointer->name = NULL; //ptr + str_size - 1;
cfg_pointer->pattern = NULL; //ptr + str_size - 1;
cfg_pointer->web_name = NULL;
cfg_pointer->next = NULL;
break;
case 1: cfg_pointer->web_name = a; break;
}
else {
printf("area=\"\" not full, broken by %d\n", i+1);
break;
}
i++;
}
if(i != 5){
printf("Collected parameter: [%s:%lld]\n", cfg_pointer->web_name, cfg_pointer->size);
cfg_p = &(cfg_pointer->next);
}else{
printf("ERROR allocate memory\n");
if(cfg_p && *cfg_p){
free(*cfg_p);
*cfg_p = NULL; //main criteria to abort moving in array.
}
}
} else
//printf("include file: %s\n", var_head);
if ((var_head2 = parsestr2(&strct, var_head,"file=\"/[/*/N\\N/]\"/ "))!=NULL){ //<!--#include file="..\".." -->
//printf("include file: %s\n", var_head2);
copy_file_include(var_head2, out);
} else
if ((var_head2 = parsestr2(&strct, var_head,"exec=\"/[/*/]\"/ "))!=NULL){ //<!--#include exec="..." -->
my_system(out, var_head2);
} else if ((var_head2 = parsestr2(&strct, var_head,"shell=\"/[/*/N\\N/]\"/ "))!=NULL){ //<!--#include shell="..." -->
my_shell(out, var_head2);
} else if ((var_head2 = parsestr2(&strct, var_head,"cgi=\"/[/*/]\"/ "))!=NULL){ //<!--#include cgi="..." -->
if(DoCGI(out, var_head2, 1)) fprintf(out, "Not Found: %s\n", var_head2);// what about arg??
else free_par_tmp();//clear all "temp"-parameters
} else if((var_head2 = parsestr2(&strct, var_head, "tbl_select=\"/[/*/]\"/ "))!=NULL){
show_tbl(var_head2, out);
} else if((var_head2 = parsestr2(&strct, var_head, "tbl_check=\"/[/*/]\"/ "))!=NULL){
show_tbl_chck(var_head2, out);
} else if((var_head2 = parsestr2(&strct, var_head, "table=\"/[/*/N\\N/]\"/ "))!=NULL){
tabs(var_head2, out);
} else if((var_head2 = parsestr2(&strct, var_head, "exchange=\"/[/*/N\\N/]\"/ "))!=NULL){
reg_ticket(var_head2, 0);
} else if((var_head2 = parsestr2(&strct, var_head, "ticket=\"/[/*/N\\N/]\"/ "))!=NULL){
reg_ticket(var_head2, 1);
} else if((var_head2 = parsestr2(&strct, var_head, "chtbl_stat=\"/[/*/]\"/ "))!=NULL){
change_tbl_stat(var_head2);
} else if((var_head2 = parsestr2(&strct, var_head, "wr_shell=\"/[/*/N\\N/]\"/ "))!=NULL){
wr_shell(var_head2, 0);//0->write_system, not cat!!
} else if((var_head2 = parsestr2(&strct, var_head, "InIt/ "))!=NULL){ //<!--#include InIt -->
ReadConfiguration();
} else if((var_head2 = parsestr2(&strct, var_head, "write_par=\"/[/*/N\\N/]\"/ "))!=NULL){ //<!--#include write_par="par:value" -->
// write_par par:value this is used in copy_CGI.c - the same code!
char *tmp, *tmp2;
long long size;
tmp2 = w_strtok(&var_head2, ':');
if(tmp2 && *tmp2 && *var_head2){
tmp = get_var(&size, tmp2);
if(tmp && size){
strncpy_(tmp, var_head2, size-1);
// tmp[size-1] = '\0';
}
// if(tmp2 != var_head2) *(var_head2-1) = ':';
}
}//need some end hier (else)
}while(var_head2 && (var_head = strct.end)); //end of parsing string
}
//free all parameter marked as "temp"
void free_par_tmp(void){
struct cfg_parse1 *ptr = cfg1;
while(ptr){
if(ptr->type == CFG_TMP){
ptr->size = 0;
#ifdef DEBUG
printf("-->clear: %s\n", ptr->web_name);
#endif
}
ptr = ptr->next;
}
}