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 if ((NULL == trc->internal)||
199 (NULL == trc->internal->hostname)) {
200 tr_debug("tr_cfg_validate: Error: No internal configuration, or no hostname.");
204 if (NULL == trc->rp_clients) {
205 tr_debug("tr_cfg_validate: Error: No RP Clients configured");
209 if (0==tr_comm_table_size(trc->ctable)) {
210 tr_debug("tr_cfg_validate: Error: No Communities configured");
214 if ((NULL == trc->default_servers) && (NULL == trc->ctable->idp_realms)) {
215 tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
222 static void tr_cfg_log_json_error(const char *label, json_error_t *rc)
224 tr_debug("%s: JSON parse error on line %d: %s",
231 * Parse a config file and return its JSON structure. Also emits a serial number to the log
234 * @param file_with_path The file (with path!) to parse
235 * @return Pointer to the result of parsing, or null on error
237 static json_t *tr_cfg_parse_one_config_file(const char *file_with_path)
242 if (NULL==(jcfg=json_load_file(file_with_path,
243 JSON_DISABLE_EOF_CHECK|JSON_REJECT_DUPLICATES, &rc))) {
244 tr_debug("tr_cfg_parse_one_config_file: Error parsing config file %s.",
246 tr_cfg_log_json_error("tr_cfg_parse_one_config_file", &rc);
253 /* extract serial number */
254 static json_int_t get_cfg_serial(json_t *jcfg)
258 if (NULL != (jser = json_object_get(jcfg, "serial_number"))) {
259 if (json_is_integer(jser)) {
260 return json_integer_value(jser);
263 return TR_CFG_INVALID_SERIAL;
267 * Helper to free an array returned by tr_cfg_parse_config_files
271 static void tr_cfg_parse_free_jcfgs(unsigned int n_jcfgs, json_t **jcfgs)
274 for (ii=0; ii<n_jcfgs; ii++)
275 json_decref(jcfgs[ii]);
280 * Parse a list of configuration files. Returns an array of JSON objects, free this with
281 * tr_cfg_parse_free_jcfgs(), a helper function
288 static json_t **tr_cfg_parse_config_files(TALLOC_CTX *mem_ctx, unsigned int n_files, GArray *files)
290 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
293 TR_CFG_FILE *this_file = NULL;
295 /* first allocate the jcfgs */
296 jcfgs=talloc_array(NULL, json_t *, n_files);
298 tr_crit("tr_parse_config_files: cannot allocate JSON structure array");
301 for (ii=0; ii<n_files; ii++) {
302 this_file = &g_array_index(files, TR_CFG_FILE, ii);
303 jcfgs[ii]=tr_cfg_parse_one_config_file(this_file->name);
304 if (jcfgs[ii]==NULL) {
305 tr_err("tr_parse_config: Error parsing JSON in %s", this_file->name);
306 tr_cfg_parse_free_jcfgs(ii, jcfgs); /* frees the JSON objects and the jcfgs array */
311 this_file->serial = get_cfg_serial(jcfgs[ii]);
312 if (this_file->serial != TR_CFG_INVALID_SERIAL) {
313 tr_notice("tr_parse_config_files: Attempting to load revision %"
319 tr_notice("tr_parse_config_files: Attempting to load '%s'.",
326 talloc_steal(mem_ctx, jcfgs); /* give this to the caller's context if we succeeded */
327 talloc_free(tmp_ctx);
331 /* define a type for config parse functions */
332 typedef TR_CFG_RC (TR_CFG_PARSE_FN)(TR_CFG *, json_t *);
334 * Helper function to parse a collection of JSON structures using a generic parse function.
336 * @param cfg Config structure to receive results
337 * @param parse_fn Function to apply
338 * @param n_jcfg Number of JSON structures in the array
339 * @param jcfgs Pointer to an array of decoded JSON structures
340 * @param key Key to extract from each jcfg before parsing, or NULL to use the object itself
341 * @return TR_CFG_SUCCESS on success, _FAIL or an error code on failure
343 static TR_CFG_RC tr_cfg_parse_helper(TR_CFG *cfg,
344 TR_CFG_PARSE_FN parse_fn,
350 json_t *this_jcfg=NULL;
351 TR_CFG_RC ret=TR_CFG_ERROR;
353 if ((cfg==NULL) || (jcfgs==NULL) || (parse_fn==NULL))
356 for (ii=0; ii<n_jcfg; ii++) {
358 this_jcfg = json_object_get(jcfgs[ii], key);
360 this_jcfg = jcfgs[ii];
362 /* do not try to parse a missing jcfg */
363 if (this_jcfg == NULL)
366 ret=parse_fn(cfg, this_jcfg);
367 if (ret!=TR_CFG_SUCCESS)
373 static void add_files(TR_CFG *cfg, unsigned int n, char **filenames)
375 TR_CFG_FILE frec = {0};
378 frec.name = talloc_strdup(cfg, filenames[n]);
379 frec.serial = TR_CFG_INVALID_SERIAL;
381 g_array_append_val(cfg->files, frec);
386 * Read a list of configuration files
388 * @param cfg_mgr Configuration manager
389 * @param n_files Number of entries in cfg_files
390 * @param files_with_paths Array of filenames with path to load
391 * @return TR_CFG_SUCCESS on success, TR_CFG_ERROR or a more specific error on failure
393 TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, unsigned int n_files, char **files_with_paths)
395 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
397 TR_CFG_RC cfg_rc=TR_CFG_ERROR;
399 if ((!cfg_mgr) || (!files_with_paths)) {
400 cfg_rc=TR_CFG_BAD_PARAMS;
404 /* get a fresh config to fill in, freeing old one if needed */
405 if (cfg_mgr->new != NULL)
406 tr_cfg_free(cfg_mgr->new);
407 cfg_mgr->new=tr_cfg_new(tmp_ctx); /* belongs to the temporary context for now */
408 if (cfg_mgr->new == NULL) {
413 /* add the list of files to the config */
414 add_files(cfg_mgr->new, n_files, files_with_paths);
416 /* first parse the json */
417 jcfgs=tr_cfg_parse_config_files(tmp_ctx, n_files, cfg_mgr->new->files);
419 cfg_rc=TR_CFG_NOPARSE;
423 cfg_mgr->new->peers=trp_ptable_new(cfg_mgr); /* not sure why this isn't in cfg_mgr->new's context */
425 /* now run through the parsers on the JSON */
426 if ((TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_internal, n_files, jcfgs, "tr_internal"))) ||
427 (TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_local_orgs, n_files, jcfgs, NULL))) ||
428 (TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_peer_orgs, n_files, jcfgs, NULL))) ||
429 (TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_default_servers, n_files, jcfgs,
431 (TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_comms, n_files, jcfgs, NULL))))
432 goto cleanup; /* cfg_rc was set above */
434 /* make sure we got a complete, consistent configuration */
435 if (TR_CFG_SUCCESS != tr_cfg_validate(cfg_mgr->new)) {
436 tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
442 talloc_steal(cfg_mgr, cfg_mgr->new); /* hand this over to the cfg_mgr context */
443 cfg_rc=TR_CFG_SUCCESS;
447 tr_cfg_parse_free_jcfgs(n_files, jcfgs);
448 talloc_free(tmp_ctx);
452 static int is_cfg_file(const struct dirent *dent) {
455 /* Only accept filenames ending in ".cfg" and starting with a character
456 * other than an ASCII '.' */
458 /* filename must be at least 4 characters long to be acceptable */
459 n=strlen(dent->d_name);
464 /* filename must not start with '.' */
465 if ('.' == dent->d_name[0]) {
469 /* If the above passed and the last four characters of the filename are .cfg, accept.
470 * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
471 if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
475 /* otherwise, return false. */
479 /* Find configuration files in a particular directory. Returns the
480 * number of entries found, 0 if none are found, or <0 for some
481 * errors. If n>=0, the cfg_files parameter will contain a newly
482 * allocated array of pointers to struct dirent entries, as returned
483 * by scandir(). These can be freed with tr_free_config_file_list().
485 int tr_find_config_files(const char *config_dir, struct dirent ***cfg_files) {
488 n = scandir(config_dir, cfg_files, is_cfg_file, alphasort);
492 tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
498 /* Free memory allocated for configuration file list returned from tr_find_config_files().
499 * This can be called regardless of the return value of tr_find_config_values(). */
500 void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
503 /* if n < 0, then scandir did not allocate anything because it failed */
504 if((n>=0) && (*cfg_files != NULL)) {
505 for(ii=0; ii<n; ii++) {
506 free((*cfg_files)[ii]);
508 free(*cfg_files); /* safe even if n==0 */
509 *cfg_files=NULL; /* this will help prevent accidentally freeing twice */