/[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.40 by sysadm, Sat Jun 7 06:16:25 2025 UTC Revision 1.48 by sysadm, Sat Jun 21 02:15:18 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #define _POSIX_C_SOURCE 200112L  
   
 #include "login.h"  
17  #include "bbs.h"  #include "bbs.h"
 #include "user_priv.h"  
18  #include "common.h"  #include "common.h"
19  #include "log.h"  #include "database.h"
20  #include "io.h"  #include "io.h"
21    #include "log.h"
22    #include "login.h"
23  #include "screen.h"  #include "screen.h"
24  #include "database.h"  #include "user_priv.h"
 #include <errno.h>  
25  #include <ctype.h>  #include <ctype.h>
26    #include <errno.h>
27    #include <stdlib.h>
28  #include <string.h>  #include <string.h>
29  #include <regex.h>  #include <regex.h>
 #include <stdlib.h>  
30  #include <unistd.h>  #include <unistd.h>
31  #include <mysql/mysql.h>  #include <mysql/mysql.h>
32    
33  int bbs_login(MYSQL *db)  int bbs_login(void)
34  {  {
35          char username[BBS_username_max_len + 1];          char username[BBS_username_max_len + 1];
36          char password[BBS_password_max_len + 1];          char password[BBS_password_max_len + 1];
# Line 52  int bbs_login(MYSQL *db) Line 50  int bbs_login(MYSQL *db)
50    
51                  if (strcmp(username, "guest") == 0)                  if (strcmp(username, "guest") == 0)
52                  {                  {
53                          load_guest_info(db);                          load_guest_info();
54    
55                          return 0;                          return 0;
56                  }                  }
57    
58                  if (strcmp(username, "new") == 0)                  if (strcmp(username, "new") == 0)
59                  {                  {
60                          display_file(DATA_REGISTER, 1, 1);                          display_file(DATA_REGISTER, 1);
61    
62                          return 0;                          return 0;
63                  }                  }
# Line 74  int bbs_login(MYSQL *db) Line 72  int bbs_login(MYSQL *db)
72                                  continue;                                  continue;
73                          }                          }
74    
75                          ok = (check_user(db, username, password) == 0);                          ok = (check_user(username, password) == 0);
76                          iflush();                          iflush();
77                  }                  }
78          }          }
79    
80          if (!ok)          if (!ok)
81          {          {
82                  display_file(DATA_LOGIN_ERROR, 1, 1);                  display_file(DATA_LOGIN_ERROR, 1);
83                  return -1;                  return -1;
84          }          }
85    
# Line 91  int bbs_login(MYSQL *db) Line 89  int bbs_login(MYSQL *db)
89          return 0;          return 0;
90  }  }
91    
92  int check_user(MYSQL *db, const char *username, const char *password)  int check_user(const char *username, const char *password)
93  {  {
94          MYSQL_RES *rs;          MYSQL *db = NULL;
95            MYSQL_RES *rs = NULL;
96          MYSQL_ROW row;          MYSQL_ROW row;
97          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
98          int ret;          int ret = 0;
99          int BBS_uid = 0;          int BBS_uid = 0;
100          char client_addr[IP_ADDR_LEN];          char client_addr[IP_ADDR_LEN];
101          int i;          int i;
102          int ok = 1;          int ok = 1;
103          char user_tz_env[BBS_user_tz_max_len + 2];          char user_tz_env[BBS_user_tz_max_len + 2];
104    
105            db = db_open();
106            if (db == NULL)
107            {
108                    ret = -1;
109                    goto cleanup;
110            }
111    
112          // Verify format          // Verify format
113          for (i = 0; ok && username[i] != '\0'; i++)          for (i = 0; ok && username[i] != '\0'; i++)
114          {          {
# Line 130  int check_user(MYSQL *db, const char *us Line 136  int check_user(MYSQL *db, const char *us
136          if (!ok)          if (!ok)
137          {          {
138                  prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");                  prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");
139                  return 1;                  ret = 1;
140                    goto cleanup;
141          }          }
142    
143          // Begin transaction          // Begin transaction
144          if (mysql_query(db, "SET autocommit=0") != 0)          if (mysql_query(db, "SET autocommit=0") != 0)
145          {          {
146                  log_error("SET autocommit=0 error: %s\n", mysql_error(db));                  log_error("SET autocommit=0 error: %s\n", mysql_error(db));
147                  return -1;                  ret = -1;
148                    goto cleanup;
149          }          }
150    
151          if (mysql_query(db, "BEGIN") != 0)          if (mysql_query(db, "BEGIN") != 0)
152          {          {
153                  log_error("Begin transaction error: %s\n", mysql_error(db));                  log_error("Begin transaction error: %s\n", mysql_error(db));
154                  return -1;                  ret = -1;
155                    goto cleanup;
156          }          }
157    
158          // 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 152  int check_user(MYSQL *db, const char *us Line 161  int check_user(MYSQL *db, const char *us
161    
162          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
163                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "
164                           "WHERE login_dt >= SUBDATE(NOW(), INTERVAL 10 MINUTE) "                           "WHERE login_dt >= SUBDATE(NOW(), INTERVAL %d MINUTE) "
165                           "AND login_ip LIKE '%s'",                           "AND login_ip LIKE '%s'",
166                             BBS_login_failures_count_interval,
167                           ip_mask(client_addr, 1, '%'));                           ip_mask(client_addr, 1, '%'));
168          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
169          {          {
170                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s\n", mysql_error(db));
171                  return -1;                  ret = -1;
172                    goto cleanup;
173          }          }
174          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
175          {          {
176                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
177                  return -1;                  ret = -1;
178                    goto cleanup;
179          }          }
180          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
181          {          {
182                  if (atoi(row[0]) >= 2)                  if (atoi(row[0]) > BBS_allowed_login_failures_within_interval)
183                  {                  {
                         mysql_free_result(rs);  
   
184                          prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n");                          prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n");
185                            ret = 1;
186                          return 1;                          goto cleanup;
187                  }                  }
188          }          }
189          mysql_free_result(rs);          mysql_free_result(rs);
190            rs = NULL;
191    
192          // Failed login attempts against the current username during certain time period          // Failed login attempts against the current username during certain time period
193          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 186  int check_user(MYSQL *db, const char *us Line 197  int check_user(MYSQL *db, const char *us
197          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
198          {          {
199                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s\n", mysql_error(db));
200                  return -1;                  ret = -1;
201                    goto cleanup;
202          }          }
203          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
204          {          {
205                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
206                  return -1;                  ret = -1;
207                    goto cleanup;
208          }          }
209          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
210          {          {
211                  if (atoi(row[0]) >= 5)                  if (atoi(row[0]) >= 5)
212                  {                  {
                         mysql_free_result(rs);  
   
213                          prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n");                          prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n");
214                            ret = 1;
215                          return 1;                          goto cleanup;
216                  }                  }
217          }          }
218          mysql_free_result(rs);          mysql_free_result(rs);
219            rs = NULL;
220    
221          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
222                           "SELECT UID, username, p_login FROM user_list "                           "SELECT UID, username, p_login FROM user_list "
# Line 213  int check_user(MYSQL *db, const char *us Line 225  int check_user(MYSQL *db, const char *us
225          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
226          {          {
227                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s\n", mysql_error(db));
228                  return -1;                  ret = -1;
229                    goto cleanup;
230          }          }
231          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
232          {          {
233                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
234                  return -1;                  ret = -1;
235                    goto cleanup;
236          }          }
237          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
238          {          {
# Line 228  int check_user(MYSQL *db, const char *us Line 242  int check_user(MYSQL *db, const char *us
242                  int p_login = atoi(row[2]);                  int p_login = atoi(row[2]);
243    
244                  mysql_free_result(rs);                  mysql_free_result(rs);
245                    rs = NULL;
246    
247                  // Add user login log                  // Add user login log
248                  snprintf(sql, sizeof(sql),                  snprintf(sql, sizeof(sql),
# Line 237  int check_user(MYSQL *db, const char *us Line 252  int check_user(MYSQL *db, const char *us
252                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
253                  {                  {
254                          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));
255                          return -1;                          ret = -1;
256                            goto cleanup;
257                  }                  }
258    
259                  // Commit transaction                  // Commit transaction
260                  if (mysql_query(db, "COMMIT") != 0)                  if (mysql_query(db, "COMMIT") != 0)
261                  {                  {
262                          log_error("Commit transaction error: %s\n", mysql_error(db));                          log_error("Commit transaction error: %s\n", mysql_error(db));
263                          return -1;                          ret = -1;
264                            goto cleanup;
265                  }                  }
266    
267                  if (p_login == 0)                  if (p_login == 0)
268                  {                  {
                         mysql_free_result(rs);  
   
269                          prints("\033[1;31m您目前无权登陆...\033[m\r\n");                          prints("\033[1;31m您目前无权登陆...\033[m\r\n");
270                          return 1;                          ret = 1;
271                            goto cleanup;
272                  }                  }
273          }          }
274          else          else
275          {          {
276                  mysql_free_result(rs);                  mysql_free_result(rs);
277                    rs = NULL;
278    
279                  snprintf(sql, sizeof(sql),                  snprintf(sql, sizeof(sql),
280                                   "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 266  int check_user(MYSQL *db, const char *us Line 283  int check_user(MYSQL *db, const char *us
283                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
284                  {                  {
285                          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));
286                          return -1;                          ret = -1;
287                            goto cleanup;
288                  }                  }
289    
290                  // Commit transaction                  // Commit transaction
291                  if (mysql_query(db, "COMMIT") != 0)                  if (mysql_query(db, "COMMIT") != 0)
292                  {                  {
293                          log_error("Commit transaction error: %s\n", mysql_error(db));                          log_error("Commit transaction error: %s\n", mysql_error(db));
294                          return -1;                          ret = -1;
295                            goto cleanup;
296                  }                  }
297    
298                  prints("\033[1;31m错误的用户名或密码...\033[m\r\n");                  prints("\033[1;31m错误的用户名或密码...\033[m\r\n");
299                  return 1;                  ret = 1;
300                    goto cleanup;
301          }          }
302    
303          // Set AUTOCOMMIT = 1          // Set AUTOCOMMIT = 1
304          if (mysql_query(db, "SET autocommit=1") != 0)          if (mysql_query(db, "SET autocommit=1") != 0)
305          {          {
306                  log_error("SET autocommit=1 error: %s\n", mysql_error(db));                  log_error("SET autocommit=1 error: %s\n", mysql_error(db));
307                  return -1;                  ret = -1;
308                    goto cleanup;
309          }          }
310    
311          ret = load_user_info(db, BBS_uid);          ret = load_user_info(db, BBS_uid);
# Line 295  int check_user(MYSQL *db, const char *us Line 316  int check_user(MYSQL *db, const char *us
316                  break;                  break;
317          case -1: // Load data error          case -1: // Load data error
318                  prints("\033[1;31m读取用户数据错误...\033[m\r\n");                  prints("\033[1;31m读取用户数据错误...\033[m\r\n");
319                  return -1;                  ret = -1;
320                    goto cleanup;
321          case -2: // Unused          case -2: // Unused
322                  prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");                  prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");
323                  return 1;                  ret = 1;
324                    goto cleanup;
325          case -3: // Dead          case -3: // Dead
326                  prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");                  prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
327                  return 1;                  ret = 1;
328                    goto cleanup;
329          default:          default:
330                  return -2;                  ret = -2;
331                    goto cleanup;
332          }          }
333    
334          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 313  int check_user(MYSQL *db, const char *us Line 338  int check_user(MYSQL *db, const char *us
338          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
339          {          {
340                  log_error("Update user_pubinfo error: %s\n", mysql_error(db));                  log_error("Update user_pubinfo error: %s\n", mysql_error(db));
341                  return -1;                  ret = -1;
342                    goto cleanup;
343          }          }
344    
345          if (user_online_add(db) != 0)          if (user_online_add(db) != 0)
346          {          {
347                  return -1;                  ret = -1;
348                    goto cleanup;
349          }          }
350    
351          BBS_last_access_tm = BBS_login_tm = time(0);          BBS_last_access_tm = BBS_login_tm = time(NULL);
352    
353          // Set user tz to process env          // Set user tz to process env
354          if (BBS_user_tz[0] != '\0')          if (BBS_user_tz[0] != '\0')
# Line 339  int check_user(MYSQL *db, const char *us Line 366  int check_user(MYSQL *db, const char *us
366                  tzset();                  tzset();
367          }          }
368    
369          return 0;  cleanup:
370            mysql_free_result(rs);
371            mysql_close(db);
372    
373            return ret;
374  }  }
375    
376  int load_user_info(MYSQL *db, int BBS_uid)  int load_user_info(MYSQL *db, int BBS_uid)
377  {  {
378          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
379          MYSQL_ROW row;          MYSQL_ROW row;
380          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
381          int life;          int life;
382          time_t last_login_dt;          time_t last_login_dt;
383            int ret = 0;
384    
385          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
386                           "SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone "                           "SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone, exp, nickname "
387                           "FROM user_pubinfo WHERE UID = %d",                           "FROM user_pubinfo WHERE UID = %d",
388                           BBS_uid);                           BBS_uid);
389          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
390          {          {
391                  log_error("Query user_pubinfo error: %s\n", mysql_error(db));                  log_error("Query user_pubinfo error: %s\n", mysql_error(db));
392                  return -1;                  ret = -1;
393                    goto cleanup;
394          }          }
395          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
396          {          {
397                  log_error("Get user_pubinfo data failed\n");                  log_error("Get user_pubinfo data failed\n");
398                  return -1;                  ret = -1;
399                    goto cleanup;
400          }          }
401          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
402          {          {
# Line 371  int load_user_info(MYSQL *db, int BBS_ui Line 405  int load_user_info(MYSQL *db, int BBS_ui
405    
406                  strncpy(BBS_user_tz, row[2], sizeof(BBS_user_tz) - 1);                  strncpy(BBS_user_tz, row[2], sizeof(BBS_user_tz) - 1);
407                  BBS_user_tz[sizeof(BBS_user_tz) - 1] = '\0';                  BBS_user_tz[sizeof(BBS_user_tz) - 1] = '\0';
408    
409                    BBS_user_exp = atoi(row[3]);
410    
411                    strncpy(BBS_nickname, row[4], sizeof(BBS_nickname));
412                    BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
413          }          }
414          else          else
415          {          {
416                  mysql_free_result(rs);                  ret = -1; // Data not found
417                  return -1; // Data not found                  goto cleanup;
418          }          }
419          mysql_free_result(rs);          mysql_free_result(rs);
420            rs = NULL;
421    
422          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
423                  time(0) - last_login_dt > 60 * 60 * 24 * life)                  time(NULL) - last_login_dt > 60 * 60 * 24 * life)
424          {          {
425                  return -3; // Dead                  ret = -3; // Dead
426                    goto cleanup;
427          }          }
428    
429          if (load_priv(db, &BBS_priv, BBS_uid) != 0)          if (load_priv(db, &BBS_priv, BBS_uid) != 0)
430          {          {
431                  return -1;                  ret = -1;
432                    goto cleanup;
433          }          }
434    
435          return 0;  cleanup:
436            mysql_free_result(rs);
437    
438            return ret;
439  }  }
440    
441  int load_guest_info(MYSQL *db)  int load_guest_info(void)
442  {  {
443            MYSQL *db = NULL;
444            int ret = 0;
445    
446            db = db_open();
447            if (db == NULL)
448            {
449                    ret = -1;
450                    goto cleanup;
451            }
452    
453          strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);          strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);
454          BBS_username[sizeof(BBS_username) - 1] = '\0';          BBS_username[sizeof(BBS_username) - 1] = '\0';
455    
456            BBS_user_exp = 0;
457    
458            strncpy(BBS_nickname, "Guest", sizeof(BBS_nickname));
459            BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
460    
461          if (load_priv(db, &BBS_priv, 0) != 0)          if (load_priv(db, &BBS_priv, 0) != 0)
462          {          {
463                  return -1;                  ret = -1;
464                    goto cleanup;
465          }          }
466    
467          if (user_online_add(db) != 0)          if (user_online_add(db) != 0)
468          {          {
469                  return -1;                  ret = -1;
470                    goto cleanup;
471          }          }
472    
473          BBS_last_access_tm = BBS_login_tm = time(0);          BBS_last_access_tm = BBS_login_tm = time(NULL);
474    
475          return 0;  cleanup:
476            mysql_close(db);
477    
478            return ret;
479  }  }
480    
481  int user_online_add(MYSQL *db)  int user_online_add(MYSQL *db)
# Line 450  int user_online_del(MYSQL *db) Line 515  int user_online_del(MYSQL *db)
515    
516          return 0;          return 0;
517  }  }
518    
519    int user_online_update(const char *action)
520    {
521            MYSQL *db = NULL;
522            char sql[SQL_BUFFER_LEN];
523    
524            if (strcmp(BBS_current_action, action) == 0) // No change
525            {
526                    return 0;
527            }
528    
529            strncpy(BBS_current_action, action, sizeof(BBS_current_action) - 1);
530            BBS_current_action[sizeof(BBS_current_action) - 1] = '\0';
531    
532            db = db_open();
533            if (db == NULL)
534            {
535                    log_error("db_open() error: %s\n", mysql_error(db));
536                    return -1;
537            }
538    
539            snprintf(sql, sizeof(sql),
540                             "UPDATE user_online SET current_action = '%s', last_tm=NOW() "
541                             "WHERE SID = 'Telnet_Process_%d'",
542                             BBS_current_action, getpid());
543            if (mysql_query(db, sql) != 0)
544            {
545                    log_error("Update user_online error: %s\n", mysql_error(db));
546                    return -2;
547            }
548    
549            mysql_close(db);
550    
551            return 1;
552    }


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

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