1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-23 13:57:52 +00:00

recode for multiple protocol to sync

This commit is contained in:
nishiki 2014-01-30 23:08:38 +01:00
parent 8ac531ffed
commit 958792801e
5 changed files with 183 additions and 168 deletions

View file

@ -18,10 +18,12 @@ module MPW
attr_accessor :file_gpg attr_accessor :file_gpg
attr_accessor :timeout_pwd attr_accessor :timeout_pwd
attr_accessor :last_update attr_accessor :last_update
attr_accessor :sync_type
attr_accessor :sync_host attr_accessor :sync_host
attr_accessor :sync_port attr_accessor :sync_port
attr_accessor :sync_user
attr_accessor :sync_pwd attr_accessor :sync_pwd
attr_accessor :sync_suffix attr_accessor :sync_path
attr_accessor :last_update attr_accessor :last_update
# Constructor # Constructor
@ -40,12 +42,14 @@ module MPW
# lang -> the software language # lang -> the software language
# file_gpg -> the file who is encrypted # file_gpg -> the file who is encrypted
# timeout_pwd -> time to save the password # timeout_pwd -> time to save the password
# sync_type -> the type to synchronization
# sync_host -> the server host for synchronization # sync_host -> the server host for synchronization
# sync_port -> the server port for synchronization # sync_port -> the server port for synchronization
# sync_user -> the user for synchronization
# sync_pwd -> the password for synchronization # sync_pwd -> the password for synchronization
# sync_suffix -> the suffix file (optionnal) # sync_suffix -> the suffix file (optionnal)
# @rtrn: true if le config file is create # @rtrn: true if le config file is create
def setup(key, lang, file_gpg, timeout_pwd, sync_host=nil, sync_port=nil, sync_pwd=nil, sync_suffix=nil) def setup(key, lang, file_gpg, timeout_pwd, sync_type=nil, sync_host=nil, sync_port=nil, sync_user=nil, sync_pwd=nil, sync_path=nil)
if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/ if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/
@error_msg = I18n.t('error.config.key_bad_format') @error_msg = I18n.t('error.config.key_bad_format')
@ -62,37 +66,38 @@ module MPW
'lang' => lang, 'lang' => lang,
'file_gpg' => file_gpg, 'file_gpg' => file_gpg,
'timeout_pwd' => timeout_pwd, 'timeout_pwd' => timeout_pwd,
'sync_type' => sync_type,
'sync_host' => sync_host, 'sync_host' => sync_host,
'sync_port' => sync_port, 'sync_port' => sync_port,
'sync_user' => sync_user,
'sync_pwd' => sync_pwd, 'sync_pwd' => sync_pwd,
'sync_suffix' => sync_suffix, 'sync_path' => sync_path,
'last_update' => 0 }} 'last_update' => 0 }}
begin
File.open(@file_config, 'w') do |file| File.open(@file_config, 'w') do |file|
file << config.to_yaml file << config.to_yaml
end end
return true
rescue Exception => e rescue Exception => e
@error_msg = "#{I18n.t('error.config.write')}\n#{e}" @error_msg = "#{I18n.t('error.config.write')}\n#{e}"
return false return false
end end
return true
end
# Check the config file # Check the config file
# @rtrn: true if the config file is correct # @rtrn: true if the config file is correct
def checkconfig() def checkconfig()
begin
config = YAML::load_file(@file_config) config = YAML::load_file(@file_config)
@key = config['config']['key'] @key = config['config']['key']
@lang = config['config']['lang'] @lang = config['config']['lang']
@file_gpg = config['config']['file_gpg'] @file_gpg = config['config']['file_gpg']
@timeout_pwd = config['config']['timeout_pwd'].to_i @timeout_pwd = config['config']['timeout_pwd'].to_i
@sync_type = config['config']['sync_type']
@sync_host = config['config']['sync_host'] @sync_host = config['config']['sync_host']
@sync_port = config['config']['sync_port'] @sync_port = config['config']['sync_port']
@sync_user = config['config']['sync_user']
@sync_pwd = config['config']['sync_pwd'] @sync_pwd = config['config']['sync_pwd']
@sync_suffix = config['config']['sync_suffix'] @sync_path = config['config']['sync_path']
@last_update = config['config']['last_update'].to_i @last_update = config['config']['last_update'].to_i
if @key.empty? || @file_gpg.empty? if @key.empty? || @file_gpg.empty?
@ -102,39 +107,37 @@ module MPW
I18n.locale = @lang.to_sym I18n.locale = @lang.to_sym
return true
rescue Exception => e rescue Exception => e
@error_msg = "#{I18n.t('error.config.check')}\n#{e}" @error_msg = "#{I18n.t('error.config.check')}\n#{e}"
return false return false
end end
return true
end
# Set the last update when there is a sync # Set the last update when there is a sync
# @rtrn: true is the file has been updated # @rtrn: true is the file has been updated
def set_last_update() def set_last_update
config = {'config' => {'key' => @key, config = {'config' => {'key' => @key,
'lang' => @lang, 'lang' => @lang,
'file_gpg' => @file_gpg, 'file_gpg' => @file_gpg,
'timeout_pwd' => @timeout_pwd, 'timeout_pwd' => @timeout_pwd,
'sync_type' => @sync_type,
'sync_host' => @sync_host, 'sync_host' => @sync_host,
'sync_port' => @sync_port, 'sync_port' => @sync_port,
'sync_user' => @sync_user,
'sync_pwd' => @sync_pwd, 'sync_pwd' => @sync_pwd,
'sync_suffix' => @sync_suffix, 'sync_path' => @sync_path,
'last_update' => Time.now.to_i }} 'last_update' => Time.now.to_i }}
begin
File.open(@file_config, 'w') do |file| File.open(@file_config, 'w') do |file|
file << config.to_yaml file << config.to_yaml
end end
return true
rescue Exception => e rescue Exception => e
@error_msg = "#{I18n.t('error.config.write')}\n#{e}" @error_msg = "#{I18n.t('error.config.write')}\n#{e}"
return false return false
end end
return true
end
end end
end end

View file

@ -36,9 +36,8 @@ module MPW
# @args: password -> the GPG key password # @args: password -> the GPG key password
# @rtrn: true if data has been decrypted # @rtrn: true if data has been decrypted
def decrypt(passwd=nil) def decrypt(passwd=nil)
@data = Array.new @data = []
begin
if File.exist?(@file_gpg) if File.exist?(@file_gpg)
crypto = GPGME::Crypto.new(:armor => true) crypto = GPGME::Crypto.new(:armor => true)
data_decrypt = crypto.decrypt(IO.read(@file_gpg), :password => passwd).read data_decrypt = crypto.decrypt(IO.read(@file_gpg), :password => passwd).read
@ -53,12 +52,10 @@ module MPW
@error_msg = "#{I18n.t('error.gpg_file.decrypt')}\n#{e}" @error_msg = "#{I18n.t('error.gpg_file.decrypt')}\n#{e}"
return false return false
end end
end
# Encrypt a file # Encrypt a file
# @rtrn: true if the file has been encrypted # @rtrn: true if the file has been encrypted
def encrypt() def encrypt
begin
crypto = GPGME::Crypto.new(:armor => true) crypto = GPGME::Crypto.new(:armor => true)
file_gpg = File.open(@file_gpg, 'w+') file_gpg = File.open(@file_gpg, 'w+')
@ -75,7 +72,6 @@ module MPW
@error_msg = "#{I18n.t('error.gpg_file.encrypt')}\n#{e}" @error_msg = "#{I18n.t('error.gpg_file.encrypt')}\n#{e}"
return false return false
end end
end
# Search in some csv data # Search in some csv data
# @args: search -> the string to search # @args: search -> the string to search
@ -159,13 +155,13 @@ module MPW
row_update[PORT] = port.nil? || port.empty? ? row[PORT] : port row_update[PORT] = port.nil? || port.empty? ? row[PORT] : port
row_update[COMMENT] = comment.nil? || comment.empty? ? row[COMMENT] : comment row_update[COMMENT] = comment.nil? || comment.empty? ? row[COMMENT] : comment
row_update[] = row_update[NAME].nil? ? nil : row_update[NAME].force_encoding('ASCII-8BIT') row_update[NAME] = row_update[NAME].nil? ? nil : row_update[NAME].force_encoding('ASCII-8BIT')
row_update[] = row_update[GROUP].nil? ? nil : row_update[GROUP].force_encoding('ASCII-8BIT') row_update[GROUP] = row_update[GROUP].nil? ? nil : row_update[GROUP].force_encoding('ASCII-8BIT')
row_update[] = row_update[SERVER].nil? ? nil : row_update[SERVER].force_encoding('ASCII-8BIT') row_update[SERVER] = row_update[SERVER].nil? ? nil : row_update[SERVER].force_encoding('ASCII-8BIT')
row_update[] = row_update[PROTOCOL].nil? ? nil : row_update[PROTOCOL].force_encoding('ASCII-8BIT') row_update[PROTOCOL] = row_update[PROTOCOL].nil? ? nil : row_update[PROTOCOL].force_encoding('ASCII-8BIT')
row_update[] = row_update[LOGIN].nil? ? nil : row_update[LOGIN].force_encoding('ASCII-8BIT') row_update[LOGIN] = row_update[LOGIN].nil? ? nil : row_update[LOGIN].force_encoding('ASCII-8BIT')
row_update[] = row_update[PASSWORD].nil? ? nil : row_update[PASSWORD].force_encoding('ASCII-8BIT') row_update[PASSWORD] = row_update[PASSWORD].nil? ? nil : row_update[PASSWORD].force_encoding('ASCII-8BIT')
row_update[] = row_update[COMMENT].nil? ? nil : row_update[COMMENT].force_encoding('ASCII-8BIT') row_update[COMMENT] = row_update[COMMENT].nil? ? nil : row_update[COMMENT].force_encoding('ASCII-8BIT')
if row_update[NAME].nil? || row_update[NAME].empty? if row_update[NAME].nil? || row_update[NAME].empty?
@error_msg = I18n.t('error.update.name_empty') @error_msg = I18n.t('error.update.name_empty')
@ -202,7 +198,6 @@ module MPW
# @args: file -> a string to match # @args: file -> a string to match
# @rtrn: true if export work # @rtrn: true if export work
def export(file) def export(file)
begin
File.open(file, 'w+') do |file| File.open(file, 'w+') do |file|
@data.each do |row| @data.each do |row|
row.delete_at(ID).delete_at(DATE) row.delete_at(ID).delete_at(DATE)
@ -215,13 +210,11 @@ module MPW
@error_msg = "#{I18n.t('error.export.write', :file => file)}\n#{e}" @error_msg = "#{I18n.t('error.export.write', :file => file)}\n#{e}"
return false return false
end end
end
# Import to csv # Import to csv
# @args: file -> path to file import # @args: file -> path to file import
# @rtrn: true if the import work # @rtrn: true if the import work
def import(file) def import(file)
begin
data_new = IO.read(file) data_new = IO.read(file)
data_new.lines do |line| data_new.lines do |line|
if not line =~ /(.*,){6}/ if not line =~ /(.*,){6}/
@ -240,13 +233,11 @@ module MPW
@error_msg = "#{I18n.t('error.import.read', :file => file)}\n#{e}" @error_msg = "#{I18n.t('error.import.read', :file => file)}\n#{e}"
return false return false
end end
end
# Return a preview import # Return a preview import
# @args: file -> path to file import # @args: file -> path to file import
# @rtrn: an array with the items to import, if there is an error return false # @rtrn: an array with the items to import, if there is an error return false
def import_preview(file) def import_preview(file)
begin
result = Array.new() result = Array.new()
id = 0 id = 0
@ -267,7 +258,6 @@ module MPW
@error_msg = "#{I18n.t('error.import.read', :file => file)}\n#{e}" @error_msg = "#{I18n.t('error.import.read', :file => file)}\n#{e}"
return false return false
end end
end
# Sync remote data and local data # Sync remote data and local data
# @args: data_remote -> array with the data remote # @args: data_remote -> array with the data remote
@ -308,7 +298,7 @@ module MPW
end end
end end
return encrypt() return encrypt
end end
# Generate a random password # Generate a random password

