386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose/*
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose SSSD
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose System Database - ID ranges related calls
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose Copyright (C) 2012 Sumit Bose <sbose@redhat.com>
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose This program is free software; you can redistribute it and/or modify
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose it under the terms of the GNU General Public License as published by
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose the Free Software Foundation; either version 3 of the License, or
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose (at your option) any later version.
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose This program is distributed in the hope that it will be useful,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose but WITHOUT ANY WARRANTY; without even the implied warranty of
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose GNU General Public License for more details.
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose You should have received a copy of the GNU General Public License
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose along with this program. If not, see <http://www.gnu.org/licenses/>.
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose*/
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose#include "util/util.h"
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose#include "db/sysdb_private.h"
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bosestatic errno_t find_attr_as_uint32_t(const struct ldb_message *msg,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose const char *attr_name, uint32_t *result)
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose{
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose uint64_t val;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose val = ldb_msg_find_attr_as_uint64(msg, attr_name, UINT64_MAX);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (val == UINT64_MAX) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose return ENOENT;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose } else if (val >= UINT32_MAX) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose return EINVAL;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose *result = val;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose return EOK;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose}
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Boseerrno_t sysdb_get_ranges(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose size_t *range_count,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose struct range_info ***range_list)
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose{
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose size_t c;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose errno_t ret;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose TALLOC_CTX *tmp_ctx;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose struct ldb_result *res;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose const char *attrs[] = {SYSDB_NAME,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose SYSDB_BASE_ID,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose SYSDB_ID_RANGE_SIZE,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose SYSDB_BASE_RID,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose SYSDB_SECONDARY_BASE_RID,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose SYSDB_DOMAIN_ID,
5e60c73cb91d1659755fb5ea829837db68d46163Sumit Bose SYSDB_ID_RANGE_TYPE,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose NULL};
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose struct range_info **list;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose struct ldb_dn *basedn;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose const char *tmp_str;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose tmp_ctx = talloc_new(NULL);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (tmp_ctx == NULL) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ENOMEM;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
62dbfd0596aa15ddf0d9384f426814edcf627331Simo Sorce basedn = ldb_dn_new(tmp_ctx, sysdb->ldb, SYSDB_TMPL_RANGE_BASE);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (basedn == NULL) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = EIO;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ldb_search(sysdb->ldb, tmp_ctx, &res,
98195e591c4d97caa6125e8214879660b740973fSumit Bose basedn, LDB_SCOPE_SUBTREE,
5c95a8f77a37cd9ca4e2f6037bebade5284f415cSimo Sorce attrs, "objectclass=%s", SYSDB_ID_RANGE_CLASS);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != LDB_SUCCESS) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = EIO;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
6a8999bf933f72923a271831d85a01ef88a81f39Jakub Hrozek list = talloc_zero_array(tmp_ctx, struct range_info *, res->count + 1);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (list == NULL) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ENOMEM;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose for (c = 0; c < res->count; c++) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose list[c] = talloc_zero(list, struct range_info);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (list[c] == NULL) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ENOMEM;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose tmp_str = ldb_msg_find_attr_as_string(res->msgs[c], SYSDB_NAME, NULL);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (tmp_str == NULL) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_MINOR_FAILURE, "The object [%s] doesn't have a name.\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov ldb_dn_get_linearized(res->msgs[c]->dn));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = EINVAL;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose list[c]->name = talloc_strdup(list, tmp_str);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (list[c]->name == NULL) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ENOMEM;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose tmp_str = ldb_msg_find_attr_as_string(res->msgs[c], SYSDB_DOMAIN_ID,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose NULL);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (tmp_str != NULL) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose list[c]->trusted_dom_sid = talloc_strdup(list, tmp_str);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (list[c]->trusted_dom_sid == NULL) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ENOMEM;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = find_attr_as_uint32_t(res->msgs[c], SYSDB_BASE_ID,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose &list[c]->base_id);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK && ret != ENOENT) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_MINOR_FAILURE, "find_attr_as_uint32_t failed.\n");
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = find_attr_as_uint32_t(res->msgs[c], SYSDB_ID_RANGE_SIZE,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose &list[c]->id_range_size);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK && ret != ENOENT) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_MINOR_FAILURE, "find_attr_as_uint32_t failed.\n");
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = find_attr_as_uint32_t(res->msgs[c], SYSDB_BASE_RID,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose &list[c]->base_rid);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK && ret != ENOENT) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_MINOR_FAILURE, "find_attr_as_uint32_t failed.\n");
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = find_attr_as_uint32_t(res->msgs[c], SYSDB_SECONDARY_BASE_RID,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose &list[c]->secondary_base_rid);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK && ret != ENOENT) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_MINOR_FAILURE, "find_attr_as_uint32_t failed.\n");
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
5e60c73cb91d1659755fb5ea829837db68d46163Sumit Bose
5e60c73cb91d1659755fb5ea829837db68d46163Sumit Bose tmp_str = ldb_msg_find_attr_as_string(res->msgs[c], SYSDB_ID_RANGE_TYPE,
5e60c73cb91d1659755fb5ea829837db68d46163Sumit Bose NULL);
5e60c73cb91d1659755fb5ea829837db68d46163Sumit Bose if (tmp_str != NULL) {
5e60c73cb91d1659755fb5ea829837db68d46163Sumit Bose list[c]->range_type = talloc_strdup(list, tmp_str);
5e60c73cb91d1659755fb5ea829837db68d46163Sumit Bose if (list[c]->range_type == NULL) {
5e60c73cb91d1659755fb5ea829837db68d46163Sumit Bose ret = ENOMEM;
5e60c73cb91d1659755fb5ea829837db68d46163Sumit Bose goto done;
5e60c73cb91d1659755fb5ea829837db68d46163Sumit Bose }
5e60c73cb91d1659755fb5ea829837db68d46163Sumit Bose }
5e60c73cb91d1659755fb5ea829837db68d46163Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
6a8999bf933f72923a271831d85a01ef88a81f39Jakub Hrozek list[res->count] = NULL;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose *range_count = res->count;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose *range_list = talloc_steal(mem_ctx, list);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = EOK;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bosedone:
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose talloc_free(tmp_ctx);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose return ret;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose}
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Boseerrno_t sysdb_range_create(struct sysdb_ctx *sysdb, struct range_info *range)
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose{
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose struct ldb_message *msg;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose int ret;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose TALLOC_CTX *tmp_ctx;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
fbd63f8f920a2591310d601b01c7b79aa4023a95Ondrej Kos /* if both or none are set, skip */
fbd63f8f920a2591310d601b01c7b79aa4023a95Ondrej Kos if ((range->trusted_dom_sid == NULL && range->secondary_base_rid == 0) ||
fbd63f8f920a2591310d601b01c7b79aa4023a95Ondrej Kos (range->trusted_dom_sid != NULL && range->secondary_base_rid != 0)) {
fbd63f8f920a2591310d601b01c7b79aa4023a95Ondrej Kos
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_OP_FAILURE, "Invalid range, skipping. Expected that "
fbd63f8f920a2591310d601b01c7b79aa4023a95Ondrej Kos "either the secondary base RID or the SID of the trusted "
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "domain is set, but not both or none of them.\n");
fbd63f8f920a2591310d601b01c7b79aa4023a95Ondrej Kos return EOK;
fbd63f8f920a2591310d601b01c7b79aa4023a95Ondrej Kos }
fbd63f8f920a2591310d601b01c7b79aa4023a95Ondrej Kos
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose tmp_ctx = talloc_new(NULL);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (!tmp_ctx) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose return ENOMEM;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose msg = ldb_msg_new(tmp_ctx);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (!msg) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ENOMEM;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
62dbfd0596aa15ddf0d9384f426814edcf627331Simo Sorce msg->dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb,
62dbfd0596aa15ddf0d9384f426814edcf627331Simo Sorce SYSDB_TMPL_RANGE, range->name);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (!msg->dn) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ENOMEM;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
a928f7a6bd7681db6e26cba3eb7da22d14288737Fabiano FidĂȘncio ret = sysdb_add_string(msg, SYSDB_OBJECTCLASS, SYSDB_ID_RANGE_CLASS);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret) goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (range->trusted_dom_sid == NULL && range->secondary_base_rid != 0) {
a928f7a6bd7681db6e26cba3eb7da22d14288737Fabiano FidĂȘncio ret = sysdb_add_string(msg, SYSDB_OBJECTCLASS,
a928f7a6bd7681db6e26cba3eb7da22d14288737Fabiano FidĂȘncio SYSDB_DOMAIN_ID_RANGE_CLASS);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret) goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
49d84c926b00ba1368372cdec255bceb58d66f43Fabiano FidĂȘncio ret = sysdb_add_ulong(msg, SYSDB_SECONDARY_BASE_RID,
49d84c926b00ba1368372cdec255bceb58d66f43Fabiano FidĂȘncio (unsigned long) range->secondary_base_rid);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret) goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose } else if (range->trusted_dom_sid != NULL &&
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose range->secondary_base_rid == 0) {
a928f7a6bd7681db6e26cba3eb7da22d14288737Fabiano FidĂȘncio ret = sysdb_add_string(msg, SYSDB_OBJECTCLASS,
a928f7a6bd7681db6e26cba3eb7da22d14288737Fabiano FidĂȘncio SYSDB_TRUSTED_AD_DOMAIN_RANGE_CLASS);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret) goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
a928f7a6bd7681db6e26cba3eb7da22d14288737Fabiano FidĂȘncio ret = sysdb_add_string(msg, SYSDB_DOMAIN_ID, range->trusted_dom_sid);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret) goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
a928f7a6bd7681db6e26cba3eb7da22d14288737Fabiano FidĂȘncio ret = sysdb_add_string(msg, SYSDB_NAME, range->name);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret) goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
49d84c926b00ba1368372cdec255bceb58d66f43Fabiano FidĂȘncio ret = sysdb_add_ulong(msg, SYSDB_BASE_ID, (unsigned long) range->base_id);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret) goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
49d84c926b00ba1368372cdec255bceb58d66f43Fabiano FidĂȘncio ret = sysdb_add_ulong(msg, SYSDB_ID_RANGE_SIZE,
49d84c926b00ba1368372cdec255bceb58d66f43Fabiano FidĂȘncio (unsigned long) range->id_range_size);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret) goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
49d84c926b00ba1368372cdec255bceb58d66f43Fabiano FidĂȘncio ret = sysdb_add_ulong(msg, SYSDB_BASE_RID,
49d84c926b00ba1368372cdec255bceb58d66f43Fabiano FidĂȘncio (unsigned long) range->base_rid);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret) goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
49d84c926b00ba1368372cdec255bceb58d66f43Fabiano FidĂȘncio ret = sysdb_add_ulong(msg, SYSDB_CREATE_TIME, (unsigned long)time(NULL));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret) goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
a928f7a6bd7681db6e26cba3eb7da22d14288737Fabiano FidĂȘncio ret = sysdb_add_string(msg, SYSDB_ID_RANGE_TYPE, range->range_type);
5e60c73cb91d1659755fb5ea829837db68d46163Sumit Bose if (ret) goto done;
5e60c73cb91d1659755fb5ea829837db68d46163Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ldb_add(sysdb->ldb, msg);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret) goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = sysdb_error_to_errno(ret);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bosedone:
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_TRACE_FUNC, "Error: %d (%s)\n", ret, strerror(ret));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose talloc_zfree(tmp_ctx);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose return ret;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose}
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Boseerrno_t sysdb_update_ranges(struct sysdb_ctx *sysdb,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose struct range_info **ranges)
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose{
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose int ret;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose int sret;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose size_t c;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose size_t d;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose TALLOC_CTX *tmp_ctx = NULL;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose size_t cur_range_count;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose struct range_info **cur_ranges;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose struct ldb_dn *dn;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose bool in_transaction = false;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose bool *keep_range;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose tmp_ctx = talloc_new(NULL);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (tmp_ctx == NULL) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ENOMEM;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose /* Retrieve all ranges that are currently in sysdb */
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = sysdb_get_ranges(tmp_ctx, sysdb, &cur_range_count,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose &cur_ranges);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_OP_FAILURE, "sysdb_get_ranges failed.\n");
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose keep_range = talloc_zero_array(tmp_ctx, bool, cur_range_count);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (keep_range == NULL) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ENOMEM;
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_OP_FAILURE, "talloc_zero_array failed.\n");
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = sysdb_transaction_start(sysdb);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_OP_FAILURE, "sysdb_transaction_start failed.\n");
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose in_transaction = true;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose /* Go through a list of retrieved ranges and:
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose * - if a range already exists in sysdb, mark it for preservation
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose * - if the range doesn't exist in sysdb, create it
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose */
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose for (c = 0; ranges[c] != NULL; c++) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose for (d = 0; d < cur_range_count; d++) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (strcasecmp(ranges[c]->name, cur_ranges[d]->name) == 0) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose keep_range[d] = true;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose /* range already in cache, nothing to do */
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose break;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (d == cur_range_count) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_FUNC, "Adding range [%s].\n", ranges[c]->name);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = sysdb_range_create(sysdb, ranges[c]);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_OP_FAILURE, "sysdb_range_create failed.\n");
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose /* Now delete all ranges that have been in sysdb prior to
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose * refreshing the list and are not marked for preservation
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose * (i.e. they are not in the new list of ranges)
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose */
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose for (d = 0; d < cur_range_count; d++) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (!keep_range[d]) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_FUNC, "Removing range [%s].\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov cur_ranges[d]->name);
62dbfd0596aa15ddf0d9384f426814edcf627331Simo Sorce dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb,
62dbfd0596aa15ddf0d9384f426814edcf627331Simo Sorce SYSDB_TMPL_RANGE, cur_ranges[d]->name);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (dn == NULL) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ENOMEM;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = sysdb_delete_entry(sysdb, dn, true);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_OP_FAILURE, "sysdb_delete_entry failed.\n");
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = sysdb_transaction_commit(sysdb);
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek if (ret != EOK) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "Could not commit transaction\n");
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek in_transaction = false;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bosedone:
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (in_transaction) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose sret = sysdb_transaction_cancel(sysdb);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (sret != EOK) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "Could not cancel transaction\n");
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose talloc_free(tmp_ctx);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose return ret;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose}