/[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.25 by sysadm, Sat May 10 15:23:42 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                            main.c  -  description                                                    login.c  -  description
3                               -------------------                                                           -------------------
4      begin                : Mon Oct 11 2004          Copyright            : (C) 2004-2025 by Leaflet
5      copyright            : (C) 2004 by Leaflet          Email                : leaflet@leafok.com
     email                : leaflet@leafok.com  
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
11   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
12   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 3 of the License, or     *
13   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #include "login.h"
18    #include "register.h"
19  #include "bbs.h"  #include "bbs.h"
20    #include "user_priv.h"
21    #include "reg_ex.h"
22  #include "common.h"  #include "common.h"
23    #include "log.h"
24    #include "io.h"
25    #include "screen.h"
26    #include "database.h"
27    #include <string.h>
28    #include <mysql.h>
29    #include <regex.h>
30    #include <unistd.h>
31    
32  int  void login_fail()
 bbs_login()  
33  {  {
34    return 0;          display_file(DATA_LOGIN_ERROR);
35    
36            sleep(1);
37    }
38    
39    int bbs_login()
40    {
41            char username[BBS_username_max_len + 1];
42            char password[BBS_password_max_len + 1];
43            int count = 0;
44            int ok = 0;
45    
46            for (; !SYS_server_exit && !ok && count < 3; count++)
47            {
48                    prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', "
49                               "注册请输入`\033[1;31mnew\033[m'): ");
50                    iflush();
51    
52                    if (str_input(username, sizeof(username), DOECHO) < 0)
53                    {
54                            continue;
55                    }
56    
57                    if (strcmp(username, "guest") == 0)
58                    {
59                            MYSQL *db = db_open();
60                            if (db == NULL)
61                            {
62                                    return -1;
63                            }
64    
65                            load_guest_info(db);
66    
67                            mysql_close(db);
68    
69                            return 0;
70                    }
71    
72                    if (strcmp(username, "new") == 0)
73                    {
74                            if (user_register() == 0)
75                            {
76                                    return 0;
77                            }
78                            else
79                            {
80                                    return -2;
81                            }
82                    }
83    
84                    if (username[0] != '\0')
85                    {
86                            prints("\033[1;37m请输入密码\033[m: ");
87                            iflush();
88    
89                            if (str_input(password, sizeof(password), NOECHO) < 0)
90                            {
91                                    continue;
92                            }
93    
94                            MYSQL *db = db_open();
95                            if (db == NULL)
96                            {
97                                    return -1;
98                            }
99    
100                            ok = (check_user(db, username, password) == 0);
101    
102                            mysql_close(db);
103                    }
104            }
105    
106            if (!ok)
107            {
108                    login_fail();
109                    return -1;
110            }
111    
112            return 0;
113    }
114    
115    int check_user(MYSQL *db, char *username, char *password)
116    {
117            MYSQL_RES *rs;
118            MYSQL_ROW row;
119            char sql[SQL_BUFFER_LEN];
120            int ret;
121            long BBS_uid = 0;
122            char client_addr[IP_ADDR_LEN];
123    
124            // Verify format
125            if (ireg("^[A-Za-z][A-Za-z0-9]{2,11}$", username, 0, NULL) != 0 ||
126                    ireg("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0)
127            {
128                    prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");
129                    iflush();
130                    return 1;
131            }
132    
133            // Begin transaction
134            if (mysql_query(db, "SET autocommit=0") != 0)
135            {
136                    log_error("SET autocommit=0 failed\n");
137                    return -1;
138            }
139    
140            if (mysql_query(db, "BEGIN") != 0)
141            {
142                    log_error("Begin transaction failed\n");
143                    return -1;
144            }
145    
146            // Failed login attempts from the same source (subnet /24) during certain time period
147            strncpy(client_addr, hostaddr_client, sizeof(client_addr) - 1);
148            client_addr[sizeof(client_addr) - 1] = '\0';
149    
150            snprintf(sql, sizeof(sql),
151                             "SELECT COUNT(*) AS err_count FROM user_err_login_log "
152                             "WHERE login_dt >= SUBDATE(NOW(), INTERVAL 10 MINUTE) "
153                             "AND login_ip LIKE '%s'",
154                             ip_mask(client_addr, 1, '%'));
155            if (mysql_query(db, sql) != 0)
156            {
157                    log_error("Query user_list failed\n");
158                    return -1;
159            }
160            if ((rs = mysql_store_result(db)) == NULL)
161            {
162                    log_error("Get user_list data failed\n");
163                    return -1;
164            }
165            if ((row = mysql_fetch_row(rs)))
166            {
167                    if (atoi(row[0]) >= 2)
168                    {
169                            mysql_free_result(rs);
170    
171                            prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n");
172                            iflush();
173    
174                            return 1;
175                    }
176            }
177            mysql_free_result(rs);
178    
179            // Failed login attempts against the current username during certain time period
180            snprintf(sql, sizeof(sql),
181                             "SELECT COUNT(*) AS err_count FROM user_err_login_log "
182                             "WHERE username = '%s' AND login_dt >= SUBDATE(NOW(), INTERVAL 1 DAY)",
183                             username);
184            if (mysql_query(db, sql) != 0)
185            {
186                    log_error("Query user_list failed\n");
187                    return -1;
188            }
189            if ((rs = mysql_store_result(db)) == NULL)
190            {
191                    log_error("Get user_list data failed\n");
192                    return -1;
193            }
194            if ((row = mysql_fetch_row(rs)))
195            {
196                    if (atoi(row[0]) >= 5)
197                    {
198                            mysql_free_result(rs);
199    
200                            prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n");
201                            iflush();
202    
203                            return 1;
204                    }
205            }
206            mysql_free_result(rs);
207    
208            snprintf(sql, sizeof(sql),
209                             "SELECT UID, username, p_login FROM user_list "
210                             "WHERE username = '%s' AND password = SHA2('%s', 256) AND enable",
211                             username, password);
212            if (mysql_query(db, sql) != 0)
213            {
214                    log_error("Query user_list failed\n");
215                    return -1;
216            }
217            if ((rs = mysql_store_result(db)) == NULL)
218            {
219                    log_error("Get user_list data failed\n");
220                    return -1;
221            }
222            if ((row = mysql_fetch_row(rs)))
223            {
224                    BBS_uid = atol(row[0]);
225                    strncpy(BBS_username, row[1], sizeof(BBS_username) - 1);
226                    BBS_username[sizeof(BBS_username) - 1] = '\0';
227                    int p_login = atoi(row[2]);
228    
229                    mysql_free_result(rs);
230    
231                    // Add user login log
232                    snprintf(sql, sizeof(sql),
233                                     "INSERT INTO user_login_log(UID, login_dt, login_ip) "
234                                     "VALUES(%ld, NOW(), '%s')",
235                                     BBS_uid, hostaddr_client);
236                    if (mysql_query(db, sql) != 0)
237                    {
238                            log_error("Insert into user_login_log failed\n");
239                            return -1;
240                    }
241    
242                    // Commit transaction
243                    if (mysql_query(db, "COMMIT") != 0)
244                    {
245                            log_error("Commit transaction failed\n");
246                            return -1;
247                    }
248    
249                    if (p_login == 0)
250                    {
251                            mysql_free_result(rs);
252    
253                            prints("\033[1;31m您目前无权登陆...\033[m\r\n");
254                            iflush();
255                            return 1;
256                    }
257            }
258            else
259            {
260                    mysql_free_result(rs);
261    
262                    snprintf(sql, sizeof(sql),
263                                     "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "
264                                     "VALUES('%s', '%s', NOW(), '%s')",
265                                     username, password, hostaddr_client);
266                    if (mysql_query(db, sql) != 0)
267                    {
268                            log_error("Insert into user_err_login_log failed\n");
269                            return -1;
270                    }
271    
272                    // Commit transaction
273                    if (mysql_query(db, "COMMIT") != 0)
274                    {
275                            log_error("Commit transaction failed\n");
276                            return -1;
277                    }
278    
279                    prints("\033[1;31m错误的用户名或密码...\033[m\r\n");
280                    iflush();
281                    return 1;
282            }
283    
284            // Set AUTOCOMMIT = 1
285            if (mysql_query(db, "SET autocommit=1") != 0)
286            {
287                    log_error("SET autocommit=1 failed\n");
288                    return -1;
289            }
290    
291            ret = load_user_info(db, BBS_uid);
292    
293            switch (ret)
294            {
295            case 0: // Login successfully
296                    break;
297            case -1: // Load data error
298                    prints("\033[1;31m读取用户数据错误...\033[m\r\n");
299                    iflush();
300                    return -1;
301            case -2: // Unused
302                    prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");
303                    iflush();
304                    return 1;
305            case -3: // Dead
306                    prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
307                    iflush();
308                    return 1;
309            default:
310                    return -2;
311            }
312    
313            snprintf(sql, sizeof(sql),
314                             "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
315                             "last_login_dt = NOW() WHERE UID = %ld",
316                             BBS_uid);
317            if (mysql_query(db, sql) != 0)
318            {
319                    log_error("Update user_pubinfo failed\n");
320                    return -1;
321            }
322    
323            if (user_online_add(db) != 0)
324            {
325                    return -1;
326            }
327    
328            BBS_last_access_tm = BBS_login_tm = time(0);
329    
330            return 0;
331    }
332    
333    int load_user_info(MYSQL *db, long int BBS_uid)
334    {
335            MYSQL_RES *rs;
336            MYSQL_ROW row;
337            char sql[SQL_BUFFER_LEN];
338            int life;
339            time_t last_login_dt;
340    
341            snprintf(sql, sizeof(sql),
342                             "SELECT life, UNIX_TIMESTAMP(last_login_dt) "
343                             "FROM user_pubinfo WHERE UID = %ld",
344                             BBS_uid);
345            if (mysql_query(db, sql) != 0)
346            {
347                    log_error("Query user_pubinfo failed\n");
348                    return -1;
349            }
350            if ((rs = mysql_store_result(db)) == NULL)
351            {
352                    log_error("Get user_pubinfo data failed\n");
353                    return -1;
354            }
355            if ((row = mysql_fetch_row(rs)))
356            {
357                    life = atoi(row[0]);
358                    last_login_dt = (time_t)atol(row[1]);
359            }
360            else
361            {
362                    mysql_free_result(rs);
363                    return -1; // Data not found
364            }
365            mysql_free_result(rs);
366    
367            if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
368                    time(0) - last_login_dt > 60 * 60 * 24 * life)
369            {
370                    return -3; // Dead
371            }
372    
373            if (load_priv(db, &BBS_priv, BBS_uid) != 0)
374            {
375                    return -1;
376            }
377    
378            return 0;
379    }
380    
381    int load_guest_info(MYSQL *db)
382    {
383            strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);
384            BBS_username[sizeof(BBS_username) - 1] = '\0';
385    
386            if (load_priv(db, &BBS_priv, 0) != 0)
387            {
388                    return -1;
389            }
390    
391            if (user_online_add(db) != 0)
392            {
393                    return -1;
394            }
395    
396            BBS_last_access_tm = BBS_login_tm = time(0);
397    
398            return 0;
399    }
400    
401    int user_online_add(MYSQL *db)
402    {
403            char sql[SQL_BUFFER_LEN];
404    
405            if (user_online_del(db) != 0)
406            {
407                    return -1;
408            }
409    
410            snprintf(sql, sizeof(sql),
411                             "INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) "
412                             "VALUES('Telnet_Process_%d', %ld, '%s', NOW(), NOW())",
413                             getpid(), BBS_priv.uid, hostaddr_client);
414            if (mysql_query(db, sql) != 0)
415            {
416                    log_error("Add user_online failed\n");
417                    return -1;
418            }
419    
420            return 0;
421    }
422    
423    int user_online_del(MYSQL *db)
424    {
425            char sql[SQL_BUFFER_LEN];
426    
427            snprintf(sql, sizeof(sql),
428                             "DELETE FROM user_online WHERE SID = 'Telnet_Process_%d'",
429                             getpid());
430            if (mysql_query(db, sql) != 0)
431            {
432                    log_error("Delete user_online failed\n");
433                    return -1;
434            }
435    
436            return 0;
437  }  }


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

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