2013-07-16 20:36:41 +00:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
# author: nishiki
|
|
|
|
# mail: nishiki@yaegashi.fr
|
2013-07-17 20:31:28 +00:00
|
|
|
# info: a simple script who m your passwords
|
2013-07-16 20:36:41 +00:00
|
|
|
|
|
|
|
require 'rubygems'
|
|
|
|
require 'highline/import'
|
2013-07-18 19:56:45 +00:00
|
|
|
require 'pathname'
|
2013-12-20 16:38:58 +00:00
|
|
|
require 'readline'
|
2013-12-25 17:51:41 +00:00
|
|
|
require 'i18n'
|
2014-12-06 22:53:03 +00:00
|
|
|
require 'colorize'
|
2013-07-18 19:56:45 +00:00
|
|
|
|
2014-09-12 06:41:27 +00:00
|
|
|
require "#{APP_ROOT}/lib/MPW"
|
2015-02-06 23:10:39 +00:00
|
|
|
require "#{APP_ROOT}/lib/Sync"
|
2013-07-16 20:36:41 +00:00
|
|
|
|
|
|
|
class Cli
|
|
|
|
|
2013-08-25 08:07:39 +00:00
|
|
|
# Constructor
|
2013-12-30 21:14:29 +00:00
|
|
|
# @args: lang -> the operating system language
|
|
|
|
# config_file -> a specify config file
|
2014-09-07 08:00:46 +00:00
|
|
|
def initialize(config)
|
2014-01-15 22:11:39 +00:00
|
|
|
@config = config
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sync the data with the server
|
2014-01-26 11:13:32 +00:00
|
|
|
# @rtnr: true if the synchro is finish
|
2014-01-30 22:08:38 +00:00
|
|
|
def sync
|
2015-02-08 19:56:39 +00:00
|
|
|
@sync = MPW::Sync.new(@config, @mpw, @password)
|
2015-01-17 11:01:37 +00:00
|
|
|
|
2015-02-06 23:10:39 +00:00
|
|
|
raise(@sync.error_msg) if not @sync.get_remote
|
|
|
|
raise(@sync.error_msg) if not @sync.sync
|
2015-01-17 11:01:37 +00:00
|
|
|
|
2015-02-06 23:10:39 +00:00
|
|
|
return true
|
2015-02-08 20:40:01 +00:00
|
|
|
rescue Exception => e
|
2014-12-06 22:53:03 +00:00
|
|
|
puts "#{I18n.t('display.error')} #7: #{e}".red
|
2014-01-26 11:13:32 +00:00
|
|
|
return false
|
2013-07-16 20:36:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Create a new config file
|
2013-12-25 17:51:41 +00:00
|
|
|
# @args: lang -> the software language
|
|
|
|
def setup(lang)
|
2014-01-26 10:39:53 +00:00
|
|
|
puts I18n.t('form.setup.title')
|
2013-12-30 18:47:47 +00:00
|
|
|
puts '--------------------'
|
2014-08-31 10:29:57 +00:00
|
|
|
language = ask(I18n.t('form.setup.lang', lang: lang)).to_s
|
2014-01-26 10:39:53 +00:00
|
|
|
key = ask(I18n.t('form.setup.gpg_key')).to_s
|
2014-02-02 13:20:24 +00:00
|
|
|
share_keys = ask(I18n.t('form.setup.share_gpg_keys')).to_s
|
2014-12-06 15:24:40 +00:00
|
|
|
file_gpg = ask(I18n.t('form.setup.gpg_file', home: @conf.dir_config)).to_s
|
2014-02-01 18:11:19 +00:00
|
|
|
sync_type = ask(I18n.t('form.setup.sync_type')).to_s
|
2014-02-02 13:20:24 +00:00
|
|
|
|
|
|
|
if ['ssh', 'ftp', 'mpw'].include?(sync_type)
|
|
|
|
sync_host = ask(I18n.t('form.setup.sync_host')).to_s
|
|
|
|
sync_port = ask(I18n.t('form.setup.sync_port')).to_s
|
|
|
|
sync_user = ask(I18n.t('form.setup.sync_user')).to_s
|
|
|
|
sync_pwd = ask(I18n.t('form.setup.sync_pwd')).to_s
|
|
|
|
sync_path = ask(I18n.t('form.setup.sync_path')).to_s
|
|
|
|
end
|
2013-07-16 20:36:41 +00:00
|
|
|
|
2014-11-16 18:39:38 +00:00
|
|
|
if language.nil? or language.empty?
|
2014-03-16 20:28:23 +00:00
|
|
|
language = lang
|
2014-02-02 13:20:24 +00:00
|
|
|
end
|
2014-03-16 20:28:23 +00:00
|
|
|
I18n.locale = language.to_sym
|
2013-12-25 17:51:41 +00:00
|
|
|
|
2014-11-16 18:39:38 +00:00
|
|
|
sync_type = sync_type.nil? or sync_type.empty? ? nil : sync_type
|
|
|
|
sync_host = sync_host.nil? or sync_host.empty? ? nil : sync_host
|
|
|
|
sync_port = sync_port.nil? or sync_port.empty? ? nil : sync_port.to_i
|
|
|
|
sync_user = sync_user.nil? or sync_user.empty? ? nil : sync_user
|
|
|
|
sync_pwd = sync_pwd.nil? or sync_pwd.empty? ? nil : sync_pwd
|
|
|
|
sync_path = sync_path.nil? or sync_path.empty? ? nil : sync_path
|
2014-01-25 15:53:48 +00:00
|
|
|
|
2014-12-06 22:40:09 +00:00
|
|
|
if @config.setup(key, share_keys, language, file_gpg, sync_type, sync_host, sync_port, sync_user, sync_pwd, sync_path)
|
2014-12-06 22:53:03 +00:00
|
|
|
puts "#{I18n.t('form.setup.valid')}".green
|
2013-07-17 20:31:28 +00:00
|
|
|
else
|
2014-12-06 22:53:03 +00:00
|
|
|
puts "#{I18n.t('display.error')} #8: #{@config.error_msg}".red
|
2014-02-02 13:20:24 +00:00
|
|
|
exit 2
|
2013-07-16 20:36:41 +00:00
|
|
|
end
|
2014-01-03 13:31:43 +00:00
|
|
|
|
2014-02-02 09:16:10 +00:00
|
|
|
if not @config.checkconfig
|
2014-12-06 22:53:03 +00:00
|
|
|
puts "#{I18n.t('display.error')} #9: #{@config.error_msg}".red
|
2014-01-25 15:53:48 +00:00
|
|
|
exit 2
|
2014-01-03 13:31:43 +00:00
|
|
|
end
|
2013-07-17 20:31:28 +00:00
|
|
|
end
|
2013-07-16 20:36:41 +00:00
|
|
|
|
2014-03-16 20:28:23 +00:00
|
|
|
# Setup a new GPG key
|
|
|
|
def setup_gpg_key
|
|
|
|
puts I18n.t('form.setup_gpg_key.title')
|
|
|
|
puts '--------------------'
|
2014-03-18 20:08:09 +00:00
|
|
|
ask = ask(I18n.t('form.setup_gpg_key.ask')).to_s
|
|
|
|
|
2014-11-16 18:39:38 +00:00
|
|
|
if not ['Y', 'y', 'O', 'o'].include?(ask)
|
2014-03-18 20:08:09 +00:00
|
|
|
puts I18n.t('form.setup_gpg_key.no_create')
|
|
|
|
exit 2
|
|
|
|
end
|
|
|
|
|
2014-03-16 20:28:23 +00:00
|
|
|
name = ask(I18n.t('form.setup_gpg_key.name')).to_s
|
2014-04-04 15:03:46 +00:00
|
|
|
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
|
|
|
|
puts I18n.t('form.setup_gpg_key.error_password')
|
|
|
|
exit 2
|
|
|
|
end
|
|
|
|
|
2014-03-16 20:28:23 +00:00
|
|
|
length = ask(I18n.t('form.setup_gpg_key.length')).to_s
|
|
|
|
expire = ask(I18n.t('form.setup_gpg_key.expire')).to_s
|
2014-04-04 15:03:46 +00:00
|
|
|
password = password.to_s
|
2014-03-16 20:28:23 +00:00
|
|
|
|
2014-11-16 18:39:38 +00:00
|
|
|
length = length.nil? or length.empty? ? 2048 : length.to_i
|
|
|
|
expire = expire.nil? or expire.empty? ? 0 : expire.to_i
|
2014-03-16 20:28:23 +00:00
|
|
|
|
|
|
|
puts I18n.t('form.setup_gpg_key.wait')
|
|
|
|
|
|
|
|
if @config.setup_gpg_key(password, name, length, expire)
|
2014-12-06 22:53:03 +00:00
|
|
|
puts "#{I18n.t('form.setup_gpg_key.valid')}".green
|
2014-03-16 20:28:23 +00:00
|
|
|
else
|
2014-12-06 22:53:03 +00:00
|
|
|
puts "#{I18n.t('display.error')} #10: #{@config.error_msg}".red
|
2014-03-16 20:28:23 +00:00
|
|
|
exit 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-25 08:07:39 +00:00
|
|
|
# Request the GPG password and decrypt the file
|
2014-01-30 22:08:38 +00:00
|
|
|
def decrypt
|
2014-11-16 18:39:38 +00:00
|
|
|
if not defined?(@mpw)
|
2014-02-02 16:34:05 +00:00
|
|
|
@mpw = MPW::MPW.new(@config.file_gpg, @config.key, @config.share_keys)
|
2014-01-25 15:53:48 +00:00
|
|
|
end
|
|
|
|
|
2015-02-06 23:10:39 +00:00
|
|
|
@password = ask(I18n.t('display.gpg_password')) {|q| q.echo = false}
|
|
|
|
if not @mpw.decrypt(@password)
|
2014-12-06 22:53:03 +00:00
|
|
|
puts "#{I18n.t('display.error')} #11: #{@mpw.error_msg}".red
|
2014-01-25 15:53:48 +00:00
|
|
|
exit 2
|
|
|
|
end
|
2013-07-17 20:31:28 +00:00
|
|
|
end
|
2013-07-16 20:36:41 +00:00
|
|
|
|
2013-08-25 08:07:39 +00:00
|
|
|
# Display the query's result
|
|
|
|
# @args: search -> the string to search
|
|
|
|
# protocol -> search from a particular protocol
|
2015-01-16 23:05:40 +00:00
|
|
|
def display(search, protocol=nil, group=nil)
|
2015-01-28 22:08:25 +00:00
|
|
|
result = @mpw.list(search: search)
|
2013-07-16 20:36:41 +00:00
|
|
|
|
2015-01-16 23:05:40 +00:00
|
|
|
case result.length
|
|
|
|
when 0
|
|
|
|
puts I18n.t('display.nothing')
|
|
|
|
when 1
|
|
|
|
display_item(result.first)
|
|
|
|
else
|
|
|
|
i = 1
|
2015-01-28 22:08:25 +00:00
|
|
|
result.each do |item|
|
2015-01-16 23:05:40 +00:00
|
|
|
print "#{i}: ".cyan
|
2015-01-28 22:08:25 +00:00
|
|
|
print item.name
|
|
|
|
print " -> #{item.comment}".magenta if not item.comment.to_s.empty?
|
2015-01-16 23:05:40 +00:00
|
|
|
print "\n"
|
|
|
|
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
choice = ask(I18n.t('form.select')).to_i
|
|
|
|
|
|
|
|
if choice >= 1 and choice < i
|
|
|
|
display_item(result[choice-1])
|
|
|
|
else
|
|
|
|
puts "#{I18n.t('display.warning')}: #{I18n.t('warning.select')}".yellow
|
2013-07-17 20:31:28 +00:00
|
|
|
end
|
2013-07-16 20:36:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-02 17:50:40 +00:00
|
|
|
# Display an item in the default format
|
2013-09-02 17:14:33 +00:00
|
|
|
# @args: item -> an array with the item information
|
2015-01-16 23:05:40 +00:00
|
|
|
def display_item(item)
|
2015-01-16 23:12:01 +00:00
|
|
|
puts '--------------------'.cyan
|
|
|
|
print 'Id: '.cyan
|
2015-01-28 22:08:25 +00:00
|
|
|
puts item.id
|
2015-01-16 23:12:01 +00:00
|
|
|
print "#{I18n.t('display.name')}: ".cyan
|
2015-01-28 22:08:25 +00:00
|
|
|
puts item.name
|
2015-01-16 23:12:01 +00:00
|
|
|
print "#{I18n.t('display.group')}: ".cyan
|
2015-01-28 22:08:25 +00:00
|
|
|
puts item.group
|
2015-01-16 23:12:01 +00:00
|
|
|
print "#{I18n.t('display.server')}: ".cyan
|
2015-01-28 22:08:25 +00:00
|
|
|
puts item.host
|
2015-01-16 23:12:01 +00:00
|
|
|
print "#{I18n.t('display.protocol')}: ".cyan
|
2015-01-28 22:08:25 +00:00
|
|
|
puts item.protocol
|
2015-01-16 23:12:01 +00:00
|
|
|
print "#{I18n.t('display.login')}: ".cyan
|
2015-01-28 22:08:25 +00:00
|
|
|
puts item.user
|
2015-01-16 23:12:01 +00:00
|
|
|
print "#{I18n.t('display.password')}: ".cyan
|
2015-01-28 22:08:25 +00:00
|
|
|
puts item.password
|
2015-01-16 23:12:01 +00:00
|
|
|
print "#{I18n.t('display.port')}: ".cyan
|
2015-01-28 22:08:25 +00:00
|
|
|
puts item.port
|
2015-01-16 23:12:01 +00:00
|
|
|
print "#{I18n.t('display.comment')}: ".cyan
|
2015-01-28 22:08:25 +00:00
|
|
|
puts item.comment
|
2013-09-02 17:14:33 +00:00
|
|
|
end
|
|
|
|
|
2013-08-25 08:07:39 +00:00
|
|
|
# Form to add a new item
|
2014-02-02 09:16:10 +00:00
|
|
|
def add
|
2015-02-01 10:48:02 +00:00
|
|
|
options = {}
|
|
|
|
|
2014-01-26 10:39:53 +00:00
|
|
|
puts I18n.t('form.add.title')
|
2013-12-30 18:47:47 +00:00
|
|
|
puts '--------------------'
|
2015-02-01 10:48:02 +00:00
|
|
|
options[:name] = ask(I18n.t('form.add.name')).to_s
|
|
|
|
options[:group] = ask(I18n.t('form.add.group')).to_s
|
|
|
|
options[:host] = ask(I18n.t('form.add.server')).to_s
|
|
|
|
options[:protocol] = ask(I18n.t('form.add.protocol')).to_s
|
|
|
|
options[:user] = ask(I18n.t('form.add.login')).to_s
|
2015-02-01 11:23:50 +00:00
|
|
|
options[:password] = ask(I18n.t('form.add.password')).to_s
|
2015-02-01 10:48:02 +00:00
|
|
|
options[:port] = ask(I18n.t('form.add.port')).to_s
|
|
|
|
options[:comment] = ask(I18n.t('form.add.comment')).to_s
|
|
|
|
|
|
|
|
item = MPW::Item.new(options)
|
|
|
|
if @mpw.add(item)
|
2014-02-02 09:16:10 +00:00
|
|
|
if @mpw.encrypt
|
|
|
|
sync
|
2014-12-06 22:53:03 +00:00
|
|
|
puts "#{I18n.t('form.add.valid')}".green
|
2013-08-26 18:52:18 +00:00
|
|
|
else
|
2014-12-06 22:53:03 +00:00
|
|
|
puts "#{I18n.t('display.error')} #12: #{@mpw.error_msg}".red
|
2013-08-26 18:52:18 +00:00
|
|
|
end
|
2013-07-17 20:31:28 +00:00
|
|
|
else
|
2015-02-01 11:23:50 +00:00
|
|
|
puts "#{I18n.t('display.error')} #13: #{item.error_msg}".red
|
2013-07-17 20:31:28 +00:00
|
|
|
end
|
|
|
|
end
|
2013-07-16 20:36:41 +00:00
|
|
|
|
2013-08-25 08:07:39 +00:00
|
|
|
# Update an item
|
|
|
|
# @args: id -> the item's id
|
2013-07-17 20:31:28 +00:00
|
|
|
def update(id)
|
2015-01-28 22:08:25 +00:00
|
|
|
item = @mpw.search_by_id(id)
|
2013-07-17 20:31:28 +00:00
|
|
|
|
2015-02-01 11:23:50 +00:00
|
|
|
if not item.nil?
|
|
|
|
options = {}
|
|
|
|
|
2014-01-26 10:39:53 +00:00
|
|
|
puts I18n.t('form.update.title')
|
2013-12-30 18:47:47 +00:00
|
|
|
puts '--------------------'
|
2015-02-01 11:23:50 +00:00
|
|
|
options[:name] = ask(I18n.t('form.update.name' , name: item.name)).to_s
|
|
|
|
options[:group] = ask(I18n.t('form.update.group' , group: item.group)).to_s
|
|
|
|
options[:host] = ask(I18n.t('form.update.server' , server: item.host)).to_s
|
|
|
|
options[:protocol] = ask(I18n.t('form.update.protocol', protocol: item.protocol)).to_s
|
|
|
|
options[:user] = ask(I18n.t('form.update.login' , login: item.user)).to_s
|
|
|
|
options[:password] = ask(I18n.t('form.update.password')).to_s
|
|
|
|
options[:port] = ask(I18n.t('form.update.port' , port: item.port)).to_s
|
|
|
|
options[:comment] = ask(I18n.t('form.update.comment' , comment: item.comment)).to_s
|
|
|
|
|
|
|
|
options.delete_if { |k,v| v.empty? }
|
2013-07-17 20:31:28 +00:00
|
|
|
|
2015-02-01 11:23:50 +00:00
|
|
|
if item.update(options)
|
2014-02-02 09:16:10 +00:00
|
|
|
if @mpw.encrypt
|
|
|
|
sync
|
2014-12-06 22:53:03 +00:00
|
|
|
puts "#{I18n.t('form.update.valid')}".green
|
2013-07-17 20:31:28 +00:00
|
|
|
else
|
2014-12-06 22:53:03 +00:00
|
|
|
puts "#{I18n.t('display.error')} #14: #{@mpw.error_msg}".red
|
2013-07-17 20:31:28 +00:00
|
|
|
end
|
|
|
|
else
|
2015-02-01 11:23:50 +00:00
|
|
|
puts "#{I18n.t('display.error')} #15: #{item.error_msg}".red
|
2013-07-16 20:36:41 +00:00
|
|
|
end
|
2013-07-17 20:31:28 +00:00
|
|
|
else
|
2014-01-26 10:39:53 +00:00
|
|
|
puts I18n.t('display.nothing')
|
2013-07-17 20:31:28 +00:00
|
|
|
end
|
|
|
|
end
|
2013-07-16 20:36:41 +00:00
|
|
|
|
2013-08-25 08:07:39 +00:00
|
|
|
# Remove an item
|
|
|
|
# @args: id -> the item's id
|
|
|
|
# force -> no resquest a validation
|
2015-02-01 11:35:42 +00:00
|
|
|
def delete(id, force=false)
|
2013-07-17 21:19:35 +00:00
|
|
|
if not force
|
2015-02-01 11:35:42 +00:00
|
|
|
item = @mpw.search_by_id(id)
|
2013-08-26 19:23:35 +00:00
|
|
|
|
2015-02-01 11:35:42 +00:00
|
|
|
if not item.nil?
|
|
|
|
display_item(item)
|
2013-08-26 19:23:35 +00:00
|
|
|
|
2014-08-31 10:29:57 +00:00
|
|
|
confirm = ask("#{I18n.t('form.delete.ask', id: id)} (y/N) ").to_s
|
2013-09-08 16:02:10 +00:00
|
|
|
if confirm =~ /^(y|yes|YES|Yes|Y)$/
|
|
|
|
force = true
|
|
|
|
end
|
|
|
|
else
|
2014-01-26 10:39:53 +00:00
|
|
|
puts I18n.t('display.nothing')
|
2013-07-17 21:19:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if force
|
2015-02-01 11:35:42 +00:00
|
|
|
item.delete
|
|
|
|
|
|
|
|
if @mpw.encrypt
|
|
|
|
sync
|
|
|
|
puts "#{I18n.t('form.delete.valid', id: id)}".green
|
2013-07-17 20:31:28 +00:00
|
|
|
else
|
2015-02-01 11:35:42 +00:00
|
|
|
puts "#{I18n.t('display.error')} #16: #{@mpw.error_msg}".red
|
2013-07-17 20:31:28 +00:00
|
|
|
end
|
2013-07-16 20:36:41 +00:00
|
|
|
end
|
2013-07-17 20:31:28 +00:00
|
|
|
end
|
2013-07-16 20:36:41 +00:00
|
|
|
|
2013-08-25 08:07:39 +00:00
|
|
|
# Export the items in a CSV file
|
|
|
|
# @args: file -> the destination file
|
2014-12-06 22:32:09 +00:00
|
|
|
def export(file, type=:yaml)
|
2014-11-15 18:14:42 +00:00
|
|
|
if @mpw.export(file, type)
|
2014-12-06 22:53:03 +00:00
|
|
|
puts "#{I18n.t('export.valid', file)}".green
|
2013-07-25 17:51:43 +00:00
|
|
|
else
|
2014-12-06 22:53:03 +00:00
|
|
|
puts "#{I18n.t('display.error')} #17: #{@mpw.error_msg}".red
|
2013-07-25 17:51:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-25 08:07:39 +00:00
|
|
|
# Import items from a CSV file
|
|
|
|
# @args: file -> the import file
|
2013-08-26 20:19:37 +00:00
|
|
|
# force -> no resquest a validation
|
2014-12-06 22:32:09 +00:00
|
|
|
def import(file, type=:yaml, force=false)
|
2013-08-26 20:19:37 +00:00
|
|
|
|
|
|
|
if not force
|
2014-12-06 22:32:09 +00:00
|
|
|
result = @mpw.import_preview(file, type)
|
2014-12-06 20:37:18 +00:00
|
|
|
if result.is_a?(Array) and not result.empty?
|
2013-08-26 20:19:37 +00:00
|
|
|
result.each do |r|
|
2015-01-16 22:11:10 +00:00
|
|
|
display_item(r)
|
2013-08-26 20:19:37 +00:00
|
|
|
end
|
|
|
|
|
2014-08-31 10:29:57 +00:00
|
|
|
confirm = ask("#{I18n.t('form.import.ask', file: file)} (y/N) ").to_s
|
2013-08-26 20:19:37 +00:00
|
|
|
if confirm =~ /^(y|yes|YES|Yes|Y)$/
|
|
|
|
force = true
|
|
|
|
end
|
|
|
|
else
|
2014-01-26 10:39:53 +00:00
|
|
|
puts I18n.t('form.import.not_valid')
|
2013-08-26 20:19:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if force
|
2014-12-06 22:32:09 +00:00
|
|
|
if @mpw.import(file, type) and @mpw.encrypt
|
2014-02-02 09:16:10 +00:00
|
|
|
sync
|
2014-12-06 22:53:03 +00:00
|
|
|
puts "#{I18n.t('form.import.valid')}".green
|
2013-07-25 17:51:43 +00:00
|
|
|
else
|
2014-12-06 22:53:03 +00:00
|
|
|
puts "#{I18n.t('display.error')} #18: #{@mpw.error_msg}".red
|
2013-07-25 17:51:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-16 20:36:41 +00:00
|
|
|
end
|