1 /*
2 * SysDB - src/frontend/query.c
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "sysdb.h"
30 #include "core/store.h"
31 #include "frontend/connection-private.h"
32 #include "frontend/parser.h"
33 #include "utils/error.h"
34 #include "utils/strbuf.h"
36 #include <errno.h>
37 #include <string.h>
39 /*
40 * private helper functions
41 */
43 typedef struct {
44 sdb_strbuf_t *buf;
45 sdb_store_matcher_t *filter;
47 size_t last_len;
48 } tojson_data_t;
50 static int
51 lookup_tojson(sdb_store_obj_t *obj, void *user_data)
52 {
53 tojson_data_t *data = user_data;
54 int status;
56 if (data->filter && (! sdb_store_matcher_matches(data->filter, obj, NULL)))
57 return 0;
59 if (sdb_strbuf_len(data->buf) > data->last_len)
60 sdb_strbuf_append(data->buf, ",");
61 data->last_len = sdb_strbuf_len(data->buf);
62 status = sdb_store_host_tojson(obj, data->buf,
63 data->filter, /* flags = */ 0);
64 return status;
65 } /* lookup_tojson */
67 /*
68 * public API
69 */
71 int
72 sdb_fe_query(sdb_conn_t *conn)
73 {
74 sdb_llist_t *parsetree;
75 sdb_conn_node_t *node = NULL;
76 int status = 0;
78 if ((! conn) || (conn->cmd != CONNECTION_QUERY))
79 return -1;
81 parsetree = sdb_fe_parse(sdb_strbuf_string(conn->buf),
82 (int)conn->cmd_len);
83 if (! parsetree) {
84 char query[conn->cmd_len + 1];
85 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
86 query[sizeof(query) - 1] = '\0';
87 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse query '%s'",
88 query);
89 return -1;
90 }
92 switch (sdb_llist_len(parsetree)) {
93 case 0:
94 /* skipping empty command */
95 break;
96 case 1:
97 node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
98 break;
100 default:
101 {
102 char query[conn->cmd_len + 1];
103 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
104 query[sizeof(query) - 1] = '\0';
105 sdb_log(SDB_LOG_WARNING, "frontend: Ignoring %d command%s "
106 "in multi-statement query '%s'",
107 sdb_llist_len(parsetree) - 1,
108 sdb_llist_len(parsetree) == 2 ? "" : "s",
109 query);
110 node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
111 }
112 }
114 if (node) {
115 status = sdb_fe_exec(conn, node);
116 sdb_object_deref(SDB_OBJ(node));
117 }
119 sdb_llist_destroy(parsetree);
120 return status;
121 } /* sdb_fe_query */
123 int
124 sdb_fe_fetch(sdb_conn_t *conn)
125 {
126 char hostname[conn->cmd_len + 1];
127 if ((! conn) || (conn->cmd != CONNECTION_FETCH))
128 return -1;
129 strncpy(hostname, sdb_strbuf_string(conn->buf), conn->cmd_len);
130 hostname[sizeof(hostname) - 1] = '\0';
131 return sdb_fe_exec_fetch(conn, hostname, /* filter = */ NULL);
132 } /* sdb_fe_fetch */
134 int
135 sdb_fe_list(sdb_conn_t *conn)
136 {
137 if ((! conn) || (conn->cmd != CONNECTION_LIST))
138 return -1;
139 return sdb_fe_exec_list(conn, /* filter = */ NULL);
140 } /* sdb_fe_list */
142 int
143 sdb_fe_lookup(sdb_conn_t *conn)
144 {
145 sdb_store_matcher_t *m;
146 int status;
148 if ((! conn) || (conn->cmd != CONNECTION_LOOKUP))
149 return -1;
151 m = sdb_fe_parse_matcher(sdb_strbuf_string(conn->buf),
152 (int)conn->cmd_len);
153 if (! m) {
154 char expr[conn->cmd_len + 1];
155 strncpy(expr, sdb_strbuf_string(conn->buf), conn->cmd_len);
156 expr[sizeof(expr) - 1] = '\0';
157 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse "
158 "lookup condition '%s'", expr);
159 return -1;
160 }
162 status = sdb_fe_exec_lookup(conn, m, /* filter = */ NULL);
163 sdb_object_deref(SDB_OBJ(m));
164 return status;
165 } /* sdb_fe_lookup */
167 int
168 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node)
169 {
170 sdb_store_matcher_t *m = NULL, *filter = NULL;
172 if (! node)
173 return -1;
175 switch (node->cmd) {
176 case CONNECTION_FETCH:
177 if (CONN_FETCH(node)->filter)
178 filter = CONN_FETCH(node)->filter->matcher;
179 return sdb_fe_exec_fetch(conn, CONN_FETCH(node)->name, filter);
180 case CONNECTION_LIST:
181 if (CONN_LIST(node)->filter)
182 filter = CONN_LIST(node)->filter->matcher;
183 return sdb_fe_exec_list(conn, filter);
184 case CONNECTION_LOOKUP:
185 if (CONN_LOOKUP(node)->matcher)
186 m = CONN_LOOKUP(node)->matcher->matcher;
187 if (CONN_LOOKUP(node)->filter)
188 filter = CONN_LOOKUP(node)->filter->matcher;
189 return sdb_fe_exec_lookup(conn, m, filter);
191 default:
192 sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd);
193 return -1;
194 }
195 return -1;
196 } /* sdb_fe_exec */
198 int
199 sdb_fe_exec_fetch(sdb_conn_t *conn, const char *name,
200 sdb_store_matcher_t *filter)
201 {
202 sdb_strbuf_t *buf;
203 sdb_store_obj_t *host;
205 host = sdb_store_get_host(name);
206 if (! host) {
207 sdb_log(SDB_LOG_DEBUG, "frontend: Failed to fetch host '%s': "
208 "not found", name);
210 sdb_strbuf_sprintf(conn->errbuf, "Host %s not found", name);
211 return -1;
212 }
214 buf = sdb_strbuf_create(1024);
215 if (! buf) {
216 char errbuf[1024];
217 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
218 "buffer to handle FETCH command: %s",
219 sdb_strerror(errno, errbuf, sizeof(errbuf)));
221 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
222 sdb_strbuf_destroy(buf);
223 sdb_object_deref(SDB_OBJ(host));
224 return -1;
225 }
227 if (sdb_store_host_tojson(host, buf, filter, /* flags = */ 0)) {
228 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
229 "host '%s' to JSON", name);
230 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
231 sdb_strbuf_destroy(buf);
232 sdb_object_deref(SDB_OBJ(host));
233 return -1;
234 }
236 sdb_connection_send(conn, CONNECTION_OK,
237 (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
238 sdb_strbuf_destroy(buf);
239 sdb_object_deref(SDB_OBJ(host));
240 return 0;
241 } /* sdb_fe_exec_fetch */
243 int
244 sdb_fe_exec_list(sdb_conn_t *conn, sdb_store_matcher_t *filter)
245 {
246 sdb_strbuf_t *buf;
248 buf = sdb_strbuf_create(1024);
249 if (! buf) {
250 char errbuf[1024];
251 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
252 "buffer to handle LIST command: %s",
253 sdb_strerror(errno, errbuf, sizeof(errbuf)));
255 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
256 sdb_strbuf_destroy(buf);
257 return -1;
258 }
260 if (sdb_store_tojson(buf, filter, /* flags = */ SDB_SKIP_ALL)) {
261 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
262 "store to JSON");
263 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
264 sdb_strbuf_destroy(buf);
265 return -1;
266 }
268 sdb_connection_send(conn, CONNECTION_OK,
269 (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
270 sdb_strbuf_destroy(buf);
271 return 0;
272 } /* sdb_fe_exec_list */
274 int
275 sdb_fe_exec_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m,
276 sdb_store_matcher_t *filter)
277 {
278 tojson_data_t data = { NULL, filter, 0 };
280 data.buf = sdb_strbuf_create(1024);
281 if (! data.buf) {
282 char errbuf[1024];
283 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
284 "buffer to handle LOOKUP command: %s",
285 sdb_strerror(errno, errbuf, sizeof(errbuf)));
287 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
288 sdb_strbuf_destroy(data.buf);
289 return -1;
290 }
292 sdb_strbuf_append(data.buf, "[");
294 /* Let the JSON serializer handle the filter instead of the scanner. Else,
295 * we'd have to filter twice -- once in the scanner and then again in the
296 * serializer. */
297 data.last_len = sdb_strbuf_len(data.buf);
298 if (sdb_store_scan(m, /* filter */ NULL, lookup_tojson, &data)) {
299 sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts");
300 sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts");
301 sdb_strbuf_destroy(data.buf);
302 return -1;
303 }
305 sdb_strbuf_append(data.buf, "]");
307 sdb_connection_send(conn, CONNECTION_OK,
308 (uint32_t)sdb_strbuf_len(data.buf), sdb_strbuf_string(data.buf));
309 sdb_strbuf_destroy(data.buf);
310 return 0;
311 } /* sdb_fe_exec_lookup */
313 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */