43 #if defined(ENFORCER_DATABASE_SQLITE3) 50 static sqlite3* db = NULL;
51 #elif defined(ENFORCER_DATABASE_MYSQL) 52 #include <mysql/mysql.h> 58 static MYSQL* db = NULL;
61 #define AUTHOR_NAME "Jerry Lundström" 62 #define COPYRIGHT_STR "Copyright (c) 2014 .SE (The Internet Infrastructure Foundation) OpenDNSSEC" 64 static void usage(FILE* out) {
66 "\nBSD licensed, see LICENSE in source package for details.\n" 67 "Version %s. Report bugs to <%s>.\n",
72 static void version(FILE* out) {
74 "Database setup tool for %s version %s\n" 76 "%s. This is free software.\n" 77 "See source files for more license information\n",
86 #if defined(ENFORCER_DATABASE_SQLITE3) 94 if (sqlite3_initialize() != SQLITE_OK) {
99 SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_CREATE,
106 #elif defined(ENFORCER_DATABASE_MYSQL) 114 if (mysql_library_init(0, NULL, NULL)) {
118 if (!(db = mysql_init(NULL))
119 || !mysql_real_connect(db,
141 static int disconnect_db() {
142 #if defined(ENFORCER_DATABASE_SQLITE3) 151 #elif defined(ENFORCER_DATABASE_MYSQL) 165 static int db_do(
const char *sql,
size_t size) {
166 #if defined(ENFORCER_DATABASE_SQLITE3) 167 sqlite3_stmt* stmt = NULL;
176 if (sqlite3_prepare_v2(db, sql, size, &stmt, NULL) != SQLITE_OK
177 || sqlite3_step(stmt) != SQLITE_DONE)
180 sqlite3_finalize(stmt);
184 sqlite3_finalize(stmt);
187 #elif defined(ENFORCER_DATABASE_MYSQL) 195 if (mysql_real_query(db, sql, size)) {
205 static int db_do2(
const char** strs) {
210 for (i = 0; strs[i]; i++) {
214 for (; strs[i]; i++) {
215 if ((ret = snprintf(sqlp, left,
"%s", strs[i])) >= left) {
222 if (db_do(sql,
sizeof(sql) - left)) {
230 int main(
int argc,
char* argv[]) {
231 int c, options_index = 0;
232 static struct option long_options[] = {
233 {
"help", no_argument, 0,
'h'},
234 {
"version", no_argument, 0,
'V'},
235 {
"force", no_argument, 0,
'f'},
241 const char* cfgfile = ODS_SE_CFGFILE;
243 ods_log_init(
"ods-enforcerd", 0, NULL, 0);
245 while ((c=getopt_long(argc, argv,
"hVf",
246 long_options, &options_index)) != -1) {
263 printf(
"*WARNING* This will erase all data in the database; " 264 "are you sure? [y/N] ");
265 user_certain = getchar();
266 if (user_certain !=
'y' && user_certain !=
'Y') {
267 printf(
"Okay, quitting...\n");
275 fprintf(stderr,
"Error: unable to load configuration!\n");
279 if (connect_db(cfg)) {
281 fprintf(stderr,
"Error: unable to connect to database!\n");
289 fprintf(stderr,
"Error: unable to drop existing schema!\n");
297 if (db_do2(create)) {
298 fprintf(stderr,
"Error: unable to create schema!\n");
307 fprintf(stderr,
"Error: unable to insert initial data!\n");
313 printf(
"Database setup successfully.\n");
void engine_config_cleanup(engineconfig_type *config)
const char * db_schema_sqlite_create[]
engineconfig_type * engine_config(const char *cfgfile, int cmdline_verbosity, engineconfig_type *oldcfg)
void(* usage)(int sockfd)
const char * db_data_mysql[]
const char * db_schema_mysql_drop[]
int main(int argc, char *argv[])
const char * db_schema_mysql_create[]
const char * db_data_sqlite[]
ods_status engine_config_check(engineconfig_type *config)
const char * db_schema_sqlite_drop[]