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
;
344 * map a SID to xid using the idmap_tdb like pool
346 static NTSTATUS
idmap_autorid_sid_to_id_alloc(
347 struct idmap_tdb_common_context
*ctx
,
348 struct idmap_domain
*dom
,
354 map
->status
= ID_UNKNOWN
;
356 /* see if we already have a mapping */
357 ret
= idmap_tdb_common_sid_to_unixid(dom
, map
);
359 if (NT_STATUS_IS_OK(ret
)) {
360 map
->status
= ID_MAPPED
;
364 /* bad things happened */
365 if (!NT_STATUS_EQUAL(ret
, NT_STATUS_NONE_MAPPED
)) {
366 DEBUG(1, ("Looking up SID->ID mapping for %s failed: %s\n",
367 sid_string_dbg(map
->sid
), nt_errstr(ret
)));
371 if (dom
->read_only
) {
372 DEBUG(3, ("Not allocating new mapping for %s, because backend "
373 "is read-only\n", sid_string_dbg(map
->sid
)));
374 map
->status
= ID_UNMAPPED
;
375 return NT_STATUS_NONE_MAPPED
;
378 DEBUG(10, ("Creating new mapping in pool for %s\n",
379 sid_string_dbg(map
->sid
)));
381 /* create new mapping */
382 res
= dbwrap_transaction_start(ctx
->db
);
384 DEBUG(2, ("transaction_start failed\n"));
385 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
388 ret
= idmap_tdb_common_new_mapping(dom
, map
);
389 if (!NT_STATUS_IS_OK(ret
)) {
390 if (dbwrap_transaction_cancel(ctx
->db
) != 0) {
391 smb_panic("Cancelling transaction failed");
393 map
->status
= ID_UNMAPPED
;
397 res
= dbwrap_transaction_commit(ctx
->db
);
399 map
->status
= ID_MAPPED
;
403 DEBUG(2, ("transaction_commit failed\n"));
404 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
408 static bool idmap_autorid_domsid_is_for_alloc(struct dom_sid
*sid
)
412 match
= sid_check_is_wellknown_domain(sid
, NULL
);
420 static NTSTATUS
idmap_autorid_sid_to_id(struct idmap_tdb_common_context
*common
,
421 struct idmap_domain
*dom
,
424 struct autorid_global_config
*global
=
425 talloc_get_type_abort(common
->private_data
,
426 struct autorid_global_config
);
427 struct winbindd_tdc_domain
*domain
;
428 struct autorid_range_config range
;
430 struct dom_sid domainsid
;
434 map
->status
= ID_UNKNOWN
;
436 DEBUG(10, ("Trying to map %s\n", sid_string_dbg(map
->sid
)));
438 sid_copy(&domainsid
, map
->sid
);
439 if (!sid_split_rid(&domainsid
, &rid
)) {
440 DEBUG(4, ("Could not determine domain SID from %s, "
441 "ignoring mapping request\n",
442 sid_string_dbg(map
->sid
)));
443 map
->status
= ID_UNMAPPED
;
444 return NT_STATUS_NONE_MAPPED
;
447 if (idmap_autorid_domsid_is_for_alloc(&domainsid
)) {
448 DEBUG(10, ("SID %s is for ALLOC range.\n",
449 sid_string_dbg(map
->sid
)));
451 return idmap_autorid_sid_to_id_alloc(common
, dom
, map
);
454 if (dom_sid_equal(&domainsid
, &global_sid_Builtin
) && ignore_builtin
) {
455 DEBUG(10, ("Ignoring request for BUILTIN domain\n"));
456 map
->status
= ID_UNMAPPED
;
457 return NT_STATUS_NONE_MAPPED
;
461 * Check if the domain is around
463 domain
= wcache_tdc_fetch_domainbysid(talloc_tos(),
465 if (domain
== NULL
) {
466 DEBUG(10, ("Ignoring unknown domain sid %s\n",
467 sid_string_dbg(&domainsid
)));
468 map
->status
= ID_UNMAPPED
;
469 return NT_STATUS_NONE_MAPPED
;
473 sid_to_fstring(range
.domsid
, &domainsid
);
475 range
.domain_range_index
= rid
/ (global
->rangesize
);
477 ret
= idmap_autorid_get_domainrange(autorid_db
, &range
, dom
->read_only
);
478 if (NT_STATUS_EQUAL(ret
, NT_STATUS_NOT_FOUND
) && dom
->read_only
) {
479 DEBUG(10, ("read-only is enabled, did not allocate "
480 "new range for domain %s\n",
481 sid_string_dbg(&domainsid
)));
482 map
->status
= ID_UNMAPPED
;
483 return NT_STATUS_NONE_MAPPED
;
485 if (!NT_STATUS_IS_OK(ret
)) {
486 DEBUG(3, ("Could not determine range for domain, "
487 "check previous messages for reason\n"));
491 return idmap_autorid_sid_to_id_rid(global
, &range
, map
);
494 /**********************************
495 lookup a set of sids.
496 **********************************/
498 static NTSTATUS
idmap_autorid_sids_to_unixids(struct idmap_domain
*dom
,
501 struct idmap_tdb_common_context
*commoncfg
;
507 /* initialize the status to avoid surprise */
508 for (i
= 0; ids
[i
]; i
++) {
509 ids
[i
]->status
= ID_UNKNOWN
;
514 talloc_get_type_abort(dom
->private_data
,
515 struct idmap_tdb_common_context
);
517 for (i
= 0; ids
[i
]; i
++) {
518 ret
= idmap_autorid_sid_to_id(commoncfg
, dom
, ids
[i
]);
519 if ((!NT_STATUS_IS_OK(ret
)) &&
520 (!NT_STATUS_EQUAL(ret
, NT_STATUS_NONE_MAPPED
))) {
521 /* some fatal error occurred, log it */
522 DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
523 sid_string_dbg(ids
[i
]->sid
)));
527 if (NT_STATUS_IS_OK(ret
) && ids
[i
]->status
== ID_MAPPED
) {
532 if (num_tomap
== num_mapped
) {
534 } else if (num_mapped
== 0) {
535 return NT_STATUS_NONE_MAPPED
;
538 return STATUS_SOME_UNMAPPED
;
541 static NTSTATUS
idmap_autorid_preallocate_wellknown(struct idmap_domain
*dom
)
543 const char *groups
[] = { "S-1-1-0", "S-1-2-0", "S-1-2-1",
544 "S-1-3-0", "S-1-3-1", "S-1-3-2", "S-1-3-3", "S-1-3-4",
545 "S-1-5-1", "S-1-5-2", "S-1-5-3", "S-1-5-4", "S-1-5-6",
546 "S-1-5-7", "S-1-5-8", "S-1-5-9", "S-1-5-10", "S-1-5-11",
547 "S-1-5-12", "S-1-5-13", "S-1-5-14", "S-1-5-15",
548 "S-1-5-17", "S-1-5-18", "S-1-5-19", "S-1-5-20"
551 struct id_map
**maps
;
555 if (dom
->read_only
) {
559 num
= ARRAY_SIZE(groups
);
561 maps
= talloc_array(talloc_tos(), struct id_map
*, num
+1);
563 return NT_STATUS_NO_MEMORY
;
566 for (i
= 0; i
< num
; i
++) {
567 maps
[i
] = talloc(maps
, struct id_map
);
568 if (maps
[i
] == NULL
) {
570 return NT_STATUS_NO_MEMORY
;
572 maps
[i
]->xid
.type
= ID_TYPE_GID
;
573 maps
[i
]->sid
= dom_sid_parse_talloc(maps
, groups
[i
]);
578 status
= idmap_autorid_sids_to_unixids(dom
, maps
);
580 DEBUG(10,("Preallocation run finished with status %s\n",
585 return NT_STATUS_IS_OK(status
)?NT_STATUS_OK
:NT_STATUS_UNSUCCESSFUL
;
588 static NTSTATUS
idmap_autorid_initialize_action(struct db_context
*db
,
591 struct idmap_domain
*dom
;
592 struct idmap_tdb_common_context
*common
;
593 struct autorid_global_config
*config
;
596 dom
= (struct idmap_domain
*)private_data
;
597 common
= (struct idmap_tdb_common_context
*)dom
->private_data
;
598 config
= (struct autorid_global_config
*)common
->private_data
;
600 status
= idmap_autorid_init_hwms(db
);
601 if (!NT_STATUS_IS_OK(status
)) {
605 status
= idmap_autorid_saveconfig(db
, config
);
606 if (!NT_STATUS_IS_OK(status
)) {
607 DEBUG(1, ("Failed to store configuration data!\n"));
611 status
= idmap_autorid_preallocate_wellknown(dom
);
612 if (!NT_STATUS_IS_OK(status
)) {
613 DEBUG(1, ("Failed to preallocate wellknown sids: %s\n",
621 static NTSTATUS
idmap_autorid_initialize(struct idmap_domain
*dom
)
623 struct idmap_tdb_common_context
*commonconfig
;
624 struct autorid_global_config
*config
;
627 if (!strequal(dom
->name
, "*")) {
628 DEBUG(0, ("idmap_autorid_initialize: Error: autorid configured "
629 "for domain '%s'. But autorid can only be used for "
630 "the default idmap configuration.\n", dom
->name
));
631 return NT_STATUS_INVALID_PARAMETER
;
634 commonconfig
= talloc_zero(dom
, struct idmap_tdb_common_context
);
636 DEBUG(0, ("Out of memory!\n"));
637 return NT_STATUS_NO_MEMORY
;
639 dom
->private_data
= commonconfig
;
641 commonconfig
->rw_ops
= talloc_zero(commonconfig
, struct idmap_rw_ops
);
642 if (commonconfig
->rw_ops
== NULL
) {
643 DEBUG(0, ("Out of memory!\n"));
644 return NT_STATUS_NO_MEMORY
;
647 config
= talloc_zero(commonconfig
, struct autorid_global_config
);
649 DEBUG(0, ("Out of memory!\n"));
650 return NT_STATUS_NO_MEMORY
;
652 commonconfig
->private_data
= config
;
654 config
->minvalue
= dom
->low_id
;
655 config
->rangesize
= lp_parm_int(-1, "idmap config *",
656 "rangesize", 100000);
658 config
->maxranges
= (dom
->high_id
- dom
->low_id
+ 1) /
661 if (config
->maxranges
== 0) {
662 DEBUG(1, ("Allowed uid range is smaller than rangesize. "
663 "Increase uid range or decrease rangesize.\n"));
664 status
= NT_STATUS_INVALID_PARAMETER
;
668 /* check if the high-low limit is a multiple of the rangesize */
669 if ((dom
->high_id
- dom
->low_id
+ 1) % config
->rangesize
!= 0) {
670 DEBUG(5, ("High uid-low uid difference of %d "
671 "is not a multiple of the rangesize %d, "
672 "limiting ranges to lower boundary number of %d\n",
673 (dom
->high_id
- dom
->low_id
+ 1), config
->rangesize
,
677 DEBUG(5, ("%d domain ranges with a size of %d are available\n",
678 config
->maxranges
, config
->rangesize
));
680 ignore_builtin
= lp_parm_bool(-1, "idmap config *",
681 "ignore builtin", false);
683 /* fill the TDB common configuration */
685 commonconfig
->max_id
= config
->rangesize
- 1
686 - IDMAP_AUTORID_ALLOC_RESERVED
;
687 commonconfig
->hwmkey_uid
= ALLOC_HWM_UID
;
688 commonconfig
->hwmkey_gid
= ALLOC_HWM_GID
;
689 commonconfig
->rw_ops
->get_new_id
= idmap_autorid_allocate_id
;
690 commonconfig
->rw_ops
->set_mapping
= idmap_tdb_common_set_mapping
;
692 status
= idmap_autorid_db_open(state_path("autorid.tdb"),
693 NULL
, /* TALLOC_CTX */
695 if (!NT_STATUS_IS_OK(status
)) {
699 commonconfig
->db
= autorid_db
;
701 status
= dbwrap_trans_do(autorid_db
,
702 idmap_autorid_initialize_action
,
704 if (!NT_STATUS_IS_OK(status
)) {
705 DEBUG(1, ("Failed to init the idmap database: %s\n",
720 Close the idmap tdb instance
722 static struct idmap_methods autorid_methods
= {
723 .init
= idmap_autorid_initialize
,
724 .unixids_to_sids
= idmap_autorid_unixids_to_sids
,
725 .sids_to_unixids
= idmap_autorid_sids_to_unixids
,
726 .allocate_id
= idmap_autorid_allocate_id
729 NTSTATUS
idmap_autorid_init(void)
731 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
,
732 "autorid", &autorid_methods
);