-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathkb.c
1877 lines (1654 loc) · 43.9 KB
/
kb.c
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-FileCopyrightText: 2014-2023 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/**
* @file
* @brief Knowledge base management API - Redis backend.
*/
#define _GNU_SOURCE
#include "kb.h"
#include <errno.h> /* for ENOMEM, EINVAL, EPROTO, EALREADY, ECONN... */
#include <glib.h> /* for g_log, g_free */
#include <hiredis/hiredis.h> /* for redisReply, freeReplyObject, redisCommand */
#include <stdbool.h> /* for bool, true, false */
#include <stdio.h>
#include <stdlib.h> /* for atoi */
#include <string.h> /* for strlen, strerror, strncpy, memset */
#undef G_LOG_DOMAIN
/**
* @brief GLib logging domain.
*/
#define G_LOG_DOMAIN "libgvm util"
#if GLIB_CHECK_VERSION(2, 67, 3)
#define memdup g_memdup2
#else
#define memdup g_memdup
#endif
/**
* @file kb.c
*
* @brief Contains specialized structures and functions to use redis as a KB
* server.
*/
/**
* @brief Name of the namespace usage bitmap in redis.
*/
#define GLOBAL_DBINDEX_NAME "GVM.__GlobalDBIndex"
static const struct kb_operations KBRedisOperations;
/**
* @brief Subclass of struct kb, it contains the redis-specific fields, such as
* the redis context, current DB (namespace) id and the server socket
* path.
*/
struct kb_redis
{
struct kb kb; /**< Parent KB handle. */
unsigned int max_db; /**< Max # of databases. */
unsigned int db; /**< Namespace ID number, 0 if uninitialized. */
redisContext *rctx; /**< Redis client context. */
char *path; /**< Path to the server socket. */
};
#define redis_kb(__kb) ((struct kb_redis *) (__kb))
static int
redis_delete_all (struct kb_redis *);
static int redis_lnk_reset (kb_t);
static int
redis_flush_all (kb_t, const char *);
static redisReply *
redis_cmd (struct kb_redis *kbr, const char *fmt, ...);
/**
* @brief Attempt to atomically acquire ownership of a database.
*
* @return 0 on success, negative integer otherwise.
*/
static int
try_database_index (struct kb_redis *kbr, int index)
{
redisContext *ctx = kbr->rctx;
redisReply *rep;
int rc = 0;
rep = redisCommand (ctx, "HSETNX %s %d 1", GLOBAL_DBINDEX_NAME, index);
if (rep == NULL)
return -ENOMEM;
if (rep->type != REDIS_REPLY_INTEGER)
rc = -EPROTO;
else if (rep->integer == 0)
rc = -EALREADY;
else
kbr->db = index;
freeReplyObject (rep);
return rc;
}
/**
* @brief Set the number of databases have been configured
* into kbr struct.
*
* @param[in] kbr Subclass of struct kb where to save the max db index founded.
*
* @return 0 on success, -1 on error.
*/
static int
fetch_max_db_index (struct kb_redis *kbr)
{
int rc = 0;
redisContext *ctx = kbr->rctx;
redisReply *rep = NULL;
rep = redisCommand (ctx, "CONFIG GET databases");
if (rep == NULL)
{
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"%s: redis command failed with '%s'", __func__, ctx->errstr);
rc = -1;
goto err_cleanup;
}
if (rep->type != REDIS_REPLY_ARRAY)
{
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"%s: cannot retrieve max DB number: %s", __func__, rep->str);
rc = -1;
goto err_cleanup;
}
if (rep->elements == 2)
{
kbr->max_db = (unsigned) atoi (rep->element[1]->str);
}
else
{
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"%s: unexpected reply length (%zd)", __func__, rep->elements);
rc = -1;
goto err_cleanup;
}
g_debug ("%s: maximum DB number: %u", __func__, kbr->max_db);
err_cleanup:
if (rep != NULL)
freeReplyObject (rep);
return rc;
}
/**
* @brief Select DB.
*
* WARNING: do not call redis_cmd in here, since our context is not fully
* acquired yet!
*
* @param[in] kbr Subclass of struct kb where to save the db index.
*
* @return 0 on success, -1 on error.
*/
static int
select_database (struct kb_redis *kbr)
{
int rc;
redisContext *ctx = kbr->rctx;
redisReply *rep = NULL;
if (kbr->db == 0)
{
unsigned i;
if (kbr->max_db == 0)
fetch_max_db_index (kbr);
for (i = 1; i < kbr->max_db; i++)
{
rc = try_database_index (kbr, i);
if (rc == 0)
break;
}
}
/* No DB available, give up. */
if (kbr->db == 0)
{
rc = -1;
goto err_cleanup;
}
rep = redisCommand (ctx, "SELECT %u", kbr->db);
if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
{
rc = -1;
goto err_cleanup;
}
rc = 0;
err_cleanup:
if (rep != NULL)
freeReplyObject (rep);
return rc;
}
/**
* @brief Release DB.
*
* @param[in] kbr Subclass of struct kb.
*
* @return 0 on success, -1 on error.
*/
static int
redis_release_db (struct kb_redis *kbr)
{
int rc;
redisContext *ctx = kbr->rctx;
redisReply *rep;
if (ctx == NULL)
return -EINVAL;
rep = redisCommand (ctx, "SELECT 0"); /* Management database*/
if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
{
rc = -1;
goto err_cleanup;
}
freeReplyObject (rep);
rep = redisCommand (ctx, "HDEL %s %d", GLOBAL_DBINDEX_NAME, kbr->db);
if (rep == NULL || rep->type != REDIS_REPLY_INTEGER)
{
rc = -1;
goto err_cleanup;
}
rc = 0;
err_cleanup:
if (rep != NULL)
freeReplyObject (rep);
return rc;
}
static inline char *
parse_port_of_addr (const char *addr, int tcp_indicator_len)
{
char *tmp;
int is_ip_v6;
if ((tmp = rindex (addr + tcp_indicator_len, ':')) == NULL)
return NULL;
is_ip_v6 = addr[tcp_indicator_len] == '[';
if (is_ip_v6 && (tmp - 1)[0] != ']')
return NULL;
return tmp + 1;
}
static redisContext *
connect_redis (const char *addr, int len)
{
const char *tcp_indicator = "tcp://";
const int tcp_indicator_len = strlen (tcp_indicator);
const int redis_default_port = 6379;
int port, host_len;
char *tmp, *host;
redisContext *result;
static int warn_flag = 0;
if (len < tcp_indicator_len + 1)
goto unix_connect;
if (memcmp (addr, tcp_indicator, tcp_indicator_len) != 0)
goto unix_connect;
host_len = len - tcp_indicator_len;
if ((tmp = parse_port_of_addr (addr, tcp_indicator_len)) == NULL)
port = redis_default_port;
else
{
port = atoi (tmp);
host_len -= strlen (tmp) + 1;
}
host = calloc (1, host_len);
memmove (host, addr + tcp_indicator_len, host_len);
result = redisConnect (host, port);
if (warn_flag == 0)
{
g_warning ("A Redis TCP connection is being used. This feature is "
"experimental and insecure, since it is not an encrypted "
"channel. We discourage its usage in production environments");
warn_flag = 1;
}
free (host);
return result;
unix_connect:
return redisConnectUnix (addr);
}
/**
* @brief Get redis context if it is already connected or do a
* a connection.
*
* @param[in] kbr Subclass of struct kb where to fetch the context.
* or where it is saved in case of a new connection.
*
* @return 0 on success, -1 on connection error, -2 on unavailable DB slot.
*/
static int
get_redis_ctx (struct kb_redis *kbr)
{
int rc;
if (kbr->rctx != NULL)
return 0;
kbr->rctx = connect_redis (kbr->path, strlen (kbr->path));
if (kbr->rctx == NULL || kbr->rctx->err)
{
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"%s: redis connection error to %s: %s", __func__, kbr->path,
kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM));
redisFree (kbr->rctx);
kbr->rctx = NULL;
return -1;
}
rc = select_database (kbr);
if (rc)
{
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No redis DB available");
redisFree (kbr->rctx);
kbr->rctx = NULL;
return -2;
}
g_debug ("%s: connected to redis://%s/%d", __func__, kbr->path, kbr->db);
return 0;
}
/**
* @brief Test redis connection.
*
* @param[in] kbr Subclass of struct kb to test.
*
* @return 0 on success, negative integer on error.
*/
static int
redis_test_connection (struct kb_redis *kbr)
{
int rc = 0;
redisReply *rep;
rep = redis_cmd (kbr, "PING");
if (rep == NULL)
{
/* not 100% relevant but hiredis doesn't provide us with proper error
* codes. */
rc = -ECONNREFUSED;
goto out;
}
if (rep->type != REDIS_REPLY_STATUS)
{
rc = -EINVAL;
goto out;
}
if (g_ascii_strcasecmp (rep->str, "PONG"))
{
rc = -EPROTO;
goto out;
}
out:
if (rep != NULL)
freeReplyObject (rep);
return rc;
}
/**
* @brief Delete all entries and release ownership on the namespace.
*
* @param[in] kb KB handle to release.
*
* @return 0 on success, non-null on error.
*/
static int
redis_delete (kb_t kb)
{
struct kb_redis *kbr;
kbr = redis_kb (kb);
redis_delete_all (kbr);
redis_release_db (kbr);
if (kbr->rctx != NULL)
{
g_free (kbr->path);
redisFree (kbr->rctx);
kbr->rctx = NULL;
}
g_free (kb);
return 0;
}
/**
* @brief Return the kb index
*
* @param[in] kb KB handle.
*
* @return kb_index on success, null on error.
*/
static int
redis_get_kb_index (kb_t kb)
{
int i;
i = ((struct kb_redis *) kb)->db;
if (i > 0)
return i;
return -1;
}
/**
* @brief Attempt to purge dirty pages.
*
* Attempt to purge dirty pages so these can be reclaimed by the allocator.
* This command only works when using jemalloc as an allocator, and evaluates
* to a benign NOOP for all others. Command is applied to complete redis
* instance and not only single db.
*
* @param[in] kb KB handle where to run the command.
*
* @return 0 on success, non-null on error.
*/
static int
redis_memory_purge (kb_t kb)
{
redisReply *rep;
int rc = 0;
rep = redis_cmd (redis_kb (kb), "MEMORY PURGE");
if (!rep || rep->type == REDIS_REPLY_ERROR)
rc = -1;
if (rep)
freeReplyObject (rep);
return rc;
}
/**
* @brief Initialize a new Knowledge Base object.
*
* @param[in] kb Reference to a kb_t to initialize.
* @param[in] kb_path Path to KB.
*
* @return 0 on success, -1 on connection error, -2 when no DB is available, -3
* when given kb_path was NULL.
*/
static int
redis_new (kb_t *kb, const char *kb_path)
{
struct kb_redis *kbr;
int rc = 0;
if (kb_path == NULL)
return -3;
kbr = g_malloc0 (sizeof (struct kb_redis));
kbr->kb.kb_ops = &KBRedisOperations;
kbr->path = g_strdup (kb_path);
if ((rc = get_redis_ctx (kbr)) < 0)
{
redis_delete ((kb_t) kbr);
return rc;
}
if (redis_test_connection (kbr))
{
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"%s: cannot access redis at '%s'", __func__, kb_path);
redis_delete ((kb_t) kbr);
kbr = NULL;
rc = -1;
}
/* Ensure that the new kb is clean */
redis_delete_all (kbr);
*kb = (kb_t) kbr;
/* Try to make unused memory available for the OS again. */
if (redis_memory_purge (*kb))
g_warning ("%s: Memory purge was not successful", __func__);
return rc;
}
/**
* @brief Connect to a Knowledge Base object with the given kb_index.
*
* @param[in] kb_path Path to KB.
* @param[in] kb_index DB index
*
* @return Knowledge Base object, NULL otherwise.
*/
static kb_t
redis_direct_conn (const char *kb_path, const int kb_index)
{
struct kb_redis *kbr;
redisReply *rep;
if (kb_path == NULL)
return NULL;
kbr = g_malloc0 (sizeof (struct kb_redis));
kbr->kb.kb_ops = &KBRedisOperations;
kbr->path = g_strdup (kb_path);
kbr->rctx = connect_redis (kbr->path, strlen (kbr->path));
if (kbr->rctx == NULL || kbr->rctx->err)
{
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"%s: redis connection error to %s: %s", __func__, kbr->path,
kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM));
redisFree (kbr->rctx);
g_free (kbr->path);
g_free (kbr);
return NULL;
}
kbr->db = kb_index;
rep = redisCommand (kbr->rctx, "SELECT %d", kb_index);
if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
{
if (rep != NULL)
freeReplyObject (rep);
redisFree (kbr->rctx);
kbr->rctx = NULL;
g_free (kbr->path);
g_free (kbr);
return NULL;
}
freeReplyObject (rep);
return (kb_t) kbr;
}
/**
* @brief Find an existing Knowledge Base object with key.
*
* @param[in] kb_path Path to KB.
* @param[in] key Marker key to search for in KB objects.
*
* @return Knowledge Base object, NULL otherwise.
*/
static kb_t
redis_find (const char *kb_path, const char *key)
{
struct kb_redis *kbr;
unsigned int i = 1;
if (kb_path == NULL)
return NULL;
kbr = g_malloc0 (sizeof (struct kb_redis));
kbr->kb.kb_ops = &KBRedisOperations;
kbr->path = g_strdup (kb_path);
do
{
redisReply *rep;
kbr->rctx = connect_redis (kbr->path, strlen (kbr->path));
if (kbr->rctx == NULL || kbr->rctx->err)
{
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"%s: redis connection error to %s: %s", __func__, kbr->path,
kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM));
redisFree (kbr->rctx);
g_free (kbr->path);
g_free (kbr);
return NULL;
}
if (kbr->max_db == 0)
fetch_max_db_index (kbr);
kbr->db = i;
rep = redisCommand (kbr->rctx, "HEXISTS %s %d", GLOBAL_DBINDEX_NAME, i);
if (rep == NULL || rep->type != REDIS_REPLY_INTEGER || rep->integer != 1)
{
if (rep != NULL)
freeReplyObject (rep);
i++;
redisFree (kbr->rctx);
kbr->rctx = NULL;
continue;
}
freeReplyObject (rep);
rep = redisCommand (kbr->rctx, "SELECT %u", i);
if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
{
redisFree (kbr->rctx);
kbr->rctx = NULL;
}
else
{
freeReplyObject (rep);
if (key)
{
char *tmp = kb_item_get_str (&kbr->kb, key);
if (tmp)
{
g_free (tmp);
return (kb_t) kbr;
}
}
redisFree (kbr->rctx);
}
i++;
}
while (i < kbr->max_db);
g_free (kbr->path);
g_free (kbr);
return NULL;
}
/**
* @brief Release a KB item (or a list).
*
* @param[in] item Item or list to be release
*/
void
kb_item_free (struct kb_item *item)
{
while (item != NULL)
{
struct kb_item *next;
next = item->next;
if (item->type == KB_TYPE_STR && item->v_str != NULL)
g_free (item->v_str);
g_free (item);
item = next;
}
}
/**
* @brief Give a single KB item.
*
* @param[in] name Name of the item.
* @param[in] elt A redisReply element where to fetch the item.
* @param[in] force_int To force string to integer conversion.
*
* @return Single retrieve kb_item on success, NULL otherwise.
*/
static struct kb_item *
redis2kbitem_single (const char *name, const redisReply *elt, int force_int)
{
struct kb_item *item;
size_t namelen;
if (elt->type != REDIS_REPLY_STRING && elt->type != REDIS_REPLY_INTEGER)
return NULL;
namelen = strlen (name) + 1;
item = g_malloc0 (sizeof (struct kb_item) + namelen);
if (elt->type == REDIS_REPLY_INTEGER)
{
item->type = KB_TYPE_INT;
item->v_int = elt->integer;
}
else if (force_int)
{
item->type = KB_TYPE_INT;
item->v_int = atoi (elt->str);
}
else
{
item->type = KB_TYPE_STR;
item->v_str = memdup (elt->str, elt->len + 1);
item->len = elt->len;
}
item->next = NULL;
item->namelen = namelen;
memset (item->name, 0, namelen);
memcpy (item->name, name, namelen);
return item;
}
/**
* @brief Fetch a KB item or list from a redis Reply.
*
* @param[in] name Name of the item.
* @param[in] rep A redisReply element where to fetch the item.
*
* @return kb_item or list on success, NULL otherwise.
*/
static struct kb_item *
redis2kbitem (const char *name, const redisReply *rep)
{
struct kb_item *kbi;
kbi = NULL;
switch (rep->type)
{
unsigned int i;
case REDIS_REPLY_STRING:
case REDIS_REPLY_INTEGER:
kbi = redis2kbitem_single (name, rep, 0);
break;
case REDIS_REPLY_ARRAY:
for (i = 0; i < rep->elements; i++)
{
struct kb_item *tmpitem;
tmpitem = redis2kbitem_single (name, rep->element[i], 0);
if (tmpitem == NULL)
break;
if (kbi != NULL)
{
tmpitem->next = kbi;
kbi = tmpitem;
}
else
kbi = tmpitem;
}
break;
case REDIS_REPLY_NIL:
case REDIS_REPLY_STATUS:
case REDIS_REPLY_ERROR:
default:
break;
}
return kbi;
}
/**
* @brief Execute a redis command and get a redis reply.
*
* @param[in] kbr Subclass of struct kb to connect to.
* @param[in] fmt Formatted variable argument list with the cmd to be executed.
*
* @return Redis reply on success, NULL otherwise.
*/
static redisReply *
redis_cmd (struct kb_redis *kbr, const char *fmt, ...)
{
redisReply *rep;
va_list ap, aq;
int retry = 0;
va_start (ap, fmt);
do
{
if (get_redis_ctx (kbr) < 0)
{
va_end (ap);
return NULL;
}
va_copy (aq, ap);
rep = redisvCommand (kbr->rctx, fmt, aq);
va_end (aq);
if (kbr->rctx->err)
{
if (rep != NULL)
freeReplyObject (rep);
redis_lnk_reset ((kb_t) kbr);
retry = !retry;
}
else
retry = 0;
}
while (retry);
va_end (ap);
return rep;
}
/**
* @brief Get a single KB element.
*
* @param[in] kb KB handle where to fetch the item.
* @param[in] name Name of the element to retrieve.
* @param[in] type Desired element type.
*
* @return A struct kb_item to be freed with kb_item_free() or NULL if no
* element was found or on error.
*/
static struct kb_item *
redis_get_single (kb_t kb, const char *name, enum kb_item_type type)
{
struct kb_item *kbi;
struct kb_redis *kbr;
redisReply *rep;
kbr = redis_kb (kb);
kbi = NULL;
rep = redis_cmd (kbr, "LINDEX %s -1", name);
if (rep == NULL || rep->type != REDIS_REPLY_STRING)
{
kbi = NULL;
goto out;
}
kbi = redis2kbitem_single (name, rep, type == KB_TYPE_INT);
out:
if (rep != NULL)
freeReplyObject (rep);
return kbi;
}
/**
* @brief Get a single KB string item.
*
* @param[in] kb KB handle where to fetch the item.
* @param[in] name Name of the element to retrieve.
*
* @return A struct kb_item to be freed with kb_item_free() or NULL if no
* element was found or on error.
*/
static char *
redis_get_str (kb_t kb, const char *name)
{
struct kb_item *kbi;
kbi = redis_get_single (kb, name, KB_TYPE_STR);
if (kbi != NULL)
{
char *res;
res = kbi->v_str;
kbi->v_str = NULL;
kb_item_free (kbi);
return res;
}
return NULL;
}
/**
* @brief Push a new entry under a given key.
*
* @param[in] kb KB handle where to store the item.
* @param[in] name Key to push to.
* @param[in] value Value to push.
*
* @return 0 on success, non-null on error.
*/
static int
redis_push_str (kb_t kb, const char *name, const char *value)
{
struct kb_redis *kbr;
redisReply *rep = NULL;
int rc = 0;
if (!value)
return -1;
kbr = redis_kb (kb);
rep = redis_cmd (kbr, "LPUSH %s %s", name, value);
if (!rep || rep->type == REDIS_REPLY_ERROR)
rc = -1;
if (rep)
freeReplyObject (rep);
return rc;
}
/**
* @brief Pops a single KB string item.
*
* @param[in] kb KB handle where to fetch the item.
* @param[in] name Name of the key from where to retrieve.
*
* @return A string to be freed or NULL if list is empty or on error.
*/
static char *
redis_pop_str (kb_t kb, const char *name)
{
struct kb_redis *kbr;
redisReply *rep;
char *value = NULL;
kbr = redis_kb (kb);
rep = redis_cmd (kbr, "RPOP %s", name);
if (!rep)
return NULL;
if (rep->type == REDIS_REPLY_STRING)
value = g_strdup (rep->str);
freeReplyObject (rep);
return value;
}
/**
* @brief Get a single KB integer item.
*
* @param[in] kb KB handle where to fetch the item.
* @param[in] name Name of the element to retrieve.
*
* @return An integer.
*/
static int
redis_get_int (kb_t kb, const char *name)
{
struct kb_item *kbi;
kbi = redis_get_single (kb, name, KB_TYPE_INT);
if (kbi != NULL)
{
int res;
res = kbi->v_int;
kb_item_free (kbi);
return res;
}
return -1;
}
/**
* @brief Get field of a NVT.
*
* @param[in] kb KB handle where to store the nvt.
* @param[in] oid OID of NVT to get from.
* @param[in] position Position of field to get.
*
* @return Value of field, NULL otherwise.
*/
static char *
redis_get_nvt (kb_t kb, const char *oid, enum kb_nvt_pos position)
{
struct kb_redis *kbr;
redisReply *rep;
char *res = NULL;
kbr = redis_kb (kb);
if (position >= NVT_TIMESTAMP_POS)
rep = redis_cmd (kbr, "LINDEX filename:%s %d", oid,
position - NVT_TIMESTAMP_POS);
else
rep = redis_cmd (kbr, "LINDEX nvt:%s %d", oid, position);
if (!rep)
return NULL;
if (rep->type == REDIS_REPLY_INTEGER)
res = g_strdup_printf ("%lld", rep->integer);
else if (rep->type == REDIS_REPLY_STRING)
res = g_strdup (rep->str);
freeReplyObject (rep);
return res;
}
/**
* @brief Get a full NVT.
*
* @param[in] kb KB handle where to store the nvt.
* @param[in] oid OID of NVT to get.
*
* @return nvti_t of NVT, NULL otherwise.
*/
static nvti_t *
redis_get_nvt_all (kb_t kb, const char *oid)
{
struct kb_redis *kbr;
redisReply *rep;
kbr = redis_kb (kb);
rep =
redis_cmd (kbr, "LRANGE nvt:%s %d %d", oid, NVT_FILENAME_POS, NVT_NAME_POS);
if (!rep)
return NULL;
if (rep->type != REDIS_REPLY_ARRAY || rep->elements != NVT_NAME_POS + 1)
{
freeReplyObject (rep);
return NULL;
}
else