HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/__pycache__/win_reg.cpython-310.pyc
o

�N�gHz�@sdZddlZddlZddlZddlmZzddlZddlZddl	Z	dZ
Wney/dZ
Ynwe�e
�ZdZdd�Zd	d
�Zdd�ZGd
d�d�Zd$dd�Zd$dd�Zdd�Zd%dd�Zd%dd�Zd%dd�Z					d&dd�Zd'dd�Zd$d d!�Zd%d"d#�ZdS)(aM
Manage the Windows registry

-----
Hives
-----
Hives are the main sections of the registry and all begin with the word HKEY.

    - HKEY_LOCAL_MACHINE
    - HKEY_CURRENT_USER
    - HKEY_USER

----
Keys
----
Keys are the folders in the registry. Keys can have many nested subkeys. Keys
can have a value assigned to them under the (Default) value name

-----------------
Values or Entries
-----------------
Values/Entries are name/data pairs. There can be many values in a key. The
(Default) value corresponds to the Key itself, the rest are their own name/value
pairs.

:depends:  PyWin32
�N)�CommandExecutionErrorTFZregcCstjj��sdStsdStS)z8
    Only works on Windows systems with the PyWin32
    )FzPreg execution module failed to load: The module will only run on Windows systems)Fznreg execution module failed to load: One of the following libraries did not load: win32gui, win32con, win32api)�salt�utils�platformZ
is_windows�HAS_WINDOWS_MODULES�__virtualname__�rr�F/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/win_reg.py�__virtual__3s
r
cCstjj�|d�S)zq
    Converts unicode to current users character encoding. Use this for values
    returned by reg functions
    �mbcs)rr�stringutils�
to_unicode��vdatarrr	�_to_mbcsIsrcCs.|durd}t|t�rt|�}tjj�|d�S)z|
    Converts from current users character encoding to unicode. Use this for
    parameters being pass to reg functions
    N��utf-8)�
isinstance�int�strrrrr
rrrr	�_to_unicodeQs

rc@s eZdZdZdd�Zdd�ZdS)�Registryz�
    This was put in a class to delay usage until this module is actually used
    This class contains all the lookup dicts for working with the registry
    cCs�tjtjtjtjtjtjtjtjtjtjd�
|_dtjtjtj	tj
tjtjd�|_
ddd�|_ddtjdtjdtj	d	tj
d
tjdtjdi|_d
dd�|_tjdtjdtjdtjdtjdi|_tjtjBtjd�|_dS)N)
�HKEY_CURRENT_CONFIG�HKEY_CLASSES_ROOT�HKEY_CURRENT_USER�HKEY_LOCAL_MACHINE�
HKEY_USERSZHKCCZHKCRZHKCUZHKLMZHKUr)�REG_NONE�
REG_BINARY�	REG_DWORD�
REG_EXPAND_SZ�REG_MULTI_SZ�REG_SZ�	REG_QWORD�)�REG_OPTION_NON_VOLATILE�REG_OPTION_VOLATILErrrr r!r"r#r%r&)rr$)TF)�win32conrrrrr�hkeysrrr r!r"r#�vtype�opttype�
vtype_reverseZopttype_reverse�subkey_slash_checkZKEY_READZKEY_WOW64_32KEY�registry_32)�selfrrr	�__init__dsL�
�	�	�
	�zRegistry.__init__cCs<z|j|WStyd}d�|j�}t|�||���w)Nz#No hkey named '{0}. Try one of {1}'z, )r(�KeyError�joinr�format)r.�k�msgr(rrr	�__getattr__�s�zRegistry.__getattr__N)�__name__�
__module__�__qualname__�__doc__r/r5rrrr	r^s1rc

