From ea3fc9141fa679d9971107564ef9dbab095541dd Mon Sep 17 00:00:00 2001 From: Margaret Wasserman Date: Mon, 1 Apr 2013 08:28:43 -0400 Subject: [PATCH] Added TR_INSTANCE, fleshed out config functions. --- Makefile.am | 3 +- common/tr_config.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++- include/tr.h | 14 +++++-- include/tr_config.h | 36 +++++++++++++++++- tr/tr_config.c | 45 ---------------------- tr/tr_main.c | 48 ++++++++++++++++++------ 6 files changed, 189 insertions(+), 62 deletions(-) delete mode 100644 tr/tr_config.c diff --git a/Makefile.am b/Makefile.am index 4e501b1..9530476 100644 --- a/Makefile.am +++ b/Makefile.am @@ -9,7 +9,8 @@ common/tr_dh.c lib_LTLIBRARIES = libtr_tid.la tr_tr_SOURCES = tr/tr_main.c \ -common/tr_config.c +common/tr_config.c \ +tr/tr.c tr_tr_LDADD = gsscon/libgsscon.la libtr_tid.la diff --git a/common/tr_config.c b/common/tr_config.c index bd47832..e1a81c7 100644 --- a/common/tr_config.c +++ b/common/tr_config.c @@ -34,10 +34,113 @@ #include #include +#include +#include #include +#include -int tr_read_config (FILE *cfg_file) { +void tr_print_config (FILE *stream, TR_CFG *cfg) { + fprintf(stream, "tr_print_config(): Not yet implemented.\n"); + return; +} + +void tr_cfg_free (TR_CFG *cfg) { + /* TBD */ + return; +} + +TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) { + TR_CFG_RC rc = TR_CFG_SUCCESS; + + if (!tr) + return TR_CFG_BAD_PARAMS; + + tr->active_cfg = tr->new_cfg; + return rc; +} + +TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) { + + if (tr->new_cfg) { + tr_cfg_free(tr->new_cfg); + tr->new_cfg = NULL; + } + + // if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr, jcfg)) || + // (TR_CFG_SUCCESS != tr_cfg_parse_rp_realms(tr, jcfg)) || + // (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr, jcfg)) || + // (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr, jcfg))) { + // if (tr->new_cfg) + // tr_cfg_free(tr->new_cfg); + // return TR_CFG_ERROR; + // } + return TR_CFG_SUCCESS; +} + +json_t *tr_read_config (int n, struct dirent **cfg_files) { + json_t *jcfg = NULL; + json_t *temp = NULL; + json_error_t err; + + if (!cfg_files) + return NULL; + while (n--) { + fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name); + if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) { + fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name); + return NULL; + } + + if (!jcfg) { + jcfg = temp; + }else { + if (-1 == json_object_update(jcfg, temp)) { + fprintf(stderr, "tr_read_config: Error merging config information.\n"); + return NULL; + } + } + } + + fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0)); + + return jcfg; +} + +static int is_cfg_file(const struct dirent *dent) { + int n; + + /* if the last four letters of the filename are .cfg, return true. */ + if ((4 <= (n = strlen(dent->d_name))) && + (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) { + return 1; + } + + /* otherwise, return false. */ return 0; } + +int tr_find_config_files (struct dirent ***cfg_files) { + int n = 0, i = 0; + + n = scandir(".", cfg_files, &is_cfg_file, 0); + + if (n < 0) { + perror("scandir"); + fprintf(stderr, "tr_find_config(): scandir error.\n"); + return 0; + } + + if (n == 0) { + fprintf (stderr, "tr_find_config(): No config files found.\n"); + return 0; + } + + i = n; + while(i--) { + fprintf(stderr, "tr_find_config(): Config file found (%s).\n", (*cfg_files)[i]->d_name); + } + + return n; +} diff --git a/include/tr.h b/include/tr.h index 8548bd1..61ceb3d 100644 --- a/include/tr.h +++ b/include/tr.h @@ -35,11 +35,19 @@ #ifndef TR_H #define TR_H -#include -#include -#include #include +#include +#include #define TRUST_ROUTER_PORT 12308 +typedef struct tr_instance { + struct tr_cfg *new_cfg; /* unapplyed configuration */ + struct tr_cfg *active_cfg; + TIDC_INSTANCE tidc; + TIDS_INSTANCE tids; +} TR_INSTANCE; + +TR_INSTANCE *tr_create(void); + #endif diff --git a/include/tr_config.h b/include/tr_config.h index 3b5559e..17c481a 100644 --- a/include/tr_config.h +++ b/include/tr_config.h @@ -36,7 +36,41 @@ #define TR_CONFIG_H #include +#include +#include -int tr_read_config (FILE *cfg_file); +#include +typedef enum tr_cfg_rc { + TR_CFG_SUCCESS = 0, /* No error */ + TR_CFG_ERROR, /* General processing error */ + TR_CFG_BAD_PARAMS /* Bad parameters passed to tr_config function */ +} TR_CFG_RC; + + +typedef struct tr_cfg_file { + struct tr_cfg_file *next; + FILE *cfg_file; +} TR_CFG_FILE; + +typedef struct tr_cfg_internal { + unsigned int tr_max_tree_depth; +} TR_CFG_INTERNAL; + +typedef struct tr_cfg { + TR_CFG_INTERNAL *internal; /* internal trust router config */ + // TR_COMM *comms; /* locally-known communities */ + // TR_IDP_REALM *idp_realms; /* locally associated IDP Realms */ + // TR_RP_CLIENT *rp_clients; /* locally associated RP Clients */ + /* TBD -- Global Filters */ + /* TBD -- Trust Router Peers */ + /* TBD -- Trust Links */ +} TR_CFG; + +int tr_find_config_files (struct dirent ***cfg_files); +json_t *tr_read_config (int n, struct dirent **cfgfiles); +TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg); +TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr); +void tr_cfg_free(TR_CFG *cfg); +void tr_print_config(FILE *stream, TR_CFG *cfg); #endif diff --git a/tr/tr_config.c b/tr/tr_config.c deleted file mode 100644 index b4820b3..0000000 --- a/tr/tr_config.c +++ /dev/null @@ -1,45 +0,0 @@ - - -/* - * Copyright (c) 2012, JANET(UK) - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * 3. Neither the name of JANET(UK) nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -#include - -#include - -int tr_read_config (FILE *cfg_file) { - int rc = 0; - - return rc; -} diff --git a/tr/tr_main.c b/tr/tr_main.c index d413ef0..21422a7 100644 --- a/tr/tr_main.c +++ b/tr/tr_main.c @@ -33,10 +33,14 @@ */ #include +#include #include +#include +#include -int static tids_req_handler (TIDS_INSTANCE * tids, + +static int tids_req_handler (TIDS_INSTANCE * tids, TID_REQ *req, TID_RESP **resp, void *cookie) @@ -50,28 +54,50 @@ int static tids_req_handler (TIDS_INSTANCE * tids, int main (int argc, const char *argv[]) { - TIDS_INSTANCE *tids = 0; - int err; - FILE *cfg_file = 0; + TR_INSTANCE *tr = NULL; + TIDS_INSTANCE *tids = NULL; + struct dirent **cfg_files = NULL; + json_t *jcfg = NULL; + TR_CFG_RC rc = TR_CFG_SUCCESS; /* presume success */ + int err = 0, n = 0;; /* parse command-line arguments -- TBD */ - /* open the configuration file*/ - cfg_file = fopen ("tr.cfg", "r"); - - /* read initial configuration */ - if (0 != (err = tr_read_config (cfg_file))) { - printf ("Error reading configuration, err = %d.\n", err); + /* create a Trust Router instance */ + if (NULL == (tr = tr_create())) { + fprintf(stderr, "Unable to create Trust Router instance, exiting.\n"); return 1; } + /* find the configuration files */ + if (0 == (n = tr_find_config_files(&cfg_files))) { + fprintf (stderr, "Can't locate configuration files, exiting.\n"); + exit(1); + } + + /* read and parse initial configuration */ + if (NULL == (jcfg = tr_read_config (n, cfg_files))) { + fprintf (stderr, "Error reading or parsing configuration files, exiting.\n"); + exit(1); + } + if (TR_CFG_SUCCESS != tr_parse_config(tr, jcfg)) { + fprintf (stderr, "Error decoding configuration information, exiting.\n"); + exit(1); + } + + /* apply initial configuration */ + if (TR_CFG_SUCCESS != (rc = tr_apply_new_config(tr))) { + fprintf (stderr, "Error applying configuration, rc = %d.\n", rc); + exit(1); + } + /* initialize the trust path query server instance */ if (0 == (tids = tids_create ())) { printf ("Error initializing Trust Path Query Server instance.\n"); return 1; } - /* start the trust path query server, won't return unless there is an error. */ + /* start the trust path query server, won't return unless error. */ if (0 != (err = tids_start(tids, &tids_req_handler, NULL))) { printf ("Error starting Trust Path Query Server, err = %d.\n", err); return err; -- 2.1.4