| 1 |
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
| 2 |
/*
|
| 3 |
* database
|
| 4 |
* - configuration and function of DB connection
|
| 5 |
*
|
| 6 |
* Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com>
|
| 7 |
*/
|
| 8 |
|
| 9 |
#ifdef HAVE_CONFIG_H
|
| 10 |
#include "config.h"
|
| 11 |
#endif
|
| 12 |
|
| 13 |
#include "common.h"
|
| 14 |
#include "database.h"
|
| 15 |
#include "log.h"
|
| 16 |
#include <errno.h>
|
| 17 |
#include <fcntl.h>
|
| 18 |
#include <mysql.h>
|
| 19 |
#include <stdio.h>
|
| 20 |
#include <string.h>
|
| 21 |
#include <sys/stat.h>
|
| 22 |
|
| 23 |
// Global declaration for database
|
| 24 |
char DB_ca_cert[FILE_PATH_LEN] = "conf/ca_cert.pem";
|
| 25 |
char DB_host[DB_host_max_len + 1];
|
| 26 |
char DB_username[DB_username_max_len + 1];
|
| 27 |
char DB_password[DB_password_max_len + 1];
|
| 28 |
char DB_database[DB_database_max_len + 1];
|
| 29 |
char DB_timezone[DB_timezone_max_len + 1];
|
| 30 |
|
| 31 |
MYSQL *db_open()
|
| 32 |
{
|
| 33 |
MYSQL *db = NULL;
|
| 34 |
#ifdef HAVE_MARIADB_CLIENT
|
| 35 |
my_bool verify_server_cert = 0;
|
| 36 |
#else
|
| 37 |
unsigned int ssl_mode = SSL_MODE_PREFERRED;
|
| 38 |
#endif
|
| 39 |
char sql[SQL_BUFFER_LEN];
|
| 40 |
int fd;
|
| 41 |
int have_ca_cert = 0;
|
| 42 |
|
| 43 |
db = mysql_init(NULL);
|
| 44 |
if (db == NULL)
|
| 45 |
{
|
| 46 |
log_error("mysql_init() failed");
|
| 47 |
return NULL;
|
| 48 |
}
|
| 49 |
|
| 50 |
fd = open(DB_ca_cert, O_RDONLY);
|
| 51 |
if (fd == -1)
|
| 52 |
{
|
| 53 |
if (errno != ENOENT)
|
| 54 |
{
|
| 55 |
log_error("open(%s) error: %d", DB_ca_cert, errno);
|
| 56 |
}
|
| 57 |
}
|
| 58 |
else
|
| 59 |
{
|
| 60 |
close(fd);
|
| 61 |
have_ca_cert = 1;
|
| 62 |
#ifndef HAVE_MARIADB_CLIENT
|
| 63 |
ssl_mode = SSL_MODE_VERIFY_CA;
|
| 64 |
#endif
|
| 65 |
}
|
| 66 |
|
| 67 |
if (mysql_ssl_set(db, NULL, NULL, (have_ca_cert ? DB_ca_cert : NULL), NULL, NULL) != 0)
|
| 68 |
{
|
| 69 |
log_error("mysql_ssl_set() error");
|
| 70 |
return NULL;
|
| 71 |
}
|
| 72 |
|
| 73 |
#ifdef HAVE_MARIADB_CLIENT
|
| 74 |
if (mysql_optionsv(db, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, &verify_server_cert) != 0)
|
| 75 |
{
|
| 76 |
log_error("mysql_optionsv() error");
|
| 77 |
return NULL;
|
| 78 |
}
|
| 79 |
#else
|
| 80 |
if (mysql_options(db, MYSQL_OPT_SSL_MODE, &ssl_mode) != 0)
|
| 81 |
{
|
| 82 |
log_error("mysql_options() error");
|
| 83 |
return NULL;
|
| 84 |
}
|
| 85 |
#endif
|
| 86 |
|
| 87 |
if (mysql_real_connect(db, DB_host, DB_username, DB_password, DB_database,
|
| 88 |
0, NULL, 0) == NULL)
|
| 89 |
{
|
| 90 |
log_error("mysql_real_connect() error: %s", mysql_error(db));
|
| 91 |
mysql_close(db);
|
| 92 |
return NULL;
|
| 93 |
}
|
| 94 |
|
| 95 |
if (mysql_set_character_set(db, "utf8") != 0)
|
| 96 |
{
|
| 97 |
log_error("Set character set error: %s", mysql_error(db));
|
| 98 |
mysql_close(db);
|
| 99 |
return NULL;
|
| 100 |
}
|
| 101 |
|
| 102 |
snprintf(sql, sizeof(sql),
|
| 103 |
"SET time_zone = '%s'",
|
| 104 |
DB_timezone);
|
| 105 |
|
| 106 |
if (mysql_query(db, sql) != 0)
|
| 107 |
{
|
| 108 |
log_error("Set timezone error: %s", mysql_error(db));
|
| 109 |
mysql_close(db);
|
| 110 |
return NULL;
|
| 111 |
}
|
| 112 |
|
| 113 |
return db;
|
| 114 |
}
|