| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
login.c - description |
login.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 20 2004 |
begin : Mon Oct 20 2004 |
| 5 |
copyright : (C) 2004 by Leaflet |
copyright : (C) 2004 by Leaflet |
| 6 |
email : leaflet@leafok.com |
email : leaflet@leafok.com |
| 7 |
***************************************************************************/ |
***************************************************************************/ |
| 8 |
|
|
| 9 |
/*************************************************************************** |
/*************************************************************************** |
| 18 |
#include "bbs.h" |
#include "bbs.h" |
| 19 |
#include "common.h" |
#include "common.h" |
| 20 |
#include "io.h" |
#include "io.h" |
| 21 |
|
#include "database.h" |
| 22 |
#include <mysql.h> |
#include <mysql.h> |
| 23 |
#include <regex.h> |
#include <regex.h> |
| 24 |
|
|
| 25 |
void |
void login_fail() |
|
login_fail () |
|
| 26 |
{ |
{ |
| 27 |
char temp[256]; |
char temp[256]; |
| 28 |
|
|
| 29 |
strcpy (temp, app_home_dir); |
strcpy(temp, app_home_dir); |
| 30 |
strcat (temp, "data/login_error.dat"); |
strcat(temp, "data/login_error.txt"); |
| 31 |
display_file (temp); |
display_file(temp); |
| 32 |
|
|
| 33 |
sleep (1); |
sleep(1); |
| 34 |
} |
} |
| 35 |
|
|
| 36 |
int |
int bbs_login() |
|
bbs_login () |
|
| 37 |
{ |
{ |
| 38 |
char username[20], password[20]; |
char username[20], password[20]; |
| 39 |
int count, ok; |
int count, ok; |
| 40 |
|
|
| 41 |
//Input username |
// Input username |
| 42 |
count = 0; |
count = 0; |
| 43 |
ok = 0; |
ok = 0; |
| 44 |
while (!ok) |
while (!ok) |
| 45 |
{ |
{ |
| 46 |
prints |
prints("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', " |
| 47 |
("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', " |
"注册请输入`\033[1;31mnew\033[m'): "); |
| 48 |
"注册请输入`\033[1;31mnew\033[m'): "); |
iflush(); |
| 49 |
iflush (); |
|
| 50 |
|
str_input(username, 19, DOECHO); |
| 51 |
|
count++; |
| 52 |
|
|
| 53 |
|
if (strcmp(username, "guest") == 0) |
| 54 |
|
{ |
| 55 |
|
load_guest_info(); |
| 56 |
|
return 0; |
| 57 |
|
} |
| 58 |
|
|
| 59 |
|
if (strcmp(username, "new") == 0) |
| 60 |
|
{ |
| 61 |
|
if (user_register() == 0) |
| 62 |
|
return 0; |
| 63 |
|
else |
| 64 |
|
return -2; |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
if (strlen(username) > 0) |
| 68 |
|
{ |
| 69 |
|
// Input password |
| 70 |
|
prints("\033[1;37m请输入密码\033[m: "); |
| 71 |
|
iflush(); |
| 72 |
|
|
| 73 |
|
str_input(password, 19, NOECHO); |
| 74 |
|
|
| 75 |
|
ok = (check_user(username, password) == 0); |
| 76 |
|
} |
| 77 |
|
if (count >= 3 && !ok) |
| 78 |
|
{ |
| 79 |
|
login_fail(); |
| 80 |
|
return -1; |
| 81 |
|
} |
| 82 |
|
} |
| 83 |
|
|
| 84 |
str_input (username, 19, 0); |
return 0; |
| 85 |
count++; |
} |
| 86 |
|
|
| 87 |
if (strcmp (username, "guest") == 0) |
int check_user(char *username, char *password) |
| 88 |
return 1; |
{ |
| 89 |
|
MYSQL *db; |
| 90 |
|
MYSQL_RES *rs; |
| 91 |
|
MYSQL_ROW row; |
| 92 |
|
char sql[1024]; |
| 93 |
|
long int BBS_uid; |
| 94 |
|
int ret; |
| 95 |
|
|
| 96 |
|
// Verify format |
| 97 |
|
if (ireg("^[A-Za-z0-9_]{3,14}$", username, 0, NULL) != 0 || |
| 98 |
|
ireg("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0) |
| 99 |
|
{ |
| 100 |
|
prints("\033[1;31m用户名或密码格式错误...\033[m\r\n"); |
| 101 |
|
iflush(); |
| 102 |
|
return 1; |
| 103 |
|
} |
| 104 |
|
|
| 105 |
if (strlen (username) > 0) |
db = (MYSQL *)db_open(); |
| 106 |
|
if (db == NULL) |
| 107 |
{ |
{ |
| 108 |
//Input password |
return -1; |
| 109 |
prints ("\033[1;37m请输入密码\033[m: "); |
} |
|
iflush (); |
|
| 110 |
|
|
| 111 |
str_input (password, 19, 1); |
sprintf(sql, |
| 112 |
|
"select UID,username,p_login from user_list where username='%s' " |
| 113 |
|
"and (password=MD5('%s') or password=SHA2('%s', 256)) and " |
| 114 |
|
"enable", |
| 115 |
|
username, password, password); |
| 116 |
|
if (mysql_query(db, sql) != 0) |
| 117 |
|
{ |
| 118 |
|
log_error("Query user_list failed\n"); |
| 119 |
|
return -1; |
| 120 |
|
} |
| 121 |
|
if ((rs = mysql_store_result(db)) == NULL) |
| 122 |
|
{ |
| 123 |
|
log_error("Get user_list data failed\n"); |
| 124 |
|
return -1; |
| 125 |
|
} |
| 126 |
|
if (row = mysql_fetch_row(rs)) |
| 127 |
|
{ |
| 128 |
|
BBS_uid = atol(row[0]); |
| 129 |
|
strcpy(BBS_username, row[1]); |
| 130 |
|
if (atoi(row[2]) == 0) |
| 131 |
|
{ |
| 132 |
|
mysql_free_result(rs); |
| 133 |
|
mysql_close(db); |
| 134 |
|
|
| 135 |
|
prints("\033[1;31m您目前无权登陆...\033[m\r\n"); |
| 136 |
|
iflush(); |
| 137 |
|
return 1; |
| 138 |
|
} |
| 139 |
|
} |
| 140 |
|
else |
| 141 |
|
{ |
| 142 |
|
mysql_free_result(rs); |
| 143 |
|
|
| 144 |
ok = (check_user (username, password) == 0); |
sprintf(sql, |
| 145 |
|
"insert delayed into user_err_login_log" |
| 146 |
|
"(username,password,login_dt,login_ip) values" |
| 147 |
|
"('%s','%s',now(),'%s')", |
| 148 |
|
username, password, hostaddr_client); |
| 149 |
|
if (mysql_query(db, sql) != 0) |
| 150 |
|
{ |
| 151 |
|
log_error("Insert into user_err_login_log failed\n"); |
| 152 |
|
return -1; |
| 153 |
|
} |
| 154 |
|
|
| 155 |
|
mysql_close(db); |
| 156 |
|
|
| 157 |
|
prints("\033[1;31m错误的用户名或密码...\033[m\r\n"); |
| 158 |
|
iflush(); |
| 159 |
|
return 1; |
| 160 |
} |
} |
| 161 |
if (count >= 3 && !ok) |
mysql_free_result(rs); |
| 162 |
|
|
| 163 |
|
BBS_passwd_complex = verify_pass_complexity(password, username, 6); |
| 164 |
|
|
| 165 |
|
ret = load_user_info(db, BBS_uid); |
| 166 |
|
|
| 167 |
|
switch (ret) |
| 168 |
{ |
{ |
| 169 |
login_fail (); |
case 0: // Login successfully |
| 170 |
return -1; |
return 0; |
| 171 |
|
break; |
| 172 |
|
case -1: // Load data error |
| 173 |
|
prints("\033[1;31m读取用户数据错误...\033[m\r\n"); |
| 174 |
|
iflush(); |
| 175 |
|
return -1; |
| 176 |
|
break; |
| 177 |
|
case -2: // Unused |
| 178 |
|
return 0; |
| 179 |
|
break; |
| 180 |
|
case -3: // Dead |
| 181 |
|
prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n"); |
| 182 |
|
iflush(); |
| 183 |
|
return 1; |
| 184 |
|
default: |
| 185 |
|
return -2; |
| 186 |
} |
} |
|
} |
|
| 187 |
|
|
| 188 |
return 0; |
mysql_close(db); |
| 189 |
|
|
| 190 |
|
return 0; |
| 191 |
} |
} |
| 192 |
|
|
| 193 |
int |
int load_user_info(MYSQL *db, long int BBS_uid) |
|
check_user (char *username, char *password) |
|
| 194 |
{ |
{ |
| 195 |
MYSQL *db; |
MYSQL_RES *rs; |
| 196 |
MYSQL_RES *rs; |
MYSQL_ROW row; |
| 197 |
MYSQL_ROW row; |
char sql[1024]; |
| 198 |
char sql[1024]; |
long int BBS_auth_uid = 0; |
| 199 |
long int BBS_uid; |
int life; |
| 200 |
int ret; |
time_t last_login_dt; |
| 201 |
|
|
| 202 |
//Verify format |
sprintf(sql, |
| 203 |
if (ireg ("^[A-Za-z0-9_]{3,14}$", username, 0, NULL) != 0 || |
"select life,UNIX_TIMESTAMP(last_login_dt) " |
| 204 |
ireg ("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0) |
"from user_pubinfo where UID=%ld limit 1", |
| 205 |
{ |
BBS_uid); |
| 206 |
prints ("\033[1;31m用户名或密码格式错误...\033[m\r\n"); |
if (mysql_query(db, sql) != 0) |
| 207 |
iflush (); |
{ |
| 208 |
return 1; |
log_error("Query user_pubinfo failed\n"); |
| 209 |
} |
return -1; |
| 210 |
|
} |
| 211 |
db = (MYSQL *) db_open (); |
if ((rs = mysql_store_result(db)) == NULL) |
| 212 |
if (db == NULL) |
{ |
| 213 |
{ |
log_error("Get user_pubinfo data failed\n"); |
| 214 |
return -1; |
return -1; |
| 215 |
} |
} |
| 216 |
|
if (row = mysql_fetch_row(rs)) |
| 217 |
sprintf (sql, |
{ |
| 218 |
"select UID,p_login from user_list where username='%s' " |
life = atoi(row[0]); |
| 219 |
"and (password=MD5('%s') or password=PASSWORD('%s')) and " |
last_login_dt = (time_t)atol(row[1]); |
| 220 |
"enable", username, password, password); |
} |
| 221 |
if (mysql_query (db, sql) != 0) |
else |
| 222 |
{ |
{ |
| 223 |
log_error ("Query user_list failed\n"); |
mysql_free_result(rs); |
| 224 |
return -1; |
return (-1); // Data not found |
| 225 |
} |
} |
| 226 |
if ((rs = mysql_store_result (db)) == NULL) |
mysql_free_result(rs); |
|
{ |
|
|
log_error ("Get user_list data failed\n"); |
|
|
return -1; |
|
|
} |
|
|
if (row = mysql_fetch_row (rs)) |
|
|
{ |
|
|
BBS_uid = atol (row[0]); |
|
|
if (atoi (row[1]) == 0) |
|
|
{ |
|
|
mysql_free_result (rs); |
|
|
mysql_close (db); |
|
|
|
|
|
prints ("\033[1;31m您目前无权登陆...\033[m\r\n"); |
|
|
iflush (); |
|
|
return 1; |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
mysql_free_result (rs); |
|
|
|
|
|
sprintf (sql, |
|
|
"insert delayed into user_err_login_log" |
|
|
"(username,password,login_dt,login_ip) values" |
|
|
"('%s','%s',now(),'%s')", |
|
|
username, password, hostaddr_client); |
|
|
if (mysql_query (db, sql) != 0) |
|
|
{ |
|
|
log_error ("Insert into user_err_login_log failed\n"); |
|
|
return -1; |
|
|
} |
|
|
|
|
|
mysql_close (db); |
|
|
|
|
|
prints ("\033[1;31m错误的用户名或密码...\033[m\r\n"); |
|
|
iflush (); |
|
|
return 1; |
|
|
} |
|
|
mysql_free_result (rs); |
|
|
|
|
|
BBS_passwd_complex = verify_pass_complexity(password, username, 6); |
|
|
|
|
|
ret = load_user_info (db, BBS_uid); |
|
|
|
|
|
mysql_close (db); |
|
|
|
|
|
switch (ret) |
|
|
{ |
|
|
case 0: //Login successfully |
|
|
return 0; |
|
|
break; |
|
|
case -1: //Load data error |
|
|
prints ("\033[1;31m读取用户数据错误...\033[m\r\n"); |
|
|
iflush (); |
|
|
return -1; |
|
|
break; |
|
|
case -2: //Unused |
|
|
return 0; |
|
|
break; |
|
|
case -3: |
|
|
prints ("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n"); |
|
|
iflush (); |
|
|
return 1; |
|
|
default: |
|
|
return -2; |
|
|
} |
|
| 227 |
|
|
| 228 |
return 0; |
if (time(0) - last_login_dt >= 60 * 60 * 24 * life) |
| 229 |
|
{ |
| 230 |
|
return (-3); // Dead |
| 231 |
|
} |
| 232 |
|
|
| 233 |
|
sprintf(sql, |
| 234 |
|
"select AUID from user_auth where UID=%ld" |
| 235 |
|
" and enable and expire_dt>now()", |
| 236 |
|
BBS_uid); |
| 237 |
|
if (mysql_query(db, sql) != 0) |
| 238 |
|
{ |
| 239 |
|
log_error("Query user_auth failed\n"); |
| 240 |
|
return -1; |
| 241 |
|
} |
| 242 |
|
if ((rs = mysql_store_result(db)) == NULL) |
| 243 |
|
{ |
| 244 |
|
log_error("Get user_auth data failed\n"); |
| 245 |
|
return -1; |
| 246 |
|
} |
| 247 |
|
if (row = mysql_fetch_row(rs)) |
| 248 |
|
{ |
| 249 |
|
BBS_auth_uid = atol(row[0]); |
| 250 |
|
} |
| 251 |
|
else |
| 252 |
|
{ |
| 253 |
|
BBS_auth_uid = 0; |
| 254 |
|
} |
| 255 |
|
mysql_free_result(rs); |
| 256 |
|
|
| 257 |
|
sprintf(sql, |
| 258 |
|
"insert delayed into user_login_log" |
| 259 |
|
"(uid,login_dt,login_ip) values(%ld" |
| 260 |
|
",now(),'%s')", |
| 261 |
|
BBS_uid, hostaddr_client); |
| 262 |
|
if (mysql_query(db, sql) != 0) |
| 263 |
|
{ |
| 264 |
|
log_error("Insert into user_login_log failed\n"); |
| 265 |
|
return -1; |
| 266 |
|
} |
| 267 |
|
|
| 268 |
|
load_priv(db, &BBS_priv, BBS_uid, BBS_auth_uid, |
| 269 |
|
(!BBS_passwd_complex ? S_MAN_M : S_NONE) | |
| 270 |
|
(BBS_auth_uid ? S_NONE : S_MAIL)); |
| 271 |
|
|
| 272 |
|
BBS_last_access_tm = BBS_login_tm = time(0); |
| 273 |
|
BBS_last_sub_tm = time(0) - 60; |
| 274 |
|
|
| 275 |
|
sprintf(sql, |
| 276 |
|
"update user_pubinfo set visit_count=visit_count+1," |
| 277 |
|
"last_login_dt=now() where uid=%ld", |
| 278 |
|
BBS_uid); |
| 279 |
|
if (mysql_query(db, sql) != 0) |
| 280 |
|
{ |
| 281 |
|
log_error("Update user_pubinfo failed\n"); |
| 282 |
|
return -1; |
| 283 |
|
} |
| 284 |
|
|
| 285 |
|
return 0; |
| 286 |
} |
} |
| 287 |
|
|
| 288 |
int |
int load_guest_info(MYSQL *db, long int BBS_uid) |
|
load_user_info (MYSQL * db, long int BBS_uid) |
|
| 289 |
{ |
{ |
| 290 |
MYSQL_RES *rs; |
MYSQL_RES *rs; |
| 291 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 292 |
char sql[1024]; |
|
| 293 |
long int BBS_auth_uid = 0; |
db = (MYSQL *)db_open(); |
| 294 |
int life; |
if (db == NULL) |
| 295 |
time_t last_login_dt; |
{ |
| 296 |
|
return -1; |
| 297 |
sprintf (sql, |
} |
| 298 |
"select life,UNIX_TIMESTAMP(last_login_dt) " |
|
| 299 |
"from user_pubinfo where UID=%ld limit 1", BBS_uid); |
strcpy(BBS_username, "guest"); |
| 300 |
if (mysql_query (db, sql) != 0) |
|
| 301 |
{ |
load_priv(db, &BBS_priv, 0, 0, S_NONE); |
| 302 |
log_error ("Query user_pubinfo failed\n"); |
|
| 303 |
return -1; |
BBS_last_access_tm = BBS_login_tm = time(0); |
| 304 |
} |
BBS_last_sub_tm = time(0) - 60; |
| 305 |
if ((rs = mysql_store_result (db)) == NULL) |
|
| 306 |
{ |
mysql_close(db); |
|
log_error ("Get user_pubinfo data failed\n"); |
|
|
return -1; |
|
|
} |
|
|
if (row = mysql_fetch_row (rs)) |
|
|
{ |
|
|
life = atoi (row[0]); |
|
|
last_login_dt = (time_t) atol (row[1]); |
|
|
} |
|
|
else |
|
|
{ |
|
|
mysql_free_result (rs); |
|
|
return (-1); //Data not found |
|
|
} |
|
|
mysql_free_result (rs); |
|
|
|
|
|
if (time (0) - last_login_dt >= 60 * 60 * 24 * life) |
|
|
{ |
|
|
return (-3); //Dead |
|
|
} |
|
|
|
|
|
sprintf (sql, |
|
|
"select AUID from user_auth where UID=%ld" |
|
|
" and enable and expire_dt>now()", BBS_uid); |
|
|
if (mysql_query (db, sql) != 0) |
|
|
{ |
|
|
log_error ("Query user_auth failed\n"); |
|
|
return -1; |
|
|
} |
|
|
if ((rs = mysql_store_result (db)) == NULL) |
|
|
{ |
|
|
log_error ("Get user_auth data failed\n"); |
|
|
return -1; |
|
|
} |
|
|
if (row = mysql_fetch_row (rs)) |
|
|
{ |
|
|
BBS_auth_uid = atol (row[0]); |
|
|
} |
|
|
mysql_free_result (rs); |
|
|
|
|
|
sprintf (sql, |
|
|
"insert delayed into user_login_log" |
|
|
"(uid,login_dt,login_ip) values(%ld" |
|
|
",now(),'%s')", |
|
|
BBS_uid, hostaddr_client); |
|
|
if (mysql_query (db, sql) != 0) |
|
|
{ |
|
|
log_error ("Insert into user_login_log failed\n"); |
|
|
return -1; |
|
|
} |
|
|
|
|
|
load_priv (db, &BBS_priv, BBS_uid, BBS_auth_uid, |
|
|
(!BBS_passwd_complex ? S_MAN_M : S_NONE) | |
|
|
(BBS_auth_uid ? S_NONE : S_MAIL)); |
|
|
|
|
|
BBS_last_access_tm = BBS_login_tm = time (0); |
|
|
BBS_last_sub_tm = time (0) - 60; |
|
|
|
|
|
sprintf (sql, |
|
|
"update user_pubinfo set visit_count=visit_count+1," |
|
|
"last_login_dt=now() where uid=%ld", BBS_uid); |
|
|
if (mysql_query (db, sql) != 0) |
|
|
{ |
|
|
log_error ("Update user_pubinfo failed\n"); |
|
|
return -1; |
|
|
} |
|
| 307 |
|
|
| 308 |
return 0; |
return 0; |
| 309 |
} |
} |