o vfh@shdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlmZddlZddlZedZdaddZdkd d Zdld d Zdkd dZdmddZddZddZddZddZddZ ddZ!ddZ"dd Z#dej$ddfd!d"Z%d#d$Z&d%d&Z'd'd(Z(dej$dfd)d*Z)d+d,Z*d-d.Z+dnd/d0Z,dnd1d2Z-d3Z.d4Z/d5Z0d6Z1d7Z2d8Z3d9Z4d:Z5d;Z6dZ9d?d@Z:dAdBZ;dndCdDZdIdJZ?dodpdLdMZ@dNdOZAdPdQZBdRdSZCdndTdUZDdVdWZEdXdYZFdZd[ZGd\d]ZHdqd_d`ZIdadbZJdcddZKdedfZLdndgdhZMdidjZNejOjPZPejOjQZQdS)rz/Convenience functions for use in package hooks.N)implz[^0-9a-zA-Z_.-]cCsLtjddkrt|tr|d}n t|ts|d}td|ddS)z{Generate a valid report key name from a file path. This will replace invalid punctuation symbols with valid ones. r3UTF-8. _) sysversion isinstancebytesdecodeencode_invalid_key_chars_resubreplace)pathr2/usr/lib/python3/dist-packages/apport/hookutils.py path_to_key's    rTFcCs<d|vrdS|s t|}tj|rt|||||dSdS)aAttach file contents if file exists. If key is not specified, the key name will be derived from the file name with path_to_key(). If overwrite is True, an existing key will be updated. If it is False, a new key with '_' appended will be added instead. If the contents is valid UTF-8, or force_unicode is True, then the value will be a string, otherwise it will be bytes. ../N)rosrexists attach_filereportrkey overwrite force_unicoderrrattach_file_if_exists5s rc Cs,z}d|vrWdSt|tjtjBtjB}t|}tj|}|jt |jks-||kr5t |WdSt |j rXt |d}|}Wdn1sRwYnt |WdS|rj|jddd WSz|dWWSty}|YWSwty}z d t|WYd}~Sd}~ww) a Return the contents of the specified path. If the contents is valid UTF-8, or force_unicode is True, then the value will a string, otherwise it will be bytes. Upon error, this will deliver a text representation of the error, instead of failing. rzError: invalid path.zError: path contained symlinks.rbNz#Error: path was not a regular file.rrerrorsError: )ropen O_NOFOLLOWO_RDONLY O_NONBLOCKfstatrrealpathst_inostatcloseS_ISREGst_modefdopenreadstripr UnicodeDecodeError Exceptionstr)rrfdst real_pathfcontentserrr read_fileMs6        r:cCs<|st|}|s||vr|d7}||vs t||d||<dS)aAttach a file to the report. If key is not specified, the key name will be derived from the file name with path_to_key(). If overwrite is True, an existing key will be updated. If it is False, a new key with '_' appended will be added instead. If the contents is valid UTF-8, or force_unicode is True, then the value will a string, otherwise it will be bytes. r)rN)rr:rrrrrrs rc Cst|}|D]R\}}|r||vrq dt|}t|tkr.|dks)|dr.|||<q |rC|d|}|r>|||<n d||<nd||<tj t |j } | |dt|<q dS)a(Attach information about any modified or deleted conffiles. If conffiles is given, only this subset will be attached. If ui is given, ask whether the contents of the file may be added to the report; if this is denied, or there is no UI, just mark it as "modified" in the report. zmodified.conffile.z [deleted]z [inaccessibleznIt seems you have modified the contents of "%s". Would you like to add the contents of it to your bug report?z [modified]zmtime.conffile.N) packagingget_modified_conffilesitemsrtyper3 startswithyesnodatetime fromtimestamprr*st_mtime isoformat) rpackage conffilesuimodifiedrr8rresponsemtimerrrattach_conffiless"     rKcCsrztj|}Wn tyYdSw|D]!}tj|r6|dr6|dd}d|dd}t |||qdS)z3Attach information about any Upstart override filesNz /etc/init/.confz .overrideupstart.) apportr; get_files ValueErrorrrrr?rr)rrEfilesfileoverriderrrrattach_upstart_overridess   rUc Cs|ztj|}Wn tyYdSw|D]}tj|sq|drgtj| dd}d|}z tj tj dd|}Wn"t y`ztj tj dd d|}Wn t y]YYqwYnwt ||||d r|d rtjtj|d }d |}d|}z tj tj dd|}Wn"t yztj tj dd d|}Wn t yYYqwYnwt |||qdS)z9Attach information about a package's session upstart logsNz/usr/share/upstart/sessions/rLz.logrMXDG_CACHE_HOMEupstartHOMEz.cachez/usr/share/applications/z.desktoprzupstart.application.zapplication-%s.log)rOr;rPrQrrrr?basenamerjoinenvironKeyErrorrendswithsplitext)rrErRr7logr desktopnamerrrattach_upstart_logssN         racCs&|ddstdg|d<dSdS)zwAttach information from the kernel ring buffer (dmesg). This will not overwrite already existing information. CurrentDmesgrNdmesgN)getr0root_command_outputrrrr attach_dmesgsrgc Csd}tj|rPt|D]D}tjd||f}t|}t|jr+|jd@dkr,q |dvr1q zt|}Wn t t fyBYq w|rO||d| dd<q dSdS) Nz/sys/class/dmi/idz%s/%sr) subsystemueventzdmi.rr) rrisdirlistdirr(r*r,r-r:OSErrorIOErrorr)rdmi_dirr7pr5valuerrr attach_dmis&   rrcCst|t|ddt|ddt|ddtjdr,tdd g|d <tdd g|d <td g|d<td dg|d<td dg|d<tddg|d<tgd|d<tdg|d<|d}tdd|}tdd|}tdd|}tdd|}tdd|}tdd|}||d<t |d |vrd!|vrd"|d |d!f|d#<t d$rtd$g|d%<t d&rtd&d'g }|r||d(<td&d)g }|r||d*<d+Sd+Sd+S),a~Attach a standard set of hardware-related data to the report, including: - kernel dmesg (boot and current) - /proc/interrupts - /proc/cpuinfo - /proc/cmdline - /proc/modules - lspci -vvnn - lscpi -vt - lsusb - lsusb -v - lsusb -t - devices from udev - DMI information from /sys - prtconf (sparc) - pccardctl status/ident z/proc/interruptsProcInterrupts /proc/cpuinfo ProcCpuinfo /proc/cmdlineProcKernelCmdLinez /sys/bus/pcilspci-vvnnLspciz-vtzLspci-vtlsusbLsusb-vzLsusb-vz-tzLsusb-tsort /proc/modules ProcModules)udevadminfoz --export-dbUdevDbz%/usr/share/apport/dump_acpi_tables.pyacpidumpzID_FS_LABEL=(.*)zID_FS_LABEL=zID_FS_LABEL_ENC=(.*)zID_FS_LABEL_ENC=z by-label/(.*)zby-label/zdmi.sys.vendorzdmi.product.namez%s %s MachineTypeprtconfPrtconf pccardctlstatusPccardctlStatusidentPccardctlIdentN) rgrrrrcommand_outputrererrrcommand_availabler0)rlabelsoutrrrattach_hardwaresL      rc Cst|tjddt|tjddt|dt|ddt|dd tgd |d <td d g|d<tdd g|d<tt|d<g}tjdrwt d }|D]}d|vrg| }| t |dqRWdn1srwY|D]s}d|}tddt|dg||<d|}tddt|g||<td|D]J}tj|rtj|}d|t|f}t|||dqtj|rtj|}t|D]} tj|| } d|t|t| f}t|| |qqqydS)z (loosely based on http://www.alsa-project.org/alsa-info.sh) for systems where alsa-info is not installed (i e, *buntu 12.04 and earlier) z ~/.asoundrc UserAsoundrcz~/.asoundrc.asoundconfUserAsoundrcAsoundconfz/etc/asound.confz/proc/asound/version AlsaVersionrtru)ls-lz /dev/snd/ AlsaDevicesaplayr AplayDevicesarecordArecordDevices PciMultimediaz/proc/asound/cardsz]:rNzCard%d.Amixer.infoamixerz-crzCard%d.Amixer.valuesz/proc/asound/card%d/codec*zCard%d.Codecs.%srzCard%d.Codecs.%s.%s)rrr expanduserrr pci_devicesPCI_MULTIMEDIArr#lstripsplitappendintr3globisfilerYrrkrlrZ) rcardsr4linefieldscardr codecpathcodecnamerrrrattach_alsa_old:sT           rcCstjdrtgd|d<ntjdrtgd|d<nt|tddgtdtd td |d <tjd rGtd dg|d<tjdrTtdg|d<t|t|dS)z5Attach ALSA subsystem information to the report. /usr/sbin/alsa-info)r--stdout --no-uploadAlsaInfo!/usr/share/alsa-base/alsa-info.sh)rrrfuserr}z /dev/dsp*z /dev/snd/*z /dev/seq*AudioDevicesInUsez/usr/bin/pacmdpacmdlist PulseListz/usr/bin/pa-infoPaInfoN)rrrrrrrrrgrfrrr attach_alsags  $   rcCsbdtjvrdStjd}|tjD]}|sqtj||}tj|r.t|tjr.dSqdS)z/Is given command on the executable search path?PATHFT) rr[rpathseprrZraccessX_OK)commandrelementfilenamerrrr~s  rc Cstj}|s d|d<ztj|tj||rtjpd|d}Wnty5}z dt|WYd}~Sd}~ww||d}|j dkrG| } ndt| dt|j  d |} |re| j d d d } | S) a;Try to execute given command (list) and return its stdout. In case of failure, a textual error gets returned. This function forces LC_MESSAGES to C, to avoid translated output in bug reports. If decode_utf8 is True (default), the output will be converted to a string, otherwise left as bytes. C LC_MESSAGESN)stdoutstderrstdinenvr"rsError: command s failed with exit code s: rrr ) rr[copy subprocessPopenPIPErmr3 communicate returncoder0r r ) rinputr keep_locale decode_utf8rspr9rresrrrrs4       rcCstdurdStdkrdStjsdStjdsdSz td\}}Wn t y0YdSwt j ddt |dgdt j t j dat|t(} ||tj|}|D]\}}|tj@rqt|WddSqZqM1svwYdS)Nrz/usr/bin/pkttyagent pkttyagentz --notify-fdz --fallbackF) close_fdsrr)_AGENTrgeteuidrrisattyrrpipe2rmrrr3rr+selectepollregisterEPOLLINpollEPOLLHUP)rwreventsr4 event_typerrr_spawn_pkttyagents@         rcCs$tdurdSttdadSN)r terminatewaitrrrrkill_pkttyagents rcCs,tdkrgStjdrtdgSgS)Nrz/usr/bin/pkexecpkexec)rgetuidrrrrrrr_root_command_prefixs  rcCs.t|ts Jdtt|||d|d}|S)a=Try to execute given command (list) as root and return its stdout. This passes the command through pkexec, unless the caller is already root. In case of failure, a textual error gets returned. If decode_utf8 is True (default), the output will be converted to a string, otherwise left as bytes. zcommand must be a listT)rr)r rrr)rrrroutputrrrres  rec CsZtjtjtjddd}t}ztj|d}t|d}| D]\}}t |ds2Jd| d|tj||fq%| t t||g}||D]I}z"ttj||d } | } Wd n1sswYWn tyYqUwz| d } Wn tyYnw| r| ||<| qUWt|d St|w) a*Execute multiple commands as root and put their outputs into report. command_map is a keyname -> 'shell command' dictionary with the commands to run. They are all run through /bin/sh, so you need to take care of shell escaping yourself. To include stderr output of a command, end it with "2>&1". Just like root_command_output, this passes the command through pkexec, unless the caller is already root. This is preferrable to using root_command_output() multiple times, as that will ask for the password every time. APPORT_DATA_DIRz/usr/share/apportroot_info_wrapperz:script:rr0z(command must be a string (shell command)z%s | cat > %s rNr)rrrZabspathr[rdtempfilemkdtempr#r=hasattrwriter+rrrrr/r0rnr r1shutilrmtree) r command_map wrapper_pathworkdir script_pathscriptkeynamerrr7bufrrrattach_root_command_outputssD      rcCsld}|dur$|jD]}|jddd}||r||7}q |dus|j||jdkr4|SdS)NrNrrr r)rrr searchr+rr)patternprocesslinesrrrr__filter_re_process+s     rcCsl|rtjddd|gtjd}n"tjdr tjgdtjd}ntdtjr1tjgdtjd}t||S) zExtract recent system messages which match a regex. pattern should be a "re" object. By default, messages are read from the systemd journal, or /var/log/syslog; but when giving "path", messages are read from there instead. tail-n10000r/run/systemd/system) journalctlz--systemz--quiet-bz-a/var/log/syslog)rrrr ) rrrrrrrR_OKr)rrrprrr recent_syslog9s    r cCstjd}tj|rt|tjsdS|std}d}t|d}|D]}|j ddd}| |r8||7}q&Wd|S1sDwY|S) alExtract messages from ~/.xsession-errors. By default this parses out glib-style warnings, errors, criticals etc. and X window errors. You can specify a "re" object as pattern to customize the filtering. Please note that you should avoid attaching the whole file to reports, as it can, and often does, contain sensitive and private data. z~/.xsession-errorsrNz^(\(.*:\d+\): \w+-(WARNING|CRITICAL|ERROR))|(Error: .*No Symbols named)|([^ ]+\[\d+\]: ([A-Z]+):)|([^ ]-[A-Z]+ \*\*:)|(received an X Window System error)|(^The error was \')|(^ \(Details: serial \d+ error_code)rrrr N) rrrrrr rcompiler#r r)rrrr7rrrrxsession_errorsLs&      rrh c Gs|stddgSd}tddg}|dD]]}d}d}|dD]8}z |dd \}}Wn ty5Yq w|}|}|d krRt|d d d } | d@d?}q |dkrX|}q |rr|rr||vrr|rg|d7}|tdd|g7}q|S)z9Return a text dump of PCI devices attached to the system.rxryrNz-vvmmnnz N :rClassirSlotz-vvnns)rrrQr0r) pci_classesresultr paragraph pci_classslotrrrqnrrrrus6   rcCs tddgS)z9Return a text dump of USB devices attached to the system.r{r})rrrrr usb_devicess r(cs,t|}rfdd|D}|S|}|S)zFRetrieve a list of files owned by package, optionally matching globpatcsg|] }tj|r|qSr)rfnmatch).0r7globpatrr sz$files_in_package..)r;rP)rEr,rRr#rr+rfiles_in_packages r.cCsdS)ObsoleteNr)rrErrr attach_gconfsr0c Cs&|dd}i}tj}d|d<tjdd|g|tjd}|jD]#}z|dd \}}} | } Wn t y:Yq w| | |i|<q tjdd|gtjd }|jD]:}z|dd \}}} | } Wn t ylYqRw| ||i|dkr|d kr|d vrd } |d||| f7}qR||d<dS)z0Attach user-modified gsettings keys of a schema.GsettingsChangesrNz /nonexistingXDG_CONFIG_HOME gsettingszlist-recursively)rrNrrsorg.gnome.shell)scommand-historys favorite-appszredacted by apportz %s %s %s ) rdrr[rrrrrrrstriprQ setdefault) rschema cur_valuedefaultsrr3r schema_namerrqrrrattach_gsettings_schemas@           r:cCs2t|dD]}tj|dd}t||qdS)z@Attach user-modified gsettings keys of all schemas in a package.z)/usr/share/glib-2.0/schemas/*.gschema.xmlNi)r.rrrYr:)rrE schema_filer6rrrattach_gsettings_packages r<returncCsbtjdsdS|}|r!||}||}d|d|g}nddg}tddg||d <dS) zAttach journal warnings and errors. If the report contains a date, get the journal logs around that date (plus/minus the time_window in seconds). Otherwise attach the latest 1000 journal logs since the last boot. rNz --since=@z --until=@r z --lines=1000r z--priority=warning JournalErrors)rrr get_timestampr)r time_windowcrash_timestamp before_crash after_crashargsrrrattach_journal_errorss   rEcCs`tddg|d<tddg|d<tt|d<t|ddd d D]}|tjvr-tj|||<qd S) z5Attach generic network-related information to report.iprouteIpRouteaddrIpAddr PciNetworkz/etc/network/interfacesIfupdownConfigr) http_proxy ftp_proxyno_proxyN)rr PCI_NETWORKrrr[)rvarrrrattach_networks  rRcCsttd|d<tddtddtddtd g|d <td d g|d <tjdr4tgd}nd}||d<t|ddddS)z5Attach wireless (WiFi) network information to report.zH(NetworkManager|modem-manager|dhclient|kernel|wpa_supplicant)(\[\d+\])?: WifiSyslogz ESSID:(.*)zESSID:zEncryption key:(.*)zEncryption key: zAccess Point: (.*)zAccess Point: iwconfigIwConfigrfkillrRfKillz/sbin/iw)iwregrdN/ACRDAz/var/log/wpa_supplicant.logWpaSupplicantLogrN) r rrrrrrrr)r iw_outputrrr attach_wifis r^cCsvt|ddt|ddtdg|d<tddg|d <td }|r3tgd |}td d ||d<td|d<dS)zkAttach printing information to the report. Based on http://wiki.ubuntu.com/PrintingBugInfoScript. z/etc/papersize Papersizez/var/log/cups/error_log CupsErrorLoglocaleLocalelpstatr}Lpstatz/etc/cups/ppd/*.ppd)fgrepz-Hz *NickNamez*/etc/cups/ppd/(.*).ppd:\*NickName: *"(.*)"z \g<1>: \g<2>PpdFiles) foo2zjsz foomatic-dbzfoomatic-db-enginefoomatic-db-gutenprintzfoomatic-db-hpijszfoomatic-filtersz foomatic-guihpijshplipm2300wmin12xxwc2050hpojpxljrpnm2ppasplixzhp-ppdz hpijs-ppdszlinuxprinting.org-ppdszopenprinting-ppdszopenprinting-ppds-extra ghostscriptcupszcups-driver-gutenprintrh ijsgutenprintzcupsys-driver-gutenprintzgimp-gutenprintzgutenprint-doczgutenprint-localeszsystem-config-printer-commonkdeprintPrintingPackagesN)rrrrrpackage_versions)rppds nicknamesrrrattach_printings   rzc Csht|tr|g}d}t|tj}d}t|tj}d|vr,dt|tdg|d<d|vrAtj drAt |dd|d it |d d t |d d t||dd|ddD]T}|sit|ddSz(|ddkrw|dd}ntjddkrt|jddd}n|jddd}Wn tyYq]w|D]}td|d|rt|dnqq]dS)z0Attach MAC information and events to the report.z!audit\(|apparmor|selinux|securityz'apparmor="DENIED".+?profile=([^ ]+?)[ ]KernLogrrcAuditLogz/var/run/auditd.pidzegrep "z" /var/log/audit/audit.logz/proc/version_signatureProcVersionSignaturerv ProcCmdlinerNapparmorr"rrrrrr hex^$N)r r3rr IGNORECASErZfindallrerrrrrrrd_add_tagrr r fromhexr r2match) rprofiles mac_regexmac_reaa_regexaa_rerprofilesearch_profilerrrattach_mac_events-sF     $    rcCs(|dd}|r |d7}|||d<dS)z#Adds or appends a tag to the reportTagsrNrN)rd)rtag current_tagsrrrrXs rcCst||d<dS)ziAttach version information for related packages In the future, this might also run their hooks. RelatedPackageVersionsN)rw)rpackagesrrrattach_related_packages`src s|sdSg}|D]:}|s qt|}|s||dft|D]"}zt|}Wn ty3d}Ynw|dur:d}|||fqqttddt|D\}}d|d fdd|DS) ztReturn a text listing of package names and versions. Arguments may be package names or globs, e. g. "foo*" rNrZNcSsg|]}tt|qSr)maplen)r*trrrr-sz$package_versions..z %%-%ds %%srcsg|]}|qSrr)r*vfmtrrr-s) r;package_name_globrsorted get_versionrQrmaxziprZ)rversionspackage_patternmatching_packagesrEr package_width version_widthrrrrwhs.    rwcCsztjd|gtjtjd}|dd}|jdkrWdSWn ty)YdSw|D]}|dd}t |d kr=q.|dd krK|d Sq.dS) z-Return the license for a given kernel module.z /sbin/modinfo)rrrrinvalidNrrrlicense) rrrrr rrm splitlinesrrr0)modulemodinforrrrrr_get_module_licenses&       rrcCszt|}dd|D}Wdn1swYWn ty(gYSwg}|D]}t|}|rJd|vsJd|vsJd|vsJd|vsJ||q-|S)zCCheck loaded modules and return a list of those which are not free.cSsg|]}|dqS)r)rr*rrrrr-sz*nonfree_kernel_modules..NGPLBSDMPLMIT)r#rnrr) module_listr7modsnonfreemsrrrnonfree_kernel_moduless  $ rcCsd}t|D]A}tj||}|dkstj|sqt|d}|dkr.|dd}|dkr;t |}|d7}|d ||j d d d f7}q|S) NrNrjrmodes  edidz-base64z%s: %s rrr ) rrlrrZrr#r/r0rbase64 b64encoder )conrr7rvalrrr__drm_con_infos  rcCs`d}tj|s dSt|D]}tj||}tjtj|dr-t||dt|<qdS)zWAdd information about DRM hardware. Collect information from /sys/class/drm/. z/sys/class/drmNenabledzDRM.)rrrkrlrZrrr)rdrm_dirr7rrrrattach_drm_infos rc Cs`tjd}|sItd5}|D]!}|}d|vr0|dr0d|vr0|ddddd}n q WddSWdn1sDwYttj }z;z0zt tj d t t |d }WntywYWt tj |WdSwWt tj |nt tj |wWn tjyYdSwz td |j}W||kSttfyYdSw) zCheck if the problem happened in the currently running XDG session. This can be used to determine if e. g. ~/.xsession-errors is relevant and should be attached. Return None if this cannot be determined. XDG_SESSION_IDz/proc/self/cgroupz name=systemd:z.scopez /session-rNirDatez/run/systemd/sessions/)rr[rdr#r0r]rra getlocaleLC_TIME setlocaletimemktimestrptimer\Errorr*rCrnrm)r session_idr7r orig_ctime report_timesession_start_timerrrin_session_of_problemsB    $rcCspd}|st|}tj|r6t|d}dd|D}d|||<WddS1s/wYdSdS)z;attach /etc/default/grub after filtering out password linesz/etc/default/grubrcSsg|] }|ds |ndqS)passwordz### PASSWORD LINE REMOVED ###)r?rrrrr-s z'attach_default_grub..rNN)rrrrr# readlinesrZ)rrrr7filteredrrrattach_default_grubs  "rcCsvd}g}tj|r&t|dd|id|vr&t|d}|d}|d}||d<|r3d||d<|dd d S) z:attach the results of the casper md5check of install mediaunknown CasperMD5jsonzcat '%s'r#checksum_missmatchCasperMD5CheckResultrCasperMD5CheckMismatchesN)rrrrjsonloadsrZpop)rlocationr# mismatchescheckrrrattach_casper_md5check s r)NTF)F)NNr)r)r=N)r)R__doc__rrrrrArrr*rrrrrarapport.packaging_implrr;rOapport.fileutilsrrrrrr:rrKrUrargrrrrrrSTDOUTrrrrrerrr rPCI_MASS_STORAGErP PCI_DISPLAYr PCI_MEMORY PCI_BRIDGEPCI_SIMPLE_COMMUNICATIONSPCI_BASE_SYSTEM_PERIPHERALSPCI_INPUT_DEVICESPCI_DOCKING_STATIONSPCI_PROCESSORSPCI_SERIAL_BUSrr(r.r0r:r<rErRr^rzrrrrwrrrrrrr fileutilsshared_librarieslinks_with_shared_libraryrrrrs     % ' @- !#  5  !  $   +!  *