3 # password store management tool
5 # Copyright (c) 2008, 2009 Peter Palfrader <peter@palfrader.org>
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
15 # The above copyright notice and this permission notice shall be
16 # included in all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 Thread.abort_on_exception = true
33 GNUPG = "/usr/bin/gpg"
34 GROUP_PATTERN = "@[a-zA-Z0-9-]+"
35 $program_name = File.basename($0, '.*')
37 $editor = ENV['EDITOR']
39 %w{/usr/bin/sensible-editor /usr/bin/editor /usr/bin/vi}.each do |editor|
40 if FileTest.executable?(editor)
50 @@keyid_fpr_mapping = {}
52 def GnuPG.readwrite3(intxt, infd, stdoutfd, stderrfd, statusfd=nil)
53 outtxt, stderrtxt, statustxt = ''
54 thread_in = Thread.new {
58 thread_out = Thread.new {
59 outtxt = stdoutfd.read
62 thread_err = Thread.new {
63 errtxt = stderrfd.read
66 thread_status = Thread.new {
67 statustxt = statusfd.read
74 thread_status.join if thread_status
76 return outtxt, stderrtxt, statustxt
79 def GnuPG.open3call(cmd, intxt, args, require_success = false, do_status=true)
83 statR, statW = IO.pipe if do_status
89 statR.close if do_status
95 exec(cmd, "--status-fd=#{statW.fileno}", *args)
100 outW.puts("[PWSEXECERROR]: #{e}")
103 raise ("Calling gnupg failed")
110 (outtxt, stderrtxt, statustxt) = readwrite3(intxt, inW, outR, errR, statR);
112 (outtxt, stderrtxt) = readwrite3(intxt, inW, outR, errR);
114 wpid, status = Process.waitpid2 pid
115 throw "Unexpected pid: #{pid} vs #{wpid}" unless pid == wpid
116 throw "Process has not exited!?" unless status.exited?
117 throw "#{cmd} call did not exit sucessfully" if (require_success and status.exitstatus != 0)
118 if m=/^\[PWSEXECERROR\]: (.*)/.match(outtxt) then
119 STDERR.puts "Could not run GnuPG: #{m[1]}"
123 return outtxt, stderrtxt, statustxt, status.exitstatus
125 return outtxt, stderrtxt, status.exitstatus
129 def GnuPG.gpgcall(intxt, args, require_success = false)
130 return open3call(GNUPG, intxt, args, require_success)
133 def GnuPG.init_keys()
135 (outtxt, stderrtxt, statustxt) = GnuPG.gpgcall('', %w{--fast-list-mode --with-colons --with-fingerprint --list-secret-keys}, true)
138 outtxt.split("\n").each do |line|
139 parts = line.split(':')
140 if (parts[0] == "ssb" or parts[0] == "sec")
141 @@my_keys.push parts[4]
142 elsif (parts[0] == "fpr")
143 @@my_fprs.push parts[9]
147 # This is for my private keys, so we can tell if a file is encrypted to us
148 def GnuPG.get_my_keys()
152 # And this is for my private keys also, so we can tell if we are encrypting to ourselves
153 def GnuPG.get_my_fprs()
158 # This maps public keyids to fingerprints, so we can figure
159 # out if a file that is encrypted to a bunch of keys is
160 # encrypted to the fingerprints it should be encrypted to
161 def GnuPG.get_fpr_from_keyid(keyid)
162 fpr = @@keyid_fpr_mapping[keyid]
163 # this can be null, if we tried to find the fpr but failed to find the key in our keyring
165 STDERR.puts "Warning: No key found for keyid #{keyid}"
169 def GnuPG.get_fprs_from_keyids(keyids)
170 learn_fingerprints_from_keyids(keyids)
171 return keyids.collect{ |k| get_fpr_from_keyid(k) or "unknown" }
174 # this is to load the keys we will soon be asking about into
175 # our keyid-fpr-mapping hash
176 def GnuPG.learn_fingerprints_from_keyids(keyids)
177 need_to_learn = keyids.reject{ |k| @@keyid_fpr_mapping.has_key?(k) }
178 if need_to_learn.size > 0
179 # we can't use --fast-list-mode here because GnuPG is broken
180 # and does not show elmo's fingerprint in a call like
181 # gpg --with-colons --fast-list-mode --with-fingerprint --list-key D7C3F131AB2A91F5
182 args = %w{--with-colons --with-fingerprint --list-keys}
183 args.concat need_to_learn
184 (outtxt, stderrtxt, statustxt) = GnuPG.gpgcall('', args, true)
188 outtxt.split("\n").each do |line|
189 parts = line.split(':')
190 if (parts[0] == "pub")
192 elsif (parts[0] == "fpr")
194 @@keyid_fpr_mapping[pub] = fpr
195 elsif (parts[0] == "sub")
196 @@keyid_fpr_mapping[parts[4]] = fpr
200 need_to_learn.reject{ |k| @@keyid_fpr_mapping.has_key?(k) }.each { |k| @@keyid_fpr_mapping[k] = nil }
204 def read_input(query, default_yes=true)
212 print "#{query} #{append} "
214 i = STDIN.readline.chomp.downcase
236 f = File.open(ENV['HOME']+'/.pws-trusted-users')
237 rescue Exception => e
243 f.readlines.each do |line|
252 args.push "--keyring=./.keyring" if FileTest.exists?(".keyring")
253 (outtxt, stderrtxt, statustxt, exitstatus) = GnuPG.gpgcall(content, args)
256 statustxt.split("\n").each do |line|
257 if m = /^\[GNUPG:\] GOODSIG/.match(line)
259 elsif m = /^\[GNUPG:\] VALIDSIG \S+ \S+ \S+ \S+ \S+ \S+ \S+ \S+ \S+ ([0-9A-F]+)/.match(line)
265 STDERR.puts ".users file is not signed properly. GnuPG said on stdout:"
267 STDERR.puts "and on stderr:"
268 STDERR.puts stderrtxt
269 STDERR.puts "and via statusfd:"
270 STDERR.puts statustxt
274 if not trusted.include?(validsig)
275 STDERR.puts ".users file is signed by #{validsig} which is not in ~/.pws-trusted-users"
280 STDERR.puts "gpg verify failed for .users file"
289 f = File.open('.users')
290 rescue Exception => e
298 users = verify(users)
304 users.split("\n").each do |line|
308 if (m = /^([a-zA-Z0-9:-]+)\s*=\s*([0-9A-Fa-f]{40})\s*$/.match line)
311 if @users.has_key?(user)
312 STDERR.puts "User #{user} redefined at line #{lno}!"
316 elsif (m = /^(#{GROUP_PATTERN})\s*=\s*(.*)$/.match line)
319 if @groups.has_key?(group)
320 STDERR.puts "Group #{group} redefined at line #{lno}!"
323 members = members.split(/[\t ,]+/)
324 @groups[group] = { "members" => members }
330 return (name =~ /^@/)
332 def check_exists(x, whence, fatal=true)
335 ok=false unless (@groups.has_key?(x))
337 ok=false unless @users.has_key?(x)
340 STDERR.puts( (fatal ? "Error: " : "Warning: ") + "#{whence} contains unknown member #{x}")
346 @groups.each_pair do |groupname, group|
347 group['members'].each do |member|
348 check_exists(member, "Group #{groupname}")
350 group['members_to_do'] = group['members'].clone
356 @groups.each_pair do |groupname, group|
357 group['keys'] = [] unless group['keys']
359 still_contains_groups = false
360 group['members_to_do'].clone.each do |member|
362 if @groups[member]['members_to_do'].size == 0
363 group['keys'].concat @groups[member]['keys']
364 group['members_to_do'].delete(member)
367 still_contains_groups = true
370 group['keys'].push @users[member]
371 group['members_to_do'].delete(member)
375 all_expanded = false if still_contains_groups
377 break if all_expanded
379 cyclic_groups = @groups.keys.reject{|name| @groups[name]['members_to_do'].size == 0}.join(", ")
380 STDERR.puts "Cyclic group memberships in #{cyclic_groups}?"
386 def expand_targets(targets)
390 unless check_exists(t, "access line", false)
395 fprs.concat @groups[t]['keys']
409 attr_reader :accessible, :encrypted, :readable, :readers
411 def EncryptedData.determine_readable(readers)
412 GnuPG.get_my_keys.each do |keyid|
413 return true if readers.include?(keyid)
418 def EncryptedData.list_readers(statustxt)
420 statustxt.split("\n").each do |line|
421 m = /^\[GNUPG:\] ENC_TO ([0-9A-F]+)/.match line
428 def EncryptedData.targets(text)
429 text.split("\n").each do |line|
433 m = /^access: "?(#{GROUP_PATTERN})"?/.match line
435 return m[1].strip.split(/[\t ,]+/)
440 def initialize(encrypted_content, label)
441 @ignore_decrypt_errors = false
444 @encrypted_content = encrypted_content
445 (outtxt, stderrtxt, statustxt) = GnuPG.gpgcall(@encrypted_content, %w{--with-colons --no-options --no-default-keyring --secret-keyring=/dev/null --keyring=/dev/null})
446 @encrypted = !(statustxt =~ /\[GNUPG:\] NODATA/)
448 @readers = EncryptedData.list_readers(statustxt)
449 @readable = EncryptedData.determine_readable(@readers)
454 (outtxt, stderrtxt, statustxt, exitstatus) = GnuPG.gpgcall(@encrypted_content, %w{--decrypt})
455 if !@ignore_decrypt_errors and exitstatus != 0
456 proceed = read_input("Warning: gpg returned non-zero exit status #{exitstatus} when decrypting #{@label}. Proceed?", false)
457 exit(0) unless proceed
458 elsif !@ignore_decrypt_errors and outtxt.length == 0
459 proceed = read_input("Warning: #{@label} decrypted to an empty file. Proceed?")
460 exit(0) unless proceed
466 def encrypt(content, recipients)
467 args = recipients.collect{ |r| "--recipient=#{r}"}
468 args.push "--trust-model=always"
469 args.push "--keyring=./.keyring" if FileTest.exists?(".keyring")
471 args.push "--encrypt"
472 (outtxt, stderrtxt, statustxt, exitstatus) = GnuPG.gpgcall(content, args)
475 statustxt.split("\n").each do |line|
476 m = /^\[GNUPG:\] INV_RECP \S+ ([0-9A-F]+)/.match line
481 again = read_input("Warning: the following recipients are invalid: #{invalid.join(", ")}. Try again (or proceed)?")
482 return false if again
484 if outtxt.length == 0
485 tryagain = read_input("Error: #{@label} encrypted to an empty file. Edit again (or exit)?")
486 return false if tryagain
490 proceed = read_input("Warning: gpg returned non-zero exit status #{exitstatus} when encrypting #{@label}. Said:\n#{stderrtxt}\n#{statustxt}\n\nProceed (or try again)?")
491 return false unless proceed
498 def determine_encryption_targets(content)
499 targets = EncryptedData.targets(content)
501 tryagain = read_input("Warning: Did not find targets to encrypt to in header. Try again (or exit)?", true)
502 return false if tryagain
506 ok, expanded = @groupconfig.expand_targets(targets)
507 if (expanded.size == 0)
508 tryagain = read_input("Errors in access header. Edit again (or exit)?", true)
509 return false if tryagain
512 tryagain = read_input("Warnings in access header. Edit again (or continue)?", true)
513 return false if tryagain
517 GnuPG.get_my_fprs.each do |fpr|
518 if expanded.include?(fpr)
524 tryagain = read_input("File is not being encrypted to you. Edit again (or continue)?", true)
525 return false if tryagain
528 return true, expanded
533 class EncryptedFile < EncryptedData
534 def initialize(filename, new=false)
535 @groupconfig = GroupConfig.new
542 unless FileTest.readable?(filename)
550 encrypted_content = File.read(filename)
551 super(encrypted_content, filename)
554 def write_back(content, targets)
555 ok, encrypted = encrypt(content, targets)
556 return false unless ok
558 File.open(@filename,"w").write(encrypted)
564 def help(parser, code=0, io=STDOUT)
565 io.puts "Usage: #{$program_name} ls [<directory> ...]"
566 io.puts parser.summarize
567 io.puts "Lists the contents of the given directory/directories, or the current"
568 io.puts "directory if none is given. For each file show whether it is PGP-encrypted"
569 io.puts "file, and if yes whether we can read it."
575 dir = Dir.open(dirname)
576 rescue Exception => e
581 Dir.chdir(dirname) do
582 unless FileTest.exists?(".users")
583 STDERR.puts "The .users file does not exists here. This is not a password store, is it?"
586 dir.sort.each do |filename|
587 next if (filename =~ /^\./) and not (@all >= 3)
588 stat = File::Stat.new(filename)
590 puts "(sym) #{filename}" if (@all >= 2)
591 elsif stat.directory?
592 puts "(dir) #{filename}" if (@all >= 2)
594 puts "(other) #{filename}" if (@all >= 2)
596 f = EncryptedFile.new(filename)
598 puts "(!perm) #{filename}"
600 puts "(file) #{filename}" if (@all >= 2)
602 puts "(ok) #{filename}"
604 puts "(locked) #{filename}" if (@all >= 1)
613 ARGV.options do |opts|
614 opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
615 opts.on_tail("-a", "--all" , "Show all files (use up to 3 times to show even more than all)") { @all = @all+1 }
620 dirs.push('.') unless dirs.size > 0
621 dirs.each { |dir| ls_dir(dir) }
626 def help(parser, code=0, io=STDOUT)
627 io.puts "Usage: #{$program_name} ed <filename>"
628 io.puts parser.summarize
629 io.puts "Decrypts the file, spawns an editor, and encrypts it again"
634 encrypted_file = EncryptedFile.new(filename, @new)
635 if !@new and !encrypted_file.readable && !@force
636 STDERR.puts "#{filename} is probably not readable"
640 encrypted_to = GnuPG.get_fprs_from_keyids(encrypted_file.readers).sort
642 content = encrypted_file.decrypt
643 original_content = content
645 oldsize = content.length
646 tempfile = Tempfile.open('pws')
647 tempfile.puts content
649 system($editor, tempfile.path)
651 throw "Process has not exited!?" unless status.exited?
652 unless status.exitstatus == 0
653 proceed = read_input("Warning: Editor did not exit successfully (exit code #{status.exitstatus}. Proceed?")
654 exit(0) unless proceed
657 # some editors do not write new content in place, but instead
658 # make a new file and more it in the old file's place.
660 reopened = File.open(tempfile.path, "r+")
661 rescue Exception => e
665 content = reopened.read
667 # zero the file, well, both of them.
668 newsize = content.length
669 clearsize = (newsize > oldsize) ? newsize : oldsize
671 [tempfile, reopened].each do |f|
672 f.seek(0, IO::SEEK_SET)
673 f.print "\0"*clearsize
679 if content.length == 0
680 proceed = read_input("Warning: Content is now empty. Proceed?")
681 exit(0) unless proceed
684 ok, targets = encrypted_file.determine_encryption_targets(content)
687 if (original_content == content)
688 if (targets.sort == encrypted_to)
689 proceed = read_input("Nothing changed. Re-encrypt anyway?", false)
690 exit(0) unless proceed
692 STDERR.puts("Info: Content not changed but re-encrypting anyway because the list of keys changed")
696 success = encrypted_file.write_back(content, targets)
702 ARGV.options do |opts|
703 opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
704 opts.on_tail("-n", "--new" , "Edit new file") { |new| @new=new }
705 opts.on_tail("-f", "--force" , "Spawn an editor even if the file is probably not readable") { |force| @force=force }
708 help(ARGV.options, 1, STDERR) if ARGV.length != 1
709 filename = ARGV.shift
712 if FileTest.exists?(filename)
713 STDERR.puts "#{filename} does exist"
717 if !FileTest.exists?(filename)
718 STDERR.puts "#{filename} does not exist"
720 elsif !FileTest.file?(filename)
721 STDERR.puts "#{filename} is not a regular file"
723 elsif !FileTest.readable?(filename)
724 STDERR.puts "#{filename} is not accessible (unix perms)"
729 dirname = File.dirname(filename)
730 basename = File.basename(filename)
738 def help(parser, code=0, io=STDOUT)
739 io.puts "Usage: #{$program_name} get <filename> <query>"
740 io.puts parser.summarize
741 io.puts "Decrypts the file, fetches a key and outputs it to stdout."
742 io.puts "The file must be in YAML format."
743 io.puts "query is a query, formatted like host/users/root"
747 def get(filename, what)
748 encrypted_file = EncryptedFile.new(filename, @new)
749 if !encrypted_file.readable
750 STDERR.puts "#{filename} is probably not readable"
755 yaml = YAML::load(encrypted_file.decrypt)
756 rescue Psych::SyntaxError, ArgumentError => e
757 STDERR.puts "Could not parse YAML: #{e.message}"
769 STDERR.puts("No such key or invalid lookup expression")
770 elsif hit.respond_to?(:keys)
772 puts hit.keys.join("\n")
779 ARGV.options do |opts|
780 opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
783 help(ARGV.options, 1, STDERR) if ARGV.length != 2
784 filename = ARGV.shift
787 if !FileTest.exists?(filename)
788 STDERR.puts "#{filename} does not exist"
790 elsif !FileTest.file?(filename)
791 STDERR.puts "#{filename} is not a regular file"
793 elsif !FileTest.readable?(filename)
794 STDERR.puts "#{filename} is not accessible (unix perms)"
798 dirname = File.dirname(filename)
799 basename = File.basename(filename)
807 def help(parser, code=0, io=STDOUT)
808 io.puts "Usage: #{$program_name} update-keyring [<keyserver>]"
809 io.puts parser.summarize
810 io.puts "Updates the local .keyring file"
815 ARGV.options do |opts|
816 opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
819 help(ARGV.options, 1, STDERR) if ARGV.length > 1
820 keyserver = ARGV.shift
821 keyserver = 'keys.gnupg.net' unless keyserver
823 groupconfig = GroupConfig.new
824 users = groupconfig.get_users()
825 args = %w{--with-colons --no-options --no-default-keyring --keyring=./.keyring}
827 system('touch', '.keyring')
828 users.each_pair() do |uid, keyid|
830 cmd << "--keyserver=#{keyserver}"
833 puts "Fetching key for #{uid}"
834 (outtxt, stderrtxt, statustxt) = GnuPG.gpgcall('', cmd)
835 unless (statustxt =~ /^\[GNUPG:\] IMPORT_OK /)
836 STDERR.puts "Warning: did not find IMPORT_OK token in status output"
837 STDERR.puts "gpg exited with exit code #{ecode})"
838 STDERR.puts "Command was gpg #{cmd.join(' ')}"
839 STDERR.puts "stdout was #{outtxt}"
840 STDERR.puts "stderr was #{stderrtxt}"
841 STDERR.puts "statustxt was #{statustxt}"
845 cmd << '--batch' << '--edit' << keyid << 'minimize' << 'save'
846 (outtxt, stderrtxt, statustxt, ecode) = GnuPG.gpgcall('', cmd)
854 def help(parser, code=0, io=STDOUT)
855 io.puts "Usage: #{$program_name} gitdiff <commit> <file>"
856 io.puts parser.summarize
857 io.puts "Shows a diff between the version of <file> in your directory and the"
858 io.puts "version in git at <commit> (or HEAD). Requires that your tree be git"
859 io.puts "managed, obviously."
863 def check_readable(e, label)
864 if !e.readable && !@force
865 STDERR.puts "#{label} is probably not readable."
870 def get_file_at_commit()
871 label = @commit+':'+@filename
872 (encrypted_content, stderrtxt, exitcode) = GnuPG.open3call('git', '', ['show', label], require_success=true, do_status=false)
873 data = EncryptedData.new(encrypted_content, label)
874 check_readable(data, label)
878 def get_file_current()
879 data = EncryptedFile.new(@filename)
880 check_readable(data, @filename)
885 old = get_file_at_commit()
886 cur = get_file_current()
888 t1 = Tempfile.open('pws')
892 t2 = Tempfile.open('pws')
896 system("diff", "-u", t1.path, t2.path)
898 t1.seek(0, IO::SEEK_SET)
899 t1.print "\0"*old.length
903 t2.seek(0, IO::SEEK_SET)
904 t2.print "\0"*cur.length
910 ARGV.options do |opts|
911 opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
912 opts.on_tail("-f", "--force" , "Do it even if the file is probably not readable") { |force| @force=force }
918 @filename = ARGV.shift
919 elsif ARGV.length == 2
921 @filename = ARGV.shift
923 help(ARGV.options, 1, STDERR)
931 def help(code=0, io=STDOUT)
932 io.puts "Usage: #{$program_name} ed"
933 io.puts " #{$program_name} ls"
934 io.puts " #{$program_name} gitdiff"
935 io.puts " #{$program_name} update-keyring"
936 io.puts " #{$program_name} help"
937 io.puts "Call #{$program_name} <command> --help for additional options/parameters"
944 when 'ls' then Ls.new
945 when 'ed' then Ed.new
946 when 'gitdiff' then GitDiff.new
947 when 'get' then Get.new
948 when 'update-keyring' then KeyringUpdater.new
964 # vim:set shiftwidth=2: