/[LeafOK_CVS]/lbbs/src/login.c
ViewVC logotype

Contents of /lbbs/src/login.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.75 - (show annotations)
Fri Dec 19 06:16:27 2025 UTC (2 months, 4 weeks ago) by sysadm
Branch: MAIN
Changes since 1.74: +24 -24 lines
Content type: text/x-csrc
Append \n to the end of logging message by log_...()
Remove ending \n from each logging message

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * login
4 * - user authentication and online status manager
5 *
6 * Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include "bbs.h"
14 #include "common.h"
15 #include "database.h"
16 #include "io.h"
17 #include "ip_mask.h"
18 #include "log.h"
19 #include "login.h"
20 #include "screen.h"
21 #include "user_priv.h"
22 #include <ctype.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <regex.h>
27 #include <unistd.h>
28 #include <mysql.h>
29 #include <sys/param.h>
30
31 static const int BBS_username_min_len = 3; // common len = 5, special len = 3
32 static const int BBS_password_min_len = 5; // legacy len = 5, current len = 6
33
34 static const int BBS_allowed_login_failures_within_interval = 10;
35 static const int BBS_login_failures_count_interval = 10; // minutes
36 static const int BBS_allowed_login_failures_per_account = 3;
37
38 const int BBS_login_retry_times = 3;
39
40 int bbs_login(void)
41 {
42 char username[BBS_username_max_len + 1];
43 char password[BBS_password_max_len + 1];
44 int i = 0;
45 int ok = 0;
46 int ret;
47
48 for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++)
49 {
50 prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', "
51 "注册请输入`\033[1;31mnew\033[m'): ");
52 iflush();
53
54 if (str_input(username, sizeof(username), DOECHO) < 0)
55 {
56 continue;
57 }
58
59 if (strcmp(username, "guest") == 0)
60 {
61 load_guest_info();
62
63 return 0;
64 }
65
66 if (strcmp(username, "new") == 0)
67 {
68 display_file(DATA_REGISTER, 1);
69
70 return -1;
71 }
72
73 if (username[0] != '\0')
74 {
75 prints("\033[1;37m请输入密码\033[m: ");
76 iflush();
77
78 if (str_input(password, sizeof(password), NOECHO) < 0)
79 {
80 continue;
81 }
82
83 ret = check_user(username, password);
84 if (ret == 2) // Enforce update user agreement
85 {
86 BBS_update_eula = 1;
87 ret = 0;
88 }
89
90 ok = (ret == 0);
91 iflush();
92 }
93 }
94
95 if (!ok)
96 {
97 display_file(DATA_LOGIN_ERROR, 1);
98 return -1;
99 }
100
101 return 0;
102 }
103
104 int check_user(const char *username, const char *password)
105 {
106 MYSQL *db = NULL;
107 MYSQL_RES *rs = NULL;
108 MYSQL_ROW row;
109 char sql[SQL_BUFFER_LEN];
110 int ret = 0;
111 int BBS_uid = 0;
112 char client_addr[IP_ADDR_LEN];
113 int i;
114 int ok = 1;
115 char user_tz_env[BBS_user_tz_max_len + 2];
116
117 db = db_open();
118 if (db == NULL)
119 {
120 ret = -1;
121 goto cleanup;
122 }
123
124 // Verify format
125 for (i = 0; ok && username[i] != '\0'; i++)
126 {
127 if (!(isalpha((int)username[i]) || (i > 0 && (isdigit((int)username[i]) || username[i] == '_'))))
128 {
129 ok = 0;
130 }
131 }
132 if (ok && (i < BBS_username_min_len || i > BBS_username_max_len))
133 {
134 ok = 0;
135 }
136 for (i = 0; ok && password[i] != '\0'; i++)
137 {
138 if (!isalnum((int)password[i]))
139 {
140 ok = 0;
141 }
142 }
143 if (ok && (i < BBS_password_min_len || i > BBS_password_max_len))
144 {
145 ok = 0;
146 }
147
148 if (!ok)
149 {
150 prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");
151 ret = 1;
152 goto cleanup;
153 }
154
155 // Begin transaction
156 if (mysql_query(db, "SET autocommit=0") != 0)
157 {
158 log_error("SET autocommit=0 error: %s", mysql_error(db));
159 ret = -1;
160 goto cleanup;
161 }
162
163 if (mysql_query(db, "BEGIN") != 0)
164 {
165 log_error("Begin transaction error: %s", mysql_error(db));
166 ret = -1;
167 goto cleanup;
168 }
169
170 // Failed login attempts from the same source (subnet /24) during certain time period
171 strncpy(client_addr, hostaddr_client, sizeof(client_addr) - 1);
172 client_addr[sizeof(client_addr) - 1] = '\0';
173
174 snprintf(sql, sizeof(sql),
175 "SELECT COUNT(*) AS err_count FROM user_err_login_log "
176 "WHERE login_dt >= SUBDATE(NOW(), INTERVAL %d MINUTE) "
177 "AND login_ip LIKE '%s'",
178 BBS_login_failures_count_interval,
179 ip_mask(client_addr, 1, '%'));
180 if (mysql_query(db, sql) != 0)
181 {
182 log_error("Query user_list error: %s", mysql_error(db));
183 ret = -1;
184 goto cleanup;
185 }
186 if ((rs = mysql_store_result(db)) == NULL)
187 {
188 log_error("Get user_list data failed");
189 ret = -1;
190 goto cleanup;
191 }
192 if ((row = mysql_fetch_row(rs)))
193 {
194 if (atoi(row[0]) >= BBS_allowed_login_failures_within_interval)
195 {
196 prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试,或使用Web方式访问\033[m\r\n");
197 ret = 1;
198 goto cleanup;
199 }
200 }
201 mysql_free_result(rs);
202 rs = NULL;
203
204 // Failed login attempts against the current username since last successful login
205 snprintf(sql, sizeof(sql),
206 "SELECT COUNT(*) AS err_count FROM user_err_login_log "
207 "LEFT JOIN user_list ON user_err_login_log.username = user_list.username "
208 "LEFT JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID "
209 "WHERE user_err_login_log.username = '%s' "
210 "AND (user_err_login_log.login_dt >= user_pubinfo.last_login_dt "
211 "OR user_pubinfo.last_login_dt IS NULL)",
212 username);
213 if (mysql_query(db, sql) != 0)
214 {
215 log_error("Query user_list error: %s", mysql_error(db));
216 ret = -1;
217 goto cleanup;
218 }
219 if ((rs = mysql_store_result(db)) == NULL)
220 {
221 log_error("Get user_list data failed");
222 ret = -1;
223 goto cleanup;
224 }
225 if ((row = mysql_fetch_row(rs)))
226 {
227 if (atoi(row[0]) >= BBS_allowed_login_failures_per_account)
228 {
229 prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录解锁\033[m\r\n");
230 ret = 1;
231 goto cleanup;
232 }
233 }
234 mysql_free_result(rs);
235 rs = NULL;
236
237 snprintf(sql, sizeof(sql),
238 "SELECT UID, username, p_login FROM user_list "
239 "WHERE username = '%s' AND password = SHA2('%s', 256) AND enable",
240 username, password);
241 if (mysql_query(db, sql) != 0)
242 {
243 log_error("Query user_list error: %s", mysql_error(db));
244 ret = -1;
245 goto cleanup;
246 }
247 if ((rs = mysql_store_result(db)) == NULL)
248 {
249 log_error("Get user_list data failed");
250 ret = -1;
251 goto cleanup;
252 }
253 if ((row = mysql_fetch_row(rs)))
254 {
255 BBS_uid = atoi(row[0]);
256 strncpy(BBS_username, row[1], sizeof(BBS_username) - 1);
257 BBS_username[sizeof(BBS_username) - 1] = '\0';
258 int p_login = atoi(row[2]);
259
260 mysql_free_result(rs);
261 rs = NULL;
262
263 // Add user login log
264 snprintf(sql, sizeof(sql),
265 "INSERT INTO user_login_log(UID, login_dt, login_ip) "
266 "VALUES(%d, NOW(), '%s')",
267 BBS_uid, hostaddr_client);
268 if (mysql_query(db, sql) != 0)
269 {
270 log_error("Insert into user_login_log error: %s", mysql_error(db));
271 ret = -1;
272 goto cleanup;
273 }
274
275 // Commit transaction
276 if (mysql_query(db, "COMMIT") != 0)
277 {
278 log_error("Commit transaction error: %s", mysql_error(db));
279 ret = -1;
280 goto cleanup;
281 }
282
283 if (p_login == 0)
284 {
285 prints("\033[1;31m您目前无权登陆...\033[m\r\n");
286 ret = 1;
287 goto cleanup;
288 }
289 }
290 else
291 {
292 mysql_free_result(rs);
293 rs = NULL;
294
295 snprintf(sql, sizeof(sql),
296 "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "
297 "VALUES('%s', '%s', NOW(), '%s')",
298 username, password, hostaddr_client);
299 if (mysql_query(db, sql) != 0)
300 {
301 log_error("Insert into user_err_login_log error: %s", mysql_error(db));
302 ret = -1;
303 goto cleanup;
304 }
305
306 // Commit transaction
307 if (mysql_query(db, "COMMIT") != 0)
308 {
309 log_error("Commit transaction error: %s", mysql_error(db));
310 ret = -1;
311 goto cleanup;
312 }
313
314 prints("\033[1;31m错误的用户名或密码...\033[m\r\n");
315 ret = 1;
316 goto cleanup;
317 }
318
319 // Set AUTOCOMMIT = 1
320 if (mysql_query(db, "SET autocommit=1") != 0)
321 {
322 log_error("SET autocommit=1 error: %s", mysql_error(db));
323 ret = -1;
324 goto cleanup;
325 }
326
327 if (!SSH_v2 && checklevel2(&BBS_priv, P_MAN_S))
328 {
329 prints("\033[1;31m非普通账户必须使用SSH方式登录\033[m\r\n");
330 ret = 1;
331 goto cleanup;
332 }
333
334 ret = load_user_info(db, BBS_uid);
335
336 switch (ret)
337 {
338 case 0: // Login successfully
339 break;
340 case -1: // Load data error
341 prints("\033[1;31m读取用户数据错误...\033[m\r\n");
342 ret = -1;
343 goto cleanup;
344 case -2: // Enforce update user agreement
345 ret = 2;
346 goto cleanup;
347 case -3: // Dead
348 prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
349 ret = 1;
350 goto cleanup;
351 default:
352 ret = -2;
353 goto cleanup;
354 }
355
356 snprintf(sql, sizeof(sql),
357 "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
358 "last_login_dt = NOW() WHERE UID = %d",
359 BBS_uid);
360 if (mysql_query(db, sql) != 0)
361 {
362 log_error("Update user_pubinfo error: %s", mysql_error(db));
363 ret = -1;
364 goto cleanup;
365 }
366
367 if (user_online_add(db) != 0)
368 {
369 ret = -1;
370 goto cleanup;
371 }
372
373 BBS_last_access_tm = BBS_login_tm = time(NULL);
374
375 // Set user tz to process env
376 if (BBS_user_tz[0] != '\0')
377 {
378 user_tz_env[0] = ':';
379 strncpy(user_tz_env + 1, BBS_user_tz, sizeof(user_tz_env) - 2);
380 user_tz_env[sizeof(user_tz_env) - 1] = '\0';
381
382 if (setenv("TZ", user_tz_env, 1) == -1)
383 {
384 log_error("setenv(TZ = %s) error %d", user_tz_env, errno);
385 return -3;
386 }
387
388 tzset();
389 }
390
391 cleanup:
392 mysql_free_result(rs);
393 mysql_close(db);
394
395 return ret;
396 }
397
398 int load_user_info(MYSQL *db, int BBS_uid)
399 {
400 MYSQL_RES *rs = NULL;
401 MYSQL_ROW row;
402 char sql[SQL_BUFFER_LEN];
403 int life;
404 time_t last_login_dt;
405 int ret = 0;
406
407 snprintf(sql, sizeof(sql),
408 "SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone, exp, nickname "
409 "FROM user_pubinfo WHERE UID = %d",
410 BBS_uid);
411 if (mysql_query(db, sql) != 0)
412 {
413 log_error("Query user_pubinfo error: %s", mysql_error(db));
414 ret = -1;
415 goto cleanup;
416 }
417 if ((rs = mysql_store_result(db)) == NULL)
418 {
419 log_error("Get user_pubinfo data failed");
420 ret = -1;
421 goto cleanup;
422 }
423 if ((row = mysql_fetch_row(rs)))
424 {
425 life = atoi(row[0]);
426 last_login_dt = (time_t)atol(row[1]);
427
428 strncpy(BBS_user_tz, row[2], sizeof(BBS_user_tz) - 1);
429 BBS_user_tz[sizeof(BBS_user_tz) - 1] = '\0';
430
431 BBS_user_exp = atoi(row[3]);
432
433 strncpy(BBS_nickname, row[4], sizeof(BBS_nickname));
434 BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
435 }
436 else
437 {
438 ret = -1; // Data not found
439 goto cleanup;
440 }
441 mysql_free_result(rs);
442 rs = NULL;
443
444 if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
445 time(NULL) - last_login_dt > 60 * 60 * 24 * life)
446 {
447 ret = -3; // Dead
448 goto cleanup;
449 }
450
451 if (load_priv(db, &BBS_priv, BBS_uid) != 0)
452 {
453 ret = -1; // Data not found
454 goto cleanup;
455 }
456
457 if (last_login_dt < BBS_eula_tm)
458 {
459 ret = -2; // require update agreement first
460 goto cleanup;
461 }
462
463 cleanup:
464 mysql_free_result(rs);
465
466 return ret;
467 }
468
469 int load_guest_info(void)
470 {
471 MYSQL *db = NULL;
472 int ret = 0;
473
474 db = db_open();
475 if (db == NULL)
476 {
477 ret = -1;
478 goto cleanup;
479 }
480
481 strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);
482 BBS_username[sizeof(BBS_username) - 1] = '\0';
483
484 BBS_user_exp = 0;
485
486 strncpy(BBS_nickname, "Guest", sizeof(BBS_nickname));
487 BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
488
489 if (load_priv(db, &BBS_priv, 0) != 0)
490 {
491 ret = -1;
492 goto cleanup;
493 }
494
495 if (user_online_add(db) != 0)
496 {
497 ret = -1;
498 goto cleanup;
499 }
500
501 BBS_last_access_tm = BBS_login_tm = time(NULL);
502
503 cleanup:
504 mysql_close(db);
505
506 return ret;
507 }
508
509 int user_online_add(MYSQL *db)
510 {
511 char sql[SQL_BUFFER_LEN];
512
513 snprintf(sql, sizeof(sql),
514 "INSERT INTO visit_log(dt, IP) VALUES(NOW(), '%s')",
515 hostaddr_client);
516 if (mysql_query(db, sql) != 0)
517 {
518 log_error("Add visit log error: %s", mysql_error(db));
519 return -1;
520 }
521
522 if (user_online_del(db) != 0)
523 {
524 return -2;
525 }
526
527 snprintf(sql, sizeof(sql),
528 "INSERT INTO user_online(SID, UID, ip, current_action, login_tm, last_tm) "
529 "VALUES('Telnet_Process_%d', %d, '%s', 'LOGIN', NOW(), NOW())",
530 getpid(), BBS_priv.uid, hostaddr_client);
531 if (mysql_query(db, sql) != 0)
532 {
533 log_error("Add user_online error: %s", mysql_error(db));
534 return -3;
535 }
536
537 return 0;
538 }
539
540 int user_online_del(MYSQL *db)
541 {
542 char sql[SQL_BUFFER_LEN];
543
544 snprintf(sql, sizeof(sql),
545 "DELETE FROM user_online WHERE SID = 'Telnet_Process_%d'",
546 getpid());
547 if (mysql_query(db, sql) != 0)
548 {
549 log_error("Delete user_online error: %s", mysql_error(db));
550 return -1;
551 }
552
553 return 0;
554 }
555
556 int user_online_exp(MYSQL *db)
557 {
558 char sql[SQL_BUFFER_LEN];
559
560 // +1 exp for every 5 minutes online since last logout
561 // but at most 24 hours worth of exp can be gained in Telnet session
562 snprintf(sql, sizeof(sql),
563 "UPDATE user_pubinfo SET exp = exp + FLOOR(LEAST(TIMESTAMPDIFF("
564 "SECOND, GREATEST(last_login_dt, IF(last_logout_dt IS NULL, last_login_dt, last_logout_dt)), NOW()"
565 ") / 60 / 5, 12 * 24)), last_logout_dt = NOW() "
566 "WHERE UID = %d",
567 BBS_priv.uid);
568 if (mysql_query(db, sql) != 0)
569 {
570 log_error("Update user_pubinfo error: %s", mysql_error(db));
571 return -1;
572 }
573
574 return 0;
575 }
576
577 int user_online_update(const char *action)
578 {
579 MYSQL *db = NULL;
580 char sql[SQL_BUFFER_LEN];
581
582 if ((action == NULL || strcmp(BBS_current_action, action) == 0) &&
583 time(NULL) - BBS_current_action_tm < BBS_current_action_refresh_interval) // No change
584 {
585 return 0;
586 }
587
588 if (action != NULL)
589 {
590 strncpy(BBS_current_action, action, sizeof(BBS_current_action) - 1);
591 BBS_current_action[sizeof(BBS_current_action) - 1] = '\0';
592 }
593
594 BBS_current_action_tm = time(NULL);
595
596 db = db_open();
597 if (db == NULL)
598 {
599 log_error("db_open() error: %s", mysql_error(db));
600 return -1;
601 }
602
603 snprintf(sql, sizeof(sql),
604 "UPDATE user_online SET current_action = '%s', last_tm = NOW() "
605 "WHERE SID = 'Telnet_Process_%d'",
606 BBS_current_action, getpid());
607 if (mysql_query(db, sql) != 0)
608 {
609 log_error("Update user_online error: %s", mysql_error(db));
610 return -2;
611 }
612
613 mysql_close(db);
614
615 return 1;
616 }
617
618 int user_update_agreement(void)
619 {
620 MYSQL *db = NULL;
621 char sql[SQL_BUFFER_LEN];
622 int ch;
623 int ret = 0;
624
625 clearscr();
626 moveto(2, 1);
627 prints("尊敬的用户:");
628 moveto(3, 1);
629 prints(" 本站的《用户许可协议》已更新,您必须在仔细阅读并接受后,才能继续使用本站提供的服务。");
630 press_any_key();
631
632 clearscr();
633 display_file(DATA_EULA, 1);
634
635 while (!SYS_server_exit)
636 {
637 clearscr();
638 moveto(2, 1);
639
640 ch = press_any_key_ex("您是否接受本站的《用户许可协议》? (A)接受, (D)拒绝, (R)再看看协议 [D]", 60);
641 switch (toupper(ch))
642 {
643 case KEY_NULL:
644 return -1;
645 case KEY_TIMEOUT:
646 case CR:
647 case 'D':
648 moveto(3, 1);
649 prints("您已拒绝《用户许可协议》,再见!");
650 press_any_key();
651 return 0;
652 case 'R':
653 clearscr();
654 display_file(DATA_EULA, 1);
655 continue;
656 case 'A':
657 db = db_open();
658 if (db == NULL)
659 {
660 ret = -1;
661 goto cleanup;
662 }
663
664 snprintf(sql, sizeof(sql),
665 "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
666 "last_login_dt = NOW() WHERE UID = %d",
667 BBS_priv.uid);
668 if (mysql_query(db, sql) != 0)
669 {
670 log_error("Update user_pubinfo error: %s", mysql_error(db));
671 ret = -1;
672 goto cleanup;
673 }
674
675 mysql_close(db);
676 db = NULL;
677
678 moveto(3, 1);
679 prints("您已接受《用户许可协议》,请重新登陆。");
680 press_any_key();
681 return 1;
682 default:
683 continue;
684 }
685
686 break;
687 }
688
689 cleanup:
690 mysql_close(db);
691
692 return ret;
693 }

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