/[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.19 by sysadm, Tue May 6 05:12:29 2025 UTC Revision 1.24 by sysadm, Sat May 10 11:09:02 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                                                    login.c  -  description                                                    login.c  -  description
3                                                           -------------------                                                           -------------------
4          begin                : Mon Oct 20 2004          Copyright            : (C) 2004-2025 by Leaflet
5          copyright            : (C) 2004 by Leaflet          Email                : leaflet@leafok.com
         email                : leaflet@leafok.com  
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
11   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
12   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 3 of the License, or     *
13   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
# Line 44  int bbs_login() Line 43  int bbs_login()
43          int count = 0;          int count = 0;
44          int ok = 0;          int ok = 0;
45    
46          // Input username          for (; !ok && count < 3; count++)
         while (!ok)  
47          {          {
48                  prints("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "                  prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', "
49                             "注册请输入`\033[1;31mnew\033[m'): ");                             "注册请输入`\033[1;31mnew\033[m'): ");
50                  iflush();                  iflush();
51    
52                  str_input(username, sizeof(username) - 1, DOECHO);                  if (str_input(username, sizeof(username), DOECHO) < 0)
53                  count++;                  {
54                            continue;
55                    }
56    
57                  if (strcmp(username, "guest") == 0)                  if (strcmp(username, "guest") == 0)
58                  {                  {
# Line 83  int bbs_login() Line 83  int bbs_login()
83    
84                  if (username[0] != '\0')                  if (username[0] != '\0')
85                  {                  {
                         // Input password  
86                          prints("\033[1;37m请输入密码\033[m: ");                          prints("\033[1;37m请输入密码\033[m: ");
87                          iflush();                          iflush();
88    
89                          str_input(password, sizeof(password) - 1, NOECHO);                          if (str_input(password, sizeof(password), NOECHO) < 0)
90                            {
91                                    continue;
92                            }
93    
94                          MYSQL *db = db_open();                          MYSQL *db = db_open();
95                          if (db == NULL)                          if (db == NULL)
# Line 99  int bbs_login() Line 101  int bbs_login()
101    
102                          mysql_close(db);                          mysql_close(db);
103                  }                  }
104                  if (count >= 3 && !ok)          }
105                  {  
106                          login_fail();          if (!ok)
107                          return -1;          {
108                  }                  login_fail();
109                    return -1;
110          }          }
111    
112          return 0;          return 0;
# Line 116  int check_user(MYSQL *db, char *username Line 119  int check_user(MYSQL *db, char *username
119          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
120          int ret;          int ret;
121          long BBS_uid = 0;          long BBS_uid = 0;
122            char client_addr[IP_ADDR_LEN];
123    
124          // Verify format          // Verify format
125          if (ireg("^[A-Za-z][A-Za-z0-9]{2,11}$", username, 0, NULL) != 0 ||          if (ireg("^[A-Za-z][A-Za-z0-9]{2,11}$", username, 0, NULL) != 0 ||
# Line 139  int check_user(MYSQL *db, char *username Line 143  int check_user(MYSQL *db, char *username
143                  return -1;                  return -1;
144          }          }
145    
146            // Failed login attempts from the same source (subnet /24) during certain time period
147            strncpy(client_addr, hostaddr_client, sizeof(client_addr) - 1);
148            client_addr[sizeof(client_addr) - 1] = '\0';
149    
150            snprintf(sql, sizeof(sql),
151                             "SELECT COUNT(*) AS err_count FROM user_err_login_log "
152                             "WHERE login_dt >= SUBDATE(NOW(), INTERVAL 10 MINUTE) "
153                             "AND login_ip LIKE '%s'",
154                             ip_mask(client_addr, 1, '%'));
155            if (mysql_query(db, sql) != 0)
156            {
157                    log_error("Query user_list failed\n");
158                    return -1;
159            }
160            if ((rs = mysql_store_result(db)) == NULL)
161            {
162                    log_error("Get user_list data failed\n");
163                    return -1;
164            }
165            if ((row = mysql_fetch_row(rs)))
166            {
167                    if (atoi(row[0]) >= 2)
168                    {
169                            mysql_free_result(rs);
170    
171                            prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n");
172                            iflush();
173    
174                            return 1;
175                    }
176            }
177            mysql_free_result(rs);
178    
179            // Failed login attempts against the current username during certain time period
180            snprintf(sql, sizeof(sql),
181                             "SELECT COUNT(*) AS err_count FROM user_err_login_log "
182                             "WHERE username = '%s' AND login_dt >= SUBDATE(NOW(), INTERVAL 1 DAY)",
183                             username);
184            if (mysql_query(db, sql) != 0)
185            {
186                    log_error("Query user_list failed\n");
187                    return -1;
188            }
189            if ((rs = mysql_store_result(db)) == NULL)
190            {
191                    log_error("Get user_list data failed\n");
192                    return -1;
193            }
194            if ((row = mysql_fetch_row(rs)))
195            {
196                    if (atoi(row[0]) >= 5)
197                    {
198                            mysql_free_result(rs);
199    
200                            prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n");
201                            iflush();
202    
203                            return 1;
204                    }
205            }
206            mysql_free_result(rs);
207    
208          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
209                          "SELECT UID, username, p_login FROM user_list "                           "SELECT UID, username, p_login FROM user_list "
210                          "WHERE username = '%s' AND password = SHA2('%s', 256) AND enable",                           "WHERE username = '%s' AND password = SHA2('%s', 256) AND enable",
211                          username, password);                           username, password);
212          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
213          {          {
214                  log_error("Query user_list failed\n");                  log_error("Query user_list failed\n");
# Line 164  int check_user(MYSQL *db, char *username Line 230  int check_user(MYSQL *db, char *username
230    
231                  // Add user login log                  // Add user login log
232                  snprintf(sql, sizeof(sql),                  snprintf(sql, sizeof(sql),
233                                  "INSERT INTO user_login_log(UID, login_dt, login_ip) "                                   "INSERT INTO user_login_log(UID, login_dt, login_ip) "
234                                  "VALUES(%ld, NOW(), '%s')",                                   "VALUES(%ld, NOW(), '%s')",
235                                  BBS_uid, hostaddr_client);                                   BBS_uid, hostaddr_client);
236                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
237                  {                  {
238                          log_error("Insert into user_login_log failed\n");                          log_error("Insert into user_login_log failed\n");
# Line 194  int check_user(MYSQL *db, char *username Line 260  int check_user(MYSQL *db, char *username
260                  mysql_free_result(rs);                  mysql_free_result(rs);
261    
262                  snprintf(sql, sizeof(sql),                  snprintf(sql, sizeof(sql),
263                                  "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "                                   "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "
264                                  "VALUES('%s', '%s', NOW(), '%s')",                                   "VALUES('%s', '%s', NOW(), '%s')",
265                                  username, password, hostaddr_client);                                   username, password, hostaddr_client);
266                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
267                  {                  {
268                          log_error("Insert into user_err_login_log failed\n");                          log_error("Insert into user_err_login_log failed\n");
# Line 245  int check_user(MYSQL *db, char *username Line 311  int check_user(MYSQL *db, char *username
311          }          }
312    
313          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
314                          "UPDATE user_pubinfo SET visit_count = visit_count + 1, "                           "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
315                          "last_login_dt = NOW() WHERE UID = %ld",                           "last_login_dt = NOW() WHERE UID = %ld",
316                          BBS_uid);                           BBS_uid);
317          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
318          {          {
319                  log_error("Update user_pubinfo failed\n");                  log_error("Update user_pubinfo failed\n");
# Line 273  int load_user_info(MYSQL *db, long int B Line 339  int load_user_info(MYSQL *db, long int B
339          time_t last_login_dt;          time_t last_login_dt;
340    
341          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
342                          "SELECT life, UNIX_TIMESTAMP(last_login_dt) "                           "SELECT life, UNIX_TIMESTAMP(last_login_dt) "
343                          "FROM user_pubinfo WHERE UID = %ld",                           "FROM user_pubinfo WHERE UID = %ld",
344                          BBS_uid);                           BBS_uid);
345          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
346          {          {
347                  log_error("Query user_pubinfo failed\n");                  log_error("Query user_pubinfo failed\n");
# Line 342  int user_online_add(MYSQL *db) Line 408  int user_online_add(MYSQL *db)
408          }          }
409    
410          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
411                          "INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) "                           "INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) "
412                          "VALUES('Telnet_Process_%d', %ld, '%s', NOW(), NOW())",                           "VALUES('Telnet_Process_%d', %ld, '%s', NOW(), NOW())",
413                          getpid(), BBS_priv.uid, hostaddr_client);                           getpid(), BBS_priv.uid, hostaddr_client);
414          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
415          {          {
416                  log_error("Add user_online failed\n");                  log_error("Add user_online failed\n");
# Line 359  int user_online_del(MYSQL *db) Line 425  int user_online_del(MYSQL *db)
425          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
426    
427          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
428                          "DELETE FROM user_online WHERE SID = 'Telnet_Process_%d'",                           "DELETE FROM user_online WHERE SID = 'Telnet_Process_%d'",
429                          getpid());                           getpid());
430          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
431          {          {
432                  log_error("Delete user_online failed\n");                  log_error("Delete user_online failed\n");


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

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