/[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.14 by sysadm, Wed Apr 30 09:18:19 2025 UTC Revision 1.26 by sysadm, Sun May 11 04:09:08 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                                                    login.c  -  description                                                    login.c  -  description
3                                                           -------------------                                                           -------------------
4          begin                : Mon Oct 20 2004          Copyright            : (C) 2004-2025 by Leaflet
5          copyright            : (C) 2004 by Leaflet          Email                : leaflet@leafok.com
         email                : leaflet@leafok.com  
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
11   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
12   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 3 of the License, or     *
13   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
# Line 32  Line 31 
31    
32  void login_fail()  void login_fail()
33  {  {
34          char temp[256];          display_file(DATA_LOGIN_ERROR);
   
         strcpy(temp, app_home_dir);  
         strcat(temp, "data/login_error.txt");  
         display_file(temp);  
   
         sleep(1);  
35  }  }
36    
37  int bbs_login()  int bbs_login()
38  {  {
39          char username[20], password[20];          char username[BBS_username_max_len + 1];
40          int count, ok;          char password[BBS_password_max_len + 1];
41            int count = 0;
42            int ok = 0;
43    
44          // Input username          for (; !SYS_server_exit && !ok && count < 3; count++)
         count = 0;  
         ok = 0;  
         while (!ok)  
45          {          {
46                  prints("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "                  prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', "
47                             "注册请输入`\033[1;31mnew\033[m'): ");                             "注册请输入`\033[1;31mnew\033[m'): ");
48                  iflush();                  iflush();
49    
50                  str_input(username, 19, DOECHO);                  if (str_input(username, sizeof(username), DOECHO) < 0)
51                  count++;                  {
52                            continue;
53                    }
54    
55                  if (strcmp(username, "guest") == 0)                  if (strcmp(username, "guest") == 0)
56                  {                  {
57                          MYSQL * db = db_open();                          MYSQL *db = db_open();
58                          if (db == NULL)                          if (db == NULL)
59                          {                          {
60                                  return -1;                                  return -1;
61                          }                          }
62                    
63                          load_guest_info(db);                          load_guest_info(db);
64    
65                          mysql_close(db);                          mysql_close(db);
# Line 76  int bbs_login() Line 70  int bbs_login()
70                  if (strcmp(username, "new") == 0)                  if (strcmp(username, "new") == 0)
71                  {                  {
72                          if (user_register() == 0)                          if (user_register() == 0)
73                            {
74                                  return 0;                                  return 0;
75                            }
76                          else                          else
77                            {
78                                  return -2;                                  return -2;
79                            }
80                  }                  }
81    
82                  if (strlen(username) > 0)                  if (username[0] != '\0')
83                  {                  {
                         // Input password  
84                          prints("\033[1;37m请输入密码\033[m: ");                          prints("\033[1;37m请输入密码\033[m: ");
85                          iflush();                          iflush();
86    
87                          str_input(password, 19, NOECHO);                          if (str_input(password, sizeof(password), NOECHO) < 0)
88                            {
89                                    continue;
90                            }
91    
92                          MYSQL * db = db_open();                          MYSQL *db = db_open();
93                          if (db == NULL)                          if (db == NULL)
94                          {                          {
95                                  return -1;                                  return -1;
96                          }                          }
97                    
98                          ok = (check_user(db, username, password) == 0);                          ok = (check_user(db, username, password) == 0);
99    
100                          mysql_close(db);                          mysql_close(db);
101                  }                  }
102                  if (count >= 3 && !ok)          }
103                  {  
104                          login_fail();          if (!ok)
105                          return -1;          {
106                  }                  login_fail();
107                    return -1;
108          }          }
109    
110          return 0;          return 0;
# Line 113  int check_user(MYSQL *db, char *username Line 114  int check_user(MYSQL *db, char *username
114  {  {
115          MYSQL_RES *rs;          MYSQL_RES *rs;
116          MYSQL_ROW row;          MYSQL_ROW row;
117          char sql[1024];          char sql[SQL_BUFFER_LEN];
         long int BBS_uid;  
118          int ret;          int ret;
119            long BBS_uid = 0;
120            char client_addr[IP_ADDR_LEN];
121    
122          // Verify format          // Verify format
123          if (ireg("^[A-Za-z][A-Za-z0-9]{2,11}$", username, 0, NULL) != 0 ||          if (ireg("^[A-Za-z][A-Za-z0-9]{2,11}$", username, 0, NULL) != 0 ||
# Line 139  int check_user(MYSQL *db, char *username Line 141  int check_user(MYSQL *db, char *username
141                  return -1;                  return -1;
142          }          }
143    
144          sprintf(sql,          // Failed login attempts from the same source (subnet /24) during certain time period
145                          "SELECT UID, username, p_login FROM user_list "          strncpy(client_addr, hostaddr_client, sizeof(client_addr) - 1);
146                          "WHERE username = '%s' AND password = SHA2('%s', 256) AND enable",          client_addr[sizeof(client_addr) - 1] = '\0';
147                          username, password);  
148            snprintf(sql, sizeof(sql),
149                             "SELECT COUNT(*) AS err_count FROM user_err_login_log "
150                             "WHERE login_dt >= SUBDATE(NOW(), INTERVAL 10 MINUTE) "
151                             "AND login_ip LIKE '%s'",
152                             ip_mask(client_addr, 1, '%'));
153            if (mysql_query(db, sql) != 0)
154            {
155                    log_error("Query user_list failed\n");
156                    return -1;
157            }
158            if ((rs = mysql_store_result(db)) == NULL)
159            {
160                    log_error("Get user_list data failed\n");
161                    return -1;
162            }
163            if ((row = mysql_fetch_row(rs)))
164            {
165                    if (atoi(row[0]) >= 2)
166                    {
167                            mysql_free_result(rs);
168    
169                            prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n");
170                            iflush();
171    
172                            return 1;
173                    }
174            }
175            mysql_free_result(rs);
176    
177            // Failed login attempts against the current username during certain time period
178            snprintf(sql, sizeof(sql),
179                             "SELECT COUNT(*) AS err_count FROM user_err_login_log "
180                             "WHERE username = '%s' AND login_dt >= SUBDATE(NOW(), INTERVAL 1 DAY)",
181                             username);
182            if (mysql_query(db, sql) != 0)
183            {
184                    log_error("Query user_list failed\n");
185                    return -1;
186            }
187            if ((rs = mysql_store_result(db)) == NULL)
188            {
189                    log_error("Get user_list data failed\n");
190                    return -1;
191            }
192            if ((row = mysql_fetch_row(rs)))
193            {
194                    if (atoi(row[0]) >= 5)
195                    {
196                            mysql_free_result(rs);
197    
198                            prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n");
199                            iflush();
200    
201                            return 1;
202                    }
203            }
204            mysql_free_result(rs);
205    
206            snprintf(sql, sizeof(sql),
207                             "SELECT UID, username, p_login FROM user_list "
208                             "WHERE username = '%s' AND password = SHA2('%s', 256) AND enable",
209                             username, password);
210          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
211          {          {
212                  log_error("Query user_list failed\n");                  log_error("Query user_list failed\n");
# Line 153  int check_user(MYSQL *db, char *username Line 217  int check_user(MYSQL *db, char *username
217                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
218                  return -1;                  return -1;
219          }          }
220          if (row = mysql_fetch_row(rs))          if ((row = mysql_fetch_row(rs)))
221          {          {
222                  BBS_uid = atol(row[0]);                  BBS_uid = atol(row[0]);
223                  strcpy(BBS_username, row[1]);                  strncpy(BBS_username, row[1], sizeof(BBS_username) - 1);
224                    BBS_username[sizeof(BBS_username) - 1] = '\0';
225                  int p_login = atoi(row[2]);                  int p_login = atoi(row[2]);
226    
227                  mysql_free_result(rs);                  mysql_free_result(rs);
228    
229                  // Add user login log                  // Add user login log
230                  sprintf(sql,                  snprintf(sql, sizeof(sql),
231                                  "INSERT INTO user_login_log(UID, login_dt, login_ip) "                                   "INSERT INTO user_login_log(UID, login_dt, login_ip) "
232                                  "VALUES(%ld, NOW(), '%s')",                                   "VALUES(%ld, NOW(), '%s')",
233                                  BBS_uid, hostaddr_client);                                   BBS_uid, hostaddr_client);
234                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
235                  {                  {
236                          log_error("Insert into user_login_log failed\n");                          log_error("Insert into user_login_log failed\n");
# Line 192  int check_user(MYSQL *db, char *username Line 257  int check_user(MYSQL *db, char *username
257          {          {
258                  mysql_free_result(rs);                  mysql_free_result(rs);
259    
260                  sprintf(sql,                  snprintf(sql, sizeof(sql),
261                                  "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "                                   "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "
262                                  "VALUES('%s', '%s', NOW(), '%s')",                                   "VALUES('%s', '%s', NOW(), '%s')",
263                                  username, password, hostaddr_client);                                   username, password, hostaddr_client);
264                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
265                  {                  {
266                          log_error("Insert into user_err_login_log failed\n");                          log_error("Insert into user_err_login_log failed\n");
# Line 243  int check_user(MYSQL *db, char *username Line 308  int check_user(MYSQL *db, char *username
308                  return -2;                  return -2;
309          }          }
310    
311          sprintf(sql,          snprintf(sql, sizeof(sql),
312                          "UPDATE user_pubinfo SET visit_count = visit_count + 1, "                           "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
313                          "last_login_dt = NOW() WHERE UID = %ld",                           "last_login_dt = NOW() WHERE UID = %ld",
314                          BBS_uid);                           BBS_uid);
315          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
316          {          {
317                  log_error("Update user_pubinfo failed\n");                  log_error("Update user_pubinfo failed\n");
318                  return -1;                  return -1;
319          }          }
320    
321            if (user_online_add(db) != 0)
322            {
323                    return -1;
324            }
325    
326          BBS_last_access_tm = BBS_login_tm = time(0);          BBS_last_access_tm = BBS_login_tm = time(0);
327    
328          return 0;          return 0;
# Line 262  int load_user_info(MYSQL *db, long int B Line 332  int load_user_info(MYSQL *db, long int B
332  {  {
333          MYSQL_RES *rs;          MYSQL_RES *rs;
334          MYSQL_ROW row;          MYSQL_ROW row;
335          char sql[1024];          char sql[SQL_BUFFER_LEN];
336          int life;          int life;
337          time_t last_login_dt;          time_t last_login_dt;
338    
339          sprintf(sql,          snprintf(sql, sizeof(sql),
340                          "SELECT life, UNIX_TIMESTAMP(last_login_dt) "                           "SELECT life, UNIX_TIMESTAMP(last_login_dt) "
341                          "FROM user_pubinfo WHERE UID = %ld",                           "FROM user_pubinfo WHERE UID = %ld",
342                          BBS_uid);                           BBS_uid);
343          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
344          {          {
345                  log_error("Query user_pubinfo failed\n");                  log_error("Query user_pubinfo failed\n");
# Line 280  int load_user_info(MYSQL *db, long int B Line 350  int load_user_info(MYSQL *db, long int B
350                  log_error("Get user_pubinfo data failed\n");                  log_error("Get user_pubinfo data failed\n");
351                  return -1;                  return -1;
352          }          }
353          if (row = mysql_fetch_row(rs))          if ((row = mysql_fetch_row(rs)))
354          {          {
355                  life = atoi(row[0]);                  life = atoi(row[0]);
356                  last_login_dt = (time_t)atol(row[1]);                  last_login_dt = (time_t)atol(row[1]);
# Line 288  int load_user_info(MYSQL *db, long int B Line 358  int load_user_info(MYSQL *db, long int B
358          else          else
359          {          {
360                  mysql_free_result(rs);                  mysql_free_result(rs);
361                  return (-1); // Data not found                  return -1; // Data not found
362          }          }
363          mysql_free_result(rs);          mysql_free_result(rs);
364    
365          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
366                  time(0) - last_login_dt > 60 * 60 * 24 * life)                  time(0) - last_login_dt > 60 * 60 * 24 * life)
367          {          {
368                  return (-3); // Dead                  return -3; // Dead
369          }          }
370    
371          load_priv(db, &BBS_priv, BBS_uid);          if (load_priv(db, &BBS_priv, BBS_uid) != 0)
372            {
373                    return -1;
374            }
375    
376          return 0;          return 0;
377  }  }
378    
379  int load_guest_info(MYSQL *db)  int load_guest_info(MYSQL *db)
380  {  {
381          MYSQL_RES *rs;          strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);
382          MYSQL_ROW row;          BBS_username[sizeof(BBS_username) - 1] = '\0';
383    
384          strcpy(BBS_username, "guest");          if (load_priv(db, &BBS_priv, 0) != 0)
385            {
386                    return -1;
387            }
388    
389          load_priv(db, &BBS_priv, 0);          if (user_online_add(db) != 0)
390            {
391                    return -1;
392            }
393    
394          BBS_last_access_tm = BBS_login_tm = time(0);          BBS_last_access_tm = BBS_login_tm = time(0);
395    
396          return 0;          return 0;
397  }  }
398    
399    int user_online_add(MYSQL *db)
400    {
401            char sql[SQL_BUFFER_LEN];
402    
403            if (user_online_del(db) != 0)
404            {
405                    return -1;
406            }
407    
408            snprintf(sql, sizeof(sql),
409                             "INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) "
410                             "VALUES('Telnet_Process_%d', %ld, '%s', NOW(), NOW())",
411                             getpid(), BBS_priv.uid, hostaddr_client);
412            if (mysql_query(db, sql) != 0)
413            {
414                    log_error("Add user_online failed\n");
415                    return -1;
416            }
417    
418            return 0;
419    }
420    
421    int user_online_del(MYSQL *db)
422    {
423            char sql[SQL_BUFFER_LEN];
424    
425            snprintf(sql, sizeof(sql),
426                             "DELETE FROM user_online WHERE SID = 'Telnet_Process_%d'",
427                             getpid());
428            if (mysql_query(db, sql) != 0)
429            {
430                    log_error("Delete user_online failed\n");
431                    return -1;
432            }
433    
434            return 0;
435    }


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

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