Cst|�}t|�}t�}z|j|}Wnty td|����w|j|}d}zPzt�||d|�}WW|r=t�|�dSdStj	yx}	z-|	j
dkr]WYd}	~	W|r[t�|�dSdS|	j
dkrsWYd}	~	W|rqt�|�dSdS�d}	~	ww|r�t�|�ww)a
    Check that the key is found in the registry. This refers to keys and not
    value/data pairs. To check value/data pairs, use ``value_exists``

    Args:

        hive (str): The hive to connect to

        key (str): The key to check

        use_32bit_registry (bool): Look in the 32bit portion of the registry

    Returns:
        bool: True if exists, otherwise False

    Usage:

        .. code-block:: python

            import salt.utils.win_reg as reg
            reg.key_exists(hive='HKLM', key='SOFTWARE\Microsoft')
    �Invalid Hive: NrT�F�)rrr(r0rr-�win32api�RegOpenKeyEx�RegCloseKey�error�winerror)
�hive�key�use_32bit_registry�
local_hive�	local_key�registry�hkey�access_mask�handle�excrrr	�
key_exists�sB�
	��
�
������rLc

CsNt|�}t|�}t|�}t�}z|j|}Wnty$td|����w|j|}	z
t�||d|	�}
WntjyN}z
|j	dkrIWYd}~dS�d}~wwzNzt�
|
|�\}}WW|
rdt�|
�dSdStjy�}z+|j	dkr�|dur�WYd}~W|
r�t�|
�dSdSWYd}~W|
r�t�|
�dSdSd}~ww|
r�t�|
�ww)a�
    Check that the value/data pair is found in the registry.

    .. versionadded:: 2018.3.4

    Args:

        hive (str): The hive to connect to

        key (str): The key to check in

        vname (str): The name of the value/data pair you're checking

        use_32bit_registry (bool): Look in the 32bit portion of the registry

    Returns:
        bool: True if exists, otherwise False

    Usage:

        .. code-block:: python

            import salt.utils.win_reg as reg
            reg.value_exists(hive='HKLM',
                                key='SOFTWARE\Microsoft\Windows\CurrentVersion',
                                vname='CommonFilesDir')
    r:rr;NFT)rrr(r0rr-r=r>r@rA�RegQueryValueExr?)
rBrC�vnamerDrErF�local_vnamerGrHrIrJrK�_rrr	�value_exists�sN�

��	��������rQcCs(t�tjtjddtjd�\}}t|�S)ao
    Refresh the windows environment.

    .. note::
        This will only effect new processes and windows. Services will not see
        the change until the system restarts.

    Returns:
        bool: True if successful, otherwise False

    Usage:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.broadcast_change()
    ri�)�win32guiZSendMessageTimeoutr'ZHWND_BROADCASTZWM_SETTINGCHANGEZSMTO_ABORTIFHUNG�bool)rP�resrrr	�broadcast_changes�
rUc

Csxt|�}t|�}t�}z|j|}Wnty td|����w|j|}g}d}	z�z!t�||d|�}	tt�	|	�d�D]
}
t�
|	|
�}|�|�q=Wn\tjy�}zO|j
dkr|tjd||dd�dd	|�d
|��fWYd}~W|	r{t�|	�SS|j
dkr�tjd||dd�dd
|�d
|��fWYd}~W|	r�t�|	�SS�d}~wwW|	r�t�|	�|S|	r�t�|	�ww)a�
    Enumerates the subkeys in a registry key or hive.

    Args:

       hive (str):
            The name of the hive. Can be one of the following:

                - HKEY_LOCAL_MACHINE or HKLM
                - HKEY_CURRENT_USER or HKCU
                - HKEY_USERS or HKU
                - HKEY_CLASSES_ROOT or HKCR
                - HKEY_CURRENT_CONFIG or HKCC

        key (str):
            The key (looks like a path) to the value name. If a key is not
            passed, the keys under the hive will be returned.

        use_32bit_registry (bool):
            Accesses the 32bit portion of the registry on 64 bit installations.
            On 32bit machines this is ignored.

    Returns:
        list: A list of keys/subkeys under the hive or key.

    Usage:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.list_keys(hive='HKLM', key='SOFTWARE\Microsoft')
    r:Nrr;�Cannot find key: %s\%sT��exc_infoF�Cannot find key: �\r<�Access is denied: %s\%s�Access is denied: )rrr(r0rr-r=r>�range�RegQueryInfoKey�
RegEnumKey�appendr@rA�log�debugr?)
rBrCrDrErFrGrHrIZsubkeysrJ�i�subkeyrKrrr	�	list_keys-sP"�
�
 �
