>From 428c7e9febf470ca737ab93e2cce3b20ee4f482b Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Sun, 11 May 2014 16:15:24 +0200 Subject: [PATCH 1/3] IFP: Add utility functions to escape and unescape object paths D-Bus spec says: Each element must only contain the ASCII characters "[A-Z][a-z][0-9]_" This patch adds two utility functions to escape raw input into format suitable for DBus and conversely transform escaped paths back into raw paths. --- src/responder/ifp/ifp_private.h | 5 +- src/responder/ifp/ifpsrv_util.c | 120 ++++++++++++++++++++++++++++++++++++++++ src/tests/cmocka/test_ifp.c | 96 ++++++++++++++++++++++++++++++++ 3 files changed, 220 insertions(+), 1 deletion(-) diff --git a/src/responder/ifp/ifp_private.h b/src/responder/ifp/ifp_private.h index 41598c819bfe924dc1d1c81c6b8c8c609705adcd..286cc75c9b774b0fd9d18ef6166a7c3f1ae41e53 100644 --- a/src/responder/ifp/ifp_private.h +++ b/src/responder/ifp/ifp_private.h @@ -69,9 +69,12 @@ errno_t ifp_req_create(struct sbus_request *dbus_req, int ifp_req_create_handle_failure(struct sbus_request *dbus_req, errno_t err); const char *ifp_path_strip_prefix(const char *path, const char *prefix); + +char *ifp_bus_path_unescape(TALLOC_CTX *mem_ctx, const char *path); +char *ifp_bus_path_escape(TALLOC_CTX *mem_ctx, const char *path); + errno_t ifp_add_ldb_el_to_dict(DBusMessageIter *iter_dict, struct ldb_message_element *el); - const char **ifp_parse_attr_list(TALLOC_CTX *mem_ctx, const char *conf_str); bool ifp_attr_allowed(const char *whitelist[], const char *attr); #endif /* _IFPSRV_PRIVATE_H_ */ diff --git a/src/responder/ifp/ifpsrv_util.c b/src/responder/ifp/ifpsrv_util.c index 4ef0be6afe5fc1b638eae69166a40dcd400d7c37..facfff6c8ec859a8d2156386bcb1e1ae1d129500 100644 --- a/src/responder/ifp/ifpsrv_util.c +++ b/src/responder/ifp/ifpsrv_util.c @@ -106,6 +106,126 @@ const char *ifp_path_strip_prefix(const char *path, const char *prefix) return NULL; } +/* The following path related functions are based on similar code in + * storaged, just tailored to use talloc instead of glib + */ +char *ifp_bus_path_escape(TALLOC_CTX *mem_ctx, const char *path) +{ + size_t n; + char *safe_path = NULL; + TALLOC_CTX *tmp_ctx = NULL; + + tmp_ctx = talloc_new(NULL); + if (tmp_ctx == NULL) { + return NULL; + } + + safe_path = talloc_strdup(tmp_ctx, ""); + if (safe_path == NULL) { + goto done; + } + + for (n = 0; path[n]; n++) { + int c = path[n]; + /* D-Bus spec says: + * * + * * Each element must only contain the ASCII characters + * "[A-Z][a-z][0-9]_" + * */ + if ((c >= 'A' && c <= 'Z') + || (c >= 'a' && c <= 'z') + || (c >= '0' && c <= '9') + || c == '_') { + safe_path = talloc_asprintf_append_buffer(safe_path, "%c", c); + if (safe_path == NULL) { + goto done; + } + } else { + safe_path = talloc_asprintf_append_buffer(safe_path, "_%02x", c); + if (safe_path == NULL) { + goto done; + } + } + } + + safe_path = talloc_steal(mem_ctx, safe_path); +done: + talloc_free(tmp_ctx); + return safe_path; +} + +static inline int unhexchar(char c) +{ + if (c >= '0' && c <= '9') { + return c - '0'; + } + + if (c >= 'a' && c <= 'f') { + return c - 'a' + 10; + } + + if (c >= 'A' && c <= 'F') { + return c - 'A' + 10; + } + + return -1; +} + +char *ifp_bus_path_unescape(TALLOC_CTX *mem_ctx, const char *path) +{ + char *safe_path; + const char *p; + int a, b, c; + TALLOC_CTX *tmp_ctx = NULL; + + tmp_ctx = talloc_new(NULL); + if (tmp_ctx == NULL) { + return NULL; + } + + safe_path = talloc_strdup(tmp_ctx, ""); + if (safe_path == NULL) { + goto done; + } + + /* Special case for the empty string */ + if (strcmp(path, "_") == 0) { + safe_path = talloc_steal(mem_ctx, safe_path); + goto done; + } + + for (p = path; *p; p++) { + if (*p == '_') { + /* There must be at least two more chars after underscore */ + if (p[1] == '\0' || p[2] == '\0') { + safe_path = NULL; + goto done; + } + + if ((a = unhexchar(p[1])) < 0 + || (b = unhexchar(p[2])) < 0) { + /* Invalid escape code, let's take it literal then */ + c = '_'; + } else { + c = ((a << 4) | b); + p += 2; + } + } else { + c = *p; + } + + safe_path = talloc_asprintf_append_buffer(safe_path, "%c", c); + if (safe_path == NULL) { + goto done; + } + } + + safe_path = talloc_steal(mem_ctx, safe_path); +done: + talloc_free(tmp_ctx); + return safe_path; +} + errno_t ifp_add_ldb_el_to_dict(DBusMessageIter *iter_dict, struct ldb_message_element *el) { diff --git a/src/tests/cmocka/test_ifp.c b/src/tests/cmocka/test_ifp.c index 70ebf942dcb1e366516bed92620dece6dedd0a0d..5dfa04b069635f42d27ac911977f28be8db37499 100644 --- a/src/tests/cmocka/test_ifp.c +++ b/src/tests/cmocka/test_ifp.c @@ -28,6 +28,11 @@ #include "responder/ifp/ifp_private.h" #include "sbus/sssd_dbus_private.h" +/* dbus library checks for valid object paths when unit testing, we don't + * want that */ +#undef DBUS_TYPE_OBJECT_PATH +#define DBUS_TYPE_OBJECT_PATH ((int) 's') + static struct ifp_ctx * mock_ifp_ctx(TALLOC_CTX *mem_ctx) { @@ -305,6 +310,95 @@ void test_attr_allowed(void **state) assert_false(ifp_attr_allowed(NULL, "name")); } +void test_path_escape(void **state) +{ + char *p; + TALLOC_CTX *mem_ctx; + + assert_true(leak_check_setup()); + mem_ctx = talloc_new(global_talloc_context); + + p = ifp_bus_path_escape(mem_ctx, "noescape"); + assert_non_null(p); + assert_string_equal(p, "noescape"); + talloc_free(p); + + p = ifp_bus_path_escape(mem_ctx, "redhat.com"); + assert_non_null(p); + assert_string_equal(p, "redhat_2ecom"); + talloc_free(p); + + assert_true(leak_check_teardown()); +} + +void test_path_unescape(void **state) +{ + char *p; + TALLOC_CTX *mem_ctx; + + assert_true(leak_check_setup()); + mem_ctx = talloc_new(global_talloc_context); + + p = ifp_bus_path_unescape(mem_ctx, "noescape"); + assert_non_null(p); + assert_string_equal(p, "noescape"); + talloc_free(p); + + p = ifp_bus_path_unescape(mem_ctx, "redhat_2ecom"); + assert_non_null(p); + assert_string_equal(p, "redhat.com"); + talloc_free(p); + + p = ifp_bus_path_unescape(mem_ctx, "wrongpath_"); + assert_null(p); + + assert_true(leak_check_teardown()); +} + +#define PATH_BASE "/some/path" + +struct ifp_test_req_ctx { + struct ifp_req *ireq; + struct sbus_request *sr; + struct ifp_ctx *ifp_ctx; +}; + +void ifp_test_req_setup(void **state) +{ + struct ifp_test_req_ctx *test_ctx; + errno_t ret; + + assert_true(leak_check_setup()); + + test_ctx = talloc_zero(global_talloc_context, struct ifp_test_req_ctx); + assert_non_null(test_ctx); + test_ctx->ifp_ctx = mock_ifp_ctx(test_ctx); + assert_non_null(test_ctx->ifp_ctx); + + test_ctx->sr = mock_sbus_request(test_ctx, geteuid()); + assert_non_null(test_ctx->sr); + + ret = ifp_req_create(test_ctx->sr, test_ctx->ifp_ctx, &test_ctx->ireq); + assert_int_equal(ret, EOK); + assert_non_null(test_ctx->ireq); + + check_leaks_push(test_ctx); + *state = test_ctx; +} + +void ifp_test_req_teardown(void **state) +{ + struct ifp_test_req_ctx *test_ctx = talloc_get_type_abort(*state, + struct ifp_test_req_ctx); + + assert_true(check_leaks_pop(test_ctx) == true); + + dbus_message_unref(test_ctx->sr->message); + talloc_free(test_ctx); + + assert_true(leak_check_teardown()); +} + int main(int argc, const char *argv[]) { poptContext pc; @@ -322,6 +416,8 @@ int main(int argc, const char *argv[]) unit_test(test_el_to_dict), unit_test(test_attr_acl), unit_test(test_attr_allowed), + unit_test(test_path_escape), + unit_test(test_path_unescape), }; /* Set debug level to invalid value so we can deside if -d 0 was used. */ -- 1.9.0