| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
login.c - description |
/* |
| 3 |
------------------- |
* login |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - user authentication and online status manager |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
| 8 |
/*************************************************************************** |
|
| 9 |
* * |
#ifdef HAVE_CONFIG_H |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
#include "config.h" |
| 11 |
* it under the terms of the GNU General Public License as published by * |
#endif |
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 12 |
|
|
| 13 |
#include "bbs.h" |
#include "bbs.h" |
| 14 |
#include "common.h" |
#include "common.h" |
| 15 |
#include "database.h" |
#include "database.h" |
| 16 |
#include "io.h" |
#include "io.h" |
| 17 |
|
#include "ip_mask.h" |
| 18 |
#include "log.h" |
#include "log.h" |
| 19 |
#include "login.h" |
#include "login.h" |
| 20 |
#include "screen.h" |
#include "screen.h" |
| 25 |
#include <string.h> |
#include <string.h> |
| 26 |
#include <regex.h> |
#include <regex.h> |
| 27 |
#include <unistd.h> |
#include <unistd.h> |
| 28 |
#include <mysql/mysql.h> |
#include <mysql.h> |
| 29 |
|
#include <sys/param.h> |
| 30 |
|
|
| 31 |
|
static const int BBS_username_min_len = 3; // common len = 5, special len = 3 |
| 32 |
|
static const int BBS_password_min_len = 5; // legacy len = 5, current len = 6 |
| 33 |
|
|
| 34 |
|
static const int BBS_allowed_login_failures_within_interval = 10; |
| 35 |
|
static const int BBS_login_failures_count_interval = 10; // minutes |
| 36 |
|
static const int BBS_allowed_login_failures_per_account = 3; |
| 37 |
|
|
| 38 |
|
const int BBS_login_retry_times = 3; |
| 39 |
|
|
| 40 |
int bbs_login(void) |
int bbs_login(void) |
| 41 |
{ |
{ |
| 43 |
char password[BBS_password_max_len + 1]; |
char password[BBS_password_max_len + 1]; |
| 44 |
int i = 0; |
int i = 0; |
| 45 |
int ok = 0; |
int ok = 0; |
| 46 |
|
int ret; |
| 47 |
|
|
| 48 |
for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++) |
for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++) |
| 49 |
{ |
{ |
| 50 |
prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', " |
prints("\033[1;33m璇疯緭鍏ュ笎鍙穃033[m(璇曠敤璇疯緭鍏\033[1;36mguest\033[m', " |
| 51 |
"注册请输入`\033[1;31mnew\033[m'): "); |
"娉ㄥ唽璇疯緭鍏\033[1;31mnew\033[m'): "); |
| 52 |
iflush(); |
iflush(); |
| 53 |
|
|
| 54 |
if (str_input(username, sizeof(username), DOECHO) < 0) |
if (str_input(username, sizeof(username), DOECHO) < 0) |
| 67 |
{ |
{ |
| 68 |
display_file(DATA_REGISTER, 1); |
display_file(DATA_REGISTER, 1); |
| 69 |
|
|
| 70 |
return 0; |
return -1; |
| 71 |
} |
} |
| 72 |
|
|
| 73 |
if (username[0] != '\0') |
if (username[0] != '\0') |
| 74 |
{ |
{ |
| 75 |
prints("\033[1;37m请输入密码\033[m: "); |
prints("\033[1;37m璇疯緭鍏ュ瘑鐮乗033[m: "); |
| 76 |
iflush(); |
iflush(); |
| 77 |
|
|
| 78 |
if (str_input(password, sizeof(password), NOECHO) < 0) |
if (str_input(password, sizeof(password), NOECHO) < 0) |
| 80 |
continue; |
continue; |
| 81 |
} |
} |
| 82 |
|
|
| 83 |
ok = (check_user(username, password) == 0); |
ret = check_user(username, password); |
| 84 |
|
if (ret == 2) // Enforce update user agreement |
| 85 |
|
{ |
| 86 |
|
BBS_update_eula = 1; |
| 87 |
|
ret = 0; |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
ok = (ret == 0); |
| 91 |
iflush(); |
iflush(); |
| 92 |
} |
} |
| 93 |
} |
} |
| 98 |
return -1; |
return -1; |
| 99 |
} |
} |
| 100 |
|
|
|
log_common("User \"%s\"(%ld) login from %s:%d\n", |
|
|
BBS_username, BBS_priv.uid, hostaddr_client, port_client); |
|
|
|
|
| 101 |
return 0; |
return 0; |
| 102 |
} |
} |
| 103 |
|
|
| 124 |
// Verify format |
// Verify format |
| 125 |
for (i = 0; ok && username[i] != '\0'; i++) |
for (i = 0; ok && username[i] != '\0'; i++) |
| 126 |
{ |
{ |
| 127 |
if (!(isalpha(username[i]) || (i > 0 && isdigit(username[i])))) |
if (!(isalpha((int)username[i]) || (i > 0 && (isdigit((int)username[i]) || username[i] == '_')))) |
| 128 |
{ |
{ |
| 129 |
ok = 0; |
ok = 0; |
| 130 |
} |
} |
| 131 |
} |
} |
| 132 |
if (ok && (i < 3 || i > 12)) |
if (ok && (i < BBS_username_min_len || i > BBS_username_max_len)) |
| 133 |
{ |
{ |
| 134 |
ok = 0; |
ok = 0; |
| 135 |
} |
} |
| 136 |
for (i = 0; ok && password[i] != '\0'; i++) |
for (i = 0; ok && password[i] != '\0'; i++) |
| 137 |
{ |
{ |
| 138 |
if (!isalnum(password[i])) |
if (!isalnum((int)password[i])) |
| 139 |
{ |
{ |
| 140 |
ok = 0; |
ok = 0; |
| 141 |
} |
} |
| 142 |
} |
} |
| 143 |
if (ok && (i < 5 || i > 12)) |
if (ok && (i < BBS_password_min_len || i > BBS_password_max_len)) |
| 144 |
{ |
{ |
| 145 |
ok = 0; |
ok = 0; |
| 146 |
} |
} |
| 147 |
|
|
| 148 |
if (!ok) |
if (!ok) |
| 149 |
{ |
{ |
| 150 |
prints("\033[1;31m用户名或密码格式错误...\033[m\r\n"); |
prints("\033[1;31m鐢ㄦ埛鍚嶆垨瀵嗙爜鏍煎紡閿欒...\033[m\r\n"); |
| 151 |
ret = 1; |
ret = 1; |
| 152 |
goto cleanup; |
goto cleanup; |
| 153 |
} |
} |
| 191 |
} |
} |
| 192 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 193 |
{ |
{ |
| 194 |
if (atoi(row[0]) > BBS_allowed_login_failures_within_interval) |
if (atoi(row[0]) >= BBS_allowed_login_failures_within_interval) |
| 195 |
{ |
{ |
| 196 |
prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n"); |
prints("\033[1;31m鏉ユ簮瀛樺湪澶氭澶辫触鐧婚檰灏濊瘯锛岃绋嶅悗鍐嶈瘯锛屾垨浣跨敤Web鏂瑰紡璁块棶\033[m\r\n"); |
| 197 |
ret = 1; |
ret = 1; |
| 198 |
goto cleanup; |
goto cleanup; |
| 199 |
} |
} |
| 201 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 202 |
rs = NULL; |
rs = NULL; |
| 203 |
|
|
| 204 |
// Failed login attempts against the current username during certain time period |
// Failed login attempts against the current username since last successful login |
| 205 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 206 |
"SELECT COUNT(*) AS err_count FROM user_err_login_log " |
"SELECT COUNT(*) AS err_count FROM user_err_login_log " |
| 207 |
"WHERE username = '%s' AND login_dt >= SUBDATE(NOW(), INTERVAL 1 DAY)", |
"LEFT JOIN user_list ON user_err_login_log.username = user_list.username " |
| 208 |
|
"LEFT JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID " |
| 209 |
|
"WHERE user_err_login_log.username = '%s' " |
| 210 |
|
"AND (user_err_login_log.login_dt >= user_pubinfo.last_login_dt " |
| 211 |
|
"OR user_pubinfo.last_login_dt IS NULL)", |
| 212 |
username); |
username); |
| 213 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 214 |
{ |
{ |
| 224 |
} |
} |
| 225 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 226 |
{ |
{ |
| 227 |
if (atoi(row[0]) >= 5) |
if (atoi(row[0]) >= BBS_allowed_login_failures_per_account) |
| 228 |
{ |
{ |
| 229 |
prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n"); |
prints("\033[1;31m璐︽埛瀛樺湪澶氭澶辫触鐧婚檰灏濊瘯锛岃浣跨敤Web鏂瑰紡鐧诲綍瑙i攣\033[m\r\n"); |
| 230 |
ret = 1; |
ret = 1; |
| 231 |
goto cleanup; |
goto cleanup; |
| 232 |
} |
} |
| 282 |
|
|
| 283 |
if (p_login == 0) |
if (p_login == 0) |
| 284 |
{ |
{ |
| 285 |
prints("\033[1;31m您目前无权登陆...\033[m\r\n"); |
prints("\033[1;31m鎮ㄧ洰鍓嶆棤鏉冪櫥闄...\033[m\r\n"); |
| 286 |
ret = 1; |
ret = 1; |
| 287 |
goto cleanup; |
goto cleanup; |
| 288 |
} |
} |
| 311 |
goto cleanup; |
goto cleanup; |
| 312 |
} |
} |
| 313 |
|
|
| 314 |
prints("\033[1;31m错误的用户名或密码...\033[m\r\n"); |
prints("\033[1;31m閿欒鐨勭敤鎴峰悕鎴栧瘑鐮...\033[m\r\n"); |
| 315 |
ret = 1; |
ret = 1; |
| 316 |
goto cleanup; |
goto cleanup; |
| 317 |
} |
} |
| 324 |
goto cleanup; |
goto cleanup; |
| 325 |
} |
} |
| 326 |
|
|
| 327 |
|
if (!SSH_v2 && checklevel2(&BBS_priv, P_MAN_S)) |
| 328 |
|
{ |
| 329 |
|
prints("\033[1;31m闈炴櫘閫氳处鎴峰繀椤讳娇鐢⊿SH鏂瑰紡鐧诲綍\033[m\r\n"); |
| 330 |
|
ret = 1; |
| 331 |
|
goto cleanup; |
| 332 |
|
} |
| 333 |
|
|
| 334 |
ret = load_user_info(db, BBS_uid); |
ret = load_user_info(db, BBS_uid); |
| 335 |
|
|
| 336 |
switch (ret) |
switch (ret) |
| 338 |
case 0: // Login successfully |
case 0: // Login successfully |
| 339 |
break; |
break; |
| 340 |
case -1: // Load data error |
case -1: // Load data error |
| 341 |
prints("\033[1;31m读取用户数据错误...\033[m\r\n"); |
prints("\033[1;31m璇诲彇鐢ㄦ埛鏁版嵁閿欒...\033[m\r\n"); |
| 342 |
ret = -1; |
ret = -1; |
| 343 |
goto cleanup; |
goto cleanup; |
| 344 |
case -2: // Unused |
case -2: // Enforce update user agreement |
| 345 |
prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n"); |
ret = 2; |
|
ret = 1; |
|
| 346 |
goto cleanup; |
goto cleanup; |
| 347 |
case -3: // Dead |
case -3: // Dead |
| 348 |
prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n"); |
prints("\033[1;31m寰堥仐鎲撅紝鎮ㄥ凡缁忔案杩滅寮浜嗘垜浠殑涓栫晫锛乗033[m\r\n"); |
| 349 |
ret = 1; |
ret = 1; |
| 350 |
goto cleanup; |
goto cleanup; |
| 351 |
default: |
default: |
| 450 |
|
|
| 451 |
if (load_priv(db, &BBS_priv, BBS_uid) != 0) |
if (load_priv(db, &BBS_priv, BBS_uid) != 0) |
| 452 |
{ |
{ |
| 453 |
ret = -1; |
ret = -1; // Data not found |
| 454 |
|
goto cleanup; |
| 455 |
|
} |
| 456 |
|
|
| 457 |
|
if (last_login_dt < BBS_eula_tm) |
| 458 |
|
{ |
| 459 |
|
ret = -2; // require update agreement first |
| 460 |
goto cleanup; |
goto cleanup; |
| 461 |
} |
} |
| 462 |
|
|
| 510 |
{ |
{ |
| 511 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 512 |
|
|
| 513 |
if (user_online_del(db) != 0) |
snprintf(sql, sizeof(sql), |
| 514 |
|
"INSERT INTO visit_log(dt, IP) VALUES(NOW(), '%s')", |
| 515 |
|
hostaddr_client); |
| 516 |
|
if (mysql_query(db, sql) != 0) |
| 517 |
{ |
{ |
| 518 |
|
log_error("Add visit log error: %s\n", mysql_error(db)); |
| 519 |
return -1; |
return -1; |
| 520 |
} |
} |
| 521 |
|
|
| 522 |
|
if (user_online_del(db) != 0) |
| 523 |
|
{ |
| 524 |
|
return -2; |
| 525 |
|
} |
| 526 |
|
|
| 527 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 528 |
"INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) " |
"INSERT INTO user_online(SID, UID, ip, current_action, login_tm, last_tm) " |
| 529 |
"VALUES('Telnet_Process_%d', %d, '%s', NOW(), NOW())", |
"VALUES('Telnet_Process_%d', %d, '%s', 'LOGIN', NOW(), NOW())", |
| 530 |
getpid(), BBS_priv.uid, hostaddr_client); |
getpid(), BBS_priv.uid, hostaddr_client); |
| 531 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 532 |
{ |
{ |
| 533 |
log_error("Add user_online error: %s\n", mysql_error(db)); |
log_error("Add user_online error: %s\n", mysql_error(db)); |
| 534 |
return -1; |
return -3; |
| 535 |
} |
} |
| 536 |
|
|
| 537 |
return 0; |
return 0; |
| 553 |
return 0; |
return 0; |
| 554 |
} |
} |
| 555 |
|
|
| 556 |
|
int user_online_exp(MYSQL *db) |
| 557 |
|
{ |
| 558 |
|
char sql[SQL_BUFFER_LEN]; |
| 559 |
|
|
| 560 |
|
// +1 exp for every 5 minutes online since last logout |
| 561 |
|
// but at most 24 hours worth of exp can be gained in Telnet session |
| 562 |
|
snprintf(sql, sizeof(sql), |
| 563 |
|
"UPDATE user_pubinfo SET exp = exp + FLOOR(LEAST(TIMESTAMPDIFF(" |
| 564 |
|
"SECOND, GREATEST(last_login_dt, IF(last_logout_dt IS NULL, last_login_dt, last_logout_dt)), NOW()" |
| 565 |
|
") / 60 / 5, 12 * 24)), last_logout_dt = NOW() " |
| 566 |
|
"WHERE UID = %d", |
| 567 |
|
BBS_priv.uid); |
| 568 |
|
if (mysql_query(db, sql) != 0) |
| 569 |
|
{ |
| 570 |
|
log_error("Update user_pubinfo error: %s\n", mysql_error(db)); |
| 571 |
|
return -1; |
| 572 |
|
} |
| 573 |
|
|
| 574 |
|
return 0; |
| 575 |
|
} |
| 576 |
|
|
| 577 |
int user_online_update(const char *action) |
int user_online_update(const char *action) |
| 578 |
{ |
{ |
| 579 |
MYSQL *db = NULL; |
MYSQL *db = NULL; |
| 580 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 581 |
|
|
| 582 |
if (strcmp(BBS_current_action, action) == 0) // No change |
if ((action == NULL || strcmp(BBS_current_action, action) == 0) && |
| 583 |
|
time(NULL) - BBS_current_action_tm < BBS_current_action_refresh_interval) // No change |
| 584 |
{ |
{ |
| 585 |
return 0; |
return 0; |
| 586 |
} |
} |
| 587 |
|
|
| 588 |
strncpy(BBS_current_action, action, sizeof(BBS_current_action) - 1); |
if (action != NULL) |
| 589 |
BBS_current_action[sizeof(BBS_current_action) - 1] = '\0'; |
{ |
| 590 |
|
strncpy(BBS_current_action, action, sizeof(BBS_current_action) - 1); |
| 591 |
|
BBS_current_action[sizeof(BBS_current_action) - 1] = '\0'; |
| 592 |
|
} |
| 593 |
|
|
| 594 |
|
BBS_current_action_tm = time(NULL); |
| 595 |
|
|
| 596 |
db = db_open(); |
db = db_open(); |
| 597 |
if (db == NULL) |
if (db == NULL) |
| 601 |
} |
} |
| 602 |
|
|
| 603 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 604 |
"UPDATE user_online SET current_action = '%s', last_tm=NOW() " |
"UPDATE user_online SET current_action = '%s', last_tm = NOW() " |
| 605 |
"WHERE SID = 'Telnet_Process_%d'", |
"WHERE SID = 'Telnet_Process_%d'", |
| 606 |
BBS_current_action, getpid()); |
BBS_current_action, getpid()); |
| 607 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 614 |
|
|
| 615 |
return 1; |
return 1; |
| 616 |
} |
} |
| 617 |
|
|
| 618 |
|
int user_update_agreement(void) |
| 619 |
|
{ |
| 620 |
|
MYSQL *db = NULL; |
| 621 |
|
char sql[SQL_BUFFER_LEN]; |
| 622 |
|
int ch; |
| 623 |
|
int ret = 0; |
| 624 |
|
|
| 625 |
|
clearscr(); |
| 626 |
|
moveto(2, 1); |
| 627 |
|
prints("灏婃暚鐨勭敤鎴凤細"); |
| 628 |
|
moveto(3, 1); |
| 629 |
|
prints(" 鏈珯鐨勩婄敤鎴疯鍙崗璁嬪凡鏇存柊锛屾偍蹇呴』鍦ㄤ粩缁嗛槄璇诲苟鎺ュ彈鍚庯紝鎵嶈兘缁х画浣跨敤鏈珯鎻愪緵鐨勬湇鍔°"); |
| 630 |
|
press_any_key(); |
| 631 |
|
|
| 632 |
|
clearscr(); |
| 633 |
|
display_file(DATA_EULA, 1); |
| 634 |
|
|
| 635 |
|
while (!SYS_server_exit) |
| 636 |
|
{ |
| 637 |
|
clearscr(); |
| 638 |
|
moveto(2, 1); |
| 639 |
|
|
| 640 |
|
ch = press_any_key_ex("鎮ㄦ槸鍚︽帴鍙楁湰绔欑殑銆婄敤鎴疯鍙崗璁? (A)鎺ュ彈, (D)鎷掔粷, (R)鍐嶇湅鐪嬪崗璁 [D]", 60); |
| 641 |
|
switch (toupper(ch)) |
| 642 |
|
{ |
| 643 |
|
case KEY_NULL: |
| 644 |
|
return -1; |
| 645 |
|
case KEY_TIMEOUT: |
| 646 |
|
case CR: |
| 647 |
|
case 'D': |
| 648 |
|
moveto(3, 1); |
| 649 |
|
prints("鎮ㄥ凡鎷掔粷銆婄敤鎴疯鍙崗璁嬶紝鍐嶈锛"); |
| 650 |
|
press_any_key(); |
| 651 |
|
return 0; |
| 652 |
|
case 'R': |
| 653 |
|
clearscr(); |
| 654 |
|
display_file(DATA_EULA, 1); |
| 655 |
|
continue; |
| 656 |
|
case 'A': |
| 657 |
|
db = db_open(); |
| 658 |
|
if (db == NULL) |
| 659 |
|
{ |
| 660 |
|
ret = -1; |
| 661 |
|
goto cleanup; |
| 662 |
|
} |
| 663 |
|
|
| 664 |
|
snprintf(sql, sizeof(sql), |
| 665 |
|
"UPDATE user_pubinfo SET visit_count = visit_count + 1, " |
| 666 |
|
"last_login_dt = NOW() WHERE UID = %d", |
| 667 |
|
BBS_priv.uid); |
| 668 |
|
if (mysql_query(db, sql) != 0) |
| 669 |
|
{ |
| 670 |
|
log_error("Update user_pubinfo error: %s\n", mysql_error(db)); |
| 671 |
|
ret = -1; |
| 672 |
|
goto cleanup; |
| 673 |
|
} |
| 674 |
|
|
| 675 |
|
mysql_close(db); |
| 676 |
|
db = NULL; |
| 677 |
|
|
| 678 |
|
moveto(3, 1); |
| 679 |
|
prints("鎮ㄥ凡鎺ュ彈銆婄敤鎴疯鍙崗璁嬶紝璇烽噸鏂扮櫥闄嗐"); |
| 680 |
|
press_any_key(); |
| 681 |
|
return 1; |
| 682 |
|
default: |
| 683 |
|
continue; |
| 684 |
|
} |
| 685 |
|
|
| 686 |
|
break; |
| 687 |
|
} |
| 688 |
|
|
| 689 |
|
cleanup: |
| 690 |
|
mysql_close(db); |
| 691 |
|
|
| 692 |
|
return ret; |
| 693 |
|
} |