/[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.12 by sysadm, Mon Apr 28 04:23:38 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.txt");          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-z0-9_]{3,14}$", 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);          sprintf(sql,
112                            "select UID,username,p_login from user_list where username='%s' "
113                            "and (password=MD5('%s') or password=SHA2('%s', 256)) and "
114                            "enable",
115                            username, password, password);
116            if (mysql_query(db, sql) != 0)
117            {
118                    log_error("Query user_list failed\n");
119                    return -1;
120            }
121            if ((rs = mysql_store_result(db)) == NULL)
122            {
123                    log_error("Get user_list data failed\n");
124                    return -1;
125            }
126            if (row = mysql_fetch_row(rs))
127            {
128                    BBS_uid = atol(row[0]);
129                    strcpy(BBS_username, row[1]);
130                    if (atoi(row[2]) == 0)
131                    {
132                            mysql_free_result(rs);
133                            mysql_close(db);
134    
135                            prints("\033[1;31m您目前无权登陆...\033[m\r\n");
136                            iflush();
137                            return 1;
138                    }
139            }
140            else
141            {
142                    mysql_free_result(rs);
143    
144            ok = (check_user (username, password) == 0);                  sprintf(sql,
145                                    "insert into user_err_login_log"
146                                    "(username,password,login_dt,login_ip) values"
147                                    "('%s','%s',now(),'%s')",
148                                    username, password, hostaddr_client);
149                    if (mysql_query(db, sql) != 0)
150                    {
151                            log_error("Insert into user_err_login_log failed\n");
152                            return -1;
153                    }
154    
155                    mysql_close(db);
156    
157                    prints("\033[1;31m错误的用户名或密码...\033[m\r\n");
158                    iflush();
159                    return 1;
160          }          }
161        if (count >= 3 && !ok)          mysql_free_result(rs);
162    
163            ret = load_user_info(db, BBS_uid);
164    
165            switch (ret)
166          {          {
167            login_fail ();          case 0: // Login successfully
168            return -1;                  return 0;
169                    break;
170            case -1: // Load data error
171                    prints("\033[1;31m读取用户数据错误...\033[m\r\n");
172                    iflush();
173                    return -1;
174                    break;
175            case -2: // Unused
176                    return 0;
177                    break;
178            case -3: // Dead
179                    prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
180                    iflush();
181                    return 1;
182            default:
183                    return -2;
184          }          }
     }  
185    
186    return 0;          mysql_close(db);
187    
188            return 0;
189  }  }
190    
191  int  int load_user_info(MYSQL *db, long int BBS_uid)
 check_user (char *username, char *password)  
192  {  {
193    MYSQL *db;          MYSQL_RES *rs;
194    MYSQL_RES *rs;          MYSQL_ROW row;
195    MYSQL_ROW row;          char sql[1024];
196    char sql[1024];          long int BBS_auth_uid = 0;
197    long int BBS_uid;          int life;
198    int ret;          time_t last_login_dt;
199    
200    //Verify format          sprintf(sql,
201    if (ireg ("^[A-Za-z0-9_]{3,14}$", username, 0, NULL) != 0 ||                          "select life,UNIX_TIMESTAMP(last_login_dt) "
202        ireg ("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0)                          "from user_pubinfo where UID=%ld limit 1",
203      {                          BBS_uid);
204        prints ("\033[1;31m用户名或密码格式错误...\033[m\r\n");          if (mysql_query(db, sql) != 0)
205        iflush ();          {
206        return 1;                  log_error("Query user_pubinfo failed\n");
207      }                  return -1;
208            }
209    db = (MYSQL *) db_open ();          if ((rs = mysql_store_result(db)) == NULL)
210    if (db == NULL)          {
211      {                  log_error("Get user_pubinfo data failed\n");
212        return -1;                  return -1;
213      }          }
214            if (row = mysql_fetch_row(rs))
215    sprintf (sql,          {
216             "select UID,p_login from user_list where username='%s' "                  life = atoi(row[0]);
217             "and (password=MD5('%s') or password=PASSWORD('%s')) and "                  last_login_dt = (time_t)atol(row[1]);
218             "enable", username, password, password);          }
219    if (mysql_query (db, sql) != 0)          else
220      {          {
221        log_error ("Query user_list failed\n");                  mysql_free_result(rs);
222        return -1;                  return (-1); // Data not found
223      }          }
224    if ((rs = mysql_store_result (db)) == NULL)          mysql_free_result(rs);
225      {  
226        log_error ("Get user_list data failed\n");          if (time(0) - last_login_dt >= 60 * 60 * 24 * life)
227        return -1;          {
228      }                  return (-3); // Dead
229    if (row = mysql_fetch_row (rs))          }
230      {  
231        BBS_uid = atol (row[0]);          sprintf(sql,
232        if (atoi (row[1]) == 0)                          "insert into user_login_log"
233          {                          "(uid,login_dt,login_ip) values(%ld"
234            mysql_free_result (rs);                          ",now(),'%s')",
235            mysql_close (db);                          BBS_uid, hostaddr_client);
236            if (mysql_query(db, sql) != 0)
237            prints ("\033[1;31m您目前无权登陆...\033[m\r\n");          {
238            iflush ();                  log_error("Insert into user_login_log failed\n");
239            return 1;                  return -1;
240          }          }
241      }  
242    else          load_priv(db, &BBS_priv, BBS_uid);
243      {  
244        mysql_free_result (rs);          BBS_last_access_tm = BBS_login_tm = time(0);
245            BBS_last_sub_tm = time(0) - 60;
       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;  
     }  
246    
247    return 0;          sprintf(sql,
248                            "update user_pubinfo set visit_count=visit_count+1,"
249                            "last_login_dt=now() where uid=%ld",
250                            BBS_uid);
251            if (mysql_query(db, sql) != 0)
252            {
253                    log_error("Update user_pubinfo failed\n");
254                    return -1;
255            }
256    
257            return 0;
258  }  }
259    
260  int  int load_guest_info(MYSQL *db, long int BBS_uid)
 load_user_info (MYSQL * db, long int BBS_uid)  
261  {  {
262    MYSQL_RES *rs;          MYSQL_RES *rs;
263    MYSQL_ROW row;          MYSQL_ROW row;
264    char sql[1024];  
265    long int BBS_auth_uid = 0;          db = (MYSQL *)db_open();
266    int life;          if (db == NULL)
267    time_t last_login_dt;          {
268                    return -1;
269    sprintf (sql,          }
270             "select life,UNIX_TIMESTAMP(last_login_dt) "  
271             "from user_pubinfo where UID=%ld limit 1", BBS_uid);          strcpy(BBS_username, "guest");
272    if (mysql_query (db, sql) != 0)  
273      {          load_priv(db, &BBS_priv, 0, 0, S_NONE);
274        log_error ("Query user_pubinfo failed\n");  
275        return -1;          BBS_last_access_tm = BBS_login_tm = time(0);
276      }          BBS_last_sub_tm = time(0) - 60;
277    if ((rs = mysql_store_result (db)) == NULL)  
278      {          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;  
     }  
279    
280    return 0;          return 0;
281  }  }


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

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