File: classic-sql-join.c

package info (click to toggle)
libpreludedb 5.2.0-2.2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,232 kB
  • sloc: ansic: 29,652; cpp: 16,567; sh: 12,736; sql: 1,436; python: 449; makefile: 291; yacc: 227; lex: 106; xml: 36
file content (392 lines) | stat: -rw-r--r-- 11,389 bytes parent folder | download | duplicates (3)
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*****
*
* Copyright (C) 2005-2020 CS GROUP - France. All Rights Reserved.
* Author: Nicolas Delon <nicolas.delon@prelude-ids.com>
*
* This file is part of the PreludeDB library.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*****/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include <libprelude/prelude.h>

#include "preludedb-path-selection.h"
#include "preludedb-sql-settings.h"
#include "preludedb-sql.h"
#include "preludedb-error.h"

#include "classic-sql-join.h"


struct classic_sql_joined_table {
        prelude_list_t list;
        const idmef_path_t *path;
        char *table_name;
        char aliased_table_name[16];
        char parent_type;
        prelude_string_t *index_constraints;
};


struct classic_sql_join {
        idmef_class_id_t top_class;
        prelude_list_t tables;
        unsigned int next_id;
};



int classic_sql_join_new(classic_sql_join_t **join)
{
        *join = calloc(1, sizeof (**join));
        if ( ! *join )
                return prelude_error_from_errno(errno);

        prelude_list_init(&(*join)->tables);

        return 0;
}



void classic_sql_join_destroy(classic_sql_join_t *join)
{
        prelude_list_t *tmp, *next;
        classic_sql_joined_table_t *table;

        prelude_list_for_each_safe(&join->tables, tmp, next) {
                table = prelude_list_entry(tmp, classic_sql_joined_table_t, list);
                free(table->table_name);
                prelude_string_destroy(table->index_constraints);
                prelude_list_del(&table->list);
                free(table);
        }

        free(join);
}



void classic_sql_join_set_top_class(classic_sql_join_t *join, idmef_class_id_t top_class)
{
        join->top_class = top_class;
}


classic_sql_joined_table_t *classic_sql_join_lookup_table(const classic_sql_join_t *join, const idmef_path_t *path)
{
        prelude_list_t *tmp;
        classic_sql_joined_table_t *table;
        unsigned int depth;
        int last_element_is_listed;
        int ret;

        depth = idmef_path_get_depth(path);
        last_element_is_listed = ((ret = idmef_path_get_index(path, depth - 1)) > -1 ||
                                  prelude_error_get_code(ret) != PRELUDE_ERROR_IDMEF_PATH_INDEX_FORBIDDEN);

        prelude_list_for_each(&join->tables, tmp) {
                table = prelude_list_entry(tmp, classic_sql_joined_table_t, list);
                if ( depth == idmef_path_get_depth(table->path) ) {
                        if ( last_element_is_listed || (idmef_path_get_value_type(path, -1) == IDMEF_VALUE_TYPE_TIME && idmef_path_get_class(path, depth - 2) != IDMEF_CLASS_ID_FILE) )
                                ret = idmef_path_compare(path, table->path);
                        else
                                ret = idmef_path_ncompare(path, table->path, depth - 1);


                        if ( ret == 0 )
                                return table;
                }
        }

        return NULL;
}



static char resolve_file_parent_type(const idmef_path_t *path)
{
        if ( idmef_path_get_class(path, 3) == IDMEF_CLASS_ID_FILE_ACCESS &&
             idmef_path_get_class(path, 4) == IDMEF_CLASS_ID_USER_ID )
                return 'F';

        return 0;
}



static char resolve_target_parent_type(const idmef_path_t *path)
{
        if ( idmef_path_get_depth(path) == 3 )
                return 0;

        if ( idmef_path_get_class(path, 2) == IDMEF_CLASS_ID_FILE )
                return resolve_file_parent_type(path);

        return 'T';
}



static char resolve_source_parent_type(const idmef_path_t *path)
{
        return (idmef_path_get_depth(path) > 3) ? 'S' : 0;
}



static char resolve_tool_alert_parent_type(const idmef_path_t *path)
{
        if ( idmef_path_get_class(path, 2) == IDMEF_CLASS_ID_ALERTIDENT )
                return 'T';

        return 0;
}



static char resolve_correlation_alert_parent_type(const idmef_path_t *path)
{
        if ( idmef_path_get_class(path, 2) == IDMEF_CLASS_ID_ALERTIDENT )
                return 'C';

        return 0;
}



static char resolve_alert_parent_type(const idmef_path_t *path)
{
        switch ( idmef_path_get_class(path, 1) ) {
        case IDMEF_CLASS_ID_CLASSIFICATION:
        case IDMEF_CLASS_ID_ASSESSMENT:
        case IDMEF_CLASS_ID_OVERFLOW_ALERT:
                return 0;

        case IDMEF_CLASS_ID_TOOL_ALERT:
                return resolve_tool_alert_parent_type(path);

        case IDMEF_CLASS_ID_CORRELATION_ALERT:
                return resolve_correlation_alert_parent_type(path);

        case IDMEF_CLASS_ID_SOURCE:
                return resolve_source_parent_type(path);

        case IDMEF_CLASS_ID_TARGET:
                return resolve_target_parent_type(path);

        default:
                /* nop */;
        }

        if ( strcmp(idmef_path_get_name(path, idmef_path_get_depth(path) - 1), "detect_time") == 0 )
                return 0;

        return 'A';
}



