
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
."Éd$!  ã               @   sp  d dl Z d dlZd dlZd dlZd dlmZ d dlZd dlZd dlZd dl	Zd dl
Z
dd„ e_dZdZdZdZd	d
„ Zyejd Zeej_W n ek
r¤   eZY nX yejd Zeej_W n ek
rÔ   eZY nX ejjedƒZejjedƒZejjedƒZejjeeƒZdd„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"dd„ Z#dd„ Z$G dd„ dƒZ%d d!„ Z&e'd"k�rle&ƒ  dS )#é    N)Úmkstempc             O   s   |j › dt| ƒjƒ › d�S )Nz: Ú
)Ú__name__ÚstrÚ
capitalize)ÚmsgÚcategoryÚaZkwa© r
   ú<./usr/share/crypto-policies/python/update-crypto-policies.pyÚ<lambda>   s    r   z/usr/share/crypto-policiesz/etc/crypto-policieszreload-cmds.shz/proc/sys/crypto/fips_enabledc              O   s   t | dtji|—Ž d S )NÚfile)ÚprintÚsysÚstderr)ÚargsÚkwargsr
   r
   r   Úeprint    s    r   Úprofile_dirÚbase_dirzlocal.dz	back-endsÚstatec              C   sr   t jdd�} | jƒ }|jdddddd� |jd	d
dd� |jdd
dd� | jdd
t jd� | jdd
dd� | jƒ S )zParse the command lineF)Zallow_abbrevz--setú?Ú ZPOLICYzset the policy POLICY)ÚnargsÚdefaultÚmetavarÚhelpz--showÚ
store_truez.show the current policy from the configuration)Úactionr   z--is-appliedz+check whether the current policy is appliedz
--no-checkz--no-reloadz3do not run the reload scripts when setting a policy)ÚargparseÚArgumentParserZadd_mutually_exclusive_groupÚadd_argumentZSUPPRESSÚ
parse_args)ÚparserÚgroupr
   r
   r   r"   8   s    
r"   c              C   s€   y0t jt jjtdƒƒj} t jt jjtdƒƒj}W n tk
rN   tj	dƒ Y nX | |krjt
dƒ tj	dƒ t
dƒ tj	dƒ d S )NÚcurrentÚconfigéM   z The configured policy is appliedr   z$The configured policy is NOT appliedé   )ÚosÚstatÚpathÚjoinÚ	state_dirÚst_mtimer   ÚOSErrorr   Úexitr   )Ztime1Ztime2r
   r
   r   Ú
is_appliedI   s    
r1   c               C   s2   yt jtƒ t jtƒ W n tk
r,   Y nX d S )N)r)   ÚmakedirsÚbackend_config_dirr-   r/   r
   r
   r
   r   Úsetup_directoriesW   s
    
r4   c              C   s>   y$t tƒ�} t| jƒ ƒdkS Q R X W n tk
r8   dS X d S )Nr   F)ÚopenÚFIPS_MODE_FLAGÚintÚreadr/   )Úfr
   r
   r   Ú	fips_mode_   s
    
r:   c             C   s¤   t || d�\}}tj|t|dƒƒ tj|ƒ tj|dƒ zZytj|tjj| |ƒƒ W n: t	k
rŽ } ztj
|ƒ tj|ƒ |‚W Y d d }~X nX W d tj|ƒ X d S )N)ÚprefixÚdirzutf-8i¤  )r   r)   ÚwriteÚbytesÚfsyncÚfchmodÚrenamer+   r,   r/   ÚunlinkÚclose)Ú	directoryÚfilenameÚcontentsÚfdr+   Úer
   r
   r   Ú
safe_writeg   s    


rI   c             C   s€   t || d�\}}tj|ƒ tj|ƒ tj||ƒ ytj|tjj| |ƒƒ W n0 tk
rz } ztj|ƒ |‚W Y d d }~X nX d S )N)r;   r<   )	r   r)   rC   rB   ÚsymlinkrA   r+   r,   r/   )rD   rE   ÚtargetrG   r+   rH   r
   r
   r   Úsafe_symlinkv   s    


