--- lbbs/src/database.c 2025/07/02 04:17:33 1.17 +++ lbbs/src/database.c 2026/01/03 10:27:14 1.29 @@ -1,55 +1,100 @@ -/*************************************************************************** - database.c - description - ------------------- - Copyright : (C) 2004-2025 by Leaflet - Email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * database + * - configuration and function of DB connection + * + * Copyright (C) 2004-2026 Leaflet + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include "common.h" #include "database.h" #include "log.h" +#include +#include +#include #include -#include +#include +#include // Global declaration for database -char DB_host[256]; -char DB_username[50]; -char DB_password[50]; -char DB_database[50]; -char DB_timezone[50]; +char DB_ca_cert[FILE_PATH_LEN] = "conf/ca_cert.pem"; +char DB_host[DB_host_max_len + 1]; +char DB_username[DB_username_max_len + 1]; +char DB_password[DB_password_max_len + 1]; +char DB_database[DB_database_max_len + 1]; +char DB_timezone[DB_timezone_max_len + 1]; MYSQL *db_open() { MYSQL *db = NULL; +#ifdef HAVE_MARIADB_CLIENT + my_bool verify_server_cert = 0; +#else + unsigned int ssl_mode = SSL_MODE_PREFERRED; +#endif char sql[SQL_BUFFER_LEN]; + int fd; + int have_ca_cert = 0; db = mysql_init(NULL); if (db == NULL) { - log_error("mysql_init() failed\n"); + log_error("mysql_init() failed"); return NULL; } + fd = open(DB_ca_cert, O_RDONLY); + if (fd == -1) + { + if (errno != ENOENT) + { + log_error("open(%s) error: %d", DB_ca_cert, errno); + } + } + else + { + close(fd); + have_ca_cert = 1; +#ifndef HAVE_MARIADB_CLIENT + ssl_mode = SSL_MODE_VERIFY_CA; +#endif + } + + if (mysql_ssl_set(db, NULL, NULL, (have_ca_cert ? DB_ca_cert : NULL), NULL, NULL) != 0) + { + log_error("mysql_ssl_set() error"); + return NULL; + } + +#ifdef HAVE_MARIADB_CLIENT + if (mysql_optionsv(db, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, &verify_server_cert) != 0) + { + log_error("mysql_optionsv() error"); + return NULL; + } +#else + if (mysql_options(db, MYSQL_OPT_SSL_MODE, &ssl_mode) != 0) + { + log_error("mysql_options() error"); + return NULL; + } +#endif + if (mysql_real_connect(db, DB_host, DB_username, DB_password, DB_database, 0, NULL, 0) == NULL) { - log_error("mysql_real_connect() error: %s\n", mysql_error(db)); + log_error("mysql_real_connect() error: %s", mysql_error(db)); mysql_close(db); return NULL; } if (mysql_set_character_set(db, "utf8") != 0) { - log_error("Set character set error: %s\n", mysql_error(db)); + log_error("Set character set error: %s", mysql_error(db)); mysql_close(db); return NULL; } @@ -60,7 +105,7 @@ MYSQL *db_open() if (mysql_query(db, sql) != 0) { - log_error("Set timezone error: %s\n", mysql_error(db)); + log_error("Set timezone error: %s", mysql_error(db)); mysql_close(db); return NULL; }