관리-도구
편집 파일: safe_fileops.cpython-311.pyc
� IMg� � � � d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl m Z mZ d dlm Z d dlmZmZ d dlmZ ej Zej ej z ej z Zd� Z G d� d e� � Zd � Zd"d�Zd efd�Zd edefd�Z d edefd�Z!d efd�Z" eej# � � Z# eej$ � � Z$e d#d�� � Z%e d� � � Z&e defd�� � Z'e d$deee(f fd�� � Z)e dede(de(de*fd�� � Z+deeee(f ee(df f deeee(f ee(df f fd �Z, d%dedefd!�Z-dS )&� N)�ProcessPoolExecutor)�contextmanager�suppress)�chain)�Tuple�Union)�utilsc � � t j g � � t j |� � t j |� � | |� S �N)�os� setgroups�setgid�setuid)�fun�uid�gid�argss �W/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/utils/safe_fileops.py�dropr s8 � ��L������I�c�N�N�N��I�c�N�N�N��3��:�� c � � e Zd ZdS )�UnsafeFileOperationN)�__name__� __module__�__qualname__� r r r r s � � � � � ��Dr r c � � t j t | � � � � }|j t j � � k rt dt | � � z � � �dS )Nz The file belongs to admin user: T)r �stat�str�st_uidr �get_min_uidr )�file�sts r �check_non_admin_filer$ sT � � ���T��� � �B� �y�5�$�&�&�&�&�!�.��T���:� � � � �4r Fc � � � � fd�}|S )Nc �P �� � t j � � � d d�� �fd� � � }|S )N)�loopc � �@ �K � t j � | � � s� st d| z � � �t j | � � }t t |j � � |g� � }� rt |j � � }|D ]I}t j t |� � � � }|j dk r|j dk r|j |j }} n�Jt d� � �|pt j � � } |j t# d�� � t$ � ||| g|�R � � d {V ��S )NzNo such file or directory: r z Unsafe file operation under root� ��max_workers)r �path�exists�FileNotFoundError�pathlib�Pathr �reversed�parentsr r r �st_gidr �asyncio�get_event_loop�run_in_executorr r )�filenamer'