� �����
��rec
Cs�t|�}t|�}t�}z|j|}Wnty td|����w|j|}d}t�}	z�zWt�||d|�}t	t�
|�d�D]C}
t�||
�\}}}
|sMd}||t|�|j
|
dd�}|
tjkridd	�|D�|d
<n|
tjtjfvrxt|�|d
<n||d
<|	�|�q>WnXtjy�}zK|jdkr�t�d||�d
d|�d|��fWYd}~W|r�t�|�SS|jdkr�t�d||�d
d|�d|��fWYd}~W|r�t�|�SS�d}~wwW|r�t�|�|	S|r�t�|�ww)a8
    Enumerates the values in a registry key or hive.

    .. note::
        The ``(Default)`` value will only be returned if it is set, otherwise it
        will not be returned in the list of values.

    Args:

        hive (str):
            The name of the hive. Can be one of the following:

                - HKEY_LOCAL_MACHINE or HKLM
                - HKEY_CURRENT_USER or HKCU
                - HKEY_USER or HKU
                - HKEY_CLASSES_ROOT or HKCR
                - HKEY_CURRENT_CONFIG or HKCC

        key (str):
            The key (looks like a path) to the value name. If a key is not
            passed, the values under the hive will be returned.

        use_32bit_registry (bool):
            Accesses the 32bit portion of the registry on 64 bit installations.
            On 32bit machines this is ignored.

    Returns:
        list: A list of values under the hive or key.

    Usage:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.list_values(hive='HKLM', key='SYSTEM\CurrentControlSet\Services\Tcpip')
    r:Nrr$�	(Default)T)rBrCrNr)�successcS�g|]}t|��qSr�r��.0rcrrr	�
<listcomp>��zlist_values.<locals>.<listcomp>rr;rVFrYrZr<r[r\)rrr(r0rr-�listr=r>r]r^ZRegEnumValuerr+r'r!r"r r`r@rArarbr?)rBrCrDrErFrGrHrIrJ�valuesrcrNrr)�valuerKrrr	�list_valuesrsj%�
�
�
 �
� ����� 
��rqc
Csrt|�}t|�}t|�}|||dddd�}|sd|d<t�}z|j|}	Wnty3td|����w|j|}
d}z�z�t�|	|d|
�}zAt�||�\}}
|sW|ddgfvr�|j	|
|d	<|
t
jkrmd
d�|D�|d<n|
t
jt
j
fvr|t|�|d<n	||d<nd
|d<WnEtjy�}z8|jdkr�|dur�d|d<d|d	<n |jdkr�d�|||�}t�|�t�|�||d<d|d<n�WYd}~nd}~wwWnVtj�y#}zH|jdkr�d|�d|��}t�|�t�|�||d<d|d<n"|jdk�rd|�d|��}t�|�t�|�||d<d|d<n�WYd}~nd}~wwW|�r-t�|�|S|�r8t�|�ww)a�
    Reads a registry value entry or the default value for a key. To read the
    default value, don't pass ``vname``

    Args:

        hive (str): The name of the hive. Can be one of the following:

            - HKEY_LOCAL_MACHINE or HKLM
            - HKEY_CURRENT_USER or HKCU
            - HKEY_USER or HKU
            - HKEY_CLASSES_ROOT or HKCR
            - HKEY_CURRENT_CONFIG or HKCC

        key (str):
            The key (looks like a path) to the value name.

        vname (str):
            The value name. These are the individual name/data pairs under the
            key. If not passed, the key (Default) value will be returned.

        use_32bit_registry (bool):
            Accesses the 32bit portion of the registry on 64bit installations.
            On 32bit machines this is ignored.

    Returns:
        dict: A dictionary containing the passed settings as well as the
            value_data if successful. If unsuccessful, sets success to False.

        bool: Returns False if the key is not found

        If vname is not passed:

            - Returns the first unnamed value (Default) as a string.
            - Returns none if first unnamed value is empty.

    Usage:

        The following will get the value of the ``version`` value name in the
        ``HKEY_LOCAL_MACHINE\\SOFTWARE\\Salt`` key

        .. code-block:: python

            import salt.utils.win_reg as reg
            reg.read_value(hive='HKLM', key='SOFTWARE\\Salt', vname='version')

    Usage:

        The following will get the default value of the
        ``HKEY_LOCAL_MACHINE\\SOFTWARE\\Salt`` key

        .. code-block:: python

            import salt.utils.win_reg as reg
            reg.read_value(hive='HKLM', key='SOFTWARE\\Salt')
    NT)rBrCrNrr)rgrfrNr:rrr)cSrhrrirjrrr	rl)rmzread_value.<locals>.<listcomp>rzEmpty ValueZcommentr;z(value not set)r"zCannot find {} in {}\{}FrgrYrZr<r\)rrr(r0rr-r=r>rMr+r'r!r"r rr@rAr2raZtracer?)rBrCrNrDrErFrO�retrGrHrIrJrr)rKr4rrr	�
