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 "utils/error.h"
33 #include "utils/strbuf.h"
35 #include <errno.h>
37 /*
38 * private helper functions
39 */
41 static int
42 lookup_tojson(sdb_store_obj_t *obj, void *user_data)
43 {
44 sdb_strbuf_t *buf = user_data;
45 if (sdb_strbuf_len(buf) > 1)
46 sdb_strbuf_append(buf, ",");
47 return sdb_store_host_tojson(obj, buf,
48 /* filter = */ NULL, /* flags = */ 0);
49 } /* lookup_tojson */
51 /*
52 * public API
53 */
55 int
56 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node)
57 {
58 if (! node)
59 return -1;
61 switch (node->cmd) {
62 case CONNECTION_FETCH:
63 return sdb_fe_fetch(conn, CONN_FETCH(node)->name);
64 case CONNECTION_LIST:
65 return sdb_fe_list(conn);
66 case CONNECTION_LOOKUP:
67 return sdb_fe_lookup(conn, CONN_LOOKUP(node)->matcher->matcher);
69 default:
70 sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd);
71 return -1;
72 }
73 return -1;
74 } /* sdb_fe_exec */
76 int
77 sdb_fe_fetch(sdb_conn_t *conn, const char *name)
78 {
79 sdb_strbuf_t *buf;
80 sdb_store_obj_t *host;
82 host = sdb_store_get_host(name);
83 if (! host) {
84 sdb_log(SDB_LOG_DEBUG, "frontend: Failed to fetch host '%s': "
85 "not found", name);
87 sdb_strbuf_sprintf(conn->errbuf, "Host %s not found", name);
88 return -1;
89 }
91 buf = sdb_strbuf_create(1024);
92 if (! buf) {
93 char errbuf[1024];
94 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
95 "buffer to handle FETCH command: %s",
96 sdb_strerror(errno, errbuf, sizeof(errbuf)));
98 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
99 sdb_strbuf_destroy(buf);
100 sdb_object_deref(SDB_OBJ(host));
101 return -1;
102 }
104 if (sdb_store_host_tojson(host, buf,
105 /* filter = */ NULL, /* flags = */ 0)) {
106 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
107 "host '%s' to JSON", name);
108 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
109 sdb_strbuf_destroy(buf);
110 sdb_object_deref(SDB_OBJ(host));
111 return -1;
112 }
114 sdb_connection_send(conn, CONNECTION_OK,
115 (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
116 sdb_strbuf_destroy(buf);
117 sdb_object_deref(SDB_OBJ(host));
118 return 0;
119 } /* sdb_fe_fetch */
121 int
122 sdb_fe_list(sdb_conn_t *conn)
123 {
124 sdb_strbuf_t *buf;
126 buf = sdb_strbuf_create(1024);
127 if (! buf) {
128 char errbuf[1024];
129 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
130 "buffer to handle LIST command: %s",
131 sdb_strerror(errno, errbuf, sizeof(errbuf)));
133 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
134 sdb_strbuf_destroy(buf);
135 return -1;
136 }
138 if (sdb_store_tojson(buf,
139 /* filter = */ NULL, /* flags = */ SDB_SKIP_ALL)) {
140 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
141 "store to JSON");
142 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
143 sdb_strbuf_destroy(buf);
144 return -1;
145 }
147 sdb_connection_send(conn, CONNECTION_OK,
148 (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
149 sdb_strbuf_destroy(buf);
150 return 0;
151 } /* sdb_fe_list */
153 int
154 sdb_fe_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m)
155 {
156 sdb_strbuf_t *buf;
158 buf = sdb_strbuf_create(1024);
159 if (! buf) {
160 char errbuf[1024];
161 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
162 "buffer to handle LOOKUP command: %s",
163 sdb_strerror(errno, errbuf, sizeof(errbuf)));
165 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
166 sdb_strbuf_destroy(buf);
167 return -1;
168 }
170 sdb_strbuf_append(buf, "[");
172 if (sdb_store_scan(m, /* filter */ NULL, lookup_tojson, buf)) {
173 sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts");
174 sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts");
175 sdb_strbuf_destroy(buf);
176 return -1;
177 }
179 sdb_strbuf_append(buf, "]");
181 sdb_connection_send(conn, CONNECTION_OK,
182 (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
183 sdb_strbuf_destroy(buf);
184 return 0;
185 } /* sdb_fe_lookup */
187 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */