3 # password store management tool
5 # Copyright (c) 2008, 2009, 2011, 2013 Peter Palfrader <peter@palfrader.org>
6 # Copyright (c) 2014 Fastly
8 # Permission is hereby granted, free of charge, to any person obtaining
9 # a copy of this software and associated documentation files (the
10 # "Software"), to deal in the Software without restriction, including
11 # without limitation the rights to use, copy, modify, merge, publish,
12 # distribute, sublicense, and/or sell copies of the Software, and to
13 # permit persons to whom the Software is furnished to do so, subject to
14 # the following conditions:
16 # The above copyright notice and this permission notice shall be
17 # included in all copies or substantial portions of the Software.
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 Thread.abort_on_exception = true
34 GNUPG = "/usr/bin/gpg"
35 GROUP_PATTERN = "@[a-zA-Z0-9-]+"
36 USER_PATTERN = "[a-zA-Z0-9:-]+"
37 $program_name = File.basename($0, '.*')
39 $editor = ENV['EDITOR']
41 %w{/usr/bin/sensible-editor /usr/bin/editor /usr/bin/vi}.each do |editor|
42 if FileTest.executable?(editor)
52 @@keyid_fpr_mapping = {}
55 def GnuPG.extra_args=(val)
59 def GnuPG.readwrite3(intxt, infd, stdoutfd, stderrfd, statusfd=nil)
60 outtxt, stderrtxt, statustxt = ''
61 thread_in = Thread.new {
65 thread_out = Thread.new {
66 outtxt = stdoutfd.read
69 thread_err = Thread.new {
70 errtxt = stderrfd.read
73 thread_status = Thread.new {
74 statustxt = statusfd.read
81 thread_status.join if thread_status
83 return outtxt, stderrtxt, statustxt
86 def GnuPG.open3call(cmd, intxt, args, require_success = false, do_status=true)
90 statR, statW = IO.pipe if do_status
96 statR.close if do_status
102 exec(cmd, "--status-fd=#{statW.fileno}", *@@extra_args, *args)
106 rescue Exception => e
107 outW.puts("[PWSEXECERROR]: #{e}")
110 raise ("Calling gnupg failed")
117 (outtxt, stderrtxt, statustxt) = readwrite3(intxt, inW, outR, errR, statR);
119 (outtxt, stderrtxt) = readwrite3(intxt, inW, outR, errR);
121 wpid, status = Process.waitpid2 pid
122 throw "Unexpected pid: #{pid} vs #{wpid}" unless pid == wpid
123 throw "Process has not exited!?" unless status.exited?
124 throw "#{cmd} call did not exit sucessfully" if (require_success and status.exitstatus != 0)
125 if m=/^\[PWSEXECERROR\]: (.*)/.match(outtxt) then
126 STDERR.puts "Could not run GnuPG: #{m[1]}"
130 return outtxt, stderrtxt, statustxt, status.exitstatus
132 return outtxt, stderrtxt, status.exitstatus
136 def GnuPG.gpgcall(intxt, args, require_success = false)
137 return open3call(GNUPG, intxt, args, require_success)
140 def GnuPG.init_keys()
142 (outtxt, stderrtxt, statustxt) = GnuPG.gpgcall('', %w{--fast-list-mode --with-colons --with-fingerprint --list-secret-keys}, true)
145 outtxt.split("\n").each do |line|
146 parts = line.split(':')
147 if (parts[0] == "ssb" or parts[0] == "sec")
148 @@my_keys.push parts[4]
149 elsif (parts[0] == "fpr")
150 @@my_fprs.push parts[9]
154 # This is for my private keys, so we can tell if a file is encrypted to us
155 def GnuPG.get_my_keys()
159 # And this is for my private keys also, so we can tell if we are encrypting to ourselves
160 def GnuPG.get_my_fprs()
165 # This maps public keyids to fingerprints, so we can figure
166 # out if a file that is encrypted to a bunch of keys is
167 # encrypted to the fingerprints it should be encrypted to
168 def GnuPG.get_fpr_from_keyid(keyid)
169 fpr = @@keyid_fpr_mapping[keyid]
170 # this can be null, if we tried to find the fpr but failed to find the key in our keyring
172 STDERR.puts "Warning: No key found for keyid #{keyid}"
176 def GnuPG.get_fprs_from_keyids(keyids)
177 learn_fingerprints_from_keyids(keyids)
178 return keyids.collect{ |k| get_fpr_from_keyid(k) or "unknown" }
181 # this is to load the keys we will soon be asking about into
182 # our keyid-fpr-mapping hash
183 def GnuPG.learn_fingerprints_from_keyids(keyids)
184 need_to_learn = keyids.reject{ |k| @@keyid_fpr_mapping.has_key?(k) }
185 if need_to_learn.size > 0
186 # we can't use --fast-list-mode here because GnuPG is broken
187 # and does not show elmo's fingerprint in a call like
188 # gpg --with-colons --fast-list-mode --with-fingerprint --list-key D7C3F131AB2A91F5
189 args = %w{--with-colons --with-fingerprint --list-keys}
190 args.push "--keyring=./.keyring" if FileTest.exists?(".keyring")
191 args.concat need_to_learn
192 (outtxt, stderrtxt, statustxt) = GnuPG.gpgcall('', args, true)
196 outtxt.split("\n").each do |line|
197 parts = line.split(':')
198 if (parts[0] == "pub")
200 elsif (parts[0] == "fpr")
202 @@keyid_fpr_mapping[pub] = fpr
203 elsif (parts[0] == "sub")
204 @@keyid_fpr_mapping[parts[4]] = fpr
208 need_to_learn.reject{ |k| @@keyid_fpr_mapping.has_key?(k) }.each { |k| @@keyid_fpr_mapping[k] = nil }
212 def read_input(query, default_yes=true)
220 print "#{query} #{append} "
222 i = STDIN.readline.chomp.downcase
237 def initialize(dirname=".", trusted_users=nil)
240 @trusted_users = trusted_users
242 @trusted_users = ENV['HOME']+'/.pws-trusted-users'
250 f = File.open(@trusted_users)
251 rescue Exception => e
256 f.readlines.each do |line|
265 args.push "--keyring=./.keyring" if FileTest.exists?(".keyring")
266 (outtxt, stderrtxt, statustxt, exitstatus) = GnuPG.gpgcall(content, args)
269 statustxt.split("\n").each do |line|
270 if m = /^\[GNUPG:\] GOODSIG/.match(line)
272 elsif m = /^\[GNUPG:\] VALIDSIG \S+ \S+ \S+ \S+ \S+ \S+ \S+ \S+ \S+ ([0-9A-F]+)/.match(line)
278 STDERR.puts ".users file is not signed properly. GnuPG said on stdout:"
280 STDERR.puts "and on stderr:"
281 STDERR.puts stderrtxt
282 STDERR.puts "and via statusfd:"
283 STDERR.puts statustxt
287 if not trusted.include?(validsig)
288 raise ".users file is signed by #{validsig} which is not in #{@trusted_users}"
292 raise "gpg verify failed for .users file"
300 f = File.open(File.join(@dirname, '.users'))
301 rescue Exception => e
308 users = verify(users)
314 users.split("\n").each do |line|
318 if (m = /^(#{USER_PATTERN})\s*=\s*([0-9A-Fa-f]{40})\s*$/.match line)
321 if @users.has_key?(user)
322 STDERR.puts "User #{user} redefined at line #{lno}!"
326 elsif (m = /^(#{GROUP_PATTERN})\s*=\s*(.*)$/.match line)
329 if @groups.has_key?(group)
330 STDERR.puts "Group #{group} redefined at line #{lno}!"
333 members = members.split(/[\t ,]+/)
334 @groups[group] = { "members" => members }
340 return (name =~ /^@/)
342 def check_exists(x, whence, fatal=true)
345 ok=false unless (@groups.has_key?(x))
347 ok=false unless @users.has_key?(x)
350 STDERR.puts( (fatal ? "Error: " : "Warning: ") + "#{whence} contains unknown member #{x}")
356 @groups.each_pair do |groupname, group|
357 group['members'].each do |member|
358 check_exists(member, "Group #{groupname}")
360 group['members_to_do'] = group['members'].clone
366 @groups.each_pair do |groupname, group|
367 group['keys'] = [] unless group['keys']
369 still_contains_groups = false
370 group['members_to_do'].clone.each do |member|
372 if @groups[member]['members_to_do'].size == 0
373 group['keys'].concat @groups[member]['keys']
374 group['members_to_do'].delete(member)
377 still_contains_groups = true
380 group['keys'].push @users[member]
381 group['members_to_do'].delete(member)
385 all_expanded = false if still_contains_groups
387 break if all_expanded
389 cyclic_groups = @groups.keys.reject{|name| @groups[name]['members_to_do'].size == 0}.join(", ")
390 STDERR.puts "Cyclic group memberships in #{cyclic_groups}?"
396 def expand_targets(targets)
400 unless check_exists(t, "access line", false)
405 fprs.concat @groups[t]['keys']
419 attr_reader :accessible, :encrypted, :readable, :readers
421 def EncryptedData.determine_readable(readers)
422 GnuPG.get_my_keys.each do |keyid|
423 return true if readers.include?(keyid)
428 def EncryptedData.list_readers(statustxt)
430 statustxt.split("\n").each do |line|
431 m = /^\[GNUPG:\] ENC_TO ([0-9A-F]+)/.match line
438 def EncryptedData.targets(text)
439 text.split("\n").each do |line|
440 if /^(#|---)/.match line
443 m = /^access: "?((?:(?:#{GROUP_PATTERN}|#{USER_PATTERN}),?\s*)+)"?/.match line
445 return m[1].strip.split(/[\t ,]+/)
450 def initialize(encrypted_content, label)
451 @ignore_decrypt_errors = false
454 @encrypted_content = encrypted_content
455 (outtxt, stderrtxt, statustxt) = GnuPG.gpgcall(@encrypted_content, %w{--with-colons --no-options --no-default-keyring --secret-keyring=/dev/null --keyring=/dev/null})
456 @encrypted = !(statustxt =~ /\[GNUPG:\] NODATA/)
458 @readers = EncryptedData.list_readers(statustxt)
459 @readable = EncryptedData.determine_readable(@readers)
464 (outtxt, stderrtxt, statustxt, exitstatus) = GnuPG.gpgcall(@encrypted_content, %w{--decrypt})
465 if !@ignore_decrypt_errors and exitstatus != 0
466 proceed = read_input("Warning: gpg returned non-zero exit status #{exitstatus} when decrypting #{@label}. Proceed?", false)
467 exit(0) unless proceed
468 elsif !@ignore_decrypt_errors and outtxt.length == 0
469 proceed = read_input("Warning: #{@label} decrypted to an empty file. Proceed?")
470 exit(0) unless proceed
476 def encrypt(content, recipients)
477 args = recipients.collect{ |r| "--recipient=#{r}"}
478 args.push "--trust-model=always"
479 args.push "--keyring=./.keyring" if FileTest.exists?(".keyring")
481 args.push "--encrypt"
482 (outtxt, stderrtxt, statustxt, exitstatus) = GnuPG.gpgcall(content, args)
485 statustxt.split("\n").each do |line|
486 m = /^\[GNUPG:\] INV_RECP \S+ ([0-9A-F]+)/.match line
491 again = read_input("Warning: the following recipients are invalid: #{invalid.join(", ")}. Try again (or proceed)?")
492 return false if again
494 if outtxt.length == 0
495 tryagain = read_input("Error: #{@label} encrypted to an empty file. Edit again (or exit)?")
496 return false if tryagain
500 proceed = read_input("Warning: gpg returned non-zero exit status #{exitstatus} when encrypting #{@label}. Said:\n#{stderrtxt}\n#{statustxt}\n\nProceed (or try again)?")
501 return false unless proceed
508 def determine_encryption_targets(content)
509 targets = EncryptedData.targets(content)
511 tryagain = read_input("Warning: Did not find targets to encrypt to in header. Try again (or exit)?", true)
512 return false if tryagain
516 ok, expanded = @groupconfig.expand_targets(targets)
517 if (expanded.size == 0)
518 tryagain = read_input("Errors in access header. Edit again (or exit)?", true)
519 return false if tryagain
522 tryagain = read_input("Warnings in access header. Edit again (or continue)?", true)
523 return false if tryagain
527 GnuPG.get_my_fprs.each do |fpr|
528 if expanded.include?(fpr)
534 tryagain = read_input("File is not being encrypted to you. Edit again (or continue)?", true)
535 return false if tryagain
538 return true, expanded
543 class EncryptedFile < EncryptedData
544 def initialize(filename, new=false, trusted_file=nil)
545 @groupconfig = GroupConfig.new(dirname=File.dirname(filename), trusted_users=trusted_file)
552 unless FileTest.readable?(filename)
560 encrypted_content = File.read(filename)
561 super(encrypted_content, filename)
564 def write_back(content, targets)
565 ok, encrypted = encrypt(content, targets)
566 return false unless ok
568 File.open(@filename,"w").write(encrypted)
574 def help(parser, code=0, io=STDOUT)
575 io.puts "Usage: #{$program_name} ls [<directory> ...]"
576 io.puts parser.summarize
577 io.puts "Lists the contents of the given directory/directories, or the current"
578 io.puts "directory if none is given. For each file show whether it is PGP-encrypted"
579 io.puts "file, and if yes whether we can read it."
585 dir = Dir.open(dirname)
586 rescue Exception => e
591 Dir.chdir(dirname) do
592 unless FileTest.exists?(".users")
593 STDERR.puts "The .users file does not exists here. This is not a password store, is it?"
596 dir.sort.each do |filename|
597 next if (filename =~ /^\./) and not (@all >= 3)
598 stat = File::Stat.new(filename)
600 puts "(sym) #{filename}" if (@all >= 2)
601 elsif stat.directory?
602 puts "(dir) #{filename}" if (@all >= 2)
604 puts "(other) #{filename}" if (@all >= 2)
606 f = EncryptedFile.new(filename)
608 puts "(!perm) #{filename}"
610 puts "(file) #{filename}" if (@all >= 2)
612 puts "(ok) #{filename}"
614 puts "(locked) #{filename}" if (@all >= 1)
623 ARGV.options do |opts|
624 opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
625 opts.on_tail("-a", "--all" , "Show all files (use up to 3 times to show even more than all)") { @all = @all+1 }
630 dirs.push('.') unless dirs.size > 0
631 dirs.each { |dir| ls_dir(dir) }
636 def help(parser, code=0, io=STDOUT)
637 io.puts "Usage: #{$program_name} ed <filename>"
638 io.puts parser.summarize
639 io.puts "Decrypts the file, spawns an editor, and encrypts it again"
644 encrypted_file = EncryptedFile.new(filename, @new)
645 if !@new and !encrypted_file.readable && !@force
646 STDERR.puts "#{filename} is probably not readable"
650 encrypted_to = GnuPG.get_fprs_from_keyids(encrypted_file.readers).sort
652 content = encrypted_file.decrypt
653 original_content = content
655 oldsize = content.length
656 tempfile = Tempfile.open('pws')
657 tempfile.puts content
659 system($editor, tempfile.path)
661 throw "Process has not exited!?" unless status.exited?
662 unless status.exitstatus == 0
663 proceed = read_input("Warning: Editor did not exit successfully (exit code #{status.exitstatus}. Proceed?")
664 exit(0) unless proceed
667 # some editors do not write new content in place, but instead
668 # make a new file and more it in the old file's place.
670 reopened = File.open(tempfile.path, "r+")
671 rescue Exception => e
675 content = reopened.read
677 # zero the file, well, both of them.
678 newsize = content.length
679 clearsize = (newsize > oldsize) ? newsize : oldsize
681 [tempfile, reopened].each do |f|
682 f.seek(0, IO::SEEK_SET)
683 f.print "\0"*clearsize
689 if content.length == 0
690 proceed = read_input("Warning: Content is now empty. Proceed?")
691 exit(0) unless proceed
694 ok, targets = encrypted_file.determine_encryption_targets(content)
697 if (original_content == content)
698 if (targets.sort == encrypted_to)
699 proceed = read_input("Nothing changed. Re-encrypt anyway?", false)
700 exit(0) unless proceed
702 STDERR.puts("Info: Content not changed but re-encrypting anyway because the list of keys changed")
706 success = encrypted_file.write_back(content, targets)
712 ARGV.options do |opts|
713 opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
714 opts.on_tail("-n", "--new" , "Edit new file") { |new| @new=new }
715 opts.on_tail("-f", "--force" , "Spawn an editor even if the file is probably not readable") { |force| @force=force }
718 help(ARGV.options, 1, STDERR) if ARGV.length != 1
719 filename = ARGV.shift
722 if FileTest.exists?(filename)
723 STDERR.puts "#{filename} does exist"
727 if !FileTest.exists?(filename)
728 STDERR.puts "#{filename} does not exist"
730 elsif !FileTest.file?(filename)
731 STDERR.puts "#{filename} is not a regular file"
733 elsif !FileTest.readable?(filename)
734 STDERR.puts "#{filename} is not accessible (unix perms)"
739 dirname = File.dirname(filename)
740 basename = File.basename(filename)
748 def help(parser, code=0, io=STDOUT)
749 io.puts "Usage: #{$program_name} ed <filename>"
750 io.puts parser.summarize
751 io.puts "Reencrypts the file (useful for changed user lists or keys)"
755 $editor = '/bin/true'
761 def help(parser, code=0, io=STDOUT)
762 io.puts "Usage: #{$program_name} get <filename> <query>"
763 io.puts parser.summarize
764 io.puts "Decrypts the file, fetches a key and outputs it to stdout."
765 io.puts "The file must be in YAML format."
766 io.puts "query is a query, formatted like /host/users/root"
770 def get(filename, what)
771 encrypted_file = EncryptedFile.new(filename, @new)
772 if !encrypted_file.readable
773 STDERR.puts "#{filename} is probably not readable"
778 yaml = YAML::load(encrypted_file.decrypt)
779 rescue Psych::SyntaxError, ArgumentError => e
780 STDERR.puts "Could not parse YAML: #{e.message}"
786 a = what.split("/")[1..-1]
789 # q = /, so print top level keys
800 STDERR.puts("No such key or invalid lookup expression")
801 elsif hit.respond_to?(:keys)
812 ARGV.options do |opts|
813 opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
816 help(ARGV.options, 1, STDERR) if ARGV.length != 2
817 filename = ARGV.shift
820 if !FileTest.exists?(filename)
821 STDERR.puts "#{filename} does not exist"
823 elsif !FileTest.file?(filename)
824 STDERR.puts "#{filename} is not a regular file"
826 elsif !FileTest.readable?(filename)
827 STDERR.puts "#{filename} is not accessible (unix perms)"
831 dirname = File.dirname(filename)
832 basename = File.basename(filename)
840 def help(parser, code=0, io=STDOUT)
841 io.puts "Usage: #{$program_name} update-keyring [<keyserver>]"
842 io.puts parser.summarize
843 io.puts "Updates the local .keyring file"
848 ARGV.options do |opts|
849 opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
852 help(ARGV.options, 1, STDERR) if ARGV.length > 1
853 keyserver = ARGV.shift
854 keyserver = 'keys.gnupg.net' unless keyserver
856 groupconfig = GroupConfig.new
857 users = groupconfig.get_users()
858 args = %w{--with-colons --no-options --no-default-keyring --keyring=./.keyring}
860 system('touch', '.keyring')
861 users.each_pair() do |uid, keyid|
863 cmd << "--keyserver=#{keyserver}"
866 puts "Fetching key for #{uid}"
867 (outtxt, stderrtxt, statustxt) = GnuPG.gpgcall('', cmd)
868 unless (statustxt =~ /^\[GNUPG:\] IMPORT_OK /)
869 STDERR.puts "Warning: did not find IMPORT_OK token in status output"
870 STDERR.puts "gpg exited with exit code #{ecode})"
871 STDERR.puts "Command was gpg #{cmd.join(' ')}"
872 STDERR.puts "stdout was #{outtxt}"
873 STDERR.puts "stderr was #{stderrtxt}"
874 STDERR.puts "statustxt was #{statustxt}"
878 cmd << '--batch' << '--edit' << keyid << 'minimize' << 'save'
879 (outtxt, stderrtxt, statustxt, ecode) = GnuPG.gpgcall('', cmd)
887 def help(parser, code=0, io=STDOUT)
888 io.puts "Usage: #{$program_name} gitdiff <commit> <file>"
889 io.puts parser.summarize
890 io.puts "Shows a diff between the version of <file> in your directory and the"
891 io.puts "version in git at <commit> (or HEAD). Requires that your tree be git"
892 io.puts "managed, obviously."
896 def check_readable(e, label)
897 if !e.readable && !@force
898 STDERR.puts "#{label} is probably not readable."
903 def get_file_at_commit()
904 label = @commit+':'+@filename
905 (encrypted_content, stderrtxt, exitcode) = GnuPG.open3call('git', '', ['show', label], require_success=true, do_status=false)
906 data = EncryptedData.new(encrypted_content, label)
907 check_readable(data, label)
911 def get_file_current()
912 data = EncryptedFile.new(@filename)
913 check_readable(data, @filename)
918 old = get_file_at_commit()
919 cur = get_file_current()
921 t1 = Tempfile.open('pws')
925 t2 = Tempfile.open('pws')
929 system("diff", "-u", t1.path, t2.path)
931 t1.seek(0, IO::SEEK_SET)
932 t1.print "\0"*old.length
936 t2.seek(0, IO::SEEK_SET)
937 t2.print "\0"*cur.length
943 ARGV.options do |opts|
944 opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
945 opts.on_tail("-f", "--force" , "Do it even if the file is probably not readable") { |force| @force=force }
951 @filename = ARGV.shift
952 elsif ARGV.length == 2
954 @filename = ARGV.shift
956 help(ARGV.options, 1, STDERR)
964 def help(code=0, io=STDOUT)
965 io.puts "Usage: #{$program_name} ed"
966 io.puts "Usage: #{$program_name} rc"
967 io.puts " #{$program_name} ls"
968 io.puts " #{$program_name} gitdiff"
969 io.puts " #{$program_name} update-keyring"
970 io.puts " #{$program_name} help"
971 io.puts "Call #{$program_name} <command> --help for additional options/parameters"
978 when 'ls' then Ls.new
979 when 'ed' then Ed.new
980 when 'rc' then Reencrypt.new
981 when 'gitdiff' then GitDiff.new
982 when 'get' then Get.new
983 when 'update-keyring' then KeyringUpdater.new
1001 # vim:set shiftwidth=2: