login.c 76 KB

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