| 43 |
#include <sys/syscall.h> |
#include <sys/syscall.h> |
| 44 |
#include <sys/types.h> |
#include <sys/types.h> |
| 45 |
#include <sys/wait.h> |
#include <sys/wait.h> |
| 46 |
|
|
| 47 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 48 |
#include <systemd/sd-daemon.h> |
#include <systemd/sd-daemon.h> |
| 49 |
|
#endif |
| 50 |
|
|
| 51 |
enum _net_server_constant_t |
enum _net_server_constant_t |
| 52 |
{ |
{ |
| 56 |
SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds |
SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds |
| 57 |
}; |
}; |
| 58 |
|
|
|
static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server"; |
|
|
|
|
| 59 |
/* A userdata struct for session. */ |
/* A userdata struct for session. */ |
| 60 |
struct session_data_struct |
struct session_data_struct |
| 61 |
{ |
{ |
| 82 |
struct winsize *winsize; |
struct winsize *winsize; |
| 83 |
}; |
}; |
| 84 |
|
|
| 85 |
|
static int socket_server[2]; |
| 86 |
|
static int socket_client; |
| 87 |
|
static int epollfd_server = -1; |
| 88 |
|
static ssh_bind sshbind; |
| 89 |
|
|
| 90 |
|
static HASH_DICT *hash_dict_pid_sockaddr = NULL; |
| 91 |
|
static HASH_DICT *hash_dict_sockaddr_count = NULL; |
| 92 |
|
|
| 93 |
|
static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server"; |
| 94 |
|
|
| 95 |
static int auth_password(ssh_session session, const char *user, |
static int auth_password(ssh_session session, const char *user, |
| 96 |
const char *password, void *userdata) |
const char *password, void *userdata) |
| 97 |
{ |
{ |
| 318 |
} |
} |
| 319 |
|
|
| 320 |
// Child process |
// Child process |
| 321 |
|
if (close(epollfd_server) < 0) |
| 322 |
|
{ |
| 323 |
|
log_error("close(epollfd_server) error (%d)\n"); |
| 324 |
|
} |
| 325 |
|
|
| 326 |
if (close(socket_server[0]) == -1 || close(socket_server[1]) == -1) |
for (i = 0; i < 2; i++) |
| 327 |
{ |
{ |
| 328 |
log_error("Close server socket failed\n"); |
if (close(socket_server[i]) == -1) |
| 329 |
|
{ |
| 330 |
|
log_error("Close server socket failed\n"); |
| 331 |
|
} |
| 332 |
} |
} |
| 333 |
|
|
| 334 |
|
hash_dict_destroy(hash_dict_pid_sockaddr); |
| 335 |
|
hash_dict_destroy(hash_dict_sockaddr_count); |
| 336 |
|
|
| 337 |
SSH_session = ssh_new(); |
SSH_session = ssh_new(); |
| 338 |
|
|
| 339 |
if (SSH_v2) |
if (SSH_v2) |
| 427 |
goto cleanup; |
goto cleanup; |
| 428 |
} |
} |
| 429 |
|
|
| 430 |
|
if (io_init() < 0) |
| 431 |
|
{ |
| 432 |
|
log_error("io_init() error\n"); |
| 433 |
|
goto cleanup; |
| 434 |
|
} |
| 435 |
|
|
| 436 |
SYS_child_process_count = 0; |
SYS_child_process_count = 0; |
| 437 |
|
|
| 438 |
bbs_main(); |
bbs_main(); |
| 443 |
|
|
| 444 |
if (SSH_v2) |
if (SSH_v2) |
| 445 |
{ |
{ |
| 446 |
close(cdata.pty_master); |
if (cdata.pty_master != -1) |
| 447 |
close(cdata.child_stdin); |
{ |
| 448 |
close(cdata.child_stdout); |
close(cdata.pty_master); |
| 449 |
close(cdata.child_stderr); |
} |
| 450 |
|
if (cdata.child_stdin != -1) |
| 451 |
|
{ |
| 452 |
|
close(cdata.child_stdin); |
| 453 |
|
} |
| 454 |
|
if (cdata.child_stdout != -1) |
| 455 |
|
{ |
| 456 |
|
close(cdata.child_stdout); |
| 457 |
|
} |
| 458 |
|
if (cdata.child_stderr != -1) |
| 459 |
|
{ |
| 460 |
|
close(cdata.child_stderr); |
| 461 |
|
} |
| 462 |
|
|
| 463 |
ssh_channel_free(SSH_channel); |
ssh_channel_free(SSH_channel); |
| 464 |
ssh_disconnect(SSH_session); |
ssh_disconnect(SSH_session); |
| 472 |
ssh_finalize(); |
ssh_finalize(); |
| 473 |
|
|
| 474 |
// Close Input and Output for client |
// Close Input and Output for client |
| 475 |
|
io_cleanup(); |
| 476 |
close(STDIN_FILENO); |
close(STDIN_FILENO); |
| 477 |
close(STDOUT_FILENO); |
close(STDOUT_FILENO); |
| 478 |
|
|
| 486 |
|
|
| 487 |
int net_server(const char *hostaddr, in_port_t port[]) |
int net_server(const char *hostaddr, in_port_t port[]) |
| 488 |
{ |
{ |
|
HASH_DICT *hash_dict_pid_sockaddr = NULL; |
|
|
HASH_DICT *hash_dict_sockaddr_count = NULL; |
|
|
|
|
| 489 |
unsigned int addrlen; |
unsigned int addrlen; |
| 490 |
int ret; |
int ret; |
| 491 |
int flags[2]; |
int flags_server[2]; |
| 492 |
struct sockaddr_in sin; |
struct sockaddr_in sin; |
| 493 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 494 |
int nfds, epollfd; |
int nfds; |
| 495 |
siginfo_t siginfo; |
siginfo_t siginfo; |
| 496 |
int notify_child_exit = 0; |
int notify_child_exit = 0; |
| 497 |
time_t tm_notify_child_exit = time(NULL); |
time_t tm_notify_child_exit = time(NULL); |
|
int sd_notify_stopping = 0; |
|
| 498 |
MENU_SET bbs_menu_new; |
MENU_SET bbs_menu_new; |
| 499 |
MENU_SET top10_menu_new; |
MENU_SET top10_menu_new; |
| 500 |
int i, j; |
int i, j; |
| 501 |
pid_t pid; |
pid_t pid; |
| 502 |
int ssh_log_level = SSH_LOG_NOLOG; |
int ssh_log_level = SSH_LOG_NOLOG; |
| 503 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 504 |
|
int sd_notify_stopping = 0; |
| 505 |
|
#endif |
| 506 |
|
|
| 507 |
ssh_init(); |
ssh_init(); |
| 508 |
|
|
| 519 |
return -1; |
return -1; |
| 520 |
} |
} |
| 521 |
|
|
| 522 |
epollfd = epoll_create1(0); |
epollfd_server = epoll_create1(0); |
| 523 |
if (epollfd < 0) |
if (epollfd_server == -1) |
| 524 |
{ |
{ |
| 525 |
log_error("epoll_create1() error (%d)\n", errno); |
log_error("epoll_create1() error (%d)\n", errno); |
| 526 |
return -1; |
return -1; |
| 542 |
sin.sin_port = htons(port[i]); |
sin.sin_port = htons(port[i]); |
| 543 |
|
|
| 544 |
// Reuse address and port |
// Reuse address and port |
| 545 |
flags[i] = 1; |
flags_server[i] = 1; |
| 546 |
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) |
| 547 |
{ |
{ |
| 548 |
log_error("setsockopt SO_REUSEADDR error (%d)\n", errno); |
log_error("setsockopt SO_REUSEADDR error (%d)\n", errno); |
| 549 |
} |
} |
| 550 |
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags[i], sizeof(flags[i])) < 0) |
if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags_server[i], sizeof(flags_server[i])) < 0) |
| 551 |
{ |
{ |
| 552 |
log_error("setsockopt SO_REUSEPORT error (%d)\n", errno); |
log_error("setsockopt SO_REUSEPORT error (%d)\n", errno); |
| 553 |
} |
} |
| 569 |
|
|
| 570 |
ev.events = EPOLLIN; |
ev.events = EPOLLIN; |
| 571 |
ev.data.fd = socket_server[i]; |
ev.data.fd = socket_server[i]; |
| 572 |
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) |
| 573 |
{ |
{ |
| 574 |
log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno); |
log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno); |
| 575 |
if (close(epollfd) < 0) |
if (close(epollfd_server) < 0) |
| 576 |
{ |
{ |
| 577 |
log_error("close(epoll) error (%d)\n"); |
log_error("close(epoll) error (%d)\n"); |
| 578 |
} |
} |
| 579 |
return -1; |
return -1; |
| 580 |
} |
} |
| 581 |
|
|
| 582 |
flags[i] = fcntl(socket_server[i], F_GETFL, 0); |
flags_server[i] = fcntl(socket_server[i], F_GETFL, 0); |
| 583 |
fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK); |
fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK); |
| 584 |
} |
} |
| 585 |
|
|
| 586 |
hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT); |
hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT); |
| 597 |
} |
} |
| 598 |
|
|
| 599 |
// Startup complete |
// Startup complete |
| 600 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 601 |
sd_notifyf(0, "READY=1\n" |
sd_notifyf(0, "READY=1\n" |
| 602 |
"STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n" |
"STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n" |
| 603 |
"MAINPID=%d", |
"MAINPID=%d", |
| 604 |
hostaddr, port[0], hostaddr, port[1], getpid()); |
hostaddr, port[0], hostaddr, port[1], getpid()); |
| 605 |
|
#endif |
| 606 |
|
|
| 607 |
while (!SYS_server_exit || SYS_child_process_count > 0) |
while (!SYS_server_exit || SYS_child_process_count > 0) |
| 608 |
{ |
{ |
| 609 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 610 |
if (SYS_server_exit && !sd_notify_stopping) |
if (SYS_server_exit && !sd_notify_stopping) |
| 611 |
{ |
{ |
| 612 |
sd_notify(0, "STOPPING=1"); |
sd_notify(0, "STOPPING=1"); |
| 613 |
sd_notify_stopping = 1; |
sd_notify_stopping = 1; |
| 614 |
} |
} |
| 615 |
|
#endif |
| 616 |
|
|
| 617 |
while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) |
while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) |
| 618 |
{ |
{ |
| 666 |
{ |
{ |
| 667 |
if (notify_child_exit == 0) |
if (notify_child_exit == 0) |
| 668 |
{ |
{ |
| 669 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 670 |
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); |
| 671 |
log_common("Notify %d child process to exit\n", SYS_child_process_count); |
log_common("Notify %d child process to exit\n", SYS_child_process_count); |
| 672 |
|
#endif |
| 673 |
|
|
| 674 |
if (kill(-getpid(), SIGTERM) < 0) |
if (kill(-getpid(), SIGTERM) < 0) |
| 675 |
{ |
{ |
| 681 |
} |
} |
| 682 |
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) |
| 683 |
{ |
{ |
| 684 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 685 |
sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count); |
sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count); |
| 686 |
|
#endif |
| 687 |
|
|
| 688 |
if (kill(-getpid(), SIGKILL) < 0) |
if (kill(-getpid(), SIGKILL) < 0) |
| 689 |
{ |
{ |
| 703 |
if (SYS_conf_reload && !SYS_server_exit) |
if (SYS_conf_reload && !SYS_server_exit) |
| 704 |
{ |
{ |
| 705 |
SYS_conf_reload = 0; |
SYS_conf_reload = 0; |
| 706 |
|
|
| 707 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 708 |
sd_notify(0, "RELOADING=1"); |
sd_notify(0, "RELOADING=1"); |
| 709 |
|
#endif |
| 710 |
|
|
| 711 |
// Restart log |
// Restart log |
| 712 |
if (log_restart() < 0) |
if (log_restart() < 0) |
| 776 |
log_error("Send SIGUSR1 signal failed (%d)\n", errno); |
log_error("Send SIGUSR1 signal failed (%d)\n", errno); |
| 777 |
} |
} |
| 778 |
|
|
| 779 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 780 |
sd_notify(0, "READY=1"); |
sd_notify(0, "READY=1"); |
| 781 |
|
#endif |
| 782 |
} |
} |
| 783 |
|
|
| 784 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
nfds = epoll_wait(epollfd_server, events, MAX_EVENTS, 100); // 0.1 second |
| 785 |
|
|
| 786 |
if (nfds < 0) |
if (nfds < 0) |
| 787 |
{ |
{ |
| 882 |
} |
} |
| 883 |
} |
} |
| 884 |
|
|
| 885 |
if (close(epollfd) < 0) |
if (close(epollfd_server) < 0) |
| 886 |
{ |
{ |
| 887 |
log_error("close(epoll) error (%d)\n"); |
log_error("close(epollfd_server) error (%d)\n"); |
| 888 |
} |
} |
| 889 |
|
|
| 890 |
for (i = 0; i < 2; i++) |
for (i = 0; i < 2; i++) |
| 891 |
{ |
{ |
|
fcntl(socket_server[i], F_SETFL, flags[i]); |
|
|
|
|
| 892 |
if (close(socket_server[i]) == -1) |
if (close(socket_server[i]) == -1) |
| 893 |
{ |
{ |
| 894 |
log_error("Close server socket failed\n"); |
log_error("Close server socket failed\n"); |