2014-01-29 19:49:39 +00:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
# author: nishiki
|
|
|
|
# mail: nishiki@yaegashi.fr
|
|
|
|
# info: a simple script who manage your passwords
|
|
|
|
|
|
|
|
module MPW
|
|
|
|
|
|
|
|
require 'rubygems'
|
2014-03-16 20:28:23 +00:00
|
|
|
require 'gpgme'
|
2014-01-29 19:49:39 +00:00
|
|
|
require 'yaml'
|
|
|
|
require 'i18n'
|
|
|
|
|
|
|
|
class Config
|
|
|
|
|
|
|
|
attr_accessor :error_msg
|
|
|
|
|
|
|
|
attr_accessor :key
|
2014-02-02 13:20:24 +00:00
|
|
|
attr_accessor :share_keys
|
2014-01-29 19:49:39 +00:00
|
|
|
attr_accessor :lang
|
|
|
|
attr_accessor :file_gpg
|
|
|
|
attr_accessor :last_update
|
2014-01-30 22:08:38 +00:00
|
|
|
attr_accessor :sync_type
|
2014-01-29 19:49:39 +00:00
|
|
|
attr_accessor :sync_host
|
|
|
|
attr_accessor :sync_port
|
2014-01-30 22:08:38 +00:00
|
|
|
attr_accessor :sync_user
|
2014-01-29 19:49:39 +00:00
|
|
|
attr_accessor :sync_pwd
|
2014-01-30 22:08:38 +00:00
|
|
|
attr_accessor :sync_path
|
2014-01-29 19:49:39 +00:00
|
|
|
attr_accessor :last_update
|
2014-04-27 16:53:20 +00:00
|
|
|
attr_accessor :dir_config
|
2014-01-29 19:49:39 +00:00
|
|
|
|
|
|
|
# Constructor
|
|
|
|
# @args: file_config -> the specify config file
|
|
|
|
def initialize(file_config=nil)
|
2014-04-27 16:53:20 +00:00
|
|
|
@error_msg = nil
|
|
|
|
|
|
|
|
if /darwin/ =~ RUBY_PLATFORM
|
|
|
|
@dir_config = "#{Dir.home}/Library/Preferences/mpw"
|
|
|
|
elsif /cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM
|
|
|
|
@dir_config = "#{Dir.home}/AppData/Local/mpw"
|
|
|
|
else
|
|
|
|
@dir_config = "#{Dir.home}/.config/mpw"
|
|
|
|
end
|
|
|
|
|
|
|
|
@file_config = "#{@dir_config}/conf/default.cfg"
|
2014-11-16 18:39:38 +00:00
|
|
|
if not file_config.nil? and not file_config.empty?
|
2014-01-29 19:49:39 +00:00
|
|
|
@file_config = file_config
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a new config file
|
|
|
|
# @args: key -> the gpg key to encrypt
|
2014-02-02 13:20:24 +00:00
|
|
|
# share_keys -> multiple keys to share the password with other people
|
2014-01-29 19:49:39 +00:00
|
|
|
# lang -> the software language
|
|
|
|
# file_gpg -> the file who is encrypted
|
2014-01-30 22:08:38 +00:00
|
|
|
# sync_type -> the type to synchronization
|
2014-01-29 19:49:39 +00:00
|
|
|
# sync_host -> the server host for synchronization
|
|
|
|
# sync_port -> the server port for synchronization
|
2014-01-30 22:08:38 +00:00
|
|
|
# sync_user -> the user for synchronization
|
2014-01-29 19:49:39 +00:00
|
|
|
# sync_pwd -> the password for synchronization
|
|
|
|
# sync_suffix -> the suffix file (optionnal)
|
|
|
|
# @rtrn: true if le config file is create
|
2014-12-06 22:40:09 +00:00
|
|
|
def setup(key, share_keys, lang, file_gpg, sync_type, sync_host, sync_port, sync_user, sync_pwd, sync_path)
|
2014-01-29 19:49:39 +00:00
|
|
|
|
|
|
|
if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/
|
|
|
|
@error_msg = I18n.t('error.config.key_bad_format')
|
|
|
|
return false
|
|
|
|
end
|
2014-02-02 13:20:24 +00:00
|
|
|
|
2014-11-16 18:39:38 +00:00
|
|
|
if not check_public_gpg_key(share_keys)
|
2014-04-21 20:30:41 +00:00
|
|
|
return false
|
2014-02-02 13:20:24 +00:00
|
|
|
end
|
2014-01-29 19:49:39 +00:00
|
|
|
|
|
|
|
if file_gpg.empty?
|
2014-04-27 16:53:20 +00:00
|
|
|
file_gpg = "#{@dir_config}/db/default.gpg"
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
config = {'config' => {'key' => key,
|
2014-02-02 13:20:24 +00:00
|
|
|
'share_keys' => share_keys,
|
2014-01-29 19:49:39 +00:00
|
|
|
'lang' => lang,
|
|
|
|
'file_gpg' => file_gpg,
|
2014-01-30 22:08:38 +00:00
|
|
|
'sync_type' => sync_type,
|
2014-01-29 19:49:39 +00:00
|
|
|
'sync_host' => sync_host,
|
|
|
|
'sync_port' => sync_port,
|
2014-01-30 22:08:38 +00:00
|
|
|
'sync_user' => sync_user,
|
2014-01-29 19:49:39 +00:00
|
|
|
'sync_pwd' => sync_pwd,
|
2014-01-30 22:08:38 +00:00
|
|
|
'sync_path' => sync_path,
|
2014-08-31 10:42:26 +00:00
|
|
|
'last_update' => 0
|
|
|
|
}
|
|
|
|
}
|
2014-01-29 19:49:39 +00:00
|
|
|
|
2014-04-27 16:53:20 +00:00
|
|
|
Dir.mkdir("#{@config_dir}/conf", 700)
|
|
|
|
Dir.mkdir("#{@config_dir}/db", 700)
|
2014-01-30 22:08:38 +00:00
|
|
|
File.open(@file_config, 'w') do |file|
|
|
|
|
file << config.to_yaml
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
2014-01-30 22:08:38 +00:00
|
|
|
|
2014-01-29 19:49:39 +00:00
|
|
|
return true
|
2014-01-30 22:08:38 +00:00
|
|
|
rescue Exception => e
|
|
|
|
@error_msg = "#{I18n.t('error.config.write')}\n#{e}"
|
|
|
|
return false
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
2014-03-16 20:28:23 +00:00
|
|
|
|
|
|
|
# Setup a new gpg key
|
2014-03-18 20:08:09 +00:00
|
|
|
# @args: password -> the GPG key password
|
|
|
|
# name -> the name of user
|
|
|
|
# length -> length of the GPG key
|
|
|
|
# expire -> the time of expire to GPG key
|
|
|
|
# @rtrn: true if the GPG key is create, else false
|
2014-03-16 20:28:23 +00:00
|
|
|
def setup_gpg_key(password, name, length = 2048, expire = 0)
|
2014-11-16 18:39:38 +00:00
|
|
|
if name.nil? or name.empty?
|
2014-03-16 20:28:23 +00:00
|
|
|
@error_msg = "#{I18n.t('error.config.genkey_gpg.name')}"
|
|
|
|
return false
|
2014-11-16 18:39:38 +00:00
|
|
|
elsif password.nil? or password.empty?
|
2014-03-16 20:28:23 +00:00
|
|
|
@error_msg = "#{I18n.t('error.config.genkey_gpg.password')}"
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
param = ''
|
|
|
|
param << '<GnupgKeyParms format="internal">' + "\n"
|
|
|
|
param << "Key-Type: DSA\n"
|
|
|
|
param << "Key-Length: #{length}\n"
|
|
|
|
param << "Subkey-Type: ELG-E\n"
|
|
|
|
param << "Subkey-Length: #{length}\n"
|
|
|
|
param << "Name-Real: #{name}\n"
|
|
|
|
param << "Name-Comment: #{name}\n"
|
|
|
|
param << "Name-Email: #{@key}\n"
|
|
|
|
param << "Expire-Date: #{expire}\n"
|
2014-04-21 20:30:41 +00:00
|
|
|
param << "Passphrase: #{password}\n"
|
2014-03-16 20:28:23 +00:00
|
|
|
param << "</GnupgKeyParms>\n"
|
|
|
|
|
|
|
|
ctx = GPGME::Ctx.new
|
|
|
|
ctx.genkey(param, nil, nil)
|
|
|
|
|
|
|
|
return true
|
|
|
|
rescue Exception => e
|
|
|
|
@error_msg = "#{I18n.t('error.config.genkey_gpg.exception')}\n#{e}"
|
|
|
|
return false
|
|
|
|
end
|
2014-01-29 19:49:39 +00:00
|
|
|
|
|
|
|
# Check the config file
|
|
|
|
# @rtrn: true if the config file is correct
|
2014-02-02 09:16:10 +00:00
|
|
|
def checkconfig
|
2014-01-30 22:08:38 +00:00
|
|
|
config = YAML::load_file(@file_config)
|
|
|
|
@key = config['config']['key']
|
2014-02-02 13:20:24 +00:00
|
|
|
@share_keys = config['config']['share_keys']
|
2014-01-30 22:08:38 +00:00
|
|
|
@lang = config['config']['lang']
|
|
|
|
@file_gpg = config['config']['file_gpg']
|
|
|
|
@sync_type = config['config']['sync_type']
|
|
|
|
@sync_host = config['config']['sync_host']
|
|
|
|
@sync_port = config['config']['sync_port']
|
|
|
|
@sync_user = config['config']['sync_user']
|
|
|
|
@sync_pwd = config['config']['sync_pwd']
|
|
|
|
@sync_path = config['config']['sync_path']
|
|
|
|
@last_update = config['config']['last_update'].to_i
|
|
|
|
|
2014-11-16 18:39:38 +00:00
|
|
|
if @key.empty? or @file_gpg.empty?
|
2014-01-30 22:08:38 +00:00
|
|
|
@error_msg = I18n.t('error.config.check')
|
2014-01-29 19:49:39 +00:00
|
|
|
return false
|
|
|
|
end
|
2014-01-30 22:08:38 +00:00
|
|
|
I18n.locale = @lang.to_sym
|
|
|
|
|
2014-01-29 19:49:39 +00:00
|
|
|
return true
|
2014-01-30 22:08:38 +00:00
|
|
|
rescue Exception => e
|
2014-03-16 20:28:23 +00:00
|
|
|
puts e
|
2014-01-30 22:08:38 +00:00
|
|
|
@error_msg = "#{I18n.t('error.config.check')}\n#{e}"
|
|
|
|
return false
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
2014-03-16 20:28:23 +00:00
|
|
|
|
|
|
|
# Check if private key exist
|
|
|
|
# @rtrn: true if the key exist, else false
|
2014-04-21 20:30:41 +00:00
|
|
|
def check_gpg_key?
|
2014-03-16 20:28:23 +00:00
|
|
|
ctx = GPGME::Ctx.new
|
2014-12-29 12:59:17 +00:00
|
|
|
ctx.each_key(@key, true) do
|
2014-03-16 20:28:23 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
2014-04-21 20:30:41 +00:00
|
|
|
|
|
|
|
# Check if private key exist
|
|
|
|
# @args: share_keys -> string with all public keys
|
|
|
|
# @rtrn: true if the key exist, else false
|
|
|
|
def check_public_gpg_key(share_keys = @share_keys)
|
|
|
|
ctx = GPGME::Ctx.new
|
|
|
|
|
|
|
|
share_keys = share_keys.nil? ? '' : share_keys
|
2014-11-16 18:39:38 +00:00
|
|
|
if not share_keys.empty?
|
2014-04-21 20:30:41 +00:00
|
|
|
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')
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
ctx.each_key(key, false) do
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2014-08-31 10:42:26 +00:00
|
|
|
@error_msg = I18n.t('error.config.no_key_public', key: k)
|
2014-04-21 20:30:41 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
2014-01-29 19:49:39 +00:00
|
|
|
|
|
|
|
# Set the last update when there is a sync
|
|
|
|
# @rtrn: true is the file has been updated
|
2014-01-30 22:08:38 +00:00
|
|
|
def set_last_update
|
2014-01-29 19:49:39 +00:00
|
|
|
config = {'config' => {'key' => @key,
|
2014-02-02 13:20:24 +00:00
|
|
|
'share_keys' => @share_keys,
|
2014-01-29 19:49:39 +00:00
|
|
|
'lang' => @lang,
|
|
|
|
'file_gpg' => @file_gpg,
|
2014-01-30 22:08:38 +00:00
|
|
|
'sync_type' => @sync_type,
|
2014-01-29 19:49:39 +00:00
|
|
|
'sync_host' => @sync_host,
|
|
|
|
'sync_port' => @sync_port,
|
2014-01-30 22:08:38 +00:00
|
|
|
'sync_user' => @sync_user,
|
2014-01-29 19:49:39 +00:00
|
|
|
'sync_pwd' => @sync_pwd,
|
2014-01-30 22:08:38 +00:00
|
|
|
'sync_path' => @sync_path,
|
2014-08-31 10:42:26 +00:00
|
|
|
'last_update' => Time.now.to_i
|
|
|
|
}
|
|
|
|
}
|
2014-01-29 19:49:39 +00:00
|
|
|
|
2014-01-30 22:08:38 +00:00
|
|
|
File.open(@file_config, 'w') do |file|
|
|
|
|
file << config.to_yaml
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
2014-01-30 22:08:38 +00:00
|
|
|
|
2014-01-29 19:49:39 +00:00
|
|
|
return true
|
2014-01-30 22:08:38 +00:00
|
|
|
rescue Exception => e
|
|
|
|
@error_msg = "#{I18n.t('error.config.write')}\n#{e}"
|
|
|
|
return false
|
2014-01-29 19:49:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|