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


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

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