Merge pull request #99 from painless-security/jennifer/count_failed_reqs
[trust_router.git] / mon / mon_req_decode.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
36 #include <talloc.h>
37 #include <jansson.h>
38
39 #include <mon_internal.h>
40
41 /* Monitoring request decoders */
42
43 /**
44  * Decode a single option
45  *
46  * Format:
47  * { "type": "some_tpye" }
48  *
49  * @param opt_json JSON object reference
50  * @param dest allocated memory for the result
51  * @return MON_SUCCESS on success, error on error
52  */
53 static MON_RC mon_decode_one_opt(json_t *opt_json, MON_OPT *dest)
54 {
55   json_t *jstr = NULL;
56   MON_OPT_TYPE opt_type = OPT_TYPE_UNKNOWN;
57
58   if ( (opt_json == NULL) || (dest == NULL))
59     return MON_BADARG;
60
61   if (! json_is_object(opt_json))
62     return MON_NOPARSE;
63
64   jstr = json_object_get(opt_json, "type");
65   if ( (jstr == NULL) || (! json_is_string(jstr)) )
66     return MON_NOPARSE;
67
68   opt_type = mon_opt_type_from_string(json_string_value(jstr));
69   if (opt_type == OPT_TYPE_UNKNOWN)
70     return MON_NOPARSE;
71
72   dest->type = opt_type;
73   return MON_SUCCESS;
74 }
75
76 /**
77  * Decode options array
78  *
79  * Format:
80  * [{option}, {option}, ...]
81  *
82  */
83 static MON_RC mon_options_decode(json_t *opts_json, MON_REQ *req)
84 {
85   MON_OPT opt; // not a pointer
86   size_t n_opts=0;
87   size_t ii=0;
88
89   if ( (opts_json == NULL) || (req == NULL))
90     return MON_BADARG;
91
92   if (! json_is_array(opts_json))
93     return MON_NOPARSE;
94
95   n_opts = json_array_size(opts_json);
96   for (ii=0; ii < n_opts; ii++) {
97     if (mon_decode_one_opt(json_array_get(opts_json, ii),
98                            &opt) != MON_SUCCESS) {
99       return MON_NOPARSE;
100     }
101     if (MON_SUCCESS != mon_req_add_option(req, opt.type)){
102       return MON_NOPARSE;
103     }
104   }
105   return MON_SUCCESS;
106 }
107
108 /**
109  * Parse a JSON string into a request
110  */
111 MON_REQ *mon_req_parse(TALLOC_CTX *mem_ctx, const char *input)
112 {
113   json_t *parsed_json = NULL;
114   json_error_t json_error;
115
116   parsed_json = json_loads(input, JSON_REJECT_DUPLICATES, &json_error);
117   return mon_req_decode(mem_ctx, parsed_json);
118 }
119
120 /**
121  * Decode a JSON request
122  *
123  * Expected format:
124  * {
125  *   "command": "some_command_name",
126  *   "options": [{option1}, ...]
127  * }
128  *
129  * (options are optional)
130  *
131  * Caller must free the return value with mon_req_free().
132  *
133  * @param mem_ctx talloc context for the returned struct
134  * @param req_json reference to JSON request object
135  * @return decoded request struct or NULL on failure
136  */
137 MON_REQ *mon_req_decode(TALLOC_CTX *mem_ctx, json_t *req_json)
138 {
139   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
140   MON_REQ *req = NULL;
141   json_t *jval = NULL;
142   json_t *opts_json = NULL;
143   MON_CMD cmd = MON_CMD_UNKNOWN;
144
145   if (! json_is_object(req_json))
146     goto cleanup;
147
148   // Get the command and verify that it is a string value
149   jval = json_object_get(req_json, "command");
150   if (! json_is_string(jval))
151     goto cleanup;
152
153   cmd = mon_cmd_from_string(json_string_value(jval));
154   if (cmd == MON_CMD_UNKNOWN)
155     goto cleanup;
156
157   /* Command is good. Allocate the request in the tmp context */
158   req = mon_req_new(tmp_ctx, cmd);
159   if (req == NULL)
160     goto cleanup;
161
162   /* Parse options if we have any */
163   opts_json = json_object_get(req_json, "options");
164   if (opts_json) {
165     if (mon_options_decode(opts_json, req) != MON_SUCCESS) {
166       req = NULL; // memory still in tmp_ctx, so it will be cleaned up
167       goto cleanup;
168     }
169   }
170
171   /* Success! Put the request in the caller's talloc context */
172   talloc_steal(mem_ctx, req);
173
174 cleanup:
175   talloc_free(tmp_ctx);
176
177   return req;
178 }