U
    $‰/hæG  ã                   @   sØ  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZ z&ddlmZmZmZmZ eeef W n, ek
rª   ddlmZmZmZmZ Y nX ddlmZ ddlmZ ej dd¡Zd	Zd
ZdZdd„ Zdd„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"dd„ Z#dd„ Z$dd„ Z%dd„ Z&d d!„ Z'd"d#„ Z(d$d%„ Z)d&d'„ Z*d(d)„ Z+d*d+„ Z,dGd,d-„Z-d.d/„ Z.dHd1d2„Z/de/_0d3d4„ Z1d5d6„ Z2d7d8„ Z3d9d:„ Z4dId;d<„Z5dJd=d>„Z6d?d@„ Z7dAdB„ Z8dCdD„ Z9dEdF„ Z:dS )Kz0Functions to manage apport problem report files.é    N)Ú
itemgetter)ÚConfigParserÚNoOptionErrorÚNoSectionErrorÚMissingSectionHeaderError)ÚProblemReport)ÚimplÚAPPORT_REPORT_DIRz
/var/crashz/var/lib/apport/coredumpé   z~/.config/apport/settingsc                   C   sF   t  dt j¡sdS zt ddddg¡dkW S  tk
r@   Y dS X d	S )
z)Check whether crash reporting is enabled.z/usr/bin/whoopsieTz/bin/systemctlz-qz
is-enabledzwhoopsie.servicer   FN)ÚosÚaccessÚX_OKÚ
subprocessÚcallÚOSError© r   r   ú2/usr/lib/python3/dist-packages/apport/fileutils.pyÚallowed_to_report$   s    r   c                 C   s\   | sdS |   d¡sdS d| kr"dS dD ]}|| kr& dS q&|  d¡}t|ƒdkrTdS |d S )z'Extract the socket from a DBus address.Nzunix:path=/run/user/z../)ú%ú,ú;ú=é   é   )Ú
startswithÚsplitÚlen)Z	dbus_addrÚsearchÚpartsr   r   r   Úget_dbus_socket0   s    

r   c              
   C   st   | dkrdS d}t  | ¡D ]T}| d¡r| d¡s>| d¡s>q|rH dS t|dƒ}d| ¡ krd|}W 5 Q R X q|S )z›Return a package's .desktop file.

    If given package is installed and has a single .desktop file, return the
    path to it, otherwise return None.
    Nz.desktopz/etc/xdg/autostartz/usr/share/applications/Úrbs   NoDisplay=true)Ú	packagingZ	get_filesÚendswithr   ÚopenÚread)ÚpackageZdesktopfileÚlineÚfr   r   r   Úfind_package_desktopfileK   s    
r(   c              	   C   sR   ddddddddd	g	}d
}|D ]}|   |¡rd} q6q|oP|   d¡ oP|   d¡ S )a2  Check whether the given file is likely to belong to a package.

    This is semi-decidable: A return value of False is definitive, a True value
    is only a guess which needs to be checked with find_file_package().
    However, this function is very fast and does not access the package
    database.
    z/bin/z/bootz/etc/z/initrdz/libz/sbin/z/optz/usr/z/varFTz/usr/local/z	/var/lib/)r   )ÚfileZpkg_whitelistZwhitelist_matchÚir   r   r   Úlikely_packagedg   s      ÿ
ÿr+   c                 C   sL   t j | ¡\}}t j |¡}t j |¡r6t j ||¡} t| ƒsBdS t | ¡S )z[Return the package that ships the given file.

    Return None if no package ships it.
    N)	r   Úpathr   ÚrealpathÚisdirÚjoinr+   r!   Zget_file_package)r)   ÚdirÚnameZresolved_dirr   r   r   Úfind_file_package{   s    r2   c                 C   sT   t  ¡ }z0| d | ¡¡}|jdkr6| ¡ }|d W S W n tk
rN   Y dS X dS )zbReturn the data of the given snap.

    Return None if the snap is not found to be installed.
    z-http+unix://%2Frun%2Fsnapd.socket/v2/snaps/{}éÈ   ÚresultN)Úrequests_unixsocketZSessionÚgetÚformatZstatus_codeZjsonÚ	Exception)ZsnapZsessionÚrÚjr   r   r   Ú	find_snapŒ   s    
r;   c                 C   s    t  | ¡}|j|jkp|jdkS )zACheck whether the report file has already been processed earlier.r   )r   ÚstatÚst_atimeÚst_mtimeÚst_size)ÚreportÚstr   r   r   Úseen_report›   s    
rB   c              	   C   s„   d|   dd¡d  }d|   dd¡d  }tj |¡rjtj |¡rjt | ¡}t |¡}|j|jk rjt |¡ t|dƒ W 5 Q R X d S )Nz	%s.uploadÚ.r   r   z%s.uploadedÚa)Úrsplitr   r,   Úexistsr<   r>   Úunlinkr#   )r@   ZuploadZuploadedZ	report_stZ	upload_str   r   r   Úmark_report_upload¢   s    


rH   c              	   C   sd   d| kr| d   dd¡}ntdƒ‚t ¡ }d|t|ƒ|f }tj t|¡}t|dƒ W 5 Q R X d S )NÚExecutablePathú/Ú_z1report does not have the ExecutablePath attributez%s.%s.%s.hangingrD   )	ÚreplaceÚ
ValueErrorr   ÚgeteuidÚstrr,   r/   Ú
report_dirr#   )r@   ÚpidÚsubjectÚuidÚbaser,   r   r   r   Úmark_hanging_process¯   s    rU   c                 C   sÀ   t  | ¡}zt  | |j|jd f¡ W n” tk
rº   d}|dkr¦t| ƒ}| d¡ | ¡  zt  | ¡}W n tk
r‚   Y Y dS X |j|jkr’q¦t	 
d¡ |d8 }q:|dkr¶t| ƒ Y nX dS )zMark given report file as seen.r   é   r   Ngš™™™™™¹?)r   r<   Úutimer>   r   r#   r$   Úcloser=   ÚtimeÚsleepÚdelete_report)r@   rA   Ztimeoutr'   r   r   r   Úmark_report_seen¼   s&    




r\   c               	   C   sj   g } t   tj td¡¡D ]L}z2tj |¡dkrLt |tjtjB ¡rL|  	|¡ W q t
k
rb   Y qX q| S )zCReturn a list with all report files accessible to the calling user.ú*.crashr   )Úglobr   r,   r/   rP   Úgetsizer   ÚR_OKÚW_OKÚappendr   ©Úreportsr9   r   r   r   Úget_all_reportsÛ   s    $re   c               	   C   s@   g } t ƒ D ]0}zt|ƒs"|  |¡ W q
 tk
r8   Y q
X q
| S )zGet new reports for calling user.

    Return a list with all report files which have not yet been processed
    and are accessible to the calling user.
    )re   rB   rb   r   rc   r   r   r   Úget_new_reportsê   s    
rf   c               	   C   sœ   g } t   tj td¡¡D ]~}zdt |¡}|jdkr~|jdk r~z"t 	|j¡}|j
 d¡r\W W qW n tk
rr   Y nX |  |¡ W q tk
r”   Y qX q| S )z‹Get all system reports.

    Return a list with all report files which belong to a system user (i. e.
    uid < 500 according to LSB).
    r]   r   éô  Zguest)r^   r   r,   r/   rP   r<   r?   Úst_uidÚpwdÚgetpwuidÚpw_namer   ÚKeyErrorrb   r   )rd   r9   rA   Zpwr   r   r   Úget_all_system_reportsü   s    

rm   c                   C   s   dd„ t ƒ D ƒS )z«Get new system reports.

    Return a list with all report files which have not yet been processed
    and belong to a system user (i. e. uid < 500 according to LSB).
    c                 S   s   g | ]}t |ƒs|‘qS r   )rB   )Ú.0r9   r   r   r   Ú
<listcomp>  s      z*get_new_system_reports.<locals>.<listcomp>)rm   r   r   r   r   Úget_new_system_reports  s    rp   c                 C   sH   zt  | ¡ W n4 tk
rB   t| dƒ}| d¡ W 5 Q R X Y nX dS )zÁDelete the given report file.

    If unlinking the file fails due to a permission error (if report_dir is not
    writable to normal users), the file will be truncated to 0 bytes instead.
    Úwr   N)r   rG   r   r#   Útruncate)r@   r'   r   r   r   r[      s
    r[   c              	   C   s€   t ƒ }|j| dddgd zFt|d ƒ}t t |d ¡¡}t t ¡ ¡}|| dkr\W dS |W S  ttfk
rz   Y dS X dS )zºReturn the number of recent crashes for the given report file.

    Return the number of recent crashes (currently, crashes which happened more
    than 24 hours ago are discarded).
    FÚCrashCounterZDate)Z
