| 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 |
|
|
| 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) |
| 391 |
ret = ssh_event_dopoll(event, 100); // 0.1 second |
ret = ssh_event_dopoll(event, 100); // 0.1 second |
| 392 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 393 |
{ |
{ |
| 394 |
#ifdef _DEBUG |
log_debug("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session)); |
|
log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session)); |
|
|
#endif |
|
| 395 |
goto cleanup; |
goto cleanup; |
| 396 |
} |
} |
| 397 |
} |
} |
| 426 |
log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session)); |
log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session)); |
| 427 |
goto cleanup; |
goto cleanup; |
| 428 |
} |
} |
| 429 |
|
|
| 430 |
|
ssh_set_blocking(SSH_session, 0); |
| 431 |
} |
} |
| 432 |
|
|
| 433 |
// Redirect Input |
// Redirect Input |
| 452 |
|
|
| 453 |
SYS_child_process_count = 0; |
SYS_child_process_count = 0; |
| 454 |
|
|
| 455 |
#ifdef __MSYS__ |
// BWF compile |
| 456 |
// Load BWF config |
if (bwf_compile() < 0) |
|
if (bwf_load(CONF_BWF) < 0) |
|
| 457 |
{ |
{ |
| 458 |
log_error("bwf_load() error\n"); |
log_error("bwf_compile() error\n"); |
| 459 |
goto cleanup; |
goto cleanup; |
| 460 |
} |
} |
|
#endif |
|
| 461 |
|
|
| 462 |
bbs_main(); |
bbs_main(); |
| 463 |
|
|
| 495 |
ssh_free(SSH_session); |
ssh_free(SSH_session); |
| 496 |
ssh_finalize(); |
ssh_finalize(); |
| 497 |
|
|
| 498 |
#ifdef __MSYS__ |
// BWF cleanup |
| 499 |
// Cleanup BWF |
bwf_cleanup(); |
|
bwf_unload(); |
|
|
#endif |
|
| 500 |
|
|
| 501 |
// Close Input and Output for client |
// Close Input and Output for client |
| 502 |
io_cleanup(); |
io_cleanup(); |
| 513 |
|
|
| 514 |
int net_server(const char *hostaddr, in_port_t port[]) |
int net_server(const char *hostaddr, in_port_t port[]) |
| 515 |
{ |
{ |
| 516 |
|
struct stat file_stat; |
| 517 |
unsigned int addrlen; |
unsigned int addrlen; |
| 518 |
int ret; |
int ret; |
| 519 |
int flags_server[2]; |
int flags_server[2]; |
| 528 |
int nfds; |
int nfds; |
| 529 |
int notify_child_exit = 0; |
int notify_child_exit = 0; |
| 530 |
time_t tm_notify_child_exit = time(NULL); |
time_t tm_notify_child_exit = time(NULL); |
|
int i, j; |
|
| 531 |
pid_t pid; |
pid_t pid; |
| 532 |
|
int ssh_key_valid = 0; |
| 533 |
int ssh_log_level = SSH_LOG_NOLOG; |
int ssh_log_level = SSH_LOG_NOLOG; |
| 534 |
|
|
| 535 |
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 540 |
|
|
| 541 |
sshbind = ssh_bind_new(); |
sshbind = ssh_bind_new(); |
| 542 |
|
|
| 543 |
|
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_RSA_KEY_FILE) < 0) |
| 544 |
|
{ |
| 545 |
|
log_error("Error loading SSH RSA key: %s\n", SSH_HOST_RSA_KEY_FILE); |
| 546 |
|
} |
| 547 |
|
else |
| 548 |
|
{ |
| 549 |
|
ssh_key_valid = 1; |
| 550 |
|
} |
| 551 |
|
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ED25519_KEY_FILE) < 0) |
| 552 |
|
{ |
| 553 |
|
log_error("Error loading SSH ED25519 key: %s\n", SSH_HOST_ED25519_KEY_FILE); |
| 554 |
|
} |
| 555 |
|
else |
| 556 |
|
{ |
| 557 |
|
ssh_key_valid = 1; |
| 558 |
|
} |
| 559 |
|
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ECDSA_KEY_FILE) < 0) |
| 560 |
|
{ |
| 561 |
|
log_error("Error loading SSH ECDSA key: %s\n", SSH_HOST_ECDSA_KEY_FILE); |
| 562 |
|
} |
| 563 |
|
else |
| 564 |
|
{ |
| 565 |
|
ssh_key_valid = 1; |
| 566 |
|
} |
| 567 |
|
|
| 568 |
|
if (!ssh_key_valid) |
| 569 |
|
{ |
| 570 |
|
log_error("Error: no valid SSH host key\n"); |
| 571 |
|
ssh_bind_free(sshbind); |
| 572 |
|
return -1; |
| 573 |
|
} |
| 574 |
|
|
| 575 |
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 || |
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 || |
| 576 |
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 || |
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 || |
| 577 |
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 || |
|
| 578 |
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) |
| 579 |
{ |
{ |
| 580 |
log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind)); |
log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind)); |
| 592 |
#endif |
#endif |
| 593 |
|
|
| 594 |
// Server socket |
// Server socket |
| 595 |
for (i = 0; i < 2; i++) |
for (int i = 0; i < 2; i++) |
| 596 |
{ |
{ |
| 597 |
socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 598 |
|
|
| 652 |
fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK); |
fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK); |
| 653 |
} |
} |
| 654 |
|
|
| 655 |
|
ssh_bind_set_blocking(sshbind, 0); |
| 656 |
|
|
| 657 |
hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT); |
hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT); |
| 658 |
if (hash_dict_pid_sockaddr == NULL) |
if (hash_dict_pid_sockaddr == NULL) |
| 659 |
{ |
{ |
| 710 |
|
|
| 711 |
if (pid != section_list_loader_pid) |
if (pid != section_list_loader_pid) |
| 712 |
{ |
{ |
| 713 |
j = 0; |
int64_t j = 0; |
| 714 |
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); |
| 715 |
if (ret < 0) |
if (ret < 0) |
| 716 |
{ |
{ |
| 717 |
log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", pid); |
log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", pid); |
| 718 |
} |
} |
| 719 |
else |
else |
| 720 |
{ |
{ |
| 721 |
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); |
| 722 |
if (ret < 0) |
if (ret <= 0) |
| 723 |
{ |
{ |
| 724 |
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\n", (in_addr_t)j, ret); |
| 725 |
} |
} |
| 726 |
|
|
| 727 |
ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)pid); |
ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)pid); |
| 728 |
if (ret < 0) |
if (ret < 0) |
| 729 |
{ |
{ |
| 730 |
log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", pid); |
log_error("hash_dict_del(hash_dict_pid_sockaddr, %lu) error\n", (uint64_t)pid); |
| 731 |
} |
} |
| 732 |
} |
} |
| 733 |
} |
} |
| 801 |
log_error("Reload conf failed\n"); |
log_error("Reload conf failed\n"); |
| 802 |
} |
} |
| 803 |
|
|
|
#ifndef __MSYS__ |
|
| 804 |
// Reload BWF config |
// Reload BWF config |
| 805 |
if (bwf_load(CONF_BWF) < 0) |
if (bwf_load(CONF_BWF) < 0) |
| 806 |
{ |
{ |
| 807 |
log_error("Reload BWF conf failed\n"); |
log_error("Reload BWF conf failed\n"); |
| 808 |
} |
} |
| 809 |
#endif |
|
| 810 |
|
// Get EULA modification tm |
| 811 |
|
if (stat(DATA_EULA, &file_stat) == -1) |
| 812 |
|
{ |
| 813 |
|
log_error("stat(%s) error\n", DATA_EULA, errno); |
| 814 |
|
} |
| 815 |
|
else |
| 816 |
|
{ |
| 817 |
|
BBS_eula_tm = file_stat.st_mtim.tv_sec; |
| 818 |
|
} |
| 819 |
|
|
| 820 |
if (detach_menu_shm(&bbs_menu) < 0) |
if (detach_menu_shm(&bbs_menu) < 0) |
| 821 |
{ |
{ |
| 836 |
log_error("load_menu(top10_menu) error\n"); |
log_error("load_menu(top10_menu) error\n"); |
| 837 |
unload_menu(&top10_menu); |
unload_menu(&top10_menu); |
| 838 |
} |
} |
| 839 |
|
top10_menu.allow_exit = 1; |
| 840 |
|
|
| 841 |
for (int i = 0; i < data_files_load_startup_count; i++) |
for (int i = 0; i < data_files_load_startup_count; i++) |
| 842 |
{ |
{ |
| 934 |
|
|
| 935 |
port_client = ntohs(sin.sin_port); |
port_client = ntohs(sin.sin_port); |
| 936 |
|
|
|
log_common("Accept %s connection from %s:%d\n", (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client); |
|
|
|
|
| 937 |
if (SYS_child_process_count - 1 < BBS_max_client) |
if (SYS_child_process_count - 1 < BBS_max_client) |
| 938 |
{ |
{ |
| 939 |
j = 0; |
int64_t j = 0; |
| 940 |
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); |
| 941 |
if (ret < 0) |
if (ret < 0) |
| 942 |
{ |
{ |
| 943 |
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\n", hostaddr_client); |
| 954 |
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); |
| 955 |
if (ret < 0) |
if (ret < 0) |
| 956 |
{ |
{ |
| 957 |
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\n", (uint64_t)pid, hostaddr_client); |
| 958 |
} |
} |
| 959 |
|
|
| 960 |
ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1); |
if (j == 0) |
|
if (ret < 0) |
|
| 961 |
{ |
{ |
| 962 |
log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, 1); |
// First connection from this IP |
| 963 |
|
log_common("Accept %s connection from %s:%d\n", |
| 964 |
|
(SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client); |
| 965 |
|
|
| 966 |
|
ret = hash_dict_set(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1); |
| 967 |
|
if (ret < 0) |
| 968 |
|
{ |
| 969 |
|
log_error("hash_dict_set(hash_dict_sockaddr_count, %s, 1) error\n", hostaddr_client); |
| 970 |
|
} |
| 971 |
|
} |
| 972 |
|
else |
| 973 |
|
{ |
| 974 |
|
// Increase connection count from this IP |
| 975 |
|
log_common("Accept %s connection from %s:%d, already have %d connections\n", |
| 976 |
|
(SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, j); |
| 977 |
|
|
| 978 |
|
ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1); |
| 979 |
|
if (ret <= 0) |
| 980 |
|
{ |
| 981 |
|
log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, 1) error: %d\n", hostaddr_client, ret); |
| 982 |
|
} |
| 983 |
} |
} |
| 984 |
} |
} |
| 985 |
} |
} |
| 986 |
else |
else |
| 987 |
{ |
{ |
| 988 |
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)\n", |
| 989 |
|
(SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, j, BBS_max_client_per_ip); |
| 990 |
} |
} |
| 991 |
} |
} |
| 992 |
else |
else |
| 993 |
{ |
{ |
| 994 |
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)\n", |
| 995 |
|
(SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, SYS_child_process_count - 1, BBS_max_client); |
| 996 |
} |
} |
| 997 |
|
|
| 998 |
if (close(socket_client) == -1) |
if (close(socket_client) == -1) |
| 1011 |
} |
} |
| 1012 |
#endif |
#endif |
| 1013 |
|
|
| 1014 |
for (i = 0; i < 2; i++) |
for (int i = 0; i < 2; i++) |
| 1015 |
{ |
{ |
| 1016 |
if (close(socket_server[i]) == -1) |
if (close(socket_server[i]) == -1) |
| 1017 |
{ |
{ |