U
    -Ø_g  ã                   @   s>   d dl Z d dlmZ d dlmZmZmZ G dd„ deeƒZdS )é    N©Úglob)ÚPluginÚIndependentPluginÚ	PluginOptc                   @   sB   e Zd ZdZdZdZeddeddgZdd	„ Z	d
d„ Z
dd„ ZdS )ÚSshzSecure shell serviceZssh)ÚservicesZsecurityÚsystemZidentityÚ	userconfsTz5Changes whether module will collect user .ssh configs)ÚdefaultZval_typeZdescc                 C   sD   |   dddœ¡ dddg}|  |¡ |  |¡ |  d¡r@|  ¡  d S )NZsshd_configZ
ssh_config)z/etc/ssh/sshd_config$z/etc/ssh/ssh_config$z/etc/ssh/ssh_configz/etc/ssh/sshd_configz/etc/ssh/sshd_config.d/*r
   )Zadd_file_tagsÚadd_copy_specÚincluded_configsZ
get_optionÚuser_ssh_files_permissions)ÚselfÚsshcfgs© r   ú8/usr/lib/python3/dist-packages/sos/report/plugins/ssh.pyÚsetup   s    þý


z	Ssh.setupc              
   C   s¼   z¢dd„ dd„ |D ƒD ƒ}|D ]€}|  d¡d }t|  |¡dddT}|D ]H}t|  ¡ ƒd	ksJ| d
¡rjqJ| ¡  d¡rJ|  ¡ }| j|d |d qJW 5 Q R X qW n tk
r¶   Y nX dS )z Include subconfig files c                 S   s   g | ]}|D ]}|‘qqS r   r   )Ú.0ÚfilesÚfr   r   r   Ú
<listcomp>7   s    þz(Ssh.included_configs.<locals>.<listcomp>c                 S   s   g | ]}t |d d‘qS )T)Ú	recursiver   )r   Zcopyspecr   r   r   r   8   s    ú/éÿÿÿÿÚrúUTF-8©Úencodingr   ú#Zincludeé   )ZtagsN)ÚsplitÚopenÚ	path_joinÚlenÚ
startswithÚlowerr   Ú	Exception)r   r   ZcfgfilesZsshcfgÚtagZcfgfileÚlineZconfargr   r   r   r   3   s(    ÿÿÿ$zSsh.included_configsc           
   	   C   sÐ   t  ¡ }i }zDtdddd,}|D ] }| ¡ dd… \}}|||< q"W 5 Q R X W n  tk
rp   |  d¡ Y dS X d	d
dh}|D ]J}|j|kr²||j |kr²|  d|j› d¡ q€|  |jd¡}	|  	|	¡ q€dS )z 
        Iterate over .ssh folders in user homes to see their permissions.

        Bad permissions can prevent SSH from allowing access to given user.
        z/proc/mountsr   r   r   r    é   zCouldn't read /proc/mountsNZnfsZnfs4ZautofszSkipping capture in z  because it's a remote directoryz.ssh)
ÚpwdZgetpwallr"   r!   r'   Z
_log_errorÚpw_dirZ	_log_infor#   Zadd_dir_listing)
r   Z
users_dataZfs_mount_infoZmounts_filer)   Zfs_fileZ	fs_vstypeZnon_local_fsÚuserZhome_dirr   r   r   r   K   s*    


ÿÿzSsh.user_ssh_files_permissionsN)Ú__name__Ú
__module__Ú__qualname__Z
short_descZplugin_nameZprofilesr   ÚstrZoption_listr   r   r   r   r   r   r   r      s   ÿÿr   )r+   r   Zsos.report.pluginsr   r   r   r   r   r   r   r   Ú<module>   s   