login.c 68 KB

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