View file

@ -12,7 +12,7 @@ module MPW
require 'socket' require 'socket'
require 'json' require 'json'
class MPW class MPWSync
attr_accessor :error_msg attr_accessor :error_msg
attr_accessor :enable attr_accessor :enable
@ -30,12 +30,12 @@ module MPW
# password -> the remote password # password -> the remote password
# suffix -> the suffix file # suffix -> the suffix file
# @rtrn: false if the connection fail # @rtrn: false if the connection fail
def connect(host, port, gpg_key, password, suffix=nil) def connect(host, user, password, path, port=nil)
@gpg_key = gpg_key @gpg_key = user
@password = password @password = password
@suffix = suffix @suffix = path
@socket = TCPSocket.new(host, port) @socket = TCPSocket.new(host, port.to_i)
@enable = true @enable = true
rescue Exception => e rescue Exception => e
@error_msg = "#{I18n.t('error.sync.connection')}\n#{e}" @error_msg = "#{I18n.t('error.sync.connection')}\n#{e}"
@ -64,24 +64,23 @@ module MPW
@error_msg = I18n.t('error.sync.communication') @error_msg = I18n.t('error.sync.communication')
return nil return nil
elsif msg['error'].nil? elsif msg['error'].nil?
tmp_file = "/tmp/mpw-#{MPW.password()}.gpg" tmp_file = tmpfile
File.open(tmp_file, 'w') do |file| File.open(tmp_file, 'w') do |file|
file << msg['data'] file << msg['data']
end end
@mpw = MPW.new(tmp_file) mpw = MPW.new(tmp_file)
if !@mpw.decrypt(gpg_password) if !mpw.decrypt(gpg_password)
puts @mpw.error_msg @error_msg = mpw.error_msg
return nil return nil
end end
File.unlink(tmp_file) File.unlink(tmp_file)
return @mpw.search() return mpw.search
else else
@error_msg = I18n.t(msg['error']) @error_msg = I18n.t(msg['error'])
return nil return nil
end end
end end
# Update the remote data # Update the remote data
@ -121,6 +120,16 @@ module MPW
send_msg = {:action => 'close'} send_msg = {:action => 'close'}
@socket.puts send_msg.to_json @socket.puts send_msg.to_json
end end
# Generate a random string
# @rtrn: a random string
def tmpfile
result = ''
result << ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(6).join
return "/tmp/mpw-#{result}"
end
end end
end end

View file

@ -41,7 +41,6 @@ module MPW
@error_msg = "#{I18n.t('error.sync.connection')}\n#{e}" @error_msg = "#{I18n.t('error.sync.connection')}\n#{e}"
@enable = false @enable = false
else else
return @enable return @enable
end end
@ -53,7 +52,7 @@ module MPW
return nil return nil
end end
tmp_file = "/tmp/mpw-#{MPW.password()}.gpg" tmp_file = tmpfile
Net::SCP.start(@host, @user, :password => @password, :port => @port) do |ssh| Net::SCP.start(@host, @user, :password => @password, :port => @port) do |ssh|
ssh.scp.download(@path, tmp_file) ssh.scp.download(@path, tmp_file)
end end
@ -62,15 +61,14 @@ module MPW
file << msg['data'] file << msg['data']
end end
@mpw = MPW.new(tmp_file) mpw = MPW.new(tmp_file)
if !@mpw.decrypt(gpg_password) if !mpw.decrypt(gpg_password)
puts @mpw.error_msg @error_msg = mpw.error_msg
return nil return nil
end end
File.unlink(tmp_file) File.unlink(tmp_file)
return mpw.search
return @mpw.search()
rescue Exception => e rescue Exception => e
@error_msg = "#{I18n.t('error.sync.download')}\n#{e}" @error_msg = "#{I18n.t('error.sync.download')}\n#{e}"
return nil return nil
@ -84,7 +82,7 @@ module MPW
return true return true
end end
tmp_file = "/tmp/mpw-#{MPW.password()}.gpg" tmp_file = tmpfile
Net::SCP.start(@host, @user, :password => @password, :port => @port) do |ssh| Net::SCP.start(@host, @user, :password => @password, :port => @port) do |ssh|
ssh.scp.upload(tmp_file, @path) ssh.scp.upload(tmp_file, @path)
end end
@ -100,6 +98,16 @@ module MPW
# Close the connection # Close the connection
def close def close
end end
# Generate a random string
# @rtrn: a random string
def tmpfile
result = ''
result << ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(6).join
return "/tmp/mpw-#{result}"
end
end end
end end

