2014-01-10 16:55:05 +00:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
|
|
|
|
require 'socket'
|
|
|
|
require 'json'
|
|
|
|
require 'highline/import'
|
|
|
|
require 'digest'
|
2014-01-25 10:35:12 +00:00
|
|
|
require 'logger'
|
2014-01-10 16:55:05 +00:00
|
|
|
|
|
|
|
class Server
|
2014-01-25 10:35:12 +00:00
|
|
|
|
2014-01-10 16:55:05 +00:00
|
|
|
attr_accessor :error_msg
|
|
|
|
|
|
|
|
# Constructor
|
|
|
|
def initialize()
|
|
|
|
YAML::ENGINE.yamler='syck'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Start the server
|
|
|
|
def start()
|
2014-01-25 10:35:12 +00:00
|
|
|
begin
|
|
|
|
server = TCPServer.open(@host, @port)
|
|
|
|
@log.info("The server is started on #{@host}:#{@port}")
|
|
|
|
rescue Exception => e
|
|
|
|
@log.error("Impossible to start the server: #{e}")
|
|
|
|
exit 2
|
|
|
|
end
|
|
|
|
|
2014-01-10 16:55:05 +00:00
|
|
|
loop do
|
|
|
|
Thread.start(server.accept) do |client|
|
2014-01-25 10:35:12 +00:00
|
|
|
@log.info("#{client.peeraddr[3]} is connected")
|
|
|
|
|
2014-01-14 22:00:52 +00:00
|
|
|
while true do
|
2014-01-26 14:09:48 +00:00
|
|
|
msg = get_client_msg(client)
|
2014-01-14 22:00:52 +00:00
|
|
|
|
|
|
|
if !msg
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2014-01-26 16:26:02 +00:00
|
|
|
if !msg['action'].nil? && msg['action'] == 'close'
|
|
|
|
@log.info("#{client.peeraddr[3]} is disconnected")
|
|
|
|
close_connection(client)
|
|
|
|
end
|
|
|
|
|
2014-01-14 22:00:52 +00:00
|
|
|
if msg['gpg_key'].nil? || msg['gpg_key'].empty? || msg['password'].nil? || msg['password'].empty?
|
2014-01-26 16:26:02 +00:00
|
|
|
@log.warning("#{client.peeraddr[3]} is disconnected because no password or no gpg_key")
|
2014-01-26 14:09:48 +00:00
|
|
|
close_connection(client)
|
2014-01-14 22:00:52 +00:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
case msg['action']
|
|
|
|
when 'get'
|
2014-01-25 10:35:12 +00:00
|
|
|
@log.debug("#{client.peeraddr[3]} GET gpg_key=#{msg['gpg_key']} suffix=#{msg['suffix']}")
|
2014-01-26 14:09:48 +00:00
|
|
|
client.puts get_file(msg)
|
2014-01-14 22:00:52 +00:00
|
|
|
when 'update'
|
2014-01-25 10:35:12 +00:00
|
|
|
@log.debug("#{client.peeraddr[3]} UPDATE gpg_key=#{msg['gpg_key']} suffix=#{msg['suffix']}")
|
2014-01-26 14:09:48 +00:00
|
|
|
client.puts update_file(msg)
|
2014-01-14 22:00:52 +00:00
|
|
|
when 'delete'
|
2014-01-25 10:35:12 +00:00
|
|
|
@log.debug("#{client.peeraddr[3]} DELETE gpg_key=#{msg['gpg_key']} suffix=#{msg['suffix']}")
|
2014-01-26 14:09:48 +00:00
|
|
|
client.puts delete_file(msg)
|
2014-01-14 22:00:52 +00:00
|
|
|
else
|
2014-01-26 16:26:02 +00:00
|
|
|
@log.warning("#{client.peeraddr[3]} is disconnected because unkwnow command")
|
2014-01-16 20:24:08 +00:00
|
|
|
send_msg = {:action => 'unknown',
|
|
|
|
:gpg_key => msg['gpg_key'],
|
|
|
|
:error => 'server.error.client.unknown'}
|
|
|
|
client.puts send_msg
|
2014-01-26 14:09:48 +00:00
|
|
|
close_connection(client)
|
2014-01-14 22:00:52 +00:00
|
|
|
end
|
2014-01-10 16:55:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get a gpg file
|
|
|
|
# @args: msg -> message puts by the client
|
|
|
|
# @rtrn: json message
|
2014-01-26 14:09:48 +00:00
|
|
|
def get_file(msg)
|
2014-01-10 16:55:05 +00:00
|
|
|
gpg_key = msg['gpg_key'].sub('@', '_')
|
|
|
|
|
|
|
|
if msg['suffix'].nil? || msg['suffix'].empty?
|
|
|
|
file_gpg = "#{@data_dir}/#{gpg_key}.yml"
|
|
|
|
else
|
|
|
|
file_gpg = "#{@data_dir}/#{gpg_key}-#{msg['suffix']}.yml"
|
|
|
|
end
|
|
|
|
|
|
|
|
if File.exist?(file_gpg)
|
2014-01-12 10:07:54 +00:00
|
|
|
gpg_data = YAML::load_file(file_gpg)
|
|
|
|
salt = gpg_data['gpg']['salt']
|
|
|
|
hash = gpg_data['gpg']['hash']
|
|
|
|
data = gpg_data['gpg']['data']
|
2014-01-10 16:55:05 +00:00
|
|
|
|
2014-01-26 14:09:48 +00:00
|
|
|
if is_authorized?(msg['password'], salt, hash)
|
2014-01-14 22:00:52 +00:00
|
|
|
send_msg = {:action => 'get',
|
|
|
|
:gpg_key => msg['gpg_key'],
|
2014-01-16 20:24:08 +00:00
|
|
|
:data => data,
|
|
|
|
:error => nil}
|
2014-01-10 16:55:05 +00:00
|
|
|
else
|
|
|
|
send_msg = {:action => 'get',
|
|
|
|
:gpg_key => msg['gpg_key'],
|
2014-01-16 20:24:08 +00:00
|
|
|
:error => 'server.error.client.no_authorized'}
|
2014-01-10 16:55:05 +00:00
|
|
|
end
|
|
|
|
else
|
2014-01-16 20:24:08 +00:00
|
|
|
send_msg = {:action => 'get',
|
|
|
|
:gpg_key => msg['gpg_key'],
|
|
|
|
:data => '',
|
|
|
|
:error => nil}
|
2014-01-10 16:55:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return send_msg.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update a file
|
|
|
|
# @args: msg -> message puts by the client
|
|
|
|
# @rtrn: json message
|
2014-01-26 14:09:48 +00:00
|
|
|
def update_file(msg)
|
2014-01-10 16:55:05 +00:00
|
|
|
gpg_key = msg['gpg_key'].sub('@', '_')
|
|
|
|
data = msg['data']
|
|
|
|
|
|
|
|
if data.nil? || data.empty?
|
|
|
|
send_msg = {:action => 'update',
|
|
|
|
:gpg_key => msg['gpg_key'],
|
2014-01-16 20:24:08 +00:00
|
|
|
:error => 'server.error.client.no_data'}
|
2014-01-10 16:55:05 +00:00
|
|
|
|
|
|
|
return send_msg.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
if msg['suffix'].nil? || msg['suffix'].empty?
|
|
|
|
file_gpg = "#{@data_dir}/#{gpg_key}.yml"
|
|
|
|
else
|
|
|
|
file_gpg = "#{@data_dir}/#{gpg_key}-#{msg['suffix']}.yml"
|
|
|
|
end
|
|
|
|
|
|
|
|
if File.exist?(file_gpg)
|
|
|
|
gpg_data = YAML::load_file(file_gpg)
|
|
|
|
salt = gpg_data['gpg']['salt']
|
|
|
|
hash = gpg_data['gpg']['hash']
|
|
|
|
|
|
|
|
else
|
2014-01-27 17:27:37 +00:00
|
|
|
salt = salt()
|
2014-01-10 16:55:05 +00:00
|
|
|
hash = Digest::SHA256.hexdigest(salt + msg['password'])
|
|
|
|
end
|
|
|
|
|
2014-01-26 14:09:48 +00:00
|
|
|
if is_authorized?(msg['password'], salt, hash)
|
2014-01-10 16:55:05 +00:00
|
|
|
begin
|
2014-01-12 10:07:54 +00:00
|
|
|
config = {'gpg' => {'salt' => salt,
|
|
|
|
'hash' => hash,
|
|
|
|
'data' => data}}
|
2014-01-10 16:55:05 +00:00
|
|
|
|
2014-01-14 22:00:52 +00:00
|
|
|
File.open(file_gpg, 'w+') do |file|
|
2014-01-10 16:55:05 +00:00
|
|
|
file << config.to_yaml
|
|
|
|
end
|
|
|
|
|
2014-01-16 20:24:08 +00:00
|
|
|
send_msg = {:action => 'update',
|
|
|
|
:gpg_key => msg['gpg_key'],
|
|
|
|
:error => nil}
|
2014-01-10 16:55:05 +00:00
|
|
|
rescue Exception => e
|
|
|
|
send_msg = {:action => 'update',
|
|
|
|
:gpg_key => msg['gpg_key'],
|
2014-01-16 20:24:08 +00:00
|
|
|
:error => 'server.error.client.unknown'}
|
2014-01-10 16:55:05 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
send_msg = {:action => 'update',
|
|
|
|
:gpg_key => msg['gpg_key'],
|
2014-01-16 20:24:08 +00:00
|
|
|
:error => 'server.error.client.no_authorized'}
|
2014-01-10 16:55:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return send_msg.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
# Remove a gpg file
|
|
|
|
# @args: msg -> message puts by the client
|
|
|
|
# @rtrn: json message
|
2014-01-26 14:09:48 +00:00
|
|
|
def delete_file(msg)
|
2014-01-10 17:12:24 +00:00
|
|
|
gpg_key = msg['gpg_key'].sub('@', '_')
|
|
|
|
|
|
|
|
if msg['suffix'].nil? || msg['suffix'].empty?
|
|
|
|
file_gpg = "#{@data_dir}/#{gpg_key}.yml"
|
|
|
|
else
|
|
|
|
file_gpg = "#{@data_dir}/#{gpg_key}-#{msg['suffix']}.yml"
|
|
|
|
end
|
|
|
|
|
|
|
|
if !File.exist?(file_gpg)
|
|
|
|
send_msg = {:action => 'delete',
|
|
|
|
:gpg_key => msg['gpg_key'],
|
2014-01-16 20:24:08 +00:00
|
|
|
:error => nil}
|
2014-01-10 17:12:24 +00:00
|
|
|
|
|
|
|
return send_msg.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
gpg_data = YAML::load_file(file_gpg)
|
|
|
|
salt = gpg_data['gpg']['salt']
|
|
|
|
hash = gpg_data['gpg']['hash']
|
|
|
|
|
2014-01-26 14:09:48 +00:00
|
|
|
if is_authorized?(msg['password'], salt, hash)
|
2014-01-10 17:12:24 +00:00
|
|
|
begin
|
|
|
|
File.unlink(file_gpg)
|
|
|
|
|
|
|
|
send_msg = {:action => 'delete',
|
|
|
|
:gpg_key => msg['gpg_key'],
|
2014-01-16 20:24:08 +00:00
|
|
|
:error => nil}
|
2014-01-10 17:12:24 +00:00
|
|
|
rescue Exception => e
|
|
|
|
send_msg = {:action => 'delete',
|
|
|
|
:gpg_key => msg['gpg_key'],
|
2014-01-16 20:24:08 +00:00
|
|
|
:error => 'server.error.client.unknown'}
|
2014-01-10 17:12:24 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
send_msg = {:action => 'delete',
|
|
|
|
:gpg_key => msg['gpg_key'],
|
2014-01-16 20:24:08 +00:00
|
|
|
:error => 'server.error.client.no_authorized'}
|
2014-01-10 17:12:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return send_msg.to_json
|
2014-01-10 16:55:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Check is the hash equal the password with the salt
|
|
|
|
# @args: password -> the user password
|
|
|
|
# salt -> the salt
|
|
|
|
# hash -> the hash of the password with the salt
|
|
|
|
# @rtrn: true is is good, else false
|
2014-01-26 14:09:48 +00:00
|
|
|
def is_authorized?(password, salt, hash)
|
2014-01-10 16:55:05 +00:00
|
|
|
if hash == Digest::SHA256.hexdigest(salt + password)
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get message to client
|
|
|
|
# @args: client -> client connection
|
|
|
|
# @rtrn: array of the json string, or false if isn't json message
|
2014-01-26 14:09:48 +00:00
|
|
|
def get_client_msg(client)
|
2014-01-10 16:55:05 +00:00
|
|
|
begin
|
|
|
|
msg = client.gets
|
|
|
|
return JSON.parse(msg)
|
|
|
|
rescue
|
2014-01-15 21:49:01 +00:00
|
|
|
closeConnection(client)
|
2014-01-10 16:55:05 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Close the client connection
|
|
|
|
# @args: client -> client connection
|
2014-01-26 14:09:48 +00:00
|
|
|
def close_connection(client)
|
2014-01-10 16:55:05 +00:00
|
|
|
client.puts "Closing the connection. Bye!"
|
|
|
|
client.close
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check the config file
|
|
|
|
# @args: file_config -> the configuration file
|
|
|
|
# @rtrn: true if the config file is correct
|
|
|
|
def checkconfig(file_config)
|
|
|
|
begin
|
|
|
|
config = YAML::load_file(file_config)
|
|
|
|
@host = config['config']['host']
|
2014-01-14 22:00:52 +00:00
|
|
|
@port = config['config']['port'].to_i
|
2014-01-10 16:55:05 +00:00
|
|
|
@data_dir = config['config']['data_dir']
|
2014-01-25 10:35:12 +00:00
|
|
|
@log_file = config['config']['log_file']
|
2014-01-10 16:55:05 +00:00
|
|
|
@timeout = config['config']['timeout'].to_i
|
|
|
|
|
2014-01-14 22:00:52 +00:00
|
|
|
if @host.empty? || @port <= 0 || @data_dir.empty?
|
2014-01-25 17:40:47 +00:00
|
|
|
puts I18n.t('checkconfig.fail')
|
|
|
|
puts I18n.t('checkconfig.empty')
|
2014-01-10 21:49:22 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if !Dir.exist?(@data_dir)
|
2014-01-25 17:40:47 +00:00
|
|
|
puts I18n.t('checkconfig.fail')
|
|
|
|
puts I18n.t('checkconfig.datadir')
|
2014-01-10 16:55:05 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2014-01-25 10:35:12 +00:00
|
|
|
if @log_file.nil? || @log_file.empty?
|
2014-01-25 17:40:47 +00:00
|
|
|
puts I18n.t('checkconfig.fail')
|
|
|
|
puts I18n.t('checkconfig.log_file_empty')
|
2014-01-25 10:35:12 +00:00
|
|
|
return false
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
@log = Logger.new(@log_file)
|
|
|
|
rescue
|
2014-01-25 17:40:47 +00:00
|
|
|
puts I18n.t('checkconfig.fail')
|
|
|
|
puts I18n.t('checkconfig.log_file_create')
|
2014-01-25 10:35:12 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-10 16:55:05 +00:00
|
|
|
rescue Exception => e
|
2014-01-25 17:40:47 +00:00
|
|
|
puts "#{I18n.t('checkconfig.fail')}\n#{e}"
|
2014-01-10 16:55:05 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a new config file
|
|
|
|
# @args: file_config -> the configuration file
|
|
|
|
# @rtrn: true if le config file is create
|
|
|
|
def setup(file_config)
|
2014-01-25 17:40:47 +00:00
|
|
|
puts I18n.t('form.setup.title')
|
2014-01-10 16:55:05 +00:00
|
|
|
puts '--------------------'
|
2014-01-25 17:40:47 +00:00
|
|
|
host = ask(I18n.t('form.setup.host')).to_s
|
|
|
|
port = ask(I18n.t('form.setup.port')).to_s
|
|
|
|
data_dir = ask(I18n.t('form.setup.data_dir')).to_s
|
|
|
|
log_file = ask(I18n.t('form.setup.log_file')).to_s
|
|
|
|
timeout = ask(I18n.t('form.setup.timeout')).to_s
|
2014-01-10 16:55:05 +00:00
|
|
|
|
|
|
|
config = {'config' => {'host' => host,
|
|
|
|
'port' => port,
|
|
|
|
'data_dir' => data_dir,
|
2014-01-25 10:35:12 +00:00
|
|
|
'log_file' => log_file,
|
2014-01-10 16:55:05 +00:00
|
|
|
'timeout' => timeout}}
|
|
|
|
|
|
|
|
begin
|
|
|
|
File.open(file_config, 'w') do |file|
|
|
|
|
file << config.to_yaml
|
|
|
|
end
|
|
|
|
rescue Exception => e
|
2014-01-25 17:40:47 +00:00
|
|
|
puts "#{I18n.t('form.setup.not_valid')}\n#{e}"
|
2014-01-10 16:55:05 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
2014-01-27 17:27:37 +00:00
|
|
|
|
|
|
|
# Generate a random salt
|
|
|
|
# @args: length -> the length salt
|
|
|
|
# @rtrn: a random string
|
|
|
|
def salt(length=4)
|
|
|
|
if length.to_i <= 0 || length.to_i > 16
|
|
|
|
length = 4
|
|
|
|
else
|
|
|
|
length = length.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
return ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(length).join
|
|
|
|
end
|
|
|
|
|
2014-01-10 16:55:05 +00:00
|
|
|
end
|