LCOV - code coverage report
Current view: top level - db - sysdb_ranges.c (source / functions) Hit Total Coverage
Test: .coverage.total Lines: 0 188 0.0 %
Date: 2015-10-19 Functions: 0 4 0.0 %

          Line data    Source code
       1             : /*
       2             :    SSSD
       3             : 
       4             :    System Database - ID ranges related calls
       5             : 
       6             :    Copyright (C) 2012 Sumit Bose <sbose@redhat.com>
       7             : 
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             : 
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "util/util.h"
      23             : #include "db/sysdb_private.h"
      24             : 
      25           0 : static errno_t find_attr_as_uint32_t(const struct ldb_message *msg,
      26             :                                      const char *attr_name, uint32_t *result)
      27             : {
      28             :     uint64_t val;
      29             : 
      30           0 :     val = ldb_msg_find_attr_as_uint64(msg, attr_name, UINT64_MAX);
      31             : 
      32           0 :     if (val == UINT64_MAX) {
      33           0 :         return ENOENT;
      34           0 :     } else if (val >= UINT32_MAX) {
      35           0 :         return EINVAL;
      36             :     }
      37             : 
      38           0 :     *result = val;
      39           0 :     return EOK;
      40             : }
      41             : 
      42           0 : errno_t sysdb_get_ranges(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
      43             :                              size_t *range_count,
      44             :                              struct range_info ***range_list)
      45             : {
      46             :     size_t c;
      47             :     errno_t ret;
      48             :     TALLOC_CTX *tmp_ctx;
      49             :     struct ldb_result *res;
      50           0 :     const char *attrs[] = {SYSDB_NAME,
      51             :                            SYSDB_BASE_ID,
      52             :                            SYSDB_ID_RANGE_SIZE,
      53             :                            SYSDB_BASE_RID,
      54             :                            SYSDB_SECONDARY_BASE_RID,
      55             :                            SYSDB_DOMAIN_ID,
      56             :                            SYSDB_ID_RANGE_TYPE,
      57             :                            NULL};
      58             :     struct range_info **list;
      59             :     struct ldb_dn *basedn;
      60             :     const char *tmp_str;
      61             : 
      62           0 :     tmp_ctx = talloc_new(NULL);
      63           0 :     if (tmp_ctx == NULL) {
      64           0 :         ret = ENOMEM;
      65           0 :         goto done;
      66             :     }
      67             : 
      68           0 :     basedn = ldb_dn_new(tmp_ctx, sysdb->ldb, SYSDB_TMPL_RANGE_BASE);
      69           0 :     if (basedn == NULL) {
      70           0 :         ret = EIO;
      71           0 :         goto done;
      72             :     }
      73           0 :     ret = ldb_search(sysdb->ldb, tmp_ctx, &res,
      74             :                      basedn, LDB_SCOPE_ONELEVEL,
      75             :                      attrs, "objectclass=%s", SYSDB_ID_RANGE_CLASS);
      76           0 :     if (ret != LDB_SUCCESS) {
      77           0 :         ret = EIO;
      78           0 :         goto done;
      79             :     }
      80             : 
      81           0 :     list = talloc_zero_array(tmp_ctx, struct range_info *, res->count + 1);
      82           0 :     if (list == NULL) {
      83           0 :         ret = ENOMEM;
      84           0 :         goto done;
      85             :     }
      86             : 
      87           0 :     for (c = 0; c < res->count; c++) {
      88           0 :         list[c] = talloc_zero(list, struct range_info);
      89           0 :         if (list[c] == NULL) {
      90           0 :             ret = ENOMEM;
      91           0 :             goto done;
      92             :         }
      93           0 :         tmp_str = ldb_msg_find_attr_as_string(res->msgs[c], SYSDB_NAME, NULL);
      94           0 :         if (tmp_str == NULL) {
      95           0 :             DEBUG(SSSDBG_MINOR_FAILURE, "The object [%s] doesn't have a name.\n",
      96             :                                        ldb_dn_get_linearized(res->msgs[c]->dn));
      97           0 :             ret = EINVAL;
      98           0 :             goto done;
      99             :         }
     100             : 
     101           0 :         list[c]->name = talloc_strdup(list, tmp_str);
     102           0 :         if (list[c]->name == NULL) {
     103           0 :             ret = ENOMEM;
     104           0 :             goto done;
     105             :         }
     106             : 
     107           0 :         tmp_str = ldb_msg_find_attr_as_string(res->msgs[c], SYSDB_DOMAIN_ID,
     108             :                                               NULL);
     109           0 :         if (tmp_str != NULL) {
     110           0 :             list[c]->trusted_dom_sid = talloc_strdup(list, tmp_str);
     111           0 :             if (list[c]->trusted_dom_sid == NULL) {
     112           0 :                 ret = ENOMEM;
     113           0 :                 goto done;
     114             :             }
     115             :         }
     116             : 
     117           0 :         ret = find_attr_as_uint32_t(res->msgs[c], SYSDB_BASE_ID,
     118           0 :                                     &list[c]->base_id);
     119           0 :         if (ret != EOK && ret != ENOENT) {
     120           0 :             DEBUG(SSSDBG_MINOR_FAILURE, "find_attr_as_uint32_t failed.\n");
     121           0 :             goto done;
     122             :         }
     123             : 
     124           0 :         ret = find_attr_as_uint32_t(res->msgs[c], SYSDB_ID_RANGE_SIZE,
     125           0 :                                     &list[c]->id_range_size);
     126           0 :         if (ret != EOK && ret != ENOENT) {
     127           0 :             DEBUG(SSSDBG_MINOR_FAILURE, "find_attr_as_uint32_t failed.\n");
     128           0 :             goto done;
     129             :         }
     130             : 
     131           0 :         ret = find_attr_as_uint32_t(res->msgs[c], SYSDB_BASE_RID,
     132           0 :                                     &list[c]->base_rid);
     133           0 :         if (ret != EOK && ret != ENOENT) {
     134           0 :             DEBUG(SSSDBG_MINOR_FAILURE, "find_attr_as_uint32_t failed.\n");
     135           0 :             goto done;
     136             :         }
     137             : 
     138           0 :         ret = find_attr_as_uint32_t(res->msgs[c], SYSDB_SECONDARY_BASE_RID,
     139           0 :                                     &list[c]->secondary_base_rid);
     140           0 :         if (ret != EOK && ret != ENOENT) {
     141           0 :             DEBUG(SSSDBG_MINOR_FAILURE, "find_attr_as_uint32_t failed.\n");
     142           0 :             goto done;
     143             :         }
     144             : 
     145           0 :         tmp_str = ldb_msg_find_attr_as_string(res->msgs[c], SYSDB_ID_RANGE_TYPE,
     146             :                                               NULL);
     147           0 :         if (tmp_str != NULL) {
     148           0 :             list[c]->range_type = talloc_strdup(list, tmp_str);
     149           0 :             if (list[c]->range_type == NULL) {
     150           0 :                 ret = ENOMEM;
     151           0 :                 goto done;
     152             :             }
     153             :         }
     154             : 
     155             :     }
     156           0 :     list[res->count] = NULL;
     157             : 
     158           0 :     *range_count = res->count;
     159           0 :     *range_list = talloc_steal(mem_ctx, list);
     160           0 :     ret = EOK;
     161             : 
     162             : done:
     163           0 :     talloc_free(tmp_ctx);
     164           0 :     return ret;
     165             : }
     166             : 
     167           0 : errno_t sysdb_range_create(struct sysdb_ctx *sysdb, struct range_info *range)
     168             : {
     169             :     struct ldb_message *msg;
     170             :     int ret;
     171             :     TALLOC_CTX *tmp_ctx;
     172             : 
     173             :     /* if both or none are set, skip */
     174           0 :     if ((range->trusted_dom_sid == NULL && range->secondary_base_rid == 0) ||
     175           0 :         (range->trusted_dom_sid != NULL && range->secondary_base_rid != 0)) {
     176             : 
     177           0 :         DEBUG(SSSDBG_OP_FAILURE, "Invalid range, skipping. Expected that "
     178             :                     "either the secondary base RID or the SID of the trusted "
     179             :                     "domain is set, but not both or none of them.\n");
     180           0 :         return EOK;
     181             :     }
     182             : 
     183           0 :     tmp_ctx = talloc_new(NULL);
     184           0 :     if (!tmp_ctx) {
     185           0 :         return ENOMEM;
     186             :     }
     187             : 
     188           0 :     msg = ldb_msg_new(tmp_ctx);
     189           0 :     if (!msg) {
     190           0 :         ret = ENOMEM;
     191           0 :         goto done;
     192             :     }
     193             : 
     194           0 :     msg->dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb,
     195             :                              SYSDB_TMPL_RANGE, range->name);
     196           0 :     if (!msg->dn) {
     197           0 :         ret = ENOMEM;
     198           0 :         goto done;
     199             :     }
     200             : 
     201           0 :     ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_OBJECTCLASS,
     202             :                      SYSDB_ID_RANGE_CLASS);
     203           0 :     if (ret) goto done;
     204             : 
     205           0 :     if (range->trusted_dom_sid == NULL && range->secondary_base_rid != 0) {
     206           0 :         ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_OBJECTCLASS,
     207             :                          SYSDB_DOMAIN_ID_RANGE_CLASS);
     208           0 :         if (ret) goto done;
     209             : 
     210           0 :         ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_SECONDARY_BASE_RID,
     211           0 :                         (unsigned long) range->secondary_base_rid);
     212           0 :         if (ret) goto done;
     213           0 :     } else if (range->trusted_dom_sid != NULL &&
     214           0 :                range->secondary_base_rid == 0) {
     215           0 :         ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_OBJECTCLASS,
     216             :                          SYSDB_TRUSTED_AD_DOMAIN_RANGE_CLASS);
     217           0 :         if (ret) goto done;
     218             : 
     219           0 :         ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_DOMAIN_ID,
     220           0 :                          range->trusted_dom_sid);
     221           0 :         if (ret) goto done;
     222             :     }
     223             : 
     224           0 :     ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_NAME, range->name);
     225           0 :     if (ret) goto done;
     226             : 
     227           0 :     ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_BASE_ID,
     228           0 :                     (unsigned long) range->base_id);
     229           0 :     if (ret) goto done;
     230             : 
     231           0 :     ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_ID_RANGE_SIZE,
     232           0 :                     (unsigned long) range->id_range_size);
     233           0 :     if (ret) goto done;
     234             : 
     235           0 :     ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_BASE_RID,
     236           0 :                     (unsigned long) range->base_rid);
     237           0 :     if (ret) goto done;
     238             : 
     239           0 :     ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_CREATE_TIME,
     240           0 :                     (unsigned long)time(NULL));
     241           0 :     if (ret) goto done;
     242             : 
     243           0 :     ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_ID_RANGE_TYPE,
     244           0 :                      range->range_type);
     245           0 :     if (ret) goto done;
     246             : 
     247           0 :     ret = ldb_add(sysdb->ldb, msg);
     248           0 :     if (ret) goto done;
     249             : 
     250           0 :     ret = sysdb_error_to_errno(ret);
     251             : 
     252             : done:
     253           0 :     if (ret) {
     254           0 :         DEBUG(SSSDBG_TRACE_FUNC, "Error: %d (%s)\n", ret, strerror(ret));
     255             :     }
     256           0 :     talloc_zfree(tmp_ctx);
     257           0 :     return ret;
     258             : }
     259             : 
     260           0 : errno_t sysdb_update_ranges(struct sysdb_ctx *sysdb,
     261             :                             struct range_info **ranges)
     262             : {
     263             :     int ret;
     264             :     int sret;
     265             :     size_t c;
     266             :     size_t d;
     267           0 :     TALLOC_CTX *tmp_ctx = NULL;
     268             :     size_t cur_range_count;
     269             :     struct range_info **cur_ranges;
     270             :     struct ldb_dn *dn;
     271           0 :     bool in_transaction = false;
     272             :     bool *keep_range;
     273             : 
     274           0 :     tmp_ctx = talloc_new(NULL);
     275           0 :     if (tmp_ctx == NULL) {
     276           0 :         ret = ENOMEM;
     277           0 :         goto done;
     278             :     }
     279             : 
     280             :     /* Retrieve all ranges that are currently in sysdb */
     281           0 :     ret = sysdb_get_ranges(tmp_ctx, sysdb, &cur_range_count,
     282             :                                &cur_ranges);
     283           0 :     if (ret != EOK) {
     284           0 :         DEBUG(SSSDBG_OP_FAILURE, "sysdb_get_ranges failed.\n");
     285           0 :         goto done;
     286             :     }
     287             : 
     288           0 :     keep_range = talloc_zero_array(tmp_ctx, bool, cur_range_count);
     289           0 :     if (keep_range == NULL) {
     290           0 :         ret = ENOMEM;
     291           0 :         DEBUG(SSSDBG_OP_FAILURE, "talloc_zero_array failed.\n");
     292           0 :         goto done;
     293             :     }
     294             : 
     295           0 :     ret = sysdb_transaction_start(sysdb);
     296           0 :     if (ret != EOK) {
     297           0 :         DEBUG(SSSDBG_OP_FAILURE, "sysdb_transaction_start failed.\n");
     298           0 :         goto done;
     299             :     }
     300           0 :     in_transaction = true;
     301             : 
     302             :     /* Go through a list of retrieved ranges and:
     303             :      * - if a range already exists in sysdb, mark it for preservation
     304             :      * - if the range doesn't exist in sysdb, create it
     305             :      */
     306           0 :     for (c = 0; ranges[c] != NULL; c++) {
     307           0 :         for (d = 0; d < cur_range_count; d++) {
     308           0 :             if (strcasecmp(ranges[c]->name, cur_ranges[d]->name) == 0) {
     309           0 :                 keep_range[d] = true;
     310             :                 /* range already in cache, nothing to do */
     311           0 :                 break;
     312             :             }
     313             :         }
     314             : 
     315           0 :         if (d == cur_range_count) {
     316           0 :             DEBUG(SSSDBG_TRACE_FUNC, "Adding range [%s].\n", ranges[c]->name);
     317           0 :             ret = sysdb_range_create(sysdb, ranges[c]);
     318           0 :             if (ret != EOK) {
     319           0 :                 DEBUG(SSSDBG_OP_FAILURE, "sysdb_range_create failed.\n");
     320           0 :                 goto done;
     321             :             }
     322             :         }
     323             :     }
     324             : 
     325             :     /* Now delete all ranges that have been in sysdb prior to
     326             :      * refreshing the list and are not marked for preservation
     327             :      * (i.e. they are not in the new list of ranges)
     328             :      */
     329           0 :     for (d = 0; d < cur_range_count; d++) {
     330           0 :         if (!keep_range[d]) {
     331           0 :             DEBUG(SSSDBG_TRACE_FUNC, "Removing range [%s].\n",
     332             :                                       cur_ranges[d]->name);
     333           0 :             dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb,
     334           0 :                                 SYSDB_TMPL_RANGE, cur_ranges[d]->name);
     335           0 :             if (dn == NULL) {
     336           0 :                 ret = ENOMEM;
     337           0 :                 goto done;
     338             :             }
     339             : 
     340           0 :             ret = sysdb_delete_entry(sysdb, dn, true);
     341           0 :             if (ret != EOK) {
     342           0 :                 DEBUG(SSSDBG_OP_FAILURE, "sysdb_delete_entry failed.\n");
     343           0 :                 goto done;
     344             :             }
     345             :         }
     346             :     }
     347             : 
     348           0 :     ret = sysdb_transaction_commit(sysdb);
     349           0 :     if (ret != EOK) {
     350           0 :         DEBUG(SSSDBG_CRIT_FAILURE, "Could not commit transaction\n");
     351           0 :         goto done;
     352             :     }
     353           0 :     in_transaction = false;
     354             : 
     355             : done:
     356           0 :     if (in_transaction) {
     357           0 :         sret = sysdb_transaction_cancel(sysdb);
     358           0 :         if (sret != EOK) {
     359           0 :             DEBUG(SSSDBG_CRIT_FAILURE, "Could not cancel transaction\n");
     360             :         }
     361             :     }
     362           0 :     talloc_free(tmp_ctx);
     363           0 :     return ret;
     364             : }

Generated by: LCOV version 1.10