summaryrefslogtreecommitdiffstats
path: root/python/samba/gp_parse/gp_inf.py
blob: 51035e6ec9fcfdb9499fa109fde0b5689cdf5b5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# GPO Parser for security extensions
#
# Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
# Written by Garming Sam <garming@catalyst.net.nz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import codecs
import collections
import re

from abc import ABCMeta, abstractmethod
from xml.etree.ElementTree import Element, SubElement

from samba.gp_parse import GPParser

# [MS-GPSB] Security Protocol Extension
class GptTmplInfParser(GPParser):
    sections = None
    encoding = 'utf-16'
    output_encoding = 'utf-16le'

    class AbstractParam:
        __metaclass__ = ABCMeta

        def __init__(self):
            self.param_list = []

        @abstractmethod
        def parse(self, line):
            pass

        @abstractmethod
        def write_section(self, header, fp):
            pass

        @abstractmethod
        def build_xml(self, xml_parent):
            pass

        @abstractmethod
        def from_xml(self, section):
            pass

    class IniParam(AbstractParam):
        # param_list = [(Key, Value),]

        def parse(self, line):
            key, val = line.split('=')

            self.param_list.append((key.strip(),
                                    val.strip()))

            # print key.strip(), val.strip()

        def write_section(self, header, fp):
            if len(self.param_list) ==  0:
                return
            fp.write(u'[%s]\r\n' % header)
            for key_out, val_out in self.param_list:
                fp.write(u'%s = %s\r\n' % (key_out,
                                           val_out))

        def build_xml(self, xml_parent):
            for key_ini, val_ini in self.param_list:
                child = SubElement(xml_parent, 'Parameter')
                key = SubElement(child, 'Key')
                value = SubElement(child, 'Value')
                key.text = key_ini
                value.text = val_ini

        def from_xml(self, section):
            for param in section.findall('Parameter'):
                key = param.find('Key').text
                value = param.find('Value').text
                if value is None:
                    value = ''

                self.param_list.append((key, value))

    class RegParam(AbstractParam):
        # param_list = [Value, Value, ...]
        def parse(self, line):
            # = can occur in a registry key, so don't parse these
            self.param_list.append(line)
            # print line

        def write_section(self, header, fp):
            if len(self.param_list) ==  0:
                return
            fp.write(u'[%s]\r\n' % header)
            for param in self.param_list:
                fp.write(u'%s\r\n' % param)

        def build_xml(self, xml_parent):
            for val_ini in self.param_list:
                child = SubElement(xml_parent, 'Parameter')
                value = SubElement(child, 'Value')
                value.text = val_ini

        def from_xml(self, section):
            for param in section.findall('Parameter'):
                value = param.find('Value').text
                if value is None:
                    value = ''

                self.param_list.append(value)

    class PrivSIDListParam(AbstractParam):
        # param_list = [(Key, [SID, SID,..]),
        def parse(self, line):
            key, val = line.split('=')

            self.param_list.append((key.strip(),
                                    [x.strip() for x in val.split(',')]))
            # print line

        def write_section(self, header, fp):
            if len(self.param_list) ==  0:
                return
            fp.write(u'[%s]\r\n' % header)
            for key_out, val in self.param_list:
                val_out = u','.join(val)
                fp.write(u'%s = %s\r\n' % (key_out, val_out))

        def build_xml(self, xml_parent):
            for key_ini, sid_list in self.param_list:
                child = SubElement(xml_parent, 'Parameter')
                key = SubElement(child, 'Key')
                key.text = key_ini
                for val_ini in sid_list:
                    value = SubElement(child, 'Value')
                    value.attrib['user_id'] = 'TRUE'
                    value.text = val_ini

        def from_xml(self, section):
            for param in section.findall('Parameter'):
                key = param.find('Key').text

                sid_list = []
                for val in param.findall('Value'):
                    value = val.text
                    if value is None:
                        value = ''

                    sid_list.append(value)

                self.param_list.append((key, sid_list))

    class NameModeACLParam(AbstractParam):
        # param_list = [[Name, Mode, ACL],]
        def parse(self, line):
            parameters = [None, None, None]
            current_arg = 0

            while line != '':
                # Read quoted string
                if line[:1] == '"':
                    line = line[1:]
                    findex = line.find('"')
                    parameters[current_arg] = line[:findex]
                    line = line[findex + 1:]
                # Skip past delimiter
                elif line[:1] == ',':
                    line = line[1:]
                    current_arg += 1
                # Read unquoted string
                else:
                    findex = line.find(',')
                    parameters[current_arg] = line[:findex]
                    line = line[findex:]

            # print parameters
            # print line
            self.param_list.append(parameters)

        def write_section(self, header, fp):
            if len(self.param_list) ==  0:
                return
            fp.write(u'[%s]\r\n' % header)
            for param in self.param_list:
                fp.write(u'"%s",%s,"%s"\r\n' % tuple(param))

        def build_xml(self, xml_parent):
            for name_mode_acl in self.param_list:
                child = SubElement(xml_parent, 'Parameter')

                value = SubElement(child, 'Value')
                value.text = name_mode_acl[0]

                value = SubElement(child, 'Value')
                value.text = name_mode_acl[1]

                value = SubElement(child, 'Value')
                value.attrib['acl'] = 'TRUE'
                value.text = name_mode_acl[2]

        def from_xml(self, section):
            for param in section.findall('Parameter'):
                name_mode_acl = [x.text if x.text else '' for x in param.findall('Value')]
                self.param_list.append(name_mode_acl)

    class MemberSIDListParam(AbstractParam):
        # param_list = [([XXXX, Memberof|Members], [SID, SID...]),...]
        def parse(self, line):
            key, val = line.split('=')

            key = key.strip()

            self.param_list.append((key.split('__'),
                                    [x.strip() for x in val.split(',')]))
            # print line

        def write_section(self, header, fp):
            if len(self.param_list) ==  0:
                return
            fp.write(u'[%s]\r\n' % header)

            for key, val in self.param_list:
                key_out = u'__'.join(key)
                val_out = u','.join(val)
                fp.write(u'%s = %s\r\n' % (key_out, val_out))

        def build_xml(self, xml_parent):
            for key_ini, sid_list in self.param_list:
                child = SubElement(xml_parent, 'Parameter')
                key = SubElement(child, 'Key')
                key.text = key_ini[0]
                key.attrib['member_type'] = key_ini[1]
                key.attrib['user_id'] = 'TRUE'

                for val_ini in sid_list:
                    value = SubElement(child, 'Value')
                    value.attrib['user_id'] = 'TRUE'
                    value.text = val_ini

        def from_xml(self, section):
            for param in section.findall('Parameter'):
                key = param.find('Key')
                member_type = key.attrib['member_type']

                sid_list = []
                for val in param.findall('Value'):
                    value = val.text
                    if value is None:
                        value = ''

                    sid_list.append(value)

                self.param_list.append(([key.text, member_type], sid_list))

    class UnicodeParam(AbstractParam):
        def parse(self, line):
            # print line
            pass

        def write_section(self, header, fp):
            fp.write(u'[Unicode]\r\nUnicode=yes\r\n')

        def build_xml(self, xml_parent):
            # We do not bother storing this field
            pass

        def from_xml(self, section):
            # We do not bother storing this field
            pass

    class VersionParam(AbstractParam):
        def parse(self, line):
            # print line
            pass

        def write_section(self, header, fp):
            out = u'[Version]\r\nsignature="$CHICAGO$"\r\nRevision=1\r\n'
            fp.write(out)

        def build_xml(self, xml_parent):
            # We do not bother storing this field
            pass

        def from_xml(self, section):
            # We do not bother storing this field
            pass

    def parse(self, contents):
        inf_file = contents.decode(self.encoding)

        self.sections = collections.OrderedDict([
            (u'Unicode', self.UnicodeParam()),
            (u'Version', self.VersionParam()),

            (u'System Access', self.IniParam()),
            (u'Kerberos Policy', self.IniParam()),
            (u'System Log', self.IniParam()),
            (u'Security Log', self.IniParam()),
            (u'Application Log', self.IniParam()),
            (u'Event Audit', self.IniParam()),
            (u'Registry Values', self.RegParam()),
            (u'Privilege Rights', self.PrivSIDListParam()),
            (u'Service General Setting', self.NameModeACLParam()),
            (u'Registry Keys', self.NameModeACLParam()),
            (u'File Security', self.NameModeACLParam()),
            (u'Group Membership', self.MemberSIDListParam()),
        ])

        current_param_parser = None
        current_header_name = None

        for line in inf_file.splitlines():
            match = re.match(r'\[(.*)\]', line)
            if match:
                header_name = match.group(1)
                if header_name in self.sections:
                    current_param_parser = self.sections[header_name]
                    # print current_param_parser
                    continue

            # print 'using', current_param_parser
            current_param_parser.parse(line)


    def write_binary(self, filename):
        with codecs.open(filename, 'wb+',
                         self.output_encoding) as f:
            # Write the byte-order mark
            f.write(u'\ufeff')

            for s in self.sections:
                self.sections[s].write_section(s, f)

    def write_xml(self, filename):
        with open(filename, 'wb') as f:
            root = Element('GptTmplInfFile')

            for sec_inf in self.sections:
                section = SubElement(root, 'Section')
                section.attrib['name'] = sec_inf

                self.sections[sec_inf].build_xml(section)

            self.write_pretty_xml(root, f)

        # contents = codecs.open(filename, encoding='utf-8').read()
        # self.load_xml(fromstring(contents))

    def load_xml(self, root):
        self.sections = collections.OrderedDict([
            (u'Unicode', self.UnicodeParam()),
            (u'Version', self.VersionParam()),

            (u'System Access', self.IniParam()),
            (u'Kerberos Policy', self.IniParam()),
            (u'System Log', self.IniParam()),
            (u'Security Log', self.IniParam()),
            (u'Application Log', self.IniParam()),
            (u'Event Audit', self.IniParam()),
            (u'Registry Values', self.RegParam()),
            (u'Privilege Rights', self.PrivSIDListParam()),
            (u'Service General Setting', self.NameModeACLParam()),
            (u'Registry Keys', self.NameModeACLParam()),
            (u'File Security', self.NameModeACLParam()),
            (u'Group Membership', self.MemberSIDListParam()),
        ])

        for s in root.findall('Section'):
            self.sections[s.attrib['name']].from_xml(s)