idmap_autorid: change idmap_autorid_loadconfig() to return NTSTATUS
[Samba.git] / source3 / winbindd / idmap_autorid.c
blob60ce33c35f738a5f3a52374c4c19aa783718d122
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 /* handle to the tdb storing domain <-> range assignments */
86 static struct db_context *autorid_db;
88 static bool ignore_builtin = false;
90 static NTSTATUS idmap_autorid_allocate_id(struct idmap_domain *dom,
91 struct unixid *xid) {
93 NTSTATUS ret;
94 struct idmap_tdb_common_context *commoncfg;
95 struct autorid_global_config *globalcfg;
96 struct autorid_range_config range;
98 commoncfg =
99 talloc_get_type_abort(dom->private_data,
100 struct idmap_tdb_common_context);
102 globalcfg = talloc_get_type(commoncfg->private_data,
103 struct autorid_global_config);
105 if (dom->read_only) {
106 DEBUG(3, ("Backend is read-only, refusing "
107 "new allocation request\n"));
108 return NT_STATUS_UNSUCCESSFUL;
111 /* fetch the range for the allocation pool */
113 ZERO_STRUCT(range);
115 range.globalcfg = globalcfg;
116 fstrcpy(range.domsid, ALLOC_RANGE);
118 ret = idmap_autorid_get_domainrange(autorid_db, &range, dom->read_only);
120 if (!NT_STATUS_IS_OK(ret)) {
121 DEBUG(3, ("Could not determine range for allocation pool, "
122 "check previous messages for reason\n"));
123 return ret;
126 ret = idmap_tdb_common_get_new_id(dom, xid);
128 if (!NT_STATUS_IS_OK(ret)) {
129 DEBUG(1, ("Fatal error while allocating new ID!\n"));
130 return ret;
133 xid->id = xid->id + range.low_id;
135 DEBUG(10, ("Returned new %s %d from allocation range\n",
136 (xid->type==ID_TYPE_UID)?"uid":"gid", xid->id));
138 return ret;
142 * map a SID to xid using the idmap_tdb like pool
144 static NTSTATUS idmap_autorid_map_id_to_sid(struct idmap_domain *dom,
145 struct id_map *map)
147 NTSTATUS ret;
149 /* look out for the mapping */
150 ret = idmap_tdb_common_unixid_to_sid(dom, map);
152 if (NT_STATUS_IS_OK(ret)) {
153 map->status = ID_MAPPED;
154 return ret;
157 map->status = ID_UNKNOWN;
159 DEBUG(10, ("no ID->SID mapping for %d could be found\n", map->xid.id));
161 return ret;
164 static NTSTATUS idmap_autorid_id_to_sid(struct autorid_global_config *cfg,
165 struct idmap_domain *dom,
166 struct id_map *map)
168 uint32_t range_number;
169 uint32_t domain_range_index = 0;
170 uint32_t normalized_id;
171 uint32_t reduced_rid;
172 uint32_t rid;
173 TDB_DATA data = tdb_null;
174 char *keystr;
175 struct dom_sid domsid;
176 NTSTATUS status;
177 bool ok;
178 const char *q = NULL;
180 /* can this be one of our ids? */
181 if (map->xid.id < cfg->minvalue) {
182 DEBUG(10, ("id %d is lower than minimum value, "
183 "ignoring mapping request\n", map->xid.id));
184 map->status = ID_UNKNOWN;
185 return NT_STATUS_OK;
188 if (map->xid.id > (cfg->minvalue + cfg->rangesize * cfg->maxranges)) {
189 DEBUG(10, ("id %d is outside of maximum id value, "
190 "ignoring mapping request\n", map->xid.id));
191 map->status = ID_UNKNOWN;
192 return NT_STATUS_OK;
195 /* determine the range of this uid */
197 normalized_id = map->xid.id - cfg->minvalue;
198 range_number = normalized_id / cfg->rangesize;
200 keystr = talloc_asprintf(talloc_tos(), "%u", range_number);
201 if (!keystr) {
202 return NT_STATUS_NO_MEMORY;
205 status = dbwrap_fetch_bystring(autorid_db, talloc_tos(), keystr, &data);
206 TALLOC_FREE(keystr);
208 if (!NT_STATUS_IS_OK(status)) {
209 DEBUG(4, ("id %d belongs to range %d which does not have "
210 "domain mapping, ignoring mapping request\n",
211 map->xid.id, range_number));
212 TALLOC_FREE(data.dptr);
213 map->status = ID_UNKNOWN;
214 return NT_STATUS_OK;
217 if (strncmp((const char *)data.dptr,
218 ALLOC_RANGE,
219 strlen(ALLOC_RANGE)) == 0) {
221 * this is from the alloc range, check if there is a mapping
223 DEBUG(5, ("id %d belongs to allocation range, "
224 "checking for mapping\n",
225 map->xid.id));
226 TALLOC_FREE(data.dptr);
227 return idmap_autorid_map_id_to_sid(dom, map);
230 ok = dom_sid_parse_endp((const char *)data.dptr, &domsid, &q);
231 TALLOC_FREE(data.dptr);
232 if (!ok) {
233 map->status = ID_UNKNOWN;
234 return NT_STATUS_OK;
236 if (q != NULL)
237 if (sscanf(q+1, "%"SCNu32, &domain_range_index) != 1) {
238 DEBUG(10, ("Domain range index not found, "
239 "ignoring mapping request\n"));
240 map->status = ID_UNKNOWN;
241 return NT_STATUS_OK;
244 reduced_rid = normalized_id % cfg->rangesize;
245 rid = reduced_rid + domain_range_index * cfg->rangesize;
247 sid_compose(map->sid, &domsid, rid);
249 /* We **really** should have some way of validating
250 the SID exists and is the correct type here. But
251 that is a deficiency in the idmap_rid design. */
253 map->status = ID_MAPPED;
254 map->xid.type = ID_TYPE_BOTH;
256 return NT_STATUS_OK;
259 /**********************************
260 Single sid to id lookup function.
261 **********************************/
263 static NTSTATUS idmap_autorid_sid_to_id(struct autorid_global_config *global,
264 struct autorid_range_config *range,
265 struct id_map *map)
267 uint32_t rid;
268 uint32_t reduced_rid;
270 sid_peek_rid(map->sid, &rid);
272 reduced_rid = rid % global->rangesize;
274 map->xid.id = reduced_rid + range->low_id;
275 map->xid.type = ID_TYPE_BOTH;
277 /* We **really** should have some way of validating
278 the SID exists and is the correct type here. But
279 that is a deficiency in the idmap_rid design. */
281 map->status = ID_MAPPED;
283 return NT_STATUS_OK;
286 /**********************************
287 lookup a set of unix ids.
288 **********************************/
290 static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
291 struct id_map **ids)
293 struct idmap_tdb_common_context *commoncfg;
294 struct autorid_global_config *globalcfg;
295 NTSTATUS ret;
296 int i;
297 int num_tomap = 0;
298 int num_mapped = 0;
300 /* initialize the status to avoid surprise */
301 for (i = 0; ids[i]; i++) {
302 ids[i]->status = ID_UNKNOWN;
303 num_tomap++;
306 commoncfg =
307 talloc_get_type_abort(dom->private_data,
308 struct idmap_tdb_common_context);
310 globalcfg = talloc_get_type(commoncfg->private_data,
311 struct autorid_global_config);
313 for (i = 0; ids[i]; i++) {
315 ret = idmap_autorid_id_to_sid(globalcfg, dom, ids[i]);
317 if ((!NT_STATUS_IS_OK(ret)) &&
318 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
319 /* some fatal error occurred, log it */
320 DEBUG(3, ("Unexpected error resolving an ID "
321 " (%d)\n", ids[i]->xid.id));
322 goto failure;
325 if (NT_STATUS_IS_OK(ret) && ids[i]->status == ID_MAPPED) {
326 num_mapped++;
331 if (num_tomap == num_mapped) {
332 return NT_STATUS_OK;
333 } else if (num_mapped == 0) {
334 return NT_STATUS_NONE_MAPPED;
337 return STATUS_SOME_UNMAPPED;
340 failure:
341 return ret;
345 * map a SID to xid using the idmap_tdb like pool
347 static NTSTATUS idmap_autorid_map_sid_to_id(struct idmap_domain *dom,
348 struct id_map *map,
349 struct idmap_tdb_common_context *ctx)
351 NTSTATUS ret;
352 int res;
354 /* see if we already have a mapping */
355 ret = idmap_tdb_common_sid_to_unixid(dom, map);
357 if (NT_STATUS_IS_OK(ret)) {
358 map->status = ID_MAPPED;
359 return ret;
362 /* bad things happened */
363 if (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
364 DEBUG(1, ("Looking up SID->ID mapping for %s failed\n",
365 sid_string_dbg(map->sid)));
366 return ret;
369 if (dom->read_only) {
370 DEBUG(3, ("Not allocating new mapping for %s, because backend "
371 "is read-only\n", sid_string_dbg(map->sid)));
372 return NT_STATUS_NONE_MAPPED;
375 DEBUG(10, ("Creating new mapping in pool for %s\n",
376 sid_string_dbg(map->sid)));
378 /* create new mapping */
379 res = dbwrap_transaction_start(ctx->db);
380 if (res != 0) {
381 DEBUG(2, ("transaction_start failed\n"));
382 return NT_STATUS_INTERNAL_DB_CORRUPTION;
385 ret = idmap_tdb_common_new_mapping(dom, map);
387 map->status = (NT_STATUS_IS_OK(ret))?ID_MAPPED:ID_UNMAPPED;
389 if (!NT_STATUS_IS_OK(ret)) {
390 if (dbwrap_transaction_cancel(ctx->db) != 0) {
391 smb_panic("Cancelling transaction failed");
393 return ret;
396 res = dbwrap_transaction_commit(ctx->db);
397 if (res == 0) {
398 return ret;
401 DEBUG(2, ("transaction_commit failed\n"));
402 return NT_STATUS_INTERNAL_DB_CORRUPTION;
406 /**********************************
407 lookup a set of sids.
408 **********************************/
410 static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
411 struct id_map **ids)
413 struct idmap_tdb_common_context *commoncfg;
414 struct autorid_global_config *global;
415 NTSTATUS ret;
416 int i;
417 int num_tomap = 0;
418 int num_mapped = 0;
420 /* initialize the status to avoid surprise */
421 for (i = 0; ids[i]; i++) {
422 ids[i]->status = ID_UNKNOWN;
423 num_tomap++;
426 commoncfg =
427 talloc_get_type_abort(dom->private_data,
428 struct idmap_tdb_common_context);
430 global = talloc_get_type(commoncfg->private_data,
431 struct autorid_global_config);
433 for (i = 0; ids[i]; i++) {
434 struct winbindd_tdc_domain *domain;
435 struct autorid_range_config range;
436 uint32_t rid;
437 struct dom_sid domainsid;
439 ZERO_STRUCT(range);
441 DEBUG(10, ("Trying to map %s\n", sid_string_dbg(ids[i]->sid)));
443 sid_copy(&domainsid, ids[i]->sid);
444 if (!sid_split_rid(&domainsid, &rid)) {
445 DEBUG(4, ("Could not determine domain SID from %s, "
446 "ignoring mapping request\n",
447 sid_string_dbg(ids[i]->sid)));
448 continue;
451 /* is this a well-known SID? */
453 if (sid_check_is_wellknown_domain(&domainsid, NULL)) {
455 DEBUG(10, ("SID %s is well-known, using pool\n",
456 sid_string_dbg(ids[i]->sid)));
458 ret = idmap_autorid_map_sid_to_id(dom, ids[i],
459 commoncfg);
461 if (!NT_STATUS_IS_OK(ret) &&
462 !NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
463 DEBUG(3, ("Unexpected error resolving "
464 "SID (%s)\n",
465 sid_string_dbg(ids[i]->sid)));
466 goto failure;
469 if (ids[i]->status == ID_MAPPED) {
470 num_mapped++;
473 continue;
476 /* BUILTIN is passdb's job */
477 if (dom_sid_equal(&domainsid, &global_sid_Builtin) &&
478 ignore_builtin) {
479 DEBUG(10, ("Ignoring request for BUILTIN domain\n"));
480 continue;
484 * Check if the domain is around
486 domain = wcache_tdc_fetch_domainbysid(talloc_tos(),
487 &domainsid);
488 if (domain == NULL) {
489 DEBUG(10, ("Ignoring unknown domain sid %s\n",
490 sid_string_dbg(&domainsid)));
491 continue;
493 TALLOC_FREE(domain);
495 range.globalcfg = global;
496 sid_to_fstring(range.domsid, &domainsid);
498 /* Calculate domain_range_index for multi-range support */
499 range.domain_range_index = rid / (global->rangesize);
501 ret = idmap_autorid_get_domainrange(autorid_db, &range,
502 dom->read_only);
504 /* read-only mode and a new domain range would be required? */
505 if (NT_STATUS_EQUAL(ret, NT_STATUS_NOT_FOUND) &&
506 dom->read_only) {
507 DEBUG(10, ("read-only is enabled, did not allocate "
508 "new range for domain %s\n",
509 sid_string_dbg(&domainsid)));
510 continue;
513 if (!NT_STATUS_IS_OK(ret)) {
514 DEBUG(3, ("Could not determine range for domain, "
515 "check previous messages for reason\n"));
516 goto failure;
519 ret = idmap_autorid_sid_to_id(global, &range, ids[i]);
521 if ((!NT_STATUS_IS_OK(ret)) &&
522 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
523 /* some fatal error occurred, log it */
524 DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
525 sid_string_dbg(ids[i]->sid)));
526 goto failure;
529 if (NT_STATUS_IS_OK(ret)) {
530 num_mapped++;
534 if (num_tomap == num_mapped) {
535 return NT_STATUS_OK;
536 } else if (num_mapped == 0) {
537 return NT_STATUS_NONE_MAPPED;
540 return STATUS_SOME_UNMAPPED;
542 failure:
543 return ret;
547 static NTSTATUS idmap_autorid_preallocate_wellknown(struct idmap_domain *dom)
549 const char *groups[] = { "S-1-1-0", "S-1-2-0", "S-1-2-1",
550 "S-1-3-0", "S-1-3-1", "S-1-3-2", "S-1-3-3", "S-1-3-4",
551 "S-1-5-1", "S-1-5-2", "S-1-5-3", "S-1-5-4", "S-1-5-6",
552 "S-1-5-7", "S-1-5-8", "S-1-5-9", "S-1-5-10", "S-1-5-11",
553 "S-1-5-12", "S-1-5-13", "S-1-5-14", "S-1-5-15",
554 "S-1-5-17", "S-1-5-18", "S-1-5-19", "S-1-5-20"
557 struct id_map **maps;
558 int i, num;
559 NTSTATUS status;
561 if (dom->read_only) {
562 return NT_STATUS_OK;
565 num = ARRAY_SIZE(groups);
567 maps = talloc_array(talloc_tos(), struct id_map*, num+1);
568 if (!maps) {
569 return NT_STATUS_NO_MEMORY;
572 for (i = 0; i < num; i++) {
573 maps[i] = talloc(maps, struct id_map);
574 if (maps[i] == NULL) {
575 talloc_free(maps);
576 return NT_STATUS_NO_MEMORY;
578 maps[i]->xid.type = ID_TYPE_GID;
579 maps[i]->sid = dom_sid_parse_talloc(maps, groups[i]);
582 maps[num] = NULL;
584 status = idmap_autorid_sids_to_unixids(dom, maps);
586 DEBUG(10,("Preallocation run finished with status %s\n",
587 nt_errstr(status)));
589 talloc_free(maps);
591 return NT_STATUS_IS_OK(status)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
594 static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
596 struct idmap_tdb_common_context *commonconfig;
597 struct autorid_global_config *config;
598 struct autorid_global_config *storedconfig = NULL;
599 NTSTATUS status;
600 uint32_t hwm;
602 if (!strequal(dom->name, "*")) {
603 DEBUG(0, ("idmap_autorid_initialize: Error: autorid configured "
604 "for domain '%s'. But autorid can only be used for "
605 "the default idmap configuration.\n", dom->name));
606 return NT_STATUS_INVALID_PARAMETER;
609 commonconfig = talloc_zero(dom, struct idmap_tdb_common_context);
610 if (!commonconfig) {
611 DEBUG(0, ("Out of memory!\n"));
612 return NT_STATUS_NO_MEMORY;
615 commonconfig->rw_ops = talloc_zero(commonconfig, struct idmap_rw_ops);
616 if (commonconfig->rw_ops == NULL) {
617 DEBUG(0, ("Out of memory!\n"));
618 return NT_STATUS_NO_MEMORY;
621 config = talloc_zero(commonconfig, struct autorid_global_config);
622 if (!config) {
623 DEBUG(0, ("Out of memory!\n"));
624 return NT_STATUS_NO_MEMORY;
627 status = idmap_autorid_db_init(state_path("autorid.tdb"),
628 NULL, /* TALLOC_CTX */
629 &autorid_db);
630 if (!NT_STATUS_IS_OK(status)) {
631 goto error;
634 config->minvalue = dom->low_id;
635 config->rangesize = lp_parm_int(-1, "idmap config *",
636 "rangesize", 100000);
638 if (config->rangesize < 2000) {
639 DEBUG(1, ("autorid rangesize must be at least 2000\n"));
640 status = NT_STATUS_INVALID_PARAMETER;
641 goto error;
644 config->maxranges = (dom->high_id - dom->low_id + 1) /
645 config->rangesize;
647 if (config->maxranges == 0) {
648 DEBUG(1, ("allowed uid range is smaller then rangesize, "
649 "increase uid range or decrease rangesize\n"));
650 status = NT_STATUS_INVALID_PARAMETER;
651 goto error;
654 /* check if the high-low limit is a multiple of the rangesize */
655 if ((dom->high_id - dom->low_id + 1) % config->rangesize != 0) {
656 DEBUG(5, ("High uid-low uid difference of %d "
657 "is not a multiple of the rangesize %d, "
658 "limiting ranges to lower boundary number of %d\n",
659 (dom->high_id - dom->low_id + 1), config->rangesize,
660 config->maxranges));
663 DEBUG(10, ("Current configuration in config is "
664 "minvalue:%d rangesize:%d maxranges:%d\n",
665 config->minvalue, config->rangesize, config->maxranges));
667 /* read previously stored config and current HWM */
668 status = idmap_autorid_loadconfig(autorid_db, talloc_tos(),
669 &storedconfig);
670 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
671 DEBUG(5, ("No configuration found. Storing initial "
672 "configuration.\n"));
673 } else if (!NT_STATUS_IS_OK(status)) {
674 goto error;
677 status = dbwrap_fetch_uint32_bystring(autorid_db, HWM, &hwm);
678 if (!NT_STATUS_IS_OK(status)) {
679 DEBUG(1, ("Fatal error while fetching current "
680 "HWM value: %s\n", nt_errstr(status)));
681 status = NT_STATUS_INTERNAL_ERROR;
682 goto error;
685 /* did the minimum value or rangesize change? */
686 if (storedconfig &&
687 ((storedconfig->minvalue != config->minvalue) ||
688 (storedconfig->rangesize != config->rangesize))) {
689 DEBUG(1, ("New configuration values for rangesize or "
690 "minimum uid value conflict with previously "
691 "used values! Aborting initialization\n"));
692 status = NT_STATUS_INVALID_PARAMETER;
693 goto error;
697 * has the highest uid value been reduced to setting that is not
698 * sufficient any more for already existing ranges?
700 if (hwm > config->maxranges) {
701 DEBUG(1, ("New upper uid limit is too low to cover "
702 "existing mappings! Aborting initialization\n"));
703 status = NT_STATUS_INVALID_PARAMETER;
704 goto error;
707 status = idmap_autorid_saveconfig(autorid_db, config);
709 if (!NT_STATUS_IS_OK(status)) {
710 DEBUG(1, ("Failed to store configuration data!\n"));
711 goto error;
714 DEBUG(5, ("%d domain ranges with a size of %d are available\n",
715 config->maxranges, config->rangesize));
717 ignore_builtin = lp_parm_bool(-1, "idmap config *",
718 "ignore builtin", false);
720 /* fill the TDB common configuration */
721 commonconfig->private_data = config;
723 commonconfig->db = autorid_db;
724 commonconfig->max_id = config->rangesize -1;
725 commonconfig->hwmkey_uid = ALLOC_HWM_UID;
726 commonconfig->hwmkey_gid = ALLOC_HWM_GID;
727 commonconfig->rw_ops->get_new_id = idmap_autorid_allocate_id;
728 commonconfig->rw_ops->set_mapping = idmap_tdb_common_set_mapping;
730 dom->private_data = commonconfig;
732 /* preallocate well-known SIDs in the pool */
733 status = idmap_autorid_preallocate_wellknown(dom);
735 goto done;
737 error:
738 talloc_free(config);
740 done:
741 talloc_free(storedconfig);
743 return status;
747 Close the idmap tdb instance
749 static struct idmap_methods autorid_methods = {
750 .init = idmap_autorid_initialize,
751 .unixids_to_sids = idmap_autorid_unixids_to_sids,
752 .sids_to_unixids = idmap_autorid_sids_to_unixids,
753 .allocate_id = idmap_autorid_allocate_id
756 NTSTATUS samba_init_module(void)
758 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
759 "autorid", &autorid_methods);