From 6edc50161eec9e6f053c01ad7bbca0c8b37f2a66 Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Sun, 10 Mar 2019 11:47:03 -0700 Subject: [PATCH] More server fixes. --- agents/modules_meshcore/amt-manage.js | 20 ++++++++++++++----- agents/modules_meshcore_min/amt-manage.min.js | 2 +- meshagent.js | 4 ++-- meshcentral.js | 10 +++++----- package.json | 2 +- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/agents/modules_meshcore/amt-manage.js b/agents/modules_meshcore/amt-manage.js index 375019f6..2d8a294a 100644 --- a/agents/modules_meshcore/amt-manage.js +++ b/agents/modules_meshcore/amt-manage.js @@ -88,7 +88,7 @@ function AmtManager(agent, db, isdebug) { amtMei.getProvisioningMode(function (result) { if (result) { amtMeiTmpState.ProvisioningMode = result.mode; } }); amtMei.getProvisioningState(function (result) { if (result) { amtMeiTmpState.ProvisioningState = result.state; } }); amtMei.getEHBCState(function (result) { if ((result != null) && (result.EHBC == true)) { amtMeiTmpState.Flags += 1; } }); - amtMei.getControlMode(function (result) { if (result != null) { if (result.controlMode == 1) { amtMeiTmpState.Flags += 2; } if (result.controlMode == 2) { amtMeiTmpState.Flags += 4; } } }); + amtMei.getControlMode(function (result) { if (result != null) { if (result.controlMode == 1) { amtMeiTmpState.Flags += 2; } if (result.controlMode == 2) { amtMeiTmpState.Flags += 4; } } }); // Flag 2 = CCM, 4 = ACM //amtMei.getMACAddresses(function (result) { if (result) { amtMeiTmpState.mac = result; } }); amtMei.getLanInterfaceSettings(0, function (result) { if (result) { amtMeiTmpState.net0 = result; } }); amtMei.getLanInterfaceSettings(1, function (result) { if (result) { amtMeiTmpState.net1 = result; } }); @@ -496,7 +496,7 @@ function AmtManager(agent, db, isdebug) { if ((amtMeiState != 3) || (typeof amtpolicy != 'object') || (typeof amtpolicy.type != 'number') || (amtpolicy.type == 0)) return; if ((amtpolicy.password != null) && (amtpolicy.password != '')) { intelAmtAdminPass = amtpolicy.password; } obj.getAmtInfo(function (meinfo) { - if ((amtpolicy.type == 1) && (meinfo.ProvisioningState == 2)) { + if ((amtpolicy.type == 1) && (meinfo.ProvisioningState == 2) && ((meinfo.Flags & 2) != 0)) { // CCM Deactivation Policy. wsstack = null; amtstack = null; @@ -507,14 +507,14 @@ function AmtManager(agent, db, isdebug) { amtstack = null; if ((amtpolicy.password == null) || (amtpolicy.password == '')) { intelAmtAdminPass = null; } obj.activeToCCM(intelAmtAdminPass); - } else if ((amtpolicy.type == 2) && (meinfo.ProvisioningState == 2) && (intelAmtAdminPass != null)) { + } else if ((amtpolicy.type == 2) && (meinfo.ProvisioningState == 2) && (intelAmtAdminPass != null) && ((meinfo.Flags & 2) != 0)) { // Perform password test var transport = require('amt-wsman-duk'); var wsman = require('amt-wsman'); var amt = require('amt'); wsstack = new wsman(transport, '127.0.0.1', 16992, 'admin', intelAmtAdminPass, false); amtstack = new amt(wsstack); - try { amtstack.BatchEnum(null, ['*AMT_GeneralSettings', '*IPS_HostBasedSetupService'], wsmanPassTestResponse); } catch (ex) { debug(ex); } + try { amtstack.BatchEnum(null, ['*AMT_GeneralSettings', '*IPS_HostBasedSetupService', '*AMT_RedirectionService', '*CIM_KVMRedirectionSAP'], wsmanPassTestResponse); } catch (ex) { debug(ex); } } else { // Other possible cases... } @@ -525,8 +525,18 @@ function AmtManager(agent, db, isdebug) { if (status != 200) { if (amtpolicy.badpass == 1) { obj.deactivateCCM(); } // Something went wrong, reactivate. } else { + /* + var redir = (amtsysstate['AMT_RedirectionService'].response["ListenerEnabled"] == true); + var sol = ((amtsysstate['AMT_RedirectionService'].response["EnabledState"] & 2) != 0); + var ider = ((amtsysstate['AMT_RedirectionService'].response["EnabledState"] & 1) != 0); + var kvm = false; + if (amtsysstate['CIM_KVMRedirectionSAP'] != null) { + kvm = ((amtsysstate['CIM_KVMRedirectionSAP'].response["EnabledState"] == 6 && amtsysstate['CIM_KVMRedirectionSAP'].response["RequestedState"] == 2) || amtsysstate['CIM_KVMRedirectionSAP'].response["EnabledState"] == 2 || amtsysstate['CIM_KVMRedirectionSAP'].response["EnabledState"] == 6); + } + */ + // Success, make sure - debug('SUCCESS!'); + debug('SUCCESS!' + JSON.stringify(responses)); // TODO: Check Intel AMT Features need to be enabled & if Intel AMT CIRA needs to be setup } } diff --git a/agents/modules_meshcore_min/amt-manage.min.js b/agents/modules_meshcore_min/amt-manage.min.js index af225a80..2c0f4fbd 100644 --- a/agents/modules_meshcore_min/amt-manage.min.js +++ b/agents/modules_meshcore_min/amt-manage.min.js @@ -1 +1 @@ -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® AMT Serial-over-LAN connected"}else{if(L=="49"){M="Intel® AMT Serial-over-LAN disconnected"}}break;case"iAMT0052":if(L=="1"){M="Intel® AMT KVM connected"}else{if(L=="2"){M="Intel® 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=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