lib/mpw/config.rb in mpw-2.0.3 vs lib/mpw/config.rb in mpw-3.0.0

- old
+ new

@@ -1,231 +1,142 @@ #!/usr/bin/ruby -# author: nishiki -# mail: nishiki@yaegashi.fr +# MPW is a software to crypt and manage your passwords +# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im> +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -require 'rubygems' require 'gpgme' require 'yaml' require 'i18n' +require 'fileutils' module MPW - class Config - - attr_accessor :error_msg +class Config - attr_accessor :key - attr_accessor :share_keys - attr_accessor :lang - attr_accessor :file_gpg - attr_accessor :last_update - attr_accessor :sync_type - attr_accessor :sync_host - attr_accessor :sync_port - attr_accessor :sync_user - attr_accessor :sync_pwd - attr_accessor :sync_path - attr_accessor :last_sync - attr_accessor :dir_config - - # Constructor - # @args: file_config -> the specify config file - def initialize(file_config=nil) - @error_msg = nil + attr_accessor :error_msg - 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" - if not file_config.nil? and not file_config.empty? - @file_config = file_config - end - end - - # Create a new config file - # @args: key -> the gpg key to encrypt - # share_keys -> multiple keys to share the password with other people - # lang -> the software language - # file_gpg -> the file who is encrypted - # sync_type -> the type to synchronization - # sync_host -> the server host for synchronization - # sync_port -> the server port for synchronization - # sync_user -> the user for synchronization - # sync_pwd -> the password for synchronization - # sync_suffix -> the suffix file (optionnal) - # @rtrn: true if le config file is create - def setup(key, share_keys, lang, file_gpg, sync_type, sync_host, sync_port, sync_user, sync_pwd, sync_path) - - 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 + attr_accessor :key + attr_accessor :lang + attr_accessor :config_dir + attr_accessor :wallet_dir - if not check_public_gpg_key(share_keys) - return false - end - - if file_gpg.empty? - file_gpg = "#{@dir_config}/db/default.gpg" - end - - config = {'config' => {'key' => key, - 'share_keys' => share_keys, - 'lang' => lang, - 'file_gpg' => file_gpg, - 'sync_type' => sync_type, - 'sync_host' => sync_host, - 'sync_port' => sync_port, - 'sync_user' => sync_user, - 'sync_pwd' => sync_pwd, - 'sync_path' => sync_path, - 'last_sync' => 0 - } - } - - Dir.mkdir("#{@config_dir}/conf", 700) - Dir.mkdir("#{@config_dir}/db", 700) - File.open(@file_config, 'w') do |file| - file << config.to_yaml - end - - return true - rescue Exception => e - @error_msg = "#{I18n.t('error.config.write')}\n#{e}" - return false + # Constructor + # @args: config_file -> the specify config file + def initialize(config_file=nil) + @config_file = config_file + + if /darwin/ =~ RUBY_PLATFORM + @config_dir = "#{Dir.home}/Library/Preferences/mpw" + elsif /cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM + @config_dir = "#{Dir.home}/AppData/Local/mpw" + else + @config_dir = "#{Dir.home}/.config/mpw" end + + if @config_file.nil? or @config_file.empty? + @config_file = "#{@config_dir}/mpw.cfg" + end + end - # Setup a new gpg key - # @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 - def setup_gpg_key(password, name, length = 2048, expire = 0) - if name.nil? or name.empty? - @error_msg = "#{I18n.t('error.config.genkey_gpg.name')}" - return false - elsif password.nil? or password.empty? - @error_msg = "#{I18n.t('error.config.genkey_gpg.password')}" - return false - end + # Create a new config file + # @args: key -> the gpg key to encrypt + # lang -> the software language + # wallet_dir -> the directory where are the wallets password + # @rtrn: true if le config file is create + def setup(key, lang, wallet_dir) - 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" - param << "Passphrase: #{password}\n" - param << "</GnupgKeyParms>\n" + if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/ + raise I18n.t('error.config.key_bad_format') + end - 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 + if wallet_dir.empty? + wallet_dir = "#{@config_dir}/wallets" end - - # Check the config file - # @rtrn: true if the config file is correct - def checkconfig - config = YAML::load_file(@file_config) - @key = config['config']['key'] - @share_keys = config['config']['share_keys'] - @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_sync = config['config']['last_sync'].to_i - if @key.empty? or @file_gpg.empty? - @error_msg = I18n.t('error.config.check') - return false - end - I18n.locale = @lang.to_sym + config = {'config' => {'key' => key, + 'lang' => lang, + 'wallet_dir' => wallet_dir, + } + } - return true - rescue Exception => e - @error_msg = "#{I18n.t('error.config.check')}\n#{e}" - return false + FileUtils.mkdir_p(wallet_dir, mode: 0700) + File.open(@config_file, 'w') do |file| + file << config.to_yaml end + + rescue Exception => e + raise "#{I18n.t('error.config.write')}\n#{e}" + end - # Check if private key exist - # @rtrn: true if the key exist, else false - def check_gpg_key? - ctx = GPGME::Ctx.new - ctx.each_key(@key, true) do - return true - end - - return false + # Setup a new gpg key + # @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 + def setup_gpg_key(password, name, length = 4096, expire = 0) + if name.nil? or name.empty? + raise "#{I18n.t('error.config.genkey_gpg.name')}" + elsif password.nil? or password.empty? + raise "#{I18n.t('error.config.genkey_gpg.password')}" end - # 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 + 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" + param << "Passphrase: #{password}\n" + param << "</GnupgKeyParms>\n" - share_keys = share_keys.nil? ? '' : share_keys - if not share_keys.empty? - 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 + ctx = GPGME::Ctx.new + ctx.genkey(param, nil, nil) + rescue Exception => e + raise "#{I18n.t('error.config.genkey_gpg.exception')}\n#{e}" + end - @error_msg = I18n.t('error.config.no_key_public', key: k) - return false - end - end + # Check the config file + # @rtrn: true if the config file is correct + def is_valid? + config = YAML::load_file(@config_file) + @key = config['config']['key'] + @lang = config['config']['lang'] + @wallet_dir = config['config']['wallet_dir'] - return true - end - - # Set the last update when there is a sync - # @rtrn: true is the file has been updated - def set_last_sync - config = {'config' => {'key' => @key, - 'share_keys' => @share_keys, - 'lang' => @lang, - 'file_gpg' => @file_gpg, - 'sync_type' => @sync_type, - 'sync_host' => @sync_host, - 'sync_port' => @sync_port, - 'sync_user' => @sync_user, - 'sync_pwd' => @sync_pwd, - 'sync_path' => @sync_path, - 'last_sync' => Time.now.to_i - } - } - - File.open(@file_config, 'w') do |file| - file << config.to_yaml - end + raise if @key.empty? or @wallet_dir.empty? + + I18n.locale = @lang.to_sym + return true + rescue + return false + end + + # Check if private key exist + # @rtrn: true if the key exist, else false + def check_gpg_key? + ctx = GPGME::Ctx.new + ctx.each_key(@key, true) do return true - rescue Exception => e - @error_msg = "#{I18n.t('error.config.write')}\n#{e}" - return false end - + + return false end +end end