/
zxidconf.c
2094 lines (1882 loc) · 73 KB
/
zxidconf.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
/* zxidconf.c - Handwritten functions for parsing ZXID configuration file
* Copyright (c) 2009-2011 Sampo Kellomaki (sampo@iki.fi), All Rights Reserved.
* Copyright (c) 2006-2009 Symlabs (symlabs@symlabs.com), All Rights Reserved.
* Author: Sampo Kellomaki (sampo@iki.fi)
* This is confidential unpublished proprietary source code of the author.
* NO WARRANTY, not even implied warranties. Contains trade secrets.
* Distribution prohibited unless authorized in writing.
* Licensed under Apache License 2.0, see file COPYING.
* $Id: zxidconf.c,v 1.51 2010-01-08 02:10:09 sampo Exp $
*
* 12.8.2006, created --Sampo
* 16.1.2007, split from zxidlib.c --Sampo
* 27.3.2007, lazy reading of certificates --Sampo
* 22.2.2008, added path_supplied feature --Sampo
* 7.10.2008, added documentation --Sampo
* 29.8.2009, added Auto-Cert feature a.k.a. zxid_mk_self_signed_cert() --Sampo
* 4.9.2009, added NEED, WANT, INMAP, PEPMAP, OUTMAP, and ATTRSRC --Sampo
* 15.11.2009, added SHOW_CONF (o=d) option --Sampo
* 7.1.2010, added WSC and WSP signing options --Sampo
* 12.2.2010, added pthread locking --Sampo
* 31.5.2010, added 4 web service call PEPs --Sampo
* 21.4.2011, fixed DSA key reading and reading unqualified keys --Sampo
* 3.12.2011, added VPATH feature --Sampo
* 10.12.2011, added VURL and BUTTON_URL, deleted ORG_URL except for legacy check --Sampo
*/
#include "platform.h" /* needed on Win32 for pthread_mutex_lock() et al. */
#include <memory.h>
#include <string.h>
#ifdef USE_CURL
#include <curl/curl.h>
#endif
#include "errmac.h"
#include "zxid.h"
#include "zxidutil.h"
#include "zxidconf.h"
#include "zxidpriv.h"
#include "c/zxidvers.h"
/* ============== Configuration ============== */
/* Eventually configuration will be read from some file, but for
* now, we settle for compilation time configuration, see zxidconf.h */
#ifdef USE_OPENSSL
#include <openssl/rand.h>
#include <openssl/x509.h>
#include <openssl/rsa.h>
#if 0
/*(-) Compute raw SHA1 digest hash over contents of a file.
*
* cf:: ZXID configuration object, used for deteminin path prefix and for memory allocation
* name:: Name of the file (under hierarchy defined by PATH configuration option)
* sha1:: A sha1 buffer which should be exactly 20 bytes (160 bits) long. The
* buffer will be modified in place by this function. */
/* Called by: zxid_init_conf */
void zxid_sha1_file(zxid_conf* cf, char* name, char* sha1)
{
int gotall;
char* buf;
ZERO(sha1, 20);
buf = read_all_alloc(cf->ctx, "sha1_file", 1, &gotall, "%s%s", cf->path, name);
if (!buf)
return;
SHA1(buf, gotall, sha1);
ZX_FREE(cf->ctx, buf);
}
#endif
/*() Extract a certificate from PEM encoded string. */
/* Called by: opt, test_mode, zxid_read_cert */
X509* zxid_extract_cert(char* buf, char* name)
{
X509* x = 0; /* Forces d2i_X509() to alloc the memory. */
char* p;
char* e;
OpenSSL_add_all_algorithms();
p = strstr(buf, PEM_CERT_START);
if (!p) {
ERR("No certificate found in file(%s)\n", name);
return 0;
}
p += sizeof(PEM_CERT_START) - 1;
if (*p == 0xd) ++p;
if (*p != 0xa) return 0;
++p;
e = strstr(buf, PEM_CERT_END);
if (!e) return 0;
p = unbase64_raw(p, e, buf, zx_std_index_64);
if (!d2i_X509(&x, (const unsigned char**)&buf /* *** compile warning */, p-buf) || !x) {
ERR("DER decoding of X509 certificate failed.\n%d", 0);
return 0;
}
return x;
}
/*() Extract a private key from PEM encoded string.
* *** This function needs to expand to handle DSA */
/* Called by: opt, test_mode, zxid_read_private_key */
EVP_PKEY* zxid_extract_private_key(char* buf, char* name)
{
char* p;
char* e;
int typ;
EVP_PKEY* pk = 0; /* Forces d2i_PrivateKey() to alloc the memory. */
OpenSSL_add_all_algorithms();
if (p = strstr(buf, PEM_RSA_PRIV_KEY_START)) {
typ = EVP_PKEY_RSA;
e = PEM_RSA_PRIV_KEY_END;
} else if (p = strstr(buf, PEM_DSA_PRIV_KEY_START)) {
typ = EVP_PKEY_DSA;
e = PEM_DSA_PRIV_KEY_END;
} else if (p = strstr(buf, PEM_PRIV_KEY_START)) { /* Not official format, but sometimes seen. */
typ = EVP_PKEY_RSA;
e = PEM_PRIV_KEY_END;
} else {
ERR("No private key found in file(%s). Looking for separator (%s) or (%s).\npem data(%s)", name, PEM_RSA_PRIV_KEY_START, PEM_DSA_PRIV_KEY_START, buf);
return 0;
}
p += sizeof(PEM_RSA_PRIV_KEY_START) - 1;
if (*p == 0xd) ++p;
if (*p != 0xa) return 0;
++p;
e = strstr(buf, e);
if (!e) return 0;
p = unbase64_raw(p, e, buf, zx_std_index_64);
if (!d2i_PrivateKey(typ, &pk, (const unsigned char**)&buf, p-buf) || !pk) {
ERR("DER decoding of private key failed.\n%d", 0);
return 0;
}
return pk; /* RSA* rsa = EVP_PKEY_get1_RSA(pk); */
}
/*() Extract a certificate from PEM encoded file. */
/* Called by: zxid_idp_sso_desc x2, zxid_init_conf x3, zxid_lazy_load_sign_cert_and_pkey, zxid_sp_sso_desc x2, zxlog_write_line */
X509* zxid_read_cert(zxid_conf* cf, char* name)
{
char buf[4096];
int got = read_all(sizeof(buf), buf, "read_cert", 1, "%s" ZXID_PEM_DIR "%s", cf->path, name);
if (!got && cf->auto_cert)
zxid_mk_self_sig_cert(cf, sizeof(buf), buf, "read_cert", name);
return zxid_extract_cert(buf, name);
}
/*() Extract a private key from PEM encoded file. */
/* Called by: test_ibm_cert_problem x2, test_ibm_cert_problem_enc_dec x2, zxenc_privkey_dec, zxid_init_conf x3, zxid_lazy_load_sign_cert_and_pkey, zxlog_write_line x2 */
EVP_PKEY* zxid_read_private_key(zxid_conf* cf, char* name)
{
char buf[4096];
int got = read_all(sizeof(buf), buf, "read_private_key", 1, "%s" ZXID_PEM_DIR "%s", cf->path, name);
if (!got && cf->auto_cert)
zxid_mk_self_sig_cert(cf, sizeof(buf), buf, "read_private_key", name);
return zxid_extract_private_key(buf, name);
}
/*() Lazy load signing certificate and private key. This reads them from disk
* if needed. If they do not exist and auto_cert is enabled, they will be
* generated on disk and the read. Once read from disk, they will be cached in
* memory. */
/* Called by: zxid_anoint_a7n, zxid_anoint_sso_resp, zxid_az_soap x3, zxid_idp_soap_dispatch x2, zxid_idp_sso, zxid_mk_art_deref, zxid_mk_at_cert, zxid_saml2_post_enc, zxid_saml2_redir_enc, zxid_sp_mni_soap, zxid_sp_slo_soap, zxid_sp_soap_dispatch x7, zxid_ssos_anreq, zxid_wsf_sign */
int zxid_lazy_load_sign_cert_and_pkey(zxid_conf* cf, X509** cert, EVP_PKEY** pkey, const char* logkey)
{
LOCK(cf->mx, logkey);
if (cert) {
if (!(*cert = cf->sign_cert)) // Lazy load cert and private key
*cert = cf->sign_cert = zxid_read_cert(cf, "sign-nopw-cert.pem");
}
if (!(*pkey = cf->sign_pkey))
*pkey = cf->sign_pkey = zxid_read_private_key(cf, "sign-nopw-cert.pem");
UNLOCK(cf->mx, logkey);
if (cert && !*cert || !*pkey)
return 0;
return 1;
}
#endif
/*() Set obscure options of ZX and ZXID layers. Used to set debug options.
* Generally setting these options is not supported, but this function
* exists to avoid uncontrolled access to global variables. At least this
* way the unsupported activity will happen in one controlled place where
* it can be ignored, if need to be. You have been warned. */
/* Called by: zxid_fed_mgmt_cf, zxid_idp_list_cf_cgi, zxid_simple_cf_ses */
int zxid_set_opt(zxid_conf* cf, int which, int val)
{
switch (which) {
case 1: zx_debug = val; return val;
case 5: exit(val); /* This is typically used to force __gcov_flush() */
case 6: zxid_set_opt_cstr(cf, 6, "/var/zxid/log/log.dbg"); return 0;
default: ERR("zxid_set_opt: this version " ZXID_REL " does not support which=%d val=%d (ignored)", which, val);
}
return -1;
}
/*() Set obscure options of ZX and ZXID layers. Used to set debug options.
* Generally setting these options is not supported, but this function
* exists to avoid uncontrolled access to global variables. At least this
* way the unsupported activity will happen in one controlled place where
* it can be ignored, if need to be. You have been warned. */
/* Called by: */
char* zxid_set_opt_cstr(zxid_conf* cf, int which, char* val)
{
char buf[PATH_MAX];
switch (which) {
case 2: strncpy(zx_instance, val, sizeof(zx_instance)); return zx_instance;
case 3: D_INDENT(val); return zx_indent;
case 4: D_DEDENT(val); return zx_indent;
case 6:
D("Forwarding debug output to file(%s) cwd(%s)", STRNULLCHK(val), getcwd(buf, sizeof(buf)));
zx_debug_log = fopen(val, "a");
if (!zx_debug_log) {
perror("zxid_set_opt_cstr: failed to open new log file");
fprintf(stderr, "zxid_set_opt_cstr: failed to open new log file(%s), euid=%d egid=%d cwd(%s)", STRNULLCHK(val), geteuid(), getegid(), getcwd(buf, sizeof(buf)));
exit(1);
}
INFO("zxid_set_opt_cstr: opened new log file(%s), rel=" ZXID_REL " euid=%d egid=%d cwd(%s)", STRNULLCHK(val), geteuid(), getegid(), getcwd(buf, sizeof(buf)));
return "";
default: ERR("zxid_set_opt_cstr: this version " ZXID_REL " does not support which=%d val(%s) (ignored)", which, STRNULLCHK(val));
}
return 0;
}
/*() Set the URL configuration variable. Special accessor function to
* manipulate URL config option. Manipulating this option is common in
* virtual hosting situations - hence this convenience function. You
* could use zxid_parse_conf() instead to manipulate URL and some other
* options. */
/* Called by: main x2, zxidwspcgi_main */
void zxid_url_set(zxid_conf* cf, const char* url)
{
if (!cf || !url) {
ERR("NULL pointer as cf or url argument cf=%p url=%p", cf, url);
return;
}
D("Setting url(%s)", url);
cf->url = zx_dup_cstr(cf->ctx, url);
}
/* ================== Attribute Broker Config ================*/
#define IS_RULE(rule, val) (!memcmp((rule), (val), sizeof(val)-1) && (rule)[sizeof(val)-1] == '$')
/*() Create new (common pool) attribute and add it to a linked list */
/* Called by: zxid_add_at_values x3, zxid_add_attr_to_ses x2, zxid_add_qs_to_ses, zxid_load_atsrc, zxid_load_need */
struct zxid_attr* zxid_new_at(zxid_conf* cf, struct zxid_attr* at, int name_len, char* name, int val_len, char* val, char* lk)
{
struct zxid_attr* aa = ZX_ZALLOC(cf->ctx, struct zxid_attr);
aa->n = at;
at = aa;
COPYVAL(at->name, name, name+name_len);
if (val)
COPYVAL(at->val, val, val+val_len);
D("%s:\tATTR name(%.*s)=val(%.*s)", lk, name_len, name, MIN(val_len, 100), STRNULLCHK(val));
return aa;
}
/*() Reverse of zxid_new_at(). */
void zxid_free_at(struct zxid_conf *cf, struct zxid_attr *attr)
{
while (attr) {
struct zxid_attr *next = attr->n;
ZX_FREE(cf->ctx, attr->name);
if (attr->val) ZX_FREE(cf->ctx, attr->val);
ZX_FREE(cf->ctx, attr);
attr = next;
}
}
/*() Parse need specification and add it to linked list
* A,B$usage$retention$oblig$ext;A,B$usage$retention$oblig$ext;...
*/
/* Called by: zxid_init_conf x2, zxid_parse_conf_raw x2 */
struct zxid_need* zxid_load_need(zxid_conf* cf, struct zxid_need* need, char* v)
{
char* attrs;
char* usage;
char* retent;
char* oblig;
char* ext;
char* p = v;
char* a;
int len;
struct zxid_need* nn;
while (p && *p) {
attrs = p;
p = strchr(p, '$');
if (!p) {
ERR("Malformed NEED or WANT directive: attribute list at pos %d", ((int)(p-v)));
return need;
}
usage = ++p;
p = strchr(p, '$');
if (!p) {
ERR("Malformed NEED or WANT directive: usage missing at pos %d", ((int)(p-v)));
return need;
}
retent = ++p;
p = strchr(p, '$');
if (!p) {
ERR("Malformed NEED or WANT directive: retention missing at pos %d", ((int)(p-v)));
return need;
}
oblig = ++p;
p = strchr(p, '$');
if (!p) {
ERR("Malformed NEED or WANT directive: obligations missing at pos %d", ((int)(p-v)));
return need;
}
ext = ++p;
p = strchr(p, ';'); /* Stanza ends in separator ; or end of string nul */
if (!p)
p = ext + strlen(ext);
if (IS_RULE(usage, "reset")) {
INFO("Reset need %p", need);
zxid_free_need(cf, need);
need = 0;
if (!*p) break;
++p;
continue;
}
nn = ZX_ZALLOC(cf->ctx, struct zxid_need);
nn->n = need;
need = nn;
COPYVAL(nn->usage, usage, retent-1);
COPYVAL(nn->retent, retent, oblig-1);
COPYVAL(nn->oblig, oblig, ext-1);
COPYVAL(nn->ext, ext, p);
DD("need attrs(%.*s) usage(%s) retent(%s) oblig(%s) ext(%s)", usage-attrs-1, attrs, nn->usage, nn->retent, nn->oblig, nn->ext);
for (a = attrs; ; a += len+1) {
len = strcspn(a, ",$");
nn->at = zxid_new_at(cf, nn->at, len, a, 0,0, "need/want");
if (a[len] == '$')
break;
}
if (!*p) break;
++p;
}
return need;
}
/*() Reverse of zxid_load_need(). */
void zxid_free_need(struct zxid_conf *cf, struct zxid_need *need)
{
while (need) {
struct zxid_need *next = need->n;
ZX_FREE(cf->ctx, need->usage);
ZX_FREE(cf->ctx, need->retent);
ZX_FREE(cf->ctx, need->oblig);
ZX_FREE(cf->ctx, need->ext);
zxid_free_at(cf, need->at);
ZX_FREE(cf->ctx, need);
need = next;
}
}
/*() Parse map specification and add it to linked list
* srcns$A$rule$b$ext;src$A$rule$b$ext;...
* The list ends up being built in reverse order, which at runtime
* causes last stanzas to be evaluated first and first match is used.
* Thus you should place most specific rules last and most generic rules first.
* See also: zxid_find_map() and zxid_map_val()
*/
/* Called by: zxid_init_conf x7, zxid_mk_usr_a7n_to_sp, zxid_parse_conf_raw x7, zxid_read_map */
struct zxid_map* zxid_load_map(zxid_conf* cf, struct zxid_map* map, char* v)
{
char* ns;
char* A;
char* rule;
char* b;
char* ext;
char* p = v;
int len;
struct zxid_map* mm;
DD("v(%s)", v);
while (p && *p) {
ns = p;
p = strchr(p, '$');
if (!p) {
ERR("Malformed MAP directive: source namespace missing at pos %d", ((int)(p-v)));
return map;
}
A = ++p;
p = strchr(p, '$');
if (!p) {
ERR("Malformed MAP directive: source attribute name missing at pos %d", ((int)(p-v)));
return map;
}
rule = ++p;
p = strchr(p, '$');
if (!p) {
ERR("Malformed MAP directive: rule missing at pos %d", ((int)(p-v)));
return map;
}
b = ++p;
p = strchr(p, '$');
if (!p) {
ERR("Malformed MAP directive: destination attribute name missing at pos %d", ((int)(p-v)));
return map;
}
ext = ++p;
len = strcspn(p, ";\n"); /* Stanza ends in separator ; or end of string nul */
p = ext + len;
if (IS_RULE(rule, "reset")) {
INFO("Reset map %p", map);
for (; map; map = mm) {
mm = map->n;
ZX_FREE(cf->ctx, map);
}
if (!*p) break;
++p;
continue;
}
mm = ZX_ZALLOC(cf->ctx, struct zxid_map);
mm->n = map;
map = mm;
if (IS_RULE(rule, "") || IS_RULE(rule, "rename")) { mm->rule = ZXID_MAP_RULE_RENAME; }
else if (IS_RULE(rule, "del")) { mm->rule = ZXID_MAP_RULE_DEL; }
else if (IS_RULE(rule, "feidedec")) { mm->rule = ZXID_MAP_RULE_FEIDEDEC; }
else if (IS_RULE(rule, "feideenc")) { mm->rule = ZXID_MAP_RULE_FEIDEENC; }
else if (IS_RULE(rule, "unsb64-inf")) { mm->rule = ZXID_MAP_RULE_UNSB64_INF; }
else if (IS_RULE(rule, "def-sb64")) { mm->rule = ZXID_MAP_RULE_DEF_SB64; }
else if (IS_RULE(rule, "unsb64")) { mm->rule = ZXID_MAP_RULE_UNSB64; }
else if (IS_RULE(rule, "sb64")) { mm->rule = ZXID_MAP_RULE_SB64; }
else if (IS_RULE(rule, "a7n")) { mm->rule = ZXID_MAP_RULE_WRAP_A7N; }
else if (IS_RULE(rule, "a7n-feideenc")) { mm->rule = ZXID_MAP_RULE_WRAP_A7N | ZXID_MAP_RULE_FEIDEENC; }
else if (IS_RULE(rule, "a7n-def-sb64")) { mm->rule = ZXID_MAP_RULE_WRAP_A7N | ZXID_MAP_RULE_DEF_SB64; }
else if (IS_RULE(rule, "a7n-sb64")) { mm->rule = ZXID_MAP_RULE_WRAP_A7N | ZXID_MAP_RULE_SB64; }
else if (IS_RULE(rule, "x509")) { mm->rule = ZXID_MAP_RULE_WRAP_X509; }
else if (IS_RULE(rule, "x509-feideenc")) { mm->rule = ZXID_MAP_RULE_WRAP_X509 | ZXID_MAP_RULE_FEIDEENC; }
else if (IS_RULE(rule, "x509-def-sb64")) { mm->rule = ZXID_MAP_RULE_WRAP_X509 | ZXID_MAP_RULE_DEF_SB64; }
else if (IS_RULE(rule, "x509-sb64")) { mm->rule = ZXID_MAP_RULE_WRAP_X509 | ZXID_MAP_RULE_SB64; }
else if (IS_RULE(rule, "file")) { mm->rule = ZXID_MAP_RULE_WRAP_FILE; }
else if (IS_RULE(rule, "file-feideenc")) { mm->rule = ZXID_MAP_RULE_WRAP_FILE | ZXID_MAP_RULE_FEIDEENC; }
else if (IS_RULE(rule, "file-def-sb64")) { mm->rule = ZXID_MAP_RULE_WRAP_FILE | ZXID_MAP_RULE_DEF_SB64; }
else if (IS_RULE(rule, "file-sb64")) { mm->rule = ZXID_MAP_RULE_WRAP_FILE | ZXID_MAP_RULE_SB64; }
else {
ERR("Unknown map rule(%.*s) at col %d of (%s)", ((int)(b-rule)), rule, ((int)(rule-v)), v);
//ERR("sizeof(rename)=%d cmp=%d c(%c)", sizeof("rename"), memcmp(rule, "rename", sizeof("rename")-1), rule[sizeof("rename")]);
}
COPYVAL(mm->ns, ns, A-1);
COPYVAL(mm->src, A, rule-1);
COPYVAL(mm->dst, b, ext-1);
COPYVAL(mm->ext, ext, p);
DD("map ns(%s) src(%s) rule=%d dst(%s) ext(%s)", mm->ns, mm->src, mm->rule, mm->dst, mm->ext);
if (!*p || *p == '\n') break;
++p;
}
return map;
}
/*() Reverse of zxid_load_map(). */
void zxid_free_map(struct zxid_conf *cf, struct zxid_map *map)
{
while (map) {
struct zxid_map *next = map->n;
ZX_FREE(cf->ctx, map->ns);
ZX_FREE(cf->ctx, map->src);
ZX_FREE(cf->ctx, map->dst);
ZX_FREE(cf->ctx, map->ext);
ZX_FREE(cf->ctx, map);
map = next;
}
}
/*() Parse ATTRSRC specification and add it to linked list
* namespace$A,B$weight$accessparamURL$AAPMLref$otherLim$ext;namespace$A,B$weight$accessparamURL$AAPMLref$otherLim$ext;...
*/
/* Called by: zxid_init_conf x4, zxid_parse_conf_raw x4 */
struct zxid_cstr_list* zxid_load_cstr_list(zxid_conf* cf, struct zxid_cstr_list* l, char* p)
{
char* q;
struct zxid_cstr_list* cs;
for (; p && *p;) {
q = p;
p = strchr(p, ',');
if (!p)
p = q + strlen(q);
cs = ZX_ZALLOC(cf->ctx, struct zxid_cstr_list);
cs->n = l;
l = cs;
COPYVAL(cs->s, q, p);
}
return l;
}
/*() Reverse of zxid_load_cstr_list(). */
void zxid_free_cstr_list(struct zxid_conf* cf, struct zxid_cstr_list* l)
{
while (l) {
struct zxid_cstr_list *next = l->n;
ZX_FREE(cf->ctx, l->s);
ZX_FREE(cf->ctx, l);
l = next;
}
}
/*() Parse ATTRSRC specification and add it to linked list
* namespace$A,B$weight$accessparamURL$AAPMLref$otherLim$ext;namespace$A,B$weight$accessparamURL$AAPMLref$otherLim$ext;...
*/
/* Called by: zxid_init_conf, zxid_parse_conf_raw */
struct zxid_atsrc* zxid_load_atsrc(zxid_conf* cf, struct zxid_atsrc* atsrc, char* v)
{
char* ns;
char* attrs;
char* weight;
char* url;
char* aapml;
char* otherlim;
char* ext;
char* p = v;
char* a;
int len;
struct zxid_atsrc* as;
while (p && *p) {
ns = p;
p = strchr(p, '$');
if (!p) {
ERR("Malformed ATSRC directive: namespace missing at pos %d", ((int)(p-v)));
return atsrc;
}
attrs = ++p;
p = strchr(p, '$');
if (!p) {
ERR("Malformed ATSRC directive: attribute list missing at pos %d", ((int)(p-v)));
return atsrc;
}
weight = ++p;
p = strchr(p, '$');
if (!p) {
ERR("Malformed ATSRC directive: weight missing at pos %d", ((int)(p-v)));
return atsrc;
}
url = ++p;
p = strchr(p, '$');
if (!p) {
ERR("Malformed ATSRC directive: url missing at pos %d", ((int)(p-v)));
return atsrc;
}
aapml = ++p;
p = strchr(p, '$');
if (!p) {
ERR("Malformed ATSRC directive: aapml ref missing at pos %d", ((int)(p-v)));
return atsrc;
}
otherlim = ++p;
p = strchr(p, '$');
if (!p) {
ERR("Malformed ATSRC directive: otherlim missing at pos %d", ((int)(p-v)));
return atsrc;
}
ext = ++p;
p = strchr(p, ';'); /* Stanza ends in separator ; or end of string nul */
if (!p)
p = ext + strlen(ext);
if (IS_RULE(url, "reset")) {
INFO("Reset atsrc %p", atsrc);
zxid_free_atsrc(cf, atsrc);
atsrc = NULL;
if (!*p) break;
++p;
continue;
}
as = ZX_ZALLOC(cf->ctx, struct zxid_atsrc);
as->n = atsrc;
atsrc = as;
COPYVAL(as->ns, ns, attrs-1);
COPYVAL(as->weight, weight, url-1);
COPYVAL(as->url, url, aapml-1);
COPYVAL(as->aapml, aapml, otherlim-1);
COPYVAL(as->otherlim, otherlim, ext-1);
COPYVAL(as->ext, ext, p);
D("atsrc ns(%s) attrs(%.*s) weight(%s) url(%s) aapml(%s) otherlim(%s) ext(%s)", as->ns, ((int)(weight-attrs-1)), attrs, as->weight, as->url, as->aapml, as->otherlim, as->ext);
for (a = attrs; ; a += len+1) {
len = strcspn(a, ",$");
as->at = zxid_new_at(cf, as->at, len, a, 0,0, "atsrc");
if (a[len] == '$')
break;
}
if (!*p) break;
++p;
}
return atsrc;
}
/*() Reverse of zxid_load_atsrc(). */
void zxid_free_atsrc(struct zxid_conf *cf, struct zxid_atsrc *src)
{
while (src) {
struct zxid_atsrc *next = src->n;
zxid_free_at(cf, src->at);
ZX_FREE(cf->ctx, src->ns);
ZX_FREE(cf->ctx, src->weight);
ZX_FREE(cf->ctx, src->url);
ZX_FREE(cf->ctx, src->aapml);
ZX_FREE(cf->ctx, src->otherlim);
ZX_FREE(cf->ctx, src->ext);
ZX_FREE(cf->ctx, src);
src = next;
}
}
/*() Check whether attribute is in a (needed or wanted) list. Just a linear
* scan as it is simple and good enough for handful of attributes. */
/* Called by: zxid_add_at_values x2, zxid_add_attr_to_ses x2 */
struct zxid_need* zxid_is_needed(struct zxid_need* need, const char* name)
{
struct zxid_attr* at;
if (!name || !*name)
return 0;
for (; need; need = need->n)
for (at = need->at; at; at = at->n)
if (at->name[0] == '*' && !at->name[1] /* Wild card */
|| !strcmp(at->name, name)) /* Match! */
return need;
return 0;
}
/*() Check whether attribute is in a (needed or wanted) list. Just a linear
* scan as it is simple and good enough for handful of attributes.
* The list ends up being built in reverse order, which at runtime
* causes last stanzas to be evaluated first and first match is used.
* Thus you should place most specific rules last and most generic rules first.
* See also: zxid_load_map() and zxid_map_val() */
/* Called by: pool2apache, zxid_add_at_values, zxid_add_attr_to_ses, zxid_add_mapped_attr x2, zxid_pepmap_extract, zxid_pool_to_json x2, zxid_pool_to_ldif x2, zxid_pool_to_qs x2 */
struct zxid_map* zxid_find_map(struct zxid_map* map, const char* name)
{
if (!name || !*name)
return 0;
for (; map; map = map->n)
if (map->src[0] == '*' && !map->src[1] /* Wild card (only sensible for del and data xform) */
|| !strcmp(map->src, name)) /* Match! */
return map;
return 0;
}
/*() Check whether name is in the list. Used for Local PDP wite and black lists. */
/* Called by: zxid_localpdp x4 */
struct zxid_cstr_list* zxid_find_cstr_list(struct zxid_cstr_list* cs, const char* name)
{
if (!name || !*name)
return 0;
for (; cs; cs = cs->n)
if (cs->s[0] == '*' && !cs->s[1] /* Wild card */
|| !strcmp(cs->s, name)) /* Match! */
return cs;
return 0;
}
/*() Check whether attribute is in pool. */
/* Called by: zxid_localpdp x2 */
struct zxid_attr* zxid_find_at(struct zxid_attr* pool, const char* name)
{
if (!name || !*name)
return 0;
for (; pool; pool = pool->n)
if (!strcmp(pool->name, name)) /* Match! */
return pool;
return 0;
}
/*() Given URL, return a newly allocated string corresponding
* to the domain name part of the URL. Used to grab fedusername_suffix
* from the url config option. */
/* Called by: zxid_parse_conf_raw */
char* zxid_grab_domain_name(zxid_conf* cf, const char* url)
{
char* dom;
char* p;
int len;
if (!url || !*url)
return 0;
dom = strchr(url, ':');
if (!dom || dom[1] != '/' || dom[2] != '/')
return 0;
dom += 3;
/* After shipping https:// scan for domain name allowable characters. */
len = strspn(dom, ".abcdefghijklmnopqrstuvwxyz0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ");
p = ZX_ALLOC(cf->ctx, len+1);
memcpy(p, dom, len);
p[len] = 0;
return p;
}
pthread_mutex_t zxid_ent_cache_mx;
int zxid_ent_cache_mx_init = 0;
/*(i) Initialize configuration object, which must have already been
* allocated, to factory defaults (i.e. compiled in defaults, see
* zxidconf.h).
*
* cf:: Pointer to previously allocated configuration object
* path:: Since this configuration option is so fundamental, it can
* be supplied directly as argument. However, unlike zxid_new_conf()
* this does not cause the config file to be read.
* return:: 0 on success (currently, 2008, this function can not
* fail - thus it is common to ignore the return value)
*
* N.B. This function does NOT initialize the ZX context object although
* it is a field of this object. You MUST separately initialize
* the ZX context object, e.g. using zx_reset_ctx() or zx_init_ctx(),
* before you can use ZXID configuration object in any memory allocation prone
* activity (which is nearly every function in this API).
*/
/* Called by: zxid_conf_to_cf_len, zxid_init_conf_ctx */
int zxid_init_conf(zxid_conf* cf, const char* zxid_path)
{
DD("Initconf with path(%s)", zxid_path);
cf->magic = ZXID_CONF_MAGIC;
cf->path_len = zxid_path ? strlen(zxid_path) : 0;
cf->path = ZX_ALLOC(cf->ctx, cf->path_len+1);
memcpy(cf->path, zxid_path, cf->path_len);
cf->path[cf->path_len] = 0;
cf->nice_name = ZXID_NICE_NAME;
cf->button_url = ZXID_BUTTON_URL;
cf->pref_button_size = ZXID_PREF_BUTTON_SIZE;
cf->org_name = ZXID_ORG_NAME;
cf->locality = ZXID_LOCALITY;
cf->state = ZXID_STATE;
cf->country = ZXID_COUNTRY;
cf->contact_org = ZXID_CONTACT_ORG;
cf->contact_name = ZXID_CONTACT_NAME;
cf->contact_email = ZXID_CONTACT_EMAIL;
cf->contact_tel = ZXID_CONTACT_TEL;
/* NB: Typically allocated by zxid_grab_domain_name(). */
COPYVAL(cf->fedusername_suffix, ZXID_FEDUSERNAME_SUFFIX,
ZXID_FEDUSERNAME_SUFFIX + strlen(ZXID_FEDUSERNAME_SUFFIX));
cf->url = ZXID_URL;
cf->non_standard_entityid = ZXID_NON_STANDARD_ENTITYID;
cf->redirect_hack_imposed_url = ZXID_REDIRECT_HACK_IMPOSED_URL;
cf->redirect_hack_zxid_url = ZXID_REDIRECT_HACK_ZXID_URL;
cf->cdc_url = ZXID_CDC_URL;
cf->cdc_choice = ZXID_CDC_CHOICE;
cf->authn_req_sign = ZXID_AUTHN_REQ_SIGN;
cf->want_sso_a7n_signed = ZXID_WANT_SSO_A7N_SIGNED;
cf->want_authn_req_signed = ZXID_WANT_AUTHN_REQ_SIGNED;
cf->sso_soap_sign = ZXID_SSO_SOAP_SIGN;
cf->sso_soap_resp_sign = ZXID_SSO_SOAP_RESP_SIGN;
cf->sso_sign = ZXID_SSO_SIGN;
cf->wsc_sign = ZXID_WSC_SIGN;
cf->wsp_sign = ZXID_WSP_SIGN;
cf->oaz_jwt_sigenc_alg = ZXID_OAZ_JWT_SIGENC_ALG;
cf->wspcgicmd = ZXID_WSPCGICMD;
cf->nameid_enc = ZXID_NAMEID_ENC;
cf->post_a7n_enc = ZXID_POST_A7N_ENC;
cf->canon_inopt = ZXID_CANON_INOPT;
if (cf->ctx) cf->ctx->canon_inopt = cf->canon_inopt;
cf->enc_tail_opt = ZXID_ENC_TAIL_OPT;
cf->enckey_opt = ZXID_ENCKEY_OPT;
cf->valid_opt = ZXID_VALID_OPT;
cf->idpatopt = ZXID_IDPATOPT;
cf->idp_list_meth = ZXID_IDP_LIST_METH;
cf->di_allow_create = ZXID_DI_ALLOW_CREATE;
cf->di_nid_fmt = ZXID_DI_NID_FMT;
cf->di_a7n_enc = ZXID_DI_A7N_ENC;
cf->bootstrap_level = ZXID_BOOTSTRAP_LEVEL;
cf->show_conf = ZXID_SHOW_CONF;
#ifdef USE_OPENSSL
if (zxid_path) {
#if 0
/* DO NOT ENABLE! The certificates and keys are read "just in time" if and when needed. */
cf->sign_cert = zxid_read_cert(cf, "sign-nopw-cert.pem");
cf->sign_pkey = zxid_read_private_key(cf, "sign-nopw-cert.pem");
cf->enc_cert = zxid_read_cert(cf, "enc-nopw-cert.pem");
cf->enc_pkey = zxid_read_private_key(cf, "enc-nopw-cert.pem");
cf->log_sign_pkey = zxid_read_private_key(cf, "logsign-nopw-cert.pem");
cf->log_enc_cert = zxid_read_cert(cf, "logenc-nopw-cert.pem");
zxid_sha1_file(cf, "pem/logenc.key", cf->log_symkey);
#endif
}
#else
ERR("This copy of zxid was compiled to NOT use OpenSSL. Reading certificate and private key is not supported. Signing and signature verification are not supported either. Add -DUSE_OPENSSL and recompile. %d", 0);
#endif
cf->md_fetch = ZXID_MD_FETCH;
cf->md_populate_cache = ZXID_MD_POPULATE_CACHE;
cf->md_cache_first = ZXID_MD_CACHE_FIRST;
cf->md_cache_last = ZXID_MD_CACHE_LAST;
cf->load_cot_cache = ZXID_LOAD_COT_CACHE;
cf->auto_cert = ZXID_AUTO_CERT;
cf->ses_arch_dir = ZXID_SES_ARCH_DIR;
cf->ses_cookie_name = ZXID_SES_COOKIE_NAME;
cf->user_local = ZXID_USER_LOCAL;
cf->idp_ena = ZXID_IDP_ENA;
cf->idp_pxy_ena = ZXID_IDP_PXY_ENA;
cf->imps_ena = ZXID_IMPS_ENA;
cf->as_ena = ZXID_AS_ENA;
cf->pdp_ena = ZXID_PDP_ENA;
cf->cpn_ena = ZXID_CPN_ENA;
cf->az_opt = ZXID_AZ_OPT;
cf->loguser = ZXID_LOGUSER;
cf->log_level = ZXLOG_LEVEL;
cf->log_err = ZXLOG_ERR; /* Log enables and signing and encryption flags (if USE_OPENSSL) */
cf->log_act = ZXLOG_ACT;
cf->log_issue_a7n = ZXLOG_ISSUE_A7N;
cf->log_issue_msg = ZXLOG_ISSUE_MSG;
cf->log_rely_a7n = ZXLOG_RELY_A7N;
cf->log_rely_msg = ZXLOG_RELY_MSG;
cf->log_err_in_act = ZXLOG_ERR_IN_ACT;
cf->log_act_in_err = ZXLOG_ACT_IN_ERR;
cf->log_sigfail_is_err = ZXLOG_SIGFAIL_IS_ERR;
cf->sig_fatal = ZXID_SIG_FATAL;
cf->nosig_fatal = ZXID_NOSIG_FATAL;
cf->msg_sig_ok = ZXID_MSG_SIG_OK;
cf->timeout_fatal = ZXID_TIMEOUT_FATAL;
cf->audience_fatal = ZXID_AUDIENCE_FATAL;
cf->dup_a7n_fatal = ZXID_DUP_A7N_FATAL;
cf->dup_msg_fatal = ZXID_DUP_MSG_FATAL;
cf->relto_fatal = ZXID_RELTO_FATAL;
cf->wsp_nosig_fatal = ZXID_WSP_NOSIG_FATAL;
cf->notimestamp_fatal = ZXID_NOTIMESTAMP_FATAL;
cf->anon_ok = ZXID_ANON_OK;
cf->required_authnctx = ZXID_REQUIRED_AUTHNCTX; /* NB: NULL. */
cf->issue_authnctx_pw = ZXID_ISSUE_AUTHNCTX_PW;
cf->idp_pref_acs_binding = ZXID_IDP_PREF_ACS_BINDING;
cf->mandatory_attr = ZXID_MANDATORY_ATTR;
cf->before_slop = ZXID_BEFORE_SLOP;
cf->after_slop = ZXID_AFTER_SLOP;
cf->timeskew = ZXID_TIMESKEW;
cf->a7nttl = ZXID_A7NTTL;
cf->pdp_url = ZXID_PDP_URL;
cf->pdp_call_url = ZXID_PDP_CALL_URL;
cf->xasp_vers = ZXID_XASP_VERS;
cf->trustpdp_url = ZXID_TRUSTPDP_URL;
cf->need = zxid_load_need(cf, 0, ZXID_NEED);
cf->want = zxid_load_need(cf, 0, ZXID_WANT);
cf->attrsrc = zxid_load_atsrc(cf, 0, ZXID_ATTRSRC);
cf->inmap = zxid_load_map(cf, 0, ZXID_INMAP);
cf->outmap = zxid_load_map(cf, 0, ZXID_OUTMAP);
cf->pepmap = zxid_load_map(cf, 0, ZXID_PEPMAP);
cf->pepmap_rqout = zxid_load_map(cf, 0, ZXID_PEPMAP_RQOUT);
cf->pepmap_rqin = zxid_load_map(cf, 0, ZXID_PEPMAP_RQIN);
cf->pepmap_rsout = zxid_load_map(cf, 0, ZXID_PEPMAP_RSOUT);
cf->pepmap_rsin = zxid_load_map(cf, 0, ZXID_PEPMAP_RSIN);
cf->localpdp_role_permit = zxid_load_cstr_list(cf, 0, ZXID_LOCALPDP_ROLE_PERMIT);
cf->localpdp_role_deny = zxid_load_cstr_list(cf, 0, ZXID_LOCALPDP_ROLE_DENY);
cf->localpdp_idpnid_permit = zxid_load_cstr_list(cf, 0, ZXID_LOCALPDP_IDPNID_PERMIT);
cf->localpdp_idpnid_deny = zxid_load_cstr_list(cf, 0, ZXID_LOCALPDP_IDPNID_DENY);
cf->wsc_localpdp_obl_pledge = ZXID_WSC_LOCALPDP_OBL_PLEDGE;
cf->wsp_localpdp_obl_req = ZXID_WSP_LOCALPDP_OBL_REQ;
cf->wsp_localpdp_obl_emit = ZXID_WSP_LOCALPDP_OBL_EMIT;
cf->wsc_localpdp_obl_accept = ZXID_WSC_LOCALPDP_OBL_ACCEPT;
cf->redir_to_content = ZXID_REDIR_TO_CONTENT;
cf->remote_user_ena = ZXID_REMOTE_USER_ENA;
cf->max_soap_retry = ZXID_MAX_SOAP_RETRY;
cf->mod_saml_attr_prefix = ZXID_MOD_SAML_ATTR_PREFIX;
cf->bare_url_entityid = ZXID_BARE_URL_ENTITYID;
cf->show_tech = ZXID_SHOW_TECH;
cf->idp_sel_page = ZXID_IDP_SEL_PAGE;
cf->idp_sel_templ_file= ZXID_IDP_SEL_TEMPL_FILE;
cf->idp_sel_templ = ZXID_IDP_SEL_TEMPL;
#if 0
cf->idp_sel_start = ZXID_IDP_SEL_START;
cf->idp_sel_new_idp = ZXID_IDP_SEL_NEW_IDP;
cf->idp_sel_our_eid = ZXID_IDP_SEL_OUR_EID;
cf->idp_sel_tech_user = ZXID_IDP_SEL_TECH_USER;
cf->idp_sel_tech_site = ZXID_IDP_SEL_TECH_SITE;
cf->idp_sel_footer = ZXID_IDP_SEL_FOOTER;
cf->idp_sel_end = ZXID_IDP_SEL_END;
#endif
cf->an_page = ZXID_AN_PAGE;
cf->an_templ_file = ZXID_AN_TEMPL_FILE;
cf->an_templ = ZXID_AN_TEMPL;
cf->post_templ_file = ZXID_POST_TEMPL_FILE;
cf->post_templ = ZXID_POST_TEMPL;
cf->err_page = ZXID_ERR_PAGE;
cf->err_templ_file = ZXID_ERR_TEMPL_FILE;
cf->err_templ = ZXID_ERR_TEMPL;
cf->new_user_page = ZXID_NEW_USER_PAGE;
cf->recover_passwd = ZXID_RECOVER_PASSWD;
cf->atsel_page = ZXID_ATSEL_PAGE;
cf->mgmt_start = ZXID_MGMT_START;
cf->mgmt_logout = ZXID_MGMT_LOGOUT;
cf->mgmt_defed = ZXID_MGMT_DEFED;
cf->mgmt_footer = ZXID_MGMT_FOOTER;
cf->mgmt_end = ZXID_MGMT_END;
LOCK_INIT(cf->mx);
LOCK_INIT(cf->curl_mx);
if (!zxid_ent_cache_mx_init) {
LOCK_INIT(zxid_ent_cache_mx);
zxid_ent_cache_mx_init = 1;
}
#if 1
DD("path(%.*s) cf->magic=%x", cf->path_len, cf->path, cf->magic);
#else
fprintf(stderr, "t %9s:%-3d %-16s %s d " "path(%.*s) cf->magic=%x" "\n",
__FILE__, __LINE__, __FUNCTION__, ERRMAC_INSTANCE, cf->path_len, cf->path, cf->magic);
fflush(stderr);
#endif
return 0;
}
/*() Reverse of zxid_init_conf() and zxid_parse_conf_raw(). */
void zxid_free_conf(zxid_conf *cf)
{
zxid_free_need(cf, cf->need);
zxid_free_need(cf, cf->want);
zxid_free_atsrc(cf, cf->attrsrc);
zxid_free_map(cf, cf->inmap);
zxid_free_map(cf, cf->outmap);
zxid_free_map(cf, cf->pepmap);
zxid_free_map(cf, cf->pepmap_rqout);
zxid_free_map(cf, cf->pepmap_rqin);
zxid_free_map(cf, cf->pepmap_rsout);
zxid_free_map(cf, cf->pepmap_rsin);
zxid_free_cstr_list(cf, cf->localpdp_role_permit);
zxid_free_cstr_list(cf, cf->localpdp_role_deny);
zxid_free_cstr_list(cf, cf->localpdp_idpnid_permit);
zxid_free_cstr_list(cf, cf->localpdp_idpnid_deny);
if (cf->required_authnctx) {
ZX_FREE(cf->ctx, cf->required_authnctx);
}
if (cf->fedusername_suffix) {