Configure IDPs and implement AAA server lookup based on IDP realm
[trust_router.git] / common / tr_config.c
1 /*
2  * Copyright (c) 2012, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <jansson.h>
38 #include <dirent.h>
39
40 #include <tr_config.h>
41 #include <tr.h>
42
43 void tr_print_config (FILE *stream, TR_CFG *cfg) {
44   fprintf(stream, "tr_print_config: Not yet implemented.\n");
45   return;
46 }
47
48 void tr_cfg_free (TR_CFG *cfg) {
49   /* TBD */
50   return;
51 }
52
53 TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) {
54
55   if (!tr)
56     return TR_CFG_BAD_PARAMS;
57
58   if (tr->active_cfg)
59     tr_cfg_free(tr->active_cfg);
60
61   tr->active_cfg = tr->new_cfg;
62   return TR_CFG_SUCCESS;
63 }
64
65 static TR_CFG_RC tr_cfg_parse_internal (TR_INSTANCE *tr, json_t *jcfg) {
66   json_t *jint = NULL;
67   json_t *jmtd = NULL;
68
69   if ((!tr) || (!tr->new_cfg) || (!jcfg))
70     return TR_CFG_BAD_PARAMS;
71
72   if (NULL == (tr->new_cfg->internal = malloc(sizeof(TR_CFG_INTERNAL))))
73     return TR_CFG_NOMEM;
74
75   memset(tr->new_cfg->internal, 0, sizeof(TR_CFG_INTERNAL));
76
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);
81     } else {
82       fprintf(stderr,"tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.\n");
83       return TR_CFG_NOPARSE;
84     }
85   } else {
86     /* If not configured, use the default */
87     tr->new_cfg->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
88   }
89   fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n");
90   return TR_CFG_SUCCESS;
91 }
92
93 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, json_t *jcfg) {
94   //  json_t *jrpr = NULL;
95   
96   return TR_CFG_SUCCESS;
97 }
98
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;
101
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;
105     return NULL;
106   }
107
108   if (NULL == (aaa = malloc(sizeof(TR_AAA_SERVER)))) {
109     fprintf(stderr, "tr_config_parse_one_aaa_server: Out of memory.\n");
110     *rc = TR_CFG_NOMEM;
111     return NULL;
112   }
113
114   memset(aaa, 0, sizeof(TR_AAA_SERVER));
115
116   /* TBD -- Handle IPv6 addresses */
117   inet_aton(json_string_value(jaddr), &(aaa->aaa_server_addr));
118
119   return aaa;
120 }
121
122
123 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, TR_CFG_RC *rc) 
124 {
125   TR_AAA_SERVER *aaa = NULL;
126   TR_AAA_SERVER *temp_aaa = NULL;
127   int i = 0;
128
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))) {
131       return NULL;
132     }
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;
136     aaa = temp_aaa;
137   }
138   return aaa;
139 }
140
141 static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *apcs, TR_CFG_RC *rc) 
142 {
143   TR_APC *apc;
144
145   return (apc = malloc(sizeof(TR_APC)));
146 }
147
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;
150   json_t *jrid = NULL;
151   json_t *jscfg = NULL;
152   json_t *jsrvrs = NULL;
153   json_t *japcs = NULL;
154
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;
158     return NULL;
159   }
160
161   if (NULL == (idp = malloc(sizeof(TR_IDP_REALM)))) {
162     fprintf(stderr, "tr_config_parse_one_idp_realm: Our of memory.\n");
163     *rc = TR_CFG_NOMEM;
164     return NULL;
165   }
166
167   memset(idp, 0, sizeof(TR_IDP_REALM));
168
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");
178     free(idp);
179     *rc = TR_CFG_NOPARSE;
180     return NULL;
181   }
182
183   if (0 == strcmp(json_string_value(jscfg), "no")) {
184     idp->shared_config = 0;
185   } else {
186     idp->shared_config = 1;
187   }
188
189   if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
190     free(idp);
191     fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
192     *rc = TR_CFG_NOMEM;
193     return NULL;
194   }
195
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);
199     free(idp);
200     return NULL;
201   }
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 */;
206     free(idp);
207     return NULL;
208   }
209
210 return idp;
211 }
212
213 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg) 
214 {
215   json_t *jidps = NULL;
216   TR_CFG_RC rc = TR_CFG_SUCCESS;
217   TR_IDP_REALM *idp = NULL;
218   int i = 0;
219
220   if ((!tr) || (!tr->new_cfg) || (!jcfg))
221     return TR_CFG_BAD_PARAMS;
222
223   if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) ||
224       (!json_is_array(jidps))) {
225     return TR_CFG_NOPARSE;
226   }
227
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))) {
230        return rc;
231     }
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;
235   }
236   return rc;
237 }
238
239 static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg) {
240  
241   
242   return TR_CFG_SUCCESS;
243 }
244
245 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
246
247   /* If there is a partial/abandoned config lying around, free it */
248   if (tr->new_cfg) {
249     tr_cfg_free(tr->new_cfg);
250   }
251   
252   if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
253     return TR_CFG_NOMEM;
254
255   memset(tr->new_cfg, 0, sizeof(TR_CFG));
256
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);
262     return TR_CFG_ERROR;
263   }
264   return TR_CFG_SUCCESS;
265 }
266
267 json_t *tr_read_config (int n, struct dirent **cfg_files) {
268   json_t *jcfg = NULL;
269   json_t *temp = NULL;
270   json_error_t err;
271
272   if (!cfg_files)
273     return NULL;
274
275   while (n--) {
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);
279       return NULL;
280     }
281
282     if (!jcfg) {
283       jcfg = temp;
284     }else {
285       if (-1 == json_object_update(jcfg, temp)) {
286         fprintf(stderr, "tr_read_config: Error merging config information.\n");
287         return NULL;
288       }
289     }
290   }
291
292   //  fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
293
294   return jcfg;
295 }
296
297 static int is_cfg_file(const struct dirent *dent) {
298   int n;
299
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"))) {
303     return 1;
304   }
305
306   /* otherwise, return false. */
307   return 0;
308 }
309
310 int tr_find_config_files (struct dirent ***cfg_files) {
311   int n = 0, i = 0;
312   
313   n = scandir(".", cfg_files, &is_cfg_file, 0);
314
315   if (n < 0) {
316     perror("scandir");
317     fprintf(stderr, "tr_find_config: scandir error.\n");
318     return 0;
319   }
320
321   if (n == 0) {
322     fprintf (stderr, "tr_find_config: No config files found.\n");
323     return 0;
324   }
325
326   i = n;
327   while(i--) {
328     fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);
329   }
330     
331   return n;
332 }