1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-24 06:13:05 +00:00
mpw/lib/UI/Cli.rb

416 lines
12 KiB
Ruby
Raw Normal View History

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-01-14 22:00:52 +00:00
require 'yaml'
2013-07-18 19:56:45 +00:00
2014-09-12 06:41:27 +00:00
require "#{APP_ROOT}/lib/MPW"
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
2014-01-25 15:53:48 +00:00
if !defined?(@sync)
2014-01-30 22:08:38 +00:00
case @config.sync_type
when 'mpw'
2014-09-12 06:41:27 +00:00
require "#{APP_ROOT}/lib/Sync/MPWSync"
2014-01-30 22:08:38 +00:00
@sync = MPW::Sync::MPWSync.new
when 'sftp', 'scp', 'ssh'
2014-09-12 06:41:27 +00:00
require "#{APP_ROOT}/lib/Sync/SSH"
2014-01-30 22:08:38 +00:00
@sync = MPW::Sync::SSH.new
2014-02-02 10:24:28 +00:00
when 'ftp'
2014-09-12 06:41:27 +00:00
require "#{APP_ROOT}/lib/Sync/FTP"
2014-02-02 10:24:28 +00:00
@sync = MPW::Sync::FTP.new
2014-01-30 22:08:38 +00:00
else
return false
2014-01-25 15:53:48 +00:00
end
end
2014-01-30 22:08:38 +00:00
if !@config.sync_host.nil? && !@config.sync_port.nil?
if !@sync.connect(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #1: #{@sync.error_msg}"
2014-01-23 22:04:13 +00:00
end
2014-01-14 22:00:52 +00:00
end
2014-01-26 11:13:32 +00:00
2014-01-30 22:08:38 +00:00
if @sync.enable
if !@mpw.sync(@sync.get(@passwd), @config.last_update)
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #2: #{@mpw.error_msg}" if !@mpw.error_msg.nil?
puts "#{I18n.t('display.error')} #3: #{@sync.error_msg}" if !@sync.error_msg.nil?
2014-03-16 14:29:21 +00:00
elsif !@sync.update(File.open(@config.file_gpg).read)
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #4: #{@sync.error_msg}"
2014-01-30 22:08:38 +00:00
elsif !@config.set_last_update
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #5: #{@config.error_msg}"
2014-03-16 13:44:26 +00:00
elsif !@mpw.encrypt
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #6: #{@mpw.error_msg}"
2014-01-30 22:08:38 +00:00
else
return true
end
end
rescue Exception => e
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #7: #{e}"
2014-03-16 13:44:26 +00:00
puts @sync.error_msg if @sync.error_msg.nil?
puts @config.error_msg if @config.error_msg.nil?
puts @mpw.error_msg if @mpw.error_msg.nil?
2014-01-30 22:08:38 +00:00
else
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')
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-08-31 10:29:57 +00:00
file_gpg = ask(I18n.t('form.setup.gpg_file', home: @conf.dir_home)).to_s
2014-01-26 10:39:53 +00:00
timeout_pwd = ask(I18n.t('form.setup.timeout')).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-03-16 20:28:23 +00:00
if language.nil? || language.empty?
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-02-02 13:20:24 +00:00
sync_type = sync_type.nil? || sync_type.empty? ? nil : sync_type
sync_host = sync_host.nil? || sync_host.empty? ? nil : sync_host
sync_port = sync_port.nil? || sync_port.empty? ? nil : sync_port.to_i
sync_user = sync_user.nil? || sync_user.empty? ? nil : sync_user
sync_pwd = sync_pwd.nil? || sync_pwd.empty? ? nil : sync_pwd
sync_path = sync_path.nil? || sync_path.empty? ? nil : sync_path
2014-01-25 15:53:48 +00:00
2014-02-02 13:20:24 +00:00
if @config.setup(key, share_keys, language, file_gpg, timeout_pwd, sync_type, sync_host, sync_port, sync_user, sync_pwd, sync_path)
2014-01-26 10:39:53 +00:00
puts I18n.t('form.setup.valid')
2013-07-17 20:31:28 +00:00
else
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #8: #{@config.error_msg}"
2014-02-02 13:20:24 +00:00
exit 2
2013-07-16 20:36:41 +00:00
end
2014-02-02 09:16:10 +00:00
if not @config.checkconfig
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #9: #{@config.error_msg}"
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
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
if !['Y', 'y', 'O', 'o'].include?(ask)
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
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
password = password.to_s
2014-03-16 20:28:23 +00:00
length = length.nil? || length.empty? ? 2048 : length.to_i
expire = expire.nil? || expire.empty? ? 0 : expire.to_i
puts I18n.t('form.setup_gpg_key.wait')
if @config.setup_gpg_key(password, name, length, expire)
puts I18n.t('form.setup_gpg_key.valid')
else
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #10: #{@config.error_msg}"
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-01-25 15:53:48 +00:00
if !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
2014-01-26 10:39:53 +00:00
@passwd = ask(I18n.t('display.gpg_password')) {|q| q.echo = false}
2014-01-25 15:53:48 +00:00
if !@mpw.decrypt(@passwd)
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #11: #{@mpw.error_msg}"
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
2013-09-11 18:47:51 +00:00
def display(search, protocol=nil, group=nil, format=nil)
2014-01-11 23:37:46 +00:00
result = @mpw.search(search, group, protocol)
2013-07-16 20:36:41 +00:00
2013-07-17 20:31:28 +00:00
if not result.empty?
result.each do |r|
if format.nil? || !format
2014-01-15 19:01:23 +00:00
displayFormat(r)
else
2014-01-15 19:01:23 +00:00
displayFormatAlt(r)
end
2013-07-17 20:31:28 +00:00
end
else
2014-01-26 10:39:53 +00:00
puts I18n.t('display.nothing')
2013-07-16 20:36:41 +00:00
end
end
# Display an item in the default format
2013-09-02 17:14:33 +00:00
# @args: item -> an array with the item information
def displayFormat(item)
puts '--------------------'
2014-08-31 10:28:20 +00:00
puts "Id: #{item[:id]}"
puts "#{I18n.t('display.name')}: #{item[:name]}"
puts "#{I18n.t('display.group')}: #{item[:group]}"
puts "#{I18n.t('display.server')}: #{item[:host]}"
puts "#{I18n.t('display.protocol')}: #{item[:protocol]}"
puts "#{I18n.t('display.login')}: #{item[:login]}"
puts "#{I18n.t('display.password')}: #{item[:password]}"
puts "#{I18n.t('display.port')}: #{item[:port]}"
puts "#{I18n.t('display.comment')}: #{item[:comment]}"
2013-09-02 17:14:33 +00:00
end
# Display an item in the alternative format
# @args: item -> an array with the item information
def displayFormatAlt(item)
2014-08-31 10:28:20 +00:00
port = item[:port].nil? ? '' : ":#{item[:port]}"
2014-08-31 10:28:20 +00:00
if item[:password].nil? || item[:password].empty?
if item[:login].include('@')
puts "# #{item[:id]} #{item[:protocol]}://#{item[:login]}@#{item[:host]}#{port}"
else
2014-08-31 10:28:20 +00:00
puts "# #{item[:id]} #{item[:protocol]}://{#{item[:login]}}@#{item[:host]}#{port}"
end
else
2014-08-31 10:28:20 +00:00
puts "# #{item[:id]} #{item[:protocol]}://{#{item[:login]}:#{item[:password]}}@#{item[:host]}#{port}"
end
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
2014-01-31 23:09:22 +00:00
row = []
2014-01-26 10:39:53 +00:00
puts I18n.t('form.add.title')
puts '--------------------'
2014-01-26 10:39:53 +00:00
name = ask(I18n.t('form.add.name')).to_s
group = ask(I18n.t('form.add.group')).to_s
server = ask(I18n.t('form.add.server')).to_s
protocol = ask(I18n.t('form.add.protocol')).to_s
login = ask(I18n.t('form.add.login')).to_s
passwd = ask(I18n.t('form.add.password')).to_s
port = ask(I18n.t('form.add.port')).to_s
comment = ask(I18n.t('form.add.comment')).to_s
2013-07-17 20:31:28 +00:00
2014-01-15 19:01:23 +00:00
if @mpw.update(name, group, server, protocol, login, passwd, port, comment)
2014-02-02 09:16:10 +00:00
if @mpw.encrypt
sync
2014-01-26 10:39:53 +00:00
puts I18n.t('form.add.valid')
else
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #12: #{@mpw.error_msg}"
end
2013-07-17 20:31:28 +00:00
else
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #13: #{@mpw.error_msg}"
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)
2014-01-26 14:09:48 +00:00
row = @mpw.search_by_id(id)
2013-07-17 20:31:28 +00:00
if not row.empty?
2014-01-26 10:39:53 +00:00
puts I18n.t('form.update.title')
puts '--------------------'
2014-08-31 10:28:20 +00:00
name = ask(I18n.t('form.update.name' , name: row[:name])).to_s
group = ask(I18n.t('form.update.group' , group: row[:group])).to_s
server = ask(I18n.t('form.update.server' , server: row[:host])).to_s
protocol = ask(I18n.t('form.update.protocol', protocol: row[:protocol])).to_s
login = ask(I18n.t('form.update.login' , login: row[:login])).to_s
2014-01-26 10:39:53 +00:00
passwd = ask(I18n.t('form.update.password')).to_s
2014-08-31 10:28:20 +00:00
port = ask(I18n.t('form.update.port' , port: row[:port])).to_s
comment = ask(I18n.t('form.update.comment' , comment: row[:comment])).to_s
2013-07-17 20:31:28 +00:00
2014-01-15 19:01:23 +00:00
if @mpw.update(name, group, server, protocol, login, passwd, port, comment, id)
2014-02-02 09:16:10 +00:00
if @mpw.encrypt
sync
2014-01-26 10:39:53 +00:00
puts I18n.t('form.update.valid')
2013-07-17 20:31:28 +00:00
else
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #14: #{@mpw.error_msg}"
2013-07-17 20:31:28 +00:00
end
else
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #15: #{@mpw.error_msg}"
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
def remove(id, force=false)
if not force
2014-01-26 14:09:48 +00:00
result = @mpw.search_by_id(id)
2013-08-26 19:23:35 +00:00
if result.length > 0
2014-01-15 19:01:23 +00:00
displayFormat(result)
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
if confirm =~ /^(y|yes|YES|Yes|Y)$/
force = true
end
else
2014-01-26 10:39:53 +00:00
puts I18n.t('display.nothing')
end
end
if force
2014-01-11 23:37:46 +00:00
if @mpw.remove(id)
2014-02-02 09:16:10 +00:00
if @mpw.encrypt
sync
2014-08-31 10:29:57 +00:00
puts I18n.t('form.delete.valid', id: id)
else
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #16: #{@mpw.error_msg}"
end
2013-07-17 20:31:28 +00:00
else
2014-01-26 10:39:53 +00:00
puts I18n.t('form.delete.not_valid')
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
2013-07-25 17:51:43 +00:00
def export(file)
2014-01-11 23:37:46 +00:00
if @mpw.export(file)
2013-07-25 17:51:43 +00:00
puts "The export in #{file} is succesfull!"
else
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #17: #{@mpw.error_msg}"
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
def import(file, force=false)
2014-01-26 14:09:48 +00:00
result = @mpw.import_preview(file)
2013-08-26 20:19:37 +00:00
if not force
if result.is_a?(Array) && !result.empty?
result.each do |r|
2014-01-15 19:01:23 +00:00
displayFormat(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-02-02 09:16:10 +00:00
if @mpw.import(file) && @mpw.encrypt
sync
2014-01-26 10:39:53 +00:00
puts I18n.t('form.import.valid')
2013-07-25 17:51:43 +00:00
else
2014-04-27 16:31:50 +00:00
puts "#{I18n.t('display.error')} #18: #{@mpw.error_msg}"
2013-07-25 17:51:43 +00:00
end
end
end
2013-09-08 15:56:10 +00:00
# Interactive mode
2014-02-02 09:16:10 +00:00
def interactive
2013-09-09 19:48:42 +00:00
group = nil
2013-09-08 18:04:31 +00:00
last_access = Time.now.to_i
2013-12-25 17:51:41 +00:00
while buf = Readline.readline('<mpw> ', true)
2013-12-20 16:38:58 +00:00
2014-01-11 23:37:46 +00:00
if @config.timeout_pwd < Time.now.to_i - last_access
2014-01-26 10:39:53 +00:00
passwd_confirm = ask(I18n.t('interactive.ask_password')) {|q| q.echo = false}
2013-09-08 18:04:31 +00:00
if @passwd.eql?(passwd_confirm)
last_access = Time.now.to_i
else
2014-01-26 10:39:53 +00:00
puts I18n.t('interactive.bad_password')
2013-09-08 18:04:31 +00:00
next
end
else
last_access = Time.now.to_i
end
2013-12-20 16:38:58 +00:00
command = buf.split(' ')
2013-09-08 15:56:10 +00:00
case command[0]
when 'display', 'show', 'd', 's'
if !command[1].nil? && !command[1].empty?
2014-01-15 19:01:23 +00:00
display(command[1], group, command[2])
2013-09-08 15:56:10 +00:00
end
when 'add', 'a'
2014-02-02 09:16:10 +00:00
add
2013-09-08 15:56:10 +00:00
when 'update', 'u'
if !command[1].nil? && !command[1].empty?
2014-01-15 19:01:23 +00:00
update(command[1])
2013-09-08 15:56:10 +00:00
end
when 'remove', 'delete', 'r', 'd'
if !command[1].nil? && !command[1].empty?
2014-01-15 19:01:23 +00:00
remove(command[1])
2013-09-08 15:56:10 +00:00
end
2013-09-09 19:48:42 +00:00
when 'group', 'g'
if !command[1].nil? && !command[1].empty?
group = command[1]
else
group = nil
end
2013-09-08 15:56:10 +00:00
when 'help', 'h', '?'
2014-01-26 10:39:53 +00:00
puts I18n.t('interactive.option.title')
puts '--------------------'
2014-01-26 10:39:53 +00:00
puts "display, show, d, s SEARCH #{I18n.t('interactive.option.show')}"
puts "group, g #{I18n.t('interactive.option.group')}"
puts "add, a #{I18n.t('interactive.option.add')}"
puts "update, u ID #{I18n.t('interactive.option.update')}"
puts "remove, delete, r, d ID #{I18n.t('interactive.option.remove')}"
puts "help, h, ? #{I18n.t('interactive.option.help')}"
puts "quit, exit, q #{I18n.t('interactive.option.quit')}"
2013-09-08 15:56:10 +00:00
when 'quit', 'exit', 'q'
2014-01-26 10:39:53 +00:00
puts I18n.t('interactive.goodbye')
2013-09-08 15:56:10 +00:00
break
2013-09-09 19:48:42 +00:00
else
2013-09-09 20:45:14 +00:00
if !command[0].nil? && !command[0].empty?
2014-01-26 10:39:53 +00:00
puts I18n.t('interactive.unknown_command')
2013-09-09 19:48:42 +00:00
end
2013-09-08 15:56:10 +00:00
end
end
end
2013-07-16 20:36:41 +00:00
end