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


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

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