| 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 |
| 61 |
{ |
{ |
| 62 |
char host1[20]; |
char org_name[40]; |
| 63 |
char host2[40]; |
char site_name[40]; |
| 64 |
char ip[40]; |
char host_name[IP_ADDR_LEN]; |
| 65 |
in_port_t port; |
in_port_t port; |
| 66 |
int8_t use_ssh; |
int8_t use_ssh; |
| 67 |
char charset[CHARSET_MAX_LEN + 1]; |
char charset[CHARSET_MAX_LEN + 1]; |
| 69 |
|
|
| 70 |
static MENU_SET bbsnet_menu; |
static MENU_SET bbsnet_menu; |
| 71 |
|
|
| 72 |
|
static void unload_bbsnet_conf(void); |
| 73 |
|
|
| 74 |
static int load_bbsnet_conf(const char *file_config) |
static int load_bbsnet_conf(const char *file_config) |
| 75 |
{ |
{ |
| 76 |
FILE *fp; |
FILE *fp; |
| 77 |
MENU *p_menu; |
MENU *p_menu; |
| 78 |
MENU_ITEM *p_menu_item; |
MENU_ITEM *p_menu_item; |
| 79 |
MENU_ITEM_ID menu_item_id; |
MENU_ITEM_ID menu_item_id; |
| 80 |
char t[256], *t1, *t2, *t3, *t4, *t5, *t6, *saveptr; |
char line[LINE_BUFFER_LEN], *t1, *t2, *t3, *t4, *t5, *t6, *saveptr; |
| 81 |
|
int port; |
| 82 |
|
|
| 83 |
fp = fopen(file_config, "r"); |
unload_bbsnet_conf(); |
|
if (fp == NULL) |
|
|
{ |
|
|
return -1; |
|
|
} |
|
| 84 |
|
|
| 85 |
bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU)); |
bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU)); |
| 86 |
if (bbsnet_menu.p_menu_pool == NULL) |
if (bbsnet_menu.p_menu_pool == NULL) |
| 87 |
{ |
{ |
| 88 |
log_error("calloc(p_menu_pool) error\n"); |
log_error("calloc(p_menu_pool) error\n"); |
| 89 |
return -3; |
return -1; |
| 90 |
} |
} |
| 91 |
bbsnet_menu.menu_count = 1; |
bbsnet_menu.menu_count = 1; |
| 92 |
|
|
| 94 |
if (bbsnet_menu.p_menu_item_pool == NULL) |
if (bbsnet_menu.p_menu_item_pool == NULL) |
| 95 |
{ |
{ |
| 96 |
log_error("calloc(p_menu_item_pool) error\n"); |
log_error("calloc(p_menu_item_pool) error\n"); |
| 97 |
return -3; |
unload_bbsnet_conf(); |
| 98 |
|
return -1; |
| 99 |
} |
} |
| 100 |
bbsnet_menu.menu_item_count = MAXSTATION; |
bbsnet_menu.menu_item_count = MAXSTATION; |
| 101 |
|
|
| 106 |
p_menu->title.show = 0; |
p_menu->title.show = 0; |
| 107 |
p_menu->screen_show = 0; |
p_menu->screen_show = 0; |
| 108 |
|
|
| 109 |
|
fp = fopen(file_config, "r"); |
| 110 |
|
if (fp == NULL) |
| 111 |
|
{ |
| 112 |
|
unload_bbsnet_conf(); |
| 113 |
|
return -2; |
| 114 |
|
} |
| 115 |
|
|
| 116 |
menu_item_id = 0; |
menu_item_id = 0; |
| 117 |
while (fgets(t, 255, fp) && menu_item_id < MAXSTATION) |
while (fgets(line, sizeof(line), fp) && menu_item_id < MAXSTATION) |
| 118 |
{ |
{ |
| 119 |
t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr); |
t1 = strtok_r(line, MENU_CONF_DELIM, &saveptr); |
| 120 |
t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 121 |
t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 122 |
t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 124 |
t6 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
t6 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 125 |
|
|
| 126 |
if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || |
if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || |
| 127 |
t5 == NULL || t6 == NULL || t[0] == '#' || t[0] == '*') |
t5 == NULL || t6 == NULL || line[0] == '#' || line[0] == '*') |
| 128 |
{ |
{ |
| 129 |
continue; |
continue; |
| 130 |
} |
} |
| 131 |
|
|
| 132 |
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); |
| 133 |
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'; |
| 134 |
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); |
| 135 |
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'; |
| 136 |
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); |
| 137 |
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'; |
| 138 |
bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23); |
port = atoi(t4); |
| 139 |
|
if (port <= 0 || port > 65535) |
| 140 |
|
{ |
| 141 |
|
log_error("Invalid port value %d of menu item %d\n", port, menu_item_id); |
| 142 |
|
fclose(fp); |
| 143 |
|
unload_bbsnet_conf(); |
| 144 |
|
return -3; |
| 145 |
|
} |
| 146 |
|
bbsnet_conf[menu_item_id].port = (in_port_t)port; |
| 147 |
bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y'); |
bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y'); |
| 148 |
strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1); |
strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1); |
| 149 |
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'; |
| 152 |
if (p_menu_item == NULL) |
if (p_menu_item == NULL) |
| 153 |
{ |
{ |
| 154 |
log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); |
log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); |
| 155 |
return -1; |
fclose(fp); |
| 156 |
|
unload_bbsnet_conf(); |
| 157 |
|
return -3; |
| 158 |
} |
} |
| 159 |
|
|
| 160 |
p_menu_item->row = (int16_t)(2 + menu_item_id / STATION_PER_LINE); |
p_menu_item->row = (int16_t)(2 + menu_item_id / STATION_PER_LINE); |
| 166 |
p_menu_item->name[0] = |
p_menu_item->name[0] = |
| 167 |
(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); |
| 168 |
p_menu_item->name[1] = '\0'; |
p_menu_item->name[1] = '\0'; |
| 169 |
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), "\033[1;36m%c.\033[m %s", |
| 170 |
p_menu_item->name[0], bbsnet_conf[menu_item_id].host1); |
p_menu_item->name[0], bbsnet_conf[menu_item_id].site_name); |
| 171 |
|
|
| 172 |
p_menu->items[p_menu->item_count] = menu_item_id; |
p_menu->items[p_menu->item_count] = menu_item_id; |
| 173 |
p_menu->item_count++; |
p_menu->item_count++; |
| 189 |
bbsnet_menu.menu_count = 0; |
bbsnet_menu.menu_count = 0; |
| 190 |
bbsnet_menu.menu_item_count = 0; |
bbsnet_menu.menu_item_count = 0; |
| 191 |
|
|
| 192 |
free(bbsnet_menu.p_menu_pool); |
if (bbsnet_menu.p_menu_pool) |
| 193 |
bbsnet_menu.p_menu_pool = NULL; |
{ |
| 194 |
free(bbsnet_menu.p_menu_item_pool); |
free(bbsnet_menu.p_menu_pool); |
| 195 |
bbsnet_menu.p_menu_item_pool = NULL; |
bbsnet_menu.p_menu_pool = NULL; |
| 196 |
|
} |
| 197 |
|
|
| 198 |
|
if (bbsnet_menu.p_menu_item_pool) |
| 199 |
|
{ |
| 200 |
|
free(bbsnet_menu.p_menu_item_pool); |
| 201 |
|
bbsnet_menu.p_menu_item_pool = NULL; |
| 202 |
|
} |
| 203 |
} |
} |
| 204 |
|
|
| 205 |
static void process_bar(int n, int len) |
static void process_bar(int n, int len) |
| 207 |
char buf[LINE_BUFFER_LEN]; |
char buf[LINE_BUFFER_LEN]; |
| 208 |
char buf2[LINE_BUFFER_LEN]; |
char buf2[LINE_BUFFER_LEN]; |
| 209 |
|
|
| 210 |
if (len > LINE_BUFFER_LEN) |
if (len <= 0) |
| 211 |
|
{ |
| 212 |
|
len = 1; |
| 213 |
|
} |
| 214 |
|
else if (len > LINE_BUFFER_LEN) |
| 215 |
{ |
{ |
| 216 |
len = LINE_BUFFER_LEN - 1; |
len = LINE_BUFFER_LEN - 1; |
| 217 |
} |
} |
| 236 |
|
|
| 237 |
static int bbsnet_connect(int n) |
static int bbsnet_connect(int n) |
| 238 |
{ |
{ |
| 239 |
int sock, ret, loop, error; |
int sock = -1; |
| 240 |
|
int ret; |
| 241 |
|
int loop; |
| 242 |
|
int error; |
| 243 |
int sock_connected = 0; |
int sock_connected = 0; |
| 244 |
int flags_sock; |
int flags_sock = -1; |
| 245 |
int flags_stdin; |
int flags_stdin = -1; |
| 246 |
int flags_stdout; |
int flags_stdout = -1; |
|
int len; |
|
| 247 |
struct sockaddr_in sin; |
struct sockaddr_in sin; |
| 248 |
char input_buf[LINE_BUFFER_LEN]; |
char input_buf[LINE_BUFFER_LEN]; |
| 249 |
char output_buf[LINE_BUFFER_LEN]; |
char output_buf[LINE_BUFFER_LEN]; |
| 257 |
int output_conv_len = 0; |
int output_conv_len = 0; |
| 258 |
int input_conv_offset = 0; |
int input_conv_offset = 0; |
| 259 |
int output_conv_offset = 0; |
int output_conv_offset = 0; |
| 260 |
iconv_t input_cd = NULL; |
iconv_t input_cd = (iconv_t)(-1); |
| 261 |
iconv_t output_cd = NULL; |
iconv_t output_cd = (iconv_t)(-1); |
| 262 |
char tocode[32]; |
char tocode[CHARSET_MAX_LEN + 20]; |
| 263 |
|
|
| 264 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 265 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 266 |
int epollfd; |
int epollfd = -1; |
| 267 |
#else |
#else |
| 268 |
struct pollfd pfds[3]; |
struct pollfd pfds[3]; |
| 269 |
#endif |
#endif |
| 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; |
| 289 |
ssh_session session = NULL; |
ssh_session session = NULL; |
| 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_PROTOCOL; |
int ssh_log_level = SSH_LOG_NOLOG; |
| 293 |
|
|
| 294 |
if (user_online_update("BBS_NET") < 0) |
if (user_online_update("BBS_NET") < 0) |
| 295 |
{ |
{ |
| 309 |
} |
} |
| 310 |
|
|
| 311 |
moveto(1, 1); |
moveto(1, 1); |
| 312 |
prints("通过SSH方式连接[%s]...", bbsnet_conf[n].host1); |
prints("通过SSH方式连接[%s]...", bbsnet_conf[n].site_name); |
| 313 |
moveto(2, 1); |
moveto(2, 1); |
| 314 |
prints("请输入用户名: "); |
prints("请输入用户名: "); |
| 315 |
iflush(); |
iflush(); |
| 339 |
|
|
| 340 |
moveto(1, 1); |
moveto(1, 1); |
| 341 |
prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n", |
prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n", |
| 342 |
bbsnet_conf[n].host1, bbsnet_conf[n].ip); |
bbsnet_conf[n].site_name, bbsnet_conf[n].host_name); |
| 343 |
iflush(); |
iflush(); |
| 344 |
|
|
| 345 |
p_host = gethostbyname(bbsnet_conf[n].ip); |
p_host = gethostbyname(bbsnet_conf[n].host_name); |
| 346 |
|
|
| 347 |
if (p_host == NULL) |
if (p_host == NULL) |
| 348 |
{ |
{ |
| 349 |
prints("\033[1;31m查找主机名失败!\033[m\r\n"); |
prints("\033[1;31m查找主机名失败!\033[m\r\n"); |
| 350 |
press_any_key(); |
press_any_key(); |
| 351 |
return -1; |
goto cleanup; |
| 352 |
} |
} |
| 353 |
|
|
| 354 |
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 357 |
{ |
{ |
| 358 |
prints("\033[1;31m无法创建socket!\033[m\r\n"); |
prints("\033[1;31m无法创建socket!\033[m\r\n"); |
| 359 |
press_any_key(); |
press_any_key(); |
| 360 |
return -1; |
goto cleanup; |
| 361 |
} |
} |
| 362 |
|
|
| 363 |
sin.sin_family = AF_INET; |
sin.sin_family = AF_INET; |
| 368 |
{ |
{ |
| 369 |
log_error("Bind address %s:%u failed (%d)\n", |
log_error("Bind address %s:%u failed (%d)\n", |
| 370 |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); |
| 371 |
return -2; |
goto cleanup; |
| 372 |
} |
} |
| 373 |
|
|
| 374 |
memset(&sin, 0, sizeof(sin)); |
memset(&sin, 0, sizeof(sin)); |
| 384 |
process_bar(0, MAX_PROCESS_BAR_LEN); |
process_bar(0, MAX_PROCESS_BAR_LEN); |
| 385 |
|
|
| 386 |
// Set socket as non-blocking |
// Set socket as non-blocking |
| 387 |
flags_sock = fcntl(sock, F_GETFL, 0); |
if ((flags_sock = fcntl(sock, F_GETFL, 0)) == -1) |
| 388 |
fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK); |
{ |
| 389 |
|
log_error("fcntl(F_GETFL) error (%d)\n", errno); |
| 390 |
|
goto cleanup; |
| 391 |
|
} |
| 392 |
|
if ((fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK)) == -1) |
| 393 |
|
{ |
| 394 |
|
log_error("fcntl(F_SETFL) error (%d)\n", errno); |
| 395 |
|
goto cleanup; |
| 396 |
|
} |
| 397 |
|
|
| 398 |
// Set STDIN/STDOUT as non-blocking |
// Set STDIN/STDOUT as non-blocking |
| 399 |
flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0); |
if ((flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1) |
| 400 |
flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0); |
{ |
| 401 |
fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK); |
log_error("fcntl(F_GETFL) error (%d)\n", errno); |
| 402 |
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK); |
goto cleanup; |
| 403 |
|
} |
| 404 |
|
if ((flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1) |
| 405 |
|
{ |
| 406 |
|
log_error("fcntl(F_GETFL) error (%d)\n", errno); |
| 407 |
|
goto cleanup; |
| 408 |
|
} |
| 409 |
|
if ((fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK)) == -1) |
| 410 |
|
{ |
| 411 |
|
log_error("fcntl(F_SETFL) error (%d)\n", errno); |
| 412 |
|
goto cleanup; |
| 413 |
|
} |
| 414 |
|
if ((fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK)) == -1) |
| 415 |
|
{ |
| 416 |
|
log_error("fcntl(F_SETFL) error (%d)\n", errno); |
| 417 |
|
goto cleanup; |
| 418 |
|
} |
| 419 |
|
|
| 420 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 421 |
epollfd = epoll_create1(0); |
epollfd = epoll_create1(0); |
| 422 |
if (epollfd < 0) |
if (epollfd < 0) |
| 423 |
{ |
{ |
| 424 |
log_error("epoll_create1() error (%d)\n", errno); |
log_error("epoll_create1() error (%d)\n", errno); |
| 425 |
return -1; |
goto cleanup; |
| 426 |
} |
} |
| 427 |
|
|
| 428 |
ev.events = EPOLLOUT | EPOLLET; |
ev.events = EPOLLOUT | EPOLLET; |
| 509 |
if (pfds[i].fd == sock && (pfds[i].revents & POLLOUT)) |
if (pfds[i].fd == sock && (pfds[i].revents & POLLOUT)) |
| 510 |
#endif |
#endif |
| 511 |
{ |
{ |
| 512 |
len = sizeof(error); |
socklen_t len = sizeof(error); |
| 513 |
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0) |
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) |
| 514 |
{ |
{ |
| 515 |
log_error("getsockopt() error (%d) !\n", error); |
log_error("getsockopt() error (%d) !\n", errno); |
| 516 |
goto cleanup; |
goto cleanup; |
| 517 |
} |
} |
| 518 |
if (error == 0) |
if (error == 0) |
| 578 |
|
|
| 579 |
if (ssh_options_set(session, SSH_OPTIONS_FD, &sock) < 0 || |
if (ssh_options_set(session, SSH_OPTIONS_FD, &sock) < 0 || |
| 580 |
ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &ssh_process_config) < 0 || |
ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &ssh_process_config) < 0 || |
| 581 |
ssh_options_set(session, SSH_OPTIONS_HOST, bbsnet_conf[n].ip) < 0 || |
ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, SSH_KNOWN_HOSTS_FILE) < 0 || |
| 582 |
|
ssh_options_set(session, SSH_OPTIONS_HOST, bbsnet_conf[n].host_name) < 0 || |
| 583 |
ssh_options_set(session, SSH_OPTIONS_USER, remote_user) < 0 || |
ssh_options_set(session, SSH_OPTIONS_USER, remote_user) < 0 || |
| 584 |
ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "+ssh-rsa") < 0 || |
ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "+ssh-rsa") < 0 || |
| 585 |
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0) |
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0) |
| 588 |
goto cleanup; |
goto cleanup; |
| 589 |
} |
} |
| 590 |
|
|
| 591 |
while (!SYS_server_exit) |
ssh_set_blocking(session, 0); |
| 592 |
|
|
| 593 |
|
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\n"); |
// log_error("ssh_connect() error: SSH_AGAIN\n"); |
| 605 |
|
} |
| 606 |
|
else // if (ret == SSH_ERROR) |
| 607 |
|
{ |
| 608 |
|
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 |
ssh_set_blocking(session, 0); |
ret = ssh_session_is_known_server(session); |
| 620 |
|
switch (ret) |
| 621 |
|
{ |
| 622 |
|
case SSH_KNOWN_HOSTS_NOT_FOUND: |
| 623 |
|
case SSH_KNOWN_HOSTS_UNKNOWN: |
| 624 |
|
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); |
| 627 |
|
prints("\033[1;31m无法添加服务器证书\033[m\r\n"); |
| 628 |
|
press_any_key(); |
| 629 |
|
goto cleanup; |
| 630 |
|
} |
| 631 |
|
log_common("SSH key of (%s) is added into %s\n", bbsnet_conf[n].host_name, SSH_KNOWN_HOSTS_FILE); |
| 632 |
|
case SSH_KNOWN_HOSTS_OK: |
| 633 |
|
break; |
| 634 |
|
case SSH_KNOWN_HOSTS_CHANGED: |
| 635 |
|
case SSH_KNOWN_HOSTS_OTHER: |
| 636 |
|
log_error("ssh_session_is_known_server(%s) error: %d\n", bbsnet_conf[n].host_name, ret); |
| 637 |
|
prints("\033[1;31m服务器证书已变更\033[m\r\n"); |
| 638 |
|
press_any_key(); |
| 639 |
|
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\n"); |
// 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: 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) |
| 750 |
|
{ |
| 751 |
|
prints("\033[1;31m连接超时!\033[m\r\n"); |
| 752 |
|
press_any_key(); |
| 753 |
|
goto cleanup; |
| 754 |
|
} |
| 755 |
} |
} |
| 756 |
|
|
| 757 |
prints("\033[1;31m连接成功!\033[m\r\n"); |
prints("\033[1;31m连接成功!\033[m\r\n"); |
| 774 |
if (output_cd == (iconv_t)(-1)) |
if (output_cd == (iconv_t)(-1)) |
| 775 |
{ |
{ |
| 776 |
log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno); |
log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno); |
|
iconv_close(input_cd); |
|
| 777 |
goto cleanup; |
goto cleanup; |
| 778 |
} |
} |
| 779 |
|
|
| 802 |
{ |
{ |
| 803 |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
| 804 |
{ |
{ |
| 805 |
|
#ifdef _DEBUG |
| 806 |
log_error("SSH channel is closed\n"); |
log_error("SSH channel is closed\n"); |
| 807 |
|
#endif |
| 808 |
loop = 0; |
loop = 0; |
| 809 |
break; |
break; |
| 810 |
} |
} |
| 811 |
|
|
| 812 |
if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(channel)) |
if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(channel)) |
| 813 |
{ |
{ |
| 814 |
|
#ifdef _DEBUG |
| 815 |
log_error("Remote SSH channel is closed\n"); |
log_error("Remote SSH channel is closed\n"); |
| 816 |
|
#endif |
| 817 |
loop = 0; |
loop = 0; |
| 818 |
break; |
break; |
| 819 |
} |
} |
| 908 |
ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0); |
ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0); |
| 909 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 910 |
{ |
{ |
| 911 |
|
#ifdef _DEBUG |
| 912 |
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
| 913 |
|
#endif |
| 914 |
loop = 0; |
loop = 0; |
| 915 |
break; |
break; |
| 916 |
} |
} |
| 1013 |
ret = ssh_channel_write(channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset)); |
ret = ssh_channel_write(channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset)); |
| 1014 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 1015 |
{ |
{ |
| 1016 |
|
#ifdef _DEBUG |
| 1017 |
log_error("ssh_channel_write() error: %s\n", ssh_get_error(session)); |
log_error("ssh_channel_write() error: %s\n", ssh_get_error(session)); |
| 1018 |
|
#endif |
| 1019 |
loop = 0; |
loop = 0; |
| 1020 |
break; |
break; |
| 1021 |
} |
} |
| 1037 |
} |
} |
| 1038 |
else |
else |
| 1039 |
{ |
{ |
| 1040 |
|
#ifdef _DEBUG |
| 1041 |
log_error("write(socket) error (%d)\n", errno); |
log_error("write(socket) error (%d)\n", errno); |
| 1042 |
|
#endif |
| 1043 |
loop = 0; |
loop = 0; |
| 1044 |
break; |
break; |
| 1045 |
} |
} |
| 1077 |
(uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0); |
(uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0); |
| 1078 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 1079 |
{ |
{ |
| 1080 |
|
#ifdef _DEBUG |
| 1081 |
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(session)); |
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(session)); |
| 1082 |
|
#endif |
| 1083 |
loop = 0; |
loop = 0; |
| 1084 |
break; |
break; |
| 1085 |
} |
} |
| 1112 |
} |
} |
| 1113 |
else |
else |
| 1114 |
{ |
{ |
| 1115 |
|
#ifdef _DEBUG |
| 1116 |
log_error("read(socket) error (%d)\n", errno); |
log_error("read(socket) error (%d)\n", errno); |
| 1117 |
|
#endif |
| 1118 |
loop = 0; |
loop = 0; |
| 1119 |
break; |
break; |
| 1120 |
} |
} |
| 1155 |
ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset)); |
ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset)); |
| 1156 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 1157 |
{ |
{ |
| 1158 |
|
#ifdef _DEBUG |
| 1159 |
log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); |
log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); |
| 1160 |
|
#endif |
| 1161 |
loop = 0; |
loop = 0; |
| 1162 |
break; |
break; |
| 1163 |
} |
} |
| 1179 |
} |
} |
| 1180 |
else |
else |
| 1181 |
{ |
{ |
| 1182 |
|
#ifdef _DEBUG |
| 1183 |
log_error("write(STDOUT) error (%d)\n", errno); |
log_error("write(STDOUT) error (%d)\n", errno); |
| 1184 |
|
#endif |
| 1185 |
loop = 0; |
loop = 0; |
| 1186 |
break; |
break; |
| 1187 |
} |
} |
| 1210 |
} |
} |
| 1211 |
} |
} |
| 1212 |
|
|
|
iconv_close(input_cd); |
|
|
iconv_close(output_cd); |
|
|
|
|
| 1213 |
cleanup: |
cleanup: |
| 1214 |
|
if (input_cd != (iconv_t)(-1)) |
| 1215 |
|
{ |
| 1216 |
|
iconv_close(input_cd); |
| 1217 |
|
} |
| 1218 |
|
if (output_cd != (iconv_t)(-1)) |
| 1219 |
|
{ |
| 1220 |
|
iconv_close(output_cd); |
| 1221 |
|
} |
| 1222 |
|
|
| 1223 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 1224 |
if (close(epollfd) < 0) |
if (epollfd != -1 && close(epollfd) < 0) |
| 1225 |
{ |
{ |
| 1226 |
log_error("close(epoll) error (%d)\n"); |
log_error("close(epoll) error (%d)\n"); |
| 1227 |
} |
} |
| 1229 |
|
|
| 1230 |
if (bbsnet_conf[n].use_ssh) |
if (bbsnet_conf[n].use_ssh) |
| 1231 |
{ |
{ |
| 1232 |
ssh_channel_free(channel); |
if (channel != NULL) |
| 1233 |
ssh_disconnect(session); |
{ |
| 1234 |
ssh_free(session); |
ssh_channel_free(channel); |
| 1235 |
|
} |
| 1236 |
|
if (session != NULL) |
| 1237 |
|
{ |
| 1238 |
|
ssh_disconnect(session); |
| 1239 |
|
ssh_free(session); |
| 1240 |
|
} |
| 1241 |
} |
} |
| 1242 |
|
|
| 1243 |
// Restore STDIN/STDOUT flags |
// Restore STDIN/STDOUT flags |
| 1244 |
fcntl(STDIN_FILENO, F_SETFL, flags_stdin); |
if (flags_stdin != -1 && fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1) |
| 1245 |
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout); |
{ |
| 1246 |
|
log_error("fcntl(F_SETFL) error (%d)\n", errno); |
| 1247 |
|
} |
| 1248 |
|
if (flags_stdout != -1 && fcntl(STDOUT_FILENO, F_SETFL, flags_stdout) == -1) |
| 1249 |
|
{ |
| 1250 |
|
log_error("fcntl(F_SETFL) error (%d)\n", errno); |
| 1251 |
|
} |
| 1252 |
|
|
| 1253 |
if (close(sock) == -1) |
if (sock != -1 && close(sock) == -1) |
| 1254 |
{ |
{ |
| 1255 |
log_error("Close socket failed\n"); |
log_error("Close socket failed\n"); |
| 1256 |
} |
} |
| 1259 |
tm_used = gmtime(&t_used); |
tm_used = gmtime(&t_used); |
| 1260 |
|
|
| 1261 |
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", |
| 1262 |
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); |
|
| 1263 |
|
|
| 1264 |
BBS_last_access_tm = time(NULL); |
BBS_last_access_tm = time(NULL); |
| 1265 |
|
|
| 1297 |
|
|
| 1298 |
moveto(20, 1); |
moveto(20, 1); |
| 1299 |
clrtoeol(); |
clrtoeol(); |
| 1300 |
prints("|\033[1m单位: \033[1;33m%s\033[m%*s 站名: \033[1;33m%s\033[m", |
prints("|\033[1m单位: \033[1;33m%s\033[m%*s 站名: \033[1;33m%s\033[m%*s 类型: \033[1;33m%s\033[m", |
| 1301 |
bbsnet_conf[i].host2, 20 - str_length(bbsnet_conf[i].host2, 1), "", bbsnet_conf[i].host1); |
bbsnet_conf[i].org_name, 20 - str_length(bbsnet_conf[i].org_name, 1), "", |
| 1302 |
|
bbsnet_conf[i].site_name, 20 - str_length(bbsnet_conf[i].site_name, 1), "", |
| 1303 |
|
(bbsnet_conf[i].use_ssh ? "SSH" : "Telnet")); |
| 1304 |
moveto(20, 80); |
moveto(20, 80); |
| 1305 |
prints("|"); |
prints("|"); |
| 1306 |
moveto(21, 1); |
moveto(21, 1); |
| 1307 |
clrtoeol(); |
clrtoeol(); |
| 1308 |
prints("|\033[1m连往: \033[1;33m%-20s\033[m 端口: \033[1;33m%-5d\033[m 类型: \033[1;33m%s\033[m", |
prints("|\033[1m连往: \033[1;33m%-20s\033[m 端口: \033[1;33m%-5d\033[m 编码: \033[1;33m%s\033[m", |
| 1309 |
bbsnet_conf[i].ip, bbsnet_conf[i].port, (bbsnet_conf[i].use_ssh ? "SSH" : "Telnet")); |
bbsnet_conf[i].host_name, bbsnet_conf[i].port, bbsnet_conf[i].charset); |
| 1310 |
moveto(21, 80); |
moveto(21, 80); |
| 1311 |
prints("|"); |
prints("|"); |
| 1312 |
iflush(); |
iflush(); |
| 1318 |
{ |
{ |
| 1319 |
int ch, i; |
int ch, i; |
| 1320 |
|
|
| 1321 |
load_bbsnet_conf(CONF_BBSNET); |
if (load_bbsnet_conf(CONF_BBSNET) < 0) |
| 1322 |
|
{ |
| 1323 |
|
clearscr(); |
| 1324 |
|
moveto(1, 1); |
| 1325 |
|
prints("加载穿梭配置失败!"); |
| 1326 |
|
press_any_key(); |
| 1327 |
|
return -1; |
| 1328 |
|
} |
| 1329 |
|
|
| 1330 |
bbsnet_refresh(); |
bbsnet_refresh(); |
| 1331 |
display_menu(&bbsnet_menu); |
display_menu(&bbsnet_menu); |
| 1343 |
switch (ch) |
switch (ch) |
| 1344 |
{ |
{ |
| 1345 |
case KEY_NULL: // broken pipe |
case KEY_NULL: // broken pipe |
| 1346 |
|
#ifdef _DEBUG |
| 1347 |
log_error("KEY_NULL\n"); |
log_error("KEY_NULL\n"); |
| 1348 |
|
#endif |
| 1349 |
goto cleanup; |
goto cleanup; |
| 1350 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 1351 |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |