o vfK@s&dZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl mZddlmZzddlmZmZmZmZeeefWneybddlmZmZmZmZYnwddlmZddlmZejdd Zd Zd Z d Z!d Z"Gddde j#j$e%Z&ddZ'ddZ(ddZ)ddZ*ddZ+ddZ,ddZ-ddZ.d d!Z/d"d#Z0d$d%Z1d&d'Z2d(d)Z3d*d+Z4d,d-Z5d.d/Z6dOd0d1Z7d2d3Z8dPd5d6Z9de9_:d7d8Z;d9d:ZZ>d?e?d@e@eAeAffdAdBZBdOdCdDZCdQdEdFZDdGdHZEdIdJZFdKdLZGdMdNZHdS)Rz0Functions to manage apport problem report files.N)closing) itemgetter) ConfigParser NoOptionErrorNoSectionErrorMissingSectionHeaderError) ProblemReport)implAPPORT_REPORT_DIRz /var/crashz/var/lib/apport/coredumpz~/.config/apport/settingsz/run/snapd.socketc@seZdZddZddZdS)UHTTPConnectioncCstjj|d||_dS)N localhost)httpclientHTTPConnection__init__path)selfrr2/usr/lib/python3/dist-packages/apport/fileutils.pyr,s zUHTTPConnection.__init__cCs&ttjtj}||j||_dSN)socketAF_UNIX SOCK_STREAMconnectrsock)rrrrrr0s  zUHTTPConnection.connectN)__name__ __module__ __qualname__rrrrrrr *s r cCs<tdtjs dSz tgddkWStyYdSw)z)Check whether crash reporting is enabled.z/usr/bin/whoopsieT)z/bin/systemctlz-qz is-enabledz whoopsie.pathrF)osaccessX_OK subprocesscallOSErrorrrrrallowed_to_report6s r%cCs\|sdS|ds dSd|vrdSdD] }||vrdSq|d}t|dkr*dS|dS)z'Extract the socket from a DBus address.Nzunix:path=/run/user/z../)%,;=) startswithsplitlen) dbus_addrsearchpartsrrrget_dbus_socketBs   r2c Cs|durdSd}t|D]4}|drA|ds|dsq |r$dSt|d}d|vr2|}Wdn1sdirname resolved_dirrrrfind_file_packages   rJcCsz@ttt/}d|}|d||}|jdkr-t|dWdWSWdWdS1s9wYWdSt yJYdSw)zbReturn the data of the given snap. Return None if the snap is not found to be installed. z /v2/snaps/GETresultN) rr SNAPD_SOCKETrequest getresponsestatusjsonloadsr8 Exception)snapcurlresponserrr find_snaps   & rYcCs t|}|j|jkp|jdkS)zACheck whether the report file has already been processed earlier.r)rstatst_atimest_mtimest_size)reportstrrr seen_reports r`cCsd|ddd}d|ddd}tj|r5tj|r5t|}t|}|j|jkr5t|t|d WddS1sFwYdS)Nz %s.upload.r+rz %s.uploadeda)rsplitrrexistsrZr\unlinkr7)r^uploaduploaded report_st upload_strrrmark_report_uploads     "rjcCszd|vr |ddd}ntdt}d|t||f}tjt|}t|d WddS1s6wYdS)NExecutablePath/_z1report does not have the ExecutablePath attributez%s.%s.%s.hangingrb) replace ValueErrorrgeteuidstrrrE report_dirr7)r^pidsubjectuidbaserrrrmark_hanging_processs "rwc Cst|}zt||j|jdfWdStycd}|dkrUt|}|d|zt|}Wn ty@YYdSw|j|jkrHn t d|d8}|dks!|dkr`t |YdSYdSw)zMark given report file as seen.r+ rNg?) rrZutimer\r$r7r8closer[timesleep delete_report)r^r_timeoutr<rrrmark_report_seens.      rc Csfg}ttjtdD]$}ztj|dkr&t|tjtjBr&| |Wq t y0Yq w|S)zCReturn a list with all report files accessible to the calling user.*.crashr) globrrrErrgetsizer R_OKW_OKappendr$reportsrrrrget_all_reportss$  rc Cs<g}tD]}z t|s||WqtyYqw|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. )rr`rr$rrrrget_new_reportss   rc Csg}ttjtdD]<}z1t|}|jdkr>|jdkr>zt |j}|j dr.WWq Wn t y8Ynw| |Wq tyHYq 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). rrguest)rrrrErrrZr]st_uidpwdgetpwuidpw_namer,KeyErrorrr$)rrr_pwrrrget_all_system_reportss&      rcCsddtDS)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). cSsg|]}t|s|qSr)r`).0rrrr 0sz*get_new_system_reports..)rrrrrget_new_system_reports*src Cs^zt|WdSty.t|d}|dWdYdS1s&wYYdSw)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. wrN)rrer$r7truncate)r^r<rrrr}3s   &r}c Cszt}|j|dddgdz#t|d}tt|d}tt}||dkr.WdS|WSttfy<YdSw)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 CrashCounterDate) key_filteriQr) rloadintr{mktimestrptime localtimeror)r^prcount report_timecur_timerrrget_recent_crashes@s  rcCsd|vr |ddd}nd|vr|dddd}ntd|s&t}tjtd |t|f}t j d kr=t |d St t |tj tjBtjBd d S)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. rkrlrmPackageNr+rz7report has neither ExecutablePath nor Package attributez %s.%s.crash3xbiwb)rnr-rorrprrErrrqsysversionr7fdopenO_WRONLYO_CREATO_EXCL)r^rurtrrrrmake_report_fileTs   $rcCstj|sJtjdd|gtjtjdid}|d}|jdkr&gSg}| D]}| dr>| | dddq,|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)stdoutstderrcwdenvrFAILED:r+) rrrdr"PopenPIPE communicatedecode returncode splitlinesr6rrc)sumfilemout mismatchesr;rrrcheck_files_md5ns    rFc CsR|sttd}td|}d}d}d}tjswtddt_zBz"t |tj tj B}t |} t | jrBt|d}|d}Wn ttfyNYnwW|durY|n|durbt|n|durl|w|durvt|wwztj|Wn tyYnwz|rtj||WStj||WSttfy|YSw)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) interpolationrr)rrrrp _config_filern get_configconfigrr7 O_NOFOLLOWO_RDONLYfstatrZS_ISREGst_moderr8IOErrorr$rz read_stringr getbooleangetrr) sectionsettingdefaultrboolhomedircontentsfdr<r_rrrrsR            rcCs&||ddd}t|dS)z7Extracts the starttime from the contents of a stat file)r*N)rfindrr-)rstrippedrrr get_starttimesrcCstd}d}|D]-}|dr t|dkr t|d}q|dr5t|dkr5t|d}q||fS)z;Extracts the uid and gid from the contents of a status fileNzUid:r+zGid:)rr,r.r-r)rreal_uidreal_gidr;rrrget_uid_and_gids rcCsV|D]&}|}t|dkrqt|d}|t|d}||kr(||kr(dSqdS)zSearch for an ID in a map fdr+r*TF)r-r.r)mapfdrur;fields host_starthost_endrrr search_maps  rcCs:td}|}Wd|S1swY|S)zGets the kernel boot idz/proc/sys/kernel/random/boot_idN)r7r8strip)r<boot_idrrr get_boot_ids  r proc_pid_fdreturncstdtdtffdd }tddd|d}|d }Wd n1s%wY|s.iStd d |d DS)zGet the process environ from a proc directory file descriptor. Raises an OSError in case the environ file could not been read. flagsrcstj||dS)Ndir_fd)rr7)rrrrropenersz#get_process_environ..openerenvironzutf-8rn)encodingerrorsrz NcSs g|] }d|vr|ddqS)r)r+)r-)rentryrrrrs z'get_process_environ..)rr7r8rstripdictr-)rr environ_fdrrrrget_process_environsrcCs2|durdSztjd|dWStyYdSw)z;Gets the process path from a proc directory file descriptorNunknownexer)rreadlinkr$rrrrget_process_paths rc Cs|dur d}d}n"|dur+td| }|}Wdn1s"wYt|}|dur3t|}|dddd}|durEt}d||tt|t|f}tj t |}||fS)zGet the path to a core fileNrz /proc/%s/statrlrmrazcore.%s.%s.%s.%s.%s) r7r8rrrnrgetuidrrqrrEcore_dir) rsrru timestampr stat_file stat_contents core_name core_pathrrr get_core_paths$   rc Cst|}g}g}tjtrtjtd}|D]*}z|dd|kr4tjtjt|}| ||gWqt t fy@Yqw|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.)rrar*) rqrrrdrlistdirr-getmtimerEr IndexErrorFileNotFoundError)ru core_files uid_filesr<r{rrrfind_core_files_by_uid,s  r cCstt|}t|tdd}t|tdkr6tt|tdD]}ttj t |dd||dqdSdS)zcRemoves old files from the core directory if there are more than the maximum allowed per uidr+)keyrN) r sortedrr.max_corefiles_per_uidrangerremoverrEr)rur  sorted_filesxrrrclean_core_directoryAsrc Csi}tjd|gtjtjdd}|jD]1}z |dd\}}Wn ty'Yqw|}d|vr1q|d}|dr?q|||<q|j | |j dkrTiS|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)rruniversal_newlinesz=>r+z linux-vdsor() r"rrSTDOUTrr-rorr,rzwaitr)rlibsrr;rHrestrrrshared_librariesOs.      rcCs6t|}||vr dS|D] }||drdSq dS)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' or 'lib.so.' Tz.so.F)rr,)rlibr linked_librrrlinks_with_shared_libraryqsrr)NNF)NNNNN)I__doc__rrr"os.pathr{rrrZrRr http.clientr contextlibroperatorr configparserrrrr ImportErrorproblem_reportrapport.packaging_implr r4rrrrrrrrNrrobjectr r%r2r=rBrJrYr`rjrwrrrrrr}rrrrrrrrrrrrqrrrr rrrrrrrsdP             1    "