View file

@ -23,30 +23,36 @@ class Cli
end end
# Close sync # Close sync
def sync_close() def sync_close
@sync.close() @sync.close
end end
# Sync the data with the server # Sync the data with the server
# @rtnr: true if the synchro is finish # @rtnr: true if the synchro is finish
def sync() def sync
if !defined?(@sync) if !defined?(@sync)
@sync = MPW::Sync::MPW.new case @config.sync_type
when 'mpw'
@sync = MPW::Sync::MPWSync.new
when 'sftp', 'scp', 'ssh'
@sync = MPW::Sync::SSH.new
else
return false
end
end
if !@config.sync_host.nil? && !@config.sync_port.nil? if !@config.sync_host.nil? && !@config.sync_port.nil?
if !@sync.connect(@config.sync_host, @config.sync_port, @config.key, @config.sync_pwd, @config.sync_suffix) if !@sync.connect(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
puts "#{I18n.t('display.error')}: #{@sync.error_msg}" puts "#{I18n.t('display.error')}: #{@sync.error_msg}"
end end
end end
end
begin
if @sync.enable if @sync.enable
if !@mpw.sync(@sync.get(@passwd), @config.last_update) if !@mpw.sync(@sync.get(@passwd), @config.last_update)
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}" puts "#{I18n.t('display.error')}: #{@sync.error_msg}"
elsif !@sync.update(File.open(@config.file_gpg).read) elsif !@sync.update(File.open(@config.file_gpg).read)
puts "#{I18n.t('display.error')}: #{@sync.error_msg}" puts "#{I18n.t('display.error')}: #{@sync.error_msg}"
elsif !@config.set_last_update() elsif !@config.set_last_update
puts "#{I18n.t('display.error')}: #{@config.error_msg}" puts "#{I18n.t('display.error')}: #{@config.error_msg}"
else else
return true return true
@ -54,8 +60,7 @@ class Cli
end end
rescue Exception => e rescue Exception => e
puts "#{I18n.t('display.error')}: #{e}" puts "#{I18n.t('display.error')}: #{e}"
end else
return false return false
end end
@ -96,7 +101,7 @@ class Cli
end end
# Request the GPG password and decrypt the file # Request the GPG password and decrypt the file
def decrypt() def decrypt
if !defined?(@mpw) if !defined?(@mpw)
@mpw = MPW::MPW.new(@config.file_gpg, @config.key) @mpw = MPW::MPW.new(@config.file_gpg, @config.key)
end end