HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/apport/__pycache__/fileutils.cpython-38.pyc
U

$�/h�G�@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZz&ddlm
Z
mZmZmZe
eefWn,ek
r�ddl
m
Z
mZmZmZYnXddlmZddlmZej�dd�Zd	Zd
ZdZdd
�Zdd�Zdd�Zdd�Z dd�Z!dd�Z"dd�Z#dd�Z$dd�Z%dd�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/de/_0d3d4�Z1d5d6�Z2d7d8�Z3d9d:�Z4dId;d<�Z5dJd=d>�Z6d?d@�Z7dAdB�Z8dCdD�Z9dEdF�Z:dS)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/settingscCsFt�dtj�sdSzt�ddddg�dkWStk
r@YdSXd	S)
z)Check whether crash reporting is enabled.z/usr/bin/whoopsieTz/bin/systemctlz-qz
is-enabledzwhoopsie.servicerFN)�os�access�X_OK�
subprocess�call�OSError�rr�2/usr/lib/python3/dist-packages/apport/fileutils.py�allowed_to_report$srcCs\|sdS|�d�sdSd|kr"dSdD]}||kr&dSq&|�d�}t|�dkrTdS|dS)z'Extract the socket from a DBus address.Nzunix:path=/run/user/z../)�%�,�;�=��)�
startswith�split�len)Z	dbus_addr�search�partsrrr�get_dbus_socket0s

rc
Cst|dkrdSd}t�|�D]T}|�d�r|�d�s>|�d�s>q|rHdSt|d��}d|��krd|}W5QRXq|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/�rbsNoDisplay=true)�	packagingZ	get_files�endswithr�open�read)�packageZdesktopfile�line�frrr�find_package_desktopfileKs
r(c	CsRddddddddd	g	}d
}|D]}|�|�rd}q6q|oP|�d�oP|�d
�S)a2Check 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_whitelistZwhitelist_match�irrr�likely_packagedgs�
�r+cCsLtj�|�\}}tj�|�}tj�|�r6tj�||�}t|�sBdSt�|�S)z[Return the package that ships the given file.

    Return None if no package ships it.
    N)	r�pathr�realpath�isdir�joinr+r!Zget_file_package)r)�dir�nameZresolved_dirrrr�find_file_package{sr2cCsTt��}z0|�d�|��}|jdkr6|��}|dWSWntk
rNYdSXdS)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_unixsocketZSession�get�formatZstatus_codeZjson�	Exception)ZsnapZsession�r�jrrr�	find_snap�s
r;cCs t�|�}|j|jkp|jdkS)zACheck whether the report file has already been processed earlier.r)r�stat�st_atime�st_mtime�st_size)�report�strrr�seen_report�s
rBc	Cs�d|�dd�d}d|�dd�d}tj�|�rjtj�|�rjt�|�}t�|�}|j|jkrjt�|�t|d��W5QRXdS)Nz	%s.upload�.rrz%s.uploaded�a)�rsplitrr,�existsr<r>�unlinkr#)r@ZuploadZuploadedZ	report_stZ	upload_strrr�mark_report_upload�s


rHc	Csdd|kr|d�dd�}ntd��t��}d|t|�|f}tj�t|�}t|d��W5QRXdS)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,rrr�mark_hanging_process�srUcCs�t�|�}zt�||j|jdf�Wn�tk
r�d}|dkr�t|�}|�d�|��zt�|�}Wntk
r�YYdSX|j|jkr�q�t	�
d�|d8}q:|dkr�t|�YnXdS)zMark given report file as seen.r�rNg�������?)rr<�utimer>rr#r$�closer=�time�sleep�
delete_report)r@rAZtimeoutr'rrr�mark_report_seen�s&




r\c	Csjg}t�tj�td��D]L}z2tj�|�dkrLt�|tjtjB�rL|�	|�Wqt
k
rbYqXq|S)zCReturn a list with all report files accessible to the calling user.�*.crashr)�globrr,r/rP�getsizer�R_OK�W_OK�appendr��reportsr9rrr�get_all_reports�s$rec	Cs@g}t�D]0}zt|�s"|�|�Wq
tk
r8Yq
Xq
|S)z�Get 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.
    )rerBrbrrcrrr�get_new_reports�s
rfc	Cs�g}t�tj�td��D]~}zdt�|�}|jdkr~|jdkr~z"t�	|j�}|j
�d�r\WWqWntk
rrYnX|�
|�Wqtk
r�YqXq|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��Zguest)r^rr,r/rPr<r?�st_uid�pwd�getpwuidZpw_namer�KeyErrorrbr)rdr9rAZpwrrr�get_all_system_reports�s

rlcCsdd�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).
    cSsg|]}t|�s|�qSr)rB)�.0r9rrr�
<listcomp>sz*get_new_system_reports.<locals>.<listcomp>)rlrrrr�get_new_system_reportssrocCsHzt�|�Wn4tk
rBt|d��}|�d�W5QRXYnXdS)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.
    �wrN)rrGrr#�truncate)r@r'rrrr[ s
