Exemplo n.º 1
0
 public ModuleCommandManager(DtoClientCommandModule module)
 {
     _moduleResult         = new DtoModuleResult();
     _moduleResult.Name    = module.DisplayName;
     _moduleResult.Guid    = module.Guid;
     _moduleResult.Success = true;
     _module = module;
 }
Exemplo n.º 2
0
        private void CommandModule(EntityPolicyModules policyModule)
        {
            var clientCommandModule = new DtoClientCommandModule();
            var commandModule       = new ServiceCommandModule().GetModule(policyModule.ModuleId);

            clientCommandModule.Order            = policyModule.Order;
            clientCommandModule.Guid             = commandModule.Guid;
            clientCommandModule.Command          = commandModule.Command;
            clientCommandModule.Arguments        = commandModule.Arguments;
            clientCommandModule.DisplayName      = commandModule.Name;
            clientCommandModule.Timeout          = commandModule.Timeout;
            clientCommandModule.RedirectOutput   = commandModule.RedirectStdOut;
            clientCommandModule.RedirectError    = commandModule.RedirectStdError;
            clientCommandModule.WorkingDirectory = commandModule.WorkingDirectory;
            foreach (var successCode in commandModule.SuccessCodes.Split(','))
            {
                clientCommandModule.SuccessCodes.Add(successCode);
            }

            if (commandModule.ImpersonationId != -1)
            {
                var impersonationGuid = new ServiceImpersonationAccount().GetGuid(commandModule.ImpersonationId);
                if (!string.IsNullOrEmpty(impersonationGuid))
                {
                    clientCommandModule.RunAs = impersonationGuid;
                }
            }

            if (policyModule.ConditionId != -1)
            {
                var conditionScript = new ServiceScriptModule().GetModule(policyModule.ConditionId);
                if (conditionScript != null)
                {
                    clientCommandModule.ConditionFailedAction    = policyModule.ConditionFailedAction;
                    clientCommandModule.ConditionNextOrder       = policyModule.ConditionNextModule;
                    clientCommandModule.Condition                = new DtoClientModuleCondition();
                    clientCommandModule.Condition.Arguments      = conditionScript.Arguments;
                    clientCommandModule.Condition.DisplayName    = conditionScript.Name;
                    clientCommandModule.Condition.Guid           = conditionScript.Guid;
                    clientCommandModule.Condition.RedirectError  = conditionScript.RedirectStdError;
                    clientCommandModule.Condition.RedirectOutput = conditionScript.RedirectStdOut;
                    if (conditionScript.ImpersonationId != -1)
                    {
                        var scriptImpersonationGuid = new ServiceImpersonationAccount().GetGuid(conditionScript.ImpersonationId);
                        if (!string.IsNullOrEmpty(scriptImpersonationGuid))
                        {
                            clientCommandModule.Condition.RunAs = scriptImpersonationGuid;
                        }
                    }
                    clientCommandModule.Condition.ScriptType = conditionScript.ScriptType;
                    foreach (var successCode in conditionScript.SuccessCodes.Split(','))
                    {
                        clientCommandModule.Condition.SuccessCodes.Add(successCode);
                    }
                    clientCommandModule.Condition.Timeout          = conditionScript.Timeout;
                    clientCommandModule.Condition.WorkingDirectory = conditionScript.WorkingDirectory;
                }
            }

            var moduleFiles = new ServiceModule().GetModuleFiles(commandModule.Guid);

            foreach (var file in moduleFiles.OrderBy(x => x.FileName))
            {
                var clientFile = new DtoClientFileHash();
                clientFile.FileName = file.FileName;
                clientFile.FileHash = file.Md5Hash;
                clientCommandModule.Files.Add(clientFile);
            }

            _clientPolicy.CommandModules.Add(clientCommandModule);
        }
