/
registry.go
191 lines (167 loc) · 5.1 KB
/
registry.go
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
package registry
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"strconv"
"unicode/utf16"
)
type dataType uint8
/* From winNT.h */
const (
regNone dataType = 0 /* no type */
regSz dataType = 1 /* string type (ASCII) */
regExpandSz dataType = 2 /* string, includes %ENVVAR% (expanded by caller) (ASCII) */
regBinary dataType = 3 /* binary format, callerspecific */
regDwordLittleEndian dataType = 4 /* DWORD in little endian format */
regDword dataType = 4 /* DWORD in little endian format */
regDwordBigEndian dataType = 5 /* DWORD in big endian format */
regLink dataType = 6 /* symbolic link (UNICODE) */
regMultiSz dataType = 7 /* multiple strings, delimited by \0, terminated by \0\0 (ASCII) */
regQword dataType = 11 /* QWORD in little endian format */
regQwordLittleEndian dataType = 11 /* QWORD in little endian format */
)
// PolicyEntry represents an entry of a policy file
type PolicyEntry struct {
Key string // Absolute path to setting. Ex: Sofware/Ubuntu/User/dconf/wallpaper
Value string
}
// DecodePolicy parses a policy stream in registry file format and returns a slice of entries.
func DecodePolicy(r io.Reader) (entries []PolicyEntry, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("couldn't decode policy: %v", err)
}
}()
ent, err := readPolicy(r)
if err != nil {
return nil, err
}
// translate to strings based on type
for _, e := range ent {
var res string
switch t := e.dType; t {
case regSz:
res, err = decodeUtf16(e.data)
if err != nil {
return nil, err
}
case regDword:
var resInt uint32
buf := bytes.NewReader(e.data)
if err := binary.Read(buf, binary.LittleEndian, &resInt); err != nil {
return nil, err
}
res = strconv.FormatUint(uint64(resInt), 10)
default:
return nil, fmt.Errorf("%d type is not supported set for key %s", t, e.key)
}
entries = append(entries, PolicyEntry{
Key: e.key,
Value: res,
})
}
return entries, nil
}
type policyRawEntry struct {
key string // Absolute path to setting. Ex: Sofware/Ubuntu/User/dconf/wallpaper
dType dataType
data []byte
}
type policyFileHeader struct {
Signature int32
Version int32
}
func readPolicy(r io.Reader) (entries []policyRawEntry, err error) {
validPolicyFileHeader := policyFileHeader{
Signature: 0x67655250,
Version: 1,
}
header := policyFileHeader{}
err = binary.Read(r, binary.LittleEndian, &header)
if err != nil {
if errors.Is(err, io.EOF) {
return nil, errors.New("invalid policy file (empty)")
}
return nil, err
}
// TODO: check EOF for small file, empty object…
if header != validPolicyFileHeader {
return nil, fmt.Errorf("invalid policy file header: %x%x", header.Signature, header.Version)
}
sectionStart := []byte{'[', 0} // [ in UTF-16 (little endian)
sectionEnd := []byte{0, 0, ']', 0} // \0] in UTF-16 (little endian)
dataOffset := len(sectionStart)
sectionEndWidth := len(sectionEnd)
// [key;value;type;size;data]
scanEntries := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
// Skip leading sectionStart.
start := 0
for ; start+dataOffset-1 < len(data); start++ {
if bytes.Compare(data[start:start+dataOffset], sectionStart) == 0 {
break
}
}
// Scan until sectionEnd, marking end of word.
for i := start + dataOffset; i+sectionEndWidth-1 < len(data); i++ {
if bytes.Compare(data[i:i+sectionEndWidth], sectionEnd) == 0 {
return i + sectionEndWidth, data[start+dataOffset : i+2], nil
}
}
// If we're at EOF, we have a final, non-empty, non-terminated word. Return an error.
if atEOF && len(data) > start {
return 0, nil, fmt.Errorf("item does not end with ']'")
}
// Request more data.
return start, nil, nil
}
s := bufio.NewScanner(r)
s.Split(scanEntries)
delimiter := []byte{0, 0, ';', 0} // \0; in little endian (UTF-16)
for s.Scan() {
elems := bytes.SplitN(s.Bytes(), delimiter, 5)
if len(elems) < 5 {
return nil, fmt.Errorf("item should contains at least 5 ';' separator: %s", s.Text())
}
keyPrefix, err := decodeUtf16(elems[0])
if err != nil {
return nil, err
}
keySuffix, err := decodeUtf16(elems[1])
if err != nil {
return nil, err
}
if keyPrefix == "" {
return nil, fmt.Errorf("empty key in %s", s.Text())
}
if keySuffix == "" {
return nil, fmt.Errorf("empty value in %s", s.Text())
}
entries = append(entries, policyRawEntry{
key: fmt.Sprintf("%s\\%s", keyPrefix, keySuffix),
dType: dataType(elems[2][0]),
data: elems[4], // TODO: if admx support binary data, then also return size
})
}
if err := s.Err(); err != nil {
return nil, err
}
return entries, nil
}
func decodeUtf16(b []byte) (string, error) {
if len(b)%2 != 0 {
return "", fmt.Errorf("%x is not a valid UTF-16 string", b)
}
ints := make([]uint16, len(b)/2)
if err := binary.Read(bytes.NewReader(b), binary.LittleEndian, &ints); err != nil {
return "", err
}
// remove trailing \0
if len(ints) >= 1 && ints[len(ints)-1] == 0 {
ints = ints[:len(ints)-1]
}
return string(utf16.Decode(ints)), nil
}