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 USER_PATTERN = "[a-zA-Z0-9:-]+"
36 $program_name = File.basename($0, '.*')
38 $editor = ENV['EDITOR']
40 %w{/usr/bin/sensible-editor /usr/bin/editor /usr/bin/vi}.each do |editor|
41 if FileTest.executable?(editor)
51 @@keyid_fpr_mapping = {}
53 def GnuPG.readwrite3(intxt, infd, stdoutfd, stderrfd, statusfd=nil)
54 outtxt, stderrtxt, statustxt = ''
55 thread_in = Thread.new {
59 thread_out = Thread.new {
60 outtxt = stdoutfd.read
63 thread_err = Thread.new {
64 errtxt = stderrfd.read
67 thread_status = Thread.new {
68 statustxt = statusfd.read
75 thread_status.join if thread_status
77 return outtxt, stderrtxt, statustxt
80 def GnuPG.open3call(cmd, intxt, args, require_success = false, do_status=true)
84 statR, statW = IO.pipe if do_status
90 statR.close if do_status
96 exec(cmd, "--status-fd=#{statW.fileno}", *args)
100 rescue Exception => e
101 outW.puts("[PWSEXECERROR]: #{e}")
104 raise ("Calling gnupg failed")
111 (outtxt, stderrtxt, statustxt) = readwrite3(intxt, inW, outR, errR, statR);
113 (outtxt, stderrtxt) = readwrite3(intxt, inW, outR, errR);
115 wpid, status = Process.waitpid2 pid
116 throw "Unexpected pid: #{pid} vs #{wpid}" unless pid == wpid
117 throw "Process has not exited!?" unless status.exited?
118 throw "#{cmd} call did not exit sucessfully" if (require_success and status.exitstatus != 0)
119 if m=/^\[PWSEXECERROR\]: (.*)/.match(outtxt) then
120 STDERR.puts "Could not run GnuPG: #{m[1]}"
124 return outtxt, stderrtxt, statustxt, status.exitstatus
126 return outtxt, stderrtxt, status.exitstatus
130 def GnuPG.gpgcall(intxt, args, require_success = false)
131 return open3call(GNUPG, intxt, args, require_success)
134 def GnuPG.init_keys()
136 (outtxt, stderrtxt, statustxt) = GnuPG.gpgcall('', %w{--fast-list-mode --with-colons --with-fingerprint --list-secret-keys}, true)
139 outtxt.split("\n").each do |line|
140 parts = line.split(':')
141 if (parts[0] == "ssb" or parts[0] == "sec")
142 @@my_keys.push parts[4]
143 elsif (parts[0] == "fpr")
144 @@my_fprs.push parts[9]
148 # This is for my private keys, so we can tell if a file is encrypted to us
149 def GnuPG.get_my_keys()
153 # And this is for my private keys also, so we can tell if we are encrypting to ourselves
154 def GnuPG.get_my_fprs()
159 # This maps public keyids to fingerprints, so we can figure
160 # out if a file that is encrypted to a bunch of keys is
161 # encrypted to the fingerprints it should be encrypted to
162 def GnuPG.get_fpr_from_keyid(keyid)
163 fpr = @@keyid_fpr_mapping[keyid]
164 # this can be null, if we tried to find the fpr but failed to find the key in our keyring
166 STDERR.puts "Warning: No key found for keyid #{keyid}"
170 def GnuPG.get_fprs_from_keyids(keyids)
171 learn_fingerprints_from_keyids(keyids)
172 return keyids.collect{ |k| get_fpr_from_keyid(k) or "unknown" }
175 # this is to load the keys we will soon be asking about into
176 # our keyid-fpr-mapping hash
177 def GnuPG.learn_fingerprints_from_keyids(keyids)
178 need_to_learn = keyids.reject{ |k| @@keyid_fpr_mapping.has_key?(k) }
179 if need_to_learn.size > 0
180 # we can't use --fast-list-mode here because GnuPG is broken
181 # and does not show elmo's fingerprint in a call like
182 # gpg --with-colons --fast-list-mode --with-fingerprint --list-key D7C3F131AB2A91F5
183 args = %w{--with-colons --with-fingerprint --list-keys}
184 args.push "--keyring=./.keyring" if FileTest.exists?(".keyring")
185 args.concat need_to_learn
186 (outtxt, stderrtxt, statustxt) = GnuPG.gpgcall('', args, true)
190 outtxt.split("\n").each do |line|
191 parts = line.split(':')
192 if (parts[0] == "pub")
194 elsif (parts[0] == "fpr")
196 @@keyid_fpr_mapping[pub] = fpr
197 elsif (parts[0] == "sub")
198 @@keyid_fpr_mapping[parts[4]] = fpr
202 need_to_learn.reject{ |k| @@keyid_fpr_mapping.has_key?(k) }.each { |k| @@keyid_fpr_mapping[k] = nil }
206 def read_input(query, default_yes=true)
214 print "#{query} #{append} "
216 i = STDIN.readline.chomp.downcase
231 def initialize(dirname=".", trusted_users=nil)
234 @trusted_users = trusted_users
236 @trusted_users = ENV['HOME']+'/.pws-trusted-users'
244 f = File.open(@trusted_users)
245 rescue Exception => e
251 f.readlines.each do |line|
260 args.push "--keyring=./.keyring" if FileTest.exists?(".keyring")
261 (outtxt, stderrtxt, statustxt, exitstatus) = GnuPG.gpgcall(content, args)
264 statustxt.split("\n").each do |line|
265 if m = /^\[GNUPG:\] GOODSIG/.match(line)
267 elsif m = /^\[GNUPG:\] VALIDSIG \S+ \S+ \S+ \S+ \S+ \S+ \S+ \S+ \S+ ([0-9A-F]+)/.match(line)
273 STDERR.puts ".users file is not signed properly. GnuPG said on stdout:"
275 STDERR.puts "and on stderr:"
276 STDERR.puts stderrtxt
277 STDERR.puts "and via statusfd:"
278 STDERR.puts statustxt
282 if not trusted.include?(validsig)
283 raise ".users file is signed by #{validsig} which is not in #{@trusted_users}"
287 STDERR.puts "gpg verify failed for .users file"
296 f = File.open('.users')
297 rescue Exception => e
305 users = verify(users)
311 users.split("\n").each do |line|
315 if (m = /^(#{USER_PATTERN})\s*=\s*([0-9A-Fa-f]{40})\s*$/.match line)
318 if @users.has_key?(user)
319 STDERR.puts "User #{user} redefined at line #{lno}!"
323 elsif (m = /^(#{GROUP_PATTERN})\s*=\s*(.*)$/.match line)
326 if @groups.has_key?(group)
327 STDERR.puts "Group #{group} redefined at line #{lno}!"
330 members = members.split(/[\t ,]+/)
331 @groups[group] = { "members" => members }
337 return (name =~ /^@/)
339 def check_exists(x, whence, fatal=true)
342 ok=false unless (@groups.has_key?(x))
344 ok=false unless @users.has_key?(x)
347 STDERR.puts( (fatal ? "Error: " : "Warning: ") + "#{whence} contains unknown member #{x}")
353 @groups.each_pair do |groupname, group|
354 group['members'].each do |member|
355 check_exists(member, "Group #{groupname}")
357 group['members_to_do'] = group['members'].clone
363 @groups.each_pair do |groupname, group|
364 group['keys'] = [] unless group['keys']
366 still_contains_groups = false
367 group['members_to_do'].clone.each do |member|
369 if @groups[member]['members_to_do'].size == 0
370 group['keys'].concat @groups[member]['keys']
371 group['members_to_do'].delete(member)
374 still_contains_groups = true
377 group['keys'].push @users[member]
378 group['members_to_do'].delete(member)
382 all_expanded = false if still_contains_groups
384 break if all_expanded
386 cyclic_groups = @groups.keys.reject{|name| @groups[name]['members_to_do'].size == 0}.join(", ")
387 STDERR.puts "Cyclic group memberships in #{cyclic_groups}?"
393 def expand_targets(targets)
397 unless check_exists(t, "access line", false)
402 fprs.concat @groups[t]['keys']
416 attr_reader :accessible, :encrypted, :readable, :readers
418 def EncryptedData.determine_readable(readers)
419 GnuPG.get_my_keys.each do |keyid|
420 return true if readers.include?(keyid)
425 def EncryptedData.list_readers(statustxt)
427 statustxt.split("\n").each do |line|
428 m = /^\[GNUPG:\] ENC_TO ([0-9A-F]+)/.match line
435 def EncryptedData.targets(text)
436 text.split("\n").each do |line|
437 if /^(#|---)/.match line
440 m = /^access: "?((?:(?:#{GROUP_PATTERN}|#{USER_PATTERN}),?\s*)+)"?/.match line
442 return m[1].strip.split(/[\t ,]+/)
447 def initialize(encrypted_content, label)
448 @ignore_decrypt_errors = false
451 @encrypted_content = encrypted_content
452 (outtxt, stderrtxt, statustxt) = GnuPG.gpgcall(@encrypted_content, %w{--with-colons --no-options --no-default-keyring --secret-keyring=/dev/null --keyring=/dev/null})
453 @encrypted = !(statustxt =~ /\[GNUPG:\] NODATA/)
455 @readers = EncryptedData.list_readers(statustxt)
456 @readable = EncryptedData.determine_readable(@readers)
461 (outtxt, stderrtxt, statustxt, exitstatus) = GnuPG.gpgcall(@encrypted_content, %w{--decrypt})
462 if !@ignore_decrypt_errors and exitstatus != 0
463 proceed = read_input("Warning: gpg returned non-zero exit status #{exitstatus} when decrypting #{@label}. Proceed?", false)
464 exit(0) unless proceed
465 elsif !@ignore_decrypt_errors and outtxt.length == 0
466 proceed = read_input("Warning: #{@label} decrypted to an empty file. Proceed?")
467 exit(0) unless proceed
473 def encrypt(content, recipients)
474 args = recipients.collect{ |r| "--recipient=#{r}"}
475 args.push "--trust-model=always"
476 args.push "--keyring=./.keyring" if FileTest.exists?(".keyring")
478 args.push "--encrypt"
479 (outtxt, stderrtxt, statustxt, exitstatus) = GnuPG.gpgcall(content, args)
482 statustxt.split("\n").each do |line|
483 m = /^\[GNUPG:\] INV_RECP \S+ ([0-9A-F]+)/.match line
488 again = read_input("Warning: the following recipients are invalid: #{invalid.join(", ")}. Try again (or proceed)?")
489 return false if again
491 if outtxt.length == 0
492 tryagain = read_input("Error: #{@label} encrypted to an empty file. Edit again (or exit)?")
493 return false if tryagain
497 proceed = read_input("Warning: gpg returned non-zero exit status #{exitstatus} when encrypting #{@label}. Said:\n#{stderrtxt}\n#{statustxt}\n\nProceed (or try again)?")
498 return false unless proceed
505 def determine_encryption_targets(content)
506 targets = EncryptedData.targets(content)
508 tryagain = read_input("Warning: Did not find targets to encrypt to in header. Try again (or exit)?", true)
509 return false if tryagain
513 ok, expanded = @groupconfig.expand_targets(targets)
514 if (expanded.size == 0)
515 tryagain = read_input("Errors in access header. Edit again (or exit)?", true)
516 return false if tryagain
519 tryagain = read_input("Warnings in access header. Edit again (or continue)?", true)
520 return false if tryagain
524 GnuPG.get_my_fprs.each do |fpr|
525 if expanded.include?(fpr)
531 tryagain = read_input("File is not being encrypted to you. Edit again (or continue)?", true)
532 return false if tryagain
535 return true, expanded
540 class EncryptedFile < EncryptedData
541 def initialize(filename, new=false, trusted_file=nil)
542 @groupconfig = GroupConfig.new(dirname=File.dirname(filename), trusted_users=trusted_file)
549 unless FileTest.readable?(filename)
557 encrypted_content = File.read(filename)
558 super(encrypted_content, filename)
561 def write_back(content, targets)
562 ok, encrypted = encrypt(content, targets)
563 return false unless ok
565 File.open(@filename,"w").write(encrypted)
571 def help(parser, code=0, io=STDOUT)
572 io.puts "Usage: #{$program_name} ls [<directory> ...]"
573 io.puts parser.summarize
574 io.puts "Lists the contents of the given directory/directories, or the current"
575 io.puts "directory if none is given. For each file show whether it is PGP-encrypted"
576 io.puts "file, and if yes whether we can read it."
582 dir = Dir.open(dirname)
583 rescue Exception => e
588 Dir.chdir(dirname) do
589 unless FileTest.exists?(".users")
590 STDERR.puts "The .users file does not exists here. This is not a password store, is it?"
593 dir.sort.each do |filename|
594 next if (filename =~ /^\./) and not (@all >= 3)
595 stat = File::Stat.new(filename)
597 puts "(sym) #{filename}" if (@all >= 2)
598 elsif stat.directory?
599 puts "(dir) #{filename}" if (@all >= 2)
601 puts "(other) #{filename}" if (@all >= 2)
603 f = EncryptedFile.new(filename)
605 puts "(!perm) #{filename}"
607 puts "(file) #{filename}" if (@all >= 2)
609 puts "(ok) #{filename}"
611 puts "(locked) #{filename}" if (@all >= 1)
620 ARGV.options do |opts|
621 opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
622 opts.on_tail("-a", "--all" , "Show all files (use up to 3 times to show even more than all)") { @all = @all+1 }
627 dirs.push('.') unless dirs.size > 0
628 dirs.each { |dir| ls_dir(dir) }
633 def help(parser, code=0, io=STDOUT)
634 io.puts "Usage: #{$program_name} ed <filename>"
635 io.puts parser.summarize
636 io.puts "Decrypts the file, spawns an editor, and encrypts it again"
641 encrypted_file = EncryptedFile.new(filename, @new)
642 if !@new and !encrypted_file.readable && !@force
643 STDERR.puts "#{filename} is probably not readable"
647 encrypted_to = GnuPG.get_fprs_from_keyids(encrypted_file.readers).sort
649 content = encrypted_file.decrypt
650 original_content = content
652 oldsize = content.length
653 tempfile = Tempfile.open('pws')
654 tempfile.puts content
656 system($editor, tempfile.path)
658 throw "Process has not exited!?" unless status.exited?
659 unless status.exitstatus == 0
660 proceed = read_input("Warning: Editor did not exit successfully (exit code #{status.exitstatus}. Proceed?")
661 exit(0) unless proceed
664 # some editors do not write new content in place, but instead
665 # make a new file and more it in the old file's place.
667 reopened = File.open(tempfile.path, "r+")
668 rescue Exception => e
672 content = reopened.read
674 # zero the file, well, both of them.
675 newsize = content.length
676 clearsize = (newsize > oldsize) ? newsize : oldsize
678 [tempfile, reopened].each do |f|
679 f.seek(0, IO::SEEK_SET)
680 f.print "\0"*clearsize
686 if content.length == 0
687 proceed = read_input("Warning: Content is now empty. Proceed?")
688 exit(0) unless proceed
691 ok, targets = encrypted_file.determine_encryption_targets(content)
694 if (original_content == content)
695 if (targets.sort == encrypted_to)
696 proceed = read_input("Nothing changed. Re-encrypt anyway?", false)
697 exit(0) unless proceed
699 STDERR.puts("Info: Content not changed but re-encrypting anyway because the list of keys changed")
703 success = encrypted_file.write_back(content, targets)
709 ARGV.options do |opts|
710 opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
711 opts.on_tail("-n", "--new" , "Edit new file") { |new| @new=new }
712 opts.on_tail("-f", "--force" , "Spawn an editor even if the file is probably not readable") { |force| @force=force }
715 help(ARGV.options, 1, STDERR) if ARGV.length != 1
716 filename = ARGV.shift
719 if FileTest.exists?(filename)
720 STDERR.puts "#{filename} does exist"
724 if !FileTest.exists?(filename)
725 STDERR.puts "#{filename} does not exist"
727 elsif !FileTest.file?(filename)
728 STDERR.puts "#{filename} is not a regular file"
730 elsif !FileTest.readable?(filename)
731 STDERR.puts "#{filename} is not accessible (unix perms)"
736 dirname = File.dirname(filename)
737 basename = File.basename(filename)
745 def help(parser, code=0, io=STDOUT)
746 io.puts "Usage: #{$program_name} ed <filename>"
747 io.puts parser.summarize
748 io.puts "Reencrypts the file (useful for changed user lists or keys)"
752 $editor = '/bin/true'
758 def help(parser, code=0, io=STDOUT)
759 io.puts "Usage: #{$program_name} get <filename> <query>"
760 io.puts parser.summarize
761 io.puts "Decrypts the file, fetches a key and outputs it to stdout."
762 io.puts "The file must be in YAML format."
763 io.puts "query is a query, formatted like /host/users/root"
767 def get(filename, what)
768 encrypted_file = EncryptedFile.new(filename, @new)
769 if !encrypted_file.readable
770 STDERR.puts "#{filename} is probably not readable"
775 yaml = YAML::load(encrypted_file.decrypt)
776 rescue Psych::SyntaxError, ArgumentError => e
777 STDERR.puts "Could not parse YAML: #{e.message}"
783 a = what.split("/")[1..-1]
786 # q = /, so print top level keys
797 STDERR.puts("No such key or invalid lookup expression")
798 elsif hit.respond_to?(:keys)
809 ARGV.options do |opts|
810 opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
813 help(ARGV.options, 1, STDERR) if ARGV.length != 2
814 filename = ARGV.shift
817 if !FileTest.exists?(filename)
818 STDERR.puts "#{filename} does not exist"
820 elsif !FileTest.file?(filename)
821 STDERR.puts "#{filename} is not a regular file"
823 elsif !FileTest.readable?(filename)
824 STDERR.puts "#{filename} is not accessible (unix perms)"
828 dirname = File.dirname(filename)
829 basename = File.basename(filename)
837 def help(parser, code=0, io=STDOUT)
838 io.puts "Usage: #{$program_name} update-keyring [<keyserver>]"
839 io.puts parser.summarize
840 io.puts "Updates the local .keyring file"
845 ARGV.options do |opts|
846 opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
849 help(ARGV.options, 1, STDERR) if ARGV.length > 1
850 keyserver = ARGV.shift
851 keyserver = 'keys.gnupg.net' unless keyserver
853 groupconfig = GroupConfig.new
854 users = groupconfig.get_users()
855 args = %w{--with-colons --no-options --no-default-keyring --keyring=./.keyring}
857 system('touch', '.keyring')
858 users.each_pair() do |uid, keyid|
860 cmd << "--keyserver=#{keyserver}"
863 puts "Fetching key for #{uid}"
864 (outtxt, stderrtxt, statustxt) = GnuPG.gpgcall('', cmd)
865 unless (statustxt =~ /^\[GNUPG:\] IMPORT_OK /)
866 STDERR.puts "Warning: did not find IMPORT_OK token in status output"
867 STDERR.puts "gpg exited with exit code #{ecode})"
868 STDERR.puts "Command was gpg #{cmd.join(' ')}"
869 STDERR.puts "stdout was #{outtxt}"
870 STDERR.puts "stderr was #{stderrtxt}"
871 STDERR.puts "statustxt was #{statustxt}"
875 cmd << '--batch' << '--edit' << keyid << 'minimize' << 'save'
876 (outtxt, stderrtxt, statustxt, ecode) = GnuPG.gpgcall('', cmd)
884 def help(parser, code=0, io=STDOUT)
885 io.puts "Usage: #{$program_name} gitdiff <commit> <file>"
886 io.puts parser.summarize
887 io.puts "Shows a diff between the version of <file> in your directory and the"
888 io.puts "version in git at <commit> (or HEAD). Requires that your tree be git"
889 io.puts "managed, obviously."
893 def check_readable(e, label)
894 if !e.readable && !@force
895 STDERR.puts "#{label} is probably not readable."
900 def get_file_at_commit()
901 label = @commit+':'+@filename
902 (encrypted_content, stderrtxt, exitcode) = GnuPG.open3call('git', '', ['show', label], require_success=true, do_status=false)
903 data = EncryptedData.new(encrypted_content, label)
904 check_readable(data, label)
908 def get_file_current()
909 data = EncryptedFile.new(@filename)
910 check_readable(data, @filename)
915 old = get_file_at_commit()
916 cur = get_file_current()
918 t1 = Tempfile.open('pws')
922 t2 = Tempfile.open('pws')
926 system("diff", "-u", t1.path, t2.path)
928 t1.seek(0, IO::SEEK_SET)
929 t1.print "\0"*old.length
933 t2.seek(0, IO::SEEK_SET)
934 t2.print "\0"*cur.length
940 ARGV.options do |opts|
941 opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
942 opts.on_tail("-f", "--force" , "Do it even if the file is probably not readable") { |force| @force=force }
948 @filename = ARGV.shift
949 elsif ARGV.length == 2
951 @filename = ARGV.shift
953 help(ARGV.options, 1, STDERR)
961 def help(code=0, io=STDOUT)
962 io.puts "Usage: #{$program_name} ed"
963 io.puts "Usage: #{$program_name} rc"
964 io.puts " #{$program_name} ls"
965 io.puts " #{$program_name} gitdiff"
966 io.puts " #{$program_name} update-keyring"
967 io.puts " #{$program_name} help"
968 io.puts "Call #{$program_name} <command> --help for additional options/parameters"
975 when 'ls' then Ls.new
976 when 'ed' then Ed.new
977 when 'rc' then Reencrypt.new
978 when 'gitdiff' then GitDiff.new
979 when 'get' then Get.new
980 when 'update-keyring' then KeyringUpdater.new
998 # vim:set shiftwidth=2: