-
Notifications
You must be signed in to change notification settings - Fork 18
/
mod_proxy.c
5833 lines (4680 loc) · 171 KB
/
mod_proxy.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
/*
* ProFTPD - mod_proxy
* Copyright (c) 2012-2024 TJ Saunders
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, TJ Saunders and other respective copyright holders
* give permission to link this program with OpenSSL, and distribute the
* resulting executable, without including the source code for OpenSSL in the
* source distribution.
*
* -----DO NOT EDIT BELOW THIS LINE-----
* $Archive: mod_proxy.a $
* $Libraries: -lsqlite3 -lssl -lcrypto$
*/
#include "mod_proxy.h"
#include "proxy/random.h"
#include "proxy/db.h"
#include "proxy/session.h"
#include "proxy/conn.h"
#include "proxy/netio.h"
#include "proxy/inet.h"
#include "proxy/ssh.h"
#include "proxy/tls.h"
#include "proxy/forward.h"
#include "proxy/reverse.h"
#include "proxy/ftp/conn.h"
#include "proxy/ftp/ctrl.h"
#include "proxy/ftp/data.h"
#include "proxy/ftp/dirlist.h"
#include "proxy/ftp/facts.h"
#include "proxy/ftp/msg.h"
#include "proxy/ftp/sess.h"
#include "proxy/ftp/xfer.h"
#include "proxy/ssh/ssh2.h"
#include "proxy/ssh/auth.h"
#include "proxy/ssh/crypto.h"
#if defined(HAVE_OSSL_PROVIDER_LOAD_OPENSSL)
# include <openssl/provider.h>
#endif /* HAVE_OSSL_PROVIDER_LOAD_OPENSSL */
/* Proxy role */
#define PROXY_ROLE_REVERSE 1
#define PROXY_ROLE_FORWARD 2
/* How long (in secs) to wait to connect to real server? */
#define PROXY_CONNECT_DEFAULT_TIMEOUT 5
/* How long (in secs) to wait for the end-of-data-transfer response? */
#define PROXY_LINGER_DEFAULT_TIMEOUT 3
extern xaset_t *server_list;
extern module xfer_module;
/* From response.c */
extern pr_response_t *resp_list, *resp_err_list;
module proxy_module;
int proxy_logfd = -1;
pool *proxy_pool = NULL;
unsigned long proxy_opts = 0UL;
unsigned int proxy_sess_state = 0U;
int proxy_datastore = PROXY_DATASTORE_SQLITE;
void *proxy_datastore_data = NULL;
size_t proxy_datastore_datasz = 0;
static int proxy_engine = FALSE;
static unsigned int proxy_login_attempts = 0;
static int proxy_role = PROXY_ROLE_REVERSE;
static const char *proxy_tables_dir = NULL;
static int proxy_tls_xfer_prot_policy = PROXY_FTP_SESS_TLS_XFER_PROTECTION_POLICY_REQUIRED;
#if defined(HAVE_OSSL_PROVIDER_LOAD_OPENSSL)
static OSSL_PROVIDER *legacy_provider = NULL;
#endif /* HAVE_OSSL_PROVIDER_LOAD_OPENSSL */
static const char *trace_channel = "proxy";
/* Necessary function prototypes. */
static int proxy_sess_init(void);
static void proxy_timeoutidle_ev(const void *, void *);
static void proxy_timeoutnoxfer_ev(const void *, void *);
static void proxy_timeoutstalled_ev(const void *, void *);
static int recv_resp(cmd_rec *cmd, struct proxy_session *proxy_sess,
pr_response_t **rp) {
int res, xerrno = 0;
pr_response_t *resp;
unsigned int resp_nlines = 0;
resp = proxy_ftp_ctrl_recv_resp(cmd->tmp_pool, proxy_sess->backend_ctrl_conn,
&resp_nlines, 0);
if (resp == NULL) {
xerrno = errno;
/* For a certain number of conditions, if we cannot read the response
* from the backend, then we should just close the frontend, otherwise
* we might "leak" to the client the fact that we are fronting some
* backend server rather than being the server.
*/
if (xerrno == ECONNRESET ||
xerrno == ECONNABORTED ||
xerrno == ENOENT ||
xerrno == EPIPE) {
pr_session_disconnect(&proxy_module, PR_SESS_DISCONNECT_BY_APPLICATION,
"Backend control connection lost");
}
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error receiving %s response from backend: %s", (char *) cmd->argv[0],
strerror(xerrno));
pr_response_add_err(R_500, _("%s: %s"), (char *) cmd->argv[0],
strerror(xerrno));
pr_response_flush(&resp_err_list);
errno = xerrno;
return -1;
}
res = proxy_ftp_ctrl_send_resp(cmd->tmp_pool, proxy_sess->frontend_ctrl_conn,
resp, resp_nlines);
if (res < 0) {
xerrno = errno;
pr_response_block(TRUE);
errno = xerrno;
return -1;
}
if (rp != NULL) {
*rp = resp;
}
return 0;
}
MODRET proxy_cmd(cmd_rec *cmd, struct proxy_session *proxy_sess,
pr_response_t **rp) {
int res, xerrno = 0;
res = proxy_ftp_ctrl_send_cmd(cmd->tmp_pool, proxy_sess->backend_ctrl_conn,
cmd);
xerrno = errno;
if (res < 0) {
xerrno = errno;
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error sending %s to backend: %s", (char *) cmd->argv[0],
strerror(xerrno));
pr_response_add_err(R_500, _("%s: %s"), (char *) cmd->argv[0],
strerror(xerrno));
pr_response_flush(&resp_err_list);
errno = xerrno;
return PR_ERROR(cmd);
}
if (recv_resp(cmd, proxy_sess, rp) < 0) {
return PR_ERROR(cmd);
}
pr_response_block(TRUE);
return PR_HANDLED(cmd);
}
MODRET proxy_abort(cmd_rec *cmd, struct proxy_session *proxy_sess,
pr_response_t **rp) {
int res, xerrno = 0;
res = proxy_ftp_ctrl_send_abort(cmd->tmp_pool,
proxy_sess->backend_ctrl_conn, cmd);
xerrno = errno;
if (res < 0) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error sending %s to backend: %s", (char *) cmd->argv[0],
strerror(xerrno));
pr_response_add_err(R_500, _("%s: %s"), (char *) cmd->argv[0],
strerror(xerrno));
pr_response_flush(&resp_err_list);
errno = xerrno;
return PR_ERROR(cmd);
}
if (proxy_sess->backend_data_conn != NULL) {
pr_trace_msg(trace_channel, 19, "received ABOR on frontend connection, "
"closing backend data connection");
proxy_inet_close(session.pool, proxy_sess->backend_data_conn);
pr_inet_close(session.pool, proxy_sess->backend_data_conn);
proxy_sess->backend_data_conn = NULL;
}
if (recv_resp(cmd, proxy_sess, rp) < 0) {
return PR_ERROR(cmd);
}
/* The ABOR command might have two responses, as when there is a data
* transfer in progress: one response for the data transfer, and one for
* handling the ABOR command itself.
*/
if ((proxy_sess->frontend_sess_flags & SF_XFER) ||
(proxy_sess->backend_sess_flags & SF_XFER)) {
if (recv_resp(cmd, proxy_sess, rp) < 0) {
return PR_ERROR(cmd);
}
}
pr_response_block(TRUE);
return PR_HANDLED(cmd);
}
MODRET proxy_data_cmd(cmd_rec *cmd, struct proxy_session *proxy_sess) {
int res, xerrno = 0;
modret_t *mr;
pr_response_t *resp = NULL;
unsigned int resp_nlines = 0;
mr = proxy_cmd(cmd, proxy_sess, &resp);
if (!MODRET_ISHANDLED(mr)) {
pr_response_block(TRUE);
return mr;
}
if (resp->num[0] != '1') {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"received non-1xx response from backend for %s: %s %s",
(char *) cmd->argv[0], resp->num, resp->msg);
pr_response_block(FALSE);
pr_response_add_err(resp->num, "%s", resp->msg);
pr_response_flush(&resp_err_list);
pr_response_block(TRUE);
errno = EINVAL;
return PR_ERROR(cmd);
}
/* Now we wait for our closing response. */
resp = proxy_ftp_ctrl_recv_resp(cmd->tmp_pool, proxy_sess->backend_ctrl_conn,
&resp_nlines, 0);
if (resp == NULL) {
xerrno = errno;
pr_response_block(FALSE);
/* For a certain number of conditions, if we cannot read the response
* from the backend, then we should just close the frontend, otherwise
* we might "leak" to the client the fact that we are fronting some
* backend server rather than being the server.
*/
if (xerrno == ECONNRESET ||
xerrno == ECONNABORTED ||
xerrno == ENOENT ||
xerrno == EPIPE) {
pr_session_disconnect(&proxy_module, PR_SESS_DISCONNECT_BY_APPLICATION,
"Backend control connection lost");
}
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error receiving %s response from backend: %s", (char *) cmd->argv[0],
strerror(xerrno));
pr_response_add_err(R_500, _("%s: %s"), (char *) cmd->argv[0],
strerror(xerrno));
pr_response_flush(&resp_err_list);
pr_response_block(TRUE);
errno = xerrno;
return PR_ERROR(cmd);
}
pr_response_block(FALSE);
res = proxy_ftp_ctrl_send_resp(cmd->tmp_pool, proxy_sess->frontend_ctrl_conn,
resp, resp_nlines);
if (res < 0) {
xerrno = errno;
pr_response_block(TRUE);
errno = xerrno;
return PR_ERROR(cmd);
}
pr_response_block(TRUE);
return PR_HANDLED(cmd);
}
static int proxy_stalled_timeout_cb(CALLBACK_FRAME) {
int timeout_stalled;
timeout_stalled = pr_data_get_timeout(PR_DATA_TIMEOUT_STALLED);
pr_event_generate("core.timeout-stalled", NULL);
pr_log_pri(PR_LOG_NOTICE, "Data transfer stall timeout: %d %s",
timeout_stalled, timeout_stalled != 1 ? "seconds" : "second");
pr_session_disconnect(NULL, PR_SESS_DISCONNECT_TIMEOUT,
"TimeoutStalled during data transfer");
/* Do not restart the timer (should never be reached). */
return 0;
}
static int proxy_mkdir(const char *dir, uid_t uid, gid_t gid, mode_t mode) {
mode_t prev_mask;
struct stat st;
int res = -1;
res = stat(dir, &st);
if (res < 0 &&
errno != ENOENT) {
return -1;
}
/* The directory already exists. */
if (res == 0) {
return 0;
}
/* The given mode is absolute, not subject to any Umask setting. */
prev_mask = umask(0);
if (mkdir(dir, mode) < 0) {
int xerrno = errno;
(void) umask(prev_mask);
errno = xerrno;
return -1;
}
umask(prev_mask);
if (chown(dir, uid, gid) < 0) {
return -1;
}
return 0;
}
static int proxy_mkpath(pool *p, const char *path, uid_t uid, gid_t gid,
mode_t mode) {
char *currpath = NULL, *tmppath = NULL;
struct stat st;
if (stat(path, &st) == 0) {
/* Path already exists, nothing to be done. */
errno = EEXIST;
return -1;
}
tmppath = pstrdup(p, path);
currpath = "/";
while (tmppath && *tmppath) {
char *currdir = strsep(&tmppath, "/");
currpath = pdircat(p, currpath, currdir, NULL);
if (proxy_mkdir(currpath, uid, gid, mode) < 0) {
return -1;
}
pr_signals_handle();
}
return 0;
}
static void proxy_remove_symbols(void) {
int res;
/* Remove mod_xfer's PRE_CMD handlers; they will only interfere
* with proxied data transfers (see GitHub issue #3).
*/
res = pr_stash_remove_cmd(C_APPE, &xfer_module, PRE_CMD, NULL, -1);
if (res < 0) {
pr_trace_msg(trace_channel, 1,
"error removing PRE_CMD APPE mod_xfer handler: %s", strerror(errno));
} else {
pr_trace_msg(trace_channel, 9, "removed PRE_CMD APPE mod_xfer handlers");
}
res = pr_stash_remove_cmd(C_RETR, &xfer_module, PRE_CMD, NULL, -1);
if (res < 0) {
pr_trace_msg(trace_channel, 1,
"error removing PRE_CMD RETR mod_xfer handler: %s", strerror(errno));
} else {
pr_trace_msg(trace_channel, 9, "removed PRE_CMD RETR mod_xfer handlers");
}
res = pr_stash_remove_cmd(C_STOR, &xfer_module, PRE_CMD, NULL, -1);
if (res < 0) {
pr_trace_msg(trace_channel, 1,
"error removing PRE_CMD STOR mod_xfer handler: %s", strerror(errno));
} else {
pr_trace_msg(trace_channel, 9, "removed PRE_CMD STOR mod_xfer handlers");
}
res = pr_stash_remove_cmd(C_STOU, &xfer_module, PRE_CMD, NULL, -1);
if (res < 0) {
pr_trace_msg(trace_channel, 1,
"error removing PRE_CMD STOU mod_xfer handler: %s", strerror(errno));
} else {
pr_trace_msg(trace_channel, 9, "removed PRE_CMD STOU mod_xfer handlers");
}
}
static void proxy_restrict_session(void) {
const char *proxy_chroot = NULL;
config_rec *c;
char *xferlog = PR_XFERLOG_PATH;
/* Open the TransferLog here, BEFORE we chroot. */
c = find_config(main_server->conf, CONF_PARAM, "TransferLog", FALSE);
if (c != NULL) {
xferlog = c->argv[0];
}
PRIVS_ROOT
if (strcasecmp(xferlog, "none") == 0) {
xferlog_open(NULL);
} else {
xferlog_open(xferlog);
}
if (getuid() == PR_ROOT_UID) {
int res;
/* Chroot to the ProxyTables/empty/ directory before dropping root privs. */
proxy_chroot = pdircat(proxy_pool, proxy_tables_dir, "empty", NULL);
res = chroot(proxy_chroot);
if (res < 0) {
int xerrno = errno;
PRIVS_RELINQUISH
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"unable to chroot to ProxyTables/empty/ directory '%s': %s",
proxy_chroot, strerror(xerrno));
pr_session_disconnect(&proxy_module, PR_SESS_DISCONNECT_MODULE_ACL,
"Unable to chroot proxy session");
} else {
pr_trace_msg(trace_channel, 9, "chrooted session to '%s'", proxy_chroot);
}
if (chdir("/") < 0) {
int xerrno = errno;
PRIVS_RELINQUISH
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"unable to chdir to root directory within chroot: %s",
strerror(xerrno));
pr_session_disconnect(&proxy_module, PR_SESS_DISCONNECT_MODULE_ACL,
"Unable to chroot proxy session");
}
}
/* Make the proxy session process have the identity of the configured daemon
* User/Group.
*/
PRIVS_REVOKE
session.disable_id_switching = TRUE;
if (proxy_chroot != NULL) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"proxy session running as UID %lu, GID %lu, restricted to '%s'",
(unsigned long) getuid(), (unsigned long) getgid(), proxy_chroot);
} else {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"proxy session running as UID %lu, GID %lu, located in '%s'",
(unsigned long) getuid(), (unsigned long) getgid(), getcwd(NULL, 0));
}
}
/* Configuration handlers
*/
/* usage: ProxyDataTransferPolicy "active"|"passive"|"pasv"|"epsv"|"port"|
* "eprt"|"client"
*/
MODRET set_proxydataxferpolicy(cmd_rec *cmd) {
config_rec *c;
int cmd_id = -1;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
cmd_id = pr_cmd_get_id(cmd->argv[1]);
if (cmd_id < 0) {
if (strncasecmp(cmd->argv[1], "active", 7) == 0) {
/* Try to use EPRT over PORT. */
cmd_id = PR_CMD_EPRT_ID;
} else if (strncasecmp(cmd->argv[1], "passive", 8) == 0) {
/* Try to use EPSV over PASV. */
cmd_id = PR_CMD_EPSV_ID;
} else if (strncasecmp(cmd->argv[1], "client", 7) == 0) {
cmd_id = 0;
} else {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unsupported DataTransferPolicy: ",
(char *) cmd->argv[1], NULL));
}
}
if (cmd_id != PR_CMD_PASV_ID &&
cmd_id != PR_CMD_EPSV_ID &&
cmd_id != PR_CMD_PORT_ID &&
cmd_id != PR_CMD_EPRT_ID &&
cmd_id != 0) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unsupported DataTransferPolicy: ",
(char *) cmd->argv[1], NULL));
}
c = add_config_param(cmd->argv[0], 1, NULL);
c->argv[0] = palloc(c->pool, sizeof(int));
*((int *) c->argv[0]) = cmd_id;
return PR_HANDLED(cmd);
}
/* usage: ProxyDatastore type [info] */
MODRET set_proxydatastore(cmd_rec *cmd) {
int ds = -1;
const char *ds_name = NULL;
void *ds_data = NULL;
size_t ds_datasz = 0;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT);
ds_name = cmd->argv[1];
if (strcasecmp(ds_name, "sqlite") == 0) {
ds = PROXY_DATASTORE_SQLITE;
ds_data = NULL;
ds_datasz = 0;
#ifdef PR_USE_REDIS
} else if (strcasecmp(ds_name, "redis") == 0) {
if (cmd->argc != 3) {
CONF_ERROR(cmd, "missing required Redis key prefix");
}
ds = PROXY_DATASTORE_REDIS;
ds_data = pstrdup(proxy_pool, cmd->argv[2]);
ds_datasz = strlen(ds_data);
#endif /* PR_USE_REDIS */
} else {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unsupported ProxyDatastore: ",
ds_name, NULL));
}
proxy_datastore = ds;
proxy_datastore_data = ds_data;
proxy_datastore_datasz = ds_datasz;
return PR_HANDLED(cmd);
}
/* usage: ProxyDirectoryListPolicy "client"|"LIST" [opt1 ... ]*/
MODRET set_proxydirlistpolicy(cmd_rec *cmd) {
config_rec *c;
int policy_id = -1;
unsigned long opts = 0UL;
if (cmd->argc < 2) {
CONF_ERROR(cmd, "wrong number of parameters");
}
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
if (strcasecmp(cmd->argv[1], "client") == 0) {
policy_id = PROXY_SESS_DIRECTORY_LIST_POLICY_DEFAULT;
} else if (strcasecmp(cmd->argv[1], "LIST") == 0) {
policy_id = PROXY_SESS_DIRECTORY_LIST_POLICY_LIST;
} else {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unsupported DirectoryListPolicy: ",
(char *) cmd->argv[1], NULL));
}
if (cmd->argc > 2) {
register unsigned int i;
for (i = 2; i < cmd->argc; i++) {
if (strcasecmp(cmd->argv[i], "UseSlink") == 0) {
opts |= PROXY_FTP_DIRLIST_OPT_USE_SLINK;
} else {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
"unknown DirectoryListPolicy option: ", (char *) cmd->argv[i], NULL));
}
}
}
c = add_config_param(cmd->argv[0], 2, NULL, NULL);
c->argv[0] = palloc(c->pool, sizeof(int));
*((int *) c->argv[0]) = policy_id;
c->argv[1] = palloc(c->pool, sizeof(unsigned long));
*((unsigned long *) c->argv[1]) = opts;
return PR_HANDLED(cmd);
}
/* usage: ProxyEngine on|off */
MODRET set_proxyengine(cmd_rec *cmd) {
int engine = 1;
config_rec *c;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
engine = get_boolean(cmd, 1);
if (engine == -1) {
CONF_ERROR(cmd, "expected Boolean parameter");
}
c = add_config_param(cmd->argv[0], 1, NULL);
c->argv[0] = pcalloc(c->pool, sizeof(int));
*((int *) c->argv[0]) = engine;
return PR_HANDLED(cmd);
}
/* usage: ProxyForwardEnabled on|off */
MODRET set_proxyforwardenabled(cmd_rec *cmd) {
int enabled = -1, *note = NULL, res;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_CLASS);
enabled = get_boolean(cmd, 1);
if (enabled < 0) {
CONF_ERROR(cmd, "expected Boolean parameter");
}
/* Stash this setting in the notes for this class. */
note = palloc(cmd->server->pool, sizeof(int));
*note = enabled;
res = pr_class_add_note(PROXY_FORWARD_ENABLED_NOTE, note, sizeof(int));
if (res < 0) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error storing parameter: ",
strerror(errno), NULL));
}
return PR_HANDLED(cmd);
}
/* usage: ProxyForwardMethod method */
MODRET set_proxyforwardmethod(cmd_rec *cmd) {
config_rec *c;
int forward_method = -1;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
forward_method = proxy_forward_get_method(cmd->argv[1]);
if (forward_method < 0) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
"unknown/unsupported forward method: ", (char *) cmd->argv[1], NULL));
}
c = add_config_param(cmd->argv[0], 1, NULL);
c->argv[0] = palloc(c->pool, sizeof(int));
*((int *) c->argv[0]) = forward_method;
return PR_HANDLED(cmd);
}
/* usage: ProxyForwardTo [!]pattern [flags] */
MODRET set_proxyforwardto(cmd_rec *cmd) {
#ifdef PR_USE_REGEX
config_rec *c;
pr_regex_t *pre = NULL;
int negated = FALSE, regex_flags = REG_EXTENDED|REG_NOSUB, res = 0;
char *pattern;
if (cmd->argc-1 < 1 ||
cmd->argc-1 > 2) {
CONF_ERROR(cmd, "bad number of parameters");
}
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
/* Make sure that, if present, the flags parameter is correctly formatted. */
if (cmd->argc-1 == 2) {
int flags = 0;
/* We need to parse the flags parameter here, to see if any flags which
* affect the compilation of the regex (e.g. NC) are present.
*/
flags = pr_filter_parse_flags(cmd->tmp_pool, cmd->argv[2]);
if (flags < 0) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
": badly formatted flags parameter: '", (char *) cmd->argv[2], "'",
NULL));
}
if (flags == 0) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
": unknown filter flags '", (char *) cmd->argv[2], "'", NULL));
}
regex_flags |= flags;
}
pre = pr_regexp_alloc(&proxy_module);
pattern = cmd->argv[1];
if (*pattern == '!') {
negated = TRUE;
pattern++;
}
res = pr_regexp_compile(pre, pattern, regex_flags);
if (res != 0) {
char errstr[200] = {'\0'};
pr_regexp_error(res, pre, errstr, sizeof(errstr));
pr_regexp_free(NULL, pre);
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "'", pattern, "' failed regex "
"compilation: ", errstr, NULL));
}
c = add_config_param(cmd->argv[0], 2, pre, NULL);
c->argv[1] = palloc(c->pool, sizeof(int));
*((int *) c->argv[1]) = negated;
return PR_HANDLED(cmd);
#else /* no regular expression support at the moment */
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "The ", param, " directive cannot be "
"used on this system, as you do not have POSIX compliant regex support",
NULL));
#endif
}
/* usage: ProxyLog path|"none" */
MODRET set_proxylog(cmd_rec *cmd) {
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
(void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
return PR_HANDLED(cmd);
}
/* usage: ProxyOptions opt1 ... optN */
MODRET set_proxyoptions(cmd_rec *cmd) {
register unsigned int i;
config_rec *c;
unsigned long opts = 0UL;
if (cmd->argc-1 == 0) {
CONF_ERROR(cmd, "wrong number of parameters");
}
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
c = add_config_param(cmd->argv[0], 1, NULL);
for (i = 1; i < cmd->argc; i++) {
if (strcmp(cmd->argv[i], "UseProxyProtocol") == 0 ||
strcmp(cmd->argv[i], "UseProxyProtocolV1") == 0) {
opts |= PROXY_OPT_USE_PROXY_PROTOCOL_V1;
} else if (strcmp(cmd->argv[i], "UseProxyProtocolV2") == 0) {
opts |= PROXY_OPT_USE_PROXY_PROTOCOL_V2;
} else if (strcmp(cmd->argv[i], "UseProxyProtocolV2TLVs") == 0) {
opts |= PROXY_OPT_USE_PROXY_PROTOCOL_V2_TLVS;
} else if (strcmp(cmd->argv[i], "ShowFeatures") == 0) {
opts |= PROXY_OPT_SHOW_FEATURES;
} else if (strcmp(cmd->argv[i], "UseReverseProxyAuth") == 0) {
opts |= PROXY_OPT_USE_REVERSE_PROXY_AUTH;
} else if (strcmp(cmd->argv[i], "UseDirectDataTransfers") == 0) {
opts |= PROXY_OPT_USE_DIRECT_DATA_TRANSFERS;
} else if (strcmp(cmd->argv[i], "IgnoreConfigPerms") == 0) {
opts |= PROXY_OPT_IGNORE_CONFIG_PERMS;
} else if (strcmp(cmd->argv[i], "AllowForeignAddress") == 0) {
opts |= PROXY_OPT_ALLOW_FOREIGN_ADDRESS;
} else {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown ProxyOption '",
(char *) cmd->argv[i], "'", NULL));
}
}
c->argv[0] = pcalloc(c->pool, sizeof(unsigned long));
*((unsigned long *) c->argv[0]) = opts;
return PR_HANDLED(cmd);
}
/* usage: ProxyRetryCount count */
MODRET set_proxyretrycount(cmd_rec *cmd) {
config_rec *c;
int retry_count = -1;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
retry_count = atoi(cmd->argv[1]);
if (retry_count < 1) {
CONF_ERROR(cmd, "retry count must be one or more");
}
c = add_config_param(cmd->argv[0], 1, NULL);
c->argv[0] = palloc(c->pool, sizeof(int));
*((int *) c->argv[0]) = retry_count;
return PR_HANDLED(cmd);
}
/* usage: ProxyReverseConnectPolicy [policy] */
MODRET set_proxyreverseconnectpolicy(cmd_rec *cmd) {
config_rec *c;
int connect_policy = -1;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
connect_policy = proxy_reverse_connect_get_policy_id(cmd->argv[1]);
if (connect_policy < 0) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
"unknown/unsupported connect policy: ", (char *) cmd->argv[1], NULL));
}
c = add_config_param(cmd->argv[0], 1, NULL);
c->argv[0] = palloc(c->pool, sizeof(int));
*((int *) c->argv[0]) = connect_policy;
return PR_HANDLED(cmd);
}
/* usage: ProxyReverseServers server1 ... server N
* file:/path/to/server/list.txt
* sql:/SQLNamedQuery
*/
MODRET set_proxyreverseservers(cmd_rec *cmd) {
config_rec *c;
array_header *backend_servers;
char *uri = NULL;
unsigned int flags = PROXY_CONN_CREATE_FL_USE_DNS_TTL;
if (cmd->argc-1 < 1) {
CONF_ERROR(cmd, "wrong number of parameters");
}
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
c = add_config_param(cmd->argv[0], 2, NULL, NULL);
backend_servers = make_array(c->pool, 1, sizeof(struct proxy_conn *));
if (cmd->argc-1 == 1) {
/* We are dealing with one of the following possibilities:
*
* file:/path/to/file.txt
* sql://SQLNamedQuery/...
* <server>
*/
if (strncmp(cmd->argv[1], "file:", 5) == 0) {
char *param, *path;
param = cmd->argv[1];
path = param + 5;
/* If the path contains the %U or %g variables, then defer loading of
* this file until the USER name is known.
*/
if (strstr(path, "%U") == NULL &&
strstr(path, "%g") == NULL) {
int xerrno;
/* For now, load the list of servers at sess init time. In
* the future, we will want to load it at postparse time, mapped
* to the appropriate server_rec, and clear/reload on 'core.restart'.
*/
PRIVS_ROOT
backend_servers = proxy_reverse_json_parse_uris(cmd->server->pool,
path, flags);
xerrno = errno;
PRIVS_RELINQUISH
if (backend_servers == NULL) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
"error reading ProxyReverseServers file '", path, "': ",
strerror(xerrno), NULL));
}
if (backend_servers->nelts == 0) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
"no usable URLs found in file '", path, "'", NULL));
}
} else {
/* Only provide a URI for dynamic lookup, e.g. per-user/group/etc. */
uri = cmd->argv[1];
}
} else if (strncmp(cmd->argv[1], "sql:/", 5) == 0) {
/* Unfortunately there's not very much we can do to validate these
* SQL URIs at the moment. They point to a SQLNamedQuery, which
* may not have been parsed yet from the config file, or which may be
* in a <Global> scope. Thus we simply store them for now, and
* let the lookup routines do the necessary validation.
*/
uri = cmd->argv[1];
} else {
/* Treat it as a server-spec (i.e. a URI) */
const struct proxy_conn *pconn;
pconn = proxy_conn_create(c->pool, cmd->argv[1], flags);
if (pconn == NULL) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error parsing '",
(char *) cmd->argv[1], "': ", strerror(errno), NULL));
}
*((const struct proxy_conn **) push_array(backend_servers)) = pconn;
}
} else {
register unsigned int i;
/* More than one parameter, which means they are all URIs. */
for (i = 1; i < cmd->argc; i++) {
const struct proxy_conn *pconn;
pconn = proxy_conn_create(c->pool, cmd->argv[i], flags);
if (pconn == NULL) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error parsing '",
(char *) cmd->argv[i], "': ", strerror(errno), NULL));
}
*((const struct proxy_conn **) push_array(backend_servers)) = pconn;
}
}
c->argv[0] = backend_servers;
if (uri != NULL) {
c->argv[1] = pstrdup(c->pool, uri);
}
return PR_HANDLED(cmd);
}
/* usage: ProxyRole "forward"|"reverse" */
MODRET set_proxyrole(cmd_rec *cmd) {
int role = 0;
config_rec *c;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
if (strcasecmp(cmd->argv[1], "forward") == 0) {
role = PROXY_ROLE_FORWARD;
} else if (strcasecmp(cmd->argv[1], "reverse") == 0) {