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


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

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