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.
40 #include <tr_config.h>
43 void tr_print_config (FILE *stream, TR_CFG *cfg) {
44 fprintf(stream, "tr_print_config: Not yet implemented.\n");
48 void tr_cfg_free (TR_CFG *cfg) {
53 TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) {
56 return TR_CFG_BAD_PARAMS;
59 tr_cfg_free(tr->active_cfg);
61 tr->active_cfg = tr->new_cfg;
62 return TR_CFG_SUCCESS;
65 static TR_CFG_RC tr_cfg_parse_internal (TR_INSTANCE *tr, json_t *jcfg) {
69 if ((!tr) || (!tr->new_cfg) || (!jcfg))
70 return TR_CFG_BAD_PARAMS;
72 if (NULL == (tr->new_cfg->internal = malloc(sizeof(TR_CFG_INTERNAL))))
75 memset(tr->new_cfg->internal, 0, sizeof(TR_CFG_INTERNAL));
77 if ((NULL != (jint = json_object_get(jcfg, "tr_internal"))) &&
78 (NULL != (jmtd = json_object_get(jint, "max_tree_depth")))) {
79 if (json_is_number(jmtd)) {
80 tr->new_cfg->internal->max_tree_depth = json_integer_value(jmtd);
82 fprintf(stderr,"tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.\n");
83 return TR_CFG_NOPARSE;
86 /* If not configured, use the default */
87 tr->new_cfg->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
89 fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n");
90 return TR_CFG_SUCCESS;
93 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, json_t *jcfg) {
94 // json_t *jrpr = NULL;
96 return TR_CFG_SUCCESS;
99 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_INSTANCE *tr, json_t *jaddr, TR_CFG_RC *rc) {
100 TR_AAA_SERVER *aaa = NULL;
102 if ((!tr) || (!tr->new_cfg) || (!jaddr) || (!json_is_string(jaddr))) {
103 fprintf(stderr, "tr_cfg_parse_one_aaa_server: Bad parameters.\n");
104 *rc = TR_CFG_BAD_PARAMS;
108 if (NULL == (aaa = malloc(sizeof(TR_AAA_SERVER)))) {
109 fprintf(stderr, "tr_config_parse_one_aaa_server: Out of memory.\n");
114 memset(aaa, 0, sizeof(TR_AAA_SERVER));
116 /* TBD -- Handle IPv6 addresses */
117 inet_aton(json_string_value(jaddr), &(aaa->aaa_server_addr));
123 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, TR_CFG_RC *rc)
125 TR_AAA_SERVER *aaa = NULL;
126 TR_AAA_SERVER *temp_aaa = NULL;
129 for (i = 0; i < json_array_size(jaaas); i++) {
130 if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tr, json_array_get(jaaas, i), rc))) {
133 /* TBD -- IPv6 addresses */
134 // fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
135 temp_aaa->next = aaa;
141 static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *apcs, TR_CFG_RC *rc)
145 return (apc = malloc(sizeof(TR_APC)));
148 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, TR_CFG_RC *rc) {
149 TR_IDP_REALM *idp = NULL;
151 json_t *jscfg = NULL;
152 json_t *jsrvrs = NULL;
153 json_t *japcs = NULL;
155 if ((!tr) || (!tr->new_cfg) || (!jidp)) {
156 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
157 *rc = TR_CFG_BAD_PARAMS;
161 if (NULL == (idp = malloc(sizeof(TR_IDP_REALM)))) {
162 fprintf(stderr, "tr_config_parse_one_idp_realm: Our of memory.\n");
167 memset(idp, 0, sizeof(TR_IDP_REALM));
169 if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
170 (!json_is_string(jrid)) ||
171 (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
172 (!json_is_string(jscfg)) ||
173 (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
174 (!json_is_array(jsrvrs)) ||
175 (NULL == (japcs = json_object_get(jidp, "apcs"))) ||
176 (!json_is_array(japcs))) {
177 fprintf(stderr, "tr_cfg_parse_one_realm: Error parsing IDP realm configuration.\n");
179 *rc = TR_CFG_NOPARSE;
183 if (0 == strcmp(json_string_value(jscfg), "no")) {
184 idp->shared_config = 0;
186 idp->shared_config = 1;
189 if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
191 fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
196 if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(tr, jsrvrs, rc))) {
197 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
198 tr_free_name(idp->realm_id);
202 if (NULL == (idp->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
203 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
204 tr_free_name(idp->realm_id);
205 /* TBD -- free aaa_servers */;
213 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg)
215 json_t *jidps = NULL;
216 TR_CFG_RC rc = TR_CFG_SUCCESS;
217 TR_IDP_REALM *idp = NULL;
220 if ((!tr) || (!tr->new_cfg) || (!jcfg))
221 return TR_CFG_BAD_PARAMS;
223 if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) ||
224 (!json_is_array(jidps))) {
225 return TR_CFG_NOPARSE;
228 for (i = 0; i < json_array_size(jidps); i++) {
229 if (NULL == (idp = tr_cfg_parse_one_idp_realm(tr, json_array_get(jidps, i), &rc))) {
232 fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: realm_id = %s.\n", idp->realm_id->buf);
233 idp->next = tr->new_cfg->idp_realms;
234 tr->new_cfg->idp_realms = idp;
239 static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg) {
242 return TR_CFG_SUCCESS;
245 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
247 /* If there is a partial/abandoned config lying around, free it */
249 tr_cfg_free(tr->new_cfg);
252 if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
255 memset(tr->new_cfg, 0, sizeof(TR_CFG));
257 if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr, jcfg)) ||
258 (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr, jcfg)) ||
259 (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr, jcfg)) ||
260 (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr, jcfg))) {
261 tr_cfg_free(tr->new_cfg);
264 return TR_CFG_SUCCESS;
267 json_t *tr_read_config (int n, struct dirent **cfg_files) {
276 fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
277 if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
278 fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
285 if (-1 == json_object_update(jcfg, temp)) {
286 fprintf(stderr, "tr_read_config: Error merging config information.\n");
292 // fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
297 static int is_cfg_file(const struct dirent *dent) {
300 /* if the last four letters of the filename are .cfg, return true. */
301 if ((4 <= (n = strlen(dent->d_name))) &&
302 (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
306 /* otherwise, return false. */
310 int tr_find_config_files (struct dirent ***cfg_files) {
313 n = scandir(".", cfg_files, &is_cfg_file, 0);
317 fprintf(stderr, "tr_find_config: scandir error.\n");
322 fprintf (stderr, "tr_find_config: No config files found.\n");
328 fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);