s3:idmap:autorid: rename domainnum to rangenum
[obnox/samba/samba-obnox.git] / source3 / winbindd / idmap_autorid.c
index 1f4af335106e11d423a62367aaa18210568a7c33..ad0f32c55be5e1cc22533f98f9988d71a8cb11cc 100644 (file)
@@ -5,7 +5,7 @@
  *  based on the idmap_rid module, but this module defines the ranges
  *  for the domains by automatically allocating a range for each domain
  *
- *  Copyright (C) Christian Ambach, 2010-2011
+ *  Copyright (C) Christian Ambach, 2010-2012
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 #include "includes.h"
 #include "system/filesys.h"
 #include "winbindd.h"
-#include "dbwrap.h"
+#include "dbwrap/dbwrap.h"
+#include "dbwrap/dbwrap_open.h"
 #include "idmap.h"
+#include "idmap_rw.h"
 #include "../libcli/security/dom_sid.h"
 #include "util_tdb.h"
+#include "winbindd/idmap_tdb_common.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_IDMAP
 
 #define HWM "NEXT RANGE"
+#define ALLOC_HWM_UID "NEXT ALLOC UID"
+#define ALLOC_HWM_GID "NEXT ALLOC GID"
+#define ALLOC_RANGE "ALLOC"
 #define CONFIGKEY "CONFIG"
 
 struct autorid_global_config {
        uint32_t minvalue;
        uint32_t rangesize;
        uint32_t maxranges;
+       bool ignore_builtin;
 };
 
 struct autorid_domain_config {
-       struct dom_sid sid;
-       uint32_t domainnum;
+       fstring sid;
+       fstring keystr;
+       uint32_t rangenum;
+       uint32_t multiplier;
        struct autorid_global_config *globalcfg;
 };
 
 /* handle to the tdb storing domain <-> range assignments */
 static struct db_context *autorid_db;
 
