| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
#include "login.h" |
#include "login.h" |
|
#include "register.h" |
|
| 18 |
#include "bbs.h" |
#include "bbs.h" |
| 19 |
#include "user_priv.h" |
#include "user_priv.h" |
|
#include "reg_ex.h" |
|
| 20 |
#include "common.h" |
#include "common.h" |
| 21 |
#include "log.h" |
#include "log.h" |
| 22 |
#include "io.h" |
#include "io.h" |
| 23 |
#include "screen.h" |
#include "screen.h" |
| 24 |
#include "database.h" |
#include "database.h" |
| 25 |
|
#include <ctype.h> |
| 26 |
#include <string.h> |
#include <string.h> |
| 27 |
#include <mysql.h> |
#include <mysql.h> |
| 28 |
#include <regex.h> |
#include <regex.h> |
| 29 |
#include <unistd.h> |
#include <unistd.h> |
| 30 |
|
|
| 31 |
void login_fail() |
int bbs_login(MYSQL *db) |
|
{ |
|
|
display_file(DATA_LOGIN_ERROR); |
|
|
|
|
|
sleep(1); |
|
|
} |
|
|
|
|
|
int bbs_login() |
|
| 32 |
{ |
{ |
| 33 |
char username[BBS_username_max_len + 1]; |
char username[BBS_username_max_len + 1]; |
| 34 |
char password[BBS_password_max_len + 1]; |
char password[BBS_password_max_len + 1]; |
| 35 |
int count = 0; |
int count = 0; |
| 36 |
int ok = 0; |
int ok = 0; |
| 37 |
|
|
| 38 |
// Input username |
for (; !SYS_server_exit && !ok && count < 3; count++) |
|
while (!ok) |
|
| 39 |
{ |
{ |
| 40 |
prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', " |
prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', " |
| 41 |
"注册请输入`\033[1;31mnew\033[m'): "); |
"注册请输入`\033[1;31mnew\033[m'): "); |
| 42 |
iflush(); |
iflush(); |
| 43 |
|
|
| 44 |
str_input(username, sizeof(username), DOECHO); |
if (str_input(username, sizeof(username), DOECHO) < 0) |
| 45 |
count++; |
{ |
| 46 |
|
continue; |
| 47 |
|
} |
| 48 |
|
|
| 49 |
if (strcmp(username, "guest") == 0) |
if (strcmp(username, "guest") == 0) |
| 50 |
{ |
{ |
|
MYSQL *db = db_open(); |
|
|
if (db == NULL) |
|
|
{ |
|
|
return -1; |
|
|
} |
|
|
|
|
| 51 |
load_guest_info(db); |
load_guest_info(db); |
| 52 |
|
|
|
mysql_close(db); |
|
|
|
|
| 53 |
return 0; |
return 0; |
| 54 |
} |
} |
| 55 |
|
|
| 56 |
if (strcmp(username, "new") == 0) |
if (strcmp(username, "new") == 0) |
| 57 |
{ |
{ |
| 58 |
if (user_register() == 0) |
display_file_ex(DATA_REGISTER, 1, 1); |
| 59 |
{ |
|
| 60 |
return 0; |
return 0; |
|
} |
|
|
else |
|
|
{ |
|
|
return -2; |
|
|
} |
|
| 61 |
} |
} |
| 62 |
|
|
| 63 |
if (username[0] != '\0') |
if (username[0] != '\0') |
| 64 |
{ |
{ |
|
// Input password |
|
| 65 |
prints("\033[1;37m请输入密码\033[m: "); |
prints("\033[1;37m请输入密码\033[m: "); |
| 66 |
iflush(); |
iflush(); |
| 67 |
|
|
| 68 |
str_input(password, sizeof(password), NOECHO); |
if (str_input(password, sizeof(password), NOECHO) < 0) |
|
|
|
|
MYSQL *db = db_open(); |
|
|
if (db == NULL) |
|
| 69 |
{ |
{ |
| 70 |
return -1; |
continue; |
| 71 |
} |
} |
| 72 |
|
|
| 73 |
ok = (check_user(db, username, password) == 0); |
ok = (check_user(db, username, password) == 0); |
|
|
|
|
mysql_close(db); |
|
|
} |
|
|
if (count >= 3 && !ok) |
|
|
{ |
|
|
login_fail(); |
|
|
return -1; |
|
| 74 |
} |
} |
| 75 |
} |
} |
| 76 |
|
|
| 77 |
|
if (!ok) |
| 78 |
|
{ |
| 79 |
|
display_file_ex(DATA_LOGIN_ERROR, 1, 0); |
| 80 |
|
return -1; |
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
log_std("User \"%s\"(%ld) login from %s:%d\n", |
| 84 |
|
BBS_username, BBS_priv.uid, hostaddr_client, port_client); |
| 85 |
|
|
| 86 |
return 0; |
return 0; |
| 87 |
} |
} |
| 88 |
|
|
| 94 |
int ret; |
int ret; |
| 95 |
long BBS_uid = 0; |
long BBS_uid = 0; |
| 96 |
char client_addr[IP_ADDR_LEN]; |
char client_addr[IP_ADDR_LEN]; |
| 97 |
|
int i; |
| 98 |
|
int ok = 1; |
| 99 |
|
|
| 100 |
// Verify format |
// Verify format |
| 101 |
if (ireg("^[A-Za-z][A-Za-z0-9]{2,11}$", username, 0, NULL) != 0 || |
for (i = 0; ok && username[i] != '\0'; i++) |
| 102 |
ireg("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0) |
{ |
| 103 |
|
if (!(isalpha(username[i]) || (i > 0 && isdigit(username[i])))) |
| 104 |
|
{ |
| 105 |
|
ok = 0; |
| 106 |
|
} |
| 107 |
|
} |
| 108 |
|
if (ok && (i < 3 || i > 12)) |
| 109 |
|
{ |
| 110 |
|
ok = 0; |
| 111 |
|
} |
| 112 |
|
for (i = 0; ok && password[i] != '\0'; i++) |
| 113 |
|
{ |
| 114 |
|
if (!isalnum(password[i])) |
| 115 |
|
{ |
| 116 |
|
ok = 0; |
| 117 |
|
} |
| 118 |
|
} |
| 119 |
|
if (ok && (i < 5 || i > 12)) |
| 120 |
|
{ |
| 121 |
|
ok = 0; |
| 122 |
|
} |
| 123 |
|
|
| 124 |
|
if (!ok) |
| 125 |
{ |
{ |
| 126 |
prints("\033[1;31m用户名或密码格式错误...\033[m\r\n"); |
prints("\033[1;31m用户名或密码格式错误...\033[m\r\n"); |
| 127 |
iflush(); |
iflush(); |