Skip to content

Commit 4c658d0

Browse files
4.0.8 (#31)
* Add support to remove records from encrypted kbs * solve bug collating mz tables due to consistency error. * Extend 'dump keys' command to mz tables. * Solve minor bug with version checking
1 parent ea5f911 commit 4c658d0

16 files changed

Lines changed: 211 additions & 83 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ unlink list from DBNAME/TABLENAME key KEY
105105
106106
dump DBNAME/TABLENAME hex N
107107
Dumps table contents with first N bytes in hex
108+
109+
dump keys from DBNAME/TABLENAME [sector N]
110+
Dumps a unique list of existing keys
108111
```
109112
## Other Uses
110113

src/collate.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "mz.h"
2424
#include "collate.h"
2525
#include "logger.h"
26+
#include "decode.h"
2627
/**
2728
* @file collate.c
2829
* @date 19 Aug 2020
@@ -524,11 +525,25 @@ bool key_in_delete_list(struct ldb_collate_data *collate, uint8_t *key, uint8_t
524525

525526
if (result)
526527
{
527-
char * aux = strndup((char*)data + (collate->del_tuples->keys_number - 1) * collate->del_tuples->key_ln,
528-
size - (collate->del_tuples->keys_number - 1) * collate->del_tuples->key_ln);
529-
530-
result = data_compare(collate->del_tuples->tuples[i]->data + char_to_skip, aux);
531-
free(aux);
528+
if (collate->in_table.definitions & LDB_TABLE_DEFINITION_ENCRYPTED)
529+
{
530+
unsigned char tuple_bin[MAX_CSV_LINE_LEN];
531+
if(!decode && !ldb_decoder_lib_load())
532+
return false;
533+
534+
int r_size = decode(DECODE_BASE64, NULL, NULL, collate->del_tuples->tuples[i]->data + char_to_skip, strlen(collate->del_tuples->tuples[i]->data) - char_to_skip, tuple_bin);
535+
if (r_size > 0)
536+
result = !memcmp(tuple_bin, data + (collate->del_tuples->keys_number - 1) * collate->del_tuples->key_ln, r_size);
537+
else
538+
result = false;
539+
}
540+
else
541+
{
542+
char * aux = strndup((char*)data + (collate->del_tuples->keys_number - 1) * collate->del_tuples->key_ln,
543+
size - (collate->del_tuples->keys_number - 1) * collate->del_tuples->key_ln);
544+
result = data_compare(collate->del_tuples->tuples[i]->data + char_to_skip, aux);
545+
free(aux);
546+
}
532547
}
533548
}
534549
/* Increment del_count and return true */

src/command.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ char *ldb_commands[] =
7272
"dump {ascii} hex {ascii} sector {hex}",
7373
"dump {ascii} hex {ascii}",
7474
"dump keys from {ascii}",
75+
"dump keys from {ascii} sector {hex}",
7576
"cat {hex} from {ascii}"
7677
};
7778
int ldb_commands_count = sizeof(ldb_commands) / sizeof(ldb_commands[0]);
@@ -914,12 +915,29 @@ void ldb_command_dump_keys(char *command)
914915
{
915916
/* Extract values from command */
916917
char *dbtable = ldb_extract_word(4, command);
918+
char * sector = ldb_extract_word(5, command);
919+
int sectorn = -1;
920+
if (*sector)
921+
{
922+
char *sector_n = ldb_extract_word(6, command);
923+
sectorn = (int) strtol(sector_n, NULL, 16);
924+
free(sector_n);
925+
}
926+
free(sector);
917927

918928
if (ldb_valid_table(dbtable))
919929
{
920930
/* Assembly ldb table structure */
921931
struct ldb_table ldbtable = ldb_read_cfg(dbtable);
922-
ldb_dump_keys(ldbtable);
932+
if ((ldbtable.definitions > 0 && ldbtable.definitions & LDB_TABLE_DEFINITION_MZ) ||
933+
(!strcmp(ldbtable.table, "sources") || !strcmp(ldbtable.table, "notices")))
934+
{
935+
mz_list_keys(ldbtable, sectorn);
936+
}
937+
else
938+
{
939+
ldb_dump_keys(ldbtable, sectorn);
940+
}
923941
}
924942

925943
/* Free memory */

src/decode.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "decode.h"
2+
#include "logger.h"
3+
void * lib_handle = NULL;
4+
5+
int (*decode) (int op, unsigned char *key, unsigned char *nonce,
6+
const char *buffer_in, int buffer_in_len, unsigned char *buffer_out) = NULL;
7+
8+
bool ldb_decoder_lib_load(void)
9+
{
10+
if (lib_handle != NULL)
11+
return true;
12+
/*set decode funtion pointer to NULL*/
13+
decode = NULL;
14+
lib_handle = dlopen("libscanoss_encoder.so", RTLD_NOW);
15+
char * err;
16+
if (lib_handle)
17+
{
18+
log_info("Lib scanoss_encoder present\n");
19+
decode = dlsym(lib_handle, "scanoss_decode");
20+
if ((err = dlerror()))
21+
{
22+
log_info("%s\n", err);
23+
return false;
24+
}
25+
return true;
26+
}
27+
28+
return false;
29+
}
30+
31+
void ldb_decoder_lib_close(void)
32+
{
33+
dlclose(lib_handle);
34+
}

src/decode.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef __DECODE_H
2+
#define __DECODE_H
3+
#include <stdio.h>
4+
#include <dlfcn.h>
5+
#include <stdint.h>
6+
#include <stdbool.h>
7+
#define DECODE_BASE64 8
8+
extern int (*decode) (int op, unsigned char *key, unsigned char *nonce,
9+
const char *buffer_in, int buffer_in_len, unsigned char *buffer_out);
10+
extern void * lib_handle;
11+
bool ldb_decoder_lib_load(void);
12+
void ldb_decoder_lib_close(void);
13+
#endif

src/import.c

Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include <libgen.h>
3232
#include <dirent.h>
3333
#include <string.h>
34-
#include <dlfcn.h>
3534
#include <time.h>
3635
#include "import.h"
3736
#include "./ldb.h"
@@ -44,48 +43,16 @@
4443
#include "collate.h"
4544
#include "logger.h"
4645
#include "ldb_error.h"
47-
48-
#define DECODE_BASE64 8
49-
#define MAX_CSV_LINE_LEN 1024
46+
#include "decode.h"
5047
#define REC_SIZE_LEN 2
5148

5249
pthread_mutex_t lock;
5350

5451
int max_threads = 0;
5552
pthread_t * threads_list;
5653
static long IGNORED_WFP_LN = sizeof(IGNORED_WFP);
57-
58-
int (*decode) (int op, unsigned char *key, unsigned char *nonce,
59-
const char *buffer_in, int buffer_in_len, unsigned char *buffer_out) = NULL;
60-
6154
double progress_timer = 0;
6255

63-
64-
void * lib_handle = NULL;
65-
bool ldb_import_decoder_lib_load(void)
66-
{
67-
if (lib_handle != NULL)
68-
return true;
69-
/*set decode funtion pointer to NULL*/
70-
decode = NULL;
71-
lib_handle = dlopen("libscanoss_encoder.so", RTLD_NOW);
72-
char * err;
73-
if (lib_handle)
74-
{
75-
log_info("Lib scanoss_encoder present\n");
76-
decode = dlsym(lib_handle, "scanoss_decode");
77-
if ((err = dlerror()))
78-
{
79-
log_info("%s\n", err);
80-
return false;
81-
}
82-
return true;
83-
}
84-
85-
return false;
86-
}
87-
88-
8956
/**
9057
* @brief Sort a csv file invokinkg a new process and executing a sort command
9158
*
@@ -521,6 +488,20 @@ int ldb_import_mz(ldb_importation_config_t * job)
521488
if (!ldb_table_exists(job->dbname, job->table))
522489
ldb_create_table_new(job->dbname, job->table, 16, 0, job->opt.params.keys_number, LDB_TABLE_DEFINITION_MZ |
523490
(job->opt.params.binary_mode ? LDB_TABLE_DEFINITION_ENCRYPTED : LDB_TABLE_DEFINITION_STANDARD));
491+
else
492+
{
493+
/* Create table structure for bulk import (32-bit key) */
494+
char db_table[LDB_MAX_NAME];
495+
snprintf(db_table,LDB_MAX_NAME-1, "%s/%s", job->dbname, job->table);
496+
struct ldb_table oss_bulk = ldb_read_cfg(db_table);
497+
if (oss_bulk.keys < 1 || oss_bulk.definitions == LDB_TABLE_DEFINITION_UNDEFINED)
498+
{
499+
oss_bulk.keys = job->opt.params.keys_number;
500+
ldb_write_cfg(oss_bulk.db, oss_bulk.table, oss_bulk.key_ln, oss_bulk.rec_ln, oss_bulk.keys,
501+
job->opt.params.binary_mode ? LDB_TABLE_DEFINITION_MZ | LDB_TABLE_DEFINITION_ENCRYPTED : LDB_TABLE_DEFINITION_MZ | LDB_TABLE_DEFINITION_STANDARD);
502+
log_info("Table %s config file was updated\n", oss_bulk.table);
503+
}
504+
}
524505

525506
return (ldb_bin_join(job->csv_path, dest_path, job->opt.params.overwrite, false, job->opt.params.delete_after_import));
526507
}
@@ -1491,7 +1472,7 @@ int ldb_import(ldb_importation_config_t * job)
14911472
{
14921473
pthread_mutex_lock(&lock);
14931474
if(!decode)
1494-
ldb_import_decoder_lib_load();
1475+
ldb_decoder_lib_load();
14951476
pthread_mutex_unlock(&lock);
14961477
}
14971478

@@ -1820,7 +1801,7 @@ bool ldb_import_command(char * dbtable, char * path, char * config)
18201801
import_params_t user_opt = {.params = LDB_IMPORTATION_CONFIG_UNDEFINED};
18211802
import_params_t global_opt = {.params = LDB_IMPORTATION_CONFIG_UNDEFINED};
18221803
ldb_importation_config_t job = {.opt.params = LDB_IMPORTATION_CONFIG_DEFAULT, .csv_path = "\0", .path = "\0"};
1823-
1804+
18241805
char *table = strrchr(dbtable, '/');
18251806
if (table)
18261807
strncpy(job.dbname, dbtable, table - dbtable);
@@ -1863,18 +1844,19 @@ bool ldb_import_command(char * dbtable, char * path, char * config)
18631844
strcpy(job.path, path);
18641845
//check if the version file is present and update the kb version.
18651846
bool version_present = version_import(&job);
1847+
1848+
recurse_directory(&jobs, path, NULL);
1849+
opt_add(jobs.user_opt, jobs.global_opt);
1850+
opt_add(jobs.global_opt, jobs.user_opt);
1851+
18661852
//abort the job if VERSION_VALIDATION is active and the json file is not present
18671853
if (job.opt.params.version_validation && !version_present)
18681854
{
1869-
logger_basic("Failed to validate version.json, check if it is present in %s and it has the correct format\n", job.path);
1870-
logger_basic("You can avoid this error by setting \"VALIDATE_VERSION = 0\" in the import configuration options\n", job.path);
1871-
1855+
fprintf(stderr, "Failed to validate version.json, check if it is present in %s and it has the correct format\n", job.path);
1856+
fprintf(stderr, "You can avoid this error by setting \"VALIDATE_VERSION = 0\" in the import configuration options\n");
18721857
exit(EXIT_FAILURE);
18731858
}
18741859

1875-
recurse_directory(&jobs, path, NULL);
1876-
opt_add(jobs.user_opt, jobs.global_opt);
1877-
opt_add(jobs.global_opt, jobs.user_opt);
18781860
print_jobs(&jobs);
18791861
max_threads = jobs.user_opt->params.threads;
18801862
fprintf(stderr, "Max threads set to: %d\n", max_threads);
@@ -1938,18 +1920,19 @@ bool ldb_import_command(char * dbtable, char * path, char * config)
19381920
strcpy(job.path,path);
19391921
}
19401922

1923+
load_import_config(&job, &global_opt);
1924+
opt_add(&user_opt, &global_opt);
1925+
opt_add(&user_opt, &job.opt);
1926+
19411927
bool version_present = version_import(&job);
19421928
//abort the job if VERSION_VALIDATION is active and the json file is not present
19431929
if (job.opt.params.version_validation && !version_present)
19441930
{
1945-
logger_basic("Failed to validate version.json, check if it is present in %s and it has the correct format\n", job.path);
1931+
fprintf(stderr, "Failed to validate version.json, check if it is present in %s and it has the correct format\n", job.path);
1932+
fprintf(stderr, "You can avoid this error by setting \"VALIDATE_VERSION = 0\" in the import configuration options\n");
19461933
exit(EXIT_FAILURE);
19471934
}
19481935

1949-
load_import_config(&job, &global_opt);
1950-
opt_add(&user_opt, &global_opt);
1951-
opt_add(&user_opt, &job.opt);
1952-
19531936
max_threads = job.opt.params.threads;
19541937
fprintf(stderr, "Max threads set to: %d\n", max_threads);
19551938
pthread_mutex_init(&lock, NULL);

src/import.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define __IMPORT_H
33
#include <stdbool.h>
44
#include "ldb.h"
5+
56
#define IMPORT_PARAMS_NUMBER 15
67
typedef union import_params {
78
struct __attribute__((__packed__)) params

src/keys.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,9 @@ bool ldb_dump_keys_handler(uint8_t *key, uint8_t *subkey, int subkey_ln, uint8_t
5656
/* Save into last key */
5757
memcpy(table->last_key, table->current_key, table->key_ln);
5858

59-
/* Output key to stdout */
60-
for (int i = 0; i < table->key_ln; i++)
61-
{
62-
fwrite(table->current_key + i, 1, 1, stdout);
63-
}
64-
59+
char hex[MD5_LEN * 2 + 1];
60+
ldb_bin_to_hex(table->current_key, MD5_LEN, hex);
61+
printf("%s\n", hex);
6562
return false;
6663
}
6764

@@ -70,10 +67,10 @@ bool ldb_dump_keys_handler(uint8_t *key, uint8_t *subkey, int subkey_ln, uint8_t
7067
*
7168
* @param table input table
7269
*/
73-
void ldb_dump_keys(struct ldb_table table)
70+
void ldb_dump_keys(struct ldb_table table, int s)
7471
{
7572
/* Read each DB sector */
76-
uint8_t k0 = 0;
73+
uint8_t k0 = s >= 0 ? s : 0;
7774
setlocale(LC_NUMERIC, "");
7875

7976
table.current_key = calloc(table.key_ln, 1);
@@ -93,14 +90,14 @@ void ldb_dump_keys(struct ldb_table table)
9390
k[1] = k1;
9491
k[2] = k2;
9592
k[3] = k3;
96-
/* If there is a pointer, read the list */
97-
if (ldb_map_pointer_pos(k))
98-
{
99-
/* Process records */
100-
ldb_fetch_recordset(sector, table, k, true, ldb_dump_keys_handler, &table);
101-
}
93+
94+
/* Process records */
95+
ldb_fetch_recordset(sector, table, k, true, ldb_dump_keys_handler, &table);
96+
10297
}
10398
free(sector);
99+
if (s >=0)
100+
break;
104101
}
105102
} while (k0++ < 255);
106103

src/ldb.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "./ldb/types.h"
2828
#include "./ldb/mz.h"
2929

30-
#define LDB_VERSION "4.0.7"
30+
#define LDB_VERSION "4.0.8"
3131

3232
#define LDB_TABLE_DEFINITION_UNDEFINED -1
3333
#define LDB_TABLE_DEFINITION_STANDARD 0
@@ -90,7 +90,7 @@ bool ldb_hexprint_width(uint8_t *key, uint8_t *subkey, int subkey_ln, uint8_t *d
9090
void ldb_sector_update(struct ldb_table table, uint8_t *key);
9191
void ldb_sector_erase(struct ldb_table table, uint8_t *key);
9292
void ldb_dump(struct ldb_table table, int hex_bytes, int sector);
93-
void ldb_dump_keys(struct ldb_table table);
93+
void ldb_dump_keys(struct ldb_table table, int s);
9494

9595
char * ldb_file_extension(char * path);
9696
bool ldb_create_dir(char *path);

src/ldb/definitions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define MD5_LEN 16
1717
#define MD5_LEN_HEX 32
1818
#define BUFFER_SIZE 1048576
19+
#define MAX_CSV_LINE_LEN 1024
1920

2021
extern char ldb_root[];
2122
extern char ldb_lock_path[];

0 commit comments

Comments
 (0)