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


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

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