OpenDNSSEC-enforcer 2.1.13
ods-migrate.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 NLNet Labs. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
27#include "config.h"
28
29#include <getopt.h>
30#include <dlfcn.h>
31#include <libxml/parser.h>
32
33#ifdef HAVE_SQLITE3
34#include <sqlite3.h>
35#endif
36#ifdef HAVE_MYSQL
37#include <mysql/mysql.h>
38#endif
39
40#include "log.h"
41#include "libhsm.h"
42#include "daemon/cfg.h"
43#include "libhsmdns.h"
44#include "db/key_data.h"
45#include "utilities.h"
46
47extern hsm_repository_t* parse_conf_repositories(const char* cfgfile);
48
50char* argv0;
51
52static void
53usage(void)
54{
55 fprintf(stderr, "%s [-h] [-v] [-c <alternate-configuration>]\n", argv0);
56}
57
58/****************************************************************************/
59
61 void (*foreach)(const char* listQueryStr, const char* updateQueryStr, int (*compute)(char**,int*,uint16_t*));
62 void (*close)(void);
64
65#ifdef HAVE_SQLITE3
66
67#define CHECKSQLITE(EX) do { dblayer_sqlite3.message = NULL; if((dblayer_sqlite3.status = (EX)) != SQLITE_OK) { fprintf(stderr, "%s: sql error: %s (%d)\n%s:%d: %s\n",argv0,(dblayer_sqlite3.message?dblayer_sqlite3.message:dblayer_sqlite3.sqlite3_errmsg(dblayer_sqlite3.handle)),dblayer_sqlite3.status,__FILE__,__LINE__,#EX); if(dblayer_sqlite3.message) dblayer_sqlite3.sqlite3_free(dblayer_sqlite3.message); } } while(0)
68
69struct dblayer_sqlite3_struct {
70 int status;
71 char* message;
72 void* library;
73 sqlite3* handle;
74 int (*sqlite3_prepare_v2)(sqlite3 *, const char *, int , sqlite3_stmt **, const char **);
75 int (*sqlite3_reset)(sqlite3_stmt *pStmt);
76 int (*sqlite3_bind_int)(sqlite3_stmt*, int, int);
77 int (*sqlite3_finalize)(sqlite3_stmt *pStmt);
78 int (*sqlite3_open)(const char *filename, sqlite3 **ppDb);
79 int (*sqlite3_exec)(sqlite3*, const char *sql, int (*callback)(void*, int, char**, char**), void *, char **errmsg);
80 int (*sqlite3_step)(sqlite3_stmt*);
81 int (*sqlite3_close)(sqlite3*);
82 const char* (*sqlite3_errmsg)(sqlite3*);
83 int (*sqlite3_free)(void*);
84};
85struct dblayer_sqlite3_struct dblayer_sqlite3;
86
87static void
88dblayer_sqlite3_initialize(void)
89{
90 void *handle;
91 char const *error;
92
93 dlerror();
94 handle = dlopen(SQLITE3_SONAME, RTLD_NOW);
95 if ((error = dlerror()) != NULL) {
96 printf("Failed to load sqlite3 library. dlerror(): %s\n", error);
97 exit(1);
98 }
99
100 dblayer_sqlite3.sqlite3_prepare_v2 = (int(*)(sqlite3*, const char*, int, sqlite3_stmt**, const char **))functioncast(dlsym(handle, "sqlite3_prepare_v2"));
101 dblayer_sqlite3.sqlite3_reset = (int(*)(sqlite3_stmt*)) functioncast(dlsym(handle, "sqlite3_reset"));
102 dblayer_sqlite3.sqlite3_bind_int = (int(*)(sqlite3_stmt*, int, int))functioncast(dlsym(handle, "sqlite3_bind_int"));
103 dblayer_sqlite3.sqlite3_finalize = (int(*)(sqlite3_stmt*))functioncast(dlsym(handle, "sqlite3_finalize"));
104 dblayer_sqlite3.sqlite3_open = (int(*)(const char*, sqlite3**)) functioncast(dlsym(handle, "sqlite3_open"));
105 dblayer_sqlite3.sqlite3_exec = (int(*)(sqlite3*, const char*, int(*)(void*, int, char**, char**), void*, char **)) functioncast(dlsym(handle, "sqlite3_exec"));
106 dblayer_sqlite3.sqlite3_step = (int(*)(sqlite3_stmt*)) functioncast(dlsym(handle, "sqlite3_step"));
107 dblayer_sqlite3.sqlite3_close = (int(*)(sqlite3*)) functioncast(dlsym(handle, "sqlite3_close"));
108 dblayer_sqlite3.sqlite3_errmsg = (const char*(*)(sqlite3*)) functioncast(dlsym(handle, "sqlite3_errmsg"));
109 dblayer_sqlite3.sqlite3_free = (int(*)(void*)) functioncast(dlsym(handle, "sqlite3_free"));
110
111 if (!dblayer_sqlite3.sqlite3_open) {
112 printf("Failed to load sqlite3 library.\n");
113 exit(1);
114 }
115}
116
117static void
118dblayer_sqlite3_close(void)
119{
120 dblayer_sqlite3.sqlite3_close(dblayer_sqlite3.handle);
121}
122
123struct callbackoperation {
124 int (*compute)(char **argv, int* id, uint16_t *keytag);
125 sqlite3_stmt* updateStmt;
126};
127
128static int
129callback(void *cargo, int argc, char **argv, char **names)
130{
131 int status;
132 int id;
133 uint16_t keytag;
134 struct callbackoperation* operation = (struct callbackoperation*) cargo;
135
136 operation->compute(argv, &id, &keytag);
137
138 CHECKSQLITE(dblayer_sqlite3.sqlite3_reset(operation->updateStmt));
139 CHECKSQLITE(dblayer_sqlite3.sqlite3_bind_int(operation->updateStmt, 1, keytag));
140 CHECKSQLITE(dblayer_sqlite3.sqlite3_bind_int(operation->updateStmt, 2, id));
141 do {
142 switch ((status = dblayer_sqlite3.sqlite3_step(operation->updateStmt))) {
143 case SQLITE_ROW:
144 break;
145 case SQLITE_DONE:
146 break;
147 case SQLITE_BUSY:
148 sleep(1);
149 break;
150 case SQLITE_ERROR:
151 case SQLITE_MISUSE:
152 default:
153 fprintf(stderr, "%s: sql error: %s\n", argv0, dblayer_sqlite3.sqlite3_errmsg(dblayer_sqlite3.handle));
154 break;
155 }
156 } while(status == SQLITE_BUSY);
157 return SQLITE_OK;
158}
159
160static void
161dblayer_sqlite3_foreach(const char* listQueryStr, const char* updateQueryStr, int (*compute)(char**,int*,uint16_t*))
162{
163 struct callbackoperation operation;
164 const char* queryEnd;
165 operation.compute = compute;
166 CHECKSQLITE(dblayer_sqlite3.sqlite3_prepare_v2(dblayer_sqlite3.handle, updateQueryStr, strlen(updateQueryStr)+1, &operation.updateStmt, &queryEnd));
167 CHECKSQLITE(dblayer_sqlite3.sqlite3_exec(dblayer_sqlite3.handle, listQueryStr, callback, &operation, &dblayer_sqlite3.message));
168 CHECKSQLITE(dblayer_sqlite3.sqlite3_finalize(operation.updateStmt));
169 dblayer_sqlite3.sqlite3_close(dblayer_sqlite3.handle);
170}
171
172static void
173dblayer_sqlite3_open(const char *datastore) {
174 CHECKSQLITE(dblayer_sqlite3.sqlite3_open(datastore, &dblayer_sqlite3.handle));
175 dblayer.close = &dblayer_sqlite3_close;
176 dblayer.foreach = &dblayer_sqlite3_foreach;
177}
178
179#endif
180
181/****************************************************************************/
182
183#ifdef HAVE_MYSQL
184
185struct dblayer_mysql_struct {
186 MYSQL* handle;
187};
188extern struct dblayer_mysql_struct dblayer_mysql;
189struct dblayer_mysql_struct dblayer_mysql;
190
191
192static void
193dblayer_mysql_initialize(void) {
194 if (mysql_library_init(0, NULL, NULL)) {
195 fprintf(stderr, "could not initialize MySQL library\n");
196 exit(1);
197 }
198}
199
200static void
201dblayer_mysql_close(void)
202{
203 if (dblayer_mysql.handle) {
204 mysql_close(dblayer_mysql.handle);
205 dblayer_mysql.handle = NULL;
206 }
207}
208
209static void
210dblayer_mysql_foreach(const char* listQueryStr, const char* updateQueryStr, int (*compute)(char**,int*,uint16_t*))
211{
212 int id;
213 uint16_t keytag;
214 MYSQL_BIND bind[2];
215 MYSQL_STMT *updateStmt;
216 MYSQL_RES* res;
217 MYSQL_ROW row;
218 updateStmt = mysql_stmt_init(dblayer_mysql.handle);
219 mysql_stmt_prepare(updateStmt, updateQueryStr, strlen(updateQueryStr) + 1);
220 mysql_query(dblayer_mysql.handle, listQueryStr);
221 res = mysql_store_result(dblayer_mysql.handle);
222 if (!res) {
223 fprintf(stderr, "Failed to update db. Is it set correctly in conf.xml?\n");
224 exit(1);
225 }
226 mysql_num_fields(res);
227 while ((row = mysql_fetch_row(res))) {
228 compute(row, &id, &keytag);
229 memset(bind, 0, sizeof (bind));
230 bind[0].buffer = &keytag;
231 bind[0].buffer_length = sizeof(keytag);
232 bind[0].buffer_type = MYSQL_TYPE_SHORT;
233 bind[0].is_unsigned = 1;
234 bind[1].buffer = &id;
235 bind[1].buffer_length = sizeof(id);
236 bind[1].buffer_type = MYSQL_TYPE_LONG;
237 mysql_stmt_bind_param(updateStmt, bind);
238 mysql_stmt_execute(updateStmt);
239 mysql_stmt_affected_rows(updateStmt);
240 }
241 mysql_free_result(res);
242 mysql_stmt_close(updateStmt);
243}
244
245static void
246dblayer_mysql_open(const char* host, const char* user, const char* pass,
247 const char *rsrc, unsigned int port, const char *unix_socket)
248{
249 dblayer_mysql.handle = mysql_init(NULL);
250 if (!mysql_real_connect(dblayer_mysql.handle, host, user, pass, rsrc, port, NULL, 0)) {
251 fprintf(stderr, "Failed to connect to database: Error: %s\n",
252 mysql_error(dblayer_mysql.handle));
253 exit(1);
254 }
255 dblayer.close = &dblayer_mysql_close;
256 dblayer.foreach = &dblayer_mysql_foreach;
257
258}
259
260#endif
261
262/****************************************************************************/
263
264static void
265dblayer_initialize(void)
266{
267#ifdef HAVE_SQLITE3
268 dblayer_sqlite3_initialize();
269#endif
270#ifdef HAVE_MYSQL
271 dblayer_mysql_initialize();
272#endif
273}
274
275static void
276dblayer_close(void) {
277 dblayer.close();
278}
279
280static void
281dblayer_finalize(void) {
282#ifdef HAVE_MYSQL
283 mysql_library_end();
284#endif
285}
286
287static void
288dblayer_foreach(const char* listQueryStr, const char* updateQueryStr, int (*compute)(char**,int*,uint16_t*))
289{
291}
292
293/****************************************************************************/
294
295const char* listQueryStr = "select keyData.id,keyData.algorithm,keyData.role,keyData.keytag,hsmKey.locator from keyData join hsmKey on keyData.hsmKeyId = hsmKey.id";
296const char* updateQueryStr = "update keyData set keytag = ? where id = ?";
297
298static int keytagcount;
299
300static int
301compute(char **argv, int* id, uint16_t* keytag)
302{
303 char *locator;
304 int algorithm;
305 int sep;
306
307 *id = atoi(argv[0]);
308 algorithm = atoi(argv[1]);
309 sep = KEY_DATA_ROLE_SEP(atoi(argv[2]));
310 *keytag = atoi(argv[3]);
311 locator = argv[4];
312 hsm_keytag(locator, algorithm, sep, keytag);
313 keytagcount += 1;
314
315 return 0;
316}
317
318int
319main(int argc, char* argv[])
320{
321 ods_status status;
323 int c;
324 int options_index = 0;
325 const char* cfgfile = ODS_SE_CFGFILE;
326 static struct option long_options[] = {
327 {"config", required_argument, 0, 'c'},
328 {"help", no_argument, 0, 'h'},
329 {"verbose", no_argument, 0, 'v'},
330 { 0, 0, 0, 0}
331 };
332
333 argv0 = argv[0];
334
335 /* parse the commandline */
336 while ((c=getopt_long(argc, argv, "c:hv", long_options, &options_index)) != -1) {
337 switch (c) {
338 case 'c':
339 cfgfile = optarg;
340 break;
341 case 'h':
342 usage();
343 exit(0);
344 case 'v':
345 ++verbosity;
346 break;
347 default:
348 usage();
349 exit(1);
350 }
351 }
352 argc -= optind;
353 argv += optind;
354 if (argc != 0) {
355 usage();
356 exit(1);
357 }
358
359 ods_log_init("ods-migrate", 0, NULL, verbosity);
360
361 xmlInitGlobals();
362 xmlInitParser();
363 xmlInitThreads();
364
365 tzset(); /* for portability */
366
367 /* Parse config file */
368 printf("Reading config file '%s'..\n", cfgfile);
369 cfg = engine_config(cfgfile, verbosity, NULL);
370 cfg->verbosity = verbosity;
371 /* does it make sense? */
372 if (engine_config_check(cfg) != ODS_STATUS_OK) {
373 fprintf(stderr,"Configuration error.\n");
374 exit(1);
375 }
376
377 printf("Connecting to HSM..\n");
378 status = hsm_open2(parse_conf_repositories(cfgfile), hsm_prompt_pin);
379 if (status != HSM_OK) {
380 char* errorstr = hsm_get_error(NULL);
381 if (errorstr != NULL) {
382 fprintf(stderr, "%s", errorstr);
383 free(errorstr);
384 } else {
385 fprintf(stderr,"error opening libhsm (errno %i)\n", status);
386 }
387 return 1;
388 }
389 dblayer_initialize();
390
391 printf("Connecting to database..\n");
392 switch (cfg->db_type) {
394#ifdef HAVE_SQLITE3
395 dblayer_sqlite3_open(cfg->datastore);
396#else
397 fprintf(stderr, "Database SQLite3 not available during compile-time.\n");
398 exit(1);
399#endif
400 break;
402#ifdef HAVE_MYSQL
403 dblayer_mysql_open(cfg->db_host, cfg->db_username, cfg->db_password, cfg->datastore, cfg->db_port, NULL);
404#else
405 fprintf(stderr, "Database MySQL not available during compile-time.\n");
406 exit(1);
407#endif
408 break;
410 default:
411 fprintf(stderr, "No database defined, possible mismatch build\n");
412 fprintf(stderr, "and configuration for SQLite3 or MySQL.\n");
413 exit(1);
414 }
415
416 keytagcount = 0;
417 printf("Computing keytags, this could take a while.\n");
418 dblayer_foreach(listQueryStr, updateQueryStr, &compute);
419 printf("Added keytags for %d keys.\n", keytagcount);
420
421 printf("Finishing..\n");
422 hsm_close();
423
425 /* dblayer_foreach for each frees something dblayer_close uses
426 * We better just let it leak. */
427 /* dblayer_close(); */
428 dblayer_finalize();
429 ods_log_close();
430
431 xmlCleanupParser();
432 xmlCleanupGlobals();
433
434 return 0;
435}
engineconfig_type * engine_config(const char *cfgfile, int cmdline_verbosity, engineconfig_type *oldcfg)
Definition: cfg.c:59
void engine_config_cleanup(engineconfig_type *config)
Definition: cfg.c:278
ods_status engine_config_check(engineconfig_type *config)
Definition: cfg.c:155
@ ENFORCER_DATABASE_TYPE_MYSQL
Definition: cfg.h:46
@ ENFORCER_DATABASE_TYPE_SQLITE
Definition: cfg.h:45
@ ENFORCER_DATABASE_TYPE_NONE
Definition: cfg.h:44
#define KEY_DATA_ROLE_SEP(role)
Definition: key_data.h:48
int main(int argc, char *argv[])
Definition: ods-migrate.c:319
int verbosity
Definition: ods-migrate.c:49
hsm_repository_t * parse_conf_repositories(const char *cfgfile)
Definition: confparser.c:205
const char * updateQueryStr
Definition: ods-migrate.c:296
char * argv0
Definition: ods-migrate.c:50
struct dblayer_struct dblayer
const char * listQueryStr
Definition: ods-migrate.c:295
void(* close)(void)
Definition: ods-migrate.c:62
void(* foreach)(const char *listQueryStr, const char *updateQueryStr, int(*compute)(char **, int *, uint16_t *))
Definition: ods-migrate.c:61
const char * datastore
Definition: cfg.h:68
const char * db_password
Definition: cfg.h:71
engineconfig_database_type_t db_type
Definition: cfg.h:80
const char * db_username
Definition: cfg.h:70
const char * db_host
Definition: cfg.h:69