/[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.50 by sysadm, Wed Jul 23 01:18:00 2025 UTC Revision 1.76 by sysadm, Sun Dec 21 12:45:47 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    login.c  -  description  /*
3                                                           -------------------   * login
4          Copyright            : (C) 2004-2025 by Leaflet   *   - user authentication and online status manager
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "bbs.h"  #include "bbs.h"
14  #include "common.h"  #include "common.h"
# Line 29  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>
30    
31    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
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  {  {
# Line 37  int bbs_login(void) Line 43  int bbs_login(void)
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 60  int bbs_login(void) Line 67  int bbs_login(void)
67                  {                  {
68                          display_file(DATA_REGISTER, 1);                          display_file(DATA_REGISTER, 1);
69    
70                          return 0;                          return -1;
71                  }                  }
72    
73                  if (username[0] != '\0')                  if (username[0] != '\0')
# Line 73  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 84  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 113  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]))))                  if (!(isalpha((int)username[i]) || (i > 0 && (isdigit((int)username[i]) || username[i] == '_'))))
128                  {                  {
129                          ok = 0;                          ok = 0;
130                  }                  }
131          }          }
132          if (ok && (i < 3 || i > 12))          if (ok && (i < BBS_username_min_len || i > BBS_username_max_len))
133          {          {
134                  ok = 0;                  ok = 0;
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                  }                  }
142          }          }
143          if (ok && (i < 5 || i > 12))          if (ok && (i < BBS_password_min_len || i > BBS_password_max_len))
144          {          {
145                  ok = 0;                  ok = 0;
146          }          }
# Line 144  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 168  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          }          }
192          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
193          {          {
194                  if (atoi(row[0]) > BBS_allowed_login_failures_within_interval)                  if (atoi(row[0]) >= BBS_allowed_login_failures_within_interval)
195                  {                  {
196                          prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n");                          prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试,或使用Web方式访问\033[m\r\n");
197                          ret = 1;                          ret = 1;
198                          goto cleanup;                          goto cleanup;
199                  }                  }
# Line 190  int check_user(const char *username, con Line 201  int check_user(const char *username, con
201          mysql_free_result(rs);          mysql_free_result(rs);
202          rs = NULL;          rs = NULL;
203    
204          // Failed login attempts against the current username during certain time period          // Failed login attempts against the current username since last successful login
205          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
206                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "
207                           "WHERE username = '%s' AND login_dt >= SUBDATE(NOW(), INTERVAL 1 DAY)",                           "LEFT JOIN user_list ON user_err_login_log.username = user_list.username "
208                             "LEFT JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID "
209                             "WHERE user_err_login_log.username = '%s' "
210                             "AND (user_err_login_log.login_dt >= user_pubinfo.last_login_dt "
211                             "OR user_pubinfo.last_login_dt IS NULL)",
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          }          }
225          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
226          {          {
227                  if (atoi(row[0]) >= 5)                  if (atoi(row[0]) >= BBS_allowed_login_failures_per_account)
228                  {                  {
229                          prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n");                          prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录解锁\033[m\r\n");
230                          ret = 1;                          ret = 1;
231                          goto cleanup;                          goto cleanup;
232                  }                  }
# 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          }          }
# Line 314  int check_user(const char *username, con Line 329  int check_user(const char *username, con
329          switch (ret)          switch (ret)
330          {          {
331          case 0: // Login successfully          case 0: // Login successfully
332                    if (!SSH_v2 && checklevel2(&BBS_priv, P_MAN_S))
333                    {
334                            prints("\033[1;31m非普通账户必须使用SSH方式登录\033[m\r\n");
335                            ret = 1;
336                            goto cleanup;
337                    }
338                  break;                  break;
339          case -1: // Load data error          case -1: // Load data error
340                  prints("\033[1;31m读取用户数据错误...\033[m\r\n");                  prints("\033[1;31m读取用户数据错误...\033[m\r\n");
341                  ret = -1;                  ret = -1;
342                  goto cleanup;                  goto cleanup;
343          case -2: // Unused          case -2: // Enforce update user agreement
344                  prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");                  if (!SSH_v2 && checklevel2(&BBS_priv, P_MAN_S))
345                  ret = 1;                  {
346                            prints("\033[1;31m非普通账户必须使用SSH方式登录\033[m\r\n");
347                            ret = 1;
348                            goto cleanup;
349                    }
350                    ret = 2;
351                  goto cleanup;                  goto cleanup;
352          case -3: // Dead          case -3: // Dead
353                  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 364  int check_user(const char *username, con
364                           BBS_uid);                           BBS_uid);
365          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
366          {          {
367                  log_error("Update user_pubinfo error: %s\n", mysql_error(db));                  log_error("Update user_pubinfo error: %s", mysql_error(db));
368                  ret = -1;                  ret = -1;
369                  goto cleanup;                  goto cleanup;
370          }          }
# Line 360  int check_user(const char *username, con Line 386  int check_user(const char *username, con
386    
387                  if (setenv("TZ", user_tz_env, 1) == -1)                  if (setenv("TZ", user_tz_env, 1) == -1)
388                  {                  {
389                          log_error("setenv(TZ = %s) error %d\n", user_tz_env, errno);                          log_error("setenv(TZ = %s) error %d", user_tz_env, errno);
390                          return -3;                          return -3;
391                  }                  }
392    
# Line 389  int load_user_info(MYSQL *db, int BBS_ui Line 415  int load_user_info(MYSQL *db, int BBS_ui
415                           BBS_uid);                           BBS_uid);
416          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
417          {          {
418                  log_error("Query user_pubinfo error: %s\n", mysql_error(db));                  log_error("Query user_pubinfo error: %s", mysql_error(db));
419                  ret = -1;                  ret = -1;
420                  goto cleanup;                  goto cleanup;
421          }          }
422          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
423          {          {
424                  log_error("Get user_pubinfo data failed\n");                  log_error("Get user_pubinfo data failed");
425                  ret = -1;                  ret = -1;
426                  goto cleanup;                  goto cleanup;
427          }          }
# Line 429  int load_user_info(MYSQL *db, int BBS_ui Line 455  int load_user_info(MYSQL *db, int BBS_ui
455    
456          if (load_priv(db, &BBS_priv, BBS_uid) != 0)          if (load_priv(db, &BBS_priv, BBS_uid) != 0)
457          {          {
458                  ret = -1;                  ret = -1; // Data not found
459                    goto cleanup;
460            }
461    
462            if (last_login_dt < BBS_eula_tm)
463            {
464                    ret = -2; // require update agreement first
465                  goto cleanup;                  goto cleanup;
466          }          }
467    
# Line 483  int user_online_add(MYSQL *db) Line 515  int user_online_add(MYSQL *db)
515  {  {
516          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
517    
518          if (user_online_del(db) != 0)          snprintf(sql, sizeof(sql),
519                             "INSERT INTO visit_log(dt, IP) VALUES(NOW(), '%s')",
520                             hostaddr_client);
521            if (mysql_query(db, sql) != 0)
522          {          {
523                    log_error("Add visit log error: %s", mysql_error(db));
524                  return -1;                  return -1;
525          }          }
526    
527            if (user_online_del(db) != 0)
528            {
529                    return -2;
530            }
531    
532          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
533                           "INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) "                           "INSERT INTO user_online(SID, UID, ip, current_action, login_tm, last_tm) "
534                           "VALUES('Telnet_Process_%d', %d, '%s', NOW(), NOW())",                           "VALUES('Telnet_Process_%d', %d, '%s', 'LOGIN', NOW(), NOW())",
535                           getpid(), BBS_priv.uid, hostaddr_client);                           getpid(), BBS_priv.uid, hostaddr_client);
536          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
537          {          {
538                  log_error("Add user_online error: %s\n", mysql_error(db));                  log_error("Add user_online error: %s", mysql_error(db));
539                  return -1;                  return -3;
540          }          }
541    
542          return 0;          return 0;
# Line 510  int user_online_del(MYSQL *db) Line 551  int user_online_del(MYSQL *db)
551                           getpid());                           getpid());
552          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
553          {          {
554                  log_error("Delete user_online error: %s\n", mysql_error(db));                  log_error("Delete user_online error: %s", mysql_error(db));
555                    return -1;
556            }
557    
558            return 0;
559    }
560    
561    int user_online_exp(MYSQL *db)
562    {
563            char sql[SQL_BUFFER_LEN];
564    
565            // +1 exp for every 5 minutes online since last logout
566            // but at most 24 hours worth of exp can be gained in Telnet session
567            snprintf(sql, sizeof(sql),
568                             "UPDATE user_pubinfo SET exp = exp + FLOOR(LEAST(TIMESTAMPDIFF("
569                             "SECOND, GREATEST(last_login_dt, IF(last_logout_dt IS NULL, last_login_dt, last_logout_dt)), NOW()"
570                             ") / 60 / 5, 12 * 24)), last_logout_dt = NOW() "
571                             "WHERE UID = %d",
572                             BBS_priv.uid);
573            if (mysql_query(db, sql) != 0)
574            {
575                    log_error("Update user_pubinfo error: %s", mysql_error(db));
576                  return -1;                  return -1;
577          }          }
578    
# Line 522  int user_online_update(const char *actio Line 584  int user_online_update(const char *actio
584          MYSQL *db = NULL;          MYSQL *db = NULL;
585          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
586    
587          if (strcmp(BBS_current_action, action) == 0) // No change          if ((action == NULL || strcmp(BBS_current_action, action) == 0) &&
588                    time(NULL) - BBS_current_action_tm < BBS_current_action_refresh_interval) // No change
589          {          {
590                  return 0;                  return 0;
591          }          }
592    
593          strncpy(BBS_current_action, action, sizeof(BBS_current_action) - 1);          if (action != NULL)
594          BBS_current_action[sizeof(BBS_current_action) - 1] = '\0';          {
595                    strncpy(BBS_current_action, action, sizeof(BBS_current_action) - 1);
596                    BBS_current_action[sizeof(BBS_current_action) - 1] = '\0';
597            }
598    
599            BBS_current_action_tm = time(NULL);
600    
601          db = db_open();          db = db_open();
602          if (db == NULL)          if (db == NULL)
603          {          {
604                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s", mysql_error(db));
605                  return -1;                  return -1;
606          }          }
607    
608          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
609                           "UPDATE user_online SET current_action = '%s', last_tm=NOW() "                           "UPDATE user_online SET current_action = '%s', last_tm = NOW() "
610                           "WHERE SID = 'Telnet_Process_%d'",                           "WHERE SID = 'Telnet_Process_%d'",
611                           BBS_current_action, getpid());                           BBS_current_action, getpid());
612          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
613          {          {
614                  log_error("Update user_online error: %s\n", mysql_error(db));                  log_error("Update user_online error: %s", mysql_error(db));
615                  return -2;                  return -2;
616          }          }
617    
# Line 551  int user_online_update(const char *actio Line 619  int user_online_update(const char *actio
619    
620          return 1;          return 1;
621  }  }
622    
623    int user_update_agreement(void)
624    {
625            MYSQL *db = NULL;
626            char sql[SQL_BUFFER_LEN];
627            int ch;
628            int ret = 0;
629    
630            clearscr();
631            moveto(2, 1);
632            prints("尊敬的用户:");
633            moveto(3, 1);
634            prints("    本站的《用户许可协议》已更新,您必须在仔细阅读并接受后,才能继续使用本站提供的服务。");
635            press_any_key();
636    
637            clearscr();
638            display_file(DATA_EULA, 1);
639    
640            while (!SYS_server_exit)
641            {
642                    clearscr();
643                    moveto(2, 1);
644    
645                    ch = press_any_key_ex("您是否接受本站的《用户许可协议》? (A)接受, (D)拒绝, (R)再看看协议 [D]", 60);
646                    switch (toupper(ch))
647                    {
648                    case KEY_NULL:
649                            return -1;
650                    case KEY_TIMEOUT:
651                    case CR:
652                    case 'D':
653                            moveto(3, 1);
654                            prints("您已拒绝《用户许可协议》,再见!");
655                            press_any_key();
656                            return 0;
657                    case 'R':
658                            clearscr();
659                            display_file(DATA_EULA, 1);
660                            continue;
661                    case 'A':
662                            db = db_open();
663                            if (db == NULL)
664                            {
665                                    ret = -1;
666                                    goto cleanup;
667                            }
668    
669                            snprintf(sql, sizeof(sql),
670                                             "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
671                                             "last_login_dt = NOW() WHERE UID = %d",
672                                             BBS_priv.uid);
673                            if (mysql_query(db, sql) != 0)
674                            {
675                                    log_error("Update user_pubinfo error: %s", mysql_error(db));
676                                    ret = -1;
677                                    goto cleanup;
678                            }
679    
680                            mysql_close(db);
681                            db = NULL;
682    
683                            moveto(3, 1);
684                            prints("您已接受《用户许可协议》,请重新登陆。");
685                            press_any_key();
686                            return 1;
687                    default:
688                            continue;
689                    }
690    
691                    break;
692            }
693    
694    cleanup:
695            mysql_close(db);
696    
697            return ret;
698    }


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

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