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
|
|
|
|
2013-09-04 17:48:06 +00:00
|
|
|
require "#{APP_ROOT}/lib/MPW.rb"
|
2014-01-11 23:37:46 +00:00
|
|
|
require "#{APP_ROOT}/lib/MPWConfig.rb"
|
2014-01-14 22:00:52 +00:00
|
|
|
require "#{APP_ROOT}/lib/Sync.rb"
|
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
|
2013-12-30 19:15:00 +00:00
|
|
|
def initialize(lang, config_file=nil)
|
2014-01-11 23:37:46 +00:00
|
|
|
@config = MPWConfig.new(config_file)
|
2013-07-17 20:31:28 +00:00
|
|
|
|
2014-01-11 23:37:46 +00:00
|
|
|
if not @config.checkconfig()
|
2014-01-15 19:01:23 +00:00
|
|
|
setup(lang)
|
2013-07-17 20:31:28 +00:00
|
|
|
end
|
|
|
|
|
2014-01-11 23:37:46 +00:00
|
|
|
@mpw = MPW.new(@config.file_gpg, @config.key)
|
2014-01-15 19:01:23 +00:00
|
|
|
if not decrypt()
|
2014-01-11 23:37:46 +00:00
|
|
|
puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
|
2013-07-17 20:31:28 +00:00
|
|
|
exit 2
|
2013-07-16 20:36:41 +00:00
|
|
|
end
|
2014-01-14 22:00:52 +00:00
|
|
|
|
|
|
|
@sync = Sync.new()
|
|
|
|
if @config.sync_host.nil? || @config.sync_port.nil?
|
|
|
|
@sync.disable()
|
|
|
|
elsif !@sync.connect(@config.sync_host, @config.sync_port, @config.key, @config.sync_pwd, @config.sync_suffix)
|
|
|
|
puts "#{I18n.t('cli.sync.not_connect')}:\n#{@sync.error_msg}"
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
@mpw.sync(@sync.get(@passwd), @config.last_update)
|
|
|
|
@sync.update(File.open(@config.file_gpg).read)
|
|
|
|
@config.setLastUpdate()
|
|
|
|
rescue Exception => e
|
|
|
|
puts "#{I18n.t('cli.sync.error')}:\n#{e}"
|
|
|
|
else
|
|
|
|
@sync.close()
|
|
|
|
end
|
|
|
|
end
|
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)
|
2013-12-30 18:47:47 +00:00
|
|
|
puts I18n.t('cli.form.setup.title')
|
|
|
|
puts '--------------------'
|
|
|
|
language = ask(I18n.t('cli.form.setup.lang', :lang => lang)).to_s
|
|
|
|
key = ask(I18n.t('cli.form.setup.gpg_key')).to_s
|
|
|
|
file_gpg = ask(I18n.t('cli.form.setup.gpg_file', :home => Dir.home())).to_s
|
|
|
|
timeout_pwd = ask(I18n.t('cli.form.setup.timeout')).to_s
|
2013-07-16 20:36:41 +00:00
|
|
|
|
2013-12-25 17:51:41 +00:00
|
|
|
if !File.exist?("#{APP_ROOT}/i18n/#{language}.yml")
|
2013-12-26 09:47:54 +00:00
|
|
|
language= 'en'
|
2013-12-25 17:51:41 +00:00
|
|
|
end
|
|
|
|
I18n.locale = language.to_sym
|
|
|
|
|
2014-01-11 23:37:46 +00:00
|
|
|
if @config.setup(key, language, file_gpg, timeout_pwd)
|
2013-12-25 17:51:41 +00:00
|
|
|
puts I18n.t('cli.form.setup.valid')
|
2013-07-17 20:31:28 +00:00
|
|
|
else
|
2014-01-11 23:37:46 +00:00
|
|
|
puts "#{I18n.t('cli.display.error')}: #{@config.error_msg}"
|
2013-07-16 20:36:41 +00:00
|
|
|
end
|
2014-01-03 13:31:43 +00:00
|
|
|
|
2014-01-11 23:37:46 +00:00
|
|
|
if not @config.checkconfig()
|
|
|
|
puts "#{I18n.t('cli.display.error')}: #{@config.error_msg}"
|
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
|
|
|
|
2013-08-25 08:07:39 +00:00
|
|
|
# Request the GPG password and decrypt the file
|
2013-07-17 20:31:28 +00:00
|
|
|
def decrypt()
|
2013-12-25 17:51:41 +00:00
|
|
|
@passwd = ask(I18n.t('cli.display.gpg_password')) {|q| q.echo = false}
|
2014-01-11 23:37:46 +00:00
|
|
|
return @mpw.decrypt(@passwd)
|
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|
|
2013-09-02 17:50:40 +00:00
|
|
|
if format.nil? || !format
|
2014-01-15 19:01:23 +00:00
|
|
|
displayFormat(r)
|
2013-09-02 17:50:40 +00:00
|
|
|
else
|
2014-01-15 19:01:23 +00:00
|
|
|
displayFormatAlt(r)
|
2013-09-02 17:50:40 +00:00
|
|
|
end
|
2013-07-17 20:31:28 +00:00
|
|
|
end
|
|
|
|
else
|
2013-12-25 17:51:41 +00:00
|
|
|
puts I18n.t('cli.display.nothing')
|
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
|
|
|
|
def displayFormat(item)
|
2013-12-30 18:47:47 +00:00
|
|
|
puts '--------------------'
|
|
|
|
puts "Id: #{item[MPW::ID]}"
|
|
|
|
puts "#{I18n.t('cli.display.name')}: #{item[MPW::NAME]}"
|
|
|
|
puts "#{I18n.t('cli.display.group')}: #{item[MPW::GROUP]}"
|
|
|
|
puts "#{I18n.t('cli.display.server')}: #{item[MPW::SERVER]}"
|
|
|
|
puts "#{I18n.t('cli.display.protocol')}: #{item[MPW::PROTOCOL]}"
|
|
|
|
puts "#{I18n.t('cli.display.login')}: #{item[MPW::LOGIN]}"
|
|
|
|
puts "#{I18n.t('cli.display.password')}: #{item[MPW::PASSWORD]}"
|
|
|
|
puts "#{I18n.t('cli.display.port')}: #{item[MPW::PORT]}"
|
|
|
|
puts "#{I18n.t('cli.display.comment')}: #{item[MPW::COMMENT]}"
|
2013-09-02 17:14:33 +00:00
|
|
|
end
|
|
|
|
|
2013-09-02 17:50:40 +00:00
|
|
|
# Display an item in the alternative format
|
|
|
|
# @args: item -> an array with the item information
|
|
|
|
def displayFormatAlt(item)
|
|
|
|
item[MPW::PORT].nil? ? (port = '') : (port = ":#{item[MPW::PORT]}")
|
|
|
|
|
|
|
|
if item[MPW::PASSWORD].nil? || item[MPW::PASSWORD].empty?
|
2013-09-04 17:48:06 +00:00
|
|
|
if item[MPW::LOGIN].include('@')
|
2013-09-02 17:50:40 +00:00
|
|
|
puts "# #{item[MPW::ID]} #{item[MPW::PROTOCOL]}://#{item[MPW::LOGIN]}@#{item[MPW::SERVER]}#{port}"
|
|
|
|
else
|
|
|
|
puts "# #{item[MPW::ID]} #{item[MPW::PROTOCOL]}://{#{item[MPW::LOGIN]}}@#{item[MPW::SERVER]}#{port}"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
puts "# #{item[MPW::ID]} #{item[MPW::PROTOCOL]}://{#{item[MPW::LOGIN]}:#{item[MPW::PASSWORD]}}@#{item[MPW::SERVER]}#{port}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-25 08:07:39 +00:00
|
|
|
# Form to add a new item
|
2013-07-17 20:31:28 +00:00
|
|
|
def add()
|
|
|
|
row = Array.new()
|
2013-12-30 18:47:47 +00:00
|
|
|
puts I18n.t('cli.form.add.title')
|
|
|
|
puts '--------------------'
|
|
|
|
name = ask(I18n.t('cli.form.add.name')).to_s
|
|
|
|
group = ask(I18n.t('cli.form.add.group')).to_s
|
|
|
|
server = ask(I18n.t('cli.form.add.server')).to_s
|
|
|
|
protocol = ask(I18n.t('cli.form.add.protocol')).to_s
|
|
|
|
login = ask(I18n.t('cli.form.add.login')).to_s
|
|
|
|
passwd = ask(I18n.t('cli.form.add.password')).to_s
|
|
|
|
port = ask(I18n.t('cli.form.add.port')).to_s
|
|
|
|
comment = ask(I18n.t('cli.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-01-11 23:37:46 +00:00
|
|
|
if @mpw.encrypt()
|
2013-12-25 17:51:41 +00:00
|
|
|
puts I18n.t('cli.form.add.valid')
|
2013-08-26 18:52:18 +00:00
|
|
|
else
|
2014-01-11 23:37:46 +00:00
|
|
|
puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
|
2013-08-26 18:52:18 +00:00
|
|
|
end
|
2013-07-17 20:31:28 +00:00
|
|
|
else
|
2014-01-11 23:37:46 +00:00
|
|
|
puts "#{I18n.t('cli.display.error')}: #{@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-11 23:37:46 +00:00
|
|
|
row = @mpw.searchById(id)
|
2013-07-17 20:31:28 +00:00
|
|
|
|
|
|
|
if not row.empty?
|
2013-12-30 18:47:47 +00:00
|
|
|
puts I18n.t('cli.form.update.title')
|
|
|
|
puts '--------------------'
|
|
|
|
name = ask(I18n.t('cli.form.update.name' , :name => row[MPW::NAME])).to_s
|
|
|
|
group = ask(I18n.t('cli.form.update.group' , :group => row[MPW::GROUP])).to_s
|
|
|
|
server = ask(I18n.t('cli.form.update.server' , :server => row[MPW::SERVER])).to_s
|
|
|
|
protocol = ask(I18n.t('cli.form.update.protocol', :protocol => row[MPW::PROTOCOL])).to_s
|
|
|
|
login = ask(I18n.t('cli.form.update.login' , :login => row[MPW::LOGIN])).to_s
|
|
|
|
passwd = ask(I18n.t('cli.form.update.password')).to_s
|
|
|
|
port = ask(I18n.t('cli.form.update.port' , :port => row[MPW::PORT])).to_s
|
|
|
|
comment = ask(I18n.t('cli.form.update.comment' , :comment => row[MPW::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-01-11 23:37:46 +00:00
|
|
|
if @mpw.encrypt()
|
2013-12-25 17:51:41 +00:00
|
|
|
puts I18n.t('cli.form.update.valid')
|
2013-07-17 20:31:28 +00:00
|
|
|
else
|
2014-01-11 23:37:46 +00:00
|
|
|
puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
|
2013-07-17 20:31:28 +00:00
|
|
|
end
|
|
|
|
else
|
2014-01-11 23:37:46 +00:00
|
|
|
puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
|
2013-07-16 20:36:41 +00:00
|
|
|
end
|
2013-07-17 20:31:28 +00:00
|
|
|
else
|
2013-12-25 17:51:41 +00:00
|
|
|
puts I18n.t('cli.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
|
2013-07-17 21:19:35 +00:00
|
|
|
def remove(id, force=false)
|
|
|
|
if not force
|
2014-01-11 23:37:46 +00:00
|
|
|
result = @mpw.searchById(id)
|
2013-08-26 19:23:35 +00:00
|
|
|
|
2013-09-08 16:02:10 +00:00
|
|
|
if result.length > 0
|
2014-01-15 19:01:23 +00:00
|
|
|
displayFormat(result)
|
2013-08-26 19:23:35 +00:00
|
|
|
|
2013-12-30 18:47:47 +00:00
|
|
|
confirm = ask("#{I18n.t('cli.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
|
2013-12-25 17:51:41 +00:00
|
|
|
puts I18n.t('cli.display.nothing')
|
2013-07-17 21:19:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if force
|
2014-01-11 23:37:46 +00:00
|
|
|
if @mpw.remove(id)
|
|
|
|
if @mpw.encrypt()
|
2013-12-25 17:51:41 +00:00
|
|
|
puts I18n.t('cli.form.delete.valid', :id => id)
|
2013-07-17 21:19:35 +00:00
|
|
|
else
|
2014-01-11 23:37:46 +00:00
|
|
|
puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
|
2013-07-17 21:19:35 +00:00
|
|
|
end
|
2013-07-17 20:31:28 +00:00
|
|
|
else
|
2013-12-25 17:51:41 +00:00
|
|
|
puts I18n.t('cli.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-01-11 23:37:46 +00:00
|
|
|
puts "#{I18n.t('cli.display.error')}: #{@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-11 23:37:46 +00:00
|
|
|
result = @mpw.importPreview(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
|
|
|
|
|
2013-12-30 18:47:47 +00:00
|
|
|
confirm = ask("#{I18n.t('cli.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
|
2013-12-25 17:51:41 +00:00
|
|
|
puts I18n.t('cli.form.import.not_valid')
|
2013-08-26 20:19:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if force
|
2014-01-11 23:37:46 +00:00
|
|
|
if @mpw.import(file) && @mpw.encrypt()
|
2013-12-25 17:51:41 +00:00
|
|
|
puts I18n.t('cli.form.import.valid')
|
2013-07-25 17:51:43 +00:00
|
|
|
else
|
2014-01-11 23:37:46 +00:00
|
|
|
puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
|
2013-07-25 17:51:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-08 15:56:10 +00:00
|
|
|
# Interactive mode
|
|
|
|
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
|
2013-12-25 17:51:41 +00:00
|
|
|
passwd_confirm = ask(I18n.t('cli.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
|
2013-12-25 17:51:41 +00:00
|
|
|
puts I18n.t('cli.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'
|
2013-09-08 18:04:31 +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', '?'
|
2013-12-30 18:47:47 +00:00
|
|
|
puts I18n.t('cli.interactive.option.title')
|
|
|
|
puts '--------------------'
|
|
|
|
puts "display, show, d, s SEARCH #{I18n.t('cli.interactive.option.show')}"
|
|
|
|
puts "group, g #{I18n.t('cli.interactive.option.group')}"
|
|
|
|
puts "add, a #{I18n.t('cli.interactive.option.add')}"
|
|
|
|
puts "update, u ID #{I18n.t('cli.interactive.option.update')}"
|
|
|
|
puts "remove, delete, r, d ID #{I18n.t('cli.interactive.option.remove')}"
|
|
|
|
puts "help, h, ? #{I18n.t('cli.interactive.option.help')}"
|
|
|
|
puts "quit, exit, q #{I18n.t('cli.interactive.option.quit')}"
|
2013-09-08 15:56:10 +00:00
|
|
|
when 'quit', 'exit', 'q'
|
2013-12-25 17:51:41 +00:00
|
|
|
puts I18n.t('cli.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?
|
2013-12-25 17:51:41 +00:00
|
|
|
puts I18n.t('cli.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
|