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


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

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