/[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.21 by sysadm, Tue May 6 12:10:22 2025 UTC Revision 1.45 by sysadm, Tue Jun 17 02:06:48 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #define _POSIX_C_SOURCE 200112L
18    
19  #include "login.h"  #include "login.h"
 #include "register.h"  
20  #include "bbs.h"  #include "bbs.h"
21  #include "user_priv.h"  #include "user_priv.h"
 #include "reg_ex.h"  
22  #include "common.h"  #include "common.h"
23  #include "log.h"  #include "log.h"
24  #include "io.h"  #include "io.h"
25  #include "screen.h"  #include "screen.h"
26  #include "database.h"  #include "database.h"
27    #include <errno.h>
28    #include <ctype.h>
29  #include <string.h>  #include <string.h>
 #include <mysql.h>  
30  #include <regex.h>  #include <regex.h>
31    #include <stdlib.h>
32  #include <unistd.h>  #include <unistd.h>
33    #include <mysql/mysql.h>
34    
35  void login_fail()  int bbs_login(void)
 {  
         display_file(DATA_LOGIN_ERROR);  
   
         sleep(1);  
 }  
   
 int bbs_login()  
36  {  {
37          char username[BBS_username_max_len + 1];          char username[BBS_username_max_len + 1];
38          char password[BBS_password_max_len + 1];          char password[BBS_password_max_len + 1];
39          int count = 0;          int i = 0;
40          int ok = 0;          int ok = 0;
41    
42          // Input username          for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++)
         while (!ok)  
43          {          {
44                  prints("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "                  prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', "
45                             "注册请输入`\033[1;31mnew\033[m'): ");                             "注册请输入`\033[1;31mnew\033[m'): ");
46                  iflush();                  iflush();
47    
48                  str_input(username, sizeof(username), DOECHO);                  if (str_input(username, sizeof(username), DOECHO) < 0)
49                  count++;                  {
50                            continue;
51                    }
52    
53                  if (strcmp(username, "guest") == 0)                  if (strcmp(username, "guest") == 0)
54                  {                  {
55                          MYSQL *db = db_open();                          load_guest_info();
                         if (db == NULL)  
                         {  
                                 return -1;  
                         }  
   
                         load_guest_info(db);  
   
                         mysql_close(db);  
56    
57                          return 0;                          return 0;
58                  }                  }
59    
60                  if (strcmp(username, "new") == 0)                  if (strcmp(username, "new") == 0)
61                  {                  {
62                          if (user_register() == 0)                          display_file(DATA_REGISTER, 1);
63                          {  
64                                  return 0;                          return 0;
                         }  
                         else  
                         {  
                                 return -2;  
                         }  
65                  }                  }
66    
67                  if (username[0] != '\0')                  if (username[0] != '\0')
68                  {                  {
                         // Input password  
69                          prints("\033[1;37m请输入密码\033[m: ");                          prints("\033[1;37m请输入密码\033[m: ");
70                          iflush();                          iflush();
71    
72                          str_input(password, sizeof(password), NOECHO);                          if (str_input(password, sizeof(password), NOECHO) < 0)
   
                         MYSQL *db = db_open();  
                         if (db == NULL)  
73                          {                          {
74                                  return -1;                                  continue;
75                          }                          }
76    
77                          ok = (check_user(db, username, password) == 0);                          ok = (check_user(username, password) == 0);
78                            iflush();
                         mysql_close(db);  
                 }  
                 if (count >= 3 && !ok)  
                 {  
                         login_fail();  
                         return -1;  
79                  }                  }
80          }          }
81    
82            if (!ok)
83            {
84                    display_file(DATA_LOGIN_ERROR, 1);
85                    return -1;
86            }
87    
88            log_common("User \"%s\"(%ld) login from %s:%d\n",
89                               BBS_username, BBS_priv.uid, hostaddr_client, port_client);
90    
91          return 0;          return 0;
92  }  }
93    
94  int check_user(MYSQL *db, char *username, char *password)  int check_user(const char *username, const char *password)
95  {  {
96          MYSQL_RES *rs;          MYSQL *db = NULL;
97            MYSQL_RES *rs = NULL;
98          MYSQL_ROW row;          MYSQL_ROW row;
99          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
100          int ret;          int ret = 0;
101          long BBS_uid = 0;          int BBS_uid = 0;
102            char client_addr[IP_ADDR_LEN];
103            int i;
104            int ok = 1;
105            char user_tz_env[BBS_user_tz_max_len + 2];
106    
107            db = db_open();
108            if (db == NULL)
109            {
110                    ret = -1;
111                    goto cleanup;
112            }
113    
114          // Verify format          // Verify format
115          if (ireg("^[A-Za-z][A-Za-z0-9]{2,11}$", username, 0, NULL) != 0 ||          for (i = 0; ok && username[i] != '\0'; i++)
116                  ireg("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0)          {
117                    if (!(isalpha(username[i]) || (i > 0 && isdigit(username[i]))))
118                    {
119                            ok = 0;
120                    }
121            }
122            if (ok && (i < 3 || i > 12))
123            {
124                    ok = 0;
125            }
126            for (i = 0; ok && password[i] != '\0'; i++)
127            {
128                    if (!isalnum(password[i]))
129                    {
130                            ok = 0;
131                    }
132            }
133            if (ok && (i < 5 || i > 12))
134            {
135                    ok = 0;
136            }
137    
138            if (!ok)
139          {          {
140                  prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");                  prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");
141                  iflush();                  ret = 1;
142                  return 1;                  goto cleanup;
143          }          }
144    
145          // Begin transaction          // Begin transaction
146          if (mysql_query(db, "SET autocommit=0") != 0)          if (mysql_query(db, "SET autocommit=0") != 0)
147          {          {
148                  log_error("SET autocommit=0 failed\n");                  log_error("SET autocommit=0 error: %s\n", mysql_error(db));
149                  return -1;                  ret = -1;
150                    goto cleanup;
151          }          }
152    
153          if (mysql_query(db, "BEGIN") != 0)          if (mysql_query(db, "BEGIN") != 0)
154          {          {
155                  log_error("Begin transaction failed\n");                  log_error("Begin transaction error: %s\n", mysql_error(db));
156                  return -1;                  ret = -1;
157                    goto cleanup;
158          }          }
159    
160            // Failed login attempts from the same source (subnet /24) during certain time period
161            strncpy(client_addr, hostaddr_client, sizeof(client_addr) - 1);
162            client_addr[sizeof(client_addr) - 1] = '\0';
163    
164          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
165                          "SELECT UID, username, p_login FROM user_list "                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "
166                          "WHERE username = '%s' AND password = SHA2('%s', 256) AND enable",                           "WHERE login_dt >= SUBDATE(NOW(), INTERVAL %d MINUTE) "
167                          username, password);                           "AND login_ip LIKE '%s'",
168                             BBS_login_failures_count_interval,
169                             ip_mask(client_addr, 1, '%'));
170          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
171          {          {
172                  log_error("Query user_list failed\n");                  log_error("Query user_list error: %s\n", mysql_error(db));
173                  return -1;                  ret = -1;
174                    goto cleanup;
175          }          }
176          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
177          {          {
178                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
179                  return -1;                  ret = -1;
180                    goto cleanup;
181          }          }
182          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
183          {          {
184                  BBS_uid = atol(row[0]);                  if (atoi(row[0]) > BBS_allowed_login_failures_within_interval)
185                    {
186                            prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n");
187                            ret = 1;
188                            goto cleanup;
189                    }
190            }
191            mysql_free_result(rs);
192            rs = NULL;
193    
194            // Failed login attempts against the current username during certain time period
195            snprintf(sql, sizeof(sql),
196                             "SELECT COUNT(*) AS err_count FROM user_err_login_log "
197                             "WHERE username = '%s' AND login_dt >= SUBDATE(NOW(), INTERVAL 1 DAY)",
198                             username);
199            if (mysql_query(db, sql) != 0)
200            {
201                    log_error("Query user_list error: %s\n", mysql_error(db));
202                    ret = -1;
203                    goto cleanup;
204            }
205            if ((rs = mysql_store_result(db)) == NULL)
206            {
207                    log_error("Get user_list data failed\n");
208                    ret = -1;
209                    goto cleanup;
210            }
211            if ((row = mysql_fetch_row(rs)))
212            {
213                    if (atoi(row[0]) >= 5)
214                    {
215                            prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n");
216                            ret = 1;
217                            goto cleanup;
218                    }
219            }
220            mysql_free_result(rs);
221            rs = NULL;
222    
223            snprintf(sql, sizeof(sql),
224                             "SELECT UID, username, p_login FROM user_list "
225                             "WHERE username = '%s' AND password = SHA2('%s', 256) AND enable",
226                             username, password);
227            if (mysql_query(db, sql) != 0)
228            {
229                    log_error("Query user_list error: %s\n", mysql_error(db));
230                    ret = -1;
231                    goto cleanup;
232            }
233            if ((rs = mysql_store_result(db)) == NULL)
234            {
235                    log_error("Get user_list data failed\n");
236                    ret = -1;
237                    goto cleanup;
238            }
239            if ((row = mysql_fetch_row(rs)))
240            {
241                    BBS_uid = atoi(row[0]);
242                  strncpy(BBS_username, row[1], sizeof(BBS_username) - 1);                  strncpy(BBS_username, row[1], sizeof(BBS_username) - 1);
243                  BBS_username[sizeof(BBS_username) - 1] = '\0';                  BBS_username[sizeof(BBS_username) - 1] = '\0';
244                  int p_login = atoi(row[2]);                  int p_login = atoi(row[2]);
245    
246                  mysql_free_result(rs);                  mysql_free_result(rs);
247                    rs = NULL;
248    
249                  // Add user login log                  // Add user login log
250                  snprintf(sql, sizeof(sql),                  snprintf(sql, sizeof(sql),
251                                  "INSERT INTO user_login_log(UID, login_dt, login_ip) "                                   "INSERT INTO user_login_log(UID, login_dt, login_ip) "
252                                  "VALUES(%ld, NOW(), '%s')",                                   "VALUES(%d, NOW(), '%s')",
253                                  BBS_uid, hostaddr_client);                                   BBS_uid, hostaddr_client);
254                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
255                  {                  {
256                          log_error("Insert into user_login_log failed\n");                          log_error("Insert into user_login_log error: %s\n", mysql_error(db));
257                          return -1;                          ret = -1;
258                            goto cleanup;
259                  }                  }
260    
261                  // Commit transaction                  // Commit transaction
262                  if (mysql_query(db, "COMMIT") != 0)                  if (mysql_query(db, "COMMIT") != 0)
263                  {                  {
264                          log_error("Commit transaction failed\n");                          log_error("Commit transaction error: %s\n", mysql_error(db));
265                          return -1;                          ret = -1;
266                            goto cleanup;
267                  }                  }
268    
269                  if (p_login == 0)                  if (p_login == 0)
270                  {                  {
                         mysql_free_result(rs);  
   
271                          prints("\033[1;31m您目前无权登陆...\033[m\r\n");                          prints("\033[1;31m您目前无权登陆...\033[m\r\n");
272                          iflush();                          ret = 1;
273                          return 1;                          goto cleanup;
274                  }                  }
275          }          }
276          else          else
277          {          {
278                  mysql_free_result(rs);                  mysql_free_result(rs);
279                    rs = NULL;
280    
281                  snprintf(sql, sizeof(sql),                  snprintf(sql, sizeof(sql),
282                                  "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "                                   "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "
283                                  "VALUES('%s', '%s', NOW(), '%s')",                                   "VALUES('%s', '%s', NOW(), '%s')",
284                                  username, password, hostaddr_client);                                   username, password, hostaddr_client);
285                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
286                  {                  {
287                          log_error("Insert into user_err_login_log failed\n");                          log_error("Insert into user_err_login_log error: %s\n", mysql_error(db));
288                          return -1;                          ret = -1;
289                            goto cleanup;
290                  }                  }
291    
292                  // Commit transaction                  // Commit transaction
293                  if (mysql_query(db, "COMMIT") != 0)                  if (mysql_query(db, "COMMIT") != 0)
294                  {                  {
295                          log_error("Commit transaction failed\n");                          log_error("Commit transaction error: %s\n", mysql_error(db));
296                          return -1;                          ret = -1;
297                            goto cleanup;
298                  }                  }
299    
300                  prints("\033[1;31m错误的用户名或密码...\033[m\r\n");                  prints("\033[1;31m错误的用户名或密码...\033[m\r\n");
301                  iflush();                  ret = 1;
302                  return 1;                  goto cleanup;
303          }          }
304    
305          // Set AUTOCOMMIT = 1          // Set AUTOCOMMIT = 1
306          if (mysql_query(db, "SET autocommit=1") != 0)          if (mysql_query(db, "SET autocommit=1") != 0)
307          {          {
308                  log_error("SET autocommit=1 failed\n");                  log_error("SET autocommit=1 error: %s\n", mysql_error(db));
309                  return -1;                  ret = -1;
310                    goto cleanup;
311          }          }
312    
313          ret = load_user_info(db, BBS_uid);          ret = load_user_info(db, BBS_uid);
# Line 229  int check_user(MYSQL *db, char *username Line 318  int check_user(MYSQL *db, char *username
318                  break;                  break;
319          case -1: // Load data error          case -1: // Load data error
320                  prints("\033[1;31m读取用户数据错误...\033[m\r\n");                  prints("\033[1;31m读取用户数据错误...\033[m\r\n");
321                  iflush();                  ret = -1;
322                  return -1;                  goto cleanup;
323          case -2: // Unused          case -2: // Unused
324                  prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");                  prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");
325                  iflush();                  ret = 1;
326                  return 1;                  goto cleanup;
327          case -3: // Dead          case -3: // Dead
328                  prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");                  prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
329                  iflush();                  ret = 1;
330                  return 1;                  goto cleanup;
331          default:          default:
332                  return -2;                  ret = -2;
333                    goto cleanup;
334          }          }
335    
336          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
337                          "UPDATE user_pubinfo SET visit_count = visit_count + 1, "                           "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
338                          "last_login_dt = NOW() WHERE UID = %ld",                           "last_login_dt = NOW() WHERE UID = %d",
339                          BBS_uid);                           BBS_uid);
340          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
341          {          {
342                  log_error("Update user_pubinfo failed\n");                  log_error("Update user_pubinfo error: %s\n", mysql_error(db));
343                  return -1;                  ret = -1;
344                    goto cleanup;
345          }          }
346    
347          if (user_online_add(db) != 0)          if (user_online_add(db) != 0)
348          {          {
349                  return -1;                  ret = -1;
350                    goto cleanup;
351          }          }
352    
353          BBS_last_access_tm = BBS_login_tm = time(0);          BBS_last_access_tm = BBS_login_tm = time(NULL);
354    
355          return 0;          // Set user tz to process env
356            if (BBS_user_tz[0] != '\0')
357            {
358                    user_tz_env[0] = ':';
359                    strncpy(user_tz_env + 1, BBS_user_tz, sizeof(user_tz_env) - 2);
360                    user_tz_env[sizeof(user_tz_env) - 1] = '\0';
361    
362                    if (setenv("TZ", user_tz_env, 1) == -1)
363                    {
364                            log_error("setenv(TZ = %s) error %d\n", user_tz_env, errno);
365                            return -3;
366                    }
367    
368                    tzset();
369            }
370    
371    cleanup:
372            mysql_free_result(rs);
373            mysql_close(db);
374    
375            return ret;
376  }  }
377    
378  int load_user_info(MYSQL *db, long int BBS_uid)  int load_user_info(MYSQL *db, int BBS_uid)
379  {  {
380          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
381          MYSQL_ROW row;          MYSQL_ROW row;
382          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
383          int life;          int life;
384          time_t last_login_dt;          time_t last_login_dt;
385            int ret = 0;
386    
387          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
388                          "SELECT life, UNIX_TIMESTAMP(last_login_dt) "                           "SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone, exp, nickname "
389                          "FROM user_pubinfo WHERE UID = %ld",                           "FROM user_pubinfo WHERE UID = %d",
390                          BBS_uid);                           BBS_uid);
391          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
392          {          {
393                  log_error("Query user_pubinfo failed\n");                  log_error("Query user_pubinfo error: %s\n", mysql_error(db));
394                  return -1;                  ret = -1;
395                    goto cleanup;
396          }          }
397          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
398          {          {
399                  log_error("Get user_pubinfo data failed\n");                  log_error("Get user_pubinfo data failed\n");
400                  return -1;                  ret = -1;
401                    goto cleanup;
402          }          }
403          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
404          {          {
405                  life = atoi(row[0]);                  life = atoi(row[0]);
406                  last_login_dt = (time_t)atol(row[1]);                  last_login_dt = (time_t)atol(row[1]);
407    
408                    strncpy(BBS_user_tz, row[2], sizeof(BBS_user_tz) - 1);
409                    BBS_user_tz[sizeof(BBS_user_tz) - 1] = '\0';
410    
411                    BBS_user_exp = atoi(row[3]);
412    
413                    strncpy(BBS_nickname, row[4], sizeof(BBS_nickname));
414                    BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
415          }          }
416          else          else
417          {          {
418                  mysql_free_result(rs);                  ret = -1; // Data not found
419                  return -1; // Data not found                  goto cleanup;
420          }          }
421          mysql_free_result(rs);          mysql_free_result(rs);
422            rs = NULL;
423    
424          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
425                  time(0) - last_login_dt > 60 * 60 * 24 * life)                  time(NULL) - last_login_dt > 60 * 60 * 24 * life)
426          {          {
427                  return -3; // Dead                  ret = -3; // Dead
428                    goto cleanup;
429          }          }
430    
431          if (load_priv(db, &BBS_priv, BBS_uid) != 0)          if (load_priv(db, &BBS_priv, BBS_uid) != 0)
432          {          {
433                  return -1;                  ret = -1;
434                    goto cleanup;
435          }          }
436    
437          return 0;  cleanup:
438            mysql_free_result(rs);
439    
440            return ret;
441  }  }
442    
443  int load_guest_info(MYSQL *db)  int load_guest_info(void)
444  {  {
445            MYSQL *db = NULL;
446            int ret = 0;
447    
448            db = db_open();
449            if (db == NULL)
450            {
451                    ret = -1;
452                    goto cleanup;
453            }
454    
455          strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);          strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);
456          BBS_username[sizeof(BBS_username) - 1] = '\0';          BBS_username[sizeof(BBS_username) - 1] = '\0';
457    
458            BBS_user_exp = 0;
459    
460            strncpy(BBS_nickname, "Guest", sizeof(BBS_nickname));
461            BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
462    
463          if (load_priv(db, &BBS_priv, 0) != 0)          if (load_priv(db, &BBS_priv, 0) != 0)
464          {          {
465                  return -1;                  ret = -1;
466                    goto cleanup;
467          }          }
468    
469          if (user_online_add(db) != 0)          if (user_online_add(db) != 0)
470          {          {
471                  return -1;                  ret = -1;
472                    goto cleanup;
473          }          }
474    
475          BBS_last_access_tm = BBS_login_tm = time(0);          BBS_last_access_tm = BBS_login_tm = time(NULL);
476    
477          return 0;  cleanup:
478            mysql_close(db);
479    
480            return ret;
481  }  }
482    
483  int user_online_add(MYSQL *db)  int user_online_add(MYSQL *db)
# Line 341  int user_online_add(MYSQL *db) Line 490  int user_online_add(MYSQL *db)
490          }          }
491    
492          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
493                          "INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) "                           "INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) "
494                          "VALUES('Telnet_Process_%d', %ld, '%s', NOW(), NOW())",                           "VALUES('Telnet_Process_%d', %d, '%s', NOW(), NOW())",
495                          getpid(), BBS_priv.uid, hostaddr_client);                           getpid(), BBS_priv.uid, hostaddr_client);
496          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
497          {          {
498                  log_error("Add user_online failed\n");                  log_error("Add user_online error: %s\n", mysql_error(db));
499                  return -1;                  return -1;
500          }          }
501    
# Line 358  int user_online_del(MYSQL *db) Line 507  int user_online_del(MYSQL *db)
507          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
508    
509          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
510                          "DELETE FROM user_online WHERE SID = 'Telnet_Process_%d'",                           "DELETE FROM user_online WHERE SID = 'Telnet_Process_%d'",
511                          getpid());                           getpid());
512          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
513          {          {
514                  log_error("Delete user_online failed\n");                  log_error("Delete user_online error: %s\n", mysql_error(db));
515                  return -1;                  return -1;
516          }          }
517    


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

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