o
    vfK                     @   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- Z5d.d/ Z6dOd0d1Z7d2d3 Z8dPd5d6Z9de9_:d7d8 Z;d9d: Z<d;d< Z=d=d> Z>d?e?d@e@eAeAf fdAdBZBdOdCdDZCdQdEdFZDdGdH ZEdIdJ ZFdKdL ZGdMdN ZHdS )Rz0Functions 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 )N	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)socketAF_UNIX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\   | sdS |  dsdS d| v rdS dD ]	}|| v r dS q| d}t|dkr*dS |d S )z'Extract the socket from a DBus address.Nzunix:path=/run/user/z../)%,;=      )
startswithsplitlen)	dbus_addrsearchpartsr   r   r   get_dbus_socketB   s   

r2   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)	packaging	get_filesendswithr,   openread)packagedesktopfilelinefr   r   r   find_package_desktopfile]   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,   )filepkg_whitelistwhitelist_matchir   r   r   likely_packagedy   s   
rB   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   r-   realpathisdirjoinrB   r4   get_file_package)r>   dirnameresolved_dirr   r   r   find_file_package   s   
rJ   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/GET   resultN)
r   r   SNAPD_SOCKETrequestgetresponsestatusjsonloadsr8   	Exception)snapcurlresponser   r   r   	find_snap   s   

&rY   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   
r`   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+   r   z%s.uploadeda)rsplitr   r   existsrZ   r\   unlinkr7   )r^   uploaduploaded	report_st	upload_str   r   r   mark_report_upload   s   


"rj   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.hangingrb   )	replace
ValueErrorr   geteuidstrr   rE   
report_dirr7   )r^   pidsubjectuidbaser   r   r   r   mark_hanging_process   s   "rw   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.r+      r   Ng?)r   rZ   utimer\   r$   r7   r8   closer[   timesleepdelete_report)r^   r_   timeoutr<   r   r   r   mark_report_seen   s.   


r   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   rE   rr   getsizer    R_OKW_OKappendr$   reportsrr   r   r   get_all_reports   s   $
r   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.
    )r   r`   r   r$   r   r   r   r   get_new_reports   s   

r   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).
    r   r     guest)r   r   r   rE   rr   rZ   r]   st_uidpwdgetpwuidpw_namer,   KeyErrorr   r$   )r   r   r_   pwr   r   r   get_all_system_reports  s&   

r   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   )r`   ).0r   r   r   r   
<listcomp>0  s    z*get_new_system_reports.<locals>.<listcomp>)r   r   r   r   r   get_new_system_reports*  s   r   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   re   r$   r7   truncate)r^   r<   r   r   r   r}   3  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CrashCounterDate)
key_filteriQ r   )	r   loadintr{   mktimestrptime	localtimero   r   )r^   prcountreport_time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.
    rk   rl   rm   PackageNr+   r   z7report has neither ExecutablePath nor Package attributez%s.%s.crash3xbi  wb)rn   r-   ro   r   rp   r   rE   rr   rq   sysversionr7   fdopenO_WRONLYO_CREATO_EXCL)r^   ru   rt   r   r   r   r   make_report_fileT  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-crl   )stdoutstderrcwdenvr   FAILED:r+   )r   r   rd   r"   PopenPIPEcommunicatedecode
returncode
splitlinesr6   r   rc   )sumfilemout
mismatchesr;   r   r   r   check_files_md5n  s   

r   Fc           
   	   C   sR  |st t d }td|}d}d}d}tjswtddt_zBz"t	|tj
tjB }t|}	t|	jrBt|d}|d}W n ttfyN   Y nw W |durY|  n|durbt| n|durl|  w |durvt| 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   ~ N)interpolationr   r   )r   r   r   rp   _config_filern   
get_configconfigr   r7   
O_NOFOLLOWO_RDONLYfstatrZ   S_ISREGst_moder   r8   IOErrorr$   rz   read_stringr   
getbooleangetr   r   )
sectionsettingdefaultr   boolhomedircontentsfdr<   r_   r   r   r   r     sR   	




r   c                 C   s&   | |  dd d }t| d S )z7Extracts the starttime from the contents of a stat file)r*   N   )rfindr   r-   )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:r+   zGid:)r   r,   r.   r-   r   )r   real_uidreal_gidr;   r   r   r   get_uid_and_gid  s   r   c                 C   sV   | D ]&}|  }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.   r   )mapfdru   r;   fields
host_starthost_endr   r   r   
search_map  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)r7   r8   strip)r<   boot_idr   r   r   get_boot_id  s   

r   proc_pid_fdreturnc                    st   dt dt f fdd}tddd|d}| d	}W d
   n1 s%w   Y  |s.i S tdd |dD S )zGet the process environ from a proc directory file descriptor.

    Raises an OSError in case the environ file could not been read.
    flagsr   c                    s   t j| | dS )Ndir_fd)r   r7   )r   r   r   r   r   opener  s   z#get_process_environ.<locals>.openerenvironzutf-8rn   )encodingerrorsr   z  Nc                 S   s    g | ]}d |v r| d dqS )r)   r+   )r-   )r   entryr   r   r   r      s     z'get_process_environ.<locals>.<listcomp> )r   r7   r8   rstripdictr-   )r   r   
environ_fdr   r   r   r   get_process_environ  s   r   c                 C   s2   | du rdS zt jd| dW S  ty   Y dS w )z;Gets the process path from a proc directory file descriptorNunknownexer   )r   readlinkr$   r   r   r   r   get_process_path  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 r3t|}|dddd}|du rEt }d||t t| t|f }tj	
t|}||fS )zGet the path to a core fileNr   z/proc/%s/statrl   rm   ra   zcore.%s.%s.%s.%s.%s)r7   r8   r   r   rn   r   getuidr   rq   r   rE   core_dir)	rs   r   ru   	timestampr   	stat_filestat_contents	core_name	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r4t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   ra   r*   )rq   r   r   rd   r   listdirr-   getmtimerE   r   
IndexErrorFileNotFoundError)ru   
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 uidr+   )keyr   N)r  sortedr   r.   max_corefiles_per_uidranger   remover   rE   r   )ru   r
  sorted_filesxr   r   r   clean_core_directoryA  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   universal_newlinesz=>r+   z
linux-vdsor   ()r"   r   r   STDOUTr   r-   ro   r   r,   rz   waitr   )r   libsr  r;   rH   restr   r   r   shared_librariesO  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  
linked_libr   r   r   links_with_shared_libraryq  s   r  r   )NNF)NNNNN)I__doc__r   r   r"   os.pathr{   r   r   rZ   rR   r   http.clientr   
contextlibr   operatorr   configparserr   r   r   r   ImportErrorproblem_reportr   apport.packaging_implr	   r4   r   r   rr   r   r  r   rN   r   r   objectr   r%   r2   r=   rB   rJ   rY   r`   rj   rw   r   r   r   r   r   r}   r   r   r   r   r   r   r   r   r   r   r   rq   r   r   r  r  r  r  r  r   r   r   r   <module>   sd    P	

1

"