r[c	Cs�t�}|j|dddgd�zFt|d�}t�t�|d��}t�t���}||dkr\WdS|WSttfk
rzYdSXdS)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�CrashCounterZDate)Z
key_filteri�QrN)	r�load�intrY�mktime�strptime�	localtimerMrk)r@�pr�countZreport_timeZcur_timerrr�get_recent_crashes-srzcCs�d|kr|d�dd�}n&d|kr8|d�dd�d}ntd��|sLt��}tj�td	|t|�f�}t	j
d
krzt|d�St�t�|tj
tjBtjBd�d
�SdS)a6Construct 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.
    rIrJrKZPackageNrrz7report has neither ExecutablePath nor Package attributez%s.%s.crash�3Zxbi��wb)rLrrMrrNr,r/rPrO�sys�versionr#�fdopen�O_WRONLY�O_CREAT�O_EXCL)r@rSrRr,rrr�make_report_fileAs	

r�cCs�tj�|�st�tjdd|gtjtjdid�}|��d��}|j	dkrLgSg}|�
�D]$}|�d�rX|�|�
dd�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�envrZFAILED�:r)rr,rF�AssertionErrorr�Popen�PIPEZcommunicate�decode�
returncode�
splitlinesr"rbrE)Zsumfile�m�outZ
mismatchesr&rrr�check_files_md5[s�

r�Fc
	Cs>|s"t�t���d}t�d|�}d}d}d}tjs�tdd�t_zbzDt�
|tjtjB�}t�
|�}	t�|	j�r�t�|d�}|�d�}Wnttfk
r�YnXW5|dk	r�|�	�n|dk	r�t�	|�Xztj�|�Wntk
r�YnXz*|�rtj�||�WStj�||�WSWnttfk
�r8|YSXdS)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)�
interpolationr9rg)rirjrrN�_config_filerL�
get_config�configrrXr#�
O_NOFOLLOW�O_RDONLY�fstatr<�S_ISREG�st_moderr$�IOErrorrZread_stringrZ
getbooleanr6rr)
ZsectionZsetting�defaultr,�boolZhomedir�contents�fdr'rArrrr�ts<	


r�cCs&||�d�dd�}t|��d�S)z7Extracts the starttime from the contents of a stat file�)rN�)�rfindrtr)r��strippedrrr�
get_starttime�sr�cCstd}d}|��D]Z}|�d�r@t|���dkr@t|��d�}q|�d�rt|���dkrt|��d�}q||fS)z;Extracts the uid and gid from the contents of a status fileNzUid:rzGid:)r�rrrrt)r�Zreal_uidZreal_gidr&rrr�get_uid_and_gid�sr�cCsV|D]L}|��}t|�dkrqt|d�}|t|d�}||kr||krdSqdS)zSearch for an ID in a map fd�rrTF)rrrt)ZmapfdrSr&ZfieldsZ
host_startZhost_endrrr�
search_map�sr�c	Cs$td��}|����}W5QRX|S)zGets the kernel boot idz/proc/sys/kernel/random/boot_id)r#r$�strip)r'Zboot_idrrr�get_boot_id�s
r�cCs8|dkrdSztjd|d�WStk
r2YdSXdS)z;Gets the process path from a proc directory file descriptorN�unknown�exe��dir_fd)r�readlinkr��proc_pid_fdrrr�get_process_path�sr�c	s��fdd�}|dkrd}d}nF|dkrd�dk	r4d}nd|}t||d��}|��}W5QRXt|�}|dkrtt��}|�dd	��d
d	�}|dkr�t��}d||t�t|�t|�f}	tj	�
t|	�}
|	|
fS)zGet the path to a core filecstj||�d�S)Nr�)rr#)r,�flagsr�rr�opener�szget_core_path.<locals>.openerNr�r<z
/proc/%s/stat)r�rJrKrCzcore.%s.%s.%s.%s.%s)r#r$r�r�rLr�getuidr�rOr,r/�core_dir)rQr�rSZ	timestampr�r�Z
stat_filenameZ	stat_fileZ
stat_contentsZ	core_nameZ	core_pathrr�r�
get_core_path�s,
�r�c
Cs�t|�}g}g}tj�t�r(tjtd�}|D]\}z:|�d�d|krhtj�tj�t|��}|�	||g�Wq,t
tfk
r�Yq,Yq,Xq,|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,rCr)rOrr,rFr��listdirr�getmtimer/rb�
IndexError�FileNotFoundError)rSZ
core_files�	uid_filesr'rYrrr�find_core_files_by_uidsr�cCspt|�}t|td�d�}t|�tdkrltt|�td�D].}t�tj�	t
|dd��|�|d�q<dS)zcRemoves old files from the core directory if there are more than
       the maximum allowed per uidr)�keyrN)r��sortedrr�max_corefiles_per_uid�ranger�remover,r/r�)rSr�Zsorted_files�xrrr�clean_core_directory sr�c	Cs�i}tjd|gtjtjdd�}|jD]h}z|�dd�\}}Wntk
rTYq$YnX|��}d|krhq$|��d��}|�d�r�q$|||<q$|j�	�|�
�|jdkr�iS|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�Zuniversal_newlinesz=>rz
linux-vdsor�()rr�r�ZSTDOUTr�rrMr�rrX�waitr�)r,�libsr�r&r1�restrrr�shared_libraries.s,�





r�cCs6t|�}||krdS|D]}|�|d�rdSqdS)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_librrr�links_with_shared_libraryPsr�)N)NNF)N)NNNNN);�__doc__rr^r�os.pathrYrir}r5r<�operatorr�configparserrrrr�ImportErrorZproblem_reportrZapport.packaging_implrr!�environr6rPr�r�r�rrr(r+r2r;rBrHrUr\rerfrlror[rzr�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrr�<module>sX@

	


1

$"