]> err.no Git - pwstore/blob - pws
Syntax updates for ruby 1.9 compatibility
[pwstore] / pws
1 #!/usr/bin/ruby
2
3 # password store management tool
4
5 # Copyright (c) 2008, 2009 Peter Palfrader <peter@palfrader.org>
6 #
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:
14 #
15 # The above copyright notice and this permission notice shall be
16 # included in all copies or substantial portions of the Software.
17 #
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.
25
26 require 'optparse'
27 require 'thread'
28 require 'tempfile'
29
30 require 'yaml'
31 Thread.abort_on_exception = true
32
33 GNUPG = "/usr/bin/gpg"
34
35 $program_name = File.basename($0, '.*')
36
37 $editor = ENV['EDITOR']
38 if $editor == nil
39   %w{/usr/bin/sensible-editor /usr/bin/editor /usr/bin/vi}.each do |editor|
40     if FileTest.executable?(editor)
41       $editor = editor
42       break
43     end
44   end
45 end
46
47 class GnuPG
48   @@my_keys = nil
49   @@my_fprs = nil
50   @@keyid_fpr_mapping = {}
51
52   def GnuPG.readwrite3(intxt, infd, stdoutfd, stderrfd, statusfd)
53     outtxt, stderrtxt, statustxt = ''
54     thread_in = Thread.new {
55       infd.print intxt
56       infd.close
57     }
58     thread_out = Thread.new {
59       outtxt = stdoutfd.read
60       stdoutfd.close
61     }
62     thread_err = Thread.new {
63       errtxt = stderrfd.read
64       stderrfd.close
65     }
66     thread_status = Thread.new {
67       statustxt = statusfd.read
68       statusfd.close
69     } if (statusfd)
70
71     thread_in.join
72     thread_out.join
73     thread_err.join
74     thread_status.join if thread_status
75
76     return outtxt, stderrtxt, statustxt
77   end
78
79   def GnuPG.gpgcall(intxt, args, require_success = false)
80     inR, inW = IO.pipe
81     outR, outW = IO.pipe
82     errR, errW = IO.pipe
83     statR, statW = IO.pipe
84
85     pid = Kernel.fork do
86       inW.close
87       outR.close
88       errR.close
89       statR.close
90       STDIN.reopen(inR)
91       STDOUT.reopen(outW)
92       STDERR.reopen(errW)
93       begin
94         exec(GNUPG, "--status-fd=#{statW.fileno}",  *args)
95       rescue Exception => e
96         outW.puts("[PWSEXECERROR]: #{e}")
97         exit(1)
98       end
99       raise ("Calling gnupg failed")
100     end
101     inR.close
102     outW.close
103     errW.close
104     statW.close
105     (outtxt, stderrtxt, statustxt) = readwrite3(intxt, inW, outR, errR, statR);
106     wpid, status = Process.waitpid2 pid
107     throw "Unexpected pid: #{pid} vs #{wpid}" unless pid == wpid
108     throw "Process has not exited!?" unless status.exited?
109     throw "gpg call did not exit sucessfully" if (require_success and status.exitstatus != 0)
110     if m=/^\[PWSEXECERROR\]: (.*)/.match(outtxt) then
111       STDERR.puts "Could not run GnuPG: #{m[1]}"
112       exit(1)
113     end
114     return outtxt, stderrtxt, statustxt, status.exitstatus
115   end
116
117   def GnuPG.init_keys()
118     return if @@my_keys
119     (outtxt, stderrtxt, statustxt) = GnuPG.gpgcall('', %w{--fast-list-mode --with-colons --with-fingerprint --list-secret-keys}, true)
120     @@my_keys = []
121     @@my_fprs = []
122     outtxt.split("\n").each do |line|
123       parts = line.split(':')
124       if (parts[0] == "ssb" or parts[0] == "sec")
125         @@my_keys.push parts[4]
126       elsif (parts[0] == "fpr")
127         @@my_fprs.push parts[9]
128       end
129     end
130   end
131   # This is for my private keys, so we can tell if a file is encrypted to us
132   def GnuPG.get_my_keys()
133     init_keys
134     @@my_keys
135   end
136   # And this is for my private keys also, so we can tell if we are encrypting to ourselves
137   def GnuPG.get_my_fprs()
138     init_keys
139     @@my_fprs
140   end
141
142   # This maps public keyids to fingerprints, so we can figure
143   # out if a file that is encrypted to a bunch of keys is
144   # encrypted to the fingerprints it should be encrypted to
145   def GnuPG.get_fpr_from_keyid(keyid)
146     fpr = @@keyid_fpr_mapping[keyid]
147     # this can be null, if we tried to find the fpr but failed to find the key in our keyring
148     unless fpr
149       STDERR.puts "Warning: No key found for keyid #{keyid}"
150     end
151     return fpr
152   end
153   def GnuPG.get_fprs_from_keyids(keyids)
154     learn_fingerprints_from_keyids(keyids)
155     return keyids.collect{ |k| get_fpr_from_keyid(k) or "unknown" }
156   end
157
158   # this is to load the keys we will soon be asking about into
159   # our keyid-fpr-mapping hash
160   def GnuPG.learn_fingerprints_from_keyids(keyids)
161     need_to_learn = keyids.reject{ |k| @@keyid_fpr_mapping.has_key?(k) }
162     if need_to_learn.size > 0
163       # we can't use --fast-list-mode here because GnuPG is broken
164       # and does not show elmo's fingerprint in a call like
165       # gpg --with-colons --fast-list-mode --with-fingerprint --list-key D7C3F131AB2A91F5
166       args = %w{--with-colons --with-fingerprint --list-keys}
167       args.concat need_to_learn
168       (outtxt, stderrtxt, statustxt) = GnuPG.gpgcall('', args, true)
169
170       pub = nil
171       fpr = nil
172       outtxt.split("\n").each do |line|
173         parts = line.split(':')
174         if (parts[0] == "pub")
175           pub = parts[4]
176         elsif (parts[0] == "fpr")
177           fpr = parts[9]
178           @@keyid_fpr_mapping[pub] = fpr
179         elsif (parts[0] == "sub")
180           @@keyid_fpr_mapping[parts[4]] = fpr
181         end
182       end
183     end
184     need_to_learn.reject{ |k| @@keyid_fpr_mapping.has_key?(k) }.each { |k| @@keyid_fpr_mapping[k] = nil }
185   end
186 end
187
188 def read_input(query, default_yes=true)
189   if default_yes
190     append = '[Y/n]'
191   else
192     append = '[y/N]'
193   end
194
195   while true
196     print "#{query} #{append} "
197     begin
198       i = STDIN.readline.chomp.downcase
199     rescue EOFError
200       return default_yes
201     end
202     if i==""
203       return default_yes
204     elsif i=="y"
205       return true
206     elsif i=="n"
207       return false
208     end
209   end
210 end
211
212 class GroupConfig
213   def initialize
214     parse_file
215     expand_groups
216   end
217
218   def verify(content)
219     begin
220       f = File.open(ENV['HOME']+'/.pws-trusted-users')
221     rescue Exception => e
222       STDERR.puts e
223       exit(1)
224     end
225
226     trusted = []
227     f.readlines.each do |line|
228       line.chomp!
229       next if line =~ /^$/
230       next if line =~ /^#/
231
232       trusted.push line
233     end
234
235     (outtxt, stderrtxt, statustxt, exitstatus) = GnuPG.gpgcall(content, %w{})
236     goodsig = false
237     validsig = nil
238     statustxt.split("\n").each do |line|
239       if m = /^\[GNUPG:\] GOODSIG/.match(line)
240         goodsig = true
241       elsif m = /^\[GNUPG:\] VALIDSIG \S+ \S+ \S+ \S+ \S+ \S+ \S+ \S+ \S+ ([0-9A-F]+)/.match(line)
242         validsig = m[1]
243       end
244     end
245
246     if not goodsig
247       STDERR.puts ".users file is not signed properly.  GnuPG said on stdout:"
248       STDERR.puts outtxt
249       STDERR.puts "and on stderr:"
250       STDERR.puts stderrtxt
251       STDERR.puts "and via statusfd:"
252       STDERR.puts statustxt
253       exit(1)
254     end
255
256     if not trusted.include?(validsig)
257       STDERR.puts ".users file is signed by #{validsig} which is not in ~/.pws-trusted-users"
258       exit(1)
259     end
260
261     if not exitstatus==0
262       STDERR.puts "gpg verify failed for .users file"
263       exit(1)
264     end
265
266     return outtxt
267   end
268
269   def parse_file
270     begin
271       f = File.open('.users')
272     rescue Exception => e
273       STDERR.puts e
274       exit(1)
275     end
276
277     users = f.read
278     f.close
279
280     users = verify(users)
281
282     @users = {}
283     @groups = {}
284
285     lno = 0
286     users.split("\n").each do |line|
287       lno = lno+1
288       next if line =~ /^$/
289       next if line =~ /^#/
290       if (m = /^([a-zA-Z0-9-]+)\s*=\s*([0-9A-Fa-f]{40})\s*$/.match line)
291         user = m[1]
292         fpr = m[2]
293         if @users.has_key?(user)
294           STDERR.puts "User #{user} redefined at line #{lno}!"
295           exit(1)
296         end
297         @users[user] = fpr
298       elsif (m = /^(@[a-zA-Z0-9-]+)\s*=\s*(.*)$/.match line)
299         group = m[1]
300         members = m[2].strip
301         if @groups.has_key?(group)
302           STDERR.puts "Group #{group} redefined at line #{lno}!"
303           exit(1)
304         end
305         members = members.split(/[\t ,]+/)
306         @groups[group] = { "members" => members }
307       end
308     end
309   end
310
311   def is_group(name)
312     return (name =~ /^@/)
313   end
314   def check_exists(x, whence, fatal=true)
315     ok=true
316     if is_group(x)
317       ok=false unless (@groups.has_key?(x))
318     else
319       ok=false unless @users.has_key?(x)
320     end
321     unless ok
322       STDERR.puts( (fatal ? "Error: " : "Warning: ") + "#{whence} contains unknown member #{x}")
323       exit(1) if fatal
324     end
325     return ok
326   end
327   def expand_groups
328     @groups.each_pair do |groupname, group|
329       group['members'].each do |member|
330         check_exists(member, "Group #{groupname}")
331       end
332       group['members_to_do'] = group['members'].clone
333     end
334
335     while true
336       had_progress = false
337       all_expanded = true
338       @groups.each_pair do |groupname, group|
339         group['keys'] = [] unless group['keys'] 
340
341         still_contains_groups = false
342         group['members_to_do'].clone.each do |member|
343           if is_group(member)
344             if @groups[member]['members_to_do'].size == 0
345               group['keys'].concat @groups[member]['keys']
346               group['members_to_do'].delete(member)
347               had_progress = true
348             else
349               still_contains_groups = true
350             end
351           else
352             group['keys'].push @users[member]
353             group['members_to_do'].delete(member)
354             had_progress = true
355           end
356         end
357         all_expanded = false if still_contains_groups
358       end
359       break if all_expanded
360       unless had_progress
361         cyclic_groups = @groups.keys.reject{|name| @groups[name]['members_to_do'].size == 0}.join(", ")
362         STDERR.puts "Cyclic group memberships in #{cyclic_groups}?"
363         exit(1)
364       end
365     end
366   end
367
368   def expand_targets(targets)
369     fprs = []
370     ok = true
371     targets.each do |t|
372       unless check_exists(t, "access line", false)
373         ok = false
374         next
375       end
376       if is_group(t)
377         fprs.concat @groups[t]['keys']
378       else
379         fprs.push @users[t]
380       end
381     end
382     return ok, fprs.uniq
383   end
384
385   def get_users()
386     return @users
387   end
388 end
389
390 class EncryptedFile
391   attr_reader :accessible, :encrypted, :readable, :readers
392
393   def EncryptedFile.determine_readable(readers)
394     GnuPG.get_my_keys.each do |keyid|
395       return true if readers.include?(keyid)
396     end
397     return false
398   end
399
400   def EncryptedFile.list_readers(statustxt)
401     readers = []
402     statustxt.split("\n").each do |line|
403       m = /^\[GNUPG:\] ENC_TO ([0-9A-F]+)/.match line
404       next unless m
405       readers.push m[1]
406     end
407     return readers
408   end
409
410   def EncryptedFile.targets(text)
411     metaline = text.split("\n").first
412     m = /^access: (.*)/.match metaline
413     return [] unless m
414     return m[1].strip.split(/[\t ,]+/)
415   end
416
417
418   def initialize(filename, new=false)
419     @groupconfig = GroupConfig.new
420     @new = new
421     if @new
422       @readers = []
423     end
424
425     @filename = filename
426     unless FileTest.readable?(filename)
427       @accessible = false
428       return
429     end
430     @accessible = true
431     @encrypted_content = File.read(filename)
432     (outtxt, stderrtxt, statustxt) = GnuPG.gpgcall(@encrypted_content, %w{--with-colons --no-options --no-default-keyring --secret-keyring=/dev/null --keyring=/dev/null})
433     @encrypted = !(statustxt =~ /\[GNUPG:\] NODATA/)
434     if @encrypted
435       @readers = EncryptedFile.list_readers(statustxt)
436       @readable = EncryptedFile.determine_readable(@readers)
437     end
438   end
439
440   def decrypt
441     (outtxt, stderrtxt, statustxt, exitstatus) = GnuPG.gpgcall(@encrypted_content, %w{--decrypt})
442     if !@new and exitstatus != 0
443       proceed = read_input("Warning: gpg returned non-zero exit status #{exitstatus} when decrypting #{@filename}.  Proceed?", false)
444       exit(0) unless proceed
445     elsif !@new and outtxt.length == 0
446       proceed = read_input("Warning: #{@filename} decrypted to an empty file.  Proceed?")
447       exit(0) unless proceed
448     end
449
450     return outtxt
451   end
452
453   def encrypt(content, recipients)
454     args = recipients.collect{ |r| "--recipient=#{r}"}
455     args.push "--trust-model=always"
456     args.push "--keyring=./.keyring" if FileTest.exists?(".keyring")
457     args.push "--armor"
458     args.push "--encrypt"
459     (outtxt, stderrtxt, statustxt, exitstatus) = GnuPG.gpgcall(content, args)
460
461     invalid = []
462     statustxt.split("\n").each do |line|
463       m = /^\[GNUPG:\] INV_RECP \S+ ([0-9A-F]+)/.match line
464       next unless m
465       invalid.push m[1]
466     end
467     if invalid.size > 0
468       again = read_input("Warning: the following recipients are invalid: #{invalid.join(", ")}. Try again (or proceed)?")
469       return false if again
470     end
471     if outtxt.length == 0
472       tryagain = read_input("Error: #{@filename} encrypted to an empty file.  Edit again (or exit)?")
473       return false if tryagain
474       exit(0)
475     end
476     if exitstatus != 0
477       proceed = read_input("Warning: gpg returned non-zero exit status #{exitstatus} when encrypting #{@filename}. Said:\n#{stderrtxt}\n#{statustxt}\n\nProceed (or try again)?")
478       return false unless proceed
479     end
480
481     return true, outtxt
482   end
483
484
485   def determine_encryption_targets(content)
486     targets = EncryptedFile.targets(content)
487     if targets.size == 0
488       tryagain = read_input("Warning: Did not find targets to encrypt to in header.  Try again (or exit)?", true)
489       return false if tryagain
490       exit(0)
491     end
492
493     ok, expanded = @groupconfig.expand_targets(targets)
494     if (expanded.size == 0)
495       tryagain = read_input("Errors in access header.  Edit again (or exit)?", true)
496       return false if tryagain
497       exit(0)
498     elsif (not ok)
499       tryagain = read_input("Warnings in access header.  Edit again (or continue)?", true)
500       return false if tryagain
501     end
502
503     to_me = false
504     GnuPG.get_my_fprs.each do |fpr|
505       if expanded.include?(fpr)
506         to_me = true
507         break
508       end
509     end
510     unless to_me
511       tryagain = read_input("File is not being encrypted to you.  Edit again (or continue)?", true)
512       return false if tryagain
513     end
514
515     return true, expanded
516   end
517
518   def write_back(content, targets)
519     ok, encrypted = encrypt(content, targets)
520     return false unless ok
521
522     File.open(@filename,"w").write(encrypted)
523     return true
524   end
525 end
526
527 class Ls
528   def help(parser, code=0, io=STDOUT)
529     io.puts "Usage: #{$program_name} ls [<directory> ...]"
530     io.puts parser.summarize
531     io.puts "Lists the contents of the given directory/directories, or the current"
532     io.puts "directory if none is given.  For each file show whether it is PGP-encrypted"
533     io.puts "file, and if yes whether we can read it."
534     exit(code)
535   end
536
537   def ls_dir(dirname)
538     begin
539       dir = Dir.open(dirname)
540     rescue Exception => e
541       STDERR.puts e
542       return
543     end
544     puts "#{dirname}:"
545     Dir.chdir(dirname) do
546       unless FileTest.exists?(".users")
547         STDERR.puts "The .users file does not exists here.  This is not a password store, is it?"
548         exit(1)
549       end
550       dir.sort.each do |filename|
551         next if (filename =~ /^\./) and not (@all >= 3)
552         stat = File::Stat.new(filename)
553         if stat.symlink?
554           puts "(sym)      #{filename}" if (@all >= 2)
555         elsif stat.directory?
556           puts "(dir)      #{filename}" if (@all >= 2)
557         elsif !stat.file?
558           puts "(other)    #{filename}" if (@all >= 2)
559         else
560           f = EncryptedFile.new(filename)
561           if !f.accessible
562             puts "(!perm)    #{filename}"
563           elsif !f.encrypted
564             puts "(file)     #{filename}" if (@all >= 2)
565           elsif f.readable
566             puts "(ok)       #{filename}"
567           else
568             puts "(locked)   #{filename}" if (@all >= 1)
569           end
570         end
571       end
572     end
573   end
574
575   def initialize()
576     @all = 0
577     ARGV.options do |opts|
578       opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
579       opts.on_tail("-a", "--all" , "Show all files (use up to 3 times to show even more than all)") { @all = @all+1 }
580       opts.parse!
581     end
582
583     dirs = ARGV
584     dirs.push('.') unless dirs.size > 0
585     dirs.each { |dir| ls_dir(dir) }
586   end
587 end
588
589 class Ed
590   def help(parser, code=0, io=STDOUT)
591     io.puts "Usage: #{$program_name} ed <filename>"
592     io.puts parser.summarize
593     io.puts "Decrypts the file, spawns an editor, and encrypts it again"
594     exit(code)
595   end
596
597   def edit(filename)
598     encrypted_file = EncryptedFile.new(filename, @new)
599     if !@new and !encrypted_file.readable && !@force
600       STDERR.puts "#{filename} is probably not readable"
601       exit(1)
602     end
603
604     encrypted_to = GnuPG.get_fprs_from_keyids(encrypted_file.readers).sort
605
606     content = encrypted_file.decrypt
607     original_content = content
608     while true
609       oldsize = content.length
610       tempfile = Tempfile.open('pws')
611       tempfile.puts content
612       tempfile.flush
613       system($editor, tempfile.path)
614       status = $?
615       throw "Process has not exited!?" unless status.exited?
616       unless status.exitstatus == 0
617         proceed = read_input("Warning: Editor did not exit successfully (exit code #{status.exitstatus}.  Proceed?")
618         exit(0) unless proceed
619       end
620
621       # some editors do not write new content in place, but instead
622       # make a new file and more it in the old file's place.
623       begin
624         reopened = File.open(tempfile.path, "r+")
625       rescue Exception => e
626         STDERR.puts e
627         exit(1)
628       end
629       content = reopened.read
630
631       # zero the file, well, both of them.
632       newsize = content.length
633       clearsize = (newsize > oldsize) ? newsize : oldsize
634
635       [tempfile, reopened].each do |f|
636         f.seek(0, IO::SEEK_SET)
637         f.print "\0"*clearsize
638         f.fsync
639       end
640       reopened.close
641       tempfile.close(true)
642
643       if content.length == 0
644         proceed = read_input("Warning: Content is now empty.  Proceed?")
645         exit(0) unless proceed
646       end
647
648       ok, targets = encrypted_file.determine_encryption_targets(content)
649       next unless ok
650
651       if (original_content == content)
652         if (targets.sort == encrypted_to)
653           proceed = read_input("Nothing changed.  Re-encrypt anyway?", false)
654           exit(0) unless proceed
655         else
656           STDERR.puts("Info: Content not changed but re-encrypting anyway because the list of keys changed")
657         end
658       end
659
660       success = encrypted_file.write_back(content, targets)
661       break if success
662     end
663   end
664
665   def initialize()
666     ARGV.options do |opts|
667       opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
668       opts.on_tail("-n", "--new" , "Edit new file") { |new| @new=new }
669       opts.on_tail("-f", "--force" , "Spawn an editor even if the file is probably not readable") { |force| @force=force }
670       opts.parse!
671     end
672     help(ARGV.options, 1, STDERR) if ARGV.length != 1
673     filename = ARGV.shift
674
675     if @new
676       if FileTest.exists?(filename)
677         STDERR.puts "#{filename} does exist"
678         exit(1)
679       end
680     else
681       if !FileTest.exists?(filename)
682         STDERR.puts "#{filename} does not exist"
683         exit(1)
684       elsif !FileTest.file?(filename)
685         STDERR.puts "#{filename} is not a regular file"
686         exit(1)
687       elsif !FileTest.readable?(filename)
688         STDERR.puts "#{filename} is not accessible (unix perms)"
689         exit(1)
690       end
691     end
692
693     dirname = File.dirname(filename)
694     basename = File.basename(filename)
695     Dir.chdir(dirname) {
696       edit(basename)
697     }
698   end
699 end
700
701 class KeyringUpdater
702   def help(parser, code=0, io=STDOUT)
703     io.puts "Usage: #{$program_name} update-keyring [<keyserver>]"
704     io.puts parser.summarize
705     io.puts "Updates the local .keyring file"
706     exit(code)
707   end
708
709   def initialize()
710     ARGV.options do |opts|
711       opts.on_tail("-h", "--help" , "Display this help screen") { help(opts) }
712       opts.parse!
713     end
714     help(ARGV.options, 1, STDERR) if ARGV.length > 1
715     keyserver = ARGV.shift
716     keyserver = 'keys.gnupg.net' unless keyserver
717
718     groupconfig = GroupConfig.new
719     users = groupconfig.get_users()
720     args = %w{--with-colons --no-options --no-default-keyring --keyring=./.keyring}
721
722     system('touch', '.keyring')
723     users.each_pair() do |uid, keyid|
724       cmd = args.clone()
725       cmd << "--keyserver=#{keyserver}"
726       cmd << "--recv-keys"
727       cmd << keyid
728       puts "Fetching key for #{uid}"
729       (outtxt, stderrtxt, statustxt) = GnuPG.gpgcall('', cmd)
730       unless (statustxt =~ /^\[GNUPG:\] IMPORT_OK /)
731         STDERR.puts "Warning: did not find IMPORT_OK token in status output"
732         STDERR.puts "gpg exited with exit code #{ecode})"
733         STDERR.puts "Command was gpg #{cmd.join(' ')}"
734         STDERR.puts "stdout was #{outtxt}"
735         STDERR.puts "stderr was #{stderrtxt}"
736         STDERR.puts "statustxt was #{statustxt}"
737       end
738
739       cmd = args.clone()
740       cmd << '--batch' << '--edit' << keyid << 'minimize' << 'save'
741       (outtxt, stderrtxt, statustxt, ecode) = GnuPG.gpgcall('', cmd)
742     end
743
744
745   end
746 end
747
748 def help(code=0, io=STDOUT)
749   io.puts "Usage: #{$program_name} ed"
750   io.puts "       #{$program_name} ls"
751   io.puts "       #{$program_name} update-keyring"
752   io.puts "       #{$program_name} help"
753   io.puts "Call #{$program_name} <command> --help for additional options/parameters"
754   exit(code)
755 end
756
757
758 def parse_command
759   case ARGV.shift
760     when 'ls' then Ls.new
761     when 'ed' then Ed.new
762     when 'update-keyring' then KeyringUpdater.new
763     when 'help' then
764       case ARGV.length
765         when 0 then help
766         when 1 then
767           ARGV.push "--help"
768           parse_command
769         else help(1, STDERR)
770       end
771     else
772       help(1, STDERR)
773   end
774 end
775
776 parse_command
777
778 # vim:set shiftwidth=2:
779 # vim:set et:
780 # vim:set ts=2: