/
options.c
5105 lines (4797 loc) · 153 KB
/
options.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
/*
* stunnel TLS offloading and load-balancing proxy
* Copyright (C) 1998-2024 Michal Trojnara <Michal.Trojnara@stunnel.org>
*
* 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.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, see <http://www.gnu.org/licenses>.
*
* Linking stunnel statically or dynamically with other modules is making
* a combined work based on stunnel. Thus, the terms and conditions of
* the GNU General Public License cover the whole combination.
*
* In addition, as a special exception, the copyright holder of stunnel
* gives you permission to combine stunnel with free software programs or
* libraries that are released under the GNU LGPL and with code included
* in the standard release of OpenSSL under the OpenSSL License (or
* modified versions of such code, with unchanged license). You may copy
* and distribute such a system following the terms of the GNU GPL for
* stunnel and the licenses of the other code concerned.
*
* Note that people who make modified versions of stunnel are not obligated
* to grant this special exception for their modified versions; it is their
* choice whether to do so. The GNU General Public License gives permission
* to release a modified version without this exception; this exception
* also makes it possible to release a modified version which carries
* forward this exception.
*/
#include "prototypes.h"
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
#define DEFAULT_CURVES "X25519:P-256:X448:P-521:P-384"
#ifdef SSL_SYSTEM_DEFAULT_CIPHER_LIST /* Red Hat OpenSSL */
#define DEFAULT_CURVES_FIPS "P-256:P-521:P-384"
#else /* standard OpenSSL */
#define DEFAULT_CURVES_FIPS DEFAULT_CURVES
#endif /* Red Hat OpenSSL */
#else /* OpenSSL version < 1.1.1 */
#define DEFAULT_CURVES "prime256v1"
#define DEFAULT_CURVES_FIPS DEFAULT_CURVES
#endif /* OpenSSL version >= 1.1.1 */
#if defined(_WIN32_WCE) && !defined(CONFDIR)
#define CONFDIR "\\stunnel"
#endif
#define CONFLINELEN (16*1024)
#define INVALID_SSL_OPTION ((long unsigned)-1)
typedef enum {
CMD_SET_DEFAULTS, /* set default values */
CMD_SET_COPY, /* duplicate from new_service_options */
CMD_FREE, /* deallocate memory */
CMD_SET_VALUE, /* set a user-specified value */
CMD_INITIALIZE, /* initialize the global options or a section */
CMD_PRINT_DEFAULTS, /* print default values */
CMD_PRINT_HELP /* print help */
} CMD;
NOEXPORT int options_file(char *, CONF_TYPE, SERVICE_OPTIONS **);
NOEXPORT int init_section(int, SERVICE_OPTIONS **);
#ifdef USE_WIN32
struct dirent {
char d_name[MAX_PATH];
};
int scandir(const char *, struct dirent ***,
int (*)(const struct dirent *),
int (*)(const struct dirent **, const struct dirent **));
int alphasort(const struct dirent **, const struct dirent **);
#endif
NOEXPORT const char *parse_global_option(CMD, GLOBAL_OPTIONS *, char *, char *);
NOEXPORT const char *parse_service_option(CMD, SERVICE_OPTIONS **, char *, char *);
#ifndef OPENSSL_NO_TLSEXT
NOEXPORT const char *sni_init(SERVICE_OPTIONS *);
NOEXPORT void sni_free(SERVICE_OPTIONS *);
#endif /* !defined(OPENSSL_NO_TLSEXT) */
#if OPENSSL_VERSION_NUMBER>=0x10100000L
NOEXPORT int str_to_proto_version(const char *);
#else /* OPENSSL_VERSION_NUMBER<0x10100000L */
NOEXPORT char *tls_methods_set(SERVICE_OPTIONS *, const char *);
NOEXPORT char *tls_methods_check(SERVICE_OPTIONS *);
#endif /* OPENSSL_VERSION_NUMBER<0x10100000L */
NOEXPORT const char *parse_debug_level(char *, SERVICE_OPTIONS *);
#ifndef OPENSSL_NO_PSK
NOEXPORT PSK_KEYS *psk_read(char *);
NOEXPORT PSK_KEYS *psk_dup(PSK_KEYS *);
NOEXPORT void psk_free(PSK_KEYS *);
#endif /* !defined(OPENSSL_NO_PSK) */
#if OPENSSL_VERSION_NUMBER>=0x10000000L
NOEXPORT TICKET_KEY *key_read(char *, const char *);
NOEXPORT TICKET_KEY *key_dup(TICKET_KEY *);
NOEXPORT void key_free(TICKET_KEY *);
#endif /* OpenSSL 1.0.0 or later */
typedef struct {
const char *name;
long unsigned value;
} SSL_OPTION;
static const SSL_OPTION ssl_opts[] = {
#ifdef SSL_OP_ALL
{"ALL", SSL_OP_ALL},
#endif
#ifdef SSL_OP_ALLOW_CLIENT_RENEGOTIATION
{"ALLOW_CLIENT_RENEGOTIATION", SSL_OP_ALLOW_CLIENT_RENEGOTIATION},
#endif
#ifdef SSL_OP_ALLOW_NO_DHE_KEX
{"ALLOW_NO_DHE_KEX", SSL_OP_ALLOW_NO_DHE_KEX},
#endif
#ifdef SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
{"ALLOW_UNSAFE_LEGACY_RENEGOTIATION", SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION},
#endif
#ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
{"CIPHER_SERVER_PREFERENCE", SSL_OP_CIPHER_SERVER_PREFERENCE},
#endif
#ifdef SSL_OP_CISCO_ANYCONNECT
{"CISCO_ANYCONNECT", SSL_OP_CISCO_ANYCONNECT},
#endif
#ifdef SSL_OP_CLEANSE_PLAINTEXT
{"CLEANSE_PLAINTEXT", SSL_OP_CLEANSE_PLAINTEXT},
#endif
#ifdef SSL_OP_COOKIE_EXCHANGE
{"COOKIE_EXCHANGE", SSL_OP_COOKIE_EXCHANGE},
#endif
#ifdef SSL_OP_CRYPTOPRO_TLSEXT_BUG
{"CRYPTOPRO_TLSEXT_BUG", SSL_OP_CRYPTOPRO_TLSEXT_BUG},
#endif
#ifdef SSL_OP_DISABLE_TLSEXT_CA_NAMES
{"DISABLE_TLSEXT_CA_NAMES", SSL_OP_DISABLE_TLSEXT_CA_NAMES},
#endif
#ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
{"DONT_INSERT_EMPTY_FRAGMENTS", SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS},
#endif
#ifdef SSL_OP_ENABLE_KTLS
{"ENABLE_KTLS", SSL_OP_ENABLE_KTLS},
#endif
#ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT
{"ENABLE_MIDDLEBOX_COMPAT", SSL_OP_ENABLE_MIDDLEBOX_COMPAT},
#endif
#ifdef SSL_OP_EPHEMERAL_RSA
{"EPHEMERAL_RSA", SSL_OP_EPHEMERAL_RSA},
#endif
#ifdef SSL_OP_IGNORE_UNEXPECTED_EOF
{"IGNORE_UNEXPECTED_EOF", SSL_OP_IGNORE_UNEXPECTED_EOF},
#endif
#ifdef SSL_OP_LEGACY_SERVER_CONNECT
{"LEGACY_SERVER_CONNECT", SSL_OP_LEGACY_SERVER_CONNECT},
#endif
#ifdef SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER
{"MICROSOFT_BIG_SSLV3_BUFFER", SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER},
#endif
#ifdef SSL_OP_MICROSOFT_SESS_ID_BUG
{"MICROSOFT_SESS_ID_BUG", SSL_OP_MICROSOFT_SESS_ID_BUG},
#endif
#ifdef SSL_OP_MSIE_SSLV2_RSA_PADDING
{"MSIE_SSLV2_RSA_PADDING", SSL_OP_MSIE_SSLV2_RSA_PADDING},
#endif
#ifdef SSL_OP_NETSCAPE_CA_DN_BUG
{"NETSCAPE_CA_DN_BUG", SSL_OP_NETSCAPE_CA_DN_BUG},
#endif
#ifdef SSL_OP_NETSCAPE_CHALLENGE_BUG
{"NETSCAPE_CHALLENGE_BUG", SSL_OP_NETSCAPE_CHALLENGE_BUG},
#endif
#ifdef SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG
{"NETSCAPE_DEMO_CIPHER_CHANGE_BUG", SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG},
#endif
#ifdef SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
{"NETSCAPE_REUSE_CIPHER_CHANGE_BUG", SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG},
#endif
#ifdef SSL_OP_NO_ANTI_REPLAY
{"NO_ANTI_REPLAY", SSL_OP_NO_ANTI_REPLAY},
#endif
#ifdef SSL_OP_NO_COMPRESSION
{"NO_COMPRESSION", SSL_OP_NO_COMPRESSION},
#endif
#ifdef SSL_OP_NO_DTLS_MASK
{"NO_DTLS_MASK", SSL_OP_NO_DTLS_MASK},
#endif
#ifdef SSL_OP_NO_DTLSv1
{"NO_DTLSv1", SSL_OP_NO_DTLSv1},
#endif
#ifdef SSL_OP_NO_DTLSv1_2
{"NO_DTLSv1_2", SSL_OP_NO_DTLSv1_2},
#endif
#ifdef SSL_OP_NO_ENCRYPT_THEN_MAC
{"NO_ENCRYPT_THEN_MAC", SSL_OP_NO_ENCRYPT_THEN_MAC},
#endif
#ifdef SSL_OP_NO_EXTENDED_MASTER_SECRET
{"NO_EXTENDED_MASTER_SECRET", SSL_OP_NO_EXTENDED_MASTER_SECRET},
#endif
#ifdef SSL_OP_NO_QUERY_MTU
{"NO_QUERY_MTU", SSL_OP_NO_QUERY_MTU},
#endif
#ifdef SSL_OP_NO_RENEGOTIATION
{"NO_RENEGOTIATION", SSL_OP_NO_RENEGOTIATION},
#endif
#ifdef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
{"NO_SESSION_RESUMPTION_ON_RENEGOTIATION", SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION},
#endif
#ifdef SSL_OP_NO_SSL_MASK
{"NO_SSL_MASK", SSL_OP_NO_SSL_MASK},
#endif
#ifdef SSL_OP_NO_SSLv2
{"NO_SSLv2", SSL_OP_NO_SSLv2},
#endif
#ifdef SSL_OP_NO_SSLv3
{"NO_SSLv3", SSL_OP_NO_SSLv3},
#endif
#ifdef SSL_OP_NO_TICKET
{"NO_TICKET", SSL_OP_NO_TICKET},
#endif
#ifdef SSL_OP_NO_TLSv1
{"NO_TLSv1", SSL_OP_NO_TLSv1},
#endif
#ifdef SSL_OP_NO_TLSv1_1
{"NO_TLSv1_1", SSL_OP_NO_TLSv1_1},
#endif
#ifdef SSL_OP_NO_TLSv1_2
{"NO_TLSv1_2", SSL_OP_NO_TLSv1_2},
#endif
#ifdef SSL_OP_NO_TLSv1_3
{"NO_TLSv1_3", SSL_OP_NO_TLSv1_3},
#endif
#ifdef SSL_OP_PKCS1_CHECK_1
{"PKCS1_CHECK_1", SSL_OP_PKCS1_CHECK_1},
#endif
#ifdef SSL_OP_PKCS1_CHECK_2
{"PKCS1_CHECK_2", SSL_OP_PKCS1_CHECK_2},
#endif
#ifdef SSL_OP_PRIORITIZE_CHACHA
{"PRIORITIZE_CHACHA", SSL_OP_PRIORITIZE_CHACHA},
#endif
#ifdef SSL_OP_SAFARI_ECDHE_ECDSA_BUG
{"SAFARI_ECDHE_ECDSA_BUG", SSL_OP_SAFARI_ECDHE_ECDSA_BUG},
#endif
#ifdef SSL_OP_SINGLE_DH_USE
{"SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE},
#endif
#ifdef SSL_OP_SINGLE_ECDH_USE
{"SINGLE_ECDH_USE", SSL_OP_SINGLE_ECDH_USE},
#endif
#ifdef SSL_OP_SSLEAY_080_CLIENT_DH_BUG
{"SSLEAY_080_CLIENT_DH_BUG", SSL_OP_SSLEAY_080_CLIENT_DH_BUG},
#endif
#ifdef SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG
{"SSLREF2_REUSE_CERT_TYPE_BUG", SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG},
#endif
#ifdef SSL_OP_TLS_BLOCK_PADDING_BUG
{"TLS_BLOCK_PADDING_BUG", SSL_OP_TLS_BLOCK_PADDING_BUG},
#endif
#ifdef SSL_OP_TLS_D5_BUG
{"TLS_D5_BUG", SSL_OP_TLS_D5_BUG},
#endif
#ifdef SSL_OP_TLSEXT_PADDING
{"TLSEXT_PADDING", SSL_OP_TLSEXT_PADDING},
#endif
#ifdef SSL_OP_TLS_ROLLBACK_BUG
{"TLS_ROLLBACK_BUG", SSL_OP_TLS_ROLLBACK_BUG},
#endif
{NULL, 0}
};
NOEXPORT long unsigned parse_ssl_option(char *);
NOEXPORT void print_ssl_options(void);
NOEXPORT SOCK_OPT *socket_options_init(void);
NOEXPORT void socket_option_set_int(SOCK_OPT *, const char *, int, int);
NOEXPORT SOCK_OPT *socket_options_dup(SOCK_OPT *);
NOEXPORT void socket_options_free(SOCK_OPT *);
NOEXPORT int socket_options_print(void);
NOEXPORT char *socket_option_text(VAL_TYPE, OPT_UNION *);
NOEXPORT int socket_option_parse(SOCK_OPT *, char *);
#ifndef OPENSSL_NO_OCSP
NOEXPORT unsigned long parse_ocsp_flag(char *);
#endif /* !defined(OPENSSL_NO_OCSP) */
#ifndef OPENSSL_NO_ENGINE
NOEXPORT void engine_reset_list(void);
NOEXPORT const char *engine_auto(void);
NOEXPORT const char *engine_open(const char *);
NOEXPORT const char *engine_ctrl(const char *, const char *);
NOEXPORT const char *engine_default(const char *);
NOEXPORT const char *engine_init(void);
NOEXPORT ENGINE *engine_get_by_id(const char *);
NOEXPORT ENGINE *engine_get_by_num(const int);
#endif /* !defined(OPENSSL_NO_ENGINE) */
NOEXPORT const char *include_config(char *, SERVICE_OPTIONS **);
NOEXPORT void print_syntax(void);
NOEXPORT void name_list_append(NAME_LIST **, char *);
NOEXPORT void name_list_dup(NAME_LIST **, NAME_LIST *);
NOEXPORT void name_list_free(NAME_LIST *);
#ifndef USE_WIN32
NOEXPORT char **arg_alloc(char *);
NOEXPORT char **arg_dup(char **);
NOEXPORT void arg_free(char **arg);
#endif
static char *configuration_file=NULL;
GLOBAL_OPTIONS global_options;
SERVICE_OPTIONS service_options;
unsigned number_of_sections=0;
static GLOBAL_OPTIONS new_global_options;
static SERVICE_OPTIONS new_service_options;
static const char *option_not_found=
"Specified option name is not valid here";
static const char *stunnel_cipher_list=
"HIGH:!aNULL:!SSLv2:!DH:!kDHEPSK";
static const char *fips_cipher_list=
"FIPS:!DH:!kDHEPSK";
#ifndef OPENSSL_NO_TLS1_3
static const char *stunnel_ciphersuites=
"TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256";
#endif /* TLS 1.3 */
/**************************************** parse commandline parameters */
/* return values:
0 - configuration accepted
1 - error
2 - information printed
*/
int options_cmdline(char *arg1, char *arg2) {
const char *name;
CONF_TYPE type;
#ifdef USE_WIN32
(void)arg2; /* squash the unused parameter warning */
#endif
if(!arg1) {
name=
#ifdef CONFDIR
CONFDIR
#ifdef USE_WIN32
"\\"
#else
"/"
#endif
#endif
"stunnel.conf";
type=CONF_FILE;
} else if(!strcasecmp(arg1, "-help")) {
parse_global_option(CMD_PRINT_HELP, NULL, NULL, NULL);
parse_service_option(CMD_PRINT_HELP, NULL, NULL, NULL);
log_flush(LOG_MODE_INFO);
return 2;
} else if(!strcasecmp(arg1, "-version")) {
parse_global_option(CMD_PRINT_DEFAULTS, NULL, NULL, NULL);
parse_service_option(CMD_PRINT_DEFAULTS, NULL, NULL, NULL);
log_flush(LOG_MODE_INFO);
return 2;
} else if(!strcasecmp(arg1, "-sockets")) {
socket_options_print();
log_flush(LOG_MODE_INFO);
return 2;
} else if(!strcasecmp(arg1, "-options")) {
print_ssl_options();
log_flush(LOG_MODE_INFO);
return 2;
} else
#ifndef USE_WIN32
if(!strcasecmp(arg1, "-fd")) {
if(!arg2) {
s_log(LOG_ERR, "No file descriptor specified");
print_syntax();
return 1;
}
name=arg2;
type=CONF_FD;
} else
#endif
{
name=arg1;
type=CONF_FILE;
}
if(type==CONF_FILE) {
#ifdef HAVE_REALPATH
char *buffer=NULL, *real_path;
#ifdef MAXPATHLEN
/* a workaround for pre-POSIX.1-2008 4.4BSD and Solaris */
buffer=malloc(MAXPATHLEN);
#endif
real_path=realpath(name, buffer);
if(!real_path) {
free(buffer);
s_log(LOG_ERR, "Invalid configuration file name \"%s\"", name);
ioerror("realpath");
return 1;
}
configuration_file=str_dup(real_path);
free(real_path);
#else
configuration_file=str_dup(name);
#endif
#ifndef USE_WIN32
} else if(type==CONF_FD) {
configuration_file=str_dup(name);
#endif
}
return options_parse(type);
}
/**************************************** parse configuration file */
int options_parse(CONF_TYPE type) {
SERVICE_OPTIONS *section;
options_defaults();
section=&new_service_options;
/* options_file() is a recursive function, so the last section of the
* configuration file section needs to be initialized separately */
if(options_file(configuration_file, type, §ion) ||
init_section(1, §ion)) {
s_log(LOG_ERR, "Configuration failed");
options_free(0); /* free the new options */
return 1;
}
s_log(LOG_NOTICE, "Configuration successful");
return 0;
}
NOEXPORT int options_file(char *path, CONF_TYPE type,
SERVICE_OPTIONS **section_ptr) {
DISK_FILE *df;
char line_text[CONFLINELEN];
const char *errstr;
char config_line[CONFLINELEN], *config_opt, *config_arg;
int i, line_number=0;
const u_char utf8_bom[] = {0xef, 0xbb, 0xbf};
#ifndef USE_WIN32
int fd;
char *tmp_str;
#endif
s_log(LOG_NOTICE, "Reading configuration from %s %s",
type==CONF_FD ? "descriptor" : "file", path);
#ifndef USE_WIN32
if(type==CONF_FD) { /* file descriptor */
fd=(int)strtol(path, &tmp_str, 10);
if(tmp_str==path || *tmp_str) { /* not a number */
s_log(LOG_ERR, "Invalid file descriptor number");
print_syntax();
return 1;
}
df=file_fdopen(fd, FILE_MODE_READ);
} else
#endif
df=file_open(path, FILE_MODE_READ);
if(!df) {
s_log(LOG_ERR, "Cannot open configuration file");
if(type!=CONF_RELOAD)
print_syntax();
return 1;
}
while(file_getline(df, line_text, CONFLINELEN)>=0) {
memcpy(config_line, line_text, CONFLINELEN);
++line_number;
config_opt=config_line;
if(line_number==1) {
if(!memcmp(config_opt, utf8_bom, sizeof utf8_bom)) {
s_log(LOG_NOTICE, "UTF-8 byte order mark detected");
config_opt+=sizeof utf8_bom;
} else {
s_log(LOG_NOTICE, "UTF-8 byte order mark not detected");
}
}
while(isspace((unsigned char)*config_opt))
++config_opt; /* remove initial whitespaces */
for(i=(int)strlen(config_opt)-1; i>=0 && isspace((unsigned char)config_opt[i]); --i)
config_opt[i]='\0'; /* remove trailing whitespaces */
if(config_opt[0]=='\0' || config_opt[0]=='#' || config_opt[0]==';') /* empty or comment */
continue;
if(config_opt[0]=='[' && config_opt[strlen(config_opt)-1]==']') { /* new section */
if(init_section(0, section_ptr)) {
file_close(df);
return 1;
}
/* append a new SERVICE_OPTIONS structure to the list */
{
SERVICE_OPTIONS *new_section;
new_section=str_alloc_detached(sizeof(SERVICE_OPTIONS));
new_section->next=NULL;
(*section_ptr)->next=new_section;
*section_ptr=new_section;
}
/* initialize the newly allocated section */
++config_opt;
config_opt[strlen(config_opt)-1]='\0';
(*section_ptr)->servname=str_dup_detached(config_opt);
(*section_ptr)->session=NULL;
parse_service_option(CMD_SET_COPY, section_ptr, NULL, NULL);
continue;
}
config_arg=strchr(config_line, '=');
if(!config_arg) {
s_log(LOG_ERR, "%s:%d: \"%s\": No '=' found",
path, line_number, line_text);
file_close(df);
return 1;
}
*config_arg++='\0'; /* split into option name and argument value */
for(i=(int)strlen(config_opt)-1; i>=0 && isspace((unsigned char)config_opt[i]); --i)
config_opt[i]='\0'; /* remove trailing whitespaces */
while(isspace((unsigned char)*config_arg))
++config_arg; /* remove initial whitespaces */
errstr=option_not_found;
/* try global options first (e.g. for 'debug') */
if(!new_service_options.next)
errstr=parse_global_option(CMD_SET_VALUE, &new_global_options, config_opt, config_arg);
if(errstr==option_not_found)
errstr=parse_service_option(CMD_SET_VALUE, section_ptr, config_opt, config_arg);
if(errstr) {
s_log(LOG_ERR, "%s:%d: \"%s\": %s",
path, line_number, line_text, errstr);
file_close(df);
return 1;
}
}
file_close(df);
return 0;
}
NOEXPORT int init_section(int eof, SERVICE_OPTIONS **section_ptr) {
const char *errstr;
#ifndef USE_WIN32
(*section_ptr)->option.log_stderr=new_global_options.option.log_stderr;
#endif /* USE_WIN32 */
if(*section_ptr==&new_service_options) {
/* end of global options or inetd mode -> initialize globals */
errstr=parse_global_option(CMD_INITIALIZE, &new_global_options, NULL, NULL);
if(errstr) {
s_log(LOG_ERR, "Global options: %s", errstr);
return 1;
}
}
if(*section_ptr!=&new_service_options || eof) {
/* end service section or inetd mode -> initialize service */
errstr=parse_service_option(CMD_INITIALIZE, section_ptr, NULL, NULL);
if(errstr) {
if(*section_ptr==&new_service_options)
s_log(LOG_ERR, "Inetd mode: %s", errstr);
else
s_log(LOG_ERR, "Service [%s]: %s",
(*section_ptr)->servname, errstr);
return 1;
}
}
return 0;
}
#ifdef USE_WIN32
int scandir(const char *dirp, struct dirent ***namelist,
int (*filter)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **)) {
WIN32_FIND_DATA data;
HANDLE h;
unsigned num=0, allocated=0;
LPTSTR path, pattern;
char *name;
DWORD saved_errno;
(void)filter; /* squash the unused parameter warning */
(void)compar; /* squash the unused parameter warning */
path=str2tstr(dirp);
pattern=str_tprintf(TEXT("%s\\*"), path);
str_free(path);
h=FindFirstFile(pattern, &data);
saved_errno=GetLastError();
str_free(pattern);
SetLastError(saved_errno);
if(h==INVALID_HANDLE_VALUE)
return -1;
*namelist=NULL;
do {
if(num>=allocated) {
allocated+=16;
*namelist=realloc(*namelist, allocated*sizeof(**namelist));
}
(*namelist)[num]=malloc(sizeof(struct dirent));
if(!(*namelist)[num])
return -1;
name=tstr2str(data.cFileName);
strncpy((*namelist)[num]->d_name, name, MAX_PATH-1);
(*namelist)[num]->d_name[MAX_PATH-1]='\0';
str_free(name);
++num;
} while(FindNextFile(h, &data));
FindClose(h);
return (int)num;
}
int alphasort(const struct dirent **a, const struct dirent **b) {
(void)a; /* squash the unused parameter warning */
(void)b; /* squash the unused parameter warning */
/* most Windows filesystem return sorted data */
return 0;
}
#endif
void options_defaults() {
SERVICE_OPTIONS *service;
/* initialize globals *before* opening the config file */
memset(&new_global_options, 0, sizeof(GLOBAL_OPTIONS));
memset(&new_service_options, 0, sizeof(SERVICE_OPTIONS));
new_service_options.next=NULL;
parse_global_option(CMD_SET_DEFAULTS, &new_global_options, NULL, NULL);
service=&new_service_options;
parse_service_option(CMD_SET_DEFAULTS, &service, NULL, NULL);
}
void options_apply() { /* apply default/validated configuration */
unsigned num=0;
SERVICE_OPTIONS *section;
CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_SECTIONS]);
memcpy(&global_options, &new_global_options, sizeof(GLOBAL_OPTIONS));
memset(&new_global_options, 0, sizeof(GLOBAL_OPTIONS));
/* service_options are used for inetd mode and to enumerate services */
for(section=new_service_options.next; section; section=section->next)
section->section_number=num++;
memcpy(&service_options, &new_service_options, sizeof(SERVICE_OPTIONS));
memset(&new_service_options, 0, sizeof(SERVICE_OPTIONS));
number_of_sections=num;
CRYPTO_THREAD_unlock(stunnel_locks[LOCK_SECTIONS]);
}
void options_free(int current) {
GLOBAL_OPTIONS *global=current?&global_options:&new_global_options;
SERVICE_OPTIONS *service=current?&service_options:&new_service_options;
parse_global_option(CMD_FREE, global, NULL, NULL);
CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_SECTIONS]);
while(service) {
SERVICE_OPTIONS *tmp=service;
service=service->next;
tmp->next=NULL;
service_free(tmp);
}
CRYPTO_THREAD_unlock(stunnel_locks[LOCK_SECTIONS]);
}
void service_up_ref(SERVICE_OPTIONS *section) {
#ifdef USE_OS_THREADS
int ref;
CRYPTO_atomic_add(§ion->ref, 1, &ref, stunnel_locks[LOCK_REF]);
#else
++(section->ref);
#endif
}
void service_free(SERVICE_OPTIONS *section) {
int ref;
#ifdef USE_OS_THREADS
CRYPTO_atomic_add(§ion->ref, -1, &ref, stunnel_locks[LOCK_REF]);
#else
ref=--(section->ref);
#endif
if(ref<0)
fatal("Negative section reference counter");
if(ref==0) {
parse_service_option(CMD_FREE, §ion, NULL, NULL);
}
}
/**************************************** global options */
NOEXPORT const char *parse_global_option(CMD cmd, GLOBAL_OPTIONS *options, char *opt, char *arg) {
void *tmp;
if(cmd==CMD_PRINT_DEFAULTS || cmd==CMD_PRINT_HELP) {
s_log(LOG_NOTICE, " ");
s_log(LOG_NOTICE, "Global options:");
}
/* chroot */
#ifdef HAVE_CHROOT
switch(cmd) {
case CMD_SET_DEFAULTS:
options->chroot_dir=NULL;
break;
case CMD_SET_COPY: /* not used for global options */
break;
case CMD_FREE:
tmp=options->chroot_dir;
options->chroot_dir=NULL;
str_free(tmp);
break;
case CMD_SET_VALUE:
if(strcasecmp(opt, "chroot"))
break;
options->chroot_dir=str_dup(arg);
return NULL; /* OK */
case CMD_INITIALIZE:
break;
case CMD_PRINT_DEFAULTS:
break;
case CMD_PRINT_HELP:
s_log(LOG_NOTICE, "%-22s = directory to chroot stunnel process", "chroot");
break;
}
#endif /* HAVE_CHROOT */
/* compression */
#ifndef OPENSSL_NO_COMP
switch(cmd) {
case CMD_SET_DEFAULTS:
options->compression=COMP_NONE;
break;
case CMD_SET_COPY: /* not used for global options */
break;
case CMD_FREE:
break;
case CMD_SET_VALUE:
if(strcasecmp(opt, "compression"))
break;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
/* only allow compression with OpenSSL 0.9.8 or later
* with OpenSSL #1468 zlib memory leak fixed */
if(OpenSSL_version_num()<0x00908051L) /* 0.9.8e-beta1 */
return "Compression unsupported due to a memory leak";
#endif /* OpenSSL version < 1.1.0 */
if(!strcasecmp(arg, "deflate"))
options->compression=COMP_DEFLATE;
else if(!strcasecmp(arg, "zlib"))
options->compression=COMP_ZLIB;
else
return "Specified compression type is not available";
return NULL; /* OK */
case CMD_INITIALIZE:
break;
case CMD_PRINT_DEFAULTS:
break;
case CMD_PRINT_HELP:
s_log(LOG_NOTICE, "%-22s = compression type",
"compression");
break;
}
#endif /* !defined(OPENSSL_NO_COMP) */
/* EGD */
switch(cmd) {
case CMD_SET_DEFAULTS:
#ifdef EGD_SOCKET
options->egd_sock=EGD_SOCKET;
#else
options->egd_sock=NULL;
#endif
break;
case CMD_SET_COPY: /* not used for global options */
break;
case CMD_FREE:
tmp=options->egd_sock;
options->egd_sock=NULL;
str_free(tmp);
break;
case CMD_SET_VALUE:
if(strcasecmp(opt, "EGD"))
break;
options->egd_sock=str_dup(arg);
return NULL; /* OK */
case CMD_INITIALIZE:
break;
case CMD_PRINT_DEFAULTS:
#ifdef EGD_SOCKET
s_log(LOG_NOTICE, "%-22s = %s", "EGD", EGD_SOCKET);
#endif
break;
case CMD_PRINT_HELP:
s_log(LOG_NOTICE, "%-22s = path to Entropy Gathering Daemon socket", "EGD");
break;
}
#ifndef OPENSSL_NO_ENGINE
/* engine */
switch(cmd) {
case CMD_SET_DEFAULTS:
engine_reset_list();
break;
case CMD_SET_COPY: /* not used for global options */
break;
case CMD_FREE:
/* FIXME: investigate if we can free it */
break;
case CMD_SET_VALUE:
if(strcasecmp(opt, "engine"))
break;
if(!strcasecmp(arg, "auto"))
return engine_auto();
else
return engine_open(arg);
case CMD_INITIALIZE:
engine_init();
break;
case CMD_PRINT_DEFAULTS:
break;
case CMD_PRINT_HELP:
s_log(LOG_NOTICE, "%-22s = auto|engine_id",
"engine");
break;
}
/* engineCtrl */
switch(cmd) {
case CMD_SET_DEFAULTS:
break;
case CMD_SET_COPY: /* not used for global options */
break;
case CMD_FREE:
break;
case CMD_SET_VALUE:
if(strcasecmp(opt, "engineCtrl"))
break;
{
char *tmp_str=strchr(arg, ':');
if(tmp_str)
*tmp_str++='\0';
return engine_ctrl(arg, tmp_str);
}
case CMD_INITIALIZE:
break;
case CMD_PRINT_DEFAULTS:
break;
case CMD_PRINT_HELP:
s_log(LOG_NOTICE, "%-22s = cmd[:arg]",
"engineCtrl");
break;
}
/* engineDefault */
switch(cmd) {
case CMD_SET_DEFAULTS:
break;
case CMD_SET_COPY: /* not used for global options */
break;
case CMD_FREE:
break;
case CMD_SET_VALUE:
if(strcasecmp(opt, "engineDefault"))
break;
return engine_default(arg);
case CMD_INITIALIZE:
break;
case CMD_PRINT_DEFAULTS:
break;
case CMD_PRINT_HELP:
s_log(LOG_NOTICE, "%-22s = TASK_LIST",
"engineDefault");
break;
}
#endif /* !defined(OPENSSL_NO_ENGINE) */
/* fips */
switch(cmd) {
case CMD_SET_DEFAULTS:
#ifdef USE_FIPS
options->option.fips=fips_default()?1:0;
#endif /* USE_FIPS */
break;
case CMD_SET_COPY: /* not used for global options */
break;
case CMD_FREE:
break;
case CMD_SET_VALUE:
if(strcasecmp(opt, "fips"))
break;
if(!strcasecmp(arg, "yes")) {
#ifdef USE_FIPS
options->option.fips=1;
#else
return "FIPS support is not available";
#endif /* USE_FIPS */
} else if(!strcasecmp(arg, "no")) {
#ifdef USE_FIPS
if(fips_default())
return "Failed to override system-wide FIPS mode";
options->option.fips=0;
#endif /* USE_FIPS */
} else {
return "The argument needs to be either 'yes' or 'no'";
}
return NULL; /* OK */
case CMD_INITIALIZE:
break;
case CMD_PRINT_DEFAULTS:
#ifdef USE_FIPS
if(fips_available())
s_log(LOG_NOTICE, "%-22s = %s", "fips", fips_default()?"yes":"no");
#endif /* USE_FIPS */
break;
case CMD_PRINT_HELP:
if(fips_available())
s_log(LOG_NOTICE, "%-22s = yes|no FIPS 140-2 mode",
"fips");
break;
}
/* foreground */
#ifndef USE_WIN32
switch(cmd) {
case CMD_SET_DEFAULTS:
options->option.foreground=0;
options->option.log_stderr=0;
break;
case CMD_SET_COPY: /* not used for global options */
break;
case CMD_FREE:
break;
case CMD_SET_VALUE:
if(strcasecmp(opt, "foreground"))
break;
if(!strcasecmp(arg, "yes")) {
options->option.foreground=1;
options->option.log_stderr=1;
} else if(!strcasecmp(arg, "quiet")) {
options->option.foreground=1;
options->option.log_stderr=0;
} else if(!strcasecmp(arg, "no")) {
options->option.foreground=0;
options->option.log_stderr=0;
} else
return "The argument needs to be either 'yes', 'quiet' or 'no'";
return NULL; /* OK */
case CMD_INITIALIZE:
break;
case CMD_PRINT_DEFAULTS:
break;
case CMD_PRINT_HELP:
s_log(LOG_NOTICE, "%-22s = yes|quiet|no foreground mode (don't fork, log to stderr)",
"foreground");
break;
}
#else
switch(cmd) {
case CMD_SET_DEFAULTS:
break;
case CMD_SET_COPY: /* not used for global options */
break;
case CMD_FREE:
break;
case CMD_SET_VALUE:
if(strcasecmp(opt, "foreground"))
break;
if(!strcasecmp(arg, "yes")) {
/* ignore */
} else if(!strcasecmp(arg, "quiet")) {
/* ignore */
} else if(!strcasecmp(arg, "no")) {
return "The argument needs to be 'yes' or 'quiet'";
} else
return "The argument needs to be 'yes' or 'quiet'";
return NULL; /* OK */
case CMD_INITIALIZE:
break;