From d1403df7fbace17cd794629361e4acb693cab731 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Fri, 30 Mar 2007 00:58:57 +0000 Subject: [PATCH] Reindented using tabs only --- src/conf.c | 282 ++++++++++++++++++++++---------------------- src/conf.h | 47 ++++---- src/device.c | 100 ++++++++-------- src/device.h | 2 +- src/hal.c | 280 +++++++++++++++++++++---------------------- src/hal.h | 22 ++-- src/local.c | 66 +++++------ src/local.h | 2 +- src/log.c | 92 +++++++-------- src/log.h | 8 +- src/pad.c | 326 +++++++++++++++++++++++++-------------------------- src/pad.h | 3 +- src/pam.c | 124 ++++++++++---------- src/volume.c | 206 ++++++++++++++++---------------- src/volume.h | 4 +- src/xpath.c | 326 +++++++++++++++++++++++++-------------------------- src/xpath.h | 18 +-- 17 files changed, 946 insertions(+), 962 deletions(-) diff --git a/src/conf.c b/src/conf.c index 8a83b6d..e8b0aa1 100644 --- a/src/conf.c +++ b/src/conf.c @@ -22,166 +22,166 @@ #include "xpath.h" #include "log.h" -static void pusb_conf_options_get_from(t_pusb_options *opts, - const char *from, - xmlDoc *doc) +static void pusb_conf_options_get_from(t_pusb_options *opts, + const char *from, + xmlDoc *doc) { - pusb_xpath_get_string_from(doc, from, "option[@name='hostname']", - opts->hostname, sizeof(opts->hostname)); - pusb_xpath_get_string_from(doc, from, "option[@name='system_pad_directory']", - opts->system_pad_directory, - sizeof(opts->system_pad_directory)); - pusb_xpath_get_string_from(doc, from, "option[@name='device_pad_directory']", - opts->device_pad_directory, - sizeof(opts->device_pad_directory)); - pusb_xpath_get_bool_from(doc, from, "option[@name='debug']", - &(opts->debug)); - pusb_xpath_get_bool_from(doc, from, "option[@name='quiet']", - &(opts->quiet)); - pusb_xpath_get_bool_from(doc, from, "option[@name='color_log']", - &(opts->color_log)); - pusb_xpath_get_bool_from(doc, from, "option[@name='enable']", - &(opts->enable)); - pusb_xpath_get_bool_from(doc, from, "option[@name='one_time_pad']", - &(opts->one_time_pad)); - pusb_xpath_get_int_from(doc, from, "option[@name='probe_timeout']", - &(opts->probe_timeout)); + pusb_xpath_get_string_from(doc, from, "option[@name='hostname']", + opts->hostname, sizeof(opts->hostname)); + pusb_xpath_get_string_from(doc, from, "option[@name='system_pad_directory']", + opts->system_pad_directory, + sizeof(opts->system_pad_directory)); + pusb_xpath_get_string_from(doc, from, "option[@name='device_pad_directory']", + opts->device_pad_directory, + sizeof(opts->device_pad_directory)); + pusb_xpath_get_bool_from(doc, from, "option[@name='debug']", + &(opts->debug)); + pusb_xpath_get_bool_from(doc, from, "option[@name='quiet']", + &(opts->quiet)); + pusb_xpath_get_bool_from(doc, from, "option[@name='color_log']", + &(opts->color_log)); + pusb_xpath_get_bool_from(doc, from, "option[@name='enable']", + &(opts->enable)); + pusb_xpath_get_bool_from(doc, from, "option[@name='one_time_pad']", + &(opts->one_time_pad)); + pusb_xpath_get_int_from(doc, from, "option[@name='probe_timeout']", + &(opts->probe_timeout)); } -static int pusb_conf_parse_options(t_pusb_options *opts, - xmlDoc *doc, - const char *user, - const char *service) +static int pusb_conf_parse_options(t_pusb_options *opts, + xmlDoc *doc, + const char *user, + const char *service) { - char *xpath = NULL; - size_t xpath_size; - int i; - struct s_opt_list opt_list[] = { - { CONF_DEVICE_XPATH, opts->device.name }, - { CONF_USER_XPATH, (char *)user }, - { CONF_SERVICE_XPATH, (char *)service }, - { NULL, NULL } - }; + char *xpath = NULL; + size_t xpath_size; + int i; + struct s_opt_list opt_list[] = { + { CONF_DEVICE_XPATH, opts->device.name }, + { CONF_USER_XPATH, (char *)user }, + { CONF_SERVICE_XPATH, (char *)service }, + { NULL, NULL } + }; - pusb_conf_options_get_from(opts, "//configuration/defaults/", doc); - for (i = 0; opt_list[i].name != NULL; ++i) - { - xpath_size = strlen(opt_list[i].name) + strlen(opt_list[i].value) + 1; - if (!(xpath = malloc(xpath_size))) + pusb_conf_options_get_from(opts, "//configuration/defaults/", doc); + for (i = 0; opt_list[i].name != NULL; ++i) { - log_error("malloc error\n"); - return (0); + xpath_size = strlen(opt_list[i].name) + strlen(opt_list[i].value) + 1; + if (!(xpath = malloc(xpath_size))) + { + log_error("malloc error\n"); + return (0); + } + memset(xpath, 0x00, xpath_size); + snprintf(xpath, xpath_size, opt_list[i].name, opt_list[i].value, ""); + pusb_conf_options_get_from(opts, xpath, doc); + free(xpath); } - memset(xpath, 0x00, xpath_size); - snprintf(xpath, xpath_size, opt_list[i].name, opt_list[i].value, ""); - pusb_conf_options_get_from(opts, xpath, doc); - free(xpath); - } - return (1); + return (1); } -static int pusb_conf_device_get_property(t_pusb_options *opts, - xmlDoc *doc, - const char *property, - char *store, - size_t size) +static int pusb_conf_device_get_property(t_pusb_options *opts, + xmlDoc *doc, + const char *property, + char *store, + size_t size) { - char *xpath = NULL; - size_t xpath_len; - int retval; + char *xpath = NULL; + size_t xpath_len; + int retval; - xpath_len = strlen(CONF_DEVICE_XPATH) + strlen(opts->device.name) + \ - strlen(property) + 1; - if (!(xpath = malloc(xpath_len))) - { - log_error("malloc error!\n"); - return (0); - } - memset(xpath, 0x00, xpath_len); - snprintf(xpath, xpath_len, CONF_DEVICE_XPATH, opts->device.name, - property); - retval = pusb_xpath_get_string(doc, xpath, store, size); - free(xpath); - return (retval); + xpath_len = strlen(CONF_DEVICE_XPATH) + strlen(opts->device.name) + \ + strlen(property) + 1; + if (!(xpath = malloc(xpath_len))) + { + log_error("malloc error!\n"); + return (0); + } + memset(xpath, 0x00, xpath_len); + snprintf(xpath, xpath_len, CONF_DEVICE_XPATH, opts->device.name, + property); + retval = pusb_xpath_get_string(doc, xpath, store, size); + free(xpath); + return (retval); } -static int pusb_conf_parse_device(t_pusb_options *opts, xmlDoc *doc) +static int pusb_conf_parse_device(t_pusb_options *opts, xmlDoc *doc) { - if (!pusb_conf_device_get_property(opts, doc, "vendor", opts->device.vendor, - sizeof(opts->device.vendor))) - return (0); - if (!pusb_conf_device_get_property(opts, doc, "model", opts->device.model, - sizeof(opts->device.model))) - return (0); - if (!pusb_conf_device_get_property(opts, doc, "serial", opts->device.serial, - sizeof(opts->device.serial))) - return (0); - pusb_conf_device_get_property(opts, doc, "volume_uuid", - opts->device.volume_uuid, - sizeof(opts->device.volume_uuid)); - return (1); + if (!pusb_conf_device_get_property(opts, doc, "vendor", opts->device.vendor, + sizeof(opts->device.vendor))) + return (0); + if (!pusb_conf_device_get_property(opts, doc, "model", opts->device.model, + sizeof(opts->device.model))) + return (0); + if (!pusb_conf_device_get_property(opts, doc, "serial", opts->device.serial, + sizeof(opts->device.serial))) + return (0); + pusb_conf_device_get_property(opts, doc, "volume_uuid", + opts->device.volume_uuid, + sizeof(opts->device.volume_uuid)); + return (1); } -int pusb_conf_init(t_pusb_options *opts) +int pusb_conf_init(t_pusb_options *opts) { - memset(opts, 0x00, sizeof(*opts)); - if (gethostname(opts->hostname, sizeof(opts->hostname)) == -1) - { - log_error("gethostname: %s\n", strerror(errno)); - return (0); - } - strcpy(opts->system_pad_directory, ".pamusb"); - strcpy(opts->device_pad_directory, ".pamusb"); - opts->probe_timeout = 10; - opts->enable = 1; - opts->debug = 0; - opts->quiet = 0; - opts->color_log = 1; - opts->one_time_pad = 1; - return (1); + memset(opts, 0x00, sizeof(*opts)); + if (gethostname(opts->hostname, sizeof(opts->hostname)) == -1) + { + log_error("gethostname: %s\n", strerror(errno)); + return (0); + } + strcpy(opts->system_pad_directory, ".pamusb"); + strcpy(opts->device_pad_directory, ".pamusb"); + opts->probe_timeout = 10; + opts->enable = 1; + opts->debug = 0; + opts->quiet = 0; + opts->color_log = 1; + opts->one_time_pad = 1; + return (1); } -int pusb_conf_parse(const char *file, t_pusb_options *opts, - const char *user, const char *service) +int pusb_conf_parse(const char *file, t_pusb_options *opts, + const char *user, const char *service) { - xmlDoc *doc = NULL; - int retval; - char device_xpath[sizeof(CONF_USER_XPATH) + CONF_USER_MAXLEN + \ - sizeof("device")]; + xmlDoc *doc = NULL; + int retval; + char device_xpath[sizeof(CONF_USER_XPATH) + CONF_USER_MAXLEN + \ + sizeof("device")]; - log_debug("Parsing settings...\n", - user, service); - if (strlen(user) > CONF_USER_MAXLEN) - { - log_error("Username \"%s\" is too long (max: %d).\n", user, - CONF_USER_MAXLEN); - return (0); - } - if (!(doc = xmlReadFile(file, NULL, 0))) - { - log_error("Unable to parse \"%s\".\n", file); - return (0); - } - snprintf(device_xpath, sizeof(device_xpath), CONF_USER_XPATH, user, - "device"); - retval = pusb_xpath_get_string(doc, - device_xpath, - opts->device.name, - sizeof(opts->device.name)); - if (!retval || !pusb_conf_parse_device(opts, doc)) - { - log_error("No device configured for user \"%s\".\n", user); - xmlFreeDoc(doc); - xmlCleanupParser(); - return (0); - } - if (!pusb_conf_parse_options(opts, doc, user, service)) - { - xmlFreeDoc(doc); - xmlCleanupParser(); - return (0); - } - xmlFreeDoc(doc); - xmlCleanupParser(); - return (1); + log_debug("Parsing settings...\n", + user, service); + if (strlen(user) > CONF_USER_MAXLEN) + { + log_error("Username \"%s\" is too long (max: %d).\n", user, + CONF_USER_MAXLEN); + return (0); + } + if (!(doc = xmlReadFile(file, NULL, 0))) + { + log_error("Unable to parse \"%s\".\n", file); + return (0); + } + snprintf(device_xpath, sizeof(device_xpath), CONF_USER_XPATH, user, + "device"); + retval = pusb_xpath_get_string(doc, + device_xpath, + opts->device.name, + sizeof(opts->device.name)); + if (!retval || !pusb_conf_parse_device(opts, doc)) + { + log_error("No device configured for user \"%s\".\n", user); + xmlFreeDoc(doc); + xmlCleanupParser(); + return (0); + } + if (!pusb_conf_parse_options(opts, doc, user, service)) + { + xmlFreeDoc(doc); + xmlCleanupParser(); + return (0); + } + xmlFreeDoc(doc); + xmlCleanupParser(); + return (1); } diff --git a/src/conf.h b/src/conf.h index 3382488..4570048 100644 --- a/src/conf.h +++ b/src/conf.h @@ -30,35 +30,34 @@ typedef struct pusb_device { - char name[32]; - char vendor[128]; - char model[128]; - char serial[128]; - char volume_uuid[128]; -} t_pusb_device; + char name[32]; + char vendor[128]; + char model[128]; + char serial[128]; + char volume_uuid[128]; +} t_pusb_device; -typedef struct pusb_options +typedef struct pusb_options { - int probe_timeout; - int enable; - int debug; - int quiet; - int color_log; - int one_time_pad; - char hostname[32]; - char system_pad_directory[PATH_MAX]; - char device_pad_directory[PATH_MAX]; - t_pusb_device device; -} t_pusb_options; + int probe_timeout; + int enable; + int debug; + int quiet; + int color_log; + int one_time_pad; + char hostname[32]; + char system_pad_directory[PATH_MAX]; + char device_pad_directory[PATH_MAX]; + t_pusb_device device; +} t_pusb_options; -struct s_opt_list +struct s_opt_list { - char *name; - char *value; + char *name; + char *value; }; -int pusb_conf_init(t_pusb_options *opts); -int pusb_conf_parse(const char *file, t_pusb_options *opts, - const char *user, const char *service); +int pusb_conf_init(t_pusb_options *opts); +int pusb_conf_parse(const char *file, t_pusb_options *opts, const char *user, const char *service); #endif /* !PUSB_CONF_H_ */ diff --git a/src/device.c b/src/device.c index 24667a4..8c1642c 100644 --- a/src/device.c +++ b/src/device.c @@ -25,64 +25,64 @@ #include "pad.h" #include "device.h" -static int pusb_device_connected(t_pusb_options *opts, LibHalContext *ctx) +static int pusb_device_connected(t_pusb_options *opts, LibHalContext *ctx) { - char *udi = NULL; + char *udi = NULL; - log_debug("Searching for \"%s\" in the hardware database...\n", - opts->device.name); - udi = pusb_hal_find_item(ctx, - "usb_device.serial", opts->device.serial, - "usb_device.vendor", opts->device.vendor, - "info.product", opts->device.model, - NULL); - if (!udi) - { - log_error("Device \"%s\" is not connected.\n", - opts->device.name); - return (0); - } - libhal_free_string(udi); - log_info("Device \"%s\" is connected (good).\n", opts->device.name); - return (1); + log_debug("Searching for \"%s\" in the hardware database...\n", + opts->device.name); + udi = pusb_hal_find_item(ctx, + "usb_device.serial", opts->device.serial, + "usb_device.vendor", opts->device.vendor, + "info.product", opts->device.model, + NULL); + if (!udi) + { + log_error("Device \"%s\" is not connected.\n", + opts->device.name); + return (0); + } + libhal_free_string(udi); + log_info("Device \"%s\" is connected (good).\n", opts->device.name); + return (1); } -int pusb_device_check(t_pusb_options *opts, - const char *user) +int pusb_device_check(t_pusb_options *opts, + const char *user) { - DBusConnection *dbus = NULL; - LibHalContext *ctx = NULL; - int retval = 0; + DBusConnection *dbus = NULL; + LibHalContext *ctx = NULL; + int retval = 0; - log_debug("Connecting to HAL...\n"); - if (!(dbus = pusb_hal_dbus_connect())) - return (0); + log_debug("Connecting to HAL...\n"); + if (!(dbus = pusb_hal_dbus_connect())) + return (0); - if (!(ctx = pusb_hal_init(dbus))) - { - pusb_hal_dbus_disconnect(dbus); - return (0); - } + if (!(ctx = pusb_hal_init(dbus))) + { + pusb_hal_dbus_disconnect(dbus); + return (0); + } - if (!pusb_device_connected(opts, ctx)) - { - pusb_hal_dbus_disconnect(dbus); - libhal_ctx_free(ctx); - return (0); - } + if (!pusb_device_connected(opts, ctx)) + { + pusb_hal_dbus_disconnect(dbus); + libhal_ctx_free(ctx); + return (0); + } - if (opts->one_time_pad) - { - log_info("Performing one time pad verification...\n"); - retval = pusb_pad_check(opts, ctx, user); - } - else - { - log_debug("One time pad is disabled, no more verifications to do.\n"); - retval = 1; - } + if (opts->one_time_pad) + { + log_info("Performing one time pad verification...\n"); + retval = pusb_pad_check(opts, ctx, user); + } + else + { + log_debug("One time pad is disabled, no more verifications to do.\n"); + retval = 1; + } - pusb_hal_dbus_disconnect(dbus); - libhal_ctx_free(ctx); - return (retval); + pusb_hal_dbus_disconnect(dbus); + libhal_ctx_free(ctx); + return (retval); } diff --git a/src/device.h b/src/device.h index 253b270..e9a3851 100644 --- a/src/device.h +++ b/src/device.h @@ -18,6 +18,6 @@ #ifndef PUSB_DEVICE_H_ # define PUSB_DEVICE_H_ -int pusb_device_check(t_pusb_options *opts, const char *user); +int pusb_device_check(t_pusb_options *opts, const char *user); #endif /* !PUSB_DEVICE_H_ */ diff --git a/src/hal.c b/src/hal.c index 49e1d84..c19c351 100644 --- a/src/hal.c +++ b/src/hal.c @@ -21,169 +21,169 @@ #include #include "log.h" -DBusConnection *pusb_hal_dbus_connect(void) +DBusConnection *pusb_hal_dbus_connect(void) { - DBusConnection *dbus = NULL; - DBusError error; - - dbus_error_init(&error); - if (!(dbus = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) - { - log_error("Cannot connect to system bus: %s\n", - error.message); - dbus_error_free(&error); - return (NULL); - } - return (dbus); + DBusConnection *dbus = NULL; + DBusError error; + + dbus_error_init(&error); + if (!(dbus = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) + { + log_error("Cannot connect to system bus: %s\n", + error.message); + dbus_error_free(&error); + return (NULL); + } + return (dbus); } -void pusb_hal_dbus_disconnect(DBusConnection *dbus) +void pusb_hal_dbus_disconnect(DBusConnection *dbus) { - dbus_connection_unref(dbus); + dbus_connection_unref(dbus); } -LibHalContext *pusb_hal_init(DBusConnection *dbus) +LibHalContext *pusb_hal_init(DBusConnection *dbus) { - DBusError error; - LibHalContext *ctx = NULL; - - dbus_error_init(&error); - if (!(ctx = libhal_ctx_new())) - { - log_error("Failed to create a HAL context\n"); - return (NULL); - } - if (!libhal_ctx_set_dbus_connection(ctx, dbus)) - { - log_error("Failed to attach dbus connection to hal\n"); - libhal_ctx_free(ctx); - return (NULL); - } - if (!libhal_ctx_init(ctx, &error)) - { - log_error("libhal_ctx_init: %s\n", error.name, error.message); - libhal_ctx_free(ctx); - return (NULL); - } - return (ctx); + DBusError error; + LibHalContext *ctx = NULL; + + dbus_error_init(&error); + if (!(ctx = libhal_ctx_new())) + { + log_error("Failed to create a HAL context\n"); + return (NULL); + } + if (!libhal_ctx_set_dbus_connection(ctx, dbus)) + { + log_error("Failed to attach dbus connection to hal\n"); + libhal_ctx_free(ctx); + return (NULL); + } + if (!libhal_ctx_init(ctx, &error)) + { + log_error("libhal_ctx_init: %s\n", error.name, error.message); + libhal_ctx_free(ctx); + return (NULL); + } + return (ctx); } -void pusb_hal_destroy(LibHalContext *ctx) +void pusb_hal_destroy(LibHalContext *ctx) { - libhal_ctx_free(ctx); + libhal_ctx_free(ctx); } -char *pusb_hal_get_property(LibHalContext *ctx, - const char *udi, - const char *name) +char *pusb_hal_get_property(LibHalContext *ctx, + const char *udi, + const char *name) { - DBusError error; - char *data; - - dbus_error_init(&error); - data = libhal_device_get_property_string(ctx, udi, - name, &error); - if (!data) - { - log_debug("%s\n", error.message); - dbus_error_free(&error); - return (NULL); - } - return (data); + DBusError error; + char *data; + + dbus_error_init(&error); + data = libhal_device_get_property_string(ctx, udi, + name, &error); + if (!data) + { + log_debug("%s\n", error.message); + dbus_error_free(&error); + return (NULL); + } + return (data); } -int pusb_hal_check_property(LibHalContext *ctx, - const char *udi, - const char *name, - const char *value) +int pusb_hal_check_property(LibHalContext *ctx, + const char *udi, + const char *name, + const char *value) { - char *data; - int retval; - - data = pusb_hal_get_property(ctx, udi, name); - if (!data) - return (0); - retval = (strcmp(data, value) == 0); - libhal_free_string(data); - return (retval); + char *data; + int retval; + + data = pusb_hal_get_property(ctx, udi, name); + if (!data) + return (0); + retval = (strcmp(data, value) == 0); + libhal_free_string(data); + return (retval); } -char **pusb_hal_find_all_items(LibHalContext *ctx, - const char *property, - const char *value, - int *count) +char **pusb_hal_find_all_items(LibHalContext *ctx, + const char *property, + const char *value, + int *count) { - DBusError error; - char **devices; - int n_devices; - - dbus_error_init(&error); - *count = 0; - devices = libhal_manager_find_device_string_match(ctx, - property, - value, - &n_devices, - &error); - if (!devices) - { - log_error("Unable to find item \"%s\": %s\n", property, - error.message); - dbus_error_free(&error); - return (NULL); - } - if (!n_devices) - { - libhal_free_string_array(devices); - return (NULL); - } - *count = n_devices; - return (devices); + DBusError error; + char **devices; + int n_devices; + + dbus_error_init(&error); + *count = 0; + devices = libhal_manager_find_device_string_match(ctx, + property, + value, + &n_devices, + &error); + if (!devices) + { + log_error("Unable to find item \"%s\": %s\n", property, + error.message); + dbus_error_free(&error); + return (NULL); + } + if (!n_devices) + { + libhal_free_string_array(devices); + return (NULL); + } + *count = n_devices; + return (devices); } -char *pusb_hal_find_item(LibHalContext *ctx, - const char *property, - const char *value, - ...) +char *pusb_hal_find_item(LibHalContext *ctx, + const char *property, + const char *value, + ...) { - char **devices; - int n_devices; - char *udi = NULL; - va_list ap; - int i; - - devices = pusb_hal_find_all_items(ctx, property, value, &n_devices); - if (!devices) - return (NULL); - if (!n_devices) - return (NULL); - - for (i = 0; i < n_devices; ++i) - { - char *key = NULL; - int match = 1; - - va_start(ap, value); - while ((key = va_arg(ap, char *))) - { - char *value = NULL; - - value = va_arg(ap, char *); - if (!pusb_hal_check_property(ctx, devices[i], - key, value)) - { - match = 0; - break; - } - match = 1; - } - if (match) + char **devices; + int n_devices; + char *udi = NULL; + va_list ap; + int i; + + devices = pusb_hal_find_all_items(ctx, property, value, &n_devices); + if (!devices) + return (NULL); + if (!n_devices) + return (NULL); + + for (i = 0; i < n_devices; ++i) { - udi = strdup(devices[i]); - break; + char *key = NULL; + int match = 1; + + va_start(ap, value); + while ((key = va_arg(ap, char *))) + { + char *value = NULL; + + value = va_arg(ap, char *); + if (!pusb_hal_check_property(ctx, devices[i], + key, value)) + { + match = 0; + break; + } + match = 1; + } + if (match) + { + udi = strdup(devices[i]); + break; + } + va_end(ap); } - va_end(ap); - } - libhal_free_string_array(devices); - return (udi); + libhal_free_string_array(devices); + return (udi); } diff --git a/src/hal.h b/src/hal.h index b4e8ea8..43f597a 100644 --- a/src/hal.h +++ b/src/hal.h @@ -18,20 +18,12 @@ #ifndef PUSB_HAL_H_ # define PUSB_HAL_H_ -DBusConnection *pusb_hal_dbus_connect(void); -void pusb_hal_dbus_disconnect(DBusConnection *dbus); -LibHalContext *pusb_hal_init(DBusConnection *dbus); -void pusb_hal_destroy(LibHalContext *ctx); -char *pusb_hal_get_property(LibHalContext *ctx, - const char *udi, - const char *name); -int pusb_hal_check_property(LibHalContext *ctx, - const char *udi, - const char *name, - const char *value); -char *pusb_hal_find_item(LibHalContext *ctx, - const char *property, - const char *value, - ...); +DBusConnection *pusb_hal_dbus_connect(void); +void pusb_hal_dbus_disconnect(DBusConnection *dbus); +LibHalContext *pusb_hal_init(DBusConnection *dbus); +void pusb_hal_destroy(LibHalContext *ctx); +char *pusb_hal_get_property(LibHalContext *ctx, const char *udi, const char *name); +int pusb_hal_check_property(LibHalContext *ctx, const char *udi, const char *name, const char *value); +char *pusb_hal_find_item(LibHalContext *ctx, const char *property, const char *value, ...); #endif /* !PUSB_HAL_H_ */ diff --git a/src/local.c b/src/local.c index 7372b0a..b085f49 100644 --- a/src/local.c +++ b/src/local.c @@ -22,41 +22,41 @@ #include "log.h" #include "conf.h" -int pusb_local_login(t_pusb_options *opts, const char *user) +int pusb_local_login(t_pusb_options *opts, const char *user) { - struct utmp utsearch; - struct utmp *utent; - const char *from; - int i; + struct utmp utsearch; + struct utmp *utent; + const char *from; + int i; - log_debug("Checking whether the caller is local or not...\n"); - from = ttyname(STDIN_FILENO); - if (!from || !(*from)) - { - log_debug("Couldn't retrieve the tty name, aborting.\n"); - return (1); - } - if (!strncmp(from, "/dev/", strlen("/dev/"))) - from += strlen("/dev/"); - log_debug("Authentication request from tty %s\n", from); - strncpy(utsearch.ut_line, from, sizeof(utsearch.ut_line)); - setutent(); - utent = getutline(&utsearch); - endutent(); - if (!utent) - { - log_debug("No utmp entry found for tty \"%s\"\n", - from); - return (1); - } - for (i = 0; i < 4; ++i) - { - if (utent->ut_addr_v6[i] != 0) + log_debug("Checking whether the caller is local or not...\n"); + from = ttyname(STDIN_FILENO); + if (!from || !(*from)) { - log_error("Remote authentication request: %s\n", utent->ut_host); - return (0); + log_debug("Couldn't retrieve the tty name, aborting.\n"); + return (1); } - } - log_debug("Caller is local (good)\n"); - return (1); + if (!strncmp(from, "/dev/", strlen("/dev/"))) + from += strlen("/dev/"); + log_debug("Authentication request from tty %s\n", from); + strncpy(utsearch.ut_line, from, sizeof(utsearch.ut_line)); + setutent(); + utent = getutline(&utsearch); + endutent(); + if (!utent) + { + log_debug("No utmp entry found for tty \"%s\"\n", + from); + return (1); + } + for (i = 0; i < 4; ++i) + { + if (utent->ut_addr_v6[i] != 0) + { + log_error("Remote authentication request: %s\n", utent->ut_host); + return (0); + } + } + log_debug("Caller is local (good)\n"); + return (1); } diff --git a/src/local.h b/src/local.h index 88e4ce7..1efb66c 100644 --- a/src/local.h +++ b/src/local.h @@ -18,6 +18,6 @@ #ifndef PUSB_LOCAL_H_ # define PUSB_LOCAL_H_ -int pusb_local_login(t_pusb_options *opts, const char *user); +int pusb_local_login(t_pusb_options *opts, const char *user); #endif /* !PUSB_LOCAL_H_ */ diff --git a/src/log.c b/src/log.c index 303f40d..9918982 100644 --- a/src/log.c +++ b/src/log.c @@ -23,74 +23,74 @@ static t_pusb_options *pusb_opts = NULL; -static void pusb_log_syslog(int level, const char *format, va_list ap) +static void pusb_log_syslog(int level, const char *format, va_list ap) { - openlog("pam_usb", LOG_PID, LOG_AUTH); - vsyslog(level, format, ap); - closelog(); + openlog("pam_usb", LOG_PID, LOG_AUTH); + vsyslog(level, format, ap); + closelog(); } -static void pusb_log_output(int level, const char *format, va_list ap) +static void pusb_log_output(int level, const char *format, va_list ap) { - if ((pusb_opts && !pusb_opts->quiet) || - level == LOG_ERR) - { - if (pusb_opts && pusb_opts->color_log) + if ((pusb_opts && !pusb_opts->quiet) || + level == LOG_ERR) { - if (level == LOG_ERR) - fprintf(stderr, "\033[01;31m*\033[00m "); - else if (level == LOG_NOTICE) - fprintf(stderr, "\033[01;32m*\033[00m "); + if (pusb_opts && pusb_opts->color_log) + { + if (level == LOG_ERR) + fprintf(stderr, "\033[01;31m*\033[00m "); + else if (level == LOG_NOTICE) + fprintf(stderr, "\033[01;32m*\033[00m "); + } + else + fprintf(stderr, "* "); + vfprintf(stderr, format, ap); } - else - fprintf(stderr, "* "); - vfprintf(stderr, format, ap); - } } -void __log_debug(const char *file, int line, const char *fmt, ...) +void __log_debug(const char *file, int line, const char *fmt, ...) { - va_list ap; + va_list ap; - if (!pusb_opts || !pusb_opts->debug) - return ; - fprintf(stderr, "[%s:%03d] ", file, line); - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); + if (!pusb_opts || !pusb_opts->debug) + return ; + fprintf(stderr, "[%s:%03d] ", file, line); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); - va_start(ap, fmt); - pusb_log_syslog(LOG_DEBUG, fmt, ap); - va_end(ap); + va_start(ap, fmt); + pusb_log_syslog(LOG_DEBUG, fmt, ap); + va_end(ap); } -void log_error(const char *fmt, ...) +void log_error(const char *fmt, ...) { - va_list ap; + va_list ap; - va_start(ap, fmt); - pusb_log_syslog(LOG_ERR, fmt, ap); - va_end(ap); + va_start(ap, fmt); + pusb_log_syslog(LOG_ERR, fmt, ap); + va_end(ap); - va_start(ap, fmt); - pusb_log_output(LOG_ERR, fmt, ap); - va_end(ap); + va_start(ap, fmt); + pusb_log_output(LOG_ERR, fmt, ap); + va_end(ap); } -void log_info(const char *fmt, ...) +void log_info(const char *fmt, ...) { - va_list ap; + va_list ap; - va_start(ap, fmt); - pusb_log_syslog(LOG_NOTICE, fmt, ap); - va_end(ap); + va_start(ap, fmt); + pusb_log_syslog(LOG_NOTICE, fmt, ap); + va_end(ap); - va_start(ap, fmt); - pusb_log_output(LOG_NOTICE, fmt, ap); - va_end(ap); + va_start(ap, fmt); + pusb_log_output(LOG_NOTICE, fmt, ap); + va_end(ap); } -void pusb_log_init(t_pusb_options *opts) +void pusb_log_init(t_pusb_options *opts) { - pusb_opts = opts; + pusb_opts = opts; } diff --git a/src/log.h b/src/log.h index ca439f7..7c779e8 100644 --- a/src/log.h +++ b/src/log.h @@ -20,10 +20,10 @@ # define log_debug(s, ...) __log_debug(__FILE__, __LINE__, s, ##__VA_ARGS__) # include "conf.h" -void __log_debug(const char *file, int line, const char *fmt, ...); -void log_error(const char *fmt, ...); -void log_info(const char *fmt, ...); -void pusb_log_init(t_pusb_options *opts); +void __log_debug(const char *file, int line, const char *fmt, ...); +void log_error(const char *fmt, ...); +void log_info(const char *fmt, ...); +void pusb_log_init(t_pusb_options *opts); #endif /* !PUSB_LOG_H_ */ diff --git a/src/pad.c b/src/pad.c index 177838c..88d0fab 100644 --- a/src/pad.c +++ b/src/pad.c @@ -30,193 +30,193 @@ #include "volume.h" #include "pad.h" -static FILE *pusb_pad_open_device(t_pusb_options *opts, - LibHalVolume *volume, - const char *user, - const char *mode) +static FILE *pusb_pad_open_device(t_pusb_options *opts, + LibHalVolume *volume, + const char *user, + const char *mode) { - FILE *f; - char path[PATH_MAX]; - const char *mnt_point; - struct stat sb; + FILE *f; + char path[PATH_MAX]; + const char *mnt_point; + struct stat sb; - mnt_point = (char *)libhal_volume_get_mount_point(volume); - if (!mnt_point) - return (NULL); - memset(path, 0x00, PATH_MAX); - snprintf(path, PATH_MAX, "%s/%s", mnt_point, opts->device_pad_directory); - if (stat(path, &sb) != 0) - { - log_debug("Directory %s does not exist, creating one.\n", path); - if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) + mnt_point = (char *)libhal_volume_get_mount_point(volume); + if (!mnt_point) + return (NULL); + memset(path, 0x00, PATH_MAX); + snprintf(path, PATH_MAX, "%s/%s", mnt_point, opts->device_pad_directory); + if (stat(path, &sb) != 0) { - log_debug("Unable to create directory %s: %s\n", path, - strerror(errno)); - return (NULL); + log_debug("Directory %s does not exist, creating one.\n", path); + if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) + { + log_debug("Unable to create directory %s: %s\n", path, + strerror(errno)); + return (NULL); + } + memset(path, 0x00, PATH_MAX); } - memset(path, 0x00, PATH_MAX); - } - snprintf(path, PATH_MAX, "%s/%s/%s.%s.pad", mnt_point, - opts->device_pad_directory, user, opts->hostname); - f = fopen(path, mode); - if (!f) - { - log_debug("Cannot open device file: %s\n", strerror(errno)); - return (NULL); - } - return (f); + snprintf(path, PATH_MAX, "%s/%s/%s.%s.pad", mnt_point, + opts->device_pad_directory, user, opts->hostname); + f = fopen(path, mode); + if (!f) + { + log_debug("Cannot open device file: %s\n", strerror(errno)); + return (NULL); + } + return (f); } -static FILE *pusb_pad_open_system(t_pusb_options *opts, - const char *user, - const char *mode) +static FILE *pusb_pad_open_system(t_pusb_options *opts, + const char *user, + const char *mode) { - FILE *f; - char path[PATH_MAX]; - struct passwd *user_ent = NULL; - struct stat sb; + FILE *f; + char path[PATH_MAX]; + struct passwd *user_ent = NULL; + struct stat sb; - if (!(user_ent = getpwnam(user)) || !(user_ent->pw_dir)) - { - log_error("Unable to retrieve informations for user \"%s\": %s\n", - strerror(errno)); - return (0); - } - memset(path, 0x00, PATH_MAX); - snprintf(path, PATH_MAX, "%s/%s", user_ent->pw_dir, - opts->system_pad_directory); - if (stat(path, &sb) != 0) - { - log_debug("Directory %s does not exist, creating one.\n", path); - if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) + if (!(user_ent = getpwnam(user)) || !(user_ent->pw_dir)) + { + log_error("Unable to retrieve informations for user \"%s\": %s\n", + strerror(errno)); + return (0); + } + memset(path, 0x00, PATH_MAX); + snprintf(path, PATH_MAX, "%s/%s", user_ent->pw_dir, + opts->system_pad_directory); + if (stat(path, &sb) != 0) + { + log_debug("Directory %s does not exist, creating one.\n", path); + if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) + { + log_debug("Unable to create directory %s: %s\n", path, + strerror(errno)); + return (NULL); + } + chown(path, user_ent->pw_uid, user_ent->pw_gid); + chmod(path, S_IRUSR | S_IWUSR | S_IXUSR); + } + memset(path, 0x00, PATH_MAX); + snprintf(path, PATH_MAX, "%s/%s/%s.pad", user_ent->pw_dir, + opts->system_pad_directory, opts->device.name); + f = fopen(path, mode); + if (!f) { - log_debug("Unable to create directory %s: %s\n", path, - strerror(errno)); - return (NULL); + log_debug("Cannot open system file: %s\n", strerror(errno)); + return (NULL); } - chown(path, user_ent->pw_uid, user_ent->pw_gid); - chmod(path, S_IRUSR | S_IWUSR | S_IXUSR); - } - memset(path, 0x00, PATH_MAX); - snprintf(path, PATH_MAX, "%s/%s/%s.pad", user_ent->pw_dir, - opts->system_pad_directory, opts->device.name); - f = fopen(path, mode); - if (!f) - { - log_debug("Cannot open system file: %s\n", strerror(errno)); - return (NULL); - } - return (f); + return (f); } -static int pusb_pad_protect(const char *user, int fd) +static int pusb_pad_protect(const char *user, int fd) { - struct passwd *user_ent = NULL; + struct passwd *user_ent = NULL; - log_debug("Protecting pad file...\n"); - if (!(user_ent = getpwnam(user))) - { - log_error("Unable to retrieve informations for user \"%s\": %s\n", - strerror(errno)); - return (0); - } - if (fchown(fd, user_ent->pw_uid, user_ent->pw_gid) == -1) - { - log_error("Unable to change owner of the pad: %s\n", - strerror(errno)); - return (0); - } - if (fchmod(fd, S_IRUSR | S_IWUSR) == -1) - { - log_error("Unable to change mode of the pad: %s\n", - strerror(errno)); - return (0); - } - return (1); + log_debug("Protecting pad file...\n"); + if (!(user_ent = getpwnam(user))) + { + log_error("Unable to retrieve informations for user \"%s\": %s\n", + strerror(errno)); + return (0); + } + if (fchown(fd, user_ent->pw_uid, user_ent->pw_gid) == -1) + { + log_error("Unable to change owner of the pad: %s\n", + strerror(errno)); + return (0); + } + if (fchmod(fd, S_IRUSR | S_IWUSR) == -1) + { + log_error("Unable to change mode of the pad: %s\n", + strerror(errno)); + return (0); + } + return (1); } -static void pusb_pad_update(t_pusb_options *opts, - LibHalVolume *volume, - const char *user) +static void pusb_pad_update(t_pusb_options *opts, + LibHalVolume *volume, + const char *user) { - FILE *f_device = NULL; - FILE *f_system = NULL; - char magic[1024]; - int i; + FILE *f_device = NULL; + FILE *f_system = NULL; + char magic[1024]; + int i; - if (!(f_device = pusb_pad_open_device(opts, volume, user, "w+"))) - { - log_error("Unable to update pads.\n"); - return ; - } - pusb_pad_protect(user, fileno(f_device)); - if (!(f_system = pusb_pad_open_system(opts, user, "w+"))) - { - log_error("Unable to update pads.\n"); - fclose(f_device); - return ; - } - pusb_pad_protect(user, fileno(f_system)); - log_debug("Generating %d bytes unique pad...\n", sizeof(magic)); - srand(getpid() * time(NULL)); - for (i = 0; i < sizeof(magic); ++i) - magic[i] = (char)rand(); - log_debug("Writing pad to the device...\n"); - fwrite(magic, sizeof(char), sizeof(magic), f_system); - log_debug("Writing pad to the system...\n"); - fwrite(magic, sizeof(char), sizeof(magic), f_device); - log_debug("Synchronizing filesystems...\n"); - fclose(f_system); - fclose(f_device); - sync(); - log_debug("One time pads updated.\n"); + if (!(f_device = pusb_pad_open_device(opts, volume, user, "w+"))) + { + log_error("Unable to update pads.\n"); + return ; + } + pusb_pad_protect(user, fileno(f_device)); + if (!(f_system = pusb_pad_open_system(opts, user, "w+"))) + { + log_error("Unable to update pads.\n"); + fclose(f_device); + return ; + } + pusb_pad_protect(user, fileno(f_system)); + log_debug("Generating %d bytes unique pad...\n", sizeof(magic)); + srand(getpid() * time(NULL)); + for (i = 0; i < sizeof(magic); ++i) + magic[i] = (char)rand(); + log_debug("Writing pad to the device...\n"); + fwrite(magic, sizeof(char), sizeof(magic), f_system); + log_debug("Writing pad to the system...\n"); + fwrite(magic, sizeof(char), sizeof(magic), f_device); + log_debug("Synchronizing filesystems...\n"); + fclose(f_system); + fclose(f_device); + sync(); + log_debug("One time pads updated.\n"); } -static int pusb_pad_compare(t_pusb_options *opts, LibHalVolume *volume, - const char *user) +static int pusb_pad_compare(t_pusb_options *opts, LibHalVolume *volume, + const char *user) { - FILE *f_device = NULL; - FILE *f_system = NULL; - char magic_device[1024]; - char magic_system[1024]; - int retval; + FILE *f_device = NULL; + FILE *f_system = NULL; + char magic_device[1024]; + char magic_system[1024]; + int retval; - if (!(f_system = pusb_pad_open_system(opts, user, "r"))) - return (1); - if (!(f_device = pusb_pad_open_device(opts, volume, user, "r"))) - { - fclose(f_system); - return (0); - } - log_debug("Loading device pad...\n"); - fread(magic_device, sizeof(char), sizeof(magic_device), f_device); - log_debug("Loading system pad...\n"); - fread(magic_system, sizeof(char), sizeof(magic_system), f_system); - retval = memcmp(magic_system, magic_device, sizeof(magic_system)); - fclose(f_system); - fclose(f_device); - if (!retval) - log_debug("Pad match.\n"); - return (retval == 0); + if (!(f_system = pusb_pad_open_system(opts, user, "r"))) + return (1); + if (!(f_device = pusb_pad_open_device(opts, volume, user, "r"))) + { + fclose(f_system); + return (0); + } + log_debug("Loading device pad...\n"); + fread(magic_device, sizeof(char), sizeof(magic_device), f_device); + log_debug("Loading system pad...\n"); + fread(magic_system, sizeof(char), sizeof(magic_system), f_system); + retval = memcmp(magic_system, magic_device, sizeof(magic_system)); + fclose(f_system); + fclose(f_device); + if (!retval) + log_debug("Pad match.\n"); + return (retval == 0); } -int pusb_pad_check(t_pusb_options *opts, LibHalContext *ctx, - const char *user) +int pusb_pad_check(t_pusb_options *opts, LibHalContext *ctx, + const char *user) { - LibHalVolume *volume = NULL; - int retval; + LibHalVolume *volume = NULL; + int retval; - volume = pusb_volume_get(opts, ctx); - if (!volume) - return (0); - retval = pusb_pad_compare(opts, volume, user); - if (retval) - { - log_info("Verification match, updating one time pads...\n"); - pusb_pad_update(opts, volume, user); - } - else - log_error("Pad checking failed !\n"); - pusb_volume_destroy(volume); - return (retval); + volume = pusb_volume_get(opts, ctx); + if (!volume) + return (0); + retval = pusb_pad_compare(opts, volume, user); + if (retval) + { + log_info("Verification match, updating one time pads...\n"); + pusb_pad_update(opts, volume, user); + } + else + log_error("Pad checking failed !\n"); + pusb_volume_destroy(volume); + return (retval); } diff --git a/src/pad.h b/src/pad.h index 18bbf9d..3814ab1 100644 --- a/src/pad.h +++ b/src/pad.h @@ -18,7 +18,6 @@ #ifndef PUSB_OTP_H_ # define PUSB_OTP_H_ -int pusb_pad_check(t_pusb_options *opts, LibHalContext *ctx, - const char *user); +int pusb_pad_check(t_pusb_options *opts, LibHalContext *ctx, const char *user); #endif /* !PUSB_OTP_H_ */ diff --git a/src/pam.c b/src/pam.c index 9ce2dc1..539f38c 100644 --- a/src/pam.c +++ b/src/pam.c @@ -26,86 +26,86 @@ #include "device.h" PAM_EXTERN -int pam_sm_authenticate(pam_handle_t *pamh, int flags, - int argc, const char **argv) +int pam_sm_authenticate(pam_handle_t *pamh, int flags, + int argc, const char **argv) { - t_pusb_options opts; - const char *service; - const char *user; - const char *tty; - char *conf_file = PUSB_CONF_FILE; - int retval; + t_pusb_options opts; + const char *service; + const char *user; + const char *tty; + char *conf_file = PUSB_CONF_FILE; + int retval; - retval = pam_get_item(pamh, PAM_SERVICE, (const void **)&service); - if (retval != PAM_SUCCESS) - { - log_error("Unable to retrieve the PAM service name.\n"); - return (PAM_AUTH_ERR); - } + retval = pam_get_item(pamh, PAM_SERVICE, (const void **)&service); + if (retval != PAM_SUCCESS) + { + log_error("Unable to retrieve the PAM service name.\n"); + return (PAM_AUTH_ERR); + } - if (pam_get_user(pamh, &user, NULL) != PAM_SUCCESS || !user || !*user) - { - log_error("Unable to retrieve the PAM user name.\n"); - return (PAM_AUTH_ERR); - } + if (pam_get_user(pamh, &user, NULL) != PAM_SUCCESS || !user || !*user) + { + log_error("Unable to retrieve the PAM user name.\n"); + return (PAM_AUTH_ERR); + } - if (argc > 1) - if (!strcmp(argv[0], "-c")) - conf_file = (char *)argv[1]; - pusb_conf_init(&opts); - if (!pusb_conf_parse(conf_file, &opts, user, service)) - return (PAM_AUTH_ERR); + if (argc > 1) + if (!strcmp(argv[0], "-c")) + conf_file = (char *)argv[1]; + pusb_conf_init(&opts); + if (!pusb_conf_parse(conf_file, &opts, user, service)) + return (PAM_AUTH_ERR); - pusb_log_init(&opts); - if (!opts.enable) - { - log_debug("Not enabled, exiting...\n"); - return (PAM_IGNORE); - } + pusb_log_init(&opts); + if (!opts.enable) + { + log_debug("Not enabled, exiting...\n"); + return (PAM_IGNORE); + } - log_info("pam_usb v%s\n", PUSB_VERSION); - log_info("Authentication request for user \"%s\" (%s)\n", - user, service); + log_info("pam_usb v%s\n", PUSB_VERSION); + log_info("Authentication request for user \"%s\" (%s)\n", + user, service); - if (pam_get_item(pamh, PAM_TTY, (const void **)&tty) == PAM_SUCCESS) - { - if (tty && !strcmp(tty, "ssh")) + if (pam_get_item(pamh, PAM_TTY, (const void **)&tty) == PAM_SUCCESS) + { + if (tty && !strcmp(tty, "ssh")) + { + log_debug("SSH Authentication, aborting.\n"); + return (0); + } + } + if (!pusb_local_login(&opts, user)) + { + log_error("Access denied.\n"); + return (PAM_AUTH_ERR); + } + if (pusb_device_check(&opts, user)) { - log_debug("SSH Authentication, aborting.\n"); - return (0); + log_info("Access granted.\n"); + return (PAM_SUCCESS); } - } - if (!pusb_local_login(&opts, user)) - { - log_error("Access denied.\n"); - return (PAM_AUTH_ERR); - } - if (pusb_device_check(&opts, user)) - { - log_info("Access granted.\n"); - return (PAM_SUCCESS); - } - log_error("Access denied.\n"); - return (PAM_AUTH_ERR); + log_error("Access denied.\n"); + return (PAM_AUTH_ERR); } PAM_EXTERN -int pam_sm_setcred(pam_handle_t *pamh,int flags,int argc, - const char **argv) +int pam_sm_setcred(pam_handle_t *pamh,int flags,int argc, + const char **argv) { - return (PAM_SUCCESS); + return (PAM_SUCCESS); } #ifdef PAM_STATIC struct pam_module _pam_usb_modstruct = { - "pam_usb", - pam_sm_authenticate, - pam_sm_setcred, - NULL, - NULL, - NULL, - NULL + "pam_usb", + pam_sm_authenticate, + pam_sm_setcred, + NULL, + NULL, + NULL, + NULL }; #endif diff --git a/src/volume.c b/src/volume.c index 5c3dd01..449d71d 100644 --- a/src/volume.c +++ b/src/volume.c @@ -28,125 +28,125 @@ #include "hal.h" #include "volume.h" -static int pusb_volume_mount(t_pusb_options *opts, LibHalVolume **volume, - LibHalContext *ctx) +static int pusb_volume_mount(t_pusb_options *opts, LibHalVolume **volume, + LibHalContext *ctx) { - char command[1024]; - char tempname[32]; - const char *devname; - const char *udi; - const char *fs; + char command[1024]; + char tempname[32]; + const char *devname; + const char *udi; + const char *fs; - snprintf(tempname, sizeof(tempname), "pam_usb%d", getpid()); - if (!(devname = libhal_volume_get_device_file(*volume))) - { - log_error("Unable to retrieve device filename\n"); - return (0); - } - fs = libhal_volume_get_fstype(*volume); - log_debug("Attempting to mount device %s with label %s\n", - devname, tempname); - if (!fs) - snprintf(command, sizeof(command), "pmount -s %s %s", - devname, tempname); - else - snprintf(command, sizeof(command), "pmount -s -t %s %s %s", - fs, devname, tempname); - log_debug("Executing \"%s\"\n", command); - if (system(command) != 0) - { - log_error("Mount failed\n"); - return (0); - } - udi = libhal_volume_get_udi(*volume); - if (!udi) - { - log_error("Unable to retrieve volume UDI\n"); - return (0); - } - udi = strdup(udi); - libhal_volume_free(*volume); - *volume = libhal_volume_from_udi(ctx, udi); - free((char *)udi); - log_debug("Mount succeeded.\n"); - return (1); + snprintf(tempname, sizeof(tempname), "pam_usb%d", getpid()); + if (!(devname = libhal_volume_get_device_file(*volume))) + { + log_error("Unable to retrieve device filename\n"); + return (0); + } + fs = libhal_volume_get_fstype(*volume); + log_debug("Attempting to mount device %s with label %s\n", + devname, tempname); + if (!fs) + snprintf(command, sizeof(command), "pmount -s %s %s", + devname, tempname); + else + snprintf(command, sizeof(command), "pmount -s -t %s %s %s", + fs, devname, tempname); + log_debug("Executing \"%s\"\n", command); + if (system(command) != 0) + { + log_error("Mount failed\n"); + return (0); + } + udi = libhal_volume_get_udi(*volume); + if (!udi) + { + log_error("Unable to retrieve volume UDI\n"); + return (0); + } + udi = strdup(udi); + libhal_volume_free(*volume); + *volume = libhal_volume_from_udi(ctx, udi); + free((char *)udi); + log_debug("Mount succeeded.\n"); + return (1); } static LibHalVolume *pusb_volume_probe(t_pusb_options *opts, - LibHalContext *ctx) + LibHalContext *ctx) { - LibHalVolume *volume = NULL; - int maxtries = 0; - int i; - - if (!*(opts->device.volume_uuid)) - { - log_debug("No UUID configured for device\n"); - return (NULL); - } - log_debug("Searching for volume with uuid %s\n", opts->device.volume_uuid); - maxtries = ((opts->probe_timeout * 1000000) / 250000); - for (i = 0; i < maxtries; ++i) - { - char *udi = NULL; + LibHalVolume *volume = NULL; + int maxtries = 0; + int i; - if (i == 1) - log_info("Probing volume (this could take a while)...\n"); - udi = pusb_hal_find_item(ctx, - "volume.uuid", opts->device.volume_uuid, - NULL); - if (!udi) + if (!*(opts->device.volume_uuid)) { - usleep(250000); - continue; + log_debug("No UUID configured for device\n"); + return (NULL); } - volume = libhal_volume_from_udi(ctx, udi); - libhal_free_string(udi); - if (!libhal_volume_should_ignore(volume)) - return (volume); - libhal_volume_free(volume); - usleep(250000); - } - return (NULL); + log_debug("Searching for volume with uuid %s\n", opts->device.volume_uuid); + maxtries = ((opts->probe_timeout * 1000000) / 250000); + for (i = 0; i < maxtries; ++i) + { + char *udi = NULL; + + if (i == 1) + log_info("Probing volume (this could take a while)...\n"); + udi = pusb_hal_find_item(ctx, + "volume.uuid", opts->device.volume_uuid, + NULL); + if (!udi) + { + usleep(250000); + continue; + } + volume = libhal_volume_from_udi(ctx, udi); + libhal_free_string(udi); + if (!libhal_volume_should_ignore(volume)) + return (volume); + libhal_volume_free(volume); + usleep(250000); + } + return (NULL); } -LibHalVolume *pusb_volume_get(t_pusb_options *opts, LibHalContext *ctx) +LibHalVolume *pusb_volume_get(t_pusb_options *opts, LibHalContext *ctx) { - LibHalVolume *volume; + LibHalVolume *volume; - if (!(volume = pusb_volume_probe(opts, ctx))) - return (NULL); - log_debug("Found volume %s\n", opts->device.volume_uuid); - if (libhal_volume_is_mounted(volume)) - { - log_debug("Volume is already mounted.\n"); - return (volume); - } - if (!pusb_volume_mount(opts, &volume, ctx)) - { - libhal_volume_free(volume); - return (NULL); - } - return (volume); + if (!(volume = pusb_volume_probe(opts, ctx))) + return (NULL); + log_debug("Found volume %s\n", opts->device.volume_uuid); + if (libhal_volume_is_mounted(volume)) + { + log_debug("Volume is already mounted.\n"); + return (volume); + } + if (!pusb_volume_mount(opts, &volume, ctx)) + { + libhal_volume_free(volume); + return (NULL); + } + return (volume); } -void pusb_volume_destroy(LibHalVolume *volume) +void pusb_volume_destroy(LibHalVolume *volume) { - const char *mntpoint; + const char *mntpoint; - mntpoint = libhal_volume_get_mount_point(volume); - if (mntpoint && strstr(mntpoint, "pam_usb")) - { - char command[1024]; + mntpoint = libhal_volume_get_mount_point(volume); + if (mntpoint && strstr(mntpoint, "pam_usb")) + { + char command[1024]; - log_debug("Attempting to umount %s\n", - mntpoint); - snprintf(command, sizeof(command), "pumount %s", mntpoint); - log_debug("Executing \"%s\"\n", command); - if (!system(command)) - log_debug("Umount succeeded.\n"); - else - log_error("Unable to umount %s\n", mntpoint); - } - libhal_volume_free(volume); + log_debug("Attempting to umount %s\n", + mntpoint); + snprintf(command, sizeof(command), "pumount %s", mntpoint); + log_debug("Executing \"%s\"\n", command); + if (!system(command)) + log_debug("Umount succeeded.\n"); + else + log_error("Unable to umount %s\n", mntpoint); + } + libhal_volume_free(volume); } diff --git a/src/volume.h b/src/volume.h index 53d604b..74ab98d 100644 --- a/src/volume.h +++ b/src/volume.h @@ -18,7 +18,7 @@ #ifndef VOLUME_H_ # define VOLUME_H_ -LibHalVolume *pusb_volume_get(t_pusb_options *opts, LibHalContext *ctx); -void pusb_volume_destroy(LibHalVolume *volume); +LibHalVolume *pusb_volume_get(t_pusb_options *opts, LibHalContext *ctx); +void pusb_volume_destroy(LibHalVolume *volume); #endif /* !VOLUME_H_ */ diff --git a/src/xpath.c b/src/xpath.c index 3d7203f..cd2ffeb 100644 --- a/src/xpath.c +++ b/src/xpath.c @@ -21,199 +21,199 @@ #include "xpath.h" #include "log.h" -static xmlXPathObject *pusb_xpath_match(xmlDocPtr doc, const char *path) +static xmlXPathObject *pusb_xpath_match(xmlDocPtr doc, const char *path) { - xmlXPathContext *context = NULL; - xmlXPathObject *result = NULL; - - context = xmlXPathNewContext(doc); - if (context == NULL) - { - log_error("Unable to create XML context\n"); - return (NULL); - } - result = xmlXPathEvalExpression((xmlChar *)path, context); - xmlXPathFreeContext(context); - if (result == NULL) - { - log_error("Error in xmlXPathEvalExpression\n"); - return (NULL); - } - if (xmlXPathNodeSetIsEmpty(result->nodesetval)) - { - xmlXPathFreeObject(result); - return (NULL); - } - return (result); + xmlXPathContext *context = NULL; + xmlXPathObject *result = NULL; + + context = xmlXPathNewContext(doc); + if (context == NULL) + { + log_error("Unable to create XML context\n"); + return (NULL); + } + result = xmlXPathEvalExpression((xmlChar *)path, context); + xmlXPathFreeContext(context); + if (result == NULL) + { + log_error("Error in xmlXPathEvalExpression\n"); + return (NULL); + } + if (xmlXPathNodeSetIsEmpty(result->nodesetval)) + { + xmlXPathFreeObject(result); + return (NULL); + } + return (result); } -static int pusb_xpath_strip_string(char *dest, const char *src, - size_t size) +static int pusb_xpath_strip_string(char *dest, const char *src, + size_t size) { - int first_char = -1; - int last_char = -1; - int i; + int first_char = -1; + int last_char = -1; + int i; - for (i = 0; src[i]; ++i) - { - if (isspace(src[i])) - continue ; + for (i = 0; src[i]; ++i) + { + if (isspace(src[i])) + continue ; - if (first_char == -1) - first_char = i; - last_char = i; - } + if (first_char == -1) + first_char = i; + last_char = i; + } - if (first_char == -1 || last_char == -1) - return (0); + if (first_char == -1 || last_char == -1) + return (0); - if ((last_char - first_char) > (size - 1)) - return (0); + if ((last_char - first_char) > (size - 1)) + return (0); - memset(dest, 0x0, size); - strncpy(dest, &(src[first_char]), last_char - first_char + 1); - return (1); + memset(dest, 0x0, size); + strncpy(dest, &(src[first_char]), last_char - first_char + 1); + return (1); } -int pusb_xpath_get_string(xmlDocPtr doc, const char *path, - char *value, size_t size) +int pusb_xpath_get_string(xmlDocPtr doc, const char *path, + char *value, size_t size) { - xmlXPathObject *result = NULL; - xmlNode *node = NULL; - xmlChar *result_string = NULL; - - if (!(result = pusb_xpath_match(doc, path))) - return (0); - - if (result->nodesetval->nodeNr > 1) - { - xmlXPathFreeObject(result); - log_debug("Syntax error: %s: more than one record found\n", path); - return (0); - } - - node = result->nodesetval->nodeTab[0]->xmlChildrenNode; - result_string = xmlNodeListGetString(doc, node, 1); - if (!result_string) - { - xmlXPathFreeObject(result); - log_debug("Empty value for %s\n", path); - return (0); - } - if (!pusb_xpath_strip_string(value, (const char *)result_string, size)) - { - xmlFree(result_string); - xmlXPathFreeObject(result); - log_debug("Result for %s (%s) is too long (max: %d)\n", - path, (const char *)result_string, size); - return (0); - } - xmlFree(result_string); - xmlXPathFreeObject(result); - return (1); + xmlXPathObject *result = NULL; + xmlNode *node = NULL; + xmlChar *result_string = NULL; + + if (!(result = pusb_xpath_match(doc, path))) + return (0); + + if (result->nodesetval->nodeNr > 1) + { + xmlXPathFreeObject(result); + log_debug("Syntax error: %s: more than one record found\n", path); + return (0); + } + + node = result->nodesetval->nodeTab[0]->xmlChildrenNode; + result_string = xmlNodeListGetString(doc, node, 1); + if (!result_string) + { + xmlXPathFreeObject(result); + log_debug("Empty value for %s\n", path); + return (0); + } + if (!pusb_xpath_strip_string(value, (const char *)result_string, size)) + { + xmlFree(result_string); + xmlXPathFreeObject(result); + log_debug("Result for %s (%s) is too long (max: %d)\n", + path, (const char *)result_string, size); + return (0); + } + xmlFree(result_string); + xmlXPathFreeObject(result); + return (1); } -int pusb_xpath_get_string_from(xmlDocPtr doc, - const char *base, - const char *path, - char *value, size_t size) +int pusb_xpath_get_string_from(xmlDocPtr doc, + const char *base, + const char *path, + char *value, size_t size) { - char *xpath = NULL; - size_t xpath_size; - int retval; - - xpath_size = strlen(base) + strlen(path) + 1; - if (!(xpath = malloc(xpath_size))) - { - log_error("malloc error !\n"); - return (0); - } - memset(xpath, 0x00, xpath_size); - snprintf(xpath, xpath_size, "%s%s", base, path); - retval = pusb_xpath_get_string(doc, xpath, value, size); - if (retval) - log_debug("%s%s -> %s\n", base, path, value); - free(xpath); - return (retval); + char *xpath = NULL; + size_t xpath_size; + int retval; + + xpath_size = strlen(base) + strlen(path) + 1; + if (!(xpath = malloc(xpath_size))) + { + log_error("malloc error !\n"); + return (0); + } + memset(xpath, 0x00, xpath_size); + snprintf(xpath, xpath_size, "%s%s", base, path); + retval = pusb_xpath_get_string(doc, xpath, value, size); + if (retval) + log_debug("%s%s -> %s\n", base, path, value); + free(xpath); + return (retval); } -int pusb_xpath_get_bool(xmlDocPtr doc, const char *path, int *value) +int pusb_xpath_get_bool(xmlDocPtr doc, const char *path, int *value) { - char ret[6]; /* strlen("false") + 1 */ + char ret[6]; /* strlen("false") + 1 */ - if (!pusb_xpath_get_string(doc, path, ret, sizeof(ret))) - return (0); + if (!pusb_xpath_get_string(doc, path, ret, sizeof(ret))) + return (0); - if (!strcmp(ret, "true")) - { - *value = 1; - return (1); - } + if (!strcmp(ret, "true")) + { + *value = 1; + return (1); + } - if (!strcmp(ret, "false")) - { - *value = 0; - return (1); - } + if (!strcmp(ret, "false")) + { + *value = 0; + return (1); + } - log_debug("Expecting a boolean, got %s\n", ret); - return (0); + log_debug("Expecting a boolean, got %s\n", ret); + return (0); } -int pusb_xpath_get_bool_from(xmlDocPtr doc, - const char *base, - const char *path, - int *value) +int pusb_xpath_get_bool_from(xmlDocPtr doc, + const char *base, + const char *path, + int *value) { - char *xpath = NULL; - size_t xpath_size; - int retval; - - xpath_size = strlen(base) + strlen(path) + 1; - if (!(xpath = malloc(xpath_size))) - { - log_error("malloc error!\n"); - return (0); - } - memset(xpath, 0x00, xpath_size); - snprintf(xpath, xpath_size, "%s%s", base, path); - retval = pusb_xpath_get_bool(doc, xpath, value); - free(xpath); - if (retval) - log_debug("%s%s -> %s\n", base, path, *value ? "true" : "false"); - return (retval); + char *xpath = NULL; + size_t xpath_size; + int retval; + + xpath_size = strlen(base) + strlen(path) + 1; + if (!(xpath = malloc(xpath_size))) + { + log_error("malloc error!\n"); + return (0); + } + memset(xpath, 0x00, xpath_size); + snprintf(xpath, xpath_size, "%s%s", base, path); + retval = pusb_xpath_get_bool(doc, xpath, value); + free(xpath); + if (retval) + log_debug("%s%s -> %s\n", base, path, *value ? "true" : "false"); + return (retval); } -int pusb_xpath_get_int(xmlDocPtr doc, const char *path, int *value) +int pusb_xpath_get_int(xmlDocPtr doc, const char *path, int *value) { - char ret[64]; /* strlen("false") + 1 */ + char ret[64]; /* strlen("false") + 1 */ - if (!pusb_xpath_get_string(doc, path, ret, sizeof(ret))) - return (0); - *value = atoi(ret); - return (1); + if (!pusb_xpath_get_string(doc, path, ret, sizeof(ret))) + return (0); + *value = atoi(ret); + return (1); } -int pusb_xpath_get_int_from(xmlDocPtr doc, - const char *base, - const char *path, - int *value) +int pusb_xpath_get_int_from(xmlDocPtr doc, + const char *base, + const char *path, + int *value) { - char *xpath = NULL; - size_t xpath_size; - int retval; - - xpath_size = strlen(base) + strlen(path) + 1; - if (!(xpath = malloc(xpath_size))) - { - log_error("malloc error!\n"); - return (0); - } - memset(xpath, 0x00, xpath_size); - snprintf(xpath, xpath_size, "%s%s", base, path); - retval = pusb_xpath_get_int(doc, xpath, value); - free(xpath); - if (retval) - log_debug("%s%s -> %d\n", base, path, *value); - return (retval); + char *xpath = NULL; + size_t xpath_size; + int retval; + + xpath_size = strlen(base) + strlen(path) + 1; + if (!(xpath = malloc(xpath_size))) + { + log_error("malloc error!\n"); + return (0); + } + memset(xpath, 0x00, xpath_size); + snprintf(xpath, xpath_size, "%s%s", base, path); + retval = pusb_xpath_get_int(doc, xpath, value); + free(xpath); + if (retval) + log_debug("%s%s -> %d\n", base, path, *value); + return (retval); } diff --git a/src/xpath.h b/src/xpath.h index 76ec59d..fd6995d 100644 --- a/src/xpath.h +++ b/src/xpath.h @@ -19,17 +19,11 @@ # define PUSB_XPATH_H_ # include -int pusb_xpath_get_string(xmlDocPtr doc, const char *path, char *value, - size_t size); -int pusb_xpath_get_bool(xmlDocPtr doc, const char *path, int *value); -int pusb_xpath_get_string_from(xmlDocPtr doc, const char *base, - const char *path, char *value, size_t size); -int pusb_xpath_get_bool_from(xmlDocPtr doc, const char *base, - const char *path, int *value); -int pusb_xpath_get_int(xmlDocPtr doc, const char *path, int *value); -int pusb_xpath_get_int_from(xmlDocPtr doc, - const char *base, - const char *path, - int *value); +int pusb_xpath_get_string(xmlDocPtr doc, const char *path, char *value, size_t size); +int pusb_xpath_get_bool(xmlDocPtr doc, const char *path, int *value); +int pusb_xpath_get_string_from(xmlDocPtr doc, const char *base, const char *path, char *value, size_t size); +int pusb_xpath_get_bool_from(xmlDocPtr doc, const char *base, const char *path, int *value); +int pusb_xpath_get_int(xmlDocPtr doc, const char *path, int *value); +int pusb_xpath_get_int_from(xmlDocPtr doc, const char *base, const char *path, int *value); #endif /* !PUSB_XPATH_H_ */