/[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.35 by sysadm, Thu May 29 13:17:33 2025 UTC Revision 1.52 by sysadm, Sun Sep 28 12:25:14 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #define _POSIX_C_SOURCE 200112L  
   
 #include "login.h"  
17  #include "bbs.h"  #include "bbs.h"
 #include "user_priv.h"  
18  #include "common.h"  #include "common.h"
19  #include "log.h"  #include "database.h"
20  #include "io.h"  #include "io.h"
21    #include "ip_mask.h"
22    #include "log.h"
23    #include "login.h"
24  #include "screen.h"  #include "screen.h"
25  #include "database.h"  #include "user_priv.h"
 #include <errno.h>  
26  #include <ctype.h>  #include <ctype.h>
27    #include <errno.h>
28    #include <stdlib.h>
29  #include <string.h>  #include <string.h>
 #include <mysql.h>  
30  #include <regex.h>  #include <regex.h>
 #include <stdlib.h>  
31  #include <unistd.h>  #include <unistd.h>
32    #include <mysql/mysql.h>
33    
34  int bbs_login(MYSQL *db)  int bbs_login(void)
35  {  {
36          char username[BBS_username_max_len + 1];          char username[BBS_username_max_len + 1];
37          char password[BBS_password_max_len + 1];          char password[BBS_password_max_len + 1];
38          int count = 0;          int i = 0;
39          int ok = 0;          int ok = 0;
40    
41          for (; !SYS_server_exit && !ok && count < 3; count++)          for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++)
42          {          {
43                  prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', "                  prints("\033[1;33m璇疯緭鍏ュ笎鍙穃033[m(璇曠敤璇疯緭鍏\033[1;36mguest\033[m', "
44                             "注册请输入`\033[1;31mnew\033[m'): ");                             "娉ㄥ唽璇疯緭鍏\033[1;31mnew\033[m'): ");
45                  iflush();                  iflush();
46    
47                  if (str_input(username, sizeof(username), DOECHO) < 0)                  if (str_input(username, sizeof(username), DOECHO) < 0)
# Line 52  int bbs_login(MYSQL *db) Line 51  int bbs_login(MYSQL *db)
51    
52                  if (strcmp(username, "guest") == 0)                  if (strcmp(username, "guest") == 0)
53                  {                  {
54                          load_guest_info(db);                          load_guest_info();
55    
56                          return 0;                          return 0;
57                  }                  }
58    
59                  if (strcmp(username, "new") == 0)                  if (strcmp(username, "new") == 0)
60                  {                  {
61                          display_file(DATA_REGISTER, 1, 1);                          display_file(DATA_REGISTER, 1);
62    
63                          return 0;                          return -1;
64                  }                  }
65    
66                  if (username[0] != '\0')                  if (username[0] != '\0')
67                  {                  {
68                          prints("\033[1;37m请输入密码\033[m: ");                          prints("\033[1;37m璇疯緭鍏ュ瘑鐮乗033[m: ");
69                          iflush();                          iflush();
70    
71                          if (str_input(password, sizeof(password), NOECHO) < 0)                          if (str_input(password, sizeof(password), NOECHO) < 0)
# Line 74  int bbs_login(MYSQL *db) Line 73  int bbs_login(MYSQL *db)
73                                  continue;                                  continue;
74                          }                          }
75    
76                          ok = (check_user(db, username, password) == 0);                          ok = (check_user(username, password) == 0);
77                          iflush();                          iflush();
78                  }                  }
79          }          }
80    
81          if (!ok)          if (!ok)
82          {          {
83                  display_file(DATA_LOGIN_ERROR, 1, 1);                  display_file(DATA_LOGIN_ERROR, 1);
84                  return -1;                  return -1;
85          }          }
86    
87          log_std("User \"%s\"(%ld) login from %s:%d\n",          log_common("User \"%s\"(%ld) login from %s:%d\n",
88                          BBS_username, BBS_priv.uid, hostaddr_client, port_client);                             BBS_username, BBS_priv.uid, hostaddr_client, port_client);
89    
90          return 0;          return 0;
91  }  }
92    
93  int check_user(MYSQL *db, char *username, char *password)  int check_user(const char *username, const char *password)
94  {  {
95          MYSQL_RES *rs;          MYSQL *db = NULL;
96            MYSQL_RES *rs = NULL;
97          MYSQL_ROW row;          MYSQL_ROW row;
98          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
99          int ret;          int ret = 0;
100          long BBS_uid = 0;          int BBS_uid = 0;
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];          char user_tz_env[BBS_user_tz_max_len + 2];
105    
106            db = db_open();
107            if (db == NULL)
108            {
109                    ret = -1;
110                    goto cleanup;
111            }
112    
113          // Verify format          // Verify format
114          for (i = 0; ok && username[i] != '\0'; i++)          for (i = 0; ok && username[i] != '\0'; i++)
115          {          {
# Line 129  int check_user(MYSQL *db, char *username Line 136  int check_user(MYSQL *db, char *username
136    
137          if (!ok)          if (!ok)
138          {          {
139                  prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");                  prints("\033[1;31m鐢ㄦ埛鍚嶆垨瀵嗙爜鏍煎紡閿欒...\033[m\r\n");
140                  return 1;                  ret = 1;
141                    goto cleanup;
142          }          }
143    
144          // Begin transaction          // Begin transaction
145          if (mysql_query(db, "SET autocommit=0") != 0)          if (mysql_query(db, "SET autocommit=0") != 0)
146          {          {
147                  log_error("SET autocommit=0 error: %s\n", mysql_error(db));                  log_error("SET autocommit=0 error: %s\n", mysql_error(db));
148                  return -1;                  ret = -1;
149                    goto cleanup;
150          }          }
151    
152          if (mysql_query(db, "BEGIN") != 0)          if (mysql_query(db, "BEGIN") != 0)
153          {          {
154                  log_error("Begin transaction error: %s\n", mysql_error(db));                  log_error("Begin transaction error: %s\n", mysql_error(db));
155                  return -1;                  ret = -1;
156                    goto cleanup;
157          }          }
158    
159          // 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 152  int check_user(MYSQL *db, char *username Line 162  int check_user(MYSQL *db, char *username
162    
163          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
164                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "
165                           "WHERE login_dt >= SUBDATE(NOW(), INTERVAL 10 MINUTE) "                           "WHERE login_dt >= SUBDATE(NOW(), INTERVAL %d MINUTE) "
166                           "AND login_ip LIKE '%s'",                           "AND login_ip LIKE '%s'",
167                             BBS_login_failures_count_interval,
168                           ip_mask(client_addr, 1, '%'));                           ip_mask(client_addr, 1, '%'));
169          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
170          {          {
171                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s\n", mysql_error(db));
172                  return -1;                  ret = -1;
173                    goto cleanup;
174          }          }
175          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
176          {          {
177                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
178                  return -1;                  ret = -1;
179                    goto cleanup;
180          }          }
181          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
182          {          {
183                  if (atoi(row[0]) >= 2)                  if (atoi(row[0]) >= BBS_allowed_login_failures_within_interval)
184                  {                  {
185                          mysql_free_result(rs);                          prints("\033[1;31m鏉ユ簮瀛樺湪澶氭澶辫触鐧婚檰灏濊瘯锛岃绋嶅悗鍐嶈瘯锛屾垨浣跨敤Web鏂瑰紡璁块棶\033[m\r\n");
186                            ret = 1;
187                          prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n");                          goto cleanup;
   
                         return 1;  
188                  }                  }
189          }          }
190          mysql_free_result(rs);          mysql_free_result(rs);
191            rs = NULL;
192    
193          // Failed login attempts against the current username during certain time period          // Failed login attempts against the current username since last successful login
194          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
195                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "
196                           "WHERE username = '%s' AND login_dt >= SUBDATE(NOW(), INTERVAL 1 DAY)",                           "LEFT JOIN user_list ON user_err_login_log.username = user_list.username "
197                             "LEFT JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID "
198                             "WHERE user_err_login_log.username = '%s' "
199                             "AND (user_err_login_log.login_dt >= user_pubinfo.last_login_dt "
200                             "OR user_pubinfo.last_login_dt IS NULL)",
201                           username);                           username);
202          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
203          {          {
204                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s\n", mysql_error(db));
205                  return -1;                  ret = -1;
206                    goto cleanup;
207          }          }
208          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
209          {          {
210                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
211                  return -1;                  ret = -1;
212                    goto cleanup;
213          }          }
214          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
215          {          {
216                  if (atoi(row[0]) >= 5)                  if (atoi(row[0]) >= 3)
217                  {                  {
218                          mysql_free_result(rs);                          prints("\033[1;31m璐︽埛瀛樺湪澶氭澶辫触鐧婚檰灏濊瘯锛岃浣跨敤Web鏂瑰紡鐧诲綍瑙i攣\033[m\r\n");
219                            ret = 1;
220                          prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n");                          goto cleanup;
   
                         return 1;  
221                  }                  }
222          }          }
223          mysql_free_result(rs);          mysql_free_result(rs);
224            rs = NULL;
225    
226          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
227                           "SELECT UID, username, p_login FROM user_list "                           "SELECT UID, username, p_login FROM user_list "
# Line 213  int check_user(MYSQL *db, char *username Line 230  int check_user(MYSQL *db, char *username
230          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
231          {          {
232                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s\n", mysql_error(db));
233                  return -1;                  ret = -1;
234                    goto cleanup;
235          }          }
236          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
237          {          {
238                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
239                  return -1;                  ret = -1;
240                    goto cleanup;
241          }          }
242          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
243          {          {
244                  BBS_uid = atol(row[0]);                  BBS_uid = atoi(row[0]);
245                  strncpy(BBS_username, row[1], sizeof(BBS_username) - 1);                  strncpy(BBS_username, row[1], sizeof(BBS_username) - 1);
246                  BBS_username[sizeof(BBS_username) - 1] = '\0';                  BBS_username[sizeof(BBS_username) - 1] = '\0';
247                  int p_login = atoi(row[2]);                  int p_login = atoi(row[2]);
248    
249                  mysql_free_result(rs);                  mysql_free_result(rs);
250                    rs = NULL;
251    
252                  // Add user login log                  // Add user login log
253                  snprintf(sql, sizeof(sql),                  snprintf(sql, sizeof(sql),
254                                   "INSERT INTO user_login_log(UID, login_dt, login_ip) "                                   "INSERT INTO user_login_log(UID, login_dt, login_ip) "
255                                   "VALUES(%ld, NOW(), '%s')",                                   "VALUES(%d, NOW(), '%s')",
256                                   BBS_uid, hostaddr_client);                                   BBS_uid, hostaddr_client);
257                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
258                  {                  {
259                          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));
260                          return -1;                          ret = -1;
261                            goto cleanup;
262                  }                  }
263    
264                  // Commit transaction                  // Commit transaction
265                  if (mysql_query(db, "COMMIT") != 0)                  if (mysql_query(db, "COMMIT") != 0)
266                  {                  {
267                          log_error("Commit transaction error: %s\n", mysql_error(db));                          log_error("Commit transaction error: %s\n", mysql_error(db));
268                          return -1;                          ret = -1;
269                            goto cleanup;
270                  }                  }
271    
272                  if (p_login == 0)                  if (p_login == 0)
273                  {                  {
274                          mysql_free_result(rs);                          prints("\033[1;31m鎮ㄧ洰鍓嶆棤鏉冪櫥闄...\033[m\r\n");
275                            ret = 1;
276                          prints("\033[1;31m您目前无权登陆...\033[m\r\n");                          goto cleanup;
                         return 1;  
277                  }                  }
278          }          }
279          else          else
280          {          {
281                  mysql_free_result(rs);                  mysql_free_result(rs);
282                    rs = NULL;
283    
284                  snprintf(sql, sizeof(sql),                  snprintf(sql, sizeof(sql),
285                                   "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 266  int check_user(MYSQL *db, char *username Line 288  int check_user(MYSQL *db, char *username
288                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
289                  {                  {
290                          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));
291                          return -1;                          ret = -1;
292                            goto cleanup;
293                  }                  }
294    
295                  // Commit transaction                  // Commit transaction
296                  if (mysql_query(db, "COMMIT") != 0)                  if (mysql_query(db, "COMMIT") != 0)
297                  {                  {
298                          log_error("Commit transaction error: %s\n", mysql_error(db));                          log_error("Commit transaction error: %s\n", mysql_error(db));
299                          return -1;                          ret = -1;
300                            goto cleanup;
301                  }                  }
302    
303                  prints("\033[1;31m错误的用户名或密码...\033[m\r\n");                  prints("\033[1;31m閿欒鐨勭敤鎴峰悕鎴栧瘑鐮...\033[m\r\n");
304                  return 1;                  ret = 1;
305                    goto cleanup;
306          }          }
307    
308          // Set AUTOCOMMIT = 1          // Set AUTOCOMMIT = 1
309          if (mysql_query(db, "SET autocommit=1") != 0)          if (mysql_query(db, "SET autocommit=1") != 0)
310          {          {
311                  log_error("SET autocommit=1 error: %s\n", mysql_error(db));                  log_error("SET autocommit=1 error: %s\n", mysql_error(db));
312                  return -1;                  ret = -1;
313                    goto cleanup;
314          }          }
315    
316          ret = load_user_info(db, BBS_uid);          ret = load_user_info(db, BBS_uid);
# Line 294  int check_user(MYSQL *db, char *username Line 320  int check_user(MYSQL *db, char *username
320          case 0: // Login successfully          case 0: // Login successfully
321                  break;                  break;
322          case -1: // Load data error          case -1: // Load data error
323                  prints("\033[1;31m读取用户数据错误...\033[m\r\n");                  prints("\033[1;31m璇诲彇鐢ㄦ埛鏁版嵁閿欒...\033[m\r\n");
324                  return -1;                  ret = -1;
325                    goto cleanup;
326          case -2: // Unused          case -2: // Unused
327                  prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");                  prints("\033[1;31m璇烽氳繃Web鐧诲綍鏇存柊鐢ㄦ埛璁稿彲鍗忚...\033[m\r\n");
328                  return 1;                  ret = 1;
329                    goto cleanup;
330          case -3: // Dead          case -3: // Dead
331                  prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");                  prints("\033[1;31m寰堥仐鎲撅紝鎮ㄥ凡缁忔案杩滅寮浜嗘垜浠殑涓栫晫锛乗033[m\r\n");
332                  return 1;                  ret = 1;
333                    goto cleanup;
334          default:          default:
335                  return -2;                  ret = -2;
336                    goto cleanup;
337          }          }
338    
339          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
340                           "UPDATE user_pubinfo SET visit_count = visit_count + 1, "                           "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
341                           "last_login_dt = NOW() WHERE UID = %ld",                           "last_login_dt = NOW() WHERE UID = %d",
342                           BBS_uid);                           BBS_uid);
343          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
344          {          {
345                  log_error("Update user_pubinfo error: %s\n", mysql_error(db));                  log_error("Update user_pubinfo error: %s\n", mysql_error(db));
346                  return -1;                  ret = -1;
347                    goto cleanup;
348          }          }
349    
350          if (user_online_add(db) != 0)          if (user_online_add(db) != 0)
351          {          {
352                  return -1;                  ret = -1;
353                    goto cleanup;
354          }          }
355    
356          BBS_last_access_tm = BBS_login_tm = time(0);          BBS_last_access_tm = BBS_login_tm = time(NULL);
357    
358          // Set user tz to process env          // Set user tz to process env
359          if (BBS_user_tz[0] != '\0')          if (BBS_user_tz[0] != '\0')
# Line 339  int check_user(MYSQL *db, char *username Line 371  int check_user(MYSQL *db, char *username
371                  tzset();                  tzset();
372          }          }
373    
374          return 0;  cleanup:
375            mysql_free_result(rs);
376            mysql_close(db);
377    
378            return ret;
379  }  }
380    
381  int load_user_info(MYSQL *db, long int BBS_uid)  int load_user_info(MYSQL *db, int BBS_uid)
382  {  {
383          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
384          MYSQL_ROW row;          MYSQL_ROW row;
385          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
386          int life;          int life;
387          time_t last_login_dt;          time_t last_login_dt;
388            int ret = 0;
389    
390          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
391                           "SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone "                           "SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone, exp, nickname "
392                           "FROM user_pubinfo WHERE UID = %ld",                           "FROM user_pubinfo WHERE UID = %d",
393                           BBS_uid);                           BBS_uid);
394          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
395          {          {
396                  log_error("Query user_pubinfo error: %s\n", mysql_error(db));                  log_error("Query user_pubinfo error: %s\n", mysql_error(db));
397                  return -1;                  ret = -1;
398                    goto cleanup;
399          }          }
400          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
401          {          {
402                  log_error("Get user_pubinfo data failed\n");                  log_error("Get user_pubinfo data failed\n");
403                  return -1;                  ret = -1;
404                    goto cleanup;
405          }          }
406          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
407          {          {
# Line 371  int load_user_info(MYSQL *db, long int B Line 410  int load_user_info(MYSQL *db, long int B
410    
411                  strncpy(BBS_user_tz, row[2], sizeof(BBS_user_tz) - 1);                  strncpy(BBS_user_tz, row[2], sizeof(BBS_user_tz) - 1);
412                  BBS_user_tz[sizeof(BBS_user_tz) - 1] = '\0';                  BBS_user_tz[sizeof(BBS_user_tz) - 1] = '\0';
413    
414                    BBS_user_exp = atoi(row[3]);
415    
416                    strncpy(BBS_nickname, row[4], sizeof(BBS_nickname));
417                    BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
418          }          }
419          else          else
420          {          {
421                  mysql_free_result(rs);                  ret = -1; // Data not found
422                  return -1; // Data not found                  goto cleanup;
423          }          }
424          mysql_free_result(rs);          mysql_free_result(rs);
425            rs = NULL;
426    
427          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
428                  time(0) - last_login_dt > 60 * 60 * 24 * life)                  time(NULL) - last_login_dt > 60 * 60 * 24 * life)
429          {          {
430                  return -3; // Dead                  ret = -3; // Dead
431                    goto cleanup;
432          }          }
433    
434          if (load_priv(db, &BBS_priv, BBS_uid) != 0)          if (load_priv(db, &BBS_priv, BBS_uid) != 0)
435          {          {
436                  return -1;                  ret = -1;
437                    goto cleanup;
438          }          }
439    
440          return 0;  cleanup:
441            mysql_free_result(rs);
442    
443            return ret;
444  }  }
445    
446  int load_guest_info(MYSQL *db)  int load_guest_info(void)
447  {  {
448            MYSQL *db = NULL;
449            int ret = 0;
450    
451            db = db_open();
452            if (db == NULL)
453            {
454                    ret = -1;
455                    goto cleanup;
456            }
457    
458          strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);          strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);
459          BBS_username[sizeof(BBS_username) - 1] = '\0';          BBS_username[sizeof(BBS_username) - 1] = '\0';
460    
461            BBS_user_exp = 0;
462    
463            strncpy(BBS_nickname, "Guest", sizeof(BBS_nickname));
464            BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
465    
466          if (load_priv(db, &BBS_priv, 0) != 0)          if (load_priv(db, &BBS_priv, 0) != 0)
467          {          {
468                  return -1;                  ret = -1;
469                    goto cleanup;
470          }          }
471    
472          if (user_online_add(db) != 0)          if (user_online_add(db) != 0)
473          {          {
474                  return -1;                  ret = -1;
475                    goto cleanup;
476          }          }
477    
478          BBS_last_access_tm = BBS_login_tm = time(0);          BBS_last_access_tm = BBS_login_tm = time(NULL);
479    
480          return 0;  cleanup:
481            mysql_close(db);
482    
483            return ret;
484  }  }
485    
486  int user_online_add(MYSQL *db)  int user_online_add(MYSQL *db)
# Line 424  int user_online_add(MYSQL *db) Line 494  int user_online_add(MYSQL *db)
494    
495          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
496                           "INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) "                           "INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) "
497                           "VALUES('Telnet_Process_%d', %ld, '%s', NOW(), NOW())",                           "VALUES('Telnet_Process_%d', %d, '%s', NOW(), NOW())",
498                           getpid(), BBS_priv.uid, hostaddr_client);                           getpid(), BBS_priv.uid, hostaddr_client);
499          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
500          {          {
# Line 450  int user_online_del(MYSQL *db) Line 520  int user_online_del(MYSQL *db)
520    
521          return 0;          return 0;
522  }  }
523    
524    int user_online_update(const char *action)
525    {
526            MYSQL *db = NULL;
527            char sql[SQL_BUFFER_LEN];
528    
529            if (strcmp(BBS_current_action, action) == 0) // No change
530            {
531                    return 0;
532            }
533    
534            strncpy(BBS_current_action, action, sizeof(BBS_current_action) - 1);
535            BBS_current_action[sizeof(BBS_current_action) - 1] = '\0';
536    
537            db = db_open();
538            if (db == NULL)
539            {
540                    log_error("db_open() error: %s\n", mysql_error(db));
541                    return -1;
542            }
543    
544            snprintf(sql, sizeof(sql),
545                             "UPDATE user_online SET current_action = '%s', last_tm=NOW() "
546                             "WHERE SID = 'Telnet_Process_%d'",
547                             BBS_current_action, getpid());
548            if (mysql_query(db, sql) != 0)
549            {
550                    log_error("Update user_online error: %s\n", mysql_error(db));
551                    return -2;
552            }
553    
554            mysql_close(db);
555    
556            return 1;
557    }


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

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