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


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

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