| 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" |
| 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> |
#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 |
{ |
{ |
| 42 |
char username[BBS_username_max_len + 1]; |
char username[BBS_username_max_len + 1]; |
| 43 |
char password[BBS_password_max_len + 1]; |
char password[BBS_password_max_len + 1]; |
| 44 |
int i = 0; |
int i; |
| 45 |
int ok = 0; |
int ret; |
| 46 |
|
|
| 47 |
for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++) |
for (i = 0; !SYS_server_exit && i < BBS_login_retry_times; i++) |
| 48 |
{ |
{ |
| 49 |
prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', " |
prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', " |
| 50 |
"注册请输入`\033[1;31mnew\033[m'): "); |
"注册请输入`\033[1;31mnew\033[m'): "); |
| 58 |
if (strcmp(username, "guest") == 0) |
if (strcmp(username, "guest") == 0) |
| 59 |
{ |
{ |
| 60 |
load_guest_info(); |
load_guest_info(); |
| 61 |
|
log_common("User [%s] authenticated successfully", username); |
| 62 |
return 0; |
return 0; |
| 63 |
} |
} |
| 64 |
|
|
| 65 |
if (strcmp(username, "new") == 0) |
if (strcmp(username, "new") == 0) |
| 66 |
{ |
{ |
| 67 |
display_file(DATA_REGISTER, 1); |
display_file(DATA_REGISTER, 1); |
|
|
|
| 68 |
return -1; |
return -1; |
| 69 |
} |
} |
| 70 |
|
|
| 78 |
continue; |
continue; |
| 79 |
} |
} |
| 80 |
|
|
| 81 |
ok = (check_user(username, password) == 0); |
ret = check_user(username, password); |
| 82 |
|
if (ret == 2) // Enforce update user agreement |
| 83 |
|
{ |
| 84 |
|
BBS_update_eula = 1; |
| 85 |
|
ret = 0; |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
if (ret == 0) |
| 89 |
|
{ |
| 90 |
|
log_common("User [%s] authenticated successfully", username); |
| 91 |
|
return 0; |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
log_common("User [%s] authentication failed (%d/%d)", username, |
| 95 |
|
i + 1, BBS_login_retry_times); |
| 96 |
iflush(); |
iflush(); |
| 97 |
} |
} |
| 98 |
} |
} |
| 99 |
|
|
| 100 |
if (!ok) |
display_file(DATA_LOGIN_ERROR, 1); |
| 101 |
{ |
return -1; |
|
display_file(DATA_LOGIN_ERROR, 1); |
|
|
return -1; |
|
|
} |
|
|
|
|
|
log_common("User \"%s\"(%ld) login from %s:%d\n", |
|
|
BBS_username, BBS_priv.uid, hostaddr_client, port_client); |
|
|
|
|
|
return 0; |
|
| 102 |
} |
} |
| 103 |
|
|
| 104 |
int check_user(const char *username, const char *password) |
int check_user(const char *username, const char *password) |
| 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]) || 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 < 5 || i > BBS_username_max_len)) |
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 > BBS_password_max_len)) |
if (ok && (i < BBS_password_min_len || i > BBS_password_max_len)) |
| 144 |
{ |
{ |
| 145 |
ok = 0; |
ok = 0; |
| 146 |
} |
} |
| 155 |
// Begin transaction |
// Begin transaction |
| 156 |
if (mysql_query(db, "SET autocommit=0") != 0) |
if (mysql_query(db, "SET autocommit=0") != 0) |
| 157 |
{ |
{ |
| 158 |
log_error("SET autocommit=0 error: %s\n", mysql_error(db)); |
log_error("SET autocommit=0 error: %s", mysql_error(db)); |
| 159 |
ret = -1; |
ret = -1; |
| 160 |
goto cleanup; |
goto cleanup; |
| 161 |
} |
} |
| 162 |
|
|
| 163 |
if (mysql_query(db, "BEGIN") != 0) |
if (mysql_query(db, "BEGIN") != 0) |
| 164 |
{ |
{ |
| 165 |
log_error("Begin transaction error: %s\n", mysql_error(db)); |
log_error("Begin transaction error: %s", mysql_error(db)); |
| 166 |
ret = -1; |
ret = -1; |
| 167 |
goto cleanup; |
goto cleanup; |
| 168 |
} |
} |
| 179 |
ip_mask(client_addr, 1, '%')); |
ip_mask(client_addr, 1, '%')); |
| 180 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 181 |
{ |
{ |
| 182 |
log_error("Query user_list error: %s\n", mysql_error(db)); |
log_error("Query user_list error: %s", mysql_error(db)); |
| 183 |
ret = -1; |
ret = -1; |
| 184 |
goto cleanup; |
goto cleanup; |
| 185 |
} |
} |
| 186 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 187 |
{ |
{ |
| 188 |
log_error("Get user_list data failed\n"); |
log_error("Get user_list data failed"); |
| 189 |
ret = -1; |
ret = -1; |
| 190 |
goto cleanup; |
goto cleanup; |
| 191 |
} |
} |
| 212 |
username); |
username); |
| 213 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 214 |
{ |
{ |
| 215 |
log_error("Query user_list error: %s\n", mysql_error(db)); |
log_error("Query user_list error: %s", mysql_error(db)); |
| 216 |
ret = -1; |
ret = -1; |
| 217 |
goto cleanup; |
goto cleanup; |
| 218 |
} |
} |
| 219 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 220 |
{ |
{ |
| 221 |
log_error("Get user_list data failed\n"); |
log_error("Get user_list data failed"); |
| 222 |
ret = -1; |
ret = -1; |
| 223 |
goto cleanup; |
goto cleanup; |
| 224 |
} |
} |
| 240 |
username, password); |
username, password); |
| 241 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 242 |
{ |
{ |
| 243 |
log_error("Query user_list error: %s\n", mysql_error(db)); |
log_error("Query user_list error: %s", mysql_error(db)); |
| 244 |
ret = -1; |
ret = -1; |
| 245 |
goto cleanup; |
goto cleanup; |
| 246 |
} |
} |
| 247 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 248 |
{ |
{ |
| 249 |
log_error("Get user_list data failed\n"); |
log_error("Get user_list data failed"); |
| 250 |
ret = -1; |
ret = -1; |
| 251 |
goto cleanup; |
goto cleanup; |
| 252 |
} |
} |
| 267 |
BBS_uid, hostaddr_client); |
BBS_uid, hostaddr_client); |
| 268 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 269 |
{ |
{ |
| 270 |
log_error("Insert into user_login_log error: %s\n", mysql_error(db)); |
log_error("Insert into user_login_log error: %s", mysql_error(db)); |
| 271 |
ret = -1; |
ret = -1; |
| 272 |
goto cleanup; |
goto cleanup; |
| 273 |
} |
} |
| 275 |
// Commit transaction |
// Commit transaction |
| 276 |
if (mysql_query(db, "COMMIT") != 0) |
if (mysql_query(db, "COMMIT") != 0) |
| 277 |
{ |
{ |
| 278 |
log_error("Commit transaction error: %s\n", mysql_error(db)); |
log_error("Commit transaction error: %s", mysql_error(db)); |
| 279 |
ret = -1; |
ret = -1; |
| 280 |
goto cleanup; |
goto cleanup; |
| 281 |
} |
} |
| 298 |
username, password, hostaddr_client); |
username, password, hostaddr_client); |
| 299 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 300 |
{ |
{ |
| 301 |
log_error("Insert into user_err_login_log error: %s\n", mysql_error(db)); |
log_error("Insert into user_err_login_log error: %s", mysql_error(db)); |
| 302 |
ret = -1; |
ret = -1; |
| 303 |
goto cleanup; |
goto cleanup; |
| 304 |
} |
} |
| 306 |
// Commit transaction |
// Commit transaction |
| 307 |
if (mysql_query(db, "COMMIT") != 0) |
if (mysql_query(db, "COMMIT") != 0) |
| 308 |
{ |
{ |
| 309 |
log_error("Commit transaction error: %s\n", mysql_error(db)); |
log_error("Commit transaction error: %s", mysql_error(db)); |
| 310 |
ret = -1; |
ret = -1; |
| 311 |
goto cleanup; |
goto cleanup; |
| 312 |
} |
} |
| 319 |
// Set AUTOCOMMIT = 1 |
// Set AUTOCOMMIT = 1 |
| 320 |
if (mysql_query(db, "SET autocommit=1") != 0) |
if (mysql_query(db, "SET autocommit=1") != 0) |
| 321 |
{ |
{ |
| 322 |
log_error("SET autocommit=1 error: %s\n", mysql_error(db)); |
log_error("SET autocommit=1 error: %s", mysql_error(db)); |
| 323 |
ret = -1; |
ret = -1; |
| 324 |
goto cleanup; |
goto cleanup; |
| 325 |
} |
} |
| 329 |
switch (ret) |
switch (ret) |
| 330 |
{ |
{ |
| 331 |
case 0: // Login successfully |
case 0: // Login successfully |
| 332 |
|
if (!SSH_v2 && checklevel2(&BBS_priv, P_MAN_S)) |
| 333 |
|
{ |
| 334 |
|
prints("\033[1;31m非普通账户必须使用SSH方式登录\033[m\r\n"); |
| 335 |
|
ret = 1; |
| 336 |
|
goto cleanup; |
| 337 |
|
} |
| 338 |
break; |
break; |
| 339 |
case -1: // Load data error |
case -1: // Load data error |
| 340 |
prints("\033[1;31m读取用户数据错误...\033[m\r\n"); |
prints("\033[1;31m读取用户数据错误...\033[m\r\n"); |
| 341 |
ret = -1; |
ret = -1; |
| 342 |
goto cleanup; |
goto cleanup; |
| 343 |
case -2: // Unused |
case -2: // Enforce update user agreement |
| 344 |
prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n"); |
if (!SSH_v2 && checklevel2(&BBS_priv, P_MAN_S)) |
| 345 |
ret = 1; |
{ |
| 346 |
|
prints("\033[1;31m非普通账户必须使用SSH方式登录\033[m\r\n"); |
| 347 |
|
ret = 1; |
| 348 |
|
goto cleanup; |
| 349 |
|
} |
| 350 |
|
ret = 2; |
| 351 |
goto cleanup; |
goto cleanup; |
| 352 |
case -3: // Dead |
case -3: // Dead |
| 353 |
prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n"); |
prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n"); |
| 364 |
BBS_uid); |
BBS_uid); |
| 365 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 366 |
{ |
{ |
| 367 |
log_error("Update user_pubinfo error: %s\n", mysql_error(db)); |
log_error("Update user_pubinfo error: %s", mysql_error(db)); |
| 368 |
ret = -1; |
ret = -1; |
| 369 |
goto cleanup; |
goto cleanup; |
| 370 |
} |
} |
| 386 |
|
|
| 387 |
if (setenv("TZ", user_tz_env, 1) == -1) |
if (setenv("TZ", user_tz_env, 1) == -1) |
| 388 |
{ |
{ |
| 389 |
log_error("setenv(TZ = %s) error %d\n", user_tz_env, errno); |
log_error("setenv(TZ = %s) error %d", user_tz_env, errno); |
| 390 |
return -3; |
return -3; |
| 391 |
} |
} |
| 392 |
|
|
| 393 |
tzset(); |
tzset(); |
| 394 |
} |
} |
| 395 |
|
|
| 396 |
|
log_common("User [%s] authenticated successfully", username); |
| 397 |
|
|
| 398 |
cleanup: |
cleanup: |
| 399 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 400 |
mysql_close(db); |
mysql_close(db); |
| 417 |
BBS_uid); |
BBS_uid); |
| 418 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 419 |
{ |
{ |
| 420 |
log_error("Query user_pubinfo error: %s\n", mysql_error(db)); |
log_error("Query user_pubinfo error: %s", mysql_error(db)); |
| 421 |
ret = -1; |
ret = -1; |
| 422 |
goto cleanup; |
goto cleanup; |
| 423 |
} |
} |
| 424 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 425 |
{ |
{ |
| 426 |
log_error("Get user_pubinfo data failed\n"); |
log_error("Get user_pubinfo data failed"); |
| 427 |
ret = -1; |
ret = -1; |
| 428 |
goto cleanup; |
goto cleanup; |
| 429 |
} |
} |
| 457 |
|
|
| 458 |
if (load_priv(db, &BBS_priv, BBS_uid) != 0) |
if (load_priv(db, &BBS_priv, BBS_uid) != 0) |
| 459 |
{ |
{ |
| 460 |
ret = -1; |
ret = -1; // Data not found |
| 461 |
|
goto cleanup; |
| 462 |
|
} |
| 463 |
|
|
| 464 |
|
if (last_login_dt < BBS_eula_tm) |
| 465 |
|
{ |
| 466 |
|
ret = -2; // require update agreement first |
| 467 |
goto cleanup; |
goto cleanup; |
| 468 |
} |
} |
| 469 |
|
|
| 522 |
hostaddr_client); |
hostaddr_client); |
| 523 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 524 |
{ |
{ |
| 525 |
log_error("Add visit log error: %s\n", mysql_error(db)); |
log_error("Add visit log error: %s", mysql_error(db)); |
| 526 |
return -1; |
return -1; |
| 527 |
} |
} |
| 528 |
|
|
| 537 |
getpid(), BBS_priv.uid, hostaddr_client); |
getpid(), BBS_priv.uid, hostaddr_client); |
| 538 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 539 |
{ |
{ |
| 540 |
log_error("Add user_online error: %s\n", mysql_error(db)); |
log_error("Add user_online error: %s", mysql_error(db)); |
| 541 |
return -3; |
return -3; |
| 542 |
} |
} |
| 543 |
|
|
| 553 |
getpid()); |
getpid()); |
| 554 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 555 |
{ |
{ |
| 556 |
log_error("Delete user_online error: %s\n", mysql_error(db)); |
log_error("Delete user_online error: %s", mysql_error(db)); |
| 557 |
return -1; |
return -1; |
| 558 |
} |
} |
| 559 |
|
|
| 574 |
BBS_priv.uid); |
BBS_priv.uid); |
| 575 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 576 |
{ |
{ |
| 577 |
log_error("Update user_pubinfo error: %s\n", mysql_error(db)); |
log_error("Update user_pubinfo error: %s", mysql_error(db)); |
| 578 |
return -1; |
return -1; |
| 579 |
} |
} |
| 580 |
|
|
| 603 |
db = db_open(); |
db = db_open(); |
| 604 |
if (db == NULL) |
if (db == NULL) |
| 605 |
{ |
{ |
| 606 |
log_error("db_open() error: %s\n", mysql_error(db)); |
log_error("db_open() error: %s", mysql_error(db)); |
| 607 |
return -1; |
return -1; |
| 608 |
} |
} |
| 609 |
|
|
| 613 |
BBS_current_action, getpid()); |
BBS_current_action, getpid()); |
| 614 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 615 |
{ |
{ |
| 616 |
log_error("Update user_online error: %s\n", mysql_error(db)); |
log_error("Update user_online error: %s", mysql_error(db)); |
| 617 |
return -2; |
return -2; |
| 618 |
} |
} |
| 619 |
|
|
| 621 |
|
|
| 622 |
return 1; |
return 1; |
| 623 |
} |
} |
| 624 |
|
|
| 625 |
|
int user_update_agreement(void) |
| 626 |
|
{ |
| 627 |
|
MYSQL *db = NULL; |
| 628 |
|
char sql[SQL_BUFFER_LEN]; |
| 629 |
|
int ch; |
| 630 |
|
int ret = 0; |
| 631 |
|
|
| 632 |
|
clearscr(); |
| 633 |
|
moveto(2, 1); |
| 634 |
|
prints("尊敬的用户:"); |
| 635 |
|
moveto(3, 1); |
| 636 |
|
prints(" 本站的《用户许可协议》已更新,您必须在仔细阅读并接受后,才能继续使用本站提供的服务。"); |
| 637 |
|
press_any_key(); |
| 638 |
|
|
| 639 |
|
clearscr(); |
| 640 |
|
display_file(DATA_EULA, 1); |
| 641 |
|
|
| 642 |
|
while (!SYS_server_exit) |
| 643 |
|
{ |
| 644 |
|
clearscr(); |
| 645 |
|
moveto(2, 1); |
| 646 |
|
|
| 647 |
|
ch = press_any_key_ex("您是否接受本站的《用户许可协议》? (A)接受, (D)拒绝, (R)再看看协议 [D]", 60); |
| 648 |
|
switch (toupper(ch)) |
| 649 |
|
{ |
| 650 |
|
case KEY_NULL: |
| 651 |
|
return -1; |
| 652 |
|
case KEY_TIMEOUT: |
| 653 |
|
case CR: |
| 654 |
|
case 'D': |
| 655 |
|
moveto(3, 1); |
| 656 |
|
prints("您已拒绝《用户许可协议》,再见!"); |
| 657 |
|
press_any_key(); |
| 658 |
|
return 0; |
| 659 |
|
case 'R': |
| 660 |
|
clearscr(); |
| 661 |
|
display_file(DATA_EULA, 1); |
| 662 |
|
continue; |
| 663 |
|
case 'A': |
| 664 |
|
db = db_open(); |
| 665 |
|
if (db == NULL) |
| 666 |
|
{ |
| 667 |
|
ret = -1; |
| 668 |
|
goto cleanup; |
| 669 |
|
} |
| 670 |
|
|
| 671 |
|
snprintf(sql, sizeof(sql), |
| 672 |
|
"UPDATE user_pubinfo SET visit_count = visit_count + 1, " |
| 673 |
|
"last_login_dt = NOW() WHERE UID = %d", |
| 674 |
|
BBS_priv.uid); |
| 675 |
|
if (mysql_query(db, sql) != 0) |
| 676 |
|
{ |
| 677 |
|
log_error("Update user_pubinfo error: %s", mysql_error(db)); |
| 678 |
|
ret = -1; |
| 679 |
|
goto cleanup; |
| 680 |
|
} |
| 681 |
|
|
| 682 |
|
mysql_close(db); |
| 683 |
|
db = NULL; |
| 684 |
|
|
| 685 |
|
moveto(3, 1); |
| 686 |
|
prints("您已接受《用户许可协议》,请重新登陆。"); |
| 687 |
|
press_any_key(); |
| 688 |
|
return 1; |
| 689 |
|
default: |
| 690 |
|
continue; |
| 691 |
|
} |
| 692 |
|
|
| 693 |
|
break; |
| 694 |
|
} |
| 695 |
|
|
| 696 |
|
cleanup: |
| 697 |
|
mysql_close(db); |
| 698 |
|
|
| 699 |
|
return ret; |
| 700 |
|
} |