MeshCentral/agents/modules_meshcore_min/amt-manage.min.js
2019-03-04 23:49:20 -08:00

1 line
11 KiB
JavaScript

function AmtManager(c,m,r){var E=function(K){c.SendCommand({action:"msg",type:"console",value:K})};var n=function(K){if(r){E("amt-manager: "+K)}};var g=null,h=0;var e=null,f=0;var d=null;var B=null;var A=null;var j=null;var z=this;z.state=0;z.lmsstate=0;z.onStateChange=null;z.setDebug=function(K){r=K};z.setPolicy=function(K){if(JSON.stringify(j)!=JSON.stringify(K)){j=K;z.applyPolicy()}};var D=0;z.reset=function(){++D;g=null,h=0,e=null,f=0,z.state=0,z.lmsstate=0;try{var K=require("amt-mei");g=new K();g.on("error",function(M){n("MEI error");g=null;h=-1;z.state=-1;z.onStateChange(h)});g.getVersion(function(M){if(M==null){h=-1;z.state=-1;if(z.onStateChange!=null){z.onStateChange(h)}if(D<10){setTimeout(z.reset,10000)}}else{d=M;h=2;z.state=2;D=0;if(z.onStateChange!=null){z.onStateChange(h)}z.lmsreset()}})}catch(L){n("MEI exception: "+L);g=null;h=-1;z.state=-1}};var i=null;z.getAmtInfo=function(L){if((g==null)||(h<2)){if(L!=null){L(null)}return}try{i={Flags:0};g.getProtocolVersion(function(M){if(M!=null){i.MeiVersion=M}});g.getVersion(function(M){if(M){i.Versions={};for(var N in M.Versions){i.Versions[M.Versions[N].Description]=M.Versions[N].Version}}});g.getProvisioningMode(function(M){if(M){i.ProvisioningMode=M.mode}});g.getProvisioningState(function(M){if(M){i.ProvisioningState=M.state}});g.getEHBCState(function(M){if((M!=null)&&(M.EHBC==true)){i.Flags+=1}});g.getControlMode(function(M){if(M!=null){if(M.controlMode==1){i.Flags+=2}if(M.controlMode==2){i.Flags+=4}}});g.getLanInterfaceSettings(0,function(M){if(M){i.net0=M}});g.getLanInterfaceSettings(1,function(M){if(M){i.net1=M}});g.getUuid(function(M){if((M!=null)&&(M.uuid!=null)){i.UUID=M.uuid}});g.getDnsSuffix(function(M){if(M!=null){i.dns=M}if(L!=null){L(i)}})}catch(K){if(L!=null){L(null)}return}};var p=function(N){if((N==null)||(N.Body==null)||(N.Body.MessageID==null)||(N.Body.MessageArguments==null)){return null}var K=N.Body.MessageID,L=N.Body.MessageArguments[0],M=null;switch(K){case"iAMT0050":if(L=="48"){M="Intel&reg; AMT Serial-over-LAN connected"}else{if(L=="49"){M="Intel&reg; AMT Serial-over-LAN disconnected"}}break;case"iAMT0052":if(L=="1"){M="Intel&reg; AMT KVM connected"}else{if(L=="2"){M="Intel&reg; AMT KVM disconnected"}}break;default:break}if(M!=null){c.SendCommand({action:"msg",type:"notify",value:M,tag:"general"})}};z.lmsreset=function(){var K=null,L=0;z.lmsstate=0;try{var N=require("amt-lme");L=1;z.lmsstate=1;K=new N();K.on("error",function(O){L=0;z.lmsstate=0;K=null;n("LMS error");F(1)});K.on("connect",function(){L=2;z.lmsstate=2;n("LMS connected");F(2)});K.on("notify",function(P,Q,R,O){if(O=="iAMT0052-3"){u()}else{p(P)}})}catch(M){L=-1;z.lmsstate=-1;K=null}};var F=function(K){if((g==null)||(h<2)||(d==null)){return}g.getLocalSystemAccount(function(R){if(R==null){return}var P=require("amt-wsman-duk");var Q=require("amt-wsman");var L=require("amt");B=new Q(P,"127.0.0.1",16992,R.user,R.pass,false);A=new L(B);h=3;z.state=3;if(z.onStateChange!=null){z.onStateChange(h)}z.applyPolicy();var M=null;try{for(var O in d.Versions){if(d.Versions[O].Description=="AMT"){M=parseInt(d.Versions[O].Version.split(".")[0])}}}catch(N){}if((M!=null)&&(M>=12)){n("KVM data channel setup");u("skip");z.kvmTempTimer=setInterval(function(){u()},2000);w(JSON.stringify({action:"restart",ver:1}))}})};var u=function(K){A.IPS_KVMRedirectionSettingData_DataChannelRead(z.kvmDataGetResponse,K)};var t=function(N,L,M,O,P){if((P!="skip")&&(O==200)&&(M.Body.ReturnValue==0)){var Q=null;try{Q=Buffer.from(M.Body.DataMessage,"base64").toString()}catch(K){return}if(Q!=null){z.kvmProcessData(M.Body.RealmsBitmap,M.Body.MessageId,Q)}}};var H=null;var v=function(N,M,P){var K=null;try{K=JSON.parse(P)}catch(L){}if((K!=null)&&(K.action)){if(K.action=="present"){w(JSON.stringify({action:"present",ver:1,platform:process.platform}))}if(K.action=="offer"){H={};var O=require("ILibWebRTC");H.webrtc=O.createConnection();H.webrtc.on("connected",function(){});H.webrtc.on("disconnected",function(){z.webRtcCleanUp()});H.webrtc.on("dataChannel",function(Q){H.rtcchannel=Q;H.kvm=mesh.getRemoteDesktopStream();H.kvm.pipe(H.rtcchannel,{dataTypeSkip:1,end:false});H.rtcchannel.on("end",function(){z.webRtcCleanUp()});H.rtcchannel.on("data",function(R){z.kvmCtrlData(this,R)});H.rtcchannel.pipe(H.kvm,{dataTypeSkip:1,end:false})});w(JSON.stringify({action:"answer",ver:1,sdp:H.webrtc.setOffer(K.sdp)}))}}};var s=function(L,N){if(N.length>0&&N.charCodeAt(0)!=123){if(z.fileupload!=null){N=Buffer.from(N,"base64");var T=N.readUInt32BE(0);if((T==16777216)||(T==16777217)){fs.writeSync(z.fileupload.fp,N.slice(4));L.write({action:"upload",sub:"ack",reqid:z.fileupload.reqid});if(T==16777217){fs.closeSync(z.fileupload.fp);z.fileupload=null}}}return}n("KVM Ctrl Data: "+N);try{N=JSON.parse(N)}catch(Q){n("Invalid JSON: "+N);return}if((N.path!=null)&&(process.platform!="win32")&&(N.path[0]!="/")){N.path="/"+N.path}switch(N.action){case"ping":L.write({action:"pong"});break;case"lock":if(process.platform=="win32"){var M=require("child_process");M.execFile(process.env.windir+"\\system32\\cmd.exe",["/c","RunDll32.exe user32.dll,LockWorkStation"],{type:1})}break;case"ls":var W=getDirectoryInfo(N.path);if(N.reqid!=undefined){W.reqid=N.reqid}L.write(W);break;case"mkdir":fs.mkdirSync(N.path);break;case"rm":for(var U in N.delfiles){var S=C.join(N.path,N.delfiles[U]);try{fs.unlinkSync(S)}catch(P){n(P)}}break;case"rename":try{fs.renameSync(C.join(N.path,N.oldname),C.join(N.path,N.newname))}catch(P){n(P)}break;case"download":var Y=0;if(N.sub=="start"){if(z.filedownload!=null){L.write({action:"download",sub:"cancel",id:z.filedownload.id});delete z.filedownload}z.filedownload={id:N.id,path:N.path,ptr:0};try{z.filedownload.f=fs.openSync(z.filedownload.path,"rbN")}catch(P){L.write({action:"download",sub:"cancel",id:z.filedownload.id});delete z.filedownload}if(z.filedownload){L.write({action:"download",sub:"start",id:N.id})}}else{if((z.filedownload!=null)&&(N.id==z.filedownload.id)){if(N.sub=="startack"){Y=8}else{if(N.sub=="stop"){delete z.filedownload}else{if(N.sub=="ack"){Y=1}}}}}while(Y>0){Y--;var K=Buffer.alloc(4096);var V=fs.readSync(z.filedownload.f,K,4,4092,null);z.filedownload.ptr+=V;if(V<4092){K.writeInt32BE(16777217,0);fs.closeSync(z.filedownload.f);delete z.filedownload;Y=0}else{K.writeInt32BE(16777216,0)}L.write(K.slice(0,V+4).toString("base64"))}break;case"upload":if(N.sub=="start"){if(z.fileupload!=null){fs.closeSync(z.fileupload.fp)}if(!N.path||!N.name){break}z.fileupload={reqid:N.reqid};var R=C.join(N.path,N.name);try{z.fileupload.fp=fs.openSync(R,"wbN")}catch(P){}if(z.fileupload.fp){L.write({action:"upload",sub:"start",reqid:z.fileupload.reqid})}else{z.fileupload=null;L.write({action:"upload",sub:"error",reqid:z.fileupload.reqid})}}else{if(N.sub=="cancel"){if(z.fileupload!=null){fs.closeSync(z.fileupload.fp);z.fileupload=null}}}break;case"copy":for(var U in N.names){var X=C.join(N.scpath,N.names[U]),O=C.join(N.dspath,N.names[U]);if(X!=O){try{fs.copyFileSync(X,O)}catch(P){}}}break;case"move":for(var U in N.names){var X=C.join(N.scpath,N.names[U]),O=C.join(N.dspath,N.names[U]);if(X!=O){try{fs.copyFileSync(X,O);fs.unlinkSync(X)}catch(P){}}}break;default:n("Invalid KVM command: "+N);break}};var G=function(){n("webRtcCleanUp");if(H==null){return}if(H.rtcchannel){try{H.rtcchannel.close()}catch(K){}try{H.rtcchannel.removeAllListeners("data")}catch(K){}try{H.rtcchannel.removeAllListeners("end")}catch(K){}delete H.rtcchannel}if(H.webrtc){try{H.webrtc.close()}catch(K){}try{H.webrtc.removeAllListeners("connected")}catch(K){}try{H.webrtc.removeAllListeners("disconnected")}catch(K){}try{H.webrtc.removeAllListeners("dataChannel")}catch(K){}delete H.webrtc}if(H.kvm){try{H.kvm.end()}catch(K){}delete H.kvm}H=null};var w=function(K){A.IPS_KVMRedirectionSettingData_DataChannelWrite(Buffer.from(K).toString("base64"),function(){})};var o=function(K,L){if(fs.existsSync(K)){if(L==true){fs.readdirSync(z.path.join(K,"*")).forEach(function(N,O){var M=z.path.join(K,N);if(fs.statSync(M).isDirectory()){o(M,true)}else{fs.unlinkSync(M)}})}fs.unlinkSync(K)}};var C={join:function(){var M=[];for(var K in arguments){var L=arguments[K];if(L!=null){while(L.endsWith("/")||L.endsWith("\\")){L=L.substring(0,L.length-1)}if(K!=0){while(L.startsWith("/")||L.startsWith("\\")){L=L.substring(1)}}M.push(L)}}if(M.length==0){return"/"}return M.join("/")}};function y(K){return require("MD5Stream").create().syncHash(K).toString("hex")}z.deactivateCCM=function(){g.unprovision(1,function(K){if(K==0){n("Success deactivating Intel AMT CCM.");c.SendCommand({action:"coreinfo",intelamt:{state:0,flags:0}});l=setTimeout(z.applyPolicy,8000)}else{n("Intel AMT CCM deactivation error: "+K)}})};function x(L){var N="",M="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";for(var K=0;K<L;K++){N+=M.charAt(Math.floor(Math.random()*M.length))}return N}z.activeToCCM=function(K){if((K==null)||(K=="")){K="P@0s"+x(23)}q=K;g.getLocalSystemAccount(function(O){if((O!=null)&&O.user&&O.pass){var M=require("amt-wsman-duk");var N=require("amt-wsman");var L=require("amt");B=new N(M,"127.0.0.1",16992,O.user,O.pass,false);A=new L(B);A.BatchEnum(null,["*AMT_GeneralSettings","*IPS_HostBasedSetupService"],a,K)}else{n("Unable to get $$OsAdmin password.")}})};var a=function(N,L,M,O,K){if(O!=200){n("Failed to fetch activation information, status "+O)}else{if(M.IPS_HostBasedSetupService.response.AllowedControlModes.length!=2){n("Client control mode activation not allowed")}else{N.IPS_HostBasedSetupService_Setup(2,y("admin:"+M.AMT_GeneralSettings.response.DigestRealm+":"+K).substring(0,32),null,null,null,null,b)}}};var b=function(M,K,L,N){if(N!=200){n("Failed to activate, status "+N)}else{if(L.Body.ReturnValue!=0){n("Client control mode activation failed: "+L.Body.ReturnValueStr)}else{n("Intel AMT CCM activation success.");m.Put("amtCCMPass",q);c.SendCommand({action:"coreinfo",intelamt:{state:2,flags:2,user:"admin",pass:q}})}}l=setTimeout(z.applyPolicy,8000)};z.start=function(){var K=null;try{K=JSON.parse(m.Get("amtPolicy"))}catch(L){n("Exception loading amtPolicy")}try{q=m.Get("amtCCMPass")}catch(L){}if(typeof q!="string"){q=null}z.reset()};var q,J,k,l;z.applyPolicy=function(){l=null;if((h!=3)||(typeof j!="object")||(typeof j.type!="number")||(j.type==0)){return}if((j.password!=null)&&(j.password!="")){q=j.password}z.getAmtInfo(function(M){if((j.type==1)&&(M.ProvisioningState==2)){J=null;k=null;z.deactivateCCM()}else{if((j.type==2)&&(M.ProvisioningState==0)){J=null;k=null;if((j.password==null)||(j.password=="")){q=null}z.activeToCCM(q)}else{if((j.type==2)&&(M.ProvisioningState==2)&&(q!=null)){var N=require("amt-wsman-duk");var O=require("amt-wsman");var K=require("amt");J=new O(N,"127.0.0.1",16992,"admin",q,false);k=new K(J);try{k.BatchEnum(null,["*AMT_GeneralSettings","*IPS_HostBasedSetupService"],I)}catch(L){n(L)}}else{}}}})};var I=function(M,K,L,N){if(N!=200){if(j.badpass==1){z.deactivateCCM()}}else{n("SUCCESS!")}}}module.exports=AmtManager;