| 54 |
STATION_PER_LINE = 4, |
STATION_PER_LINE = 4, |
| 55 |
USERNAME_MAX_LEN = 20, |
USERNAME_MAX_LEN = 20, |
| 56 |
PASSWORD_MAX_LEN = 20, |
PASSWORD_MAX_LEN = 20, |
| 57 |
|
SSH_CONNECT_TIMEOUT = 5, // seconds |
| 58 |
}; |
}; |
| 59 |
|
|
| 60 |
struct _bbsnet_conf |
struct _bbsnet_conf |
| 280 |
char local_addr[IP_ADDR_LEN]; |
char local_addr[IP_ADDR_LEN]; |
| 281 |
int local_port; |
int local_port; |
| 282 |
socklen_t sock_len; |
socklen_t sock_len; |
| 283 |
|
time_t t_begin; |
| 284 |
time_t t_used = time(NULL); |
time_t t_used = time(NULL); |
| 285 |
struct tm *tm_used; |
struct tm *tm_used; |
| 286 |
int ch; |
int ch; |
| 290 |
ssh_channel channel = NULL; |
ssh_channel channel = NULL; |
| 291 |
int ssh_process_config = 0; |
int ssh_process_config = 0; |
| 292 |
int ssh_log_level = SSH_LOG_NOLOG; |
int ssh_log_level = SSH_LOG_NOLOG; |
|
long ssh_timeout = 0; |
|
| 293 |
|
|
| 294 |
if (user_online_update("BBS_NET") < 0) |
if (user_online_update("BBS_NET") < 0) |
| 295 |
{ |
{ |
| 588 |
goto cleanup; |
goto cleanup; |
| 589 |
} |
} |
| 590 |
|
|
| 591 |
ssh_timeout = 5; // second |
ssh_set_blocking(session, 0); |
|
if (ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) |
|
|
{ |
|
|
log_error("Error setting SSH options: %s\n", ssh_get_error(session)); |
|
|
goto cleanup; |
|
|
} |
|
| 592 |
|
|
| 593 |
while (!SYS_server_exit) |
t_begin = time(NULL); |
| 594 |
|
ret = SSH_ERROR; |
| 595 |
|
while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) |
| 596 |
{ |
{ |
| 597 |
ret = ssh_connect(session); |
ret = ssh_connect(session); |
| 598 |
if (ret == SSH_OK) |
if (ret == SSH_OK) |
| 599 |
{ |
{ |
| 600 |
break; |
break; |
| 601 |
} |
} |
| 602 |
else if (ret == SSH_ERROR) |
else if (ret == SSH_AGAIN) |
| 603 |
|
{ |
| 604 |
|
// log_error("ssh_connect() error: SSH_AGAIN\n"); |
| 605 |
|
} |
| 606 |
|
else // if (ret == SSH_ERROR) |
| 607 |
{ |
{ |
| 608 |
log_error("ssh_connect() error\n"); |
log_error("ssh_connect() error: SSH_ERROR\n"); |
| 609 |
goto cleanup; |
goto cleanup; |
| 610 |
} |
} |
| 611 |
} |
} |
| 612 |
|
if (ret != SSH_OK) |
| 613 |
|
{ |
| 614 |
|
prints("\033[1;31m连接超时!\033[m\r\n"); |
| 615 |
|
press_any_key(); |
| 616 |
|
goto cleanup; |
| 617 |
|
} |
| 618 |
|
|
| 619 |
ret = ssh_session_is_known_server(session); |
ret = ssh_session_is_known_server(session); |
| 620 |
switch (ret) |
switch (ret) |
| 624 |
if (ssh_session_update_known_hosts(session) != SSH_OK) |
if (ssh_session_update_known_hosts(session) != SSH_OK) |
| 625 |
{ |
{ |
| 626 |
log_error("ssh_session_update_known_hosts(%s) error\n", bbsnet_conf[n].host_name); |
log_error("ssh_session_update_known_hosts(%s) error\n", bbsnet_conf[n].host_name); |
| 627 |
prints("\033[1;31m无法添加服务器证书\033[m"); |
prints("\033[1;31m无法添加服务器证书\033[m\r\n"); |
| 628 |
press_any_key(); |
press_any_key(); |
| 629 |
goto cleanup; |
goto cleanup; |
| 630 |
} |
} |
| 634 |
case SSH_KNOWN_HOSTS_CHANGED: |
case SSH_KNOWN_HOSTS_CHANGED: |
| 635 |
case SSH_KNOWN_HOSTS_OTHER: |
case SSH_KNOWN_HOSTS_OTHER: |
| 636 |
log_error("ssh_session_is_known_server(%s) error: %d\n", bbsnet_conf[n].host_name, ret); |
log_error("ssh_session_is_known_server(%s) error: %d\n", bbsnet_conf[n].host_name, ret); |
| 637 |
prints("\033[1;31m服务器证书已变更\033[m"); |
prints("\033[1;31m服务器证书已变更\033[m\r\n"); |
| 638 |
press_any_key(); |
press_any_key(); |
| 639 |
goto cleanup; |
goto cleanup; |
| 640 |
} |
} |
| 641 |
|
|
| 642 |
for (int i = 0; !SYS_server_exit;) |
ret = SSH_AUTH_ERROR; |
| 643 |
|
while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) |
| 644 |
{ |
{ |
| 645 |
ret = ssh_userauth_password(session, NULL, remote_pass); |
ret = ssh_userauth_password(session, NULL, remote_pass); |
| 646 |
if (ret == SSH_AUTH_SUCCESS) |
if (ret == SSH_AUTH_SUCCESS) |
| 649 |
} |
} |
| 650 |
else if (ret == SSH_AUTH_AGAIN) |
else if (ret == SSH_AUTH_AGAIN) |
| 651 |
{ |
{ |
| 652 |
#ifdef _DEBUG |
// log_error("ssh_userauth_password() error: SSH_AUTH_AGAIN\n"); |
|
log_error("ssh_userauth_password() error: SSH_AUTH_AGAIN\n"); |
|
|
#endif |
|
| 653 |
} |
} |
| 654 |
else if (ret == SSH_AUTH_ERROR) |
else if (ret == SSH_AUTH_ERROR) |
| 655 |
{ |
{ |
| 656 |
log_error("ssh_userauth_password() error: %d\n", ret); |
log_error("ssh_userauth_password() error: SSH_AUTH_ERROR\n"); |
| 657 |
goto cleanup; |
goto cleanup; |
| 658 |
} |
} |
| 659 |
else // if (ret == SSH_AUTH_DENIED) |
else // if (ret == SSH_AUTH_DENIED) |
| 660 |
{ |
{ |
| 661 |
|
log_error("ssh_userauth_password() error: SSH_AUTH_DENIED\n"); |
| 662 |
prints("\033[1;31m身份验证失败!\033[m\r\n"); |
prints("\033[1;31m身份验证失败!\033[m\r\n"); |
| 663 |
i++; |
press_any_key(); |
| 664 |
if (i < BBS_login_retry_times) |
goto cleanup; |
|
{ |
|
|
prints("请输入密码: "); |
|
|
iflush(); |
|
|
if (str_input(remote_pass, sizeof(remote_pass), NOECHO) < 0) |
|
|
{ |
|
|
goto cleanup; |
|
|
} |
|
|
if (remote_pass[0] == '\0') |
|
|
{ |
|
|
goto cleanup; |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
goto cleanup; |
|
|
} |
|
| 665 |
} |
} |
| 666 |
} |
} |
| 667 |
|
if (ret != SSH_AUTH_SUCCESS) |
| 668 |
|
{ |
| 669 |
|
prints("\033[1;31m连接超时!\033[m\r\n"); |
| 670 |
|
press_any_key(); |
| 671 |
|
goto cleanup; |
| 672 |
|
} |
| 673 |
|
|
| 674 |
channel = ssh_channel_new(session); |
channel = ssh_channel_new(session); |
| 675 |
if (channel == NULL) |
if (channel == NULL) |
| 678 |
goto cleanup; |
goto cleanup; |
| 679 |
} |
} |
| 680 |
|
|
| 681 |
while (!SYS_server_exit) |
ret = SSH_ERROR; |
| 682 |
|
while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) |
| 683 |
{ |
{ |
| 684 |
ret = ssh_channel_open_session(channel); |
ret = ssh_channel_open_session(channel); |
| 685 |
if (ret == SSH_OK) |
if (ret == SSH_OK) |
| 686 |
{ |
{ |
| 687 |
break; |
break; |
| 688 |
} |
} |
| 689 |
else if (ret == SSH_ERROR) |
else if (ret == SSH_AGAIN) |
| 690 |
|
{ |
| 691 |
|
// log_error("ssh_channel_open_session() error: SSH_AGAIN\n"); |
| 692 |
|
} |
| 693 |
|
else // if (ret == SSH_ERROR) |
| 694 |
{ |
{ |
| 695 |
log_error("ssh_channel_open_session() error\n"); |
log_error("ssh_channel_open_session() error: SSH_ERROR\n"); |
| 696 |
goto cleanup; |
goto cleanup; |
| 697 |
} |
} |
| 698 |
} |
} |
| 699 |
|
if (ret != SSH_OK) |
| 700 |
|
{ |
| 701 |
|
prints("\033[1;31m连接超时!\033[m\r\n"); |
| 702 |
|
press_any_key(); |
| 703 |
|
goto cleanup; |
| 704 |
|
} |
| 705 |
|
|
| 706 |
while (!SYS_server_exit) |
ret = SSH_ERROR; |
| 707 |
|
while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) |
| 708 |
{ |
{ |
| 709 |
ret = ssh_channel_request_pty(channel); |
ret = ssh_channel_request_pty(channel); |
| 710 |
if (ret == SSH_OK) |
if (ret == SSH_OK) |
| 711 |
{ |
{ |
| 712 |
break; |
break; |
| 713 |
} |
} |
| 714 |
else if (ret == SSH_ERROR) |
else if (ret == SSH_AGAIN) |
| 715 |
|
{ |
| 716 |
|
// log_error("ssh_channel_request_pty() error: SSH_AGAIN\n"); |
| 717 |
|
} |
| 718 |
|
else // if (ret == SSH_ERROR) |
| 719 |
{ |
{ |
| 720 |
log_error("ssh_channel_request_pty() error\n"); |
log_error("ssh_channel_request_pty() error: SSH_ERROR\n"); |
| 721 |
goto cleanup; |
goto cleanup; |
| 722 |
} |
} |
| 723 |
} |
} |
| 724 |
|
if (ret != SSH_OK) |
| 725 |
|
{ |
| 726 |
|
prints("\033[1;31m连接超时!\033[m\r\n"); |
| 727 |
|
press_any_key(); |
| 728 |
|
goto cleanup; |
| 729 |
|
} |
| 730 |
|
|
| 731 |
while (!SYS_server_exit) |
ret = SSH_ERROR; |
| 732 |
|
while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) |
| 733 |
{ |
{ |
| 734 |
ret = ssh_channel_request_shell(channel); |
ret = ssh_channel_request_shell(channel); |
| 735 |
if (ret == SSH_OK) |
if (ret == SSH_OK) |
| 736 |
{ |
{ |
| 737 |
break; |
break; |
| 738 |
} |
} |
| 739 |
else if (ret == SSH_ERROR) |
else if (ret == SSH_AGAIN) |
| 740 |
|
{ |
| 741 |
|
// log_error("ssh_channel_request_shell() error: SSH_AGAIN\n"); |
| 742 |
|
} |
| 743 |
|
else // if (ret == SSH_ERROR) |
| 744 |
{ |
{ |
| 745 |
log_error("ssh_channel_request_shell() error\n"); |
log_error("ssh_channel_request_shell() error: SSH_ERROR\n"); |
| 746 |
goto cleanup; |
goto cleanup; |
| 747 |
} |
} |
| 748 |
} |
} |
| 749 |
|
if (ret != SSH_OK) |
|
ssh_timeout = 0; // second |
|
|
if (ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) |
|
| 750 |
{ |
{ |
| 751 |
log_error("Error setting SSH options: %s\n", ssh_get_error(session)); |
prints("\033[1;31m连接超时!\033[m\r\n"); |
| 752 |
|
press_any_key(); |
| 753 |
goto cleanup; |
goto cleanup; |
| 754 |
} |
} |
|
|
|
|
ssh_set_blocking(session, 0); |
|
| 755 |
} |
} |
| 756 |
|
|
| 757 |
prints("\033[1;31m连接成功!\033[m\r\n"); |
prints("\033[1;31m连接成功!\033[m\r\n"); |
| 1223 |
} |
} |
| 1224 |
|
|
| 1225 |
// Restore STDIN/STDOUT flags |
// Restore STDIN/STDOUT flags |
| 1226 |
if (flags_stdin != -1 &&fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1) |
if (flags_stdin != -1 && fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1) |
| 1227 |
{ |
{ |
| 1228 |
log_error("fcntl(F_SETFL) error (%d)\n", errno); |
log_error("fcntl(F_SETFL) error (%d)\n", errno); |
| 1229 |
} |
} |
| 1241 |
tm_used = gmtime(&t_used); |
tm_used = gmtime(&t_used); |
| 1242 |
|
|
| 1243 |
log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n", |
log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n", |
| 1244 |
tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min, |
tm_used->tm_yday, tm_used->tm_hour, tm_used->tm_min, tm_used->tm_sec); |
|
tm_used->tm_sec); |
|
| 1245 |
|
|
| 1246 |
BBS_last_access_tm = time(NULL); |
BBS_last_access_tm = time(NULL); |
| 1247 |
|
|