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


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

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