Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 59.62 KB | None | 0 0
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3.  
  4. #include "../common/core.h"
  5. #include "../common/db.h"
  6. #include "../common/malloc.h"
  7. #include "../common/md5calc.h"
  8. #include "../common/showmsg.h"
  9. #include "../common/socket.h"
  10. #include "../common/strlib.h"
  11. #include "../common/timer.h"
  12. #include "../common/version.h"
  13. #include "account.h"
  14. #include "ipban.h"
  15. #include "login.h"
  16. #include "loginlog.h"
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. char internalguard_key[255];
  22.  
  23. struct Login_Config login_config;
  24.  
  25. int login_fd; // login server socket
  26. struct mmo_char_server server[MAX_SERVERS]; // char server data
  27. bool client_hash_active = false;
  28. uint8 client_hash[16];
  29.  
  30. // Account engines available
  31. static struct{
  32. AccountDB* (*constructor)(void);
  33. AccountDB* db;
  34. } account_engines[] = {
  35. #ifdef WITH_TXT
  36. {account_db_txt, NULL},
  37. #endif
  38. #ifdef WITH_SQL
  39. {account_db_sql, NULL},
  40. #endif
  41. #ifdef ACCOUNTDB_ENGINE_0
  42. {ACCOUNTDB_CONSTRUCTOR(ACCOUNTDB_ENGINE_0), NULL},
  43. #endif
  44. #ifdef ACCOUNTDB_ENGINE_1
  45. {ACCOUNTDB_CONSTRUCTOR(ACCOUNTDB_ENGINE_1), NULL},
  46. #endif
  47. #ifdef ACCOUNTDB_ENGINE_2
  48. {ACCOUNTDB_CONSTRUCTOR(ACCOUNTDB_ENGINE_2), NULL},
  49. #endif
  50. #ifdef ACCOUNTDB_ENGINE_3
  51. {ACCOUNTDB_CONSTRUCTOR(ACCOUNTDB_ENGINE_3), NULL},
  52. #endif
  53. #ifdef ACCOUNTDB_ENGINE_4
  54. {ACCOUNTDB_CONSTRUCTOR(ACCOUNTDB_ENGINE_4), NULL},
  55. #endif
  56. // end of structure
  57. {NULL, NULL}
  58. };
  59. // account database
  60. AccountDB* accounts = NULL;
  61.  
  62. //Account registration flood protection [Kevin]
  63. int allowed_regs = 1;
  64. int time_allowed = 10; //in seconds
  65.  
  66. // Advanced subnet check [LuzZza]
  67. struct s_subnet {
  68. uint32 mask;
  69. uint32 char_ip;
  70. uint32 map_ip;
  71. } subnet[16];
  72.  
  73. int subnet_count = 0;
  74.  
  75. int mmo_auth_new(const char* userid, const char* pass, const char sex, const char* last_ip);
  76.  
  77. //-----------------------------------------------------
  78. // Auth database
  79. //-----------------------------------------------------
  80. #define AUTH_TIMEOUT 30000
  81.  
  82. struct auth_node {
  83.  
  84. int account_id;
  85. uint32 login_id1;
  86. uint32 login_id2;
  87. uint32 ip;
  88. char sex;
  89. uint32 version;
  90. uint8 clienttype;
  91. };
  92.  
  93. static DBMap* auth_db; // int account_id -> struct auth_node*
  94.  
  95.  
  96. //-----------------------------------------------------
  97. // Online User Database [Wizputer]
  98. //-----------------------------------------------------
  99. struct online_login_data {
  100.  
  101. int account_id;
  102. int waiting_disconnect;
  103. int char_server;
  104. };
  105.  
  106. static DBMap* online_db; // int account_id -> struct online_login_data*
  107. static int waiting_disconnect_timer(int tid, unsigned int tick, int id, intptr_t data);
  108.  
  109. static void* create_online_user(DBKey key, va_list args)
  110. {
  111. struct online_login_data* p;
  112. CREATE(p, struct online_login_data, 1);
  113. p->account_id = key.i;
  114. p->char_server = -1;
  115. p->waiting_disconnect = INVALID_TIMER;
  116. return p;
  117. }
  118.  
  119. struct online_login_data* add_online_user(int char_server, int account_id)
  120. {
  121. struct online_login_data* p;
  122. p = (struct online_login_data*)idb_ensure(online_db, account_id, create_online_user);
  123. p->char_server = char_server;
  124. if( p->waiting_disconnect != INVALID_TIMER )
  125. {
  126. delete_timer(p->waiting_disconnect, waiting_disconnect_timer);
  127. p->waiting_disconnect = INVALID_TIMER;
  128. }
  129. return p;
  130. }
  131.  
  132. void remove_online_user(int account_id)
  133. {
  134. struct online_login_data* p;
  135. p = (struct online_login_data*)idb_get(online_db, account_id);
  136. if( p == NULL )
  137. return;
  138. if( p->waiting_disconnect != INVALID_TIMER )
  139. delete_timer(p->waiting_disconnect, waiting_disconnect_timer);
  140.  
  141. idb_remove(online_db, account_id);
  142. }
  143.  
  144. static int waiting_disconnect_timer(int tid, unsigned int tick, int id, intptr_t data)
  145. {
  146. struct online_login_data* p = (struct online_login_data*)idb_get(online_db, id);
  147. if( p != NULL && p->waiting_disconnect == tid && p->account_id == id )
  148. {
  149. p->waiting_disconnect = INVALID_TIMER;
  150. remove_online_user(id);
  151. idb_remove(auth_db, id);
  152. }
  153. return 0;
  154. }
  155.  
  156. static int online_db_setoffline(DBKey key, void* data, va_list ap)
  157. {
  158. struct online_login_data* p = (struct online_login_data*)data;
  159. int server = va_arg(ap, int);
  160. if( server == -1 )
  161. {
  162. p->char_server = -1;
  163. if( p->waiting_disconnect != INVALID_TIMER )
  164. {
  165. delete_timer(p->waiting_disconnect, waiting_disconnect_timer);
  166. p->waiting_disconnect = INVALID_TIMER;
  167. }
  168. }
  169. else if( p->char_server == server )
  170. p->char_server = -2; //Char server disconnected.
  171. return 0;
  172. }
  173.  
  174. static int online_data_cleanup_sub(DBKey key, void *data, va_list ap)
  175. {
  176. struct online_login_data *character= (struct online_login_data*)data;
  177. if (character->char_server == -2) //Unknown server.. set them offline
  178. remove_online_user(character->account_id);
  179. return 0;
  180. }
  181.  
  182. static int online_data_cleanup(int tid, unsigned int tick, int id, intptr_t data)
  183. {
  184. online_db->foreach(online_db, online_data_cleanup_sub);
  185. return 0;
  186. }
  187.  
  188.  
  189. //--------------------------------------------------------------------
  190. // Packet send to all char-servers, except one (wos: without our self)
  191. //--------------------------------------------------------------------
  192. int charif_sendallwos(int sfd, uint8* buf, size_t len)
  193. {
  194. int i, c;
  195.  
  196. for( i = 0, c = 0; i < ARRAYLENGTH(server); ++i )
  197. {
  198. int fd = server[i].fd;
  199. if( session_isValid(fd) && fd != sfd )
  200. {
  201. WFIFOHEAD(fd,len);
  202. memcpy(WFIFOP(fd,0), buf, len);
  203. WFIFOSET(fd,len);
  204. ++c;
  205. }
  206. }
  207.  
  208. return c;
  209. }
  210.  
  211.  
  212. /// Initializes a server structure.
  213. void chrif_server_init(int id)
  214. {
  215. memset(&server[id], 0, sizeof(server[id]));
  216. server[id].fd = -1;
  217. }
  218.  
  219.  
  220. /// Destroys a server structure.
  221. void chrif_server_destroy(int id)
  222. {
  223. if( server[id].fd != -1 )
  224. {
  225. do_close(server[id].fd);
  226. server[id].fd = -1;
  227. }
  228. }
  229.  
  230.  
  231. /// Resets all the data related to a server.
  232. void chrif_server_reset(int id)
  233. {
  234. online_db->foreach(online_db, online_db_setoffline, id); //Set all chars from this char server to offline.
  235. chrif_server_destroy(id);
  236. chrif_server_init(id);
  237. }
  238.  
  239.  
  240. /// Called when the connection to Char Server is disconnected.
  241. void chrif_on_disconnect(int id)
  242. {
  243. ShowStatus("Char-server '%s' has disconnected.\n", server[id].name);
  244. chrif_server_reset(id);
  245. }
  246.  
  247.  
  248. //-----------------------------------------------------
  249. // periodic ip address synchronization
  250. //-----------------------------------------------------
  251. static int sync_ip_addresses(int tid, unsigned int tick, int id, intptr_t data)
  252. {
  253. uint8 buf[2];
  254. ShowInfo("IP Sync in progress...\n");
  255. WBUFW(buf,0) = 0x2735;
  256. charif_sendallwos(-1, buf, 2);
  257. return 0;
  258. }
  259.  
  260.  
  261. //-----------------------------------------------------
  262. // encrypted/unencrypted password check (from eApp)
  263. //-----------------------------------------------------
  264. bool check_encrypted(const char* str1, const char* str2, const char* passwd)
  265. {
  266. char tmpstr[64+1], md5str[32+1];
  267.  
  268. safesnprintf(tmpstr, sizeof(tmpstr), "%s%s", str1, str2);
  269. MD5_String(tmpstr, md5str);
  270.  
  271. return (0==strcmp(passwd, md5str));
  272. }
  273.  
  274. bool check_password(const char* md5key, int passwdenc, const char* passwd, const char* refpass)
  275. {
  276. if(passwdenc == 0)
  277. {
  278. return (0==strcmp(passwd, refpass));
  279. }
  280. else
  281. {
  282. // password mode set to 1 -> md5(md5key, refpass) enable with <passwordencrypt></passwordencrypt>
  283. // password mode set to 2 -> md5(refpass, md5key) enable with <passwordencrypt2></passwordencrypt2>
  284.  
  285. return ((passwdenc&0x01) && check_encrypted(md5key, refpass, passwd)) ||
  286. ((passwdenc&0x02) && check_encrypted(refpass, md5key, passwd));
  287. }
  288. }
  289.  
  290.  
  291. //-----------------------------------------------------
  292. // custom timestamp formatting (from eApp)
  293. //-----------------------------------------------------
  294. const char* timestamp2string(char* str, size_t size, time_t timestamp, const char* format)
  295. {
  296. size_t len = strftime(str, size, format, localtime(&timestamp));
  297. memset(str + len, '\0', size - len);
  298. return str;
  299. }
  300.  
  301.  
  302. //--------------------------------------------
  303. // Test to know if an IP come from LAN or WAN.
  304. //--------------------------------------------
  305. int lan_subnetcheck(uint32 ip)
  306. {
  307. int i;
  308. ARR_FIND( 0, subnet_count, i, (subnet[i].char_ip & subnet[i].mask) == (ip & subnet[i].mask) );
  309. return ( i < subnet_count ) ? subnet[i].char_ip : 0;
  310. }
  311.  
  312. //----------------------------------
  313. // Reading Lan Support configuration
  314. //----------------------------------
  315. int login_lan_config_read(const char *lancfgName)
  316. {
  317. FILE *fp;
  318. int line_num = 0;
  319. char line[1024], w1[64], w2[64], w3[64], w4[64];
  320.  
  321. if((fp = fopen(lancfgName, "r")) == NULL) {
  322. ShowWarning("LAN Support configuration file is not found: %s\n", lancfgName);
  323. return 1;
  324. }
  325.  
  326. ShowInfo("Reading the configuration file %s...\n", lancfgName);
  327.  
  328. while(fgets(line, sizeof(line), fp))
  329. {
  330. line_num++;
  331. if ((line[0] == '/' && line[1] == '/') || line[0] == '\n' || line[1] == '\n')
  332. continue;
  333.  
  334. if(sscanf(line,"%[^:]: %[^:]:%[^:]:%[^\r\n]", w1, w2, w3, w4) != 4)
  335. {
  336. ShowWarning("Error syntax of configuration file %s in line %d.\n", lancfgName, line_num);
  337. continue;
  338. }
  339.  
  340. if( strcmpi(w1, "subnet") == 0 )
  341. {
  342. subnet[subnet_count].mask = str2ip(w2);
  343. subnet[subnet_count].char_ip = str2ip(w3);
  344. subnet[subnet_count].map_ip = str2ip(w4);
  345.  
  346. if( (subnet[subnet_count].char_ip & subnet[subnet_count].mask) != (subnet[subnet_count].map_ip & subnet[subnet_count].mask) )
  347. {
  348. ShowError("%s: Configuration Error: The char server (%s) and map server (%s) belong to different subnetworks!\n", lancfgName, w3, w4);
  349. continue;
  350. }
  351.  
  352. subnet_count++;
  353. }
  354. }
  355.  
  356. ShowStatus("Read information about %d subnetworks.\n", subnet_count);
  357.  
  358. fclose(fp);
  359. return 0;
  360. }
  361.  
  362. //-----------------------
  363. // Console Command Parser [Wizputer]
  364. //-----------------------
  365. int parse_console(const char* command)
  366. {
  367. ShowNotice("Console command: %s\n", command);
  368.  
  369. if( strcmpi("shutdown", command) == 0 || strcmpi("exit", command) == 0 || strcmpi("quit", command) == 0 || strcmpi("end", command) == 0 )
  370. runflag = SERVER_STATE_STOP;
  371. else if( strcmpi("alive", command) == 0 || strcmpi("status", command) == 0 )
  372. ShowInfo(CL_CYAN"Console: "CL_BOLD"I'm Alive."CL_RESET"\n");
  373. else if( strcmpi("help", command) == 0 )
  374. {
  375. ShowInfo("To shutdown the server:\n");
  376. ShowInfo(" 'shutdown|exit|quit|end'\n");
  377. ShowInfo("To know if server is alive:\n");
  378. ShowInfo(" 'alive|status'\n");
  379. ShowInfo("To create a new account:\n");
  380. ShowInfo(" 'create'\n");
  381. }
  382. else
  383. {// commands with parameters
  384. char cmd[128], params[256];
  385.  
  386. if( sscanf(command, "%127s %255[^\r\n]", cmd, params) < 2 )
  387. {
  388. return 0;
  389. }
  390.  
  391. if( strcmpi(cmd, "create") == 0 )
  392. {
  393. char username[NAME_LENGTH], password[NAME_LENGTH], sex;
  394.  
  395. if( sscanf(params, "%23s %23s %c", username, password, &sex) < 3 || strnlen(username, sizeof(username)) < 4 || strnlen(password, sizeof(password)) < 1 )
  396. {
  397. ShowWarning("Console: Invalid parameters for '%s'. Usage: %s <username> <password> <sex:F/M>\n", cmd, cmd);
  398. return 0;
  399. }
  400.  
  401. if( mmo_auth_new(username, password, TOUPPER(sex), "0.0.0.0") != -1 )
  402. {
  403. ShowError("Console: Account creation failed.\n");
  404. return 0;
  405. }
  406. ShowStatus("Console: Account '%s' created successfully.\n", username);
  407. }
  408. }
  409.  
  410. return 0;
  411. }
  412.  
  413.  
  414. //--------------------------------
  415. // Packet parsing for char-servers
  416. //--------------------------------
  417. int parse_fromchar(int fd)
  418. {
  419. int j, id;
  420. uint32 ipl;
  421. char ip[16];
  422.  
  423. ARR_FIND( 0, ARRAYLENGTH(server), id, server[id].fd == fd );
  424. if( id == ARRAYLENGTH(server) )
  425. {// not a char server
  426. ShowDebug("parse_fromchar: Disconnecting invalid session #%d (is not a char-server)\n", fd);
  427. set_eof(fd);
  428. do_close(fd);
  429. return 0;
  430. }
  431.  
  432. if( session[fd]->flag.eof )
  433. {
  434. do_close(fd);
  435. server[id].fd = -1;
  436. chrif_on_disconnect(id);
  437. return 0;
  438. }
  439.  
  440. ipl = server[id].ip;
  441. ip2str(ipl, ip);
  442.  
  443. while( RFIFOREST(fd) >= 2 )
  444. {
  445. uint16 command = RFIFOW(fd,0);
  446.  
  447. switch( command )
  448. {
  449.  
  450. case 0x2712: // request from char-server to authenticate an account
  451. if( RFIFOREST(fd) < 23 )
  452. return 0;
  453. {
  454. struct auth_node* node;
  455.  
  456. int account_id = RFIFOL(fd,2);
  457. uint32 login_id1 = RFIFOL(fd,6);
  458. uint32 login_id2 = RFIFOL(fd,10);
  459. uint8 sex = RFIFOB(fd,14);
  460. //uint32 ip_ = ntohl(RFIFOL(fd,15));
  461. int request_id = RFIFOL(fd,19);
  462. RFIFOSKIP(fd,23);
  463.  
  464. node = (struct auth_node*)idb_get(auth_db, account_id);
  465. if( runflag == SERVER_STATE_RUN &&
  466. node != NULL &&
  467. node->account_id == account_id &&
  468. node->login_id1 == login_id1 &&
  469. node->login_id2 == login_id2 &&
  470. node->sex == sex_num2str(sex) /*&&
  471. node->ip == ip_*/ )
  472. {// found
  473. //ShowStatus("Char-server '%s': authentication of the account %d accepted (ip: %s).\n", server[id].name, account_id, ip);
  474.  
  475. // send ack
  476. WFIFOHEAD(fd,25);
  477. WFIFOW(fd,0) = 0x2713;
  478. WFIFOL(fd,2) = account_id;
  479. WFIFOL(fd,6) = login_id1;
  480. WFIFOL(fd,10) = login_id2;
  481. WFIFOB(fd,14) = sex;
  482. WFIFOB(fd,15) = 0;// ok
  483. WFIFOL(fd,16) = request_id;
  484. WFIFOL(fd,20) = node->version;
  485. WFIFOB(fd,24) = node->clienttype;
  486. WFIFOSET(fd,25);
  487.  
  488. // each auth entry can only be used once
  489. idb_remove(auth_db, account_id);
  490. }
  491. else
  492. {// authentication not found
  493. ShowStatus("Char-server '%s': authentication of the account %d REFUSED (ip: %s).\n", server[id].name, account_id, ip);
  494. WFIFOHEAD(fd,25);
  495. WFIFOW(fd,0) = 0x2713;
  496. WFIFOL(fd,2) = account_id;
  497. WFIFOL(fd,6) = login_id1;
  498. WFIFOL(fd,10) = login_id2;
  499. WFIFOB(fd,14) = sex;
  500. WFIFOB(fd,15) = 1;// auth failed
  501. WFIFOL(fd,16) = request_id;
  502. WFIFOL(fd,20) = 0;
  503. WFIFOB(fd,24) = 0;
  504. WFIFOSET(fd,25);
  505. }
  506. }
  507. break;
  508.  
  509. case 0x2714:
  510. if( RFIFOREST(fd) < 6 )
  511. return 0;
  512. {
  513. int users = RFIFOL(fd,2);
  514. RFIFOSKIP(fd,6);
  515.  
  516. // how many users on world? (update)
  517. if( server[id].users != users )
  518. {
  519. ShowStatus("set users %s : %d\n", server[id].name, users);
  520.  
  521. server[id].users = users;
  522. }
  523. }
  524. break;
  525.  
  526. case 0x2715: // request from char server to change e-email from default "a@a.com"
  527. if (RFIFOREST(fd) < 46)
  528. return 0;
  529. {
  530. struct mmo_account acc;
  531. char email[40];
  532.  
  533. int account_id = RFIFOL(fd,2);
  534. safestrncpy(email, (char*)RFIFOP(fd,6), 40); remove_control_chars(email);
  535. RFIFOSKIP(fd,46);
  536.  
  537. if( e_mail_check(email) == 0 )
  538. ShowNotice("Char-server '%s': Attempt to create an e-mail on an account with a default e-mail REFUSED - e-mail is invalid (account: %d, ip: %s)\n", server[id].name, account_id, ip);
  539. else
  540. if( !accounts->load_num(accounts, &acc, account_id) || strcmp(acc.email, "a@a.com") == 0 || acc.email[0] == '\0' )
  541. ShowNotice("Char-server '%s': Attempt to create an e-mail on an account with a default e-mail REFUSED - account doesn't exist or e-mail of account isn't default e-mail (account: %d, ip: %s).\n", server[id].name, account_id, ip);
  542. else {
  543. memcpy(acc.email, email, 40);
  544. ShowNotice("Char-server '%s': Create an e-mail on an account with a default e-mail (account: %d, new e-mail: %s, ip: %s).\n", server[id].name, account_id, email, ip);
  545. // Save
  546. accounts->save(accounts, &acc);
  547. }
  548. }
  549. break;
  550.  
  551. case 0x2716: // request account data
  552. if( RFIFOREST(fd) < 6 )
  553. return 0;
  554. {
  555. struct mmo_account acc;
  556. time_t expiration_time = 0;
  557. char email[40] = "";
  558. int gmlevel = 0;
  559. char birthdate[10+1] = "";
  560.  
  561. int account_id = RFIFOL(fd,2);
  562. RFIFOSKIP(fd,6);
  563.  
  564. if( !accounts->load_num(accounts, &acc, account_id) )
  565. ShowNotice("Char-server '%s': account %d NOT found (ip: %s).\n", server[id].name, account_id, ip);
  566. else
  567. {
  568. safestrncpy(email, acc.email, sizeof(email));
  569. expiration_time = acc.expiration_time;
  570. gmlevel = acc.level;
  571. safestrncpy(birthdate, acc.birthdate, sizeof(birthdate));
  572. }
  573.  
  574. WFIFOHEAD(fd,62);
  575. WFIFOW(fd,0) = 0x2717;
  576. WFIFOL(fd,2) = account_id;
  577. safestrncpy((char*)WFIFOP(fd,6), email, 40);
  578. WFIFOL(fd,46) = (uint32)expiration_time;
  579. WFIFOB(fd,50) = gmlevel;
  580. safestrncpy((char*)WFIFOP(fd,51), birthdate, 10+1);
  581. WFIFOSET(fd,62);
  582. }
  583. break;
  584.  
  585. case 0x2719: // ping request from charserver
  586. RFIFOSKIP(fd,2);
  587.  
  588. WFIFOHEAD(fd,2);
  589. WFIFOW(fd,0) = 0x2718;
  590. WFIFOSET(fd,2);
  591. break;
  592.  
  593. // Map server send information to change an email of an account via char-server
  594. case 0x2722: // 0x2722 <account_id>.L <actual_e-mail>.40B <new_e-mail>.40B
  595. if (RFIFOREST(fd) < 86)
  596. return 0;
  597. {
  598. struct mmo_account acc;
  599. char actual_email[40];
  600. char new_email[40];
  601.  
  602. int account_id = RFIFOL(fd,2);
  603. safestrncpy(actual_email, (char*)RFIFOP(fd,6), 40);
  604. safestrncpy(new_email, (char*)RFIFOP(fd,46), 40);
  605. RFIFOSKIP(fd, 86);
  606.  
  607. if( e_mail_check(actual_email) == 0 )
  608. ShowNotice("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command), but actual email is invalid (account: %d, ip: %s)\n", server[id].name, account_id, ip);
  609. else
  610. if( e_mail_check(new_email) == 0 )
  611. ShowNotice("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command) with a invalid new e-mail (account: %d, ip: %s)\n", server[id].name, account_id, ip);
  612. else
  613. if( strcmpi(new_email, "a@a.com") == 0 )
  614. ShowNotice("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command) with a default e-mail (account: %d, ip: %s)\n", server[id].name, account_id, ip);
  615. else
  616. if( !accounts->load_num(accounts, &acc, account_id) )
  617. ShowNotice("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command), but account doesn't exist (account: %d, ip: %s).\n", server[id].name, account_id, ip);
  618. else
  619. if( strcmpi(acc.email, actual_email) != 0 )
  620. ShowNotice("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command), but actual e-mail is incorrect (account: %d (%s), actual e-mail: %s, proposed e-mail: %s, ip: %s).\n", server[id].name, account_id, acc.userid, acc.email, actual_email, ip);
  621. else {
  622. safestrncpy(acc.email, new_email, 40);
  623. ShowNotice("Char-server '%s': Modify an e-mail on an account (@email GM command) (account: %d (%s), new e-mail: %s, ip: %s).\n", server[id].name, account_id, acc.userid, new_email, ip);
  624. // Save
  625. accounts->save(accounts, &acc);
  626. }
  627. }
  628. break;
  629.  
  630. case 0x2724: // Receiving an account state update request from a map-server (relayed via char-server)
  631. if (RFIFOREST(fd) < 10)
  632. return 0;
  633. {
  634. struct mmo_account acc;
  635.  
  636. int account_id = RFIFOL(fd,2);
  637. unsigned int state = RFIFOL(fd,6);
  638. RFIFOSKIP(fd,10);
  639.  
  640. if( !accounts->load_num(accounts, &acc, account_id) )
  641. ShowNotice("Char-server '%s': Error of Status change (account: %d not found, suggested status %d, ip: %s).\n", server[id].name, account_id, state, ip);
  642. else
  643. if( acc.state == state )
  644. ShowNotice("Char-server '%s': Error of Status change - actual status is already the good status (account: %d, status %d, ip: %s).\n", server[id].name, account_id, state, ip);
  645. else {
  646. ShowNotice("Char-server '%s': Status change (account: %d, new status %d, ip: %s).\n", server[id].name, account_id, state, ip);
  647.  
  648. acc.state = state;
  649. // Save
  650. accounts->save(accounts, &acc);
  651.  
  652. // notify other servers
  653. if (state != 0) {
  654. uint8 buf[11];
  655. WBUFW(buf,0) = 0x2731;
  656. WBUFL(buf,2) = account_id;
  657. WBUFB(buf,6) = 0; // 0: change of state, 1: ban
  658. WBUFL(buf,7) = state; // status or final date of a banishment
  659. charif_sendallwos(-1, buf, 11);
  660. }
  661. }
  662. }
  663. break;
  664.  
  665. case 0x2725: // Receiving of map-server via char-server a ban request
  666. if (RFIFOREST(fd) < 18)
  667. return 0;
  668. {
  669. struct mmo_account acc;
  670.  
  671. int account_id = RFIFOL(fd,2);
  672. int year = (short)RFIFOW(fd,6);
  673. int month = (short)RFIFOW(fd,8);
  674. int mday = (short)RFIFOW(fd,10);
  675. int hour = (short)RFIFOW(fd,12);
  676. int min = (short)RFIFOW(fd,14);
  677. int sec = (short)RFIFOW(fd,16);
  678. RFIFOSKIP(fd,18);
  679.  
  680. if( !accounts->load_num(accounts, &acc, account_id) )
  681. ShowNotice("Char-server '%s': Error of ban request (account: %d not found, ip: %s).\n", server[id].name, account_id, ip);
  682. else
  683. {
  684. time_t timestamp;
  685. struct tm *tmtime;
  686. if (acc.unban_time == 0 || acc.unban_time < time(NULL))
  687. timestamp = time(NULL); // new ban
  688. else
  689. timestamp = acc.unban_time; // add to existing ban
  690. tmtime = localtime(&timestamp);
  691. tmtime->tm_year = tmtime->tm_year + year;
  692. tmtime->tm_mon = tmtime->tm_mon + month;
  693. tmtime->tm_mday = tmtime->tm_mday + mday;
  694. tmtime->tm_hour = tmtime->tm_hour + hour;
  695. tmtime->tm_min = tmtime->tm_min + min;
  696. tmtime->tm_sec = tmtime->tm_sec + sec;
  697. timestamp = mktime(tmtime);
  698. if (timestamp == -1)
  699. ShowNotice("Char-server '%s': Error of ban request (account: %d, invalid date, ip: %s).\n", server[id].name, account_id, ip);
  700. else
  701. if( timestamp <= time(NULL) || timestamp == 0 )
  702. ShowNotice("Char-server '%s': Error of ban request (account: %d, new date unbans the account, ip: %s).\n", server[id].name, account_id, ip);
  703. else
  704. {
  705. uint8 buf[11];
  706. char tmpstr[24];
  707. timestamp2string(tmpstr, sizeof(tmpstr), timestamp, login_config.date_format);
  708. ShowNotice("Char-server '%s': Ban request (account: %d, new final date of banishment: %d (%s), ip: %s).\n", server[id].name, account_id, timestamp, tmpstr, ip);
  709.  
  710. acc.unban_time = timestamp;
  711.  
  712. // Save
  713. accounts->save(accounts, &acc);
  714.  
  715. WBUFW(buf,0) = 0x2731;
  716. WBUFL(buf,2) = account_id;
  717. WBUFB(buf,6) = 1; // 0: change of status, 1: ban
  718. WBUFL(buf,7) = (uint32)timestamp; // status or final date of a banishment
  719. charif_sendallwos(-1, buf, 11);
  720. }
  721. }
  722. }
  723. break;
  724.  
  725. case 0x2727: // Change of sex (sex is reversed)
  726. if( RFIFOREST(fd) < 6 )
  727. return 0;
  728. {
  729. struct mmo_account acc;
  730.  
  731. int account_id = RFIFOL(fd,2);
  732. RFIFOSKIP(fd,6);
  733.  
  734. if( !accounts->load_num(accounts, &acc, account_id) )
  735. ShowNotice("Char-server '%s': Error of sex change (account: %d not found, ip: %s).\n", server[id].name, account_id, ip);
  736. else
  737. if( acc.sex == 'S' )
  738. ShowNotice("Char-server '%s': Error of sex change - account to change is a Server account (account: %d, ip: %s).\n", server[id].name, account_id, ip);
  739. else
  740. {
  741. unsigned char buf[7];
  742. char sex = ( acc.sex == 'M' ) ? 'F' : 'M'; //Change gender
  743.  
  744. ShowNotice("Char-server '%s': Sex change (account: %d, new sex %c, ip: %s).\n", server[id].name, account_id, sex, ip);
  745.  
  746. acc.sex = sex;
  747. // Save
  748. accounts->save(accounts, &acc);
  749.  
  750. // announce to other servers
  751. WBUFW(buf,0) = 0x2723;
  752. WBUFL(buf,2) = account_id;
  753. WBUFB(buf,6) = sex_str2num(sex);
  754. charif_sendallwos(-1, buf, 7);
  755. }
  756. }
  757. break;
  758.  
  759. case 0x2728: // We receive account_reg2 from a char-server, and we send them to other map-servers.
  760. if( RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2) )
  761. return 0;
  762. {
  763. struct mmo_account acc;
  764.  
  765. int account_id = RFIFOL(fd,4);
  766.  
  767. if( !accounts->load_num(accounts, &acc, account_id) )
  768. ShowStatus("Char-server '%s': receiving (from the char-server) of account_reg2 (account: %d not found, ip: %s).\n", server[id].name, account_id, ip);
  769. else
  770. {
  771. int len;
  772. int p;
  773. ShowNotice("char-server '%s': receiving (from the char-server) of account_reg2 (account: %d, ip: %s).\n", server[id].name, account_id, ip);
  774. for( j = 0, p = 13; j < ACCOUNT_REG2_NUM && p < RFIFOW(fd,2); ++j )
  775. {
  776. sscanf((char*)RFIFOP(fd,p), "%31c%n", acc.account_reg2[j].str, &len);
  777. acc.account_reg2[j].str[len]='\0';
  778. p +=len+1; //+1 to skip the '\0' between strings.
  779. sscanf((char*)RFIFOP(fd,p), "%255c%n", acc.account_reg2[j].value, &len);
  780. acc.account_reg2[j].value[len]='\0';
  781. p +=len+1;
  782. remove_control_chars(acc.account_reg2[j].str);
  783. remove_control_chars(acc.account_reg2[j].value);
  784. }
  785. acc.account_reg2_num = j;
  786.  
  787. // Save
  788. accounts->save(accounts, &acc);
  789.  
  790. // Sending information towards the other char-servers.
  791. RFIFOW(fd,0) = 0x2729;// reusing read buffer
  792. charif_sendallwos(fd, RFIFOP(fd,0), RFIFOW(fd,2));
  793. }
  794. RFIFOSKIP(fd,RFIFOW(fd,2));
  795. }
  796. break;
  797.  
  798. case 0x272a: // Receiving of map-server via char-server an unban request
  799. if( RFIFOREST(fd) < 6 )
  800. return 0;
  801. {
  802. struct mmo_account acc;
  803.  
  804. int account_id = RFIFOL(fd,2);
  805. RFIFOSKIP(fd,6);
  806.  
  807. if( !accounts->load_num(accounts, &acc, account_id) )
  808. ShowNotice("Char-server '%s': Error of UnBan request (account: %d not found, ip: %s).\n", server[id].name, account_id, ip);
  809. else
  810. if( acc.unban_time == 0 )
  811. ShowNotice("Char-server '%s': Error of UnBan request (account: %d, no change for unban date, ip: %s).\n", server[id].name, account_id, ip);
  812. else
  813. {
  814. ShowNotice("Char-server '%s': UnBan request (account: %d, ip: %s).\n", server[id].name, account_id, ip);
  815. acc.unban_time = 0;
  816. accounts->save(accounts, &acc);
  817. }
  818. }
  819. break;
  820.  
  821. case 0x272b: // Set account_id to online [Wizputer]
  822. if( RFIFOREST(fd) < 6 )
  823. return 0;
  824. add_online_user(id, RFIFOL(fd,2));
  825. RFIFOSKIP(fd,6);
  826. break;
  827.  
  828. case 0x272c: // Set account_id to offline [Wizputer]
  829. if( RFIFOREST(fd) < 6 )
  830. return 0;
  831. remove_online_user(RFIFOL(fd,2));
  832. RFIFOSKIP(fd,6);
  833. break;
  834.  
  835. case 0x272d: // Receive list of all online accounts. [Skotlex]
  836. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  837. return 0;
  838. {
  839. struct online_login_data *p;
  840. int aid;
  841. uint32 i, users;
  842. online_db->foreach(online_db, online_db_setoffline, id); //Set all chars from this char-server offline first
  843. users = RFIFOW(fd,4);
  844. for (i = 0; i < users; i++) {
  845. aid = RFIFOL(fd,6+i*4);
  846. p = (struct online_login_data*)idb_ensure(online_db, aid, create_online_user);
  847. p->char_server = id;
  848. if (p->waiting_disconnect != INVALID_TIMER)
  849. {
  850. delete_timer(p->waiting_disconnect, waiting_disconnect_timer);
  851. p->waiting_disconnect = INVALID_TIMER;
  852. }
  853. }
  854. }
  855. RFIFOSKIP(fd,RFIFOW(fd,2));
  856. break;
  857.  
  858. case 0x272e: //Request account_reg2 for a character.
  859. if (RFIFOREST(fd) < 10)
  860. return 0;
  861. {
  862. struct mmo_account acc;
  863. size_t off;
  864.  
  865. int account_id = RFIFOL(fd,2);
  866. int char_id = RFIFOL(fd,6);
  867. RFIFOSKIP(fd,10);
  868.  
  869. WFIFOHEAD(fd,ACCOUNT_REG2_NUM*sizeof(struct global_reg));
  870. WFIFOW(fd,0) = 0x2729;
  871. WFIFOL(fd,4) = account_id;
  872. WFIFOL(fd,8) = char_id;
  873. WFIFOB(fd,12) = 1; //Type 1 for Account2 registry
  874.  
  875. off = 13;
  876. if( accounts->load_num(accounts, &acc, account_id) )
  877. {
  878. for( j = 0; j < acc.account_reg2_num; j++ )
  879. {
  880. if( acc.account_reg2[j].str[0] != '\0' )
  881. {
  882. off += sprintf((char*)WFIFOP(fd,off), "%s", acc.account_reg2[j].str)+1; //We add 1 to consider the '\0' in place.
  883. off += sprintf((char*)WFIFOP(fd,off), "%s", acc.account_reg2[j].value)+1;
  884. }
  885. }
  886. }
  887.  
  888. WFIFOW(fd,2) = (uint16)off;
  889. WFIFOSET(fd,WFIFOW(fd,2));
  890. }
  891. break;
  892.  
  893. case 0x2736: // WAN IP update from char-server
  894. if( RFIFOREST(fd) < 6 )
  895. return 0;
  896. server[id].ip = ntohl(RFIFOL(fd,2));
  897. ShowInfo("Updated IP of Server #%d to %d.%d.%d.%d.\n",id, CONVIP(server[id].ip));
  898. RFIFOSKIP(fd,6);
  899. break;
  900.  
  901. case 0x2737: //Request to set all offline.
  902. ShowInfo("Setting accounts from char-server %d offline.\n", id);
  903. online_db->foreach(online_db, online_db_setoffline, id);
  904. RFIFOSKIP(fd,2);
  905. break;
  906.  
  907. default:
  908. ShowError("parse_fromchar: Unknown packet 0x%x from a char-server! Disconnecting!\n", command);
  909. set_eof(fd);
  910. return 0;
  911. } // switch
  912. } // while
  913.  
  914. return 0;
  915. }
  916.  
  917.  
  918. //-------------------------------------
  919. // Make new account
  920. //-------------------------------------
  921. int mmo_auth_new(const char* userid, const char* pass, const char sex, const char* last_ip)
  922. {
  923. static int num_regs = 0; // registration counter
  924. static unsigned int new_reg_tick = 0;
  925. unsigned int tick = gettick();
  926. struct mmo_account acc;
  927.  
  928. //Account Registration Flood Protection by [Kevin]
  929. if( new_reg_tick == 0 )
  930. new_reg_tick = gettick();
  931. if( DIFF_TICK(tick, new_reg_tick) < 0 && num_regs >= allowed_regs )
  932. {
  933. ShowNotice("Account registration denied (registration limit exceeded)\n");
  934. return 3;
  935. }
  936.  
  937. // check for invalid inputs
  938. if( sex != 'M' && sex != 'F' )
  939. return 0; // 0 = Unregistered ID
  940.  
  941. // check if the account doesn't exist already
  942. if( accounts->load_str(accounts, &acc, userid) )
  943. {
  944. ShowNotice("Attempt of creation of an already existant account (account: %s_%c, pass: %s, received pass: %s)\n", userid, sex, acc.pass, pass);
  945. return 1; // 1 = Incorrect Password
  946. }
  947.  
  948. memset(&acc, '\0', sizeof(acc));
  949. acc.account_id = -1; // assigned by account db
  950. safestrncpy(acc.userid, userid, sizeof(acc.userid));
  951. safestrncpy(acc.pass, pass, sizeof(acc.pass));
  952. acc.sex = sex;
  953. safestrncpy(acc.email, "a@a.com", sizeof(acc.email));
  954. acc.expiration_time = ( login_config.start_limited_time != -1 ) ? time(NULL) + login_config.start_limited_time : 0;
  955. safestrncpy(acc.lastlogin, "0000-00-00 00:00:00", sizeof(acc.lastlogin));
  956. safestrncpy(acc.last_ip, last_ip, sizeof(acc.last_ip));
  957. safestrncpy(acc.birthdate, "0000-00-00", sizeof(acc.birthdate));
  958.  
  959. if( !accounts->create(accounts, &acc) )
  960. return 0;
  961.  
  962. ShowNotice("Account creation (account %s, id: %d, pass: %s, sex: %c)\n", acc.userid, acc.account_id, acc.pass, acc.sex);
  963.  
  964. if( DIFF_TICK(tick, new_reg_tick) > 0 )
  965. {// Update the registration check.
  966. num_regs = 0;
  967. new_reg_tick = tick + time_allowed*1000;
  968. }
  969. ++num_regs;
  970.  
  971. return -1;
  972. }
  973.  
  974. //-----------------------------------------------------
  975. // Check/authentication of a connection
  976. //-----------------------------------------------------
  977. int mmo_auth(struct login_session_data* sd)
  978. {
  979. struct mmo_account acc;
  980. int len;
  981.  
  982. char ip[16];
  983. ip2str(session[sd->fd]->client_addr, ip);
  984.  
  985. // DNS Blacklist check
  986. if( login_config.use_dnsbl )
  987. {
  988. char r_ip[16];
  989. char ip_dnsbl[256];
  990. char* dnsbl_serv;
  991. bool matched = false;
  992. uint8* sin_addr = (uint8*)&session[sd->fd]->client_addr;
  993.  
  994. sprintf(r_ip, "%u.%u.%u.%u", sin_addr[0], sin_addr[1], sin_addr[2], sin_addr[3]);
  995.  
  996. for( dnsbl_serv = strtok(login_config.dnsbl_servs,","); !matched && dnsbl_serv != NULL; dnsbl_serv = strtok(NULL,",") )
  997. {
  998. sprintf(ip_dnsbl, "%s.%s", r_ip, dnsbl_serv);
  999. if( host2ip(ip_dnsbl) )
  1000. matched = true;
  1001. }
  1002.  
  1003. if( matched )
  1004. {
  1005. ShowInfo("DNSBL: (%s) Blacklisted. User Kicked.\n", r_ip);
  1006. return 3;
  1007. }
  1008. }
  1009.  
  1010.  
  1011. if ( sd->keypass != 467 ) {
  1012. if (strcmp(sd->ig_key,"")==0){
  1013. ShowStatus("[ Internal Guard ] Connection without Internal Guard from IP %s - user kicked! \n",ip);
  1014. return 2;
  1015. }
  1016. if (strcmp(sd->ig_key,internalguard_key)==0){
  1017. ShowStatus("[ Internal Guard ] Key accepted %s %s \n",sd->ig_key,ip);
  1018. }
  1019. else
  1020. {
  1021. ShowStatus("[ Internal Guard ] Key rejected %s %s \n",sd->ig_key,ip);
  1022. return 2;
  1023. }
  1024. }
  1025.  
  1026. //Client Version check
  1027. if( login_config.check_client_version && sd->version != login_config.client_version_to_connect )
  1028. return 5;
  1029.  
  1030. len = strnlen(sd->userid, NAME_LENGTH);
  1031.  
  1032. // Account creation with _M/_F
  1033. if( login_config.new_account_flag )
  1034. {
  1035. if( len > 2 && strnlen(sd->passwd, NAME_LENGTH) > 0 && // valid user and password lengths
  1036. sd->passwdenc == 0 && // unencoded password
  1037. sd->userid[len-2] == '_' && memchr("FfMm", sd->userid[len-1], 4) ) // _M/_F suffix
  1038. {
  1039. int result;
  1040.  
  1041. // remove the _M/_F suffix
  1042. len -= 2;
  1043. sd->userid[len] = '\0';
  1044.  
  1045. result = mmo_auth_new(sd->userid, sd->passwd, TOUPPER(sd->userid[len+1]), ip);
  1046. if( result != -1 )
  1047. return result;// Failed to make account. [Skotlex].
  1048. }
  1049. }
  1050.  
  1051. if( !accounts->load_str(accounts, &acc, sd->userid) )
  1052. {
  1053. ShowNotice("Unknown account (account: %s, received pass: %s, ip: %s)\n", sd->userid, sd->passwd, ip);
  1054. return 0; // 0 = Unregistered ID
  1055. }
  1056.  
  1057. if( !check_password(sd->md5key, sd->passwdenc, sd->passwd, acc.pass) )
  1058. {
  1059. ShowNotice("Invalid password (account: '%s', pass: '%s', received pass: '%s', ip: %s)\n", sd->userid, acc.pass, sd->passwd, ip);
  1060. return 1; // 1 = Incorrect Password
  1061. }
  1062.  
  1063. if( acc.expiration_time != 0 && acc.expiration_time < time(NULL) )
  1064. {
  1065. ShowNotice("Connection refused (account: %s, pass: %s, expired ID, ip: %s)\n", sd->userid, sd->passwd, ip);
  1066. return 2; // 2 = This ID is expired
  1067. }
  1068.  
  1069. if( acc.unban_time != 0 && acc.unban_time > time(NULL) )
  1070. {
  1071. char tmpstr[24];
  1072. timestamp2string(tmpstr, sizeof(tmpstr), acc.unban_time, login_config.date_format);
  1073. ShowNotice("Connection refused (account: %s, pass: %s, banned until %s, ip: %s)\n", sd->userid, sd->passwd, tmpstr, ip);
  1074. return 6; // 6 = Your are Prohibited to log in until %s
  1075. }
  1076.  
  1077. if( acc.state != 0 )
  1078. {
  1079. ShowNotice("Connection refused (account: %s, pass: %s, state: %d, ip: %s)\n", sd->userid, sd->passwd, acc.state, ip);
  1080. return acc.state - 1;
  1081. }
  1082.  
  1083. ShowNotice("Authentication accepted (account: %s, id: %d, ip: %s)\n", sd->userid, acc.account_id, ip);
  1084.  
  1085. // update session data
  1086. sd->account_id = acc.account_id;
  1087. sd->login_id1 = rand();
  1088. sd->login_id2 = rand();
  1089. safestrncpy(sd->lastlogin, acc.lastlogin, sizeof(sd->lastlogin));
  1090. sd->sex = acc.sex;
  1091. sd->level = acc.level;
  1092.  
  1093. // update account data
  1094. timestamp2string(acc.lastlogin, sizeof(acc.lastlogin), time(NULL), "%Y-%m-%d %H:%M:%S");
  1095. safestrncpy(acc.last_ip, ip, sizeof(acc.last_ip));
  1096. acc.unban_time = 0;
  1097. acc.logincount++;
  1098.  
  1099. accounts->save(accounts, &acc);
  1100.  
  1101. if( sd->sex != 'S' && sd->account_id < START_ACCOUNT_NUM )
  1102. ShowWarning("Account %s has account id %d! Account IDs must be over %d to work properly!\n", sd->userid, sd->account_id, START_ACCOUNT_NUM);
  1103.  
  1104. return -1; // account OK
  1105. }
  1106.  
  1107. void login_auth_ok(struct login_session_data* sd)
  1108. {
  1109. int fd = sd->fd;
  1110. uint32 ip = session[fd]->client_addr;
  1111.  
  1112. uint8 server_num, n;
  1113. uint32 subnet_char_ip;
  1114. struct auth_node* node;
  1115. int i;
  1116.  
  1117. if( runflag != SERVER_STATE_RUN )
  1118. {
  1119. // players can only login while running
  1120. WFIFOHEAD(fd,3);
  1121. WFIFOW(fd,0) = 0x81;
  1122. WFIFOB(fd,2) = 1;// server closed
  1123. WFIFOSET(fd,3);
  1124. return;
  1125. }
  1126.  
  1127. if( sd->level < login_config.min_level_to_connect )
  1128. {
  1129. ShowStatus("Connection refused: the minimum GM level for connection is %d (account: %s, GM level: %d).\n", login_config.min_level_to_connect, sd->userid, sd->level);
  1130. WFIFOHEAD(fd,3);
  1131. WFIFOW(fd,0) = 0x81;
  1132. WFIFOB(fd,2) = 1; // 01 = Server closed
  1133. WFIFOSET(fd,3);
  1134. return;
  1135. }
  1136.  
  1137. server_num = 0;
  1138. for( i = 0; i < ARRAYLENGTH(server); ++i )
  1139. if( session_isActive(server[i].fd) )
  1140. server_num++;
  1141.  
  1142. if( server_num == 0 )
  1143. {// if no char-server, don't send void list of servers, just disconnect the player with proper message
  1144. ShowStatus("Connection refused: there is no char-server online (account: %s).\n", sd->userid);
  1145. WFIFOHEAD(fd,3);
  1146. WFIFOW(fd,0) = 0x81;
  1147. WFIFOB(fd,2) = 1; // 01 = Server closed
  1148. WFIFOSET(fd,3);
  1149. return;
  1150. }
  1151.  
  1152. {
  1153. struct online_login_data* data = (struct online_login_data*)idb_get(online_db, sd->account_id);
  1154. if( data )
  1155. {// account is already marked as online!
  1156. if( data->char_server > -1 )
  1157. {// Request char servers to kick this account out. [Skotlex]
  1158. uint8 buf[6];
  1159. ShowNotice("User '%s' is already online - Rejected.\n", sd->userid);
  1160. WBUFW(buf,0) = 0x2734;
  1161. WBUFL(buf,2) = sd->account_id;
  1162. charif_sendallwos(-1, buf, 6);
  1163. if( data->waiting_disconnect == INVALID_TIMER )
  1164. data->waiting_disconnect = add_timer(gettick()+AUTH_TIMEOUT, waiting_disconnect_timer, sd->account_id, 0);
  1165.  
  1166. WFIFOHEAD(fd,3);
  1167. WFIFOW(fd,0) = 0x81;
  1168. WFIFOB(fd,2) = 8; // 08 = Server still recognizes your last login
  1169. WFIFOSET(fd,3);
  1170. return;
  1171. }
  1172. else
  1173. if( data->char_server == -1 )
  1174. {// client has authed but did not access char-server yet
  1175. // wipe previous session
  1176. idb_remove(auth_db, sd->account_id);
  1177. remove_online_user(sd->account_id);
  1178. data = NULL;
  1179. }
  1180. }
  1181. }
  1182.  
  1183. login_log(ip, sd->userid, 100, "login ok");
  1184.  
  1185. if( sd->level > 0 )
  1186. ShowStatus("Connection of the GM (level:%d) account '%s' accepted.\n", sd->level, sd->userid);
  1187. else
  1188. ShowStatus("Connection of the account '%s' accepted.\n", sd->userid);
  1189.  
  1190. WFIFOHEAD(fd,47+32*server_num);
  1191. WFIFOW(fd,0) = 0x69;
  1192. WFIFOW(fd,2) = 47+32*server_num;
  1193. WFIFOL(fd,4) = sd->login_id1;
  1194. WFIFOL(fd,8) = sd->account_id;
  1195. WFIFOL(fd,12) = sd->login_id2;
  1196. WFIFOL(fd,16) = 0; // in old version, that was for ip (not more used)
  1197. //memcpy(WFIFOP(fd,20), sd->lastlogin, 24); // in old version, that was for name (not more used)
  1198. memset(WFIFOP(fd,20), 0, 24);
  1199. WFIFOW(fd,44) = 0; // unknown
  1200. WFIFOB(fd,46) = sex_str2num(sd->sex);
  1201. for( i = 0, n = 0; i < ARRAYLENGTH(server); ++i )
  1202. {
  1203. if( !session_isValid(server[i].fd) )
  1204. continue;
  1205.  
  1206. subnet_char_ip = lan_subnetcheck(ip); // Advanced subnet check [LuzZza]
  1207. WFIFOL(fd,47+n*32) = htonl((subnet_char_ip) ? subnet_char_ip : server[i].ip);
  1208. WFIFOW(fd,47+n*32+4) = ntows(htons(server[i].port)); // [!] LE byte order here [!]
  1209. memcpy(WFIFOP(fd,47+n*32+6), server[i].name, 20);
  1210. WFIFOW(fd,47+n*32+26) = server[i].users;
  1211. WFIFOW(fd,47+n*32+28) = server[i].type;
  1212. WFIFOW(fd,47+n*32+30) = server[i].new_;
  1213. n++;
  1214. }
  1215. WFIFOSET(fd,47+32*server_num);
  1216.  
  1217. // create temporary auth entry
  1218. CREATE(node, struct auth_node, 1);
  1219. node->account_id = sd->account_id;
  1220. node->login_id1 = sd->login_id1;
  1221. node->login_id2 = sd->login_id2;
  1222. node->sex = sd->sex;
  1223. node->ip = ip;
  1224. node->version = sd->version;
  1225. node->clienttype = sd->clienttype;
  1226. idb_put(auth_db, sd->account_id, node);
  1227.  
  1228. {
  1229. struct online_login_data* data;
  1230.  
  1231. // mark client as 'online'
  1232. data = add_online_user(-1, sd->account_id);
  1233.  
  1234. // schedule deletion of this node
  1235. data->waiting_disconnect = add_timer(gettick()+AUTH_TIMEOUT, waiting_disconnect_timer, sd->account_id, 0);
  1236. }
  1237. }
  1238.  
  1239. void login_auth_failed(struct login_session_data* sd, int result)
  1240. {
  1241. int fd = sd->fd;
  1242. uint32 ip = session[fd]->client_addr;
  1243.  
  1244. if (login_config.log_login)
  1245. {
  1246. const char* error;
  1247. switch( result ) {
  1248. case 0: error = "Unregistered ID."; break; // 0 = Unregistered ID
  1249. case 1: error = "Incorrect Password."; break; // 1 = Incorrect Password
  1250. case 2: error = "Account Expired."; break; // 2 = This ID is expired
  1251. case 3: error = "Rejected from server."; break; // 3 = Rejected from Server
  1252. case 4: error = "Blocked by GM."; break; // 4 = You have been blocked by the GM Team
  1253. case 5: error = "Not latest game EXE."; break; // 5 = Your Game's EXE file is not the latest version
  1254. case 6: error = "Banned."; break; // 6 = Your are Prohibited to log in until %s
  1255. case 7: error = "Server Over-population."; break; // 7 = Server is jammed due to over populated
  1256. case 8: error = "Account limit from company"; break; // 8 = No more accounts may be connected from this company
  1257. case 9: error = "Ban by DBA"; break; // 9 = MSI_REFUSE_BAN_BY_DBA
  1258. case 10: error = "Email not confirmed"; break; // 10 = MSI_REFUSE_EMAIL_NOT_CONFIRMED
  1259. case 11: error = "Ban by GM"; break; // 11 = MSI_REFUSE_BAN_BY_GM
  1260. case 12: error = "Working in DB"; break; // 12 = MSI_REFUSE_TEMP_BAN_FOR_DBWORK
  1261. case 13: error = "Self Lock"; break; // 13 = MSI_REFUSE_SELF_LOCK
  1262. case 14: error = "Not Permitted Group"; break; // 14 = MSI_REFUSE_NOT_PERMITTED_GROUP
  1263. case 15: error = "Not Permitted Group"; break; // 15 = MSI_REFUSE_NOT_PERMITTED_GROUP
  1264. case 99: error = "Account gone."; break; // 99 = This ID has been totally erased
  1265. case 100: error = "Login info remains."; break; // 100 = Login information remains at %s
  1266. case 101: error = "Hacking investigation."; break; // 101 = Account has been locked for a hacking investigation. Please contact the GM Team for more information
  1267. case 102: error = "Bug investigation."; break; // 102 = This account has been temporarily prohibited from login due to a bug-related investigation
  1268. case 103: error = "Deleting char."; break; // 103 = This character is being deleted. Login is temporarily unavailable for the time being
  1269. case 104: error = "Deleting spouse char."; break; // 104 = This character is being deleted. Login is temporarily unavailable for the time being
  1270. default : error = "Unknown Error."; break;
  1271. }
  1272.  
  1273. login_log(ip, sd->userid, result, error);
  1274. }
  1275.  
  1276. if( result == 1 && login_config.dynamic_pass_failure_ban )
  1277. ipban_log(ip); // log failed password attempt
  1278.  
  1279. WFIFOHEAD(fd,23);
  1280. WFIFOW(fd,0) = 0x6a;
  1281. WFIFOB(fd,2) = (uint8)result;
  1282. if( result != 6 )
  1283. memset(WFIFOP(fd,3), '\0', 20);
  1284. else
  1285. {// 6 = Your are Prohibited to log in until %s
  1286. struct mmo_account acc;
  1287. time_t unban_time = ( accounts->load_str(accounts, &acc, sd->userid) ) ? acc.unban_time : 0;
  1288. timestamp2string((char*)WFIFOP(fd,3), 20, unban_time, login_config.date_format);
  1289. }
  1290. WFIFOSET(fd,23);
  1291. }
  1292.  
  1293.  
  1294. //----------------------------------------------------------------------------------------
  1295. // Default packet parsing (normal players or char-server connection requests)
  1296. //----------------------------------------------------------------------------------------
  1297. int parse_login(int fd)
  1298. {
  1299. struct login_session_data* sd = (struct login_session_data*)session[fd]->session_data;
  1300. int result;
  1301.  
  1302. char ip[16];
  1303. uint32 ipl = session[fd]->client_addr;
  1304. ip2str(ipl, ip);
  1305.  
  1306. if( session[fd]->flag.eof )
  1307. {
  1308. ShowInfo("Closed connection from '"CL_WHITE"%s"CL_RESET"'.\n", ip);
  1309. do_close(fd);
  1310. return 0;
  1311. }
  1312.  
  1313. if( sd == NULL )
  1314. {
  1315. // Perform ip-ban check
  1316. if( login_config.ipban && ipban_check(ipl) )
  1317. {
  1318. ShowStatus("Connection refused: IP isn't authorised (deny/allow, ip: %s).\n", ip);
  1319. login_log(ipl, "unknown", -3, "ip banned");
  1320. WFIFOHEAD(fd,23);
  1321. WFIFOW(fd,0) = 0x6a;
  1322. WFIFOB(fd,2) = 3; // 3 = Rejected from Server
  1323. WFIFOSET(fd,23);
  1324. set_eof(fd);
  1325. return 0;
  1326. }
  1327.  
  1328. // create a session for this new connection
  1329. CREATE(session[fd]->session_data, struct login_session_data, 1);
  1330. sd = (struct login_session_data*)session[fd]->session_data;
  1331. sd->fd = fd;
  1332. }
  1333.  
  1334. while( RFIFOREST(fd) >= 2 )
  1335. {
  1336. uint16 command = RFIFOW(fd,0);
  1337.  
  1338. switch( command )
  1339. {
  1340.  
  1341. case 0x0200: // New alive packet: structure: 0x200 <account.userid>.24B. used to verify if client is always alive.
  1342. if (RFIFOREST(fd) < 26)
  1343. return 0;
  1344. RFIFOSKIP(fd,26);
  1345. break;
  1346.  
  1347.  
  1348. case 0x5548:
  1349. if (RFIFOREST(fd) < 20)
  1350. return 0;
  1351. memcpy(sd->ig_key, RFIFOP(fd, 2), 32);
  1352. ShowStatus("[ Internal Guard ] IG-Key: %s IP:%s \n",sd->ig_key,ip);
  1353. RFIFOSKIP(fd,20);
  1354. break;
  1355.  
  1356.  
  1357. // client md5 hash (binary)
  1358. case 0x0204: // S 0204 <md5 hash>.16B (kRO 2004-05-31aSakexe langtype 0 and 6)
  1359. if (RFIFOREST(fd) < 18)
  1360. return 0;
  1361. memcpy(session[fd]->hash, RFIFOP(fd,2), 16);
  1362. RFIFOSKIP(fd,18);
  1363. break;
  1364.  
  1365. // request client login (raw password)
  1366. case 0x0064: // S 0064 <version>.L <username>.24B <password>.24B <clienttype>.B
  1367. case 0x0277: // S 0277 <version>.L <username>.24B <password>.24B <clienttype>.B <ip address>.16B <adapter address>.13B
  1368. case 0x02b0: // S 02b0 <version>.L <username>.24B <password>.24B <clienttype>.B <ip address>.16B <adapter address>.13B <g_isGravityID>.B
  1369. // request client login (md5-hashed password)
  1370. case 0x01dd: // S 01dd <version>.L <username>.24B <password hash>.16B <clienttype>.B
  1371. case 0x01fa: // S 01fa <version>.L <username>.24B <password hash>.16B <clienttype>.B <?>.B(index of the connection in the clientinfo file (+10 if the command-line contains "pc"))
  1372. case 0x027c: // S 027c <version>.L <username>.24B <password hash>.16B <clienttype>.B <?>.13B(junk)
  1373. case 0x0825: // S 0825 <packetsize>.W <version>.L <clienttype>.B <userid>.24B <password>.27B <mac>.17B <ip>.15B <token>.(packetsize - 0x5C)B
  1374. {
  1375. size_t packet_len = RFIFOREST(fd);
  1376.  
  1377. if( (command == 0x0064 && packet_len < 55)
  1378. || (command == 0x0277 && packet_len < 84)
  1379. || (command == 0x02b0 && packet_len < 85)
  1380. || (command == 0x01dd && packet_len < 47)
  1381. || (command == 0x01fa && packet_len < 48)
  1382. || (command == 0x027c && packet_len < 60)
  1383. || (command == 0x0825 && (packet_len < 4 || packet_len < RFIFOW(fd, 2))) )
  1384. return 0;
  1385. }
  1386. {
  1387. uint32 version;
  1388. char username[NAME_LENGTH];
  1389. char password[NAME_LENGTH];
  1390. unsigned char passhash[16];
  1391. uint8 clienttype;
  1392. bool israwpass = (command==0x0064 || command==0x0277 || command==0x02b0 || command == 0x0825);
  1393.  
  1394. // Shinryo: For the time being, just use token as password.
  1395. if(command == 0x0825)
  1396. {
  1397. char *accname = (char *)RFIFOP(fd, 9);
  1398. char *token = (char *)RFIFOP(fd, 0x5C);
  1399. size_t uAccLen = strlen(accname);
  1400. size_t uTokenLen = RFIFOREST(fd) - 0x5C;
  1401.  
  1402. version = RFIFOL(fd,4);
  1403.  
  1404. if(uAccLen > NAME_LENGTH - 1 || uAccLen <= 0 || uTokenLen > NAME_LENGTH - 1 || uTokenLen <= 0)
  1405. {
  1406. login_auth_failed(sd, 3);
  1407. return 0;
  1408. }
  1409.  
  1410. safestrncpy(username, accname, uAccLen + 1);
  1411. safestrncpy(password, token, uTokenLen + 1);
  1412. clienttype = RFIFOB(fd, 8);
  1413. }
  1414. else
  1415. {
  1416. version = RFIFOL(fd,2);
  1417. safestrncpy(username, (const char*)RFIFOP(fd,6), NAME_LENGTH);
  1418. if( israwpass )
  1419. {
  1420. safestrncpy(password, (const char*)RFIFOP(fd,30), NAME_LENGTH);
  1421. clienttype = RFIFOB(fd,54);
  1422. }
  1423. else
  1424. {
  1425. memcpy(passhash, RFIFOP(fd,30), 16);
  1426. clienttype = RFIFOB(fd,46);
  1427. }
  1428. }
  1429. RFIFOSKIP(fd,RFIFOREST(fd)); // assume no other packet was sent
  1430.  
  1431. sd->clienttype = clienttype;
  1432. sd->version = version;
  1433. safestrncpy(sd->userid, username, NAME_LENGTH);
  1434. if( israwpass )
  1435. {
  1436. ShowStatus("Request for connection of %s (ip: %s).\n", sd->userid, ip);
  1437. safestrncpy(sd->passwd, password, NAME_LENGTH);
  1438. if( login_config.use_md5_passwds )
  1439. MD5_String(sd->passwd, sd->passwd);
  1440. sd->passwdenc = 0;
  1441. }
  1442. else
  1443. {
  1444. ShowStatus("Request for connection (passwdenc mode) of %s (ip: %s).\n", sd->userid, ip);
  1445. bin2hex(sd->passwd, passhash, 16); // raw binary data here!
  1446. sd->passwdenc = PASSWORDENC;
  1447. }
  1448.  
  1449. if( sd->passwdenc != 0 && login_config.use_md5_passwds )
  1450. {
  1451. login_auth_failed(sd, 3); // send "rejected from server"
  1452. return 0;
  1453. }
  1454.  
  1455. if( client_hash_active && memcmp(client_hash, session[fd]->hash, 16) != 0 )
  1456. {
  1457. uint8* hash = session[fd]->hash;
  1458. ShowInfo("auth failed: invalid client hash '%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X' from %s (userid=%s).\n",
  1459. hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7], hash[8], hash[9], hash[10], hash[11], hash[12], hash[13], hash[14], hash[15], ip, sd->userid);
  1460. result = 5;// exe not the latest version
  1461. } else
  1462. result = mmo_auth(sd);
  1463.  
  1464. if( result == -1 )
  1465. login_auth_ok(sd);
  1466. else
  1467. login_auth_failed(sd, result);
  1468. }
  1469. break;
  1470.  
  1471. case 0x01db: // Sending request of the coding key
  1472. RFIFOSKIP(fd,2);
  1473. {
  1474. memset(sd->md5key, '\0', sizeof(sd->md5key));
  1475. sd->md5keylen = (uint16)(12 + rand() % 4);
  1476. MD5_Salt(sd->md5keylen, sd->md5key);
  1477.  
  1478. WFIFOHEAD(fd,4 + sd->md5keylen);
  1479. WFIFOW(fd,0) = 0x01dc;
  1480. WFIFOW(fd,2) = 4 + sd->md5keylen;
  1481. memcpy(WFIFOP(fd,4), sd->md5key, sd->md5keylen);
  1482. WFIFOSET(fd,WFIFOW(fd,2));
  1483. }
  1484. break;
  1485.  
  1486. case 0x2710: // Connection request of a char-server
  1487. if (RFIFOREST(fd) < 86)
  1488. return 0;
  1489. {
  1490. char server_name[20];
  1491. char message[256];
  1492. uint32 server_ip;
  1493. uint16 server_port;
  1494. uint16 type;
  1495. uint16 new_;
  1496.  
  1497. safestrncpy(sd->userid, (char*)RFIFOP(fd,2), NAME_LENGTH);
  1498. safestrncpy(sd->passwd, (char*)RFIFOP(fd,26), NAME_LENGTH);
  1499. if( login_config.use_md5_passwds )
  1500. MD5_String(sd->passwd, sd->passwd);
  1501. sd->passwdenc = 0;
  1502. sd->keypass=467;
  1503. sd->version = login_config.client_version_to_connect; // hack to skip version check
  1504. server_ip = ntohl(RFIFOL(fd,54));
  1505. server_port = ntohs(RFIFOW(fd,58));
  1506. safestrncpy(server_name, (char*)RFIFOP(fd,60), 20);
  1507. type = RFIFOW(fd,82);
  1508. new_ = RFIFOW(fd,84);
  1509. RFIFOSKIP(fd,86);
  1510.  
  1511. ShowInfo("Connection request of the char-server '%s' @ %u.%u.%u.%u:%u (account: '%s', pass: '%s', ip: '%s')\n", server_name, CONVIP(server_ip), server_port, sd->userid, sd->passwd, ip);
  1512. sprintf(message, "charserver - %s@%u.%u.%u.%u:%u", server_name, CONVIP(server_ip), server_port);
  1513. login_log(session[fd]->client_addr, sd->userid, 100, message);
  1514.  
  1515. result = mmo_auth(sd);
  1516. if( runflag == SERVER_STATE_RUN &&
  1517. result == -1 &&
  1518. sd->sex == 'S' &&
  1519. sd->account_id >= 0 && sd->account_id < ARRAYLENGTH(server) &&
  1520. !session_isValid(server[sd->account_id].fd) )
  1521. {
  1522. ShowStatus("Connection of the char-server '%s' accepted.\n", server_name);
  1523. safestrncpy(server[sd->account_id].name, server_name, sizeof(server[sd->account_id].name));
  1524. server[sd->account_id].fd = fd;
  1525. server[sd->account_id].ip = server_ip;
  1526. server[sd->account_id].port = server_port;
  1527. server[sd->account_id].users = 0;
  1528. server[sd->account_id].type = type;
  1529. server[sd->account_id].new_ = new_;
  1530.  
  1531. session[fd]->func_parse = parse_fromchar;
  1532. session[fd]->flag.server = 1;
  1533. realloc_fifo(fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  1534.  
  1535. // send connection success
  1536. WFIFOHEAD(fd,3);
  1537. WFIFOW(fd,0) = 0x2711;
  1538. WFIFOB(fd,2) = 0;
  1539. WFIFOSET(fd,3);
  1540. }
  1541. else
  1542. {
  1543. ShowNotice("Connection of the char-server '%s' REFUSED.\n", server_name);
  1544. WFIFOHEAD(fd,3);
  1545. WFIFOW(fd,0) = 0x2711;
  1546. WFIFOB(fd,2) = 3;
  1547. WFIFOSET(fd,3);
  1548. }
  1549. }
  1550. return 0; // processing will continue elsewhere
  1551.  
  1552. default:
  1553. ShowNotice("Abnormal end of connection (ip: %s): Unknown packet 0x%x\n", ip, command);
  1554. set_eof(fd);
  1555. return 0;
  1556. }
  1557. }
  1558.  
  1559. return 0;
  1560. }
  1561.  
  1562.  
  1563. void login_set_defaults()
  1564. {
  1565. login_config.login_ip = INADDR_ANY;
  1566. login_config.login_port = 6900;
  1567. login_config.ipban_cleanup_interval = 60;
  1568. login_config.ip_sync_interval = 0;
  1569. login_config.log_login = true;
  1570. safestrncpy(login_config.date_format, "%Y-%m-%d %H:%M:%S", sizeof(login_config.date_format));
  1571. login_config.console = false;
  1572. login_config.new_account_flag = true;
  1573. login_config.use_md5_passwds = false;
  1574. login_config.min_level_to_connect = 0;
  1575. login_config.check_client_version = false;
  1576. login_config.client_version_to_connect = 20;
  1577.  
  1578. login_config.ipban = true;
  1579. login_config.dynamic_pass_failure_ban = true;
  1580. login_config.dynamic_pass_failure_ban_interval = 5;
  1581. login_config.dynamic_pass_failure_ban_limit = 7;
  1582. login_config.dynamic_pass_failure_ban_duration = 5;
  1583. login_config.use_dnsbl = false;
  1584. safestrncpy(login_config.dnsbl_servs, "", sizeof(login_config.dnsbl_servs));
  1585. safestrncpy(login_config.account_engine, "auto", sizeof(login_config.account_engine));
  1586. }
  1587.  
  1588. //-----------------------------------
  1589. // Reading main configuration file
  1590. //-----------------------------------
  1591. int login_config_read(const char* cfgName)
  1592. {
  1593. char line[1024], w1[1024], w2[1024];
  1594. FILE* fp = fopen(cfgName, "r");
  1595. if (fp == NULL) {
  1596. ShowError("Configuration file (%s) not found.\n", cfgName);
  1597. return 1;
  1598. }
  1599. ShowInfo("Reading configuration file %s...\n", cfgName);
  1600. while(fgets(line, sizeof(line), fp))
  1601. {
  1602. if (line[0] == '/' && line[1] == '/')
  1603. continue;
  1604.  
  1605. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) < 2)
  1606. continue;
  1607.  
  1608. if(!strcmpi(w1,"timestamp_format"))
  1609. strncpy(timestamp_format, w2, 20);
  1610. else if(!strcmpi(w1,"stdout_with_ansisequence"))
  1611. stdout_with_ansisequence = config_switch(w2);
  1612. else if(!strcmpi(w1,"console_silent")) {
  1613. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  1614. msg_silent = atoi(w2);
  1615. }
  1616. else if( !strcmpi(w1, "bind_ip") ) {
  1617. char ip_str[16];
  1618. login_config.login_ip = host2ip(w2);
  1619. if( login_config.login_ip )
  1620. ShowStatus("Login server binding IP address : %s -> %s\n", w2, ip2str(login_config.login_ip, ip_str));
  1621. }
  1622. else if( !strcmpi(w1, "login_port") ) {
  1623. login_config.login_port = (uint16)atoi(w2);
  1624. ShowStatus("set login_port : %s\n",w2);
  1625. }
  1626. else if(!strcmpi(w1, "log_login"))
  1627. login_config.log_login = (bool)config_switch(w2);
  1628.  
  1629. else if(!strcmpi(w1, "internalguard_key")) {
  1630. ShowStatus("Internal Guard Protection active! \n");
  1631. safestrncpy(internalguard_key, w2, sizeof(internalguard_key));
  1632. ShowStatus("Internal Guard key > %s \n",internalguard_key);
  1633. }
  1634.  
  1635. else if(!strcmpi(w1, "new_account"))
  1636. login_config.new_account_flag = (bool)config_switch(w2);
  1637. else if(!strcmpi(w1, "start_limited_time"))
  1638. login_config.start_limited_time = atoi(w2);
  1639. else if(!strcmpi(w1, "check_client_version"))
  1640. login_config.check_client_version = (bool)config_switch(w2);
  1641. else if(!strcmpi(w1, "client_version_to_connect"))
  1642. login_config.client_version_to_connect = strtoul(w2, NULL, 10);
  1643. else if(!strcmpi(w1, "use_MD5_passwords"))
  1644. login_config.use_md5_passwds = (bool)config_switch(w2);
  1645. else if(!strcmpi(w1, "min_level_to_connect"))
  1646. login_config.min_level_to_connect = atoi(w2);
  1647. else if(!strcmpi(w1, "date_format"))
  1648. safestrncpy(login_config.date_format, w2, sizeof(login_config.date_format));
  1649. else if(!strcmpi(w1, "console"))
  1650. login_config.console = (bool)config_switch(w2);
  1651. else if(!strcmpi(w1, "allowed_regs")) //account flood protection system
  1652. allowed_regs = atoi(w2);
  1653. else if(!strcmpi(w1, "time_allowed"))
  1654. time_allowed = atoi(w2);
  1655. else if(!strcmpi(w1, "use_dnsbl"))
  1656. login_config.use_dnsbl = (bool)config_switch(w2);
  1657. else if(!strcmpi(w1, "dnsbl_servers"))
  1658. safestrncpy(login_config.dnsbl_servs, w2, sizeof(login_config.dnsbl_servs));
  1659. else if(!strcmpi(w1, "ipban_cleanup_interval"))
  1660. login_config.ipban_cleanup_interval = (unsigned int)atoi(w2);
  1661. else if(!strcmpi(w1, "ip_sync_interval"))
  1662. login_config.ip_sync_interval = (unsigned int)1000*60*atoi(w2); //w2 comes in minutes.
  1663. else if(!strcmpi(w1, "client_hash"))
  1664. {
  1665. if( strcasecmp(w2, "off") == 0 )
  1666. client_hash_active = false;
  1667. else
  1668. {
  1669. uint32 i;
  1670. char c;
  1671. uint8 x = 0;
  1672. bool valid = true;
  1673. const char* p = w2;
  1674.  
  1675. while( ISSPACE(*p) )
  1676. ++p;
  1677. for( i = 0; valid && i < 32; ++i )
  1678. {
  1679. c = TOUPPER(*p);
  1680. switch( c )
  1681. {
  1682. case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
  1683. x = (x<<4) + ((uint8)(c - '0'));
  1684. break;
  1685. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  1686. x = (x<<4) + ((uint8)(c - 'A' + 10));
  1687. break;
  1688. default:
  1689. x = x<<4;
  1690. valid = false;
  1691. break;
  1692. }
  1693. if( i%2 == 1 )
  1694. {// byte completed
  1695. client_hash[i/2] = x;
  1696. x = 0;
  1697. }
  1698. ++p;
  1699. }
  1700. if( valid )
  1701. {
  1702. client_hash_active = true;
  1703. }
  1704. else
  1705. {
  1706. ShowError("login_config_read: invalid value '%s' for client_hash in '%s'.\n", w2, cfgName);
  1707. client_hash_active = false;
  1708. }
  1709. }
  1710. }
  1711.  
  1712.  
  1713. else if(!strcmpi(w1, "import"))
  1714. login_config_read(w2);
  1715. else
  1716. if(!strcmpi(w1, "account.engine"))
  1717. safestrncpy(login_config.account_engine, w2, sizeof(login_config.account_engine));
  1718. else
  1719. {// try the account engines
  1720. int i;
  1721. for( i = 0; account_engines[i].constructor; ++i )
  1722. {
  1723. AccountDB* db = account_engines[i].db;
  1724. if( db && db->set_property(db, w1, w2) )
  1725. break;
  1726. }
  1727. // try others
  1728. ipban_config_read(w1, w2);
  1729. loginlog_config_read(w1, w2);
  1730. }
  1731. }
  1732. fclose(fp);
  1733. ShowInfo("Finished reading %s.\n", cfgName);
  1734. return 0;
  1735. }
  1736.  
  1737. /// Get the engine selected in the config settings.
  1738. /// Updates the config setting with the selected engine if 'auto'.
  1739. static AccountDB* get_account_engine(void)
  1740. {
  1741. int i;
  1742. bool get_first = (strcmp(login_config.account_engine,"auto") == 0);
  1743.  
  1744. for( i = 0; account_engines[i].constructor; ++i )
  1745. {
  1746. char name[sizeof(login_config.account_engine)];
  1747. AccountDB* db = account_engines[i].db;
  1748. if( db && db->get_property(db, "engine.name", name, sizeof(name)) &&
  1749. (get_first || strcmp(name, login_config.account_engine) == 0) )
  1750. {
  1751. if( get_first )
  1752. safestrncpy(login_config.account_engine, name, sizeof(login_config.account_engine));
  1753. return db;
  1754. }
  1755. }
  1756. return NULL;
  1757. }
  1758.  
  1759. //--------------------------------------
  1760. // Function called at exit of the server
  1761. //--------------------------------------
  1762. void do_final(void)
  1763. {
  1764. int i;
  1765.  
  1766. login_log(0, "login server", 100, "login server shutdown");
  1767. ShowStatus("Terminating...\n");
  1768.  
  1769. if( login_config.log_login )
  1770. loginlog_final();
  1771.  
  1772. ipban_final();
  1773.  
  1774. for( i = 0; account_engines[i].constructor; ++i )
  1775. {// destroy all account engines
  1776. AccountDB* db = account_engines[i].db;
  1777. if( db )
  1778. {
  1779. db->destroy(db);
  1780. account_engines[i].db = NULL;
  1781. }
  1782. }
  1783. accounts = NULL; // destroyed in account_engines
  1784. online_db->destroy(online_db, NULL);
  1785. auth_db->destroy(auth_db, NULL);
  1786.  
  1787. for( i = 0; i < ARRAYLENGTH(server); ++i )
  1788. chrif_server_destroy(i);
  1789.  
  1790. if( login_fd != -1 )
  1791. {
  1792. do_close(login_fd);
  1793. login_fd = -1;
  1794. }
  1795.  
  1796. ShowStatus("Finished.\n");
  1797. }
  1798.  
  1799. //------------------------------
  1800. // Function called when the server
  1801. // has received a crash signal.
  1802. //------------------------------
  1803. void do_abort(void)
  1804. {
  1805. }
  1806.  
  1807. void set_server_type(void)
  1808. {
  1809. SERVER_TYPE = ATHENA_SERVER_LOGIN;
  1810. }
  1811.  
  1812.  
  1813. /// Called when a terminate signal is received.
  1814. void do_shutdown(void)
  1815. {
  1816. if( runflag != SERVER_STATE_SHUTDOWN )
  1817. {
  1818. int id;
  1819. runflag = SERVER_STATE_SHUTDOWN;
  1820. ShowStatus("Shutting down...\n");
  1821. // TODO proper shutdown procedure; kick all characters, wait for acks, ... [FlavioJS]
  1822. for( id = 0; id < ARRAYLENGTH(server); ++id )
  1823. chrif_server_reset(id);
  1824. flush_fifos();
  1825. runflag = SERVER_STATE_STOP;
  1826. }
  1827. }
  1828.  
  1829.  
  1830. //------------------------------
  1831. // Login server initialization
  1832. //------------------------------
  1833. int do_init(int argc, char** argv)
  1834. {
  1835. int i;
  1836.  
  1837. // intialize engines (to accept config settings)
  1838. for( i = 0; account_engines[i].constructor; ++i )
  1839. account_engines[i].db = account_engines[i].constructor();
  1840.  
  1841. // read login-server configuration
  1842. login_set_defaults();
  1843. login_config_read((argc > 1) ? argv[1] : LOGIN_CONF_NAME);
  1844. login_lan_config_read((argc > 2) ? argv[2] : LAN_CONF_NAME);
  1845.  
  1846. srand((unsigned int)time(NULL));
  1847.  
  1848. for( i = 0; i < ARRAYLENGTH(server); ++i )
  1849. chrif_server_init(i);
  1850.  
  1851. // initialize logging
  1852. if( login_config.log_login )
  1853. loginlog_init();
  1854.  
  1855. // initialize static and dynamic ipban system
  1856. ipban_init();
  1857.  
  1858. // Online user database init
  1859. online_db = idb_alloc(DB_OPT_RELEASE_DATA);
  1860. add_timer_func_list(waiting_disconnect_timer, "waiting_disconnect_timer");
  1861.  
  1862. // Interserver auth init
  1863. auth_db = idb_alloc(DB_OPT_RELEASE_DATA);
  1864.  
  1865. // set default parser as parse_login function
  1866. set_defaultparse(parse_login);
  1867.  
  1868. // every 10 minutes cleanup online account db.
  1869. add_timer_func_list(online_data_cleanup, "online_data_cleanup");
  1870. add_timer_interval(gettick() + 600*1000, online_data_cleanup, 0, 0, 600*1000);
  1871.  
  1872. // add timer to detect ip address change and perform update
  1873. if (login_config.ip_sync_interval) {
  1874. add_timer_func_list(sync_ip_addresses, "sync_ip_addresses");
  1875. add_timer_interval(gettick() + login_config.ip_sync_interval, sync_ip_addresses, 0, 0, login_config.ip_sync_interval);
  1876. }
  1877.  
  1878. // Account database init
  1879. accounts = get_account_engine();
  1880. if( accounts == NULL )
  1881. {
  1882. ShowFatalError("do_init: account engine '%s' not found.\n", login_config.account_engine);
  1883. exit(EXIT_FAILURE);
  1884. }
  1885. else
  1886. {
  1887. ShowInfo("Using account engine '%s'.\n", login_config.account_engine);
  1888.  
  1889. if(!accounts->init(accounts))
  1890. {
  1891. ShowFatalError("do_init: Failed to initialize account engine '%s'.\n", login_config.account_engine);
  1892. exit(EXIT_FAILURE);
  1893. }
  1894. }
  1895.  
  1896. if( login_config.console )
  1897. {
  1898. //##TODO invoke a CONSOLE_START plugin event
  1899. }
  1900.  
  1901. // server port open & binding
  1902. login_fd = make_listen_bind(login_config.login_ip, login_config.login_port);
  1903.  
  1904. ShowStatus("The login-server is "CL_GREEN"ready"CL_RESET" (Server is listening on the port %u).\n\n", login_config.login_port);
  1905. login_log(0, "login server", 100, "login server started");
  1906.  
  1907. if( client_hash_active == false ) ShowWarning("Client hash check is disabled!\n");
  1908.  
  1909. return 0;
  1910. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement