-
Notifications
You must be signed in to change notification settings - Fork 2
/
bootstrap.py
607 lines (438 loc) · 18.8 KB
/
bootstrap.py
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
import os
import sys
import shutil
import time
import tempfile
import shutil
import datetime
import operator
import sqlite3
from subprocess import call
from stat import *
from flask import Flask, request, redirect, url_for, render_template, abort, make_response, send_from_directory, jsonify
from werkzeug.utils import secure_filename
from werkzeug.middleware.proxy_fix import ProxyFix
from config import config
#
# These locations should works out-of-box if you use default settings
#
thispath = os.path.dirname(os.path.realpath(__file__))
BASEPATH = os.path.join(thispath)
app = Flask(__name__, static_url_path='/static')
app.url_map.strict_slashes = False
#
# Database
#
def db_open():
return sqlite3.connect(config['bootstrap-db'])
def db_check():
db = db_open()
# sanity check
try:
c = db.cursor()
c.execute("SELECT COUNT(*) FROM provision")
db.close()
except sqlite3.OperationalError:
print("[-] database not initialized, please check installation steps")
sys.exit(1)
db_check()
#
# Helpers
#
def get_protocol():
if 'unsecure' in request.host:
return 'http'
return 'https'
# Full cycle ipxe script
def ipxe_script(release, farmer, extra="", source=None):
if not source:
source = 'net/%s.efi' % release
kernel = os.path.join(config['kernel-path'], source)
if release not in config['runmodes'].keys():
abort(401)
if not os.path.exists(kernel):
abort(404)
kernel_secure = "%s://%s/kernel/%s" % (get_protocol(), request.host, source)
kernel_simple = "http://unsecure.%s/kernel/%s" % (request.host, source)
version = request.args.get("version", "v3")
chain = "nomodeset version=%s runmode=%s panic=7200" % (version, release)
if farmer:
chain += " farmer_id=%s" % farmer
if extra:
chain += " " + extra.replace("___", "/")
settings = {
"release": config['runmodes'][release],
"farmerid": farmer,
"parameters": extra,
"kernel": kernel_secure,
"cmdline": chain,
}
return render_template("boot.ipxe", **settings)
# Debug cycle ipxe script
def ipxe_debug_script(release, farmer, extra="", source=None):
if not source:
source = 'net/%s.efi' % release
kernel = os.path.join(config['kernel-path'], source)
if release not in config['runmodes'].keys():
abort(401)
if not os.path.exists(kernel):
abort(404)
kernel_secure = "%s://%s/kernel/%s" % (get_protocol(), request.host, source)
kernel_simple = "http://unsecure.%s/kernel/%s" % (request.host, source)
chain = "nomodeset version=v3 runmode=%s panic=7200" % release
if farmer:
chain += " farmer_id=%s" % farmer
if extra:
chain += " " + extra.replace("___", "/")
settings = {
"release": config['runmodes'][release],
"farmerid": farmer,
"parameters": extra,
"kernel": kernel_secure,
"cmdline": chain,
}
return render_template("debug.ipxe", **settings)
# No network setup script
# Used for provision clients which have already network setup
# by provision image
def ipxe_quick_script(release, farmer, extra=""):
source = 'zero-os-development-zos-v2-generic.efi'
kernel = os.path.join(config['kernel-path'], source)
if release not in config['runmodes'].keys():
abort(401)
if not os.path.exists(kernel):
abort(404)
kernel = "%s://%s/kernel/%s" % (get_protocol(), request.host, source)
cmdline = "nomodeset runmode=%s" % release
if farmer:
cmdline += " farmer_id=%s" % farmer
if extra:
cmdline += " " + extra
settings = {
"release": config['runmodes'][release],
"parameters": extra,
"kernel": kernel,
"cmdline": cmdline,
}
return render_template("boot-quick.ipxe", **settings)
# Provisioning image requesting configuration on runtime
def ipxe_provision():
url = "%s://%s/provision/${net${idx}/mac}" % (get_protocol(), request.host)
settings = {
"url": url,
}
return render_template("boot-provision.ipxe", **settings)
def text_reply(payload):
response = make_response(payload)
response.headers["Content-Type"] = "text/plain"
return response
#
# Routing
#
@app.route('/kernel/<path:filename>', methods=['GET'])
def download(filename):
print("[+] downloading: %s" % filename)
return send_from_directory(config['kernel-path'], filename)
@app.route('/kernel/net/<path:filename>', methods=['GET'])
def download_net(filename):
print("[+] downloading (network based): %s" % filename)
return send_from_directory(config['kernel-net-path'], filename)
#
# Generic Image Generator
#
def srcdir_from_filename(targetfile):
efi = ["ipxe.efi", "uefimg.img"]
if targetfile in efi:
return config["ipxe-template-uefi"]
return config["ipxe-template"]
def download_mkresponse(data, filename):
response = make_response(data)
response.headers["Content-Type"] = "application/octet-stream"
response.headers['Content-Disposition'] = "inline; filename=%s" % filename
return response
def generic_image_generator(release, farmer, extra, buildscript, targetfile, filename, kernel=None):
response = make_response("Request failed")
srcdir = srcdir_from_filename(targetfile)
with tempfile.TemporaryDirectory() as tmpdir:
src = os.path.join(tmpdir, "src")
print("[+] copying template: %s > %s" % (srcdir, src))
call(["cp", "-ar", srcdir, src])
print("[+] creating ipxe script")
with open(os.path.join(tmpdir, "boot.ipxe"), 'w') as f:
f.write(ipxe_script(release, farmer, extra, kernel))
print("[+] building: %s" % buildscript)
script = os.path.join(BASEPATH, "scripts", buildscript)
call(["bash", script, tmpdir])
filecontents = ""
with open(os.path.join(tmpdir, targetfile), 'rb') as f:
filecontents = f.read()
response = download_mkresponse(filecontents, filename)
return response
def generic_debug_image_generator(release, farmer, extra, buildscript, targetfile, filename, kernel=None):
response = make_response("Request failed")
srcdir = srcdir_from_filename(targetfile)
with tempfile.TemporaryDirectory() as tmpdir:
src = os.path.join(tmpdir, "src")
print("[+] copying template: %s > %s" % (srcdir, src))
call(["cp", "-ar", srcdir, src])
print("[+] creating ipxe debug script")
with open(os.path.join(tmpdir, "boot.ipxe"), 'w') as f:
f.write(ipxe_debug_script(release, farmer, extra, kernel))
print("[+] building: %s" % buildscript)
script = os.path.join(BASEPATH, "scripts", buildscript)
call(["bash", script, tmpdir])
filecontents = ""
with open(os.path.join(tmpdir, targetfile), 'rb') as f:
filecontents = f.read()
response = download_mkresponse(filecontents, filename)
return response
def generic_image_provision(buildscript, targetfile, filename):
response = make_response("Request failed")
srcdir = srcdir_from_filename(targetfile)
with tempfile.TemporaryDirectory() as tmpdir:
src = os.path.join(tmpdir, "src")
print("[+] copying template: %s > %s" % (srcdir, src))
call(["cp", "-ar", srcdir, src])
print("[+] creating ipxe script")
with open(os.path.join(tmpdir, "boot.ipxe"), 'w') as f:
f.write(ipxe_provision())
print("[+] building: %s" % buildscript)
script = os.path.join(BASEPATH, "scripts", buildscript)
call(["bash", script, tmpdir])
filecontents = ""
with open(os.path.join(tmpdir, targetfile), 'rb') as f:
filecontents = f.read()
response = download_mkresponse(filecontents, filename)
return response
def generic_image_quickipxe(release, farmer, extra, buildscript, targetfile, filename):
response = make_response("Request failed")
srcdir = srcdir_from_filename(targetfile)
with tempfile.TemporaryDirectory() as tmpdir:
src = os.path.join(tmpdir, "src")
print("[+] copying template: %s > %s" % (srcdir, src))
call(["cp", "-ar", srcdir, src])
print("[+] creating ipxe script")
with open(os.path.join(tmpdir, "boot.ipxe"), 'w') as f:
f.write(ipxe_script(release, farmer, extra, None))
print("[+] building: " % buildscript)
script = os.path.join(BASEPATH, "scripts", buildscript)
call(["bash", script, tmpdir])
filecontents = ""
with open(os.path.join(tmpdir, targetfile), 'rb') as f:
filecontents = f.read()
response = download_mkresponse(filecontents, filename)
return response
def baseurl():
base = request.base_url[:-1]
base = base.replace("http://", "https://") # force https
return base
def kernel_list():
files = []
target = os.listdir(config['kernel-path'])
ordered = {}
for filename in target:
if filename.startswith("."):
continue
endpoint = os.path.join(config['kernel-path'], filename)
stat = os.stat(endpoint, follow_symlinks=False)
updated = datetime.datetime.utcfromtimestamp(stat.st_mtime).strftime('%Y-%m-%d, %H:%M:%S (UTC)')
if not S_ISREG(stat.st_mode):
continue
ordered[endpoint] = stat.st_mtime
starget = sorted(ordered.items(), key=operator.itemgetter(1))
starget.reverse()
for endpoint in starget:
fullpath = endpoint[0]
filename = os.path.basename(fullpath)
updated = datetime.datetime.utcfromtimestamp(endpoint[1]).strftime('%Y-%m-%d, %H:%M:%S (UTC)')
files.append({
'name': filename,
'release': filename[:-4],
'updated': updated,
'timestamp': int(endpoint[1]),
})
return files
#
# Target Image Generator
#
@app.route('/iso/<release>', methods=['GET'])
def iso_release(release):
return iso_release_farmer_extra(release, "", "")
@app.route('/iso/<release>/<farmer>', methods=['GET'])
def iso_release_farmer(release, farmer):
return iso_release_farmer_extra(release, farmer, "")
@app.route('/iso/<release>/<farmer>/<extra>', methods=['GET'])
def iso_release_farmer_extra(release, farmer, extra):
print("[+] release: %s, network: %s, extra: %s" % (release, farmer, extra))
return generic_image_generator(release, farmer, extra, "mkiso.sh", "ipxe.iso", "ipxe-%s.iso" % release)
@app.route('/iso/<release>/<farmer>/<extra>/<kernel>', methods=['GET'])
def iso_release_farmer_extra_kernel(release, farmer, extra, kernel):
print("[+] release: %s, network: %s, extra: %s [kernel: %s]" % (release, farmer, extra, kernel))
return generic_image_generator(release, farmer, extra, "mkiso.sh", "ipxe.iso", "ipxe-%s.iso" % release, kernel)
@app.route('/usb/<release>', methods=['GET'])
def usb_release(release):
return usb_release_farmer_extra(release, "", "")
@app.route('/usb/<release>/<farmer>', methods=['GET'])
def usb_release_farmer(release, farmer):
return usb_release_farmer_extra(release, farmer, "")
@app.route('/usb/<release>/<farmer>/<extra>', methods=['GET'])
def usb_release_farmer_extra(release, farmer, extra):
print("[+] release: %s, network: %s, extra: %s" % (release, farmer, extra))
return generic_image_generator(release, farmer, extra, "mkusb.sh", "ipxe.usb", "ipxe-%s.img" % release)
@app.route('/usb/<release>/<farmer>/<extra>/<kernel>', methods=['GET'])
def usb_release_farmer_extra_kernel(release, farmer, extra, kernel):
print("[+] release: %s, network: %s, extra: %s [kernel: %s]" % (release, farmer, extra, kernel))
return generic_image_generator(release, farmer, extra, "mkusb.sh", "ipxe.usb", "ipxe-%s.img" % release, kernel)
@app.route('/krn-generic', methods=['GET'])
def krn_generic():
print("[+] generic ipxe kernel")
return generic_image_provision("mkkrn-generic.sh", "ipxe.lkrn", "ipxe-zero-os-generic.lkrn")
@app.route('/uefi-generic', methods=['GET'])
def uefi_generic():
print("[+] generic uefi ipxe")
return generic_image_provision("mkuefi-generic.sh", "ipxe.efi", "ipxe-zero-os-generic.efi")
@app.route('/krn-provision', methods=['GET'])
def krn_provision():
print("[+] provision ipxe kernel")
return generic_image_quickipxe("mkkrn.sh", "ipxe.lkrn", "ipxe-zero-os-provision.lkrn")
@app.route('/uefi-provision', methods=['GET'])
def uefi_provision():
print("[+] provisioning uefi ipxe")
return generic_image_quickipxe("mkuefi.sh", "ipxe.efi", "ipxe-zero-os-provision.efi")
@app.route('/krn/<release>', methods=['GET'])
def krn_release(release):
return krn_release_farmer_extra(release, "", "")
@app.route('/krn/<release>/<farmer>', methods=['GET'])
def krn_release_farmer(release, farmer):
return krn_release_farmer_extra(release, farmer, "")
@app.route('/krn/<release>/<farmer>/<extra>', methods=['GET'])
def krn_release_farmer_extra(release, farmer, extra):
print("[+] release: %s, network: %s, extra: %s" % (release, farmer, extra))
return generic_image_generator(release, farmer, extra, "mkkrn.sh", "ipxe.lkrn", "ipxe-%s.lkrn" % release)
@app.route('/uefi/<release>', methods=['GET'])
def uefi_release(release):
return uefi_release_farmer_extra(release, "", "")
@app.route('/uefi/<release>/<farmer>', methods=['GET'])
def uefi_release_farmer(release, farmer):
return uefi_release_farmer_extra(release, farmer, "")
@app.route('/uefi/<release>/<farmer>/<extra>', methods=['GET'])
def uefi_release_farmer_extra(release, farmer, extra):
print("[+] release: %s, network: %s, extra: %s" % (release, farmer, extra))
return generic_image_generator(release, farmer, extra, "mkuefi.sh", "ipxe.efi", "ipxe-%s.efi" % release)
@app.route('/uefi/<release>/<farmer>/<extra>/<kernel>', methods=['GET'])
def uefi_release_farmer_extra_kernel(release, farmer, extra, kernel):
print("[+] release: %s, network: %s, extra: %s [kernel: %s]" % (release, farmer, extra, kernel))
return generic_image_generator(release, farmer, extra, "mkuefi.sh", "ipxe.efi", "ipxe-%s.efi" % release, kernel)
@app.route('/snponly/<release>/<farmer>/<extra>', methods=['GET'])
def snponly_release_farmer_extra(release, farmer, extra):
print("[+] snponly, release: %s, network: %s, extra: %s" % (release, farmer, extra))
return generic_image_generator(release, farmer, extra, "mksnponly.sh", "ipxe.efi", "ipxe-snp-%s.efi" % release)
@app.route('/uefimg/<release>', methods=['GET'])
def uefimg_release(release):
return uefimg_release_farmer_extra(release, "", "")
@app.route('/uefimg/<release>/<farmer>', methods=['GET'])
def uefimg_release_farmer(release, farmer):
return uefimg_release_farmer_extra(release, farmer, "")
@app.route('/uefimg/<release>/<farmer>/<extra>', methods=['GET'])
def uefimg_release_farmer_extra(release, farmer, extra):
print("[+] release: %s, network: %s, extra: %s" % (release, farmer, extra))
return generic_image_generator(release, farmer, extra, "mkuefimg.sh", "uefimg.img", "uefiusb-%s.img" % release)
@app.route('/uefimg/<release>/<farmer>/<extra>/<kernel>', methods=['GET'])
def uefimg_release_farmer_extra_kernel(release, farmer, extra, kernel):
print("[+] release: %s, network: %s, extra: %s [kernel: %s]" % (release, farmer, extra, kernel))
return generic_image_generator(release, farmer, extra, "mkuefimg.sh", "uefimg.img", "uefiusb-%s.img" % release, kernel)
@app.route('/debug/<release>/<farmer>', methods=['GET'])
def uefimg_debug_release(release, farmer):
return generic_debug_image_generator(release, farmer, "", "mkuefimg.sh", "uefimg.img", "uefiusb-debug.img")
@app.route('/ipxe/<release>', methods=['GET'])
def ipxe_release(release):
return ipxe_release_farmer_extra(release, "", "")
@app.route('/ipxe/<release>/<farmer>', methods=['GET'])
def ipxe_release_farmer(release, farmer):
return ipxe_release_farmer_extra(release, farmer, "")
@app.route('/ipxe/<release>/<farmer>/<extra>', methods=['GET'])
def ipxe_release_farmer_extra(release, farmer, extra):
print("[+] release: %s, network: %s, extra: %s" % (release, farmer, extra))
return text_reply(ipxe_script(release, farmer, extra))
@app.route('/ipxe/<release>/<farmer>/<extra>/<kernel>', methods=['GET'])
def ipxe_release_farmer_extra_kernel(release, farmer, extra, kernel):
print("[+] release: %s, network: %s, extra: %s [kernel: %s]" % (release, farmer, extra, kernel))
return text_reply(ipxe_script(release, farmer, extra, kernel))
@app.route('/provision/<client>')
def provision_client(client):
print("[+] provisioning client: %s" % client)
db = db_open()
t = (client,)
c = db.cursor()
c.execute('SELECT client, release, zerotier, kargs FROM provision WHERE client = ?', t)
data = c.fetchone()
db.close()
if data is None:
print("[-] no client registered with this identifier")
abort(404)
return text_reply(ipxe_quick_script(data[1], data[2], data[3]))
#
# Helpers
#
#
# Web Frontend Routing
#
@app.route('/', methods=['GET'])
def homepage():
content = {
"baseurl": baseurl(),
}
return render_template("generate.html", **content)
@app.route('/images', methods=['GET'])
def kernels():
content = {
'files': kernel_list(),
}
return render_template("kernels.html", **content)
@app.route('/generate', methods=['GET'])
def generate():
content = {
"baseurl": baseurl(),
}
return render_template("generate.html", **content)
@app.route('/expert', methods=['GET'])
def expert():
content = {
"baseurl": baseurl(),
"kernels": kernel_list()
}
return render_template("expert.html", **content)
@app.route('/api/images', methods=['GET'])
def api_kernels():
return jsonify(kernel_list())
@app.route('/api/kernel', methods=['POST'])
def api_kernel():
if not request.cookies.get("token"):
return {'status': 'error', 'message': 'token not found'}
if request.cookies.get("token") != config['api-token']:
return {'status': 'error', 'message': 'invalid token'}
if 'kernel' not in request.files:
return {'status': 'error', 'message': 'no file found'}
file = request.files['kernel']
if file.filename == '':
return {'status': 'error', 'message': 'no file selected'}
source = os.path.join(config['kernel-path'], file.filename)
print(source)
file.save(source)
return {'status': 'success'}
@app.route('/api/symlink/<linkname>/<target>')
def api_symlink(linkname, target):
if not request.cookies.get("token"):
return {'status': 'error', 'message': 'token not found'}
if request.cookies.get("token") != config['api-token']:
return {'status': 'error', 'message': 'invalid token'}
check = os.path.join(config['kernel-path'], linkname)
if os.path.islink(check) or os.path.isfile(check):
os.remove(check)
now = os.getcwd()
os.chdir(config['kernel-path'])
os.symlink(target, linkname)
os.chdir(now)
return {'status': 'success'}
print("[+] listening")
app.run(host="0.0.0.0", port=config['http-port'], debug=config['debug'], threaded=True)