| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
init.c - description |
/* |
| 3 |
------------------- |
* init |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - initializer of server daemon |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 by Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
|
/*************************************************************************** |
|
|
* * |
|
|
* 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. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 8 |
|
|
|
#include "init.h" |
|
|
#include "database.h" |
|
| 9 |
#include "bbs.h" |
#include "bbs.h" |
| 10 |
#include "common.h" |
#include "common.h" |
| 11 |
#include "log.h" |
#include "database.h" |
| 12 |
|
#include "init.h" |
| 13 |
#include "io.h" |
#include "io.h" |
| 14 |
#include <string.h> |
#include "log.h" |
|
#include <stdlib.h> |
|
| 15 |
#include <signal.h> |
#include <signal.h> |
| 16 |
|
#include <stdlib.h> |
| 17 |
|
#include <string.h> |
| 18 |
|
#include <unistd.h> |
| 19 |
#include <sys/param.h> |
#include <sys/param.h> |
|
#include <sys/types.h> |
|
| 20 |
#include <sys/stat.h> |
#include <sys/stat.h> |
| 21 |
#include <unistd.h> |
#include <sys/types.h> |
| 22 |
|
|
| 23 |
|
#define CONF_DELIM_WITH_SPACE " \t\r\n" |
| 24 |
|
|
| 25 |
int init_daemon(void) |
int init_daemon(void) |
| 26 |
{ |
{ |
| 59 |
|
|
| 60 |
int load_conf(const char *conf_file) |
int load_conf(const char *conf_file) |
| 61 |
{ |
{ |
| 62 |
char temp[256]; |
char buffer[LINE_BUFFER_LEN]; |
| 63 |
|
char *saveptr = NULL; |
| 64 |
// Load configuration |
char *p, *q, *r; |
| 65 |
char c_name[256]; |
char *y, *m, *d; |
| 66 |
|
int v; |
| 67 |
FILE *fin; |
FILE *fin; |
| 68 |
|
|
| 69 |
|
// Load configuration |
| 70 |
if ((fin = fopen(conf_file, "r")) == NULL) |
if ((fin = fopen(conf_file, "r")) == NULL) |
| 71 |
{ |
{ |
| 72 |
log_error("Open %s failed", conf_file); |
log_error("Open %s failed", conf_file); |
| 73 |
return -1; |
return -1; |
| 74 |
} |
} |
| 75 |
|
|
| 76 |
while (fscanf(fin, "%s", c_name) != EOF) |
while (fgets(buffer, sizeof(buffer), fin) != NULL) |
| 77 |
{ |
{ |
| 78 |
if (c_name[0] == '#') |
p = strtok_r(buffer, CONF_DELIM_WITH_SPACE, &saveptr); |
| 79 |
|
if (p == NULL) // Blank line |
| 80 |
|
{ |
| 81 |
|
continue; |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line |
| 85 |
{ |
{ |
|
fgets(temp, 256, fin); |
|
| 86 |
continue; |
continue; |
| 87 |
} |
} |
| 88 |
fscanf(fin, "%*c"); |
|
| 89 |
if (strcmp(c_name, "bbs_id") == 0) |
q = strtok_r(NULL, CONF_DELIM_WITH_SPACE, &saveptr); |
| 90 |
|
if (q == NULL) // Empty value |
| 91 |
{ |
{ |
| 92 |
fscanf(fin, "%s", BBS_id); |
log_error("Skip empty value of config item: %s\n", p); |
| 93 |
|
continue; |
| 94 |
} |
} |
| 95 |
if (strcmp(c_name, "bbs_name") == 0) |
|
| 96 |
|
// Check syntax |
| 97 |
|
r = strtok_r(NULL, CONF_DELIM_WITH_SPACE, &saveptr); |
| 98 |
|
if (r != NULL && r[0] != '#') |
| 99 |
{ |
{ |
| 100 |
fscanf(fin, "%s", BBS_name); |
log_error("Skip config line with extra value %s = %s %s\n", p, q, r); |
| 101 |
|
continue; |
| 102 |
} |
} |
| 103 |
if (strcmp(c_name, "bbs_server") == 0) |
|
| 104 |
|
if (strcasecmp(p, "bbs_id") == 0) |
| 105 |
{ |
{ |
| 106 |
fscanf(fin, "%s", BBS_server); |
strncpy(BBS_id, q, sizeof(BBS_id) - 1); |
| 107 |
|
BBS_id[sizeof(BBS_id) - 1] = '\0'; |
| 108 |
} |
} |
| 109 |
if (strcmp(c_name, "bbs_address") == 0) |
else if (strcasecmp(p, "bbs_name") == 0) |
| 110 |
{ |
{ |
| 111 |
fscanf(fin, "%s", BBS_address); |
strncpy(BBS_name, q, sizeof(BBS_name) - 1); |
| 112 |
|
BBS_name[sizeof(BBS_name) - 1] = '\0'; |
| 113 |
} |
} |
| 114 |
if (strcmp(c_name, "bbs_port") == 0) |
else if (strcasecmp(p, "bbs_server") == 0) |
| 115 |
{ |
{ |
| 116 |
fscanf(fin, "%hu", &BBS_port); |
strncpy(BBS_server, q, sizeof(BBS_server) - 1); |
| 117 |
|
BBS_server[sizeof(BBS_server) - 1] = '\0'; |
| 118 |
} |
} |
| 119 |
if (strcmp(c_name, "bbs_max_client") == 0) |
else if (strcasecmp(p, "bbs_address") == 0) |
| 120 |
{ |
{ |
| 121 |
fscanf(fin, "%d", &BBS_max_client); |
strncpy(BBS_address, q, sizeof(BBS_address) - 1); |
| 122 |
if (BBS_max_client > MAX_CLIENTS_LIMIT) |
BBS_address[sizeof(BBS_address) - 1] = '\0'; |
| 123 |
|
} |
| 124 |
|
else if (strcasecmp(p, "bbs_port") == 0) |
| 125 |
|
{ |
| 126 |
|
BBS_port[0] = (in_port_t)atoi(q); |
| 127 |
|
} |
| 128 |
|
else if (strcasecmp(p, "bbs_ssh_port") == 0) |
| 129 |
|
{ |
| 130 |
|
BBS_port[1] = (in_port_t)atoi(q); |
| 131 |
|
} |
| 132 |
|
else if (strcasecmp(p, "bbs_max_client") == 0) |
| 133 |
|
{ |
| 134 |
|
BBS_max_client = atoi(q); |
| 135 |
|
if (BBS_max_client <= 0 || BBS_max_client > MAX_CLIENT_LIMIT) |
| 136 |
|
{ |
| 137 |
|
log_error("Ignore config bbs_max_client with incorrect value %d\n", BBS_max_client); |
| 138 |
|
BBS_max_client = MAX_CLIENT_LIMIT; |
| 139 |
|
} |
| 140 |
|
} |
| 141 |
|
else if (strcasecmp(p, "bbs_max_client_per_ip") == 0) |
| 142 |
|
{ |
| 143 |
|
BBS_max_client_per_ip = atoi(q); |
| 144 |
|
if (BBS_max_client_per_ip <= 0 || BBS_max_client_per_ip > MAX_CLIENT_PER_IP_LIMIT) |
| 145 |
{ |
{ |
| 146 |
log_error("Config BBS_max_client > limit (%d), reset it to limit\n", BBS_max_client); |
log_error("Ignore config bbs_max_client with incorrect value %d\n", BBS_max_client_per_ip); |
| 147 |
BBS_max_client = MAX_CLIENTS_LIMIT; |
BBS_max_client_per_ip = MAX_CLIENT_PER_IP_LIMIT; |
| 148 |
} |
} |
| 149 |
} |
} |
| 150 |
if (strcmp(c_name, "bbs_max_client_per_ip") == 0) |
else if (strcasecmp(p, "bbs_start_dt") == 0) |
| 151 |
{ |
{ |
| 152 |
fscanf(fin, "%d", &BBS_max_client_per_ip); |
y = strtok_r(q, "-", &saveptr); |
| 153 |
|
m = strtok_r(NULL, "-", &saveptr); |
| 154 |
|
d = strtok_r(NULL, "-", &saveptr); |
| 155 |
|
|
| 156 |
|
if (y == NULL || m == NULL || d == NULL) |
| 157 |
|
{ |
| 158 |
|
log_error("Ignore config bbs_start_dt with incorrect value\n"); |
| 159 |
|
continue; |
| 160 |
|
} |
| 161 |
|
snprintf(BBS_start_dt, sizeof(BBS_start_dt), "%4s年%2s月%2s日", y, m, d); |
| 162 |
} |
} |
| 163 |
if (strcmp(c_name, "bbs_max_user") == 0) |
else if (strcasecmp(p, "bbs_sys_id") == 0) |
| 164 |
{ |
{ |
| 165 |
fscanf(fin, "%d", &BBS_max_user); |
v = atoi(q); |
| 166 |
|
if (v <= 0) |
| 167 |
|
{ |
| 168 |
|
log_error("Ignore config bbs_sys_id with incorrect value %d\n", v); |
| 169 |
|
continue; |
| 170 |
|
} |
| 171 |
|
BBS_sys_id = v; |
| 172 |
} |
} |
| 173 |
if (strcmp(c_name, "bbs_start_dt") == 0) |
else if (strcasecmp(p, "db_host") == 0) |
| 174 |
{ |
{ |
| 175 |
int y = 0, m = 0, d = 0; |
strncpy(DB_host, q, sizeof(DB_host) - 1); |
| 176 |
fscanf(fin, "%d-%d-%d", &y, &m, &d); |
DB_host[sizeof(DB_host) - 1] = '\0'; |
|
snprintf(BBS_start_dt, sizeof(BBS_start_dt), "%4dÄê%2dÔÂ%2dÈÕ", y, m, d); |
|
| 177 |
} |
} |
| 178 |
if (strcmp(c_name, "db_host") == 0) |
else if (strcasecmp(p, "db_username") == 0) |
| 179 |
{ |
{ |
| 180 |
fscanf(fin, "%s", DB_host); |
strncpy(DB_username, q, sizeof(DB_username) - 1); |
| 181 |
|
DB_username[sizeof(DB_username) - 1] = '\0'; |
| 182 |
} |
} |
| 183 |
if (strcmp(c_name, "db_username") == 0) |
else if (strcasecmp(p, "db_password") == 0) |
| 184 |
{ |
{ |
| 185 |
fscanf(fin, "%s", DB_username); |
strncpy(DB_password, q, sizeof(DB_password) - 1); |
| 186 |
|
DB_password[sizeof(DB_password) - 1] = '\0'; |
| 187 |
} |
} |
| 188 |
if (strcmp(c_name, "db_password") == 0) |
else if (strcasecmp(p, "db_database") == 0) |
| 189 |
{ |
{ |
| 190 |
fscanf(fin, "%s", DB_password); |
strncpy(DB_database, q, sizeof(DB_database) - 1); |
| 191 |
|
DB_database[sizeof(DB_database) - 1] = '\0'; |
| 192 |
} |
} |
| 193 |
if (strcmp(c_name, "db_database") == 0) |
else if (strcasecmp(p, "db_timezone") == 0) |
| 194 |
{ |
{ |
| 195 |
fscanf(fin, "%s", DB_database); |
strncpy(DB_timezone, q, sizeof(DB_timezone) - 1); |
| 196 |
|
DB_timezone[sizeof(DB_timezone) - 1] = '\0'; |
| 197 |
} |
} |
| 198 |
if (strcmp(c_name, "db_timezone") == 0) |
else |
| 199 |
{ |
{ |
| 200 |
fscanf(fin, "%s", DB_timezone); |
log_error("Unknown config %s = %s\n", p, q); |
| 201 |
} |
} |
| 202 |
} |
} |
| 203 |
|
|