SetSvcRegistryValue() public static method

public static SetSvcRegistryValue ( string key, object value ) : void
key string
value object
return void
Exemplo n.º 1
0
        static private void UnblockFirewall()
        {
            /// in some win versions (s2003, s2003r2), firewall service is not started by default.
            /// before firewall's rule check, we need to make sure they are started.
            ///

            if (!(OSEnvironment.OS_Name == OSEnvironment.Enum_OsName.S2008 ||
                  OSEnvironment.OS_Name == OSEnvironment.Enum_OsName.S2008R2 ||
                  OSEnvironment.OS_Name == OSEnvironment.Enum_OsName.Win7))
            {
                // check if firewall is auto-starting
                try
                {
                    RegistryKey regKey = null;
                    if (OSEnvironment.OS_Name == OSEnvironment.Enum_OsName.Vista)
                    {
                        regKey = Registry.LocalMachine.OpenSubKey("SYSTEM").OpenSubKey("CurrentControlSet")
                                 .OpenSubKey("Services").OpenSubKey("MpsSvc");
                    }
                    else // xp, s2003, s2003r2
                    {
                        regKey = Registry.LocalMachine.OpenSubKey("SYSTEM").OpenSubKey("CurrentControlSet")
                                 .OpenSubKey("Services").OpenSubKey("SharedAccess");
                    }

                    object val = regKey.GetValue("Start");
                    regKey.Close();

                    if ((int)val != 2) // not automatic
                    {
                        Log("Firewall service is not auto-start");
                        return;
                    }
                }
                catch (Exception e)
                {
                    Log(string.Format("Could not open registry key for checking firewall/ics service ({0})", e.Message));
                }

                // make sure firewall service is running
                try
                {
                    bool                svcFound   = false;
                    const int           timeoutSec = 5;
                    ServiceController[] svcs       = ServiceController.GetServices();
                    foreach (ServiceController svc in svcs)
                    {
                        if (svc.DisplayName.Contains("Windows Firewall")) // I believe this covers all cases
                        {
                            svcFound = true;
                            svc.WaitForStatus(ServiceControllerStatus.Running,
                                              new TimeSpan(DateTime.Now.AddSeconds(timeoutSec).Ticks - DateTime.Now.Ticks));
                        }
                    }
                    if (!svcFound)
                    {
                        Log("Firewall service is not found in the system");
                    }
                }
                catch (System.ServiceProcess.TimeoutException)
                {
                    Log("Firewall service is not running (timed out)");
                    return;
                }
                catch (Exception e)
                {
                    Log(string.Format("Firewall service is not running ({0})", e.Message));
                    return;
                }
            }

            Type      FwMgrType = null;
            INetFwMgr mgr       = null;

            try
            {
                FwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", true);
                mgr       = (INetFwMgr)Activator.CreateInstance(FwMgrType);
            }
            catch (Exception e)
            {
                throw;
            }

            foreach (NET_FW_PROFILE_TYPE_ profile in Enum.GetValues(typeof(NET_FW_PROFILE_TYPE_)))
            {
                if (profile == NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_TYPE_MAX)
                {
                    continue;
                }

                try
                {
                    INetFwServices svcs     = mgr.LocalPolicy.GetProfileByType(profile).Services;
                    bool           svcFound = false;
                    foreach (INetFwService svc in svcs)
                    {
                        if (svc.Name == "Remote Desktop")
                        {
                            svcFound = true;
                            if (!svc.Enabled)
                            {
                                svc.Enabled = true;
                                Log(string.Format("Remote desktop service is unblocked in firewall setting in {0}", profile));
                            }
                            else
                            {
                                Log(string.Format("Remote desktop service was already unblocked in firewall setting in {0}", profile));
                            }
                            break;
                        }
                    }

                    if (!svcFound)
                    {
                        throw new Exception("Can't find a RDP service in the existing firewall setting.");
                    }
                }
                catch (Exception e)
                {
                    Log(string.Format("Can't unblock RDP port (tcp-3389) in {0}", profile));
                    Log(e.Message);
                }
            }

            EucaServiceLibraryUtil.SetSvcRegistryValue("FirewallCheck", 1);
        }
Exemplo n.º 2
0
        private void buttonApply_Click(object sender, EventArgs e)
        {
            string adAddr = textBoxADAddress.Text;

            if (adAddr != null && adAddr.Length == 0)
            {
                adAddr = null;
            }
            string adUsername = textBoxADUsername.Text;

            if (adUsername != null && adUsername.Length == 0)
            {
                adUsername = null;
            }
            string adPassword = textBoxADPassword.Text;

            if (adPassword != null && adPassword.Length == 0)
            {
                adPassword = null;
            }
            string adPasswordConfirm = textBoxPwdConfirm.Text;

            if (adPasswordConfirm != null && adPasswordConfirm.Length == 0)
            {
                adPasswordConfirm = null;
            }

            string adOu = textBoxADOU.Text;

            if (adOu != null && adOu.Length == 0)
            {
                adOu = null;
            }

            if (!(adAddr == null && adUsername == null && adPassword == null && adOu == null))
            {// user has put something in the config window
                if (adAddr == null || adUsername == null || adPassword == null)
                {
                    MessageBox.Show("AD address, username, and password are required to enable AD join");
                    return;
                }
            }
            else
            {
                MessageBox.Show("AD address, username, and password are required to enable AD join");
                return;
            }

            if (adPassword != adPasswordConfirm)
            {
                MessageBox.Show("Passwords do not match!");
                return;
            }

            bool updated = false;

            if (adAddr.Trim() != (string)EucaServiceLibraryUtil.GetSvcRegistryValue("ADAddress"))
            {
                EucaServiceLibraryUtil.SetSvcRegistryValue("ADAddress", adAddr.Trim());
                updated = true;
            }
            if (adUsername.Trim() != (string)EucaServiceLibraryUtil.GetSvcRegistryValue("ADUsername"))
            {
                EucaServiceLibraryUtil.SetSvcRegistryValue("ADUsername", adUsername.Trim());
                updated = true;
            }

            //encrypt AD password properly
            if (adPassword.Trim() != (string)EucaServiceLibraryUtil.GetSvcRegistryValue("ADPassword"))
            {
                string data = EucaUtil.Encrypt(adPassword.Trim());
                EucaServiceLibraryUtil.SetSvcRegistryValue("ADPassword", data);
                updated = true;
            }

            if (adOu != null)
            {
                if (adOu.Trim() != (string)EucaServiceLibraryUtil.GetSvcRegistryValue("ADOU"))
                {
                    EucaServiceLibraryUtil.SetSvcRegistryValue("ADOU", adOu.Trim());
                    updated = true;
                }
            }
            if (updated)
            {
                MessageBox.Show("AD information is updated succesfully");
                buttonApply.Enabled = false;
            }
            else
            {
                MessageBox.Show("No change has been saved");
            }
        }
Exemplo n.º 3
0
        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);
            }
        }