Skip to content

Commit

Permalink
Make error messages locale agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
vpodzime committed Mar 8, 2016
1 parent db5fb51 commit 4ed6f0b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 32 deletions.
66 changes: 39 additions & 27 deletions src/plugins/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <linux/random.h>
#include <locale.h>
#include <unistd.h>

#include "crypto.h"
Expand All @@ -45,6 +46,17 @@
* Sizes are given in bytes unless stated otherwise.
*/

/* "C" locale to get the locale-agnostic error messages */
static locale_t c_locale = (locale_t) 0;

/**
* init: (skip)
*/
gboolean init () {
c_locale = newlocale (LC_ALL_MASK, "C", c_locale);
return TRUE;
}

/**
* bd_crypto_error_quark: (skip)
*/
Expand Down Expand Up @@ -97,7 +109,7 @@ gboolean bd_crypto_device_is_luks (const gchar *device, GError **error) {
ret = crypt_init (&cd, device);
if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to initialize device: %s", strerror(-ret));
"Failed to initialize device: %s", strerror_l(-ret, c_locale));
return FALSE;
}

Expand All @@ -122,14 +134,14 @@ gchar* bd_crypto_luks_uuid (const gchar *device, GError **error) {
ret_num = crypt_init (&cd, device);
if (ret_num != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to initialize device: %s", strerror(-ret_num));
"Failed to initialize device: %s", strerror_l(-ret_num, c_locale));
return NULL;
}

ret_num = crypt_load (cd, CRYPT_LUKS1, NULL);
if (ret_num != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to load device: %s", strerror(-ret_num));
"Failed to load device: %s", strerror_l(-ret_num, c_locale));
crypt_free (cd);
return NULL;
}
Expand Down Expand Up @@ -158,7 +170,7 @@ gchar* bd_crypto_luks_status (const gchar *luks_device, GError **error) {
ret_num = crypt_init_by_name (&cd, luks_device);
if (ret_num != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to initialize device: %s", strerror(-ret_num));
"Failed to initialize device: %s", strerror_l(-ret_num, c_locale));
return NULL;
}

Expand Down Expand Up @@ -230,7 +242,7 @@ gboolean bd_crypto_luks_format (const gchar *device, const gchar *cipher, guint6
ret = crypt_init (&cd, device);
if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to initialize device: %s", strerror(-ret));
"Failed to initialize device: %s", strerror_l(-ret, c_locale));
return FALSE;
}

Expand Down Expand Up @@ -264,7 +276,7 @@ gboolean bd_crypto_luks_format (const gchar *device, const gchar *cipher, guint6

if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_FORMAT_FAILED,
"Failed to format device: %s", strerror(-ret));
"Failed to format device: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
return FALSE;
}
Expand All @@ -273,7 +285,7 @@ gboolean bd_crypto_luks_format (const gchar *device, const gchar *cipher, guint6
ret = crypt_keyslot_add_by_volume_key (cd, CRYPT_ANY_SLOT, NULL, 0, passphrase, strlen(passphrase));
if (ret < 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_ADD_KEY,
"Failed to add passphrase: %s", strerror(-ret));
"Failed to add passphrase: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
return FALSE;
}
Expand All @@ -284,7 +296,7 @@ gboolean bd_crypto_luks_format (const gchar *device, const gchar *cipher, guint6
ret = crypt_keyslot_add_by_keyfile (cd, CRYPT_ANY_SLOT, NULL, 0, key_file, 0);
if (ret < 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_ADD_KEY,
"Failed to add key file: %s", strerror(-ret));
"Failed to add key file: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
return FALSE;
}
Expand Down Expand Up @@ -319,14 +331,14 @@ gboolean bd_crypto_luks_open (const gchar *device, const gchar *name, const gcha
ret = crypt_init (&cd, device);
if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to initialize device: %s", strerror(-ret));
"Failed to initialize device: %s", strerror_l(-ret, c_locale));
return FALSE;
}

ret = crypt_load (cd, CRYPT_LUKS1, NULL);
if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to load device's parameters: %s", strerror(-ret));
"Failed to load device's parameters: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
return FALSE;
}
Expand All @@ -338,7 +350,7 @@ gboolean bd_crypto_luks_open (const gchar *device, const gchar *name, const gcha

if (ret < 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to activate device: %s", strerror(-ret));
"Failed to activate device: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
return FALSE;
}
Expand All @@ -361,14 +373,14 @@ gboolean bd_crypto_luks_close (const gchar *luks_device, GError **error) {
ret = crypt_init_by_name (&cd, luks_device);
if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to initialize device: %s", strerror(-ret));
"Failed to initialize device: %s", strerror_l(-ret, c_locale));
return FALSE;
}

