/[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.44 by sysadm, Mon Jun 16 09:42:28 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 <regex.h>
31    #include <stdlib.h>
32    #include <unistd.h>
33    #include <mysql/mysql.h>
34    
35    int bbs_login(void)
36    {
37            char username[BBS_username_max_len + 1];
38            char password[BBS_password_max_len + 1];
39            int i = 0;
40            int ok = 0;
41    
42            for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++)
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();
56    
57                            return 0;
58                    }
59    
60                    if (strcmp(username, "new") == 0)
61                    {
62                            display_file(DATA_REGISTER, 1);
63    
64                            return 0;
65                    }
66    
67                    if (username[0] != '\0')
68                    {
69                            prints("\033[1;37m请输入密码\033[m: ");
70                            iflush();
71    
72  int                          if (str_input(password, sizeof(password), NOECHO) < 0)
73  bbs_login()                          {
74                                    continue;
75                            }
76    
77                            ok = (check_user(username, password) == 0);
78                            iflush();
79                    }
80            }
81    
82            if (!ok)
83            {
84                    display_file(DATA_LOGIN_ERROR, 1);
85                    return -1;
86            }
87    
88            log_common("User \"%s\"(%ld) login from %s:%d\n",
89                               BBS_username, BBS_priv.uid, hostaddr_client, port_client);
90    
91            return 0;
92    }
93    
94    int check_user(const char *username, const char *password)
95  {  {
96    return 0;          MYSQL *db = NULL;
97            MYSQL_RES *rs = NULL;
98            MYSQL_ROW row;
99            char sql[SQL_BUFFER_LEN];
100            int ret = 0;
101            int BBS_uid = 0;
102            char client_addr[IP_ADDR_LEN];
103            int i;
104            int ok = 1;
105            char user_tz_env[BBS_user_tz_max_len + 2];
106    
107            db = db_open();
108            if (db == NULL)
109            {
110                    ret = -1;
111                    goto cleanup;
112            }
113    
114            // Verify format
115            for (i = 0; ok && username[i] != '\0'; i++)
116            {
117                    if (!(isalpha(username[i]) || (i > 0 && isdigit(username[i]))))
118                    {
119                            ok = 0;
120                    }
121            }
122            if (ok && (i < 3 || i > 12))
123            {
124                    ok = 0;
125            }
126            for (i = 0; ok && password[i] != '\0'; i++)
127            {
128                    if (!isalnum(password[i]))
129                    {
130                            ok = 0;
131                    }
132            }
133            if (ok && (i < 5 || i > 12))
134            {
135                    ok = 0;
136            }
137    
138            if (!ok)
139            {
140                    prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");
141                    ret = 1;
142                    goto cleanup;
143            }
144    
145            // Begin transaction
146            if (mysql_query(db, "SET autocommit=0") != 0)
147            {
148                    log_error("SET autocommit=0 error: %s\n", mysql_error(db));
149                    ret = -1;
150                    goto cleanup;
151            }
152    
153            if (mysql_query(db, "BEGIN") != 0)
154            {
155                    log_error("Begin transaction error: %s\n", mysql_error(db));
156                    ret = -1;
157                    goto cleanup;
158            }
159    
160            // Failed login attempts from the same source (subnet /24) during certain time period
161            strncpy(client_addr, hostaddr_client, sizeof(client_addr) - 1);
162            client_addr[sizeof(client_addr) - 1] = '\0';
163    
164            snprintf(sql, sizeof(sql),
165                             "SELECT COUNT(*) AS err_count FROM user_err_login_log "
166                             "WHERE login_dt >= SUBDATE(NOW(), INTERVAL %d MINUTE) "
167                             "AND login_ip LIKE '%s'",
168                             BBS_login_failures_count_interval,
169                             ip_mask(client_addr, 1, '%'));
170            if (mysql_query(db, sql) != 0)
171            {
172                    log_error("Query user_list error: %s\n", mysql_error(db));
173                    ret = -1;
174                    goto cleanup;
175            }
176            if ((rs = mysql_store_result(db)) == NULL)
177            {
178                    log_error("Get user_list data failed\n");
179                    ret = -1;
180                    goto cleanup;
181            }
182            if ((row = mysql_fetch_row(rs)))
183            {
184                    if (atoi(row[0]) > BBS_allowed_login_failures_within_interval)
185                    {
186                            prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n");
187                            ret = 1;
188                            goto cleanup;
189                    }
190            }
191            mysql_free_result(rs);
192            rs = NULL;
193    
194            // Failed login attempts against the current username during certain time period
195            snprintf(sql, sizeof(sql),
196                             "SELECT COUNT(*) AS err_count FROM user_err_login_log "
197                             "WHERE username = '%s' AND login_dt >= SUBDATE(NOW(), INTERVAL 1 DAY)",
198                             username);
199            if (mysql_query(db, sql) != 0)
200            {
201                    log_error("Query user_list error: %s\n", mysql_error(db));
202                    ret = -1;
203                    goto cleanup;
204            }
205            if ((rs = mysql_store_result(db)) == NULL)
206            {
207                    log_error("Get user_list data failed\n");
208                    ret = -1;
209                    goto cleanup;
210            }
211            if ((row = mysql_fetch_row(rs)))
212            {
213                    if (atoi(row[0]) >= 5)
214                    {
215                            prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n");
216                            ret = 1;
217                            goto cleanup;
218                    }
219            }
220            mysql_free_result(rs);
221            rs = NULL;
222    
223            snprintf(sql, sizeof(sql),
224                             "SELECT UID, username, p_login FROM user_list "
225                             "WHERE username = '%s' AND password = SHA2('%s', 256) AND enable",
226                             username, password);
227            if (mysql_query(db, sql) != 0)
228            {
229                    log_error("Query user_list error: %s\n", mysql_error(db));
230                    ret = -1;
231                    goto cleanup;
232            }
233            if ((rs = mysql_store_result(db)) == NULL)
234            {
235                    log_error("Get user_list data failed\n");
236                    ret = -1;
237                    goto cleanup;
238            }
239            if ((row = mysql_fetch_row(rs)))
240            {
241                    BBS_uid = atoi(row[0]);
242                    strncpy(BBS_username, row[1], sizeof(BBS_username) - 1);
243                    BBS_username[sizeof(BBS_username) - 1] = '\0';
244                    int p_login = atoi(row[2]);
245    
246                    mysql_free_result(rs);
247                    rs = NULL;
248    
249                    // Add user login log
250                    snprintf(sql, sizeof(sql),
251                                     "INSERT INTO user_login_log(UID, login_dt, login_ip) "
252                                     "VALUES(%d, NOW(), '%s')",
253                                     BBS_uid, hostaddr_client);
254                    if (mysql_query(db, sql) != 0)
255                    {
256                            log_error("Insert into user_login_log error: %s\n", mysql_error(db));
257                            ret = -1;
258                            goto cleanup;
259                    }
260    
261                    // Commit transaction
262                    if (mysql_query(db, "COMMIT") != 0)
263                    {
264                            log_error("Commit transaction error: %s\n", mysql_error(db));
265                            ret = -1;
266                            goto cleanup;
267                    }
268    
269                    if (p_login == 0)
270                    {
271                            prints("\033[1;31m您目前无权登陆...\033[m\r\n");
272                            ret = 1;
273                            goto cleanup;
274                    }
275            }
276            else
277            {
278                    mysql_free_result(rs);
279                    rs = NULL;
280    
281                    snprintf(sql, sizeof(sql),
282                                     "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "
283                                     "VALUES('%s', '%s', NOW(), '%s')",
284                                     username, password, hostaddr_client);
285                    if (mysql_query(db, sql) != 0)
286                    {
287                            log_error("Insert into user_err_login_log error: %s\n", mysql_error(db));
288                            ret = -1;
289                            goto cleanup;
290                    }
291    
292                    // Commit transaction
293                    if (mysql_query(db, "COMMIT") != 0)
294                    {
295                            log_error("Commit transaction error: %s\n", mysql_error(db));
296                            ret = -1;
297                            goto cleanup;
298                    }
299    
300                    prints("\033[1;31m错误的用户名或密码...\033[m\r\n");
301                    ret = 1;
302                    goto cleanup;
303            }
304    
305            // Set AUTOCOMMIT = 1
306            if (mysql_query(db, "SET autocommit=1") != 0)
307            {
308                    log_error("SET autocommit=1 error: %s\n", mysql_error(db));
309                    ret = -1;
310                    goto cleanup;
311            }
312    
313            ret = load_user_info(db, BBS_uid);
314    
315            switch (ret)
316            {
317            case 0: // Login successfully
318                    break;
319            case -1: // Load data error
320                    prints("\033[1;31m读取用户数据错误...\033[m\r\n");
321                    ret = -1;
322                    goto cleanup;
323            case -2: // Unused
324                    prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");
325                    ret = 1;
326                    goto cleanup;
327            case -3: // Dead
328                    prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
329                    ret = 1;
330                    goto cleanup;
331            default:
332                    ret = -2;
333                    goto cleanup;
334            }
335    
336            snprintf(sql, sizeof(sql),
337                             "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
338                             "last_login_dt = NOW() WHERE UID = %d",
339                             BBS_uid);
340            if (mysql_query(db, sql) != 0)
341            {
342                    log_error("Update user_pubinfo error: %s\n", mysql_error(db));
343                    ret = -1;
344                    goto cleanup;
345            }
346    
347            if (user_online_add(db) != 0)
348            {
349                    ret = -1;
350                    goto cleanup;
351            }
352    
353            BBS_last_access_tm = BBS_login_tm = time(0);
354    
355            // Set user tz to process env
356            if (BBS_user_tz[0] != '\0')
357            {
358                    user_tz_env[0] = ':';
359                    strncpy(user_tz_env + 1, BBS_user_tz, sizeof(user_tz_env) - 2);
360                    user_tz_env[sizeof(user_tz_env) - 1] = '\0';
361    
362                    if (setenv("TZ", user_tz_env, 1) == -1)
363                    {
364                            log_error("setenv(TZ = %s) error %d\n", user_tz_env, errno);
365                            return -3;
366                    }
367    
368                    tzset();
369            }
370    
371    cleanup:
372            mysql_free_result(rs);
373            mysql_close(db);
374    
375            return ret;
376    }
377    
378    int load_user_info(MYSQL *db, int BBS_uid)
379    {
380            MYSQL_RES *rs = NULL;
381            MYSQL_ROW row;
382            char sql[SQL_BUFFER_LEN];
383            int life;
384            time_t last_login_dt;
385            int ret = 0;
386    
387            snprintf(sql, sizeof(sql),
388                             "SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone, exp, nickname "
389                             "FROM user_pubinfo WHERE UID = %d",
390                             BBS_uid);
391            if (mysql_query(db, sql) != 0)
392            {
393                    log_error("Query user_pubinfo error: %s\n", mysql_error(db));
394                    ret = -1;
395                    goto cleanup;
396            }
397            if ((rs = mysql_store_result(db)) == NULL)
398            {
399                    log_error("Get user_pubinfo data failed\n");
400                    ret = -1;
401                    goto cleanup;
402            }
403            if ((row = mysql_fetch_row(rs)))
404            {
405                    life = atoi(row[0]);
406                    last_login_dt = (time_t)atol(row[1]);
407    
408                    strncpy(BBS_user_tz, row[2], sizeof(BBS_user_tz) - 1);
409                    BBS_user_tz[sizeof(BBS_user_tz) - 1] = '\0';
410    
411                    BBS_user_exp = atoi(row[3]);
412    
413                    strncpy(BBS_nickname, row[4], sizeof(BBS_nickname));
414                    BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
415            }
416            else
417            {
418                    ret = -1; // Data not found
419                    goto cleanup;
420            }
421            mysql_free_result(rs);
422            rs = NULL;
423    
424            if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
425                    time(0) - last_login_dt > 60 * 60 * 24 * life)
426            {
427                    ret = -3; // Dead
428                    goto cleanup;
429            }
430    
431            if (load_priv(db, &BBS_priv, BBS_uid) != 0)
432            {
433                    ret = -1;
434                    goto cleanup;
435            }
436    
437    cleanup:
438            mysql_free_result(rs);
439    
440            return ret;
441    }
442    
443    int load_guest_info(void)
444    {
445            MYSQL *db = NULL;
446            int ret = 0;
447    
448            db = db_open();
449            if (db == NULL)
450            {
451                    ret = -1;
452                    goto cleanup;
453            }
454    
455            strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);
456            BBS_username[sizeof(BBS_username) - 1] = '\0';
457    
458            BBS_user_exp = 0;
459    
460            strncpy(BBS_nickname, "Guest", sizeof(BBS_nickname));
461            BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
462    
463            if (load_priv(db, &BBS_priv, 0) != 0)
464            {
465                    ret = -1;
466                    goto cleanup;
467            }
468    
469            if (user_online_add(db) != 0)
470            {
471                    ret = -1;
472                    goto cleanup;
473            }
474    
475            BBS_last_access_tm = BBS_login_tm = time(0);
476    
477    cleanup:
478            mysql_close(db);
479    
480            return ret;
481    }
482    
483    int user_online_add(MYSQL *db)
484    {
485            char sql[SQL_BUFFER_LEN];
486    
487            if (user_online_del(db) != 0)
488            {
489                    return -1;
490            }
491    
492            snprintf(sql, sizeof(sql),
493                             "INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) "
494                             "VALUES('Telnet_Process_%d', %d, '%s', NOW(), NOW())",
495                             getpid(), BBS_priv.uid, hostaddr_client);
496            if (mysql_query(db, sql) != 0)
497            {
498                    log_error("Add user_online error: %s\n", mysql_error(db));
499                    return -1;
500            }
501    
502            return 0;
503    }
504    
505    int user_online_del(MYSQL *db)
506    {
507            char sql[SQL_BUFFER_LEN];
508    
509            snprintf(sql, sizeof(sql),
510                             "DELETE FROM user_online WHERE SID = 'Telnet_Process_%d'",
511                             getpid());
512            if (mysql_query(db, sql) != 0)
513            {
514                    log_error("Delete user_online error: %s\n", mysql_error(db));
515                    return -1;
516            }
517    
518            return 0;
519  }  }


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

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