Debian Codegraph @

src/OVAL/probes/independent/sql57_probe.c

raw symbols SCIP
1 /**
2 * @file sql57_probe.c
3 * @brief sql57 probe
4 * @author "Daniel Kopecek" <[email protected]>
5 *
6 */
7
8 /*
9 * Copyright 2011--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 "sql57_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" }, /* standard since OVAL 5.10 */
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 SEXP_t *item;
267
268 sql_dbe = oscap_bfind (engine_map, ENGINE_MAP_COUNT, sizeof(dbEngineMap_t), (char *)engine,
269 (int(*)(void *, void *))&engine_cmp);
270
271 if (sql_dbe == NULL) {
272 SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
273 "DB engine not found: %s", engine);
274 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
275 SEXP_free(msg);
276 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
277 err = 0;
278 dE("DB engine not found: %s", engine);
279 goto __exit;
280 }
281
282 if (sql_dbe->b_engine == NULL) {
283 SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
284 "DB engine not supported: %s", engine);
285 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
286 SEXP_free(msg);
287 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
288 err = 0;
289 dE("DB engine not supported: %s", engine);
290 goto __exit;
291 }
292
293 int odbx_res = odbx_init (&sql_dbh, sql_dbe->b_engine,
294 uriInfo.host, uriInfo.port);
295 if (odbx_res != ODBX_ERR_SUCCESS) {
296 const char *error_msg = odbx_error(NULL, odbx_res);
297 dE("odbx_init failed: e=%s, h=%s:%s msg=%s",
298 sql_dbe->b_engine, uriInfo.host, uriInfo.port,
299 error_msg != NULL ? error_msg : "(none)");
300 if (odbx_res == -ODBX_ERR_NOTEXIST) {
301 SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
302 "odbx_init failed. Please install the opendbx %s backend", engine);
303 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
304 SEXP_free(msg);
305 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
306 err = 0;
307 fprintf(stderr, "Could not connect to the database. "
308 "Please install the opendbx %s backend.\n",
309 sql_dbe->b_engine);
310 }
311 goto __exit;
312 }
313
314 /* set options */
315 odbx_res = odbx_bind(sql_dbh, uriInfo.db, uriInfo.user, uriInfo.pass, ODBX_BIND_SIMPLE);
316 if (odbx_res != ODBX_ERR_SUCCESS)
317 {
318 const char *error_msg = odbx_error(sql_dbh, odbx_res);
319 SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
320 "odbx_bind failed. Could not connect to the database '%s': %s",
321 uriInfo.db, error_msg != NULL ? error_msg : "(none)");
322 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
323 SEXP_free(msg);
324 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
325 err = 0;
326 dE("odbx_bind failed: db=%s, u=%s, p=%s",
327 uriInfo.db, uriInfo.user, uriInfo.pass);
328 odbx_finish(sql_dbh);
329 goto __exit;
330 }
331
332 if (odbx_query(sql_dbh, sql, strlen (sql)) != ODBX_ERR_SUCCESS) {
333 dE("odbx_query failed: q=%s", sql);
334 odbx_finish(sql_dbh);
335
336 goto __exit;
337 } else {
338 sql_dbr = NULL;
339 item = probe_item_create(OVAL_INDEPENDENT_SQL57, NULL,
340 "engine", OVAL_DATATYPE_STRING, engine,
341 "version", OVAL_DATATYPE_STRING, version,
342 "connection_string", OVAL_DATATYPE_STRING, conn,
343 "sql", OVAL_DATATYPE_STRING, sql,
344 NULL);
345
346 while ((sql_err = odbx_result (sql_dbh, &sql_dbr, NULL, 0)) == ODBX_RES_ROWS) {
347 while (odbx_row_fetch(sql_dbr) == ODBX_ROW_NEXT) {
348 SEXP_t se_tmp_mem, *field, *result;
349 const char *col_val, *col_name;
350 oval_datatype_t col_type;
351 unsigned int ci;
352
353 result = probe_ent_creat1("result", NULL, NULL);
354 probe_ent_setdatatype(result, OVAL_DATATYPE_RECORD);
355
356 for (ci = 0; ci < odbx_column_count(sql_dbr); ++ci) {
357 col_val = odbx_field_value (sql_dbr, ci);
358 col_name = odbx_column_name (sql_dbr, ci);
359 col_type = OVAL_DATATYPE_UNKNOWN;
360 field = NULL;
361
362 dD("Column type: %d.", odbx_column_type(sql_dbr, ci));
363 switch(odbx_column_type(sql_dbr, ci)) {
364 case ODBX_TYPE_BOOLEAN:
365 break;
366 case ODBX_TYPE_INTEGER:
367 case ODBX_TYPE_SMALLINT: {
368 char *end = NULL;
369 int val = strtol(col_val, &end, 10);
370
371 if (val == 0 && (end == col_val))
372 dE("strtol(%s) failed", col_val);
373
374 field = probe_ent_creat1(col_name, NULL, SEXP_number_newi_r(&se_tmp_mem, val));
375 col_type = OVAL_DATATYPE_INTEGER;
376 SEXP_free_r(&se_tmp_mem);
377
378 } break;
379 case ODBX_TYPE_REAL:
380 case ODBX_TYPE_DOUBLE:
381 case ODBX_TYPE_FLOAT: {
382 char *end = NULL;
383 double val = strtod(col_val, &end);
384
385 if (val == 0 && (end == col_val))
386 dE("strtod(%s) failed", col_val);
387
388 field = probe_ent_creat1(col_name, NULL, SEXP_number_newf_r(&se_tmp_mem, val));
389 col_type = OVAL_DATATYPE_FLOAT;
390 SEXP_free_r(&se_tmp_mem);
391
392 } break;
393 case ODBX_TYPE_CHAR:
394 case ODBX_TYPE_NCHAR:
395 case ODBX_TYPE_VARCHAR:
396 case ODBX_TYPE_CLOB:
397 field = probe_ent_creat1(col_name, NULL, SEXP_string_new_r(&se_tmp_mem, col_val, strlen(col_val)));
398 col_type = OVAL_DATATYPE_STRING;
399 SEXP_free_r(&se_tmp_mem);
400 break;
401 case ODBX_TYPE_TIMESTAMP:
402 break;
403 default:
404 dW("Unsupported ODBX type: %d.", odbx_column_type(sql_dbr, ci));
405 break;
406 }
407
408 if (field != NULL) {
409 probe_ent_setdatatype(field, col_type);
410
411 SEXP_list_add(result, field);
412 SEXP_free(field);
413 }
414 }
415
416 SEXP_list_add(item, result);
417 SEXP_free(result);
418 }
419
420 odbx_result_finish(sql_dbr);
421 }
422
423 probe_item_collect(ctx, item);
424 }
425
426 if (odbx_finish(sql_dbh) != ODBX_ERR_SUCCESS) {
427 dE("odbx_finish failed");
428 goto __exit;
429 }
430 }
431
432 err = 0;
433 __exit:
434 dbURIInfo_clear(&uriInfo);
435 return (err);
436 }
437
438 int sql57_probe_main(probe_ctx *ctx, void *arg)
439 {
440 char *engine, *version, *conn, *sqlexp;
441 int err;
442 SEXP_t *probe_in;
443
444 #define get_string(dst, obj, ent_name) \
445 do { \
446 SEXP_t *__sval; \
447 \
448 __sval = probe_obj_getentval (obj, ent_name, 1); \
449 \
450 if (__sval == NULL) { \
451 dE("Missing entity or value: obj=%p, ent=%s", obj, #ent_name); \
452 err = PROBE_ENOENT; \
453 goto __exit; \
454 } \
455 \
456 (dst) = SEXP_string_cstr (__sval); \
457 \
458 if ((dst) == NULL) { \
459 SEXP_free(__sval); \
460 err = PROBE_EINVAL; \
461 goto __exit; \
462 } \
463 \
464 SEXP_free(__sval); \
465 } while (0)
466
467 engine = NULL;
468 version = NULL;
469 conn = NULL;
470 sqlexp = NULL;
471
472 probe_in = probe_ctx_getobject(ctx);
473
474 get_string(engine, probe_in, "engine");
475 get_string(version, probe_in, "version");
476 get_string(conn, probe_in, "connection_string");
477 get_string(sqlexp, probe_in, "sql");
478
479 /*
480 * evaluate the SQL statement
481 */
482 err = dbSQL_eval(engine, version, conn, sqlexp, ctx);
483 __exit:
484 if (engine != NULL) {
485 __clearmem(engine, strlen(engine));
486 free(engine);
487 }
488
489 if (sqlexp != NULL) {
490 __clearmem(sqlexp, strlen(sqlexp));
491 free(sqlexp);
492 }
493
494 if (conn != NULL) {
495 __clearmem(conn, strlen(conn));
496 free(conn);
497 }
498
499 if (version != NULL) {
500 __clearmem(version, strlen(version));
501 free(version);
502 }
503
504 return (err);
505 }