rL   c          $   C   s|  t jj||d ƒ}ttj|ƒƒ}d}	x|D ]}
t jj|
ƒr*d}	q*W t jj|t| ƒ|d ƒ}t j|t jƒ}|	 r†|r†t	||d |ƒ d S |r²| j
 r²|r²t|ƒ�}|jƒ }W d Q R X t||d |ƒ |	�rxt jj||d ƒ}xœ|D ]”}
y"t|
dƒ�}|jƒ }W d Q R X W n$ tk
�r*   td| ƒ wàY nX y$t|dƒ�}|j|ƒ W d Q R X W qà tk
�rr   td	| ƒ Y qàX qàW d S )
Nz	-*.configFTz.txtz.configÚrzCannot read local policy file r	   z&Error applying local configuration to )r)   r+   r,   ÚsortedÚglobÚexistsr   ÚaccessÚR_OKrL   Úsubpoliciesr5   r8   rI   r/   r   r=   )ÚpconfigZcfgnameZcfgdataZcfgdirZlocaldirZ
profiledirÚpolicy_was_emptyZlocal_cfg_pathZ
local_cfgsZlocal_cfg_presentZlcfgZprofilepathZprofilepath_existsZf_preZcfgfileZlfZ
local_dataZcfr
   r
   r   Úsave_config‚   s:    



rV   c               @   s>   e Zd Zdd„ Zddd„Zdd„ Zdd	„ Zd
d„ Zdd„ ZdS )ÚProfileConfigc             C   s   d| _ g | _d S )Nr   )ÚpolicyrS   )Úselfr
   r
   r   Ú__init__®   s    zProfileConfig.__init__Fc                s`   |j ƒ jdƒ‰ ˆ d r2| r2ˆ d | _ˆ dd … ‰ ‡ fdd„ˆ D ƒ‰ |rV| jjˆ ƒ nˆ | _d S )Nú:r   r(   c                s   g | ]}ˆ r|‘qS r
   r
   )Ú.0Úi)Úlr
   r   ú
<listcomp>·   s    z.ProfileConfig.parse_string.<locals>.<listcomp>)ÚupperÚsplitrX   rS   Úappend)rY   ÚsÚ	subpolicyr
   )r^   r   Úparse_string²   s    
zProfileConfig.parse_stringc             C   sV   d}t |ƒ�@}x8|D ]0}|jddƒd }|jƒ }|r| j||ƒ d}qW W d Q R X d S )NFú#r(   r   T)r5   ra   Ústripre   )rY   rE   rd   r9   Úliner
   r
   r   Ú
parse_file½   s    

zProfileConfig.parse_filec                s(   |j ƒ jdƒ‰ ‡ fdd„| jD ƒ| _d S )Nr[   c                s   g | ]}|ˆ kr|‘qS r
   r
   )r\   r]   )r^   r
   r   r_   É   s    z4ProfileConfig.remove_subpolicies.<locals>.<listcomp>)r`   ra   rS   )rY   rc   r
   )r^   r   Úremove_subpoliciesÇ   s    z ProfileConfig.remove_subpoliciesc             C   s&   | j }dj| jƒ}|r"|d | }|S )Nr[   )rX   r,   rS   )rY   rc   Zsubsr
   r
   r   Ú__str__Ë   s
    zProfileConfig.__str__c             C   s   t t| ƒƒ d S )N)r   r   )rY   r
   r
   r   ÚshowÒ   s    zProfileConfig.showN)F)	r   Ú
__module__Ú__qualname__rZ   re   ri   rj   rk   rl   r
   r
   r
   r   rW   ­   s   

