Exemplo n.º 1
0
        private void ApplyConfiguration()
        {
            try
            {
                //Only set right it is not a build in account
                if (!Equals(_tempServiceConfig.Credentials, ServiceCredentials.LocalSystem) &&
                    !Equals(_tempServiceConfig.Credentials, ServiceCredentials.LocalService) &&
                    !Equals(_tempServiceConfig.Credentials, ServiceCredentials.NetworkService) &&
                    !Equals(_tempServiceConfig.Credentials, ServiceCredentials.NoChange) &&
                    !ServiceCredentials.IsVirtualAccount(_tempServiceConfig.Credentials)) //Normally all NT SERVICE\\... service has that right, so no need to add it.
                {
                    string username = _tempServiceConfig.Credentials.Username;
                    if (string.IsNullOrWhiteSpace(username))
                    {
                        username = TextBoxUsername.Text;
                    }

                    using (LsaPolicyHandle lsaWrapper = LsaPolicyHandle.OpenPolicyHandle())
                    {
                        bool hasRightToStartAsService = lsaWrapper.EnumeratePrivileges(username).Any(x => x.Buffer == "SeServiceLogonRight");
                        if (!hasRightToStartAsService)
                        {
                            MessageBoxResult result = MessageBox.Show(_resManager.GetString("logon_as_a_service", CultureInfo.CurrentUICulture), _resManager.GetString("question", CultureInfo.CurrentUICulture), MessageBoxButton.YesNo, MessageBoxImage.Question);
                            if (result != MessageBoxResult.Yes)
                            {
                                return;
                            }

                            //Give the account the right to start as service
                            lsaWrapper.AddPrivileges(username, "SeServiceLogonRight");
                        }
                    }
                }

                if (_createNewService)
                {
                    using (ServiceControlManager scm = ServiceControlManager.Connect(Advapi32.ServiceControlManagerAccessRights.CreateService))
                    {
                        scm.CreateService(_tempServiceConfig);

                        ////When no exception has been throwed show up a message (no longer)
                        //MessageBox.Show(
                        //    _resManager.GetString("the_service_installation_was_successful", CultureInfo.CurrentUICulture),
                        //    _resManager.GetString("success", CultureInfo.CurrentUICulture), MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }
                else
                {
                    using (ServiceControlManager scm = ServiceControlManager.Connect(Advapi32.ServiceControlManagerAccessRights.Connect))
                    {
                        using (ServiceHandle serviceHandle = scm.OpenService(_tempServiceConfig.ServiceName, Advapi32.ServiceAccessRights.AllAccess))
                        {
                            serviceHandle.ChangeConfig(_tempServiceConfig);
                        }
                    }
                }


                //Save settings in registry after no error is occured
                RegistryManagement.SaveInRegistry(_tempServiceConfig);

                DialogResult = true;
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    _resManager.GetString("the_service_installation_was_unsuccessful",
                                          CultureInfo.CurrentUICulture) + "\n" + ex.Message, "Error", MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
        }
Exemplo n.º 2
0
        private void LoadServiceInfos()
        {
            #region GeneralTab

            //Set to readonly when it has already a service name
            if (!string.IsNullOrWhiteSpace(_tempServiceConfig.ServiceName))
            {
                TextBoxServiceName.Text = _tempServiceConfig.ServiceName;
            }

            if (!_createNewService)
            {
                TextBoxServiceName.IsReadOnly  = true;
                TextBoxServiceName.Foreground  = Brushes.Gray;
                TextBoxServiceName.BorderBrush = Brushes.LightGray;
            }

            TextBoxDisplayName.Text = _tempServiceConfig.DisplayName;

            if (!string.IsNullOrWhiteSpace(_tempServiceConfig.BinaryPath))
            {
                TextBoxFilePath.Text = _tempServiceConfig.BinaryPath;
            }

            TextBoxParam.Text       = _tempServiceConfig.Arguments;
            TextBoxDescription.Text = _tempServiceConfig.Description;

            //StartType
            switch (_tempServiceConfig.StartType)
            {
            case Advapi32.ServiceStartType.AutoStart:
                ComboBoxStartType.SelectedIndex = _tempServiceConfig.DelayedStart ? 1 : 0;
                break;

            case Advapi32.ServiceStartType.StartOnDemand:
                ComboBoxStartType.SelectedIndex = 2;
                break;

            case Advapi32.ServiceStartType.Disabled:
                ComboBoxStartType.SelectedIndex = 3;
                break;

            default:
                ComboBoxStartType.SelectedIndex = 0;
                break;
            }

            #endregion

            #region CustomUser

            if (Equals(_tempServiceConfig.Credentials, ServiceCredentials.LocalSystem))
            {
                TextBoxUsername.Clear();
                TextBoxPassword.Clear();
                CheckBoxUseLocalSystem.IsChecked    = true;
                CheckBoxUseVirtualAccount.IsChecked = false;
            }
            else if (ServiceCredentials.IsVirtualAccount(_tempServiceConfig.Credentials))
            {
                CheckBoxUseVirtualAccount.IsChecked = true;
                CheckBoxUseLocalSystem.IsChecked    = false;
            }
            else
            {
                TextBoxUsername.Text                = _tempServiceConfig.Credentials.Username;
                TextBoxPassword.Password            = _createNewService ? string.Empty : PLACEHOLDER_PASSWORD;
                CheckBoxUseLocalSystem.IsChecked    = false;
                CheckBoxUseVirtualAccount.IsChecked = false;
            }

            #endregion

            #region AdvancedTab


            TextBoxMaxRestarts.Text         = _tempServiceConfig.ProcessMaxRestarts.ToString();
            TextBoxProcessTimeoutTime.Text  = _tempServiceConfig.ProcessTimeoutTime.ToString();
            TextBoxProcessRestartDelay.Text = _tempServiceConfig.ProcessRestartDelay.ToString();
            TextBoxCounterResetTime.Text    = _tempServiceConfig.CounterResetTime.ToString();
            TextBoxLoadOrderGroup.Text      = _tempServiceConfig.LoadOrderGroup;

            //Process Priority
            switch (_tempServiceConfig.ProcessPriority)
            {
            case ProcessPriorityClass.Idle:
                ComboBoxProcessPriority.SelectedIndex = 0;
                break;

            case ProcessPriorityClass.BelowNormal:
                ComboBoxProcessPriority.SelectedIndex = 1;
                break;

            case ProcessPriorityClass.Normal:
                ComboBoxProcessPriority.SelectedIndex = 2;
                break;

            case ProcessPriorityClass.AboveNormal:
                ComboBoxProcessPriority.SelectedIndex = 3;
                break;

            case ProcessPriorityClass.High:
                ComboBoxProcessPriority.SelectedIndex = 4;
                break;

            case ProcessPriorityClass.RealTime:
                ComboBoxProcessPriority.SelectedIndex = 5;
                break;

            default:
                ComboBoxProcessPriority.SelectedIndex = 2;
                break;
            }

            CheckBoxIsConsoleApp.IsChecked    = _tempServiceConfig.IsConsoleApplication;
            RadioButtonUseCtrlC.IsChecked     = _tempServiceConfig.UseCtrlC;
            RadioButtonUseCtrlBreak.IsChecked = !_tempServiceConfig.UseCtrlC;


            //Hide check box interact with desktop on not supported systems (windows 10 1803+)
            if (!DaemonMasterUtils.IsSupportedWindows10VersionForIwd)
            {
                CheckBoxInteractDesk.IsChecked = false;
                CheckBoxInteractDesk.IsEnabled = false;
            }
            else
            {
                CheckBoxInteractDesk.IsChecked = _tempServiceConfig.CanInteractWithDesktop;
            }

            CheckBoxUseEventLog.IsChecked = _tempServiceConfig.UseEventLog;

            #endregion

            #region Dependency Listboxes

            #region DependOnService

            //Load Data into _dependOnServiceObservableCollection
            _dependOnServiceObservableCollection = new ObservableCollection <ServiceInfo>();
            foreach (string dep in _tempServiceConfig.DependOnService)
            {
                var serviceInfo = new ServiceInfo
                {
                    ServiceName = dep
                };

                //Get display name
                using (var serviceController = new ServiceController(dep))
                {
                    serviceInfo.DisplayName = serviceController.DisplayName;
                }

                _dependOnServiceObservableCollection.Add(serviceInfo);
            }

            //Sort the list in alphabetical order
            ICollectionView collectionView1 = CollectionViewSource.GetDefaultView(_dependOnServiceObservableCollection);
            collectionView1.SortDescriptions.Add(new SortDescription("DisplayName", ListSortDirection.Ascending));
            ListBoxDependOnService.ItemsSource = collectionView1;

            #endregion

            #region AllServices

            //Load Data into _allServicesObservableCollection
            _allServicesObservableCollection = new ObservableCollection <ServiceInfo>();
            foreach (ServiceController service in ServiceController.GetServices())
            {
                var serviceInfo = new ServiceInfo
                {
                    DisplayName = service.DisplayName,
                    ServiceName = service.ServiceName
                };

                if (_dependOnServiceObservableCollection.All(x => x.ServiceName != serviceInfo.ServiceName))
                {
                    _allServicesObservableCollection.Add(serviceInfo);
                }
            }

            //Sort the list in alphabetical order
            ICollectionView collectionView2 = CollectionViewSource.GetDefaultView(_allServicesObservableCollection);
            collectionView2.SortDescriptions.Add(new SortDescription("DisplayName", ListSortDirection.Ascending));
            ListBoxAllServices.ItemsSource = collectionView2;

            #endregion

            #region AllGroups

            //Load Data into _allGroupsObservableCollection
            _allGroupsObservableCollection = new ObservableCollection <string>(RegistryManagement.GetAllServiceGroups());
            //Sort the list in alphabetical order
            ICollectionView collectionView3 = CollectionViewSource.GetDefaultView(_allGroupsObservableCollection);
            collectionView3.SortDescriptions.Add(new SortDescription());
            ListBoxAllGroups.ItemsSource = collectionView3;

            #endregion

            #region DependOnGroup

            //Load Data into _dependOnGroupObservableCollection
            _dependOnGroupObservableCollection = new ObservableCollection <string>(_tempServiceConfig.DependOnGroup);
            //Sort the list in alphabetical order
            ICollectionView collectionView4 = CollectionViewSource.GetDefaultView(_dependOnGroupObservableCollection);
            collectionView3.SortDescriptions.Add(new SortDescription());
            ListBoxDependOnGroup.ItemsSource = collectionView4;

            #endregion

            #endregion
        }
Exemplo n.º 3
0
        private static void CheckAndSetCommonArguments(ref DmServiceDefinition serviceDefinition, CommonEditInstallOptions opts)
        {
            serviceDefinition.Description            = opts.Description ?? serviceDefinition.Description;
            serviceDefinition.Arguments              = opts.Arguments ?? serviceDefinition.Arguments;
            serviceDefinition.LoadOrderGroup         = opts.LoadOrderGroup ?? serviceDefinition.LoadOrderGroup;
            serviceDefinition.CanInteractWithDesktop = opts.CanInteractWithDesktop ?? serviceDefinition.CanInteractWithDesktop;
            serviceDefinition.ProcessMaxRestarts     = opts.MaxRestarts ?? serviceDefinition.ProcessMaxRestarts;
            serviceDefinition.ProcessTimeoutTime     = opts.ProcessTimeoutTime ?? serviceDefinition.ProcessTimeoutTime;
            serviceDefinition.ProcessRestartDelay    = opts.ProcessRestartDelay ?? serviceDefinition.ProcessRestartDelay;
            serviceDefinition.CounterResetTime       = opts.CounterResetTime ?? serviceDefinition.CounterResetTime;
            serviceDefinition.IsConsoleApplication   = opts.ConsoleApplication ?? serviceDefinition.IsConsoleApplication;
            serviceDefinition.UseCtrlC    = opts.UseCtrlC ?? serviceDefinition.UseCtrlC;
            serviceDefinition.Credentials = new ServiceCredentials(opts.Username, opts?.Password?.ConvertStringToSecureString());

            if (opts.StartType != null)
            {
                switch (opts.StartType)
                {
                case 0:
                    serviceDefinition.StartType    = Advapi32.ServiceStartType.Disabled;
                    serviceDefinition.DelayedStart = false;
                    break;

                case 1:
                    serviceDefinition.StartType    = Advapi32.ServiceStartType.StartOnDemand;
                    serviceDefinition.DelayedStart = false;
                    break;

                case 2:
                    serviceDefinition.StartType    = Advapi32.ServiceStartType.AutoStart;
                    serviceDefinition.DelayedStart = false;
                    break;

                case 4:
                    serviceDefinition.StartType    = Advapi32.ServiceStartType.AutoStart;
                    serviceDefinition.DelayedStart = true;
                    break;

                default:
                    throw new ArgumentException("The StartType can only be between 0-4 (0 = Disabled / 1 = Demand start / 2 = Auto start / 4 = Delayed auto start).");
                }
            }

            if (opts.ProcessPriority != null)
            {
                switch (opts.ProcessPriority)
                {
                case -2:
                    serviceDefinition.ProcessPriority = ProcessPriorityClass.Idle;
                    break;

                case -1:
                    serviceDefinition.ProcessPriority = ProcessPriorityClass.BelowNormal;
                    break;

                case 0:
                    serviceDefinition.ProcessPriority = ProcessPriorityClass.Normal;
                    break;

                case 1:
                    serviceDefinition.ProcessPriority = ProcessPriorityClass.AboveNormal;
                    break;

                case 2:
                    serviceDefinition.ProcessPriority = ProcessPriorityClass.High;
                    break;

                case 3:
                    serviceDefinition.ProcessPriority = ProcessPriorityClass.RealTime;
                    break;

                default:
                    throw new ArgumentException("The ProcessPriority can only be between -2<->3 (-2 = Idle / -1 = Below normal / 0 = Normal / 1 = Above normal / 2 = High / 3 = Real time (not recommended to use)).");
                }
            }


            if (serviceDefinition.CanInteractWithDesktop && !DaemonMasterUtils.IsSupportedWindows10VersionForIwd)
            {
                throw new ArgumentException("CanInteractWithDesktop is not supported in this windows version.");
            }

            if (serviceDefinition.CanInteractWithDesktop && (!string.IsNullOrWhiteSpace(serviceDefinition.Credentials.Username) || serviceDefinition.Credentials.Password != null))
            {
                throw new ArgumentException("CanInteractWithDesktop is not supported with custom user.");
            }

            if ((string.IsNullOrWhiteSpace(serviceDefinition.Credentials.Username) && serviceDefinition.Credentials.Password != null) || (!string.IsNullOrWhiteSpace(serviceDefinition.Credentials.Username) && serviceDefinition.Credentials == null))
            {
                throw new ArgumentException("Password/username parameter is missing!");
            }


            //Custom user
            //Create new ServiceCredentials instance
            if (!string.IsNullOrWhiteSpace(serviceDefinition.Credentials.Username) && serviceDefinition.Credentials.Password != null && !ServiceCredentials.IsVirtualAccount(serviceDefinition.Credentials))
            {
                //Check if he has the right to start as service
                using (LsaPolicyHandle lsaWrapper = LsaPolicyHandle.OpenPolicyHandle())
                {
                    bool hasRightToStartAsService = lsaWrapper.EnumeratePrivileges(serviceDefinition.Credentials.Username).Any(x => x == "SeServiceLogonRight");
                    if (!hasRightToStartAsService)
                    {
                        Console.WriteLine("The user doesn't have the right to start as service. Do you want to give him that right? [Yes/No]");
                        switch (Console.ReadLine())
                        {
                        case "yes":
                        case "Yes":
                        case "y":
                        case "Y":
                            //Give the account the right to start as service
                            lsaWrapper.AddPrivilege(serviceDefinition.Credentials.Username, "SeServiceLogonRight");
                            break;

                        default:
                            throw new ArgumentException("Cannot create the service without that right.");
                        }
                    }
                }
            }
        }