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


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

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