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
|
/* Nessuslib -- the Nessus Library
* Copyright (C) 1998 - 2003 Renaud Deraison
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*
* Knowledge base management API
*/
#include <includes.h>
#define HASH_MAX 65537
static unsigned int mkkey(char * name )
{
char * p;
unsigned int h = 0;
if ( name == NULL )
return 0;
for ( p = name ; *p != '\0' ; p ++ )
h = (h << 3) + (unsigned char)*p;
return h % HASH_MAX;
}
struct kb_item ** kb_new()
{
return emalloc(HASH_MAX * sizeof(struct kb_item*));
}
/*
* READ the knowledge base
*/
struct kb_item * kb_item_get_single(struct kb_item ** kb, char * name, int type)
{
unsigned int h = mkkey(name);
struct kb_item * ret;
if ( kb == NULL || name == NULL ) return NULL;
ret = kb[h];
while ( ret != NULL )
{
if( (strcmp(ret->name, name) == 0) && (type == 0 || (ret->type == type)) ) return ret;
ret = ret->next;
}
return ret;
}
char * kb_item_get_str(struct kb_item ** kb, char * name)
{
struct kb_item * item = kb_item_get_single(kb, name, KB_TYPE_STR);
if(item == NULL)
return NULL;
else
return item->v.v_str;
}
int kb_item_get_int(struct kb_item ** kb, char * name)
{
struct kb_item * item = kb_item_get_single(kb, name, KB_TYPE_INT);
if(item == NULL)
return -1;
else
return item->v.v_int;
}
struct kb_item * kb_item_get_all(struct kb_item ** kb, char * name)
{
unsigned h = mkkey(name);
struct kb_item * k;
struct kb_item *ret = NULL;
if ( kb == NULL || name == NULL)
return NULL;
k = kb[h];
while ( k != NULL )
{
if( strcmp(k->name, name) == 0 )
{
struct kb_item * p;
p = emalloc(sizeof(struct kb_item));
memcpy(p, k, sizeof(struct kb_item));
p->next = ret;
ret = p;
}
k = k->next;
}
return ret;
}
struct kb_item * kb_item_get_pattern(struct kb_item ** kb, char * expr )
{
int i;
struct kb_item * k;
struct kb_item * ret = NULL;
if ( kb == NULL )
return NULL;
for ( i = 0 ; i < HASH_MAX ; i ++ )
{
k = kb[i];
while ( k != NULL )
{
if( fnmatch(expr, k->name, 0) == 0)
{
struct kb_item * p;
p = emalloc(sizeof(struct kb_item));
memcpy(p, k, sizeof(struct kb_item));
p->next = ret;
ret = p;
}
k = k->next;
}
}
return ret;
}
/* Free the result of kb_item_get_all() */
void kb_item_get_all_free(struct kb_item * items)
{
while ( items != NULL )
{
struct kb_item * next;
next = items->next;
memset(items, 0xd7, sizeof(struct kb_item));
efree(&items);
items = next;
}
}
/*
* WRITE to the knowledge base
*/
static int kb_item_addset_str(struct kb_item ** kb, char * name, char * value, int replace)
{
/*
* Before we write anything to the KB, we need to make sure that the same
* (name,value) pair is not present already
*/
int h = mkkey(name);
struct kb_item * item;
if ( kb == NULL )
return -1;
item = kb[h];
while ( item != NULL )
{
if ( strcmp(item->name, name) == 0 )
{
if(item->type == KB_TYPE_STR &&
strcmp(item->v.v_str, value) == 0)
return -1;
if ( replace != 0 )
{
if ( item->type == KB_TYPE_STR )
efree(&item->v.v_str);
item->type = KB_TYPE_STR;
item->v.v_str = estrdup(value);
return 0;
}
}
item = item->next;
}
item = emalloc(sizeof(struct kb_item));
item->name = estrdup(name);
item->v.v_str = estrdup(value);
item->type = KB_TYPE_STR;
item->next = kb[h];
kb[h] = item;
return 0;
}
int kb_item_add_str(struct kb_item ** kb, char * name, char * value)
{
return kb_item_addset_str(kb, name, value, 0);
}
int kb_item_set_str(struct kb_item ** kb, char * name, char * value)
{
return kb_item_addset_str(kb, name, value, 1);
}
/* Replace an old value in the KB by a new one */
static int kb_item_addset_int(struct kb_item ** kb, char * name, int value, int replace)
{
/*
* Before we write anything to the KB, we need to make sure that the same
* (name,value) pair is not present already
*/
int h = mkkey(name);
struct kb_item * item;
if ( kb == NULL )
return -1;
item = kb[h];
while ( item != NULL )
{
if ( strcmp(item->name, name) == 0 )
{
if(item->type == KB_TYPE_INT &&
item->v.v_int == value)
return -1;
if ( replace != 0 )
{
if ( item->type == KB_TYPE_STR )
efree(&item->v.v_str);
item->type = KB_TYPE_INT;
item->v.v_int = value;
return 0;
}
}
item = item->next;
}
item = emalloc(sizeof(struct kb_item));
item->name = estrdup(name);
item->v.v_int = value;
item->type = KB_TYPE_INT;
item->next = kb[h];
kb[h] = item;
return 0;
}
int kb_item_set_int(struct kb_item ** kb, char * name, int value)
{
return kb_item_addset_int(kb, name, value, 1);
}
int kb_item_add_int(struct kb_item ** kb, char * name, int value)
{
return kb_item_addset_int(kb, name, value, 0);
}
void kb_item_rm_all(struct kb_item ** kb, char * name)
{
int h = mkkey(name);
struct kb_item * k, * prev = NULL;
if ( kb == NULL )
return;
k = kb[h];
while ( k != NULL )
{
if(strcmp(k->name, name) == 0)
{
struct kb_item * next;
if(k->type == ARG_STRING)
efree(&k->v.v_str);
efree(&k->name);
next = k->next;
efree(&k);
if(prev != NULL) prev->next = next;
else kb[h] = next;
k = next;
}
else {
prev = k;
k = k->next;
}
}
}
/*
* Backward compatibilty
*/
struct arglist * plug_get_oldstyle_kb(struct arglist * desc )
{
struct kb_item ** kb = arg_get_value(desc, "key");
struct arglist * ret;
struct kb_item * k;
int i;
if ( kb == NULL )
return NULL;
ret = emalloc ( sizeof(struct arglist) );
for ( i = 0 ; i < HASH_MAX ; i ++ )
{
k = kb[i];
while ( k != NULL )
{
if ( k->type == KB_TYPE_INT )
arg_add_value(ret, k->name, ARG_INT, -1, (void*)k->v.v_int);
else if ( k->type == KB_TYPE_STR )
arg_add_value(ret, k->name, ARG_STRING, strlen(k->v.v_str), estrdup(k->v.v_str));
k = k->next;
}
}
return ret;
}
|