/[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.11 by sysadm, Mon Apr 28 03:31:00 2025 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   ***************************************************************************/   ***************************************************************************/
8    
9  /***************************************************************************  /***************************************************************************
# 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  int  void login_fail()
 bbs_login()  
26  {  {
27    return 0;          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 bbs_login()
37    {
38            char username[20], password[20];
39            int count, ok;
40    
41            // Input username
42            count = 0;
43            ok = 0;
44            while (!ok)
45            {
46                    prints("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "
47                               "注册请输入`\033[1;31mnew\033[m'): ");
48                    iflush();
49    
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            return 0;
85    }
86    
87    int check_user(char *username, char *password)
88    {
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            db = (MYSQL *)db_open();
106            if (db == NULL)
107            {
108                    return -1;
109            }
110    
111            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                    sprintf(sql,
145                                    "insert delayed 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            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 load_user_info(MYSQL *db, long int BBS_uid)
194    {
195            MYSQL_RES *rs;
196            MYSQL_ROW row;
197            char sql[1024];
198            long int BBS_auth_uid = 0;
199            int life;
200            time_t last_login_dt;
201    
202            sprintf(sql,
203                            "select life,UNIX_TIMESTAMP(last_login_dt) "
204                            "from user_pubinfo where UID=%ld limit 1",
205                            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()",
236                            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')",
261                            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",
278                            BBS_uid);
279            if (mysql_query(db, sql) != 0)
280            {
281                    log_error("Update user_pubinfo failed\n");
282                    return -1;
283            }
284    
285            return 0;
286    }
287    
288    int 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;
309  }  }


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

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