diff --git a/bin/mpw-config b/bin/mpw-config index 4740ed1..67ffc50 100644 --- a/bin/mpw-config +++ b/bin/mpw-config @@ -60,6 +60,14 @@ OptionParser.new do |opts| values[:lang] = lang end + opts.on('-P', '--enable-pinmode', I18n.t('option.pinmode')) do + values[:pinmode] = true + end + + opts.on('-p', '--disable-pinmode', I18n.t('option.disable_pinmode')) do + values[:pinmode] = false + end + opts.on('-w', '--wallet-dir PATH', I18n.t('option.wallet_dir')) do |wallet_dir| values[:wallet_dir] = wallet_dir end @@ -94,7 +102,7 @@ OptionParser.new do |opts| end.parse! config = MPW::Config.new(options[:config]) -cli = MPW::Cli.new(config, nil) +cli = MPW::Cli.new(config) if options.key?(:init) cli.setup(values) diff --git a/i18n/en.yml b/i18n/en.yml index 162f6b3..97f1928 100644 --- a/i18n/en.yml +++ b/i18n/en.yml @@ -49,6 +49,7 @@ en: delete_gpg_key: "Delete the wallet's share with an other GPG key" disable_alpha: "Don't use letter to generate a password" disable_numeric: "Don't use number to generate a password" + disable_pinmode: "Disable the pinentry mode" disable_special_chars: "Don't use special char to generate a password" export: "Export a wallet in an yaml file" file_export: "Specify the file where export data" @@ -68,6 +69,7 @@ en: list_keys: "List the GPG keys in wallet" numeric: "Use number to generate a password" pattern: "Given search pattern" + pinmode: "Enable pinentry mode (available with gpg >= 2.1)" random_password: "Generate a random password" setup: "Create a new configuration file" setup_wallet: "Create a new configuration file for a wallet" diff --git a/i18n/fr.yml b/i18n/fr.yml index b2e3db4..0c7319c 100644 --- a/i18n/fr.yml +++ b/i18n/fr.yml @@ -49,6 +49,7 @@ fr: delete_gpg_key: "Supprime le partage le portefeuille avec une autre clé GPG" disable_alpha: "Désactive l'utilisation des lettres dans la génération d'un mot de passe" disable_numeric: "Désactive l'utilisation des chiffre dans la génération d'un mot de passe" + disable_pinmode: "Désactive le mode pinentry" disable_special_chars: "Désactive l'utilisation des charactères speciaux dans la génération d'un mot de passe" export: "Exporte un portefeuille dans un fichier yaml" file_export: "Spécifie le fichier où exporter les données" @@ -68,6 +69,7 @@ fr: list_keys: "Liste les clés GPG dans le portefeuille" numeric: "Utilise des chiffre dans la génération d'un mot de passe" pattern: "Motif de donnée à chercher" + pinmode: "Active le mode pinentry (valable avec gpg >= 2.1)" random_password: "Génére un mot de passe aléatoire" setup: "Création d'un nouveau fichier de configuration" setup_wallet: "Création d'un nouveau fichier de configuration pour un portefeuille" diff --git a/lib/mpw/cli.rb b/lib/mpw/cli.rb index d4d4027..56e77a1 100644 --- a/lib/mpw/cli.rb +++ b/lib/mpw/cli.rb @@ -101,7 +101,7 @@ module MPW def decrypt unless defined?(@mpw) @password = ask(I18n.t('display.gpg_password')) { |q| q.echo = false } - @mpw = MPW.new(@config.gpg_key, @wallet_file, @password, @config.gpg_exe) + @mpw = MPW.new(@config.gpg_key, @wallet_file, @password, @config.gpg_exe, @config.pinmode) end @mpw.read_data diff --git a/lib/mpw/config.rb b/lib/mpw/config.rb index 3b88b0a..591b991 100644 --- a/lib/mpw/config.rb +++ b/lib/mpw/config.rb @@ -32,6 +32,7 @@ module MPW attr_accessor :wallet_dir attr_accessor :gpg_exe attr_accessor :password + attr_accessor :pinmode # Constructor # @args: config_file -> the specify config file @@ -59,12 +60,13 @@ module MPW wallet_dir = options[:wallet_dir] || @wallet_dir default_wallet = options[:default_wallet] || @default_wallet gpg_exe = options[:gpg_exe] || @gpg_exe + pinmode = options[:pinmode] || @pinmode password = { numeric: true, alpha: true, special: false, length: 16 } - %w(numeric special alpha length).each do |k| + %w[numeric special alpha length].each do |k| if options.key?("pwd_#{k}".to_sym) password[k.to_sym] = options["pwd_#{k}".to_sym] elsif !@password.nil? && @password.key?(k.to_sym) @@ -82,7 +84,8 @@ module MPW 'wallet_dir' => wallet_dir, 'default_wallet' => default_wallet, 'gpg_exe' => gpg_exe, - 'password' => password } + 'password' => password, + 'pinmode' => pinmode } FileUtils.mkdir_p(@config_dir, mode: 0700) FileUtils.mkdir_p(wallet_dir, mode: 0700) @@ -132,6 +135,7 @@ module MPW @default_wallet = config['default_wallet'] @gpg_exe = config['gpg_exe'] @password = config['password'] || {} + @pinmode = config['pinmode'] raise if @gpg_key.empty? || @wallet_dir.empty? diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb index c52f8db..c6a5cf0 100644 --- a/lib/mpw/mpw.rb +++ b/lib/mpw/mpw.rb @@ -26,11 +26,12 @@ require 'mpw/item' module MPW class MPW # Constructor - def initialize(key, wallet_file, gpg_pass = nil, gpg_exe = nil) + def initialize(key, wallet_file, gpg_pass = nil, gpg_exe = nil, pinmode = false) @key = key @gpg_pass = gpg_pass @gpg_exe = gpg_exe @wallet_file = wallet_file + @pinmode = pinmode GPGME::Engine.set_info(GPGME::PROTOCOL_OpenPGP, @gpg_exe, "#{Dir.home}/.gnupg") unless @gpg_exe.to_s.empty? end @@ -313,9 +314,18 @@ module MPW def decrypt(data) return nil if data.to_s.empty? - crypto = GPGME::Crypto.new(armor: true) + password = + if /^(1\.[0-9.]+|2\.0)(\.[0-9]+)?/ =~ GPGME::Engine.info.first.version || @pinmode + { password: @gpg_pass } + else + { password: @gpg_pass, + pinentry_mode: GPGME::PINENTRY_MODE_LOOPBACK } + end - crypto.decrypt(data, password: @gpg_pass).read.force_encoding('utf-8') + crypto = GPGME::Crypto.new(armor: true) + crypto + .decrypt(data, password) + .read.force_encoding('utf-8') rescue => e raise "#{I18n.t('error.gpg_file.decrypt')}\n#{e}" end