/[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.43 by sysadm, Sat Jun 14 09:20:13 2025 UTC Revision 1.44 by sysadm, Mon Jun 16 09:42:28 2025 UTC
# Line 93  int bbs_login(void) Line 93  int bbs_login(void)
93    
94  int check_user(const char *username, const char *password)  int check_user(const char *username, const char *password)
95  {  {
96          MYSQL *db;          MYSQL *db = NULL;
97          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
98          MYSQL_ROW row;          MYSQL_ROW row;
99          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
100          int ret;          int ret = 0;
101          int BBS_uid = 0;          int BBS_uid = 0;
102          char client_addr[IP_ADDR_LEN];          char client_addr[IP_ADDR_LEN];
103          int i;          int i;
# Line 107  int check_user(const char *username, con Line 107  int check_user(const char *username, con
107          db = db_open();          db = db_open();
108          if (db == NULL)          if (db == NULL)
109          {          {
110                  return -1;                  ret = -1;
111                    goto cleanup;
112          }          }
113    
114          // Verify format          // Verify format
# Line 137  int check_user(const char *username, con Line 138  int check_user(const char *username, con
138          if (!ok)          if (!ok)
139          {          {
140                  prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");                  prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");
141                  return 1;                  ret = 1;
142                    goto cleanup;
143          }          }
144    
145          // Begin transaction          // Begin transaction
146          if (mysql_query(db, "SET autocommit=0") != 0)          if (mysql_query(db, "SET autocommit=0") != 0)
147          {          {
148                  log_error("SET autocommit=0 error: %s\n", mysql_error(db));                  log_error("SET autocommit=0 error: %s\n", mysql_error(db));
149                  mysql_close(db);                  ret = -1;
150                  return -1;                  goto cleanup;
151          }          }
152    
153          if (mysql_query(db, "BEGIN") != 0)          if (mysql_query(db, "BEGIN") != 0)
154          {          {
155                  log_error("Begin transaction error: %s\n", mysql_error(db));                  log_error("Begin transaction error: %s\n", mysql_error(db));
156                  mysql_close(db);                  ret = -1;
157                  return -1;                  goto cleanup;
158          }          }
159    
160          // Failed login attempts from the same source (subnet /24) during certain time period          // Failed login attempts from the same source (subnet /24) during certain time period
# Line 161  int check_user(const char *username, con Line 163  int check_user(const char *username, con
163    
164          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
165                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "
166                           "WHERE login_dt >= SUBDATE(NOW(), INTERVAL 10 MINUTE) "                           "WHERE login_dt >= SUBDATE(NOW(), INTERVAL %d MINUTE) "
167                           "AND login_ip LIKE '%s'",                           "AND login_ip LIKE '%s'",
168                             BBS_login_failures_count_interval,
169                           ip_mask(client_addr, 1, '%'));                           ip_mask(client_addr, 1, '%'));
170          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
171          {          {
172                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s\n", mysql_error(db));
173                  mysql_close(db);                  ret = -1;
174                  return -1;                  goto cleanup;
175          }          }
176          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
177          {          {
178                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
179                  mysql_close(db);                  ret = -1;
180                  return -1;                  goto cleanup;
181          }          }
182          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
183          {          {
184                  if (atoi(row[0]) >= 2)                  if (atoi(row[0]) > BBS_allowed_login_failures_within_interval)
185                  {                  {
                         mysql_free_result(rs);  
                         mysql_close(db);  
   
186                          prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n");                          prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n");
187                            ret = 1;
188                          return 1;                          goto cleanup;
189                  }                  }
190          }          }
191          mysql_free_result(rs);          mysql_free_result(rs);
192            rs = NULL;
193    
194          // Failed login attempts against the current username during certain time period          // Failed login attempts against the current username during certain time period
195          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 198  int check_user(const char *username, con Line 199  int check_user(const char *username, con
199          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
200          {          {
201                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s\n", mysql_error(db));
202                  mysql_close(db);                  ret = -1;
203                  return -1;                  goto cleanup;
204          }          }
205          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
206          {          {
207                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
208                  mysql_close(db);                  ret = -1;
209                  return -1;                  goto cleanup;
210          }          }
211          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
212          {          {
213                  if (atoi(row[0]) >= 5)                  if (atoi(row[0]) >= 5)
214                  {                  {
                         mysql_free_result(rs);  
                         mysql_close(db);  
   
215                          prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n");                          prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n");
216                            ret = 1;
217                          return 1;                          goto cleanup;
218                  }                  }
219          }          }
220          mysql_free_result(rs);          mysql_free_result(rs);
221            rs = NULL;
222    
223          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
224                           "SELECT UID, username, p_login FROM user_list "                           "SELECT UID, username, p_login FROM user_list "
# Line 228  int check_user(const char *username, con Line 227  int check_user(const char *username, con
227          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
228          {          {
229                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s\n", mysql_error(db));
230                  mysql_close(db);                  ret = -1;
231                  return -1;                  goto cleanup;
232          }          }
233          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
234          {          {
235                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
236                  mysql_close(db);                  ret = -1;
237                  return -1;                  goto cleanup;
238          }          }
239          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
240          {          {
# Line 245  int check_user(const char *username, con Line 244  int check_user(const char *username, con
244                  int p_login = atoi(row[2]);                  int p_login = atoi(row[2]);
245    
246                  mysql_free_result(rs);                  mysql_free_result(rs);
247                    rs = NULL;
248    
249                  // Add user login log                  // Add user login log
250                  snprintf(sql, sizeof(sql),                  snprintf(sql, sizeof(sql),
# Line 254  int check_user(const char *username, con Line 254  int check_user(const char *username, con
254                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
255                  {                  {
256                          log_error("Insert into user_login_log error: %s\n", mysql_error(db));                          log_error("Insert into user_login_log error: %s\n", mysql_error(db));
257                          mysql_close(db);                          ret = -1;
258                          return -1;                          goto cleanup;
259                  }                  }
260    
261                  // Commit transaction                  // Commit transaction
262                  if (mysql_query(db, "COMMIT") != 0)                  if (mysql_query(db, "COMMIT") != 0)
263                  {                  {
264                          log_error("Commit transaction error: %s\n", mysql_error(db));                          log_error("Commit transaction error: %s\n", mysql_error(db));
265                          mysql_close(db);                          ret = -1;
266                          return -1;                          goto cleanup;
267                  }                  }
268    
269                  if (p_login == 0)                  if (p_login == 0)
270                  {                  {
                         mysql_free_result(rs);  
                         mysql_close(db);  
   
271                          prints("\033[1;31m您目前无权登陆...\033[m\r\n");                          prints("\033[1;31m您目前无权登陆...\033[m\r\n");
272                          return 1;                          ret = 1;
273                            goto cleanup;
274                  }                  }
275          }          }
276          else          else
277          {          {
278                  mysql_free_result(rs);                  mysql_free_result(rs);
279                  mysql_close(db);                  rs = NULL;
280    
281                  snprintf(sql, sizeof(sql),                  snprintf(sql, sizeof(sql),
282                                   "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "                                   "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "
# Line 287  int check_user(const char *username, con Line 285  int check_user(const char *username, con
285                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
286                  {                  {
287                          log_error("Insert into user_err_login_log error: %s\n", mysql_error(db));                          log_error("Insert into user_err_login_log error: %s\n", mysql_error(db));
288                          return -1;                          ret = -1;
289                            goto cleanup;
290                  }                  }
291    
292                  // Commit transaction                  // Commit transaction
293                  if (mysql_query(db, "COMMIT") != 0)                  if (mysql_query(db, "COMMIT") != 0)
294                  {                  {
295                          log_error("Commit transaction error: %s\n", mysql_error(db));                          log_error("Commit transaction error: %s\n", mysql_error(db));
296                          mysql_close(db);                          ret = -1;
297                          return -1;                          goto cleanup;
298                  }                  }
299    
300                  prints("\033[1;31m错误的用户名或密码...\033[m\r\n");                  prints("\033[1;31m错误的用户名或密码...\033[m\r\n");
301                  mysql_close(db);                  ret = 1;
302                  return 1;                  goto cleanup;
303          }          }
304    
305          // Set AUTOCOMMIT = 1          // Set AUTOCOMMIT = 1
306          if (mysql_query(db, "SET autocommit=1") != 0)          if (mysql_query(db, "SET autocommit=1") != 0)
307          {          {
308                  log_error("SET autocommit=1 error: %s\n", mysql_error(db));                  log_error("SET autocommit=1 error: %s\n", mysql_error(db));
309                  mysql_close(db);                  ret = -1;
310                  return -1;                  goto cleanup;
311          }          }
312    
313          ret = load_user_info(db, BBS_uid);          ret = load_user_info(db, BBS_uid);
# Line 319  int check_user(const char *username, con Line 318  int check_user(const char *username, con
318                  break;                  break;
319          case -1: // Load data error          case -1: // Load data error
320                  prints("\033[1;31m读取用户数据错误...\033[m\r\n");                  prints("\033[1;31m读取用户数据错误...\033[m\r\n");
321                  mysql_close(db);                  ret = -1;
322                  return -1;                  goto cleanup;
323          case -2: // Unused          case -2: // Unused
324                  prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");                  prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");
325                  mysql_close(db);                  ret = 1;
326                  return 1;                  goto cleanup;
327          case -3: // Dead          case -3: // Dead
328                  prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");                  prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
329                  mysql_close(db);                  ret = 1;
330                  return 1;                  goto cleanup;
331          default:          default:
332                  mysql_close(db);                  ret = -2;
333                  return -2;                  goto cleanup;
334          }          }
335    
336          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 341  int check_user(const char *username, con Line 340  int check_user(const char *username, con
340          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
341          {          {
342                  log_error("Update user_pubinfo error: %s\n", mysql_error(db));                  log_error("Update user_pubinfo error: %s\n", mysql_error(db));
343                  mysql_close(db);                  ret = -1;
344                  return -1;                  goto cleanup;
345          }          }
346    
347          if (user_online_add(db) != 0)          if (user_online_add(db) != 0)
348          {          {
349                  mysql_close(db);                  ret = -1;
350                  return -1;                  goto cleanup;
351          }          }
352    
353          BBS_last_access_tm = BBS_login_tm = time(0);          BBS_last_access_tm = BBS_login_tm = time(0);
# Line 369  int check_user(const char *username, con Line 368  int check_user(const char *username, con
368                  tzset();                  tzset();
369          }          }
370    
371    cleanup:
372            mysql_free_result(rs);
373          mysql_close(db);          mysql_close(db);
374    
375          return 0;          return ret;
376  }  }
377    
378  int load_user_info(MYSQL *db, int BBS_uid)  int load_user_info(MYSQL *db, int BBS_uid)
379  {  {
380          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
381          MYSQL_ROW row;          MYSQL_ROW row;
382          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
383          int life;          int life;
384          time_t last_login_dt;          time_t last_login_dt;
385            int ret = 0;
386    
387          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
388                           "SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone, exp, nickname "                           "SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone, exp, nickname "
# Line 389  int load_user_info(MYSQL *db, int BBS_ui Line 391  int load_user_info(MYSQL *db, int BBS_ui
391          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
392          {          {
393                  log_error("Query user_pubinfo error: %s\n", mysql_error(db));                  log_error("Query user_pubinfo error: %s\n", mysql_error(db));
394                  return -1;                  ret = -1;
395                    goto cleanup;
396          }          }
397          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
398          {          {
399                  log_error("Get user_pubinfo data failed\n");                  log_error("Get user_pubinfo data failed\n");
400                  return -1;                  ret = -1;
401                    goto cleanup;
402          }          }
403          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
404          {          {
# Line 411  int load_user_info(MYSQL *db, int BBS_ui Line 415  int load_user_info(MYSQL *db, int BBS_ui
415          }          }
416          else          else
417          {          {
418                  mysql_free_result(rs);                  ret = -1; // Data not found
419                  return -1; // Data not found                  goto cleanup;
420          }          }
421          mysql_free_result(rs);          mysql_free_result(rs);
422            rs = NULL;
423    
424          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
425                  time(0) - last_login_dt > 60 * 60 * 24 * life)                  time(0) - last_login_dt > 60 * 60 * 24 * life)
426          {          {
427                  return -3; // Dead                  ret = -3; // Dead
428                    goto cleanup;
429          }          }
430    
431          if (load_priv(db, &BBS_priv, BBS_uid) != 0)          if (load_priv(db, &BBS_priv, BBS_uid) != 0)
432          {          {
433                  return -1;                  ret = -1;
434                    goto cleanup;
435          }          }
436    
437          return 0;  cleanup:
438            mysql_free_result(rs);
439    
440            return ret;
441  }  }
442    
443  int load_guest_info(void)  int load_guest_info(void)
444  {  {
445          MYSQL *db;          MYSQL *db = NULL;
446            int ret = 0;
447    
448          db = db_open();          db = db_open();
449          if (db == NULL)          if (db == NULL)
450          {          {
451                  return -1;                  ret = -1;
452                    goto cleanup;
453          }          }
454    
455          strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);          strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);
# Line 450  int load_guest_info(void) Line 462  int load_guest_info(void)
462    
463          if (load_priv(db, &BBS_priv, 0) != 0)          if (load_priv(db, &BBS_priv, 0) != 0)
464          {          {
465                  return -1;                  ret = -1;
466                    goto cleanup;
467          }          }
468    
469          if (user_online_add(db) != 0)          if (user_online_add(db) != 0)
470          {          {
471                  return -1;                  ret = -1;
472                    goto cleanup;
473          }          }
474    
475          BBS_last_access_tm = BBS_login_tm = time(0);          BBS_last_access_tm = BBS_login_tm = time(0);
476    
477    cleanup:
478          mysql_close(db);          mysql_close(db);
479            
480          return 0;          return ret;
481  }  }
482    
483  int user_online_add(MYSQL *db)  int user_online_add(MYSQL *db)


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

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