-static NTSTATUS idmap_autorid_get_domainrange(struct db_context *db,
+static NTSTATUS idmap_autorid_get_domainrange_action(struct db_context *db,
                                              void *private_data)
 {
        NTSTATUS ret;
-       uint32_t domainnum, hwm;
-       fstring sidstr;
+       uint32_t rangenum, hwm;
        char *numstr;
        struct autorid_domain_config *cfg;
 
        cfg = (struct autorid_domain_config *)private_data;
-       dom_sid_string_buf(&(cfg->sid), sidstr, sizeof(sidstr));
 
-       if (!dbwrap_fetch_uint32(db, sidstr, &domainnum)) {
-               DEBUG(10, ("Acquiring new range for domain %s\n", sidstr));
+       ret = dbwrap_fetch_uint32_bystring(db, cfg->keystr,
+                                          &(cfg->rangenum));
 
-               /* fetch the current HWM */
-               if (!dbwrap_fetch_uint32(db, HWM, &hwm)) {
-                       DEBUG(1, ("Fatal error while fetching current "
-                                 "HWM value!\n"));
-                       ret = NT_STATUS_INTERNAL_ERROR;
-                       goto error;
-               }
+       if (NT_STATUS_IS_OK(ret)) {
+               /* entry is already present*/
+               return ret;
+       }
 
-               /* do we have a range left? */
-               if (hwm >= cfg->globalcfg->maxranges) {
-                       DEBUG(1, ("No more domain ranges available!\n"));
-                       ret = NT_STATUS_NO_MEMORY;
-                       goto error;
-               }
+       DEBUG(10, ("Acquiring new range for domain %s (multiplier=%"PRIu32")\n",
+                  cfg->sid, cfg->multiplier));
 
-               /* increase the HWM */
-               ret = dbwrap_change_uint32_atomic(db, HWM, &domainnum, 1);
-               if (!NT_STATUS_IS_OK(ret)) {
-                       DEBUG(1, ("Fatal error while fetching a new "
-                                 "domain range value!\n"));
-                       goto error;
-               }
+       /* fetch the current HWM */
+       ret = dbwrap_fetch_uint32_bystring(db, HWM, &hwm);
+       if (!NT_STATUS_IS_OK(ret)) {
+               DEBUG(1, ("Fatal error while fetching current "
+                         "HWM value: %s\n", nt_errstr(ret)));
+               ret = NT_STATUS_INTERNAL_ERROR;
+               goto error;
+       }
 
-               /* store away the new mapping in both directions */
-               ret = dbwrap_trans_store_uint32(db, sidstr, domainnum);
-               if (!NT_STATUS_IS_OK(ret)) {
-                       DEBUG(1, ("Fatal error while storing new "
-                                 "domain->range assignment!\n"));
-                       goto error;
-               }
+       /* do we have a range left? */
+       if (hwm >= cfg->globalcfg->maxranges) {
+               DEBUG(1, ("No more domain ranges available!\n"));
+               ret = NT_STATUS_NO_MEMORY;
+               goto error;
+       }
 
-               numstr = talloc_asprintf(db, "%u", domainnum);
-               if (!numstr) {
-                       ret = NT_STATUS_NO_MEMORY;
-                       goto error;
-               }
+       /* increase the HWM */
+       ret = dbwrap_change_uint32_atomic_bystring(db, HWM, &rangenum, 1);
+       if (!NT_STATUS_IS_OK(ret)) {
+               DEBUG(1, ("Fatal error while fetching a new "
+                         "domain range value!\n"));
+               goto error;
+       }
 
-               ret = dbwrap_trans_store_bystring(db, numstr,
-                                                 string_term_tdb_data(sidstr),
-                                                 TDB_INSERT);
-               talloc_free(numstr);
-               if (!NT_STATUS_IS_OK(ret)) {
-                       DEBUG(1, ("Fatal error while storing "
-                                 "new domain->range assignment!\n"));
-                       goto error;
-               }
-               DEBUG(5, ("Acquired new range #%d for domain %s\n",
-                         domainnum, sidstr));
+       /* store away the new mapping in both directions */
+       ret = dbwrap_store_uint32_bystring(db, cfg->keystr, rangenum);
+       if (!NT_STATUS_IS_OK(ret)) {
+               DEBUG(1, ("Fatal error while storing new "
+                         "domain->range assignment!\n"));
+               goto error;
        }
 
-       DEBUG(10, ("Using range #%d for domain %s\n", domainnum, sidstr));
-       cfg->domainnum = domainnum;
+       numstr = talloc_asprintf(db, "%u", rangenum);
+       if (!numstr) {
+               ret = NT_STATUS_NO_MEMORY;
+               goto error;
+       }
+
+       ret = dbwrap_store_bystring(db, numstr,
+                       string_term_tdb_data(cfg->keystr), TDB_INSERT);
+
+       talloc_free(numstr);
+       if (!NT_STATUS_IS_OK(ret)) {
+               DEBUG(1, ("Fatal error while storing "
+                         "new domain->range assignment!\n"));
+               goto error;
+       }
+       DEBUG(5, ("Acquired new range #%d for domain %s "
+                 "(multiplier=%"PRIu32")\n", rangenum, cfg->keystr,
+                 cfg->multiplier));
+
+       cfg->rangenum = rangenum;
 
        return NT_STATUS_OK;
 
-      error:
+error:
+       return ret;
+
+}
+
+static NTSTATUS idmap_autorid_get_domainrange(struct autorid_domain_config *dom,
+                                             bool read_only)
+{
+       NTSTATUS ret;
+
+       /*
+        * try to find mapping without locking the database,
+        * if it is not found create a mapping in a transaction unless
+        * read-only mode has been set
+        */
+       if (dom->multiplier > 0) {
+               snprintf(dom->keystr, FSTRING_LEN, "%s#%"PRIu32, dom->sid,
+                       dom->multiplier);
+       } else {
+               fstrcpy(dom->keystr, dom->sid);
+       }
+
+       ret = dbwrap_fetch_uint32_bystring(autorid_db, dom->keystr,
+                                          &(dom->rangenum));
+
+       if (!NT_STATUS_IS_OK(ret)) {
+               if (read_only) {
+                       return NT_STATUS_NOT_FOUND;
+               }
+               ret = dbwrap_trans_do(autorid_db,
+                             idmap_autorid_get_domainrange_action, dom);
+       }
+
+       DEBUG(10, ("Using range #%d for domain %s (multiplier=%"PRIu32")\n",
+                  dom->rangenum, dom->sid, dom->multiplier));
+
+       return ret;
+}
+
+static NTSTATUS idmap_autorid_allocate_id(struct idmap_domain *dom,
+                                         struct unixid *xid) {
+
+       NTSTATUS ret;
+       struct idmap_tdb_common_context *commoncfg;
+       struct autorid_global_config *globalcfg;
+       struct autorid_domain_config domaincfg;
+
+       commoncfg =
+           talloc_get_type_abort(dom->private_data,
+                                 struct idmap_tdb_common_context);
+
+       globalcfg = talloc_get_type(commoncfg->private_data,
+                                   struct autorid_global_config);
+
+       if (dom->read_only) {
+               DEBUG(3, ("Backend is read-only, refusing "
+                         "new allocation request\n"));
+               return NT_STATUS_UNSUCCESSFUL;
+       }
+
+       /* fetch the range for the allocation pool */
+
+       ZERO_STRUCT(domaincfg);
+
+       domaincfg.globalcfg = globalcfg;
+       fstrcpy(domaincfg.sid, ALLOC_RANGE);
+
+       ret = idmap_autorid_get_domainrange(&domaincfg, dom->read_only);
+
+       if (!NT_STATUS_IS_OK(ret)) {
+               DEBUG(3, ("Could not determine range for allocation pool, "
+                         "check previous messages for reason\n"));
+               return ret;
+       }
+
+       ret = idmap_tdb_common_get_new_id(dom, xid);
+
+       if (!NT_STATUS_IS_OK(ret)) {
+               DEBUG(1, ("Fatal error while allocating new ID!\n"));
+               return ret;
+       }
+
+       xid->id = globalcfg->minvalue +
+                 globalcfg->rangesize * domaincfg.rangenum +
+                 xid->id;
+
+       DEBUG(10, ("Returned new %s %d from allocation range\n",
+                  (xid->type==ID_TYPE_UID)?"uid":"gid", xid->id));
+
        return ret;
+}
 
+/*
+ * 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)
+{
+       NTSTATUS ret;
+
+       /* look out for the mapping */
+       ret = idmap_tdb_common_unixid_to_sid(dom, map);
+
+       if (NT_STATUS_IS_OK(ret)) {
+               map->status = ID_MAPPED;
+               return ret;
+       }
+
+       map->status = ID_UNKNOWN;
+
+       DEBUG(10, ("no ID->SID mapping for %d could be found\n", map->xid.id));
+
+       return ret;
 }
 
 static NTSTATUS idmap_autorid_id_to_sid(struct autorid_global_config *cfg,
+                                       struct idmap_domain *dom,
                                        struct id_map *map)
 {
        uint32_t range;
-       TDB_DATA data;
+       uint32_t multiplier = 0;
+       TDB_DATA data = tdb_null;
        char *keystr;
        struct dom_sid sid;
+       NTSTATUS status;
+       bool ok;
+       const char *q = NULL;
 
        /* can this be one of our ids? */
        if (map->xid.id < cfg->minvalue) {
@@ -157,29 +287,56 @@ static NTSTATUS idmap_autorid_id_to_sid(struct autorid_global_config *cfg,
                return NT_STATUS_NO_MEMORY;
        }
 
-       data = dbwrap_fetch_bystring(autorid_db, talloc_tos(), keystr);
+       status = dbwrap_fetch_bystring(autorid_db, talloc_tos(), keystr, &data);
        TALLOC_FREE(keystr);
 
-       if (!data.dptr) {
+       if (!NT_STATUS_IS_OK(status)) {
                DEBUG(4, ("id %d belongs to range %d which does not have "
                          "domain mapping, ignoring mapping request\n",
                          map->xid.id, range));
+               TALLOC_FREE(data.dptr);
                map->status = ID_UNKNOWN;
                return NT_STATUS_OK;
        }
 
-       string_to_sid(&sid, (const char *)data.dptr);
+       if (strncmp((const char *)data.dptr,
+                   ALLOC_RANGE,
+                   strlen(ALLOC_RANGE)) == 0) {
+               /*
+                * this is from the alloc range, check if there is a mapping
+                */
+               DEBUG(5, ("id %d belongs to allocation range, "
+                         "checking for mapping\n",
+                         map->xid.id));
+               TALLOC_FREE(data.dptr);
+               return idmap_autorid_map_id_to_sid(dom, map);
+       }
+
+       ok = dom_sid_parse_endp((const char *)data.dptr, &sid, &q);
        TALLOC_FREE(data.dptr);
+       if (!ok) {
+               map->status = ID_UNKNOWN;
+               return NT_STATUS_OK;
+       }
+       if (q != NULL)
+               if (sscanf(q+1, "%"SCNu32, &multiplier) != 1) {
+                       DEBUG(10, ("Multiplier not found! "
+                                  "ignoring mapping request\n"));
+                       map->status = ID_UNKNOWN;
+                       return NT_STATUS_OK;
+               }
 
        sid_compose(map->sid, &sid,
                    (map->xid.id - cfg->minvalue -
-                    range * cfg->rangesize));
+                    range * cfg->rangesize + (cfg->rangesize * multiplier)));
 
        /* 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;
+       map->xid.type = ID_TYPE_BOTH;
+
        return NT_STATUS_OK;
 }
 
@@ -195,15 +352,10 @@ static NTSTATUS idmap_autorid_sid_to_id(struct autorid_global_config *global,
 
        sid_peek_rid(map->sid, &rid);
 
-       /* if the rid is higher than the size of the range, we cannot map it */
-       if (rid >= global->rangesize) {
-               map->status = ID_UNKNOWN;
-               DEBUG(2, ("RID %d is larger then size of range (%d), "
-                         "user cannot be mapped\n", rid, global->rangesize));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
        map->xid.id = global->minvalue +
-           (global->rangesize * domain->domainnum)+rid;
+           (global->rangesize * domain->rangenum) + rid -
+           (global->rangesize * domain->multiplier);
+       map->xid.type = ID_TYPE_BOTH;
 
        /* We **really** should have some way of validating
           the SID exists and is the correct type here.  But
@@ -221,21 +373,29 @@ static NTSTATUS idmap_autorid_sid_to_id(struct autorid_global_config *global,
 static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
                                              struct id_map **ids)
 {
+       struct idmap_tdb_common_context *commoncfg;
        struct autorid_global_config *globalcfg;
        NTSTATUS ret;
        int i;
+       int num_tomap = 0;
+       int num_mapped = 0;
 
        /* initialize the status to avoid surprise */
        for (i = 0; ids[i]; i++) {
                ids[i]->status = ID_UNKNOWN;
+               num_tomap++;
        }
 
-       globalcfg = talloc_get_type(dom->private_data,
+       commoncfg =
+           talloc_get_type_abort(dom->private_data,
+                                 struct idmap_tdb_common_context);
+
+       globalcfg = talloc_get_type(commoncfg->private_data,
                                    struct autorid_global_config);
 
        for (i = 0; ids[i]; i++) {
 
-               ret = idmap_autorid_id_to_sid(globalcfg, ids[i]);
+               ret = idmap_autorid_id_to_sid(globalcfg, dom, ids[i]);
 
                if ((!NT_STATUS_IS_OK(ret)) &&
                    (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
@@ -244,13 +404,88 @@ static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
                                  " (%d)\n", ids[i]->xid.id));
                        goto failure;
                }
+
+               if (NT_STATUS_IS_OK(ret) && ids[i]->status == ID_MAPPED) {
+                       num_mapped++;
+               }
+
+       }
+
+       if (num_tomap == num_mapped) {
+               return NT_STATUS_OK;
+       } else if (num_mapped == 0) {
+               return NT_STATUS_NONE_MAPPED;
        }
-       return NT_STATUS_OK;
+
+       return STATUS_SOME_UNMAPPED;
+
 
       failure:
        return ret;
 }
 
+/*
+ * 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)
+{
+       NTSTATUS ret;
+       int res;
+
+       /* see if we already have a mapping */
+       ret = idmap_tdb_common_sid_to_unixid(dom, map);
+
+       if (NT_STATUS_IS_OK(ret)) {
+               map->status = ID_MAPPED;
+               return ret;
+       }
+
+       /* 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)));
+               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)));
+               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"));
+               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       }
+
+       ret = idmap_tdb_common_new_mapping(dom, map);
+
+       map->status = (NT_STATUS_IS_OK(ret))?ID_MAPPED:ID_UNMAPPED;
+
+       if (!NT_STATUS_IS_OK(ret)) {
+               if (dbwrap_transaction_cancel(ctx->db) != 0) {
+                       smb_panic("Cancelling transaction failed");
+               }
+               return ret;
+       }
+
+       res = dbwrap_transaction_commit(ctx->db);
+       if (res == 0) {
+               return ret;
+       }
+
+       DEBUG(2, ("transaction_commit failed\n"));
+       return NT_STATUS_INTERNAL_DB_CORRUPTION;
+
+}
+
 /**********************************
  lookup a set of sids.
 **********************************/
@@ -258,50 +493,104 @@ static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
 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;
+       int num_mapped = 0;
 
        /* initialize the status to avoid surprise */
        for (i = 0; ids[i]; i++) {
                ids[i]->status = ID_UNKNOWN;
+               num_tomap++;
        }
 
-       global = talloc_get_type(dom->private_data,
+       commoncfg =
+           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_domain_config domaincfg;
                uint32_t rid;
+               struct dom_sid domainsid;
 
                ZERO_STRUCT(domaincfg);
 
-               sid_copy(&domaincfg.sid, ids[i]->sid);
-               if (!sid_split_rid(&domaincfg.sid, &rid)) {
+               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) &&
+                   global->ignore_builtin) {
+                       DEBUG(10, ("Ignoring request for BUILTIN domain\n"));
+                       continue;
+               }
+
                /*
                 * Check if the domain is around
                 */
                domain = wcache_tdc_fetch_domainbysid(talloc_tos(),
-                                                     &domaincfg.sid);
+                                                     &domainsid);
                if (domain == NULL) {
                        DEBUG(10, ("Ignoring unknown domain sid %s\n",
-                                  sid_string_dbg(&domaincfg.sid)));
+                                  sid_string_dbg(&domainsid)));
                        continue;
                }
                TALLOC_FREE(domain);
 
                domaincfg.globalcfg = global;
+               sid_to_fstring(domaincfg.sid, &domainsid);
 
-               ret = dbwrap_trans_do(autorid_db,
-                                     idmap_autorid_get_domainrange,
-                                     &domaincfg);
+               /* Calculate multiplier for multi-range support */
+               domaincfg.multiplier = rid / (global->rangesize);
+
+               ret = idmap_autorid_get_domainrange(&domaincfg, 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, "
@@ -318,20 +607,55 @@ static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
                                  sid_string_dbg(ids[i]->sid)));
                        goto failure;
                }
+
+               if (NT_STATUS_IS_OK(ret)) {
+                       num_mapped++;
+               }
        }
-       return NT_STATUS_OK;
+
+       if (num_tomap == num_mapped) {
+               return NT_STATUS_OK;
+       } else if (num_mapped == 0) {
+               return NT_STATUS_NONE_MAPPED;
+       }
+
+       return STATUS_SOME_UNMAPPED;
 
       failure:
        return ret;
 
 }
 
+/* initialize the given HWM to 0 if it does not exist yet */
+static NTSTATUS idmap_autorid_init_hwm(const char *hwm) {
+
+       NTSTATUS status;
+       uint32_t hwmval;
+
+       status = dbwrap_fetch_uint32_bystring(autorid_db, hwm, &hwmval);
+       if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND))  {
+               status = dbwrap_trans_store_int32_bystring(autorid_db, hwm, 0);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0,
+                             ("Unable to initialise HWM (%s) in autorid "
+                              "database: %s\n", hwm, nt_errstr(status)));
+                       return NT_STATUS_INTERNAL_DB_ERROR;
+               }
+       } else if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0, ("unable to fetch HWM (%s) from autorid "
+                         "database: %s\n", hwm,  nt_errstr(status)));
+               return status;
+       }
+
+       return NT_STATUS_OK;
+}
+
 /*
  * open and initialize the database which stores the ranges for the domains
  */
 static NTSTATUS idmap_autorid_db_init(void)
 {
-       int32_t hwm;
+       NTSTATUS status;
 
        if (autorid_db) {
                /* its already open */
@@ -340,7 +664,8 @@ static NTSTATUS idmap_autorid_db_init(void)
 
        /* Open idmap repository */
        autorid_db = db_open(NULL, state_path("autorid.tdb"), 0,
-                            TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
+                            TDB_DEFAULT, O_RDWR | O_CREAT, 0644,
+                            DBWRAP_LOCK_ORDER_1);
 
        if (!autorid_db) {
                DEBUG(0, ("Unable to open idmap_autorid database '%s'\n",
@@ -349,18 +674,16 @@ static NTSTATUS idmap_autorid_db_init(void)
        }
 
        /* Initialize high water mark for the currently used range to 0 */
-       hwm = dbwrap_fetch_int32(autorid_db, HWM);
-       if ((hwm < 0)) {
-               if (!NT_STATUS_IS_OK
-                   (dbwrap_trans_store_int32(autorid_db, HWM, 0))) {
-                       DEBUG(0,
-                             ("Unable to initialise HWM in autorid "
-                              "database\n"));
-                       return NT_STATUS_INTERNAL_DB_ERROR;
-               }
-       }
 
-       return NT_STATUS_OK;
+       status = idmap_autorid_init_hwm(HWM);
+       NT_STATUS_NOT_OK_RETURN(status);
+
+       status = idmap_autorid_init_hwm(ALLOC_HWM_UID);
+       NT_STATUS_NOT_OK_RETURN(status);
+
+       status = idmap_autorid_init_hwm(ALLOC_HWM_GID);
+
+       return status;
 }
 
 static struct autorid_global_config *idmap_autorid_loadconfig(TALLOC_CTX * ctx)
@@ -369,15 +692,16 @@ static struct autorid_global_config *idmap_autorid_loadconfig(TALLOC_CTX * ctx)
        TDB_DATA data;
        struct autorid_global_config *cfg;
        unsigned long minvalue, rangesize, maxranges;
+       NTSTATUS status;
 
-       data = dbwrap_fetch_bystring(autorid_db, ctx, CONFIGKEY);
+       status = dbwrap_fetch_bystring(autorid_db, ctx, CONFIGKEY, &data);
 
-       if (!data.dptr) {
+       if (!NT_STATUS_IS_OK(status)) {
                DEBUG(10, ("No saved config found\n"));
                return NULL;
        }
 
-       cfg = TALLOC_ZERO_P(ctx, struct autorid_global_config);
+       cfg = talloc_zero(ctx, struct autorid_global_config);
        if (!cfg) {
                return NULL;
        }
@@ -429,14 +753,77 @@ static NTSTATUS idmap_autorid_saveconfig(struct autorid_global_config *cfg)
        return status;
 }
 
+static NTSTATUS idmap_autorid_preallocate_wellknown(struct idmap_domain *dom)
+{
+       const char *groups[] = { "S-1-1-0", "S-1-2-0", "S-1-2-1",
+               "S-1-3-0", "S-1-3-1", "S-1-3-2", "S-1-3-3", "S-1-3-4",
+               "S-1-5-1", "S-1-5-2", "S-1-5-3", "S-1-5-4", "S-1-5-6",
+               "S-1-5-7", "S-1-5-8", "S-1-5-9", "S-1-5-10", "S-1-5-11",
+               "S-1-5-12", "S-1-5-13", "S-1-5-14", "S-1-5-15",
+               "S-1-5-17", "S-1-5-18", "S-1-5-19", "S-1-5-20"
+       };
+
+       struct id_map **maps;
+       int i, num;
+       NTSTATUS status;
+
+       if (dom->read_only) {
+               return NT_STATUS_OK;
+       }
+
+       num = sizeof(groups)/sizeof(char*);
+
+       maps = talloc_zero_array(talloc_tos(), struct id_map*, num+1);
+       if (!maps) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       for (i = 0; i < num; i++) {
+               maps[i] = talloc(maps, struct id_map);
+               maps[i]->xid.type = ID_TYPE_GID;
+               maps[i]->sid = dom_sid_parse_talloc(maps, groups[i]);
+       }
+
+       maps[num] = NULL;
+
+       status = idmap_autorid_sids_to_unixids(dom, maps);
+
+       DEBUG(10,("Preallocation run finished with status %s\n",
+                 nt_errstr(status)));
+
+       talloc_free(maps);
+
+       return NT_STATUS_IS_OK(status)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
+}
+
 static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
 {
+       struct idmap_tdb_common_context *commonconfig;
        struct autorid_global_config *config;
        struct autorid_global_config *storedconfig = NULL;
        NTSTATUS status;
        uint32_t hwm;
 
-       config = TALLOC_ZERO_P(dom, struct autorid_global_config);
+       if (!strequal(dom->name, "*")) {
+               DEBUG(0, ("idmap_autorid_initialize: Error: autorid configured "
+                         "for domain '%s'. But autorid can only be used for "
+                         "the default idmap configuration.\n", dom->name));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       commonconfig = talloc_zero(dom, struct idmap_tdb_common_context);
+       if (!commonconfig) {
+               DEBUG(0, ("Out of memory!\n"));
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       commonconfig->rw_ops = talloc_zero(commonconfig, struct idmap_rw_ops);
+       if (commonconfig->rw_ops == NULL) {
+               DEBUG(0, ("Out of memory!\n"));
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       config = talloc_zero(commonconfig, struct autorid_global_config);
        if (!config) {
                DEBUG(0, ("Out of memory!\n"));
                return NT_STATUS_NO_MEMORY;
@@ -448,7 +835,8 @@ static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
        }
 
        config->minvalue = dom->low_id;
-       config->rangesize = lp_parm_int(-1, "autorid", "rangesize", 100000);
+       config->rangesize = lp_parm_int(-1, "idmap config *",
+                                       "rangesize", 100000);
 
        if (config->rangesize < 2000) {
                DEBUG(1, ("autorid rangesize must be at least 2000\n"));
@@ -482,9 +870,10 @@ static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
        /* read previously stored config and current HWM */
        storedconfig = idmap_autorid_loadconfig(talloc_tos());
 
-       if (!dbwrap_fetch_uint32(autorid_db, HWM, &hwm)) {
+       status = dbwrap_fetch_uint32_bystring(autorid_db, HWM, &hwm);
+       if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("Fatal error while fetching current "
-                         "HWM value!\n"));
+                         "HWM value: %s\n", nt_errstr(status)));
                status = NT_STATUS_INTERNAL_ERROR;
                goto error;
        }
@@ -521,16 +910,30 @@ static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
        DEBUG(5, ("%d domain ranges with a size of %d are available\n",
                  config->maxranges, config->rangesize));
 
-       dom->private_data = config;
+       config->ignore_builtin = lp_parm_bool(-1, "idmap config *",
+                                             "ignore builtin", false);
 
-       if (!NT_STATUS_IS_OK(status)) {
-               goto error;
-       }
+       /* fill the TDB common configuration */
+       commonconfig->private_data = config;
 
-       return NT_STATUS_OK;
+       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;
 
-      error:
+       /* preallocate well-known SIDs in the pool */
+       status = idmap_autorid_preallocate_wellknown(dom);
+
+       goto done;
+
+error:
        talloc_free(config);
+
+done:
        talloc_free(storedconfig);
 
        return status;
@@ -543,9 +946,10 @@ static struct idmap_methods autorid_methods = {
        .init = idmap_autorid_initialize,
        .unixids_to_sids = idmap_autorid_unixids_to_sids,
        .sids_to_unixids = idmap_autorid_sids_to_unixids,
+       .allocate_id     = idmap_autorid_allocate_id
 };
 
-NTSTATUS idmap_autorid_init(void)
+NTSTATUS samba_init_module(void)
 {
        return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
                                  "autorid", &autorid_methods);