| 3 |
* bbs_net |
* bbs_net |
| 4 |
* - user interactive feature of site shuttle |
* - user interactive feature of site shuttle |
| 5 |
* |
* |
| 6 |
* Copyright (C) 2004-2025 by 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_net.h" |
| 15 |
#include "common.h" |
#include "common.h" |
| 16 |
#include "io.h" |
#include "io.h" |
| 17 |
#include "log.h" |
#include "log.h" |
| 18 |
#include "login.h" |
#include "login.h" |
| 19 |
#include "menu.h" |
#include "menu.h" |
| 20 |
#include "screen.h" |
#include "screen.h" |
| 21 |
|
#include "str_process.h" |
| 22 |
#include <errno.h> |
#include <errno.h> |
| 23 |
#include <fcntl.h> |
#include <fcntl.h> |
| 24 |
#include <netdb.h> |
#include <netdb.h> |
| 29 |
#include <time.h> |
#include <time.h> |
| 30 |
#include <unistd.h> |
#include <unistd.h> |
| 31 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
| 32 |
|
#include <ctype.h> |
| 33 |
#include <libssh/libssh.h> |
#include <libssh/libssh.h> |
| 34 |
#include <libssh/server.h> |
#include <libssh/server.h> |
| 35 |
#include <libssh/callbacks.h> |
#include <libssh/callbacks.h> |
| 38 |
#include <sys/select.h> |
#include <sys/select.h> |
| 39 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
| 40 |
#include <sys/socket.h> |
#include <sys/socket.h> |
| 41 |
|
|
| 42 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 43 |
#include <sys/epoll.h> |
#include <sys/epoll.h> |
| 44 |
|
#else |
| 45 |
|
#include <poll.h> |
| 46 |
|
#endif |
| 47 |
|
|
| 48 |
#define MENU_CONF_DELIM " \t\r\n" |
static const char MENU_CONF_DELIM[] = " \t\r\n"; |
| 49 |
|
|
| 50 |
#define MAX_PROCESS_BAR_LEN 30 |
enum _bbs_net_constant_t |
| 51 |
#define MAXSTATION 26 * 2 |
{ |
| 52 |
#define STATION_PER_LINE 4 |
MAX_PROCESS_BAR_LEN = 30, |
| 53 |
|
MAXSTATION = 26 * 2, |
| 54 |
|
STATION_PER_LINE = 4, |
| 55 |
|
USERNAME_MAX_LEN = 20, |
| 56 |
|
PASSWORD_MAX_LEN = 20, |
| 57 |
|
}; |
| 58 |
|
|
| 59 |
struct _bbsnet_conf |
struct _bbsnet_conf |
| 60 |
{ |
{ |
| 61 |
char host1[20]; |
char org_name[40]; |
| 62 |
char host2[40]; |
char site_name[40]; |
| 63 |
char ip[40]; |
char host_name[IP_ADDR_LEN]; |
| 64 |
in_port_t port; |
in_port_t port; |
| 65 |
char charset[20]; |
int8_t use_ssh; |
| 66 |
|
char charset[CHARSET_MAX_LEN + 1]; |
| 67 |
} bbsnet_conf[MAXSTATION]; |
} bbsnet_conf[MAXSTATION]; |
| 68 |
|
|
| 69 |
static MENU_SET bbsnet_menu; |
static MENU_SET bbsnet_menu; |
| 74 |
MENU *p_menu; |
MENU *p_menu; |
| 75 |
MENU_ITEM *p_menu_item; |
MENU_ITEM *p_menu_item; |
| 76 |
MENU_ITEM_ID menu_item_id; |
MENU_ITEM_ID menu_item_id; |
| 77 |
char t[256], *t1, *t2, *t3, *t4, *t5, *saveptr; |
char line[LINE_BUFFER_LEN], *t1, *t2, *t3, *t4, *t5, *t6, *saveptr; |
| 78 |
|
|
| 79 |
fp = fopen(file_config, "r"); |
fp = fopen(file_config, "r"); |
| 80 |
if (fp == NULL) |
if (fp == NULL) |
| 106 |
p_menu->screen_show = 0; |
p_menu->screen_show = 0; |
| 107 |
|
|
| 108 |
menu_item_id = 0; |
menu_item_id = 0; |
| 109 |
while (fgets(t, 255, fp) && menu_item_id < MAXSTATION) |
while (fgets(line, sizeof(line), fp) && menu_item_id < MAXSTATION) |
| 110 |
{ |
{ |
| 111 |
t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr); |
t1 = strtok_r(line, MENU_CONF_DELIM, &saveptr); |
| 112 |
t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 113 |
t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 114 |
t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 115 |
t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 116 |
|
t6 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 117 |
|
|
| 118 |
if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t5 == NULL || t[0] == '#' || t[0] == '*') |
if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || |
| 119 |
|
t5 == NULL || t6 == NULL || line[0] == '#' || line[0] == '*') |
| 120 |
{ |
{ |
| 121 |
continue; |
continue; |
| 122 |
} |
} |
| 123 |
|
|
| 124 |
strncpy(bbsnet_conf[menu_item_id].host1, t2, sizeof(bbsnet_conf[menu_item_id].host1) - 1); |
strncpy(bbsnet_conf[menu_item_id].site_name, t2, sizeof(bbsnet_conf[menu_item_id].site_name) - 1); |
| 125 |
bbsnet_conf[menu_item_id].host1[sizeof(bbsnet_conf[menu_item_id].host1) - 1] = '\0'; |
bbsnet_conf[menu_item_id].site_name[sizeof(bbsnet_conf[menu_item_id].site_name) - 1] = '\0'; |
| 126 |
strncpy(bbsnet_conf[menu_item_id].host2, t1, sizeof(bbsnet_conf[menu_item_id].host2) - 1); |
strncpy(bbsnet_conf[menu_item_id].org_name, t1, sizeof(bbsnet_conf[menu_item_id].org_name) - 1); |
| 127 |
bbsnet_conf[menu_item_id].host2[sizeof(bbsnet_conf[menu_item_id].host2) - 1] = '\0'; |
bbsnet_conf[menu_item_id].org_name[sizeof(bbsnet_conf[menu_item_id].org_name) - 1] = '\0'; |
| 128 |
strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1); |
strncpy(bbsnet_conf[menu_item_id].host_name, t3, sizeof(bbsnet_conf[menu_item_id].host_name) - 1); |
| 129 |
bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0'; |
bbsnet_conf[menu_item_id].host_name[sizeof(bbsnet_conf[menu_item_id].host_name) - 1] = '\0'; |
| 130 |
bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23); |
bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23); |
| 131 |
strncpy(bbsnet_conf[menu_item_id].charset, t5, sizeof(bbsnet_conf[menu_item_id].charset) - 1); |
bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y'); |
| 132 |
|
strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1); |
| 133 |
bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0'; |
bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0'; |
| 134 |
|
|
| 135 |
p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id); |
p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id); |
| 149 |
(char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id); |
(char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id); |
| 150 |
p_menu_item->name[1] = '\0'; |
p_menu_item->name[1] = '\0'; |
| 151 |
snprintf(p_menu_item->text, sizeof(p_menu_item->text), "[1;36m%c.[m %s", |
snprintf(p_menu_item->text, sizeof(p_menu_item->text), "[1;36m%c.[m %s", |
| 152 |
p_menu_item->name[0], bbsnet_conf[menu_item_id].host1); |
p_menu_item->name[0], bbsnet_conf[menu_item_id].site_name); |
| 153 |
|
|
| 154 |
p_menu->items[p_menu->item_count] = menu_item_id; |
p_menu->items[p_menu->item_count] = menu_item_id; |
| 155 |
p_menu->item_count++; |
p_menu->item_count++; |
| 195 |
n = len; |
n = len; |
| 196 |
} |
} |
| 197 |
|
|
| 198 |
moveto(4, 0); |
moveto(4, 1); |
| 199 |
prints(" ------------------------------ \r\n"); |
prints(" ------------------------------ \r\n"); |
| 200 |
snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); |
snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); |
| 201 |
memcpy(buf2, buf, (size_t)n); |
memcpy(buf2, buf, (size_t)n); |
| 229 |
iconv_t input_cd = NULL; |
iconv_t input_cd = NULL; |
| 230 |
iconv_t output_cd = NULL; |
iconv_t output_cd = NULL; |
| 231 |
char tocode[32]; |
char tocode[32]; |
| 232 |
|
|
| 233 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 234 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 235 |
int nfds, epollfd; |
int epollfd; |
| 236 |
|
#else |
| 237 |
|
struct pollfd pfds[3]; |
| 238 |
|
#endif |
| 239 |
|
|
| 240 |
|
int nfds; |
| 241 |
int stdin_read_wait = 0; |
int stdin_read_wait = 0; |
| 242 |
int stdout_write_wait = 0; |
int stdout_write_wait = 0; |
| 243 |
int sock_read_wait = 0; |
int sock_read_wait = 0; |
| 252 |
time_t t_used = time(NULL); |
time_t t_used = time(NULL); |
| 253 |
struct tm *tm_used; |
struct tm *tm_used; |
| 254 |
int ch; |
int ch; |
| 255 |
|
char remote_user[USERNAME_MAX_LEN + 1]; |
| 256 |
|
char remote_pass[PASSWORD_MAX_LEN + 1]; |
| 257 |
|
ssh_session session = NULL; |
| 258 |
|
ssh_channel channel = NULL; |
| 259 |
|
int ssh_process_config = 0; |
| 260 |
|
int ssh_log_level = SSH_LOG_NOLOG; |
| 261 |
|
|
| 262 |
if (user_online_update("BBS_NET") < 0) |
if (user_online_update("BBS_NET") < 0) |
| 263 |
{ |
{ |
| 264 |
log_error("user_online_update(BBS_NET) error\n"); |
log_error("user_online_update(BBS_NET) error\n"); |
| 265 |
} |
} |
| 266 |
|
|
| 267 |
|
if (bbsnet_conf[n].use_ssh) |
| 268 |
|
{ |
| 269 |
|
clearscr(); |
| 270 |
|
|
| 271 |
|
if (!SSH_v2) |
| 272 |
|
{ |
| 273 |
|
moveto(1, 1); |
| 274 |
|
prints("只有在以SSH方式登陆本站时,才能使用SSH站点穿梭。"); |
| 275 |
|
press_any_key(); |
| 276 |
|
return 0; |
| 277 |
|
} |
| 278 |
|
|
| 279 |
|
moveto(1, 1); |
| 280 |
|
prints("通过SSH方式连接[%s]...", bbsnet_conf[n].site_name); |
| 281 |
|
moveto(2, 1); |
| 282 |
|
prints("请输入用户名: "); |
| 283 |
|
iflush(); |
| 284 |
|
if (str_input(remote_user, sizeof(remote_user), DOECHO) < 0) |
| 285 |
|
{ |
| 286 |
|
return -1; |
| 287 |
|
} |
| 288 |
|
if (remote_user[0] == '\0') |
| 289 |
|
{ |
| 290 |
|
return 0; |
| 291 |
|
} |
| 292 |
|
|
| 293 |
|
moveto(3, 1); |
| 294 |
|
prints("请输入密码: "); |
| 295 |
|
iflush(); |
| 296 |
|
if (str_input(remote_pass, sizeof(remote_pass), NOECHO) < 0) |
| 297 |
|
{ |
| 298 |
|
return -1; |
| 299 |
|
} |
| 300 |
|
if (remote_pass[0] == '\0') |
| 301 |
|
{ |
| 302 |
|
return 0; |
| 303 |
|
} |
| 304 |
|
} |
| 305 |
|
|
| 306 |
clearscr(); |
clearscr(); |
| 307 |
|
|
| 308 |
moveto(0, 0); |
moveto(1, 1); |
| 309 |
prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n", |
prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n", |
| 310 |
bbsnet_conf[n].host1, bbsnet_conf[n].ip); |
bbsnet_conf[n].site_name, bbsnet_conf[n].host_name); |
| 311 |
iflush(); |
iflush(); |
| 312 |
|
|
| 313 |
p_host = gethostbyname(bbsnet_conf[n].ip); |
p_host = gethostbyname(bbsnet_conf[n].host_name); |
| 314 |
|
|
| 315 |
if (p_host == NULL) |
if (p_host == NULL) |
| 316 |
{ |
{ |
| 361 |
fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK); |
fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK); |
| 362 |
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK); |
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK); |
| 363 |
|
|
| 364 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 365 |
epollfd = epoll_create1(0); |
epollfd = epoll_create1(0); |
| 366 |
if (epollfd < 0) |
if (epollfd < 0) |
| 367 |
{ |
{ |
| 384 |
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
| 385 |
goto cleanup; |
goto cleanup; |
| 386 |
} |
} |
| 387 |
|
#endif |
| 388 |
|
|
| 389 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 390 |
{ |
{ |
| 415 |
|
|
| 416 |
for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++) |
for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++) |
| 417 |
{ |
{ |
| 418 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 419 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second |
| 420 |
|
ret = nfds; |
| 421 |
|
#else |
| 422 |
|
pfds[0].fd = sock; |
| 423 |
|
pfds[0].events = POLLOUT; |
| 424 |
|
pfds[1].fd = STDIN_FILENO; |
| 425 |
|
pfds[1].events = POLLIN; |
| 426 |
|
nfds = 2; |
| 427 |
|
ret = poll(pfds, (nfds_t)nfds, 500); // 0.5 second |
| 428 |
|
#endif |
| 429 |
|
|
| 430 |
if (nfds < 0) |
if (ret < 0) |
| 431 |
{ |
{ |
| 432 |
if (errno != EINTR) |
if (errno != EINTR) |
| 433 |
{ |
{ |
| 434 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 435 |
log_error("epoll_wait() error (%d)\n", errno); |
log_error("epoll_wait() error (%d)\n", errno); |
| 436 |
|
#else |
| 437 |
|
log_error("poll() error (%d)\n", errno); |
| 438 |
|
#endif |
| 439 |
break; |
break; |
| 440 |
} |
} |
| 441 |
} |
} |
| 442 |
else if (nfds == 0) // timeout |
else if (ret == 0) // timeout |
| 443 |
{ |
{ |
| 444 |
process_bar(j + 1, MAX_PROCESS_BAR_LEN); |
process_bar(j + 1, MAX_PROCESS_BAR_LEN); |
| 445 |
} |
} |
| 447 |
{ |
{ |
| 448 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 449 |
{ |
{ |
| 450 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 451 |
if (events[i].data.fd == sock) |
if (events[i].data.fd == sock) |
| 452 |
|
#else |
| 453 |
|
if (pfds[i].fd == sock && (pfds[i].revents & POLLOUT)) |
| 454 |
|
#endif |
| 455 |
{ |
{ |
| 456 |
len = sizeof(error); |
len = sizeof(error); |
| 457 |
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0) |
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0) |
| 464 |
sock_connected = 1; |
sock_connected = 1; |
| 465 |
} |
} |
| 466 |
} |
} |
| 467 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 468 |
else if (events[i].data.fd == STDIN_FILENO) |
else if (events[i].data.fd == STDIN_FILENO) |
| 469 |
|
#else |
| 470 |
|
else if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) |
| 471 |
|
#endif |
| 472 |
{ |
{ |
| 473 |
ch = igetch(0); |
do |
| 474 |
|
{ |
| 475 |
|
ch = igetch(0); |
| 476 |
|
} while (ch == 0); |
| 477 |
if (ch == Ctrl('C') || ch == KEY_ESC) |
if (ch == Ctrl('C') || ch == KEY_ESC) |
| 478 |
{ |
{ |
| 479 |
goto cleanup; |
goto cleanup; |
| 511 |
local_addr[sizeof(local_addr) - 1] = '\0'; |
local_addr[sizeof(local_addr) - 1] = '\0'; |
| 512 |
local_port = ntohs(sin.sin_port); |
local_port = ntohs(sin.sin_port); |
| 513 |
|
|
| 514 |
|
if (bbsnet_conf[n].use_ssh) |
| 515 |
|
{ |
| 516 |
|
session = ssh_new(); |
| 517 |
|
if (session == NULL) |
| 518 |
|
{ |
| 519 |
|
log_error("ssh_new() error\n"); |
| 520 |
|
goto cleanup; |
| 521 |
|
} |
| 522 |
|
|
| 523 |
|
if (ssh_options_set(session, SSH_OPTIONS_FD, &sock) < 0 || |
| 524 |
|
ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &ssh_process_config) < 0 || |
| 525 |
|
ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, SSH_KNOWN_HOSTS_FILE) < 0 || |
| 526 |
|
ssh_options_set(session, SSH_OPTIONS_HOST, bbsnet_conf[n].host_name) < 0 || |
| 527 |
|
ssh_options_set(session, SSH_OPTIONS_USER, remote_user) < 0 || |
| 528 |
|
ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "+ssh-rsa") < 0 || |
| 529 |
|
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0) |
| 530 |
|
{ |
| 531 |
|
log_error("Error setting SSH options: %s\n", ssh_get_error(session)); |
| 532 |
|
goto cleanup; |
| 533 |
|
} |
| 534 |
|
|
| 535 |
|
ssh_set_blocking(session, 0); |
| 536 |
|
|
| 537 |
|
while (!SYS_server_exit) |
| 538 |
|
{ |
| 539 |
|
ret = ssh_connect(session); |
| 540 |
|
if (ret == SSH_OK) |
| 541 |
|
{ |
| 542 |
|
break; |
| 543 |
|
} |
| 544 |
|
else if (ret == SSH_ERROR) |
| 545 |
|
{ |
| 546 |
|
log_error("ssh_connect() error\n"); |
| 547 |
|
goto cleanup; |
| 548 |
|
} |
| 549 |
|
} |
| 550 |
|
|
| 551 |
|
ret = ssh_session_is_known_server(session); |
| 552 |
|
switch (ret) |
| 553 |
|
{ |
| 554 |
|
case SSH_KNOWN_HOSTS_NOT_FOUND: |
| 555 |
|
case SSH_KNOWN_HOSTS_UNKNOWN: |
| 556 |
|
if (ssh_session_update_known_hosts(session) != SSH_OK) |
| 557 |
|
{ |
| 558 |
|
log_error("ssh_session_update_known_hosts(%s) error\n", bbsnet_conf[n].host_name); |
| 559 |
|
prints("\033[1;31m无法添加服务器证书\033[m"); |
| 560 |
|
press_any_key(); |
| 561 |
|
goto cleanup; |
| 562 |
|
} |
| 563 |
|
log_common("SSH key of (%s) is added into %s\n", bbsnet_conf[n].host_name, SSH_KNOWN_HOSTS_FILE); |
| 564 |
|
case SSH_KNOWN_HOSTS_OK: |
| 565 |
|
break; |
| 566 |
|
case SSH_KNOWN_HOSTS_CHANGED: |
| 567 |
|
case SSH_KNOWN_HOSTS_OTHER: |
| 568 |
|
log_error("ssh_session_is_known_server(%s) error: %d\n", bbsnet_conf[n].host_name, ret); |
| 569 |
|
prints("\033[1;31m服务器证书已变更\033[m"); |
| 570 |
|
press_any_key(); |
| 571 |
|
goto cleanup; |
| 572 |
|
} |
| 573 |
|
|
| 574 |
|
for (int i = 0; !SYS_server_exit;) |
| 575 |
|
{ |
| 576 |
|
ret = ssh_userauth_password(session, NULL, remote_pass); |
| 577 |
|
if (ret == SSH_AUTH_SUCCESS) |
| 578 |
|
{ |
| 579 |
|
break; |
| 580 |
|
} |
| 581 |
|
else if (ret == SSH_AUTH_AGAIN) |
| 582 |
|
{ |
| 583 |
|
#ifdef _DEBUG |
| 584 |
|
log_error("ssh_userauth_password() error: SSH_AUTH_AGAIN\n"); |
| 585 |
|
#endif |
| 586 |
|
} |
| 587 |
|
else if (ret == SSH_AUTH_ERROR) |
| 588 |
|
{ |
| 589 |
|
log_error("ssh_userauth_password() error: %d\n", ret); |
| 590 |
|
goto cleanup; |
| 591 |
|
} |
| 592 |
|
else // if (ret == SSH_AUTH_DENIED) |
| 593 |
|
{ |
| 594 |
|
prints("\033[1;31m身份验证失败!\033[m\r\n"); |
| 595 |
|
i++; |
| 596 |
|
if (i < BBS_login_retry_times) |
| 597 |
|
{ |
| 598 |
|
prints("请输入密码: "); |
| 599 |
|
iflush(); |
| 600 |
|
if (str_input(remote_pass, sizeof(remote_pass), NOECHO) < 0) |
| 601 |
|
{ |
| 602 |
|
goto cleanup; |
| 603 |
|
} |
| 604 |
|
if (remote_pass[0] == '\0') |
| 605 |
|
{ |
| 606 |
|
goto cleanup; |
| 607 |
|
} |
| 608 |
|
} |
| 609 |
|
else |
| 610 |
|
{ |
| 611 |
|
goto cleanup; |
| 612 |
|
} |
| 613 |
|
} |
| 614 |
|
} |
| 615 |
|
|
| 616 |
|
channel = ssh_channel_new(session); |
| 617 |
|
if (channel == NULL) |
| 618 |
|
{ |
| 619 |
|
log_error("ssh_channel_new() error\n"); |
| 620 |
|
goto cleanup; |
| 621 |
|
} |
| 622 |
|
|
| 623 |
|
while (!SYS_server_exit) |
| 624 |
|
{ |
| 625 |
|
ret = ssh_channel_open_session(channel); |
| 626 |
|
if (ret == SSH_OK) |
| 627 |
|
{ |
| 628 |
|
break; |
| 629 |
|
} |
| 630 |
|
else if (ret == SSH_ERROR) |
| 631 |
|
{ |
| 632 |
|
log_error("ssh_channel_open_session() error\n"); |
| 633 |
|
goto cleanup; |
| 634 |
|
} |
| 635 |
|
} |
| 636 |
|
|
| 637 |
|
while (!SYS_server_exit) |
| 638 |
|
{ |
| 639 |
|
ret = ssh_channel_request_pty(channel); |
| 640 |
|
if (ret == SSH_OK) |
| 641 |
|
{ |
| 642 |
|
break; |
| 643 |
|
} |
| 644 |
|
else if (ret == SSH_ERROR) |
| 645 |
|
{ |
| 646 |
|
log_error("ssh_channel_request_pty() error\n"); |
| 647 |
|
goto cleanup; |
| 648 |
|
} |
| 649 |
|
} |
| 650 |
|
|
| 651 |
|
while (!SYS_server_exit) |
| 652 |
|
{ |
| 653 |
|
ret = ssh_channel_request_shell(channel); |
| 654 |
|
if (ret == SSH_OK) |
| 655 |
|
{ |
| 656 |
|
break; |
| 657 |
|
} |
| 658 |
|
else if (ret == SSH_ERROR) |
| 659 |
|
{ |
| 660 |
|
log_error("ssh_channel_request_shell() error\n"); |
| 661 |
|
goto cleanup; |
| 662 |
|
} |
| 663 |
|
} |
| 664 |
|
} |
| 665 |
|
|
| 666 |
prints("\033[1;31m连接成功!\033[m\r\n"); |
prints("\033[1;31m连接成功!\033[m\r\n"); |
| 667 |
iflush(); |
iflush(); |
| 668 |
log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n", |
log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n", |
| 687 |
goto cleanup; |
goto cleanup; |
| 688 |
} |
} |
| 689 |
|
|
| 690 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 691 |
ev.events = EPOLLIN | EPOLLOUT | EPOLLET; |
ev.events = EPOLLIN | EPOLLOUT | EPOLLET; |
| 692 |
ev.data.fd = sock; |
ev.data.fd = sock; |
| 693 |
if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1) |
if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1) |
| 703 |
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); |
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); |
| 704 |
goto cleanup; |
goto cleanup; |
| 705 |
} |
} |
| 706 |
|
#endif |
| 707 |
|
|
| 708 |
BBS_last_access_tm = t_used = time(NULL); |
BBS_last_access_tm = t_used = time(NULL); |
| 709 |
loop = 1; |
loop = 1; |
| 717 |
break; |
break; |
| 718 |
} |
} |
| 719 |
|
|
| 720 |
|
if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(channel)) |
| 721 |
|
{ |
| 722 |
|
log_error("Remote SSH channel is closed\n"); |
| 723 |
|
loop = 0; |
| 724 |
|
break; |
| 725 |
|
} |
| 726 |
|
|
| 727 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 728 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
| 729 |
|
ret = nfds; |
| 730 |
|
#else |
| 731 |
|
pfds[0].fd = STDIN_FILENO; |
| 732 |
|
pfds[0].events = POLLIN; |
| 733 |
|
pfds[1].fd = sock; |
| 734 |
|
pfds[1].events = POLLIN | POLLOUT; |
| 735 |
|
pfds[2].fd = STDOUT_FILENO; |
| 736 |
|
pfds[2].events = POLLOUT; |
| 737 |
|
nfds = 3; |
| 738 |
|
ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second |
| 739 |
|
#endif |
| 740 |
|
|
| 741 |
if (nfds < 0) |
if (ret < 0) |
| 742 |
{ |
{ |
| 743 |
if (errno != EINTR) |
if (errno != EINTR) |
| 744 |
{ |
{ |
| 745 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 746 |
log_error("epoll_wait() error (%d)\n", errno); |
log_error("epoll_wait() error (%d)\n", errno); |
| 747 |
|
#else |
| 748 |
|
log_error("poll() error (%d)\n", errno); |
| 749 |
|
#endif |
| 750 |
break; |
break; |
| 751 |
} |
} |
| 752 |
continue; |
continue; |
| 753 |
} |
} |
| 754 |
else if (nfds == 0) // timeout |
else if (ret == 0) // timeout |
| 755 |
{ |
{ |
| 756 |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |
| 757 |
{ |
{ |
| 758 |
break; |
break; |
| 759 |
} |
} |
| 761 |
|
|
| 762 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 763 |
{ |
{ |
| 764 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 765 |
if (events[i].data.fd == STDIN_FILENO) |
if (events[i].data.fd == STDIN_FILENO) |
| 766 |
|
#else |
| 767 |
|
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) |
| 768 |
|
#endif |
| 769 |
{ |
{ |
| 770 |
stdin_read_wait = 1; |
stdin_read_wait = 1; |
| 771 |
} |
} |
| 772 |
|
|
| 773 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 774 |
if (events[i].data.fd == sock) |
if (events[i].data.fd == sock) |
| 775 |
|
#else |
| 776 |
|
if (pfds[i].fd == sock) |
| 777 |
|
#endif |
| 778 |
{ |
{ |
| 779 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 780 |
if (events[i].events & EPOLLIN) |
if (events[i].events & EPOLLIN) |
| 781 |
|
#else |
| 782 |
|
if (pfds[i].revents & POLLIN) |
| 783 |
|
#endif |
| 784 |
{ |
{ |
| 785 |
sock_read_wait = 1; |
sock_read_wait = 1; |
| 786 |
} |
} |
| 787 |
|
|
| 788 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 789 |
if (events[i].events & EPOLLOUT) |
if (events[i].events & EPOLLOUT) |
| 790 |
|
#else |
| 791 |
|
if (pfds[i].revents & POLLOUT) |
| 792 |
|
#endif |
| 793 |
{ |
{ |
| 794 |
sock_write_wait = 1; |
sock_write_wait = 1; |
| 795 |
} |
} |
| 796 |
} |
} |
| 797 |
|
|
| 798 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 799 |
if (events[i].data.fd == STDOUT_FILENO) |
if (events[i].data.fd == STDOUT_FILENO) |
| 800 |
|
#else |
| 801 |
|
if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) |
| 802 |
|
#endif |
| 803 |
{ |
{ |
| 804 |
stdout_write_wait = 1; |
stdout_write_wait = 1; |
| 805 |
} |
} |
| 912 |
|
|
| 913 |
while (input_conv_offset < input_conv_len && !SYS_server_exit) |
while (input_conv_offset < input_conv_len && !SYS_server_exit) |
| 914 |
{ |
{ |
| 915 |
ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset)); |
if (bbsnet_conf[n].use_ssh) |
| 916 |
|
{ |
| 917 |
|
ret = ssh_channel_write(channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset)); |
| 918 |
|
if (ret == SSH_ERROR) |
| 919 |
|
{ |
| 920 |
|
log_error("ssh_channel_write() error: %s\n", ssh_get_error(session)); |
| 921 |
|
loop = 0; |
| 922 |
|
break; |
| 923 |
|
} |
| 924 |
|
} |
| 925 |
|
else |
| 926 |
|
{ |
| 927 |
|
ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset)); |
| 928 |
|
} |
| 929 |
if (ret < 0) |
if (ret < 0) |
| 930 |
{ |
{ |
| 931 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 971 |
{ |
{ |
| 972 |
while (output_buf_len < sizeof(output_buf) && !SYS_server_exit) |
while (output_buf_len < sizeof(output_buf) && !SYS_server_exit) |
| 973 |
{ |
{ |
| 974 |
ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len); |
if (bbsnet_conf[n].use_ssh) |
| 975 |
|
{ |
| 976 |
|
ret = ssh_channel_read_nonblocking(channel, output_buf + output_buf_len, |
| 977 |
|
(uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0); |
| 978 |
|
if (ret == SSH_ERROR) |
| 979 |
|
{ |
| 980 |
|
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(session)); |
| 981 |
|
loop = 0; |
| 982 |
|
break; |
| 983 |
|
} |
| 984 |
|
else if (ret == SSH_EOF) |
| 985 |
|
{ |
| 986 |
|
sock_read_wait = 0; |
| 987 |
|
loop = 0; |
| 988 |
|
break; |
| 989 |
|
} |
| 990 |
|
else if (ret == 0) |
| 991 |
|
{ |
| 992 |
|
sock_read_wait = 0; |
| 993 |
|
break; |
| 994 |
|
} |
| 995 |
|
} |
| 996 |
|
else |
| 997 |
|
{ |
| 998 |
|
ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len); |
| 999 |
|
} |
| 1000 |
if (ret < 0) |
if (ret < 0) |
| 1001 |
{ |
{ |
| 1002 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 1106 |
iconv_close(output_cd); |
iconv_close(output_cd); |
| 1107 |
|
|
| 1108 |
cleanup: |
cleanup: |
| 1109 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 1110 |
if (close(epollfd) < 0) |
if (close(epollfd) < 0) |
| 1111 |
{ |
{ |
| 1112 |
log_error("close(epoll) error (%d)\n"); |
log_error("close(epoll) error (%d)\n"); |
| 1113 |
} |
} |
| 1114 |
|
#endif |
| 1115 |
|
|
| 1116 |
|
if (bbsnet_conf[n].use_ssh) |
| 1117 |
|
{ |
| 1118 |
|
ssh_channel_free(channel); |
| 1119 |
|
ssh_disconnect(session); |
| 1120 |
|
ssh_free(session); |
| 1121 |
|
} |
| 1122 |
|
|
| 1123 |
// Restore STDIN/STDOUT flags |
// Restore STDIN/STDOUT flags |
| 1124 |
fcntl(STDIN_FILENO, F_SETFL, flags_stdin); |
fcntl(STDIN_FILENO, F_SETFL, flags_stdin); |
| 1125 |
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout); |
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout); |
| 1126 |
|
|
|
// Restore socket flags |
|
|
fcntl(sock, F_SETFL, flags_sock); |
|
|
|
|
| 1127 |
if (close(sock) == -1) |
if (close(sock) == -1) |
| 1128 |
{ |
{ |
| 1129 |
log_error("Close socket failed\n"); |
log_error("Close socket failed\n"); |
| 1144 |
static int bbsnet_refresh() |
static int bbsnet_refresh() |
| 1145 |
{ |
{ |
| 1146 |
clearscr(); |
clearscr(); |
| 1147 |
moveto(1, 0); |
|
| 1148 |
prints(" ----------------------------------------------------------------------------- "); |
moveto(1, 1); |
| 1149 |
|
prints(" ------------------------------------------------------------------------------ "); |
| 1150 |
for (int i = 2; i < 19; i++) |
for (int i = 2; i < 19; i++) |
| 1151 |
{ |
{ |
| 1152 |
moveto(i, 0); |
moveto(i, 1); |
| 1153 |
prints("|"); |
prints("|"); |
| 1154 |
moveto(i, 79); |
moveto(i, 80); |
| 1155 |
prints("|"); |
prints("|"); |
| 1156 |
} |
} |
| 1157 |
moveto(19, 0); |
moveto(19, 1); |
| 1158 |
prints("|-----------------------------------------------------------------------------|"); |
prints("|------------------------------------------------------------------------------|"); |
| 1159 |
moveto(22, 0); |
moveto(22, 1); |
| 1160 |
prints(" ----------------------------------------------------------------------------- "); |
prints(" ------------------------------------------------------------------------------ "); |
| 1161 |
moveto(23, 0); |
moveto(23, 1); |
| 1162 |
prints(" [\x1b[1;32mCtrl+C\x1b[m]退出"); |
prints(" [\033[1;32mCtrl+C\033[m]退出"); |
| 1163 |
|
|
| 1164 |
iflush(); |
iflush(); |
| 1165 |
|
|
| 1170 |
{ |
{ |
| 1171 |
int i = bbsnet_menu.menu_item_pos[0]; |
int i = bbsnet_menu.menu_item_pos[0]; |
| 1172 |
|
|
| 1173 |
moveto(20, 0); |
moveto(20, 1); |
| 1174 |
clrtoeol(); |
clrtoeol(); |
| 1175 |
prints("|\x1b[1m单位:\x1b[1;33m%-18s\x1b[m 站名:\x1b[1;33m%s\x1b[m", |
prints("|\033[1m单位: \033[1;33m%s\033[m%*s 站名: \033[1;33m%s\033[m%*s 类型: \033[1;33m%s\033[m", |
| 1176 |
bbsnet_conf[i].host2, bbsnet_conf[i].host1); |
bbsnet_conf[i].org_name, 20 - str_length(bbsnet_conf[i].org_name, 1), "", |
| 1177 |
moveto(20, 79); |
bbsnet_conf[i].site_name, 20 - str_length(bbsnet_conf[i].site_name, 1), "", |
| 1178 |
|
(bbsnet_conf[i].use_ssh ? "SSH" : "Telnet")); |
| 1179 |
|
moveto(20, 80); |
| 1180 |
prints("|"); |
prints("|"); |
| 1181 |
moveto(21, 0); |
moveto(21, 1); |
| 1182 |
clrtoeol(); |
clrtoeol(); |
| 1183 |
prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip); |
prints("|\033[1m连往: \033[1;33m%-20s\033[m 端口: \033[1;33m%-5d\033[m 编码: \033[1;33m%s\033[m", |
| 1184 |
if (bbsnet_conf[i].port != 23) |
bbsnet_conf[i].host_name, bbsnet_conf[i].port, bbsnet_conf[i].charset); |
| 1185 |
{ |
moveto(21, 80); |
|
prints(" %d", bbsnet_conf[i].port); |
|
|
} |
|
|
prints("\x1b[m"); |
|
|
moveto(21, 79); |
|
| 1186 |
prints("|"); |
prints("|"); |
| 1187 |
iflush(); |
iflush(); |
| 1188 |
|
|
| 1189 |
return 0; |
return 0; |
| 1190 |
} |
} |
| 1191 |
|
|
| 1192 |
extern int bbs_net() |
int bbs_net() |
| 1193 |
{ |
{ |
| 1194 |
int ch, i; |
int ch, i; |
| 1195 |
|
|
| 1196 |
load_bbsnet_conf(CONF_BBSNET); |
load_bbsnet_conf(CONF_BBSNET); |
| 1197 |
|
|
|
clearscr(); |
|
| 1198 |
bbsnet_refresh(); |
bbsnet_refresh(); |
| 1199 |
display_menu(&bbsnet_menu); |
display_menu(&bbsnet_menu); |
| 1200 |
bbsnet_selchange(); |
bbsnet_selchange(); |
| 1214 |
log_error("KEY_NULL\n"); |
log_error("KEY_NULL\n"); |
| 1215 |
goto cleanup; |
goto cleanup; |
| 1216 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 1217 |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |
| 1218 |
{ |
{ |
| 1219 |
log_error("User input timeout\n"); |
log_error("User input timeout\n"); |
| 1220 |
goto cleanup; |
goto cleanup; |
| 1225 |
goto cleanup; |
goto cleanup; |
| 1226 |
case CR: |
case CR: |
| 1227 |
bbsnet_connect(bbsnet_menu.menu_item_pos[0]); |
bbsnet_connect(bbsnet_menu.menu_item_pos[0]); |
| 1228 |
|
// Force cleanup anything remaining in the output buffer |
| 1229 |
|
clearscr(); |
| 1230 |
|
iflush(); |
| 1231 |
|
// Clear screen and redraw menu |
| 1232 |
bbsnet_refresh(); |
bbsnet_refresh(); |
| 1233 |
display_menu(&bbsnet_menu); |
display_menu(&bbsnet_menu); |
| 1234 |
bbsnet_selchange(); |
bbsnet_selchange(); |