-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanifest-tester.lua
280 lines (230 loc) · 7.86 KB
/
manifest-tester.lua
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
#!/usr/bin/lua
local nixio = require('nixio')
local fs = require('nixio.fs')
local platform_info = require('platform_info')
local uci = require('uci').cursor()
local autoupdater_util = require('autoupdater.util')
local autoupdater_version = require('autoupdater.version')
if not platform_info.get_image_name() then
io.stderr:write("The autoupdater doesn't support this hardware model.\n")
os.exit(1)
end
local settings = uci:get_all('autoupdater', 'settings')
local branch_name = settings.branch
local mirror_name = '0'
local version_file = io.open(settings.version_file)
local old_version = version_file and version_file:read('*l') or ''
version_file:close()
local function parse_args()
local i = 1
while arg[i] do
if arg[i] == '-h' then
print("\nHelp:")
print("This script just tells you if the manifest is valide and whether has enought valid signatures.")
print("It reads the manifest for the branch of this nodes firmware. It's:'" .. branch_name .."'")
print("Arguments:")
print("-b <BRANCH>: test signatures against the public-keys of branch <BRANCH>")
print("-m <url> : check the manifest from <url>, i.e.: 1.updates.services.fftr/firmware/tackin_test/sysupgrade")
print("Name conventions:")
print("manifest-filename of 'stable' must be: stable.manifest\nmanifest-filename of 'beta' must be: beta.manifest\n")
os.exit(1)
elseif arg[i] == '-m' then
i = i+1
if not arg[i] then
print("Error parsing command line: expected mirror-URL")
os.exit(1)
end
mirror_name = arg[i]
elseif arg[i] == '-b' then
i = i+1
if not arg[i] then
print("Error parsing command line: expected branch name")
os.exit(1)
end
branch_name = arg[i]
else
print("Error parsing command line: unexpected argument '" .. arg[i] .. "'")
os.exit(1)
end
i = i+1
end
end
parse_args()
local branch = uci:get_all('autoupdater', branch_name)
if not branch then
print("Can't find configuration for branch '" .. branch_name .."'")
os.exit(1)
end
-- Test only this signature
local function test_this_sig(lines,command_s,sig)
table.insert(command_s, '-s')
table.insert(command_s, sig)
local pid_s, f_s = autoupdater_util.popen(true, unpack(command_s))
for _, line in ipairs(lines) do
f_s:write(line)
f_s:write('\n')
end
f_s:close()
table.remove(command_s)
table.remove(command_s)
local vpid, status, code = nixio.waitpid(pid_s)
return vpid and status == 'exited' and code == 0
end -- End test only this sig
-- Verifies a file given as a list of lines with a list of signatures using ecdsaverify
local function verify_lines(lines, sigs)
local command = {'ecdsaverify', '-n', tostring(branch.good_signatures)}
local command_single = {'ecdsaverify', '-n', '1'}
local good = tostring(branch.good_signatures)
print("We use branch: '" .. branch_name .. "'")
print("Minimum good signatures: '" .. good .. "'")
-- Build command line from sigs and branch.pubkey
for _, key in ipairs(branch.pubkey) do
if key:match('^' .. string.rep('%x', 64) .. '$') then
table.insert(command, '-p')
table.insert(command, key)
print("Found public-key: '" .. key .. "'")
table.insert(command_single, '-p')
table.insert(command_single, key)
end
end
for _, sig in ipairs(sigs) do
if sig:match('^' .. string.rep('%x', 128) .. '$') then
table.insert(command, '-s')
table.insert(command, sig)
print("\nFound signature: '" .. sig .. "'")
-- Test only this signature (does not mind duplicates)
if test_this_sig(lines,command_single,sig) then
print("Signature is ok!")
else
print("Signature is bad!")
end
end
end
-- Call ecdsautils
local pid, f = autoupdater_util.popen(true, unpack(command))
for _, line in ipairs(lines) do
f:write(line)
f:write('\n')
end
f:close()
local wpid, status, code = nixio.waitpid(pid)
return wpid and status == 'exited' and code == 0
end
-- Downloads, parses and verifies the update manifest from a mirror
-- Returns a table with the fields version, checksum and filename if everything is ok, nil otherwise
local function read_manifest(mirror)
print("Read manifest from: '" .. mirror .. "'\n")
local sep = false
local lines = {}
local sigs = {}
local branch_ok = false
local ret = {}
-- Remove potential trailing slash
mirror = mirror:gsub('/$', '')
local starttime = os.time()
local pid, manifest_loader = autoupdater_util.popen(false, 'wget', '-T', '120', '-O-', string.format('%s/%s.manifest', mirror, branch.name))
local data = ''
-- Read all lines from the manifest
-- The upper part is saved to lines, the lower part to sigs
while true do
-- If the manifest download takes more than 1 minute, we don't really
-- have a chance to download a whole image
local timeout = starttime+60 - os.time()
if timeout < 0 or not nixio.poll({{fd = manifest_loader, events = nixio.poll_flags('in')}}, timeout * 1000) then
print("Timeout while reading manifest.\n")
nixio.kill(pid, nixio.const.SIGTERM)
manifest_loader:close()
return nil
end
local r = manifest_loader:read(1024)
if not r or r == '' then
break
end
data = data .. r
while data:match('\n') do
local line, rest = data:match('^([^\n]*)\n(.*)$')
data = rest
if not sep then
if line == '---' then
sep = true
else
table.insert(lines, line)
if line == ('BRANCH=' .. branch.name) then
branch_ok = true
end
local date = line:match('^DATE=(.+)$')
local priority = line:match('^PRIORITY=([%d%.]+)$')
local model, version, checksum, filename = line:match('^([^ ]+) ([^ ]+) ([^ ]+) ([^ ]+)$')
if date then
ret.date = autoupdater_util.parse_date(date)
elseif priority then
ret.priority = tonumber(priority)
elseif model == platform_info.get_image_name() then
ret.version = version
ret.checksum = checksum
ret.filename = filename
end
end
else
table.insert(sigs, line)
end
end
end
manifest_loader:close()
-- Do some very basic checks before checking the signatures
-- (as the signature verification is computationally expensive)
if not sep then
print('There seems to have gone something wrong downloading the manifest from ' .. mirror .. '')
print('Or I could not find the seperator: "---" \n')
return nil
end
if not ret.date or not ret.priority then
print('The manifest downloaded from ' .. mirror .. ' is invalid (DATE or PRIORITY missing)\n')
return nil
end
if not branch_ok then
print('Wrong branch. We are on: ', branch.name, '')
return nil
end
if not ret.version then
print('No matching firmware found (model ' .. platform_info.get_image_name() .. ')')
return nil
end
if not verify_lines(lines, sigs) then
print('\nNot enough valid signatures!\n')
return nil
else
-- we are good to go
print('\nEnough valid signatures!\n')
end
return ret
end
-- Tries to perform an update from a given mirror
local function autoupdate(mirror)
local manifest = read_manifest(mirror)
if not manifest then
print('No good manifest found!')
return false
end
if not autoupdater_version.newer_than(manifest.version, old_version) then
print('No new firmware available.')
return true
end
print('New version available.')
end
if mirror_name ~= '0' then
print('\nGiven url: ' .. mirror_name .. '')
if autoupdate(mirror_name) then
os.exit(0)
end
else
local mirrors = branch.mirror
while #mirrors > 0 do
local mirror = table.remove(mirrors, math.random(#mirrors))
if autoupdate(mirror) then
os.exit(0)
end
end
end
print('Sorry.')
os.exit(1)