login.c 78 KB

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