| 3 |
* net_server |
* net_server |
| 4 |
* - network server with SSH support |
* - network server with SSH support |
| 5 |
* |
* |
| 6 |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
* Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com> |
| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
| 40 |
#include <netinet/in.h> |
#include <netinet/in.h> |
| 41 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
| 42 |
#include <sys/socket.h> |
#include <sys/socket.h> |
| 43 |
|
#include <sys/stat.h> |
| 44 |
#include <sys/types.h> |
#include <sys/types.h> |
| 45 |
#include <sys/wait.h> |
#include <sys/wait.h> |
| 46 |
|
|
| 56 |
|
|
| 57 |
enum _net_server_constant_t |
enum _net_server_constant_t |
| 58 |
{ |
{ |
| 59 |
|
SOCKET_LISTEN_BACKLOG = 20, |
| 60 |
WAIT_CHILD_PROCESS_EXIT_TIMEOUT = 5, // second |
WAIT_CHILD_PROCESS_EXIT_TIMEOUT = 5, // second |
| 61 |
WAIT_CHILD_PROCESS_KILL_TIMEOUT = 1, // second |
WAIT_CHILD_PROCESS_KILL_TIMEOUT = 1, // second |
| 62 |
|
|
| 116 |
else |
else |
| 117 |
{ |
{ |
| 118 |
ret = check_user(user, password); |
ret = check_user(user, password); |
| 119 |
|
if (ret == 2) // Enforce update user agreement |
| 120 |
|
{ |
| 121 |
|
BBS_update_eula = 1; |
| 122 |
|
ret = 0; |
| 123 |
|
} |
| 124 |
} |
} |
| 125 |
|
|
| 126 |
if (ret == 0) |
if (ret == 0) |
| 127 |
{ |
{ |
| 128 |
|
log_common("User [%s] authenticated successfully", user); |
| 129 |
return SSH_AUTH_SUCCESS; |
return SSH_AUTH_SUCCESS; |
| 130 |
} |
} |
| 131 |
|
|
| 134 |
sdata->error = 1; |
sdata->error = 1; |
| 135 |
} |
} |
| 136 |
|
|
| 137 |
|
log_common("User [%s] authentication failed (%d/%d)", user, |
| 138 |
|
sdata->tries, BBS_login_retry_times); |
| 139 |
return SSH_AUTH_DENIED; |
return SSH_AUTH_DENIED; |
| 140 |
} |
} |
| 141 |
|
|
| 157 |
rc = openpty(&cdata->pty_master, &cdata->pty_slave, NULL, NULL, cdata->winsize); |
rc = openpty(&cdata->pty_master, &cdata->pty_slave, NULL, NULL, cdata->winsize); |
| 158 |
if (rc != 0) |
if (rc != 0) |
| 159 |
{ |
{ |
| 160 |
log_error("Failed to open pty\n"); |
log_error("Failed to open pty"); |
| 161 |
return SSH_ERROR; |
return SSH_ERROR; |
| 162 |
} |
} |
| 163 |
|
|
| 191 |
|
|
| 192 |
if (command != NULL) |
if (command != NULL) |
| 193 |
{ |
{ |
| 194 |
log_error("Forbid exec /bin/sh %s %s)\n", mode, command); |
log_error("Forbid exec /bin/sh %s %s)", mode, command); |
| 195 |
} |
} |
| 196 |
|
|
| 197 |
return SSH_OK; |
return SSH_OK; |
| 203 |
|
|
| 204 |
if (command != NULL) |
if (command != NULL) |
| 205 |
{ |
{ |
| 206 |
log_error("Forbid exec /bin/sh -c %s)\n", command); |
log_error("Forbid exec /bin/sh -c %s)", command); |
| 207 |
} |
} |
| 208 |
|
|
| 209 |
return SSH_OK; |
return SSH_OK; |
| 253 |
(void)session; |
(void)session; |
| 254 |
(void)channel; |
(void)channel; |
| 255 |
|
|
| 256 |
log_error("subsystem_request(subsystem=%s)\n", subsystem); |
log_error("subsystem_request(subsystem=%s)", subsystem); |
| 257 |
|
|
| 258 |
/* subsystem requests behave similarly to exec requests. */ |
/* subsystem requests behave similarly to exec requests. */ |
| 259 |
if (strcmp(subsystem, "sftp") == 0) |
if (strcmp(subsystem, "sftp") == 0) |
| 327 |
if (pid > 0) // Parent process |
if (pid > 0) // Parent process |
| 328 |
{ |
{ |
| 329 |
SYS_child_process_count++; |
SYS_child_process_count++; |
| 330 |
log_common("Child process (%d) start\n", pid); |
log_common("Child process (%d) start", pid); |
| 331 |
return pid; |
return pid; |
| 332 |
} |
} |
| 333 |
else if (pid < 0) // Error |
else if (pid < 0) // Error |
| 334 |
{ |
{ |
| 335 |
log_error("fork() error (%d)\n", errno); |
log_error("fork() error (%d)", errno); |
| 336 |
return -1; |
return -1; |
| 337 |
} |
} |
| 338 |
|
|
| 340 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 341 |
if (close(epollfd_server) < 0) |
if (close(epollfd_server) < 0) |
| 342 |
{ |
{ |
| 343 |
log_error("close(epollfd_server) error (%d)\n"); |
log_error("close(epollfd_server) error (%d)"); |
| 344 |
} |
} |
| 345 |
#endif |
#endif |
| 346 |
|
|
| 348 |
{ |
{ |
| 349 |
if (close(socket_server[i]) == -1) |
if (close(socket_server[i]) == -1) |
| 350 |
{ |
{ |
| 351 |
log_error("Close server socket failed\n"); |
log_error("Close server socket failed"); |
| 352 |
} |
} |
| 353 |
} |
} |
| 354 |
|
|
| 361 |
{ |
{ |
| 362 |
if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK) |
if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK) |
| 363 |
{ |
{ |
| 364 |
log_error("ssh_bind_accept_fd() error: %s\n", ssh_get_error(SSH_session)); |
log_error("ssh_bind_accept_fd() error: %s", ssh_get_error(SSH_session)); |
| 365 |
goto cleanup; |
goto cleanup; |
| 366 |
} |
} |
| 367 |
|
|
| 370 |
ssh_timeout = 60; // second |
ssh_timeout = 60; // second |
| 371 |
if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) |
if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) |
| 372 |
{ |
{ |
| 373 |
log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session)); |
log_error("Error setting SSH options: %s", ssh_get_error(SSH_session)); |
| 374 |
goto cleanup; |
goto cleanup; |
| 375 |
} |
} |
| 376 |
|
|
| 383 |
|
|
| 384 |
if (ssh_handle_key_exchange(SSH_session)) |
if (ssh_handle_key_exchange(SSH_session)) |
| 385 |
{ |
{ |
| 386 |
log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session)); |
log_error("ssh_handle_key_exchange() error: %s", ssh_get_error(SSH_session)); |
| 387 |
goto cleanup; |
goto cleanup; |
| 388 |
} |
} |
| 389 |
|
|
| 395 |
ret = ssh_event_dopoll(event, 100); // 0.1 second |
ret = ssh_event_dopoll(event, 100); // 0.1 second |
| 396 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 397 |
{ |
{ |
| 398 |
#ifdef _DEBUG |
log_debug("ssh_event_dopoll() error: %s", ssh_get_error(SSH_session)); |
|
log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session)); |
|
|
#endif |
|
| 399 |
goto cleanup; |
goto cleanup; |
| 400 |
} |
} |
| 401 |
} |
} |
| 402 |
|
|
| 403 |
if (cb_data.error) |
if (cb_data.error) |
| 404 |
{ |
{ |
| 405 |
log_error("SSH auth error, tried %d times\n", cb_data.tries); |
log_error("SSH auth error, tried %d times", cb_data.tries); |
| 406 |
goto cleanup; |
goto cleanup; |
| 407 |
} |
} |
| 408 |
|
|
| 427 |
ssh_timeout = 0; |
ssh_timeout = 0; |
| 428 |
if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) |
if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) |
| 429 |
{ |
{ |
| 430 |
log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session)); |
log_error("Error setting SSH options: %s", ssh_get_error(SSH_session)); |
| 431 |
goto cleanup; |
goto cleanup; |
| 432 |
} |
} |
| 433 |
|
|
| 434 |
|
ssh_set_blocking(SSH_session, 0); |
| 435 |
} |
} |
| 436 |
|
|
| 437 |
// Redirect Input |
// Redirect Input |
| 438 |
if (dup2(socket_client, STDIN_FILENO) == -1) |
if (dup2(socket_client, STDIN_FILENO) == -1) |
| 439 |
{ |
{ |
| 440 |
log_error("Redirect stdin to client socket failed\n"); |
log_error("Redirect stdin to client socket failed"); |
| 441 |
goto cleanup; |
goto cleanup; |
| 442 |
} |
} |
| 443 |
|
|
| 444 |
// Redirect Output |
// Redirect Output |
| 445 |
if (dup2(socket_client, STDOUT_FILENO) == -1) |
if (dup2(socket_client, STDOUT_FILENO) == -1) |
| 446 |
{ |
{ |
| 447 |
log_error("Redirect stdout to client socket failed\n"); |
log_error("Redirect stdout to client socket failed"); |
| 448 |
goto cleanup; |
goto cleanup; |
| 449 |
} |
} |
| 450 |
|
|
| 451 |
if (io_init() < 0) |
if (io_init() < 0) |
| 452 |
{ |
{ |
| 453 |
log_error("io_init() error\n"); |
log_error("io_init() error"); |
| 454 |
goto cleanup; |
goto cleanup; |
| 455 |
} |
} |
| 456 |
|
|
| 457 |
SYS_child_process_count = 0; |
SYS_child_process_count = 0; |
| 458 |
|
|
| 459 |
|
// BWF compile |
| 460 |
|
if (bwf_compile() < 0) |
| 461 |
|
{ |
| 462 |
|
log_error("bwf_compile() error"); |
| 463 |
|
goto cleanup; |
| 464 |
|
} |
| 465 |
|
|
| 466 |
bbs_main(); |
bbs_main(); |
| 467 |
|
|
| 468 |
cleanup: |
cleanup: |
| 493 |
} |
} |
| 494 |
else if (close(socket_client) == -1) |
else if (close(socket_client) == -1) |
| 495 |
{ |
{ |
| 496 |
log_error("Close client socket failed\n"); |
log_error("Close client socket failed"); |
| 497 |
} |
} |
| 498 |
|
|
| 499 |
ssh_free(SSH_session); |
ssh_free(SSH_session); |
| 500 |
ssh_finalize(); |
ssh_finalize(); |
| 501 |
|
|
| 502 |
|
// BWF cleanup |
| 503 |
|
bwf_cleanup(); |
| 504 |
|
|
| 505 |
// Close Input and Output for client |
// Close Input and Output for client |
| 506 |
io_cleanup(); |
io_cleanup(); |
| 507 |
close(STDIN_FILENO); |
close(STDIN_FILENO); |
| 508 |
close(STDOUT_FILENO); |
close(STDOUT_FILENO); |
| 509 |
|
|
| 510 |
log_common("Process exit normally\n"); |
log_common("Process exit normally"); |
| 511 |
log_end(); |
log_end(); |
| 512 |
|
|
| 513 |
_exit(0); |
_exit(0); |
| 517 |
|
|
| 518 |
int net_server(const char *hostaddr, in_port_t port[]) |
int net_server(const char *hostaddr, in_port_t port[]) |
| 519 |
{ |
{ |
| 520 |
|
struct stat file_stat; |
| 521 |
unsigned int addrlen; |
unsigned int addrlen; |
| 522 |
int ret; |
int ret; |
| 523 |
int flags_server[2]; |
int flags_server[2]; |
| 524 |
struct sockaddr_in sin; |
struct sockaddr_in sin; |
| 525 |
|
char local_addr[INET_ADDRSTRLEN]; |
| 526 |
|
|
| 527 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 528 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 533 |
int nfds; |
int nfds; |
| 534 |
int notify_child_exit = 0; |
int notify_child_exit = 0; |
| 535 |
time_t tm_notify_child_exit = time(NULL); |
time_t tm_notify_child_exit = time(NULL); |
|
MENU_SET bbs_menu_new; |
|
|
MENU_SET top10_menu_new; |
|
|
int i, j; |
|
| 536 |
pid_t pid; |
pid_t pid; |
| 537 |
|
int ssh_key_valid = 0; |
| 538 |
int ssh_log_level = SSH_LOG_NOLOG; |
int ssh_log_level = SSH_LOG_NOLOG; |
| 539 |
|
|
| 540 |
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 545 |
|
|
| 546 |
sshbind = ssh_bind_new(); |
sshbind = ssh_bind_new(); |
| 547 |
|
|
| 548 |
|
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_RSA_KEY_FILE) < 0) |
| 549 |
|
{ |
| 550 |
|
log_error("Error loading SSH RSA key: %s", SSH_HOST_RSA_KEY_FILE); |
| 551 |
|
} |
| 552 |
|
else |
| 553 |
|
{ |
| 554 |
|
ssh_key_valid = 1; |
| 555 |
|
} |
| 556 |
|
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ED25519_KEY_FILE) < 0) |
| 557 |
|
{ |
| 558 |
|
log_error("Error loading SSH ED25519 key: %s", SSH_HOST_ED25519_KEY_FILE); |
| 559 |
|
} |
| 560 |
|
else |
| 561 |
|
{ |
| 562 |
|
ssh_key_valid = 1; |
| 563 |
|
} |
| 564 |
|
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ECDSA_KEY_FILE) < 0) |
| 565 |
|
{ |
| 566 |
|
log_error("Error loading SSH ECDSA key: %s", SSH_HOST_ECDSA_KEY_FILE); |
| 567 |
|
} |
| 568 |
|
else |
| 569 |
|
{ |
| 570 |
|
ssh_key_valid = 1; |
| 571 |
|
} |
| 572 |
|
|
| 573 |
|
if (!ssh_key_valid) |
| 574 |
|
{ |
| 575 |
|
log_error("Error: no valid SSH host key"); |
| 576 |
|
ssh_bind_free(sshbind); |
| 577 |
|
return -1; |
| 578 |
|
} |
| 579 |
|
|
| 580 |
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 || |
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 || |
| 581 |
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 || |
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 || |
| 582 |
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 || |
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "+ssh-ed25519,ecdsa-sha2-nistp256,ssh-rsa") < 0 || |
|
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 || |
|
| 583 |
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0) |
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0) |
| 584 |
{ |
{ |
| 585 |
log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind)); |
log_error("Error setting SSH bind options: %s", ssh_get_error(sshbind)); |
| 586 |
ssh_bind_free(sshbind); |
ssh_bind_free(sshbind); |
| 587 |
return -1; |
return -1; |
| 588 |
} |
} |
| 591 |
epollfd_server = epoll_create1(0); |
epollfd_server = epoll_create1(0); |
| 592 |
if (epollfd_server == -1) |
if (epollfd_server == -1) |
| 593 |
{ |
{ |
| 594 |
log_error("epoll_create1() error (%d)\n", errno); |
log_error("epoll_create1() error (%d)", errno); |
| 595 |
return -1; |
return -1; |
| 596 |
} |
} |
| 597 |
#endif |
#endif |
| 598 |
|
|
| 599 |
// Server socket |
// Server socket |
| 600 |
for (i = 0; i < 2; i++) |
for (int i = 0; i < 2; i++) |
| 601 |
{ |
{ |
| 602 |
socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 603 |
|
|
| 604 |
if (socket_server[i] < 0) |
if (socket_server[i] < 0) |
| 605 |
{ |
{ |
| 606 |
log_error("Create socket_server error (%d)\n", errno); |
log_error("Create socket_server error (%d)", errno); |
| 607 |
return -1; |
return -1; |
| 608 |
} |
} |
| 609 |
|
|
| 611 |
sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY); |
sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY); |
| 612 |
sin.sin_port = htons(port[i]); |
sin.sin_port = htons(port[i]); |
| 613 |
|
|
| 614 |
|
if (inet_ntop(AF_INET, &(sin.sin_addr), local_addr, sizeof(local_addr)) == NULL) |
| 615 |
|
{ |
| 616 |
|
log_error("inet_ntop() error (%d)", errno); |
| 617 |
|
return -1; |
| 618 |
|
} |
| 619 |
|
|
| 620 |
// Reuse address and port |
// Reuse address and port |
| 621 |
flags_server[i] = 1; |
flags_server[i] = 1; |
| 622 |
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags_server[i], sizeof(flags_server[i])) < 0) |
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags_server[i], sizeof(flags_server[i])) < 0) |
| 623 |
{ |
{ |
| 624 |
log_error("setsockopt SO_REUSEADDR error (%d)\n", errno); |
log_error("setsockopt SO_REUSEADDR error (%d)", errno); |
| 625 |
} |
} |
| 626 |
#if defined(SO_REUSEPORT) |
#if defined(SO_REUSEPORT) |
| 627 |
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags_server[i], sizeof(flags_server[i])) < 0) |
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags_server[i], sizeof(flags_server[i])) < 0) |
| 628 |
{ |
{ |
| 629 |
log_error("setsockopt SO_REUSEPORT error (%d)\n", errno); |
log_error("setsockopt SO_REUSEPORT error (%d)", errno); |
| 630 |
} |
} |
| 631 |
#endif |
#endif |
| 632 |
|
|
| 633 |
if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0) |
if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0) |
| 634 |
{ |
{ |
| 635 |
log_error("Bind address %s:%u error (%d)\n", |
log_error("Bind address %s:%u error (%d)", |
| 636 |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); |
local_addr, port[i], errno); |
| 637 |
return -1; |
return -1; |
| 638 |
} |
} |
| 639 |
|
|
| 640 |
if (listen(socket_server[i], 10) < 0) |
if (listen(socket_server[i], SOCKET_LISTEN_BACKLOG) < 0) |
| 641 |
{ |
{ |
| 642 |
log_error("Telnet socket listen error (%d)\n", errno); |
log_error("Telnet socket listen error (%d)", errno); |
| 643 |
return -1; |
return -1; |
| 644 |
} |
} |
| 645 |
|
|
| 646 |
log_common("Listening at %s:%u\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); |
log_common("Listening at %s:%u", local_addr, port[i]); |
| 647 |
|
|
| 648 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 649 |
ev.events = EPOLLIN; |
ev.events = EPOLLIN; |
| 650 |
ev.data.fd = socket_server[i]; |
ev.data.fd = socket_server[i]; |
| 651 |
if (epoll_ctl(epollfd_server, EPOLL_CTL_ADD, socket_server[i], &ev) == -1) |
if (epoll_ctl(epollfd_server, EPOLL_CTL_ADD, socket_server[i], &ev) == -1) |
| 652 |
{ |
{ |
| 653 |
log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno); |
log_error("epoll_ctl(socket_server[%d]) error (%d)", i, errno); |
| 654 |
if (close(epollfd_server) < 0) |
if (close(epollfd_server) < 0) |
| 655 |
{ |
{ |
| 656 |
log_error("close(epoll) error (%d)\n"); |
log_error("close(epoll) error (%d)"); |
| 657 |
} |
} |
| 658 |
return -1; |
return -1; |
| 659 |
} |
} |
| 663 |
fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK); |
fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK); |
| 664 |
} |
} |
| 665 |
|
|
| 666 |
|
ssh_bind_set_blocking(sshbind, 0); |
| 667 |
|
|
| 668 |
hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT); |
hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT); |
| 669 |
if (hash_dict_pid_sockaddr == NULL) |
if (hash_dict_pid_sockaddr == NULL) |
| 670 |
{ |
{ |
| 671 |
log_error("hash_dict_create(hash_dict_pid_sockaddr) error\n"); |
log_error("hash_dict_create(hash_dict_pid_sockaddr) error"); |
| 672 |
return -1; |
return -1; |
| 673 |
} |
} |
| 674 |
hash_dict_sockaddr_count = hash_dict_create(MAX_CLIENT_LIMIT); |
hash_dict_sockaddr_count = hash_dict_create(MAX_CLIENT_LIMIT); |
| 675 |
if (hash_dict_sockaddr_count == NULL) |
if (hash_dict_sockaddr_count == NULL) |
| 676 |
{ |
{ |
| 677 |
log_error("hash_dict_create(hash_dict_sockaddr_count) error\n"); |
log_error("hash_dict_create(hash_dict_sockaddr_count) error"); |
| 678 |
return -1; |
return -1; |
| 679 |
} |
} |
| 680 |
|
|
| 700 |
{ |
{ |
| 701 |
SYS_child_exit = 0; |
SYS_child_exit = 0; |
| 702 |
|
|
| 703 |
pid = waitpid(-1, NULL, WNOHANG); |
pid = waitpid(-1, &ret, WNOHANG); |
| 704 |
if (pid > 0) |
if (pid > 0) |
| 705 |
{ |
{ |
| 706 |
SYS_child_exit = 1; // Retry waitid |
SYS_child_exit = 1; // Retry waitid |
|
|
|
| 707 |
SYS_child_process_count--; |
SYS_child_process_count--; |
| 708 |
log_common("Child process (%d) exited\n", pid); |
|
| 709 |
|
if (WIFEXITED(ret)) |
| 710 |
|
{ |
| 711 |
|
log_common("Child process (%d) exited, status=%d", pid, WEXITSTATUS(ret)); |
| 712 |
|
} |
| 713 |
|
else if (WIFSIGNALED(ret)) |
| 714 |
|
{ |
| 715 |
|
log_common("Child process (%d) is killed, status=%d", pid, WTERMSIG(ret)); |
| 716 |
|
} |
| 717 |
|
else |
| 718 |
|
{ |
| 719 |
|
log_common("Child process (%d) exited abnormally, status=%d", pid, ret); |
| 720 |
|
} |
| 721 |
|
|
| 722 |
if (pid != section_list_loader_pid) |
if (pid != section_list_loader_pid) |
| 723 |
{ |
{ |
| 724 |
j = 0; |
int64_t j = 0; |
| 725 |
ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)pid, (int64_t *)&j); |
ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)pid, &j); |
| 726 |
if (ret < 0) |
if (ret < 0) |
| 727 |
{ |
{ |
| 728 |
log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", pid); |
log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error", pid); |
| 729 |
} |
} |
| 730 |
else |
else |
| 731 |
{ |
{ |
| 732 |
ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)j, -1); |
ret = hash_dict_inc(hash_dict_sockaddr_count, (in_addr_t)j, -1); |
| 733 |
if (ret < 0) |
if (ret <= 0) |
| 734 |
{ |
{ |
| 735 |
log_error("hash_dict_inc(hash_dict_sockaddr_count, %d, -1) error\n", j); |
log_error("hash_dict_inc(hash_dict_sockaddr_count, %lu, -1) error: %d", (in_addr_t)j, ret); |
| 736 |
} |
} |
| 737 |
|
|
| 738 |
ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)pid); |
ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)pid); |
| 739 |
if (ret < 0) |
if (ret < 0) |
| 740 |
{ |
{ |
| 741 |
log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", pid); |
log_error("hash_dict_del(hash_dict_pid_sockaddr, %lu) error", (uint64_t)pid); |
| 742 |
} |
} |
| 743 |
} |
} |
| 744 |
} |
} |
| 749 |
} |
} |
| 750 |
else if (pid < 0) |
else if (pid < 0) |
| 751 |
{ |
{ |
| 752 |
log_error("Error in waitpid(): %d\n", errno); |
log_error("Error in waitpid(): %d", errno); |
| 753 |
break; |
break; |
| 754 |
} |
} |
| 755 |
} |
} |
| 760 |
{ |
{ |
| 761 |
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 762 |
sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count); |
sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count); |
| 763 |
log_common("Notify %d child process to exit\n", SYS_child_process_count); |
log_common("Notify %d child process to exit", SYS_child_process_count); |
| 764 |
#endif |
#endif |
| 765 |
|
|
| 766 |
if (kill(-getpid(), SIGTERM) < 0) |
if (kill(0, SIGTERM) < 0) |
| 767 |
{ |
{ |
| 768 |
log_error("Send SIGTERM signal failed (%d)\n", errno); |
log_error("Send SIGTERM signal failed (%d)", errno); |
| 769 |
} |
} |
| 770 |
|
|
| 771 |
notify_child_exit = 1; |
notify_child_exit = 1; |
| 777 |
sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count); |
sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count); |
| 778 |
#endif |
#endif |
| 779 |
|
|
| 780 |
if (kill(-getpid(), SIGKILL) < 0) |
if (kill(0, SIGKILL) < 0) |
| 781 |
{ |
{ |
| 782 |
log_error("Send SIGKILL signal failed (%d)\n", errno); |
log_error("Send SIGKILL signal failed (%d)", errno); |
| 783 |
} |
} |
| 784 |
|
|
| 785 |
notify_child_exit = 2; |
notify_child_exit = 2; |
| 787 |
} |
} |
| 788 |
else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT) |
else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT) |
| 789 |
{ |
{ |
| 790 |
log_error("Main process prepare to exit without waiting for %d child process any longer\n", SYS_child_process_count); |
log_error("Main process prepare to exit without waiting for %d child process any longer", SYS_child_process_count); |
| 791 |
SYS_child_process_count = 0; |
SYS_child_process_count = 0; |
| 792 |
} |
} |
| 793 |
} |
} |
| 800 |
sd_notify(0, "RELOADING=1"); |
sd_notify(0, "RELOADING=1"); |
| 801 |
#endif |
#endif |
| 802 |
|
|
| 803 |
|
log_common("Reload configuration"); |
| 804 |
|
|
| 805 |
// Restart log |
// Restart log |
| 806 |
if (log_restart() < 0) |
if (log_restart() < 0) |
| 807 |
{ |
{ |
| 808 |
log_error("Restart logging failed\n"); |
log_error("Restart logging failed"); |
| 809 |
} |
} |
| 810 |
|
|
| 811 |
// Reload configuration |
// Reload configuration |
| 812 |
if (load_conf(CONF_BBSD) < 0) |
if (load_conf(CONF_BBSD) < 0) |
| 813 |
{ |
{ |
| 814 |
log_error("Reload conf failed\n"); |
log_error("Reload conf failed"); |
| 815 |
} |
} |
| 816 |
|
|
| 817 |
// Reload BWF config |
// Reload BWF config |
| 818 |
if (bwf_load(CONF_BWF) < 0) |
if (bwf_load(CONF_BWF) < 0) |
| 819 |
{ |
{ |
| 820 |
log_error("Reload BWF conf failed\n"); |
log_error("Reload BWF conf failed"); |
| 821 |
} |
} |
| 822 |
|
|
| 823 |
if (load_menu(&bbs_menu_new, CONF_MENU) < 0) |
// Get EULA modification tm |
| 824 |
|
if (stat(DATA_EULA, &file_stat) == -1) |
| 825 |
{ |
{ |
| 826 |
unload_menu(&bbs_menu_new); |
log_error("stat(%s) error", DATA_EULA, errno); |
|
log_error("Reload bbs menu failed\n"); |
|
| 827 |
} |
} |
| 828 |
else |
else |
| 829 |
{ |
{ |
| 830 |
|
BBS_eula_tm = file_stat.st_mtim.tv_sec; |
| 831 |
|
} |
| 832 |
|
|
| 833 |
|
if (detach_menu_shm(&bbs_menu) < 0) |
| 834 |
|
{ |
| 835 |
|
log_error("detach_menu_shm(bbs_menu) error"); |
| 836 |
|
} |
| 837 |
|
if (load_menu(&bbs_menu, CONF_MENU) < 0) |
| 838 |
|
{ |
| 839 |
|
log_error("load_menu(bbs_menu) error"); |
| 840 |
unload_menu(&bbs_menu); |
unload_menu(&bbs_menu); |
|
memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new)); |
|
|
log_common("Reload bbs menu successfully\n"); |
|
| 841 |
} |
} |
| 842 |
|
|
| 843 |
if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0) |
if (detach_menu_shm(&top10_menu) < 0) |
| 844 |
{ |
{ |
| 845 |
unload_menu(&top10_menu_new); |
log_error("detach_menu_shm(top10_menu) error"); |
|
log_error("Reload top10 menu failed\n"); |
|
| 846 |
} |
} |
| 847 |
else |
if (load_menu(&top10_menu, CONF_TOP10_MENU) < 0) |
| 848 |
{ |
{ |
| 849 |
|
log_error("load_menu(top10_menu) error"); |
| 850 |
unload_menu(&top10_menu); |
unload_menu(&top10_menu); |
|
top10_menu_new.allow_exit = 1; |
|
|
memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new)); |
|
|
log_common("Reload top10 menu successfully\n"); |
|
| 851 |
} |
} |
| 852 |
|
top10_menu.allow_exit = 1; |
| 853 |
|
|
| 854 |
for (int i = 0; i < data_files_load_startup_count; i++) |
for (int i = 0; i < data_files_load_startup_count; i++) |
| 855 |
{ |
{ |
| 856 |
if (load_file(data_files_load_startup[i]) < 0) |
if (load_file(data_files_load_startup[i]) < 0) |
| 857 |
{ |
{ |
| 858 |
log_error("load_file(%s) error\n", data_files_load_startup[i]); |
log_error("load_file(%s) error", data_files_load_startup[i]); |
| 859 |
} |
} |
| 860 |
} |
} |
|
log_common("Reload data files successfully\n"); |
|
| 861 |
|
|
| 862 |
// Load section config and gen_ex |
// Load section config and gen_ex |
| 863 |
if (load_section_config_from_db(1) < 0) |
if (load_section_config_from_db(1) < 0) |
| 864 |
{ |
{ |
| 865 |
log_error("load_section_config_from_db(1) error\n"); |
log_error("load_section_config_from_db(1) error"); |
|
} |
|
|
else |
|
|
{ |
|
|
log_common("Reload section config and gen_ex successfully\n"); |
|
| 866 |
} |
} |
| 867 |
|
|
| 868 |
// Notify child processes to reload configuration |
// Notify child processes to reload configuration |
| 869 |
if (kill(-getpid(), SIGUSR1) < 0) |
if (kill(0, SIGUSR1) < 0) |
| 870 |
{ |
{ |
| 871 |
log_error("Send SIGUSR1 signal failed (%d)\n", errno); |
log_error("Send SIGUSR1 signal failed (%d)", errno); |
| 872 |
} |
} |
| 873 |
|
|
| 874 |
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 892 |
if (errno != EINTR) |
if (errno != EINTR) |
| 893 |
{ |
{ |
| 894 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 895 |
log_error("epoll_wait() error (%d)\n", errno); |
log_error("epoll_wait() error (%d)", errno); |
| 896 |
#else |
#else |
| 897 |
log_error("poll() error (%d)\n", errno); |
log_error("poll() error (%d)", errno); |
| 898 |
#endif |
#endif |
| 899 |
break; |
break; |
| 900 |
} |
} |
| 937 |
} |
} |
| 938 |
else |
else |
| 939 |
{ |
{ |
| 940 |
log_error("accept(socket_server) error (%d)\n", errno); |
log_error("accept(socket_server) error (%d)", errno); |
| 941 |
break; |
break; |
| 942 |
} |
} |
| 943 |
} |
} |
| 944 |
|
|
| 945 |
strncpy(hostaddr_client, inet_ntoa(sin.sin_addr), sizeof(hostaddr_client) - 1); |
if (inet_ntop(AF_INET, &(sin.sin_addr), hostaddr_client, sizeof(hostaddr_client)) == NULL) |
| 946 |
hostaddr_client[sizeof(hostaddr_client) - 1] = '\0'; |
{ |
| 947 |
|
log_error("inet_ntop() error (%d)", errno); |
| 948 |
|
close(socket_client); |
| 949 |
|
break; |
| 950 |
|
} |
| 951 |
port_client = ntohs(sin.sin_port); |
port_client = ntohs(sin.sin_port); |
| 952 |
|
|
|
log_common("Accept %s connection from %s:%d\n", (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client); |
|
|
|
|
| 953 |
if (SYS_child_process_count - 1 < BBS_max_client) |
if (SYS_child_process_count - 1 < BBS_max_client) |
| 954 |
{ |
{ |
| 955 |
j = 0; |
int64_t j = 0; |
| 956 |
ret = hash_dict_get(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, (int64_t *)&j); |
ret = hash_dict_get(hash_dict_sockaddr_count, sin.sin_addr.s_addr, &j); |
| 957 |
if (ret < 0) |
if (ret < 0) |
| 958 |
{ |
{ |
| 959 |
log_error("hash_dict_get(hash_dict_sockaddr_count, %s) error\n", hostaddr_client); |
log_error("hash_dict_get(hash_dict_sockaddr_count, %s) error", hostaddr_client); |
| 960 |
} |
} |
| 961 |
|
|
| 962 |
if (j < BBS_max_client_per_ip) |
if (j < BBS_max_client_per_ip) |
| 963 |
{ |
{ |
| 964 |
if ((pid = fork_server()) < 0) |
if ((pid = fork_server()) < 0) |
| 965 |
{ |
{ |
| 966 |
log_error("fork_server() error\n"); |
log_error("fork_server() error"); |
| 967 |
} |
} |
| 968 |
else if (pid > 0) |
else if (pid > 0) |
| 969 |
{ |
{ |
| 970 |
ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr); |
ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr); |
| 971 |
if (ret < 0) |
if (ret < 0) |
| 972 |
{ |
{ |
| 973 |
log_error("hash_dict_set(hash_dict_pid_sockaddr, %d, %s) error\n", pid, hostaddr_client); |
log_error("hash_dict_set(hash_dict_pid_sockaddr, %lu, %s) error", (uint64_t)pid, hostaddr_client); |
| 974 |
} |
} |
| 975 |
|
|
| 976 |
ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1); |
if (j == 0) |
|
if (ret < 0) |
|
| 977 |
{ |
{ |
| 978 |
log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, 1); |
// First connection from this IP |
| 979 |
|
log_common("Accept %s connection from %s:%d", |
| 980 |
|
(SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client); |
| 981 |
|
|
| 982 |
|
ret = hash_dict_set(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1); |
| 983 |
|
if (ret < 0) |
| 984 |
|
{ |
| 985 |
|
log_error("hash_dict_set(hash_dict_sockaddr_count, %s, 1) error", hostaddr_client); |
| 986 |
|
} |
| 987 |
|
} |
| 988 |
|
else |
| 989 |
|
{ |
| 990 |
|
// Increase connection count from this IP |
| 991 |
|
log_common("Accept %s connection from %s:%d, already have %d connections", |
| 992 |
|
(SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, j); |
| 993 |
|
|
| 994 |
|
ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1); |
| 995 |
|
if (ret <= 0) |
| 996 |
|
{ |
| 997 |
|
log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, 1) error: %d", hostaddr_client, ret); |
| 998 |
|
} |
| 999 |
} |
} |
| 1000 |
} |
} |
| 1001 |
} |
} |
| 1002 |
else |
else |
| 1003 |
{ |
{ |
| 1004 |
log_error("Rejected client connection from %s over limit per IP (%d)\n", hostaddr_client, BBS_max_client_per_ip); |
log_error("Rejected %s connection from %s:%d over limit per IP (%d >= %d)", |
| 1005 |
|
(SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, j, BBS_max_client_per_ip); |
| 1006 |
} |
} |
| 1007 |
} |
} |
| 1008 |
else |
else |
| 1009 |
{ |
{ |
| 1010 |
log_error("Rejected client connection over limit (%d)\n", SYS_child_process_count - 1); |
log_error("Rejected %s connection from %s:%d over limit (%d >= %d)", |
| 1011 |
|
(SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, SYS_child_process_count - 1, BBS_max_client); |
| 1012 |
} |
} |
| 1013 |
|
|
| 1014 |
if (close(socket_client) == -1) |
if (close(socket_client) == -1) |
| 1015 |
{ |
{ |
| 1016 |
log_error("close(socket_lient) error (%d)\n", errno); |
log_error("close(socket_lient) error (%d)", errno); |
| 1017 |
} |
} |
| 1018 |
} |
} |
| 1019 |
} |
} |
| 1023 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 1024 |
if (close(epollfd_server) < 0) |
if (close(epollfd_server) < 0) |
| 1025 |
{ |
{ |
| 1026 |
log_error("close(epollfd_server) error (%d)\n"); |
log_error("close(epollfd_server) error (%d)"); |
| 1027 |
} |
} |
| 1028 |
#endif |
#endif |
| 1029 |
|
|
| 1030 |
for (i = 0; i < 2; i++) |
for (int i = 0; i < 2; i++) |
| 1031 |
{ |
{ |
| 1032 |
if (close(socket_server[i]) == -1) |
if (close(socket_server[i]) == -1) |
| 1033 |
{ |
{ |
| 1034 |
log_error("Close server socket failed\n"); |
log_error("Close server socket failed"); |
| 1035 |
} |
} |
| 1036 |
} |
} |
| 1037 |
|
|