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
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
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
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"
80 #include "../libcli/security/dom_sid.h"
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
)
99 fstrcpy(range
->domsid
, ALLOC_RANGE
);
101 status
= idmap_autorid_get_domainrange(autorid_db
,
108 static NTSTATUS
idmap_autorid_allocate_id(struct idmap_domain
*dom
,
109 struct unixid
*xid
) {
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"));
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"));
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
));
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
,
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
;
160 map
->status
= ID_UNKNOWN
;
162 DEBUG(10, ("no ID->SID mapping for %d could be found\n", map
->xid
.id
));
167 static NTSTATUS
idmap_autorid_id_to_sid(struct autorid_global_config
*cfg
,
168 struct idmap_domain
*dom
,
171 uint32_t range_number
;
172 uint32_t domain_range_index
= 0;
173 uint32_t normalized_id
;
174 uint32_t reduced_rid
;
176 TDB_DATA data
= tdb_null
;
178 struct dom_sid domsid
;
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
;
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
;
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
);
205 return NT_STATUS_NO_MEMORY
;
208 status
= dbwrap_fetch_bystring(autorid_db
, talloc_tos(), keystr
, &data
);
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
;
220 if (strncmp((const char *)data
.dptr
,
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",
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
);
236 map
->status
= ID_UNKNOWN
;
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
;
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
;
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
,
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
;
285 /**********************************
286 lookup a set of unix ids.
287 **********************************/
289 static NTSTATUS
idmap_autorid_unixids_to_sids(struct idmap_domain
*dom
,
292 struct idmap_tdb_common_context
*commoncfg
;
293 struct autorid_global_config
*globalcfg
;
299 /* initialize the status to avoid surprise */
300 for (i
= 0; ids
[i
]; i
++) {
301 ids
[i
]->status
= ID_UNKNOWN
;
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
));
324 if (NT_STATUS_IS_OK(ret
) && ids
[i
]->status
== ID_MAPPED
) {
330 if (num_tomap
== num_mapped
) {
332 } else if (num_mapped
== 0) {
333 return NT_STATUS_NONE_MAPPED
;
336 return STATUS_SOME_UNMAPPED
;
343 static bool idmap_autorid_sid_is_special(struct dom_sid
*sid
)
347 match
= sid_check_is_in_wellknown_domain(sid
);
355 static NTSTATUS
idmap_autorid_sid_to_id_special(struct idmap_domain
*dom
,
358 struct idmap_tdb_common_context
*common
=
359 talloc_get_type_abort(dom
->private_data
,
360 struct idmap_tdb_common_context
);
362 struct autorid_range_config range
;
366 status
= idmap_autorid_get_alloc_range(dom
, &range
);
367 if (!NT_STATUS_IS_OK(status
)) {
371 /* Take the next free ID, counting from the top */
373 for (count
= 0; count
< IDMAP_AUTORID_ALLOC_RESERVED
; count
++) {
374 struct id_map test_map
;
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
;
388 if (!NT_STATUS_IS_OK(status
)) {
389 /* error - get out */
393 /* mapping exists - try next ID */
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",
413 struct idmap_autorid_sid_to_id_alloc_ctx
{
414 struct idmap_domain
*dom
;
418 static NTSTATUS
idmap_autorid_sid_to_id_alloc_action(
419 struct db_context
*db
,
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
)) {
429 ret
= idmap_autorid_sid_to_id_special(ctx
->dom
, ctx
->map
);
430 if (NT_STATUS_IS_OK(ret
)) {
433 if (!NT_STATUS_EQUAL(NT_STATUS_NONE_MAPPED
, 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
,
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
;
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
)));
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
)));
486 ret
= dbwrap_trans_do(ctx
->db
, idmap_autorid_sid_to_id_alloc_action
,
488 if (!NT_STATUS_IS_OK(ret
)) {
489 DEBUG(1, ("Failed to create a new mapping in alloc range: %s\n",
491 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
494 map
->status
= ID_MAPPED
;
498 static bool idmap_autorid_domsid_is_for_alloc(struct dom_sid
*sid
)
502 match
= sid_check_is_wellknown_domain(sid
, NULL
);
510 static NTSTATUS
idmap_autorid_sid_to_id(struct idmap_tdb_common_context
*common
,
511 struct idmap_domain
*dom
,
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
;
520 struct dom_sid domainsid
;
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(),
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
;
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"));
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
,
591 struct idmap_tdb_common_context
*commoncfg
;
597 /* initialize the status to avoid surprise */
598 for (i
= 0; ids
[i
]; i
++) {
599 ids
[i
]->status
= ID_UNKNOWN
;
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
)));
617 if (NT_STATUS_IS_OK(ret
) && ids
[i
]->status
== ID_MAPPED
) {
622 if (num_tomap
== num_mapped
) {
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
;
645 if (dom
->read_only
) {
649 num
= ARRAY_SIZE(groups
);
651 maps
= talloc_array(talloc_tos(), struct id_map
*, num
+1);
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
) {
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
]);
668 status
= idmap_autorid_sids_to_unixids(dom
, maps
);
670 DEBUG(10,("Preallocation run finished with status %s\n",
675 return NT_STATUS_IS_OK(status
)?NT_STATUS_OK
:NT_STATUS_UNSUCCESSFUL
;
678 static NTSTATUS
idmap_autorid_initialize_action(struct db_context
*db
,
681 struct idmap_domain
*dom
;
682 struct idmap_tdb_common_context
*common
;
683 struct autorid_global_config
*config
;
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
)) {
695 status
= idmap_autorid_saveconfig(db
, config
);
696 if (!NT_STATUS_IS_OK(status
)) {
697 DEBUG(1, ("Failed to store configuration data!\n"));
701 status
= idmap_autorid_preallocate_wellknown(dom
);
702 if (!NT_STATUS_IS_OK(status
)) {
703 DEBUG(1, ("Failed to preallocate wellknown sids: %s\n",
711 static NTSTATUS
idmap_autorid_initialize(struct idmap_domain
*dom
)
713 struct idmap_tdb_common_context
*commonconfig
;
714 struct autorid_global_config
*config
;
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
);
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
);
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) /
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
;
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
,
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
;
789 status
= idmap_autorid_db_open(db_path
,
790 NULL
, /* TALLOC_CTX */
792 TALLOC_FREE(db_path
);
793 if (!NT_STATUS_IS_OK(status
)) {
797 commonconfig
->db
= autorid_db
;
799 status
= dbwrap_trans_do(autorid_db
,
800 idmap_autorid_initialize_action
,
802 if (!NT_STATUS_IS_OK(status
)) {
803 DEBUG(1, ("Failed to init the idmap database: %s\n",
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
);