sdap_sudo_refresh.c revision 3b99f7a97553a0a357d50abe507d4f0060c4ecea
967e5f3c25249c779575864692935627004d3f9eChristian Maeder/*
967e5f3c25249c779575864692935627004d3f9eChristian Maeder Authors:
81d182b21020b815887e9057959228546cf61b6bChristian Maeder Pavel Březina <pbrezina@redhat.com>
f11f713bebd8e1e623a0a4361065df256033de47Christian Maeder
97018cf5fa25b494adffd7e9b4e87320dae6bf47Christian Maeder Copyright (C) 2015 Red Hat
967e5f3c25249c779575864692935627004d3f9eChristian Maeder
3f69b6948966979163bdfe8331c38833d5d90ecdChristian Maeder This program is free software; you can redistribute it and/or modify
967e5f3c25249c779575864692935627004d3f9eChristian Maeder it under the terms of the GNU General Public License as published by
89054b2b95a3f92e78324dc852f3d34704e2ca49Christian Maeder the Free Software Foundation; either version 3 of the License, or
f3a94a197960e548ecd6520bb768cb0d547457bbChristian Maeder (at your option) any later version.
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder This program is distributed in the hope that it will be useful,
967e5f3c25249c779575864692935627004d3f9eChristian Maeder but WITHOUT ANY WARRANTY; without even the implied warranty of
967e5f3c25249c779575864692935627004d3f9eChristian Maeder MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
967e5f3c25249c779575864692935627004d3f9eChristian Maeder GNU General Public License for more details.
967e5f3c25249c779575864692935627004d3f9eChristian Maeder
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maeder You should have received a copy of the GNU General Public License
7221c71b38c871ce66eee4537cb681d468308dfbChristian Maeder along with this program. If not, see <http://www.gnu.org/licenses/>.
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder*/
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder#include <errno.h>
ac19f8695aa1b2d2d1cd1319da2530edd8f46a96Christian Maeder#include <talloc.h>
8b9fda012e5ee53b7b2320c0638896a0ff6e99f3Christian Maeder#include <tevent.h>
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder#include "util/util.h"
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian Maeder#include "providers/be_ptask.h"
4ef2a978e66e2246ff0b7f00c77deb7aabb28b8eChristian Maeder#include "providers/ldap/sdap_sudo.h"
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder#include "providers/ldap/sdap_sudo_shared.h"
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maeder#include "db/sysdb_sudo.h"
967e5f3c25249c779575864692935627004d3f9eChristian Maeder
8e9c3881fb6e710b1e08bf5ac8ff9d393df2e74eChristian Maederstruct sdap_sudo_full_refresh_state {
78eeae099616e255ccf2e5f9122387bb10c68338Christian Maeder struct sdap_sudo_ctx *sudo_ctx;
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder struct sdap_id_ctx *id_ctx;
ad270004874ce1d0697fb30d7309f180553bb315Christian Maeder struct sysdb_ctx *sysdb;
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maeder struct sss_domain_info *domain;
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder int dp_error;
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder};
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maederstatic void sdap_sudo_full_refresh_done(struct tevent_req *subreq);
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maederstruct tevent_req *sdap_sudo_full_refresh_send(TALLOC_CTX *mem_ctx,
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder struct sdap_sudo_ctx *sudo_ctx)
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder{
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder struct tevent_req *req = NULL;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder struct tevent_req *subreq = NULL;
51fb5d7edd9369c367dda2f8b15ddd6f8a146606Christian Maeder struct sdap_id_ctx *id_ctx = sudo_ctx->id_ctx;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder struct sdap_sudo_full_refresh_state *state = NULL;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder char *search_filter = NULL;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder char *delete_filter = NULL;
51fb5d7edd9369c367dda2f8b15ddd6f8a146606Christian Maeder int ret;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder req = tevent_req_create(mem_ctx, &state, struct sdap_sudo_full_refresh_state);
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder if (req == NULL) {
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder DEBUG(SSSDBG_CRIT_FAILURE, "tevent_req_create() failed\n");
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder return NULL;
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder }
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder state->sudo_ctx = sudo_ctx;
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder state->id_ctx = id_ctx;
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder state->sysdb = id_ctx->be->domain->sysdb;
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder state->domain = id_ctx->be->domain;
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder
7a879b08ae0ca30006f9be887a73212b07f10204Christian Maeder /* Download all rules from LDAP */
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder search_filter = talloc_asprintf(state, SDAP_SUDO_FILTER_CLASS,
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder id_ctx->opts->sudorule_map[SDAP_OC_SUDORULE].name);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder if (search_filter == NULL) {
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder ret = ENOMEM;
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder goto immediately;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder }
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder /* Remove all rules from cache */
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder delete_filter = talloc_asprintf(state, "(%s=%s)",
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder SYSDB_OBJECTCLASS, SYSDB_SUDO_CACHE_OC);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder if (delete_filter == NULL) {
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder ret = ENOMEM;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder goto immediately;
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder }
7a879b08ae0ca30006f9be887a73212b07f10204Christian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder DEBUG(SSSDBG_TRACE_FUNC, "Issuing a full refresh of sudo rules\n");
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder subreq = sdap_sudo_refresh_send(state, sudo_ctx, search_filter,
a89389521ddf76109168a0b339031575aafbd512Christian Maeder delete_filter);
a89389521ddf76109168a0b339031575aafbd512Christian Maeder if (subreq == NULL) {
a89389521ddf76109168a0b339031575aafbd512Christian Maeder ret = ENOMEM;
a89389521ddf76109168a0b339031575aafbd512Christian Maeder goto immediately;
a89389521ddf76109168a0b339031575aafbd512Christian Maeder }
a89389521ddf76109168a0b339031575aafbd512Christian Maeder
a89389521ddf76109168a0b339031575aafbd512Christian Maeder tevent_req_set_callback(subreq, sdap_sudo_full_refresh_done, req);
a89389521ddf76109168a0b339031575aafbd512Christian Maeder
a89389521ddf76109168a0b339031575aafbd512Christian Maeder return req;
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maeder
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maederimmediately:
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder if (ret == EOK) {
9b30898b139ee02f97ac933b6d935ef0a4206921Christian Maeder tevent_req_done(req);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder } else {
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder tevent_req_error(req, ret);
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder }
51fb5d7edd9369c367dda2f8b15ddd6f8a146606Christian Maeder tevent_req_post(req, id_ctx->be->ev);
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian Maeder
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder return req;
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder}
51fb5d7edd9369c367dda2f8b15ddd6f8a146606Christian Maeder
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maederstatic void sdap_sudo_full_refresh_done(struct tevent_req *subreq)
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder{
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder struct tevent_req *req = NULL;
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder struct sdap_sudo_full_refresh_state *state = NULL;
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder int ret;
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder
51fb5d7edd9369c367dda2f8b15ddd6f8a146606Christian Maeder req = tevent_req_callback_data(subreq, struct tevent_req);
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian Maeder state = tevent_req_data(req, struct sdap_sudo_full_refresh_state);
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder
67086e0fe40a985c5e8a3cf50e611f43234580c2Christian Maeder ret = sdap_sudo_refresh_recv(state, subreq, &state->dp_error, NULL);
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder talloc_zfree(subreq);
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder if (ret != EOK || state->dp_error != DP_ERR_OK) {
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder goto done;
67086e0fe40a985c5e8a3cf50e611f43234580c2Christian Maeder }
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder /* save the time in the sysdb */
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder ret = sysdb_sudo_set_last_full_refresh(state->domain, time(NULL));
67086e0fe40a985c5e8a3cf50e611f43234580c2Christian Maeder if (ret != EOK) {
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder DEBUG(SSSDBG_MINOR_FAILURE, "Unable to save time of "
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder "a successful full refresh\n");
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder /* this is only a minor error that does not affect the functionality,
d48085f765fca838c1d972d2123601997174583dChristian Maeder * therefore there is no need to report it with tevent_req_error()
d48085f765fca838c1d972d2123601997174583dChristian Maeder * which would cause problems in the consumers */
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder }
d48085f765fca838c1d972d2123601997174583dChristian Maeder
d48085f765fca838c1d972d2123601997174583dChristian Maeder DEBUG(SSSDBG_TRACE_FUNC, "Successful full refresh of sudo rules\n");
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder
d48085f765fca838c1d972d2123601997174583dChristian Maederdone:
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder if (ret != EOK) {
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder tevent_req_error(req, ret);
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian Maeder return;
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder }
9b30898b139ee02f97ac933b6d935ef0a4206921Christian Maeder
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder tevent_req_done(req);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder}
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maederint sdap_sudo_full_refresh_recv(struct tevent_req *req,
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder int *dp_error)
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder{
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder struct sdap_sudo_full_refresh_state *state = NULL;
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder state = tevent_req_data(req, struct sdap_sudo_full_refresh_state);
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder TEVENT_REQ_RETURN_ON_ERROR(req);
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder *dp_error = state->dp_error;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder return EOK;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder}
f11f713bebd8e1e623a0a4361065df256033de47Christian Maeder
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maederstruct sdap_sudo_smart_refresh_state {
967e5f3c25249c779575864692935627004d3f9eChristian Maeder struct sdap_id_ctx *id_ctx;
18b513ff41708f24e1a7407f36b719add813ffeaChristian Maeder struct sysdb_ctx *sysdb;
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder int dp_error;
a89e661aad28f1b39f4fc9f9f9a4d46074234123Christian Maeder};
a23e572c8f957cc051a1b0831abd6fe9380d45c7Christian Maeder
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maederstatic void sdap_sudo_smart_refresh_done(struct tevent_req *subreq);
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maederstruct tevent_req *sdap_sudo_smart_refresh_send(TALLOC_CTX *mem_ctx,
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder struct sdap_sudo_ctx *sudo_ctx)
f2ee9fc53048ea92bad79e3f5d292d83efd7f8beMihai Codescu{
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maeder struct tevent_req *req = NULL;
81d182b21020b815887e9057959228546cf61b6bChristian Maeder struct tevent_req *subreq = NULL;
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder struct sdap_id_ctx *id_ctx = sudo_ctx->id_ctx;
242397ba0f1cc490e892130bf0df239deeecf5daChristian Maeder struct sdap_attr_map *map = id_ctx->opts->sudorule_map;
2e9985cd67e4f2414becb670ef33b8f16513e41dChristian Maeder struct sdap_server_opts *srv_opts = id_ctx->srv_opts;
242397ba0f1cc490e892130bf0df239deeecf5daChristian Maeder struct sdap_sudo_smart_refresh_state *state = NULL;
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder char *search_filter = NULL;
d769b9ca726a9b50d661847c0e58c41d6ef334b4Christian Maeder const char *usn;
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder int ret;
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder req = tevent_req_create(mem_ctx, &state, struct sdap_sudo_smart_refresh_state);
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder if (req == NULL) {
d769b9ca726a9b50d661847c0e58c41d6ef334b4Christian Maeder DEBUG(SSSDBG_CRIT_FAILURE, "tevent_req_create() failed\n");
d769b9ca726a9b50d661847c0e58c41d6ef334b4Christian Maeder return NULL;
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maeder }
d769b9ca726a9b50d661847c0e58c41d6ef334b4Christian Maeder
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder state->id_ctx = id_ctx;
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder state->sysdb = id_ctx->be->domain->sysdb;
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maeder
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder /* Download all rules from LDAP that are newer than usn */
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder if (srv_opts == NULL || srv_opts->max_sudo_value == 0) {
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder DEBUG(SSSDBG_TRACE_FUNC, "USN value is unknown, assuming zero.\n");
842eedc62639561781b6c33533d1949693ef6cc5Christian Maeder usn = "0";
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder search_filter = talloc_asprintf(state, "(objectclass=%s)",
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder map[SDAP_OC_SUDORULE].name);
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder } else {
842eedc62639561781b6c33533d1949693ef6cc5Christian Maeder usn = srv_opts->max_sudo_value;
bfa9e03532243ceb487f0384d0f6a447f1ce7670Till Mossakowski search_filter = talloc_asprintf(state, "(&(objectclass=%s)(%s>=%s))",
967e5f3c25249c779575864692935627004d3f9eChristian Maeder map[SDAP_OC_SUDORULE].name,
967e5f3c25249c779575864692935627004d3f9eChristian Maeder map[SDAP_AT_SUDO_USN].name, usn);
f2ee9fc53048ea92bad79e3f5d292d83efd7f8beMihai Codescu }
967e5f3c25249c779575864692935627004d3f9eChristian Maeder if (search_filter == NULL) {
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder ret = ENOMEM;
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder goto immediately;
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder }
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder /* Do not remove any rules that are already in the sysdb
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder * sysdb_filter = NULL; */
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder DEBUG(SSSDBG_TRACE_FUNC, "Issuing a smart refresh of sudo rules "
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder "(USN >= %s)\n", usn);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder subreq = sdap_sudo_refresh_send(state, sudo_ctx, search_filter, NULL);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder if (subreq == NULL) {
967e5f3c25249c779575864692935627004d3f9eChristian Maeder ret = ENOMEM;
967e5f3c25249c779575864692935627004d3f9eChristian Maeder goto immediately;
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder }
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder tevent_req_set_callback(subreq, sdap_sudo_smart_refresh_done, req);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder return req;
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maederimmediately:
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder if (ret == EOK) {
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder tevent_req_done(req);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder } else {
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder tevent_req_error(req, ret);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder }
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder tevent_req_post(req, id_ctx->be->ev);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder return req;
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder}
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maederstatic void sdap_sudo_smart_refresh_done(struct tevent_req *subreq)
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder{
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder struct tevent_req *req = NULL;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder struct sdap_sudo_smart_refresh_state *state = NULL;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder int ret;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder req = tevent_req_callback_data(subreq, struct tevent_req);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder state = tevent_req_data(req, struct sdap_sudo_smart_refresh_state);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder ret = sdap_sudo_refresh_recv(state, subreq, &state->dp_error, NULL);
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder talloc_zfree(subreq);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder if (ret != EOK || state->dp_error != DP_ERR_OK) {
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder goto done;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder }
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder DEBUG(SSSDBG_TRACE_FUNC, "Successful smart refresh of sudo rules\n");
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder
ad187062b0009820118c1b773a232e29b879a2faChristian Maederdone:
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder if (ret != EOK) {
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder tevent_req_error(req, ret);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder return;
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder }
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder tevent_req_done(req);
9659c509ce5e78adc51d7b02a76274eddcba9338Christian Maeder}
9659c509ce5e78adc51d7b02a76274eddcba9338Christian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maederint sdap_sudo_smart_refresh_recv(struct tevent_req *req,
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder int *dp_error)
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder{
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder struct sdap_sudo_smart_refresh_state *state = NULL;
9659c509ce5e78adc51d7b02a76274eddcba9338Christian Maeder state = tevent_req_data(req, struct sdap_sudo_smart_refresh_state);
9659c509ce5e78adc51d7b02a76274eddcba9338Christian Maeder
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder TEVENT_REQ_RETURN_ON_ERROR(req);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder *dp_error = state->dp_error;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder return EOK;
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder}
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maederstruct sdap_sudo_rules_refresh_state {
97ee7048e63953c5617342ce38c30cbcb35cc0beChristian Maeder struct sdap_id_ctx *id_ctx;
97ee7048e63953c5617342ce38c30cbcb35cc0beChristian Maeder size_t num_rules;
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder int dp_error;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder bool deleted;
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder};
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maederstatic void sdap_sudo_rules_refresh_done(struct tevent_req *subreq);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maederstruct tevent_req *sdap_sudo_rules_refresh_send(TALLOC_CTX *mem_ctx,
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder struct sdap_sudo_ctx *sudo_ctx,
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder char **rules)
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder{
2dfc7b04f2db681992ca04175f2beb0f127c9844Christian Maeder struct tevent_req *req = NULL;
2dfc7b04f2db681992ca04175f2beb0f127c9844Christian Maeder struct tevent_req *subreq = NULL;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder struct sdap_sudo_rules_refresh_state *state = NULL;
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder struct sdap_id_ctx *id_ctx = sudo_ctx->id_ctx;
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder struct sdap_options *opts = id_ctx->opts;
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder TALLOC_CTX *tmp_ctx = NULL;
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder char *search_filter = NULL;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder char *delete_filter = NULL;
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder char *safe_rule = NULL;
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder int ret;
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder int i;
if (rules == NULL) {
return NULL;
}
tmp_ctx = talloc_new(NULL);
if (tmp_ctx == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n");
return NULL;
}
req = tevent_req_create(mem_ctx, &state, struct sdap_sudo_rules_refresh_state);
if (req == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "tevent_req_create() failed\n");
return NULL;
}
search_filter = talloc_zero(tmp_ctx, char); /* assign to tmp_ctx */
delete_filter = talloc_zero(tmp_ctx, char); /* assign to tmp_ctx */
/* Download only selected rules from LDAP */
/* Remove all selected rules from cache */
for (i = 0; rules[i] != NULL; i++) {
ret = sss_filter_sanitize(tmp_ctx, rules[i], &safe_rule);
if (ret != EOK) {
ret = ENOMEM;
goto immediately;
}
search_filter = talloc_asprintf_append_buffer(search_filter, "(%s=%s)",
opts->sudorule_map[SDAP_AT_SUDO_NAME].name,
safe_rule);
if (search_filter == NULL) {
ret = ENOMEM;
goto immediately;
}
delete_filter = talloc_asprintf_append_buffer(delete_filter, "(%s=%s)",
SYSDB_SUDO_CACHE_AT_CN,
safe_rule);
if (delete_filter == NULL) {
ret = ENOMEM;
goto immediately;
}
}
state->id_ctx = sudo_ctx->id_ctx;
state->num_rules = i;
search_filter = talloc_asprintf(tmp_ctx, "(&"SDAP_SUDO_FILTER_CLASS"(|%s))",
opts->sudorule_map[SDAP_OC_SUDORULE].name,
search_filter);
if (search_filter == NULL) {
ret = ENOMEM;
goto immediately;
}
delete_filter = talloc_asprintf(tmp_ctx, "(&(%s=%s)(|%s))",
SYSDB_OBJECTCLASS, SYSDB_SUDO_CACHE_OC,
delete_filter);
if (delete_filter == NULL) {
ret = ENOMEM;
goto immediately;
}
subreq = sdap_sudo_refresh_send(req, sudo_ctx, search_filter,
delete_filter);
if (subreq == NULL) {
ret = ENOMEM;
goto immediately;
}
tevent_req_set_callback(subreq, sdap_sudo_rules_refresh_done, req);
ret = EOK;
immediately:
talloc_free(tmp_ctx);
if (ret != EOK) {
tevent_req_error(req, ret);
tevent_req_post(req, id_ctx->be->ev);
}
return req;
}
static void sdap_sudo_rules_refresh_done(struct tevent_req *subreq)
{
struct tevent_req *req = NULL;
struct sdap_sudo_rules_refresh_state *state = NULL;
size_t downloaded_rules_num;
int ret;
req = tevent_req_callback_data(subreq, struct tevent_req);
state = tevent_req_data(req, struct sdap_sudo_rules_refresh_state);
ret = sdap_sudo_refresh_recv(state, subreq, &state->dp_error,
&downloaded_rules_num);
talloc_zfree(subreq);
if (ret != EOK || state->dp_error != DP_ERR_OK) {
goto done;
}
state->deleted = downloaded_rules_num != state->num_rules ? true : false;
done:
if (ret != EOK) {
tevent_req_error(req, ret);
return;
}
tevent_req_done(req);
}
int sdap_sudo_rules_refresh_recv(struct tevent_req *req,
int *dp_error,
bool *deleted)
{
struct sdap_sudo_rules_refresh_state *state = NULL;
state = tevent_req_data(req, struct sdap_sudo_rules_refresh_state);
TEVENT_REQ_RETURN_ON_ERROR(req);
*dp_error = state->dp_error;
*deleted = state->deleted;
return EOK;
}
static struct tevent_req *
sdap_sudo_ptask_full_refresh_send(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct be_ctx *be_ctx,
struct be_ptask *be_ptask,
void *pvt)
{
struct sdap_sudo_ctx *sudo_ctx;
sudo_ctx = talloc_get_type(pvt, struct sdap_sudo_ctx);
return sdap_sudo_full_refresh_send(mem_ctx, sudo_ctx);
}
static errno_t
sdap_sudo_ptask_full_refresh_recv(struct tevent_req *req)
{
int dp_error;
return sdap_sudo_full_refresh_recv(req, &dp_error);
}
static struct tevent_req *
sdap_sudo_ptask_smart_refresh_send(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct be_ctx *be_ctx,
struct be_ptask *be_ptask,
void *pvt)
{
struct sdap_sudo_ctx *sudo_ctx;
sudo_ctx = talloc_get_type(pvt, struct sdap_sudo_ctx);
return sdap_sudo_smart_refresh_send(mem_ctx, sudo_ctx);
}
static errno_t
sdap_sudo_ptask_smart_refresh_recv(struct tevent_req *req)
{
int dp_error;
return sdap_sudo_smart_refresh_recv(req, &dp_error);
}
errno_t
sdap_sudo_ptask_setup(struct be_ctx *be_ctx, struct sdap_sudo_ctx *sudo_ctx)
{
return sdap_sudo_ptask_setup_generic(be_ctx, sudo_ctx->id_ctx->opts->basic,
sdap_sudo_ptask_full_refresh_send,
sdap_sudo_ptask_full_refresh_recv,
sdap_sudo_ptask_smart_refresh_send,
sdap_sudo_ptask_smart_refresh_recv,
sudo_ctx);
}