Debian Codegraph @

src/OVAL/probes/independent/sql_probe.c

raw symbols SCIP
1 /**
2 * @file sql_probe.c
3 * @brief sql probe
4 * @author "Daniel Kopecek" <[email protected]>
5 *
6 */
7
8 /*
9 * Copyright 2009--2013 Red Hat Inc., Durham, North Carolina.
10 * All Rights Reserved.
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 *
26 * Authors:
27 * "Daniel Kopecek" <[email protected]>
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #include "_seap.h"
35 #include <probe-api.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <common/debug_priv.h>
40 #include <common/bfind.h>
41 #include <time.h>
42 #include <errno.h>
43 #include <opendbx/api.h>
44 #include "sql_probe.h"
45
46 #ifndef SQLPROBE_DEFAULT_CONNTIMEOUT
47 # define SQLPROBE_DEFAULT_CONNTIMEOUT 30
48 #endif
49
50 typedef struct {
51 char *o_engine; /* object engine */
52 char *b_engine; /* backend engine */
53 } dbEngineMap_t;
54
55 static dbEngineMap_t engine_map[] = {
56 { "access", NULL },
57 { "db2", NULL },
58 { "cache", NULL },
59 { "firebird", "firebird" },
60 { "firstsql", NULL },
61 { "foxpro", NULL },
62 { "informix", NULL },
63 { "ingres", NULL },
64 { "interbase", NULL },
65 { "lightbase", NULL },
66 { "maxdb", NULL },
67 { "monetdb", NULL },
68 { "mimer", NULL },
69 { "mssql", "mssql" }, /* non-standard */
70 { "mysql", "mysql" }, /* non-standard */
71 { "oracle", "oracle" },
72 { "paradox", NULL },
73 { "pervasive", NULL },
74 { "postgre", "pgsql" },
75 { "sqlbase", NULL },
76 { "sqlite", "sqlite" },
77 { "sqlite3", "sqlite3" }, /* non-standard */
78 { "sqlserver", NULL },
79 { "sybase", "sybase" }
80 };
81
82 #define ENGINE_MAP_COUNT (sizeof engine_map / sizeof (dbEngineMap_t))
83
84 static int engine_cmp (const char *a, const dbEngineMap_t *b)
85 {
86 if (a == NULL || b == NULL) {
87 return -1;
88 }
89 return strcmp(a, b->o_engine);
90 }
91
92 typedef struct {
93 char *host;
94 char *port;
95 char *user;
96 char *pass;
97 char *db;
98 long conn_timeout;
99 } dbURIInfo_t;
100
101 static void __clearmem(void *ptr, int len)
102 {
103 if (ptr != NULL) {
104 do {
105 register int l = len / sizeof(uint32_t) - 1;
106 register uint32_t *p = (uint32_t *)ptr;
107
108 while (l >= 0) {
109 p[l] = (uint32_t)random();
110 --l;
111 }
112 } while (0);
113
114 do {
115 register int l = len % sizeof(uint32_t);
116 register uint8_t *p = (uint8_t *)ptr;
117
118 for (int i = 0; i < l; i++) {
119 p[i] = (uint8_t)(random() % (1 << 8));
120 }
121 } while (0);
122 }
123 return;
124 }
125
126 static void dbURIInfo_clear(dbURIInfo_t *ui)
127 {
128 if (ui == NULL)
129 return;
130
131 srandom((long)clock()^(long)(ui->pass));
132
133 if (ui->host != NULL) {
134 __clearmem(ui->host, strlen(ui->host));
135 free(ui->host);
136 }
137
138 if (ui->user != NULL) {
139 __clearmem(ui->user, strlen(ui->user));
140 free(ui->user);
141 }
142
143 if (ui->pass != NULL) {
144 __clearmem(ui->pass, strlen(ui->pass));
145 free(ui->pass);
146 }
147
148 if (ui->db != NULL) {
149 __clearmem(ui->db, strlen(ui->db));
150 free(ui->db);
151 }
152
153 ui->host = NULL;
154 ui->port = 0;
155 ui->user = NULL;
156 ui->pass = NULL;
157 ui->db = NULL;
158
159 return;
160 }
161
162 static int dbURIInfo_parse(dbURIInfo_t *info, const char *conn)
163 {
164 char *tmp, *tok, *copy;
165 char *conn_copy = strdup(conn);
166 copy = conn_copy;
167 if (copy == NULL)
168 return (-1);
169
170 #define skipspace(s) while (isspace(*s)) ++s;
171
172 #define matchitem1(tok, first, rest, dst) \
173 case first: \
174 if (strncasecmp((rest), ++(tok), strlen(rest)) == 0) { \
175 tok += strlen(rest); \
176 skipspace(tok); \
177 if (*(tok) != '=') goto __fail; \
178 else (dst) = strdup((tok) + 1); \
179 } \
180 else { \
181 dE("Unrecognized token: '%s'", (tok)-1); \
182 }
183
184 #define matchitem2(tok, first, rest1, dst1, rest2, dst2) \
185 case first: \
186 if (strncasecmp((rest1), (tok)+1, strlen(rest1)) == 0) { \
187 tok += 1+strlen(rest1); \
188 skipspace(tok); \
189 if (*(tok) != '=') goto __fail; \
190 else (dst1) = strdup((tok) + 1); \
191 } \
192 else if (strncasecmp((rest2), (tok)+1, strlen(rest2)) == 0) { \
193 tok += 1+strlen(rest2); \
194 skipspace(tok); \
195 if (*(tok) != '=') goto __fail; \
196 else (dst2) = strdup((tok) + 1); \
197 } \
198 else { \
199 dE("Unrecognized token: '%s'", (tok)); \
200 }
201
202 tmp = NULL;
203
204 while ((tok = strsep (&copy, ";")) != NULL) {
205 dD("tok: '%s'.", tok);
206 switch (tolower(*tok)) {
207 matchitem1(tok, 's',
208 "erver", info->host); break;
209 matchitem2(tok, 'p',
210 "ort", info->port,
211 "wd", info->pass); break;
212 matchitem1(tok, 'd',
213 "atabase", info->db); break;
214 matchitem1(tok, 'u',
215 "id",info->user); break;
216 matchitem1(tok, 'c',
217 "onnecttimeout", tmp);
218 if (tmp != NULL) {
219 errno = 0;
220 info->conn_timeout = strtol(tmp, NULL, 10);
221
222 if (errno == ERANGE || errno == EINVAL)
223 info->conn_timeout = SQLPROBE_DEFAULT_CONNTIMEOUT;
224
225 free(tmp);
226 }
227 break;
228
229 case '\0':
230 break;
231 default:
232 dE("Unrecognized token: '%s'", (tok));
233 break;
234 }
235 }
236
237 free(conn_copy);
238 return (0);
239 __fail:
240 dE("Parsing failed.");
241 free(conn_copy);
242 return (-1);
243 }
244
245 static int dbSQL_eval(const char *engine, const char *version,
246 const char *conn, const char *sql, probe_ctx *ctx)
247 {
248 int err = -1;
249 dbURIInfo_t uriInfo = { .host = NULL,
250 .port = 0,
251 .user = NULL,
252 .pass = NULL,
253 .db = NULL};
254
255 /*
256 * parse the connection string
257 */
258 if (dbURIInfo_parse(&uriInfo, conn) != 0) {
259 dE("Malformed connection string: %s", conn);
260 goto __exit;
261 } else {
262 int sql_err = 0;
263 odbx_t *sql_dbh = NULL; /* handle */
264 dbEngineMap_t *sql_dbe; /* engine */
265 odbx_result_t *sql_dbr; /* result */
266 const char *sql_dbv; /* value */
267 SEXP_t *item;
268
269 sql_dbe = oscap_bfind (engine_map, ENGINE_MAP_COUNT, sizeof(dbEngineMap_t), (char *)engine,
270 (int(*)(void *, void *))&engine_cmp);
271
272 if (sql_dbe == NULL) {
273 SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
274 "DB engine not found: %s", engine);
275 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
276 SEXP_free(msg);
277 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
278 err = 0;
279 dE("DB engine not found: %s", engine);
280 goto __exit;
281 }
282
283 if (sql_dbe->b_engine == NULL) {
284 SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
285 "DB engine not supported: %s", engine);
286 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
287 SEXP_free(msg);
288 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
289 err = 0;
290 dE("DB engine not supported: %s", engine);
291 goto __exit;
292 }
293
294 int odbx_res = odbx_init (&sql_dbh, sql_dbe->b_engine,
295 uriInfo.host, uriInfo.port);
296 if (odbx_res != ODBX_ERR_SUCCESS) {
297 const char *error_msg = odbx_error(NULL, odbx_res);
298 dE("odbx_init failed: e=%s, h=%s:%s msg=%s",
299 sql_dbe->b_engine, uriInfo.host, uriInfo.port,
300 error_msg != NULL ? error_msg : "(none)");
301 if (odbx_res == -ODBX_ERR_NOTEXIST) {
302 SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
303 "odbx_init failed. Please install the opendbx %s backend", engine);
304 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
305 SEXP_free(msg);
306 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
307 err = 0;
308 fprintf(stderr, "Could not connect to the database. "
309 "Please install the opendbx %s backend.\n",
310 sql_dbe->b_engine);
311 }
312 goto __exit;
313 }
314
315 /* set options */
316 odbx_res = odbx_bind(sql_dbh, uriInfo.db, uriInfo.user, uriInfo.pass, ODBX_BIND_SIMPLE);
317 if (odbx_res != ODBX_ERR_SUCCESS)
318 {
319 const char *error_msg = odbx_error(sql_dbh, odbx_res);
320 SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
321 "odbx_bind failed. Could not connect to the database '%s': %s",
322 uriInfo.db, error_msg != NULL ? error_msg : "(none)");
323 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
324 SEXP_free(msg);
325 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
326 err = 0;
327 dE("odbx_bind failed: db=%s, u=%s, p=%s",
328 uriInfo.db, uriInfo.user, uriInfo.pass);
329 odbx_finish(sql_dbh);
330 goto __exit;
331 }
332
333 if (odbx_query(sql_dbh, sql, strlen (sql)) != ODBX_ERR_SUCCESS) {
334 dE("odbx_query failed: q=%s", sql);
335 odbx_finish(sql_dbh);
336
337 goto __exit;
338 } else {
339 SEXP_t *r0;
340
341 sql_dbr = NULL;
342 item = probe_item_create(OVAL_INDEPENDENT_SQL, NULL,
343 "engine", OVAL_DATATYPE_STRING, engine,
344 "version", OVAL_DATATYPE_STRING, version,
345 "sql", OVAL_DATATYPE_STRING, sql,
346 "connection_string", OVAL_DATATYPE_STRING, conn,
347 NULL);
348
349 while ((sql_err = odbx_result (sql_dbh, &sql_dbr, NULL, 0)) == ODBX_RES_ROWS) {
350 if (odbx_column_count(sql_dbr) != 1) {
351 dE("Don't how to handle result, column count != 1");
352 odbx_result_finish(sql_dbr);
353 SEXP_free(item);
354 goto __exit;
355 }
356
357 while (odbx_row_fetch(sql_dbr) == ODBX_ROW_NEXT) {
358 sql_dbv = odbx_field_value (sql_dbr, 0);
359 /* TODO: support for other "simple" types */
360 probe_item_ent_add(item, "result", NULL,
361 r0 = SEXP_string_newf(sql_dbv, strlen(sql_dbv)));
362 SEXP_free(r0);
363 }
364
365 odbx_result_finish(sql_dbr);
366 }
367
368 probe_item_collect(ctx, item);
369 }
370
371 if (odbx_finish(sql_dbh) != ODBX_ERR_SUCCESS) {
372 dE("odbx_finish failed");
373 goto __exit;
374 }
375 }
376
377 err = 0;
378 __exit:
379 dbURIInfo_clear(&uriInfo);
380 return (err);
381 }
382
383 int sql_probe_main(probe_ctx *ctx, void *arg)
384 {
385 char *engine, *version, *conn, *sqlexp;
386 int err;
387 SEXP_t *probe_in;
388
389 #define get_string(dst, obj, ent_name) \
390 do { \
391 SEXP_t *__sval; \
392 \
393 __sval = probe_obj_getentval (obj, ent_name, 1); \
394 \
395 if (__sval == NULL) { \
396 dE("Missing entity or value: obj=%p, ent=%s", obj, #ent_name); \
397 err = PROBE_ENOENT; \
398 goto __exit; \
399 } \
400 \
401 (dst) = SEXP_string_cstr (__sval); \
402 \
403 if ((dst) == NULL) { \
404 SEXP_free(__sval); \
405 err = PROBE_EINVAL; \
406 goto __exit; \
407 } \
408 \
409 SEXP_free(__sval); \
410 } while (0)
411
412 engine = NULL;
413 version = NULL;
414 conn = NULL;
415 sqlexp = NULL;
416
417 probe_in = probe_ctx_getobject(ctx);
418
419 get_string(engine, probe_in, "engine");
420 get_string(version, probe_in, "version");
421 get_string(conn, probe_in, "connection_string");
422 get_string(sqlexp, probe_in, "sql");
423
424 /*
425 * evaluate the SQL statement
426 */
427 err = dbSQL_eval(engine, version, conn, sqlexp, ctx);
428 __exit:
429 if (engine != NULL) {
430 __clearmem(engine, strlen(engine));
431 free(engine);
432 }
433
434 if (sqlexp != NULL) {
435 __clearmem(sqlexp, strlen(sqlexp));
436 free(sqlexp);
437 }
438
439 if (conn != NULL) {
440 __clearmem(conn, strlen(conn));
441 free(conn);
442 }
443
444 if (version != NULL) {
445 __clearmem(version, strlen(version));
446 free(version);
447 }
448
449 return (err);
450 }