login.c 147 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/mmo.h"
  5. #include "../common/core.h"
  6. #include "../common/socket.h"
  7. #include "../common/db.h"
  8. #include "../common/timer.h"
  9. #include "../common/malloc.h"
  10. #include "../common/strlib.h"
  11. #include "../common/showmsg.h"
  12. #include "../common/version.h"
  13. #include "../common/md5calc.h"
  14. #include "../common/lock.h"
  15. #include "login.h"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/stat.h> // for stat/lstat/fstat
  20. struct Login_Config {
  21. uint32 login_ip; // the address to bind to
  22. uint16 login_port; // the port to bind to
  23. unsigned int ip_sync_interval; // interval (in minutes) to execute a DNS/IP update (for dynamic IPs)
  24. bool log_login; // whether to log login server actions or not
  25. char date_format[32]; // date format used in messages
  26. bool console; // console input system enabled?
  27. bool new_account_flag; // autoregistration via _M/_F ?
  28. // bool case_sensitive; // are logins case sensitive ?
  29. bool use_md5_passwds; // work with password hashes instead of plaintext passwords?
  30. // bool login_gm_read; // should the login server handle info about gm accounts?
  31. int min_level_to_connect; // minimum level of player/GM (0: player, 1-99: GM) to connect
  32. bool online_check; // reject incoming players that are already registered as online ?
  33. bool check_client_version; // check the clientversion set in the clientinfo ?
  34. unsigned int client_version_to_connect; // the client version needed to connect (if checking is enabled)
  35. // bool ipban; // perform IP blocking (via contents of `ipbanlist`) ?
  36. // bool dynamic_pass_failure_ban; // automatic IP blocking due to failed login attemps ?
  37. // unsigned int dynamic_pass_failure_ban_interval; // how far to scan the loginlog for password failures
  38. // unsigned int dynamic_pass_failure_ban_limit; // number of failures needed to trigger the ipban
  39. // unsigned int dynamic_pass_failure_ban_duration; // duration of the ipban
  40. bool use_dnsbl; // dns blacklist blocking ?
  41. char dnsbl_servs[1024]; // comma-separated list of dnsbl servers
  42. } login_config;
  43. int login_fd; // login server socket
  44. #define MAX_SERVERS 30
  45. int server_fd[MAX_SERVERS]; // char server sockets
  46. struct mmo_char_server server[MAX_SERVERS]; // char server data
  47. // Advanced subnet check [LuzZza]
  48. struct s_subnet {
  49. uint32 mask;
  50. uint32 char_ip;
  51. uint32 map_ip;
  52. } subnet[16];
  53. int subnet_count = 0;
  54. struct gm_account* gm_account_db = NULL;
  55. unsigned int GM_num = 0; // number of gm accounts
  56. unsigned int GM_max = 0;
  57. //Account registration flood protection [Kevin]
  58. int allowed_regs = 1;
  59. int time_allowed = 10; //in seconds
  60. unsigned int new_reg_tick = 0;
  61. int num_regs = 0;
  62. uint32 account_id_count = START_ACCOUNT_NUM;
  63. char account_filename[1024] = "save/account.txt";
  64. char GM_account_filename[1024] = "conf/GM_account.txt";
  65. char login_log_filename[1024] = "log/login.log";
  66. FILE *log_fp = NULL;
  67. char login_log_unknown_packets_filename[1024] = "log/login_unknown_packets.log";
  68. int save_unknown_packets = 0;
  69. long creation_time_GM_account_file;
  70. int gm_account_filename_check_timer = 15; // Timer to check if GM_account file has been changed and reload GM account automaticaly (in seconds; default: 15)
  71. int display_parse_login = 0; // 0: no, 1: yes
  72. int display_parse_admin = 0; // 0: no, 1: yes
  73. int display_parse_fromchar = 0; // 0: no, 1: yes (without packet 0x2714), 2: all packets
  74. enum {
  75. ACO_DENY_ALLOW = 0,
  76. ACO_ALLOW_DENY,
  77. ACO_MUTUAL_FAILTURE,
  78. ACO_STRSIZE = 128,
  79. };
  80. int access_order = ACO_DENY_ALLOW;
  81. int access_allownum = 0;
  82. int access_denynum = 0;
  83. char *access_allow = NULL;
  84. char *access_deny = NULL;
  85. int access_ladmin_allownum = 0;
  86. char *access_ladmin_allow = NULL;
  87. int add_to_unlimited_account = 0; // Give possibility or not to adjust (ladmin command: timeadd) the time of an unlimited account.
  88. int start_limited_time = -1; // Starting additional sec from now for the limited time at creation of accounts (-1: unlimited time, 0 or more: additional sec from now)
  89. struct login_session_data {
  90. uint16 md5keylen;
  91. char md5key[20];
  92. };
  93. #define AUTH_FIFO_SIZE 256
  94. struct _auth_fifo {
  95. uint32 account_id;
  96. uint32 login_id1, login_id2;
  97. uint32 ip;
  98. uint8 sex;
  99. bool delflag;
  100. } auth_fifo[AUTH_FIFO_SIZE];
  101. int auth_fifo_pos = 0;
  102. struct online_login_data {
  103. uint32 account_id;
  104. int waiting_disconnect;
  105. int char_server;
  106. };
  107. // holds info about all existing accounts
  108. struct auth_data {
  109. uint32 account_id;
  110. uint8 sex; // 0, 1, 2
  111. char userid[24];
  112. char pass[32+1]; // 23+1 for normal, 32+1 for md5-ed passwords
  113. char lastlogin[24];
  114. int logincount;
  115. uint32 state; // packet 0x006a value + 1 (0: compte OK)
  116. char email[40]; // e-mail (by default: a@a.com)
  117. char error_message[20]; // Message of error code #6 = Your are Prohibited to log in until %s (packet 0x006a)
  118. time_t ban_until_time; // # of seconds 1/1/1970 (timestamp): ban time limit of the account (0 = no ban)
  119. time_t connect_until_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  120. char last_ip[16]; // save of last IP of connection
  121. char memo[255]; // a memo field
  122. int account_reg2_num;
  123. struct global_reg account_reg2[ACCOUNT_REG2_NUM]; // account script variables (stored on login server)
  124. } *auth_dat = NULL;
  125. uint32 auth_num = 0, auth_max = 0;
  126. // define the number of times that some players must authentify them before to save account file.
  127. // it's just about normal authentification. If an account is created or modified, save is immediatly done.
  128. // An authentification just change last connected IP and date. It already save in log file.
  129. // set minimum auth change before save:
  130. #define AUTH_BEFORE_SAVE_FILE 10
  131. // set divider of auth_num to found number of change before save
  132. #define AUTH_SAVE_FILE_DIVIDER 50
  133. int auth_before_save_file = 0; // Counter. First save when 1st char-server do connection.
  134. int admin_state = 0;
  135. char admin_pass[24] = "";
  136. char gm_pass[64] = "";
  137. int level_new_gm = 60;
  138. static struct dbt *online_db;
  139. int charif_sendallwos(int sfd, unsigned char *buf, unsigned int len);
  140. //------------------------------
  141. // Writing function of logs file
  142. //------------------------------
  143. int login_log(char *fmt, ...)
  144. {
  145. if( login_config.log_login ) {
  146. va_list ap;
  147. time_t raw_time;
  148. char tmpstr[2048];
  149. if(!log_fp)
  150. log_fp = fopen(login_log_filename, "a");
  151. if (log_fp) {
  152. if (fmt[0] == '\0') // jump a line if no message
  153. fprintf(log_fp, "\n");
  154. else {
  155. va_start(ap, fmt);
  156. time(&raw_time);
  157. strftime(tmpstr, 24, login_config.date_format, localtime(&raw_time));
  158. sprintf(tmpstr + strlen(tmpstr), ": %s", fmt);
  159. vfprintf(log_fp, tmpstr, ap);
  160. va_end(ap);
  161. }
  162. fflush(log_fp); // under cygwin or windows, if software is stopped, data are not written in the file -> fflush at every line
  163. }
  164. }
  165. return 0;
  166. }
  167. static void* create_online_user(DBKey key, va_list args)
  168. {
  169. struct online_login_data *p;
  170. p = aCalloc(1, sizeof(struct online_login_data));
  171. p->account_id = key.i;
  172. p->char_server = -1;
  173. p->waiting_disconnect = -1;
  174. return p;
  175. }
  176. static int waiting_disconnect_timer(int tid, unsigned int tick, int id, int data);
  177. //-----------------------------------------------------
  178. // Online User Database [Wizputer]
  179. //-----------------------------------------------------
  180. void add_online_user(int char_server, int account_id)
  181. {
  182. struct online_login_data *p;
  183. if( !login_config.online_check )
  184. return;
  185. p = idb_ensure(online_db, account_id, create_online_user);
  186. p->char_server = char_server;
  187. if( p->waiting_disconnect != -1 )
  188. {
  189. delete_timer(p->waiting_disconnect, waiting_disconnect_timer);
  190. p->waiting_disconnect = -1;
  191. }
  192. }
  193. void remove_online_user(int account_id)
  194. {
  195. if( !login_config.online_check )
  196. return;
  197. if( account_id == 99 )
  198. {// reset all to offline
  199. online_db->clear(online_db, NULL); // purge db
  200. return;
  201. }
  202. idb_remove(online_db, account_id);
  203. }
  204. static int waiting_disconnect_timer(int tid, unsigned int tick, int id, int data)
  205. {
  206. struct online_login_data* p = idb_get(online_db, id);
  207. if( p != NULL && p->waiting_disconnect == id )
  208. {
  209. p->waiting_disconnect = -1;
  210. remove_online_user(id);
  211. }
  212. return 0;
  213. }
  214. static int sync_ip_addresses(int tid, unsigned int tick, int id, int data)
  215. {
  216. uint8 buf[2];
  217. ShowInfo("IP Sync in progress...\n");
  218. WBUFW(buf,0) = 0x2735;
  219. charif_sendallwos(-1, buf, 2);
  220. return 0;
  221. }
  222. //----------------------------------------------------------------------
  223. // Determine if an account (id) is a GM account
  224. // and returns its level (or 0 if it isn't a GM account or if not found)
  225. //----------------------------------------------------------------------
  226. int isGM(int account_id)
  227. {
  228. unsigned int i;
  229. for(i=0; i < GM_num; i++)
  230. if(gm_account_db[i].account_id == account_id)
  231. return gm_account_db[i].level;
  232. return 0;
  233. }
  234. //----------------------------------------------------------------------
  235. // Adds a new GM using acc id and level
  236. //----------------------------------------------------------------------
  237. void addGM(uint32 account_id, int level)
  238. {
  239. unsigned int i;
  240. int do_add = 0;
  241. for(i = 0; i < auth_num; i++) {
  242. if (auth_dat[i].account_id == account_id) {
  243. do_add = 1;
  244. break;
  245. }
  246. }
  247. for(i = 0; i < GM_num; i++)
  248. if (gm_account_db[i].account_id == account_id) {
  249. if (gm_account_db[i].level == level)
  250. ShowWarning("addGM: GM account %d defined twice (same level: %d).\n", account_id, level);
  251. else {
  252. ShowWarning("addGM: GM account %d defined twice (levels: %d and %d).\n", account_id, gm_account_db[i].level, level);
  253. gm_account_db[i].level = level;
  254. }
  255. return;
  256. }
  257. // if new account
  258. if (i == GM_num && do_add) {
  259. if (GM_num >= GM_max) {
  260. GM_max += 256;
  261. gm_account_db = (struct gm_account*)aRealloc(gm_account_db, sizeof(struct gm_account) * GM_max);
  262. memset(gm_account_db + (GM_max - 256), 0, sizeof(struct gm_account) * 256);
  263. }
  264. gm_account_db[GM_num].account_id = account_id;
  265. gm_account_db[GM_num].level = level;
  266. GM_num++;
  267. if (GM_num >= 4000) {
  268. ShowWarning("4000 GM accounts found. Next GM accounts are not read.\n");
  269. login_log("***WARNING: 4000 GM accounts found. Next GM accounts are not read.\n");
  270. }
  271. }
  272. }
  273. //-------------------------------------------------------
  274. // Reading function of GM accounts file (and their level)
  275. //-------------------------------------------------------
  276. int read_gm_account(void)
  277. {
  278. char line[512];
  279. FILE *fp;
  280. int account_id, level;
  281. int line_counter;
  282. struct stat file_stat;
  283. int start_range = 0, end_range = 0, is_range = 0, current_id = 0;
  284. if(gm_account_db) aFree(gm_account_db);
  285. gm_account_db = (struct gm_account*)aCalloc(GM_max, sizeof(struct gm_account));
  286. GM_num = 0;
  287. // get last modify time/date
  288. if (stat(GM_account_filename, &file_stat))
  289. creation_time_GM_account_file = 0; // error
  290. else
  291. creation_time_GM_account_file = (long)file_stat.st_mtime;
  292. if ((fp = fopen(GM_account_filename, "r")) == NULL) {
  293. ShowError("read_gm_account: GM accounts file [%s] not found.\n", GM_account_filename);
  294. ShowError(" Actually, there is no GM accounts on the server.\n");
  295. login_log("read_gm_account: GM accounts file [%s] not found.\n", GM_account_filename);
  296. login_log(" Actually, there is no GM accounts on the server.\n");
  297. return 1;
  298. }
  299. line_counter = 0;
  300. // limited to 4000, because we send information to char-servers (more than 4000 GM accounts???)
  301. // int (id) + int (level) = 8 bytes * 4000 = 32k (limit of packets in windows)
  302. while(fgets(line, sizeof(line), fp) && GM_num < 4000)
  303. {
  304. line_counter++;
  305. if ((line[0] == '/' && line[1] == '/') || line[0] == '\0' || line[0] == '\n' || line[0] == '\r')
  306. continue;
  307. is_range = (sscanf(line, "%d%*[-~]%d %d",&start_range,&end_range,&level)==3); // ID Range [MC Cameri]
  308. if (!is_range && sscanf(line, "%d %d", &account_id, &level) != 2 && sscanf(line, "%d: %d", &account_id, &level) != 2)
  309. ShowError("read_gm_account: file [%s], invalid 'acount_id|range level' format (line #%d).\n", GM_account_filename, line_counter);
  310. else if (level <= 0)
  311. ShowError("read_gm_account: file [%s] %dth account (line #%d) (invalid level [0 or negative]: %d).\n", GM_account_filename, GM_num+1, line_counter, level);
  312. else {
  313. if (level > 99) {
  314. ShowNotice("read_gm_account: file [%s] %dth account (invalid level, but corrected: %d->99).\n", GM_account_filename, GM_num+1, level);
  315. level = 99;
  316. }
  317. if (is_range) {
  318. if (start_range==end_range)
  319. ShowError("read_gm_account: file [%s] invalid range, beginning of range is equal to end of range (line #%d).\n", GM_account_filename, line_counter);
  320. else if (start_range>end_range)
  321. ShowError("read_gm_account: file [%s] invalid range, beginning of range must be lower than end of range (line #%d).\n", GM_account_filename, line_counter);
  322. else
  323. for (current_id = start_range;current_id<=end_range;current_id++)
  324. addGM(current_id,level);
  325. } else {
  326. addGM(account_id,level);
  327. }
  328. }
  329. }
  330. fclose(fp);
  331. ShowStatus("read_gm_account: file '%s' read (%d GM accounts found).\n", GM_account_filename, GM_num);
  332. login_log("read_gm_account: file '%s' read (%d GM accounts found).\n", GM_account_filename, GM_num);
  333. return 0;
  334. }
  335. //--------------------------------------------------------------
  336. // Test of the IP mask
  337. // (ip: IP to be tested, str: mask x.x.x.x/# or x.x.x.x/y.y.y.y)
  338. //--------------------------------------------------------------
  339. int check_ipmask(uint32 ip, const unsigned char *str)
  340. {
  341. unsigned int i = 0, m = 0;
  342. uint32 ip2, mask = 0;
  343. uint32 a0, a1, a2, a3;
  344. uint8* p = (uint8 *)&ip2, *p2 = (uint8 *)&mask;
  345. // scan ip address
  346. if (sscanf((const char*)str, "%u.%u.%u.%u/%n", &a0, &a1, &a2, &a3, &i) != 4 || i == 0)
  347. return 0;
  348. p[0] = (uint8)a3; p[1] = (uint8)a2; p[2] = (uint8)a1; p[3] = (uint8)a0;
  349. // scan mask
  350. if (sscanf((const char*)str+i, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) == 4) {
  351. p2[0] = (uint8)a3; p2[1] = (uint8)a2; p2[2] = (uint8)a1; p2[3] = (uint8)a0;
  352. } else if (sscanf((const char*)(str+i), "%u", &m) == 1 && m <= 32) {
  353. for(i = 32 - m; i < 32; i++)
  354. mask |= (1 << i);
  355. } else {
  356. ShowError("check_ipmask: invalid mask [%s].\n", str);
  357. return 0;
  358. }
  359. return ((ip & mask) == (ip2 & mask));
  360. }
  361. //---------------------
  362. // Access control by IP
  363. //---------------------
  364. int check_ip(uint32 ip)
  365. {
  366. int i;
  367. char buf[20];
  368. char * access_ip;
  369. enum { ACF_DEF, ACF_ALLOW, ACF_DENY } flag = ACF_DEF;
  370. if (access_allownum == 0 && access_denynum == 0)
  371. return 1; // When there is no restriction, all IP are authorised.
  372. // + 012.345.: front match form, or
  373. // all: all IP are matched, or
  374. // 012.345.678.901/24: network form (mask with # of bits), or
  375. // 012.345.678.901/255.255.255.0: network form (mask with ip mask)
  376. // + Note about the DNS resolution (like www.ne.jp, etc.):
  377. // There is no guarantee to have an answer.
  378. // If we have an answer, there is no guarantee to have a 100% correct value.
  379. // And, the waiting time (to check) can be long (over 1 minute to a timeout). That can block the software.
  380. // So, DNS notation isn't authorised for ip checking.
  381. sprintf(buf, "%u.%u.%u.%u.", CONVIP(ip));
  382. for(i = 0; i < access_allownum; i++) {
  383. access_ip = access_allow + i * ACO_STRSIZE;
  384. if (memcmp(access_ip, buf, strlen(access_ip)) == 0 || check_ipmask(ip, (unsigned char*)access_ip)) {
  385. if(access_order == ACO_ALLOW_DENY)
  386. return 1; // With 'allow, deny' (deny if not allow), allow has priority
  387. flag = ACF_ALLOW;
  388. break;
  389. }
  390. }
  391. for(i = 0; i < access_denynum; i++) {
  392. access_ip = access_deny + i * ACO_STRSIZE;
  393. if (memcmp(access_ip, buf, strlen(access_ip)) == 0 || check_ipmask(ip, (unsigned char*)access_ip)) {
  394. //flag = ACF_DENY; // not necessary to define flag
  395. return 0; // At this point, if it's 'deny', we refuse connection.
  396. }
  397. }
  398. return (flag == ACF_ALLOW || access_order == ACO_DENY_ALLOW) ? 1:0;
  399. // With 'mutual-failture', only 'allow' and non 'deny' IP are authorised.
  400. // A non 'allow' (even non 'deny') IP is not authorised. It's like: if allowed and not denied, it's authorised.
  401. // So, it's disapproval if you have no description at the time of 'mutual-failture'.
  402. // With 'deny,allow' (allow if not deny), because here it's not deny, we authorise.
  403. }
  404. //--------------------------------
  405. // Access control by IP for ladmin
  406. //--------------------------------
  407. int check_ladminip(uint32 ip)
  408. {
  409. int i;
  410. char buf[20];
  411. char * access_ip;
  412. if (access_ladmin_allownum == 0)
  413. return 1; // When there is no restriction, all IP are authorised.
  414. // + 012.345.: front match form, or
  415. // all: all IP are matched, or
  416. // 012.345.678.901/24: network form (mask with # of bits), or
  417. // 012.345.678.901/255.255.255.0: network form (mask with ip mask)
  418. // + Note about the DNS resolution (like www.ne.jp, etc.):
  419. // There is no guarantee to have an answer.
  420. // If we have an answer, there is no guarantee to have a 100% correct value.
  421. // And, the waiting time (to check) can be long (over 1 minute to a timeout). That can block the software.
  422. // So, DNS notation isn't authorised for ip checking.
  423. sprintf(buf, "%u.%u.%u.%u.", CONVIP(ip));
  424. for(i = 0; i < access_ladmin_allownum; i++) {
  425. access_ip = access_ladmin_allow + i * ACO_STRSIZE;
  426. if (memcmp(access_ip, buf, strlen(access_ip)) == 0 || check_ipmask(ip, (unsigned char*)access_ip)) {
  427. return 1;
  428. }
  429. }
  430. return 0;
  431. }
  432. //-----------------------------------------------
  433. // Search an account id
  434. // (return account index or -1 (if not found))
  435. // If exact account name is not found,
  436. // the function checks without case sensitive
  437. // and returns index if only 1 account is found
  438. // and similar to the searched name.
  439. //-----------------------------------------------
  440. int search_account_index(char* account_name)
  441. {
  442. unsigned int i, quantity;
  443. int index;
  444. quantity = 0;
  445. index = -1;
  446. for(i = 0; i < auth_num; i++) {
  447. // Without case sensitive check (increase the number of similar account names found)
  448. if (stricmp(auth_dat[i].userid, account_name) == 0) {
  449. // Strict comparison (if found, we finish the function immediatly with correct value)
  450. if (strcmp(auth_dat[i].userid, account_name) == 0)
  451. return i;
  452. quantity++;
  453. index = i;
  454. }
  455. }
  456. // Here, the exact account name is not found
  457. // We return the found index of a similar account ONLY if there is 1 similar account
  458. if (quantity == 1)
  459. return index;
  460. // Exact account name is not found and 0 or more than 1 similar accounts have been found ==> we say not found
  461. return -1;
  462. }
  463. //--------------------------------------------------------
  464. // Create a string to save the account in the account file
  465. //--------------------------------------------------------
  466. int mmo_auth_tostr(char* str, struct auth_data* p)
  467. {
  468. int i;
  469. char *str_p = str;
  470. str_p += sprintf(str_p, "%u\t%s\t%s\t%s\t%c\t%d\t%u\t%s\t%s\t%ld\t%s\t%s\t%ld\t",
  471. p->account_id, p->userid, p->pass, p->lastlogin,
  472. p->sex == 2 ? 'S' : p->sex == 1 ? 'M' : 'F',
  473. p->logincount, p->state, p->email, p->error_message,
  474. (long)p->connect_until_time, p->last_ip, p->memo, (long)p->ban_until_time);
  475. for(i = 0; i < p->account_reg2_num; i++)
  476. if (p->account_reg2[i].str[0])
  477. str_p += sprintf(str_p, "%s,%s ", p->account_reg2[i].str, p->account_reg2[i].value);
  478. return 0;
  479. }
  480. //---------------------------------
  481. // Reading of the accounts database
  482. //---------------------------------
  483. int mmo_auth_init(void)
  484. {
  485. FILE *fp;
  486. uint32 account_id;
  487. uint32 state;
  488. int logincount, n;
  489. uint32 i, j;
  490. char line[2048], *p, userid[2048], pass[2048], lastlogin[2048], sex, email[2048], error_message[2048], last_ip[2048], memo[2048];
  491. long ban_until_time;
  492. long connect_until_time;
  493. char str[2048];
  494. char v[2048];
  495. int GM_count = 0;
  496. int server_count = 0;
  497. auth_max = 256;
  498. auth_dat = (struct auth_data*)aCalloc(auth_max, sizeof(struct auth_data));
  499. if ((fp = fopen(account_filename, "r")) == NULL) {
  500. // no account file -> no account -> no login, including char-server (ERROR)
  501. ShowError(CL_RED"mmmo_auth_init: Accounts file [%s] not found."CL_RESET"\n", account_filename);
  502. return 0;
  503. }
  504. while(fgets(line, sizeof(line), fp) != NULL)
  505. {
  506. if (line[0] == '/' && line[1] == '/')
  507. continue;
  508. p = line;
  509. memset(userid, 0, sizeof(userid));
  510. memset(pass, 0, sizeof(pass));
  511. memset(lastlogin, 0, sizeof(lastlogin));
  512. memset(email, 0, sizeof(email));
  513. memset(error_message, 0, sizeof(error_message));
  514. memset(last_ip, 0, sizeof(last_ip));
  515. memset(memo, 0, sizeof(memo));
  516. // database version reading (v2)
  517. if (((i = sscanf(line, "%u\t%[^\t]\t%[^\t]\t%[^\t]\t%c\t%d\t%u\t"
  518. "%[^\t]\t%[^\t]\t%ld\t%[^\t]\t%[^\t]\t%ld%n",
  519. &account_id, userid, pass, lastlogin, &sex, &logincount, &state,
  520. email, error_message, &connect_until_time, last_ip, memo, &ban_until_time, &n)) == 13 && line[n] == '\t') ||
  521. ((i = sscanf(line, "%u\t%[^\t]\t%[^\t]\t%[^\t]\t%c\t%d\t%u\t"
  522. "%[^\t]\t%[^\t]\t%ld\t%[^\t]\t%[^\t]%n",
  523. &account_id, userid, pass, lastlogin, &sex, &logincount, &state,
  524. email, error_message, &connect_until_time, last_ip, memo, &n)) == 12 && line[n] == '\t')) {
  525. n = n + 1;
  526. // Some checks
  527. if (account_id > END_ACCOUNT_NUM) {
  528. ShowError(CL_RED"mmmo_auth_init: an account has an id higher than %d\n", END_ACCOUNT_NUM);
  529. ShowError(" account id #%d -> account not read (saved in log file)."CL_RESET"\n", account_id);
  530. login_log("mmmo_auth_init: ******Error: an account has an id higher than %d.\n", END_ACCOUNT_NUM);
  531. login_log(" account id #%d -> account not read (saved in next line):\n", account_id);
  532. login_log("%s", line);
  533. continue;
  534. }
  535. userid[23] = '\0';
  536. remove_control_chars(userid);
  537. for(j = 0; j < auth_num; j++) {
  538. if (auth_dat[j].account_id == account_id) {
  539. ShowError(CL_RED"mmmo_auth_init: an account has an identical id to another.\n");
  540. ShowError(" account id #%d -> new account not read (saved in log file)."CL_RED"\n", account_id);
  541. login_log("mmmo_auth_init: ******Error: an account has an identical id to another.\n");
  542. login_log(" account id #%d -> new account not read (saved in next line):\n", account_id);
  543. login_log("%s", line);
  544. break;
  545. } else if (strcmp(auth_dat[j].userid, userid) == 0) {
  546. ShowError(CL_RED"mmmo_auth_init: account name already exists.\n");
  547. ShowError(" account name '%s' -> new account not read (saved in log file)."CL_RESET"\n", userid); // 2 lines, account name can be long.
  548. login_log("mmmo_auth_init: ******Error: an account has an identical name to another.\n");
  549. login_log(" account name '%s' -> new account not read (saved in next line):\n", userid);
  550. login_log("%s", line);
  551. break;
  552. }
  553. }
  554. if (j != auth_num)
  555. continue;
  556. if (auth_num >= auth_max) {
  557. auth_max += 256;
  558. auth_dat = (struct auth_data*)aRealloc(auth_dat, sizeof(struct auth_data) * auth_max);
  559. }
  560. memset(&auth_dat[auth_num], '\0', sizeof(struct auth_data));
  561. auth_dat[auth_num].account_id = account_id;
  562. strncpy(auth_dat[auth_num].userid, userid, 24);
  563. pass[32] = '\0';
  564. remove_control_chars(pass);
  565. strncpy(auth_dat[auth_num].pass, pass, 32);
  566. lastlogin[23] = '\0';
  567. remove_control_chars(lastlogin);
  568. strncpy(auth_dat[auth_num].lastlogin, lastlogin, 24);
  569. auth_dat[auth_num].sex = (sex == 'S' || sex == 's') ? 2 : (sex == 'M' || sex == 'm');
  570. if (logincount >= 0)
  571. auth_dat[auth_num].logincount = logincount;
  572. else
  573. auth_dat[auth_num].logincount = 0;
  574. if (state > 255)
  575. auth_dat[auth_num].state = 100;
  576. else
  577. auth_dat[auth_num].state = state;
  578. if (e_mail_check(email) == 0) {
  579. ShowNotice("Account %s (%d): invalid e-mail (replaced par a@a.com).\n", auth_dat[auth_num].userid, auth_dat[auth_num].account_id);
  580. strncpy(auth_dat[auth_num].email, "a@a.com", 40);
  581. } else {
  582. remove_control_chars(email);
  583. strncpy(auth_dat[auth_num].email, email, 40);
  584. }
  585. error_message[19] = '\0';
  586. remove_control_chars(error_message);
  587. if (error_message[0] == '\0' || state != 7) { // 7, because state is packet 0x006a value + 1
  588. strncpy(auth_dat[auth_num].error_message, "-", 20);
  589. } else {
  590. strncpy(auth_dat[auth_num].error_message, error_message, 20);
  591. }
  592. if (i == 13)
  593. auth_dat[auth_num].ban_until_time = (time_t)ban_until_time;
  594. else
  595. auth_dat[auth_num].ban_until_time = 0;
  596. auth_dat[auth_num].connect_until_time = (time_t)connect_until_time;
  597. last_ip[15] = '\0';
  598. remove_control_chars(last_ip);
  599. strncpy(auth_dat[auth_num].last_ip, last_ip, 16);
  600. memo[254] = '\0';
  601. remove_control_chars(memo);
  602. strncpy(auth_dat[auth_num].memo, memo, 255);
  603. for(j = 0; j < ACCOUNT_REG2_NUM; j++) {
  604. p += n;
  605. if (sscanf(p, "%[^\t,],%[^\t ] %n", str, v, &n) != 2) {
  606. // We must check if a str is void. If it's, we can continue to read other REG2.
  607. // Account line will have something like: str2,9 ,9 str3,1 (here, ,9 is not good)
  608. if (p[0] == ',' && sscanf(p, ",%[^\t ] %n", v, &n) == 1) {
  609. j--;
  610. continue;
  611. } else
  612. break;
  613. }
  614. str[31] = '\0';
  615. remove_control_chars(str);
  616. strncpy(auth_dat[auth_num].account_reg2[j].str, str, 32);
  617. strncpy(auth_dat[auth_num].account_reg2[j].value,v,256);
  618. }
  619. auth_dat[auth_num].account_reg2_num = j;
  620. if (isGM(account_id) > 0)
  621. GM_count++;
  622. if (auth_dat[auth_num].sex == 2)
  623. server_count++;
  624. auth_num++;
  625. if (account_id >= account_id_count)
  626. account_id_count = account_id + 1;
  627. // Old athena database version reading (v1)
  628. } else if ((i = sscanf(line, "%u\t%[^\t]\t%[^\t]\t%[^\t]\t%c\t%d\t%u\t%n",
  629. &account_id, userid, pass, lastlogin, &sex, &logincount, &state, &n)) >= 5) {
  630. if (account_id > END_ACCOUNT_NUM) {
  631. ShowError(CL_RED"mmmo_auth_init: an account has an id higher than %d\n", END_ACCOUNT_NUM);
  632. ShowError(" account id #%d -> account not read (saved in log file)."CL_RESET"\n", account_id);
  633. login_log("mmmo_auth_init: ******Error: an account has an id higher than %d.\n", END_ACCOUNT_NUM);
  634. login_log(" account id #%d -> account not read (saved in next line):\n", account_id);
  635. login_log("%s", line);
  636. continue;
  637. }
  638. userid[23] = '\0';
  639. remove_control_chars(userid);
  640. for(j = 0; j < auth_num; j++) {
  641. if (auth_dat[j].account_id == account_id) {
  642. ShowError(CL_RED"mmo_auth_init: an account has an identical id to another.\n");
  643. ShowError(" account id #%d -> new account not read (saved in log file)."CL_RESET"\n", account_id);
  644. login_log("mmmo_auth_init: ******Error: an account has an identical id to another.\n");
  645. login_log(" account id #%d -> new account not read (saved in next line):\n", account_id);
  646. login_log("%s", line);
  647. break;
  648. } else if (strcmp(auth_dat[j].userid, userid) == 0) {
  649. ShowError(CL_RED"mmo_auth_init: account name already exists.\n");
  650. ShowError(" account name '%s' -> new account not read (saved in log file)."CL_RESET"\n", userid);
  651. login_log("mmmo_auth_init: ******Error: an account has an identical id to another.\n");
  652. login_log(" account id #%d -> new account not read (saved in next line):\n", account_id);
  653. login_log("%s", line);
  654. break;
  655. }
  656. }
  657. if (j != auth_num)
  658. continue;
  659. if (auth_num >= auth_max) {
  660. auth_max += 256;
  661. auth_dat = (struct auth_data*)aRealloc(auth_dat, sizeof(struct auth_data) * auth_max);
  662. }
  663. memset(&auth_dat[auth_num], '\0', sizeof(struct auth_data));
  664. auth_dat[auth_num].account_id = account_id;
  665. strncpy(auth_dat[auth_num].userid, userid, 24);
  666. pass[23] = '\0';
  667. remove_control_chars(pass);
  668. strncpy(auth_dat[auth_num].pass, pass, 24);
  669. lastlogin[23] = '\0';
  670. remove_control_chars(lastlogin);
  671. strncpy(auth_dat[auth_num].lastlogin, lastlogin, 24);
  672. auth_dat[auth_num].sex = (sex == 'S' || sex == 's') ? 2 : (sex == 'M' || sex == 'm');
  673. if (i >= 6) {
  674. if (logincount >= 0)
  675. auth_dat[auth_num].logincount = logincount;
  676. else
  677. auth_dat[auth_num].logincount = 0;
  678. } else
  679. auth_dat[auth_num].logincount = 0;
  680. if (i >= 7) {
  681. if (state > 255)
  682. auth_dat[auth_num].state = 100;
  683. else
  684. auth_dat[auth_num].state = state;
  685. } else
  686. auth_dat[auth_num].state = 0;
  687. // Initialization of new data
  688. strncpy(auth_dat[auth_num].email, "a@a.com", 40);
  689. strncpy(auth_dat[auth_num].error_message, "-", 20);
  690. auth_dat[auth_num].ban_until_time = 0;
  691. auth_dat[auth_num].connect_until_time = 0;
  692. strncpy(auth_dat[auth_num].last_ip, "-", 16);
  693. strncpy(auth_dat[auth_num].memo, "-", 255);
  694. for(j = 0; j < ACCOUNT_REG2_NUM; j++) {
  695. p += n;
  696. if (sscanf(p, "%[^\t,],%[^\t ] %n", str, v, &n) != 2) {
  697. // We must check if a str is void. If it's, we can continue to read other REG2.
  698. // Account line will have something like: str2,9 ,9 str3,1 (here, ,9 is not good)
  699. if (p[0] == ',' && sscanf(p, ",%[^\t ] %n", v, &n) == 1) {
  700. j--;
  701. continue;
  702. } else
  703. break;
  704. }
  705. str[31] = '\0';
  706. remove_control_chars(str);
  707. strncpy(auth_dat[auth_num].account_reg2[j].str, str, 32);
  708. strncpy(auth_dat[auth_num].account_reg2[j].value,v,256);
  709. }
  710. auth_dat[auth_num].account_reg2_num = j;
  711. if (isGM(account_id) > 0)
  712. GM_count++;
  713. if (auth_dat[auth_num].sex == 2)
  714. server_count++;
  715. auth_num++;
  716. if (account_id >= account_id_count)
  717. account_id_count = account_id + 1;
  718. } else {
  719. int i = 0;
  720. if (sscanf(line, "%u\t%%newid%%\n%n", &account_id, &i) == 1 &&
  721. i > 0 && account_id > account_id_count)
  722. account_id_count = account_id;
  723. }
  724. }
  725. fclose(fp);
  726. if (auth_num == 0) {
  727. ShowNotice("mmo_auth_init: No account found in %s.\n", account_filename);
  728. sprintf(line, "No account found in %s.", account_filename);
  729. } else {
  730. if (auth_num == 1) {
  731. ShowStatus("mmo_auth_init: 1 account read in %s,\n", account_filename);
  732. sprintf(line, "1 account read in %s,", account_filename);
  733. } else {
  734. ShowStatus("mmo_auth_init: %d accounts read in %s,\n", auth_num, account_filename);
  735. sprintf(line, "%u accounts read in %s,", auth_num, account_filename);
  736. }
  737. if (GM_count == 0) {
  738. ShowStatus(" of which is no GM account, and ");
  739. sprintf(str, "%s of which is no GM account and", line);
  740. } else if (GM_count == 1) {
  741. ShowStatus(" of which is 1 GM account, and ");
  742. sprintf(str, "%s of which is 1 GM account and", line);
  743. } else {
  744. ShowStatus(" of which is %d GM accounts, and ", GM_count);
  745. sprintf(str, "%s of which is %d GM accounts and", line, GM_count);
  746. }
  747. if (server_count == 0) {
  748. printf("no server account ('S').\n");
  749. sprintf(line, "%s no server account ('S').", str);
  750. } else if (server_count == 1) {
  751. printf("1 server account ('S').\n");
  752. sprintf(line, "%s 1 server account ('S').", str);
  753. } else {
  754. printf("%d server accounts ('S').\n", server_count);
  755. sprintf(line, "%s %d server accounts ('S').", str, server_count);
  756. }
  757. }
  758. login_log("%s\n", line);
  759. return 0;
  760. }
  761. //------------------------------------------
  762. // Writing of the accounts database file
  763. // (accounts are sorted by id before save)
  764. //------------------------------------------
  765. void mmo_auth_sync(void)
  766. {
  767. FILE *fp;
  768. unsigned int i, j, k;
  769. int lock;
  770. uint32 account_id;
  771. CREATE_BUFFER(id, int, auth_num);
  772. char line[65536];
  773. // Sorting before save
  774. for(i = 0; i < auth_num; i++) {
  775. id[i] = i;
  776. account_id = auth_dat[i].account_id;
  777. for(j = 0; j < i; j++) {
  778. if (account_id < auth_dat[id[j]].account_id) {
  779. for(k = i; k > j; k--)
  780. id[k] = id[k-1];
  781. id[j] = i; // id[i]
  782. break;
  783. }
  784. }
  785. }
  786. // Data save
  787. if ((fp = lock_fopen(account_filename, &lock)) == NULL) {
  788. //if (id) aFree(id); // aFree, right?
  789. DELETE_BUFFER(id);
  790. return;
  791. }
  792. fprintf(fp, "// Accounts file: here are saved all information about the accounts.\n");
  793. fprintf(fp, "// Structure: ID, account name, password, last login time, sex, # of logins, state, email, error message for state 7, validity time, last (accepted) login ip, memo field, ban timestamp, repeated(register text, register value)\n");
  794. fprintf(fp, "// Some explanations:\n");
  795. fprintf(fp, "// account name : between 4 to 23 char for a normal account (standard client can't send less than 4 char).\n");
  796. fprintf(fp, "// account password: between 4 to 23 char\n");
  797. fprintf(fp, "// sex : M or F for normal accounts, S for server accounts\n");
  798. fprintf(fp, "// state : 0: account is ok, 1 to 256: error code of packet 0x006a + 1\n");
  799. fprintf(fp, "// email : between 3 to 39 char (a@a.com is like no email)\n");
  800. fprintf(fp, "// error message : text for the state 7: 'Your are Prohibited to login until <text>'. Max 19 char\n");
  801. fprintf(fp, "// valitidy time : 0: unlimited account, <other value>: date calculated by addition of 1/1/1970 + value (number of seconds since the 1/1/1970)\n");
  802. fprintf(fp, "// memo field : max 254 char\n");
  803. fprintf(fp, "// ban time : 0: no ban, <other value>: banned until the date: date calculated by addition of 1/1/1970 + value (number of seconds since the 1/1/1970)\n");
  804. for(i = 0; i < auth_num; i++) {
  805. k = id[i]; // use of sorted index
  806. if (auth_dat[k].account_id == (uint32)-1)
  807. continue;
  808. mmo_auth_tostr(line, &auth_dat[k]);
  809. fprintf(fp, "%s\n", line);
  810. }
  811. fprintf(fp, "%u\t%%newid%%\n", account_id_count);
  812. lock_fclose(fp, account_filename, &lock);
  813. // set new counter to minimum number of auth before save
  814. auth_before_save_file = auth_num / AUTH_SAVE_FILE_DIVIDER; // Re-initialise counter. We have save.
  815. if (auth_before_save_file < AUTH_BEFORE_SAVE_FILE)
  816. auth_before_save_file = AUTH_BEFORE_SAVE_FILE;
  817. //if (id) aFree(id);
  818. DELETE_BUFFER(id);
  819. return;
  820. }
  821. //-----------------------------------------------------
  822. // Check if we must save accounts file or not
  823. // every minute, we check if we must save because we
  824. // have do some authentifications without arrive to
  825. // the minimum of authentifications for the save.
  826. // Note: all other modification of accounts (deletion,
  827. // change of some informations excepted lastip/
  828. // lastlogintime, creation) are always save
  829. // immediatly and set the minimum of
  830. // authentifications to its initialization value.
  831. //-----------------------------------------------------
  832. int check_auth_sync(int tid, unsigned int tick, int id, int data)
  833. {
  834. // we only save if necessary:
  835. // we have do some authentifications without do saving
  836. if (auth_before_save_file < AUTH_BEFORE_SAVE_FILE ||
  837. auth_before_save_file < (int)(auth_num / AUTH_SAVE_FILE_DIVIDER))
  838. mmo_auth_sync();
  839. return 0;
  840. }
  841. //--------------------------------------------------------------------
  842. // Packet send to all char-servers, except one (wos: without our self)
  843. //--------------------------------------------------------------------
  844. int charif_sendallwos(int sfd, unsigned char *buf, unsigned int len)
  845. {
  846. int i, c, fd;
  847. for(i = 0, c = 0; i < MAX_SERVERS; i++) {
  848. if ((fd = server_fd[i]) >= 0 && fd != sfd) {
  849. WFIFOHEAD(fd,len);
  850. memcpy(WFIFOP(fd,0), buf, len);
  851. WFIFOSET(fd,len);
  852. c++;
  853. }
  854. }
  855. return c;
  856. }
  857. //-----------------------------------------------------
  858. // Send GM accounts to one or all char-servers
  859. //-----------------------------------------------------
  860. void send_GM_accounts(int fd)
  861. {
  862. unsigned int i;
  863. uint8 buf[32767];
  864. uint16 len;
  865. len = 4;
  866. WBUFW(buf,0) = 0x2732;
  867. for(i = 0; i < GM_num; i++)
  868. // send only existing accounts. We can not create a GM account when server is online.
  869. if (gm_account_db[i].level > 0) {
  870. WBUFL(buf,len) = gm_account_db[i].account_id;
  871. WBUFB(buf,len+4) = (uint8)gm_account_db[i].level;
  872. len += 5;
  873. if (len >= 32000) {
  874. ShowWarning("send_GM_accounts: Too many accounts! Only %d out of %d were sent.\n", i, GM_num);
  875. break;
  876. }
  877. }
  878. WBUFW(buf,2) = len;
  879. if (fd == -1) // send to all charservers
  880. charif_sendallwos(-1, buf, len);
  881. else { // send only to target
  882. WFIFOHEAD(fd,len);
  883. memcpy(WFIFOP(fd,0), buf, len);
  884. WFIFOSET(fd,len);
  885. }
  886. return;
  887. }
  888. //-----------------------------------------------------
  889. // Check if GM file account have been changed
  890. //-----------------------------------------------------
  891. int check_GM_file(int tid, unsigned int tick, int id, int data)
  892. {
  893. struct stat file_stat;
  894. long new_time;
  895. // if we would not check
  896. if (gm_account_filename_check_timer < 1)
  897. return 0;
  898. // get last modify time/date
  899. if (stat(GM_account_filename, &file_stat))
  900. new_time = 0; // error
  901. else
  902. new_time = (long)file_stat.st_mtime;
  903. if (new_time != creation_time_GM_account_file) {
  904. read_gm_account();
  905. send_GM_accounts(-1);
  906. }
  907. return 0;
  908. }
  909. //-----------------------------------------------------
  910. // encrypted/unencrypted password check
  911. //-----------------------------------------------------
  912. bool check_encrypted(const char* str1, const char* str2, const char* passwd)
  913. {
  914. char md5str[64], md5bin[32];
  915. snprintf(md5str, sizeof(md5str), "%s%s", str1, str2);
  916. md5str[sizeof(md5str)-1] = '\0';
  917. MD5_String2binary(md5str, md5bin);
  918. return (0==memcmp(passwd, md5bin, 16));
  919. }
  920. bool check_password(struct login_session_data* ld, int passwdenc, const char* passwd, const char* refpass)
  921. {
  922. if(passwdenc == 0)
  923. {
  924. return (0==strcmp(passwd, refpass));
  925. }
  926. else if (ld)
  927. {
  928. // password mode set to 1 -> (md5key, refpass) enable with <passwordencrypt></passwordencrypt>
  929. // password mode set to 2 -> (refpass, md5key) enable with <passwordencrypt2></passwordencrypt2>
  930. return ((passwdenc&0x01) && check_encrypted(ld->md5key, refpass, passwd)) ||
  931. ((passwdenc&0x02) && check_encrypted(refpass, ld->md5key, passwd));
  932. }
  933. return false;
  934. }
  935. //-------------------------------------
  936. // Make new account
  937. //-------------------------------------
  938. int mmo_auth_new(struct mmo_account* account, char sex, char* email)
  939. {
  940. time_t timestamp, timestamp_temp;
  941. struct tm *tmtime;
  942. int i = auth_num;
  943. if (auth_num >= auth_max) {
  944. auth_max += 256;
  945. auth_dat = (struct auth_data*)aRealloc(auth_dat, sizeof(struct auth_data) * auth_max);
  946. }
  947. memset(&auth_dat[i], '\0', sizeof(struct auth_data));
  948. while (isGM(account_id_count) > 0)
  949. account_id_count++;
  950. auth_dat[i].account_id = account_id_count++;
  951. safestrncpy(auth_dat[i].userid, account->userid, NAME_LENGTH);
  952. if( login_config.use_md5_passwds )
  953. MD5_String(account->passwd, auth_dat[i].pass);
  954. else
  955. safestrncpy(auth_dat[i].pass, account->passwd, NAME_LENGTH);
  956. safestrncpy(auth_dat[i].lastlogin, "-", sizeof(auth_dat[i].lastlogin));
  957. auth_dat[i].sex = (sex == 'M' || sex == 'm');
  958. auth_dat[i].logincount = 0;
  959. auth_dat[i].state = 0;
  960. safestrncpy(auth_dat[i].email, e_mail_check(email) ? email : "a@a.com", sizeof(auth_dat[i].email));
  961. safestrncpy(auth_dat[i].error_message, "-", sizeof(auth_dat[i].error_message));
  962. auth_dat[i].ban_until_time = 0;
  963. if (start_limited_time < 0)
  964. auth_dat[i].connect_until_time = 0; // unlimited
  965. else { // limited time
  966. timestamp = time(NULL) + start_limited_time;
  967. // double conversion to be sure that it is possible
  968. tmtime = localtime(&timestamp);
  969. timestamp_temp = mktime(tmtime);
  970. if (timestamp_temp != -1 && (timestamp_temp + 3600) >= timestamp) // check possible value and overflow (and avoid summer/winter hour)
  971. auth_dat[i].connect_until_time = timestamp_temp;
  972. else
  973. auth_dat[i].connect_until_time = 0; // unlimited
  974. }
  975. strncpy(auth_dat[i].last_ip, "-", 16);
  976. strncpy(auth_dat[i].memo, "-", 255);
  977. auth_dat[i].account_reg2_num = 0;
  978. auth_num++;
  979. return (account_id_count - 1);
  980. }
  981. //-----------------------------------------------------
  982. // Check/authentication of a connection
  983. //-----------------------------------------------------
  984. int mmo_auth(struct mmo_account* account, int fd)
  985. {
  986. unsigned int i;
  987. time_t raw_time;
  988. char tmpstr[256];
  989. int len;
  990. int newaccount = 0;
  991. char user_password[32+1]; // reserve for md5-ed pw
  992. char ip[16];
  993. uint8* sin_addr = (uint8*)&session[fd]->client_addr;
  994. sprintf(ip, "%u.%u.%u.%u", sin_addr[3], sin_addr[2], sin_addr[1], sin_addr[0]);
  995. // DNS Blacklist check
  996. if( login_config.use_dnsbl )
  997. {
  998. char r_ip[16];
  999. char ip_dnsbl[256];
  1000. char* dnsbl_serv;
  1001. bool matched = false;
  1002. sprintf(r_ip, "%u.%u.%u.%u", sin_addr[0], sin_addr[1], sin_addr[2], sin_addr[3]);
  1003. for( dnsbl_serv = strtok(login_config.dnsbl_servs,","); !matched && dnsbl_serv != NULL; dnsbl_serv = strtok(NULL,",") )
  1004. {
  1005. sprintf(ip_dnsbl, "%s.%s", r_ip, dnsbl_serv);
  1006. if( host2ip(ip_dnsbl) )
  1007. matched = true;
  1008. }
  1009. if( matched )
  1010. {
  1011. ShowInfo("DNSBL: (%s) Blacklisted. User Kicked.\n", r_ip);
  1012. return 3;
  1013. }
  1014. }
  1015. //Client Version check
  1016. if( login_config.check_client_version && account->version != 0 &&
  1017. account->version != login_config.client_version_to_connect )
  1018. return 5;
  1019. len = strnlen(account->userid, NAME_LENGTH);
  1020. // Account creation with _M/_F
  1021. if( login_config.new_account_flag )
  1022. {
  1023. if( len > 2 && strnlen(account->passwd, NAME_LENGTH) > 0 && // valid user and password lengths
  1024. account->passwdenc == 0 && // unencoded password
  1025. account->userid[len-2] == '_' && memchr("FfMm", (unsigned char)account->userid[len-1], 4) && // _M/_F suffix
  1026. account_id_count <= END_ACCOUNT_NUM )
  1027. {
  1028. //only continue if amount in this time limit is allowed (account registration flood protection)[Kevin]
  1029. if(DIFF_TICK(gettick(), new_reg_tick) < 0 && num_regs >= allowed_regs) {
  1030. ShowNotice("Account registration denied (registration limit exceeded) to %s!\n", ip);
  1031. login_log("Notice: Account registration denied (registration limit exceeded) to %s!", ip);
  1032. return 3;
  1033. }
  1034. newaccount = 1;
  1035. account->userid[len-2] = '\0';
  1036. }
  1037. }
  1038. // Strict account search
  1039. ARR_FIND( 0, auth_num, i, strcmp(account->userid, auth_dat[i].userid) == 0 );
  1040. if( newaccount )
  1041. {
  1042. if( i != auth_num )
  1043. {
  1044. login_log("Attempt of creation of an already existant account (account: %s_%c, pass: %s, received pass: %s, ip: %s)\n", account->userid, account->userid[len-1], auth_dat[i].pass, account->passwd, ip);
  1045. return 1; // 1 = Incorrect Password
  1046. }
  1047. else
  1048. {
  1049. int new_id = mmo_auth_new(account, account->userid[len-1], "a@a.com");
  1050. unsigned int tick = gettick();
  1051. login_log("Account creation (account %s (id: %d), pass: %s, sex: %c, connection with _F/_M, ip: %s)\n", account->userid, new_id, account->passwd, account->userid[len-1], ip);
  1052. auth_before_save_file = 0; // Creation of an account -> save accounts file immediatly
  1053. if( DIFF_TICK(tick, new_reg_tick) > 0 )
  1054. { //Update the registration check.
  1055. num_regs = 0;
  1056. new_reg_tick=tick +time_allowed*1000;
  1057. }
  1058. num_regs++;
  1059. }
  1060. }
  1061. // if there is no creation request and strict account search fails, we do a no sensitive case research for index
  1062. if( !newaccount && i == auth_num )
  1063. {
  1064. i = search_account_index(account->userid);
  1065. if( i == -1 )
  1066. i = auth_num;
  1067. else
  1068. memcpy(account->userid, auth_dat[i].userid, NAME_LENGTH); // for the possible tests/checks afterwards (copy correcte sensitive case).
  1069. }
  1070. if( i == auth_num )
  1071. {
  1072. login_log("Unknown account (account: %s, received pass: %s, ip: %s)\n", account->userid, account->passwd, ip);
  1073. return 0; // 0 = Unregistered ID
  1074. }
  1075. if( login_config.use_md5_passwds )
  1076. MD5_String(account->passwd, user_password);
  1077. else
  1078. safestrncpy(user_password, account->passwd, NAME_LENGTH);
  1079. if( !check_password(session[fd]->session_data, account->passwdenc, user_password, auth_dat[i].pass) )
  1080. {
  1081. login_log("Invalid password (account: %s, pass: %s, received pass: %s, ip: %s)\n", account->userid, auth_dat[i].pass, (account->passwdenc) ? "[MD5]" : account->passwd, ip);
  1082. return 1; // 1 = Incorrect Password
  1083. }
  1084. if( auth_dat[i].connect_until_time != 0 && auth_dat[i].connect_until_time < time(NULL) )
  1085. {
  1086. login_log("Connection refused (account: %s, pass: %s, expired ID, ip: %s)\n", account->userid, account->passwd, ip);
  1087. return 2; // 2 = This ID is expired
  1088. }
  1089. if( auth_dat[i].ban_until_time != 0 && auth_dat[i].ban_until_time > time(NULL) )
  1090. {
  1091. strftime(tmpstr, 20, login_config.date_format, localtime(&auth_dat[i].ban_until_time));
  1092. tmpstr[19] = '\0';
  1093. login_log("Connection refused (account: %s, pass: %s, banned until %s, ip: %s)\n", account->userid, account->passwd, tmpstr, ip);
  1094. return 6; // 6 = Your are Prohibited to log in until %s
  1095. }
  1096. if( auth_dat[i].state )
  1097. {
  1098. login_log("Connection refused (account: %s, pass: %s, state: %d, ip: %s)\n", account->userid, account->passwd, auth_dat[i].state, ip);
  1099. return auth_dat[i].state - 1;
  1100. }
  1101. if( login_config.online_check )
  1102. {
  1103. struct online_login_data* data = idb_get(online_db,auth_dat[i].account_id);
  1104. if( data && data->char_server > -1 )
  1105. {
  1106. //Request char servers to kick this account out. [Skotlex]
  1107. unsigned char buf[8];
  1108. ShowNotice("User [%d] is already online - Rejected.\n",auth_dat[i].account_id);
  1109. WBUFW(buf,0) = 0x2734;
  1110. WBUFL(buf,2) = auth_dat[i].account_id;
  1111. charif_sendallwos(-1, buf, 6);
  1112. if( data->waiting_disconnect == -1 )
  1113. data->waiting_disconnect = add_timer(gettick()+30000, waiting_disconnect_timer, auth_dat[i].account_id, 0);
  1114. return 3; // Rejected
  1115. }
  1116. }
  1117. login_log("Authentification accepted (account: %s (id: %d), ip: %s)\n", account->userid, auth_dat[i].account_id, ip);
  1118. // auth start : time seed
  1119. time(&raw_time);
  1120. strftime(tmpstr, 24, "%Y-%m-%d %H:%M:%S",localtime(&raw_time));
  1121. account->account_id = auth_dat[i].account_id;
  1122. account->login_id1 = rand();
  1123. account->login_id2 = rand();
  1124. safestrncpy(account->lastlogin, auth_dat[i].lastlogin, 24);
  1125. account->sex = auth_dat[i].sex;
  1126. if( account->sex != 2 && account->account_id < START_ACCOUNT_NUM )
  1127. 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);
  1128. safestrncpy(auth_dat[i].lastlogin, tmpstr, sizeof(auth_dat[i].lastlogin));
  1129. safestrncpy(auth_dat[i].last_ip, ip, sizeof(auth_dat[i].last_ip));
  1130. auth_dat[i].ban_until_time = 0;
  1131. auth_dat[i].logincount++;
  1132. // Save until for change ip/time of auth is not very useful => limited save for that
  1133. // Save there informations isnot necessary, because they are saved in log file.
  1134. if (--auth_before_save_file <= 0) // Reduce counter. 0 or less, we save
  1135. mmo_auth_sync();
  1136. return -1; // account OK
  1137. }
  1138. static int online_db_setoffline(DBKey key, void* data, va_list ap)
  1139. {
  1140. struct online_login_data* p = (struct online_login_data*)data;
  1141. int server = va_arg(ap, int);
  1142. if( server == -1 )
  1143. {
  1144. p->char_server = -1;
  1145. if( p->waiting_disconnect != -1 )
  1146. {
  1147. delete_timer(p->waiting_disconnect, waiting_disconnect_timer);
  1148. p->waiting_disconnect = -1;
  1149. }
  1150. }
  1151. else if( p->char_server == server )
  1152. p->char_server = -2; //Char server disconnected.
  1153. return 0;
  1154. }
  1155. //--------------------------------
  1156. // Packet parsing for char-servers
  1157. //--------------------------------
  1158. int parse_fromchar(int fd)
  1159. {
  1160. uint32 i;
  1161. int j, id;
  1162. uint32 ipl = session[fd]->client_addr;
  1163. char ip[16];
  1164. ip2str(ipl, ip);
  1165. ARR_FIND( 0, MAX_SERVERS, id, server_fd[id] == fd );
  1166. if (id == MAX_SERVERS)
  1167. {// not a char server
  1168. set_eof(fd);
  1169. do_close(fd);
  1170. return 0;
  1171. }
  1172. if( session[fd]->eof )
  1173. {
  1174. ShowStatus("Char-server '%s' has disconnected.\n", server[id].name);
  1175. login_log("Char-server '%s' has disconnected (ip: %s).\n", server[id].name, ip);
  1176. server_fd[id] = -1;
  1177. memset(&server[id], 0, sizeof(struct mmo_char_server));
  1178. online_db->foreach(online_db, online_db_setoffline, id); //Set all chars from this char server to offline.
  1179. do_close(fd);
  1180. return 0;
  1181. }
  1182. while( RFIFOREST(fd) >= 2 )
  1183. {
  1184. uint16 command = RFIFOW(fd,0);
  1185. if (display_parse_fromchar == 2 || (display_parse_fromchar == 1 && command != 0x2714)) // 0x2714 is done very often (number of players)
  1186. ShowDebug("parse_fromchar: connection #%d, packet: 0x%x (with being read: %d bytes).\n", fd, command, RFIFOREST(fd));
  1187. switch( command )
  1188. {
  1189. case 0x2709: // request from map-server via char-server to reload GM accounts
  1190. login_log("Char-server '%s': Request to re-load GM configuration file (ip: %s).\n", server[id].name, ip);
  1191. read_gm_account();
  1192. // send GM accounts to all char-servers
  1193. send_GM_accounts(-1);
  1194. RFIFOSKIP(fd,2);
  1195. break;
  1196. case 0x2712: // request from char-server to authenticate an account
  1197. if( RFIFOREST(fd) < 19 )
  1198. return 0;
  1199. {
  1200. uint32 account_id = RFIFOL(fd,2);
  1201. for( i = 0; i < AUTH_FIFO_SIZE; ++i )
  1202. {
  1203. if( auth_fifo[i].account_id == RFIFOL(fd,2) &&
  1204. auth_fifo[i].login_id1 == RFIFOL(fd,6) &&
  1205. auth_fifo[i].login_id2 == RFIFOL(fd,10) &&
  1206. auth_fifo[i].sex == RFIFOB(fd,14) &&
  1207. auth_fifo[i].ip == ntohl(RFIFOL(fd,15)) &&
  1208. !auth_fifo[i].delflag)
  1209. {
  1210. auth_fifo[i].delflag = 1;
  1211. break;
  1212. }
  1213. }
  1214. if( i != AUTH_FIFO_SIZE && account_id > 0 )
  1215. {// send ack
  1216. time_t connect_until_time;
  1217. char email[40];
  1218. unsigned int k;
  1219. login_log("Char-server '%s': authentification of the account %d accepted (ip: %s).\n", server[id].name, account_id, ip);
  1220. ARR_FIND( 0, auth_num, k, auth_dat[k].account_id == account_id );
  1221. if( k != auth_num ) {
  1222. strcpy(email, auth_dat[k].email);
  1223. connect_until_time = auth_dat[k].connect_until_time;
  1224. } else {
  1225. memset(email, 0, sizeof(email));
  1226. connect_until_time = 0;
  1227. }
  1228. WFIFOHEAD(fd,51);
  1229. WFIFOW(fd,0) = 0x2713;
  1230. WFIFOL(fd,2) = account_id;
  1231. WFIFOB(fd,6) = 0;
  1232. memcpy(WFIFOP(fd, 7), email, 40);
  1233. WFIFOL(fd,47) = (unsigned long)connect_until_time;
  1234. WFIFOSET(fd,51);
  1235. }
  1236. else
  1237. {// authentification not found
  1238. login_log("Char-server '%s': authentification of the account %d REFUSED (ip: %s).\n", server[id].name, account_id, ip);
  1239. WFIFOHEAD(fd,51);
  1240. WFIFOW(fd,0) = 0x2713;
  1241. WFIFOL(fd,2) = account_id;
  1242. WFIFOB(fd,6) = 1;
  1243. // It is unnecessary to send email
  1244. // It is unnecessary to send validity date of the account
  1245. WFIFOSET(fd,51);
  1246. }
  1247. RFIFOSKIP(fd,19);
  1248. }
  1249. break;
  1250. case 0x2714:
  1251. if (RFIFOREST(fd) < 6)
  1252. return 0;
  1253. //printf("parse_fromchar: Receiving of the users number of the server '%s': %d\n", server[id].name, RFIFOL(fd,2));
  1254. server[id].users = RFIFOL(fd,2);
  1255. // send some answer
  1256. WFIFOHEAD(fd,2);
  1257. WFIFOW(fd,0) = 0x2718;
  1258. WFIFOSET(fd,2);
  1259. RFIFOSKIP(fd,6);
  1260. break;
  1261. case 0x2715: // request from char server to change e-email from default "a@a.com"
  1262. if (RFIFOREST(fd) < 46)
  1263. return 0;
  1264. {
  1265. char email[40];
  1266. uint32 acc = RFIFOL(fd,2);
  1267. memcpy(email, RFIFOP(fd,6), 40); email[39] = '\0';
  1268. remove_control_chars(email);
  1269. if (e_mail_check(email) == 0)
  1270. login_log("Char-server '%s': Attempt to create an e-mail on an account with a default e-mail REFUSED - e-mail is invalid (account: %d, ip: %s)\n", server[id].name, acc, ip);
  1271. else {
  1272. for(i = 0; i < auth_num; i++) {
  1273. if (auth_dat[i].account_id == acc && (strcmp(auth_dat[i].email, "a@a.com") == 0 || auth_dat[i].email[0] == '\0')) {
  1274. memcpy(auth_dat[i].email, email, 40);
  1275. login_log("Char-server '%s': Create an e-mail on an account with a default e-mail (account: %d, new e-mail: %s, ip: %s).\n", server[id].name, acc, email, ip);
  1276. // Save
  1277. mmo_auth_sync();
  1278. break;
  1279. }
  1280. }
  1281. if (i == auth_num)
  1282. login_log("Char-server '%s': Attempt to create an e-mail on an account with a default e-mail REFUSED - account doesn't exist or e-mail of account isn't default e-mail (account: %d, ip: %s).\n", server[id].name, acc, ip);
  1283. }
  1284. RFIFOSKIP(fd,46);
  1285. }
  1286. break;
  1287. case 0x2716: // received an e-mail/limited time request, because a player comes back from a map-server to the char-server
  1288. if (RFIFOREST(fd) < 6)
  1289. return 0;
  1290. for(i = 0; i < auth_num; i++) {
  1291. if (auth_dat[i].account_id == RFIFOL(fd,2)) {
  1292. login_log("Char-server '%s': e-mail of the account %d found (ip: %s).\n",
  1293. server[id].name, RFIFOL(fd,2), ip);
  1294. WFIFOW(fd,0) = 0x2717;
  1295. WFIFOL(fd,2) = RFIFOL(fd,2);
  1296. memcpy(WFIFOP(fd, 6), auth_dat[i].email, 40);
  1297. WFIFOL(fd,46) = (unsigned long)auth_dat[i].connect_until_time;
  1298. WFIFOSET(fd,50);
  1299. break;
  1300. }
  1301. }
  1302. if (i == auth_num)
  1303. login_log("Char-server '%s': e-mail of the account %d NOT found (ip: %s).\n",
  1304. server[id].name, RFIFOL(fd,2), ip);
  1305. RFIFOSKIP(fd,6);
  1306. break;
  1307. case 0x2720: // Request to become a GM
  1308. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  1309. return 0;
  1310. {
  1311. unsigned char buf[10];
  1312. FILE *fp;
  1313. int acc = RFIFOL(fd,4);
  1314. //printf("parse_fromchar: Request to become a GM acount from %d account.\n", acc);
  1315. WBUFW(buf,0) = 0x2721;
  1316. WBUFL(buf,2) = acc;
  1317. WBUFL(buf,6) = 0;
  1318. if (strcmp((char*)RFIFOP(fd,8), gm_pass) == 0) {
  1319. // only non-GM can become GM
  1320. if (isGM(acc) == 0) {
  1321. // if we autorise creation
  1322. if (level_new_gm > 0) {
  1323. // if we can open the file to add the new GM
  1324. if ((fp = fopen(GM_account_filename, "a")) != NULL) {
  1325. char tmpstr[24];
  1326. time_t raw_time;
  1327. time(&raw_time);
  1328. strftime(tmpstr, 23, login_config.date_format, localtime(&raw_time));
  1329. fprintf(fp, "\n// %s: @GM command on account %d\n%d %d\n", tmpstr, acc, acc, level_new_gm);
  1330. fclose(fp);
  1331. WBUFL(buf,6) = level_new_gm;
  1332. read_gm_account();
  1333. send_GM_accounts(-1);
  1334. ShowNotice("GM Change of the account %d: level 0 -> %d.\n", acc, level_new_gm);
  1335. login_log("Char-server '%s': GM Change of the account %d: level 0 -> %d (ip: %s).\n",
  1336. server[id].name, acc, level_new_gm, ip);
  1337. } else {
  1338. ShowError("Error of GM change (suggested account: %d, correct password, unable to add a GM account in GM accounts file)\n", acc);
  1339. login_log("Char-server '%s': Error of GM change (suggested account: %d, correct password, unable to add a GM account in GM accounts file, ip: %s).\n",
  1340. server[id].name, acc, ip);
  1341. }
  1342. } else {
  1343. ShowError("Error of GM change (suggested account: %d, correct password, but GM creation is disable (level_new_gm = 0))\n", acc);
  1344. login_log("Char-server '%s': Error of GM change (suggested account: %d, correct password, but GM creation is disable (level_new_gm = 0), ip: %s).\n",
  1345. server[id].name, acc, ip);
  1346. }
  1347. } else {
  1348. ShowError("Error of GM change (suggested account: %d (already GM), correct password).\n", acc);
  1349. login_log("Char-server '%s': Error of GM change (suggested account: %d (already GM), correct password, ip: %s).\n",
  1350. server[id].name, acc, ip);
  1351. }
  1352. } else {
  1353. ShowError("Error of GM change (suggested account: %d, invalid password).\n", acc);
  1354. login_log("Char-server '%s': Error of GM change (suggested account: %d, invalid password, ip: %s).\n",
  1355. server[id].name, acc, ip);
  1356. }
  1357. charif_sendallwos(-1, buf, 10);
  1358. RFIFOSKIP(fd, RFIFOW(fd,2));
  1359. return 0;
  1360. }
  1361. // Map server send information to change an email of an account via char-server
  1362. case 0x2722: // 0x2722 <account_id>.L <actual_e-mail>.40B <new_e-mail>.40B
  1363. if (RFIFOREST(fd) < 86)
  1364. return 0;
  1365. {
  1366. char actual_email[40], new_email[40];
  1367. uint32 acc = RFIFOL(fd,2);
  1368. memcpy(actual_email, RFIFOP(fd,6), 40); actual_email[39] = '\0'; remove_control_chars(actual_email);
  1369. memcpy(new_email, RFIFOP(fd,46), 40); new_email[39] = '\0'; remove_control_chars(new_email);
  1370. if (e_mail_check(actual_email) == 0)
  1371. login_log("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",
  1372. server[id].name, acc, ip);
  1373. else if (e_mail_check(new_email) == 0)
  1374. login_log("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",
  1375. server[id].name, acc, ip);
  1376. else if (strcmpi(new_email, "a@a.com") == 0)
  1377. login_log("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",
  1378. server[id].name, acc, ip);
  1379. else {
  1380. for(i = 0; i < auth_num; i++) {
  1381. if (auth_dat[i].account_id == acc) {
  1382. if (strcmpi(auth_dat[i].email, actual_email) == 0) {
  1383. memcpy(auth_dat[i].email, new_email, 40);
  1384. login_log("Char-server '%s': Modify an e-mail on an account (@email GM command) (account: %d (%s), new e-mail: %s, ip: %s).\n",
  1385. server[id].name, acc, auth_dat[i].userid, new_email, ip);
  1386. // Save
  1387. mmo_auth_sync();
  1388. } else
  1389. login_log("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",
  1390. server[id].name, acc, auth_dat[i].userid, auth_dat[i].email, actual_email, ip);
  1391. break;
  1392. }
  1393. }
  1394. if (i == auth_num)
  1395. login_log("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",
  1396. server[id].name, acc, ip);
  1397. }
  1398. RFIFOSKIP(fd, 86);
  1399. }
  1400. break;
  1401. case 0x2724: // Receiving of map-server via char-server a status change request
  1402. if (RFIFOREST(fd) < 10)
  1403. return 0;
  1404. {
  1405. uint32 acc, statut;
  1406. acc = RFIFOL(fd,2);
  1407. statut = RFIFOL(fd,6);
  1408. for(i = 0; i < auth_num && auth_dat[i].account_id != acc; i++);
  1409. if (i == auth_num) {
  1410. login_log("Char-server '%s': Error of Status change (account: %d not found, suggested status %d, ip: %s).\n",
  1411. server[id].name, acc, statut, ip);
  1412. } else {
  1413. if (auth_dat[i].state != statut) {
  1414. login_log("Char-server '%s': Status change (account: %d, new status %d, ip: %s).\n",
  1415. server[id].name, acc, statut, ip);
  1416. if (statut != 0) {
  1417. unsigned char buf[16];
  1418. WBUFW(buf,0) = 0x2731;
  1419. WBUFL(buf,2) = acc;
  1420. WBUFB(buf,6) = 0; // 0: change of statut, 1: ban
  1421. WBUFL(buf,7) = statut; // status or final date of a banishment
  1422. charif_sendallwos(-1, buf, 11);
  1423. for(j = 0; j < AUTH_FIFO_SIZE; j++)
  1424. if (auth_fifo[j].account_id == acc)
  1425. auth_fifo[j].login_id1++; // to avoid reconnection error when come back from map-server (char-server will ask again the authentification)
  1426. }
  1427. auth_dat[i].state = statut;
  1428. // Save
  1429. mmo_auth_sync();
  1430. } else
  1431. login_log("Char-server '%s': Error of Status change - actual status is already the good status (account: %d, status %d, ip: %s).\n",
  1432. server[id].name, acc, statut, ip);
  1433. }
  1434. RFIFOSKIP(fd,10);
  1435. return 0;
  1436. }
  1437. case 0x2725: // Receiving of map-server via char-server a ban request
  1438. if (RFIFOREST(fd) < 18)
  1439. return 0;
  1440. {
  1441. uint32 acc = RFIFOL(fd,2);
  1442. for(i = 0; i < auth_num; i++) {
  1443. if (auth_dat[i].account_id == acc) {
  1444. time_t timestamp;
  1445. struct tm *tmtime;
  1446. if (auth_dat[i].ban_until_time == 0 || auth_dat[i].ban_until_time < time(NULL))
  1447. timestamp = time(NULL);
  1448. else
  1449. timestamp = auth_dat[i].ban_until_time;
  1450. tmtime = localtime(&timestamp);
  1451. tmtime->tm_year = tmtime->tm_year + (short)RFIFOW(fd,6);
  1452. tmtime->tm_mon = tmtime->tm_mon + (short)RFIFOW(fd,8);
  1453. tmtime->tm_mday = tmtime->tm_mday + (short)RFIFOW(fd,10);
  1454. tmtime->tm_hour = tmtime->tm_hour + (short)RFIFOW(fd,12);
  1455. tmtime->tm_min = tmtime->tm_min + (short)RFIFOW(fd,14);
  1456. tmtime->tm_sec = tmtime->tm_sec + (short)RFIFOW(fd,16);
  1457. timestamp = mktime(tmtime);
  1458. if (timestamp != -1) {
  1459. if (timestamp <= time(NULL))
  1460. timestamp = 0;
  1461. if (auth_dat[i].ban_until_time != timestamp) {
  1462. if (timestamp != 0) {
  1463. unsigned char buf[16];
  1464. char tmpstr[2048];
  1465. strftime(tmpstr, 24, login_config.date_format, localtime(&timestamp));
  1466. login_log("Char-server '%s': Ban request (account: %d, new final date of banishment: %d (%s), ip: %s).\n",
  1467. server[id].name, acc, timestamp, (timestamp == 0 ? "no banishment" : tmpstr), ip);
  1468. WBUFW(buf,0) = 0x2731;
  1469. WBUFL(buf,2) = auth_dat[i].account_id;
  1470. WBUFB(buf,6) = 1; // 0: change of statut, 1: ban
  1471. WBUFL(buf,7) = (unsigned int)timestamp; // status or final date of a banishment
  1472. charif_sendallwos(-1, buf, 11);
  1473. for(j = 0; j < AUTH_FIFO_SIZE; j++)
  1474. if (auth_fifo[j].account_id == acc)
  1475. auth_fifo[j].login_id1++; // to avoid reconnection error when come back from map-server (char-server will ask again the authentification)
  1476. } else {
  1477. login_log("Char-server '%s': Error of ban request (account: %d, new date unbans the account, ip: %s).\n",
  1478. server[id].name, acc, ip);
  1479. }
  1480. auth_dat[i].ban_until_time = timestamp;
  1481. // Save
  1482. mmo_auth_sync();
  1483. } else {
  1484. login_log("Char-server '%s': Error of ban request (account: %d, no change for ban date, ip: %s).\n",
  1485. server[id].name, acc, ip);
  1486. }
  1487. } else {
  1488. login_log("Char-server '%s': Error of ban request (account: %d, invalid date, ip: %s).\n",
  1489. server[id].name, acc, ip);
  1490. }
  1491. break;
  1492. }
  1493. }
  1494. if (i == auth_num)
  1495. login_log("Char-server '%s': Error of ban request (account: %d not found, ip: %s).\n",
  1496. server[id].name, acc, ip);
  1497. RFIFOSKIP(fd,18);
  1498. return 0;
  1499. }
  1500. case 0x2727: // Change of sex (sex is reversed)
  1501. if (RFIFOREST(fd) < 6)
  1502. return 0;
  1503. {
  1504. uint8 sex;
  1505. uint32 acc = RFIFOL(fd,2);
  1506. for(i = 0; i < auth_num; i++) {
  1507. if (auth_dat[i].account_id == acc) {
  1508. if (auth_dat[i].sex == 2)
  1509. login_log("Char-server '%s': Error of sex change - Server account (suggested account: %d, actual sex %d (Server), ip: %s).\n",
  1510. server[id].name, acc, auth_dat[i].sex, ip);
  1511. else {
  1512. unsigned char buf[16];
  1513. if (auth_dat[i].sex == 0)
  1514. sex = 1;
  1515. else
  1516. sex = 0;
  1517. login_log("Char-server '%s': Sex change (account: %d, new sex %c, ip: %s).\n",
  1518. server[id].name, acc, (sex == 2) ? 'S' : (sex == 1 ? 'M' : 'F'), ip);
  1519. for(j = 0; j < AUTH_FIFO_SIZE; j++)
  1520. if (auth_fifo[j].account_id == acc)
  1521. auth_fifo[j].login_id1++; // to avoid reconnection error when come back from map-server (char-server will ask again the authentification)
  1522. auth_dat[i].sex = sex;
  1523. WBUFW(buf,0) = 0x2723;
  1524. WBUFL(buf,2) = acc;
  1525. WBUFB(buf,6) = sex;
  1526. charif_sendallwos(-1, buf, 7);
  1527. // Save
  1528. mmo_auth_sync();
  1529. }
  1530. break;
  1531. }
  1532. }
  1533. if (i == auth_num)
  1534. login_log("Char-server '%s': Error of sex change (account: %d not found, sex would be reversed, ip: %s).\n",
  1535. server[id].name, acc, ip);
  1536. RFIFOSKIP(fd,6);
  1537. return 0;
  1538. }
  1539. case 0x2728: // We receive account_reg2 from a char-server, and we send them to other map-servers.
  1540. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  1541. return 0;
  1542. {
  1543. int p;
  1544. uint32 acc = RFIFOL(fd,4);
  1545. for(i = 0; i < auth_num; i++) {
  1546. if (auth_dat[i].account_id == acc) {
  1547. //unsigned char buf[rfifow(fd,2)+1];
  1548. unsigned char *buf;
  1549. int len;
  1550. buf = (unsigned char*)aCalloc(RFIFOW(fd,2)+1, sizeof(unsigned char));
  1551. login_log("char-server '%s': receiving (from the char-server) of account_reg2 (account: %d, ip: %s).\n",
  1552. server[id].name, acc, ip);
  1553. for(j=0,p=13;j<ACCOUNT_REG2_NUM && p<RFIFOW(fd,2);j++){
  1554. sscanf((char*)RFIFOP(fd,p), "%31c%n",auth_dat[i].account_reg2[j].str,&len);
  1555. auth_dat[i].account_reg2[j].str[len]='\0';
  1556. p +=len+1; //+1 to skip the '\0' between strings.
  1557. sscanf((char*)RFIFOP(fd,p), "%255c%n",auth_dat[i].account_reg2[j].value,&len);
  1558. auth_dat[i].account_reg2[j].value[len]='\0';
  1559. p +=len+1;
  1560. remove_control_chars(auth_dat[i].account_reg2[j].str);
  1561. remove_control_chars(auth_dat[i].account_reg2[j].value);
  1562. }
  1563. auth_dat[i].account_reg2_num = j;
  1564. // Sending information towards the other char-servers.
  1565. memcpy(WBUFP(buf,0), RFIFOP(fd,0), RFIFOW(fd,2));
  1566. WBUFW(buf,0) = 0x2729;
  1567. charif_sendallwos(fd, buf, WBUFW(buf,2));
  1568. // Save
  1569. mmo_auth_sync();
  1570. // printf("parse_fromchar: receiving (from the char-server) of account_reg2 (account id: %d).\n", acc);
  1571. if (buf) aFree(buf);
  1572. break;
  1573. }
  1574. }
  1575. if (i == auth_num) {
  1576. // printf("parse_fromchar: receiving (from the char-server) of account_reg2 (unknwon account id: %d).\n", acc);
  1577. login_log("Char-server '%s': receiving (from the char-server) of account_reg2 (account: %d not found, ip: %s).\n",
  1578. server[id].name, acc, ip);
  1579. }
  1580. }
  1581. RFIFOSKIP(fd,RFIFOW(fd,2));
  1582. break;
  1583. case 0x272a: // Receiving of map-server via char-server an unban request
  1584. if (RFIFOREST(fd) < 6)
  1585. return 0;
  1586. {
  1587. uint32 acc = RFIFOL(fd,2);
  1588. for(i = 0; i < auth_num; i++) {
  1589. if (auth_dat[i].account_id == acc) {
  1590. if (auth_dat[i].ban_until_time != 0) {
  1591. auth_dat[i].ban_until_time = 0;
  1592. login_log("Char-server '%s': UnBan request (account: %d, ip: %s).\n",
  1593. server[id].name, acc, ip);
  1594. } else {
  1595. login_log("Char-server '%s': Error of UnBan request (account: %d, no change for unban date, ip: %s).\n",
  1596. server[id].name, acc, ip);
  1597. }
  1598. break;
  1599. }
  1600. }
  1601. if (i == auth_num)
  1602. login_log("Char-server '%s': Error of UnBan request (account: %d not found, ip: %s).\n",
  1603. server[id].name, acc, ip);
  1604. RFIFOSKIP(fd,6);
  1605. return 0;
  1606. }
  1607. case 0x272b: // Set account_id to online [Wizputer]
  1608. if (RFIFOREST(fd) < 6)
  1609. return 0;
  1610. add_online_user(id, RFIFOL(fd,2));
  1611. RFIFOSKIP(fd,6);
  1612. break;
  1613. case 0x272c: // Set account_id to offline [Wizputer]
  1614. if (RFIFOREST(fd) < 6)
  1615. return 0;
  1616. remove_online_user(RFIFOL(fd,2));
  1617. RFIFOSKIP(fd,6);
  1618. break;
  1619. case 0x272d: // Receive list of all online accounts. [Skotlex]
  1620. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  1621. return 0;
  1622. if( login_config.online_check )
  1623. {
  1624. struct online_login_data *p;
  1625. int aid;
  1626. uint32 i, users;
  1627. online_db->foreach(online_db, online_db_setoffline, id); //Set all chars from this char-server offline first
  1628. users = RFIFOW(fd,4);
  1629. for (i = 0; i < users; i++) {
  1630. aid = RFIFOL(fd,6+i*4);
  1631. p = idb_ensure(online_db, aid, create_online_user);
  1632. p->char_server = id;
  1633. if (p->waiting_disconnect != -1)
  1634. {
  1635. delete_timer(p->waiting_disconnect, waiting_disconnect_timer);
  1636. p->waiting_disconnect = -1;
  1637. }
  1638. }
  1639. }
  1640. RFIFOSKIP(fd,RFIFOW(fd,2));
  1641. break;
  1642. case 0x272e: //Request account_reg2 for a character.
  1643. if (RFIFOREST(fd) < 10)
  1644. return 0;
  1645. {
  1646. uint32 account_id = RFIFOL(fd, 2);
  1647. uint32 char_id = RFIFOL(fd, 6);
  1648. int p;
  1649. WFIFOW(fd,0) = 0x2729;
  1650. WFIFOL(fd,4) = account_id;
  1651. WFIFOL(fd,8) = char_id;
  1652. WFIFOB(fd,12) = 1; //Type 1 for Account2 registry
  1653. for(i = 0; i < auth_num && auth_dat[i].account_id != account_id; i++);
  1654. if (i == auth_num) {
  1655. //Account not found? Send at least empty data, map servers need a reply!
  1656. WFIFOW(fd,2) = 13;
  1657. WFIFOSET(fd,WFIFOW(fd,2));
  1658. break;
  1659. }
  1660. for(p = 13, j = 0; j < auth_dat[i].account_reg2_num; j++) {
  1661. if (auth_dat[i].account_reg2[j].str[0]) {
  1662. p+= sprintf((char*)WFIFOP(fd,p), "%s", auth_dat[i].account_reg2[j].str)+1; //We add 1 to consider the '\0' in place.
  1663. p+= sprintf((char*)WFIFOP(fd,p), "%s", auth_dat[i].account_reg2[j].value)+1;
  1664. }
  1665. }
  1666. WFIFOW(fd,2) = (uint16) p;
  1667. WFIFOSET(fd,WFIFOW(fd,2));
  1668. RFIFOSKIP(fd,10);
  1669. }
  1670. break;
  1671. case 0x2736: // WAN IP update from char-server
  1672. if (RFIFOREST(fd) < 6)
  1673. return 0;
  1674. server[id].ip = ntohl(RFIFOL(fd,2));
  1675. ShowInfo("Updated IP of Server #%d to %d.%d.%d.%d.\n",id, CONVIP(server[id].ip));
  1676. RFIFOSKIP(fd,6);
  1677. break;
  1678. case 0x2737: //Request to set all offline.
  1679. ShowInfo("Setting accounts from char-server %d offline.\n", id);
  1680. online_db->foreach(online_db, online_db_setoffline, id);
  1681. RFIFOSKIP(fd,2);
  1682. break;
  1683. default:
  1684. {
  1685. FILE* logfp;
  1686. char tmpstr[24];
  1687. time_t raw_time;
  1688. logfp = fopen(login_log_unknown_packets_filename, "a");
  1689. if (logfp) {
  1690. time(&raw_time);
  1691. strftime(tmpstr, 23, login_config.date_format, localtime(&raw_time));
  1692. fprintf(logfp, "%s: receiving of an unknown packet -> disconnection\n", tmpstr);
  1693. fprintf(logfp, "parse_fromchar: connection #%d (ip: %s), packet: 0x%x (with being read: %lu).\n", fd, ip, command, (unsigned long)RFIFOREST(fd));
  1694. fprintf(logfp, "Detail (in hex):\n");
  1695. fprintf(logfp, "---- 00-01-02-03-04-05-06-07 08-09-0A-0B-0C-0D-0E-0F\n");
  1696. memset(tmpstr, '\0', sizeof(tmpstr));
  1697. for(i = 0; i < RFIFOREST(fd); i++) {
  1698. if ((i & 15) == 0)
  1699. fprintf(logfp, "%04X ",i);
  1700. fprintf(logfp, "%02x ", RFIFOB(fd,i));
  1701. if (RFIFOB(fd,i) > 0x1f)
  1702. tmpstr[i % 16] = RFIFOB(fd,i);
  1703. else
  1704. tmpstr[i % 16] = '.';
  1705. if ((i - 7) % 16 == 0) // -8 + 1
  1706. fprintf(logfp, " ");
  1707. else if ((i + 1) % 16 == 0) {
  1708. fprintf(logfp, " %s\n", tmpstr);
  1709. memset(tmpstr, '\0', sizeof(tmpstr));
  1710. }
  1711. }
  1712. if (i % 16 != 0) {
  1713. for(j = i; j % 16 != 0; j++) {
  1714. fprintf(logfp, " ");
  1715. if ((j - 7) % 16 == 0) // -8 + 1
  1716. fprintf(logfp, " ");
  1717. }
  1718. fprintf(logfp, " %s\n", tmpstr);
  1719. }
  1720. fprintf(logfp, "\n");
  1721. fclose(logfp);
  1722. }
  1723. ShowError("parse_fromchar: Unknown packet 0x%x from a char-server! Disconnecting!\n", command);
  1724. set_eof(fd);
  1725. return 0;
  1726. }
  1727. } // switch
  1728. } // while
  1729. RFIFOSKIP(fd,RFIFOREST(fd));
  1730. return 0;
  1731. }
  1732. //---------------------------------------
  1733. // Packet parsing for administation login
  1734. //---------------------------------------
  1735. int parse_admin(int fd)
  1736. {
  1737. unsigned int i, j;
  1738. char* account_name;
  1739. uint32 ipl = session[fd]->client_addr;
  1740. char ip[16];
  1741. ip2str(ipl, ip);
  1742. if( session[fd]->eof )
  1743. {
  1744. do_close(fd);
  1745. ShowInfo("Remote administration has disconnected (session #%d).\n", fd);
  1746. return 0;
  1747. }
  1748. while( RFIFOREST(fd) >= 2 )
  1749. {
  1750. uint16 command = RFIFOW(fd,0);
  1751. if (display_parse_admin)
  1752. ShowDebug("parse_admin: connection #%d, packet: 0x%x (with being read: %d).\n", fd, command, RFIFOREST(fd));
  1753. switch( command )
  1754. {
  1755. case 0x7530: // Request of the server version
  1756. login_log("'ladmin': Sending of the server version (ip: %s)\n", ip);
  1757. WFIFOHEAD(fd,10);
  1758. WFIFOW(fd,0) = 0x7531;
  1759. WFIFOB(fd,2) = ATHENA_MAJOR_VERSION;
  1760. WFIFOB(fd,3) = ATHENA_MINOR_VERSION;
  1761. WFIFOB(fd,4) = ATHENA_REVISION;
  1762. WFIFOB(fd,5) = ATHENA_RELEASE_FLAG;
  1763. WFIFOB(fd,6) = ATHENA_OFFICIAL_FLAG;
  1764. WFIFOB(fd,7) = ATHENA_SERVER_LOGIN;
  1765. WFIFOW(fd,8) = ATHENA_MOD_VERSION;
  1766. WFIFOSET(fd,10);
  1767. RFIFOSKIP(fd,2);
  1768. break;
  1769. case 0x7532: // Request of end of connection
  1770. login_log("'ladmin': End of connection (ip: %s)\n", ip);
  1771. RFIFOSKIP(fd,2);
  1772. set_eof(fd);
  1773. break;
  1774. case 0x7920: // Request of an accounts list
  1775. if (RFIFOREST(fd) < 10)
  1776. return 0;
  1777. {
  1778. int st, ed;
  1779. uint16 len;
  1780. CREATE_BUFFER(id, int, auth_num);
  1781. st = RFIFOL(fd,2);
  1782. ed = RFIFOL(fd,6);
  1783. RFIFOSKIP(fd,10);
  1784. WFIFOW(fd,0) = 0x7921;
  1785. if (st < 0)
  1786. st = 0;
  1787. if (ed > END_ACCOUNT_NUM || ed < st || ed <= 0)
  1788. ed = END_ACCOUNT_NUM;
  1789. login_log("'ladmin': Sending an accounts list (ask: from %d to %d, ip: %s)\n", st, ed, ip);
  1790. // Sort before send
  1791. for(i = 0; i < auth_num; i++) {
  1792. unsigned int k;
  1793. id[i] = i;
  1794. for(j = 0; j < i; j++) {
  1795. if (auth_dat[id[i]].account_id < auth_dat[id[j]].account_id) {
  1796. for(k = i; k > j; k--) {
  1797. id[k] = id[k-1];
  1798. }
  1799. id[j] = i; // id[i]
  1800. break;
  1801. }
  1802. }
  1803. }
  1804. // Sending accounts information
  1805. len = 4;
  1806. for(i = 0; i < auth_num && len < 30000; i++) {
  1807. int account_id = auth_dat[id[i]].account_id; // use sorted index
  1808. if (account_id >= st && account_id <= ed) {
  1809. j = id[i];
  1810. WFIFOL(fd,len) = account_id;
  1811. WFIFOB(fd,len+4) = (unsigned char)isGM(account_id);
  1812. memcpy(WFIFOP(fd,len+5), auth_dat[j].userid, 24);
  1813. WFIFOB(fd,len+29) = auth_dat[j].sex;
  1814. WFIFOL(fd,len+30) = auth_dat[j].logincount;
  1815. if (auth_dat[j].state == 0 && auth_dat[j].ban_until_time != 0) // if no state and banished
  1816. WFIFOL(fd,len+34) = 7; // 6 = Your are Prohibited to log in until %s
  1817. else
  1818. WFIFOL(fd,len+34) = auth_dat[j].state;
  1819. len += 38;
  1820. }
  1821. }
  1822. WFIFOW(fd,2) = len;
  1823. WFIFOSET(fd,len);
  1824. //if (id) free(id);
  1825. DELETE_BUFFER(id);
  1826. }
  1827. break;
  1828. case 0x7930: // Request for an account creation
  1829. if (RFIFOREST(fd) < 91)
  1830. return 0;
  1831. {
  1832. struct mmo_account ma;
  1833. memcpy(ma.userid,RFIFOP(fd, 2),NAME_LENGTH);
  1834. ma.userid[23] = '\0';
  1835. memcpy(ma.passwd, RFIFOP(fd, 26), NAME_LENGTH);
  1836. ma.passwd[23] = '\0';
  1837. memcpy(ma.lastlogin, "-", 2);
  1838. ma.sex = RFIFOB(fd,50);
  1839. WFIFOW(fd,0) = 0x7931;
  1840. WFIFOL(fd,2) = 0xffffffff;
  1841. memcpy(WFIFOP(fd,6), RFIFOP(fd,2), 24);
  1842. if (strlen(ma.userid) < 4 || strlen(ma.passwd) < 4) {
  1843. login_log("'ladmin': Attempt to create an invalid account (account or pass is too short, ip: %s)\n",
  1844. ip);
  1845. } else if (ma.sex != 'F' && ma.sex != 'M') {
  1846. login_log("'ladmin': Attempt to create an invalid account (account: %s, received pass: %s, invalid sex, ip: %s)\n",
  1847. ma.userid, ma.passwd, ip);
  1848. } else if (account_id_count > END_ACCOUNT_NUM) {
  1849. login_log("'ladmin': Attempt to create an account, but there is no more available id number (account: %s, pass: %s, sex: %c, ip: %s)\n",
  1850. ma.userid, ma.passwd, ma.sex, ip);
  1851. } else {
  1852. remove_control_chars(ma.userid);
  1853. remove_control_chars(ma.passwd);
  1854. for(i = 0; i < auth_num; i++) {
  1855. if (strncmp(auth_dat[i].userid, ma.userid, 24) == 0) {
  1856. login_log("'ladmin': Attempt to create an already existing account (account: %s, pass: %s, received pass: %s, ip: %s)\n",
  1857. auth_dat[i].userid, auth_dat[i].pass, ma.passwd, ip);
  1858. break;
  1859. }
  1860. }
  1861. if (i == auth_num) {
  1862. int new_id;
  1863. char email[40];
  1864. memcpy(email, RFIFOP(fd,51), 40);
  1865. email[39] = '\0';
  1866. remove_control_chars(email);
  1867. new_id = mmo_auth_new(&ma, ma.sex, email);
  1868. login_log("'ladmin': Account creation (account: %s (id: %d), pass: %s, sex: %c, email: %s, ip: %s)\n",
  1869. ma.userid, new_id, ma.passwd, ma.sex, auth_dat[i].email, ip);
  1870. WFIFOL(fd,2) = new_id;
  1871. mmo_auth_sync();
  1872. }
  1873. }
  1874. WFIFOSET(fd,30);
  1875. RFIFOSKIP(fd,91);
  1876. }
  1877. break;
  1878. case 0x7932: // Request for an account deletion
  1879. if (RFIFOREST(fd) < 26)
  1880. return 0;
  1881. WFIFOW(fd,0) = 0x7933;
  1882. WFIFOL(fd,2) = 0xFFFFFFFF;
  1883. account_name = (char*)RFIFOP(fd,2);
  1884. account_name[23] = '\0';
  1885. remove_control_chars(account_name);
  1886. i = search_account_index(account_name);
  1887. if (i != -1) {
  1888. // Char-server is notified of deletion (for characters deletion).
  1889. unsigned char buf[65535];
  1890. WBUFW(buf,0) = 0x2730;
  1891. WBUFL(buf,2) = auth_dat[i].account_id;
  1892. charif_sendallwos(-1, buf, 6);
  1893. // send answer
  1894. memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24);
  1895. WFIFOL(fd,2) = auth_dat[i].account_id;
  1896. // save deleted account in log file
  1897. login_log("'ladmin': Account deletion (account: %s, id: %d, ip: %s) - saved in next line:\n",
  1898. auth_dat[i].userid, auth_dat[i].account_id, ip);
  1899. mmo_auth_tostr((char*)buf, &auth_dat[i]);
  1900. login_log("%s\n", buf);
  1901. // delete account
  1902. memset(auth_dat[i].userid, '\0', sizeof(auth_dat[i].userid));
  1903. auth_dat[i].account_id = (uint32)-1;
  1904. mmo_auth_sync();
  1905. } else {
  1906. memcpy(WFIFOP(fd,6), account_name, 24);
  1907. login_log("'ladmin': Attempt to delete an unknown account (account: %s, ip: %s)\n",
  1908. account_name, ip);
  1909. }
  1910. WFIFOSET(fd,30);
  1911. RFIFOSKIP(fd,26);
  1912. break;
  1913. case 0x7934: // Request to change a password
  1914. if (RFIFOREST(fd) < 50)
  1915. return 0;
  1916. WFIFOW(fd,0) = 0x7935;
  1917. WFIFOL(fd,2) = 0xFFFFFFFF; /// WTF??? an unsigned being set to a -1
  1918. account_name = (char*)RFIFOP(fd,2);
  1919. account_name[23] = '\0';
  1920. remove_control_chars(account_name);
  1921. i = search_account_index(account_name);
  1922. if (i != -1) {
  1923. memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24);
  1924. memcpy(auth_dat[i].pass, RFIFOP(fd,26), 24);
  1925. auth_dat[i].pass[23] = '\0';
  1926. remove_control_chars(auth_dat[i].pass);
  1927. WFIFOL(fd,2) = auth_dat[i].account_id;
  1928. login_log("'ladmin': Modification of a password (account: %s, new password: %s, ip: %s)\n",
  1929. auth_dat[i].userid, auth_dat[i].pass, ip);
  1930. mmo_auth_sync();
  1931. } else {
  1932. memcpy(WFIFOP(fd,6), account_name, 24);
  1933. login_log("'ladmin': Attempt to modify the password of an unknown account (account: %s, ip: %s)\n",
  1934. account_name, ip);
  1935. }
  1936. WFIFOSET(fd,30);
  1937. RFIFOSKIP(fd,50);
  1938. break;
  1939. case 0x7936: // Request to modify a state
  1940. if (RFIFOREST(fd) < 50)
  1941. return 0;
  1942. {
  1943. char error_message[20];
  1944. uint32 statut;
  1945. WFIFOW(fd,0) = 0x7937;
  1946. WFIFOL(fd,2) = 0xFFFFFFFF; // WTF???
  1947. account_name = (char*)RFIFOP(fd,2);
  1948. account_name[23] = '\0';
  1949. remove_control_chars(account_name);
  1950. statut = RFIFOL(fd,26);
  1951. memcpy(error_message, RFIFOP(fd,30), 20);
  1952. error_message[19] = '\0';
  1953. remove_control_chars(error_message);
  1954. if (statut != 7 || error_message[0] == '\0') { // 7: // 6 = Your are Prohibited to log in until %s
  1955. strcpy(error_message, "-");
  1956. }
  1957. i = search_account_index(account_name);
  1958. if (i != -1) {
  1959. memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24);
  1960. WFIFOL(fd,2) = auth_dat[i].account_id;
  1961. if (auth_dat[i].state == statut && strcmp(auth_dat[i].error_message, error_message) == 0)
  1962. login_log("'ladmin': Modification of a state, but the state of the account is already the good state (account: %s, received state: %d, ip: %s)\n",
  1963. account_name, statut, ip);
  1964. else {
  1965. if (statut == 7)
  1966. login_log("'ladmin': Modification of a state (account: %s, new state: %d - prohibited to login until '%s', ip: %s)\n",
  1967. auth_dat[i].userid, statut, error_message, ip);
  1968. else
  1969. login_log("'ladmin': Modification of a state (account: %s, new state: %d, ip: %s)\n",
  1970. auth_dat[i].userid, statut, ip);
  1971. if (auth_dat[i].state == 0) {
  1972. unsigned char buf[16];
  1973. WBUFW(buf,0) = 0x2731;
  1974. WBUFL(buf,2) = auth_dat[i].account_id;
  1975. WBUFB(buf,6) = 0; // 0: change of statut, 1: ban
  1976. WBUFL(buf,7) = statut; // status or final date of a banishment
  1977. charif_sendallwos(-1, buf, 11);
  1978. for(j = 0; j < AUTH_FIFO_SIZE; j++)
  1979. if (auth_fifo[j].account_id == auth_dat[i].account_id)
  1980. auth_fifo[j].login_id1++; // to avoid reconnection error when come back from map-server (char-server will ask again the authentification)
  1981. }
  1982. auth_dat[i].state = statut;
  1983. memcpy(auth_dat[i].error_message, error_message, 20);
  1984. mmo_auth_sync();
  1985. }
  1986. } else {
  1987. memcpy(WFIFOP(fd,6), account_name, 24);
  1988. login_log("'ladmin': Attempt to modify the state of an unknown account (account: %s, received state: %d, ip: %s)\n",
  1989. account_name, statut, ip);
  1990. }
  1991. WFIFOL(fd,30) = statut;
  1992. }
  1993. WFIFOSET(fd,34);
  1994. RFIFOSKIP(fd,50);
  1995. break;
  1996. case 0x7938: // Request for servers list and # of online players
  1997. {
  1998. uint8 server_num = 0;
  1999. login_log("'ladmin': Sending of servers list (ip: %s)\n", ip);
  2000. for(i = 0; i < MAX_SERVERS; i++) {
  2001. if (server_fd[i] >= 0) {
  2002. WFIFOL(fd,4+server_num*32) = htonl(server[i].ip);
  2003. WFIFOW(fd,4+server_num*32+4) = htons(server[i].port);
  2004. memcpy(WFIFOP(fd,4+server_num*32+6), server[i].name, 20);
  2005. WFIFOW(fd,4+server_num*32+26) = server[i].users;
  2006. WFIFOW(fd,4+server_num*32+28) = server[i].maintenance;
  2007. WFIFOW(fd,4+server_num*32+30) = server[i].new_;
  2008. server_num++;
  2009. }
  2010. }
  2011. WFIFOW(fd,0) = 0x7939;
  2012. WFIFOW(fd,2) = 4 + 32 * server_num;
  2013. WFIFOSET(fd,4+32*server_num);
  2014. RFIFOSKIP(fd,2);
  2015. break;
  2016. }
  2017. case 0x793a: // Request to password check
  2018. if (RFIFOREST(fd) < 50)
  2019. return 0;
  2020. WFIFOW(fd,0) = 0x793b;
  2021. WFIFOL(fd,2) = 0xFFFFFFFF; // WTF???
  2022. account_name = (char*)RFIFOP(fd,2);
  2023. account_name[23] = '\0';
  2024. remove_control_chars(account_name);
  2025. i = search_account_index(account_name);
  2026. if (i != -1) {
  2027. char pass[25];
  2028. memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24);
  2029. memcpy(pass, RFIFOP(fd,26), 24);
  2030. pass[24] = '\0';
  2031. remove_control_chars(pass);
  2032. if (strcmp(auth_dat[i].pass, pass) == 0) {
  2033. WFIFOL(fd,2) = auth_dat[i].account_id;
  2034. login_log("'ladmin': Check of password OK (account: %s, password: %s, ip: %s)\n",
  2035. auth_dat[i].userid, auth_dat[i].pass, ip);
  2036. } else {
  2037. login_log("'ladmin': Failure of password check (account: %s, proposed pass: %s, ip: %s)\n",
  2038. auth_dat[i].userid, pass, ip);
  2039. }
  2040. } else {
  2041. memcpy(WFIFOP(fd,6), account_name, 24);
  2042. login_log("'ladmin': Attempt to check the password of an unknown account (account: %s, ip: %s)\n",
  2043. account_name, ip);
  2044. }
  2045. WFIFOSET(fd,30);
  2046. RFIFOSKIP(fd,50);
  2047. break;
  2048. case 0x793c: // Request to modify sex
  2049. if (RFIFOREST(fd) < 27)
  2050. return 0;
  2051. WFIFOW(fd,0) = 0x793d;
  2052. WFIFOL(fd,2) = 0xFFFFFFFF; // WTF???
  2053. account_name = (char*)RFIFOP(fd,2);
  2054. account_name[23] = '\0';
  2055. remove_control_chars(account_name);
  2056. memcpy(WFIFOP(fd,6), account_name, 24);
  2057. {
  2058. char sex;
  2059. sex = RFIFOB(fd,26);
  2060. if (sex != 'F' && sex != 'M') {
  2061. if (sex > 31)
  2062. login_log("'ladmin': Attempt to give an invalid sex (account: %s, received sex: %c, ip: %s)\n",
  2063. account_name, sex, ip);
  2064. else
  2065. login_log("'ladmin': Attempt to give an invalid sex (account: %s, received sex: 'control char', ip: %s)\n",
  2066. account_name, ip);
  2067. } else {
  2068. i = search_account_index(account_name);
  2069. if (i != -1) {
  2070. memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24);
  2071. if (auth_dat[i].sex != ((sex == 'S' || sex == 's') ? 2 : (sex == 'M' || sex == 'm'))) {
  2072. unsigned char buf[16];
  2073. WFIFOL(fd,2) = auth_dat[i].account_id;
  2074. for(j = 0; j < AUTH_FIFO_SIZE; j++)
  2075. if (auth_fifo[j].account_id == auth_dat[i].account_id)
  2076. auth_fifo[j].login_id1++; // to avoid reconnection error when come back from map-server (char-server will ask again the authentification)
  2077. auth_dat[i].sex = (sex == 'S' || sex == 's') ? 2 : (sex == 'M' || sex == 'm');
  2078. login_log("'ladmin': Modification of a sex (account: %s, new sex: %c, ip: %s)\n",
  2079. auth_dat[i].userid, sex, ip);
  2080. mmo_auth_sync();
  2081. // send to all char-server the change
  2082. WBUFW(buf,0) = 0x2723;
  2083. WBUFL(buf,2) = auth_dat[i].account_id;
  2084. WBUFB(buf,6) = auth_dat[i].sex;
  2085. charif_sendallwos(-1, buf, 7);
  2086. } else {
  2087. login_log("'ladmin': Modification of a sex, but the sex is already the good sex (account: %s, sex: %c, ip: %s)\n",
  2088. auth_dat[i].userid, sex, ip);
  2089. }
  2090. } else {
  2091. login_log("'ladmin': Attempt to modify the sex of an unknown account (account: %s, received sex: %c, ip: %s)\n",
  2092. account_name, sex, ip);
  2093. }
  2094. }
  2095. }
  2096. WFIFOSET(fd,30);
  2097. RFIFOSKIP(fd,27);
  2098. break;
  2099. case 0x793e: // Request to modify GM level
  2100. if (RFIFOREST(fd) < 27)
  2101. return 0;
  2102. WFIFOW(fd,0) = 0x793f;
  2103. WFIFOL(fd,2) = 0xFFFFFFFF; // WTF???
  2104. account_name = (char*)RFIFOP(fd,2);
  2105. account_name[23] = '\0';
  2106. remove_control_chars(account_name);
  2107. memcpy(WFIFOP(fd,6), account_name, 24);
  2108. {
  2109. char new_gm_level;
  2110. new_gm_level = RFIFOB(fd,26);
  2111. if (new_gm_level < 0 || new_gm_level > 99) {
  2112. login_log("'ladmin': Attempt to give an invalid GM level (account: %s, received GM level: %d, ip: %s)\n",
  2113. account_name, (int)new_gm_level, ip);
  2114. } else {
  2115. i = search_account_index(account_name);
  2116. if (i != -1) {
  2117. int acc = auth_dat[i].account_id;
  2118. memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24);
  2119. if (isGM(acc) != new_gm_level) {
  2120. // modification of the file
  2121. FILE *fp, *fp2;
  2122. int lock;
  2123. char line[512];
  2124. int GM_account, GM_level;
  2125. int modify_flag;
  2126. char tmpstr[24];
  2127. time_t raw_time;
  2128. if ((fp2 = lock_fopen(GM_account_filename, &lock)) != NULL) {
  2129. if ((fp = fopen(GM_account_filename, "r")) != NULL) {
  2130. time(&raw_time);
  2131. strftime(tmpstr, 23, login_config.date_format, localtime(&raw_time));
  2132. modify_flag = 0;
  2133. // read/write GM file
  2134. while(fgets(line, sizeof(line), fp))
  2135. {
  2136. while(line[0] != '\0' && (line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r'))
  2137. line[strlen(line)-1] = '\0'; // TODO: remove this
  2138. if ((line[0] == '/' && line[1] == '/') || line[0] == '\0')
  2139. fprintf(fp2, "%s\n", line);
  2140. else {
  2141. if (sscanf(line, "%d %d", &GM_account, &GM_level) != 2 && sscanf(line, "%d: %d", &GM_account, &GM_level) != 2)
  2142. fprintf(fp2, "%s\n", line);
  2143. else if (GM_account != acc)
  2144. fprintf(fp2, "%s\n", line);
  2145. else if (new_gm_level < 1) {
  2146. fprintf(fp2, "// %s: 'ladmin' GM level removed on account %d '%s' (previous level: %d)\n//%d %d\n", tmpstr, acc, auth_dat[i].userid, GM_level, acc, new_gm_level);
  2147. modify_flag = 1;
  2148. } else {
  2149. fprintf(fp2, "// %s: 'ladmin' GM level on account %d '%s' (previous level: %d)\n%d %d\n", tmpstr, acc, auth_dat[i].userid, GM_level, acc, new_gm_level);
  2150. modify_flag = 1;
  2151. }
  2152. }
  2153. }
  2154. if (modify_flag == 0)
  2155. fprintf(fp2, "// %s: 'ladmin' GM level on account %d '%s' (previous level: 0)\n%d %d\n", tmpstr, acc, auth_dat[i].userid, acc, new_gm_level);
  2156. fclose(fp);
  2157. } else {
  2158. login_log("'ladmin': Attempt to modify of a GM level - impossible to read GM accounts file (account: %s (%d), received GM level: %d, ip: %s)\n",
  2159. auth_dat[i].userid, acc, (int)new_gm_level, ip);
  2160. }
  2161. if (lock_fclose(fp2, GM_account_filename, &lock) == 0) {
  2162. WFIFOL(fd,2) = acc;
  2163. login_log("'ladmin': Modification of a GM level (account: %s (%d), new GM level: %d, ip: %s)\n",
  2164. auth_dat[i].userid, acc, (int)new_gm_level, ip);
  2165. // read and send new GM informations
  2166. read_gm_account();
  2167. send_GM_accounts(-1);
  2168. } else {
  2169. login_log("'ladmin': Attempt to modify of a GM level - impossible to write GM accounts file (account: %s (%d), received GM level: %d, ip: %s)\n",
  2170. auth_dat[i].userid, acc, (int)new_gm_level, ip);
  2171. }
  2172. } else {
  2173. login_log("'ladmin': Attempt to modify of a GM level - impossible to write GM accounts file (account: %s (%d), received GM level: %d, ip: %s)\n",
  2174. auth_dat[i].userid, acc, (int)new_gm_level, ip);
  2175. }
  2176. } else {
  2177. login_log("'ladmin': Attempt to modify of a GM level, but the GM level is already the good GM level (account: %s (%d), GM level: %d, ip: %s)\n",
  2178. auth_dat[i].userid, acc, (int)new_gm_level, ip);
  2179. }
  2180. } else {
  2181. login_log("'ladmin': Attempt to modify the GM level of an unknown account (account: %s, received GM level: %d, ip: %s)\n",
  2182. account_name, (int)new_gm_level, ip);
  2183. }
  2184. }
  2185. }
  2186. WFIFOSET(fd,30);
  2187. RFIFOSKIP(fd,27);
  2188. break;
  2189. case 0x7940: // Request to modify e-mail
  2190. if (RFIFOREST(fd) < 66)
  2191. return 0;
  2192. WFIFOW(fd,0) = 0x7941;
  2193. WFIFOL(fd,2) = 0xFFFFFFFF; // WTF???
  2194. account_name = (char*)RFIFOP(fd,2);
  2195. account_name[23] = '\0';
  2196. remove_control_chars(account_name);
  2197. memcpy(WFIFOP(fd,6), account_name, 24);
  2198. {
  2199. char email[40];
  2200. memcpy(email, RFIFOP(fd,26), 40);
  2201. if (e_mail_check(email) == 0) {
  2202. login_log("'ladmin': Attempt to give an invalid e-mail (account: %s, ip: %s)\n",
  2203. account_name, ip);
  2204. } else {
  2205. remove_control_chars(email);
  2206. i = search_account_index(account_name);
  2207. if (i != -1) {
  2208. memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24);
  2209. memcpy(auth_dat[i].email, email, 40);
  2210. WFIFOL(fd,2) = auth_dat[i].account_id;
  2211. login_log("'ladmin': Modification of an email (account: %s, new e-mail: %s, ip: %s)\n",
  2212. auth_dat[i].userid, email, ip);
  2213. mmo_auth_sync();
  2214. } else {
  2215. login_log("'ladmin': Attempt to modify the e-mail of an unknown account (account: %s, received e-mail: %s, ip: %s)\n",
  2216. account_name, email, ip);
  2217. }
  2218. }
  2219. }
  2220. WFIFOSET(fd,30);
  2221. RFIFOSKIP(fd,66);
  2222. break;
  2223. case 0x7942: // Request to modify memo field
  2224. if ((int)RFIFOREST(fd) < 28 || (int)RFIFOREST(fd) < (28 + RFIFOW(fd,26)))
  2225. return 0;
  2226. WFIFOW(fd,0) = 0x7943;
  2227. WFIFOL(fd,2) = 0xFFFFFFFF; // WTF???
  2228. account_name = (char*)RFIFOP(fd,2);
  2229. account_name[23] = '\0';
  2230. remove_control_chars(account_name);
  2231. i = search_account_index(account_name);
  2232. if (i != -1) {
  2233. int size_of_memo = sizeof(auth_dat[i].memo);
  2234. memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24);
  2235. memset(auth_dat[i].memo, '\0', size_of_memo);
  2236. if (RFIFOW(fd,26) == 0) {
  2237. strncpy(auth_dat[i].memo, "-", size_of_memo);
  2238. } else if (RFIFOW(fd,26) > size_of_memo - 1) {
  2239. memcpy(auth_dat[i].memo, RFIFOP(fd,28), size_of_memo - 1);
  2240. } else {
  2241. memcpy(auth_dat[i].memo, RFIFOP(fd,28), RFIFOW(fd,26));
  2242. }
  2243. auth_dat[i].memo[size_of_memo - 1] = '\0';
  2244. remove_control_chars(auth_dat[i].memo);
  2245. WFIFOL(fd,2) = auth_dat[i].account_id;
  2246. login_log("'ladmin': Modification of a memo field (account: %s, new memo: %s, ip: %s)\n",
  2247. auth_dat[i].userid, auth_dat[i].memo, ip);
  2248. mmo_auth_sync();
  2249. } else {
  2250. memcpy(WFIFOP(fd,6), account_name, 24);
  2251. login_log("'ladmin': Attempt to modify the memo field of an unknown account (account: %s, ip: %s)\n",
  2252. account_name, ip);
  2253. }
  2254. WFIFOSET(fd,30);
  2255. RFIFOSKIP(fd,28 + RFIFOW(fd,26));
  2256. break;
  2257. case 0x7944: // Request to found an account id
  2258. if (RFIFOREST(fd) < 26)
  2259. return 0;
  2260. WFIFOW(fd,0) = 0x7945;
  2261. WFIFOL(fd,2) = 0xFFFFFFFF; // WTF???
  2262. account_name = (char*)RFIFOP(fd,2);
  2263. account_name[23] = '\0';
  2264. remove_control_chars(account_name);
  2265. i = search_account_index(account_name);
  2266. if (i != -1) {
  2267. memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24);
  2268. WFIFOL(fd,2) = auth_dat[i].account_id;
  2269. login_log("'ladmin': Request (by the name) of an account id (account: %s, id: %d, ip: %s)\n",
  2270. auth_dat[i].userid, auth_dat[i].account_id, ip);
  2271. } else {
  2272. memcpy(WFIFOP(fd,6), account_name, 24);
  2273. login_log("'ladmin': ID request (by the name) of an unknown account (account: %s, ip: %s)\n",
  2274. account_name, ip);
  2275. }
  2276. WFIFOSET(fd,30);
  2277. RFIFOSKIP(fd,26);
  2278. break;
  2279. case 0x7946: // Request to found an account name
  2280. if (RFIFOREST(fd) < 6)
  2281. return 0;
  2282. WFIFOW(fd,0) = 0x7947;
  2283. WFIFOL(fd,2) = RFIFOL(fd,2);
  2284. memset(WFIFOP(fd,6), '\0', 24);
  2285. for(i = 0; i < auth_num; i++) {
  2286. if (auth_dat[i].account_id == RFIFOL(fd,2)) {
  2287. strncpy((char*)WFIFOP(fd,6), auth_dat[i].userid, 24);
  2288. login_log("'ladmin': Request (by id) of an account name (account: %s, id: %d, ip: %s)\n",
  2289. auth_dat[i].userid, RFIFOL(fd,2), ip);
  2290. break;
  2291. }
  2292. }
  2293. if (i == auth_num) {
  2294. login_log("'ladmin': Name request (by id) of an unknown account (id: %d, ip: %s)\n",
  2295. RFIFOL(fd,2), ip);
  2296. strncpy((char*)WFIFOP(fd,6), "", 24);
  2297. }
  2298. WFIFOSET(fd,30);
  2299. RFIFOSKIP(fd,6);
  2300. break;
  2301. case 0x7948: // Request to change the validity limit (timestamp) (absolute value)
  2302. if (RFIFOREST(fd) < 30)
  2303. return 0;
  2304. {
  2305. time_t timestamp;
  2306. char tmpstr[2048];
  2307. WFIFOW(fd,0) = 0x7949;
  2308. WFIFOL(fd,2) = 0xFFFFFFFF; // WTF???
  2309. account_name = (char*)RFIFOP(fd,2);
  2310. account_name[23] = '\0';
  2311. remove_control_chars(account_name);
  2312. timestamp = (time_t)RFIFOL(fd,26);
  2313. strftime(tmpstr, 24, login_config.date_format, localtime(&timestamp));
  2314. i = search_account_index(account_name);
  2315. if (i != -1) {
  2316. memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24);
  2317. login_log("'ladmin': Change of a validity limit (account: %s, new validity: %d (%s), ip: %s)\n",
  2318. auth_dat[i].userid, timestamp, (timestamp == 0 ? "unlimited" : tmpstr), ip);
  2319. auth_dat[i].connect_until_time = timestamp;
  2320. WFIFOL(fd,2) = auth_dat[i].account_id;
  2321. mmo_auth_sync();
  2322. } else {
  2323. memcpy(WFIFOP(fd,6), account_name, 24);
  2324. login_log("'ladmin': Attempt to change the validity limit of an unknown account (account: %s, received validity: %d (%s), ip: %s)\n",
  2325. account_name, timestamp, (timestamp == 0 ? "unlimited" : tmpstr), ip);
  2326. }
  2327. WFIFOL(fd,30) = (unsigned int)timestamp;
  2328. }
  2329. WFIFOSET(fd,34);
  2330. RFIFOSKIP(fd,30);
  2331. break;
  2332. case 0x794a: // Request to change the final date of a banishment (timestamp) (absolute value)
  2333. if (RFIFOREST(fd) < 30)
  2334. return 0;
  2335. {
  2336. time_t timestamp;
  2337. char tmpstr[2048];
  2338. WFIFOW(fd,0) = 0x794b;
  2339. WFIFOL(fd,2) = 0xFFFFFFFF; // WTF???
  2340. account_name = (char*)RFIFOP(fd,2);
  2341. account_name[23] = '\0';
  2342. remove_control_chars(account_name);
  2343. timestamp = (time_t)RFIFOL(fd,26);
  2344. if (timestamp <= time(NULL))
  2345. timestamp = 0;
  2346. strftime(tmpstr, 24, login_config.date_format, localtime(&timestamp));
  2347. i = search_account_index(account_name);
  2348. if (i != -1) {
  2349. memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24);
  2350. WFIFOL(fd,2) = auth_dat[i].account_id;
  2351. login_log("'ladmin': Change of the final date of a banishment (account: %s, new final date of banishment: %d (%s), ip: %s)\n",
  2352. auth_dat[i].userid, timestamp, (timestamp == 0 ? "no banishment" : tmpstr), ip);
  2353. if (auth_dat[i].ban_until_time != timestamp) {
  2354. if (timestamp != 0) {
  2355. unsigned char buf[16];
  2356. WBUFW(buf,0) = 0x2731;
  2357. WBUFL(buf,2) = auth_dat[i].account_id;
  2358. WBUFB(buf,6) = 1; // 0: change of statut, 1: ban
  2359. WBUFL(buf,7) = (unsigned int)timestamp; // status or final date of a banishment
  2360. charif_sendallwos(-1, buf, 11);
  2361. for(j = 0; j < AUTH_FIFO_SIZE; j++)
  2362. if (auth_fifo[j].account_id == auth_dat[i].account_id)
  2363. auth_fifo[j].login_id1++; // to avoid reconnection error when come back from map-server (char-server will ask again the authentification)
  2364. }
  2365. auth_dat[i].ban_until_time = timestamp;
  2366. mmo_auth_sync();
  2367. }
  2368. } else {
  2369. memcpy(WFIFOP(fd,6), account_name, 24);
  2370. login_log("'ladmin': Attempt to change the final date of a banishment of an unknown account (account: %s, received final date of banishment: %d (%s), ip: %s)\n",
  2371. account_name, timestamp, (timestamp == 0 ? "no banishment" : tmpstr), ip);
  2372. }
  2373. WFIFOL(fd,30) = (unsigned int)timestamp;
  2374. }
  2375. WFIFOSET(fd,34);
  2376. RFIFOSKIP(fd,30);
  2377. break;
  2378. case 0x794c: // Request to change the final date of a banishment (timestamp) (relative change)
  2379. if (RFIFOREST(fd) < 38)
  2380. return 0;
  2381. {
  2382. time_t timestamp;
  2383. struct tm *tmtime;
  2384. char tmpstr[2048];
  2385. WFIFOW(fd,0) = 0x794d;
  2386. WFIFOL(fd,2) = 0xFFFFFFFF; // WTF???
  2387. account_name = (char*)RFIFOP(fd,2);
  2388. account_name[23] = '\0';
  2389. remove_control_chars(account_name);
  2390. i = search_account_index(account_name);
  2391. if (i != -1) {
  2392. WFIFOL(fd,2) = auth_dat[i].account_id;
  2393. memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24);
  2394. if (auth_dat[i].ban_until_time == 0 || auth_dat[i].ban_until_time < time(NULL))
  2395. timestamp = time(NULL);
  2396. else
  2397. timestamp = auth_dat[i].ban_until_time;
  2398. tmtime = localtime(&timestamp);
  2399. tmtime->tm_year = tmtime->tm_year + (short)RFIFOW(fd,26);
  2400. tmtime->tm_mon = tmtime->tm_mon + (short)RFIFOW(fd,28);
  2401. tmtime->tm_mday = tmtime->tm_mday + (short)RFIFOW(fd,30);
  2402. tmtime->tm_hour = tmtime->tm_hour + (short)RFIFOW(fd,32);
  2403. tmtime->tm_min = tmtime->tm_min + (short)RFIFOW(fd,34);
  2404. tmtime->tm_sec = tmtime->tm_sec + (short)RFIFOW(fd,36);
  2405. timestamp = mktime(tmtime);
  2406. if (timestamp != -1) {
  2407. if (timestamp <= time(NULL))
  2408. timestamp = 0;
  2409. strftime(tmpstr, 24, login_config.date_format, localtime(&timestamp));
  2410. login_log("'ladmin': Adjustment of a final date of a banishment (account: %s, (%+d y %+d m %+d d %+d h %+d mn %+d s) -> new validity: %d (%s), ip: %s)\n",
  2411. auth_dat[i].userid, (short)RFIFOW(fd,26), (short)RFIFOW(fd,28), (short)RFIFOW(fd,30), (short)RFIFOW(fd,32), (short)RFIFOW(fd,34), (short)RFIFOW(fd,36), timestamp, (timestamp == 0 ? "no banishment" : tmpstr), ip);
  2412. if (auth_dat[i].ban_until_time != timestamp) {
  2413. if (timestamp != 0) {
  2414. unsigned char buf[16];
  2415. WBUFW(buf,0) = 0x2731;
  2416. WBUFL(buf,2) = auth_dat[i].account_id;
  2417. WBUFB(buf,6) = 1; // 0: change of statut, 1: ban
  2418. WBUFL(buf,7) = (unsigned int)timestamp; // status or final date of a banishment
  2419. charif_sendallwos(-1, buf, 11);
  2420. for(j = 0; j < AUTH_FIFO_SIZE; j++)
  2421. if (auth_fifo[j].account_id == auth_dat[i].account_id)
  2422. auth_fifo[j].login_id1++; // to avoid reconnection error when come back from map-server (char-server will ask again the authentification)
  2423. }
  2424. auth_dat[i].ban_until_time = timestamp;
  2425. mmo_auth_sync();
  2426. }
  2427. } else {
  2428. strftime(tmpstr, 24, login_config.date_format, localtime(&auth_dat[i].ban_until_time));
  2429. login_log("'ladmin': Impossible to adjust the final date of a banishment (account: %s, %d (%s) + (%+d y %+d m %+d d %+d h %+d mn %+d s) -> ???, ip: %s)\n",
  2430. auth_dat[i].userid, auth_dat[i].ban_until_time, (auth_dat[i].ban_until_time == 0 ? "no banishment" : tmpstr), (short)RFIFOW(fd,26), (short)RFIFOW(fd,28), (short)RFIFOW(fd,30), (short)RFIFOW(fd,32), (short)RFIFOW(fd,34), (short)RFIFOW(fd,36), ip);
  2431. }
  2432. WFIFOL(fd,30) = (unsigned long)auth_dat[i].ban_until_time;
  2433. } else {
  2434. memcpy(WFIFOP(fd,6), account_name, 24);
  2435. login_log("'ladmin': Attempt to adjust the final date of a banishment of an unknown account (account: %s, ip: %s)\n",
  2436. account_name, ip);
  2437. WFIFOL(fd,30) = 0;
  2438. }
  2439. }
  2440. WFIFOSET(fd,34);
  2441. RFIFOSKIP(fd,38);
  2442. break;
  2443. case 0x794e: // Request to send a broadcast message
  2444. if (RFIFOREST(fd) < 8 || RFIFOREST(fd) < (8 + RFIFOL(fd,4)))
  2445. return 0;
  2446. WFIFOW(fd,0) = 0x794f;
  2447. WFIFOW(fd,2) = 0xFFFF; // WTF???
  2448. if (RFIFOL(fd,4) < 1) {
  2449. login_log("'ladmin': Receiving a message for broadcast, but message is void (ip: %s)\n",
  2450. ip);
  2451. } else {
  2452. // at least 1 char-server
  2453. for(i = 0; i < MAX_SERVERS; i++)
  2454. if (server_fd[i] >= 0)
  2455. break;
  2456. if (i == MAX_SERVERS) {
  2457. login_log("'ladmin': Receiving a message for broadcast, but no char-server is online (ip: %s)\n",
  2458. ip);
  2459. } else {
  2460. unsigned char buf[32000];
  2461. char message[32000];
  2462. WFIFOW(fd,2) = 0;
  2463. memset(message, '\0', sizeof(message));
  2464. memcpy(message, RFIFOP(fd,8), RFIFOL(fd,4));
  2465. message[sizeof(message)-1] = '\0';
  2466. remove_control_chars(message);
  2467. if (RFIFOW(fd,2) == 0)
  2468. login_log("'ladmin': Receiving a message for broadcast (message (in yellow): %s, ip: %s)\n",
  2469. message, ip);
  2470. else
  2471. login_log("'ladmin': Receiving a message for broadcast (message (in blue): %s, ip: %s)\n",
  2472. message, ip);
  2473. // send same message to all char-servers (no answer)
  2474. memcpy(WBUFP(buf,0), RFIFOP(fd,0), 8 + RFIFOL(fd,4));
  2475. WBUFW(buf,0) = 0x2726;
  2476. charif_sendallwos(-1, buf, 8 + RFIFOL(fd,4));
  2477. }
  2478. }
  2479. WFIFOSET(fd,4);
  2480. RFIFOSKIP(fd,8 + RFIFOL(fd,4));
  2481. break;
  2482. case 0x7950: // Request to change the validity limite (timestamp) (relative change)
  2483. if (RFIFOREST(fd) < 38)
  2484. return 0;
  2485. {
  2486. time_t timestamp;
  2487. struct tm *tmtime;
  2488. char tmpstr[2048];
  2489. char tmpstr2[2048];
  2490. WFIFOW(fd,0) = 0x7951;
  2491. WFIFOL(fd,2) = 0xFFFFFFFF; // WTF???
  2492. account_name = (char*)RFIFOP(fd,2);
  2493. account_name[23] = '\0';
  2494. remove_control_chars(account_name);
  2495. i = search_account_index(account_name);
  2496. if (i != -1) {
  2497. WFIFOL(fd,2) = auth_dat[i].account_id;
  2498. memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24);
  2499. timestamp = auth_dat[i].connect_until_time;
  2500. if (add_to_unlimited_account == 0 && timestamp == 0) {
  2501. login_log("'ladmin': Attempt to adjust the validity limit of an unlimited account (account: %s, ip: %s)\n",
  2502. auth_dat[i].userid, ip);
  2503. WFIFOL(fd,30) = 0;
  2504. } else {
  2505. if (timestamp == 0 || timestamp < time(NULL))
  2506. timestamp = time(NULL);
  2507. tmtime = localtime(&timestamp);
  2508. tmtime->tm_year = tmtime->tm_year + (short)RFIFOW(fd,26);
  2509. tmtime->tm_mon = tmtime->tm_mon + (short)RFIFOW(fd,28);
  2510. tmtime->tm_mday = tmtime->tm_mday + (short)RFIFOW(fd,30);
  2511. tmtime->tm_hour = tmtime->tm_hour + (short)RFIFOW(fd,32);
  2512. tmtime->tm_min = tmtime->tm_min + (short)RFIFOW(fd,34);
  2513. tmtime->tm_sec = tmtime->tm_sec + (short)RFIFOW(fd,36);
  2514. timestamp = mktime(tmtime);
  2515. if (timestamp != -1) {
  2516. strftime(tmpstr, 24, login_config.date_format, localtime(&auth_dat[i].connect_until_time));
  2517. strftime(tmpstr2, 24, login_config.date_format, localtime(&timestamp));
  2518. login_log("'ladmin': Adjustment of a validity limit (account: %s, %d (%s) + (%+d y %+d m %+d d %+d h %+d mn %+d s) -> new validity: %d (%s), ip: %s)\n",
  2519. auth_dat[i].userid, auth_dat[i].connect_until_time, (auth_dat[i].connect_until_time == 0 ? "unlimited" : tmpstr), (short)RFIFOW(fd,26), (short)RFIFOW(fd,28), (short)RFIFOW(fd,30), (short)RFIFOW(fd,32), (short)RFIFOW(fd,34), (short)RFIFOW(fd,36), timestamp, (timestamp == 0 ? "unlimited" : tmpstr2), ip);
  2520. auth_dat[i].connect_until_time = timestamp;
  2521. mmo_auth_sync();
  2522. WFIFOL(fd,30) = (unsigned long)auth_dat[i].connect_until_time;
  2523. } else {
  2524. strftime(tmpstr, 24, login_config.date_format, localtime(&auth_dat[i].connect_until_time));
  2525. login_log("'ladmin': Impossible to adjust a validity limit (account: %s, %d (%s) + (%+d y %+d m %+d d %+d h %+d mn %+d s) -> ???, ip: %s)\n",
  2526. auth_dat[i].userid, auth_dat[i].connect_until_time, (auth_dat[i].connect_until_time == 0 ? "unlimited" : tmpstr), (short)RFIFOW(fd,26), (short)RFIFOW(fd,28), (short)RFIFOW(fd,30), (short)RFIFOW(fd,32), (short)RFIFOW(fd,34), (short)RFIFOW(fd,36), ip);
  2527. WFIFOL(fd,30) = 0;
  2528. }
  2529. }
  2530. } else {
  2531. memcpy(WFIFOP(fd,6), account_name, 24);
  2532. login_log("'ladmin': Attempt to adjust the validity limit of an unknown account (account: %s, ip: %s)\n",
  2533. account_name, ip);
  2534. WFIFOL(fd,30) = 0;
  2535. }
  2536. }
  2537. WFIFOSET(fd,34);
  2538. RFIFOSKIP(fd,38);
  2539. break;
  2540. case 0x7952: // Request about informations of an account (by account name)
  2541. if (RFIFOREST(fd) < 26)
  2542. return 0;
  2543. WFIFOW(fd,0) = 0x7953;
  2544. WFIFOL(fd,2) = 0xFFFFFFFF; // WTF???
  2545. account_name = (char*)RFIFOP(fd,2);
  2546. account_name[23] = '\0';
  2547. remove_control_chars(account_name);
  2548. i = search_account_index(account_name);
  2549. if (i != -1) {
  2550. WFIFOL(fd,2) = auth_dat[i].account_id;
  2551. WFIFOB(fd,6) = (unsigned char)isGM(auth_dat[i].account_id);
  2552. memcpy(WFIFOP(fd,7), auth_dat[i].userid, 24);
  2553. WFIFOB(fd,31) = auth_dat[i].sex;
  2554. WFIFOL(fd,32) = auth_dat[i].logincount;
  2555. WFIFOL(fd,36) = auth_dat[i].state;
  2556. memcpy(WFIFOP(fd,40), auth_dat[i].error_message, 20);
  2557. memcpy(WFIFOP(fd,60), auth_dat[i].lastlogin, 24);
  2558. memcpy(WFIFOP(fd,84), auth_dat[i].last_ip, 16);
  2559. memcpy(WFIFOP(fd,100), auth_dat[i].email, 40);
  2560. WFIFOL(fd,140) = (unsigned long)auth_dat[i].connect_until_time;
  2561. WFIFOL(fd,144) = (unsigned long)auth_dat[i].ban_until_time;
  2562. WFIFOW(fd,148) = (uint16)strlen(auth_dat[i].memo);
  2563. if (auth_dat[i].memo[0]) {
  2564. memcpy(WFIFOP(fd,150), auth_dat[i].memo, strlen(auth_dat[i].memo));
  2565. }
  2566. login_log("'ladmin': Sending information of an account (request by the name; account: %s, id: %d, ip: %s)\n",
  2567. auth_dat[i].userid, auth_dat[i].account_id, ip);
  2568. WFIFOSET(fd,150+strlen(auth_dat[i].memo));
  2569. } else {
  2570. memcpy(WFIFOP(fd,7), account_name, 24);
  2571. WFIFOW(fd,148) = 0;
  2572. login_log("'ladmin': Attempt to obtain information (by the name) of an unknown account (account: %s, ip: %s)\n",
  2573. account_name, ip);
  2574. WFIFOSET(fd,150);
  2575. }
  2576. RFIFOSKIP(fd,26);
  2577. break;
  2578. case 0x7954: // Request about information of an account (by account id)
  2579. if (RFIFOREST(fd) < 6)
  2580. return 0;
  2581. WFIFOW(fd,0) = 0x7953;
  2582. WFIFOL(fd,2) = RFIFOL(fd,2);
  2583. memset(WFIFOP(fd,7), '\0', 24);
  2584. for(i = 0; i < auth_num; i++) {
  2585. if (auth_dat[i].account_id == RFIFOL(fd,2)) {
  2586. login_log("'ladmin': Sending information of an account (request by the id; account: %s, id: %d, ip: %s)\n",
  2587. auth_dat[i].userid, RFIFOL(fd,2), ip);
  2588. WFIFOB(fd,6) = (unsigned char)isGM(auth_dat[i].account_id);
  2589. memcpy(WFIFOP(fd,7), auth_dat[i].userid, 24);
  2590. WFIFOB(fd,31) = auth_dat[i].sex;
  2591. WFIFOL(fd,32) = auth_dat[i].logincount;
  2592. WFIFOL(fd,36) = auth_dat[i].state;
  2593. memcpy(WFIFOP(fd,40), auth_dat[i].error_message, 20);
  2594. memcpy(WFIFOP(fd,60), auth_dat[i].lastlogin, 24);
  2595. memcpy(WFIFOP(fd,84), auth_dat[i].last_ip, 16);
  2596. memcpy(WFIFOP(fd,100), auth_dat[i].email, 40);
  2597. WFIFOL(fd,140) = (unsigned long)auth_dat[i].connect_until_time;
  2598. WFIFOL(fd,144) = (unsigned long)auth_dat[i].ban_until_time;
  2599. WFIFOW(fd,148) = (uint16)strlen(auth_dat[i].memo);
  2600. if (auth_dat[i].memo[0]) {
  2601. memcpy(WFIFOP(fd,150), auth_dat[i].memo, strlen(auth_dat[i].memo));
  2602. }
  2603. WFIFOSET(fd,150+strlen(auth_dat[i].memo));
  2604. break;
  2605. }
  2606. }
  2607. if (i == auth_num) {
  2608. login_log("'ladmin': Attempt to obtain information (by the id) of an unknown account (id: %d, ip: %s)\n",
  2609. RFIFOL(fd,2), ip);
  2610. strncpy((char*)WFIFOP(fd,7), "", 24);
  2611. WFIFOW(fd,148) = 0;
  2612. WFIFOSET(fd,150);
  2613. }
  2614. RFIFOSKIP(fd,6);
  2615. break;
  2616. case 0x7955: // Request to reload GM file (no answer)
  2617. login_log("'ladmin': Request to re-load GM configuration file (ip: %s).\n", ip);
  2618. read_gm_account();
  2619. // send GM accounts to all char-servers
  2620. send_GM_accounts(-1);
  2621. RFIFOSKIP(fd,2);
  2622. break;
  2623. default:
  2624. {
  2625. FILE *logfp;
  2626. char tmpstr[24];
  2627. time_t raw_time;
  2628. logfp = fopen(login_log_unknown_packets_filename, "a");
  2629. if (logfp) {
  2630. time(&raw_time);
  2631. strftime(tmpstr, 23, login_config.date_format, localtime(&raw_time));
  2632. fprintf(logfp, "%s: receiving of an unknown packet -> disconnection\n", tmpstr);
  2633. fprintf(logfp, "parse_admin: connection #%d (ip: %s), packet: 0x%x (with being read: %lu).\n", fd, ip, command, (unsigned long)RFIFOREST(fd));
  2634. fprintf(logfp, "Detail (in hex):\n");
  2635. fprintf(logfp, "---- 00-01-02-03-04-05-06-07 08-09-0A-0B-0C-0D-0E-0F\n");
  2636. memset(tmpstr, '\0', sizeof(tmpstr));
  2637. for(i = 0; i < RFIFOREST(fd); i++) {
  2638. if ((i & 15) == 0)
  2639. fprintf(logfp, "%04X ",i);
  2640. fprintf(logfp, "%02x ", RFIFOB(fd,i));
  2641. if (RFIFOB(fd,i) > 0x1f)
  2642. tmpstr[i % 16] = RFIFOB(fd,i);
  2643. else
  2644. tmpstr[i % 16] = '.';
  2645. if ((i - 7) % 16 == 0) // -8 + 1
  2646. fprintf(logfp, " ");
  2647. else if ((i + 1) % 16 == 0) {
  2648. fprintf(logfp, " %s\n", tmpstr);
  2649. memset(tmpstr, '\0', sizeof(tmpstr));
  2650. }
  2651. }
  2652. if (i % 16 != 0) {
  2653. for(j = i; j % 16 != 0; j++) {
  2654. fprintf(logfp, " ");
  2655. if ((j - 7) % 16 == 0) // -8 + 1
  2656. fprintf(logfp, " ");
  2657. }
  2658. fprintf(logfp, " %s\n", tmpstr);
  2659. }
  2660. fprintf(logfp, "\n");
  2661. fclose(logfp);
  2662. }
  2663. }
  2664. login_log("'ladmin': End of connection, unknown packet (ip: %s)\n", ip);
  2665. set_eof(fd);
  2666. ShowWarning("Remote administration has been disconnected (unknown packet).\n");
  2667. return 0;
  2668. }
  2669. }
  2670. RFIFOSKIP(fd,RFIFOREST(fd));
  2671. return 0;
  2672. }
  2673. //--------------------------------------------
  2674. // Test to know if an IP come from LAN or WAN.
  2675. //--------------------------------------------
  2676. int lan_subnetcheck(uint32 ip)
  2677. {
  2678. int i;
  2679. ARR_FIND( 0, subnet_count, i, (subnet[i].char_ip & subnet[i].mask) == (ip & subnet[i].mask) );
  2680. return ( i < subnet_count ) ? subnet[i].char_ip : 0;
  2681. }
  2682. //----------------------------------------------------------------------------------------
  2683. // Default packet parsing (normal players or administation/char-server connection requests)
  2684. //----------------------------------------------------------------------------------------
  2685. int parse_login(int fd)
  2686. {
  2687. struct mmo_account account;
  2688. int result, j;
  2689. unsigned int i;
  2690. uint32 ipl = session[fd]->client_addr;
  2691. char ip[16];
  2692. if (session[fd]->eof) {
  2693. do_close(fd);
  2694. return 0;
  2695. }
  2696. ip2str(ipl, ip);
  2697. while( RFIFOREST(fd) >= 2 )
  2698. {
  2699. uint16 command = RFIFOW(fd,0);
  2700. if (display_parse_login)
  2701. {
  2702. if (command == 0x0064 || command == 0x01dd)
  2703. {
  2704. if ((int)RFIFOREST(fd) >= ((command == 0x0064) ? 55 : 47))
  2705. ShowDebug("parse_login: connection #%d, packet: 0x%x (with being read: %d), account: %s.\n", fd, command, RFIFOREST(fd), RFIFOP(fd,6));
  2706. }
  2707. else if (command == 0x2710)
  2708. {
  2709. if (RFIFOREST(fd) >= 86)
  2710. ShowDebug("parse_login: connection #%d, packet: 0x%x (with being read: %d), server: %s.\n", fd, command, RFIFOREST(fd), RFIFOP(fd,60));
  2711. }
  2712. else
  2713. ShowDebug("parse_login: connection #%d, packet: 0x%x (with being read: %d).\n", fd, command, RFIFOREST(fd));
  2714. }
  2715. switch(command)
  2716. {
  2717. case 0x0200: // New alive packet: structure: 0x200 <account.userid>.24B. used to verify if client is always alive.
  2718. if (RFIFOREST(fd) < 26)
  2719. return 0;
  2720. RFIFOSKIP(fd,26);
  2721. break;
  2722. case 0x0204: // New alive packet: structure: 0x204 <encrypted.account.userid>.16B. (new ragexe from 22 june 2004)
  2723. if (RFIFOREST(fd) < 18)
  2724. return 0;
  2725. RFIFOSKIP(fd,18);
  2726. break;
  2727. case 0x0064: // request client login
  2728. case 0x01dd: // request client login (encryption mode)
  2729. case 0x0277: // New login packet (kRO 2006-04-24aSakexe langtype 0)
  2730. case 0x02b0: // New login packet (kRO 2007-05-14aSakexe langtype 0)
  2731. {
  2732. int packet_len = RFIFOREST(fd); // assume no other packet was sent
  2733. //Perform ip-ban check
  2734. if (!check_ip(ipl))
  2735. {
  2736. login_log("Connection refused: IP isn't authorised (deny/allow, ip: %s).\n", ip);
  2737. WFIFOHEAD(fd,23);
  2738. WFIFOW(fd,0) = 0x6a;
  2739. WFIFOB(fd,2) = 3; // 3 = Rejected from Server
  2740. WFIFOSET(fd,23);
  2741. RFIFOSKIP(fd,packet_len);
  2742. set_eof(fd);
  2743. break;
  2744. }
  2745. if( (command == 0x0064 && packet_len < 55)
  2746. || (command == 0x01dd && packet_len < 47)
  2747. || (command == 0x0277 && packet_len < 84)
  2748. || (command == 0x02b0 && packet_len < 85) )
  2749. return 0;
  2750. // S 0064 <version>.l <account name>.24B <password>.24B <version2>.B
  2751. // S 01dd <version>.l <account name>.24B <md5 binary>.16B <version2>.B
  2752. // S 0277 <version>.l <account name>.24B <password>.24B <junk?>.29B <version2>.B
  2753. // S 02b0 <version>.l <account name>.24B <password>.24B <junk?>.30B <version2>.B
  2754. memset(&account, 0, sizeof(account));
  2755. account.version = RFIFOL(fd,2);
  2756. if (!account.version) account.version = 1; //Force some version...
  2757. memcpy(account.userid,RFIFOP(fd,6),NAME_LENGTH); account.userid[23] = '\0';
  2758. remove_control_chars(account.userid);
  2759. if (command != 0x01dd) {
  2760. login_log("Request for connection (non encryption mode) of %s (ip: %s).\n", account.userid, ip);
  2761. memcpy(account.passwd, RFIFOP(fd,30), NAME_LENGTH); account.passwd[23] = '\0';
  2762. remove_control_chars(account.passwd);
  2763. } else {
  2764. login_log("Request for connection (encryption mode) of %s (ip: %s).\n", account.userid, ip);
  2765. memcpy(account.passwd, RFIFOP(fd,30), 16); account.passwd[16] = '\0'; // binary data here
  2766. }
  2767. account.passwdenc = (command == 0x01dd) ? PASSWORDENC : 0;
  2768. result = mmo_auth(&account, fd);
  2769. if( result == -1 )
  2770. { // auth success
  2771. int gm_level = isGM(account.account_id);
  2772. if( login_config.min_level_to_connect > gm_level )
  2773. {
  2774. login_log("Connection refused: the minimum GM level for connection is %d (account: %s, GM level: %d, ip: %s).\n",
  2775. login_config.min_level_to_connect, account.userid, gm_level, ip);
  2776. WFIFOHEAD(fd,3);
  2777. WFIFOW(fd,0) = 0x81;
  2778. WFIFOB(fd,2) = 1; // 01 = Server closed
  2779. WFIFOSET(fd,3);
  2780. }
  2781. else
  2782. {
  2783. uint8 server_num = 0;
  2784. WFIFOHEAD(fd,47+32*MAX_SERVERS);
  2785. for( i = 0; i < MAX_SERVERS; ++i )
  2786. {
  2787. if( session_isValid(server_fd[i]) )
  2788. {
  2789. // Advanced subnet check [LuzZza]
  2790. uint32 subnet_char_ip = lan_subnetcheck(ipl);
  2791. WFIFOL(fd,47+server_num*32) = htonl((subnet_char_ip) ? subnet_char_ip : server[i].ip);
  2792. WFIFOW(fd,47+server_num*32+4) = ntows(htons(server[i].port)); // [!] LE byte order here [!]
  2793. memcpy(WFIFOP(fd,47+server_num*32+6), server[i].name, 20);
  2794. WFIFOW(fd,47+server_num*32+26) = server[i].users;
  2795. WFIFOW(fd,47+server_num*32+28) = server[i].maintenance;
  2796. WFIFOW(fd,47+server_num*32+30) = server[i].new_;
  2797. server_num++;
  2798. }
  2799. }
  2800. if (server_num > 0) { // if at least 1 char-server
  2801. if (gm_level)
  2802. ShowStatus("Connection of the GM (level:%d) account '%s' accepted.\n", gm_level, account.userid);
  2803. else
  2804. ShowStatus("Connection of the account '%s' accepted.\n", account.userid);
  2805. WFIFOW(fd,0) = 0x69;
  2806. WFIFOW(fd,2) = 47+32*server_num;
  2807. WFIFOL(fd,4) = account.login_id1;
  2808. WFIFOL(fd,8) = account.account_id;
  2809. WFIFOL(fd,12) = account.login_id2;
  2810. WFIFOL(fd,16) = 0; // in old version, that was for ip (not more used)
  2811. //memcpy(WFIFOP(fd,20), account.lastlogin, 24); // in old version, that was for name (not more used)
  2812. WFIFOB(fd,46) = account.sex;
  2813. WFIFOSET(fd,47+32*server_num);
  2814. if (auth_fifo_pos >= AUTH_FIFO_SIZE)
  2815. auth_fifo_pos = 0;
  2816. auth_fifo[auth_fifo_pos].account_id = account.account_id;
  2817. auth_fifo[auth_fifo_pos].login_id1 = account.login_id1;
  2818. auth_fifo[auth_fifo_pos].login_id2 = account.login_id2;
  2819. auth_fifo[auth_fifo_pos].sex = account.sex;
  2820. auth_fifo[auth_fifo_pos].delflag = 0;
  2821. auth_fifo[auth_fifo_pos].ip = session[fd]->client_addr;
  2822. auth_fifo_pos++;
  2823. } else { // if no char-server, don't send void list of servers, just disconnect the player with proper message
  2824. ShowStatus("Connection refused: there is no char-server online (account: %s, ip: %s).\n", account.userid, ip);
  2825. login_log("Connection refused: there is no char-server online (account: %s, ip: %s).\n", account.userid, ip);
  2826. WFIFOW(fd,0) = 0x81;
  2827. WFIFOB(fd,2) = 1; // 01 = Server closed
  2828. WFIFOSET(fd,3);
  2829. }
  2830. }
  2831. }
  2832. else
  2833. { // auth failed
  2834. WFIFOHEAD(fd,23);
  2835. WFIFOW(fd,0) = 0x6a;
  2836. WFIFOB(fd,2) = (uint8)result;
  2837. if( result == 6 )
  2838. {// 6 = Your are Prohibited to log in until %s
  2839. char tmpstr[20];
  2840. time_t ban_until_time;
  2841. i = search_account_index(account.userid);
  2842. ban_until_time = (i) ? auth_dat[i].ban_until_time : 0;
  2843. strftime(tmpstr, 20, login_config.date_format, localtime(&ban_until_time));
  2844. safestrncpy((char*)WFIFOP(fd,3), tmpstr, 20); // ban timestamp goes here
  2845. }
  2846. else
  2847. memset(WFIFOP(fd,3), '\0', 20);
  2848. WFIFOSET(fd,23);
  2849. }
  2850. RFIFOSKIP(fd,packet_len);
  2851. }
  2852. break;
  2853. case 0x01db: // Sending request of the coding key
  2854. case 0x791a: // Sending request of the coding key (administration packet)
  2855. {
  2856. struct login_session_data* ld;
  2857. if( session[fd]->session_data )
  2858. {
  2859. ShowWarning("login: abnormal request of MD5 key (already opened session).\n");
  2860. set_eof(fd);
  2861. return 0;
  2862. }
  2863. CREATE(ld, struct login_session_data, 1);
  2864. session[fd]->session_data = ld;
  2865. // Creation of the coding key
  2866. memset(ld->md5key, '\0', sizeof(ld->md5key));
  2867. ld->md5keylen = (uint16)(12 + rand() % 4);
  2868. for( i = 0; i < ld->md5keylen; ++i )
  2869. ld->md5key[i] = (char)(1 + rand() % 255);
  2870. WFIFOHEAD(fd,4 + ld->md5keylen);
  2871. WFIFOW(fd,0) = 0x01dc;
  2872. WFIFOW(fd,2) = 4 + ld->md5keylen;
  2873. memcpy(WFIFOP(fd,4), ld->md5key, ld->md5keylen);
  2874. WFIFOSET(fd,WFIFOW(fd,2));
  2875. RFIFOSKIP(fd,2);
  2876. }
  2877. break;
  2878. case 0x2710: // Connection request of a char-server
  2879. if (RFIFOREST(fd) < 86)
  2880. return 0;
  2881. {
  2882. char* server_name;
  2883. uint32 server_ip;
  2884. uint16 server_port;
  2885. memset(&account, 0, sizeof(account));
  2886. safestrncpy(account.userid, RFIFOP(fd,2), NAME_LENGTH); remove_control_chars(account.userid);
  2887. safestrncpy(account.passwd, RFIFOP(fd,26), NAME_LENGTH); remove_control_chars(account.passwd);
  2888. account.passwdenc = 0;
  2889. server_name = (char*)RFIFOP(fd,60); server_name[20] = '\0'; remove_control_chars(server_name);
  2890. server_ip = ntohl(RFIFOL(fd, 54));
  2891. server_port = ntohs(RFIFOW(fd, 58));
  2892. ShowInfo("Connection request of the char-server '%s' @ %d.%d.%d.%d:%d (account: '%s', pass: '%s', ip: '%s')\n", server_name, CONVIP(server_ip), server_port, account.userid, account.passwd, ip);
  2893. login_log("Connection request of the char-server '%s' @ %d.%d.%d.%d:%d (account: '%s', pass: '%s', ip: '%s')\n", server_name, CONVIP(server_ip), server_port, account.userid, account.passwd, ip);
  2894. result = mmo_auth(&account, fd);
  2895. if( result == -1 && account.sex == 2 && account.account_id < MAX_SERVERS && server_fd[account.account_id] == -1 )
  2896. {
  2897. ShowStatus("Connection of the char-server '%s' accepted.\n", server_name);
  2898. login_log("Connection of the char-server '%s' accepted.\n", server_name);
  2899. memset(&server[account.account_id], 0, sizeof(struct mmo_char_server));
  2900. server[account.account_id].ip = ntohl(RFIFOL(fd,54));
  2901. server[account.account_id].port = ntohs(RFIFOW(fd,58));
  2902. safestrncpy(server[account.account_id].name, server_name, sizeof(server[account.account_id].name));
  2903. server[account.account_id].users = 0;
  2904. server[account.account_id].maintenance = RFIFOW(fd,82);
  2905. server[account.account_id].new_ = RFIFOW(fd,84);
  2906. server_fd[account.account_id] = fd;
  2907. WFIFOHEAD(fd,3);
  2908. WFIFOW(fd,0) = 0x2711;
  2909. WFIFOB(fd,2) = 0;
  2910. WFIFOSET(fd,3);
  2911. session[fd]->func_parse = parse_fromchar;
  2912. session[fd]->client_addr = 0;
  2913. realloc_fifo(fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  2914. send_GM_accounts(fd); // send GM account to char-server
  2915. }
  2916. else
  2917. {
  2918. ShowNotice("Connection of the char-server '%s' REFUSED.\n", server_name);
  2919. login_log("Connection of the char-server '%s' REFUSED.\n", server_name);
  2920. WFIFOHEAD(fd,3);
  2921. WFIFOW(fd,0) = 0x2711;
  2922. WFIFOB(fd,2) = 3;
  2923. WFIFOSET(fd,3);
  2924. }
  2925. }
  2926. RFIFOSKIP(fd,86);
  2927. return 0;
  2928. case 0x7530: // Server version information request
  2929. login_log("Sending of the server version (ip: %s)\n", ip);
  2930. WFIFOHEAD(fd,10);
  2931. WFIFOW(fd,0) = 0x7531;
  2932. WFIFOB(fd,2) = ATHENA_MAJOR_VERSION;
  2933. WFIFOB(fd,3) = ATHENA_MINOR_VERSION;
  2934. WFIFOB(fd,4) = ATHENA_REVISION;
  2935. WFIFOB(fd,5) = ATHENA_RELEASE_FLAG;
  2936. WFIFOB(fd,6) = ATHENA_OFFICIAL_FLAG;
  2937. WFIFOB(fd,7) = ATHENA_SERVER_LOGIN;
  2938. WFIFOW(fd,8) = ATHENA_MOD_VERSION;
  2939. WFIFOSET(fd,10);
  2940. RFIFOSKIP(fd,2);
  2941. break;
  2942. case 0x7532: // Request to end connection
  2943. login_log("End of connection (ip: %s)\n", ip);
  2944. set_eof(fd);
  2945. return 0;
  2946. case 0x7918: // Request for administation login
  2947. if ((int)RFIFOREST(fd) < 4 || (int)RFIFOREST(fd) < ((RFIFOW(fd,2) == 0) ? 28 : 20))
  2948. return 0;
  2949. WFIFOW(fd,0) = 0x7919;
  2950. WFIFOB(fd,2) = 1;
  2951. if (!check_ladminip(session[fd]->client_addr)) {
  2952. login_log("'ladmin'-login: Connection in administration mode refused: IP isn't authorised (ladmin_allow, ip: %s).\n", ip);
  2953. } else {
  2954. struct login_session_data *ld = (struct login_session_data*)session[fd]->session_data;
  2955. if (RFIFOW(fd,2) == 0) { // non encrypted password
  2956. char password[25];
  2957. memcpy(password, RFIFOP(fd,4), 24);
  2958. password[24] = '\0';
  2959. remove_control_chars(password);
  2960. // If remote administration is enabled and password sent by client matches password read from login server configuration file
  2961. if ((admin_state == 1) && (strcmp(password, admin_pass) == 0)) {
  2962. login_log("'ladmin'-login: Connection in administration mode accepted (non encrypted password: %s, ip: %s)\n", password, ip);
  2963. ShowNotice("Connection of a remote administration accepted (non encrypted password).\n");
  2964. WFIFOB(fd,2) = 0;
  2965. session[fd]->func_parse = parse_admin;
  2966. } else if (admin_state != 1)
  2967. login_log("'ladmin'-login: Connection in administration mode REFUSED - remote administration is disabled (non encrypted password: %s, ip: %s)\n", password, ip);
  2968. else
  2969. login_log("'ladmin'-login: Connection in administration mode REFUSED - invalid password (non encrypted password: %s, ip: %s)\n", password, ip);
  2970. } else { // encrypted password
  2971. if (!ld)
  2972. ShowError("'ladmin'-login: error! MD5 key not created/requested for an administration login.\n");
  2973. else {
  2974. char md5str[64] = "", md5bin[32];
  2975. if (RFIFOW(fd,2) == 1) {
  2976. sprintf(md5str, "%s%s", ld->md5key, admin_pass); // 20 24
  2977. } else if (RFIFOW(fd,2) == 2) {
  2978. sprintf(md5str, "%s%s", admin_pass, ld->md5key); // 24 20
  2979. }
  2980. MD5_String2binary(md5str, md5bin);
  2981. // If remote administration is enabled and password hash sent by client matches hash of password read from login server configuration file
  2982. if ((admin_state == 1) && (memcmp(md5bin, RFIFOP(fd,4), 16) == 0)) {
  2983. login_log("'ladmin'-login: Connection in administration mode accepted (encrypted password, ip: %s)\n", ip);
  2984. ShowNotice("Connection of a remote administration accepted (encrypted password).\n");
  2985. WFIFOB(fd,2) = 0;
  2986. session[fd]->func_parse = parse_admin;
  2987. } else if (admin_state != 1)
  2988. login_log("'ladmin'-login: Connection in administration mode REFUSED - remote administration is disabled (encrypted password, ip: %s)\n", ip);
  2989. else
  2990. login_log("'ladmin'-login: Connection in administration mode REFUSED - invalid password (encrypted password, ip: %s)\n", ip);
  2991. }
  2992. }
  2993. }
  2994. WFIFOSET(fd,3);
  2995. RFIFOSKIP(fd, (RFIFOW(fd,2) == 0) ? 28 : 20);
  2996. break;
  2997. default:
  2998. if (save_unknown_packets) {
  2999. FILE *logfp;
  3000. char tmpstr[24];
  3001. time_t raw_time;
  3002. logfp = fopen(login_log_unknown_packets_filename, "a");
  3003. if (logfp) {
  3004. time(&raw_time);
  3005. strftime(tmpstr, 23, login_config.date_format, localtime(&raw_time));
  3006. fprintf(logfp, "%s: receiving of an unknown packet -> disconnection\n", tmpstr);
  3007. fprintf(logfp, "parse_login: connection #%d (ip: %s), packet: 0x%x (with being read: %lu).\n", fd, ip, command, (unsigned long)RFIFOREST(fd));
  3008. fprintf(logfp, "Detail (in hex):\n");
  3009. fprintf(logfp, "---- 00-01-02-03-04-05-06-07 08-09-0A-0B-0C-0D-0E-0F\n");
  3010. memset(tmpstr, '\0', sizeof(tmpstr));
  3011. for(i = 0; i < RFIFOREST(fd); i++) {
  3012. if ((i & 15) == 0)
  3013. fprintf(logfp, "%04X ",i);
  3014. fprintf(logfp, "%02x ", RFIFOB(fd,i));
  3015. if (RFIFOB(fd,i) > 0x1f)
  3016. tmpstr[i % 16] = RFIFOB(fd,i);
  3017. else
  3018. tmpstr[i % 16] = '.';
  3019. if ((i - 7) % 16 == 0) // -8 + 1
  3020. fprintf(logfp, " ");
  3021. else if ((i + 1) % 16 == 0) {
  3022. fprintf(logfp, " %s\n", tmpstr);
  3023. memset(tmpstr, '\0', sizeof(tmpstr));
  3024. }
  3025. }
  3026. if (i % 16 != 0) {
  3027. for(j = i; j % 16 != 0; j++) {
  3028. fprintf(logfp, " ");
  3029. if ((j - 7) % 16 == 0) // -8 + 1
  3030. fprintf(logfp, " ");
  3031. }
  3032. fprintf(logfp, " %s\n", tmpstr);
  3033. }
  3034. fprintf(logfp, "\n");
  3035. fclose(logfp);
  3036. }
  3037. }
  3038. login_log("Abnormal end of connection (ip: %s): Unknown packet 0x%x\n", ip, command);
  3039. set_eof(fd);
  3040. return 0;
  3041. }
  3042. }
  3043. RFIFOSKIP(fd,RFIFOREST(fd));
  3044. return 0;
  3045. }
  3046. //-----------------------
  3047. // Console Command Parser [Wizputer]
  3048. //-----------------------
  3049. int parse_console(char* buf)
  3050. {
  3051. char command[256];
  3052. memset(command, 0, sizeof(command));
  3053. sscanf(buf, "%[^\n]", command);
  3054. ShowInfo("Console command :%s", command);
  3055. login_log("Console command :%s\n", command);
  3056. if( strcmpi("shutdown", command) == 0 ||
  3057. strcmpi("exit", command) == 0 ||
  3058. strcmpi("quit", command) == 0 ||
  3059. strcmpi("end", command) == 0 )
  3060. runflag = 0;
  3061. else
  3062. if( strcmpi("alive", command) == 0 ||
  3063. strcmpi("status", command) == 0 )
  3064. ShowInfo(CL_CYAN"Console: "CL_BOLD"I'm Alive."CL_RESET"\n");
  3065. else
  3066. if( strcmpi("help", command) == 0 ) {
  3067. printf(CL_BOLD"Help of commands:"CL_RESET"\n");
  3068. printf(" To shutdown the server:\n");
  3069. printf(" 'shutdown|exit|quit|end'\n");
  3070. printf(" To know if server is alive:\n");
  3071. printf(" 'alive|status'\n");
  3072. }
  3073. return 0;
  3074. }
  3075. static int online_data_cleanup_sub(DBKey key, void *data, va_list ap)
  3076. {
  3077. struct online_login_data *character= (struct online_login_data*)data;
  3078. if (character->char_server == -2) //Unknown server.. set them offline
  3079. remove_online_user(character->account_id);
  3080. else if (character->char_server < 0)
  3081. //Free data from players that have not been online for a while.
  3082. db_remove(online_db, key);
  3083. return 0;
  3084. }
  3085. static int online_data_cleanup(int tid, unsigned int tick, int id, int data)
  3086. {
  3087. online_db->foreach(online_db, online_data_cleanup_sub);
  3088. return 0;
  3089. }
  3090. //----------------------------------
  3091. // Reading Lan Support configuration
  3092. //----------------------------------
  3093. int login_lan_config_read(const char *lancfgName)
  3094. {
  3095. FILE *fp;
  3096. int line_num = 0;
  3097. char line[1024], w1[64], w2[64], w3[64], w4[64];
  3098. if((fp = fopen(lancfgName, "r")) == NULL) {
  3099. ShowWarning("LAN Support configuration file is not found: %s\n", lancfgName);
  3100. return 1;
  3101. }
  3102. ShowInfo("Reading the configuration file %s...\n", lancfgName);
  3103. while(fgets(line, sizeof(line), fp))
  3104. {
  3105. line_num++;
  3106. if ((line[0] == '/' && line[1] == '/') || line[0] == '\n' || line[1] == '\n')
  3107. continue;
  3108. if(sscanf(line,"%[^:]: %[^:]:%[^:]:%[^\r\n]", w1, w2, w3, w4) != 4)
  3109. {
  3110. ShowWarning("Error syntax of configuration file %s in line %d.\n", lancfgName, line_num);
  3111. continue;
  3112. }
  3113. remove_control_chars(w1);
  3114. remove_control_chars(w2);
  3115. remove_control_chars(w3);
  3116. remove_control_chars(w4);
  3117. if( strcmpi(w1, "subnet") == 0 )
  3118. {
  3119. subnet[subnet_count].mask = str2ip(w2);
  3120. subnet[subnet_count].char_ip = str2ip(w3);
  3121. subnet[subnet_count].map_ip = str2ip(w4);
  3122. if( (subnet[subnet_count].char_ip & subnet[subnet_count].mask) != (subnet[subnet_count].map_ip&subnet[subnet_count].mask) )
  3123. {
  3124. ShowError("%s: Configuration Error: The char server (%s) and map server (%s) belong to different subnetworks!\n", lancfgName, w3, w4);
  3125. continue;
  3126. }
  3127. subnet_count++;
  3128. }
  3129. ShowStatus("Read information about %d subnetworks.\n", subnet_count);
  3130. }
  3131. fclose(fp);
  3132. return 0;
  3133. }
  3134. //-----------------------------------
  3135. // Reading main configuration file
  3136. //-----------------------------------
  3137. int login_config_read(const char* cfgName)
  3138. {
  3139. char line[1024], w1[1024], w2[1024];
  3140. FILE* fp = fopen(cfgName, "r");
  3141. if (fp == NULL) {
  3142. ShowError("Configuration file (%s) not found.\n", cfgName);
  3143. return 1;
  3144. }
  3145. ShowInfo("Reading configuration file %s...\n", cfgName);
  3146. while(fgets(line, sizeof(line), fp))
  3147. {
  3148. if (line[0] == '/' && line[1] == '/')
  3149. continue;
  3150. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) < 2)
  3151. continue;
  3152. remove_control_chars(w1);
  3153. remove_control_chars(w2);
  3154. if(!strcmpi(w1,"timestamp_format"))
  3155. strncpy(timestamp_format, w2, 20);
  3156. else if(!strcmpi(w1,"stdout_with_ansisequence"))
  3157. stdout_with_ansisequence = config_switch(w2);
  3158. else if(!strcmpi(w1,"console_silent")) {
  3159. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  3160. msg_silent = atoi(w2);
  3161. }
  3162. else if (strcmpi(w1, "admin_state") == 0) {
  3163. admin_state = config_switch(w2);
  3164. } else if (strcmpi(w1, "admin_pass") == 0) {
  3165. memset(admin_pass, 0, sizeof(admin_pass));
  3166. strncpy(admin_pass, w2, sizeof(admin_pass));
  3167. admin_pass[sizeof(admin_pass)-1] = '\0';
  3168. } else if (strcmpi(w1, "ladminallowip") == 0) {
  3169. if (strcmpi(w2, "clear") == 0) {
  3170. if (access_ladmin_allow)
  3171. aFree(access_ladmin_allow);
  3172. access_ladmin_allow = NULL;
  3173. access_ladmin_allownum = 0;
  3174. } else {
  3175. if (strcmpi(w2, "all") == 0) {
  3176. // reset all previous values
  3177. if (access_ladmin_allow)
  3178. aFree(access_ladmin_allow);
  3179. // set to all
  3180. access_ladmin_allow = (char*)aCalloc(ACO_STRSIZE, sizeof(char));
  3181. access_ladmin_allownum = 1;
  3182. access_ladmin_allow[0] = '\0';
  3183. } else if (w2[0] && !(access_ladmin_allownum == 1 && access_ladmin_allow[0] == '\0')) { // don't add IP if already 'all'
  3184. if (access_ladmin_allow)
  3185. access_ladmin_allow = (char*)aRealloc(access_ladmin_allow, (access_ladmin_allownum+1) * ACO_STRSIZE);
  3186. else
  3187. access_ladmin_allow = (char*)aCalloc(ACO_STRSIZE, sizeof(char));
  3188. strncpy(access_ladmin_allow + (access_ladmin_allownum++) * ACO_STRSIZE, w2, ACO_STRSIZE);
  3189. access_ladmin_allow[access_ladmin_allownum * ACO_STRSIZE - 1] = '\0';
  3190. }
  3191. }
  3192. } else if (strcmpi(w1, "gm_pass") == 0) {
  3193. memset(gm_pass, 0, sizeof(gm_pass));
  3194. strncpy(gm_pass, w2, sizeof(gm_pass));
  3195. gm_pass[sizeof(gm_pass)-1] = '\0';
  3196. } else if (strcmpi(w1, "level_new_gm") == 0) {
  3197. level_new_gm = atoi(w2);
  3198. }
  3199. else if( !strcmpi(w1, "bind_ip") ) {
  3200. char ip_str[16];
  3201. login_config.login_ip = host2ip(w2);
  3202. if( login_config.login_ip )
  3203. ShowStatus("Login server binding IP address : %s -> %s\n", w2, ip2str(login_config.login_ip, ip_str));
  3204. }
  3205. else if( !strcmpi(w1, "login_port") ) {
  3206. login_config.login_port = (uint16)atoi(w2);
  3207. ShowStatus("set login_port : %s\n",w2);
  3208. }
  3209. else if (strcmpi(w1, "account_filename") == 0) {
  3210. memset(account_filename, 0, sizeof(account_filename));
  3211. strncpy(account_filename, w2, sizeof(account_filename));
  3212. account_filename[sizeof(account_filename)-1] = '\0';
  3213. } else if (strcmpi(w1, "gm_account_filename") == 0) {
  3214. memset(GM_account_filename, 0, sizeof(GM_account_filename));
  3215. strncpy(GM_account_filename, w2, sizeof(GM_account_filename));
  3216. GM_account_filename[sizeof(GM_account_filename)-1] = '\0';
  3217. } else if (strcmpi(w1, "gm_account_filename_check_timer") == 0) {
  3218. gm_account_filename_check_timer = atoi(w2);
  3219. } else if (strcmpi(w1, "log_login") == 0) {
  3220. login_config.log_login = config_switch(w2);
  3221. } else if (strcmpi(w1, "login_log_filename") == 0) {
  3222. memset(login_log_filename, 0, sizeof(login_log_filename));
  3223. strncpy(login_log_filename, w2, sizeof(login_log_filename));
  3224. login_log_filename[sizeof(login_log_filename)-1] = '\0';
  3225. } else if (strcmpi(w1, "login_log_unknown_packets_filename") == 0) {
  3226. memset(login_log_unknown_packets_filename, 0, sizeof(login_log_unknown_packets_filename));
  3227. strncpy(login_log_unknown_packets_filename, w2, sizeof(login_log_unknown_packets_filename));
  3228. login_log_unknown_packets_filename[sizeof(login_log_unknown_packets_filename)-1] = '\0';
  3229. } else if (strcmpi(w1, "save_unknown_packets") == 0) {
  3230. save_unknown_packets = config_switch(w2);
  3231. } else if (strcmpi(w1, "display_parse_login") == 0) {
  3232. display_parse_login = config_switch(w2);
  3233. } else if (strcmpi(w1, "display_parse_admin") == 0) {
  3234. display_parse_admin = config_switch(w2);
  3235. } else if (strcmpi(w1, "display_parse_fromchar") == 0) {
  3236. display_parse_fromchar = config_switch(w2); // 0: no, 1: yes (without packet 0x2714), 2: all packets
  3237. } else if (strcmpi(w1, "add_to_unlimited_account") == 0) {
  3238. add_to_unlimited_account = config_switch(w2);
  3239. } else if (strcmpi(w1, "start_limited_time") == 0) {
  3240. start_limited_time = atoi(w2);
  3241. } else if (strcmpi(w1, "order") == 0) {
  3242. access_order = atoi(w2);
  3243. if (strcmpi(w2, "deny,allow") == 0 ||
  3244. strcmpi(w2, "deny, allow") == 0) access_order = ACO_DENY_ALLOW;
  3245. if (strcmpi(w2, "allow,deny") == 0 ||
  3246. strcmpi(w2, "allow, deny") == 0) access_order = ACO_ALLOW_DENY;
  3247. if (strcmpi(w2, "mutual-failture") == 0 ||
  3248. strcmpi(w2, "mutual-failure") == 0) access_order = ACO_MUTUAL_FAILTURE;
  3249. } else if (strcmpi(w1, "allow") == 0) {
  3250. if (strcmpi(w2, "clear") == 0) {
  3251. if (access_allow)
  3252. aFree(access_allow);
  3253. access_allow = NULL;
  3254. access_allownum = 0;
  3255. } else {
  3256. if (strcmpi(w2, "all") == 0) {
  3257. // reset all previous values
  3258. if (access_allow)
  3259. aFree(access_allow);
  3260. // set to all
  3261. access_allow = (char*)aCalloc(ACO_STRSIZE, sizeof(char));
  3262. access_allownum = 1;
  3263. access_allow[0] = '\0';
  3264. } else if (w2[0] && !(access_allownum == 1 && access_allow[0] == '\0')) { // don't add IP if already 'all'
  3265. if (access_allow)
  3266. access_allow = (char*)aRealloc(access_allow, (access_allownum+1) * ACO_STRSIZE);
  3267. else
  3268. access_allow = (char*)aCalloc(ACO_STRSIZE, sizeof(char));
  3269. strncpy(access_allow + (access_allownum++) * ACO_STRSIZE, w2, ACO_STRSIZE);
  3270. access_allow[access_allownum * ACO_STRSIZE - 1] = '\0';
  3271. }
  3272. }
  3273. } else if (strcmpi(w1, "deny") == 0) {
  3274. if (strcmpi(w2, "clear") == 0) {
  3275. if (access_deny)
  3276. aFree(access_deny);
  3277. access_deny = NULL;
  3278. access_denynum = 0;
  3279. } else {
  3280. if (strcmpi(w2, "all") == 0) {
  3281. // reset all previous values
  3282. if (access_deny)
  3283. aFree(access_deny);
  3284. // set to all
  3285. access_deny = (char*)aCalloc(ACO_STRSIZE, sizeof(char));
  3286. access_denynum = 1;
  3287. access_deny[0] = '\0';
  3288. } else if (w2[0] && !(access_denynum == 1 && access_deny[0] == '\0')) { // don't add IP if already 'all'
  3289. if (access_deny)
  3290. access_deny = (char*)aRealloc(access_deny, (access_denynum+1) * ACO_STRSIZE);
  3291. else
  3292. access_deny = (char*)aCalloc(ACO_STRSIZE, sizeof(char));
  3293. strncpy(access_deny + (access_denynum++) * ACO_STRSIZE, w2, ACO_STRSIZE);
  3294. access_deny[access_denynum * ACO_STRSIZE - 1] = '\0';
  3295. }
  3296. }
  3297. }
  3298. else if(!strcmpi(w1, "new_account"))
  3299. login_config.new_account_flag = (bool)config_switch(w2);
  3300. else if(!strcmpi(w1, "check_client_version"))
  3301. login_config.check_client_version = (bool)config_switch(w2);
  3302. else if(!strcmpi(w1, "client_version_to_connect"))
  3303. login_config.client_version_to_connect = (unsigned int)atoi(w2);
  3304. else if(!strcmpi(w1, "use_MD5_passwords"))
  3305. login_config.use_md5_passwds = (bool)config_switch(w2);
  3306. else if(!strcmpi(w1, "min_level_to_connect"))
  3307. login_config.min_level_to_connect = atoi(w2);
  3308. else if(!strcmpi(w1, "date_format"))
  3309. safestrncpy(login_config.date_format, w2, sizeof(login_config.date_format));
  3310. else if(!strcmpi(w1, "console"))
  3311. login_config.console = config_switch(w2);
  3312. // else if(!strcmpi(w1, "case_sensitive"))
  3313. // login_config.case_sensitive = config_switch(w2);
  3314. else if(!strcmpi(w1, "allowed_regs")) //account flood protection system
  3315. allowed_regs = atoi(w2);
  3316. else if(!strcmpi(w1, "time_allowed"))
  3317. time_allowed = atoi(w2);
  3318. else if(!strcmpi(w1, "online_check"))
  3319. login_config.online_check = (bool)config_switch(w2);
  3320. else if(!strcmpi(w1, "use_dnsbl"))
  3321. login_config.use_dnsbl = (bool)config_switch(w2);
  3322. else if(!strcmpi(w1, "dnsbl_servers"))
  3323. safestrncpy(login_config.dnsbl_servs, w2, sizeof(login_config.dnsbl_servs));
  3324. else if(!strcmpi(w1, "ip_sync_interval"))
  3325. login_config.ip_sync_interval = (unsigned int)1000*60*atoi(w2); //w2 comes in minutes.
  3326. else if(!strcmpi(w1, "import"))
  3327. login_config_read(w2);
  3328. }
  3329. fclose(fp);
  3330. ShowInfo("Finished reading %s.\n", cfgName);
  3331. return 0;
  3332. }
  3333. //-------------------------------------
  3334. // Displaying of configuration warnings
  3335. //-------------------------------------
  3336. void display_conf_warnings(void)
  3337. {
  3338. if (admin_state != 0 && admin_state != 1) {
  3339. ShowWarning("Invalid value for admin_state parameter -> setting to 0 (no remote admin).\n");
  3340. admin_state = 0;
  3341. }
  3342. if (admin_state == 1) {
  3343. if (admin_pass[0] == '\0') {
  3344. ShowWarning("Administrator password is void (admin_pass).\n");
  3345. } else if (strcmp(admin_pass, "admin") == 0) {
  3346. ShowWarning("You are using the default administrator password (admin_pass).\n");
  3347. ShowWarning(" We highly recommend that you change it.\n");
  3348. }
  3349. }
  3350. if (gm_pass[0] == '\0') {
  3351. ShowWarning("'To GM become' password is void (gm_pass).\n");
  3352. ShowWarning(" We highly recommend that you set one password.\n");
  3353. } else if (strcmp(gm_pass, "gm") == 0) {
  3354. ShowWarning("You are using the default GM password (gm_pass).\n");
  3355. ShowWarning(" We highly recommend that you change it.\n");
  3356. }
  3357. if (level_new_gm < 0 || level_new_gm > 99) {
  3358. ShowWarning("Invalid value for level_new_gm parameter -> setting to 60 (default).\n");
  3359. level_new_gm = 60;
  3360. }
  3361. if (login_config.new_account_flag != 0 && login_config.new_account_flag != 1) {
  3362. ShowWarning("Invalid value for new_account parameter -> setting to 0 (no new account).\n");
  3363. login_config.new_account_flag = 0;
  3364. }
  3365. if (login_config.login_port < 1024) {
  3366. ShowWarning("Invalid value for login_port parameter -> setting to 6900 (default).\n");
  3367. login_config.login_port = 6900;
  3368. }
  3369. if (gm_account_filename_check_timer < 0) {
  3370. ShowWarning("Invalid value for gm_account_filename_check_timer parameter. Setting to 15 sec (default).\n");
  3371. gm_account_filename_check_timer = 15;
  3372. } else if (gm_account_filename_check_timer == 1) {
  3373. ShowWarning("Invalid value for gm_account_filename_check_timer parameter. Setting to 2 sec (minimum value).\n");
  3374. gm_account_filename_check_timer = 2;
  3375. }
  3376. if (save_unknown_packets != 0 && save_unknown_packets != 1) {
  3377. ShowWarning("Invalid value for save_unknown_packets parameter -> setting to 0-no save.\n");
  3378. save_unknown_packets = 0;
  3379. }
  3380. if (display_parse_login != 0 && display_parse_login != 1) { // 0: no, 1: yes
  3381. ShowWarning("Invalid value for display_parse_login parameter -> setting to 0 (no display).\n");
  3382. display_parse_login = 0;
  3383. }
  3384. if (display_parse_admin != 0 && display_parse_admin != 1) { // 0: no, 1: yes
  3385. ShowWarning("Invalid value for display_parse_admin parameter -> setting to 0 (no display).\n");
  3386. display_parse_admin = 0;
  3387. }
  3388. if (display_parse_fromchar < 0 || display_parse_fromchar > 2) { // 0: no, 1: yes (without packet 0x2714), 2: all packets
  3389. ShowWarning("Invalid value for display_parse_fromchar parameter -> setting to 0 (no display).\n");
  3390. display_parse_fromchar = 0;
  3391. }
  3392. if (login_config.min_level_to_connect < 0) { // 0: all players, 1-99 at least gm level x
  3393. ShowWarning("Invalid value for min_level_to_connect (%d) parameter -> setting 0 (any player).\n", login_config.min_level_to_connect);
  3394. login_config.min_level_to_connect = 0;
  3395. } else if (login_config.min_level_to_connect > 99) { // 0: all players, 1-99 at least gm level x
  3396. ShowWarning("Invalid value for min_level_to_connect (%d) parameter -> setting to 99 (only GM level 99)\n", login_config.min_level_to_connect);
  3397. login_config.min_level_to_connect = 99;
  3398. }
  3399. if (add_to_unlimited_account != 0 && add_to_unlimited_account != 1) { // 0: no, 1: yes
  3400. ShowWarning("Invalid value for add_to_unlimited_account parameter\n");
  3401. ShowWarning(" -> setting to 0 (impossible to add a time to an unlimited account).\n");
  3402. add_to_unlimited_account = 0;
  3403. }
  3404. if (start_limited_time < -1) { // -1: create unlimited account, 0 or more: additionnal sec from now to create limited time
  3405. ShowWarning("Invalid value for start_limited_time parameter\n");
  3406. ShowWarning(" -> setting to -1 (new accounts are created with unlimited time).\n");
  3407. start_limited_time = -1;
  3408. }
  3409. if (access_order == ACO_DENY_ALLOW) {
  3410. if (access_denynum == 1 && access_deny[0] == '\0') {
  3411. ShowWarning("The IP security order is 'deny,allow' (allow if not deny) and you refuse ALL IP.\n");
  3412. }
  3413. } else if (access_order == ACO_ALLOW_DENY) {
  3414. if (access_allownum == 0) {
  3415. ShowWarning("The IP security order is 'allow,deny' (deny if not allow) but, NO IP IS AUTHORISED!\n");
  3416. }
  3417. } else { // ACO_MUTUAL_FAILTURE
  3418. if (access_allownum == 0) {
  3419. ShowWarning("The IP security order is 'mutual-failture'\n");
  3420. ShowWarning(" (allow if in the allow list and not in the deny list).\n");
  3421. ShowWarning(" But, NO IP IS AUTHORISED!\n");
  3422. } else if (access_denynum == 1 && access_deny[0] == '\0') {
  3423. ShowWarning("The IP security order is mutual-failture\n");
  3424. ShowWarning(" (allow if in the allow list and not in the deny list).\n");
  3425. ShowWarning(" But, you refuse ALL IP!\n");
  3426. }
  3427. }
  3428. return;
  3429. }
  3430. //-------------------------------
  3431. // Save configuration in log file
  3432. //-------------------------------
  3433. void save_config_in_log(void)
  3434. {
  3435. int i;
  3436. // a newline in the log...
  3437. login_log("");
  3438. login_log("The login-server starting...\n");
  3439. // save configuration in log file
  3440. login_log("The configuration of the server is set:\n");
  3441. if (admin_state != 1)
  3442. login_log("- with no remote administration.\n");
  3443. else if (admin_pass[0] == '\0')
  3444. login_log("- with a remote administration with a VOID password.\n");
  3445. else if (strcmp(admin_pass, "admin") == 0)
  3446. login_log("- with a remote administration with the DEFAULT password.\n");
  3447. else
  3448. login_log("- with a remote administration with the password of %d character(s).\n", strlen(admin_pass));
  3449. if (access_ladmin_allownum == 0 || (access_ladmin_allownum == 1 && access_ladmin_allow[0] == '\0')) {
  3450. login_log("- to accept any IP for remote administration\n");
  3451. } else {
  3452. login_log("- to accept following IP for remote administration:\n");
  3453. for(i = 0; i < access_ladmin_allownum; i++)
  3454. login_log(" %s\n", (char *)(access_ladmin_allow + i * ACO_STRSIZE));
  3455. }
  3456. if (gm_pass[0] == '\0')
  3457. login_log("- with a VOID 'To GM become' password (gm_pass).\n");
  3458. else if (strcmp(gm_pass, "gm") == 0)
  3459. login_log("- with the DEFAULT 'To GM become' password (gm_pass).\n");
  3460. else
  3461. login_log("- with a 'To GM become' password (gm_pass) of %d character(s).\n", strlen(gm_pass));
  3462. if (level_new_gm == 0)
  3463. login_log("- to refuse any creation of GM with @gm.\n");
  3464. else
  3465. login_log("- to create GM with level '%d' when @gm is used.\n", level_new_gm);
  3466. if (login_config.new_account_flag == 1)
  3467. login_log("- to ALLOW new users (with _F/_M).\n");
  3468. else
  3469. login_log("- to NOT ALLOW new users (with _F/_M).\n");
  3470. login_log("- with port: %d.\n", login_config.login_port);
  3471. login_log("- with the accounts file name: '%s'.\n", account_filename);
  3472. login_log("- with the GM accounts file name: '%s'.\n", GM_account_filename);
  3473. if (gm_account_filename_check_timer == 0)
  3474. login_log("- to NOT check GM accounts file modifications.\n");
  3475. else
  3476. login_log("- to check GM accounts file modifications every %d seconds.\n", gm_account_filename_check_timer);
  3477. if (login_config.use_md5_passwds == 0)
  3478. login_log("- to save password in plain text.\n");
  3479. else
  3480. login_log("- to save password with MD5 encrypting.\n");
  3481. // not necessary to log the 'login_log_filename', we are inside :)
  3482. login_log("- with the unknown packets file name: '%s'.\n", login_log_unknown_packets_filename);
  3483. if (save_unknown_packets)
  3484. login_log("- to SAVE all unkown packets.\n");
  3485. else
  3486. login_log("- to SAVE only unkown packets sending by a char-server or a remote administration.\n");
  3487. if (display_parse_login)
  3488. login_log("- to display normal parse packets on console.\n");
  3489. else
  3490. login_log("- to NOT display normal parse packets on console.\n");
  3491. if (display_parse_admin)
  3492. login_log("- to display administration parse packets on console.\n");
  3493. else
  3494. login_log("- to NOT display administration parse packets on console.\n");
  3495. if (display_parse_fromchar)
  3496. login_log("- to display char-server parse packets on console.\n");
  3497. else
  3498. login_log("- to NOT display char-server parse packets on console.\n");
  3499. if (login_config.min_level_to_connect == 0) // 0: all players, 1-99 at least gm level x
  3500. login_log("- with no minimum level for connection.\n");
  3501. else if (login_config.min_level_to_connect == 99)
  3502. login_log("- to accept only GM with level 99.\n");
  3503. else
  3504. login_log("- to accept only GM with level %d or more.\n", login_config.min_level_to_connect);
  3505. if (add_to_unlimited_account)
  3506. login_log("- to authorize adjustment (with timeadd ladmin) on an unlimited account.\n");
  3507. else
  3508. login_log("- to refuse adjustment (with timeadd ladmin) on an unlimited account. You must use timeset (ladmin command) before.\n");
  3509. if (start_limited_time < 0)
  3510. login_log("- to create new accounts with an unlimited time.\n");
  3511. else if (start_limited_time == 0)
  3512. login_log("- to create new accounts with a limited time: time of creation.\n");
  3513. else
  3514. login_log("- to create new accounts with a limited time: time of creation + %d second(s).\n", start_limited_time);
  3515. if (access_order == ACO_DENY_ALLOW) {
  3516. if (access_denynum == 0) {
  3517. login_log("- with the IP security order: 'deny,allow' (allow if not deny). You refuse no IP.\n");
  3518. } else if (access_denynum == 1 && access_deny[0] == '\0') {
  3519. login_log("- with the IP security order: 'deny,allow' (allow if not deny). You refuse ALL IP.\n");
  3520. } else {
  3521. login_log("- with the IP security order: 'deny,allow' (allow if not deny). Refused IP are:\n");
  3522. for(i = 0; i < access_denynum; i++)
  3523. login_log(" %s\n", (char *)(access_deny + i * ACO_STRSIZE));
  3524. }
  3525. } else if (access_order == ACO_ALLOW_DENY) {
  3526. if (access_allownum == 0) {
  3527. login_log("- with the IP security order: 'allow,deny' (deny if not allow). But, NO IP IS AUTHORISED!\n");
  3528. } else if (access_allownum == 1 && access_allow[0] == '\0') {
  3529. login_log("- with the IP security order: 'allow,deny' (deny if not allow). You authorise ALL IP.\n");
  3530. } else {
  3531. login_log("- with the IP security order: 'allow,deny' (deny if not allow). Authorised IP are:\n");
  3532. for(i = 0; i < access_allownum; i++)
  3533. login_log(" %s\n", (char *)(access_allow + i * ACO_STRSIZE));
  3534. }
  3535. } else { // ACO_MUTUAL_FAILTURE
  3536. login_log("- with the IP security order: 'mutual-failture' (allow if in the allow list and not in the deny list).\n");
  3537. if (access_allownum == 0) {
  3538. login_log(" But, NO IP IS AUTHORISED!\n");
  3539. } else if (access_denynum == 1 && access_deny[0] == '\0') {
  3540. login_log(" But, you refuse ALL IP!\n");
  3541. } else {
  3542. if (access_allownum == 1 && access_allow[0] == '\0') {
  3543. login_log(" You authorise ALL IP.\n");
  3544. } else {
  3545. login_log(" Authorised IP are:\n");
  3546. for(i = 0; i < access_allownum; i++)
  3547. login_log(" %s\n", (char *)(access_allow + i * ACO_STRSIZE));
  3548. }
  3549. login_log(" Refused IP are:\n");
  3550. for(i = 0; i < access_denynum; i++)
  3551. login_log(" %s\n", (char *)(access_deny + i * ACO_STRSIZE));
  3552. }
  3553. }
  3554. }
  3555. void login_set_defaults()
  3556. {
  3557. login_config.login_ip = INADDR_ANY;
  3558. login_config.login_port = 6900;
  3559. login_config.ip_sync_interval = 0;
  3560. login_config.log_login = true;
  3561. safestrncpy(login_config.date_format, "%Y-%m-%d %H:%M:%S", sizeof(login_config.date_format));
  3562. login_config.console = false;
  3563. login_config.new_account_flag = true;
  3564. // login_config.case_sensitive = true;
  3565. login_config.use_md5_passwds = false;
  3566. // login_config.login_gm_read = true;
  3567. login_config.min_level_to_connect = 0;
  3568. login_config.online_check = true;
  3569. login_config.check_client_version = false;
  3570. login_config.client_version_to_connect = 20;
  3571. // login_config.ipban = true;
  3572. // login_config.dynamic_pass_failure_ban = true;
  3573. // login_config.dynamic_pass_failure_ban_interval = 5;
  3574. // login_config.dynamic_pass_failure_ban_limit = 7;
  3575. // login_config.dynamic_pass_failure_ban_duration = 5;
  3576. login_config.use_dnsbl = false;
  3577. safestrncpy(login_config.dnsbl_servs, "", sizeof(login_config.dnsbl_servs));
  3578. }
  3579. //--------------------------------------
  3580. // Function called at exit of the server
  3581. //--------------------------------------
  3582. void do_final(void)
  3583. {
  3584. int i, fd;
  3585. ShowInfo("Terminating...\n");
  3586. mmo_auth_sync();
  3587. online_db->destroy(online_db, NULL);
  3588. if(auth_dat) aFree(auth_dat);
  3589. if(gm_account_db) aFree(gm_account_db);
  3590. if(access_ladmin_allow) aFree(access_ladmin_allow);
  3591. if(access_allow) aFree(access_allow);
  3592. if(access_deny) aFree(access_deny);
  3593. for (i = 0; i < MAX_SERVERS; i++) {
  3594. if ((fd = server_fd[i]) >= 0) {
  3595. server_fd[i] = -1;
  3596. memset(&server[i], 0, sizeof(struct mmo_char_server));
  3597. do_close(fd);
  3598. }
  3599. }
  3600. do_close(login_fd);
  3601. login_log("----End of login-server (normal end with closing of all files).\n");
  3602. if(log_fp)
  3603. fclose(log_fp);
  3604. ShowStatus("Finished.\n");
  3605. }
  3606. //------------------------------
  3607. // Function called when the server
  3608. // has received a crash signal.
  3609. //------------------------------
  3610. void do_abort(void)
  3611. {
  3612. }
  3613. void set_server_type(void)
  3614. {
  3615. SERVER_TYPE = ATHENA_SERVER_LOGIN;
  3616. }
  3617. //------------------------------
  3618. // Login server initialization
  3619. //------------------------------
  3620. int do_init(int argc, char** argv)
  3621. {
  3622. int i, j;
  3623. login_set_defaults();
  3624. // read login-server configuration
  3625. login_config_read((argc > 1) ? argv[1] : LOGIN_CONF_NAME);
  3626. display_conf_warnings(); // not in login_config_read, because we can use 'import' option, and display same message twice or more
  3627. save_config_in_log(); // not before, because log file name can be changed
  3628. login_lan_config_read((argc > 2) ? argv[2] : LAN_CONF_NAME);
  3629. srand((unsigned int)time(NULL));
  3630. for( i = 0; i < AUTH_FIFO_SIZE; i++ )
  3631. auth_fifo[i].delflag = 1;
  3632. for( i = 0; i < MAX_SERVERS; i++ )
  3633. server_fd[i] = -1;
  3634. // Online user database init
  3635. online_db = db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
  3636. add_timer_func_list(waiting_disconnect_timer, "waiting_disconnect_timer");
  3637. // Auth init
  3638. mmo_auth_init();
  3639. // Read account information.
  3640. read_gm_account();
  3641. // set default parser as parse_login function
  3642. set_defaultparse(parse_login);
  3643. add_timer_func_list(check_auth_sync, "check_auth_sync");
  3644. add_timer_interval(gettick() + 60000, check_auth_sync, 0, 0, 60000); // every 60 sec we check if we must save accounts file (only if necessary to save)
  3645. // add timer to check GM accounts file modification
  3646. j = gm_account_filename_check_timer;
  3647. if (j == 0) // if we would not to check, we check every 60 sec, just to have timer (if we change timer, is was not necessary to check if timer already exists)
  3648. j = 60;
  3649. // every x sec we check if gm file has been changed
  3650. add_timer_func_list(check_GM_file, "check_GM_file");
  3651. add_timer_interval(gettick() + j * 1000, check_GM_file, 0, 0, j * 1000);
  3652. // every 10 minutes cleanup online account db.
  3653. add_timer_func_list(online_data_cleanup, "online_data_cleanup");
  3654. add_timer_interval(gettick() + 600*1000, online_data_cleanup, 0, 0, 600*1000);
  3655. // add timer to detect ip address change and perform update
  3656. if (login_config.ip_sync_interval) {
  3657. add_timer_func_list(sync_ip_addresses, "sync_ip_addresses");
  3658. add_timer_interval(gettick() + login_config.ip_sync_interval, sync_ip_addresses, 0, 0, login_config.ip_sync_interval);
  3659. }
  3660. if( login_config.console )
  3661. {
  3662. //##TODO invoke a CONSOLE_START plugin event
  3663. }
  3664. new_reg_tick = gettick();
  3665. // server port open & binding
  3666. login_fd = make_listen_bind(login_config.login_ip, login_config.login_port);
  3667. login_log("The login-server is ready (Server is listening on the port %d).\n", login_config.login_port);
  3668. ShowStatus("The login-server is "CL_GREEN"ready"CL_RESET" (Server is listening on the port %u).\n\n", login_config.login_port);
  3669. return 0;
  3670. }