/[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.71 by sysadm, Fri Nov 28 14:35:38 2025 UTC Revision 1.80 by sysadm, Sun Jan 4 13:24:00 2026 UTC
# Line 3  Line 3 
3   * login   * login
4   *   - user authentication and online status manager   *   - user authentication and online status manager
5   *   *
6   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>   * Copyright (C) 2004-2026  Leaflet <leaflet@leafok.com>
7   */   */
8    
9  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
# Line 41  int bbs_login(void) Line 41  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;
45          int ok = 0;          int ret;
46    
47          for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++)          for (i = 0; !SYS_server_exit && i < BBS_login_retry_times; i++)
48          {          {
49                    clrtoeol();
50                  prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', "                  prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', "
51                             "注册请输入`\033[1;31mnew\033[m'): ");                             "注册请输入`\033[1;31mnew\033[m'): ");
52                  iflush();                  iflush();
# Line 58  int bbs_login(void) Line 59  int bbs_login(void)
59                  if (strcmp(username, "guest") == 0)                  if (strcmp(username, "guest") == 0)
60                  {                  {
61                          load_guest_info();                          load_guest_info();
62                            log_common("User [%s] authenticated successfully", username);
63                          return 0;                          return 0;
64                  }                  }
65    
66                  if (strcmp(username, "new") == 0)                  if (strcmp(username, "new") == 0)
67                  {                  {
68                          display_file(DATA_REGISTER, 1);                          display_file(DATA_REGISTER, 1);
   
69                          return -1;                          return -1;
70                  }                  }
71    
72                  if (username[0] != '\0')                  if (username[0] != '\0')
73                  {                  {
74                            clrtoeol();
75                          prints("\033[1;37m请输入密码\033[m: ");                          prints("\033[1;37m请输入密码\033[m: ");
76                          iflush();                          iflush();
77    
# Line 79  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                            if (ret == 0)
91                            {
92                                    log_common("User [%s] authenticated successfully", username);
93                                    return 0;
94                            }
95    
96                            log_common("User [%s] authentication failed (%d/%d)", username,
97                                               i + 1, BBS_login_retry_times);
98                          iflush();                          iflush();
99                  }                  }
100          }          }
101    
102          if (!ok)          display_file(DATA_LOGIN_ERROR, 1);
103          {          return -1;
                 display_file(DATA_LOGIN_ERROR, 1);  
                 return -1;  
         }  
   
         log_common("User \"%s\"(%ld) login from %s:%d\n",  
                            BBS_username, BBS_priv.uid, hostaddr_client, port_client);  
   
         return 0;  
