Sindbad~EG File Manager
3
�f�� � @ s, d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl m
Z
d dlmZ d dlm
Z
d dlmZ d dlmZ d dlmZ d dlmZ d d lmZ d d
lmZ d dlmZ d dlmZ d d
lmZmZm Z m!Z! d dl"m#Z#m$Z$ d dl%m&Z& d dl'm(Z(m)Z)m*Z* d dl+m,Z, G dd� de�Z-dS )� N)�ThreadPoolExecutor)�datetime)�getpwuid)�__version__)�SoSComponent)�SoSIPParser)�SoSMacParser)�SoSHostnameParser)�SoSKeywordParser)�SoSUsernameParser)�
SoSIPv6Parser)�SoSReportArchive�SoSReportDirectory�SoSCollectorArchive�SoSCollectorDirectory)�DataDirArchive�TarballArchive)�InsightsArchive)�get_human_readable�
import_module�ImporterHelper)�fillc
sb e Zd ZdZdZdg g g dg dddddg d �ZdN� fd
d� ZdOdd
�ZdPdd�ZdQdd�Z dRdd�Z
dd� Zedd� �Z
dd� Zdd� Zedd� �Zdd� Zd d!� Zd"d#� Zd$d%� Zd&d'� Zd(d)� Zd*d+� Zd,d-� Zd.d/� ZdSd0d1�Zd2d3� Zd4d5� Zd6d7� Zd8d9� Zd:d;� Zd<d=� Z d>d?� Z!d@dA� Z"dTdBdC�Z#dDdE� Z$dFdG� Z%dHdI� Z&dUdJdK�Z'dLdM� Z(� Z)S )V�
SoSCleanera
This function is designed to obfuscate potentially sensitive information
from an sos report archive in a consistent and reproducible manner.
It may either be invoked during the creation of a report by using the
--clean option in the report command, or may be used on an already existing
archive by way of 'sos clean'.
The target of obfuscation are items such as IP addresses, MAC addresses,
hostnames, usernames, and also keywords provided by users via the
--keywords and/or --keyword-file options.
For every collection made in a report the collection is parsed for such
items, and when items are found SoS will generate an obfuscated replacement
for it, and in all places that item is found replace the text with the
obfuscated replacement mapped to it. These mappings are saved locally so
that future iterations will maintain the same consistent obfuscation
pairing.
In the case of IP addresses, support is for IPv4 and IPv6 - effort is made
to keep network topology intact so that later analysis is as accurate and
easily understandable as possible. If an IP address is encountered that we
cannot determine the netmask for, a random IP address is used instead.
For IPv6, note that IPv4-mapped addresses, e.g. ::ffff:10.11.12.13, are
NOT supported currently, and will remain unobfuscated.
For hostnames, domains are obfuscated as whole units, leaving the TLD in
place.
For instance, 'example.com' may be obfuscated to 'obfuscateddomain0.com'
and 'foo.example.com' may end up being 'obfuscateddomain1.com'.
Users will be notified of a 'mapping' file that records all items and the
obfuscated counterpart mapped to them for ease of reference later on. This
file should be kept private.
z6Obfuscate sensitive networking information in a report�auto� Nz /etc/sos/cleaner/default_mappingF� )�archive_type�domains�disable_parsers�skip_clean_files�jobs�keywords�keyword_file�map_file� no_update�keep_binary_files�target� usernamesc
s� |s t t| �j|||� d| _n�|d | _|d | _|d | _|d | _|d | _d| _t | jd�sld | j_
d
| j_tj
d�| _tj
d�| _tjtjj| jd
�dd� | j� | j� | _tjd� || _| jj� | _| jjjd
�| _| jj}t| j|�t | j|�t!| j|�t"| j|�t#| j|�t$| j|�g| _%xx| jj&D ]l}xd| j%D ]Z}|j'j(� j)d�d j*� } |j(� j*� | k�rD| j+d| � | jj,d| � | j%j-|� �qDW �q8W t.t/t0t1t2t3t4g| _5d | _6| j+d| j � d S )NT�options�tmpdir�sys_tmp�policy�manifestFr r r �sosZsos_ui�cleaner)�exist_ok�? �parserr zDisabling parser: %szaDisabling the '%s' parser. Be aware that this may leave sensitive plain-text data in the archive.z%Cleaner initialized. From cmdline: %s)7�superr �__init__�from_cmdline�optsr) r* r+ r, �hasattrr r �loggingZ getLogger�soslog�ui_log�os�makedirs�path�join�review_parser_values�
load_map_fileZcleaner_mapping�umask�in_placeZget_preferred_hash_name� hash_nameZ
components�add_section�
cleaner_mdr r r r r r
r �parsersr �name�lower�split�strip�log_info�warning�remover r
r r r r r �
archive_types�nested_archive)
�selfr1 �argsZcmdlinerA Zhook_commonsr �_parserZ_loadedZ_loaded_name)� __class__� �/usr/lib/python3.6/__init__.pyr3 ` s^
zSoSCleaner.__init__c C s d|rd| nd|f S )Nz[cleaner%s] %sz:%sr rS )rO �msg�callerrS rS rT �_fmt_log_msg� s zSoSCleaner._fmt_log_msgc C s | j j| j||�� d S )N)r8 �debugrW )rO rU rV rS rS rT � log_debug� s zSoSCleaner.log_debugc C s | j j| j||�� d S )N)r8 �inforW )rO rU rV rS rS rT rJ � s zSoSCleaner.log_infoc C s | j j| j||�� d S )N)r8 �errorrW )rO rU rV rS rS rT � log_error� s zSoSCleaner.log_errorc C s4 d}d}x&|j � D ]}|t||dd� d }qW |S )N�P r F)Zreplace_whitespace�
)�
splitlinesr )rO rU �widthZ_fmt�linerS rS rT �_fmt_msg� s
zSoSCleaner._fmt_msgc C s |j d� |j| j� d S )NzSoS Cleaner Detailed Help)Z set_titleZadd_text�__doc__)�clsZsectionrS rS rT �display_help� s
zSoSCleaner.display_helpc C s� i }d}t jj| jj�r(td| jj ��t jj| jj�sX| jj|kr�| jd| jj � n�t| jjd��l}yt j
|�}W nV t jk
r� | jd� Y n8 tk
r� } z| jd| jj|f � W Y dd}~X nX W dQ R X |S )z�Verifies that the map file exists and has usable content.
If the provided map file does not exist, or it is empty, we will print
a warning and continue on with cleaning building a fresh map
z /etc/sos/cleaner/default_mappingz$Requested map file %s is a directoryzHERROR: map file %s does not exist, will not load any obfuscation matches�rzOERROR: Unable to parse map file, json is malformed. Will not load any mappings.zERROR: Could not load '%s': %sN)r: r<