login.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/mmo.h"
  5. #include "../common/core.h"
  6. #include "../common/socket.h"
  7. #include "../common/db.h"
  8. #include "../common/timer.h"
  9. #include "../common/malloc.h"
  10. #include "../common/strlib.h"
  11. #include "../common/showmsg.h"
  12. #include "../common/version.h"
  13. #include "../common/md5calc.h"
  14. #include "../common/sql.h"
  15. #include "login.h"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/stat.h> // for stat/lstat/fstat
  20. struct Login_Config {
  21. uint32 login_ip; // the address to bind to
  22. uint16 login_port; // the port to bind to
  23. unsigned int ip_sync_interval; // interval (in minutes) to execute a DNS/IP update (for dynamic IPs)
  24. bool log_login; // whether to log login server actions or not
  25. char date_format[32]; // date format used in messages
  26. bool console; // console input system enabled?
  27. bool new_account_flag; // autoregistration via _M/_F ?
  28. bool case_sensitive; // are logins case sensitive ?
  29. bool use_md5_passwds; // work with password hashes instead of plaintext passwords?
  30. bool login_gm_read; // should the login server handle info about gm accounts?
  31. uint8 min_level_to_connect; // minimum level of player/GM (0: player, 1-99: GM) to connect
  32. bool online_check; // reject incoming players that are already registered as online ?
  33. bool check_client_version; // check the clientversion set in the clientinfo ?
  34. unsigned int client_version_to_connect; // the client version needed to connect (if checking is enabled)
  35. bool ipban; // perform IP blocking (via contents of `ipbanlist`) ?
  36. bool dynamic_pass_failure_ban; // automatic IP blocking due to failed login attemps ?
  37. unsigned int dynamic_pass_failure_ban_interval; // how far to scan the loginlog for password failures
  38. unsigned int dynamic_pass_failure_ban_limit; // number of failures needed to trigger the ipban
  39. unsigned int dynamic_pass_failure_ban_duration; // duration of the ipban
  40. bool use_dnsbl; // dns blacklist blocking ?
  41. char dnsbl_servs[1024]; // comma-separated list of dnsbl servers
  42. } login_config;
  43. int login_fd; // login server socket
  44. #define MAX_SERVERS 30
  45. int server_fd[MAX_SERVERS]; // char server sockets
  46. struct mmo_char_server server[MAX_SERVERS]; // char server data
  47. // Advanced subnet check [LuzZza]
  48. struct s_subnet {
  49. uint32 mask;
  50. uint32 char_ip;
  51. uint32 map_ip;
  52. } subnet[16];
  53. int subnet_count = 0;
  54. struct gm_account* gm_account_db = NULL;
  55. unsigned int GM_num = 0; // number of gm accounts
  56. //Account registration flood protection [Kevin]
  57. int allowed_regs = 1;
  58. int time_allowed = 10; //in seconds
  59. unsigned int new_reg_tick = 0;
  60. int num_regs = 0;
  61. Sql* sql_handle;
  62. // database parameters
  63. uint16 login_server_port = 3306;
  64. char login_server_ip[32] = "127.0.0.1";
  65. char login_server_id[32] = "ragnarok";
  66. char login_server_pw[32] = "ragnarok";
  67. char login_server_db[32] = "ragnarok";
  68. char default_codepage[32] = "";
  69. char login_db[256] = "login";
  70. char loginlog_db[256] = "loginlog";
  71. char reg_db[256] = "global_reg_value";
  72. // added to help out custom login tables, without having to recompile
  73. // source so options are kept in the login_athena.conf or the inter_athena.conf
  74. char login_db_account_id[256] = "account_id";
  75. char login_db_userid[256] = "userid";
  76. char login_db_user_pass[256] = "user_pass";
  77. char login_db_level[256] = "level";
  78. //-----------------------------------------------------
  79. struct login_session_data {
  80. uint16 md5keylen;
  81. char md5key[20];
  82. };
  83. #define AUTH_FIFO_SIZE 256
  84. struct {
  85. int account_id, login_id1, login_id2;
  86. uint32 ip;
  87. char sex;
  88. bool delflag;
  89. } auth_fifo[AUTH_FIFO_SIZE];
  90. int auth_fifo_pos = 0;
  91. struct online_login_data {
  92. int account_id;
  93. int waiting_disconnect;
  94. short char_server;
  95. };
  96. //-----------------------------------------------------
  97. struct dbt* online_db;
  98. static void* create_online_user(DBKey key, va_list args)
  99. {
  100. struct online_login_data* p;
  101. CREATE(p, struct online_login_data, 1);
  102. p->account_id = key.i;
  103. p->char_server = -1;
  104. p->waiting_disconnect = -1;
  105. return p;
  106. }
  107. int charif_sendallwos(int sfd, uint8* buf, size_t len);
  108. static int waiting_disconnect_timer(int tid, unsigned int tick, int id, int data);
  109. //-----------------------------------------------------
  110. // Online User Database [Wizputer]
  111. //-----------------------------------------------------
  112. void add_online_user(int char_server, int account_id)
  113. {
  114. struct online_login_data* p;
  115. if( !login_config.online_check )
  116. return;
  117. p = idb_ensure(online_db, account_id, create_online_user);
  118. p->char_server = char_server;
  119. if( p->waiting_disconnect != -1 )
  120. {
  121. delete_timer(p->waiting_disconnect, waiting_disconnect_timer);
  122. p->waiting_disconnect = -1;
  123. }
  124. }
  125. void remove_online_user(int account_id)
  126. {
  127. if( !login_config.online_check )
  128. return;
  129. if( account_id == 99 )
  130. {// reset all to offline
  131. online_db->clear(online_db, NULL); // purge db
  132. return;
  133. }
  134. idb_remove(online_db, account_id);
  135. }
  136. static int waiting_disconnect_timer(int tid, unsigned int tick, int id, int data)
  137. {
  138. struct online_login_data* p = idb_get(online_db, id);
  139. if( p != NULL && p->waiting_disconnect == id )
  140. {
  141. p->waiting_disconnect = -1;
  142. remove_online_user(id);
  143. }
  144. return 0;
  145. }
  146. static int sync_ip_addresses(int tid, unsigned int tick, int id, int data)
  147. {
  148. uint8 buf[2];
  149. ShowInfo("IP Sync in progress...\n");
  150. WBUFW(buf,0) = 0x2735;
  151. charif_sendallwos(-1, buf, 2);
  152. return 0;
  153. }
  154. //-----------------------------------------------------
  155. // Read GM accounts
  156. //-----------------------------------------------------
  157. void read_gm_account(void)
  158. {
  159. if( !login_config.login_gm_read )
  160. return;// char server's job
  161. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `%s`,`%s` FROM `%s` WHERE `%s` > '0'", login_db_account_id, login_db_level, login_db, login_db_level) )
  162. {
  163. Sql_ShowDebug(sql_handle);
  164. return;// Failed to read GM list!
  165. }
  166. RECREATE(gm_account_db, struct gm_account, (size_t)Sql_NumRows(sql_handle));
  167. for( GM_num = 0; SQL_SUCCESS == Sql_NextRow(sql_handle); ++GM_num )
  168. {
  169. char* account;
  170. char* level;
  171. Sql_GetData(sql_handle, 0, &account, NULL);
  172. Sql_GetData(sql_handle, 1, &level, NULL);
  173. gm_account_db[GM_num].account_id = atoi(account);
  174. gm_account_db[GM_num].level = atoi(level);
  175. }
  176. Sql_FreeResult(sql_handle);
  177. }
  178. //-----------------------------------------------------
  179. // Send GM accounts to one or all char-servers
  180. //-----------------------------------------------------
  181. void send_GM_accounts(int fd)
  182. {
  183. unsigned int i;
  184. uint8 buf[32767];
  185. uint16 len;
  186. if( !login_config.login_gm_read )
  187. return;
  188. len = 4;
  189. WBUFW(buf,0) = 0x2732;
  190. for( i = 0; i < GM_num; ++i )
  191. {
  192. // send only existing accounts. We can not create a GM account when server is online.
  193. if( gm_account_db[i].level > 0 )
  194. {
  195. WBUFL(buf,len) = gm_account_db[i].account_id;
  196. WBUFB(buf,len+4) = (uint8)gm_account_db[i].level;
  197. len += 5;
  198. if( len >= 32000 )
  199. {
  200. ShowWarning("send_GM_accounts: Too many accounts! Only %d out of %d were sent.\n", i, GM_num);
  201. break;
  202. }
  203. }
  204. }
  205. WBUFW(buf,2) = len;
  206. if( fd == -1 )// send to all charservers
  207. charif_sendallwos(-1, buf, len);
  208. else
  209. {// send only to target
  210. WFIFOHEAD(fd,len);
  211. memcpy(WFIFOP(fd,0), buf, len);
  212. WFIFOSET(fd,len);
  213. }
  214. return;
  215. }
  216. /*=============================================
  217. * Does a mysql_ping to all connection handles
  218. *---------------------------------------------*/
  219. int login_sql_ping(int tid, unsigned int tick, int id, int data)
  220. {
  221. ShowInfo("Pinging SQL server to keep connection alive...\n");
  222. Sql_Ping(sql_handle);
  223. return 0;
  224. }
  225. int sql_ping_init(void)
  226. {
  227. uint32 connection_timeout, connection_ping_interval;
  228. // set a default value first
  229. connection_timeout = 28800; // 8 hours
  230. // ask the mysql server for the timeout value
  231. if( SQL_SUCCESS == Sql_GetTimeout(sql_handle, &connection_timeout) && connection_timeout < 60 )
  232. connection_timeout = 60;
  233. // establish keepalive
  234. connection_ping_interval = connection_timeout - 30; // 30-second reserve
  235. add_timer_func_list(login_sql_ping, "login_sql_ping");
  236. add_timer_interval(gettick() + connection_ping_interval*1000, login_sql_ping, 0, 0, connection_ping_interval*1000);
  237. return 0;
  238. }
  239. //-----------------------------------------------------
  240. // Read Account database - mysql db
  241. //-----------------------------------------------------
  242. int mmo_auth_sqldb_init(void)
  243. {
  244. ShowStatus("Login server init....\n");
  245. sql_handle = Sql_Malloc();
  246. // DB connection start
  247. ShowStatus("Connect Login Database Server....\n");
  248. if( SQL_ERROR == Sql_Connect(sql_handle, login_server_id, login_server_pw, login_server_ip, login_server_port, login_server_db) )
  249. {
  250. Sql_ShowDebug(sql_handle);
  251. Sql_Free(sql_handle);
  252. exit(1);
  253. }
  254. else
  255. {
  256. ShowStatus("Connect success!\n");
  257. }
  258. if( default_codepage[0] != '\0' && SQL_ERROR == Sql_SetEncoding(sql_handle, default_codepage) )
  259. Sql_ShowDebug(sql_handle);
  260. if( login_config.log_login && SQL_ERROR == Sql_Query(sql_handle, "INSERT DELAYED INTO `%s` (`time`,`ip`,`user`,`rcode`,`log`) VALUES (NOW(), '0', 'lserver','100','login server started')", loginlog_db) )
  261. Sql_ShowDebug(sql_handle);
  262. sql_ping_init();
  263. return 0;
  264. }
  265. //-----------------------------------------------------
  266. // close DB
  267. //-----------------------------------------------------
  268. void mmo_db_close(void)
  269. {
  270. int i, fd;
  271. //set log.
  272. if( login_config.log_login && SQL_ERROR == Sql_Query(sql_handle, "INSERT DELAYED INTO `%s`(`time`,`ip`,`user`,`rcode`,`log`) VALUES (NOW(), '0', 'lserver','100', 'login server shutdown')", loginlog_db) )
  273. Sql_ShowDebug(sql_handle);
  274. for( i = 0; i < MAX_SERVERS; ++i )
  275. {
  276. fd = server_fd[i];
  277. if( session_isValid(fd) )
  278. {// Clean only data related to servers we are connected to. [Skotlex]
  279. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `sstatus` WHERE `index` = '%d'", i) )
  280. Sql_ShowDebug(sql_handle);
  281. do_close(fd);
  282. }
  283. }
  284. Sql_Free(sql_handle);
  285. sql_handle = NULL;
  286. ShowStatus("close DB connect....\n");
  287. if( login_fd > 0 )
  288. do_close(login_fd);
  289. }
  290. //--------------------------------------------------------------------
  291. // Packet send to all char-servers, except one (wos: without our self)
  292. //--------------------------------------------------------------------
  293. int charif_sendallwos(int sfd, uint8* buf, size_t len)
  294. {
  295. int i, c;
  296. for( i = 0, c = 0; i < MAX_SERVERS; ++i )
  297. {
  298. int fd = server_fd[i];
  299. if( session_isValid(fd) && fd != sfd )
  300. {
  301. WFIFOHEAD(fd,len);
  302. memcpy(WFIFOP(fd,0), buf, len);
  303. WFIFOSET(fd,len);
  304. ++c;
  305. }
  306. }
  307. return c;
  308. }
  309. //-----------------------------------------------------
  310. // encrypted/unencrypted password check
  311. //-----------------------------------------------------
  312. bool check_encrypted(const char* str1, const char* str2, const char* passwd)
  313. {
  314. char md5str[64], md5bin[32];
  315. snprintf(md5str, sizeof(md5str), "%s%s", str1, str2);
  316. md5str[sizeof(md5str)-1] = '\0';
  317. MD5_String2binary(md5str, md5bin);
  318. return (0==memcmp(passwd, md5bin, 16));
  319. }
  320. bool check_password(struct login_session_data* ld, int passwdenc, const char* passwd, const char* refpass)
  321. {
  322. if(passwdenc == 0)
  323. {
  324. return (0==strcmp(passwd, refpass));
  325. }
  326. else if (ld)
  327. {
  328. // password mode set to 1 -> (md5key, refpass) enable with <passwordencrypt></passwordencrypt>
  329. // password mode set to 2 -> (refpass, md5key) enable with <passwordencrypt2></passwordencrypt2>
  330. return ((passwdenc&0x01) && check_encrypted(ld->md5key, refpass, passwd)) ||
  331. ((passwdenc&0x02) && check_encrypted(refpass, ld->md5key, passwd));
  332. }
  333. return false;
  334. }
  335. //-----------------------------------------------------
  336. // Make new account
  337. //-----------------------------------------------------
  338. int mmo_auth_new(struct mmo_account* account, char sex)
  339. {
  340. unsigned int tick = gettick();
  341. char md5buf[32+1];
  342. SqlStmt* stmt;
  343. int result = 0;
  344. //Account Registration Flood Protection by [Kevin]
  345. if( DIFF_TICK(tick, new_reg_tick) < 0 && num_regs >= allowed_regs )
  346. {
  347. ShowNotice("Account registration denied (registration limit exceeded)\n");
  348. return 3;
  349. }
  350. // check if the account doesn't exist already
  351. stmt = SqlStmt_Malloc(sql_handle);
  352. if( SQL_SUCCESS != SqlStmt_Prepare(stmt, "SELECT `%s` FROM `%s` WHERE `userid` = ?", login_db_userid, login_db)
  353. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, account->userid, strnlen(account->userid, NAME_LENGTH))
  354. || SQL_SUCCESS != SqlStmt_Execute(stmt) )
  355. {
  356. SqlStmt_ShowDebug(stmt);
  357. result = 1;// error
  358. }
  359. else if( SqlStmt_NumRows(stmt) > 0 )
  360. result = 1;// incorrect user/pass
  361. SqlStmt_Free(stmt);
  362. if( result )
  363. return result;// error or incorrect user/pass
  364. // insert new entry into db
  365. //TODO: error checking
  366. stmt = SqlStmt_Malloc(sql_handle);
  367. SqlStmt_Prepare(stmt, "INSERT INTO `%s` (`%s`, `%s`, `sex`, `email`) VALUES (?, ?, '%c', 'a@a.com')", login_db, login_db_userid, login_db_user_pass, TOUPPER(sex));
  368. SqlStmt_BindParam(stmt, 0, SQLDT_STRING, account->userid, strnlen(account->userid, NAME_LENGTH));
  369. if( login_config.use_md5_passwds )
  370. {
  371. MD5_String(account->passwd, md5buf);
  372. SqlStmt_BindParam(stmt, 1, SQLDT_STRING, md5buf, 32);
  373. }
  374. else
  375. SqlStmt_BindParam(stmt, 1, SQLDT_STRING, account->passwd, strnlen(account->passwd, NAME_LENGTH));
  376. SqlStmt_Execute(stmt);
  377. SqlStmt_Free(stmt);
  378. ShowInfo("New account: userid='%s' passwd='%s' sex='%c'\n", account->userid, account->passwd, TOUPPER(sex));
  379. if( DIFF_TICK(tick, new_reg_tick) > 0 )
  380. {// Update the registration check.
  381. num_regs = 0;
  382. new_reg_tick = tick + time_allowed*1000;
  383. }
  384. ++num_regs;
  385. return 0;
  386. }
  387. //-----------------------------------------------------
  388. // Check/authentication of a connection
  389. //-----------------------------------------------------
  390. int mmo_auth(struct mmo_account* account, int fd)
  391. {
  392. time_t ban_until_time;
  393. char esc_userid[NAME_LENGTH*2+1];// escaped username
  394. char user_password[256], password[256];
  395. long connect_until;
  396. int state;
  397. size_t len;
  398. char* data;
  399. char ip[16];
  400. uint8* sin_addr = (uint8*)&session[fd]->client_addr;
  401. sprintf(ip, "%u.%u.%u.%u", sin_addr[3], sin_addr[2], sin_addr[1], sin_addr[0]);
  402. // DNS Blacklist check
  403. if( login_config.use_dnsbl )
  404. {
  405. char r_ip[16];
  406. char ip_dnsbl[256];
  407. char* dnsbl_serv;
  408. bool matched = false;
  409. sprintf(r_ip, "%u.%u.%u.%u", sin_addr[0], sin_addr[1], sin_addr[2], sin_addr[3]);
  410. for( dnsbl_serv = strtok(login_config.dnsbl_servs,","); !matched && dnsbl_serv != NULL; dnsbl_serv = strtok(NULL,",") )
  411. {
  412. sprintf(ip_dnsbl, "%s.%s", r_ip, dnsbl_serv);
  413. if( host2ip(ip_dnsbl) )
  414. matched = true;
  415. }
  416. if( matched )
  417. {
  418. ShowInfo("DNSBL: (%s) Blacklisted. User Kicked.\n", r_ip);
  419. return 3;
  420. }
  421. }
  422. //Client Version check
  423. if( login_config.check_client_version && account->version != 0 &&
  424. account->version != login_config.client_version_to_connect )
  425. return 5;
  426. len = strnlen(account->userid, NAME_LENGTH);
  427. // Account creation with _M/_F
  428. if( login_config.new_account_flag )
  429. {
  430. if( len > 2 && strnlen(account->passwd, NAME_LENGTH) > 0 && // valid user and password lengths
  431. account->passwdenc == 0 && // unencoded password
  432. account->userid[len-2] == '_' && memchr("FfMm", (unsigned char)account->userid[len-1], 4) ) // _M/_F suffix
  433. {
  434. int result;
  435. char sex;
  436. len -= 2;
  437. account->userid[len] = '\0';// nul-terminate the name.
  438. sex = account->userid[len+1];
  439. result = mmo_auth_new(account, sex);
  440. if( result )
  441. return result;// Failed to make account. [Skotlex].
  442. }
  443. }
  444. // escape username
  445. Sql_EscapeStringLen(sql_handle, esc_userid, account->userid, len);
  446. // retrieve login entry for the specified username
  447. if( SQL_ERROR == Sql_Query(sql_handle,
  448. "SELECT `%s`,`%s`,`lastlogin`,`sex`,`connect_until`,`ban_until`,`state`,`%s` FROM `%s` WHERE `%s`= %s '%s'",
  449. login_db_account_id, login_db_user_pass, login_db_level,
  450. login_db, login_db_userid, (login_config.case_sensitive ? "BINARY" : ""), esc_userid) )
  451. Sql_ShowDebug(sql_handle);
  452. if( Sql_NumRows(sql_handle) == 0 ) // no such entry
  453. {
  454. ShowNotice("auth failed: no such account '%s'\n", esc_userid);
  455. Sql_FreeResult(sql_handle);
  456. return 0;
  457. }
  458. Sql_NextRow(sql_handle); //TODO: error checking?
  459. Sql_GetData(sql_handle, 0, &data, NULL); account->account_id = atoi(data);
  460. Sql_GetData(sql_handle, 1, &data, &len); safestrncpy(password, data, sizeof(password));
  461. Sql_GetData(sql_handle, 2, &data, NULL); safestrncpy(account->lastlogin, data, sizeof(account->lastlogin));
  462. Sql_GetData(sql_handle, 3, &data, NULL); account->sex = (*data == 'S' ? 2 : *data == 'M' ? 1 : 0);
  463. Sql_GetData(sql_handle, 4, &data, NULL); connect_until = atol(data);
  464. Sql_GetData(sql_handle, 5, &data, NULL); ban_until_time = atol(data);
  465. Sql_GetData(sql_handle, 6, &data, NULL); state = atoi(data);
  466. Sql_GetData(sql_handle, 7, &data, NULL); account->level = atoi(data);
  467. if( len > sizeof(password) - 1 )
  468. ShowDebug("mmo_auth: password buffer is too small (len=%u,buflen=%u)\n", len, sizeof(password));
  469. if( account->level > 99 )
  470. account->level = 99;
  471. Sql_FreeResult(sql_handle);
  472. if( login_config.use_md5_passwds )
  473. MD5_String(account->passwd, user_password);
  474. else
  475. safestrncpy(user_password, account->passwd, NAME_LENGTH);
  476. if( !check_password(session[fd]->session_data, account->passwdenc, user_password, password) )
  477. {
  478. ShowInfo("Invalid password (account: '%s', pass: '%s', received pass: '%s', ip: %s)\n",
  479. esc_userid, password, (account->passwdenc) ? "[MD5]" : user_password, ip);
  480. return 1; // 1 = Incorrect Password
  481. }
  482. if( connect_until != 0 && connect_until < time(NULL) )
  483. return 2; // 2 = This ID is expired
  484. if( ban_until_time != 0 && ban_until_time > time(NULL) )
  485. return 6; // 6 = Your are Prohibited to log in until %s
  486. if( state )
  487. {
  488. ShowInfo("Connection refused (account: %s, pass: %s, state: %d, ip: %s)\n", account->userid, account->passwd, state, ip);
  489. return state - 1;
  490. }
  491. if( login_config.online_check )
  492. {
  493. struct online_login_data* data = idb_get(online_db, account->account_id);
  494. if( data && data->char_server > -1 )
  495. {
  496. //Request char servers to kick this account out. [Skotlex]
  497. uint8 buf[8];
  498. ShowNotice("User '%s' is already online - Rejected.\n", account->userid);
  499. WBUFW(buf,0) = 0x2734;
  500. WBUFL(buf,2) = account->account_id;
  501. charif_sendallwos(-1, buf, 6);
  502. if( data->waiting_disconnect == -1 )
  503. data->waiting_disconnect = add_timer(gettick()+30000, waiting_disconnect_timer, account->account_id, 0);
  504. return 3; // Rejected
  505. }
  506. }
  507. account->login_id1 = rand();
  508. account->login_id2 = rand();
  509. if( account->sex != 2 && account->account_id < START_ACCOUNT_NUM )
  510. ShowWarning("Account %s has account id %d! Account IDs must be over %d to work properly!\n", account->userid, account->account_id, START_ACCOUNT_NUM);
  511. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `lastlogin` = NOW(), `logincount`=`logincount`+1, `last_ip`='%s', `ban_until`='0', `state`='0' WHERE `%s` = '%d'",
  512. login_db, ip, login_db_account_id, account->account_id) )
  513. Sql_ShowDebug(sql_handle);
  514. return -1; // account OK
  515. }
  516. static int online_db_setoffline(DBKey key, void* data, va_list ap)
  517. {
  518. struct online_login_data* p = (struct online_login_data*)data;
  519. int server = va_arg(ap, int);
  520. if( server == -1 )
  521. {
  522. p->char_server = -1;
  523. if( p->waiting_disconnect != -1 )
  524. {
  525. delete_timer(p->waiting_disconnect, waiting_disconnect_timer);
  526. p->waiting_disconnect = -1;
  527. }
  528. }
  529. else if( p->char_server == server )
  530. p->char_server = -2; //Char server disconnected.
  531. return 0;
  532. }
  533. //--------------------------------
  534. // Packet parsing for char-servers
  535. //--------------------------------
  536. int parse_fromchar(int fd)
  537. {
  538. int i, id;
  539. uint32 ipl = session[fd]->client_addr;
  540. char ip[16];
  541. ip2str(ipl, ip);
  542. ARR_FIND( 0, MAX_SERVERS, id, server_fd[id] == fd );
  543. if( id == MAX_SERVERS )
  544. {// not a char server
  545. set_eof(fd);
  546. do_close(fd);
  547. return 0;
  548. }
  549. if( session[fd]->eof )
  550. {
  551. ShowStatus("Char-server '%s' has disconnected.\n", server[id].name);
  552. server_fd[id] = -1;
  553. memset(&server[id], 0, sizeof(struct mmo_char_server));
  554. online_db->foreach(online_db, online_db_setoffline, id); //Set all chars from this char server to offline.
  555. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `sstatus` WHERE `index`='%d'", id) )
  556. Sql_ShowDebug(sql_handle);
  557. do_close(fd);
  558. return 0;
  559. }
  560. while( RFIFOREST(fd) >= 2 )
  561. {
  562. uint16 command = RFIFOW(fd,0);
  563. switch( command )
  564. {
  565. case 0x2709: // request from map-server via char-server to reload GM accounts
  566. if( login_config.log_login )
  567. {
  568. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT DELAYED INTO `%s`(`time`,`ip`,`user`,`log`) VALUES (NOW(), '%u', '%s', 'GM reload request')", loginlog_db, ipl, server[id].name) )
  569. Sql_ShowDebug(sql_handle);
  570. }
  571. read_gm_account();
  572. // send GM accounts to all char-servers
  573. send_GM_accounts(-1);
  574. RFIFOSKIP(fd,2);
  575. break;
  576. case 0x2712: // request from char-server to authenticate an account
  577. if( RFIFOREST(fd) < 19 )
  578. return 0;
  579. {
  580. uint32 account_id = RFIFOL(fd,2);
  581. for( i = 0; i < AUTH_FIFO_SIZE; ++i )
  582. {
  583. if( auth_fifo[i].account_id == RFIFOL(fd,2) &&
  584. auth_fifo[i].login_id1 == RFIFOL(fd,6) &&
  585. auth_fifo[i].login_id2 == RFIFOL(fd,10) &&
  586. auth_fifo[i].sex == RFIFOB(fd,14) &&
  587. auth_fifo[i].ip == ntohl(RFIFOL(fd,15)) &&
  588. !auth_fifo[i].delflag)
  589. {
  590. auth_fifo[i].delflag = 1;
  591. break;
  592. }
  593. }
  594. if( i != AUTH_FIFO_SIZE && account_id > 0 )
  595. {// send ack
  596. uint32 connect_until_time;
  597. char email[40];
  598. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `email`,`connect_until` FROM `%s` WHERE `%s`='%d'", login_db, login_db_account_id, account_id) )
  599. Sql_ShowDebug(sql_handle);
  600. if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  601. {
  602. char* data = NULL;
  603. size_t len = 0;
  604. Sql_GetData(sql_handle, 0, &data, &len); safestrncpy(email, data, sizeof(email));
  605. Sql_GetData(sql_handle, 1, &data, NULL); connect_until_time = (uint32)strtoul(data, NULL, 10);
  606. if( len > sizeof(email) )
  607. ShowDebug("parse_fromchar:0x2712: email is too long (len=%u,maxlen=%u)\n", len, sizeof(email));
  608. Sql_FreeResult(sql_handle);
  609. }
  610. else
  611. {
  612. memset(email, 0, sizeof(email));
  613. connect_until_time = 0;
  614. }
  615. WFIFOHEAD(fd,51);
  616. WFIFOW(fd,0) = 0x2713;
  617. WFIFOL(fd,2) = account_id;
  618. WFIFOB(fd,6) = 0;
  619. memcpy(WFIFOP(fd, 7), email, 40);
  620. WFIFOL(fd,47) = connect_until_time;
  621. WFIFOSET(fd,51);
  622. }
  623. else
  624. {// authentification not found
  625. WFIFOHEAD(fd,51);
  626. WFIFOW(fd,0) = 0x2713;
  627. WFIFOL(fd,2) = account_id;
  628. WFIFOB(fd,6) = 1;
  629. // It is unnecessary to send email
  630. // It is unnecessary to send validity date of the account
  631. WFIFOSET(fd,51);
  632. }
  633. RFIFOSKIP(fd,19);
  634. }
  635. break;
  636. case 0x2714:
  637. if (RFIFOREST(fd) < 6)
  638. return 0;
  639. // how many users on world? (update)
  640. if( server[id].users != RFIFOL(fd,2) )
  641. {
  642. ShowStatus("set users %s : %d\n", server[id].name, RFIFOL(fd,2));
  643. server[id].users = RFIFOL(fd,2);
  644. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `sstatus` SET `user` = '%d' WHERE `index` = '%d'", server[id].users, id) )
  645. Sql_ShowDebug(sql_handle);
  646. }
  647. // send some answer
  648. WFIFOHEAD(fd,2);
  649. WFIFOW(fd,0) = 0x2718;
  650. WFIFOSET(fd,2);
  651. RFIFOSKIP(fd,6);
  652. break;
  653. case 0x2716: // received an e-mail/limited time request, because a player comes back from a map-server to the char-server
  654. if( RFIFOREST(fd) < 6 )
  655. return 0;
  656. {
  657. int account_id;
  658. uint32 connect_until_time = 0;
  659. char email[40];
  660. memset(email, 0, sizeof(email));
  661. account_id = RFIFOL(fd,2);
  662. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `email`,`connect_until` FROM `%s` WHERE `%s`='%d'", login_db, login_db_account_id, account_id) )
  663. Sql_ShowDebug(sql_handle);
  664. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  665. {
  666. char* data;
  667. size_t len;
  668. Sql_GetData(sql_handle, 0, &data, &len);
  669. if( len > sizeof(email) )
  670. {
  671. #if defined(DEBUG)
  672. ShowDebug("parse_fromchar:0x2716: email is too long (len=%u,maxlen=%u)\n", len, sizeof(email));
  673. #endif
  674. len = sizeof(email);
  675. }
  676. memcpy(email, data, len);
  677. Sql_GetData(sql_handle, 1, &data, NULL);
  678. connect_until_time = (uint32)strtoul(data, NULL, 10);
  679. Sql_FreeResult(sql_handle);
  680. }
  681. //printf("parse_fromchar: E-mail/limited time request from '%s' server (concerned account: %d)\n", server[id].name, RFIFOL(fd,2));
  682. WFIFOHEAD(fd,50);
  683. WFIFOW(fd,0) = 0x2717;
  684. WFIFOL(fd,2) = RFIFOL(fd,2);
  685. memcpy(WFIFOP(fd, 6), email, 40);
  686. WFIFOL(fd,46) = connect_until_time;
  687. WFIFOSET(fd,50);
  688. RFIFOSKIP(fd,6);
  689. }
  690. break;
  691. case 0x2720: // Request to become a GM (TXT only!)
  692. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  693. return 0;
  694. //oldacc = RFIFOL(fd,4);
  695. ShowWarning("change GM isn't supported in this login server version.\n");
  696. ShowError("change GM error 0 %s\n", RFIFOP(fd, 8));
  697. WFIFOHEAD(fd,10);
  698. WFIFOW(fd,0) = 0x2721;
  699. WFIFOL(fd,2) = RFIFOL(fd,4); // oldacc;
  700. WFIFOL(fd,6) = 0; // newacc;
  701. WFIFOSET(fd,10);
  702. RFIFOSKIP(fd, RFIFOW(fd, 2));
  703. return 0;
  704. // Map server send information to change an email of an account via char-server
  705. case 0x2722: // 0x2722 <account_id>.L <actual_e-mail>.40B <new_e-mail>.40B
  706. if (RFIFOREST(fd) < 86)
  707. return 0;
  708. {
  709. int account_id;
  710. char actual_email[40], new_email[40];
  711. account_id = RFIFOL(fd,2);
  712. memcpy(actual_email, RFIFOP(fd,6), 40);
  713. memcpy(new_email, RFIFOP(fd,46), 40);
  714. if( e_mail_check(actual_email) == 0 )
  715. ShowWarning("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",
  716. server[id].name, account_id, ip);
  717. else if( e_mail_check(new_email) == 0 )
  718. ShowWarning("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",
  719. server[id].name, account_id, ip);
  720. else if( strcmpi(new_email, "a@a.com") == 0 )
  721. ShowWarning("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",
  722. server[id].name, account_id, ip);
  723. else if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `%s`,`email` FROM `%s` WHERE `%s` = '%d'", login_db_userid, login_db, login_db_account_id, account_id) )
  724. Sql_ShowDebug(sql_handle);
  725. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  726. {
  727. char* data;
  728. size_t len;
  729. Sql_GetData(sql_handle, 1, &data, &len);
  730. if( len > sizeof(actual_email) )
  731. {
  732. #if defined(DEBUG)
  733. ShowDebug("parse_fromchar:0x2722: email is too long (len=%u,maxlen=%u)\n", len, sizeof(actual_email));
  734. #endif
  735. len = sizeof(actual_email);
  736. }
  737. if( strncasecmp(data, actual_email, sizeof(actual_email)) == 0 )
  738. {
  739. char esc_user_id[NAME_LENGTH*2+1];
  740. char esc_new_email[sizeof(new_email)*2+1];
  741. Sql_GetData(sql_handle, 0, &data, &len);
  742. if( len > NAME_LENGTH )
  743. {
  744. #if defined(DEBUG)
  745. ShowDebug("parse_fromchar:0x2722: userid is too long (len=%u,maxlen=%u)\n", len, NAME_LENGTH);
  746. #endif
  747. len = NAME_LENGTH;
  748. }
  749. Sql_EscapeStringLen(sql_handle, esc_user_id, data, len);
  750. Sql_EscapeStringLen(sql_handle, esc_new_email, new_email, strnlen(new_email, sizeof(new_email)));
  751. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `email` = '%s' WHERE `%s` = '%d'", login_db, esc_new_email, login_db_account_id, account_id) )
  752. Sql_ShowDebug(sql_handle);
  753. ShowInfo("Char-server '%s': Modify an e-mail on an account (@email GM command) (account: %d ('%s'), new e-mail: '%s', ip: %s).\n",
  754. server[id].name, account_id, esc_user_id, esc_new_email, ip);
  755. }
  756. Sql_FreeResult(sql_handle);
  757. }
  758. RFIFOSKIP(fd, 86);
  759. }
  760. break;
  761. case 0x2724: // Receiving an account state update request from a map-server (relayed via char-server)
  762. if (RFIFOREST(fd) < 10)
  763. return 0;
  764. {
  765. int account_id, state;
  766. account_id = RFIFOL(fd,2);
  767. state = RFIFOL(fd,6);
  768. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `state` FROM `%s` WHERE `%s` = '%d'", login_db, login_db_account_id, account_id) )
  769. Sql_ShowDebug(sql_handle);
  770. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  771. {
  772. char* data;
  773. Sql_GetData(sql_handle, 0, &data, NULL);
  774. if( atoi(data) != state && state != 0 )
  775. {
  776. uint8 buf[11];
  777. WBUFW(buf,0) = 0x2731;
  778. WBUFL(buf,2) = account_id;
  779. WBUFB(buf,6) = 0; // 0: change of state, 1: ban
  780. WBUFL(buf,7) = state; // status or final date of a banishment
  781. charif_sendallwos(-1, buf, 11);
  782. }
  783. Sql_FreeResult(sql_handle);
  784. }
  785. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `state` = '%d' WHERE `%s` = '%d'", login_db, state, login_db_account_id, account_id) )
  786. Sql_ShowDebug(sql_handle);
  787. RFIFOSKIP(fd,10);
  788. }
  789. break;
  790. case 0x2725: // Receiving of map-server via char-server a ban request
  791. if (RFIFOREST(fd) < 18)
  792. return 0;
  793. {
  794. int account_id;
  795. struct tm *tmtime;
  796. time_t tmptime = 0;
  797. time_t timestamp = time(NULL);
  798. account_id = RFIFOL(fd,2);
  799. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `ban_until` FROM `%s` WHERE `%s` = '%d'", login_db, login_db_account_id, account_id) )
  800. Sql_ShowDebug(sql_handle);
  801. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  802. {
  803. char* data;
  804. Sql_GetData(sql_handle, 0, &data, NULL);
  805. tmptime = (time_t)strtoul(data, NULL, 10);
  806. if( tmptime > time(NULL) )
  807. timestamp = tmptime;
  808. }
  809. tmtime = localtime(&timestamp);
  810. tmtime->tm_year = tmtime->tm_year + (int16)RFIFOW(fd,6);
  811. tmtime->tm_mon = tmtime->tm_mon + (int16)RFIFOW(fd,8);
  812. tmtime->tm_mday = tmtime->tm_mday + (int16)RFIFOW(fd,10);
  813. tmtime->tm_hour = tmtime->tm_hour + (int16)RFIFOW(fd,12);
  814. tmtime->tm_min = tmtime->tm_min + (int16)RFIFOW(fd,14);
  815. tmtime->tm_sec = tmtime->tm_sec + (int16)RFIFOW(fd,16);
  816. timestamp = mktime(tmtime);
  817. if( timestamp != (time_t)-1 )
  818. {
  819. if( timestamp <= time(NULL) )
  820. timestamp = 0;
  821. if( tmptime != timestamp )
  822. {
  823. if( timestamp != 0 )
  824. {
  825. uint8 buf[11];
  826. WBUFW(buf,0) = 0x2731;
  827. WBUFL(buf,2) = account_id;
  828. WBUFB(buf,6) = 1; // 0: change of statut, 1: ban
  829. WBUFL(buf,7) = (uint32)timestamp; // status or final date of a banishment
  830. charif_sendallwos(-1, buf, 11);
  831. }
  832. ShowNotice("Account: %d Banned until: %lu\n", account_id, (unsigned long)timestamp);
  833. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `ban_until` = '%lu' WHERE `%s` = '%d'", login_db, (unsigned long)timestamp, login_db_account_id, account_id) )
  834. Sql_ShowDebug(sql_handle);
  835. }
  836. }
  837. RFIFOSKIP(fd,18);
  838. }
  839. break;
  840. case 0x2727: // Change of sex (sex is reversed)
  841. if (RFIFOREST(fd) < 6)
  842. return 0;
  843. {
  844. int account_id;
  845. int sex;
  846. uint8 buf[16];
  847. account_id = RFIFOL(fd,2);
  848. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `sex` FROM `%s` WHERE `%s` = '%d'", login_db, login_db_account_id, account_id) )
  849. Sql_ShowDebug(sql_handle);
  850. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  851. {
  852. char* data;
  853. Sql_GetData(sql_handle, 0, &data, NULL);
  854. if( *data == 'M' || *data == 'm' )
  855. sex = 0; //Change to female
  856. else
  857. sex = 1; //Change to male
  858. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `sex` = '%c' WHERE `%s` = '%d'", login_db, (sex ? 'M' : 'F'), login_db_account_id, account_id) )
  859. Sql_ShowDebug(sql_handle);
  860. WBUFW(buf,0) = 0x2723;
  861. WBUFL(buf,2) = account_id;
  862. WBUFB(buf,6) = sex;
  863. charif_sendallwos(-1, buf, 7);
  864. }
  865. RFIFOSKIP(fd,6);
  866. }
  867. break;
  868. case 0x2728: // save account_reg2
  869. if( RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2) )
  870. return 0;
  871. if( RFIFOL(fd,4) > 0 )
  872. {
  873. SqlStmt* stmt;
  874. int account_id;
  875. size_t off;
  876. account_id = RFIFOL(fd,4);
  877. //Delete all global account variables....
  878. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `type`='1' AND `account_id`='%d';", reg_db, account_id) )
  879. Sql_ShowDebug(sql_handle);
  880. //Proceed to insert them....
  881. stmt = SqlStmt_Malloc(sql_handle);
  882. if( SQL_ERROR == SqlStmt_Prepare(stmt, "INSERT INTO `%s` (`type`, `account_id`, `str`, `value`) VALUES ( 1 , '%d' , ? , ?);", reg_db, account_id) )
  883. SqlStmt_ShowDebug(stmt);
  884. for( i = 0, off = 13; i < ACCOUNT_REG2_NUM && off < RFIFOW(fd,2); ++i )
  885. {
  886. uint8* p;
  887. size_t len;
  888. // str
  889. p = (uint8*)RFIFOP(fd,off);
  890. len = strlen(p);
  891. SqlStmt_BindParam(stmt, 0, SQLDT_STRING, p, len);
  892. off += len + 1;
  893. // value
  894. p = (uint8*)RFIFOP(fd,off);
  895. len = strlen(p);
  896. SqlStmt_BindParam(stmt, 1, SQLDT_STRING, p, len);
  897. off += len + 1;
  898. if( SQL_ERROR == SqlStmt_Execute(stmt) )
  899. SqlStmt_ShowDebug(stmt);
  900. }
  901. SqlStmt_Free(stmt);
  902. {// Send to char
  903. //uint8* buf;
  904. //CREATE(buf, uint8, RFIFOW(fd,2));
  905. //memcpy(WBUFP(buf,0), RFIFOP(fd,0), RFIFOW(fd,2));
  906. //WBUFW(buf,0)=0x2729;
  907. //charif_sendallwos(fd, buf, WBUFW(buf,2));
  908. //aFree(buf);
  909. RFIFOW(fd,0) = 0x2729;// reusing read buffer
  910. charif_sendallwos(fd, RFIFOP(fd,0), RFIFOW(fd,2));
  911. }
  912. }
  913. RFIFOSKIP(fd,RFIFOW(fd,2));
  914. break;
  915. case 0x272a: // Receiving of map-server via char-server an unban request
  916. if( RFIFOREST(fd) < 6 )
  917. return 0;
  918. {
  919. int account_id;
  920. account_id = RFIFOL(fd,2);
  921. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `ban_until` FROM `%s` WHERE `%s` = '%d'", login_db, login_db_account_id, account_id) )
  922. Sql_ShowDebug(sql_handle);
  923. else if( Sql_NumRows(sql_handle) > 0 )
  924. {// Found a match
  925. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `ban_until` = '0' WHERE `%s` = '%d'", login_db, login_db_account_id, account_id) )
  926. Sql_ShowDebug(sql_handle);
  927. }
  928. RFIFOSKIP(fd,6);
  929. return 0;
  930. }
  931. case 0x272b: // Set account_id to online [Wizputer]
  932. if (RFIFOREST(fd) < 6)
  933. return 0;
  934. add_online_user(id, RFIFOL(fd,2));
  935. RFIFOSKIP(fd,6);
  936. break;
  937. case 0x272c: // Set account_id to offline [Wizputer]
  938. if (RFIFOREST(fd) < 6)
  939. return 0;
  940. remove_online_user(RFIFOL(fd,2));
  941. RFIFOSKIP(fd,6);
  942. break;
  943. case 0x272d: // Receive list of all online accounts. [Skotlex]
  944. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  945. return 0;
  946. if( login_config.online_check )
  947. {
  948. struct online_login_data *p;
  949. int aid;
  950. uint32 i, users;
  951. online_db->foreach(online_db, online_db_setoffline, id); //Set all chars from this char-server offline first
  952. users = RFIFOW(fd,4);
  953. for (i = 0; i < users; i++) {
  954. aid = RFIFOL(fd,6+i*4);
  955. p = idb_ensure(online_db, aid, create_online_user);
  956. p->char_server = id;
  957. if (p->waiting_disconnect != -1)
  958. {
  959. delete_timer(p->waiting_disconnect, waiting_disconnect_timer);
  960. p->waiting_disconnect = -1;
  961. }
  962. }
  963. }
  964. RFIFOSKIP(fd,RFIFOW(fd,2));
  965. break;
  966. case 0x272e: //Request account_reg2 for a character.
  967. if (RFIFOREST(fd) < 10)
  968. return 0;
  969. {
  970. int account_id = RFIFOL(fd, 2);
  971. int char_id = RFIFOL(fd, 6);
  972. size_t off;
  973. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `str`,`value` FROM `%s` WHERE `type`='1' AND `account_id`='%d'", reg_db, account_id) )
  974. Sql_ShowDebug(sql_handle);
  975. WFIFOHEAD(fd,10000);
  976. WFIFOW(fd,0) = 0x2729;
  977. WFIFOL(fd,4) = account_id;
  978. WFIFOL(fd,8) = char_id;
  979. WFIFOB(fd,12) = 1; //Type 1 for Account2 registry
  980. off = 13;
  981. while( SQL_SUCCESS == Sql_NextRow(sql_handle) && off < 9000 )
  982. {
  983. char* data;
  984. // str
  985. Sql_GetData(sql_handle, 0, &data, NULL);
  986. if( *data != '\0' )
  987. {
  988. off += sprintf(WFIFOP(fd,off), "%s", data)+1; //We add 1 to consider the '\0' in place.
  989. // value
  990. Sql_GetData(sql_handle, 1, &data, NULL);
  991. off += sprintf(WFIFOP(fd,off), "%s", data)+1;
  992. }
  993. }
  994. Sql_FreeResult(sql_handle);
  995. if( off >= 9000 )
  996. ShowWarning("Too many account2 registries for AID %d. Some registries were not sent.\n", account_id);
  997. WFIFOW(fd,2) = (uint16)off;
  998. WFIFOSET(fd,WFIFOW(fd,2));
  999. RFIFOSKIP(fd,10);
  1000. }
  1001. break;
  1002. case 0x2736: // WAN IP update from char-server
  1003. if (RFIFOREST(fd) < 6)
  1004. return 0;
  1005. server[id].ip = ntohl(RFIFOL(fd,2));
  1006. ShowInfo("Updated IP of Server #%d to %d.%d.%d.%d.\n",id, CONVIP(server[id].ip));
  1007. RFIFOSKIP(fd,6);
  1008. break;
  1009. case 0x2737: //Request to set all offline.
  1010. ShowInfo("Setting accounts from char-server %d offline.\n", id);
  1011. online_db->foreach(online_db, online_db_setoffline, id);
  1012. RFIFOSKIP(fd,2);
  1013. break;
  1014. default:
  1015. ShowError("parse_fromchar: Unknown packet 0x%x from a char-server! Disconnecting!\n", command);
  1016. set_eof(fd);
  1017. return 0;
  1018. } // switch
  1019. } // while
  1020. RFIFOSKIP(fd,RFIFOREST(fd));
  1021. return 0;
  1022. }
  1023. //--------------------------------------------
  1024. // Test to know if an IP come from LAN or WAN.
  1025. //--------------------------------------------
  1026. int lan_subnetcheck(uint32 ip)
  1027. {
  1028. int i;
  1029. ARR_FIND( 0, subnet_count, i, (subnet[i].char_ip & subnet[i].mask) == (ip & subnet[i].mask) );
  1030. return ( i < subnet_count ) ? subnet[i].char_ip : 0;
  1031. }
  1032. int login_ip_ban_check(uint32 ip)
  1033. {
  1034. uint8* p = (uint8*)&ip;
  1035. char* data = NULL;
  1036. int matches;
  1037. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT count(*) FROM `ipbanlist` WHERE `list` = '%u.*.*.*' OR `list` = '%u.%u.*.*' OR `list` = '%u.%u.%u.*' OR `list` = '%u.%u.%u.%u'",
  1038. p[3], p[3], p[2], p[3], p[2], p[1], p[3], p[2], p[1], p[0]) )
  1039. {
  1040. Sql_ShowDebug(sql_handle);
  1041. // close connection because we can't verify their connectivity.
  1042. return 1;
  1043. }
  1044. if( SQL_ERROR == Sql_NextRow(sql_handle) )
  1045. return 1;// Shouldn't happen, but just in case...
  1046. Sql_GetData(sql_handle, 0, &data, NULL);
  1047. matches = atoi(data);
  1048. Sql_FreeResult(sql_handle);
  1049. if( matches == 0 )
  1050. return 0;// No ban
  1051. // ip ban ok.
  1052. ShowInfo("Packet from banned ip : %u.%u.%u.%u\n", CONVIP(ip));
  1053. if( login_config.log_login && SQL_ERROR == Sql_Query(sql_handle, "INSERT DELAYED INTO `%s`(`time`,`ip`,`user`,`rcode`,`log`) VALUES (NOW(), '%u', 'unknown','-3', 'ip banned')", loginlog_db, ip) )
  1054. Sql_ShowDebug(sql_handle);
  1055. return 1;
  1056. }
  1057. //----------------------------------------------------------------------------------------
  1058. // Default packet parsing (normal players or administation/char-server connection requests)
  1059. //----------------------------------------------------------------------------------------
  1060. int parse_login(int fd)
  1061. {
  1062. char esc_userid[NAME_LENGTH*2+1];// escaped username
  1063. struct mmo_account account;
  1064. int result, i;
  1065. uint32 ipl = session[fd]->client_addr;
  1066. char ip[16];
  1067. ip2str(ipl, ip);
  1068. if( session[fd]->eof )
  1069. {
  1070. do_close(fd);
  1071. return 0;
  1072. }
  1073. while( RFIFOREST(fd) >= 2 )
  1074. {
  1075. uint16 command = RFIFOW(fd,0);
  1076. switch( command )
  1077. {
  1078. case 0x0200: // New alive packet: structure: 0x200 <account.userid>.24B. used to verify if client is always alive.
  1079. if (RFIFOREST(fd) < 26)
  1080. return 0;
  1081. RFIFOSKIP(fd,26);
  1082. break;
  1083. case 0x0204: // New alive packet: structure: 0x204 <encrypted.account.userid>.16B. (new ragexe from 22 june 2004)
  1084. if (RFIFOREST(fd) < 18)
  1085. return 0;
  1086. RFIFOSKIP(fd,18);
  1087. break;
  1088. case 0x0064: // request client login
  1089. case 0x01dd: // request client login (encryption mode)
  1090. case 0x0277: // New login packet (kRO 2006-04-24aSakexe langtype 0)
  1091. case 0x02b0: // New login packet (kRO 2007-05-14aSakexe langtype 0)
  1092. {
  1093. size_t packet_len = RFIFOREST(fd);
  1094. // Perform ip-ban check
  1095. if( login_config.ipban && login_ip_ban_check(ipl) )
  1096. {
  1097. WFIFOHEAD(fd,23);
  1098. WFIFOW(fd,0) = 0x6a;
  1099. WFIFOB(fd,2) = 3; // 3 = Rejected from Server
  1100. WFIFOSET(fd,23);
  1101. RFIFOSKIP(fd,packet_len);
  1102. set_eof(fd);
  1103. break;
  1104. }
  1105. if( (command == 0x0064 && packet_len < 55)
  1106. || (command == 0x01dd && packet_len < 47)
  1107. || (command == 0x0277 && packet_len < 84)
  1108. || (command == 0x02b0 && packet_len < 85) )
  1109. return 0;
  1110. // S 0064 <version>.l <account name>.24B <password>.24B <version2>.B
  1111. // S 01dd <version>.l <account name>.24B <md5 binary>.16B <version2>.B
  1112. // S 0277 <version>.l <account name>.24B <password>.24B <junk?>.29B <version2>.B
  1113. // S 02b0 <version>.l <account name>.24B <password>.24B <junk?>.30B <version2>.B
  1114. memset(&account, 0, sizeof(account));
  1115. account.version = RFIFOL(fd,2);
  1116. if( !account.version )
  1117. account.version = 1; //Force some version...
  1118. safestrncpy(account.userid, RFIFOP(fd,6), NAME_LENGTH);//## does it have to be nul-terminated?
  1119. safestrncpy(account.passwd, RFIFOP(fd,30), NAME_LENGTH);//## does it have to be nul-terminated?
  1120. account.passwdenc = (command == 0x01dd) ? PASSWORDENC : 0;
  1121. Sql_EscapeStringLen(sql_handle, esc_userid, account.userid, strlen(account.userid));
  1122. result = mmo_auth(&account, fd);
  1123. if( result == -1 )
  1124. { // auth success
  1125. if( login_config.min_level_to_connect > account.level )
  1126. {
  1127. WFIFOHEAD(fd,3);
  1128. WFIFOW(fd,0) = 0x81;
  1129. WFIFOB(fd,2) = 1; // 01 = Server closed
  1130. WFIFOSET(fd,3);
  1131. }
  1132. else
  1133. {
  1134. uint8 server_num = 0;
  1135. if( login_config.log_login && SQL_ERROR == Sql_Query(sql_handle, "INSERT DELAYED INTO `%s`(`time`,`ip`,`user`,`rcode`,`log`) VALUES (NOW(), '%u', '%s','100', 'login ok')", loginlog_db, ipl, esc_userid) )
  1136. Sql_ShowDebug(sql_handle);
  1137. if( account.level )
  1138. ShowStatus("Connection of the GM (level:%d) account '%s' accepted.\n", account.level, account.userid);
  1139. else
  1140. ShowStatus("Connection of the account '%s' accepted.\n", account.userid);
  1141. WFIFOHEAD(fd,47+32*MAX_SERVERS);
  1142. for( i = 0; i < MAX_SERVERS; ++i )
  1143. {
  1144. if( session_isValid(server_fd[i]) )
  1145. {
  1146. // Advanced subnet check [LuzZza]
  1147. uint32 subnet_char_ip = lan_subnetcheck(ipl);
  1148. WFIFOL(fd,47+server_num*32) = htonl((subnet_char_ip) ? subnet_char_ip : server[i].ip);
  1149. WFIFOW(fd,47+server_num*32+4) = ntows(htons(server[i].port)); // [!] LE byte order here [!]
  1150. memcpy(WFIFOP(fd,47+server_num*32+6), server[i].name, 20);
  1151. WFIFOW(fd,47+server_num*32+26) = server[i].users;
  1152. WFIFOW(fd,47+server_num*32+28) = server[i].maintenance;
  1153. WFIFOW(fd,47+server_num*32+30) = server[i].new_;
  1154. server_num++;
  1155. }
  1156. }
  1157. if (server_num > 0) { // if at least 1 char-server
  1158. WFIFOW(fd,0) = 0x69;
  1159. WFIFOW(fd,2) = 47+32*server_num;
  1160. WFIFOL(fd,4) = account.login_id1;
  1161. WFIFOL(fd,8) = account.account_id;
  1162. WFIFOL(fd,12) = account.login_id2;
  1163. WFIFOL(fd,16) = 0; // in old version, that was for ip (not more used)
  1164. //memcpy(WFIFOP(fd,20), account.lastlogin, 24); // in old version, that was for name (not more used)
  1165. WFIFOB(fd,46) = account.sex;
  1166. WFIFOSET(fd,47+32*server_num);
  1167. if (auth_fifo_pos >= AUTH_FIFO_SIZE)
  1168. auth_fifo_pos = 0;
  1169. auth_fifo[auth_fifo_pos].account_id = account.account_id;
  1170. auth_fifo[auth_fifo_pos].login_id1 = account.login_id1;
  1171. auth_fifo[auth_fifo_pos].login_id2 = account.login_id2;
  1172. auth_fifo[auth_fifo_pos].sex = account.sex;
  1173. auth_fifo[auth_fifo_pos].delflag = 0;
  1174. auth_fifo[auth_fifo_pos].ip = session[fd]->client_addr;
  1175. auth_fifo_pos++;
  1176. } else { // if no char-server, don't send void list of servers, just disconnect the player with proper message
  1177. WFIFOW(fd,0) = 0x81;
  1178. WFIFOB(fd,2) = 1; // 01 = Server closed
  1179. WFIFOSET(fd,3);
  1180. }
  1181. }
  1182. }
  1183. else
  1184. { // auth failed
  1185. if (login_config.log_login)
  1186. {
  1187. const char* error;
  1188. switch( result ) {
  1189. case 0: error = "Unregistered ID."; break; // 0 = Unregistered ID
  1190. case 1: error = "Incorrect Password."; break; // 1 = Incorrect Password
  1191. case 2: error = "Account Expired."; break; // 2 = This ID is expired
  1192. case 3: error = "Rejected from server."; break; // 3 = Rejected from Server
  1193. case 4: error = "Blocked by GM."; break; // 4 = You have been blocked by the GM Team
  1194. case 5: error = "Not latest game EXE."; break; // 5 = Your Game's EXE file is not the latest version
  1195. case 6: error = "Banned."; break; // 6 = Your are Prohibited to log in until %s
  1196. case 7: error = "Server Over-population."; break; // 7 = Server is jammed due to over populated
  1197. case 8: error = "Account limit from company"; break; // 8 = No more accounts may be connected from this company
  1198. case 9: error = "Ban by DBA"; break; // 9 = MSI_REFUSE_BAN_BY_DBA
  1199. case 10: error = "Email not confirmed"; break; // 10 = MSI_REFUSE_EMAIL_NOT_CONFIRMED
  1200. case 11: error = "Ban by GM"; break; // 11 = MSI_REFUSE_BAN_BY_GM
  1201. case 12: error = "Working in DB"; break; // 12 = MSI_REFUSE_TEMP_BAN_FOR_DBWORK
  1202. case 13: error = "Self Lock"; break; // 13 = MSI_REFUSE_SELF_LOCK
  1203. case 14: error = "Not Permitted Group"; break; // 14 = MSI_REFUSE_NOT_PERMITTED_GROUP
  1204. case 15: error = "Not Permitted Group"; break; // 15 = MSI_REFUSE_NOT_PERMITTED_GROUP
  1205. case 99: error = "Account gone."; break; // 99 = This ID has been totally erased
  1206. case 100: error = "Login info remains."; break; // 100 = Login information remains at %s
  1207. case 101: error = "Hacking investigation."; break; // 101 = Account has been locked for a hacking investigation. Please contact the GM Team for more information
  1208. case 102: error = "Bug investigation."; break; // 102 = This account has been temporarily prohibited from login due to a bug-related investigation
  1209. case 103: error = "Deleting char."; break; // 103 = This character is being deleted. Login is temporarily unavailable for the time being
  1210. case 104: error = "Deleting spouse char."; break; // 104 = This character is being deleted. Login is temporarily unavailable for the time being
  1211. default : error = "Unknown Error."; break;
  1212. }
  1213. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT DELAYED INTO `%s`(`time`,`ip`,`user`,`rcode`,`log`) VALUES (NOW(), '%u', '%s', '%d','login failed : %s')", loginlog_db, ipl, esc_userid, result, error) )
  1214. Sql_ShowDebug(sql_handle);
  1215. }
  1216. if( result == 1 && login_config.dynamic_pass_failure_ban && login_config.log_login ) // failed password
  1217. {
  1218. unsigned long failures = 0;
  1219. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT count(*) FROM `%s` WHERE `ip` = '%u' AND `rcode` = '1' AND `time` > NOW() - INTERVAL %d MINUTE",
  1220. loginlog_db, ipl, login_config.dynamic_pass_failure_ban_interval) )// how many times failed account? in one ip.
  1221. Sql_ShowDebug(sql_handle);
  1222. //check query result
  1223. if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1224. {
  1225. char* data;
  1226. Sql_GetData(sql_handle, 0, &data, NULL);
  1227. failures = strtoul(data, NULL, 10);
  1228. Sql_FreeResult(sql_handle);
  1229. }
  1230. if( failures >= login_config.dynamic_pass_failure_ban_limit )
  1231. {
  1232. uint8* p = (uint8*)&ipl;
  1233. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `ipbanlist`(`list`,`btime`,`rtime`,`reason`) VALUES ('%u.%u.%u.*', NOW() , NOW() + INTERVAL %d MINUTE ,'Password error ban: %s')", p[3], p[2], p[1], login_config.dynamic_pass_failure_ban_duration, esc_userid) )
  1234. Sql_ShowDebug(sql_handle);
  1235. }
  1236. }
  1237. WFIFOHEAD(fd,23);
  1238. WFIFOW(fd,0) = 0x6a;
  1239. WFIFOB(fd,2) = (uint8)result;
  1240. if( result == 6 )
  1241. {// 6 = Your are Prohibited to log in until %s
  1242. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `ban_until` FROM `%s` WHERE `%s` = %s '%s'", login_db, login_db_userid, (login_config.case_sensitive ? "BINARY" : ""), esc_userid) )
  1243. Sql_ShowDebug(sql_handle);
  1244. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1245. {
  1246. char* data;
  1247. time_t ban_until_time;
  1248. Sql_GetData(sql_handle, 0, &data, NULL);
  1249. ban_until_time = (time_t)strtoul(data, NULL, 10);
  1250. Sql_FreeResult(sql_handle);
  1251. strftime(WFIFOP(fd,3), 20, login_config.date_format, localtime(&ban_until_time));
  1252. }
  1253. }
  1254. else
  1255. memset(WFIFOP(fd,3), '\0', 20);
  1256. WFIFOSET(fd,23);
  1257. }
  1258. RFIFOSKIP(fd,packet_len);
  1259. }
  1260. break;
  1261. case 0x01db: // Sending request of the coding key
  1262. {
  1263. struct login_session_data* ld;
  1264. if( session[fd]->session_data )
  1265. {
  1266. ShowWarning("login: abnormal request of MD5 key (already opened session).\n");
  1267. set_eof(fd);
  1268. return 0;
  1269. }
  1270. CREATE(ld, struct login_session_data, 1);
  1271. session[fd]->session_data = ld;
  1272. // Creation of the coding key
  1273. memset(ld->md5key, '\0', sizeof(ld->md5key));
  1274. ld->md5keylen = (uint16)(12 + rand() % 4);
  1275. for( i = 0; i < ld->md5keylen; ++i )
  1276. ld->md5key[i] = (char)(1 + rand() % 255);
  1277. WFIFOHEAD(fd,4 + ld->md5keylen);
  1278. WFIFOW(fd,0) = 0x01dc;
  1279. WFIFOW(fd,2) = 4 + ld->md5keylen;
  1280. memcpy(WFIFOP(fd,4), ld->md5key, ld->md5keylen);
  1281. WFIFOSET(fd,WFIFOW(fd,2));
  1282. RFIFOSKIP(fd,2);
  1283. }
  1284. break;
  1285. case 0x2710: // Connection request of a char-server
  1286. if (RFIFOREST(fd) < 86)
  1287. return 0;
  1288. {
  1289. char esc_server_name[20*2+1];
  1290. char* server_name;
  1291. uint32 server_ip;
  1292. uint16 server_port;
  1293. memset(&account, 0, sizeof(account));
  1294. safestrncpy(account.userid, RFIFOP(fd,2), NAME_LENGTH);
  1295. safestrncpy(account.passwd, RFIFOP(fd,26), NAME_LENGTH);
  1296. account.passwdenc = 0;
  1297. server_ip = ntohl(RFIFOL(fd,54));
  1298. server_port = ntohs(RFIFOW(fd,58));
  1299. server_name = (char*)RFIFOP(fd,60);
  1300. Sql_EscapeStringLen(sql_handle, esc_server_name, server_name, strnlen(server_name, 20));
  1301. Sql_EscapeStringLen(sql_handle, esc_userid, account.userid, strnlen(account.userid, NAME_LENGTH));
  1302. ShowInfo("Connection request of the char-server '%s' @ %d.%d.%d.%d:%d (account: '%s', pass: '%s', ip: '%s')\n", esc_server_name, CONVIP(server_ip), server_port, account.userid, account.passwd, ip);
  1303. if( login_config.log_login && SQL_ERROR == Sql_Query(sql_handle, "INSERT DELAYED INTO `%s`(`time`,`ip`,`user`,`rcode`,`log`) VALUES (NOW(), '%u', '%s@%s','100', 'charserver - %s@%u.%u.%u.%u:%d')",
  1304. loginlog_db, ipl, esc_userid, esc_server_name, esc_server_name, CONVIP(server_ip), server_port) )
  1305. Sql_ShowDebug(sql_handle);
  1306. result = mmo_auth(&account, fd);
  1307. if( result == -1 && account.sex == 2 && account.account_id < MAX_SERVERS && server_fd[account.account_id] == -1 )
  1308. {
  1309. ShowStatus("Connection of the char-server '%s' accepted.\n", esc_server_name);
  1310. memset(&server[account.account_id], 0, sizeof(struct mmo_char_server));
  1311. server[account.account_id].ip = ntohl(RFIFOL(fd,54));
  1312. server[account.account_id].port = ntohs(RFIFOW(fd,58));
  1313. safestrncpy(server[account.account_id].name, server_name, sizeof(server[account.account_id].name));
  1314. server[account.account_id].users = 0;
  1315. server[account.account_id].maintenance = RFIFOW(fd,82);
  1316. server[account.account_id].new_ = RFIFOW(fd,84);
  1317. server_fd[account.account_id] = fd;
  1318. WFIFOHEAD(fd,3);
  1319. WFIFOW(fd,0) = 0x2711;
  1320. WFIFOB(fd,2) = 0;
  1321. WFIFOSET(fd,3);
  1322. session[fd]->func_parse = parse_fromchar;
  1323. realloc_fifo(fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  1324. send_GM_accounts(fd); // send GM account to char-server
  1325. if( SQL_ERROR == Sql_Query(sql_handle, "REPLACE INTO `sstatus`(`index`,`name`,`user`) VALUES ( '%d', '%s', '%d')", account.account_id, esc_server_name, 0) )
  1326. Sql_ShowDebug(sql_handle);
  1327. }
  1328. else
  1329. {
  1330. ShowNotice("Connection of the char-server '%s' REFUSED.\n", esc_server_name);
  1331. WFIFOHEAD(fd,3);
  1332. WFIFOW(fd,0) = 0x2711;
  1333. WFIFOB(fd,2) = 3;
  1334. WFIFOSET(fd,3);
  1335. }
  1336. RFIFOSKIP(fd,86);
  1337. return 0;
  1338. }
  1339. case 0x7530: // Server version information request
  1340. {
  1341. ShowInfo ("Athena version check...\n");
  1342. WFIFOHEAD(fd,10);
  1343. WFIFOW(fd,0) = 0x7531;
  1344. WFIFOB(fd,2) = ATHENA_MAJOR_VERSION;
  1345. WFIFOB(fd,3) = ATHENA_MINOR_VERSION;
  1346. WFIFOB(fd,4) = ATHENA_REVISION;
  1347. WFIFOB(fd,5) = ATHENA_RELEASE_FLAG;
  1348. WFIFOB(fd,6) = ATHENA_OFFICIAL_FLAG;
  1349. WFIFOB(fd,7) = ATHENA_SERVER_LOGIN;
  1350. WFIFOW(fd,8) = ATHENA_MOD_VERSION;
  1351. WFIFOSET(fd,10);
  1352. RFIFOSKIP(fd,2);
  1353. }
  1354. break;
  1355. case 0x7532: // Request to end connection
  1356. ShowStatus("End of connection (ip: %s)\n", ip);
  1357. set_eof(fd);
  1358. break;
  1359. default:
  1360. ShowStatus("Abnormal end of connection (ip: %s): Unknown packet 0x%x\n", ip, RFIFOW(fd,0));
  1361. set_eof(fd);
  1362. return 0;
  1363. }
  1364. }
  1365. RFIFOSKIP(fd,RFIFOREST(fd));
  1366. return 0;
  1367. }
  1368. //-----------------------
  1369. // Console Command Parser [Wizputer]
  1370. //-----------------------
  1371. int parse_console(char* buf)
  1372. {
  1373. char command[256];
  1374. memset(command, 0, sizeof(command));
  1375. sscanf(buf, "%[^\n]", command);
  1376. ShowInfo("Console command :%s", command);
  1377. if( strcmpi("shutdown", command) == 0 ||
  1378. strcmpi("exit", command) == 0 ||
  1379. strcmpi("quit", command) == 0 ||
  1380. strcmpi("end", command) == 0 )
  1381. runflag = 0;
  1382. else
  1383. if( strcmpi("alive", command) == 0 ||
  1384. strcmpi("status", command) == 0 )
  1385. ShowInfo(CL_CYAN"Console: "CL_BOLD"I'm Alive."CL_RESET"\n");
  1386. else
  1387. if( strcmpi("help", command) == 0 ) {
  1388. printf(CL_BOLD"Help of commands:"CL_RESET"\n");
  1389. printf(" To shutdown the server:\n");
  1390. printf(" 'shutdown|exit|quit|end'\n");
  1391. printf(" To know if server is alive:\n");
  1392. printf(" 'alive|status'\n");
  1393. }
  1394. return 0;
  1395. }
  1396. static int online_data_cleanup_sub(DBKey key, void *data, va_list ap)
  1397. {
  1398. struct online_login_data *character= (struct online_login_data*)data;
  1399. if (character->char_server == -2) //Unknown server.. set them offline
  1400. remove_online_user(character->account_id);
  1401. else if (character->char_server < 0)
  1402. //Free data from players that have not been online for a while.
  1403. db_remove(online_db, key);
  1404. return 0;
  1405. }
  1406. static int online_data_cleanup(int tid, unsigned int tick, int id, int data)
  1407. {
  1408. online_db->foreach(online_db, online_data_cleanup_sub);
  1409. return 0;
  1410. }
  1411. //----------------------------------
  1412. // Reading Lan Support configuration
  1413. //----------------------------------
  1414. int login_lan_config_read(const char *lancfgName)
  1415. {
  1416. FILE *fp;
  1417. int line_num = 0;
  1418. char line[1024], w1[64], w2[64], w3[64], w4[64];
  1419. if((fp = fopen(lancfgName, "r")) == NULL) {
  1420. ShowWarning("LAN Support configuration file is not found: %s\n", lancfgName);
  1421. return 1;
  1422. }
  1423. ShowInfo("Reading the configuration file %s...\n", lancfgName);
  1424. while(fgets(line, sizeof(line), fp))
  1425. {
  1426. line_num++;
  1427. if ((line[0] == '/' && line[1] == '/') || line[0] == '\n' || line[1] == '\n')
  1428. continue;
  1429. if(sscanf(line,"%[^:]: %[^:]:%[^:]:%[^\r\n]", w1, w2, w3, w4) != 4)
  1430. {
  1431. ShowWarning("Error syntax of configuration file %s in line %d.\n", lancfgName, line_num);
  1432. continue;
  1433. }
  1434. remove_control_chars(w1);
  1435. remove_control_chars(w2);
  1436. remove_control_chars(w3);
  1437. remove_control_chars(w4);
  1438. if( strcmpi(w1, "subnet") == 0 )
  1439. {
  1440. subnet[subnet_count].mask = str2ip(w2);
  1441. subnet[subnet_count].char_ip = str2ip(w3);
  1442. subnet[subnet_count].map_ip = str2ip(w4);
  1443. if( (subnet[subnet_count].char_ip & subnet[subnet_count].mask) != (subnet[subnet_count].map_ip & subnet[subnet_count].mask) )
  1444. {
  1445. ShowError("%s: Configuration Error: The char server (%s) and map server (%s) belong to different subnetworks!\n", lancfgName, w3, w4);
  1446. continue;
  1447. }
  1448. subnet_count++;
  1449. }
  1450. ShowStatus("Read information about %d subnetworks.\n", subnet_count);
  1451. }
  1452. fclose(fp);
  1453. return 0;
  1454. }
  1455. //-----------------------------------------------------
  1456. // clear expired ip bans
  1457. //-----------------------------------------------------
  1458. int ip_ban_flush(int tid, unsigned int tick, int id, int data)
  1459. {
  1460. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `ipbanlist` WHERE `rtime` <= NOW()") )
  1461. Sql_ShowDebug(sql_handle);
  1462. return 0;
  1463. }
  1464. //-----------------------------------
  1465. // Reading main configuration file
  1466. //-----------------------------------
  1467. int login_config_read(const char* cfgName)
  1468. {
  1469. char line[1024], w1[1024], w2[1024];
  1470. FILE* fp = fopen(cfgName, "r");
  1471. if (fp == NULL) {
  1472. ShowError("Configuration file (%s) not found.\n", cfgName);
  1473. return 1;
  1474. }
  1475. ShowInfo("Reading configuration file %s...\n", cfgName);
  1476. while(fgets(line, sizeof(line), fp))
  1477. {
  1478. if (line[0] == '/' && line[1] == '/')
  1479. continue;
  1480. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) < 2)
  1481. continue;
  1482. remove_control_chars(w1);
  1483. remove_control_chars(w2);
  1484. if(!strcmpi(w1,"timestamp_format"))
  1485. strncpy(timestamp_format, w2, 20);
  1486. else if(!strcmpi(w1,"stdout_with_ansisequence"))
  1487. stdout_with_ansisequence = config_switch(w2);
  1488. else if(!strcmpi(w1,"console_silent")) {
  1489. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  1490. msg_silent = atoi(w2);
  1491. }
  1492. else if( !strcmpi(w1, "bind_ip") ) {
  1493. char ip_str[16];
  1494. login_config.login_ip = host2ip(w2);
  1495. if( login_config.login_ip )
  1496. ShowStatus("Login server binding IP address : %s -> %s\n", w2, ip2str(login_config.login_ip, ip_str));
  1497. }
  1498. else if( !strcmpi(w1, "login_port") ) {
  1499. login_config.login_port = (uint16)atoi(w2);
  1500. ShowStatus("set login_port : %s\n",w2);
  1501. }
  1502. else if(!strcmpi(w1, "log_login"))
  1503. login_config.log_login = config_switch(w2);
  1504. else if(!strcmpi(w1, "ipban"))
  1505. login_config.ipban = config_switch(w2);
  1506. else if(!strcmpi(w1, "dynamic_pass_failure_ban"))
  1507. login_config.dynamic_pass_failure_ban = config_switch(w2);
  1508. else if(!strcmpi(w1, "dynamic_pass_failure_ban_interval"))
  1509. login_config.dynamic_pass_failure_ban_interval = atoi(w2);
  1510. else if(!strcmpi(w1, "dynamic_pass_failure_ban_limit"))
  1511. login_config.dynamic_pass_failure_ban_limit = atoi(w2);
  1512. else if(!strcmpi(w1, "dynamic_pass_failure_ban_duration"))
  1513. login_config.dynamic_pass_failure_ban_duration = atoi(w2);
  1514. else if(!strcmpi(w1, "new_account"))
  1515. login_config.new_account_flag = (bool)config_switch(w2);
  1516. else if(!strcmpi(w1, "check_client_version"))
  1517. login_config.check_client_version = (bool)config_switch(w2);
  1518. else if(!strcmpi(w1, "client_version_to_connect"))
  1519. login_config.client_version_to_connect = (unsigned int)atoi(w2);
  1520. else if(!strcmpi(w1, "use_MD5_passwords"))
  1521. login_config.use_md5_passwds = (bool)config_switch(w2);
  1522. else if(!strcmpi(w1, "min_level_to_connect"))
  1523. login_config.min_level_to_connect = (uint8)atoi(w2);
  1524. else if(!strcmpi(w1, "date_format"))
  1525. safestrncpy(login_config.date_format, w2, sizeof(login_config.date_format));
  1526. else if(!strcmpi(w1, "console"))
  1527. login_config.console = config_switch(w2);
  1528. else if(!strcmpi(w1, "case_sensitive"))
  1529. login_config.case_sensitive = config_switch(w2);
  1530. else if(!strcmpi(w1, "allowed_regs")) //account flood protection system
  1531. allowed_regs = atoi(w2);
  1532. else if(!strcmpi(w1, "time_allowed"))
  1533. time_allowed = atoi(w2);
  1534. else if(!strcmpi(w1, "online_check"))
  1535. login_config.online_check = (bool)config_switch(w2);
  1536. else if(!strcmpi(w1, "use_dnsbl"))
  1537. login_config.use_dnsbl = (bool)config_switch(w2);
  1538. else if(!strcmpi(w1, "dnsbl_servers"))
  1539. safestrncpy(login_config.dnsbl_servs, w2, sizeof(login_config.dnsbl_servs));
  1540. else if(!strcmpi(w1, "ip_sync_interval"))
  1541. login_config.ip_sync_interval = (unsigned int)1000*60*atoi(w2); //w2 comes in minutes.
  1542. else if(!strcmpi(w1, "import"))
  1543. login_config_read(w2);
  1544. }
  1545. fclose(fp);
  1546. ShowInfo("Finished reading %s.\n", cfgName);
  1547. return 0;
  1548. }
  1549. void sql_config_read(const char* cfgName)
  1550. {
  1551. char line[1024], w1[1024], w2[1024];
  1552. FILE* fp = fopen(cfgName, "r");
  1553. if(fp == NULL) {
  1554. ShowError("file not found: %s\n", cfgName);
  1555. return;
  1556. }
  1557. ShowInfo("reading configuration file %s...\n", cfgName);
  1558. while(fgets(line, sizeof(line), fp))
  1559. {
  1560. if (line[0] == '/' && line[1] == '/')
  1561. continue;
  1562. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) < 2)
  1563. continue;
  1564. if (!strcmpi(w1, "gm_read_method"))
  1565. login_config.login_gm_read = (atoi(w2) == 0);
  1566. else if (!strcmpi(w1, "login_db"))
  1567. strcpy(login_db, w2);
  1568. else if (!strcmpi(w1, "login_server_ip"))
  1569. strcpy(login_server_ip, w2);
  1570. else if (!strcmpi(w1, "login_server_port"))
  1571. login_server_port = (uint16)atoi(w2);
  1572. else if (!strcmpi(w1, "login_server_id"))
  1573. strcpy(login_server_id, w2);
  1574. else if (!strcmpi(w1, "login_server_pw"))
  1575. strcpy(login_server_pw, w2);
  1576. else if (!strcmpi(w1, "login_server_db"))
  1577. strcpy(login_server_db, w2);
  1578. else if (!strcmpi(w1, "default_codepage"))
  1579. strcpy(default_codepage, w2);
  1580. else if (!strcmpi(w1, "login_db_account_id"))
  1581. strcpy(login_db_account_id, w2);
  1582. else if (!strcmpi(w1, "login_db_userid"))
  1583. strcpy(login_db_userid, w2);
  1584. else if (!strcmpi(w1, "login_db_user_pass"))
  1585. strcpy(login_db_user_pass, w2);
  1586. else if (!strcmpi(w1, "login_db_level"))
  1587. strcpy(login_db_level, w2);
  1588. else if (!strcmpi(w1, "loginlog_db"))
  1589. strcpy(loginlog_db, w2);
  1590. else if (!strcmpi(w1, "reg_db"))
  1591. strcpy(reg_db, w2);
  1592. else if (!strcmpi(w1, "import"))
  1593. sql_config_read(w2);
  1594. }
  1595. fclose(fp);
  1596. ShowInfo("Done reading %s.\n", cfgName);
  1597. }
  1598. void login_set_defaults()
  1599. {
  1600. login_config.login_ip = INADDR_ANY;
  1601. login_config.login_port = 6900;
  1602. login_config.ip_sync_interval = 0;
  1603. login_config.log_login = true;
  1604. safestrncpy(login_config.date_format, "%Y-%m-%d %H:%M:%S", sizeof(login_config.date_format));
  1605. login_config.console = false;
  1606. login_config.new_account_flag = true;
  1607. login_config.case_sensitive = true;
  1608. login_config.use_md5_passwds = false;
  1609. login_config.login_gm_read = true;
  1610. login_config.min_level_to_connect = 0;
  1611. login_config.online_check = true;
  1612. login_config.check_client_version = false;
  1613. login_config.client_version_to_connect = 20;
  1614. login_config.ipban = true;
  1615. login_config.dynamic_pass_failure_ban = true;
  1616. login_config.dynamic_pass_failure_ban_interval = 5;
  1617. login_config.dynamic_pass_failure_ban_limit = 7;
  1618. login_config.dynamic_pass_failure_ban_duration = 5;
  1619. login_config.use_dnsbl = false;
  1620. safestrncpy(login_config.dnsbl_servs, "", sizeof(login_config.dnsbl_servs));
  1621. }
  1622. //--------------------------------------
  1623. // Function called at exit of the server
  1624. //--------------------------------------
  1625. void do_final(void)
  1626. {
  1627. //sync account when terminating.
  1628. //but no need when you using DBMS (mysql)
  1629. ShowStatus("Terminating...\n");
  1630. mmo_db_close();
  1631. online_db->destroy(online_db, NULL);
  1632. if (gm_account_db)
  1633. aFree(gm_account_db);
  1634. }
  1635. //------------------------------
  1636. // Function called when the server
  1637. // has received a crash signal.
  1638. //------------------------------
  1639. void do_abort(void)
  1640. {
  1641. }
  1642. void set_server_type(void)
  1643. {
  1644. SERVER_TYPE = ATHENA_SERVER_LOGIN;
  1645. }
  1646. //------------------------------
  1647. // Login server initialization
  1648. //------------------------------
  1649. int do_init(int argc, char** argv)
  1650. {
  1651. int i;
  1652. login_set_defaults();
  1653. // read login-server configuration
  1654. login_config_read((argc > 1) ? argv[1] : LOGIN_CONF_NAME);
  1655. sql_config_read(SQL_CONF_NAME);
  1656. login_lan_config_read((argc > 2) ? argv[2] : LAN_CONF_NAME);
  1657. srand((unsigned int)time(NULL));
  1658. for( i = 0; i < AUTH_FIFO_SIZE; i++ )
  1659. auth_fifo[i].delflag = 1;
  1660. for( i = 0; i < MAX_SERVERS; i++ )
  1661. server_fd[i] = -1;
  1662. // Online user database init
  1663. online_db = db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
  1664. add_timer_func_list(waiting_disconnect_timer, "waiting_disconnect_timer");
  1665. // Auth init
  1666. mmo_auth_sqldb_init();
  1667. // Read account information.
  1668. if(login_config.login_gm_read)
  1669. read_gm_account();
  1670. // set default parser as parse_login function
  1671. set_defaultparse(parse_login);
  1672. // ban deleter timer
  1673. add_timer_func_list(ip_ban_flush, "ip_ban_flush");
  1674. add_timer_interval(gettick()+10, ip_ban_flush, 0, 0, 60*1000);
  1675. // every 10 minutes cleanup online account db.
  1676. add_timer_func_list(online_data_cleanup, "online_data_cleanup");
  1677. add_timer_interval(gettick() + 600*1000, online_data_cleanup, 0, 0, 600*1000);
  1678. // add timer to detect ip address change and perform update
  1679. if (login_config.ip_sync_interval) {
  1680. add_timer_func_list(sync_ip_addresses, "sync_ip_addresses");
  1681. add_timer_interval(gettick() + login_config.ip_sync_interval, sync_ip_addresses, 0, 0, login_config.ip_sync_interval);
  1682. }
  1683. if( login_config.console )
  1684. {
  1685. //##TODO invoke a CONSOLE_START plugin event
  1686. }
  1687. new_reg_tick = gettick();
  1688. // server port open & binding
  1689. login_fd = make_listen_bind(login_config.login_ip, login_config.login_port);
  1690. ShowStatus("The login-server is "CL_GREEN"ready"CL_RESET" (Server is listening on the port %u).\n\n", login_config.login_port);
  1691. return 0;
  1692. }