/[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.21 by sysadm, Tue May 6 12:10:22 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"
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    
119            // Verify format
120            if (ireg("^[A-Za-z][A-Za-z0-9]{2,11}$", username, 0, NULL) != 0 ||
121                    ireg("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0)
122            {
123                    prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");
124                    iflush();
125                    return 1;
126            }
127    
128            // Begin transaction
129            if (mysql_query(db, "SET autocommit=0") != 0)
130            {
131                    log_error("SET autocommit=0 failed\n");
132                    return -1;
133            }
134    
135            if (mysql_query(db, "BEGIN") != 0)
136            {
137                    log_error("Begin transaction failed\n");
138                    return -1;
139            }
140    
141            snprintf(sql, sizeof(sql),
142                            "SELECT UID, username, p_login FROM user_list "
143                            "WHERE username = '%s' AND password = SHA2('%s', 256) AND enable",
144                            username, password);
145            if (mysql_query(db, sql) != 0)
146            {
147                    log_error("Query user_list failed\n");
148                    return -1;
149            }
150            if ((rs = mysql_store_result(db)) == NULL)
151            {
152                    log_error("Get user_list data failed\n");
153                    return -1;
154            }
155            if ((row = mysql_fetch_row(rs)))
156            {
157                    BBS_uid = atol(row[0]);
158                    strncpy(BBS_username, row[1], sizeof(BBS_username) - 1);
159                    BBS_username[sizeof(BBS_username) - 1] = '\0';
160                    int p_login = atoi(row[2]);
161    
162                    mysql_free_result(rs);
163    
164                    // Add user login log
165                    snprintf(sql, sizeof(sql),
166                                    "INSERT INTO user_login_log(UID, login_dt, login_ip) "
167                                    "VALUES(%ld, NOW(), '%s')",
168                                    BBS_uid, hostaddr_client);
169                    if (mysql_query(db, sql) != 0)
170                    {
171                            log_error("Insert into user_login_log failed\n");
172                            return -1;
173                    }
174    
175                    // Commit transaction
176                    if (mysql_query(db, "COMMIT") != 0)
177                    {
178                            log_error("Commit transaction failed\n");
179                            return -1;
180                    }
181    
182                    if (p_login == 0)
183                    {
184                            mysql_free_result(rs);
185    
186                            prints("\033[1;31m您目前无权登陆...\033[m\r\n");
187                            iflush();
188                            return 1;
189                    }
190            }
191            else
192            {
193                    mysql_free_result(rs);
194    
195                    snprintf(sql, sizeof(sql),
196                                    "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "
197                                    "VALUES('%s', '%s', NOW(), '%s')",
198                                    username, password, hostaddr_client);
199                    if (mysql_query(db, sql) != 0)
200                    {
201                            log_error("Insert into user_err_login_log failed\n");
202                            return -1;
203                    }
204    
205                    // Commit transaction
206                    if (mysql_query(db, "COMMIT") != 0)
207                    {
208                            log_error("Commit transaction failed\n");
209                            return -1;
210                    }
211    
212                    prints("\033[1;31m错误的用户名或密码...\033[m\r\n");
213                    iflush();
214                    return 1;
215            }
216    
217            // Set AUTOCOMMIT = 1
218            if (mysql_query(db, "SET autocommit=1") != 0)
219            {
220                    log_error("SET autocommit=1 failed\n");
221                    return -1;
222            }
223    
224            ret = load_user_info(db, BBS_uid);
225    
226            switch (ret)
227            {
228            case 0: // Login successfully
229                    break;
230            case -1: // Load data error
231                    prints("\033[1;31m读取用户数据错误...\033[m\r\n");
232                    iflush();
233                    return -1;
234            case -2: // Unused
235                    prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");
236                    iflush();
237                    return 1;
238            case -3: // Dead
239                    prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
240                    iflush();
241                    return 1;
242            default:
243                    return -2;
244            }
245    
246            snprintf(sql, sizeof(sql),
247                            "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
248                            "last_login_dt = NOW() WHERE UID = %ld",
249                            BBS_uid);
250            if (mysql_query(db, sql) != 0)
251            {
252                    log_error("Update user_pubinfo failed\n");
253                    return -1;
254            }
255    
256    //Input username          if (user_online_add(db) != 0)
257    count = 0;          {
258    ok = 0;                  return -1;
259    while (!ok)          }
     {  
       prints  
         ("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "  
          "注册请输入`\033[1;31mnew\033[m'): ");  
       iflush ();  
260    
261        str_input (username, 19, 0);          BBS_last_access_tm = BBS_login_tm = time(0);
       count++;  
262    
263        if (strcmp (username, "guest") == 0)          return 0;
264          return 1;  }
265    
266        if (strlen (username) > 0)  int load_user_info(MYSQL *db, long int BBS_uid)
267    {
268            MYSQL_RES *rs;
269            MYSQL_ROW row;
270            char sql[SQL_BUFFER_LEN];
271            int life;
272            time_t last_login_dt;
273    
274            snprintf(sql, sizeof(sql),
275                            "SELECT life, UNIX_TIMESTAMP(last_login_dt) "
276                            "FROM user_pubinfo WHERE UID = %ld",
277                            BBS_uid);
278            if (mysql_query(db, sql) != 0)
279            {
280                    log_error("Query user_pubinfo failed\n");
281                    return -1;
282            }
283            if ((rs = mysql_store_result(db)) == NULL)
284            {
285                    log_error("Get user_pubinfo data failed\n");
286                    return -1;
287            }
288            if ((row = mysql_fetch_row(rs)))
289          {          {
290            //Input password                  life = atoi(row[0]);
291            prints ("\033[1;37m请输入密码\033[m: ");                  last_login_dt = (time_t)atol(row[1]);
292            iflush ();          }
293            else
294            {
295                    mysql_free_result(rs);
296                    return -1; // Data not found
297            }
298            mysql_free_result(rs);
299    
300            str_input (password, 19, 1);          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
301                    time(0) - last_login_dt > 60 * 60 * 24 * life)
302            {
303                    return -3; // Dead
304            }
305    
306            ok = (check_user (username, password) == 0);          if (load_priv(db, &BBS_priv, BBS_uid) != 0)
307            {
308                    return -1;
309          }          }
310        if (count >= 3 && !ok)  
311            return 0;
312    }
313    
314    int load_guest_info(MYSQL *db)
315    {
316            strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);
317            BBS_username[sizeof(BBS_username) - 1] = '\0';
318    
319            if (load_priv(db, &BBS_priv, 0) != 0)
320            {
321                    return -1;
322            }
323    
324            if (user_online_add(db) != 0)
325          {          {
326            login_fail ();                  return -1;
           return -1;  
327          }          }
     }  
328    
329    return 0;          BBS_last_access_tm = BBS_login_tm = time(0);
330    
331            return 0;
332  }  }
333    
334  int  int user_online_add(MYSQL *db)
 check_user (char *username, char *password)  
335  {  {
336    MYSQL *db;          char sql[SQL_BUFFER_LEN];
337    MYSQL_RES *rs;  
338    MYSQL_ROW row;          if (user_online_del(db) != 0)
339    char sql[1024];          {
340    long int BBS_uid;                  return -1;
341    int ret;          }
342    
343    //Verify format          snprintf(sql, sizeof(sql),
344    if (ireg ("^[A-Za-z0-9_]{3,14}$", username, 0, NULL) != 0 ||                          "INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) "
345        ireg ("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0)                          "VALUES('Telnet_Process_%d', %ld, '%s', NOW(), NOW())",
346      {                          getpid(), BBS_priv.uid, hostaddr_client);
347        prints ("\033[1;31m用户名或密码格式错误...\033[m\r\n");          if (mysql_query(db, sql) != 0)
348        iflush ();          {
349        return 1;                  log_error("Add user_online failed\n");
350      }                  return -1;
351            }
   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;  
     }  
352    
353    return 0;          return 0;
354  }  }
355    
356  int  int user_online_del(MYSQL *db)
 load_user_info (MYSQL * db, long int BBS_uid)  
357  {  {
358    MYSQL_RES *rs;          char sql[SQL_BUFFER_LEN];
359    MYSQL_ROW row;  
360    char sql[1024];          snprintf(sql, sizeof(sql),
361    long int BBS_auth_uid = 0;                          "DELETE FROM user_online WHERE SID = 'Telnet_Process_%d'",
362    int life;                          getpid());
363    time_t last_login_dt;          if (mysql_query(db, sql) != 0)
364            {
365    sprintf (sql,                  log_error("Delete user_online failed\n");
366             "select life,UNIX_TIMESTAMP(last_login_dt) "                  return -1;
367             "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;  
     }  
368    
369    return 0;          return 0;
370  }  }


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

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