2013-07-17 20:31:28 +00:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
# author: nishiki
|
|
|
|
# mail: nishiki@yaegashi.fr
|
|
|
|
# info: a simple script who manage your passwords
|
|
|
|
|
|
|
|
require 'rubygems'
|
|
|
|
require 'gpgme'
|
|
|
|
require 'csv'
|
|
|
|
require 'yaml'
|
|
|
|
|
|
|
|
class MPW
|
|
|
|
|
2013-08-31 19:57:27 +00:00
|
|
|
ID = 0
|
|
|
|
NAME = 1
|
|
|
|
GROUP = 2
|
|
|
|
PROTOCOL = 3
|
|
|
|
SERVER = 4
|
|
|
|
LOGIN = 5
|
|
|
|
PASSWORD = 6
|
|
|
|
PORT = 7
|
|
|
|
COMMENT = 8
|
2013-08-26 18:52:18 +00:00
|
|
|
|
2013-07-17 20:31:28 +00:00
|
|
|
attr_accessor :error_msg
|
2013-09-08 18:04:31 +00:00
|
|
|
attr_accessor :timeout_pwd
|
2013-07-17 20:31:28 +00:00
|
|
|
|
|
|
|
# Constructor
|
|
|
|
def initialize()
|
|
|
|
@file_config = "#{Dir.home()}/.mpw.cfg"
|
|
|
|
@error_mgs = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a new config file
|
|
|
|
# @args: key -> the gpg key to encrypt
|
|
|
|
# file_gpg -> the file who is encrypted
|
|
|
|
# file_pwd -> the file who stock the password
|
|
|
|
# timeout_pwd -> time to save the password
|
|
|
|
# @rtrn: true if le config file is create
|
2013-09-08 18:04:31 +00:00
|
|
|
def setup(key, file_gpg, timeout_pwd)
|
2013-07-17 20:31:28 +00:00
|
|
|
|
|
|
|
if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/
|
|
|
|
@error_msg = "The key string isn't in good format!"
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if file_gpg.empty?
|
|
|
|
file_gpg = "#{Dir.home()}/.mpw.gpg"
|
|
|
|
end
|
|
|
|
|
2013-09-08 18:04:31 +00:00
|
|
|
timeout_pwd.empty? ? (timeout_pwd = 60) : (timeout_pwd = timeout_pwd.to_i)
|
2013-07-17 20:31:28 +00:00
|
|
|
|
|
|
|
config = {'config' => {'key' => key,
|
|
|
|
'file_gpg' => file_gpg,
|
2013-09-08 18:04:31 +00:00
|
|
|
'timeout_pwd' => timeout_pwd}}
|
2013-07-17 20:31:28 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
File.open(@file_config, 'w') do |file|
|
|
|
|
file << config.to_yaml
|
|
|
|
end
|
2013-09-04 20:41:56 +00:00
|
|
|
rescue Exception => e
|
|
|
|
@error_msg = "Can't write the config file!\n#{e}"
|
2013-07-17 20:31:28 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check the config file
|
|
|
|
# @rtrn: true if the config file is correct
|
|
|
|
def checkconfig()
|
|
|
|
begin
|
|
|
|
config = YAML::load_file(@file_config)
|
|
|
|
@key = config['config']['key']
|
|
|
|
@file_gpg = config['config']['file_gpg']
|
|
|
|
@timeout_pwd = config['config']['timeout_pwd'].to_i
|
|
|
|
|
2013-09-08 18:04:31 +00:00
|
|
|
if @key.empty? || @file_gpg.empty?
|
2013-08-31 21:20:59 +00:00
|
|
|
@error_msg = "Checkconfig failed!"
|
2013-07-17 20:31:28 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2013-09-04 20:41:56 +00:00
|
|
|
rescue Exception => e
|
|
|
|
@error_msg = "Checkconfig failed!\n#{e}"
|
2013-07-17 20:31:28 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Decrypt a gpg file
|
|
|
|
# @args: password -> the GPG key password
|
2013-08-25 08:07:39 +00:00
|
|
|
# @rtrn: true if data has been decrypted
|
2013-07-17 20:31:28 +00:00
|
|
|
def decrypt(passwd=nil)
|
2013-08-31 19:57:27 +00:00
|
|
|
@data = Array.new
|
2013-07-17 20:31:28 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
if File.exist?(@file_gpg)
|
|
|
|
crypto = GPGME::Crypto.new(:armor => true)
|
2013-08-31 19:57:27 +00:00
|
|
|
data_decrypt = crypto.decrypt(IO.read(@file_gpg), :password => passwd).read
|
|
|
|
|
|
|
|
id = 0
|
|
|
|
data_decrypt.lines do |line|
|
2013-09-19 20:18:30 +00:00
|
|
|
@data[id] = line.parse_csv.unshift(id)
|
2013-08-31 19:57:27 +00:00
|
|
|
id += 1;
|
|
|
|
end
|
2013-07-17 20:31:28 +00:00
|
|
|
end
|
2013-08-31 21:20:59 +00:00
|
|
|
|
2013-07-17 20:31:28 +00:00
|
|
|
return true
|
2013-09-04 20:41:56 +00:00
|
|
|
rescue Exception => e
|
2013-07-17 22:17:24 +00:00
|
|
|
if !@file_pwd.nil? && File.exist?(@file_pwd)
|
2013-07-17 20:31:28 +00:00
|
|
|
File.delete(@file_pwd)
|
|
|
|
end
|
|
|
|
|
2013-09-04 20:41:56 +00:00
|
|
|
@error_msg = "Can't decrypt file!\n#{e}"
|
2013-07-17 20:31:28 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Encrypt a file
|
2013-08-25 08:07:39 +00:00
|
|
|
# @rtrn: true if the file has been encrypted
|
2013-07-17 20:31:28 +00:00
|
|
|
def encrypt()
|
|
|
|
begin
|
|
|
|
crypto = GPGME::Crypto.new(:armor => true)
|
|
|
|
file_gpg = File.open(@file_gpg, 'w+')
|
2013-08-31 19:57:27 +00:00
|
|
|
|
|
|
|
data_to_encrypt = ''
|
|
|
|
@data.each do |row|
|
|
|
|
row.shift
|
2013-09-19 20:18:30 +00:00
|
|
|
data_to_encrypt << row.to_csv
|
2013-08-31 19:57:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
crypto.encrypt(data_to_encrypt, :recipients => @key, :output => file_gpg)
|
2013-07-17 20:31:28 +00:00
|
|
|
file_gpg.close
|
|
|
|
|
|
|
|
return true
|
2013-09-04 20:41:56 +00:00
|
|
|
rescue Exception => e
|
|
|
|
@error_msg = "Can't encrypt the GPG file!\n#{e}"
|
2013-07-17 20:31:28 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Search in some csv data
|
|
|
|
# @args: search -> the string to search
|
2013-09-07 09:37:40 +00:00
|
|
|
# protocol -> the connection protocol (ssh, web, other)
|
2013-07-17 20:31:28 +00:00
|
|
|
# @rtrn: a list with the resultat of the search
|
2013-09-09 19:48:42 +00:00
|
|
|
def search(search, group=nil, protocol=nil)
|
2013-07-17 20:31:28 +00:00
|
|
|
result = Array.new()
|
2013-08-31 19:57:27 +00:00
|
|
|
@data.each do |row|
|
2013-09-09 19:48:42 +00:00
|
|
|
if row[NAME] =~ /^.*#{search}.*$/ || row[SERVER] =~ /^.*#{search}.*$/ || row[COMMENT] =~ /^.*#{search}.*$/
|
|
|
|
if (protocol.nil? || protocol.eql?(row[PROTOCOL])) && (group.nil? || group.eql?(row[GROUP]))
|
2013-07-17 20:31:28 +00:00
|
|
|
result.push(row)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
# Search in some csv data
|
|
|
|
# @args: id -> the id item
|
|
|
|
# @rtrn: a row with the resultat of the search
|
|
|
|
def searchById(id)
|
2013-08-31 19:57:27 +00:00
|
|
|
if not @data[id.to_i].nil?
|
|
|
|
return @data[id.to_i]
|
|
|
|
else
|
|
|
|
return Array.new
|
2013-07-17 20:31:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add a new item
|
2013-08-26 18:52:18 +00:00
|
|
|
# @args: name -> the item name
|
|
|
|
# group -> the item group
|
|
|
|
# server -> the ip or server
|
2013-07-17 20:31:28 +00:00
|
|
|
# protocol -> the protocol
|
|
|
|
# login -> the login
|
|
|
|
# passwd -> the password
|
|
|
|
# port -> the port
|
|
|
|
# comment -> a comment
|
2013-08-26 18:52:18 +00:00
|
|
|
# @rtrn: true if it works
|
|
|
|
def add(name, group=nil, server=nil, protocol=nil, login=nil, passwd=nil, port=nil, comment=nil)
|
2013-07-17 20:31:28 +00:00
|
|
|
row = Array.new()
|
2013-08-26 18:52:18 +00:00
|
|
|
|
|
|
|
if name.nil? || name.empty?
|
|
|
|
@error_msg = "You must define a name!"
|
|
|
|
return false
|
|
|
|
end
|
2013-09-07 09:37:40 +00:00
|
|
|
|
|
|
|
if port.to_i <= 0
|
|
|
|
port = nil
|
|
|
|
end
|
2013-08-31 19:57:27 +00:00
|
|
|
|
|
|
|
if not @data.last.nil?
|
|
|
|
id = @data.last
|
|
|
|
id = id[ID].to_i + 1
|
|
|
|
else
|
|
|
|
id = 0
|
2013-08-26 18:52:18 +00:00
|
|
|
end
|
2013-07-17 20:31:28 +00:00
|
|
|
|
2013-08-31 20:10:29 +00:00
|
|
|
row[ID] = id
|
|
|
|
row[PORT] = port
|
|
|
|
row[NAME] = name.force_encoding('ASCII-8BIT')
|
2013-09-20 09:59:57 +00:00
|
|
|
group.nil? || group.empty? ? (row[GROUP] = nil) : (row[GROUP] = group.force_encoding('ASCII-8BIT'))
|
|
|
|
server.nil? || server.empty? ? (row[SERVER] = nil) : (row[SERVER] = server.force_encoding('ASCII-8BIT'))
|
|
|
|
protocol.nil? || protocol.empty? ? (row[PROTOCOL] = nil) : (row[PROTOCOL] = protocol.force_encoding('ASCII-8BIT'))
|
|
|
|
login.nil? || login.empty? ? (row[LOGIN] = nil) : (row[LOGIN] = login.force_encoding('ASCII-8BIT'))
|
|
|
|
passwd.nil? || passwd.empty? ? (row[PASSWORD] = nil) : (row[PASSWORD] = passwd.force_encoding('ASCII-8BIT'))
|
|
|
|
comment.nil? || comment.empty? ? (row[COMMENT] = nil) : (row[COMMENT] = comment.force_encoding('ASCII-8BIT'))
|
2013-07-17 20:31:28 +00:00
|
|
|
|
2013-08-31 19:57:27 +00:00
|
|
|
@data[id] = row
|
2013-08-26 18:52:18 +00:00
|
|
|
|
|
|
|
return true
|
2013-07-17 20:31:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Update an item
|
|
|
|
# @args: id -> the item's identifiant
|
2013-08-26 18:52:18 +00:00
|
|
|
# name -> the item name
|
|
|
|
# group -> the item group
|
|
|
|
# server -> the ip or hostname
|
2013-07-17 20:31:28 +00:00
|
|
|
# protocol -> the protocol
|
|
|
|
# login -> the login
|
|
|
|
# passwd -> the password
|
|
|
|
# port -> the port
|
|
|
|
# comment -> a comment
|
|
|
|
# @rtrn: true if the item has been updated
|
2013-08-26 18:52:18 +00:00
|
|
|
def update(id, name=nil, group=nil, server=nil, protocol=nil, login=nil, passwd=nil, port=nil, comment=nil)
|
2013-08-31 19:57:27 +00:00
|
|
|
id = id.to_i
|
|
|
|
|
|
|
|
if not @data[id].nil?
|
2013-09-07 09:37:40 +00:00
|
|
|
|
|
|
|
if port.to_i <= 0
|
|
|
|
port = nil
|
|
|
|
end
|
|
|
|
|
2013-08-31 19:57:27 +00:00
|
|
|
row = @data[id]
|
|
|
|
row_update = Array.new()
|
|
|
|
|
2013-09-20 09:59:57 +00:00
|
|
|
name.nil? || name.empty? ? (row_update[NAME] = row[NAME]) : (row_update[NAME] = name)
|
|
|
|
group.nil? || group.empty? ? (row_update[GROUP] = row[GROUP]) : (row_update[GROUP] = group)
|
|
|
|
server.nil? || server.empty? ? (row_update[SERVER] = row[SERVER]) : (row_update[SERVER] = server)
|
|
|
|
protocol.nil? || protocol.empty? ? (row_update[PROTOCOL] = row[PROTOCOL]) : (row_update[PROTOCOL] = protocol)
|
|
|
|
login.nil? || login.empty? ? (row_update[LOGIN] = row[LOGIN]) : (row_update[LOGIN] = login)
|
|
|
|
passwd.nil? || passwd.empty? ? (row_update[PASSWORD] = row[PASSWORD]) : (row_update[PASSWORD] = passwd)
|
|
|
|
port.nil? || port.empty? ? (row_update[PORT] = row[PORT]) : (row_update[PORT] = port)
|
2013-09-20 10:13:02 +00:00
|
|
|
comment.nil? || comment.empty? ? (row_update[COMMENT] = row[COMMENT]) : (row_update[COMMENT] = comment)
|
|
|
|
|
2013-08-31 19:57:27 +00:00
|
|
|
@data[id] = row_update
|
2013-07-17 20:31:28 +00:00
|
|
|
|
2013-08-31 19:57:27 +00:00
|
|
|
return true
|
|
|
|
else
|
|
|
|
@error_msg = "Can't update the item, the item #{id} doesn't exist!"
|
|
|
|
return false
|
2013-07-17 20:31:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Remove an item
|
|
|
|
# @args: id -> the item's identifiant
|
|
|
|
# @rtrn: true if the item has been deleted
|
|
|
|
def remove(id)
|
2013-08-31 19:57:27 +00:00
|
|
|
if not @data.delete_at(id.to_i).nil?
|
|
|
|
return true
|
|
|
|
else
|
2013-08-31 21:10:07 +00:00
|
|
|
@error_msg = "Can't delete the item, the item #{id} doesn't exist!"
|
2013-08-31 19:57:27 +00:00
|
|
|
return false
|
2013-07-17 20:31:28 +00:00
|
|
|
end
|
|
|
|
end
|
2013-07-25 17:51:43 +00:00
|
|
|
|
|
|
|
# Export to csv
|
|
|
|
# @args: file -> a string to match
|
|
|
|
# @rtrn: true if export work
|
|
|
|
def export(file)
|
|
|
|
begin
|
2013-08-31 19:57:27 +00:00
|
|
|
File.open(file, 'w+') do |file|
|
|
|
|
@data.each do |row|
|
|
|
|
row.delete_at(ID)
|
2013-09-19 20:18:30 +00:00
|
|
|
file << row.to_csv
|
2013-08-31 19:57:27 +00:00
|
|
|
end
|
2013-07-25 17:51:43 +00:00
|
|
|
end
|
2013-08-31 21:20:59 +00:00
|
|
|
|
2013-07-25 17:51:43 +00:00
|
|
|
return true
|
2013-09-04 20:41:56 +00:00
|
|
|
rescue Exception => e
|
|
|
|
@error_msg = "Can't export, impossible to write in #{file}!\n#{e}"
|
2013-07-25 17:51:43 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Import to csv
|
2013-08-26 20:19:37 +00:00
|
|
|
# @args: file -> path to file import
|
2013-07-25 17:51:43 +00:00
|
|
|
# @rtrn: true if the import work
|
|
|
|
def import(file)
|
|
|
|
begin
|
|
|
|
data_new = IO.read(file)
|
|
|
|
data_new.lines do |line|
|
2013-09-19 20:18:30 +00:00
|
|
|
if not line =~ /(.*,){6}/
|
2013-07-25 17:51:43 +00:00
|
|
|
@error_msg = "Can't import, the file is bad format!"
|
|
|
|
return false
|
2013-08-31 19:57:27 +00:00
|
|
|
else
|
2013-09-19 20:18:30 +00:00
|
|
|
row = line.parse_csv.unshift(0)
|
2013-08-31 19:57:27 +00:00
|
|
|
if not add(row[NAME], row[GROUP], row[SERVER], row[PROTOCOL], row[LOGIN], row[PASSWORD], row[PORT], row[COMMENT])
|
|
|
|
return false
|
|
|
|
end
|
2013-07-25 17:51:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
2013-09-04 20:41:56 +00:00
|
|
|
rescue Exception => e
|
|
|
|
@error_msg = "Can't import, impossible to read #{file}!\n#{e}"
|
2013-07-25 17:51:43 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2013-08-26 20:19:37 +00:00
|
|
|
|
|
|
|
# Return
|
|
|
|
# @args: file -> path to file import
|
|
|
|
# @rtrn: an array with the items to import, if there is an error return false
|
|
|
|
def importPreview(file)
|
|
|
|
begin
|
|
|
|
result = Array.new()
|
2013-08-31 20:14:51 +00:00
|
|
|
id = 0
|
2013-08-26 20:19:37 +00:00
|
|
|
|
|
|
|
data = IO.read(file)
|
|
|
|
data.lines do |line|
|
2013-09-19 20:18:30 +00:00
|
|
|
if not line =~ /(.*,){6}/
|
2013-08-26 20:19:37 +00:00
|
|
|
@error_msg = "Can't import, the file is bad format!"
|
|
|
|
return false
|
|
|
|
else
|
2013-09-19 20:18:30 +00:00
|
|
|
result.push(line.parse_csv.unshift(id))
|
2013-08-26 20:19:37 +00:00
|
|
|
end
|
2013-08-31 20:14:51 +00:00
|
|
|
|
|
|
|
id += 1
|
2013-08-26 20:19:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
2013-09-04 20:41:56 +00:00
|
|
|
rescue Exception => e
|
|
|
|
@error_msg = "Can't import, impossible to read #{file}!\n#{e}"
|
2013-08-26 20:19:37 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2013-07-17 20:31:28 +00:00
|
|
|
|
|
|
|
end
|