loginchrif.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "loginchrif.hpp"
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <common/showmsg.hpp> //show notice
  7. #include <common/socket.hpp> //wfifo session
  8. #include <common/strlib.hpp> //safeprint
  9. #include <common/timer.hpp> //difftick
  10. #include "account.hpp"
  11. #include "login.hpp"
  12. #include "loginlog.hpp"
  13. //early declaration
  14. void logchrif_on_disconnect(int32 id);
  15. /**
  16. * Packet send to all char-servers, except one. (wos: without our self)
  17. * @param sfd: fd to discard sending to
  18. * @param buf: packet to send in form of an array buffer
  19. * @param len: size of packet
  20. * @return : the number of char-serv the packet was sent to
  21. */
  22. int32 logchrif_sendallwos(int32 sfd, uint8* buf, size_t len) {
  23. int32 i, c;
  24. for( i = 0, c = 0; i < ARRAYLENGTH(ch_server); ++i ) {
  25. int32 fd = ch_server[i].fd;
  26. if( session_isValid(fd) && fd != sfd ){
  27. WFIFOHEAD(fd,len);
  28. memcpy(WFIFOP(fd,0), buf, len);
  29. WFIFOSET(fd,len);
  30. ++c;
  31. }
  32. }
  33. return c;
  34. }
  35. /**
  36. * Timered function to synchronize ip addresses.
  37. * Requesting all char to update their registered ip and transmit their new ip.
  38. * Performed each ip_sync_interval.
  39. * @param tid: timer id
  40. * @param tick: tick of execution
  41. * @param id: unused
  42. * @param data: unused
  43. * @return 0
  44. */
  45. TIMER_FUNC(logchrif_sync_ip_addresses){
  46. uint8 buf[2];
  47. ShowInfo("IP Sync in progress...\n");
  48. WBUFW(buf,0) = 0x2735;
  49. logchrif_sendallwos(-1, buf, 2);
  50. return 0;
  51. }
  52. /// Parsing handlers
  53. /**
  54. * Request from char-server to authenticate an account.
  55. * @param fd: fd to parse from (char-serv)
  56. * @param id: id of char-serv
  57. * @param ip: char-serv ip (used for info)
  58. * @return 0 not enough info transmitted, 1 success
  59. */
  60. int32 logchrif_parse_reqauth(int32 fd, int32 id,char* ip){
  61. if( RFIFOREST(fd) < 23 )
  62. return 0;
  63. else{
  64. uint32 account_id = RFIFOL(fd,2);
  65. uint32 login_id1 = RFIFOL(fd,6);
  66. uint32 login_id2 = RFIFOL(fd,10);
  67. uint8 sex = RFIFOB(fd,14);
  68. //uint32 ip_ = ntohl(RFIFOL(fd,15));
  69. int32 request_id = RFIFOL(fd,19);
  70. RFIFOSKIP(fd,23);
  71. struct auth_node* node = login_get_auth_node( account_id );
  72. if( global_core->is_running() &&
  73. node != nullptr &&
  74. node->account_id == account_id &&
  75. node->login_id1 == login_id1 &&
  76. node->login_id2 == login_id2 &&
  77. node->sex == sex_num2str(sex) /*&&
  78. node->ip == ip_*/ ){// found
  79. //ShowStatus("Char-server '%s': authentication of the account %d accepted (ip: %s).\n", server[id].name, account_id, ip);
  80. // send ack
  81. WFIFOHEAD(fd,21);
  82. WFIFOW(fd,0) = 0x2713;
  83. WFIFOL(fd,2) = account_id;
  84. WFIFOL(fd,6) = login_id1;
  85. WFIFOL(fd,10) = login_id2;
  86. WFIFOB(fd,14) = sex;
  87. WFIFOB(fd,15) = 0;// ok
  88. WFIFOL(fd,16) = request_id;
  89. WFIFOB(fd,20) = node->clienttype;
  90. WFIFOSET(fd,21);
  91. // each auth entry can only be used once
  92. login_remove_auth_node( account_id );
  93. }else{// authentication not found
  94. ShowStatus("Char-server '%s': authentication of the account %d REFUSED (ip: %s).\n", ch_server[id].name, account_id, ip);
  95. WFIFOHEAD(fd,21);
  96. WFIFOW(fd,0) = 0x2713;
  97. WFIFOL(fd,2) = account_id;
  98. WFIFOL(fd,6) = login_id1;
  99. WFIFOL(fd,10) = login_id2;
  100. WFIFOB(fd,14) = sex;
  101. WFIFOB(fd,15) = 1;// auth failed
  102. WFIFOL(fd,16) = request_id;
  103. WFIFOB(fd,20) = 0;
  104. WFIFOSET(fd,21);
  105. }
  106. }
  107. return 1;
  108. }
  109. /**
  110. * Receive a request to update user count for char-server identified by id.
  111. * @param fd: fd to parse from (char-serv)
  112. * @param id: id of char-serv
  113. * @return 0 not enough info transmitted, 1 success
  114. */
  115. int32 logchrif_parse_ackusercount(int32 fd, int32 id){
  116. if( RFIFOREST(fd) < 6 )
  117. return 0;
  118. else{
  119. int32 users = RFIFOL(fd,2);
  120. RFIFOSKIP(fd,6);
  121. // how many users on world? (update)
  122. if( ch_server[id].users != users ){
  123. ShowStatus("set users %s : %d\n", ch_server[id].name, users);
  124. ch_server[id].users = users;
  125. }
  126. }
  127. return 1;
  128. }
  129. /**
  130. * Transmit account data to char_server
  131. * S 2717 aid.W email.40B exp_time.L group_id.B char_slot.B birthdate.11B pincode.5B pincode_change.L
  132. * isvip.1B char_vip.1B max_billing.1B (tot 75)
  133. * @return -1 : account not found, 1:sucess
  134. */
  135. int32 logchrif_send_accdata(int32 fd, uint32 aid) {
  136. struct mmo_account acc;
  137. time_t expiration_time = 0;
  138. char email[40] = "";
  139. int32 group_id = 0;
  140. char birthdate[10+1] = "";
  141. char pincode[PINCODE_LENGTH+1];
  142. char isvip = false;
  143. uint8 char_slots = MIN_CHARS, char_vip = 0, char_billing = MAX_CHAR_BILLING;
  144. AccountDB* accounts = login_get_accounts_db();
  145. memset(pincode,0,PINCODE_LENGTH+1);
  146. if( !accounts->load_num(accounts, &acc, aid) )
  147. return -1;
  148. else {
  149. safestrncpy(email, acc.email, sizeof(email));
  150. expiration_time = acc.expiration_time;
  151. group_id = acc.group_id;
  152. safestrncpy(birthdate, acc.birthdate, sizeof(birthdate));
  153. safestrncpy(pincode, acc.pincode, sizeof(pincode));
  154. char_slots = login_config.char_per_account;
  155. // Respect account information from login-server
  156. if( acc.char_slots > 0 ){
  157. char_slots = acc.char_slots;
  158. }
  159. #ifdef VIP_ENABLE
  160. char_vip = login_config.vip_sys.char_increase;
  161. if( acc.vip_time > time(nullptr) ) {
  162. isvip = true;
  163. char_slots += char_vip;
  164. }
  165. #endif
  166. }
  167. WFIFOHEAD(fd,75);
  168. WFIFOW(fd,0) = 0x2717;
  169. WFIFOL(fd,2) = aid;
  170. safestrncpy(WFIFOCP(fd,6), email, 40);
  171. WFIFOL(fd,46) = (uint32)expiration_time;
  172. WFIFOB(fd,50) = (unsigned char)group_id;
  173. WFIFOB(fd,51) = char_slots;
  174. safestrncpy(WFIFOCP(fd,52), birthdate, 10+1);
  175. safestrncpy(WFIFOCP(fd,63), pincode, 4+1 );
  176. WFIFOL(fd,68) = (uint32)acc.pincode_change;
  177. WFIFOB(fd,72) = isvip;
  178. WFIFOB(fd,73) = char_vip;
  179. WFIFOB(fd,74) = char_billing;
  180. WFIFOSET(fd,75);
  181. return 1;
  182. }
  183. /**
  184. * Transmit vip specific data to char-serv (will be transfered to mapserv)
  185. * @param fd
  186. * @param acc
  187. * @param flag 0x1: VIP, 0x2: GM, 0x4: Show rates on player
  188. * @param mapfd
  189. */
  190. int32 logchrif_sendvipdata(int32 fd, struct mmo_account* acc, unsigned char flag, int32 mapfd) {
  191. #ifdef VIP_ENABLE
  192. WFIFOHEAD(fd,19);
  193. WFIFOW(fd,0) = 0x2743;
  194. WFIFOL(fd,2) = acc->account_id;
  195. WFIFOL(fd,6) = (int32)acc->vip_time;
  196. WFIFOB(fd,10) = flag;
  197. WFIFOL(fd,11) = acc->group_id; //new group id
  198. WFIFOL(fd,15) = mapfd; //link to mapserv
  199. WFIFOSET(fd,19);
  200. logchrif_send_accdata(fd,acc->account_id); //refresh char with new setting
  201. #endif
  202. return 1;
  203. }
  204. /**
  205. * Receive a request for account data reply by sending all mmo_account information.
  206. * @param fd: fd to parse from (char-serv)
  207. * @param id: id of char-serv
  208. * @param ip: char-serv ip (used for info)
  209. * @return 0 not enough info transmitted, 1 success
  210. */
  211. int32 logchrif_parse_reqaccdata(int32 fd, int32 id, char *ip){
  212. if( RFIFOREST(fd) < 6 )
  213. return 0;
  214. else {
  215. uint32 aid = RFIFOL(fd,2);
  216. RFIFOSKIP(fd,6);
  217. if( logchrif_send_accdata(fd,aid) < 0 )
  218. ShowNotice("Char-server '%s': account %d NOT found (ip: %s).\n", ch_server[id].name, aid, ip);
  219. }
  220. return 1;
  221. }
  222. /**
  223. * Ping request from char-server to send a reply.
  224. * @param fd: fd to parse from (char-serv)
  225. * @return 1 success
  226. */
  227. int32 logchrif_parse_keepalive(int32 fd){
  228. RFIFOSKIP(fd,2);
  229. WFIFOHEAD(fd,2);
  230. WFIFOW(fd,0) = 0x2718;
  231. WFIFOSET(fd,2);
  232. return 1;
  233. }
  234. /**
  235. * Map server send information to change an email of an account via char-server.
  236. * 0x2722 <account_id>.L <actual_e-mail>.40B <new_e-mail>.40B
  237. * @param fd: fd to parse from (char-serv)
  238. * @param id: id of char-serv
  239. * @param ip: char-serv ip (used for info)
  240. * @return 0 not enough info transmitted, 1 success
  241. */
  242. int32 logchrif_parse_reqchangemail(int32 fd, int32 id, char* ip){
  243. if (RFIFOREST(fd) < 86)
  244. return 0;
  245. else{
  246. struct mmo_account acc;
  247. AccountDB* accounts = login_get_accounts_db();
  248. char actual_email[40];
  249. char new_email[40];
  250. uint32 account_id = RFIFOL(fd,2);
  251. safestrncpy(actual_email, RFIFOCP(fd,6), 40);
  252. safestrncpy(new_email, RFIFOCP(fd,46), 40);
  253. RFIFOSKIP(fd, 86);
  254. if( e_mail_check(actual_email) == 0 )
  255. ShowNotice("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command), but actual email is invalid (account: %d, ip: %s)\n", ch_server[id].name, account_id, ip);
  256. else if( e_mail_check(new_email) == 0 )
  257. ShowNotice("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command) with a invalid new e-mail (account: %d, ip: %s)\n", ch_server[id].name, account_id, ip);
  258. else if( strcmpi(new_email, "a@a.com") == 0 )
  259. ShowNotice("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command) with a default e-mail (account: %d, ip: %s)\n", ch_server[id].name, account_id, ip);
  260. else if( !accounts->load_num(accounts, &acc, account_id) )
  261. ShowNotice("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command), but account doesn't exist (account: %d, ip: %s).\n", ch_server[id].name, account_id, ip);
  262. else if( strcmpi(acc.email, actual_email) != 0 )
  263. ShowNotice("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command), but actual e-mail is incorrect (account: %d (%s), actual e-mail: %s, proposed e-mail: %s, ip: %s).\n", ch_server[id].name, account_id, acc.userid, acc.email, actual_email, ip);
  264. else{
  265. safestrncpy(acc.email, new_email, 40);
  266. ShowNotice("Char-server '%s': Modify an e-mail on an account (@email GM command) (account: %d (%s), new e-mail: %s, ip: %s).\n", ch_server[id].name, account_id, acc.userid, new_email, ip);
  267. // Save
  268. accounts->save(accounts, &acc, false);
  269. }
  270. }
  271. return 1;
  272. }
  273. /**
  274. * Receiving an account state update request from a map-server (relayed via char-server).
  275. * @param fd: fd to parse from (char-serv)
  276. * @param id: id of char-serv
  277. * @param ip: char-serv ip (used for info)
  278. * @return 0 not enough info transmitted, 1 success
  279. * TODO seems pretty damn close to logchrif_parse_reqbanacc
  280. */
  281. int32 logchrif_parse_requpdaccstate(int32 fd, int32 id, char* ip){
  282. if (RFIFOREST(fd) < 10)
  283. return 0;
  284. else{
  285. struct mmo_account acc;
  286. uint32 account_id = RFIFOL(fd,2);
  287. uint32 state = RFIFOL(fd,6);
  288. AccountDB* accounts = login_get_accounts_db();
  289. RFIFOSKIP(fd,10);
  290. if( !accounts->load_num(accounts, &acc, account_id) )
  291. ShowNotice("Char-server '%s': Error of Status change (account: %d not found, suggested status %d, ip: %s).\n", ch_server[id].name, account_id, state, ip);
  292. else if( acc.state == state )
  293. ShowNotice("Char-server '%s': Error of Status change - actual status is already the good status (account: %d, status %d, ip: %s).\n", ch_server[id].name, account_id, state, ip);
  294. else{
  295. ShowNotice("Char-server '%s': Status change (account: %d, new status %d, ip: %s).\n", ch_server[id].name, account_id, state, ip);
  296. acc.state = state;
  297. // Save
  298. accounts->save(accounts, &acc, false);
  299. // notify other servers
  300. if (state != 0){
  301. uint8 buf[11];
  302. WBUFW(buf,0) = 0x2731;
  303. WBUFL(buf,2) = account_id;
  304. WBUFB(buf,6) = 0; // 0: change of state, 1: ban
  305. WBUFL(buf,7) = state; // status or final date of a banishment
  306. logchrif_sendallwos(-1, buf, 11);
  307. }
  308. }
  309. }
  310. return 1;
  311. }
  312. /**
  313. * Receiving a ban request from map-server via char-server.
  314. * @param fd: fd to parse from (char-serv)
  315. * @param id: id of char-serv
  316. * @param ip: char-serv ip (used for info)
  317. * @return 0 not enough info transmitted, 1 success
  318. * TODO check logchrif_parse_requpdaccstate for possible merge
  319. */
  320. int32 logchrif_parse_reqbanacc(int32 fd, int32 id, char* ip){
  321. if (RFIFOREST(fd) < 10)
  322. return 0;
  323. else{
  324. struct mmo_account acc;
  325. AccountDB* accounts = login_get_accounts_db();
  326. uint32 account_id = RFIFOL(fd,2);
  327. int32 timediff = RFIFOL(fd,6);
  328. RFIFOSKIP(fd,10);
  329. if( !accounts->load_num(accounts, &acc, account_id) )
  330. ShowNotice("Char-server '%s': Error of ban request (account: %d not found, ip: %s).\n", ch_server[id].name, account_id, ip);
  331. else{
  332. time_t timestamp;
  333. if (acc.unban_time == 0 || acc.unban_time < time(nullptr))
  334. timestamp = time(nullptr); // new ban
  335. else
  336. timestamp = acc.unban_time; // add to existing ban
  337. timestamp += timediff;
  338. if (timestamp == -1)
  339. ShowNotice("Char-server '%s': Error of ban request (account: %d, invalid date, ip: %s).\n", ch_server[id].name, account_id, ip);
  340. else if( timestamp <= time(nullptr) || timestamp == 0 )
  341. ShowNotice("Char-server '%s': Error of ban request (account: %d, new date unbans the account, ip: %s).\n", ch_server[id].name, account_id, ip);
  342. else{
  343. uint8 buf[11];
  344. char tmpstr[24];
  345. timestamp2string(tmpstr, sizeof(tmpstr), timestamp, login_config.date_format);
  346. ShowNotice("Char-server '%s': Ban request (account: %d, new final date of banishment: %s, ip: %s).\n", ch_server[id].name, account_id, tmpstr, ip);
  347. acc.unban_time = timestamp;
  348. // Save
  349. accounts->save(accounts, &acc, false);
  350. WBUFW(buf,0) = 0x2731;
  351. WBUFL(buf,2) = account_id;
  352. WBUFB(buf,6) = 1; // 0: change of status, 1: ban
  353. WBUFL(buf,7) = (uint32)timestamp; // status or final date of a banishment
  354. logchrif_sendallwos(-1, buf, 11);
  355. }
  356. }
  357. }
  358. return 1;
  359. }
  360. /**
  361. * Receiving a sex change request (sex is reversed).
  362. * @param fd: fd to parse from (char-serv)
  363. * @param id: id of char-serv
  364. * @param ip: char-serv ip (used for info)
  365. * @return 0 not enough info transmitted, 1 success
  366. */
  367. int32 logchrif_parse_reqchgsex(int32 fd, int32 id, char* ip){
  368. if( RFIFOREST(fd) < 6 )
  369. return 0;
  370. else{
  371. struct mmo_account acc;
  372. AccountDB* accounts = login_get_accounts_db();
  373. uint32 account_id = RFIFOL(fd,2);
  374. RFIFOSKIP(fd,6);
  375. if( !accounts->load_num(accounts, &acc, account_id) )
  376. ShowNotice("Char-server '%s': Error of sex change (account: %d not found, ip: %s).\n", ch_server[id].name, account_id, ip);
  377. else if( acc.sex == 'S' )
  378. ShowNotice("Char-server '%s': Error of sex change - account to change is a Server account (account: %d, ip: %s).\n", ch_server[id].name, account_id, ip);
  379. else{
  380. unsigned char buf[7];
  381. char sex = ( acc.sex == 'M' ) ? 'F' : 'M'; //Change gender
  382. ShowNotice("Char-server '%s': Sex change (account: %d, new sex %c, ip: %s).\n", ch_server[id].name, account_id, sex, ip);
  383. acc.sex = sex;
  384. // Save
  385. accounts->save(accounts, &acc, false);
  386. // announce to other servers
  387. WBUFW(buf,0) = 0x2723;
  388. WBUFL(buf,2) = account_id;
  389. WBUFB(buf,6) = sex_str2num(sex);
  390. logchrif_sendallwos(-1, buf, 7);
  391. }
  392. }
  393. return 1;
  394. }
  395. /**
  396. * We receive account_reg2 from a char-server, and we send them to other char-servers.
  397. * @param fd: fd to parse from (char-serv)
  398. * @param id: id of char-serv
  399. * @param ip: char-serv ip (used for info)
  400. * @return 0 not enough info transmitted, 1 success
  401. */
  402. int32 logchrif_parse_upd_global_accreg(int32 fd, int32 id, char* ip){
  403. if( RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2) )
  404. return 0;
  405. else{
  406. struct mmo_account acc;
  407. AccountDB* accounts = login_get_accounts_db();
  408. uint32 account_id = RFIFOL(fd,4);
  409. if( !accounts->load_num(accounts, &acc, account_id) )
  410. ShowStatus("Char-server '%s': receiving (from the char-server) of account_reg2 (account: %d not found, ip: %s).\n", ch_server[id].name, account_id, ip);
  411. else
  412. mmo_save_global_accreg(accounts,fd,account_id,RFIFOL(fd, 8));
  413. RFIFOSKIP(fd,RFIFOW(fd,2));
  414. }
  415. return 1;
  416. }
  417. /**
  418. * Receiving an unban request from map-server via char-server.
  419. * @param fd: fd to parse from (char-serv)
  420. * @param id: id of char-serv
  421. * @param ip: char-serv ip (used for info)
  422. * @return 0 not enough info transmitted, 1 success
  423. */
  424. int32 logchrif_parse_requnbanacc(int32 fd, int32 id, char* ip){
  425. if( RFIFOREST(fd) < 6 )
  426. return 0;
  427. else{
  428. struct mmo_account acc;
  429. AccountDB* accounts = login_get_accounts_db();
  430. uint32 account_id = RFIFOL(fd,2);
  431. RFIFOSKIP(fd,6);
  432. if( !accounts->load_num(accounts, &acc, account_id) )
  433. ShowNotice("Char-server '%s': Error of UnBan request (account: %d not found, ip: %s).\n", ch_server[id].name, account_id, ip);
  434. else if( acc.unban_time == 0 )
  435. ShowNotice("Char-server '%s': Error of UnBan request (account: %d, no change for unban date, ip: %s).\n", ch_server[id].name, account_id, ip);
  436. else{
  437. ShowNotice("Char-server '%s': UnBan request (account: %d, ip: %s).\n", ch_server[id].name, account_id, ip);
  438. acc.unban_time = 0;
  439. accounts->save(accounts, &acc, false);
  440. }
  441. }
  442. return 1;
  443. }
  444. /**
  445. * Set account_id to online.
  446. * @author [Wizputer]
  447. * @param fd: fd to parse from (char-serv)
  448. * @param id: id of char-serv
  449. * @return 0 not enough info transmitted, 1 success
  450. */
  451. int32 logchrif_parse_setacconline(int32 fd, int32 id){
  452. if( RFIFOREST(fd) < 6 )
  453. return 0;
  454. login_add_online_user(id, RFIFOL(fd,2));
  455. RFIFOSKIP(fd,6);
  456. return 1;
  457. }
  458. /**
  459. * Set account_id to offline.
  460. * @author [Wizputer]
  461. * @param fd: fd to parse from (char-serv)
  462. * @return 0 not enough info transmitted, 1 success
  463. */
  464. int32 logchrif_parse_setaccoffline(int32 fd){
  465. if( RFIFOREST(fd) < 6 )
  466. return 0;
  467. login_remove_online_user(RFIFOL(fd,2));
  468. RFIFOSKIP(fd,6);
  469. return 1;
  470. }
  471. /**
  472. * Receive list of all online accounts.
  473. * @author [Skotlex]
  474. * @param fd: fd to parse from (char-serv)
  475. * @param id: id of char-serv
  476. * @return 0 not enough info transmitted, 1 success
  477. */
  478. int32 logchrif_parse_updonlinedb(int32 fd, int32 id){
  479. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  480. return 0;
  481. else{
  482. //Set all chars from this char-server offline first
  483. login_online_db_setoffline( id );
  484. for( uint32 i = 0, users = RFIFOW(fd, 4); i < users; i++) {
  485. uint32 aid = RFIFOL(fd,6+i*4);
  486. login_add_online_user( id, aid );
  487. }
  488. RFIFOSKIP(fd,RFIFOW(fd,2));
  489. }
  490. return 1;
  491. }
  492. /**
  493. * Request account_reg2 for a character.
  494. * @param fd: fd to parse from (char-serv)
  495. * @return 0 not enough info transmitted, 1 success
  496. */
  497. int32 logchrif_parse_req_global_accreg(int32 fd){
  498. if (RFIFOREST(fd) < 10)
  499. return 0;
  500. else{
  501. AccountDB* accounts = login_get_accounts_db();
  502. uint32 account_id = RFIFOL(fd,2);
  503. uint32 char_id = RFIFOL(fd,6);
  504. RFIFOSKIP(fd,10);
  505. mmo_send_global_accreg(accounts,fd,account_id,char_id);
  506. }
  507. return 1;
  508. }
  509. /**
  510. * Received new charip from char-serv, update information.
  511. * @param fd: char-serv file descriptor
  512. * @param id: char-serv id
  513. * @return 0 not enough info transmitted, 1 success
  514. */
  515. int32 logchrif_parse_updcharip(int32 fd, int32 id){
  516. if( RFIFOREST(fd) < 6 )
  517. return 0;
  518. ch_server[id].ip = ntohl(RFIFOL(fd,2));
  519. ShowInfo("Updated IP of Server #%d to %d.%d.%d.%d.\n",id, CONVIP(ch_server[id].ip));
  520. RFIFOSKIP(fd,6);
  521. return 1;
  522. }
  523. /**
  524. * Request to set all accounts offline.
  525. * @param fd: fd to parse from (char-serv)
  526. * @param id: id of char-serv (char-serv)
  527. * @return 1 success
  528. */
  529. int32 logchrif_parse_setalloffline(int32 fd, int32 id){
  530. ShowInfo("Setting accounts from char-server %d offline.\n", id);
  531. login_online_db_setoffline( id );
  532. RFIFOSKIP(fd,2);
  533. return 1;
  534. }
  535. #if PACKETVER_SUPPORTS_PINCODE
  536. /**
  537. * Request to change PIN Code for an account.
  538. * @param fd: fd to parse from (char-serv)
  539. * @return 0 fail (packet does not have enough data), 1 success
  540. */
  541. int32 logchrif_parse_updpincode(int32 fd){
  542. if( RFIFOREST(fd) < 8 + PINCODE_LENGTH+1 )
  543. return 0;
  544. else{
  545. struct mmo_account acc;
  546. AccountDB* accounts = login_get_accounts_db();
  547. if( accounts->load_num(accounts, &acc, RFIFOL(fd,4) ) ){
  548. strncpy( acc.pincode, RFIFOCP(fd,8), PINCODE_LENGTH+1 );
  549. acc.pincode_change = time( nullptr );
  550. accounts->save(accounts, &acc, false);
  551. }
  552. RFIFOSKIP(fd,8 + PINCODE_LENGTH+1);
  553. }
  554. return 1;
  555. }
  556. /**
  557. * PIN Code was incorrectly entered too many times.
  558. * @param fd: fd to parse from (char-serv)
  559. * @return 0 fail (packet does not have enough data), 1 success (continue parsing)
  560. */
  561. int32 logchrif_parse_pincode_authfail(int32 fd){
  562. if( RFIFOREST(fd) < 6 )
  563. return 0;
  564. else{
  565. struct mmo_account acc;
  566. AccountDB* accounts = login_get_accounts_db();
  567. if( accounts->load_num(accounts, &acc, RFIFOL(fd,2) ) ){
  568. struct online_login_data* ld = login_get_online_user( acc.account_id );
  569. if( ld == nullptr ){
  570. return 0;
  571. }
  572. login_log( host2ip(acc.last_ip), acc.userid, 100, "PIN Code check failed" );
  573. }
  574. login_remove_online_user(acc.account_id);
  575. RFIFOSKIP(fd,6);
  576. }
  577. return 1;
  578. }
  579. #endif
  580. /**
  581. * Received a vip data reqest from char
  582. * type is the query to perform
  583. * 0x1 : Select info and update old_groupid
  584. * 0x2 : VIP duration is changed by atcommand or script
  585. * 0x8 : First request on player login
  586. * @param fd link to charserv
  587. * @return 0 missing data, 1 succeeded
  588. */
  589. int32 logchrif_parse_reqvipdata(int32 fd) {
  590. #ifdef VIP_ENABLE
  591. if( RFIFOREST(fd) < 15 )
  592. return 0;
  593. else { //request vip info
  594. struct mmo_account acc;
  595. AccountDB* accounts = login_get_accounts_db();
  596. int32 aid = RFIFOL(fd,2);
  597. int8 flag = RFIFOB(fd,6);
  598. int32 timediff = RFIFOL(fd,7);
  599. int32 mapfd = RFIFOL(fd,11);
  600. RFIFOSKIP(fd,15);
  601. if( accounts->load_num(accounts, &acc, aid ) ) {
  602. time_t now = time(nullptr);
  603. time_t vip_time = acc.vip_time;
  604. bool isvip = false;
  605. if( acc.group_id > login_config.vip_sys.group ) { //Don't change group if it's higher.
  606. logchrif_sendvipdata(fd,&acc,0x2|((flag&0x8)?0x4:0),mapfd);
  607. return 1;
  608. }
  609. if( flag&2 ) {
  610. if(!vip_time)
  611. vip_time = now; //new entry
  612. vip_time += timediff; // set new duration
  613. }
  614. if( now < vip_time ) { //isvip
  615. if(acc.group_id != login_config.vip_sys.group){ //only upd this if we're not vip already
  616. acc.old_group = acc.group_id;
  617. if( acc.char_slots == 0 ){
  618. acc.char_slots = MIN_CHARS;
  619. }
  620. acc.char_slots += login_config.vip_sys.char_increase;
  621. }
  622. acc.group_id = login_config.vip_sys.group;
  623. isvip = true;
  624. } else { //expired or @vip -xx
  625. vip_time = 0;
  626. if(acc.group_id == login_config.vip_sys.group){ //prevent alteration in case account wasn't registered as vip yet
  627. acc.group_id = acc.old_group;
  628. if( acc.char_slots == 0 ){
  629. acc.char_slots = MIN_CHARS;
  630. }else{
  631. acc.char_slots -= login_config.vip_sys.char_increase;
  632. }
  633. }
  634. acc.old_group = 0;
  635. }
  636. acc.vip_time = vip_time;
  637. accounts->save(accounts,&acc, false);
  638. if( flag&1 )
  639. logchrif_sendvipdata(fd,&acc,((isvip)?0x1:0)|((flag&0x8)?0x4:0),mapfd);
  640. }
  641. }
  642. #endif
  643. return 1;
  644. }
  645. /**
  646. * IA 0x2720
  647. * Get account info that asked by inter/char-server
  648. */
  649. int32 logchrif_parse_accinfo(int32 fd) {
  650. if( RFIFOREST(fd) < 19 )
  651. return 0;
  652. else {
  653. int32 map_fd = RFIFOL(fd, 2), u_fd = RFIFOL(fd, 6), u_aid = RFIFOL(fd, 10), account_id = RFIFOL(fd, 14);
  654. int8 type = RFIFOB(fd, 18);
  655. AccountDB* accounts = login_get_accounts_db();
  656. struct mmo_account acc;
  657. RFIFOSKIP(fd,19);
  658. // Send back the result to char-server
  659. if (accounts->load_num(accounts, &acc, account_id)) {
  660. int32 len = 122 + NAME_LENGTH;
  661. //ShowInfo("Found account info for %d, requested by %d\n", account_id, u_aid);
  662. WFIFOHEAD(fd, len);
  663. WFIFOW(fd, 0) = 0x2721;
  664. WFIFOL(fd, 2) = map_fd;
  665. WFIFOL(fd, 6) = u_fd;
  666. WFIFOL(fd, 10) = u_aid;
  667. WFIFOL(fd, 14) = account_id;
  668. WFIFOB(fd, 18) = (1<<type); // success
  669. WFIFOL(fd, 19) = acc.group_id;
  670. WFIFOL(fd, 23) = acc.logincount;
  671. WFIFOL(fd, 27) = acc.state;
  672. safestrncpy(WFIFOCP(fd, 31), acc.email, 40);
  673. safestrncpy(WFIFOCP(fd, 71), acc.last_ip, 16);
  674. safestrncpy(WFIFOCP(fd, 87), acc.lastlogin, 24);
  675. safestrncpy(WFIFOCP(fd, 111), acc.birthdate, 11);
  676. safestrncpy(WFIFOCP(fd, 122), acc.userid, NAME_LENGTH);
  677. WFIFOSET(fd, len);
  678. }
  679. else {
  680. //ShowInfo("Cannot found account info for %d, requested by %d\n", account_id, u_aid);
  681. WFIFOHEAD(fd, 19);
  682. WFIFOW(fd, 0) = 0x2721;
  683. WFIFOL(fd, 2) = map_fd;
  684. WFIFOL(fd, 6) = u_fd;
  685. WFIFOL(fd, 10) = u_aid;
  686. WFIFOL(fd, 14) = account_id;
  687. WFIFOB(fd, 18) = 0; // failed
  688. WFIFOSET(fd, 19);
  689. }
  690. }
  691. return 1;
  692. }
  693. /**
  694. * Entry point from char-server to log-server.
  695. * Function that checks incoming command, then splits it to the correct handler.
  696. * @param fd: file descriptor to parse, (link to char-serv)
  697. * @return 0=invalid server,marked for disconnection,unknow packet; 1=success
  698. */
  699. int32 logchrif_parse(int32 fd){
  700. int32 cid; //char-serv id
  701. uint32 ipl;
  702. char ip[16];
  703. ARR_FIND( 0, ARRAYLENGTH(ch_server), cid, ch_server[cid].fd == fd );
  704. if( cid == ARRAYLENGTH(ch_server) ){// not a char server
  705. ShowDebug("logchrif_parse: Disconnecting invalid session #%d (is not a char-server)\n", fd);
  706. set_eof(fd);
  707. do_close(fd);
  708. return 0;
  709. }
  710. if( session[fd]->flag.eof ){
  711. do_close(fd);
  712. ch_server[cid].fd = -1;
  713. logchrif_on_disconnect(cid);
  714. return 0;
  715. }
  716. ipl = ch_server[cid].ip;
  717. ip2str(ipl, ip);
  718. while( RFIFOREST(fd) >= 2 ){
  719. int32 next = 1; // 0: avoid processing followup packets (prev was probably incomplete) packet, 1: Continue parsing
  720. uint16 command = RFIFOW(fd,0);
  721. switch( command ){
  722. case 0x2712: next = logchrif_parse_reqauth(fd, cid, ip); break;
  723. case 0x2714: next = logchrif_parse_ackusercount(fd, cid); break;
  724. case 0x2716: next = logchrif_parse_reqaccdata(fd, cid, ip); break;
  725. case 0x2719: next = logchrif_parse_keepalive(fd); break;
  726. case 0x2720: next = logchrif_parse_accinfo(fd); break; //@accinfo from inter-server
  727. case 0x2722: next = logchrif_parse_reqchangemail(fd,cid,ip); break;
  728. case 0x2724: next = logchrif_parse_requpdaccstate(fd,cid,ip); break;
  729. case 0x2725: next = logchrif_parse_reqbanacc(fd,cid,ip); break;
  730. case 0x2727: next = logchrif_parse_reqchgsex(fd,cid,ip); break;
  731. case 0x2728: next = logchrif_parse_upd_global_accreg(fd,cid,ip); break;
  732. case 0x272a: next = logchrif_parse_requnbanacc(fd,cid,ip); break;
  733. case 0x272b: next = logchrif_parse_setacconline(fd,cid); break;
  734. case 0x272c: next = logchrif_parse_setaccoffline(fd); break;
  735. case 0x272d: next = logchrif_parse_updonlinedb(fd,cid); break;
  736. case 0x272e: next = logchrif_parse_req_global_accreg(fd); break;
  737. case 0x2736: next = logchrif_parse_updcharip(fd,cid); break;
  738. case 0x2737: next = logchrif_parse_setalloffline(fd,cid); break;
  739. #if PACKETVER_SUPPORTS_PINCODE
  740. case 0x2738: next = logchrif_parse_updpincode(fd); break;
  741. case 0x2739: next = logchrif_parse_pincode_authfail(fd); break;
  742. #endif
  743. case 0x2742: next = logchrif_parse_reqvipdata(fd); break; //Vip sys
  744. default:
  745. ShowError("logchrif_parse: Unknown packet 0x%x from a char-server! Disconnecting!\n", command);
  746. set_eof(fd);
  747. return 0;
  748. } // switch
  749. if (next == 0)
  750. return 0;
  751. } // while
  752. return 1; //or 0
  753. }
  754. /// Constructor destructor and signal handlers
  755. /**
  756. * Initializes a server structure.
  757. * @param id: id of char-serv (should be >0, FIXME)
  758. */
  759. void logchrif_server_init(int32 id) {
  760. memset(&ch_server[id], 0, sizeof(ch_server[id]));
  761. ch_server[id].fd = -1;
  762. }
  763. /**
  764. * Destroys a server structure.
  765. * @param id: id of char-serv (should be >0, FIXME)
  766. */
  767. void logchrif_server_destroy(int32 id){
  768. if( ch_server[id].fd != -1 ) {
  769. do_close(ch_server[id].fd);
  770. ch_server[id].fd = -1;
  771. }
  772. }
  773. /**
  774. * Resets all the data related to a server.
  775. * Actually destroys then recreates the struct.
  776. * @param id: id of char-serv (should be >0, FIXME)
  777. */
  778. void logchrif_server_reset(int32 id) {
  779. login_online_db_setoffline(id); //Set all chars from this char server to offline.
  780. logchrif_server_destroy(id);
  781. logchrif_server_init(id);
  782. }
  783. /**
  784. * Called when the connection to Char Server is disconnected.
  785. * @param id: id of char-serv (should be >0, FIXME)
  786. */
  787. void logchrif_on_disconnect(int32 id) {
  788. ShowStatus("Char-server '%s' has disconnected.\n", ch_server[id].name);
  789. logchrif_server_reset(id);
  790. }
  791. /**
  792. * loginchrif constructor
  793. * Initialisation, function called at start of the login-serv.
  794. */
  795. void do_init_loginchrif(void){
  796. int32 i;
  797. for( i = 0; i < ARRAYLENGTH(ch_server); ++i )
  798. logchrif_server_init(i);
  799. // add timer to detect ip address change and perform update
  800. if (login_config.ip_sync_interval) {
  801. add_timer_func_list(logchrif_sync_ip_addresses, "sync_ip_addresses");
  802. add_timer_interval(gettick() + login_config.ip_sync_interval, logchrif_sync_ip_addresses, 0, 0, login_config.ip_sync_interval);
  803. }
  804. }
  805. /**
  806. * Signal handler
  807. * This function attempts to properly close the server when an interrupt signal is received.
  808. * current signal catch : SIGTERM, SIGINT
  809. */
  810. void do_shutdown_loginchrif(void){
  811. int32 id;
  812. for( id = 0; id < ARRAYLENGTH(ch_server); ++id )
  813. logchrif_server_reset(id);
  814. }
  815. /**
  816. * loginchrif destructor
  817. * dealloc..., function called at exit of the login-serv
  818. */
  819. void do_final_loginchrif(void){
  820. int32 i;
  821. for( i = 0; i < ARRAYLENGTH(ch_server); ++i )
  822. logchrif_server_destroy(i);
  823. }