/[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.66 by sysadm, Tue Nov 4 14:58:56 2025 UTC Revision 1.75 by sysadm, Fri Dec 19 06:16:27 2025 UTC
# Line 6  Line 6 
6   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7   */   */
8    
9    #ifdef HAVE_CONFIG_H
10    #include "config.h"
11    #endif
12    
13  #include "bbs.h"  #include "bbs.h"
14  #include "common.h"  #include "common.h"
15  #include "database.h"  #include "database.h"
# Line 21  Line 25 
25  #include <string.h>  #include <string.h>
26  #include <regex.h>  #include <regex.h>
27  #include <unistd.h>  #include <unistd.h>
28  #include <mysql/mysql.h>  #include <mysql.h>
29  #include <sys/param.h>  #include <sys/param.h>
30    
31  static const int BBS_username_min_len = 3; // common len = 5, special len = 3  static const int BBS_username_min_len = 3; // common len = 5, special len = 3
32  static const int BBS_password_min_len = 5; // legacy len = 5, current len = 6  static const int BBS_password_min_len = 5; // legacy len = 5, current len = 6
33    
34    static const int BBS_allowed_login_failures_within_interval = 10;
35    static const int BBS_login_failures_count_interval = 10; // minutes
36    static const int BBS_allowed_login_failures_per_account = 3;
37    
38    const int BBS_login_retry_times = 3;
39    
40  int bbs_login(void)  int bbs_login(void)
41  {  {
42          char username[BBS_username_max_len + 1];          char username[BBS_username_max_len + 1];
43          char password[BBS_password_max_len + 1];          char password[BBS_password_max_len + 1];
44          int i = 0;          int i = 0;
45          int ok = 0;          int ok = 0;
46            int ret;
47    
48          for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++)          for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++)
49          {          {
# Line 69  int bbs_login(void) Line 80  int bbs_login(void)
80                                  continue;                                  continue;
81                          }                          }
82    
83                          ok = (check_user(username, password) == 0);                          ret = check_user(username, password);
84                            if (ret == 2) // Enforce update user agreement
85                            {
86                                    BBS_update_eula = 1;
87                                    ret = 0;
88                            }
89    
90                            ok = (ret == 0);
91                          iflush();                          iflush();
92                  }                  }
93          }          }
# Line 80  int bbs_login(void) Line 98  int bbs_login(void)
98                  return -1;                  return -1;
99          }          }
100    
         log_common("User \"%s\"(%ld) login from %s:%d\n",  
                            BBS_username, BBS_priv.uid, hostaddr_client, port_client);  
   
