-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod_paa.c
1290 lines (1073 loc) · 38.4 KB
/
mod_paa.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2020 Ping Identity Corporation
* All rights reserved.
*
* The contents of this file are the property of Ping Identity Corporation.
* You may not copy or use this file, in either source code or executable
* form, except in compliance with terms set by Ping Identity Corporation.
* For further information please contact:
*
* Ping Identity Corporation
* 1001 17th St Suite 100
* Denver, CO 80202
* 303.468.2900
* https://www.pingidentity.com
*/
/**
* PAA Apache module
*
* @file mod_paa.c
*/
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "http_request.h"
#include "http_log.h"
#include "ap_config.h"
#include "ap_mpm.h"
#include "apr_tables.h"
#include "apr_time.h"
#include "apr_strings.h"
#include "apr_atomic.h"
#include "apr_thread_proc.h"
#include "apr_base64.h"
#include "paa-log.h"
#include "paa-http-client-curl.h"
#include "paa-config-filesystem.h"
#include "paa-cache-zmq.h"
#include "paa-cache-standalone.h"
#include "paa.h"
#include "apache-http-server-facade.h"
#include "mod-paa-platform.h"
/* Equivalent of ASCII "AP24" */
#if (MODULE_MAGIC_COOKIE == 0x41503234UL)
#define APACHE24
/* Equivalent of ASCII "AP22" */
#elif (MODULE_MAGIC_COOKIE == 0x41503232UL)
#define APACHE22
#endif
// constants
static
const char * const SET_RESPONSE_HEADERS_FILTER = "SET_RESPONSE_HEADERS";
static
const char * const APACHE_DISABLE_MONITORING = "agent.apache.monitoring.disabled";
static
const char * const PAA_TRACE_LOGGING = "agent.logging.trace";
static
const char * const APACHE_TEST_CONNECTION = "agent.apache.test.connection";
static
const char * const APACHE_PAA_VERSION = "1.5.0";
static
const char * const CACHE_TYPE_PROPERTY = "agent.cache.type";
static
const char * const AUTO_TYPE = "AUTO";
static
const char * const ZMQ_TYPE = "ZMQ";
static
const char * const STANDALONE_TYPE = "STANDALONE";
static const int MAX_SERVERS_FOR_ZMQ_CACHE = 16;
static
const char * const PAA_ENABLE_NOTE = "paa-enable-note";
static
const char * const AGENT_INVENTORY_PROPERTY = "agent.inventory";
static
const char * const AGENT_SEND_INVENTORY_PROPERTY = "agent.send.inventory";
static const int INITIAL_INVENTORY_SIZE = 3;
static
const char * const INVENTORY_VERSION = "v";
static
const char * const INVENTORY_TYPE = "t";
static
const char * const INVENTORY_HOSTNAME = "h";
// Globals
// Enable per-module logging configuration, if available
#ifdef APLOG_USE_MODULE
APLOG_USE_MODULE(paa);
#else
extern module AP_DECLARE_DATA paa_module;
#endif
static
const paa_cache *cache = NULL;
static
server_rec *global_server_log_handle = NULL;
static
apr_threadkey_t *thread_key = NULL;
static
int paa_trace_logging_enabled = 0;
static
int paa_apache_monitoring_disabled = 0; // enabled by default
static
int paa_test_pa_connection = 1; // enabled by default
static
apr_table_t *agent_inventory = NULL;
// Data structures //
typedef enum {
PAA_CACHE_ZMQ,
PAA_CACHE_STANDALONE
} paa_cache_type_e;
typedef
struct paa_req_config_struct {
const paa_agent_response *agent_resp;
} paa_req_config;
typedef
struct paa_server_config_struct {
apr_array_header_t *properties_files;
const char *cert_dir;
const char *cert_path;
const paa_config *config;
const paa_http_client *http_client;
paa_cache_type_e configured_cache_type;
const char *paa_enabled_note_name;
} paa_server_config;
/* This config will be created for the global config
and VirualHost cases. */
typedef
struct paa_dir_config_struct {
int paa_enabled;
} paa_dir_config;
// Prototypes //
void paa_ap_log_error(
const char *file,
int line,
int level,
apr_status_t status,
const server_rec *s,
const char *fmt,
const char *msg
);
void paa_ap_log_rerror(
const char *file,
int line,
int level,
apr_status_t status,
const request_rec *r,
const char *fmt,
const char *msg
);
int paa_get_log_level_from_server_rec(server_rec *srec);
void apache_paa_log_msg(const char *file,
int line,
apr_pool_t *pool,
const char *msgid,
paa_log_level level,
apr_status_t error_code,
const char *format,
va_list va_args);
paa_log_level apache_paa_get_log_level();
void test_pingaccess_connection(const paa_http_client *client, apr_pool_t *parent_pool);
apr_status_t validate_cache_config(paa_server_config *server_config, apr_pool_t *pool);
static
int parse_enable_note(const char *paa_enable_note);
static
apr_table_t *configure_agent_inventory(const paa_config *config,
apr_pool_t *pool,
const char *hostname);
static
apr_table_t *apache_paa_inventory_cb(const paa_config *config,
const paa_client_request *req);
// Apache-specific library function implementations //
static const char * const NORMAL_MSG_FORMAT = "[paa] %s";
static const char * const MONITOR_MSG_FORMAT = "[paa-monitoring] %s";
/**
* The header_parser hook for the PAA module--essentially the "main" function for the module.
*
* @param r the request record for the current request
*
* @return the standard result of the hook
*/
static
int paa_header_parser(request_rec *r)
{
apache_client_req req_wrapper;
req_wrapper.rec = r;
req_wrapper.eos_reached = 0;
req_wrapper.full_normalized_uri = NULL;
paa_client_request client_req;
apache_client_req_init(&client_req, &req_wrapper);
apache_client_resp resp_wrapper;
resp_wrapper.rec = r;
paa_client_response client_resp;
apache_client_resp_init(&client_resp, &resp_wrapper);
int hook_result;
do {
apr_status_t result;
paa_server_config *server_config =
(paa_server_config *)ap_get_module_config(r->server->module_config, &paa_module);
paa_dir_config *dir_config =
(paa_dir_config *)ap_get_module_config(r->per_dir_config, &paa_module);
if (server_config == NULL) {
paa_log(r->pool, APACHE_MSGID, PAA_ERROR, "failed to obtain PAA server configuration");
hook_result = HTTP_INTERNAL_SERVER_ERROR;
break;
}
if (dir_config == NULL) {
paa_log(r->pool, APACHE_MSGID, PAA_ERROR, "failed to obtain PAA directory configuration");
hook_result = HTTP_INTERNAL_SERVER_ERROR;
break;
}
apr_table_t *notes = r->notes;
const char *paa_enable_note_value = apr_table_get(notes, server_config->paa_enabled_note_name);
int enable_note = -1;
if(paa_enable_note_value != NULL){
enable_note = parse_enable_note(paa_enable_note_value);
}
// In order to log the request context, paa_pa_log_rerror must be used.
if((enable_note == 0) || (enable_note == -1 && !dir_config->paa_enabled)){
paa_ap_log_rerror(__FILE__, __LINE__, APLOG_DEBUG, APR_SUCCESS, r,
NORMAL_MSG_FORMAT, "agent not enabled");
hook_result = OK;
break;
}
if (cache == NULL) {
paa_log(r->pool, APACHE_MSGID, PAA_ERROR,
"PAA cache unavailable, refusing to process request");
hook_result = HTTP_INTERNAL_SERVER_ERROR;
break;
}
if (server_config->http_client == NULL) {
paa_log(r->pool, APACHE_MSGID, PAA_ERROR,
"PAA HTTP client was not initialized, refusing to process request");
hook_result = HTTP_INTERNAL_SERVER_ERROR;
break;
}
if (thread_key == NULL) {
paa_log(r->pool, APACHE_MSGID, PAA_ERROR, "thread private key unavailable");
hook_result = HTTP_INTERNAL_SERVER_ERROR;
break;
}
result = apr_threadkey_private_set(r, thread_key);
if (result != APR_SUCCESS) {
paa_log_error(r->pool, APACHE_MSGID, PAA_ERROR, result,
"failed to set thread private key");
hook_result = HTTP_INTERNAL_SERVER_ERROR;
break;
}
// create the normalized uri and store it for later use
char *normalized_uri = apr_psprintf(r->pool, "%s", r->parsed_uri.path);
if (normalized_uri == NULL) {
paa_log_error(r->pool, APACHE_MSGID, PAA_ERROR, APR_ENOMEM,
"failed to create normalized uri");
hook_result = HTTP_INTERNAL_SERVER_ERROR;
break;
}
ap_no2slash(normalized_uri);
req_wrapper.full_normalized_uri = normalized_uri;
const paa_agent_response *agent_resp = NULL;
result = paa_submit_agent_request(r->pool,
server_config->http_client,
server_config->config,
cache,
&client_req,
&client_resp,
&agent_resp);
if (result != APR_SUCCESS) {
paa_log_error(r->pool, APACHE_MSGID, PAA_ERROR,
result, "failed to submit agent request");
hook_result = HTTP_INTERNAL_SERVER_ERROR;
break;
}
if (paa_agent_response_req_allowed(agent_resp)) {
result = paa_client_request_modify(r->pool, &client_req, agent_resp);
if (result != APR_SUCCESS) {
paa_log_error(r->pool, APACHE_MSGID, PAA_ERROR,
result, "failed to modify client request");
hook_result = HTTP_INTERNAL_SERVER_ERROR;
break;
}
// Attach the agent response to the request to modify the response headers later on
paa_req_config *req_config = (paa_req_config *)
apr_palloc(r->pool, sizeof(paa_req_config));
if (req_config == NULL) {
paa_log_error(r->pool, APACHE_MSGID, PAA_ERROR,
APR_ENOMEM, "failed to allocate request config");
hook_result = HTTP_INTERNAL_SERVER_ERROR;
break;
}
req_config->agent_resp = agent_resp;
ap_set_module_config(r->request_config, &paa_module, req_config);
}else{
// sanity check for an issue where the response wasn't sent
if (r->status == 0) {
paa_log(r->pool, APACHE_MSGID, PAA_ERROR, "response from PingAccess not injected");
hook_result = HTTP_INTERNAL_SERVER_ERROR;
break;
}
// submit_agent_request internally has modified the client response
paa_log(r->pool, APACHE_MSGID, PAA_DEBUG, "agent response sent verbatim to client");
hook_result = DONE;
break;
}
hook_result = OK;
}while(0);
apr_threadkey_private_set(NULL, thread_key);
return hook_result;
}
apr_status_t paa_set_response_headers(request_rec *r)
{
apr_status_t result;
const paa_req_config *req_config;
req_config =
(paa_req_config*)ap_get_module_config(r->request_config, &paa_module);
if (req_config != NULL && req_config->agent_resp != NULL) {
apache_client_resp response_wrapper;
response_wrapper.rec = r;
paa_client_response client_resp;
apache_client_resp_init(&client_resp, &response_wrapper);
if (paa_agent_response_modifies_response(req_config->agent_resp)) {
result = paa_client_response_modify(r->pool, &client_resp,
req_config->agent_resp);
if (result != APR_SUCCESS) {
paa_log_error(r->pool, APACHE_MSGID, PAA_ERROR,
result, "failed to modify response headers");
}
}else{
result = APR_SUCCESS;
paa_log(r->pool, APACHE_MSGID, PAA_DEBUG,
"agent response being returned verbatim, not modifying headers");
}
}else{
paa_log(r->pool, APACHE_MSGID, PAA_DEBUG,
"agent response dictates NO changes to client response");
result = APR_SUCCESS;
}
return result;
}
static
int parse_enable_note(const char *paa_enable_note)
{
if (strcmp(paa_enable_note, "on") == 0) {
return 1;
}
if (strcmp(paa_enable_note, "off") == 0) {
return 0;
}
return -1;
}
apr_status_t paa_set_response_headers_filter(ap_filter_t *filter, apr_bucket_brigade *in)
{
apr_status_t result;
do {
result = apr_threadkey_private_set(filter->r, thread_key);
if (result != APR_SUCCESS) {
paa_log_error(filter->r->pool, APACHE_MSGID,
PAA_ERROR, result, "failed to set thread private key");
break;
}
result = paa_set_response_headers(filter->r);
if (result != APR_SUCCESS) {
break;
}
}while(0);
if (result != APR_SUCCESS) {
apr_bucket *error_bucket = ap_bucket_error_create(HTTP_INTERNAL_SERVER_ERROR,
NULL, filter->r->pool, filter->c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(in, error_bucket);
}
// This is a one-shot filter that runs on the first input brigade only
ap_remove_output_filter(filter);
apr_threadkey_private_set(NULL, thread_key);
return ap_pass_brigade(filter->next, in);
}
void paa_insert_filters(request_rec *r)
{
paa_req_config *req_config = (paa_req_config *)
ap_get_module_config(r->request_config, &paa_module);
if (req_config != NULL
&& req_config->agent_resp != NULL
&& paa_agent_response_modifies_response(req_config->agent_resp))
{
// Only insert filter if modifications to the response headers need to occur
ap_add_output_filter(SET_RESPONSE_HEADERS_FILTER, NULL, r, r->connection);
}
}
void paa_insert_error_filters(request_rec *r)
{
paa_req_config *req_config = (paa_req_config *)
ap_get_module_config(r->request_config, &paa_module);
if (req_config != NULL &&
req_config->agent_resp != NULL &&
paa_agent_response_modifies_response(req_config->agent_resp))
{
// Only insert filter if modifications to the response headers need to occur
ap_add_output_filter(SET_RESPONSE_HEADERS_FILTER, NULL, r, r->connection);
}
}
static
apr_status_t log_handle_cleanup(void *handledata)
{
USE(handledata);
global_server_log_handle = NULL;
return APR_SUCCESS;
}
static
void set_global_log_handle(apr_pool_t *pool, server_rec *s)
{
global_server_log_handle = s;
apr_pool_cleanup_register(pool, global_server_log_handle, log_handle_cleanup, apr_pool_cleanup_null);
}
static
void noop_threadkey_free(void *unused)
{
USE(unused);
}
void paa_child_init(apr_pool_t *pool, server_rec *s)
{
set_global_log_handle(pool, s);
apr_status_t result;
do {
result = apr_threadkey_private_create(&thread_key, noop_threadkey_free, pool);
if (result != APR_SUCCESS) {
paa_log_error(pool, APACHE_MSGID, PAA_ERROR, result,
"failed to create thread private key");
break;
}
paa_server_config *server_config =
(paa_server_config *)ap_get_module_config(s->module_config, &paa_module);
if (server_config == NULL) {
paa_log(pool, APACHE_MSGID, PAA_ERROR, "failed to obtain PAA server configuration");
break;
}
char *errmsg = NULL;
result = paa_http_client_curl_create(pool,
server_config->config,
server_config->cert_path,
&errmsg,
&(server_config->http_client));
if (result != APR_SUCCESS) {
paa_log_error(pool, APACHE_MSGID, PAA_ERROR, result,
"failed to initialize PAA HTTP client: %s",
errmsg);
break;
}
test_pingaccess_connection(server_config->http_client, pool);
if (server_config->configured_cache_type == PAA_CACHE_ZMQ) {
result = paa_cache_zmq_create(pool,
server_config->config,
&cache);
paa_log(pool, APACHE_MSGID, PAA_INFO, "using caching mechanism ZMQ");
} else {
result = paa_cache_standalone_create(pool,
server_config->config,
&cache);
paa_log(pool, APACHE_MSGID, PAA_INFO, "using caching mechanism STANDALONE");
}
if (result != APR_SUCCESS) {
paa_log_error(pool, APACHE_MSGID, PAA_ERROR, result,
"failed to initialize PAA policy cache");
break;
}
// copy all global config to each virtual host
server_rec *virt = s->next;
while (virt != NULL)
{
paa_server_config *virt_config =
(paa_server_config *)ap_get_module_config(virt->module_config, &paa_module);
// Only update the virtual host config if it differs from main server config
if (virt_config != server_config)
{
virt_config->http_client = server_config->http_client;
virt_config->properties_files = server_config->properties_files;
virt_config->cert_dir = server_config->cert_dir;
virt_config->cert_path = server_config->cert_path;
virt_config->config = server_config->config;
virt_config->paa_enabled_note_name = server_config->paa_enabled_note_name;
// Don't set paa_enabled, this is a per-virtual host setting
}
virt = virt->next;
}
result = APR_SUCCESS;
}while(0);
if (result != APR_SUCCESS) {
cache = NULL;
}
}
static
void test_conn_status_cb(unsigned int status, const char *reason, void *userdata)
{
USE(status);
USE(reason);
USE(userdata);
}
static
void test_conn_header_cb(const char *name, const char *value, void *userdata)
{
USE(name);
USE(value);
USE(userdata);
}
static
size_t test_conn_body_cb(const unsigned char *src,
size_t size,
void *userdata)
{
USE(src);
USE(userdata);
return size;
}
static
size_t test_conn_request_body_cb(unsigned char *dst, size_t size, void *userdata)
{
USE(dst);
USE(size);
USE(userdata);
return 0;
}
static
paa_client_request_read_cb test_conn_get_read_cb(const paa_client_request *req)
{
USE(req);
return test_conn_request_body_cb;
}
static
void * test_conn_get_read_data(const paa_client_request *req)
{
USE(req);
return NULL;
}
/**
* Tests the connection to the PingAccess policy server using the specified HTTP client
*
* @param client the client
* @param parent_pool the parent pool in which a subpool will be created
*/
void test_pingaccess_connection(const paa_http_client *client, apr_pool_t *parent_pool)
{
if (paa_test_pa_connection) {
apr_status_t result;
apr_pool_t *request_pool = NULL;
do {
result = apr_pool_create(&request_pool, parent_pool);
if (result != APR_SUCCESS) {
break;
}
const paa_http_context *context = NULL;
result = client->create_context(client, request_pool, &context);
if (result != APR_SUCCESS) {
break;
}
context->set_resp_pool(context, request_pool);
context->set_resp_status_cb(context, test_conn_status_cb);
context->set_resp_header_cb(context, test_conn_header_cb);
context->set_resp_body_cb(context, test_conn_body_cb);
const paa_http_request *request = NULL;
result = context->create_request_handle(context, &request);
if (result != APR_SUCCESS) {
break;
}
// Configure the request to be sent to the PingAccess policy server heartbeat endpoint
request->set_include_client_request_body(request, 0);
request->set_request_uri(request, "/pa/heartbeat.ping");
request->set_request_method(request, HTTP_GET_METHOD);
paa_client_request req;
req.get_read_cb = test_conn_get_read_cb;
req.get_read_data = test_conn_get_read_data;
result = context->send_request(context, &req);
if (result != APR_SUCCESS) {
break;
}
paa_log(request_pool, APACHE_MSGID, PAA_DEBUG,
"successfully connected to PingAccess policy server");
}while(0);
if (result != APR_SUCCESS) {
paa_log_error(parent_pool, APACHE_MSGID, PAA_ERROR, result,
"failed to connect to PingAccess policy server");
}
if (request_pool != NULL) {
apr_pool_destroy(request_pool);
request_pool = NULL;
}
}
}
int paa_post_config(apr_pool_t *pool, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
{
USE(plog);
apr_status_t result;
set_global_log_handle(pool, s);
// Initialize the configuration
paa_server_config *server_config =
(paa_server_config *)ap_get_module_config(s->module_config, &paa_module);
if (server_config == NULL) {
paa_log(pool, APACHE_MSGID, PAA_ERROR, "failed to retrieve PAA server config");
return HTTP_INTERNAL_SERVER_ERROR;
}
if (server_config->properties_files->nelts <= 0) {
paa_log(pool, APACHE_MSGID, PAA_ERROR, "PAA properties files were not specified");
return HTTP_INTERNAL_SERVER_ERROR;
}
if (server_config->cert_dir == NULL) {
paa_log(pool, APACHE_MSGID, PAA_ERROR,
"Directory for PAA trust certificates was not specified");
return HTTP_INTERNAL_SERVER_ERROR;
}
const char **prop_files_array = (const char **)apr_palloc(ptemp,
sizeof(const char *)*server_config->properties_files->nelts);
int i;
for (i = 0; i < server_config->properties_files->nelts; ++i) {
prop_files_array[i] = ((const char **)server_config->properties_files->elts)[i];
}
char *errmsg = NULL;
result = paa_config_filesystem_create(pool,
prop_files_array,
server_config->properties_files->nelts,
&errmsg,
&(server_config->config));
if (result != APR_SUCCESS) {
paa_log_error(pool, APACHE_MSGID, PAA_ERROR, result,
"failed to initialize PAA configuration: %s",
errmsg);
return HTTP_INTERNAL_SERVER_ERROR;
}
if (validate_cache_config(server_config, pool) != APR_SUCCESS) {
return HTTP_INTERNAL_SERVER_ERROR;
}
result = paa_curl_create_cert_file(pool,
server_config->config,
server_config->cert_dir,
&(server_config->cert_path));
if (result != APR_SUCCESS && !APR_STATUS_IS_EEXIST(result)) {
paa_log_error(pool, APACHE_MSGID, PAA_ERROR, result,
"failed to create trust store certificate file");
return HTTP_INTERNAL_SERVER_ERROR;
}
if (APR_STATUS_IS_EEXIST(result)) {
// This is an acceptable case, translate it into APR_SUCCESS
result = APR_SUCCESS;
}
// Enable trace logging -- if specified
const char *trace_logging = server_config->config->get_value(server_config->config,
PAA_TRACE_LOGGING);
if (trace_logging != NULL) {
char *invalid = NULL;
paa_trace_logging_enabled = (int)apr_strtoi64(trace_logging, &invalid, 10);
if (invalid == NULL || *invalid != '\0') {
paa_log(ptemp, APACHE_MSGID, PAA_ERROR, "failed to enable trace logging");
paa_trace_logging_enabled = 0;
// proceed with processing, this is not critical
}else{
paa_log(ptemp, APACHE_MSGID,
PAA_INFO, "trace logging set to = %d", paa_trace_logging_enabled);
}
}
agent_inventory = configure_agent_inventory(server_config->config, pool, s->server_hostname);
if (agent_inventory == NULL || !paa_validate_inventory(pool, agent_inventory)) {
paa_log_error(pool, APACHE_MSGID, PAA_ERROR, result,
"invalid agent inventory value");
return HTTP_INTERNAL_SERVER_ERROR;
}
const char *test_connection = server_config->config->get_value(server_config->config,
APACHE_TEST_CONNECTION);
if (test_connection != NULL) {
char *invalid = NULL;
paa_test_pa_connection = (int)apr_strtoi64(test_connection, &invalid, 10);
if (invalid == NULL || *invalid != '\0') {
// fallback to the default value
paa_test_pa_connection = 1;
paa_log(pool, APACHE_MSGID, PAA_ERROR, "invalid test PingAccess connection value");
// proceed with processing, this is not critical
}else{
paa_log(ptemp, APACHE_MSGID, PAA_INFO,
"test PingAccess connection set to = %d", paa_test_pa_connection);
}
}
// Disable monitoring -- if specified
const char *monitoring_disable = server_config->config->get_value(server_config->config,
APACHE_DISABLE_MONITORING);
if (monitoring_disable != NULL) {
char *invalid = NULL;
paa_apache_monitoring_disabled = (int)apr_strtoi64(monitoring_disable, &invalid, 10);
if (invalid == NULL || *invalid != '\0') {
paa_apache_monitoring_disabled = 0;
paa_log(pool, APACHE_MSGID, PAA_ERROR, "invalid monitoring disabled value");
return HTTP_INTERNAL_SERVER_ERROR;
}else{
paa_log(ptemp, APACHE_MSGID, PAA_INFO,
"monitoring disable set to = %d", paa_apache_monitoring_disabled);
}
}
paa_log(ptemp, APACHE_MSGID, PAA_INFO, "PAA SDK version %s",
paa_get_version());
paa_log(ptemp, APACHE_MSGID, PAA_INFO, "Apache PAA version %s",
APACHE_PAA_VERSION);
return OK;
}
apr_status_t validate_cache_config(paa_server_config *server_config, apr_pool_t *pool)
{
const char *cache_type;
const char *errmsg;
cache_type = server_config->config->get_value(server_config->config, CACHE_TYPE_PROPERTY);
if (cache_type != NULL) {
if (strcmp(AUTO_TYPE, cache_type) != 0 &&
strcmp(ZMQ_TYPE, cache_type) != 0 &&
strcmp(STANDALONE_TYPE, cache_type) != 0)
{
errmsg = apr_psprintf(pool, "invalid %s value in PingAccess agent properties file, "
"must be one of %s, %s, or %s",
CACHE_TYPE_PROPERTY,
AUTO_TYPE,
ZMQ_TYPE,
STANDALONE_TYPE);
paa_log(pool, APACHE_MSGID, PAA_ERROR, errmsg);
return APR_EINVAL;
}
}
if (cache_type == NULL || strcmp(AUTO_TYPE, cache_type) == 0) {
int max_servers;
apr_status_t result;
result = ap_mpm_query(AP_MPMQ_MAX_DAEMONS, &max_servers);
if (result == APR_SUCCESS) {
server_config->configured_cache_type = max_servers > 1 && max_servers < MAX_SERVERS_FOR_ZMQ_CACHE ?
PAA_CACHE_ZMQ : PAA_CACHE_STANDALONE;
} else {
server_config->configured_cache_type = PAA_CACHE_ZMQ;
errmsg = apr_psprintf(pool, "unable to determine server limit, using ZMQ caching");
paa_log(pool, APACHE_MSGID, PAA_WARN, errmsg);
}
} else if (strcmp(ZMQ_TYPE, cache_type) == 0) {
server_config->configured_cache_type = PAA_CACHE_ZMQ;
} else {
server_config->configured_cache_type = PAA_CACHE_STANDALONE;
}
return APR_SUCCESS;
}
void paa_register_hooks(apr_pool_t *p)
{
USE(p);
paa_set_log_functions(apache_paa_log_msg, apache_paa_get_log_level);
paa_set_inventory_cb(apache_paa_inventory_cb);
// Register filters
ap_register_output_filter(SET_RESPONSE_HEADERS_FILTER, paa_set_response_headers_filter,
NULL, AP_FTYPE_CONTENT_SET);
// Register hooks
//ap_hook_header_parser
ap_hook_header_parser(paa_header_parser, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_insert_filter(paa_insert_filters, NULL, NULL, APR_HOOK_LAST);
ap_hook_insert_error_filter(paa_insert_error_filters, NULL, NULL, APR_HOOK_LAST);
ap_hook_post_config(paa_post_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_child_init(paa_child_init, NULL, NULL, APR_HOOK_MIDDLE);
}
static
void *paa_create_server_config(apr_pool_t *pool, server_rec *s)
{
USE(s);
paa_server_config *server_config =
(paa_server_config *)apr_pcalloc(pool, sizeof(paa_server_config));
server_config->cert_dir = NULL;
server_config->config = NULL;
server_config->properties_files = apr_array_make(pool, 3, sizeof(char *));
server_config->paa_enabled_note_name = PAA_ENABLE_NOTE;
return server_config;
}
static
void *paa_create_dir_config(apr_pool_t *pool, char *dir)
{
USE(dir);
paa_dir_config *dir_config = (paa_dir_config*) apr_pcalloc(pool, sizeof(paa_dir_config));
dir_config->paa_enabled = -1;
return dir_config;
}
static
void *paa_merge_dir_config(apr_pool_t *p, void *base_conf, void *new_conf)
{
paa_dir_config *base_dir_config = (paa_dir_config *)base_conf;
paa_dir_config *new_dir_config = (paa_dir_config *)new_conf;
paa_dir_config *dir_config = (paa_dir_config *)paa_create_dir_config(p, "Merged configuration.");
dir_config->paa_enabled = (new_dir_config->paa_enabled == -1) ?
base_dir_config->paa_enabled : new_dir_config->paa_enabled;
return dir_config;
}
static
const char *set_property_files(cmd_parms *parms, void *unused, const char *arg)
{
USE(unused);
// This directive is only valid in the global context (not within a virtual host)
const char *errmsg = ap_check_cmd_context(parms, GLOBAL_ONLY);
if (errmsg != NULL) {
return errmsg;
}
server_rec *s = parms->server;
paa_server_config *server_config =
(paa_server_config *)ap_get_module_config(s->module_config, &paa_module);
void *prop_file_path = apr_array_push(server_config->properties_files);
// The value of the property can be relative or absolute and this function will resolve the
// file accordingly
return ap_set_file_slot(parms, prop_file_path, arg);
}
static
const char *set_cert_dir(cmd_parms *parms, void *unused, const char *arg)
{
USE(unused);
// This directive is only valid in the global context (not within a virtual host)
const char *errmsg = ap_check_cmd_context(parms, GLOBAL_ONLY);
if (errmsg != NULL) {
return errmsg;
}
server_rec *s = parms->server;
paa_server_config *server_config =
(paa_server_config *)ap_get_module_config(s->module_config, &paa_module);
// The value of the property can be relative or absolute and this function will resolve the
// file accordingly
return ap_set_file_slot(parms, &(server_config->cert_dir), arg);
}
static
const char *set_paa_enabled_note_name(cmd_parms *parms, void *unused, const char *arg)
{
USE(unused);
const char *errmsg = ap_check_cmd_context(parms, GLOBAL_ONLY);
if (errmsg != NULL) {
return errmsg;
}
server_rec *s = parms->server;
paa_server_config *server_config =
(paa_server_config *)ap_get_module_config(s->module_config, &paa_module);