U
    i©¹d  ã                   @   s*   d dl mZmZmZ G dd„ deeƒZdS )é    )Ú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   sB   |   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_configr   )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 }t|  |¡dƒT}|D ]H}t|  ¡ ƒdks.| d¡rNq.| ¡  d¡r.|  ¡ }| j|d |d q.W 5 Q R X qW n tk
rš   Y nX d S )	Nú/éÿÿÿÿÚrr   ú#Zincludeé   )Ztags)ÚsplitÚopenÚ	path_joinÚlenÚ
startswithÚlowerr
   Ú	Exception)r   r   ZsshcfgÚtagZcfgfileÚlineZconfargr   r   r   r   0   s    $zSsh.included_configsc              	   C   sÀ   |   d¡}|d r\z&t|  d¡ƒ}| ¡ }W 5 Q R X W qh tk
rX   |  d¡ Y dS X n|d  ¡ }|D ]N}z4|  | d¡d d	¡}|  |¡r¢|  	d
 
|¡¡ W ql tk
r¸   Y qlX qld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getent passwdZstatusz/etc/passwdzCouldn't read /etc/passwdNÚoutputú:é   z.sshz
ls -laZ {})Zexec_cmdr   r   Ú	readlinesr   Z
_log_errorÚ
splitlinesr   Z
path_isdirZadd_cmd_outputÚformatÚ
IndexError)r   Z
users_dataZpasswd_fileZusers_data_linesZusr_lineZhome_dirr   r   r   r   A   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   N)Zsos.report.pluginsr   r   r   r   r   r   r   r   Ú<module>   s   