File manager - Edit - /opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/__pycache__/win_lgpo_reg.cpython-310.pyc
Back
o ;j�f � @ st d Z ddlZddlZddlZddlZddlZddlZddlZddlm Z ddl ZddlZddl ZddlZddlmZ zddlZdZW n eyM dZY nw ej�e�dd�d d dd�d dddd�ej�e�dd�d d dd�ddddd�d�ZdZej�e�dd�d d d�Ze�e�ZdZdd� Zdd� Zdd � Z d!d"� Z!d#d$� Z"e d/d%d&��Z#ed'd(fd)d*�Z$d+d,� Z%d-d.� Z&dS )0z� A Salt Util for working with the Registry.pol file. The Registry.pol file is the source of truth for registry settings that are configured via LGPO. � N)�contextmanager)�CommandExecutionErrorTFZWINDIRz C:\WindowsZSystem32ZGroupPolicy�UserzRegistry.polZ HKEY_USERSzUser Configuration�gPCUserExtensionNameszN[{35378EAC-683F-11D2-A89A-00C04FBBCFA2}{D02B1F73-3407-48AE-BA88-E8213C6761F1}])Zpolicy_pathZhiveZlgpo_sectionZgpt_extension_location�gpt_extension_guid�Machine�HKEY_LOCAL_MACHINEzComputer Configuration�gPCMachineExtensionNameszN[{35378EAC-683F-11D2-A89A-00C04FBBCFA2}{D02B1F72-3407-48AE-BA88-E8213C6761F1}])r r u 剐来 zgpt.iniZlgpo_regc C s t jj�� sdS tsdS tS )z8 Only works on Windows with the lgpo_reg module )Fz(LGPO_REG Util: Only available on Windows)Fz$LGPO_REG Util: Missing win32 modules)�salt�utils�platformZ is_windows�HAS_WINDOWS_MODULES�__virtualname__� r r �K/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/win_lgpo_reg.py�__virtual__C s r c C s: |r| rt �d| � t�| |tj�}|rt �d� dS dS )a{ Helper function to do a regex search of a string value in policy_data. This is used to search the policy data from a registry.pol file or from gpt.ini Args: search_string (str): The string to search for policy_data (str): The data to be searched Returns: bool: ``True`` if the regex search_string is found, otherwise ``False`` zLGPO_REG Util: Searching for %szLGPO_REG Util: FoundTF)�log�debug�re�search� IGNORECASE)� search_string�policy_data�matchr r r �search_reg_polO s r c C s� z't �d�} t j| j_t jg| j_| �d�}|rt�d� nt�d� t |�W S t y@ } z t�d|� W Y d}~dS d}~ww )u_ Trigger a native in-process Machine Group Policy refresh via userenv.dll. Calls ``RefreshPolicy(bMachine=True)`` exported by ``userenv.dll`` via ``ctypes``. This signals the native OS Group Policy service to process the local ``.pol`` file and commit registry keys without requiring a direct write to the protected policy hive. .. note:: This call is **asynchronous** — it signals the GP service to begin processing the local ``Registry.pol`` file and returns before that processing is complete. Registry values will reflect the updated policy only after the service finishes its refresh cycle. To verify the applied state after the refresh, use :func:`salt.modules.win_lgpo_reg.get_rsop_value`. Returns: bool: ``True`` if the refresh signal was accepted successfully �userenv.dllTz:LGPO_REG Util: Group Policy refresh triggered successfullyz2LGPO_REG Util: Group Policy refresh returned Falsez9LGPO_REG Util: Failed to trigger Group Policy refresh: %sNF)�ctypes�WinDLL�c_boolZ RefreshPolicy�restype�argtypesr r �warning�bool� Exception�error)�userenv�result�excr r r �refresh_policyg s ��r( c C sZ d}t j�| �r+t�d| � tjj�| d�� }|� � }W d � |S 1 s&w Y |S )z� Helper function to read the content of the Registry.pol file Args: reg_pol_path (str): The path to the Registry.pol file Returns: bytes: The data as contained in the Registry.pol file NzLGPO_REG Util: Reading from %s�rb) �os�path�existsr r r r �files�fopen�read)Zreg_pol_pathZreturn_dataZpol_filer r r �read_reg_pol_file� s ��r0 c C s� t d|d �D ]P}z$tjj�| |��}|�|� W d � W dS 1 s%w Y W dS tyW } z|jdks>||kr?� t� d| |||� t �|� W Y d}~qd}~ww dS )z� Write data to a file, retrying on Windows sharing violations (winerror 32). Fails immediately on any other error (e.g. winerror 5, true access denied). � N� zFLGPO_REG Util: %s is locked (attempt %d/%d). Retrying in %d seconds...)�ranger r r- r. �write�PermissionError�winerrorr r! �time�sleep)r+ �data�mode�retry_count�retry_delay�attempt�f�er r r �_write_with_retry� s, ������r@ c c sv � t �d�}t jj|j_t jg|j_t j|j_t jjg|j_|�| �}|s)t d��zdV W |�|� dS |�|� w )uc Context manager that holds the Windows GP critical section for the duration of a read-modify-write cycle on Registry.pol. EnterCriticalPolicySection / LeaveCriticalPolicySection are the same primitives gpsvc uses internally, so holding this lock prevents the GP service from opening the policy file concurrently. EnterCriticalPolicySection is a blocking call — it does not return until the critical section is acquired. If gpsvc currently holds it (e.g. during a background GP refresh), this call blocks until gpsvc releases it, at which point gpsvc will also have released any file lock on Registry.pol. No retry loop is needed here; the blocking behavior is the wait mechanism. The only residual risk after acquiring the critical section is a non-GP locker (AV scanner, VSS) that does not participate in this handshake; the _write_with_retry layer handles those. If the lock cannot be acquired (returns NULL), a CommandExecutionError is raised. NULL from this API always indicates a genuine system error (handle exhaustion, access denied creating the kernel object, etc.) — not a "GP service is busy" condition. The blocking behavior handles the busy case. Proceeding silently without the lock would risk an uncoordinated read-modify-write followed by a confusing downstream write error. r z4LGPO_REG Util: Failed to acquire GP critical sectionN) r r ZwintypesZHANDLEZEnterCriticalPolicySectionr r r ZLeaveCriticalPolicySectionr )�machiner% �handler r r �_policy_lock� s � �rC � � c C s t j�|�st�d� tjj�|� | du rd} z$t � d�}| �|�s-t�d|� || } t�d|� t|| d||� W n t yY } zd|� d |� �} t�| � t| ��d}~ww d } t j�|�r�tjj�|d��}|�� } W d � n1 syw Y | �dd ��dd ��d d�} td| �s�t�d� d| } tt�|�� d�| �r�t�dt�|�� d�| tjtjB �}| |�� |�� � } tt�|�� | d�s�t�d� | �d�} || d | d<