Code

frontend: Let sdb_fe_<cmd> check the current command.
[sysdb.git] / src / frontend / query.c
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)
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)
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)
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)
170         if (! node)
171                 return -1;
173         switch (node->cmd) {
174                 case CONNECTION_FETCH:
175                         return sdb_fe_exec_fetch(conn, CONN_FETCH(node)->name,
176                                         /* filter = */ NULL);
177                 case CONNECTION_LIST:
178                         return sdb_fe_exec_list(conn, /* filter = */ NULL);
179                 case CONNECTION_LOOKUP:
180                 {
181                         sdb_store_matcher_t *m = NULL, *filter = NULL;
182                         if (CONN_LOOKUP(node)->matcher)
183                                 m = CONN_LOOKUP(node)->matcher->matcher;
184                         if (CONN_LOOKUP(node)->filter)
185                                 filter = CONN_LOOKUP(node)->filter->matcher;
186                         return sdb_fe_exec_lookup(conn, m, filter);
187                 }
189                 default:
190                         sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd);
191                         return -1;
192         }
193         return -1;
194 } /* sdb_fe_exec */
196 int
197 sdb_fe_exec_fetch(sdb_conn_t *conn, const char *name,
198                 sdb_store_matcher_t *filter)
200         sdb_strbuf_t *buf;
201         sdb_store_obj_t *host;
203         host = sdb_store_get_host(name);
204         if (! host) {
205                 sdb_log(SDB_LOG_DEBUG, "frontend: Failed to fetch host '%s': "
206                                 "not found", name);
208                 sdb_strbuf_sprintf(conn->errbuf, "Host %s not found", name);
209                 return -1;
210         }
212         buf = sdb_strbuf_create(1024);
213         if (! buf) {
214                 char errbuf[1024];
215                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
216                                 "buffer to handle FETCH command: %s",
217                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
219                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
220                 sdb_strbuf_destroy(buf);
221                 sdb_object_deref(SDB_OBJ(host));
222                 return -1;
223         }
225         if (sdb_store_host_tojson(host, buf, filter, /* flags = */ 0)) {
226                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
227                                 "host '%s' to JSON", name);
228                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
229                 sdb_strbuf_destroy(buf);
230                 sdb_object_deref(SDB_OBJ(host));
231                 return -1;
232         }
234         sdb_connection_send(conn, CONNECTION_OK,
235                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
236         sdb_strbuf_destroy(buf);
237         sdb_object_deref(SDB_OBJ(host));
238         return 0;
239 } /* sdb_fe_exec_fetch */
241 int
242 sdb_fe_exec_list(sdb_conn_t *conn, sdb_store_matcher_t *filter)
244         sdb_strbuf_t *buf;
246         buf = sdb_strbuf_create(1024);
247         if (! buf) {
248                 char errbuf[1024];
249                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
250                                 "buffer to handle LIST command: %s",
251                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
253                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
254                 sdb_strbuf_destroy(buf);
255                 return -1;
256         }
258         if (sdb_store_tojson(buf, filter, /* flags = */ SDB_SKIP_ALL)) {
259                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
260                                 "store to JSON");
261                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
262                 sdb_strbuf_destroy(buf);
263                 return -1;
264         }
266         sdb_connection_send(conn, CONNECTION_OK,
267                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
268         sdb_strbuf_destroy(buf);
269         return 0;
270 } /* sdb_fe_exec_list */
272 int
273 sdb_fe_exec_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m,
274                 sdb_store_matcher_t *filter)
276         tojson_data_t data = { NULL, filter, 0 };
278         data.buf = sdb_strbuf_create(1024);
279         if (! data.buf) {
280                 char errbuf[1024];
281                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
282                                 "buffer to handle LOOKUP command: %s",
283                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
285                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
286                 sdb_strbuf_destroy(data.buf);
287                 return -1;
288         }
290         sdb_strbuf_append(data.buf, "[");
292         /* Let the JSON serializer handle the filter instead of the scanner. Else,
293          * we'd have to filter twice -- once in the scanner and then again in the
294          * serializer. */
295         data.last_len = sdb_strbuf_len(data.buf);
296         if (sdb_store_scan(m, /* filter */ NULL, lookup_tojson, &data)) {
297                 sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts");
298                 sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts");
299                 sdb_strbuf_destroy(data.buf);
300                 return -1;
301         }
303         sdb_strbuf_append(data.buf, "]");
305         sdb_connection_send(conn, CONNECTION_OK,
306                         (uint32_t)sdb_strbuf_len(data.buf), sdb_strbuf_string(data.buf));
307         sdb_strbuf_destroy(data.buf);
308         return 0;
309 } /* sdb_fe_exec_lookup */
311 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */