/[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.49 by sysadm, Wed Jul 2 04:17:33 2025 UTC Revision 1.65 by sysadm, Tue Nov 4 13:49:51 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 by 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    
9  #include "bbs.h"  #include "bbs.h"
10  #include "common.h"  #include "common.h"
11  #include "database.h"  #include "database.h"
12  #include "io.h"  #include "io.h"
13    #include "ip_mask.h"
14  #include "log.h"  #include "log.h"
15  #include "login.h"  #include "login.h"
16  #include "screen.h"  #include "screen.h"
# Line 29  Line 22 
22  #include <regex.h>  #include <regex.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  int bbs_login(void)  int bbs_login(void)
31  {  {
# Line 59  int bbs_login(void) Line 56  int bbs_login(void)
56                  {                  {
57                          display_file(DATA_REGISTER, 1);                          display_file(DATA_REGISTER, 1);
58    
59                          return 0;                          return -1;
60                  }                  }
61    
62                  if (username[0] != '\0')                  if (username[0] != '\0')
# Line 112  int check_user(const char *username, con Line 109  int check_user(const char *username, con
109          // Verify format          // Verify format
110          for (i = 0; ok && username[i] != '\0'; i++)          for (i = 0; ok && username[i] != '\0'; i++)
111          {          {
112                  if (!(isalpha(username[i]) || (i > 0 && isdigit(username[i]))))                  if (!(isalpha(username[i]) || (i > 0 && (isdigit(username[i]) || username[i] == '_'))))
113                  {                  {
114                          ok = 0;                          ok = 0;
115                  }                  }
116          }          }
117          if (ok && (i < 3 || i > 12))          if (ok && (i < BBS_username_min_len || i > BBS_username_max_len))
118          {          {
119                  ok = 0;                  ok = 0;
120          }          }
# Line 128  int check_user(const char *username, con Line 125  int check_user(const char *username, con
125                          ok = 0;                          ok = 0;
126                  }                  }
127          }          }
128          if (ok && (i < 5 || i > 12))          if (ok && (i < BBS_password_min_len || i > BBS_password_max_len))
129          {          {
130                  ok = 0;                  ok = 0;
131          }          }
# Line 179  int check_user(const char *username, con Line 176  int check_user(const char *username, con
176          }          }
177          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
178          {          {
179                  if (atoi(row[0]) > BBS_allowed_login_failures_within_interval)                  if (atoi(row[0]) >= BBS_allowed_login_failures_within_interval)
180                  {                  {
181                          prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n");                          prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试,或使用Web方式访问\033[m\r\n");
182                          ret = 1;                          ret = 1;
183                          goto cleanup;                          goto cleanup;
184                  }                  }
# Line 189  int check_user(const char *username, con Line 186  int check_user(const char *username, con
186          mysql_free_result(rs);          mysql_free_result(rs);
187          rs = NULL;          rs = NULL;
188    
189          // Failed login attempts against the current username during certain time period          // Failed login attempts against the current username since last successful login
190          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
191                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "
192                           "WHERE username = '%s' AND login_dt >= SUBDATE(NOW(), INTERVAL 1 DAY)",                           "LEFT JOIN user_list ON user_err_login_log.username = user_list.username "
193                             "LEFT JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID "
194                             "WHERE user_err_login_log.username = '%s' "
195                             "AND (user_err_login_log.login_dt >= user_pubinfo.last_login_dt "
196                             "OR user_pubinfo.last_login_dt IS NULL)",
197                           username);                           username);
198          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
199          {          {
# Line 208  int check_user(const char *username, con Line 209  int check_user(const char *username, con
209          }          }
210          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
211          {          {
212                  if (atoi(row[0]) >= 5)                  if (atoi(row[0]) >= BBS_allowed_login_failures_per_account)
213                  {                  {
214                          prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n");                          prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录解锁\033[m\r\n");
215                          ret = 1;                          ret = 1;
216                          goto cleanup;                          goto cleanup;
217                  }                  }
# Line 482  int user_online_add(MYSQL *db) Line 483  int user_online_add(MYSQL *db)
483  {  {
484          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
485    
486          if (user_online_del(db) != 0)          snprintf(sql, sizeof(sql),
487                             "INSERT INTO visit_log(dt, IP) VALUES(NOW(), '%s')",
488                             hostaddr_client);
489            if (mysql_query(db, sql) != 0)
490          {          {
491                    log_error("Add visit log error: %s\n", mysql_error(db));
492                  return -1;                  return -1;
493          }          }
494    
495            if (user_online_del(db) != 0)
496            {
497                    return -2;
498            }
499    
500          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
501                           "INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) "                           "INSERT INTO user_online(SID, UID, ip, current_action, login_tm, last_tm) "
502                           "VALUES('Telnet_Process_%d', %d, '%s', NOW(), NOW())",                           "VALUES('Telnet_Process_%d', %d, '%s', 'LOGIN', NOW(), NOW())",
503                           getpid(), BBS_priv.uid, hostaddr_client);                           getpid(), BBS_priv.uid, hostaddr_client);
504          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
505          {          {
506                  log_error("Add user_online error: %s\n", mysql_error(db));                  log_error("Add user_online error: %s\n", mysql_error(db));
507                  return -1;                  return -3;
508          }          }
509    
510          return 0;          return 0;
# Line 516  int user_online_del(MYSQL *db) Line 526  int user_online_del(MYSQL *db)
526          return 0;          return 0;
527  }  }
528    
529    int user_online_exp(MYSQL *db)
530    {
531            char sql[SQL_BUFFER_LEN];
532    
533            // +1 exp for every 5 minutes online since last logout
534            // but at most 24 hours worth of exp can be gained in Telnet session
535            snprintf(sql, sizeof(sql),
536                             "UPDATE user_pubinfo SET exp = exp + FLOOR(LEAST(TIMESTAMPDIFF("
537                             "SECOND, GREATEST(last_login_dt, IF(last_logout_dt IS NULL, last_login_dt, last_logout_dt)), NOW()"
538                             ") / 60 / 5, 12 * 24)), last_logout_dt = NOW() "
539                             "WHERE UID = %d",
540                             BBS_priv.uid);
541            if (mysql_query(db, sql) != 0)
542            {
543                    log_error("Update user_pubinfo error: %s\n", mysql_error(db));
544                    return -1;
545            }
546    
547            return 0;
548    }
549    
550  int user_online_update(const char *action)  int user_online_update(const char *action)
551  {  {
552          MYSQL *db = NULL;          MYSQL *db = NULL;
553          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
554    
555          if (strcmp(BBS_current_action, action) == 0) // No change          if ((action == NULL || strcmp(BBS_current_action, action) == 0) &&
556                    time(NULL) - BBS_current_action_tm < BBS_current_action_refresh_interval) // No change
557          {          {
558                  return 0;                  return 0;
559          }          }
560    
561          strncpy(BBS_current_action, action, sizeof(BBS_current_action) - 1);          if (action != NULL)
562          BBS_current_action[sizeof(BBS_current_action) - 1] = '\0';          {
563                    strncpy(BBS_current_action, action, sizeof(BBS_current_action) - 1);
564                    BBS_current_action[sizeof(BBS_current_action) - 1] = '\0';
565            }
566    
567            BBS_current_action_tm = time(NULL);
568    
569          db = db_open();          db = db_open();
570          if (db == NULL)          if (db == NULL)
# Line 537  int user_online_update(const char *actio Line 574  int user_online_update(const char *actio
574          }          }
575    
576          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
577                           "UPDATE user_online SET current_action = '%s', last_tm=NOW() "                           "UPDATE user_online SET current_action = '%s', last_tm = NOW() "
578                           "WHERE SID = 'Telnet_Process_%d'",                           "WHERE SID = 'Telnet_Process_%d'",
579                           BBS_current_action, getpid());                           BBS_current_action, getpid());
580          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)


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

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