semanage, sepolicy: list also ports not attributed with port_type
PATCH v2 1/1 semanage, sepolicy: list also ports not attributed with port_type
For semanage port -l and sepolicy network -t type, show also ports which are not attributed with port_type. Such ports may exist in custom policies and even the attribute port_type may not be defined.
This fixes the following error with semanage port -l (and similar error with sepolicy network -t type):
Traceback (most recent call last):
File "/usr/sbin/semanage", line 975, in <module>
do_parser()
File "/usr/sbin/semanage", line 947, in do_parser
args.func(args)
File "/usr/sbin/semanage", line 441, in handlePort
OBJECT = object_dict['port'](args)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/seobject.py", line 1057, in __init__
self.valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range
---
v2: fix other cases and use better version courtesy of Petr Lautrbach
--- python/semanage/semanage-bash-completion.sh | 2 +- python/semanage/seobject.py | 2 +- python/sepolicy/sepolicy-bash-completion.sh | 2 +- python/sepolicy/sepolicy/__init__.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
python/semanage/semanage-bash-completion.sh +1 -1
@@ -37,7 +37,7 @@ __get_all_types () { seinfo -t 2> /dev/null | tail -n +3 } __get_all_port_types () { - seinfo -aport_type -x 2>/dev/null | tail -n +2 + sepolicy network -l } __get_all_domains () { seinfo -adomain -x 2>/dev/null | tail -n +2
python/semanage/seobject.py +1 -1
@@ -1055,7 +1055,7 @@ class portRecords(semanageRecords): def __init__(self, args = None): semanageRecords.__init__(self, args) try: - self.valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"]) + self.valid_types = [x["type"] for x in list(list(sepolicy.info(sepolicy.PORT)))] except RuntimeError: pass
python/sepolicy/sepolicy-bash-completion.sh +1 -1
@@ -52,7 +52,7 @@ __get_all_classes () { seinfo -c 2> /dev/null | tail -n +2 } __get_all_port_types () { - seinfo -aport_type -x 2> /dev/null | tail -n +2 + sepolicy network -l } __get_all_domain_types () { seinfo -adomain -x 2> /dev/null | tail -n +2
python/sepolicy/sepolicy/__init__.py +1 -1
@@ -989,7 +989,7 @@ def get_all_port_types(): global port_types if port_types: return port_types - port_types = list(sorted(info(ATTRIBUTE, "port_type"))[0]["types"]) + port_types = [x["type"] for x in list(list(info(PORT)))] return port_types
Signature · 1 line
2.39.2
Quoted 37 lines · Topi Miettinen
For
semanage port -landsepolicy network -t type, show also ports which are not attributed withport_type. Such ports may exist in custom policies and even the attributeport_typemay not be defined.This fixes the following error with
semanage port -l(and similar error withsepolicy network -t type):Traceback (most recent call last):
File "/usr/sbin/semanage", line 975, in <module>do_parser()File "
/usr/sbin/semanage", line 947, indo_parser
args.func(args)
File "/usr/sbin/semanage", line 441, in handlePort
OBJECT = object_dict['port'](args)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/seobject.py", line 1057, in__init__
self.valid_types =list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of rangeSigned-off-by Topi Miettinen <[email protected]>---
v2: fix other cases and use better version courtesy of Petr Lautrbach
--- python/semanage/semanage-bash-completion.sh | 2 +- python/semanage/seobject.py | 2 +- python/sepolicy/sepolicy-bash-completion.sh | 2 +- python/sepolicy/sepolicy/__init__.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)python/semanage/semanage-bash-completion.sh +0
-0
python/semanage/semanage-bash-completion.sh __get_all_types () { · line 41
Quoted 6 lines
python/semanage/semanage-bash-completion.sh +1
-1@@ -37,7 +37,7 @@ __get_all_types () { seinfo -t 2> /dev/null | tail -n +3 } __get_all_port_types () { - seinfo -aport_type -x 2>/dev/null | tail -n +2 + sepolicy network -l }
I support this change but it could have a side effect on distributions. E.g. in Fedora we ship semanage bash completion in policycoreutils-python-utils while sepolicy in policycoreutils-devel. On the other hand seinfo is in setools-console package which is not required by policycoreutils-python-utils so completions would not work anyway.
From upstream POV, it improves the situation so unless there's any other objection from other distribution maintainers I would not block it..
python/semanage/seobject.py class portRecords(semanageRecords): · line 1058
Quoted 12 lines
python/semanage/semanage-bash-completion.sh +0
-0__get_all_domains () { seinfo -adomain -x 2>/dev/null | tail -n +2python/semanage/seobject.py +1
-1@@ -1055,7 +1055,7 @@ class portRecords(semanageRecords): def __init__(self, args = None): semanageRecords.__init__(self, args) try: - self.valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"]) + self.valid_types = [x["type"] for x in list(list(sepolicy.info(sepolicy.PORT)))]
I know it's suggested by me. But looking on to it I see repeating list() which is unnecessary. sepolicy.info() returns a generator and so the new list could be constructed directly from it:
[x["type"] for x in sepolicy.info(sepolicy.PORT)]
Quoted 8 lines
python/semanage/seobject.py +0
-0except RuntimeError: passpython/sepolicy/sepolicy-bash-completion.sh +0
-0
python/sepolicy/sepolicy-bash-completion.sh __get_all_classes () { · line 56
Quoted 6 lines
python/sepolicy/sepolicy-bash-completion.sh +1
-1@@ -52,7 +52,7 @@ __get_all_classes () { seinfo -c 2> /dev/null | tail -n +2 } __get_all_port_types () { - seinfo -aport_type -x 2> /dev/null | tail -n +2 + sepolicy network -l }
Here the change does not have any side effect and improves the functionality
python/sepolicy/sepolicy/__init__.py def get_all_port_types(): · line 992
Quoted 12 lines
python/sepolicy/sepolicy-bash-completion.sh +0
-0__get_all_domain_types () { seinfo -adomain -x 2> /dev/null | tail -n +2python/sepolicy/sepolicy/__init__.py +1
-1@@ -989,7 +989,7 @@ def get_all_port_types(): global port_types if port_types: return port_types - port_types = list(sorted(info(ATTRIBUTE, "port_type"))[0]["types"]) + port_types = [x["type"] for x in list(list(info(PORT)))]
[x["type"] for x in info(PORT)]
Quoted 5 lines
python/sepolicy/sepolicy/__init__.py +0
-0return port_typesSignature · 1 line
2.39.2
Quoted 52 lines · Petr Lautrbach
Quoted 37 lines · Topi Miettinen
For
semanage port -landsepolicy network -t type, show also ports which are not attributed withport_type. Such ports may exist in custom policies and even the attributeport_typemay not be defined.This fixes the following error with
semanage port -l(and similar error withsepolicy network -t type):Traceback (most recent call last):
File "/usr/sbin/semanage", line 975, in <module>do_parser()File "
/usr/sbin/semanage", line 947, indo_parser
args.func(args)
File "/usr/sbin/semanage", line 441, in handlePort
OBJECT = object_dict['port'](args)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/seobject.py", line 1057, in__init__
self.valid_types =list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of rangeSigned-off-by Topi Miettinen <[email protected]>---
v2: fix other cases and use better version courtesy of Petr Lautrbach
--- python/semanage/semanage-bash-completion.sh | 2 +- python/semanage/seobject.py | 2 +- python/sepolicy/sepolicy-bash-completion.sh | 2 +- python/sepolicy/sepolicy/__init__.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)python/semanage/semanage-bash-completion.sh +0
-0python/semanage/semanage-bash-completion.sh __get_all_types () { · line 41
Quoted 6 lines
python/semanage/semanage-bash-completion.sh +1
-1@@ -37,7 +37,7 @@ __get_all_types () { seinfo -t 2> /dev/null | tail -n +3 } __get_all_port_types () { - seinfo -aport_type -x 2>/dev/null | tail -n +2 + sepolicy network -l }I support this change but it could have a side effect on distributions. E.g. in Fedora we ship semanage bash completion in policycoreutils-python-utils while sepolicy in policycoreutils-devel. On the other hand seinfo is in setools-console package which is not required by policycoreutils-python-utils so completions would not work anyway.
Quoted 2 lines
From upstream POV, it improves the situation so unless there's any other
objection from other distribution maintainers I would not block it..
If you prefer, it's also possible to continue to use seinfo with:
seinfo --portcon 2>/dev/null | sed -n 's/^\s\+portcon\s\+\S\+\s\+\S\+\s\+[^:]\+:[^:]\+:\([^:]\+\):\S\+$/\1/gp'
Quoted 17 lines
python/semanage/seobject.py class portRecords(semanageRecords): · line 1058
Quoted 12 lines
python/semanage/semanage-bash-completion.sh +0
-0__get_all_domains () { seinfo -adomain -x 2>/dev/null | tail -n +2python/semanage/seobject.py +1
-1@@ -1055,7 +1055,7 @@ class portRecords(semanageRecords): def __init__(self, args = None): semanageRecords.__init__(self, args) try: - self.valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"]) + self.valid_types = [x["type"] for x in list(list(sepolicy.info(sepolicy.PORT)))]I know it's suggested by me. But looking on to it I see repeating
list()which is unnecessary.sepolicy.info()returns a generator and so the new list could be constructed directly from it:
Quoted 1 line
[x["type"] for x in
sepolicy.info(sepolicy.PORT)]
Thanks!
Quoted 5 lines
Quoted 5 lines
python/semanage/seobject.py +0
-0except RuntimeError: pass
Quoted 14 lines
python/sepolicy/sepolicy-bash-completion.sh __get_all_classes () { · line 56
Quoted 11 lines
python/sepolicy/sepolicy-bash-completion.sh +1
-1@@ -52,7 +52,7 @@ __get_all_classes () { seinfo -c 2> /dev/null | tail -n +2 } __get_all_port_types () { - seinfo -aport_type -x 2> /dev/null | tail -n +2 + sepolicy network -l }Here the change does not have any side effect and improves the functionality
It's also possible to use the seinfo | sed version here too.
Quoted 22 lines
python/sepolicy/sepolicy/__init__.py def get_all_port_types(): · line 992
Quoted 13 lines
python/sepolicy/sepolicy-bash-completion.sh +0
-0__get_all_domain_types () { seinfo -adomain -x 2> /dev/null | tail -n +2python/sepolicy/sepolicy/__init__.py +1
-1@@ -989,7 +989,7 @@ def get_all_port_types(): global port_types if port_types: return port_types - port_types = list(sorted(info(ATTRIBUTE, "port_type"))[0]["types"]) + port_types = [x["type"] for x in list(list(info(PORT)))][x["type"] for x in info(PORT)]
Quoted 5 lines
python/sepolicy/sepolicy/__init__.py +0
-0return port_typesSignature · 1 line
2.39.2
Quoted 53 lines · Topi Miettinen
Quoted 52 lines · Petr Lautrbach
Quoted 37 lines · Topi Miettinen
For
semanage port -landsepolicy network -t type, show also ports which are not attributed withport_type. Such ports may exist in custom policies and even the attributeport_typemay not be defined.This fixes the following error with
semanage port -l(and similar error withsepolicy network -t type):Traceback (most recent call last):
File "/usr/sbin/semanage", line 975, in <module>do_parser()File "
/usr/sbin/semanage", line 947, indo_parser
args.func(args)
File "/usr/sbin/semanage", line 441, in handlePort
OBJECT = object_dict['port'](args)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/seobject.py", line 1057, in__init__
self.valid_types =list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of rangeSigned-off-by Topi Miettinen <[email protected]>---
v2: fix other cases and use better version courtesy of Petr Lautrbach
--- python/semanage/semanage-bash-completion.sh | 2 +- python/semanage/seobject.py | 2 +- python/sepolicy/sepolicy-bash-completion.sh | 2 +- python/sepolicy/sepolicy/__init__.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)python/semanage/semanage-bash-completion.sh +0
-0python/semanage/semanage-bash-completion.sh __get_all_types () { · line 41
Quoted 6 lines
python/semanage/semanage-bash-completion.sh +1
-1@@ -37,7 +37,7 @@ __get_all_types () { seinfo -t 2> /dev/null | tail -n +3 } __get_all_port_types () { - seinfo -aport_type -x 2>/dev/null | tail -n +2 + sepolicy network -l }I support this change but it could have a side effect on distributions. E.g. in Fedora we ship semanage bash completion in policycoreutils-python-utils while sepolicy in policycoreutils-devel. On the other hand seinfo is in setools-console package which is not required by policycoreutils-python-utils so completions would not work anyway.
Quoted 8 lines
Quoted 2 lines
From upstream POV, it improves the situation so unless there's any other
objection from other distribution maintainers I would not block it..If you prefer, it's also possible to continue to use seinfo with:
seinfo --portcon 2>/dev/null | sed -n 's/^\s\+portcon\s\+\S\+\s\+\S\+\s\+[^:]\+:[^:]\+:\([^:]\+\):\S\+$/\1/gp'
sepolicy network -l definitely looks better so I'd stick with it.
btw seinfo --portcon generates duplicates on Fedora:
$ seinfo --portcon 2>/dev/null | sed -n 's/^\s\+portcon\s\+\S\+\s\+\S\+\s\+[^:]\+:[^:]\+:\([^:]\+\):\S\+$/\1/gp' | wc -l 663
$ seinfo --portcon 2>/dev/null | sed -n 's/^\s\+portcon\s\+\S\+\s\+\S\+\s\+[^:]\+:[^:]\+:\([^:]\+\):\S\+$/\1/gp' | sort | uniq | wc -l 308
Quoted 66 lines
Quoted 18 lines
python/semanage/seobject.py class portRecords(semanageRecords): · line 1058
Quoted 13 lines
python/semanage/semanage-bash-completion.sh +0
-0__get_all_domains () { seinfo -adomain -x 2>/dev/null | tail -n +2python/semanage/seobject.py +1
-1@@ -1055,7 +1055,7 @@ class portRecords(semanageRecords): def __init__(self, args = None): semanageRecords.__init__(self, args) try: - self.valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"]) + self.valid_types = [x["type"] for x in list(list(sepolicy.info(sepolicy.PORT)))]I know it's suggested by me. But looking on to it I see repeating
list()which is unnecessary.sepolicy.info()returns a generator and so the new list could be constructed directly from it:Quoted 1 line
[x["type"] for x in
sepolicy.info(sepolicy.PORT)]Thanks!
Quoted 5 lines
Quoted 5 lines
python/semanage/seobject.py +0
-0except RuntimeError: passQuoted 14 lines
python/sepolicy/sepolicy-bash-completion.sh __get_all_classes () { · line 56
Quoted 11 lines
python/sepolicy/sepolicy-bash-completion.sh +1
-1@@ -52,7 +52,7 @@ __get_all_classes () { seinfo -c 2> /dev/null | tail -n +2 } __get_all_port_types () { - seinfo -aport_type -x 2> /dev/null | tail -n +2 + sepolicy network -l }Here the change does not have any side effect and improves the functionality
It's also possible to use the seinfo | sed version here too.
Quoted 22 lines
python/sepolicy/sepolicy/__init__.py def get_all_port_types(): · line 992
Quoted 13 lines
python/sepolicy/sepolicy-bash-completion.sh +0
-0__get_all_domain_types () { seinfo -adomain -x 2> /dev/null | tail -n +2python/sepolicy/sepolicy/__init__.py +1
-1@@ -989,7 +989,7 @@ def get_all_port_types(): global port_types if port_types: return port_types - port_types = list(sorted(info(ATTRIBUTE, "port_type"))[0]["types"]) + port_types = [x["type"] for x in list(list(info(PORT)))][x["type"] for x in info(PORT)]
Quoted 5 lines
python/sepolicy/sepolicy/__init__.py +0
-0return port_typesSignature · 1 line
2.39.2