/[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.14 by sysadm, Wed Apr 30 09:18:19 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 15  Line 15 
15   *                                                                         *   *                                                                         *
16   ***************************************************************************/   ***************************************************************************/
17    
18    #include "login.h"
19    #include "register.h"
20  #include "bbs.h"  #include "bbs.h"
21    #include "user_priv.h"
22    #include "reg_ex.h"
23  #include "common.h"  #include "common.h"
24    #include "log.h"
25  #include "io.h"  #include "io.h"
26    #include "screen.h"
27    #include "database.h"
28    #include <string.h>
29  #include <mysql.h>  #include <mysql.h>
30  #include <regex.h>  #include <regex.h>
31    #include <unistd.h>
32    
33  void  void login_fail()
 login_fail ()  
34  {  {
35    char temp[256];          char temp[256];
36    
37    strcpy (temp, app_home_dir);          strcpy(temp, app_home_dir);
38    strcat (temp, "data/login_error.dat");          strcat(temp, "data/login_error.txt");
39    display_file (temp);          display_file(temp);
40    
41    sleep (1);          sleep(1);
42  }  }
43    
44  int  int bbs_login()
 bbs_login ()  
45  {  {
46    char username[20], password[20];          char username[20], password[20];
47    int count, ok;          int count, ok;
48    
49    //Input username          // Input username
50    count = 0;          count = 0;
51    ok = 0;          ok = 0;
52    while (!ok)          while (!ok)
53      {          {
54        prints                  prints("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "
55          ("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "                             "注册请输入`\033[1;31mnew\033[m'): ");
56           "注册请输入`\033[1;31mnew\033[m'): ");                  iflush();
57        iflush ();  
58                    str_input(username, 19, DOECHO);
59                    count++;
60    
61                    if (strcmp(username, "guest") == 0)
62                    {
63                            MYSQL * db = db_open();
64                            if (db == NULL)
65                            {
66                                    return -1;
67                            }
68                    
69                            load_guest_info(db);
70    
71                            mysql_close(db);
72    
73                            return 0;
74                    }
75    
76                    if (strcmp(username, "new") == 0)
77                    {
78                            if (user_register() == 0)
79                                    return 0;
80                            else
81                                    return -2;
82                    }
83    
84                    if (strlen(username) > 0)
85                    {
86                            // Input password
87                            prints("\033[1;37m请输入密码\033[m: ");
88                            iflush();
89    
90                            str_input(password, 19, NOECHO);
91    
92                            MYSQL * db = db_open();
93                            if (db == NULL)
94                            {
95                                    return -1;
96                            }
97                    
98                            ok = (check_user(db, username, password) == 0);
99    
100                            mysql_close(db);
101                    }
102                    if (count >= 3 && !ok)
103                    {
104                            login_fail();
105                            return -1;
106                    }
107            }
108    
109        str_input (username, 19, 0);          return 0;
110        count++;  }
111    
112        if (strcmp (username, "guest") == 0)  int check_user(MYSQL *db, char *username, char *password)
113          return 1;  {
114            MYSQL_RES *rs;
115            MYSQL_ROW row;
116            char sql[1024];
117            long int BBS_uid;
118            int ret;
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        if (strlen (username) > 0)          // Begin transaction
130            if (mysql_query(db, "SET autocommit=0") != 0)
131          {          {
132            //Input password                  log_error("SET autocommit=0 failed\n");
133            prints ("\033[1;37m请输入密码\033[m: ");                  return -1;
134            iflush ();          }
135    
136            str_input (password, 19, 1);          if (mysql_query(db, "BEGIN") != 0)
137            {
138                    log_error("Begin transaction failed\n");
139                    return -1;
140            }
141    
142            ok = (check_user (username, password) == 0);          sprintf(sql,
143                            "SELECT UID, username, p_login FROM user_list "
144                            "WHERE username = '%s' AND password = SHA2('%s', 256) AND enable",
145                            username, password);
146            if (mysql_query(db, sql) != 0)
147            {
148                    log_error("Query user_list failed\n");
149                    return -1;
150          }          }
151        if (count >= 3 && !ok)          if ((rs = mysql_store_result(db)) == NULL)
152          {          {
153            login_fail ();                  log_error("Get user_list data failed\n");
154            return -1;                  return -1;
155          }          }
156      }          if (row = mysql_fetch_row(rs))
157            {
158                    BBS_uid = atol(row[0]);
159                    strcpy(BBS_username, row[1]);
160                    int p_login = atoi(row[2]);
161    
162                    mysql_free_result(rs);
163    
164                    // Add user login log
165                    sprintf(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                    sprintf(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    return 0;          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            sprintf(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            BBS_last_access_tm = BBS_login_tm = time(0);
257    
258            return 0;
259  }  }
260    
261  int  int load_user_info(MYSQL *db, long int BBS_uid)
 check_user (char *username, char *password)  
262  {  {
263    MYSQL *db;          MYSQL_RES *rs;
264    MYSQL_RES *rs;          MYSQL_ROW row;
265    MYSQL_ROW row;          char sql[1024];
266    char sql[1024];          int life;
267    long int BBS_uid;          time_t last_login_dt;
268    int ret;  
269            sprintf(sql,
270    //Verify format                          "SELECT life, UNIX_TIMESTAMP(last_login_dt) "
271    if (ireg ("^[A-Za-z0-9_]{3,14}$", username, 0, NULL) != 0 ||                          "FROM user_pubinfo WHERE UID = %ld",
272        ireg ("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0)                          BBS_uid);
273      {          if (mysql_query(db, sql) != 0)
274        prints ("\033[1;31m用户名或密码格式错误...\033[m\r\n");          {
275        iflush ();                  log_error("Query user_pubinfo failed\n");
276        return 1;                  return -1;
277      }          }
278            if ((rs = mysql_store_result(db)) == NULL)
279    db = (MYSQL *) db_open ();          {
280    if (db == NULL)                  log_error("Get user_pubinfo data failed\n");
281      {                  return -1;
282        return -1;          }
283      }          if (row = mysql_fetch_row(rs))
284            {
285    sprintf (sql,                  life = atoi(row[0]);
286             "select UID,p_login from user_list where username='%s' "                  last_login_dt = (time_t)atol(row[1]);
287             "and (password=MD5('%s') or password=PASSWORD('%s')) and "          }
288             "enable", username, password, password);          else
289    if (mysql_query (db, sql) != 0)          {
290      {                  mysql_free_result(rs);
291        log_error ("Query user_list failed\n");                  return (-1); // Data not found
292        return -1;          }
293      }          mysql_free_result(rs);
294    if ((rs = mysql_store_result (db)) == NULL)  
295      {          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
296        log_error ("Get user_list data failed\n");                  time(0) - last_login_dt > 60 * 60 * 24 * life)
297        return -1;          {
298      }                  return (-3); // Dead
299    if (row = mysql_fetch_row (rs))          }
300      {  
301        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;  
     }  
302    
303    return 0;          return 0;
304  }  }
305    
306  int  int load_guest_info(MYSQL *db)
 load_user_info (MYSQL * db, long int BBS_uid)  
307  {  {
308    MYSQL_RES *rs;          MYSQL_RES *rs;
309    MYSQL_ROW row;          MYSQL_ROW row;
310    char sql[1024];  
311    long int BBS_auth_uid = 0;          strcpy(BBS_username, "guest");
312    int life;  
313    time_t last_login_dt;          load_priv(db, &BBS_priv, 0);
314    
315    sprintf (sql,          BBS_last_access_tm = BBS_login_tm = time(0);
            "select life,UNIX_TIMESTAMP(last_login_dt) "  
            "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;  
     }  
316    
317    return 0;          return 0;
318  }  }


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

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