read_value�s�=�	�


�

�


���






���
��rsr"c
Cst|�}t|�}t|�}	t|�}
t�}z|j|}Wnty(td|����w|j|
}
|j|tjB}t	||
d�}|rD|j
d}n|j
d}d}z�z�tj||||d�\}}|dkr^dnd	}t
�|||�zt�||	d
|
|�t�|�t�WWW|r�t�|�dSdSty�}zt
�d|�WYd}~WW|r�t�|�d
Sd
Sd}~wttfy�}zt
�d|�WYd}~WW|r�t�|�d
Sd
Sd}~wwtjy�}zt
�d|||j|j�WYd}~W|r�t�|�d
Sd
Sd}~ww|�rt�|�ww)a�
    Sets a value in the registry. If ``vname`` is passed, it will be the value
    for that value name, otherwise it will be the default value for the
    specified key

    Args:

        hive (str):
            The name of the hive. Can be one of the following

                - HKEY_LOCAL_MACHINE or HKLM
                - HKEY_CURRENT_USER or HKCU
                - HKEY_USER or HKU
                - HKEY_CLASSES_ROOT or HKCR
                - HKEY_CURRENT_CONFIG or HKCC

        key (str):
            The key (looks like a path) to the value name.

        vname (str):
            The value name. These are the individual name/data pairs under the
            key. If not passed, the key (Default) value will be set.

        vdata (str, int, list, bytes):
            The value you'd like to set. If a value name (vname) is passed, this
            will be the data for that value name. If not, this will be the
            (Default) value for the key.

            The type of data this parameter expects is determined by the value
            type specified in ``vtype``. The correspondence is as follows:

                - REG_BINARY: Binary data (bytes)
                - REG_DWORD: int
                - REG_EXPAND_SZ: str
                - REG_MULTI_SZ: list of str
                - REG_QWORD: int
                - REG_SZ: str

                .. note::
                    When setting REG_BINARY, string data will be converted to
                    binary. You can pass base64 encoded using the ``binascii``
                    built-in module. Use ``binascii.b2a_base64('your data')``

            .. note::
                The type for the (Default) value is always REG_SZ and cannot be
                changed.

            .. note::
                This parameter is optional. If not passed, the Key will be
                created with no associated item/value pairs.

        vtype (str):
            The value type. The possible values of the vtype parameter are
            indicated above in the description of the vdata parameter.

        use_32bit_registry (bool):
            Sets the 32bit portion of the registry on 64bit installations. On
            32bit machines this is ignored.

        volatile (bool):
            When this parameter has a value of True, the registry key will be
            made volatile (i.e. it will not persist beyond a system reset or
            shutdown). This parameter only has an effect when a key is being
            created and at no other time.

    Returns:
        bool: True if successful, otherwise False

    Usage:

        This will set the version value to 2015.5.2 in the SOFTWARE\Salt key in
        the HKEY_LOCAL_MACHINE hive

        .. code-block:: python

            import salt.utils.win_reg
            winreg.set_value(hive='HKLM', key='SOFTWARE\Salt', vname='version', vdata='2015.5.2')

    Usage:

        This function is strict about the type of vdata. For instance this
        example will fail because vtype has a value of REG_SZ and vdata has a
        type of int (as opposed to str as expected).

        .. code-block:: python

            import salt.utils.win_reg
            winreg.set_value(hive='HKLM', key='SOFTWARE\Salt', vname='str_data', vdata=1.2)

    Usage:

        In this next example vdata is properly quoted and should succeed.

        .. code-block:: python

            import salt.utils.win_reg
            winreg.set_value(hive='HKLM', key='SOFTWARE\Salt', vname='str_data', vdata='1.2')

    Usage:

        This is an example of using vtype REG_BINARY. Both ``set_value``
        commands will set the same value ``Salty Test``

        .. code-block:: python

            import salt.utils.win_reg
            winreg.set_value(hive='HKLM', key='SOFTWARE\Salt', vname='bin_data', vdata='Salty Test', vtype='REG_BINARY')

            import binascii
            bin_data = binascii.b2a_base64('Salty Test')
            winreg.set_value(hive='HKLM', key='SOFTWARE\Salt', vname='bin_data_encoded', vdata=bin_data, vtype='REG_BINARY')

    Usage:

        An example using vtype REG_MULTI_SZ is as follows:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.set_value(hive='HKLM', key='SOFTWARE\Salt', vname='list_data', vdata=['Salt', 'is', 'great'], vtype='REG_MULTI_SZ')
    r:)rr)r&r%N)�Optionsr$zCreated new key: %s\%szOpened existing key: %s\%srTz1"vdata" does not match the expected data type.
