diff --git a/MPW/Config.rb b/MPW/Config.rb index 4ace575..42a840d 100644 --- a/MPW/Config.rb +++ b/MPW/Config.rb @@ -6,6 +6,7 @@ module MPW require 'rubygems' + require 'gpgme' require 'yaml' require 'i18n' @@ -96,6 +97,38 @@ module MPW @error_msg = "#{I18n.t('error.config.write')}\n#{e}" return false end + + # Setup a new gpg key + def setup_gpg_key(password, name, length = 2048, expire = 0) + if name.nil? || name.empty? + @error_msg = "#{I18n.t('error.config.genkey_gpg.name')}" + return false + elsif password.nil? || password.empty? + @error_msg = "#{I18n.t('error.config.genkey_gpg.password')}" + return false + end + + param = '' + param << '' + "\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: apc\n" + param << "\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 # Check the config file # @rtrn: true if the config file is correct @@ -118,14 +151,25 @@ module MPW @error_msg = I18n.t('error.config.check') return false end - I18n.locale = @lang.to_sym return true rescue Exception => e + puts e @error_msg = "#{I18n.t('error.config.check')}\n#{e}" 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 |key| + return true + end + + return false + end # Set the last update when there is a sync # @rtrn: true is the file has been updated diff --git a/MPW/UI/Cli.rb b/MPW/UI/Cli.rb index c3ea99f..050e0d4 100644 --- a/MPW/UI/Cli.rb +++ b/MPW/UI/Cli.rb @@ -89,11 +89,10 @@ class Cli sync_path = ask(I18n.t('form.setup.sync_path')).to_s end - if !language.nil? && !language.empty? - I18n.locale = language.to_sym - else - language = nil + if language.nil? || language.empty? + language = lang end + I18n.locale = language.to_sym sync_type = sync_type.nil? || sync_type.empty? ? nil : sync_type sync_host = sync_host.nil? || sync_host.empty? ? nil : sync_host @@ -115,6 +114,28 @@ class Cli end end + # Setup a new GPG key + def setup_gpg_key + puts I18n.t('form.setup_gpg_key.title') + puts '--------------------' + name = ask(I18n.t('form.setup_gpg_key.name')).to_s + password = ask(I18n.t('form.setup_gpg_key.password')).to_s + length = ask(I18n.t('form.setup_gpg_key.length')).to_s + expire = ask(I18n.t('form.setup_gpg_key.expire')).to_s + + length = length.nil? || length.empty? ? 2048 : length.to_i + expire = expire.nil? || expire.empty? ? 0 : expire.to_i + + puts I18n.t('form.setup_gpg_key.wait') + + if @config.setup_gpg_key(password, name, length, expire) + puts I18n.t('form.setup_gpg_key.valid') + else + puts "#{I18n.t('display.error')}: #{@config.error_msg}" + exit 2 + end + end + # Request the GPG password and decrypt the file def decrypt if !defined?(@mpw) diff --git a/i18n/cli/en.yml b/i18n/cli/en.yml index 622dfe8..dd58ab0 100644 --- a/i18n/cli/en.yml +++ b/i18n/cli/en.yml @@ -5,6 +5,10 @@ en: write: "Can't write the config file!" check: "Checkconfig failed!" key_bad_format: "The key string isn't in good format!" + genkey_gpg: + exception: "Can't create the GPG key!" + name: "You must define a name for your GPG key!" + password: "You must define a password for your GPG key!" delete: id_no_exist: "Can't delete the item %{id}, it doesn't exist!" export: @@ -77,6 +81,13 @@ en: sync_pwd: "Password for the synchronization: " sync_path: "File path for the synchronization : " valid: "The config file has been created!" + setup_gpg_key: + title: "Setup a GPG key" + name: "Your name and lastname: " + password: "A password for the GPG key: " + length: "Size of the GPG key [default=2048]: " + expire: "Expire time of the GPG key [default=0 (unlimited)]: " + wait: "Please waiting during the GPG key generate, this process can take few minutes." update: title: "Update an item" name: "Enter the name [%{name}]: " diff --git a/i18n/cli/fr.yml b/i18n/cli/fr.yml index 1f2f7b2..e38e00d 100644 --- a/i18n/cli/fr.yml +++ b/i18n/cli/fr.yml @@ -5,6 +5,10 @@ fr: write: "Impossible d'écrire le fichier de configuration!" check: "Le fichier de configuration est invalide!" key_bad_format: "La clé GPG est invalide!" + genkey_gpg: + exception: "La création de la clé GPG n'a pas pu aboutir!" + name: "Vous devez définir un nom pour votre clé GPG!" + password: "Vous devez définir un mot de passe pour votre clé GPG!" delete: id_no_exist: "Impossible de supprimer l'élément %{id}, car il n'existe pas!" export: @@ -77,6 +81,13 @@ fr: sync_pwd: "Mot de passe pour la synchronisation: " sync_path: "Chemin du fichier pour la synchronisation: " valid: "Le fichier de configuration a bien été créé!" + setup_gpg_key: + title: "Configuration d'une clé GPG" + name: "Votre nom et prénom: " + password: "Mot de passe de la clé GPG: " + length: "Taille de la clé GPG [défaut=2048]: " + expire: "Expiration de la clé GPG [défaut=0 (illimité)]: " + wait: "Veuillez patienter durant la génération de votre clé GPG, ce processus peut prendre quelques minutes." update: title: "Mis à jour d'un élément" name: "Entrez le nom [%{name}]: " diff --git a/mpw b/mpw index c13baeb..6db35f0 100755 --- a/mpw +++ b/mpw @@ -117,6 +117,8 @@ cli = Cli.new(lang, config) # Setup a new config if !check_error || !options[:setup].nil? cli.setup(lang) +elsif !config.check_gpg_key? + cli.setup_gpg_key end cli.decrypt