| 20 |
|
|
| 21 |
#include "net_server.h" |
#include "net_server.h" |
| 22 |
#include "common.h" |
#include "common.h" |
| 23 |
|
#include "bbs_main.h" |
| 24 |
|
#include "bbs.h" |
| 25 |
#include "log.h" |
#include "log.h" |
| 26 |
#include "io.h" |
#include "io.h" |
| 27 |
#include "init.h" |
#include "init.h" |
|
#include "fork.h" |
|
| 28 |
#include "menu.h" |
#include "menu.h" |
| 29 |
|
#include "database.h" |
| 30 |
|
#include "login.h" |
| 31 |
#include "file_loader.h" |
#include "file_loader.h" |
| 32 |
#include "section_list_loader.h" |
#include "section_list_loader.h" |
| 33 |
#include <errno.h> |
#include <errno.h> |
| 43 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
| 44 |
#include <netinet/in.h> |
#include <netinet/in.h> |
| 45 |
#include <systemd/sd-daemon.h> |
#include <systemd/sd-daemon.h> |
| 46 |
|
#include <libssh/libssh.h> |
| 47 |
|
#include <libssh/server.h> |
| 48 |
|
#include <libssh/callbacks.h> |
| 49 |
|
|
| 50 |
struct process_sockaddr_t |
struct process_sockaddr_t |
| 51 |
{ |
{ |
| 56 |
|
|
| 57 |
static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT]; |
static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT]; |
| 58 |
|
|
| 59 |
int net_server(const char *hostaddr, in_port_t port) |
#define SSH_AUTH_MAX_DURATION 60 // seconds |
| 60 |
|
|
| 61 |
|
struct ssl_server_cb_data_t |
| 62 |
{ |
{ |
| 63 |
unsigned int namelen; |
int tries; |
| 64 |
|
int error; |
| 65 |
|
}; |
| 66 |
|
|
| 67 |
|
static int auth_password(ssh_session session, const char *user, |
| 68 |
|
const char *password, void *userdata) |
| 69 |
|
{ |
| 70 |
|
MYSQL *db; |
| 71 |
|
struct ssl_server_cb_data_t *p_data = userdata; |
| 72 |
int ret; |
int ret; |
|
int flags; |
|
|
struct sockaddr_in sin; |
|
|
struct epoll_event ev, events[MAX_EVENTS]; |
|
|
int nfds, epollfd; |
|
|
siginfo_t siginfo; |
|
|
int sd_notify_stopping = 0; |
|
|
MENU_SET *p_bbs_menu_new; |
|
|
int i, j; |
|
|
pid_t pid; |
|
| 73 |
|
|
| 74 |
socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
if ((db = db_open()) == NULL) |
| 75 |
|
{ |
| 76 |
|
return SSH_AUTH_ERROR; |
| 77 |
|
} |
| 78 |
|
|
| 79 |
if (socket_server < 0) |
if (strcmp(user, "guest") == 0) |
| 80 |
{ |
{ |
| 81 |
log_error("Create socket failed\n"); |
ret = load_guest_info(db); |
| 82 |
return -1; |
} |
| 83 |
|
else |
| 84 |
|
{ |
| 85 |
|
ret = check_user(db, user, password); |
| 86 |
} |
} |
| 87 |
|
|
| 88 |
sin.sin_family = AF_INET; |
mysql_close(db); |
|
sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY); |
|
|
sin.sin_port = htons(port); |
|
| 89 |
|
|
| 90 |
// Reuse address and port |
if (ret == 0) |
|
flags = 1; |
|
|
if (setsockopt(socket_server, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags)) < 0) |
|
| 91 |
{ |
{ |
| 92 |
log_error("setsockopt SO_REUSEADDR error (%d)\n", errno); |
return SSH_AUTH_SUCCESS; |
| 93 |
} |
} |
| 94 |
if (setsockopt(socket_server, SOL_SOCKET, SO_REUSEPORT, &flags, sizeof(flags)) < 0) |
|
| 95 |
|
if ((++(p_data->tries)) >= BBS_login_retry_times) |
| 96 |
{ |
{ |
| 97 |
log_error("setsockopt SO_REUSEPORT error (%d)\n", errno); |
p_data->error = 1; |
| 98 |
} |
} |
| 99 |
|
|
| 100 |
if (bind(socket_server, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
return SSH_AUTH_DENIED; |
| 101 |
|
} |
| 102 |
|
|
| 103 |
|
static int pty_request(ssh_session session, ssh_channel channel, const char *term, |
| 104 |
|
int x, int y, int px, int py, void *userdata) |
| 105 |
|
{ |
| 106 |
|
return 0; |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
static int shell_request(ssh_session session, ssh_channel channel, void *userdata) |
| 110 |
|
{ |
| 111 |
|
return 0; |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
static struct ssh_channel_callbacks_struct channel_cb = { |
| 115 |
|
.channel_pty_request_function = pty_request, |
| 116 |
|
.channel_shell_request_function = shell_request}; |
| 117 |
|
|
| 118 |
|
static ssh_channel new_session_channel(ssh_session session, void *userdata) |
| 119 |
|
{ |
| 120 |
|
if (SSH_channel != NULL) |
| 121 |
{ |
{ |
| 122 |
log_error("Bind address %s:%u failed (%d)\n", |
return NULL; |
|
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); |
|
|
return -1; |
|
| 123 |
} |
} |
| 124 |
|
|
| 125 |
if (listen(socket_server, 10) < 0) |
SSH_channel = ssh_channel_new(session); |
| 126 |
|
ssh_callbacks_init(&channel_cb); |
| 127 |
|
ssh_set_channel_callbacks(SSH_channel, &channel_cb); |
| 128 |
|
|
| 129 |
|
return SSH_channel; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
static int fork_server(void) |
| 133 |
|
{ |
| 134 |
|
ssh_event event; |
| 135 |
|
int pid; |
| 136 |
|
int i; |
| 137 |
|
int ret; |
| 138 |
|
|
| 139 |
|
struct ssl_server_cb_data_t cb_data = { |
| 140 |
|
.tries = 0, |
| 141 |
|
.error = 0, |
| 142 |
|
}; |
| 143 |
|
|
| 144 |
|
struct ssh_server_callbacks_struct cb = { |
| 145 |
|
.userdata = &cb_data, |
| 146 |
|
.auth_password_function = auth_password, |
| 147 |
|
.channel_open_request_session_function = new_session_channel, |
| 148 |
|
}; |
| 149 |
|
|
| 150 |
|
pid = fork(); |
| 151 |
|
|
| 152 |
|
if (pid > 0) // Parent process |
| 153 |
|
{ |
| 154 |
|
SYS_child_process_count++; |
| 155 |
|
log_common("Child process (%d) start\n", pid); |
| 156 |
|
return pid; |
| 157 |
|
} |
| 158 |
|
else if (pid < 0) // Error |
| 159 |
{ |
{ |
| 160 |
log_error("Socket listen failed (%d)\n", errno); |
log_error("fork() error (%d)\n", errno); |
| 161 |
return -1; |
return -1; |
| 162 |
} |
} |
| 163 |
|
|
| 164 |
strncpy(hostaddr_server, inet_ntoa(sin.sin_addr), sizeof(hostaddr_server) - 1); |
// Child process |
| 165 |
hostaddr_server[sizeof(hostaddr_server) - 1] = '\0'; |
|
| 166 |
|
if (close(socket_server[0]) == -1 || close(socket_server[1]) == -1) |
| 167 |
|
{ |
| 168 |
|
log_error("Close server socket failed\n"); |
| 169 |
|
} |
| 170 |
|
|
| 171 |
|
SSH_session = ssh_new(); |
| 172 |
|
|
| 173 |
|
if (SSH_v2) |
| 174 |
|
{ |
| 175 |
|
if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK) |
| 176 |
|
{ |
| 177 |
|
log_error("ssh_bind_accept_fd() error: %s\n", ssh_get_error(SSH_session)); |
| 178 |
|
goto cleanup; |
| 179 |
|
} |
| 180 |
|
|
| 181 |
|
ssh_bind_free(sshbind); |
| 182 |
|
|
| 183 |
|
ssh_callbacks_init(&cb); |
| 184 |
|
ssh_set_server_callbacks(SSH_session, &cb); |
| 185 |
|
|
| 186 |
|
if (ssh_handle_key_exchange(SSH_session)) |
| 187 |
|
{ |
| 188 |
|
log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session)); |
| 189 |
|
goto cleanup; |
| 190 |
|
} |
| 191 |
|
ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD); |
| 192 |
|
|
| 193 |
port_server = ntohs(sin.sin_port); |
event = ssh_event_new(); |
| 194 |
namelen = sizeof(sin); |
ssh_event_add_session(event, SSH_session); |
| 195 |
|
|
| 196 |
|
for (i = 0; i < SSH_AUTH_MAX_DURATION && !SYS_server_exit && !cb_data.error && SSH_channel == NULL; i++) |
| 197 |
|
{ |
| 198 |
|
ret = ssh_event_dopoll(event, 1000); // 1 second |
| 199 |
|
if (ret == SSH_ERROR) |
| 200 |
|
{ |
| 201 |
|
log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session)); |
| 202 |
|
goto cleanup; |
| 203 |
|
} |
| 204 |
|
} |
| 205 |
|
|
| 206 |
log_std("Listening at %s:%d\n", hostaddr_server, port_server); |
if (cb_data.error) |
| 207 |
|
{ |
| 208 |
|
log_error("SSH auth error, tried %d times\n", cb_data.tries); |
| 209 |
|
goto cleanup; |
| 210 |
|
} |
| 211 |
|
} |
| 212 |
|
|
| 213 |
|
// Redirect Input |
| 214 |
|
close(STDIN_FILENO); |
| 215 |
|
if (dup2(socket_client, STDIN_FILENO) == -1) |
| 216 |
|
{ |
| 217 |
|
log_error("Redirect stdin to client socket failed\n"); |
| 218 |
|
goto cleanup; |
| 219 |
|
} |
| 220 |
|
|
| 221 |
|
// Redirect Output |
| 222 |
|
close(STDOUT_FILENO); |
| 223 |
|
if (dup2(socket_client, STDOUT_FILENO) == -1) |
| 224 |
|
{ |
| 225 |
|
log_error("Redirect stdout to client socket failed\n"); |
| 226 |
|
goto cleanup; |
| 227 |
|
} |
| 228 |
|
|
| 229 |
|
SYS_child_process_count = 0; |
| 230 |
|
|
| 231 |
|
bbs_main(); |
| 232 |
|
|
| 233 |
|
cleanup: |
| 234 |
|
// Child process exit |
| 235 |
|
SYS_server_exit = 1; |
| 236 |
|
|
| 237 |
|
if (SSH_v2) |
| 238 |
|
{ |
| 239 |
|
ssh_channel_free(SSH_channel); |
| 240 |
|
ssh_disconnect(SSH_session); |
| 241 |
|
} |
| 242 |
|
else if (close(socket_client) == -1) |
| 243 |
|
{ |
| 244 |
|
log_error("Close client socket failed\n"); |
| 245 |
|
} |
| 246 |
|
|
| 247 |
|
ssh_free(SSH_session); |
| 248 |
|
ssh_finalize(); |
| 249 |
|
|
| 250 |
|
// Close Input and Output for client |
| 251 |
|
close(STDIN_FILENO); |
| 252 |
|
close(STDOUT_FILENO); |
| 253 |
|
|
| 254 |
|
log_common("Process exit normally\n"); |
| 255 |
|
log_end(); |
| 256 |
|
|
| 257 |
|
_exit(0); |
| 258 |
|
|
| 259 |
|
return 0; |
| 260 |
|
} |
| 261 |
|
|
| 262 |
|
int net_server(const char *hostaddr, in_port_t port[]) |
| 263 |
|
{ |
| 264 |
|
unsigned int addrlen; |
| 265 |
|
int ret; |
| 266 |
|
int flags[2]; |
| 267 |
|
struct sockaddr_in sin; |
| 268 |
|
struct epoll_event ev, events[MAX_EVENTS]; |
| 269 |
|
int nfds, epollfd; |
| 270 |
|
siginfo_t siginfo; |
| 271 |
|
int sd_notify_stopping = 0; |
| 272 |
|
MENU_SET *p_bbs_menu_new; |
| 273 |
|
int i, j; |
| 274 |
|
pid_t pid; |
| 275 |
|
int ssh_log_level = SSH_LOG_NOLOG; |
| 276 |
|
|
| 277 |
|
ssh_init(); |
| 278 |
|
|
| 279 |
|
sshbind = ssh_bind_new(); |
| 280 |
|
|
| 281 |
|
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 || |
| 282 |
|
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 || |
| 283 |
|
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 || |
| 284 |
|
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0) |
| 285 |
|
{ |
| 286 |
|
log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind)); |
| 287 |
|
ssh_bind_free(sshbind); |
| 288 |
|
return -1; |
| 289 |
|
} |
| 290 |
|
|
| 291 |
epollfd = epoll_create1(0); |
epollfd = epoll_create1(0); |
| 292 |
if (epollfd < 0) |
if (epollfd < 0) |
| 295 |
return -1; |
return -1; |
| 296 |
} |
} |
| 297 |
|
|
| 298 |
ev.events = EPOLLIN; |
// Server socket |
| 299 |
ev.data.fd = socket_server; |
for (i = 0; i < 2; i++) |
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server, &ev) == -1) |
|
| 300 |
{ |
{ |
| 301 |
log_error("epoll_ctl(socket_server) error (%d)\n", errno); |
socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 302 |
if (close(epollfd) < 0) |
|
| 303 |
|
if (socket_server[i] < 0) |
| 304 |
{ |
{ |
| 305 |
log_error("close(epoll) error (%d)\n"); |
log_error("Create socket_server error (%d)\n", errno); |
| 306 |
|
return -1; |
| 307 |
|
} |
| 308 |
|
|
| 309 |
|
sin.sin_family = AF_INET; |
| 310 |
|
sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY); |
| 311 |
|
sin.sin_port = htons(port[i]); |
| 312 |
|
|
| 313 |
|
// Reuse address and port |
| 314 |
|
flags[i] = 1; |
| 315 |
|
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags[i], sizeof(flags[i])) < 0) |
| 316 |
|
{ |
| 317 |
|
log_error("setsockopt SO_REUSEADDR error (%d)\n", errno); |
| 318 |
|
} |
| 319 |
|
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags[i], sizeof(flags[i])) < 0) |
| 320 |
|
{ |
| 321 |
|
log_error("setsockopt SO_REUSEPORT error (%d)\n", errno); |
| 322 |
|
} |
| 323 |
|
|
| 324 |
|
if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0) |
| 325 |
|
{ |
| 326 |
|
log_error("Bind address %s:%u error (%d)\n", |
| 327 |
|
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); |
| 328 |
|
return -1; |
| 329 |
|
} |
| 330 |
|
|
| 331 |
|
if (listen(socket_server[i], 10) < 0) |
| 332 |
|
{ |
| 333 |
|
log_error("Telnet socket listen error (%d)\n", errno); |
| 334 |
|
return -1; |
| 335 |
|
} |
| 336 |
|
|
| 337 |
|
log_common("Listening at %s:%u\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); |
| 338 |
|
|
| 339 |
|
ev.events = EPOLLIN; |
| 340 |
|
ev.data.fd = socket_server[i]; |
| 341 |
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server[i], &ev) == -1) |
| 342 |
|
{ |
| 343 |
|
log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno); |
| 344 |
|
if (close(epollfd) < 0) |
| 345 |
|
{ |
| 346 |
|
log_error("close(epoll) error (%d)\n"); |
| 347 |
|
} |
| 348 |
|
return -1; |
| 349 |
} |
} |
|
return -1; |
|
|
} |
|
| 350 |
|
|
| 351 |
flags = fcntl(socket_server, F_GETFL, 0); |
flags[i] = fcntl(socket_server[i], F_GETFL, 0); |
| 352 |
fcntl(socket_server, F_SETFL, flags | O_NONBLOCK); |
fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK); |
| 353 |
|
} |
| 354 |
|
|
| 355 |
// Startup complete |
// Startup complete |
| 356 |
sd_notifyf(0, "READY=1\n" |
sd_notifyf(0, "READY=1\n" |
| 357 |
"STATUS=Listening at %s:%d\n" |
"STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n" |
| 358 |
"MAINPID=%d", |
"MAINPID=%d", |
| 359 |
hostaddr_server, port_server, getpid()); |
hostaddr, port[0], hostaddr, port[1], getpid()); |
| 360 |
|
|
| 361 |
while (!SYS_server_exit || SYS_child_process_count > 0) |
while (!SYS_server_exit || SYS_child_process_count > 0) |
| 362 |
{ |
{ |
| 377 |
SYS_child_exit = 1; // Retry waitid |
SYS_child_exit = 1; // Retry waitid |
| 378 |
|
|
| 379 |
SYS_child_process_count--; |
SYS_child_process_count--; |
| 380 |
log_std("Child process (%d) exited\n", siginfo.si_pid); |
log_common("Child process (%d) exited\n", siginfo.si_pid); |
| 381 |
|
|
| 382 |
if (siginfo.si_pid != section_list_loader_pid) |
if (siginfo.si_pid != section_list_loader_pid) |
| 383 |
{ |
{ |
| 409 |
|
|
| 410 |
if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0) |
if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0) |
| 411 |
{ |
{ |
| 412 |
log_std("Notify %d child process to exit\n", SYS_child_process_count); |
log_common("Notify %d child process to exit\n", SYS_child_process_count); |
| 413 |
if (kill(0, SIGTERM) < 0) |
if (kill(0, SIGTERM) < 0) |
| 414 |
{ |
{ |
| 415 |
log_error("Send SIGTERM signal failed (%d)\n", errno); |
log_error("Send SIGTERM signal failed (%d)\n", errno); |
| 450 |
p_bbs_menu = p_bbs_menu_new; |
p_bbs_menu = p_bbs_menu_new; |
| 451 |
p_bbs_menu_new = NULL; |
p_bbs_menu_new = NULL; |
| 452 |
|
|
| 453 |
log_std("Reload menu successfully\n"); |
log_common("Reload menu successfully\n"); |
| 454 |
} |
} |
| 455 |
|
|
| 456 |
sd_notify(0, "READY=1"); |
sd_notify(0, "READY=1"); |
| 469 |
} |
} |
| 470 |
} |
} |
| 471 |
|
|
| 472 |
log_std("Reload data files successfully\n"); |
log_common("Reload data files successfully\n"); |
| 473 |
sd_notify(0, "READY=1"); |
sd_notify(0, "READY=1"); |
| 474 |
} |
} |
| 475 |
|
|
| 503 |
|
|
| 504 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 505 |
{ |
{ |
| 506 |
if (events[i].data.fd == socket_server) |
if (events[i].data.fd == socket_server[0] || events[i].data.fd == socket_server[1]) |
| 507 |
{ |
{ |
| 508 |
|
SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0); |
| 509 |
|
|
| 510 |
while (!SYS_server_exit) // Accept all incoming connections until error |
while (!SYS_server_exit) // Accept all incoming connections until error |
| 511 |
{ |
{ |
| 512 |
socket_client = accept(socket_server, (struct sockaddr *)&sin, &namelen); |
addrlen = sizeof(sin); |
| 513 |
|
socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, &addrlen); |
| 514 |
if (socket_client < 0) |
if (socket_client < 0) |
| 515 |
{ |
{ |
| 516 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 533 |
|
|
| 534 |
port_client = ntohs(sin.sin_port); |
port_client = ntohs(sin.sin_port); |
| 535 |
|
|
| 536 |
log_std("Accept connection from %s:%d\n", hostaddr_client, port_client); |
log_common("Accept %sconnection from %s:%d\n", (SSH_v2 ? "" : "SSH2 "), hostaddr_client, port_client); |
| 537 |
|
|
| 538 |
if (SYS_child_process_count - 1 < BBS_max_client) |
if (SYS_child_process_count - 1 < BBS_max_client) |
| 539 |
{ |
{ |
| 545 |
j++; |
j++; |
| 546 |
if (j >= BBS_max_client_per_ip) |
if (j >= BBS_max_client_per_ip) |
| 547 |
{ |
{ |
| 548 |
log_error("Too many client connections (%d) from %s\n", j, hostaddr_client); |
log_common("Too many client connections (%d) from %s\n", j, hostaddr_client); |
| 549 |
break; |
break; |
| 550 |
} |
} |
| 551 |
} |
} |
| 599 |
log_error("close(epoll) error (%d)\n"); |
log_error("close(epoll) error (%d)\n"); |
| 600 |
} |
} |
| 601 |
|
|
| 602 |
fcntl(socket_server, F_SETFL, flags); |
for (i = 0; i < 2; i++) |
|
|
|
|
if (close(socket_server) == -1) |
|
| 603 |
{ |
{ |
| 604 |
log_error("Close server socket failed\n"); |
fcntl(socket_server[i], F_SETFL, flags[i]); |
| 605 |
|
|
| 606 |
|
if (close(socket_server[i]) == -1) |
| 607 |
|
{ |
| 608 |
|
log_error("Close server socket failed\n"); |
| 609 |
|
} |
| 610 |
} |
} |
| 611 |
|
|
| 612 |
|
ssh_bind_free(sshbind); |
| 613 |
|
ssh_finalize(); |
| 614 |
|
|
| 615 |
return 0; |
return 0; |
| 616 |
} |
} |