forked from clayrisser/forkbuntu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2238dc0
commit 91d3bf1
Showing
9 changed files
with
164 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
from .pack import Pack | ||
from .setup import Setup | ||
from .unpack import Unpack | ||
from .util import Util |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters