
­­­­­­­­­­­­­­­­­­
<!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configM   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|rt	||d | d S |r| j
 r|rt|}|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|  wY nX y$t|d}|j| W d Q R X W q tk
rr   td	|  Y qX qW 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rt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 qW |ry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-st.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
