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.
38 #include <tr_config.h>
39 #include <tr_cfgwatch.h>
44 * If the key does not exist in the src object, returns success but does fill in *dest.
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
51 static TR_CFG_RC tr_cfg_parse_boolean(json_t *src, const char *key, int *dest)
55 /* Validate parameters */
56 if ((src == NULL) || (key == NULL) || (dest == NULL))
57 return TR_CFG_BAD_PARAMS;
59 /* See if we have a value for this key; do nothing if not */
60 jtmp = json_object_get(src, key);
62 if (json_is_boolean(jtmp)) {
63 *dest = json_boolean_value(jtmp);
65 tr_debug("tr_cfg_parse_unsigned: Parsing error, %s is not a boolean.", key);
66 return TR_CFG_NOPARSE;
70 return TR_CFG_SUCCESS;
74 * Parse an unsigned integer
76 * If the key does not exist in the src object, returns success but does fill in *dest.
78 * @param src JSON object to pull a value from
79 * @param key key to pull
80 * @param dest (output) pointer to an allocated unsigned integer
81 * @return TR_CFG_SUCCESS or an error code
83 static TR_CFG_RC tr_cfg_parse_unsigned(json_t *src, const char *key, unsigned int *dest)
87 /* Validate parameters */
88 if ((src == NULL) || (key == NULL) || (dest == NULL))
89 return TR_CFG_BAD_PARAMS;
91 /* See if we have a value for this key; do nothing if not */
92 jtmp = json_object_get(src, key);
94 if (json_is_number(jtmp)) {
95 *dest = (unsigned int) json_integer_value(jtmp);
97 tr_debug("tr_cfg_parse_unsigned: Parsing error, %s is not a number.", key);
98 return TR_CFG_NOPARSE;
102 return TR_CFG_SUCCESS;
108 * If the key does not exist in the src object, returns success but does not allocate
109 * a return value in dest. Nulls the destination pointer if there is no return value.
111 * Return value is allocated in talloc's NULL context and must be freed with talloc_free()
112 * or put into a non-NULL context with talloc_steal()
114 * @param src JSON object to pull a value from
115 * @param key key to pull
116 * @param dest (output) pointer to a pointer that will hold the newly allocated return value
117 * @return TR_CFG_SUCCESS or an error code
119 static TR_CFG_RC tr_cfg_parse_string(json_t *src, const char *key, const char **dest)
123 /* Validate parameters */
124 if ((src == NULL) || (key == NULL) || (dest == NULL))
125 return TR_CFG_BAD_PARAMS;
127 /* See if we have a value for this key; do nothing if not */
128 jtmp = json_object_get(src, key);
130 *dest = NULL; /* No return value, null this out */
132 if (json_is_string(jtmp)) {
133 *dest = talloc_strdup(NULL, json_string_value(jtmp));
135 tr_debug("tr_cfg_parse_string: Parsing error, %s is not a string.", key);
136 return TR_CFG_NOPARSE;
140 return TR_CFG_SUCCESS;
144 * Set default values for settings that have them
146 * @param cfg configuration structure to fill in, not null
148 static void set_defaults(TR_CFG_INTERNAL *cfg)
150 cfg->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
151 cfg->tids_port = TR_DEFAULT_TIDS_PORT;
152 cfg->trps_port = TR_DEFAULT_TRPS_PORT;
153 cfg->monitoring_port = TR_DEFAULT_MONITORING_PORT;
154 cfg->cfg_poll_interval = TR_CFGWATCH_DEFAULT_POLL;
155 cfg->cfg_settling_time = TR_CFGWATCH_DEFAULT_SETTLE;
156 cfg->trp_connect_interval = TR_DEFAULT_TRP_CONNECT_INTERVAL;
157 cfg->trp_sweep_interval = TR_DEFAULT_TRP_SWEEP_INTERVAL;
158 cfg->trp_update_interval = TR_DEFAULT_TRP_UPDATE_INTERVAL;
159 cfg->tid_req_timeout = TR_DEFAULT_TID_REQ_TIMEOUT;
160 cfg->tid_resp_numer = TR_DEFAULT_TID_RESP_NUMER;
161 cfg->tid_resp_denom = TR_DEFAULT_TID_RESP_DENOM;
162 cfg->log_threshold = TR_DEFAULT_LOG_THRESHOLD;
163 cfg->console_threshold = TR_DEFAULT_CONSOLE_THRESHOLD;
164 cfg->monitoring_credentials = NULL;
167 /* Helper that checks return value of a parse fn and returns if it failed */
168 #define NOPARSE_UNLESS(x) \
170 if ((x) != TR_CFG_SUCCESS) \
171 return TR_CFG_NOPARSE; \
174 static TR_CFG_RC tr_cfg_parse_monitoring(TR_CFG *trc, json_t *jmon)
176 int enabled = 1; /* assume we are enabled unless we are told not to be */
178 NOPARSE_UNLESS(tr_cfg_parse_boolean(jmon, "enabled", &enabled));
180 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jmon, "port", &(trc->internal->monitoring_port)));
181 NOPARSE_UNLESS(tr_cfg_parse_gss_names(trc->internal,
182 json_object_get(jmon, "authorized_credentials"),
183 &(trc->internal->monitoring_credentials)));
186 return TR_CFG_SUCCESS;
190 * Parse internal configuration JSON
192 * @param trc configuration structure to fill in
193 * @param jint internal configuration JSON object
194 * @return TR_CFG_SUCCESS or an error code
196 TR_CFG_RC tr_cfg_parse_internal(TR_CFG *trc, json_t *jint)
199 const char *s = NULL;
201 if ((!trc) || (!jint))
202 return TR_CFG_BAD_PARAMS;
204 /* If we don't yet have an internal config, allocate one and set defaults. If it
205 * already exists, do not disturb existing settings. */
206 if (NULL == trc->internal) {
207 if (NULL == (trc->internal = talloc_zero(trc, TR_CFG_INTERNAL)))
209 set_defaults(trc->internal); /* Install defaults for any unspecified settings */
212 NOPARSE_UNLESS(tr_cfg_parse_string(jint, "hostname", &(trc->internal->hostname)));
213 talloc_steal(trc->internal, trc->internal->hostname);
215 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "max_tree_depth", &(trc->internal->max_tree_depth)));
216 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "tids_port", &(trc->internal->tids_port)));
217 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "trps_port", &(trc->internal->trps_port)));
218 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "cfg_poll_interval", &(trc->internal->cfg_poll_interval)));
219 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "cfg_settling_time", &(trc->internal->cfg_settling_time)));
220 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "trp_connect_interval", &(trc->internal->trp_connect_interval)));
221 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "trp_sweep_interval", &(trc->internal->trp_sweep_interval)));
222 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "trp_update_interval", &(trc->internal->trp_update_interval)));
223 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "tid_request_timeout", &(trc->internal->tid_req_timeout)));
224 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "tid_response_numerator", &(trc->internal->tid_resp_numer)));
225 NOPARSE_UNLESS(tr_cfg_parse_unsigned(jint, "tid_response_denominator", &(trc->internal->tid_resp_denom)));
227 /* Parse the logging section */
228 if (NULL != (jtmp = json_object_get(jint, "logging"))) {
229 NOPARSE_UNLESS(tr_cfg_parse_string(jtmp, "log_threshold", &s));
231 trc->internal->log_threshold = str2sev(s);
232 talloc_free((void *) s);
235 NOPARSE_UNLESS(tr_cfg_parse_string(jtmp, "console_threshold", &s));
237 trc->internal->console_threshold = str2sev(s);
238 talloc_free((void *) s);
242 /* Parse the monitoring section */
243 if (NULL != (jtmp = json_object_get(jint, "monitoring"))) {
244 NOPARSE_UNLESS(tr_cfg_parse_monitoring(trc, jtmp));
247 tr_debug("tr_cfg_parse_internal: Internal config parsed.");
248 return TR_CFG_SUCCESS;