-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinuxCCP.c
executable file
·1107 lines (1006 loc) · 37.2 KB
/
LinuxCCP.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
/*********************************************************
* LinuxCCP.c
* Web services wrapper to CyberArk Credential Provider
* to broker access to applications via REST call
* and process client-cert authentication
*
*********************************************************
*
* NOTE: Currently requires Shared Logon authentication to
* PVWA API using incredibly privileged user (must have List
* Safe Members access for all Safes that require credentials
* to be retrieved from and Audit Users authorization), due to
* the lack of PSDKWebserviceRequest method for any SDK that
* is supported on a non-Windows server environment
*
*********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <pthread.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#include <cyberark/cpasswordsdk.h>
#include <ulfius.h>
// TODO: Parameterize PORT, SERVICE_APPID and CYBR_PVWA via
// configuration environment variables
#define PORT 9500
#define CYBR_PVWA "https://components.cyberarkdemo.com/PasswordVault/"
#define CYBR_SL_URI "WebServices/auth/Shared/RestfulAuthenticationService.svc/Logon"
#define CYBR_APP_URI "/WebServices/PIMServices.svc/Applications/"
#define CYBR_AUTH_METHOD_URI "/Authentications"
#define CYBR_ACCT_URI "api/Accounts?search="
#define CYBR_SAFE_URI "WebServices/PIMServices.svc/Safes/"
#define CYBR_SAFE_MEMBERS_URI "/Members"
#define SERVICE_APPID "ProvAuth"
/*
* Struct to track all Safes that Provider will have access to
* and JSON objects containing the Safe's members and their level
* of access to the Safe
*/
typedef struct {
char *safe;
int num_members;
json_t **members;
} cybr_safe_members;
/*
* Struct to track all active Applications within the environment
* and the available Authentication Methods that are attached to
* each one
*/
typedef struct {
char *appid;
int num_methods;
int num_serial;
int num_certattr;
int num_ip;
json_t **serial;
json_t **certattr;
json_t **ip;
} cybr_auth_methods;
// Mutex variables for locking access to specific variables between main
// thread and maintenance threads running
pthread_mutex_t lock_auth;
pthread_mutex_t lock_num_request;
pthread_mutex_t lock_kill;
// Boolean value set to 1 when application ends. Used to
// kill authentication thread infinite loop
bool shutdown_app = false;
// Tracker for number of requests
int num_request = 0;
// Declare global variables for the Application Authentication
// methods and Safe Members, along with counter variables for
// tracking the number of elements in each
int num_aml=0;
int num_sml=0;
cybr_safe_members *safe_members_list = NULL;
cybr_auth_methods *auth_methods_list = NULL;
/*
* Utility function to read the contents of a file
* into a string buffer, then return the string
*/
char *read_file(const char *filename) {
char *buffer;
long len;
FILE *f = fopen(filename, "rb");
if (f) {
fseek(f, 0, SEEK_END);
len = ftell(f);
fseek(f, 0, SEEK_SET);
buffer = malloc(len+1);
if (buffer) {
fread(buffer, 1, len, f);
buffer[len] = '\0';
}
fclose(f);
}
return buffer;
}
/*
* Utility function to convert binary values to the
* corresponding hex value and return it as a string; Used by
* the certificate serial number extraction method
*/
static const char *bin2hex(const void *bin, size_t bin_size) {
static char printable[110];
const unsigned char *_bin = bin;
char *print;
size_t i;
// Restrict the size of the binary to 50 bytes
if (bin_size > 50)
bin_size = 50;
print = printable;
// Loop through byte by byte and convert each byte
// from binary to hex
for (i = 0; i < bin_size; i++) {
sprintf(print, "%.2x ", _bin[i]);
print += 2;
}
return printable;
}
/*
* Method to authenticate to CyberArk's PVWA API and return a
* string containing the authentication token on successful
* login
*/
char *cybr_api_sharedlogon() {
char *token;
struct _u_map headers;
struct _u_request req;
struct _u_response res;
json_t *res_body;
int err;
// Define a map object containing HTTP request headers
u_map_init(&headers);
u_map_put(&headers, "Content-Length", "0");
u_map_put(&headers, "Content-Type", "application/json");
// Initialize the HTTP request and populate with the appropriate
// request data
ulfius_init_request(&req);
ulfius_set_empty_body_request(&req);
req.http_verb = strdup("POST");
req.http_url = strdup(CYBR_PVWA CYBR_SL_URI);
req.check_server_certificate = 0;
u_map_copy_into(req.map_header, &headers);
// Initialize the HTTP response object and send HTTP request
ulfius_init_response(&res);
err = ulfius_send_http_request(&req, &res);
if (err == U_OK) {
// Populate the response JSON data into a variable, copy the
// portion of the JSON object containing the "LogonResult" key
// to a temporary JSON variable, and extract the token string
// to be returned
res_body = ulfius_get_json_body_response(&res, NULL);
json_t *object = json_deep_copy(json_object_get(res_body, "LogonResult"));
token = strdup(json_string_value(object));
// Release all allocated memory prior to return
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
json_decref(res_body);
json_decref(object);
return token;
}
else {
// Release all allocated memory prior to return
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
return NULL;
}
}
// TODO: Comment the rest of the PVWA function definitions....
// YOU KNOW WHAT THESE ALL DO!!!
json_t *cybr_api_get_apps(char *token) {
struct _u_map headers;
struct _u_request req;
struct _u_response res;
json_t *res_body;
char *url;
int err, url_len;
// Define a map object containing HTTP request headers
u_map_init(&headers);
u_map_put(&headers, "Content-Type", "application/json");
u_map_put(&headers, "Authorization", token);
// Allocate memory for the URL string
url_len = strlen(CYBR_PVWA CYBR_APP_URI);
url = malloc(url_len + 1);
strcpy(url, CYBR_PVWA CYBR_APP_URI);
ulfius_init_request(&req);
ulfius_set_empty_body_request(&req);
req.http_verb = strdup("GET");
req.http_url = strdup(url);
req.check_server_certificate = 0;
u_map_copy_into(req.map_header, &headers);
free(url);
ulfius_init_response(&res);
err = ulfius_send_http_request(&req, &res);
if (err == U_OK) {
if (res.status != 200) {
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
return NULL;
}
res_body = ulfius_get_json_body_response(&res, NULL);
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
json_t *temp_body = json_deep_copy(json_object_get(res_body, "application"));
json_decref(res_body);
return temp_body;
}
else {
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
return NULL;
}
}
json_t *cybr_api_get_safes(char* token) {
struct _u_map headers;
struct _u_request req;
struct _u_response res;
json_t *res_body;
char *url;
int err, url_len;
u_map_init(&headers);
u_map_put(&headers, "Content-Type", "application/json");
u_map_put(&headers, "Authorization", token);
url_len = strlen(CYBR_PVWA CYBR_SAFE_URI);
url = malloc(url_len + 1);
strcpy(url, CYBR_PVWA CYBR_SAFE_URI);
ulfius_init_request(&req);
ulfius_set_empty_body_request(&req);
req.http_verb = strdup("GET");
req.http_url = strdup(url);
req.check_server_certificate = 0;
u_map_copy_into(req.map_header, &headers);
free(url);
ulfius_init_response(&res);
err = ulfius_send_http_request(&req, &res);
if (err == U_OK) {
if (res.status != 200) {
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
return NULL;
}
res_body = ulfius_get_json_body_response(&res, NULL);
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
json_t *temp_body = json_deep_copy(json_object_get(res_body, "GetSafesSlashResult"));
json_decref(res_body);
return temp_body;
}
else {
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
return NULL;
}
}
json_t *cybr_api_get_safe_members(char *token, char *safe_name) {
struct _u_map headers;
struct _u_request req;
struct _u_response res;
json_t *res_body;
char *url;
int err, url_len;
u_map_init(&headers);
u_map_put(&headers, "Content-Type", "application/json");
u_map_put(&headers, "Authorization", token);
url_len = strlen(CYBR_PVWA CYBR_SAFE_URI CYBR_SAFE_MEMBERS_URI) + strlen(safe_name);
url = malloc(url_len + 1);
strcpy(url, CYBR_PVWA CYBR_SAFE_URI);
strcat(url, safe_name);
strcat(url, CYBR_SAFE_MEMBERS_URI);
ulfius_init_request(&req);
ulfius_set_empty_body_request(&req);
req.http_verb = strdup("GET");
req.http_url = strdup(url);
req.check_server_certificate = 0;
u_map_copy_into(req.map_header, &headers);
free(url);
ulfius_init_response(&res);
err = ulfius_send_http_request(&req, &res);
if (err == U_OK) {
if (res.status != 200) {
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
return NULL;
}
res_body = ulfius_get_json_body_response(&res, NULL);
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
json_t *temp_body = json_deep_copy(json_object_get(res_body, "members"));
json_decref(res_body);
return temp_body;
}
else {
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
return NULL;
}
}
json_t *cybr_api_get_auth_methods(char *token, char *appid) {
struct _u_map headers;
struct _u_request req;
struct _u_response res;
json_t *res_body;
char *url;
int err, url_len;
u_map_init(&headers);
u_map_put(&headers, "Content-Type", "application/json");
u_map_put(&headers, "Authorization", token);
url_len = strlen(CYBR_PVWA CYBR_APP_URI CYBR_AUTH_METHOD_URI) + strlen(appid);
url = malloc(url_len + 1);
strcpy(url, CYBR_PVWA CYBR_APP_URI);
strcat(url, appid);
strcat(url, CYBR_AUTH_METHOD_URI);
ulfius_init_request(&req);
ulfius_set_empty_body_request(&req);
req.http_verb = strdup("GET");
req.http_url = strdup(url);
req.check_server_certificate = 0;
u_map_copy_into(req.map_header, &headers);
free(url);
ulfius_init_response(&res);
err = ulfius_send_http_request(&req, &res);
if (err == U_OK) {
if (res.status != 200) {
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
return NULL;
}
res_body = ulfius_get_json_body_response(&res, NULL);
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
return res_body;
}
else {
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
return NULL;
}
}
json_t *cybr_api_get_account(char *token, char *account_object) {
struct _u_map headers;
struct _u_request req;
struct _u_response res;
json_t *res_body;
char *url;
int err, url_len;
u_map_init(&headers);
u_map_put(&headers, "Content-Type", "application/json");
u_map_put(&headers, "Authorization", token);
url_len = strlen(CYBR_PVWA CYBR_ACCT_URI) + strlen(account_object);
url = malloc(url_len + 1);
strcpy(url, CYBR_PVWA CYBR_ACCT_URI);
strcat(url, account_object);
ulfius_init_request(&req);
ulfius_set_empty_body_request(&req);
req.http_verb = strdup("GET");
req.http_url = strdup(url);
req.check_server_certificate = 0;
u_map_copy_into(req.map_header, &headers);
free(url);
ulfius_init_response(&res);
err = ulfius_send_http_request(&req, &res);
if (err == U_OK) {
if (res.status != 200) {
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
return NULL;
}
res_body = ulfius_get_json_body_response(&res, NULL);
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
json_t *temp_body = json_deep_copy(json_object_get(res_body, "value"));
json_t *temp_body2 = json_deep_copy(json_array_get(temp_body, 0));
json_decref(res_body);
json_decref(temp_body);
return temp_body2;
}
else {
ulfius_clean_request(&req);
ulfius_clean_response(&res);
u_map_clean(&headers);
return NULL;
}
}
/*
* Clean up all allocated resources associated with the
* auth_methods_list
*/
void free_auth_methods_list() {
for(int i=0; i<num_aml; i++) {
free(auth_methods_list[i].appid);
for(int j=0; j<auth_methods_list[i].num_ip; j++)
json_decref(auth_methods_list[i].ip[j]);
for(int j=0; j<auth_methods_list[i].num_serial; j++)
json_decref(auth_methods_list[i].serial[j]);
for(int j=0; j<auth_methods_list[i].num_certattr; j++)
json_decref(auth_methods_list[i].certattr[j]);
free(auth_methods_list[i].serial);
free(auth_methods_list[i].certattr);
free(auth_methods_list[i].ip);
}
free(auth_methods_list);
}
/*
* Clean up all allocated resources associated with the
* safe_members_list
*/
void free_safe_members_list() {
for(int i=0; i<num_sml; i++) {
free(safe_members_list[i].safe);
for(int j=0; j<safe_members_list[i].num_members; j++)
json_decref(safe_members_list[i].members[j]);
free(safe_members_list[i].members);
}
free(safe_members_list);
}
void load_authentication() {
char *token;
json_t *applications, *account, *safes, *safe_members;
cybr_safe_members *tmp_safe_members_list;
cybr_auth_methods *tmp_auth_methods_list;
int tmp_num_aml=0, tmp_num_sml=0;
token = cybr_api_sharedlogon();
if(token) {
// Retrieve all AppIDs from the PVWA API
applications = cybr_api_get_apps(token);
if(applications) {
// Allocate memory for the the new auth methods list and
// get the number of AppIDs that were returned
tmp_auth_methods_list = malloc(sizeof(cybr_auth_methods) * json_array_size(applications));
tmp_num_aml = json_array_size(applications);
for (int i=0; i<json_array_size(applications); i++) {
// Initialize the various auth methods containers for the given AppID
tmp_auth_methods_list[i].num_certattr = 0;
tmp_auth_methods_list[i].certattr = NULL;
tmp_auth_methods_list[i].num_serial = 0;
tmp_auth_methods_list[i].serial = NULL;
tmp_auth_methods_list[i].num_ip = 0;
tmp_auth_methods_list[i].ip = NULL;
tmp_auth_methods_list[i].appid = strdup(json_string_value(json_object_get(json_array_get(applications, i), "AppID")));
// Retrieve the authentication methods for the AppID from the PVWA API
// and copy the inner object within the "authentication" key
json_t* temp_methods = cybr_api_get_auth_methods(token, tmp_auth_methods_list[i].appid);
json_t* auth_methods = json_deep_copy(json_object_get(temp_methods, "authentication"));
json_decref(temp_methods);
tmp_auth_methods_list[i].num_methods = json_array_size(auth_methods);
if (tmp_auth_methods_list[i].num_methods > 0) {
for (int j=0; j<tmp_auth_methods_list[i].num_methods; j++) {
// Check if the given auth method is a serial number, and
// handle the appropriate logic to store a serial number type
// auth method
if (!strcmp(json_string_value(json_object_get(json_array_get(auth_methods, j), "AuthType")), "certificateSerialNumber")) {
tmp_auth_methods_list[i].num_serial++;
tmp_auth_methods_list[i].serial = realloc(tmp_auth_methods_list[i].serial, sizeof(json_t) * tmp_auth_methods_list[i].num_serial);
tmp_auth_methods_list[i].serial[tmp_auth_methods_list[i].num_serial-1] = json_deep_copy(json_array_get(auth_methods, j));
}
// Check if the given auth method is an IP address, and
// handle the appropriate logic to store an IP address type
// auth method
else if (!strcmp(json_string_value(json_object_get(json_array_get(auth_methods, j), "AuthType")), "machineAddress")) {
tmp_auth_methods_list[i].num_ip++;
tmp_auth_methods_list[i].ip = realloc(tmp_auth_methods_list[i].ip, sizeof(json_t) * tmp_auth_methods_list[i].num_ip);
tmp_auth_methods_list[i].ip[tmp_auth_methods_list[i].num_ip-1] = json_deep_copy(json_array_get(auth_methods, j));
}
// Check if the given auth method is a certificate attribute
// (Subject, Issuer, SAN), and handle the appropriate logic
// to store a serial number type auth method
else if (!strcmp(json_string_value(json_object_get(json_array_get(auth_methods, j), "AuthType")), "certificateattr")) {
tmp_auth_methods_list[i].num_certattr++;
tmp_auth_methods_list[i].certattr = realloc(tmp_auth_methods_list[i].certattr, sizeof(json_t) * tmp_auth_methods_list[i].num_certattr);
tmp_auth_methods_list[i].certattr[tmp_auth_methods_list[i].num_certattr-1] = json_deep_copy(json_array_get(auth_methods, j));
}
}
}
json_decref(auth_methods);
}
json_decref(applications);
// Lock the auth_methods_list variable if it is already populated
// so that it cannot be accessed by another thread, free the already
// allocated auth_method_list, and point it to the newly allocated
// auth_method_list
pthread_mutex_lock(&lock_auth);
if (auth_methods_list != NULL) {
free_auth_methods_list();
auth_methods_list = NULL;
}
num_aml = tmp_num_aml;
auth_methods_list = tmp_auth_methods_list;
tmp_auth_methods_list = NULL;
pthread_mutex_unlock(&lock_auth);
}
// Retrieve all Safes from the PVWA API
safes = cybr_api_get_safes(token);
if(safes) {
// Allocate memory for the safe_members_list based on the number of Safes returned
tmp_safe_members_list = malloc(sizeof(cybr_safe_members) * json_array_size(safes));
tmp_num_sml = json_array_size(safes);
for(int i=0; i<json_array_size(safes); i++) {
// Copy Safe name into safe member list struct
tmp_safe_members_list[i].safe = strdup(json_string_value(json_object_get(json_array_get(safes, i), "SafeName")));
// Retrieve all Safe Members for a given Safe from PVWA API
safe_members = cybr_api_get_safe_members(token, tmp_safe_members_list[i].safe);
tmp_safe_members_list[i].num_members = json_array_size(safe_members);
if (tmp_safe_members_list[i].num_members > 0) {
// Allocate memory for the Safe Members and copy each member into the structure
tmp_safe_members_list[i].members = malloc(sizeof(json_t) * tmp_safe_members_list[i].num_members);
for (int j=0; j<tmp_safe_members_list[i].num_members; j++) {
tmp_safe_members_list[i].members[j] = json_deep_copy(json_array_get(safe_members, j));
}
}
json_decref(safe_members);
}
json_decref(safes);
// Lock the safe_members_list variable if it is already populated
// so that it cannot be accessed by another thread, free the already
// allocated safe_members_list, and point it to the newly allocated
// safe_members_list
pthread_mutex_lock(&lock_auth);
if (safe_members_list != NULL) {
free_safe_members_list();
safe_members_list = NULL;
}
num_sml = tmp_num_sml;
safe_members_list = tmp_safe_members_list;
tmp_safe_members_list = NULL;
pthread_mutex_unlock(&lock_auth);
}
free(token);
}
}
/*
* Thread function that is used to periodically update
* the loaded safe members list authentication methods
* for the AppIDs that are available
*/
void *load_auth_thread_loop() {
bool temp = 0;
// Loop until the application has been killed
// Sleep for 60 seconds, then reload the authentication
// methods
while (!temp) {
// Wait for one minute
sleep(60);
load_authentication();
// Lock the shutdown_app variable, so that it cannot be overwritten
// Copy it to temp
pthread_mutex_lock(&lock_kill);
temp = shutdown_app;
pthread_mutex_unlock(&lock_kill);
}
}
/*
* Utility function to convert a CIDR bitmask
* to its corresponding netmask string and return it
*/
char *cidr_to_netmask(char *cidr) {
uint32_t i_netmask;
struct in_addr netmask;
char *end;
int i_cidr = strtol(cidr, &end, 10);
i_netmask = 0xFFFFFFFF;
if (i_cidr < 32)
i_netmask <<= 32 - i_cidr;
i_netmask = htnol(i_netmask);
netmask.s_addr = i_netmask;
return inet_ntoa(netmask);
}
/***
* Return variable containing contents of file
*/
/*
* Function to process the provided AppID's allowed authentication
* methods and Safe membership ACLs to determine if it is allowed
* to retrieve the requested password object. Returns non-zero if
* successful.
*/
bool validate_appid_access(char *appid, char* safename, struct sockaddr *client_address, gnutls_x509_crt_t client_cert) {
// Extract IP address from provided socket object
struct sockaddr_in *address = (struct sockaddr_in *)client_address;
char *ip_address = strdup(inet_ntoa(address->sin_addr));
char serial[40];
size_t size;
gnutls_x509_subject_alt_name_t *san;
size_t san_size=0;
int app_index=-1;
int res;
bool valid=true;
// Locate the provided AppID in the pre-loaded list of
// authentication methods
for (int i=0; i<num_aml; i++) {
if(!strcmp(auth_methods_list[i].appid, appid)) {
app_index=i;
break;
}
}
// If there are any authentication methods for the provided AppID
// for Allowed Machines, loop through all allowed IP address and CIDR
// ranges to determine if the requestor address matches any
if (auth_methods_list[app_index].num_ip > 0) {
bool ipfound=false;
for(int i=0; i<auth_methods_list[app_index].num_ip; i++) {
// Extract the address or CIDR range from the JSON object
char *ip_auth = strdup(json_string_value(json_object_get(auth_methods_list[app_index].ip[i], "AuthValue")));
char *saveptr = NULL;
// Check if there is a CIDR range, and if so isolate the bitmask from the network address
char* cidr = strtok_r(ip_auth, "/", &saveptr);
// No CIDR range, so the extracted value is an IP address
if(!strcmp(saveptr, "")) {
if(!strcmp(ip_auth, ip_address)) {
ipfound=true;
break;
}
}
else {
// Convert the CIDR bitmask to a full IP netmask
struct in_addr netmask_ip, network_ip, requestor_ip;
ip_auth = strdup(cidr);
inet_aton(ip_auth, &network_ip);
cidr = strtok_r(NULL, "/", &saveptr);
inet_aton(cidr_to_netmask(cidr), &netmask_ip);
inet_aton(ip_address, &requestor_ip);
// Verify that the requestor IP address falls within the network address
if((requestor_ip.s_addr & netmask_ip.s_addr) == (network_ip.s_addr & netmask_ip.s_addr)) {
ipfound=true;
break;
}
}
}
if(!ipfound)
valid = false;
}
// Extract the serial number from the presented mTLS certificate
size = sizeof(serial);
gnutls_x509_crt_get_serial(client_cert, serial, &size);
// Check if any of the authentication methods for the AppID use
// certificate serial numbers, and if so extract the serial number
// from the JSON object and validate against the presented mTLS certificate's
// serial number
if (auth_methods_list[app_index].num_serial > 0) {
bool serialfound = false;
for (int i=0; i<auth_methods_list[app_index].num_serial; i++) {
char *serial_number = strdup(json_string_value(json_object_get(auth_methods_list[app_index].serial[i], "AuthValue")));
if(!strcmp(bin2hex(serial, size), serial_number)) {
serialfound = true;
break;
}
}
if(!serialfound)
valid = false;
}
// Locate the index of the Safe Member's list for the Safe
// that matches the requested passwords object's Safe
for (int i=0; i<num_sml; i++) {
if(!strcmp(safe_members_list[i].safe, safename)) {
app_index = i;
break;
}
}
// Ensure there is at least one Safe Member for this Safe, then
// validate that the AppID user has been added to the Safe
if (safe_members_list[app_index].num_members > 0) {
bool safematch = false;
for (int i=0; i<safe_members_list[app_index].num_members; i++) {
if (!strcmp(json_string_value(json_object_get(safe_members_list[app_index].members[i], "UserName")), appid))
safematch = true;
}
}
// TODO: Check for and validate SAN, Issuer, and Subject authentication methods
/*do {
res = gnutls_x509_crt_get_subject_alt_name (client_cert, i, san, &san_size, NULL);
if (res == GNUTLS_E_SHORT_MEMORY_BUFFER) {
san = malloc(san_size+1);
res = gnutls_x509_crt_get_subject_alt_name (client_cert, i, san, &san_size, NULL);
}
if (res == GNUTLS_SAN_IPADDRESS) {
struct in_addr addr;
char *temp = malloc(san_size + 1);
char *temp2 = malloc(san_size+1);
strncpy(temp, (char*) san, san_size);
for (int j=0; j<san_size; j++) {
temp2[j] = temp[san_size-j-1];
}
memcpy(san, temp2, san_size);
addr.s_addr = htonl((unsigned int) *san);
printf("%s\n", inet_ntoa(addr));
}
else if (res == GNUTLS_SAN_DNSNAME || res == GNUTLS_SAN_URI || res == GNUTLS_SAN_RFC822NAME)
printf("%s\n", (char*)san);
i++;
} while(res != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);*/
free(ip_address);
return valid;
}
/**
* Using CyberArk's C Password SDK for AAM CP, retrieve
* the requested password object. Store the request credential
* and additional properties in a JSON object that will be returned
* to the caller.
*/
json_t *get_password(char *query) {
json_t *response = json_object();
ObjectHandle psdkResponse = NULL;
ObjectHandle psdkRequest = NULL;
char **pVals = NULL;
int err = 0;
// Create a new password request
psdkRequest = PSDK_CreateRequest("PASSWORD");
if(psdkRequest != NULL) {
// Set request AppID, and appropriate error handling
err = PSDK_SetAttribute(psdkRequest, "AppDescs.AppID", "ProvAuth");
if (err == PSDK_RC_ERROR) {
printf("Error setting AppID\n");
printf("Error Code: %d - %s\n", PSDK_GetErrorCode(psdkRequest), PSDK_GetErrorMsg(psdkRequest));
PSDK_ReleaseHandle(&psdkRequest);
return NULL;
}
//// Set request Query, and appropriate error handling
err = PSDK_SetAttribute(psdkRequest, "Query", query);
if (err == PSDK_RC_ERROR) {
printf("Error setting query\n");
printf("Error Code: %d - %s\n", PSDK_GetErrorCode(psdkRequest), PSDK_GetErrorMsg(psdkRequest));
PSDK_ReleaseHandle(&psdkRequest);
return NULL;
}
// Attempt to get the password from the CP
err = PSDK_GetPassword(psdkRequest, &psdkResponse);
if (err == PSDK_RC_ERROR) {
printf("Error retrieving password\n");
printf("Error Code: %d - %s", PSDK_GetErrorCode(psdkRequest), PSDK_GetErrorMsg(psdkRequest));
PSDK_ReleaseHandle(&psdkRequest);
return NULL;
}
else {
// Retrieve the password value, and return an error if the password is unable to be retrieved.
// Set the response object with the key/value pair for the password
pVals = PSDK_GetAttribute(psdkResponse, "Password");
if (pVals) {
json_object_set_new(response, "Password", json_pack("s", pVals[0]));
PSDK_ReleaseAttributeData(&pVals);
}
else
printf("Error Code: %d - %s", PSDK_GetErrorCode(psdkResponse), PSDK_GetErrorMsg(psdkResponse));
// Return the additional password properties for the requested object, if they exist
// Sets appropriate key/values for the retrieved properties in the response object
pVals = PSDK_GetAttribute(psdkResponse, "PassProps.UserName");
if (pVals) {
json_object_set_new(response, "UserName", json_pack("s", pVals[0]));
PSDK_ReleaseAttributeData(&pVals);
}
pVals = PSDK_GetAttribute(psdkResponse, "PassProps.Address");
if (pVals) {
json_object_set_new(response, "Address", json_pack("s", pVals[0]));
PSDK_ReleaseAttributeData(&pVals);
}
pVals = PSDK_GetAttribute(psdkResponse, "PassProps.Database");
if (pVals) {
json_object_set_new(response, "Database", json_pack("b", pVals[0]));
PSDK_ReleaseAttributeData(&pVals);
}
pVals = PSDK_GetAttribute(psdkResponse, "PasswordChangeInProcess");
if (pVals) {
bool pwdChange;
if (!strncmp(pVals[0], "true", strlen(pVals[0])))
pwdChange = true;
else
pwdChange = false;
json_object_set_new(response, "PasswordChangeInProcess", json_pack("b", pwdChange));
PSDK_ReleaseAttributeData(&pVals);
}
}
}
if(psdkRequest)
PSDK_ReleaseHandle(&psdkRequest);
if(psdkResponse)
PSDK_ReleaseHandle(&psdkResponse);
return response;
}
/**
* Callback function for the web application on /HTTPUsage url call
*/
int c_http_usage(const struct _u_request * request, struct _u_response * response, void * user_data) {
int temp;
char str[20];
// Lock num_request, assign value to a temporary variable, and reset to 0
pthread_mutex_lock(&lock_num_request);
temp=num_request;
num_request=0;
pthread_mutex_unlock(&lock_num_request);
// Convert the number of requests to a string and then pass as the HTTP response
sprintf(str, "%d", temp);
ulfius_set_string_body_response(response, 403, str);
return U_CALLBACK_CONTINUE;
}
/**
* Callback function for the web application on /GetPassword url call
*/
int c_get_password (const struct _u_request * request, struct _u_response * response, void * user_data) {
json_t *body = ulfius_get_json_body_request(request, NULL);
json_t *appid, *data, *query, *query_fmt, *object, *safe, *folder, *username,
*address, *database, *policy_id, *timeout, *fail_on_change, *password;
char *str_query, *str_query_fmt = NULL, *str_appid, *temp, *safename=NULL;
// Lock and increment total HTTP request count
pthread_mutex_lock(&lock_num_request);
++num_request;
pthread_mutex_unlock(&lock_num_request);
// Initialize str_query as an empty null-terminated string
str_query = malloc(1);
strcpy(str_query, "");
// Attempt to get App ID from request JSON, and validate that it actually exists
// Return error if it is not presented, otherwise copy it to a string variable
appid = json_object_get(body, "appId");
if (!json_is_string(appid)) {
ulfius_set_string_body_response(response, 400, "appId required");
json_decref(body);
return U_CALLBACK_CONTINUE;
}
str_appid = malloc(strlen(json_string_value(appid)) + 1);
strcpy(str_appid, json_string_value(appid));
// Attempt to get data JSON object from request JSON, throw error if it does not exist
data = json_object_get(body, "data");
if (!json_is_object(data)) {
ulfius_set_string_body_response(response, 400, "Request Message content is invalid");
json_decref(body);
return U_CALLBACK_CONTINUE;
}
// If "Query" key was sent in data, set query to that string and ignore everything else
query = json_object_get(data, "Query");
if (query && json_is_string(query)) {
str_query = malloc(strlen(json_string_value(query)) + 1);
strcpy(str_query, json_string_value(query));
// Check to see if a format for the query was set
// Default is for exact, so only testing for regex
// Only needs to be done in the event that
query_fmt = json_object_get(data, "QueryFormat");
if (query_fmt && json_is_string(query_fmt)) {
if(!strcmp(json_string_value(query_fmt), "Regexp")) {
str_query_fmt = malloc(7);
strcpy(str_query_fmt, "Regexp");
}
}
// Look for a Safe name in the query (to be used to validate AppID safe membership)
// If Safe name is found, store it to be used
char *safelocate, *saveptr;
safelocate=strtok_r(str_query, ";", &saveptr);
while(safelocate != NULL) {
if(strstr(safelocate, "Safe"))
break;
}
if(safelocate) {
char *temp = strtok_r(safelocate, "=", &saveptr);
safename = strdup(saveptr);
}
else
ulfius_set_string_body_response(response, 400, "Safe is a required query parameter\n\n");
}
// If no query string passed in request, check for other properties to create query
else {
// Check for "Object" key, and if it exists append the value to the query string
object = json_object_get(data, "Object");
if (object && json_is_string(object)) {
temp = malloc(strlen(json_string_value(object)) + 10);
strcpy(temp, "Object=");
strcat(temp, json_string_value(object));
strcat(temp, ";");
str_query = realloc(str_query, strlen(str_query) + strlen(temp) + 1);
strcat(str_query, temp);
free(temp);
}
// Check for "Safe" key, and if it exists append the value to the query string
safe = json_object_get(data, "Safe");
if (safe && json_is_string(safe)) {
temp = malloc(strlen(json_string_value(safe)) + 7);
safename = strdup(json_string_value(safe));
strcpy(temp, "Safe=");
strcat(temp, json_string_value(safe));
strcat(temp, ";");
str_query = realloc(str_query, strlen(str_query) + strlen(temp) + 1);
strcat(str_query, temp);
free(temp);
}
// Check for "Folder" key, and if it exists append the value to the query string
folder = json_object_get(data, "Folder");
if (folder && json_is_string(folder)) {
temp = malloc(strlen(json_string_value(folder)) + 9);
strcpy(temp, "Folder=");
strcat(temp, json_string_value(folder));
strcat(temp, ";");
str_query = realloc(str_query, strlen(str_query) + strlen(temp) + 1);
strcat(str_query, temp);
free(temp);
}
}
// If no valid safe name, set appropriate response