Skip to contentSkip to conversation

sepolicy: fix Python3 syntax in manpage

PATCH Applied Nicolas Iooss 2 participants 7 messages Aug 5, 2017 – Aug 7, 2017 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/6 sepolicy: fix Python3 syntax in manpage

Commit c624c4abaaf3 ("sepolicy: Fix syntax errors in 'manpage -w'") missed an occurence of print statement. While at it, fix self.os_version test.

---
 python/sepolicy/sepolicy/manpage.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
python/sepolicy/sepolicy/manpage.py +2 -2
index 4d846364cccf..035065afd292 100755
@@ -192,7 +192,7 @@ class HTMLManPages:
         self.old_path = path + "/"
         self.new_path = self.old_path + self.os_version + "/"
 
-        if self.os_version in fedora_releases or rhel_releases:
+        if self.os_version in fedora_releases or self.os_version in rhel_releases:
             self.__gen_html_manpages()
         else:
             print("SELinux HTML man pages can not be generated for this %s" % os_version)
@@ -262,7 +262,7 @@ Fedora or Red Hat Enterprise Linux Man Pages.</h2>
 </pre>
 	""")
         fd.close()
-        print("%s has been created") % index
+        print("%s has been created" % index)
 
     def _gen_body(self):
         html = self.new_path + self.os_version + ".html"
Signature · 1 line
2.13.3
#1 lore raw

PATCH 2/6 sepolicy: do not fail when file_contexts.local does not exist

When running "sepolicy manpage" on a system without file_contexts.local, the command fails with:

FileNotFoundError: [Errno 2] No such file or directory:
'//etc/selinux/refpolicy/contexts/files/file_contexts.local'

Verify that the file exists before using it.

---
 python/sepolicy/sepolicy/__init__.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
python/sepolicy/sepolicy/__init__.py +4 -3
index 5cfc0715bc90..62158864b7cd 100644
@@ -573,9 +573,10 @@ def get_fcdict(fc_path=selinux.selinux_file_context_path()):
     fc += fd.readlines()
     fd.close()
     fcdict = {}
-    fd = open(fc_path + ".local", "r")
-    fc += fd.readlines()
-    fd.close()
+    if os.path.exists(fc_path + ".local"):
+        fd = open(fc_path + ".local", "r")
+        fc += fd.readlines()
+        fd.close()
 
     for i in fc:
         rec = i.split()
Signature · 1 line
2.13.3
#2 lore raw

PATCH 3/6 sepolicy: fix misspelling of _ra_content_t suffix

In reply to Nicolas Iooss

When "sepolicy manpage" generates descriptions for file type, it uses a mispelled _ra_content_t suffix for prettyprinting, which results in an unwanted double-space on the line.

For example sepolicy manpage -d httpd_t produces:

    .EX
    .P
    .B httpd_apcupsd_cgi_ra_content_t
    .EE

    - Set files with the httpd_apcupsd_cgi_ra_content_t type, if you
    want to treat the files as httpd apcupsd cgi  read/append content.
---
 python/sepolicy/sepolicy/__init__.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
python/sepolicy/sepolicy/__init__.py +1 -1
index 62158864b7cd..2d8e9ef6e862 100644
@@ -956,7 +956,7 @@ def get_description(f, markup=markup):
     if f.endswith("_db_t"):
         return txt + "treat the files as %s database content." % prettyprint(f, "_db_t")
     if f.endswith("_ra_content_t"):
-        return txt + "treat the files as %s read/append content." % prettyprint(f, "_ra_conten_t")
+        return txt + "treat the files as %s read/append content." % prettyprint(f, "_ra_content_t")
     if f.endswith("_cert_t"):
         return txt + "treat the files as %s certificate data." % prettyprint(f, "_cert_t")
     if f.endswith("_key_t"):
Signature · 1 line
2.13.3
#3 lore raw

PATCH 4/6 sepolicy: support non-MLS policy in manpage

In reply to Nicolas Iooss

"sepolicy manpage" fails when the system does not use MLS because using x.context.range_ raises an exception. Avoid using range and levels when _pol.mls is False.

---
 python/sepolicy/sepolicy/__init__.py | 18 +++++++++++++++---
 python/sepolicy/sepolicy/manpage.py  |  3 ++-
 2 files changed, 17 insertions(+), 4 deletions(-)
python/sepolicy/sepolicy/__init__.py +15 -3
index 2d8e9ef6e862..03742346caf0 100644
@@ -207,10 +207,17 @@ def info(setype, name=None):
             elif len(ports) == 1:
                 q.ports = (ports[0], ports[0])
 
+        if _pol.mls:
+            return ({
+                'high': x.ports.high,
+                'protocol': str(x.protocol),
+                'range': str(x.context.range_),
+                'type': str(x.context.type_),
+                'low': x.ports.low,
+            } for x in q.results())
         return ({
             'high': x.ports.high,
             'protocol': str(x.protocol),
-            'range': str(x.context.range_),
             'type': str(x.context.type_),
             'low': x.ports.low,
         } for x in q.results())
@@ -220,11 +227,16 @@ def info(setype, name=None):
         if name:
             q.name = name
 
+        if _pol.mls:
+            return ({
+                'range': str(x.mls_range),
+                'name': str(x),
+                'roles': list(map(str, x.roles)),
+                'level': str(x.mls_level),
+            } for x in q.results())
         return ({
-            'range': str(x.mls_range),
             'name': str(x),
             'roles': list(map(str, x.roles)),
-            'level': str(x.mls_level),
         } for x in q.results())
 
     elif setype == BOOLEAN:
python/sepolicy/sepolicy/manpage.py +2 -1
index 035065afd292..494c34319c26 100755
@@ -84,7 +84,8 @@ def get_all_users_info():
 
     for d in allusers_info:
         allusers.append(d['name'])
-        users_range[d['name'].split("_")[0]] = d['range']
+        if 'range' in d:
+            users_range[d['name'].split("_")[0]] = d['range']
 
     for u in allusers:
         if u not in ["system_u", "root", "unconfined_u"]:
Signature · 1 line
2.13.3
#4 lore raw

PATCH 5/6 sepolicy: support non-MCS policy in manpage

In reply to Nicolas Iooss

This prevents the following exception to occur when using "sepolicy manpage":

Traceback (most recent call last):
File "/bin/sepolicy", line 699, in <module>
args.func(args)
File "/bin/sepolicy", line 359, in manpage
m = ManPage(domain, path, args.root, args.source_files, args.web)
File "/usr/lib/python3.6/site-packages/sepolicy/manpage.py", line 459, in __init__
self.__gen_man_page()
File "/usr/lib/python3.6/site-packages/sepolicy/manpage.py", line 548, in __gen_man_page
self._mcs_types()
File "/usr/lib/python3.6/site-packages/sepolicy/manpage.py", line 978, in _mcs_types
mcs_constrained_type = next(sepolicy.info(sepolicy.ATTRIBUTE, "mcs_constrained_type"))
StopIteration

---
 python/sepolicy/sepolicy/manpage.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
python/sepolicy/sepolicy/manpage.py +4 -1
index 494c34319c26..35b3722dbe45 100755
@@ -975,7 +975,10 @@ All executeables with the default executable label, usually stored in /usr/bin a
 %s""" % ", ".join(paths))
 
     def _mcs_types(self):
