1#!/usr/bin/python
2# Copyright 2014-2015, Tresys Technology, LLC
3#
4# This file is part of SETools.
5#
6# SETools is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 2 of the License, or
9# (at your option) any later version.
10#
11# SETools is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with SETools.  If not, see <http://www.gnu.org/licenses/>.
18#
19
20from __future__ import print_function
21import setools
22import argparse
23import sys
24import logging
25
26
27def expand_attr(attr):
28    """Render type and role attributes."""
29    items = "\n\t".join(sorted(str(i) for i in attr.expand()))
30    contents = items if items else "<empty attribute>"
31    return "{0}\n\t{1}".format(attr.statement(), contents)
32
33parser = argparse.ArgumentParser(
34    description="SELinux policy information tool.")
35parser.add_argument("--version", action="version", version=setools.__version__)
36parser.add_argument("policy", help="Path to the SELinux policy to query.", nargs="?")
37parser.add_argument("-x", "--expand", action="store_true",
38                    help="Print additional information about the specified components.")
39parser.add_argument("--flat",  help="Print without item count nor indentation.",
40                    dest="flat", default=False, action="store_true")
41parser.add_argument("-v", "--verbose", action="store_true",
42                    help="Print extra informational messages")
43parser.add_argument("--debug", action="store_true", dest="debug", help="Enable debugging.")
44
45queries = parser.add_argument_group("Component Queries")
46queries.add_argument("-a", "--attribute",  help="Print type attributes.", dest="typeattrquery",
47                     nargs='?', const=True, metavar="ATTR")
48queries.add_argument("-b", "--bool", help="Print Booleans.", dest="boolquery",
49                     nargs='?', const=True, metavar="BOOL")
50queries.add_argument("-c", "--class", help="Print object classes.", dest="classquery",
51                     nargs='?', const=True, metavar="CLASS")
52queries.add_argument("-r", "--role", help="Print roles.", dest="rolequery",
53                     nargs='?', const=True, metavar="ROLE")
54queries.add_argument("-t", "--type", help="Print types.", dest="typequery",
55                     nargs='?', const=True, metavar="TYPE")
56queries.add_argument("-u", "--user", help="Print users.", dest="userquery",
57                     nargs='?', const=True, metavar="USER")
58queries.add_argument("--category", help="Print MLS categories.", dest="mlscatsquery",
59                     nargs='?', const=True, metavar="CAT")
60queries.add_argument("--common", help="Print common permission set.", dest="commonquery",
61                     nargs='?', const=True, metavar="COMMON")
62queries.add_argument("--constrain", help="Print constraints.", dest="constraintquery",
63                     nargs='?', const=True, metavar="CLASS")
64queries.add_argument("--default", help="Print default_* rules.", dest="defaultquery",
65                     nargs='?', const=True, metavar="CLASS")
66queries.add_argument("--fs_use", help="Print fs_use statements.", dest="fsusequery",
67                     nargs='?', const=True, metavar="FS_TYPE")
68queries.add_argument("--genfscon", help="Print genfscon statements.", dest="genfsconquery",
69                     nargs='?', const=True, metavar="FS_TYPE")
70queries.add_argument("--initialsid", help="Print initial SIDs (contexts).", dest="initialsidquery",
71                     nargs='?', const=True, metavar="NAME")
72queries.add_argument("--netifcon", help="Print netifcon statements.", dest="netifconquery",
73                     nargs='?', const=True, metavar="DEVICE")
74queries.add_argument("--nodecon", help="Print nodecon statements.", dest="nodeconquery",
75                     nargs='?', const=True, metavar="ADDR")
76queries.add_argument("--permissive", help="Print permissive types.", dest="permissivequery",
77                     nargs='?', const=True, metavar="TYPE")
78queries.add_argument("--polcap", help="Print policy capabilities.", dest="polcapquery",
79                     nargs='?', const=True, metavar="NAME")
80queries.add_argument("--portcon", help="Print portcon statements.", dest="portconquery",
81                     nargs='?', const=True, metavar="PORTNUM[-PORTNUM]")
82queries.add_argument("--sensitivity", help="Print MLS sensitivities.", dest="mlssensquery",
83                     nargs='?', const=True, metavar="SENS")
84queries.add_argument("--validatetrans", help="Print validatetrans.", dest="validatetransquery",
85                     nargs='?', const=True, metavar="CLASS")
86queries.add_argument("--all", help="Print all of the above.",
87                     dest="all", default=False, action="store_true")
88
89args = parser.parse_args()
90
91if args.debug:
92    logging.basicConfig(level=logging.DEBUG,
93                        format='%(asctime)s|%(levelname)s|%(name)s|%(message)s')
94elif args.verbose:
95    logging.basicConfig(level=logging.INFO, format='%(message)s')
96else:
97    logging.basicConfig(level=logging.WARNING, format='%(message)s')
98
99try:
100    p = setools.SELinuxPolicy(args.policy)
101    components = []
102
103    if args.boolquery or args.all:
104        q = setools.BoolQuery(p)
105        if isinstance(args.boolquery, str):
106            q.name = args.boolquery
107
108        components.append(("Booleans", q, lambda x: x.statement()))
109
110    if args.mlscatsquery or args.all:
111        q = setools.CategoryQuery(p)
112        if isinstance(args.mlscatsquery, str):
113            q.name = args.mlscatsquery
114
115        components.append(("Categories", q, lambda x: x.statement()))
116
117    if args.classquery or args.all:
118        q = setools.ObjClassQuery(p)
119        if isinstance(args.classquery, str):
120            q.name = args.classquery
121
122        components.append(("Classes", q, lambda x: x.statement()))
123
124    if args.commonquery or args.all:
125        q = setools.CommonQuery(p)
126        if isinstance(args.commonquery, str):
127            q.name = args.commonquery
128
129        components.append(("Commons", q, lambda x: x.statement()))
130
131    if args.constraintquery or args.all:
132        q = setools.ConstraintQuery(p, ruletype=["constrain", "mlsconstrain"])
133        if isinstance(args.constraintquery, str):
134            q.tclass = [args.constraintquery]
135
136        components.append(("Constraints", q, lambda x: x.statement()))
137
138    if args.defaultquery or args.all:
139        q = setools.DefaultQuery(p)
140        if isinstance(args.defaultquery, str):
141            q.tclass = [args.defaultquery]
142
143        components.append(("Default rules", q, lambda x: x.statement()))
144
145    if args.fsusequery or args.all:
146        q = setools.FSUseQuery(p)
147        if isinstance(args.fsusequery, str):
148            q.fs = args.fsusequery
149
150        components.append(("Fs_use", q, lambda x: x.statement()))
151
152    if args.genfsconquery or args.all:
153        q = setools.GenfsconQuery(p)
154        if isinstance(args.genfsconquery, str):
155            q.fs = args.genfsconquery
156
157        components.append(("Genfscon", q, lambda x: x.statement()))
158
159    if args.initialsidquery or args.all:
160        q = setools.InitialSIDQuery(p)
161        if isinstance(args.initialsidquery, str):
162            q.name = args.initialsidquery
163
164        components.append(("Initial SIDs", q, lambda x: x.statement()))
165
166    if args.netifconquery or args.all:
167        q = setools.NetifconQuery(p)
168        if isinstance(args.netifconquery, str):
169            q.name = args.netifconquery
170
171        components.append(("Netifcon", q, lambda x: x.statement()))
172
173    if args.nodeconquery or args.all:
174        q = setools.NodeconQuery(p)
175        if isinstance(args.nodeconquery, str):
176            q.network = args.nodeconquery
177
178        components.append(("Nodecon", q, lambda x: x.statement()))
179
180    if args.permissivequery or args.all:
181        q = setools.TypeQuery(p, permissive=True, match_permissive=True)
182        if isinstance(args.permissivequery, str):
183            q.name = args.permissivequery
184
185        components.append(("Permissive Types", q, lambda x: x.statement()))
186
187    if args.polcapquery or args.all:
188        q = setools.PolCapQuery(p)
189        if isinstance(args.polcapquery, str):
190            q.name = args.polcapquery
191
192        components.append(("Polcap", q, lambda x: x.statement()))
193
194    if args.portconquery or args.all:
195        q = setools.PortconQuery(p)
196        if isinstance(args.portconquery, str):
197            try:
198                ports = [int(i) for i in args.portconquery.split("-")]
199            except ValueError:
200                parser.error("Enter a port number or range, e.g. 22 or 6000-6020")
201
202            if len(ports) == 2:
203                q.ports = ports
204            elif len(ports) == 1:
205                q.ports = (ports[0], ports[0])
206            else:
207                parser.error("Enter a port number or range, e.g. 22 or 6000-6020")
208
209        components.append(("Portcon", q, lambda x: x.statement()))
210
211    if args.rolequery or args.all:
212        q = setools.RoleQuery(p)
213        if isinstance(args.rolequery, str):
214            q.name = args.rolequery
215
216        components.append(("Roles", q, lambda x: x.statement()))
217
218    if args.mlssensquery or args.all:
219        q = setools.SensitivityQuery(p)
220        if isinstance(args.mlssensquery, str):
221            q.name = args.mlssensquery
222
223        components.append(("Sensitivities", q, lambda x: x.statement()))
224
225    if args.typequery or args.all:
226        q = setools.TypeQuery(p)
227        if isinstance(args.typequery, str):
228            q.name = args.typequery
229
230        components.append(("Types", q, lambda x: x.statement()))
231
232    if args.typeattrquery or args.all:
233        q = setools.TypeAttributeQuery(p)
234        if isinstance(args.typeattrquery, str):
235            q.name = args.typeattrquery
236
237        components.append(("Type Attributes", q, expand_attr))
238
239    if args.userquery or args.all:
240        q = setools.UserQuery(p)
241        if isinstance(args.userquery, str):
242            q.name = args.userquery
243
244        components.append(("Users", q, lambda x: x.statement()))
245
246    if args.validatetransquery or args.all:
247        q = setools.ConstraintQuery(p, ruletype=["validatetrans", "mlsvalidatetrans"])
248        if isinstance(args.validatetransquery, str):
249            q.tclass = [args.validatetransquery]
250
251        components.append(("Validatetrans", q, lambda x: x.statement()))
252
253    if (not components or args.all) and not args.flat:
254        mls = "enabled" if p.mls else "disabled"
255
256        print("Statistics for policy file: {0}".format(p))
257        print("Policy Version: {0} (MLS {1}, {2} unknown permissions)".format(
258            p.version, mls, p.handle_unknown))
259        print("  Classes:       {0:7}    Permissions:   {1:7}".format(
260            p.class_count, p.permission_count))
261        print("  Sensitivities: {0:7}    Categories:    {1:7}".format(
262            p.level_count, p.category_count))
263        print("  Types:         {0:7}    Attributes:    {1:7}".format(
264            p.type_count, p.type_attribute_count))
265        print("  Users:         {0:7}    Roles:         {1:7}".format(
266            p.user_count, p.role_count))
267        print("  Booleans:      {0:7}    Cond. Expr.:   {1:7}".format(
268            p.boolean_count, p.conditional_count))
269        print("  Allow:         {0:7}    Neverallow:    {1:7}".format(
270            p.allow_count, p.neverallow_count))
271        print("  Auditallow:    {0:7}    Dontaudit:     {1:7}".format(
272            p.auditallow_count, p.dontaudit_count))
273        print("  Type_trans:    {0:7}    Type_change:   {1:7}".format(
274            p.type_transition_count, p.type_change_count))
275        print("  Type_member:   {0:7}    Range_trans:   {1:7}".format(
276            p.type_member_count, p.range_transition_count))
277        print("  Role allow:    {0:7}    Role_trans:    {1:7}".format(
278            p.role_allow_count, p.role_transition_count))
279        print("  Constraints:   {0:7}    Validatetrans: {1:7}".format(
280            p.constraint_count, p.validatetrans_count))
281        print("  MLS Constrain: {0:7}    MLS Val. Tran: {1:7}".format(
282            p.mlsconstraint_count, p.mlsvalidatetrans_count))
283        print("  Initial SIDs:  {0:7}    Fs_use:        {1:7}".format(
284            p.initialsids_count, p.fs_use_count))
285        print("  Genfscon:      {0:7}    Portcon:       {1:7}".format(
286            p.genfscon_count, p.portcon_count))
287        print("  Netifcon:      {0:7}    Nodecon:       {1:7}".format(
288            p.netifcon_count, p.nodecon_count))
289        print("  Permissives:   {0:7}    Polcap:        {1:7}".format(
290            p.permissives_count, p.polcap_count))
291        print("  Defaults:      {0:7}".format(p.default_count))
292
293    for desc, component, expander in components:
294        results = sorted(component.results())
295        if not args.flat:
296            print("\n{0}: {1}".format(desc, len(results)))
297        for item in results:
298            result = expander(item) if args.expand else item
299            strfmt = "   {0}" if not args.flat else "{0}"
300            print(strfmt.format(result))
301
302except Exception as err:
303    if args.debug:
304        import traceback
305        traceback.print_exc()
306    else:
307        print(err)
308
309    sys.exit(-1)
310