key_filteri€Q r   N)	r   ÚloadÚintrY   ÚmktimeÚstrptimeÚ	localtimerM   rl   )r@   ÚprÚcountZreport_timeZcur_timer   r   r   Úget_recent_crashes-  s    r{   c                 C   s¢   d| kr| d   dd¡}n&d| kr8| d  dd¡d }ntdƒ‚|sLt ¡ }tj td	|t|ƒf ¡}t	j
d
krzt|dƒS t t |tjtjB tjB d¡d¡S dS )a6  Construct a canonical pathname for a report and open it for writing

    If uid is not given, it defaults to the effective uid of the current process.
    The report file must not exist already, to prevent losing previous reports
    or symlink attacks.

    Return an open file object for binary writing.
    rI   rJ   rK   ZPackageNr   r   z7report has neither ExecutablePath nor Package attributez%s.%s.crashÚ3Zxbi   Úwb)rL   r   rM   r   rN   r,   r/   rP   rO   ÚsysÚversionr#   ÚfdopenÚO_WRONLYÚO_CREATÚO_EXCL)r@   rS   rR   r,   r   r   r   Úmake_report_fileA  s    	

r„   c                 C   s‚   t j | ¡st‚tjdd| gtjtjdi d}| ¡ d  ¡ }|j	dkrLg S g }| 
¡ D ]$}| d¡rX| | dd¡d ¡ qX|S )	z”Check file integrity against md5 sum file.

    sumfile must be md5sum(1) format (relative to /).

    Return a list of files that don't match.
    z/usr/bin/md5sumz-crJ   )ÚstdoutÚstderrÚcwdÚenvr   ZFAILEDú:r   )r   r,   rF   ÚAssertionErrorr   ÚPopenÚPIPEZcommunicateÚdecodeÚ
returncodeÚ
splitlinesr"   rb   rE   )ZsumfileÚmÚoutZ
mismatchesr&   r   r   r   Úcheck_files_md5[  s      þ

r’   Fc           
   	   C   s>  |s"t  t ¡ ¡d }t d|¡}d}d}d}tjsÊtddt_zbzDt 
|tjtjB ¡}t |¡}	t |	j¡r„t |d¡}| d¡}W n ttfk
rž   Y nX W 5 |dk	r¶| 	¡  n|dk	rÈt 	|¡ X ztj |¡ W n tk
rî   Y nX z*|rtj | |¡W S tj | |¡W S W n ttfk
r8   | Y S X dS )zàReturn a setting from user configuration.

    This is read from ~/.config/apport/settings or path. If bool is True, the
    value is interpreted as a boolean.

    Privileges may need to be dropped before calling this.
    r
   ú~Ú N)Úinterpolationr9   rg   )ri   rj   r   rN   Ú_config_filerL   Ú
get_configÚconfigr   rX   r#   Ú
O_NOFOLLOWÚO_RDONLYÚfstatr<   ÚS_ISREGÚst_moder€   r$   ÚIOErrorr   Zread_stringr   Z
getbooleanr6   r   r   )
ZsectionZsettingÚdefaultr,   ÚboolZhomedirÚcontentsÚfdr'   rA   r   r   r   r—   t  s<    	


