vlmcs_all.c 201 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525
  1. #ifndef CONFIG
  2. #define CONFIG "config.h"
  3. #endif // CONFIG
  4. #include CONFIG
  5. #ifndef _GNU_SOURCE
  6. #define _GNU_SOURCE
  7. #endif
  8. #include "vlmcs.h"
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <stdint.h>
  14. #include <getopt.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <unistd.h>
  18. #ifndef _WIN32
  19. #include <sys/ioctl.h>
  20. #include <termios.h>
  21. #else // _WIN32
  22. #endif // _WIN32
  23. #include "endian.h"
  24. #include "shared_globals.h"
  25. #include "output.h"
  26. #ifndef USE_MSRPC
  27. #include "network.h"
  28. #include "rpc.h"
  29. #else // USE_MSRPC
  30. #include "msrpc-client.h"
  31. #endif // USE_MSRPC
  32. #include "kms.h"
  33. #include "helpers.h"
  34. #include "dns_srv.h"
  35. #define VLMCS_OPTION_GRAB_INI 1
  36. #define VLMCS_OPTION_NO_GRAB_INI 2
  37. #define kmsVersionMinor 0 // Currently constant. May change in future KMS versions
  38. // Function Prototypes
  39. static void CreateRequestBase(REQUEST *Request);
  40. // KMS Parameters
  41. static int_fast8_t verbose = FALSE;
  42. static int_fast8_t VMInfo = FALSE;
  43. static int_fast8_t dnsnames = TRUE;
  44. static int FixedRequests = 0;
  45. static BYTE LicenseStatus = 0x02;
  46. static const char *CMID = NULL;
  47. static const char *CMID_prev = NULL;
  48. static const char *WorkstationName = NULL;
  49. static int BindingExpiration = 43200; //30 days
  50. static const char *RemoteAddr;
  51. static int_fast8_t ReconnectForEachRequest = FALSE;
  52. static int AddressFamily = AF_UNSPEC;
  53. static int_fast8_t incompatibleOptions = 0;
  54. static const char* fn_ini_client = NULL;
  55. #ifndef NO_DNS
  56. static int_fast8_t NoSrvRecordPriority = FALSE;
  57. #endif // NO_DNS
  58. // Structure for handling "License Packs" (e.g. Office2013v5 or WindowsVista)
  59. typedef struct
  60. {
  61. const char *names; //This is a list of strings. Terminate with additional Zero!!!
  62. int N_Policy;
  63. int kmsVersionMajor;
  64. const GUID *AppID;
  65. GUID ActID;
  66. GUID KMSID;
  67. } LicensePack;
  68. typedef char iniFileEpidLines[3][256];
  69. // Well known "license packs"
  70. static const LicensePack LicensePackList[] =
  71. {
  72. // List of names min lics version appID skuId KMSCountedID
  73. /* 000 */ { "Vista\000W6\000"
  74. "WindowsVista\000"
  75. "Windows\000", 25, 4, PWINGUID, { 0x4f3d1606, 0x3fea, 0x4c01, { 0xbe, 0x3c, 0x8d, 0x67, 0x1c, 0x40, 0x1e, 0x3b, } }, { 0x212a64dc, 0x43b1, 0x4d3d, { 0xa3, 0x0c, 0x2f, 0xc6, 0x9d, 0x20, 0x95, 0xc6 } } },
  76. /* 001 */ { "W7\000Windows7\000", 25, 4, PWINGUID, { 0xb92e9980, 0xb9d5, 0x4821, { 0x9c, 0x94, 0x14, 0x0f, 0x63, 0x2f, 0x63, 0x12, } }, { 0x7fde5219, 0xfbfa, 0x484a, { 0x82, 0xc9, 0x34, 0xd1, 0xad, 0x53, 0xe8, 0x56 } } },
  77. /* 002 */ { "W8\000Windows8\000", 25, 5, PWINGUID, { 0xa98bcd6d, 0x5343, 0x4603, { 0x8a, 0xfe, 0x59, 0x08, 0xe4, 0x61, 0x11, 0x12, } }, { 0x3c40b358, 0x5948, 0x45af, { 0x92, 0x3b, 0x53, 0xd2, 0x1f, 0xcc, 0x7e, 0x79 } } },
  78. /* 003 */ { "W8C\000Windows8C\000", 25, 5, PWINGUID, { 0xc04ed6bf, 0x55c8, 0x4b47, { 0x9f, 0x8e, 0x5a, 0x1f, 0x31, 0xce, 0xee, 0x60, } }, { 0xbbb97b3b, 0x8ca4, 0x4a28, { 0x97, 0x17, 0x89, 0xfa, 0xbd, 0x42, 0xc4, 0xac } } },
  79. /* 004 */ { "W81\000Windows81\000", 25, 6, PWINGUID, { 0xc06b6981, 0xd7fd, 0x4a35, { 0xb7, 0xb4, 0x05, 0x47, 0x42, 0xb7, 0xaf, 0x67, } }, { 0xcb8fc780, 0x2c05, 0x495a, { 0x97, 0x10, 0x85, 0xaf, 0xff, 0xc9, 0x04, 0xd7 } } },
  80. /* 005 */ { "W81C\000Windows81C\000", 25, 6, PWINGUID, { 0xfe1c3238, 0x432a, 0x43a1, { 0x8e, 0x25, 0x97, 0xe7, 0xd1, 0xef, 0x10, 0xf3, } }, { 0x6d646890, 0x3606, 0x461a, { 0x86, 0xab, 0x59, 0x8b, 0xb8, 0x4a, 0xce, 0x82 } } },
  81. /* 006 */ { "W10\000Windows10\000", 25, 6, PWINGUID, { 0x73111121, 0x5638, 0x40f6, { 0xbc, 0x11, 0xf1, 0xd7, 0xb0, 0xd6, 0x43, 0x00, } }, { 0x58e2134f, 0x8e11, 0x4d17, { 0x9c, 0xb2, 0x91, 0x06, 0x9c, 0x15, 0x11, 0x48 } } },
  82. /* 007 */ { "W10C\000Windows10C\000", 25, 6, PWINGUID, { 0x58e97c99, 0xf377, 0x4ef1, { 0x81, 0xd5, 0x4a, 0xd5, 0x52, 0x2b, 0x5f, 0xd8, } }, { 0xe1c51358, 0xfe3e, 0x4203, { 0xa4, 0xa2, 0x3b, 0x6b, 0x20, 0xc9, 0x73, 0x4e } } },
  83. /* 008 */ { "2008" "\0" "2008A\000", 5, 4, PWINGUID, { 0xddfa9f7c, 0xf09e, 0x40b9, { 0x8c, 0x1a, 0xbe, 0x87, 0x7a, 0x9a, 0x7f, 0x4b, } }, { 0x33e156e4, 0xb76f, 0x4a52, { 0x9f, 0x91, 0xf6, 0x41, 0xdd, 0x95, 0xac, 0x48 } } },
  84. /* 009 */ { "2008B\000", 5, 4, PWINGUID, { 0xc1af4d90, 0xd1bc, 0x44ca, { 0x85, 0xd4, 0x00, 0x3b, 0xa3, 0x3d, 0xb3, 0xb9, } }, { 0x8fe53387, 0x3087, 0x4447, { 0x89, 0x85, 0xf7, 0x51, 0x32, 0x21, 0x5a, 0xc9 } } },
  85. /* 010 */ { "2008C\000", 5, 4, PWINGUID, { 0x68b6e220, 0xcf09, 0x466b, { 0x92, 0xd3, 0x45, 0xcd, 0x96, 0x4b, 0x95, 0x09, } }, { 0x8a21fdf3, 0xcbc5, 0x44eb, { 0x83, 0xf3, 0xfe, 0x28, 0x4e, 0x66, 0x80, 0xa7 } } },
  86. /* 011 */ { "2008R2" "\0" "2008R2A\000", 5, 4, PWINGUID, { 0xa78b8bd9, 0x8017, 0x4df5, { 0xb8, 0x6a, 0x09, 0xf7, 0x56, 0xaf, 0xfa, 0x7c, } }, { 0x0fc6ccaf, 0xff0e, 0x4fae, { 0x9d, 0x08, 0x43, 0x70, 0x78, 0x5b, 0xf7, 0xed } } },
  87. /* 012 */ { "2008R2B\000", 5, 4, PWINGUID, { 0x620e2b3d, 0x09e7, 0x42fd, { 0x80, 0x2a, 0x17, 0xa1, 0x36, 0x52, 0xfe, 0x7a, } }, { 0xca87f5b6, 0xcd46, 0x40c0, { 0xb0, 0x6d, 0x8e, 0xcd, 0x57, 0xa4, 0x37, 0x3f } } },
  88. /* 013 */ { "2008R2C\000", 5, 4, PWINGUID, { 0x7482e61b, 0xc589, 0x4b7f, { 0x8e, 0xcc, 0x46, 0xd4, 0x55, 0xac, 0x3b, 0x87, } }, { 0xb2ca2689, 0xa9a8, 0x42d7, { 0x93, 0x8d, 0xcf, 0x8e, 0x9f, 0x20, 0x19, 0x58 } } },
  89. /* 014 */ { "2012\000", 5, 5, PWINGUID, { 0xf0f5ec41, 0x0d55, 0x4732, { 0xaf, 0x02, 0x44, 0x0a, 0x44, 0xa3, 0xcf, 0x0f, } }, { 0x8665cb71, 0x468c, 0x4aa3, { 0xa3, 0x37, 0xcb, 0x9b, 0xc9, 0xd5, 0xea, 0xac } } },
  90. /* 015 */ { "2012R2\000" "12R2\000", 5, 6, PWINGUID, { 0x00091344, 0x1ea4, 0x4f37, { 0xb7, 0x89, 0x01, 0x75, 0x0b, 0xa6, 0x98, 0x8c, } }, { 0x8456EFD3, 0x0C04, 0x4089, { 0x87, 0x40, 0x5b, 0x72, 0x38, 0x53, 0x5a, 0x65 } } },
  91. /* 016 */ { "Office2010\000O14\000", 5, 4, POFFICE2010GUID, { 0x6f327760, 0x8c5c, 0x417c, { 0x9b, 0x61, 0x83, 0x6a, 0x98, 0x28, 0x7e, 0x0c, } }, { 0xe85af946, 0x2e25, 0x47b7, { 0x83, 0xe1, 0xbe, 0xbc, 0xeb, 0xea, 0xc6, 0x11 } } },
  92. /* 017 */ { "Office2013\000O15\000", 5, 6, POFFICE2013GUID, { 0xb322da9c, 0xa2e2, 0x4058, { 0x9e, 0x4e, 0xf5, 0x9a, 0x69, 0x70, 0xbd, 0x69, } }, { 0xe6a6f1bf, 0x9d40, 0x40c3, { 0xaa, 0x9f, 0xc7, 0x7b, 0xa2, 0x15, 0x78, 0xc0 } } },
  93. /* 018 */ { "Office2013V5\000", 5, 5, POFFICE2013GUID, { 0xb322da9c, 0xa2e2, 0x4058, { 0x9e, 0x4e, 0xf5, 0x9a, 0x69, 0x70, 0xbd, 0x69, } }, { 0xe6a6f1bf, 0x9d40, 0x40c3, { 0xaa, 0x9f, 0xc7, 0x7b, 0xa2, 0x15, 0x78, 0xc0 } } },
  94. /* 019 */ { "Office2016\000" "O16\000", 5, 6, POFFICE2013GUID, { 0xd450596f, 0x894d, 0x49e0, { 0x96, 0x6a, 0xfd, 0x39, 0xed, 0x4c, 0x4c, 0x64, } }, { 0x85b5f61b, 0x320b, 0x4be3, { 0x81, 0x4a, 0xb7, 0x6b, 0x2b, 0xfa, 0xfc, 0x82 } } },
  95. /* 020 */ { NULL, 0, 0, NULL, { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } }, { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } } }
  96. };
  97. typedef struct
  98. {
  99. const char* first[16];
  100. const char* second[16];
  101. const char* tld[22];
  102. } DnsNames;
  103. // Some names for the DNS name random generator
  104. static DnsNames ClientDnsNames =
  105. {
  106. { "www", "ftp", "kms", "hack-me", "smtp", "ns1", "mx1", "ns1", "pop3", "imap", "mail", "dns", "headquarter", "we-love", "_vlmcs._tcp", "ceo-laptop" },
  107. { ".microsoft", ".apple", ".amazon", ".samsung", ".adobe", ".google", ".yahoo", ".facebook", ".ubuntu", ".oracle", ".borland", ".htc", ".acer", ".windows", ".linux", ".sony" },
  108. { ".com", ".net", ".org", ".cn", ".co.uk", ".de", ".com.tw", ".us", ".fr", ".it", ".me", ".info", ".biz", ".co.jp", ".ua", ".at", ".es", ".pro", ".by", ".ru", ".pl", ".kr" }
  109. };
  110. // This is the one, we are actually using. We use Vista, if user selects nothing
  111. LicensePack ActiveLicensePack;
  112. // Request Count Control Variables
  113. static int RequestsToGo = 1;
  114. static BOOL firstRequestSent = FALSE;
  115. static void string2UuidOrExit(const char *const restrict input, GUID *const restrict guid)
  116. {
  117. if (strlen(input) != GUID_STRING_LENGTH || !string2Uuid(input, guid))
  118. {
  119. errorout("Fatal: Command line contains an invalid GUID.\n");
  120. exit(!0);
  121. }
  122. }
  123. #ifndef NO_HELP
  124. __noreturn static void clientUsage(const char* const programName)
  125. {
  126. errorout(
  127. "vlmcs %s \n\n"
  128. # ifndef NO_DNS
  129. "Usage: %s [options] [ <host>[:<port>] | .<domain> | - ] [options]\n\n"
  130. # else // DNS
  131. "Usage: %s [options] [<host>[:<port>]] [options]\n\n"
  132. # endif // DNS
  133. "Options:\n\n"
  134. " -v Be verbose\n"
  135. " -l <app>\n"
  136. " -4 Force V4 protocol\n"
  137. " -5 Force V5 protocol\n"
  138. " -6 Force V6 protocol\n"
  139. # ifndef USE_MSRPC
  140. " -i <IpVersion> Use IP protocol (4 or 6)\n"
  141. # endif // USE_MSRPC
  142. " -e Show some valid examples\n"
  143. " -x Show valid Apps\n"
  144. " -d no DNS names, use Netbios names (no effect if -w is used)\n\n"
  145. "Advanced options:\n\n"
  146. " -a <AppGUID> Use custom Application GUID\n"
  147. " -s <ActGUID> Use custom Activation Configuration GUID\n"
  148. " -k <KmsGUID> Use custom KMS GUID\n"
  149. " -c <ClientGUID> Use custom Client GUID. Default: Use random\n"
  150. " -o <PreviousClientGUID> Use custom Prevoius Client GUID. Default: ZeroGUID\n"
  151. " -w <Workstation> Use custom workstation name. Default: Use random\n"
  152. " -r <RequiredClientCount> Fake required clients\n"
  153. " -n <Requests> Fixed # of requests (Default: Enough to charge)\n"
  154. " -m Pretend to be a virtual machine\n"
  155. " -G <file> Get ePID/HwId data and write to <file>. Can't be used with -l, -4, -5, -6, -a, -s, -k, -r and -n\n"
  156. # ifndef USE_MSRPC
  157. " -T Use a new TCP connection for each request.\n"
  158. " -N <0|1> disable or enable NDR64. Default: 1\n"
  159. " -B <0|1> disable or enable RPC bind time feature negotiation. Default: 1\n"
  160. # endif // USE_MSRPC
  161. " -t <LicenseStatus> Use specfic license status (0 <= T <= 6)\n"
  162. " -g <BindingExpiration> Use a specfic binding expiration time in minutes. Default 43200\n"
  163. # ifndef NO_DNS
  164. " -P Ignore priority and weight in DNS SRV records\n"
  165. # endif // NO_DNS
  166. # ifndef USE_MSRPC
  167. " -p Don't use multiplexed RPC bind\n"
  168. # endif // USE_MSRPC
  169. "\n"
  170. "<port>:\t\tTCP port name of the KMS to use. Default 1688.\n"
  171. "<host>:\t\thost name of the KMS to use. Default 127.0.0.1\n"
  172. # ifndef NO_DNS
  173. ".<domain>:\tfind KMS server in <domain> via DNS\n"
  174. # endif // NO_DNS
  175. "<app>:\t\t(Type %s -x to see a list of valid apps)\n\n",
  176. Version, programName, programName
  177. );
  178. exit(!0);
  179. }
  180. __pure static int getLineWidth(void)
  181. {
  182. #ifdef TERMINAL_FIXED_WIDTH // For Toolchains that to not have winsize
  183. return TERMINAL_FIXED_WIDTH;
  184. #else // Can determine width of terminal
  185. #ifndef _WIN32
  186. struct winsize w;
  187. if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &w))
  188. {
  189. return 80; // Return this if stdout is not a tty
  190. }
  191. return w.ws_col;
  192. #else // _WIN32
  193. CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
  194. HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  195. if (!GetConsoleScreenBufferInfo(hStdout, &csbiInfo))
  196. {
  197. return 80; // Return this if stdout is not a Console
  198. }
  199. return csbiInfo.srWindow.Right - csbiInfo.srWindow.Left;
  200. #endif // WIN32
  201. #endif // Can determine width of terminal
  202. }
  203. __noreturn static void showProducts(PRINTFUNC p)
  204. {
  205. int cols = getLineWidth();
  206. int itemsPerLine;
  207. uint8_t i;
  208. p(
  209. "The following "
  210. #if !defined(NO_EXTENDED_PRODUCT_LIST) && !defined(NO_BASIC_PRODUCT_LIST)
  211. "aliases "
  212. #else
  213. "names "
  214. #endif
  215. "can be used with -l:\n\n"
  216. );
  217. const LicensePack* lp;
  218. itemsPerLine = cols / 20;
  219. if (!itemsPerLine) itemsPerLine = 1;
  220. for (i = 1, lp = LicensePackList; lp->names; lp++)
  221. {
  222. const char* name;
  223. for (name = lp->names; *name; name += strlen(name) + 1, i++)
  224. {
  225. p("%-20s", name);
  226. if (!(i % itemsPerLine)) p("\n");
  227. }
  228. }
  229. p("\n\n");
  230. #if !defined(NO_EXTENDED_PRODUCT_LIST) && !defined(NO_BASIC_PRODUCT_LIST)
  231. const KmsIdList* currentProduct;
  232. uint_fast8_t longestString = 0;
  233. uint8_t k, items = getExtendedProductListSize();
  234. p("You may also use these product names or numbers:\n\n");
  235. for (currentProduct = ExtendedProductList; currentProduct->name; currentProduct++)
  236. {
  237. uint_fast8_t len = strlen(currentProduct->name);
  238. if (len > longestString)
  239. longestString = len;
  240. }
  241. itemsPerLine = cols / (longestString + 10);
  242. if (!itemsPerLine) itemsPerLine = 1;
  243. uint8_t lines = items / itemsPerLine;
  244. if (items % itemsPerLine) lines++;
  245. for (i = 0; i < lines; i++)
  246. {
  247. for (k = 0; k < itemsPerLine; k++)
  248. {
  249. uint8_t j;
  250. uint8_t index = k * lines + i;
  251. if (index >= items) break;
  252. p("%3u = %s", index + 1, ExtendedProductList[index].name);
  253. for (j = 0; j < longestString + 4 - strlen(ExtendedProductList[index].name); j++)
  254. {
  255. p(" ");
  256. }
  257. }
  258. p("\n");
  259. }
  260. p("\n");
  261. #endif // !defined(NO_EXTENDED_PRODUCT_LIST) && !defined(NO_BASIC_PRODUCT_LIST)
  262. exit(0);
  263. }
  264. __noreturn static void examples(const char* const programName)
  265. {
  266. printf(
  267. "\nRequest activation for Office2013 using V4 protocol from 192.168.1.5:1688\n"
  268. "\t%s -l O15 -4 192.168.1.5\n"
  269. "\t%s -l O15 -4 192.168.1.5:1688\n\n"
  270. "Request activation for Windows Server 2012 using V4 protocol from localhost:1688\n"
  271. "\t%s -4 -l Windows -k 8665cb71-468c-4aa3-a337-cb9bc9d5eaac\n"
  272. "\t%s -4 -l 2012\n"
  273. "\t%s -4 -l 2012 [::1]:1688\n"
  274. "\t%s -4 -l 12 127.0.0.2:1688\n\n"
  275. "Send 100,000 requests to localhost:1688\n"
  276. "\t%s -n 100000 -l Office2010\n\n"
  277. "Request Activation for Windows 8 from 10.0.0.1:4711 and pretend to be Steve Ballmer\n"
  278. "\t%s -l Windows8 -w steveb1.redmond.microsoft.com 10.0.0.1:4711\n\n",
  279. programName, programName, programName, programName, programName, programName, programName, programName
  280. );
  281. exit(0);
  282. }
  283. #else // NO_HELP
  284. __noreturn static void clientUsage(const char* const programName)
  285. {
  286. errorout("Incorrect parameter specified.\n");
  287. exit(!0);
  288. }
  289. #endif // NO_HELP
  290. static BOOL findLicensePackByName(const char* const name, LicensePack* const lp)
  291. {
  292. // Try to find a package in the short list first
  293. LicensePack *licensePack;
  294. for (licensePack = (LicensePack*)&LicensePackList; licensePack->names; licensePack ++)
  295. {
  296. const char *currentName;
  297. for (currentName = licensePack->names; *currentName; currentName += strlen(currentName) + 1)
  298. {
  299. if (!strcasecmp(name, currentName))
  300. {
  301. *lp = *licensePack;
  302. return TRUE;
  303. }
  304. }
  305. }
  306. #if defined(NO_BASIC_PRODUCT_LIST) || defined(NO_EXTENDED_PRODUCT_LIST)
  307. return FALSE;
  308. #else // Both Lists are available
  309. // search extended product list
  310. uint8_t items = getExtendedProductListSize();
  311. unsigned int index;
  312. if (stringToInt(name, 1, items, &index))
  313. {
  314. index--;
  315. }
  316. else
  317. {
  318. for (index = 0; index < items; index++)
  319. {
  320. if (!strcasecmp(ExtendedProductList[index].name, name)) break;
  321. }
  322. if (index >= items) return FALSE;
  323. }
  324. lp->AppID = &AppList[ExtendedProductList[index].AppIndex].guid;
  325. lp->KMSID = ProductList[ExtendedProductList[index].KmsIndex].guid;
  326. lp->ActID = ExtendedProductList[index].guid;
  327. lp->N_Policy = ProductList[ExtendedProductList[index].KmsIndex].KMS_PARAM_REQUIREDCOUNT;
  328. lp->kmsVersionMajor = ProductList[ExtendedProductList[index].KmsIndex].KMS_PARAM_MAJOR;
  329. return TRUE;
  330. #endif // Both Lists are available
  331. }
  332. static const char* const client_optstring = "+N:B:i:l:a:s:k:c:w:r:n:t:g:G:o:pPTv456mexd";
  333. //First pass. We handle only "-l". Since -a -k -s -4 -5 and -6 are exceptions to -l, we process -l first
  334. static void parseCommandLinePass1(const int argc, CARGV argv)
  335. {
  336. int o;
  337. optReset();
  338. for (opterr = 0; ( o = getopt(argc, (char* const*)argv, client_optstring) ) > 0; ) switch (o)
  339. {
  340. case 'l': // Set "License Pack" and protocol version (e.g. Windows8, Office2013v5, ...)
  341. if (!findLicensePackByName(optarg, &ActiveLicensePack))
  342. {
  343. errorout("Invalid client application. \"%s\" is not valid for -l.\n\n", optarg);
  344. #ifndef NO_HELP
  345. showProducts(&errorout);
  346. #endif // !NO_HELP
  347. }
  348. break;
  349. default:
  350. break;
  351. }
  352. }
  353. // Second Pass. Handle all options except "-l"
  354. static void parseCommandLinePass2(const char *const programName, const int argc, CARGV argv)
  355. {
  356. int o;
  357. optReset();
  358. for (opterr = 0; ( o = getopt(argc, (char* const*)argv, client_optstring) ) > 0; ) switch (o)
  359. {
  360. #ifndef NO_HELP
  361. case 'e': // Show examples
  362. examples(programName);
  363. break;
  364. case 'x': // Show Apps
  365. showProducts(&printf);
  366. break;
  367. #endif // NO_HELP
  368. # ifndef NO_DNS
  369. case 'P':
  370. NoSrvRecordPriority = TRUE;
  371. break;
  372. # endif // NO_DNS
  373. case 'G':
  374. incompatibleOptions |= VLMCS_OPTION_GRAB_INI;
  375. fn_ini_client = optarg;
  376. break;
  377. # ifndef USE_MSRPC
  378. case 'N':
  379. if (!getArgumentBool(&UseRpcNDR64, optarg)) clientUsage(programName);
  380. break;
  381. case 'B':
  382. if (!getArgumentBool(&UseRpcBTFN, optarg)) clientUsage(programName);
  383. break;
  384. case 'i':
  385. switch(getOptionArgumentInt(o, 4, 6))
  386. {
  387. case 4:
  388. AddressFamily = AF_INET;
  389. break;
  390. case 6:
  391. AddressFamily = AF_INET6;
  392. break;
  393. default:
  394. errorout("IPv5 does not exist.\n");
  395. exit(!0);
  396. break;
  397. }
  398. break;
  399. case 'p': // Multiplexed RPC
  400. UseMultiplexedRpc = FALSE;
  401. break;
  402. # endif // USE_MSRPC
  403. case 'n': // Fixed number of Requests (regardless, whether they are required)
  404. incompatibleOptions |= VLMCS_OPTION_NO_GRAB_INI;
  405. FixedRequests = getOptionArgumentInt(o, 1, INT_MAX);
  406. break;
  407. case 'r': // Fake minimum required client count
  408. incompatibleOptions |= VLMCS_OPTION_NO_GRAB_INI;
  409. ActiveLicensePack.N_Policy = getOptionArgumentInt(o, 1, INT_MAX);
  410. break;
  411. case 'c': // use a specific client GUID
  412. // If using a constant Client ID, send only one request unless /N= explicitly specified
  413. if (!FixedRequests) FixedRequests = 1;
  414. CMID = optarg;
  415. break;
  416. case 'o': // use a specific previous client GUID
  417. CMID_prev = optarg;
  418. break;
  419. case 'a': // Set specific App Id
  420. incompatibleOptions |= VLMCS_OPTION_NO_GRAB_INI;
  421. ActiveLicensePack.AppID = (GUID*)vlmcsd_malloc(sizeof(GUID));
  422. string2UuidOrExit(optarg, (GUID*)ActiveLicensePack.AppID);
  423. break;
  424. case 'g': // Set custom "grace" time in minutes (default 30 days)
  425. BindingExpiration = getOptionArgumentInt(o, 0, INT_MAX);
  426. break;
  427. case 's': // Set specfic SKU ID
  428. incompatibleOptions |= VLMCS_OPTION_NO_GRAB_INI;
  429. string2UuidOrExit(optarg, &ActiveLicensePack.ActID);
  430. break;
  431. case 'k': // Set specific KMS ID
  432. incompatibleOptions |= VLMCS_OPTION_NO_GRAB_INI;
  433. string2UuidOrExit(optarg, &ActiveLicensePack.KMSID);
  434. break;
  435. case '4': // Force V4 protocol
  436. case '5': // Force V5 protocol
  437. case '6': // Force V5 protocol
  438. incompatibleOptions |= VLMCS_OPTION_NO_GRAB_INI;
  439. ActiveLicensePack.kmsVersionMajor = o - 0x30;
  440. break;
  441. case 'd': // Don't use DNS names
  442. dnsnames = FALSE;
  443. break;
  444. case 'v': // Be verbose
  445. verbose = TRUE;
  446. break;
  447. case 'm': // Pretend to be a virtual machine
  448. VMInfo = TRUE;
  449. break;
  450. case 'w': // WorkstationName (max. 63 chars)
  451. WorkstationName = optarg;
  452. if (strlen(WorkstationName) > 63)
  453. {
  454. errorout("\007WARNING! Truncating Workstation name to 63 characters (%s).\n", WorkstationName);
  455. }
  456. break;
  457. case 't':
  458. LicenseStatus = getOptionArgumentInt(o, 0, 6) & 0xff;
  459. break;
  460. # ifndef USE_MSRPC
  461. case 'T':
  462. ReconnectForEachRequest = TRUE;
  463. break;
  464. # endif // USE_MSRPC
  465. case 'l':
  466. incompatibleOptions |= VLMCS_OPTION_NO_GRAB_INI;
  467. break;
  468. default:
  469. clientUsage(programName);
  470. }
  471. if ((incompatibleOptions & (VLMCS_OPTION_NO_GRAB_INI | VLMCS_OPTION_GRAB_INI)) == (VLMCS_OPTION_NO_GRAB_INI | VLMCS_OPTION_GRAB_INI))
  472. clientUsage(programName);
  473. }
  474. /*
  475. * Compares 2 GUIDs where one is host-endian and the other is little-endian (network byte order)
  476. */
  477. int_fast8_t IsEqualGuidLEHE(const GUID* const guid1, const GUID* const guid2)
  478. {
  479. GUID tempGuid;
  480. LEGUID(&tempGuid, guid2);
  481. return IsEqualGUID(guid1, &tempGuid);
  482. }
  483. #ifndef USE_MSRPC
  484. static void checkRpcLevel(const REQUEST* request, RESPONSE* response)
  485. {
  486. if (!RpcFlags.HasNDR32)
  487. errorout("\nWARNING: Server's RPC protocol does not support NDR32.\n");
  488. if (UseRpcBTFN && UseRpcNDR64 && RpcFlags.HasNDR64 && !RpcFlags.HasBTFN)
  489. errorout("\nWARNING: Server's RPC protocol has NDR64 but no BTFN.\n");
  490. if (!IsEqualGuidLEHE(&request->KMSID, &ProductList[15].guid) && UseRpcBTFN && !RpcFlags.HasBTFN)
  491. errorout("\nWARNING: A server with pre-Vista RPC activated a product other than Office 2010.\n");
  492. }
  493. #endif // USE_MSRPC
  494. static void displayResponse(const RESPONSE_RESULT result, const REQUEST* request, RESPONSE* response, BYTE *hwid)
  495. {
  496. fflush(stdout);
  497. if (!result.RpcOK) errorout("\n\007ERROR: Non-Zero RPC result code.\n");
  498. if (!result.DecryptSuccess) errorout("\n\007ERROR: Decryption of V5/V6 response failed.\n");
  499. if (!result.IVsOK) errorout("\n\007ERROR: AES CBC initialization vectors (IVs) of request and response do not match.\n");
  500. if (!result.PidLengthOK) errorout("\n\007ERROR: The length of the PID is not valid.\n");
  501. if (!result.HashOK) errorout("\n\007ERROR: Computed hash does not match hash in response.\n");
  502. if (!result.ClientMachineIDOK) errorout("\n\007ERROR: Client machine GUIDs of request and response do not match.\n");
  503. if (!result.TimeStampOK) errorout("\n\007ERROR: Time stamps of request and response do not match.\n");
  504. if (!result.VersionOK) errorout("\n\007ERROR: Protocol versions of request and response do not match.\n");
  505. if (!result.HmacSha256OK) errorout("\n\007ERROR: Keyed-Hash Message Authentication Code (HMAC) is incorrect.\n");
  506. if (!result.IVnotSuspicious) errorout("\nWARNING: Response uses an IV following KMSv5 rules in KMSv6 protocol.\n");
  507. if (result.effectiveResponseSize != result.correctResponseSize)
  508. {
  509. errorout("\n\007WARNING: Size of RPC payload (KMS Message) should be %u but is %u.", result.correctResponseSize, result.effectiveResponseSize);
  510. }
  511. # ifndef USE_MSRPC
  512. checkRpcLevel(request, response);
  513. # endif // USE_MSRPC
  514. if (!result.DecryptSuccess) return; // Makes no sense to display anything
  515. char ePID[3 * PID_BUFFER_SIZE];
  516. if (!ucs2_to_utf8(response->KmsPID, ePID, PID_BUFFER_SIZE, 3 * PID_BUFFER_SIZE))
  517. {
  518. memset(ePID + 3 * PID_BUFFER_SIZE - 3, 0, 3);
  519. }
  520. // Read KMSPID from Response
  521. if (!verbose)
  522. {
  523. printf(" -> %s", ePID);
  524. if (LE16(response->MajorVer) > 5)
  525. {
  526. # ifndef _WIN32
  527. printf(" (%016llX)", (unsigned long long)BE64(*(uint64_t*)hwid));
  528. # else // _WIN32
  529. printf(" (%016I64X)", (unsigned long long)BE64(*(uint64_t*)hwid));
  530. # endif // _WIN32
  531. }
  532. printf("\n");
  533. }
  534. else
  535. {
  536. printf(
  537. "\n\nResponse from KMS server\n========================\n\n"
  538. "Size of KMS Response : %u (0x%x)\n", result.effectiveResponseSize, result.effectiveResponseSize
  539. );
  540. logResponseVerbose(ePID, hwid, response, &printf);
  541. printf("\n");
  542. }
  543. }
  544. static void connectRpc(RpcCtx *s)
  545. {
  546. # ifdef NO_DNS
  547. *s = connectToAddress(RemoteAddr, AddressFamily, FALSE);
  548. if (*s == INVALID_RPCCTX)
  549. {
  550. errorout("Fatal: Could not connect to %s\n", RemoteAddr);
  551. exit(!0);
  552. }
  553. if (verbose)
  554. printf("\nPerforming RPC bind ...\n");
  555. if (rpcBindClient(*s, verbose))
  556. {
  557. errorout("Fatal: Could not bind RPC\n");
  558. exit(!0);
  559. }
  560. if (verbose) printf("... successful\n");
  561. # else // DNS
  562. static kms_server_dns_ptr* serverlist = NULL;
  563. static int numServers = 0;
  564. //static int_fast8_t ServerListAlreadyPrinted = FALSE;
  565. int i;
  566. if (!strcmp(RemoteAddr, "-") || *RemoteAddr == '.') // Get KMS server via DNS SRV record
  567. {
  568. if (!serverlist)
  569. numServers = getKmsServerList(&serverlist, RemoteAddr);
  570. if (numServers < 1)
  571. {
  572. errorout("Fatal: No KMS servers found\n");
  573. exit(!0);
  574. }
  575. if (!NoSrvRecordPriority) sortSrvRecords(serverlist, numServers);
  576. if (verbose /*&& !ServerListAlreadyPrinted*/)
  577. {
  578. for (i = 0; i < numServers; i++)
  579. {
  580. printf(
  581. "Found %-40s (priority: %hu, weight: %hu, randomized weight: %i)\n",
  582. serverlist[i]->serverName,
  583. serverlist[i]->priority, serverlist[i]->weight,
  584. NoSrvRecordPriority ? 0 : serverlist[i]->random_weight
  585. );
  586. }
  587. printf("\n");
  588. //ServerListAlreadyPrinted = TRUE;
  589. }
  590. }
  591. else // Just use the server supplied on the command line
  592. {
  593. if (!serverlist)
  594. {
  595. serverlist = (kms_server_dns_ptr*)vlmcsd_malloc(sizeof(kms_server_dns_ptr));
  596. *serverlist = (kms_server_dns_ptr)vlmcsd_malloc(sizeof(kms_server_dns_t));
  597. numServers = 1;
  598. strncpy((*serverlist)->serverName, RemoteAddr, sizeof((*serverlist)->serverName));
  599. }
  600. }
  601. for (i = 0; i < numServers; i++)
  602. {
  603. *s = connectToAddress(serverlist[i]->serverName, AddressFamily, (*RemoteAddr == '.' || *RemoteAddr == '-'));
  604. if (*s == INVALID_RPCCTX) continue;
  605. if (verbose)
  606. printf("\nPerforming RPC bind ...\n");
  607. if (rpcBindClient(*s, verbose))
  608. {
  609. errorout("Warning: Could not bind RPC\n");
  610. continue;
  611. }
  612. if (verbose) printf("... successful\n");
  613. return;
  614. }
  615. errorout("Fatal: Could not connect to any KMS server\n");
  616. exit(!0);
  617. # endif // DNS
  618. }
  619. static int SendActivationRequest(const RpcCtx sock, RESPONSE *baseResponse, REQUEST *baseRequest, RESPONSE_RESULT *result, BYTE *const hwid)
  620. {
  621. size_t requestSize, responseSize;
  622. BYTE *request, *response;
  623. int status;
  624. result->mask = 0;
  625. if (LE16(baseRequest->MajorVer) == 4)
  626. request = CreateRequestV4(&requestSize, baseRequest);
  627. else
  628. request = CreateRequestV6(&requestSize, baseRequest);
  629. if (!(status = rpcSendRequest(sock, request, requestSize, &response, &responseSize)))
  630. {
  631. if (LE16(((RESPONSE*)(response))->MajorVer) == 4)
  632. {
  633. RESPONSE_V4 response_v4;
  634. *result = DecryptResponseV4(&response_v4, responseSize, response, request);
  635. memcpy(baseResponse, &response_v4.ResponseBase, sizeof(RESPONSE));
  636. }
  637. else
  638. {
  639. RESPONSE_V6 response_v6;
  640. *result = DecryptResponseV6(&response_v6, responseSize, response, request, hwid);
  641. memcpy(baseResponse, &response_v6.ResponseBase, sizeof(RESPONSE));
  642. }
  643. result->RpcOK = TRUE;
  644. }
  645. if (response) free(response);
  646. free(request);
  647. return status;
  648. }
  649. static int sendRequest(RpcCtx *const s, REQUEST *const request, RESPONSE *const response, hwid_t hwid, RESPONSE_RESULT *const result)
  650. {
  651. CreateRequestBase(request);
  652. if (*s == INVALID_RPCCTX )
  653. connectRpc(s);
  654. else
  655. {
  656. // Check for lame KMS emulators that close the socket after each request
  657. int_fast8_t disconnected = isDisconnected(*s);
  658. if (disconnected)
  659. errorout("\nWarning: Server closed RPC connection (probably non-multitasked KMS emulator)\n");
  660. if (ReconnectForEachRequest || disconnected)
  661. {
  662. closeRpc(*s);
  663. connectRpc(s);
  664. }
  665. }
  666. printf("Sending activation request (KMS V%u) ", ActiveLicensePack.kmsVersionMajor);
  667. fflush(stdout);
  668. return SendActivationRequest(*s, response, request, result, hwid);
  669. }
  670. static void displayRequestError(RpcCtx *const s, const int status, const int currentRequest, const int totalRequests)
  671. {
  672. errorout("\nError 0x%08X while sending request %u of %u\n", status, currentRequest, RequestsToGo + totalRequests);
  673. switch(status)
  674. {
  675. case 0xC004F042: // not licensed
  676. errorout("The server refused to activate the requested product\n");
  677. break;
  678. case 0x8007000D: // e.g. v6 protocol on a v5 server
  679. errorout("The server didn't understand the request\n");
  680. break;
  681. case 1:
  682. errorout("An RPC protocol error has occured\n");
  683. closeRpc(*s);
  684. connectRpc(s);
  685. break;
  686. default:
  687. break;
  688. }
  689. }
  690. static void newIniBackupFile(const char* const restrict fname)
  691. {
  692. FILE *restrict f = fopen(fname, "wb");
  693. if (!f)
  694. {
  695. errorout("Fatal: Cannot create %s: %s\n", fname, strerror(errno));
  696. exit(!0);
  697. }
  698. if (fclose(f))
  699. {
  700. errorout("Fatal: Cannot write to %s: %s\n", fname, strerror(errno));
  701. unlink(fname);
  702. exit(!0);
  703. }
  704. }
  705. static void updateIniFile(iniFileEpidLines* const restrict lines)
  706. {
  707. int_fast8_t lineWritten[_countof(*lines)];
  708. struct stat statbuf;
  709. uint_fast8_t i;
  710. int_fast8_t iniFileExistedBefore = TRUE;
  711. unsigned int lineNumber;
  712. memset(lineWritten, FALSE, sizeof(lineWritten));
  713. char* restrict fn_bak = (char*)vlmcsd_malloc(strlen(fn_ini_client) + 2);
  714. strcpy(fn_bak, fn_ini_client);
  715. strcat(fn_bak, "~");
  716. if (stat(fn_ini_client, &statbuf))
  717. {
  718. if (errno != ENOENT)
  719. {
  720. errorout("Fatal: %s: %s\n", fn_ini_client, strerror(errno));
  721. exit(!0);
  722. }
  723. else
  724. {
  725. iniFileExistedBefore = FALSE;
  726. newIniBackupFile(fn_bak);
  727. }
  728. }
  729. else
  730. {
  731. unlink(fn_bak); // Required for Windows. Most Unix systems don't need it.
  732. if (rename(fn_ini_client, fn_bak))
  733. {
  734. errorout("Fatal: Cannot create %s: %s\n", fn_bak, strerror(errno));
  735. exit(!0);
  736. }
  737. }
  738. printf("\n%s file %s\n", iniFileExistedBefore ? "Updating" : "Creating", fn_ini_client);
  739. FILE *restrict in, *restrict out;
  740. in = fopen(fn_bak, "rb");
  741. if (!in)
  742. {
  743. errorout("Fatal: Cannot open %s: %s\n", fn_bak, strerror(errno));
  744. exit(!0);
  745. }
  746. out = fopen(fn_ini_client, "wb");
  747. if (!out)
  748. {
  749. errorout("Fatal: Cannot create %s: %s\n", fn_ini_client, strerror(errno));
  750. exit(!0);
  751. }
  752. char sourceLine[256];
  753. for (lineNumber = 1; fgets(sourceLine, sizeof(sourceLine), in); lineNumber++)
  754. {
  755. for (i = 0; i < _countof(*lines); i++)
  756. {
  757. if (*(*lines)[i] && !strncasecmp(sourceLine, (*lines)[i], GUID_STRING_LENGTH))
  758. {
  759. if (lineWritten[i]) break;
  760. fprintf(out, "%s", (*lines)[i]);
  761. printf("line %2i: %s", lineNumber, (*lines)[i]);
  762. lineWritten[i] = TRUE;
  763. break;
  764. }
  765. }
  766. if (i >= _countof(*lines))
  767. {
  768. fprintf(out, "%s", sourceLine);
  769. }
  770. }
  771. if (ferror(in))
  772. {
  773. errorout("Fatal: Cannot read from %s: %s\n", fn_bak, strerror(errno));
  774. exit(!0);
  775. }
  776. fclose(in);
  777. for (i = 0; i < _countof(*lines); i++)
  778. {
  779. if (!lineWritten[i] && *(*lines)[i])
  780. {
  781. fprintf(out, "%s", (*lines)[i]);
  782. printf("line %2i: %s", lineNumber + i, (*lines)[i]);
  783. }
  784. }
  785. if (fclose(out))
  786. {
  787. errorout("Fatal: Cannot write to %s: %s\n", fn_ini_client, strerror(errno));
  788. exit(!0);
  789. }
  790. if (!iniFileExistedBefore) unlink(fn_bak);
  791. free(fn_bak);
  792. }
  793. static void grabServerData()
  794. {
  795. RpcCtx s = INVALID_RPCCTX;
  796. WORD MajorVer = 6;
  797. iniFileEpidLines lines;
  798. int_fast8_t Licenses[_countof(lines)] = { 0, 15, 14 };
  799. uint_fast8_t i;
  800. RESPONSE response;
  801. RESPONSE_RESULT result;
  802. REQUEST request;
  803. hwid_t hwid;
  804. int status;
  805. size_t len;
  806. for (i = 0; i < _countof(lines); i++) *lines[i] = 0;
  807. for (i = 0; i < _countof(Licenses) && MajorVer > 3; i++)
  808. {
  809. ActiveLicensePack = LicensePackList[Licenses[i]];
  810. ActiveLicensePack.kmsVersionMajor = MajorVer;
  811. status = sendRequest(&s, &request, &response, hwid, &result);
  812. printf("%-11s", ActiveLicensePack.names);
  813. if (status)
  814. {
  815. displayRequestError(&s, status, i + 7 - MajorVer, 9 - MajorVer);
  816. if (status == 1) break;
  817. if ((status & 0xF0000000) == 0x80000000)
  818. {
  819. MajorVer--;
  820. i--;
  821. }
  822. continue;
  823. }
  824. printf("%i of %i", (int)(i + 7 - MajorVer), (int)(9 - MajorVer));
  825. displayResponse(result, &request, &response, hwid);
  826. char guidBuffer[GUID_STRING_LENGTH + 1];
  827. char ePID[3 * PID_BUFFER_SIZE];
  828. uuid2StringLE(&request.AppID, guidBuffer);
  829. if (!ucs2_to_utf8(response.KmsPID, ePID, PID_BUFFER_SIZE, 3 * PID_BUFFER_SIZE))
  830. {
  831. memset(ePID + 3 * PID_BUFFER_SIZE - 3, 0, 3);
  832. }
  833. snprintf(lines[i], sizeof(lines[0]), "%s = %s", guidBuffer, ePID);
  834. if (response.MajorVer > 5)
  835. {
  836. len = strlen(lines[i]);
  837. snprintf (lines[i] + len, sizeof(lines[0]) - len, "/ %02X %02X %02X %02X %02X %02X %02X %02X", hwid[0], hwid[1], hwid[2], hwid[3], hwid[4], hwid[5], hwid[6], hwid[7]);
  838. }
  839. len = strlen(lines[i]);
  840. snprintf(lines[i] + len, sizeof(lines[0]) - len, "\n");
  841. }
  842. if (strcmp(fn_ini_client, "-"))
  843. {
  844. updateIniFile(&lines);
  845. }
  846. else
  847. {
  848. printf("\n");
  849. for (i = 0; i < _countof(lines); i++) printf("%s", lines[i]);
  850. }
  851. }
  852. int client_main(const int argc, CARGV argv)
  853. {
  854. #if defined(_WIN32) && !defined(USE_MSRPC)
  855. // Windows Sockets must be initialized
  856. WSADATA wsadata;
  857. int error;
  858. if ((error = WSAStartup(0x0202, &wsadata)))
  859. {
  860. printerrorf("Fatal: Could not initialize Windows sockets (Error: %d).\n", error);
  861. return error;
  862. }
  863. #endif // _WIN32
  864. #ifdef _NTSERVICE
  865. // We are not a service
  866. IsNTService = FALSE;
  867. // Set console output page to UTF-8
  868. // SetConsoleOutputCP(65001);
  869. #endif // _NTSERVICE
  870. randomNumberInit();
  871. ActiveLicensePack = *LicensePackList; //first license is Windows Vista
  872. parseCommandLinePass1(argc, argv);
  873. int_fast8_t useDefaultHost = FALSE;
  874. if (optind < argc)
  875. RemoteAddr = argv[optind];
  876. else
  877. useDefaultHost = TRUE;
  878. int hostportarg = optind;
  879. if (optind < argc - 1)
  880. {
  881. parseCommandLinePass1(argc - hostportarg, argv + hostportarg);
  882. if (optind < argc - hostportarg)
  883. clientUsage(argv[0]);
  884. }
  885. parseCommandLinePass2(argv[0], argc, argv);
  886. if (optind < argc - 1)
  887. parseCommandLinePass2(argv[0], argc - hostportarg, argv + hostportarg);
  888. if (useDefaultHost)
  889. RemoteAddr = AddressFamily == AF_INET6 ? "::1" : "127.0.0.1";
  890. if (fn_ini_client != NULL)
  891. grabServerData();
  892. else
  893. {
  894. int requests;
  895. RpcCtx s = INVALID_RPCCTX;
  896. for (requests = 0, RequestsToGo = ActiveLicensePack.N_Policy - 1; RequestsToGo; requests++)
  897. {
  898. RESPONSE response;
  899. REQUEST request;
  900. RESPONSE_RESULT result;
  901. hwid_t hwid;
  902. int status = sendRequest(&s, &request, &response, hwid, &result);
  903. if (FixedRequests) RequestsToGo = FixedRequests - requests - 1;
  904. if (status)
  905. {
  906. displayRequestError(&s, status, requests + 1, RequestsToGo + requests + 1);
  907. if (!FixedRequests) RequestsToGo = 0;
  908. }
  909. else
  910. {
  911. if (!FixedRequests)
  912. {
  913. if (firstRequestSent && ActiveLicensePack.N_Policy - (int)response.Count >= RequestsToGo)
  914. {
  915. errorout("\nThe KMS server does not increment it's active clients. Aborting...\n");
  916. RequestsToGo = 0;
  917. }
  918. else
  919. {
  920. RequestsToGo = ActiveLicensePack.N_Policy - response.Count;
  921. if (RequestsToGo < 0) RequestsToGo = 0;
  922. }
  923. }
  924. fflush(stderr);
  925. printf("%i of %i ", requests + 1, RequestsToGo + requests + 1);
  926. displayResponse(result, &request, &response, hwid);
  927. firstRequestSent = TRUE;
  928. }
  929. }
  930. }
  931. return 0;
  932. }
  933. // Create Base KMS Client Request
  934. static void CreateRequestBase(REQUEST *Request)
  935. {
  936. Request->MinorVer = LE16((WORD)kmsVersionMinor);
  937. Request->MajorVer = LE16((WORD)ActiveLicensePack.kmsVersionMajor);
  938. Request->VMInfo = LE32(VMInfo);
  939. Request->LicenseStatus = LE32(LicenseStatus);
  940. Request->BindingExpiration = LE32(BindingExpiration);
  941. LEGUID(&Request->AppID, ActiveLicensePack.AppID);
  942. LEGUID(&Request->ActID, &ActiveLicensePack.ActID);
  943. LEGUID(&Request->KMSID, &ActiveLicensePack.KMSID);
  944. getUnixTimeAsFileTime(&Request->ClientTime);
  945. Request->N_Policy = LE32(ActiveLicensePack.N_Policy);
  946. {
  947. GUID tempGUID;
  948. if (CMID)
  949. {
  950. string2UuidOrExit(CMID, &tempGUID);
  951. LEGUID(&Request->CMID, &tempGUID);
  952. }
  953. else
  954. {
  955. get16RandomBytes(&Request->CMID);
  956. // Set reserved UUID bits
  957. Request->CMID.Data4[0] &= 0x3F;
  958. Request->CMID.Data4[0] |= 0x80;
  959. // Set UUID type 4 (random UUID)
  960. Request->CMID.Data3 &= LE16(0xfff);
  961. Request->CMID.Data3 |= LE16(0x4000);
  962. }
  963. if (CMID_prev)
  964. {
  965. string2UuidOrExit(CMID_prev, &tempGUID);
  966. LEGUID(&Request->CMID_prev, &tempGUID);
  967. }
  968. else
  969. {
  970. memset(&Request->CMID_prev, 0, sizeof(Request->CMID_prev));
  971. }
  972. }
  973. static const char alphanum[] = "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" /*"abcdefghijklmnopqrstuvwxyz" */;
  974. if (WorkstationName)
  975. {
  976. utf8_to_ucs2(Request->WorkstationName, WorkstationName, WORKSTATION_NAME_BUFFER, WORKSTATION_NAME_BUFFER * 3);
  977. }
  978. else if (dnsnames)
  979. {
  980. int len, len2;
  981. unsigned int index = rand() % _countof(ClientDnsNames.first);
  982. len = utf8_to_ucs2(Request->WorkstationName, ClientDnsNames.first[index], WORKSTATION_NAME_BUFFER, WORKSTATION_NAME_BUFFER * 3);
  983. index = rand() % _countof(ClientDnsNames.second);
  984. len2 = utf8_to_ucs2(Request->WorkstationName + len, ClientDnsNames.second[index], WORKSTATION_NAME_BUFFER, WORKSTATION_NAME_BUFFER * 3);
  985. index = rand() % _countof(ClientDnsNames.tld);
  986. utf8_to_ucs2(Request->WorkstationName + len + len2, ClientDnsNames.tld[index], WORKSTATION_NAME_BUFFER, WORKSTATION_NAME_BUFFER * 3);
  987. }
  988. else
  989. {
  990. unsigned int size = (rand() % 14) + 1;
  991. const unsigned char *dummy;
  992. unsigned int i;
  993. for (i = 0; i < size; i++)
  994. {
  995. Request->WorkstationName[i] = utf8_to_ucs2_char((unsigned char*)alphanum + (rand() % (sizeof(alphanum) - 1)), &dummy);
  996. }
  997. Request->WorkstationName[size] = 0;
  998. }
  999. //Show Details
  1000. if (verbose)
  1001. {
  1002. printf("\nRequest Parameters\n==================\n\n");
  1003. logRequestVerbose(Request, &printf);
  1004. printf("\n");
  1005. }
  1006. }
  1007. #ifndef CONFIG
  1008. #define CONFIG "config.h"
  1009. #endif // CONFIG
  1010. #include CONFIG
  1011. #include "crypto.h"
  1012. #include "endian.h"
  1013. #include <stdint.h>
  1014. const BYTE AesKeyV4[] = {
  1015. 0x05, 0x3D, 0x83, 0x07, 0xF9, 0xE5, 0xF0, 0x88, 0xEB, 0x5E, 0xA6, 0x68, 0x6C, 0xF0, 0x37, 0xC7, 0xE4, 0xEF, 0xD2, 0xD6};
  1016. const BYTE AesKeyV5[] = {
  1017. 0xCD, 0x7E, 0x79, 0x6F, 0x2A, 0xB2, 0x5D, 0xCB, 0x55, 0xFF, 0xC8, 0xEF, 0x83, 0x64, 0xC4, 0x70 };
  1018. const BYTE AesKeyV6[] = {
  1019. 0xA9, 0x4A, 0x41, 0x95, 0xE2, 0x01, 0x43, 0x2D, 0x9B, 0xCB, 0x46, 0x04, 0x05, 0xD8, 0x4A, 0x21 };
  1020. static const BYTE SBox[] = {
  1021. 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B,
  1022. 0xFE, 0xD7, 0xAB, 0x76, 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0,
  1023. 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, 0xB7, 0xFD, 0x93, 0x26,
  1024. 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
  1025. 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2,
  1026. 0xEB, 0x27, 0xB2, 0x75, 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0,
  1027. 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, 0x53, 0xD1, 0x00, 0xED,
  1028. 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
  1029. 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F,
  1030. 0x50, 0x3C, 0x9F, 0xA8, 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5,
  1031. 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, 0xCD, 0x0C, 0x13, 0xEC,
  1032. 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
  1033. 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14,
  1034. 0xDE, 0x5E, 0x0B, 0xDB, 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C,
  1035. 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, 0xE7, 0xC8, 0x37, 0x6D,
  1036. 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
  1037. 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F,
  1038. 0x4B, 0xBD, 0x8B, 0x8A, 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E,
  1039. 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, 0xE1, 0xF8, 0x98, 0x11,
  1040. 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
  1041. 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F,
  1042. 0xB0, 0x54, 0xBB, 0x16
  1043. };
  1044. void XorBlock(const BYTE *const in, const BYTE *out) // Ensure that this is always 32 bit aligned
  1045. {
  1046. /*UAA64( out, 0 ) ^= UAA64( in, 0 );
  1047. UAA64( out, 1 ) ^= UAA64( in, 1 );*/
  1048. uint_fast8_t i;
  1049. for (i = 0; i < AES_BLOCK_WORDS; i++)
  1050. {
  1051. ((DWORD*)out)[i] ^= ((DWORD*)in)[i];
  1052. }
  1053. }
  1054. #define AddRoundKey(d, rk) XorBlock((const BYTE *)rk, (const BYTE *)d)
  1055. #define Mul2(word) (((word & 0x7f7f7f7f) << 1) ^ (((word & 0x80808080) >> 7) * 0x1b))
  1056. #define Mul3(word) (Mul2(word) ^ word)
  1057. #define Mul4(word) (Mul2(Mul2(word)))
  1058. #define Mul8(word) (Mul2(Mul2(Mul2(word))))
  1059. #define Mul9(word) (Mul8(word) ^ word)
  1060. #define MulB(word) (Mul8(word) ^ Mul3(word))
  1061. #define MulD(word) (Mul8(word) ^ Mul4(word) ^ word)
  1062. #define MulE(word) (Mul8(word) ^ Mul4(word) ^ Mul2(word))
  1063. //32 bit Galois Multiplication (generates bigger code than Macros)
  1064. /*static DWORD Mul(DWORD x, DWORD y)
  1065. {
  1066. DWORD result = x, yTemp = y, log2;
  1067. if (!y) return 0;
  1068. for (log2 = 0; yTemp >>= 1; log2++ )
  1069. {
  1070. result = Mul2(result);
  1071. }
  1072. return result ^ Mul(x, y - (1 << log2));
  1073. }*/
  1074. void MixColumnsR(BYTE *restrict state)
  1075. {
  1076. uint_fast8_t i = 0;
  1077. for (; i < AES_BLOCK_WORDS; i++)
  1078. {
  1079. #if defined(_CRYPTO_OPENSSL) && defined(_OPENSSL_SOFTWARE) && defined(_USE_AES_FROM_OPENSSL) //Always byte swap regardless of endianess
  1080. DWORD word = BS32(((DWORD *) state)[i]);
  1081. ((DWORD *) state)[i] = BS32(MulE(word) ^ ROR32(MulB(word), 8) ^ ROR32(MulD(word), 16) ^ ROR32(Mul9(word), 24));
  1082. #else
  1083. DWORD word = LE32(((DWORD *) state)[i]);
  1084. ((DWORD *) state)[i] = LE32(MulE(word) ^ ROR32(MulB(word), 8) ^ ROR32(MulD(word), 16) ^ ROR32(Mul9(word), 24));
  1085. #endif
  1086. }
  1087. }
  1088. static DWORD SubDword(DWORD v)
  1089. {
  1090. BYTE *b = (BYTE *)&v;
  1091. uint_fast8_t i = 0;
  1092. for (; i < sizeof(DWORD); i++) b[i] = SBox[b[i]];
  1093. return v;
  1094. }
  1095. void AesInitKey(AesCtx *Ctx, const BYTE *Key, int_fast8_t IsV6, int RijndaelKeyBytes)
  1096. {
  1097. int RijndaelKeyDwords = RijndaelKeyBytes / sizeof(DWORD);
  1098. Ctx->rounds = (uint_fast8_t)(RijndaelKeyDwords + 6);
  1099. static const DWORD RCon[] = {
  1100. 0x00000000, 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000,
  1101. 0x20000000, 0x40000000, 0x80000000, 0x1B000000, 0x36000000 };
  1102. uint_fast8_t i;
  1103. DWORD temp;
  1104. memcpy(Ctx->Key, Key, RijndaelKeyBytes);
  1105. for ( i = RijndaelKeyDwords; i < ( Ctx->rounds + 1 ) << 2; i++ )
  1106. {
  1107. temp = Ctx->Key[ i - 1 ];
  1108. if ( ( i % RijndaelKeyDwords ) == 0 )
  1109. temp = BE32( SubDword( ROR32( BE32(temp), 24) ) ^ RCon[ i / RijndaelKeyDwords ] );
  1110. Ctx->Key[ i ] = Ctx->Key[ i - RijndaelKeyDwords ] ^ temp;
  1111. }
  1112. if ( IsV6 )
  1113. {
  1114. BYTE *_p = (BYTE *)Ctx->Key;
  1115. _p[ 4 * 16 ] ^= 0x73;
  1116. _p[ 6 * 16 ] ^= 0x09;
  1117. _p[ 8 * 16 ] ^= 0xE4;
  1118. }
  1119. }
  1120. #if !defined(_CRYPTO_OPENSSL) || !defined(_USE_AES_FROM_OPENSSL) || defined(_OPENSSL_SOFTWARE)
  1121. static void SubBytes(BYTE *block)
  1122. {
  1123. uint_fast8_t i;
  1124. for (i = 0; i < AES_BLOCK_BYTES; i++)
  1125. block[i] = SBox[ block[i] ];
  1126. }
  1127. static void ShiftRows(BYTE *state)
  1128. {
  1129. BYTE bIn[AES_BLOCK_BYTES];
  1130. uint_fast8_t i;
  1131. memcpy(bIn, state, AES_BLOCK_BYTES);
  1132. for (i = 0; i < AES_BLOCK_BYTES; i++)
  1133. {
  1134. state[i] = bIn[(i + ((i & 3) << 2)) & 0xf];
  1135. }
  1136. };
  1137. static void MixColumns(BYTE *state)
  1138. {
  1139. uint_fast8_t i = 0;
  1140. for (; i < AES_BLOCK_WORDS; i++)
  1141. {
  1142. DWORD word = LE32(((DWORD *) state)[i]);
  1143. ((DWORD *) state)[i] = LE32(Mul2(word) ^ ROR32(Mul3(word), 8) ^ ROR32(word, 16) ^ ROR32(word, 24));
  1144. }
  1145. }
  1146. void AesEncryptBlock(const AesCtx *const Ctx, BYTE *block)
  1147. {
  1148. uint_fast8_t i;
  1149. for ( i = 0 ;; i += 4 )
  1150. {
  1151. AddRoundKey(block, &Ctx->Key[ i ]);
  1152. SubBytes(block);
  1153. ShiftRows(block);
  1154. if ( i >= ( Ctx->rounds - 1 ) << 2 ) break;
  1155. MixColumns(block);
  1156. }
  1157. AddRoundKey(block, &Ctx->Key[ Ctx->rounds << 2 ]);
  1158. }
  1159. void AesCmacV4(BYTE *Message, size_t MessageSize, BYTE *MacOut)
  1160. {
  1161. size_t i;
  1162. BYTE mac[AES_BLOCK_BYTES];
  1163. AesCtx Ctx;
  1164. AesInitKey(&Ctx, AesKeyV4, FALSE, V4_KEY_BYTES);
  1165. memset(mac, 0, sizeof(mac));
  1166. memset(Message + MessageSize, 0, AES_BLOCK_BYTES);
  1167. Message[MessageSize] = 0x80;
  1168. for (i = 0; i <= MessageSize; i += AES_BLOCK_BYTES)
  1169. {
  1170. XorBlock(Message + i, mac);
  1171. AesEncryptBlock(&Ctx, mac);
  1172. }
  1173. memcpy(MacOut, mac, AES_BLOCK_BYTES);
  1174. }
  1175. #endif
  1176. #if !defined(_CRYPTO_OPENSSL) || !defined(_USE_AES_FROM_OPENSSL)
  1177. static const BYTE SBoxR[] = {
  1178. 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E,
  1179. 0x81, 0xF3, 0xD7, 0xFB, 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87,
  1180. 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB, 0x54, 0x7B, 0x94, 0x32,
  1181. 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
  1182. 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49,
  1183. 0x6D, 0x8B, 0xD1, 0x25, 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16,
  1184. 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92, 0x6C, 0x70, 0x48, 0x50,
  1185. 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
  1186. 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05,
  1187. 0xB8, 0xB3, 0x45, 0x06, 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02,
  1188. 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B, 0x3A, 0x91, 0x11, 0x41,
  1189. 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
  1190. 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8,
  1191. 0x1C, 0x75, 0xDF, 0x6E, 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89,
  1192. 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B, 0xFC, 0x56, 0x3E, 0x4B,
  1193. 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
  1194. 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59,
  1195. 0x27, 0x80, 0xEC, 0x5F, 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D,
  1196. 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, 0xA0, 0xE0, 0x3B, 0x4D,
  1197. 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
  1198. 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63,
  1199. 0x55, 0x21, 0x0C, 0x7D
  1200. };
  1201. static void ShiftRowsR(BYTE *state)
  1202. {
  1203. BYTE b[AES_BLOCK_BYTES];
  1204. uint_fast8_t i;
  1205. memcpy(b, state, AES_BLOCK_BYTES);
  1206. for (i = 0; i < AES_BLOCK_BYTES; i++)
  1207. state[i] = b[(i - ((i & 0x3) << 2)) & 0xf];
  1208. }
  1209. static void SubBytesR(BYTE *block)
  1210. {
  1211. uint_fast8_t i;
  1212. for (i = 0; i < AES_BLOCK_BYTES; i++)
  1213. block[i] = SBoxR[ block[i] ];
  1214. }
  1215. void AesEncryptCbc(const AesCtx *const Ctx, BYTE *restrict iv, BYTE *restrict data, size_t *restrict len)
  1216. {
  1217. // Pad up to blocksize inclusive
  1218. size_t i;
  1219. uint_fast8_t pad = (~*len & (AES_BLOCK_BYTES - 1)) + 1;
  1220. #if defined(__GNUC__) && (__GNUC__ == 4 && __GNUC_MINOR__ == 8) // gcc 4.8 memset bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56977
  1221. for (i = 0; i < pad; i++) data[*len + i] = pad;
  1222. #else
  1223. memset(data + *len, pad, pad);
  1224. #endif
  1225. *len += pad;
  1226. if ( iv ) XorBlock(iv, data);
  1227. AesEncryptBlock(Ctx, data);
  1228. for (i = *len - AES_BLOCK_BYTES; i; i -= AES_BLOCK_BYTES)
  1229. {
  1230. XorBlock(data, data + AES_BLOCK_BYTES);
  1231. data += AES_BLOCK_BYTES;
  1232. AesEncryptBlock(Ctx, data);
  1233. }
  1234. }
  1235. void AesDecryptBlock(const AesCtx *const Ctx, BYTE *block)
  1236. {
  1237. uint_fast8_t i;
  1238. AddRoundKey(block, &Ctx->Key[ Ctx->rounds << 2 ]);
  1239. for ( i = ( Ctx->rounds - 1 ) << 2 ;; i -= 4 )
  1240. {
  1241. ShiftRowsR(block);
  1242. SubBytesR(block);
  1243. AddRoundKey(block, &Ctx->Key[ i ]);
  1244. if ( i == 0 ) break;
  1245. MixColumnsR(block);
  1246. }
  1247. }
  1248. void AesDecryptCbc(const AesCtx *const Ctx, BYTE *iv, BYTE *data, size_t len)
  1249. {
  1250. BYTE *cc;
  1251. for (cc = data + len - AES_BLOCK_BYTES; cc > data; cc -= AES_BLOCK_BYTES)
  1252. {
  1253. AesDecryptBlock(Ctx, cc);
  1254. XorBlock(cc - AES_BLOCK_BYTES, cc);
  1255. }
  1256. AesDecryptBlock(Ctx, cc);
  1257. if ( iv ) XorBlock(iv, cc);
  1258. }
  1259. #endif // _CRYPTO_OPENSSL || OPENSSL_VERSION_NUMBER < 0x10000000L
  1260. #ifndef CONFIG
  1261. #define CONFIG "config.h"
  1262. #endif // CONFIG
  1263. #include CONFIG
  1264. #include <stdio.h>
  1265. #include <string.h>
  1266. #include <stdint.h>
  1267. #include <ctype.h>
  1268. #include <time.h>
  1269. #if !defined(_WIN32)
  1270. #include <sys/socket.h>
  1271. #endif
  1272. #include "output.h"
  1273. #include "crypto.h"
  1274. #include "endian.h"
  1275. #include "kms.h"
  1276. #include "shared_globals.h"
  1277. #include "helpers.h"
  1278. #define FRIENDLY_NAME_WINDOWS "Windows"
  1279. #define FRIENDLY_NAME_OFFICE2010 "Office 2010"
  1280. #define FRIENDLY_NAME_OFFICE2013 "Office"
  1281. #ifndef NO_BASIC_PRODUCT_LIST
  1282. // Do not change the order of this list. Append items as necessary
  1283. const KmsIdList ProductList[] = {
  1284. /* 000 */ { { 0x212a64dc, 0x43b1, 0x4d3d, { 0xa3, 0x0c, 0x2f, 0xc6, 0x9d, 0x20, 0x95, 0xc6 } } /*"212a64dc-43b1-4d3d-a30c-2fc69d2095c6"*/, "Vista", EPID_WINDOWS, 4, 25 },
  1285. /* 001 */ { { 0x7fde5219, 0xfbfa, 0x484a, { 0x82, 0xc9, 0x34, 0xd1, 0xad, 0x53, 0xe8, 0x56 } } /*"7fde5219-fbfa-484a-82c9-34d1ad53e856"*/, "Windows 7", EPID_WINDOWS, 4, 25 },
  1286. /* 002 */ { { 0x3c40b358, 0x5948, 0x45af, { 0x92, 0x3b, 0x53, 0xd2, 0x1f, 0xcc, 0x7e, 0x79 } } /*"3c40b358-5948-45af-923b-53d21fcc7e79"*/, "Windows 8 VL", EPID_WINDOWS, 5, 25 },
  1287. /* 003 */ { { 0x5f94a0bb, 0xd5a0, 0x4081, { 0xa6, 0x85, 0x58, 0x19, 0x41, 0x8b, 0x2f, 0xe0 } } /*"5f94a0bb-d5a0-4081-a685-5819418b2fe0"*/, "Windows Preview", EPID_WINDOWS, 6, 25 },
  1288. /* 004 */ { { 0xbbb97b3b, 0x8ca4, 0x4a28, { 0x97, 0x17, 0x89, 0xfa, 0xbd, 0x42, 0xc4, 0xac } } /*"bbb97b3b-8ca4-4a28-9717-89fabd42c4ac"*/, "Windows 8 Retail", EPID_WINDOWS, 5, 25 },
  1289. /* 005 */ { { 0xcb8fc780, 0x2c05, 0x495a, { 0x97, 0x10, 0x85, 0xaf, 0xff, 0xc9, 0x04, 0xd7 } } /*"cb8fc780-2c05-495a-9710-85afffc904d7"*/, "Windows 8.1 VL", EPID_WINDOWS, 6, 25 },
  1290. /* 006 */ { { 0x6d646890, 0x3606, 0x461a, { 0x86, 0xab, 0x59, 0x8b, 0xb8, 0x4a, 0xce, 0x82 } } /*"6d646890-3606-461a-86ab-598bb84ace82"*/, "Windows 8.1 Retail", EPID_WINDOWS, 6, 25 },
  1291. /* 007 */ { { 0x33e156e4, 0xb76f, 0x4a52, { 0x9f, 0x91, 0xf6, 0x41, 0xdd, 0x95, 0xac, 0x48 } } /*"33e156e4-b76f-4a52-9f91-f641dd95ac48"*/, "Windows 2008 A", EPID_WINDOWS, 4, 5 },
  1292. /* 008 */ { { 0x8fe53387, 0x3087, 0x4447, { 0x89, 0x85, 0xf7, 0x51, 0x32, 0x21, 0x5a, 0xc9 } } /*"8fe53387-3087-4447-8985-f75132215ac9"*/, "Windows 2008 B", EPID_WINDOWS, 4, 5 },
  1293. /* 009 */ { { 0x8a21fdf3, 0xcbc5, 0x44eb, { 0x83, 0xf3, 0xfe, 0x28, 0x4e, 0x66, 0x80, 0xa7 } } /*"8a21fdf3-cbc5-44eb-83f3-fe284e6680a7"*/, "Windows 2008 C", EPID_WINDOWS, 4, 5 },
  1294. /* 010 */ { { 0x0fc6ccaf, 0xff0e, 0x4fae, { 0x9d, 0x08, 0x43, 0x70, 0x78, 0x5b, 0xf7, 0xed } } /*"0fc6ccaf-ff0e-4fae-9d08-4370785bf7ed"*/, "Windows 2008 R2 A", EPID_WINDOWS, 4, 5 },
  1295. /* 011 */ { { 0xca87f5b6, 0xcd46, 0x40c0, { 0xb0, 0x6d, 0x8e, 0xcd, 0x57, 0xa4, 0x37, 0x3f } } /*"ca87f5b6-cd46-40c0-b06d-8ecd57a4373f"*/, "Windows 2008 R2 B", EPID_WINDOWS, 4, 5 },
  1296. /* 012 */ { { 0xb2ca2689, 0xa9a8, 0x42d7, { 0x93, 0x8d, 0xcf, 0x8e, 0x9f, 0x20, 0x19, 0x58 } } /*"b2ca2689-a9a8-42d7-938d-cf8e9f201958"*/, "Windows 2008 R2 C", EPID_WINDOWS, 4, 5 },
  1297. /* 013 */ { { 0x8665cb71, 0x468c, 0x4aa3, { 0xa3, 0x37, 0xcb, 0x9b, 0xc9, 0xd5, 0xea, 0xac } } /*"8665cb71-468c-4aa3-a337-cb9bc9d5eaac"*/, "Windows 2012", EPID_WINDOWS, 5, 5 },
  1298. /* 014 */ { { 0x8456EFD3, 0x0C04, 0x4089, { 0x87, 0x40, 0x5b, 0x72, 0x38, 0x53, 0x5a, 0x65 } } /*"8456EFD3-0C04-4089-8740-5B7238535A65"*/, "Windows 2012 R2", EPID_WINDOWS, 6, 5 },
  1299. /* 015 */ { { 0xe85af946, 0x2e25, 0x47b7, { 0x83, 0xe1, 0xbe, 0xbc, 0xeb, 0xea, 0xc6, 0x11 } } /*"e85af946-2e25-47b7-83e1-bebcebeac611"*/, "Office 2010", EPID_OFFICE2010, 4, 5 },
  1300. /* 016 */ { { 0xe6a6f1bf, 0x9d40, 0x40c3, { 0xaa, 0x9f, 0xc7, 0x7b, 0xa2, 0x15, 0x78, 0xc0 } } /*"e6a6f1bf-9d40-40c3-aa9f-c77ba21578c0"*/, "Office 2013", EPID_OFFICE2013, 6, 5 },
  1301. /* 017 */ { { 0x6d5f5270, 0x31ac, 0x433e, { 0xb9, 0x0a, 0x39, 0x89, 0x29, 0x23, 0xc6, 0x57 } } /*"6d5f5270-31ac-433e-b90a-39892923c657"*/, "Windows Server Preview", EPID_WINDOWS, 6, 5 },
  1302. /* 018 */ { { 0x85b5f61b, 0x320b, 0x4be3, { 0x81, 0x4a, 0xb7, 0x6b, 0x2b, 0xfa, 0xfc, 0x82 } } /*"85b5f61b-320b-4be3-814a-b76b2bfafc82"*/, "Office 2016", EPID_OFFICE2013, 6, 5 },
  1303. /* 019 */ { { 0x58e2134f, 0x8e11, 0x4d17, { 0x9c, 0xb2, 0x91, 0x06, 0x9c, 0x15, 0x11, 0x48 } } /*"58e2134f-8e11-4d17-9cb2-91069c151148"*/, "Windows 10 VL", EPID_WINDOWS, 6, 25 },
  1304. /* 020 */ { { 0xe1c51358, 0xfe3e, 0x4203, { 0xa4, 0xa2, 0x3b, 0x6b, 0x20, 0xc9, 0x73, 0x4e } } /*"e1c51358-fe3e-4203-a4a2-3b6b20c9734e"*/, "Windows 10 Retail", EPID_WINDOWS, 6, 25 },
  1305. /* 021 */ { { 0x00000000, 0x0000, 0x0000, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, NULL, NULL, 0, 0 }
  1306. };
  1307. #endif
  1308. // Application ID is used by KMS server to count KeyManagementServiceCurrentCount
  1309. // Do not change the order of this list. Append items as necessary
  1310. const KmsIdList AppList[] = {
  1311. /* 000 */ { { 0x55c92734, 0xd682, 0x4d71, { 0x98, 0x3e, 0xd6, 0xec, 0x3f, 0x16, 0x05, 0x9f } } /*"55C92734-D682-4D71-983E-D6EC3F16059F"*/, FRIENDLY_NAME_WINDOWS, EPID_WINDOWS, 0, 0},
  1312. /* 001 */ { { 0x59A52881, 0xa989, 0x479d, { 0xaf, 0x46, 0xf2, 0x75, 0xc6, 0x37, 0x06, 0x63 } } /*"59A52881-A989-479D-AF46-F275C6370663"*/, FRIENDLY_NAME_OFFICE2010, EPID_OFFICE2010, 0, 0},
  1313. /* 002 */ { { 0x0FF1CE15, 0xA989, 0x479D, { 0xaf, 0x46, 0xf2, 0x75, 0xc6, 0x37, 0x06, 0x63 } } /*"0FF1CE15-A989-479D-AF46-F275C6370663"*/, FRIENDLY_NAME_OFFICE2013, EPID_OFFICE2013, 0, 0},
  1314. /* 003 */ { { 0x00000000, 0x0000, 0x0000, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, NULL, NULL, 0, 0 }
  1315. };
  1316. #ifndef NO_EXTENDED_PRODUCT_LIST
  1317. const KmsIdList ExtendedProductList [] = {
  1318. // Windows Server
  1319. { { 0xad2542d4, 0x9154, 0x4c6d, { 0x8a, 0x44, 0x30, 0xf1, 0x1e, 0xe9, 0x69, 0x89, } } /*ad2542d4-9154-4c6d-8a44-30f11ee96989*/, "Windows Server 2008 Standard", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2008A },
  1320. { { 0x2401e3d0, 0xc50a, 0x4b58, { 0x87, 0xb2, 0x7e, 0x79, 0x4b, 0x7d, 0x26, 0x07, } } /*2401e3d0-c50a-4b58-87b2-7e794b7d2607*/, "Windows Server 2008 Standard V", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2008A },
  1321. { { 0x68b6e220, 0xcf09, 0x466b, { 0x92, 0xd3, 0x45, 0xcd, 0x96, 0x4b, 0x95, 0x09, } } /*68b6e220-cf09-466b-92d3-45cd964b9509*/, "Windows Server 2008 Datacenter", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2008C },
  1322. { { 0xfd09ef77, 0x5647, 0x4eff, { 0x80, 0x9c, 0xaf, 0x2b, 0x64, 0x65, 0x9a, 0x45, } } /*fd09ef77-5647-4eff-809c-af2b64659a45*/, "Windows Server 2008 Datacenter V", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2008C },
  1323. { { 0xc1af4d90, 0xd1bc, 0x44ca, { 0x85, 0xd4, 0x00, 0x3b, 0xa3, 0x3d, 0xb3, 0xb9, } } /*c1af4d90-d1bc-44ca-85d4-003ba33db3b9*/, "Windows Server 2008 Enterprise", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2008B },
  1324. { { 0x8198490a, 0xadd0, 0x47b2, { 0xb3, 0xba, 0x31, 0x6b, 0x12, 0xd6, 0x47, 0xb4, } } /*8198490a-add0-47b2-b3ba-316b12d647b4*/, "Windows Server 2008 Enterprise V", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2008B },
  1325. { { 0xddfa9f7c, 0xf09e, 0x40b9, { 0x8c, 0x1a, 0xbe, 0x87, 0x7a, 0x9a, 0x7f, 0x4b, } } /*ddfa9f7c-f09e-40b9-8c1a-be877a9a7f4b*/, "Windows Server 2008 Web", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2008A },
  1326. { { 0x7afb1156, 0x2c1d, 0x40fc, { 0xb2, 0x60, 0xaa, 0xb7, 0x44, 0x2b, 0x62, 0xfe, } } /*7afb1156-2c1d-40fc-b260-aab7442b62fe*/, "Windows Server 2008 Compute Cluster", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2008C },
  1327. { { 0x68531fb9, 0x5511, 0x4989, { 0x97, 0xbe, 0xd1, 0x1a, 0x0f, 0x55, 0x63, 0x3f, } } /*68531fb9-5511-4989-97be-d11a0f55633f*/, "Windows Server 2008 R2 Standard", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2008R2A },
  1328. { { 0x7482e61b, 0xc589, 0x4b7f, { 0x8e, 0xcc, 0x46, 0xd4, 0x55, 0xac, 0x3b, 0x87, } } /*7482e61b-c589-4b7f-8ecc-46d455ac3b87*/, "Windows Server 2008 R2 Datacenter", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2008R2C },
  1329. { { 0x620e2b3d, 0x09e7, 0x42fd, { 0x80, 0x2a, 0x17, 0xa1, 0x36, 0x52, 0xfe, 0x7a, } } /*620e2b3d-09e7-42fd-802a-17a13652fe7a*/, "Windows Server 2008 R2 Enterprise", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2008R2B },
  1330. { { 0xa78b8bd9, 0x8017, 0x4df5, { 0xb8, 0x6a, 0x09, 0xf7, 0x56, 0xaf, 0xfa, 0x7c, } } /*a78b8bd9-8017-4df5-b86a-09f756affa7c*/, "Windows Server 2008 R2 Web", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2008R2A },
  1331. { { 0xcda18cf3, 0xc196, 0x46ad, { 0xb2, 0x89, 0x60, 0xc0, 0x72, 0x86, 0x99, 0x94, } } /*cda18cf3-c196-46ad-b289-60c072869994*/, "Windows Server 2008 R2 Compute Cluster", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2008R2C },
  1332. { { 0xd3643d60, 0x0c42, 0x412d, { 0xa7, 0xd6, 0x52, 0xe6, 0x63, 0x53, 0x27, 0xf6, } } /*d3643d60-0c42-412d-a7d6-52e6635327f6*/, "Windows Server 2012 Datacenter", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2012 },
  1333. { { 0xf0f5ec41, 0x0d55, 0x4732, { 0xaf, 0x02, 0x44, 0x0a, 0x44, 0xa3, 0xcf, 0x0f, } } /*f0f5ec41-0d55-4732-af02-440a44a3cf0f*/, "Windows Server 2012 Standard", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2012 },
  1334. { { 0x95fd1c83, 0x7df5, 0x494a, { 0xbe, 0x8b, 0x13, 0x00, 0xe1, 0xc9, 0xd1, 0xcd, } } /*95fd1c83-7df5-494a-be8b-1300e1c9d1cd*/, "Windows Server 2012 MultiPoint Premium", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2012 },
  1335. { { 0x7d5486c7, 0xe120, 0x4771, { 0xb7, 0xf1, 0x7b, 0x56, 0xc6, 0xd3, 0x17, 0x0c, } } /*7d5486c7-e120-4771-b7f1-7b56c6d3170c*/, "Windows Server 2012 MultiPoint Standard", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2012 },
  1336. { { 0x00091344, 0x1ea4, 0x4f37, { 0xb7, 0x89, 0x01, 0x75, 0x0b, 0xa6, 0x98, 0x8c, } } /*00091344-1ea4-4f37-b789-01750ba6988c*/, "Windows Server 2012 R2 Datacenter", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2012R2 },
  1337. { { 0xb3ca044e, 0xa358, 0x4d68, { 0x98, 0x83, 0xaa, 0xa2, 0x94, 0x1a, 0xca, 0x99, } } /*b3ca044e-a358-4d68-9883-aaa2941aca99*/, "Windows Server 2012 R2 Standard", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2012R2 },
  1338. { { 0xb743a2be, 0x68d4, 0x4dd3, { 0xaf, 0x32, 0x92, 0x42, 0x5b, 0x7b, 0xb6, 0x23, } } /*b743a2be-68d4-4dd3-af32-92425b7bb623*/, "Windows Server 2012 R2 Cloud Storage", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2012R2 },
  1339. { { 0x21db6ba4, 0x9a7b, 0x4a14, { 0x9e, 0x29, 0x64, 0xa6, 0x0c, 0x59, 0x30, 0x1d, } } /*21db6ba4-9a7b-4a14-9e29-64a60c59301d*/, "Windows Server 2012 R2 Essentials", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN2012R2 },
  1340. { { 0xba947c44, 0xd19d, 0x4786, { 0xb6, 0xae, 0x22, 0x77, 0x0b, 0xc9, 0x4c, 0x54, } } /*ba947c44-d19d-4786-b6ae-22770bc94c54*/, "Windows Server 2016 Datacenter Preview", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN_SRV_BETA },
  1341. // Windows 10 Preview
  1342. # ifdef INCLUDE_BETAS
  1343. { { 0x6496e59d, 0x89dc, 0x49eb, { 0xa3, 0x53, 0x09, 0xce, 0xb9, 0x40, 0x48, 0x45, } } /*6496e59d-89dc-49eb-a353-09ceb9404845*/, "Windows 10 Core Preview", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN_BETA },
  1344. { { 0xa4383e6b, 0xdada, 0x423d, { 0xa4, 0x3d, 0xf2, 0x56, 0x78, 0x42, 0x96, 0x76, } } /*a4383e6b-dada-423d-a43d-f25678429676*/, "Windows 10 Professional Preview", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN_BETA },
  1345. { { 0xcf59a07b, 0x1a2a, 0x4be0, { 0xbf, 0xe0, 0x42, 0x3b, 0x58, 0x23, 0xe6, 0x63, } } /*cf59a07b-1a2a-4be0-bfe0-423b5823e663*/, "Windows 10 Professional WMC Preview", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN_BETA },
  1346. { { 0xcde952c7, 0x2f96, 0x4d9d, { 0x8f, 0x2b, 0x2d, 0x34, 0x9f, 0x64, 0xfc, 0x51, } } /*cde952c7-2f96-4d9d-8f2b-2d349f64fc51*/, "Windows 10 Enterprise Preview", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN_BETA },
  1347. # endif
  1348. // Windows 10
  1349. { { 0x73111121, 0x5638, 0x40f6, { 0xbc, 0x11, 0xf1, 0xd7, 0xb0, 0xd6, 0x43, 0x00, } } /*73111121-5638-40f6-bc11-f1d7b0d64300*/, "Windows 10 Enterprise", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN10_VL },
  1350. { { 0xe272e3e2, 0x732f, 0x4c65, { 0xa8, 0xf0, 0x48, 0x47, 0x47, 0xd0, 0xd9, 0x47, } } /*e272e3e2-732f-4c65-a8f0-484747d0d947*/, "Windows 10 Enterprise N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN10_VL},
  1351. { { 0x7b51a46c, 0x0c04, 0x4e8f, { 0x9a, 0xf4, 0x84, 0x96, 0xcc, 0xa9, 0x0d, 0x5e, } } /*7b51a46c-0c04-4e8f-9af4-8496cca90d5e*/, "Windows 10 Enterprise LTSB", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN10_VL},
  1352. { { 0x87b838b7, 0x41b6, 0x4590, { 0x83, 0x18, 0x57, 0x97, 0x95, 0x1d, 0x85, 0x29, } } /*87b838b7-41b6-4590-8318-5797951d8529*/, "Windows 10 Enterprise LTSB N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN10_VL},
  1353. { { 0xe0c42288, 0x980c, 0x4788, { 0xa0, 0x14, 0xc0, 0x80, 0xd2, 0xe1, 0x92, 0x6e, } } /*e0c42288-980c-4788-a014-c080d2e1926e*/, "Windows 10 Education", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN10_VL},
  1354. { { 0x3c102355, 0xd027, 0x42c6, { 0xad, 0x23, 0x2e, 0x7e, 0xf8, 0xa0, 0x25, 0x85, } } /*3c102355-d027-42c6-ad23-2e7ef8a02585*/, "Windows 10 Education N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN10_VL},
  1355. { { 0x2de67392, 0xb7a7, 0x462a, { 0xb1, 0xca, 0x10, 0x8d, 0xd1, 0x89, 0xf5, 0x88, } } /*2de67392-b7a7-462a-b1ca-108dd189f588*/, "Windows 10 Professional", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN10_VL },
  1356. { { 0xa80b5abf, 0x75ad, 0x428b, { 0xb0, 0x5d, 0xa4, 0x7d, 0x2d, 0xff, 0xee, 0xbf, } } /*a80b5abf-76ad-428b-b05d-a47d2dffeebf*/, "Windows 10 Professional N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN10_VL},
  1357. { { 0x58e97c99, 0xf377, 0x4ef1, { 0x81, 0xd5, 0x4a, 0xd5, 0x52, 0x2b, 0x5f, 0xd8, } } /*58e97c99-f377-4ef1-81d5-4ad5522b5fd8*/, "Windows 10 Home", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN10_RETAIL},
  1358. { { 0x7b9e1751, 0xa8da, 0x4f75, { 0x95, 0x60, 0x5f, 0xad, 0xfe, 0x3d, 0x8e, 0x38, } } /*7b9e1751-a8da-4f75-9560-5fadfe3d8e38*/, "Windows 10 Home N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN10_RETAIL},
  1359. { { 0xcd918a57, 0xa41b, 0x4c82, { 0x8d, 0xce, 0x1a, 0x53, 0x8e, 0x22, 0x1a, 0x83, } } /*cd918a57-a41b-4c82-8dce-1a538e221a83*/, "Windows 10 Home Single Language", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN10_RETAIL},
  1360. { { 0xa9107544, 0xf4a0, 0x4053, { 0xa9, 0x6a, 0x14, 0x79, 0xab, 0xde, 0xf9, 0x12, } } /*a9107544-f4a0-4053-a96a-1479abdef912*/, "Windows 10 Home Country Specific", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN10_RETAIL},
  1361. // Windows 8.x
  1362. # ifdef INCLUDE_BETAS
  1363. { { 0x2B9C337F, 0x7A1D, 0x4271, { 0x90, 0xA3, 0xC6, 0x85, 0x5A, 0x2B, 0x8A, 0x1C, } } /*2B9C337F-7A1D-4271-90A3-C6855A2B8A1C*/, "Windows 8.x Preview", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN_BETA },
  1364. { { 0x631EAD72, 0xA8AB, 0x4DF8, { 0xBB, 0xDF, 0x37, 0x20, 0x29, 0x98, 0x9B, 0xDD, } } /*631EAD72-A8AB-4DF8-BBDF-372029989BDD*/, "Windows 8.x Preview ARM", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN_BETA },
  1365. # endif
  1366. { { 0x81671aaf, 0x79d1, 0x4eb1, { 0xb0, 0x04, 0x8c, 0xbb, 0xe1, 0x73, 0xaf, 0xea, } } /*81671aaf-79d1-4eb1-b004-8cbbe173afea*/, "Windows 8.1 Enterprise", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_VL },
  1367. { { 0x113e705c, 0xfa49, 0x48a4, { 0xbe, 0xea, 0x7d, 0xd8, 0x79, 0xb4, 0x6b, 0x14, } } /*113e705c-fa49-48a4-beea-7dd879b46b14*/, "Windows 8.1 Enterprise N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_VL },
  1368. { { 0x096ce63d, 0x4fac, 0x48a9, { 0x82, 0xa9, 0x61, 0xae, 0x9e, 0x80, 0x0e, 0x5f, } } /*096ce63d-4fac-48a9-82a9-61ae9e800e5f*/, "Windows 8.1 Professional WMC", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_RETAIL },
  1369. { { 0xc06b6981, 0xd7fd, 0x4a35, { 0xb7, 0xb4, 0x05, 0x47, 0x42, 0xb7, 0xaf, 0x67, } } /*c06b6981-d7fd-4a35-b7b4-054742b7af67*/, "Windows 8.1 Professional", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_VL },
  1370. { { 0x7476d79f, 0x8e48, 0x49b4, { 0xab, 0x63, 0x4d, 0x0b, 0x81, 0x3a, 0x16, 0xe4, } } /*7476d79f-8e48-49b4-ab63-4d0b813a16e4*/, "Windows 8.1 Professional N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_VL },
  1371. { { 0xfe1c3238, 0x432a, 0x43a1, { 0x8e, 0x25, 0x97, 0xe7, 0xd1, 0xef, 0x10, 0xf3, } } /*fe1c3238-432a-43a1-8e25-97e7d1ef10f3*/, "Windows 8.1 Core", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_RETAIL },
  1372. { { 0x78558a64, 0xdc19, 0x43fe, { 0xa0, 0xd0, 0x80, 0x75, 0xb2, 0xa3, 0x70, 0xa3, } } /*78558a64-dc19-43fe-a0d0-8075b2a370a3*/, "Windows 8.1 Core N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_RETAIL },
  1373. { { 0xffee456a, 0xcd87, 0x4390, { 0x8e, 0x07, 0x16, 0x14, 0x6c, 0x67, 0x2f, 0xd0, } } /*ffee456a-cd87-4390-8e07-16146c672fd0*/, "Windows 8.1 Core ARM", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_RETAIL },
  1374. { { 0xc72c6a1d, 0xf252, 0x4e7e, { 0xbd, 0xd1, 0x3f, 0xca, 0x34, 0x2a, 0xcb, 0x35, } } /*c72c6a1d-f252-4e7e-bdd1-3fca342acb35*/, "Windows 8.1 Core Single Language", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_RETAIL },
  1375. { { 0xdb78b74f, 0xef1c, 0x4892, { 0xab, 0xfe, 0x1e, 0x66, 0xb8, 0x23, 0x1d, 0xf6, } } /*db78b74f-ef1c-4892-abfe-1e66b8231df6*/, "Windows 8.1 Core Country Specific", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_RETAIL },
  1376. { { 0xe9942b32, 0x2e55, 0x4197, { 0xb0, 0xbd, 0x5f, 0xf5, 0x8c, 0xba, 0x88, 0x60, } } /*e9942b32-2e55-4197-b0bd-5ff58cba8860*/, "Windows 8.1 Core Connected", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_VL },
  1377. { { 0xc6ddecd6, 0x2354, 0x4c19, { 0x90, 0x9b, 0x30, 0x6a, 0x30, 0x58, 0x48, 0x4e, } } /*c6ddecd6-2354-4c19-909b-306a3058484e*/, "Windows 8.1 Core Connected N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_VL },
  1378. { { 0xb8f5e3a3, 0xed33, 0x4608, { 0x81, 0xe1, 0x37, 0xd6, 0xc9, 0xdc, 0xfd, 0x9c, } } /*b8f5e3a3-ed33-4608-81e1-37d6c9dcfd9c*/, "Windows 8.1 Core Connected Single Language", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_VL },
  1379. { { 0xba998212, 0x460a, 0x44db, { 0xbf, 0xb5, 0x71, 0xbf, 0x09, 0xd1, 0xc6, 0x8b, } } /*ba998212-460a-44db-bfb5-71bf09d1c68b*/, "Windows 8.1 Core Connected Country Specific", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_VL },
  1380. { { 0xe58d87b5, 0x8126, 0x4580, { 0x80, 0xfb, 0x86, 0x1b, 0x22, 0xf7, 0x92, 0x96, } } /*e58d87b5-8126-4580-80fb-861b22f79296*/, "Windows 8.1 Professional Student", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_RETAIL },
  1381. { { 0xcab491c7, 0xa918, 0x4f60, { 0xb5, 0x02, 0xda, 0xb7, 0x5e, 0x33, 0x4f, 0x40, } } /*cab491c7-a918-4f60-b502-dab75e334f40*/, "Windows 8.1 Professional Student N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_RETAIL },
  1382. { { 0xa00018a3, 0xf20f, 0x4632, { 0xbf, 0x7c, 0x8d, 0xaa, 0x53, 0x51, 0xc9, 0x14, } } /*a00018a3-f20f-4632-bf7c-8daa5351c914*/, "Windows 8 Professional WMC", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN8_RETAIL },
  1383. { { 0xa98bcd6d, 0x5343, 0x4603, { 0x8a, 0xfe, 0x59, 0x08, 0xe4, 0x61, 0x11, 0x12, } } /*a98bcd6d-5343-4603-8afe-5908e4611112*/, "Windows 8 Professional", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN8_VL },
  1384. { { 0xebf245c1, 0x29a8, 0x4daf, { 0x9c, 0xb1, 0x38, 0xdf, 0xc6, 0x08, 0xa8, 0xc8, } } /*ebf245c1-29a8-4daf-9cb1-38dfc608a8c8*/, "Windows 8 Professional N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN8_VL },
  1385. { { 0x458e1bec, 0x837a, 0x45f6, { 0xb9, 0xd5, 0x92, 0x5e, 0xd5, 0xd2, 0x99, 0xde, } } /*458e1bec-837a-45f6-b9d5-925ed5d299de*/, "Windows 8 Enterprise", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN8_VL },
  1386. { { 0xe14997e7, 0x800a, 0x4cf7, { 0xad, 0x10, 0xde, 0x4b, 0x45, 0xb5, 0x78, 0xdb, } } /*e14997e7-800a-4cf7-ad10-de4b45b578db*/, "Windows 8 Enterprise N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN8_VL },
  1387. { { 0xc04ed6bf, 0x55c8, 0x4b47, { 0x9f, 0x8e, 0x5a, 0x1f, 0x31, 0xce, 0xee, 0x60, } } /*c04ed6bf-55c8-4b47-9f8e-5a1f31ceee60*/, "Windows 8 Core", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN8_RETAIL },
  1388. { { 0x197390a0, 0x65f6, 0x4a95, { 0xbd, 0xc4, 0x55, 0xd5, 0x8a, 0x3b, 0x02, 0x53, } } /*197390a0-65f6-4a95-bdc4-55d58a3b0253*/, "Windows 8 Core N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN8_RETAIL },
  1389. { { 0x9d5584a2, 0x2d85, 0x419a, { 0x98, 0x2c, 0xa0, 0x08, 0x88, 0xbb, 0x9d, 0xdf, } } /*9d5584a2-2d85-419a-982c-a00888bb9ddf*/, "Windows 8 Core Country Specific", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN8_RETAIL },
  1390. { { 0x8860fcd4, 0xa77b, 0x4a20, { 0x90, 0x45, 0xa1, 0x50, 0xff, 0x11, 0xd6, 0x09, } } /*8860fcd4-a77b-4a20-9045-a150ff11d609*/, "Windows 8 Core Single Language", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN8_RETAIL },
  1391. // Windows 7
  1392. { { 0xae2ee509, 0x1b34, 0x41c0, { 0xac, 0xb7, 0x6d, 0x46, 0x50, 0x16, 0x89, 0x15, } } /*ae2ee509-1b34-41c0-acb7-6d4650168915*/, "Windows 7 Enterprise", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN7 },
  1393. { { 0x1cb6d605, 0x11b3, 0x4e14, { 0xbb, 0x30, 0xda, 0x91, 0xc8, 0xe3, 0x98, 0x3a, } } /*1cb6d605-11b3-4e14-bb30-da91c8e3983a*/, "Windows 7 Enterprise N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN7 },
  1394. { { 0xb92e9980, 0xb9d5, 0x4821, { 0x9c, 0x94, 0x14, 0x0f, 0x63, 0x2f, 0x63, 0x12, } } /*b92e9980-b9d5-4821-9c94-140f632f6312*/, "Windows 7 Professional", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN7 },
  1395. { { 0x54a09a0d, 0xd57b, 0x4c10, { 0x8b, 0x69, 0xa8, 0x42, 0xd6, 0x59, 0x0a, 0xd5, } } /*54a09a0d-d57b-4c10-8b69-a842d6590ad5*/, "Windows 7 Professional N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN7 },
  1396. // Windows Vista
  1397. { { 0xcfd8ff08, 0xc0d7, 0x452b, { 0x9f, 0x60, 0xef, 0x5c, 0x70, 0xc3, 0x20, 0x94, } } /*cfd8ff08-c0d7-452b-9f60-ef5c70c32094*/, "Windows Vista Enterprise", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_VISTA },
  1398. { { 0xd4f54950, 0x26f2, 0x4fb4, { 0xba, 0x21, 0xff, 0xab, 0x16, 0xaf, 0xca, 0xde, } } /*d4f54950-26f2-4fb4-ba21-ffab16afcade*/, "Windows Vista Enterprise N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_VISTA },
  1399. { { 0x4f3d1606, 0x3fea, 0x4c01, { 0xbe, 0x3c, 0x8d, 0x67, 0x1c, 0x40, 0x1e, 0x3b, } } /*4f3d1606-3fea-4c01-be3c-8d671c401e3b*/, "Windows Vista Business", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_VISTA },
  1400. { { 0x2c682dc2, 0x8b68, 0x4f63, { 0xa1, 0x65, 0xae, 0x29, 0x1d, 0x4c, 0xf1, 0x38, } } /*2c682dc2-8b68-4f63-a165-ae291d4cf138*/, "Windows Vista Business N", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_VISTA },
  1401. // Windows Embedded
  1402. { { 0xaa6dd3aa, 0xc2b4, 0x40e2, { 0xa5, 0x44, 0xa6, 0xbb, 0xb3, 0xf5, 0xc3, 0x95, } } /*aa6dd3aa-c2b4-40e2-a544-a6bbb3f5c395*/, "Windows ThinPC", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN7 },
  1403. { { 0xdb537896, 0x376f, 0x48ae, { 0xa4, 0x92, 0x53, 0xd0, 0x54, 0x77, 0x73, 0xd0, } } /*db537896-376f-48ae-a492-53d0547773d0*/, "Windows Embedded POSReady 7", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN7 },
  1404. { { 0x0ab82d54, 0x47f4, 0x4acb, { 0x81, 0x8c, 0xcc, 0x5b, 0xf0, 0xec, 0xb6, 0x49, } } /*0ab82d54-47f4-4acb-818c-cc5bf0ecb649*/, "Windows Embedded Industry 8.1", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_VL },
  1405. { { 0xcd4e2d9f, 0x5059, 0x4a50, { 0xa9, 0x2d, 0x05, 0xd5, 0xbb, 0x12, 0x67, 0xc7, } } /*cd4e2d9f-5059-4a50-a92d-05d5bb1267c7*/, "Windows Embedded Industry E 8.1", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_VL },
  1406. { { 0xf7e88590, 0xdfc7, 0x4c78, { 0xbc, 0xcb, 0x6f, 0x38, 0x65, 0xb9, 0x9d, 0x1a, } } /*f7e88590-dfc7-4c78-bccb-6f3865b99d1a*/, "Windows Embedded Industry A 8.1", EPID_WINDOWS, APP_ID_WINDOWS, KMS_ID_WIN81_VL },
  1407. // Office 2010
  1408. { { 0x8ce7e872, 0x188c, 0x4b98, { 0x9d, 0x90, 0xf8, 0xf9, 0x0b, 0x7a, 0xad, 0x02, } } /*8ce7e872-188c-4b98-9d90-f8f90b7aad02*/, "Office Access 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1409. { { 0xcee5d470, 0x6e3b, 0x4fcc, { 0x8c, 0x2b, 0xd1, 0x74, 0x28, 0x56, 0x8a, 0x9f, } } /*cee5d470-6e3b-4fcc-8c2b-d17428568a9f*/, "Office Excel 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1410. { { 0x8947d0b8, 0xc33b, 0x43e1, { 0x8c, 0x56, 0x9b, 0x67, 0x4c, 0x05, 0x28, 0x32, } } /*8947d0b8-c33b-43e1-8c56-9b674c052832*/, "Office Groove 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1411. { { 0xca6b6639, 0x4ad6, 0x40ae, { 0xa5, 0x75, 0x14, 0xde, 0xe0, 0x7f, 0x64, 0x30, } } /*ca6b6639-4ad6-40ae-a575-14dee07f6430*/, "Office InfoPath 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1412. { { 0x09ed9640, 0xf020, 0x400a, { 0xac, 0xd8, 0xd7, 0xd8, 0x67, 0xdf, 0xd9, 0xc2, } } /*09ed9640-f020-400a-acd8-d7d867dfd9c2*/, "Office Mondo 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1413. { { 0xef3d4e49, 0xa53d, 0x4d81, { 0xa2, 0xb1, 0x2c, 0xa6, 0xc2, 0x55, 0x6b, 0x2c, } } /*ef3d4e49-a53d-4d81-a2b1-2ca6c2556b2c*/, "Office Mondo 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1414. { { 0xab586f5c, 0x5256, 0x4632, { 0x96, 0x2f, 0xfe, 0xfd, 0x8b, 0x49, 0xe6, 0xf4, } } /*ab586f5c-5256-4632-962f-fefd8b49e6f4*/, "Office OneNote 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1415. { { 0xecb7c192, 0x73ab, 0x4ded, { 0xac, 0xf4, 0x23, 0x99, 0xb0, 0x95, 0xd0, 0xcc, } } /*ecb7c192-73ab-4ded-acf4-2399b095d0cc*/, "Office OutLook 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1416. { { 0x45593b1d, 0xdfb1, 0x4e91, { 0xbb, 0xfb, 0x2d, 0x5d, 0x0c, 0xe2, 0x22, 0x7a, } } /*45593b1d-dfb1-4e91-bbfb-2d5d0ce2227a*/, "Office PowerPoint 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1417. { { 0xdf133ff7, 0xbf14, 0x4f95, { 0xaf, 0xe3, 0x7b, 0x48, 0xe7, 0xe3, 0x31, 0xef, } } /*df133ff7-bf14-4f95-afe3-7b48e7e331ef*/, "Office Project Pro 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1418. { { 0x5dc7bf61, 0x5ec9, 0x4996, { 0x9c, 0xcb, 0xdf, 0x80, 0x6a, 0x2d, 0x0e, 0xfe, } } /*5dc7bf61-5ec9-4996-9ccb-df806a2d0efe*/, "Office Project Standard 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1419. { { 0xb50c4f75, 0x599b, 0x43e8, { 0x8d, 0xcd, 0x10, 0x81, 0xa7, 0x96, 0x72, 0x41, } } /*b50c4f75-599b-43e8-8dcd-1081a7967241*/, "Office Publisher 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1420. { { 0x92236105, 0xbb67, 0x494f, { 0x94, 0xc7, 0x7f, 0x7a, 0x60, 0x79, 0x29, 0xbd, } } /*92236105-bb67-494f-94c7-7f7a607929bd*/, "Office Visio Premium 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1421. { { 0xe558389c, 0x83c3, 0x4b29, { 0xad, 0xfe, 0x5e, 0x4d, 0x7f, 0x46, 0xc3, 0x58, } } /*e558389c-83c3-4b29-adfe-5e4d7f46c358*/, "Office Visio Pro 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1422. { { 0x9ed833ff, 0x4f92, 0x4f36, { 0xb3, 0x70, 0x86, 0x83, 0xa4, 0xf1, 0x32, 0x75, } } /*9ed833ff-4f92-4f36-b370-8683a4f13275*/, "Office Visio Standard 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1423. { { 0x2d0882e7, 0xa4e7, 0x423b, { 0x8c, 0xcc, 0x70, 0xd9, 0x1e, 0x01, 0x58, 0xb1, } } /*2d0882e7-a4e7-423b-8ccc-70d91e0158b1*/, "Office Word 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1424. { { 0x6f327760, 0x8c5c, 0x417c, { 0x9b, 0x61, 0x83, 0x6a, 0x98, 0x28, 0x7e, 0x0c, } } /*6f327760-8c5c-417c-9b61-836a98287e0c*/, "Office Professional Plus 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1425. { { 0x9da2a678, 0xfb6b, 0x4e67, { 0xab, 0x84, 0x60, 0xdd, 0x6a, 0x9c, 0x81, 0x9a, } } /*9da2a678-fb6b-4e67-ab84-60dd6a9c819a*/, "Office Standard 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1426. { { 0xea509e87, 0x07a1, 0x4a45, { 0x9e, 0xdc, 0xeb, 0xa5, 0xa3, 0x9f, 0x36, 0xaf, } } /*ea509e87-07a1-4a45-9edc-eba5a39f36af*/, "Office Small Business Basics 2010", EPID_OFFICE2010, APP_ID_OFFICE2010, KMS_ID_OFFICE2010 },
  1427. // Office 2013
  1428. { { 0x6ee7622c, 0x18d8, 0x4005, { 0x9f, 0xb7, 0x92, 0xdb, 0x64, 0x4a, 0x27, 0x9b, } } /*6ee7622c-18d8-4005-9fb7-92db644a279b*/, "Office Access 2013", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2013 },
  1429. { { 0xf7461d52, 0x7c2b, 0x43b2, { 0x87, 0x44, 0xea, 0x95, 0x8e, 0x0b, 0xd0, 0x9a, } } /*f7461d52-7c2b-43b2-8744-ea958e0bd09a*/, "Office Excel 2013", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2013 },
  1430. { { 0xa30b8040, 0xd68a, 0x423f, { 0xb0, 0xb5, 0x9c, 0xe2, 0x92, 0xea, 0x5a, 0x8f, } } /*a30b8040-d68a-423f-b0b5-9ce292ea5a8f*/, "Office InfoPath 2013", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2013 },
  1431. { { 0x1b9f11e3, 0xc85c, 0x4e1b, { 0xbb, 0x29, 0x87, 0x9a, 0xd2, 0xc9, 0x09, 0xe3, } } /*1b9f11e3-c85c-4e1b-bb29-879ad2c909e3*/, "Office Lync 2013", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2013 },
  1432. { { 0xdc981c6b, 0xfc8e, 0x420f, { 0xaa, 0x43, 0xf8, 0xf3, 0x3e, 0x5c, 0x09, 0x23, } } /*dc981c6b-fc8e-420f-aa43-f8f33e5c0923*/, "Office Mondo 2013", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2013 },
  1433. { { 0xefe1f3e6, 0xaea2, 0x4144, { 0xa2, 0x08, 0x32, 0xaa, 0x87, 0x2b, 0x65, 0x45, } } /*efe1f3e6-aea2-4144-a208-32aa872b6545*/, "Office OneNote 2013", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2013 },
  1434. { { 0x771c3afa, 0x50c5, 0x443f, { 0xb1, 0x51, 0xff, 0x25, 0x46, 0xd8, 0x63, 0xa0, } } /*771c3afa-50c5-443f-b151-ff2546d863a0*/, "Office OutLook 2013", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2013 },
  1435. { { 0x8c762649, 0x97d1, 0x4953, { 0xad, 0x27, 0xb7, 0xe2, 0xc2, 0x5b, 0x97, 0x2e, } } /*8c762649-97d1-4953-ad27-b7e2c25b972e*/, "Office PowerPoint 2013", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2013 },
  1436. { { 0x4a5d124a, 0xe620, 0x44ba, { 0xb6, 0xff, 0x65, 0x89, 0x61, 0xb3, 0x3b, 0x9a, } } /*4a5d124a-e620-44ba-b6ff-658961b33b9a*/, "Office Project Pro 2013", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2013 },
  1437. { { 0x427a28d1, 0xd17c, 0x4abf, { 0xb7, 0x17, 0x32, 0xc7, 0x80, 0xba, 0x6f, 0x07, } } /*427a28d1-d17c-4abf-b717-32c780ba6f07*/, "Office Project Standard 2013", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2013 },
  1438. { { 0x00c79ff1, 0x6850, 0x443d, { 0xbf, 0x61, 0x71, 0xcd, 0xe0, 0xde, 0x30, 0x5f, } } /*00c79ff1-6850-443d-bf61-71cde0de305f*/, "Office Publisher 2013", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2013 },
  1439. { { 0xac4efaf0, 0xf81f, 0x4f61, { 0xbd, 0xf7, 0xea, 0x32, 0xb0, 0x2a, 0xb1, 0x17, } } /*ac4efaf0-f81f-4f61-bdf7-ea32b02ab117*/, "Office Visio Standard 2013", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2013 },
  1440. { { 0xe13ac10e, 0x75d0, 0x4aff, { 0xa0, 0xcd, 0x76, 0x49, 0x82, 0xcf, 0x54, 0x1c, } } /*e13ac10e-75d0-4aff-a0cd-764982cf541c*/, "Office Visio Pro 2013", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2013 },
  1441. { { 0xd9f5b1c6, 0x5386, 0x495a, { 0x88, 0xf9, 0x9a, 0xd6, 0xb4, 0x1a, 0xc9, 0xb3, } } /*d9f5b1c6-5386-495a-88f9-9ad6b41ac9b3*/, "Office Word 2013", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2013 },
  1442. { { 0xb322da9c, 0xa2e2, 0x4058, { 0x9e, 0x4e, 0xf5, 0x9a, 0x69, 0x70, 0xbd, 0x69, } } /*b322da9c-a2e2-4058-9e4e-f59a6970bd69*/, "Office Professional Plus 2013", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2013 },
  1443. { { 0xb13afb38, 0xcd79, 0x4ae5, { 0x9f, 0x7f, 0xee, 0xd0, 0x58, 0xd7, 0x50, 0xca, } } /*b13afb38-cd79-4ae5-9f7f-eed058d750ca*/, "Office Standard 2013", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2013 },
  1444. // Office 2016
  1445. { { 0xd450596f, 0x894d, 0x49e0, { 0x96, 0x6a, 0xfd, 0x39, 0xed, 0x4c, 0x4c, 0x64, } } /*d450596f-894d-49e0-966a-fd39ed4c4c64*/, "Office Professional Plus 2016", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2016 },
  1446. { { 0x4f414197, 0x0fc2, 0x4c01, { 0xb6, 0x8a, 0x86, 0xcb, 0xb9, 0xac, 0x25, 0x4c, } } /*4f414197-0fc2-4c01-b68a-86cbb9ac254c*/, "Office Project Pro 2016", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2016 },
  1447. { { 0x6bf301c1, 0xb94a, 0x43e9, { 0xba, 0x31, 0xd4, 0x94, 0x59, 0x8c, 0x47, 0xfb, } } /*6bf301c1-b94a-43e9-ba31-d494598c47fb*/, "Office Visio Pro 2016", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2016 },
  1448. { { 0x041a06cb, 0xc5b8, 0x4772, { 0x80, 0x9f, 0x41, 0x6d, 0x03, 0xd1, 0x66, 0x54, } } /*041a06cb-c5b8-4772-809f-416d03d16654*/, "Office Publisher 2016", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2016 },
  1449. { { 0x67c0fc0c, 0xdeba, 0x401b, { 0xbf, 0x8b, 0x9c, 0x8a, 0xd8, 0x39, 0x58, 0x04, } } /*67c0fc0c-deba-401b-bf8b-9c8ad8395804*/, "Office Access 2016", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2016 },
  1450. { { 0x83e04ee1, 0xfa8d, 0x436d, { 0x89, 0x94, 0xd3, 0x1a, 0x86, 0x2c, 0xab, 0x77, } } /*83e04ee1-fa8d-436d-8994-d31a862cab77*/, "Office Skype for Business 2016", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2016 },
  1451. { { 0x9caabccb, 0x61b1, 0x4b4b, { 0x8b, 0xec, 0xd1, 0x0a, 0x3c, 0x3a, 0xc2, 0xce, } } /*9caabccb-61b1-4b4b-8bec-d10a3c3ac2ce*/, "Office Mondo 2016", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2016 },
  1452. { { 0xaa2a7821, 0x1827, 0x4c2c, { 0x8f, 0x1d, 0x45, 0x13, 0xa3, 0x4d, 0xda, 0x97, } } /*aa2a7821-1827-4c2c-8f1d-4513a34dda97*/, "Office Visio Standard 2016", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2016 },
  1453. { { 0xbb11badf, 0xd8aa, 0x470e, { 0x93, 0x11, 0x20, 0xea, 0xf8, 0x0f, 0xe5, 0xcc, } } /*bb11badf-d8aa-470e-9311-20eaf80fe5cc*/, "Office Word 2016", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2016 },
  1454. { { 0xc3e65d36, 0x141f, 0x4d2f, { 0xa3, 0x03, 0xa8, 0x42, 0xee, 0x75, 0x6a, 0x29, } } /*c3e65d36-141f-4d2f-a303-a842ee756a29*/, "Office Excel 2016", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2016 },
  1455. { { 0xd70b1bba, 0xb893, 0x4544, { 0x96, 0xe2, 0xb7, 0xa3, 0x18, 0x09, 0x1c, 0x33, } } /*d70b1bba-b893-4544-96e2-b7a318091c33*/, "Office Powerpoint 2016", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2016 },
  1456. { { 0xd8cace59, 0x33d2, 0x4ac7, { 0x9b, 0x1b, 0x9b, 0x72, 0x33, 0x9c, 0x51, 0xc8, } } /*d8cace59-33d2-4ac7-9b1b-9b72339c51c8*/, "Office OneNote 2016", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2016 },
  1457. { { 0xda7ddabc, 0x3fbe, 0x4447, { 0x9e, 0x01, 0x6a, 0xb7, 0x44, 0x0b, 0x4c, 0xd4, } } /*da7ddabc-3fbe-4447-9e01-6ab7440b4cd4*/, "Office Project Standard 2016", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2016 },
  1458. { { 0xdedfa23d, 0x6ed1, 0x45a6, { 0x85, 0xdc, 0x63, 0xca, 0xe0, 0x54, 0x6d, 0xe6, } } /*dedfa23d-6ed1-45a6-85dc-63cae0546de6*/, "Office Standard 2016", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2016 },
  1459. { { 0xe914ea6e, 0xa5fa, 0x4439, { 0xa3, 0x94, 0xa9, 0xbb, 0x32, 0x93, 0xca, 0x09, } } /*e914ea6e-a5fa-4439-a394-a9bb3293ca09*/, "Office Mondo R 2016", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2016 },
  1460. { { 0xec9d9265, 0x9d1e, 0x4ed0, { 0x83, 0x8a, 0xcd, 0xc2, 0x0f, 0x25, 0x51, 0xa1, } } /*ec9d9265-9d1e-4ed0-838a-cdc20f2551a1*/, "Office Outlook 2016", EPID_OFFICE2013, APP_ID_OFFICE2013, KMS_ID_OFFICE2016 },
  1461. // End marker (necessity should be removed when time permits)
  1462. { { 0x00000000, 0x0000, 0x0000, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, NULL, NULL, 0, 0 }
  1463. };
  1464. // necessary because other .c files cannot access _countof()
  1465. __pure ProdListIndex_t getExtendedProductListSize(void)
  1466. {
  1467. return _countof(ExtendedProductList) - 1;
  1468. }
  1469. __pure ProdListIndex_t getAppListSize(void)
  1470. {
  1471. return _countof(AppList);
  1472. }
  1473. #endif
  1474. #ifndef NO_RANDOM_EPID
  1475. // HostType and OSBuild
  1476. static const struct KMSHostOS { uint16_t Type; uint16_t Build; } HostOS[] =
  1477. {
  1478. { 55041, 6002 }, // Windows Server 2008 SP2
  1479. { 55041, 7601 }, // Windows Server 2008 R2 SP1
  1480. { 5426, 9200 }, // Windows Server 2012
  1481. { 6401, 9600 }, // Windows Server 2012 R2
  1482. { 3612, 10240}, // Windows Server 2016
  1483. };
  1484. // GroupID and PIDRange
  1485. static const struct PKEYCONFIG { uint16_t GroupID; uint32_t RangeMin; uint32_t RangeMax; } pkeyconfig[] = {
  1486. { 206, 152000000, 191999999 }, // Windows Server 2012 KMS Host pkeyconfig
  1487. { 206, 271000000, 310999999 }, // Windows Server 2012 R2 KMS Host pkeyconfig
  1488. { 96, 199000000, 217999999 }, // Office2010 KMS Host pkeyconfig
  1489. { 206, 234000000, 255999999 }, // Office2013 KMS Host pkeyconfig
  1490. };
  1491. // Valid language identifiers to be used in the ePID
  1492. static const uint16_t LcidList[] = {
  1493. 1078, 1052, 1025, 2049, 3073, 4097, 5121, 6145, 7169, 8193, 9217, 10241, 11265, 12289, 13313, 14337, 15361, 16385,
  1494. 1067, 1068, 2092, 1069, 1059, 1093, 5146, 1026, 1027, 1028, 2052, 3076, 4100, 5124, 1050, 4122, 1029, 1030, 1125, 1043, 2067,
  1495. 1033, 2057, 3081, 4105, 5129, 6153, 7177, 8201, 9225, 10249, 11273, 12297, 13321, 1061, 1080, 1065, 1035, 1036, 2060,
  1496. 3084, 4108, 5132, 6156, 1079, 1110, 1031, 2055, 3079, 4103, 5127, 1032, 1095, 1037, 1081, 1038, 1039, 1057, 1040, 2064, 1041, 1099,
  1497. 1087, 1111, 1042, 1088, 1062, 1063, 1071, 1086, 2110, 1100, 1082, 1153, 1102, 1104, 1044, 2068, 1045, 1046, 2070,
  1498. 1094, 1131, 2155, 3179, 1048, 1049, 9275, 4155, 5179, 3131, 1083, 2107, 8251, 6203, 7227, 1103, 2074, 6170, 3098,
  1499. 7194, 1051, 1060, 1034, 2058, 3082, 4106, 5130, 6154, 7178, 8202, 9226, 10250, 11274, 12298, 13322, 14346, 15370, 16394,
  1500. 17418, 18442, 19466, 20490, 1089, 1053, 2077, 1114, 1097, 1092, 1098, 1054, 1074, 1058, 1056, 1091, 2115, 1066, 1106, 1076, 1077
  1501. };
  1502. #ifdef _PEDANTIC
  1503. uint16_t IsValidLcid(const uint16_t Lcid)
  1504. {
  1505. uint16_t i;
  1506. for (i = 0; i < _countof(LcidList); i++)
  1507. {
  1508. if (Lcid == LcidList[i]) return Lcid;
  1509. }
  1510. return 0;
  1511. }
  1512. #endif // _PEDANTIC
  1513. #endif // NO_RANDOM_EPID
  1514. // Unix time is seconds from 1970-01-01. Should be 64 bits to avoid Year 2035 overflow bug.
  1515. // FILETIME is 100 nanoseconds from 1601-01-01. Must be 64 bits.
  1516. void getUnixTimeAsFileTime(FILETIME *const ts)
  1517. {
  1518. int64_t unixtime = (int64_t)time(NULL);
  1519. int64_t *filetime = (int64_t*)ts;
  1520. *filetime = LE64( (unixtime + 11644473600LL) * 10000000LL );
  1521. }
  1522. __pure int64_t fileTimeToUnixTime(const FILETIME *const ts)
  1523. {
  1524. return LE64( *((const int64_t *const)ts) ) / 10000000LL - 11644473600LL;
  1525. }
  1526. /*
  1527. * Get's a product name with a GUID in host-endian order.
  1528. * List can be any list defined above.
  1529. */
  1530. const char* getProductNameHE(const GUID *const guid, const KmsIdList *const List, ProdListIndex_t *const i)
  1531. {
  1532. for (*i = 0; List[*i].name != NULL; (*i)++)
  1533. {
  1534. if (IsEqualGUID(guid, &List[*i].guid))
  1535. return List[*i].name;
  1536. }
  1537. return "Unknown";
  1538. }
  1539. /*
  1540. * same as getProductnameHE except GUID is in little-endian (network) order
  1541. */
  1542. const char* getProductNameLE(const GUID *const guid, const KmsIdList *const List, ProdListIndex_t *const i)
  1543. {
  1544. #if __BYTE_ORDER != __LITTLE_ENDIAN
  1545. GUID HeGUID;
  1546. LEGUID(&HeGUID, guid);
  1547. return getProductNameHE(&HeGUID, List, i);
  1548. #else
  1549. return getProductNameHE(guid, List, i);
  1550. #endif
  1551. }
  1552. #ifndef NO_RANDOM_EPID
  1553. // formats an int with a fixed number of digits with leading zeros (helper for ePID generation)
  1554. static char* itoc(char *const c, const int i, uint_fast8_t digits)
  1555. {
  1556. char formatString[8];
  1557. if (digits > 9) digits = 0;
  1558. strcpy(formatString,"%");
  1559. if (digits)
  1560. {
  1561. formatString[1] = '0';
  1562. formatString[2] = digits | 0x30;
  1563. formatString[3] = 0;
  1564. }
  1565. strcat(formatString, "u");
  1566. sprintf(c, formatString, i);
  1567. return c;
  1568. }
  1569. static int getRandomServerType()
  1570. {
  1571. # ifndef USE_MSRPC
  1572. if (!UseRpcBTFN)
  1573. # endif // USE_MSRPC
  1574. {
  1575. // This isn't possible at all, e.g. KMS host on XP
  1576. return rand() % (int)_countof(HostOS);
  1577. }
  1578. # ifndef USE_MSRPC
  1579. else
  1580. {
  1581. // return 9200/9600/10240 if NDR64 is in use, otherwise 6002/7601
  1582. if (UseRpcNDR64) return (rand() % 3) + 2;
  1583. return (rand() % 2);
  1584. }
  1585. # endif // USE_MSRPC
  1586. }
  1587. /*
  1588. * Generates a random ePID
  1589. */
  1590. static void generateRandomPid(const int index, char *const szPid, int serverType, int16_t lang)
  1591. {
  1592. int clientApp;
  1593. char numberBuffer[12];
  1594. if (serverType < 0 || serverType >= (int)_countof(HostOS))
  1595. {
  1596. serverType = getRandomServerType();
  1597. }
  1598. strcpy(szPid, itoc(numberBuffer, HostOS[serverType].Type, 5));
  1599. strcat(szPid, "-");
  1600. if (index == 2)
  1601. clientApp = 3;
  1602. else if (index == 1)
  1603. clientApp = 2;
  1604. else
  1605. clientApp = serverType == 3 /*change if HostOS changes*/ ? 1 : 0;
  1606. strcat(szPid, itoc(numberBuffer, pkeyconfig[clientApp].GroupID, 5));
  1607. strcat(szPid, "-");
  1608. int keyId = (rand32() % (pkeyconfig[clientApp].RangeMax - pkeyconfig[clientApp].RangeMin)) + pkeyconfig[clientApp].RangeMin;
  1609. strcat(szPid, itoc(numberBuffer, keyId / 1000000, 3));
  1610. strcat(szPid, "-");
  1611. strcat(szPid, itoc(numberBuffer, keyId % 1000000, 6));
  1612. strcat(szPid, "-03-");
  1613. if (lang < 0) lang = LcidList[rand() % _countof(LcidList)];
  1614. strcat(szPid, itoc(numberBuffer, lang, 0));
  1615. strcat(szPid, "-");
  1616. strcat(szPid, itoc(numberBuffer, HostOS[serverType].Build, 0));
  1617. strcat(szPid, ".0000-");
  1618. # define minTime ((time_t)1436958000) // Release Date Windows 10 RTM Escrow
  1619. time_t maxTime, kmsTime;
  1620. time(&maxTime);
  1621. if (maxTime < minTime) // Just in case the system time is < 07/15/2015 1:00 pm
  1622. maxTime = (time_t)BUILD_TIME;
  1623. kmsTime = (rand32() % (maxTime - minTime)) + minTime;
  1624. # undef minTime
  1625. struct tm *pidTime;
  1626. pidTime = gmtime(&kmsTime);
  1627. strcat(szPid, itoc(numberBuffer, pidTime->tm_yday, 3));
  1628. strcat(szPid, itoc(numberBuffer, pidTime->tm_year + 1900, 4));
  1629. }
  1630. /*
  1631. * Generates random ePIDs and stores them if not already read from ini file.
  1632. * For use with randomization level 1
  1633. */
  1634. void randomPidInit()
  1635. {
  1636. ProdListIndex_t i;
  1637. int serverType = getRandomServerType();
  1638. int16_t lang = Lcid ? Lcid : LcidList[rand() % _countof(LcidList)];
  1639. for (i = 0; i < _countof(AppList) - 1; i++)
  1640. {
  1641. if (KmsResponseParameters[i].Epid) continue;
  1642. char Epid[PID_BUFFER_SIZE];
  1643. generateRandomPid(i, Epid, serverType, lang);
  1644. KmsResponseParameters[i].Epid = (const char*)vlmcsd_malloc(strlen(Epid) + 1);
  1645. strcpy((char*)KmsResponseParameters[i].Epid, Epid);
  1646. #ifndef NO_LOG
  1647. KmsResponseParameters[i].EpidSource = "randomized at program start";
  1648. #endif // NO_LOG
  1649. }
  1650. }
  1651. #endif // NO_RANDOM_EPID
  1652. #ifndef NO_LOG
  1653. /*
  1654. * Logs a Request
  1655. */
  1656. static void logRequest(const REQUEST *const baseRequest)
  1657. {
  1658. const char *productName;
  1659. char clientname[64];
  1660. ProdListIndex_t index;
  1661. #ifndef NO_EXTENDED_PRODUCT_LIST
  1662. productName = getProductNameLE(&baseRequest->ActID, ExtendedProductList, &index);
  1663. if (++index >= (int)_countof(ExtendedProductList))
  1664. #endif // NO_EXTENDED_PRODUCT_LIST
  1665. {
  1666. #ifndef NO_BASIC_PRODUCT_LIST
  1667. productName = getProductNameLE(&baseRequest->KMSID, ProductList, &index);
  1668. if (++index >= (int)_countof(ProductList))
  1669. #endif // NO_BASIC_PRODUCT_LIST
  1670. {
  1671. productName = getProductNameLE(&baseRequest->AppID, AppList, &index);
  1672. }
  1673. }
  1674. #ifndef NO_VERBOSE_LOG
  1675. if (logverbose)
  1676. {
  1677. logger("<<< Incoming KMS request\n");
  1678. logRequestVerbose(baseRequest, &logger);
  1679. }
  1680. else
  1681. {
  1682. #endif // NO_VERBOSE_LOG
  1683. ucs2_to_utf8(baseRequest->WorkstationName, clientname, 64, 64);
  1684. logger("KMS v%i.%i request from %s for %s\n", LE16(baseRequest->MajorVer), LE16(baseRequest->MinorVer), clientname, productName);
  1685. #ifndef NO_VERBOSE_LOG
  1686. }
  1687. #endif // NO_VERBOSE_LOG
  1688. }
  1689. #endif // NO_LOG
  1690. /*
  1691. * Converts a utf-8 ePID string to UCS-2 and writes it to a RESPONSE struct
  1692. */
  1693. static void getEpidFromString(RESPONSE *const Response, const char *const pid)
  1694. {
  1695. size_t length = utf8_to_ucs2(Response->KmsPID, pid, PID_BUFFER_SIZE, PID_BUFFER_SIZE * 3);
  1696. Response->PIDSize = LE32(((unsigned int )length + 1) << 1);
  1697. }
  1698. /*
  1699. * get ePID from appropriate source
  1700. */
  1701. static void getEpid(RESPONSE *const baseResponse, const char** EpidSource, const ProdListIndex_t index, BYTE *const HwId)
  1702. {
  1703. const char* pid;
  1704. if (KmsResponseParameters[index].Epid == NULL)
  1705. {
  1706. #ifndef NO_RANDOM_EPID
  1707. if (RandomizationLevel == 2)
  1708. {
  1709. char szPid[PID_BUFFER_SIZE];
  1710. generateRandomPid(index, szPid, -1, Lcid ? Lcid : -1);
  1711. pid = szPid;
  1712. #ifndef NO_LOG
  1713. *EpidSource = "randomized on every request";
  1714. #endif // NO_LOG
  1715. }
  1716. else
  1717. #endif // NO_RANDOM_EPID
  1718. {
  1719. pid = AppList[index].pid;
  1720. #ifndef NO_LOG
  1721. *EpidSource = "vlmcsd default";
  1722. #endif // NO_LOG
  1723. }
  1724. }
  1725. else
  1726. {
  1727. pid = KmsResponseParameters[index].Epid;
  1728. if (HwId && KmsResponseParameters[index].HwId != NULL)
  1729. memcpy(HwId, KmsResponseParameters[index].HwId, sizeof(((RESPONSE_V6 *)0)->HwId));
  1730. #ifndef NO_LOG
  1731. *EpidSource = KmsResponseParameters[index].EpidSource;
  1732. #endif // NO_LOG
  1733. }
  1734. getEpidFromString(baseResponse, pid);
  1735. }
  1736. #if !defined(NO_LOG) && defined(_PEDANTIC)
  1737. static BOOL CheckVersion4Uuid(const GUID *const guid, const char *const szGuidName)
  1738. {
  1739. if (LE16(guid->Data3) >> 12 != 4 || guid->Data4[0] >> 6 != 2)
  1740. {
  1741. logger("Warning: %s does not conform to version 4 UUID according to RFC 4122\n", szGuidName);
  1742. return FALSE;
  1743. }
  1744. return TRUE;
  1745. }
  1746. static void CheckRequest(const REQUEST *const Request)
  1747. {
  1748. CheckVersion4Uuid(&Request->CMID, "Client machine ID");
  1749. CheckVersion4Uuid(&Request->AppID, "Application ID");
  1750. CheckVersion4Uuid(&Request->KMSID, "Server SKU ID");
  1751. CheckVersion4Uuid(&Request->ActID, "Client SKU ID");
  1752. if (LE32(Request->IsClientVM) > 1)
  1753. logger("Warning: Virtual Machine field in request must be 0 or 1 but is %u\n", LE32(Request->IsClientVM));
  1754. if (LE32(Request->LicenseStatus) > 6 )
  1755. logger("Warning: License status must be between 0 and 6 but is %u\n", LE32(Request->LicenseStatus));
  1756. }
  1757. #endif // !defined(NO_LOG) && defined(_PEDANTIC)
  1758. #ifndef NO_LOG
  1759. /*
  1760. * Logs the Response
  1761. */
  1762. static void logResponse(const RESPONSE *const baseResponse, const BYTE *const hwId, const char *const EpidSource)
  1763. {
  1764. char utf8pid[PID_BUFFER_SIZE * 3];
  1765. ucs2_to_utf8(baseResponse->KmsPID, utf8pid, PID_BUFFER_SIZE, PID_BUFFER_SIZE * 3);
  1766. #ifndef NO_VERBOSE_LOG
  1767. if (!logverbose)
  1768. {
  1769. #endif // NO_VERBOSE_LOG
  1770. logger("Sending ePID (%s): %s\n", EpidSource, utf8pid);
  1771. #ifndef NO_VERBOSE_LOG
  1772. }
  1773. else
  1774. {
  1775. logger(">>> Sending response, ePID source = %s\n", EpidSource);
  1776. logResponseVerbose(utf8pid, hwId, baseResponse, &logger);
  1777. }
  1778. #endif // NO_VERBOSE_LOG
  1779. }
  1780. #endif
  1781. /*
  1782. * Creates the unencrypted base response
  1783. */
  1784. static BOOL __stdcall CreateResponseBaseCallback(const REQUEST *const baseRequest, RESPONSE *const baseResponse, BYTE *const hwId, const char* const ipstr)
  1785. {
  1786. const char* EpidSource;
  1787. #ifndef NO_LOG
  1788. logRequest(baseRequest);
  1789. #ifdef _PEDANTIC
  1790. CheckRequest(baseRequest);
  1791. #endif // _PEDANTIC
  1792. #endif // NO_LOG
  1793. ProdListIndex_t index;
  1794. getProductNameLE(&baseRequest->AppID, AppList, &index);
  1795. if (index >= _countof(AppList) - 1) index = 0; //default to Windows
  1796. getEpid(baseResponse, &EpidSource, index, hwId);
  1797. baseResponse->Version = baseRequest->Version;
  1798. memcpy(&baseResponse->CMID, &baseRequest->CMID, sizeof(GUID));
  1799. memcpy(&baseResponse->ClientTime, &baseRequest->ClientTime, sizeof(FILETIME));
  1800. baseResponse->Count = LE32(LE32(baseRequest->N_Policy) << 1);
  1801. baseResponse->VLActivationInterval = LE32(VLActivationInterval);
  1802. baseResponse->VLRenewalInterval = LE32(VLRenewalInterval);
  1803. #ifndef NO_LOG
  1804. logResponse(baseResponse, hwId, EpidSource);
  1805. #endif // NO_LOG
  1806. return !0;
  1807. }
  1808. RequestCallback_t CreateResponseBase = &CreateResponseBaseCallback;
  1809. ////TODO: Move to helpers.c
  1810. void get16RandomBytes(void* ptr)
  1811. {
  1812. int i;
  1813. for (i = 0; i < 4; i++) ((DWORD*)ptr)[i] = rand32();
  1814. }
  1815. /*
  1816. * Creates v4 response
  1817. */
  1818. size_t CreateResponseV4(REQUEST_V4 *const request_v4, BYTE *const responseBuffer, const char* const ipstr)
  1819. {
  1820. RESPONSE_V4* Response = (RESPONSE_V4*)responseBuffer;
  1821. if ( !CreateResponseBase(&request_v4->RequestBase, &Response->ResponseBase, NULL, ipstr) ) return 0;
  1822. DWORD pidSize = LE32(Response->ResponseBase.PIDSize);
  1823. BYTE* postEpidPtr = responseBuffer + V4_PRE_EPID_SIZE + pidSize;
  1824. memmove(postEpidPtr, &Response->ResponseBase.CMID, V4_POST_EPID_SIZE);
  1825. size_t encryptSize = V4_PRE_EPID_SIZE + V4_POST_EPID_SIZE + pidSize;
  1826. AesCmacV4(responseBuffer, encryptSize, responseBuffer + encryptSize);
  1827. return encryptSize + sizeof(Response->MAC);
  1828. }
  1829. /*
  1830. // Workaround for buggy GCC 4.2/4.3
  1831. #if defined(__GNUC__) && (__GNUC__ == 4 && __GNUC_MINOR__ < 4)
  1832. __attribute__((noinline))
  1833. #endif
  1834. __pure static uint64_t TimestampInterval(void *ts)
  1835. {
  1836. return ( GET_UA64LE(ts) / TIME_C1 ) * TIME_C2 + TIME_C3;
  1837. }*/
  1838. /*
  1839. * Creates the HMAC for v6
  1840. */
  1841. static int_fast8_t CreateV6Hmac(BYTE *const encrypt_start, const size_t encryptSize, int_fast8_t tolerance)
  1842. {
  1843. BYTE hash[32];
  1844. # define halfHashSize (sizeof(hash) >> 1)
  1845. uint64_t timeSlot;
  1846. BYTE *responseEnd = encrypt_start + encryptSize;
  1847. // This is the time from the response
  1848. FILETIME* ft = (FILETIME*)(responseEnd - V6_POST_EPID_SIZE + sizeof(((RESPONSE*)0)->CMID));
  1849. // Generate a time slot that changes every 4.11 hours.
  1850. // Request and repsonse time must match +/- 1 slot.
  1851. // When generating a response tolerance must be 0.
  1852. // If verifying the hash, try tolerance -1, 0 and +1. One of them must match.
  1853. timeSlot = LE64( (GET_UA64LE(ft) / TIME_C1 * TIME_C2 + TIME_C3) + (tolerance * TIME_C1) );
  1854. // The time slot is hashed with SHA256 so it is not so obvious that it is time
  1855. Sha256((BYTE*) &timeSlot, sizeof(timeSlot), hash);
  1856. // The last 16 bytes of the hashed time slot are the actual HMAC key
  1857. if (!Sha256Hmac
  1858. (
  1859. hash + halfHashSize, // Use last 16 bytes of SHA256 as HMAC key
  1860. encrypt_start, // hash only the encrypted part of the v6 response
  1861. encryptSize - sizeof(((RESPONSE_V6*)0)->HMAC), // encryptSize minus the HMAC itself
  1862. hash // use same buffer for resulting hash where the key came from
  1863. ))
  1864. {
  1865. return FALSE;
  1866. }
  1867. memcpy(responseEnd - sizeof(((RESPONSE_V6*)0)->HMAC), hash + halfHashSize, halfHashSize);
  1868. return TRUE;
  1869. # undef halfHashSize
  1870. }
  1871. /*
  1872. * Creates v5 or v6 response
  1873. */
  1874. size_t CreateResponseV6(REQUEST_V6 *restrict request_v6, BYTE *const responseBuffer, const char* const ipstr)
  1875. {
  1876. // The response will be created in a fixed sized struct to
  1877. // avoid unaligned access macros and packed structs on RISC systems
  1878. // which largely increase code size.
  1879. //
  1880. // The fixed sized struct with 64 WCHARs for the ePID will be converted
  1881. // to a variable sized struct later and requires unaligned access macros.
  1882. RESPONSE_V6* Response = (RESPONSE_V6*)responseBuffer;
  1883. RESPONSE* baseResponse = &Response->ResponseBase;
  1884. #ifdef _DEBUG
  1885. RESPONSE_V6_DEBUG* xxx = (RESPONSE_V6_DEBUG*)responseBuffer;
  1886. #endif
  1887. static const BYTE DefaultHwid[8] = { HWID };
  1888. int_fast8_t v6 = LE16(request_v6->MajorVer) > 5;
  1889. AesCtx aesCtx;
  1890. AesInitKey(&aesCtx, v6 ? AesKeyV6 : AesKeyV5, v6, AES_KEY_BYTES);
  1891. AesDecryptCbc(&aesCtx, NULL, request_v6->IV, V6_DECRYPT_SIZE);
  1892. // get random salt and SHA256 it
  1893. get16RandomBytes(Response->RandomXoredIVs);
  1894. Sha256(Response->RandomXoredIVs, sizeof(Response->RandomXoredIVs), Response->Hash);
  1895. if (v6) // V6 specific stuff
  1896. {
  1897. // In v6 a random IV is generated
  1898. Response->Version = request_v6->Version;
  1899. get16RandomBytes(Response->IV);
  1900. // pre-fill with default HwId (not required for v5)
  1901. memcpy(Response->HwId, DefaultHwid, sizeof(Response->HwId));
  1902. // Just copy decrypted request IV (using Null IV) here. Note this is identical
  1903. // to XORing non-decrypted request and reponse IVs
  1904. memcpy(Response->XoredIVs, request_v6->IV, sizeof(Response->XoredIVs));
  1905. }
  1906. else // V5 specific stuff
  1907. {
  1908. // In v5 IVs of request and response must be identical (MS client checks this)
  1909. // The following memcpy copies Version and IVs at once
  1910. memcpy(Response, request_v6, V6_UNENCRYPTED_SIZE);
  1911. }
  1912. // Xor Random bytes with decrypted request IV
  1913. XorBlock(request_v6->IV, Response->RandomXoredIVs);
  1914. // Get the base response
  1915. if ( !CreateResponseBase(&request_v6->RequestBase, baseResponse, Response->HwId, ipstr) ) return 0;
  1916. // Convert the fixed sized struct into variable sized
  1917. DWORD pidSize = LE32(baseResponse->PIDSize);
  1918. BYTE* postEpidPtr = responseBuffer + V6_PRE_EPID_SIZE + pidSize;
  1919. size_t post_epid_size = v6 ? V6_POST_EPID_SIZE : V5_POST_EPID_SIZE;
  1920. memmove(postEpidPtr, &baseResponse->CMID, post_epid_size);
  1921. // number of bytes to encrypt
  1922. size_t encryptSize =
  1923. V6_PRE_EPID_SIZE
  1924. - sizeof(Response->Version)
  1925. + pidSize
  1926. + post_epid_size;
  1927. //AesDecryptBlock(&aesCtx, Response->IV);
  1928. if (v6 && !CreateV6Hmac(Response->IV, encryptSize, 0)) return 0;
  1929. // Padding auto handled by encryption func
  1930. AesEncryptCbc(&aesCtx, NULL, Response->IV, &encryptSize);
  1931. return encryptSize + sizeof(Response->Version);
  1932. }
  1933. // Create Hashed KMS Client Request Data for KMS Protocol Version 4
  1934. BYTE *CreateRequestV4(size_t *size, const REQUEST* requestBase)
  1935. {
  1936. *size = sizeof(REQUEST_V4);
  1937. // Build a proper KMS client request data
  1938. BYTE *request = (BYTE *)vlmcsd_malloc(sizeof(REQUEST_V4));
  1939. // Temporary Pointer for access to REQUEST_V4 structure
  1940. REQUEST_V4 *request_v4 = (REQUEST_V4 *)request;
  1941. // Set KMS Client Request Base
  1942. memcpy(&request_v4->RequestBase, requestBase, sizeof(REQUEST));
  1943. // Generate Hash Signature
  1944. AesCmacV4(request, sizeof(REQUEST), request_v4->MAC);
  1945. // Return Request Data
  1946. return request;
  1947. }
  1948. // Create Encrypted KMS Client Request Data for KMS Protocol Version 6
  1949. BYTE* CreateRequestV6(size_t *size, const REQUEST* requestBase)
  1950. {
  1951. *size = sizeof(REQUEST_V6);
  1952. // Temporary Pointer for access to REQUEST_V5 structure
  1953. REQUEST_V6 *request = (REQUEST_V6 *)vlmcsd_malloc(sizeof(REQUEST_V6));
  1954. // KMS Protocol Version
  1955. request->Version = requestBase->Version;
  1956. // Initialize the IV
  1957. get16RandomBytes(request->IV);
  1958. // Set KMS Client Request Base
  1959. memcpy(&request->RequestBase, requestBase, sizeof(REQUEST));
  1960. // Encrypt KMS Client Request
  1961. size_t encryptSize = sizeof(request->RequestBase);
  1962. AesCtx Ctx;
  1963. int_fast8_t v6 = LE16(request->MajorVer) > 5;
  1964. AesInitKey(&Ctx, v6 ? AesKeyV6 : AesKeyV5, v6, 16);
  1965. AesEncryptCbc(&Ctx, request->IV, (BYTE*)(&request->RequestBase), &encryptSize);
  1966. // Return Proper Request Data
  1967. return (BYTE*)request;
  1968. }
  1969. /*
  1970. * Checks whether Length of ePID is valid
  1971. */
  1972. static uint8_t checkPidLength(const RESPONSE *const responseBase)
  1973. {
  1974. unsigned int i;
  1975. if (LE32(responseBase->PIDSize) > (PID_BUFFER_SIZE << 1)) return FALSE;
  1976. if (responseBase->KmsPID[(LE32(responseBase->PIDSize) >> 1) - 1]) return FALSE;
  1977. for (i = 0; i < (LE32(responseBase->PIDSize) >> 1) - 2; i++)
  1978. {
  1979. if (!responseBase->KmsPID[i]) return FALSE;
  1980. }
  1981. return TRUE;
  1982. }
  1983. /*
  1984. * "Decrypts" a KMS v4 response. Actually just copies to a fixed size buffer
  1985. */
  1986. RESPONSE_RESULT DecryptResponseV4(RESPONSE_V4* response_v4, const int responseSize, BYTE* const rawResponse, const BYTE* const rawRequest)
  1987. {
  1988. int copySize =
  1989. V4_PRE_EPID_SIZE +
  1990. (LE32(((RESPONSE_V4*)rawResponse)->ResponseBase.PIDSize) <= PID_BUFFER_SIZE << 1 ?
  1991. LE32(((RESPONSE_V4*)rawResponse)->ResponseBase.PIDSize) :
  1992. PID_BUFFER_SIZE << 1);
  1993. int messageSize = copySize + V4_POST_EPID_SIZE;
  1994. memcpy(response_v4, rawResponse, copySize);
  1995. memcpy(&response_v4->ResponseBase.CMID, rawResponse + copySize, responseSize - copySize);
  1996. // ensure PID is null terminated
  1997. response_v4->ResponseBase.KmsPID[PID_BUFFER_SIZE-1] = 0;
  1998. uint8_t* mac = rawResponse + messageSize;
  1999. AesCmacV4(rawResponse, messageSize, mac);
  2000. REQUEST_V4* request_v4 = (REQUEST_V4*)rawRequest;
  2001. RESPONSE_RESULT result;
  2002. result.mask = (DWORD)~0;
  2003. result.PidLengthOK = checkPidLength((RESPONSE*)rawResponse);
  2004. result.VersionOK = response_v4->ResponseBase.Version == request_v4->RequestBase.Version;
  2005. result.HashOK = !memcmp(&response_v4->MAC, mac, sizeof(response_v4->MAC));
  2006. result.TimeStampOK = !memcmp(&response_v4->ResponseBase.ClientTime, &request_v4->RequestBase.ClientTime, sizeof(FILETIME));
  2007. result.ClientMachineIDOK = !memcmp(&response_v4->ResponseBase.CMID, &request_v4->RequestBase.CMID, sizeof(GUID));
  2008. result.effectiveResponseSize = responseSize;
  2009. result.correctResponseSize = sizeof(RESPONSE_V4) - sizeof(response_v4->ResponseBase.KmsPID) + LE32(response_v4->ResponseBase.PIDSize);
  2010. return result;
  2011. }
  2012. static RESPONSE_RESULT VerifyResponseV6(RESPONSE_RESULT result, const AesCtx* Ctx, RESPONSE_V6* response_v6, REQUEST_V6* request_v6, BYTE* const rawResponse)
  2013. {
  2014. // Check IVs
  2015. result.IVsOK = !memcmp // In V6 the XoredIV is actually the request IV
  2016. (
  2017. response_v6->XoredIVs,
  2018. request_v6->IV,
  2019. sizeof(response_v6->XoredIVs)
  2020. );
  2021. result.IVnotSuspicious = !!memcmp // If IVs are identical, it is obviously an emulator
  2022. (
  2023. request_v6->IV,
  2024. response_v6->IV,
  2025. sizeof(request_v6->IV)
  2026. );
  2027. // Check Hmac
  2028. int_fast8_t tolerance;
  2029. BYTE OldHmac[sizeof(response_v6->HMAC)];
  2030. result.HmacSha256OK = FALSE;
  2031. memcpy // Save received HMAC to compare with calculated HMAC later
  2032. (
  2033. OldHmac,
  2034. response_v6->HMAC,
  2035. sizeof(response_v6->HMAC)
  2036. );
  2037. //AesEncryptBlock(Ctx, Response_v6->IV); // CreateV6Hmac needs original IV as received over the network
  2038. for (tolerance = -1; tolerance < 2; tolerance++)
  2039. {
  2040. CreateV6Hmac
  2041. (
  2042. rawResponse + sizeof(response_v6->Version), // Pointer to start of the encrypted part of the response
  2043. (size_t)result.correctResponseSize - V6_UNENCRYPTED_SIZE, // size of the encrypted part
  2044. tolerance // tolerance -1, 0, or +1
  2045. );
  2046. if
  2047. ((
  2048. result.HmacSha256OK = !memcmp // Compare both HMACs
  2049. (
  2050. OldHmac,
  2051. rawResponse + (size_t)result.correctResponseSize - sizeof(response_v6->HMAC),
  2052. sizeof(OldHmac)
  2053. )
  2054. ))
  2055. {
  2056. break;
  2057. }
  2058. }
  2059. return result;
  2060. }
  2061. static RESPONSE_RESULT VerifyResponseV5(RESPONSE_RESULT result, REQUEST_V5* request_v5, RESPONSE_V5* response_v5)
  2062. {
  2063. // Check IVs: in V5 (and only v5) request and response IVs must match
  2064. result.IVsOK = !memcmp(request_v5->IV, response_v5->IV, sizeof(request_v5->IV));
  2065. // V5 has no Hmac, always set to TRUE
  2066. result.HmacSha256OK = TRUE;
  2067. return result;
  2068. }
  2069. /*
  2070. * Decrypts a KMS v5 or v6 response received from a server.
  2071. * hwid must supply a valid 16 byte buffer for v6. hwid is ignored in v5
  2072. */
  2073. RESPONSE_RESULT DecryptResponseV6(RESPONSE_V6* response_v6, int responseSize, BYTE* const response, const BYTE* const rawRequest, BYTE* hwid)
  2074. {
  2075. RESPONSE_RESULT result;
  2076. result.mask = ~0; // Set all bits in the results mask to 1. Assume success first.
  2077. result.effectiveResponseSize = responseSize;
  2078. int copySize1 =
  2079. sizeof(response_v6->Version);
  2080. // Decrypt KMS Server Response (encrypted part starts after RequestIV)
  2081. responseSize -= copySize1;
  2082. AesCtx Ctx;
  2083. int_fast8_t v6 = LE16(((RESPONSE_V6*)response)->MajorVer) > 5;
  2084. AesInitKey(&Ctx, v6 ? AesKeyV6 : AesKeyV5, v6, AES_KEY_BYTES);
  2085. AesDecryptCbc(&Ctx, NULL, response + copySize1, responseSize);
  2086. // Check padding
  2087. BYTE* lastPadByte = response + (size_t)result.effectiveResponseSize - 1;
  2088. // Must be from 1 to 16
  2089. if (!*lastPadByte || *lastPadByte > AES_BLOCK_BYTES)
  2090. {
  2091. result.DecryptSuccess = FALSE;
  2092. return result;
  2093. }
  2094. // Check if pad bytes are all the same
  2095. BYTE* padByte;
  2096. for (padByte = lastPadByte - *lastPadByte + 1; padByte < lastPadByte; padByte++)
  2097. if (*padByte != *lastPadByte)
  2098. {
  2099. result.DecryptSuccess = FALSE;
  2100. return result;
  2101. }
  2102. // Add size of Version, KmsPIDLen and variable size PID
  2103. DWORD pidSize = LE32(((RESPONSE_V6*) response)->ResponseBase.PIDSize);
  2104. copySize1 +=
  2105. V6_UNENCRYPTED_SIZE +
  2106. sizeof(response_v6->ResponseBase.PIDSize) +
  2107. (pidSize <= PID_BUFFER_SIZE << 1 ? pidSize : PID_BUFFER_SIZE << 1);
  2108. // Copy part 1 of response up to variable sized PID
  2109. memcpy(response_v6, response, copySize1);
  2110. // ensure PID is null terminated
  2111. response_v6->ResponseBase.KmsPID[PID_BUFFER_SIZE - 1] = 0;
  2112. // Copy part 2
  2113. size_t copySize2 = v6 ? V6_POST_EPID_SIZE : V5_POST_EPID_SIZE;
  2114. memcpy(&response_v6->ResponseBase.CMID, response + copySize1, copySize2);
  2115. // Decrypting the response is finished here. Now we check the results for validity
  2116. // A basic client doesn't need the stuff below this comment but we want to use vlmcs
  2117. // as a debug tool for KMS emulators.
  2118. REQUEST_V6* request_v6 = (REQUEST_V6*) rawRequest;
  2119. DWORD decryptSize = sizeof(request_v6->IV) + sizeof(request_v6->RequestBase) + sizeof(request_v6->Pad);
  2120. AesDecryptCbc(&Ctx, NULL, request_v6->IV, decryptSize);
  2121. // Check that all version informations are the same
  2122. result.VersionOK =
  2123. request_v6->Version == response_v6->ResponseBase.Version &&
  2124. request_v6->Version == response_v6->Version &&
  2125. request_v6->Version == request_v6->RequestBase.Version;
  2126. // Check Base Request
  2127. result.PidLengthOK = checkPidLength(&((RESPONSE_V6*) response)->ResponseBase);
  2128. result.TimeStampOK = !memcmp(&response_v6->ResponseBase.ClientTime, &request_v6->RequestBase.ClientTime, sizeof(FILETIME));
  2129. result.ClientMachineIDOK = IsEqualGUID(&response_v6->ResponseBase.CMID, &request_v6->RequestBase.CMID);
  2130. // Rebuild Random Key and Sha256 Hash
  2131. BYTE HashVerify[sizeof(response_v6->Hash)];
  2132. BYTE RandomKey[sizeof(response_v6->RandomXoredIVs)];
  2133. memcpy(RandomKey, request_v6->IV, sizeof(RandomKey));
  2134. XorBlock(response_v6->RandomXoredIVs, RandomKey);
  2135. Sha256(RandomKey, sizeof(RandomKey), HashVerify);
  2136. result.HashOK = !memcmp(response_v6->Hash, HashVerify, sizeof(HashVerify));
  2137. // size before encryption (padding not included)
  2138. result.correctResponseSize =
  2139. (v6 ? sizeof(RESPONSE_V6) : sizeof(RESPONSE_V5))
  2140. - sizeof(response_v6->ResponseBase.KmsPID)
  2141. + LE32(response_v6->ResponseBase.PIDSize);
  2142. // Version specific stuff
  2143. if (v6)
  2144. {
  2145. // Copy the HwId
  2146. memcpy(hwid, response_v6->HwId, sizeof(response_v6->HwId));
  2147. // Verify the V6 specific part of the response
  2148. result = VerifyResponseV6(result, &Ctx, response_v6, request_v6, response);
  2149. }
  2150. else // V5
  2151. {
  2152. // Verify the V5 specific part of the response
  2153. result = VerifyResponseV5(result, request_v6, (RESPONSE_V5*)response_v6);
  2154. }
  2155. // padded size after encryption
  2156. result.correctResponseSize += (~(result.correctResponseSize - sizeof(response_v6->ResponseBase.Version)) & 0xf) + 1;
  2157. return result;
  2158. }
  2159. #ifndef CONFIG
  2160. #define CONFIG "config.h"
  2161. #endif // CONFIG
  2162. #include CONFIG
  2163. #include "endian.h"
  2164. #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && defined(__LITTLE_ENDIAN) \
  2165. && defined(BS16) && defined(BS32) && defined(BS64)
  2166. #else // ! defined(__BYTE_ORDER)
  2167. void PUT_UAA64BE(void *p, unsigned long long v, unsigned int i)
  2168. {
  2169. unsigned char *_p = (unsigned char *)&((unsigned long long *)p)[i];
  2170. _p[ 0 ] = v >> 56;
  2171. _p[ 1 ] = v >> 48;
  2172. _p[ 2 ] = v >> 40;
  2173. _p[ 3 ] = v >> 32;
  2174. _p[ 4 ] = v >> 24;
  2175. _p[ 5 ] = v >> 16;
  2176. _p[ 6 ] = v >> 8;
  2177. _p[ 7 ] = v;
  2178. }
  2179. void PUT_UAA32BE(void *p, unsigned int v, unsigned int i)
  2180. {
  2181. unsigned char *_p = (unsigned char *)&((unsigned int *)p)[i];
  2182. _p[ 0 ] = v >> 24;
  2183. _p[ 1 ] = v >> 16;
  2184. _p[ 2 ] = v >> 8;
  2185. _p[ 3 ] = v;
  2186. }
  2187. void PUT_UAA16BE(void *p, unsigned short v, unsigned int i)
  2188. {
  2189. unsigned char *_p = (unsigned char *)&((unsigned short *)p)[i];
  2190. _p[ 0 ] = v >> 8;
  2191. _p[ 1 ] = v;
  2192. }
  2193. void PUT_UAA64LE(void *p, unsigned long long v, unsigned int i)
  2194. {
  2195. unsigned char *_p = (unsigned char *)&((unsigned long long *)p)[i];
  2196. _p[ 0 ] = v;
  2197. _p[ 1 ] = v >> 8;
  2198. _p[ 2 ] = v >> 16;
  2199. _p[ 3 ] = v >> 24;
  2200. _p[ 4 ] = v >> 32;
  2201. _p[ 5 ] = v >> 40;
  2202. _p[ 6 ] = v >> 48;
  2203. _p[ 7 ] = v >> 56;
  2204. }
  2205. void PUT_UAA32LE(void *p, unsigned int v, unsigned int i)
  2206. {
  2207. unsigned char *_p = (unsigned char *)&((unsigned int *)p)[i];
  2208. _p[ 0 ] = v;
  2209. _p[ 1 ] = v >> 8;
  2210. _p[ 2 ] = v >> 16;
  2211. _p[ 3 ] = v >> 24;
  2212. }
  2213. void PUT_UAA16LE(void *p, unsigned short v, unsigned int i)
  2214. {
  2215. unsigned char *_p = (unsigned char *)&((unsigned short *)p)[i];
  2216. _p[ 0 ] = v;
  2217. _p[ 1 ] = v >> 8;
  2218. }
  2219. unsigned long long GET_UAA64BE(void *p, unsigned int i)
  2220. {
  2221. unsigned char *_p = (unsigned char *)&((unsigned long long *)p)[i];
  2222. return
  2223. (unsigned long long)_p[ 0 ] << 56 |
  2224. (unsigned long long)_p[ 1 ] << 48 |
  2225. (unsigned long long)_p[ 2 ] << 40 |
  2226. (unsigned long long)_p[ 3 ] << 32 |
  2227. (unsigned long long)_p[ 4 ] << 24 |
  2228. (unsigned long long)_p[ 5 ] << 16 |
  2229. (unsigned long long)_p[ 6 ] << 8 |
  2230. (unsigned long long)_p[ 7 ];
  2231. }
  2232. unsigned int GET_UAA32BE(void *p, unsigned int i)
  2233. {
  2234. unsigned char *_p = (unsigned char *)&((unsigned int *)p)[i];
  2235. return
  2236. (unsigned int)_p[ 0 ] << 24 |
  2237. (unsigned int)_p[ 1 ] << 16 |
  2238. (unsigned int)_p[ 2 ] << 8 |
  2239. (unsigned int)_p[ 3 ];
  2240. }
  2241. unsigned short GET_UAA16BE(void *p, unsigned int i)
  2242. {
  2243. unsigned char *_p = (unsigned char *)&((unsigned short *)p)[i];
  2244. return
  2245. (unsigned short)_p[ 0 ] << 8 |
  2246. (unsigned short)_p[ 1 ];
  2247. }
  2248. unsigned long long GET_UAA64LE(void *p, unsigned int i)
  2249. {
  2250. unsigned char *_p = (unsigned char *)&((unsigned long long *)p)[i];
  2251. return
  2252. (unsigned long long)_p[ 0 ] |
  2253. (unsigned long long)_p[ 1 ] << 8 |
  2254. (unsigned long long)_p[ 2 ] << 16 |
  2255. (unsigned long long)_p[ 3 ] << 24 |
  2256. (unsigned long long)_p[ 4 ] << 32 |
  2257. (unsigned long long)_p[ 5 ] << 40 |
  2258. (unsigned long long)_p[ 6 ] << 48 |
  2259. (unsigned long long)_p[ 7 ] << 56;
  2260. }
  2261. unsigned int GET_UAA32LE(void *p, unsigned int i)
  2262. {
  2263. unsigned char *_p = (unsigned char *)&((unsigned int *)p)[i];
  2264. return
  2265. (unsigned int)_p[ 0 ] |
  2266. (unsigned int)_p[ 1 ] << 8 |
  2267. (unsigned int)_p[ 2 ] << 16 |
  2268. (unsigned int)_p[ 3 ] << 24;
  2269. }
  2270. unsigned short GET_UAA16LE(void *p, unsigned int i)
  2271. {
  2272. unsigned char *_p = (unsigned char *)&((unsigned short *)p)[i];
  2273. return
  2274. (unsigned short)_p[ 0 ] |
  2275. (unsigned short)_p[ 1 ] << 8;
  2276. }
  2277. unsigned short BE16(unsigned short x)
  2278. {
  2279. return GET_UAA16BE(&x, 0);
  2280. }
  2281. unsigned short LE16(unsigned short x)
  2282. {
  2283. return GET_UAA16LE(&x, 0);
  2284. }
  2285. unsigned int BE32(unsigned int x)
  2286. {
  2287. return GET_UAA32BE(&x, 0);
  2288. }
  2289. unsigned int LE32(unsigned int x)
  2290. {
  2291. return GET_UAA32LE(&x, 0);
  2292. }
  2293. unsigned long long BE64(unsigned long long x)
  2294. {
  2295. return GET_UAA64BE(&x, 0);
  2296. }
  2297. inline unsigned long long LE64(unsigned long long x)
  2298. {
  2299. return GET_UAA64LE(&x, 0);
  2300. }
  2301. #endif // defined(__BYTE_ORDER)
  2302. #ifndef _DEFAULT_SOURCE
  2303. #define _DEFAULT_SOURCE
  2304. #endif // _DEFAULT_SOURCE
  2305. #ifndef CONFIG
  2306. #define CONFIG "config.h"
  2307. #endif // CONFIG
  2308. #include CONFIG
  2309. #include "output.h"
  2310. #include "shared_globals.h"
  2311. #include "endian.h"
  2312. #include "helpers.h"
  2313. #ifndef NO_LOG
  2314. static void vlogger(const char *message, va_list args)
  2315. {
  2316. FILE *log;
  2317. #ifdef _NTSERVICE
  2318. if (!IsNTService && logstdout) log = stdout;
  2319. #else
  2320. if (logstdout) log = stdout;
  2321. #endif
  2322. else
  2323. {
  2324. if (fn_log == NULL) return;
  2325. #ifndef _WIN32
  2326. if (!strcmp(fn_log, "syslog"))
  2327. {
  2328. openlog("vlmcsd", LOG_CONS | LOG_PID, LOG_USER);
  2329. ////PORTABILITY: vsyslog is not in Posix but virtually all Unixes have it
  2330. vsyslog(LOG_INFO, message, args);
  2331. closelog();
  2332. return;
  2333. }
  2334. #endif // _WIN32
  2335. log = fopen(fn_log, "a");
  2336. if ( !log ) return;
  2337. }
  2338. time_t now = time(0);
  2339. #ifdef USE_THREADS
  2340. char mbstr[2048];
  2341. #else
  2342. char mbstr[24];
  2343. #endif
  2344. strftime(mbstr, sizeof(mbstr), "%Y-%m-%d %X", localtime(&now));
  2345. #ifndef USE_THREADS
  2346. fprintf(log, "%s: ", mbstr);
  2347. vfprintf(log, message, args);
  2348. fflush(log);
  2349. #else // USE_THREADS
  2350. // We write everything to a string before we really log inside the critical section
  2351. // so formatting the output can be concurrent
  2352. strcat(mbstr, ": ");
  2353. int len = strlen(mbstr);
  2354. vsnprintf(mbstr + len, sizeof(mbstr) - len, message, args);
  2355. lock_mutex(&logmutex);
  2356. fputs(mbstr, log);
  2357. fflush(log);
  2358. unlock_mutex(&logmutex);
  2359. #endif // USE_THREADS
  2360. if (log != stdout) fclose(log);
  2361. }
  2362. // Always sends to log output
  2363. int logger(const char *const fmt, ...)
  2364. {
  2365. va_list args;
  2366. va_start(args, fmt);
  2367. vlogger(fmt, args);
  2368. va_end(args);
  2369. return 0;
  2370. }
  2371. #endif //NO_LOG
  2372. // Output to stderr if it is available or to log otherwise (e.g. if running as daemon/service)
  2373. void printerrorf(const char *const fmt, ...)
  2374. {
  2375. va_list arglist;
  2376. va_start(arglist, fmt);
  2377. #ifndef NO_LOG
  2378. #ifdef _NTSERVICE
  2379. if (InetdMode || IsNTService)
  2380. #else // !_NTSERVICE
  2381. if (InetdMode)
  2382. #endif // NTSERVIICE
  2383. vlogger(fmt, arglist);
  2384. else
  2385. #endif //NO_LOG
  2386. {
  2387. vfprintf(stderr, fmt, arglist);
  2388. fflush(stderr);
  2389. }
  2390. va_end(arglist);
  2391. }
  2392. // Always output to stderr
  2393. int errorout(const char* fmt, ...)
  2394. {
  2395. va_list args;
  2396. va_start(args, fmt);
  2397. int i = vfprintf(stderr, fmt, args);
  2398. va_end(args);
  2399. fflush(stderr);
  2400. return i;
  2401. }
  2402. static const char *LicenseStatusText[] =
  2403. {
  2404. "Unlicensed", "Licensed", "OOB grace", "OOT grace", "Non-Genuine", "Notification", "Extended grace"
  2405. };
  2406. void uuid2StringLE(const GUID *const guid, char *const string)
  2407. {
  2408. sprintf(string,
  2409. #ifdef _WIN32
  2410. "%08x-%04x-%04x-%04x-%012I64x",
  2411. #else
  2412. "%08x-%04x-%04x-%04x-%012llx",
  2413. #endif
  2414. (unsigned int)LE32( guid->Data1 ),
  2415. (unsigned int)LE16( guid->Data2 ),
  2416. (unsigned int)LE16( guid->Data3 ),
  2417. (unsigned int)BE16( *(uint16_t*)guid->Data4 ),
  2418. (unsigned long long)BE64(*(uint64_t*)(guid->Data4)) & 0xffffffffffffLL
  2419. );
  2420. }
  2421. void logRequestVerbose(const REQUEST *const Request, const PRINTFUNC p)
  2422. {
  2423. char guidBuffer[GUID_STRING_LENGTH + 1];
  2424. char WorkstationBuffer[3 * WORKSTATION_NAME_BUFFER];
  2425. const char *productName;
  2426. ProdListIndex_t index;
  2427. p("Protocol version : %u.%u\n", LE16(Request->MajorVer), LE16(Request->MinorVer));
  2428. p("Client is a virtual machine : %s\n", LE32(Request->VMInfo) ? "Yes" : "No");
  2429. p("Licensing status : %u (%s)\n", (uint32_t)LE32(Request->LicenseStatus), LE32(Request->LicenseStatus) < _countof(LicenseStatusText) ? LicenseStatusText[LE32(Request->LicenseStatus)] : "Unknown");
  2430. p("Remaining time (0 = forever) : %i minutes\n", (uint32_t)LE32(Request->BindingExpiration));
  2431. uuid2StringLE(&Request->AppID, guidBuffer);
  2432. productName = getProductNameLE(&Request->AppID, AppList, &index);
  2433. p("Application ID : %s (%s)\n", guidBuffer, productName);
  2434. uuid2StringLE(&Request->ActID, guidBuffer);
  2435. #ifndef NO_EXTENDED_PRODUCT_LIST
  2436. productName = getProductNameLE(&Request->ActID, ExtendedProductList, &index);
  2437. #else
  2438. productName = "Unknown";
  2439. #endif
  2440. p("Activation ID (Product) : %s (%s)\n", guidBuffer, productName);
  2441. uuid2StringLE(&Request->KMSID, guidBuffer);
  2442. #ifndef NO_BASIC_PRODUCT_LIST
  2443. productName = getProductNameLE(&Request->KMSID, ProductList, &index);
  2444. #else
  2445. productName = "Unknown";
  2446. #endif
  2447. p("Key Management Service ID : %s (%s)\n", guidBuffer, productName);
  2448. uuid2StringLE(&Request->CMID, guidBuffer);
  2449. p("Client machine ID : %s\n", guidBuffer);
  2450. uuid2StringLE(&Request->CMID_prev, guidBuffer);
  2451. p("Previous client machine ID : %s\n", guidBuffer);
  2452. char mbstr[64];
  2453. time_t st;
  2454. st = fileTimeToUnixTime(&Request->ClientTime);
  2455. strftime(mbstr, sizeof(mbstr), "%Y-%m-%d %X", gmtime(&st));
  2456. p("Client request timestamp (UTC) : %s\n", mbstr);
  2457. ucs2_to_utf8(Request->WorkstationName, WorkstationBuffer, WORKSTATION_NAME_BUFFER, sizeof(WorkstationBuffer));
  2458. p("Workstation name : %s\n", WorkstationBuffer);
  2459. p("N count policy (minimum clients): %u\n", (uint32_t)LE32(Request->N_Policy));
  2460. }
  2461. void logResponseVerbose(const char *const ePID, const BYTE *const hwid, const RESPONSE *const response, const PRINTFUNC p)
  2462. {
  2463. char guidBuffer[GUID_STRING_LENGTH + 1];
  2464. //SYSTEMTIME st;
  2465. p("Protocol version : %u.%u\n", (uint32_t)LE16(response->MajorVer), (uint32_t)LE16(response->MinorVer));
  2466. p("KMS host extended PID : %s\n", ePID);
  2467. if (LE16(response->MajorVer) > 5)
  2468. # ifndef _WIN32
  2469. p("KMS host Hardware ID : %016llX\n", (unsigned long long)BE64(*(uint64_t*)hwid));
  2470. # else // _WIN32
  2471. p("KMS host Hardware ID : %016I64X\n", (unsigned long long)BE64(*(uint64_t*)hwid));
  2472. # endif // WIN32
  2473. uuid2StringLE(&response->CMID, guidBuffer);
  2474. p("Client machine ID : %s\n", guidBuffer);
  2475. char mbstr[64];
  2476. time_t st;
  2477. st = fileTimeToUnixTime(&response->ClientTime);
  2478. strftime(mbstr, sizeof(mbstr), "%Y-%m-%d %X", gmtime(&st));
  2479. p("Client request timestamp (UTC) : %s\n", mbstr);
  2480. p("KMS host current active clients : %u\n", (uint32_t)LE32(response->Count));
  2481. p("Renewal interval policy : %u\n", (uint32_t)LE32(response->VLRenewalInterval));
  2482. p("Activation interval policy : %u\n", (uint32_t)LE32(response->VLActivationInterval));
  2483. }
  2484. #ifndef CONFIG
  2485. #define CONFIG "config.h"
  2486. #endif // CONFIG
  2487. #include CONFIG
  2488. #include "shared_globals.h"
  2489. int global_argc, multi_argc = 0;
  2490. CARGV global_argv, multi_argv = NULL;
  2491. const char *const Version = VERSION;
  2492. DWORD VLActivationInterval = 60 * 2; // 2 hours
  2493. DWORD VLRenewalInterval = 60 * 24 * 7; // 7 days
  2494. int_fast8_t DisconnectImmediately = FALSE;
  2495. const char *const cIPv4 = "IPv4";
  2496. const char *const cIPv6 = "IPv6";
  2497. #ifndef USE_MSRPC
  2498. int_fast8_t UseMultiplexedRpc = TRUE;
  2499. int_fast8_t UseRpcNDR64 = TRUE;
  2500. int_fast8_t UseRpcBTFN = TRUE;
  2501. #endif // USE_MSRPC
  2502. #ifndef NO_SOCKETS
  2503. const char *defaultport = "1688";
  2504. #endif // NO_SOCKETS
  2505. KmsResponseParam_t KmsResponseParameters[MAX_KMSAPPS];
  2506. #if !defined(NO_SOCKETS) && !defined(NO_SIGHUP) && !defined(_WIN32)
  2507. int_fast8_t IsRestarted = FALSE;
  2508. #endif // !defined(NO_SOCKETS) && !defined(NO_SIGHUP) && !defined(_WIN32)
  2509. #if !defined(NO_TIMEOUT) && !__minix__
  2510. DWORD ServerTimeout = 30;
  2511. #endif // !defined(NO_TIMEOUT) && !__minix__
  2512. #if !defined(NO_LIMIT) && !defined (NO_SOCKETS) && !__minix__
  2513. #ifdef USE_MSRPC
  2514. uint32_t MaxTasks = RPC_C_LISTEN_MAX_CALLS_DEFAULT;
  2515. #else // !USE_MSRPC
  2516. uint32_t MaxTasks = SEM_VALUE_MAX;
  2517. #endif // !USE_MSRPC
  2518. #endif // !defined(NO_LIMIT) && !defined (NO_SOCKETS) && !__minix__
  2519. #ifndef NO_LOG
  2520. char *fn_log = NULL;
  2521. int_fast8_t logstdout = 0;
  2522. #ifndef NO_VERBOSE_LOG
  2523. int_fast8_t logverbose = 0;
  2524. #endif // NO_VERBOSE_LOG
  2525. #endif // NO_LOG
  2526. #ifndef NO_SOCKETS
  2527. int_fast8_t nodaemon = 0;
  2528. int_fast8_t InetdMode = 0;
  2529. #else
  2530. int_fast8_t nodaemon = 1;
  2531. int_fast8_t InetdMode = 1;
  2532. #endif
  2533. #ifndef NO_RANDOM_EPID
  2534. int_fast8_t RandomizationLevel = 1;
  2535. uint16_t Lcid = 0;
  2536. #endif
  2537. #ifndef NO_SOCKETS
  2538. SOCKET *SocketList;
  2539. int numsockets = 0;
  2540. #if !defined(NO_LIMIT) && !__minix__
  2541. #ifndef _WIN32 // Posix
  2542. sem_t *Semaphore;
  2543. #else // _WIN32
  2544. HANDLE Semaphore;
  2545. #endif // _WIN32
  2546. #endif // !defined(NO_LIMIT) && !__minix__
  2547. #endif // NO_SOCKETS
  2548. #ifdef _NTSERVICE
  2549. int_fast8_t IsNTService = TRUE;
  2550. int_fast8_t ServiceShutdown = FALSE;
  2551. #endif // _NTSERVICE
  2552. #ifndef NO_LOG
  2553. #ifdef USE_THREADS
  2554. #if !defined(_WIN32) && !defined(__CYGWIN__)
  2555. pthread_mutex_t logmutex = PTHREAD_MUTEX_INITIALIZER;
  2556. #else
  2557. CRITICAL_SECTION logmutex;
  2558. #endif // !defined(_WIN32) && !defined(__CYGWIN__)
  2559. #endif // USE_THREADS
  2560. #endif // NO_LOG
  2561. /*
  2562. * Helper functions used by other modules
  2563. */
  2564. #ifndef CONFIG
  2565. #define CONFIG "config.h"
  2566. #endif // CONFIG
  2567. #include CONFIG
  2568. #ifndef _WIN32
  2569. #include <errno.h>
  2570. #endif // _WIN32
  2571. #include <getopt.h>
  2572. #include <string.h>
  2573. #include <stdlib.h>
  2574. #include <ctype.h>
  2575. #include "helpers.h"
  2576. #include "output.h"
  2577. #include "endian.h"
  2578. #include "shared_globals.h"
  2579. /*
  2580. * UCS2 <-> UTF-8 functions
  2581. * All functions use little endian UCS2 since we only need it to communicate with Windows via RPC
  2582. */
  2583. // Convert one character from UTF-8 to UCS2
  2584. // Returns 0xffff, if utf-8 evaluates to > 0xfffe (outside basic multilingual pane)
  2585. WCHAR utf8_to_ucs2_char (const unsigned char *input, const unsigned char **end_ptr)
  2586. {
  2587. *end_ptr = input;
  2588. if (input[0] == 0)
  2589. return ~0;
  2590. if (input[0] < 0x80) {
  2591. *end_ptr = input + 1;
  2592. return LE16(input[0]);
  2593. }
  2594. if ((input[0] & 0xE0) == 0xE0) {
  2595. if (input[1] == 0 || input[2] == 0)
  2596. return ~0;
  2597. *end_ptr = input + 3;
  2598. return
  2599. LE16((input[0] & 0x0F)<<12 |
  2600. (input[1] & 0x3F)<<6 |
  2601. (input[2] & 0x3F));
  2602. }
  2603. if ((input[0] & 0xC0) == 0xC0) {
  2604. if (input[1] == 0)
  2605. return ~0;
  2606. *end_ptr = input + 2;
  2607. return
  2608. LE16((input[0] & 0x1F)<<6 |
  2609. (input[1] & 0x3F));
  2610. }
  2611. return ~0;
  2612. }
  2613. // Convert one character from UCS2 to UTF-8
  2614. // Returns length of UTF-8 char (1, 2 or 3) or -1 on error (UTF-16 outside UCS2)
  2615. // char *utf8 must be large enough to hold 3 bytes
  2616. int ucs2_to_utf8_char (const WCHAR ucs2_le, char *utf8)
  2617. {
  2618. const WCHAR ucs2 = LE16(ucs2_le);
  2619. if (ucs2 < 0x80) {
  2620. utf8[0] = ucs2;
  2621. utf8[1] = '\0';
  2622. return 1;
  2623. }
  2624. if (ucs2 >= 0x80 && ucs2 < 0x800) {
  2625. utf8[0] = (ucs2 >> 6) | 0xC0;
  2626. utf8[1] = (ucs2 & 0x3F) | 0x80;
  2627. utf8[2] = '\0';
  2628. return 2;
  2629. }
  2630. if (ucs2 >= 0x800 && ucs2 < 0xFFFF) {
  2631. if (ucs2 >= 0xD800 && ucs2 <= 0xDFFF) {
  2632. /* Ill-formed (UTF-16 ouside of BMP) */
  2633. return -1;
  2634. }
  2635. utf8[0] = ((ucs2 >> 12) ) | 0xE0;
  2636. utf8[1] = ((ucs2 >> 6 ) & 0x3F) | 0x80;
  2637. utf8[2] = ((ucs2 ) & 0x3F) | 0x80;
  2638. utf8[3] = '\0';
  2639. return 3;
  2640. }
  2641. return -1;
  2642. }
  2643. // Converts UTF8 to UCS2. Returns size in bytes of the converted string or -1 on error
  2644. size_t utf8_to_ucs2(WCHAR* const ucs2_le, const char* const utf8, const size_t maxucs2, const size_t maxutf8)
  2645. {
  2646. const unsigned char* current_utf8 = (unsigned char*)utf8;
  2647. WCHAR* current_ucs2_le = ucs2_le;
  2648. for (; *current_utf8; current_ucs2_le++)
  2649. {
  2650. size_t size = (char*)current_utf8 - utf8;
  2651. if (size >= maxutf8) return (size_t)-1;
  2652. if (((*current_utf8 & 0xc0) == 0xc0) && (size >= maxutf8 - 1)) return (size_t)-1;
  2653. if (((*current_utf8 & 0xe0) == 0xe0) && (size >= maxutf8 - 2)) return (size_t)-1;
  2654. if (current_ucs2_le - ucs2_le >= (intptr_t)maxucs2 - 1) return (size_t)-1;
  2655. *current_ucs2_le = utf8_to_ucs2_char(current_utf8, &current_utf8);
  2656. current_ucs2_le[1] = 0;
  2657. if (*current_ucs2_le == (WCHAR)-1) return (size_t)-1;
  2658. }
  2659. return current_ucs2_le - ucs2_le;
  2660. }
  2661. // Converts UCS2 to UTF-8. Return TRUE or FALSE
  2662. BOOL ucs2_to_utf8(const WCHAR* const ucs2_le, char* utf8, size_t maxucs2, size_t maxutf8)
  2663. {
  2664. char utf8_char[4];
  2665. const WCHAR* current_ucs2 = ucs2_le;
  2666. unsigned int index_utf8 = 0;
  2667. for(*utf8 = 0; *current_ucs2; current_ucs2++)
  2668. {
  2669. if (current_ucs2 - ucs2_le > (intptr_t)maxucs2) return FALSE;
  2670. int len = ucs2_to_utf8_char(*current_ucs2, utf8_char);
  2671. if (index_utf8 + len > maxutf8) return FALSE;
  2672. strncat(utf8, utf8_char, len);
  2673. index_utf8+=len;
  2674. }
  2675. return TRUE;
  2676. }
  2677. /* End of UTF-8 <-> UCS2 conversion */
  2678. // Checks, whether a string is a valid integer number between min and max. Returns TRUE or FALSE. Puts int value in *value
  2679. BOOL stringToInt(const char *const szValue, const unsigned int min, const unsigned int max, unsigned int *const value)
  2680. {
  2681. char *nextchar;
  2682. errno = 0;
  2683. long long result = strtoll(szValue, &nextchar, 10);
  2684. if (errno || result < (long long)min || result > (long long)max || *nextchar)
  2685. {
  2686. return FALSE;
  2687. }
  2688. *value = (unsigned int)result;
  2689. return TRUE;
  2690. }
  2691. //Converts a String Guid to a host binary guid in host endianess
  2692. int_fast8_t string2Uuid(const char *const restrict input, GUID *const restrict guid)
  2693. {
  2694. int i;
  2695. if (strlen(input) < GUID_STRING_LENGTH) return FALSE;
  2696. if (input[8] != '-' || input[13] != '-' || input[18] != '-' || input[23] != '-') return FALSE;
  2697. for (i = 0; i < GUID_STRING_LENGTH; i++)
  2698. {
  2699. if (i == 8 || i == 13 || i == 18 || i == 23) continue;
  2700. const char c = toupper((int)input[i]);
  2701. if (c < '0' || c > 'F' || (c > '9' && c < 'A')) return FALSE;
  2702. }
  2703. char inputCopy[GUID_STRING_LENGTH + 1];
  2704. strncpy(inputCopy, input, GUID_STRING_LENGTH + 1);
  2705. inputCopy[8] = inputCopy[13] = inputCopy[18] = 0;
  2706. hex2bin((BYTE*)&guid->Data1, inputCopy, 8);
  2707. hex2bin((BYTE*)&guid->Data2, inputCopy + 9, 4);
  2708. hex2bin((BYTE*)&guid->Data3, inputCopy + 14, 4);
  2709. hex2bin(guid->Data4, input + 19, 16);
  2710. guid->Data1 = BE32(guid->Data1);
  2711. guid->Data2 = BE16(guid->Data2);
  2712. guid->Data3 = BE16(guid->Data3);
  2713. return TRUE;
  2714. }
  2715. // convert GUID to little-endian
  2716. void LEGUID(GUID *const restrict out, const GUID* const restrict in)
  2717. {
  2718. #if __BYTE_ORDER != __LITTLE_ENDIAN
  2719. out->Data1 = LE32(in->Data1);
  2720. out->Data2 = LE16(in->Data2);
  2721. out->Data3 = LE16(in->Data3);
  2722. memcpy(out->Data4, in->Data4, sizeof(out->Data4));
  2723. #else
  2724. memcpy(out, in, sizeof(GUID));
  2725. #endif
  2726. }
  2727. //Checks a command line argument if it is numeric and between min and max. Returns the numeric value or exits on error
  2728. __pure unsigned int getOptionArgumentInt(const char o, const unsigned int min, const unsigned int max)
  2729. {
  2730. unsigned int result;
  2731. if (!stringToInt(optarg, min, max, &result))
  2732. {
  2733. printerrorf("Fatal: Option \"-%c\" must be numeric between %u and %u.\n", o, min, max);
  2734. exit(!0);
  2735. }
  2736. return result;
  2737. }
  2738. // Resets getopt() to start parsing from the beginning
  2739. void optReset(void)
  2740. {
  2741. #if __minix__ || defined(__BSD__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
  2742. optind = 1;
  2743. optreset = 1; // Makes newer BSD getopt happy
  2744. #elif defined(__UCLIBC__) // uClibc headers also define __GLIBC__ so be careful here
  2745. optind = 0; // uClibc seeks compatibility with GLIBC
  2746. #elif defined(__GLIBC__)
  2747. optind = 0; // Makes GLIBC getopt happy
  2748. #else // Standard for most systems
  2749. optind = 1;
  2750. #endif
  2751. }
  2752. #if defined(_WIN32) || defined(USE_MSRPC)
  2753. // Returns a static message buffer containing text for a given Win32 error. Not thread safe (same as strerror)
  2754. char* win_strerror(const int message)
  2755. {
  2756. #define STRERROR_BUFFER_SIZE 256
  2757. static char buffer[STRERROR_BUFFER_SIZE];
  2758. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, message, 0, buffer, STRERROR_BUFFER_SIZE, NULL);
  2759. return buffer;
  2760. }
  2761. #endif // defined(_WIN32) || defined(USE_MSRPC)
  2762. /*
  2763. * parses an address in the form host:[port] in addr
  2764. * returns host and port in seperate strings
  2765. */
  2766. void parseAddress(char *const addr, char** szHost, char** szPort)
  2767. {
  2768. *szHost = addr;
  2769. # ifndef NO_SOCKETS
  2770. *szPort = (char*)defaultport;
  2771. # else // NO_SOCKETS
  2772. *szPort = "1688";
  2773. # endif // NO_SOCKETS
  2774. char *lastcolon = strrchr(addr, ':');
  2775. char *firstcolon = strchr(addr, ':');
  2776. char *closingbracket = strrchr(addr, ']');
  2777. if (*addr == '[' && closingbracket) //Address in brackets
  2778. {
  2779. *closingbracket = 0;
  2780. (*szHost)++;
  2781. if (closingbracket[1] == ':')
  2782. *szPort = closingbracket + 2;
  2783. }
  2784. else if (firstcolon && firstcolon == lastcolon) //IPv4 address or hostname with port
  2785. {
  2786. *firstcolon = 0;
  2787. *szPort = firstcolon + 1;
  2788. }
  2789. }
  2790. // Initialize random generator (needs to be done in each thread)
  2791. void randomNumberInit()
  2792. {
  2793. struct timeval tv;
  2794. gettimeofday(&tv, NULL);
  2795. srand((unsigned int)(tv.tv_sec ^ tv.tv_usec));
  2796. }
  2797. // We always exit immediately if any OOM condition occurs
  2798. __noreturn void OutOfMemory(void)
  2799. {
  2800. errorout("Fatal: Out of memory");
  2801. exit(!0);
  2802. }
  2803. void* vlmcsd_malloc(size_t len)
  2804. {
  2805. void* buf = malloc(len);
  2806. if (!buf) OutOfMemory();
  2807. return buf;
  2808. }
  2809. /*
  2810. * Converts hex digits to bytes in big-endian order.
  2811. * Ignores any non-hex characters
  2812. */
  2813. void hex2bin(BYTE *const bin, const char *hex, const size_t maxbin)
  2814. {
  2815. static const char *const hexdigits = "0123456789ABCDEF";
  2816. char* nextchar;
  2817. size_t i;
  2818. for (i = 0; (i < 16) && utf8_to_ucs2_char((const unsigned char*)hex, (const unsigned char**)&nextchar) != (WCHAR)-1; hex = nextchar)
  2819. {
  2820. const char* pos = strchr(hexdigits, toupper((int)*hex));
  2821. if (!pos) continue;
  2822. if (!(i & 1)) bin[i >> 1] = 0;
  2823. bin[i >> 1] |= (char)(pos - hexdigits);
  2824. if (!(i & 1)) bin[i >> 1] <<= 4;
  2825. i++;
  2826. if (i >> 1 > maxbin) break;
  2827. }
  2828. }
  2829. __pure BOOL getArgumentBool(int_fast8_t *result, const char *const argument)
  2830. {
  2831. if (
  2832. !strncasecmp(argument, "true", 4) ||
  2833. !strncasecmp(argument, "on", 2) ||
  2834. !strncasecmp(argument, "yes", 3) ||
  2835. !strncasecmp(argument, "1", 1)
  2836. )
  2837. {
  2838. *result = TRUE;
  2839. return TRUE;
  2840. }
  2841. else if (
  2842. !strncasecmp(argument, "false", 5) ||
  2843. !strncasecmp(argument, "off", 3) ||
  2844. !strncasecmp(argument, "no", 2) ||
  2845. !strncasecmp(argument, "0", 1)
  2846. )
  2847. {
  2848. *result = FALSE;
  2849. return TRUE;
  2850. }
  2851. return FALSE;
  2852. }
  2853. #ifndef CONFIG
  2854. #define CONFIG "config.h"
  2855. #endif // CONFIG
  2856. #include CONFIG
  2857. #ifndef USE_MSRPC
  2858. #ifndef _GNU_SOURCE
  2859. #define _GNU_SOURCE
  2860. #endif
  2861. #include <string.h>
  2862. #ifndef _WIN32
  2863. #include <signal.h>
  2864. #include <unistd.h>
  2865. #include <fcntl.h>
  2866. #include <errno.h>
  2867. #include <netinet/in.h>
  2868. #endif // WIN32
  2869. #include "network.h"
  2870. #include "endian.h"
  2871. #include "output.h"
  2872. #include "helpers.h"
  2873. #include "shared_globals.h"
  2874. #include "rpc.h"
  2875. #ifndef _WIN32
  2876. typedef ssize_t (*sendrecv_t)(int, void*, size_t, int);
  2877. #else
  2878. typedef int (WINAPI *sendrecv_t)(SOCKET, void*, int, int);
  2879. #endif
  2880. // Send or receive a fixed number of bytes regardless if received in one or more chunks
  2881. int_fast8_t sendrecv(SOCKET sock, BYTE *data, int len, int_fast8_t do_send)
  2882. {
  2883. int n;
  2884. sendrecv_t f = do_send
  2885. ? (sendrecv_t) send
  2886. : (sendrecv_t) recv;
  2887. do
  2888. {
  2889. n = f(sock, data, len, 0);
  2890. }
  2891. while (
  2892. ( n < 0 && socket_errno == VLMCSD_EINTR ) || ( n > 0 && ( data += n, (len -= n) > 0 ) ));
  2893. return ! len;
  2894. }
  2895. static int_fast8_t ip2str(char *restrict result, const size_t resultLength, const struct sockaddr *const restrict socketAddress, const socklen_t socketLength)
  2896. {
  2897. static const char *const fIPv4 = "%s:%s";
  2898. static const char *const fIPv6 = "[%s]:%s";
  2899. char ipAddress[64], portNumber[8];
  2900. if (getnameinfo
  2901. (
  2902. socketAddress,
  2903. socketLength,
  2904. ipAddress,
  2905. sizeof(ipAddress),
  2906. portNumber,
  2907. sizeof(portNumber),
  2908. NI_NUMERICHOST | NI_NUMERICSERV
  2909. ))
  2910. {
  2911. return FALSE;
  2912. }
  2913. if ((unsigned int)snprintf(result, resultLength, socketAddress->sa_family == AF_INET6 ? fIPv6 : fIPv4, ipAddress, portNumber) > resultLength) return FALSE;
  2914. return TRUE;
  2915. }
  2916. static int_fast8_t getSocketList(struct addrinfo **saList, const char *const addr, const int flags, const int AddressFamily)
  2917. {
  2918. int status;
  2919. char *szHost, *szPort;
  2920. size_t len = strlen(addr) + 1;
  2921. // Don't alloca too much
  2922. if (len > 264) return FALSE;
  2923. char *addrcopy = (char*)alloca(len);
  2924. memcpy(addrcopy, addr, len);
  2925. parseAddress(addrcopy, &szHost, &szPort);
  2926. struct addrinfo hints;
  2927. memset(&hints, 0, sizeof(struct addrinfo));
  2928. hints.ai_family = AddressFamily;
  2929. hints.ai_socktype = SOCK_STREAM;
  2930. hints.ai_protocol = IPPROTO_TCP;
  2931. hints.ai_flags = flags;
  2932. if ((status = getaddrinfo(szHost, szPort, &hints, saList)))
  2933. {
  2934. printerrorf("Warning: %s: %s\n", addr, gai_strerror(status));
  2935. return FALSE;
  2936. }
  2937. return TRUE;
  2938. }
  2939. static int_fast8_t setBlockingEnabled(SOCKET fd, int_fast8_t blocking)
  2940. {
  2941. if (fd == INVALID_SOCKET) return FALSE;
  2942. #ifdef _WIN32
  2943. unsigned long mode = blocking ? 0 : 1;
  2944. return (ioctlsocket(fd, FIONBIO, &mode) == 0) ? TRUE : FALSE;
  2945. #else // POSIX
  2946. int flags = fcntl(fd, F_GETFL, 0);
  2947. if (flags < 0) return FALSE;
  2948. flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
  2949. return (fcntl(fd, F_SETFL, flags) == 0) ? TRUE : FALSE;
  2950. #endif // POSIX
  2951. }
  2952. int_fast8_t isDisconnected(const SOCKET s)
  2953. {
  2954. char buffer[1];
  2955. if (!setBlockingEnabled(s, FALSE)) return TRUE;
  2956. int n = recv(s, buffer, 1, MSG_PEEK);
  2957. if (!setBlockingEnabled(s, TRUE)) return TRUE;
  2958. if (n == 0) return TRUE;
  2959. return FALSE;
  2960. }
  2961. // Connect to TCP address addr (e.g. "kms.example.com:1688") and return an
  2962. // open socket for the connection if successful or INVALID_SOCKET otherwise
  2963. SOCKET connectToAddress(const char *const addr, const int AddressFamily, int_fast8_t showHostName)
  2964. {
  2965. struct addrinfo *saList, *sa;
  2966. SOCKET s = INVALID_SOCKET;
  2967. char szAddr[128];
  2968. if (!getSocketList(&saList, addr, 0, AddressFamily)) return INVALID_SOCKET;
  2969. for (sa = saList; sa; sa = sa->ai_next)
  2970. {
  2971. // struct sockaddr_in* addr4 = (struct sockaddr_in*)sa->ai_addr;
  2972. // struct sockaddr_in6* addr6 = (struct sockaddr_in6*)sa->ai_addr;
  2973. if (ip2str(szAddr, sizeof(szAddr), sa->ai_addr, sa->ai_addrlen))
  2974. {
  2975. if (showHostName)
  2976. printf("Connecting to %s (%s) ... ", addr, szAddr);
  2977. else
  2978. printf("Connecting to %s ... ", szAddr);
  2979. fflush(stdout);
  2980. }
  2981. s = socket(sa->ai_family, SOCK_STREAM, IPPROTO_TCP);
  2982. # if !defined(NO_TIMEOUT) && !__minix__
  2983. # ifndef _WIN32 // Standard Posix timeout structure
  2984. struct timeval to;
  2985. to.tv_sec = 10;
  2986. to.tv_usec = 0;
  2987. # else // Windows requires a DWORD with milliseconds
  2988. DWORD to = 10000;
  2989. # endif // _WIN32
  2990. setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (sockopt_t)&to, sizeof(to));
  2991. setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (sockopt_t)&to, sizeof(to));
  2992. # endif // !defined(NO_TIMEOUT) && !__minix__
  2993. if (!connect(s, sa->ai_addr, sa->ai_addrlen))
  2994. {
  2995. printf("successful\n");
  2996. break;
  2997. }
  2998. errorout("%s\n", socket_errno == VLMCSD_EINPROGRESS ? "Timed out" : vlmcsd_strerror(socket_errno));
  2999. socketclose(s);
  3000. s = INVALID_SOCKET;
  3001. }
  3002. freeaddrinfo(saList);
  3003. return s;
  3004. }
  3005. #ifndef NO_SOCKETS
  3006. // Create a Listening socket for addrinfo sa and return socket s
  3007. // szHost and szPort are for logging only
  3008. static int listenOnAddress(const struct addrinfo *const ai, SOCKET *s)
  3009. {
  3010. int error;
  3011. char ipstr[64];
  3012. ip2str(ipstr, sizeof(ipstr), ai->ai_addr, ai->ai_addrlen);
  3013. //*s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  3014. *s = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
  3015. if (*s == INVALID_SOCKET)
  3016. {
  3017. error = socket_errno;
  3018. printerrorf("Warning: %s error. %s\n", ai->ai_family == AF_INET6 ? cIPv6 : cIPv4, vlmcsd_strerror(error));
  3019. return error;
  3020. }
  3021. # if !defined(_WIN32) && !defined(NO_SIGHUP)
  3022. int flags = fcntl(*s, F_GETFD, 0);
  3023. if (flags != -1)
  3024. {
  3025. flags |= FD_CLOEXEC;
  3026. fcntl(*s, F_SETFD, flags);
  3027. }
  3028. # ifdef _PEDANTIC
  3029. else
  3030. {
  3031. printerrorf("Warning: Could not set FD_CLOEXEC flag on %s: %s\n", ipstr, vlmcsd_strerror(errno));
  3032. }
  3033. # endif // _PEDANTIC
  3034. # endif // !defined(_WIN32) && !defined(NO_SIGHUP)
  3035. BOOL socketOption = TRUE;
  3036. // fix for lame tomato toolchain
  3037. # ifndef IPV6_V6ONLY
  3038. # ifdef __linux__
  3039. # define IPV6_V6ONLY (26)
  3040. # endif // __linux__
  3041. # endif // IPV6_V6ONLY
  3042. # ifdef IPV6_V6ONLY
  3043. if (ai->ai_family == AF_INET6) setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY, (sockopt_t)&socketOption, sizeof(socketOption));
  3044. # endif
  3045. # ifndef _WIN32
  3046. setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, (sockopt_t)&socketOption, sizeof(socketOption));
  3047. # endif
  3048. if (bind(*s, ai->ai_addr, ai->ai_addrlen) || listen(*s, SOMAXCONN))
  3049. {
  3050. error = socket_errno;
  3051. printerrorf("Warning: %s: %s\n", ipstr, vlmcsd_strerror(error));
  3052. socketclose(*s);
  3053. return error;
  3054. }
  3055. # ifndef NO_LOG
  3056. logger("Listening on %s\n", ipstr);
  3057. # endif
  3058. return 0;
  3059. }
  3060. // Adds a listening socket for an address string,
  3061. // e.g. 127.0.0.1:1688 or [2001:db8:dead:beef::1]:1688
  3062. BOOL addListeningSocket(const char *const addr)
  3063. {
  3064. struct addrinfo *aiList, *ai;
  3065. int result = FALSE;
  3066. SOCKET *s = SocketList + numsockets;
  3067. if (getSocketList(&aiList, addr, AI_PASSIVE | AI_NUMERICHOST, AF_UNSPEC))
  3068. {
  3069. for (ai = aiList; ai; ai = ai->ai_next)
  3070. {
  3071. // struct sockaddr_in* addr4 = (struct sockaddr_in*)sa->ai_addr;
  3072. // struct sockaddr_in6* addr6 = (struct sockaddr_in6*)sa->ai_addr;
  3073. if (numsockets >= FD_SETSIZE)
  3074. {
  3075. #ifdef _PEDANTIC // Do not report this error in normal builds to keep file size low
  3076. printerrorf("Warning: Cannot listen on %s. Your OS only supports %u listening sockets in an FD_SET.\n", addr, FD_SETSIZE);
  3077. #endif
  3078. break;
  3079. }
  3080. if (!listenOnAddress(ai, s))
  3081. {
  3082. numsockets++;
  3083. result = TRUE;
  3084. }
  3085. }
  3086. freeaddrinfo(aiList);
  3087. }
  3088. return result;
  3089. }
  3090. // Just create some dummy sockets to see if we have a specific protocol (IPv4 or IPv6)
  3091. __pure int_fast8_t checkProtocolStack(const int addressfamily)
  3092. {
  3093. SOCKET s; // = INVALID_SOCKET;
  3094. s = socket(addressfamily, SOCK_STREAM, 0);
  3095. int_fast8_t success = (s != INVALID_SOCKET);
  3096. socketclose(s);
  3097. return success;
  3098. }
  3099. // Build an fd_set of all listening socket then use select to wait for an incoming connection
  3100. static SOCKET network_accept_any()
  3101. {
  3102. fd_set ListeningSocketsList;
  3103. SOCKET maxSocket, sock;
  3104. int i;
  3105. int status;
  3106. FD_ZERO(&ListeningSocketsList);
  3107. maxSocket = 0;
  3108. for (i = 0; i < numsockets; i++)
  3109. {
  3110. FD_SET(SocketList[i], &ListeningSocketsList);
  3111. if (SocketList[i] > maxSocket) maxSocket = SocketList[i];
  3112. }
  3113. status = select(maxSocket + 1, &ListeningSocketsList, NULL, NULL, NULL);
  3114. if (status < 0) return INVALID_SOCKET;
  3115. sock = INVALID_SOCKET;
  3116. for (i = 0; i < numsockets; i++)
  3117. {
  3118. if (FD_ISSET(SocketList[i], &ListeningSocketsList))
  3119. {
  3120. sock = SocketList[i];
  3121. break;
  3122. }
  3123. }
  3124. if (sock == INVALID_SOCKET)
  3125. return INVALID_SOCKET;
  3126. else
  3127. return accept(sock, NULL, NULL);
  3128. }
  3129. void closeAllListeningSockets()
  3130. {
  3131. int i;
  3132. for (i = 0; i < numsockets; i++)
  3133. {
  3134. shutdown(SocketList[i], VLMCSD_SHUT_RDWR);
  3135. socketclose(SocketList[i]);
  3136. }
  3137. }
  3138. #endif // NO_SOCKETS
  3139. static void serveClient(const SOCKET s_client, const DWORD RpcAssocGroup)
  3140. {
  3141. # if !defined(NO_TIMEOUT) && !__minix__
  3142. # ifndef _WIN32 // Standard Posix timeout structure
  3143. struct timeval to;
  3144. to.tv_sec = ServerTimeout;
  3145. to.tv_usec = 0;
  3146. #else // Windows requires a DWORD with milliseconds
  3147. DWORD to = ServerTimeout * 1000;
  3148. # endif // _WIN32
  3149. # if !defined(NO_LOG) && defined(_PEDANTIC)
  3150. int result =
  3151. setsockopt(s_client, SOL_SOCKET, SO_RCVTIMEO, (sockopt_t)&to, sizeof(to)) ||
  3152. setsockopt(s_client, SOL_SOCKET, SO_SNDTIMEO, (sockopt_t)&to, sizeof(to));
  3153. if (result) logger("Warning: Set timeout failed: %s\n", vlmcsd_strerror(socket_errno));
  3154. # else // !(!defined(NO_LOG) && defined(_PEDANTIC))
  3155. setsockopt(s_client, SOL_SOCKET, SO_RCVTIMEO, (sockopt_t)&to, sizeof(to));
  3156. setsockopt(s_client, SOL_SOCKET, SO_SNDTIMEO, (sockopt_t)&to, sizeof(to));
  3157. # endif // !(!defined(NO_LOG) && defined(_PEDANTIC))
  3158. # endif // !defined(NO_TIMEOUT) && !__minix__
  3159. char ipstr[64];
  3160. socklen_t len;
  3161. struct sockaddr_storage addr;
  3162. len = sizeof addr;
  3163. if (getpeername(s_client, (struct sockaddr*)&addr, &len) ||
  3164. !ip2str(ipstr, sizeof(ipstr), (struct sockaddr*)&addr, len))
  3165. {
  3166. # if !defined(NO_LOG) && defined(_PEDANTIC)
  3167. logger("Fatal: Cannot determine client's IP address: %s\n", vlmcsd_strerror(errno));
  3168. # endif // !defined(NO_LOG) && defined(_PEDANTIC)
  3169. socketclose(s_client);
  3170. return;
  3171. }
  3172. # ifndef NO_LOG
  3173. const char *const connection_type = addr.ss_family == AF_INET6 ? cIPv6 : cIPv4;
  3174. static const char *const cAccepted = "accepted";
  3175. static const char *const cClosed = "closed";
  3176. static const char *const fIP = "%s connection %s: %s.\n";
  3177. logger(fIP, connection_type, cAccepted, ipstr);
  3178. #endif // NO_LOG
  3179. rpcServer(s_client, RpcAssocGroup, ipstr);
  3180. # ifndef NO_LOG
  3181. logger(fIP, connection_type, cClosed, ipstr);
  3182. # endif // NO_LOG
  3183. socketclose(s_client);
  3184. }
  3185. #ifndef NO_SOCKETS
  3186. static void post_sem(void)
  3187. {
  3188. #if !defined(NO_LIMIT) && !__minix__
  3189. if (!InetdMode && MaxTasks != SEM_VALUE_MAX)
  3190. {
  3191. semaphore_post(Semaphore);
  3192. }
  3193. #endif // !defined(NO_LIMIT) && !__minix__
  3194. }
  3195. static void wait_sem(void)
  3196. {
  3197. #if !defined(NO_LIMIT) && !__minix__
  3198. if (!InetdMode && MaxTasks != SEM_VALUE_MAX)
  3199. {
  3200. semaphore_wait(Semaphore);
  3201. }
  3202. #endif // !defined(NO_LIMIT) && !__minix__
  3203. }
  3204. #endif // NO_SOCKETS
  3205. #if defined(USE_THREADS) && !defined(NO_SOCKETS)
  3206. #if defined(_WIN32) || defined(__CYGWIN__) // Win32 Threads
  3207. static DWORD WINAPI serveClientThreadProc(PCLDATA clData)
  3208. #else // Posix threads
  3209. static void *serveClientThreadProc (PCLDATA clData)
  3210. #endif // Thread proc is identical in WIN32 and Posix threads
  3211. {
  3212. serveClient(clData->socket, clData->RpcAssocGroup);
  3213. free(clData);
  3214. post_sem();
  3215. return 0;
  3216. }
  3217. #endif // USE_THREADS
  3218. #ifndef NO_SOCKETS
  3219. #if defined(USE_THREADS) && (defined(_WIN32) || defined(__CYGWIN__)) // Windows Threads
  3220. static int serveClientAsyncWinThreads(const PCLDATA thr_CLData)
  3221. {
  3222. wait_sem();
  3223. HANDLE h = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)serveClientThreadProc, thr_CLData, 0, NULL);
  3224. if (h)
  3225. CloseHandle(h);
  3226. else
  3227. {
  3228. socketclose(thr_CLData->socket);
  3229. free(thr_CLData);
  3230. post_sem();
  3231. return GetLastError();
  3232. }
  3233. return NO_ERROR;
  3234. }
  3235. #endif // defined(USE_THREADS) && defined(_WIN32) // Windows Threads
  3236. #if defined(USE_THREADS) && !defined(_WIN32) && !defined(__CYGWIN__) // Posix Threads
  3237. static int ServeClientAsyncPosixThreads(const PCLDATA thr_CLData)
  3238. {
  3239. pthread_t p_thr;
  3240. pthread_attr_t attr;
  3241. wait_sem();
  3242. // Must set detached state to avoid memory leak
  3243. if (pthread_attr_init(&attr) ||
  3244. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) ||
  3245. pthread_create(&p_thr, &attr, (void * (*)(void *))serveClientThreadProc, thr_CLData))
  3246. {
  3247. socketclose(thr_CLData->socket);
  3248. free(thr_CLData);
  3249. post_sem();
  3250. return !0;
  3251. }
  3252. return 0;
  3253. }
  3254. #endif // defined(USE_THREADS) && !defined(_WIN32) // Posix Threads
  3255. #ifndef USE_THREADS // fork() implementation
  3256. static void ChildSignalHandler(const int signal)
  3257. {
  3258. if (signal == SIGHUP) return;
  3259. post_sem();
  3260. #ifndef NO_LOG
  3261. logger("Warning: Child killed/crashed by %s\n", strsignal(signal));
  3262. #endif // NO_LOG
  3263. exit(!0);
  3264. }
  3265. static int ServeClientAsyncFork(const SOCKET s_client, const DWORD RpcAssocGroup)
  3266. {
  3267. int pid;
  3268. wait_sem();
  3269. if ((pid = fork()) < 0)
  3270. {
  3271. return errno;
  3272. }
  3273. else if ( pid )
  3274. {
  3275. // Parent process
  3276. socketclose(s_client);
  3277. return 0;
  3278. }
  3279. else
  3280. {
  3281. // Child process
  3282. // Setup a Child Handler for most common termination signals
  3283. struct sigaction sa;
  3284. sa.sa_flags = 0;
  3285. sa.sa_handler = ChildSignalHandler;
  3286. static int signallist[] = { SIGHUP, SIGINT, SIGTERM, SIGSEGV, SIGILL, SIGFPE, SIGBUS };
  3287. if (!sigemptyset(&sa.sa_mask))
  3288. {
  3289. uint_fast8_t i;
  3290. for (i = 0; i < _countof(signallist); i++)
  3291. {
  3292. sigaction(signallist[i], &sa, NULL);
  3293. }
  3294. }
  3295. serveClient(s_client, RpcAssocGroup);
  3296. post_sem();
  3297. exit(0);
  3298. }
  3299. }
  3300. #endif
  3301. int serveClientAsync(const SOCKET s_client, const DWORD RpcAssocGroup)
  3302. {
  3303. #ifndef USE_THREADS // fork() implementation
  3304. return ServeClientAsyncFork(s_client, RpcAssocGroup);
  3305. #else // threads implementation
  3306. PCLDATA thr_CLData = (PCLDATA)vlmcsd_malloc(sizeof(CLDATA));
  3307. thr_CLData->socket = s_client;
  3308. thr_CLData->RpcAssocGroup = RpcAssocGroup;
  3309. #if defined(_WIN32) || defined (__CYGWIN__) // Windows threads
  3310. return serveClientAsyncWinThreads(thr_CLData);
  3311. #else // Posix Threads
  3312. return ServeClientAsyncPosixThreads(thr_CLData);
  3313. #endif // Posix Threads
  3314. #endif // USE_THREADS
  3315. }
  3316. #endif // NO_SOCKETS
  3317. int runServer()
  3318. {
  3319. DWORD RpcAssocGroup = rand32();
  3320. // If compiled for inetd-only mode just serve the stdin socket
  3321. #ifdef NO_SOCKETS
  3322. serveClient(STDIN_FILENO, RpcAssocGroup);
  3323. return 0;
  3324. #else
  3325. // In inetd mode just handle the stdin socket
  3326. if (InetdMode)
  3327. {
  3328. serveClient(STDIN_FILENO, RpcAssocGroup);
  3329. return 0;
  3330. }
  3331. // Standalone mode
  3332. for (;;)
  3333. {
  3334. int error;
  3335. SOCKET s_client;
  3336. if ( (s_client = network_accept_any()) == INVALID_SOCKET )
  3337. {
  3338. error = socket_errno;
  3339. if (error == VLMCSD_EINTR || error == VLMCSD_ECONNABORTED) continue;
  3340. #ifdef _NTSERVICE
  3341. if (ServiceShutdown) return 0;
  3342. #endif
  3343. #ifndef NO_LOG
  3344. logger("Fatal: %s\n",vlmcsd_strerror(error));
  3345. #endif
  3346. return error;
  3347. }
  3348. RpcAssocGroup++;
  3349. serveClientAsync(s_client, RpcAssocGroup);
  3350. }
  3351. #endif // NO_SOCKETS
  3352. return 0;
  3353. }
  3354. #endif // USE_MSRPC
  3355. #ifndef _DEFAULT_SOURCE
  3356. #define _DEFAULT_SOURCE
  3357. #endif // _DEFAULT_SOURCE
  3358. #ifndef CONFIG
  3359. #define CONFIG "config.h"
  3360. #endif // CONFIG
  3361. #include CONFIG
  3362. #ifndef USE_MSRPC
  3363. #include <stdlib.h>
  3364. #include <stdio.h>
  3365. #include <string.h>
  3366. #include <stdint.h>
  3367. #include <ctype.h>
  3368. #include <time.h>
  3369. #if !defined(_WIN32)
  3370. #include <sys/socket.h>
  3371. #include <netdb.h>
  3372. #endif
  3373. #include "rpc.h"
  3374. #include "output.h"
  3375. #include "crypto.h"
  3376. #include "endian.h"
  3377. #include "helpers.h"
  3378. #include "network.h"
  3379. #include "shared_globals.h"
  3380. /* Forwards */
  3381. static int checkRpcHeader(const RPC_HEADER *const Header, const BYTE desiredPacketType, const PRINTFUNC p);
  3382. /* Data definitions */
  3383. // All GUIDs are defined as BYTE[16] here. No big-endian/little-endian byteswapping required.
  3384. static const BYTE TransferSyntaxNDR32[] = {
  3385. 0x04, 0x5D, 0x88, 0x8A, 0xEB, 0x1C, 0xC9, 0x11, 0x9F, 0xE8, 0x08, 0x00, 0x2B, 0x10, 0x48, 0x60
  3386. };
  3387. static const BYTE InterfaceUuid[] = {
  3388. 0x75, 0x21, 0xc8, 0x51, 0x4e, 0x84, 0x50, 0x47, 0xB0, 0xD8, 0xEC, 0x25, 0x55, 0x55, 0xBC, 0x06
  3389. };
  3390. static const BYTE TransferSyntaxNDR64[] = {
  3391. 0x33, 0x05, 0x71, 0x71, 0xba, 0xbe, 0x37, 0x49, 0x83, 0x19, 0xb5, 0xdb, 0xef, 0x9c, 0xcc, 0x36
  3392. };
  3393. static const BYTE BindTimeFeatureNegotiation[] = {
  3394. 0x2c, 0x1c, 0xb7, 0x6c, 0x12, 0x98, 0x40, 0x45, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  3395. };
  3396. //
  3397. // Dispatch RPC payload to kms.c
  3398. //
  3399. typedef int (*CreateResponse_t)(const void *const, void *const, const char* const);
  3400. static const struct {
  3401. unsigned int RequestSize;
  3402. CreateResponse_t CreateResponse;
  3403. } _Versions[] = {
  3404. { sizeof(REQUEST_V4), (CreateResponse_t) CreateResponseV4 },
  3405. { sizeof(REQUEST_V6), (CreateResponse_t) CreateResponseV6 },
  3406. { sizeof(REQUEST_V6), (CreateResponse_t) CreateResponseV6 }
  3407. };
  3408. RPC_FLAGS RpcFlags;
  3409. static int_fast8_t firstPacketSent;
  3410. //
  3411. // RPC request (server)
  3412. //
  3413. #if defined(_PEDANTIC) && !defined(NO_LOG)
  3414. static void CheckRpcRequest(const RPC_REQUEST64 *const Request, const unsigned int len, WORD* NdrCtx, WORD* Ndr64Ctx, WORD Ctx)
  3415. {
  3416. uint_fast8_t kmsMajorVersion;
  3417. uint32_t requestSize = Ctx != *Ndr64Ctx ? sizeof(RPC_REQUEST) : sizeof(RPC_REQUEST64);
  3418. if (len < requestSize)
  3419. {
  3420. logger("Fatal: RPC request (including header) must be at least %i bytes but is only %i bytes.\n",
  3421. (int)(sizeof(RPC_HEADER) + requestSize),
  3422. (int)(len + sizeof(RPC_HEADER))
  3423. );
  3424. return;
  3425. }
  3426. if (len < requestSize + sizeof(DWORD))
  3427. {
  3428. logger("Fatal: KMS Request too small to contain version info (less than 4 bytes).\n");
  3429. return;
  3430. }
  3431. if (Ctx != *Ndr64Ctx)
  3432. kmsMajorVersion = LE16(((WORD*)Request->Ndr.Data)[1]);
  3433. else
  3434. kmsMajorVersion = LE16(((WORD*)Request->Ndr64.Data)[1]);
  3435. if (kmsMajorVersion > 6)
  3436. {
  3437. logger("Fatal: KMSv%u is not supported.\n", (unsigned int)kmsMajorVersion);
  3438. }
  3439. else
  3440. {
  3441. if (len >_Versions[kmsMajorVersion].RequestSize + requestSize)
  3442. logger("Warning: %u excess bytes in RPC request.\n",
  3443. len - _Versions[kmsMajorVersion].RequestSize
  3444. );
  3445. }
  3446. if (Ctx != *Ndr64Ctx && Ctx != *NdrCtx)
  3447. logger("Warning: Context id should be %u (NDR32) or %u (NDR64) but is %u.\n",
  3448. (unsigned int)*NdrCtx,
  3449. (unsigned int)*Ndr64Ctx,
  3450. Ctx
  3451. );
  3452. if (Request->Opnum)
  3453. logger("Warning: OpNum should be 0 but is %u.\n",
  3454. (unsigned int)LE16(Request->Opnum)
  3455. );
  3456. if (LE32(Request->AllocHint) != len - sizeof(RPC_REQUEST) + sizeof(Request->Ndr))
  3457. logger("Warning: Allocation hint should be %u but is %u.\n",
  3458. len + sizeof(Request->Ndr),
  3459. LE32(Request->AllocHint)
  3460. );
  3461. if (Ctx != *Ndr64Ctx)
  3462. {
  3463. if (LE32(Request->Ndr.DataLength) != len - sizeof(RPC_REQUEST))
  3464. logger("Warning: NDR32 data length field should be %u but is %u.\n",
  3465. len - sizeof(RPC_REQUEST),
  3466. LE32(Request->Ndr.DataLength)
  3467. );
  3468. if (LE32(Request->Ndr.DataSizeIs) != len - sizeof(RPC_REQUEST))
  3469. logger("Warning: NDR32 data size field should be %u but is %u.\n",
  3470. len - sizeof(RPC_REQUEST),
  3471. LE32(Request->Ndr.DataSizeIs)
  3472. );
  3473. }
  3474. else
  3475. {
  3476. if (LE64(Request->Ndr64.DataLength) != len - sizeof(RPC_REQUEST64))
  3477. logger("Warning: NDR32 data length field should be %u but is %u.\n",
  3478. len - sizeof(RPC_REQUEST) + sizeof(Request->Ndr),
  3479. LE64(Request->Ndr64.DataLength)
  3480. );
  3481. if (LE64(Request->Ndr64.DataSizeIs) != len - sizeof(RPC_REQUEST64))
  3482. logger("Warning: NDR32 data size field should be %u but is %u.\n",
  3483. len - sizeof(RPC_REQUEST64),
  3484. LE64(Request->Ndr64.DataSizeIs)
  3485. );
  3486. }
  3487. }
  3488. #endif // defined(_PEDANTIC) && !defined(NO_LOG)
  3489. /*
  3490. * check RPC request for (somewhat) correct size
  3491. * allow any size that does not cause CreateResponse to fail badly
  3492. */
  3493. static unsigned int checkRpcRequestSize(const RPC_REQUEST64 *const Request, const unsigned int requestSize, WORD* NdrCtx, WORD* Ndr64Ctx)
  3494. {
  3495. WORD Ctx = LE16(Request->ContextId);
  3496. # if defined(_PEDANTIC) && !defined(NO_LOG)
  3497. CheckRpcRequest(Request, requestSize, NdrCtx, Ndr64Ctx, Ctx);
  3498. # endif // defined(_PEDANTIC) && !defined(NO_LOG)
  3499. // Anything that is smaller than a v4 request is illegal
  3500. if (requestSize < sizeof(REQUEST_V4) + (Ctx != *Ndr64Ctx ? sizeof(RPC_REQUEST) : sizeof(RPC_REQUEST64))) return 0;
  3501. // Get KMS major version
  3502. uint_fast16_t _v;
  3503. if (Ctx != *Ndr64Ctx)
  3504. _v = LE16(((WORD*)Request->Ndr.Data)[1]) - 4;
  3505. else
  3506. _v = LE16(((WORD*)Request->Ndr64.Data)[1]) - 4;
  3507. // Only KMS v4, v5 and v6 are supported
  3508. if (_v >= vlmcsd_countof(_Versions))
  3509. {
  3510. # ifndef NO_LOG
  3511. logger("Fatal: KMSv%i unsupported\n", _v + 4);
  3512. # endif // NO_LOG
  3513. return 0;
  3514. }
  3515. // Could check for equality but allow bigger requests to support buggy RPC clients (e.g. wine)
  3516. // Buffer overrun is check by caller.
  3517. return (requestSize >= _Versions[_v].RequestSize);
  3518. }
  3519. /*
  3520. * Handles the actual KMS request from the client.
  3521. * Calls KMS functions (CreateResponseV4 or CreateResponseV6) in kms.c
  3522. * Returns size of the KMS response packet or 0 on failure.
  3523. *
  3524. * The RPC packet size (excluding header) is actually in Response->AllocHint
  3525. */
  3526. static int rpcRequest(const RPC_REQUEST64 *const Request, RPC_RESPONSE64 *const Response, const DWORD RpcAssocGroup_unused, const SOCKET sock_unused, WORD* NdrCtx, WORD* Ndr64Ctx, BYTE packetType, const char* const ipstr)
  3527. {
  3528. uint_fast16_t _v;
  3529. int ResponseSize;
  3530. WORD Ctx = LE16(Request->ContextId);
  3531. BYTE* requestData;
  3532. BYTE* responseData;
  3533. BYTE* pRpcReturnCode;
  3534. int len;
  3535. if (Ctx != *Ndr64Ctx)
  3536. {
  3537. requestData = (BYTE*)&Request->Ndr.Data;
  3538. responseData = (BYTE*)&Response->Ndr.Data;
  3539. }
  3540. else
  3541. {
  3542. requestData = (BYTE*)&Request->Ndr64.Data;
  3543. responseData = (BYTE*)&Response->Ndr64.Data;
  3544. }
  3545. _v = LE16(((WORD*)requestData)[1]) - 4;
  3546. if (!(ResponseSize = _Versions[_v].CreateResponse(requestData, responseData, ipstr)))
  3547. {
  3548. return 0;
  3549. }
  3550. if (Ctx != *Ndr64Ctx)
  3551. {
  3552. Response->Ndr.DataSizeMax = LE32(0x00020000);
  3553. Response->Ndr.DataLength = Response->Ndr.DataSizeIs = LE32(ResponseSize);
  3554. len = ResponseSize + sizeof(Response->Ndr);
  3555. }
  3556. else
  3557. {
  3558. Response->Ndr64.DataSizeMax = LE64(0x00020000ULL);
  3559. Response->Ndr64.DataLength = Response->Ndr64.DataSizeIs = LE64((uint64_t)ResponseSize);
  3560. len = ResponseSize + sizeof(Response->Ndr64);
  3561. }
  3562. pRpcReturnCode = ((BYTE*)&Response->Ndr) + len;
  3563. UA32(pRpcReturnCode) = 0; //LE32 not needed for 0
  3564. len += sizeof(DWORD);
  3565. // Pad zeros to 32-bit align (seems not neccassary but Windows RPC does it this way)
  3566. int pad = ((~len & 3) + 1) & 3;
  3567. memset(pRpcReturnCode + sizeof(DWORD), 0, pad);
  3568. len += pad;
  3569. Response->AllocHint = LE32(len);
  3570. Response->ContextId = Request->ContextId;
  3571. *((WORD*)&Response->CancelCount) = 0; // CancelCount + Pad1
  3572. return len + 8;
  3573. }
  3574. #if defined(_PEDANTIC) && !defined(NO_LOG)
  3575. static void CheckRpcBindRequest(const RPC_BIND_REQUEST *const Request, const unsigned int len)
  3576. {
  3577. uint_fast8_t i, HasTransferSyntaxNDR32 = FALSE;
  3578. char guidBuffer1[GUID_STRING_LENGTH + 1], guidBuffer2[GUID_STRING_LENGTH + 1];
  3579. uint32_t CapCtxItems = (len - sizeof(*Request) + sizeof(Request->CtxItems)) / sizeof(Request->CtxItems);
  3580. DWORD NumCtxItems = LE32(Request->NumCtxItems);
  3581. if (NumCtxItems < CapCtxItems) // Can't be too small because already handled by RpcBindSize
  3582. logger("Warning: Excess bytes in RPC bind request.\n");
  3583. for (i = 0; i < NumCtxItems; i++)
  3584. {
  3585. if (!IsEqualGUID(&Request->CtxItems[i].InterfaceUUID, InterfaceUuid))
  3586. {
  3587. uuid2StringLE((GUID*)&Request->CtxItems[i].InterfaceUUID, guidBuffer1);
  3588. uuid2StringLE((GUID*)InterfaceUuid, guidBuffer2);
  3589. logger("Warning: Interface UUID is %s but should be %s in Ctx item %u.\n", guidBuffer1, guidBuffer2, (unsigned int)i);
  3590. }
  3591. if (Request->CtxItems[i].NumTransItems != LE16(1))
  3592. logger("Fatal: %u NDR32 transfer items detected in Ctx item %u, but only one is supported.\n",
  3593. (unsigned int)LE16(Request->CtxItems[i].NumTransItems), (unsigned int)i
  3594. );
  3595. if (Request->CtxItems[i].InterfaceVerMajor != LE16(1) || Request->CtxItems[i].InterfaceVerMinor != 0)
  3596. logger("Warning: NDR32 Interface version is %u.%u but should be 1.0.\n",
  3597. (unsigned int)LE16(Request->CtxItems[i].InterfaceVerMajor),
  3598. (unsigned int)LE16(Request->CtxItems[i].InterfaceVerMinor)
  3599. );
  3600. if (Request->CtxItems[i].ContextId != LE16((WORD)i))
  3601. logger("Warning: context id of Ctx item %u is %u.\n", (unsigned int)i, (unsigned int)Request->CtxItems[i].ContextId);
  3602. if ( IsEqualGUID((GUID*)TransferSyntaxNDR32, &Request->CtxItems[i].TransferSyntax) )
  3603. {
  3604. HasTransferSyntaxNDR32 = TRUE;
  3605. if (Request->CtxItems[i].SyntaxVersion != LE32(2))
  3606. logger("NDR32 transfer syntax version is %u but should be 2.\n", LE32(Request->CtxItems[i].SyntaxVersion));
  3607. }
  3608. else if ( IsEqualGUID((GUID*)TransferSyntaxNDR64, &Request->CtxItems[i].TransferSyntax) )
  3609. {
  3610. if (Request->CtxItems[i].SyntaxVersion != LE32(1))
  3611. logger("NDR64 transfer syntax version is %u but should be 1.\n", LE32(Request->CtxItems[i].SyntaxVersion));
  3612. }
  3613. else if (!memcmp(BindTimeFeatureNegotiation, (BYTE*)(&Request->CtxItems[i].TransferSyntax), 8))
  3614. {
  3615. if (Request->CtxItems[i].SyntaxVersion != LE32(1))
  3616. logger("BTFN syntax version is %u but should be 1.\n", LE32(Request->CtxItems[i].SyntaxVersion));
  3617. }
  3618. }
  3619. if (!HasTransferSyntaxNDR32)
  3620. logger("Warning: RPC bind request has no NDR32 CtxItem.\n");
  3621. }
  3622. #endif // defined(_PEDANTIC) && !defined(NO_LOG)
  3623. /*
  3624. * Check, if we receive enough bytes to return a valid RPC bind response
  3625. */
  3626. static unsigned int checkRpcBindSize(const RPC_BIND_REQUEST *const Request, const unsigned int RequestSize, WORD* NdrCtx, WORD* Ndr64Ctx)
  3627. {
  3628. if ( RequestSize < sizeof(RPC_BIND_REQUEST) ) return FALSE;
  3629. unsigned int _NumCtxItems = LE32(Request->NumCtxItems);
  3630. if ( RequestSize < sizeof(RPC_BIND_REQUEST) - sizeof(Request->CtxItems[0]) + _NumCtxItems * sizeof(Request->CtxItems[0]) ) return FALSE;
  3631. #if defined(_PEDANTIC) && !defined(NO_LOG)
  3632. CheckRpcBindRequest(Request, RequestSize);
  3633. #endif // defined(_PEDANTIC) && !defined(NO_LOG)
  3634. return TRUE;
  3635. }
  3636. /*
  3637. * Accepts a bind or alter context request from the client and composes the bind response.
  3638. * Needs the socket because the tcp port number is part of the response.
  3639. * len is not used here.
  3640. *
  3641. * Returns TRUE on success.
  3642. */
  3643. static int rpcBind(const RPC_BIND_REQUEST *const Request, RPC_BIND_RESPONSE* Response, const DWORD RpcAssocGroup, const SOCKET sock, WORD* NdrCtx, WORD* Ndr64Ctx, BYTE packetType, const char* const ipstr_unused)
  3644. {
  3645. unsigned int i, _st = FALSE;
  3646. DWORD numCtxItems = LE32(Request->NumCtxItems);
  3647. int_fast8_t IsNDR64possible = FALSE;
  3648. uint_fast8_t portNumberSize;
  3649. socklen_t socklen;
  3650. struct sockaddr_storage addr;
  3651. // M$ RPC does not do this. Pad bytes contain apparently random data
  3652. // memset(Response->SecondaryAddress, 0, sizeof(Response->SecondaryAddress));
  3653. socklen = sizeof addr;
  3654. if (
  3655. packetType == RPC_PT_ALTERCONTEXT_REQ ||
  3656. getsockname(sock, (struct sockaddr*)&addr, &socklen) ||
  3657. getnameinfo((struct sockaddr*)&addr, socklen, NULL, 0, (char*)Response->SecondaryAddress, sizeof(Response->SecondaryAddress), NI_NUMERICSERV))
  3658. {
  3659. portNumberSize = Response->SecondaryAddressLength = 0;
  3660. }
  3661. else
  3662. {
  3663. portNumberSize = strlen((char*)Response->SecondaryAddress) + 1;
  3664. Response->SecondaryAddressLength = LE16(portNumberSize);
  3665. }
  3666. Response->MaxXmitFrag = Request->MaxXmitFrag;
  3667. Response->MaxRecvFrag = Request->MaxRecvFrag;
  3668. Response->AssocGroup = LE32(RpcAssocGroup);
  3669. // This is really ugly (but efficient) code to support padding after the secondary address field
  3670. if (portNumberSize < 3)
  3671. {
  3672. Response = (RPC_BIND_RESPONSE*)((BYTE*)Response - 4);
  3673. }
  3674. Response->NumResults = Request->NumCtxItems;
  3675. if (UseRpcNDR64)
  3676. {
  3677. for (i = 0; i < numCtxItems; i++)
  3678. {
  3679. if ( IsEqualGUID((GUID*)TransferSyntaxNDR32, &Request->CtxItems[i].TransferSyntax) )
  3680. {
  3681. /*if (packetType == RPC_PT_BIND_REQ)*/
  3682. *NdrCtx = LE16(Request->CtxItems[i].ContextId);
  3683. }
  3684. if ( IsEqualGUID((GUID*)TransferSyntaxNDR64, &Request->CtxItems[i].TransferSyntax) )
  3685. {
  3686. IsNDR64possible = TRUE;
  3687. /*if (packetType == RPC_PT_BIND_REQ)*/
  3688. *Ndr64Ctx = LE16(Request->CtxItems[i].ContextId);
  3689. }
  3690. }
  3691. }
  3692. for (i = 0; i < numCtxItems; i++)
  3693. {
  3694. memset(&Response->Results[i].TransferSyntax, 0, sizeof(GUID));
  3695. if ( !IsNDR64possible && IsEqualGUID((GUID*)TransferSyntaxNDR32, &Request->CtxItems[i].TransferSyntax) )
  3696. {
  3697. Response->Results[i].SyntaxVersion = LE32(2);
  3698. Response->Results[i].AckResult =
  3699. Response->Results[i].AckReason = RPC_BIND_ACCEPT;
  3700. memcpy(&Response->Results[i].TransferSyntax, TransferSyntaxNDR32, sizeof(GUID));
  3701. _st = TRUE;
  3702. }
  3703. else if ( IsNDR64possible && IsEqualGUID((GUID*)TransferSyntaxNDR64, &Request->CtxItems[i].TransferSyntax) )
  3704. {
  3705. Response->Results[i].SyntaxVersion = LE32(1);
  3706. Response->Results[i].AckResult =
  3707. Response->Results[i].AckReason = RPC_BIND_ACCEPT;
  3708. memcpy(&Response->Results[i].TransferSyntax, TransferSyntaxNDR64, sizeof(GUID));
  3709. _st = TRUE;
  3710. }
  3711. else if ( UseRpcBTFN && !memcmp(BindTimeFeatureNegotiation, (BYTE*)(&Request->CtxItems[i].TransferSyntax), 8) )
  3712. {
  3713. Response->Results[i].SyntaxVersion = 0;
  3714. Response->Results[i].AckResult = RPC_BIND_ACK;
  3715. // Features requested are actually encoded in the GUID
  3716. Response->Results[i].AckReason =
  3717. ((WORD*)(&Request->CtxItems[i].TransferSyntax))[4] &
  3718. (RPC_BTFN_SEC_CONTEXT_MULTIPLEX | RPC_BTFN_KEEP_ORPHAN);
  3719. }
  3720. else
  3721. {
  3722. Response->Results[i].SyntaxVersion = 0;
  3723. Response->Results[i].AckResult =
  3724. Response->Results[i].AckReason = RPC_BIND_NACK; // Unsupported
  3725. }
  3726. }
  3727. if ( !_st ) return 0;
  3728. return sizeof(RPC_BIND_RESPONSE) + numCtxItems * sizeof(((RPC_BIND_RESPONSE *)0)->Results[0]) - (portNumberSize < 3 ? 4 : 0);
  3729. }
  3730. //
  3731. // Main RPC handling routine
  3732. //
  3733. typedef unsigned int (*GetResponseSize_t)(const void *const request, const unsigned int requestSize, WORD* NdrCtx, WORD* Ndr64Ctx);
  3734. typedef int (*GetResponse_t)(const void* const request, void* response, const DWORD rpcAssocGroup, const SOCKET socket, WORD* NdrCtx, WORD* Ndr64Ctx, BYTE packetType, const char* const ipstr);
  3735. static const struct {
  3736. BYTE ResponsePacketType;
  3737. GetResponseSize_t CheckRequestSize;
  3738. GetResponse_t GetResponse;
  3739. }
  3740. _Actions[] = {
  3741. { RPC_PT_BIND_ACK, (GetResponseSize_t)checkRpcBindSize, (GetResponse_t) rpcBind },
  3742. { RPC_PT_RESPONSE, (GetResponseSize_t)checkRpcRequestSize, (GetResponse_t) rpcRequest },
  3743. { RPC_PT_ALTERCONTEXT_ACK, (GetResponseSize_t)checkRpcBindSize, (GetResponse_t) rpcBind },
  3744. };
  3745. /*
  3746. * This is the main RPC server loop. Returns after KMS request has been serviced
  3747. * or a timeout has occured.
  3748. */
  3749. void rpcServer(const SOCKET sock, const DWORD RpcAssocGroup, const char* const ipstr)
  3750. {
  3751. RPC_HEADER rpcRequestHeader;
  3752. WORD NdrCtx = INVALID_NDR_CTX, Ndr64Ctx = INVALID_NDR_CTX;
  3753. randomNumberInit();
  3754. while (_recv(sock, &rpcRequestHeader, sizeof(rpcRequestHeader)))
  3755. {
  3756. //int_fast8_t _st;
  3757. unsigned int request_len, response_len;
  3758. uint_fast8_t _a;
  3759. #if defined(_PEDANTIC) && !defined(NO_LOG)
  3760. checkRpcHeader(&rpcRequestHeader, rpcRequestHeader.PacketType, &logger);
  3761. #endif // defined(_PEDANTIC) && !defined(NO_LOG)
  3762. switch (rpcRequestHeader.PacketType)
  3763. {
  3764. case RPC_PT_BIND_REQ: _a = 0; break;
  3765. case RPC_PT_REQUEST: _a = 1; break;
  3766. case RPC_PT_ALTERCONTEXT_REQ: _a = 2; break;
  3767. default: return;
  3768. }
  3769. request_len = LE16(rpcRequestHeader.FragLength) - sizeof(rpcRequestHeader);
  3770. BYTE requestBuffer[MAX_REQUEST_SIZE + sizeof(RPC_RESPONSE64)];
  3771. BYTE responseBuffer[MAX_RESPONSE_SIZE + sizeof(RPC_HEADER) + sizeof(RPC_RESPONSE64)];
  3772. RPC_HEADER *rpcResponseHeader = (RPC_HEADER *)responseBuffer;
  3773. RPC_RESPONSE* rpcResponse = (RPC_RESPONSE*)(responseBuffer + sizeof(rpcRequestHeader));
  3774. // The request is larger than the buffer size
  3775. if (request_len > MAX_REQUEST_SIZE + sizeof(RPC_REQUEST64)) return;
  3776. // Unable to receive the complete request
  3777. if (!_recv(sock, requestBuffer, request_len)) return;
  3778. // Request is invalid
  3779. if (!_Actions[_a].CheckRequestSize(requestBuffer, request_len, &NdrCtx, &Ndr64Ctx)) return;
  3780. // Unable to create a valid response from request
  3781. if (!(response_len = _Actions[_a].GetResponse(requestBuffer, rpcResponse, RpcAssocGroup, sock, &NdrCtx, &Ndr64Ctx, rpcRequestHeader.PacketType, ipstr))) return;
  3782. response_len += sizeof(RPC_HEADER);
  3783. memcpy(rpcResponseHeader, &rpcRequestHeader, sizeof(RPC_HEADER));
  3784. rpcResponseHeader->FragLength = LE16(response_len);
  3785. rpcResponseHeader->PacketType = _Actions[_a].ResponsePacketType;
  3786. if (rpcResponseHeader->PacketType == RPC_PT_ALTERCONTEXT_ACK)
  3787. rpcResponseHeader->PacketFlags = RPC_PF_FIRST | RPC_PF_LAST;
  3788. if (!_send(sock, responseBuffer, response_len)) return;
  3789. if (DisconnectImmediately && rpcResponseHeader->PacketType == RPC_PT_RESPONSE)
  3790. shutdown(sock, VLMCSD_SHUT_RDWR);
  3791. }
  3792. }
  3793. /* RPC client functions */
  3794. static DWORD CallId = 2; // M$ starts with CallId 2. So we do the same.
  3795. /*
  3796. * Checks RPC header. Returns 0 on success.
  3797. * This is mainly for debugging a non Microsoft KMS server that uses its own RPC code.
  3798. */
  3799. static int checkRpcHeader(const RPC_HEADER *const Header, const BYTE desiredPacketType, const PRINTFUNC p)
  3800. {
  3801. int status = 0;
  3802. if (Header->PacketType != desiredPacketType)
  3803. {
  3804. p("Fatal: Received wrong RPC packet type. Expected %u but got %u\n",
  3805. (uint32_t)desiredPacketType,
  3806. Header->PacketType
  3807. );
  3808. status = !0;
  3809. }
  3810. if (Header->DataRepresentation != BE32(0x10000000))
  3811. {
  3812. p("Fatal: RPC response does not conform to Microsoft's limited support of DCE RPC\n");
  3813. status = !0;
  3814. }
  3815. if (Header->AuthLength != 0)
  3816. {
  3817. p("Fatal: RPC response requests authentication\n");
  3818. status = !0;
  3819. }
  3820. // vlmcsd does not support fragmented packets (not yet neccassary)
  3821. if ( (Header->PacketFlags & (RPC_PF_FIRST | RPC_PF_LAST)) != (RPC_PF_FIRST | RPC_PF_LAST) )
  3822. {
  3823. p("Fatal: RPC packet flags RPC_PF_FIRST and RPC_PF_LAST are not both set.\n");
  3824. status = !0;
  3825. }
  3826. if (Header->PacketFlags & RPC_PF_CANCEL_PENDING) p("Warning: %s should not be set\n", "RPC_PF_CANCEL_PENDING");
  3827. if (Header->PacketFlags & RPC_PF_RESERVED) p("Warning: %s should not be set\n", "RPC_PF_RESERVED");
  3828. if (Header->PacketFlags & RPC_PF_NOT_EXEC) p("Warning: %s should not be set\n", "RPC_PF_NOT_EXEC");
  3829. if (Header->PacketFlags & RPC_PF_MAYBE) p("Warning: %s should not be set\n", "RPC_PF_MAYBE");
  3830. if (Header->PacketFlags & RPC_PF_OBJECT) p("Warning: %s should not be set\n", "RPC_PF_OBJECT");
  3831. if (Header->VersionMajor != 5 || Header->VersionMinor != 0)
  3832. {
  3833. p("Fatal: Expected RPC version 5.0 and got %u.%u\n", Header->VersionMajor, Header->VersionMinor);
  3834. status = !0;
  3835. }
  3836. return status;
  3837. }
  3838. /*
  3839. * Checks an RPC response header. Does basic header checks by calling checkRpcHeader()
  3840. * and then does additional checks if response header complies with the respective request header.
  3841. * PRINTFUNC p can be anything that has the same prototype as printf.
  3842. * Returns 0 on success.
  3843. */
  3844. static int checkRpcResponseHeader(const RPC_HEADER *const ResponseHeader, const RPC_HEADER *const RequestHeader, const BYTE desiredPacketType, const PRINTFUNC p)
  3845. {
  3846. static int_fast8_t WineBugDetected = FALSE;
  3847. int status = checkRpcHeader(ResponseHeader, desiredPacketType, p);
  3848. if (desiredPacketType == RPC_PT_BIND_ACK)
  3849. {
  3850. if ((ResponseHeader->PacketFlags & RPC_PF_MULTIPLEX) != (RequestHeader->PacketFlags & RPC_PF_MULTIPLEX))
  3851. {
  3852. p("Warning: RPC_PF_MULTIPLEX of RPC request and response should match\n");
  3853. }
  3854. }
  3855. else
  3856. {
  3857. if (ResponseHeader->PacketFlags & RPC_PF_MULTIPLEX)
  3858. {
  3859. p("Warning: %s should not be set\n", "RPC_PF_MULTIPLEX");
  3860. }
  3861. }
  3862. if (!status && ResponseHeader->CallId == LE32(1))
  3863. {
  3864. if (!WineBugDetected)
  3865. {
  3866. p("Warning: Buggy RPC of Wine detected. Call Id of Response is always 1\n");
  3867. WineBugDetected = TRUE;
  3868. }
  3869. }
  3870. else if (ResponseHeader->CallId != RequestHeader->CallId)
  3871. {
  3872. p("Fatal: Sent Call Id %u but received answer for Call Id %u\n",
  3873. (uint32_t)LE32(RequestHeader->CallId),
  3874. (uint32_t)LE32(ResponseHeader->CallId)
  3875. );
  3876. status = !0;
  3877. }
  3878. return status;
  3879. }
  3880. /*
  3881. * Initializes an RPC request header as needed for KMS, i.e. packet always fits in one fragment.
  3882. * size cannot be greater than fragment length negotiated during RPC bind.
  3883. */
  3884. static void createRpcRequestHeader(RPC_HEADER* RequestHeader, BYTE packetType, WORD size)
  3885. {
  3886. RequestHeader->PacketType = packetType;
  3887. RequestHeader->PacketFlags = RPC_PF_FIRST | RPC_PF_LAST;
  3888. RequestHeader->VersionMajor = 5;
  3889. RequestHeader->VersionMinor = 0;
  3890. RequestHeader->AuthLength = 0;
  3891. RequestHeader->DataRepresentation = BE32(0x10000000); // Little endian, ASCII charset, IEEE floating point
  3892. RequestHeader->CallId = LE32(CallId);
  3893. RequestHeader->FragLength = LE16(size);
  3894. }
  3895. /*
  3896. * Sends a KMS request via RPC and receives a response.
  3897. * Parameters are raw (encrypted) reqeuests / responses.
  3898. * Returns 0 on success.
  3899. */
  3900. RpcStatus rpcSendRequest(const RpcCtx sock, const BYTE *const KmsRequest, const size_t requestSize, BYTE **KmsResponse, size_t *const responseSize)
  3901. {
  3902. #define MAX_EXCESS_BYTES 16
  3903. RPC_HEADER *RequestHeader, ResponseHeader;
  3904. RPC_REQUEST64 *RpcRequest;
  3905. RPC_RESPONSE64 _Response;
  3906. int status = 0;
  3907. int_fast8_t useNdr64 = UseRpcNDR64 && firstPacketSent;
  3908. size_t size = sizeof(RPC_HEADER) + (useNdr64 ? sizeof(RPC_REQUEST64) : sizeof(RPC_REQUEST)) + requestSize;
  3909. size_t responseSize2;
  3910. *KmsResponse = NULL;
  3911. BYTE *_Request = (BYTE*)vlmcsd_malloc(size);
  3912. RequestHeader = (RPC_HEADER*)_Request;
  3913. RpcRequest = (RPC_REQUEST64*)(_Request + sizeof(RPC_HEADER));
  3914. createRpcRequestHeader(RequestHeader, RPC_PT_REQUEST, size);
  3915. // Increment CallId for next Request
  3916. CallId++;
  3917. RpcRequest->Opnum = 0;
  3918. if (useNdr64)
  3919. {
  3920. RpcRequest->ContextId = LE16(1); // We negotiate NDR64 always as context 1
  3921. RpcRequest->AllocHint = LE32(requestSize + sizeof(RpcRequest->Ndr64));
  3922. RpcRequest->Ndr64.DataLength = LE64((uint64_t)requestSize);
  3923. RpcRequest->Ndr64.DataSizeIs = LE64((uint64_t)requestSize);
  3924. memcpy(RpcRequest->Ndr64.Data, KmsRequest, requestSize);
  3925. }
  3926. else
  3927. {
  3928. RpcRequest->ContextId = 0; // We negotiate NDR32 always as context 0
  3929. RpcRequest->AllocHint = LE32(requestSize + sizeof(RpcRequest->Ndr));
  3930. RpcRequest->Ndr.DataLength = LE32(requestSize);
  3931. RpcRequest->Ndr.DataSizeIs = LE32(requestSize);
  3932. memcpy(RpcRequest->Ndr.Data, KmsRequest, requestSize);
  3933. }
  3934. for(;;)
  3935. {
  3936. int bytesread;
  3937. if (!_send(sock, _Request, size))
  3938. {
  3939. errorout("\nFatal: Could not send RPC request\n");
  3940. status = !0;
  3941. break;
  3942. }
  3943. if (!_recv(sock, &ResponseHeader, sizeof(RPC_HEADER)))
  3944. {
  3945. errorout("\nFatal: No RPC response received from server\n");
  3946. status = !0;
  3947. break;
  3948. }
  3949. if ((status = checkRpcResponseHeader(&ResponseHeader, RequestHeader, RPC_PT_RESPONSE, &errorout))) break;
  3950. size = useNdr64 ? sizeof(RPC_RESPONSE64) : sizeof(RPC_RESPONSE);
  3951. if (size > LE16(ResponseHeader.FragLength) - sizeof(ResponseHeader))
  3952. size = LE16(ResponseHeader.FragLength) - sizeof(ResponseHeader);
  3953. if (!_recv(sock, &_Response, size))
  3954. {
  3955. errorout("\nFatal: RPC response is incomplete\n");
  3956. status = !0;
  3957. break;
  3958. }
  3959. if (_Response.CancelCount != 0)
  3960. {
  3961. errorout("\nFatal: RPC response cancel count is not 0\n");
  3962. status = !0;
  3963. }
  3964. if (_Response.ContextId != (useNdr64 ? LE16(1) : 0))
  3965. {
  3966. errorout("\nFatal: RPC response context id %u is not bound\n", (unsigned int)LE16(_Response.ContextId));
  3967. status = !0;
  3968. }
  3969. int_fast8_t sizesMatch;
  3970. if (useNdr64)
  3971. {
  3972. *responseSize = (size_t)LE64(_Response.Ndr64.DataLength);
  3973. responseSize2 = (size_t)LE64(_Response.Ndr64.DataSizeIs);
  3974. if (!*responseSize || !_Response.Ndr64.DataSizeMax)
  3975. {
  3976. status = (int)LE32(_Response.Ndr64.status);
  3977. break;
  3978. }
  3979. sizesMatch = (size_t)LE64(_Response.Ndr64.DataLength) == responseSize2;
  3980. }
  3981. else
  3982. {
  3983. *responseSize = (size_t)LE32(_Response.Ndr.DataLength);
  3984. responseSize2 = (size_t)LE32(_Response.Ndr.DataSizeIs);
  3985. if (!*responseSize || !_Response.Ndr.DataSizeMax)
  3986. {
  3987. status = (int)LE32(_Response.Ndr.status);
  3988. break;
  3989. }
  3990. sizesMatch = (size_t)LE32(_Response.Ndr.DataLength) == responseSize2;
  3991. }
  3992. if (!sizesMatch)
  3993. {
  3994. errorout("\nFatal: NDR data length (%u) does not match NDR data size (%u)\n",
  3995. (uint32_t)*responseSize,
  3996. (uint32_t)LE32(_Response.Ndr.DataSizeIs)
  3997. );
  3998. status = !0;
  3999. }
  4000. *KmsResponse = (BYTE*)vlmcsd_malloc(*responseSize + MAX_EXCESS_BYTES);
  4001. // If RPC stub is too short, assume missing bytes are zero (same ill behavior as MS RPC)
  4002. memset(*KmsResponse, 0, *responseSize + MAX_EXCESS_BYTES);
  4003. // Read up to 16 bytes more than bytes expected to detect faulty KMS emulators
  4004. if ((bytesread = recv(sock, (char*)*KmsResponse, *responseSize + MAX_EXCESS_BYTES, 0)) < (int)*responseSize)
  4005. {
  4006. errorout("\nFatal: No or incomplete KMS response received. Required %u bytes but only got %i\n",
  4007. (uint32_t)*responseSize,
  4008. (int32_t)(bytesread < 0 ? 0 : bytesread)
  4009. );
  4010. status = !0;
  4011. break;
  4012. }
  4013. DWORD *pReturnCode;
  4014. size_t len = *responseSize + (useNdr64 ? sizeof(_Response.Ndr64) : sizeof(_Response.Ndr)) + sizeof(*pReturnCode);
  4015. size_t pad = ((~len & 3) + 1) & 3;
  4016. if (len + pad != LE32(_Response.AllocHint))
  4017. {
  4018. errorout("\nWarning: RPC stub size is %u, should be %u (probably incorrect padding)\n", (uint32_t)LE32(_Response.AllocHint), (uint32_t)(len + pad));
  4019. }
  4020. else
  4021. {
  4022. size_t i;
  4023. for (i = 0; i < pad; i++)
  4024. {
  4025. if (*(*KmsResponse + *responseSize + sizeof(*pReturnCode) + i))
  4026. {
  4027. errorout("\nWarning: RPC stub data not padded to zeros according to Microsoft standard\n");
  4028. break;
  4029. }
  4030. }
  4031. }
  4032. pReturnCode = (DWORD*)(*KmsResponse + *responseSize + pad);
  4033. status = LE32(UA32(pReturnCode));
  4034. if (status) errorout("\nWarning: RPC stub data reported Error %u\n", (uint32_t)status);
  4035. break;
  4036. }
  4037. free(_Request);
  4038. firstPacketSent = TRUE;
  4039. return status;
  4040. #undef MAX_EXCESS_BYTES
  4041. }
  4042. static int_fast8_t IsNullGuid(BYTE* guidPtr)
  4043. {
  4044. int_fast8_t i;
  4045. for (i = 0; i < 16; i++)
  4046. {
  4047. if (guidPtr[i]) return FALSE;
  4048. }
  4049. return TRUE;
  4050. }
  4051. /*
  4052. * Perform RPC client bind. Accepts a connected client socket.
  4053. * Returns 0 on success. RPC binding is required before any payload can be
  4054. * exchanged. It negotiates about protocol details.
  4055. */
  4056. RpcStatus rpcBindOrAlterClientContext(const RpcCtx sock, BYTE packetType, const int_fast8_t verbose)
  4057. {
  4058. RPC_HEADER *RequestHeader, ResponseHeader;
  4059. RPC_BIND_REQUEST *bindRequest;
  4060. RPC_BIND_RESPONSE *bindResponse;
  4061. int status;
  4062. WORD ctxItems = 1 + (packetType == RPC_PT_BIND_REQ ? UseRpcNDR64 + UseRpcBTFN : 0);
  4063. size_t rpcBindSize = (sizeof(RPC_HEADER) + sizeof(RPC_BIND_REQUEST) + (ctxItems - 1) * sizeof(bindRequest->CtxItems[0]));
  4064. WORD ctxIndex = 0;
  4065. WORD i;
  4066. WORD CtxBTFN = (WORD)~0, CtxNDR64 = (WORD)~0;
  4067. BYTE _Request[rpcBindSize];
  4068. RequestHeader = (RPC_HEADER*)_Request;
  4069. bindRequest = (RPC_BIND_REQUEST* )(_Request + sizeof(RPC_HEADER));
  4070. createRpcRequestHeader(RequestHeader, packetType, rpcBindSize);
  4071. RequestHeader->PacketFlags |= UseMultiplexedRpc ? RPC_PF_MULTIPLEX : 0;
  4072. bindRequest->AssocGroup = 0;
  4073. bindRequest->MaxRecvFrag = bindRequest->MaxXmitFrag = LE16(5840);
  4074. bindRequest->NumCtxItems = LE32(ctxItems);
  4075. // data that is identical in all Ctx items
  4076. for (i = 0; i < ctxItems; i++)
  4077. {
  4078. bindRequest->CtxItems[i].ContextId = LE16(i);
  4079. bindRequest->CtxItems[i].InterfaceVerMajor = LE16(1);
  4080. bindRequest->CtxItems[i].InterfaceVerMinor = 0;
  4081. bindRequest->CtxItems[i].NumTransItems = LE16(1);
  4082. bindRequest->CtxItems[i].SyntaxVersion = i ? LE32(1) : LE32(2);
  4083. memcpy(&bindRequest->CtxItems[i].InterfaceUUID, InterfaceUuid, sizeof(GUID));
  4084. }
  4085. memcpy(&bindRequest->CtxItems[0].TransferSyntax, TransferSyntaxNDR32, sizeof(GUID));
  4086. if (UseRpcNDR64 && packetType == RPC_PT_BIND_REQ)
  4087. {
  4088. memcpy(&bindRequest->CtxItems[++ctxIndex].TransferSyntax, TransferSyntaxNDR64, sizeof(GUID));
  4089. CtxNDR64 = ctxIndex;
  4090. }
  4091. if (UseRpcBTFN && packetType == RPC_PT_BIND_REQ)
  4092. {
  4093. memcpy(&bindRequest->CtxItems[++ctxIndex].TransferSyntax, BindTimeFeatureNegotiation, sizeof(GUID));
  4094. CtxBTFN = ctxIndex;
  4095. }
  4096. if (!_send(sock, _Request, rpcBindSize))
  4097. {
  4098. errorout("\nFatal: Sending RPC bind request failed\n");
  4099. return !0;
  4100. }
  4101. if (!_recv(sock, &ResponseHeader, sizeof(RPC_HEADER)))
  4102. {
  4103. errorout("\nFatal: Did not receive a response from server\n");
  4104. return !0;
  4105. }
  4106. if ((status = checkRpcResponseHeader
  4107. (
  4108. &ResponseHeader,
  4109. RequestHeader,
  4110. packetType == RPC_PT_BIND_REQ ? RPC_PT_BIND_ACK : RPC_PT_ALTERCONTEXT_ACK,
  4111. &errorout
  4112. )))
  4113. {
  4114. return status;
  4115. }
  4116. bindResponse = (RPC_BIND_RESPONSE*)vlmcsd_malloc(LE16(ResponseHeader.FragLength) - sizeof(RPC_HEADER));
  4117. BYTE* bindResponseBytePtr = (BYTE*)bindResponse;
  4118. if (!_recv(sock, bindResponse, LE16(ResponseHeader.FragLength) - sizeof(RPC_HEADER)))
  4119. {
  4120. errorout("\nFatal: Incomplete RPC bind acknowledgement received\n");
  4121. free(bindResponseBytePtr);
  4122. return !0;
  4123. }
  4124. else
  4125. {
  4126. /*
  4127. * checking, whether a bind or alter context response is as expected.
  4128. * This check is very strict and checks whether a KMS emulator behaves exactly the same way
  4129. * as Microsoft's RPC does.
  4130. */
  4131. status = 0;
  4132. if (bindResponse->SecondaryAddressLength < LE16(3))
  4133. bindResponse = (RPC_BIND_RESPONSE*)(bindResponseBytePtr - 4);
  4134. if (bindResponse->NumResults != bindRequest->NumCtxItems)
  4135. {
  4136. errorout("\nFatal: Expected %u CTX items but got %u\n",
  4137. (uint32_t)LE32(bindRequest->NumCtxItems),
  4138. (uint32_t)LE32(bindResponse->NumResults)
  4139. );
  4140. status = !0;
  4141. }
  4142. for (i = 0; i < ctxItems; i++)
  4143. {
  4144. const char* transferSyntaxName =
  4145. i == CtxBTFN ? "BTFN" : i == CtxNDR64 ? "NDR64" : "NDR32";
  4146. if (bindResponse->Results[i].AckResult == RPC_BIND_NACK) // transfer syntax was declined
  4147. {
  4148. if (!IsNullGuid((BYTE*)&bindResponse->Results[i].TransferSyntax))
  4149. {
  4150. errorout(
  4151. "\nWarning: Rejected transfer syntax %s did not return NULL Guid\n",
  4152. transferSyntaxName
  4153. );
  4154. }
  4155. if (bindResponse->Results[i].SyntaxVersion)
  4156. {
  4157. errorout(
  4158. "\nWarning: Rejected transfer syntax %s did not return syntax version 0 but %u\n",
  4159. transferSyntaxName,
  4160. LE32(bindResponse->Results[i].SyntaxVersion)
  4161. );
  4162. }
  4163. if (bindResponse->Results[i].AckReason == RPC_ABSTRACTSYNTAX_UNSUPPORTED)
  4164. {
  4165. errorout(
  4166. "\nWarning: Transfer syntax %s does not support KMS activation\n",
  4167. transferSyntaxName
  4168. );
  4169. }
  4170. else if (bindResponse->Results[i].AckReason != RPC_SYNTAX_UNSUPPORTED)
  4171. {
  4172. errorout(
  4173. "\nWarning: Rejected transfer syntax %s did not return ack reason RPC_SYNTAX_UNSUPPORTED\n",
  4174. transferSyntaxName
  4175. );
  4176. }
  4177. continue;
  4178. }
  4179. if (i == CtxBTFN) // BTFN
  4180. {
  4181. if (bindResponse->Results[i].AckResult != RPC_BIND_ACK)
  4182. {
  4183. errorout("\nWarning: BTFN did not respond with RPC_BIND_ACK or RPC_BIND_NACK\n");
  4184. }
  4185. if (bindResponse->Results[i].AckReason != LE16(3))
  4186. {
  4187. errorout("\nWarning: BTFN did not return expected feature mask 0x3 but 0x%X\n", (unsigned int)LE16(bindResponse->Results[i].AckReason));
  4188. }
  4189. if (verbose) printf("... BTFN ");
  4190. RpcFlags.HasBTFN = TRUE;
  4191. continue;
  4192. }
  4193. // NDR32 or NDR64 Ctx
  4194. if (bindResponse->Results[i].AckResult != RPC_BIND_ACCEPT)
  4195. {
  4196. errorout(
  4197. "\nFatal: transfer syntax %s returned an invalid status, neither RPC_BIND_ACCEPT nor RPC_BIND_NACK\n",
  4198. transferSyntaxName
  4199. );
  4200. status = !0;
  4201. }
  4202. if (!IsEqualGUID(&bindResponse->Results[i].TransferSyntax, &bindRequest->CtxItems[i].TransferSyntax))
  4203. {
  4204. errorout(
  4205. "\nFatal: Transfer syntax of RPC bind request and response does not match\n"
  4206. );
  4207. status = !0;
  4208. }
  4209. if (bindResponse->Results[i].SyntaxVersion != bindRequest->CtxItems[i].SyntaxVersion)
  4210. {
  4211. errorout("\nFatal: Expected transfer syntax version %u for %s but got %u\n",
  4212. (uint32_t)LE32(bindRequest->CtxItems[0].SyntaxVersion),
  4213. transferSyntaxName,
  4214. (uint32_t)LE32(bindResponse->Results[0].SyntaxVersion)
  4215. );
  4216. status = !0;
  4217. }
  4218. // The ack reason field is actually undefined here but Microsoft sets this to 0
  4219. if (bindResponse->Results[i].AckReason != 0)
  4220. {
  4221. errorout(
  4222. "\nWarning: Ack reason should be 0 but is %u\n",
  4223. LE16(bindResponse->Results[i].AckReason)
  4224. );
  4225. }
  4226. if (!status)
  4227. {
  4228. if (i == CtxNDR64)
  4229. {
  4230. RpcFlags.HasNDR64 = TRUE;
  4231. if (verbose) printf("... NDR64 ");
  4232. }
  4233. if (!i)
  4234. {
  4235. RpcFlags.HasNDR32 = TRUE;
  4236. if (verbose) printf("... NDR32 ");
  4237. }
  4238. }
  4239. }
  4240. }
  4241. free(bindResponseBytePtr);
  4242. if (!RpcFlags.HasNDR64 && !RpcFlags.HasNDR32)
  4243. {
  4244. errorout("\nFatal: Could neither negotiate NDR32 nor NDR64 with the RPC server\n");
  4245. status = !0;
  4246. }
  4247. return status;
  4248. }
  4249. RpcStatus rpcBindClient(const RpcCtx sock, const int_fast8_t verbose)
  4250. {
  4251. firstPacketSent = FALSE;
  4252. RpcFlags.mask = 0;
  4253. RpcStatus status =
  4254. rpcBindOrAlterClientContext(sock, RPC_PT_BIND_REQ, verbose);
  4255. if (status) return status;
  4256. if (!RpcFlags.HasNDR32)
  4257. status = rpcBindOrAlterClientContext(sock, RPC_PT_ALTERCONTEXT_REQ, verbose);
  4258. return status;
  4259. }
  4260. #endif // USE_MSRPC
  4261. #ifndef CONFIG
  4262. #define CONFIG "config.h"
  4263. #endif // CONFIG
  4264. #include CONFIG
  4265. #if !defined(_CRYPTO_OPENSSL) && !defined(_CRYPTO_POLARSSL) && !defined(_CRYPTO_WINDOWS)
  4266. #include "crypto_internal.h"
  4267. #include "endian.h"
  4268. #define F0(x, y, z) ( ((x) & (y)) | (~(x) & (z)) )
  4269. #define F1(x, y, z) ( ((x) & (y)) | ((x) & (z)) | ((y) & (z)) )
  4270. #define SI1(x) ( ROR32(x, 2 ) ^ ROR32(x, 13) ^ ROR32(x, 22) )
  4271. #define SI2(x) ( ROR32(x, 6 ) ^ ROR32(x, 11) ^ ROR32(x, 25) )
  4272. #define SI3(x) ( ROR32(x, 7 ) ^ ROR32(x, 18) ^ ((x) >> 3 ) )
  4273. #define SI4(x) ( ROR32(x, 17) ^ ROR32(x, 19) ^ ((x) >> 10) )
  4274. static const DWORD k[] = {
  4275. 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1,
  4276. 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
  4277. 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786,
  4278. 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
  4279. 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147,
  4280. 0x06CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
  4281. 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B,
  4282. 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
  4283. 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A,
  4284. 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
  4285. 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
  4286. };
  4287. static void Sha256Init(Sha256Ctx *Ctx)
  4288. {
  4289. Ctx->State[0] = 0x6A09E667;
  4290. Ctx->State[1] = 0xBB67AE85;
  4291. Ctx->State[2] = 0x3C6EF372;
  4292. Ctx->State[3] = 0xA54FF53A;
  4293. Ctx->State[4] = 0x510E527F;
  4294. Ctx->State[5] = 0x9B05688C;
  4295. Ctx->State[6] = 0x1F83D9AB;
  4296. Ctx->State[7] = 0x5BE0CD19;
  4297. Ctx->Len = 0;
  4298. }
  4299. static void Sha256ProcessBlock(Sha256Ctx *Ctx, BYTE *block)
  4300. {
  4301. unsigned int i;
  4302. DWORD w[64], temp1, temp2;
  4303. DWORD a = Ctx->State[0];
  4304. DWORD b = Ctx->State[1];
  4305. DWORD c = Ctx->State[2];
  4306. DWORD d = Ctx->State[3];
  4307. DWORD e = Ctx->State[4];
  4308. DWORD f = Ctx->State[5];
  4309. DWORD g = Ctx->State[6];
  4310. DWORD h = Ctx->State[7];
  4311. for (i = 0; i < 16; i++)
  4312. //w[ i ] = GET_UAA32BE(block, i);
  4313. w[i] = BE32(((DWORD*)block)[i]);
  4314. for (i = 16; i < 64; i++)
  4315. w[ i ] = SI4(w[ i - 2 ]) + w[ i - 7 ] + SI3(w[ i - 15 ]) + w[ i - 16 ];
  4316. for (i = 0; i < 64; i++)
  4317. {
  4318. temp1 = h + SI2(e) + F0(e, f, g) + k[ i ] + w[ i ];
  4319. temp2 = SI1(a) + F1(a, b, c);
  4320. h = g;
  4321. g = f;
  4322. f = e;
  4323. e = d + temp1;
  4324. d = c;
  4325. c = b;
  4326. b = a;
  4327. a = temp1 + temp2;
  4328. }
  4329. Ctx->State[0] += a;
  4330. Ctx->State[1] += b;
  4331. Ctx->State[2] += c;
  4332. Ctx->State[3] += d;
  4333. Ctx->State[4] += e;
  4334. Ctx->State[5] += f;
  4335. Ctx->State[6] += g;
  4336. Ctx->State[7] += h;
  4337. }
  4338. static void Sha256Update(Sha256Ctx *Ctx, BYTE *data, size_t len)
  4339. {
  4340. unsigned int b_len = Ctx->Len & 63,
  4341. r_len = (b_len ^ 63) + 1;
  4342. Ctx->Len += len;
  4343. if ( len < r_len )
  4344. {
  4345. memcpy(Ctx->Buffer + b_len, data, len);
  4346. return;
  4347. }
  4348. if ( r_len < 64 )
  4349. {
  4350. memcpy(Ctx->Buffer + b_len, data, r_len);
  4351. len -= r_len;
  4352. data += r_len;
  4353. Sha256ProcessBlock(Ctx, Ctx->Buffer);
  4354. }
  4355. for (; len >= 64; len -= 64, data += 64)
  4356. Sha256ProcessBlock(Ctx, data);
  4357. if ( len ) memcpy(Ctx->Buffer, data, len);
  4358. }
  4359. static void Sha256Finish(Sha256Ctx *Ctx, BYTE *hash)
  4360. {
  4361. unsigned int i, b_len = Ctx->Len & 63;
  4362. Ctx->Buffer[ b_len ] = 0x80;
  4363. if ( b_len ^ 63 ) memset(Ctx->Buffer + b_len + 1, 0, b_len ^ 63);
  4364. if ( b_len >= 56 )
  4365. {
  4366. Sha256ProcessBlock(Ctx, Ctx->Buffer);
  4367. memset(Ctx->Buffer, 0, 56);
  4368. }
  4369. //PUT_UAA64BE(Ctx->Buffer, (unsigned long long)(Ctx->Len * 8), 7);
  4370. ((uint64_t*)Ctx->Buffer)[7] = BE64((uint64_t)Ctx->Len << 3);
  4371. Sha256ProcessBlock(Ctx, Ctx->Buffer);
  4372. for (i = 0; i < 8; i++)
  4373. //PUT_UAA32BE(hash, Ctx->State[i], i);
  4374. ((DWORD*)hash)[i] = BE32(Ctx->State[i]);
  4375. }
  4376. void Sha256(BYTE *data, size_t len, BYTE *hash)
  4377. {
  4378. Sha256Ctx Ctx;
  4379. Sha256Init(&Ctx);
  4380. Sha256Update(&Ctx, data, len);
  4381. Sha256Finish(&Ctx, hash);
  4382. }
  4383. static void _Sha256HmacInit(Sha256HmacCtx *Ctx, BYTE *key, size_t klen)
  4384. {
  4385. BYTE IPad[64];
  4386. unsigned int i;
  4387. memset(IPad, 0x36, sizeof(IPad));
  4388. memset(Ctx->OPad, 0x5C, sizeof(Ctx->OPad));
  4389. if ( klen > 64 )
  4390. {
  4391. BYTE *temp = (BYTE*)alloca(32);
  4392. Sha256(key, klen, temp);
  4393. klen = 32;
  4394. key = temp;
  4395. }
  4396. for (i = 0; i < klen; i++)
  4397. {
  4398. IPad[ i ] ^= key[ i ];
  4399. Ctx->OPad[ i ] ^= key[ i ];
  4400. }
  4401. Sha256Init(&Ctx->ShaCtx);
  4402. Sha256Update(&Ctx->ShaCtx, IPad, sizeof(IPad));
  4403. }
  4404. static void _Sha256HmacUpdate(Sha256HmacCtx *Ctx, BYTE *data, size_t len)
  4405. {
  4406. Sha256Update(&Ctx->ShaCtx, data, len);
  4407. }
  4408. static void _Sha256HmacFinish(Sha256HmacCtx *Ctx, BYTE *hmac)
  4409. {
  4410. BYTE temp[32];
  4411. Sha256Finish(&Ctx->ShaCtx, temp);
  4412. Sha256Init(&Ctx->ShaCtx);
  4413. Sha256Update(&Ctx->ShaCtx, Ctx->OPad, sizeof(Ctx->OPad));
  4414. Sha256Update(&Ctx->ShaCtx, temp, sizeof(temp));
  4415. Sha256Finish(&Ctx->ShaCtx, hmac);
  4416. }
  4417. int_fast8_t Sha256Hmac(BYTE* key, BYTE* restrict data, DWORD len, BYTE* restrict hmac)
  4418. {
  4419. Sha256HmacCtx Ctx;
  4420. _Sha256HmacInit(&Ctx, key, 16);
  4421. _Sha256HmacUpdate(&Ctx, data, len);
  4422. _Sha256HmacFinish(&Ctx, hmac);
  4423. return TRUE;
  4424. }
  4425. #endif // No external Crypto
  4426. /*
  4427. * dns_srv.c
  4428. *
  4429. * This file contains the code for KMS SRV record lookup in DNS (_vlmcs._tcp.example.com IN SRV 0 0 1688 mykms.example.com)
  4430. *
  4431. */
  4432. #ifndef CONFIG
  4433. #define CONFIG "config.h"
  4434. #endif // CONFIG
  4435. #include CONFIG
  4436. #ifndef NO_DNS
  4437. #include "dns_srv.h"
  4438. #include <string.h>
  4439. #include <stdio.h>
  4440. #ifndef _WIN32
  4441. #include <signal.h>
  4442. #include <unistd.h>
  4443. #include <fcntl.h>
  4444. #include <errno.h>
  4445. #include <netdb.h>
  4446. //#ifndef DNS_PARSER_INTERNAL
  4447. #if __ANDROID__
  4448. #include <netinet/in.h>
  4449. #include "nameser.h"
  4450. #include "resolv.h"
  4451. #else // other Unix non-Android
  4452. #include <netinet/in.h>
  4453. #include <arpa/nameser.h>
  4454. #include <resolv.h>
  4455. #endif // other Unix non-Android
  4456. //#endif // DNS_PARSER_INTERNAL
  4457. #else // WIN32
  4458. #include <windns.h>
  4459. #endif // WIN32
  4460. #include "helpers.h"
  4461. #include "output.h"
  4462. #include "endian.h"
  4463. #if defined(DNS_PARSER_INTERNAL) && !defined(_WIN32)
  4464. #include "ns_name.h"
  4465. #include "ns_parse.h"
  4466. // Define macros to redirect DNS parser functions to internal versions
  4467. #undef ns_msg
  4468. #undef ns_initparse
  4469. #undef ns_parserr
  4470. #undef ns_rr
  4471. #undef ns_name_uncompress
  4472. #undef ns_msg_base
  4473. #undef ns_msg_end
  4474. #undef ns_rr_rdata
  4475. #undef ns_rr_type
  4476. #undef ns_msg_count
  4477. #undef ns_rr_class
  4478. #undef ns_s_an
  4479. #define ns_msg ns_msg_vlmcsd
  4480. #define ns_initparse ns_initparse_vlmcsd
  4481. #define ns_parserr ns_parserr_vlmcsd
  4482. #define ns_rr ns_rr_vlmcsd
  4483. #define ns_name_uncompress ns_name_uncompress_vlmcsd
  4484. #define ns_msg_base ns_msg_base_vlmcsd
  4485. #define ns_msg_end ns_msg_end_vlmcsd
  4486. #define ns_rr_rdata ns_rr_rdata_vlmcsd
  4487. #define ns_rr_type ns_rr_type_vlmcsd
  4488. #define ns_msg_count ns_msg_count_vlmcsd
  4489. #define ns_rr_class ns_rr_class_vlmcsd
  4490. #define ns_s_an ns_s_an_vlmcsd
  4491. #ifndef NS_MAXLABEL
  4492. #define NS_MAXLABEL 63
  4493. #endif
  4494. #endif // defined(DNS_PARSER_INTERNAL) && !defined(_WIN32)
  4495. //TODO: maybe move to helpers.c
  4496. static unsigned int isqrt(unsigned int n)
  4497. {
  4498. unsigned int c = 0x8000;
  4499. unsigned int g = 0x8000;
  4500. for(;;)
  4501. {
  4502. if(g*g > n)
  4503. g ^= c;
  4504. c >>= 1;
  4505. if(c == 0) return g;
  4506. g |= c;
  4507. }
  4508. }
  4509. /*
  4510. * Compare function for qsort to sort SRV records by priority and weight
  4511. * random_weight must be product of weight from SRV record and square root of a random number
  4512. */
  4513. static int kmsServerListCompareFunc1(const void* a, const void* b)
  4514. {
  4515. if ( !a && !b) return 0;
  4516. if ( a && !b) return -1;
  4517. if ( !a && b) return 1;
  4518. int priority_order = (int)((*(kms_server_dns_ptr*)a)->priority) - ((int)(*(kms_server_dns_ptr*)b)->priority);
  4519. if (priority_order) return priority_order;
  4520. return (int)((*(kms_server_dns_ptr*)b)->random_weight) - ((int)(*(kms_server_dns_ptr*)a)->random_weight);
  4521. }
  4522. /* Sort resulting SRV records */
  4523. void sortSrvRecords(kms_server_dns_ptr* serverlist, const int answers)
  4524. {
  4525. int i;
  4526. for (i = 0; i < answers; i++)
  4527. {
  4528. serverlist[i]->random_weight = (rand32() % 256) * isqrt(serverlist[i]->weight * 1000);
  4529. }
  4530. qsort(serverlist, answers, sizeof(kms_server_dns_ptr), kmsServerListCompareFunc1);
  4531. }
  4532. #define RECEIVE_BUFFER_SIZE 2048
  4533. #ifndef _WIN32 // UNIX resolver
  4534. /*
  4535. * Retrieves a raw DNS answer (a buffer of what came over the net)
  4536. * Result must be parsed
  4537. */
  4538. static int getDnsRawAnswer(const char *restrict query, unsigned char** receive_buffer)
  4539. {
  4540. if (res_init() < 0)
  4541. {
  4542. errorout("Cannot initialize resolver: %s", strerror(errno));
  4543. return 0;
  4544. }
  4545. //if(!(*receive_buffer = (unsigned char*)malloc(RECEIVE_BUFFER_SIZE))) OutOfMemory();
  4546. *receive_buffer = (unsigned char*)vlmcsd_malloc(RECEIVE_BUFFER_SIZE);
  4547. int bytes_received;
  4548. if (*query == '.')
  4549. {
  4550. # if __ANDROID__ || __GLIBC__ /* including __UCLIBC__*/ || __APPLE__ || __CYGWIN__ || __FreeBSD__ || __NetBSD__ || __DragonFly__ || __OpenBSD__ || __sun__
  4551. bytes_received = res_querydomain("_vlmcs._tcp", query + 1, ns_c_in, ns_t_srv, *receive_buffer, RECEIVE_BUFFER_SIZE);
  4552. # else
  4553. char* querystring = (char*)alloca(strlen(query) + 12);
  4554. strcpy(querystring, "_vlmcs._tcp");
  4555. strcat(querystring, query);
  4556. bytes_received = res_query(querystring, ns_c_in, ns_t_srv, *receive_buffer, RECEIVE_BUFFER_SIZE);
  4557. # endif
  4558. }
  4559. else
  4560. {
  4561. bytes_received = res_search("_vlmcs._tcp", ns_c_in, ns_t_srv, *receive_buffer, RECEIVE_BUFFER_SIZE);
  4562. }
  4563. if (bytes_received < 0)
  4564. {
  4565. errorout("Fatal: DNS query to %s%s failed: %s\n", "_vlmcs._tcp", *query == '.' ? query : "", hstrerror(h_errno));
  4566. return 0;
  4567. }
  4568. return bytes_received;
  4569. }
  4570. /*
  4571. * Retrieves an unsorted array of SRV records (Unix / Posix)
  4572. */
  4573. int getKmsServerList(kms_server_dns_ptr** serverlist, const char *restrict query)
  4574. {
  4575. unsigned char* receive_buffer;
  4576. *serverlist = NULL;
  4577. int bytes_received = getDnsRawAnswer(query, &receive_buffer);
  4578. if (bytes_received == 0) return 0;
  4579. ns_msg msg;
  4580. if (ns_initparse(receive_buffer, bytes_received, &msg) < 0)
  4581. {
  4582. errorout("Fatal: Incorrect DNS response: %s\n", strerror(errno));
  4583. free(receive_buffer);
  4584. return 0;
  4585. }
  4586. uint16_t i, answers = ns_msg_count(msg, ns_s_an);
  4587. //if(!(*serverlist = (kms_server_dns_ptr*)malloc(answers * sizeof(kms_server_dns_ptr)))) OutOfMemory();
  4588. *serverlist = (kms_server_dns_ptr*)malloc(answers * sizeof(kms_server_dns_ptr));
  4589. memset(*serverlist, 0, answers * sizeof(kms_server_dns_ptr));
  4590. for (i = 0; i < answers; i++)
  4591. {
  4592. ns_rr rr;
  4593. if (ns_parserr(&msg, ns_s_an, i, &rr) < 0)
  4594. {
  4595. errorout("Warning: Error in DNS resource record: %s\n", strerror(errno));
  4596. continue;
  4597. }
  4598. if (ns_rr_type(rr) != ns_t_srv)
  4599. {
  4600. errorout("Warning: DNS server returned non-SRV record\n");
  4601. continue;
  4602. }
  4603. if (ns_rr_class(rr) != ns_c_in)
  4604. {
  4605. errorout("Warning: DNS server returned non-IN class record\n");
  4606. continue;
  4607. }
  4608. dns_srv_record_ptr srvrecord = (dns_srv_record_ptr)ns_rr_rdata(rr);
  4609. kms_server_dns_ptr kms_server = (kms_server_dns_ptr)vlmcsd_malloc(sizeof(kms_server_dns_t));
  4610. (*serverlist)[i] = kms_server;
  4611. if (ns_name_uncompress(ns_msg_base(msg), ns_msg_end(msg), srvrecord->name, kms_server->serverName, sizeof(kms_server->serverName)) < 0)
  4612. {
  4613. errorout("Warning: No valid DNS name returned in SRV record: %s\n", strerror(errno));
  4614. continue;
  4615. }
  4616. sprintf(kms_server->serverName + strlen(kms_server->serverName), ":%hu", GET_UA16BE(&srvrecord->port));
  4617. kms_server->priority = GET_UA16BE(&srvrecord->priority);
  4618. kms_server->weight = GET_UA16BE(&srvrecord->weight);
  4619. }
  4620. free(receive_buffer);
  4621. return answers;
  4622. }
  4623. #else // WIN32 (Windows Resolver)
  4624. /*
  4625. * Retrieves an unsorted array of SRV records (Windows)
  4626. */
  4627. int getKmsServerList(kms_server_dns_ptr** serverlist, const char *const restrict query)
  4628. {
  4629. # define MAX_DNS_NAME_SIZE 254
  4630. *serverlist = NULL;
  4631. PDNS_RECORD receive_buffer;
  4632. char dnsDomain[MAX_DNS_NAME_SIZE];
  4633. char FqdnQuery[MAX_DNS_NAME_SIZE];
  4634. DWORD size = MAX_DNS_NAME_SIZE;
  4635. DNS_STATUS result;
  4636. int answers = 0;
  4637. PDNS_RECORD dns_iterator;
  4638. if (*query == '-')
  4639. {
  4640. if (!GetComputerNameExA(ComputerNamePhysicalDnsDomain, dnsDomain, &size))
  4641. {
  4642. errorout("Fatal: Could not determine computer's DNS name: %s\n", vlmcsd_strerror(GetLastError()));
  4643. return 0;
  4644. }
  4645. strcpy(FqdnQuery, "_vlmcs._tcp.");
  4646. strncat(FqdnQuery, dnsDomain, MAX_DNS_NAME_SIZE - 12);
  4647. }
  4648. else
  4649. {
  4650. strcpy(FqdnQuery, "_vlmcs._tcp");
  4651. strncat(FqdnQuery, query, MAX_DNS_NAME_SIZE - 11);
  4652. }
  4653. if ((result = DnsQuery_UTF8(FqdnQuery, DNS_TYPE_SRV, 0, NULL, &receive_buffer, NULL)) != 0)
  4654. {
  4655. errorout("Fatal: DNS query to %s failed: %s\n", FqdnQuery, vlmcsd_strerror(result));
  4656. return 0;
  4657. }
  4658. for (dns_iterator = receive_buffer; dns_iterator; dns_iterator = dns_iterator->pNext)
  4659. {
  4660. if (dns_iterator->Flags.S.Section != 1) continue;
  4661. if (dns_iterator->wType != DNS_TYPE_SRV)
  4662. {
  4663. errorout("Warning: DNS server returned non-SRV record\n");
  4664. continue;
  4665. }
  4666. answers++;
  4667. }
  4668. *serverlist = (kms_server_dns_ptr*)vlmcsd_malloc(answers * sizeof(kms_server_dns_ptr));
  4669. for (answers = 0, dns_iterator = receive_buffer; dns_iterator; dns_iterator = dns_iterator->pNext)
  4670. {
  4671. if (dns_iterator->wType != DNS_TYPE_SRV) continue;
  4672. kms_server_dns_ptr kms_server = (kms_server_dns_ptr)vlmcsd_malloc(sizeof(kms_server_dns_t));
  4673. memset(kms_server, 0, sizeof(kms_server_dns_t));
  4674. snprintf(kms_server->serverName, sizeof(kms_server->serverName), "%s:%hu", dns_iterator->Data.SRV.pNameTarget, dns_iterator->Data.SRV.wPort);
  4675. kms_server->priority = dns_iterator->Data.SRV.wPriority;
  4676. kms_server->weight = dns_iterator->Data.SRV.wWeight;
  4677. (*serverlist)[answers++] = kms_server;
  4678. }
  4679. //sortSrvRecords(*serverlist, answers, NoSrvRecordPriority);
  4680. DnsRecordListFree(receive_buffer, DnsFreeRecordList);
  4681. return answers;
  4682. # undef MAX_DNS_NAME_SIZE
  4683. }
  4684. #endif // _WIN32
  4685. #undef RECEIVE_BUFFER_SIZE
  4686. #endif // NO_DNS
  4687. /*
  4688. * Copyright (c) 1996,1999 by Internet Software Consortium.
  4689. *
  4690. * Permission to use, copy, modify, and distribute this software for any
  4691. * purpose with or without fee is hereby granted, provided that the above
  4692. * copyright notice and this permission notice appear in all copies.
  4693. *
  4694. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  4695. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  4696. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  4697. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  4698. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  4699. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  4700. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  4701. * SOFTWARE.
  4702. */
  4703. /*
  4704. * Modified by Hotbird64 for use with vlmcs.
  4705. */
  4706. #ifndef CONFIG
  4707. #define CONFIG "config.h"
  4708. #endif // CONFIG
  4709. #include CONFIG
  4710. #ifdef DNS_PARSER_INTERNAL
  4711. #ifndef NO_DNS
  4712. /* Import. */
  4713. #include <sys/types.h>
  4714. #include <errno.h>
  4715. #include <string.h>
  4716. #include "types.h"
  4717. #include "endian.h"
  4718. #include "ns_name.h"
  4719. #include "ns_parse.h"
  4720. /* Macros. */
  4721. #define NS_GET16_VLMCSD(s, cp) do { \
  4722. (s) = GET_UA16BE(cp); \
  4723. (cp) += NS_INT16SZ; \
  4724. } while (0)
  4725. #define NS_GET32_VLMCSD(l, cp) do { \
  4726. (l) = GET_UA32BE(cp); \
  4727. (cp) += NS_INT32SZ; \
  4728. } while (0)
  4729. #define RETERR(err) do { errno = (err); return (-1); } while (0)
  4730. /* Forward. */
  4731. static void setsection_vlmcsd(ns_msg_vlmcsd *msg, ns_sect_vlmcsd sect);
  4732. static int dn_skipname_vlmcsd(const unsigned char *s, const unsigned char *end)
  4733. {
  4734. const unsigned char *p;
  4735. for (p=s; p<end; p++)
  4736. if (!*p) return p-s+1;
  4737. else if (*p>=192)
  4738. {if (p+1<end) return p-s+2;
  4739. else break;}
  4740. return -1;
  4741. }
  4742. static int
  4743. ns_skiprr_vlmcsd(const uint8_t *ptr, const uint8_t *eom, ns_sect_vlmcsd section, int count) {
  4744. const uint8_t *optr = ptr;
  4745. for ((void)NULL; count > 0; count--) {
  4746. int b, rdlength;
  4747. b = dn_skipname_vlmcsd(ptr, eom);
  4748. if (b < 0)
  4749. RETERR(EMSGSIZE);
  4750. ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
  4751. if (section != ns_s_qd_vlmcsd) {
  4752. if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
  4753. RETERR(EMSGSIZE);
  4754. ptr += NS_INT32SZ/*TTL*/;
  4755. NS_GET16_VLMCSD(rdlength, ptr);
  4756. ptr += rdlength/*RData*/;
  4757. }
  4758. }
  4759. if (ptr > eom)
  4760. RETERR(EMSGSIZE);
  4761. return (ptr - optr);
  4762. }
  4763. int
  4764. ns_initparse_vlmcsd(const uint8_t *msg, int msglen, ns_msg_vlmcsd *handle) {
  4765. const uint8_t *eom = msg + msglen;
  4766. int i;
  4767. memset(handle, 0x5e, sizeof *handle);
  4768. handle->_msg = msg;
  4769. handle->_eom = eom;
  4770. if (msg + NS_INT16SZ > eom)
  4771. RETERR(EMSGSIZE);
  4772. NS_GET16_VLMCSD(handle->_id, msg);
  4773. if (msg + NS_INT16SZ > eom)
  4774. RETERR(EMSGSIZE);
  4775. NS_GET16_VLMCSD(handle->_flags, msg);
  4776. for (i = 0; i < ns_s_max_vlmcsd; i++) {
  4777. if (msg + NS_INT16SZ > eom)
  4778. RETERR(EMSGSIZE);
  4779. NS_GET16_VLMCSD(handle->_counts[i], msg);
  4780. }
  4781. for (i = 0; i < ns_s_max_vlmcsd; i++)
  4782. if (handle->_counts[i] == 0)
  4783. handle->_sections[i] = NULL;
  4784. else {
  4785. int b = ns_skiprr_vlmcsd(msg, eom, (ns_sect_vlmcsd)i,
  4786. handle->_counts[i]);
  4787. if (b < 0)
  4788. return (-1);
  4789. handle->_sections[i] = msg;
  4790. msg += b;
  4791. }
  4792. if (msg > eom)
  4793. RETERR(EMSGSIZE);
  4794. handle->_eom = msg;
  4795. setsection_vlmcsd(handle, ns_s_max_vlmcsd);
  4796. return (0);
  4797. }
  4798. int
  4799. ns_parserr_vlmcsd(ns_msg_vlmcsd *handle, ns_sect_vlmcsd section, int rrnum, ns_rr_vlmcsd *rr) {
  4800. int b;
  4801. /* Make section right. */
  4802. if (section >= ns_s_max_vlmcsd)
  4803. RETERR(ENODEV);
  4804. if (section != handle->_sect)
  4805. setsection_vlmcsd(handle, section);
  4806. /* Make rrnum right. */
  4807. if (rrnum == -1)
  4808. rrnum = handle->_rrnum;
  4809. if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
  4810. RETERR(ENODEV);
  4811. if (rrnum < handle->_rrnum)
  4812. setsection_vlmcsd(handle, section);
  4813. if (rrnum > handle->_rrnum) {
  4814. b = ns_skiprr_vlmcsd(handle->_msg_ptr, handle->_eom, section,
  4815. rrnum - handle->_rrnum);
  4816. if (b < 0)
  4817. return (-1);
  4818. handle->_msg_ptr += b;
  4819. handle->_rrnum = rrnum;
  4820. }
  4821. /* Do the parse. */
  4822. b = ns_name_uncompress_vlmcsd(handle->_msg, handle->_eom,
  4823. handle->_msg_ptr, rr->name, NS_MAXDNAME);
  4824. if (b < 0)
  4825. return (-1);
  4826. handle->_msg_ptr += b;
  4827. if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
  4828. RETERR(EMSGSIZE);
  4829. NS_GET16_VLMCSD(rr->type, handle->_msg_ptr);
  4830. NS_GET16_VLMCSD(rr->rr_class, handle->_msg_ptr);
  4831. if (section == ns_s_qd_vlmcsd) {
  4832. rr->ttl = 0;
  4833. rr->rdlength = 0;
  4834. rr->rdata = NULL;
  4835. } else {
  4836. if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
  4837. RETERR(EMSGSIZE);
  4838. NS_GET32_VLMCSD(rr->ttl, handle->_msg_ptr);
  4839. NS_GET16_VLMCSD(rr->rdlength, handle->_msg_ptr);
  4840. if (handle->_msg_ptr + rr->rdlength > handle->_eom)
  4841. RETERR(EMSGSIZE);
  4842. rr->rdata = handle->_msg_ptr;
  4843. handle->_msg_ptr += rr->rdlength;
  4844. }
  4845. if (++handle->_rrnum > handle->_counts[(int)section])
  4846. setsection_vlmcsd(handle, (ns_sect_vlmcsd)((int)section + 1));
  4847. /* All done. */
  4848. return (0);
  4849. }
  4850. /* Private. */
  4851. static void
  4852. setsection_vlmcsd(ns_msg_vlmcsd *msg, ns_sect_vlmcsd sect) {
  4853. msg->_sect = sect;
  4854. if (sect == ns_s_max_vlmcsd) {
  4855. msg->_rrnum = -1;
  4856. msg->_msg_ptr = NULL;
  4857. } else {
  4858. msg->_rrnum = 0;
  4859. msg->_msg_ptr = msg->_sections[(int)sect];
  4860. }
  4861. }
  4862. #endif // !NO_DNS
  4863. #endif // DNS_PARSER_INTERNAL
  4864. /*
  4865. * Copyright (c) 1996,1999 by Internet Software Consortium.
  4866. *
  4867. * Permission to use, copy, modify, and distribute this software for any
  4868. * purpose with or without fee is hereby granted, provided that the above
  4869. * copyright notice and this permission notice appear in all copies.
  4870. *
  4871. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  4872. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  4873. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  4874. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  4875. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  4876. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  4877. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  4878. * SOFTWARE.
  4879. */
  4880. /*
  4881. * Modified by Hotbird64 for use with vlmcs.
  4882. */
  4883. #ifndef CONFIG
  4884. #define CONFIG "config.h"
  4885. #endif // CONFIG
  4886. #include CONFIG
  4887. #ifdef DNS_PARSER_INTERNAL
  4888. #ifndef NO_DNS
  4889. #include <sys/types.h>
  4890. #include <errno.h>
  4891. #include <string.h>
  4892. #include <ctype.h>
  4893. #include <stdlib.h>
  4894. #include <stdio.h>
  4895. #include <limits.h>
  4896. #include "types.h"
  4897. #include "ns_name.h"
  4898. #ifdef SPRINTF_CHAR
  4899. # define SPRINTF(x) strlen(sprintf/**/x)
  4900. #else
  4901. # define SPRINTF(x) ((size_t)sprintf x)
  4902. #endif
  4903. #define NS_TYPE_ELT 0x40 /* EDNS0 extended label type */
  4904. #define DNS_LABELTYPE_BITSTRING 0x41
  4905. #define NS_MAXCDNAME 255
  4906. #define NS_CMPRSFLGS 0xc0
  4907. /* Data. */
  4908. static char digits[] = "0123456789";
  4909. /* Forward. */
  4910. static int special_vlmcsd(int);
  4911. static int printable_vlmcsd(int);
  4912. static int labellen_vlmcsd(const uint8_t *);
  4913. static int decode_bitstring_vlmcsd(const char **, char *, const char *);
  4914. /*
  4915. * ns_name_ntop(src, dst, dstsiz)
  4916. * Convert an encoded domain name to printable ascii as per RFC1035.
  4917. * return:
  4918. * Number of bytes written to buffer, or -1 (with errno set)
  4919. * notes:
  4920. * The root is returned as "."
  4921. * All other domains are returned in non absolute form
  4922. */
  4923. static int
  4924. ns_name_ntop_vlmcsd(const uint8_t *src, char *dst, size_t dstsiz)
  4925. {
  4926. const uint8_t *cp;
  4927. char *dn, *eom;
  4928. uint8_t c;
  4929. uint32_t n;
  4930. int l;
  4931. cp = src;
  4932. dn = dst;
  4933. eom = dst + dstsiz;
  4934. while ((n = *cp++) != 0) {
  4935. if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
  4936. /* Some kind of compression pointer. */
  4937. errno = EMSGSIZE;
  4938. return (-1);
  4939. }
  4940. if (dn != dst) {
  4941. if (dn >= eom) {
  4942. errno = EMSGSIZE;
  4943. return (-1);
  4944. }
  4945. *dn++ = '.';
  4946. }
  4947. if ((l = labellen_vlmcsd(cp - 1)) < 0) {
  4948. errno = EMSGSIZE; /* XXX */
  4949. return(-1);
  4950. }
  4951. if (dn + l >= eom) {
  4952. errno = EMSGSIZE;
  4953. return (-1);
  4954. }
  4955. if ((n & NS_CMPRSFLGS) == NS_TYPE_ELT) {
  4956. int m;
  4957. if (n != DNS_LABELTYPE_BITSTRING) {
  4958. /* XXX: labellen should reject this case */
  4959. errno = EINVAL;
  4960. return(-1);
  4961. }
  4962. if ((m = decode_bitstring_vlmcsd((const char **)&cp, dn, eom)) < 0)
  4963. {
  4964. errno = EMSGSIZE;
  4965. return(-1);
  4966. }
  4967. dn += m;
  4968. continue;
  4969. }
  4970. for ((void)NULL; l > 0; l--) {
  4971. c = *cp++;
  4972. if (special_vlmcsd(c)) {
  4973. if (dn + 1 >= eom) {
  4974. errno = EMSGSIZE;
  4975. return (-1);
  4976. }
  4977. *dn++ = '\\';
  4978. *dn++ = (char)c;
  4979. } else if (!printable_vlmcsd(c)) {
  4980. if (dn + 3 >= eom) {
  4981. errno = EMSGSIZE;
  4982. return (-1);
  4983. }
  4984. *dn++ = '\\';
  4985. *dn++ = digits[c / 100];
  4986. *dn++ = digits[(c % 100) / 10];
  4987. *dn++ = digits[c % 10];
  4988. } else {
  4989. if (dn >= eom) {
  4990. errno = EMSGSIZE;
  4991. return (-1);
  4992. }
  4993. *dn++ = (char)c;
  4994. }
  4995. }
  4996. }
  4997. if (dn == dst) {
  4998. if (dn >= eom) {
  4999. errno = EMSGSIZE;
  5000. return (-1);
  5001. }
  5002. *dn++ = '.';
  5003. }
  5004. if (dn >= eom) {
  5005. errno = EMSGSIZE;
  5006. return (-1);
  5007. }
  5008. *dn++ = '\0';
  5009. return (dn - dst);
  5010. }
  5011. static int
  5012. ns_name_unpack_vlmcsd(const uint8_t *msg, const uint8_t *eom, const uint8_t *src,
  5013. uint8_t *dst, size_t dstsiz)
  5014. {
  5015. const uint8_t *srcp, *dstlim;
  5016. uint8_t *dstp;
  5017. int n, len, checked, l;
  5018. len = -1;
  5019. checked = 0;
  5020. dstp = dst;
  5021. srcp = src;
  5022. dstlim = dst + dstsiz;
  5023. if (srcp < msg || srcp >= eom) {
  5024. errno = EMSGSIZE;
  5025. return (-1);
  5026. }
  5027. /* Fetch next label in domain name. */
  5028. while ((n = *srcp++) != 0) {
  5029. /* Check for indirection. */
  5030. switch (n & NS_CMPRSFLGS) {
  5031. case 0:
  5032. case NS_TYPE_ELT:
  5033. /* Limit checks. */
  5034. if ((l = labellen_vlmcsd(srcp - 1)) < 0) {
  5035. errno = EMSGSIZE;
  5036. return(-1);
  5037. }
  5038. if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
  5039. errno = EMSGSIZE;
  5040. return (-1);
  5041. }
  5042. checked += l + 1;
  5043. *dstp++ = n;
  5044. memcpy(dstp, srcp, l);
  5045. dstp += l;
  5046. srcp += l;
  5047. break;
  5048. case NS_CMPRSFLGS:
  5049. if (srcp >= eom) {
  5050. errno = EMSGSIZE;
  5051. return (-1);
  5052. }
  5053. if (len < 0)
  5054. len = srcp - src + 1;
  5055. srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
  5056. if (srcp < msg || srcp >= eom) { /* Out of range. */
  5057. errno = EMSGSIZE;
  5058. return (-1);
  5059. }
  5060. checked += 2;
  5061. /*
  5062. * Check for loops in the compressed name;
  5063. * if we've looked at the whole message,
  5064. * there must be a loop.
  5065. */
  5066. if (checked >= eom - msg) {
  5067. errno = EMSGSIZE;
  5068. return (-1);
  5069. }
  5070. break;
  5071. default:
  5072. errno = EMSGSIZE;
  5073. return (-1); /* flag error */
  5074. }
  5075. }
  5076. *dstp = '\0';
  5077. if (len < 0)
  5078. len = srcp - src;
  5079. return (len);
  5080. }
  5081. /*
  5082. * ns_name_uncompress_vlmcsd(msg, eom, src, dst, dstsiz)
  5083. * Expand compressed domain name to presentation format.
  5084. * return:
  5085. * Number of bytes read out of `src', or -1 (with errno set).
  5086. * note:
  5087. * Root domain returns as "." not "".
  5088. */
  5089. int
  5090. ns_name_uncompress_vlmcsd(const uint8_t *msg, const uint8_t *eom, const uint8_t *src,
  5091. char *dst, size_t dstsiz)
  5092. {
  5093. uint8_t tmp[NS_MAXCDNAME];
  5094. int n;
  5095. if ((n = ns_name_unpack_vlmcsd(msg, eom, src, tmp, sizeof tmp)) == -1)
  5096. return (-1);
  5097. if (ns_name_ntop_vlmcsd(tmp, dst, dstsiz) == -1)
  5098. return (-1);
  5099. return (n);
  5100. }
  5101. /*
  5102. * special(ch)
  5103. * Thinking in noninternationalized USASCII (per the DNS spec),
  5104. * is this characted special ("in need of quoting") ?
  5105. * return:
  5106. * boolean.
  5107. */
  5108. static int
  5109. special_vlmcsd(int ch) {
  5110. switch (ch) {
  5111. case 0x22: /* '"' */
  5112. case 0x2E: /* '.' */
  5113. case 0x3B: /* ';' */
  5114. case 0x5C: /* '\\' */
  5115. case 0x28: /* '(' */
  5116. case 0x29: /* ')' */
  5117. /* Special modifiers in zone files. */
  5118. case 0x40: /* '@' */
  5119. case 0x24: /* '$' */
  5120. return (1);
  5121. default:
  5122. return (0);
  5123. }
  5124. }
  5125. /*
  5126. * printable(ch)
  5127. * Thinking in noninternationalized USASCII (per the DNS spec),
  5128. * is this character visible and not a space when printed ?
  5129. * return:
  5130. * boolean.
  5131. */
  5132. static int
  5133. printable_vlmcsd(int ch) {
  5134. return (ch > 0x20 && ch < 0x7f);
  5135. }
  5136. static int
  5137. decode_bitstring_vlmcsd(const char **cpp, char *dn, const char *eom)
  5138. {
  5139. const char *cp = *cpp;
  5140. char *beg = dn, tc;
  5141. int b, blen, plen;
  5142. if ((blen = (*cp & 0xff)) == 0)
  5143. blen = 256;
  5144. plen = (blen + 3) / 4;
  5145. plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
  5146. if (dn + plen >= eom)
  5147. return(-1);
  5148. cp++;
  5149. dn += SPRINTF((dn, "\\[x"));
  5150. for (b = blen; b > 7; b -= 8, cp++)
  5151. dn += SPRINTF((dn, "%02x", *cp & 0xff));
  5152. if (b > 4) {
  5153. tc = *cp++;
  5154. dn += SPRINTF((dn, "%02x", tc & (0xff << (8 - b))));
  5155. } else if (b > 0) {
  5156. tc = *cp++;
  5157. dn += SPRINTF((dn, "%1x",
  5158. ((tc >> 4) & 0x0f) & (0x0f << (4 - b))));
  5159. }
  5160. dn += SPRINTF((dn, "/%d]", blen));
  5161. *cpp = cp;
  5162. return(dn - beg);
  5163. }
  5164. static int
  5165. labellen_vlmcsd(const uint8_t *lp)
  5166. {
  5167. int bitlen;
  5168. uint8_t l = *lp;
  5169. if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
  5170. /* should be avoided by the caller */
  5171. return(-1);
  5172. }
  5173. if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT) {
  5174. if (l == DNS_LABELTYPE_BITSTRING) {
  5175. if ((bitlen = *(lp + 1)) == 0)
  5176. bitlen = 256;
  5177. return((bitlen + 7 ) / 8 + 1);
  5178. }
  5179. return(-1); /* unknwon ELT */
  5180. }
  5181. return(l);
  5182. }
  5183. #endif // !NO_DNS
  5184. #endif // DNS_PARSER_INTERNAL