static char resolve_parent_type(const idmef_path_t *path)
{
        if ( idmef_path_get_class(path, 0) == IDMEF_CLASS_ID_HEARTBEAT )
                return 'H';

        return resolve_alert_parent_type(path);
}



static int add_index_constraint(classic_sql_joined_table_t *table, int parent_level, int index)
{
        int ret;
        const char *operator;

        if ( ! prelude_string_is_empty(table->index_constraints) ) {
                ret = prelude_string_cat(table->index_constraints, " AND ");
                if ( ret < 0 )
                        return ret;
        }

        if ( index >= -1 )
                operator = "=";
        else {
                /* index is PRELUDE_ERROR_IDMEF_PATH_INDEX_UNDEFINED */
                index = -1;
                operator = "!=";
        }

        if ( parent_level == -1 )
                ret = prelude_string_sprintf(table->index_constraints, "%s._index %s %d",
                                             table->aliased_table_name, operator, index);
        else
                ret = prelude_string_sprintf(table->index_constraints, "%s._parent%d_index %s %d",
                                             table->aliased_table_name, parent_level, operator, index);

        return ret;
}



static int resolve_indexes(classic_sql_joined_table_t *table)
{
        unsigned int depth;
        unsigned int max_depth;
        unsigned int parent_level;
        int index, index1, index2;
        int ret = 0;

        max_depth = idmef_path_get_depth(table->path);
        if ( max_depth < 2 )
                return preludedb_error(PRELUDEDB_ERROR_QUERY);

        parent_level = 0;

        for ( depth = 1; depth < max_depth - 2; depth++ ) {
                index = idmef_path_get_index(table->path, depth);
                if ( prelude_error_get_code(index) == PRELUDE_ERROR_IDMEF_PATH_INDEX_FORBIDDEN )
                        continue;

                ret = add_index_constraint(table, parent_level, index);
                if ( ret < 0 )
                        return ret;

                parent_level++;
        }

        index1 = idmef_path_get_index(table->path, max_depth - 1);
        index2 = idmef_path_get_index(table->path, max_depth - 2);

        if ( prelude_error_get_code(index = index1) != PRELUDE_ERROR_IDMEF_PATH_INDEX_FORBIDDEN ||
             prelude_error_get_code(index = index2) != PRELUDE_ERROR_IDMEF_PATH_INDEX_FORBIDDEN )
                ret = add_index_constraint(table, -1, index);

        return ret;
}



int classic_sql_join_new_table(classic_sql_join_t *join, classic_sql_joined_table_t **table,
                               const idmef_path_t *path, char *table_name)
{
        int ret;
        idmef_class_id_t top_class;

        top_class = idmef_path_get_class(path, 0);

        if ( join->top_class ) {
                if ( top_class != join->top_class )
                        return -1; /* conflicting top tables */

        } else {
                join->top_class = top_class;
        }

        *table = calloc(1, sizeof (**table));
        if ( ! *table )
                return prelude_error_from_errno(errno);

        ret = prelude_string_new(&(*table)->index_constraints);
        if ( ret < 0 ) {
                free(*table);
                return ret;
        }

        (*table)->path = path;
        (*table)->table_name = table_name;
        sprintf((*table)->aliased_table_name, "t%d", join->next_id++);

        (*table)->parent_type = resolve_parent_type((*table)->path);

        ret = resolve_indexes(*table);
        if ( ret < 0 ) {
                prelude_string_destroy((*table)->index_constraints);
                free((*table)->table_name);
                free(*table);
                return ret;
        }

        prelude_list_add_tail(&join->tables, &(*table)->list);

        return 0;
}



const char *classic_sql_joined_table_get_name(classic_sql_joined_table_t *table)
{
        return table->aliased_table_name;
}



static int classic_joined_table_to_string(classic_sql_joined_table_t *table, prelude_string_t *output)
{
        int ret;

        ret = prelude_string_sprintf(output, " LEFT JOIN %s AS %s ON (", table->table_name, table->aliased_table_name);
        if ( ret < 0 )
                return ret;

        if ( table->parent_type ) {
                ret = prelude_string_sprintf(output, "%s._parent_type='%c' AND ",
                                             table->aliased_table_name, table->parent_type);
                if ( ret < 0 )
                        return ret;
        }

        ret = prelude_string_sprintf(output, "%s._message_ident=top_table._ident", table->aliased_table_name);
        if ( ret < 0 )
                return ret;

        if ( ! prelude_string_is_empty(table->index_constraints) ) {
                ret = prelude_string_sprintf(output, " AND %s", prelude_string_get_string(table->index_constraints));
                if ( ret < 0 )
                        return ret;
        }

        return prelude_string_cat(output, ")");
}



int classic_sql_join_to_string(classic_sql_join_t *join, prelude_string_t *output)
{
        prelude_list_t *tmp;
        classic_sql_joined_table_t *table;
        int ret;

        ret = prelude_string_sprintf(output, "%s AS top_table",
                                     (join->top_class == IDMEF_CLASS_ID_ALERT) ? "Prelude_Alert" : "Prelude_Heartbeat");
        if ( ret < 0 )
                return ret;

        prelude_list_for_each(&join->tables, tmp) {
                table = prelude_list_entry(tmp, classic_sql_joined_table_t, list);
                ret = classic_joined_table_to_string(table, output);
                if ( ret < 0 )
                        return ret;
        }

        return 0;
}