python/samba/gp_parse: PY3 file -> open
[amitay/samba.git] / python / samba / gp_parse / gp_pol.py
1 # GPO Parser for registry extension
2 #
3 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
4 # Written by Garming Sam <garming@catalyst.net.nz>
5 #
6 # This program 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 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 import base64
21
22 from xml.etree.ElementTree import Element, SubElement
23
24 from samba.dcerpc import preg
25 from samba.dcerpc import misc
26 from samba.ndr import ndr_pack, ndr_unpack
27
28 from samba.gp_parse import GPParser
29
30 # [MS-GPREG]
31 # [MS-GPFAS] Firewall and Advanced Security
32 # [MS-GPEF] Encrypting File System
33 # [MS-GPNRPT] Name Resolution Table
34 class GPPolParser(GPParser):
35     pol_file = None
36
37     reg_type = {
38         misc.REG_NONE: "REG_NONE",
39         misc.REG_SZ: "REG_SZ",
40         misc.REG_DWORD: "REG_DWORD",
41         misc.REG_DWORD_BIG_ENDIAN: "REG_DWORD_BIG_ENDIAN",
42         misc.REG_QWORD: "REG_QWORD",
43         misc.REG_EXPAND_SZ: "REG_EXPAND_SZ",
44         misc.REG_MULTI_SZ: "REG_MULTI_SZ",
45         misc.REG_BINARY: "REG_BINARY"
46     }
47
48     def map_reg_type(self, val):
49         ret = self.reg_type.get(val)
50         if ret is None:
51             return "REG_UNKNOWN"
52         return ret
53
54     def parse(self, contents):
55         self.pol_file = ndr_unpack(preg.file, contents)
56
57     def load_xml(self, root):
58         self.pol_file = preg.file()
59         self.pol_file.header.signature = root.attrib['signature']
60         self.pol_file.header.version = int(root.attrib['version'])
61         self.pol_file.num_entries = int(root.attrib['num_entries'])
62
63         entries = []
64         for e in root.findall('Entry'):
65             entry = preg.entry()
66             entry_type = int(e.attrib['type'])
67
68             entry.type = entry_type
69
70             entry.keyname = e.find('Key').text
71             value_name = e.find('ValueName').text
72             if value_name is None:
73                 value_name = ''
74
75             entry.valuename = value_name
76             # entry.size = int(e.attrib['size'])
77
78             if misc.REG_MULTI_SZ == entry_type:
79                 values = [x.text for x in e.findall('Value')]
80                 entry.data = (u'\x00'.join(values) + u'\x00\x00').encode('utf-16le')
81             elif (misc.REG_NONE == entry_type):
82                 pass
83             elif (misc.REG_SZ == entry_type or
84                   misc.REG_EXPAND_SZ == entry_type):
85                 string_val = e.find('Value').text
86                 if string_val is None:
87                     string_val = ''
88                 entry.data = string_val
89             elif (misc.REG_DWORD == entry_type or
90                   misc.REG_DWORD_BIG_ENDIAN == entry_type or
91                   misc.REG_QWORD == entry_type):
92                 entry.data = int(e.find('Value').text)
93             else: # REG UNKNOWN or REG_BINARY
94                 entry.data = base64.b64decode(e.find('Value').text)
95
96             entries.append(entry)
97
98         self.pol_file.entries = entries
99         # print self.pol_file.__ndr_print__()
100
101     def write_xml(self, filename):
102         with open(filename, 'wb') as f:
103             root = Element('PolFile')
104             root.attrib['signature'] = self.pol_file.header.signature
105             root.attrib['version'] = str(self.pol_file.header.version)
106             root.attrib['num_entries'] = str(self.pol_file.num_entries)
107             for entry in self.pol_file.entries:
108                 child = SubElement(root, 'Entry')
109                 # child.attrib['size'] = str(entry.size)
110                 child.attrib['type'] = str(entry.type)
111                 child.attrib['type_name'] = self.map_reg_type(entry.type)
112                 key = SubElement(child, 'Key')
113                 key.text = entry.keyname
114                 valuename = SubElement(child, 'ValueName')
115                 valuename.text = entry.valuename
116                 if misc.REG_MULTI_SZ == entry.type:
117                     multi = entry.data.decode('utf-16').rstrip(u'\x00').split(u'\x00')
118                     # print repr(multi)
119                     for m in multi:
120                         value = SubElement(child, 'Value')
121                         value.text = m
122                     # print tostring(value)
123                 elif (misc.REG_NONE == entry.type or
124                       misc.REG_SZ == entry.type or
125                       misc.REG_DWORD == entry.type or
126                       misc.REG_DWORD_BIG_ENDIAN == entry.type or
127                       misc.REG_QWORD == entry.type or
128                       misc.REG_EXPAND_SZ == entry.type):
129                     value = SubElement(child, 'Value')
130                     value.text = str(entry.data)
131                     # print tostring(value)
132                 else: # REG UNKNOWN or REG_BINARY
133                     value = SubElement(child, 'Value')
134                     value.text = base64.b64encode(entry.data).decode('utf8')
135                     # print tostring(value)
136
137             # print tostring(root)
138
139             self.write_pretty_xml(root, f)
140
141         # contents = codecs.open(filename, encoding='utf-8').read()
142         # self.load_xml(fromstring(contents))
143
144     def write_binary(self, filename):
145         with open(filename, 'wb') as f:
146             binary_data = ndr_pack(self.pol_file)
147             f.write(binary_data)