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-2011
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 #include "system/filesys.h"
28 #include "dbwrap/dbwrap.h"
29 #include "dbwrap/dbwrap_open.h"
31 #include "../libcli/security/dom_sid.h"
35 #define DBGC_CLASS DBGC_IDMAP
37 #define HWM "NEXT RANGE"
38 #define ALLOC_HWM "NEXT ALLOC ID"
39 #define ALLOC_POOL_SIZE 500
40 #define CONFIGKEY "CONFIG"
42 struct autorid_global_config {
48 struct autorid_domain_config {
51 struct autorid_global_config *globalcfg;
54 /* handle to the tdb storing domain <-> range assignments */
55 static struct db_context *autorid_db;
57 static NTSTATUS idmap_autorid_get_domainrange(struct db_context *db,
61 uint32_t domainnum, hwm;
64 struct autorid_domain_config *cfg;
66 cfg = (struct autorid_domain_config *)private_data;
67 dom_sid_string_buf(&(cfg->sid), sidstr, sizeof(sidstr));
69 ret = dbwrap_fetch_uint32(db, sidstr, &domainnum);
70 if (!NT_STATUS_IS_OK(ret)) {
71 DEBUG(10, ("Acquiring new range for domain %s\n", sidstr));
73 /* fetch the current HWM */
74 ret = dbwrap_fetch_uint32(db, HWM, &hwm);
75 if (!NT_STATUS_IS_OK(ret)) {
76 DEBUG(1, ("Fatal error while fetching current "
77 "HWM value: %s\n", nt_errstr(ret)));
78 ret = NT_STATUS_INTERNAL_ERROR;
82 /* do we have a range left? */
83 if (hwm >= cfg->globalcfg->maxranges) {
84 DEBUG(1, ("No more domain ranges available!\n"));
85 ret = NT_STATUS_NO_MEMORY;
89 /* increase the HWM */
90 ret = dbwrap_change_uint32_atomic(db, HWM, &domainnum, 1);
91 if (!NT_STATUS_IS_OK(ret)) {
92 DEBUG(1, ("Fatal error while fetching a new "
93 "domain range value!\n"));
97 /* store away the new mapping in both directions */
98 ret = dbwrap_trans_store_uint32(db, sidstr, domainnum);
99 if (!NT_STATUS_IS_OK(ret)) {
100 DEBUG(1, ("Fatal error while storing new "
101 "domain->range assignment!\n"));
105 numstr = talloc_asprintf(db, "%u", domainnum);
107 ret = NT_STATUS_NO_MEMORY;
111 ret = dbwrap_trans_store_bystring(db, numstr,
112 string_term_tdb_data(sidstr),
115 if (!NT_STATUS_IS_OK(ret)) {
116 DEBUG(1, ("Fatal error while storing "
117 "new domain->range assignment!\n"));
120 DEBUG(5, ("Acquired new range #%d for domain %s\n",
124 DEBUG(10, ("Using range #%d for domain %s\n", domainnum, sidstr));
125 cfg->domainnum = domainnum;
134 static NTSTATUS idmap_autorid_id_to_sid(struct autorid_global_config *cfg,
143 /* can this be one of our ids? */
144 if (map->xid.id < cfg->minvalue) {
145 DEBUG(10, ("id %d is lower than minimum value, "
146 "ignoring mapping request\n", map->xid.id));
147 map->status = ID_UNKNOWN;
151 if (map->xid.id > (cfg->minvalue + cfg->rangesize * cfg->maxranges)) {
152 DEBUG(10, ("id %d is outside of maximum id value, "
153 "ignoring mapping request\n", map->xid.id));
154 map->status = ID_UNKNOWN;
158 /* determine the range of this uid */
159 range = ((map->xid.id - cfg->minvalue) / cfg->rangesize);
161 keystr = talloc_asprintf(talloc_tos(), "%u", range);
163 return NT_STATUS_NO_MEMORY;
166 status = dbwrap_fetch_bystring(autorid_db, talloc_tos(), keystr, &data);
169 if (!NT_STATUS_IS_OK(status)) {
170 DEBUG(4, ("id %d belongs to range %d which does not have "
171 "domain mapping, ignoring mapping request\n",
172 map->xid.id, range));
173 map->status = ID_UNKNOWN;
177 string_to_sid(&sid, (const char *)data.dptr);
178 TALLOC_FREE(data.dptr);
180 sid_compose(map->sid, &sid,
181 (map->xid.id - cfg->minvalue -
182 range * cfg->rangesize));
184 /* We **really** should have some way of validating
185 the SID exists and is the correct type here. But
186 that is a deficiency in the idmap_rid design. */
188 map->status = ID_MAPPED;
192 /**********************************
193 Single sid to id lookup function.
194 **********************************/
196 static NTSTATUS idmap_autorid_sid_to_id(struct autorid_global_config *global,
197 struct autorid_domain_config *domain,
202 sid_peek_rid(map->sid, &rid);
204 /* if the rid is higher than the size of the range, we cannot map it */
205 if (rid >= global->rangesize) {
206 map->status = ID_UNKNOWN;
207 DEBUG(2, ("RID %d is larger then size of range (%d), "
208 "user cannot be mapped\n", rid, global->rangesize));
209 return NT_STATUS_UNSUCCESSFUL;
211 map->xid.id = global->minvalue +
212 (global->rangesize * domain->domainnum)+rid;
214 /* We **really** should have some way of validating
215 the SID exists and is the correct type here. But
216 that is a deficiency in the idmap_rid design. */
218 map->status = ID_MAPPED;
223 /**********************************
224 lookup a set of unix ids.
225 **********************************/
227 static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
230 struct autorid_global_config *globalcfg;
234 /* initialize the status to avoid surprise */
235 for (i = 0; ids[i]; i++) {
236 ids[i]->status = ID_UNKNOWN;
239 globalcfg = talloc_get_type(dom->private_data,
240 struct autorid_global_config);
242 for (i = 0; ids[i]; i++) {
244 ret = idmap_autorid_id_to_sid(globalcfg, ids[i]);
246 if ((!NT_STATUS_IS_OK(ret)) &&
247 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
248 /* some fatal error occurred, log it */
249 DEBUG(3, ("Unexpected error resolving an ID "
250 " (%d)\n", ids[i]->xid.id));
260 /**********************************
261 lookup a set of sids.
262 **********************************/
264 static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
267 struct autorid_global_config *global;
271 /* initialize the status to avoid surprise */
272 for (i = 0; ids[i]; i++) {
273 ids[i]->status = ID_UNKNOWN;
276 global = talloc_get_type(dom->private_data,
277 struct autorid_global_config);
279 for (i = 0; ids[i]; i++) {
280 struct winbindd_tdc_domain *domain;
281 struct autorid_domain_config domaincfg;
284 ZERO_STRUCT(domaincfg);
286 sid_copy(&domaincfg.sid, ids[i]->sid);
287 if (!sid_split_rid(&domaincfg.sid, &rid)) {
288 DEBUG(4, ("Could not determine domain SID from %s, "
289 "ignoring mapping request\n",
290 sid_string_dbg(ids[i]->sid)));
295 * Check if the domain is around
297 domain = wcache_tdc_fetch_domainbysid(talloc_tos(),
299 if (domain == NULL) {
300 DEBUG(10, ("Ignoring unknown domain sid %s\n",
301 sid_string_dbg(&domaincfg.sid)));
306 domaincfg.globalcfg = global;
308 ret = dbwrap_trans_do(autorid_db,
309 idmap_autorid_get_domainrange,
312 if (!NT_STATUS_IS_OK(ret)) {
313 DEBUG(3, ("Could not determine range for domain, "
314 "check previous messages for reason\n"));
318 ret = idmap_autorid_sid_to_id(global, &domaincfg, ids[i]);
320 if ((!NT_STATUS_IS_OK(ret)) &&
321 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
322 /* some fatal error occurred, log it */
323 DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
324 sid_string_dbg(ids[i]->sid)));
336 * open and initialize the database which stores the ranges for the domains
338 static NTSTATUS idmap_autorid_db_init(void)
344 /* its already open */
348 /* Open idmap repository */
349 autorid_db = db_open(NULL, state_path("autorid.tdb"), 0,
350 TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
353 DEBUG(0, ("Unable to open idmap_autorid database '%s'\n",
354 state_path("autorid.tdb")));
355 return NT_STATUS_UNSUCCESSFUL;
358 /* Initialize high water mark for the currently used range to 0 */
359 status = dbwrap_fetch_int32(autorid_db, HWM, &hwm);
360 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ||
361 (NT_STATUS_IS_OK(status) && (hwm < 0)))
363 status = dbwrap_trans_store_int32(autorid_db, HWM, 0);
364 if (!NT_STATUS_IS_OK(status)) {
366 ("Unable to initialise HWM in autorid "
367 "database: %s\n", nt_errstr(status)));
368 return NT_STATUS_INTERNAL_DB_ERROR;
370 } else if (!NT_STATUS_IS_OK(status)) {
371 DEBUG(0, ("unable to fetch HWM from autorid database: %s\n",
376 /* Initialize high water mark for alloc pool to 0 */
377 status = dbwrap_fetch_int32(autorid_db, ALLOC_HWM, &hwm);
378 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ||
379 (NT_STATUS_IS_OK(status) && (hwm < 0)))
381 status = dbwrap_trans_store_int32(autorid_db, ALLOC_HWM, 0);
382 if (!NT_STATUS_IS_OK(status)) {
384 ("Unable to initialise alloc HWM in autorid "
385 "database: %s\n", nt_errstr(status)));
386 return NT_STATUS_INTERNAL_DB_ERROR;
388 } else if (!NT_STATUS_IS_OK(status)) {
389 DEBUG(0, ("unable to fetch alloc HWM from autorid database: "
390 "%s\n", nt_errstr(status)));
397 static struct autorid_global_config *idmap_autorid_loadconfig(TALLOC_CTX * ctx)
401 struct autorid_global_config *cfg;
402 unsigned long minvalue, rangesize, maxranges;
405 status = dbwrap_fetch_bystring(autorid_db, ctx, CONFIGKEY, &data);
407 if (!NT_STATUS_IS_OK(status)) {
408 DEBUG(10, ("No saved config found\n"));
412 cfg = talloc_zero(ctx, struct autorid_global_config);
417 if (sscanf((char *)data.dptr,
418 "minvalue:%lu rangesize:%lu maxranges:%lu",
419 &minvalue, &rangesize, &maxranges) != 3) {
421 ("Found invalid configuration data"
422 "creating new config\n"));
426 cfg->minvalue = minvalue;
427 cfg->rangesize = rangesize;
428 cfg->maxranges = maxranges;
430 DEBUG(10, ("Loaded previously stored configuration "
431 "minvalue:%d rangesize:%d\n",
432 cfg->minvalue, cfg->rangesize));
438 static NTSTATUS idmap_autorid_saveconfig(struct autorid_global_config *cfg)
446 talloc_asprintf(talloc_tos(),
447 "minvalue:%u rangesize:%u maxranges:%u",
448 cfg->minvalue, cfg->rangesize, cfg->maxranges);
451 return NT_STATUS_NO_MEMORY;
454 data = string_tdb_data(cfgstr);
456 status = dbwrap_trans_store_bystring(autorid_db, CONFIGKEY,
464 static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
466 struct autorid_global_config *config;
467 struct autorid_global_config *storedconfig = NULL;
471 if (!strequal(dom->name, "*")) {
472 DEBUG(0, ("idmap_autorid_initialize: Error: autorid configured "
473 "for domain '%s'. But autorid can only be used for "
474 "the default idmap configuration.\n", dom->name));
475 return NT_STATUS_INVALID_PARAMETER;
478 config = talloc_zero(dom, struct autorid_global_config);
480 DEBUG(0, ("Out of memory!\n"));
481 return NT_STATUS_NO_MEMORY;
484 status = idmap_autorid_db_init();
485 if (!NT_STATUS_IS_OK(status)) {
489 config->minvalue = dom->low_id;
490 config->rangesize = lp_parm_int(-1, "idmap config *", "rangesize", 100000);
492 if (config->rangesize < 2000) {
493 DEBUG(1, ("autorid rangesize must be at least 2000\n"));
494 status = NT_STATUS_INVALID_PARAMETER;
498 config->maxranges = (dom->high_id - dom->low_id + 1) /
501 if (config->maxranges == 0) {
502 DEBUG(1, ("allowed uid range is smaller then rangesize, "
503 "increase uid range or decrease rangesize\n"));
504 status = NT_STATUS_INVALID_PARAMETER;
508 /* check if the high-low limit is a multiple of the rangesize */
509 if ((dom->high_id - dom->low_id + 1) % config->rangesize != 0) {
510 DEBUG(5, ("High uid-low uid difference of %d "
511 "is not a multiple of the rangesize %d, "
512 "limiting ranges to lower boundary number of %d\n",
513 (dom->high_id - dom->low_id + 1), config->rangesize,
517 DEBUG(10, ("Current configuration in config is "
518 "minvalue:%d rangesize:%d maxranges:%d\n",
519 config->minvalue, config->rangesize, config->maxranges));
521 /* read previously stored config and current HWM */
522 storedconfig = idmap_autorid_loadconfig(talloc_tos());
524 status = dbwrap_fetch_uint32(autorid_db, HWM, &hwm);
525 if (!NT_STATUS_IS_OK(status)) {
526 DEBUG(1, ("Fatal error while fetching current "
527 "HWM value: %s\n", nt_errstr(status)));
528 status = NT_STATUS_INTERNAL_ERROR;
532 /* did the minimum value or rangesize change? */
534 ((storedconfig->minvalue != config->minvalue) ||
535 (storedconfig->rangesize != config->rangesize))) {
536 DEBUG(1, ("New configuration values for rangesize or "
537 "minimum uid value conflict with previously "
538 "used values! Aborting initialization\n"));
539 status = NT_STATUS_INVALID_PARAMETER;
544 * has the highest uid value been reduced to setting that is not
545 * sufficient any more for already existing ranges?
547 if (hwm > config->maxranges) {
548 DEBUG(1, ("New upper uid limit is too low to cover "
549 "existing mappings! Aborting initialization\n"));
550 status = NT_STATUS_INVALID_PARAMETER;
554 status = idmap_autorid_saveconfig(config);
556 if (!NT_STATUS_IS_OK(status)) {
557 DEBUG(1, ("Failed to store configuration data!\n"));
561 DEBUG(5, ("%d domain ranges with a size of %d are available\n",
562 config->maxranges, config->rangesize));
564 dom->private_data = config;
572 talloc_free(storedconfig);
577 static NTSTATUS idmap_autorid_allocate_id(struct idmap_domain *dom,
578 struct unixid *xid) {
580 struct autorid_global_config *globalcfg;
584 if (!strequal(dom->name, "*")) {
585 DEBUG(3, ("idmap_autorid_allocate_id: "
586 "Refusing creation of mapping for domain'%s'. "
587 "Currently only supported for the default "
590 return NT_STATUS_NOT_IMPLEMENTED;
593 globalcfg = talloc_get_type(dom->private_data,
594 struct autorid_global_config);
596 ret = dbwrap_fetch_uint32(autorid_db, ALLOC_HWM, &hwm);
597 if (!NT_STATUS_IS_OK(ret)) {
598 DEBUG(1, ("Failed to fetch current allocation HWM value: %s\n",
600 return NT_STATUS_INTERNAL_ERROR;
603 if (hwm > ALLOC_POOL_SIZE) {
604 DEBUG(1, ("allocation pool is depleted!\n"));
605 return NT_STATUS_NO_MEMORY;
608 ret = dbwrap_change_uint32_atomic(autorid_db, ALLOC_HWM, &(xid->id), 1);
609 if (!NT_STATUS_IS_OK(ret)) {
610 DEBUG(1, ("Fatal error while allocating new ID!\n"));
612 xid->id = (xid->id)+(globalcfg->minvalue);
618 Close the idmap tdb instance
620 static struct idmap_methods autorid_methods = {
621 .init = idmap_autorid_initialize,
622 .unixids_to_sids = idmap_autorid_unixids_to_sids,
623 .sids_to_unixids = idmap_autorid_sids_to_unixids,
624 .allocate_id = idmap_autorid_allocate_id
627 NTSTATUS idmap_autorid_init(void)
629 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
630 "autorid", &autorid_methods);