-        mcs_constrained_type = next(sepolicy.info(sepolicy.ATTRIBUTE, "mcs_constrained_type"))
+        try:
+            mcs_constrained_type = next(sepolicy.info(sepolicy.ATTRIBUTE, "mcs_constrained_type"))
+        except StopIteration:
+            return
         if self.type not in mcs_constrained_type['types']:
             return
         self.fd.write ("""
Signature · 1 line
2.13.3
#5 lore raw

PATCH 6/6 sepolicy: remove stray space in section "SEE ALSO"

In reply to Nicolas Iooss

The space after "sepolicy(8)" breaks the formatting of section SEE ALSO in man pages generated with sepolicy:

SEE ALSO

    selinux(8), ssh(8), semanage(8), restorecon(8), chcon(1), sepolicy(8)  ,  setsebool(8),
---
 python/sepolicy/sepolicy/manpage.py | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
python/sepolicy/sepolicy/manpage.py +1 -2
index 35b3722dbe45..ed8cb71cd6d4 100755
@@ -922,8 +922,7 @@ This manual page was auto-generated using
 .B "sepolicy manpage".
 
 .SH "SEE ALSO"
-selinux(8), %s(8), semanage(8), restorecon(8), chcon(1), sepolicy(8)
-""" % (self.domainname))
+selinux(8), %s(8), semanage(8), restorecon(8), chcon(1), sepolicy(8)""" % (self.domainname))
 
         if self.booltext != "":
             self.fd.write(", setsebool(8)")
Signature · 1 line
2.13.3
#6 lore raw

Re: [PATCH 6/6] sepolicy: remove stray space in section "SEE ALSO"

Quoted 9 lines · Nicolas Iooss

The space after "sepolicy(8)" breaks the formatting of section SEE ALSO
in man pages generated with sepolicy:

  SEE ALSO
    selinux(8), ssh(8), semanage(8), restorecon(8), chcon(1), sepolicy(8)  ,  setsebool(8),

Thanks, applied all six patches.

Quoted 20 lines

---
 python/sepolicy/sepolicy/manpage.py | 3 +--

 1 file changed, 1 insertion(+), 2 deletions(-)
python/sepolicy/sepolicy/manpage.py +0 -0
index 35b3722dbe45..ed8cb71cd6d4 100755
@@ -922,8 +922,7 @@ This manual page was auto-generated using

 .B "sepolicy manpage".

 .SH "SEE ALSO"

python/sepolicy/sepolicy/manpage.py +1 -2
-selinux(8), %s(8), semanage(8), restorecon(8), chcon(1), sepolicy(8)
-""" % (self.domainname))
+selinux(8), %s(8), semanage(8), restorecon(8), chcon(1),

sepolicy(8)""" % (self.domainname))

         if self.booltext != "":
             self.fd.write(", setsebool(8)")