| 6 |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
|
#ifdef HAVE_CONFIG_H |
| 10 |
|
#include "config.h" |
| 11 |
|
#endif |
| 12 |
|
|
| 13 |
#include "bbs.h" |
#include "bbs.h" |
| 14 |
#include "bbs_main.h" |
#include "bbs_main.h" |
| 15 |
|
#include "bwf.h" |
| 16 |
#include "common.h" |
#include "common.h" |
| 17 |
#include "database.h" |
#include "database.h" |
| 18 |
#include "file_loader.h" |
#include "file_loader.h" |
| 19 |
|
#include "hash_dict.h" |
| 20 |
#include "io.h" |
#include "io.h" |
| 21 |
#include "init.h" |
#include "init.h" |
| 22 |
#include "log.h" |
#include "log.h" |
| 38 |
#include <libssh/libssh.h> |
#include <libssh/libssh.h> |
| 39 |
#include <libssh/server.h> |
#include <libssh/server.h> |
| 40 |
#include <netinet/in.h> |
#include <netinet/in.h> |
| 41 |
#include <sys/epoll.h> |
#include <sys/ioctl.h> |
| 42 |
#include <sys/socket.h> |
#include <sys/socket.h> |
| 43 |
#include <sys/syscall.h> |
#include <sys/stat.h> |
| 44 |
#include <sys/types.h> |
#include <sys/types.h> |
| 45 |
#include <sys/wait.h> |
#include <sys/wait.h> |
| 46 |
|
|
| 47 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 48 |
|
#include <sys/epoll.h> |
| 49 |
|
#else |
| 50 |
|
#include <poll.h> |
| 51 |
|
#endif |
| 52 |
|
|
| 53 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 54 |
#include <systemd/sd-daemon.h> |
#include <systemd/sd-daemon.h> |
| 55 |
|
#endif |
| 56 |
|
|
| 57 |
enum _net_server_constant_t |
enum _net_server_constant_t |
| 58 |
{ |
{ |
| 62 |
SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds |
SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds |
| 63 |
}; |
}; |
| 64 |
|
|
|
struct process_sockaddr_t |
|
|
{ |
|
|
pid_t pid; |
|
|
in_addr_t s_addr; |
|
|
}; |
|
|
typedef struct process_sockaddr_t PROCESS_SOCKADDR; |
|
|
|
|
|
static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT]; |
|
|
|
|
|
static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server"; |
|
|
|
|
| 65 |
/* A userdata struct for session. */ |
/* A userdata struct for session. */ |
| 66 |
struct session_data_struct |
struct session_data_struct |
| 67 |
{ |
{ |
| 88 |
struct winsize *winsize; |
struct winsize *winsize; |
| 89 |
}; |
}; |
| 90 |
|
|
| 91 |
|
static int socket_server[2]; |
| 92 |
|
static int socket_client; |
| 93 |
|
|
| 94 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 95 |
|
static int epollfd_server = -1; |
| 96 |
|
#endif |
| 97 |
|
|
| 98 |
|
static ssh_bind sshbind; |
| 99 |
|
|
| 100 |
|
static HASH_DICT *hash_dict_pid_sockaddr = NULL; |
| 101 |
|
static HASH_DICT *hash_dict_sockaddr_count = NULL; |
| 102 |
|
|
| 103 |
|
static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server"; |
| 104 |
|
|
| 105 |
static int auth_password(ssh_session session, const char *user, |
static int auth_password(ssh_session session, const char *user, |
| 106 |
const char *password, void *userdata) |
const char *password, void *userdata) |
| 107 |
{ |
{ |
| 115 |
else |
else |
| 116 |
{ |
{ |
| 117 |
ret = check_user(user, password); |
ret = check_user(user, password); |
| 118 |
|
if (ret == 2) // Enforce update user agreement |
| 119 |
|
{ |
| 120 |
|
BBS_update_eula = 1; |
| 121 |
|
ret = 0; |
| 122 |
|
} |
| 123 |
} |
} |
| 124 |
|
|
| 125 |
if (ret == 0) |
if (ret == 0) |
| 126 |
{ |
{ |
| 127 |
|
log_common("User [%s] authenticated successfully", user); |
| 128 |
return SSH_AUTH_SUCCESS; |
return SSH_AUTH_SUCCESS; |
| 129 |
} |
} |
| 130 |
|
|
| 133 |
sdata->error = 1; |
sdata->error = 1; |
| 134 |
} |
} |
| 135 |
|
|
| 136 |
|
log_common("User [%s] authentication failed (%d/%d)", user, |
| 137 |
|
sdata->tries, BBS_login_retry_times); |
| 138 |
return SSH_AUTH_DENIED; |
return SSH_AUTH_DENIED; |
| 139 |
} |
} |
| 140 |
|
|
| 156 |
rc = openpty(&cdata->pty_master, &cdata->pty_slave, NULL, NULL, cdata->winsize); |
rc = openpty(&cdata->pty_master, &cdata->pty_slave, NULL, NULL, cdata->winsize); |
| 157 |
if (rc != 0) |
if (rc != 0) |
| 158 |
{ |
{ |
| 159 |
log_error("Failed to open pty\n"); |
log_error("Failed to open pty"); |
| 160 |
return SSH_ERROR; |
return SSH_ERROR; |
| 161 |
} |
} |
| 162 |
|
|
| 190 |
|
|
| 191 |
if (command != NULL) |
if (command != NULL) |
| 192 |
{ |
{ |
| 193 |
log_error("Forbid exec /bin/sh %s %s)\n", mode, command); |
log_error("Forbid exec /bin/sh %s %s)", mode, command); |
| 194 |
} |
} |
| 195 |
|
|
| 196 |
return SSH_OK; |
return SSH_OK; |
| 202 |
|
|
| 203 |
if (command != NULL) |
if (command != NULL) |
| 204 |
{ |
{ |
| 205 |
log_error("Forbid exec /bin/sh -c %s)\n", command); |
log_error("Forbid exec /bin/sh -c %s)", command); |
| 206 |
} |
} |
| 207 |
|
|
| 208 |
return SSH_OK; |
return SSH_OK; |
| 252 |
(void)session; |
(void)session; |
| 253 |
(void)channel; |
(void)channel; |
| 254 |
|
|
| 255 |
log_error("subsystem_request(subsystem=%s)\n", subsystem); |
log_error("subsystem_request(subsystem=%s)", subsystem); |
| 256 |
|
|
| 257 |
/* subsystem requests behave similarly to exec requests. */ |
/* subsystem requests behave similarly to exec requests. */ |
| 258 |
if (strcmp(subsystem, "sftp") == 0) |
if (strcmp(subsystem, "sftp") == 0) |
| 326 |
if (pid > 0) // Parent process |
if (pid > 0) // Parent process |
| 327 |
{ |
{ |
| 328 |
SYS_child_process_count++; |
SYS_child_process_count++; |
| 329 |
log_common("Child process (%d) start\n", pid); |
log_common("Child process (%d) start", pid); |
| 330 |
return pid; |
return pid; |
| 331 |
} |
} |
| 332 |
else if (pid < 0) // Error |
else if (pid < 0) // Error |
| 333 |
{ |
{ |
| 334 |
log_error("fork() error (%d)\n", errno); |
log_error("fork() error (%d)", errno); |
| 335 |
return -1; |
return -1; |
| 336 |
} |
} |
| 337 |
|
|
| 338 |
// Child process |
// Child process |
| 339 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 340 |
|
if (close(epollfd_server) < 0) |
| 341 |
|
{ |
| 342 |
|
log_error("close(epollfd_server) error (%d)"); |
| 343 |
|
} |
| 344 |
|
#endif |
| 345 |
|
|
| 346 |
if (close(socket_server[0]) == -1 || close(socket_server[1]) == -1) |
for (i = 0; i < 2; i++) |
| 347 |
{ |
{ |
| 348 |
log_error("Close server socket failed\n"); |
if (close(socket_server[i]) == -1) |
| 349 |
|
{ |
| 350 |
|
log_error("Close server socket failed"); |
| 351 |
|
} |
| 352 |
} |
} |
| 353 |
|
|
| 354 |
|
hash_dict_destroy(hash_dict_pid_sockaddr); |
| 355 |
|
hash_dict_destroy(hash_dict_sockaddr_count); |
| 356 |
|
|
| 357 |
SSH_session = ssh_new(); |
SSH_session = ssh_new(); |
| 358 |
|
|
| 359 |
if (SSH_v2) |
if (SSH_v2) |
| 360 |
{ |
{ |
| 361 |
if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK) |
if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK) |
| 362 |
{ |
{ |
| 363 |
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)); |
| 364 |
goto cleanup; |
goto cleanup; |
| 365 |
} |
} |
| 366 |
|
|
| 369 |
ssh_timeout = 60; // second |
ssh_timeout = 60; // second |
| 370 |
if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) |
if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) |
| 371 |
{ |
{ |
| 372 |
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)); |
| 373 |
goto cleanup; |
goto cleanup; |
| 374 |
} |
} |
| 375 |
|
|
| 382 |
|
|
| 383 |
if (ssh_handle_key_exchange(SSH_session)) |
if (ssh_handle_key_exchange(SSH_session)) |
| 384 |
{ |
{ |
| 385 |
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)); |
| 386 |
goto cleanup; |
goto cleanup; |
| 387 |
} |
} |
| 388 |
|
|
| 394 |
ret = ssh_event_dopoll(event, 100); // 0.1 second |
ret = ssh_event_dopoll(event, 100); // 0.1 second |
| 395 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 396 |
{ |
{ |
| 397 |
#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 |
|
| 398 |
goto cleanup; |
goto cleanup; |
| 399 |
} |
} |
| 400 |
} |
} |
| 401 |
|
|
| 402 |
if (cb_data.error) |
if (cb_data.error) |
| 403 |
{ |
{ |
| 404 |
log_error("SSH auth error, tried %d times\n", cb_data.tries); |
log_error("SSH auth error, tried %d times", cb_data.tries); |
| 405 |
goto cleanup; |
goto cleanup; |
| 406 |
} |
} |
| 407 |
|
|
| 426 |
ssh_timeout = 0; |
ssh_timeout = 0; |
| 427 |
if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) |
if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) |
| 428 |
{ |
{ |
| 429 |
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)); |
| 430 |
goto cleanup; |
goto cleanup; |
| 431 |
} |
} |
| 432 |
|
|
| 433 |
|
ssh_set_blocking(SSH_session, 0); |
| 434 |
} |
} |
| 435 |
|
|
| 436 |
// Redirect Input |
// Redirect Input |
|
close(STDIN_FILENO); |
|
| 437 |
if (dup2(socket_client, STDIN_FILENO) == -1) |
if (dup2(socket_client, STDIN_FILENO) == -1) |
| 438 |
{ |
{ |
| 439 |
log_error("Redirect stdin to client socket failed\n"); |
log_error("Redirect stdin to client socket failed"); |
| 440 |
goto cleanup; |
goto cleanup; |
| 441 |
} |
} |
| 442 |
|
|
| 443 |
// Redirect Output |
// Redirect Output |
|
close(STDOUT_FILENO); |
|
| 444 |
if (dup2(socket_client, STDOUT_FILENO) == -1) |
if (dup2(socket_client, STDOUT_FILENO) == -1) |
| 445 |
{ |
{ |
| 446 |
log_error("Redirect stdout to client socket failed\n"); |
log_error("Redirect stdout to client socket failed"); |
| 447 |
|
goto cleanup; |
| 448 |
|
} |
| 449 |
|
|
| 450 |
|
if (io_init() < 0) |
| 451 |
|
{ |
| 452 |
|
log_error("io_init() error"); |
| 453 |
goto cleanup; |
goto cleanup; |
| 454 |
} |
} |
| 455 |
|
|
| 456 |
SYS_child_process_count = 0; |
SYS_child_process_count = 0; |
| 457 |
|
|
| 458 |
|
// BWF compile |
| 459 |
|
if (bwf_compile() < 0) |
| 460 |
|
{ |
| 461 |
|
log_error("bwf_compile() error"); |
| 462 |
|
goto cleanup; |
| 463 |
|
} |
| 464 |
|
|
| 465 |
bbs_main(); |
bbs_main(); |
| 466 |
|
|
| 467 |
cleanup: |
cleanup: |
| 470 |
|
|
| 471 |
if (SSH_v2) |
if (SSH_v2) |
| 472 |
{ |
{ |
| 473 |
close(cdata.pty_master); |
if (cdata.pty_master != -1) |
| 474 |
close(cdata.child_stdin); |
{ |
| 475 |
close(cdata.child_stdout); |
close(cdata.pty_master); |
| 476 |
close(cdata.child_stderr); |
} |
| 477 |
|
if (cdata.child_stdin != -1) |
| 478 |
|
{ |
| 479 |
|
close(cdata.child_stdin); |
| 480 |
|
} |
| 481 |
|
if (cdata.child_stdout != -1) |
| 482 |
|
{ |
| 483 |
|
close(cdata.child_stdout); |
| 484 |
|
} |
| 485 |
|
if (cdata.child_stderr != -1) |
| 486 |
|
{ |
| 487 |
|
close(cdata.child_stderr); |
| 488 |
|
} |
| 489 |
|
|
| 490 |
ssh_channel_free(SSH_channel); |
ssh_channel_free(SSH_channel); |
| 491 |
ssh_disconnect(SSH_session); |
ssh_disconnect(SSH_session); |
| 492 |
} |
} |
| 493 |
else if (close(socket_client) == -1) |
else if (close(socket_client) == -1) |
| 494 |
{ |
{ |
| 495 |
log_error("Close client socket failed\n"); |
log_error("Close client socket failed"); |
| 496 |
} |
} |
| 497 |
|
|
| 498 |
ssh_free(SSH_session); |
ssh_free(SSH_session); |
| 499 |
ssh_finalize(); |
ssh_finalize(); |
| 500 |
|
|
| 501 |
|
// BWF cleanup |
| 502 |
|
bwf_cleanup(); |
| 503 |
|
|
| 504 |
// Close Input and Output for client |
// Close Input and Output for client |
| 505 |
|
io_cleanup(); |
| 506 |
close(STDIN_FILENO); |
close(STDIN_FILENO); |
| 507 |
close(STDOUT_FILENO); |
close(STDOUT_FILENO); |
| 508 |
|
|
| 509 |
log_common("Process exit normally\n"); |
log_common("Process exit normally"); |
| 510 |
log_end(); |
log_end(); |
| 511 |
|
|
| 512 |
_exit(0); |
_exit(0); |
| 516 |
|
|
| 517 |
int net_server(const char *hostaddr, in_port_t port[]) |
int net_server(const char *hostaddr, in_port_t port[]) |
| 518 |
{ |
{ |
| 519 |
|
struct stat file_stat; |
| 520 |
unsigned int addrlen; |
unsigned int addrlen; |
| 521 |
int ret; |
int ret; |
| 522 |
int flags[2]; |
int flags_server[2]; |
| 523 |
struct sockaddr_in sin; |
struct sockaddr_in sin; |
| 524 |
|
char local_addr[INET_ADDRSTRLEN]; |
| 525 |
|
|
| 526 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 527 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 528 |
int nfds, epollfd; |
#else |
| 529 |
siginfo_t siginfo; |
struct pollfd pfds[2]; |
| 530 |
|
#endif |
| 531 |
|
|
| 532 |
|
int nfds; |
| 533 |
int notify_child_exit = 0; |
int notify_child_exit = 0; |
| 534 |
time_t tm_notify_child_exit = time(NULL); |
time_t tm_notify_child_exit = time(NULL); |
|
int sd_notify_stopping = 0; |
|
|
MENU_SET bbs_menu_new; |
|
|
MENU_SET top10_menu_new; |
|
|
int i, j; |
|
| 535 |
pid_t pid; |
pid_t pid; |
| 536 |
|
int ssh_key_valid = 0; |
| 537 |
int ssh_log_level = SSH_LOG_NOLOG; |
int ssh_log_level = SSH_LOG_NOLOG; |
| 538 |
|
|
| 539 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 540 |
|
int sd_notify_stopping = 0; |
| 541 |
|
#endif |
| 542 |
|
|
| 543 |
ssh_init(); |
ssh_init(); |
| 544 |
|
|
| 545 |
sshbind = ssh_bind_new(); |
sshbind = ssh_bind_new(); |
| 546 |
|
|
| 547 |
|
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_RSA_KEY_FILE) < 0) |
| 548 |
|
{ |
| 549 |
|
log_error("Error loading SSH RSA key: %s", SSH_HOST_RSA_KEY_FILE); |
| 550 |
|
} |
| 551 |
|
else |
| 552 |
|
{ |
| 553 |
|
ssh_key_valid = 1; |
| 554 |
|
} |
| 555 |
|
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ED25519_KEY_FILE) < 0) |
| 556 |
|
{ |
| 557 |
|
log_error("Error loading SSH ED25519 key: %s", SSH_HOST_ED25519_KEY_FILE); |
| 558 |
|
} |
| 559 |
|
else |
| 560 |
|
{ |
| 561 |
|
ssh_key_valid = 1; |
| 562 |
|
} |
| 563 |
|
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ECDSA_KEY_FILE) < 0) |
| 564 |
|
{ |
| 565 |
|
log_error("Error loading SSH ECDSA key: %s", SSH_HOST_ECDSA_KEY_FILE); |
| 566 |
|
} |
| 567 |
|
else |
| 568 |
|
{ |
| 569 |
|
ssh_key_valid = 1; |
| 570 |
|
} |
| 571 |
|
|
| 572 |
|
if (!ssh_key_valid) |
| 573 |
|
{ |
| 574 |
|
log_error("Error: no valid SSH host key"); |
| 575 |
|
ssh_bind_free(sshbind); |
| 576 |
|
return -1; |
| 577 |
|
} |
| 578 |
|
|
| 579 |
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 || |
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 || |
| 580 |
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 || |
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 || |
| 581 |
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 || |
|
| 582 |
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) |
| 583 |
{ |
{ |
| 584 |
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)); |
| 585 |
ssh_bind_free(sshbind); |
ssh_bind_free(sshbind); |
| 586 |
return -1; |
return -1; |
| 587 |
} |
} |
| 588 |
|
|
| 589 |
epollfd = epoll_create1(0); |
#ifdef HAVE_SYS_EPOLL_H |
| 590 |
if (epollfd < 0) |
epollfd_server = epoll_create1(0); |
| 591 |
|
if (epollfd_server == -1) |
| 592 |
{ |
{ |
| 593 |
log_error("epoll_create1() error (%d)\n", errno); |
log_error("epoll_create1() error (%d)", errno); |
| 594 |
return -1; |
return -1; |
| 595 |
} |
} |
| 596 |
|
#endif |
| 597 |
|
|
| 598 |
// Server socket |
// Server socket |
| 599 |
for (i = 0; i < 2; i++) |
for (int i = 0; i < 2; i++) |
| 600 |
{ |
{ |
| 601 |
socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 602 |
|
|
| 603 |
if (socket_server[i] < 0) |
if (socket_server[i] < 0) |
| 604 |
{ |
{ |
| 605 |
log_error("Create socket_server error (%d)\n", errno); |
log_error("Create socket_server error (%d)", errno); |
| 606 |
return -1; |
return -1; |
| 607 |
} |
} |
| 608 |
|
|
| 610 |
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); |
| 611 |
sin.sin_port = htons(port[i]); |
sin.sin_port = htons(port[i]); |
| 612 |
|
|
| 613 |
|
if (inet_ntop(AF_INET, &(sin.sin_addr), local_addr, sizeof(local_addr)) == NULL) |
| 614 |
|
{ |
| 615 |
|
log_error("inet_ntop() error (%d)", errno); |
| 616 |
|
return -1; |
| 617 |
|
} |
| 618 |
|
|
| 619 |
// Reuse address and port |
// Reuse address and port |
| 620 |
flags[i] = 1; |
flags_server[i] = 1; |
| 621 |
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags[i], sizeof(flags[i])) < 0) |
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags_server[i], sizeof(flags_server[i])) < 0) |
| 622 |
{ |
{ |
| 623 |
log_error("setsockopt SO_REUSEADDR error (%d)\n", errno); |
log_error("setsockopt SO_REUSEADDR error (%d)", errno); |
| 624 |
} |
} |
| 625 |
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags[i], sizeof(flags[i])) < 0) |
#if defined(SO_REUSEPORT) |
| 626 |
|
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags_server[i], sizeof(flags_server[i])) < 0) |
| 627 |
{ |
{ |
| 628 |
log_error("setsockopt SO_REUSEPORT error (%d)\n", errno); |
log_error("setsockopt SO_REUSEPORT error (%d)", errno); |
| 629 |
} |
} |
| 630 |
|
#endif |
| 631 |
|
|
| 632 |
if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0) |
if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0) |
| 633 |
{ |
{ |
| 634 |
log_error("Bind address %s:%u error (%d)\n", |
log_error("Bind address %s:%u error (%d)", |
| 635 |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); |
local_addr, port[i], errno); |
| 636 |
return -1; |
return -1; |
| 637 |
} |
} |
| 638 |
|
|
| 639 |
if (listen(socket_server[i], 10) < 0) |
if (listen(socket_server[i], 10) < 0) |
| 640 |
{ |
{ |
| 641 |
log_error("Telnet socket listen error (%d)\n", errno); |
log_error("Telnet socket listen error (%d)", errno); |
| 642 |
return -1; |
return -1; |
| 643 |
} |
} |
| 644 |
|
|
| 645 |
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]); |
| 646 |
|
|
| 647 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 648 |
ev.events = EPOLLIN; |
ev.events = EPOLLIN; |
| 649 |
ev.data.fd = socket_server[i]; |
ev.data.fd = socket_server[i]; |
| 650 |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server[i], &ev) == -1) |
if (epoll_ctl(epollfd_server, EPOLL_CTL_ADD, socket_server[i], &ev) == -1) |
| 651 |
{ |
{ |
| 652 |
log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno); |
log_error("epoll_ctl(socket_server[%d]) error (%d)", i, errno); |
| 653 |
if (close(epollfd) < 0) |
if (close(epollfd_server) < 0) |
| 654 |
{ |
{ |
| 655 |
log_error("close(epoll) error (%d)\n"); |
log_error("close(epoll) error (%d)"); |
| 656 |
} |
} |
| 657 |
return -1; |
return -1; |
| 658 |
} |
} |
| 659 |
|
#endif |
| 660 |
|
|
| 661 |
flags[i] = fcntl(socket_server[i], F_GETFL, 0); |
flags_server[i] = fcntl(socket_server[i], F_GETFL, 0); |
| 662 |
fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK); |
fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK); |
| 663 |
|
} |
| 664 |
|
|
| 665 |
|
ssh_bind_set_blocking(sshbind, 0); |
| 666 |
|
|
| 667 |
|
hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT); |
| 668 |
|
if (hash_dict_pid_sockaddr == NULL) |
| 669 |
|
{ |
| 670 |
|
log_error("hash_dict_create(hash_dict_pid_sockaddr) error"); |
| 671 |
|
return -1; |
| 672 |
|
} |
| 673 |
|
hash_dict_sockaddr_count = hash_dict_create(MAX_CLIENT_LIMIT); |
| 674 |
|
if (hash_dict_sockaddr_count == NULL) |
| 675 |
|
{ |
| 676 |
|
log_error("hash_dict_create(hash_dict_sockaddr_count) error"); |
| 677 |
|
return -1; |
| 678 |
} |
} |
| 679 |
|
|
| 680 |
// Startup complete |
// Startup complete |
| 681 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 682 |
sd_notifyf(0, "READY=1\n" |
sd_notifyf(0, "READY=1\n" |
| 683 |
"STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n" |
"STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n" |
| 684 |
"MAINPID=%d", |
"MAINPID=%d", |
| 685 |
hostaddr, port[0], hostaddr, port[1], getpid()); |
hostaddr, port[0], hostaddr, port[1], getpid()); |
| 686 |
|
#endif |
| 687 |
|
|
| 688 |
while (!SYS_server_exit || SYS_child_process_count > 0) |
while (!SYS_server_exit || SYS_child_process_count > 0) |
| 689 |
{ |
{ |
| 690 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 691 |
if (SYS_server_exit && !sd_notify_stopping) |
if (SYS_server_exit && !sd_notify_stopping) |
| 692 |
{ |
{ |
| 693 |
sd_notify(0, "STOPPING=1"); |
sd_notify(0, "STOPPING=1"); |
| 694 |
sd_notify_stopping = 1; |
sd_notify_stopping = 1; |
| 695 |
} |
} |
| 696 |
|
#endif |
| 697 |
|
|
| 698 |
while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) |
while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) |
| 699 |
{ |
{ |
| 700 |
SYS_child_exit = 0; |
SYS_child_exit = 0; |
| 701 |
|
|
| 702 |
siginfo.si_pid = 0; |
pid = waitpid(-1, &ret, WNOHANG); |
| 703 |
ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG); |
if (pid > 0) |
|
if (ret == 0 && siginfo.si_pid > 0) |
|
| 704 |
{ |
{ |
| 705 |
SYS_child_exit = 1; // Retry waitid |
SYS_child_exit = 1; // Retry waitid |
|
|
|
| 706 |
SYS_child_process_count--; |
SYS_child_process_count--; |
|
log_common("Child process (%d) exited\n", siginfo.si_pid); |
|
| 707 |
|
|
| 708 |
if (siginfo.si_pid != section_list_loader_pid) |
if (WIFEXITED(ret)) |
| 709 |
|
{ |
| 710 |
|
log_common("Child process (%d) exited, status=%d", pid, WEXITSTATUS(ret)); |
| 711 |
|
} |
| 712 |
|
else if (WIFSIGNALED(ret)) |
| 713 |
|
{ |
| 714 |
|
log_common("Child process (%d) is killed, status=%d", pid, WTERMSIG(ret)); |
| 715 |
|
} |
| 716 |
|
else |
| 717 |
|
{ |
| 718 |
|
log_common("Child process (%d) exited abnormally, status=%d", pid, ret); |
| 719 |
|
} |
| 720 |
|
|
| 721 |
|
if (pid != section_list_loader_pid) |
| 722 |
{ |
{ |
| 723 |
i = 0; |
int64_t j = 0; |
| 724 |
for (; i < BBS_max_client; i++) |
ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)pid, &j); |
| 725 |
|
if (ret < 0) |
| 726 |
{ |
{ |
| 727 |
if (process_sockaddr_pool[i].pid == siginfo.si_pid) |
log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error", pid); |
|
{ |
|
|
process_sockaddr_pool[i].pid = 0; |
|
|
break; |
|
|
} |
|
| 728 |
} |
} |
| 729 |
if (i >= BBS_max_client) |
else |
| 730 |
{ |
{ |
| 731 |
log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid); |
ret = hash_dict_inc(hash_dict_sockaddr_count, (in_addr_t)j, -1); |
| 732 |
|
if (ret <= 0) |
| 733 |
|
{ |
| 734 |
|
log_error("hash_dict_inc(hash_dict_sockaddr_count, %lu, -1) error: %d", (in_addr_t)j, ret); |
| 735 |
|
} |
| 736 |
|
|
| 737 |
|
ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)pid); |
| 738 |
|
if (ret < 0) |
| 739 |
|
{ |
| 740 |
|
log_error("hash_dict_del(hash_dict_pid_sockaddr, %lu) error", (uint64_t)pid); |
| 741 |
|
} |
| 742 |
} |
} |
| 743 |
} |
} |
| 744 |
} |
} |
| 745 |
else if (ret == 0) |
else if (pid == 0) |
| 746 |
{ |
{ |
| 747 |
break; |
break; |
| 748 |
} |
} |
| 749 |
else if (ret < 0) |
else if (pid < 0) |
| 750 |
{ |
{ |
| 751 |
log_error("Error in waitid: %d\n", errno); |
log_error("Error in waitpid(): %d", errno); |
| 752 |
break; |
break; |
| 753 |
} |
} |
| 754 |
} |
} |
| 757 |
{ |
{ |
| 758 |
if (notify_child_exit == 0) |
if (notify_child_exit == 0) |
| 759 |
{ |
{ |
| 760 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 761 |
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); |
| 762 |
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); |
| 763 |
|
#endif |
| 764 |
|
|
| 765 |
if (kill(0, SIGTERM) < 0) |
if (kill(0, SIGTERM) < 0) |
| 766 |
{ |
{ |
| 767 |
log_error("Send SIGTERM signal failed (%d)\n", errno); |
log_error("Send SIGTERM signal failed (%d)", errno); |
| 768 |
} |
} |
| 769 |
|
|
| 770 |
notify_child_exit = 1; |
notify_child_exit = 1; |
| 772 |
} |
} |
| 773 |
else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT) |
else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT) |
| 774 |
{ |
{ |
| 775 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 776 |
sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count); |
sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count); |
| 777 |
|
#endif |
| 778 |
|
|
| 779 |
for (i = 0; i < BBS_max_client; i++) |
if (kill(0, SIGKILL) < 0) |
| 780 |
{ |
{ |
| 781 |
if (process_sockaddr_pool[i].pid != 0) |
log_error("Send SIGKILL signal failed (%d)", errno); |
|
{ |
|
|
log_error("Kill child process (pid=%d)\n", process_sockaddr_pool[i].pid); |
|
|
if (kill(process_sockaddr_pool[i].pid, SIGKILL) < 0) |
|
|
{ |
|
|
log_error("Send SIGKILL signal failed (%d)\n", errno); |
|
|
} |
|
|
} |
|
| 782 |
} |
} |
| 783 |
|
|
| 784 |
notify_child_exit = 2; |
notify_child_exit = 2; |
| 786 |
} |
} |
| 787 |
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) |
| 788 |
{ |
{ |
| 789 |
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); |
| 790 |
SYS_child_process_count = 0; |
SYS_child_process_count = 0; |
| 791 |
} |
} |
| 792 |
} |
} |
| 794 |
if (SYS_conf_reload && !SYS_server_exit) |
if (SYS_conf_reload && !SYS_server_exit) |
| 795 |
{ |
{ |
| 796 |
SYS_conf_reload = 0; |
SYS_conf_reload = 0; |
| 797 |
|
|
| 798 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 799 |
sd_notify(0, "RELOADING=1"); |
sd_notify(0, "RELOADING=1"); |
| 800 |
|
#endif |
| 801 |
|
|
| 802 |
|
log_common("Reload configuration"); |
| 803 |
|
|
| 804 |
|
// Restart log |
| 805 |
|
if (log_restart() < 0) |
| 806 |
|
{ |
| 807 |
|
log_error("Restart logging failed"); |
| 808 |
|
} |
| 809 |
|
|
| 810 |
// Reload configuration |
// Reload configuration |
| 811 |
if (load_conf(CONF_BBSD) < 0) |
if (load_conf(CONF_BBSD) < 0) |
| 812 |
{ |
{ |
| 813 |
log_error("Reload conf failed\n"); |
log_error("Reload conf failed"); |
| 814 |
|
} |
| 815 |
|
|
| 816 |
|
// Reload BWF config |
| 817 |
|
if (bwf_load(CONF_BWF) < 0) |
| 818 |
|
{ |
| 819 |
|
log_error("Reload BWF conf failed"); |
| 820 |
} |
} |
| 821 |
|
|
| 822 |
if (load_menu(&bbs_menu_new, CONF_MENU) < 0) |
// Get EULA modification tm |
| 823 |
|
if (stat(DATA_EULA, &file_stat) == -1) |
| 824 |
{ |
{ |
| 825 |
unload_menu(&bbs_menu_new); |
log_error("stat(%s) error", DATA_EULA, errno); |
|
log_error("Reload bbs menu failed\n"); |
|
| 826 |
} |
} |
| 827 |
else |
else |
| 828 |
{ |
{ |
| 829 |
|
BBS_eula_tm = file_stat.st_mtim.tv_sec; |
| 830 |
|
} |
| 831 |
|
|
| 832 |
|
if (detach_menu_shm(&bbs_menu) < 0) |
| 833 |
|
{ |
| 834 |
|
log_error("detach_menu_shm(bbs_menu) error"); |
| 835 |
|
} |
| 836 |
|
if (load_menu(&bbs_menu, CONF_MENU) < 0) |
| 837 |
|
{ |
| 838 |
|
log_error("load_menu(bbs_menu) error"); |
| 839 |
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"); |
|
| 840 |
} |
} |
| 841 |
|
|
| 842 |
if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0) |
if (detach_menu_shm(&top10_menu) < 0) |
| 843 |
{ |
{ |
| 844 |
unload_menu(&top10_menu_new); |
log_error("detach_menu_shm(top10_menu) error"); |
|
log_error("Reload top10 menu failed\n"); |
|
| 845 |
} |
} |
| 846 |
else |
if (load_menu(&top10_menu, CONF_TOP10_MENU) < 0) |
| 847 |
{ |
{ |
| 848 |
|
log_error("load_menu(top10_menu) error"); |
| 849 |
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"); |
|
| 850 |
} |
} |
| 851 |
|
top10_menu.allow_exit = 1; |
| 852 |
|
|
| 853 |
for (int i = 0; i < data_files_load_startup_count; i++) |
for (int i = 0; i < data_files_load_startup_count; i++) |
| 854 |
{ |
{ |
| 855 |
if (load_file(data_files_load_startup[i]) < 0) |
if (load_file(data_files_load_startup[i]) < 0) |
| 856 |
{ |
{ |
| 857 |
log_error("load_file(%s) error\n", data_files_load_startup[i]); |
log_error("load_file(%s) error", data_files_load_startup[i]); |
| 858 |
} |
} |
| 859 |
} |
} |
|
log_common("Reload data files successfully\n"); |
|
| 860 |
|
|
| 861 |
// Load section config and gen_ex |
// Load section config and gen_ex |
| 862 |
if (load_section_config_from_db(1) < 0) |
if (load_section_config_from_db(1) < 0) |
| 863 |
{ |
{ |
| 864 |
log_error("load_section_config_from_db(1) error\n"); |
log_error("load_section_config_from_db(1) error"); |
| 865 |
} |
} |
| 866 |
else |
|
| 867 |
|
// Notify child processes to reload configuration |
| 868 |
|
if (kill(0, SIGUSR1) < 0) |
| 869 |
{ |
{ |
| 870 |
log_common("Reload section config and gen_ex successfully\n"); |
log_error("Send SIGUSR1 signal failed (%d)", errno); |
| 871 |
} |
} |
| 872 |
|
|
| 873 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 874 |
sd_notify(0, "READY=1"); |
sd_notify(0, "READY=1"); |
| 875 |
|
#endif |
| 876 |
} |
} |
| 877 |
|
|
| 878 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
#ifdef HAVE_SYS_EPOLL_H |
| 879 |
|
nfds = epoll_wait(epollfd_server, events, MAX_EVENTS, 100); // 0.1 second |
| 880 |
if (nfds < 0) |
ret = nfds; |
| 881 |
|
#else |
| 882 |
|
pfds[0].fd = socket_server[0]; |
| 883 |
|
pfds[0].events = POLLIN; |
| 884 |
|
pfds[1].fd = socket_server[1]; |
| 885 |
|
pfds[1].events = POLLIN; |
| 886 |
|
nfds = 2; |
| 887 |
|
ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second |
| 888 |
|
#endif |
| 889 |
|
if (ret < 0) |
| 890 |
{ |
{ |
| 891 |
if (errno != EINTR) |
if (errno != EINTR) |
| 892 |
{ |
{ |
| 893 |
log_error("epoll_wait() error (%d)\n", errno); |
#ifdef HAVE_SYS_EPOLL_H |
| 894 |
|
log_error("epoll_wait() error (%d)", errno); |
| 895 |
|
#else |
| 896 |
|
log_error("poll() error (%d)", errno); |
| 897 |
|
#endif |
| 898 |
break; |
break; |
| 899 |
} |
} |
| 900 |
continue; |
continue; |
| 908 |
|
|
| 909 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 910 |
{ |
{ |
| 911 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 912 |
if (events[i].data.fd == socket_server[0] || events[i].data.fd == socket_server[1]) |
if (events[i].data.fd == socket_server[0] || events[i].data.fd == socket_server[1]) |
| 913 |
|
#else |
| 914 |
|
if ((pfds[i].fd == socket_server[0] || pfds[i].fd == socket_server[1]) && (pfds[i].revents & POLLIN)) |
| 915 |
|
#endif |
| 916 |
{ |
{ |
| 917 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 918 |
SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0); |
SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0); |
| 919 |
|
#else |
| 920 |
|
SSH_v2 = (pfds[i].fd == socket_server[1] ? 1 : 0); |
| 921 |
|
#endif |
| 922 |
|
|
| 923 |
while (!SYS_server_exit) // Accept all incoming connections until error |
while (!SYS_server_exit) // Accept all incoming connections until error |
| 924 |
{ |
{ |
| 925 |
addrlen = sizeof(sin); |
addrlen = sizeof(sin); |
| 926 |
socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, &addrlen); |
socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, (socklen_t *)&addrlen); |
| 927 |
if (socket_client < 0) |
if (socket_client < 0) |
| 928 |
{ |
{ |
| 929 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 936 |
} |
} |
| 937 |
else |
else |
| 938 |
{ |
{ |
| 939 |
log_error("accept(socket_server) error (%d)\n", errno); |
log_error("accept(socket_server) error (%d)", errno); |
| 940 |
break; |
break; |
| 941 |
} |
} |
| 942 |
} |
} |
| 943 |
|
|
| 944 |
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) |
| 945 |
hostaddr_client[sizeof(hostaddr_client) - 1] = '\0'; |
{ |
| 946 |
|
log_error("inet_ntop() error (%d)", errno); |
| 947 |
|
close(socket_client); |
| 948 |
|
break; |
| 949 |
|
} |
| 950 |
port_client = ntohs(sin.sin_port); |
port_client = ntohs(sin.sin_port); |
| 951 |
|
|
|
log_common("Accept %s connection from %s:%d\n", (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client); |
|
|
|
|
| 952 |
if (SYS_child_process_count - 1 < BBS_max_client) |
if (SYS_child_process_count - 1 < BBS_max_client) |
| 953 |
{ |
{ |
| 954 |
j = 0; |
int64_t j = 0; |
| 955 |
for (i = 0; i < BBS_max_client; i++) |
ret = hash_dict_get(hash_dict_sockaddr_count, sin.sin_addr.s_addr, &j); |
| 956 |
|
if (ret < 0) |
| 957 |
{ |
{ |
| 958 |
if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr) |
log_error("hash_dict_get(hash_dict_sockaddr_count, %s) error", hostaddr_client); |
|
{ |
|
|
j++; |
|
|
if (j >= BBS_max_client_per_ip) |
|
|
{ |
|
|
log_common("Too many client connections (%d) from %s\n", j, hostaddr_client); |
|
|
break; |
|
|
} |
|
|
} |
|
| 959 |
} |
} |
| 960 |
|
|
| 961 |
if (j < BBS_max_client_per_ip) |
if (j < BBS_max_client_per_ip) |
| 962 |
{ |
{ |
| 963 |
if ((pid = fork_server()) < 0) |
if ((pid = fork_server()) < 0) |
| 964 |
{ |
{ |
| 965 |
log_error("fork_server() error\n"); |
log_error("fork_server() error"); |
| 966 |
} |
} |
| 967 |
else if (pid > 0) |
else if (pid > 0) |
| 968 |
{ |
{ |
| 969 |
i = 0; |
ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr); |
| 970 |
for (; i < BBS_max_client; i++) |
if (ret < 0) |
| 971 |
{ |
{ |
| 972 |
if (process_sockaddr_pool[i].pid == 0) |
log_error("hash_dict_set(hash_dict_pid_sockaddr, %lu, %s) error", (uint64_t)pid, hostaddr_client); |
|
{ |
|
|
break; |
|
|
} |
|
| 973 |
} |
} |
| 974 |
|
|
| 975 |
if (i >= BBS_max_client) |
if (j == 0) |
| 976 |
{ |
{ |
| 977 |
log_error("Process sockaddr pool depleted\n"); |
// First connection from this IP |
| 978 |
|
log_common("Accept %s connection from %s:%d", |
| 979 |
|
(SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client); |
| 980 |
|
|
| 981 |
|
ret = hash_dict_set(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1); |
| 982 |
|
if (ret < 0) |
| 983 |
|
{ |
| 984 |
|
log_error("hash_dict_set(hash_dict_sockaddr_count, %s, 1) error", hostaddr_client); |
| 985 |
|
} |
| 986 |
} |
} |
| 987 |
else |
else |
| 988 |
{ |
{ |
| 989 |
process_sockaddr_pool[i].pid = pid; |
// Increase connection count from this IP |
| 990 |
process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr; |
log_common("Accept %s connection from %s:%d, already have %d connections", |
| 991 |
|
(SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, j); |
| 992 |
|
|
| 993 |
|
ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1); |
| 994 |
|
if (ret <= 0) |
| 995 |
|
{ |
| 996 |
|
log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, 1) error: %d", hostaddr_client, ret); |
| 997 |
|
} |
| 998 |
} |
} |
| 999 |
} |
} |
| 1000 |
} |
} |
| 1001 |
|
else |
| 1002 |
|
{ |
| 1003 |
|
log_error("Rejected %s connection from %s:%d over limit per IP (%d >= %d)", |
| 1004 |
|
(SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, j, BBS_max_client_per_ip); |
| 1005 |
|
} |
| 1006 |
} |
} |
| 1007 |
else |
else |
| 1008 |
{ |
{ |
| 1009 |
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)", |
| 1010 |
|
(SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, SYS_child_process_count - 1, BBS_max_client); |
| 1011 |
} |
} |
| 1012 |
|
|
| 1013 |
if (close(socket_client) == -1) |
if (close(socket_client) == -1) |
| 1014 |
{ |
{ |
| 1015 |
log_error("close(socket_lient) error (%d)\n", errno); |
log_error("close(socket_lient) error (%d)", errno); |
| 1016 |
} |
} |
| 1017 |
} |
} |
| 1018 |
} |
} |
| 1019 |
} |
} |
| 1020 |
} |
} |
| 1021 |
|
|
| 1022 |
if (close(epollfd) < 0) |
#ifdef HAVE_SYS_EPOLL_H |
| 1023 |
|
if (close(epollfd_server) < 0) |
| 1024 |
{ |
{ |
| 1025 |
log_error("close(epoll) error (%d)\n"); |
log_error("close(epollfd_server) error (%d)"); |
| 1026 |
} |
} |
| 1027 |
|
#endif |
| 1028 |
|
|
| 1029 |
for (i = 0; i < 2; i++) |
for (int i = 0; i < 2; i++) |
| 1030 |
{ |
{ |
|
fcntl(socket_server[i], F_SETFL, flags[i]); |
|
|
|
|
| 1031 |
if (close(socket_server[i]) == -1) |
if (close(socket_server[i]) == -1) |
| 1032 |
{ |
{ |
| 1033 |
log_error("Close server socket failed\n"); |
log_error("Close server socket failed"); |
| 1034 |
} |
} |
| 1035 |
} |
} |
| 1036 |
|
|
| 1037 |
|
hash_dict_destroy(hash_dict_pid_sockaddr); |
| 1038 |
|
hash_dict_destroy(hash_dict_sockaddr_count); |
| 1039 |
|
|
| 1040 |
ssh_bind_free(sshbind); |
ssh_bind_free(sshbind); |
| 1041 |
ssh_finalize(); |
ssh_finalize(); |
| 1042 |
|
|