/[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.32 by sysadm, Mon May 19 01:45:45 2025 UTC Revision 1.38 by sysadm, Wed Jun 4 13:42:53 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #define _POSIX_C_SOURCE 200112L
18    
19  #include "login.h"  #include "login.h"
20  #include "bbs.h"  #include "bbs.h"
21  #include "user_priv.h"  #include "user_priv.h"
# Line 22  Line 24 
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>  #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  int bbs_login(MYSQL *db)  int bbs_login(MYSQL *db)
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          for (; !SYS_server_exit && !ok && count < 3; count++)          for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++)
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'): ");
# Line 55  int bbs_login(MYSQL *db) Line 59  int bbs_login(MYSQL *db)
59    
60                  if (strcmp(username, "new") == 0)                  if (strcmp(username, "new") == 0)
61                  {                  {
62                          display_file_ex(DATA_REGISTER, 1, 1);                          display_file(DATA_REGISTER, 1, 1);
63    
64                          return 0;                          return 0;
65                  }                  }
# Line 77  int bbs_login(MYSQL *db) Line 81  int bbs_login(MYSQL *db)
81    
82          if (!ok)          if (!ok)
83          {          {
84                  display_file_ex(DATA_LOGIN_ERROR, 1, 1);                  display_file(DATA_LOGIN_ERROR, 1, 1);
85                  return -1;                  return -1;
86          }          }
87    
88          log_std("User \"%s\"(%ld) login from %s:%d\n",          log_common("User \"%s\"(%ld) login from %s:%d\n",
89                          BBS_username, BBS_priv.uid, hostaddr_client, port_client);                          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(MYSQL *db, const char *username, const char *password)
95  {  {
96          MYSQL_RES *rs;          MYSQL_RES *rs;
97          MYSQL_ROW row;          MYSQL_ROW row;
# Line 97  int check_user(MYSQL *db, char *username Line 101  int check_user(MYSQL *db, char *username
101          char client_addr[IP_ADDR_LEN];          char client_addr[IP_ADDR_LEN];
102          int i;          int i;
103          int ok = 1;          int ok = 1;
104            char user_tz_env[BBS_user_tz_max_len + 2];
105    
106          // Verify format          // Verify format
107          for (i = 0; ok && username[i] != '\0'; i++)          for (i = 0; ok && username[i] != '\0'; i++)
# Line 131  int check_user(MYSQL *db, char *username Line 136  int check_user(MYSQL *db, char *username
136          // Begin transaction          // Begin transaction
137          if (mysql_query(db, "SET autocommit=0") != 0)          if (mysql_query(db, "SET autocommit=0") != 0)
138          {          {
139                  log_error("SET autocommit=0 failed\n");                  log_error("SET autocommit=0 error: %s\n", mysql_error(db));
140                  return -1;                  return -1;
141          }          }
142    
143          if (mysql_query(db, "BEGIN") != 0)          if (mysql_query(db, "BEGIN") != 0)
144          {          {
145                  log_error("Begin transaction failed\n");                  log_error("Begin transaction error: %s\n", mysql_error(db));
146                  return -1;                  return -1;
147          }          }
148    
# Line 152  int check_user(MYSQL *db, char *username Line 157  int check_user(MYSQL *db, char *username
157                           ip_mask(client_addr, 1, '%'));                           ip_mask(client_addr, 1, '%'));
158          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
159          {          {
160                  log_error("Query user_list failed\n");                  log_error("Query user_list error: %s\n", mysql_error(db));
161                  return -1;                  return -1;
162          }          }
163          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 180  int check_user(MYSQL *db, char *username Line 185  int check_user(MYSQL *db, char *username
185                           username);                           username);
186          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
187          {          {
188                  log_error("Query user_list failed\n");                  log_error("Query user_list error: %s\n", mysql_error(db));
189                  return -1;                  return -1;
190          }          }
191          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 207  int check_user(MYSQL *db, char *username Line 212  int check_user(MYSQL *db, char *username
212                           username, password);                           username, password);
213          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
214          {          {
215                  log_error("Query user_list failed\n");                  log_error("Query user_list error: %s\n", mysql_error(db));
216                  return -1;                  return -1;
217          }          }
218          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 231  int check_user(MYSQL *db, char *username Line 236  int check_user(MYSQL *db, char *username
236                                   BBS_uid, hostaddr_client);                                   BBS_uid, hostaddr_client);
237                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
238                  {                  {
239                          log_error("Insert into user_login_log failed\n");                          log_error("Insert into user_login_log error: %s\n", mysql_error(db));
240                          return -1;                          return -1;
241                  }                  }
242    
243                  // Commit transaction                  // Commit transaction
244                  if (mysql_query(db, "COMMIT") != 0)                  if (mysql_query(db, "COMMIT") != 0)
245                  {                  {
246                          log_error("Commit transaction failed\n");                          log_error("Commit transaction error: %s\n", mysql_error(db));
247                          return -1;                          return -1;
248                  }                  }
249    
# Line 260  int check_user(MYSQL *db, char *username Line 265  int check_user(MYSQL *db, char *username
265                                   username, password, hostaddr_client);                                   username, password, hostaddr_client);
266                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
267                  {                  {
268                          log_error("Insert into user_err_login_log failed\n");                          log_error("Insert into user_err_login_log error: %s\n", mysql_error(db));
269                          return -1;                          return -1;
270                  }                  }
271    
272                  // Commit transaction                  // Commit transaction
273                  if (mysql_query(db, "COMMIT") != 0)                  if (mysql_query(db, "COMMIT") != 0)
274                  {                  {
275                          log_error("Commit transaction failed\n");                          log_error("Commit transaction error: %s\n", mysql_error(db));
276                          return -1;                          return -1;
277                  }                  }
278    
# Line 278  int check_user(MYSQL *db, char *username Line 283  int check_user(MYSQL *db, char *username
283          // Set AUTOCOMMIT = 1          // Set AUTOCOMMIT = 1
284          if (mysql_query(db, "SET autocommit=1") != 0)          if (mysql_query(db, "SET autocommit=1") != 0)
285          {          {
286                  log_error("SET autocommit=1 failed\n");                  log_error("SET autocommit=1 error: %s\n", mysql_error(db));
287                  return -1;                  return -1;
288          }          }
289    
# Line 307  int check_user(MYSQL *db, char *username Line 312  int check_user(MYSQL *db, char *username
312                           BBS_uid);                           BBS_uid);
313          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
314          {          {
315                  log_error("Update user_pubinfo failed\n");                  log_error("Update user_pubinfo error: %s\n", mysql_error(db));
316                  return -1;                  return -1;
317          }          }
318    
# Line 318  int check_user(MYSQL *db, char *username Line 323  int check_user(MYSQL *db, char *username
323    
324          BBS_last_access_tm = BBS_login_tm = time(0);          BBS_last_access_tm = BBS_login_tm = time(0);
325    
326            // Set user tz to process env
327            if (BBS_user_tz[0] != '\0')
328            {
329                    user_tz_env[0] = ':';
330                    strncpy(user_tz_env + 1, BBS_user_tz, sizeof(user_tz_env) - 2);
331                    user_tz_env[sizeof(user_tz_env) - 1] = '\0';
332    
333                    if (setenv("TZ", user_tz_env, 1) == -1)
334                    {
335                            log_error("setenv(TZ = %s) error %d\n", user_tz_env, errno);
336                            return -3;
337                    }
338    
339                    tzset();
340            }
341    
342          return 0;          return 0;
343  }  }
344    
# Line 330  int load_user_info(MYSQL *db, long int B Line 351  int load_user_info(MYSQL *db, long int B
351          time_t last_login_dt;          time_t last_login_dt;
352    
353          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
354                           "SELECT life, UNIX_TIMESTAMP(last_login_dt) "                           "SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone "
355                           "FROM user_pubinfo WHERE UID = %ld",                           "FROM user_pubinfo WHERE UID = %ld",
356                           BBS_uid);                           BBS_uid);
357          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
358          {          {
359                  log_error("Query user_pubinfo failed\n");                  log_error("Query user_pubinfo error: %s\n", mysql_error(db));
360                  return -1;                  return -1;
361          }          }
362          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 347  int load_user_info(MYSQL *db, long int B Line 368  int load_user_info(MYSQL *db, long int B
368          {          {
369                  life = atoi(row[0]);                  life = atoi(row[0]);
370                  last_login_dt = (time_t)atol(row[1]);                  last_login_dt = (time_t)atol(row[1]);
371    
372                    strncpy(BBS_user_tz, row[2], sizeof(BBS_user_tz) - 1);
373                    BBS_user_tz[sizeof(BBS_user_tz) - 1] = '\0';
374          }          }
375          else          else
376          {          {
# Line 404  int user_online_add(MYSQL *db) Line 428  int user_online_add(MYSQL *db)
428                           getpid(), BBS_priv.uid, hostaddr_client);                           getpid(), BBS_priv.uid, hostaddr_client);
429          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
430          {          {
431                  log_error("Add user_online failed\n");                  log_error("Add user_online error: %s\n", mysql_error(db));
432                  return -1;                  return -1;
433          }          }
434    
# Line 420  int user_online_del(MYSQL *db) Line 444  int user_online_del(MYSQL *db)
444                           getpid());                           getpid());
445          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
446          {          {
447                  log_error("Delete user_online failed\n");                  log_error("Delete user_online error: %s\n", mysql_error(db));
448                  return -1;                  return -1;
449          }          }
450    


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

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