101          return 0;          return 0;
102  }  }
103    
# Line 109  int check_user(const char *username, con Line 124  int check_user(const char *username, con
124          // Verify format          // Verify format
125          for (i = 0; ok && username[i] != '\0'; i++)          for (i = 0; ok && username[i] != '\0'; i++)
126          {          {
127                  if (!(isalpha(username[i]) || (i > 0 && (isdigit(username[i]) || username[i] == '_'))))                  if (!(isalpha((int)username[i]) || (i > 0 && (isdigit((int)username[i]) || username[i] == '_'))))
128                  {                  {
129                          ok = 0;                          ok = 0;
130                  }                  }
# Line 120  int check_user(const char *username, con Line 135  int check_user(const char *username, con
135          }          }
136          for (i = 0; ok && password[i] != '\0'; i++)          for (i = 0; ok && password[i] != '\0'; i++)
137          {          {
138                  if (!isalnum(password[i]))                  if (!isalnum((int)password[i]))
139                  {                  {
140                          ok = 0;                          ok = 0;
141                  }                  }
# Line 140  int check_user(const char *username, con Line 155  int check_user(const char *username, con
155          // Begin transaction          // Begin transaction
156          if (mysql_query(db, "SET autocommit=0") != 0)          if (mysql_query(db, "SET autocommit=0") != 0)
157          {          {
158                  log_error("SET autocommit=0 error: %s\n", mysql_error(db));                  log_error("SET autocommit=0 error: %s", mysql_error(db));
159                  ret = -1;                  ret = -1;
160                  goto cleanup;                  goto cleanup;
161          }          }
162    
163          if (mysql_query(db, "BEGIN") != 0)          if (mysql_query(db, "BEGIN") != 0)
164          {          {
165                  log_error("Begin transaction error: %s\n", mysql_error(db));                  log_error("Begin transaction error: %s", mysql_error(db));
166                  ret = -1;                  ret = -1;
167                  goto cleanup;                  goto cleanup;
168          }          }
# Line 164  int check_user(const char *username, con Line 179  int check_user(const char *username, con
179                           ip_mask(client_addr, 1, '%'));                           ip_mask(client_addr, 1, '%'));
180          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
181          {          {
182                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s", mysql_error(db));
183                  ret = -1;                  ret = -1;
184                  goto cleanup;                  goto cleanup;
185          }          }
186          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
187          {          {
188                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed");
189                  ret = -1;                  ret = -1;
190                  goto cleanup;                  goto cleanup;
191          }          }
# Line 197  int check_user(const char *username, con Line 212  int check_user(const char *username, con
212                           username);                           username);
213          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
214          {          {
215                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s", mysql_error(db));
216                  ret = -1;                  ret = -1;
217                  goto cleanup;                  goto cleanup;
218          }          }
219          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
220          {          {
221                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed");
222                  ret = -1;                  ret = -1;
223                  goto cleanup;                  goto cleanup;
224          }          }
# Line 225  int check_user(const char *username, con Line 240  int check_user(const char *username, con
240                           username, password);                           username, password);
241          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
242          {          {
243                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s", mysql_error(db));
244                  ret = -1;                  ret = -1;
245                  goto cleanup;                  goto cleanup;
246          }          }
247          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
248          {          {
249                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed");
250                  ret = -1;                  ret = -1;
251                  goto cleanup;                  goto cleanup;
252          }          }
# Line 252  int check_user(const char *username, con Line 267  int check_user(const char *username, con
267                                   BBS_uid, hostaddr_client);                                   BBS_uid, hostaddr_client);
268                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
269                  {                  {
270                          log_error("Insert into user_login_log error: %s\n", mysql_error(db));                          log_error("Insert into user_login_log error: %s", mysql_error(db));
271                          ret = -1;                          ret = -1;
272                          goto cleanup;                          goto cleanup;
273                  }                  }
# Line 260  int check_user(const char *username, con Line 275  int check_user(const char *username, con
275                  // Commit transaction                  // Commit transaction
276                  if (mysql_query(db, "COMMIT") != 0)                  if (mysql_query(db, "COMMIT") != 0)
277                  {                  {
278                          log_error("Commit transaction error: %s\n", mysql_error(db));                          log_error("Commit transaction error: %s", mysql_error(db));
279                          ret = -1;                          ret = -1;
280                          goto cleanup;                          goto cleanup;
281                  }                  }
# Line 283  int check_user(const char *username, con Line 298  int check_user(const char *username, con
298                                   username, password, hostaddr_client);                                   username, password, hostaddr_client);
299                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
300                  {                  {
301                          log_error("Insert into user_err_login_log error: %s\n", mysql_error(db));                          log_error("Insert into user_err_login_log error: %s", mysql_error(db));
302                          ret = -1;                          ret = -1;
303                          goto cleanup;                          goto cleanup;
304                  }                  }
# Line 291  int check_user(const char *username, con Line 306  int check_user(const char *username, con
306                  // Commit transaction                  // Commit transaction
307                  if (mysql_query(db, "COMMIT") != 0)                  if (mysql_query(db, "COMMIT") != 0)
308                  {                  {
309                          log_error("Commit transaction error: %s\n", mysql_error(db));                          log_error("Commit transaction error: %s", mysql_error(db));
310                          ret = -1;                          ret = -1;
311                          goto cleanup;                          goto cleanup;
312                  }                  }
# Line 304  int check_user(const char *username, con Line 319  int check_user(const char *username, con
319          // Set AUTOCOMMIT = 1          // Set AUTOCOMMIT = 1
320          if (mysql_query(db, "SET autocommit=1") != 0)          if (mysql_query(db, "SET autocommit=1") != 0)
321          {          {
322                  log_error("SET autocommit=1 error: %s\n", mysql_error(db));                  log_error("SET autocommit=1 error: %s", mysql_error(db));
323                  ret = -1;                  ret = -1;
324                  goto cleanup;                  goto cleanup;
325          }          }
326    
327            if (!SSH_v2 && checklevel2(&BBS_priv, P_MAN_S))
328            {
329                    prints("\033[1;31m非普通账户必须使用SSH方式登录\033[m\r\n");
330                    ret = 1;
331                    goto cleanup;
332            }
333    
334          ret = load_user_info(db, BBS_uid);          ret = load_user_info(db, BBS_uid);
335    
336          switch (ret)          switch (ret)
# Line 319  int check_user(const char *username, con Line 341  int check_user(const char *username, con
341                  prints("\033[1;31m读取用户数据错误...\033[m\r\n");                  prints("\033[1;31m读取用户数据错误...\033[m\r\n");
342                  ret = -1;                  ret = -1;
343                  goto cleanup;                  goto cleanup;
344          case -2: // Unused          case -2: // Enforce update user agreement
345                  prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");                  ret = 2;
                 ret = 1;  
