/[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.1 by sysadm, Wed Oct 20 07:46:26 2004 UTC Revision 1.5 by sysadm, Sat Oct 23 18:41:41 2004 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
25    login_fail ()
26    {
27      char temp[256];
28    
29      strcpy (temp, app_home_dir);
30      strcat (temp, "data/login_error.txt");
31      display_file (temp);
32    
33      sleep (1);
34    }
35    
36  int  int
37  bbs_login()  bbs_login ()
38  {  {
39      char username[20], password[20];
40      int count, ok;
41    
42      //Input username
43      count = 0;
44      ok = 0;
45      while (!ok)
46        {
47          prints
48            ("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "
49             "注册请输入`\033[1;31mnew\033[m'): ");
50          iflush ();
51    
52          str_input (username, 19, 0);
53          count++;
54    
55          if (strcmp (username, "guest") == 0)
56            {
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)
70            {
71              //Input password
72              prints ("\033[1;37m请输入密码\033[m: ");
73              iflush ();
74    
75              str_input (password, 19, 1);
76    
77              ok = (check_user (username, password) == 0);
78            }
79          if (count >= 3 && !ok)
80            {
81              login_fail ();
82              return -1;
83            }
84        }
85    
86      return 0;
87    }
88    
89    int
90    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,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          if (atoi (row[1]) == 0)
132            {
133              mysql_free_result (rs);
134              mysql_close (db);
135    
136              prints ("\033[1;31m您目前无权登陆...\033[m\r\n");
137              iflush ();
138              return 1;
139            }
140        }
141      else
142        {
143          mysql_free_result (rs);
144    
145          sprintf (sql,
146                   "insert delayed into user_err_login_log"
147                   "(username,password,login_dt,login_ip) values"
148                   "('%s','%s',now(),'%s')", 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      mysql_free_result (rs);
162    
163      BBS_passwd_complex = verify_pass_complexity (password, username, 6);
164    
165      ret = load_user_info (db, BBS_uid);
166    
167      switch (ret)
168        {
169        case 0:                     //Login successfully
170          return 0;
171          break;
172        case -1:                    //Load data error
173          prints ("\033[1;31m读取用户数据错误...\033[m\r\n");
174          iflush ();
175          return -1;
176          break;
177        case -2:                    //Unused
178          return 0;
179          break;
180        case -3:                    //Dead
181          prints ("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
182          iflush ();
183          return 1;
184        default:
185          return -2;
186        }
187    
188      mysql_close (db);
189    
190      return 0;
191    }
192    
193    int
194    load_user_info (MYSQL * db, long int BBS_uid)
195    {
196      MYSQL_RES *rs;
197      MYSQL_ROW row;
198      char sql[1024];
199      long int BBS_auth_uid = 0;
200      int life;
201      time_t last_login_dt;
202    
203      sprintf (sql,
204               "select life,UNIX_TIMESTAMP(last_login_dt) "
205               "from user_pubinfo where UID=%ld limit 1", BBS_uid);
206      if (mysql_query (db, sql) != 0)
207        {
208          log_error ("Query user_pubinfo failed\n");
209          return -1;
210        }
211      if ((rs = mysql_store_result (db)) == NULL)
212        {
213          log_error ("Get user_pubinfo data failed\n");
214          return -1;
215        }
216      if (row = mysql_fetch_row (rs))
217        {
218          life = atoi (row[0]);
219          last_login_dt = (time_t) atol (row[1]);
220        }
221      else
222        {
223          mysql_free_result (rs);
224          return (-1);              //Data not found
225        }
226      mysql_free_result (rs);
227    
228      if (time (0) - last_login_dt >= 60 * 60 * 24 * life)
229        {
230          return (-3);              //Dead
231        }
232    
233      sprintf (sql,
234               "select AUID from user_auth where UID=%ld"
235               " and enable and expire_dt>now()", BBS_uid);
236      if (mysql_query (db, sql) != 0)
237        {
238          log_error ("Query user_auth failed\n");
239          return -1;
240        }
241      if ((rs = mysql_store_result (db)) == NULL)
242        {
243          log_error ("Get user_auth data failed\n");
244          return -1;
245        }
246      if (row = mysql_fetch_row (rs))
247        {
248          BBS_auth_uid = atol (row[0]);
249        }
250      mysql_free_result (rs);
251    
252      sprintf (sql,
253               "insert delayed into user_login_log"
254               "(uid,login_dt,login_ip) values(%ld"
255               ",now(),'%s')", BBS_uid, hostaddr_client);
256      if (mysql_query (db, sql) != 0)
257        {
258          log_error ("Insert into user_login_log failed\n");
259          return -1;
260        }
261    
262      load_priv (db, &BBS_priv, BBS_uid, BBS_auth_uid,
263                 (!BBS_passwd_complex ? S_MAN_M : S_NONE) |
264                 (BBS_auth_uid ? S_NONE : S_MAIL));
265    
266      BBS_last_access_tm = BBS_login_tm = time (0);
267      BBS_last_sub_tm = time (0) - 60;
268    
269      sprintf (sql,
270               "update user_pubinfo set visit_count=visit_count+1,"
271               "last_login_dt=now() where uid=%ld", BBS_uid);
272      if (mysql_query (db, sql) != 0)
273        {
274          log_error ("Update user_pubinfo failed\n");
275          return -1;
276        }
277    
278      return 0;
279    }
280    
281    int
282    load_guest_info (MYSQL * db, long int BBS_uid)
283    {
284      MYSQL_RES *rs;
285      MYSQL_ROW row;
286    
287      db = (MYSQL *) db_open ();
288      if (db == NULL)
289        {
290          return -1;
291        }
292    
293      load_priv (db, &BBS_priv, 0, 0, S_NONE);
294    
295      BBS_last_access_tm = BBS_login_tm = time (0);
296      BBS_last_sub_tm = time (0) - 60;
297    
298      mysql_close (db);
299    
300    return 0;    return 0;
301  }  }


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

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