/[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.4 by sysadm, Fri Oct 22 19:51:01 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
37    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            return 1;
57    
58          if (strlen (username) > 0)
59            {
60              //Input password
61              prints ("\033[1;37m请输入密码\033[m: ");
62              iflush ();
63    
64              str_input (password, 19, 1);
65    
66              ok = (check_user (username, password) == 0);
67            }
68          if (count >= 3 && !ok)
69            {
70              login_fail ();
71              return -1;
72            }
73        }
74    
75      return 0;
76    }
77    
78    int
79    check_user (char *username, char *password)
80    {
81      MYSQL *db;
82      MYSQL_RES *rs;
83      MYSQL_ROW row;
84      char sql[1024];
85      long int BBS_uid;
86      int ret;
87    
88      //Verify format
89      if (ireg ("^[A-Za-z0-9_]{3,14}$", username, 0, NULL) != 0 ||
90          ireg ("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0)
91        {
92          prints ("\033[1;31m用户名或密码格式错误...\033[m\r\n");
93          iflush ();
94          return 1;
95        }
96    
97      db = (MYSQL *) db_open ();
98      if (db == NULL)
99        {
100          return -1;
101        }
102    
103      sprintf (sql,
104               "select UID,p_login from user_list where username='%s' "
105               "and (password=MD5('%s') or password=PASSWORD('%s')) and "
106               "enable", username, password, password);
107      if (mysql_query (db, sql) != 0)
108        {
109          log_error ("Query user_list failed\n");
110          return -1;
111        }
112      if ((rs = mysql_store_result (db)) == NULL)
113        {
114          log_error ("Get user_list data failed\n");
115          return -1;
116        }
117      if (row = mysql_fetch_row (rs))
118        {
119          BBS_uid = atol (row[0]);
120          if (atoi (row[1]) == 0)
121            {
122              mysql_free_result (rs);
123              mysql_close (db);
124    
125              prints ("\033[1;31m您目前无权登陆...\033[m\r\n");
126              iflush ();
127              return 1;
128            }
129        }
130      else
131        {
132          mysql_free_result (rs);
133    
134          sprintf (sql,
135                   "insert delayed into user_err_login_log"
136                   "(username,password,login_dt,login_ip) values"
137                   "('%s','%s',now(),'%s')",
138                   username, password, hostaddr_client);
139          if (mysql_query (db, sql) != 0)
140            {
141              log_error ("Insert into user_err_login_log failed\n");
142              return -1;
143            }
144    
145          mysql_close (db);
146    
147          prints ("\033[1;31m错误的用户名或密码...\033[m\r\n");
148          iflush ();
149          return 1;
150        }
151      mysql_free_result (rs);
152    
153      BBS_passwd_complex = verify_pass_complexity(password, username, 6);
154    
155      ret = load_user_info (db, BBS_uid);
156    
157      mysql_close (db);
158    
159      switch (ret)
160        {
161        case 0:                     //Login successfully
162          return 0;
163          break;
164        case -1:                    //Load data error
165          prints ("\033[1;31m读取用户数据错误...\033[m\r\n");
166          iflush ();
167          return -1;
168          break;
169        case -2:                    //Unused
170          return 0;
171          break;
172        case -3:
173          prints ("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
174          iflush ();
175          return 1;
176        default:
177          return -2;
178        }
179    
180      return 0;
181    }
182    
183  int  int
184  bbs_login()  load_user_info (MYSQL * db, long int BBS_uid)
185  {  {
186      MYSQL_RES *rs;
187      MYSQL_ROW row;
188      char sql[1024];
189      long int BBS_auth_uid = 0;
190      int life;
191      time_t last_login_dt;
192    
193      sprintf (sql,
194               "select life,UNIX_TIMESTAMP(last_login_dt) "
195               "from user_pubinfo where UID=%ld limit 1", BBS_uid);
196      if (mysql_query (db, sql) != 0)
197        {
198          log_error ("Query user_pubinfo failed\n");
199          return -1;
200        }
201      if ((rs = mysql_store_result (db)) == NULL)
202        {
203          log_error ("Get user_pubinfo data failed\n");
204          return -1;
205        }
206      if (row = mysql_fetch_row (rs))
207        {
208          life = atoi (row[0]);
209          last_login_dt = (time_t) atol (row[1]);
210        }
211      else
212        {
213          mysql_free_result (rs);
214          return (-1);              //Data not found
215        }
216      mysql_free_result (rs);
217    
218      if (time (0) - last_login_dt >= 60 * 60 * 24 * life)
219        {
220          return (-3);              //Dead
221        }
222    
223      sprintf (sql,
224               "select AUID from user_auth where UID=%ld"
225               " and enable and expire_dt>now()", BBS_uid);
226      if (mysql_query (db, sql) != 0)
227        {
228          log_error ("Query user_auth failed\n");
229          return -1;
230        }
231      if ((rs = mysql_store_result (db)) == NULL)
232        {
233          log_error ("Get user_auth data failed\n");
234          return -1;
235        }
236      if (row = mysql_fetch_row (rs))
237        {
238          BBS_auth_uid = atol (row[0]);
239        }
240      mysql_free_result (rs);
241    
242      sprintf (sql,
243       "insert delayed into user_login_log"
244       "(uid,login_dt,login_ip) values(%ld"
245       ",now(),'%s')",
246       BBS_uid, hostaddr_client);
247      if (mysql_query (db, sql) != 0)
248        {
249          log_error ("Insert into user_login_log failed\n");
250          return -1;
251        }
252    
253      load_priv (db, &BBS_priv, BBS_uid, BBS_auth_uid,
254        (!BBS_passwd_complex ? S_MAN_M : S_NONE) |
255        (BBS_auth_uid ? S_NONE : S_MAIL));
256    
257      BBS_last_access_tm = BBS_login_tm = time (0);
258      BBS_last_sub_tm = time (0) - 60;
259    
260      sprintf (sql,
261       "update user_pubinfo set visit_count=visit_count+1,"
262       "last_login_dt=now() where uid=%ld", BBS_uid);
263      if (mysql_query (db, sql) != 0)
264        {
265          log_error ("Update user_pubinfo failed\n");
266          return -1;
267        }
268    
269    return 0;    return 0;
270  }  }


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

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