43#if defined(ENFORCER_DATABASE_SQLITE3)
50static sqlite3* db = NULL;
51#elif defined(ENFORCER_DATABASE_MYSQL)
52#include <mysql/mysql.h>
58static MYSQL* db = NULL;
61#define AUTHOR_NAME "Jerry Lundström"
62#define COPYRIGHT_STR "Copyright (c) 2014 .SE (The Internet Infrastructure Foundation) OpenDNSSEC"
64static void usage(FILE* out) {
66 "\nBSD licensed, see LICENSE in source package for details.\n"
67 "Version %s. Report bugs to <%s>.\n",
70 fprintf(out,
"--help, -h: Print usage.\n");
71 fprintf(out,
"--version, -V: Print version.\n");
72 fprintf(out,
"--force, -f: Yes to all questions.\n");
75static void version(FILE* out) {
77 "Database setup tool for %s version %s\n"
79 "%s. This is free software.\n"
80 "See source files for more license information\n",
89#if defined(ENFORCER_DATABASE_SQLITE3)
97 if (sqlite3_initialize() != SQLITE_OK) {
102 SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_CREATE,
109#elif defined(ENFORCER_DATABASE_MYSQL)
117 if (mysql_library_init(0, NULL, NULL)) {
121 if (!(db = mysql_init(NULL))
122 || !mysql_real_connect(db,
144static int disconnect_db() {
145#if defined(ENFORCER_DATABASE_SQLITE3)
154#elif defined(ENFORCER_DATABASE_MYSQL)
168static int db_do(
const char *sql,
size_t size) {
169#if defined(ENFORCER_DATABASE_SQLITE3)
170 sqlite3_stmt* stmt = NULL;
179 if (sqlite3_prepare_v2(db, sql, size, &stmt, NULL) != SQLITE_OK
180 || sqlite3_step(stmt) != SQLITE_DONE)
183 sqlite3_finalize(stmt);
187 sqlite3_finalize(stmt);
190#elif defined(ENFORCER_DATABASE_MYSQL)
198 if (mysql_real_query(db, sql, size)) {
208static int db_do2(
const char** strs) {
213 for (i = 0; strs[i]; i++) {
217 for (; strs[i]; i++) {
218 if ((ret = snprintf(sqlp, left,
"%s", strs[i])) >= left) {
225 if (db_do(sql,
sizeof(sql) - left)) {
233int main(
int argc,
char* argv[]) {
234 int c, options_index = 0;
235 static struct option long_options[] = {
236 {
"help", no_argument, 0,
'h'},
237 {
"version", no_argument, 0,
'V'},
238 {
"force", no_argument, 0,
'f'},
244 const char* cfgfile = ODS_SE_CFGFILE;
246 ods_log_init(
"ods-enforcerd", 0, NULL, 0);
248 while ((c=getopt_long(argc, argv,
"hVf",
249 long_options, &options_index)) != -1) {
266 printf(
"*WARNING* This will erase all data in the database; "
267 "are you sure? [y/N] ");
268 user_certain = getchar();
269 if (user_certain !=
'y' && user_certain !=
'Y') {
270 printf(
"Okay, quitting...\n");
278 fprintf(stderr,
"Error: unable to load configuration!\n");
282 if (connect_db(cfg)) {
284 fprintf(stderr,
"Error: unable to connect to database!\n");
292 fprintf(stderr,
"Error: unable to drop existing schema!\n");
300 if (db_do2(create)) {
301 fprintf(stderr,
"Error: unable to create schema!\n");
310 fprintf(stderr,
"Error: unable to insert initial data!\n");
316 printf(
"Database setup successfully.\n");
engineconfig_type * engine_config(const char *cfgfile, int cmdline_verbosity, engineconfig_type *oldcfg)
void engine_config_cleanup(engineconfig_type *config)
ods_status engine_config_check(engineconfig_type *config)
const char * db_data_mysql[]
const char * db_data_sqlite[]
const char * db_schema_mysql_create[]
const char * db_schema_mysql_drop[]
const char * db_schema_sqlite_drop[]
const char * db_schema_sqlite_create[]