From 24eac34a8c1f0a284cb697e8d5c09ff049181691 Mon Sep 17 00:00:00 2001 From: Pavel Březina Date: Dec 15 2015 15:26:51 +0000 Subject: SUDO: fix tevent style Rearrage and rename functions in sdap_async_sudo.c to obey tevent style and improve readability. Reviewed-by: Jakub Hrozek Reviewed-by: Lukáš Slebodník --- diff --git a/src/providers/ldap/sdap_async_sudo.c b/src/providers/ldap/sdap_async_sudo.c index 5ee0d6f..4879173 100644 --- a/src/providers/ldap/sdap_async_sudo.c +++ b/src/providers/ldap/sdap_async_sudo.c @@ -34,21 +34,6 @@ #include "providers/ldap/sdap_sudo_cache.h" #include "db/sysdb_sudo.h" -struct sdap_sudo_refresh_state { - struct be_ctx *be_ctx; - struct sdap_options *opts; - struct sdap_id_op *sdap_op; - struct sysdb_ctx *sysdb; - struct sss_domain_info *domain; - - const char *ldap_filter; /* search */ - const char *sysdb_filter; /* delete */ - - int dp_error; - char *highest_usn; - size_t num_rules; -}; - struct sdap_sudo_load_sudoers_state { struct tevent_context *ev; struct sdap_options *opts; @@ -63,204 +48,22 @@ struct sdap_sudo_load_sudoers_state { int timeout; }; -static int sdap_sudo_refresh_retry(struct tevent_req *req); - -static void sdap_sudo_refresh_connect_done(struct tevent_req *subreq); - -static struct tevent_req * sdap_sudo_load_sudoers_send(TALLOC_CTX *mem_ctx, - struct tevent_context *ev, - struct sdap_options *opts, - struct sdap_handle *sh, - const char *ldap_filter); - static errno_t sdap_sudo_load_sudoers_next_base(struct tevent_req *req); - -static void sdap_sudo_load_sudoers_process(struct tevent_req *subreq); - -static int sdap_sudo_load_sudoers_recv(struct tevent_req *req, - TALLOC_CTX *mem_ctx, - size_t *rules_count, - struct sysdb_attrs ***rules); - -static void sdap_sudo_refresh_load_done(struct tevent_req *subreq); - -static int sdap_sudo_purge_sudoers(struct sss_domain_info *dom, - const char *filter, - struct sdap_attr_map *map, - size_t rules_count, - struct sysdb_attrs **rules); - -static int sdap_sudo_store_sudoers(TALLOC_CTX *mem_ctx, - struct sss_domain_info *domain, - struct sdap_options *opts, - size_t rules_count, - struct sysdb_attrs **rules, - int cache_timeout, - time_t now, - char **_usn); - -struct tevent_req *sdap_sudo_refresh_send(TALLOC_CTX *mem_ctx, - struct be_ctx *be_ctx, - struct sdap_options *opts, - struct sdap_id_conn_cache *conn_cache, - const char *ldap_filter, - const char *sysdb_filter) -{ - struct tevent_req *req; - struct sdap_sudo_refresh_state *state; - int ret; - - req = tevent_req_create(mem_ctx, &state, struct sdap_sudo_refresh_state); - if (!req) { - return NULL; - } - - /* if we don't have a search filter, this request is meaningless */ - if (ldap_filter == NULL) { - ret = EINVAL; - goto immediately; - } - - state->be_ctx = be_ctx; - state->opts = opts; - state->sdap_op = sdap_id_op_create(state, conn_cache); - state->sysdb = be_ctx->domain->sysdb; - state->domain = be_ctx->domain; - state->ldap_filter = talloc_strdup(state, ldap_filter); - state->sysdb_filter = talloc_strdup(state, sysdb_filter); - state->dp_error = DP_ERR_FATAL; - state->highest_usn = NULL; - - if (!state->sdap_op) { - DEBUG(SSSDBG_OP_FAILURE, "sdap_id_op_create() failed\n"); - ret = ENOMEM; - goto immediately; - } - - if (state->ldap_filter == NULL) { - ret = ENOMEM; - goto immediately; - } - - if (sysdb_filter != NULL && state->sysdb_filter == NULL) { - ret = ENOMEM; - goto immediately; - } - - ret = sdap_sudo_refresh_retry(req); - if (ret == EAGAIN) { - /* asynchronous processing */ - return req; - } - -immediately: - if (ret == EOK) { - tevent_req_done(req); - } else { - tevent_req_error(req, ret); - } - tevent_req_post(req, be_ctx->ev); - - return req; -} - -int sdap_sudo_refresh_recv(TALLOC_CTX *mem_ctx, - struct tevent_req *req, - int *dp_error, - char **usn, - size_t *num_rules) -{ - struct sdap_sudo_refresh_state *state; - - state = tevent_req_data(req, struct sdap_sudo_refresh_state); - - TEVENT_REQ_RETURN_ON_ERROR(req); - - *dp_error = state->dp_error; - - if (usn != NULL && state->highest_usn != NULL) { - *usn = talloc_steal(mem_ctx, state->highest_usn); - } - - if (num_rules != NULL) { - *num_rules = state->num_rules; - } - - return EOK; -} - -static int sdap_sudo_refresh_retry(struct tevent_req *req) -{ - struct sdap_sudo_refresh_state *state; - struct tevent_req *subreq; - int ret; - - state = tevent_req_data(req, struct sdap_sudo_refresh_state); - - subreq = sdap_id_op_connect_send(state->sdap_op, state, &ret); - if (subreq == NULL) { - DEBUG(SSSDBG_CRIT_FAILURE, "sdap_id_op_connect_send() failed: " - "%d(%s)\n", ret, strerror(ret)); - return ret; - } - - tevent_req_set_callback(subreq, sdap_sudo_refresh_connect_done, req); - - return EAGAIN; -} - -static void sdap_sudo_refresh_connect_done(struct tevent_req *subreq) -{ - struct tevent_req *req; /* req from sdap_sudo_refresh_send() */ - struct sdap_sudo_refresh_state *state; - int dp_error; - int ret; - - req = tevent_req_callback_data(subreq, struct tevent_req); - state = tevent_req_data(req, struct sdap_sudo_refresh_state); - - ret = sdap_id_op_connect_recv(subreq, &dp_error); - talloc_zfree(subreq); - - if (ret != EOK) { - DEBUG(SSSDBG_CRIT_FAILURE, "SUDO LDAP connection failed " - "[%d]: %s\n", ret, strerror(ret)); - state->dp_error = dp_error; - tevent_req_error(req, ret); - return; - } - - DEBUG(SSSDBG_TRACE_FUNC, "SUDO LDAP connection successful\n"); - - subreq = sdap_sudo_load_sudoers_send(state, state->be_ctx->ev, - state->opts, - sdap_id_op_handle(state->sdap_op), - state->ldap_filter); - if (subreq == NULL) { - state->dp_error = DP_ERR_FATAL; - tevent_req_error(req, ENOMEM); - return; - } - - tevent_req_set_callback(subreq, sdap_sudo_refresh_load_done, req); - - return; -} - -static struct tevent_req * sdap_sudo_load_sudoers_send(TALLOC_CTX *mem_ctx, - struct tevent_context *ev, - struct sdap_options *opts, - struct sdap_handle *sh, - const char *ldap_filter) - - - +static void sdap_sudo_load_sudoers_done(struct tevent_req *subreq); + +static struct tevent_req * +sdap_sudo_load_sudoers_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct sdap_options *opts, + struct sdap_handle *sh, + const char *ldap_filter) { struct tevent_req *req; struct sdap_sudo_load_sudoers_state *state; int ret; - req = tevent_req_create(mem_ctx, &state, struct sdap_sudo_load_sudoers_state); + req = tevent_req_create(mem_ctx, &state, + struct sdap_sudo_load_sudoers_state); if (!req) { return NULL; } @@ -348,12 +151,12 @@ static errno_t sdap_sudo_load_sudoers_next_base(struct tevent_req *req) return ENOMEM; } - tevent_req_set_callback(subreq, sdap_sudo_load_sudoers_process, req); + tevent_req_set_callback(subreq, sdap_sudo_load_sudoers_done, req); return EOK; } -static void sdap_sudo_load_sudoers_process(struct tevent_req *subreq) +static void sdap_sudo_load_sudoers_done(struct tevent_req *subreq) { struct tevent_req *req; struct sdap_sudo_load_sudoers_state *state; @@ -428,9 +231,234 @@ static int sdap_sudo_load_sudoers_recv(struct tevent_req *req, return EOK; } -static void sdap_sudo_refresh_load_done(struct tevent_req *subreq) +static int sdap_sudo_purge_sudoers(struct sss_domain_info *dom, + const char *filter, + struct sdap_attr_map *map, + size_t rules_count, + struct sysdb_attrs **rules) +{ + const char *name; + int i; + errno_t ret; + + if (filter == NULL) { + /* removes downloaded rules from the cache */ + if (rules_count == 0 || rules == NULL) { + return EOK; + } + + for (i = 0; i < rules_count; i++) { + ret = sysdb_attrs_get_string(rules[i], + map[SDAP_AT_SUDO_NAME].sys_name, + &name); + if (ret != EOK) { + DEBUG(SSSDBG_MINOR_FAILURE, + "Failed to retrieve rule name: [%s]\n", strerror(ret)); + continue; + } + + ret = sysdb_sudo_purge_byname(dom, name); + if (ret != EOK) { + DEBUG(SSSDBG_MINOR_FAILURE, + "Failed to delete rule %s: [%s]\n", + name, strerror(ret)); + continue; + } + } + + ret = EOK; + } else { + /* purge cache by provided filter */ + ret = sysdb_sudo_purge_byfilter(dom, filter); + if (ret != EOK) { + goto done; + } + } + +done: + if (ret != EOK) { + DEBUG(SSSDBG_OP_FAILURE, "failed to purge sudo rules [%d]: %s\n", + ret, strerror(ret)); + } + + return ret; +} + +static int sdap_sudo_store_sudoers(TALLOC_CTX *mem_ctx, + struct sss_domain_info *domain, + struct sdap_options *opts, + size_t rules_count, + struct sysdb_attrs **rules, + int cache_timeout, + time_t now, + char **_usn) +{ + errno_t ret; + + /* Empty sudoers? Done. */ + if (rules_count == 0 || rules == NULL) { + return EOK; + } + + ret = sdap_save_native_sudorule_list(mem_ctx, domain, + opts->sudorule_map, rules, + rules_count, cache_timeout, now, + _usn); + if (ret != EOK) { + DEBUG(SSSDBG_OP_FAILURE, "failed to save sudo rules [%d]: %s\n", + ret, strerror(ret)); + return ret; + } + + return EOK; +} + +struct sdap_sudo_refresh_state { + struct tevent_context *ev; + struct sdap_options *opts; + struct sdap_id_op *sdap_op; + struct sysdb_ctx *sysdb; + struct sss_domain_info *domain; + + const char *ldap_filter; /* search */ + const char *sysdb_filter; /* delete */ + + int dp_error; + char *highest_usn; + size_t num_rules; +}; + +static errno_t sdap_sudo_refresh_retry(struct tevent_req *req); +static void sdap_sudo_refresh_connect_done(struct tevent_req *subreq); +static void sdap_sudo_refresh_done(struct tevent_req *subreq); + +struct tevent_req *sdap_sudo_refresh_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct sss_domain_info *domain, + struct sdap_options *opts, + struct sdap_id_conn_ctx *conn, + const char *ldap_filter, + const char *sysdb_filter) +{ + struct tevent_req *req; + struct sdap_sudo_refresh_state *state; + int ret; + + req = tevent_req_create(mem_ctx, &state, struct sdap_sudo_refresh_state); + if (!req) { + return NULL; + } + + /* if we don't have a search filter, this request is meaningless */ + if (ldap_filter == NULL) { + ret = EINVAL; + goto immediately; + } + + state->ev = ev; + state->opts = opts; + state->domain = domain; + state->sysdb = domain->sysdb; + state->dp_error = DP_ERR_FATAL; + state->highest_usn = NULL; + + state->sdap_op = sdap_id_op_create(state, conn->conn_cache); + if (!state->sdap_op) { + DEBUG(SSSDBG_OP_FAILURE, "sdap_id_op_create() failed\n"); + ret = ENOMEM; + goto immediately; + } + + state->ldap_filter = talloc_strdup(state, ldap_filter); + if (state->ldap_filter == NULL) { + ret = ENOMEM; + goto immediately; + } + + state->sysdb_filter = talloc_strdup(state, sysdb_filter); + if (sysdb_filter != NULL && state->sysdb_filter == NULL) { + ret = ENOMEM; + goto immediately; + } + + ret = sdap_sudo_refresh_retry(req); + if (ret == EAGAIN) { + /* asynchronous processing */ + return req; + } + +immediately: + if (ret == EOK) { + tevent_req_done(req); + } else { + tevent_req_error(req, ret); + } + tevent_req_post(req, ev); + + return req; +} + +static errno_t sdap_sudo_refresh_retry(struct tevent_req *req) { - struct tevent_req *req; /* req from sdap_sudo_refresh_send() */ + struct sdap_sudo_refresh_state *state; + struct tevent_req *subreq; + int ret; + + state = tevent_req_data(req, struct sdap_sudo_refresh_state); + + subreq = sdap_id_op_connect_send(state->sdap_op, state, &ret); + if (subreq == NULL) { + DEBUG(SSSDBG_CRIT_FAILURE, "sdap_id_op_connect_send() failed: " + "%d(%s)\n", ret, strerror(ret)); + return ret; + } + + tevent_req_set_callback(subreq, sdap_sudo_refresh_connect_done, req); + + return EAGAIN; +} + +static void sdap_sudo_refresh_connect_done(struct tevent_req *subreq) +{ + struct tevent_req *req; + struct sdap_sudo_refresh_state *state; + int dp_error; + int ret; + + req = tevent_req_callback_data(subreq, struct tevent_req); + state = tevent_req_data(req, struct sdap_sudo_refresh_state); + + ret = sdap_id_op_connect_recv(subreq, &dp_error); + talloc_zfree(subreq); + + if (ret != EOK) { + DEBUG(SSSDBG_CRIT_FAILURE, "SUDO LDAP connection failed " + "[%d]: %s\n", ret, strerror(ret)); + state->dp_error = dp_error; + tevent_req_error(req, ret); + return; + } + + DEBUG(SSSDBG_TRACE_FUNC, "SUDO LDAP connection successful\n"); + + subreq = sdap_sudo_load_sudoers_send(state, state->ev, + state->opts, + sdap_id_op_handle(state->sdap_op), + state->ldap_filter); + if (subreq == NULL) { + state->dp_error = DP_ERR_FATAL; + tevent_req_error(req, ENOMEM); + return; + } + + tevent_req_set_callback(subreq, sdap_sudo_refresh_done, req); + + return; +} + +static void sdap_sudo_refresh_done(struct tevent_req *subreq) +{ + struct tevent_req *req; struct sdap_sudo_refresh_state *state; struct sysdb_attrs **rules = NULL; size_t rules_count = 0; @@ -512,83 +540,26 @@ done: } } -static int sdap_sudo_purge_sudoers(struct sss_domain_info *dom, - const char *filter, - struct sdap_attr_map *map, - size_t rules_count, - struct sysdb_attrs **rules) +int sdap_sudo_refresh_recv(TALLOC_CTX *mem_ctx, + struct tevent_req *req, + int *dp_error, + char **usn, + size_t *num_rules) { - const char *name; - int i; - errno_t ret; - - if (filter == NULL) { - /* removes downloaded rules from the cache */ - if (rules_count == 0 || rules == NULL) { - return EOK; - } - - for (i = 0; i < rules_count; i++) { - ret = sysdb_attrs_get_string(rules[i], - map[SDAP_AT_SUDO_NAME].sys_name, - &name); - if (ret != EOK) { - DEBUG(SSSDBG_MINOR_FAILURE, - "Failed to retrieve rule name: [%s]\n", strerror(ret)); - continue; - } - - ret = sysdb_sudo_purge_byname(dom, name); - if (ret != EOK) { - DEBUG(SSSDBG_MINOR_FAILURE, - "Failed to delete rule %s: [%s]\n", - name, strerror(ret)); - continue; - } - } - - ret = EOK; - } else { - /* purge cache by provided filter */ - ret = sysdb_sudo_purge_byfilter(dom, filter); - if (ret != EOK) { - goto done; - } - } + struct sdap_sudo_refresh_state *state; -done: - if (ret != EOK) { - DEBUG(SSSDBG_OP_FAILURE, "failed to purge sudo rules [%d]: %s\n", - ret, strerror(ret)); - } + state = tevent_req_data(req, struct sdap_sudo_refresh_state); - return ret; -} + TEVENT_REQ_RETURN_ON_ERROR(req); -static int sdap_sudo_store_sudoers(TALLOC_CTX *mem_ctx, - struct sss_domain_info *domain, - struct sdap_options *opts, - size_t rules_count, - struct sysdb_attrs **rules, - int cache_timeout, - time_t now, - char **_usn) -{ - errno_t ret; + *dp_error = state->dp_error; - /* Empty sudoers? Done. */ - if (rules_count == 0 || rules == NULL) { - return EOK; + if (usn != NULL && state->highest_usn != NULL) { + *usn = talloc_steal(mem_ctx, state->highest_usn); } - ret = sdap_save_native_sudorule_list(mem_ctx, domain, - opts->sudorule_map, rules, - rules_count, cache_timeout, now, - _usn); - if (ret != EOK) { - DEBUG(SSSDBG_OP_FAILURE, "failed to save sudo rules [%d]: %s\n", - ret, strerror(ret)); - return ret; + if (num_rules != NULL) { + *num_rules = state->num_rules; } return EOK; diff --git a/src/providers/ldap/sdap_sudo.c b/src/providers/ldap/sdap_sudo.c index e759878..97c71b7 100644 --- a/src/providers/ldap/sdap_sudo.c +++ b/src/providers/ldap/sdap_sudo.c @@ -170,7 +170,6 @@ void sdap_sudo_handler(struct be_req *be_req) struct tevent_req *req = NULL; struct be_sudo_req *sudo_req = NULL; struct sdap_sudo_ctx *sudo_ctx = NULL; - struct sdap_id_ctx *id_ctx = NULL; int ret = EOK; if (be_is_offline(be_ctx)) { @@ -180,7 +179,6 @@ void sdap_sudo_handler(struct be_req *be_req) sudo_ctx = talloc_get_type(be_ctx->bet_info[BET_SUDO].pvt_bet_data, struct sdap_sudo_ctx); - id_ctx = sudo_ctx->id_ctx; sudo_req = talloc_get_type(be_req_get_data(be_req), struct be_sudo_req); @@ -191,9 +189,7 @@ void sdap_sudo_handler(struct be_req *be_req) break; case BE_REQ_SUDO_RULES: DEBUG(SSSDBG_TRACE_FUNC, "Issuing a refresh of specific sudo rules\n"); - req = sdap_sudo_rules_refresh_send(be_req, sudo_ctx, id_ctx->be, - id_ctx->opts, id_ctx->conn->conn_cache, - sudo_req->rules); + req = sdap_sudo_rules_refresh_send(be_req, sudo_ctx, sudo_req->rules); break; default: DEBUG(SSSDBG_CRIT_FAILURE, "Invalid request type: %d\n", diff --git a/src/providers/ldap/sdap_sudo.h b/src/providers/ldap/sdap_sudo.h index 38e1b88..7b47a29 100644 --- a/src/providers/ldap/sdap_sudo.h +++ b/src/providers/ldap/sdap_sudo.h @@ -46,9 +46,10 @@ int sdap_sudo_init(struct be_ctx *be_ctx, /* sdap async interface */ struct tevent_req *sdap_sudo_refresh_send(TALLOC_CTX *mem_ctx, - struct be_ctx *be_ctx, + struct tevent_context *ev, + struct sss_domain_info *domain, struct sdap_options *opts, - struct sdap_id_conn_cache *conn_cache, + struct sdap_id_conn_ctx *conn, const char *ldap_filter, const char *sysdb_filter); @@ -72,9 +73,6 @@ int sdap_sudo_smart_refresh_recv(struct tevent_req *req, struct tevent_req *sdap_sudo_rules_refresh_send(TALLOC_CTX *mem_ctx, struct sdap_sudo_ctx *sudo_ctx, - struct be_ctx *be_ctx, - struct sdap_options *opts, - struct sdap_id_conn_cache *conn_cache, char **rules); int sdap_sudo_rules_refresh_recv(struct tevent_req *req, diff --git a/src/providers/ldap/sdap_sudo_refresh.c b/src/providers/ldap/sdap_sudo_refresh.c index 6d87bee..fc703b4 100644 --- a/src/providers/ldap/sdap_sudo_refresh.c +++ b/src/providers/ldap/sdap_sudo_refresh.c @@ -249,8 +249,8 @@ struct tevent_req *sdap_sudo_full_refresh_send(TALLOC_CTX *mem_ctx, DEBUG(SSSDBG_TRACE_FUNC, "Issuing a full refresh of sudo rules\n"); - subreq = sdap_sudo_refresh_send(state, id_ctx->be, id_ctx->opts, - id_ctx->conn->conn_cache, + subreq = sdap_sudo_refresh_send(state, id_ctx->be->ev, id_ctx->be->domain, + id_ctx->opts, id_ctx->conn, ldap_full_filter, sysdb_filter); if (subreq == NULL) { ret = ENOMEM; @@ -407,8 +407,8 @@ struct tevent_req *sdap_sudo_smart_refresh_send(TALLOC_CTX *mem_ctx, DEBUG(SSSDBG_TRACE_FUNC, "Issuing a smart refresh of sudo rules " "(USN > %s)\n", (usn == NULL ? "0" : usn)); - subreq = sdap_sudo_refresh_send(state, id_ctx->be, id_ctx->opts, - id_ctx->conn->conn_cache, + subreq = sdap_sudo_refresh_send(state, id_ctx->be->ev, id_ctx->be->domain, + id_ctx->opts, id_ctx->conn, ldap_full_filter, NULL); if (subreq == NULL) { ret = ENOMEM; @@ -491,14 +491,13 @@ static void sdap_sudo_rules_refresh_done(struct tevent_req *subreq); struct tevent_req *sdap_sudo_rules_refresh_send(TALLOC_CTX *mem_ctx, struct sdap_sudo_ctx *sudo_ctx, - struct be_ctx *be_ctx, - struct sdap_options *opts, - struct sdap_id_conn_cache *conn_cache, char **rules) { struct tevent_req *req = NULL; struct tevent_req *subreq = NULL; struct sdap_sudo_rules_refresh_state *state = NULL; + struct sdap_id_ctx *id_ctx = sudo_ctx->id_ctx; + struct sdap_options *opts = id_ctx->opts; TALLOC_CTX *tmp_ctx = NULL; char *ldap_filter = NULL; char *ldap_full_filter = NULL; @@ -578,7 +577,8 @@ struct tevent_req *sdap_sudo_rules_refresh_send(TALLOC_CTX *mem_ctx, goto immediately; } - subreq = sdap_sudo_refresh_send(req, be_ctx, opts, conn_cache, + subreq = sdap_sudo_refresh_send(req, id_ctx->be->ev, id_ctx->be->domain, + opts, id_ctx->conn, ldap_full_filter, sysdb_filter); if (subreq == NULL) { ret = ENOMEM; @@ -593,7 +593,7 @@ immediately: if (ret != EOK) { tevent_req_error(req, ret); - tevent_req_post(req, be_ctx->ev); + tevent_req_post(req, id_ctx->be->ev); } return req;