/[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.13 by sysadm, Mon Apr 28 12:45:57 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                            login.c  -  description                                                    login.c  -  description
3                               -------------------                                                           -------------------
4      begin                : Mon Oct 20 2004          begin                : Mon Oct 20 2004
5      copyright            : (C) 2004 by Leaflet          copyright            : (C) 2004 by Leaflet
6      email                : leaflet@leafok.com          email                : leaflet@leafok.com
7   ***************************************************************************/   ***************************************************************************/
8    
9  /***************************************************************************  /***************************************************************************
# Line 18  Line 18 
18  #include "bbs.h"  #include "bbs.h"
19  #include "common.h"  #include "common.h"
20  #include "io.h"  #include "io.h"
21    #include "database.h"
22  #include <mysql.h>  #include <mysql.h>
23  #include <regex.h>  #include <regex.h>
24    
25  void  void login_fail()
 login_fail ()  
26  {  {
27    char temp[256];          char temp[256];
28    
29    strcpy (temp, app_home_dir);          strcpy(temp, app_home_dir);
30    strcat (temp, "data/login_error.dat");          strcat(temp, "data/login_error.txt");
31    display_file (temp);          display_file(temp);
32    
33    sleep (1);          sleep(1);
34  }  }
35    
36  int  int bbs_login()
 bbs_login ()  
37  {  {
38    char username[20], password[20];          char username[20], password[20];
39    int count, ok;          int count, ok;
40    
41    //Input username          // Input username
42    count = 0;          count = 0;
43    ok = 0;          ok = 0;
44    while (!ok)          while (!ok)
45      {          {
46        prints                  prints("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "
47          ("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "                             "注册请输入`\033[1;31mnew\033[m'): ");
48           "注册请输入`\033[1;31mnew\033[m'): ");                  iflush();
49        iflush ();  
50                    str_input(username, 19, DOECHO);
51                    count++;
52    
53                    if (strcmp(username, "guest") == 0)
54                    {
55                            load_guest_info();
56                            return 0;
57                    }
58    
59                    if (strcmp(username, "new") == 0)
60                    {
61                            if (user_register() == 0)
62                                    return 0;
63                            else
64                                    return -2;
65                    }
66    
67                    if (strlen(username) > 0)
68                    {
69                            // Input password
70                            prints("\033[1;37m请输入密码\033[m: ");
71                            iflush();
72    
73                            str_input(password, 19, NOECHO);
74    
75                            ok = (check_user(username, password) == 0);
76                    }
77                    if (count >= 3 && !ok)
78                    {
79                            login_fail();
80                            return -1;
81                    }
82            }
83    
84        str_input (username, 19, 0);          return 0;
85        count++;  }
86    
87        if (strcmp (username, "guest") == 0)  int check_user(char *username, char *password)
88          return 1;  {
89            MYSQL *db;
90            MYSQL_RES *rs;
91            MYSQL_ROW row;
92            char sql[1024];
93            long int BBS_uid;
94            int ret;
95    
96            // Verify format
97            if (ireg("^[A-Za-z][A-Za-z0-9]{2,11}$", username, 0, NULL) != 0 ||
98                    ireg("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0)
99            {
100                    prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");
101                    iflush();
102                    return 1;
103            }
104    
105        if (strlen (username) > 0)          db = (MYSQL *)db_open();
106            if (db == NULL)
107          {          {
108            //Input password                  return -1;
109            prints ("\033[1;37m请输入密码\033[m: ");          }
           iflush ();  
110    
111            str_input (password, 19, 1);          // Begin transaction
112            if (mysql_query(db, "SET autocommit=0") != 0)
113            {
114                    log_error("SET autocommit=0 failed\n");
115                    return -1;
116            }
117    
118            if (mysql_query(db, "BEGIN") != 0)
119            {
120                    log_error("Begin transaction failed\n");
121                    return -1;
122            }
123    
124            ok = (check_user (username, password) == 0);          sprintf(sql,
125                            "SELECT UID, username, p_login FROM user_list "
126                            "WHERE username = '%s' AND password = SHA2('%s', 256) AND enable",
127                            username, password);
128            if (mysql_query(db, sql) != 0)
129            {
130                    log_error("Query user_list failed\n");
131                    return -1;
132          }          }
133        if (count >= 3 && !ok)          if ((rs = mysql_store_result(db)) == NULL)
134            {
135                    log_error("Get user_list data failed\n");
136                    return -1;
137            }
138            if (row = mysql_fetch_row(rs))
139            {
140                    BBS_uid = atol(row[0]);
141                    strcpy(BBS_username, row[1]);
142                    int p_login = atoi(row[2]);
143    
144                    mysql_free_result(rs);
145    
146                    // Add user login log
147                    sprintf(sql,
148                                    "INSERT INTO user_login_log(UID, login_dt, login_ip) "
149                                    "VALUES(%ld, NOW(), '%s')",
150                                    BBS_uid, hostaddr_client);
151                    if (mysql_query(db, sql) != 0)
152                    {
153                            log_error("Insert into user_login_log failed\n");
154                            return -1;
155                    }
156    
157                    // Commit transaction
158                    if (mysql_query(db, "COMMIT") != 0)
159                    {
160                            log_error("Commit transaction failed\n");
161                            return -1;
162                    }
163    
164                    if (p_login == 0)
165                    {
166                            mysql_free_result(rs);
167                            mysql_close(db);
168    
169                            prints("\033[1;31m您目前无权登陆...\033[m\r\n");
170                            iflush();
171                            return 1;
172                    }
173            }
174            else
175            {
176                    mysql_free_result(rs);
177    
178                    sprintf(sql,
179                                    "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "
180                                    "VALUES('%s', '%s', NOW(), '%s')",
181                                    username, password, hostaddr_client);
182                    if (mysql_query(db, sql) != 0)
183                    {
184                            log_error("Insert into user_err_login_log failed\n");
185                            return -1;
186                    }
187    
188                    // Commit transaction
189                    if (mysql_query(db, "COMMIT") != 0)
190                    {
191                            log_error("Commit transaction failed\n");
192                            return -1;
193                    }
194    
195                    mysql_close(db);
196    
197                    prints("\033[1;31m错误的用户名或密码...\033[m\r\n");
198                    iflush();
199                    return 1;
200            }
201    
202            // Set AUTOCOMMIT = 1
203            if (mysql_query(db, "SET autocommit=1") != 0)
204            {
205                    log_error("SET autocommit=1 failed\n");
206                    return -1;
207            }
208    
209            ret = load_user_info(db, BBS_uid);
210    
211            switch (ret)
212          {          {
213            login_fail ();          case 0: // Login successfully
214            return -1;                  break;
215            case -1: // Load data error
216                    prints("\033[1;31m读取用户数据错误...\033[m\r\n");
217                    iflush();
218                    return -1;
219            case -2: // Unused
220                    prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");
221                    iflush();
222                    return 1;
223            case -3: // Dead
224                    prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
225                    iflush();
226                    return 1;
227            default:
228                    return -2;
229          }          }
     }  
230    
231    return 0;          sprintf(sql,
232                            "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
233                            "last_login_dt = NOW() WHERE UID = %ld",
234                            BBS_uid);
235            if (mysql_query(db, sql) != 0)
236            {
237                    log_error("Update user_pubinfo failed\n");
238                    return -1;
239            }
240    
241            mysql_close(db);
242    
243            BBS_last_access_tm = BBS_login_tm = time(0);
244    
245            return 0;
246  }  }
247    
248  int  int load_user_info(MYSQL *db, long int BBS_uid)
 check_user (char *username, char *password)  
249  {  {
250    MYSQL *db;          MYSQL_RES *rs;
251    MYSQL_RES *rs;          MYSQL_ROW row;
252    MYSQL_ROW row;          char sql[1024];
253    char sql[1024];          int life;
254    long int BBS_uid;          time_t last_login_dt;
255    int ret;  
256            sprintf(sql,
257    //Verify format                          "SELECT life, UNIX_TIMESTAMP(last_login_dt) "
258    if (ireg ("^[A-Za-z0-9_]{3,14}$", username, 0, NULL) != 0 ||                          "FROM user_pubinfo WHERE UID = %ld",
259        ireg ("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0)                          BBS_uid);
260      {          if (mysql_query(db, sql) != 0)
261        prints ("\033[1;31m用户名或密码格式错误...\033[m\r\n");          {
262        iflush ();                  log_error("Query user_pubinfo failed\n");
263        return 1;                  return -1;
264      }          }
265            if ((rs = mysql_store_result(db)) == NULL)
266    db = (MYSQL *) db_open ();          {
267    if (db == NULL)                  log_error("Get user_pubinfo data failed\n");
268      {                  return -1;
269        return -1;          }
270      }          if (row = mysql_fetch_row(rs))
271            {
272    sprintf (sql,                  life = atoi(row[0]);
273             "select UID,p_login from user_list where username='%s' "                  last_login_dt = (time_t)atol(row[1]);
274             "and (password=MD5('%s') or password=PASSWORD('%s')) and "          }
275             "enable", username, password, password);          else
276    if (mysql_query (db, sql) != 0)          {
277      {                  mysql_free_result(rs);
278        log_error ("Query user_list failed\n");                  return (-1); // Data not found
279        return -1;          }
280      }          mysql_free_result(rs);
281    if ((rs = mysql_store_result (db)) == NULL)  
282      {          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
283        log_error ("Get user_list data failed\n");                  time(0) - last_login_dt > 60 * 60 * 24 * life)
284        return -1;          {
285      }                  return (-3); // Dead
286    if (row = mysql_fetch_row (rs))          }
287      {  
288        BBS_uid = atol (row[0]);          load_priv(db, &BBS_priv, BBS_uid);
       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;  
     }  
289    
290    return 0;          return 0;
291  }  }
292    
293  int  int load_guest_info(MYSQL *db, long int BBS_uid)
 load_user_info (MYSQL * db, long int BBS_uid)  
294  {  {
295    MYSQL_RES *rs;          MYSQL_RES *rs;
296    MYSQL_ROW row;          MYSQL_ROW row;
297    char sql[1024];  
298    long int BBS_auth_uid = 0;          db = (MYSQL *)db_open();
299    int life;          if (db == NULL)
300    time_t last_login_dt;          {
301                    return -1;
302    sprintf (sql,          }
303             "select life,UNIX_TIMESTAMP(last_login_dt) "  
304             "from user_pubinfo where UID=%ld limit 1", BBS_uid);          strcpy(BBS_username, "guest");
305    if (mysql_query (db, sql) != 0)  
306      {          load_priv(db, &BBS_priv, 0);
307        log_error ("Query user_pubinfo failed\n");  
308        return -1;          BBS_last_access_tm = BBS_login_tm = time(0);
309      }  
310    if ((rs = mysql_store_result (db)) == NULL)          mysql_close(db);
     {  
       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;  
     }  
311    
312    return 0;          return 0;
313  }  }


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

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