From bed8cbb8b65ff8be7ddcd07edf1595f76ad7d529 Mon Sep 17 00:00:00 2001 From: nishiki Date: Sun, 2 Feb 2014 17:34:05 +0100 Subject: [PATCH] add share_keys --- MPW/Config.rb | 3 ++- MPW/MPW.rb | 17 ++++++++++++----- MPW/Sync/MPWSync.rb | 10 +++++++--- MPW/UI/Cli.rb | 2 +- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/MPW/Config.rb b/MPW/Config.rb index bd993bb..4ace575 100644 --- a/MPW/Config.rb +++ b/MPW/Config.rb @@ -58,7 +58,8 @@ module MPW return false end - if !share_keys.nil? && !share_keys.empty? + share_keys = share_keys.nil? ? '' : share_keys + if !share_keys.empty? share_keys.split.each do |k| if not k =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/ @error_msg = I18n.t('error.config.key_bad_format') diff --git a/MPW/MPW.rb b/MPW/MPW.rb index 6d851d6..05da9cf 100644 --- a/MPW/MPW.rb +++ b/MPW/MPW.rb @@ -26,10 +26,11 @@ module MPW attr_accessor :error_msg # Constructor - def initialize(file_gpg, key=nil) - @error_msg = nil - @file_gpg = file_gpg - @key = key + def initialize(file_gpg, key=nil, share_keys='') + @error_msg = nil + @file_gpg = file_gpg + @key = key + @share_keys = share_keys end # Decrypt a gpg file @@ -64,7 +65,13 @@ module MPW data_to_encrypt << row.to_csv end - crypto.encrypt(data_to_encrypt, :recipients => @key, :output => file_gpg) + recipients = [] + recipients.push(@key) + if !@share_keys.nil? + @share_keys.split.each { |k| recipients.push(k) } + end + + crypto.encrypt(data_to_encrypt, :recipients => recipients, :output => file_gpg) file_gpg.close return true diff --git a/MPW/Sync/MPWSync.rb b/MPW/Sync/MPWSync.rb index 69fdd83..da62975 100644 --- a/MPW/Sync/MPWSync.rb +++ b/MPW/Sync/MPWSync.rb @@ -31,11 +31,13 @@ module MPW # suffix -> the suffix file # @rtrn: false if the connection fail def connect(host, user, password, path, port=nil) + @host = host + @port = !port.instance_of?(Integer) ? 2201 : port @gpg_key = user @password = password @suffix = path - TCPSocket.new(host, port.to_i) do + TCPSocket.open(@host, @port) do @enable = true end rescue Exception => e @@ -53,7 +55,8 @@ module MPW return nil end - TCPSocket.new(host, port.to_i) do |socket| + msg = nil + TCPSocket.open(@host, @port) do |socket| send_msg = {:action => 'get', :gpg_key => @gpg_key, :password => @password, @@ -95,7 +98,8 @@ module MPW return true end - TCPSocket.new(host, port.to_i) do |socket| + msg = nil + TCPSocket.open(@host, @port) do |socket| send_msg = {:action => 'update', :gpg_key => @gpg_key, :password => @password, diff --git a/MPW/UI/Cli.rb b/MPW/UI/Cli.rb index 46d5a81..708f3ec 100644 --- a/MPW/UI/Cli.rb +++ b/MPW/UI/Cli.rb @@ -117,7 +117,7 @@ class Cli # Request the GPG password and decrypt the file def decrypt if !defined?(@mpw) - @mpw = MPW::MPW.new(@config.file_gpg, @config.key) + @mpw = MPW::MPW.new(@config.file_gpg, @config.key, @config.share_keys) end @passwd = ask(I18n.t('display.gpg_password')) {|q| q.echo = false}