Revert "autorid: reserve 500 IDs at the top of the ALLOC range."
[obnox/samba/samba-obnox.git] / source3 / winbindd / idmap_autorid.c
index 35b4b68ba56f374d7b30b048760c7336b6fe6958..9e930b1748b90a2a9f5b8ce013ade4771746472a 100644 (file)
@@ -87,6 +87,22 @@ static struct db_context *autorid_db;
 
 static bool ignore_builtin = false;
 
+static NTSTATUS idmap_autorid_get_alloc_range(struct idmap_domain *dom,
+                                       struct autorid_range_config *range)
+{
+       NTSTATUS status;
+
+       ZERO_STRUCT(*range);
+
+       fstrcpy(range->domsid, ALLOC_RANGE);
+
+       status = idmap_autorid_get_domainrange(autorid_db,
+                                              range,
+                                              dom->read_only);
+
+       return status;
+}
+
 static NTSTATUS idmap_autorid_allocate_id(struct idmap_domain *dom,
                                          struct unixid *xid) {
 
@@ -101,12 +117,7 @@ static NTSTATUS idmap_autorid_allocate_id(struct idmap_domain *dom,
 
        /* fetch the range for the allocation pool */
 
-       ZERO_STRUCT(range);
-
-       fstrcpy(range.domsid, ALLOC_RANGE);
-
-       ret = idmap_autorid_get_domainrange(autorid_db, &range, dom->read_only);
-
+       ret = idmap_autorid_get_alloc_range(dom, &range);
        if (!NT_STATUS_IS_OK(ret)) {
                DEBUG(3, ("Could not determine range for allocation pool, "
                          "check previous messages for reason\n"));
@@ -131,8 +142,8 @@ static NTSTATUS idmap_autorid_allocate_id(struct idmap_domain *dom,
 /*
  * map a SID to xid using the idmap_tdb like pool
  */
-static NTSTATUS idmap_autorid_map_id_to_sid(struct idmap_domain *dom,
-                                           struct id_map *map)
+static NTSTATUS idmap_autorid_id_to_sid_alloc(struct idmap_domain *dom,
+                                             struct id_map *map)
 {
        NTSTATUS ret;
 
@@ -214,7 +225,7 @@ static NTSTATUS idmap_autorid_id_to_sid(struct autorid_global_config *cfg,
                          "checking for mapping\n",
                          map->xid.id));
                TALLOC_FREE(data.dptr);
-               return idmap_autorid_map_id_to_sid(dom, map);
+               return idmap_autorid_id_to_sid_alloc(dom, map);
        }
 
        ok = dom_sid_parse_endp((const char *)data.dptr, &domsid, &q);
@@ -250,7 +261,8 @@ static NTSTATUS idmap_autorid_id_to_sid(struct autorid_global_config *cfg,
  Single sid to id lookup function.
 **********************************/
 
-static NTSTATUS idmap_autorid_sid_to_id(struct autorid_global_config *global,
+static NTSTATUS idmap_autorid_sid_to_id_rid(
+                                       struct autorid_global_config *global,
                                        struct autorid_range_config *range,
                                        struct id_map *map)
 {
@@ -263,11 +275,6 @@ static NTSTATUS idmap_autorid_sid_to_id(struct autorid_global_config *global,
 
        map->xid.id = reduced_rid + range->low_id;
        map->xid.type = ID_TYPE_BOTH;
-
-       /* We **really** should have some way of validating
-          the SID exists and is the correct type here.  But
-          that is a deficiency in the idmap_rid design. */
-
        map->status = ID_MAPPED;
 
        return NT_STATUS_OK;
@@ -331,15 +338,120 @@ static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
        return ret;
 }
 
+static bool idmap_autorid_sid_is_special(struct dom_sid *sid)
+{
+       bool match;
+
+       match = sid_check_is_in_wellknown_domain(sid);
+       if (match) {
+               return true;
+       }
+
+       return false;
+}
+
+static NTSTATUS idmap_autorid_sid_to_id_special(struct idmap_domain *dom,
+                                               struct id_map *map)
+{
+       struct idmap_tdb_common_context *common =
+               talloc_get_type_abort(dom->private_data,
+                                     struct idmap_tdb_common_context);
+       uint32_t count;
+       struct autorid_range_config range;
+       NTSTATUS status;
+       uint32_t free_id;
+
+       status = idmap_autorid_get_alloc_range(dom, &range);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       /* Take the next free ID, counting from the top */
+       free_id = 0;
+       for (count = 0; count < IDMAP_AUTORID_ALLOC_RESERVED; count++) {
+               struct id_map test_map;
+               struct dom_sid sid;
+
+               test_map.sid = &sid;
+               test_map.xid.type = map->xid.type;
+               test_map.xid.id = range.high_id - count;
+               test_map.status = ID_UNKNOWN;
+
+               status = idmap_tdb_common_unixid_to_sid(dom, &test_map);
+               if (NT_STATUS_EQUAL(NT_STATUS_NONE_MAPPED, status)) {
+                       free_id = test_map.xid.id;
+                       break;
+               }
+
+               if (!NT_STATUS_IS_OK(status)) {
+                       /* error - get out */
+                       return status;
+               }
+
+               /* mapping exists - try next ID */
+       }
+
+       if (free_id == 0) {
+               return NT_STATUS_NONE_MAPPED;
+       }
+
+       map->status = ID_MAPPED;
+       map->xid.id = free_id;
+
+       status = common->rw_ops->set_mapping(dom, map);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(2, ("Error storing new mapping: %s\n",
+                         nt_errstr(status)));
+               return status;
+       }
+
+       return NT_STATUS_OK;
+}
+
+struct idmap_autorid_sid_to_id_alloc_ctx {
+       struct idmap_domain *dom;
+       struct id_map *map;
+};
+
+static NTSTATUS idmap_autorid_sid_to_id_alloc_action(
+                               struct db_context *db,
+                               void *private_data)
+{
+       struct idmap_autorid_sid_to_id_alloc_ctx *ctx;
+
+       ctx = (struct idmap_autorid_sid_to_id_alloc_ctx *)private_data;
+
+       if (idmap_autorid_sid_is_special(ctx->map->sid)) {
+               NTSTATUS ret;
+
+               ret = idmap_autorid_sid_to_id_special(ctx->dom, ctx->map);
+               if (NT_STATUS_IS_OK(ret)) {
+                       return NT_STATUS_OK;
+               }
+               if (!NT_STATUS_EQUAL(NT_STATUS_NONE_MAPPED, ret)) {
+                       return ret;
+               }
+
+               DEBUG(10, ("Sepecial sid %s not mapped. falling back to "
+                          "regular allocation\n",
+                          sid_string_dbg(ctx->map->sid)));
+       }
+
+       return idmap_tdb_common_new_mapping(ctx->dom, ctx->map);
+}
+
 /*
  * map a SID to xid using the idmap_tdb like pool
  */
-static NTSTATUS idmap_autorid_map_sid_to_id(struct idmap_domain *dom,
-                                           struct id_map *map,
-                                           struct idmap_tdb_common_context *ctx)
+static NTSTATUS idmap_autorid_sid_to_id_alloc(
+                                       struct idmap_tdb_common_context *ctx,
+                                       struct idmap_domain *dom,
+                                       struct id_map *map)
 {
        NTSTATUS ret;
-       int res;
+       struct idmap_autorid_sid_to_id_alloc_ctx alloc_ctx;
+
+       map->status = ID_UNKNOWN;
 
        /* see if we already have a mapping */
        ret = idmap_tdb_common_sid_to_unixid(dom, map);
@@ -351,46 +463,120 @@ static NTSTATUS idmap_autorid_map_sid_to_id(struct idmap_domain *dom,
 
        /* bad things happened */
        if (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
-               DEBUG(1, ("Looking up SID->ID mapping for %s failed\n",
-                         sid_string_dbg(map->sid)));
+               DEBUG(1, ("Looking up SID->ID mapping for %s failed: %s\n",
+                         sid_string_dbg(map->sid), nt_errstr(ret)));
                return ret;
        }
 
        if (dom->read_only) {
                DEBUG(3, ("Not allocating new mapping for %s, because backend "
                          "is read-only\n", sid_string_dbg(map->sid)));
+               map->status = ID_UNMAPPED;
                return NT_STATUS_NONE_MAPPED;
        }
 
        DEBUG(10, ("Creating new mapping in pool for %s\n",
                   sid_string_dbg(map->sid)));
 
-       /* create new mapping */
-       res = dbwrap_transaction_start(ctx->db);
-       if (res != 0) {
-               DEBUG(2, ("transaction_start failed\n"));
+       alloc_ctx.dom = dom;
+       alloc_ctx.map = map;
+
+       ret = dbwrap_trans_do(ctx->db, idmap_autorid_sid_to_id_alloc_action,
+                             &alloc_ctx);
+       if (!NT_STATUS_IS_OK(ret)) {
+               DEBUG(1, ("Failed to create a new mapping in alloc range: %s\n",
+                         nt_errstr(ret)));
                return NT_STATUS_INTERNAL_DB_CORRUPTION;
        }
 
-       ret = idmap_tdb_common_new_mapping(dom, map);
+       map->status = ID_MAPPED;
+       return NT_STATUS_OK;
+}
 
-       map->status = (NT_STATUS_IS_OK(ret))?ID_MAPPED:ID_UNMAPPED;
+static bool idmap_autorid_domsid_is_for_alloc(struct dom_sid *sid)
+{
+       bool match;
 
-       if (!NT_STATUS_IS_OK(ret)) {
-               if (dbwrap_transaction_cancel(ctx->db) != 0) {
-                       smb_panic("Cancelling transaction failed");
-               }
-               return ret;
+       match = sid_check_is_wellknown_domain(sid, NULL);
+       if (match) {
+               return true;
        }
 
-       res = dbwrap_transaction_commit(ctx->db);
-       if (res == 0) {
-               return ret;
+       return false;
+}
+
+static NTSTATUS idmap_autorid_sid_to_id(struct idmap_tdb_common_context *common,
+                                       struct idmap_domain *dom,
+                                       struct id_map *map)
+{
+       struct autorid_global_config *global =
+               talloc_get_type_abort(common->private_data,
+                                     struct autorid_global_config);
+       struct winbindd_tdc_domain *domain;
+       struct autorid_range_config range;
+       uint32_t rid;
+       struct dom_sid domainsid;
+       NTSTATUS ret;
+
+       ZERO_STRUCT(range);
+       map->status = ID_UNKNOWN;
+
+       DEBUG(10, ("Trying to map %s\n", sid_string_dbg(map->sid)));
+
+       sid_copy(&domainsid, map->sid);
+       if (!sid_split_rid(&domainsid, &rid)) {
+               DEBUG(4, ("Could not determine domain SID from %s, "
+                         "ignoring mapping request\n",
+                         sid_string_dbg(map->sid)));
+               map->status = ID_UNMAPPED;
+               return NT_STATUS_NONE_MAPPED;
+       }
+
+       if (idmap_autorid_domsid_is_for_alloc(&domainsid)) {
+               DEBUG(10, ("SID %s is for ALLOC range.\n",
+                          sid_string_dbg(map->sid)));
+
+               return idmap_autorid_sid_to_id_alloc(common, dom, map);
        }
 
-       DEBUG(2, ("transaction_commit failed\n"));
-       return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       if (dom_sid_equal(&domainsid, &global_sid_Builtin) && ignore_builtin) {
+               DEBUG(10, ("Ignoring request for BUILTIN domain\n"));
+               map->status = ID_UNMAPPED;
+               return NT_STATUS_NONE_MAPPED;
+       }
+
+       /*
+        * Check if the domain is around
+        */
+       domain = wcache_tdc_fetch_domainbysid(talloc_tos(),
+                                             &domainsid);
+       if (domain == NULL) {
+               DEBUG(10, ("Ignoring unknown domain sid %s\n",
+                          sid_string_dbg(&domainsid)));
+               map->status = ID_UNMAPPED;
+               return NT_STATUS_NONE_MAPPED;
+       }
+       TALLOC_FREE(domain);
+
+       sid_to_fstring(range.domsid, &domainsid);
+
+       range.domain_range_index = rid / (global->rangesize);
 
+       ret = idmap_autorid_get_domainrange(autorid_db, &range, dom->read_only);
+       if (NT_STATUS_EQUAL(ret, NT_STATUS_NOT_FOUND) && dom->read_only) {
+               DEBUG(10, ("read-only is enabled, did not allocate "
+                          "new range for domain %s\n",
+                          sid_string_dbg(&domainsid)));
+               map->status = ID_UNMAPPED;
+               return NT_STATUS_NONE_MAPPED;
+       }
+       if (!NT_STATUS_IS_OK(ret)) {
+               DEBUG(3, ("Could not determine range for domain, "
+                         "check previous messages for reason\n"));
+               return ret;
+       }
+
+       return idmap_autorid_sid_to_id_rid(global, &range, map);
 }
 
 /**********************************
@@ -401,7 +587,6 @@ static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
                                              struct id_map **ids)
 {
        struct idmap_tdb_common_context *commoncfg;
-       struct autorid_global_config *global;
        NTSTATUS ret;
        int i;
        int num_tomap = 0;
@@ -417,105 +602,17 @@ static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
            talloc_get_type_abort(dom->private_data,
                                  struct idmap_tdb_common_context);
 
-       global = talloc_get_type(commoncfg->private_data,
-                                struct autorid_global_config);
-
        for (i = 0; ids[i]; i++) {
-               struct winbindd_tdc_domain *domain;
-               struct autorid_range_config range;
-               uint32_t rid;
-               struct dom_sid domainsid;
-
-               ZERO_STRUCT(range);
-
-               DEBUG(10, ("Trying to map %s\n", sid_string_dbg(ids[i]->sid)));
-
-               sid_copy(&domainsid, ids[i]->sid);
-               if (!sid_split_rid(&domainsid, &rid)) {
-                       DEBUG(4, ("Could not determine domain SID from %s, "
-                                 "ignoring mapping request\n",
-                                 sid_string_dbg(ids[i]->sid)));
-                       continue;
-               }
-
-               /* is this a well-known SID? */
-
-               if (sid_check_is_wellknown_domain(&domainsid, NULL)) {
-
-                       DEBUG(10, ("SID %s is well-known, using pool\n",
-                                  sid_string_dbg(ids[i]->sid)));
-
-                       ret = idmap_autorid_map_sid_to_id(dom, ids[i],
-                                                         commoncfg);
-
-                       if (!NT_STATUS_IS_OK(ret) &&
-                           !NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
-                               DEBUG(3, ("Unexpected error resolving "
-                                         "SID (%s)\n",
-                                         sid_string_dbg(ids[i]->sid)));
-                               goto failure;
-                       }
-
-                       if (ids[i]->status == ID_MAPPED) {
-                               num_mapped++;
-                       }
-
-                       continue;
-               }
-
-               /* BUILTIN is passdb's job */
-               if (dom_sid_equal(&domainsid, &global_sid_Builtin) &&
-                   ignore_builtin) {
-                       DEBUG(10, ("Ignoring request for BUILTIN domain\n"));
-                       continue;
-               }
-
-               /*
-                * Check if the domain is around
-                */
-               domain = wcache_tdc_fetch_domainbysid(talloc_tos(),
-                                                     &domainsid);
-               if (domain == NULL) {
-                       DEBUG(10, ("Ignoring unknown domain sid %s\n",
-                                  sid_string_dbg(&domainsid)));
-                       continue;
-               }
-               TALLOC_FREE(domain);
-
-               sid_to_fstring(range.domsid, &domainsid);
-
-               /* Calculate domain_range_index for multi-range support */
-               range.domain_range_index = rid / (global->rangesize);
-
-               ret = idmap_autorid_get_domainrange(autorid_db, &range,
-                                                   dom->read_only);
-
-               /* read-only mode and a new domain range would be required? */
-               if (NT_STATUS_EQUAL(ret, NT_STATUS_NOT_FOUND) &&
-                   dom->read_only) {
-                       DEBUG(10, ("read-only is enabled, did not allocate "
-                                  "new range for domain %s\n",
-                                  sid_string_dbg(&domainsid)));
-                       continue;
-               }
-
-               if (!NT_STATUS_IS_OK(ret)) {
-                       DEBUG(3, ("Could not determine range for domain, "
-                                 "check previous messages for reason\n"));
-                       goto failure;
-               }
-
-               ret = idmap_autorid_sid_to_id(global, &range, ids[i]);
-
+               ret = idmap_autorid_sid_to_id(commoncfg, dom, ids[i]);
                if ((!NT_STATUS_IS_OK(ret)) &&
                    (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
                        /* some fatal error occurred, log it */
                        DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
                                  sid_string_dbg(ids[i]->sid)));
-                       goto failure;
+                       return ret;
                }
 
-               if (NT_STATUS_IS_OK(ret)) {
+               if (NT_STATUS_IS_OK(ret) && ids[i]->status == ID_MAPPED) {
                        num_mapped++;
                }
        }
@@ -527,10 +624,6 @@ static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
        }
 
        return STATUS_SOME_UNMAPPED;
-
-      failure:
-       return ret;
-
 }
 
 static NTSTATUS idmap_autorid_preallocate_wellknown(struct idmap_domain *dom)
@@ -580,6 +673,39 @@ static NTSTATUS idmap_autorid_preallocate_wellknown(struct idmap_domain *dom)
        return NT_STATUS_IS_OK(status)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
 }
 
+static NTSTATUS idmap_autorid_initialize_action(struct db_context *db,
+                                               void *private_data)
+{
+       struct idmap_domain *dom;
+       struct idmap_tdb_common_context *common;
+       struct autorid_global_config *config;
+       NTSTATUS status;
+
+       dom = (struct idmap_domain *)private_data;
+       common = (struct idmap_tdb_common_context *)dom->private_data;
+       config = (struct autorid_global_config *)common->private_data;
+
+       status = idmap_autorid_init_hwms(db);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       status = idmap_autorid_saveconfig(db, config);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("Failed to store configuration data!\n"));
+               return status;
+       }
+
+       status = idmap_autorid_preallocate_wellknown(dom);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("Failed to preallocate wellknown sids: %s\n",
+                         nt_errstr(status)));
+               return status;
+       }
+
+       return NT_STATUS_OK;
+}
+
 static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
 {
        struct idmap_tdb_common_context *commonconfig;
@@ -598,6 +724,7 @@ static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
                DEBUG(0, ("Out of memory!\n"));
                return NT_STATUS_NO_MEMORY;
        }
+       dom->private_data = commonconfig;
 
        commonconfig->rw_ops = talloc_zero(commonconfig, struct idmap_rw_ops);
        if (commonconfig->rw_ops == NULL) {
@@ -610,13 +737,7 @@ static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
                DEBUG(0, ("Out of memory!\n"));
                return NT_STATUS_NO_MEMORY;
        }
-
-       status = idmap_autorid_db_init(state_path("autorid.tdb"),
-                                      NULL, /* TALLOC_CTX */
-                                      &autorid_db);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto error;
-       }
+       commonconfig->private_data = config;
 
        config->minvalue = dom->low_id;
        config->rangesize = lp_parm_int(-1, "idmap config *",
@@ -626,8 +747,8 @@ static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
            config->rangesize;
 
        if (config->maxranges == 0) {
-               DEBUG(1, ("allowed uid range is smaller then rangesize, "
-                         "increase uid range or decrease rangesize\n"));
+               DEBUG(1, ("Allowed uid range is smaller than rangesize. "
+                         "Increase uid range or decrease rangesize.\n"));
                status = NT_STATUS_INVALID_PARAMETER;
                goto error;
        }
@@ -641,13 +762,6 @@ static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
                          config->maxranges));
        }
 
