b6a16ad3e02c3e512fb9dde71e0936dc8b082361
[trust_router.git] / common / tr_config_internal.c
1 /*
2  * Copyright (c) 2012-2018, 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 <talloc.h>
36 #include <jansson.h>
37 #include <tr_debug.h>
38 #include <tr_config.h>
39 #include <tr_cfgwatch.h>
40
41 /**
42  * Parse a boolean
43  *
44  * If the key does not exist in the src object, returns success but does fill in *dest.
45  *
46  * @param src JSON object to pull a value from
47  * @param key key to pull
48  * @param dest (output) pointer to an allocated integer
49  * @return TR_CFG_SUCCESS or an error code
50  */
51 static TR_CFG_RC tr_cfg_parse_boolean(json_t *src, const char *key, int *dest)
52 {
53   json_t *jtmp;
54
55   /* Validate parameters */
56   if ((src == NULL) || (key == NULL) || (dest == NULL))
57     return TR_CFG_BAD_PARAMS;
58
59   /* See if we have a value for this key; do nothing if not */
60   jtmp = json_object_get(src, key);
61   if (jtmp) {
62     if (json_is_boolean(jtmp)) {
63       *dest = json_boolean_value(jtmp);
64     } else {
65       tr_debug("tr_cfg_parse_unsigned: Parsing error, %s is not a boolean.", key);
66       return TR_CFG_NOPARSE;
67     }
68   }
69
70   return TR_CFG_SUCCESS;
71 }
72
73 /**
74  * Parse a signed integer
75  *
76  * If the key does not exist in the src object, returns success but does fill in *dest.
77  *
78  * @param src JSON object to pull a value from
79  * @param key key to pull
80  * @param dest (output) pointer to an allocated integer
81  * @return TR_CFG_SUCCESS or an error code
82  */
83 static TR_CFG_RC tr_cfg_parse_integer(json_t *src, const char *key, int *dest)
84 {
85   json_t *jtmp;
86
87   /* Validate parameters */
88   if ((src == NULL) || (key == NULL) || (dest == NULL))
89     return TR_CFG_BAD_PARAMS;
90
91   /* See if we have a value for this key; do nothing if not */
92   jtmp = json_object_get(src, key);
93   if (jtmp) {
94     if (json_is_number(jtmp)) {
95       *dest = (int) json_integer_value(jtmp);
96     } else {
97       tr_debug("tr_cfg_parse_unsigned: Parsing error, %s is not a number.", key);
98       return TR_CFG_NOPARSE;
99     }
100   }
101
102   return TR_CFG_SUCCESS;
103 }
104
105 /**
106  * Parse an unsigned integer
107  *
108  * If the key does not exist in the src object, returns success but does fill in *dest.
109  *
110  * @param src JSON object to pull a value from
111  * @param key key to pull
112  * @param dest (output) pointer to an allocated unsigned integer
113  * @return TR_CFG_SUCCESS or an error code
114  */
115 static TR_CFG_RC tr_cfg_parse_unsigned(json_t *src, const char *key, unsigned int *dest)
116 {
117   json_t *jtmp;
118
119   /* Validate parameters */
120   if ((src == NULL) || (key == NULL) || (dest == NULL))
121     return TR_CFG_BAD_PARAMS;
122
123   /* See if we have a value for this key; do nothing if not */
124   jtmp = json_object_get(src, key);
125   if (jtmp) {
126     if (json_is_number(jtmp)) {
127       *dest = (unsigned int) json_integer_value(jtmp);
128     } else {
129       tr_debug("tr_cfg_parse_unsigned: Parsing error, %s is not a number.", key);
130       return TR_CFG_NOPARSE;
131     }
132   }
133
134   return TR_CFG_SUCCESS;
135 }
136
137 /**
138  * Parse a string
139  *
140  * If the key does not exist in the src object, returns success but does not allocate
141  * a return value in dest. Nulls the destination pointer if there is no return value.
142  *
143  * Return value is allocated in talloc's NULL context and must be freed with talloc_free()
144  * or put into a non-NULL context with talloc_steal()
145  *
146  * @param src JSON object to pull a value from
147  * @param key key to pull
148  * @param dest (output) pointer to a pointer that will hold the newly allocated return value
149  * @return TR_CFG_SUCCESS or an error code
150  */
151 static TR_CFG_RC tr_cfg_parse_string(json_t *src, const char *key, const char **dest)
152 {
153   json_t *jtmp;
154
155   /* Validate parameters */
156   if ((src == NULL) || (key == NULL) || (dest == NULL))
157     return TR_CFG_BAD_PARAMS;
158
159   /* See if we have a value for this key; do nothing if not */
160   jtmp = json_object_get(src, key);
161   if (!jtmp) {
162     *dest = NULL; /* No return value, null this out */
163   } else {
164     if (json_is_string(jtmp)) {
165       *dest = talloc_strdup(NULL, json_string_value(jtmp));
166     } else {
167       tr_debug("tr_cfg_parse_string: Parsing error, %s is not a string.", key);
168       return TR_CFG_NOPARSE;
169     }
170   }
171
172   return TR_CFG_SUCCESS;
173 }
174
175 /**
176  * Set default values for settings that have them
177  *
178  * @param cfg configuration structure to fill in, not null
179  */
180 static void set_defaults(TR_CFG_INTERNAL *cfg)
181 {
182   cfg->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
183   cfg->tids_port = TR_DEFAULT_TIDS_PORT;
184   cfg->trps_port = TR_DEFAULT_TRPS_PORT;
185   cfg->mons_port = TR_DEFAULT_MONITORING_PORT;
186   cfg->cfg_poll_interval = TR_CFGWATCH_DEFAULT_POLL;
187   cfg->cfg_settling_time = TR_CFGWATCH_DEFAULT_SETTLE;
188   cfg->trp_connect_interval = TR_DEFAULT_TRP_CONNECT_INTERVAL;
189   cfg->trp_sweep_interval = TR_DEFAULT_TRP_SWEEP_INTERVAL;
190   cfg->trp_update_interval = TR_DEFAULT_TRP_UPDATE_INTERVAL;
191   cfg->tid_req_timeout = TR_DEFAULT_TID_REQ_TIMEOUT;
192   cfg->tid_resp_numer = TR_DEFAULT_TID_RESP_NUMER;
193   cfg->tid_resp_denom = TR_DEFAULT_TID_RESP_DENOM;
194   cfg->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
195   cfg->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
196   cfg->monitoring_credentials = NULL;
197 }
198
199 /* Helper that checks return value of a parse fn and returns if it failed */
200 #define NOPARSE_UNLESS(x)    \
201 do {                         \
202   if ((x) != TR_CFG_SUCCESS) \
203     return TR_CFG_NOPARSE;   \
204 } while(0)
205
206 static TR_CFG_RC tr_cfg_parse_monitoring(TR_CFG *trc, json_t *jmon)
207 {
208   int enabled = 1; /* assume we are enabled unless we are told not to be */
209
210   NOPARSE_UNLESS(tr_cfg_parse_boolean(jmon, "enabled", &enabled));
211   if (enabled) {
212     NOPARSE_UNLESS(tr_cfg_parse_integer(jmon, "port", &(trc->internal->mons_port)));
213     NOPARSE_UNLESS(tr_cfg_parse_gss_names(trc->internal,
214                                           json_object_get(jmon, "authorized_credentials"),
215                                           &(trc->internal->monitoring_credentials)));
216   }
217
218   return TR_CFG_SUCCESS;
219 }
220
221 /**
222  * Parse internal configuration JSON
223  *
224  * @param trc configuration structure to fill in
225  * @param jint internal configuration JSON object
226  * @return TR_CFG_SUCCESS or an error code
227  */
228 TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jint)
229 {
230   json_t *jtmp = NULL;
231   const char *s = NULL;
232
233   if ((!trc) || (!jint))
234     return TR_CFG_BAD_PARAMS;
235
236   /* If we don't yet have an internal config, allocate one and set defaults. If it
237    * already exists, do not disturb existing settings. */
238   if (NULL == trc->internal) {
239     if (NULL == (trc->internal = talloc_zero(trc, TR_CFG_INTERNAL)))
240       return TR_CFG_NOMEM;
241     set_defaults(trc->internal); /* Install defaults for any unspecified settings */
242   }
243
244   NOPARSE_UNLESS(tr_cfg_parse_string(jint, "hostname", &(trc->internal->hostname)));
245   talloc_steal(trc->internal, trc->internal->hostname);
246
247   NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "max_tree_depth",           &(trc->internal->max_tree_depth)));
248   NOPARSE_UNLESS(tr_cfg_parse_integer(jint, "tids_port",                &(trc->internal->tids_port)));
249   NOPARSE_UNLESS(tr_cfg_parse_integer(jint, "trps_port",                &(trc->internal->trps_port)));
250   NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "cfg_poll_interval",        &(trc->internal->cfg_poll_interval)));
251   NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "cfg_settling_time",        &(trc->internal->cfg_settling_time)));
252   NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "trp_connect_interval",     &(trc->internal->trp_connect_interval)));
253   NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "trp_sweep_interval",       &(trc->internal->trp_sweep_interval)));
254   NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "trp_update_interval",      &(trc->internal->trp_update_interval)));
255   NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "tid_request_timeout",      &(trc->internal->tid_req_timeout)));
256   NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "tid_response_numerator",   &(trc->internal->tid_resp_numer)));
257   NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "tid_response_denominator", &(trc->internal->tid_resp_denom)));
258
259   /* Parse the logging section */
260   if (NULL != (jtmp = json_object_get(jint, "logging"))) {
261     NOPARSE_UNLESS(tr_cfg_parse_string(jtmp, "log_threshold", &s));
262     if (s) {
263       trc->internal->log_threshold = str2sev(s);
264       talloc_free((void *) s);
265     }
266
267     NOPARSE_UNLESS(tr_cfg_parse_string(jtmp, "console_threshold", &s));
268     if (s) {
269       trc->internal->console_threshold = str2sev(s);
270       talloc_free((void *) s);
271     }
272   }
273
274   /* Parse the monitoring section */
275   if (NULL != (jtmp = json_object_get(jint, "monitoring"))) {
276     NOPARSE_UNLESS(tr_cfg_parse_monitoring(trc, jtmp));
277   }
278
279   tr_debug("tr_cfg_parse_internal: Internal config parsed.");
280   return TR_CFG_SUCCESS;
281 }