o
    qb-D                     @   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
Z
ddlZddlm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yb   ddlmZmZmZmZ Y nw ddlmZ ddlmZ ejdd	Zd
ZdZ dZ!dZ"G dd dej#j$e%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# Z0d$d% Z1d&d' Z2d(d) Z3d*d+ Z4d,d- Z5dEd.d/Z6d0d1 Z7dFd3d4Z8de8_9d5d6 Z:d7d8 Z;d9d: Z<dGd;d<Z=d=d> Z>d?d@ Z?dAdB Z@dCdD ZAdS )Hz0Functions to manage apport problem report files.    N)closing)
itemgetter)ConfigParserNoOptionErrorNoSectionErrorMissingSectionHeaderError)ProblemReport)implAPPORT_REPORT_DIRz
/var/crashz/var/lib/apport/coredump   z~/.config/apport/settingsz/run/snapd.socketc                   @   s   e Zd Zdd Zdd ZdS )UHTTPConnectionc                 C   s   t jj| d || _d S )NZ	localhost)httpclientHTTPConnection__init__path)selfr    r   2/usr/lib/python3/dist-packages/apport/fileutils.pyr   ,   s   
zUHTTPConnection.__init__c                 C   s&   t  t jt j}|| j || _d S N)socketZAF_UNIXZSOCK_STREAMconnectr   sock)r   r   r   r   r   r   0   s   
zUHTTPConnection.connectN)__name__
__module____qualname__r   r   r   r   r   r   r   *   s    r   c                   C   s<   t dt js	dS z
tg ddkW S  ty   Y dS w )z)Check whether crash reporting is enabled.z/usr/bin/whoopsieT)z/bin/systemctlz-qz
is-enabledzwhoopsie.pathr   F)osaccessX_OK
subprocesscallOSErrorr   r   r   r   allowed_to_report6   s   r"   c              	   C   s   | du rdS d}t | D ]4}|drA|ds|dsq|r$ dS t|d}d| vr2|}W d   n1 s<w   Y  q|S )zReturn 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endswith
startswithopenread)packageZdesktopfilelinefr   r   r   find_package_desktopfileB   s    
r,   c                 C   sD   g d}d}|D ]}|  |rd} nq|o!|  d o!|  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_packaged^   s   
r/   c                 C   sL   t j| \}}t j|}t j|rt j||} t| s!dS t| S )z[Return the package that ships the given file.

    Return None if no package ships it.
    N)	r   r   splitrealpathisdirjoinr/   r$   Zget_file_package)r-   dirnameZresolved_dirr   r   r   find_file_packager   s   
r6   c                 C   s   z@t tt/}d|  }|d| | }|jdkr-t| d W  d   W S W d   W dS 1 s9w   Y  W dS  t	yJ   Y dS w )zbReturn the data of the given snap.

    Return None if the snap is not found to be installed.
    z
/v2/snaps/ZGET   resultN)
r   r   SNAPD_SOCKETZrequestZgetresponsestatusjsonloadsr(   	Exception)ZsnapcZurlZresponser   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   
rF   c                 C   s   d|  ddd  }d|  ddd  }tj|r5tj|r5t| }t|}|j|jk r5t| t|d	 W d    d S 1 sFw   Y  d S )Nz	%s.upload.   r   z%s.uploadeda)rsplitr   r   existsr@   rB   unlinkr'   )rD   ZuploadZuploadedZ	report_stZ	upload_str   r   r   mark_report_upload   s   


