| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
|
#define _POSIX_C_SOURCE 200112L |
|
|
|
|
| 17 |
#include "login.h" |
#include "login.h" |
| 18 |
#include "bbs.h" |
#include "bbs.h" |
| 19 |
#include "user_priv.h" |
#include "user_priv.h" |
| 30 |
#include <unistd.h> |
#include <unistd.h> |
| 31 |
#include <mysql/mysql.h> |
#include <mysql/mysql.h> |
| 32 |
|
|
| 33 |
int bbs_login(MYSQL *db) |
int bbs_login(void) |
| 34 |
{ |
{ |
| 35 |
char username[BBS_username_max_len + 1]; |
char username[BBS_username_max_len + 1]; |
| 36 |
char password[BBS_password_max_len + 1]; |
char password[BBS_password_max_len + 1]; |
| 37 |
int count = 0; |
int i = 0; |
| 38 |
int ok = 0; |
int ok = 0; |
| 39 |
|
|
| 40 |
for (; !SYS_server_exit && !ok && count < 3; count++) |
for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++) |
| 41 |
{ |
{ |
| 42 |
prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', " |
prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', " |
| 43 |
"注册请输入`\033[1;31mnew\033[m'): "); |
"注册请输入`\033[1;31mnew\033[m'): "); |
| 50 |
|
|
| 51 |
if (strcmp(username, "guest") == 0) |
if (strcmp(username, "guest") == 0) |
| 52 |
{ |
{ |
| 53 |
load_guest_info(db); |
load_guest_info(); |
| 54 |
|
|
| 55 |
return 0; |
return 0; |
| 56 |
} |
} |
| 57 |
|
|
| 58 |
if (strcmp(username, "new") == 0) |
if (strcmp(username, "new") == 0) |
| 59 |
{ |
{ |
| 60 |
display_file(DATA_REGISTER, 1, 1); |
display_file(DATA_REGISTER, 1); |
| 61 |
|
|
| 62 |
return 0; |
return 0; |
| 63 |
} |
} |
| 72 |
continue; |
continue; |
| 73 |
} |
} |
| 74 |
|
|
| 75 |
ok = (check_user(db, username, password) == 0); |
ok = (check_user(username, password) == 0); |
| 76 |
iflush(); |
iflush(); |
| 77 |
} |
} |
| 78 |
} |
} |
| 79 |
|
|
| 80 |
if (!ok) |
if (!ok) |
| 81 |
{ |
{ |
| 82 |
display_file(DATA_LOGIN_ERROR, 1, 1); |
display_file(DATA_LOGIN_ERROR, 1); |
| 83 |
return -1; |
return -1; |
| 84 |
} |
} |
| 85 |
|
|
| 86 |
log_std("User \"%s\"(%ld) login from %s:%d\n", |
log_common("User \"%s\"(%ld) login from %s:%d\n", |
| 87 |
BBS_username, BBS_priv.uid, hostaddr_client, port_client); |
BBS_username, BBS_priv.uid, hostaddr_client, port_client); |
| 88 |
|
|
| 89 |
return 0; |
return 0; |
| 90 |
} |
} |
| 91 |
|
|
| 92 |
int check_user(MYSQL *db, char *username, char *password) |
int check_user(const char *username, const char *password) |
| 93 |
{ |
{ |
| 94 |
MYSQL_RES *rs; |
MYSQL *db = NULL; |
| 95 |
|
MYSQL_RES *rs = NULL; |
| 96 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 97 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 98 |
int ret; |
int ret = 0; |
| 99 |
long BBS_uid = 0; |
int BBS_uid = 0; |
| 100 |
char client_addr[IP_ADDR_LEN]; |
char client_addr[IP_ADDR_LEN]; |
| 101 |
int i; |
int i; |
| 102 |
int ok = 1; |
int ok = 1; |
| 103 |
char user_tz_env[BBS_user_tz_max_len + 2]; |
char user_tz_env[BBS_user_tz_max_len + 2]; |
| 104 |
|
|
| 105 |
|
db = db_open(); |
| 106 |
|
if (db == NULL) |
| 107 |
|
{ |
| 108 |
|
ret = -1; |
| 109 |
|
goto cleanup; |
| 110 |
|
} |
| 111 |
|
|
| 112 |
// Verify format |
// Verify format |
| 113 |
for (i = 0; ok && username[i] != '\0'; i++) |
for (i = 0; ok && username[i] != '\0'; i++) |
| 114 |
{ |
{ |
| 136 |
if (!ok) |
if (!ok) |
| 137 |
{ |
{ |
| 138 |
prints("\033[1;31m用户名或密码格式错误...\033[m\r\n"); |
prints("\033[1;31m用户名或密码格式错误...\033[m\r\n"); |
| 139 |
return 1; |
ret = 1; |
| 140 |
|
goto cleanup; |
| 141 |
} |
} |
| 142 |
|
|
| 143 |
// Begin transaction |
// Begin transaction |
| 144 |
if (mysql_query(db, "SET autocommit=0") != 0) |
if (mysql_query(db, "SET autocommit=0") != 0) |
| 145 |
{ |
{ |
| 146 |
log_error("SET autocommit=0 error: %s\n", mysql_error(db)); |
log_error("SET autocommit=0 error: %s\n", mysql_error(db)); |
| 147 |
return -1; |
ret = -1; |
| 148 |
|
goto cleanup; |
| 149 |
} |
} |
| 150 |
|
|
| 151 |
if (mysql_query(db, "BEGIN") != 0) |
if (mysql_query(db, "BEGIN") != 0) |
| 152 |
{ |
{ |
| 153 |
log_error("Begin transaction error: %s\n", mysql_error(db)); |
log_error("Begin transaction error: %s\n", mysql_error(db)); |
| 154 |
return -1; |
ret = -1; |
| 155 |
|
goto cleanup; |
| 156 |
} |
} |
| 157 |
|
|
| 158 |
// Failed login attempts from the same source (subnet /24) during certain time period |
// Failed login attempts from the same source (subnet /24) during certain time period |
| 161 |
|
|
| 162 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 163 |
"SELECT COUNT(*) AS err_count FROM user_err_login_log " |
"SELECT COUNT(*) AS err_count FROM user_err_login_log " |
| 164 |
"WHERE login_dt >= SUBDATE(NOW(), INTERVAL 10 MINUTE) " |
"WHERE login_dt >= SUBDATE(NOW(), INTERVAL %d MINUTE) " |
| 165 |
"AND login_ip LIKE '%s'", |
"AND login_ip LIKE '%s'", |
| 166 |
|
BBS_login_failures_count_interval, |
| 167 |
ip_mask(client_addr, 1, '%')); |
ip_mask(client_addr, 1, '%')); |
| 168 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 169 |
{ |
{ |
| 170 |
log_error("Query user_list error: %s\n", mysql_error(db)); |
log_error("Query user_list error: %s\n", mysql_error(db)); |
| 171 |
return -1; |
ret = -1; |
| 172 |
|
goto cleanup; |
| 173 |
} |
} |
| 174 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 175 |
{ |
{ |
| 176 |
log_error("Get user_list data failed\n"); |
log_error("Get user_list data failed\n"); |
| 177 |
return -1; |
ret = -1; |
| 178 |
|
goto cleanup; |
| 179 |
} |
} |
| 180 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 181 |
{ |
{ |
| 182 |
if (atoi(row[0]) >= 2) |
if (atoi(row[0]) > BBS_allowed_login_failures_within_interval) |
| 183 |
{ |
{ |
|
mysql_free_result(rs); |
|
|
|
|
| 184 |
prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n"); |
prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试\033[m\r\n"); |
| 185 |
|
ret = 1; |
| 186 |
return 1; |
goto cleanup; |
| 187 |
} |
} |
| 188 |
} |
} |
| 189 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 190 |
|
rs = NULL; |
| 191 |
|
|
| 192 |
// Failed login attempts against the current username during certain time period |
// Failed login attempts against the current username during certain time period |
| 193 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 197 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 198 |
{ |
{ |
| 199 |
log_error("Query user_list error: %s\n", mysql_error(db)); |
log_error("Query user_list error: %s\n", mysql_error(db)); |
| 200 |
return -1; |
ret = -1; |
| 201 |
|
goto cleanup; |
| 202 |
} |
} |
| 203 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 204 |
{ |
{ |
| 205 |
log_error("Get user_list data failed\n"); |
log_error("Get user_list data failed\n"); |
| 206 |
return -1; |
ret = -1; |
| 207 |
|
goto cleanup; |
| 208 |
} |
} |
| 209 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 210 |
{ |
{ |
| 211 |
if (atoi(row[0]) >= 5) |
if (atoi(row[0]) >= 5) |
| 212 |
{ |
{ |
|
mysql_free_result(rs); |
|
|
|
|
| 213 |
prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n"); |
prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录\033[m\r\n"); |
| 214 |
|
ret = 1; |
| 215 |
return 1; |
goto cleanup; |
| 216 |
} |
} |
| 217 |
} |
} |
| 218 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 219 |
|
rs = NULL; |
| 220 |
|
|
| 221 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 222 |
"SELECT UID, username, p_login FROM user_list " |
"SELECT UID, username, p_login FROM user_list " |
| 225 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 226 |
{ |
{ |
| 227 |
log_error("Query user_list error: %s\n", mysql_error(db)); |
log_error("Query user_list error: %s\n", mysql_error(db)); |
| 228 |
return -1; |
ret = -1; |
| 229 |
|
goto cleanup; |
| 230 |
} |
} |
| 231 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 232 |
{ |
{ |
| 233 |
log_error("Get user_list data failed\n"); |
log_error("Get user_list data failed\n"); |
| 234 |
return -1; |
ret = -1; |
| 235 |
|
goto cleanup; |
| 236 |
} |
} |
| 237 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 238 |
{ |
{ |
| 239 |
BBS_uid = atol(row[0]); |
BBS_uid = atoi(row[0]); |
| 240 |
strncpy(BBS_username, row[1], sizeof(BBS_username) - 1); |
strncpy(BBS_username, row[1], sizeof(BBS_username) - 1); |
| 241 |
BBS_username[sizeof(BBS_username) - 1] = '\0'; |
BBS_username[sizeof(BBS_username) - 1] = '\0'; |
| 242 |
int p_login = atoi(row[2]); |
int p_login = atoi(row[2]); |
| 243 |
|
|
| 244 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 245 |
|
rs = NULL; |
| 246 |
|
|
| 247 |
// Add user login log |
// Add user login log |
| 248 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 249 |
"INSERT INTO user_login_log(UID, login_dt, login_ip) " |
"INSERT INTO user_login_log(UID, login_dt, login_ip) " |
| 250 |
"VALUES(%ld, NOW(), '%s')", |
"VALUES(%d, NOW(), '%s')", |
| 251 |
BBS_uid, hostaddr_client); |
BBS_uid, hostaddr_client); |
| 252 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 253 |
{ |
{ |
| 254 |
log_error("Insert into user_login_log error: %s\n", mysql_error(db)); |
log_error("Insert into user_login_log error: %s\n", mysql_error(db)); |
| 255 |
return -1; |
ret = -1; |
| 256 |
|
goto cleanup; |
| 257 |
} |
} |
| 258 |
|
|
| 259 |
// Commit transaction |
// Commit transaction |
| 260 |
if (mysql_query(db, "COMMIT") != 0) |
if (mysql_query(db, "COMMIT") != 0) |
| 261 |
{ |
{ |
| 262 |
log_error("Commit transaction error: %s\n", mysql_error(db)); |
log_error("Commit transaction error: %s\n", mysql_error(db)); |
| 263 |
return -1; |
ret = -1; |
| 264 |
|
goto cleanup; |
| 265 |
} |
} |
| 266 |
|
|
| 267 |
if (p_login == 0) |
if (p_login == 0) |
| 268 |
{ |
{ |
|
mysql_free_result(rs); |
|
|
|
|
| 269 |
prints("\033[1;31m您目前无权登陆...\033[m\r\n"); |
prints("\033[1;31m您目前无权登陆...\033[m\r\n"); |
| 270 |
return 1; |
ret = 1; |
| 271 |
|
goto cleanup; |
| 272 |
} |
} |
| 273 |
} |
} |
| 274 |
else |
else |
| 275 |
{ |
{ |
| 276 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 277 |
|
rs = NULL; |
| 278 |
|
|
| 279 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 280 |
"INSERT INTO user_err_login_log(username, password, login_dt, login_ip) " |
"INSERT INTO user_err_login_log(username, password, login_dt, login_ip) " |
| 283 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 284 |
{ |
{ |
| 285 |
log_error("Insert into user_err_login_log error: %s\n", mysql_error(db)); |
log_error("Insert into user_err_login_log error: %s\n", mysql_error(db)); |
| 286 |
return -1; |
ret = -1; |
| 287 |
|
goto cleanup; |
| 288 |
} |
} |
| 289 |
|
|
| 290 |
// Commit transaction |
// Commit transaction |
| 291 |
if (mysql_query(db, "COMMIT") != 0) |
if (mysql_query(db, "COMMIT") != 0) |
| 292 |
{ |
{ |
| 293 |
log_error("Commit transaction error: %s\n", mysql_error(db)); |
log_error("Commit transaction error: %s\n", mysql_error(db)); |
| 294 |
return -1; |
ret = -1; |
| 295 |
|
goto cleanup; |
| 296 |
} |
} |
| 297 |
|
|
| 298 |
prints("\033[1;31m错误的用户名或密码...\033[m\r\n"); |
prints("\033[1;31m错误的用户名或密码...\033[m\r\n"); |
| 299 |
return 1; |
ret = 1; |
| 300 |
|
goto cleanup; |
| 301 |
} |
} |
| 302 |
|
|
| 303 |
// Set AUTOCOMMIT = 1 |
// Set AUTOCOMMIT = 1 |
| 304 |
if (mysql_query(db, "SET autocommit=1") != 0) |
if (mysql_query(db, "SET autocommit=1") != 0) |
| 305 |
{ |
{ |
| 306 |
log_error("SET autocommit=1 error: %s\n", mysql_error(db)); |
log_error("SET autocommit=1 error: %s\n", mysql_error(db)); |
| 307 |
return -1; |
ret = -1; |
| 308 |
|
goto cleanup; |
| 309 |
} |
} |
| 310 |
|
|
| 311 |
ret = load_user_info(db, BBS_uid); |
ret = load_user_info(db, BBS_uid); |
| 316 |
break; |
break; |
| 317 |
case -1: // Load data error |
case -1: // Load data error |
| 318 |
prints("\033[1;31m读取用户数据错误...\033[m\r\n"); |
prints("\033[1;31m读取用户数据错误...\033[m\r\n"); |
| 319 |
return -1; |
ret = -1; |
| 320 |
|
goto cleanup; |
| 321 |
case -2: // Unused |
case -2: // Unused |
| 322 |
prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n"); |
prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n"); |
| 323 |
return 1; |
ret = 1; |
| 324 |
|
goto cleanup; |
| 325 |
case -3: // Dead |
case -3: // Dead |
| 326 |
prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n"); |
prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n"); |
| 327 |
return 1; |
ret = 1; |
| 328 |
|
goto cleanup; |
| 329 |
default: |
default: |
| 330 |
return -2; |
ret = -2; |
| 331 |
|
goto cleanup; |
| 332 |
} |
} |
| 333 |
|
|
| 334 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 335 |
"UPDATE user_pubinfo SET visit_count = visit_count + 1, " |
"UPDATE user_pubinfo SET visit_count = visit_count + 1, " |
| 336 |
"last_login_dt = NOW() WHERE UID = %ld", |
"last_login_dt = NOW() WHERE UID = %d", |
| 337 |
BBS_uid); |
BBS_uid); |
| 338 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 339 |
{ |
{ |
| 340 |
log_error("Update user_pubinfo error: %s\n", mysql_error(db)); |
log_error("Update user_pubinfo error: %s\n", mysql_error(db)); |
| 341 |
return -1; |
ret = -1; |
| 342 |
|
goto cleanup; |
| 343 |
} |
} |
| 344 |
|
|
| 345 |
if (user_online_add(db) != 0) |
if (user_online_add(db) != 0) |
| 346 |
{ |
{ |
| 347 |
return -1; |
ret = -1; |
| 348 |
|
goto cleanup; |
| 349 |
} |
} |
| 350 |
|
|
| 351 |
BBS_last_access_tm = BBS_login_tm = time(0); |
BBS_last_access_tm = BBS_login_tm = time(NULL); |
| 352 |
|
|
| 353 |
// Set user tz to process env |
// Set user tz to process env |
| 354 |
if (BBS_user_tz[0] != '\0') |
if (BBS_user_tz[0] != '\0') |
| 366 |
tzset(); |
tzset(); |
| 367 |
} |
} |
| 368 |
|
|
| 369 |
return 0; |
cleanup: |
| 370 |
|
mysql_free_result(rs); |
| 371 |
|
mysql_close(db); |
| 372 |
|
|
| 373 |
|
return ret; |
| 374 |
} |
} |
| 375 |
|
|
| 376 |
int load_user_info(MYSQL *db, long int BBS_uid) |
int load_user_info(MYSQL *db, int BBS_uid) |
| 377 |
{ |
{ |
| 378 |
MYSQL_RES *rs; |
MYSQL_RES *rs = NULL; |
| 379 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 380 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 381 |
int life; |
int life; |
| 382 |
time_t last_login_dt; |
time_t last_login_dt; |
| 383 |
|
int ret = 0; |
| 384 |
|
|
| 385 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 386 |
"SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone " |
"SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone, exp, nickname " |
| 387 |
"FROM user_pubinfo WHERE UID = %ld", |
"FROM user_pubinfo WHERE UID = %d", |
| 388 |
BBS_uid); |
BBS_uid); |
| 389 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 390 |
{ |
{ |
| 391 |
log_error("Query user_pubinfo error: %s\n", mysql_error(db)); |
log_error("Query user_pubinfo error: %s\n", mysql_error(db)); |
| 392 |
return -1; |
ret = -1; |
| 393 |
|
goto cleanup; |
| 394 |
} |
} |
| 395 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 396 |
{ |
{ |
| 397 |
log_error("Get user_pubinfo data failed\n"); |
log_error("Get user_pubinfo data failed\n"); |
| 398 |
return -1; |
ret = -1; |
| 399 |
|
goto cleanup; |
| 400 |
} |
} |
| 401 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 402 |
{ |
{ |
| 405 |
|
|
| 406 |
strncpy(BBS_user_tz, row[2], sizeof(BBS_user_tz) - 1); |
strncpy(BBS_user_tz, row[2], sizeof(BBS_user_tz) - 1); |
| 407 |
BBS_user_tz[sizeof(BBS_user_tz) - 1] = '\0'; |
BBS_user_tz[sizeof(BBS_user_tz) - 1] = '\0'; |
| 408 |
|
|
| 409 |
|
BBS_user_exp = atoi(row[3]); |
| 410 |
|
|
| 411 |
|
strncpy(BBS_nickname, row[4], sizeof(BBS_nickname)); |
| 412 |
|
BBS_nickname[sizeof(BBS_nickname) - 1] = '\0'; |
| 413 |
} |
} |
| 414 |
else |
else |
| 415 |
{ |
{ |
| 416 |
mysql_free_result(rs); |
ret = -1; // Data not found |
| 417 |
return -1; // Data not found |
goto cleanup; |
| 418 |
} |
} |
| 419 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 420 |
|
rs = NULL; |
| 421 |
|
|
| 422 |
if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal |
if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal |
| 423 |
time(0) - last_login_dt > 60 * 60 * 24 * life) |
time(NULL) - last_login_dt > 60 * 60 * 24 * life) |
| 424 |
{ |
{ |
| 425 |
return -3; // Dead |
ret = -3; // Dead |
| 426 |
|
goto cleanup; |
| 427 |
} |
} |
| 428 |
|
|
| 429 |
if (load_priv(db, &BBS_priv, BBS_uid) != 0) |
if (load_priv(db, &BBS_priv, BBS_uid) != 0) |
| 430 |
{ |
{ |
| 431 |
return -1; |
ret = -1; |
| 432 |
|
goto cleanup; |
| 433 |
} |
} |
| 434 |
|
|
| 435 |
return 0; |
cleanup: |
| 436 |
|
mysql_free_result(rs); |
| 437 |
|
|
| 438 |
|
return ret; |
| 439 |
} |
} |
| 440 |
|
|
| 441 |
int load_guest_info(MYSQL *db) |
int load_guest_info(void) |
| 442 |
{ |
{ |
| 443 |
|
MYSQL *db = NULL; |
| 444 |
|
int ret = 0; |
| 445 |
|
|
| 446 |
|
db = db_open(); |
| 447 |
|
if (db == NULL) |
| 448 |
|
{ |
| 449 |
|
ret = -1; |
| 450 |
|
goto cleanup; |
| 451 |
|
} |
| 452 |
|
|
| 453 |
strncpy(BBS_username, "guest", sizeof(BBS_username) - 1); |
strncpy(BBS_username, "guest", sizeof(BBS_username) - 1); |
| 454 |
BBS_username[sizeof(BBS_username) - 1] = '\0'; |
BBS_username[sizeof(BBS_username) - 1] = '\0'; |
| 455 |
|
|
| 456 |
|
BBS_user_exp = 0; |
| 457 |
|
|
| 458 |
|
strncpy(BBS_nickname, "Guest", sizeof(BBS_nickname)); |
| 459 |
|
BBS_nickname[sizeof(BBS_nickname) - 1] = '\0'; |
| 460 |
|
|
| 461 |
if (load_priv(db, &BBS_priv, 0) != 0) |
if (load_priv(db, &BBS_priv, 0) != 0) |
| 462 |
{ |
{ |
| 463 |
return -1; |
ret = -1; |
| 464 |
|
goto cleanup; |
| 465 |
} |
} |
| 466 |
|
|
| 467 |
if (user_online_add(db) != 0) |
if (user_online_add(db) != 0) |
| 468 |
{ |
{ |
| 469 |
return -1; |
ret = -1; |
| 470 |
|
goto cleanup; |
| 471 |
} |
} |
| 472 |
|
|
| 473 |
BBS_last_access_tm = BBS_login_tm = time(0); |
BBS_last_access_tm = BBS_login_tm = time(NULL); |
| 474 |
|
|
| 475 |
return 0; |
cleanup: |
| 476 |
|
mysql_close(db); |
| 477 |
|
|
| 478 |
|
return ret; |
| 479 |
} |
} |
| 480 |
|
|
| 481 |
int user_online_add(MYSQL *db) |
int user_online_add(MYSQL *db) |
| 489 |
|
|
| 490 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 491 |
"INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) " |
"INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) " |
| 492 |
"VALUES('Telnet_Process_%d', %ld, '%s', NOW(), NOW())", |
"VALUES('Telnet_Process_%d', %d, '%s', NOW(), NOW())", |
| 493 |
getpid(), BBS_priv.uid, hostaddr_client); |
getpid(), BBS_priv.uid, hostaddr_client); |
| 494 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 495 |
{ |
{ |
| 515 |
|
|
| 516 |
return 0; |
return 0; |
| 517 |
} |
} |
| 518 |
|
|
| 519 |
|
int user_online_update(const char *action) |
| 520 |
|
{ |
| 521 |
|
MYSQL *db = NULL; |
| 522 |
|
char sql[SQL_BUFFER_LEN]; |
| 523 |
|
|
| 524 |
|
if (strcmp(BBS_current_action, action) == 0) // No change |
| 525 |
|
{ |
| 526 |
|
return 0; |
| 527 |
|
} |
| 528 |
|
|
| 529 |
|
strncpy(BBS_current_action, action, sizeof(BBS_current_action) - 1); |
| 530 |
|
BBS_current_action[sizeof(BBS_current_action) - 1] = '\0'; |
| 531 |
|
|
| 532 |
|
db = db_open(); |
| 533 |
|
if (db == NULL) |
| 534 |
|
{ |
| 535 |
|
log_error("db_open() error: %s\n", mysql_error(db)); |
| 536 |
|
return -1; |
| 537 |
|
} |
| 538 |
|
|
| 539 |
|
snprintf(sql, sizeof(sql), |
| 540 |
|
"UPDATE user_online SET current_action = '%s', last_tm=NOW() " |
| 541 |
|
"WHERE SID = 'Telnet_Process_%d'", |
| 542 |
|
BBS_current_action, getpid()); |
| 543 |
|
if (mysql_query(db, sql) != 0) |
| 544 |
|
{ |
| 545 |
|
log_error("Update user_online error: %s\n", mysql_error(db)); |
| 546 |
|
return -2; |
| 547 |
|
} |
| 548 |
|
|
| 549 |
|
mysql_close(db); |
| 550 |
|
|
| 551 |
|
return 1; |
| 552 |
|
} |