-       status = idmap_autorid_saveconfig(autorid_db, config);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to store configuration data!\n"));
-               goto error;
-       }
-
        DEBUG(5, ("%d domain ranges with a size of %d are available\n",
                  config->maxranges, config->rangesize));
 
@@ -655,19 +769,30 @@ static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
                                      "ignore builtin", false);
 
        /* fill the TDB common configuration */
-       commonconfig->private_data = config;
 
-       commonconfig->db = autorid_db;
        commonconfig->max_id = config->rangesize -1;
        commonconfig->hwmkey_uid = ALLOC_HWM_UID;
        commonconfig->hwmkey_gid = ALLOC_HWM_GID;
        commonconfig->rw_ops->get_new_id = idmap_autorid_allocate_id;
        commonconfig->rw_ops->set_mapping = idmap_tdb_common_set_mapping;
 
-       dom->private_data = commonconfig;
+       status = idmap_autorid_db_open(state_path("autorid.tdb"),
+                                      NULL, /* TALLOC_CTX */
+                                      &autorid_db);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto error;
+       }
 
-       /* preallocate well-known SIDs in the pool */
-       status = idmap_autorid_preallocate_wellknown(dom);
+       commonconfig->db = autorid_db;
+
+       status = dbwrap_trans_do(autorid_db,
+                                idmap_autorid_initialize_action,
+                                dom);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("Failed to init the idmap database: %s\n",
+                         nt_errstr(status)));
+               goto error;
+       }
 
        goto done;