Client.c 223 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088
  1. // SoftEther VPN Source Code
  2. // Cedar Communication Module
  3. //
  4. // SoftEther VPN Server, Client and Bridge are free software under GPLv2.
  5. //
  6. // Copyright (c) 2012-2014 Daiyuu Nobori.
  7. // Copyright (c) 2012-2014 SoftEther VPN Project, University of Tsukuba, Japan.
  8. // Copyright (c) 2012-2014 SoftEther Corporation.
  9. //
  10. // All Rights Reserved.
  11. //
  12. // http://www.softether.org/
  13. //
  14. // Author: Daiyuu Nobori
  15. // Contributors:
  16. // - nattoheaven (https://github.com/nattoheaven)
  17. // Comments: Tetsuo Sugiyama, Ph.D.
  18. //
  19. // This program is free software; you can redistribute it and/or
  20. // modify it under the terms of the GNU General Public License
  21. // version 2 as published by the Free Software Foundation.
  22. //
  23. // This program is distributed in the hope that it will be useful,
  24. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. // GNU General Public License for more details.
  27. //
  28. // You should have received a copy of the GNU General Public License version 2
  29. // along with this program; if not, write to the Free Software
  30. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  31. //
  32. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  33. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  34. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  35. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  36. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  37. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  38. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  39. //
  40. // THE LICENSE AGREEMENT IS ATTACHED ON THE SOURCE-CODE PACKAGE
  41. // AS "LICENSE.TXT" FILE. READ THE TEXT FILE IN ADVANCE TO USE THE SOFTWARE.
  42. //
  43. //
  44. // THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN,
  45. // UNDER JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY,
  46. // MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS
  47. // SOFTWARE, THAT ANY JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS
  48. // SOFTWARE OR ITS CONTENTS, AGAINST US (SOFTETHER PROJECT, SOFTETHER
  49. // CORPORATION, DAIYUU NOBORI OR OTHER SUPPLIERS), OR ANY JURIDICAL
  50. // DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND OF USING, COPYING,
  51. // MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING, AND/OR
  52. // SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
  53. // CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO
  54. // EXCLUSIVE JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO,
  55. // JAPAN. YOU MUST WAIVE ALL DEFENSES OF LACK OF PERSONAL JURISDICTION
  56. // AND FORUM NON CONVENIENS. PROCESS MAY BE SERVED ON EITHER PARTY IN
  57. // THE MANNER AUTHORIZED BY APPLICABLE LAW OR COURT RULE.
  58. //
  59. // USE ONLY IN JAPAN. DO NOT USE IT IN OTHER COUNTRIES. IMPORTING THIS
  60. // SOFTWARE INTO OTHER COUNTRIES IS AT YOUR OWN RISK. SOME COUNTRIES
  61. // PROHIBIT ENCRYPTED COMMUNICATIONS. USING THIS SOFTWARE IN OTHER
  62. // COUNTRIES MIGHT BE RESTRICTED.
  63. //
  64. //
  65. // SOURCE CODE CONTRIBUTION
  66. // ------------------------
  67. //
  68. // Your contribution to SoftEther VPN Project is much appreciated.
  69. // Please send patches to us through GitHub.
  70. // Read the SoftEther VPN Patch Acceptance Policy in advance:
  71. // http://www.softether.org/5-download/src/9.patch
  72. //
  73. //
  74. // DEAR SECURITY EXPERTS
  75. // ---------------------
  76. //
  77. // If you find a bug or a security vulnerability please kindly inform us
  78. // about the problem immediately so that we can fix the security problem
  79. // to protect a lot of users around the world as soon as possible.
  80. //
  81. // Our e-mail address for security reports is:
  82. // softether-vpn-security [at] softether.org
  83. //
  84. // Please note that the above e-mail address is not a technical support
  85. // inquiry address. If you need technical assistance, please visit
  86. // http://www.softether.org/ and ask your question on the users forum.
  87. //
  88. // Thank you for your cooperation.
  89. //
  90. //
  91. // NO MEMORY OR RESOURCE LEAKS
  92. // ---------------------------
  93. //
  94. // The memory-leaks and resource-leaks verification under the stress
  95. // test has been passed before release this source code.
  96. // Client.c
  97. // Client Manager
  98. #include "CedarPch.h"
  99. static CLIENT *client = NULL;
  100. static LISTENER *cn_listener = NULL;
  101. static LOCK *cn_listener_lock = NULL;
  102. static UINT64 cn_next_allow = 0;
  103. static LOCK *ci_active_sessions_lock = NULL;
  104. static UINT ci_num_active_sessions = 0;
  105. // In Windows 8 or later, change unreasonable setting of WCM to ensure normal VPN communication
  106. void CiDisableWcmNetworkMinimize(CLIENT *c)
  107. {
  108. #ifdef OS_WIN32
  109. // Validate arguments
  110. if (c == NULL)
  111. {
  112. return;
  113. }
  114. if (c->Config.NoChangeWcmNetworkSettingOnWindows8)
  115. {
  116. return;
  117. }
  118. MsDisableWcmNetworkMinimize();
  119. #endif // OS_WIN32
  120. }
  121. // Compare RPC_CLIENT_ENUM_ACCOUNT_ITEM items by last connected date (Reverse)
  122. int CiCompareClientAccountEnumItemByLastConnectDateTime(void *p1, void *p2)
  123. {
  124. RPC_CLIENT_ENUM_ACCOUNT_ITEM *a1, *a2;
  125. if (p1 == NULL || p2 == NULL)
  126. {
  127. return 0;
  128. }
  129. a1 = *(RPC_CLIENT_ENUM_ACCOUNT_ITEM **)p1;
  130. a2 = *(RPC_CLIENT_ENUM_ACCOUNT_ITEM **)p2;
  131. if (a1 == NULL || a2 == NULL)
  132. {
  133. return 0;
  134. }
  135. if (a1->LastConnectDateTime > a2->LastConnectDateTime)
  136. {
  137. return -1;
  138. }
  139. else if (a1->LastConnectDateTime < a2->LastConnectDateTime)
  140. {
  141. return 1;
  142. }
  143. return 0;
  144. }
  145. // If machine changed, reshuffle MAC address for all virtual NIC
  146. void CiChangeAllVLanMacAddressIfMachineChanged(CLIENT *c)
  147. {
  148. UCHAR current_hash_new[SHA1_SIZE];
  149. UCHAR current_hash[SHA1_SIZE];
  150. UCHAR current_hash_old[SHA1_SIZE];
  151. UCHAR saved_hash[SHA1_SIZE];
  152. // Validate arguments
  153. if (c == NULL)
  154. {
  155. return;
  156. }
  157. #ifdef OS_WIN32
  158. if (MsIsAdmin() == false)
  159. {
  160. return;
  161. }
  162. #endif
  163. CiGetCurrentMachineHashNew(current_hash_new);
  164. CiGetCurrentMachineHash(current_hash);
  165. CiGetCurrentMachineHashOld(current_hash_old);
  166. if (CiReadLastMachineHash(saved_hash) == false)
  167. {
  168. CiWriteLastMachineHash(current_hash_new);
  169. return;
  170. }
  171. if (Cmp(saved_hash, current_hash_old, SHA1_SIZE) == 0)
  172. {
  173. CiWriteLastMachineHash(current_hash_new);
  174. return;
  175. }
  176. if (Cmp(saved_hash, current_hash, SHA1_SIZE) == 0)
  177. {
  178. CiWriteLastMachineHash(current_hash_new);
  179. return;
  180. }
  181. if (Cmp(saved_hash, current_hash_new, SHA1_SIZE) == 0)
  182. {
  183. return;
  184. }
  185. if (CiWriteLastMachineHash(current_hash_new) == false)
  186. {
  187. return;
  188. }
  189. CiChangeAllVLanMacAddress(c);
  190. }
  191. // Get current machine hash (Old)
  192. void CiGetCurrentMachineHashOld(void *data)
  193. {
  194. char name[MAX_PATH];
  195. char *product_id = NULL;
  196. // Validate arguments
  197. if (data == NULL)
  198. {
  199. return;
  200. }
  201. #ifdef OS_WIN32
  202. // Priduct ID
  203. product_id = MsRegReadStr(REG_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ProductId");
  204. if (product_id == NULL)
  205. {
  206. product_id = MsRegReadStr(REG_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion", "ProductId");
  207. }
  208. StrCpy(name, sizeof(name), product_id);
  209. Free(product_id);
  210. #else // OS_WIN32
  211. GetMachineName(name, sizeof(name));
  212. #endif // OS_WIN32
  213. Trim(name);
  214. StrUpper(name);
  215. Hash(data, name, StrLen(name), true);
  216. }
  217. // Get current machine hash
  218. void CiGetCurrentMachineHash(void *data)
  219. {
  220. char name[MAX_PATH];
  221. char *product_id = NULL;
  222. // Validate arguments
  223. if (data == NULL)
  224. {
  225. return;
  226. }
  227. GetMachineName(name, sizeof(name));
  228. Trim(name);
  229. StrUpper(name);
  230. Hash(data, name, StrLen(name), true);
  231. }
  232. // Get current machine hash (without using domain name)
  233. void CiGetCurrentMachineHashNew(void *data)
  234. {
  235. char name[MAX_PATH];
  236. char *p;
  237. // Validate arguments
  238. if (data == NULL)
  239. {
  240. return;
  241. }
  242. GetMachineName(name, sizeof(name));
  243. // Ignore after first period(.)
  244. for(p=name; *p; p++)
  245. if(*p == '.')
  246. *p = 0;
  247. Trim(name);
  248. StrUpper(name);
  249. Hash(data, name, StrLen(name), true);
  250. }
  251. // Write machine hash
  252. bool CiWriteLastMachineHash(void *data)
  253. {
  254. // Validate arguments
  255. if (data == NULL)
  256. {
  257. return false;
  258. }
  259. #ifdef OS_WIN32
  260. if (MsRegWriteBinEx(REG_LOCAL_MACHINE, MS_REG_TCP_SETTING_KEY, "LastMachineHash", data, SHA1_SIZE, true) == false)
  261. {
  262. return false;
  263. }
  264. return true;
  265. #else // OS_WIN32
  266. return false;
  267. #endif // OS_WIN32
  268. }
  269. // Get previous machine hash
  270. bool CiReadLastMachineHash(void *data)
  271. {
  272. BUF *b = NULL;
  273. // Validate arguments
  274. if (data == NULL)
  275. {
  276. return false;
  277. }
  278. #ifdef OS_WIN32
  279. b = MsRegReadBinEx(REG_LOCAL_MACHINE, MS_REG_TCP_SETTING_KEY, "LastMachineHash", true);
  280. if (b == NULL)
  281. {
  282. return false;
  283. }
  284. if (b->Size == SHA1_SIZE)
  285. {
  286. Copy(data, b->Buf, b->Size);
  287. FreeBuf(b);
  288. return true;
  289. }
  290. FreeBuf(b);
  291. return false;
  292. #else // OS_WIN32
  293. return false;
  294. #endif // OS_WIN32
  295. }
  296. // If the MAC address of each virtual LAN card has been eliminated, set it to random numbers
  297. // (measures for Windows 8 -> 8.1 upgrade problem)
  298. void CiChangeAllVLanMacAddressIfCleared(CLIENT *c)
  299. {
  300. #ifdef OS_WIN32
  301. RPC_CLIENT_ENUM_VLAN t;
  302. // Validate arguments
  303. if (c == NULL)
  304. {
  305. return;
  306. }
  307. if (MsIsInfCatalogRequired() == false)
  308. {
  309. // Not required for other than Windows 8
  310. return;
  311. }
  312. Zero(&t, sizeof(t));
  313. if (CtEnumVLan(c, &t))
  314. {
  315. UINT i;
  316. for (i = 0;i < t.NumItem;i++)
  317. {
  318. RPC_CLIENT_ENUM_VLAN_ITEM *e = t.Items[i];
  319. UCHAR mac[6];
  320. if (StrToMac(mac, e->MacAddress))
  321. {
  322. if (mac[0] == 0x00 &&
  323. mac[1] == 0x00 &&
  324. mac[2] == 0x01 &&
  325. mac[3] == 0x00 &&
  326. mac[4] == 0x00 &&
  327. mac[5] == 0x01)
  328. {
  329. char *name = e->DeviceName;
  330. RPC_CLIENT_SET_VLAN s;
  331. UCHAR mac[6];
  332. GenMacAddress(mac);
  333. Zero(&s, sizeof(s));
  334. StrCpy(s.DeviceName, sizeof(s.DeviceName), name);
  335. MacToStr(s.MacAddress, sizeof(s.MacAddress), mac);
  336. CtSetVLan(c, &s);
  337. }
  338. }
  339. }
  340. CiFreeClientEnumVLan(&t);
  341. }
  342. #endif // OS_WIN32
  343. }
  344. // Set the MAC address of all virtual LAN cards to random number
  345. void CiChangeAllVLanMacAddress(CLIENT *c)
  346. {
  347. RPC_CLIENT_ENUM_VLAN t;
  348. // Validate arguments
  349. if (c == NULL)
  350. {
  351. return;
  352. }
  353. Zero(&t, sizeof(t));
  354. if (CtEnumVLan(c, &t))
  355. {
  356. UINT i;
  357. for (i = 0;i < t.NumItem;i++)
  358. {
  359. RPC_CLIENT_ENUM_VLAN_ITEM *e = t.Items[i];
  360. UCHAR mac[6];
  361. if (StrToMac(mac, e->MacAddress) && mac[1] == 0xAC)
  362. {
  363. char *name = e->DeviceName;
  364. RPC_CLIENT_SET_VLAN s;
  365. UCHAR mac[6];
  366. GenMacAddress(mac);
  367. Zero(&s, sizeof(s));
  368. StrCpy(s.DeviceName, sizeof(s.DeviceName), name);
  369. MacToStr(s.MacAddress, sizeof(s.MacAddress), mac);
  370. CtSetVLan(c, &s);
  371. }
  372. }
  373. CiFreeClientEnumVLan(&t);
  374. }
  375. }
  376. // Wait for preparation of notification service to complete
  377. void CnWaitForCnServiceReady()
  378. {
  379. UINT64 start_time = Tick64();
  380. while ((start_time + (UINT64)CLIENT_WAIT_CN_READY_TIMEOUT) >= Tick64())
  381. {
  382. if (CnIsCnServiceReady())
  383. {
  384. break;
  385. }
  386. SleepThread(100);
  387. }
  388. }
  389. // Check whether preparation of notification service completed
  390. bool CnIsCnServiceReady()
  391. {
  392. SOCK *s;
  393. // Confirm running the notification service
  394. if (CnCheckAlreadyExists(false) == false)
  395. {
  396. // Not running
  397. return false;
  398. }
  399. // Try to connect to the TCP port
  400. s = ConnectEx("localhost", CLIENT_NOTIFY_PORT, 500);
  401. if (s == NULL)
  402. {
  403. // The TCP port is not opened
  404. return false;
  405. }
  406. Disconnect(s);
  407. ReleaseSock(s);
  408. // Running
  409. return true;
  410. }
  411. // Check whether the notification service is already running
  412. bool CnCheckAlreadyExists(bool lock)
  413. {
  414. bool ret = false;
  415. #ifdef OS_WIN32
  416. ret = Win32CnCheckAlreadyExists(lock);
  417. #endif
  418. return ret;
  419. }
  420. typedef struct CNC_STATUS_PRINTER_WINDOW_PARAM
  421. {
  422. THREAD *Thread;
  423. SESSION *Session;
  424. SOCK *Sock;
  425. } CNC_STATUS_PRINTER_WINDOW_PARAM;
  426. typedef struct CNC_CONNECT_ERROR_DLG_THREAD_PARAM
  427. {
  428. SESSION *Session;
  429. SOCK *Sock;
  430. bool HaltThread;
  431. EVENT *Event;
  432. } CNC_CONNECT_ERROR_DLG_THREAD_PARAM;
  433. // Get the file name of vpnclient.exe in Win32
  434. char *CiGetVpnClientExeFileName()
  435. {
  436. if (Is64() == false)
  437. {
  438. return CLIENT_WIN32_EXE_FILENAME;
  439. }
  440. else
  441. {
  442. if (IsX64())
  443. {
  444. return CLIENT_WIN32_EXE_FILENAME_X64;
  445. }
  446. else
  447. {
  448. return CLIENT_WIN32_EXE_FILENAME_IA64;
  449. }
  450. }
  451. }
  452. // Thread to stop forcibly the Certificate check dialog client
  453. void CncCheckCertHaltThread(THREAD *thread, void *param)
  454. {
  455. CNC_CONNECT_ERROR_DLG_THREAD_PARAM *dp = (CNC_CONNECT_ERROR_DLG_THREAD_PARAM *)param;
  456. // Validate arguments
  457. if (thread == NULL || param == NULL)
  458. {
  459. return;
  460. }
  461. while (true)
  462. {
  463. if (dp->Session->Halt || dp->HaltThread)
  464. {
  465. break;
  466. }
  467. Wait(dp->Event, 100);
  468. }
  469. Disconnect(dp->Sock);
  470. }
  471. // Show the certification check dialog
  472. void CncCheckCert(SESSION *session, UI_CHECKCERT *dlg)
  473. {
  474. SOCK *s;
  475. PACK *p;
  476. CNC_CONNECT_ERROR_DLG_THREAD_PARAM *dp;
  477. THREAD *t;
  478. // Validate arguments
  479. if (dlg == NULL || session == NULL)
  480. {
  481. return;
  482. }
  483. s = CncConnect();
  484. if (s == NULL)
  485. {
  486. return;
  487. }
  488. p = NewPack();
  489. PackAddStr(p, "function", "check_cert");
  490. PackAddUniStr(p, "AccountName", dlg->AccountName);
  491. PackAddStr(p, "ServerName", dlg->ServerName);
  492. PackAddX(p, "x", dlg->x);
  493. PackAddX(p, "parent_x", dlg->parent_x);
  494. PackAddX(p, "old_x", dlg->old_x);
  495. PackAddBool(p, "DiffWarning", dlg->DiffWarning);
  496. PackAddBool(p, "Ok", dlg->Ok);
  497. PackAddBool(p, "SaveServerCert", dlg->SaveServerCert);
  498. SendPack(s, p);
  499. FreePack(p);
  500. dp = ZeroMalloc(sizeof(CNC_CONNECT_ERROR_DLG_THREAD_PARAM));
  501. dp->Sock = s;
  502. dp->Event = NewEvent();
  503. dp->Session = session;
  504. t = NewThread(CncCheckCertHaltThread, dp);
  505. p = RecvPack(s);
  506. if (p != NULL)
  507. {
  508. dlg->Ok = PackGetBool(p, "Ok");
  509. dlg->DiffWarning = PackGetBool(p, "DiffWarning");
  510. dlg->SaveServerCert = PackGetBool(p, "SaveServerCert");
  511. FreePack(p);
  512. }
  513. dp->HaltThread = true;
  514. Set(dp->Event);
  515. WaitThread(t, INFINITE);
  516. ReleaseEvent(dp->Event);
  517. Free(dp);
  518. ReleaseThread(t);
  519. Disconnect(s);
  520. ReleaseSock(s);
  521. }
  522. // Smart card signature dialog
  523. bool CncSecureSignDlg(SECURE_SIGN *sign)
  524. {
  525. SOCK *s;
  526. PACK *p;
  527. bool ret = false;
  528. // Validate arguments
  529. if (sign == NULL)
  530. {
  531. return false;
  532. }
  533. s = CncConnect();
  534. if (s == NULL)
  535. {
  536. return false;
  537. }
  538. p = NewPack();
  539. PackAddStr(p, "function", "secure_sign");
  540. OutRpcSecureSign(p, sign);
  541. SendPack(s, p);
  542. FreePack(p);
  543. p = RecvPack(s);
  544. if (p != NULL)
  545. {
  546. ret = PackGetBool(p, "ret");
  547. if (ret)
  548. {
  549. FreeRpcSecureSign(sign);
  550. Zero(sign, sizeof(SECURE_SIGN));
  551. InRpcSecureSign(sign, p);
  552. }
  553. FreePack(p);
  554. }
  555. Disconnect(s);
  556. ReleaseSock(s);
  557. return ret;
  558. }
  559. // Show the NIC information dialog
  560. SOCK *CncNicInfo(UI_NICINFO *info)
  561. {
  562. SOCK *s;
  563. PACK *p;
  564. bool ret = false;
  565. // Validate arguments
  566. if (info == NULL)
  567. {
  568. return NULL;
  569. }
  570. s = CncConnectEx(200);
  571. if (s == NULL)
  572. {
  573. return NULL;
  574. }
  575. p = NewPack();
  576. PackAddStr(p, "function", "nicinfo");
  577. PackAddStr(p, "NicName", info->NicName);
  578. PackAddUniStr(p, "AccountName", info->AccountName);
  579. SendPack(s, p);
  580. FreePack(p);
  581. return s;
  582. }
  583. // Close the NIC information dialog
  584. void CncNicInfoFree(SOCK *s)
  585. {
  586. // Validate arguments
  587. if (s == NULL)
  588. {
  589. return;
  590. }
  591. Disconnect(s);
  592. ReleaseSock(s);
  593. }
  594. // Show the message dialog
  595. SOCK *CncMsgDlg(UI_MSG_DLG *dlg)
  596. {
  597. SOCK *s;
  598. PACK *p;
  599. bool ret = false;
  600. char *utf;
  601. // Validate arguments
  602. if (dlg == NULL)
  603. {
  604. return NULL;
  605. }
  606. s = CncConnectEx(200);
  607. if (s == NULL)
  608. {
  609. return NULL;
  610. }
  611. p = NewPack();
  612. PackAddStr(p, "function", "msg_dialog");
  613. PackAddStr(p, "ServerName", dlg->ServerName);
  614. PackAddStr(p, "HubName", dlg->HubName);
  615. utf = CopyUniToUtf(dlg->Msg);
  616. PackAddData(p, "Msg", utf, StrLen(utf));
  617. Free(utf);
  618. SendPack(s, p);
  619. FreePack(p);
  620. return s;
  621. }
  622. // Close the message dialog
  623. void CndMsgDlgFree(SOCK *s)
  624. {
  625. // Validate arguments
  626. if (s == NULL)
  627. {
  628. return;
  629. }
  630. Disconnect(s);
  631. ReleaseSock(s);
  632. }
  633. // The thread to stop the password input dialog client forcibly
  634. void CncPasswordDlgHaltThread(THREAD *thread, void *param)
  635. {
  636. CNC_CONNECT_ERROR_DLG_THREAD_PARAM *dp = (CNC_CONNECT_ERROR_DLG_THREAD_PARAM *)param;
  637. // Validate arguments
  638. if (thread == NULL || param == NULL)
  639. {
  640. return;
  641. }
  642. while (true)
  643. {
  644. if (dp->Session->Halt || dp->HaltThread)
  645. {
  646. break;
  647. }
  648. Wait(dp->Event, 100);
  649. }
  650. Disconnect(dp->Sock);
  651. }
  652. // Show the password input dialog
  653. bool CncPasswordDlg(SESSION *session, UI_PASSWORD_DLG *dlg)
  654. {
  655. SOCK *s;
  656. PACK *p;
  657. CNC_CONNECT_ERROR_DLG_THREAD_PARAM *dp;
  658. THREAD *t;
  659. bool ret = false;
  660. // Validate arguments
  661. if (dlg == NULL || session == NULL)
  662. {
  663. return false;
  664. }
  665. s = CncConnect();
  666. if (s == NULL)
  667. {
  668. Wait(session->HaltEvent, session->RetryInterval);
  669. return true;
  670. }
  671. p = NewPack();
  672. PackAddStr(p, "function", "password_dialog");
  673. PackAddInt(p, "Type", dlg->Type);
  674. PackAddStr(p, "Username", dlg->Username);
  675. PackAddStr(p, "Password", dlg->Password);
  676. PackAddStr(p, "ServerName", dlg->ServerName);
  677. PackAddInt(p, "RetryIntervalSec", dlg->RetryIntervalSec);
  678. PackAddBool(p, "ProxyServer", dlg->ProxyServer);
  679. PackAddBool(p, "AdminMode", dlg->AdminMode);
  680. PackAddBool(p, "ShowNoSavePassword", dlg->ShowNoSavePassword);
  681. PackAddBool(p, "NoSavePassword", dlg->NoSavePassword);
  682. SendPack(s, p);
  683. FreePack(p);
  684. dp = ZeroMalloc(sizeof(CNC_CONNECT_ERROR_DLG_THREAD_PARAM));
  685. dp->Session = session;
  686. dp->Sock = s;
  687. dp->Event = NewEvent();
  688. t = NewThread(CncConnectErrorDlgHaltThread, dp);
  689. p = RecvPack(s);
  690. if (p != NULL)
  691. {
  692. ret = PackGetBool(p, "ok");
  693. dlg->NoSavePassword = PackGetBool(p, "NoSavePassword");
  694. dlg->ProxyServer = PackGetBool(p, "ProxyServer");
  695. dlg->Type = PackGetInt(p, "Type");
  696. PackGetStr(p, "Username", dlg->Username, sizeof(dlg->Username));
  697. PackGetStr(p, "Password", dlg->Password, sizeof(dlg->Password));
  698. FreePack(p);
  699. }
  700. dp->HaltThread = true;
  701. Set(dp->Event);
  702. WaitThread(t, INFINITE);
  703. ReleaseEvent(dp->Event);
  704. Free(dp);
  705. ReleaseThread(t);
  706. Disconnect(s);
  707. ReleaseSock(s);
  708. return ret;
  709. }
  710. // Thread to stop the connection error dialog client forcibly
  711. void CncConnectErrorDlgHaltThread(THREAD *thread, void *param)
  712. {
  713. CNC_CONNECT_ERROR_DLG_THREAD_PARAM *dp = (CNC_CONNECT_ERROR_DLG_THREAD_PARAM *)param;
  714. // Validate arguments
  715. if (thread == NULL || param == NULL)
  716. {
  717. return;
  718. }
  719. while (true)
  720. {
  721. if (dp->Session->Halt || dp->HaltThread)
  722. {
  723. break;
  724. }
  725. Wait(dp->Event, 100);
  726. }
  727. Disconnect(dp->Sock);
  728. }
  729. // Show the connection error dialog
  730. bool CncConnectErrorDlg(SESSION *session, UI_CONNECTERROR_DLG *dlg)
  731. {
  732. SOCK *s;
  733. PACK *p;
  734. CNC_CONNECT_ERROR_DLG_THREAD_PARAM *dp;
  735. THREAD *t;
  736. bool ret = false;
  737. // Validate arguments
  738. if (dlg == NULL || session == NULL)
  739. {
  740. return false;
  741. }
  742. s = CncConnect();
  743. if (s == NULL)
  744. {
  745. Wait(session->HaltEvent, session->RetryInterval);
  746. return true;
  747. }
  748. p = NewPack();
  749. PackAddStr(p, "function", "connecterror_dialog");
  750. PackAddUniStr(p, "AccountName", dlg->AccountName);
  751. PackAddStr(p, "ServerName", dlg->ServerName);
  752. PackAddInt(p, "Err", dlg->Err);
  753. PackAddInt(p, "CurrentRetryCount", dlg->CurrentRetryCount);
  754. PackAddInt(p, "RetryLimit", dlg->RetryLimit);
  755. PackAddInt(p, "RetryIntervalSec", dlg->RetryIntervalSec);
  756. PackAddBool(p, "HideWindow", dlg->HideWindow);
  757. SendPack(s, p);
  758. FreePack(p);
  759. dp = ZeroMalloc(sizeof(CNC_CONNECT_ERROR_DLG_THREAD_PARAM));
  760. dp->Session = session;
  761. dp->Sock = s;
  762. dp->Event = NewEvent();
  763. t = NewThread(CncConnectErrorDlgHaltThread, dp);
  764. p = RecvPack(s);
  765. if (p != NULL)
  766. {
  767. ret = PackGetBool(p, "ok");
  768. dlg->HideWindow = PackGetBool(p, "HideWindow");
  769. FreePack(p);
  770. }
  771. dp->HaltThread = true;
  772. Set(dp->Event);
  773. WaitThread(t, INFINITE);
  774. ReleaseEvent(dp->Event);
  775. Free(dp);
  776. ReleaseThread(t);
  777. Disconnect(s);
  778. ReleaseSock(s);
  779. return ret;
  780. }
  781. // Thread for the status indicator client
  782. void CncStatusPrinterWindowThreadProc(THREAD *thread, void *param)
  783. {
  784. CNC_STATUS_PRINTER_WINDOW_PARAM *pp;
  785. SOCK *sock;
  786. PACK *p;
  787. // Validate arguments
  788. if (thread == NULL || param == NULL)
  789. {
  790. return;
  791. }
  792. pp = (CNC_STATUS_PRINTER_WINDOW_PARAM *)param;
  793. sock = pp->Sock;
  794. pp->Thread = thread;
  795. AddRef(pp->Thread->ref);
  796. NoticeThreadInit(thread);
  797. p = RecvPack(sock);
  798. if (p != NULL)
  799. {
  800. // Stop the session
  801. StopSessionEx(pp->Session, true);
  802. FreePack(p);
  803. }
  804. }
  805. // Create a status indicator client
  806. SOCK *CncStatusPrinterWindowStart(SESSION *s)
  807. {
  808. SOCK *sock;
  809. PACK *p;
  810. THREAD *t;
  811. CNC_STATUS_PRINTER_WINDOW_PARAM *param;
  812. // Validate arguments
  813. if (s == NULL)
  814. {
  815. return NULL;
  816. }
  817. sock = CncConnect();
  818. if (sock == NULL)
  819. {
  820. return NULL;
  821. }
  822. p = NewPack();
  823. PackAddStr(p, "function", "status_printer");
  824. PackAddUniStr(p, "account_name", s->Account->ClientOption->AccountName);
  825. if (SendPack(sock, p) == false)
  826. {
  827. FreePack(p);
  828. ReleaseSock(sock);
  829. return NULL;
  830. }
  831. FreePack(p);
  832. param = ZeroMalloc(sizeof(CNC_STATUS_PRINTER_WINDOW_PARAM));
  833. param->Sock = sock;
  834. param->Session = s;
  835. sock->Param = param;
  836. t = NewThread(CncStatusPrinterWindowThreadProc, param);
  837. WaitThreadInit(t);
  838. ReleaseThread(t);
  839. return sock;
  840. }
  841. // Send a string to the status indicator
  842. void CncStatusPrinterWindowPrint(SOCK *s, wchar_t *str)
  843. {
  844. CNC_STATUS_PRINTER_WINDOW_PARAM *param;
  845. PACK *p;
  846. // Validate arguments
  847. if (s == NULL || str == NULL)
  848. {
  849. return;
  850. }
  851. param = (CNC_STATUS_PRINTER_WINDOW_PARAM *)s->Param;
  852. p = NewPack();
  853. PackAddUniStr(p, "string", str);
  854. SendPack(s, p);
  855. FreePack(p);
  856. }
  857. // Stop the status indicator client
  858. void CncStatusPrinterWindowStop(SOCK *s)
  859. {
  860. CNC_STATUS_PRINTER_WINDOW_PARAM *param;
  861. // Validate arguments
  862. if (s == NULL)
  863. {
  864. return;
  865. }
  866. param = (CNC_STATUS_PRINTER_WINDOW_PARAM *)s->Param;
  867. // Disconnect the client socket
  868. Disconnect(s);
  869. // Terminate the thread
  870. WaitThread(param->Thread, INFINITE);
  871. ReleaseThread(param->Thread);
  872. Free(param);
  873. ReleaseSock(s);
  874. }
  875. // Start the driver installer for Windows Vista
  876. bool CncExecDriverInstaller(char *arg)
  877. {
  878. SOCK *s = CncConnect();
  879. PACK *p;
  880. bool ret;
  881. if (s == NULL)
  882. {
  883. return false;
  884. }
  885. p = NewPack();
  886. PackAddStr(p, "function", "exec_driver_installer");
  887. PackAddStr(p, "arg", arg);
  888. SendPack(s, p);
  889. FreePack(p);
  890. p = RecvPack(s);
  891. if (p == NULL)
  892. {
  893. Disconnect(s);
  894. ReleaseSock(s);
  895. return false;
  896. }
  897. ret = PackGetBool(p, "ret");
  898. FreePack(p);
  899. Disconnect(s);
  900. ReleaseSock(s);
  901. return ret;
  902. }
  903. // Let the current running client notification services releasing the socket
  904. void CncReleaseSocket()
  905. {
  906. SOCK *s = CncConnect();
  907. PACK *p;
  908. if (s == NULL)
  909. {
  910. return;
  911. }
  912. p = NewPack();
  913. PackAddStr(p, "function", "release_socket");
  914. #ifdef OS_WIN32
  915. PackAddInt(p, "pid", MsGetProcessId());
  916. #endif // OS_WIN32
  917. SendPack(s, p);
  918. FreePack(p);
  919. Disconnect(s);
  920. ReleaseSock(s);
  921. }
  922. // Get the Session ID of the client notification service
  923. UINT CncGetSessionId()
  924. {
  925. SOCK *s = CncConnect();
  926. PACK *p;
  927. UINT ret;
  928. if (s == NULL)
  929. {
  930. return INFINITE;
  931. }
  932. p = NewPack();
  933. PackAddStr(p, "function", "get_session_id");
  934. SendPack(s, p);
  935. FreePack(p);
  936. p = RecvPack(s);
  937. if (p == NULL)
  938. {
  939. Disconnect(s);
  940. ReleaseSock(s);
  941. return INFINITE;
  942. }
  943. ret = PackGetInt(p, "session_id");
  944. FreePack(p);
  945. Disconnect(s);
  946. ReleaseSock(s);
  947. return ret;
  948. }
  949. // Terminate the process of the client notification service
  950. void CncExit()
  951. {
  952. SOCK *s = CncConnectEx(256);
  953. PACK *p;
  954. if (s != NULL)
  955. {
  956. p = NewPack();
  957. PackAddStr(p, "function", "exit");
  958. SendPack(s, p);
  959. FreePack(p);
  960. FreePack(RecvPack(s));
  961. Disconnect(s);
  962. ReleaseSock(s);
  963. }
  964. #ifdef OS_WIN32
  965. MsKillOtherInstanceEx("vpnclient");
  966. #endif // OS_WIN32
  967. }
  968. // Connect to the client notification service
  969. SOCK *CncConnect()
  970. {
  971. return CncConnectEx(0);
  972. }
  973. SOCK *CncConnectEx(UINT timeout)
  974. {
  975. SOCK *s = ConnectEx("localhost", CLIENT_NOTIFY_PORT, timeout);
  976. return s;
  977. }
  978. #ifdef OS_WIN32
  979. // Thread for the certificate check dialog
  980. void Win32CnCheckCertThreadProc(THREAD *thread, void *param)
  981. {
  982. UI_CHECKCERT *dlg;
  983. // Validate arguments
  984. if (thread == NULL || param == NULL)
  985. {
  986. return;
  987. }
  988. dlg = (UI_CHECKCERT *)param;
  989. CheckCertDlg(dlg);
  990. {
  991. PACK *p = NewPack();
  992. PackAddBool(p, "Ok", dlg->Ok);
  993. PackAddBool(p, "SaveServerCert", dlg->SaveServerCert);
  994. SendPack(dlg->Sock, p);
  995. FreePack(p);
  996. FreePack(RecvPack(dlg->Sock));
  997. }
  998. Disconnect(dlg->Sock);
  999. }
  1000. // Certificate check dialog
  1001. void Win32CnCheckCert(SOCK *s, PACK *p)
  1002. {
  1003. UI_CHECKCERT dlg;
  1004. THREAD *t;
  1005. Zero(&dlg, sizeof(dlg));
  1006. // Validate arguments
  1007. if (s == NULL || p == NULL)
  1008. {
  1009. return;
  1010. }
  1011. PackGetUniStr(p, "AccountName", dlg.AccountName, sizeof(dlg.AccountName));
  1012. PackGetStr(p, "ServerName", dlg.ServerName, sizeof(dlg.ServerName));
  1013. dlg.x = PackGetX(p, "x");
  1014. dlg.parent_x = PackGetX(p, "parent_x");
  1015. dlg.old_x = PackGetX(p, "old_x");
  1016. dlg.DiffWarning = PackGetBool(p, "DiffWarning");
  1017. dlg.Ok = PackGetBool(p, "Ok");
  1018. dlg.SaveServerCert = PackGetBool(p, "SaveServerCert");
  1019. dlg.Sock = s;
  1020. t = NewThread(Win32CnCheckCertThreadProc, &dlg);
  1021. FreePack(RecvPack(s));
  1022. dlg.Halt = true;
  1023. WaitThread(t, INFINITE);
  1024. ReleaseThread(t);
  1025. FreeX(dlg.parent_x);
  1026. FreeX(dlg.old_x);
  1027. FreeX(dlg.x);
  1028. }
  1029. // Message display dialog thread procedure
  1030. void Win32CnMsgDlgThreadProc(THREAD *thread, void *param)
  1031. {
  1032. UI_MSG_DLG *dlg = (UI_MSG_DLG *)param;
  1033. wchar_t tmp[MAX_SIZE];
  1034. char url[MAX_SIZE];
  1035. // Validate arguments
  1036. if (thread == NULL || dlg == NULL)
  1037. {
  1038. return;
  1039. }
  1040. UniFormat(tmp, sizeof(tmp), _UU("CM_MSG_TITLE"),
  1041. dlg->ServerName, dlg->HubName);
  1042. if (IsURLMsg(dlg->Msg, url, sizeof(url)) == false)
  1043. {
  1044. OnceMsgEx(NULL, tmp, dlg->Msg, true, 167, &dlg->Halt);
  1045. }
  1046. else
  1047. {
  1048. if (MsExecute(url, NULL) == false)
  1049. {
  1050. OnceMsgEx(NULL, tmp, dlg->Msg, true, 167, &dlg->Halt);
  1051. }
  1052. }
  1053. Disconnect(dlg->Sock);
  1054. }
  1055. // NIC information dialog thread procedure
  1056. void Win32CnNicInfoThreadProc(THREAD *thread, void *param)
  1057. {
  1058. UI_NICINFO *info = (UI_NICINFO *)param;
  1059. // Validate arguments
  1060. if (thread == NULL || info == NULL)
  1061. {
  1062. return;
  1063. }
  1064. if (MsIsNt())
  1065. {
  1066. // Do not show a dialog on Windows 9x system
  1067. NicInfo(info);
  1068. }
  1069. Disconnect(info->Sock);
  1070. }
  1071. // NIC information dialog
  1072. void Win32CnNicInfo(SOCK *s, PACK *p)
  1073. {
  1074. UI_NICINFO info;
  1075. THREAD *t;
  1076. Zero(&info, sizeof(info));
  1077. // Validate arguments
  1078. if (s == NULL || p == NULL)
  1079. {
  1080. return;
  1081. }
  1082. PackGetStr(p, "NicName", info.NicName, sizeof(info.NicName));
  1083. PackGetUniStr(p, "AccountName", info.AccountName, sizeof(info.AccountName));
  1084. info.Sock = s;
  1085. t = NewThread(Win32CnNicInfoThreadProc, &info);
  1086. FreePack(RecvPack(s));
  1087. info.Halt = true;
  1088. WaitThread(t, INFINITE);
  1089. ReleaseThread(t);
  1090. }
  1091. // Message display dialog
  1092. void Win32CnMsgDlg(SOCK *s, PACK *p)
  1093. {
  1094. UI_MSG_DLG dlg;
  1095. THREAD *t;
  1096. UINT utf_size;
  1097. char *utf;
  1098. wchar_t *msg;
  1099. Zero(&dlg, sizeof(dlg));
  1100. // Validate arguments
  1101. if (s == NULL || p == NULL)
  1102. {
  1103. return;
  1104. }
  1105. PackGetStr(p, "ServerName", dlg.ServerName, sizeof(dlg.ServerName));
  1106. PackGetStr(p, "HubName", dlg.HubName, sizeof(dlg.HubName));
  1107. utf_size = PackGetDataSize(p, "Msg");
  1108. utf = ZeroMalloc(utf_size + 8);
  1109. PackGetData(p, "Msg", utf);
  1110. msg = CopyUtfToUni(utf);
  1111. Free(utf);
  1112. dlg.Sock = s;
  1113. dlg.Msg = msg;
  1114. t = NewThread(Win32CnMsgDlgThreadProc, &dlg);
  1115. FreePack(RecvPack(s));
  1116. dlg.Halt = true;
  1117. WaitThread(t, INFINITE);
  1118. ReleaseThread(t);
  1119. Free(msg);
  1120. }
  1121. // Thread for Password input dialog
  1122. void Win32CnPasswordDlgThreadProc(THREAD *thread, void *param)
  1123. {
  1124. UI_PASSWORD_DLG *dlg;
  1125. // Validate arguments
  1126. if (thread == NULL || param == NULL)
  1127. {
  1128. return;
  1129. }
  1130. dlg = (UI_PASSWORD_DLG *)param;
  1131. if (PasswordDlg(NULL, dlg))
  1132. {
  1133. PACK *p = NewPack();
  1134. PackAddBool(p, "ok", true);
  1135. PackAddStr(p, "Username", dlg->Username);
  1136. PackAddStr(p, "Password", dlg->Password);
  1137. PackAddInt(p, "Type", dlg->Type);
  1138. PackAddBool(p, "ProxyServer", dlg->ProxyServer);
  1139. PackAddBool(p, "NoSavePassword", dlg->NoSavePassword);
  1140. SendPack(dlg->Sock, p);
  1141. FreePack(p);
  1142. FreePack(RecvPack(dlg->Sock));
  1143. }
  1144. Disconnect(dlg->Sock);
  1145. }
  1146. // Password input dialog
  1147. void Win32CnPasswordDlg(SOCK *s, PACK *p)
  1148. {
  1149. UI_PASSWORD_DLG dlg;
  1150. THREAD *t = NULL;
  1151. Zero(&dlg, sizeof(dlg));
  1152. // Validate arguments
  1153. if (s == NULL || p == NULL)
  1154. {
  1155. return;
  1156. }
  1157. dlg.Type = PackGetInt(p, "Type");
  1158. PackGetStr(p, "Username", dlg.Username, sizeof(dlg.Username));
  1159. PackGetStr(p, "Password", dlg.Password, sizeof(dlg.Password));
  1160. PackGetStr(p, "ServerName", dlg.ServerName, sizeof(dlg.ServerName));
  1161. dlg.RetryIntervalSec = PackGetInt(p, "RetryIntervalSec");
  1162. dlg.ProxyServer = PackGetBool(p, "ProxyServer");
  1163. dlg.AdminMode = PackGetBool(p, "AdminMode");
  1164. dlg.ShowNoSavePassword = PackGetBool(p, "ShowNoSavePassword");
  1165. dlg.NoSavePassword = PackGetBool(p, "NoSavePassword");
  1166. dlg.CancelEvent = NewEvent();
  1167. dlg.Sock = s;
  1168. t = NewThread(Win32CnPasswordDlgThreadProc, &dlg);
  1169. FreePack(RecvPack(s));
  1170. Set(dlg.CancelEvent);
  1171. WaitThread(t, INFINITE);
  1172. ReleaseEvent(dlg.CancelEvent);
  1173. ReleaseThread(t);
  1174. }
  1175. // Thread for the connection error dialog
  1176. void Win32CnConnectErrorDlgThreadProc(THREAD *thread, void *param)
  1177. {
  1178. UI_CONNECTERROR_DLG *dlg;
  1179. // Validate arguments
  1180. if (thread == NULL || param == NULL)
  1181. {
  1182. return;
  1183. }
  1184. dlg = (UI_CONNECTERROR_DLG *)param;
  1185. if (ConnectErrorDlg(dlg))
  1186. {
  1187. PACK *p = NewPack();
  1188. PackAddBool(p, "ok", true);
  1189. PackAddBool(p, "HideWindow", dlg->HideWindow);
  1190. SendPack(dlg->Sock, p);
  1191. FreePack(p);
  1192. FreePack(RecvPack(dlg->Sock));
  1193. }
  1194. Disconnect(dlg->Sock);
  1195. }
  1196. // Connection Error dialog (Win32)
  1197. void Win32CnConnectErrorDlg(SOCK *s, PACK *p)
  1198. {
  1199. UI_CONNECTERROR_DLG dlg;
  1200. THREAD *t;
  1201. Zero(&dlg, sizeof(dlg));
  1202. // Validate arguments
  1203. if (s == NULL || p == NULL)
  1204. {
  1205. return;
  1206. }
  1207. PackGetUniStr(p, "AccountName", dlg.AccountName, sizeof(dlg.AccountName));
  1208. PackGetStr(p, "ServerName", dlg.ServerName, sizeof(dlg.ServerName));
  1209. dlg.Err = PackGetInt(p, "Err");
  1210. dlg.CurrentRetryCount = PackGetInt(p, "CurrentRetryCount");
  1211. dlg.RetryLimit = PackGetInt(p, "RetryLimit");
  1212. dlg.RetryIntervalSec = PackGetInt(p, "RetryIntervalSec");
  1213. dlg.HideWindow = PackGetBool(p, "HideWindow");
  1214. dlg.CancelEvent = NewEvent();
  1215. dlg.Sock = s;
  1216. t = NewThread(Win32CnConnectErrorDlgThreadProc, &dlg);
  1217. FreePack(RecvPack(s));
  1218. Set(dlg.CancelEvent);
  1219. WaitThread(t, INFINITE);
  1220. ReleaseEvent(dlg.CancelEvent);
  1221. ReleaseThread(t);
  1222. }
  1223. // Status indicator (Win32)
  1224. void Win32CnStatusPrinter(SOCK *s, PACK *p)
  1225. {
  1226. STATUS_WINDOW *w;
  1227. wchar_t account_name[MAX_ACCOUNT_NAME_LEN + 1];
  1228. // Validate arguments
  1229. if (s == NULL || p == NULL)
  1230. {
  1231. return;
  1232. }
  1233. PackGetUniStr(p, "account_name", account_name, sizeof(account_name));
  1234. w = StatusPrinterWindowStart(s, account_name);
  1235. while (true)
  1236. {
  1237. PACK *p = RecvPack(s);
  1238. if (p == NULL)
  1239. {
  1240. // Exit the dialog because it is disconnected
  1241. break;
  1242. }
  1243. else
  1244. {
  1245. wchar_t tmp[MAX_SIZE];
  1246. // Rewrite the string
  1247. PackGetUniStr(p, "string", tmp, sizeof(tmp));
  1248. StatusPrinterWindowPrint(w, tmp);
  1249. FreePack(p);
  1250. }
  1251. }
  1252. StatusPrinterWindowStop(w);
  1253. }
  1254. // Start the driver installer (for Windows Vista)
  1255. void Win32CnExecDriverInstaller(SOCK *s, PACK *p)
  1256. {
  1257. char arg[MAX_SIZE];
  1258. bool ret;
  1259. void *helper = NULL;
  1260. // Validate arguments
  1261. if (s == NULL || p == NULL)
  1262. {
  1263. return;
  1264. }
  1265. if (PackGetStr(p, "arg", arg, sizeof(arg)) == false)
  1266. {
  1267. return;
  1268. }
  1269. if (MsIsVista())
  1270. {
  1271. helper = CmStartUacHelper();
  1272. }
  1273. ret = MsExecDriverInstaller(arg);
  1274. CmStopUacHelper(helper);
  1275. p = NewPack();
  1276. PackAddBool(p, "ret", ret);
  1277. SendPack(s, p);
  1278. FreePack(p);
  1279. }
  1280. #endif // OS_WIN32
  1281. // Start the driver installer
  1282. void CnExecDriverInstaller(SOCK *s, PACK *p)
  1283. {
  1284. // Validate arguments
  1285. if (s == NULL || p == NULL)
  1286. {
  1287. return;
  1288. }
  1289. #ifdef OS_WIN32
  1290. Win32CnExecDriverInstaller(s, p);
  1291. #endif // OS_WIN32
  1292. }
  1293. // Certificate confirmation dialog
  1294. void CnCheckCert(SOCK *s, PACK *p)
  1295. {
  1296. // Validate arguments
  1297. if (s == NULL || p == NULL)
  1298. {
  1299. return;
  1300. }
  1301. #ifdef OS_WIN32
  1302. Win32CnCheckCert(s, p);
  1303. #endif // OS_WIN32
  1304. }
  1305. // NIC information dialog
  1306. void CnNicInfo(SOCK *s, PACK *p)
  1307. {
  1308. // Validate arguments
  1309. if (s == NULL || p == NULL)
  1310. {
  1311. return;
  1312. }
  1313. #ifdef OS_WIN32
  1314. Win32CnNicInfo(s, p);
  1315. #endif // OS_WIN32
  1316. }
  1317. // Message display dialog
  1318. void CnMsgDlg(SOCK *s, PACK *p)
  1319. {
  1320. // Validate arguments
  1321. if (s == NULL || p == NULL)
  1322. {
  1323. return;
  1324. }
  1325. #ifdef OS_WIN32
  1326. Win32CnMsgDlg(s, p);
  1327. #endif // OS_WIN32
  1328. }
  1329. // Password input dialog
  1330. void CnPasswordDlg(SOCK *s, PACK *p)
  1331. {
  1332. // Validate arguments
  1333. if (s == NULL || p == NULL)
  1334. {
  1335. return;
  1336. }
  1337. #ifdef OS_WIN32
  1338. Win32CnPasswordDlg(s, p);
  1339. #endif // OS_WIN32
  1340. }
  1341. // Connection Error dialog
  1342. void CnConnectErrorDlg(SOCK *s, PACK *p)
  1343. {
  1344. // Validate arguments
  1345. if (s == NULL || p == NULL)
  1346. {
  1347. return;
  1348. }
  1349. #ifdef OS_WIN32
  1350. Win32CnConnectErrorDlg(s, p);
  1351. #endif // OS_WIN32
  1352. }
  1353. // Status indicator
  1354. void CnStatusPrinter(SOCK *s, PACK *p)
  1355. {
  1356. // Validate arguments
  1357. if (s == NULL || p == NULL)
  1358. {
  1359. return;
  1360. }
  1361. #ifdef OS_WIN32
  1362. Win32CnStatusPrinter(s, p);
  1363. #endif // OS_WIN32
  1364. }
  1365. // Client notification service listener thread
  1366. void CnListenerProc(THREAD *thread, void *param)
  1367. {
  1368. TCP_ACCEPTED_PARAM *data = (TCP_ACCEPTED_PARAM *)param;
  1369. SOCK *s;
  1370. PACK *p;
  1371. // Validate arguments
  1372. if (data == NULL || thread == NULL)
  1373. {
  1374. return;
  1375. }
  1376. #ifdef OS_WIN32
  1377. //Set Application ID
  1378. JL_SetCurrentProcessExplicitAppUserModelID(APPID_CM);
  1379. #endif // OS_WIN32
  1380. s = data->s;
  1381. AddRef(s->ref);
  1382. NoticeThreadInit(thread);
  1383. if (s->LocalIP.addr[0] == 127)
  1384. {
  1385. p = RecvPack(s);
  1386. if (p != NULL)
  1387. {
  1388. char function[MAX_SIZE];
  1389. if (PackGetStr(p, "function", function, sizeof(function)))
  1390. {
  1391. if (StrCmpi(function, "status_printer") == 0)
  1392. {
  1393. CnStatusPrinter(s, p);
  1394. }
  1395. else if (StrCmpi(function, "connecterror_dialog") == 0)
  1396. {
  1397. CnConnectErrorDlg(s, p);
  1398. }
  1399. else if (StrCmpi(function, "msg_dialog") == 0)
  1400. {
  1401. CnMsgDlg(s, p);
  1402. }
  1403. else if (StrCmpi(function, "nicinfo") == 0)
  1404. {
  1405. CnNicInfo(s, p);
  1406. }
  1407. else if (StrCmpi(function, "password_dialog") == 0)
  1408. {
  1409. CnPasswordDlg(s, p);
  1410. }
  1411. else if (StrCmpi(function, "secure_sign") == 0)
  1412. {
  1413. CnSecureSign(s, p);
  1414. }
  1415. else if (StrCmpi(function, "check_cert") == 0)
  1416. {
  1417. CnCheckCert(s, p);
  1418. }
  1419. else if (StrCmpi(function, "exit") == 0)
  1420. {
  1421. #ifdef OS_WIN32
  1422. MsTerminateProcess();
  1423. #else // OS_WIN32
  1424. _exit(0);
  1425. #endif // OS_WIN32
  1426. }
  1427. else if (StrCmpi(function, "get_session_id") == 0)
  1428. {
  1429. PACK *p = NewPack();
  1430. #ifdef OS_WIN32
  1431. PackAddInt(p, "session_id", MsGetCurrentTerminalSessionId());
  1432. #endif // OS_WIN32
  1433. SendPack(s, p);
  1434. FreePack(p);
  1435. }
  1436. else if (StrCmpi(function, "exec_driver_installer") == 0)
  1437. {
  1438. CnExecDriverInstaller(s, p);
  1439. }
  1440. else if (StrCmpi(function, "release_socket") == 0)
  1441. {
  1442. // Stop the listener
  1443. CnReleaseSocket(s, p);
  1444. }
  1445. }
  1446. FreePack(p);
  1447. }
  1448. }
  1449. Disconnect(s);
  1450. ReleaseSock(s);
  1451. }
  1452. // Do the Secure Sign
  1453. void CnSecureSign(SOCK *s, PACK *p)
  1454. {
  1455. SECURE_SIGN sign;
  1456. bool ret = false;
  1457. // Validate arguments
  1458. if (s == NULL || p == NULL)
  1459. {
  1460. return;
  1461. }
  1462. Zero(&sign, sizeof(sign));
  1463. InRpcSecureSign(&sign, p);
  1464. #ifdef OS_WIN32
  1465. // Win32: Show dialog
  1466. ret = Win32CiSecureSign(&sign);
  1467. #else // OS_WIN32
  1468. // UNIX: not implemented
  1469. ret = false;
  1470. #endif // OS_WIN32
  1471. p = NewPack();
  1472. OutRpcSecureSign(p, &sign);
  1473. FreeRpcSecureSign(&sign);
  1474. PackAddBool(p, "ret", ret);
  1475. SendPack(s, p);
  1476. FreePack(p);
  1477. }
  1478. // Stop the listener
  1479. void CnReleaseSocket(SOCK *s, PACK *p)
  1480. {
  1481. UINT pid = 0;
  1482. UINT current_pid = 0;
  1483. // Validate arguments
  1484. if (s == NULL || p == NULL)
  1485. {
  1486. return;
  1487. }
  1488. pid = PackGetInt(p, "pid");
  1489. #ifdef OS_WIN32
  1490. current_pid = MsGetProcessId();
  1491. #endif // OS_WIN32
  1492. if (current_pid == pid)
  1493. {
  1494. return;
  1495. }
  1496. Lock(cn_listener_lock);
  1497. {
  1498. if (cn_listener != NULL)
  1499. {
  1500. if (cn_listener->Halt == false)
  1501. {
  1502. StopListener(cn_listener);
  1503. cn_next_allow = Tick64() + (6 * 1000);
  1504. }
  1505. }
  1506. }
  1507. Unlock(cn_listener_lock);
  1508. }
  1509. // Start the client notification service
  1510. void CnStart()
  1511. {
  1512. CEDAR *cedar;
  1513. LISTENER *o;
  1514. UINT last_cursor_hash = 0;
  1515. bool last_session_active = false;
  1516. cn_next_allow = 0;
  1517. cn_listener_lock = NewLock();
  1518. #ifdef OS_WIN32
  1519. MsSetShutdownParameters(0xff, 0x00000001);
  1520. InitWinUi(_UU("CN_TITLE"), _SS("DEFAULT_FONT"), _II("DEFAULT_FONT_SIZE"));
  1521. #endif // OS_WIN32
  1522. cedar = NewCedar(NULL, NULL);
  1523. if (CnCheckAlreadyExists(true))
  1524. {
  1525. // Already started
  1526. ReleaseCedar(cedar);
  1527. #ifdef OS_WIN32
  1528. FreeWinUi();
  1529. #endif // OS_WIN32
  1530. return;
  1531. }
  1532. #ifdef OS_WIN32
  1533. MsRegWriteInt(REG_CURRENT_USER, CM_REG_KEY,
  1534. "NotifyServerProcessId", MsGetProcessId());
  1535. #endif // OS_WIN32
  1536. DisableDosProtect();
  1537. BEGIN_LISTENER:
  1538. Lock(cn_listener_lock);
  1539. cn_listener = o = NewListenerEx2(cedar, LISTENER_TCP, CLIENT_NOTIFY_PORT, CnListenerProc, NULL, true);
  1540. Unlock(cn_listener_lock);
  1541. while (true)
  1542. {
  1543. UINT current_cursor_hash = 0;
  1544. bool cursor_changed = false;
  1545. #ifdef OS_WIN32
  1546. // Get the current cursor position
  1547. current_cursor_hash = MsGetCursorPosHash();
  1548. #endif // OS_WIN32
  1549. if (last_cursor_hash != current_cursor_hash)
  1550. {
  1551. // Check the cursor position
  1552. cursor_changed = true;
  1553. last_cursor_hash = current_cursor_hash;
  1554. }
  1555. Lock(cn_listener_lock);
  1556. // Check the status periodically after that the listener has started
  1557. if (cn_listener->Status == LISTENER_STATUS_TRYING || cn_listener->Halt)
  1558. {
  1559. bool session_active = false;
  1560. #ifdef OS_WIN32
  1561. session_active = MsIsCurrentTerminalSessionActive();
  1562. if (cursor_changed)
  1563. {
  1564. // If the cursor position is changed but the terminal session is
  1565. // not active, the cursor position is regarded as not changed.
  1566. if (session_active == false)
  1567. {
  1568. cursor_changed = false;
  1569. }
  1570. }
  1571. if (last_session_active != session_active)
  1572. {
  1573. //If the cursor position doesn't changed but the terminal session
  1574. // became active than previous, the cursor position is regarded as changed.
  1575. last_session_active = session_active;
  1576. if (session_active)
  1577. {
  1578. cursor_changed = true;
  1579. }
  1580. }
  1581. #endif // OS_WIN32
  1582. // If the port cannot be opened
  1583. if (cn_next_allow <= Tick64())
  1584. {
  1585. if (cursor_changed || cn_listener->Halt)
  1586. {
  1587. if (cursor_changed)
  1588. {
  1589. // It can be judged to have the rights to open the port
  1590. // since the mouse cursor is moving.
  1591. // So, take over the port which is owned by other process forcibly
  1592. CncReleaseSocket();
  1593. }
  1594. if (cn_listener->Halt)
  1595. {
  1596. ReleaseListener(cn_listener);
  1597. cn_listener = NULL;
  1598. Unlock(cn_listener_lock);
  1599. goto BEGIN_LISTENER;
  1600. }
  1601. }
  1602. }
  1603. }
  1604. Unlock(cn_listener_lock);
  1605. SleepThread(1000);
  1606. }
  1607. }
  1608. // Confirm whether the account file is parsed successfully
  1609. bool CiTryToParseAccount(BUF *b)
  1610. {
  1611. RPC_CLIENT_CREATE_ACCOUNT *a;
  1612. // Validate arguments
  1613. if (b == NULL)
  1614. {
  1615. return false;
  1616. }
  1617. a = CiCfgToAccount(b);
  1618. if (a != NULL)
  1619. {
  1620. CiFreeClientCreateAccount(a);
  1621. Free(a);
  1622. return true;
  1623. }
  1624. else
  1625. {
  1626. return false;
  1627. }
  1628. }
  1629. bool CiTryToParseAccountFile(wchar_t *name)
  1630. {
  1631. bool ret;
  1632. BUF *b;
  1633. // Validate arguments
  1634. if (name == NULL)
  1635. {
  1636. return false;
  1637. }
  1638. b = ReadDumpW(name);
  1639. if (b == NULL)
  1640. {
  1641. return false;
  1642. }
  1643. ret = CiTryToParseAccount(b);
  1644. FreeBuf(b);
  1645. return ret;
  1646. }
  1647. // Confirm whether the account information includes sensitive information
  1648. bool CiHasAccountSensitiveInformation(BUF *b)
  1649. {
  1650. RPC_CLIENT_CREATE_ACCOUNT *a;
  1651. bool ret = false;
  1652. // Validate arguments
  1653. if (b == NULL)
  1654. {
  1655. return false;
  1656. }
  1657. a = CiCfgToAccount(b);
  1658. if (a == NULL)
  1659. {
  1660. return false;
  1661. }
  1662. if (a->ClientAuth->AuthType == CLIENT_AUTHTYPE_PASSWORD)
  1663. {
  1664. ret = true;
  1665. }
  1666. else if (a->ClientAuth->AuthType == CLIENT_AUTHTYPE_PLAIN_PASSWORD)
  1667. {
  1668. ret = true;
  1669. }
  1670. CiFreeClientCreateAccount(a);
  1671. Free(a);
  1672. return ret;
  1673. }
  1674. bool CiHasAccountSensitiveInformationFile(wchar_t *name)
  1675. {
  1676. bool ret = false;
  1677. BUF *b;
  1678. // Validate arguments
  1679. if (name == NULL)
  1680. {
  1681. return false;
  1682. }
  1683. b = ReadDumpW(name);
  1684. if (b == NULL)
  1685. {
  1686. return false;
  1687. }
  1688. ret = CiHasAccountSensitiveInformation(b);
  1689. FreeBuf(b);
  1690. return ret;
  1691. }
  1692. // Delete the sensitive information in the account information
  1693. bool CiEraseSensitiveInAccount(BUF *b)
  1694. {
  1695. RPC_CLIENT_CREATE_ACCOUNT *a;
  1696. BUF *b2;
  1697. bool ret = false;
  1698. // Validate arguments
  1699. if (b == NULL)
  1700. {
  1701. return false;
  1702. }
  1703. a = CiCfgToAccount(b);
  1704. if (a == NULL)
  1705. {
  1706. return false;
  1707. }
  1708. if (a->ClientAuth->AuthType == CLIENT_AUTHTYPE_PASSWORD)
  1709. {
  1710. Zero(a->ClientAuth->HashedPassword, sizeof(a->ClientAuth->HashedPassword));
  1711. ClearStr(a->ClientAuth->Username, sizeof(a->ClientAuth->Username));
  1712. }
  1713. else if (a->ClientAuth->AuthType == CLIENT_AUTHTYPE_PLAIN_PASSWORD)
  1714. {
  1715. ClearStr(a->ClientAuth->PlainPassword, sizeof(a->ClientAuth->PlainPassword));
  1716. ClearStr(a->ClientAuth->Username, sizeof(a->ClientAuth->Username));
  1717. }
  1718. b2 = CiAccountToCfg(a);
  1719. if (b2 != NULL)
  1720. {
  1721. ret = true;
  1722. ClearBuf(b);
  1723. WriteBuf(b, b2->Buf, b2->Size);
  1724. SeekBuf(b, 0, 0);
  1725. FreeBuf(b2);
  1726. }
  1727. CiFreeClientCreateAccount(a);
  1728. Free(a);
  1729. return ret;
  1730. }
  1731. // Read the account information from the buffer
  1732. RPC_CLIENT_CREATE_ACCOUNT *CiCfgToAccount(BUF *b)
  1733. {
  1734. RPC_CLIENT_CREATE_ACCOUNT *t;
  1735. FOLDER *f;
  1736. ACCOUNT *a;
  1737. // Validate arguments
  1738. if (b == NULL)
  1739. {
  1740. return NULL;
  1741. }
  1742. f = CfgBufTextToFolder(b);
  1743. if (f == NULL)
  1744. {
  1745. return NULL;
  1746. }
  1747. a = CiLoadClientAccount(f);
  1748. CfgDeleteFolder(f);
  1749. if (a == NULL)
  1750. {
  1751. return NULL;
  1752. }
  1753. DeleteLock(a->lock);
  1754. t = ZeroMalloc(sizeof(RPC_CLIENT_CREATE_ACCOUNT));
  1755. t->ClientOption = a->ClientOption;
  1756. t->ClientAuth = a->ClientAuth;
  1757. t->StartupAccount = a->StartupAccount;
  1758. t->CheckServerCert = a->CheckServerCert;
  1759. t->ServerCert = a->ServerCert;
  1760. Free(a);
  1761. return t;
  1762. }
  1763. // Write the account information to a buffer
  1764. BUF *CiAccountToCfg(RPC_CLIENT_CREATE_ACCOUNT *t)
  1765. {
  1766. BUF *b;
  1767. FOLDER *root;
  1768. ACCOUNT a;
  1769. // Validate arguments
  1770. if (t == NULL)
  1771. {
  1772. return NULL;
  1773. }
  1774. root = CfgCreateFolder(NULL, TAG_ROOT);
  1775. Zero(&a, sizeof(a));
  1776. a.ClientOption = t->ClientOption;
  1777. a.ClientAuth = t->ClientAuth;
  1778. a.CheckServerCert = t->CheckServerCert;
  1779. a.ServerCert = t->ServerCert;
  1780. a.StartupAccount = t->StartupAccount;
  1781. CiWriteAccountData(root, &a);
  1782. b = CfgFolderToBufEx(root, true, true);
  1783. CfgDeleteFolder(root);
  1784. return b;
  1785. }
  1786. // RPC dispatch routine
  1787. PACK *CiRpcDispatch(RPC *rpc, char *name, PACK *p)
  1788. {
  1789. CLIENT *c = rpc->Param;
  1790. PACK *ret;
  1791. // Validate arguments
  1792. if (rpc == NULL || name == NULL || p == NULL)
  1793. {
  1794. return NULL;
  1795. }
  1796. ret = NewPack();
  1797. if (StrCmpi(name, "GetClientVersion") == 0)
  1798. {
  1799. RPC_CLIENT_VERSION a;
  1800. if (CtGetClientVersion(c, &a) == false)
  1801. {
  1802. RpcError(ret, c->Err);
  1803. }
  1804. else
  1805. {
  1806. OutRpcClientVersion(ret, &a);
  1807. }
  1808. }
  1809. else if (StrCmpi(name, "GetCmSetting") == 0)
  1810. {
  1811. CM_SETTING a;
  1812. if (CtGetCmSetting(c, &a) == false)
  1813. {
  1814. RpcError(ret, c->Err);
  1815. }
  1816. else
  1817. {
  1818. OutRpcCmSetting(ret, &a);
  1819. }
  1820. }
  1821. else if (StrCmpi(name, "SetCmSetting") == 0)
  1822. {
  1823. CM_SETTING a;
  1824. Zero(&a, sizeof(a));
  1825. InRpcCmSetting(&a, p);
  1826. if (CtSetCmSetting(c, &a) == false)
  1827. {
  1828. RpcError(ret, c->Err);
  1829. }
  1830. }
  1831. else if (StrCmpi(name, "SetPassword") == 0)
  1832. {
  1833. RPC_CLIENT_PASSWORD a;
  1834. InRpcClientPassword(&a, p);
  1835. if (CtSetPassword(c, &a) == false)
  1836. {
  1837. RpcError(ret, c->Err);
  1838. }
  1839. }
  1840. else if (StrCmpi(name, "GetPasswordSetting") == 0)
  1841. {
  1842. RPC_CLIENT_PASSWORD_SETTING a;
  1843. if (CtGetPasswordSetting(c, &a) == false)
  1844. {
  1845. RpcError(ret, c->Err);
  1846. }
  1847. else
  1848. {
  1849. OutRpcClientPasswordSetting(ret, &a);
  1850. }
  1851. }
  1852. else if (StrCmpi(name, "EnumCa") == 0)
  1853. {
  1854. RPC_CLIENT_ENUM_CA a;
  1855. if (CtEnumCa(c, &a) == false)
  1856. {
  1857. RpcError(ret, c->Err);
  1858. }
  1859. else
  1860. {
  1861. OutRpcClientEnumCa(ret, &a);
  1862. CiFreeClientEnumCa(&a);
  1863. }
  1864. }
  1865. else if (StrCmpi(name, "AddCa") == 0)
  1866. {
  1867. RPC_CERT a;
  1868. InRpcCert(&a, p);
  1869. if (CtAddCa(c, &a) == false)
  1870. {
  1871. RpcError(ret, c->Err);
  1872. }
  1873. FreeX(a.x);
  1874. }
  1875. else if (StrCmpi(name, "DeleteCa") == 0)
  1876. {
  1877. RPC_CLIENT_DELETE_CA a;
  1878. InRpcClientDeleteCa(&a, p);
  1879. if (CtDeleteCa(c, &a) == false)
  1880. {
  1881. RpcError(ret, c->Err);
  1882. }
  1883. }
  1884. else if (StrCmpi(name, "GetCa") == 0)
  1885. {
  1886. RPC_GET_CA a;
  1887. InRpcGetCa(&a, p);
  1888. if (CtGetCa(c, &a) == false)
  1889. {
  1890. RpcError(ret, c->Err);
  1891. }
  1892. else
  1893. {
  1894. OutRpcGetCa(ret, &a);
  1895. }
  1896. CiFreeGetCa(&a);
  1897. }
  1898. else if (StrCmpi(name, "EnumSecure") == 0)
  1899. {
  1900. RPC_CLIENT_ENUM_SECURE a;
  1901. if (CtEnumSecure(c, &a) == false)
  1902. {
  1903. RpcError(ret, c->Err);
  1904. }
  1905. else
  1906. {
  1907. OutRpcClientEnumSecure(ret, &a);
  1908. CiFreeClientEnumSecure(&a);
  1909. }
  1910. }
  1911. else if (StrCmpi(name, "UseSecure") == 0)
  1912. {
  1913. RPC_USE_SECURE a;
  1914. InRpcUseSecure(&a, p);
  1915. if (CtUseSecure(c, &a) == false)
  1916. {
  1917. RpcError(ret, c->Err);
  1918. }
  1919. }
  1920. else if (StrCmpi(name, "GetUseSecure") == 0)
  1921. {
  1922. RPC_USE_SECURE a;
  1923. Zero(&a, sizeof(a));
  1924. if (CtGetUseSecure(c, &a) == false)
  1925. {
  1926. RpcError(ret, c->Err);
  1927. }
  1928. else
  1929. {
  1930. OutRpcUseSecure(ret, &a);
  1931. }
  1932. }
  1933. else if (StrCmpi(name, "EnumObjectInSecure") == 0)
  1934. {
  1935. RPC_ENUM_OBJECT_IN_SECURE a;
  1936. if (CtEnumObjectInSecure(c, &a) == false)
  1937. {
  1938. RpcError(ret, c->Err);
  1939. }
  1940. else
  1941. {
  1942. OutRpcEnumObjectInSecure(ret, &a);
  1943. CiFreeEnumObjectInSecure(&a);
  1944. }
  1945. }
  1946. else if (StrCmpi(name, "CreateVLan") == 0)
  1947. {
  1948. RPC_CLIENT_CREATE_VLAN a;
  1949. InRpcCreateVLan(&a, p);
  1950. if (CtCreateVLan(c, &a) == false)
  1951. {
  1952. RpcError(ret, c->Err);
  1953. }
  1954. }
  1955. else if (StrCmpi(name, "UpgradeVLan") == 0)
  1956. {
  1957. RPC_CLIENT_CREATE_VLAN a;
  1958. InRpcCreateVLan(&a, p);
  1959. if (CtUpgradeVLan(c, &a) == false)
  1960. {
  1961. RpcError(ret, c->Err);
  1962. }
  1963. }
  1964. else if (StrCmpi(name, "GetVLan") == 0)
  1965. {
  1966. RPC_CLIENT_GET_VLAN a;
  1967. InRpcClientGetVLan(&a, p);
  1968. if (CtGetVLan(c, &a) == false)
  1969. {
  1970. RpcError(ret, c->Err);
  1971. }
  1972. else
  1973. {
  1974. OutRpcClientGetVLan(ret, &a);
  1975. }
  1976. }
  1977. else if (StrCmpi(name, "SetVLan") == 0)
  1978. {
  1979. RPC_CLIENT_SET_VLAN a;
  1980. InRpcClientSetVLan(&a, p);
  1981. if (CtSetVLan(c, &a) == false)
  1982. {
  1983. RpcError(ret, c->Err);
  1984. }
  1985. }
  1986. else if (StrCmpi(name, "EnumVLan") == 0)
  1987. {
  1988. RPC_CLIENT_ENUM_VLAN a;
  1989. if (CtEnumVLan(c, &a) == false)
  1990. {
  1991. RpcError(ret, c->Err);
  1992. }
  1993. else
  1994. {
  1995. OutRpcClientEnumVLan(ret, &a);
  1996. CiFreeClientEnumVLan(&a);
  1997. }
  1998. }
  1999. else if (StrCmpi(name, "DeleteVLan") == 0)
  2000. {
  2001. RPC_CLIENT_CREATE_VLAN a;
  2002. InRpcCreateVLan(&a, p);
  2003. if (CtDeleteVLan(c, &a) == false)
  2004. {
  2005. RpcError(ret, c->Err);
  2006. }
  2007. }
  2008. else if (StrCmpi(name, "EnableVLan") == 0)
  2009. {
  2010. RPC_CLIENT_CREATE_VLAN a;
  2011. InRpcCreateVLan(&a, p);
  2012. if (CtEnableVLan(c, &a) == false)
  2013. {
  2014. RpcError(ret, c->Err);
  2015. }
  2016. }
  2017. else if (StrCmpi(name, "DisableVLan") == 0)
  2018. {
  2019. RPC_CLIENT_CREATE_VLAN a;
  2020. InRpcCreateVLan(&a, p);
  2021. if (CtDisableVLan(c, &a) == false)
  2022. {
  2023. RpcError(ret, c->Err);
  2024. }
  2025. }
  2026. else if (StrCmpi(name, "CreateAccount") == 0)
  2027. {
  2028. RPC_CLIENT_CREATE_ACCOUNT a;
  2029. InRpcClientCreateAccount(&a, p);
  2030. if (CtCreateAccount(c, &a, false) == false)
  2031. {
  2032. RpcError(ret, c->Err);
  2033. }
  2034. CiFreeClientCreateAccount(&a);
  2035. }
  2036. else if (StrCmpi(name, "EnumAccount") == 0)
  2037. {
  2038. RPC_CLIENT_ENUM_ACCOUNT a;
  2039. if (CtEnumAccount(c, &a) == false)
  2040. {
  2041. RpcError(ret, c->Err);
  2042. }
  2043. else
  2044. {
  2045. OutRpcClientEnumAccount(ret, &a);
  2046. CiFreeClientEnumAccount(&a);
  2047. }
  2048. }
  2049. else if (StrCmpi(name, "DeleteAccount") == 0)
  2050. {
  2051. RPC_CLIENT_DELETE_ACCOUNT a;
  2052. InRpcClientDeleteAccount(&a, p);
  2053. if (CtDeleteAccount(c, &a, false) == false)
  2054. {
  2055. RpcError(ret, c->Err);
  2056. }
  2057. }
  2058. else if (StrCmpi(name, "SetStartupAccount") == 0)
  2059. {
  2060. RPC_CLIENT_DELETE_ACCOUNT a;
  2061. InRpcClientDeleteAccount(&a, p);
  2062. if (CtSetStartupAccount(c, &a, false) == false)
  2063. {
  2064. RpcError(ret, c->Err);
  2065. }
  2066. }
  2067. else if (StrCmpi(name, "RemoveStartupAccount") == 0)
  2068. {
  2069. RPC_CLIENT_DELETE_ACCOUNT a;
  2070. InRpcClientDeleteAccount(&a, p);
  2071. if (CtRemoveStartupAccount(c, &a) == false)
  2072. {
  2073. RpcError(ret, c->Err);
  2074. }
  2075. }
  2076. else if (StrCmpi(name, "GetIssuer") == 0)
  2077. {
  2078. RPC_GET_ISSUER a;
  2079. InRpcGetIssuer(&a, p);
  2080. if (CtGetIssuer(c, &a))
  2081. {
  2082. OutRpcGetIssuer(ret, &a);
  2083. }
  2084. else
  2085. {
  2086. RpcError(ret, c->Err);
  2087. }
  2088. CiFreeGetIssuer(&a);
  2089. }
  2090. else if (StrCmpi(name, "GetCommonProxySetting") == 0)
  2091. {
  2092. INTERNET_SETTING t;
  2093. InRpcInternetSetting(&t, p);
  2094. if (CtGetCommonProxySetting(c, &t))
  2095. {
  2096. OutRpcInternetSetting(ret, &t);
  2097. }
  2098. else
  2099. {
  2100. RpcError(ret, c->Err);
  2101. }
  2102. }
  2103. else if (StrCmpi(name, "SetCommonProxySetting") == 0)
  2104. {
  2105. INTERNET_SETTING t;
  2106. InRpcInternetSetting(&t, p);
  2107. if (CtSetCommonProxySetting(c, &t))
  2108. {
  2109. OutRpcInternetSetting(ret, &t);
  2110. }
  2111. else
  2112. {
  2113. RpcError(ret, c->Err);
  2114. }
  2115. }
  2116. else if (StrCmpi(name, "SetAccount") == 0)
  2117. {
  2118. RPC_CLIENT_CREATE_ACCOUNT a;
  2119. InRpcClientCreateAccount(&a, p);
  2120. if (CtSetAccount(c, &a, false) == false)
  2121. {
  2122. RpcError(ret, c->Err);
  2123. }
  2124. CiFreeClientCreateAccount(&a);
  2125. }
  2126. else if (StrCmpi(name, "GetAccount") == 0)
  2127. {
  2128. RPC_CLIENT_GET_ACCOUNT a;
  2129. InRpcClientGetAccount(&a, p);
  2130. if (CtGetAccount(c, &a) == false)
  2131. {
  2132. RpcError(ret, c->Err);
  2133. }
  2134. else
  2135. {
  2136. OutRpcClientGetAccount(ret, &a);
  2137. }
  2138. CiFreeClientGetAccount(&a);
  2139. }
  2140. else if (StrCmpi(name, "RenameAccount") == 0)
  2141. {
  2142. RPC_RENAME_ACCOUNT a;
  2143. InRpcRenameAccount(&a, p);
  2144. if (CtRenameAccount(c, &a, false) == false)
  2145. {
  2146. RpcError(ret, c->Err);
  2147. }
  2148. }
  2149. else if (StrCmpi(name, "SetClientConfig") == 0)
  2150. {
  2151. CLIENT_CONFIG a;
  2152. InRpcClientConfig(&a, p);
  2153. if (CtSetClientConfig(c, &a) == false)
  2154. {
  2155. RpcError(ret, c->Err);
  2156. }
  2157. }
  2158. else if (StrCmpi(name, "GetClientConfig") == 0)
  2159. {
  2160. CLIENT_CONFIG a;
  2161. if (CtGetClientConfig(c, &a) == false)
  2162. {
  2163. RpcError(ret, c->Err);
  2164. }
  2165. else
  2166. {
  2167. OutRpcClientConfig(ret, &a);
  2168. }
  2169. }
  2170. else if (StrCmpi(name, "Connect") == 0)
  2171. {
  2172. RPC_CLIENT_CONNECT a;
  2173. InRpcClientConnect(&a, p);
  2174. if (CtConnect(c, &a) == false)
  2175. {
  2176. RpcError(ret, c->Err);
  2177. }
  2178. }
  2179. else if (StrCmpi(name, "Disconnect") == 0)
  2180. {
  2181. RPC_CLIENT_CONNECT a;
  2182. InRpcClientConnect(&a, p);
  2183. if (CtDisconnect(c, &a, false) == false)
  2184. {
  2185. RpcError(ret, c->Err);
  2186. }
  2187. }
  2188. else if (StrCmpi(name, "GetAccountStatus") == 0)
  2189. {
  2190. RPC_CLIENT_GET_CONNECTION_STATUS a;
  2191. InRpcClientGetConnectionStatus(&a, p);
  2192. if (CtGetAccountStatus(c, &a) == false)
  2193. {
  2194. RpcError(ret, c->Err);
  2195. }
  2196. else
  2197. {
  2198. OutRpcClientGetConnectionStatus(ret, &a);
  2199. }
  2200. CiFreeClientGetConnectionStatus(&a);
  2201. }
  2202. else
  2203. {
  2204. FreePack(ret);
  2205. ret = NULL;
  2206. }
  2207. return ret;
  2208. }
  2209. // Set the CM_SETTING
  2210. UINT CcSetCmSetting(REMOTE_CLIENT *r, CM_SETTING *a)
  2211. {
  2212. PACK *ret, *p;
  2213. UINT err;
  2214. // Validate arguments
  2215. if (r == NULL || a == NULL)
  2216. {
  2217. return ERR_INTERNAL_ERROR;
  2218. }
  2219. p = NewPack();
  2220. OutRpcCmSetting(p, a);
  2221. ret = RpcCall(r->Rpc, "SetCmSetting", p);
  2222. if (RpcIsOk(ret))
  2223. {
  2224. FreePack(ret);
  2225. return 0;
  2226. }
  2227. else
  2228. {
  2229. err = RpcGetError(ret);
  2230. FreePack(ret);
  2231. return err;
  2232. }
  2233. }
  2234. // Get the CM_SETTING
  2235. UINT CcGetCmSetting(REMOTE_CLIENT *r, CM_SETTING *a)
  2236. {
  2237. PACK *ret;
  2238. // Validate arguments
  2239. if (r == NULL || a == NULL)
  2240. {
  2241. return ERR_INTERNAL_ERROR;
  2242. }
  2243. ret = RpcCall(r->Rpc, "GetCmSetting", NULL);
  2244. if (RpcIsOk(ret))
  2245. {
  2246. InRpcCmSetting(a, ret);
  2247. FreePack(ret);
  2248. return 0;
  2249. }
  2250. else
  2251. {
  2252. UINT err = RpcGetError(ret);
  2253. FreePack(ret);
  2254. return err;
  2255. }
  2256. }
  2257. // Get the client version
  2258. UINT CcGetClientVersion(REMOTE_CLIENT *r, RPC_CLIENT_VERSION *a)
  2259. {
  2260. PACK *ret;
  2261. // Validate arguments
  2262. if (r == NULL || a == NULL)
  2263. {
  2264. return ERR_INTERNAL_ERROR;
  2265. }
  2266. ret = RpcCall(r->Rpc, "GetClientVersion", NULL);
  2267. if (RpcIsOk(ret))
  2268. {
  2269. InRpcClientVersion(a, ret);
  2270. FreePack(ret);
  2271. return 0;
  2272. }
  2273. else
  2274. {
  2275. UINT err = RpcGetError(ret);
  2276. FreePack(ret);
  2277. return err;
  2278. }
  2279. }
  2280. // Set the password
  2281. UINT CcSetPassword(REMOTE_CLIENT *r, RPC_CLIENT_PASSWORD *pass)
  2282. {
  2283. PACK *ret, *p;
  2284. // Validate arguments
  2285. if (r == NULL || pass == NULL)
  2286. {
  2287. return ERR_INTERNAL_ERROR;
  2288. }
  2289. p = NewPack();
  2290. OutRpcClientPassword(p, pass);
  2291. ret = RpcCall(r->Rpc, "SetPassword", p);
  2292. if (RpcIsOk(ret))
  2293. {
  2294. FreePack(ret);
  2295. return 0;
  2296. }
  2297. else
  2298. {
  2299. UINT err = RpcGetError(ret);
  2300. FreePack(ret);
  2301. return err;
  2302. }
  2303. }
  2304. // Get the password setting
  2305. UINT CcGetPasswordSetting(REMOTE_CLIENT *r, RPC_CLIENT_PASSWORD_SETTING *a)
  2306. {
  2307. PACK *ret;
  2308. UINT err = 0;
  2309. // Validate arguments
  2310. if (r == NULL || a == NULL)
  2311. {
  2312. return ERR_INTERNAL_ERROR;
  2313. }
  2314. ret = RpcCall(r->Rpc, "GetPasswordSetting", NULL);
  2315. if (RpcIsOk(ret))
  2316. {
  2317. InRpcClientPasswordSetting(a, ret);
  2318. }
  2319. else
  2320. {
  2321. err = RpcGetError(ret);
  2322. }
  2323. FreePack(ret);
  2324. return err;
  2325. }
  2326. // Enumerate the CA
  2327. UINT CcEnumCa(REMOTE_CLIENT *r, RPC_CLIENT_ENUM_CA *e)
  2328. {
  2329. PACK *ret;
  2330. UINT err = 0;
  2331. // Validate arguments
  2332. if (r == NULL || e == NULL)
  2333. {
  2334. return ERR_INTERNAL_ERROR;
  2335. }
  2336. ret = RpcCall(r->Rpc, "EnumCa", NULL);
  2337. if (RpcIsOk(ret))
  2338. {
  2339. InRpcClientEnumCa(e, ret);
  2340. }
  2341. else
  2342. {
  2343. err = RpcGetError(ret);
  2344. }
  2345. FreePack(ret);
  2346. return err;
  2347. }
  2348. // Add the CA
  2349. UINT CcAddCa(REMOTE_CLIENT *r, RPC_CERT *cert)
  2350. {
  2351. PACK *p, *ret;
  2352. UINT err = 0;
  2353. // Validate arguments
  2354. if (r == NULL || cert == NULL)
  2355. {
  2356. return ERR_INTERNAL_ERROR;
  2357. }
  2358. p = NewPack();
  2359. OutRpcCert(p, cert);
  2360. ret = RpcCall(r->Rpc, "AddCa", p);
  2361. if (RpcIsOk(ret) == false)
  2362. {
  2363. err = RpcGetError(ret);
  2364. }
  2365. FreePack(ret);
  2366. return err;
  2367. }
  2368. // Delete the CA
  2369. UINT CcDeleteCa(REMOTE_CLIENT *r, RPC_CLIENT_DELETE_CA *c)
  2370. {
  2371. PACK *p, *ret;
  2372. UINT err = 0;
  2373. // Validate arguments
  2374. if (r == NULL || c == NULL)
  2375. {
  2376. return ERR_INTERNAL_ERROR;
  2377. }
  2378. p = NewPack();
  2379. OutRpcClientDeleteCa(p, c);
  2380. ret = RpcCall(r->Rpc, "DeleteCa", p);
  2381. if (RpcIsOk(ret) == false)
  2382. {
  2383. err = RpcGetError(ret);
  2384. }
  2385. FreePack(ret);
  2386. return err;
  2387. }
  2388. // Get the proxy setting
  2389. UINT CcGetCommonProxySetting(REMOTE_CLIENT *r, INTERNET_SETTING *a)
  2390. {
  2391. PACK *p, *ret;
  2392. UINT err = 0;
  2393. // Validate arguments
  2394. if (r == NULL || a == NULL)
  2395. {
  2396. return ERR_INTERNAL_ERROR;
  2397. }
  2398. p = NewPack();
  2399. OutRpcInternetSetting(p, a);
  2400. ret = RpcCall(r->Rpc, "GetCommonProxySetting", p);
  2401. if (RpcIsOk(ret))
  2402. {
  2403. Zero(a, sizeof(INTERNET_SETTING));
  2404. InRpcInternetSetting(a, ret);
  2405. }
  2406. else
  2407. {
  2408. err = RpcGetError(ret);
  2409. }
  2410. FreePack(ret);
  2411. return err;
  2412. }
  2413. // Set the proxy setting
  2414. UINT CcSetCommonProxySetting(REMOTE_CLIENT *r, INTERNET_SETTING *a)
  2415. {
  2416. PACK *p, *ret;
  2417. UINT err = 0;
  2418. // Validate arguments
  2419. if (r == NULL || a == NULL)
  2420. {
  2421. return ERR_INTERNAL_ERROR;
  2422. }
  2423. p = NewPack();
  2424. OutRpcInternetSetting(p, a);
  2425. ret = RpcCall(r->Rpc, "SetCommonProxySetting", p);
  2426. if (RpcIsOk(ret))
  2427. {
  2428. Zero(a, sizeof(INTERNET_SETTING));
  2429. InRpcInternetSetting(a, ret);
  2430. }
  2431. else
  2432. {
  2433. err = RpcGetError(ret);
  2434. }
  2435. FreePack(ret);
  2436. return err;
  2437. }
  2438. // Get the issuer
  2439. UINT CcGetIssuer(REMOTE_CLIENT *r, RPC_GET_ISSUER *a)
  2440. {
  2441. PACK *p, *ret;
  2442. UINT err = 0;
  2443. // Validate arguments
  2444. if (r == NULL || a == NULL)
  2445. {
  2446. return ERR_INTERNAL_ERROR;
  2447. }
  2448. p = NewPack();
  2449. OutRpcGetIssuer(p, a);
  2450. ret = RpcCall(r->Rpc, "GetIssuer", p);
  2451. if (RpcIsOk(ret))
  2452. {
  2453. if (a->x != NULL)
  2454. {
  2455. FreeX(a->x);
  2456. a->x = NULL;
  2457. }
  2458. InRpcGetIssuer(a, ret);
  2459. }
  2460. else
  2461. {
  2462. err = RpcGetError(ret);
  2463. }
  2464. FreePack(ret);
  2465. return err;
  2466. }
  2467. // Get the CA
  2468. UINT CcGetCa(REMOTE_CLIENT *r, RPC_GET_CA *get)
  2469. {
  2470. PACK *p, *ret;
  2471. UINT err = 0;
  2472. // Validate arguments
  2473. if (r == NULL || get == NULL)
  2474. {
  2475. return ERR_INTERNAL_ERROR;
  2476. }
  2477. p = NewPack();
  2478. OutRpcGetCa(p, get);
  2479. ret = RpcCall(r->Rpc, "GetCa", p);
  2480. if (RpcIsOk(ret))
  2481. {
  2482. InRpcGetCa(get, ret);
  2483. }
  2484. else
  2485. {
  2486. err = RpcGetError(ret);
  2487. }
  2488. FreePack(ret);
  2489. return err;
  2490. }
  2491. // Enumeration of the secure devices
  2492. UINT CcEnumSecure(REMOTE_CLIENT *r, RPC_CLIENT_ENUM_SECURE *e)
  2493. {
  2494. PACK *ret;
  2495. UINT err = 0;
  2496. // Validate arguments
  2497. if (r == NULL || e == NULL)
  2498. {
  2499. return ERR_INTERNAL_ERROR;
  2500. }
  2501. ret = RpcCall(r->Rpc, "EnumSecure", NULL);
  2502. if (RpcIsOk(ret))
  2503. {
  2504. InRpcClientEnumSecure(e, ret);
  2505. }
  2506. else
  2507. {
  2508. err = RpcGetError(ret);
  2509. }
  2510. FreePack(ret);
  2511. return err;
  2512. }
  2513. // Get the secure device that the user is using
  2514. UINT CcGetUseSecure(REMOTE_CLIENT *r, RPC_USE_SECURE *sec)
  2515. {
  2516. PACK *p, *ret;
  2517. UINT err = 0;
  2518. // Validate arguments
  2519. if (r == NULL || sec == NULL)
  2520. {
  2521. return ERR_INTERNAL_ERROR;
  2522. }
  2523. p = NewPack();
  2524. ret = RpcCall(r->Rpc, "GetUseSecure", p);
  2525. if (RpcIsOk(ret) == false)
  2526. {
  2527. err = RpcGetError(ret);
  2528. }
  2529. else
  2530. {
  2531. InRpcUseSecure(sec, ret);
  2532. }
  2533. FreePack(ret);
  2534. return err;
  2535. }
  2536. // Use the secure device
  2537. UINT CcUseSecure(REMOTE_CLIENT *r, RPC_USE_SECURE *sec)
  2538. {
  2539. PACK *p, *ret;
  2540. UINT err = 0;
  2541. // Validate arguments
  2542. if (r == NULL || sec == NULL)
  2543. {
  2544. return ERR_INTERNAL_ERROR;
  2545. }
  2546. p = NewPack();
  2547. OutRpcUseSecure(p, sec);
  2548. ret = RpcCall(r->Rpc, "UseSecure", p);
  2549. if (RpcIsOk(ret) == false)
  2550. {
  2551. err = RpcGetError(ret);
  2552. }
  2553. FreePack(ret);
  2554. return err;
  2555. }
  2556. // Enumerate objects in the secure device
  2557. UINT CcEnumObjectInSecure(REMOTE_CLIENT *r, RPC_ENUM_OBJECT_IN_SECURE *e)
  2558. {
  2559. PACK *ret;
  2560. UINT err = 0;
  2561. // Validate arguments
  2562. if (r == NULL || e == NULL)
  2563. {
  2564. return ERR_INTERNAL_ERROR;
  2565. }
  2566. ret = RpcCall(r->Rpc, "EnumObjectInSecure", NULL);
  2567. if (RpcIsOk(ret))
  2568. {
  2569. InRpcEnumObjectInSecure(e, ret);
  2570. }
  2571. else
  2572. {
  2573. err = RpcGetError(ret);
  2574. }
  2575. FreePack(ret);
  2576. return err;
  2577. }
  2578. // Get a next recommended virtual LAN card name
  2579. bool CiGetNextRecommendedVLanName(REMOTE_CLIENT *r, char *name, UINT size)
  2580. {
  2581. RPC_CLIENT_ENUM_VLAN t;
  2582. UINT i;
  2583. bool b;
  2584. UINT j;
  2585. bool ok = false;
  2586. // Validate arguments
  2587. if (r == NULL || name == NULL)
  2588. {
  2589. return false;
  2590. }
  2591. Zero(&t, sizeof(t));
  2592. if (CcEnumVLan(r, &t) != ERR_NO_ERROR)
  2593. {
  2594. return false;
  2595. }
  2596. for (i = 1;i < 128;i++)
  2597. {
  2598. char tmp[MAX_SIZE];
  2599. CiGenerateVLanRegulatedName(tmp, sizeof(tmp), i);
  2600. b = false;
  2601. for (j = 0;j < t.NumItem;j++)
  2602. {
  2603. if (StrCmpi(t.Items[j]->DeviceName, tmp) == 0)
  2604. {
  2605. b = true;
  2606. break;
  2607. }
  2608. }
  2609. if (b == false)
  2610. {
  2611. ok = true;
  2612. StrCpy(name, size, tmp);
  2613. break;
  2614. }
  2615. }
  2616. if (ok)
  2617. {
  2618. CiFreeClientEnumVLan(&t);
  2619. }
  2620. return true;
  2621. }
  2622. // Generate a virtual LAN card name automatically
  2623. void CiGenerateVLanRegulatedName(char *name, UINT size, UINT i)
  2624. {
  2625. // Validate arguments
  2626. if (name == NULL)
  2627. {
  2628. return;
  2629. }
  2630. if (i == 1)
  2631. {
  2632. StrCpy(name, size, "VPN");
  2633. }
  2634. else
  2635. {
  2636. Format(name, size, "VPN%u", i);
  2637. }
  2638. }
  2639. // Examine whether the specified name is valid as a virtual LAN card name of Windows 8 and later?
  2640. bool CiIsValidVLanRegulatedName(char *name)
  2641. {
  2642. UINT i;
  2643. // Validate arguments
  2644. if (name == NULL)
  2645. {
  2646. return false;
  2647. }
  2648. for (i = 1;i < 128;i++)
  2649. {
  2650. char tmp[MAX_SIZE];
  2651. CiGenerateVLanRegulatedName(tmp, sizeof(tmp), i);
  2652. if (StrCmpi(name, tmp) == 0)
  2653. {
  2654. return true;
  2655. }
  2656. }
  2657. return false;
  2658. }
  2659. // Create a VLAN
  2660. UINT CcCreateVLan(REMOTE_CLIENT *r, RPC_CLIENT_CREATE_VLAN *create)
  2661. {
  2662. PACK *ret, *p;
  2663. UINT err = 0;
  2664. char *s = NULL;
  2665. // Validate arguments
  2666. if (r == NULL || create == NULL)
  2667. {
  2668. return ERR_INTERNAL_ERROR;
  2669. }
  2670. p = NewPack();
  2671. OutRpcCreateVLan(p, create);
  2672. #ifdef OS_WIN32
  2673. s = MsNoWarningSoundInit();
  2674. #endif // OS_WIN32
  2675. ret = RpcCall(r->Rpc, "CreateVLan", p);
  2676. #ifdef OS_WIN32
  2677. MsNoWarningSoundFree(s);
  2678. #endif // OS_WIN32
  2679. if (RpcIsOk(ret) == false)
  2680. {
  2681. err = RpcGetError(ret);
  2682. }
  2683. FreePack(ret);
  2684. return err;
  2685. }
  2686. // Upgrade the VLAN
  2687. UINT CcUpgradeVLan(REMOTE_CLIENT *r, RPC_CLIENT_CREATE_VLAN *create)
  2688. {
  2689. PACK *ret, *p;
  2690. UINT err = 0;
  2691. char *s = NULL;
  2692. // Validate arguments
  2693. if (r == NULL || create == NULL)
  2694. {
  2695. return ERR_INTERNAL_ERROR;
  2696. }
  2697. p = NewPack();
  2698. OutRpcCreateVLan(p, create);
  2699. #ifdef OS_WIN32
  2700. s = MsNoWarningSoundInit();
  2701. #endif // OS_WIN32
  2702. ret = RpcCall(r->Rpc, "UpgradeVLan", p);
  2703. #ifdef OS_WIN32
  2704. MsNoWarningSoundFree(s);
  2705. #endif // OS_WIN32
  2706. if (RpcIsOk(ret) == false)
  2707. {
  2708. err = RpcGetError(ret);
  2709. }
  2710. FreePack(ret);
  2711. return err;
  2712. }
  2713. // Get the VLAN
  2714. UINT CcGetVLan(REMOTE_CLIENT *r, RPC_CLIENT_GET_VLAN *get)
  2715. {
  2716. PACK *ret, *p;
  2717. UINT err = 0;
  2718. // Validate arguments
  2719. if (r == NULL || get == NULL)
  2720. {
  2721. return ERR_INTERNAL_ERROR;
  2722. }
  2723. p = NewPack();
  2724. OutRpcClientGetVLan(p, get);
  2725. ret = RpcCall(r->Rpc, "GetVLan", p);
  2726. if (RpcIsOk(ret))
  2727. {
  2728. InRpcClientGetVLan(get, ret);
  2729. }
  2730. else
  2731. {
  2732. err = RpcGetError(ret);
  2733. }
  2734. FreePack(ret);
  2735. return err;
  2736. }
  2737. // VLAN configuration
  2738. UINT CcSetVLan(REMOTE_CLIENT *r, RPC_CLIENT_SET_VLAN *set)
  2739. {
  2740. PACK *ret, *p;
  2741. UINT err = 0;
  2742. // Validate arguments
  2743. if (r == NULL || set == NULL)
  2744. {
  2745. return ERR_INTERNAL_ERROR;
  2746. }
  2747. p = NewPack();
  2748. OutRpcClientSetVLan(p, set);
  2749. ret = RpcCall(r->Rpc, "SetVLan", p);
  2750. if (RpcIsOk(ret) == false)
  2751. {
  2752. err = RpcGetError(ret);
  2753. }
  2754. FreePack(ret);
  2755. return err;
  2756. }
  2757. // Enumeration of VLAN
  2758. UINT CcEnumVLan(REMOTE_CLIENT *r, RPC_CLIENT_ENUM_VLAN *e)
  2759. {
  2760. PACK *ret;
  2761. UINT err = 0;
  2762. // Validate arguments
  2763. if (r == NULL || e == NULL)
  2764. {
  2765. return ERR_INTERNAL_ERROR;
  2766. }
  2767. ret = RpcCall(r->Rpc, "EnumVLan", NULL);
  2768. if (RpcIsOk(ret))
  2769. {
  2770. InRpcClientEnumVLan(e, ret);
  2771. }
  2772. else
  2773. {
  2774. err = RpcGetError(ret);
  2775. }
  2776. FreePack(ret);
  2777. return err;
  2778. }
  2779. // Delete the VLAN
  2780. UINT CcDeleteVLan(REMOTE_CLIENT *r, RPC_CLIENT_CREATE_VLAN *d)
  2781. {
  2782. PACK *ret, *p;
  2783. UINT err = 0;
  2784. // Validate arguments
  2785. if (r == NULL || d == NULL)
  2786. {
  2787. return ERR_INTERNAL_ERROR;
  2788. }
  2789. p = NewPack();
  2790. OutRpcCreateVLan(p, d);
  2791. ret = RpcCall(r->Rpc, "DeleteVLan", p);
  2792. if (RpcIsOk(ret) == false)
  2793. {
  2794. err = RpcGetError(ret);
  2795. }
  2796. FreePack(ret);
  2797. return err;
  2798. }
  2799. // Enable the VLAN
  2800. UINT CcEnableVLan(REMOTE_CLIENT *r, RPC_CLIENT_CREATE_VLAN *vlan)
  2801. {
  2802. PACK *ret, *p;
  2803. UINT err = 0;
  2804. // Validate arguments
  2805. if (r == NULL || vlan == NULL)
  2806. {
  2807. return ERR_INTERNAL_ERROR;
  2808. }
  2809. p = NewPack();
  2810. OutRpcCreateVLan(p, vlan);
  2811. ret = RpcCall(r->Rpc, "EnableVLan", p);
  2812. if (RpcIsOk(ret) == false)
  2813. {
  2814. err = RpcGetError(ret);
  2815. }
  2816. FreePack(ret);
  2817. return err;
  2818. }
  2819. // Disable the VLAN
  2820. UINT CcDisableVLan(REMOTE_CLIENT *r, RPC_CLIENT_CREATE_VLAN *vlan)
  2821. {
  2822. PACK *ret, *p;
  2823. UINT err = 0;
  2824. // Validate arguments
  2825. if (r == NULL || vlan == NULL)
  2826. {
  2827. return ERR_INTERNAL_ERROR;
  2828. }
  2829. p = NewPack();
  2830. OutRpcCreateVLan(p, vlan);
  2831. ret = RpcCall(r->Rpc, "DisableVLan", p);
  2832. if (RpcIsOk(ret) == false)
  2833. {
  2834. err = RpcGetError(ret);
  2835. }
  2836. FreePack(ret);
  2837. return err;
  2838. }
  2839. // Create an Account
  2840. UINT CcCreateAccount(REMOTE_CLIENT *r, RPC_CLIENT_CREATE_ACCOUNT *a)
  2841. {
  2842. PACK *ret, *p;
  2843. UINT err = 0;
  2844. // Validate arguments
  2845. if (r == NULL || a == NULL)
  2846. {
  2847. return ERR_INTERNAL_ERROR;
  2848. }
  2849. p = NewPack();
  2850. OutRpcClientCreateAccount(p, a);
  2851. ret = RpcCall(r->Rpc, "CreateAccount", p);
  2852. if (RpcIsOk(ret) == false)
  2853. {
  2854. err = RpcGetError(ret);
  2855. }
  2856. FreePack(ret);
  2857. return err;
  2858. }
  2859. // Enumeration of accounts
  2860. UINT CcEnumAccount(REMOTE_CLIENT *r, RPC_CLIENT_ENUM_ACCOUNT *e)
  2861. {
  2862. PACK *ret;
  2863. UINT err = 0;
  2864. // Validate arguments
  2865. if (r == NULL || e == NULL)
  2866. {
  2867. return ERR_INTERNAL_ERROR;
  2868. }
  2869. ret = RpcCall(r->Rpc, "EnumAccount", NULL);
  2870. if (RpcIsOk(ret))
  2871. {
  2872. UINT i;
  2873. InRpcClientEnumAccount(e, ret);
  2874. for (i = 0;i < e->NumItem;i++)
  2875. {
  2876. RPC_CLIENT_ENUM_ACCOUNT_ITEM *t = e->Items[i];
  2877. if (IsEmptyStr(t->HubName) && t->Port == 0)
  2878. {
  2879. UINT err2;
  2880. RPC_CLIENT_GET_ACCOUNT a;
  2881. // Because the Client Manager can not get the port number and HUB name
  2882. // when enumerating in the VPN Client of the old version, get these separately.
  2883. Zero(&a, sizeof(a));
  2884. UniStrCpy(a.AccountName, sizeof(a.AccountName), t->AccountName);
  2885. err2 = CcGetAccount(r, &a);
  2886. if (err2 == ERR_NO_ERROR)
  2887. {
  2888. StrCpy(t->HubName, sizeof(t->HubName), a.ClientOption->HubName);
  2889. t->Port = a.ClientOption->Port;
  2890. CiFreeClientGetAccount(&a);
  2891. }
  2892. }
  2893. }
  2894. }
  2895. else
  2896. {
  2897. err = RpcGetError(ret);
  2898. }
  2899. FreePack(ret);
  2900. return err;
  2901. }
  2902. // Unset the startup flag of the accout
  2903. UINT CcRemoveStartupAccount(REMOTE_CLIENT *r, RPC_CLIENT_DELETE_ACCOUNT *a)
  2904. {
  2905. PACK *ret, *p;
  2906. UINT err = 0;
  2907. // Validate arguments
  2908. if (r == NULL || a == NULL)
  2909. {
  2910. return ERR_INTERNAL_ERROR;
  2911. }
  2912. p = NewPack();
  2913. OutRpcClientDeleteAccount(p, a);
  2914. ret = RpcCall(r->Rpc, "RemoveStartupAccount", p);
  2915. if (RpcIsOk(ret) == false)
  2916. {
  2917. err = RpcGetError(ret);
  2918. }
  2919. FreePack(ret);
  2920. return err;
  2921. }
  2922. // Set to start-up flag of the account
  2923. UINT CcSetStartupAccount(REMOTE_CLIENT *r, RPC_CLIENT_DELETE_ACCOUNT *a)
  2924. {
  2925. PACK *ret, *p;
  2926. UINT err = 0;
  2927. // Validate arguments
  2928. if (r == NULL || a == NULL)
  2929. {
  2930. return ERR_INTERNAL_ERROR;
  2931. }
  2932. p = NewPack();
  2933. OutRpcClientDeleteAccount(p, a);
  2934. ret = RpcCall(r->Rpc, "SetStartupAccount", p);
  2935. if (RpcIsOk(ret) == false)
  2936. {
  2937. err = RpcGetError(ret);
  2938. }
  2939. FreePack(ret);
  2940. return err;
  2941. }
  2942. // Delete the account
  2943. UINT CcDeleteAccount(REMOTE_CLIENT *r, RPC_CLIENT_DELETE_ACCOUNT *a)
  2944. {
  2945. PACK *ret, *p;
  2946. UINT err = 0;
  2947. // Validate arguments
  2948. if (r == NULL || a == NULL)
  2949. {
  2950. return ERR_INTERNAL_ERROR;
  2951. }
  2952. p = NewPack();
  2953. OutRpcClientDeleteAccount(p, a);
  2954. ret = RpcCall(r->Rpc, "DeleteAccount", p);
  2955. if (RpcIsOk(ret) == false)
  2956. {
  2957. err = RpcGetError(ret);
  2958. }
  2959. FreePack(ret);
  2960. return err;
  2961. }
  2962. // Account setting
  2963. UINT CcSetAccount(REMOTE_CLIENT *r, RPC_CLIENT_CREATE_ACCOUNT *a)
  2964. {
  2965. PACK *ret, *p;
  2966. UINT err = 0;
  2967. // Validate arguments
  2968. if (r == NULL || a == NULL)
  2969. {
  2970. return ERR_INTERNAL_ERROR;
  2971. }
  2972. p = NewPack();
  2973. OutRpcClientCreateAccount(p, a);
  2974. ret = RpcCall(r->Rpc, "SetAccount", p);
  2975. if (RpcIsOk(ret) == false)
  2976. {
  2977. err = RpcGetError(ret);
  2978. }
  2979. FreePack(ret);
  2980. return err;
  2981. }
  2982. // Get the account
  2983. UINT CcGetAccount(REMOTE_CLIENT *r, RPC_CLIENT_GET_ACCOUNT *a)
  2984. {
  2985. PACK *ret, *p;
  2986. UINT err = 0;
  2987. // Validate arguments
  2988. if (r == NULL || a == NULL)
  2989. {
  2990. return ERR_INTERNAL_ERROR;
  2991. }
  2992. p = NewPack();
  2993. OutRpcClientGetAccount(p, a);
  2994. ret = RpcCall(r->Rpc, "GetAccount", p);
  2995. if (RpcIsOk(ret))
  2996. {
  2997. InRpcClientGetAccount(a, ret);
  2998. }
  2999. else
  3000. {
  3001. err = RpcGetError(ret);
  3002. }
  3003. FreePack(ret);
  3004. return err;
  3005. }
  3006. // Change the account name
  3007. UINT CcRenameAccount(REMOTE_CLIENT *r, RPC_RENAME_ACCOUNT *rename)
  3008. {
  3009. PACK *p, *ret;
  3010. UINT err = 0;
  3011. // Validate arguments
  3012. if (r == NULL || rename == NULL)
  3013. {
  3014. return ERR_INTERNAL_ERROR;
  3015. }
  3016. p = NewPack();
  3017. OutRpcRenameAccount(p, rename);
  3018. ret = RpcCall(r->Rpc, "RenameAccount", p);
  3019. if (RpcIsOk(ret) == false)
  3020. {
  3021. err = RpcGetError(ret);
  3022. }
  3023. FreePack(ret);
  3024. return err;
  3025. }
  3026. // Set the Client configuration
  3027. UINT CcSetClientConfig(REMOTE_CLIENT *r, CLIENT_CONFIG *o)
  3028. {
  3029. PACK *p, *ret;
  3030. UINT err = 0;
  3031. // Validate arguments
  3032. if (r == NULL || o == NULL)
  3033. {
  3034. return ERR_INTERNAL_ERROR;
  3035. }
  3036. p = NewPack();
  3037. OutRpcClientConfig(p, o);
  3038. ret = RpcCall(r->Rpc, "SetClientConfig", p);
  3039. if (RpcIsOk(ret) == false)
  3040. {
  3041. err = RpcGetError(ret);
  3042. }
  3043. FreePack(ret);
  3044. return err;
  3045. }
  3046. // Get the client configuration
  3047. UINT CcGetClientConfig(REMOTE_CLIENT *r, CLIENT_CONFIG *o)
  3048. {
  3049. PACK *ret;
  3050. UINT err = 0;
  3051. // Validate arguments
  3052. if (r == NULL || o == NULL)
  3053. {
  3054. return ERR_INTERNAL_ERROR;
  3055. }
  3056. ret = RpcCall(r->Rpc, "GetClientConfig", NULL);
  3057. if (RpcIsOk(ret))
  3058. {
  3059. InRpcClientConfig(o, ret);
  3060. }
  3061. else
  3062. {
  3063. err = RpcGetError(ret);
  3064. }
  3065. FreePack(ret);
  3066. return err;
  3067. }
  3068. // Set the service to foreground process
  3069. void CcSetServiceToForegroundProcess(REMOTE_CLIENT *r)
  3070. {
  3071. // Validate arguments
  3072. if (r == NULL)
  3073. {
  3074. return;
  3075. }
  3076. // Abolition
  3077. /*
  3078. if (r->Rpc != NULL && r->Rpc->Sock != NULL && r->Rpc->Sock->RemoteIP.addr[0] == 127)
  3079. {
  3080. if (OS_IS_WINDOWS_NT(GetOsInfo()->OsType) &&
  3081. GET_KETA(GetOsInfo()->OsType, 100) >= 2)
  3082. {
  3083. // Only on a Windows 2000 or later
  3084. RPC_CLIENT_VERSION v;
  3085. Zero(&v, sizeof(v));
  3086. if (r->ClientBuildInt == 0)
  3087. {
  3088. CcGetClientVersion(r, &v);
  3089. r->ClientBuildInt = v.ClientBuildInt;
  3090. r->ProcessId = v.ProcessId;
  3091. }
  3092. if (r->ProcessId != 0 && r->ClientBuildInt <= 5080)
  3093. {
  3094. #ifdef OS_WIN32
  3095. // Set the service process as a foreground window
  3096. AllowFGWindow(v.ProcessId);
  3097. #endif // OS_WIN32
  3098. }
  3099. }
  3100. }*/
  3101. }
  3102. // Connect
  3103. UINT CcConnect(REMOTE_CLIENT *r, RPC_CLIENT_CONNECT *connect)
  3104. {
  3105. PACK *ret, *p;
  3106. UINT err = 0;
  3107. // Validate arguments
  3108. if (r == NULL || connect == NULL)
  3109. {
  3110. return ERR_INTERNAL_ERROR;
  3111. }
  3112. CcSetServiceToForegroundProcess(r);
  3113. p = NewPack();
  3114. OutRpcClientConnect(p, connect);
  3115. ret = RpcCall(r->Rpc, "Connect", p);
  3116. if (RpcIsOk(ret) == false)
  3117. {
  3118. err = RpcGetError(ret);
  3119. }
  3120. FreePack(ret);
  3121. return err;
  3122. }
  3123. // Disconnect
  3124. UINT CcDisconnect(REMOTE_CLIENT *r, RPC_CLIENT_CONNECT *connect)
  3125. {
  3126. PACK *ret, *p;
  3127. UINT err = 0;
  3128. // Validate arguments
  3129. if (r == NULL || connect == NULL)
  3130. {
  3131. return ERR_INTERNAL_ERROR;
  3132. }
  3133. CcSetServiceToForegroundProcess(r);
  3134. p = NewPack();
  3135. OutRpcClientConnect(p, connect);
  3136. ret = RpcCall(r->Rpc, "Disconnect", p);
  3137. if (RpcIsOk(ret) == false)
  3138. {
  3139. err = RpcGetError(ret);
  3140. }
  3141. FreePack(ret);
  3142. return err;
  3143. }
  3144. // Get the account status
  3145. UINT CcGetAccountStatus(REMOTE_CLIENT *r, RPC_CLIENT_GET_CONNECTION_STATUS *st)
  3146. {
  3147. PACK *ret, *p;
  3148. UINT err = 0;
  3149. // Validate arguments
  3150. if (r == NULL || st == NULL)
  3151. {
  3152. return ERR_INTERNAL_ERROR;
  3153. }
  3154. p = NewPack();
  3155. OutRpcClientGetConnectionStatus(p, st);
  3156. ret = RpcCall(r->Rpc, "GetAccountStatus", p);
  3157. if (RpcIsOk(ret))
  3158. {
  3159. InRpcClientGetConnectionStatus(st, ret);
  3160. }
  3161. else
  3162. {
  3163. err = RpcGetError(ret);
  3164. }
  3165. FreePack(ret);
  3166. return err;
  3167. }
  3168. // Client service sends a notification to the connection manager
  3169. void CiNotify(CLIENT *c)
  3170. {
  3171. CiNotifyInternal(c);
  3172. }
  3173. void CiNotifyInternal(CLIENT *c)
  3174. {
  3175. // Validate arguments
  3176. if (c == NULL)
  3177. {
  3178. return;
  3179. }
  3180. // Set all the notification event
  3181. LockList(c->NotifyCancelList);
  3182. {
  3183. UINT i;
  3184. for (i = 0;i < LIST_NUM(c->NotifyCancelList);i++)
  3185. {
  3186. CANCEL *cancel = LIST_DATA(c->NotifyCancelList, i);
  3187. Cancel(cancel);
  3188. }
  3189. }
  3190. UnlockList(c->NotifyCancelList);
  3191. }
  3192. // Release the RPC_CLIENT_ENUM_ACCOUNT
  3193. void CiFreeClientEnumAccount(RPC_CLIENT_ENUM_ACCOUNT *a)
  3194. {
  3195. UINT i;
  3196. // Validate arguments
  3197. if (a == NULL)
  3198. {
  3199. return;
  3200. }
  3201. for (i = 0;i < a->NumItem;i++)
  3202. {
  3203. RPC_CLIENT_ENUM_ACCOUNT_ITEM *e = a->Items[i];
  3204. Free(e);
  3205. }
  3206. Free(a->Items);
  3207. }
  3208. // Thread to save the configuration file periodically
  3209. void CiSaverThread(THREAD *t, void *param)
  3210. {
  3211. CLIENT *c = (CLIENT *)param;
  3212. // Validate arguments
  3213. if (t == NULL || param == NULL)
  3214. {
  3215. return;
  3216. }
  3217. NoticeThreadInit(t);
  3218. // Wait for a certain period of time
  3219. while (c->Halt == false)
  3220. {
  3221. Wait(c->SaverHalter, CLIENT_SAVER_INTERVAL);
  3222. // Save
  3223. CiSaveConfigurationFile(c);
  3224. }
  3225. }
  3226. // Initialize the Saver
  3227. void CiInitSaver(CLIENT *c)
  3228. {
  3229. // Validate arguments
  3230. if (c == NULL)
  3231. {
  3232. return;
  3233. }
  3234. c->SaverHalter = NewEvent();
  3235. c->SaverThread = NewThread(CiSaverThread, c);
  3236. WaitThreadInit(c->SaverThread);
  3237. }
  3238. // Release the Saver
  3239. void CiFreeSaver(CLIENT *c)
  3240. {
  3241. // Validate arguments
  3242. if (c == NULL)
  3243. {
  3244. return;
  3245. }
  3246. c->Halt = true;
  3247. Set(c->SaverHalter);
  3248. WaitThread(c->SaverThread, INFINITE);
  3249. ReleaseThread(c->SaverThread);
  3250. ReleaseEvent(c->SaverHalter);
  3251. }
  3252. // CM_SETTING
  3253. void InRpcCmSetting(CM_SETTING *c, PACK *p)
  3254. {
  3255. // Validate arguments
  3256. if (c == NULL || p == NULL)
  3257. {
  3258. return;
  3259. }
  3260. Zero(c, sizeof(CM_SETTING));
  3261. c->EasyMode = PackGetBool(p, "EasyMode");
  3262. c->LockMode = PackGetBool(p, "LockMode");
  3263. PackGetData2(p, "HashedPassword", c->HashedPassword, sizeof(c->HashedPassword));
  3264. }
  3265. void OutRpcCmSetting(PACK *p, CM_SETTING *c)
  3266. {
  3267. // Validate arguments
  3268. if (c == NULL || p == NULL)
  3269. {
  3270. return;
  3271. }
  3272. PackAddBool(p, "EasyMode", c->EasyMode);
  3273. PackAddBool(p, "LockMode", c->LockMode);
  3274. PackAddData(p, "HashedPassword", c->HashedPassword, sizeof(c->HashedPassword));
  3275. }
  3276. // CLIENT_CONFIG
  3277. void InRpcClientConfig(CLIENT_CONFIG *c, PACK *p)
  3278. {
  3279. // Validate arguments
  3280. if (c == NULL || p == NULL)
  3281. {
  3282. return;
  3283. }
  3284. Zero(c, sizeof(CLIENT_CONFIG));
  3285. c->UseKeepConnect = PackGetInt(p, "UseKeepConnect") == 0 ? false : true;
  3286. c->KeepConnectPort = PackGetInt(p, "KeepConnectPort");
  3287. c->KeepConnectProtocol = PackGetInt(p, "KeepConnectProtocol");
  3288. c->KeepConnectInterval = PackGetInt(p, "KeepConnectInterval");
  3289. c->AllowRemoteConfig = PackGetInt(p, "AllowRemoteConfig") == 0 ? false : true;
  3290. PackGetStr(p, "KeepConnectHost", c->KeepConnectHost, sizeof(c->KeepConnectHost));
  3291. }
  3292. void OutRpcClientConfig(PACK *p, CLIENT_CONFIG *c)
  3293. {
  3294. // Validate arguments
  3295. if (c == NULL || p == NULL)
  3296. {
  3297. return;
  3298. }
  3299. PackAddInt(p, "UseKeepConnect", c->UseKeepConnect);
  3300. PackAddInt(p, "KeepConnectPort", c->KeepConnectPort);
  3301. PackAddInt(p, "KeepConnectProtocol", c->KeepConnectProtocol);
  3302. PackAddInt(p, "KeepConnectInterval", c->KeepConnectInterval);
  3303. PackAddInt(p, "AllowRemoteConfig", c->AllowRemoteConfig);
  3304. PackAddStr(p, "KeepConnectHost", c->KeepConnectHost);
  3305. }
  3306. // RPC_CLIENT_VERSION
  3307. void InRpcClientVersion(RPC_CLIENT_VERSION *ver, PACK *p)
  3308. {
  3309. // Validate arguments
  3310. if (ver == NULL || p == NULL)
  3311. {
  3312. return;
  3313. }
  3314. Zero(ver, sizeof(RPC_CLIENT_VERSION));
  3315. PackGetStr(p, "ClientProductName", ver->ClientProductName, sizeof(ver->ClientProductName));
  3316. PackGetStr(p, "ClientVersionString", ver->ClientVersionString, sizeof(ver->ClientVersionString));
  3317. PackGetStr(p, "ClientBuildInfoString", ver->ClientBuildInfoString, sizeof(ver->ClientBuildInfoString));
  3318. ver->ClientVerInt = PackGetInt(p, "ClientVerInt");
  3319. ver->ClientBuildInt = PackGetInt(p, "ClientBuildInt");
  3320. ver->ProcessId = PackGetInt(p, "ProcessId");
  3321. ver->OsType = PackGetInt(p, "OsType");
  3322. ver->IsVLanNameRegulated = PackGetBool(p, "IsVLanNameRegulated");
  3323. ver->IsVgcSupported = PackGetBool(p, "IsVgcSupported");
  3324. ver->ShowVgcLink = PackGetBool(p, "ShowVgcLink");
  3325. PackGetStr(p, "ClientId", ver->ClientId, sizeof(ver->ClientId));
  3326. }
  3327. void OutRpcClientVersion(PACK *p, RPC_CLIENT_VERSION *ver)
  3328. {
  3329. // Validate arguments
  3330. if (ver == NULL || p == NULL)
  3331. {
  3332. return;
  3333. }
  3334. PackAddStr(p, "ClientProductName", ver->ClientProductName);
  3335. PackAddStr(p, "ClientVersionString", ver->ClientVersionString);
  3336. PackAddStr(p, "ClientBuildInfoString", ver->ClientBuildInfoString);
  3337. PackAddInt(p, "ClientVerInt", ver->ClientVerInt);
  3338. PackAddInt(p, "ClientBuildInt", ver->ClientBuildInt);
  3339. PackAddInt(p, "ProcessId", ver->ProcessId);
  3340. PackAddInt(p, "OsType", ver->OsType);
  3341. PackAddBool(p, "IsVLanNameRegulated", ver->IsVLanNameRegulated);
  3342. PackAddBool(p, "IsVgcSupported", ver->IsVgcSupported);
  3343. PackAddBool(p, "ShowVgcLink", ver->ShowVgcLink);
  3344. PackAddStr(p, "ClientId", ver->ClientId);
  3345. }
  3346. // RPC_CLIENT_PASSWORD
  3347. void InRpcClientPassword(RPC_CLIENT_PASSWORD *pw, PACK *p)
  3348. {
  3349. // Validate arguments
  3350. if (pw == NULL || p == NULL)
  3351. {
  3352. return;
  3353. }
  3354. Zero(pw, sizeof(RPC_CLIENT_PASSWORD));
  3355. PackGetStr(p, "Password", pw->Password, sizeof(pw->Password));
  3356. pw->PasswordRemoteOnly = PackGetInt(p, "PasswordRemoteOnly");
  3357. }
  3358. void OutRpcClientPassword(PACK *p, RPC_CLIENT_PASSWORD *pw)
  3359. {
  3360. // Validate arguments
  3361. if (pw == NULL || p == NULL)
  3362. {
  3363. return;
  3364. }
  3365. PackAddStr(p, "Password", pw->Password);
  3366. PackAddInt(p, "PasswordRemoteOnly", pw->PasswordRemoteOnly);
  3367. }
  3368. // RPC_CLIENT_PASSWORD_SETTING
  3369. void InRpcClientPasswordSetting(RPC_CLIENT_PASSWORD_SETTING *a, PACK *p)
  3370. {
  3371. // Validate arguments
  3372. if (a == NULL || p == NULL)
  3373. {
  3374. return;
  3375. }
  3376. Zero(a, sizeof(RPC_CLIENT_PASSWORD_SETTING));
  3377. a->IsPasswordPresented = PackGetInt(p, "IsPasswordPresented") == 0 ? false : true;
  3378. a->PasswordRemoteOnly = PackGetInt(p, "PasswordRemoteOnly") == 0 ? false : true;
  3379. }
  3380. void OutRpcClientPasswordSetting(PACK *p, RPC_CLIENT_PASSWORD_SETTING *a)
  3381. {
  3382. // Validate arguments
  3383. if (a == NULL || p == NULL)
  3384. {
  3385. return;
  3386. }
  3387. PackAddInt(p, "IsPasswordPresented", a->IsPasswordPresented);
  3388. PackAddInt(p, "PasswordRemoteOnly", a->PasswordRemoteOnly);
  3389. }
  3390. // RPC_CLIENT_ENUM_CA
  3391. void InRpcClientEnumCa(RPC_CLIENT_ENUM_CA *e, PACK *p)
  3392. {
  3393. UINT i;
  3394. // Validate arguments
  3395. if (e == NULL || p == NULL)
  3396. {
  3397. return;
  3398. }
  3399. Zero(e, sizeof(RPC_CLIENT_ENUM_CA));
  3400. e->NumItem = PackGetNum(p, "NumItem");
  3401. e->Items = ZeroMalloc(sizeof(RPC_CLIENT_ENUM_CA_ITEM *) * e->NumItem);
  3402. for (i = 0;i < e->NumItem;i++)
  3403. {
  3404. RPC_CLIENT_ENUM_CA_ITEM *item = ZeroMalloc(sizeof(RPC_CLIENT_ENUM_CA_ITEM));
  3405. e->Items[i] = item;
  3406. item->Key = PackGetIntEx(p, "Key", i);
  3407. PackGetUniStrEx(p, "SubjectName", item->SubjectName, sizeof(item->SubjectName), i);
  3408. PackGetUniStrEx(p, "IssuerName", item->IssuerName, sizeof(item->IssuerName), i);
  3409. item->Expires = PackGetInt64Ex(p, "Expires", i);
  3410. }
  3411. }
  3412. void OutRpcClientEnumCa(PACK *p, RPC_CLIENT_ENUM_CA *e)
  3413. {
  3414. UINT i;
  3415. // Validate arguments
  3416. if (e == NULL || p == NULL)
  3417. {
  3418. return;
  3419. }
  3420. PackAddNum(p, "NumItem", e->NumItem);
  3421. for (i = 0;i < e->NumItem;i++)
  3422. {
  3423. RPC_CLIENT_ENUM_CA_ITEM *item = e->Items[i];
  3424. PackAddIntEx(p, "Key", item->Key, i, e->NumItem);
  3425. PackAddUniStrEx(p, "SubjectName", item->SubjectName, i, e->NumItem);
  3426. PackAddUniStrEx(p, "IssuerName", item->IssuerName, i, e->NumItem);
  3427. PackAddInt64Ex(p, "Expires", item->Expires, i, e->NumItem);
  3428. }
  3429. }
  3430. // RPC_GET_ISSUER
  3431. void InRpcGetIssuer(RPC_GET_ISSUER *c, PACK *p)
  3432. {
  3433. BUF *b;
  3434. // Validate arguments
  3435. if (c == NULL || p == NULL)
  3436. {
  3437. return;
  3438. }
  3439. Zero(c, sizeof(RPC_GET_ISSUER));
  3440. b = PackGetBuf(p, "x");
  3441. if (b != NULL)
  3442. {
  3443. if (c->x != NULL)
  3444. {
  3445. FreeX(c->x);
  3446. }
  3447. c->x = BufToX(b, false);
  3448. FreeBuf(b);
  3449. }
  3450. b = PackGetBuf(p, "issuer_x");
  3451. if (b != NULL)
  3452. {
  3453. c->issuer_x = BufToX(b, false);
  3454. FreeBuf(b);
  3455. }
  3456. }
  3457. void OutRpcGetIssuer(PACK *p, RPC_GET_ISSUER *c)
  3458. {
  3459. BUF *b;
  3460. // Validate arguments
  3461. if (p == NULL || c == NULL)
  3462. {
  3463. return;
  3464. }
  3465. if (c->x != NULL)
  3466. {
  3467. b = XToBuf(c->x, false);
  3468. PackAddBuf(p, "x", b);
  3469. FreeBuf(b);
  3470. }
  3471. if (c->issuer_x != NULL)
  3472. {
  3473. b = XToBuf(c->issuer_x, false);
  3474. PackAddBuf(p, "issuer_x", b);
  3475. FreeBuf(b);
  3476. }
  3477. }
  3478. // TRAFFIC_EX
  3479. void InRpcTrafficEx(TRAFFIC *t, PACK *p, UINT i)
  3480. {
  3481. // Validate arguments
  3482. if (t == NULL || p == NULL)
  3483. {
  3484. return;
  3485. }
  3486. Zero(t, sizeof(TRAFFIC));
  3487. t->Recv.BroadcastBytes = PackGetInt64Ex(p, "Ex.Recv.BroadcastBytes", i);
  3488. t->Recv.BroadcastCount = PackGetInt64Ex(p, "Ex.Recv.BroadcastCount", i);
  3489. t->Recv.UnicastBytes = PackGetInt64Ex(p, "Ex.Recv.UnicastBytes", i);
  3490. t->Recv.UnicastCount = PackGetInt64Ex(p, "Ex.Recv.UnicastCount", i);
  3491. t->Send.BroadcastBytes = PackGetInt64Ex(p, "Ex.Send.BroadcastBytes", i);
  3492. t->Send.BroadcastCount = PackGetInt64Ex(p, "Ex.Send.BroadcastCount", i);
  3493. t->Send.UnicastBytes = PackGetInt64Ex(p, "Ex.Send.UnicastBytes", i);
  3494. t->Send.UnicastCount = PackGetInt64Ex(p, "Ex.Send.UnicastCount", i);
  3495. }
  3496. void OutRpcTrafficEx(TRAFFIC *t, PACK *p, UINT i, UINT num)
  3497. {
  3498. // Validate arguments
  3499. if (t == NULL || p == NULL)
  3500. {
  3501. return;
  3502. }
  3503. PackAddInt64Ex(p, "Ex.Recv.BroadcastBytes", t->Recv.BroadcastBytes, i, num);
  3504. PackAddInt64Ex(p, "Ex.Recv.BroadcastCount", t->Recv.BroadcastCount, i, num);
  3505. PackAddInt64Ex(p, "Ex.Recv.UnicastBytes", t->Recv.UnicastBytes, i, num);
  3506. PackAddInt64Ex(p, "Ex.Recv.UnicastCount", t->Recv.UnicastCount, i, num);
  3507. PackAddInt64Ex(p, "Ex.Send.BroadcastBytes", t->Send.BroadcastBytes, i, num);
  3508. PackAddInt64Ex(p, "Ex.Send.BroadcastCount", t->Send.BroadcastCount, i, num);
  3509. PackAddInt64Ex(p, "Ex.Send.UnicastBytes", t->Send.UnicastBytes, i, num);
  3510. PackAddInt64Ex(p, "Ex.Send.UnicastCount", t->Send.UnicastCount, i, num);
  3511. }
  3512. // TRAFFIC
  3513. void InRpcTraffic(TRAFFIC *t, PACK *p)
  3514. {
  3515. // Validate arguments
  3516. if (t == NULL || p == NULL)
  3517. {
  3518. return;
  3519. }
  3520. Zero(t, sizeof(TRAFFIC));
  3521. t->Recv.BroadcastBytes = PackGetInt64(p, "Recv.BroadcastBytes");
  3522. t->Recv.BroadcastCount = PackGetInt64(p, "Recv.BroadcastCount");
  3523. t->Recv.UnicastBytes = PackGetInt64(p, "Recv.UnicastBytes");
  3524. t->Recv.UnicastCount = PackGetInt64(p, "Recv.UnicastCount");
  3525. t->Send.BroadcastBytes = PackGetInt64(p, "Send.BroadcastBytes");
  3526. t->Send.BroadcastCount = PackGetInt64(p, "Send.BroadcastCount");
  3527. t->Send.UnicastBytes = PackGetInt64(p, "Send.UnicastBytes");
  3528. t->Send.UnicastCount = PackGetInt64(p, "Send.UnicastCount");
  3529. }
  3530. void OutRpcTraffic(PACK *p, TRAFFIC *t)
  3531. {
  3532. // Validate arguments
  3533. if (t == NULL || p == NULL)
  3534. {
  3535. return;
  3536. }
  3537. PackAddInt64(p, "Recv.BroadcastBytes", t->Recv.BroadcastBytes);
  3538. PackAddInt64(p, "Recv.BroadcastCount", t->Recv.BroadcastCount);
  3539. PackAddInt64(p, "Recv.UnicastBytes", t->Recv.UnicastBytes);
  3540. PackAddInt64(p, "Recv.UnicastCount", t->Recv.UnicastCount);
  3541. PackAddInt64(p, "Send.BroadcastBytes", t->Send.BroadcastBytes);
  3542. PackAddInt64(p, "Send.BroadcastCount", t->Send.BroadcastCount);
  3543. PackAddInt64(p, "Send.UnicastBytes", t->Send.UnicastBytes);
  3544. PackAddInt64(p, "Send.UnicastCount", t->Send.UnicastCount);
  3545. }
  3546. // RPC_CERT
  3547. void InRpcCert(RPC_CERT *c, PACK *p)
  3548. {
  3549. BUF *b;
  3550. // Validate arguments
  3551. if (c == NULL || p == NULL)
  3552. {
  3553. return;
  3554. }
  3555. Zero(c, sizeof(RPC_CERT));
  3556. b = PackGetBuf(p, "x");
  3557. if (b == NULL)
  3558. {
  3559. return;
  3560. }
  3561. c->x = BufToX(b, false);
  3562. FreeBuf(b);
  3563. }
  3564. void OutRpcCert(PACK *p, RPC_CERT *c)
  3565. {
  3566. BUF *b;
  3567. // Validate arguments
  3568. if (p == NULL || c == NULL)
  3569. {
  3570. return;
  3571. }
  3572. if (c->x != NULL)
  3573. {
  3574. b = XToBuf(c->x, false);
  3575. PackAddBuf(p, "x", b);
  3576. FreeBuf(b);
  3577. }
  3578. }
  3579. // RPC_CLIENT_DELETE_CA
  3580. void InRpcClientDeleteCa(RPC_CLIENT_DELETE_CA *c, PACK *p)
  3581. {
  3582. // Validate arguments
  3583. if (c == NULL || p == NULL)
  3584. {
  3585. return;
  3586. }
  3587. Zero(c, sizeof(RPC_CLIENT_DELETE_CA));
  3588. c->Key = PackGetInt(p, "Key");
  3589. }
  3590. void OutRpcClientDeleteCa(PACK *p, RPC_CLIENT_DELETE_CA *c)
  3591. {
  3592. // Validate arguments
  3593. if (c == NULL || p == NULL)
  3594. {
  3595. return;
  3596. }
  3597. PackAddInt(p, "Key", c->Key);
  3598. }
  3599. // RPC_GET_CA
  3600. void InRpcGetCa(RPC_GET_CA *c, PACK *p)
  3601. {
  3602. BUF *b;
  3603. // Validate arguments
  3604. if (c == NULL || p == NULL)
  3605. {
  3606. return;
  3607. }
  3608. Zero(c, sizeof(RPC_GET_CA));
  3609. c->Key = PackGetInt(p, "Key");
  3610. b = PackGetBuf(p, "x");
  3611. if (b != NULL)
  3612. {
  3613. c->x = BufToX(b, false);
  3614. FreeBuf(b);
  3615. }
  3616. }
  3617. void OutRpcGetCa(PACK *p, RPC_GET_CA *c)
  3618. {
  3619. // Validate arguments
  3620. if (c == NULL || p == NULL)
  3621. {
  3622. return;
  3623. }
  3624. PackAddInt(p, "Key", c->Key);
  3625. if (c->x != NULL)
  3626. {
  3627. BUF *b = XToBuf(c->x, false);
  3628. PackAddBuf(p, "x", b);
  3629. FreeBuf(b);
  3630. }
  3631. }
  3632. // RPC_CLIENT_ENUM_SECURE
  3633. void InRpcClientEnumSecure(RPC_CLIENT_ENUM_SECURE *e, PACK *p)
  3634. {
  3635. UINT i;
  3636. // Validate arguments
  3637. if (e == NULL || p == NULL)
  3638. {
  3639. return;
  3640. }
  3641. Zero(e, sizeof(RPC_CLIENT_ENUM_SECURE));
  3642. e->NumItem = PackGetNum(p, "NumItem");
  3643. e->Items = ZeroMalloc(sizeof(RPC_CLIENT_ENUM_SECURE_ITEM *) * e->NumItem);
  3644. for (i = 0;i < e->NumItem;i++)
  3645. {
  3646. RPC_CLIENT_ENUM_SECURE_ITEM *item = e->Items[i] = ZeroMalloc(sizeof(RPC_CLIENT_ENUM_SECURE_ITEM));
  3647. item->DeviceId = PackGetIntEx(p, "DeviceId", i);
  3648. item->Type = PackGetIntEx(p, "Type", i);
  3649. PackGetStrEx(p, "DeviceName", item->DeviceName, sizeof(item->DeviceName), i);
  3650. PackGetStrEx(p, "Manufacturer", item->Manufacturer, sizeof(item->Manufacturer), i);
  3651. }
  3652. }
  3653. void OutRpcClientEnumSecure(PACK *p, RPC_CLIENT_ENUM_SECURE *e)
  3654. {
  3655. UINT i;
  3656. // Validate arguments
  3657. if (e == NULL || p == NULL)
  3658. {
  3659. return;
  3660. }
  3661. PackAddNum(p, "NumItem", e->NumItem);
  3662. for (i = 0;i < e->NumItem;i++)
  3663. {
  3664. RPC_CLIENT_ENUM_SECURE_ITEM *item = e->Items[i];
  3665. PackAddIntEx(p, "DeviceId", item->DeviceId, i, e->NumItem);
  3666. PackAddIntEx(p, "Type", item->Type, i, e->NumItem);
  3667. PackAddStrEx(p, "DeviceName", item->DeviceName, i, e->NumItem);
  3668. PackAddStrEx(p, "Manufacturer", item->Manufacturer, i, e->NumItem);
  3669. }
  3670. }
  3671. // RPC_USE_SECURE
  3672. void InRpcUseSecure(RPC_USE_SECURE *u, PACK *p)
  3673. {
  3674. // Validate arguments
  3675. if (u == NULL || p == NULL)
  3676. {
  3677. return;
  3678. }
  3679. Zero(u, sizeof(RPC_USE_SECURE));
  3680. u->DeviceId = PackGetInt(p, "DeviceId");
  3681. }
  3682. void OutRpcUseSecure(PACK *p, RPC_USE_SECURE *u)
  3683. {
  3684. // Validate arguments
  3685. if (u == NULL || p == NULL)
  3686. {
  3687. return;
  3688. }
  3689. PackAddInt(p, "DeviceId", u->DeviceId);
  3690. }
  3691. // Release the RPC_ENUM_OBJECT_IN_SECURE
  3692. void CiFreeEnumObjectInSecure(RPC_ENUM_OBJECT_IN_SECURE *a)
  3693. {
  3694. UINT i;
  3695. // Validate arguments
  3696. if (a == NULL)
  3697. {
  3698. return;
  3699. }
  3700. for (i = 0;i < a->NumItem;i++)
  3701. {
  3702. Free(a->ItemName[i]);
  3703. }
  3704. Free(a->ItemName);
  3705. Free(a->ItemType);
  3706. }
  3707. // RPC_ENUM_OBJECT_IN_SECURE
  3708. void InRpcEnumObjectInSecure(RPC_ENUM_OBJECT_IN_SECURE *e, PACK *p)
  3709. {
  3710. UINT i;
  3711. // Validate arguments
  3712. if (e == NULL || p == NULL)
  3713. {
  3714. return;
  3715. }
  3716. Zero(e, sizeof(RPC_ENUM_OBJECT_IN_SECURE));
  3717. e->NumItem = PackGetNum(p, "NumItem");
  3718. e->hWnd = PackGetInt(p, "hWnd");
  3719. e->ItemName = ZeroMalloc(sizeof(char *) * e->NumItem);
  3720. e->ItemType = ZeroMalloc(sizeof(bool) * e->NumItem);
  3721. for (i = 0;i < e->NumItem;i++)
  3722. {
  3723. char name[MAX_SIZE];
  3724. Zero(name, sizeof(name));
  3725. PackGetStrEx(p, "ItemName", name, sizeof(name), i);
  3726. e->ItemName[i] = CopyStr(name);
  3727. e->ItemType[i] = PackGetIntEx(p, "ItemType", i) ? true : false;
  3728. }
  3729. }
  3730. void OutRpcEnumObjectInSecure(PACK *p, RPC_ENUM_OBJECT_IN_SECURE *e)
  3731. {
  3732. UINT i;
  3733. // Validate arguments
  3734. if (e == NULL || p == NULL)
  3735. {
  3736. return;
  3737. }
  3738. PackAddNum(p, "NumItem", e->NumItem);
  3739. PackAddInt(p, "hWnd", e->hWnd);
  3740. for (i = 0;i < e->NumItem;i++)
  3741. {
  3742. PackAddStrEx(p, "ItemName", e->ItemName[i], i, e->NumItem);
  3743. PackAddIntEx(p, "ItemType", e->ItemType[i], i, e->NumItem);
  3744. }
  3745. }
  3746. // RPC_CLIENT_CREATE_VLAN
  3747. void InRpcCreateVLan(RPC_CLIENT_CREATE_VLAN *v, PACK *p)
  3748. {
  3749. // Validate arguments
  3750. if (v == NULL || p == NULL)
  3751. {
  3752. return;
  3753. }
  3754. Zero(v, sizeof(RPC_CLIENT_CREATE_VLAN));
  3755. PackGetStr(p, "DeviceName", v->DeviceName, sizeof(v->DeviceName));
  3756. }
  3757. void OutRpcCreateVLan(PACK *p, RPC_CLIENT_CREATE_VLAN *v)
  3758. {
  3759. // Validate arguments
  3760. if (v == NULL || p == NULL)
  3761. {
  3762. return;
  3763. }
  3764. PackAddStr(p, "DeviceName", v->DeviceName);
  3765. }
  3766. // RPC_CLIENT_GET_VLAN
  3767. void InRpcClientGetVLan(RPC_CLIENT_GET_VLAN *v, PACK *p)
  3768. {
  3769. // Validate arguments
  3770. if (v == NULL || p == NULL)
  3771. {
  3772. return;
  3773. }
  3774. Zero(v, sizeof(RPC_CLIENT_GET_VLAN));
  3775. PackGetStr(p, "DeviceName", v->DeviceName, sizeof(v->DeviceName));
  3776. v->Enabled = PackGetInt(p, "Enabled") ? true : false;
  3777. PackGetStr(p, "MacAddress", v->MacAddress, sizeof(v->MacAddress));
  3778. PackGetStr(p, "Version", v->Version, sizeof(v->Version));
  3779. PackGetStr(p, "FileName", v->FileName, sizeof(v->FileName));
  3780. PackGetStr(p, "Guid", v->Guid, sizeof(v->Guid));
  3781. }
  3782. void OutRpcClientGetVLan(PACK *p, RPC_CLIENT_GET_VLAN *v)
  3783. {
  3784. // Validate arguments
  3785. if (v == NULL || p == NULL)
  3786. {
  3787. return;
  3788. }
  3789. PackAddStr(p, "DeviceName", v->DeviceName);
  3790. PackAddInt(p, "Enabled", v->Enabled);
  3791. PackAddStr(p, "MacAddress", v->MacAddress);
  3792. PackAddStr(p, "Version", v->Version);
  3793. PackAddStr(p, "FileName", v->FileName);
  3794. PackAddStr(p, "Guid", v->Guid);
  3795. }
  3796. // RPC_CLIENT_SET_VLAN
  3797. void InRpcClientSetVLan(RPC_CLIENT_SET_VLAN *v, PACK *p)
  3798. {
  3799. // Validate arguments
  3800. if (v == NULL || p == NULL)
  3801. {
  3802. return;
  3803. }
  3804. Zero(v, sizeof(RPC_CLIENT_SET_VLAN));
  3805. PackGetStr(p, "DeviceName", v->DeviceName, sizeof(v->DeviceName));
  3806. PackGetStr(p, "MacAddress", v->MacAddress, sizeof(v->MacAddress));
  3807. }
  3808. void OutRpcClientSetVLan(PACK *p, RPC_CLIENT_SET_VLAN *v)
  3809. {
  3810. // Validate arguments
  3811. if (v == NULL || p == NULL)
  3812. {
  3813. return;
  3814. }
  3815. PackAddStr(p, "DeviceName", v->DeviceName);
  3816. PackAddStr(p, "MacAddress", v->MacAddress);
  3817. }
  3818. // RPC_CLIENT_ENUM_VLAN
  3819. void InRpcClientEnumVLan(RPC_CLIENT_ENUM_VLAN *v, PACK *p)
  3820. {
  3821. UINT i;
  3822. // Validate arguments
  3823. if (v == NULL || p == NULL)
  3824. {
  3825. return;
  3826. }
  3827. Zero(v, sizeof(RPC_CLIENT_ENUM_VLAN));
  3828. v->NumItem = PackGetNum(p, "NumItem");
  3829. v->Items = ZeroMalloc(sizeof(RPC_CLIENT_ENUM_VLAN_ITEM *) * v->NumItem);
  3830. for (i = 0;i < v->NumItem;i++)
  3831. {
  3832. RPC_CLIENT_ENUM_VLAN_ITEM *item = v->Items[i] =
  3833. ZeroMalloc(sizeof(RPC_CLIENT_ENUM_VLAN_ITEM));
  3834. PackGetStrEx(p, "DeviceName", item->DeviceName, sizeof(item->DeviceName), i);
  3835. item->Enabled = PackGetIntEx(p, "Enabled", i) ? true : false;
  3836. PackGetStrEx(p, "MacAddress", item->MacAddress, sizeof(item->MacAddress), i);
  3837. PackGetStrEx(p, "Version", item->Version, sizeof(item->Version), i);
  3838. }
  3839. }
  3840. void OutRpcClientEnumVLan(PACK *p, RPC_CLIENT_ENUM_VLAN *v)
  3841. {
  3842. UINT i;
  3843. // Validate arguments
  3844. if (v == NULL || p == NULL)
  3845. {
  3846. return;
  3847. }
  3848. PackAddNum(p, "NumItem", v->NumItem);
  3849. for (i = 0;i < v->NumItem;i++)
  3850. {
  3851. RPC_CLIENT_ENUM_VLAN_ITEM *item = v->Items[i];
  3852. PackAddStrEx(p, "DeviceName", item->DeviceName, i, v->NumItem);
  3853. PackAddIntEx(p, "Enabled", item->Enabled, i, v->NumItem);
  3854. PackAddStrEx(p, "MacAddress", item->MacAddress, i, v->NumItem);
  3855. PackAddStrEx(p, "Version", item->Version, i, v->NumItem);
  3856. }
  3857. }
  3858. // CLIENT_OPTION
  3859. void InRpcClientOption(CLIENT_OPTION *c, PACK *p)
  3860. {
  3861. // Validate arguments
  3862. if (c == NULL || p == NULL)
  3863. {
  3864. return;
  3865. }
  3866. Zero(c, sizeof(CLIENT_OPTION));
  3867. PackGetUniStr(p, "AccountName", c->AccountName, sizeof(c->AccountName));
  3868. PackGetStr(p, "Hostname", c->Hostname, sizeof(c->Hostname));
  3869. c->Port = PackGetInt(p, "Port");
  3870. c->PortUDP = PackGetInt(p, "PortUDP");
  3871. c->ProxyType = PackGetInt(p, "ProxyType");
  3872. c->ProxyPort = PackGetInt(p, "ProxyPort");
  3873. c->NumRetry = PackGetInt(p, "NumRetry");
  3874. c->RetryInterval = PackGetInt(p, "RetryInterval");
  3875. c->MaxConnection = PackGetInt(p, "MaxConnection");
  3876. c->AdditionalConnectionInterval = PackGetInt(p, "AdditionalConnectionInterval");
  3877. c->ConnectionDisconnectSpan = PackGetInt(p, "ConnectionDisconnectSpan");
  3878. c->HideStatusWindow = PackGetBool(p, "HideStatusWindow");
  3879. c->HideNicInfoWindow = PackGetBool(p, "HideNicInfoWindow");
  3880. c->DisableQoS = PackGetBool(p, "DisableQoS");
  3881. PackGetStr(p, "ProxyName", c->ProxyName, sizeof(c->ProxyName));
  3882. PackGetStr(p, "ProxyUsername", c->ProxyUsername, sizeof(c->ProxyUsername));
  3883. PackGetStr(p, "ProxyPassword", c->ProxyPassword, sizeof(c->ProxyPassword));
  3884. PackGetStr(p, "HubName", c->HubName, sizeof(c->HubName));
  3885. PackGetStr(p, "DeviceName", c->DeviceName, sizeof(c->DeviceName));
  3886. c->UseEncrypt = PackGetInt(p, "UseEncrypt") ? true : false;
  3887. c->UseCompress = PackGetInt(p, "UseCompress") ? true : false;
  3888. c->HalfConnection = PackGetInt(p, "HalfConnection") ? true : false;
  3889. c->NoRoutingTracking = PackGetInt(p, "NoRoutingTracking") ? true : false;
  3890. c->RequireMonitorMode = PackGetBool(p, "RequireMonitorMode");
  3891. c->RequireBridgeRoutingMode = PackGetBool(p, "RequireBridgeRoutingMode");
  3892. c->FromAdminPack = PackGetBool(p, "FromAdminPack");
  3893. c->NoTls1 = PackGetBool(p, "NoTls1");
  3894. c->NoUdpAcceleration = PackGetBool(p, "NoUdpAcceleration");
  3895. PackGetData2(p, "HostUniqueKey", c->HostUniqueKey, SHA1_SIZE);
  3896. }
  3897. void OutRpcClientOption(PACK *p, CLIENT_OPTION *c)
  3898. {
  3899. // Validate arguments
  3900. if (c == NULL || p == NULL)
  3901. {
  3902. return;
  3903. }
  3904. PackAddUniStr(p, "AccountName", c->AccountName);
  3905. PackAddStr(p, "Hostname", c->Hostname);
  3906. PackAddStr(p, "ProxyName", c->ProxyName);
  3907. PackAddStr(p, "ProxyUsername", c->ProxyUsername);
  3908. PackAddStr(p, "ProxyPassword", c->ProxyPassword);
  3909. PackAddStr(p, "HubName", c->HubName);
  3910. PackAddStr(p, "DeviceName", c->DeviceName);
  3911. PackAddInt(p, "Port", c->Port);
  3912. PackAddInt(p, "PortUDP", c->PortUDP);
  3913. PackAddInt(p, "ProxyType", c->ProxyType);
  3914. PackAddInt(p, "ProxyPort", c->ProxyPort);
  3915. PackAddInt(p, "NumRetry", c->NumRetry);
  3916. PackAddInt(p, "RetryInterval", c->RetryInterval);
  3917. PackAddInt(p, "MaxConnection", c->MaxConnection);
  3918. PackAddInt(p, "UseEncrypt", c->UseEncrypt);
  3919. PackAddInt(p, "UseCompress", c->UseCompress);
  3920. PackAddInt(p, "HalfConnection", c->HalfConnection);
  3921. PackAddInt(p, "NoRoutingTracking", c->NoRoutingTracking);
  3922. PackAddInt(p, "AdditionalConnectionInterval", c->AdditionalConnectionInterval);
  3923. PackAddInt(p, "ConnectionDisconnectSpan", c->ConnectionDisconnectSpan);
  3924. PackAddBool(p, "HideStatusWindow", c->HideStatusWindow);
  3925. PackAddBool(p, "HideNicInfoWindow", c->HideNicInfoWindow);
  3926. PackAddBool(p, "RequireMonitorMode", c->RequireMonitorMode);
  3927. PackAddBool(p, "RequireBridgeRoutingMode", c->RequireBridgeRoutingMode);
  3928. PackAddBool(p, "DisableQoS", c->DisableQoS);
  3929. PackAddBool(p, "FromAdminPack", c->FromAdminPack);
  3930. PackAddBool(p, "NoTls1", c->NoTls1);
  3931. PackAddBool(p, "NoUdpAcceleration", c->NoUdpAcceleration);
  3932. PackAddData(p, "HostUniqueKey", c->HostUniqueKey, SHA1_SIZE);
  3933. }
  3934. // CLIENT_AUTH
  3935. void InRpcClientAuth(CLIENT_AUTH *c, PACK *p)
  3936. {
  3937. BUF *b;
  3938. // Validate arguments
  3939. if (c == NULL || p == NULL)
  3940. {
  3941. return;
  3942. }
  3943. Zero(c, sizeof(CLIENT_AUTH));
  3944. c->AuthType = PackGetInt(p, "AuthType");
  3945. PackGetStr(p, "Username", c->Username, sizeof(c->Username));
  3946. switch (c->AuthType)
  3947. {
  3948. case CLIENT_AUTHTYPE_ANONYMOUS:
  3949. break;
  3950. case CLIENT_AUTHTYPE_PASSWORD:
  3951. if (PackGetDataSize(p, "HashedPassword") == SHA1_SIZE)
  3952. {
  3953. PackGetData(p, "HashedPassword", c->HashedPassword);
  3954. }
  3955. break;
  3956. case CLIENT_AUTHTYPE_PLAIN_PASSWORD:
  3957. PackGetStr(p, "PlainPassword", c->PlainPassword, sizeof(c->PlainPassword));
  3958. break;
  3959. case CLIENT_AUTHTYPE_CERT:
  3960. b = PackGetBuf(p, "ClientX");
  3961. if (b != NULL)
  3962. {
  3963. c->ClientX = BufToX(b, false);
  3964. FreeBuf(b);
  3965. }
  3966. b = PackGetBuf(p, "ClientK");
  3967. if (b != NULL)
  3968. {
  3969. c->ClientK = BufToK(b, true, false, NULL);
  3970. FreeBuf(b);
  3971. }
  3972. break;
  3973. case CLIENT_AUTHTYPE_SECURE:
  3974. PackGetStr(p, "SecurePublicCertName", c->SecurePublicCertName, sizeof(c->SecurePublicCertName));
  3975. PackGetStr(p, "SecurePrivateKeyName", c->SecurePrivateKeyName, sizeof(c->SecurePrivateKeyName));
  3976. break;
  3977. }
  3978. }
  3979. void OutRpcClientAuth(PACK *p, CLIENT_AUTH *c)
  3980. {
  3981. BUF *b;
  3982. // Validate arguments
  3983. if (c == NULL || p == NULL)
  3984. {
  3985. return;
  3986. }
  3987. PackAddInt(p, "AuthType", c->AuthType);
  3988. PackAddStr(p, "Username", c->Username);
  3989. switch (c->AuthType)
  3990. {
  3991. case CLIENT_AUTHTYPE_ANONYMOUS:
  3992. break;
  3993. case CLIENT_AUTHTYPE_PASSWORD:
  3994. PackAddData(p, "HashedPassword", c->HashedPassword, SHA1_SIZE);
  3995. break;
  3996. case CLIENT_AUTHTYPE_PLAIN_PASSWORD:
  3997. PackAddStr(p, "PlainPassword", c->PlainPassword);
  3998. break;
  3999. case CLIENT_AUTHTYPE_CERT:
  4000. b = XToBuf(c->ClientX, false);
  4001. if (b != NULL)
  4002. {
  4003. PackAddBuf(p, "ClientX", b);
  4004. FreeBuf(b);
  4005. }
  4006. b = KToBuf(c->ClientK, false, NULL);
  4007. if (b != NULL)
  4008. {
  4009. PackAddBuf(p, "ClientK", b);
  4010. FreeBuf(b);
  4011. }
  4012. break;
  4013. case CLIENT_AUTHTYPE_SECURE:
  4014. PackAddStr(p, "SecurePublicCertName", c->SecurePublicCertName);
  4015. PackAddStr(p, "SecurePrivateKeyName", c->SecurePrivateKeyName);
  4016. break;
  4017. }
  4018. }
  4019. // RPC_CLIENT_CREATE_ACCOUNT
  4020. void InRpcClientCreateAccount(RPC_CLIENT_CREATE_ACCOUNT *c, PACK *p)
  4021. {
  4022. BUF *b;
  4023. // Validate arguments
  4024. if (c == NULL || p == NULL)
  4025. {
  4026. return;
  4027. }
  4028. Zero(c, sizeof(RPC_CLIENT_CREATE_ACCOUNT));
  4029. c->ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
  4030. c->ClientAuth = ZeroMalloc(sizeof(CLIENT_AUTH));
  4031. InRpcClientOption(c->ClientOption, p);
  4032. InRpcClientAuth(c->ClientAuth, p);
  4033. c->StartupAccount = PackGetInt(p, "StartupAccount") ? true : false;
  4034. c->CheckServerCert = PackGetInt(p, "CheckServerCert") ? true : false;
  4035. b = PackGetBuf(p, "ServerCert");
  4036. if (b != NULL)
  4037. {
  4038. c->ServerCert = BufToX(b, false);
  4039. FreeBuf(b);
  4040. }
  4041. PackGetData2(p, "ShortcutKey", c->ShortcutKey, sizeof(c->ShortcutKey));
  4042. }
  4043. void OutRpcClientCreateAccount(PACK *p, RPC_CLIENT_CREATE_ACCOUNT *c)
  4044. {
  4045. BUF *b;
  4046. // Validate arguments
  4047. if (c == NULL || p == NULL)
  4048. {
  4049. return;
  4050. }
  4051. OutRpcClientOption(p, c->ClientOption);
  4052. OutRpcClientAuth(p, c->ClientAuth);
  4053. PackAddInt(p, "StartupAccount", c->StartupAccount);
  4054. PackAddInt(p, "CheckServerCert", c->CheckServerCert);
  4055. if (c->ServerCert != NULL)
  4056. {
  4057. b = XToBuf(c->ServerCert, false);
  4058. if (b != NULL)
  4059. {
  4060. PackAddBuf(p, "ServerCert", b);
  4061. FreeBuf(b);
  4062. }
  4063. }
  4064. PackAddData(p, "ShortcutKey", c->ShortcutKey, sizeof(c->ShortcutKey));
  4065. }
  4066. // RPC_CLIENT_ENUM_ACCOUNT
  4067. void InRpcClientEnumAccount(RPC_CLIENT_ENUM_ACCOUNT *e, PACK *p)
  4068. {
  4069. UINT i;
  4070. // Validate arguments
  4071. if (e == NULL || p == NULL)
  4072. {
  4073. return;
  4074. }
  4075. Zero(e, sizeof(RPC_CLIENT_ENUM_ACCOUNT));
  4076. e->NumItem = PackGetNum(p, "NumItem");
  4077. e->Items = ZeroMalloc(sizeof(RPC_CLIENT_ENUM_ACCOUNT_ITEM *) * e->NumItem);
  4078. for (i = 0;i < e->NumItem;i++)
  4079. {
  4080. RPC_CLIENT_ENUM_ACCOUNT_ITEM *item = e->Items[i] =
  4081. ZeroMalloc(sizeof(RPC_CLIENT_ENUM_ACCOUNT_ITEM));
  4082. PackGetUniStrEx(p, "AccountName", item->AccountName, sizeof(item->AccountName), i);
  4083. PackGetStrEx(p, "UserName", item->UserName, sizeof(item->UserName), i);
  4084. PackGetStrEx(p, "ServerName", item->ServerName, sizeof(item->ServerName), i);
  4085. PackGetStrEx(p, "ProxyName", item->ProxyName, sizeof(item->ProxyName), i);
  4086. PackGetStrEx(p, "DeviceName", item->DeviceName, sizeof(item->DeviceName), i);
  4087. item->ProxyType = PackGetIntEx(p, "ProxyType", i);
  4088. item->Active = PackGetIntEx(p, "Active", i) ? true : false;
  4089. item->StartupAccount = PackGetIntEx(p, "StartupAccount", i) ? true : false;
  4090. item->Connected = PackGetBoolEx(p, "Connected", i);
  4091. item->Port = PackGetIntEx(p, "Port", i);
  4092. PackGetStrEx(p, "HubName", item->HubName, sizeof(item->HubName), i);
  4093. item->CreateDateTime = PackGetInt64Ex(p, "CreateDateTime", i);
  4094. item->UpdateDateTime = PackGetInt64Ex(p, "UpdateDateTime", i);
  4095. item->LastConnectDateTime = PackGetInt64Ex(p, "LastConnectDateTime", i);
  4096. }
  4097. }
  4098. void OutRpcClientEnumAccount(PACK *p, RPC_CLIENT_ENUM_ACCOUNT *e)
  4099. {
  4100. UINT i;
  4101. // Validate arguments
  4102. if (e == NULL || p == NULL)
  4103. {
  4104. return;
  4105. }
  4106. PackAddNum(p, "NumItem", e->NumItem);
  4107. for (i = 0;i < e->NumItem;i++)
  4108. {
  4109. RPC_CLIENT_ENUM_ACCOUNT_ITEM *item = e->Items[i];
  4110. PackAddUniStrEx(p, "AccountName", item->AccountName, i, e->NumItem);
  4111. PackAddStrEx(p, "UserName", item->UserName, i, e->NumItem);
  4112. PackAddStrEx(p, "ServerName", item->ServerName, i, e->NumItem);
  4113. PackAddStrEx(p, "ProxyName", item->ProxyName, i, e->NumItem);
  4114. PackAddStrEx(p, "DeviceName", item->DeviceName, i, e->NumItem);
  4115. PackAddIntEx(p, "ProxyType", item->ProxyType, i, e->NumItem);
  4116. PackAddIntEx(p, "Active", item->Active, i, e->NumItem);
  4117. PackAddIntEx(p, "StartupAccount", item->StartupAccount, i, e->NumItem);
  4118. PackAddBoolEx(p, "Connected", item->Connected, i, e->NumItem);
  4119. PackAddIntEx(p, "Port", item->Port, i, e->NumItem);
  4120. PackAddStrEx(p, "HubName", item->HubName, i, e->NumItem);
  4121. PackAddInt64Ex(p, "CreateDateTime", item->CreateDateTime, i, e->NumItem);
  4122. PackAddInt64Ex(p, "UpdateDateTime", item->UpdateDateTime, i, e->NumItem);
  4123. PackAddInt64Ex(p, "LastConnectDateTime", item->LastConnectDateTime, i, e->NumItem);
  4124. }
  4125. }
  4126. // RPC_CLIENT_DELETE_ACCOUNT
  4127. void InRpcClientDeleteAccount(RPC_CLIENT_DELETE_ACCOUNT *a, PACK *p)
  4128. {
  4129. // Validate arguments
  4130. if (a == NULL || p == NULL)
  4131. {
  4132. return;
  4133. }
  4134. Zero(a, sizeof(RPC_CLIENT_DELETE_ACCOUNT));
  4135. PackGetUniStr(p, "AccountName", a->AccountName, sizeof(a->AccountName));
  4136. }
  4137. void OutRpcClientDeleteAccount(PACK *p, RPC_CLIENT_DELETE_ACCOUNT *a)
  4138. {
  4139. // Validate arguments
  4140. if (a == NULL || p == NULL)
  4141. {
  4142. return;
  4143. }
  4144. PackAddUniStr(p, "AccountName", a->AccountName);
  4145. }
  4146. // RPC_RENAME_ACCOUNT
  4147. void InRpcRenameAccount(RPC_RENAME_ACCOUNT *a, PACK *p)
  4148. {
  4149. // Validate arguments
  4150. if (a == NULL || p == NULL)
  4151. {
  4152. return;
  4153. }
  4154. Zero(a, sizeof(RPC_RENAME_ACCOUNT));
  4155. PackGetUniStr(p, "OldName", a->OldName, sizeof(a->OldName));
  4156. PackGetUniStr(p, "NewName", a->NewName, sizeof(a->NewName));
  4157. }
  4158. void OutRpcRenameAccount(PACK *p, RPC_RENAME_ACCOUNT *a)
  4159. {
  4160. // Validate arguments
  4161. if (a == NULL || p == NULL)
  4162. {
  4163. return;
  4164. }
  4165. PackAddUniStr(p, "OldName", a->OldName);
  4166. PackAddUniStr(p, "NewName", a->NewName);
  4167. }
  4168. // RPC_CLIENT_GET_ACCOUNT
  4169. void InRpcClientGetAccount(RPC_CLIENT_GET_ACCOUNT *c, PACK *p)
  4170. {
  4171. BUF *b;
  4172. // Validate arguments
  4173. if (c == NULL || p == NULL)
  4174. {
  4175. return;
  4176. }
  4177. Zero(c, sizeof(RPC_CLIENT_GET_ACCOUNT));
  4178. c->ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
  4179. c->ClientAuth = ZeroMalloc(sizeof(CLIENT_AUTH));
  4180. PackGetUniStr(p, "AccountName", c->AccountName, sizeof(c->AccountName));
  4181. c->StartupAccount = PackGetInt(p, "StartupAccount") ? true : false;
  4182. c->CheckServerCert = PackGetInt(p, "CheckServerCert") ? true : false;
  4183. b = PackGetBuf(p, "ServerCert");
  4184. if (b != NULL)
  4185. {
  4186. c->ServerCert = BufToX(b, false);
  4187. FreeBuf(b);
  4188. }
  4189. InRpcClientOption(c->ClientOption, p);
  4190. InRpcClientAuth(c->ClientAuth, p);
  4191. c->CreateDateTime = PackGetInt64(p, "CreateDateTime");
  4192. c->UpdateDateTime = PackGetInt64(p, "UpdateDateTime");
  4193. c->LastConnectDateTime = PackGetInt64(p, "LastConnectDateTime");
  4194. PackGetData2(p, "ShortcutKey", c->ShortcutKey, SHA1_SIZE);
  4195. }
  4196. void OutRpcClientGetAccount(PACK *p, RPC_CLIENT_GET_ACCOUNT *c)
  4197. {
  4198. BUF *b;
  4199. // Validate arguments
  4200. if (c == NULL || p == NULL)
  4201. {
  4202. return;
  4203. }
  4204. PackAddUniStr(p, "AccountName", c->AccountName);
  4205. PackAddInt(p, "StartupAccount", c->StartupAccount);
  4206. PackAddInt(p, "CheckServerCert", c->CheckServerCert);
  4207. if (c->ServerCert != NULL)
  4208. {
  4209. b = XToBuf(c->ServerCert, false);
  4210. if (b != NULL)
  4211. {
  4212. PackAddBuf(p, "ServerCert", b);
  4213. FreeBuf(b);
  4214. }
  4215. }
  4216. OutRpcClientOption(p, c->ClientOption);
  4217. OutRpcClientAuth(p, c->ClientAuth);
  4218. PackAddData(p, "ShortcutKey", c->ShortcutKey, SHA1_SIZE);
  4219. PackAddInt64(p, "CreateDateTime", c->CreateDateTime);
  4220. PackAddInt64(p, "UpdateDateTime", c->UpdateDateTime);
  4221. PackAddInt64(p, "LastConnectDateTime", c->LastConnectDateTime);
  4222. }
  4223. // RPC_CLIENT_CONNECT
  4224. void InRpcClientConnect(RPC_CLIENT_CONNECT *c, PACK *p)
  4225. {
  4226. // Validate arguments
  4227. if (c == NULL || p == NULL)
  4228. {
  4229. return;
  4230. }
  4231. Zero(c, sizeof(RPC_CLIENT_CONNECT));
  4232. PackGetUniStr(p, "AccountName", c->AccountName, sizeof(c->AccountName));
  4233. }
  4234. void OutRpcClientConnect(PACK *p, RPC_CLIENT_CONNECT *c)
  4235. {
  4236. // Validate arguments
  4237. if (c == NULL || p == NULL)
  4238. {
  4239. return;
  4240. }
  4241. PackAddUniStr(p, "AccountName", c->AccountName);
  4242. }
  4243. // POLICY
  4244. void InRpcPolicy(POLICY *o, PACK *p)
  4245. {
  4246. POLICY *pol;
  4247. // Validate arguments
  4248. if (o == NULL || p == NULL)
  4249. {
  4250. return;
  4251. }
  4252. pol = PackGetPolicy(p);
  4253. Copy(o, pol, sizeof(POLICY));
  4254. Free(pol);
  4255. }
  4256. void OutRpcPolicy(PACK *p, POLICY *o)
  4257. {
  4258. // Validate arguments
  4259. if (o == NULL || p == NULL)
  4260. {
  4261. return;
  4262. }
  4263. PackAddPolicy(p, o);
  4264. }
  4265. // RPC_CLIENT_GET_CONNECTION_STATUS
  4266. void InRpcClientGetConnectionStatus(RPC_CLIENT_GET_CONNECTION_STATUS *s, PACK *p)
  4267. {
  4268. BUF *b;
  4269. // Validate arguments
  4270. if (s == NULL || p == NULL)
  4271. {
  4272. return;
  4273. }
  4274. Zero(s, sizeof(RPC_CLIENT_GET_CONNECTION_STATUS));
  4275. PackGetUniStr(p, "AccountName", s->AccountName, sizeof(s->AccountName));
  4276. PackGetStr(p, "ServerName", s->ServerName, sizeof(s->ServerName));
  4277. PackGetStr(p, "ServerProductName", s->ServerProductName, sizeof(s->ServerProductName));
  4278. PackGetStr(p, "CipherName", s->CipherName, sizeof(s->CipherName));
  4279. PackGetStr(p, "SessionName", s->SessionName, sizeof(s->SessionName));
  4280. PackGetStr(p, "ConnectionName", s->ConnectionName, sizeof(s->ConnectionName));
  4281. if (PackGetDataSize(p, "SessionKey") == SHA1_SIZE)
  4282. {
  4283. PackGetData(p, "SessionKey", s->SessionKey);
  4284. }
  4285. s->SessionStatus = PackGetInt(p, "SessionStatus");
  4286. s->ServerPort = PackGetInt(p, "ServerPort");
  4287. s->ServerProductVer = PackGetInt(p, "ServerProductVer");
  4288. s->ServerProductBuild = PackGetInt(p, "ServerProductBuild");
  4289. s->NumConnectionsEatablished = PackGetInt(p, "NumConnectionsEatablished");
  4290. s->MaxTcpConnections = PackGetInt(p, "MaxTcpConnections");
  4291. s->NumTcpConnections = PackGetInt(p, "NumTcpConnections");
  4292. s->NumTcpConnectionsUpload = PackGetInt(p, "NumTcpConnectionsUpload");
  4293. s->NumTcpConnectionsDownload = PackGetInt(p, "NumTcpConnectionsDownload");
  4294. s->StartTime = PackGetInt64(p, "StartTime");
  4295. s->FirstConnectionEstablisiedTime = PackGetInt64(p, "FirstConnectionEstablisiedTime");
  4296. s->CurrentConnectionEstablishTime = PackGetInt64(p, "CurrentConnectionEstablishTime");
  4297. s->TotalSendSize = PackGetInt64(p, "TotalSendSize");
  4298. s->TotalRecvSize = PackGetInt64(p, "TotalRecvSize");
  4299. s->TotalSendSizeReal = PackGetInt64(p, "TotalSendSizeReal");
  4300. s->TotalRecvSizeReal = PackGetInt64(p, "TotalRecvSizeReal");
  4301. s->Active = PackGetInt(p, "Active") ? true : false;
  4302. s->Connected = PackGetInt(p, "Connected") ? true : false;
  4303. s->HalfConnection = PackGetInt(p, "HalfConnection") ? true : false;
  4304. s->QoS = PackGetInt(p, "QoS") ? true : false;
  4305. s->UseEncrypt = PackGetInt(p, "UseEncrypt") ? true : false;
  4306. s->UseCompress = PackGetInt(p, "UseCompress") ? true : false;
  4307. s->IsRUDPSession = PackGetInt(p, "IsRUDPSession") ? true : false;
  4308. PackGetStr(p, "UnderlayProtocol", s->UnderlayProtocol, sizeof(s->UnderlayProtocol));
  4309. s->IsUdpAccelerationEnabled = PackGetInt(p, "IsUdpAccelerationEnabled") ? true : false;
  4310. s->IsUsingUdpAcceleration = PackGetInt(p, "IsUsingUdpAcceleration") ? true : false;
  4311. s->IsBridgeMode = PackGetBool(p, "IsBridgeMode");
  4312. s->IsMonitorMode = PackGetBool(p, "IsMonitorMode");
  4313. s->VLanId = PackGetInt(p, "VLanId");
  4314. b = PackGetBuf(p, "ServerX");
  4315. if (b != NULL)
  4316. {
  4317. s->ServerX = BufToX(b, false);
  4318. FreeBuf(b);
  4319. }
  4320. b = PackGetBuf(p, "ClientX");
  4321. if (b != NULL)
  4322. {
  4323. s->ClientX = BufToX(b, false);
  4324. FreeBuf(b);
  4325. }
  4326. InRpcPolicy(&s->Policy, p);
  4327. InRpcTraffic(&s->Traffic, p);
  4328. }
  4329. void OutRpcClientGetConnectionStatus(PACK *p, RPC_CLIENT_GET_CONNECTION_STATUS *c)
  4330. {
  4331. BUF *b;
  4332. // Validate arguments
  4333. if (p == NULL || c == NULL)
  4334. {
  4335. return;
  4336. }
  4337. PackAddUniStr(p, "AccountName", c->AccountName);
  4338. PackAddStr(p, "ServerName", c->ServerName);
  4339. PackAddStr(p, "ServerProductName", c->ServerProductName);
  4340. PackAddStr(p, "CipherName", c->CipherName);
  4341. PackAddStr(p, "SessionName", c->SessionName);
  4342. PackAddStr(p, "ConnectionName", c->ConnectionName);
  4343. PackAddData(p, "SessionKey", c->SessionKey, SHA1_SIZE);
  4344. PackAddInt(p, "Active", c->Active);
  4345. PackAddInt(p, "Connected", c->Connected);
  4346. PackAddInt(p, "SessionStatus", c->SessionStatus);
  4347. PackAddInt(p, "ServerPort", c->ServerPort);
  4348. PackAddInt(p, "ServerProductVer", c->ServerProductVer);
  4349. PackAddInt(p, "ServerProductBuild", c->ServerProductBuild);
  4350. PackAddInt(p, "NumConnectionsEatablished", c->NumConnectionsEatablished);
  4351. PackAddInt(p, "HalfConnection", c->HalfConnection);
  4352. PackAddInt(p, "QoS", c->QoS);
  4353. PackAddInt(p, "MaxTcpConnections", c->MaxTcpConnections);
  4354. PackAddInt(p, "NumTcpConnections", c->NumTcpConnections);
  4355. PackAddInt(p, "NumTcpConnectionsUpload", c->NumTcpConnectionsUpload);
  4356. PackAddInt(p, "NumTcpConnectionsDownload", c->NumTcpConnectionsDownload);
  4357. PackAddInt(p, "UseEncrypt", c->UseEncrypt);
  4358. PackAddInt(p, "UseCompress", c->UseCompress);
  4359. PackAddInt(p, "IsRUDPSession", c->IsRUDPSession);
  4360. PackAddStr(p, "UnderlayProtocol", c->UnderlayProtocol);
  4361. PackAddInt(p, "IsUdpAccelerationEnabled", c->IsUdpAccelerationEnabled);
  4362. PackAddInt(p, "IsUsingUdpAcceleration", c->IsUsingUdpAcceleration);
  4363. PackAddBool(p, "IsBridgeMode", c->IsBridgeMode);
  4364. PackAddBool(p, "IsMonitorMode", c->IsMonitorMode);
  4365. PackAddInt64(p, "StartTime", c->StartTime);
  4366. PackAddInt64(p, "FirstConnectionEstablisiedTime", c->FirstConnectionEstablisiedTime);
  4367. PackAddInt64(p, "CurrentConnectionEstablishTime", c->CurrentConnectionEstablishTime);
  4368. PackAddInt64(p, "TotalSendSize", c->TotalSendSize);
  4369. PackAddInt64(p, "TotalRecvSize", c->TotalRecvSize);
  4370. PackAddInt64(p, "TotalSendSizeReal", c->TotalSendSizeReal);
  4371. PackAddInt64(p, "TotalRecvSizeReal", c->TotalRecvSizeReal);
  4372. PackAddInt(p, "VLanId", c->VLanId);
  4373. OutRpcPolicy(p, &c->Policy);
  4374. OutRpcTraffic(p, &c->Traffic);
  4375. if (c->ServerX != NULL)
  4376. {
  4377. b = XToBuf(c->ServerX, false);
  4378. PackAddBuf(p, "ServerX", b);
  4379. FreeBuf(b);
  4380. }
  4381. if (c->ClientX != NULL)
  4382. {
  4383. b = XToBuf(c->ClientX, false);
  4384. PackAddBuf(p, "ClientX", b);
  4385. FreeBuf(b);
  4386. }
  4387. }
  4388. void InRpcClientNotify(RPC_CLIENT_NOTIFY *n, PACK *p)
  4389. {
  4390. // Validate arguments
  4391. if (n == NULL || p == NULL)
  4392. {
  4393. return;
  4394. }
  4395. Zero(n, sizeof(RPC_CLIENT_NOTIFY));
  4396. n->NotifyCode = PackGetInt(p, "NotifyCode");
  4397. }
  4398. void OutRpcClientNotify(PACK *p, RPC_CLIENT_NOTIFY *n)
  4399. {
  4400. // Validate arguments
  4401. if (n == NULL || p == NULL)
  4402. {
  4403. return;
  4404. }
  4405. PackAddInt(p, "NotifyCode", n->NotifyCode);
  4406. }
  4407. // Notification main
  4408. void CiNotifyMain(CLIENT *c, SOCK *s)
  4409. {
  4410. CANCEL *cancel;
  4411. // Validate arguments
  4412. if (c == NULL || s == NULL)
  4413. {
  4414. return;
  4415. }
  4416. // Register a Cencel
  4417. cancel = NewCancel();
  4418. LockList(c->NotifyCancelList);
  4419. {
  4420. Add(c->NotifyCancelList, cancel);
  4421. }
  4422. UnlockList(c->NotifyCancelList);
  4423. // Wait
  4424. while (true)
  4425. {
  4426. char ch = '@';
  4427. SOCKSET set;
  4428. InitSockSet(&set);
  4429. AddSockSet(&set, s);
  4430. Select(&set, INFINITE, cancel, NULL);
  4431. if (c->Halt)
  4432. {
  4433. // Abort
  4434. break;
  4435. }
  4436. // 1 byte transmission
  4437. if (Send(s, &ch, 1, false) == 0)
  4438. {
  4439. // Disconnected
  4440. break;
  4441. }
  4442. }
  4443. // Disconnect
  4444. Disconnect(s);
  4445. // Unregister the Cancel
  4446. LockList(c->NotifyCancelList);
  4447. {
  4448. Delete(c->NotifyCancelList, cancel);
  4449. }
  4450. UnlockList(c->NotifyCancelList);
  4451. ReleaseCancel(cancel);
  4452. }
  4453. // RPC acceptance code
  4454. void CiRpcAccepted(CLIENT *c, SOCK *s)
  4455. {
  4456. UCHAR hashed_password[SHA1_SIZE];
  4457. UINT rpc_mode;
  4458. UINT retcode;
  4459. RPC *rpc;
  4460. // Validate arguments
  4461. if (c == NULL || s == NULL)
  4462. {
  4463. return;
  4464. }
  4465. // Receive the RPC mode
  4466. if (RecvAll(s, &rpc_mode, sizeof(UINT), false) == false)
  4467. {
  4468. return;
  4469. }
  4470. rpc_mode = Endian32(rpc_mode);
  4471. if (rpc_mode == CLIENT_RPC_MODE_NOTIFY)
  4472. {
  4473. // Notification mode
  4474. CiNotifyMain(c, s);
  4475. return;
  4476. }
  4477. else if (rpc_mode == CLIENT_RPC_MODE_SHORTCUT || rpc_mode == CLIENT_RPC_MODE_SHORTCUT_DISCONNECT)
  4478. {
  4479. // Shortcut key received
  4480. UCHAR key[SHA1_SIZE];
  4481. UINT err = ERR_NO_ERROR;
  4482. if (RecvAll(s, key, SHA1_SIZE, false))
  4483. {
  4484. UINT i;
  4485. wchar_t title[MAX_ACCOUNT_NAME_LEN + 1];
  4486. bool ok = false;
  4487. // Connect to the specified setting
  4488. LockList(c->AccountList);
  4489. {
  4490. for (i = 0;i < LIST_NUM(c->AccountList);i++)
  4491. {
  4492. ACCOUNT *a = LIST_DATA(c->AccountList, i);
  4493. Lock(a->lock);
  4494. {
  4495. if (Cmp(a->ShortcutKey, key, SHA1_SIZE) == 0)
  4496. {
  4497. ok = true;
  4498. UniStrCpy(title, sizeof(title), a->ClientOption->AccountName);
  4499. }
  4500. }
  4501. Unlock(a->lock);
  4502. }
  4503. }
  4504. UnlockList(c->AccountList);
  4505. if (ok == false)
  4506. {
  4507. err = ERR_ACCOUNT_NOT_FOUND;
  4508. }
  4509. else
  4510. {
  4511. RPC_CLIENT_CONNECT t;
  4512. Zero(&t, sizeof(t));
  4513. UniStrCpy(t.AccountName, sizeof(t.AccountName), title);
  4514. if (rpc_mode == CLIENT_RPC_MODE_SHORTCUT)
  4515. {
  4516. // Connect
  4517. if (CtConnect(c, &t))
  4518. {
  4519. err = ERR_NO_ERROR;
  4520. }
  4521. else
  4522. {
  4523. err = c->Err;
  4524. }
  4525. }
  4526. else
  4527. {
  4528. // Connect
  4529. if (CtDisconnect(c, &t, false))
  4530. {
  4531. err = ERR_NO_ERROR;
  4532. }
  4533. else
  4534. {
  4535. err = c->Err;
  4536. }
  4537. }
  4538. }
  4539. err = Endian32(err);
  4540. SendAll(s, &err, sizeof(UINT), false);
  4541. RecvAll(s, &err, sizeof(UINT), false);
  4542. }
  4543. return;
  4544. }
  4545. // Password reception
  4546. if (RecvAll(s, hashed_password, SHA1_SIZE, false) == false)
  4547. {
  4548. return;
  4549. }
  4550. retcode = 0;
  4551. // Password comparison
  4552. if (Cmp(hashed_password, c->EncryptedPassword, SHA1_SIZE) != 0)
  4553. {
  4554. retcode = 1;
  4555. }
  4556. if (c->PasswordRemoteOnly && s->RemoteIP.addr[0] == 127)
  4557. {
  4558. // If in a mode that requires a password only remote,
  4559. // the password sent from localhost is considered to be always correct
  4560. retcode = 0;
  4561. }
  4562. Lock(c->lock);
  4563. {
  4564. if (c->Config.AllowRemoteConfig == false)
  4565. {
  4566. // If the remote control is prohibited,
  4567. // identify whether this connection is from remote
  4568. if (s->RemoteIP.addr[0] != 127)
  4569. {
  4570. retcode = 2;
  4571. }
  4572. }
  4573. }
  4574. Unlock(c->lock);
  4575. retcode = Endian32(retcode);
  4576. // Error code transmission
  4577. if (SendAll(s, &retcode, sizeof(UINT), false) == false)
  4578. {
  4579. return;
  4580. }
  4581. if (retcode != 0)
  4582. {
  4583. // Disconnect due to an error
  4584. return;
  4585. }
  4586. // Create a RPC server
  4587. rpc = StartRpcServer(s, CiRpcDispatch, c);
  4588. // RPC server operation
  4589. RpcServer(rpc);
  4590. // Release the RPC server
  4591. EndRpc(rpc);
  4592. }
  4593. // RPC acceptance thread
  4594. void CiRpcAcceptThread(THREAD *thread, void *param)
  4595. {
  4596. CLIENT_RPC_CONNECTION *conn;
  4597. CLIENT *c;
  4598. SOCK *s;
  4599. // Validate arguments
  4600. if (thread == NULL || param == NULL)
  4601. {
  4602. return;
  4603. }
  4604. conn = (CLIENT_RPC_CONNECTION *)param;
  4605. s = conn->Sock;
  4606. c = conn->Client;
  4607. AddRef(s->ref);
  4608. // Add to the RPC connection list
  4609. LockList(c->RpcConnectionList);
  4610. {
  4611. Add(c->RpcConnectionList, conn);
  4612. }
  4613. UnlockList(c->RpcConnectionList);
  4614. NoticeThreadInit(thread);
  4615. // Main process
  4616. CiRpcAccepted(c, s);
  4617. // Release from the connection list
  4618. LockList(c->RpcConnectionList);
  4619. {
  4620. Delete(c->RpcConnectionList, conn);
  4621. }
  4622. UnlockList(c->RpcConnectionList);
  4623. ReleaseSock(conn->Sock);
  4624. ReleaseThread(conn->Thread);
  4625. Free(conn);
  4626. Disconnect(s);
  4627. ReleaseSock(s);
  4628. }
  4629. // RPC server thread
  4630. void CiRpcServerThread(THREAD *thread, void *param)
  4631. {
  4632. CLIENT *c;
  4633. SOCK *listener;
  4634. UINT i;
  4635. LIST *thread_list;
  4636. // Validate arguments
  4637. if (thread == NULL || param == NULL)
  4638. {
  4639. return;
  4640. }
  4641. c = (CLIENT *)param;
  4642. // RPC connection list
  4643. c->RpcConnectionList = NewList(NULL);
  4644. // Open the port
  4645. listener = NULL;
  4646. for (i = CLIENT_CONFIG_PORT;i < (CLIENT_CONFIG_PORT + 5);i++)
  4647. {
  4648. listener = Listen(i);
  4649. if (listener != NULL)
  4650. {
  4651. break;
  4652. }
  4653. }
  4654. if (listener == NULL)
  4655. {
  4656. // Error
  4657. Alert(CEDAR_PRODUCT_STR " VPN Client RPC Port Open Failed.", CEDAR_CLIENT_STR);
  4658. return;
  4659. }
  4660. #ifdef OS_WIN32
  4661. MsRegWriteIntEx2(REG_LOCAL_MACHINE, CLIENT_WIN32_REGKEYNAME, CLIENT_WIN32_REGVALUE_PORT, i, false, true);
  4662. MsRegWriteIntEx2(REG_LOCAL_MACHINE, CLIENT_WIN32_REGKEYNAME, CLIENT_WIN32_REGVALUE_PID, MsGetCurrentProcessId(), false, true);
  4663. #endif // OS_WIN32
  4664. c->RpcListener = listener;
  4665. AddRef(listener->ref);
  4666. NoticeThreadInit(thread);
  4667. while (true)
  4668. {
  4669. // Wait for client connection
  4670. CLIENT_RPC_CONNECTION *conn;
  4671. SOCK *s = Accept(listener);
  4672. if (s == NULL)
  4673. {
  4674. // Stop
  4675. break;
  4676. }
  4677. // Create a client processing thread
  4678. conn = ZeroMalloc(sizeof(CLIENT_RPC_CONNECTION));
  4679. conn->Client = c;
  4680. conn->Sock = s;
  4681. AddRef(s->ref);
  4682. conn->Thread = NewThread(CiRpcAcceptThread, (void *)conn);
  4683. WaitThreadInit(conn->Thread);
  4684. ReleaseSock(s);
  4685. }
  4686. // Release the listener
  4687. ReleaseSock(listener);
  4688. thread_list = NewListFast(NULL);
  4689. // Set all the event notification
  4690. LockList(c->NotifyCancelList);
  4691. {
  4692. UINT i;
  4693. for (i = 0;i < LIST_NUM(c->NotifyCancelList);i++)
  4694. {
  4695. CANCEL *cancel = LIST_DATA(c->NotifyCancelList, i);
  4696. Cancel(cancel);
  4697. }
  4698. }
  4699. UnlockList(c->NotifyCancelList);
  4700. // Disconnect all the connections of connected yet
  4701. LockList(c->RpcConnectionList);
  4702. {
  4703. for (i = 0;i < LIST_NUM(c->RpcConnectionList);i++)
  4704. {
  4705. CLIENT_RPC_CONNECTION *cc = LIST_DATA(c->RpcConnectionList, i);
  4706. AddRef(cc->Thread->ref);
  4707. Add(thread_list, cc->Thread);
  4708. Disconnect(cc->Sock);
  4709. }
  4710. }
  4711. UnlockList(c->RpcConnectionList);
  4712. for (i = 0;i < LIST_NUM(thread_list);i++)
  4713. {
  4714. THREAD *t = LIST_DATA(thread_list, i);
  4715. WaitThread(t, INFINITE);
  4716. ReleaseThread(t);
  4717. }
  4718. ReleaseList(c->RpcConnectionList);
  4719. ReleaseList(thread_list);
  4720. #ifdef OS_WIN32
  4721. MsRegDeleteValueEx2(REG_LOCAL_MACHINE, CLIENT_WIN32_REGKEYNAME, CLIENT_WIN32_REGVALUE_PORT, false, true);
  4722. MsRegDeleteValueEx2(REG_LOCAL_MACHINE, CLIENT_WIN32_REGKEYNAME, CLIENT_WIN32_REGVALUE_PID, false, true);
  4723. #endif // OS_WIN32
  4724. }
  4725. // Start the Keep
  4726. void CiInitKeep(CLIENT *c)
  4727. {
  4728. // Validate arguments
  4729. if (c == NULL)
  4730. {
  4731. return;
  4732. }
  4733. c->Keep = StartKeep();
  4734. // Apply settings
  4735. if (c->Config.UseKeepConnect)
  4736. {
  4737. KEEP *k = c->Keep;
  4738. Lock(k->lock);
  4739. {
  4740. StrCpy(k->ServerName, sizeof(k->ServerName), c->Config.KeepConnectHost);
  4741. k->ServerPort = c->Config.KeepConnectPort;
  4742. k->Interval = c->Config.KeepConnectInterval * 1000;
  4743. k->UdpMode = (c->Config.KeepConnectProtocol == CONNECTION_UDP) ? true : false;
  4744. k->Enable = true;
  4745. }
  4746. Unlock(k->lock);
  4747. }
  4748. }
  4749. // Stop the Keep
  4750. void CiFreeKeep(CLIENT *c)
  4751. {
  4752. // Validate arguments
  4753. if (c == NULL)
  4754. {
  4755. return;
  4756. }
  4757. StopKeep(c->Keep);
  4758. c->Keep = NULL;
  4759. }
  4760. // Start the RPC
  4761. void CiStartRpcServer(CLIENT *c)
  4762. {
  4763. // Validate arguments
  4764. if (c == NULL)
  4765. {
  4766. return;
  4767. }
  4768. c->RpcThread = NewThread(CiRpcServerThread, (void *)c);
  4769. WaitThreadInit(c->RpcThread);
  4770. }
  4771. // Stop the RPC
  4772. void CiStopRpcServer(CLIENT *c)
  4773. {
  4774. // Validate arguments
  4775. if (c == NULL)
  4776. {
  4777. return;
  4778. }
  4779. Disconnect(c->RpcListener);
  4780. ReleaseSock(c->RpcListener);
  4781. WaitThread(c->RpcThread, INFINITE);
  4782. ReleaseThread(c->RpcThread);
  4783. }
  4784. // Wait for the next notification
  4785. bool CcWaitNotify(NOTIFY_CLIENT *n)
  4786. {
  4787. UCHAR c;
  4788. // Validate arguments
  4789. if (n == NULL)
  4790. {
  4791. return false;
  4792. }
  4793. // 1 character reception
  4794. if (RecvAll(n->Sock, &c, 1, false) == false)
  4795. {
  4796. // Disconnected
  4797. return false;
  4798. }
  4799. return true;
  4800. }
  4801. // Connect as a notification client
  4802. NOTIFY_CLIENT *CcConnectNotify(REMOTE_CLIENT *rc)
  4803. {
  4804. NOTIFY_CLIENT *n;
  4805. SOCK *s;
  4806. char tmp[MAX_SIZE];
  4807. bool rpc_mode = false;
  4808. UINT port;
  4809. // Validate arguments
  4810. if (rc == NULL || rc->Rpc == NULL || rc->Rpc->Sock == NULL)
  4811. {
  4812. return NULL;
  4813. }
  4814. // Connect
  4815. IPToStr(tmp, sizeof(tmp), &rc->Rpc->Sock->RemoteIP);
  4816. port = rc->Rpc->Sock->RemotePort;
  4817. s = Connect(tmp, port);
  4818. if (s == NULL)
  4819. {
  4820. return NULL;
  4821. }
  4822. rpc_mode = Endian32(rpc_mode);
  4823. if (SendAll(s, &rpc_mode, sizeof(rpc_mode), false) == false)
  4824. {
  4825. ReleaseSock(s);
  4826. return NULL;
  4827. }
  4828. n = ZeroMalloc(sizeof(NOTIFY_CLIENT));
  4829. n->Sock = s;
  4830. return n;
  4831. }
  4832. // Stop the notification client
  4833. void CcStopNotify(NOTIFY_CLIENT *n)
  4834. {
  4835. // Validate arguments
  4836. if (n == NULL)
  4837. {
  4838. return;
  4839. }
  4840. Disconnect(n->Sock);
  4841. }
  4842. // Delete the notification client
  4843. void CcDisconnectNotify(NOTIFY_CLIENT *n)
  4844. {
  4845. // Validate arguments
  4846. if (n == NULL)
  4847. {
  4848. return;
  4849. }
  4850. // Disconnect
  4851. Disconnect(n->Sock);
  4852. ReleaseSock(n->Sock);
  4853. // Memory release
  4854. Free(n);
  4855. }
  4856. // Disconnect the remote connection
  4857. void CcDisconnectRpc(REMOTE_CLIENT *rc)
  4858. {
  4859. // Validate arguments
  4860. if (rc == NULL)
  4861. {
  4862. return;
  4863. }
  4864. RpcFree(rc->Rpc);
  4865. Free(rc);
  4866. }
  4867. // Connect to the client to start the shortcut connection setting
  4868. UINT CcShortcut(UCHAR *key)
  4869. {
  4870. UINT ret;
  4871. // Validate arguments
  4872. if (key == NULL)
  4873. {
  4874. return ERR_INVALID_PARAMETER;
  4875. }
  4876. CcConnectRpcEx("localhost", NULL, NULL, NULL, key, &ret, false, 0);
  4877. return ret;
  4878. }
  4879. // Disconnect the connected shortcut connection
  4880. UINT CcShortcutDisconnect(UCHAR *key)
  4881. {
  4882. UINT ret;
  4883. // Validate arguments
  4884. if (key == NULL)
  4885. {
  4886. return ERR_INVALID_PARAMETER;
  4887. }
  4888. CcConnectRpcEx("localhost", NULL, NULL, NULL, key, &ret, true, 0);
  4889. return ret;
  4890. }
  4891. // Connect to the remote client
  4892. REMOTE_CLIENT *CcConnectRpc(char *server_name, char *password, bool *bad_pass, bool *no_remote, UINT wait_retry)
  4893. {
  4894. return CcConnectRpcEx(server_name, password, bad_pass, no_remote, NULL, NULL, false, wait_retry);
  4895. }
  4896. REMOTE_CLIENT *CcConnectRpcEx(char *server_name, char *password, bool *bad_pass, bool *no_remote, UCHAR *key, UINT *key_error_code, bool shortcut_disconnect, UINT wait_retry)
  4897. {
  4898. SOCK *s = NULL;
  4899. UINT i;
  4900. UINT retcode;
  4901. UINT rpc_mode = CLIENT_RPC_MODE_MANAGEMENT;
  4902. RPC *rpc;
  4903. REMOTE_CLIENT *ret;
  4904. UCHAR hash_password[SHA1_SIZE];
  4905. UINT port_start;
  4906. UINT64 try_started = 0;
  4907. bool ok;
  4908. UINT reg_port = 0;
  4909. UINT reg_pid = 0;
  4910. // Validate arguments
  4911. if (server_name == NULL)
  4912. {
  4913. return NULL;
  4914. }
  4915. if (password == NULL)
  4916. {
  4917. password = "";
  4918. }
  4919. if (key_error_code != NULL)
  4920. {
  4921. *key_error_code = ERR_NO_ERROR;
  4922. }
  4923. if (bad_pass != NULL)
  4924. {
  4925. *bad_pass = false;
  4926. }
  4927. if (no_remote != NULL)
  4928. {
  4929. *no_remote = false;
  4930. }
  4931. #ifdef OS_WIN32
  4932. // read the current port number from the registry of the localhost
  4933. if (StrCmpi(server_name, "localhost") == 0)
  4934. {
  4935. reg_port = MsRegReadIntEx2(REG_LOCAL_MACHINE, CLIENT_WIN32_REGKEYNAME, CLIENT_WIN32_REGVALUE_PORT, false, true);
  4936. reg_pid = MsRegReadIntEx2(REG_LOCAL_MACHINE, CLIENT_WIN32_REGKEYNAME, CLIENT_WIN32_REGVALUE_PID, false, true);
  4937. if (reg_pid != 0)
  4938. {
  4939. if (MsIsServiceRunning(GC_SVC_NAME_VPNCLIENT) == false)
  4940. {
  4941. reg_port = 0;
  4942. }
  4943. }
  4944. else
  4945. {
  4946. reg_port = 0;
  4947. }
  4948. }
  4949. #endif // OS_WIN32
  4950. port_start = CLIENT_CONFIG_PORT - 1;
  4951. if (reg_port != 0)
  4952. {
  4953. s = Connect(server_name, reg_port);
  4954. if (s != NULL)
  4955. {
  4956. goto L_TRY;
  4957. }
  4958. }
  4959. RETRY:
  4960. port_start++;
  4961. if (port_start >= (CLIENT_CONFIG_PORT + 5))
  4962. {
  4963. return NULL;
  4964. }
  4965. ok = false;
  4966. while (true)
  4967. {
  4968. for (i = port_start;i < (CLIENT_CONFIG_PORT + 5);i++)
  4969. {
  4970. if (CheckTCPPort(server_name, i))
  4971. {
  4972. ok = true;
  4973. break;
  4974. }
  4975. }
  4976. if (ok)
  4977. {
  4978. break;
  4979. }
  4980. if (wait_retry == 0)
  4981. {
  4982. break;
  4983. }
  4984. if (try_started == 0)
  4985. {
  4986. try_started = Tick64();
  4987. }
  4988. if ((try_started + (UINT64)wait_retry) <= Tick64())
  4989. {
  4990. break;
  4991. }
  4992. }
  4993. if (ok == false)
  4994. {
  4995. if (key_error_code)
  4996. {
  4997. *key_error_code = ERR_CONNECT_FAILED;
  4998. }
  4999. return NULL;
  5000. }
  5001. port_start = i;
  5002. s = Connect(server_name, i);
  5003. if (s == NULL)
  5004. {
  5005. if (key_error_code)
  5006. {
  5007. *key_error_code = ERR_CONNECT_FAILED;
  5008. }
  5009. goto RETRY;
  5010. }
  5011. L_TRY:
  5012. SetTimeout(s, 10000);
  5013. Hash(hash_password, password, StrLen(password), true);
  5014. if (key != NULL)
  5015. {
  5016. if (shortcut_disconnect == false)
  5017. {
  5018. rpc_mode = CLIENT_RPC_MODE_SHORTCUT;
  5019. }
  5020. else
  5021. {
  5022. rpc_mode = CLIENT_RPC_MODE_SHORTCUT_DISCONNECT;
  5023. }
  5024. }
  5025. rpc_mode = Endian32(rpc_mode);
  5026. SendAdd(s, &rpc_mode, sizeof(UINT));
  5027. if (key != NULL)
  5028. {
  5029. SendAdd(s, key, SHA1_SIZE);
  5030. }
  5031. else
  5032. {
  5033. SendAdd(s, hash_password, SHA1_SIZE);
  5034. }
  5035. if (SendNow(s, false) == false)
  5036. {
  5037. ReleaseSock(s);
  5038. goto RETRY;
  5039. }
  5040. if (RecvAll(s, &retcode, sizeof(UINT), false) == false)
  5041. {
  5042. ReleaseSock(s);
  5043. goto RETRY;
  5044. }
  5045. retcode = Endian32(retcode);
  5046. if (retcode >= 1024)
  5047. {
  5048. ReleaseSock(s);
  5049. goto RETRY;
  5050. }
  5051. if (key != NULL)
  5052. {
  5053. if (key_error_code)
  5054. {
  5055. *key_error_code = retcode;
  5056. }
  5057. SendAll(s, &retcode, sizeof(UINT), false);
  5058. ReleaseSock(s);
  5059. return NULL;
  5060. }
  5061. switch (retcode)
  5062. {
  5063. case 1:
  5064. if (bad_pass != NULL)
  5065. {
  5066. *bad_pass = true;
  5067. }
  5068. break;
  5069. case 2:
  5070. if (no_remote != NULL)
  5071. {
  5072. *no_remote = true;
  5073. }
  5074. break;
  5075. }
  5076. if (retcode != 0)
  5077. {
  5078. ReleaseSock(s);
  5079. return NULL;
  5080. }
  5081. SetTimeout(s, INFINITE);
  5082. rpc = StartRpcClient(s, NULL);
  5083. ReleaseSock(s);
  5084. ret = ZeroMalloc(sizeof(REMOTE_CLIENT));
  5085. ret->Rpc = rpc;
  5086. rpc->Param = ret;
  5087. if (ret != NULL)
  5088. {
  5089. RPC_CLIENT_VERSION t;
  5090. Zero(&t, sizeof(t));
  5091. CcGetClientVersion(ret, &t);
  5092. ret->OsType = t.OsType;
  5093. ret->Unix = OS_IS_UNIX(ret->OsType);
  5094. ret->Win9x = OS_IS_WINDOWS_9X(ret->OsType);
  5095. ret->IsVgcSupported = t.IsVgcSupported;
  5096. ret->ShowVgcLink = t.ShowVgcLink;
  5097. StrCpy(ret->ClientId, sizeof(ret->ClientId), t.ClientId);
  5098. }
  5099. return ret;
  5100. }
  5101. // Get a RPC_CLIENT_GET_CONNECTION_STATUS from the session
  5102. void CiGetSessionStatus(RPC_CLIENT_GET_CONNECTION_STATUS *st, SESSION *s)
  5103. {
  5104. // Validate arguments
  5105. if (st == NULL || s == NULL)
  5106. {
  5107. return;
  5108. }
  5109. Lock(s->lock);
  5110. {
  5111. // Operation flag
  5112. st->Active = true;
  5113. // Session status
  5114. st->SessionStatus = s->ClientStatus;
  5115. // Account name
  5116. UniStrCpy(st->AccountName, sizeof(st->AccountName), s->ClientOption->AccountName);
  5117. if (s->ClientStatus == CLIENT_STATUS_ESTABLISHED && s->Connection != NULL)
  5118. {
  5119. Lock(s->Connection->lock);
  5120. {
  5121. // Connected flag
  5122. st->Connected = true;
  5123. // Product name
  5124. StrCpy(st->ServerProductName, sizeof(st->ServerProductName), s->Connection->ServerStr);
  5125. // Version
  5126. st->ServerProductVer = s->Connection->ServerVer;
  5127. // Build Number
  5128. st->ServerProductBuild = s->Connection->ServerBuild;
  5129. // Server certificate
  5130. st->ServerX = CloneX(s->Connection->ServerX);
  5131. // Client certificate
  5132. st->ClientX = CloneX(s->Connection->ClientX);
  5133. // Connection completion time of this connection
  5134. st->CurrentConnectionEstablishTime = TickToTime(s->CurrentConnectionEstablishTime);
  5135. // Maximum number of the TCP connections
  5136. st->MaxTcpConnections = s->MaxConnection;
  5137. // Half-connection
  5138. st->HalfConnection = s->HalfConnection;
  5139. // VLAN
  5140. st->VLanId = s->VLanId;
  5141. // VoIP / QoS
  5142. st->QoS = s->QoS;
  5143. if (s->Connection->Protocol == CONNECTION_TCP)
  5144. {
  5145. UINT i;
  5146. // Number of current TCP connections
  5147. LockList(s->Connection->Tcp->TcpSockList);
  5148. {
  5149. st->NumTcpConnections = LIST_NUM(s->Connection->Tcp->TcpSockList);
  5150. if (st->HalfConnection)
  5151. {
  5152. for (i = 0;i < st->NumTcpConnections;i++)
  5153. {
  5154. TCPSOCK *ts = LIST_DATA(s->Connection->Tcp->TcpSockList, i);
  5155. if (ts->Direction & TCP_SERVER_TO_CLIENT)
  5156. {
  5157. st->NumTcpConnectionsDownload++;
  5158. }
  5159. else
  5160. {
  5161. st->NumTcpConnectionsUpload++;
  5162. }
  5163. }
  5164. }
  5165. }
  5166. UnlockList(s->Connection->Tcp->TcpSockList);
  5167. }
  5168. // Use of encryption
  5169. st->UseEncrypt = s->UseEncrypt;
  5170. if (st->UseEncrypt)
  5171. {
  5172. StrCpy(st->CipherName, sizeof(st->CipherName), s->Connection->CipherName);
  5173. }
  5174. // Use of compression
  5175. st->UseCompress = s->UseCompress;
  5176. // R-UDP
  5177. st->IsRUDPSession = s->IsRUDPSession;
  5178. // Physical communication protocol
  5179. StrCpy(st->UnderlayProtocol, sizeof(st->UnderlayProtocol), s->UnderlayProtocol);
  5180. // UDP acceleration function
  5181. st->IsUdpAccelerationEnabled = s->UseUdpAcceleration;
  5182. st->IsUsingUdpAcceleration = s->IsUsingUdpAcceleration;
  5183. // Session key
  5184. Copy(st->SessionKey, s->SessionKey, SHA1_SIZE);
  5185. // Policy
  5186. Copy(&st->Policy, s->Policy, sizeof(POLICY));
  5187. // Data size
  5188. if (s->ServerMode == false)
  5189. {
  5190. st->TotalSendSize = s->TotalSendSize;
  5191. st->TotalRecvSize = s->TotalRecvSize;
  5192. st->TotalRecvSizeReal = s->TotalRecvSizeReal;
  5193. st->TotalSendSizeReal = s->TotalSendSizeReal;
  5194. }
  5195. else
  5196. {
  5197. st->TotalSendSize = s->TotalRecvSize;
  5198. st->TotalRecvSize = s->TotalSendSize;
  5199. st->TotalRecvSizeReal = s->TotalSendSizeReal;
  5200. st->TotalSendSizeReal = s->TotalRecvSizeReal;
  5201. }
  5202. // Session name
  5203. StrCpy(st->SessionName, sizeof(st->SessionName), s->Name);
  5204. // Connection name
  5205. StrCpy(st->ConnectionName, sizeof(st->ConnectionName), s->Connection->Name);
  5206. // Server name
  5207. StrCpy(st->ServerName, sizeof(st->ServerName), s->Connection->ServerName);
  5208. // Port number
  5209. st->ServerPort = s->Connection->ServerPort;
  5210. // Traffic data
  5211. Lock(s->TrafficLock);
  5212. {
  5213. Copy(&st->Traffic, s->Traffic, sizeof(TRAFFIC));
  5214. }
  5215. Unlock(s->TrafficLock);
  5216. st->IsBridgeMode = s->IsBridgeMode;
  5217. st->IsMonitorMode = s->IsMonitorMode;
  5218. }
  5219. Unlock(s->Connection->lock);
  5220. }
  5221. // Connection start time
  5222. st->StartTime = TickToTime(s->CreatedTime);
  5223. // Connection completion time of the first connection
  5224. st->FirstConnectionEstablisiedTime = TickToTime(s->FirstConnectionEstablisiedTime);
  5225. // Number of connections have been established so far
  5226. st->NumConnectionsEatablished = s->NumConnectionsEatablished;
  5227. }
  5228. Unlock(s->lock);
  5229. }
  5230. // Get the connection status
  5231. bool CtGetAccountStatus(CLIENT *c, RPC_CLIENT_GET_CONNECTION_STATUS *st)
  5232. {
  5233. // Validate arguments
  5234. if (c == NULL || st == NULL)
  5235. {
  5236. return false;
  5237. }
  5238. LockList(c->AccountList);
  5239. {
  5240. ACCOUNT t, *r;
  5241. // Search for account
  5242. t.ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
  5243. UniStrCpy(t.ClientOption->AccountName, sizeof(t.ClientOption->AccountName), st->AccountName);
  5244. r = Search(c->AccountList, &t);
  5245. if (r == NULL)
  5246. {
  5247. // Specified account is not found
  5248. UnlockList(c->AccountList);
  5249. Free(t.ClientOption);
  5250. CiSetError(c, ERR_ACCOUNT_NOT_FOUND);
  5251. return false;
  5252. }
  5253. Free(t.ClientOption);
  5254. Lock(r->lock);
  5255. {
  5256. Zero(st, sizeof(RPC_CLIENT_GET_CONNECTION_STATUS));
  5257. if (r->ClientSession != NULL)
  5258. {
  5259. SESSION *s = r->ClientSession;
  5260. CiGetSessionStatus(st, s);
  5261. }
  5262. }
  5263. Unlock(r->lock);
  5264. }
  5265. UnlockList(c->AccountList);
  5266. return true;
  5267. }
  5268. // Release the connection status
  5269. void CiFreeClientGetConnectionStatus(RPC_CLIENT_GET_CONNECTION_STATUS *st)
  5270. {
  5271. // Validate arguments
  5272. if (st == NULL)
  5273. {
  5274. return;
  5275. }
  5276. if (st->ServerX != NULL)
  5277. {
  5278. FreeX(st->ServerX);
  5279. }
  5280. if (st->ClientX != NULL)
  5281. {
  5282. FreeX(st->ClientX);
  5283. }
  5284. }
  5285. // Verification procedure of the server certificate
  5286. bool CiCheckCertProc(SESSION *s, CONNECTION *c, X *server_x, bool *expired)
  5287. {
  5288. #ifdef OS_WIN32
  5289. ACCOUNT *a;
  5290. X *old_x = NULL;
  5291. UI_CHECKCERT dlg;
  5292. // Validate arguments
  5293. if (s == NULL || c == NULL || server_x == NULL)
  5294. {
  5295. return false;
  5296. }
  5297. if (expired != NULL)
  5298. {
  5299. *expired = false;
  5300. }
  5301. Zero(&dlg, sizeof(dlg));
  5302. a = s->Account;
  5303. if (a == NULL)
  5304. {
  5305. return false;
  5306. }
  5307. Lock(a->lock);
  5308. {
  5309. if (a->CheckServerCert == false)
  5310. {
  5311. // Not to validate the server certificate
  5312. Unlock(a->lock);
  5313. return true;
  5314. }
  5315. if (a->ServerCert != NULL)
  5316. {
  5317. old_x = CloneX(a->ServerCert);
  5318. }
  5319. }
  5320. Unlock(a->lock);
  5321. if (CheckXDateNow(server_x) == false)
  5322. {
  5323. // Expired
  5324. if (old_x != NULL)
  5325. {
  5326. FreeX(old_x);
  5327. }
  5328. if (expired != NULL)
  5329. {
  5330. *expired = true;
  5331. }
  5332. return false;
  5333. }
  5334. if (old_x != NULL)
  5335. {
  5336. if (CompareX(old_x, server_x))
  5337. {
  5338. // Matched exactly to the certificate that is already registered
  5339. if (old_x != NULL)
  5340. {
  5341. FreeX(old_x);
  5342. }
  5343. return true;
  5344. }
  5345. else
  5346. {
  5347. dlg.DiffWarning = true;
  5348. }
  5349. }
  5350. // Because this certificate can not be trusted, confirm to be trusted by showing a dialog box
  5351. UniStrCpy(dlg.AccountName, sizeof(dlg.AccountName), a->ClientOption->AccountName);
  5352. StrCpy(dlg.ServerName, sizeof(dlg.ServerName), a->ClientOption->Hostname);
  5353. dlg.x = server_x;
  5354. dlg.old_x = old_x;
  5355. dlg.Session = s;
  5356. AddRef(s->ref);
  5357. CncCheckCert(s, &dlg);
  5358. ReleaseSession(s);
  5359. if (old_x != NULL)
  5360. {
  5361. FreeX(old_x);
  5362. }
  5363. if (dlg.Ok && dlg.SaveServerCert)
  5364. {
  5365. // Save the server certificate and trust it from the next time
  5366. Lock(a->lock);
  5367. {
  5368. if (a->ServerCert != NULL)
  5369. {
  5370. FreeX(a->ServerCert);
  5371. }
  5372. a->ServerCert = CloneX(server_x);
  5373. }
  5374. Unlock(a->lock);
  5375. CiSaveConfigurationFile(s->Cedar->Client);
  5376. }
  5377. return dlg.Ok;
  5378. #else // OS_WIN32
  5379. ACCOUNT *a;
  5380. X *old_x = NULL;
  5381. // Validate arguments
  5382. if (s == NULL || c == NULL || server_x == NULL)
  5383. {
  5384. return false;
  5385. }
  5386. if (expired != NULL)
  5387. {
  5388. *expired = false;
  5389. }
  5390. a = s->Account;
  5391. if (a == NULL)
  5392. {
  5393. return false;
  5394. }
  5395. Lock(a->lock);
  5396. {
  5397. if (a->CheckServerCert == false)
  5398. {
  5399. // Not to validate the server certificate
  5400. Unlock(a->lock);
  5401. return true;
  5402. }
  5403. if (a->ServerCert != NULL)
  5404. {
  5405. old_x = CloneX(a->ServerCert);
  5406. }
  5407. }
  5408. Unlock(a->lock);
  5409. if (CheckXDateNow(server_x) == false)
  5410. {
  5411. // Expired
  5412. if (old_x != NULL)
  5413. {
  5414. FreeX(old_x);
  5415. }
  5416. if (expired != NULL)
  5417. {
  5418. *expired = true;
  5419. }
  5420. return false;
  5421. }
  5422. if (old_x != NULL)
  5423. {
  5424. if (CompareX(old_x, server_x))
  5425. {
  5426. // Exactly matched to the certificate that is already registered
  5427. if (old_x != NULL)
  5428. {
  5429. FreeX(old_x);
  5430. }
  5431. return true;
  5432. }
  5433. else
  5434. {
  5435. // Mismatch
  5436. if (old_x != NULL)
  5437. {
  5438. FreeX(old_x);
  5439. }
  5440. return false;
  5441. }
  5442. }
  5443. if (old_x != NULL)
  5444. {
  5445. FreeX(old_x);
  5446. }
  5447. return false;
  5448. #endif // OS_WIN32
  5449. }
  5450. // Signature procedure with a secure device
  5451. bool CiSecureSignProc(SESSION *s, CONNECTION *c, SECURE_SIGN *sign)
  5452. {
  5453. // The UI is available in Win32
  5454. return CncSecureSignDlg(sign);
  5455. }
  5456. #ifdef OS_WIN32
  5457. // Signing procedure (for Win32)
  5458. bool Win32CiSecureSign(SECURE_SIGN *sign)
  5459. {
  5460. bool ret = false;
  5461. BUF *random;
  5462. // Validate arguments
  5463. if (sign == NULL)
  5464. {
  5465. return false;
  5466. }
  5467. random = NewBuf();
  5468. WriteBuf(random, sign->Random, SHA1_SIZE);
  5469. // Batch processing
  5470. {
  5471. WINUI_SECURE_BATCH batch[] =
  5472. {
  5473. {WINUI_SECURE_READ_CERT, sign->SecurePublicCertName, true, NULL, NULL, NULL, NULL, NULL, NULL},
  5474. {WINUI_SECURE_SIGN_WITH_KEY, sign->SecurePrivateKeyName, true, random, NULL, NULL, NULL, NULL, NULL}
  5475. };
  5476. if (SecureDeviceWindow(NULL, batch, sizeof(batch) / sizeof(batch[0]),
  5477. sign->UseSecureDeviceId, sign->BitmapId) == false)
  5478. {
  5479. // Failure
  5480. if (batch[0].OutputX != 0)
  5481. {
  5482. FreeX(batch[0].OutputX);
  5483. }
  5484. ret = false;
  5485. }
  5486. else
  5487. {
  5488. // Success
  5489. ret = true;
  5490. sign->ClientCert = batch[0].OutputX;
  5491. Copy(sign->Signature, batch[1].OutputSign, 128);
  5492. }
  5493. }
  5494. FreeBuf(random);
  5495. return ret;
  5496. }
  5497. #endif // OS_WIN32
  5498. // Disconnect
  5499. bool CtDisconnect(CLIENT *c, RPC_CLIENT_CONNECT *connect, bool inner)
  5500. {
  5501. bool ret = false;
  5502. ACCOUNT t, *r;
  5503. SESSION *s = NULL;
  5504. // Validate arguments
  5505. if (c == NULL || connect == NULL)
  5506. {
  5507. return false;
  5508. }
  5509. LockList(c->AccountList);
  5510. {
  5511. // Search for account
  5512. t.ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
  5513. UniStrCpy(t.ClientOption->AccountName, sizeof(t.ClientOption->AccountName), connect->AccountName);
  5514. r = Search(c->AccountList, &t);
  5515. if (r == NULL)
  5516. {
  5517. // Specified account isn't found
  5518. UnlockList(c->AccountList);
  5519. Free(t.ClientOption);
  5520. CiSetError(c, ERR_ACCOUNT_NOT_FOUND);
  5521. return false;
  5522. }
  5523. Free(t.ClientOption);
  5524. Lock(r->lock);
  5525. {
  5526. if (r->ClientSession == NULL)
  5527. {
  5528. // Not connected
  5529. CiSetError(c, ERR_ACCOUNT_INACTIVE);
  5530. }
  5531. else
  5532. {
  5533. s = r->ClientSession;
  5534. AddRef(s->ref);
  5535. // Disconnect complete
  5536. r->ClientSession = NULL;
  5537. ret = true;
  5538. }
  5539. }
  5540. Unlock(r->lock);
  5541. }
  5542. UnlockList(c->AccountList);
  5543. if (s != NULL)
  5544. {
  5545. // Disconnect the connection (Wait until the disconnection is complete)
  5546. CLog(c, "LC_DISCONNECT", connect->AccountName);
  5547. StopSession(s);
  5548. ReleaseSession(s);
  5549. }
  5550. if (ret != false)
  5551. {
  5552. CiNotify(c);
  5553. }
  5554. return ret;
  5555. }
  5556. // Connect
  5557. bool CtConnect(CLIENT *c, RPC_CLIENT_CONNECT *connect)
  5558. {
  5559. bool ret = false;
  5560. RPC_CLIENT_ENUM_VLAN t;
  5561. // Validate arguments
  5562. if (c == NULL || connect == NULL)
  5563. {
  5564. return false;
  5565. }
  5566. Lock(c->lockForConnect);
  5567. {
  5568. Zero(&t, sizeof(t));
  5569. if (CtEnumVLan(c, &t))
  5570. {
  5571. if (t.NumItem == 0)
  5572. {
  5573. // There are no virtual LAN cards in the system
  5574. if (OS_IS_WINDOWS_NT(GetOsInfo()->OsType) || OS_IS_UNIX(GetOsInfo()->OsType))
  5575. {
  5576. // Only in Linux system or Windows NT system,
  5577. // create a new virtual LAN card which named as "VPN" automatically
  5578. RPC_CLIENT_CREATE_VLAN t;
  5579. Zero(&t, sizeof(t));
  5580. StrCpy(t.DeviceName, sizeof(t.DeviceName), "VPN");
  5581. CtCreateVLan(c, &t);
  5582. }
  5583. }
  5584. CiFreeClientEnumVLan(&t);
  5585. }
  5586. }
  5587. Unlock(c->lockForConnect);
  5588. CiNormalizeAccountVLan(c);
  5589. // Ensure successfully VPN communication by changing the irrational WCM settings in the case of Windows 8 or later
  5590. CiDisableWcmNetworkMinimize(c);
  5591. LockList(c->AccountList);
  5592. {
  5593. ACCOUNT t, *r;
  5594. bool unix_disabled = false;
  5595. // Search for account
  5596. t.ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
  5597. UniStrCpy(t.ClientOption->AccountName, sizeof(t.ClientOption->AccountName), connect->AccountName);
  5598. r = Search(c->AccountList, &t);
  5599. if (r == NULL)
  5600. {
  5601. // Specified account isn't found
  5602. UnlockList(c->AccountList);
  5603. Free(t.ClientOption);
  5604. CiSetError(c, ERR_ACCOUNT_NOT_FOUND);
  5605. return false;
  5606. }
  5607. Free(t.ClientOption);
  5608. #ifndef OS_WIN32
  5609. // Search for the virtual LAN card
  5610. LockList(c->UnixVLanList);
  5611. {
  5612. UNIX_VLAN *v, t;
  5613. Zero(&t, sizeof(t));
  5614. StrCpy(t.Name, sizeof(t.Name), r->ClientOption->DeviceName);
  5615. v = Search(c->UnixVLanList, &t);
  5616. if (v == NULL)
  5617. {
  5618. UnlockList(c->UnixVLanList);
  5619. CiSetError(c, ERR_OBJECT_NOT_FOUND);
  5620. return false;
  5621. }
  5622. unix_disabled = v->Enabled ? false : true;
  5623. }
  5624. UnlockList(c->UnixVLanList);
  5625. #endif // OS_WIN32
  5626. Lock(r->lock);
  5627. {
  5628. bool already_used = false;
  5629. UINT i;
  5630. if (r->ClientSession != NULL)
  5631. {
  5632. // Already in connecting
  5633. CiSetError(c, ERR_ACCOUNT_ACTIVE);
  5634. }
  5635. else if (r->ClientAuth->AuthType == CLIENT_AUTHTYPE_SECURE &&
  5636. client->UseSecureDeviceId == 0)
  5637. {
  5638. // Secure device is not specified
  5639. CiSetError(c, ERR_NO_SECURE_DEVICE_SPECIFIED);
  5640. }
  5641. #ifdef OS_WIN32
  5642. else if (MsIsVLanExists(VLAN_ADAPTER_NAME_TAG, r->ClientOption->DeviceName) == false &&
  5643. MsIsVLanExists(VLAN_ADAPTER_NAME_TAG_OLD, r->ClientOption->DeviceName) == false)
  5644. {
  5645. // Virtual LAN card can not be found
  5646. CiSetError(c, ERR_VLAN_FOR_ACCOUNT_NOT_FOUND);
  5647. CiNotify(c);
  5648. CiSendGlobalPulse(c);
  5649. }
  5650. else if (MsIsVLanEnabled(r->ClientOption->DeviceName) == false)
  5651. {
  5652. // The virtual LAN card is disabled
  5653. CiSetError(c, ERR_VLAN_FOR_ACCOUNT_DISABLED);
  5654. CiNotify(c);
  5655. CiSendGlobalPulse(c);
  5656. }
  5657. #else // OS_WIN32
  5658. else if (unix_disabled)
  5659. {
  5660. // The virtual LAN card is disabled
  5661. CiSetError(c, ERR_VLAN_FOR_ACCOUNT_DISABLED);
  5662. CiNotify(c);
  5663. CiSendGlobalPulse(c);
  5664. }
  5665. #endif // OS_WIN32
  5666. else
  5667. {
  5668. // Check whether the virtual LAN card is being used by a different account already
  5669. for (i = 0;i < LIST_NUM(c->AccountList);i++)
  5670. {
  5671. ACCOUNT *a = LIST_DATA(c->AccountList, i);
  5672. if (a != r)
  5673. {
  5674. if (StrCmpi(a->ClientOption->DeviceName,
  5675. r->ClientOption->DeviceName) == 0)
  5676. {
  5677. if (a->ClientSession != NULL)
  5678. {
  5679. already_used = true;
  5680. break;
  5681. }
  5682. }
  5683. }
  5684. }
  5685. if (already_used)
  5686. {
  5687. CiSetError(c, ERR_VLAN_FOR_ACCOUNT_USED);
  5688. }
  5689. else
  5690. {
  5691. // Start the connection
  5692. PACKET_ADAPTER *pa = VLanGetPacketAdapter();
  5693. if (r->ClientAuth->AuthType == CLIENT_AUTHTYPE_SECURE)
  5694. {
  5695. // Register a procedure for secure device authentication
  5696. r->ClientAuth->SecureSignProc = CiSecureSignProc;
  5697. }
  5698. else
  5699. {
  5700. r->ClientAuth->SecureSignProc = NULL;
  5701. }
  5702. if (r->CheckServerCert)
  5703. {
  5704. // Register a procedure to validate the server certificate
  5705. r->ClientAuth->CheckCertProc = CiCheckCertProc;
  5706. }
  5707. else
  5708. {
  5709. r->ClientAuth->CheckCertProc = NULL;
  5710. }
  5711. r->StatusPrinter = CiClientStatusPrinter;
  5712. r->LastConnectDateTime = SystemTime64();
  5713. CLog(c, "LC_CONNECT", connect->AccountName);
  5714. r->ClientSession = NewClientSessionEx(c->Cedar, r->ClientOption, r->ClientAuth, pa, r);
  5715. Notify(r->ClientSession, CLIENT_NOTIFY_ACCOUNT_CHANGED);
  5716. ret = true;
  5717. }
  5718. }
  5719. }
  5720. Unlock(r->lock);
  5721. }
  5722. UnlockList(c->AccountList);
  5723. CiSaveConfigurationFile(c);
  5724. return ret;
  5725. }
  5726. // Get the account information
  5727. bool CtGetAccount(CLIENT *c, RPC_CLIENT_GET_ACCOUNT *a)
  5728. {
  5729. // Validate arguments
  5730. if (c == NULL || a == NULL)
  5731. {
  5732. return false;
  5733. }
  5734. LockList(c->AccountList);
  5735. {
  5736. ACCOUNT t, *r;
  5737. // Search for account
  5738. t.ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
  5739. UniStrCpy(t.ClientOption->AccountName, sizeof(t.ClientOption->AccountName), a->AccountName);
  5740. r = Search(c->AccountList, &t);
  5741. if (r == NULL)
  5742. {
  5743. // Specified account can not be found
  5744. UnlockList(c->AccountList);
  5745. Free(t.ClientOption);
  5746. CiSetError(c, ERR_ACCOUNT_NOT_FOUND);
  5747. return false;
  5748. }
  5749. Free(t.ClientOption);
  5750. Lock(r->lock);
  5751. {
  5752. // Copy the client option
  5753. if (a->ClientOption != NULL)
  5754. {
  5755. Free(a->ClientOption);
  5756. }
  5757. a->ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
  5758. Copy(a->ClientOption, r->ClientOption, sizeof(CLIENT_OPTION));
  5759. // Copy the authentication data
  5760. if (a->ClientAuth != NULL)
  5761. {
  5762. CiFreeClientAuth(a->ClientAuth);
  5763. }
  5764. a->ClientAuth = CopyClientAuth(r->ClientAuth);
  5765. a->StartupAccount = r->StartupAccount;
  5766. a->CheckServerCert = r->CheckServerCert;
  5767. a->ServerCert = NULL;
  5768. if (r->ServerCert != NULL)
  5769. {
  5770. a->ServerCert = CloneX(r->ServerCert);
  5771. }
  5772. // Shortcut Key
  5773. Copy(a->ShortcutKey, r->ShortcutKey, SHA1_SIZE);
  5774. a->CreateDateTime = r->CreateDateTime;
  5775. a->LastConnectDateTime = r->LastConnectDateTime;
  5776. a->UpdateDateTime = r->UpdateDateTime;
  5777. }
  5778. Unlock(r->lock);
  5779. }
  5780. UnlockList(c->AccountList);
  5781. return true;
  5782. }
  5783. // Change the account name
  5784. bool CtRenameAccount(CLIENT *c, RPC_RENAME_ACCOUNT *rename, bool inner)
  5785. {
  5786. bool ret;
  5787. // Validate arguments
  5788. if (c == NULL || rename == NULL)
  5789. {
  5790. return false;
  5791. }
  5792. ret = false;
  5793. if (UniStrCmp(rename->NewName, rename->OldName) == 0)
  5794. {
  5795. // The name has not been changed
  5796. return true;
  5797. }
  5798. LockList(c->AccountList);
  5799. {
  5800. ACCOUNT t, *r, *r2;
  5801. if (UniStrLen(rename->NewName) == 0)
  5802. {
  5803. // Name is invalid
  5804. CiSetError(c, ERR_INVALID_VALUE);
  5805. UnlockList(c->AccountList);
  5806. return false;
  5807. }
  5808. // Search for old account name
  5809. t.ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
  5810. UniStrCpy(t.ClientOption->AccountName, sizeof(t.ClientOption->AccountName), rename->OldName);
  5811. r = Search(c->AccountList, &t);
  5812. if (r == NULL)
  5813. {
  5814. // Specified account can not be found
  5815. UnlockList(c->AccountList);
  5816. Free(t.ClientOption);
  5817. CiSetError(c, ERR_ACCOUNT_NOT_FOUND);
  5818. return false;
  5819. }
  5820. Free(t.ClientOption);
  5821. // Search for a new account name
  5822. t.ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
  5823. UniStrCpy(t.ClientOption->AccountName, sizeof(t.ClientOption->AccountName), rename->NewName);
  5824. r2 = Search(c->AccountList, &t);
  5825. if (r2 != NULL)
  5826. {
  5827. // Account with the specified name already exists
  5828. UnlockList(c->AccountList);
  5829. Free(t.ClientOption);
  5830. CiSetError(c, ERR_ACCOUNT_ALREADY_EXISTS);
  5831. return false;
  5832. }
  5833. Free(t.ClientOption);
  5834. Lock(r->lock);
  5835. {
  5836. // Check the operating state of the account
  5837. if (r->ClientSession != NULL)
  5838. {
  5839. // The Account is working
  5840. Unlock(r->lock);
  5841. UnlockList(c->AccountList);
  5842. CiSetError(c, ERR_ACCOUNT_ACTIVE);
  5843. return false;
  5844. }
  5845. // Update the account name
  5846. UniStrCpy(r->ClientOption->AccountName, sizeof(r->ClientOption->AccountName),
  5847. rename->NewName);
  5848. CLog(c, "LC_RENAME_ACCOUNT", rename->OldName, rename->NewName);
  5849. ret = true;
  5850. }
  5851. Unlock(r->lock);
  5852. Sort(c->AccountList);
  5853. }
  5854. UnlockList(c->AccountList);
  5855. CiSaveConfigurationFile(c);
  5856. CiNotify(c);
  5857. return ret;
  5858. }
  5859. // Set the client configuration
  5860. bool CtSetClientConfig(CLIENT *c, CLIENT_CONFIG *o)
  5861. {
  5862. KEEP *k;
  5863. // Validate arguments
  5864. if (c == NULL || o == NULL)
  5865. {
  5866. return false;
  5867. }
  5868. if (o->UseKeepConnect)
  5869. {
  5870. if (IsEmptyStr(o->KeepConnectHost) ||
  5871. o->KeepConnectPort == 0 ||
  5872. o->KeepConnectPort >= 65536)
  5873. {
  5874. CiSetError(c, ERR_INVALID_PARAMETER);
  5875. return false;
  5876. }
  5877. }
  5878. Lock(c->lock);
  5879. {
  5880. Copy(&c->Config, o, sizeof(CLIENT_CONFIG));
  5881. }
  5882. Unlock(c->lock);
  5883. // Save the settings
  5884. CiSaveConfigurationFile(c);
  5885. // Apply the Keep Connect
  5886. k = c->Keep;
  5887. Lock(k->lock);
  5888. {
  5889. if (o->UseKeepConnect)
  5890. {
  5891. StrCpy(k->ServerName, sizeof(k->ServerName), c->Config.KeepConnectHost);
  5892. k->ServerPort = c->Config.KeepConnectPort;
  5893. k->Interval = c->Config.KeepConnectInterval * 1000;
  5894. k->UdpMode = (c->Config.KeepConnectProtocol == CONNECTION_UDP) ? true : false;
  5895. k->Enable = true;
  5896. }
  5897. else
  5898. {
  5899. k->Enable = false;
  5900. }
  5901. }
  5902. Unlock(k->lock);
  5903. return true;
  5904. }
  5905. // Get the network client configuration
  5906. bool CtGetClientConfig(CLIENT *c, CLIENT_CONFIG *o)
  5907. {
  5908. // Validate arguments
  5909. if (c == NULL || o == NULL)
  5910. {
  5911. return false;
  5912. }
  5913. Lock(c->lock);
  5914. {
  5915. Copy(o, &c->Config, sizeof(CLIENT_CONFIG));
  5916. }
  5917. Unlock(c->lock);
  5918. return true;
  5919. }
  5920. // Unset the startup attribute of the account
  5921. bool CtRemoveStartupAccount(CLIENT *c, RPC_CLIENT_DELETE_ACCOUNT *a)
  5922. {
  5923. bool ret;
  5924. // Validate arguments
  5925. if (c == NULL || a == NULL)
  5926. {
  5927. return false;
  5928. }
  5929. ret = false;
  5930. LockList(c->AccountList);
  5931. {
  5932. ACCOUNT t, *r;
  5933. // Search for an Account
  5934. t.ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
  5935. UniStrCpy(t.ClientOption->AccountName, sizeof(t.ClientOption->AccountName), a->AccountName);
  5936. r = Search(c->AccountList, &t);
  5937. if (r == NULL)
  5938. {
  5939. // Specified account can not be found
  5940. UnlockList(c->AccountList);
  5941. Free(t.ClientOption);
  5942. CiSetError(c, ERR_ACCOUNT_NOT_FOUND);
  5943. return false;
  5944. }
  5945. Free(t.ClientOption);
  5946. Lock(r->lock);
  5947. {
  5948. // Unset the startup account
  5949. ret = true;
  5950. r->StartupAccount = false;
  5951. }
  5952. Unlock(r->lock);
  5953. }
  5954. UnlockList(c->AccountList);
  5955. if (ret)
  5956. {
  5957. CiSaveConfigurationFile(c);
  5958. CiNotify(c);
  5959. }
  5960. return ret;
  5961. }
  5962. // Set the account as a start-up account
  5963. bool CtSetStartupAccount(CLIENT *c, RPC_CLIENT_DELETE_ACCOUNT *a, bool inner)
  5964. {
  5965. bool ret;
  5966. // Validate arguments
  5967. if (c == NULL || a == NULL)
  5968. {
  5969. return false;
  5970. }
  5971. ret = false;
  5972. LockList(c->AccountList);
  5973. {
  5974. ACCOUNT t, *r;
  5975. // Search for an account
  5976. t.ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
  5977. UniStrCpy(t.ClientOption->AccountName, sizeof(t.ClientOption->AccountName), a->AccountName);
  5978. r = Search(c->AccountList, &t);
  5979. if (r == NULL)
  5980. {
  5981. // Specified account can not be found
  5982. UnlockList(c->AccountList);
  5983. Free(t.ClientOption);
  5984. CiSetError(c, ERR_ACCOUNT_NOT_FOUND);
  5985. return false;
  5986. }
  5987. Free(t.ClientOption);
  5988. Lock(r->lock);
  5989. {
  5990. // Set to a start-up account
  5991. ret = true;
  5992. r->StartupAccount = true;
  5993. }
  5994. Unlock(r->lock);
  5995. }
  5996. UnlockList(c->AccountList);
  5997. if (ret)
  5998. {
  5999. CiSaveConfigurationFile(c);
  6000. CiNotify(c);
  6001. }
  6002. return ret;
  6003. }
  6004. // Delete the account
  6005. bool CtDeleteAccount(CLIENT *c, RPC_CLIENT_DELETE_ACCOUNT *a, bool inner)
  6006. {
  6007. bool ret;
  6008. // Validate arguments
  6009. if (c == NULL || a == NULL)
  6010. {
  6011. return false;
  6012. }
  6013. ret = false;
  6014. if (c->Halt)
  6015. {
  6016. // Don't allow the removal of the account in the process of stopping
  6017. CiSetError(c, ERR_INTERNAL_ERROR);
  6018. return false;
  6019. }
  6020. LockList(c->AccountList);
  6021. {
  6022. ACCOUNT t, *r;
  6023. // Search for an Account
  6024. t.ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
  6025. UniStrCpy(t.ClientOption->AccountName, sizeof(t.ClientOption->AccountName), a->AccountName);
  6026. r = Search(c->AccountList, &t);
  6027. if (r == NULL)
  6028. {
  6029. // Specified account can not be found
  6030. UnlockList(c->AccountList);
  6031. Free(t.ClientOption);
  6032. CiSetError(c, ERR_ACCOUNT_NOT_FOUND);
  6033. return false;
  6034. }
  6035. Free(t.ClientOption);
  6036. Lock(r->lock);
  6037. {
  6038. // Check the operating state of the account
  6039. if (r->ClientSession != NULL)
  6040. {
  6041. // The account is active
  6042. Unlock(r->lock);
  6043. UnlockList(c->AccountList);
  6044. CiSetError(c, ERR_ACCOUNT_ACTIVE);
  6045. return false;
  6046. }
  6047. // Remove this account from the list
  6048. Delete(c->AccountList, r);
  6049. }
  6050. Unlock(r->lock);
  6051. // Free the memory of this account
  6052. CiFreeAccount(r);
  6053. CLog(c, "LC_DELETE_ACCOUNT", a->AccountName);
  6054. ret = true;
  6055. }
  6056. UnlockList(c->AccountList);
  6057. if (ret)
  6058. {
  6059. CiSaveConfigurationFile(c);
  6060. CiNotify(c);
  6061. }
  6062. return ret;
  6063. }
  6064. // Enumeration of accounts
  6065. bool CtEnumAccount(CLIENT *c, RPC_CLIENT_ENUM_ACCOUNT *e)
  6066. {
  6067. // Validate arguments
  6068. if (c == NULL || e == NULL)
  6069. {
  6070. return false;
  6071. }
  6072. LockList(c->AccountList);
  6073. {
  6074. UINT i;
  6075. // Number of accounts
  6076. e->NumItem = LIST_NUM(c->AccountList);
  6077. e->Items = ZeroMalloc(sizeof(RPC_CLIENT_ENUM_ACCOUNT_ITEM *) * e->NumItem);
  6078. for (i = 0;i < e->NumItem;i++)
  6079. {
  6080. ACCOUNT *a = LIST_DATA(c->AccountList, i);
  6081. RPC_CLIENT_ENUM_ACCOUNT_ITEM *item = ZeroMalloc(sizeof(RPC_CLIENT_ENUM_ACCOUNT_ITEM));
  6082. e->Items[i] = item;
  6083. // Account name
  6084. UniStrCpy(item->AccountName, sizeof(item->AccountName), a->ClientOption->AccountName);
  6085. // User name
  6086. StrCpy(item->UserName, sizeof(item->UserName), a->ClientAuth->Username);
  6087. // Server name
  6088. StrCpy(item->ServerName, sizeof(item->ServerName), a->ClientOption->Hostname);
  6089. // Proxy type
  6090. item->ProxyType = a->ClientOption->ProxyType;
  6091. // Device name
  6092. StrCpy(item->DeviceName, sizeof(item->DeviceName), a->ClientOption->DeviceName);
  6093. // Proxy information
  6094. if (item->ProxyType != PROXY_DIRECT)
  6095. {
  6096. StrCpy(item->ProxyName, sizeof(item->ProxyName), a->ClientOption->ProxyName);
  6097. }
  6098. // Startup
  6099. item->StartupAccount = a->StartupAccount;
  6100. // Active flag
  6101. item->Active = (a->ClientSession == NULL ? false : true);
  6102. // Connection flag
  6103. item->Connected = (item->Active == false) ? false : a->ClientSession->ConnectSucceed;
  6104. // Port number
  6105. item->Port = a->ClientOption->Port;
  6106. // Virtual HUB name
  6107. StrCpy(item->HubName, sizeof(item->HubName), a->ClientOption->HubName);
  6108. item->CreateDateTime = a->CreateDateTime;
  6109. item->LastConnectDateTime = a->LastConnectDateTime;
  6110. item->UpdateDateTime = a->UpdateDateTime;
  6111. }
  6112. }
  6113. UnlockList(c->AccountList);
  6114. return true;
  6115. }
  6116. // Configure the account
  6117. bool CtSetAccount(CLIENT *c, RPC_CLIENT_CREATE_ACCOUNT *a, bool inner)
  6118. {
  6119. // Validate arguments
  6120. if (c == NULL || a == NULL)
  6121. {
  6122. return false;
  6123. }
  6124. // Check whether an account already exists
  6125. LockList(c->AccountList);
  6126. {
  6127. ACCOUNT t, *ret;
  6128. t.ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
  6129. UniStrCpy(t.ClientOption->AccountName, sizeof(t.ClientOption->AccountName),
  6130. a->ClientOption->AccountName);
  6131. ret = Search(c->AccountList, &t);
  6132. if (ret == NULL)
  6133. {
  6134. // Not exist
  6135. UnlockList(c->AccountList);
  6136. Free(t.ClientOption);
  6137. CiSetError(c, ERR_ACCOUNT_NOT_FOUND);
  6138. return false;
  6139. }
  6140. Free(t.ClientOption);
  6141. if (a->ClientAuth->AuthType == CLIENT_AUTHTYPE_CERT)
  6142. {
  6143. if (a->ClientAuth->ClientX == NULL ||
  6144. a->ClientAuth->ClientX->is_compatible_bit == false ||
  6145. a->ClientAuth->ClientK == NULL)
  6146. {
  6147. // Client certificate is invalid
  6148. UnlockList(c->AccountList);
  6149. CiSetError(c, ERR_NOT_RSA_1024);
  6150. return false;
  6151. }
  6152. }
  6153. if (a->ServerCert != NULL && a->ServerCert->is_compatible_bit == false)
  6154. {
  6155. // Server certificate is invalid
  6156. UnlockList(c->AccountList);
  6157. CiSetError(c, ERR_NOT_RSA_1024);
  6158. return false;
  6159. }
  6160. Lock(ret->lock);
  6161. {
  6162. #if 0
  6163. // Rewriting of the configuration is done even account running in the current version
  6164. // (New setting isn't applied until connecting next time)
  6165. if (ret->ClientSession != NULL)
  6166. {
  6167. // The account is operating
  6168. Unlock(ret->lock);
  6169. UnlockList(c->AccountList);
  6170. CiSetError(c, ERR_ACCOUNT_ACTIVE);
  6171. return false;
  6172. }
  6173. #endif
  6174. // Delete the client authentication data
  6175. CiFreeClientAuth(ret->ClientAuth);
  6176. // Copy the client authentication data
  6177. ret->ClientAuth = CopyClientAuth(a->ClientAuth);
  6178. // Delete the client option
  6179. Free(ret->ClientOption);
  6180. // Copy the client option
  6181. ret->ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
  6182. Copy(ret->ClientOption, a->ClientOption, sizeof(CLIENT_OPTION));
  6183. ret->StartupAccount = a->StartupAccount;
  6184. ret->CheckServerCert = a->CheckServerCert;
  6185. if (a->ServerCert != NULL)
  6186. {
  6187. if (ret->ServerCert != NULL)
  6188. {
  6189. FreeX(ret->ServerCert);
  6190. }
  6191. ret->ServerCert = CloneX(a->ServerCert);
  6192. }
  6193. else
  6194. {
  6195. if (ret->ServerCert != NULL)
  6196. {
  6197. FreeX(ret->ServerCert);
  6198. }
  6199. ret->ServerCert = false;
  6200. }
  6201. ret->UpdateDateTime = SystemTime64();
  6202. }
  6203. Unlock(ret->lock);
  6204. }
  6205. UnlockList(c->AccountList);
  6206. CiSaveConfigurationFile(c);
  6207. CiNotify(c);
  6208. return true;
  6209. }
  6210. // Create an account
  6211. bool CtCreateAccount(CLIENT *c, RPC_CLIENT_CREATE_ACCOUNT *a, bool inner)
  6212. {
  6213. // Validate arguments
  6214. if (c == NULL || a == NULL)
  6215. {
  6216. return false;
  6217. }
  6218. // Check whether an account already exists
  6219. LockList(c->AccountList);
  6220. {
  6221. ACCOUNT t, *ret, *new_account;
  6222. t.ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
  6223. UniStrCpy(t.ClientOption->AccountName, sizeof(t.ClientOption->AccountName),
  6224. a->ClientOption->AccountName);
  6225. ret = Search(c->AccountList, &t);
  6226. if (ret != NULL)
  6227. {
  6228. // Already exist
  6229. UnlockList(c->AccountList);
  6230. Free(t.ClientOption);
  6231. CiSetError(c, ERR_ACCOUNT_ALREADY_EXISTS);
  6232. return false;
  6233. }
  6234. Free(t.ClientOption);
  6235. if (UniStrLen(a->ClientOption->AccountName) == 0)
  6236. {
  6237. // The name is invalid
  6238. UnlockList(c->AccountList);
  6239. CiSetError(c, ERR_INVALID_VALUE);
  6240. return false;
  6241. }
  6242. if (a->ClientAuth->AuthType == CLIENT_AUTHTYPE_CERT)
  6243. {
  6244. if (a->ClientAuth->ClientX == NULL ||
  6245. a->ClientAuth->ClientX->is_compatible_bit == false ||
  6246. a->ClientAuth->ClientK == NULL)
  6247. {
  6248. // The client certificate is invalid
  6249. UnlockList(c->AccountList);
  6250. CiSetError(c, ERR_NOT_RSA_1024);
  6251. return false;
  6252. }
  6253. }
  6254. if (a->ServerCert != NULL && a->ServerCert->is_compatible_bit == false)
  6255. {
  6256. // The server certificate is invalid
  6257. UnlockList(c->AccountList);
  6258. CiSetError(c, ERR_NOT_RSA_1024);
  6259. return false;
  6260. }
  6261. // Add a new account
  6262. new_account = ZeroMalloc(sizeof(ACCOUNT));
  6263. new_account->lock = NewLock();
  6264. // Copy the client authentication data
  6265. new_account->ClientAuth = CopyClientAuth(a->ClientAuth);
  6266. // Copy the client option
  6267. new_account->ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
  6268. Copy(new_account->ClientOption, a->ClientOption, sizeof(CLIENT_OPTION));
  6269. new_account->StartupAccount = a->StartupAccount;
  6270. new_account->CheckServerCert = a->CheckServerCert;
  6271. if (a->ServerCert != NULL)
  6272. {
  6273. new_account->ServerCert = CloneX(a->ServerCert);
  6274. }
  6275. // Shortcut Key
  6276. if (IsZero(a->ShortcutKey, SHA1_SIZE))
  6277. {
  6278. Rand(new_account->ShortcutKey, SHA1_SIZE);
  6279. }
  6280. else
  6281. {
  6282. Copy(new_account->ShortcutKey, a->ShortcutKey, SHA1_SIZE);
  6283. }
  6284. new_account->CreateDateTime = new_account->UpdateDateTime = SystemTime64();
  6285. // Insert into the list
  6286. Insert(c->AccountList, new_account);
  6287. CLog(c, "LC_NEW_ACCOUNT", a->ClientOption->AccountName);
  6288. }
  6289. UnlockList(c->AccountList);
  6290. CiNormalizeAccountVLan(c);
  6291. CiSaveConfigurationFile(c);
  6292. CiNotify(c);
  6293. return true;
  6294. }
  6295. // Release the account acquisition structure
  6296. void CiFreeClientGetAccount(RPC_CLIENT_GET_ACCOUNT *a)
  6297. {
  6298. // Validate arguments
  6299. if (a == NULL)
  6300. {
  6301. return;
  6302. }
  6303. // Release the account information
  6304. if (a->ServerCert != NULL)
  6305. {
  6306. FreeX(a->ServerCert);
  6307. }
  6308. CiFreeClientAuth(a->ClientAuth);
  6309. Free(a->ClientOption);
  6310. }
  6311. // Release the account creation structure
  6312. void CiFreeClientCreateAccount(RPC_CLIENT_CREATE_ACCOUNT *a)
  6313. {
  6314. // Validate arguments
  6315. if (a == NULL)
  6316. {
  6317. return;
  6318. }
  6319. // Release the account information
  6320. if (a->ServerCert != NULL)
  6321. {
  6322. FreeX(a->ServerCert);
  6323. }
  6324. CiFreeClientAuth(a->ClientAuth);
  6325. Free(a->ClientOption);
  6326. }
  6327. // Stop the virtual LAN card
  6328. bool CtDisableVLan(CLIENT *c, RPC_CLIENT_CREATE_VLAN *vlan)
  6329. {
  6330. UINT i;
  6331. bool used;
  6332. // Validate arguments
  6333. if (c == NULL || vlan == NULL)
  6334. {
  6335. return false;
  6336. }
  6337. #ifndef OS_WIN32
  6338. #ifdef NO_VLAN
  6339. if (GetOsInfo()->OsType == OSTYPE_MACOS_X)
  6340. {
  6341. // Can not be added or removed the virtual LAN card in MacOS X
  6342. CiSetError(c, ERR_NOT_SUPPORTED);
  6343. return false;
  6344. }
  6345. #endif // NO_VLAN
  6346. // Check whether the virtual LAN card with the specified name is not
  6347. // being used by one or more accounts
  6348. used = false;
  6349. LockList(c->AccountList);
  6350. {
  6351. for (i = 0;i < LIST_NUM(c->AccountList);i++)
  6352. {
  6353. ACCOUNT *a = LIST_DATA(c->AccountList, i);
  6354. if (StrCmpi(a->ClientOption->DeviceName, vlan->DeviceName) == 0)
  6355. {
  6356. Lock(a->lock);
  6357. {
  6358. if (a->ClientSession != NULL)
  6359. {
  6360. used = true;
  6361. }
  6362. }
  6363. Unlock(a->lock);
  6364. }
  6365. }
  6366. }
  6367. UnlockList(c->AccountList);
  6368. // Search for the virtual LAN card
  6369. LockList(c->UnixVLanList);
  6370. {
  6371. UNIX_VLAN *v, t;
  6372. Zero(&t, sizeof(t));
  6373. StrCpy(t.Name, sizeof(t.Name), vlan->DeviceName);
  6374. v = Search(c->UnixVLanList, &t);
  6375. if (v == NULL)
  6376. {
  6377. UnlockList(c->UnixVLanList);
  6378. CiSetError(c, ERR_OBJECT_NOT_FOUND);
  6379. return false;
  6380. }
  6381. // Stop
  6382. v->Enabled = false;
  6383. }
  6384. UnlockList(c->UnixVLanList);
  6385. CiSaveConfigurationFile(c);
  6386. CiNotify(c);
  6387. CiSendGlobalPulse(c);
  6388. return true;
  6389. #else // OS_WIN32
  6390. // Check whether the virtual LAN card with the specified name is not
  6391. // being used by one or more accounts
  6392. used = false;
  6393. LockList(c->AccountList);
  6394. {
  6395. for (i = 0;i < LIST_NUM(c->AccountList);i++)
  6396. {
  6397. ACCOUNT *a = LIST_DATA(c->AccountList, i);
  6398. if (StrCmpi(a->ClientOption->DeviceName, vlan->DeviceName) == 0)
  6399. {
  6400. Lock(a->lock);
  6401. {
  6402. if (a->ClientSession != NULL)
  6403. {
  6404. used = true;
  6405. }
  6406. }
  6407. Unlock(a->lock);
  6408. }
  6409. }
  6410. }
  6411. UnlockList(c->AccountList);
  6412. #if 0
  6413. if (used)
  6414. {
  6415. // In using
  6416. CiSetError(c, ERR_VLAN_IS_USED);
  6417. return false;
  6418. }
  6419. #endif
  6420. // Check whether the virtual LAN card are present
  6421. if (MsIsVLanExists(VLAN_ADAPTER_NAME_TAG, vlan->DeviceName) == false &&
  6422. MsIsVLanExists(VLAN_ADAPTER_NAME_TAG_OLD, vlan->DeviceName) == false)
  6423. {
  6424. CiSetError(c, ERR_OBJECT_NOT_FOUND);
  6425. CiNotify(c);
  6426. CiSendGlobalPulse(c);
  6427. return false;
  6428. }
  6429. if (MsIs64BitWindows() && Is32() && MsIsAdmin())
  6430. {
  6431. // Execute the driver_installer to process since this Windows is 64 bit
  6432. // but this code is 32 bit
  6433. char tmp[MAX_SIZE];
  6434. Format(tmp, sizeof(tmp), "disablevlan %s", vlan->DeviceName);
  6435. if (MsExecDriverInstaller(tmp) == false)
  6436. {
  6437. CiSetError(c, ERR_VLAN_INSTALL_ERROR);
  6438. CiNotify(c);
  6439. CiSendGlobalPulse(c);
  6440. return false;
  6441. }
  6442. }
  6443. else
  6444. {
  6445. // Stop the virtual LAN card
  6446. if (MsDisableVLan(vlan->DeviceName) == false)
  6447. {
  6448. CiSetError(c, ERR_VLAN_INSTALL_ERROR);
  6449. CiNotify(c);
  6450. CiSendGlobalPulse(c);
  6451. return false;
  6452. }
  6453. }
  6454. CiNotify(c);
  6455. CiSendGlobalPulse(c);
  6456. return true;
  6457. #endif // OS_WIN32
  6458. }
  6459. // Start the virtual LAN card
  6460. bool CtEnableVLan(CLIENT *c, RPC_CLIENT_CREATE_VLAN *vlan)
  6461. {
  6462. // Validate arguments
  6463. if (c == NULL || vlan == NULL)
  6464. {
  6465. return false;
  6466. }
  6467. #ifndef OS_WIN32
  6468. #ifdef NO_VLAN
  6469. if (GetOsInfo()->OsType == OSTYPE_MACOS_X)
  6470. {
  6471. // Can not be added or removed the virtual LAN card in MacOS X
  6472. CiSetError(c, ERR_NOT_SUPPORTED);
  6473. return false;
  6474. }
  6475. #endif // NO_VLAN
  6476. // Search the virtual LAN card
  6477. LockList(c->UnixVLanList);
  6478. {
  6479. UNIX_VLAN *v, t;
  6480. Zero(&t, sizeof(t));
  6481. StrCpy(t.Name, sizeof(t.Name), vlan->DeviceName);
  6482. v = Search(c->UnixVLanList, &t);
  6483. if (v == NULL)
  6484. {
  6485. UnlockList(c->UnixVLanList);
  6486. CiSetError(c, ERR_OBJECT_NOT_FOUND);
  6487. return false;
  6488. }
  6489. // Enable
  6490. v->Enabled = true;
  6491. }
  6492. UnlockList(c->UnixVLanList);
  6493. CiSaveConfigurationFile(c);
  6494. CiNotify(c);
  6495. CiSendGlobalPulse(c);
  6496. return true;
  6497. #else // OS_WIN32
  6498. // Check whether the virtual LAN card are present
  6499. if (MsIsVLanExists(VLAN_ADAPTER_NAME_TAG, vlan->DeviceName) == false &&
  6500. MsIsVLanExists(VLAN_ADAPTER_NAME_TAG_OLD, vlan->DeviceName) == false)
  6501. {
  6502. CiSetError(c, ERR_OBJECT_NOT_FOUND);
  6503. CiNotify(c);
  6504. CiSendGlobalPulse(c);
  6505. return false;
  6506. }
  6507. if (MsIs64BitWindows() && Is32() && MsIsAdmin())
  6508. {
  6509. // Execute the driver_installer to process since this Windows is 64 bit
  6510. // but this code is 32 bit
  6511. char tmp[MAX_SIZE];
  6512. Format(tmp, sizeof(tmp), "enablevlan %s", vlan->DeviceName);
  6513. if (MsExecDriverInstaller(tmp) == false)
  6514. {
  6515. CiSetError(c, ERR_VLAN_INSTALL_ERROR);
  6516. CiNotify(c);
  6517. CiSendGlobalPulse(c);
  6518. return false;
  6519. }
  6520. }
  6521. else
  6522. {
  6523. // Start the virtual LAN card
  6524. if (MsEnableVLan(vlan->DeviceName) == false)
  6525. {
  6526. CiSetError(c, ERR_VLAN_INSTALL_ERROR);
  6527. CiNotify(c);
  6528. CiSendGlobalPulse(c);
  6529. return false;
  6530. }
  6531. }
  6532. CiNotify(c);
  6533. CiSendGlobalPulse(c);
  6534. return true;
  6535. #endif // OS_WIN32
  6536. }
  6537. // Delete the virtual LAN card
  6538. bool CtDeleteVLan(CLIENT *c, RPC_CLIENT_CREATE_VLAN *d)
  6539. {
  6540. UINT i;
  6541. bool used;
  6542. // Validate arguments
  6543. if (c == NULL || d == NULL)
  6544. {
  6545. return false;
  6546. }
  6547. #ifndef OS_WIN32
  6548. #ifdef NO_VLAN
  6549. if (GetOsInfo()->OsType == OSTYPE_MACOS_X)
  6550. {
  6551. // Can not be added or removed the virtual LAN card in MacOS X
  6552. CiSetError(c, ERR_NOT_SUPPORTED);
  6553. return false;
  6554. }
  6555. #endif // NO_VLAN
  6556. // Check whether the virtual LAN card with the specified name is not
  6557. // being used by one or more accounts
  6558. used = false;
  6559. LockList(c->AccountList);
  6560. {
  6561. for (i = 0;i < LIST_NUM(c->AccountList);i++)
  6562. {
  6563. ACCOUNT *a = LIST_DATA(c->AccountList, i);
  6564. if (StrCmpi(a->ClientOption->DeviceName, d->DeviceName) == 0)
  6565. {
  6566. used = true;
  6567. }
  6568. }
  6569. }
  6570. UnlockList(c->AccountList);
  6571. #if 0
  6572. if (used)
  6573. {
  6574. // In using
  6575. CiSetError(c, ERR_VLAN_IS_USED);
  6576. return false;
  6577. }
  6578. #endif
  6579. // Search for the virtual LAN card
  6580. LockList(c->UnixVLanList);
  6581. {
  6582. UNIX_VLAN *v, t;
  6583. Zero(&t, sizeof(t));
  6584. StrCpy(t.Name, sizeof(t.Name), d->DeviceName);
  6585. v = Search(c->UnixVLanList, &t);
  6586. if (v == NULL)
  6587. {
  6588. UnlockList(c->UnixVLanList);
  6589. CiSetError(c, ERR_OBJECT_NOT_FOUND);
  6590. return false;
  6591. }
  6592. // Remove
  6593. if (Delete(c->UnixVLanList, v))
  6594. {
  6595. Free(v);
  6596. }
  6597. CLog(c, "LC_DELETE_VLAN", d->DeviceName);
  6598. UnixVLanDelete(d->DeviceName);
  6599. }
  6600. UnlockList(c->UnixVLanList);
  6601. CiNormalizeAccountVLan(c);
  6602. CiSaveConfigurationFile(c);
  6603. CiNotify(c);
  6604. CiSendGlobalPulse(c);
  6605. return true;
  6606. #else // OS_WIN32
  6607. if (MsIsNt() == false)
  6608. {
  6609. // Not available in Win9x
  6610. CiSetError(c, ERR_NOT_SUPPORTED);
  6611. return false;
  6612. }
  6613. // Check whether the virtual LAN card are present
  6614. if (MsIsVLanExists(VLAN_ADAPTER_NAME_TAG, d->DeviceName) == false &&
  6615. MsIsVLanExists(VLAN_ADAPTER_NAME_TAG_OLD, d->DeviceName) == false)
  6616. {
  6617. CiSetError(c, ERR_OBJECT_NOT_FOUND);
  6618. return false;
  6619. }
  6620. // Check whether the virtual LAN card with the specified name is not
  6621. // being used by one or more accounts
  6622. used = false;
  6623. LockList(c->AccountList);
  6624. {
  6625. for (i = 0;i < LIST_NUM(c->AccountList);i++)
  6626. {
  6627. ACCOUNT *a = LIST_DATA(c->AccountList, i);
  6628. if (StrCmpi(a->ClientOption->DeviceName, d->DeviceName) == 0)
  6629. {
  6630. used = true;
  6631. }
  6632. }
  6633. }
  6634. UnlockList(c->AccountList);
  6635. #if 0
  6636. if (used)
  6637. {
  6638. // In using
  6639. CiSetError(c, ERR_VLAN_IS_USED);
  6640. return false;
  6641. }
  6642. #endif
  6643. if (MsIs64BitWindows() && Is32() && MsIsAdmin())
  6644. {
  6645. // Execute the driver_installer to process since this Windows is 64 bit
  6646. // but this code is 32 bit
  6647. char tmp[MAX_SIZE];
  6648. Format(tmp, sizeof(tmp), "uninstvlan %s", d->DeviceName);
  6649. if (MsExecDriverInstaller(tmp) == false)
  6650. {
  6651. CiSetError(c, ERR_VLAN_INSTALL_ERROR);
  6652. return false;
  6653. }
  6654. }
  6655. else
  6656. {
  6657. // Delete the virtual LAN card directly
  6658. if (MsUninstallVLan(d->DeviceName) == false)
  6659. {
  6660. CiSetError(c, ERR_VLAN_INSTALL_ERROR);
  6661. CiNotify(c);
  6662. CiSendGlobalPulse(c);
  6663. return false;
  6664. }
  6665. }
  6666. CLog(c, "LC_DELETE_VLAN", d->DeviceName);
  6667. CiNormalizeAccountVLan(c);
  6668. CiNotify(c);
  6669. CiSendGlobalPulse(c);
  6670. return true;
  6671. #endif // OS_WIN32
  6672. }
  6673. // Get the name of the first VLAN
  6674. char *CiGetFirstVLan(CLIENT *c)
  6675. {
  6676. char *ret = NULL;
  6677. RPC_CLIENT_ENUM_VLAN t;
  6678. // Validate arguments
  6679. if (c == NULL)
  6680. {
  6681. return NULL;
  6682. }
  6683. Zero(&t, sizeof(t));
  6684. if (CtEnumVLan(c, &t) == false)
  6685. {
  6686. return NULL;
  6687. }
  6688. if (t.NumItem >= 1)
  6689. {
  6690. UINT i;
  6691. char *tmp = t.Items[0]->DeviceName;
  6692. for (i = 0;i < t.NumItem;i++)
  6693. {
  6694. if (t.Items[i]->Enabled)
  6695. {
  6696. tmp = t.Items[i]->DeviceName;
  6697. }
  6698. }
  6699. ret = CopyStr(tmp);
  6700. }
  6701. CiFreeClientEnumVLan(&t);
  6702. return ret;
  6703. }
  6704. // Enumerate virtual LAN cards
  6705. bool CtEnumVLan(CLIENT *c, RPC_CLIENT_ENUM_VLAN *e)
  6706. {
  6707. UINT i;
  6708. TOKEN_LIST *t;
  6709. // Validate arguments
  6710. if (c == NULL || e == NULL)
  6711. {
  6712. return false;
  6713. }
  6714. #ifndef OS_WIN32
  6715. LockList(c->UnixVLanList);
  6716. {
  6717. e->NumItem = LIST_NUM(c->UnixVLanList);
  6718. e->Items = ZeroMalloc(sizeof(RPC_CLIENT_ENUM_VLAN_ITEM *) * e->NumItem);
  6719. for (i = 0;i < e->NumItem;i++)
  6720. {
  6721. RPC_CLIENT_ENUM_VLAN_ITEM *item;
  6722. UNIX_VLAN *v;
  6723. v = LIST_DATA(c->UnixVLanList, i);
  6724. e->Items[i] = ZeroMalloc(sizeof(RPC_CLIENT_ENUM_VLAN_ITEM));
  6725. item = e->Items[i];
  6726. item->Enabled = v->Enabled;
  6727. BinToStr(item->MacAddress, sizeof(item->MacAddress), v->MacAddress, 6);
  6728. StrCpy(item->DeviceName, sizeof(item->DeviceName), v->Name);
  6729. StrCpy(item->Version, sizeof(item->Version), c->Cedar->VerString);
  6730. }
  6731. }
  6732. UnlockList(c->UnixVLanList);
  6733. return true;
  6734. #else // OS_WIN32
  6735. // Enumeration
  6736. t = MsEnumNetworkAdapters(VLAN_ADAPTER_NAME, VLAN_ADAPTER_NAME_OLD);
  6737. if (t == NULL)
  6738. {
  6739. // Enumeration failure
  6740. e->NumItem = 0;
  6741. e->Items = ZeroMalloc(0);
  6742. }
  6743. else
  6744. {
  6745. // Enumeration success
  6746. e->NumItem = t->NumTokens;
  6747. e->Items = ZeroMalloc(sizeof(RPC_CLIENT_ENUM_VLAN_ITEM *) * e->NumItem);
  6748. for (i = 0;i < e->NumItem;i++)
  6749. {
  6750. char *tmp;
  6751. RPC_CLIENT_ENUM_VLAN_ITEM *item;
  6752. e->Items[i] = ZeroMalloc(sizeof(RPC_CLIENT_ENUM_VLAN_ITEM));
  6753. item = e->Items[i];
  6754. StrCpy(item->DeviceName, sizeof(item->DeviceName), t->Token[i]);
  6755. item->Enabled = MsIsVLanEnabled(item->DeviceName);
  6756. tmp = MsGetMacAddress(VLAN_ADAPTER_NAME_TAG, item->DeviceName);
  6757. if (tmp == NULL)
  6758. {
  6759. tmp = MsGetMacAddress(VLAN_ADAPTER_NAME_TAG_OLD, item->DeviceName);
  6760. }
  6761. StrCpy(item->MacAddress, sizeof(item->MacAddress), tmp);
  6762. Free(tmp);
  6763. tmp = MsGetDriverVersion(VLAN_ADAPTER_NAME_TAG, item->DeviceName);
  6764. if (tmp == NULL)
  6765. {
  6766. tmp = MsGetDriverVersion(VLAN_ADAPTER_NAME_TAG_OLD, item->DeviceName);
  6767. }
  6768. StrCpy(item->Version, sizeof(item->Version), tmp);
  6769. Free(tmp);
  6770. }
  6771. FreeToken(t);
  6772. }
  6773. return true;
  6774. #endif // OS_WIN32
  6775. }
  6776. // Release the virtual LAN card enumeration
  6777. void CiFreeClientEnumVLan(RPC_CLIENT_ENUM_VLAN *e)
  6778. {
  6779. UINT i;
  6780. // Validate arguments
  6781. if (e == NULL)
  6782. {
  6783. return;
  6784. }
  6785. for (i = 0;i < e->NumItem;i++)
  6786. {
  6787. Free(e->Items[i]);
  6788. }
  6789. Free(e->Items);
  6790. }
  6791. // Set the information about the virtual LAN card
  6792. bool CtSetVLan(CLIENT *c, RPC_CLIENT_SET_VLAN *set)
  6793. {
  6794. // Validate arguments
  6795. if (c == NULL || set == NULL)
  6796. {
  6797. return false;
  6798. }
  6799. #ifndef OS_WIN32
  6800. LockList(c->UnixVLanList);
  6801. {
  6802. UNIX_VLAN t, *r;
  6803. Zero(&t, sizeof(t));
  6804. StrCpy(t.Name, sizeof(t.Name), set->DeviceName);
  6805. r = Search(c->UnixVLanList, &t);
  6806. if (r == NULL)
  6807. {
  6808. // Not exist
  6809. CiSetError(c, ERR_VLAN_ALREADY_EXISTS);
  6810. UnlockList(c->UnixVLanList);
  6811. return false;
  6812. }
  6813. StrToMac(r->MacAddress, set->MacAddress);
  6814. }
  6815. UnlockList(c->UnixVLanList);
  6816. CiSaveConfigurationFile(c);
  6817. CiNotify(c);
  6818. CiSendGlobalPulse(c);
  6819. return true;
  6820. #else // OS_WIN32
  6821. // Check whether the virtual LAN card with the specified name already exists
  6822. if (MsIsVLanExists(VLAN_ADAPTER_NAME_TAG, set->DeviceName) == false &&
  6823. MsIsVLanExists(VLAN_ADAPTER_NAME_TAG_OLD, set->DeviceName) == false)
  6824. {
  6825. // Not exist
  6826. CiSetError(c, ERR_OBJECT_NOT_FOUND);
  6827. return false;
  6828. }
  6829. // Configuring MAC address
  6830. MsSetMacAddress(VLAN_ADAPTER_NAME_TAG, set->DeviceName, set->MacAddress);
  6831. MsSetMacAddress(VLAN_ADAPTER_NAME_TAG_OLD, set->DeviceName, set->MacAddress);
  6832. CiNotify(c);
  6833. CiSendGlobalPulse(c);
  6834. return true;
  6835. #endif // OS_WIN32
  6836. }
  6837. // Get the information about the virtual LAN card
  6838. bool CtGetVLan(CLIENT *c, RPC_CLIENT_GET_VLAN *get)
  6839. {
  6840. char *tmp;
  6841. // Validate arguments
  6842. if (c == NULL || get == NULL)
  6843. {
  6844. return false;
  6845. }
  6846. #ifndef OS_WIN32
  6847. // Unsupported
  6848. CiSetError(c, ERR_NOT_SUPPORTED);
  6849. return false;
  6850. #else // OS_WIN32
  6851. // Check whether the virtual LAN card with the specified name already exists
  6852. if (MsIsVLanExists(VLAN_ADAPTER_NAME_TAG, get->DeviceName) == false &&
  6853. MsIsVLanExists(VLAN_ADAPTER_NAME_TAG_OLD, get->DeviceName) == false)
  6854. {
  6855. // Not exist
  6856. CiSetError(c, ERR_OBJECT_NOT_FOUND);
  6857. return false;
  6858. }
  6859. // Activity
  6860. get->Enabled = MsIsVLanEnabled(get->DeviceName);
  6861. // MAC address
  6862. tmp = MsGetMacAddress(VLAN_ADAPTER_NAME_TAG, get->DeviceName);
  6863. if (tmp == NULL)
  6864. {
  6865. tmp = MsGetMacAddress(VLAN_ADAPTER_NAME_TAG_OLD, get->DeviceName);
  6866. }
  6867. StrCpy(get->MacAddress, sizeof(get->MacAddress), tmp);
  6868. Free(tmp);
  6869. // Version
  6870. tmp = MsGetDriverVersion(VLAN_ADAPTER_NAME_TAG, get->DeviceName);
  6871. if (tmp == NULL)
  6872. {
  6873. tmp = MsGetDriverVersion(VLAN_ADAPTER_NAME_TAG_OLD, get->DeviceName);
  6874. }
  6875. StrCpy(get->Version, sizeof(get->Version), tmp);
  6876. Free(tmp);
  6877. // File name
  6878. tmp = MsGetDriverFileName(VLAN_ADAPTER_NAME_TAG, get->DeviceName);
  6879. if (tmp == NULL)
  6880. {
  6881. tmp = MsGetDriverFileName(VLAN_ADAPTER_NAME_TAG_OLD, get->DeviceName);
  6882. }
  6883. StrCpy(get->FileName, sizeof(get->FileName), tmp);
  6884. Free(tmp);
  6885. // GUID
  6886. tmp = MsGetNetworkAdapterGuid(VLAN_ADAPTER_NAME_TAG, get->DeviceName);
  6887. if (tmp == NULL)
  6888. {
  6889. tmp = MsGetNetworkAdapterGuid(VLAN_ADAPTER_NAME_TAG_OLD, get->DeviceName);
  6890. }
  6891. StrCpy(get->Guid, sizeof(get->Guid), tmp);
  6892. Free(tmp);
  6893. return true;
  6894. #endif // OS_WIN32
  6895. }
  6896. #ifdef OS_WIN32
  6897. // Initialize the driver version information structure
  6898. void CiInitDriverVerStruct(MS_DRIVER_VER *ver)
  6899. {
  6900. UINT cedar_ver = CEDAR_VER;
  6901. // Validate arguments
  6902. if (ver == NULL)
  6903. {
  6904. return;
  6905. }
  6906. Zero(ver, sizeof(MS_DRIVER_VER));
  6907. ver->Year = BUILD_DATE_Y;
  6908. ver->Month = BUILD_DATE_M;
  6909. ver->Day = BUILD_DATE_D;
  6910. ver->Major = cedar_ver / 100;
  6911. ver->Minor = cedar_ver % 100;
  6912. ver->Build = CEDAR_BUILD;
  6913. }
  6914. #endif // OS_WIN32
  6915. // Upgrade the virtual LAN card
  6916. bool CtUpgradeVLan(CLIENT *c, RPC_CLIENT_CREATE_VLAN *create)
  6917. {
  6918. bool use_old_name = false;
  6919. #ifdef OS_WIN32
  6920. KAKUSHI *k = NULL;
  6921. MS_DRIVER_VER ver;
  6922. #endif // OS_WIN32
  6923. // Validate arguments
  6924. if (c == NULL || create == NULL)
  6925. {
  6926. return false;
  6927. }
  6928. #ifndef OS_WIN32
  6929. // Always succeed
  6930. return true;
  6931. #else // OS_WIN32
  6932. CiInitDriverVerStruct(&ver);
  6933. if (MsIsNt() == false)
  6934. {
  6935. // Not available in Win9x
  6936. CiSetError(c, ERR_NOT_SUPPORTED);
  6937. return false;
  6938. }
  6939. // Check whether the LAN card with the specified name already exists
  6940. if (MsIsVLanExists(VLAN_ADAPTER_NAME_TAG, create->DeviceName) == false &&
  6941. MsIsVLanExists(VLAN_ADAPTER_NAME_TAG_OLD, create->DeviceName) == false)
  6942. {
  6943. // Not exist
  6944. CiSetError(c, ERR_OBJECT_NOT_FOUND);
  6945. CiNotify(c);
  6946. CiSendGlobalPulse(c);
  6947. return false;
  6948. }
  6949. if (MsIsVLanExists(VLAN_ADAPTER_NAME_TAG_OLD, create->DeviceName))
  6950. {
  6951. use_old_name = true;
  6952. }
  6953. if (MsIsVista() == false)
  6954. {
  6955. k = InitKakushi();
  6956. }
  6957. if (MsIsVista() == false)
  6958. {
  6959. // Perform the installation (other than Windows Vista)
  6960. if (MsUpgradeVLan(use_old_name ? VLAN_ADAPTER_NAME_TAG_OLD : VLAN_ADAPTER_NAME_TAG,
  6961. use_old_name ? VLAN_CONNECTION_NAME_OLD : VLAN_CONNECTION_NAME,
  6962. create->DeviceName, &ver) == false)
  6963. {
  6964. // Installation Failed
  6965. FreeKakushi(k);
  6966. CiSetError(c, ERR_VLAN_INSTALL_ERROR);
  6967. CiNotify(c);
  6968. CiSendGlobalPulse(c);
  6969. return false;
  6970. }
  6971. }
  6972. else
  6973. {
  6974. // Perform the installation (Windows Vista)
  6975. char tmp[MAX_SIZE];
  6976. Format(tmp, sizeof(tmp), "upgradevlan %s", create->DeviceName);
  6977. if (CncExecDriverInstaller(tmp) == false)
  6978. {
  6979. // Installation Failed
  6980. FreeKakushi(k);
  6981. CiSetError(c, ERR_VLAN_INSTALL_ERROR);
  6982. CiNotify(c);
  6983. CiSendGlobalPulse(c);
  6984. return false;
  6985. }
  6986. }
  6987. FreeKakushi(k);
  6988. CLog(c, "LC_UPDATE_VLAN", create->DeviceName);
  6989. CiNotify(c);
  6990. CiSendGlobalPulse(c);
  6991. return true;
  6992. #endif // OS_WIN32
  6993. }
  6994. // Create a virtual LAN card
  6995. bool CtCreateVLan(CLIENT *c, RPC_CLIENT_CREATE_VLAN *create)
  6996. {
  6997. TOKEN_LIST *t;
  6998. UINT max_len;
  6999. #ifdef OS_WIN32
  7000. KAKUSHI *k = NULL;
  7001. #endif // OS_WIN32
  7002. // Validate arguments
  7003. if (c == NULL || create == NULL)
  7004. {
  7005. return false;
  7006. }
  7007. if (SearchStrEx(create->DeviceName, " ", 0, false) != INFINITE)
  7008. {
  7009. // Spaces in the name is not allowed
  7010. CiSetError(c, ERR_INVALID_PARAMETER);
  7011. return false;
  7012. }
  7013. #ifndef OS_WIN32
  7014. // Non-Win32
  7015. #ifdef NO_VLAN
  7016. if (GetOsInfo()->OsType == OSTYPE_MACOS_X)
  7017. {
  7018. // A virtual LAN card can not be added or removed in MacOS X
  7019. CiSetError(c, ERR_NOT_SUPPORTED);
  7020. return false;
  7021. }
  7022. #endif // NO_VLAN
  7023. // Check whether the specified name is valid or not
  7024. if (IsSafeStr(create->DeviceName) == false)
  7025. {
  7026. // Name is invalid
  7027. CiSetError(c, ERR_VLAN_INVALID_NAME);
  7028. return false;
  7029. }
  7030. // Check whether the LAN card of the specified name already exists
  7031. LockList(c->UnixVLanList);
  7032. {
  7033. UNIX_VLAN t, *r;
  7034. Zero(&t, sizeof(t));
  7035. StrCpy(t.Name, sizeof(t.Name), create->DeviceName);
  7036. r = Search(c->UnixVLanList, &t);
  7037. if (r != NULL)
  7038. {
  7039. // Already exist
  7040. CiSetError(c, ERR_VLAN_ALREADY_EXISTS);
  7041. UnlockList(c->UnixVLanList);
  7042. return false;
  7043. }
  7044. // Register
  7045. r = ZeroMalloc(sizeof(UNIX_VLAN));
  7046. r->Enabled = true;
  7047. GenMacAddress(r->MacAddress);
  7048. StrCpy(r->Name, sizeof(r->Name), create->DeviceName);
  7049. // Create a Tap
  7050. if (UnixVLanCreate(r->Name, r->MacAddress) == false)
  7051. {
  7052. // Failure
  7053. Free(r);
  7054. CiSetError(c, ERR_VLAN_INSTALL_ERROR);
  7055. UnlockList(c->UnixVLanList);
  7056. return false;
  7057. }
  7058. CLog(c, "LC_CREATE_VLAN", create->DeviceName);
  7059. Add(c->UnixVLanList, r);
  7060. }
  7061. UnlockList(c->UnixVLanList);
  7062. CiNormalizeAccountVLan(c);
  7063. CiNotify(c);
  7064. CiSendGlobalPulse(c);
  7065. CiSaveConfigurationFile(c);
  7066. return true;
  7067. #else // OS_WIN32
  7068. if (OS_IS_WINDOWS_9X(GetOsInfo()->OsType))
  7069. {
  7070. // Only one LAN card is available in the Win9x
  7071. TOKEN_LIST *t;
  7072. t = MsEnumNetworkAdapters(VLAN_ADAPTER_NAME, VLAN_ADAPTER_NAME_OLD);
  7073. if (t != NULL)
  7074. {
  7075. if (t->NumTokens >= 1)
  7076. {
  7077. FreeToken(t);
  7078. CiSetError(c, ERR_NOT_SUPPORTED);
  7079. return false;
  7080. }
  7081. FreeToken(t);
  7082. }
  7083. }
  7084. // Check whether the specified name is valid or not
  7085. if (IsSafeStr(create->DeviceName) == false)
  7086. {
  7087. // Name is invalid
  7088. CiSetError(c, ERR_VLAN_INVALID_NAME);
  7089. return false;
  7090. }
  7091. max_len = MsIsNt() ? MAX_DEVICE_NAME_LEN : MAX_DEVICE_NAME_LEN_9X;
  7092. if (StrLen(create->DeviceName) > max_len)
  7093. {
  7094. // Name is too long
  7095. CiSetError(c, ERR_VLAN_INVALID_NAME);
  7096. return false;
  7097. }
  7098. // Regulation in Windows 8
  7099. if (MsIsInfCatalogRequired())
  7100. {
  7101. if (CiIsValidVLanRegulatedName(create->DeviceName) == false)
  7102. {
  7103. // Name is invalid
  7104. CiSetError(c, ERR_VLAN_INVALID_NAME);
  7105. return false;
  7106. }
  7107. }
  7108. // Check whether the LAN card with the specified name already exists
  7109. if (MsIsVLanExists(VLAN_ADAPTER_NAME_TAG, create->DeviceName) ||
  7110. MsIsVLanExists(VLAN_ADAPTER_NAME_TAG_OLD, create->DeviceName))
  7111. {
  7112. // Already exist
  7113. CiSetError(c, ERR_VLAN_ALREADY_EXISTS);
  7114. return false;
  7115. }
  7116. if (MsIsNt())
  7117. {
  7118. if (MsIsVista() == false)
  7119. {
  7120. k = InitKakushi();
  7121. }
  7122. }
  7123. if (MsIsVista() == false)
  7124. {
  7125. MS_DRIVER_VER ver;
  7126. CiInitDriverVerStruct(&ver);
  7127. // Perform the installation (other than Windows Vista)
  7128. if (MsInstallVLan(VLAN_ADAPTER_NAME_TAG, VLAN_CONNECTION_NAME, create->DeviceName, &ver) == false)
  7129. {
  7130. // Installation Failed
  7131. FreeKakushi(k);
  7132. CiSetError(c, ERR_VLAN_INSTALL_ERROR);
  7133. CiNotify(c);
  7134. CiSendGlobalPulse(c);
  7135. return false;
  7136. }
  7137. }
  7138. else
  7139. {
  7140. // Perform the installation (Windows Vista)
  7141. char tmp[MAX_SIZE];
  7142. Format(tmp, sizeof(tmp), "instvlan %s", create->DeviceName);
  7143. if (CncExecDriverInstaller(tmp) == false)
  7144. {
  7145. // Installation Failed
  7146. FreeKakushi(k);
  7147. CiSetError(c, ERR_VLAN_INSTALL_ERROR);
  7148. CiNotify(c);
  7149. CiSendGlobalPulse(c);
  7150. return false;
  7151. }
  7152. }
  7153. FreeKakushi(k);
  7154. t = MsEnumNetworkAdapters(VLAN_ADAPTER_NAME, VLAN_ADAPTER_NAME_OLD);
  7155. if (t->NumTokens == 1)
  7156. {
  7157. UINT i;
  7158. // If the result of the installation, virtual LAN card is only one,
  7159. // set virtual LAN card setting of all existing accounts to this virtual LAN card
  7160. LockList(c->AccountList);
  7161. {
  7162. for (i = 0;i < LIST_NUM(c->AccountList);i++)
  7163. {
  7164. ACCOUNT *a = LIST_DATA(c->AccountList, i);
  7165. Lock(a->lock);
  7166. {
  7167. if (a->ClientOption != NULL)
  7168. {
  7169. StrCpy(a->ClientOption->DeviceName, sizeof(a->ClientOption->DeviceName), create->DeviceName);
  7170. }
  7171. }
  7172. Unlock(a->lock);
  7173. }
  7174. }
  7175. UnlockList(c->AccountList);
  7176. }
  7177. FreeToken(t);
  7178. CLog(c, "LC_CREATE_VLAN", create->DeviceName);
  7179. CiNormalizeAccountVLan(c);
  7180. CiNotify(c);
  7181. CiSendGlobalPulse(c);
  7182. CiSaveConfigurationFile(c);
  7183. if (MsIsNt() == false)
  7184. {
  7185. if (GetOsInfo()->OsType == OSTYPE_WINDOWS_ME)
  7186. {
  7187. // Show the warning in the case of Windows Me
  7188. MsgBox(NULL, 0x00000040L, _UU("CM_9X_VLAN_ME_MESSAGE"));
  7189. }
  7190. ReleaseThread(NewThread(Win9xRebootThread, NULL));
  7191. }
  7192. return true;
  7193. #endif // OS_WIN32
  7194. }
  7195. // Enumerate objects in the secure device
  7196. bool CtEnumObjectInSecure(CLIENT *c, RPC_ENUM_OBJECT_IN_SECURE *e)
  7197. {
  7198. UINT i;
  7199. // Validate arguments
  7200. if (c == NULL || e == NULL)
  7201. {
  7202. return false;
  7203. }
  7204. e->NumItem = 5;
  7205. e->ItemName = ZeroMalloc(sizeof(char *) * e->NumItem);
  7206. e->ItemType = ZeroMalloc(sizeof(bool) * e->NumItem);
  7207. for (i = 0;i < e->NumItem;i++)
  7208. {
  7209. char tmp[MAX_SIZE];
  7210. Format(tmp, sizeof(tmp), "Test Object %u", i);
  7211. e->ItemName[i] = CopyStr(tmp);
  7212. e->ItemType[i] = (i % 2 == 0) ? false : true;
  7213. }
  7214. return true;
  7215. }
  7216. // Get the secure device to be used
  7217. bool CtGetUseSecure(CLIENT *c, RPC_USE_SECURE *sec)
  7218. {
  7219. // Validate arguments
  7220. if (c == NULL || sec == NULL)
  7221. {
  7222. return false;
  7223. }
  7224. sec->DeviceId = c->UseSecureDeviceId;
  7225. return true;
  7226. }
  7227. // Specifying a secure device to be used
  7228. bool CtUseSecure(CLIENT *c, RPC_USE_SECURE *sec)
  7229. {
  7230. // Validate arguments
  7231. if (c == NULL || sec == NULL)
  7232. {
  7233. return false;
  7234. }
  7235. // Do not check whether there is the specified device on the client manager
  7236. /* if (CheckSecureDeviceId(sec->DeviceId))
  7237. {
  7238. c->UseSecureDeviceId = sec->DeviceId;
  7239. }
  7240. else
  7241. {
  7242. CiSetError(c, ERR_OBJECT_NOT_FOUND);
  7243. return false;
  7244. }
  7245. */
  7246. c->UseSecureDeviceId = sec->DeviceId;
  7247. CiSaveConfigurationFile(c);
  7248. return true;
  7249. }
  7250. // Enumeration of secure devices
  7251. bool CtEnumSecure(CLIENT *c, RPC_CLIENT_ENUM_SECURE *e)
  7252. {
  7253. LIST *o;
  7254. UINT i;
  7255. // Validate arguments
  7256. if (c == NULL || e == NULL)
  7257. {
  7258. return false;
  7259. }
  7260. o = GetSecureDeviceList();
  7261. e->NumItem = LIST_NUM(o);
  7262. e->Items = ZeroMalloc(sizeof(RPC_CLIENT_ENUM_SECURE_ITEM *) * e->NumItem);
  7263. for (i = 0;i < LIST_NUM(o);i++)
  7264. {
  7265. RPC_CLIENT_ENUM_SECURE_ITEM *item = ZeroMalloc(sizeof(RPC_CLIENT_ENUM_SECURE_ITEM));
  7266. SECURE_DEVICE *s = LIST_DATA(o, i);
  7267. item->DeviceId = s->Id;
  7268. StrCpy(item->DeviceName, sizeof(item->DeviceName), s->DeviceName);
  7269. StrCpy(item->Manufacturer, sizeof(item->Manufacturer), s->Manufacturer);
  7270. item->Type = s->Type;
  7271. e->Items[i] = item;
  7272. }
  7273. return true;
  7274. }
  7275. // Release the secure device enumeration
  7276. void CiFreeClientEnumSecure(RPC_CLIENT_ENUM_SECURE *e)
  7277. {
  7278. UINT i;
  7279. // Validate arguments
  7280. if (e == NULL)
  7281. {
  7282. return;
  7283. }
  7284. for (i = 0;i < e->NumItem;i++)
  7285. {
  7286. Free(e->Items[i]);
  7287. }
  7288. Free(e->Items);
  7289. }
  7290. // Release the RPC_GET_ISSUER
  7291. void CiFreeGetIssuer(RPC_GET_ISSUER *a)
  7292. {
  7293. // Validate arguments
  7294. if (a == NULL)
  7295. {
  7296. return;
  7297. }
  7298. if (a->issuer_x != NULL)
  7299. {
  7300. FreeX(a->issuer_x);
  7301. }
  7302. if (a->x != NULL)
  7303. {
  7304. FreeX(a->x);
  7305. }
  7306. }
  7307. // Get the common proxy settings
  7308. bool CtGetCommonProxySetting(CLIENT *c, INTERNET_SETTING *a)
  7309. {
  7310. // Validate arguments
  7311. if (c == NULL || a == NULL)
  7312. {
  7313. return false;
  7314. }
  7315. Copy(a, &c->CommonProxySetting, sizeof(INTERNET_SETTING));
  7316. return true;
  7317. }
  7318. // Set the common proxy settings
  7319. bool CtSetCommonProxySetting(CLIENT *c, INTERNET_SETTING *a)
  7320. {
  7321. // Validate arguments
  7322. if (c == NULL || a == NULL)
  7323. {
  7324. return false;
  7325. }
  7326. Copy(&c->CommonProxySetting, a, sizeof(INTERNET_SETTING));
  7327. CiSaveConfigurationFile(c);
  7328. return true;
  7329. }
  7330. // Get the issuer
  7331. bool CtGetIssuer(CLIENT *c, RPC_GET_ISSUER *a)
  7332. {
  7333. X *x;
  7334. // Validate arguments
  7335. if (c == NULL || a == NULL)
  7336. {
  7337. return false;
  7338. }
  7339. x = FindCaSignedX(c->Cedar->CaList, a->x);
  7340. if (x == NULL)
  7341. {
  7342. CiSetError(c, ERR_OBJECT_NOT_FOUND);;
  7343. return false;
  7344. }
  7345. else
  7346. {
  7347. a->issuer_x = x;
  7348. if (a->x != NULL)
  7349. {
  7350. FreeX(a->x);
  7351. a->x = NULL;
  7352. }
  7353. return true;
  7354. }
  7355. }
  7356. // Get the CA certificate
  7357. bool CtGetCa(CLIENT *c, RPC_GET_CA *get)
  7358. {
  7359. bool ret = true;
  7360. X *cert = NULL;
  7361. // Validate arguments
  7362. if (c == NULL || get == NULL)
  7363. {
  7364. return false;
  7365. }
  7366. LockList(c->Cedar->CaList);
  7367. {
  7368. UINT i;
  7369. for (i = 0;i < LIST_NUM(c->Cedar->CaList);i++)
  7370. {
  7371. X *x = LIST_DATA(c->Cedar->CaList, i);
  7372. if (POINTER_TO_KEY(x) == get->Key)
  7373. {
  7374. cert = CloneX(x);
  7375. break;
  7376. }
  7377. }
  7378. }
  7379. UnlockList(c->Cedar->CaList);
  7380. if (cert == NULL)
  7381. {
  7382. // Certificate does not exist
  7383. ret = false;
  7384. CiSetError(c, ERR_OBJECT_NOT_FOUND);
  7385. }
  7386. else
  7387. {
  7388. ret = true;
  7389. get->x = cert;
  7390. }
  7391. return ret;
  7392. }
  7393. // Delete the CA certificate
  7394. bool CtDeleteCa(CLIENT *c, RPC_CLIENT_DELETE_CA *p)
  7395. {
  7396. bool ret;
  7397. // Validate arguments
  7398. if (c == NULL || p == NULL)
  7399. {
  7400. return false;
  7401. }
  7402. ret = DeleteCa(c->Cedar, p->Key);
  7403. if (ret == false)
  7404. {
  7405. CiSetError(c, ERR_OBJECT_NOT_FOUND);
  7406. }
  7407. CiSaveConfigurationFile(c);
  7408. return ret;
  7409. }
  7410. // Add a CA certificate
  7411. bool CtAddCa(CLIENT *c, RPC_CERT *cert)
  7412. {
  7413. // Validate arguments
  7414. if (c == NULL || cert == NULL)
  7415. {
  7416. return false;
  7417. }
  7418. if (cert->x->is_compatible_bit == false)
  7419. {
  7420. CiSetError(c, ERR_NOT_RSA_1024);
  7421. return false;
  7422. }
  7423. AddCa(c->Cedar, cert->x);
  7424. CiSaveConfigurationFile(c);
  7425. return true;
  7426. }
  7427. // Enumerate the trusted CA
  7428. bool CtEnumCa(CLIENT *c, RPC_CLIENT_ENUM_CA *e)
  7429. {
  7430. // Validate arguments
  7431. if (c == NULL || e == NULL)
  7432. {
  7433. return false;
  7434. }
  7435. Zero(e, sizeof(RPC_CLIENT_ENUM_CA));
  7436. LockList(c->Cedar->CaList);
  7437. {
  7438. UINT i;
  7439. e->NumItem = LIST_NUM(c->Cedar->CaList);
  7440. e->Items = ZeroMalloc(sizeof(RPC_CLIENT_ENUM_CA_ITEM *) * e->NumItem);
  7441. for (i = 0;i < e->NumItem;i++)
  7442. {
  7443. X *x = LIST_DATA(c->Cedar->CaList, i);
  7444. e->Items[i] = ZeroMalloc(sizeof(RPC_CLIENT_ENUM_CA_ITEM));
  7445. GetAllNameFromNameEx(e->Items[i]->SubjectName, sizeof(e->Items[i]->SubjectName), x->subject_name);
  7446. GetAllNameFromNameEx(e->Items[i]->IssuerName, sizeof(e->Items[i]->IssuerName), x->issuer_name);
  7447. e->Items[i]->Expires = x->notAfter;
  7448. e->Items[i]->Key = POINTER_TO_KEY(x);
  7449. }
  7450. }
  7451. UnlockList(c->Cedar->CaList);
  7452. return true;
  7453. }
  7454. // Release the CA enumeration
  7455. void CiFreeClientEnumCa(RPC_CLIENT_ENUM_CA *e)
  7456. {
  7457. UINT i;
  7458. // Validate arguments
  7459. if (e == NULL)
  7460. {
  7461. return;
  7462. }
  7463. for (i = 0;i < e->NumItem;i++)
  7464. {
  7465. RPC_CLIENT_ENUM_CA_ITEM *ca = e->Items[i];
  7466. Free(ca);
  7467. }
  7468. Free(e->Items);
  7469. }
  7470. // Get the password setting
  7471. bool CtGetPasswordSetting(CLIENT *c, RPC_CLIENT_PASSWORD_SETTING *a)
  7472. {
  7473. UCHAR hash[SHA1_SIZE];
  7474. // Validate arguments
  7475. if (c == NULL || a == NULL)
  7476. {
  7477. return false;
  7478. }
  7479. Hash(hash, "", 0, true);
  7480. if (Cmp(hash, c->EncryptedPassword, SHA1_SIZE) == 0)
  7481. {
  7482. a->IsPasswordPresented = false;
  7483. }
  7484. else
  7485. {
  7486. a->IsPasswordPresented = true;
  7487. }
  7488. a->PasswordRemoteOnly = c->PasswordRemoteOnly;
  7489. return true;
  7490. }
  7491. // Set the password
  7492. bool CtSetPassword(CLIENT *c, RPC_CLIENT_PASSWORD *pass)
  7493. {
  7494. char *str;
  7495. if (c == NULL)
  7496. {
  7497. return false;
  7498. }
  7499. if (pass->Password == NULL)
  7500. {
  7501. str = "";
  7502. }
  7503. else
  7504. {
  7505. str = pass->Password;
  7506. }
  7507. if (StrCmp(str, "********") != 0)
  7508. {
  7509. // Hash the password
  7510. Hash(c->EncryptedPassword, str, StrLen(str), true);
  7511. }
  7512. c->PasswordRemoteOnly = pass->PasswordRemoteOnly;
  7513. CLog(c, "LC_SET_PASSWORD");
  7514. CiSaveConfigurationFile(c);
  7515. return true;
  7516. }
  7517. void CiFreeIni(LIST *o)
  7518. {
  7519. // Validate arguments
  7520. if (o == NULL)
  7521. {
  7522. return;
  7523. }
  7524. FreeIni(o);
  7525. }
  7526. // Read the custom.ini file
  7527. LIST *CiLoadIni()
  7528. {
  7529. BUF *b = ReadDump(CLIENT_CUSTOM_INI_FILENAME);
  7530. LIST *ini;
  7531. if (b == NULL)
  7532. {
  7533. return NULL;
  7534. }
  7535. ini = ReadIni(b);
  7536. FreeBuf(b);
  7537. return ini;
  7538. }
  7539. // Reflect the settings of the custom.ini
  7540. void CiLoadIniSettings(CLIENT *c)
  7541. {
  7542. LIST *o;
  7543. //char *log;
  7544. //char *config;
  7545. if (c == NULL)
  7546. {
  7547. return;
  7548. }
  7549. o = CiLoadIni();
  7550. if (o == NULL)
  7551. {
  7552. return;
  7553. }
  7554. /*log = IniStrValue(o, "NoSaveLog");
  7555. config = IniStrValue(o, "NoSaveConfig");
  7556. if(StrCmpi(log, "true") == 0)
  7557. {
  7558. c->NoSaveLog = true;
  7559. }
  7560. if(StrCmpi(config, "true") == 0)
  7561. {
  7562. c->NoSaveConfig = true;
  7563. }*/
  7564. c->NoSaveLog = ToBool(IniStrValue(o, "NoSaveLog"));
  7565. c->NoSaveConfig = ToBool(IniStrValue(o, "NoSaveConfig"));
  7566. CiFreeIni(o);
  7567. }
  7568. bool CiLoadConfigFilePathFromIni(char *path, UINT size)
  7569. {
  7570. char *tmp;
  7571. LIST *o;
  7572. bool ret = false;
  7573. // Validate arguments
  7574. if (path == NULL)
  7575. {
  7576. return false;
  7577. }
  7578. o = CiLoadIni();
  7579. if (o == NULL)
  7580. {
  7581. return false;
  7582. }
  7583. StrCpy(path, size, "");
  7584. tmp = IniStrValue(o, "ConfigPath");
  7585. NormalizePath(path, size, tmp);
  7586. if (IsEmptyStr(path) == false)
  7587. {
  7588. ret = true;
  7589. }
  7590. else
  7591. {
  7592. ret = false;
  7593. }
  7594. CiFreeIni(o);
  7595. return ret;
  7596. }
  7597. // Set the client error code
  7598. void CiSetError(CLIENT *c, UINT err)
  7599. {
  7600. // Validate arguments
  7601. if (c == NULL)
  7602. {
  7603. return;
  7604. }
  7605. c->Err = err;
  7606. }
  7607. // UNIX virtual LAN card comparison function
  7608. int CiCompareUnixVLan(void *p1, void *p2)
  7609. {
  7610. UNIX_VLAN *v1, *v2;
  7611. if (p1 == NULL || p2 == NULL)
  7612. {
  7613. return 0;
  7614. }
  7615. v1 = *(UNIX_VLAN **)p1;
  7616. v2 = *(UNIX_VLAN **)p2;
  7617. if (v1 == NULL || v2 == NULL)
  7618. {
  7619. return 0;
  7620. }
  7621. return StrCmpi(v1->Name, v2->Name);
  7622. }
  7623. // Modify the account settings that an incorrect VLAN name is specified
  7624. void CiNormalizeAccountVLan(CLIENT *c)
  7625. {
  7626. bool b = false;
  7627. char *name;
  7628. UINT i;
  7629. // Validate arguments
  7630. if (c == NULL)
  7631. {
  7632. return;
  7633. }
  7634. name = CiGetFirstVLan(c);
  7635. if (name != NULL)
  7636. {
  7637. LockList(c->AccountList);
  7638. {
  7639. for (i = 0;i < LIST_NUM(c->AccountList);i++)
  7640. {
  7641. ACCOUNT *a = LIST_DATA(c->AccountList, i);
  7642. Lock(a->lock);
  7643. {
  7644. if (a->ClientOption != NULL)
  7645. {
  7646. if (CiIsVLan(c, a->ClientOption->DeviceName) == false)
  7647. {
  7648. StrCpy(a->ClientOption->DeviceName, sizeof(a->ClientOption->DeviceName),
  7649. name);
  7650. b = true;
  7651. }
  7652. }
  7653. }
  7654. Unlock(a->lock);
  7655. }
  7656. }
  7657. UnlockList(c->AccountList);
  7658. Free(name);
  7659. }
  7660. if (b)
  7661. {
  7662. CiNotify(c);
  7663. CiSendGlobalPulse(c);
  7664. CiSaveConfigurationFile(c);
  7665. }
  7666. }
  7667. // Check whether a virtual LAN card of the specified name exists
  7668. bool CiIsVLan(CLIENT *c, char *name)
  7669. {
  7670. // Validate arguments
  7671. if (c == NULL || name == NULL)
  7672. {
  7673. return false;
  7674. }
  7675. #ifdef OS_WIN32
  7676. {
  7677. TOKEN_LIST *t;
  7678. UINT i;
  7679. t = MsEnumNetworkAdapters(VLAN_ADAPTER_NAME, VLAN_ADAPTER_NAME_OLD);
  7680. if (t == NULL)
  7681. {
  7682. return false;
  7683. }
  7684. for (i = 0;i < t->NumTokens;i++)
  7685. {
  7686. if (StrCmpi(t->Token[i], name) == 0)
  7687. {
  7688. FreeToken(t);
  7689. return true;
  7690. }
  7691. }
  7692. FreeToken(t);
  7693. return false;
  7694. }
  7695. #else // OS_WIN32
  7696. {
  7697. UNIX_VLAN *v;
  7698. UINT i;
  7699. bool ret = false;
  7700. LockList(c->UnixVLanList);
  7701. {
  7702. for (i = 0;i < LIST_NUM(c->UnixVLanList);i++)
  7703. {
  7704. v = (UNIX_VLAN *)LIST_DATA(c->UnixVLanList, i);
  7705. if (StrCmpi(v->Name, name) == 0)
  7706. {
  7707. ret = true;
  7708. }
  7709. }
  7710. }
  7711. UnlockList(c->UnixVLanList);
  7712. return ret;
  7713. }
  7714. #endif // OS_WIN32
  7715. }
  7716. // If a non-existent virtual LAN card is specified in any Account, and only
  7717. // one virtual LAN card is installed, set the virtual LAN card to the account
  7718. void CiSetVLanToDefault(CLIENT *c)
  7719. {
  7720. char device_name[MAX_SIZE];
  7721. // Validate arguments
  7722. if (c == NULL)
  7723. {
  7724. return;
  7725. }
  7726. #ifdef OS_WIN32
  7727. {
  7728. TOKEN_LIST *t;
  7729. t = MsEnumNetworkAdapters(VLAN_ADAPTER_NAME, VLAN_ADAPTER_NAME_OLD);
  7730. if (t == NULL)
  7731. {
  7732. return;
  7733. }
  7734. if (t->NumTokens != 1)
  7735. {
  7736. FreeToken(t);
  7737. return;
  7738. }
  7739. StrCpy(device_name, sizeof(device_name), t->Token[0]);
  7740. FreeToken(t);
  7741. }
  7742. #else // OS_WIN32
  7743. {
  7744. UINT i;
  7745. UNIX_VLAN *v;
  7746. LockList(c->UnixVLanList);
  7747. if (LIST_NUM(c->UnixVLanList) != 1)
  7748. {
  7749. UnlockList(c->UnixVLanList);
  7750. return;
  7751. }
  7752. v = LIST_DATA(c->UnixVLanList, 0);
  7753. StrCpy(device_name, sizeof(device_name), v->Name);
  7754. UnlockList(c->UnixVLanList);
  7755. }
  7756. #endif // OS_WIN32
  7757. {
  7758. UINT i;
  7759. LockList(c->AccountList);
  7760. {
  7761. for (i = 0;i < LIST_NUM(c->AccountList);i++)
  7762. {
  7763. ACCOUNT *a = LIST_DATA(c->AccountList, i);
  7764. Lock(a->lock);
  7765. {
  7766. if (CiIsVLan(c, a->ClientOption->DeviceName) == false)
  7767. {
  7768. StrCpy(a->ClientOption->DeviceName, sizeof(a->ClientOption->DeviceName),
  7769. device_name);
  7770. }
  7771. }
  7772. Unlock(a->lock);
  7773. }
  7774. }
  7775. UnlockList(c->AccountList);
  7776. }
  7777. }
  7778. // Initialize the settings
  7779. void CiInitConfiguration(CLIENT *c)
  7780. {
  7781. // Validate arguments
  7782. if (c == NULL)
  7783. {
  7784. return;
  7785. }
  7786. #ifdef OS_UNIX
  7787. // Initialize the VLAN
  7788. UnixVLanInit();
  7789. #endif // OS_UNIX
  7790. // Account list
  7791. c->AccountList = NewList(CiCompareAccount);
  7792. // Unix version VLAN list
  7793. if (OS_IS_UNIX(GetOsInfo()->OsType))
  7794. {
  7795. c->UnixVLanList = NewList(CiCompareUnixVLan);
  7796. }
  7797. // Read the configuration file
  7798. CLog(c, "LC_LOAD_CONFIG_1");
  7799. if (CiLoadConfigurationFile(c) == false)
  7800. {
  7801. CLog(c, "LC_LOAD_CONFIG_3");
  7802. // Do the initial setup because the configuration file does not exist
  7803. // Clear the password
  7804. Hash(c->EncryptedPassword, "", 0, true);
  7805. // Initialize the client configuration
  7806. if (OS_IS_WINDOWS(GetOsInfo()->OsType))
  7807. {
  7808. // Disable remote management in Windows
  7809. c->Config.AllowRemoteConfig = false;
  7810. }
  7811. else
  7812. {
  7813. // Disable the remote management also in case of UNIX
  7814. c->Config.AllowRemoteConfig = false;
  7815. }
  7816. StrCpy(c->Config.KeepConnectHost, sizeof(c->Config.KeepConnectHost), CLIENT_DEFAULT_KEEPALIVE_HOST);
  7817. c->Config.KeepConnectPort = CLIENT_DEFAULT_KEEPALIVE_PORT;
  7818. c->Config.KeepConnectProtocol = CONNECTION_UDP;
  7819. c->Config.KeepConnectInterval = CLIENT_DEFAULT_KEEPALIVE_INTERVAL;
  7820. c->Config.UseKeepConnect = false; // Don't use the connection maintenance function by default in the Client
  7821. // Eraser
  7822. c->Eraser = NewEraser(c->Logger, 0);
  7823. }
  7824. else
  7825. {
  7826. CLog(c, "LC_LOAD_CONFIG_2");
  7827. }
  7828. // Appropriate setting for virtual LAN card
  7829. CiSetVLanToDefault(c);
  7830. }
  7831. // Release the settings
  7832. void CiFreeConfiguration(CLIENT *c)
  7833. {
  7834. UINT i;
  7835. // Validate arguments
  7836. if (c == NULL)
  7837. {
  7838. return;
  7839. }
  7840. // Write to the configuration file
  7841. CiSaveConfigurationFile(c);
  7842. // Release the configuration file
  7843. FreeCfgRw(c->CfgRw);
  7844. // Release the account list
  7845. for (i = 0;i < LIST_NUM(c->AccountList);i++)
  7846. {
  7847. ACCOUNT *a = LIST_DATA(c->AccountList, i);
  7848. CiFreeAccount(a);
  7849. }
  7850. ReleaseList(c->AccountList);
  7851. if (c->UnixVLanList != NULL)
  7852. {
  7853. // Release of UNIX version VLAN list
  7854. for (i = 0;i < LIST_NUM(c->UnixVLanList);i++)
  7855. {
  7856. UNIX_VLAN *v = LIST_DATA(c->UnixVLanList, i);
  7857. Free(v);
  7858. }
  7859. ReleaseList(c->UnixVLanList);
  7860. }
  7861. c->UnixVLanList = NULL;
  7862. #ifdef OS_UNIX
  7863. // Release the VLAN
  7864. UnixVLanFree();
  7865. #endif // OS_UNIX
  7866. }
  7867. // Release the certificate data acquisition
  7868. void CiFreeGetCa(RPC_GET_CA *a)
  7869. {
  7870. // Validate arguments
  7871. if (a == NULL)
  7872. {
  7873. return;
  7874. }
  7875. FreeX(a->x);
  7876. }
  7877. // Release the client authentication data
  7878. void CiFreeClientAuth(CLIENT_AUTH *auth)
  7879. {
  7880. // Validate arguments
  7881. if (auth == NULL)
  7882. {
  7883. return;
  7884. }
  7885. if (auth->ClientX != NULL)
  7886. {
  7887. FreeX(auth->ClientX);
  7888. }
  7889. if (auth->ClientK != NULL)
  7890. {
  7891. FreeK(auth->ClientK);
  7892. }
  7893. Free(auth);
  7894. }
  7895. // Release the account
  7896. void CiFreeAccount(ACCOUNT *a)
  7897. {
  7898. // Validate arguments
  7899. if (a == NULL)
  7900. {
  7901. return;
  7902. }
  7903. // Release the lock
  7904. DeleteLock(a->lock);
  7905. // Release the client option
  7906. Free(a->ClientOption);
  7907. // Release the client authentication data
  7908. CiFreeClientAuth(a->ClientAuth);
  7909. if (a->ServerCert != NULL)
  7910. {
  7911. FreeX(a->ServerCert);
  7912. }
  7913. Free(a);
  7914. }
  7915. // Sort accounts
  7916. int CiCompareAccount(void *p1, void *p2)
  7917. {
  7918. ACCOUNT *a1, *a2;
  7919. if (p1 == NULL || p2 == NULL)
  7920. {
  7921. return 0;
  7922. }
  7923. a1 = *(ACCOUNT **)p1;
  7924. a2 = *(ACCOUNT **)p2;
  7925. if (a1 == NULL || a2 == NULL)
  7926. {
  7927. return 0;
  7928. }
  7929. return UniStrCmpi(a1->ClientOption->AccountName, a2->ClientOption->AccountName);
  7930. }
  7931. // Read the client configuration
  7932. void CiLoadClientConfig(CLIENT_CONFIG *c, FOLDER *f)
  7933. {
  7934. // Validate arguments
  7935. if (c == NULL || f == NULL)
  7936. {
  7937. return;
  7938. }
  7939. c->UseKeepConnect = CfgGetBool(f, "UseKeepConnect");
  7940. CfgGetStr(f, "KeepConnectHost", c->KeepConnectHost, sizeof(c->KeepConnectHost));
  7941. c->KeepConnectPort = CfgGetInt(f, "KeepConnectPort");
  7942. c->KeepConnectProtocol = CfgGetInt(f, "KeepConnectProtocol");
  7943. c->AllowRemoteConfig = CfgGetBool(f, "AllowRemoteConfig");
  7944. c->KeepConnectInterval = MAKESURE(CfgGetInt(f, "KeepConnectInterval"), KEEP_INTERVAL_MIN, KEEP_INTERVAL_MAX);
  7945. c->NoChangeWcmNetworkSettingOnWindows8 = CfgGetBool(f, "NoChangeWcmNetworkSettingOnWindows8");
  7946. }
  7947. // Read the client authentication data
  7948. CLIENT_AUTH *CiLoadClientAuth(FOLDER *f)
  7949. {
  7950. CLIENT_AUTH *a;
  7951. char *s;
  7952. BUF *b;
  7953. // Validate arguments
  7954. if (f == NULL)
  7955. {
  7956. return NULL;
  7957. }
  7958. a = ZeroMalloc(sizeof(CLIENT_AUTH));
  7959. a->AuthType = CfgGetInt(f, "AuthType");
  7960. CfgGetStr(f, "Username", a->Username, sizeof(a->Username));
  7961. switch (a->AuthType)
  7962. {
  7963. case CLIENT_AUTHTYPE_ANONYMOUS:
  7964. break;
  7965. case CLIENT_AUTHTYPE_PASSWORD:
  7966. CfgGetByte(f, "HashedPassword", a->HashedPassword, SHA1_SIZE);
  7967. break;
  7968. case CLIENT_AUTHTYPE_PLAIN_PASSWORD:
  7969. b = CfgGetBuf(f, "EncryptedPassword");
  7970. if (b != NULL)
  7971. {
  7972. s = DecryptPassword(b);
  7973. StrCpy(a->PlainPassword, sizeof(a->PlainPassword), s);
  7974. Free(s);
  7975. FreeBuf(b);
  7976. }
  7977. break;
  7978. case CLIENT_AUTHTYPE_CERT:
  7979. b = CfgGetBuf(f, "ClientCert");
  7980. if (b != NULL)
  7981. {
  7982. a->ClientX = BufToX(b, false);
  7983. }
  7984. FreeBuf(b);
  7985. b = CfgGetBuf(f, "ClientKey");
  7986. if (b != NULL)
  7987. {
  7988. a->ClientK = BufToK(b, true, false, NULL);
  7989. }
  7990. FreeBuf(b);
  7991. break;
  7992. case CLIENT_AUTHTYPE_SECURE:
  7993. CfgGetStr(f, "SecurePublicCertName", a->SecurePublicCertName, sizeof(a->SecurePublicCertName));
  7994. CfgGetStr(f, "SecurePrivateKeyName", a->SecurePrivateKeyName, sizeof(a->SecurePrivateKeyName));
  7995. break;
  7996. }
  7997. return a;
  7998. }
  7999. // Read the client option
  8000. CLIENT_OPTION *CiLoadClientOption(FOLDER *f)
  8001. {
  8002. CLIENT_OPTION *o;
  8003. char *s;
  8004. BUF *b;
  8005. // Validate arguments
  8006. if (f == NULL)
  8007. {
  8008. return NULL;
  8009. }
  8010. o = ZeroMalloc(sizeof(CLIENT_OPTION));
  8011. CfgGetUniStr(f, "AccountName", o->AccountName, sizeof(o->AccountName));
  8012. CfgGetStr(f, "Hostname", o->Hostname, sizeof(o->Hostname));
  8013. o->Port = CfgGetInt(f, "Port");
  8014. o->PortUDP = CfgGetInt(f, "PortUDP");
  8015. o->ProxyType = CfgGetInt(f, "ProxyType");
  8016. CfgGetStr(f, "ProxyName", o->ProxyName, sizeof(o->ProxyName));
  8017. o->ProxyPort = CfgGetInt(f, "ProxyPort");
  8018. CfgGetStr(f, "ProxyUsername", o->ProxyUsername, sizeof(o->ProxyUsername));
  8019. b = CfgGetBuf(f, "ProxyPassword");
  8020. s = DecryptPassword(b);
  8021. StrCpy(o->ProxyPassword, sizeof(o->ProxyPassword), s);
  8022. Free(s);
  8023. FreeBuf(b);
  8024. o->NumRetry = CfgGetInt(f, "NumRetry");
  8025. o->RetryInterval = CfgGetInt(f, "RetryInterval");
  8026. CfgGetStr(f, "HubName", o->HubName, sizeof(o->HubName));
  8027. o->MaxConnection = CfgGetInt(f, "MaxConnection");
  8028. o->UseEncrypt = CfgGetBool(f, "UseEncrypt");
  8029. o->UseCompress = CfgGetBool(f, "UseCompress");
  8030. o->HalfConnection = CfgGetBool(f, "HalfConnection");
  8031. o->NoRoutingTracking = CfgGetBool(f, "NoRoutingTracking");
  8032. CfgGetStr(f, "DeviceName", o->DeviceName, sizeof(o->DeviceName));
  8033. o->AdditionalConnectionInterval = CfgGetInt(f, "AdditionalConnectionInterval");
  8034. o->HideStatusWindow = CfgGetBool(f, "HideStatusWindow");
  8035. o->HideNicInfoWindow = CfgGetBool(f, "HideNicInfoWindow");
  8036. o->ConnectionDisconnectSpan = CfgGetInt(f, "ConnectionDisconnectSpan");
  8037. o->RequireMonitorMode = CfgGetBool(f, "RequireMonitorMode");
  8038. o->RequireBridgeRoutingMode = CfgGetBool(f, "RequireBridgeRoutingMode");
  8039. o->DisableQoS = CfgGetBool(f, "DisableQoS");
  8040. o->FromAdminPack = CfgGetBool(f, "FromAdminPack");
  8041. o->NoTls1 = CfgGetBool(f, "NoTls1");
  8042. o->NoUdpAcceleration = CfgGetBool(f, "NoUdpAcceleration");
  8043. b = CfgGetBuf(f, "HostUniqueKey");
  8044. if (b != NULL)
  8045. {
  8046. if (b->Size == SHA1_SIZE)
  8047. {
  8048. Copy(o->HostUniqueKey, b->Buf, SHA1_SIZE);
  8049. }
  8050. FreeBuf(b);
  8051. }
  8052. return o;
  8053. }
  8054. // Read the account data
  8055. ACCOUNT *CiLoadClientAccount(FOLDER *f)
  8056. {
  8057. ACCOUNT *a;
  8058. FOLDER *client_option_folder, *client_auth_folder;
  8059. BUF *b;
  8060. char tmp[64];
  8061. // Validate arguments
  8062. if (f == NULL)
  8063. {
  8064. return NULL;
  8065. }
  8066. client_option_folder = CfgGetFolder(f, "ClientOption");
  8067. if (client_option_folder != NULL)
  8068. {
  8069. // Compare whether it matches to the account name that is already registered
  8070. }
  8071. client_auth_folder = CfgGetFolder(f, "ClientAuth");
  8072. if (client_option_folder == NULL || client_auth_folder == NULL)
  8073. {
  8074. return NULL;
  8075. }
  8076. a = ZeroMalloc(sizeof(ACCOUNT));
  8077. a->lock = NewLock();
  8078. a->ClientOption = CiLoadClientOption(client_option_folder);
  8079. a->ClientAuth = CiLoadClientAuth(client_auth_folder);
  8080. a->StartupAccount = CfgGetBool(f, "StartupAccount");
  8081. a->CheckServerCert = CfgGetBool(f, "CheckServerCert");
  8082. a->CreateDateTime = CfgGetInt64(f, "CreateDateTime");
  8083. a->UpdateDateTime = CfgGetInt64(f, "UpdateDateTime");
  8084. a->LastConnectDateTime = CfgGetInt64(f, "LastConnectDateTime");
  8085. b = CfgGetBuf(f, "ServerCert");
  8086. if (b != NULL)
  8087. {
  8088. a->ServerCert = BufToX(b, false);
  8089. FreeBuf(b);
  8090. }
  8091. if (CfgGetStr(f, "ShortcutKey", tmp, sizeof(tmp)))
  8092. {
  8093. BUF *b = StrToBin(tmp);
  8094. if (b->Size == SHA1_SIZE)
  8095. {
  8096. Copy(a->ShortcutKey, b->Buf, SHA1_SIZE);
  8097. }
  8098. FreeBuf(b);
  8099. }
  8100. if (IsZero(a->ShortcutKey, SHA1_SIZE))
  8101. {
  8102. Rand(a->ShortcutKey, SHA1_SIZE);
  8103. }
  8104. return a;
  8105. }
  8106. // Read the account database
  8107. void CiLoadAccountDatabase(CLIENT *c, FOLDER *f)
  8108. {
  8109. TOKEN_LIST *t;
  8110. UINT i;
  8111. // Validate arguments
  8112. if (c == NULL || f == NULL)
  8113. {
  8114. return;
  8115. }
  8116. t = CfgEnumFolderToTokenList(f);
  8117. if (t == NULL)
  8118. {
  8119. return;
  8120. }
  8121. for (i = 0;i < t->NumTokens;i++)
  8122. {
  8123. FOLDER *ff = CfgGetFolder(f, t->Token[i]);
  8124. if (ff != NULL)
  8125. {
  8126. ACCOUNT *a = CiLoadClientAccount(ff);
  8127. if (a != NULL)
  8128. {
  8129. {
  8130. Add(c->AccountList, a);
  8131. }
  8132. }
  8133. }
  8134. }
  8135. Sort(c->AccountList);
  8136. FreeToken(t);
  8137. }
  8138. // Read the root CA certificate
  8139. void CiLoadCACert(CLIENT *c, FOLDER *f)
  8140. {
  8141. BUF *b;
  8142. X *x;
  8143. // Validate arguments
  8144. if (c == NULL || f == NULL)
  8145. {
  8146. return;
  8147. }
  8148. b = CfgGetBuf(f, "X509");
  8149. if (b == NULL)
  8150. {
  8151. return;
  8152. }
  8153. x = BufToX(b, false);
  8154. AddCa(c->Cedar, x);
  8155. FreeX(x);
  8156. FreeBuf(b);
  8157. }
  8158. // Read the root CA list
  8159. void CiLoadCAList(CLIENT *c, FOLDER *f)
  8160. {
  8161. CEDAR *cedar;
  8162. TOKEN_LIST *t;
  8163. // Validate arguments
  8164. if (c == NULL || f == NULL)
  8165. {
  8166. return;
  8167. }
  8168. t = CfgEnumFolderToTokenList(f);
  8169. cedar = c->Cedar;
  8170. LockList(cedar->CaList);
  8171. {
  8172. UINT i;
  8173. for (i = 0;i < t->NumTokens;i++)
  8174. {
  8175. FOLDER *folder = CfgGetFolder(f, t->Token[i]);
  8176. CiLoadCACert(c, folder);
  8177. }
  8178. }
  8179. UnlockList(cedar->CaList);
  8180. FreeToken(t);
  8181. }
  8182. // Read a VLAN
  8183. void CiLoadVLan(CLIENT *c, FOLDER *f)
  8184. {
  8185. char tmp[MAX_SIZE];
  8186. UCHAR addr[6];
  8187. BUF *b;
  8188. UNIX_VLAN *v;
  8189. // Validate arguments
  8190. if (c == NULL || f == NULL)
  8191. {
  8192. return;
  8193. }
  8194. if (CfgGetStr(f, "MacAddress", tmp, sizeof(tmp)) == false)
  8195. {
  8196. return;
  8197. }
  8198. b = StrToBin(tmp);
  8199. if (b == NULL)
  8200. {
  8201. return;
  8202. }
  8203. if (b->Size != 6)
  8204. {
  8205. FreeBuf(b);
  8206. return;
  8207. }
  8208. Copy(addr, b->Buf, 6);
  8209. FreeBuf(b);
  8210. if (IsZero(addr, 6))
  8211. {
  8212. return;
  8213. }
  8214. v = ZeroMalloc(sizeof(UNIX_VLAN));
  8215. Copy(v->MacAddress, addr, 6);
  8216. StrCpy(v->Name, sizeof(v->Name), f->Name);
  8217. v->Enabled = CfgGetBool(f, "Enabled");
  8218. Add(c->UnixVLanList, v);
  8219. #ifdef OS_UNIX
  8220. UnixVLanCreate(v->Name, v->MacAddress);
  8221. #endif // OS_UNIX
  8222. }
  8223. // Read a VLAN list
  8224. void CiLoadVLanList(CLIENT *c, FOLDER *f)
  8225. {
  8226. TOKEN_LIST *t;
  8227. // Validate arguments
  8228. if (c == NULL || f == NULL)
  8229. {
  8230. return;
  8231. }
  8232. t = CfgEnumFolderToTokenList(f);
  8233. LockList(c->UnixVLanList);
  8234. {
  8235. UINT i;
  8236. for (i = 0;i < t->NumTokens;i++)
  8237. {
  8238. FOLDER *folder = CfgGetFolder(f, t->Token[i]);
  8239. CiLoadVLan(c, folder);
  8240. }
  8241. }
  8242. UnlockList(c->UnixVLanList);
  8243. FreeToken(t);
  8244. }
  8245. // Read the configuration from the configuration file
  8246. bool CiReadSettingFromCfg(CLIENT *c, FOLDER *root)
  8247. {
  8248. FOLDER *config;
  8249. FOLDER *cert;
  8250. FOLDER *db;
  8251. FOLDER *vlan;
  8252. FOLDER *cmsetting;
  8253. FOLDER *proxy;
  8254. char user_agent[MAX_SIZE];
  8255. // Validate arguments
  8256. if (c == NULL || root == NULL)
  8257. {
  8258. return false;
  8259. }
  8260. // Initialize the setting if there isn't either of AccountDatabase and Config
  8261. config = CfgGetFolder(root, "Config");
  8262. if (config == NULL)
  8263. {
  8264. return false;
  8265. }
  8266. db = CfgGetFolder(root, "AccountDatabase");
  8267. if (db == NULL)
  8268. {
  8269. return false;
  8270. }
  8271. cmsetting = CfgGetFolder(root, "ClientManagerSetting");
  8272. CiLoadClientConfig(&c->Config, config);
  8273. proxy = CfgGetFolder(root, "CommonProxySetting");
  8274. if (proxy != NULL)
  8275. {
  8276. INTERNET_SETTING t;
  8277. BUF *pw;
  8278. // Proxy Setting
  8279. Zero(&t, sizeof(t));
  8280. t.ProxyType = CfgGetInt(proxy, "ProxyType");
  8281. CfgGetStr(proxy, "ProxyHostName", t.ProxyHostName, sizeof(t.ProxyHostName));
  8282. t.ProxyPort = CfgGetInt(proxy, "ProxyPort");
  8283. CfgGetStr(proxy, "ProxyUsername", t.ProxyUsername, sizeof(t.ProxyUsername));
  8284. pw = CfgGetBuf(proxy, "ProxyPassword");
  8285. if (pw != NULL)
  8286. {
  8287. char *pw_str = DecryptPassword(pw);
  8288. StrCpy(t.ProxyPassword, sizeof(t.ProxyPassword), pw_str);
  8289. Free(pw_str);
  8290. FreeBuf(pw);
  8291. }
  8292. Copy(&c->CommonProxySetting, &t, sizeof(INTERNET_SETTING));
  8293. }
  8294. // Eraser
  8295. c->Eraser = NewEraser(c->Logger, CfgGetInt64(config, "AutoDeleteCheckDiskFreeSpaceMin"));
  8296. if (OS_IS_UNIX(GetOsInfo()->OsType)
  8297. #ifdef NO_VLAN
  8298. && GetOsInfo()->OsType != OSTYPE_MACOS_X
  8299. #endif // NO_VLAN
  8300. )
  8301. {
  8302. // Read the UNIX version virtual LAN card list (except MacOS)
  8303. vlan = CfgGetFolder(root, "UnixVLan");
  8304. if (vlan != NULL)
  8305. {
  8306. CiLoadVLanList(c, vlan);
  8307. }
  8308. }
  8309. #ifdef NO_VLAN
  8310. if (GetOsInfo()->OsType == OSTYPE_MACOS_X)
  8311. {
  8312. #ifdef OS_UNIX
  8313. UNIX_VLAN *uv;
  8314. // Create a Tap for MacOS X
  8315. if (UnixVLanCreate(CLIENT_MACOS_TAP_NAME, NULL) == false)
  8316. {
  8317. // Fail (abort)
  8318. CLog(c, "LC_TAP_NOT_FOUND");
  8319. Alert("tun/tap driver not found.", NULL);
  8320. exit(0);
  8321. }
  8322. uv = ZeroMalloc(sizeof(UNIX_VLAN));
  8323. uv->Enabled = true;
  8324. StrCpy(uv->Name, sizeof(uv->Name), CLIENT_MACOS_TAP_NAME);
  8325. Add(c->UnixVLanList, uv);
  8326. #endif // OS_UNIX
  8327. }
  8328. #endif // NO_VLAN
  8329. CiLoadAccountDatabase(c, db);
  8330. if (CfgGetByte(root, "EncryptedPassword", c->EncryptedPassword, SHA1_SIZE) == false)
  8331. {
  8332. Hash(c->EncryptedPassword, "", 0, true);
  8333. }
  8334. c->PasswordRemoteOnly = CfgGetBool(root, "PasswordRemoteOnly");
  8335. c->UseSecureDeviceId = CfgGetInt(root, "UseSecureDeviceId");
  8336. if (CfgGetStr(root, "UserAgent", user_agent, sizeof(user_agent)))
  8337. {
  8338. if (IsEmptyStr(user_agent) == false)
  8339. {
  8340. Free(c->Cedar->HttpUserAgent);
  8341. c->Cedar->HttpUserAgent = CopyStr(user_agent);
  8342. }
  8343. }
  8344. cert = CfgGetFolder(root, "RootCA");
  8345. if (cert != NULL)
  8346. {
  8347. CiLoadCAList(c, cert);
  8348. }
  8349. c->DontSavePassword = CfgGetBool(root, "DontSavePassword");
  8350. if (cmsetting != NULL)
  8351. {
  8352. UINT ostype = GetOsInfo()->OsType;
  8353. // CM_SETTING
  8354. CM_SETTING *s = c->CmSetting;
  8355. if (OS_IS_UNIX(ostype) || OS_IS_WINDOWS_NT(ostype))
  8356. {
  8357. s->EasyMode = CfgGetBool(cmsetting, "EasyMode");
  8358. }
  8359. s->LockMode = CfgGetBool(cmsetting, "LockMode");
  8360. CfgGetByte(cmsetting, "HashedPassword", s->HashedPassword, sizeof(s->HashedPassword));
  8361. }
  8362. return true;
  8363. }
  8364. // Read the configuration file
  8365. bool CiLoadConfigurationFile(CLIENT *c)
  8366. {
  8367. bool ret;
  8368. FOLDER *root;
  8369. char path[MAX_SIZE];
  8370. // Validate arguments
  8371. if (c == NULL)
  8372. {
  8373. return false;
  8374. }
  8375. // Read the configuration file
  8376. if (CiLoadConfigFilePathFromIni(path, sizeof(path)))
  8377. {
  8378. c->CfgRw = NewCfgRw(&root, path);
  8379. }
  8380. else
  8381. {
  8382. c->CfgRw = NewCfgRw(&root, CLIENT_CONFIG_FILE_NAME);
  8383. }
  8384. if (root == NULL)
  8385. {
  8386. return false;
  8387. }
  8388. ret = CiReadSettingFromCfg(c, root);
  8389. CfgDeleteFolder(root);
  8390. return ret;
  8391. }
  8392. // Write the CLIENT_CONFIG
  8393. void CiWriteClientConfig(FOLDER *cc, CLIENT_CONFIG *config)
  8394. {
  8395. // Validate arguments
  8396. if (cc == NULL || config == NULL)
  8397. {
  8398. return;
  8399. }
  8400. CfgAddBool(cc, "UseKeepConnect", config->UseKeepConnect);
  8401. CfgAddStr(cc, "KeepConnectHost", config->KeepConnectHost);
  8402. CfgAddInt(cc, "KeepConnectPort", config->KeepConnectPort);
  8403. CfgAddInt(cc, "KeepConnectProtocol", config->KeepConnectProtocol);
  8404. CfgAddBool(cc, "AllowRemoteConfig", config->AllowRemoteConfig);
  8405. CfgAddInt(cc, "KeepConnectInterval", config->KeepConnectInterval);
  8406. CfgAddBool(cc, "NoChangeWcmNetworkSettingOnWindows8", config->NoChangeWcmNetworkSettingOnWindows8);
  8407. }
  8408. // Write the client authentication data
  8409. void CiWriteClientAuth(FOLDER *f, CLIENT_AUTH *a)
  8410. {
  8411. BUF *b;
  8412. // Validate arguments
  8413. if (f == NULL || a == NULL)
  8414. {
  8415. return;
  8416. }
  8417. CfgAddInt(f, "AuthType", a->AuthType);
  8418. CfgAddStr(f, "Username", a->Username);
  8419. switch (a->AuthType)
  8420. {
  8421. case CLIENT_AUTHTYPE_ANONYMOUS:
  8422. break;
  8423. case CLIENT_AUTHTYPE_PASSWORD:
  8424. CfgAddByte(f, "HashedPassword", a->HashedPassword, SHA1_SIZE);
  8425. break;
  8426. case CLIENT_AUTHTYPE_PLAIN_PASSWORD:
  8427. b = EncryptPassword(a->PlainPassword);
  8428. CfgAddByte(f, "EncryptedPassword", b->Buf, b->Size);
  8429. FreeBuf(b);
  8430. break;
  8431. case CLIENT_AUTHTYPE_CERT:
  8432. if (a->ClientK != NULL && a->ClientX != NULL)
  8433. {
  8434. b = XToBuf(a->ClientX, false);
  8435. CfgAddByte(f, "ClientCert", b->Buf, b->Size);
  8436. FreeBuf(b);
  8437. b = KToBuf(a->ClientK, false, NULL);
  8438. CfgAddByte(f, "ClientKey", b->Buf, b->Size);
  8439. FreeBuf(b);
  8440. }
  8441. break;
  8442. case CLIENT_AUTHTYPE_SECURE:
  8443. CfgAddStr(f, "SecurePublicCertName", a->SecurePublicCertName);
  8444. CfgAddStr(f, "SecurePrivateKeyName", a->SecurePrivateKeyName);
  8445. break;
  8446. }
  8447. }
  8448. // Write the client option
  8449. void CiWriteClientOption(FOLDER *f, CLIENT_OPTION *o)
  8450. {
  8451. BUF *b;
  8452. // Validate arguments
  8453. if (f == NULL || o == NULL)
  8454. {
  8455. return;
  8456. }
  8457. CfgAddUniStr(f, "AccountName", o->AccountName);
  8458. CfgAddStr(f, "Hostname", o->Hostname);
  8459. CfgAddInt(f, "Port", o->Port);
  8460. CfgAddInt(f, "PortUDP", o->PortUDP);
  8461. CfgAddInt(f, "ProxyType", o->ProxyType);
  8462. CfgAddStr(f, "ProxyName", o->ProxyName);
  8463. CfgAddInt(f, "ProxyPort", o->ProxyPort);
  8464. CfgAddStr(f, "ProxyUsername", o->ProxyUsername);
  8465. b = EncryptPassword(o->ProxyPassword);
  8466. CfgAddByte(f, "ProxyPassword", b->Buf, b->Size);
  8467. FreeBuf(b);
  8468. CfgAddInt(f, "NumRetry", o->NumRetry);
  8469. CfgAddInt(f, "RetryInterval", o->RetryInterval);
  8470. CfgAddStr(f, "HubName", o->HubName);
  8471. CfgAddInt(f, "MaxConnection", o->MaxConnection);
  8472. CfgAddBool(f, "UseEncrypt", o->UseEncrypt);
  8473. CfgAddBool(f, "UseCompress", o->UseCompress);
  8474. CfgAddBool(f, "HalfConnection", o->HalfConnection);
  8475. CfgAddBool(f, "NoRoutingTracking", o->NoRoutingTracking);
  8476. CfgAddStr(f, "DeviceName", o->DeviceName);
  8477. CfgAddInt(f, "AdditionalConnectionInterval", o->AdditionalConnectionInterval);
  8478. CfgAddBool(f, "HideStatusWindow", o->HideStatusWindow);
  8479. CfgAddBool(f, "HideNicInfoWindow", o->HideNicInfoWindow);
  8480. CfgAddInt(f, "ConnectionDisconnectSpan", o->ConnectionDisconnectSpan);
  8481. CfgAddBool(f, "RequireMonitorMode", o->RequireMonitorMode);
  8482. CfgAddBool(f, "RequireBridgeRoutingMode", o->RequireBridgeRoutingMode);
  8483. CfgAddBool(f, "DisableQoS", o->DisableQoS);
  8484. CfgAddBool(f, "NoTls1", o->NoTls1);
  8485. CfgAddBool(f, "NoUdpAcceleration", o->NoUdpAcceleration);
  8486. if (o->FromAdminPack)
  8487. {
  8488. CfgAddBool(f, "FromAdminPack", o->FromAdminPack);
  8489. }
  8490. if (IsZero(o->HostUniqueKey, SHA1_SIZE) == false)
  8491. {
  8492. BUF *b = MemToBuf(o->HostUniqueKey, SHA1_SIZE);
  8493. CfgAddBuf(f, "HostUniqueKey", b);
  8494. FreeBuf(b);
  8495. }
  8496. }
  8497. // Decrypt the password
  8498. char *DecryptPassword(BUF *b)
  8499. {
  8500. char *str;
  8501. char *key = "EncryptPassword";
  8502. CRYPT *c;
  8503. // Validate arguments
  8504. if (b == NULL)
  8505. {
  8506. return CopyStr("");
  8507. }
  8508. str = ZeroMalloc(b->Size + 1);
  8509. c = NewCrypt(key, sizeof(key));
  8510. Encrypt(c, str, b->Buf, b->Size);
  8511. FreeCrypt(c);
  8512. str[b->Size] = 0;
  8513. return str;
  8514. }
  8515. char *DecryptPassword2(BUF *b)
  8516. {
  8517. char *str;
  8518. char *key = "EncryptPassword2";
  8519. CRYPT *c;
  8520. // Validate arguments
  8521. if (b == NULL)
  8522. {
  8523. return CopyStr("");
  8524. }
  8525. str = ZeroMalloc(b->Size + 1);
  8526. c = NewCrypt(key, StrLen(key));
  8527. Encrypt(c, str, b->Buf, b->Size);
  8528. FreeCrypt(c);
  8529. str[b->Size] = 0;
  8530. return str;
  8531. }
  8532. // Encrypt the password
  8533. BUF *EncryptPassword(char *password)
  8534. {
  8535. UCHAR *tmp;
  8536. UINT size;
  8537. char *key = "EncryptPassword";
  8538. CRYPT *c;
  8539. BUF *b;
  8540. // Validate arguments
  8541. if (password == NULL)
  8542. {
  8543. password = "";
  8544. }
  8545. size = StrLen(password) + 1;
  8546. tmp = ZeroMalloc(size);
  8547. c = NewCrypt(key, sizeof(key));
  8548. Encrypt(c, tmp, password, size - 1);
  8549. FreeCrypt(c);
  8550. b = NewBuf();
  8551. WriteBuf(b, tmp, size - 1);
  8552. SeekBuf(b, 0, 0);
  8553. Free(tmp);
  8554. return b;
  8555. }
  8556. BUF *EncryptPassword2(char *password)
  8557. {
  8558. UCHAR *tmp;
  8559. UINT size;
  8560. char *key = "EncryptPassword2";
  8561. CRYPT *c;
  8562. BUF *b;
  8563. // Validate arguments
  8564. if (password == NULL)
  8565. {
  8566. password = "";
  8567. }
  8568. size = StrLen(password) + 1;
  8569. tmp = ZeroMalloc(size);
  8570. c = NewCrypt(key, StrLen(key));
  8571. Encrypt(c, tmp, password, size - 1);
  8572. FreeCrypt(c);
  8573. b = NewBuf();
  8574. WriteBuf(b, tmp, size - 1);
  8575. SeekBuf(b, 0, 0);
  8576. Free(tmp);
  8577. return b;
  8578. }
  8579. // Write the account data
  8580. void CiWriteAccountData(FOLDER *f, ACCOUNT *a)
  8581. {
  8582. // Validate arguments
  8583. if (f == NULL || a == NULL)
  8584. {
  8585. return;
  8586. }
  8587. // Client Option
  8588. CiWriteClientOption(CfgCreateFolder(f, "ClientOption"), a->ClientOption);
  8589. // Client authentication data
  8590. CiWriteClientAuth(CfgCreateFolder(f, "ClientAuth"), a->ClientAuth);
  8591. // Startup account
  8592. CfgAddBool(f, "StartupAccount", a->StartupAccount);
  8593. // Server certificate check flag
  8594. CfgAddBool(f, "CheckServerCert", a->CheckServerCert);
  8595. // Date and time
  8596. CfgAddInt64(f, "CreateDateTime", a->CreateDateTime);
  8597. CfgAddInt64(f, "UpdateDateTime", a->UpdateDateTime);
  8598. CfgAddInt64(f, "LastConnectDateTime", a->LastConnectDateTime);
  8599. // Server certificate body
  8600. if (a->ServerCert != NULL)
  8601. {
  8602. BUF *b = XToBuf(a->ServerCert, false);
  8603. if (b != NULL)
  8604. {
  8605. CfgAddBuf(f, "ServerCert", b);
  8606. FreeBuf(b);
  8607. }
  8608. }
  8609. // Shortcut Key
  8610. if (IsZero(a->ShortcutKey, SHA1_SIZE) == false)
  8611. {
  8612. char tmp[64];
  8613. BinToStr(tmp, sizeof(tmp), a->ShortcutKey, SHA1_SIZE);
  8614. CfgAddStr(f, "ShortcutKey", tmp);
  8615. }
  8616. }
  8617. // Write the account database
  8618. void CiWriteAccountDatabase(CLIENT *c, FOLDER *f)
  8619. {
  8620. char name[MAX_SIZE];
  8621. // Validate arguments
  8622. if (c == NULL || f == NULL)
  8623. {
  8624. return;
  8625. }
  8626. LockList(c->AccountList);
  8627. {
  8628. UINT i;
  8629. for (i = 0;i < LIST_NUM(c->AccountList);i++)
  8630. {
  8631. ACCOUNT *a = LIST_DATA(c->AccountList, i);
  8632. {
  8633. Format(name, sizeof(name), "Account%u", i);
  8634. Lock(a->lock);
  8635. {
  8636. CiWriteAccountData(CfgCreateFolder(f, name), a);
  8637. }
  8638. Unlock(a->lock);
  8639. }
  8640. }
  8641. }
  8642. UnlockList(c->AccountList);
  8643. }
  8644. // Write the CA certificate
  8645. void CiWriteCACert(CLIENT *c, FOLDER *f, X *x)
  8646. {
  8647. BUF *b;
  8648. // Validate arguments
  8649. if (c == NULL || f == NULL || x == NULL)
  8650. {
  8651. return;
  8652. }
  8653. b = XToBuf(x, false);
  8654. CfgAddBuf(f, "X509", b);
  8655. FreeBuf(b);
  8656. }
  8657. // Write a VLAN
  8658. void CiWriteVLan(CLIENT *c, FOLDER *f, UNIX_VLAN *v)
  8659. {
  8660. char tmp[MAX_SIZE];
  8661. // Validate arguments
  8662. if (c == NULL || f == NULL || v == NULL)
  8663. {
  8664. return;
  8665. }
  8666. MacToStr(tmp, sizeof(tmp), v->MacAddress);
  8667. CfgAddStr(f, "MacAddress", tmp);
  8668. CfgAddBool(f, "Enabled", v->Enabled);
  8669. }
  8670. // Write a VLAN list
  8671. void CiWriteVLanList(CLIENT *c, FOLDER *f)
  8672. {
  8673. // Validate arguments
  8674. if (c == NULL || f == NULL)
  8675. {
  8676. return;
  8677. }
  8678. LockList(c->UnixVLanList);
  8679. {
  8680. UINT i;
  8681. for (i = 0;i < LIST_NUM(c->UnixVLanList);i++)
  8682. {
  8683. UNIX_VLAN *v = LIST_DATA(c->UnixVLanList, i);
  8684. CiWriteVLan(c, CfgCreateFolder(f, v->Name), v);
  8685. }
  8686. }
  8687. UnlockList(c->UnixVLanList);
  8688. }
  8689. // Write the CA list
  8690. void CiWriteCAList(CLIENT *c, FOLDER *f)
  8691. {
  8692. CEDAR *cedar;
  8693. // Validate arguments
  8694. if (c == NULL || f == NULL)
  8695. {
  8696. return;
  8697. }
  8698. cedar = c->Cedar;
  8699. LockList(cedar->CaList);
  8700. {
  8701. UINT i;
  8702. for (i = 0;i < LIST_NUM(cedar->CaList);i++)
  8703. {
  8704. char tmp[MAX_SIZE];
  8705. X *x = LIST_DATA(cedar->CaList, i);
  8706. Format(tmp, sizeof(tmp), "Certificate%u", i);
  8707. CiWriteCACert(c, CfgCreateFolder(f, tmp), x);
  8708. }
  8709. }
  8710. UnlockList(cedar->CaList);
  8711. }
  8712. // Write the current settings to ROOT
  8713. void CiWriteSettingToCfg(CLIENT *c, FOLDER *root)
  8714. {
  8715. FOLDER *cc;
  8716. FOLDER *account_database;
  8717. FOLDER *ca;
  8718. FOLDER *vlan;
  8719. FOLDER *cmsetting;
  8720. FOLDER *proxy;
  8721. // Validate arguments
  8722. if (c == NULL || root == NULL)
  8723. {
  8724. return;
  8725. }
  8726. cmsetting = CfgCreateFolder(root, "ClientManagerSetting");
  8727. // CLIENT_CONFIG
  8728. cc = CfgCreateFolder(root, "Config");
  8729. CiWriteClientConfig(cc, &c->Config);
  8730. // Eraser
  8731. CfgAddInt64(cc, "AutoDeleteCheckDiskFreeSpaceMin", c->Eraser->MinFreeSpace);
  8732. // Account Database
  8733. account_database = CfgCreateFolder(root, "AccountDatabase");
  8734. CiWriteAccountDatabase(c, account_database);
  8735. // Proxy
  8736. proxy = CfgCreateFolder(root, "CommonProxySetting");
  8737. if (proxy != NULL)
  8738. {
  8739. INTERNET_SETTING *t = &c->CommonProxySetting;
  8740. BUF *pw;
  8741. CfgAddInt(proxy, "ProxyType", t->ProxyType);
  8742. CfgAddStr(proxy, "ProxyHostName", t->ProxyHostName);
  8743. CfgAddInt(proxy, "ProxyPort", t->ProxyPort);
  8744. CfgAddStr(proxy, "ProxyUsername", t->ProxyUsername);
  8745. if (IsEmptyStr(t->ProxyPassword) == false)
  8746. {
  8747. pw = EncryptPassword(t->ProxyPassword);
  8748. CfgAddBuf(proxy, "ProxyPassword", pw);
  8749. FreeBuf(pw);
  8750. }
  8751. }
  8752. // CA
  8753. ca = CfgCreateFolder(root, "RootCA");
  8754. CiWriteCAList(c, ca);
  8755. // VLAN
  8756. if (OS_IS_UNIX(GetOsInfo()->OsType)
  8757. #ifdef NO_VLAN
  8758. && GetOsInfo()->OsType != OSTYPE_MACOS_X
  8759. #endif // NO_VLAN
  8760. )
  8761. {
  8762. vlan = CfgCreateFolder(root, "UnixVLan");
  8763. CiWriteVLanList(c, vlan);
  8764. }
  8765. // Password
  8766. CfgAddByte(root, "EncryptedPassword", c->EncryptedPassword, SHA1_SIZE);
  8767. CfgAddBool(root, "PasswordRemoteOnly", c->PasswordRemoteOnly);
  8768. // UseSecureDeviceId
  8769. CfgAddInt(root, "UseSecureDeviceId", c->UseSecureDeviceId);
  8770. // DontSavePassword
  8771. CfgAddBool(root, "DontSavePassword", c->DontSavePassword);
  8772. // UserAgent
  8773. if (c->Cedar != NULL)
  8774. {
  8775. CfgAddStr(root, "UserAgent", c->Cedar->HttpUserAgent);
  8776. }
  8777. if (cmsetting != NULL)
  8778. {
  8779. CM_SETTING *s = c->CmSetting;
  8780. CfgAddBool(cmsetting, "EasyMode", s->EasyMode);
  8781. CfgAddBool(cmsetting, "LockMode", s->LockMode);
  8782. if (IsZero(s->HashedPassword, sizeof(s->HashedPassword)) == false)
  8783. {
  8784. CfgAddByte(cmsetting, "HashedPassword", s->HashedPassword, sizeof(s->HashedPassword));
  8785. }
  8786. }
  8787. }
  8788. // Create the inner VPN Server
  8789. SERVER *CiNewInnerVPNServer(CLIENT *c)
  8790. {
  8791. SERVER *s = NULL;
  8792. // Validate arguments
  8793. if (c == NULL)
  8794. {
  8795. return NULL;
  8796. }
  8797. SetNatTLowPriority();
  8798. s = SiNewServerEx(false, true);
  8799. return s;
  8800. }
  8801. // Stop the inner VPN Server
  8802. void CiFreeInnerVPNServer(CLIENT *c, SERVER *s)
  8803. {
  8804. // Validate arguments
  8805. if (c == NULL || s == NULL)
  8806. {
  8807. return;
  8808. }
  8809. SiReleaseServer(s);
  8810. }
  8811. // Apply settings of Inner VPN Server
  8812. void CiApplyInnerVPNServerConfig(CLIENT *c)
  8813. {
  8814. }
  8815. // Write to the configuration file
  8816. void CiSaveConfigurationFile(CLIENT *c)
  8817. {
  8818. FOLDER *root;
  8819. // Validate arguments
  8820. if (c == NULL)
  8821. {
  8822. return;
  8823. }
  8824. // Do not save the configuration file
  8825. if(c->NoSaveConfig)
  8826. {
  8827. return;
  8828. }
  8829. root = CfgCreateFolder(NULL, TAG_ROOT);
  8830. CiWriteSettingToCfg(c, root);
  8831. SaveCfgRw(c->CfgRw, root);
  8832. CfgDeleteFolder(root);
  8833. }
  8834. // Set the CM_SETTING
  8835. bool CtSetCmSetting(CLIENT *c, CM_SETTING *s)
  8836. {
  8837. // Validate arguments
  8838. if (c == NULL || s == NULL)
  8839. {
  8840. return false;
  8841. }
  8842. Copy(c->CmSetting, s, sizeof(CM_SETTING));
  8843. CiSaveConfigurationFile(c);
  8844. return true;
  8845. }
  8846. // Get the CM_SETTING
  8847. bool CtGetCmSetting(CLIENT *c, CM_SETTING *s)
  8848. {
  8849. // Validate arguments
  8850. if (c == NULL || s == NULL)
  8851. {
  8852. return false;
  8853. }
  8854. Copy(s, c->CmSetting, sizeof(CM_SETTING));
  8855. return true;
  8856. }
  8857. // Get the client version
  8858. bool CtGetClientVersion(CLIENT *c, RPC_CLIENT_VERSION *ver)
  8859. {
  8860. // Validate arguments
  8861. if (ver == NULL)
  8862. {
  8863. return false;
  8864. }
  8865. Zero(ver, sizeof(RPC_CLIENT_VERSION));
  8866. StrCpy(ver->ClientProductName, sizeof(ver->ClientProductName), CEDAR_CLIENT_STR);
  8867. StrCpy(ver->ClientVersionString, sizeof(ver->ClientVersionString), c->Cedar->VerString);
  8868. StrCpy(ver->ClientBuildInfoString, sizeof(ver->ClientBuildInfoString), c->Cedar->BuildInfo);
  8869. ver->ClientVerInt = c->Cedar->Version;
  8870. ver->ClientBuildInt = c->Cedar->Build;
  8871. #ifdef OS_WIN32
  8872. ver->ProcessId = MsGetProcessId();
  8873. ver->IsVLanNameRegulated = MsIsInfCatalogRequired();
  8874. #endif // OS_WIN32
  8875. ver->OsType = GetOsInfo()->OsType;
  8876. return true;
  8877. }
  8878. // Creating a Client object
  8879. CLIENT *CiNewClient()
  8880. {
  8881. CLIENT *c = ZeroMalloc(sizeof(CLIENT));
  8882. // StartCedarLog();
  8883. if (ci_active_sessions_lock == NULL)
  8884. {
  8885. ci_active_sessions_lock = NewLock();
  8886. ci_num_active_sessions = 0;
  8887. }
  8888. c->CmSetting = ZeroMalloc(sizeof(CM_SETTING));
  8889. c->SockList = NewSockList();
  8890. c->lock = NewLock();
  8891. c->lockForConnect = NewLock();
  8892. c->ref = NewRef();
  8893. c->Cedar = NewCedar(NULL, NULL);
  8894. c->Cedar->Client = c;
  8895. c->NotifyCancelList = NewList(NULL);
  8896. Hash(c->EncryptedPassword, "", 0, true);
  8897. #ifdef OS_WIN32
  8898. c->GlobalPulse = MsOpenOrCreateGlobalPulse(CLIENT_GLOBAL_PULSE_NAME);
  8899. #endif // OS_WIN32
  8900. if (c->GlobalPulse != NULL)
  8901. {
  8902. c->PulseRecvThread = NewThread(CiPulseRecvThread, c);
  8903. }
  8904. CiLoadIniSettings(c);
  8905. // Log Settings
  8906. if(c->NoSaveLog == false)
  8907. {
  8908. MakeDir(CLIENT_LOG_DIR_NAME);
  8909. c->Logger = NewLog(CLIENT_LOG_DIR_NAME, CLIENT_LOG_PREFIX, LOG_SWITCH_DAY);
  8910. }
  8911. CLog(c, "L_LINE");
  8912. CLog(c, "LC_START_2", CEDAR_CLIENT_STR, c->Cedar->VerString);
  8913. CLog(c, "LC_START_3", c->Cedar->BuildInfo);
  8914. CLog(c, "LC_START_1");
  8915. #ifdef OS_WIN32
  8916. {
  8917. // Initialize the Win32 UI
  8918. wchar_t tmp[MAX_SIZE];
  8919. StrToUni(tmp, sizeof(tmp), CEDAR_CLIENT_STR);
  8920. InitWinUi(tmp, _SS("DEFAULT_FONT"), _II("DEFAULT_FONT_SIZE"));
  8921. }
  8922. #endif // OS_WIN32
  8923. // Initialize the settings
  8924. CiInitConfiguration(c);
  8925. // Raise the priority
  8926. OSSetHighPriority();
  8927. #ifdef OS_WIN32
  8928. // For Win9x, release the DHCP address of all the virtual LAN card
  8929. if (MsIsNt() == false)
  8930. {
  8931. Win32ReleaseAllDhcp9x(true);
  8932. }
  8933. #endif // OS_WIN32
  8934. CiChangeAllVLanMacAddressIfMachineChanged(c);
  8935. CiChangeAllVLanMacAddressIfCleared(c);
  8936. // Initialize the internal VPN server
  8937. CiApplyInnerVPNServerConfig(c);
  8938. return c;
  8939. }
  8940. // Examine whether two proxy server settings equal
  8941. bool CompareInternetSetting(INTERNET_SETTING *s1, INTERNET_SETTING *s2)
  8942. {
  8943. // Validate arguments
  8944. if (s1 == NULL || s2 == NULL)
  8945. {
  8946. return false;
  8947. }
  8948. if (s1->ProxyType != s2->ProxyType)
  8949. {
  8950. return false;
  8951. }
  8952. if (s1->ProxyType == PROXY_DIRECT)
  8953. {
  8954. return true;
  8955. }
  8956. if (s1->ProxyPort != s2->ProxyPort)
  8957. {
  8958. return false;
  8959. }
  8960. if (StrCmp(s1->ProxyHostName, s2->ProxyHostName) != 0)
  8961. {
  8962. return false;
  8963. }
  8964. if (StrCmp(s1->ProxyUsername, s2->ProxyUsername) != 0)
  8965. {
  8966. return false;
  8967. }
  8968. if (StrCmp(s1->ProxyPassword, s2->ProxyPassword) != 0)
  8969. {
  8970. return false;
  8971. }
  8972. return true;
  8973. }
  8974. // Send a global pulse
  8975. void CiSendGlobalPulse(CLIENT *c)
  8976. {
  8977. // Validate arguments
  8978. if (c == NULL)
  8979. {
  8980. return;
  8981. }
  8982. #ifdef OS_WIN32
  8983. MsSendGlobalPulse(c->GlobalPulse);
  8984. #endif // OS_WIN32
  8985. }
  8986. // Pulse reception thread
  8987. void CiPulseRecvThread(THREAD *thread, void *param)
  8988. {
  8989. #ifdef OS_WIN32
  8990. CLIENT *c = (CLIENT *)param;
  8991. if (c == NULL)
  8992. {
  8993. return;
  8994. }
  8995. while (true)
  8996. {
  8997. if (c->HaltPulseThread)
  8998. {
  8999. break;
  9000. }
  9001. MsWaitForGlobalPulse(c->GlobalPulse, INFINITE);
  9002. if (c->HaltPulseThread)
  9003. {
  9004. break;
  9005. }
  9006. CiNotifyInternal(c);
  9007. }
  9008. #endif // OS_WIN32
  9009. }
  9010. // Clean-up the client
  9011. void CiCleanupClient(CLIENT *c)
  9012. {
  9013. // Validate arguments
  9014. if (c == NULL)
  9015. {
  9016. return;
  9017. }
  9018. // Release the settings
  9019. CiFreeConfiguration(c);
  9020. #ifdef OS_WIN32
  9021. // Release the Win32 UI
  9022. FreeWinUi();
  9023. #endif // OS_WIN32
  9024. CLog(c, "LC_END");
  9025. CLog(c, "L_LINE");
  9026. FreeEraser(c->Eraser);
  9027. FreeLog(c->Logger);
  9028. c->Logger = NULL;
  9029. ReleaseCedar(c->Cedar);
  9030. DeleteLock(c->lockForConnect);
  9031. DeleteLock(c->lock);
  9032. c->HaltPulseThread = true;
  9033. if (c->GlobalPulse != NULL)
  9034. {
  9035. #ifdef OS_WIN32
  9036. MsSendGlobalPulse(c->GlobalPulse);
  9037. #endif // OS_WIN32
  9038. }
  9039. if (c->PulseRecvThread != NULL)
  9040. {
  9041. WaitThread(c->PulseRecvThread, INFINITE);
  9042. ReleaseThread(c->PulseRecvThread);
  9043. }
  9044. if (c->GlobalPulse != NULL)
  9045. {
  9046. #ifdef OS_WIN32
  9047. MsCloseGlobalPulse(c->GlobalPulse);
  9048. #endif // OS_WIN32
  9049. }
  9050. ReleaseList(c->NotifyCancelList);
  9051. FreeSockList(c->SockList);
  9052. Free(c->CmSetting);
  9053. Free(c);
  9054. #ifdef OS_WIN32
  9055. // For Win9x, release the DHCP address of all the virtual LAN card
  9056. if (MsIsNt() == false)
  9057. {
  9058. Win32ReleaseAllDhcp9x(true);
  9059. }
  9060. #endif // OS_WIN32
  9061. StopCedarLog();
  9062. if (ci_active_sessions_lock != NULL)
  9063. {
  9064. DeleteLock(ci_active_sessions_lock);
  9065. ci_active_sessions_lock = NULL;
  9066. ci_num_active_sessions = 0;
  9067. }
  9068. }
  9069. // Increment of the number of active sessions
  9070. void CiIncrementNumActiveSessions()
  9071. {
  9072. Lock(ci_active_sessions_lock);
  9073. {
  9074. ci_num_active_sessions++;
  9075. }
  9076. Unlock(ci_active_sessions_lock);
  9077. }
  9078. // Decrement of the number of active sessions
  9079. void CiDecrementNumActiveSessions()
  9080. {
  9081. Lock(ci_active_sessions_lock);
  9082. {
  9083. if (ci_num_active_sessions >= 1)
  9084. {
  9085. ci_num_active_sessions--;
  9086. }
  9087. }
  9088. Unlock(ci_active_sessions_lock);
  9089. }
  9090. // Get the number of active sessions
  9091. UINT CiGetNumActiveSessions()
  9092. {
  9093. UINT ret;
  9094. Lock(ci_active_sessions_lock);
  9095. {
  9096. ret = ci_num_active_sessions;
  9097. }
  9098. Unlock(ci_active_sessions_lock);
  9099. return ret;
  9100. }
  9101. // Release the client
  9102. void CtReleaseClient(CLIENT *c)
  9103. {
  9104. // Validate arguments
  9105. if (c == NULL)
  9106. {
  9107. return;
  9108. }
  9109. if (Release(c->ref) == 0)
  9110. {
  9111. CiCleanupClient(c);
  9112. }
  9113. }
  9114. // Start the operation of the client program
  9115. void CtStartClient()
  9116. {
  9117. UINT i;
  9118. LIST *o;
  9119. if (client != NULL)
  9120. {
  9121. // It is already in running
  9122. return;
  9123. }
  9124. // OS check
  9125. CiCheckOs();
  9126. #ifdef OS_WIN32
  9127. RegistWindowsFirewallAll();
  9128. #endif
  9129. // Creating a client
  9130. client = CiNewClient();
  9131. // Start the Keep
  9132. CiInitKeep(client);
  9133. // Start the RPC server
  9134. CiStartRpcServer(client);
  9135. // Start the Saver
  9136. CiInitSaver(client);
  9137. // Start the startup connection
  9138. o = NewListFast(NULL);
  9139. LockList(client->AccountList);
  9140. {
  9141. for (i = 0;i < LIST_NUM(client->AccountList);i++)
  9142. {
  9143. ACCOUNT *a = LIST_DATA(client->AccountList, i);
  9144. Lock(a->lock);
  9145. {
  9146. if (a->StartupAccount)
  9147. {
  9148. Add(o, CopyUniStr(a->ClientOption->AccountName));
  9149. }
  9150. }
  9151. Unlock(a->lock);
  9152. }
  9153. }
  9154. UnlockList(client->AccountList);
  9155. for (i = 0;i < LIST_NUM(o);i++)
  9156. {
  9157. wchar_t *s = LIST_DATA(o, i);
  9158. RPC_CLIENT_CONNECT c;
  9159. Zero(&c, sizeof(c));
  9160. UniStrCpy(c.AccountName, sizeof(c.AccountName), s);
  9161. CtConnect(client, &c);
  9162. Free(s);
  9163. }
  9164. ReleaseList(o);
  9165. }
  9166. // Stop the operation of the client program
  9167. void CtStopClient()
  9168. {
  9169. UINT i, num;
  9170. ACCOUNT **account_list;
  9171. if (client == NULL)
  9172. {
  9173. // It is not running yet
  9174. return;
  9175. }
  9176. // Halting flag
  9177. client->Halt = true;
  9178. // Disconnect all the RPC
  9179. CiStopRpcServer(client);
  9180. // Exit the client notification service
  9181. CncExit();
  9182. // Exit the Keep
  9183. CiFreeKeep(client);
  9184. // Disconnect all accounts connected
  9185. LockList(client->AccountList);
  9186. {
  9187. num = LIST_NUM(client->AccountList);
  9188. account_list = ToArray(client->AccountList);
  9189. }
  9190. UnlockList(client->AccountList);
  9191. for (i = 0;i < num;i++)
  9192. {
  9193. ACCOUNT *a = account_list[i];
  9194. SESSION *s = NULL;
  9195. Lock(a->lock);
  9196. {
  9197. if (a->ClientSession != NULL)
  9198. {
  9199. s = a->ClientSession;
  9200. AddRef(s->ref);
  9201. }
  9202. }
  9203. Unlock(a->lock);
  9204. if (s != NULL)
  9205. {
  9206. StopSession(s);
  9207. ReleaseSession(s);
  9208. Lock(a->lock);
  9209. {
  9210. if (a->ClientSession != NULL)
  9211. {
  9212. ReleaseSession(a->ClientSession);
  9213. a->ClientSession = NULL;
  9214. }
  9215. }
  9216. Unlock(a->lock);
  9217. }
  9218. }
  9219. Free(account_list);
  9220. // Stop the Saver
  9221. CiFreeSaver(client);
  9222. // Release the client
  9223. CtReleaseClient(client);
  9224. client = NULL;
  9225. }
  9226. // OS check
  9227. void CiCheckOs()
  9228. {
  9229. // Get the OS type
  9230. OS_INFO *info = GetOsInfo();
  9231. if (OS_IS_WINDOWS(info->OsType))
  9232. {
  9233. bool ok = IS_CLIENT_SUPPORTED_OS(info->OsType);
  9234. if (ok == false)
  9235. {
  9236. Alert(
  9237. CEDAR_PRODUCT_STR " VPN Client doesn't support this Windows Operating System.\n"
  9238. CEDAR_PRODUCT_STR " VPN Client requires Windows 98, Windows Me, Windows 2000, Windows XP, Windows Server 2003 or Greater.\n\n"
  9239. "Please contact your system administrator.", CEDAR_PRODUCT_STR " VPN Client");
  9240. exit(0);
  9241. }
  9242. }
  9243. }
  9244. // Get the client object
  9245. CLIENT *CtGetClient()
  9246. {
  9247. if (client == NULL)
  9248. {
  9249. return NULL;
  9250. }
  9251. AddRef(client->ref);
  9252. return client;
  9253. }
  9254. // Client status indicator
  9255. void CiClientStatusPrinter(SESSION *s, wchar_t *status)
  9256. {
  9257. #ifdef OS_WIN32
  9258. ACCOUNT *a;
  9259. // Validate arguments
  9260. if (s == NULL || status == NULL)
  9261. {
  9262. return;
  9263. }
  9264. a = s->Account;
  9265. if (a == NULL)
  9266. {
  9267. return;
  9268. }
  9269. if (UniStrCmpi(status, L"init") == 0)
  9270. {
  9271. if (a->StatusWindow == NULL && s->Win32HideConnectWindow == false)
  9272. {
  9273. a->StatusWindow = CncStatusPrinterWindowStart(s);
  9274. }
  9275. }
  9276. else if (UniStrCmpi(status, L"free") == 0)
  9277. {
  9278. if (a->StatusWindow != NULL)
  9279. {
  9280. CncStatusPrinterWindowStop(a->StatusWindow);
  9281. a->StatusWindow = NULL;
  9282. }
  9283. }
  9284. else
  9285. {
  9286. if (a->StatusWindow != NULL)
  9287. {
  9288. CncStatusPrinterWindowPrint(a->StatusWindow, status);
  9289. }
  9290. }
  9291. #else // OS_WIN32
  9292. UniPrint(L"Status: %s\n", status);
  9293. #endif // OS_WIN32
  9294. }
  9295. // Developed by SoftEther VPN Project at University of Tsukuba in Japan.
  9296. // Department of Computer Science has dozens of overly-enthusiastic geeks.
  9297. // Join us: http://www.tsukuba.ac.jp/english/admission/