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
|
/*****
*
* Copyright (C) 2006 PreludeIDS Technologies. All Rights Reserved.
* Author: Yoann Vandoorselaere <yoann.v@prelude-ids.com>
*
* This file is part of the Prelude-LML program.
*
* 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; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*****/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <pcre.h>
#include <libprelude/prelude.h>
#include <libprelude/prelude-string.h>
#include "prelude-lml.h"
#include "pcre-mod.h"
#include "value-container.h"
struct value_container {
prelude_list_t list;
prelude_list_t value_item_list;
void *data;
};
typedef struct {
prelude_list_t list;
int refno;
char *value;
} value_item_t;
static int add_dynamic_object_value(value_container_t *vcont, unsigned int reference)
{
value_item_t *vitem;
if ( reference >= MAX_REFERENCE_PER_RULE ) {
prelude_log(PRELUDE_LOG_WARN, "reference number %d is too high.\n", reference);
return -1;
}
vitem = malloc(sizeof(*vitem));
if ( ! vitem ) {
prelude_log(PRELUDE_LOG_ERR, "memory exhausted.\n");
return -1;
}
vitem->value = NULL;
vitem->refno = reference;
prelude_list_add_tail(&vcont->value_item_list, &vitem->list);
return 0;
}
static int add_fixed_object_value(value_container_t *vcont, prelude_string_t *buf)
{
int ret;
value_item_t *vitem;
vitem = malloc(sizeof(*vitem));
if ( ! vitem ) {
prelude_log(PRELUDE_LOG_ERR, "memory exhausted.\n");
return -1;
}
ret = prelude_string_get_string_released(buf, &vitem->value);
if ( ret < 0 ) {
prelude_perror(ret, "error getting released string");
free(vitem);
return -1;
}
vitem->refno = -1;
prelude_list_add_tail(&vcont->value_item_list, &vitem->list);
return 0;
}
static int parse_value(value_container_t *vcont, const char *line)
{
int ret;
char num[10];
unsigned int i;
const char *str;
prelude_string_t *strbuf;
str = line;
while ( *str ) {
if ( *str == '$' && *(str + 1) != '$' ) {
i = 0;
str++;
while ( isdigit((int) *str) && i < (sizeof(num) - 1) )
num[i++] = *str++;
if ( ! i )
return -1;
num[i] = 0;
if ( add_dynamic_object_value(vcont, atoi(num)) < 0 )
return -1;
continue;
}
ret = prelude_string_new(&strbuf);
if ( ret < 0 ) {
prelude_perror(ret, "error creating new prelude-string");
return -1;
}
while ( *str ) {
if ( *str == '$' ) {
if ( *(str + 1) == '$' )
str++;
else
break;
}
if ( prelude_string_ncat(strbuf, str, 1) < 0 )
return -1;
str++;
}
if ( add_fixed_object_value(vcont, strbuf) < 0 )
return -1;
prelude_string_destroy(strbuf);
}
return 0;
}
static void resolve_referenced_value(value_item_t *vitem, const pcre_rule_t *rule,
const char *log_entry, int *ovector, size_t osize)
{
int ret;
ret = pcre_get_substring(log_entry, ovector, osize, vitem->refno, (const char **) &vitem->value);
if ( ret < 0 ) {
vitem->value = NULL;
if ( ret == PCRE_ERROR_NOMEMORY )
prelude_log(PRELUDE_LOG_WARN, "not enough memory to get backward reference %d.\n",
vitem->refno);
else if ( ret == PCRE_ERROR_NOSUBSTRING )
prelude_log(PRELUDE_LOG_WARN, "backward reference number %d does not exist in rule id %d.\n",
vitem->refno, rule->id);
else
prelude_log(PRELUDE_LOG_WARN, "unknown PCRE error while getting backward reference %d.\n",
vitem->refno);
}
}
prelude_string_t *value_container_resolve(value_container_t *vcont, const pcre_rule_t *rule,
const lml_log_entry_t *lentry, int *ovector, size_t osize)
{
int ret;
value_item_t *vitem;
prelude_list_t *tmp;
prelude_string_t *str;
ret = prelude_string_new(&str);
if ( ret < 0 ) {
prelude_perror(ret, "error creating prelude-string");
return NULL;
}
prelude_list_for_each(&vcont->value_item_list, tmp) {
vitem = prelude_list_entry(tmp, value_item_t, list);
if ( vitem->refno != -1 ) {
resolve_referenced_value(vitem, rule, lml_log_entry_get_message(lentry), ovector, osize);
if ( ! vitem->value )
continue;
}
ret = prelude_string_cat(str, vitem->value);
if ( vitem->refno != -1 && vitem->value )
pcre_free_substring(vitem->value);
if ( ret < 0 )
goto err;
}
if ( ! prelude_string_is_empty(str) )
return str;
err:
prelude_string_destroy(str);
return NULL;
}
int value_container_new(value_container_t **vcont, const char *str)
{
int ret;
*vcont = malloc(sizeof(**vcont));
if ( ! *vcont ) {
prelude_log(PRELUDE_LOG_ERR, "memory exhausted.\n");
return -1;
}
(*vcont)->data = NULL;
prelude_list_init(&(*vcont)->value_item_list);
ret = parse_value(*vcont, str);
if ( ret < 0 ) {
free(*vcont);
return ret;
}
return 0;
}
void value_container_destroy(value_container_t *vcont)
{
value_item_t *vitem;
prelude_list_t *tmp, *bkp;
prelude_list_for_each_safe(&vcont->value_item_list, tmp, bkp) {
vitem = prelude_list_entry(tmp, value_item_t, list);
if ( vitem->value && vitem->refno == -1 )
free(vitem->value);
prelude_list_del(&vitem->list);
free(vitem);
}
free(vcont);
}
void *value_container_get_data(value_container_t *vcont)
{
return vcont->data;
}
void value_container_set_data(value_container_t *vcont, void *data)
{
vcont->data = data;
}
|