r—   c                 C   s&   | |   d¡d d… }t| ¡ d ƒS )z7Extracts the starttime from the contents of a stat fileú)r   Né   )Úrfindru   r   )r¡   Ústrippedr   r   r   Úget_starttime¨  s    r§   c                 C   st   d}d}|   ¡ D ]Z}| d¡r@t| ¡ ƒdkr@t| ¡ d ƒ}q| d¡rt| ¡ ƒdkrt| ¡ d ƒ}q||fS )z;Extracts the uid and gid from the contents of a status fileNzUid:r   zGid:)r   r   r   r   ru   )r¡   Zreal_uidZreal_gidr&   r   r   r   Úget_uid_and_gid´  s    r¨   c                 C   sV   | D ]L}|  ¡ }t|ƒdkrqt|d ƒ}|t|d ƒ }||kr||kr dS qdS )zSearch for an ID in a map fdé   r   r   TF)r   r   ru   )ZmapfdrS   r&   ZfieldsZ
host_startZhost_endr   r   r   Ú
search_mapÄ  s    rª   c               	   C   s$   t dƒ} |  ¡  ¡ }W 5 Q R X |S )zGets the kernel boot idz/proc/sys/kernel/random/boot_id)r#   r$   Ústrip)r'   Zboot_idr   r   r   Úget_boot_idÔ  s    
r¬   c                 C   s8   | dkrdS zt jd| dW S  tk
r2   Y dS X dS )z;Gets the process path from a proc directory file descriptorNÚunknownÚexe©Údir_fd)r   Úreadlinkr   ©Úproc_pid_fdr   r   r   Úget_process_pathÜ  s    r´   c              	      sÊ   ‡ fdd„}| dkrd} d}nF|dkrdˆ dk	r4d}nd|  }t ||d}| ¡ }W 5 Q R X t|ƒ}|dkrttˆ ƒ}| dd	¡ d
d	¡}|dkr˜t ¡ }d||tƒ t| ƒt|ƒf }	tj	 
t|	¡}
|	|
fS )zGet the path to a core filec                    s   t j| |ˆ dS )Nr¯   )r   r#   )r,   Úflagsr²   r   r   Úopenerê  s    zget_core_path.<locals>.openerNr­   r<   z/proc/%s/stat)r¶   rJ   rK   rC   zcore.%s.%s.%s.%s.%s)r#   r$   r§   r´   rL   r   Úgetuidr¬   rO   r,   r/   Úcore_dir)rQ   r®   rS   Z	timestampr³   r¶   Zstat_filenameZ	stat_fileZstat_contentsZ	core_nameZ	core_pathr   r²   r   Úget_core_pathç  s,    
 ÿr¹   c              
   C   sŽ   t | ƒ} g }g }tj t¡r(tjtd}|D ]\}z:| d¡d | krhtj tj t|¡¡}| 	||g¡ W q, t
tfk
r†   Y q,Y q,X q,|S )z¨Searches the core file directory for files that belong to a
       specified uid. Returns a list of lists containing the filename and
       the file modification time.)r,   rC   r   )rO   r   r,   rF   r¸   Úlistdirr   Úgetmtimer/   rb   Ú
IndexErrorÚFileNotFoundError)rS   Z
core_filesÚ	uid_filesr'   rY   r   r   r   Úfind_core_files_by_uid  s    r¿   c                 C   sp   t | ƒ}t|tdƒd}t|ƒtd krltt|ƒt d ƒD ].}t tj 	t
|d d ¡¡ | |d ¡ q<dS )zcRemoves old files from the core directory if there are more than
       the maximum allowed per uidr   )Úkeyr   N)r¿   Úsortedr   r   Úmax_corefiles_per_uidÚranger   Úremover,   r/   r¸   )rS   r¾   Zsorted_filesÚxr   r   r   Úclean_core_directory   s    rÆ   c              	   C   s²   i }t jd| gt jt jdd}|jD ]h}z| dd¡\}}W n tk
rT   Y q$Y nX | ¡ }d|krhq$| ¡ d  ¡ }| d¡r„q$|||< q$|j 	¡  | 
¡  |jdkr®i S |S )	z«Get libraries with which the specified binary is linked.

    Return a library name -> path mapping, for example 'libc.so.6' ->
    '/lib/x86_64-linux-gnu/libc.so.6'.
    ÚlddT)r…   r†   Zuniversal_newlinesz=>r   z
linux-vdsor   ú()r   r‹   rŒ   ZSTDOUTr…   r   rM   r«   r   rX   ÚwaitrŽ   )r,   ÚlibsrÇ   r&   r1   Úrestr   r   r   Úshared_libraries.  s,    þ





rÌ   c                 C   s6   t | ƒ}||krdS |D ]}| |d ¡r dS qdS )zÑCheck if the binary at path links with the library named lib.

    path should be a fully qualified path (e.g. report['ExecutablePath']),
    lib may be of the form 'lib<name>' or 'lib<name>.so.<version>'
    Tz.so.F)rÌ   r   )r,   ÚlibrÊ   Z
linked_libr   r   r   Úlinks_with_shared_libraryP  s    rÎ   )N)NNF)N)NNNNN);Ú__doc__r   r^   r   Úos.pathrY   ri   r~   r5   r<   Úoperatorr   Úconfigparserr   r   r   r   ÚImportErrorZproblem_reportr   Zapport.packaging_implr   r!   Úenvironr6   rP   r¸   rÂ   r–   r   r   r(   r+   r2   r;   rB   rH   rU   r\   re   rf   rm   rp   r[   r{   r„   r’   r—   r˜   r§   r¨   rª   r¬   r´   r¹   r¿   rÆ   rÌ   rÎ   r   r   r   r   Ú<module>   sX   @	

1

$"