2014-01-29 22:11:01 +00:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
# author: nishiki
|
|
|
|
# mail: nishiki@yaegashi.fr
|
|
|
|
# info: a simple script who manage your passwords
|
|
|
|
|
|
|
|
module MPW
|
|
|
|
|
|
|
|
module Sync
|
|
|
|
|
|
|
|
require 'rubygems'
|
|
|
|
require 'i18n'
|
2014-02-01 18:11:19 +00:00
|
|
|
require 'net/ssh'
|
|
|
|
require 'net/scp'
|
2014-01-29 22:11:01 +00:00
|
|
|
|
2014-02-01 18:11:19 +00:00
|
|
|
class SSH
|
2014-01-29 22:11:01 +00:00
|
|
|
|
|
|
|
attr_accessor :error_msg
|
|
|
|
attr_accessor :enable
|
|
|
|
|
|
|
|
# Constructor
|
|
|
|
def initialize
|
|
|
|
@error_msg = nil
|
|
|
|
@enable = false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Connect to server
|
|
|
|
# @args: host -> the server host
|
|
|
|
# port -> ther connection port
|
|
|
|
# gpg_key -> the gpg key
|
|
|
|
# password -> the remote password
|
|
|
|
# suffix -> the suffix file
|
|
|
|
# @rtrn: false if the connection fail
|
|
|
|
def connect(host, user, password, path, port=nil)
|
|
|
|
@host = host
|
|
|
|
@user = user
|
|
|
|
@password = password
|
|
|
|
@path = path
|
2014-02-01 18:11:19 +00:00
|
|
|
@port = port.instance_of?(Integer) ? 22 : port
|
2014-01-29 22:11:01 +00:00
|
|
|
|
2014-08-31 11:07:06 +00:00
|
|
|
Net::SSH.start(@host, @user, password: @password, port: @port) do
|
2014-02-01 18:11:19 +00:00
|
|
|
@enable = true
|
2014-01-29 22:11:01 +00:00
|
|
|
end
|
2014-02-01 18:11:19 +00:00
|
|
|
rescue Exception => e
|
2014-01-29 22:11:01 +00:00
|
|
|
@error_msg = "#{I18n.t('error.sync.connection')}\n#{e}"
|
|
|
|
@enable = false
|
|
|
|
else
|
|
|
|
return @enable
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get data on server
|
|
|
|
# @args: gpg_password -> the gpg password
|
|
|
|
# @rtrn: nil if nothing data or error
|
2015-01-17 11:01:37 +00:00
|
|
|
def get(gpg_key, gpg_password)
|
2014-11-16 18:50:09 +00:00
|
|
|
return nil if not @enable
|
2014-01-29 22:11:01 +00:00
|
|
|
|
2014-01-30 22:08:38 +00:00
|
|
|
tmp_file = tmpfile
|
2014-08-31 11:07:06 +00:00
|
|
|
Net::SCP.start(@host, @user, password: @password, port: @port) do |scp|
|
2014-02-01 18:11:19 +00:00
|
|
|
scp.download!(@path, tmp_file)
|
2014-01-29 22:11:01 +00:00
|
|
|
end
|
|
|
|
|
2015-01-17 11:01:37 +00:00
|
|
|
mpw = MPW.new(tmp_file, gpg_key)
|
2014-11-16 18:50:09 +00:00
|
|
|
if not mpw.decrypt(gpg_password)
|
2014-01-30 22:08:38 +00:00
|
|
|
@error_msg = mpw.error_msg
|
2014-01-29 22:11:01 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
File.unlink(tmp_file)
|
2014-01-30 22:08:38 +00:00
|
|
|
return mpw.search
|
2014-01-29 22:11:01 +00:00
|
|
|
rescue Exception => e
|
|
|
|
@error_msg = "#{I18n.t('error.sync.download')}\n#{e}"
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update the remote data
|
|
|
|
# @args: data -> the data to send on server
|
|
|
|
# @rtrn: false if there is a problem
|
|
|
|
def update(data)
|
2014-11-16 18:50:09 +00:00
|
|
|
return true if not @enable
|
2014-01-29 22:11:01 +00:00
|
|
|
|
2014-01-30 22:08:38 +00:00
|
|
|
tmp_file = tmpfile
|
2014-02-01 18:11:19 +00:00
|
|
|
File.open(tmp_file, "w") do |file|
|
|
|
|
file << data
|
|
|
|
end
|
|
|
|
|
2014-08-31 11:07:06 +00:00
|
|
|
Net::SCP.start(@host, @user, password: @password, port: @port) do |scp|
|
2014-02-01 18:11:19 +00:00
|
|
|
scp.upload!(tmp_file, @path)
|
2014-01-29 22:11:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
File.unlink(tmp_file)
|
|
|
|
|
|
|
|
return true
|
|
|
|
rescue Exception => e
|
|
|
|
@error_msg = "#{I18n.t('error.sync.upload')}\n#{e}"
|
|
|
|
return false
|
|
|
|
end
|
2014-01-30 22:08:38 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2014-01-29 22:11:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|