Added TR_INSTANCE, fleshed out config functions.
[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   TR_CFG_RC rc = TR_CFG_SUCCESS;
55
56   if (!tr)
57     return TR_CFG_BAD_PARAMS;
58
59   tr->active_cfg = tr->new_cfg;
60   return rc;
61 }
62
63 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
64
65   if (tr->new_cfg) {
66     tr_cfg_free(tr->new_cfg);
67     tr->new_cfg = NULL;
68   }
69
70   //  if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr, jcfg)) ||
71   //      (TR_CFG_SUCCESS != tr_cfg_parse_rp_realms(tr, jcfg)) ||
72   //      (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr, jcfg)) ||
73   //      (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr, jcfg))) {
74   //    if (tr->new_cfg)
75   //      tr_cfg_free(tr->new_cfg);
76   //    return TR_CFG_ERROR;
77   //  }
78   return TR_CFG_SUCCESS;
79 }
80
81 json_t *tr_read_config (int n, struct dirent **cfg_files) {
82   json_t *jcfg = NULL;
83   json_t *temp = NULL;
84   json_error_t err;
85
86   if (!cfg_files)
87     return NULL;
88
89   while (n--) {
90     fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
91     if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
92       fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
93       return NULL;
94     }
95
96     if (!jcfg) {
97       jcfg = temp;
98     }else {
99       if (-1 == json_object_update(jcfg, temp)) {
100         fprintf(stderr, "tr_read_config: Error merging config information.\n");
101         return NULL;
102       }
103     }
104   }
105
106   fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
107
108   return jcfg;
109 }
110
111 static int is_cfg_file(const struct dirent *dent) {
112   int n;
113
114   /* if the last four letters of the filename are .cfg, return true. */
115   if ((4 <= (n = strlen(dent->d_name))) &&
116       (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
117     return 1;
118   }
119
120   /* otherwise, return false. */
121   return 0;
122 }
123
124 int tr_find_config_files (struct dirent ***cfg_files) {
125   int n = 0, i = 0;
126   
127   n = scandir(".", cfg_files, &is_cfg_file, 0);
128
129   if (n < 0) {
130     perror("scandir");
131     fprintf(stderr, "tr_find_config(): scandir error.\n");
132     return 0;
133   }
134
135   if (n == 0) {
136     fprintf (stderr, "tr_find_config(): No config files found.\n");
137     return 0;
138   }
139
140   i = n;
141   while(i--) {
142     fprintf(stderr, "tr_find_config(): Config file found (%s).\n", (*cfg_files)[i]->d_name);
143   }
144     
145   return n;
146 }