| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
main.c - description |
login.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 11 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 |
/*************************************************************************** |
/*************************************************************************** |
| 15 |
* * |
* * |
| 16 |
***************************************************************************/ |
***************************************************************************/ |
| 17 |
|
|
| 18 |
|
#include "login.h" |
| 19 |
|
#include "register.h" |
| 20 |
#include "bbs.h" |
#include "bbs.h" |
| 21 |
|
#include "user_priv.h" |
| 22 |
|
#include "reg_ex.h" |
| 23 |
#include "common.h" |
#include "common.h" |
| 24 |
|
#include "log.h" |
| 25 |
|
#include "io.h" |
| 26 |
|
#include "screen.h" |
| 27 |
|
#include "database.h" |
| 28 |
|
#include <string.h> |
| 29 |
|
#include <mysql.h> |
| 30 |
|
#include <regex.h> |
| 31 |
|
#include <unistd.h> |
| 32 |
|
|
| 33 |
void |
void login_fail() |
|
login_fail () |
|
| 34 |
{ |
{ |
| 35 |
char temp[256]; |
display_file(DATA_LOGIN_ERROR); |
| 36 |
|
|
| 37 |
strcpy (temp, app_home_dir); |
sleep(1); |
|
strcat (temp, "data/login_error.dat"); |
|
|
display_file (temp); |
|
| 38 |
} |
} |
| 39 |
|
|
| 40 |
int |
int bbs_login() |
|
bbs_login () |
|
| 41 |
{ |
{ |
| 42 |
char username[20], password[20]; |
char username[BBS_username_max_len + 1]; |
| 43 |
int count, ok; |
char password[BBS_password_max_len + 1]; |
| 44 |
|
int count = 0; |
| 45 |
|
int ok = 0; |
| 46 |
|
|
| 47 |
//Input username |
// Input username |
| 48 |
count = 0; |
while (!ok) |
| 49 |
ok = 0; |
{ |
| 50 |
while (!ok) |
prints("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', " |
| 51 |
{ |
"注册请输入`\033[1;31mnew\033[m'): "); |
| 52 |
printf |
iflush(); |
| 53 |
("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', " |
|
| 54 |
"注册请输入`\033[1;31mnew\033[m'): "); |
str_input(username, sizeof(username) - 1, DOECHO); |
| 55 |
fflush (stdout); |
count++; |
| 56 |
|
|
| 57 |
|
if (strcmp(username, "guest") == 0) |
| 58 |
|
{ |
| 59 |
|
MYSQL *db = db_open(); |
| 60 |
|
if (db == NULL) |
| 61 |
|
{ |
| 62 |
|
return -1; |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
load_guest_info(db); |
| 66 |
|
|
| 67 |
|
mysql_close(db); |
| 68 |
|
|
| 69 |
|
return 0; |
| 70 |
|
} |
| 71 |
|
|
| 72 |
|
if (strcmp(username, "new") == 0) |
| 73 |
|
{ |
| 74 |
|
if (user_register() == 0) |
| 75 |
|
return 0; |
| 76 |
|
else |
| 77 |
|
return -2; |
| 78 |
|
} |
| 79 |
|
|
| 80 |
|
if (username[0] != '\0') |
| 81 |
|
{ |
| 82 |
|
// Input password |
| 83 |
|
prints("\033[1;37m请输入密码\033[m: "); |
| 84 |
|
iflush(); |
| 85 |
|
|
| 86 |
|
str_input(password, sizeof(password) - 1, NOECHO); |
| 87 |
|
|
| 88 |
|
MYSQL *db = db_open(); |
| 89 |
|
if (db == NULL) |
| 90 |
|
{ |
| 91 |
|
return -1; |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
ok = (check_user(db, username, password) == 0); |
| 95 |
|
|
| 96 |
|
mysql_close(db); |
| 97 |
|
} |
| 98 |
|
if (count >= 3 && !ok) |
| 99 |
|
{ |
| 100 |
|
login_fail(); |
| 101 |
|
return -1; |
| 102 |
|
} |
| 103 |
|
} |
| 104 |
|
|
| 105 |
|
return 0; |
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
int check_user(MYSQL *db, char *username, char *password) |
| 109 |
|
{ |
| 110 |
|
MYSQL_RES *rs; |
| 111 |
|
MYSQL_ROW row; |
| 112 |
|
char sql[SQL_BUFFER_LEN]; |
| 113 |
|
int ret; |
| 114 |
|
long BBS_uid = 0; |
| 115 |
|
|
| 116 |
|
// Verify format |
| 117 |
|
if (ireg("^[A-Za-z][A-Za-z0-9]{2,11}$", username, 0, NULL) != 0 || |
| 118 |
|
ireg("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0) |
| 119 |
|
{ |
| 120 |
|
prints("\033[1;31m用户名或密码格式错误...\033[m\r\n"); |
| 121 |
|
iflush(); |
| 122 |
|
return 1; |
| 123 |
|
} |
| 124 |
|
|
| 125 |
|
// Begin transaction |
| 126 |
|
if (mysql_query(db, "SET autocommit=0") != 0) |
| 127 |
|
{ |
| 128 |
|
log_error("SET autocommit=0 failed\n"); |
| 129 |
|
return -1; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
if (mysql_query(db, "BEGIN") != 0) |
| 133 |
|
{ |
| 134 |
|
log_error("Begin transaction failed\n"); |
| 135 |
|
return -1; |
| 136 |
|
} |
| 137 |
|
|
| 138 |
str_input (username, 19, 0); |
sprintf(sql, |
| 139 |
count++; |
"SELECT UID, username, p_login FROM user_list " |
| 140 |
|
"WHERE username = '%s' AND password = SHA2('%s', 256) AND enable", |
| 141 |
|
username, password); |
| 142 |
|
if (mysql_query(db, sql) != 0) |
| 143 |
|
{ |
| 144 |
|
log_error("Query user_list failed\n"); |
| 145 |
|
return -1; |
| 146 |
|
} |
| 147 |
|
if ((rs = mysql_store_result(db)) == NULL) |
| 148 |
|
{ |
| 149 |
|
log_error("Get user_list data failed\n"); |
| 150 |
|
return -1; |
| 151 |
|
} |
| 152 |
|
if ((row = mysql_fetch_row(rs))) |
| 153 |
|
{ |
| 154 |
|
BBS_uid = atol(row[0]); |
| 155 |
|
strncpy(BBS_username, row[1], sizeof(BBS_username) - 1); |
| 156 |
|
BBS_username[sizeof(BBS_username) - 1] = '\0'; |
| 157 |
|
int p_login = atoi(row[2]); |
| 158 |
|
|
| 159 |
|
mysql_free_result(rs); |
| 160 |
|
|
| 161 |
|
// Add user login log |
| 162 |
|
sprintf(sql, |
| 163 |
|
"INSERT INTO user_login_log(UID, login_dt, login_ip) " |
| 164 |
|
"VALUES(%ld, NOW(), '%s')", |
| 165 |
|
BBS_uid, hostaddr_client); |
| 166 |
|
if (mysql_query(db, sql) != 0) |
| 167 |
|
{ |
| 168 |
|
log_error("Insert into user_login_log failed\n"); |
| 169 |
|
return -1; |
| 170 |
|
} |
| 171 |
|
|
| 172 |
|
// Commit transaction |
| 173 |
|
if (mysql_query(db, "COMMIT") != 0) |
| 174 |
|
{ |
| 175 |
|
log_error("Commit transaction failed\n"); |
| 176 |
|
return -1; |
| 177 |
|
} |
| 178 |
|
|
| 179 |
|
if (p_login == 0) |
| 180 |
|
{ |
| 181 |
|
mysql_free_result(rs); |
| 182 |
|
|
| 183 |
|
prints("\033[1;31m您目前无权登陆...\033[m\r\n"); |
| 184 |
|
iflush(); |
| 185 |
|
return 1; |
| 186 |
|
} |
| 187 |
|
} |
| 188 |
|
else |
| 189 |
|
{ |
| 190 |
|
mysql_free_result(rs); |
| 191 |
|
|
| 192 |
if (strcmp (username, "guest") == 0) |
sprintf(sql, |
| 193 |
return 1; |
"INSERT INTO user_err_login_log(username, password, login_dt, login_ip) " |
| 194 |
|
"VALUES('%s', '%s', NOW(), '%s')", |
| 195 |
|
username, password, hostaddr_client); |
| 196 |
|
if (mysql_query(db, sql) != 0) |
| 197 |
|
{ |
| 198 |
|
log_error("Insert into user_err_login_log failed\n"); |
| 199 |
|
return -1; |
| 200 |
|
} |
| 201 |
|
|
| 202 |
|
// Commit transaction |
| 203 |
|
if (mysql_query(db, "COMMIT") != 0) |
| 204 |
|
{ |
| 205 |
|
log_error("Commit transaction failed\n"); |
| 206 |
|
return -1; |
| 207 |
|
} |
| 208 |
|
|
| 209 |
|
prints("\033[1;31m错误的用户名或密码...\033[m\r\n"); |
| 210 |
|
iflush(); |
| 211 |
|
return 1; |
| 212 |
|
} |
| 213 |
|
|
| 214 |
if (strlen (username) > 0) |
// Set AUTOCOMMIT = 1 |
| 215 |
|
if (mysql_query(db, "SET autocommit=1") != 0) |
| 216 |
{ |
{ |
| 217 |
//Input password |
log_error("SET autocommit=1 failed\n"); |
| 218 |
printf ("\033[1;37m请输入密码\033[m: "); |
return -1; |
| 219 |
fflush (stdout); |
} |
| 220 |
|
|
| 221 |
str_input (password, 19, 0); |
ret = load_user_info(db, BBS_uid); |
| 222 |
|
|
| 223 |
if (strlen (password) > 0) |
switch (ret) |
| 224 |
{ |
{ |
| 225 |
ok = check_user (username, password); |
case 0: // Login successfully |
| 226 |
} |
break; |
| 227 |
|
case -1: // Load data error |
| 228 |
|
prints("\033[1;31m读取用户数据错误...\033[m\r\n"); |
| 229 |
|
iflush(); |
| 230 |
|
return -1; |
| 231 |
|
case -2: // Unused |
| 232 |
|
prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n"); |
| 233 |
|
iflush(); |
| 234 |
|
return 1; |
| 235 |
|
case -3: // Dead |
| 236 |
|
prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n"); |
| 237 |
|
iflush(); |
| 238 |
|
return 1; |
| 239 |
|
default: |
| 240 |
|
return -2; |
| 241 |
|
} |
| 242 |
|
|
| 243 |
if (!ok) |
sprintf(sql, |
| 244 |
{ |
"UPDATE user_pubinfo SET visit_count = visit_count + 1, " |
| 245 |
printf ("\033[1;31m错误的用户名或密码...\r\n"); |
"last_login_dt = NOW() WHERE UID = %ld", |
| 246 |
fflush (stdout); |
BBS_uid); |
| 247 |
} |
if (mysql_query(db, sql) != 0) |
| 248 |
|
{ |
| 249 |
|
log_error("Update user_pubinfo failed\n"); |
| 250 |
|
return -1; |
| 251 |
} |
} |
| 252 |
if (count >= 3) |
|
| 253 |
|
if (user_online_add(db) != 0) |
| 254 |
{ |
{ |
| 255 |
login_fail (); |
return -1; |
|
return -1; |
|
| 256 |
} |
} |
|
} |
|
| 257 |
|
|
| 258 |
return 0; |
BBS_last_access_tm = BBS_login_tm = time(0); |
| 259 |
|
|
| 260 |
|
return 0; |
| 261 |
} |
} |
| 262 |
|
|
| 263 |
int |
int load_user_info(MYSQL *db, long int BBS_uid) |
|
check_user(char *username, char *password) |
|
| 264 |
{ |
{ |
| 265 |
return 1; |
MYSQL_RES *rs; |
| 266 |
|
MYSQL_ROW row; |
| 267 |
|
char sql[SQL_BUFFER_LEN]; |
| 268 |
|
int life; |
| 269 |
|
time_t last_login_dt; |
| 270 |
|
|
| 271 |
|
sprintf(sql, |
| 272 |
|
"SELECT life, UNIX_TIMESTAMP(last_login_dt) " |
| 273 |
|
"FROM user_pubinfo WHERE UID = %ld", |
| 274 |
|
BBS_uid); |
| 275 |
|
if (mysql_query(db, sql) != 0) |
| 276 |
|
{ |
| 277 |
|
log_error("Query user_pubinfo failed\n"); |
| 278 |
|
return -1; |
| 279 |
|
} |
| 280 |
|
if ((rs = mysql_store_result(db)) == NULL) |
| 281 |
|
{ |
| 282 |
|
log_error("Get user_pubinfo data failed\n"); |
| 283 |
|
return -1; |
| 284 |
|
} |
| 285 |
|
if ((row = mysql_fetch_row(rs))) |
| 286 |
|
{ |
| 287 |
|
life = atoi(row[0]); |
| 288 |
|
last_login_dt = (time_t)atol(row[1]); |
| 289 |
|
} |
| 290 |
|
else |
| 291 |
|
{ |
| 292 |
|
mysql_free_result(rs); |
| 293 |
|
return -1; // Data not found |
| 294 |
|
} |
| 295 |
|
mysql_free_result(rs); |
| 296 |
|
|
| 297 |
|
if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal |
| 298 |
|
time(0) - last_login_dt > 60 * 60 * 24 * life) |
| 299 |
|
{ |
| 300 |
|
return -3; // Dead |
| 301 |
|
} |
| 302 |
|
|
| 303 |
|
if (load_priv(db, &BBS_priv, BBS_uid) != 0) |
| 304 |
|
{ |
| 305 |
|
return -1; |
| 306 |
|
} |
| 307 |
|
|
| 308 |
|
return 0; |
| 309 |
|
} |
| 310 |
|
|
| 311 |
|
int load_guest_info(MYSQL *db) |
| 312 |
|
{ |
| 313 |
|
strncpy(BBS_username, "guest", sizeof(BBS_username) - 1); |
| 314 |
|
BBS_username[sizeof(BBS_username) - 1] = '\0'; |
| 315 |
|
|
| 316 |
|
if (load_priv(db, &BBS_priv, 0) != 0) |
| 317 |
|
{ |
| 318 |
|
return -1; |
| 319 |
|
} |
| 320 |
|
|
| 321 |
|
if (user_online_add(db) != 0) |
| 322 |
|
{ |
| 323 |
|
return -1; |
| 324 |
|
} |
| 325 |
|
|
| 326 |
|
BBS_last_access_tm = BBS_login_tm = time(0); |
| 327 |
|
|
| 328 |
|
return 0; |
| 329 |
|
} |
| 330 |
|
|
| 331 |
|
int user_online_add(MYSQL *db) |
| 332 |
|
{ |
| 333 |
|
char sql[SQL_BUFFER_LEN]; |
| 334 |
|
|
| 335 |
|
if (user_online_del(db) != 0) |
| 336 |
|
{ |
| 337 |
|
return -1; |
| 338 |
|
} |
| 339 |
|
|
| 340 |
|
sprintf(sql, |
| 341 |
|
"INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) " |
| 342 |
|
"VALUES('Telnet_Process_%d', %ld, '%s', NOW(), NOW())", |
| 343 |
|
getpid(), BBS_priv.uid, hostaddr_client); |
| 344 |
|
if (mysql_query(db, sql) != 0) |
| 345 |
|
{ |
| 346 |
|
log_error("Add user_online failed\n"); |
| 347 |
|
return -1; |
| 348 |
|
} |
| 349 |
|
|
| 350 |
|
return 0; |
| 351 |
|
} |
| 352 |
|
|
| 353 |
|
int user_online_del(MYSQL *db) |
| 354 |
|
{ |
| 355 |
|
char sql[SQL_BUFFER_LEN]; |
| 356 |
|
|
| 357 |
|
sprintf(sql, |
| 358 |
|
"DELETE FROM user_online WHERE SID = 'Telnet_Process_%d'", |
| 359 |
|
getpid()); |
| 360 |
|
if (mysql_query(db, sql) != 0) |
| 361 |
|
{ |
| 362 |
|
log_error("Delete user_online failed\n"); |
| 363 |
|
return -1; |
| 364 |
|
} |
| 365 |
|
|
| 366 |
|
return 0; |
| 367 |
} |
} |