login.c 62 KB

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