1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2025-02-23 03:00:13 +00:00
mpw/lib/mpw/cli.rb

541 lines
14 KiB
Ruby
Raw Normal View History

2013-07-16 22:36:41 +02:00
#!/usr/bin/ruby
2016-06-30 22:02:45 +02:00
# MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2013-07-16 22:36:41 +02:00
2013-12-20 17:38:58 +01:00
require 'readline'
2016-10-13 21:59:51 +02:00
require 'locale'
2013-12-25 18:51:41 +01:00
require 'i18n'
2014-12-06 23:53:03 +01:00
require 'colorize'
require 'highline/import'
2016-07-09 14:06:49 +02:00
require 'clipboard'
2016-08-09 22:06:47 +02:00
require 'tmpdir'
2016-07-10 18:55:18 +02:00
require 'mpw/item'
require 'mpw/mpw'
2013-07-16 22:36:41 +02:00
2016-05-07 15:05:14 +02:00
module MPW
2013-07-16 22:36:41 +02:00
class Cli
2013-08-25 10:07:39 +02:00
# Constructor
2016-07-01 09:22:28 +02:00
# @args: config -> the config
# sync -> boolean for sync or not
2016-08-02 21:59:46 +02:00
# clipboard -> enable clopboard
# otp -> enable otp
def initialize(config, clipboard=true, sync=true, otp=false)
2016-07-09 14:06:49 +02:00
@config = config
@clipboard = clipboard
@sync = sync
2016-08-02 21:59:46 +02:00
@otp = otp
2013-07-16 22:36:41 +02:00
end
2016-10-13 21:59:51 +02:00
# Change a parameter int the config after init
# @args: options -> param to change
def set_config(options)
raise I18n.t('error.config.check') if not @config.is_valid?
gpg_key = options[:gpg_key] || @config.key
lang = options[:lang] || @config.lang
wallet_dir = options[:wallet_dir] || @config.wallet_dir
gpg_exe = options[:gpg_exe] || @config.gpg_exe
@config.setup(gpg_key, lang, wallet_dir, gpg_exe)
rescue Exception => e
puts "#{I18n.t('display.error')} #15: #{e}".red
exit 2
end
2013-07-16 22:36:41 +02:00
# Create a new config file
2016-09-26 21:55:38 +02:00
# @args: language -> the software language
2016-10-13 21:59:51 +02:00
def setup(options)
2016-09-26 21:55:38 +02:00
@config.is_valid?
2016-10-13 21:59:51 +02:00
lang = options[:lang] || Locale::Tag.parse(ENV['LANG']).to_simple.to_s[0..1]
2016-10-13 21:59:51 +02:00
I18n.locale = lang.to_sym
2013-12-25 18:51:41 +01:00
2016-10-13 21:59:51 +02:00
@config.setup(options[:gpg_key], lang, options[:wallet_dir], options[:gpg_exe])
raise I18n.t('error.config.check') if not @config.is_valid?
2016-05-15 18:30:33 +02:00
puts "#{I18n.t('form.setup_config.valid')}".green
rescue Exception => e
puts "#{I18n.t('display.error')} #8: #{e}".red
exit 2
2013-07-17 22:31:28 +02:00
end
2014-03-16 21:28:23 +01:00
# Setup a new GPG key
2016-10-13 21:59:51 +02:00
# @args: gpg_key -> the key name
def setup_gpg_key(gpg_key)
password = ask(I18n.t('form.setup_gpg_key.password')) {|q| q.echo = false}
confirm = ask(I18n.t('form.setup_gpg_key.confirm_password')) {|q| q.echo = false}
if password != confirm
2016-05-15 18:30:33 +02:00
raise I18n.t('form.setup_gpg_key.error_password')
end
2016-10-14 23:20:55 +02:00
@password = password.to_s
2014-03-16 21:28:23 +01:00
puts I18n.t('form.setup_gpg_key.wait')
2016-10-14 23:20:55 +02:00
@config.setup_gpg_key(@password, gpg_key)
2016-05-15 18:30:33 +02:00
puts "#{I18n.t('form.setup_gpg_key.valid')}".green
rescue Exception => e
puts "#{I18n.t('display.error')} #8: #{e}".red
exit 2
2014-03-16 21:28:23 +01:00
end
2016-05-12 22:43:57 +02:00
# Setup wallet config for sync
2016-10-14 23:20:55 +02:00
# @args: wallet -> the wallet name
def setup_wallet_config(wallet = nil)
#config = {}
#config['sync'] = {}
2016-05-12 22:43:57 +02:00
2016-10-14 23:20:55 +02:00
#puts '--------------------'
#config['sync']['type'] = ask(I18n.t('form.setup_wallet.sync_type')).to_s
2016-05-12 22:43:57 +02:00
2016-10-14 23:20:55 +02:00
#if ['ftp', 'ssh'].include?(config['sync']['type'].downcase)
# config['sync']['host'] = ask(I18n.t('form.setup_wallet.sync_host')).to_s
# config['sync']['port'] = ask(I18n.t('form.setup_wallet.sync_port')).to_s
# config['sync']['user'] = ask(I18n.t('form.setup_wallet.sync_user')).to_s
# config['sync']['password'] = ask(I18n.t('form.setup_wallet.sync_pwd')).to_s
# config['sync']['path'] = ask(I18n.t('form.setup_wallet.sync_path')).to_s
#end
wallet_file = wallet.nil? ? "#{@config.wallet_dir}/default.mpw" : "#{@config.wallet_dir}/#{wallet}.mpw"
@mpw = MPW.new(@config.key, wallet_file, @password, @config.gpg_exe)
@mpw.read_data
@mpw.set_config
2016-05-12 22:43:57 +02:00
@mpw.write_data
2016-05-14 17:01:44 +02:00
puts "#{I18n.t('form.setup_wallet.valid')}".green
2016-05-12 22:43:57 +02:00
rescue Exception => e
puts "#{I18n.t('display.error')} #10: #{e}".red
exit 2
end
2013-08-25 10:07:39 +02:00
# Request the GPG password and decrypt the file
2014-01-30 23:08:38 +01:00
def decrypt
2014-11-16 19:39:38 +01:00
if not defined?(@mpw)
@password = ask(I18n.t('display.gpg_password')) {|q| q.echo = false}
2016-07-05 22:03:55 +02:00
@mpw = MPW.new(@config.key, @wallet_file, @password, @config.gpg_exe)
2014-01-25 16:53:48 +01:00
end
@mpw.read_data
2016-07-01 09:22:28 +02:00
@mpw.sync if @sync
2016-05-05 23:46:48 +02:00
rescue Exception => e
puts "#{I18n.t('display.error')} #11: #{e}".red
exit 2
2013-07-17 22:31:28 +02:00
end
2013-07-16 22:36:41 +02:00
2016-10-17 18:35:57 +02:00
# Format items on a table
def table(items=[])
group = '.'
i = 1
length_total = 10
length = { id: 3,
host: 10,
user: 8,
comment: 15,
otp: 5,
}
items.each do |item|
length[:host] = item.host.length + 3 if item.host.to_s.length > length[:host]
length[:user] = item.user.length + 3 if item.user.to_s.length > length[:user]
length[:comment] = item.comment.length + 3 if item.comment.to_s.length > length[:comment]
end
length[:id] = items.length.to_s.length + 2 if items.length.to_s.length > length[:id]
length.each_value { |v| length_total += v }
items.sort! { |a,b| a.group.to_s.downcase <=> b.group.to_s.downcase }
2016-10-15 17:40:35 +02:00
2016-10-17 18:35:57 +02:00
items.each do |item|
if group != item.group
group = item.group
2016-10-15 17:40:35 +02:00
2016-10-17 18:35:57 +02:00
if group.to_s.empty?
puts "\n#{I18n.t('display.no_group')}".red
else
puts "\n#{group}".red
end
2016-10-17 00:06:27 +02:00
2016-10-17 18:35:57 +02:00
print ' '
length_total.times { print '=' }
print "\n "
print ' ID '
print '| Host'
(length[:host] - 'Host'.length).times { print ' ' }
print '| User'
(length[:user] - 'User'.length).times { print ' ' }
print '| OTP '
print '| Comment'
(length[:comment] - 'Comment'.length).times { print ' ' }
print "\n "
length_total.times { print '=' }
print "\n"
end
2016-10-15 17:40:35 +02:00
2016-10-17 18:35:57 +02:00
print " #{i}".cyan
(length[:id] - i.to_s.length).times { print ' ' }
print '| '
print "#{item.host}".yellow
(length[:host] - item.host.to_s.length).times { print ' ' }
print '| '
print "#{item.user}".green
(length[:user] - item.user.to_s.length).times { print ' ' }
print '| '
if item.otp
print ' X '
else
4.times { print ' ' }
2016-10-17 00:06:27 +02:00
end
2016-10-17 18:35:57 +02:00
print '| '
print "#{item.comment}".magenta
(length[:comment] - item.comment.to_s.length).times { print ' ' }
print "\n"
2016-10-17 00:06:27 +02:00
2016-10-17 18:35:57 +02:00
i += 1
end
2016-10-15 17:40:35 +02:00
2016-10-17 18:35:57 +02:00
print "\n"
end
2016-10-17 00:06:27 +02:00
2016-10-17 18:35:57 +02:00
# Display the query's result
# @args: options -> the option to search
def list(options={})
result = @mpw.list(options)
2016-10-15 17:40:35 +02:00
2016-10-17 18:35:57 +02:00
if result.length == 0
puts I18n.t('display.nothing')
2016-10-17 00:06:27 +02:00
2016-10-17 18:35:57 +02:00
else
table(result)
2016-10-15 17:40:35 +02:00
end
end
2016-10-17 20:48:20 +02:00
# Get an item when multiple choice
# @args: items -> array of items
# @rtrn: item
def get_item(items)
return items[0] if items.length == 1
2016-07-09 17:11:18 +02:00
2016-10-17 20:48:20 +02:00
items.sort! { |a,b| a.group.to_s.downcase <=> b.group.to_s.downcase }
choice = ask(I18n.t('form.select')).to_i
2016-07-09 17:11:18 +02:00
2016-10-17 20:48:20 +02:00
if choice >= 1 and choice <= items.length
return items[choice-1]
2015-01-17 00:05:40 +01:00
else
2016-10-17 20:48:20 +02:00
return nil
2013-07-16 22:36:41 +02:00
end
end
# Display an item in the default format
2013-09-02 19:14:33 +02:00
# @args: item -> an array with the item information
2015-01-17 00:05:40 +01:00
def display_item(item)
2015-01-17 00:12:01 +01:00
puts '--------------------'.cyan
print 'Id: '.cyan
puts item.id
2015-01-17 00:12:01 +01:00
print "#{I18n.t('display.name')}: ".cyan
puts item.name
2015-01-17 00:12:01 +01:00
print "#{I18n.t('display.group')}: ".cyan
puts item.group
2015-01-17 00:12:01 +01:00
print "#{I18n.t('display.server')}: ".cyan
puts item.host
2015-01-17 00:12:01 +01:00
print "#{I18n.t('display.protocol')}: ".cyan
puts item.protocol
2015-01-17 00:12:01 +01:00
print "#{I18n.t('display.login')}: ".cyan
puts item.user
2016-08-02 21:59:46 +02:00
2016-07-09 14:06:49 +02:00
if @clipboard
2016-08-03 21:26:04 +02:00
print "#{I18n.t('display.password')}: ".cyan
2016-07-09 14:06:49 +02:00
puts '***********'
else
2016-08-03 21:26:04 +02:00
print "#{I18n.t('display.password')}: ".cyan
2016-07-09 14:06:49 +02:00
puts @mpw.get_password(item.id)
if @mpw.get_otp_code(item.id) > 0
print "#{I18n.t('display.otp_code')}: ".cyan
puts "#{@mpw.get_otp_code(item.id)} (#{@mpw.get_otp_remaining_time}s)"
end
2016-07-09 14:06:49 +02:00
end
2016-08-02 21:59:46 +02:00
2015-01-17 00:12:01 +01:00
print "#{I18n.t('display.port')}: ".cyan
puts item.port
2015-01-17 00:12:01 +01:00
print "#{I18n.t('display.comment')}: ".cyan
puts item.comment
2016-07-09 14:06:49 +02:00
clipboard(item) if @clipboard
end
# Copy in clipboard the login and password
2016-07-11 23:56:58 +02:00
# @args: item -> the item
2016-07-09 14:06:49 +02:00
def clipboard(item)
2016-07-11 23:56:58 +02:00
pid = nil
# Security: force quit after 90s
2016-07-14 00:12:21 +02:00
Thread.new do
sleep 90
exit
2016-07-11 23:56:58 +02:00
end
while true
choice = ask(I18n.t('form.clipboard.choice')).to_s
case choice
when 'q', 'quit'
break
when 'l', 'login'
Clipboard.copy(item.user)
puts I18n.t('form.clipboard.login').green
when 'p', 'password'
Clipboard.copy(@mpw.get_password(item.id))
puts I18n.t('form.clipboard.password').yellow
2016-07-14 00:12:21 +02:00
Thread.new do
sleep 30
2016-07-09 14:06:49 +02:00
2016-07-14 00:12:21 +02:00
Clipboard.clear
2016-07-11 23:56:58 +02:00
end
2016-07-09 14:06:49 +02:00
2016-08-02 21:59:46 +02:00
when 'o', 'otp'
Clipboard.copy(@mpw.get_otp_code(item.id))
puts I18n.t('form.clipboard.otp', time: @mpw.get_otp_remaining_time).yellow
2016-08-09 22:06:47 +02:00
when 'd', 'delete'
2016-09-26 21:56:00 +02:00
break if delete(item)
2016-08-09 22:06:47 +02:00
when 'u', 'update', 'e', 'edit'
update(item)
2016-07-11 23:56:58 +02:00
else
2016-08-02 21:59:46 +02:00
puts "----- #{I18n.t('form.clipboard.help.name')} -----".cyan
puts I18n.t('form.clipboard.help.login')
puts I18n.t('form.clipboard.help.password')
puts I18n.t('form.clipboard.help.otp_code')
2016-09-26 21:56:00 +02:00
puts I18n.t('form.clipboard.help.update')
puts I18n.t('form.clipboard.help.delete')
2016-07-11 23:56:58 +02:00
next
end
end
2016-07-09 14:06:49 +02:00
Clipboard.clear
rescue SystemExit, Interrupt
Clipboard.clear
2013-09-02 19:14:33 +02:00
end
2016-10-19 12:56:55 +02:00
# List all wallets
def list_wallet
@config.is_valid?
wallets = Dir.glob("#{@config.wallet_dir}/*.mpw")
wallets.each do |wallet|
puts File.basename(wallet, '.mpw')
end
end
2016-05-07 10:04:07 +02:00
# Display the wallet
# @args: wallet -> the wallet name
def get_wallet(wallet=nil)
2016-10-15 09:30:41 +02:00
@config.is_valid?
2016-05-07 10:04:07 +02:00
if wallet.to_s.empty?
wallets = Dir.glob("#{@config.wallet_dir}/*.mpw")
case wallets.length
when 0
puts I18n.t('display.nothing')
when 1
@wallet_file = wallets[0]
else
2016-10-19 12:56:55 +02:00
@wallet_file = "#{@config.wallet_dir}/default.mpw"
2016-05-07 10:04:07 +02:00
end
else
@wallet_file = "#{@config.wallet_dir}/#{wallet}.mpw"
end
end
2016-05-07 17:47:24 +02:00
# Add a new public key
# args: key -> the key name to add
2016-05-08 10:13:25 +02:00
# file -> gpg public file to import
def add_key(key, file=nil)
@mpw.add_key(key, file)
2016-05-07 14:36:57 +02:00
@mpw.write_data
2016-07-06 21:48:49 +02:00
@mpw.sync(true) if @sync
2016-05-07 16:27:55 +02:00
2016-05-14 17:01:44 +02:00
puts "#{I18n.t('form.add_key.valid')}".green
2016-05-07 14:36:57 +02:00
rescue Exception => e
puts "#{I18n.t('display.error')} #13: #{e}".red
end
2016-05-07 17:47:24 +02:00
# Add new public key
# args: key -> the key name to delete
def delete_key(key)
@mpw.delete_key(key)
@mpw.write_data
2016-07-06 21:48:49 +02:00
@mpw.sync(true) if @sync
2016-05-07 17:47:24 +02:00
2016-05-14 17:01:44 +02:00
puts "#{I18n.t('form.delete_key.valid')}".green
2016-05-07 17:47:24 +02:00
rescue Exception => e
puts "#{I18n.t('display.error')} #15: #{e}".red
end
2016-08-09 22:06:47 +02:00
def text_editor(template_name, item=nil)
2016-08-09 22:34:59 +02:00
editor = ENV['EDITOR'] || 'nano'
2016-08-09 22:06:47 +02:00
options = {}
opts = {}
template_file = "#{File.expand_path('../../../templates', __FILE__)}/#{template_name}.erb"
template = ERB.new(IO.read(template_file))
2015-02-01 11:48:02 +01:00
2016-08-09 22:06:47 +02:00
Dir.mktmpdir do |dir|
tmp_file = "#{dir}/#{template_name}.yml"
File.open(tmp_file, 'w') do |f|
f << template.result(binding)
end
2016-08-09 22:34:59 +02:00
system("#{editor} #{tmp_file}")
2016-08-09 22:06:47 +02:00
opts = YAML::load_file(tmp_file)
2016-08-02 21:59:46 +02:00
end
2016-08-09 22:06:47 +02:00
opts.delete_if { |k,v| v.to_s.empty? }
opts.each do |k,v|
options[k.to_sym] = v
end
return options
end
# Form to add a new item
def add
options = text_editor('add_form')
item = Item.new(options)
@mpw.add(item)
2016-08-09 22:06:47 +02:00
@mpw.set_password(item.id, options[:password]) if options.has_key?(:password)
@mpw.set_otp_key(item.id, options[:otp_key]) if options.has_key?(:otp_key)
@mpw.write_data
2016-07-06 21:48:49 +02:00
@mpw.sync(true) if @sync
2016-05-14 17:01:44 +02:00
puts "#{I18n.t('form.add_item.valid')}".green
2016-10-15 09:30:41 +02:00
rescue Exception => e
2016-08-09 22:34:59 +02:00
puts "#{I18n.t('display.error')} #13: #{e}".red
2013-07-17 22:31:28 +02:00
end
2013-07-16 22:36:41 +02:00
2013-08-25 10:07:39 +02:00
# Update an item
2016-10-17 20:48:20 +02:00
# @args: options -> the option to search
def update(options={})
items = @mpw.list(options)
if items.length == 0
puts "#{I18n.t('display.warning')}: #{I18n.t('warning.select')}".yellow
else
table(items) if items.length > 1
2016-08-02 21:59:46 +02:00
2016-10-17 20:48:20 +02:00
item = get_item(items)
options = text_editor('update_form', item)
item.update(options)
@mpw.set_password(item.id, options[:password]) if options.has_key?(:password)
@mpw.set_otp_key(item.id, options[:otp_key]) if options.has_key?(:otp_key)
@mpw.write_data
@mpw.sync(true) if @sync
2016-10-17 20:48:20 +02:00
puts "#{I18n.t('form.update_item.valid')}".green
end
2016-05-05 23:14:28 +02:00
rescue Exception => e
puts "#{I18n.t('display.error')} #14: #{e}".red
2013-07-17 22:31:28 +02:00
end
2013-07-16 22:36:41 +02:00
2013-08-25 10:07:39 +02:00
# Remove an item
2016-10-17 21:17:15 +02:00
# @args: options -> the option to search
def delete(options={})
items = @mpw.list(options)
if items.length == 0
puts "#{I18n.t('display.warning')}: #{I18n.t('warning.select')}".yellow
else
table(items)
2013-08-26 21:23:35 +02:00
2016-10-17 21:17:15 +02:00
item = get_item(items)
confirm = ask("#{I18n.t('form.delete_item.ask')} (y/N) ").to_s
if not confirm =~ /^(y|yes|YES|Yes|Y)$/
return false
end
item.delete
@mpw.write_data
@mpw.sync(true) if @sync
puts "#{I18n.t('form.delete_item.valid')}".green
end
2016-05-14 17:01:44 +02:00
rescue Exception => e
puts "#{I18n.t('display.error')} #16: #{e}".red
2013-07-17 22:31:28 +02:00
end
2013-07-16 22:36:41 +02:00
2013-08-25 10:07:39 +02:00
# Export the items in a CSV file
# @args: file -> the destination file
2016-10-19 15:08:08 +02:00
# options -> option to search
def export(file, options)
file = 'export-mpw.yml' if file.to_s.empty?
items = @mpw.list(options)
data = {}
i = 1
items.each do |item|
data.merge!(i => { 'host' => item.host,
'user' => item.user,
'group' => item.group,
'password' => @mpw.get_password(item.id),
'protocol' => item.protocol,
'port' => item.port,
'otp_key' => @mpw.get_otp_code(item.id),
'comment' => item.comment,
'last_edit' => item.last_edit,
'created' => item.created,
}
)
i += 1
end
File.open(file, 'w') {|f| f << data.to_yaml}
2016-05-17 19:34:05 +02:00
puts "#{I18n.t('export.export.valid', file)}".green
rescue Exception => e
2016-05-14 17:01:44 +02:00
puts "#{I18n.t('display.error')} #17: #{e}".red
2013-07-25 19:51:43 +02:00
end
# Import items from a YAML file
2013-08-25 10:07:39 +02:00
# @args: file -> the import file
def import(file)
@mpw.import(file)
@mpw.write_data
2013-08-26 22:19:37 +02:00
puts "#{I18n.t('form.import.valid')}".green
rescue Exception => e
puts "#{I18n.t('display.error')} #18: #{e}".red
2013-07-25 19:51:43 +02:00
end
2013-07-16 22:36:41 +02:00
end
2016-05-07 15:05:14 +02:00
end