Exemplo n.º 3
0
        public void Run()
        {
            Logger.Info("Running Remote Access Module: ");
            if ((_policy.RemoteAccess == EnumPolicy.RemoteAccess.Enabled && !new ServiceSystemService().IsRemotelyInstalled()) ||
                _policy.RemoteAccess == EnumPolicy.RemoteAccess.ForceReinstall)
            {
                Logger.Info("Installing Remote Access Service");
                var installArgs = new ApiCall.APICall().PolicyApi.GetRemotelyInstallArgs();
                if (string.IsNullOrEmpty(installArgs))
                {
                    Logger.Error("Could Not Get Install Args");
                    return;
                }
                if (installArgs.Contains("Error"))
                {
                    Logger.Error("Could Not Get Install Args.  " + installArgs);
                    return;
                }
                var file = string.Empty;
                if (Environment.Is64BitOperatingSystem)
                {
                    file = "Remotely-Win10-x64.zip";
                }
                else
                {
                    file = "Remotely-Win10-x86.zip";
                }

                var module = new DtoClientCommandModule();
                module.Guid           = "99999999-9999-9999-9999-999999999999";
                module.Command        = Path.Combine(DtoGobalSettings.BaseCachePath, module.Guid, "Remotely_Installer.exe");
                module.DisplayName    = "Install Remotely";
                module.Timeout        = 5;
                module.RedirectError  = true;
                module.RedirectOutput = true;
                module.SuccessCodes   = new List <string>()
                {
                    "0"
                };
                module.Arguments = $"{installArgs} -path '{Path.Combine(DtoGobalSettings.BaseCachePath, module.Guid,file)}'";
                var result = new ModuleCommandManager(module).Run();
                if (!result.Success)
                {
                    Logger.Error(result.ErrorMessage);
                }

                if (!new ServiceSystemService().DisableRemotelyServiceStartup())
                {
                    Logger.Error("Could Not Change Remotely Startup Type");
                }

                var connectionInfo = new ServiceFileSystem().ReadRemotelyConnectionFile();
                if (connectionInfo != null)
                {
                    var res = new ApiCall.APICall().PolicyApi.UpdateRemoteAccessId(connectionInfo);
                    if (res != null)
                    {
                        if (!res.Success)
                        {
                            Logger.Error("Could Not Update Client Remote Access Id");
                        }
                    }
                }
                return;
            }
            if (_policy.RemoteAccess == EnumPolicy.RemoteAccess.Enabled && new ServiceSystemService().IsRemotelyInstalled())
            {
                Logger.Debug("Remote Access Already Installed. Updating Remote Access Device Id");
                var connectionInfo = new ServiceFileSystem().ReadRemotelyConnectionFile();
                if (connectionInfo != null)
                {
                    var res = new ApiCall.APICall().PolicyApi.UpdateRemoteAccessId(connectionInfo);
                    if (res != null)
                    {
                        if (!res.Success)
                        {
                            Logger.Error("Could Not Update Client Remote Access Id");
                        }
                    }
                }
                return;
            }

            if (_policy.RemoteAccess == EnumPolicy.RemoteAccess.Disabled && new ServiceSystemService().IsRemotelyInstalled())
            {
                Logger.Info("Removing Remote Access Service");
                var module = new DtoClientCommandModule();
                module.Command        = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Remotely", "Remotely_Installer.exe");
                module.Guid           = "99999999-9999-9999-9999-999999999999";
                module.DisplayName    = "Remove Remotely";
                module.Timeout        = 5;
                module.RedirectError  = true;
                module.RedirectOutput = true;
                module.SuccessCodes   = new List <string>()
                {
                    "0"
                };
                module.WorkingDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Remotely");
                module.Arguments        = "-uninstall -quiet";

                //remove remote access id from server
                var res = new ApiCall.APICall().PolicyApi.UpdateRemoteAccessId(new RemotelyConnectionInfo()
                {
                    DeviceID = "", Host = "", OrganizationID = "", ServerVerificationToken = ""
                });
                if (res != null)
                {
                    if (!res.Success)
                    {
                        Logger.Error("Could Not Remmove Client Remote Access Id");
                    }
                }

                var result = new ModuleCommandManager(module).Run();
                if (!result.Success)
                {
                    Logger.Error(result.ErrorMessage);
                }
                return;
            }
        }