346                  goto cleanup;                  goto cleanup;
347          case -3: // Dead          case -3: // Dead
348                  prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");                  prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
# Line 338  int check_user(const char *username, con Line 359  int check_user(const char *username, con
359                           BBS_uid);                           BBS_uid);
360          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
361          {          {
362                  log_error("Update user_pubinfo error: %s\n", mysql_error(db));                  log_error("Update user_pubinfo error: %s", mysql_error(db));
363                  ret = -1;                  ret = -1;
364                  goto cleanup;                  goto cleanup;
365          }          }
# Line 360  int check_user(const char *username, con Line 381  int check_user(const char *username, con
381    
382                  if (setenv("TZ", user_tz_env, 1) == -1)                  if (setenv("TZ", user_tz_env, 1) == -1)
383                  {                  {
384                          log_error("setenv(TZ = %s) error %d\n", user_tz_env, errno);                          log_error("setenv(TZ = %s) error %d", user_tz_env, errno);
385                          return -3;                          return -3;
386                  }                  }
387    
# Line 389  int load_user_info(MYSQL *db, int BBS_ui Line 410  int load_user_info(MYSQL *db, int BBS_ui
410                           BBS_uid);                           BBS_uid);
411          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
412          {          {
413                  log_error("Query user_pubinfo error: %s\n", mysql_error(db));                  log_error("Query user_pubinfo error: %s", mysql_error(db));
414                  ret = -1;                  ret = -1;
415                  goto cleanup;                  goto cleanup;
416          }          }
417          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
418          {          {
419                  log_error("Get user_pubinfo data failed\n");                  log_error("Get user_pubinfo data failed");
420                  ret = -1;                  ret = -1;
421                  goto cleanup;                  goto cleanup;
422          }          }
# Line 429  int load_user_info(MYSQL *db, int BBS_ui Line 450  int load_user_info(MYSQL *db, int BBS_ui
450    
451          if (load_priv(db, &BBS_priv, BBS_uid) != 0)          if (load_priv(db, &BBS_priv, BBS_uid) != 0)
452          {          {
453                  ret = -1;                  ret = -1; // Data not found
454                    goto cleanup;
455            }
456    
457            if (last_login_dt < BBS_eula_tm)
458            {
459                    ret = -2; // require update agreement first
460                  goto cleanup;                  goto cleanup;
461          }          }
462    
# Line 488  int user_online_add(MYSQL *db) Line 515  int user_online_add(MYSQL *db)
515                           hostaddr_client);                           hostaddr_client);
516          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
517          {          {
518                  log_error("Add visit log error: %s\n", mysql_error(db));                  log_error("Add visit log error: %s", mysql_error(db));
519                  return -1;                  return -1;
520          }          }
521    
# Line 503  int user_online_add(MYSQL *db) Line 530  int user_online_add(MYSQL *db)
530                           getpid(), BBS_priv.uid, hostaddr_client);                           getpid(), BBS_priv.uid, hostaddr_client);
531          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
532          {          {
533                  log_error("Add user_online error: %s\n", mysql_error(db));                  log_error("Add user_online error: %s", mysql_error(db));
534                  return -3;                  return -3;
535          }          }
536    
# Line 519  int user_online_del(MYSQL *db) Line 546  int user_online_del(MYSQL *db)
546                           getpid());                           getpid());
547          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
548          {          {
549                  log_error("Delete user_online error: %s\n", mysql_error(db));                  log_error("Delete user_online error: %s", mysql_error(db));
550                  return -1;                  return -1;
551          }          }
552    
# Line 540  int user_online_exp(MYSQL *db) Line 567  int user_online_exp(MYSQL *db)
567                           BBS_priv.uid);                           BBS_priv.uid);
568          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
569          {          {
570                  log_error("Update user_pubinfo error: %s\n", mysql_error(db));                  log_error("Update user_pubinfo error: %s", mysql_error(db));
571                  return -1;                  return -1;
572          }          }
573    
# Line 569  int user_online_update(const char *actio Line 596  int user_online_update(const char *actio
596          db = db_open();          db = db_open();
597          if (db == NULL)          if (db == NULL)
598          {          {
599                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s", mysql_error(db));
600                  return -1;                  return -1;
601          }          }
602    
# Line 579  int user_online_update(const char *actio Line 606  int user_online_update(const char *actio
606                           BBS_current_action, getpid());                           BBS_current_action, getpid());
607          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
608          {          {
609                  log_error("Update user_online error: %s\n", mysql_error(db));                  log_error("Update user_online error: %s", mysql_error(db));
610                  return -2;                  return -2;
611          }          }
612    
# Line 587  int user_online_update(const char *actio Line 614  int user_online_update(const char *actio
614    
615          return 1;          return 1;
616  }  }
617    
618    int user_update_agreement(void)
619    {
620            MYSQL *db = NULL;
621            char sql[SQL_BUFFER_LEN];
622            int ch;
623            int ret = 0;
624    
625            clearscr();
626            moveto(2, 1);
627            prints("尊敬的用户:");
628            moveto(3, 1);
629            prints("    本站的《用户许可协议》已更新,您必须在仔细阅读并接受后,才能继续使用本站提供的服务。");
630            press_any_key();
631    
632            clearscr();
633            display_file(DATA_EULA, 1);
634    
635            while (!SYS_server_exit)
636            {
637                    clearscr();
638                    moveto(2, 1);
639    
640                    ch = press_any_key_ex("您是否接受本站的《用户许可协议》? (A)接受, (D)拒绝, (R)再看看协议 [D]", 60);
641                    switch (toupper(ch))
642                    {
643                    case KEY_NULL:
644                            return -1;
645                    case KEY_TIMEOUT:
646                    case CR:
647                    case 'D':
648                            moveto(3, 1);
649                            prints("您已拒绝《用户许可协议》,再见!");
650                            press_any_key();
651                            return 0;
652                    case 'R':
653                            clearscr();
654                            display_file(DATA_EULA, 1);
655                            continue;
656                    case 'A':
657                            db = db_open();
658                            if (db == NULL)
659                            {
660                                    ret = -1;
661                                    goto cleanup;
662                            }
663    
664                            snprintf(sql, sizeof(sql),
665                                             "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
666                                             "last_login_dt = NOW() WHERE UID = %d",
667                                             BBS_priv.uid);
668                            if (mysql_query(db, sql) != 0)
669                            {
670                                    log_error("Update user_pubinfo error: %s", mysql_error(db));
671                                    ret = -1;
672                                    goto cleanup;
673                            }
674    
675                            mysql_close(db);
676                            db = NULL;
677    
678                            moveto(3, 1);
679                            prints("您已接受《用户许可协议》,请重新登陆。");
680                            press_any_key();
681                            return 1;
682                    default:
683                            continue;
684                    }
685    
686                    break;
687            }
688    
689    cleanup:
690            mysql_close(db);
691    
692            return ret;
693    }


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

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