diff --git a/src/users.c b/src/users.c index abb7d1f..2f6a789 100644 --- a/src/users.c +++ b/src/users.c @@ -16,6 +16,35 @@ extern struct bbs_config conf; extern struct user_record *gUser; static void open_users_db_or_die(sqlite3 **db) { + char *err_msg = NULL; + int rc = 0; + static const char *create_sql = + "CREATE TABLE IF NOT EXISTS users (" + "Id INTEGER PRIMARY KEY," + "loginname TEXT COLLATE NOCASE," + "password TEXT," + "salt TEXT," + "firstname TEXT," + "lastname TEXT," + "email TEXT," + "location TEXT," + "sec_level INTEGER," + "last_on INTEGER," + "time_left INTEGER," + "cur_mail_conf INTEGER," + "cur_mail_area INTEGER," + "cur_file_sub INTEGER," + "cur_file_dir INTEGER," + "times_on INTEGER," + "bwavepktno INTEGER," + "archiver INTEGER," + "protocol INTEGER," + "nodemsgs INTEGER," + "codepage INTEGER," + "exteditor INTEGER," + "bwavestyle INTEGER," + "signature TEXT," + "autosig INTEGER);"; char buffer[PATH_MAX]; snprintf(buffer, PATH_MAX, "%s/users.sq3", conf.bbs_path); if (sqlite3_open(buffer, db) != SQLITE_OK) { @@ -25,6 +54,13 @@ static void open_users_db_or_die(sqlite3 **db) { } assert(db != NULL); sqlite3_busy_timeout(*db, 5000); + rc = sqlite3_exec(db, create_sql, 0, 0, &err_msg); + if (rc != SQLITE_OK) { + dolog("SQL error: %s", err_msg); + sqlite3_free(err_msg); + sqlite3_close(db); + exit(1); + } } char *hash_sha256(char *pass, char *salt) { @@ -310,34 +346,6 @@ int inst_user(struct user_record *user) { char *err_msg = NULL; int rc = 0; - static const char *create_sql = - "CREATE TABLE IF NOT EXISTS users (" - "Id INTEGER PRIMARY KEY," - "loginname TEXT COLLATE NOCASE," - "password TEXT," - "salt TEXT," - "firstname TEXT," - "lastname TEXT," - "email TEXT," - "location TEXT," - "sec_level INTEGER," - "last_on INTEGER," - "time_left INTEGER," - "cur_mail_conf INTEGER," - "cur_mail_area INTEGER," - "cur_file_sub INTEGER," - "cur_file_dir INTEGER," - "times_on INTEGER," - "bwavepktno INTEGER," - "archiver INTEGER," - "protocol INTEGER," - "nodemsgs INTEGER," - "codepage INTEGER," - "exteditor INTEGER," - "bwavestyle INTEGER," - "signature TEXT," - "autosig INTEGER);"; - static const char *insert_sql = "INSERT INTO users (loginname, password, salt, firstname," "lastname, email, location, sec_level, last_on, time_left, " @@ -348,14 +356,6 @@ int inst_user(struct user_record *user) { open_users_db_or_die(&db); - rc = sqlite3_exec(db, create_sql, 0, 0, &err_msg); - if (rc != SQLITE_OK) { - dolog("SQL error: %s", err_msg); - sqlite3_free(err_msg); - sqlite3_close(db); - exit(1); - } - rc = sqlite3_prepare_v2(db, insert_sql, -1, &res, 0); if (rc != SQLITE_OK) { dolog("Failed to execute statement: %s", sqlite3_errmsg(db));