/[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.12 by sysadm, Mon Apr 28 04:23:38 2025 UTC Revision 1.76 by sysadm, Sun Dec 21 12:45:47 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    login.c  -  description  /*
3                                                           -------------------   * login
4          begin                : Mon Oct 20 2004   *   - user authentication and online status manager
5          copyright            : (C) 2004 by Leaflet   *
6          email                : leaflet@leafok.com   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7   ***************************************************************************/   */
8    
9  /***************************************************************************  #ifdef HAVE_CONFIG_H
10   *                                                                         *  #include "config.h"
11   *   This program is free software; you can redistribute it and/or modify  *  #endif
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 2 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "bbs.h"  #include "bbs.h"
14  #include "common.h"  #include "common.h"
 #include "io.h"  
15  #include "database.h"  #include "database.h"
16  #include <mysql.h>  #include "io.h"
17    #include "ip_mask.h"
18    #include "log.h"
19    #include "login.h"
20    #include "screen.h"
21    #include "user_priv.h"
22    #include <ctype.h>
23    #include <errno.h>
24    #include <stdlib.h>
25    #include <string.h>
26  #include <regex.h>  #include <regex.h>
27    #include <unistd.h>
28    #include <mysql.h>
29    #include <sys/param.h>
30    
31  void login_fail()  static const int BBS_username_min_len = 3; // common len = 5, special len = 3
32  {  static const int BBS_password_min_len = 5; // legacy len = 5, current len = 6
         char temp[256];  
33    
34          strcpy(temp, app_home_dir);  static const int BBS_allowed_login_failures_within_interval = 10;
35          strcat(temp, "data/login_error.txt");  static const int BBS_login_failures_count_interval = 10; // minutes
36          display_file(temp);  static const int BBS_allowed_login_failures_per_account = 3;
37    
38          sleep(1);  const int BBS_login_retry_times = 3;
 }  
39    
40  int bbs_login()  int bbs_login(void)
41  {  {
42          char username[20], password[20];          char username[BBS_username_max_len + 1];
43          int count, ok;          char password[BBS_password_max_len + 1];
44            int i = 0;
45            int ok = 0;
46            int ret;
47    
48          // Input username          for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++)
         count = 0;  
         ok = 0;  
         while (!ok)  
49          {          {
50                  prints("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "                  prints("\033[1;33m璇疯緭鍏ュ笎鍙穃033[m(璇曠敤璇疯緭鍏\033[1;36mguest\033[m', "
51                             "注册请输入`\033[1;31mnew\033[m'): ");                             "娉ㄥ唽璇疯緭鍏\033[1;31mnew\033[m'): ");
52                  iflush();                  iflush();
53    
54                  str_input(username, 19, DOECHO);                  if (str_input(username, sizeof(username), DOECHO) < 0)
55                  count++;                  {
56                            continue;
57                    }
58    
59                  if (strcmp(username, "guest") == 0)                  if (strcmp(username, "guest") == 0)
60                  {                  {
61                          load_guest_info();                          load_guest_info();
62    
63                          return 0;                          return 0;
64                  }                  }
65    
66                  if (strcmp(username, "new") == 0)                  if (strcmp(username, "new") == 0)
67                  {                  {
68                          if (user_register() == 0)                          display_file(DATA_REGISTER, 1);
69                                  return 0;  
70                          else                          return -1;
                                 return -2;  
71                  }                  }
72    
73                  if (strlen(username) > 0)                  if (username[0] != '\0')
74                  {                  {
75                          // Input password                          prints("\033[1;37m璇疯緭鍏ュ瘑鐮乗033[m: ");
                         prints("\033[1;37m请输入密码\033[m: ");  
76                          iflush();                          iflush();
77    
78                          str_input(password, 19, NOECHO);                          if (str_input(password, sizeof(password), NOECHO) < 0)
79                            {
80                                    continue;
81                            }
82    
83                            ret = check_user(username, password);
84                            if (ret == 2) // Enforce update user agreement
85                            {
86                                    BBS_update_eula = 1;
87                                    ret = 0;
88                            }
89    
90                          ok = (check_user(username, password) == 0);                          ok = (ret == 0);
91                  }                          iflush();
                 if (count >= 3 && !ok)  
                 {  
                         login_fail();  
                         return -1;  
92                  }                  }
93          }          }
94    
95            if (!ok)
96            {
97                    display_file(DATA_LOGIN_ERROR, 1);
98                    return -1;
99            }
100    
101          return 0;          return 0;
102  }  }
103    
104  int check_user(char *username, char *password)  int check_user(const char *username, const char *password)
105  {  {
106          MYSQL *db;          MYSQL *db = NULL;
107          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
108          MYSQL_ROW row;          MYSQL_ROW row;
109          char sql[1024];          char sql[SQL_BUFFER_LEN];
110          long int BBS_uid;          int ret = 0;
111          int ret;          int BBS_uid = 0;
112            char client_addr[IP_ADDR_LEN];
113            int i;
114            int ok = 1;
115            char user_tz_env[BBS_user_tz_max_len + 2];
116    
117            db = db_open();
118            if (db == NULL)
119            {
120                    ret = -1;
121                    goto cleanup;
122            }
123    
124          // Verify format          // Verify format
125          if (ireg("^[A-Za-z0-9_]{3,14}$", username, 0, NULL) != 0 ||          for (i = 0; ok && username[i] != '\0'; i++)
                 ireg("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0)  
126          {          {
127                  prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");                  if (!(isalpha((int)username[i]) || (i > 0 && (isdigit((int)username[i]) || username[i] == '_'))))
128                  iflush();                  {
129                  return 1;                          ok = 0;
130                    }
131            }
132            if (ok && (i < BBS_username_min_len || i > BBS_username_max_len))
133            {
134                    ok = 0;
135            }
136            for (i = 0; ok && password[i] != '\0'; i++)
137            {
138                    if (!isalnum((int)password[i]))
139                    {
140                            ok = 0;
141                    }
142            }
143            if (ok && (i < BBS_password_min_len || i > BBS_password_max_len))
144            {
145                    ok = 0;
146          }          }
147    
148          db = (MYSQL *)db_open();          if (!ok)
         if (db == NULL)  
149          {          {
150                  return -1;                  prints("\033[1;31m鐢ㄦ埛鍚嶆垨瀵嗙爜鏍煎紡閿欒...\033[m\r\n");
151                    ret = 1;
152                    goto cleanup;
153            }
154    
155            // Begin transaction
156            if (mysql_query(db, "SET autocommit=0") != 0)
157            {
158                    log_error("SET autocommit=0 error: %s", mysql_error(db));
159                    ret = -1;
160                    goto cleanup;
161            }
162    
163            if (mysql_query(db, "BEGIN") != 0)
164            {
165                    log_error("Begin transaction error: %s", mysql_error(db));
166                    ret = -1;
167                    goto cleanup;
168          }          }
169    
170          sprintf(sql,          // Failed login attempts from the same source (subnet /24) during certain time period
171                          "select UID,username,p_login from user_list where username='%s' "          strncpy(client_addr, hostaddr_client, sizeof(client_addr) - 1);
172                          "and (password=MD5('%s') or password=SHA2('%s', 256)) and "          client_addr[sizeof(client_addr) - 1] = '\0';
173                          "enable",  
174                          username, password, password);          snprintf(sql, sizeof(sql),
175                             "SELECT COUNT(*) AS err_count FROM user_err_login_log "
176                             "WHERE login_dt >= SUBDATE(NOW(), INTERVAL %d MINUTE) "
177                             "AND login_ip LIKE '%s'",
178                             BBS_login_failures_count_interval,
179                             ip_mask(client_addr, 1, '%'));
180          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
181          {          {
182                  log_error("Query user_list failed\n");                  log_error("Query user_list error: %s", mysql_error(db));
183                  return -1;                  ret = -1;
184                    goto cleanup;
185          }          }
186          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
187          {          {
188                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed");
189                  return -1;                  ret = -1;
190                    goto cleanup;
191          }          }
192          if (row = mysql_fetch_row(rs))          if ((row = mysql_fetch_row(rs)))
193          {          {
194                  BBS_uid = atol(row[0]);                  if (atoi(row[0]) >= BBS_allowed_login_failures_within_interval)
                 strcpy(BBS_username, row[1]);  
                 if (atoi(row[2]) == 0)  
195                  {                  {
196                          mysql_free_result(rs);                          prints("\033[1;31m鏉ユ簮瀛樺湪澶氭澶辫触鐧婚檰灏濊瘯锛岃绋嶅悗鍐嶈瘯锛屾垨浣跨敤Web鏂瑰紡璁块棶\033[m\r\n");
197                          mysql_close(db);                          ret = 1;
198                            goto cleanup;
199                    }
200            }
201            mysql_free_result(rs);
202            rs = NULL;
203    
204                          prints("\033[1;31m您目前无权登陆...\033[m\r\n");          // Failed login attempts against the current username since last successful login
205                          iflush();          snprintf(sql, sizeof(sql),
206                          return 1;                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "
207                             "LEFT JOIN user_list ON user_err_login_log.username = user_list.username "
208                             "LEFT JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID "
209                             "WHERE user_err_login_log.username = '%s' "
210                             "AND (user_err_login_log.login_dt >= user_pubinfo.last_login_dt "
211                             "OR user_pubinfo.last_login_dt IS NULL)",
212                             username);
213            if (mysql_query(db, sql) != 0)
214            {
215                    log_error("Query user_list error: %s", mysql_error(db));
216                    ret = -1;
217                    goto cleanup;
218            }
219            if ((rs = mysql_store_result(db)) == NULL)
220            {
221                    log_error("Get user_list data failed");
222                    ret = -1;
223                    goto cleanup;
224            }
225            if ((row = mysql_fetch_row(rs)))
226            {
227                    if (atoi(row[0]) >= BBS_allowed_login_failures_per_account)
228                    {
229                            prints("\033[1;31m璐︽埛瀛樺湪澶氭澶辫触鐧婚檰灏濊瘯锛岃浣跨敤Web鏂瑰紡鐧诲綍瑙i攣\033[m\r\n");
230                            ret = 1;
231                            goto cleanup;
232                    }
233            }
234            mysql_free_result(rs);
235            rs = NULL;
236    
237            snprintf(sql, sizeof(sql),
238                             "SELECT UID, username, p_login FROM user_list "
239                             "WHERE username = '%s' AND password = SHA2('%s', 256) AND enable",
240                             username, password);
241            if (mysql_query(db, sql) != 0)
242            {
243                    log_error("Query user_list error: %s", mysql_error(db));
244                    ret = -1;
245                    goto cleanup;
246            }
247            if ((rs = mysql_store_result(db)) == NULL)
248            {
249                    log_error("Get user_list data failed");
250                    ret = -1;
251                    goto cleanup;
252            }
253            if ((row = mysql_fetch_row(rs)))
254            {
255                    BBS_uid = atoi(row[0]);
256                    strncpy(BBS_username, row[1], sizeof(BBS_username) - 1);
257                    BBS_username[sizeof(BBS_username) - 1] = '\0';
258                    int p_login = atoi(row[2]);
259    
260                    mysql_free_result(rs);
261                    rs = NULL;
262    
263                    // Add user login log
264                    snprintf(sql, sizeof(sql),
265                                     "INSERT INTO user_login_log(UID, login_dt, login_ip) "
266                                     "VALUES(%d, NOW(), '%s')",
267                                     BBS_uid, hostaddr_client);
268                    if (mysql_query(db, sql) != 0)
269                    {
270                            log_error("Insert into user_login_log error: %s", mysql_error(db));
271                            ret = -1;
272                            goto cleanup;
273                    }
274    
275                    // Commit transaction
276                    if (mysql_query(db, "COMMIT") != 0)
277                    {
278                            log_error("Commit transaction error: %s", mysql_error(db));
279                            ret = -1;
280                            goto cleanup;
281                    }
282    
283                    if (p_login == 0)
284                    {
285                            prints("\033[1;31m鎮ㄧ洰鍓嶆棤鏉冪櫥闄...\033[m\r\n");
286                            ret = 1;
287                            goto cleanup;
288                  }                  }
289          }          }
290          else          else
291          {          {
292                  mysql_free_result(rs);                  mysql_free_result(rs);
293                    rs = NULL;
294    
295                  sprintf(sql,                  snprintf(sql, sizeof(sql),
296                                  "insert into user_err_login_log"                                   "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "
297                                  "(username,password,login_dt,login_ip) values"                                   "VALUES('%s', '%s', NOW(), '%s')",
298                                  "('%s','%s',now(),'%s')",                                   username, password, hostaddr_client);
                                 username, password, hostaddr_client);  
299                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
300                  {                  {
301                          log_error("Insert into user_err_login_log failed\n");                          log_error("Insert into user_err_login_log error: %s", mysql_error(db));
302                          return -1;                          ret = -1;
303                            goto cleanup;
304                  }                  }
305    
306                  mysql_close(db);                  // Commit transaction
307                    if (mysql_query(db, "COMMIT") != 0)
308                    {
309                            log_error("Commit transaction error: %s", mysql_error(db));
310                            ret = -1;
311                            goto cleanup;
312                    }
313    
314                  prints("\033[1;31m错误的用户名或密码...\033[m\r\n");                  prints("\033[1;31m閿欒鐨勭敤鎴峰悕鎴栧瘑鐮...\033[m\r\n");
315                  iflush();                  ret = 1;
316                  return 1;                  goto cleanup;
317            }
318    
319            // Set AUTOCOMMIT = 1
320            if (mysql_query(db, "SET autocommit=1") != 0)
321            {
322                    log_error("SET autocommit=1 error: %s", mysql_error(db));
323                    ret = -1;
324                    goto cleanup;
325          }          }
         mysql_free_result(rs);  
326    
327          ret = load_user_info(db, BBS_uid);          ret = load_user_info(db, BBS_uid);
328    
329          switch (ret)          switch (ret)
330          {          {
331          case 0: // Login successfully          case 0: // Login successfully
332                  return 0;                  if (!SSH_v2 && checklevel2(&BBS_priv, P_MAN_S))
333                    {
334                            prints("\033[1;31m闈炴櫘閫氳处鎴峰繀椤讳娇鐢⊿SH鏂瑰紡鐧诲綍\033[m\r\n");
335                            ret = 1;
336                            goto cleanup;
337                    }
338                  break;                  break;
339          case -1: // Load data error          case -1: // Load data error
340                  prints("\033[1;31m读取用户数据错误...\033[m\r\n");                  prints("\033[1;31m璇诲彇鐢ㄦ埛鏁版嵁閿欒...\033[m\r\n");
341                  iflush();                  ret = -1;
342                  return -1;                  goto cleanup;
343                  break;          case -2: // Enforce update user agreement
344          case -2: // Unused                  if (!SSH_v2 && checklevel2(&BBS_priv, P_MAN_S))
345                  return 0;                  {
346                  break;                          prints("\033[1;31m闈炴櫘閫氳处鎴峰繀椤讳娇鐢⊿SH鏂瑰紡鐧诲綍\033[m\r\n");
347                            ret = 1;
348                            goto cleanup;
349                    }
350                    ret = 2;
351                    goto cleanup;
352          case -3: // Dead          case -3: // Dead
353                  prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");                  prints("\033[1;31m寰堥仐鎲撅紝鎮ㄥ凡缁忔案杩滅寮浜嗘垜浠殑涓栫晫锛乗033[m\r\n");
354                  iflush();                  ret = 1;
355                  return 1;                  goto cleanup;
356          default:          default:
357                  return -2;                  ret = -2;
358                    goto cleanup;
359            }
360    
361            snprintf(sql, sizeof(sql),
362                             "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
363                             "last_login_dt = NOW() WHERE UID = %d",
364                             BBS_uid);
365            if (mysql_query(db, sql) != 0)
366            {
367                    log_error("Update user_pubinfo error: %s", mysql_error(db));
368                    ret = -1;
369                    goto cleanup;
370          }          }
371    
372            if (user_online_add(db) != 0)
373            {
374                    ret = -1;
375                    goto cleanup;
376            }
377    
378            BBS_last_access_tm = BBS_login_tm = time(NULL);
379    
380            // Set user tz to process env
381            if (BBS_user_tz[0] != '\0')
382            {
383                    user_tz_env[0] = ':';
384                    strncpy(user_tz_env + 1, BBS_user_tz, sizeof(user_tz_env) - 2);
385                    user_tz_env[sizeof(user_tz_env) - 1] = '\0';
386    
387                    if (setenv("TZ", user_tz_env, 1) == -1)
388                    {
389                            log_error("setenv(TZ = %s) error %d", user_tz_env, errno);
390                            return -3;
391                    }
392    
393                    tzset();
394            }
395    
396    cleanup:
397            mysql_free_result(rs);
398          mysql_close(db);          mysql_close(db);
399    
400          return 0;          return ret;
401  }  }
402    
403  int load_user_info(MYSQL *db, long int BBS_uid)  int load_user_info(MYSQL *db, int BBS_uid)
404  {  {
405          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
406          MYSQL_ROW row;          MYSQL_ROW row;
407          char sql[1024];          char sql[SQL_BUFFER_LEN];
         long int BBS_auth_uid = 0;  
408          int life;          int life;
409          time_t last_login_dt;          time_t last_login_dt;
410            int ret = 0;
411    
412          sprintf(sql,          snprintf(sql, sizeof(sql),
413                          "select life,UNIX_TIMESTAMP(last_login_dt) "                           "SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone, exp, nickname "
414                          "from user_pubinfo where UID=%ld limit 1",                           "FROM user_pubinfo WHERE UID = %d",
415                          BBS_uid);                           BBS_uid);
416          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
417          {          {
418                  log_error("Query user_pubinfo failed\n");                  log_error("Query user_pubinfo error: %s", mysql_error(db));
419                  return -1;                  ret = -1;
420                    goto cleanup;
421          }          }
422          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
423          {          {
424                  log_error("Get user_pubinfo data failed\n");                  log_error("Get user_pubinfo data failed");
425                  return -1;                  ret = -1;
426                    goto cleanup;
427          }          }
428          if (row = mysql_fetch_row(rs))          if ((row = mysql_fetch_row(rs)))
429          {          {
430                  life = atoi(row[0]);                  life = atoi(row[0]);
431                  last_login_dt = (time_t)atol(row[1]);                  last_login_dt = (time_t)atol(row[1]);
432    
433                    strncpy(BBS_user_tz, row[2], sizeof(BBS_user_tz) - 1);
434                    BBS_user_tz[sizeof(BBS_user_tz) - 1] = '\0';
435    
436                    BBS_user_exp = atoi(row[3]);
437    
438                    strncpy(BBS_nickname, row[4], sizeof(BBS_nickname));
439                    BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
440          }          }
441          else          else
442          {          {
443                  mysql_free_result(rs);                  ret = -1; // Data not found
444                  return (-1); // Data not found                  goto cleanup;
445            }
446            mysql_free_result(rs);
447            rs = NULL;
448    
449            if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
450                    time(NULL) - last_login_dt > 60 * 60 * 24 * life)
451            {
452                    ret = -3; // Dead
453                    goto cleanup;
454            }
455    
456            if (load_priv(db, &BBS_priv, BBS_uid) != 0)
457            {
458                    ret = -1; // Data not found
459                    goto cleanup;
460          }          }
461    
462            if (last_login_dt < BBS_eula_tm)
463            {
464                    ret = -2; // require update agreement first
465                    goto cleanup;
466            }
467    
468    cleanup:
469          mysql_free_result(rs);          mysql_free_result(rs);
470    
471          if (time(0) - last_login_dt >= 60 * 60 * 24 * life)          return ret;
472    }
473    
474    int load_guest_info(void)
475    {
476            MYSQL *db = NULL;
477            int ret = 0;
478    
479            db = db_open();
480            if (db == NULL)
481            {
482                    ret = -1;
483                    goto cleanup;
484            }
485    
486            strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);
487            BBS_username[sizeof(BBS_username) - 1] = '\0';
488    
489            BBS_user_exp = 0;
490    
491            strncpy(BBS_nickname, "Guest", sizeof(BBS_nickname));
492            BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
493    
494            if (load_priv(db, &BBS_priv, 0) != 0)
495            {
496                    ret = -1;
497                    goto cleanup;
498            }
499    
500            if (user_online_add(db) != 0)
501            {
502                    ret = -1;
503                    goto cleanup;
504            }
505    
506            BBS_last_access_tm = BBS_login_tm = time(NULL);
507    
508    cleanup:
509            mysql_close(db);
510    
511            return ret;
512    }
513    
514    int user_online_add(MYSQL *db)
515    {
516            char sql[SQL_BUFFER_LEN];
517    
518            snprintf(sql, sizeof(sql),
519                             "INSERT INTO visit_log(dt, IP) VALUES(NOW(), '%s')",
520                             hostaddr_client);
521            if (mysql_query(db, sql) != 0)
522          {          {
523                  return (-3); // Dead                  log_error("Add visit log error: %s", mysql_error(db));
524                    return -1;
525          }          }
526    
527          sprintf(sql,          if (user_online_del(db) != 0)
528                          "insert into user_login_log"          {
529                          "(uid,login_dt,login_ip) values(%ld"                  return -2;
530                          ",now(),'%s')",          }
531                          BBS_uid, hostaddr_client);  
532            snprintf(sql, sizeof(sql),
533                             "INSERT INTO user_online(SID, UID, ip, current_action, login_tm, last_tm) "
534                             "VALUES('Telnet_Process_%d', %d, '%s', 'LOGIN', NOW(), NOW())",
535                             getpid(), BBS_priv.uid, hostaddr_client);
536            if (mysql_query(db, sql) != 0)
537            {
538                    log_error("Add user_online error: %s", mysql_error(db));
539                    return -3;
540            }
541    
542            return 0;
543    }
544    
545    int user_online_del(MYSQL *db)
546    {
547            char sql[SQL_BUFFER_LEN];
548    
549            snprintf(sql, sizeof(sql),
550                             "DELETE FROM user_online WHERE SID = 'Telnet_Process_%d'",
551                             getpid());
552          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
553          {          {
554                  log_error("Insert into user_login_log failed\n");                  log_error("Delete user_online error: %s", mysql_error(db));
555                  return -1;                  return -1;
556          }          }
557    
558          load_priv(db, &BBS_priv, BBS_uid);          return 0;
559    }
560    
561          BBS_last_access_tm = BBS_login_tm = time(0);  int user_online_exp(MYSQL *db)
562          BBS_last_sub_tm = time(0) - 60;  {
563            char sql[SQL_BUFFER_LEN];
564    
565          sprintf(sql,          // +1 exp for every 5 minutes online since last logout
566                          "update user_pubinfo set visit_count=visit_count+1,"          // but at most 24 hours worth of exp can be gained in Telnet session
567                          "last_login_dt=now() where uid=%ld",          snprintf(sql, sizeof(sql),
568                          BBS_uid);                           "UPDATE user_pubinfo SET exp = exp + FLOOR(LEAST(TIMESTAMPDIFF("
569                             "SECOND, GREATEST(last_login_dt, IF(last_logout_dt IS NULL, last_login_dt, last_logout_dt)), NOW()"
570                             ") / 60 / 5, 12 * 24)), last_logout_dt = NOW() "
571                             "WHERE UID = %d",
572                             BBS_priv.uid);
573          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
574          {          {
575                  log_error("Update user_pubinfo failed\n");                  log_error("Update user_pubinfo error: %s", mysql_error(db));
576                  return -1;                  return -1;
577          }          }
578    
579          return 0;          return 0;
580  }  }
581    
582  int load_guest_info(MYSQL *db, long int BBS_uid)  int user_online_update(const char *action)
583  {  {
584          MYSQL_RES *rs;          MYSQL *db = NULL;
585          MYSQL_ROW row;          char sql[SQL_BUFFER_LEN];
586    
587            if ((action == NULL || strcmp(BBS_current_action, action) == 0) &&
588                    time(NULL) - BBS_current_action_tm < BBS_current_action_refresh_interval) // No change
589            {
590                    return 0;
591            }
592    
593            if (action != NULL)
594            {
595                    strncpy(BBS_current_action, action, sizeof(BBS_current_action) - 1);
596                    BBS_current_action[sizeof(BBS_current_action) - 1] = '\0';
597            }
598    
599          db = (MYSQL *)db_open();          BBS_current_action_tm = time(NULL);
600    
601            db = db_open();
602          if (db == NULL)          if (db == NULL)
603          {          {
604                    log_error("db_open() error: %s", mysql_error(db));
605                  return -1;                  return -1;
606          }          }
607    
608          strcpy(BBS_username, "guest");          snprintf(sql, sizeof(sql),
609                             "UPDATE user_online SET current_action = '%s', last_tm = NOW() "
610                             "WHERE SID = 'Telnet_Process_%d'",
611                             BBS_current_action, getpid());
612            if (mysql_query(db, sql) != 0)
613            {
614                    log_error("Update user_online error: %s", mysql_error(db));
615                    return -2;
616            }
617    
618            mysql_close(db);
619    
620            return 1;
621    }
622    
623    int user_update_agreement(void)
624    {
625            MYSQL *db = NULL;
626            char sql[SQL_BUFFER_LEN];
627            int ch;
628            int ret = 0;
629    
630            clearscr();
631            moveto(2, 1);
632            prints("灏婃暚鐨勭敤鎴凤細");
633            moveto(3, 1);
634            prints("    鏈珯鐨勩婄敤鎴疯鍙崗璁嬪凡鏇存柊锛屾偍蹇呴』鍦ㄤ粩缁嗛槄璇诲苟鎺ュ彈鍚庯紝鎵嶈兘缁х画浣跨敤鏈珯鎻愪緵鐨勬湇鍔°");
635            press_any_key();
636    
637            clearscr();
638            display_file(DATA_EULA, 1);
639    
640            while (!SYS_server_exit)
641            {
642                    clearscr();
643                    moveto(2, 1);
644    
645          load_priv(db, &BBS_priv, 0, 0, S_NONE);                  ch = press_any_key_ex("鎮ㄦ槸鍚︽帴鍙楁湰绔欑殑銆婄敤鎴疯鍙崗璁? (A)鎺ュ彈, (D)鎷掔粷, (R)鍐嶇湅鐪嬪崗璁 [D]", 60);
646                    switch (toupper(ch))
647                    {
648                    case KEY_NULL:
649                            return -1;
650                    case KEY_TIMEOUT:
651                    case CR:
652                    case 'D':
653                            moveto(3, 1);
654                            prints("鎮ㄥ凡鎷掔粷銆婄敤鎴疯鍙崗璁嬶紝鍐嶈锛");
655                            press_any_key();
656                            return 0;
657                    case 'R':
658                            clearscr();
659                            display_file(DATA_EULA, 1);
660                            continue;
661                    case 'A':
662                            db = db_open();
663                            if (db == NULL)
664                            {
665                                    ret = -1;
666                                    goto cleanup;
667                            }
668    
669                            snprintf(sql, sizeof(sql),
670                                             "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
671                                             "last_login_dt = NOW() WHERE UID = %d",
672                                             BBS_priv.uid);
673                            if (mysql_query(db, sql) != 0)
674                            {
675                                    log_error("Update user_pubinfo error: %s", mysql_error(db));
676                                    ret = -1;
677                                    goto cleanup;
678                            }
679    
680          BBS_last_access_tm = BBS_login_tm = time(0);                          mysql_close(db);
681          BBS_last_sub_tm = time(0) - 60;                          db = NULL;
682    
683                            moveto(3, 1);
684                            prints("鎮ㄥ凡鎺ュ彈銆婄敤鎴疯鍙崗璁嬶紝璇烽噸鏂扮櫥闄嗐");
685                            press_any_key();
686                            return 1;
687                    default:
688                            continue;
689                    }
690    
691                    break;
692            }
693    
694    cleanup:
695          mysql_close(db);          mysql_close(db);
696    
697          return 0;          return ret;
698  }  }


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

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