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
Showing
5 changed files
with
122 additions
and
117 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env python | ||
|
||
import sys | ||
from sala import main | ||
|
||
sys.exit(main() or 0) |
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,39 @@ | ||
import ConfigParser | ||
import os | ||
|
||
class Configuration(object): | ||
DEFAULTS = { | ||
'cipher': 'AES256', | ||
'key-length': 64, | ||
'password-generator': 'pwgen -nc 12 10', | ||
} | ||
|
||
def __init__(self): | ||
self.parser = ConfigParser.RawConfigParser() | ||
|
||
self.parser.add_section('sala') | ||
for k, v in self.DEFAULTS.items(): | ||
self.parser.set('sala', k, v) | ||
|
||
xdg_config_home = os.environ.get('XDG_CONFIG_HOME') | ||
if xdg_config_home is None: | ||
xdg_config_home = os.path.expanduser('~/.config') | ||
|
||
config_files = [ | ||
os.path.expanduser('~/.sala.conf'), | ||
os.path.join(xdg_config_home, 'sala.conf'), | ||
'sala.conf', | ||
] | ||
|
||
self.parser.read(config_files) | ||
|
||
def __getattr__(self, key): | ||
# Proxies ConfigParser getters like this: | ||
# | ||
# config.getint(x) -> config.parser.getint('sala', x) | ||
# | ||
|
||
if key not in ['get', 'getint', 'getfloat', 'getboolean']: | ||
raise AttributeError(key) | ||
|
||
return lambda x: getattr(self.parser, key)('sala', x) |
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,72 @@ | ||
import contextlib | ||
import os | ||
import tempfile | ||
|
||
import GnuPGInterface | ||
|
||
def gpg_encrypt(config, filename, passphrase, content): | ||
logger = tempfile.TemporaryFile() | ||
stderr = tempfile.TemporaryFile() | ||
target = open(filename + '.tmp', 'w') | ||
|
||
with contextlib.nested(logger, stderr, target): | ||
gnupg = GnuPGInterface.GnuPG() | ||
gnupg.options.armor = 1 | ||
gnupg.options.meta_interactive = 0 | ||
gnupg.options.extra_args = ['--cipher-algo', config.get('cipher')] | ||
|
||
p = gnupg.run( | ||
['--symmetric'], | ||
create_fhs=['stdin', 'passphrase'], | ||
attach_fhs={ | ||
'stdout': target, | ||
'stderr': stderr, | ||
'logger': logger, | ||
}) | ||
|
||
p.handles['passphrase'].write(passphrase) | ||
p.handles['passphrase'].close() | ||
|
||
p.handles['stdin'].write(content) | ||
p.handles['stdin'].close() | ||
|
||
try: | ||
p.wait() | ||
except IOError, exc: | ||
print >>sys.stderr, exc | ||
os.remove(filename + '.tmp') | ||
else: | ||
os.rename(filename + '.tmp', filename) | ||
|
||
|
||
def gpg_decrypt(filename, passphrase): | ||
logger = tempfile.TemporaryFile() | ||
stderr = tempfile.TemporaryFile() | ||
source = open(filename) | ||
|
||
with contextlib.nested(logger, stderr, source): | ||
gnupg = GnuPGInterface.GnuPG() | ||
gnupg.options.armor = 1 | ||
gnupg.options.meta_interactive = 0 | ||
|
||
p = gnupg.run( | ||
['--decrypt'], | ||
create_fhs=['stdout', 'passphrase'], | ||
attach_fhs={ | ||
'stdin': source, | ||
'stderr': stderr, | ||
'logger': logger, | ||
}) | ||
|
||
p.handles['passphrase'].write(passphrase) | ||
p.handles['passphrase'].close() | ||
|
||
content = p.handles['stdout'].read() | ||
p.handles['stdout'].close() | ||
|
||
try: | ||
p.wait() | ||
except IOError: | ||
return '' | ||
else: | ||
return content |
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