/[LeafOK_CVS]/lbbs/src/login.c
ViewVC logotype

Diff of /lbbs/src/login.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.44 by sysadm, Mon Jun 16 09:42:28 2025 UTC Revision 1.67 by sysadm, Wed Nov 5 02:06:50 2025 UTC
# Line 1  Line 1 
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     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
 #define _POSIX_C_SOURCE 200112L  
   
 #include "login.h"  
9  #include "bbs.h"  #include "bbs.h"
 #include "user_priv.h"  
10  #include "common.h"  #include "common.h"
11  #include "log.h"  #include "database.h"
12  #include "io.h"  #include "io.h"
13    #include "ip_mask.h"
14    #include "log.h"
15    #include "login.h"
16  #include "screen.h"  #include "screen.h"
17  #include "database.h"  #include "user_priv.h"
 #include <errno.h>  
18  #include <ctype.h>  #include <ctype.h>
19    #include <errno.h>
20    #include <stdlib.h>
21  #include <string.h>  #include <string.h>
22  #include <regex.h>  #include <regex.h>
 #include <stdlib.h>  
23  #include <unistd.h>  #include <unistd.h>
24  #include <mysql/mysql.h>  #include <mysql/mysql.h>
25    #include <sys/param.h>
26    
27    static const int BBS_username_min_len = 3; // common len = 5, special len = 3
28    static const int BBS_password_min_len = 5; // legacy len = 5, current len = 6
29    
30    static const int BBS_allowed_login_failures_within_interval = 10;
31    static const int BBS_login_failures_count_interval = 10; // minutes
32    static const int BBS_allowed_login_failures_per_account = 3;
33    
34    const int BBS_login_retry_times = 3;
35    
36  int bbs_login(void)  int bbs_login(void)
37  {  {
# Line 41  int bbs_login(void) Line 42  int bbs_login(void)
42    
43          for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++)          for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++)
44          {          {
45                  prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', "                  prints("\033[1;33m璇疯緭鍏ュ笎鍙穃033[m(璇曠敤璇疯緭鍏\033[1;36mguest\033[m', "
46                             "注册请输入`\033[1;31mnew\033[m'): ");                             "娉ㄥ唽璇疯緭鍏\033[1;31mnew\033[m'): ");
47                  iflush();                  iflush();
48    
49                  if (str_input(username, sizeof(username), DOECHO) < 0)                  if (str_input(username, sizeof(username), DOECHO) < 0)
# Line 61  int bbs_login(void) Line 62  int bbs_login(void)
62                  {                  {
63                          display_file(DATA_REGISTER, 1);                          display_file(DATA_REGISTER, 1);
64    
65                          return 0;                          return -1;
66                  }                  }
67    
68                  if (username[0] != '\0')                  if (username[0] != '\0')
69                  {                  {
70                          prints("\033[1;37m请输入密码\033[m: ");                          prints("\033[1;37m璇疯緭鍏ュ瘑鐮乗033[m: ");
71                          iflush();                          iflush();
72    
73                          if (str_input(password, sizeof(password), NOECHO) < 0)                          if (str_input(password, sizeof(password), NOECHO) < 0)
# Line 114  int check_user(const char *username, con Line 115  int check_user(const char *username, con
115          // Verify format          // Verify format
116          for (i = 0; ok && username[i] != '\0'; i++)          for (i = 0; ok && username[i] != '\0'; i++)
117          {          {
118                  if (!(isalpha(username[i]) || (i > 0 && isdigit(username[i]))))                  if (!(isalpha(username[i]) || (i > 0 && (isdigit(username[i]) || username[i] == '_'))))
119                  {                  {
120                          ok = 0;                          ok = 0;
121                  }                  }
122          }          }
123          if (ok && (i < 3 || i > 12))          if (ok && (i < BBS_username_min_len || i > BBS_username_max_len))
124          {          {
125                  ok = 0;                  ok = 0;
126          }          }
# Line 130  int check_user(const char *username, con Line 131  int check_user(const char *username, con
131                          ok = 0;                          ok = 0;
132                  }                  }
133          }          }
134          if (ok && (i < 5 || i > 12))          if (ok && (i < BBS_password_min_len || i > BBS_password_max_len))
135          {          {
136                  ok = 0;                  ok = 0;
137          }          }
138    
139          if (!ok)          if (!ok)
140          {          {
141                  prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");                  prints("\033[1;31m鐢ㄦ埛鍚嶆垨瀵嗙爜鏍煎紡閿欒...\033[m\r\n");
142                  ret = 1;                  ret = 1;
143                  goto cleanup;                  goto cleanup;
144          }          }
# Line 181  int check_user(const char *username, con Line 182  int check_user(const char *username, con
182          }          }
183          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
184          {          {
185                  if (atoi(row[0]) > BBS_allowed_login_failures_within_interval)                  if (atoi(row[0]) >= BBS_allowed_login_failures_within_interval)
186                  {                  {
187                          prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n");                          prints("\033[1;31m鏉ユ簮瀛樺湪澶氭澶辫触鐧婚檰灏濊瘯锛岃绋嶅悗鍐嶈瘯锛屾垨浣跨敤Web鏂瑰紡璁块棶\033[m\r\n");
188                          ret = 1;                          ret = 1;
189                          goto cleanup;                          goto cleanup;
190                  }                  }
# Line 191  int check_user(const char *username, con Line 192  int check_user(const char *username, con
192          mysql_free_result(rs);          mysql_free_result(rs);
193          rs = NULL;          rs = NULL;
194    
195          // Failed login attempts against the current username during certain time period          // Failed login attempts against the current username since last successful login
196          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
197                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "
198                           "WHERE username = '%s' AND login_dt >= SUBDATE(NOW(), INTERVAL 1 DAY)",                           "LEFT JOIN user_list ON user_err_login_log.username = user_list.username "
199                             "LEFT JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID "
200                             "WHERE user_err_login_log.username = '%s' "
201                             "AND (user_err_login_log.login_dt >= user_pubinfo.last_login_dt "
202                             "OR user_pubinfo.last_login_dt IS NULL)",
203                           username);                           username);
204          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
205          {          {
# Line 210  int check_user(const char *username, con Line 215  int check_user(const char *username, con
215          }          }
216          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
217          {          {
218                  if (atoi(row[0]) >= 5)                  if (atoi(row[0]) >= BBS_allowed_login_failures_per_account)
219                  {                  {
220                          prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n");                          prints("\033[1;31m璐︽埛瀛樺湪澶氭澶辫触鐧婚檰灏濊瘯锛岃浣跨敤Web鏂瑰紡鐧诲綍瑙i攣\033[m\r\n");
221                          ret = 1;                          ret = 1;
222                          goto cleanup;                          goto cleanup;
223                  }                  }
# Line 268  int check_user(const char *username, con Line 273  int check_user(const char *username, con
273    
274                  if (p_login == 0)                  if (p_login == 0)
275                  {                  {
276                          prints("\033[1;31m您目前无权登陆...\033[m\r\n");                          prints("\033[1;31m鎮ㄧ洰鍓嶆棤鏉冪櫥闄...\033[m\r\n");
277                          ret = 1;                          ret = 1;
278                          goto cleanup;                          goto cleanup;
279                  }                  }
# Line 297  int check_user(const char *username, con Line 302  int check_user(const char *username, con
302                          goto cleanup;                          goto cleanup;
303                  }                  }
304    
305                  prints("\033[1;31m错误的用户名或密码...\033[m\r\n");                  prints("\033[1;31m閿欒鐨勭敤鎴峰悕鎴栧瘑鐮...\033[m\r\n");
306                  ret = 1;                  ret = 1;
307                  goto cleanup;                  goto cleanup;
308          }          }
# Line 317  int check_user(const char *username, con Line 322  int check_user(const char *username, con
322          case 0: // Login successfully          case 0: // Login successfully
323                  break;                  break;
324          case -1: // Load data error          case -1: // Load data error
325                  prints("\033[1;31m读取用户数据错误...\033[m\r\n");                  prints("\033[1;31m璇诲彇鐢ㄦ埛鏁版嵁閿欒...\033[m\r\n");
326                  ret = -1;                  ret = -1;
327                  goto cleanup;                  goto cleanup;
328          case -2: // Unused          case -2: // Unused
329                  prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");                  prints("\033[1;31m璇烽氳繃Web鐧诲綍鏇存柊鐢ㄦ埛璁稿彲鍗忚...\033[m\r\n");
330                  ret = 1;                  ret = 1;
331                  goto cleanup;                  goto cleanup;
332          case -3: // Dead          case -3: // Dead
333                  prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");                  prints("\033[1;31m寰堥仐鎲撅紝鎮ㄥ凡缁忔案杩滅寮浜嗘垜浠殑涓栫晫锛乗033[m\r\n");
334                  ret = 1;                  ret = 1;
335                  goto cleanup;                  goto cleanup;
336          default:          default:
# Line 350  int check_user(const char *username, con Line 355  int check_user(const char *username, con
355                  goto cleanup;                  goto cleanup;
356          }          }
357    
358          BBS_last_access_tm = BBS_login_tm = time(0);          BBS_last_access_tm = BBS_login_tm = time(NULL);
359    
360          // Set user tz to process env          // Set user tz to process env
361          if (BBS_user_tz[0] != '\0')          if (BBS_user_tz[0] != '\0')
# Line 422  int load_user_info(MYSQL *db, int BBS_ui Line 427  int load_user_info(MYSQL *db, int BBS_ui
427          rs = NULL;          rs = NULL;
428    
429          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
430                  time(0) - last_login_dt > 60 * 60 * 24 * life)                  time(NULL) - last_login_dt > 60 * 60 * 24 * life)
431          {          {
432                  ret = -3; // Dead                  ret = -3; // Dead
433                  goto cleanup;                  goto cleanup;
# Line 472  int load_guest_info(void) Line 477  int load_guest_info(void)
477                  goto cleanup;                  goto cleanup;
478          }          }
479    
480          BBS_last_access_tm = BBS_login_tm = time(0);          BBS_last_access_tm = BBS_login_tm = time(NULL);
481    
482  cleanup:  cleanup:
483          mysql_close(db);          mysql_close(db);
# Line 484  int user_online_add(MYSQL *db) Line 489  int user_online_add(MYSQL *db)
489  {  {
490          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
491    
492          if (user_online_del(db) != 0)          snprintf(sql, sizeof(sql),
493                             "INSERT INTO visit_log(dt, IP) VALUES(NOW(), '%s')",
494                             hostaddr_client);
495            if (mysql_query(db, sql) != 0)
496          {          {
497                    log_error("Add visit log error: %s\n", mysql_error(db));
498                  return -1;                  return -1;
499          }          }
500    
501            if (user_online_del(db) != 0)
502            {
503                    return -2;
504            }
505    
506          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
507                           "INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) "                           "INSERT INTO user_online(SID, UID, ip, current_action, login_tm, last_tm) "
508                           "VALUES('Telnet_Process_%d', %d, '%s', NOW(), NOW())",                           "VALUES('Telnet_Process_%d', %d, '%s', 'LOGIN', NOW(), NOW())",
509                           getpid(), BBS_priv.uid, hostaddr_client);                           getpid(), BBS_priv.uid, hostaddr_client);
510          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
511          {          {
512                  log_error("Add user_online error: %s\n", mysql_error(db));                  log_error("Add user_online error: %s\n", mysql_error(db));
513                  return -1;                  return -3;
514          }          }
515    
516          return 0;          return 0;
# Line 517  int user_online_del(MYSQL *db) Line 531  int user_online_del(MYSQL *db)
531    
532          return 0;          return 0;
533  }  }
534    
535    int user_online_exp(MYSQL *db)
536    {
537            char sql[SQL_BUFFER_LEN];
538    
539            // +1 exp for every 5 minutes online since last logout
540            // but at most 24 hours worth of exp can be gained in Telnet session
541            snprintf(sql, sizeof(sql),
542                             "UPDATE user_pubinfo SET exp = exp + FLOOR(LEAST(TIMESTAMPDIFF("
543                             "SECOND, GREATEST(last_login_dt, IF(last_logout_dt IS NULL, last_login_dt, last_logout_dt)), NOW()"
544                             ") / 60 / 5, 12 * 24)), last_logout_dt = NOW() "
545                             "WHERE UID = %d",
546                             BBS_priv.uid);
547            if (mysql_query(db, sql) != 0)
548            {
549                    log_error("Update user_pubinfo error: %s\n", mysql_error(db));
550                    return -1;
551            }
552    
553            return 0;
554    }
555    
556    int user_online_update(const char *action)
557    {
558            MYSQL *db = NULL;
559            char sql[SQL_BUFFER_LEN];
560    
561            if ((action == NULL || strcmp(BBS_current_action, action) == 0) &&
562                    time(NULL) - BBS_current_action_tm < BBS_current_action_refresh_interval) // No change
563            {
564                    return 0;
565            }
566    
567            if (action != NULL)
568            {
569                    strncpy(BBS_current_action, action, sizeof(BBS_current_action) - 1);
570                    BBS_current_action[sizeof(BBS_current_action) - 1] = '\0';
571            }
572    
573            BBS_current_action_tm = time(NULL);
574    
575            db = db_open();
576            if (db == NULL)
577            {
578                    log_error("db_open() error: %s\n", mysql_error(db));
579                    return -1;
580            }
581    
582            snprintf(sql, sizeof(sql),
583                             "UPDATE user_online SET current_action = '%s', last_tm = NOW() "
584                             "WHERE SID = 'Telnet_Process_%d'",
585                             BBS_current_action, getpid());
586            if (mysql_query(db, sql) != 0)
587            {
588                    log_error("Update user_online error: %s\n", mysql_error(db));
589                    return -2;
590            }
591    
592            mysql_close(db);
593    
594            return 1;
595    }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1