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


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

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