| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
login.c - description |
login.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 20 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 |
***************************************************************************/ |
***************************************************************************/ |
| 31 |
|
|
| 32 |
void login_fail() |
void login_fail() |
| 33 |
{ |
{ |
| 34 |
char temp[256]; |
display_file(DATA_LOGIN_ERROR); |
|
|
|
|
strcpy(temp, app_home_dir); |
|
|
strcat(temp, "data/login_error.txt"); |
|
|
display_file(temp); |
|
| 35 |
|
|
| 36 |
sleep(1); |
sleep(1); |
| 37 |
} |
} |
| 38 |
|
|
| 39 |
int bbs_login() |
int bbs_login() |
| 40 |
{ |
{ |
| 41 |
char username[20], password[20]; |
char username[BBS_username_max_len + 1]; |
| 42 |
int count, ok; |
char password[BBS_password_max_len + 1]; |
| 43 |
|
int count = 0; |
| 44 |
|
int ok = 0; |
| 45 |
|
|
| 46 |
// Input username |
// Input username |
|
count = 0; |
|
|
ok = 0; |
|
| 47 |
while (!ok) |
while (!ok) |
| 48 |
{ |
{ |
| 49 |
prints("\033[1;33mÇëÊäÈëÕʺÅ\033[m(ÊÔÓÃÇëÊäÈë `\033[1;36mguest\033[m', " |
prints("\033[1;33mÇëÊäÈëÕʺÅ\033[m(ÊÔÓÃÇëÊäÈë `\033[1;36mguest\033[m', " |
| 50 |
"×¢²áÇëÊäÈë`\033[1;31mnew\033[m'): "); |
"×¢²áÇëÊäÈë`\033[1;31mnew\033[m'): "); |
| 51 |
iflush(); |
iflush(); |
| 52 |
|
|
| 53 |
str_input(username, 19, DOECHO); |
str_input(username, sizeof(username), DOECHO); |
| 54 |
count++; |
count++; |
| 55 |
|
|
| 56 |
if (strcmp(username, "guest") == 0) |
if (strcmp(username, "guest") == 0) |
| 57 |
{ |
{ |
| 58 |
MYSQL * db = db_open(); |
MYSQL *db = db_open(); |
| 59 |
if (db == NULL) |
if (db == NULL) |
| 60 |
{ |
{ |
| 61 |
return -1; |
return -1; |
| 62 |
} |
} |
| 63 |
|
|
| 64 |
load_guest_info(db); |
load_guest_info(db); |
| 65 |
|
|
| 66 |
mysql_close(db); |
mysql_close(db); |
| 71 |
if (strcmp(username, "new") == 0) |
if (strcmp(username, "new") == 0) |
| 72 |
{ |
{ |
| 73 |
if (user_register() == 0) |
if (user_register() == 0) |
| 74 |
|
{ |
| 75 |
return 0; |
return 0; |
| 76 |
|
} |
| 77 |
else |
else |
| 78 |
|
{ |
| 79 |
return -2; |
return -2; |
| 80 |
|
} |
| 81 |
} |
} |
| 82 |
|
|
| 83 |
if (strlen(username) > 0) |
if (username[0] != '\0') |
| 84 |
{ |
{ |
| 85 |
// Input password |
// Input password |
| 86 |
prints("\033[1;37mÇëÊäÈëÃÜÂë\033[m: "); |
prints("\033[1;37mÇëÊäÈëÃÜÂë\033[m: "); |
| 87 |
iflush(); |
iflush(); |
| 88 |
|
|
| 89 |
str_input(password, 19, NOECHO); |
str_input(password, sizeof(password), NOECHO); |
| 90 |
|
|
| 91 |
MYSQL * db = db_open(); |
MYSQL *db = db_open(); |
| 92 |
if (db == NULL) |
if (db == NULL) |
| 93 |
{ |
{ |
| 94 |
return -1; |
return -1; |
| 95 |
} |
} |
| 96 |
|
|
| 97 |
ok = (check_user(db, username, password) == 0); |
ok = (check_user(db, username, password) == 0); |
| 98 |
|
|
| 99 |
mysql_close(db); |
mysql_close(db); |
| 112 |
{ |
{ |
| 113 |
MYSQL_RES *rs; |
MYSQL_RES *rs; |
| 114 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 115 |
char sql[1024]; |
char sql[SQL_BUFFER_LEN]; |
|
long int BBS_uid; |
|
| 116 |
int ret; |
int ret; |
| 117 |
|
long BBS_uid = 0; |
| 118 |
|
|
| 119 |
// Verify format |
// Verify format |
| 120 |
if (ireg("^[A-Za-z][A-Za-z0-9]{2,11}$", username, 0, NULL) != 0 || |
if (ireg("^[A-Za-z][A-Za-z0-9]{2,11}$", username, 0, NULL) != 0 || |
| 138 |
return -1; |
return -1; |
| 139 |
} |
} |
| 140 |
|
|
| 141 |
sprintf(sql, |
snprintf(sql, sizeof(sql), |
| 142 |
"SELECT UID, username, p_login FROM user_list " |
"SELECT UID, username, p_login FROM user_list " |
| 143 |
"WHERE username = '%s' AND password = SHA2('%s', 256) AND enable", |
"WHERE username = '%s' AND password = SHA2('%s', 256) AND enable", |
| 144 |
username, password); |
username, password); |
| 152 |
log_error("Get user_list data failed\n"); |
log_error("Get user_list data failed\n"); |
| 153 |
return -1; |
return -1; |
| 154 |
} |
} |
| 155 |
if (row = mysql_fetch_row(rs)) |
if ((row = mysql_fetch_row(rs))) |
| 156 |
{ |
{ |
| 157 |
BBS_uid = atol(row[0]); |
BBS_uid = atol(row[0]); |
| 158 |
strcpy(BBS_username, row[1]); |
strncpy(BBS_username, row[1], sizeof(BBS_username) - 1); |
| 159 |
|
BBS_username[sizeof(BBS_username) - 1] = '\0'; |
| 160 |
int p_login = atoi(row[2]); |
int p_login = atoi(row[2]); |
| 161 |
|
|
| 162 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 163 |
|
|
| 164 |
// Add user login log |
// Add user login log |
| 165 |
sprintf(sql, |
snprintf(sql, sizeof(sql), |
| 166 |
"INSERT INTO user_login_log(UID, login_dt, login_ip) " |
"INSERT INTO user_login_log(UID, login_dt, login_ip) " |
| 167 |
"VALUES(%ld, NOW(), '%s')", |
"VALUES(%ld, NOW(), '%s')", |
| 168 |
BBS_uid, hostaddr_client); |
BBS_uid, hostaddr_client); |
| 192 |
{ |
{ |
| 193 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 194 |
|
|
| 195 |
sprintf(sql, |
snprintf(sql, sizeof(sql), |
| 196 |
"INSERT INTO user_err_login_log(username, password, login_dt, login_ip) " |
"INSERT INTO user_err_login_log(username, password, login_dt, login_ip) " |
| 197 |
"VALUES('%s', '%s', NOW(), '%s')", |
"VALUES('%s', '%s', NOW(), '%s')", |
| 198 |
username, password, hostaddr_client); |
username, password, hostaddr_client); |
| 243 |
return -2; |
return -2; |
| 244 |
} |
} |
| 245 |
|
|
| 246 |
sprintf(sql, |
snprintf(sql, sizeof(sql), |
| 247 |
"UPDATE user_pubinfo SET visit_count = visit_count + 1, " |
"UPDATE user_pubinfo SET visit_count = visit_count + 1, " |
| 248 |
"last_login_dt = NOW() WHERE UID = %ld", |
"last_login_dt = NOW() WHERE UID = %ld", |
| 249 |
BBS_uid); |
BBS_uid); |
| 253 |
return -1; |
return -1; |
| 254 |
} |
} |
| 255 |
|
|
| 256 |
|
if (user_online_add(db) != 0) |
| 257 |
|
{ |
| 258 |
|
return -1; |
| 259 |
|
} |
| 260 |
|
|
| 261 |
BBS_last_access_tm = BBS_login_tm = time(0); |
BBS_last_access_tm = BBS_login_tm = time(0); |
| 262 |
|
|
| 263 |
return 0; |
return 0; |
| 267 |
{ |
{ |
| 268 |
MYSQL_RES *rs; |
MYSQL_RES *rs; |
| 269 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 270 |
char sql[1024]; |
char sql[SQL_BUFFER_LEN]; |
| 271 |
int life; |
int life; |
| 272 |
time_t last_login_dt; |
time_t last_login_dt; |
| 273 |
|
|
| 274 |
sprintf(sql, |
snprintf(sql, sizeof(sql), |
| 275 |
"SELECT life, UNIX_TIMESTAMP(last_login_dt) " |
"SELECT life, UNIX_TIMESTAMP(last_login_dt) " |
| 276 |
"FROM user_pubinfo WHERE UID = %ld", |
"FROM user_pubinfo WHERE UID = %ld", |
| 277 |
BBS_uid); |
BBS_uid); |
| 285 |
log_error("Get user_pubinfo data failed\n"); |
log_error("Get user_pubinfo data failed\n"); |
| 286 |
return -1; |
return -1; |
| 287 |
} |
} |
| 288 |
if (row = mysql_fetch_row(rs)) |
if ((row = mysql_fetch_row(rs))) |
| 289 |
{ |
{ |
| 290 |
life = atoi(row[0]); |
life = atoi(row[0]); |
| 291 |
last_login_dt = (time_t)atol(row[1]); |
last_login_dt = (time_t)atol(row[1]); |
| 293 |
else |
else |
| 294 |
{ |
{ |
| 295 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 296 |
return (-1); // Data not found |
return -1; // Data not found |
| 297 |
} |
} |
| 298 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 299 |
|
|
| 300 |
if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal |
if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal |
| 301 |
time(0) - last_login_dt > 60 * 60 * 24 * life) |
time(0) - last_login_dt > 60 * 60 * 24 * life) |
| 302 |
{ |
{ |
| 303 |
return (-3); // Dead |
return -3; // Dead |
| 304 |
} |
} |
| 305 |
|
|
| 306 |
load_priv(db, &BBS_priv, BBS_uid); |
if (load_priv(db, &BBS_priv, BBS_uid) != 0) |
| 307 |
|
{ |
| 308 |
|
return -1; |
| 309 |
|
} |
| 310 |
|
|
| 311 |
return 0; |
return 0; |
| 312 |
} |
} |
| 313 |
|
|
| 314 |
int load_guest_info(MYSQL *db) |
int load_guest_info(MYSQL *db) |
| 315 |
{ |
{ |
| 316 |
MYSQL_RES *rs; |
strncpy(BBS_username, "guest", sizeof(BBS_username) - 1); |
| 317 |
MYSQL_ROW row; |
BBS_username[sizeof(BBS_username) - 1] = '\0'; |
| 318 |
|
|
| 319 |
strcpy(BBS_username, "guest"); |
if (load_priv(db, &BBS_priv, 0) != 0) |
| 320 |
|
{ |
| 321 |
|
return -1; |
| 322 |
|
} |
| 323 |
|
|
| 324 |
load_priv(db, &BBS_priv, 0); |
if (user_online_add(db) != 0) |
| 325 |
|
{ |
| 326 |
|
return -1; |
| 327 |
|
} |
| 328 |
|
|
| 329 |
BBS_last_access_tm = BBS_login_tm = time(0); |
BBS_last_access_tm = BBS_login_tm = time(0); |
| 330 |
|
|
| 331 |
return 0; |
return 0; |
| 332 |
} |
} |
| 333 |
|
|
| 334 |
|
int user_online_add(MYSQL *db) |
| 335 |
|
{ |
| 336 |
|
char sql[SQL_BUFFER_LEN]; |
| 337 |
|
|
| 338 |
|
if (user_online_del(db) != 0) |
| 339 |
|
{ |
| 340 |
|
return -1; |
| 341 |
|
} |
| 342 |
|
|
| 343 |
|
snprintf(sql, sizeof(sql), |
| 344 |
|
"INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) " |
| 345 |
|
"VALUES('Telnet_Process_%d', %ld, '%s', NOW(), NOW())", |
| 346 |
|
getpid(), BBS_priv.uid, hostaddr_client); |
| 347 |
|
if (mysql_query(db, sql) != 0) |
| 348 |
|
{ |
| 349 |
|
log_error("Add user_online failed\n"); |
| 350 |
|
return -1; |
| 351 |
|
} |
| 352 |
|
|
| 353 |
|
return 0; |
| 354 |
|
} |
| 355 |
|
|
| 356 |
|
int user_online_del(MYSQL *db) |
| 357 |
|
{ |
| 358 |
|
char sql[SQL_BUFFER_LEN]; |
| 359 |
|
|
| 360 |
|
snprintf(sql, sizeof(sql), |
| 361 |
|
"DELETE FROM user_online WHERE SID = 'Telnet_Process_%d'", |
| 362 |
|
getpid()); |
| 363 |
|
if (mysql_query(db, sql) != 0) |
| 364 |
|
{ |
| 365 |
|
log_error("Delete user_online failed\n"); |
| 366 |
|
return -1; |
| 367 |
|
} |
| 368 |
|
|
| 369 |
|
return 0; |
| 370 |
|
} |