Gitiles
blob: 6812f783232cf09171b8ad493548a481c4bf9065 [file] [
log] [
blame]
# password store management tool
# Copyright (c) 2008, 2009, 2011, 2013 Peter Palfrader <peter@palfrader.org>
# Copyright (c) 2014 Fastly
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Thread.abort_on_exception = true
GROUP_PATTERN = "@[a-zA-Z0-9_-]+"
USER_PATTERN = "[a-zA-Z0-9:_-]+"
$program_name = File.basename($0, '.*')
CONFIG_FILE = ENV['HOME']+ "/.pws.yaml"
%w{/usr/bin/sensible-editor /usr/bin/editor /usr/bin/vi}.each do |editor|
if FileTest.executable?(editor)
STDERR.puts "Cannot find an editor"
def GnuPG.extra_args=(val)
def GnuPG.readwrite3(intxt, infd, stdoutfd, stderrfd, statusfd=nil)
outtxt, stderrtxt, statustxt = ''
thread_out = Thread.new {
thread_err = Thread.new {
thread_status = Thread.new {
statustxt = statusfd.read
thread_status.join if thread_status
return outtxt, stderrtxt, statustxt
def GnuPG.open3call(cmd, intxt, args, require_success = false, do_status=true)
statR, statW = IO.pipe if do_status
fds[statW.fileno] = statW
exec(cmd, "--status-fd=#{statW.fileno}", *(@@extra_args + args), fds)
exec(cmd, *(@@extra_args + args), fds)
outW.puts("[PWSEXECERROR]: #{e}")
raise ("Calling gnupg failed")
(outtxt, stderrtxt, statustxt) = readwrite3(intxt, inW, outR, errR, statR);
(outtxt, stderrtxt) = readwrite3(intxt, inW, outR, errR);
wpid, status = Process.waitpid2 pid
throw "Unexpected pid: #{pid} vs #{wpid}" unless pid == wpid
throw "Process has not exited!?" unless status.exited?
if (require_success and status.exitstatus != 0)
STDERR.puts "#{cmd} call did not exit sucessfully."
STDERR.puts "output on stdout:"
STDERR.puts "output on stderr:"
STDERR.puts "output on statusfd:"
if m=/^\[PWSEXECERROR\]: (.*)/.match(outtxt) then
STDERR.puts "Could not run GnuPG: #{m[1]}"
return outtxt, stderrtxt, statustxt, status.exitstatus
return outtxt, stderrtxt, status.exitstatus
def GnuPG.gpgcall(intxt, args, require_success = false)
return open3call(GNUPG, intxt, args, require_success)
(outtxt, stderrtxt, statustxt) = GnuPG.gpgcall('', %w{--fast-list-mode --with-colons --with-fingerprint --list-secret-keys}, true)
outtxt.split("\n").each do |line|
if (parts[0] == "ssb" or parts[0] == "sec")
elsif (parts[0] == "fpr")
# This is for my private keys, so we can tell if a file is encrypted to us
# And this is for my private keys also, so we can tell if we are encrypting to ourselves
# This maps public keyids to fingerprints, so we can figure
# out if a file that is encrypted to a bunch of keys is
# encrypted to the fingerprints it should be encrypted to
def GnuPG.get_fpr_from_keyid(keyid)
fpr = @@keyid_fpr_mapping[keyid]
# this can be null, if we tried to find the fpr but failed to find the key in our keyring
STDERR.puts "Warning: No key found for keyid #{keyid}"
def GnuPG.get_fprs_from_keyids(keyids)
learn_fingerprints_from_keyids(keyids)
return keyids.collect{ |k| get_fpr_from_keyid(k) or "unknown" }
# this is to load the keys we will soon be asking about into
# our keyid-fpr-mapping hash
def GnuPG.learn_fingerprints_from_keyids(keyids)
need_to_learn = keyids.reject{ |k| @@keyid_fpr_mapping.has_key?(k) }
if need_to_learn.size > 0
# we can't use --fast-list-mode here because GnuPG is broken
# and does not show elmo's fingerprint in a call like
# gpg --with-colons --fast-list-mode --with-fingerprint --list-key D7C3F131AB2A91F5
args = %w{--with-colons --with-fingerprint --list-keys}
args.push("--keyring=./.keyring", "--no-default-keyring") if FileTest.exists?(".keyring")
args.concat need_to_learn
(outtxt, stderrtxt, statustxt) = GnuPG.gpgcall('', args)
outtxt.split("\n").each do |line|
elsif (parts[0] == "fpr") and fpr.nil?
@@keyid_fpr_mapping[pub] = fpr
elsif (parts[0] == "sub")
@@keyid_fpr_mapping[parts[4]] = fpr
need_to_learn.reject{ |k| @@keyid_fpr_mapping.has_key?(k) }.each { |k| @@keyid_fpr_mapping[k] = nil }
def read_input(query, default_yes=true)
print "#{query} #{append} "
i = STDIN.readline.chomp.downcase
def initialize(dirname=".", trusted_users=nil)
@trusted_users_source = trusted_users
load_deprecated_trusted_users()
elsif FileTest.exists?(CONFIG_FILE)
yaml = YAML::load_file(CONFIG_FILE)
yaml["trusted_users"].each do |k,v|
t[File.expand_path(k)] = v
@trusted_users_source = CONFIG_FILE
d = File.expand_path(dirname)
if @trusted_users.nil? or d == "/"
raise ("Could not find #{File.expand_path(dirname)} or its parents in configuration file #{CONFIG_FILE}")
rescue Psych::SyntaxError, ArgumentError => e
raise("Could not parse YAML: #{e.message}")
@trusted_users_source = ENV['HOME']+'/.pws-trusted-users'
load_deprecated_trusted_users()
def load_deprecated_trusted_users()
f = File.open(@trusted_users_source)
f.readlines.each do |line|
args.push "--keyring=#{@dirname}/.keyring" if FileTest.exists?(File.join(@dirname, ".keyring"))
(outtxt, stderrtxt, statustxt, exitstatus) = GnuPG.gpgcall(content, args)
statustxt.split("\n").each do |line|
if m = /^\[GNUPG:\] GOODSIG/.match(line)
elsif m = /^\[GNUPG:\] VALIDSIG \S+ \S+ \S+ \S+ \S+ \S+ \S+ \S+ \S+ ([0-9A-F]+)/.match(line)
STDERR.puts ".users file is not signed properly. GnuPG said on stdout:"
STDERR.puts "and on stderr:"
STDERR.puts "and via statusfd:"
if not @trusted_users.include?(validsig)
raise ".users file is signed by #{validsig} which is not in #{@trusted_users_source}"
raise "gpg verify failed for .users file"
f = File.open(File.join(@dirname, '.users'))
users.split("\n").each do |line|
if (m = /^(#{USER_PATTERN})\s*=\s*([0-9A-Fa-f]{40})\s*$/.match line)
STDERR.puts "User #{user} redefined at line #{lno}!"
elsif (m = /^(#{GROUP_PATTERN})\s*=\s*(.*)$/.match line)
if @groups.has_key?(group)
STDERR.puts "Group #{group} redefined at line #{lno}!"
members = members.split(/[\t ,]+/)
@groups[group] = { "members" => members }
def check_exists(x, whence, fatal=true)
ok=false unless (@groups.has_key?(x))
ok=false unless @users.has_key?(x)
STDERR.puts( (fatal ? "Error: " : "Warning: ") + "#{whence} contains unknown member #{x}")
@groups.each_pair do |groupname, group|
group['members'].each do |member|
check_exists(member, "Group #{groupname}")
group['members_to_do'] = group['members'].clone
@groups.each_pair do |groupname, group|
group['keys'] = [] unless group['keys']
still_contains_groups = false
group['members_to_do'].clone.each do |member|
if @groups[member]['members_to_do'].size == 0
group['keys'].concat @groups[member]['keys']
group['members_to_do'].delete(member)
still_contains_groups = true
group['keys'].push @users[member]
group['members_to_do'].delete(member)
all_expanded = false if still_contains_groups
cyclic_groups = @groups.keys.reject{|name| @groups[name]['members_to_do'].size == 0}.join(", ")
STDERR.puts "Cyclic group memberships in #{cyclic_groups}?"
def expand_targets(targets)
unless check_exists(t, "access line", false)
fprs.concat @groups[t]['keys']
attr_reader :accessible, :encrypted, :readable, :readers
def EncryptedData.determine_readable(readers)
GnuPG.get_my_keys.each do |keyid|
return true if readers.include?(keyid)
def EncryptedData.list_readers(statustxt)
statustxt.split("\n").each do |line|
m = /^\[GNUPG:\] ENC_TO ([0-9A-F]+)/.match line
def EncryptedData.targets(text)
text.split("\n").each do |line|
m = /^access: "?((?:(?:#{GROUP_PATTERN}|#{USER_PATTERN}),?\s*)+)"?/.match line
return m[1].strip.split(/[\t ,]+/)
def initialize(encrypted_content, label, keyring_directory = ".", ignore_decrypt_errors = false)
@ignore_decrypt_errors = ignore_decrypt_errors
@keyring_dir = keyring_directory
@encrypted_content = encrypted_content
(outtxt, stderrtxt, statustxt) = GnuPG.gpgcall(@encrypted_content, %w{--with-colons --no-options --no-default-keyring --secret-keyring=/dev/null --keyring=/dev/null})
@encrypted = !(statustxt =~ /\[GNUPG:\] NODATA/)
@readers = EncryptedData.list_readers(statustxt)
@readable = EncryptedData.determine_readable(@readers)
(outtxt, stderrtxt, statustxt, exitstatus) = GnuPG.gpgcall(@encrypted_content, %w{--decrypt})
if !@ignore_decrypt_errors and exitstatus != 0
proceed = read_input("Warning: gpg returned non-zero exit status #{exitstatus} when decrypting #{@label}. Proceed?", false)
elsif !@ignore_decrypt_errors and outtxt.length == 0
proceed = read_input("Warning: #{@label} decrypted to an empty file. Proceed?")
def encrypt(content, recipients)
args = recipients.collect{ |r| "--recipient=#{r}"}
args.push "--trust-model=always"
args.push("--keyring=#{@keyring_dir}/.keyring", "--no-default-keyring") if FileTest.exists?("#{@keyring_dir}/.keyring")
(outtxt, stderrtxt, statustxt, exitstatus) = GnuPG.gpgcall(content, args)
statustxt.split("\n").each do |line|
m = /^\[GNUPG:\] INV_RECP \S+ ([0-9A-F]+)/.match line
again = read_input("Warning: the following recipients are invalid: #{invalid.join(", ")}. Try again (or proceed)?")
tryagain = read_input("Error: #{@label} encrypted to an empty file. Edit again (or exit)?")
proceed = read_input("Warning: gpg returned non-zero exit status #{exitstatus} when encrypting #{@label}. Said:\n#{stderrtxt}\n#{statustxt}\n\nProceed (or try again)?")
return false unless proceed
def determine_encryption_targets(content)
targets = EncryptedData.targets(content)
tryagain = read_input("Warning: Did not find targets to encrypt to in header. Try again (or exit)?", true)
ok, expanded = @groupconfig.expand_targets(targets)
tryagain = read_input("Errors in access header. Edit again (or exit)?", true)
tryagain = read_input("Warnings in access header. Edit again (or continue)?", true)
GnuPG.get_my_fprs.each do |fpr|
if expanded.include?(fpr)
tryagain = read_input("File is not being encrypted to you. Edit again (or continue)?", true)
class EncryptedFile < EncryptedData
def initialize(filename, new=false, trusted_file=nil)
@groupconfig = GroupConfig.new(dirname=File.dirname(filename), trusted_users=trusted_file)
@accessible = FileTest.readable?(filename)
encrypted_content = File.read(filename)
super(encrypted_content, filename, @groupconfig.dirname, @new)
def write_back(content, targets)
ok, encrypted = encrypt(content, targets)
File.open(@filename,"w").write(encrypted)
def help(parser, code=0, io=STDOUT)
io.puts "Usage: #{$program_name} ls [<directory> ...]"
io.puts "Lists the contents of the given directory/directories, or the current"
io.puts "directory if none is given. For each file show whether it is PGP-encrypted"
io.puts "file, and if yes whether we can read it."
unless FileTest.exists?(".users")
STDERR.puts "The .users file does not exists here. This is not a password store, is it?"
dir.sort.each do |filename|
next if (filename =~ /^\./) and not (@all >= 3)
stat = File::Stat.new(filename)
puts "(sym) #{filename}" if (@all >= 2)
puts "(dir) #{filename}" if (@all >= 2)
puts "(other) #{filename}" if (@all >= 2)
f = EncryptedFile.new(filename)
puts "(!perm) #{filename}"
puts "(file) #{filename}" if (@all >= 2)
puts "(locked) #{filename}" if (@all >= 1)
opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
opts.on_tail("-a", "--all" , "Show all files (use up to 3 times to show even more than all)") { @all = @all+1 }
dirs.push('.') unless dirs.size > 0
dirs.each { |dir| ls_dir(dir) }
def help(parser, code=0, io=STDOUT)
io.puts "Usage: #{$program_name} ed <filename>"
io.puts "Decrypts the file, spawns an editor, and encrypts it again"
tempfile = Tempfile.open('pws')
system($editor, tempfile.path)
throw "Process has not exited!?" unless status.exited?
unless status.exitstatus == 0
proceed = read_input("Warning: Editor did not exit successfully (exit code #{status.exitstatus}. Proceed?")
# some editors do not write new content in place, but instead
# make a new file and more it in the old file's place.
reopened = File.open(tempfile.path, "r+")
# zero the file, well, both of them.
clearsize = (newsize > oldsize) ? newsize : oldsize
[tempfile, reopened].each do |f|
encrypted_file = EncryptedFile.new(filename, @new)
if !@new and !encrypted_file.readable && !@force
STDERR.puts "#{filename} is probably not readable"
encrypted_to = GnuPG.get_fprs_from_keyids(encrypted_file.readers).sort
content = encrypted_file.decrypt
original_content = content
content = do_edit(content)
proceed = read_input("Warning: Content is now empty. Proceed?")
ok, targets = encrypted_file.determine_encryption_targets(content)
if (original_content == content && ! @reencrypt_on_change)
if (targets.sort == encrypted_to)
STDERR.puts("Notice: list of keys changed -- re-encryption recommended. Run #{$program_name} rc #{filename}")
success = encrypted_file.write_back(content, targets)
def initialize(reencrypt_on_change=false)
opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
opts.on_tail("-n", "--new" , "Edit new file") { |new| @new=new }
opts.on_tail("-f", "--force" , "Spawn an editor even if the file is probably not readable") { |force| @force=force }
help(ARGV.options, 1, STDERR) if ARGV.length != 1
if FileTest.exists?(filename)
STDERR.puts "#{filename} does exist"
if !FileTest.exists?(filename)
STDERR.puts "#{filename} does not exist"
elsif !FileTest.file?(filename)
STDERR.puts "#{filename} is not a regular file"
elsif !FileTest.readable?(filename)
STDERR.puts "#{filename} is not accessible (unix perms)"
@reencrypt_on_change = reencrypt_on_change
dirname = File.dirname(filename)
basename = File.basename(filename)
def help(parser, code=0, io=STDOUT)
io.puts "Usage: #{$program_name} rc <filename>"
io.puts "Reencrypts the file"
def help(parser, code=0, io=STDOUT)
io.puts "Usage: #{$program_name} get <filename> <query>"
io.puts "Decrypts the file, fetches a key and outputs it to stdout."
io.puts "The file must be in YAML format."
io.puts "query is a query, formatted like /host/users/root"
encrypted_file = EncryptedFile.new(filename, @new)
if !encrypted_file.readable
STDERR.puts "#{filename} is probably not readable"
yaml = YAML::load(encrypted_file.decrypt)
rescue Psych::SyntaxError, ArgumentError => e
STDERR.puts "Could not parse YAML: #{e.message}"
a = what.split("/")[1..-1]
# q = /, so print top level keys
STDERR.puts("No such key or invalid lookup expression")
elsif hit.respond_to?(:keys)
opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
help(ARGV.options, 1, STDERR) if ARGV.length != 2
if !FileTest.exists?(filename)
STDERR.puts "#{filename} does not exist"
elsif !FileTest.file?(filename)
STDERR.puts "#{filename} is not a regular file"
elsif !FileTest.readable?(filename)
STDERR.puts "#{filename} is not accessible (unix perms)"
dirname = File.dirname(filename)
basename = File.basename(filename)
def help(parser, code=0, io=STDOUT)
io.puts "Usage: #{$program_name} update-keyring"
io.puts "Updates the local .keyring file"
opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
help(ARGV.options, 1, STDERR) if ARGV.length > 1
groupconfig = GroupConfig.new
users = groupconfig.get_users()
File.open('.keyring', 'w') do |keyring|
users.each_pair() do |uid, keyid|
user_keyfile = File.join('keys', "#{uid}.key")
if File.file?(user_keyfile)
keyring.write(File.read(user_keyfile))
puts "No key file found for user " + uid
def help(parser, code=0, io=STDOUT)
io.puts "Usage: #{$program_name} gitdiff <commit> <file>"
io.puts "Shows a diff between the version of <file> in your directory and the"
io.puts "version in git at <commit> (or HEAD). Requires that your tree be git"
io.puts "managed, obviously."
def check_readable(e, label)
if !e.readable && !@force
STDERR.puts "#{label} is probably not readable."
label = @commit+':'+@filename
(encrypted_content, stderrtxt, exitcode) = GnuPG.open3call('git', '', ['show', label], require_success=true, do_status=false)
data = EncryptedData.new(encrypted_content, label)
check_readable(data, label)
data = EncryptedFile.new(@filename)
check_readable(data, @filename)
old = get_file_at_commit()
t1 = Tempfile.open('pws')
t2 = Tempfile.open('pws')
system("diff", "-u", t1.path, t2.path)
opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
opts.on_tail("-f", "--force" , "Do it even if the file is probably not readable") { |force| @force=force }
help(ARGV.options, 1, STDERR)
def help(code=0, io=STDOUT)
io.puts "Usage: #{$program_name} ed"
io.puts " #{$program_name} rc"
io.puts " #{$program_name} ls"
io.puts " #{$program_name} gitdiff"
io.puts " #{$program_name} update-keyring"
io.puts " #{$program_name} help"
io.puts "Call #{$program_name} <command> --help for additional options/parameters"
when 'rc' then Reencrypt.new
when 'gitdiff' then GitDiff.new
when 'update-keyring' then KeyringUpdater.new