Debian Codegraph @

troubleshoot/CheckSELinux.py

raw
1 #!/usr/bin/python3
2
3 ## Printing troubleshooter
4
5 ## Copyright (C) 2010, 2014 Red Hat, Inc.
6 ## Copyright (C) 2010 Jiri Popelka <[email protected]>
7
8 ## This program is free software; you can redistribute it and/or modify
9 ## it under the terms of the GNU General Public License as published by
10 ## the Free Software Foundation; either version 2 of the License, or
11 ## (at your option) any later version.
12
13 ## This program is distributed in the hope that it will be useful,
14 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ## GNU General Public License for more details.
17
18 ## You should have received a copy of the GNU General Public License
19 ## along with this program; if not, write to the Free Software
20 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
22 from gi.repository import Gtk
23
24 import subprocess
25 from .base import *
26 import os
27 import shlex
28 from timedops import TimedSubprocess
29
30 class CheckSELinux(Question):
31 def __init__ (self, troubleshooter):
32 Question.__init__ (self, troubleshooter, "Check SELinux contexts")
33 troubleshooter.new_page (Gtk.Label (), self)
34
35 def display (self):
36 self.answers = {}
37 #answers = self.troubleshooter.answers
38
39 RESTORECON = "/sbin/restorecon"
40 if not os.access (RESTORECON, os.X_OK):
41 return False
42
43 try:
44 import selinux
45 except ImportError:
46 return False
47 if not selinux.is_selinux_enabled():
48 return False
49
50 paths = ["/etc/cups/", "/usr/lib/cups/", "/usr/share/cups/"]
51 parent = self.troubleshooter.get_window ()
52 contexts = {}
53 new_environ = os.environ.copy()
54 new_environ['LC_ALL'] = "C"
55 restorecon_args = [RESTORECON, "-nvR"].extend(paths)
56 try:
57 # Run restorecon -nvR
58 self.op = TimedSubprocess (parent=parent,
59 args=restorecon_args,
60 close_fds=True,
61 env=new_environ,
62 stdin=subprocess.DEVNULL,
63 stdout=subprocess.PIPE,
64 stderr=subprocess.DEVNULL)
65 (restorecon_stdout, restorecon_stderr, result) = self.op.run ()
66 except:
67 # Problem executing command.
68 return False
69 for line in restorecon_stdout:
70 l = shlex.split (line)
71 if (len (l) < 1):
72 continue
73 contexts[l[2]] = l[4]
74 self.answers['selinux_contexts'] = contexts
75 return False
76
77 def collect_answer (self):
78 return self.answers
79
80 def cancel_operation (self):
81 self.op.cancel ()