login.c 154 KB

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