%sFz,Encountered error setting registry value.
%sz'Error creating/opening key: %s\%s
%s
%s)rrr(r0rr)r-r'�KEY_ALL_ACCESS�
cast_vdatar*r=ZRegCreateKeyExrarbZ
RegSetValueExZRegFlushKeyrUr?�	TypeError�	exception�SystemError�
ValueErrorr@rA�strerror)rBrCrNrr)rDZvolatilerErFrOZlocal_vtyperGrH�vtype_valuerIZlocal_vdataZcreate_optionsrJ�resultr4rKrrr	�	set_valueTs��


��
�����
��������r~cCs|t�}|j|}|tjtjfvrt|�S|tjkr%t|t�r#|�	d�S|S|tj
kr1dd�|D�S|tjkr:t|�St|�S)a
    Cast the ``vdata` value to the appropriate data type for the registry type
    specified in ``vtype``

    Args:

        vdata (str, int, list, bytes): The data to cast

        vtype (str):
            The type of data to be written to the registry. Must be one of the
            following:

                - REG_BINARY
                - REG_DWORD
                - REG_EXPAND_SZ
                - REG_MULTI_SZ
                - REG_QWORD
                - REG_SZ

    Returns:
        The vdata cast to the appropriate type. Will be unicode string, binary,
        list of unicode strings, or int

    Usage:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.cast_vdata(vdata='This is the string', vtype='REG_SZ')
    rcSrhr)rrjrrr	rl@rmzcast_vdata.<locals>.<listcomp>)
rr)r'r"r rrrr�encoder!r#r)rr)rGr|rrr	rvs#





rvc
st|�}t|�}t�}z|j|}Wnty td|����w|}|j|tjB�t|||�s:t	�
d||�dSt|�dkrT|�dd�|j
|krTt	�d||�dSdd�����fd	d
��g}�|||��}|�|�ggd�}	|D]�}
d}z{zGt�||
d
��}zt�|d�|	d�|�d|
���Wn(ty�}zt	j|dd�|	d�|�d|
�d|���WYd}~nd}~wwWn*tjy�}zt	j|dd�|	d�|�d|
�d|j���WYd}~nd}~wwW|r�t�|�qu|r�t�|�wwt�|	S)aJ
    .. versionadded:: 2015.5.4

    Delete a registry key to include all subkeys and value/data pairs.

    Args:

        hive (str):
            The name of the hive. Can be one of the following

                - HKEY_LOCAL_MACHINE or HKLM
                - HKEY_CURRENT_USER or HKCU
                - HKEY_USER or HKU
                - HKEY_CLASSES_ROOT or HKCR
                - HKEY_CURRENT_CONFIG or HKCC

            key (str):
                The key to remove (looks like a path)

            use_32bit_registry (bool):
                Deletes the 32bit portion of the registry on 64bit
                installations. On 32bit machines this is ignored.

    Returns:
        dict: A dictionary listing the keys that deleted successfully as well as
            those that failed to delete.

    Usage:

        The following example will remove ``salt`` and all its subkeys from the
        ``SOFTWARE`` key in ``HKEY_LOCAL_MACHINE``:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.delete_key_recursive(hive='HKLM', key='SOFTWARE\DeleteMe')
    r:z"%s\%s" not foundFr$rZz0"%s\%s" is too close to root, not safe to removecssD�d}	zt�||�}t|�V|d7}Wntjy YdSwq)z 
        Enumerate keys
        rTr$N)r=r_rr@)�_keyrcrdrrr	�_subkeys�s�
��z&delete_key_recursive.<locals>._subkeyscsHt�||d|�}�|�D]}|�d|��}�|||��}|�|�q|S)zD
        Traverse the registry tree i.e. dive into the tree
        rrZ)r=r>r`)Z_hkeyZ_keypathZ_retZ_access_maskr�Z