ret = crypt_deactivate (cd, luks_device);
if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to deactivate device: %s", strerror(-ret));
"Failed to deactivate device: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
return FALSE;
}
Expand Down Expand Up @@ -411,14 +423,14 @@ gboolean bd_crypto_luks_add_key (const gchar *device, const gchar *pass, const g
ret = crypt_init (&cd, device);
if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to initialize device: %s", strerror(-ret));
"Failed to initialize device: %s", strerror_l(-ret, c_locale));
return FALSE;
}

ret = crypt_load (cd, CRYPT_LUKS1, NULL);
if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to load device's parameters: %s", strerror(-ret));
"Failed to load device's parameters: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
return FALSE;
}
Expand All @@ -437,7 +449,7 @@ gboolean bd_crypto_luks_add_key (const gchar *device, const gchar *pass, const g

if (ret < 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_ADD_KEY,
"Failed to add key: %s", strerror(-ret));
"Failed to add key: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
return FALSE;
}
Expand Down Expand Up @@ -470,14 +482,14 @@ gboolean bd_crypto_luks_remove_key (const gchar *device, const gchar *pass, cons
ret = crypt_init (&cd, device);
if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to initialize device: %s", strerror(-ret));
"Failed to initialize device: %s", strerror_l(-ret, c_locale));
return FALSE;
}

ret = crypt_load (cd, CRYPT_LUKS1, NULL);
if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to load device's parameters: %s", strerror(-ret));
"Failed to load device's parameters: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
return FALSE;
}
Expand All @@ -490,15 +502,15 @@ gboolean bd_crypto_luks_remove_key (const gchar *device, const gchar *pass, cons

if (ret < 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_KEY_SLOT,
"Failed to determine key slot: %s", strerror(-ret));
"Failed to determine key slot: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
return FALSE;
}

ret = crypt_keyslot_destroy (cd, ret);
if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_REMOVE_KEY,
"Failed to remove key: %s", strerror(-ret));
"Failed to remove key: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
return FALSE;
}
Expand Down Expand Up @@ -527,14 +539,14 @@ gboolean bd_crypto_luks_change_key (const gchar *device, const gchar *pass, cons
ret = crypt_init (&cd, device);
if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to initialize device: %s", strerror(-ret));
"Failed to initialize device: %s", strerror_l(-ret, c_locale));
return FALSE;
}

ret = crypt_load (cd, CRYPT_LUKS1, NULL);
if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to load device's parameters: %s", strerror(-ret));
"Failed to load device's parameters: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
return FALSE;
}
Expand All @@ -545,7 +557,7 @@ gboolean bd_crypto_luks_change_key (const gchar *device, const gchar *pass, cons
ret = crypt_volume_key_get (cd, CRYPT_ANY_SLOT, volume_key, &vk_size, pass, strlen(pass));
if (ret < 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to load device's volume key: %s", strerror(-ret));
"Failed to load device's volume key: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
return FALSE;
}
Expand All @@ -554,15 +566,15 @@ gboolean bd_crypto_luks_change_key (const gchar *device, const gchar *pass, cons
ret = crypt_keyslot_destroy (cd, ret);
if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_REMOVE_KEY,
"Failed to remove the old passphrase: %s", strerror(-ret));
"Failed to remove the old passphrase: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
return FALSE;
}

ret = crypt_keyslot_add_by_volume_key (cd, ret, volume_key, vk_size, npass, strlen(npass));
if (ret < 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_ADD_KEY,
"Failed to add the new passphrase: %s", strerror(-ret));
"Failed to add the new passphrase: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
return FALSE;
}
Expand All @@ -586,14 +598,14 @@ gboolean bd_crypto_luks_resize (const gchar *luks_device, guint64 size, GError *
ret = crypt_init_by_name (&cd, luks_device);
if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
"Failed to initialize device: %s", strerror(-ret));
"Failed to initialize device: %s", strerror_l(-ret, c_locale));
return FALSE;
}

ret = crypt_resize (cd, luks_device, size);
if (ret != 0) {
g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_RESIZE_FAILED,
"Failed to resize device: %s", strerror(-ret));
"Failed to resize device: %s", strerror_l(-ret, c_locale));
crypt_free (cd);
return FALSE;
}
Expand Down
22 changes: 17 additions & 5 deletions src/plugins/kbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <syslog.h>
#include <glob.h>
#include <unistd.h>
#include <locale.h>
#include <utils.h>

