-
Notifications
You must be signed in to change notification settings - Fork 32
/
cfg_param.c
1446 lines (1281 loc) · 45.2 KB
/
cfg_param.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) 2004-2008 Christos Tsantilas
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*/
#include "common.h"
#include "c-icap.h"
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include "service.h"
#include "debug.h"
#include "module.h"
#include "filetype.h"
#include "cfg_param.h"
#include "commands.h"
#include "encoding.h"
#include "acl.h"
#include "txtTemplate.h"
#include "proc_mutex.h"
#include "port.h"
#include "registry.h"
#include "shared_mem.h"
#ifdef USE_OPENSSL
#include "net_io_ssl.h"
#endif
#define MAX_INCLUDE_LEVEL 5
#define LINESIZE 8192
#define MAX_DIRECTIVE_SIZE 80
#define MAX_ARGS 50
int ARGC;
char **ARGV;
struct ci_server_conf CI_CONF = {
NULL,
#ifdef _WIN32
"c:\\TEMP", /*TMPDIR*/ "c:\\TEMP\\c-icap.pid", /*PIDFILE*/ "\\\\.\\pipe\\c-icap", /*COMMANDS_SOCKET; */
#else
"/var/tmp/", /*TMPDIR*/ "/var/run/c-icap/c-icap.pid", /*PIDFILE*/ "/var/run/c-icap/c-icap.ctl", /*COMMANDS_SOCKET; */
#endif
NULL, /* RUN_USER */
NULL, /* RUN_GROUP */
#ifdef _WIN32
CI_CONFDIR "\\c-icap.conf", /*cfg_file */
CI_CONFDIR "\\c-icap.magic", /*magics_file */
#else
CI_CONFDIR "/c-icap.conf", /*cfg_file */
CI_CONFDIR "/c-icap.magic", /*magics_file */
#endif
NULL, /*MAGIC_DB */
CI_SERVDIR, /*SERVICES_DIR */
CI_MODSDIR, /*MODULES_DIR */
NULL, /*SERVER_ADMIN*/
NULL, /*SERVER_NAME*/
5, /*START_SERVERS*/
10, /*MAX_SERVERS*/
30, /*THREADS_PER_CHILD*/
30, /*MIN_SPARE_THREADS*/
60 /*MAX_SPARE_THREADS*/
#ifdef USE_OPENSSL
,
0 /*TLS_ENABLED, set by TLSPort*/
#endif
};
int TIMEOUT = 300;
int KEEPALIVE_TIMEOUT = 15;
int MAX_KEEPALIVE_REQUESTS = 100;
int MAX_SECS_TO_LINGER = 5;
int MAX_REQUESTS_BEFORE_REALLOCATE_MEM = 100;
int MAX_REQUESTS_PER_CHILD = 0;
int DAEMON_MODE = 1;
int VERSION_MODE = 0;
int HELP_MODE = 0;
int DebugLevelSetFromCmd = 0;
const char *DEFAULT_SERVICE = NULL; /*Default service if not defined in ICAP URI*/
int PIPELINING = 1;
int CHECK_FOR_BUGGY_CLIENT = 0;
int ALLOW204_AS_200OK_ZERO_ENCAPS = 0;
int FAKE_ALLOW204 = 1;
int UMASK = 0;
int SINGLE_SERVER = 0;
#ifdef HAVE_BROTLI
int BROTLI_QUALITY = -1;
int BROTLI_MAX_INPUT_BLOCK = -1;
int BROTLI_WINDOW = -1;
#endif
#ifdef HAVE_ZLIB
int ZLIB_WINDOW_SIZE = -1;
int ZLIB_MEMLEVEL = -1;
#endif
#ifdef HAVE_ZSTD
int ZSTD_LEVEL = -1;
#endif
extern char *SERVER_LOG_FILE;
extern char *ACCESS_LOG_FILE;
extern char *ACCESS_LOG_FORMAT;
/*extern char *LOGS_DIR;*/
extern access_control_module_t **used_access_controllers;
extern char *REMOTE_PROXY_USER_HEADER;
extern int ALLOW_REMOTE_PROXY_USERS;
extern int REMOTE_PROXY_USER_HEADER_ENCODED;
#ifdef USE_OPENSSL
char *TLS_PASSPHRASE = NULL;
#endif
/*Functions declaration */
int parse_file(const char *conf_file);
/*config table functions*/
int cfg_load_magicfile(const char *directive, const char **argv, void *setdata);
int cfg_load_service(const char *directive, const char **argv, void *setdata);
int cfg_service_alias(const char *directive, const char **argv, void *setdata);
int cfg_load_module(const char *directive, const char **argv, void *setdata);
int cfg_set_logformat(const char *directive, const char **argv, void *setdata);
int cfg_set_logger(const char *directive, const char **argv, void *setdata);
int cfg_set_accesslog(const char *directive, const char **argv, void *setdata);
int cfg_set_debug_level(const char *directive, const char **argv, void *setdata);
int cfg_set_debug_stdout(const char *directive, const char **argv, void *setdata);
int cfg_set_body_maxmem(const char *directive, const char **argv, void *setdata);
int cfg_set_tmp_dir(const char *directive, const char **argv, void *setdata);
int cfg_set_acl_controllers(const char *directive, const char **argv, void *setdata);
int cfg_set_auth_method(const char *directive, const char **argv, void *setdata);
int cfg_include_config_file(const char *directive, const char **argv, void *setdata);
int cfg_group_source_by_group(const char *directive, const char **argv, void *setdata);
int cfg_group_source_by_user(const char *directive, const char **argv, void *setdata);
int cfg_shared_mem_scheme(const char *directive, const char **argv, void *setdata);
int cfg_proc_lock_scheme(const char *directive, const char **argv, void *setdata);
int cfg_set_port(const char *directive, const char **argv, void *setdata);
int cfg_set_template_dir(const char *directive, const char **argv, void *setdata);
int cfg_set_template_default_lang(const char *directive, const char **argv, void *setdata);
int cfg_set_template_reload_time(const char *directive, const char **argv, void *setdata);
int cfg_set_template_cache_size(const char *directive, const char **argv, void *setdata);
int cfg_set_template_membuf_size(const char *directive, const char **argv, void *setdata);
/*The following 2 functions defined in access.c file*/
int cfg_acl_add(const char *directive, const char **argv, void *setdata);
int cfg_default_acl_access(const char *directive, const char **argv, void *setdata);
/****/
struct sub_table {
const char *name;
int type;
struct ci_conf_entry *conf_table;
};
static struct ci_conf_entry conf_variables[] = {
// {"ListenAddress", &CI_CONF.ADDRESS, intl_cfg_set_str, NULL},
{"PidFile", &CI_CONF.PIDFILE, intl_cfg_set_str, NULL},
{"CommandsSocket", &CI_CONF.COMMANDS_SOCKET, intl_cfg_set_str, NULL},
{"Timeout", (void *) (&TIMEOUT), intl_cfg_set_int, NULL},
{"KeepAlive", NULL, NULL, NULL},
{"MaxKeepAliveRequests", &MAX_KEEPALIVE_REQUESTS, intl_cfg_set_int, NULL},
{"KeepAliveTimeout", &KEEPALIVE_TIMEOUT, intl_cfg_set_int, NULL},
{"StartServers", &CI_CONF.START_SERVERS, intl_cfg_set_int, NULL},
{"MaxServers", &CI_CONF.MAX_SERVERS, intl_cfg_set_int, NULL},
{"MinSpareThreads", &CI_CONF.MIN_SPARE_THREADS, intl_cfg_set_int, NULL},
{"MaxSpareThreads", &CI_CONF.MAX_SPARE_THREADS, intl_cfg_set_int, NULL},
{"ThreadsPerChild", &CI_CONF.THREADS_PER_CHILD, intl_cfg_set_int, NULL},
{"MaxRequestsPerChild", &MAX_REQUESTS_PER_CHILD, intl_cfg_set_int, NULL},
{"MaxRequestsReallocateMem", &MAX_REQUESTS_BEFORE_REALLOCATE_MEM, intl_cfg_set_int, NULL},
{"Port", &CI_CONF.PORTS, cfg_set_port, NULL},
#ifdef USE_OPENSSL
{"TlsPort", &CI_CONF.PORTS, cfg_set_port, NULL},
{"TlsPassphrase", &TLS_PASSPHRASE, intl_cfg_set_str, NULL},
/*The Ssl* alias of Tls* cfg params*/
{"SslPort", &CI_CONF.PORTS, cfg_set_port, NULL},
{"SslPassphrase", &TLS_PASSPHRASE, intl_cfg_set_str, NULL},
#endif
{"HttpPort", &CI_CONF.PORTS, cfg_set_port, NULL},
{"HttpsPort", &CI_CONF.PORTS, cfg_set_port, NULL},
{"User", &CI_CONF.RUN_USER, intl_cfg_set_str, NULL},
{"Group", &CI_CONF.RUN_GROUP, intl_cfg_set_str, NULL},
{"Umask", &UMASK, intl_cfg_set_octal, NULL},
{"ServerAdmin", &CI_CONF.SERVER_ADMIN, intl_cfg_set_str, NULL},
{"ServerName", &CI_CONF.SERVER_NAME, intl_cfg_set_str, NULL},
{"LoadMagicFile", NULL, cfg_load_magicfile, NULL},
{"Logger", NULL, cfg_set_logger, NULL},
{"ServerLog", &SERVER_LOG_FILE, intl_cfg_set_str, NULL},
{"AccessLog", NULL, cfg_set_accesslog, NULL},
{"LogFormat", NULL, cfg_set_logformat, NULL},
{"DebugLevel", NULL, cfg_set_debug_level, NULL}, /*Set library's debug level */
{"ServicesDir", &CI_CONF.SERVICES_DIR, intl_cfg_set_str, NULL},
{"ModulesDir", &CI_CONF.MODULES_DIR, intl_cfg_set_str, NULL},
{"Service", NULL, cfg_load_service, NULL},
{"ServiceAlias", NULL, cfg_service_alias, NULL},
{"Module", NULL, cfg_load_module, NULL},
{"TmpDir", NULL, cfg_set_tmp_dir, NULL},
{"MaxMemObject", NULL, cfg_set_body_maxmem, NULL}, /*Set library's body max mem */
{"AclControllers", NULL, cfg_set_acl_controllers, NULL},
{"acl", NULL, cfg_acl_add, NULL},
{"icap_access", NULL, cfg_default_acl_access, NULL},
{"client_access", NULL, cfg_default_acl_access, NULL},
{"AuthMethod", NULL, cfg_set_auth_method, NULL},
{"Include", NULL, cfg_include_config_file, NULL},
{"RemoteProxyUserHeader", &REMOTE_PROXY_USER_HEADER, intl_cfg_set_str, NULL},
{"RemoteProxyUserHeaderEncoded", &REMOTE_PROXY_USER_HEADER_ENCODED, intl_cfg_onoff, NULL},
{"RemoteProxyUsers", &ALLOW_REMOTE_PROXY_USERS, intl_cfg_onoff, NULL},
{"TemplateDir", NULL, cfg_set_template_dir, NULL},
{"TemplateDefaultLanguage", NULL, cfg_set_template_default_lang, NULL},
{"TemplateReloadTime", NULL, cfg_set_template_reload_time, NULL},
{"TemplateCacheSize", NULL, cfg_set_template_cache_size, NULL},
{"TemplateMemBufSize", NULL, cfg_set_template_membuf_size, NULL},
{"GroupSourceByGroup", NULL, cfg_group_source_by_group, NULL},
{"GroupSourceByUser", NULL, cfg_group_source_by_user, NULL},
{"InterProcessSharedMemScheme", NULL, cfg_shared_mem_scheme, NULL},
{"InterProcessLockingScheme", NULL, cfg_proc_lock_scheme, NULL},
{"DefaultService", &DEFAULT_SERVICE, intl_cfg_set_str, NULL},
{"Pipelining", &PIPELINING, intl_cfg_onoff, NULL},
{"SupportBuggyClients", &CHECK_FOR_BUGGY_CLIENT, intl_cfg_onoff, NULL},
{"Allow204As200okZeroEncaps", &ALLOW204_AS_200OK_ZERO_ENCAPS, intl_cfg_enable, NULL},
{"FakeAllow204", &FAKE_ALLOW204, intl_cfg_onoff, NULL},
#ifdef HAVE_BROTLI
{"BrotliQuality", CI_CFG_INT_RANGE(BROTLI_QUALITY, 0, 11), intl_cfg_set_int_range, NULL},
{"BrotliMaxInputBlock", CI_CFG_INT_RANGE(BROTLI_MAX_INPUT_BLOCK, 16, 24), intl_cfg_set_int_range, NULL},
{"BrotliWindowSize", CI_CFG_INT_RANGE(BROTLI_WINDOW, 10, 24), intl_cfg_set_int_range, NULL},
#endif
#ifdef HAVE_ZLIB
{"ZlibWindowSize", CI_CFG_INT_RANGE(ZLIB_WINDOW_SIZE, 1, 15), intl_cfg_set_int_range, NULL},
{"ZlibMemLevel", CI_CFG_INT_RANGE(ZLIB_MEMLEVEL, 1, 9), intl_cfg_set_int_range, NULL},
#endif
#ifdef HAVE_ZSTD
{"ZstdCompressionLevel", CI_CFG_INT_RANGE(ZSTD_LEVEL, 0, 22), intl_cfg_set_int_range, NULL},
#endif
{NULL, NULL, NULL, NULL}
};
#define STEPSIZE 10
static struct sub_table *extra_conf_tables = NULL;
int conf_tables_list_size = 0;
int conf_tables_num = 0;
struct ci_conf_entry *search_conf_table(struct ci_conf_entry *table, char *varname)
{
int i;
for (i = 0; table[i].name != NULL; i++) {
if (0 == strcmp(varname, table[i].name))
return &table[i];
}
return NULL;
}
void init_conf_tables()
{
if ((extra_conf_tables =
malloc(STEPSIZE * sizeof(struct sub_table))) == NULL) {
ci_debug_printf(1, "Error allocating memory...\n");
return;
}
conf_tables_list_size = STEPSIZE;
}
void reset_conf_tables()
{
conf_tables_num = 0;
}
int register_conf_table(const char *name, struct ci_conf_entry *table, int type)
{
struct sub_table *new;
int i, insert_pos;
if (!extra_conf_tables)
return 0;
insert_pos = -1;
for (i = 0; insert_pos < 0 && i < conf_tables_num; i++) {
if (extra_conf_tables[i].name && strcmp(name, extra_conf_tables[i].name) == 0) {
ci_debug_printf(1,"Config table :%s already exists!\n", name);
return 0;
} else if (extra_conf_tables[i].name == NULL) { /*empty pos use this one*/
insert_pos = i;
}
}
if (insert_pos < 0) { /*if not empry pos found add it to the end*/
insert_pos = conf_tables_num;
if (conf_tables_num == conf_tables_list_size) {
/*tables list is full, reallocating space ...... */
if (NULL ==
(new =
realloc(extra_conf_tables, sizeof(struct sub_table)*(conf_tables_list_size + STEPSIZE))))
return 0;
extra_conf_tables = new;
conf_tables_list_size += STEPSIZE;
}
conf_tables_num++;
}
ci_debug_printf(10, "Registering conf table: %s\n", name);
extra_conf_tables[insert_pos].name = name; /*It works. Points to the modules.name. (????) */
extra_conf_tables[insert_pos].type = type;
extra_conf_tables[insert_pos].conf_table = table;
return 1;
}
struct ci_conf_entry *unregister_conf_table(const char *name)
{
int i;
struct ci_conf_entry *table;
if (extra_conf_tables) { /*Not really needed........ */
for (i = 0; i < conf_tables_num; i++) {
if (extra_conf_tables[i].name && strcmp(name, extra_conf_tables[i].name) == 0) {
table = extra_conf_tables[i].conf_table;
extra_conf_tables[i].name = NULL;
extra_conf_tables[i].type = 0;
extra_conf_tables[i].conf_table = NULL;
return table;
}
}
}
ci_debug_printf(1, "Table %s not found!\n", name);
return NULL;
}
struct ci_conf_entry *conf_table_find(char *table)
{
int i;
for (i = 0; i < conf_tables_num; i++) {
if (extra_conf_tables[i].name && strcmp(table, extra_conf_tables[i].name) == 0)
return extra_conf_tables[i].conf_table;
}
return NULL;
}
void print_conf_variables(struct ci_conf_entry *table)
{
int i;
for (i = 0; table[i].name != NULL; i++) {
ci_debug_printf(9, "%s=", table[i].name);
if (!table[i].data) {
ci_debug_printf(9, "\n");
} else if (table[i].action == intl_cfg_set_str) {
if (*(char *) table[i].data) {
ci_debug_printf(9, "%s\n", *(char **) table[i].data);
} else {
ci_debug_printf(9, "\n");
}
} else if (table[i].action == intl_cfg_set_int) {
ci_debug_printf(9, "%d\n", *(int *) table[i].data);
} else if (table[i].action == intl_cfg_set_octal) {
ci_debug_printf(9, "0%.3o\n", *(int *) table[i].data);
} else if (table[i].action == intl_cfg_size_off) {
ci_debug_printf(9, "%" PRINTF_OFF_T "\n",
(CAST_OFF_T) *(ci_off_t *) table[i].data);
} else if (table[i].action == intl_cfg_size_long) {
ci_debug_printf(9, "%ld\n", *(long *) table[i].data);
} else if (table[i].action == intl_cfg_onoff) {
ci_debug_printf(9, "%d\n", *(int *) table[i].data);
} else if (table[i].action == intl_cfg_enable) {
ci_debug_printf(9, "%d\n", *(int *) table[i].data);
} else if (table[i].action == intl_cfg_disable) {
ci_debug_printf(9, "%d\n", *(int *) table[i].data);
} else if (table[i].data) {
ci_debug_printf(9, "%p\n", table[i].data);
}
}
}
int print_variables()
{
int i;
ci_debug_printf(9, "\n\nPrinting variables\n");
print_conf_variables(conf_variables);
if (!extra_conf_tables) /*Not really needed........ */
return 1;
for (i = 0; i < conf_tables_num; i++) {
if ( extra_conf_tables[i].name) {
ci_debug_printf(9, "Printing variables in table %s\n",
extra_conf_tables[i].name);
print_conf_variables(extra_conf_tables[i].conf_table);
}
}
return 1;
}
/************************************************************************/
/* Set variables functions */
/*
The following tree functions refered to non constant variables so
the compilers in Win32 have problem to appeared in static arrays
*/
int cfg_set_port(const char *directive, const char **argv, void *setdata)
{
int i;
char *s, *addr, *connect_port;
ci_port_t *pcfg = NULL;
ci_port_t tmpP;
ci_vector_t **port_configs = (ci_vector_t **)setdata;
if (argv == NULL || argv[0] == NULL) {
ci_debug_printf(1, "Missing arguments in %s directive\n", directive);
return 0;
}
if (!*port_configs)
*port_configs = ci_vector_create(2048);
memset(&tmpP, 0, sizeof(ci_port_t));
pcfg = (ci_port_t *)ci_vector_add(*port_configs, (void *)&tmpP, sizeof(ci_port_t));
if (!pcfg) {
ci_debug_printf(1, "Maximum number of configured ports is reached\n");
return 0;
}
pcfg->accept_socket = CI_SOCKET_INVALID;
connect_port = strdup(argv[0]);
if ((s = strrchr(connect_port, ':'))) {
*s = '\0';
addr = connect_port;
if (*addr == '[') {
if (addr[strlen(addr) - 1] != ']') {
ci_debug_printf(1, "Failed to parse listen address: %s\n", addr);
free(connect_port);
return 0;
}
++addr;
addr[strlen(addr) - 1] = '\0';
}
pcfg->address = strdup(addr);
s++;
} else
s = connect_port;
pcfg->port = atoi(s);
free(connect_port);
connect_port = s = NULL;
if (pcfg->port <= 0) {
ci_debug_printf(1, "Failed to parse %s (parsed port number:%d)\n", directive, pcfg->port);
return 0;
}
int isHTTP = (strcmp(directive, "HttpPort") == 0 || strcmp(directive, "HttpsPort") == 0) ? 1 : 0;
pcfg->proto = isHTTP ? CI_PROTO_HTTP : CI_PROTO_ICAP;
if (!argv[1])
return 1;
#ifdef USE_OPENSSL
int isTls = (strcmp(directive, "TlsPort") == 0 || strcmp(directive, "SslPort") == 0 || strcmp(directive, "HttpsPort") == 0) ? 1 : 0;
if (isTls)
CI_CONF.TLS_ENABLED = 1;
pcfg->tls_enabled = isTls;
#endif
for (i = 1; argv[i] != NULL; ++i) {
#ifdef USE_OPENSSL
if (isTls && icap_port_tls_option(argv[i], pcfg, CI_CONFDIR)) {
} else
#endif
{
ci_debug_printf(1, "Unknown %s option: '%s'", directive, argv[i]);
return 0;
}
}
return 1;
}
int cfg_set_debug_level(const char *directive, const char **argv, void *setdata)
{
if (!DebugLevelSetFromCmd)
return intl_cfg_set_int(directive, argv, &CI_DEBUG_LEVEL);
/*else ignore ....*/
return 1;
}
int cfg_set_debug_level_cmd(const char *directive, const char **argv, void *setdata)
{
DebugLevelSetFromCmd = 1;
return intl_cfg_set_int(directive, argv, &CI_DEBUG_LEVEL);
}
int cfg_set_debug_stdout(const char *directive, const char **argv, void *setdata)
{
return intl_cfg_enable(directive, argv, &CI_DEBUG_STDOUT);
}
int cfg_set_body_maxmem(const char *directive, const char **argv, void *setdata)
{
return intl_cfg_size_long(directive, argv, &CI_BODY_MAX_MEM);
}
int cfg_load_service(const char *directive, const char **argv, void *setdata)
{
ci_service_module_t *service = NULL;
if (argv == NULL || argv[0] == NULL || argv[1] == NULL) {
ci_debug_printf(1, "Missing arguments in LoadService directive\n");
return 0;
}
ci_debug_printf(2, "Loading service: %s path %s\n", argv[0], argv[1]);
if (!(service = register_service(argv[1], argv + 2))) {
ci_debug_printf(1, "Error loading service %s\n", argv[1]);
return 0;
}
add_service_alias(argv[0], service->mod_name, NULL);
return 1;
}
int cfg_service_alias(const char *directive, const char **argv, void *setdata)
{
char *service_args = NULL;
if (argv == NULL || argv[0] == NULL || argv[1] == NULL) {
ci_debug_printf(1, "Missing arguments in ServiceAlias directive\n");
return 0;
}
if ((service_args = strchr(argv[1], '?'))) {
*service_args = '\0';
service_args++;
}
ci_debug_printf(2, "Alias: %s of service %s\n", argv[0], argv[1]);
add_service_alias(argv[0], argv[1], service_args);
return 1;
}
int cfg_load_module(const char *directive, const char **argv, void *setdata)
{
if (argv == NULL || argv[0] == NULL || argv[1] == NULL) {
ci_debug_printf(1, "Missing arguments in LoadModule directive\n");
return 0;
}
ci_debug_printf(2, "Loading service: %s path %s\n", argv[0], argv[1]);
if (!register_module(argv[1], argv[0], argv + 2)) {
ci_debug_printf(1, "Error loading module %s, module path %s\n", argv[1], argv[0]);
return 0;
}
return 1;
}
int cfg_load_magicfile(const char *directive, const char **argv, void *setdata)
{
const char *db_file;
struct ci_magics_db *ndb;
if (argv == NULL || argv[0] == NULL) {
return 0;
}
db_file = argv[0];
if (strcmp(CI_CONF.magics_file, db_file) == 0) {
ci_debug_printf(2, "The db file %s is the same as default. Ignoring...\n", db_file);
return 1;
}
ci_debug_printf(2, "Going to load magic file %s\n", db_file);
ndb = ci_magic_db_load(db_file);
if (!ndb) {
ci_debug_printf(1, "Can not load magic file %s!!!\n", db_file);
return 0;
}
if (!CI_CONF.MAGIC_DB)
CI_CONF.MAGIC_DB = ndb;
return 1;
}
int logformat_add(const char *name, const char *format);
int cfg_set_logformat(const char *directive, const char **argv, void *setdata)
{
if (argv == NULL || argv[0] == NULL || argv[1] == NULL) {
ci_debug_printf(1, "Missing arguments in directive %s\n", directive);
return 0;
}
ci_debug_printf(2, "Adding the logformat %s: %s\n",argv[0],argv[1]);
return logformat_add(argv[0], argv[1]);
}
int file_log_addlogfile(const char *file, const char *format, const char **acls);
int cfg_set_accesslog(const char *directive, const char **argv, void *setdata)
{
const char **acls = NULL;
if (argv == NULL || argv[0] == NULL ) {
ci_debug_printf(1, "Missing arguments in directive %s\n", directive);
return 0;
}
if (argv[1] != NULL && argv[2] !=NULL) {
acls = argv+2;
}
ci_debug_printf(2, "Adding the access logfile %s\n",argv[0]);
return file_log_addlogfile(argv[0], argv[1], acls);
}
void log_add_logger(logger_module_t *logger);
void log_disable_logs();
int cfg_set_logger(const char *directive, const char **argv, void *setdata)
{
logger_module_t *logger;
if (argv == NULL || argv[0] == NULL) {
ci_debug_printf(1, "Missing arguments in directive\n");
return 0;
}
int i;
for (i = 0; argv[i] != NULL; ++i) {
if (strcasecmp(argv[i], "none") == 0) {
log_disable_logs();
continue;
}
if (!(logger = find_logger(argv[i]))) {
ci_debug_printf(1, "WARNING: setting '%s': Logger '%s' is not defined\n", directive, argv[i]);
continue;
}
log_add_logger(logger);
ci_debug_printf(2, "Adding logger: %s ...\n", argv[i]);
}
return 1;
}
int cfg_set_tmp_dir(const char *directive, const char **argv, void *setdata)
{
int len;
if (argv == NULL || argv[0] == NULL) {
return 0;
}
cfg_default_value_store(&CI_CONF.TMPDIR, &CI_CONF.TMPDIR, sizeof(char *));
len = strlen(argv[0]);
CI_CONF.TMPDIR = ci_cfg_alloc_mem((len + 2) * sizeof(char));
strncpy(CI_CONF.TMPDIR, argv[0], len + 1);
CI_CONF.TMPDIR[len] = '\0';
#ifdef _WIN32
if (CI_CONF.TMPDIR[len - 1] != '\\') {
CI_CONF.TMPDIR[len] = '\\';
CI_CONF.TMPDIR[len + 1] = '\0';
}
#else
if (CI_CONF.TMPDIR[len - 1] != '/') {
CI_CONF.TMPDIR[len] = '/';
CI_CONF.TMPDIR[len + 1] = '\0';
}
#endif
/*Check if tmpdir exists. If no try to build it , report an error and uses the default... */
CI_TMPDIR = CI_CONF.TMPDIR; /*Sets the library's temporary dir to .... */
ci_debug_printf(2, "Setting parameter: %s=%s\n", directive, argv[0]);
return 1;
}
int cfg_set_acl_controllers(const char *directive, const char **argv, void *setdata)
{
int i, k, argc, ret;
access_control_module_t *acl_mod;
if (argv == NULL || argv[0] == NULL) {
return 0;
}
if (strncasecmp(argv[0], "none", 4) == 0) {
used_access_controllers = NULL;
return 1;
}
for (argc = 0; argv[argc] != NULL; argc++); /*Find the number of acl controllers */
used_access_controllers =
ci_cfg_alloc_mem((argc+1) * sizeof(access_control_module_t *));
k = 0;
ret = 1;
for (i = 0; i < argc; i++) {
if ((acl_mod = find_access_controller(argv[i])) != NULL) {
used_access_controllers[k++] = acl_mod;
} else {
ci_debug_printf(1, "No access controller with name :%s\n",
argv[i]);
ret = 0;
}
}
used_access_controllers[k] = NULL;
return ret;
}
int cfg_set_auth_method(const char *directive, const char **argv, void *setdata)
{
const char *method = NULL;
if (argv == NULL || argv[0] == NULL || argv[1] == NULL) {
return 0;
}
method = argv[0];
if (strncasecmp(argv[1], "none", 4) == 0) {
return set_method_authenticators(method, NULL);
}
return set_method_authenticators(method, (const char **)argv + 1);
}
int cfg_acl_add(const char *directive, const char **argv, void *setdata)
{
const char *acl_name, *acl_type;
int argc, ok;
if (!argv[0] || !argv[1] || !argv[2]) /* at least an argument */
return 0;
acl_name = argv[0];
acl_type = argv[1];
for (argc = 2, ok =1; argv[argc] != NULL && ok; argc++) {
ci_debug_printf(2, "Adding to acl %s the data %s\n", acl_name, argv[argc]);
ok = ci_acl_add_data(acl_name, acl_type, argv[argc]);
}
ci_debug_printf(2, "New ACL with name:%s and ACL Type: %s\n", argv[0], argv[1]);
return ok;
}
int cfg_include_config_file(const char *directive, const char **argv, void *setdata)
{
char path[CI_MAX_PATH];
const char *cfg_file;
if (argv == NULL || argv[0] == NULL) {
return 0;
}
cfg_file = argv[0];
#ifdef _WIN32
if (cfg_file[0] != '\\' && cfg_file[1] != ':') {
#elif defined __CYGWIN__
if (cfg_file[0] != '/' && cfg_file[0] != '\\' && cfg_file[1] != ':') {
#else
if (cfg_file[0] != '/') {
#endif
snprintf(path, CI_MAX_PATH, CI_CONFDIR "/%s", cfg_file);
cfg_file = path;
}
ci_debug_printf(2, "\n*** Going to open config file %s ***\n", cfg_file);
return parse_file(cfg_file);
}
int group_source_add_by_group(const char *table_name);
int group_source_add_by_user(const char *table_name);
int cfg_group_source_by_group(const char *directive, const char **argv, void *setdata)
{
const char *group_table = NULL;
if (argv == NULL || argv[0] == NULL) {
return 0;
}
group_table = argv[0];
return group_source_add_by_group(group_table);
}
int cfg_group_source_by_user(const char *directive, const char **argv, void *setdata)
{
const char *group_table = NULL;
if (argv == NULL || argv[0] == NULL) {
return 0;
}
group_table = argv[0];
return group_source_add_by_user(group_table);
}
int cfg_shared_mem_scheme(const char *directive, const char **argv, void *setdata)
{
if (argv == NULL || argv[0] == NULL) {
return 0;
}
return ci_shared_mem_set_scheme(argv[0]);
}
int cfg_proc_lock_scheme(const char *directive, const char **argv, void *setdata)
{
if (argv == NULL || argv[0] == NULL) {
return 0;
}
return ci_proc_mutex_set_scheme(argv[0]);
}
int cfg_set_template_dir(const char *directive, const char **argv, void *setdata)
{
return intl_cfg_set_str(directive, argv, &TEMPLATE_DIR);
}
int cfg_set_template_default_lang(const char *directive, const char **argv, void *setdata)
{
return intl_cfg_set_str(directive, argv, &TEMPLATE_DEF_LANG);
}
int cfg_set_template_reload_time(const char *directive, const char **argv, void *setdata)
{
return intl_cfg_set_int(directive, argv, &TEMPLATE_RELOAD_TIME);
}
int cfg_set_template_cache_size(const char *directive, const char **argv, void *setdata)
{
return intl_cfg_set_int(directive, argv, &TEMPLATE_CACHE_SIZE);
}
int cfg_set_template_membuf_size(const char *directive, const char **argv, void *setdata)
{
return intl_cfg_set_int(directive, argv, &TEMPLATE_MEMBUF_SIZE);
}
struct keyval {const char *n; const char *v;};
extern struct keyval _CI_CONF_AUTOCONF[];
extern struct keyval _CI_CONF_C_ICAP_CONF[];
int cfg_build_configuration(const char *directive, const char **argv, void *setdata)
{
if (setdata)
*((int *) setdata) = 1;
printf("c-icap version: %s\n\n", VERSION);
int i;
printf("/* autoconf.h */\n");
for (i = 0; _CI_CONF_AUTOCONF[i].n != NULL; i++) {
printf("#define %s %s\n", _CI_CONF_AUTOCONF[i].n, _CI_CONF_AUTOCONF[i].v);
}
printf("\n/* c-icap-conf.h */\n");
for (i = 0; _CI_CONF_C_ICAP_CONF[i].n != NULL; i++) {
printf("#define %s %s\n", _CI_CONF_C_ICAP_CONF[i].n, _CI_CONF_C_ICAP_CONF[i].v);
}
return 1;
}
/**************************************************************************/
/* Parse file functions */
int fread_line(FILE * f_conf, char *line)
{
if (!fgets(line, LINESIZE, f_conf)) {
if (feof(f_conf)) {
line[0] = '\0';
return -1;
} else
return 0;
}
if (strlen(line) >= LINESIZE - 2 && line[LINESIZE - 2] != '\n') { //Size of line > LINESIZE
while (!feof(f_conf)) {
if (fgetc(f_conf) == '\n')
return 1;
}
return 0;
}
return 1;
}
char **split_args(char *args)
{
int len, i = 0, brkt;
char **argv = NULL, *str, *end, *p;
argv = malloc((MAX_ARGS + 1) * sizeof(char *));
end = args;
do {
str = end;
if (*end == '"') {
end++;
str = end;
while (*end != '\0' && *end != '"') {
/*support escaped \" */
if (*end == '\\' && *(end+1) == '"') {
for (p = end; *p != '\0'; p++)
*p = *(p+1);
}
end++;
}
} else {
/*Support arguments in the form arg{a, b...}*/
brkt = 0;
while (*end != '\0' && (!isspace((int)*end) || brkt)) {
if (*end == '{') brkt = 1;
else if (brkt && *end == '}') brkt = 0;
end++;
}
}
len = end - str;
argv[i] = malloc((len + 1) * sizeof(char));
memcpy(argv[i], str, len); /*copy until len or end of string */
argv[i][len] = '\0';
++i;
if (i >= MAX_ARGS)
break;
if (*end == '"')
end++;
while (*end != '\0' && isspace((int)*end))
end++;
} while (*end != '\0');
argv[i] = NULL;
return argv;
}
void free_args(char **argv)
{
int i;
if (argv == NULL)
return;
for (i = 0; argv[i] != NULL; i++) {
free(argv[i]);
argv[i] = NULL;
}
free(argv);
}
void parse_line(char *str, char **table, char **param, char ***argv)
{
char *end, *s, *arg;
*table = NULL;
*param = NULL;
*argv = NULL;
end = str;
while (*end != '\0' && !isspace((int)*end))
end++;
*end = '\0'; /*Mark the end of Variable...... */
end++; /*... and continue.... */
while (*end != '\0' && isspace((int)*end)) /*Find the start of arguments ...... */
end++;
arg = end;
*argv = split_args(arg);
if ((s = strchr(str, '.')) != NULL) {
*table = str;
*s = '\0';
*param = s + 1;
} else {
*table = NULL;
*param = str;
}
}
int process_line(char *orig_line)
{
char *str, *table_name = NULL, *param = NULL, **argv = NULL;
int ret = 1;
struct ci_conf_entry *table;
char line[LINESIZE];
strncpy(line, orig_line, LINESIZE);
line[LINESIZE-1] = '\0';
str = line;
while (*str != '\0' && isspace((int)*str)) str++; /*trim*/
if (*str == '\0' || *str == '#') /*Empty line or comment */
return 1;
parse_line(str, &table_name, ¶m, &argv);
if (!param || !param[0])
return 0;
table = table_name && table_name[0] ? conf_table_find(table_name) : conf_variables;
if (!table) {
ci_debug_printf(1, "Variable %s.%s: configuration table %s not found!\n", table_name, param, table_name);
return 0;
}
ret = ci_cfg_conf_table_configure(table, table_name, param, (const char **)argv);
if (argv)
free_args(argv);
return ret;
}
static int PARSE_LEVEL = 0;
int parse_file(const char *conf_file)
{
FILE *f_conf;
char line[LINESIZE];
int line_count, ret_value;
if (PARSE_LEVEL >= MAX_INCLUDE_LEVEL) {
ci_debug_printf(1, "Include level > %d. I will not parse file:%s\n",
MAX_INCLUDE_LEVEL,
conf_file);
return 0;
}
if ((f_conf = fopen(conf_file, "r")) == NULL) {
//or log_server better........
ci_debug_printf(1, "Can not open configuration file %s\n", conf_file);
return 0;
}
line_count = 0;