krb5_wrap: Move krb5_princ_component() to the top
[Samba.git] / source3 / winbindd / idmap_autorid.c
blob20cd5b72a98a28ae6292dee4c437572edd8c6c70
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 (data.dptr[data.dsize-1] != '\0') {
221 DBG_WARNING("Invalid range %"PRIu32"\n", range_number);
222 TALLOC_FREE(data.dptr);
223 map->status = ID_UNKNOWN;
224 return NT_STATUS_OK;
227 if (strncmp((const char *)data.dptr,
228 ALLOC_RANGE,
229 strlen(ALLOC_RANGE)) == 0) {
231 * this is from the alloc range, check if there is a mapping
233 DEBUG(5, ("id %d belongs to allocation range, "
234 "checking for mapping\n",
235 map->xid.id));
236 TALLOC_FREE(data.dptr);
237 return idmap_autorid_id_to_sid_alloc(dom, map);
240 ok = dom_sid_parse_endp((const char *)data.dptr, &domsid, &q);
241 if (!ok) {
242 TALLOC_FREE(data.dptr);
243 map->status = ID_UNKNOWN;
244 return NT_STATUS_OK;
246 if ((q != NULL) && (*q != '\0'))
247 if (sscanf(q+1, "%"SCNu32, &domain_range_index) != 1) {
248 DEBUG(10, ("Domain range index not found, "
249 "ignoring mapping request\n"));
250 TALLOC_FREE(data.dptr);
251 map->status = ID_UNKNOWN;
252 return NT_STATUS_OK;
255 TALLOC_FREE(data.dptr);
257 reduced_rid = normalized_id % cfg->rangesize;
258 rid = reduced_rid + domain_range_index * cfg->rangesize;
260 sid_compose(map->sid, &domsid, rid);
262 /* We **really** should have some way of validating
263 the SID exists and is the correct type here. But
264 that is a deficiency in the idmap_rid design. */
266 map->status = ID_MAPPED;
267 map->xid.type = ID_TYPE_BOTH;
269 return NT_STATUS_OK;
272 /**********************************
273 Single sid to id lookup function.
274 **********************************/
276 static NTSTATUS idmap_autorid_sid_to_id_rid(
277 struct autorid_global_config *global,
278 struct autorid_range_config *range,
279 struct id_map *map)
281 uint32_t rid;
282 uint32_t reduced_rid;
284 sid_peek_rid(map->sid, &rid);
286 reduced_rid = rid % global->rangesize;
288 map->xid.id = reduced_rid + range->low_id;
289 map->xid.type = ID_TYPE_BOTH;
290 map->status = ID_MAPPED;
292 return NT_STATUS_OK;
295 /**********************************
296 lookup a set of unix ids.
297 **********************************/
299 static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
300 struct id_map **ids)
302 struct idmap_tdb_common_context *commoncfg;
303 struct autorid_global_config *globalcfg;
304 NTSTATUS ret;
305 int i;
306 int num_tomap = 0;
307 int num_mapped = 0;
309 /* initialize the status to avoid surprise */
310 for (i = 0; ids[i]; i++) {
311 ids[i]->status = ID_UNKNOWN;
312 num_tomap++;
315 commoncfg =
316 talloc_get_type_abort(dom->private_data,
317 struct idmap_tdb_common_context);
319 globalcfg = talloc_get_type(commoncfg->private_data,
320 struct autorid_global_config);
322 for (i = 0; ids[i]; i++) {
324 ret = idmap_autorid_id_to_sid(globalcfg, dom, ids[i]);
326 if ((!NT_STATUS_IS_OK(ret)) &&
327 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
328 /* some fatal error occurred, log it */
329 DEBUG(3, ("Unexpected error resolving an ID "
330 " (%d)\n", ids[i]->xid.id));
331 goto failure;
334 if (NT_STATUS_IS_OK(ret) && ids[i]->status == ID_MAPPED) {
335 num_mapped++;
340 if (num_tomap == num_mapped) {
341 return NT_STATUS_OK;
342 } else if (num_mapped == 0) {
343 return NT_STATUS_NONE_MAPPED;
346 return STATUS_SOME_UNMAPPED;
349 failure:
350 return ret;
353 static bool idmap_autorid_sid_is_special(struct dom_sid *sid)
355 bool match;
357 match = sid_check_is_in_wellknown_domain(sid);
358 if (match) {
359 return true;
362 return false;
365 static NTSTATUS idmap_autorid_sid_to_id_special(struct idmap_domain *dom,
366 struct id_map *map)
368 struct idmap_tdb_common_context *common =
369 talloc_get_type_abort(dom->private_data,
370 struct idmap_tdb_common_context);
371 uint32_t count;
372 struct autorid_range_config range;
373 NTSTATUS status;
374 uint32_t free_id;
376 status = idmap_autorid_get_alloc_range(dom, &range);
377 if (!NT_STATUS_IS_OK(status)) {
378 return status;
381 /* Take the next free ID, counting from the top */
382 free_id = 0;
383 for (count = 0; count < IDMAP_AUTORID_ALLOC_RESERVED; count++) {
384 struct id_map test_map;
385 struct dom_sid sid;
387 test_map.sid = &sid;
388 test_map.xid.type = map->xid.type;
389 test_map.xid.id = range.high_id - count;
390 test_map.status = ID_UNKNOWN;
392 status = idmap_tdb_common_unixid_to_sid(dom, &test_map);
393 if (NT_STATUS_EQUAL(NT_STATUS_NONE_MAPPED, status)) {
394 free_id = test_map.xid.id;
395 break;
398 if (!NT_STATUS_IS_OK(status)) {
399 /* error - get out */
400 return status;
403 /* mapping exists - try next ID */
406 if (free_id == 0) {
407 return NT_STATUS_NONE_MAPPED;
410 map->status = ID_MAPPED;
411 map->xid.id = free_id;
413 status = common->rw_ops->set_mapping(dom, map);
414 if (!NT_STATUS_IS_OK(status)) {
415 DEBUG(2, ("Error storing new mapping: %s\n",
416 nt_errstr(status)));
417 return status;
420 return NT_STATUS_OK;
423 struct idmap_autorid_sid_to_id_alloc_ctx {
424 struct idmap_domain *dom;
425 struct id_map *map;
428 static NTSTATUS idmap_autorid_sid_to_id_alloc_action(
429 struct db_context *db,
430 void *private_data)
432 struct idmap_autorid_sid_to_id_alloc_ctx *ctx;
434 ctx = (struct idmap_autorid_sid_to_id_alloc_ctx *)private_data;
436 if (idmap_autorid_sid_is_special(ctx->map->sid)) {
437 NTSTATUS ret;
439 ret = idmap_autorid_sid_to_id_special(ctx->dom, ctx->map);
440 if (NT_STATUS_IS_OK(ret)) {
441 return NT_STATUS_OK;
443 if (!NT_STATUS_EQUAL(NT_STATUS_NONE_MAPPED, ret)) {
444 return ret;
447 DEBUG(10, ("Sepecial sid %s not mapped. falling back to "
448 "regular allocation\n",
449 sid_string_dbg(ctx->map->sid)));
452 return idmap_tdb_common_new_mapping(ctx->dom, ctx->map);
456 * map a SID to xid using the idmap_tdb like pool
458 static NTSTATUS idmap_autorid_sid_to_id_alloc(
459 struct idmap_tdb_common_context *ctx,
460 struct idmap_domain *dom,
461 struct id_map *map)
463 NTSTATUS ret;
464 struct idmap_autorid_sid_to_id_alloc_ctx alloc_ctx;
466 map->status = ID_UNKNOWN;
468 /* see if we already have a mapping */
469 ret = idmap_tdb_common_sid_to_unixid(dom, map);
471 if (NT_STATUS_IS_OK(ret)) {
472 map->status = ID_MAPPED;
473 return ret;
476 /* bad things happened */
477 if (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
478 DEBUG(1, ("Looking up SID->ID mapping for %s failed: %s\n",
479 sid_string_dbg(map->sid), nt_errstr(ret)));
480 return ret;
483 if (dom->read_only) {
484 DEBUG(3, ("Not allocating new mapping for %s, because backend "
485 "is read-only\n", sid_string_dbg(map->sid)));
486 map->status = ID_UNMAPPED;
487 return NT_STATUS_NONE_MAPPED;
490 DEBUG(10, ("Creating new mapping in pool for %s\n",
491 sid_string_dbg(map->sid)));
493 alloc_ctx.dom = dom;
494 alloc_ctx.map = map;
496 ret = dbwrap_trans_do(ctx->db, idmap_autorid_sid_to_id_alloc_action,
497 &alloc_ctx);
498 if (!NT_STATUS_IS_OK(ret)) {
499 DEBUG(1, ("Failed to create a new mapping in alloc range: %s\n",
500 nt_errstr(ret)));
501 return NT_STATUS_INTERNAL_DB_CORRUPTION;
504 map->status = ID_MAPPED;
505 return NT_STATUS_OK;
508 static bool idmap_autorid_domsid_is_for_alloc(struct dom_sid *sid)
510 bool match;
512 match = sid_check_is_wellknown_domain(sid, NULL);
513 if (match) {
514 return true;
517 return false;
520 static NTSTATUS idmap_autorid_sid_to_id(struct idmap_tdb_common_context *common,
521 struct idmap_domain *dom,
522 struct id_map *map)
524 struct autorid_global_config *global =
525 talloc_get_type_abort(common->private_data,
526 struct autorid_global_config);
527 struct winbindd_tdc_domain *domain;
528 struct autorid_range_config range;
529 uint32_t rid;
530 struct dom_sid domainsid;
531 NTSTATUS ret;
533 ZERO_STRUCT(range);
534 map->status = ID_UNKNOWN;
536 DEBUG(10, ("Trying to map %s\n", sid_string_dbg(map->sid)));
538 sid_copy(&domainsid, map->sid);
539 if (!sid_split_rid(&domainsid, &rid)) {
540 DEBUG(4, ("Could not determine domain SID from %s, "
541 "ignoring mapping request\n",
542 sid_string_dbg(map->sid)));
543 map->status = ID_UNMAPPED;
544 return NT_STATUS_NONE_MAPPED;
547 if (idmap_autorid_domsid_is_for_alloc(&domainsid)) {
548 DEBUG(10, ("SID %s is for ALLOC range.\n",
549 sid_string_dbg(map->sid)));
551 return idmap_autorid_sid_to_id_alloc(common, dom, map);
554 if (dom_sid_equal(&domainsid, &global_sid_Builtin) && ignore_builtin) {
555 DEBUG(10, ("Ignoring request for BUILTIN domain\n"));
556 map->status = ID_UNMAPPED;
557 return NT_STATUS_NONE_MAPPED;
561 * Check if the domain is around
563 domain = wcache_tdc_fetch_domainbysid(talloc_tos(),
564 &domainsid);
565 if (domain == NULL) {
566 DEBUG(10, ("Ignoring unknown domain sid %s\n",
567 sid_string_dbg(&domainsid)));
568 map->status = ID_UNMAPPED;
569 return NT_STATUS_NONE_MAPPED;
571 TALLOC_FREE(domain);
573 sid_to_fstring(range.domsid, &domainsid);
575 range.domain_range_index = rid / (global->rangesize);
577 ret = idmap_autorid_get_domainrange(autorid_db, &range, dom->read_only);
578 if (NT_STATUS_EQUAL(ret, NT_STATUS_NOT_FOUND) && dom->read_only) {
579 DEBUG(10, ("read-only is enabled, did not allocate "
580 "new range for domain %s\n",
581 sid_string_dbg(&domainsid)));
582 map->status = ID_UNMAPPED;
583 return NT_STATUS_NONE_MAPPED;
585 if (!NT_STATUS_IS_OK(ret)) {
586 DEBUG(3, ("Could not determine range for domain, "
587 "check previous messages for reason\n"));
588 return ret;
591 return idmap_autorid_sid_to_id_rid(global, &range, map);
594 /**********************************
595 lookup a set of sids.
596 **********************************/
598 static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
599 struct id_map **ids)
601 struct idmap_tdb_common_context *commoncfg;
602 NTSTATUS ret;
603 int i;
604 int num_tomap = 0;
605 int num_mapped = 0;
607 /* initialize the status to avoid surprise */
608 for (i = 0; ids[i]; i++) {
609 ids[i]->status = ID_UNKNOWN;
610 num_tomap++;
613 commoncfg =
614 talloc_get_type_abort(dom->private_data,
615 struct idmap_tdb_common_context);
617 for (i = 0; ids[i]; i++) {
618 ret = idmap_autorid_sid_to_id(commoncfg, dom, ids[i]);
619 if ((!NT_STATUS_IS_OK(ret)) &&
620 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
621 /* some fatal error occurred, log it */
622 DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
623 sid_string_dbg(ids[i]->sid)));
624 return ret;
627 if (NT_STATUS_IS_OK(ret) && ids[i]->status == ID_MAPPED) {
628 num_mapped++;
632 if (num_tomap == num_mapped) {
633 return NT_STATUS_OK;
634 } else if (num_mapped == 0) {
635 return NT_STATUS_NONE_MAPPED;
638 return STATUS_SOME_UNMAPPED;
641 static NTSTATUS idmap_autorid_preallocate_wellknown(struct idmap_domain *dom)
643 const char *groups[] = { "S-1-1-0", "S-1-2-0", "S-1-2-1",
644 "S-1-3-0", "S-1-3-1", "S-1-3-2", "S-1-3-3", "S-1-3-4",
645 "S-1-5-1", "S-1-5-2", "S-1-5-3", "S-1-5-4", "S-1-5-6",
646 "S-1-5-7", "S-1-5-8", "S-1-5-9", "S-1-5-10", "S-1-5-11",
647 "S-1-5-12", "S-1-5-13", "S-1-5-14", "S-1-5-15",
648 "S-1-5-17", "S-1-5-18", "S-1-5-19", "S-1-5-20"
651 struct id_map **maps;
652 int i, num;
653 NTSTATUS status;
655 if (dom->read_only) {
656 return NT_STATUS_OK;
659 num = ARRAY_SIZE(groups);
661 maps = talloc_array(talloc_tos(), struct id_map*, num+1);
662 if (!maps) {
663 return NT_STATUS_NO_MEMORY;
666 for (i = 0; i < num; i++) {
667 maps[i] = talloc(maps, struct id_map);
668 if (maps[i] == NULL) {
669 talloc_free(maps);
670 return NT_STATUS_NO_MEMORY;
672 maps[i]->xid.type = ID_TYPE_GID;
673 maps[i]->sid = dom_sid_parse_talloc(maps, groups[i]);
676 maps[num] = NULL;
678 status = idmap_autorid_sids_to_unixids(dom, maps);
680 DEBUG(10,("Preallocation run finished with status %s\n",
681 nt_errstr(status)));
683 talloc_free(maps);
685 return NT_STATUS_IS_OK(status)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
688 static NTSTATUS idmap_autorid_initialize_action(struct db_context *db,
689 void *private_data)
691 struct idmap_domain *dom;
692 struct idmap_tdb_common_context *common;
693 struct autorid_global_config *config;
694 NTSTATUS status;
696 dom = (struct idmap_domain *)private_data;
697 common = (struct idmap_tdb_common_context *)dom->private_data;
698 config = (struct autorid_global_config *)common->private_data;
700 status = idmap_autorid_init_hwms(db);
701 if (!NT_STATUS_IS_OK(status)) {
702 return status;
705 status = idmap_autorid_saveconfig(db, config);
706 if (!NT_STATUS_IS_OK(status)) {
707 DEBUG(1, ("Failed to store configuration data!\n"));
708 return status;
711 status = idmap_autorid_preallocate_wellknown(dom);
712 if (!NT_STATUS_IS_OK(status)) {
713 DEBUG(1, ("Failed to preallocate wellknown sids: %s\n",
714 nt_errstr(status)));
715 return status;
718 return NT_STATUS_OK;
721 static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
723 struct idmap_tdb_common_context *commonconfig;
724 struct autorid_global_config *config;
725 NTSTATUS status;
726 char *db_path;
728 if (!strequal(dom->name, "*")) {
729 DEBUG(0, ("idmap_autorid_initialize: Error: autorid configured "
730 "for domain '%s'. But autorid can only be used for "
731 "the default idmap configuration.\n", dom->name));
732 return NT_STATUS_INVALID_PARAMETER;
735 commonconfig = talloc_zero(dom, struct idmap_tdb_common_context);
736 if (!commonconfig) {
737 DEBUG(0, ("Out of memory!\n"));
738 return NT_STATUS_NO_MEMORY;
740 dom->private_data = commonconfig;
742 commonconfig->rw_ops = talloc_zero(commonconfig, struct idmap_rw_ops);
743 if (commonconfig->rw_ops == NULL) {
744 DEBUG(0, ("Out of memory!\n"));
745 return NT_STATUS_NO_MEMORY;
748 config = talloc_zero(commonconfig, struct autorid_global_config);
749 if (!config) {
750 DEBUG(0, ("Out of memory!\n"));
751 return NT_STATUS_NO_MEMORY;
753 commonconfig->private_data = config;
755 config->minvalue = dom->low_id;
756 config->rangesize = lp_parm_int(-1, "idmap config *",
757 "rangesize", 100000);
759 config->maxranges = (dom->high_id - dom->low_id + 1) /
760 config->rangesize;
762 if (config->maxranges == 0) {
763 DEBUG(1, ("Allowed uid range is smaller than rangesize. "
764 "Increase uid range or decrease rangesize.\n"));
765 status = NT_STATUS_INVALID_PARAMETER;
766 goto error;
769 /* check if the high-low limit is a multiple of the rangesize */
770 if ((dom->high_id - dom->low_id + 1) % config->rangesize != 0) {
771 DEBUG(5, ("High uid-low uid difference of %d "
772 "is not a multiple of the rangesize %d, "
773 "limiting ranges to lower boundary number of %d\n",
774 (dom->high_id - dom->low_id + 1), config->rangesize,
775 config->maxranges));
778 DEBUG(5, ("%d domain ranges with a size of %d are available\n",
779 config->maxranges, config->rangesize));
781 ignore_builtin = lp_parm_bool(-1, "idmap config *",
782 "ignore builtin", false);
784 /* fill the TDB common configuration */
786 commonconfig->max_id = config->rangesize - 1
787 - IDMAP_AUTORID_ALLOC_RESERVED;
788 commonconfig->hwmkey_uid = ALLOC_HWM_UID;
789 commonconfig->hwmkey_gid = ALLOC_HWM_GID;
790 commonconfig->rw_ops->get_new_id = idmap_autorid_allocate_id;
791 commonconfig->rw_ops->set_mapping = idmap_tdb_common_set_mapping;
793 db_path = state_path("autorid.tdb");
794 if (db_path == NULL) {
795 status = NT_STATUS_NO_MEMORY;
796 goto error;
799 status = idmap_autorid_db_open(db_path,
800 NULL, /* TALLOC_CTX */
801 &autorid_db);
802 TALLOC_FREE(db_path);
803 if (!NT_STATUS_IS_OK(status)) {
804 goto error;
807 commonconfig->db = autorid_db;
809 status = dbwrap_trans_do(autorid_db,
810 idmap_autorid_initialize_action,
811 dom);
812 if (!NT_STATUS_IS_OK(status)) {
813 DEBUG(1, ("Failed to init the idmap database: %s\n",
814 nt_errstr(status)));
815 goto error;
818 goto done;
820 error:
821 talloc_free(config);
823 done:
824 return status;
828 Close the idmap tdb instance
830 static struct idmap_methods autorid_methods = {
831 .init = idmap_autorid_initialize,
832 .unixids_to_sids = idmap_autorid_unixids_to_sids,
833 .sids_to_unixids = idmap_autorid_sids_to_unixids,
834 .allocate_id = idmap_autorid_allocate_id
837 static_decl_idmap;
838 NTSTATUS idmap_autorid_init(void)
840 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
841 "autorid", &autorid_methods);