subkeynameZ
subkeypath�r��_traverse_registry_treerIrr	r��sz5delete_key_recursive.<locals>._traverse_registry_tree)�Deleted�FailedNrrr�TrWr�� )rrr(r0rr-r'rurLrarb�len�countr,r@r`r=r>ZRegDeleteKey�OSErrorr{r?rU)
rBrCrDrErFrGrHZkey_pathZkey_listrrZsub_key_pathZ
key_handlerKrr�r	�delete_key_recursiveIsb'�"


*���,���
���r�c
Cs�t|�}t|�}t|�}t�}z|j|}Wnty$td|����w|j|tjB}	d}
zCzt�	||d|	�}
t�
|
|�t�WW|
rMt�|
�dSdStj
yr}z|jdkrmWYd}~W|
rkt�|
�dSdS�d}~ww|
r{t�|
�ww)a
    Delete a registry value entry or the default value for a key.

    Args:

        hive (str):
            The name of the hive. Can be one of the following

                - HKEY_LOCAL_MACHINE or HKLM
                - HKEY_CURRENT_USER or HKCU
                - HKEY_USER or HKU
                - HKEY_CLASSES_ROOT or HKCR
                - HKEY_CURRENT_CONFIG or HKCC

        key (str):
            The key (looks like a path) to the value name.

        vname (str):
            The value name. These are the individual name/data pairs under the
            key. If not passed, the key (Default) value will be deleted.

        use_32bit_registry (bool):
            Deletes the 32bit portion of the registry on 64bit installations. On
            32bit machines this is ignored.

    Return:
        bool: True if successful, otherwise False

    Usage:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.delete_value(hive='HKLM', key='SOFTWARE\SaltTest', vname='version')
    r:NrTr;)rrr(r0rr-r'rur=r>ZRegDeleteValuerUr?r@rA)rBrCrNrDrErFrOrGrHrIrJrKrrr	�delete_value�s>$���
�����r�)F)NF)NNr"FF)Nr")r9�loggingZsalt.utils.platformrZsalt.utils.stringutilsZsalt.exceptionsrr=r'rRr�ImportError�	getLoggerr6rarr
rrrrLrQrUrerqrsr~rvr�r�rrrr	�<module>sH�

@
1@

E
Y

�
<
:s