2 * Copyright (c) 2012-2018, 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_cfgwatch.h>
43 #include <tr_config.h>
44 #include <tr_gss_names.h>
46 #include <tr_filter.h>
47 #include <trust_router/tr_constraint.h>
50 #include <trust_router/trp.h>
52 #if JANSSON_VERSION_HEX < 0x020500
53 #include "jansson_iterators.h"
56 void tr_print_config (TR_CFG *cfg) {
57 tr_notice("tr_print_config: Logging running trust router configuration.");
58 tr_print_comms(cfg->ctable);
61 void tr_print_comms (TR_COMM_TABLE *ctab)
65 for (comm = ctab->comms; NULL != comm; comm = comm->next) {
66 tr_notice("tr_print_config: Community %s:", comm->id->buf);
68 tr_notice("tr_print_config: - Member IdPs:");
69 tr_print_comm_idps(ctab, comm);
71 tr_notice("tr_print_config: - Member RPs:");
72 tr_print_comm_rps(ctab, comm);
76 void tr_print_comm_idps(TR_COMM_TABLE *ctab, TR_COMM *comm)
78 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
79 TR_COMM_ITER *iter=NULL;
80 TR_IDP_REALM *idp = NULL;
83 iter=tr_comm_iter_new(tmp_ctx);
85 tr_notice("tr_print_config: unable to allocate IdP iterator.");
90 for (idp=tr_idp_realm_iter_first(iter, ctab, tr_comm_get_id(comm));
92 idp=tr_idp_realm_iter_next(iter)) {
93 s=tr_idp_realm_to_str(tmp_ctx, idp);
95 tr_notice("tr_print_config: - @%s", s);
97 tr_notice("tr_print_config: unable to allocate IdP output string.");
102 void tr_print_comm_rps(TR_COMM_TABLE *ctab, TR_COMM *comm)
104 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
105 TR_COMM_ITER *iter=NULL;
106 TR_RP_REALM *rp = NULL;
109 iter=tr_comm_iter_new(tmp_ctx);
111 tr_notice("tr_print_config: unable to allocate RP iterator.");
112 talloc_free(tmp_ctx);
116 for (rp=tr_rp_realm_iter_first(iter, ctab, tr_comm_get_id(comm));
118 rp=tr_rp_realm_iter_next(iter)) {
119 s=tr_rp_realm_to_str(tmp_ctx, rp);
121 tr_notice("tr_print_config: - @%s", s);
123 tr_notice("tr_print_config: unable to allocate RP output string.");
125 talloc_free(tmp_ctx);
128 static int tr_cfg_destructor(void *object)
130 TR_CFG *cfg = talloc_get_type_abort(object, TR_CFG);
132 g_array_unref(cfg->files);
135 TR_CFG *tr_cfg_new(TALLOC_CTX *mem_ctx)
137 TR_CFG *cfg=talloc(mem_ctx, TR_CFG);
140 cfg->rp_clients=NULL;
142 cfg->default_servers=NULL;
143 cfg->ctable=tr_comm_table_new(cfg);
144 if (cfg->ctable==NULL) {
148 cfg->files = g_array_new(FALSE, FALSE, sizeof(TR_CFG_FILE));
149 if (cfg->files == NULL) {
153 talloc_set_destructor((void *)cfg, tr_cfg_destructor);
158 void tr_cfg_free (TR_CFG *cfg)
163 TR_CFG_MGR *tr_cfg_mgr_new(TALLOC_CTX *mem_ctx)
165 return talloc_zero(mem_ctx, TR_CFG_MGR);
168 void tr_cfg_mgr_free (TR_CFG_MGR *cfg_mgr) {
169 talloc_free(cfg_mgr);
172 TR_CFG_RC tr_apply_new_config (TR_CFG_MGR *cfg_mgr)
174 /* cfg_mgr->active is allowed to be null, but new cannot be */
175 if ((cfg_mgr==NULL) || (cfg_mgr->new==NULL))
176 return TR_CFG_BAD_PARAMS;
178 if (cfg_mgr->active != NULL)
179 tr_cfg_free(cfg_mgr->active);
181 cfg_mgr->active = cfg_mgr->new;
182 cfg_mgr->new=NULL; /* only keep a single handle on the new configuration */
184 tr_log_threshold(cfg_mgr->active->internal->log_threshold);
185 tr_console_threshold(cfg_mgr->active->internal->console_threshold);
187 return TR_CFG_SUCCESS;
191 TR_CFG_RC tr_cfg_validate(TR_CFG *trc)
193 TR_CFG_RC rc = TR_CFG_SUCCESS;
196 return TR_CFG_BAD_PARAMS;
198 /* validate the internal config - error messages will be generated there, so don't genreate
200 if (tr_cfg_validate_internal(trc->internal) != TR_CFG_SUCCESS)
203 if (NULL == trc->rp_clients) {
204 tr_debug("tr_cfg_validate: Error: No RP Clients configured");
208 if (0==tr_comm_table_size(trc->ctable)) {
209 tr_debug("tr_cfg_validate: Error: No Communities configured");
213 if ((NULL == trc->default_servers) && (NULL == trc->ctable->idp_realms)) {
214 tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
221 static void tr_cfg_log_json_error(const char *label, json_error_t *rc)
223 tr_debug("%s: JSON parse error on line %d: %s",
230 * Parse a config file and return its JSON structure. Also emits a serial number to the log
233 * @param file_with_path The file (with path!) to parse
234 * @return Pointer to the result of parsing, or null on error
236 static json_t *tr_cfg_parse_one_config_file(const char *file_with_path)
241 if (NULL==(jcfg=json_load_file(file_with_path,
242 JSON_DISABLE_EOF_CHECK|JSON_REJECT_DUPLICATES, &rc))) {
243 tr_debug("tr_cfg_parse_one_config_file: Error parsing config file %s.",
245 tr_cfg_log_json_error("tr_cfg_parse_one_config_file", &rc);
252 /* extract serial number */
253 static json_int_t get_cfg_serial(json_t *jcfg)
257 if (NULL != (jser = json_object_get(jcfg, "serial_number"))) {
258 if (json_is_integer(jser)) {
259 return json_integer_value(jser);
262 return TR_CFG_INVALID_SERIAL;
266 * Helper to free an array returned by tr_cfg_parse_config_files
270 static void tr_cfg_parse_free_jcfgs(unsigned int n_jcfgs, json_t **jcfgs)
273 for (ii=0; ii<n_jcfgs; ii++)
274 json_decref(jcfgs[ii]);
279 * Parse a list of configuration files. Returns an array of JSON objects, free this with
280 * tr_cfg_parse_free_jcfgs(), a helper function
287 static json_t **tr_cfg_parse_config_files(TALLOC_CTX *mem_ctx, unsigned int n_files, GArray *files)
289 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
292 TR_CFG_FILE *this_file = NULL;
294 /* first allocate the jcfgs */
295 jcfgs=talloc_array(NULL, json_t *, n_files);
297 tr_crit("tr_parse_config_files: cannot allocate JSON structure array");
300 for (ii=0; ii<n_files; ii++) {
301 this_file = &g_array_index(files, TR_CFG_FILE, ii);
302 jcfgs[ii]=tr_cfg_parse_one_config_file(this_file->name);
303 if (jcfgs[ii]==NULL) {
304 tr_err("tr_parse_config: Error parsing JSON in %s", this_file->name);
305 tr_cfg_parse_free_jcfgs(ii, jcfgs); /* frees the JSON objects and the jcfgs array */
310 this_file->serial = get_cfg_serial(jcfgs[ii]);
311 if (this_file->serial != TR_CFG_INVALID_SERIAL) {
312 tr_notice("tr_parse_config_files: Attempting to load revision %"
318 tr_notice("tr_parse_config_files: Attempting to load '%s'.",
325 talloc_steal(mem_ctx, jcfgs); /* give this to the caller's context if we succeeded */
326 talloc_free(tmp_ctx);
330 /* define a type for config parse functions */
331 typedef TR_CFG_RC (TR_CFG_PARSE_FN)(TR_CFG *, json_t *);
333 * Helper function to parse a collection of JSON structures using a generic parse function.
335 * @param cfg Config structure to receive results
336 * @param parse_fn Function to apply
337 * @param n_jcfg Number of JSON structures in the array
338 * @param jcfgs Pointer to an array of decoded JSON structures
339 * @param key Key to extract from each jcfg before parsing, or NULL to use the object itself
340 * @return TR_CFG_SUCCESS on success, _FAIL or an error code on failure
342 static TR_CFG_RC tr_cfg_parse_helper(TR_CFG *cfg,
343 TR_CFG_PARSE_FN parse_fn,
349 json_t *this_jcfg=NULL;
350 TR_CFG_RC ret=TR_CFG_ERROR;
352 if ((cfg==NULL) || (jcfgs==NULL) || (parse_fn==NULL))
355 for (ii=0; ii<n_jcfg; ii++) {
357 this_jcfg = json_object_get(jcfgs[ii], key);
359 this_jcfg = jcfgs[ii];
361 /* do not try to parse a missing jcfg */
362 if (this_jcfg == NULL)
365 ret=parse_fn(cfg, this_jcfg);
366 if (ret!=TR_CFG_SUCCESS)
372 static void add_files(TR_CFG *cfg, unsigned int n, char **filenames)
374 TR_CFG_FILE frec = {0};
377 frec.name = talloc_strdup(cfg, filenames[n]);
378 frec.serial = TR_CFG_INVALID_SERIAL;
380 g_array_append_val(cfg->files, frec);
385 * Read a list of configuration files
387 * @param cfg_mgr Configuration manager
388 * @param n_files Number of entries in cfg_files
389 * @param files_with_paths Array of filenames with path to load
390 * @return TR_CFG_SUCCESS on success, TR_CFG_ERROR or a more specific error on failure
392 TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, unsigned int n_files, char **files_with_paths)
394 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
396 TR_CFG_RC cfg_rc=TR_CFG_ERROR;
398 if ((!cfg_mgr) || (!files_with_paths)) {
399 cfg_rc=TR_CFG_BAD_PARAMS;
403 /* get a fresh config to fill in, freeing old one if needed */
404 if (cfg_mgr->new != NULL)
405 tr_cfg_free(cfg_mgr->new);
406 cfg_mgr->new=tr_cfg_new(tmp_ctx); /* belongs to the temporary context for now */
407 if (cfg_mgr->new == NULL) {
412 /* add the list of files to the config */
413 add_files(cfg_mgr->new, n_files, files_with_paths);
415 /* first parse the json */
416 jcfgs=tr_cfg_parse_config_files(tmp_ctx, n_files, cfg_mgr->new->files);
418 cfg_rc=TR_CFG_NOPARSE;
422 cfg_mgr->new->peers=trp_ptable_new(cfg_mgr); /* not sure why this isn't in cfg_mgr->new's context */
424 /* now run through the parsers on the JSON */
425 if ((TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_internal, n_files, jcfgs, "tr_internal"))) ||
426 (TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_local_orgs, n_files, jcfgs, NULL))) ||
427 (TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_peer_orgs, n_files, jcfgs, NULL))) ||
428 (TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_default_servers, n_files, jcfgs,
430 (TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_comms, n_files, jcfgs, NULL))))
431 goto cleanup; /* cfg_rc was set above */
433 /* make sure we got a complete, consistent configuration */
434 if (TR_CFG_SUCCESS != tr_cfg_validate(cfg_mgr->new)) {
435 tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
441 talloc_steal(cfg_mgr, cfg_mgr->new); /* hand this over to the cfg_mgr context */
442 cfg_rc=TR_CFG_SUCCESS;
446 tr_cfg_parse_free_jcfgs(n_files, jcfgs);
447 talloc_free(tmp_ctx);
451 static int is_cfg_file(const struct dirent *dent) {
454 /* Only accept filenames ending in ".cfg" and starting with a character
455 * other than an ASCII '.' */
457 /* filename must be at least 4 characters long to be acceptable */
458 n=strlen(dent->d_name);
463 /* filename must not start with '.' */
464 if ('.' == dent->d_name[0]) {
468 /* If the above passed and the last four characters of the filename are .cfg, accept.
469 * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
470 if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
474 /* otherwise, return false. */
478 /* Find configuration files in a particular directory. Returns the
479 * number of entries found, 0 if none are found, or <0 for some
480 * errors. If n>=0, the cfg_files parameter will contain a newly
481 * allocated array of pointers to struct dirent entries, as returned
482 * by scandir(). These can be freed with tr_free_config_file_list().
484 int tr_find_config_files(const char *config_dir, struct dirent ***cfg_files) {
487 n = scandir(config_dir, cfg_files, is_cfg_file, alphasort);
491 tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
497 /* Free memory allocated for configuration file list returned from tr_find_config_files().
498 * This can be called regardless of the return value of tr_find_config_values(). */
499 void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
502 /* if n < 0, then scandir did not allocate anything because it failed */
503 if((n>=0) && (*cfg_files != NULL)) {
504 for(ii=0; ii<n; ii++) {
505 free((*cfg_files)[ii]);
507 free(*cfg_files); /* safe even if n==0 */
508 *cfg_files=NULL; /* this will help prevent accidentally freeing twice */