104  }  }
105    
106  int check_user(const char *username, const char *password)  int check_user(const char *username, const char *password)
# Line 150  int check_user(const char *username, con Line 157  int check_user(const char *username, con
157          // Begin transaction          // Begin transaction
158          if (mysql_query(db, "SET autocommit=0") != 0)          if (mysql_query(db, "SET autocommit=0") != 0)
159          {          {
160                  log_error("SET autocommit=0 error: %s\n", mysql_error(db));                  log_error("SET autocommit=0 error: %s", mysql_error(db));
161                  ret = -1;                  ret = -1;
162                  goto cleanup;                  goto cleanup;
163          }          }
164    
165          if (mysql_query(db, "BEGIN") != 0)          if (mysql_query(db, "BEGIN") != 0)
166          {          {
167                  log_error("Begin transaction error: %s\n", mysql_error(db));                  log_error("Begin transaction error: %s", mysql_error(db));
168                  ret = -1;                  ret = -1;
169                  goto cleanup;                  goto cleanup;
170          }          }
# Line 174  int check_user(const char *username, con Line 181  int check_user(const char *username, con
181                           ip_mask(client_addr, 1, '%'));                           ip_mask(client_addr, 1, '%'));
182          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
183          {          {
184                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s", mysql_error(db));
185                  ret = -1;                  ret = -1;
186                  goto cleanup;                  goto cleanup;
187          }          }
188          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
189          {          {
190                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed");
191                  ret = -1;                  ret = -1;
192                  goto cleanup;                  goto cleanup;
193          }          }
# Line 207  int check_user(const char *username, con Line 214  int check_user(const char *username, con
214                           username);                           username);
215          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
216          {          {
217                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s", mysql_error(db));
218                  ret = -1;                  ret = -1;
219                  goto cleanup;                  goto cleanup;
220          }          }
221          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
222          {          {
223                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed");
224                  ret = -1;                  ret = -1;
225                  goto cleanup;                  goto cleanup;
226          }          }
# Line 235  int check_user(const char *username, con Line 242  int check_user(const char *username, con
242                           username, password);                           username, password);
243          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
244          {          {
245                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s", mysql_error(db));
246                  ret = -1;                  ret = -1;
247                  goto cleanup;                  goto cleanup;
248          }          }
249          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
250          {          {
251                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed");
252                  ret = -1;                  ret = -1;
253                  goto cleanup;                  goto cleanup;
254          }          }
# Line 262  int check_user(const char *username, con Line 269  int check_user(const char *username, con
269                                   BBS_uid, hostaddr_client);                                   BBS_uid, hostaddr_client);
270                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
271                  {                  {
272                          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));
273                          ret = -1;                          ret = -1;
274                          goto cleanup;                          goto cleanup;
275                  }                  }
# Line 270  int check_user(const char *username, con Line 277  int check_user(const char *username, con
277                  // Commit transaction                  // Commit transaction
278                  if (mysql_query(db, "COMMIT") != 0)                  if (mysql_query(db, "COMMIT") != 0)
279                  {                  {
280                          log_error("Commit transaction error: %s\n", mysql_error(db));                          log_error("Commit transaction error: %s", mysql_error(db));
281                          ret = -1;                          ret = -1;
282                          goto cleanup;                          goto cleanup;
283                  }                  }
# Line 293  int check_user(const char *username, con Line 300  int check_user(const char *username, con
300                                   username, password, hostaddr_client);                                   username, password, hostaddr_client);
301                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
302                  {                  {
303                          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));
304                          ret = -1;                          ret = -1;
305                          goto cleanup;                          goto cleanup;
306                  }                  }
# Line 301  int check_user(const char *username, con Line 308  int check_user(const char *username, con
308                  // Commit transaction                  // Commit transaction
309                  if (mysql_query(db, "COMMIT") != 0)                  if (mysql_query(db, "COMMIT") != 0)
310                  {                  {
311                          log_error("Commit transaction error: %s\n", mysql_error(db));                          log_error("Commit transaction error: %s", mysql_error(db));
312                          ret = -1;                          ret = -1;
313                          goto cleanup;                          goto cleanup;
314                  }                  }
# Line 314  int check_user(const char *username, con Line 321  int check_user(const char *username, con
321          // Set AUTOCOMMIT = 1          // Set AUTOCOMMIT = 1
322          if (mysql_query(db, "SET autocommit=1") != 0)          if (mysql_query(db, "SET autocommit=1") != 0)
323          {          {
324                  log_error("SET autocommit=1 error: %s\n", mysql_error(db));                  log_error("SET autocommit=1 error: %s", mysql_error(db));
325                  ret = -1;                  ret = -1;
326                  goto cleanup;                  goto cleanup;
327          }          }
# Line 324  int check_user(const char *username, con Line 331  int check_user(const char *username, con
331          switch (ret)          switch (ret)
332          {          {
333          case 0: // Login successfully          case 0: // Login successfully
334                    if (!SSH_v2 && checklevel2(&BBS_priv, P_MAN_S))
335                    {
336                            prints("\033[1;31m非普通账户必须使用SSH方式登录\033[m\r\n");
337                            ret = 1;
338                            goto cleanup;
339                    }
340                  break;                  break;
341          case -1: // Load data error          case -1: // Load data error
342                  prints("\033[1;31m读取用户数据错误...\033[m\r\n");                  prints("\033[1;31m读取用户数据错误...\033[m\r\n");
343                  ret = -1;                  ret = -1;
344                  goto cleanup;                  goto cleanup;
345          case -2: // Unused          case -2: // Enforce update user agreement
346                  prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");                  if (!SSH_v2 && checklevel2(&BBS_priv, P_MAN_S))
347                  ret = 1;                  {
348                            prints("\033[1;31m非普通账户必须使用SSH方式登录\033[m\r\n");
349                            ret = 1;
350                            goto cleanup;
351                    }
352                    ret = 2;
353                  goto cleanup;                  goto cleanup;
354          case -3: // Dead          case -3: // Dead
355                  prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");                  prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
# Line 342  int check_user(const char *username, con Line 360  int check_user(const char *username, con
360                  goto cleanup;                  goto cleanup;
361          }          }
362    
         if (!SSH_v2 && checklevel2(&BBS_priv, P_MAN_S))  
         {  
                 prints("\033[1;31m非普通账户必须使用SSH方式登录\033[m\r\n");  
                 ret = 1;  
                 goto cleanup;  
         }  
   
