Newer
Older
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
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mod_proxy.h"
#include "mod_core.h"
#include "apr_optional.h"
#include "scoreboard.h"
#include "mod_status.h"
#include "proxy_util.h"
#if (MODULE_MAGIC_NUMBER_MAJOR > 20020903)
#include "mod_ssl.h"
#else
APR_DECLARE_OPTIONAL_FN(int, ssl_proxy_enable, (conn_rec *));
APR_DECLARE_OPTIONAL_FN(int, ssl_engine_disable, (conn_rec *));
APR_DECLARE_OPTIONAL_FN(int, ssl_is_https, (conn_rec *));
APR_DECLARE_OPTIONAL_FN(char *, ssl_var_lookup,
(apr_pool_t *, server_rec *,
conn_rec *, request_rec *, char *));
#endif
#ifndef MAX
#define MAX(x,y) ((x) >= (y) ? (x) : (y))
#endif
/*
* We do health-checks only if that (sub)module is loaded in. This
* allows for us to continue as is w/o requiring mod_watchdog for
* those implementations which aren't using health checks
*/
static APR_OPTIONAL_FN_TYPE(set_worker_hc_param) *set_worker_hc_param_f = NULL;
/* Externals */
proxy_hcmethods_t PROXY_DECLARE_DATA proxy_hcmethods[] = {
{NONE, "NONE", 1},
{TCP, "TCP", 1},
{OPTIONS, "OPTIONS", 1},
{HEAD, "HEAD", 1},
{GET, "GET", 1},
{CPING, "CPING", 0},
{PROVIDER, "PROVIDER", 0},
{EOT, NULL, 1}
};
proxy_wstat_t PROXY_DECLARE_DATA proxy_wstat_tbl[] = {
{PROXY_WORKER_INITIALIZED, PROXY_WORKER_INITIALIZED_FLAG, "Init "},
{PROXY_WORKER_IGNORE_ERRORS, PROXY_WORKER_IGNORE_ERRORS_FLAG, "Ign "},
{PROXY_WORKER_DRAIN, PROXY_WORKER_DRAIN_FLAG, "Drn "},
{PROXY_WORKER_GENERIC, PROXY_WORKER_GENERIC_FLAG, "Gen "},
{PROXY_WORKER_IN_SHUTDOWN, PROXY_WORKER_IN_SHUTDOWN_FLAG, "Shut "},
{PROXY_WORKER_DISABLED, PROXY_WORKER_DISABLED_FLAG, "Dis "},
{PROXY_WORKER_STOPPED, PROXY_WORKER_STOPPED_FLAG, "Stop "},
{PROXY_WORKER_IN_ERROR, PROXY_WORKER_IN_ERROR_FLAG, "Err "},
{PROXY_WORKER_HOT_STANDBY, PROXY_WORKER_HOT_STANDBY_FLAG, "Stby "},
{PROXY_WORKER_FREE, PROXY_WORKER_FREE_FLAG, "Free "},
{PROXY_WORKER_HC_FAIL, PROXY_WORKER_HC_FAIL_FLAG, "HcFl "},
{0x0, '\0', NULL}
};
static const char * const proxy_id = "proxy";
apr_global_mutex_t *proxy_mutex = NULL;
/*
* A Web proxy module. Stages:
*
* translate_name: set filename to proxy:<URL>
* map_to_storage: run proxy_walk (rather than directory_walk/file_walk)
* can't trust directory_walk/file_walk since these are
* not in our filesystem. Prevents mod_http from serving
* the TRACE request we will set aside to handle later.
* fix_ups: convert the URL stored in the filename to the
* canonical form.
* handler: handle proxy requests
*/
/* -------------------------------------------------------------- */
/* Translate the URL into a 'filename' */
static const char *set_worker_param(apr_pool_t *p,
server_rec *s,
proxy_worker *worker,
const char *key,
const char *val)
{
int ival;
apr_interval_time_t timeout;
if (!strcasecmp(key, "loadfactor")) {
/* Normalized load factor. Used with BalancerMember,
* it is a number between 1 and 100.
*/
double fval = atof(val);
ival = fval * 100.0;
if (ival < 100 || ival > 10000)
return "LoadFactor must be a number between 1..100";
worker->s->lbfactor = ival;
}
else if (!strcasecmp(key, "retry")) {
/* If set it will give the retry timeout for the worker
* The default value is 60 seconds, meaning that if
* in error state, it will be retried after that timeout.
*/
ival = atoi(val);
if (ival < 0)
return "Retry must be a positive value";
worker->s->retry = apr_time_from_sec(ival);
worker->s->retry_set = 1;
}
else if (!strcasecmp(key, "ttl")) {
/* Time in seconds that will destroy all the connections
* that exceed the smax
*/
ival = atoi(val);
if (ival < 1)
return "TTL must be at least one second";
worker->s->ttl = apr_time_from_sec(ival);
}
else if (!strcasecmp(key, "min")) {
/* Initial number of connections to remote
*/
ival = atoi(val);
if (ival < 0)
return "Min must be a positive number";
worker->s->min = ival;
}
else if (!strcasecmp(key, "max")) {
/* Maximum number of connections to remote
*/
ival = atoi(val);
if (ival < 0)
return "Max must be a positive number";
worker->s->hmax = ival;
}
/* XXX: More inteligent naming needed */
else if (!strcasecmp(key, "smax")) {
/* Maximum number of connections to remote that
* will not be destroyed
*/
ival = atoi(val);
if (ival < 0)
return "Smax must be a positive number";
worker->s->smax = ival;
}
else if (!strcasecmp(key, "acquire")) {
/* Acquire timeout in given unit (default is milliseconds).
* If set this will be the maximum time to
* wait for a free connection.
*/
if (ap_timeout_parameter_parse(val, &timeout, "ms") != APR_SUCCESS)
return "Acquire timeout has wrong format";
if (timeout < 1000)
return "Acquire must be at least one millisecond";
worker->s->acquire = timeout;
worker->s->acquire_set = 1;
}
else if (!strcasecmp(key, "timeout")) {
/* Connection timeout in seconds.
* Defaults to server timeout.
*/
ival = atoi(val);
if (ival < 1)
return "Timeout must be at least one second";
worker->s->timeout = apr_time_from_sec(ival);
worker->s->timeout_set = 1;
}
else if (!strcasecmp(key, "iobuffersize")) {
long s = atol(val);
if (s < 512 && s) {
return "IOBufferSize must be >= 512 bytes, or 0 for system default.";
}
worker->s->io_buffer_size = (s ? s : AP_IOBUFSIZE);
worker->s->io_buffer_size_set = 1;
}
else if (!strcasecmp(key, "receivebuffersize")) {
ival = atoi(val);
if (ival < 512 && ival != 0) {
return "ReceiveBufferSize must be >= 512 bytes, or 0 for system default.";
}
worker->s->recv_buffer_size = ival;
worker->s->recv_buffer_size_set = 1;
}
else if (!strcasecmp(key, "keepalive")) {
if (!strcasecmp(val, "on"))
worker->s->keepalive = 1;
else if (!strcasecmp(val, "off"))
worker->s->keepalive = 0;
else
return "KeepAlive must be On|Off";
worker->s->keepalive_set = 1;
}
else if (!strcasecmp(key, "disablereuse")) {
if (!strcasecmp(val, "on"))
worker->s->disablereuse = 1;
else if (!strcasecmp(val, "off"))
worker->s->disablereuse = 0;
else
return "DisableReuse must be On|Off";
worker->s->disablereuse_set = 1;
}
else if (!strcasecmp(key, "enablereuse")) {
if (!strcasecmp(val, "on"))
worker->s->disablereuse = 0;
else if (!strcasecmp(val, "off"))
worker->s->disablereuse = 1;
else
return "EnableReuse must be On|Off";
worker->s->disablereuse_set = 1;
}
else if (!strcasecmp(key, "route")) {
/* Worker route.
*/
if (strlen(val) >= sizeof(worker->s->route))
return apr_psprintf(p, "Route length must be < %d characters",
(int)sizeof(worker->s->route));
PROXY_STRNCPY(worker->s->route, val);
}
else if (!strcasecmp(key, "redirect")) {
/* Worker redirection route.
*/
if (strlen(val) >= sizeof(worker->s->redirect))
return apr_psprintf(p, "Redirect length must be < %d characters",
(int)sizeof(worker->s->redirect));
PROXY_STRNCPY(worker->s->redirect, val);
}
else if (!strcasecmp(key, "status")) {
const char *v;
int mode = 1;
apr_status_t rv;
/* Worker status.
*/
for (v = val; *v; v++) {
if (*v == '+') {
mode = 1;
v++;
}
else if (*v == '-') {
mode = 0;
v++;
}
rv = ap_proxy_set_wstatus(*v, mode, worker);
if (rv != APR_SUCCESS)
return "Unknown status parameter option";
}
}
else if (!strcasecmp(key, "flushpackets")) {
if (!strcasecmp(val, "on"))
worker->s->flush_packets = flush_on;
else if (!strcasecmp(val, "off"))
worker->s->flush_packets = flush_off;
else if (!strcasecmp(val, "auto"))
worker->s->flush_packets = flush_auto;
else
return "flushpackets must be on|off|auto";
}
else if (!strcasecmp(key, "flushwait")) {
ival = atoi(val);
if (ival > 1000 || ival < 0) {
return "flushwait must be <= 1000, or 0 for system default of 10 millseconds.";
}
if (ival == 0)
worker->s->flush_wait = PROXY_FLUSH_WAIT;
else
worker->s->flush_wait = ival * 1000; /* change to microseconds */
}
else if (!strcasecmp(key, "ping")) {
/* Ping/Pong timeout in given unit (default is second).
*/
if (ap_timeout_parameter_parse(val, &timeout, "s") != APR_SUCCESS)
return "Ping/Pong timeout has wrong format";
if (timeout < 1000)
return "Ping/Pong timeout must be at least one millisecond";
worker->s->ping_timeout = timeout;
worker->s->ping_timeout_set = 1;
}
else if (!strcasecmp(key, "lbset")) {
ival = atoi(val);
if (ival < 0 || ival > 99)
return "lbset must be between 0 and 99";
worker->s->lbset = ival;
}
else if (!strcasecmp(key, "connectiontimeout")) {
/* Request timeout in given unit (default is second).
* Defaults to connection timeout
*/
if (ap_timeout_parameter_parse(val, &timeout, "s") != APR_SUCCESS)
return "Connectiontimeout has wrong format";
if (timeout < 1000)
return "Connectiontimeout must be at least one millisecond.";
worker->s->conn_timeout = timeout;
worker->s->conn_timeout_set = 1;
}
else if (!strcasecmp(key, "flusher")) {
if (strlen(val) >= sizeof(worker->s->flusher))
apr_psprintf(p, "flusher name length must be < %d characters",
(int)sizeof(worker->s->flusher));
PROXY_STRNCPY(worker->s->flusher, val);
}
else if (!strcasecmp(key, "upgrade")) {
if (PROXY_STRNCPY(worker->s->upgrade, val) != APR_SUCCESS) {
return apr_psprintf(p, "upgrade protocol length must be < %d characters",
(int)sizeof(worker->s->upgrade));
}
}
else {
if (set_worker_hc_param_f) {
return set_worker_hc_param_f(p, s, worker, key, val, NULL);
} else {
return "unknown Worker parameter";
}
}
return NULL;
}
static const char *set_balancer_param(proxy_server_conf *conf,
apr_pool_t *p,
proxy_balancer *balancer,
const char *key,
const char *val)
{
int ival;
if (!strcasecmp(key, "stickysession")) {
char *path;
/* Balancer sticky session name.
* Set to something like JSESSIONID or
* PHPSESSIONID, etc..,
*/
if (strlen(val) >= sizeof(balancer->s->sticky_path))
apr_psprintf(p, "stickysession length must be < %d characters",
(int)sizeof(balancer->s->sticky_path));
PROXY_STRNCPY(balancer->s->sticky_path, val);
PROXY_STRNCPY(balancer->s->sticky, val);
if ((path = strchr((char *)balancer->s->sticky, '|'))) {
*path++ = '\0';
PROXY_STRNCPY(balancer->s->sticky_path, path);
}
}
else if (!strcasecmp(key, "stickysessionsep")) {
/* separator/delimiter for sessionid and route,
* normally '.'
*/
if (strlen(val) != 1) {
if (!strcasecmp(val, "off"))
balancer->s->sticky_separator = 0;
else
return "stickysessionsep must be a single character or Off";
}
else
balancer->s->sticky_separator = *val;
}
else if (!strcasecmp(key, "nofailover")) {
/* If set to 'on' the session will break
* if the worker is in error state or
* disabled.
*/
if (!strcasecmp(val, "on"))
balancer->s->sticky_force = 1;
else if (!strcasecmp(val, "off"))
balancer->s->sticky_force = 0;
else
return "failover must be On|Off";
}
else if (!strcasecmp(key, "timeout")) {
/* Balancer timeout in seconds.
* If set this will be the maximum time to
* wait for a free worker.
* Default is not to wait.
*/
ival = atoi(val);
if (ival < 1)
return "timeout must be at least one second";
balancer->s->timeout = apr_time_from_sec(ival);
}
else if (!strcasecmp(key, "maxattempts")) {
/* Maximum number of failover attempts before
* giving up.
*/
ival = atoi(val);
if (ival < 0)
return "maximum number of attempts must be a positive number";
balancer->s->max_attempts = ival;
balancer->s->max_attempts_set = 1;
}
else if (!strcasecmp(key, "lbmethod")) {
proxy_balancer_method *provider;
if (strlen(val) > (sizeof(balancer->s->lbpname)-1))
return "unknown lbmethod";
provider = ap_lookup_provider(PROXY_LBMETHOD, val, "0");
if (provider) {
balancer->lbmethod = provider;
if (PROXY_STRNCPY(balancer->s->lbpname, val) == APR_SUCCESS) {
return NULL;
}
else {
return "lbmethod name too large";
}
}
return "unknown lbmethod";
}
else if (!strcasecmp(key, "scolonpathdelim")) {
/* If set to 'on' then ';' will also be
* used as a session path separator/delim (ala
* mod_jk)
*/
if (!strcasecmp(val, "on"))
balancer->s->scolonsep = 1;
else if (!strcasecmp(val, "off"))
balancer->s->scolonsep = 0;
else
return "scolonpathdelim must be On|Off";
}
else if (!strcasecmp(key, "failonstatus")) {
char *val_split;
char *status;
char *tok_state;
val_split = apr_pstrdup(p, val);
balancer->errstatuses = apr_array_make(p, 1, sizeof(int));
status = apr_strtok(val_split, ", ", &tok_state);
while (status != NULL) {
ival = atoi(status);
if (ap_is_HTTP_VALID_RESPONSE(ival)) {
*(int *)apr_array_push(balancer->errstatuses) = ival;
}
else {
return "failonstatus must be one or more HTTP response codes";
}
status = apr_strtok(NULL, ", ", &tok_state);
}
}
else if (!strcasecmp(key, "failontimeout")) {
if (!strcasecmp(val, "on"))
balancer->failontimeout = 1;
else if (!strcasecmp(val, "off"))
balancer->failontimeout = 0;
else
return "failontimeout must be On|Off";
}
else if (!strcasecmp(key, "nonce")) {
if (!strcasecmp(val, "None")) {
*balancer->s->nonce = '\0';
}
else {
if (PROXY_STRNCPY(balancer->s->nonce, val) != APR_SUCCESS) {
return "Provided nonce is too large";
}
}
}
else if (!strcasecmp(key, "growth")) {
ival = atoi(val);
if (ival < 1 || ival > 100) /* arbitrary limit here */
return "growth must be between 1 and 100";
balancer->growth = ival;
}
else if (!strcasecmp(key, "forcerecovery")) {
if (!strcasecmp(val, "on"))
balancer->s->forcerecovery = 1;
else if (!strcasecmp(val, "off"))
balancer->s->forcerecovery = 0;
else
return "forcerecovery must be On|Off";
}
else {
return "unknown Balancer parameter";
}
return NULL;
}
static int alias_match(const char *uri, const char *alias_fakename)
{
const char *end_fakename = alias_fakename + strlen(alias_fakename);
const char *aliasp = alias_fakename, *urip = uri;
const char *end_uri = uri + strlen(uri);
while (aliasp < end_fakename && urip < end_uri) {
if (*aliasp == '/') {
/* any number of '/' in the alias matches any number in
* the supplied URI, but there must be at least one...
*/
if (*urip != '/')
return 0;
while (*aliasp == '/')
++aliasp;
while (*urip == '/')
++urip;
}
else {
/* Other characters are compared literally */
if (*urip++ != *aliasp++)
return 0;
}
}
/* fixup badly encoded stuff (e.g. % as last character) */
if (aliasp > end_fakename) {
aliasp = end_fakename;
}
if (urip > end_uri) {
urip = end_uri;
}
/* We reach the end of the uri before the end of "alias_fakename"
* for example uri is "/" and alias_fakename "/examples"
*/
if (urip == end_uri && aliasp != end_fakename) {
return 0;
}
/* Check last alias path component matched all the way */
if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
return 0;
/* Return number of characters from URI which matched (may be
* greater than length of alias, since we may have matched
* doubled slashes)
*/
return urip - uri;
}
/* Detect if an absoluteURI should be proxied or not. Note that we
* have to do this during this phase because later phases are
* "short-circuiting"... i.e. translate_names will end when the first
* module returns OK. So for example, if the request is something like:
*
* GET http://othervhost/cgi-bin/printenv HTTP/1.0
*
* mod_alias will notice the /cgi-bin part and ScriptAlias it and
* short-circuit the proxy... just because of the ordering in the
* configuration file.
*/
static int proxy_detect(request_rec *r)
{
void *sconf = r->server->module_config;
proxy_server_conf *conf =
(proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
/* Ick... msvc (perhaps others) promotes ternary short results to int */
if (conf->req && r->parsed_uri.scheme) {
/* but it might be something vhosted */
if (!(r->parsed_uri.hostname
&& !strcasecmp(r->parsed_uri.scheme, ap_http_scheme(r))
&& ap_matches_request_vhost(r, r->parsed_uri.hostname,
(apr_port_t)(r->parsed_uri.port_str ? r->parsed_uri.port
: ap_default_port(r))))) {
r->proxyreq = PROXYREQ_PROXY;
r->uri = r->unparsed_uri;
r->filename = apr_pstrcat(r->pool, "proxy:", r->uri, NULL);
r->handler = "proxy-server";
}
}
/* We need special treatment for CONNECT proxying: it has no scheme part */
else if (conf->req && r->method_number == M_CONNECT
&& r->parsed_uri.hostname
&& r->parsed_uri.port_str) {
r->proxyreq = PROXYREQ_PROXY;
r->uri = r->unparsed_uri;
r->filename = apr_pstrcat(r->pool, "proxy:", r->uri, NULL);
r->handler = "proxy-server";
}
return DECLINED;
}
static const char *proxy_interpolate(request_rec *r, const char *str)
{
/* Interpolate an env str in a configuration string
* Syntax ${var} --> value_of(var)
* Method: replace one var, and recurse on remainder of string
* Nothing clever here, and crap like nested vars may do silly things
* but we'll at least avoid sending the unwary into a loop
*/
const char *start;
const char *end;
const char *var;
const char *val;
const char *firstpart;
start = ap_strstr_c(str, "${");
if (start == NULL) {
return str;
}
end = ap_strchr_c(start+2, '}');
if (end == NULL) {
return str;
}
/* OK, this is syntax we want to interpolate. Is there such a var ? */
var = apr_pstrmemdup(r->pool, start+2, end-(start+2));
val = apr_table_get(r->subprocess_env, var);
firstpart = apr_pstrmemdup(r->pool, str, (start-str));
if (val == NULL) {
return apr_pstrcat(r->pool, firstpart,
proxy_interpolate(r, end+1), NULL);
}
else {
return apr_pstrcat(r->pool, firstpart, val,
proxy_interpolate(r, end+1), NULL);
}
}
static apr_array_header_t *proxy_vars(request_rec *r,
apr_array_header_t *hdr)
{
int i;
apr_array_header_t *ret = apr_array_make(r->pool, hdr->nelts,
sizeof (struct proxy_alias));
struct proxy_alias *old = (struct proxy_alias *) hdr->elts;
for (i = 0; i < hdr->nelts; ++i) {
struct proxy_alias *newcopy = apr_array_push(ret);
newcopy->fake = (old[i].flags & PROXYPASS_INTERPOLATE)
? proxy_interpolate(r, old[i].fake) : old[i].fake;
newcopy->real = (old[i].flags & PROXYPASS_INTERPOLATE)
? proxy_interpolate(r, old[i].real) : old[i].real;
}
return ret;
}
PROXY_DECLARE(int) ap_proxy_trans_match(request_rec *r, struct proxy_alias *ent,
proxy_dir_conf *dconf)
{
int len;
const char *fake;
const char *real;
ap_regmatch_t regm[AP_MAX_REG_MATCH];
ap_regmatch_t reg1[AP_MAX_REG_MATCH];
char *found = NULL;
int mismatch = 0;
unsigned int nocanon = ent->flags & PROXYPASS_NOCANON;
const char *use_uri = nocanon ? r->unparsed_uri : r->uri;
if (dconf && (dconf->interpolate_env == 1) && (ent->flags & PROXYPASS_INTERPOLATE)) {
fake = proxy_interpolate(r, ent->fake);
real = proxy_interpolate(r, ent->real);
}
else {
fake = ent->fake;
real = ent->real;
}
ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r, APLOGNO(03461)
"attempting to match URI path '%s' against %s '%s' for "
"proxying", r->uri, (ent->regex ? "pattern" : "prefix"),
fake);
if (ent->regex) {
if (!ap_regexec(ent->regex, r->uri, AP_MAX_REG_MATCH, regm, 0)) {
if ((real[0] == '!') && (real[1] == '\0')) {
ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, APLOGNO(03462)
"proxying is explicitly disabled for URI path "
"'%s'; declining", r->uri);
return DECLINED;
}
/* test that we haven't reduced the URI */
if (nocanon && ap_regexec(ent->regex, r->unparsed_uri,
AP_MAX_REG_MATCH, reg1, 0)) {
mismatch = 1;
use_uri = r->uri;
}
found = ap_pregsub(r->pool, real, use_uri, AP_MAX_REG_MATCH,
(use_uri == r->uri) ? regm : reg1);
if (!found) {
ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01135)
"Substitution in regular expression failed. "
"Replacement too long?");
return HTTP_INTERNAL_SERVER_ERROR;
}
/* Note: The strcmp() below catches cases where there
* was no regex substitution. This is so cases like:
*
* ProxyPassMatch \.gif balancer://foo
*
* will work "as expected". The upshot is that the 2
* directives below act the exact same way (ie: $1 is implied):
*
* ProxyPassMatch ^(/.*\.gif)$ balancer://foo
* ProxyPassMatch ^(/.*\.gif)$ balancer://foo$1
*
* which may be confusing.
*/
if (strcmp(found, real) != 0) {
found = apr_pstrcat(r->pool, "proxy:", found, NULL);
}
else {
found = apr_pstrcat(r->pool, "proxy:", real, use_uri, NULL);
}
}
}
else {
len = alias_match(r->uri, fake);
if (len != 0) {
if ((real[0] == '!') && (real[1] == '\0')) {
ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, APLOGNO(03463)
"proxying is explicitly disabled for URI path "
"'%s'; declining", r->uri);
return DECLINED;
}
if (nocanon && len != alias_match(r->unparsed_uri, ent->fake)) {
mismatch = 1;
use_uri = r->uri;
}
found = apr_pstrcat(r->pool, "proxy:", real, use_uri + len, NULL);
}
}
if (mismatch) {
/* We made a reducing transformation, so we can't safely use
* unparsed_uri. Safe fallback is to ignore nocanon.
*/
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01136)
"Unescaped URL path matched ProxyPass; ignoring unsafe nocanon");
}
if (found) {
r->filename = found;
r->handler = "proxy-server";
r->proxyreq = PROXYREQ_REVERSE;
if (nocanon && !mismatch) {
/* mod_proxy_http needs to be told. Different module. */
apr_table_setn(r->notes, "proxy-nocanon", "1");
}
if (ent->flags & PROXYPASS_NOQUERY) {
apr_table_setn(r->notes, "proxy-noquery", "1");
}
ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, APLOGNO(03464)
"URI path '%s' matches proxy handler '%s'", r->uri,
found);
return OK;
}
return DONE;
}
static int proxy_trans(request_rec *r)
{
int i;
struct proxy_alias *ent;
proxy_dir_conf *dconf;
proxy_server_conf *conf;
if (r->proxyreq) {
/* someone has already set up the proxy, it was possibly ourselves
* in proxy_detect
*/
return OK;
}
if ((r->unparsed_uri[0] == '*' && r->unparsed_uri[1] == '\0')
|| !r->uri || r->uri[0] != '/') {
return DECLINED;
}
if (apr_table_get(r->subprocess_env, "no-proxy")) {
return DECLINED;
}
/* XXX: since r->uri has been manipulated already we're not really
* compliant with RFC1945 at this point. But this probably isn't
* an issue because this is a hybrid proxy/origin server.
*/
dconf = ap_get_module_config(r->per_dir_config, &proxy_module);
/* short way - this location is reverse proxied? */
if (dconf->alias) {
int rv = ap_proxy_trans_match(r, dconf->alias, dconf);
if (DONE != rv) {
return rv;
}
}
conf = (proxy_server_conf *) ap_get_module_config(r->server->module_config,
&proxy_module);
/* long way - walk the list of aliases, find a match */
if (conf->aliases->nelts) {
ent = (struct proxy_alias *) conf->aliases->elts;
for (i = 0; i < conf->aliases->nelts; i++) {
int rv = ap_proxy_trans_match(r, &ent[i], dconf);
if (DONE != rv) {
return rv;
}
}
}
return DECLINED;
}
static int proxy_walk(request_rec *r)
{
proxy_server_conf *sconf = ap_get_module_config(r->server->module_config,
&proxy_module);
ap_conf_vector_t *per_dir_defaults = r->server->lookup_defaults;
ap_conf_vector_t **sec_proxy = (ap_conf_vector_t **) sconf->sec_proxy->elts;
ap_conf_vector_t *entry_config;
proxy_dir_conf *entry_proxy;
int num_sec = sconf->sec_proxy->nelts;
/* XXX: shouldn't we use URI here? Canonicalize it first?
* Pass over "proxy:" prefix
*/
const char *proxyname = r->filename + 6;
int j;
apr_pool_t *rxpool = NULL;
for (j = 0; j < num_sec; ++j)
{
int nmatch = 0;
int i;
ap_regmatch_t *pmatch = NULL;
entry_config = sec_proxy[j];
entry_proxy = ap_get_module_config(entry_config, &proxy_module);
if (entry_proxy->r) {
if (entry_proxy->refs && entry_proxy->refs->nelts) {
if (!rxpool) {
apr_pool_create(&rxpool, r->pool);
}
nmatch = entry_proxy->refs->nelts;
pmatch = apr_palloc(rxpool, nmatch*sizeof(ap_regmatch_t));
}
if (ap_regexec(entry_proxy->r, proxyname, nmatch, pmatch, 0)) {
continue;
}
for (i = 0; i < nmatch; i++) {
if (pmatch[i].rm_so >= 0 && pmatch[i].rm_eo >= 0 &&
((const char **)entry_proxy->refs->elts)[i]) {
apr_table_setn(r->subprocess_env,
((const char **)entry_proxy->refs->elts)[i],
apr_pstrndup(r->pool,
proxyname + pmatch[i].rm_so,
pmatch[i].rm_eo - pmatch[i].rm_so));
}
}
}
else if (
/* XXX: What about case insensitive matching ???
* Compare regex, fnmatch or string as appropriate
* If the entry doesn't relate, then continue
*/
entry_proxy->p_is_fnmatch ? apr_fnmatch(entry_proxy->p,
proxyname, 0) :
strncmp(proxyname, entry_proxy->p,
strlen(entry_proxy->p))) {
continue;
}
per_dir_defaults = ap_merge_per_dir_configs(r->pool, per_dir_defaults,
entry_config);
}
r->per_dir_config = per_dir_defaults;
if (rxpool) {
apr_pool_destroy(rxpool);
}
return OK;
}
static int proxy_map_location(request_rec *r)
{
int access_status;
if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
return DECLINED;
/* Don't let the core or mod_http map_to_storage hooks handle this,
* We don't need directory/file_walk, and we want to TRACE on our own.
*/
if ((access_status = proxy_walk(r))) {
ap_die(access_status, r);
return access_status;
}
return OK;
}
/* -------------------------------------------------------------- */
/* Fixup the filename */
/*
* Canonicalise the URL
*/
static int proxy_fixup(request_rec *r)
{
char *url, *p;
int access_status;
proxy_dir_conf *dconf = ap_get_module_config(r->per_dir_config,
&proxy_module);
if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
return DECLINED;
/* XXX: Shouldn't we try this before we run the proxy_walk? */
url = &r->filename[6];
if ((dconf->interpolate_env == 1) && (r->proxyreq == PROXYREQ_REVERSE)) {
/* create per-request copy of reverse proxy conf,
* and interpolate vars in it
*/
proxy_req_conf *rconf = apr_palloc(r->pool, sizeof(proxy_req_conf));
ap_set_module_config(r->request_config, &proxy_module, rconf);
rconf->raliases = proxy_vars(r, dconf->raliases);
rconf->cookie_paths = proxy_vars(r, dconf->cookie_paths);
rconf->cookie_domains = proxy_vars(r, dconf->cookie_domains);
}
/* canonicalise each specific scheme */
if ((access_status = proxy_run_canon_handler(r, url))) {
return access_status;
}
p = strchr(url, ':');
if (p == NULL || p == url)
return HTTP_BAD_REQUEST;
return OK; /* otherwise; we've done the best we can */
}
/* Send a redirection if the request contains a hostname which is not */
/* fully qualified, i.e. doesn't have a domain name appended. Some proxy */
/* servers like Netscape's allow this and access hosts from the local */
/* domain in this case. I think it is better to redirect to a FQDN, since */
/* these will later be found in the bookmarks files. */
/* The "ProxyDomain" directive determines what domain will be appended */
static int proxy_needsdomain(request_rec *r, const char *url, const char *domain)
{
char *nuri;
const char *ref;
/* We only want to worry about GETs */
if (!r->proxyreq || r->method_number != M_GET || !r->parsed_uri.hostname)
return DECLINED;
/* If host does contain a dot already, or it is "localhost", decline */
if (strchr(r->parsed_uri.hostname, '.') != NULL /* has domain, or IPv4 literal */
|| strchr(r->parsed_uri.hostname, ':') != NULL /* IPv6 literal */
|| strcasecmp(r->parsed_uri.hostname, "localhost") == 0)
return DECLINED; /* host name has a dot already */
ref = apr_table_get(r->headers_in, "Referer");
/* Reassemble the request, but insert the domain after the host name */
/* Note that the domain name always starts with a dot */
r->parsed_uri.hostname = apr_pstrcat(r->pool, r->parsed_uri.hostname,
domain, NULL);
nuri = apr_uri_unparse(r->pool,
&r->parsed_uri,
APR_URI_UNP_REVEALPASSWORD);
apr_table_setn(r->headers_out, "Location", nuri);
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(01138)
"Domain missing: %s sent to %s%s%s", r->uri,
apr_uri_unparse(r->pool, &r->parsed_uri,
APR_URI_UNP_OMITUSERINFO),
ref ? " from " : "", ref ? ref : "");
return HTTP_MOVED_PERMANENTLY;
}
/* -------------------------------------------------------------- */
/* Invoke handler */
static int proxy_handler(request_rec *r)
{
char *uri, *scheme, *p;
const char *p2;
void *sconf = r->server->module_config;
proxy_server_conf *conf = (proxy_server_conf *)
ap_get_module_config(sconf, &proxy_module);
apr_array_header_t *proxies = conf->proxies;
struct proxy_remote *ents = (struct proxy_remote *) proxies->elts;
int i, rc, access_status;
int direct_connect = 0;
const char *str;
apr_int64_t maxfwd;