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


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

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