363          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
364                           "UPDATE user_pubinfo SET visit_count = visit_count + 1, "                           "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
365                           "last_login_dt = NOW() WHERE UID = %d",                           "last_login_dt = NOW() WHERE UID = %d",
366                           BBS_uid);                           BBS_uid);
367          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
368          {          {
369                  log_error("Update user_pubinfo error: %s\n", mysql_error(db));                  log_error("Update user_pubinfo error: %s", mysql_error(db));
370                  ret = -1;                  ret = -1;
371                  goto cleanup;                  goto cleanup;
372          }          }
# Line 377  int check_user(const char *username, con Line 388  int check_user(const char *username, con
388    
389                  if (setenv("TZ", user_tz_env, 1) == -1)                  if (setenv("TZ", user_tz_env, 1) == -1)
390                  {                  {
391                          log_error("setenv(TZ = %s) error %d\n", user_tz_env, errno);                          log_error("setenv(TZ = %s) error %d", user_tz_env, errno);
392                          return -3;                          return -3;
393                  }                  }
394    
# Line 406  int load_user_info(MYSQL *db, int BBS_ui Line 417  int load_user_info(MYSQL *db, int BBS_ui
417                           BBS_uid);                           BBS_uid);
418          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
419          {          {
420                  log_error("Query user_pubinfo error: %s\n", mysql_error(db));                  log_error("Query user_pubinfo error: %s", mysql_error(db));
421                  ret = -1;                  ret = -1;
422                  goto cleanup;                  goto cleanup;
423          }          }
424          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
425          {          {
426                  log_error("Get user_pubinfo data failed\n");                  log_error("Get user_pubinfo data failed");
427                  ret = -1;                  ret = -1;
428                  goto cleanup;                  goto cleanup;
429          }          }
# Line 446  int load_user_info(MYSQL *db, int BBS_ui Line 457  int load_user_info(MYSQL *db, int BBS_ui
457    
458          if (load_priv(db, &BBS_priv, BBS_uid) != 0)          if (load_priv(db, &BBS_priv, BBS_uid) != 0)
459          {          {
460                  ret = -1;                  ret = -1; // Data not found
461                    goto cleanup;
462            }
463    
464            if (last_login_dt < BBS_eula_tm)
465            {
466                    ret = -2; // require update agreement first
467                  goto cleanup;                  goto cleanup;
468          }          }
469    
# Line 505  int user_online_add(MYSQL *db) Line 522  int user_online_add(MYSQL *db)
522                           hostaddr_client);                           hostaddr_client);
523          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
524          {          {
525                  log_error("Add visit log error: %s\n", mysql_error(db));                  log_error("Add visit log error: %s", mysql_error(db));
526                  return -1;                  return -1;
527          }          }
528    
# Line 520  int user_online_add(MYSQL *db) Line 537  int user_online_add(MYSQL *db)
537                           getpid(), BBS_priv.uid, hostaddr_client);                           getpid(), BBS_priv.uid, hostaddr_client);
538          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
539          {          {
540                  log_error("Add user_online error: %s\n", mysql_error(db));                  log_error("Add user_online error: %s", mysql_error(db));
541                  return -3;                  return -3;
542          }          }
543    
# Line 536  int user_online_del(MYSQL *db) Line 553  int user_online_del(MYSQL *db)
553                           getpid());                           getpid());
554          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
555          {          {
556                  log_error("Delete user_online error: %s\n", mysql_error(db));                  log_error("Delete user_online error: %s", mysql_error(db));
557                  return -1;                  return -1;
558          }          }
559    
# Line 557  int user_online_exp(MYSQL *db) Line 574  int user_online_exp(MYSQL *db)
574                           BBS_priv.uid);                           BBS_priv.uid);
575          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
576          {          {
577                  log_error("Update user_pubinfo error: %s\n", mysql_error(db));                  log_error("Update user_pubinfo error: %s", mysql_error(db));
578                  return -1;                  return -1;
579          }          }
580    
# Line 586  int user_online_update(const char *actio Line 603  int user_online_update(const char *actio
603          db = db_open();          db = db_open();
604          if (db == NULL)          if (db == NULL)
605          {          {
606                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s", mysql_error(db));
607                  return -1;                  return -1;
608          }          }
609    
# Line 596  int user_online_update(const char *actio Line 613  int user_online_update(const char *actio
613                           BBS_current_action, getpid());                           BBS_current_action, getpid());
614          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
615          {          {
616                  log_error("Update user_online error: %s\n", mysql_error(db));                  log_error("Update user_online error: %s", mysql_error(db));
617                  return -2;                  return -2;
618          }          }
619    
# Line 604  int user_online_update(const char *actio Line 621  int user_online_update(const char *actio
621    
622          return 1;          return 1;
623  }  }
624    
625    int user_update_agreement(void)
626    {
627            MYSQL *db = NULL;
628            char sql[SQL_BUFFER_LEN];
629            int ch;
630            int ret = 0;
631    
632            clearscr();
633            moveto(2, 1);
634            prints("尊敬的用户:");
635            moveto(3, 1);
636            prints("    本站的《用户许可协议》已更新,您必须在仔细阅读并接受后,才能继续使用本站提供的服务。");
637            press_any_key();
638    
639            clearscr();
640            display_file(DATA_EULA, 1);
641    
642            while (!SYS_server_exit)
643            {
644                    clearscr();
645                    moveto(2, 1);
646    
647                    ch = press_any_key_ex("您是否接受本站的《用户许可协议》? (A)接受, (D)拒绝, (R)再看看协议 [D]", 60);
648                    switch (toupper(ch))
649                    {
650                    case KEY_NULL:
651                            return -1;
652                    case KEY_TIMEOUT:
653                    case CR:
654                    case 'D':
655                            moveto(3, 1);
656                            prints("您已拒绝《用户许可协议》,再见!");
657                            press_any_key();
658                            return 0;
659                    case 'R':
660                            clearscr();
661                            display_file(DATA_EULA, 1);
662                            continue;
663                    case 'A':
664                            db = db_open();
665                            if (db == NULL)
666                            {
667                                    ret = -1;
668                                    goto cleanup;
669                            }
670    
671                            snprintf(sql, sizeof(sql),
672                                             "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
673                                             "last_login_dt = NOW() WHERE UID = %d",
674                                             BBS_priv.uid);
675                            if (mysql_query(db, sql) != 0)
676                            {
677                                    log_error("Update user_pubinfo error: %s", mysql_error(db));
678                                    ret = -1;
679                                    goto cleanup;
680                            }
681    
682                            mysql_close(db);
683                            db = NULL;
684    
685                            moveto(3, 1);
686                            prints("您已接受《用户许可协议》,请重新登陆。");
687                            press_any_key();
688                            return 1;
689                    default:
690                            continue;
691                    }
692    
693                    break;
694            }
695    
696    cleanup:
697            mysql_close(db);
698    
699            return ret;
700    }


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

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