rW   c           )   C   s˜  t ƒ } | jrtƒ  tjdƒ d}tƒ  tƒ }d}tjjt	dƒ}tj
|tjƒrX|j|ƒ n&tƒ rj|jdƒ n|jtjjtdƒƒ | jr–|jƒ  tjdƒ | j}|�r|j}|j|ƒ d}|j|k�r|jdkrætdƒ tdƒ td	ƒ n(tƒ �rtd
ƒ tdƒ tdƒ tdƒ t	tk�r8tjƒ dk�s8tdƒ tjdƒ ytj|jf|jžŽ }W nx tjjk
�rŠ } zt|ƒ tjdƒ W Y dd}~X n@ tjjk
�rÈ } ztd|› �ƒ tjdƒ W Y dd}~X nX tdt|ƒ ƒ dd„ ttƒD ƒ}	x¸|	D ]°}
tj |
 }|ƒ }y|j!|j"|j#ƒƒ}W n0 t$k
�rL   td|j% ƒ tdƒ d}Y nX y t&||j%|t't(t|j)ƒ d� W n0 t*k
�rž   td|j% ƒ tdƒ d}Y nX �qòW |�rèyt+t	dt|ƒd ƒ W n" t*k
�ræ   tdƒ d}Y nX yt+t,dt|ƒd ƒ W n" t*k
�r"   tdƒ d}Y nX yt+t,dt|ƒƒ W n" t*k
�rZ   td ƒ d}Y nX td!ƒ td"ƒ td#ƒ | j-�sŠt.j/d$t0gƒ tj|ƒ dS )%z!The actual command implementationr   Fr&   ZFIPSzdefault-configTzHWarning: Using 'update-crypto-policies --set FIPS' is not sufficient forz         FIPS compliance.z8         Use 'fips-mode-setup --enable' command instead.zOWarning: Using 'update-crypto-policies --set' in FIPS mode will make the systemz!         non-compliant with FIPS.z8         It can also break the ssh access to the system.zI         Use 'fips-mode-setup --disable' to disable the system FIPS mode.z/You must be root to run update-crypto-policies.r(   Nz%Errors found in policy, first one:  
zSetting system policy to c             S   s   g | ]}d |kr|‘qS )Ú	Generatorr
   )r\   Úgr
   r
   r   r_     s    zmain.<locals>.<listcomp>zError generating config for zKeeping original configuration)rU   zError saving config for r   z.Error setting the current policy configurationé   r%   z$Error updating current policy markeré   zCURRENT.polz"Error updating current policy dumpzFNote: System-wide crypto policies are applied on application start-up.zBIt is recommended to restart the system for the change of policieszto fully take place.z	/bin/bash)1r"   r1   r   r0   r4   rW   r)   r+   r,   r   rQ   rR   ri   r:   re   r   rl   ÚsetrX   r   ÚDEFAULT_BASE_DIRÚgeteuidÚcryptopoliciesÚUnscopedCryptoPolicyrS   Z
validationZPolicyFileNotFoundErrorZPolicySyntaxErrorr   r   r<   ÚpolicygeneratorsÚ__dict__Zgenerate_configZscopedZSCOPESÚLookupErrorZCONFIG_NAMErV   r3   Ú	local_dirZis_emptyr/   rI   r-   Z	no_reloadÚ
subprocessZcallÚreload_cmd_path)ZcmdlineÚerrrT   Z
set_configZ
configfileZprofileZ	oldpolicyZcpÚexZ
generatorsrp   ÚclsÚgenr&   r
   r
   r   ÚmainÖ   s¬    














r‚   Ú__main__)(r   r   r)   r|   Ztempfiler   rO   Úwarningsrv   Zcryptopolicies.validationrx   ÚformatwarningZDEFAULT_PROFILE_DIRrt   ZRELOAD_CMD_NAMEr6   r   Úenvironr   rw   Z	SHARE_DIRÚKeyErrorr   Z
CONFIG_DIRr+   r,   r{   r3   r-   r}   r"   r1   r4   r:   rI   rL   rV   rW   r‚   r   r
   r
   r
   r   Ú<module>   sP   




+)s
