3 Qf4@s2ddlmZdddddgZddlmZmZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZdd lmZmZed d Zed d ZiZe jdBkrded<e jfeedee jeeddZy ejZWnek rej ZYnXddlmZddlm Z m!Z!m"Z"m#Z#ddl$TddlmZddl$m%Z%ddl&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-ddl.m/Z/m0Z0m1Z1m2Z2ddl3m4Z4m5Z5m6Z6ddl7m8Z8m9Z9m:Z:m;Z;mZ>eZfdCd@dZgehdAkr.egdS)D)absolute_importRunFaultServerClientConnectionHandlerget_host_databasesend_alert_notificationConnectionPool)GObjectGLibN)parse_config_setting get_configZgeneralZi18n_text_domainZi18n_locale_dirTZunicode)domain localedir)r rZfallback) ServerAccess)PluginReportReceiverSETroubleshootDatabaseTestPluginReportReceiver AnalyzeThread)*)r )AuditRecordReceiver) ProgramErrorERR_NOT_AUTHENTICATEDERR_USER_LOOKUPERR_USER_PROHIBITEDERR_USER_PERMISSION ERR_FILE_OPENERR_DATABASE_NOT_FOUND) RpcChannelConnectionStateget_socket_list_from_configListeningServer)SETroubleshootServerInterface%SETroubleshootDatabaseNotifyInterfaceSEAlertInterface) get_hostnamemake_database_filepath!assure_file_ownership_permissions get_identitylog_init log_debug syslog_tracesetroubleshootd_logcCs8td|ddlj}|tjkr4td|jdSdS)Nzreceived signal=%srzreloading configuration file)r)setroubleshoot.configconfigsignalSIGHUPZ config_init)signumframer-r2/usr/lib/python3.6/server.py sighandlerms    r4cCs(td|tjtjdtjddS)Nzreceived signal=%sz=/sys/fs/selinux/policy is in use by another process. Exiting!)r)syslogLOG_ERRos_exit)r0r1r2r2r3polling_failed_handlervs r:cCs0ddl}t}tj}t|j}d|||fS)Nrz%s:%s:%s)timer$r8getpidstr)r;ZhostnamepidZstampr2r2r3make_instance_id}s  r?cCstS)N) host_databaser2r2r2r3rsZ system_dbusZbus_name object_pathZ interfacecCsTtjjttd}|j|j|j|jtj |xt j dD]}|j |q>WdS)Nalertsealert) dbusZlowlevelZ SignalMessagedbus_system_object_pathdbus_system_interfaceappendlevellocal_id system_busZ send_messageconnection_poolclientsrB)siginforBclientr2r2r3rs    ZemailZrecipients_filepathpkg_name pkg_version rpc_versionc@s8eZdZddZddZddZd dd Zd d d ZdS)rcCs i|_dS)N) client_pool)selfr2r2r3__init__szConnectionPool.__init__cCs(||jkrtd|dSd|j|<dS)Nz.add_client: client (%s) already in client pool)rRr))rShandlerr2r2r3 add_clients  zConnectionPool.add_clientcCs&||jkrtd|dS|j|=dS)Nz-remove_client: client (%s) not in client pool)rRr))rSrUr2r2r3 remove_clients  zConnectionPool.remove_clientNccs4x.|jD]$}|dkr|Vq|j|kr|VqWdS)N)rR channel_type)rSrXrNr2r2r3rLs   zConnectionPool.clientscCs&x |jD]}|j|kr|jqWdS)N)rRrXZclose_connection)rSrXrNr2r2r3 close_alls  zConnectionPool.close_all)N)N)__name__ __module__ __qualname__rTrVrWrLrYr2r2r2r3rs  cs(eZdZfddZfddZZS)AlertPluginReportReceivercstt|j|dS)N)superr]rT)rSdatabase) __class__r2r3rTsz"AlertPluginReportReceiver.__init__c s^tt|j|}tdk rg}xHtjD]>}d|j}|j||j}|dkr$td|j |j |jq$Wt |rddl m }||||jjtdddlm}tjtj|jtd|jx&|jjD]}|jd kr|jd } PqWtd d trtjj|j | t!d xB|j"D]8} | j#dddkr2q|j| j#}|dkr|SqWt$||S)Nzemail:%signorezEmail: siginfo.sig=%sr) email_alertzsending alert to all clients) html_to_textz1 For complete SELinux messages run: sealert -l %sAVCr>r+Zlog_full_report)Z OBJECT_PIDZSYSLOG_IDENTIFIERzemail:)%r^r]report_problememail_recipientsZrecipient_listZaddressevaluate_filter_for_user filter_typer)sigrGlenZsetroubleshoot.email_alertrbr_Z mark_modifiedZsetroubleshoot.html_utilrcr6r7summary_rI audit_eventrecords record_typefieldsr boolsystemdZjournalsendZ format_textrOZusersusernamer) rSrMZto_addrsZ recipientruactionrbrc audit_recordr>u)r`r2r3rfs<             z(AlertPluginReportReceiver.report_problem)rZr[r\rTrf __classcell__r2r2)r`r3r]s r]c@s$eZdZddZddZddZdS)rcCs*tj|d|j|_|jjd|jdS)NrCZchanged)rrTcopysocket_addressconnection_stateZconnecton_connection_state_change)rSr{r2r2r3rTs  z ClientConnectionHandler.__init__cCsTtd|jj||j||j||jf|tj@r|D]6} | j |krPq@| j ||kr@|j | j | j| jfq@W|S)Ni@Br)r'rrrsetroubleshootutilZ TimeStampfloatrsignature_listlast_seen_daterhrGrIrl report_count) rSsincer alert_actionrur_Z since_alertsdatabase_alertsZalertsrBr2r2r3_get_all_alerts_since s   z/SetroubleshootdDBusObject._get_all_alerts_sincetza(ssi)cCs |j||S)N)r)rSrrr2r2r3get_all_alerts_sincesz.SetroubleshootdDBusObject.get_all_alerts_sincercCs |jd|S)a Return array of *local_id*'s, *summary*'s, and *report_count*'s of all current alerts in a setroubleshoot database returns list of: * `local_id(s)`: a report id in a setroubleshoot database * `summary(s)`: a brief description of an alert. E.g. `"SELinux is preventing /usr/bin/bash from ioctl access on the unix_stream_socket unix_stream_socket."` * `report_count(i)`: count of reports of this alert r)r)rSrr2r2r3get_all_alertss z(SetroubleshootdDBusObject.get_all_alertscCs|jd|ddS)a Return array of *local_id*'s, *summary*'s, and *report_count*'s of all alerts which a user set to be ignored by a user returns list of: * `local_id(s)`: a report id in a setroubleshoot database * `summary(s)`: a brief description of an alert. E.g. `"SELinux is preventing /usr/bin/bash from ioctl access on the unix_stream_socket unix_stream_socket."` * `report_count(i)`: count of reports of this alert rra)r)r)rSrr2r2r3get_all_alerts_ignored(s z0SetroubleshootdDBusObject.get_all_alerts_ignoredcCsFy|j|}Wn&tk r4}z |WYdd}~XnX|jj}|S)N)rrr__next__)rSrIr_rrrBr2r2r3 _get_alert4s  z$SetroubleshootdDBusObject._get_alertzssiasa(ssssbbi)ttsc Cst|jj|}t}|j||}|j|jj}dd|D}|j\}} g} xZ| D]R\} } | j |j | j || |j | j || |j | j || | j| j| j| jfqVW|j|j|j|| t|jjddt|jjdd|jpdfS)ar Return an alert with summary, audit events, fix suggestions ##### arguments * `local_id(s)`: an alert id ##### return values * `local_id(s)`: an alert id * `summary(s)`: a brief description of an alert. E.g. `"SELinux is preventing /usr/bin/bash from ioctl access on the unix_stream_socket unix_stream_socket."` * `report_count(i)`: count of reports of this alert * `audit_event(as)`: an array of audit events (AVC, SYSCALL) connected to the alert * `plugin_analysis(a(ssssbb)`: an array of plugin analysis structure * `if_text(s)`: * `then_text(s)` * `do_text(s)` * `analysis_id(s)`: plugin id. It can be used in `org.fedoraproject.SetroubleshootFixit.run_fix()` * `fixable(b)`: True when an alert is fixable by a plugin * `report_bug(b)`: True when an alert should be reported to bugzilla * `priority(i)`: An analysis priority. Typically the value is between 1 - 100. * `first_seen_date(t)`: when the alert was seen for the first time, number of microseconds since the Epoch * `last_seen_date(t)`: when the alert was seen for the last time, number of microseconds since the Epoch * `level(s)`: "green", "yellow" or "red" cSsg|] }|jqSr2)Zto_text).0Zeventr2r2r3 ^sz7SetroubleshootdDBusObject.get_alert..z%si@Br)r'rrrrZ%update_derived_template_substitutionsrnroZ get_pluginsrGZ substituteZ get_if_textZ get_then_textZ get_do_textZ analysis_idZfixableZ report_bugZpriorityrIrlrintZfirst_seen_dateformatrrH) rSrIrrur_rBrZ audit_eventsZtotal_priorityZ alert_pluginsZpluginsZpluginargsr2r2r3 get_alert<s,  z#SetroubleshootdDBusObject.get_alertbc CsXyHt|jj|}t}|j||}ddlm}|j|j|||ddSdSdS)z Sets a filter on an alert. The alert can be "always" filtered, "never" filtered or "after_first" filtered. ##### arguments * `local_id(s)`: an alert id * `filter_type(s)`: "always", "never", "after_first" ##### return values * `success(b)`: r)map_filter_name_to_valueNTF) r'rrrrsetroubleshoot.signaturerrrj)rSrIrirrur_rBrr2r2r3rts  z$SetroubleshootdDBusObject.set_filterc Cs2y"t}|j||}|j|jdSdSdS)zz Deletes an alert from the database. ##### arguments * `local_id(s)`: an alert id ##### return values * `success(b)`: TFN)rrrrj)rSrIrr_rBr2r2r3 delete_alerts   z&SetroubleshootdDBusObject.delete_alert)rrc Cst|}|jd|jd7_td||jfx^|jjt|D]J\}}}}}t|||||}|jx"|jj|D]}|j t |qxWqDWx\|jj dD]L}y|j t |Wqt k r} zt j t jd| WYdd} ~ XqXqW|jd8_|j|jtdS)Nrr5z#dbus avc(%s) called: %d ConnectionszUnable to add audit event: %srd)r=rrr)rZfeedZ AuditRecordZaudispd_rectifyrrrdflushrr6r7rrm) rSrrpZevent_idZ body_textrqZ line_numberrwrnrr2r2r3rs"  ( zSetroubleshootdDBusObject.avccCs,|jd8_td|j|j|jdS)Nr5z*dbus iface finish() called: %d Connectionsr)rr)rr)rSr2r2r3finishs z SetroubleshootdDBusObject.finishcCs|jdkrtj|dS)Nr)rr.r)rSrr2r2r3rs zSetroubleshootdDBusObject.alarmN)r)r)r)rZr[r\rTrrDrr.rFrrBmethodrrrrrrrrrrrrrr2r2r2r3rs"    8rcCs|jS)N)r)ar2r2r3rsrc@seZdZddZddZdS)SetroubleshootdDBuscCsdy&tdtttftt||||_Wn8tk r^}ztjtjd||WYdd}~XnXdS)Nz=creating system dbus: bus_name=%s object_path=%s interface=%sz$cannot start system DBus service: %s) r)dbus_system_bus_namerErFrdbus_obj Exceptionr6r7)rSrrrrr2r2r3rTs zSetroubleshootdDBus.__init__cCs|jjddS)Nzdaemon requestT)rr)rSr2r2r3 do_restarts zSetroubleshootdDBus.do_restartN)rZr[r\rTrr2r2r2r3rsrcCs|jtjdS)N)save audit2whyr)r_r2r2r3goodbyesrcCstdtjdS)Nz SIGALRM raised in RunFaultServer)r) main_loopquit)r0r1r2r2r3 alarm_handlersrrcCstj|tjtjtxytjtjdPWqtk rf}zdt|krRw|WYdd}~Xqtk r}z dtt |ddkrw|WYdd}~XqXqWtjtjt tjtj t yt jttt}tdd}t|}t|ddt||td d atj|tjttd }xtjjD]}tjt|jj t|jj!t|jj"|jj#\}}|tj$ksx|tj%kr,|tj$krd } nd } t j t j&d|j'| fd}tj(|jq,W|rtj)ddtddt*st+t} nt,t} ddl-} | j.j/j0da1t2t1|} | j3d| j4ddl5m6} | a7tt8ddyt7j9t8Wn@t:k r}z"|j;tn|WYdd}~XnXt?d}x |D]}t@|tA}|jBqWtCjDjEtFt1| |}tGjHWntIk r}zt=dWYdd}~XntJk r.}zt=dWYdd}~XnVtKk r}z8ddlL}tM|jNt j t j&d|jOjPt|fWYdd}~XnXdS)Nrz%unable to open /sys/fs/selinux/policy __context__rr_filenameirzAudit Listener)Z friendly_nameFZallowedz dontaudit'dz-Deleting alert %s, it is %s in current policyT)ZpruneZtestanalyze)SEEmailRecipientSetZlisten_for_clientz#KeyboardInterrupt in RunFaultServerz$raising SystemExit in RunFaultServerzexception %s: %s)Qr.rSIGALRMr:rZinitrr= SystemErrorgetattrrr/r4r6ZopenlogrOrrKr r%r&rrmr@Z set_notifyatexitregisterrrrrrjZscontextZtcontextZtclassrZALLOWZ DONTAUDITr7rIrrrrr]rZsix.moves.queueZmovesrZQueuerrZ setDaemonrrrrgrZparse_recipient_filererrnorr)strerrorrr rrrDZglibZ init_threadsrrZrunKeyboardInterrupt SystemExitr tracebackr* format_excr`rZ)rrZclient_notifierZdatabase_filenameZdatabase_filepathZdeletediZwhyZboolsrrZsixZanalyze_threadrZlisten_addressesZlisten_addressZlistening_serverZsetroubleshootd_dbusrr2r2r3rs         0               __main__)r )r)iZ __future__r__all__Z gi.repositoryrr rDZ dbus.serviceZ dbus.glibgettextr8r.rsysr6Zsystemd.journalrsr,r r r rkwargs version_infoZinstallZ translationZugettextrmAttributeErrorZsetroubleshoot.access_controlrZsetroubleshoot.analyzerrrrZsetroubleshoot.avc_auditrZsetroubleshoot.errcoderrrrrrrZsetroubleshoot.rpcrrrr Zsetroubleshoot.rpc_interfacesr!r"r#Zsetroubleshoot.utilr$r%r&r'r(r)r*r4r:r?rrrErFrrJZ request_namerr@rrgrrOrPrQZ instance_idobjectrrKr]rrrZsetroubleshoot.audit_datarrrrrrrZselinux.audit2whyrrZMainLooprrrrZr2r2r2r3s         $ $          0