"rM   c                 C   sz   d| v r| d  dd}ntdt }d|t||f }tjt|}t|d	 W d    d S 1 s6w   Y  d S )NExecutablePath/_z1report does not have the ExecutablePath attributez%s.%s.%s.hangingrI   )	replace
ValueErrorr   geteuidstrr   r3   
report_dirr'   )rD   pidsubjectuidbaser   r   r   r   mark_hanging_process   s   "rZ   c                 C   s   t | }zt | |j|jd f W dS  tyc   d}|dkrUt| }|d |  zt | }W n ty@   Y Y dS w |j|jkrHnt	
d |d8 }|dks!|dkr`t|  Y dS Y dS w )zMark given report file as seen.rH      r   Ng?)r   r@   utimerB   r!   r'   r(   closerA   timesleepdelete_report)rD   rE   Ztimeoutr+   r   r   r   mark_report_seen   s.   


ra   c               	   C   sf   g } t  tjtdD ]$}ztj|dkr&t|tjtjB r&| 	| W q t
y0   Y qw | S )zCReturn a list with all report files accessible to the calling user.*.crashr   )globr   r   r3   rU   getsizer   R_OKW_OKappendr!   reportsrr   r   r   get_all_reports   s   $
rk   c               	   C   s<   g } t  D ]}zt|s| | W q ty   Y qw | 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.
    )rk   rF   rg   r!   rh   r   r   r   get_new_reports   s   

rl   c               	   C   s   g } t  tjtdD ]<}z1t|}|jdkr>|jdk r>zt	|j}|j
dr.W W qW n	 ty8   Y nw | | W q tyH   Y qw | S )zGet all system reports.

    Return a list with all report files which belong to a system user (i. e.
    uid < 500 according to LSB).
    rb   r     Zguest)rc   r   r   r3   rU   r@   rC   st_uidpwdgetpwuidZpw_namer&   KeyErrorrg   r!   )ri   rj   rE   Zpwr   r   r   get_all_system_reports   s&   

rr   c                   C   s   dd t  D S )zGet 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   )rF   ).0rj   r   r   r   
<listcomp>  s    z*get_new_system_reports.<locals>.<listcomp>)rr   r   r   r   r   get_new_system_reports  s   ru   c                 C   s^   zt |  W dS  ty.   t| d}|d W d   Y dS 1 s&w   Y  Y dS w )zDelete 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   rL   r!   r'   truncate)rD   r+   r   r   r   r`     s   &r`   c              	   C   sz   t  }|j| dddgd z#t|d }tt|d }tt }|| dkr.W dS |W S  ttfy<   Y dS w )zReturn 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_filteriQ r   )	r   loadintr^   mktimestrptime	localtimerR   rq   )rD   prcountZreport_timeZcur_timer   r   r   get_recent_crashes%  s   r   c                 C   s   d| v r| d  dd}nd| v r| d ddd }ntd|s&t }tjtd	|t|f }t	j
d
kr=t|dS tt|tjtjB tjB d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.
    rN   rO   rP   ZPackageNrH   r   z7report has neither ExecutablePath nor Package attributez%s.%s.crash3xbi  wb)rQ   r0   rR   r   rS   r   r3   rU   rT   sysversionr'   fdopenO_WRONLYO_CREATO_EXCL)rD   rX   rW   r   r   r   r   make_report_file9  s   	

$r   c                 C   s   t j| sJ tjdd| gtjtjdi d}| d  }|jdkr&g S g }|	 D ]}|
dr>||ddd  q,|S )	zCheck 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-crO   )stdoutstderrcwdenvr   ZFAILED:rH   )r   r   rK   r   PopenPIPEZcommunicatedecode
returncode
splitlinesr%   rg   rJ   )ZsumfilemoutZ
mismatchesr*   r   r   r   check_files_md5S  s   

r   Fc           
   	   C   sN  |st t d }td|}d}d}d}tjsut t_zBz"t	|tj
tjB }t|}	t|	jr@t|d}|d}W n ttfyL   Y nw W |durW|  n|dur`t| n|durj|  w |durtt| w w ztj| W n	 ty   Y nw z|rtj| |W S tj| |W S  ttfy   | Y S w )zReturn 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   ~ Nrj   rm   )ro   rp   r   rS   _config_filerQ   
get_configconfigr   r'   
O_NOFOLLOWO_RDONLYfstatr@   S_ISREGst_moder   r(   IOErrorr!   r]   Zread_stringr   Z
getbooleangetr   r   )
sectionZsettingdefaultr   boolZhomedircontentsfdr+   rE   r   r   r   r   l  sR   	




r   c                 C   s&   | |  dd d }t| d S )z7Extracts the starttime from the contents of a stat file)   N   )rfindrz   r0   )r   strippedr   r   r   get_starttime  s   r   c                 C   st   d}d}|   D ]-}|dr t| dkr t| d }q|dr5t| dkr5t| d }q||fS )z;Extracts the uid and gid from the contents of a status fileNzUid:rH   zGid:)r   r&   lenr0   rz   )r   Zreal_uidZreal_gidr*   r   r   r   get_uid_and_gid  s   r   c                  C   s:   t d} |   }W d   |S 1 sw   Y  |S )zGets the kernel boot idz/proc/sys/kernel/random/boot_idN)r'   r(   strip)r+   Zboot_idr   r   r   get_boot_id  s   

r   c                 C   s   | du r	d} d}n"|du r+t d|  }| }W d   n1 s"w   Y  t|}|du r2d}n
|dddd}|du rDt }d||t t| t|f }tj	t
|}||fS )zGet the path to a core fileNunknownz/proc/%s/statrO   rP   rG   zcore.%s.%s.%s.%s.%s)r'   r(   r   rQ   r   getuidr   rT   r   r3   core_dir)rV   exerX   Z	timestampZ	stat_fileZstat_contentsZ	core_nameZ	core_pathr   r   r   get_core_path  s$   

r   c              
   C   s   g }g }t jtrt jtd}|D ],}z|dd t| kr2t jt jt|}|	||g W q t
tfy>   Y qw |S )zSearches 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   rG   r   )r   r   rK   r   listdirr0   rT   getmtimer3   rg   
IndexErrorFileNotFoundError)rX   Z
core_files	uid_filesr+   r^   r   r   r   find_core_files_by_uid  s   r   c                 C   st   t | }t|tdd}t|td kr6tt|t d D ]}ttj	t
|d d  ||d  qdS dS )zcRemoves old files from the core directory if there are more than
       the maximum allowed per uidrH   )keyr   N)r   sortedr   r   max_corefiles_per_uidranger   remover   r3   r   )rX   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 ]1}z
|dd\}}W n	 ty'   Y qw | }d|v r1q| d  }|dr?q|||< q|j	  |
  |jdkrTi S |S )	zGet 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=>rH   z
linux-vdsor   ()r   r   r   ZSTDOUTr   r0   rR   r   r&   r]   waitr   )r   libsr   r*   r5   restr   r   r   shared_libraries  s.   




r   c                 C   s6   t | }||v r
dS |D ]}||d r dS qdS )zCheck 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_library&  s   r   r   )NNF)NNNN)B__doc__r   rc   r   os.pathr^   ro   r   r@   r;   r   Zhttp.clientr   
contextlibr   operatorr   configparserr   r   r   r   ImportErrorZproblem_reportr   Zapport.packaging_implr	   r$   environr   rU   r   r   r   r9   r   r   objectr   r"   r,   r/   r6   r?   rF   rM   rZ   ra   rk   rl   rr   ru   r`   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s\    P	

1
"