/[LeafOK_CVS]/lbbs/src/login.c
ViewVC logotype

Diff of /lbbs/src/login.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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


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

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