From 8bec41ab346a7f5f38dbc8b4d563f3a613e1c78c Mon Sep 17 00:00:00 2001 From: Milan Broz Date: Wed, 30 Sep 2009 15:07:41 +0000 Subject: [PATCH] Properly apply versioned symbols in library and fix problems uncovered by doing that:-) git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@124 36d66b0a-2a48-0410-832c-cd162a569da5 --- ChangeLog | 6 ++++++ lib/Makefile.am | 2 +- lib/internal.h | 1 - lib/libcryptsetup.h | 11 ++++++++++- lib/libcryptsetup.sym | 1 + lib/setup.c | 39 +++++++++++++++++++++++++++------------ src/cryptsetup.c | 43 +++++++++++++++++++++++++++++++------------ src/cryptsetup.h | 16 ++++------------ 8 files changed, 80 insertions(+), 39 deletions(-) diff --git a/ChangeLog b/ChangeLog index c660cf7..495089a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2009-09-30 Milan Broz + * Fix exported symbols and versions in libcryptsetup. + * Do not use internal lib functions in cryptsetup. + * Add crypt_log to library. + * Fix crypt_remove_device (remove, luksClose) implementation. + 2009-09-28 Milan Broz * Add luksHeaderBackup and luksHeaderRestore commands. * Fail passphrase read if piped input no longer exists. diff --git a/lib/Makefile.am b/lib/Makefile.am index 1822ded..4ab87f4 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -25,7 +25,7 @@ libcryptsetup_la_DEPENDENCIES = libcryptsetup.sym libcryptsetup_la_LDFLAGS = \ $(_STATIC_LIBRARY) \ - --version-script=libcryptsetup.sym \ + -Wl,--version-script=libcryptsetup.sym \ -version-info 1:0:0 libcryptsetup_la_CFLAGS = -Wall diff --git a/lib/internal.h b/lib/internal.h index b0e0868..da3c9f7 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -109,7 +109,6 @@ void get_key(char *prompt, char **key, unsigned int *passLen, int key_size, int parse_into_name_and_mode(const char *nameAndMode, char *name, char *mode); -void set_default_log(void (*log)(int class, char *msg)); void logger(struct crypt_device *cd, int class, const char *file, int line, const char *format, ...); #define log_dbg(x...) logger(NULL, CRYPT_LOG_DEBUG, __FILE__, __LINE__, x) #define log_std(c, x...) logger(c, CRYPT_LOG_NORMAL, __FILE__, __LINE__, x) diff --git a/lib/libcryptsetup.h b/lib/libcryptsetup.h index 9d6e128..82f885b 100644 --- a/lib/libcryptsetup.h +++ b/lib/libcryptsetup.h @@ -35,7 +35,7 @@ int crypt_init_by_name(struct crypt_device **cd, const char *name); /** * Set log function. * - * @cd - crypt device handle + * @cd - crypt device handle (can be NULL to set default log function) * @usrptr - provided identification in callback * @class - log type below (debug messages can uses other levels) * @msg - log message @@ -48,6 +48,15 @@ void crypt_set_log_callback(struct crypt_device *cd, void *usrptr); /** + * Log message through log function. + * + * @cd - crypt device handle + * @class - log type + * @msg - log message + */ +void crypt_log(struct crypt_device *cd, int class, const char *msg); + +/** * Set confirmation callback (yes/no) * * If code need confirmation (like deleting last key slot) this function diff --git a/lib/libcryptsetup.sym b/lib/libcryptsetup.sym index eef3bfd..d4ed0dc 100644 --- a/lib/libcryptsetup.sym +++ b/lib/libcryptsetup.sym @@ -40,6 +40,7 @@ CRYPTSETUP_1.0 { crypt_get_error; crypt_get_dir; crypt_set_debug_level; + crypt_log; crypt_header_backup; crypt_header_restore; diff --git a/lib/setup.c b/lib/setup.c index 04b1c43..be9452b 100644 --- a/lib/setup.c +++ b/lib/setup.c @@ -39,7 +39,7 @@ struct crypt_device { }; /* Log helper */ -static void (*_default_log)(int class, char *msg) = NULL; +static void (*_default_log)(int class, const char *msg, void *usrptr) = NULL; static int _debug_level = 0; void crypt_set_debug_level(int level) @@ -47,9 +47,12 @@ void crypt_set_debug_level(int level) _debug_level = level; } -void set_default_log(void (*log)(int class, char *msg)) +void crypt_log(struct crypt_device *cd, int class, const char *msg) { - _default_log = log; + if (cd && cd->log) + cd->log(class, msg, cd->log_usrptr); + else if (_default_log) + _default_log(class, msg, NULL); } void logger(struct crypt_device *cd, int class, const char *file, @@ -62,10 +65,7 @@ void logger(struct crypt_device *cd, int class, const char *file, if (vasprintf(&target, format, argp) > 0) { if (class >= 0) { - if (cd && cd->log) - cd->log(class, target, cd->log_usrptr); - else if (_default_log) - _default_log(class, target); + crypt_log(cd, class, target); #ifdef CRYPT_DEBUG } else if (_debug_level) printf("# %s:%d %s\n", file ?: "?", line, target); @@ -552,8 +552,12 @@ void crypt_set_log_callback(struct crypt_device *cd, void (*log)(int class, const char *msg, void *usrptr), void *usrptr) { - cd->log = log; - cd->log_usrptr = usrptr; + if (!cd) + _default_log = log; + else { + cd->log = log; + cd->log_usrptr = usrptr; + } } void crypt_set_confirm_callback(struct crypt_device *cd, @@ -703,7 +707,18 @@ int crypt_query_device(struct crypt_options *options) /* OPTIONS: name, icb */ int crypt_remove_device(struct crypt_options *options) { - return crypt_deactivate(NULL, options->name); + struct crypt_device *cd = NULL; + int r; + + r = crypt_init_by_name(&cd, options->name); + if (r) + return r; + + r = crypt_deactivate(cd, options->name); + + crypt_free(cd); + return r; + } /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot @@ -981,12 +996,12 @@ int crypt_init_by_name(struct crypt_device **cd, const char *name) ci = crypt_status(NULL, name); if (ci < ACTIVE) { log_err(NULL, _("Device %s is not active.\n"), name); - return -EINVAL; + return -ENODEV; } r = dm_query_device(name, &device, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); - if (!r) + if (r >= 0) r = crypt_init(cd, device); free(device); diff --git a/src/cryptsetup.c b/src/cryptsetup.c index 50833d4..5854f44 100644 --- a/src/cryptsetup.c +++ b/src/cryptsetup.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -70,7 +71,7 @@ static struct action_type { const char *desc; } action_types[] = { { "create", action_create, 0, 2, 1, 1, 1, N_(" "),N_("create device") }, - { "remove", action_remove, 0, 1, 1, 0, 1, N_(""), N_("remove device") }, + { "remove", action_remove, 0, 1, 1, 1, 1, N_(""), N_("remove device") }, { "resize", action_resize, 0, 1, 1, 1, 1, N_(""), N_("resize active device") }, { "status", action_status, 0, 1, 1, 0, 1, N_(""), N_("show device status") }, { "luksFormat", action_luksFormat, 0, 1, 1, 1, 1, N_(" []"), N_("formats a LUKS device") }, @@ -80,7 +81,7 @@ static struct action_type { { "luksKillSlot", action_luksKillSlot, 0, 2, 1, 1, 1, N_(" "), N_("wipes key with number from LUKS device") }, { "luksUUID", action_luksUUID, 0, 1, 0, 0, 1, N_(""), N_("print UUID of LUKS device") }, { "isLuks", action_isLuks, 0, 1, 0, 0, 0, N_(""), N_("tests for LUKS partition header") }, - { "luksClose", action_remove, 0, 1, 1, 0, 1, N_(""), N_("remove LUKS mapping") }, + { "luksClose", action_remove, 0, 1, 1, 1, 1, N_(""), N_("remove LUKS mapping") }, { "luksDump", action_luksDump, 0, 1, 0, 0, 1, N_(""), N_("dump LUKS partition information") }, { "luksSuspend",action_luksSuspend, 0, 1, 1, 1, 1, N_(""), N_("Suspend LUKS device and wipe key (all IOs are frozen).") }, { "luksResume", action_luksResume, 0, 1, 1, 1, 1, N_(""), N_("Resume suspended LUKS device.") }, @@ -91,6 +92,30 @@ static struct action_type { { NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL } }; +static void clogger(struct crypt_device *cd, int class, const char *file, + int line, const char *format, ...) +{ + va_list argp; + char *target = NULL; + + va_start(argp, format); + + if (vasprintf(&target, format, argp) > 0) { + if (class >= 0) { + crypt_log(cd, class, target); +#ifdef CRYPT_DEBUG + } else if (opt_debug) + printf("# %s:%d %s\n", file ?: "?", line, target); +#else + } else if (opt_debug) + printf("# %s\n", target); +#endif + } + + va_end(argp); + free(target); +} + /* Interface Callbacks */ static int yesDialog(char *msg) { @@ -319,7 +344,9 @@ static int _action_luksFormat_useMK() .data_alignment = opt_align_payload, }; - if (parse_into_name_and_mode(opt_cipher ?: DEFAULT_LUKS_CIPHER, cipher, cipher_mode)) { + if (sscanf(opt_cipher ?: DEFAULT_LUKS_CIPHER, + "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s", + cipher, cipher_mode) != 2) { log_err("No known cipher specification pattern detected.\n"); return -EINVAL; } @@ -643,11 +670,6 @@ static int run_action(struct action_type *action) { int r; - if (dm_init(NULL, action->required_dm_backend) < 1) { - log_err("Cannot communicate with device-mapper. Is dm_mod kernel module loaded?\n"); - return -ENOSYS; - } - if (action->required_memlock) crypt_memory_lock(NULL, 1); @@ -656,9 +678,6 @@ static int run_action(struct action_type *action) if (action->required_memlock) crypt_memory_lock(NULL, 0); - if (action->required_dm_backend) - dm_exit(); - if (r < 0 && (opt_verbose || action->show_status)) show_status(-r); @@ -706,7 +725,7 @@ int main(int argc, char **argv) int r; const char *null_action_argv[] = {NULL}; - set_default_log(cmdLineLog); + crypt_set_log_callback(NULL, _log, NULL); setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); diff --git a/src/cryptsetup.h b/src/cryptsetup.h index 72f0497..d787f7a 100644 --- a/src/cryptsetup.h +++ b/src/cryptsetup.h @@ -34,18 +34,10 @@ #define DEFAULT_LUKS_KEY_SIZE 128 #define MAX_CIPHER_LEN 32 +#define MAX_CIPHER_LEN_STR "32" -/* Helper funcions provided by internal libcryptsetup objects */ -void set_default_log(void (*log)(int class, char *msg)); -void logger(struct crypt_device *cd, int class, const char *file, int line, const char *format, ...); -#define log_dbg(x...) logger(NULL, CRYPT_LOG_DEBUG, __FILE__, __LINE__, x) -#define log_std(x...) logger(NULL, CRYPT_LOG_NORMAL, __FILE__, __LINE__, x) -#define log_err(x...) logger(NULL, CRYPT_LOG_ERROR, __FILE__, __LINE__, x) - -extern int memlock_inc(struct crypt_device *ctx); -extern int memlock_dec(struct crypt_device *ctx); -extern int dm_init(struct crypt_device *context, int check_kernel); -extern void dm_exit(void); -extern int parse_into_name_and_mode(const char *nameAndMode, char *name, char *mode); +#define log_dbg(x...) clogger(NULL, CRYPT_LOG_DEBUG, __FILE__, __LINE__, x) +#define log_std(x...) clogger(NULL, CRYPT_LOG_NORMAL, __FILE__, __LINE__, x) +#define log_err(x...) clogger(NULL, CRYPT_LOG_ERROR, __FILE__, __LINE__, x) #endif /* CRYPTSETUP_H */ -- 2.7.4