s3:winbindd: make sure we remove pending io requests before closing client sockets
[Samba.git] / source3 / winbindd / idmap_autorid.c
blobeeac2b063bee50720ffd20a54c0da4aaa69f7058
1 /*
2 * idmap_autorid: static map between Active Directory/NT RIDs
3 * and RFC 2307 accounts
5 * based on the idmap_rid module, but this module defines the ranges
6 * for the domains by automatically allocating a range for each domain
8 * Copyright (C) Christian Ambach, 2010-2012
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
26 * This module allocates ranges for domains to be used in a
27 * algorithmic mode like idmap_rid. Multiple ranges are supported
28 * for a single domain: If a rid exceeds the range size, a matching
29 * range is allocated to hold the rid's id.
31 * Here are the formulas applied:
34 * For a sid of the form domain_sid-rid, we have
36 * rid = reduced_rid + domain_range_index * range_size
38 * with
39 * reduced_rid := rid % range_size
40 * domain_range_index := rid / range_size
42 * And reduced_rid fits into a range.
44 * In the database, we associate a range_number to
45 * the pair domain_sid,domain_range_index.
47 * Now the unix id for the given sid calculates as:
49 * id = reduced_rid + range_low_id
51 * with
53 * range_low_id = low_id + range_number * range_size
56 * The inverse calculation goes like this:
58 * Given a unix id, let
60 * normalized_id := id - low_id
61 * reduced_rid := normalized_id % range_size
62 * range_number = normalized_id / range_size
64 * Then we have
66 * id = reduced_rid + low_id + range_number * range_size
68 * From the database, get the domain_sid,domain_range_index pair
69 * belonging to the range_number (if there is already one).
71 * Then the rid for the unix id calculates as:
73 * rid = reduced_rid + domain_range_index * range_size
76 #include "idmap_autorid_tdb.h"
77 #include "winbindd.h"
78 #include "idmap.h"
79 #include "idmap_rw.h"
80 #include "../libcli/security/dom_sid.h"
82 #undef DBGC_CLASS
83 #define DBGC_CLASS DBGC_IDMAP
85 #define IDMAP_AUTORID_ALLOC_RESERVED 500
87 /* handle to the tdb storing domain <-> range assignments */
88 static struct db_context *autorid_db;
90 static bool ignore_builtin = false;
92 static NTSTATUS idmap_autorid_get_alloc_range(struct idmap_domain *dom,
93 struct autorid_range_config *range)
95 NTSTATUS status;
97 ZERO_STRUCT(*range);
99 fstrcpy(range->domsid, ALLOC_RANGE);
101 status = idmap_autorid_get_domainrange(autorid_db,
102 range,
103 dom->read_only);
105 return status;
108 static NTSTATUS idmap_autorid_allocate_id(struct idmap_domain *dom,
109 struct unixid *xid) {
111 NTSTATUS ret;
112 struct autorid_range_config range;
114 if (dom->read_only) {
115 DEBUG(3, ("Backend is read-only, refusing "
116 "new allocation request\n"));
117 return NT_STATUS_UNSUCCESSFUL;
120 /* fetch the range for the allocation pool */
122 ret = idmap_autorid_get_alloc_range(dom, &range);
123 if (!NT_STATUS_IS_OK(ret)) {
124 DEBUG(3, ("Could not determine range for allocation pool, "
125 "check previous messages for reason\n"));
126 return ret;
129 ret = idmap_tdb_common_get_new_id(dom, xid);
131 if (!NT_STATUS_IS_OK(ret)) {
132 DEBUG(1, ("Fatal error while allocating new ID!\n"));
133 return ret;
136 xid->id = xid->id + range.low_id;
138 DEBUG(10, ("Returned new %s %d from allocation range\n",
139 (xid->type==ID_TYPE_UID)?"uid":"gid", xid->id));
141 return ret;
145 * map a SID to xid using the idmap_tdb like pool
147 static NTSTATUS idmap_autorid_id_to_sid_alloc(struct idmap_domain *dom,
148 struct id_map *map)
150 NTSTATUS ret;
152 /* look out for the mapping */
153 ret = idmap_tdb_common_unixid_to_sid(dom, map);
155 if (NT_STATUS_IS_OK(ret)) {
156 map->status = ID_MAPPED;
157 return ret;
160 map->status = ID_UNKNOWN;
162 DEBUG(10, ("no ID->SID mapping for %d could be found\n", map->xid.id));
164 return ret;
167 static NTSTATUS idmap_autorid_id_to_sid(struct autorid_global_config *cfg,
168 struct idmap_domain *dom,
169 struct id_map *map)
171 uint32_t range_number;
172 uint32_t domain_range_index = 0;
173 uint32_t normalized_id;
174 uint32_t reduced_rid;
175 uint32_t rid;
176 TDB_DATA data = tdb_null;
177 char *keystr;
178 struct dom_sid domsid;
179 NTSTATUS status;
180 bool ok;
181 const char *q = NULL;
183 /* can this be one of our ids? */
184 if (map->xid.id < cfg->minvalue) {
185 DEBUG(10, ("id %d is lower than minimum value, "
186 "ignoring mapping request\n", map->xid.id));
187 map->status = ID_UNKNOWN;
188 return NT_STATUS_OK;
191 if (map->xid.id > (cfg->minvalue + cfg->rangesize * cfg->maxranges)) {
192 DEBUG(10, ("id %d is outside of maximum id value, "
193 "ignoring mapping request\n", map->xid.id));
194 map->status = ID_UNKNOWN;
195 return NT_STATUS_OK;
198 /* determine the range of this uid */
200 normalized_id = map->xid.id - cfg->minvalue;
201 range_number = normalized_id / cfg->rangesize;
203 keystr = talloc_asprintf(talloc_tos(), "%u", range_number);
204 if (!keystr) {
205 return NT_STATUS_NO_MEMORY;
208 status = dbwrap_fetch_bystring(autorid_db, talloc_tos(), keystr, &data);
209 TALLOC_FREE(keystr);
211 if (!NT_STATUS_IS_OK(status)) {
212 DEBUG(4, ("id %d belongs to range %d which does not have "
213 "domain mapping, ignoring mapping request\n",
214 map->xid.id, range_number));
215 TALLOC_FREE(data.dptr);
216 map->status = ID_UNKNOWN;
217 return NT_STATUS_OK;
220 if (strncmp((const char *)data.dptr,
221 ALLOC_RANGE,
222 strlen(ALLOC_RANGE)) == 0) {
224 * this is from the alloc range, check if there is a mapping
226 DEBUG(5, ("id %d belongs to allocation range, "
227 "checking for mapping\n",
228 map->xid.id));
229 TALLOC_FREE(data.dptr);
230 return idmap_autorid_id_to_sid_alloc(dom, map);
233 ok = dom_sid_parse_endp((const char *)data.dptr, &domsid, &q);
234 TALLOC_FREE(data.dptr);
235 if (!ok) {
236 map->status = ID_UNKNOWN;
237 return NT_STATUS_OK;
239 if ((q != NULL) && (*q != '\0'))
240 if (sscanf(q+1, "%"SCNu32, &domain_range_index) != 1) {
241 DEBUG(10, ("Domain range index not found, "
242 "ignoring mapping request\n"));
243 map->status = ID_UNKNOWN;
244 return NT_STATUS_OK;
247 reduced_rid = normalized_id % cfg->rangesize;
248 rid = reduced_rid + domain_range_index * cfg->rangesize;
250 sid_compose(map->sid, &domsid, rid);
252 /* We **really** should have some way of validating
253 the SID exists and is the correct type here. But
254 that is a deficiency in the idmap_rid design. */
256 map->status = ID_MAPPED;
257 map->xid.type = ID_TYPE_BOTH;
259 return NT_STATUS_OK;
262 /**********************************
263 Single sid to id lookup function.
264 **********************************/
266 static NTSTATUS idmap_autorid_sid_to_id_rid(
267 struct autorid_global_config *global,
268 struct autorid_range_config *range,
269 struct id_map *map)
271 uint32_t rid;
272 uint32_t reduced_rid;
274 sid_peek_rid(map->sid, &rid);
276 reduced_rid = rid % global->rangesize;
278 map->xid.id = reduced_rid + range->low_id;
279 map->xid.type = ID_TYPE_BOTH;
280 map->status = ID_MAPPED;
282 return NT_STATUS_OK;
285 /**********************************
286 lookup a set of unix ids.
287 **********************************/
289 static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
290 struct id_map **ids)
292 struct idmap_tdb_common_context *commoncfg;
293 struct autorid_global_config *globalcfg;
294 NTSTATUS ret;
295 int i;
296 int num_tomap = 0;
297 int num_mapped = 0;
299 /* initialize the status to avoid surprise */
300 for (i = 0; ids[i]; i++) {
301 ids[i]->status = ID_UNKNOWN;
302 num_tomap++;
305 commoncfg =
306 talloc_get_type_abort(dom->private_data,
307 struct idmap_tdb_common_context);
309 globalcfg = talloc_get_type(commoncfg->private_data,
310 struct autorid_global_config);
312 for (i = 0; ids[i]; i++) {
314 ret = idmap_autorid_id_to_sid(globalcfg, dom, ids[i]);
316 if ((!NT_STATUS_IS_OK(ret)) &&
317 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
318 /* some fatal error occurred, log it */
319 DEBUG(3, ("Unexpected error resolving an ID "
320 " (%d)\n", ids[i]->xid.id));
321 goto failure;
324 if (NT_STATUS_IS_OK(ret) && ids[i]->status == ID_MAPPED) {
325 num_mapped++;
330 if (num_tomap == num_mapped) {
331 return NT_STATUS_OK;
332 } else if (num_mapped == 0) {
333 return NT_STATUS_NONE_MAPPED;
336 return STATUS_SOME_UNMAPPED;
339 failure:
340 return ret;
343 static bool idmap_autorid_sid_is_special(struct dom_sid *sid)
345 bool match;
347 match = sid_check_is_in_wellknown_domain(sid);
348 if (match) {
349 return true;
352 return false;
355 static NTSTATUS idmap_autorid_sid_to_id_special(struct idmap_domain *dom,
356 struct id_map *map)
358 struct idmap_tdb_common_context *common =
359 talloc_get_type_abort(dom->private_data,
360 struct idmap_tdb_common_context);
361 uint32_t count;
362 struct autorid_range_config range;
363 NTSTATUS status;
364 uint32_t free_id;
366 status = idmap_autorid_get_alloc_range(dom, &range);
367 if (!NT_STATUS_IS_OK(status)) {
368 return status;
371 /* Take the next free ID, counting from the top */
372 free_id = 0;
373 for (count = 0; count < IDMAP_AUTORID_ALLOC_RESERVED; count++) {
374 struct id_map test_map;
375 struct dom_sid sid;
377 test_map.sid = &sid;
378 test_map.xid.type = map->xid.type;
379 test_map.xid.id = range.high_id - count;
380 test_map.status = ID_UNKNOWN;
382 status = idmap_tdb_common_unixid_to_sid(dom, &test_map);
383 if (NT_STATUS_EQUAL(NT_STATUS_NONE_MAPPED, status)) {
384 free_id = test_map.xid.id;
385 break;
388 if (!NT_STATUS_IS_OK(status)) {
389 /* error - get out */
390 return status;
393 /* mapping exists - try next ID */
396 if (free_id == 0) {
397 return NT_STATUS_NONE_MAPPED;
400 map->status = ID_MAPPED;
401 map->xid.id = free_id;
403 status = common->rw_ops->set_mapping(dom, map);
404 if (!NT_STATUS_IS_OK(status)) {
405 DEBUG(2, ("Error storing new mapping: %s\n",
406 nt_errstr(status)));
407 return status;
410 return NT_STATUS_OK;
413 struct idmap_autorid_sid_to_id_alloc_ctx {
414 struct idmap_domain *dom;
415 struct id_map *map;
418 static NTSTATUS idmap_autorid_sid_to_id_alloc_action(
419 struct db_context *db,
420 void *private_data)
422 struct idmap_autorid_sid_to_id_alloc_ctx *ctx;
424 ctx = (struct idmap_autorid_sid_to_id_alloc_ctx *)private_data;
426 if (idmap_autorid_sid_is_special(ctx->map->sid)) {
427 NTSTATUS ret;
429 ret = idmap_autorid_sid_to_id_special(ctx->dom, ctx->map);
430 if (NT_STATUS_IS_OK(ret)) {
431 return NT_STATUS_OK;
433 if (!NT_STATUS_EQUAL(NT_STATUS_NONE_MAPPED, ret)) {
434 return ret;
437 DEBUG(10, ("Sepecial sid %s not mapped. falling back to "
438 "regular allocation\n",
439 sid_string_dbg(ctx->map->sid)));
442 return idmap_tdb_common_new_mapping(ctx->dom, ctx->map);
446 * map a SID to xid using the idmap_tdb like pool
448 static NTSTATUS idmap_autorid_sid_to_id_alloc(
449 struct idmap_tdb_common_context *ctx,
450 struct idmap_domain *dom,
451 struct id_map *map)
453 NTSTATUS ret;
454 struct idmap_autorid_sid_to_id_alloc_ctx alloc_ctx;
456 map->status = ID_UNKNOWN;
458 /* see if we already have a mapping */
459 ret = idmap_tdb_common_sid_to_unixid(dom, map);
461 if (NT_STATUS_IS_OK(ret)) {
462 map->status = ID_MAPPED;
463 return ret;
466 /* bad things happened */
467 if (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
468 DEBUG(1, ("Looking up SID->ID mapping for %s failed: %s\n",
469 sid_string_dbg(map->sid), nt_errstr(ret)));
470 return ret;
473 if (dom->read_only) {
474 DEBUG(3, ("Not allocating new mapping for %s, because backend "
475 "is read-only\n", sid_string_dbg(map->sid)));
476 map->status = ID_UNMAPPED;
477 return NT_STATUS_NONE_MAPPED;
480 DEBUG(10, ("Creating new mapping in pool for %s\n",
481 sid_string_dbg(map->sid)));
483 alloc_ctx.dom = dom;
484 alloc_ctx.map = map;
486 ret = dbwrap_trans_do(ctx->db, idmap_autorid_sid_to_id_alloc_action,
487 &alloc_ctx);
488 if (!NT_STATUS_IS_OK(ret)) {
489 DEBUG(1, ("Failed to create a new mapping in alloc range: %s\n",
490 nt_errstr(ret)));
491 return NT_STATUS_INTERNAL_DB_CORRUPTION;
494 map->status = ID_MAPPED;
495 return NT_STATUS_OK;
498 static bool idmap_autorid_domsid_is_for_alloc(struct dom_sid *sid)
500 bool match;
502 match = sid_check_is_wellknown_domain(sid, NULL);
503 if (match) {
504 return true;
507 return false;
510 static NTSTATUS idmap_autorid_sid_to_id(struct idmap_tdb_common_context *common,
511 struct idmap_domain *dom,
512 struct id_map *map)
514 struct autorid_global_config *global =
515 talloc_get_type_abort(common->private_data,
516 struct autorid_global_config);
517 struct winbindd_tdc_domain *domain;
518 struct autorid_range_config range;
519 uint32_t rid;
520 struct dom_sid domainsid;
521 NTSTATUS ret;
523 ZERO_STRUCT(range);
524 map->status = ID_UNKNOWN;
526 DEBUG(10, ("Trying to map %s\n", sid_string_dbg(map->sid)));
528 sid_copy(&domainsid, map->sid);
529 if (!sid_split_rid(&domainsid, &rid)) {
530 DEBUG(4, ("Could not determine domain SID from %s, "
531 "ignoring mapping request\n",
532 sid_string_dbg(map->sid)));
533 map->status = ID_UNMAPPED;
534 return NT_STATUS_NONE_MAPPED;
537 if (idmap_autorid_domsid_is_for_alloc(&domainsid)) {
538 DEBUG(10, ("SID %s is for ALLOC range.\n",
539 sid_string_dbg(map->sid)));
541 return idmap_autorid_sid_to_id_alloc(common, dom, map);
544 if (dom_sid_equal(&domainsid, &global_sid_Builtin) && ignore_builtin) {
545 DEBUG(10, ("Ignoring request for BUILTIN domain\n"));
546 map->status = ID_UNMAPPED;
547 return NT_STATUS_NONE_MAPPED;
551 * Check if the domain is around
553 domain = wcache_tdc_fetch_domainbysid(talloc_tos(),
554 &domainsid);
555 if (domain == NULL) {
556 DEBUG(10, ("Ignoring unknown domain sid %s\n",
557 sid_string_dbg(&domainsid)));
558 map->status = ID_UNMAPPED;
559 return NT_STATUS_NONE_MAPPED;
561 TALLOC_FREE(domain);
563 sid_to_fstring(range.domsid, &domainsid);
565 range.domain_range_index = rid / (global->rangesize);
567 ret = idmap_autorid_get_domainrange(autorid_db, &range, dom->read_only);
568 if (NT_STATUS_EQUAL(ret, NT_STATUS_NOT_FOUND) && dom->read_only) {
569 DEBUG(10, ("read-only is enabled, did not allocate "
570 "new range for domain %s\n",
571 sid_string_dbg(&domainsid)));
572 map->status = ID_UNMAPPED;
573 return NT_STATUS_NONE_MAPPED;
575 if (!NT_STATUS_IS_OK(ret)) {
576 DEBUG(3, ("Could not determine range for domain, "
577 "check previous messages for reason\n"));
578 return ret;
581 return idmap_autorid_sid_to_id_rid(global, &range, map);
584 /**********************************
585 lookup a set of sids.
586 **********************************/
588 static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
589 struct id_map **ids)
591 struct idmap_tdb_common_context *commoncfg;
592 NTSTATUS ret;
593 int i;
594 int num_tomap = 0;
595 int num_mapped = 0;
597 /* initialize the status to avoid surprise */
598 for (i = 0; ids[i]; i++) {
599 ids[i]->status = ID_UNKNOWN;
600 num_tomap++;
603 commoncfg =
604 talloc_get_type_abort(dom->private_data,
605 struct idmap_tdb_common_context);
607 for (i = 0; ids[i]; i++) {
608 ret = idmap_autorid_sid_to_id(commoncfg, dom, ids[i]);
609 if ((!NT_STATUS_IS_OK(ret)) &&
610 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
611 /* some fatal error occurred, log it */
612 DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
613 sid_string_dbg(ids[i]->sid)));
614 return ret;
617 if (NT_STATUS_IS_OK(ret) && ids[i]->status == ID_MAPPED) {
618 num_mapped++;
622 if (num_tomap == num_mapped) {
623 return NT_STATUS_OK;
624 } else if (num_mapped == 0) {
625 return NT_STATUS_NONE_MAPPED;
628 return STATUS_SOME_UNMAPPED;
631 static NTSTATUS idmap_autorid_preallocate_wellknown(struct idmap_domain *dom)
633 const char *groups[] = { "S-1-1-0", "S-1-2-0", "S-1-2-1",
634 "S-1-3-0", "S-1-3-1", "S-1-3-2", "S-1-3-3", "S-1-3-4",
635 "S-1-5-1", "S-1-5-2", "S-1-5-3", "S-1-5-4", "S-1-5-6",
636 "S-1-5-7", "S-1-5-8", "S-1-5-9", "S-1-5-10", "S-1-5-11",
637 "S-1-5-12", "S-1-5-13", "S-1-5-14", "S-1-5-15",
638 "S-1-5-17", "S-1-5-18", "S-1-5-19", "S-1-5-20"
641 struct id_map **maps;
642 int i, num;
643 NTSTATUS status;
645 if (dom->read_only) {
646 return NT_STATUS_OK;
649 num = ARRAY_SIZE(groups);
651 maps = talloc_array(talloc_tos(), struct id_map*, num+1);
652 if (!maps) {
653 return NT_STATUS_NO_MEMORY;
656 for (i = 0; i < num; i++) {
657 maps[i] = talloc(maps, struct id_map);
658 if (maps[i] == NULL) {
659 talloc_free(maps);
660 return NT_STATUS_NO_MEMORY;
662 maps[i]->xid.type = ID_TYPE_GID;
663 maps[i]->sid = dom_sid_parse_talloc(maps, groups[i]);
666 maps[num] = NULL;
668 status = idmap_autorid_sids_to_unixids(dom, maps);
670 DEBUG(10,("Preallocation run finished with status %s\n",
671 nt_errstr(status)));
673 talloc_free(maps);
675 return NT_STATUS_IS_OK(status)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
678 static NTSTATUS idmap_autorid_initialize_action(struct db_context *db,
679 void *private_data)
681 struct idmap_domain *dom;
682 struct idmap_tdb_common_context *common;
683 struct autorid_global_config *config;
684 NTSTATUS status;
686 dom = (struct idmap_domain *)private_data;
687 common = (struct idmap_tdb_common_context *)dom->private_data;
688 config = (struct autorid_global_config *)common->private_data;
690 status = idmap_autorid_init_hwms(db);
691 if (!NT_STATUS_IS_OK(status)) {
692 return status;
695 status = idmap_autorid_saveconfig(db, config);
696 if (!NT_STATUS_IS_OK(status)) {
697 DEBUG(1, ("Failed to store configuration data!\n"));
698 return status;
701 status = idmap_autorid_preallocate_wellknown(dom);
702 if (!NT_STATUS_IS_OK(status)) {
703 DEBUG(1, ("Failed to preallocate wellknown sids: %s\n",
704 nt_errstr(status)));
705 return status;
708 return NT_STATUS_OK;
711 static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
713 struct idmap_tdb_common_context *commonconfig;
714 struct autorid_global_config *config;
715 NTSTATUS status;
716 char *db_path;
718 if (!strequal(dom->name, "*")) {
719 DEBUG(0, ("idmap_autorid_initialize: Error: autorid configured "
720 "for domain '%s'. But autorid can only be used for "
721 "the default idmap configuration.\n", dom->name));
722 return NT_STATUS_INVALID_PARAMETER;
725 commonconfig = talloc_zero(dom, struct idmap_tdb_common_context);
726 if (!commonconfig) {
727 DEBUG(0, ("Out of memory!\n"));
728 return NT_STATUS_NO_MEMORY;
730 dom->private_data = commonconfig;
732 commonconfig->rw_ops = talloc_zero(commonconfig, struct idmap_rw_ops);
733 if (commonconfig->rw_ops == NULL) {
734 DEBUG(0, ("Out of memory!\n"));
735 return NT_STATUS_NO_MEMORY;
738 config = talloc_zero(commonconfig, struct autorid_global_config);
739 if (!config) {
740 DEBUG(0, ("Out of memory!\n"));
741 return NT_STATUS_NO_MEMORY;
743 commonconfig->private_data = config;
745 config->minvalue = dom->low_id;
746 config->rangesize = lp_parm_int(-1, "idmap config *",
747 "rangesize", 100000);
749 config->maxranges = (dom->high_id - dom->low_id + 1) /
750 config->rangesize;
752 if (config->maxranges == 0) {
753 DEBUG(1, ("Allowed uid range is smaller than rangesize. "
754 "Increase uid range or decrease rangesize.\n"));
755 status = NT_STATUS_INVALID_PARAMETER;
756 goto error;
759 /* check if the high-low limit is a multiple of the rangesize */
760 if ((dom->high_id - dom->low_id + 1) % config->rangesize != 0) {
761 DEBUG(5, ("High uid-low uid difference of %d "
762 "is not a multiple of the rangesize %d, "
763 "limiting ranges to lower boundary number of %d\n",
764 (dom->high_id - dom->low_id + 1), config->rangesize,
765 config->maxranges));
768 DEBUG(5, ("%d domain ranges with a size of %d are available\n",
769 config->maxranges, config->rangesize));
771 ignore_builtin = lp_parm_bool(-1, "idmap config *",
772 "ignore builtin", false);
774 /* fill the TDB common configuration */
776 commonconfig->max_id = config->rangesize - 1
777 - IDMAP_AUTORID_ALLOC_RESERVED;
778 commonconfig->hwmkey_uid = ALLOC_HWM_UID;
779 commonconfig->hwmkey_gid = ALLOC_HWM_GID;
780 commonconfig->rw_ops->get_new_id = idmap_autorid_allocate_id;
781 commonconfig->rw_ops->set_mapping = idmap_tdb_common_set_mapping;
783 db_path = state_path("autorid.tdb");
784 if (db_path == NULL) {
785 status = NT_STATUS_NO_MEMORY;
786 goto error;
789 status = idmap_autorid_db_open(db_path,
790 NULL, /* TALLOC_CTX */
791 &autorid_db);
792 TALLOC_FREE(db_path);
793 if (!NT_STATUS_IS_OK(status)) {
794 goto error;
797 commonconfig->db = autorid_db;
799 status = dbwrap_trans_do(autorid_db,
800 idmap_autorid_initialize_action,
801 dom);
802 if (!NT_STATUS_IS_OK(status)) {
803 DEBUG(1, ("Failed to init the idmap database: %s\n",
804 nt_errstr(status)));
805 goto error;
808 goto done;
810 error:
811 talloc_free(config);
813 done:
814 return status;
818 Close the idmap tdb instance
820 static struct idmap_methods autorid_methods = {
821 .init = idmap_autorid_initialize,
822 .unixids_to_sids = idmap_autorid_unixids_to_sids,
823 .sids_to_unixids = idmap_autorid_sids_to_unixids,
824 .allocate_id = idmap_autorid_allocate_id
827 NTSTATUS idmap_autorid_init(void)
829 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
830 "autorid", &autorid_methods);