login.c 77 KB

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