Skip to content

Commit

Permalink
Added gpg key generation
Browse files Browse the repository at this point in the history
  • Loading branch information
clayrisser committed May 24, 2018
1 parent 2238dc0 commit 91d3bf1
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ all: clean

.PHONY: start
start: env
@cd example && ../env/bin/python3 ../src
@cd example && ../env/bin/python3 ../forkbuntu

.PHONY: debug
debug: env
@cd example && ../env/bin/python3 ../src --debug
@cd example && ../env/bin/python3 ../forkbuntu --debug

.PHONY: install
install: env
Expand Down
6 changes: 5 additions & 1 deletion forkbuntu/controllers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ class Meta:
def default(self):
log = self.app.log
c = self.app.conf
s = self.app.services
self.app.spinner = Halo(text='initializing').start()
self.app.services.setup.init()
s.setup.init()
self.app.spinner.succeed('initialized')
setattr(self.app, 'gpg_keys', s.gpg.get_keys())
gpg_keys = self.app.gpg_keys
log.debug('gpg_keys: ' + json.dumps(gpg_keys, indent=4, sort_keys=True))
self.app.spinner = Halo(text='unpacking iso').start()
self.app.services.unpack.iso()
self.app.spinner.succeed('unpacked iso')
Expand Down
2 changes: 1 addition & 1 deletion forkbuntu/logger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 1

formatters:
simple:
format: '%(asctime)s - %(levelname)s: %(message)s'
format: '%(levelname)s: %(message)s'
datefmt: '%Y/%m/%d %H:%M:%S'

handlers:
Expand Down
1 change: 1 addition & 0 deletions forkbuntu/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from .pack import Pack
from .setup import Setup
from .unpack import Unpack
from .util import Util
6 changes: 6 additions & 0 deletions forkbuntu/services/extras.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from cfoundation import Service

class Extras(Service):
def create(self):
log = self.app.log
c = self.app.conf
79 changes: 77 additions & 2 deletions forkbuntu/services/gpg.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,82 @@
from cfoundation import Service
from getpass import getuser
from os import path
from munch import Munch
from subprocess import Popen
from time import sleep
import grp
import os
import pwd
import re
import json

class GPG(Service):
def setup(self):
os.system('gpgconf --kill gpg-agent')
os.system('gpg-agent --daemon --keep-tty --pinentry-program=$(which pinentry-curses)')
s = self.app.services
gpg_path = path.join(path.expanduser('~'), '.gnupg')
private_keys_path = path.join(gpg_path, 'private-keys-v1.d')
os.environ['GPG_TTY'] = s.util.subproc('tty')
if not path.exists(private_keys_path):
user = s.util.get_real_user()
os.makedirs(private_keys_path)
s.util.subproc('chown -R ' + user + ':' + user + ' ' + gpg_path)
s.util.subproc('gpgconf --kill gpg-agent', real_user=True)
s.util.subproc('gpg-agent --daemon --keep-tty --pinentry-program=$(which pinentry-curses)', real_user=True)

def gen_key(self):
s = self.app.services
gpg_path = path.join(path.expanduser('~'), '.gnupg')
user = s.util.get_real_user()
s.util.subproc('chown -R ' + user + ':' + user + ' ' + gpg_path)
s.util.subproc('rngd -r /dev/urandom')
s.util.subproc('gpg --gen-key', real_user=True)

def get_keys(self, trying_again=False):
s = self.app.services
keys = []
stdout = s.util.subproc('gpg --keyid-format LONG --list-keys')
matches = re.split(r'-{4}\n', stdout)
stdout_keys = []
if len(matches) > 1:
stdout_keys = list(filter(None, matches[1].split('\n\n')))
for stdout_key in stdout_keys:
key = Munch()
pubkeyshort = ''
lines = stdout_key.split('\n')
if (len(lines) >= 3):
match = next(re.finditer(r'(pub\s+)(\w+)\/(\w+)\s+([\w-]+)', lines[0]), None)
if match:
key_long = match.groups()[2].strip()
pub_key_short = key_long[8:]
key.pub = Munch({
'cipher': match.groups()[1].strip(),
'key': {
'long': key_long,
'short': pub_key_short,
},
'date': match.groups()[3].strip()
})
match = next(re.finditer(r'(uid\s+\[\w+]\s+)(.+)(<[^<>]+>)', lines[1]), None)
if match:
email = match.groups()[2].strip()
key.name = match.groups()[1].strip()
key.email = email[1:len(email) - 1]
match = next(re.finditer(r'(sub\s+)(\w+)\/(\w+)\s+([\w-]+)', lines[2]), None)
if match:
key_long = match.groups()[2].strip()
key.sub = Munch({
'cipher': match.groups()[1].strip(),
'key': {
'long': key_long,
'short': key_long[8:],
},
'date': match.groups()[3].strip()
})
keys.append(key)
if len(keys) <= 0:
if trying_again:
self.app.spinner.fail('failed to load gpg keys')
exit(1)
self.gen_key()
return self.get_keys(trying_again=True)
return keys
3 changes: 2 additions & 1 deletion forkbuntu/services/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

class Setup(Service):
def init(self):
s = self.app.services
if os.geteuid() != 0:
self.app.spinner.fail('please run as root')
exit(1)
self.app.services.gpg.setup()
s.gpg.setup()
69 changes: 69 additions & 0 deletions forkbuntu/services/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from cfoundation import Service
from os import path
from subprocess import check_output, CalledProcessError, STDOUT, Popen, DEVNULL
import os
import re
import pwd

class Util(Service):
def subproc(self, command, real_user=None):
log = self.app.log
log.debug('command: ' + command)
try:
stdout = ''
if real_user:
stdout = self.__demoted_subproc(command)
else:
stdout = check_output(
command,
stderr=STDOUT,
shell=True
).decode('utf-8')
log.debug(stdout)
return stdout
except CalledProcessError as err:
self.app.spinner.fail('subprocess command failed')
if err.output:
log.error(err.output.decode('utf-8'))
else:
raise err
if self.app.pargs.debug:
raise err
exit(1)

def get_real_user(self):
user = os.environ['SUDO_USER'] if 'SUDO_USER' in os.environ else os.environ['USER']
matches = re.findall(r'[^\/]+$', path.expanduser('~'))
if len(matches) > 0:
user = matches[0]
return user

def __demoted_subproc(self, command):
pargs = self.app.pargs
f = DEVNULL
if pargs.debug:
f = None
command = command.split()
user = self.get_real_user()
cwd = os.getcwd()
pw = pwd.getpwnam(user)
env = os.environ.copy()
env['HOME'] = pw.pw_dir
env['LOGNAME'] = pw.pw_name
env['PWD'] = cwd
env['USER'] = pw.pw_name
proc = Popen(
command,
preexec_fn=self.__demote(pw.pw_uid, pw.pw_gid),
cwd=cwd,
env=env,
stdout=f,
stderr=f
)
return proc.wait()

def __demote(self, uid, gid):
def result():
os.setgid(gid)
os.setuid(uid)
return result
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
setup(
name='forkbuntu',

version='0.1.5',
version='0.1.6',

description='Easily create your own ubuntu distribution and install cd',

Expand Down

0 comments on commit 91d3bf1

Please sign in to comment.