Exemple #1
0
        private int ListCacheDirectory(CommonOptions options)
        {
            IWin32Registry winReg = new Win32Registry();

            user.RaiseMessage(winReg.DownloadCacheDir);
            printCacheInfo();
            return(Exit.OK);
        }
Exemple #2
0
 public void Close()
 {
     if (_hKey != 0)
     {
         Win32Registry.RegCloseKey(_hKey);
         _hKey = 0;
     }
 }
Exemple #3
0
        private bool validKey()
        {
            string token;

            return(hostEntry.Value.Length > 0 &&
                   tokenEntry.Value.Length > 0 &&
                   Uri.CheckHostName(hostEntry.Value) != UriHostNameType.Unknown &&
                   !Win32Registry.TryGetAuthToken(hostEntry.Value, out token));
        }
        private static List <string> GetInstalledSoftwareFromHive(RegistryHive hive, string keyAddress, bool use64)
        {
            uint          main = 0;
            List <string> list = new List <string>();

            try
            {
                uint err = Win32Registry.RegOpenKeyEx(unchecked ((uint)hive), keyAddress, 0,
                                                      Win32Registry.RegSAM.Read | Win32Registry.RegSAM.EnumerateSubKeys | (use64 ? Win32Registry.RegSAM.WOW64_64Key : Win32Registry.RegSAM.WOW64_32Key),
                                                      out main);
                if (err != 0)
                {
                    throw new Exception();
                }
                string[] names = GetSubkeysNames(main);
                if (names == null)
                {
                    throw new Exception();
                }
                foreach (string subkeyName in names)
                {
                    uint sub = 0;
                    try
                    {
                        err = Win32Registry.RegOpenKeyEx(main, subkeyName, 0, Win32Registry.RegSAM.Read, out sub);
                        if (err != 0)
                        {
                            continue;
                        }
                        string name = GetValueAsString(sub, "DisplayName");
                        if (name == null)
                        {
                            continue;
                        }
                        list.Add(name);
                    }
                    finally
                    {
                        if (sub != 0)
                        {
                            Win32Registry.RegCloseKey(sub);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (main != 0)
                {
                    Win32Registry.RegCloseKey(main);
                }
            }
            return(list);
        }
Exemple #5
0
 private int addAuthToken(AddAuthTokenOptions opts)
 {
     if (Uri.CheckHostName(opts.host) != UriHostNameType.Unknown)
     {
         Win32Registry.SetAuthToken(opts.host, opts.token);
     }
     else
     {
         user.RaiseError("Invalid host name: {0}", opts.host);
     }
     return(Exit.OK);
 }
Exemple #6
0
        public object GetValue(string valueName, object defaultValue = null)
        {
            int lpType   = 0;
            int lpcbData = 0;
            int num3     = Win32Registry.RegQueryValueEx(_hKey, valueName, null, ref lpType, (byte[])null, ref lpcbData);

            if (num3 != 0)
            {
                return(defaultValue);
            }

            switch (lpType)
            {
            case 1:      // String = 1
            {
                RegistryValueKind subkeyKind      = RegistryValueKind.String;
                StringBuilder     subkeyValueText = new StringBuilder((int)1024);
                uint subKeyValueSize = (uint)subkeyValueText.Capacity;

                uint errorCode = Win32Registry.RegQueryValueEx(_hKey, valueName, 0, ref subkeyKind, subkeyValueText, ref subKeyValueSize);
                if (errorCode != 0)
                {
                    return(null);
                }

                return(subkeyValueText.ToString());
            }

            case 4:      // DWord
            {
                int num8 = 0;
                num3 = Win32Registry.RegQueryValueEx(_hKey, valueName, null, ref lpType, ref num8, ref lpcbData);
                return(num8);
            }

            case 3:     // Binary
            case 5:
            {
                byte[] buffer2 = new byte[lpcbData];
                num3 = Win32Registry.RegQueryValueEx(_hKey, valueName, null, ref lpType, buffer2, ref lpcbData);
                return(buffer2);
            }

                /*
                 * Non implementé pour l'instant
                 * Unknown = 0,
                 * ExpandString = 2,
                 * MultiString = 7,
                 * QWord = 11*/
            }

            return(defaultValue);
        }
        internal static string GetValueAsString(uint hSubkey, string valueName)
        {
            RegistryValueKind subkeyKind      = RegistryValueKind.String;
            StringBuilder     subkeyValueText = new StringBuilder((int)1024);
            uint subKeyValueSize = (uint)subkeyValueText.Capacity;
            uint errorCode       = Win32Registry.RegQueryValueEx(hSubkey, valueName, 0, ref subkeyKind, subkeyValueText, ref subKeyValueSize);

            if (errorCode != 0)
            {
                return(null);
            }
            return(subkeyValueText.ToString());
        }
Exemple #8
0
        private int SetCacheSizeLimit(SetLimitOptions options)
        {
            IWin32Registry winReg = new Win32Registry();

            if (options.Megabytes < 0)
            {
                winReg.CacheSizeLimit = null;
            }
            else
            {
                winReg.CacheSizeLimit = options.Megabytes * (long)1024 * (long)1024;
            }
            return(ShowCacheSizeLimit(null));
        }
Exemple #9
0
        private int ShowCacheSizeLimit(CommonOptions options)
        {
            IWin32Registry winReg = new Win32Registry();

            if (winReg.CacheSizeLimit.HasValue)
            {
                user.RaiseMessage(CkanModule.FmtSize(winReg.CacheSizeLimit.Value));
            }
            else
            {
                user.RaiseMessage("Unlimited");
            }
            return(Exit.OK);
        }
Exemple #10
0
        private int ResetCacheDirectory(CommonOptions options)
        {
            string failReason;

            if (manager.TrySetupCache("", out failReason))
            {
                IWin32Registry winReg = new Win32Registry();
                user.RaiseMessage($"Download cache reset to {winReg.DownloadCacheDir}");
                printCacheInfo();
            }
            else
            {
                user.RaiseError($"Can't reset cache path: {failReason}");
            }
            return(Exit.OK);
        }
Exemple #11
0
        public RegistryKeyEx OpenWritableSubKey(string subkey, RegistryViewEx view = RegistryViewEx.Default)
        {
            if (view == RegistryViewEx.Registry64 && !Is64BitOperatingSystem)
            {
                return(null);
            }

            Win32Registry.RegSAM samView = 0;

            if (view == RegistryViewEx.Registry32 && IntPtr.Size == 8) // Laisser défault, si on tourne pas en x64
            {
                samView = Win32Registry.RegSAM.WOW64_32Key;
            }
            else if (view == RegistryViewEx.Registry64)
            {
                samView = Win32Registry.RegSAM.WOW64_64Key;
            }

            uint hKey = 0;

            uint err = Win32Registry.RegOpenKeyEx(unchecked ((uint)_hKey), subkey, 0, Win32Registry.RegSAM.Read | Win32Registry.RegSAM.Write | Win32Registry.RegSAM.EnumerateSubKeys | samView, out hKey);

            if (err != 0)
            {
                err = Win32Registry.RegCreateKeyEx(unchecked ((uint)_hKey), subkey, 0, null,
                                                   Win32Registry.RegOption.NonVolatile,
                                                   Win32Registry.RegSAM.Read | Win32Registry.RegSAM.Write | Win32Registry.RegSAM.EnumerateSubKeys | samView,
                                                   null,
                                                   out hKey,
                                                   IntPtr.Zero);

                if (err != 0)
                {
                    return(null);
                }
            }

            return(new RegistryKeyEx(hKey));
        }
Exemple #12
0
        private int SetCacheDirectory(SetOptions options)
        {
            if (string.IsNullOrEmpty(options.Path))
            {
                user.RaiseError("set <path> - argument missing, perhaps you forgot it?");
                return(Exit.BADOPT);
            }

            string failReason;

            if (manager.TrySetupCache(options.Path, out failReason))
            {
                IWin32Registry winReg = new Win32Registry();
                user.RaiseMessage($"Download cache set to {winReg.DownloadCacheDir}");
                printCacheInfo();
                return(Exit.OK);
            }
            else
            {
                user.RaiseError($"Invalid path: {failReason}");
                return(Exit.BADOPT);
            }
        }
Exemple #13
0
        private int listAuthTokens(CommonOptions opts)
        {
            List <string> hosts = new List <string>(Win32Registry.GetAuthTokenHosts());

            if (hosts.Count > 0)
            {
                int longestHostLen  = hostHeader.Length;
                int longestTokenLen = tokenHeader.Length;
                foreach (string host in hosts)
                {
                    longestHostLen = Math.Max(longestHostLen, host.Length);
                    string token;
                    if (Win32Registry.TryGetAuthToken(host, out token))
                    {
                        longestTokenLen = Math.Max(longestTokenLen, token.Length);
                    }
                }
                // Create format string: {0,-longestHostLen}  {1,-longestTokenLen}
                string fmt = string.Format("{0}0,-{2}{1}  {0}1,-{3}{1}",
                                           "{", "}", longestHostLen, longestTokenLen);
                user.RaiseMessage(fmt, hostHeader, tokenHeader);
                user.RaiseMessage(fmt,
                                  new string('-', longestHostLen),
                                  new string('-', longestTokenLen)
                                  );
                foreach (string host in hosts)
                {
                    string token;
                    if (Win32Registry.TryGetAuthToken(host, out token))
                    {
                        user.RaiseMessage(fmt, host, token);
                    }
                }
            }
            return(Exit.OK);
        }
Exemple #14
0
        public bool SetValue(string name, object value)
        {
            RegistryValueKind valueKind = CalculateValueKind(value);

            int    size  = 0;
            IntPtr pData = IntPtr.Zero;

            switch (valueKind)
            {
            case RegistryValueKind.String:
                size  = ((string)value).Length + 1;
                pData = Marshal.StringToHGlobalAnsi((string)value);
                break;

            case RegistryValueKind.DWord:
                size  = Marshal.SizeOf(typeof(Int32));
                pData = Marshal.AllocHGlobal(size);
                Marshal.WriteInt32(pData, (int)value);
                break;

            case RegistryValueKind.QWord:
                size  = Marshal.SizeOf(typeof(Int64));
                pData = Marshal.AllocHGlobal(size);
                Marshal.WriteInt64(pData, (long)value);
                break;
            }

            uint retVal = Win32Registry.RegSetValueEx(_hKey, name, 0, valueKind, pData, size);

            if (retVal != 0)
            {
                return(false);
            }

            return(true);
        }
        internal static string[] GetSubkeysNames(uint hMainKey)
        {
            uint numSubKeys;
            uint errorCode = Win32Registry.RegQueryInfoKey(hMainKey, null, 0, 0, out numSubKeys, 0, 0, 0, 0, 0, 0, 0);

            if (errorCode != 0)
            {
                return(null);
            }
            string[] names = new string[numSubKeys];
            for (uint i = 0; i < numSubKeys; i++)
            {
                uint          maxKeySize = 1024;
                StringBuilder sb         = new StringBuilder((int)maxKeySize);
                long          writeTime;
                errorCode = Win32Registry.RegEnumKeyEx(hMainKey, i, sb, ref maxKeySize, 0, 0, 0, out writeTime);
                if (errorCode != 0)
                {
                    break;
                }
                names[i] = sb.ToString();
            }
            return(names);
        }
Exemple #16
0
        /// <summary>
        /// Initialize the screen.
        /// </summary>
        public AuthTokenScreen() : base()
        {
            mainMenu = new ConsolePopupMenu(new List <ConsoleMenuOption>()
            {
                new ConsoleMenuOption("Make a GitHub API token", "",
                                      "Open the web page for creating GitHub API authentication tokens",
                                      true, openGitHubURL)
            });

            AddObject(new ConsoleLabel(
                          1, 2, -1,
                          () => "Authentication tokens for downloads:"
                          ));

            tokenList = new ConsoleListBox <string>(
                1, 4, -1, -2,
                new List <string>(Win32Registry.GetAuthTokenHosts()),
                new List <ConsoleListBoxColumn <string> >()
            {
                new ConsoleListBoxColumn <string>()
                {
                    Header   = "Host",
                    Width    = 20,
                    Renderer = (string s) => s
                },
                new ConsoleListBoxColumn <string>()
                {
                    Header   = "Token",
                    Width    = 50,
                    Renderer = (string s) => {
                        string token;
                        return(Win32Registry.TryGetAuthToken(s, out token)
                                ? token
                                : missingTokenValue);
                    }
                }
            },
                0, 0, ListSortDirection.Descending
                );
            AddObject(tokenList);

            AddObject(new ConsoleLabel(
                          3, -1, -1,
                          () => "NOTE: These values are private! Do not share screenshots of this screen!",
                          null,
                          () => ConsoleTheme.Current.AlertFrameFg
                          ));

            AddTip("Esc", "Back");
            AddBinding(Keys.Escape, (object sender) => false);

            tokenList.AddTip("A", "Add");
            tokenList.AddBinding(Keys.A, (object sender) => {
                AuthTokenAddDialog ad = new AuthTokenAddDialog();
                ad.Run();
                DrawBackground();
                tokenList.SetData(new List <string>(Win32Registry.GetAuthTokenHosts()));
                return(true);
            });

            tokenList.AddTip("R", "Remove", () => tokenList.Selection != null);
            tokenList.AddBinding(Keys.R, (object sender) => {
                if (tokenList.Selection != null)
                {
                    Win32Registry.SetAuthToken(tokenList.Selection, null);
                    tokenList.SetData(new List <string>(Win32Registry.GetAuthTokenHosts()));
                }
                return(true);
            });
        }
Exemple #17
0
 private int removeAuthToken(RemoveAuthTokenOptions opts)
 {
     Win32Registry.SetAuthToken(opts.host, null);
     return(Exit.OK);
 }