/[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.41 by sysadm, Sat Jun 7 08:33:08 2025 UTC Revision 1.67 by sysadm, Wed Nov 5 02:06:50 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     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
 #define _POSIX_C_SOURCE 200112L  
   
 #include "login.h"  
9  #include "bbs.h"  #include "bbs.h"
 #include "user_priv.h"  
10  #include "common.h"  #include "common.h"
11  #include "log.h"  #include "database.h"
12  #include "io.h"  #include "io.h"
13    #include "ip_mask.h"
14    #include "log.h"
15    #include "login.h"
16  #include "screen.h"  #include "screen.h"
17  #include "database.h"  #include "user_priv.h"
 #include <errno.h>  
18  #include <ctype.h>  #include <ctype.h>
19    #include <errno.h>
20    #include <stdlib.h>
21  #include <string.h>  #include <string.h>
22  #include <regex.h>  #include <regex.h>
 #include <stdlib.h>  
23  #include <unistd.h>  #include <unistd.h>
24  #include <mysql/mysql.h>  #include <mysql/mysql.h>
25    #include <sys/param.h>
26    
27    static const int BBS_username_min_len = 3; // common len = 5, special len = 3
28    static const int BBS_password_min_len = 5; // legacy len = 5, current len = 6
29    
30    static const int BBS_allowed_login_failures_within_interval = 10;
31    static const int BBS_login_failures_count_interval = 10; // minutes
32    static const int BBS_allowed_login_failures_per_account = 3;
33    
34    const int BBS_login_retry_times = 3;
35    
36  int bbs_login(void)  int bbs_login(void)
37  {  {
# Line 41  int bbs_login(void) Line 42  int bbs_login(void)
42    
43          for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++)          for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++)
44          {          {
45                  prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', "                  prints("\033[1;33m璇疯緭鍏ュ笎鍙穃033[m(璇曠敤璇疯緭鍏\033[1;36mguest\033[m', "
46                             "注册请输入`\033[1;31mnew\033[m'): ");                             "娉ㄥ唽璇疯緭鍏\033[1;31mnew\033[m'): ");
47                  iflush();                  iflush();
48    
49                  if (str_input(username, sizeof(username), DOECHO) < 0)                  if (str_input(username, sizeof(username), DOECHO) < 0)
# Line 59  int bbs_login(void) Line 60  int bbs_login(void)
60    
61                  if (strcmp(username, "new") == 0)                  if (strcmp(username, "new") == 0)
62                  {                  {
63                          display_file(DATA_REGISTER, 1, 1);                          display_file(DATA_REGISTER, 1);
64    
65                          return 0;                          return -1;
66                  }                  }
67    
68                  if (username[0] != '\0')                  if (username[0] != '\0')
69                  {                  {
70                          prints("\033[1;37m请输入密码\033[m: ");                          prints("\033[1;37m璇疯緭鍏ュ瘑鐮乗033[m: ");
71                          iflush();                          iflush();
72    
73                          if (str_input(password, sizeof(password), NOECHO) < 0)                          if (str_input(password, sizeof(password), NOECHO) < 0)
# Line 81  int bbs_login(void) Line 82  int bbs_login(void)
82    
83          if (!ok)          if (!ok)
84          {          {
85                  display_file(DATA_LOGIN_ERROR, 1, 1);                  display_file(DATA_LOGIN_ERROR, 1);
86                  return -1;                  return -1;
87          }          }
88    
# Line 93  int bbs_login(void) Line 94  int bbs_login(void)
94    
95  int check_user(const char *username, const char *password)  int check_user(const char *username, const char *password)
96  {  {
97          MYSQL *db;          MYSQL *db = NULL;
98          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
99          MYSQL_ROW row;          MYSQL_ROW row;
100          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
101          int ret;          int ret = 0;
102          int BBS_uid = 0;          int BBS_uid = 0;
103          char client_addr[IP_ADDR_LEN];          char client_addr[IP_ADDR_LEN];
104          int i;          int i;
# Line 107  int check_user(const char *username, con Line 108  int check_user(const char *username, con
108          db = db_open();          db = db_open();
109          if (db == NULL)          if (db == NULL)
110          {          {
111                  return -1;                  ret = -1;
112                    goto cleanup;
113          }          }
114    
115          // Verify format          // Verify format
116          for (i = 0; ok && username[i] != '\0'; i++)          for (i = 0; ok && username[i] != '\0'; i++)
117          {          {
118                  if (!(isalpha(username[i]) || (i > 0 && isdigit(username[i]))))                  if (!(isalpha(username[i]) || (i > 0 && (isdigit(username[i]) || username[i] == '_'))))
119                  {                  {
120                          ok = 0;                          ok = 0;
121                  }                  }
122          }          }
123          if (ok && (i < 3 || i > 12))          if (ok && (i < BBS_username_min_len || i > BBS_username_max_len))
124          {          {
125                  ok = 0;                  ok = 0;
126          }          }
# Line 129  int check_user(const char *username, con Line 131  int check_user(const char *username, con
131                          ok = 0;                          ok = 0;
132                  }                  }
133          }          }
134          if (ok && (i < 5 || i > 12))          if (ok && (i < BBS_password_min_len || i > BBS_password_max_len))
135          {          {
136                  ok = 0;                  ok = 0;
137          }          }
138    
139          if (!ok)          if (!ok)
140          {          {
141                  prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");                  prints("\033[1;31m鐢ㄦ埛鍚嶆垨瀵嗙爜鏍煎紡閿欒...\033[m\r\n");
142                  return 1;                  ret = 1;
143                    goto cleanup;
144          }          }
145    
146          // Begin transaction          // Begin transaction
147          if (mysql_query(db, "SET autocommit=0") != 0)          if (mysql_query(db, "SET autocommit=0") != 0)
148          {          {
149                  log_error("SET autocommit=0 error: %s\n", mysql_error(db));                  log_error("SET autocommit=0 error: %s\n", mysql_error(db));
150                  mysql_close(db);                  ret = -1;
151                  return -1;                  goto cleanup;
152          }          }
153    
154          if (mysql_query(db, "BEGIN") != 0)          if (mysql_query(db, "BEGIN") != 0)
155          {          {
156                  log_error("Begin transaction error: %s\n", mysql_error(db));                  log_error("Begin transaction error: %s\n", mysql_error(db));
157                  mysql_close(db);                  ret = -1;
158                  return -1;                  goto cleanup;
159          }          }
160    
161          // Failed login attempts from the same source (subnet /24) during certain time period          // Failed login attempts from the same source (subnet /24) during certain time period
# Line 161  int check_user(const char *username, con Line 164  int check_user(const char *username, con
164    
165          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
166                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "
167                           "WHERE login_dt >= SUBDATE(NOW(), INTERVAL 10 MINUTE) "                           "WHERE login_dt >= SUBDATE(NOW(), INTERVAL %d MINUTE) "
168                           "AND login_ip LIKE '%s'",                           "AND login_ip LIKE '%s'",
169                             BBS_login_failures_count_interval,
170                           ip_mask(client_addr, 1, '%'));                           ip_mask(client_addr, 1, '%'));
171          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
172          {          {
173                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s\n", mysql_error(db));
174                  mysql_close(db);                  ret = -1;
175                  return -1;                  goto cleanup;
176          }          }
177          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
178          {          {
179                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
180                  mysql_close(db);                  ret = -1;
181                  return -1;                  goto cleanup;
182          }          }
183          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
184          {          {
185                  if (atoi(row[0]) >= 2)                  if (atoi(row[0]) >= BBS_allowed_login_failures_within_interval)
186                  {                  {
187                          mysql_free_result(rs);                          prints("\033[1;31m鏉ユ簮瀛樺湪澶氭澶辫触鐧婚檰灏濊瘯锛岃绋嶅悗鍐嶈瘯锛屾垨浣跨敤Web鏂瑰紡璁块棶\033[m\r\n");
188                          mysql_close(db);                          ret = 1;
189                            goto cleanup;
                         prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n");  
   
                         return 1;  
190                  }                  }
191          }          }
192          mysql_free_result(rs);          mysql_free_result(rs);
193            rs = NULL;
194    
195          // Failed login attempts against the current username during certain time period          // Failed login attempts against the current username since last successful login
196          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
197                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "
198                           "WHERE username = '%s' AND login_dt >= SUBDATE(NOW(), INTERVAL 1 DAY)",                           "LEFT JOIN user_list ON user_err_login_log.username = user_list.username "
199                             "LEFT JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID "
200                             "WHERE user_err_login_log.username = '%s' "
201                             "AND (user_err_login_log.login_dt >= user_pubinfo.last_login_dt "
202                             "OR user_pubinfo.last_login_dt IS NULL)",
203                           username);                           username);
204          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
205          {          {
206                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s\n", mysql_error(db));
207                  mysql_close(db);                  ret = -1;
208                  return -1;                  goto cleanup;
209          }          }
210          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
211          {          {
212                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
213                  mysql_close(db);                  ret = -1;
214                  return -1;                  goto cleanup;
215          }          }
216          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
217          {          {
218                  if (atoi(row[0]) >= 5)                  if (atoi(row[0]) >= BBS_allowed_login_failures_per_account)
219                  {                  {
220                          mysql_free_result(rs);                          prints("\033[1;31m璐︽埛瀛樺湪澶氭澶辫触鐧婚檰灏濊瘯锛岃浣跨敤Web鏂瑰紡鐧诲綍瑙i攣\033[m\r\n");
221                          mysql_close(db);                          ret = 1;
222                            goto cleanup;
                         prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n");  
   
                         return 1;  
223                  }                  }
224          }          }
225          mysql_free_result(rs);          mysql_free_result(rs);
226            rs = NULL;
227    
228          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
229                           "SELECT UID, username, p_login FROM user_list "                           "SELECT UID, username, p_login FROM user_list "
# Line 228  int check_user(const char *username, con Line 232  int check_user(const char *username, con
232          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
233          {          {
234                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s\n", mysql_error(db));
235                  mysql_close(db);                  ret = -1;
236                  return -1;                  goto cleanup;
237          }          }
238          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
239          {          {
240                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
241                  mysql_close(db);                  ret = -1;
242                  return -1;                  goto cleanup;
243          }          }
244          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
245          {          {
# Line 245  int check_user(const char *username, con Line 249  int check_user(const char *username, con
249                  int p_login = atoi(row[2]);                  int p_login = atoi(row[2]);
250    
251                  mysql_free_result(rs);                  mysql_free_result(rs);
252                    rs = NULL;
253    
254                  // Add user login log                  // Add user login log
255                  snprintf(sql, sizeof(sql),                  snprintf(sql, sizeof(sql),
# Line 254  int check_user(const char *username, con Line 259  int check_user(const char *username, con
259                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
260                  {                  {
261                          log_error("Insert into user_login_log error: %s\n", mysql_error(db));                          log_error("Insert into user_login_log error: %s\n", mysql_error(db));
262                          mysql_close(db);                          ret = -1;
263                          return -1;                          goto cleanup;
264                  }                  }
265    
266                  // Commit transaction                  // Commit transaction
267                  if (mysql_query(db, "COMMIT") != 0)                  if (mysql_query(db, "COMMIT") != 0)
268                  {                  {
269                          log_error("Commit transaction error: %s\n", mysql_error(db));                          log_error("Commit transaction error: %s\n", mysql_error(db));
270                          mysql_close(db);                          ret = -1;
271                          return -1;                          goto cleanup;
272                  }                  }
273    
274                  if (p_login == 0)                  if (p_login == 0)
275                  {                  {
276                          mysql_free_result(rs);                          prints("\033[1;31m鎮ㄧ洰鍓嶆棤鏉冪櫥闄...\033[m\r\n");
277                          mysql_close(db);                          ret = 1;
278                            goto cleanup;
                         prints("\033[1;31m您目前无权登陆...\033[m\r\n");  
                         return 1;  
279                  }                  }
280          }          }
281          else          else
282          {          {
283                  mysql_free_result(rs);                  mysql_free_result(rs);
284                  mysql_close(db);                  rs = NULL;
285    
286                  snprintf(sql, sizeof(sql),                  snprintf(sql, sizeof(sql),
287                                   "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "                                   "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "
# Line 287  int check_user(const char *username, con Line 290  int check_user(const char *username, con
290                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
291                  {                  {
292                          log_error("Insert into user_err_login_log error: %s\n", mysql_error(db));                          log_error("Insert into user_err_login_log error: %s\n", mysql_error(db));
293                          return -1;                          ret = -1;
294                            goto cleanup;
295                  }                  }
296    
297                  // Commit transaction                  // Commit transaction
298                  if (mysql_query(db, "COMMIT") != 0)                  if (mysql_query(db, "COMMIT") != 0)
299                  {                  {
300                          log_error("Commit transaction error: %s\n", mysql_error(db));                          log_error("Commit transaction error: %s\n", mysql_error(db));
301                          mysql_close(db);                          ret = -1;
302                          return -1;                          goto cleanup;
303                  }                  }
304    
305                  prints("\033[1;31m错误的用户名或密码...\033[m\r\n");                  prints("\033[1;31m閿欒鐨勭敤鎴峰悕鎴栧瘑鐮...\033[m\r\n");
306                  mysql_close(db);                  ret = 1;
307                  return 1;                  goto cleanup;
308          }          }
309    
310          // Set AUTOCOMMIT = 1          // Set AUTOCOMMIT = 1
311          if (mysql_query(db, "SET autocommit=1") != 0)          if (mysql_query(db, "SET autocommit=1") != 0)
312          {          {
313                  log_error("SET autocommit=1 error: %s\n", mysql_error(db));                  log_error("SET autocommit=1 error: %s\n", mysql_error(db));
314                  mysql_close(db);                  ret = -1;
315                  return -1;                  goto cleanup;
316          }          }
317    
318          ret = load_user_info(db, BBS_uid);          ret = load_user_info(db, BBS_uid);
# Line 318  int check_user(const char *username, con Line 322  int check_user(const char *username, con
322          case 0: // Login successfully          case 0: // Login successfully
323                  break;                  break;
324          case -1: // Load data error          case -1: // Load data error
325                  prints("\033[1;31m读取用户数据错误...\033[m\r\n");                  prints("\033[1;31m璇诲彇鐢ㄦ埛鏁版嵁閿欒...\033[m\r\n");
326                  mysql_close(db);                  ret = -1;
327                  return -1;                  goto cleanup;
328          case -2: // Unused          case -2: // Unused
329                  prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");                  prints("\033[1;31m璇烽氳繃Web鐧诲綍鏇存柊鐢ㄦ埛璁稿彲鍗忚...\033[m\r\n");
330                  mysql_close(db);                  ret = 1;
331                  return 1;                  goto cleanup;
332          case -3: // Dead          case -3: // Dead
333                  prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");                  prints("\033[1;31m寰堥仐鎲撅紝鎮ㄥ凡缁忔案杩滅寮浜嗘垜浠殑涓栫晫锛乗033[m\r\n");
334                  mysql_close(db);                  ret = 1;
335                  return 1;                  goto cleanup;
336          default:          default:
337                  mysql_close(db);                  ret = -2;
338                  return -2;                  goto cleanup;
339          }          }
340    
341          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 341  int check_user(const char *username, con Line 345  int check_user(const char *username, con
345          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
346          {          {
347                  log_error("Update user_pubinfo error: %s\n", mysql_error(db));                  log_error("Update user_pubinfo error: %s\n", mysql_error(db));
348                  mysql_close(db);                  ret = -1;
349                  return -1;                  goto cleanup;
350          }          }
351    
352          if (user_online_add(db) != 0)          if (user_online_add(db) != 0)
353          {          {
354                  mysql_close(db);                  ret = -1;
355                  return -1;                  goto cleanup;
356          }          }
357    
358          BBS_last_access_tm = BBS_login_tm = time(0);          BBS_last_access_tm = BBS_login_tm = time(NULL);
359    
360          // Set user tz to process env          // Set user tz to process env
361          if (BBS_user_tz[0] != '\0')          if (BBS_user_tz[0] != '\0')
# Line 369  int check_user(const char *username, con Line 373  int check_user(const char *username, con
373                  tzset();                  tzset();
374          }          }
375    
376    cleanup:
377            mysql_free_result(rs);
378          mysql_close(db);          mysql_close(db);
379    
380          return 0;          return ret;
381  }  }
382    
383  int load_user_info(MYSQL *db, int BBS_uid)  int load_user_info(MYSQL *db, int BBS_uid)
384  {  {
385          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
386          MYSQL_ROW row;          MYSQL_ROW row;
387          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
388          int life;          int life;
389          time_t last_login_dt;          time_t last_login_dt;
390            int ret = 0;
391    
392          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
393                           "SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone "                           "SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone, exp, nickname "
394                           "FROM user_pubinfo WHERE UID = %d",                           "FROM user_pubinfo WHERE UID = %d",
395                           BBS_uid);                           BBS_uid);
396          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
397          {          {
398                  log_error("Query user_pubinfo error: %s\n", mysql_error(db));                  log_error("Query user_pubinfo error: %s\n", mysql_error(db));
399                  return -1;                  ret = -1;
400                    goto cleanup;
401          }          }
402          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
403          {          {
404                  log_error("Get user_pubinfo data failed\n");                  log_error("Get user_pubinfo data failed\n");
405                  return -1;                  ret = -1;
406                    goto cleanup;
407          }          }
408          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
409          {          {
# Line 403  int load_user_info(MYSQL *db, int BBS_ui Line 412  int load_user_info(MYSQL *db, int BBS_ui
412    
413                  strncpy(BBS_user_tz, row[2], sizeof(BBS_user_tz) - 1);                  strncpy(BBS_user_tz, row[2], sizeof(BBS_user_tz) - 1);
414                  BBS_user_tz[sizeof(BBS_user_tz) - 1] = '\0';                  BBS_user_tz[sizeof(BBS_user_tz) - 1] = '\0';
415    
416                    BBS_user_exp = atoi(row[3]);
417    
418                    strncpy(BBS_nickname, row[4], sizeof(BBS_nickname));
419                    BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
420          }          }
421          else          else
422          {          {
423                  mysql_free_result(rs);                  ret = -1; // Data not found
424                  return -1; // Data not found                  goto cleanup;
425          }          }
426          mysql_free_result(rs);          mysql_free_result(rs);
427            rs = NULL;
428    
429          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
430                  time(0) - last_login_dt > 60 * 60 * 24 * life)                  time(NULL) - last_login_dt > 60 * 60 * 24 * life)
431          {          {
432                  return -3; // Dead                  ret = -3; // Dead
433                    goto cleanup;
434          }          }
435    
436          if (load_priv(db, &BBS_priv, BBS_uid) != 0)          if (load_priv(db, &BBS_priv, BBS_uid) != 0)
437          {          {
438                  return -1;                  ret = -1;
439                    goto cleanup;
440          }          }
441    
442          return 0;  cleanup:
443            mysql_free_result(rs);
444    
445            return ret;
446  }  }
447    
448  int load_guest_info(void)  int load_guest_info(void)
449  {  {
450          MYSQL *db;          MYSQL *db = NULL;
451            int ret = 0;
452    
453          db = db_open();          db = db_open();
454          if (db == NULL)          if (db == NULL)
455          {          {
456                  return -1;                  ret = -1;
457                    goto cleanup;
458          }          }
459    
460          strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);          strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);
461          BBS_username[sizeof(BBS_username) - 1] = '\0';          BBS_username[sizeof(BBS_username) - 1] = '\0';
462    
463            BBS_user_exp = 0;
464    
465            strncpy(BBS_nickname, "Guest", sizeof(BBS_nickname));
466            BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
467    
468          if (load_priv(db, &BBS_priv, 0) != 0)          if (load_priv(db, &BBS_priv, 0) != 0)
469          {          {
470                  return -1;                  ret = -1;
471                    goto cleanup;
472          }          }
473    
474          if (user_online_add(db) != 0)          if (user_online_add(db) != 0)
475          {          {
476                  return -1;                  ret = -1;
477                    goto cleanup;
478          }          }
479    
480          BBS_last_access_tm = BBS_login_tm = time(0);          BBS_last_access_tm = BBS_login_tm = time(NULL);
481    
482    cleanup:
483          mysql_close(db);          mysql_close(db);
484            
485          return 0;          return ret;
486  }  }
487    
488  int user_online_add(MYSQL *db)  int user_online_add(MYSQL *db)
489  {  {
490          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
491    
492          if (user_online_del(db) != 0)          snprintf(sql, sizeof(sql),
493                             "INSERT INTO visit_log(dt, IP) VALUES(NOW(), '%s')",
494                             hostaddr_client);
495            if (mysql_query(db, sql) != 0)
496          {          {
497                    log_error("Add visit log error: %s\n", mysql_error(db));
498                  return -1;                  return -1;
499          }          }
500    
501            if (user_online_del(db) != 0)
502            {
503                    return -2;
504            }
505    
506          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
507                           "INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) "                           "INSERT INTO user_online(SID, UID, ip, current_action, login_tm, last_tm) "
508                           "VALUES('Telnet_Process_%d', %d, '%s', NOW(), NOW())",                           "VALUES('Telnet_Process_%d', %d, '%s', 'LOGIN', NOW(), NOW())",
509                           getpid(), BBS_priv.uid, hostaddr_client);                           getpid(), BBS_priv.uid, hostaddr_client);
510          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
511          {          {
512                  log_error("Add user_online error: %s\n", mysql_error(db));                  log_error("Add user_online error: %s\n", mysql_error(db));
513                  return -1;                  return -3;
514          }          }
515    
516          return 0;          return 0;
# Line 492  int user_online_del(MYSQL *db) Line 531  int user_online_del(MYSQL *db)
531    
532          return 0;          return 0;
533  }  }
534    
535    int user_online_exp(MYSQL *db)
536    {
537            char sql[SQL_BUFFER_LEN];
538    
539            // +1 exp for every 5 minutes online since last logout
540            // but at most 24 hours worth of exp can be gained in Telnet session
541            snprintf(sql, sizeof(sql),
542                             "UPDATE user_pubinfo SET exp = exp + FLOOR(LEAST(TIMESTAMPDIFF("
543                             "SECOND, GREATEST(last_login_dt, IF(last_logout_dt IS NULL, last_login_dt, last_logout_dt)), NOW()"
544                             ") / 60 / 5, 12 * 24)), last_logout_dt = NOW() "
545                             "WHERE UID = %d",
546                             BBS_priv.uid);
547            if (mysql_query(db, sql) != 0)
548            {
549                    log_error("Update user_pubinfo error: %s\n", mysql_error(db));
550                    return -1;
551            }
552    
553            return 0;
554    }
555    
556    int user_online_update(const char *action)
557    {
558            MYSQL *db = NULL;
559            char sql[SQL_BUFFER_LEN];
560    
561            if ((action == NULL || strcmp(BBS_current_action, action) == 0) &&
562                    time(NULL) - BBS_current_action_tm < BBS_current_action_refresh_interval) // No change
563            {
564                    return 0;
565            }
566    
567            if (action != NULL)
568            {
569                    strncpy(BBS_current_action, action, sizeof(BBS_current_action) - 1);
570                    BBS_current_action[sizeof(BBS_current_action) - 1] = '\0';
571            }
572    
573            BBS_current_action_tm = time(NULL);
574    
575            db = db_open();
576            if (db == NULL)
577            {
578                    log_error("db_open() error: %s\n", mysql_error(db));
579                    return -1;
580            }
581    
582            snprintf(sql, sizeof(sql),
583                             "UPDATE user_online SET current_action = '%s', last_tm = NOW() "
584                             "WHERE SID = 'Telnet_Process_%d'",
585                             BBS_current_action, getpid());
586            if (mysql_query(db, sql) != 0)
587            {
588                    log_error("Update user_online error: %s\n", mysql_error(db));
589                    return -2;
590            }
591    
592            mysql_close(db);
593    
594            return 1;
595    }


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

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