2014-01-29 19:49:39 +00:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
# author: nishiki
|
|
|
|
# mail: nishiki@yaegashi.fr
|
|
|
|
|
2014-12-06 17:20:43 +00:00
|
|
|
require 'rubygems'
|
|
|
|
require 'gpgme'
|
|
|
|
require 'csv'
|
|
|
|
require 'i18n'
|
|
|
|
require 'fileutils'
|
2014-12-07 15:24:31 +00:00
|
|
|
require 'yaml'
|
2015-02-08 21:54:04 +00:00
|
|
|
|
|
|
|
require_relative './Item'
|
2014-01-29 19:49:39 +00:00
|
|
|
|
2014-12-06 17:20:43 +00:00
|
|
|
module MPW
|
2014-01-29 19:49:39 +00:00
|
|
|
class MPW
|
|
|
|
|
|
|
|
attr_accessor :error_msg
|
|
|
|
|
|
|
|
# Constructor
|
2014-12-07 15:24:31 +00:00
|
|
|
def initialize(file_gpg, key, share_keys='')
|
2014-02-02 16:34:05 +00:00
|
|
|
@error_msg = nil
|
|
|
|
@file_gpg = file_gpg
|
|
|
|
@key = key
|
|
|
|
@share_keys = share_keys
|
2015-01-27 22:17:16 +00:00
|
|
|
@data = []
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Decrypt a gpg file
|
|
|
|
# @args: password -> the GPG key password
|
|
|
|
# @rtrn: true if data has been decrypted
|
2015-01-27 22:17:16 +00:00
|
|
|
def decrypt(password=nil)
|
2015-02-12 22:03:01 +00:00
|
|
|
@data = []
|
|
|
|
|
2015-02-08 19:56:39 +00:00
|
|
|
if File.exist?(@file_gpg) and not File.zero?(@file_gpg)
|
2015-01-27 22:17:16 +00:00
|
|
|
crypto = GPGME::Crypto.new(armor: true)
|
|
|
|
data_decrypt = crypto.decrypt(IO.read(@file_gpg), password: password).read.force_encoding('utf-8')
|
2015-01-29 20:48:56 +00:00
|
|
|
|
2015-01-27 22:17:16 +00:00
|
|
|
if not data_decrypt.to_s.empty?
|
2015-01-28 22:08:25 +00:00
|
|
|
YAML.load(data_decrypt).each_value do |d|
|
|
|
|
@data.push(Item.new(id: d['id'],
|
|
|
|
name: d['name'],
|
|
|
|
group: d['group'],
|
|
|
|
host: d['host'],
|
|
|
|
protocol: d['protocol'],
|
2015-02-01 11:23:50 +00:00
|
|
|
user: d['user'],
|
2015-01-28 22:08:25 +00:00
|
|
|
password: d['password'],
|
|
|
|
port: d['port'],
|
|
|
|
comment: d['comment'],
|
|
|
|
last_edit: d['last_edit'],
|
|
|
|
created: d['created'],
|
|
|
|
)
|
|
|
|
)
|
2015-01-27 22:17:16 +00:00
|
|
|
end
|
2015-01-28 22:08:25 +00:00
|
|
|
end
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
2014-01-30 22:08:38 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
rescue Exception => e
|
|
|
|
@error_msg = "#{I18n.t('error.gpg_file.decrypt')}\n#{e}"
|
|
|
|
return false
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Encrypt a file
|
|
|
|
# @rtrn: true if the file has been encrypted
|
2014-01-30 22:08:38 +00:00
|
|
|
def encrypt
|
2014-12-06 15:24:40 +00:00
|
|
|
FileUtils.cp(@file_gpg, "#{@file_gpg}.bk") if File.exist?(@file_gpg)
|
2015-02-01 11:23:50 +00:00
|
|
|
|
|
|
|
data_to_encrypt = {}
|
2014-01-29 19:49:39 +00:00
|
|
|
|
2015-01-28 22:08:25 +00:00
|
|
|
@data.each do |item|
|
2015-01-29 20:48:56 +00:00
|
|
|
next if item.empty?
|
|
|
|
|
|
|
|
data_to_encrypt.merge!(item.id => {'id' => item.id,
|
|
|
|
'name' => item.name,
|
|
|
|
'group' => item.group,
|
|
|
|
'host' => item.host,
|
|
|
|
'protocol' => item.protocol,
|
|
|
|
'user' => item.user,
|
|
|
|
'password' => item.password,
|
|
|
|
'port' => item.port,
|
|
|
|
'comment' => item.comment,
|
|
|
|
'last_edit' => item.last_edit,
|
|
|
|
'created' => item.created,
|
|
|
|
}
|
|
|
|
)
|
2015-01-28 22:08:25 +00:00
|
|
|
end
|
2014-01-29 19:49:39 +00:00
|
|
|
|
2014-08-31 16:31:14 +00:00
|
|
|
recipients = []
|
2014-02-02 16:34:05 +00:00
|
|
|
recipients.push(@key)
|
2014-11-16 18:39:38 +00:00
|
|
|
if not @share_keys.nil?
|
2014-02-02 16:34:05 +00:00
|
|
|
@share_keys.split.each { |k| recipients.push(k) }
|
|
|
|
end
|
|
|
|
|
2014-12-06 15:24:40 +00:00
|
|
|
crypto = GPGME::Crypto.new(armor: true)
|
|
|
|
file_gpg = File.open(@file_gpg, 'w+')
|
2015-01-28 22:08:25 +00:00
|
|
|
crypto.encrypt(data_to_encrypt.to_yaml, recipients: recipients, output: file_gpg)
|
2014-01-30 22:08:38 +00:00
|
|
|
file_gpg.close
|
2014-01-29 19:49:39 +00:00
|
|
|
|
2014-12-06 15:24:40 +00:00
|
|
|
FileUtils.rm("#{@file_gpg}.bk") if File.exist?("#{@file_gpg}.bk")
|
2014-01-30 22:08:38 +00:00
|
|
|
return true
|
|
|
|
rescue Exception => e
|
|
|
|
@error_msg = "#{I18n.t('error.gpg_file.encrypt')}\n#{e}"
|
2014-12-06 15:24:40 +00:00
|
|
|
FileUtils.mv("#{@file_gpg}.bk", @file_gpg) if File.exist?("#{@file_gpg}.bk")
|
2014-01-30 22:08:38 +00:00
|
|
|
return false
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
|
|
|
|
2015-02-01 10:48:02 +00:00
|
|
|
# Add a new item
|
|
|
|
# @args: item -> Object MPW::Item
|
|
|
|
# @rtrn: true if add item
|
|
|
|
def add(item)
|
|
|
|
if not item.instance_of?(Item)
|
|
|
|
@error_msg = I18n.t('error.bad_class')
|
|
|
|
return false
|
|
|
|
elsif item.empty?
|
|
|
|
@error_msg = I18n.t('error.add.empty')
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
@data.push(item)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-29 19:49:39 +00:00
|
|
|
# Search in some csv data
|
2015-02-13 18:31:01 +00:00
|
|
|
# @args: options -> a hash with paramaters
|
2014-01-29 19:49:39 +00:00
|
|
|
# @rtrn: a list with the resultat of the search
|
2015-01-27 22:17:16 +00:00
|
|
|
def list(options={})
|
2014-01-31 23:09:22 +00:00
|
|
|
result = []
|
2014-01-29 19:49:39 +00:00
|
|
|
|
2015-03-19 21:52:33 +00:00
|
|
|
search = options[:search].to_s.downcase
|
|
|
|
group = options[:group].to_s.downcase
|
|
|
|
protocol = options[:protocol].to_s.downcase
|
2015-02-14 09:42:03 +00:00
|
|
|
|
2015-01-27 22:17:16 +00:00
|
|
|
@data.each do |item|
|
2015-03-22 21:38:35 +00:00
|
|
|
next if item.empty?
|
|
|
|
|
2015-02-14 09:42:03 +00:00
|
|
|
next if not group.empty? and not group.eql?(item.group.downcase)
|
|
|
|
next if not protocol.empty? and not protocol.eql?(item.protocol.downcase)
|
|
|
|
|
|
|
|
name = item.name.to_s.downcase
|
|
|
|
host = item.host.to_s.downcase
|
|
|
|
comment = item.comment.to_s.downcase
|
|
|
|
|
|
|
|
if not name =~ /^.*#{search}.*$/ and not host =~ /^.*#{search}.*$/ and not comment =~ /^.*#{search}.*$/
|
|
|
|
next
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
2015-02-14 09:42:03 +00:00
|
|
|
|
|
|
|
result.push(item)
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
# Search in some csv data
|
2014-12-31 10:31:28 +00:00
|
|
|
# @args: id -> the id item
|
2015-02-13 18:31:01 +00:00
|
|
|
# @rtrn: a row with the result of the search
|
2014-12-31 10:31:28 +00:00
|
|
|
def search_by_id(id)
|
2015-01-27 22:17:16 +00:00
|
|
|
@data.each do |item|
|
|
|
|
return item if item.id == id
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
|
|
|
|
2015-01-27 22:17:16 +00:00
|
|
|
return nil
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Export to csv
|
2014-11-15 18:14:42 +00:00
|
|
|
# @args: file -> file where you export the data
|
2014-12-06 15:24:40 +00:00
|
|
|
# type -> udata type
|
2014-01-29 19:49:39 +00:00
|
|
|
# @rtrn: true if export work
|
2014-12-06 22:32:09 +00:00
|
|
|
def export(file, type=:yaml)
|
2014-11-15 18:14:42 +00:00
|
|
|
case type
|
|
|
|
when :csv
|
2014-12-29 21:32:36 +00:00
|
|
|
CSV.open(file, 'w', write_headers: true,
|
2015-01-28 22:08:25 +00:00
|
|
|
headers: ['name', 'group', 'protocol', 'host', 'user', 'password', 'port', 'comment']) do |csv|
|
|
|
|
@data.each do |item|
|
2015-02-12 22:03:01 +00:00
|
|
|
csv << [item.name, item.group, item.protocol, item.host, item.user, item.password, item.port, item.comment]
|
2014-11-15 18:14:42 +00:00
|
|
|
end
|
2014-12-29 21:32:36 +00:00
|
|
|
end
|
2014-11-15 18:14:42 +00:00
|
|
|
|
2014-12-06 17:20:43 +00:00
|
|
|
when :yaml
|
2015-01-28 22:08:25 +00:00
|
|
|
data = {}
|
|
|
|
@data.each do |item|
|
2015-01-29 20:48:56 +00:00
|
|
|
data.merge!(item.id => {'id' => item.id,
|
|
|
|
'name' => item.name,
|
|
|
|
'group' => item.group,
|
|
|
|
'host' => item.host,
|
|
|
|
'protocol' => item.protocol,
|
|
|
|
'user' => item.user,
|
|
|
|
'password' => item.password,
|
|
|
|
'port' => item.port,
|
|
|
|
'comment' => item.comment,
|
|
|
|
'last_edit' => item.last_edit,
|
|
|
|
'created' => item.created,
|
|
|
|
}
|
|
|
|
)
|
2015-01-28 22:08:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
File.open(file, 'w') {|f| f << data.to_yaml}
|
2014-11-15 18:14:42 +00:00
|
|
|
|
|
|
|
else
|
|
|
|
@error_msg = "#{I18n.t('error.export.unknown_type', type: type)}"
|
|
|
|
return false
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
2014-08-31 15:40:37 +00:00
|
|
|
|
2014-01-30 22:08:38 +00:00
|
|
|
return true
|
|
|
|
rescue Exception => e
|
2014-08-31 10:28:20 +00:00
|
|
|
@error_msg = "#{I18n.t('error.export.write', file: file)}\n#{e}"
|
2014-01-30 22:08:38 +00:00
|
|
|
return false
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Import to csv
|
|
|
|
# @args: file -> path to file import
|
2014-12-06 15:24:40 +00:00
|
|
|
# type -> udata type
|
2014-01-29 19:49:39 +00:00
|
|
|
# @rtrn: true if the import work
|
2014-12-06 22:32:09 +00:00
|
|
|
def import(file, type=:yaml)
|
2014-12-06 15:24:40 +00:00
|
|
|
case type
|
|
|
|
when :csv
|
2014-12-06 17:20:43 +00:00
|
|
|
CSV.foreach(file, {headers: true}) do |row|
|
2015-01-29 21:09:42 +00:00
|
|
|
item = Item.new(name: row['name'],
|
|
|
|
group: row['group'],
|
|
|
|
host: row['host'],
|
|
|
|
protocol: row['protocol'],
|
2015-02-12 22:03:01 +00:00
|
|
|
user: row['user'],
|
2015-01-29 21:09:42 +00:00
|
|
|
password: row['password'],
|
|
|
|
port: row['port'],
|
|
|
|
comment: row['comment'],
|
|
|
|
)
|
|
|
|
|
|
|
|
return false if item.empty?
|
|
|
|
|
|
|
|
@data.push(item)
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
2014-12-06 17:20:43 +00:00
|
|
|
|
2014-12-06 15:24:40 +00:00
|
|
|
when :yaml
|
2014-12-31 10:31:28 +00:00
|
|
|
YAML::load_file(file).each_value do |row|
|
2015-01-29 21:09:42 +00:00
|
|
|
item = Item.new(name: row['name'],
|
|
|
|
group: row['group'],
|
|
|
|
host: row['host'],
|
|
|
|
protocol: row['protocol'],
|
2015-02-12 22:03:01 +00:00
|
|
|
user: row['user'],
|
2015-01-29 21:09:42 +00:00
|
|
|
password: row['password'],
|
|
|
|
port: row['port'],
|
|
|
|
comment: row['comment'],
|
|
|
|
)
|
|
|
|
|
|
|
|
return false if item.empty?
|
|
|
|
|
|
|
|
@data.push(item)
|
2014-12-06 15:24:40 +00:00
|
|
|
end
|
2014-12-06 17:20:43 +00:00
|
|
|
|
2014-12-06 15:24:40 +00:00
|
|
|
else
|
|
|
|
@error_msg = "#{I18n.t('error.export.unknown_type', type: type)}"
|
|
|
|
return false
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
2014-01-30 22:08:38 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
rescue Exception => e
|
2014-08-31 10:28:20 +00:00
|
|
|
@error_msg = "#{I18n.t('error.import.read', file: file)}\n#{e}"
|
2014-01-30 22:08:38 +00:00
|
|
|
return false
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return a preview import
|
|
|
|
# @args: file -> path to file import
|
2014-12-06 20:37:18 +00:00
|
|
|
# @rtrn: a hash with the items to import, if there is an error return false
|
2014-12-28 18:43:13 +00:00
|
|
|
def import_preview(file, type=:yaml)
|
2015-03-19 21:52:33 +00:00
|
|
|
data = []
|
|
|
|
|
2014-12-06 15:24:40 +00:00
|
|
|
case type
|
|
|
|
when :csv
|
|
|
|
CSV.foreach(file, {headers: true}) do |row|
|
2015-03-19 21:52:33 +00:00
|
|
|
item = Item.new(name: row['name'],
|
|
|
|
group: row['group'],
|
|
|
|
host: row['host'],
|
|
|
|
protocol: row['protocol'],
|
|
|
|
user: row['user'],
|
|
|
|
password: row['password'],
|
|
|
|
port: row['port'],
|
|
|
|
comment: row['comment'],
|
|
|
|
)
|
|
|
|
|
|
|
|
return false if item.empty?
|
|
|
|
|
|
|
|
data.push(item)
|
2014-12-06 15:24:40 +00:00
|
|
|
end
|
2015-03-19 21:52:33 +00:00
|
|
|
|
2014-12-06 15:24:40 +00:00
|
|
|
when :yaml
|
2015-03-19 21:52:33 +00:00
|
|
|
YAML::load_file(file).each_value do |row|
|
|
|
|
item = Item.new(name: row['name'],
|
|
|
|
group: row['group'],
|
|
|
|
host: row['host'],
|
|
|
|
protocol: row['protocol'],
|
|
|
|
user: row['user'],
|
|
|
|
password: row['password'],
|
|
|
|
port: row['port'],
|
|
|
|
comment: row['comment'],
|
|
|
|
)
|
|
|
|
|
|
|
|
return false if item.empty?
|
|
|
|
|
|
|
|
data.push(item)
|
2014-12-06 15:24:40 +00:00
|
|
|
end
|
2015-03-19 21:52:33 +00:00
|
|
|
|
2014-12-06 15:24:40 +00:00
|
|
|
else
|
|
|
|
@error_msg = "#{I18n.t('error.export.unknown_type', type: type)}"
|
|
|
|
return false
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
2015-03-19 21:52:33 +00:00
|
|
|
|
|
|
|
return data
|
2014-01-30 22:08:38 +00:00
|
|
|
rescue Exception => e
|
2014-08-31 10:28:20 +00:00
|
|
|
@error_msg = "#{I18n.t('error.import.read', file: file)}\n#{e}"
|
2014-01-30 22:08:38 +00:00
|
|
|
return false
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
|
|
|
|
2015-02-13 18:31:01 +00:00
|
|
|
# Generate a random password
|
2014-01-29 19:49:39 +00:00
|
|
|
# @args: length -> the length password
|
|
|
|
# @rtrn: a random string
|
|
|
|
def self.password(length=8)
|
|
|
|
if length.to_i <= 0
|
|
|
|
length = 8
|
|
|
|
else
|
|
|
|
length = length.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
result = ''
|
|
|
|
while length > 62 do
|
|
|
|
result << ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(62).join
|
|
|
|
length -= 62
|
|
|
|
end
|
|
|
|
result << ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(length).join
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2014-12-06 17:20:43 +00:00
|
|
|
end
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|