pwl 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem 'encryptor'
7
+ gem 'commander'
8
+ gem 'activesupport'
9
+
10
+ group :test do
11
+ gem "rake"
12
+ gem 'simplecov', :require => false
13
+ gem 'nokogiri-diff'
14
+ end
15
+
16
+ # Add dependencies to develop your gem here.
17
+ # Include everything needed to run rake, tests, features, etc.
18
+ group :development do
19
+ gem "rdoc", "~> 3.12"
20
+ gem "bundler", "~> 1.0.0"
21
+ gem "jeweler", "~> 1.8.3"
22
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.2.2)
5
+ i18n (~> 0.6)
6
+ multi_json (~> 1.0)
7
+ commander (4.1.2)
8
+ highline (~> 1.6.11)
9
+ encryptor (1.1.3)
10
+ git (1.2.5)
11
+ highline (1.6.11)
12
+ i18n (0.6.0)
13
+ jeweler (1.8.3)
14
+ bundler (~> 1.0)
15
+ git (>= 1.2.5)
16
+ rake
17
+ rdoc
18
+ json (1.6.5)
19
+ multi_json (1.1.0)
20
+ nokogiri (1.4.7)
21
+ nokogiri-diff (0.1.0)
22
+ nokogiri (~> 1.4.1)
23
+ tdiff (~> 0.3.2)
24
+ rake (0.9.2.2)
25
+ rdoc (3.12)
26
+ json (~> 1.4)
27
+ simplecov (0.6.1)
28
+ multi_json (~> 1.0)
29
+ simplecov-html (~> 0.5.3)
30
+ simplecov-html (0.5.3)
31
+ tdiff (0.3.2)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ activesupport
38
+ bundler (~> 1.0.0)
39
+ commander
40
+ encryptor
41
+ jeweler (~> 1.8.3)
42
+ nokogiri-diff
43
+ rake
44
+ rdoc (~> 3.12)
45
+ simplecov
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Nicholas E. Rabenau
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,102 @@
1
+ pwl - Password Locker
2
+ =====================
3
+
4
+ pwl is a secure password locker for the command-line.
5
+
6
+ [![Build Status](https://secure.travis-ci.org/nerab/pwl.png?branch=master)](http://travis-ci.org/nerab/pwl)
7
+
8
+ Installation
9
+ ============
10
+ pwl is written in [Ruby](http://www.ruby-lang.org/) and can be installed using Ruby's [gem](http://rubygems.org/) package manager:
11
+
12
+ gem install pwl
13
+
14
+ Basic Usage
15
+ ===========
16
+ Before it can store passwords, pwl needs to initialize the password database. The database file is created with the `init` command and located by default at `~/.pwl.pstore`.
17
+
18
+ pwl init
19
+
20
+ Storing a password requires a name under which the password can be retrieved later on:
21
+
22
+ pwl put "Mail Account" s3cret
23
+
24
+ This command will store the password "s3cret" under the name "Mail Account". Later on this password can be retrieved using the get command:
25
+
26
+ pwl get "Mail Account"
27
+
28
+ This command will print "s3cret" to the console (STDOUT).
29
+
30
+ For more usage information, invoke the help command:
31
+
32
+ pwl help
33
+
34
+ Concept
35
+ =======
36
+ pwl is written in the UNIX tradition of having a tool do one thing, and do it well. With this in mind, password management becomes not much more than keeping a list of name-value pairs and securing it from unauthorized access (by encrypting the password database with a master password).
37
+
38
+ The whole topic becomes much more interesting when you start integrating a password locker into various tools and applications. There is no widely accepted standard for how password lockers could hook into applications, and so almost every tool falls back to the system clipboard. Notable exceptions are modern browsers, which provide password locker integration by their generic plugin concepts.
39
+
40
+ pwl focuses on the command line where standard input (STDIN) and output (STDOUT) are the established way to share data between commands (applications). Therefore pwl was built with the following principles in mind:
41
+
42
+ * All input is read from STDIN
43
+ * All regular output (e.g. the password retrieved) goes to STDOUT
44
+ * All messages and errors are printed to STDERR (prevents side effects of the message)
45
+ * Exit code is set to 0 for success and non-zero for errors
46
+
47
+ Security
48
+ ========
49
+ When it comes to securing passwords, pwl does not make any compromises. It relies on the proven OpenSSL library for all encryption functions via the [Encryptor](https://github.com/shuber/encryptor) wrapper. The password store itself is a Ruby [PStore](http://ruby-doc.org/stdlib/libdoc/pstore/rdoc/PStore.html). All names and values are individually encrypted with the master password.
50
+
51
+ Code security is enforced with two major concepts:
52
+
53
+ 1. The complete source code of pwl, and all libraries it uses, are open source. Therefore, everyone can freely inspect the complete source code of pwl, run penetration tests, etc.
54
+ 1. Nearly 100% of pwl's code is covered by unit and acceptance tests. All tests are re-run whenever new code is pushed to the public git repository. All build and test results are published with the help of [Travis CI](http://travis-ci.org/nerab/pwl).
55
+
56
+ Integration
57
+ ===========
58
+ When invoked on a console, pwl will ask for the master password to be typed into the console (using the [HighLine](http://highline.rubyforge.org) library). pwl also behaves well when run in a pipe. You can pipe the master password into pwl. Similarly, instead of printing the retrieved password to the console, pwl's output can be used as input for yet another program.For instance, the popular request for copying a password to the clipboard can be achieved by piping pwl's output into a clipboard application that reads from STDIN, e.g. pbcopy (Mac), xclip (Linux), clip (Windows >= Vista), putclip (cygwin).
59
+
60
+ Example on MacOS:
61
+
62
+ pwl get nerab@example.com | pbcopy
63
+
64
+ Or, if you prefer to enter the master password via a regular dialog box, you can run the same command with the --gui flag:
65
+
66
+ pwl get nerab@example.com --gui | pbcopy
67
+
68
+ By calling this line, the password stored under nerab@example.com is copied to the clipboard.
69
+
70
+ Backup & Restore
71
+ ================
72
+ * Backup: Copy ~/.pwl.pstore (or whatever you pass in with --file) to a safe place.
73
+ * Restore: Replace ~/.pwl.pstore (or whatever you pass in with --file) with the version you kept in a safe place.
74
+
75
+ Export and Printing
76
+ ===================
77
+ pwl provides a simple export into the HTML format with the following command:
78
+
79
+ pwl export
80
+
81
+ This will print raw HTML markup on STDOUT, so it can be written into a file
82
+
83
+ pwl export > my_passwords.html
84
+
85
+ and then viewed and printed with a browser. With [bcat](http://rtomayko.github.com/bcat/) the export can be directly piped into a browser:
86
+
87
+ pwl export | bcat
88
+
89
+ Contributing to pwl
90
+ ===================
91
+
92
+ * Check out the [latest master](http://github.com/nerab/pwl/) to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
93
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
94
+ * Fork the project.
95
+ * Start a feature/bugfix branch.
96
+ * Commit and push until you are happy with your contribution.
97
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
98
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
99
+
100
+ Copyright
101
+ =========
102
+ Copyright (c) 2012 Nicholas E. Rabenau. See [LICENSE.txt](https://raw.github.com/nerab/pwl/master/LICENSE.txt) for further details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "pwl"
18
+ gem.homepage = "http://github.com/nerab/pwl"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Command-line password locker}
21
+ gem.description = %Q{pwl is a secure password locker for the commandline}
22
+ gem.email = "nerab@gmx.net"
23
+ gem.authors = ["Nicholas E. Rabenau"]
24
+ gem.executables << 'pwl'
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.test_files = FileList['test/**/test_*.rb'].exclude("test/acceptance/test_dialogs.rb")
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "pwl #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/bin/pwl ADDED
@@ -0,0 +1,268 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'commander/import'
5
+
6
+ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
7
+ require 'pwl'
8
+
9
+ program :version, Pwl::VERSION
10
+ program :description, "#{program(:name)} is a secure password manager for the command line."
11
+
12
+ EXIT_CODES = {
13
+ :success => Pwl::Message.new('Success.'),
14
+ :aborted => Pwl::Message.new('Aborted by user.', 1),
15
+ :passwords_dont_match => Pwl::ErrorMessage.new('Passwords do not match.', 2),
16
+ :no_value_found => Pwl::Message.new('No value found for <%= name %>.', 3, :name => 'NAME'),
17
+ :file_exists => Pwl::ErrorMessage.new('There already exists a store at <%= file %>. Use --force to overwrite it or --file to specify a different store.', 4, :file => 'FILE'),
18
+ :file_not_found => Pwl::ErrorMessage.new('Store file <%= file %> could not be found.', 5, :file => 'FILE'),
19
+ :name_blank => Pwl::ErrorMessage.new('Name may not be blank.', 6),
20
+ :value_blank => Pwl::ErrorMessage.new('Value may not be blank.', 7),
21
+ :list_empty => Pwl::Message.new('List is empty.', 8),
22
+ :list_empty_filter => Pwl::Message.new('No names found that match filter <%= filter %>.', 9, :filter => 'FILTER'),
23
+ :validation_new_failed => Pwl::ErrorMessage.new('<%= message %>.', 10, :message => 'Validation of new master password failed'),
24
+ }
25
+
26
+ program :help, 'Exit Status', "#{program(:name)} sets the following exit status:\n\n" + EXIT_CODES.values.sort{|l,r| l.exit_code <=> r.exit_code}.collect{|m| " #{m.exit_code.to_s}: #{m.to_s}"}.join("\n")
27
+ program :help, 'Author', 'Nicholas E. Rabenau <nerab@gmx.at>'
28
+
29
+ DEFAULT_STORE_FILE = File.expand_path("~/.#{program(:name)}.pstore")
30
+ DEFAULT_EXPORT_TEMPLATE = File.join(File.dirname(__FILE__), *%w[.. templates export.html.erb])
31
+
32
+ store_file = DEFAULT_STORE_FILE
33
+
34
+ global_option '-V', '--verbose', 'Enable verbose output'
35
+ global_option('-f', '--file FILE', 'Determine the file that holds the store'){|file| store_file = file}
36
+ global_option '-g', '--gui', 'Request the master password using an OS-specific GUI dialog. This option takes precedence over STDIN.'
37
+
38
+ command :init do |c|
39
+ c.syntax = "#{program(:name)} #{c.name}"
40
+ c.summary = 'Initializes a new store'
41
+ c.description = 'This command initializes a new password store. Password quality is enforced using validation rules.'
42
+ c.example "Initializes a new password store in #{DEFAULT_STORE_FILE}", "#{program(:name)} #{c.name}"
43
+ c.example "Initializes a new password store in /tmp/crackme.txt", "#{program(:name)} #{c.name} --file /tmp/crackme.txt"
44
+ c.option '--force', 'Force-overwrite an existing store file'
45
+ c.action do |args, options|
46
+ # Store checks this too, but we want to fail fast.
47
+ exit_with(:file_exists, options.verbose, :file => store_file) if File.exists?(store_file) && !options.force
48
+
49
+ begin
50
+ begin
51
+ master_password = get_password('Enter new master password:', options.gui)
52
+ end while begin
53
+ validate!(master_password) # Basic idea from http://stackoverflow.com/questions/136793/is-there-a-do-while-loop-in-ruby
54
+ rescue Pwl::InvalidMasterPasswordError => e
55
+ STDERR.puts e.message
56
+ options.gui || STDIN.tty? # only continue the loop when in interactive mode
57
+ end
58
+
59
+ # Ask for password confirmation if running in interactive mode (terminal)
60
+ if STDIN.tty? && master_password != get_password('Enter master password again:', options.gui)
61
+ exit_with(:passwords_dont_match, options.verbose)
62
+ end
63
+ rescue Pwl::Dialog::Cancelled
64
+ exit_with(:aborted, options.verbose)
65
+ end
66
+
67
+ Pwl::Store.new(store_file, master_password, {:force => options.force})
68
+ STDERR.puts "Successfully initialized new store at #{store_file}" if options.verbose
69
+ end
70
+ end
71
+
72
+ command :get do |c|
73
+ c.syntax = "#{program(:name)} #{c.name} NAME"
74
+ c.summary = 'Retrieves the value for NAME and prints it to STDOUT.'
75
+ c.description = 'This command retrieves the value stored under NAME and prints it on STDOUT.'
76
+ c.example 'Reads the value stored under the name "foo" and prints it to STDOUT', "#{program(:name)} #{c.name} foo"
77
+ c.action do |args, options|
78
+ # Store checks this too, but we want to fail fast and provide a message.
79
+ exit_with(:file_not_found, options.verbose, :file => store_file) unless File.exists?(store_file)
80
+ exit_with(:name_blank, options.verbose) if 0 == args.size || args[0].blank?
81
+
82
+ begin
83
+ result = Pwl::Store.open(store_file, get_password('Enter master password:', options.gui)).get(args[0])
84
+ if result.blank?
85
+ exit_with(:no_value_found, options.verbose, :name => args[0])
86
+ else
87
+ puts result
88
+ end
89
+ rescue Pwl::Dialog::Cancelled
90
+ exit_with(:aborted, options.verbose)
91
+ end
92
+ end
93
+ end
94
+
95
+ command :list do |c|
96
+ c.syntax = "#{program(:name)} #{c.name} [FILTER]"
97
+ c.summary = 'Lists all names with optional FILTER.'
98
+ c.description = 'This command prints all names to STDOUT. If present, only those names matching FILTER will be returned.'
99
+ c.example 'Prints all names', "#{program(:name)} #{c.name}"
100
+ c.example 'Prints all names which start with "foo"', "#{program(:name)} #{c.name} foo"
101
+ c.example 'Prints all names separated by comma', "#{program(:name)} #{c.name} --separator ,"
102
+ c.option '-s', '--separator SEPARATOR', String, 'Separate names by SEPARATOR'
103
+ c.action do |args, options|
104
+ # Store checks this too, but we want to fail fast and provide a message.
105
+ exit_with(:file_not_found, options.verbose, :file => store_file) unless File.exists?(store_file)
106
+
107
+ options.default :separator => ' '
108
+
109
+ begin
110
+ result = Pwl::Store.open(store_file, get_password('Enter master password:', options.gui)).list(args[0]).join(options.separator)
111
+ if !result.blank?
112
+ puts result
113
+ else
114
+ if args[0] # filter given
115
+ exit_with(:list_empty_filter, options.verbose, args[0])
116
+ else
117
+ exit_with(:list_empty, options.verbose)
118
+ end
119
+ end
120
+ rescue Pwl::Dialog::Cancelled
121
+ exit_with(:aborted, options.verbose)
122
+ end
123
+ end
124
+ end
125
+
126
+ command :put do |c|
127
+ c.syntax = "#{program(:name)} #{c.name} NAME [VALUE]"
128
+ c.summary = 'Stores VALUE under NAME'
129
+ c.description = 'Adds or updates the entry stored under NAME. If NAME is already present in the store, it will be updated with VALUE. If NAME is not already present in the store, a new entry will be created. If VALUE is not given, it will be read from STDIN.'
130
+ c.example 'Stores the value "bar" under the name "foo"', "#{program(:name)} #{c.name} foo bar"
131
+ c.example 'Reads STDIN and stores that as value under the name "foo"', "#{program(:name)} #{c.name} foo"
132
+ c.action do |args, options|
133
+ exit_with(:file_not_found, options.verbose, :file => store_file) unless File.exists?(store_file)
134
+ exit_with(:name_blank, options.verbose) if 0 == args.size || args[0].blank?
135
+
136
+ # Ask for the master password _before_ it may be necessary to ask for the value in a terminal
137
+ begin
138
+ store = Pwl::Store.open(store_file, get_password('Enter master password:', options.gui))
139
+ rescue Pwl::Dialog::Cancelled
140
+ exit_with(:aborted, options.verbose)
141
+ end
142
+
143
+ value = args[1]
144
+
145
+ if !value
146
+ exit_with(:value_blank, options.verbose) unless STDIN.tty? || options.gui
147
+
148
+ begin
149
+ value = get_text("Enter value for name '#{args[0]}':", options.gui)
150
+ rescue Pwl::Dialog::Cancelled
151
+ exit_with(:aborted, options.verbose)
152
+ end
153
+
154
+ # still blank, even after asking for it?
155
+ if value.blank?
156
+ exit_with(:value_blank, true)
157
+ end
158
+ end
159
+
160
+ store.put(args[0], value)
161
+ STDERR.puts "Successfully stored new value under #{args[0]}." if options.verbose
162
+ end
163
+ end
164
+
165
+ command :delete do |c|
166
+ c.syntax = "#{program(:name)} #{c.name} NAME"
167
+ c.summary = 'Deletes the entry stored under NAME'
168
+ c.description = 'Deletes the complete entry that is stored under NAME. If NAME is not present in the store, an error will thrown.'
169
+ c.example 'Deletes what was stored under the name "foo"', "#{program(:name)} #{c.name} foo"
170
+ c.action do |args, options|
171
+ exit_with(:file_not_found, options.verbose, :file => store_file) unless File.exists?(store_file)
172
+ exit_with(:name_blank, options.verbose) if 0 == args.size || args[0].blank?
173
+
174
+ begin
175
+ store = Pwl::Store.open(store_file, get_password('Enter master password:', options.gui))
176
+ rescue Pwl::Dialog::Cancelled
177
+ exit_with(:aborted, options.verbose)
178
+ end
179
+
180
+ store.delete(args[0])
181
+ STDERR.puts "Successfully deleted the value under #{args[0]}." if options.verbose
182
+ end
183
+ end
184
+
185
+ command :passwd do |c|
186
+ c.syntax = "#{program(:name)} #{c.name} [NEW_MASTER_PASSWORD]"
187
+ c.summary = 'Changes the master password to NEW_MASTER_PASSWORD.'
188
+ c.description = 'This command changes the master password of the store. Password quality is enforced using validation rules.'
189
+ c.action do |args, options|
190
+ exit_with(:file_not_found, options.verbose, :file => store_file) unless File.exists?(store_file)
191
+
192
+ begin
193
+ store = Pwl::Store.open(store_file, get_password('Enter current master password:', options.gui))
194
+
195
+ if !STDIN.tty? && !options.gui
196
+ # If we are in a pipe and do not run in GUI mode, we accept the new master password as args[0]
197
+ new_master_password = args[0]
198
+
199
+ begin
200
+ validate!(new_master_password)
201
+ rescue Pwl::InvalidMasterPasswordError => e
202
+ exit_with(:validation_new_failed, options.verbose, :message => e.message)
203
+ end
204
+ else
205
+ # If running interactively (console or gui), we loop until we get a valid password or break
206
+ begin
207
+ new_master_password = get_password('Enter new master password:', options.gui)
208
+ end while begin
209
+ validate!(new_master_password)
210
+ rescue Pwl::InvalidMasterPasswordError => e
211
+ STDERR.puts e.message
212
+ options.gui || STDIN.tty? # only continue the loop when in interactive mode
213
+ end
214
+
215
+ # Confirm new password
216
+ if new_master_password != get_password('Enter new master password again:', options.gui)
217
+ exit_with(:passwords_dont_match, options.verbose)
218
+ end
219
+ end
220
+ rescue Pwl::Dialog::Cancelled
221
+ exit_with(:aborted, options.verbose)
222
+ end
223
+
224
+ store.change_password!(new_master_password)
225
+ STDERR.puts "Successfully changed master password." if options.verbose
226
+ end
227
+ end
228
+
229
+ command :export do |c|
230
+ c.syntax = "#{program(:name)} #{c.name}"
231
+ c.summary = 'Exports all entries.'
232
+ c.description = 'This command prints all entries to STDOUT.'
233
+ c.example 'Prints all entries', "#{program(:name)} #{c.name}"
234
+ c.action do |args, options|
235
+ exit_with(:file_not_found, options.verbose, :file => store_file) unless File.exists?(store_file)
236
+
237
+ begin
238
+ template = ERB.new(File.read(DEFAULT_EXPORT_TEMPLATE))
239
+ store = Pwl::Store.open(store_file, get_password('Enter master password:', options.gui))
240
+ puts template.result(binding)
241
+ rescue Pwl::Dialog::Cancelled
242
+ exit_with(:aborted, options.verbose)
243
+ end
244
+ end
245
+ end
246
+
247
+ def exit_with(error_code, verbose, msg_args = {})
248
+ msg = EXIT_CODES[error_code]
249
+ raise "No message defined for error #{error_code}" if !msg
250
+
251
+ if msg.error? || verbose # always print errors; messages only when verbose
252
+ STDERR.puts msg.to_s(msg_args)
253
+ end
254
+
255
+ exit(msg.exit_code)
256
+ end
257
+
258
+ def get_password(prompt, gui = false)
259
+ (gui ? Pwl::Dialog::Password.new(program(:name), prompt) : Pwl::Dialog::ConsolePasswordDialog.new(prompt)).get_input
260
+ end
261
+
262
+ def get_text(prompt, gui = false)
263
+ (gui ? Pwl::Dialog::Text.new(program(:name), prompt) : Pwl::Dialog::ConsoleTextDialog.new(prompt)).get_input
264
+ end
265
+
266
+ def validate!(pwd)
267
+ Pwl::Store.password_policy.validate!(pwd)
268
+ end
@@ -0,0 +1,37 @@
1
+ require 'open3'
2
+
3
+ module Pwl
4
+ module Dialog
5
+ class AppNotFoundError < StandardError;end
6
+ class Cancelled < StandardError;end
7
+
8
+ #
9
+ # Base class for dialogs
10
+ #
11
+ class Base
12
+ attr_reader :title, :prompt
13
+
14
+ #
15
+ # Constructs a new dialog with the given title and prompt.
16
+ #
17
+ def initialize(title, prompt)
18
+ @title, @prompt = title, prompt
19
+ end
20
+ end
21
+
22
+ #
23
+ # Base class for dialogs implemented by executing a system command.
24
+ #
25
+ class SystemDialog < Base
26
+ def get_input
27
+ out, err, rc = Open3.capture3(command)
28
+ raise Cancelled.new(rc.exitstatus) unless 0 == rc.exitstatus
29
+ out.chomp
30
+ end
31
+
32
+ def command
33
+ raise "Not implemented. A derived class is expected to provide the OS command for prompting a password."
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,67 @@
1
+ module Pwl
2
+ module Dialog
3
+ class CocoaDialog < SystemDialog
4
+ #
5
+ # CocoaDialog returns two lines. The first line contains the number of the button, and the second line contains
6
+ # the actual user input. This method amends the base method with handling the two lines.
7
+ #
8
+ def get_input
9
+ result = super.lines.to_a
10
+ result = [] if result.blank?
11
+
12
+ case result.size
13
+ when 1 then return_or_cancel(result[0], '')
14
+ when 2 then return_or_cancel(result[0], result[1].chomp)
15
+ else raise "Unknown response from running '#{command}'"
16
+ end
17
+ end
18
+
19
+ protected
20
+
21
+ #
22
+ # Attempt to find an app within the user's home. If it doesn't exist, an attempt is made to find a system-installed file.
23
+ #
24
+ def local_app_name
25
+ [File.expand_path("~/"), '/'].each{|place|
26
+ local_app = File.join(place, APP_NAME)
27
+ return local_app if File.exist?(local_app) && File.executable?(local_app)
28
+ }
29
+ raise AppNotFoundError.new("Could not find the CocoaDialog app. Maybe it is not installed?")
30
+ end
31
+
32
+ #
33
+ # Return the generic command that is common for all dialogs deriving from this class.
34
+ #
35
+ # Derived classes are expected to implement the +type+ method that should return
36
+ #
37
+ def command
38
+ "#{local_app_name} #{type} --title \"#{title}\" --informative-text \"#{prompt}\""
39
+ end
40
+
41
+ private
42
+ APP_NAME = "Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog"
43
+
44
+ def return_or_cancel(statusLine, resultLine)
45
+ status = statusLine.to_i - 1
46
+
47
+ if 0 == status
48
+ resultLine
49
+ else
50
+ raise Cancelled.new(status)
51
+ end
52
+ end
53
+ end
54
+
55
+ class CocoaTextDialog < CocoaDialog
56
+ def type
57
+ 'standard-inputbox'
58
+ end
59
+ end
60
+
61
+ class CocoaPasswordDialog < CocoaDialog
62
+ def type
63
+ 'secure-standard-inputbox'
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,39 @@
1
+ require 'highline'
2
+
3
+ module Pwl
4
+ module Dialog
5
+ class ConsoleDialog < Base
6
+ def initialize(prompt)
7
+ super(nil, prompt)
8
+ @dialog = HighLine.new(STDIN, STDERR)
9
+ end
10
+
11
+ protected
12
+ attr_reader :dialog
13
+ end
14
+
15
+ class ConsolePasswordDialog < ConsoleDialog
16
+ def initialize(prompt = 'Please enter the master password:')
17
+ super(prompt)
18
+ end
19
+
20
+ def get_input
21
+ begin
22
+ STDIN.tty? ? @dialog.ask(prompt){|q| q.echo = "*"} : STDIN.read.chomp
23
+ rescue Interrupt
24
+ raise Cancelled.new(1)
25
+ end
26
+ end
27
+ end
28
+
29
+ class ConsoleTextDialog < ConsoleDialog
30
+ def get_input
31
+ begin
32
+ STDIN.tty? ? @dialog.ask(prompt) : STDIN.read.chomp
33
+ rescue Interrupt
34
+ raise Cancelled.new(1)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end