2 * Copyright (c) 2012, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <tr_config.h>
43 #include <tr_filter.h>
44 #include <trust_router/tr_constraint.h>
46 void tr_print_config (FILE *stream, TR_CFG *cfg) {
47 fprintf(stream, "tr_print_config: Not yet implemented.\n");
51 void tr_cfg_free (TR_CFG *cfg) {
56 TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) {
58 return TR_CFG_BAD_PARAMS;
61 tr_cfg_free(tr->active_cfg);
63 tr->active_cfg = tr->new_cfg;
64 return TR_CFG_SUCCESS;
67 static TR_CFG_RC tr_cfg_parse_internal (TR_CFG *trc, json_t *jcfg) {
71 json_t *jhname = NULL;
73 if ((!trc) || (!jcfg))
74 return TR_CFG_BAD_PARAMS;
76 if (NULL == trc->internal) {
77 if (NULL == (trc->internal = talloc(trc, TR_CFG_INTERNAL)))
80 memset(trc->internal, 0, sizeof(TR_CFG_INTERNAL));
83 if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
84 if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
85 if (json_is_number(jmtd)) {
86 trc->internal->max_tree_depth = json_integer_value(jmtd);
88 fprintf(stderr,"tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.\n");
89 return TR_CFG_NOPARSE;
92 /* If not configured, use the default */
93 trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
95 if (NULL != (jtp = json_object_get(jint, "tids_port"))) {
96 if (json_is_number(jtp)) {
97 trc->internal->tids_port = json_integer_value(jtp);
99 fprintf(stderr,"tr_cfg_parse_internal: Parsing error, port is not a number.\n");
100 return TR_CFG_NOPARSE;
103 /* If not configured, use the default */
104 trc->internal->tids_port = TR_DEFAULT_TIDS_PORT;
106 if (NULL != (jhname = json_object_get(jint, "hostname"))) {
107 if (json_is_string(jhname)) {
108 trc->internal->hostname = json_string_value(jhname);
110 fprintf(stderr,"tr_cfg_parse_internal: Parsing error, hostname is not a string.\n");
111 return TR_CFG_NOPARSE;
114 fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n");
115 return TR_CFG_SUCCESS;
117 return TR_CFG_SUCCESS;
120 static TR_CONSTRAINT *tr_cfg_parse_one_constraint (TR_CFG *trc, char *ctype, json_t *jc, TR_CFG_RC *rc)
125 if ((!trc) || (!ctype) || (!jc) || (!rc) ||
126 (!json_is_array(jc)) ||
127 (0 >= json_array_size(jc)) ||
128 (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
129 (!json_is_string(json_array_get(jc, 0)))) {
130 fprintf(stderr, "tr_cfg_parse_one_constraint: config error.\n");
131 *rc = TR_CFG_NOPARSE;
135 if (NULL == (cons = talloc(trc, TR_CONSTRAINT))) {
136 fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory (cons).\n");
141 memset(cons, 0, sizeof(TR_CONSTRAINT));
143 if (NULL == (cons->type = tr_new_name(ctype))) {
144 fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory (type).\n");
149 for (i = 0; i < json_array_size(jc); i++) {
150 cons->matches[i] = tr_new_name((char *)(json_string_value(json_array_get(jc, i))));
156 static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, json_t *jfilt, TR_CFG_RC *rc)
158 TR_FILTER *filt = NULL;
159 json_t *jftype = NULL;
161 json_t *jfaction = NULL;
162 json_t *jfspecs = NULL;
163 json_t *jffield = NULL;
164 json_t *jfmatch = NULL;
169 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
170 (!json_is_string(jftype))) {
171 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
172 *rc = TR_CFG_NOPARSE;
176 if ((NULL == (jfls = json_object_get(jfilt, "filter_lines"))) ||
177 (!json_is_array(jfls))) {
178 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
179 *rc = TR_CFG_NOPARSE;
183 if (TR_MAX_FILTER_LINES < json_array_size(jfls)) {
184 fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_lines, maximimum of %d.\n", TR_MAX_FILTER_LINES);
185 *rc = TR_CFG_NOPARSE;
189 if (NULL == (filt = talloc(trc, TR_FILTER))) {
190 fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory.\n");
195 memset(filt, 0, sizeof(TR_FILTER));
197 if (!strcmp(json_string_value(jftype), "rp_permitted")) {
198 filt->type = TR_FILTER_TYPE_RP_PERMITTED;
201 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type, unknown type '%s'.\n", json_string_value(jftype));
202 *rc = TR_CFG_NOPARSE;
203 tr_filter_free(filt);
207 /* For each filter line... */
208 for (i = 0; i < json_array_size(jfls); i++) {
210 if ((NULL == (jfaction = json_object_get(json_array_get(jfls, i), "action"))) ||
211 (!json_is_string(jfaction))) {
212 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action.\n");
213 *rc = TR_CFG_NOPARSE;
214 tr_filter_free(filt);
218 if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, i), "filter_specs"))) ||
219 (!json_is_array(jfspecs)) ||
220 (0 == json_array_size(jfspecs))) {
221 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter specs.\n");
222 *rc = TR_CFG_NOPARSE;
223 tr_filter_free(filt);
227 if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
228 fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.\n", TR_MAX_FILTER_SPECS);
229 *rc = TR_CFG_NOPARSE;
230 tr_filter_free(filt);
234 if (NULL == (filt->lines[i] = talloc(trc, TR_FLINE))) {
235 fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory (fline).\n");
237 tr_filter_free(filt);
241 memset(filt->lines[i], 0, sizeof(TR_FLINE));
243 if (!strcmp(json_string_value(jfaction), "accept")) {
244 filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
246 else if (!strcmp(json_string_value(jfaction), "reject")) {
247 filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
250 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.\n", json_string_value(jfaction));
251 *rc = TR_CFG_NOPARSE;
252 tr_filter_free(filt);
256 if ((NULL != (jrc = json_object_get(json_array_get(jfls, i), "realm_constraints"))) &&
257 (json_is_array(jrc)) &&
258 (0 != json_array_size(jrc)) &&
259 (TR_MAX_CONST_MATCHES >= json_array_size(jrc))) {
261 if (NULL == (filt->lines[i]->realm_cons = tr_cfg_parse_one_constraint(trc, "realm", jrc, rc))) {
262 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing realm constraint");
263 tr_filter_free(filt);
268 if ((NULL != (jdc = json_object_get(json_array_get(jfls, i), "domain_constraints"))) &&
269 (json_is_array(jdc)) &&
270 (0 != json_array_size(jdc)) &&
271 (TR_MAX_CONST_MATCHES >= json_array_size(jdc))) {
273 if (NULL == (filt->lines[i]->domain_cons = tr_cfg_parse_one_constraint(trc, "domain", jdc, rc))) {
274 fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing domain constraint");
275 tr_filter_free(filt);
280 /*For each filter spec within the filter line... */
281 for (j = 0; j <json_array_size(jfspecs); j++) {
283 if ((NULL == (jffield = json_object_get(json_array_get(jfspecs, j), "field"))) ||
284 (!json_is_string(jffield)) ||
285 (NULL == (jfmatch = json_object_get(json_array_get(jfspecs, j), "match"))) ||
286 (!json_is_string(jfmatch))) {
287 fprintf (stderr, "tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.\n", i, j);
288 *rc = TR_CFG_NOPARSE;
289 tr_filter_free(filt);
293 if (NULL == (filt->lines[i]->specs[j] = talloc(trc, TR_FSPEC))) {
294 fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory.\n");
296 tr_filter_free(filt);
300 memset(filt->lines[i]->specs[j], 0, sizeof(TR_FSPEC));
302 if ((NULL == (filt->lines[i]->specs[j]->field = tr_new_name((char *)json_string_value(jffield)))) ||
303 (NULL == (filt->lines[i]->specs[j]->match = tr_new_name((char *)json_string_value(jfmatch))))) {
304 fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory.\n");
306 tr_filter_free(filt);
315 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_CFG *trc, json_t *jrp, TR_CFG_RC *rc)
317 TR_RP_CLIENT *rp = NULL;
319 json_t *jfilt = NULL;
320 json_t *jftype = NULL;
323 if ((!trc) || (!jrp) || (!rc)) {
324 fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n");
326 *rc = TR_CFG_BAD_PARAMS;
330 if ((NULL == (jgns = json_object_get(jrp, "gss_names"))) ||
331 (!json_is_array(jgns))) {
332 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no GSS names.\n");
333 *rc = TR_CFG_NOPARSE;
337 /* TBD -- Support more than one filter per RP client? */
338 if (NULL == (jfilt = json_object_get(jrp, "filter"))) {
339 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no filter.\n");
340 *rc = TR_CFG_NOPARSE;
344 /* We only support rp_permitted filters for RP clients */
345 if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
346 (!json_is_string(jftype)) ||
347 (strcmp(json_string_value(jftype), "rp_permitted"))) {
348 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter type.\n");
349 *rc = TR_CFG_NOPARSE;
353 if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
354 fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.\n");
355 *rc = TR_CFG_NOPARSE;
359 if (NULL == (rp = talloc(trc, TR_RP_CLIENT))) {
360 fprintf(stderr, "tr_cfg_parse_one_rp_realm: Out of memory.\n");
365 memset(rp, 0, sizeof(TR_RP_CLIENT));
367 /* TBD -- support more than one filter entry per RP Client? */
368 if (NULL == (rp->filter = tr_cfg_parse_one_filter(trc, jfilt, rc))) {
369 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing filter.\n");
370 *rc = TR_CFG_NOPARSE;
374 for (i = 0; i < json_array_size(jgns); i++) {
375 if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
376 fprintf(stderr, "tr_cfg_parse_one_rp_client: No memory for GSS Name.\n");
385 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_CFG *trc, json_t *jcfg) {
387 TR_RP_CLIENT *rp = NULL;
388 TR_CFG_RC rc = TR_CFG_SUCCESS;
391 if ((!trc) || (!jcfg))
392 return TR_CFG_BAD_PARAMS;
394 if (NULL != (jrps = json_object_get(jcfg, "rp_clients"))) {
396 if (!json_is_array(jrps)) {
397 return TR_CFG_NOPARSE;
400 for (i = 0; i < json_array_size(jrps); i++) {
401 if (NULL == (rp = tr_cfg_parse_one_rp_client(trc,
402 json_array_get(jrps, i),
406 fprintf(stderr, "tr_cfg_parse_rp_clients: RP client configured -- first gss: %s\n", rp->gss_names[0]->buf);
407 rp->next = trc->rp_clients;
408 trc->rp_clients = rp;
414 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_CFG *trc, json_t *jaddr, TR_CFG_RC *rc) {
415 TR_AAA_SERVER *aaa = NULL;
417 if ((!trc) || (!jaddr) || (!json_is_string(jaddr))) {
418 fprintf(stderr, "tr_cfg_parse_one_aaa_server: Bad parameters.\n");
419 *rc = TR_CFG_BAD_PARAMS;
423 if (NULL == (aaa = talloc(trc, TR_AAA_SERVER))) {
424 fprintf(stderr, "tr_cfg_parse_one_aaa_server: Out of memory.\n");
429 memset(aaa, 0, sizeof(TR_AAA_SERVER));
431 aaa->hostname = tr_new_name((char *)(json_string_value(jaddr)));
436 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_CFG *trc, json_t *jaaas, TR_CFG_RC *rc)
438 TR_AAA_SERVER *aaa = NULL;
439 TR_AAA_SERVER *temp_aaa = NULL;
442 for (i = 0; i < json_array_size(jaaas); i++) {
443 if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(trc, json_array_get(jaaas, i), rc))) {
446 /* TBD -- IPv6 addresses */
447 // fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
448 temp_aaa->next = aaa;
454 static TR_APC *tr_cfg_parse_apcs (TR_CFG *trc, json_t *japcs, TR_CFG_RC *rc)
458 *rc = TR_CFG_SUCCESS; /* presume success */
460 if ((!trc) || (!japcs) || (!rc)) {
461 fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
463 *rc = TR_CFG_BAD_PARAMS;
467 if (NULL == (apc = talloc(trc, TR_APC))) {
468 fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
473 memset(apc, 0, sizeof(TR_APC));
475 /* TBD, deal with more than one APC. In the meantime, though... */
476 /* Only parse the first APC, because we only know how to deal with one, anyway. */
477 if (0 == json_array_size(japcs))
480 if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
481 fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
489 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_CFG *trc, json_t *jidp, TR_CFG_RC *rc) {
490 TR_IDP_REALM *idp = NULL;
492 json_t *jscfg = NULL;
493 json_t *jsrvrs = NULL;
494 json_t *japcs = NULL;
496 if ((!trc) || (!jidp) || (!rc)) {
497 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
499 *rc = TR_CFG_BAD_PARAMS;
503 if (NULL == (idp = talloc(trc, TR_IDP_REALM))) {
504 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Out of memory.\n");
509 memset(idp, 0, sizeof(TR_IDP_REALM));
511 if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
512 (!json_is_string(jrid)) ||
513 (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
514 (!json_is_string(jscfg)) ||
515 (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
516 (!json_is_array(jsrvrs))) {
517 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
518 *rc = TR_CFG_NOPARSE;
522 if (0 == strcmp(json_string_value(jscfg), "no")) {
523 idp->shared_config = 0;
525 idp->shared_config = 1;
528 if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
529 fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
534 if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(trc, jsrvrs, rc))) {
535 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
536 tr_free_name(idp->realm_id);
540 if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
541 (json_is_array(japcs))) {
542 if (NULL == (idp->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
543 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
544 tr_free_name(idp->realm_id);
545 /* TBD -- free aaa_servers */;
552 static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg)
555 TR_CFG_RC rc = TR_CFG_SUCCESS;
556 TR_AAA_SERVER *ds = NULL;
559 if ((!trc) || (!jcfg))
560 return TR_CFG_BAD_PARAMS;
562 /* If there are default servers, store them */
563 if ((NULL != (jdss = json_object_get(jcfg, "default_servers"))) &&
564 (json_is_array(jdss)) &&
565 (0 < json_array_size(jdss))) {
567 for (i = 0; i < json_array_size(jdss); i++) {
568 if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc,
569 json_array_get(jdss, i),
573 fprintf(stderr, "tr_cfg_parse_default_servers: Default server configured: %s\n", ds->hostname->buf);
574 ds->next = trc->default_servers;
575 trc->default_servers = ds;
582 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_CFG *trc, json_t *jcfg)
584 json_t *jidps = NULL;
585 TR_CFG_RC rc = TR_CFG_SUCCESS;
586 TR_IDP_REALM *idp = NULL;
589 if ((!trc) || (!jcfg))
590 return TR_CFG_BAD_PARAMS;
592 /* If there are any IDP Realms, parse them */
593 if ((NULL != (jidps = json_object_get(jcfg, "idp_realms"))) &&
594 (json_is_array(jidps))) {
595 for (i = 0; i < json_array_size(jidps); i++) {
596 if (NULL == (idp = tr_cfg_parse_one_idp_realm(trc,
597 json_array_get(jidps, i),
601 fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
602 idp->next = trc->idp_realms;
603 trc->idp_realms = idp;
610 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_RC *rc)
612 TR_IDP_REALM *idp = NULL;
613 TR_IDP_REALM *temp_idp = NULL;
618 (!json_is_array(jidps))) {
620 *rc = TR_CFG_BAD_PARAMS;
624 for (i = 0; i < json_array_size(jidps); i++) {
625 if (NULL == (temp_idp = (tr_cfg_find_idp(trc,
626 tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
628 fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n",
629 (char *)json_string_value(json_array_get(jidps, i)));
633 temp_idp->comm_next = idp;
640 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC *rc)
642 TR_RP_REALM *rp = NULL;
643 TR_RP_REALM *temp_rp = NULL;
648 (!json_is_array(jrps))) {
650 *rc = TR_CFG_BAD_PARAMS;
654 for (i = (json_array_size(jrps)-1); i >= 0; i--) {
655 if (NULL == (temp_rp = talloc(trc, TR_RP_REALM))) {
656 fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
661 memset (temp_rp, 0, sizeof(TR_RP_REALM));
663 if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
664 fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
677 static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc) {
678 TR_COMM *comm = NULL;
680 json_t *jtype = NULL;
681 json_t *japcs = NULL;
682 json_t *jidps = NULL;
685 if ((!trc) || (!jcomm) || (!rc)) {
686 fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
688 *rc = TR_CFG_BAD_PARAMS;
692 if (NULL == (comm = talloc_zero(trc, TR_COMM))) {
693 fprintf(stderr, "tr_cfg_parse_one_comm: Out of memory.\n");
699 if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
700 (!json_is_string(jid)) ||
701 (NULL == (jtype = json_object_get(jcomm, "type"))) ||
702 (!json_is_string(jtype)) ||
703 (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
704 (!json_is_array(japcs)) ||
705 (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
706 (!json_is_array(jidps)) ||
707 (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
708 (!json_is_array(jrps))) {
709 fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
710 *rc = TR_CFG_NOPARSE;
714 if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
715 fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
720 if (0 == strcmp(json_string_value(jtype), "apc")) {
721 comm->type = TR_COMM_APC;
722 } else if (0 == strcmp(json_string_value(jtype), "coi")) {
723 comm->type = TR_COMM_COI;
724 if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, japcs, rc))) {
725 fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
726 tr_free_name(comm->id);
730 fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
731 tr_free_name(comm->id);
732 *rc = TR_CFG_NOPARSE;
736 comm->idp_realms = tr_cfg_parse_comm_idps(trc, jidps, rc);
737 if (TR_CFG_SUCCESS != *rc) {
738 fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
739 tr_free_name(comm->id);
743 comm->rp_realms = tr_cfg_parse_comm_rps(trc, jrps, rc);
744 if (TR_CFG_SUCCESS != *rc) {
745 fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
746 tr_free_name(comm->id);
750 if (TR_COMM_APC == comm->type) {
751 json_t *jexpire = json_object_get(jcomm, "expiration_interval");
752 comm->expiration_interval = 43200; /*30 days*/
754 if (!json_is_integer(jexpire)) {
755 fprintf(stderr, "tr_parse_comm: expirae_interval is not an integer\n");
758 comm->expiration_interval = json_integer_value(jexpire);
759 if (comm->expiration_interval <= 10)
760 comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
761 if (comm->expiration_interval > 129600) /* 90 days*/
762 comm->expiration_interval = 129600;
769 static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg)
771 json_t *jcomms = NULL;
772 TR_CFG_RC rc = TR_CFG_SUCCESS;
773 TR_COMM *comm = NULL;
776 if ((!trc) || (!jcfg)) {
777 fprintf(stderr, "tr_cfg_parse_comms: Bad Parameters.\n");
778 return TR_CFG_BAD_PARAMS;
781 if (NULL != (jcomms = json_object_get(jcfg, "communities"))) {
782 if (!json_is_array(jcomms)) {
783 return TR_CFG_NOPARSE;
786 for (i = 0; i < json_array_size(jcomms); i++) {
787 if (NULL == (comm = tr_cfg_parse_one_comm(trc,
788 json_array_get(jcomms, i),
792 fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
793 comm->next = trc->comms;
800 TR_CFG_RC tr_cfg_validate (TR_CFG *trc) {
801 TR_CFG_RC rc = TR_CFG_SUCCESS;
804 return TR_CFG_BAD_PARAMS;
806 if ((NULL == trc->internal)||
807 (NULL == trc->internal->hostname)) {
808 fprintf(stderr, "tr_cfg_validate: Error: No internal configuration, or no hostname.\n");
812 if (NULL == trc->rp_clients) {
813 fprintf(stderr, "tr_cfg_validate: Error: No RP Clients configured\n");
817 if (NULL == trc->comms) {
818 fprintf(stderr, "tr_cfg_validate: Error: No Communities configured\n");
822 if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) {
823 fprintf(stderr, "tr_cfg_validate: Error: No default servers or IDPs configured.\n");
830 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, int n, struct dirent **cfg_files) {
834 if ((!tr) || (!cfg_files))
835 return TR_CFG_BAD_PARAMS;
837 /* If there is a partial/abandoned config lying around, free it */
839 tr_cfg_free(tr->new_cfg);
841 if (NULL == (tr->new_cfg = talloc(NULL, TR_CFG)))
844 memset(tr->new_cfg, 0, sizeof(TR_CFG));
846 /* Parse configuration information from each config file */
848 fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
849 if (NULL == (jcfg = json_load_file(cfg_files[n]->d_name,
850 JSON_DISABLE_EOF_CHECK, &rc))) {
851 fprintf (stderr, "tr_read_config: Error parsing config file %s.\n",
852 cfg_files[n]->d_name);
853 return TR_CFG_NOPARSE;
856 if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr->new_cfg, jcfg)) ||
857 (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr->new_cfg, jcfg)) ||
858 (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr->new_cfg, jcfg)) ||
859 (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(tr->new_cfg, jcfg)) ||
860 (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr->new_cfg, jcfg))) {
861 tr_cfg_free(tr->new_cfg);
866 /* make sure we got a complete, consistent configuration */
867 if (TR_CFG_SUCCESS != tr_cfg_validate(tr->new_cfg)) {
868 fprintf(stderr, "tr_parse_config: Error: INVALID CONFIGURATION, EXITING\n");
872 return TR_CFG_SUCCESS;
875 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
878 TR_IDP_REALM *cfg_idp;
880 if ((!tr_cfg) || (!idp_id)) {
882 *rc = TR_CFG_BAD_PARAMS;
886 for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
887 if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
888 fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
892 /* if we didn't find one, return NULL */
896 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
898 TR_RP_CLIENT *cfg_rp;
901 if ((!tr_cfg) || (!rp_gss)) {
903 *rc = TR_CFG_BAD_PARAMS;
907 for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
908 for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
909 if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
910 fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
915 /* if we didn't find one, return NULL */
919 static int is_cfg_file(const struct dirent *dent) {
922 /* if the last four letters of the filename are .cfg, return true. */
923 if ((4 <= (n = strlen(dent->d_name))) &&
924 (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
928 /* otherwise, return false. */
932 int tr_find_config_files (struct dirent ***cfg_files) {
935 n = scandir(".", cfg_files, &is_cfg_file, 0);
939 fprintf(stderr, "tr_find_config: scandir error.\n");
944 fprintf (stderr, "tr_find_config: No config files found.\n");
950 fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);