Пример #1
0
 /// <summary>
 /// Determines whether the credentials specifying a virtual account.
 /// </summary>
 /// <param name="credentials">The credentials.</param>
 /// <returns>
 ///   <c>true</c> if it is virtual account; otherwise, <c>false</c>.
 /// </returns>
 public static bool IsVirtualAccount(ServiceCredentials credentials)
 {
     return(DaemonMasterUtils.GetDomainFromUsername(credentials.Username) == "NT SERVICE" && !string.IsNullOrWhiteSpace(DaemonMasterUtils.GetLoginFromUsername(credentials.Username)) && (credentials.Password == EmptyPassword || credentials.Password == null));
 }
Пример #2
0
 /// <summary>
 /// Compare this object with a other service credential in equality.
 /// </summary>
 /// <param name="serviceCredentials"></param>
 /// <returns></returns>
 public bool Equals(ServiceCredentials serviceCredentials)
 {
     return(string.Equals(Username, serviceCredentials.Username) && Password.IsEquals(serviceCredentials.Password));
 }
Пример #3
0
        /// <summary>
        /// Changes the service configuration.
        /// </summary>
        /// <param name="displayName">The display name.</param>
        /// <param name="startType">The start type.</param>
        /// <param name="canInteractWithDesktop">if set to <c>true</c> the service can interact with the desktop.</param>
        /// <param name="loadOrderGroup">The load order group.</param>
        /// <param name="errorControl">The error control.</param>
        /// <param name="credentials">The credentials.</param>
        /// <param name="dependOnService">The services on which the service depend on.</param>
        /// <param name="dependOnGroup">The groups on which the service depend on.</param>
        /// <exception cref="System.ArgumentException">Can interact with desktop is currently not supported in your windows version.</exception>
        /// <exception cref="System.ArgumentNullException">credentials</exception>
        /// <exception cref="Win32Exception"></exception>
        public void ChangeConfig
        (
            string displayName,
            Advapi32.ServiceStartType startType,
            bool canInteractWithDesktop,
            string loadOrderGroup,
            Advapi32.ErrorControl errorControl,
            ServiceCredentials credentials,
            string[] dependOnService,
            string[] dependOnGroup
        )
        {
            var serviceType = Advapi32.ServiceType.Win32OwnProcess; //DM only supports Win32OwnProcess

            if (Equals(credentials, ServiceCredentials.LocalSystem) && canInteractWithDesktop)
            {
                serviceType |= Advapi32.ServiceType.InteractivProcess;
            }

            //The credentials can't be null
            if (credentials == null)
            {
                throw new ArgumentNullException(nameof(credentials));
            }

            IntPtr passwordHandle = IntPtr.Zero;

            try
            {
                //Only call marshal if a password is set (SecureString != null), otherwise leave IntPtr.Zero
                if (credentials.Password != null)
                {
                    passwordHandle = Marshal.SecureStringToGlobalAllocUnicode(credentials.Password);
                }

                bool result = Advapi32.ChangeServiceConfig
                              (
                    this,
                    serviceType,
                    startType,
                    errorControl,
                    ServiceControlManager.DmServiceExe,
                    loadOrderGroup,
                    tagId: 0, // Tags are only evaluated for driver services that have SERVICE_BOOT_START or SERVICE_SYSTEM_START start types.
                    Advapi32.ConvertDependenciesArraysToWin32String(dependOnService, dependOnGroup),
                    credentials.Username,
                    passwordHandle,
                    displayName
                              );

                if (!result)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }
            finally
            {
                if (passwordHandle != IntPtr.Zero)
                {
                    Marshal.ZeroFreeGlobalAllocUnicode(passwordHandle);
                }
            }
        }