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


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

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