Collect return codes from monitoring handlers and indicate errors
[trust_router.git] / mon / mons_handlers.c
1 /*
2  * Copyright (c) 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 /* Handlers for monitoring requests */
36
37 #include <gmodule.h>
38
39 #include <tr_debug.h>
40 #include <mon_internal.h>
41 #include <mons_handlers.h>
42
43
44 /* Static Prototypes */
45 static int dispatch_entry_matches(MONS_DISPATCH_TABLE_ENTRY *e, MON_CMD command, MON_OPT_TYPE opt_type);
46 static MONS_HANDLER_FUNC *mons_find_handler(MONS_INSTANCE *mons, MON_CMD cmd, MON_OPT_TYPE opt_type);
47 static void request_helper(void *element, void *data);
48
49 struct request_helper_data {
50   MON_CMD command;
51   MON_OPT_TYPE opt_type;
52   json_t *payload; /* json object to add responses to */
53   GArray *results;
54 };
55
56 struct handler_result {
57   MON_OPT_TYPE opt_type; /* what opt type set this? */
58   MON_RC rc;             /* what was its result code? */
59   json_t *json_data;        /* what data, if any, is it returning? */
60 };
61
62 /**
63  * Call the appropriate handler for a request
64  *
65  * TODO: report errors from handlers
66  *
67  * @return a MON_RESP structure or null if there was a processing error
68  */
69 MON_RESP *mons_handle_request(TALLOC_CTX *mem_ctx, MONS_INSTANCE *mons, MON_REQ *req)
70 {
71   MON_RESP *resp = NULL;
72   json_t *payload = NULL;
73   struct request_helper_data cookie = {0};
74   size_t ii = 0;
75
76   tr_debug("mons_handle_request: Handling a request");
77
78   /* Start off by allocating our response with a generic error message */
79   resp = mon_resp_new(mem_ctx,
80                       MON_RESP_ERROR,
81                       "error processing request",
82                       NULL);
83   if (resp == NULL) {
84     /* we can't respond, just return */
85     tr_crit("mons_handle_request: Error allocating response structure.");
86     goto cleanup;
87   }
88
89   /* Now get a JSON object for our return payload */
90   payload = json_object();
91   if (payload == NULL) {
92     tr_crit("mons_handle_request: Error allocating response payload.");
93     goto cleanup; /* This will return the generic error message set earlier */
94   }
95
96   /* Now call handlers */
97   cookie.command = req->command;
98   cookie.results = g_array_new(FALSE, TRUE, sizeof(struct handler_result));
99
100   if (mon_req_opt_count(req) == 0) {
101     /* call every handler that matches the command */
102     cookie.opt_type = OPT_TYPE_ANY;
103     g_ptr_array_foreach(mons->handlers, request_helper, &cookie);
104   } else {
105     /* call only those handlers that match an option */
106     for (ii=0; ii < mon_req_opt_count(req); ii++) {
107       cookie.opt_type = mon_req_opt_index(req, ii)->type;
108       /* Loop over all handlers - we know we can only have one match for each opt type */
109       g_ptr_array_foreach(mons->handlers, request_helper, &cookie);
110     }
111   }
112
113   /* We now have an array of results in cookie.results. If any of these failed, return an error. */
114   tr_debug("mons_handle_request: Examining %d handler results", cookie.results->len);
115   resp->code = MON_RESP_SUCCESS; /* tentatively set this to success */
116   for (ii=0; ii < cookie.results->len; ii++) {
117     struct handler_result *this = &g_array_index(cookie.results, struct handler_result, ii);
118     if (this->rc != MON_SUCCESS) {
119       tr_debug("mons_handle_request: Result %d was an error.", ii);
120       resp->code = MON_RESP_ERROR;
121     }
122
123     /* add the JSON response even if there was an error */
124     if (this->json_data) {
125       tr_debug("mons_handle_request: Result %d returned JSON data.", ii);
126       json_object_set_new(payload, mon_opt_type_to_string(this->opt_type), this->json_data);
127     }
128   }
129
130   if (resp->code == MON_RESP_SUCCESS) {
131     if (mon_resp_set_message(resp, "success") == 0) {
132       /* Failed to set the response message to success - fail ironically, don't send
133        * an inconsistent response. */
134       tr_crit("mons_handle_request: Error setting response message to 'success'.");
135       goto cleanup;
136     }
137   } else {
138     /* Failed - send a response indicating that the overall command succeeded */
139     if (mon_resp_set_message(resp, "request processed but an error occurred") == 0) {
140       tr_crit("mons_handle_request: Error setting response message after a handler error.");
141       goto cleanup;
142     }
143   }
144
145   /* Attach the accumulated payload to the response */
146   if (json_object_size(payload) > 0) {
147     tr_debug("mons_handle_request: Attaching payload to response.");
148     mon_resp_set_payload(resp, payload);
149   }
150
151   tr_debug("mons_handle_request: Successfully processed request.");
152
153 cleanup:
154   if (payload)
155     json_decref(payload);
156   if (cookie.results)
157     g_array_free(cookie.results, TRUE);
158   return resp;
159 }
160
161 /**
162  * Register a handler for a command/option combination
163  *
164  * @param mons
165  * @param cmd
166  * @param opt_type
167  * @param f
168  * @param cookie
169  * @return
170  */
171 MON_RC mons_register_handler(MONS_INSTANCE *mons,
172                              MON_CMD cmd,
173                              MON_OPT_TYPE opt_type,
174                              MONS_HANDLER_FUNC *f,
175                              void *cookie)
176 {
177   MONS_DISPATCH_TABLE_ENTRY *entry = NULL;
178
179   if (mons_find_handler(mons, cmd, opt_type) != NULL) {
180     return MON_ERROR;
181   }
182
183   /* Put these in the mons talloc context so we don't have to muck about with
184    * a free function for the GPtrArray */
185   entry = talloc(mons, MONS_DISPATCH_TABLE_ENTRY);
186   if (entry == NULL) {
187     return MON_NOMEM;
188   }
189   entry->command = cmd;
190   entry->opt_type = opt_type;
191   entry->handler = f;
192   entry->cookie = cookie;
193
194   g_ptr_array_add(mons->handlers, entry);
195   return MON_SUCCESS;
196 }
197
198 /**
199  * Two table entries match if none of the commands or opt_types are unknown,
200  * if the commands match, and if the opt types either match or at least one is
201  * OPT_TYPE_ANY.
202  *
203  * No comparison of the handler pointer is included.
204  *
205  * @return 1 if the two match, 0 if not
206  */
207 static int dispatch_entry_matches(MONS_DISPATCH_TABLE_ENTRY *e,
208                                   MON_CMD command,
209                                   MON_OPT_TYPE opt_type)
210 {
211   if ((command == MON_CMD_UNKNOWN) || (opt_type == OPT_TYPE_UNKNOWN))
212     return 0; /* request is invalid */
213
214   if ((e->command == MON_CMD_UNKNOWN) || (e->opt_type == OPT_TYPE_UNKNOWN))
215     return 0; /* e1 is invalid */
216
217   if (e->command != command)
218     return 0; /* commands do not match */
219
220   if (e->opt_type == opt_type)
221     return 1; /* exact match */
222
223   if ( (e->opt_type == OPT_TYPE_ANY) || (opt_type == OPT_TYPE_ANY) )
224     return 1; /* one is a wildcard */
225
226   return 0; /* commands matched but opt_types did not */
227 }
228
229 static MONS_HANDLER_FUNC *mons_find_handler(MONS_INSTANCE *mons, MON_CMD cmd, MON_OPT_TYPE opt_type)
230 {
231   guint index;
232
233   for (index=0; index < mons->handlers->len; index++) {
234     if (dispatch_entry_matches(g_ptr_array_index(mons->handlers, index), cmd, opt_type))
235       return g_ptr_array_index(mons->handlers, index);
236   }
237   return NULL;
238 }
239
240 /**
241  * This calls every request handler that matches a command/opt_type,
242  * gathering their results.
243  *
244  * @param element
245  * @param data
246  */
247 static void request_helper(void *element, void *data)
248 {
249   MONS_DISPATCH_TABLE_ENTRY *entry = talloc_get_type_abort(element, MONS_DISPATCH_TABLE_ENTRY);
250   struct request_helper_data *helper_data = data;
251   struct handler_result result = {0};
252
253   if (dispatch_entry_matches(entry, helper_data->command, helper_data->opt_type)) {
254     result.rc = entry->handler(entry->cookie, &(result.json_data));
255     result.opt_type = entry->opt_type;
256     g_array_append_val(helper_data->results, result);
257   }
258 }
259