/[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.5 by sysadm, Sat Oct 23 18:41:41 2004 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      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   ***************************************************************************/   */
   
 /***************************************************************************  
  *                                                                         *  
  *   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 2 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "bbs.h"  #include "bbs.h"
10  #include "common.h"  #include "common.h"
11    #include "database.h"
12  #include "io.h"  #include "io.h"
13  #include <mysql.h>  #include "ip_mask.h"
14    #include "log.h"
15    #include "login.h"
16    #include "screen.h"
17    #include "user_priv.h"
18    #include <ctype.h>
19    #include <errno.h>
20    #include <stdlib.h>
21    #include <string.h>
22  #include <regex.h>  #include <regex.h>
23    #include <unistd.h>
24    #include <mysql/mysql.h>
25    #include <sys/param.h>
26    
27  void  static const int BBS_username_min_len = 3; // common len = 5, special len = 3
28  login_fail ()  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)
37    {
38            char username[BBS_username_max_len + 1];
39            char password[BBS_password_max_len + 1];
40            int i = 0;
41            int ok = 0;
42    
43            for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++)
44            {
45                    prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', "
46                               "注册请输入`\033[1;31mnew\033[m'): ");
47                    iflush();
48    
49                    if (str_input(username, sizeof(username), DOECHO) < 0)
50                    {
51                            continue;
52                    }
53    
54                    if (strcmp(username, "guest") == 0)
55                    {
56                            load_guest_info();
57    
58                            return 0;
59                    }
60    
61                    if (strcmp(username, "new") == 0)
62                    {
63                            display_file(DATA_REGISTER, 1);
64    
65                            return -1;
66                    }
67    
68                    if (username[0] != '\0')
69                    {
70                            prints("\033[1;37m请输入密码\033[m: ");
71                            iflush();
72    
73                            if (str_input(password, sizeof(password), NOECHO) < 0)
74                            {
75                                    continue;
76                            }
77    
78                            ok = (check_user(username, password) == 0);
79                            iflush();
80                    }
81            }
82    
83            if (!ok)
84            {
85                    display_file(DATA_LOGIN_ERROR, 1);
86                    return -1;
87            }
88    
89            log_common("User \"%s\"(%ld) login from %s:%d\n",
90                               BBS_username, BBS_priv.uid, hostaddr_client, port_client);
91    
92            return 0;
93    }
94    
95    int check_user(const char *username, const char *password)
96  {  {
97    char temp[256];          MYSQL *db = NULL;
98            MYSQL_RES *rs = NULL;
99            MYSQL_ROW row;
100            char sql[SQL_BUFFER_LEN];
101            int ret = 0;
102            int BBS_uid = 0;
103            char client_addr[IP_ADDR_LEN];
104            int i;
105            int ok = 1;
106            char user_tz_env[BBS_user_tz_max_len + 2];
107    
108            db = db_open();
109            if (db == NULL)
110            {
111                    ret = -1;
112                    goto cleanup;
113            }
114    
115            // Verify format
116            for (i = 0; ok && username[i] != '\0'; i++)
117            {
118                    if (!(isalpha(username[i]) || (i > 0 && (isdigit(username[i]) || username[i] == '_'))))
119                    {
120                            ok = 0;
121                    }
122            }
123            if (ok && (i < BBS_username_min_len || i > BBS_username_max_len))
124            {
125                    ok = 0;
126            }
127            for (i = 0; ok && password[i] != '\0'; i++)
128            {
129                    if (!isalnum(password[i]))
130                    {
131                            ok = 0;
132                    }
133            }
134            if (ok && (i < BBS_password_min_len || i > BBS_password_max_len))
135            {
136                    ok = 0;
137            }
138    
139            if (!ok)
140            {
141                    prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");
142                    ret = 1;
143                    goto cleanup;
144            }
145    
146            // Begin transaction
147            if (mysql_query(db, "SET autocommit=0") != 0)
148            {
149                    log_error("SET autocommit=0 error: %s\n", mysql_error(db));
150                    ret = -1;
151                    goto cleanup;
152            }
153    
154            if (mysql_query(db, "BEGIN") != 0)
155            {
156                    log_error("Begin transaction error: %s\n", mysql_error(db));
157                    ret = -1;
158                    goto cleanup;
159            }
160    
161            // Failed login attempts from the same source (subnet /24) during certain time period
162            strncpy(client_addr, hostaddr_client, sizeof(client_addr) - 1);
163            client_addr[sizeof(client_addr) - 1] = '\0';
164    
165            snprintf(sql, sizeof(sql),
166                             "SELECT COUNT(*) AS err_count FROM user_err_login_log "
167                             "WHERE login_dt >= SUBDATE(NOW(), INTERVAL %d MINUTE) "
168                             "AND login_ip LIKE '%s'",
169                             BBS_login_failures_count_interval,
170                             ip_mask(client_addr, 1, '%'));
171            if (mysql_query(db, sql) != 0)
172            {
173                    log_error("Query user_list error: %s\n", mysql_error(db));
174                    ret = -1;
175                    goto cleanup;
176            }
177            if ((rs = mysql_store_result(db)) == NULL)
178            {
179                    log_error("Get user_list data failed\n");
180                    ret = -1;
181                    goto cleanup;
182            }
183            if ((row = mysql_fetch_row(rs)))
184            {
185                    if (atoi(row[0]) >= BBS_allowed_login_failures_within_interval)
186                    {
187                            prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试,或使用Web方式访问\033[m\r\n");
188                            ret = 1;
189                            goto cleanup;
190                    }
191            }
192            mysql_free_result(rs);
193            rs = NULL;
194    
195            // Failed login attempts against the current username since last successful login
196            snprintf(sql, sizeof(sql),
197                             "SELECT COUNT(*) AS err_count FROM user_err_login_log "
198                             "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);
204            if (mysql_query(db, sql) != 0)
205            {
206                    log_error("Query user_list error: %s\n", mysql_error(db));
207                    ret = -1;
208                    goto cleanup;
209            }
210            if ((rs = mysql_store_result(db)) == NULL)
211            {
212                    log_error("Get user_list data failed\n");
213                    ret = -1;
214                    goto cleanup;
215            }
216            if ((row = mysql_fetch_row(rs)))
217            {
218                    if (atoi(row[0]) >= BBS_allowed_login_failures_per_account)
219                    {
220                            prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录解锁\033[m\r\n");
221                            ret = 1;
222                            goto cleanup;
223                    }
224            }
225            mysql_free_result(rs);
226            rs = NULL;
227    
228            snprintf(sql, sizeof(sql),
229                             "SELECT UID, username, p_login FROM user_list "
230                             "WHERE username = '%s' AND password = SHA2('%s', 256) AND enable",
231                             username, password);
232            if (mysql_query(db, sql) != 0)
233            {
234                    log_error("Query user_list error: %s\n", mysql_error(db));
235                    ret = -1;
236                    goto cleanup;
237            }
238            if ((rs = mysql_store_result(db)) == NULL)
239            {
240                    log_error("Get user_list data failed\n");
241                    ret = -1;
242                    goto cleanup;
243            }
244            if ((row = mysql_fetch_row(rs)))
245            {
246                    BBS_uid = atoi(row[0]);
247                    strncpy(BBS_username, row[1], sizeof(BBS_username) - 1);
248                    BBS_username[sizeof(BBS_username) - 1] = '\0';
249                    int p_login = atoi(row[2]);
250    
251                    mysql_free_result(rs);
252                    rs = NULL;
253    
254                    // Add user login log
255                    snprintf(sql, sizeof(sql),
256                                     "INSERT INTO user_login_log(UID, login_dt, login_ip) "
257                                     "VALUES(%d, NOW(), '%s')",
258                                     BBS_uid, hostaddr_client);
259                    if (mysql_query(db, sql) != 0)
260                    {
261                            log_error("Insert into user_login_log error: %s\n", mysql_error(db));
262                            ret = -1;
263                            goto cleanup;
264                    }
265    
266                    // Commit transaction
267                    if (mysql_query(db, "COMMIT") != 0)
268                    {
269                            log_error("Commit transaction error: %s\n", mysql_error(db));
270                            ret = -1;
271                            goto cleanup;
272                    }
273    
274                    if (p_login == 0)
275                    {
276                            prints("\033[1;31m您目前无权登陆...\033[m\r\n");
277                            ret = 1;
278                            goto cleanup;
279                    }
280            }
281            else
282            {
283                    mysql_free_result(rs);
284                    rs = NULL;
285    
286                    snprintf(sql, sizeof(sql),
287                                     "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "
288                                     "VALUES('%s', '%s', NOW(), '%s')",
289                                     username, password, hostaddr_client);
290                    if (mysql_query(db, sql) != 0)
291                    {
292                            log_error("Insert into user_err_login_log error: %s\n", mysql_error(db));
293                            ret = -1;
294                            goto cleanup;
295                    }
296    
297                    // Commit transaction
298                    if (mysql_query(db, "COMMIT") != 0)
299                    {
300                            log_error("Commit transaction error: %s\n", mysql_error(db));
301                            ret = -1;
302                            goto cleanup;
303                    }
304    
305                    prints("\033[1;31m错误的用户名或密码...\033[m\r\n");
306                    ret = 1;
307                    goto cleanup;
308            }
309    
310            // Set AUTOCOMMIT = 1
311            if (mysql_query(db, "SET autocommit=1") != 0)
312            {
313                    log_error("SET autocommit=1 error: %s\n", mysql_error(db));
314                    ret = -1;
315                    goto cleanup;
316            }
317    
318            ret = load_user_info(db, BBS_uid);
319    
320    strcpy (temp, app_home_dir);          switch (ret)
321    strcat (temp, "data/login_error.txt");          {
322    display_file (temp);          case 0: // Login successfully
323                    break;
324            case -1: // Load data error
325                    prints("\033[1;31m读取用户数据错误...\033[m\r\n");
326                    ret = -1;
327                    goto cleanup;
328            case -2: // Unused
329                    prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");
330                    ret = 1;
331                    goto cleanup;
332            case -3: // Dead
333                    prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
334                    ret = 1;
335                    goto cleanup;
336            default:
337                    ret = -2;
338                    goto cleanup;
339            }
340    
341            snprintf(sql, sizeof(sql),
342                             "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
343                             "last_login_dt = NOW() WHERE UID = %d",
344                             BBS_uid);
345            if (mysql_query(db, sql) != 0)
346            {
347                    log_error("Update user_pubinfo error: %s\n", mysql_error(db));
348                    ret = -1;
349                    goto cleanup;
350            }
351    
352            if (user_online_add(db) != 0)
353            {
354                    ret = -1;
355                    goto cleanup;
356            }
357    
358    sleep (1);          BBS_last_access_tm = BBS_login_tm = time(NULL);
359    
360            // Set user tz to process env
361            if (BBS_user_tz[0] != '\0')
362            {
363                    user_tz_env[0] = ':';
364                    strncpy(user_tz_env + 1, BBS_user_tz, sizeof(user_tz_env) - 2);
365                    user_tz_env[sizeof(user_tz_env) - 1] = '\0';
366    
367                    if (setenv("TZ", user_tz_env, 1) == -1)
368                    {
369                            log_error("setenv(TZ = %s) error %d\n", user_tz_env, errno);
370                            return -3;
371                    }
372    
373                    tzset();
374            }
375    
376    cleanup:
377            mysql_free_result(rs);
378            mysql_close(db);
379    
380            return ret;
381  }  }
382    
383  int  int load_user_info(MYSQL *db, int BBS_uid)
 bbs_login ()  
384  {  {
385    char username[20], password[20];          MYSQL_RES *rs = NULL;
386    int count, ok;          MYSQL_ROW row;
387            char sql[SQL_BUFFER_LEN];
388            int life;
389            time_t last_login_dt;
390            int ret = 0;
391    
392            snprintf(sql, sizeof(sql),
393                             "SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone, exp, nickname "
394                             "FROM user_pubinfo WHERE UID = %d",
395                             BBS_uid);
396            if (mysql_query(db, sql) != 0)
397            {
398                    log_error("Query user_pubinfo error: %s\n", mysql_error(db));
399                    ret = -1;
400                    goto cleanup;
401            }
402            if ((rs = mysql_store_result(db)) == NULL)
403            {
404                    log_error("Get user_pubinfo data failed\n");
405                    ret = -1;
406                    goto cleanup;
407            }
408            if ((row = mysql_fetch_row(rs)))
409            {
410                    life = atoi(row[0]);
411                    last_login_dt = (time_t)atol(row[1]);
412    
413    //Input username                  strncpy(BBS_user_tz, row[2], sizeof(BBS_user_tz) - 1);
414    count = 0;                  BBS_user_tz[sizeof(BBS_user_tz) - 1] = '\0';
   ok = 0;  
   while (!ok)  
     {  
       prints  
         ("\033[1;33mʺ\033[m( `\033[1;36mguest\033[m', "  
          "ע`\033[1;31mnew\033[m'): ");  
       iflush ();  
415    
416        str_input (username, 19, 0);                  BBS_user_exp = atoi(row[3]);
       count++;  
417    
418        if (strcmp (username, "guest") == 0)                  strncpy(BBS_nickname, row[4], sizeof(BBS_nickname));
419                    BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
420            }
421            else
422          {          {
423            load_guest_info ();                  ret = -1; // Data not found
424            return 0;                  goto cleanup;
425          }          }
426            mysql_free_result(rs);
427            rs = NULL;
428    
429        if (strcmp (username, "new") == 0)          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
430                    time(NULL) - last_login_dt > 60 * 60 * 24 * life)
431          {          {
432            if (user_register () == 0)                  ret = -3; // Dead
433              return 0;                  goto cleanup;
           else  
            return -2;  
434          }          }
435    
436        if (strlen (username) > 0)          if (load_priv(db, &BBS_priv, BBS_uid) != 0)
437          {          {
438            //Input password                  ret = -1;
439            prints ("\033[1;37m\033[m: ");                  goto cleanup;
440            iflush ();          }
441    
442    cleanup:
443            mysql_free_result(rs);
444    
445            return ret;
446    }
447    
448            str_input (password, 19, 1);  int load_guest_info(void)
449    {
450            MYSQL *db = NULL;
451            int ret = 0;
452    
453            ok = (check_user (username, password) == 0);          db = db_open();
454            if (db == NULL)
455            {
456                    ret = -1;
457                    goto cleanup;
458          }          }
459        if (count >= 3 && !ok)  
460            strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);
461            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)
469          {          {
470            login_fail ();                  ret = -1;
471            return -1;                  goto cleanup;
472          }          }
     }  
473    
474    return 0;          if (user_online_add(db) != 0)
475            {
476                    ret = -1;
477                    goto cleanup;
478            }
479    
480            BBS_last_access_tm = BBS_login_tm = time(NULL);
481    
482    cleanup:
483            mysql_close(db);
484    
485            return ret;
486  }  }
487    
488  int  int user_online_add(MYSQL *db)
 check_user (char *username, char *password)  
489  {  {
490    MYSQL *db;          char sql[SQL_BUFFER_LEN];
491    MYSQL_RES *rs;  
492    MYSQL_ROW row;          snprintf(sql, sizeof(sql),
493    char sql[1024];                           "INSERT INTO visit_log(dt, IP) VALUES(NOW(), '%s')",
494    long int BBS_uid;                           hostaddr_client);
495    int ret;          if (mysql_query(db, sql) != 0)
496            {
497    //Verify format                  log_error("Add visit log error: %s\n", mysql_error(db));
498    if (ireg ("^[A-Za-z0-9_]{3,14}$", username, 0, NULL) != 0 ||                  return -1;
499        ireg ("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0)          }
500      {  
501        prints ("\033[1;31mûʽ...\033[m\r\n");          if (user_online_del(db) != 0)
502        iflush ();          {
503        return 1;                  return -2;
504      }          }
   
   db = (MYSQL *) db_open ();  
   if (db == NULL)  
     {  
       return -1;  
     }  
   
   sprintf (sql,  
            "select UID,p_login from user_list where username='%s' "  
            "and (password=MD5('%s') or password=PASSWORD('%s')) and "  
            "enable", username, password, password);  
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Query user_list failed\n");  
       return -1;  
     }  
   if ((rs = mysql_store_result (db)) == NULL)  
     {  
       log_error ("Get user_list data failed\n");  
       return -1;  
     }  
   if (row = mysql_fetch_row (rs))  
     {  
       BBS_uid = atol (row[0]);  
       if (atoi (row[1]) == 0)  
         {  
           mysql_free_result (rs);  
           mysql_close (db);  
   
           prints ("\033[1;31mĿǰȨ½...\033[m\r\n");  
           iflush ();  
           return 1;  
         }  
     }  
   else  
     {  
       mysql_free_result (rs);  
   
       sprintf (sql,  
                "insert delayed into user_err_login_log"  
                "(username,password,login_dt,login_ip) values"  
                "('%s','%s',now(),'%s')", username, password, hostaddr_client);  
       if (mysql_query (db, sql) != 0)  
         {  
           log_error ("Insert into user_err_login_log failed\n");  
           return -1;  
         }  
   
       mysql_close (db);  
   
       prints ("\033[1;31mû...\033[m\r\n");  
       iflush ();  
       return 1;  
     }  
   mysql_free_result (rs);  
   
   BBS_passwd_complex = verify_pass_complexity (password, username, 6);  
   
   ret = load_user_info (db, BBS_uid);  
   
   switch (ret)  
     {  
     case 0:                     //Login successfully  
       return 0;  
       break;  
     case -1:                    //Load data error  
       prints ("\033[1;31mȡûݴ...\033[m\r\n");  
       iflush ();  
       return -1;  
       break;  
     case -2:                    //Unused  
       return 0;  
       break;  
     case -3:                    //Dead  
       prints ("\033[1;31mźѾԶ뿪ǵ磡\033[m\r\n");  
       iflush ();  
       return 1;  
     default:  
       return -2;  
     }  
505    
506    mysql_close (db);          snprintf(sql, sizeof(sql),
507                             "INSERT INTO user_online(SID, UID, ip, current_action, login_tm, last_tm) "
508                             "VALUES('Telnet_Process_%d', %d, '%s', 'LOGIN', NOW(), NOW())",
509                             getpid(), BBS_priv.uid, hostaddr_client);
510            if (mysql_query(db, sql) != 0)
511            {
512                    log_error("Add user_online error: %s\n", mysql_error(db));
513                    return -3;
514            }
515    
516    return 0;          return 0;
517  }  }
518    
519  int  int user_online_del(MYSQL *db)
 load_user_info (MYSQL * db, long int BBS_uid)  
520  {  {
521    MYSQL_RES *rs;          char sql[SQL_BUFFER_LEN];
522    MYSQL_ROW row;  
523    char sql[1024];          snprintf(sql, sizeof(sql),
524    long int BBS_auth_uid = 0;                           "DELETE FROM user_online WHERE SID = 'Telnet_Process_%d'",
525    int life;                           getpid());
526    time_t last_login_dt;          if (mysql_query(db, sql) != 0)
527            {
528    sprintf (sql,                  log_error("Delete user_online error: %s\n", mysql_error(db));
529             "select life,UNIX_TIMESTAMP(last_login_dt) "                  return -1;
530             "from user_pubinfo where UID=%ld limit 1", BBS_uid);          }
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Query user_pubinfo failed\n");  
       return -1;  
     }  
   if ((rs = mysql_store_result (db)) == NULL)  
     {  
       log_error ("Get user_pubinfo data failed\n");  
       return -1;  
     }  
   if (row = mysql_fetch_row (rs))  
     {  
       life = atoi (row[0]);  
       last_login_dt = (time_t) atol (row[1]);  
     }  
   else  
     {  
       mysql_free_result (rs);  
       return (-1);              //Data not found  
     }  
   mysql_free_result (rs);  
   
   if (time (0) - last_login_dt >= 60 * 60 * 24 * life)  
     {  
       return (-3);              //Dead  
     }  
   
   sprintf (sql,  
            "select AUID from user_auth where UID=%ld"  
            " and enable and expire_dt>now()", BBS_uid);  
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Query user_auth failed\n");  
       return -1;  
     }  
   if ((rs = mysql_store_result (db)) == NULL)  
     {  
       log_error ("Get user_auth data failed\n");  
       return -1;  
     }  
   if (row = mysql_fetch_row (rs))  
     {  
       BBS_auth_uid = atol (row[0]);  
     }  
   mysql_free_result (rs);  
   
   sprintf (sql,  
            "insert delayed into user_login_log"  
            "(uid,login_dt,login_ip) values(%ld"  
            ",now(),'%s')", BBS_uid, hostaddr_client);  
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Insert into user_login_log failed\n");  
       return -1;  
     }  
   
   load_priv (db, &BBS_priv, BBS_uid, BBS_auth_uid,  
              (!BBS_passwd_complex ? S_MAN_M : S_NONE) |  
              (BBS_auth_uid ? S_NONE : S_MAIL));  
   
   BBS_last_access_tm = BBS_login_tm = time (0);  
   BBS_last_sub_tm = time (0) - 60;  
   
   sprintf (sql,  
            "update user_pubinfo set visit_count=visit_count+1,"  
            "last_login_dt=now() where uid=%ld", BBS_uid);  
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Update user_pubinfo failed\n");  
       return -1;  
     }  
531    
532    return 0;          return 0;
533  }  }
534    
535  int  int user_online_exp(MYSQL *db)
 load_guest_info (MYSQL * db, long int BBS_uid)  
536  {  {
537    MYSQL_RES *rs;          char sql[SQL_BUFFER_LEN];
   MYSQL_ROW row;  
538    
539    db = (MYSQL *) db_open ();          // +1 exp for every 5 minutes online since last logout
540    if (db == NULL)          // but at most 24 hours worth of exp can be gained in Telnet session
541      {          snprintf(sql, sizeof(sql),
542        return -1;                           "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    load_priv (db, &BBS_priv, 0, 0, S_NONE);          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    BBS_last_access_tm = BBS_login_tm = time (0);          if (action != NULL)
568    BBS_last_sub_tm = time (0) - 60;          {
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);          mysql_close(db);
593    
594    return 0;          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