login.c 78 KB

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