/
query.c
369 lines (310 loc) · 8.57 KB
/
query.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* sqler - a JavaScript-MySQL bridge in C
*
* Copyright (C) 2010 Pawel Foremski <pawel@foremski.pl>
* Licensed under GPLv3
*/
#include <ctype.h>
#include <rpcd/rpcd_module.h>
#include "common.h"
static char *get_query(struct req *req)
{
const char *orig_query;
int i;
bool inspace = false;
xstr *query;
query = xstr_create("", req);
orig_query = uth_char(req->params, "query");
i = sizeof SQLER_TAG;
if (i > xstr_length(uth_xstr(req->params, "query")))
goto end;
/* skip whitechars */
for (; orig_query[i]; i++)
if (isalpha(orig_query[i]))
break;
/* replace all whitechars, newlines, etc with single space */
for (; orig_query[i]; i++) {
switch (orig_query[i]) {
case ' ':
if (inspace) continue;
xstr_append_char(query, orig_query[i]);
inspace = true;
break;
case '\t':
case '\r':
case '\n':
break;
default:
xstr_append_char(query, orig_query[i]);
inspace = false;
}
}
end:
return xstr_string(query);
}
/* this probably needs a wise rewrite */
static char *fill_query(struct req *req, char *orig_query, tlist *data)
{
int i, qs;
enum fq_state { NORMAL, INQ } state = NORMAL;
xstr *query;
MYSQL *conn;
ut *arg, *el, *el2;
tlist *list, *list2;
bool atleastone, atleastone2;
#define iskeyw(a) (sizeof(a) == i - qs && strncmp((a), orig_query + qs + 1, sizeof(a) - 1) == 0)
/* XXX: uses list and el */
#define appendlist(utlist) do { \
atleastone = false; \
xstr_append(query, "("); \
list = ut_tlist(utlist); \
TLIST_ITER_LOOP(list, el) { \
if (atleastone) \
xstr_append_char(query, ','); \
xstr_append(query, \
pb("\"%s\"", escape(conn, ut_xstr(el)))); \
atleastone = true; \
} \
xstr_append(query, ")"); \
} while(0);
conn = uthp_ptr(req->prv, "sqler", "conn");
query = xstr_create("", req);
tlist_reset(data);
for (i = 0; orig_query[i]; i++) {
switch (state) {
case NORMAL:
if (orig_query[i] == '?') {
qs = i;
state = INQ;
} else {
xstr_append_char(query, orig_query[i]);
}
break;
case INQ:
if (orig_query[i] == '?') {
if (iskeyw("login")) {
xstr_append(query, pb("\"%s\"", uthp_char(req->prv, "sqler", "login")));
} else if (iskeyw("role")) {
xstr_append(query, pb("\"%s\"", uthp_char(req->prv, "sqler", "role")));
} else { /* probably needs an arg */
arg = tlist_iter(data);
if (arg) {
if (iskeyw("int")) {
xstr_append(query, pb("%d", ut_int(arg)));
} else if (iskeyw("str")) {
xstr_append(query, pb("\"%s\"", escape(conn, ut_xstr(arg))));
} else if (iskeyw("dbl")) {
xstr_append(query, pb("%g", ut_double(arg)));
} else if (iskeyw("login")) {
xstr_append(query, pb("\"%s\"", uthp_char(req->prv, "sqler", "login")));
} else if (iskeyw("role")) {
xstr_append(query, pb("\"%s\"", uthp_char(req->prv, "sqler", "role")));
} else if (iskeyw("array")) {
appendlist(arg);
} else if (iskeyw("arrays")) {
atleastone2 = false;
list2 = ut_tlist(arg);
TLIST_ITER_LOOP(list2, el2) {
if (atleastone2)
xstr_append_char(query, ',');
appendlist(el2);
atleastone2 = true;
}
}
/* XXX: arg eaten by unrecognizible substitution */
}
}
state = NORMAL;
} else if (orig_query[i] < 'a' || orig_query[i] > 'z') {
rollback:
while (qs <= i)
xstr_append_char(query, orig_query[qs++]);
state = NORMAL;
}
break;
}
}
dbg(8, "fill_query: '%s'\n", xstr_string(query));
return xstr_string(query);
}
/****************** SQL query scanner ******************/
static void scan_file(ut *queries, const char *filepath)
{
int i, j;
bool inspace = false;
char *file, *orig_query;
xstr *query;
file = asn_readfile(filepath, queries);
if (!file) {
dbg(1, "reading %s failed\n", filepath);
return;
}
/* stop on each "SQLER_TAG */
while ((file = strstr(file, "\"" SQLER_TAG))) {
file += sizeof SQLER_TAG;
for (i = 0; file[i]; i++) {
/* replace \n, \t and ending \ with spaces */
if (file[i] == '\n' || file[i] == '\t' ||
(file[i] == '\\' && file[i+1] == '\n')) {
file[i] = ' ';
continue;
}
if (file[i+1] == '"' && file[i] != '\\') {
file[i+1] = '\0';
orig_query = asn_trim(file);
/* replace all whitechars, newlines, etc with single space */
query = xstr_create("", queries);
for (j = 0; orig_query[j]; j++) {
switch (orig_query[j]) {
case ' ':
if (inspace) continue;
xstr_append_char(query, orig_query[j]);
inspace = true;
break;
case '\t':
case '\r':
case '\n':
break;
default:
xstr_append_char(query, orig_query[j]);
inspace = false;
}
}
dbg(10, "%s: %s\n", filepath, xstr_string(query));
uth_set_char(queries, xstr_string(query), filepath);
file += i + 2;
break;
}
}
}
}
static void scan_dir(ut *queries, const char *dirpath, const char *ext)
{
tlist *ls;
const char *name, *path, *dot;
ls = asn_ls(dirpath, queries);
TLIST_ITER_LOOP(ls, name) {
path = mmatic_printf(queries, "%s/%s", dirpath, name);
if (asn_isdir(path) == 1) {
scan_dir(queries, path, ext);
continue;
}
dot = strchr(name, '.');
if (!dot) continue;
if (!streq(dot + 1, ext)) continue;
scan_file(queries, path);
}
}
/*******************************************************/
static bool init(struct mod *mod)
{
thash *scan;
tlist *scanlist;
ut *v, *queries, *scandef;
const char *rolename;
char *path, *ext, *ast;
/*
* scan source code for sql queries
*/
scan = uth_thash(mod->cfg, "scan");
THASH_ITER_LOOP(scan, rolename, v) {
/* create storage point */
queries = uth_path_create(mod->dir->prv, "sqler", "roles", rolename, "queries");
scanlist = ut_tlist(v);
TLIST_ITER_LOOP(scanlist, scandef) {
/* unconst */
path = mmatic_strdup(ut_char(scandef), mod);
/* get dir path and file extension */
ext = NULL;
ast = strchr(path, '*');
if (ast) {
*ast = '\0';
if (ast[1] == '.')
ext = ast + 2;
else
ext = ast + 1;
if (!ext[0])
ext = SQLER_DEFAULT_EXT;
scan_dir(queries, path, ext);
} else {
scan_file(queries, path);
}
}
}
return true;
}
static bool handle(struct req *req)
{
MYSQL *conn;
thash *queries;
char *query;
conn = uthp_ptr(req->prv, "sqler", "conn");
queries = uthp_thash(req->prv, "sqler", "queries");
asnsert(conn);
/******* check the query *******/
query = get_query(req);
if (queries && !thash_get(queries, query))
return err(-EDENY, "Access denied", query);
/******* make the query ********/
MYSQL_RES *res;
query = fill_query(req, query, uth_tlist(req->params, "data"));
dbg(5, "executing: %s\n", query);
if (mysql_query(conn, query) != 0)
return sqlerr(-EQUERY, "SQL query failed");
/* check if we need to fetch anything back */
res = mysql_store_result(conn);
if (!res) {
/* probably an UPDATE, INSERT, etc. - fetch num of affected rows */
uth_set_int(req->reply, "insert_id", mysql_insert_id(conn));
uth_set_int(req->reply, "affected", mysql_affected_rows(conn));
mysql_free_result(res);
return true; /* we're done */
} else {
/* a SELECT -- fetch num of rows */
uth_set_int(req->reply, "rowcount", mysql_num_rows(res));
}
/******* fetch the results ********/
MYSQL_FIELD *fields;
MYSQL_ROW mrow;
ut *row, *rows, *columns;
unsigned int i, num;
rows = uth_set_tlist(req->reply, "rows", NULL);
fields = mysql_fetch_fields(res);
num = mysql_num_fields(res);
if (uth_bool(req->params, "verbose")) {
while ((mrow = mysql_fetch_row(res))) {
row = utl_add_thash(rows, NULL);
for (i = 0; i < num; i++) {
if (mrow[i] == NULL)
uth_set_null(row, fields[i].name);
else
uth_set_char(row, fields[i].name, mrow[i]);
}
}
} else {
ut *columns = uth_set_tlist(req->reply, "columns", NULL);
for (i = 0; i < num; i++)
utl_add_char(columns, fields[i].name);
while ((mrow = mysql_fetch_row(res))) {
row = utl_add_tlist(rows, NULL);
for (i = 0; i < num; i++) {
if (mrow[i] == NULL)
utl_add_null(row);
else
utl_add_char(row, mrow[i]);
}
}
}
return true;
}
struct api query_api = {
.tag = RPCD_TAG,
.init = init,
.handle = handle
};
struct fw query_fw[] = {
{ "query", true, T_STRING, NULL },
{ "verbose", false, T_BOOL, NULL },
{ "data", false, T_LIST, NULL },
NULL,
};