Beispiel #1
0
        /* this method is deprecated */
        private void buttonUnregister_Click(object sender, EventArgs e)
        {
            try
            {
                bool   partOfDomain = false;
                string domain       = null;
                using (ManagementObject comObj = WMIUtil.QueryLocalWMI("Select * from win32_computersystem"))
                {
                    partOfDomain = (bool)comObj["PartOfDomain"];
                    domain       = (string)comObj["Domain"];
                    if (!partOfDomain)
                    {// this must be a bug, because the button shouldn't be activated if the instance isn't a domain member
                        MessageBox.Show("This instance is not a member of any domain");
                        return;
                    }

                    DialogResult answer = MessageBox.Show(string.Format("Detach this instance from domain({0})?", domain),
                                                          "Detach from domain", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (answer == DialogResult.No)
                    {
                        return;
                    }

                    ManagementBaseObject paramIn = comObj.GetMethodParameters("UnjoinDomainOrWorkgroup");

                    paramIn["Password"]       = null;         // this won't delete computer record from AD controller
                    paramIn["UserName"]       = null;
                    paramIn["FUnjoinOptions"] = (UInt32)0x00; // Default, no option

                    ManagementBaseObject paramOut = comObj.InvokeMethod("UnjoinDomainOrWorkgroup", paramIn, null);
                    UInt32 retVal = (UInt32)paramOut["ReturnValue"];
                    if (retVal == 0)
                    {
                        EucaLogger.Info("The instance was successfully detached from the domain");
                        MessageBox.Show("The instance was successfully detached from the domain");
                        string hostnameFile = EucaConstant.ProgramRoot + "\\hostname";
                        if (File.Exists(hostnameFile))
                        {
                            try{ File.Delete(hostnameFile); }
                            catch (Exception)   {; }
                        }
                        System.Environment.Exit(0);
                    }
                    else
                    {
                        EucaLogger.Warning(string.Format("Could not detach the instance: exit code={0}", retVal));
                        MessageBox.Show(string.Format("Could not detach the instance: exit code={0}", retVal));
                        return;
                    }
                }
            }
            catch (Exception ie)
            {
                EucaLogger.Exception("Can't detach the instance from the domain", ie);
            }
        }
Beispiel #2
0
        static private void AllowRemoteDesktop()
        {
            string connString = null;

            if (OSEnvironment.OS_Name == OSEnvironment.Enum_OsName.XP ||
                OSEnvironment.OS_Name == OSEnvironment.Enum_OsName.S2003 ||
                OSEnvironment.OS_Name == OSEnvironment.Enum_OsName.S2003R2)
            {
                connString = @"\\.\root\CIMV2";
            }
            else
            {
                connString = @"\\.\root\CIMV2\TerminalServices";
            }

            using (ManagementObject tsObj = WMIUtil.QueryLocalWMI(connString, "Select * from Win32_TerminalServiceSetting"))
            {
                if ((UInt32)tsObj["AllowTSConnections"] != 1)
                {
                    ManagementBaseObject inParams = tsObj.GetMethodParameters("SetAllowTSConnections");
                    inParams["AllowTSConnections"] = 1;
                    try
                    {   // this works for only win7, server 2008, 2008R2
                        inParams["ModifyFirewallException"] = 1;
                    }
                    catch (ManagementException)
                    {
                        ;
                    }
                    ManagementBaseObject outParams = tsObj.InvokeMethod("SetAllowTSConnections", inParams, null);
                    UInt32 ret = (UInt32)outParams["ReturnValue"];

                    if (ret > 1)    // 0=Success, 1=Informational, 2=Warning, 3=Error
                    {
                        throw new Exception(string.Format("SetAllowTsConnects failed with error code {0}", ret));
                    }
                }
            }
        }
Beispiel #3
0
        private void InitPanel()
        {
            /// check if the instance is a member of an AD
            ///
            bool   partOfDomain = false;
            string domain       = null;

            try
            {
                using (ManagementObject comObj = WMIUtil.QueryLocalWMI("Select * from win32_computersystem"))
                {
                    partOfDomain = (bool)comObj["PartOfDomain"];
                    domain       = (string)comObj["Domain"];
                }

                if (partOfDomain)
                {
                    labelADStatus.Text = string.Format("domain member of {0}", domain);
                    // buttonUnregister.Enabled = true;
                    // buttonUnregister.Visible = true;
                }
                else
                {
                    labelADStatus.Text = "not a member of a domain";
                }
            }
            catch (Exception e)
            {
                EucaLogger.Exception("Can't determine if the host is a part of domain", e);
                labelADStatus.Text = "error-AD status could not be determined";
            }

            object tmp = EucaServiceLibraryUtil.GetSvcRegistryValue("ADAddress");

            if (tmp != null)
            {
                _adConfig.ADAddress = (string)tmp;
            }
            tmp = EucaServiceLibraryUtil.GetSvcRegistryValue("ADUsername");
            if (tmp != null)
            {
                _adConfig.ADUsername = (string)tmp;
            }
            tmp = EucaServiceLibraryUtil.GetSvcRegistryValue("ADPassword");
            {
                if (tmp != null)
                {
                    _adConfig.ADPassword = EucaUtil.Decrypt((string)tmp);
                }
            }
            tmp = EucaServiceLibraryUtil.GetSvcRegistryValue("ADOU");
            if (tmp != null)
            {
                _adConfig.ADOU = (string)tmp;
            }

            if (_adConfig.ADAddress != null)
            {
                textBoxADAddress.Text = _adConfig.ADAddress;
            }
            if (_adConfig.ADUsername != null)
            {
                textBoxADUsername.Text = _adConfig.ADUsername;
            }
            if (_adConfig.ADPassword != null)
            {
                textBoxADPassword.Text = _adConfig.ADPassword;
            }
            if (_adConfig.ADOU != null)
            {
                textBoxADOU.Text = _adConfig.ADOU;
            }


            /// bring up the list of users/groups that has RD permission on the image
            string[] rdPermissionUsers = null;
            try
            {
                rdPermissionUsers = this.GetRDPermissionUsers();
            }
            catch (Exception e) {
                EucaLogger.Exception("Could not enumerate RD users/groups from registry", e);
            }

            if (rdPermissionUsers != null)
            {
                foreach (string name in rdPermissionUsers)
                {
                    listBoxRDPermission.Items.Add(name);
                }
            }
            buttonApply.Enabled = false;

            try
            {
                int selected = (int)EucaUtil.GetRegistryValue(Registry.LocalMachine, EUCALYPTUS_REGISTRY_PATH, "FormatDrives");
                if (selected == 1)
                {
                    checkBoxFormatDrives.Checked = true;
                }
                else
                {
                    checkBoxFormatDrives.Checked = false;
                }
            }
            catch (Exception e)
            {
                EucaLogger.Exception("Couldn't check 'FormatDrives' option from registry", e);
            }
        }
        static int Main(string[] args)
        {
            if (args.Length < 1)
            {
                return(-1);
            }

            if (args[0].ToLower() == "-unjoindom")
            {
                try
                {
                    bool   partOfDomain = false;
                    string domain       = null;
                    using (ManagementObject comObj = WMIUtil.QueryLocalWMI("Select * from win32_computersystem"))
                    {
                        partOfDomain = (bool)comObj["PartOfDomain"];
                        domain       = (string)comObj["Domain"];
                        if (!partOfDomain)
                        {// this must be a bug, because the button shouldn't be activated if the instance isn't a domain member
                            //  MessageBox.Show("This instance is not a member of any domain");
                            Console.WriteLine("EucaCLI: The instance is not a member of any domain");
                            return(-1);
                        }

                        /*        string adAddress = null, adUsername = null, adPassword = null;
                         *      object tmp = EucaUtil.GetRegistryValue("ADAddress");
                         *      if (tmp != null)
                         *          adAddress = (string)tmp;
                         *      tmp = EucaUtil.GetRegistryValue("ADUsername");
                         *      if (tmp != null)
                         *          adUsername = (string)tmp;
                         *      tmp = EucaUtil.GetRegistryValue("ADPassword");
                         *      if (tmp != null)
                         *          adPassword = (string)tmp;
                         *
                         *      if (adUsername == null || adPassword == null || adAddress == null)
                         *      {
                         *          Console.WriteLine("EucaCLI: Username/password/ADaddress is not found");
                         *          return -1;
                         *      }
                         *
                         *      if (!adUsername.Contains("\\"))
                         *          adUsername = string.Format("{0}\\{1}", adAddress, adUsername);
                         *
                         *      adPassword = EucaUtil.Decrypt(adPassword);*/
                        ManagementBaseObject paramIn = comObj.GetMethodParameters("UnjoinDomainOrWorkgroup");

                        paramIn["Password"]       = null;
                        paramIn["UserName"]       = null;
                        paramIn["FUnjoinOptions"] = (UInt32)0x00; // default; No option

                        ManagementBaseObject paramOut = comObj.InvokeMethod("UnjoinDomainOrWorkgroup", paramIn, null);
                        UInt32 retVal = (UInt32)paramOut["ReturnValue"];
                        if (retVal == 0)
                        {
                            Console.WriteLine("SUCCESS");
                            string hostnameFile = EucaConstant.ProgramRoot + "\\hostname";
                            if (System.IO.File.Exists(hostnameFile))
                            {
                                try { System.IO.File.Delete(hostnameFile); }
                                catch (Exception) {; }
                            }
                            return(0);
                        }
                        else
                        {
                            Console.WriteLine(string.Format("EucaCLI: Could not detach the instance: exit code={0}", retVal));
                            return(-1);
                        }
                    }
                }
                catch (Exception ie)
                {
                    Console.WriteLine("EucaCLI: Can't detach the instance from the domain (exception thrown)");
                    return(-1);
                }
            }
            else if (args[0].ToLower() == "-setdom")
            {
                try
                {
                    string uname  = args[1].Trim();
                    string passwd = args[2].Trim();

                    if (uname == null || passwd == null)
                    {
                        Console.WriteLine("EucaCLI: uname and passwd is null");
                        return(-1);
                    }
                    string passwdEnc = SystemsUtil.Encrypt(passwd);

                    EucaServiceLibraryUtil.SetSvcRegistryValue("ADUsername", uname);
                    EucaServiceLibraryUtil.SetSvcRegistryValue("ADPassword", passwdEnc);
                    Console.WriteLine("SUCCESS");
                    return(0);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: " + e.Message);
                    return(-1);
                }
            }
            else
            {
                Console.WriteLine("Unknown parameter");
                return(-1);
            }
        }