This repository has been archived by the owner on Jan 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.c
2420 lines (2311 loc) · 88.8 KB
/
config.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
/*
* Pound - the reverse-proxy load-balancer
* Copyright (C) 2002-2010 Apsis GmbH
*
* This file is part of Pound.
*
* Pound 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 3 of the License, or
* (at your option) any later version.
*
* Pound 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/>.
*
* Contact information:
* Apsis GmbH
* P.O.Box
* 8707 Uetikon am See
* Switzerland
* EMail: [email protected]
*/
#ifndef MISS_FACILITYNAMES
#define SYSLOG_NAMES 1
#endif
#include "pound.h"
#include <openssl/x509v3.h>
#ifdef MISS_FACILITYNAMES
/* This is lifted verbatim from the Linux sys/syslog.h */
typedef struct _code {
char *c_name;
int c_val;
} CODE;
static CODE facilitynames[] = {
{ "auth", LOG_AUTH },
#ifdef LOG_AUTHPRIV
{ "authpriv", LOG_AUTHPRIV },
#endif
{ "cron", LOG_CRON },
{ "daemon", LOG_DAEMON },
#ifdef LOG_FTP
{ "ftp", LOG_FTP },
#endif
{ "kern", LOG_KERN },
{ "lpr", LOG_LPR },
{ "mail", LOG_MAIL },
{ "mark", 0 }, /* never used! */
{ "news", LOG_NEWS },
{ "security", LOG_AUTH }, /* DEPRECATED */
{ "syslog", LOG_SYSLOG },
{ "user", LOG_USER },
{ "uucp", LOG_UUCP },
{ "local0", LOG_LOCAL0 },
{ "local1", LOG_LOCAL1 },
{ "local2", LOG_LOCAL2 },
{ "local3", LOG_LOCAL3 },
{ "local4", LOG_LOCAL4 },
{ "local5", LOG_LOCAL5 },
{ "local6", LOG_LOCAL6 },
{ "local7", LOG_LOCAL7 },
{ NULL, -1 }
};
#endif
static regex_t Empty, Comment, User, Group, Name, RootJail, Daemon, LogFacility,
LogLevel, Alive, SSLEngine, Control;
#if WAF
static regex_t WafRules, Waf_body_size;
#endif
static regex_t ListenHTTP, ListenHTTPS, End, Address, Port, Cert, CertDir,
xHTTP, Client, CheckURL;
static regex_t Err414, Err500, Err501, Err503, ErrNoSsl, NoSslRedirect,
MaxRequest, HeadRemove, RemoveResponseHead, RewriteLocation,
RewriteDestination, ForwardSNI;
static regex_t Service, ServiceName, URL, OrURLs, HeadRequire, HeadDeny,
BackEnd, Emergency, Priority, HAport, HAportAddr, StrictTransportSecurity;
static regex_t Redirect, TimeOut, WSTimeOut, Session, Type, TTL, ID, DynScale;
static regex_t ClientCert, AddHeader, AddResponseHeader, DisableProto,
SSLAllowClientRenegotiation, SSLHonorCipherOrder, Ciphers;
static regex_t CAlist, VerifyList, CRLlist, NoHTTPS11, Grace, ConnTO,
IgnoreCase, Ignore100continue, HTTPS;
static regex_t Disabled, Threads, CNName, Anonymise, DHParams, ECDHCurve;
static regex_t ControlGroup, ControlUser, ControlMode;
static regex_t ThreadModel;
static regex_t ForceHTTP10, SSLUncleanShutdown;
static regex_t BackendKey, BackendCookie;
static regmatch_t matches[5];
static DH *DHCustom_params;
static char *xhttp[] = {
"^(GET|POST|HEAD) ([^ ]+) HTTP/1.[01]$",
"^(GET|POST|HEAD|PUT|PATCH|DELETE) ([^ ]+) HTTP/1.[01]$",
"^(GET|POST|HEAD|PUT|PATCH|DELETE|LOCK|UNLOCK|PROPFIND|PROPPATCH|SEARCH|MKCOL|MKCALENDAR|MOVE|COPY|OPTIONS|TRACE|MKACTIVITY|CHECKOUT|MERGE|REPORT) ([^ ]+) HTTP/1.[01]$",
"^(GET|POST|HEAD|PUT|PATCH|DELETE|LOCK|UNLOCK|PROPFIND|PROPPATCH|SEARCH|MKCOL|MKCALENDAR|MOVE|COPY|OPTIONS|TRACE|MKACTIVITY|CHECKOUT|MERGE|REPORT|SUBSCRIBE|UNSUBSCRIBE|BPROPPATCH|POLL|BMOVE|BCOPY|BDELETE|BPROPFIND|NOTIFY|CONNECT) ([^ ]+) HTTP/1.[01]$",
"^(GET|POST|HEAD|PUT|PATCH|DELETE|LOCK|UNLOCK|PROPFIND|PROPPATCH|SEARCH|MKCOL|MKCALENDAR|MOVE|COPY|OPTIONS|TRACE|MKACTIVITY|CHECKOUT|MERGE|REPORT|SUBSCRIBE|UNSUBSCRIBE|BPROPPATCH|POLL|BMOVE|BCOPY|BDELETE|BPROPFIND|NOTIFY|CONNECT|RPC_IN_DATA|RPC_OUT_DATA|VERSION-CONTROL) ([^ ]+) HTTP/1.[01]$",
"^(GET|POST|HEAD|PUT|PATCH|DELETE|OPTIONS) ([^ ]+) HTTP/1.[01]$",
};
static int log_level = 1;
static int def_facility = LOG_DAEMON;
static int clnt_to = 10;
static int be_to = 15;
static int ws_to = 600;
static int be_connto = 15;
static int dynscale = 0;
static int ignore_case = 0;
static int auto_ecdhcurve = 1;
#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
#ifndef OPENSSL_NO_ECDH
static int EC_nid = NID_X9_62_prime256v1;
#endif
#endif
#define MAX_FIN 100
static FILE *f_in[MAX_FIN];
static char *f_name[MAX_FIN];
static int n_lin[MAX_FIN];
static int cur_fin;
static void load_cert(int has_other, LISTENER * res, char *filename);
static void load_certdir(int has_other, LISTENER * res, const char *dir_path);
static int conf_init(const char *name)
{
if ((f_name[0] = strdup(name)) == NULL) {
logmsg(LOG_ERR, "open %s: out of memory", name);
exit(1);
}
if ((f_in[0] = fopen(name, "rt")) == NULL) {
logmsg(LOG_ERR, "can't open open %s", name);
exit(1);
}
n_lin[0] = 0;
cur_fin = 0;
return 0;
}
void conf_err(const char *msg)
{
logmsg(LOG_ERR, "%s line %d: %s", f_name[cur_fin], n_lin[cur_fin], msg);
exit(1);
}
static char *conf_fgets(char *buf, const int max)
{
int i;
for (;;) {
if (fgets(buf, max, f_in[cur_fin]) == NULL) {
fclose(f_in[cur_fin]);
free(f_name[cur_fin]);
if (cur_fin > 0) {
cur_fin--;
continue;
} else
return NULL;
}
n_lin[cur_fin]++;
for (i = 0; i < max; i++)
if (buf[i] == '\n' || buf[i] == '\r') {
buf[i] = '\0';
break;
}
if (!regexec(&Empty, buf, 4, matches, 0)
|| !regexec(&Comment, buf, 4, matches, 0))
/* comment or empty line */
continue;
return buf;
}
}
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
#define general_name_string(n) \
strndup(ASN1_STRING_get0_data(n->d.dNSName), \
ASN1_STRING_length(n->d.dNSName) + 1)
#else
#define general_name_string(n) \
strndup(ASN1_STRING_data(n->d.dNSName), \
ASN1_STRING_length(n->d.dNSName) + 1)
#endif
unsigned char **get_subjectaltnames(X509 * x509, unsigned int *count)
{
unsigned int local_count;
unsigned char **result;
STACK_OF(GENERAL_NAME) * san_stack =
(STACK_OF(GENERAL_NAME) *) X509_get_ext_d2i(x509, NID_subject_alt_name,
NULL, NULL);
unsigned char *temp[sk_GENERAL_NAME_num(san_stack)];
GENERAL_NAME *name;
int i;
local_count = 0;
result = NULL;
name = NULL;
*count = 0;
if (san_stack == NULL)
return NULL;
while (sk_GENERAL_NAME_num(san_stack) > 0) {
name = sk_GENERAL_NAME_pop(san_stack);
switch (name->type) {
case GEN_DNS:
temp[local_count] =
strndup(ASN1_STRING_get0_data(name->d.dNSName),
ASN1_STRING_length(name->d.dNSName)
+ 1);
if (temp[local_count] == NULL)
conf_err("out of memory");
local_count++;
break;
default:
logmsg(LOG_INFO, "unsupported subjectAltName type encountered: %i",
name->type);
}
GENERAL_NAME_free(name);
}
result = (unsigned char **) malloc(sizeof(unsigned char *) * local_count);
if (result == NULL)
conf_err("out of memory");
for (i = 0; i < local_count; i++) {
result[i] = strndup(temp[i], strlen(temp[i]) + 1);
if (result[i] == NULL)
conf_err("out of memory");
free(temp[i]);
}
*count = local_count;
sk_GENERAL_NAME_pop_free(san_stack, GENERAL_NAME_free);
return result;
}
/*
* parse a back-end
*/
static BACKEND *parse_be(const int is_emergency)
{
char lin[MAXBUF];
char *cp;
BACKEND *res;
int has_addr, has_port;
struct hostent *host;
struct sockaddr_in in;
struct sockaddr_in6 in6;
if ((res = (BACKEND *) malloc(sizeof(BACKEND))) == NULL)
conf_err("BackEnd config: out of memory - aborted");
memset(res, 0, sizeof(BACKEND));
res->be_type = 0;
res->addr.ai_socktype = SOCK_STREAM;
res->to = is_emergency ? 120 : be_to;
res->conn_to = is_emergency ? 120 : be_connto;
res->ws_to = is_emergency ? 120 : ws_to;
res->alive = 1;
memset(&res->addr, 0, sizeof(res->addr));
res->priority = 5;
memset(&res->ha_addr, 0, sizeof(res->ha_addr));
res->url = NULL;
res->bekey = NULL;
res->connections;
res->ecdhcurve_EC_nid = 0;
res->next = NULL;
has_addr = has_port = 0;
pthread_mutex_init(&res->mut, NULL);
while (conf_fgets(lin, MAXBUF)) {
if (strlen(lin) > 0 && lin[strlen(lin) - 1] == '\n')
lin[strlen(lin) - 1] = '\0';
if (!regexec(&Address, lin, 4, matches, 0)) {
lin[matches[1].rm_eo] = '\0';
if (get_host(lin + matches[1].rm_so, &res->addr, PF_UNSPEC)) {
/* if we can't resolve it assume this is a UNIX domain socket */
res->addr.ai_socktype = SOCK_STREAM;
res->addr.ai_family = AF_UNIX;
res->addr.ai_protocol = 0;
if ((res->addr.ai_addr =
(struct sockaddr *) malloc(sizeof(struct sockaddr_un))) == NULL)
conf_err("out of memory");
if ((strlen(lin + matches[1].rm_so) + 1) > UNIX_PATH_MAX)
conf_err("UNIX path name too long");
res->addr.ai_addrlen = strlen(lin + matches[1].rm_so) + 1;
res->addr.ai_addr->sa_family = AF_UNIX;
strcpy(res->addr.ai_addr->sa_data, lin + matches[1].rm_so);
res->addr.ai_addrlen = sizeof(struct sockaddr_un);
}
has_addr = 1;
} else if (!regexec(&Port, lin, 4, matches, 0)) {
switch (res->addr.ai_family) {
case AF_INET:
memcpy(&in, res->addr.ai_addr, sizeof(in));
in.sin_port = (in_port_t) htons(atoi(lin + matches[1].rm_so));
memcpy(res->addr.ai_addr, &in, sizeof(in));
break;
case AF_INET6:
memcpy(&in6, res->addr.ai_addr, sizeof(in6));
in6.sin6_port = (in_port_t) htons(atoi(lin + matches[1].rm_so));
memcpy(res->addr.ai_addr, &in6, sizeof(in6));
break;
default:
conf_err("Port is supported only for INET/INET6 back-ends");
}
has_port = 1;
} else if (!regexec(&BackendKey, lin, 4, matches, 0)) {
lin[matches[1].rm_eo] = '\0';
if ((res->bekey = strdup(lin + matches[1].rm_so)) == NULL)
conf_err("out of memory");
} else if (!regexec(&Priority, lin, 4, matches, 0)) {
if (is_emergency)
conf_err("Priority is not supported for Emergency back-ends");
res->priority = atoi(lin + matches[1].rm_so);
} else if (!regexec(&TimeOut, lin, 4, matches, 0)) {
res->to = atoi(lin + matches[1].rm_so);
} else if (!regexec(&WSTimeOut, lin, 4, matches, 0)) {
res->ws_to = atoi(lin + matches[1].rm_so);
} else if (!regexec(&ConnTO, lin, 4, matches, 0)) {
res->conn_to = atoi(lin + matches[1].rm_so);
} else if (!regexec(&HAport, lin, 4, matches, 0)) {
if (is_emergency)
conf_err("HAport is not supported for Emergency back-ends");
res->ha_addr = res->addr;
if ((res->ha_addr.ai_addr =
(struct sockaddr *) malloc(res->addr.ai_addrlen)) == NULL)
conf_err("out of memory");
memcpy(res->ha_addr.ai_addr, res->addr.ai_addr, res->addr.ai_addrlen);
switch (res->addr.ai_family) {
case AF_INET:
memcpy(&in, res->ha_addr.ai_addr, sizeof(in));
in.sin_port = (in_port_t) htons(atoi(lin + matches[1].rm_so));
memcpy(res->ha_addr.ai_addr, &in, sizeof(in));
break;
case AF_INET6:
memcpy(&in6, res->addr.ai_addr, sizeof(in6));
in6.sin6_port = (in_port_t) htons(atoi(lin + matches[1].rm_so));
memcpy(res->addr.ai_addr, &in6, sizeof(in6));
break;
default:
conf_err("HAport is supported only for INET/INET6 back-ends");
}
} else if (!regexec(&HAportAddr, lin, 4, matches, 0)) {
if (is_emergency)
conf_err("HAportAddr is not supported for Emergency back-ends");
lin[matches[1].rm_eo] = '\0';
if (get_host(lin + matches[1].rm_so, &res->ha_addr, PF_UNSPEC)) {
/* if we can't resolve it assume this is a UNIX domain socket */
res->addr.ai_socktype = SOCK_STREAM;
res->ha_addr.ai_family = AF_UNIX;
res->ha_addr.ai_protocol = 0;
if ((res->ha_addr.ai_addr =
(struct sockaddr *) strdup(lin + matches[1].rm_so)) == NULL)
conf_err("out of memory");
res->addr.ai_addrlen = strlen(lin + matches[1].rm_so) + 1;
} else
switch (res->ha_addr.ai_family) {
case AF_INET:
memcpy(&in, res->ha_addr.ai_addr, sizeof(in));
in.sin_port = (in_port_t) htons(atoi(lin + matches[2].rm_so));
memcpy(res->ha_addr.ai_addr, &in, sizeof(in));
break;
case AF_INET6:
memcpy(&in6, res->ha_addr.ai_addr, sizeof(in6));
in6.sin6_port = (in_port_t) htons(atoi(lin + matches[2].rm_so));
memcpy(res->ha_addr.ai_addr, &in6, sizeof(in6));
break;
default:
conf_err("Unknown HA address type");
}
} else if (!regexec(&ECDHCurve, lin, 4, matches, 0)) {
lin[matches[1].rm_eo] = '\0';
if ((res->ecdhcurve_EC_nid = OBJ_sn2nid(lin + matches[1].rm_so)) == 0)
conf_err("ECDHCurve config: invalid curve name");
} else if (!regexec(&HTTPS, lin, 4, matches, 0)) {
if ((res->ctx = SSL_CTX_new(SSLv23_client_method())) == NULL)
conf_err("SSL_CTX_new failed - aborted");
SSL_CTX_set_app_data(res->ctx, res);
SSL_CTX_set_verify(res->ctx, SSL_VERIFY_NONE, NULL);
SSL_CTX_set_mode(res->ctx, SSL_MODE_AUTO_RETRY);
#ifdef SSL_MODE_SEND_FALLBACK_SCSV
SSL_CTX_set_mode(res->ctx, SSL_MODE_SEND_FALLBACK_SCSV);
#endif
SSL_CTX_set_options(res->ctx, SSL_OP_ALL);
#ifdef SSL_OP_NO_COMPRESSION
SSL_CTX_set_options(res->ctx, SSL_OP_NO_COMPRESSION);
#endif
SSL_CTX_clear_options(res->ctx, SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
SSL_CTX_clear_options(res->ctx, SSL_OP_LEGACY_SERVER_CONNECT);
sprintf(lin, "%d-Pound-%ld", getpid(), random());
SSL_CTX_set_session_id_context(res->ctx, (unsigned char *) lin,
strlen(lin));
SSL_CTX_set_tmp_rsa_callback(res->ctx, RSA_tmp_callback);
if (NULL == DHCustom_params)
SSL_CTX_set_tmp_dh_callback(res->ctx, DH_tmp_callback);
else
SSL_CTX_set_tmp_dh(res->ctx, DHCustom_params);
} else if (!regexec(&Cert, lin, 4, matches, 0)) {
if (res->ctx == NULL)
conf_err("BackEnd Cert can only be used after HTTPS - aborted");
lin[matches[1].rm_eo] = '\0';
if (SSL_CTX_use_certificate_chain_file(res->ctx, lin + matches[1].rm_so)
!= 1)
conf_err("SSL_CTX_use_certificate_chain_file failed - aborted");
if (SSL_CTX_use_PrivateKey_file
(res->ctx, lin + matches[1].rm_so, SSL_FILETYPE_PEM) != 1)
conf_err("SSL_CTX_use_PrivateKey_file failed - aborted");
if (SSL_CTX_check_private_key(res->ctx) != 1)
conf_err("SSL_CTX_check_private_key failed - aborted");
} else if (!regexec(&Ciphers, lin, 4, matches, 0)) {
if (res->ctx == NULL)
conf_err("BackEnd Ciphers can only be used after HTTPS - aborted");
lin[matches[1].rm_eo] = '\0';
SSL_CTX_set_cipher_list(res->ctx, lin + matches[1].rm_so);
} else if (!regexec(&DisableProto, lin, 4, matches, 0)) {
if (res->ctx == NULL)
conf_err("BackEnd Disable can only be used after HTTPS - aborted");
lin[matches[1].rm_eo] = '\0';
if (strcasecmp(lin + matches[1].rm_so, "SSLv2") == 0)
SSL_CTX_set_options(res->ctx, SSL_OP_NO_SSLv2);
else if (strcasecmp(lin + matches[1].rm_so, "SSLv3") == 0)
SSL_CTX_set_options(res->ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
#ifdef SSL_OP_NO_TLSv1
else if (strcasecmp(lin + matches[1].rm_so, "TLSv1") == 0)
SSL_CTX_set_options(res->ctx,
SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
SSL_OP_NO_TLSv1);
#endif
#ifdef SSL_OP_NO_TLSv1_1
else if (strcasecmp(lin + matches[1].rm_so, "TLSv1_1") == 0)
SSL_CTX_set_options(res->ctx,
SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1
| SSL_OP_NO_TLSv1_1);
#endif
#ifdef SSL_OP_NO_TLSv1_2
else if (strcasecmp(lin + matches[1].rm_so, "TLSv1_2") == 0)
SSL_CTX_set_options(res->ctx,
SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1
| SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1_2);
#endif
#ifdef SSL_OP_NO_TLSv1_3
else if (strcasecmp(lin + matches[1].rm_so, "TLSv1_3") == 0)
SSL_CTX_set_options(res->ctx, SSL_OP_NO_TLSv1_3);
#endif
} else if (!regexec(&Disabled, lin, 4, matches, 0)) {
res->disabled = atoi(lin + matches[1].rm_so);
} else if (!regexec(&End, lin, 4, matches, 0)) {
if (!has_addr)
conf_err("BackEnd missing Address - aborted");
if ((res->addr.ai_family == AF_INET || res->addr.ai_family == AF_INET6)
&& !has_port)
conf_err("BackEnd missing Port - aborted");
if (!res->bekey) {
if (res->addr.ai_family == AF_INET)
snprintf(lin, MAXBUF - 1, "4-%08x-%x",
htonl(((struct sockaddr_in *) (res->addr.ai_addr))->
sin_addr.s_addr),
htons(((struct sockaddr_in *) (res->addr.
ai_addr))->sin_port));
else if (res->addr.ai_family == AF_INET6) {
cp = (char *)
&(((struct sockaddr_in6 *) (res->addr.ai_addr))->sin6_addr);
snprintf(lin, MAXBUF - 1,
"6-%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x-%x",
cp[0], cp[1], cp[2], cp[3], cp[4], cp[5], cp[6], cp[7],
cp[8], cp[9], cp[10], cp[11], cp[12], cp[13], cp[14], cp[15],
htons(((struct sockaddr_in6 *) (res->addr.
ai_addr))->sin6_port));
} else
conf_err("cannot autogenerate backendkey, please specify one");
if ((res->bekey = strdup(lin)) == NULL)
conf_err("out of memory autogenerating backendkey");
}
if (res->ctx != NULL) {
#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
#ifndef OPENSSL_NO_ECDH
if (res->ecdhcurve_EC_nid != 0) {
/* This generates a EC_KEY structure with no key, but a group defined */
EC_KEY *ecdh;
if ((ecdh = EC_KEY_new_by_curve_name(res->ecdhcurve_EC_nid)) == NULL)
conf_err("Unable to generate temp ECDH key");
SSL_CTX_set_tmp_ecdh(res->ctx, ecdh);
SSL_CTX_set_options(res->ctx, SSL_OP_SINGLE_ECDH_USE);
EC_KEY_free(ecdh);
}
#if defined(SSL_CTX_set_ecdh_auto)
else
SSL_CTX_set_ecdh_auto(res->ctx, 1);
#endif
#endif
#endif
}
return res;
} else {
conf_err("unknown directive");
}
}
conf_err("BackEnd premature EOF");
return NULL;
}
/*
* parse a session
*/
static void parse_sess(SERVICE * const svc)
{
char lin[MAXBUF], *cp, *parm;
parm = NULL;
while (conf_fgets(lin, MAXBUF)) {
if (strlen(lin) > 0 && lin[strlen(lin) - 1] == '\n')
lin[strlen(lin) - 1] = '\0';
if (!regexec(&Type, lin, 4, matches, 0)) {
if (svc->sess_type != SESS_NONE)
conf_err("Multiple Session types in one Service - aborted");
lin[matches[1].rm_eo] = '\0';
cp = lin + matches[1].rm_so;
if (!strcasecmp(cp, "IP"))
svc->sess_type = SESS_IP;
else if (!strcasecmp(cp, "COOKIE"))
svc->sess_type = SESS_COOKIE;
else if (!strcasecmp(cp, "URL"))
svc->sess_type = SESS_URL;
else if (!strcasecmp(cp, "PARM"))
svc->sess_type = SESS_PARM;
else if (!strcasecmp(cp, "BASIC"))
svc->sess_type = SESS_BASIC;
else if (!strcasecmp(cp, "HEADER"))
svc->sess_type = SESS_HEADER;
else
conf_err("Unknown Session type");
} else if (!regexec(&TTL, lin, 4, matches, 0)) {
svc->sess_ttl = atoi(lin + matches[1].rm_so);
} else if (!regexec(&ID, lin, 4, matches, 0)) {
if (svc->sess_type != SESS_COOKIE && svc->sess_type != SESS_URL
&& svc->sess_type != SESS_HEADER)
conf_err("no ID permitted unless COOKIE/URL/HEADER Session - aborted");
lin[matches[1].rm_eo] = '\0';
if ((parm = strdup(lin + matches[1].rm_so)) == NULL)
conf_err("ID config: out of memory - aborted");
} else if (!regexec(&End, lin, 4, matches, 0)) {
if (svc->sess_type == SESS_NONE)
conf_err("Session type not defined - aborted");
if (svc->sess_ttl == 0)
conf_err("Session TTL not defined - aborted");
if ((svc->sess_type == SESS_COOKIE || svc->sess_type == SESS_URL
|| svc->sess_type == SESS_HEADER)
&& parm == NULL)
conf_err("Session ID not defined - aborted");
if (svc->sess_type == SESS_COOKIE) {
snprintf(lin, MAXBUF - 1, "Cookie[^:]*:.*[; \t]%s=", parm);
if (regcomp
(&svc->sess_start, lin, REG_ICASE | REG_NEWLINE | REG_EXTENDED))
conf_err("COOKIE pattern failed - aborted");
if (regcomp
(&svc->sess_pat, "([^;]*)", REG_ICASE | REG_NEWLINE | REG_EXTENDED))
conf_err("COOKIE pattern failed - aborted");
} else if (svc->sess_type == SESS_URL) {
snprintf(lin, MAXBUF - 1, "[?&]%s=", parm);
if (regcomp
(&svc->sess_start, lin, REG_ICASE | REG_NEWLINE | REG_EXTENDED))
conf_err("URL pattern failed - aborted");
if (regcomp
(&svc->sess_pat, "([^&;#]*)",
REG_ICASE | REG_NEWLINE | REG_EXTENDED))
conf_err("URL pattern failed - aborted");
} else if (svc->sess_type == SESS_PARM) {
if (regcomp
(&svc->sess_start, ";", REG_ICASE | REG_NEWLINE | REG_EXTENDED))
conf_err("PARM pattern failed - aborted");
if (regcomp
(&svc->sess_pat, "([^?]*)", REG_ICASE | REG_NEWLINE | REG_EXTENDED))
conf_err("PARM pattern failed - aborted");
} else if (svc->sess_type == SESS_BASIC) {
if (regcomp
(&svc->sess_start, "Authorization:[ \t]*Basic[ \t]*",
REG_ICASE | REG_NEWLINE | REG_EXTENDED))
conf_err("BASIC pattern failed - aborted");
if (regcomp
(&svc->sess_pat, "([^ \t]*)",
REG_ICASE | REG_NEWLINE | REG_EXTENDED))
conf_err("BASIC pattern failed - aborted");
} else if (svc->sess_type == SESS_HEADER) {
snprintf(lin, MAXBUF - 1, "%s:[ \t]*", parm);
if (regcomp
(&svc->sess_start, lin, REG_ICASE | REG_NEWLINE | REG_EXTENDED))
conf_err("HEADER pattern failed - aborted");
if (regcomp
(&svc->sess_pat, "([^ \t]*)",
REG_ICASE | REG_NEWLINE | REG_EXTENDED))
conf_err("HEADER pattern failed - aborted");
}
if (parm != NULL)
free(parm);
return;
} else {
conf_err("unknown directive");
}
}
conf_err("Session premature EOF");
return;
}
/*
* basic hashing function, based on fmv
*/
static unsigned long t_hash(const TABNODE * e)
{
unsigned long res;
char *k;
k = e->key;
res = 2166136261;
while (*k)
res = ((res ^ *k++) * 16777619) & 0xFFFFFFFF;
return res;
}
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
static IMPLEMENT_LHASH_HASH_FN(t, TABNODE)
#else
static IMPLEMENT_LHASH_HASH_FN(t_hash, const TABNODE *)
#endif
static int t_cmp(const TABNODE * d1, const TABNODE * d2)
{
return strcmp(d1->key, d2->key);
}
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
static IMPLEMENT_LHASH_COMP_FN(t, TABNODE)
#else
static IMPLEMENT_LHASH_COMP_FN(t_cmp, const TABNODE *)
#endif
/*
* parse an OrURLs block
*
* Forms a composite pattern of all URLs within
* of the form ((url1)|(url2)|(url3)) (and so on)
*/
static char *parse_orurls(void)
{
char lin[MAXBUF];
char *pattern;
regex_t comp;
pattern = NULL;
while (conf_fgets(lin, MAXBUF)) {
if (strlen(lin) > 0 && lin[strlen(lin) - 1] == '\n')
lin[strlen(lin) - 1] = '\0';
if (!regexec(&URL, lin, 4, matches, 0)) {
lin[matches[1].rm_eo] = '\0';
/* Verify the pattern is valid */
if (regcomp(&comp, lin + matches[1].rm_so, REG_NEWLINE | REG_EXTENDED))
conf_err("URL bad pattern - aborted");
regfree(&comp);
if (pattern == NULL) {
if ((pattern =
(char *) malloc(strlen(lin + matches[1].rm_so) + 5)) == NULL)
conf_err("OrURLs config: out of memory - aborted");
*pattern = 0;
strcat(pattern, "((");
strcat(pattern, lin + matches[1].rm_so);
strcat(pattern, "))");
} else {
if ((pattern =
(char *) realloc(pattern,
strlen(pattern) + strlen(lin + matches[1].rm_so) +
4)) == NULL)
conf_err("OrURLs config: out of memory - aborted");
pattern[strlen(pattern) - 1] = 0;
strcat(pattern, "|(");
strcat(pattern, lin + matches[1].rm_so);
strcat(pattern, "))");
}
} else if (!regexec(&End, lin, 4, matches, 0)) {
if (!pattern)
conf_err("No URL directives specified within OrURLs block");
return pattern;
} else {
conf_err("unknown directive");
}
}
conf_err("OrURLs premature EOF");
return NULL;
}
/*
* parse a service
*/
static SERVICE *parse_service(const char *svc_name)
{
char lin[MAXBUF];
char pat[MAXBUF];
char *ptr;
SERVICE *res;
BACKEND *be;
MATCHER *m;
int ign_case;
if ((res = (SERVICE *) malloc(sizeof(SERVICE))) == NULL)
conf_err("Service config: out of memory - aborted");
memset(res, 0, sizeof(SERVICE));
res->sess_type = SESS_NONE;
res->dynscale = dynscale;
res->sts = -1;
pthread_mutex_init(&res->mut, NULL);
if (svc_name)
strncpy(res->name, svc_name, KEY_SIZE);
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
if ((res->sessions = lh_TABNODE_new(t_hash, t_cmp)) == NULL)
#elif OPENSSL_VERSION_NUMBER >= 0x10000000L
if ((res->sessions = LHM_lh_new(TABNODE, t)) == NULL)
#else
if ((res->sessions =
lh_new(LHASH_HASH_FN(t_hash), LHASH_COMP_FN(t_cmp))) == NULL)
#endif
conf_err("lh_new failed - aborted");
ign_case = ignore_case;
while (conf_fgets(lin, MAXBUF)) {
if (strlen(lin) > 0 && lin[strlen(lin) - 1] == '\n')
lin[strlen(lin) - 1] = '\0';
if (!regexec(&URL, lin, 4, matches, 0)) {
if (res->url) {
for (m = res->url; m->next; m = m->next);
if ((m->next = (MATCHER *) malloc(sizeof(MATCHER))) == NULL)
conf_err("URL config: out of memory - aborted");
m = m->next;
} else {
if ((res->url = (MATCHER *) malloc(sizeof(MATCHER))) == NULL)
conf_err("URL config: out of memory - aborted");
m = res->url;
}
memset(m, 0, sizeof(MATCHER));
lin[matches[1].rm_eo] = '\0';
if (regcomp
(&m->pat, lin + matches[1].rm_so,
REG_NEWLINE | REG_EXTENDED | (ign_case ? REG_ICASE : 0)))
conf_err("URL bad pattern - aborted");
} else if (!regexec(&OrURLs, lin, 4, matches, 0)) {
if (res->url) {
for (m = res->url; m->next; m = m->next);
if ((m->next = (MATCHER *) malloc(sizeof(MATCHER))) == NULL)
conf_err("URL config: out of memory - aborted");
m = m->next;
} else {
if ((res->url = (MATCHER *) malloc(sizeof(MATCHER))) == NULL)
conf_err("URL config: out of memory - aborted");
m = res->url;
}
memset(m, 0, sizeof(MATCHER));
ptr = parse_orurls();
if (regcomp
(&m->pat, ptr,
REG_NEWLINE | REG_EXTENDED | (ign_case ? REG_ICASE : 0)))
conf_err("OrURLs bad pattern - aborted");
free(ptr);
} else if (!regexec(&HeadRequire, lin, 4, matches, 0)) {
if (res->req_head) {
for (m = res->req_head; m->next; m = m->next);
if ((m->next = (MATCHER *) malloc(sizeof(MATCHER))) == NULL)
conf_err("HeadRequire config: out of memory - aborted");
m = m->next;
} else {
if ((res->req_head = (MATCHER *) malloc(sizeof(MATCHER))) == NULL)
conf_err("HeadRequire config: out of memory - aborted");
m = res->req_head;
}
memset(m, 0, sizeof(MATCHER));
lin[matches[1].rm_eo] = '\0';
if (regcomp
(&m->pat, lin + matches[1].rm_so,
REG_ICASE | REG_NEWLINE | REG_EXTENDED))
conf_err("HeadRequire bad pattern - aborted");
} else if (!regexec(&HeadDeny, lin, 4, matches, 0)) {
if (res->deny_head) {
for (m = res->deny_head; m->next; m = m->next);
if ((m->next = (MATCHER *) malloc(sizeof(MATCHER))) == NULL)
conf_err("HeadDeny config: out of memory - aborted");
m = m->next;
} else {
if ((res->deny_head = (MATCHER *) malloc(sizeof(MATCHER))) == NULL)
conf_err("HeadDeny config: out of memory - aborted");
m = res->deny_head;
}
memset(m, 0, sizeof(MATCHER));
lin[matches[1].rm_eo] = '\0';
if (regcomp
(&m->pat, lin + matches[1].rm_so,
REG_ICASE | REG_NEWLINE | REG_EXTENDED))
conf_err("HeadDeny bad pattern - aborted");
} else if (!regexec(&StrictTransportSecurity, lin, 4, matches, 0)) {
res->sts = atoi(lin + matches[1].rm_so);
} else if (!regexec(&Redirect, lin, 4, matches, 0)) {
if (res->backends) {
for (be = res->backends; be->next; be = be->next);
if ((be->next = (BACKEND *) malloc(sizeof(BACKEND))) == NULL)
conf_err("Redirect config: out of memory - aborted");
be = be->next;
} else {
if ((res->backends = (BACKEND *) malloc(sizeof(BACKEND))) == NULL)
conf_err("Redirect config: out of memory - aborted");
be = res->backends;
}
// 1 - Dynamic or not, 2 - Request Redirect #, 3 - Destination URL
memset(be, 0, sizeof(BACKEND));
be->be_type = 302;
be->redir_req = 0;
if (matches[1].rm_eo != matches[1].rm_so) {
if ((lin[matches[1].rm_so] & ~0x20) == 'D') {
be->redir_req = 2;
if (!res->url || res->url->next)
conf_err("Dynamic Redirect must be preceeded by a URL line");
} else if ((lin[matches[1].rm_so] & ~0x20) == 'A')
be->redir_req = 1;
}
if (matches[2].rm_eo != matches[2].rm_so)
be->be_type = atoi(lin + matches[2].rm_so);
be->priority = 1;
be->alive = 1;
pthread_mutex_init(&be->mut, NULL);
lin[matches[3].rm_eo] = '\0';
if ((be->url = strdup(lin + matches[3].rm_so)) == NULL)
conf_err("Redirector config: out of memory - aborted");
/* split the URL into its fields */
if (regexec(&LOCATION, be->url, 4, matches, 0))
conf_err("Redirect bad URL - aborted");
if ((matches[3].rm_eo - matches[3].rm_so) == 1)
/* the path is a single '/', so remove it */
be->url[matches[3].rm_so] = '\0';
} else if (!regexec(&BackEnd, lin, 4, matches, 0)) {
if (res->backends) {
for (be = res->backends; be->next; be = be->next);
be->next = parse_be(0);
} else
res->backends = parse_be(0);
} else if (!regexec(&Emergency, lin, 4, matches, 0)) {
res->emergency = parse_be(1);
} else if (!regexec(&BackendCookie, lin, 5, matches, 0)) {
lin[matches[1].rm_eo] = '\0';
lin[matches[2].rm_eo] = '\0';
lin[matches[3].rm_eo] = '\0';
lin[matches[4].rm_eo] = '\0';
snprintf(pat, MAXBUF - 1, "Cookie[^:]*:.*[; \t]%s=\"?([^\";]*)\"?",
lin + matches[1].rm_so);
if (matches[1].rm_so == matches[1].rm_eo)
conf_err("Backend cookie must have a name");
if ((res->becookie = strdup(lin + matches[1].rm_so)) == NULL)
conf_err("out of memory");
if (regcomp
(&res->becookie_re, pat, REG_ICASE | REG_NEWLINE | REG_EXTENDED))
conf_err("Backend Cookie pattern failed - aborted");
if (matches[2].rm_so != matches[2].rm_eo
&& (res->becdomain = strdup(lin + matches[2].rm_so)) == NULL)
conf_err("out of memory");
if (matches[3].rm_so != matches[3].rm_eo
&& (res->becpath = strdup(lin + matches[3].rm_so)) == NULL)
conf_err("out of memory");
res->becage = atoi(lin + matches[4].rm_so);
if ((lin[matches[4].rm_so] & ~0x20) == 'S')
res->becage = -1;
else
res->becage = atoi(lin + matches[4].rm_so);
} else if (!regexec(&Session, lin, 4, matches, 0)) {
parse_sess(res);
} else if (!regexec(&DynScale, lin, 4, matches, 0)) {
res->dynscale = atoi(lin + matches[1].rm_so);
} else if (!regexec(&IgnoreCase, lin, 4, matches, 0)) {
ign_case = atoi(lin + matches[1].rm_so);
} else if (!regexec(&Disabled, lin, 4, matches, 0)) {
res->disabled = atoi(lin + matches[1].rm_so);
} else if (!regexec(&End, lin, 4, matches, 0)) {
for (be = res->backends; be; be = be->next) {
if (!be->disabled)
res->tot_pri += be->priority;
res->abs_pri += be->priority;
}
return res;
} else {
conf_err("unknown directive");
}
}
conf_err("Service premature EOF");
return NULL;
}
/*
* return the file contents as a string
*/
static char *file2str(const char *fname)
{
char *res;
struct stat st;
int fin;
if (stat(fname, &st))
conf_err("can't stat Err file - aborted");
if ((fin = open(fname, O_RDONLY)) < 0)
conf_err("can't open Err file - aborted");
if ((res = malloc(st.st_size + 1)) == NULL)
conf_err("can't alloc Err file (out of memory) - aborted");
if (read(fin, res, st.st_size) != st.st_size)
conf_err("can't read Err file - aborted");
res[st.st_size] = '\0';
close(fin);
return res;
}
/*
* parse an HTTP listener
*/
static LISTENER *parse_HTTP(void)
{
char lin[MAXBUF];
LISTENER *res;
SERVICE *svc;
MATCHER *m;
int has_addr, has_port;
struct sockaddr_in in;
struct sockaddr_in6 in6;
if ((res = (LISTENER *) malloc(sizeof(LISTENER))) == NULL)
conf_err("ListenHTTP config: out of memory - aborted");
memset(res, 0, sizeof(LISTENER));
res->to = clnt_to;
res->rewr_loc = 1;
res->err414 = "Request URI is too long";
res->err500 = "An internal server error occurred. Please try again later.";
res->err501 = "This method may not be used.";
res->err503 = "The service is not available. Please try again later.";
res->errnossl = "Please use HTTPS.";
res->nossl_url = NULL;
res->nossl_redir = 0;
res->log_level = log_level;
res->ssl_forward_sni_server_name = 0;
if (regcomp(&res->verb, xhttp[0], REG_ICASE | REG_NEWLINE | REG_EXTENDED))
conf_err("xHTTP bad default pattern - aborted");
has_addr = has_port = 0;
while (conf_fgets(lin, MAXBUF)) {
if (strlen(lin) > 0 && lin[strlen(lin) - 1] == '\n')
lin[strlen(lin) - 1] = '\0';
if (!regexec(&Address, lin, 4, matches, 0)) {
lin[matches[1].rm_eo] = '\0';
if (get_host(lin + matches[1].rm_so, &res->addr, PF_UNSPEC))
conf_err("Unknown Listener address");
if (res->addr.ai_family != AF_INET && res->addr.ai_family != AF_INET6)
conf_err("Unknown Listener address family");
has_addr = 1;
} else if (!regexec(&Port, lin, 4, matches, 0)) {
switch (res->addr.ai_family) {
case AF_INET:
memcpy(&in, res->addr.ai_addr, sizeof(in));
in.sin_port = (in_port_t) htons(atoi(lin + matches[1].rm_so));
memcpy(res->addr.ai_addr, &in, sizeof(in));
break;
case AF_INET6:
memcpy(&in6, res->addr.ai_addr, sizeof(in6));
in6.sin6_port = htons(atoi(lin + matches[1].rm_so));
memcpy(res->addr.ai_addr, &in6, sizeof(in6));
break;
default:
conf_err("Unknown Listener address family");
}
has_port = 1;
} else if (!regexec(&xHTTP, lin, 4, matches, 0)) {
int n;
n = atoi(lin + matches[1].rm_so);
regfree(&res->verb);
if (regcomp(&res->verb, xhttp[n], REG_ICASE | REG_NEWLINE | REG_EXTENDED))
conf_err("xHTTP bad pattern - aborted");
} else if (!regexec(&Client, lin, 4, matches, 0)) {
res->to = atoi(lin + matches[1].rm_so);
} else if (!regexec(&CheckURL, lin, 4, matches, 0)) {
if (res->has_pat)
conf_err("CheckURL multiple pattern - aborted");
lin[matches[1].rm_eo] = '\0';
if (regcomp
(&res->url_pat, lin + matches[1].rm_so,
REG_NEWLINE | REG_EXTENDED | (ignore_case ? REG_ICASE : 0)))
conf_err("CheckURL bad pattern - aborted");
res->has_pat = 1;