forked from wybczu/warlug-ksp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kspd.py
77 lines (59 loc) · 1.86 KB
/
kspd.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
#!/usr/bin/python
# -*- coding: utf8 -*-
# Simple keysigning party key submission server
# Based on https://github.com/FOSDEM/keysigning
import os
import tempfile
import subprocess
import shutil
from flask import Flask, request
GPG_PATH = '/usr/bin/gpg'
GPG_HOME_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'gpg-home')
GPG_FLAGS = ['-q', '--no-options', '--no-default-keyring',
'--homedir', GPG_HOME_DIR]
GPG_KEY_FIELDS = [
'type',
'trust',
'keylen',
'algorithm',
'keyid',
'creationdate',
'expirationdate',
'serial',
'ownertrust',
'uid',
'_',
]
GPG_KEY_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'keys')
app = Flask(__name__)
@app.route('/pks/add', methods=['POST'])
def submit_key():
key_text = request.form['keytext']
key_meta = {}
with tempfile.NamedTemporaryFile() as temp:
temp.write(key_text)
temp.flush()
try:
gpg = subprocess.check_output(
[GPG_PATH] + GPG_FLAGS + ["--with-colons", temp.name],
stderr=open('/dev/null', 'w')
)
for line in gpg.splitlines():
if line.startswith('pub:'):
key_meta = dict(zip(GPG_KEY_FIELDS, line.split(':')))
except subprocess.CalledProcessError:
return 'Invalid data', 400
if not key_meta['keyid'] or not key_meta['uid']:
return 'Invalid data', 400
try:
shutil.copy(temp.name,
os.path.join(GPG_KEY_DIR, key_meta['keyid']))
except IOError:
return 'Internal error', 500
return 'Key successfully submitted', 200
@app.errorhandler(404)
def not_implemented(e):
return 'This keyserver only accepts submissions', 404
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=11371)