>From 0fe89c8b9412579873674ea6e62b2733ea130abf Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Sun, 6 Jan 2013 16:53:20 +0100 Subject: [PATCH 2/2] PAM: reuse existing code for reading selinux config --- src/responder/pam/pamsrv_selinux.c | 80 +++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 45 deletions(-) diff --git a/src/responder/pam/pamsrv_selinux.c b/src/responder/pam/pamsrv_selinux.c index 7d89c3f786ac7bf5834c00df3a6c768cc54f452a..d98aa6ad710fc405f74fc0675426062ebafcdafe 100644 --- a/src/responder/pam/pamsrv_selinux.c +++ b/src/responder/pam/pamsrv_selinux.c @@ -157,6 +157,38 @@ done: return ret; } +static errno_t +get_default_order(TALLOC_CTX *mem_ctx, struct ldb_message *config, + char ***_order_array, size_t *_order_count) +{ + const char *tmp_str; + char **order_array; + int order_count; + errno_t ret; + + tmp_str = ldb_msg_find_attr_as_string(config, + SYSDB_SELINUX_DEFAULT_ORDER, + NULL); + if (tmp_str == NULL) { + DEBUG(SSSDBG_OP_FAILURE, ("No map order given!\n")); + return EINVAL; + } + + /* The "order" string contains one or more SELinux user records + * separated by $. + */ + ret = split_on_separator(mem_ctx, tmp_str, '$', true, true, + &order_array, &order_count); + if (ret != EOK) { + DEBUG(SSSDBG_OP_FAILURE, ("Cannot split the order string [%s]\n", tmp_str)); + return EINVAL; + } + + *_order_array = order_array; + *_order_count = order_count; + return EOK; +} + errno_t process_selinux_mappings(struct pam_auth_req *preq) { @@ -168,12 +200,10 @@ process_selinux_mappings(struct pam_auth_req *preq) struct ldb_message *config; const char *default_user = NULL; const char *tmp_str; - char *order = NULL; char **order_array; + size_t order_count; errno_t ret, err; int i, j; - size_t order_count; - size_t len = 0; tmp_ctx = talloc_new(NULL); if (tmp_ctx == NULL) { @@ -209,51 +239,11 @@ process_selinux_mappings(struct pam_auth_req *preq) goto done; } - tmp_str = ldb_msg_find_attr_as_string(config, - SYSDB_SELINUX_DEFAULT_ORDER, - NULL); - if (tmp_str == NULL) { - DEBUG(SSSDBG_OP_FAILURE, ("No map order given!\n")); - ret = EINVAL; + ret = get_default_order(tmp_ctx, config, &order_array, &order_count); + if (ret != EOK) { goto done; } - order = talloc_strdup(tmp_ctx, tmp_str); - if (order == NULL) { - ret = ENOMEM; - goto done; - } - len = strlen(order); - - /* The "order" string contains one or more SELinux user records - * separated by $. Now we need to create an array of string from - * this one string. First find out how many elements in the array - * will be. This way only one alloc will be necessary for the array - */ - order_count = 1; - for (i = 0; i < len; i++) { - if (order[i] == '$') order_count++; - } - - order_array = talloc_array(tmp_ctx, char *, order_count); - if (order_array == NULL) { - ret = ENOMEM; - goto done; - } - - /* Now fill the array with pointers to the original string. Also - * use binary zeros to make multiple string out of the one. - */ - order_array[0] = order; - order_count = 1; - for (i = 0; i < len; i++) { - if (order[i] == '$') { - order[i] = '\0'; - order_array[order_count] = &order[i+1]; - order_count++; - } - } - /* Fetch all maps applicable to the user who is currently logging in */ ret = sysdb_search_selinux_usermap_by_username(tmp_ctx, sysdb, pd->user, &usermaps); -- 1.8.0.2