#include "kbd.h"
Expand All @@ -39,6 +40,17 @@

static const gchar * const mode_str[BD_KBD_MODE_UNKNOWN+1] = {"writethrough", "writeback", "writearound", "none", "unknown"};

/* "C" locale to get the locale-agnostic error messages */
static locale_t c_locale = (locale_t) 0;

/**
* init: (skip)
*/
gboolean init () {
c_locale = newlocale (LC_ALL_MASK, "C", c_locale);
return TRUE;
}

/**
* bd_kbd_error_quark: (skip)
*/
Expand Down Expand Up @@ -138,7 +150,7 @@ static gboolean have_kernel_module (gchar *module_name, GError **error) {
ret = kmod_module_new_from_name (ctx, module_name, &mod);
if (ret < 0) {
g_set_error (error, BD_KBD_ERROR, BD_KBD_ERROR_MODULE_FAIL,
"Failed to get the module: %s", strerror (-ret));
"Failed to get the module: %s", strerror_l (-ret, c_locale));
kmod_unref (ctx);
return FALSE;
}
Expand Down Expand Up @@ -169,7 +181,7 @@ static gboolean load_kernel_module (gchar *module_name, gchar *options, GError *
ret = kmod_module_new_from_name (ctx, module_name, &mod);
if (ret < 0) {
g_set_error (error, BD_KBD_ERROR, BD_KBD_ERROR_MODULE_FAIL,
"Failed to get the module: %s", strerror (-ret));
"Failed to get the module: %s", strerror_l (-ret, c_locale));
kmod_unref (ctx);
return FALSE;
}
Expand All @@ -187,7 +199,7 @@ static gboolean load_kernel_module (gchar *module_name, gchar *options, GError *
if (ret < 0) {
g_set_error (error, BD_KBD_ERROR, BD_KBD_ERROR_MODULE_FAIL,
"Failed to load the module '%s' with options '%s': %s",
module_name, options, strerror (-ret));
module_name, options, strerror_l (-ret, c_locale));
kmod_module_unref (mod);
kmod_unref (ctx);
return FALSE;
Expand Down Expand Up @@ -219,7 +231,7 @@ static gboolean unload_kernel_module (gchar *module_name, GError **error) {
ret = kmod_module_new_from_loaded (ctx, &list);
if (ret < 0) {
g_set_error (error, BD_KBD_ERROR, BD_KBD_ERROR_MODULE_FAIL,
"Failed to get the module: %s", strerror (-ret));
"Failed to get the module: %s", strerror_l (-ret, c_locale));
kmod_unref (ctx);
return FALSE;
}
Expand All @@ -244,7 +256,7 @@ static gboolean unload_kernel_module (gchar *module_name, GError **error) {
if (ret < 0) {
g_set_error (error, BD_KBD_ERROR, BD_KBD_ERROR_MODULE_FAIL,
"Failed to unload the module '%s': %s",
module_name, strerror (-ret));
module_name, strerror_l (-ret, c_locale));
kmod_module_unref (mod);
kmod_unref (ctx);
return FALSE;
Expand Down
25 changes: 25 additions & 0 deletions tests/crypto_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil
import subprocess
import six
import locale

from utils import create_sparse_tempfile
from gi.repository import BlockDev, GLib
Expand Down Expand Up @@ -170,6 +171,30 @@ def test_remove_key(self):
succ = BlockDev.crypto_luks_remove_key(self.loop_dev, PASSWD, None)
self.assertTrue(succ)

class CryptoTestErrorLocale(CryptoTestCase):
def setUp(self):
self._orig_loc = None
CryptoTestCase.setUp(self)
self._orig_loc = ".".join(locale.getdefaultlocale())

def _clean_up(self):
CryptoTestCase._clean_up(self)
if self._orig_loc:
locale.setlocale(locale.LC_ALL, self._orig_loc)

@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_error_locale_key(self):
"""Verify that the error msg is locale agnostic"""

succ = BlockDev.crypto_luks_format(self.loop_dev, None, 0, PASSWD, None, 0)
self.assertTrue(succ)

locale.setlocale(locale.LC_ALL, "cs_CZ.UTF-8")
try:
BlockDev.crypto_luks_remove_key(self.loop_dev, "wrong-passphrase", None)
except GLib.GError as e:
self.assertIn("Operation not permitted", str(e))

class CryptoTestChangeKey(CryptoTestCase):
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_change_key(self):
Expand Down

0 comments on commit 4ed6f0b

Please sign in to comment.