Advertisement
Guest User

Untitled

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