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/>.
29 #include "../libcli/security/dom_sid.h"
32 #define DBGC_CLASS DBGC_IDMAP
34 #define HWM "NEXT RANGE"
35 #define CONFIGKEY "CONFIG"
37 struct autorid_global_config {
43 struct autorid_domain_config {
46 struct autorid_global_config *globalcfg;
49 /* handle to the tdb storing domain <-> range assignments */
50 static struct db_context *autorid_db;
52 static NTSTATUS idmap_autorid_get_domainrange(struct db_context *db,
56 uint32_t domainnum, hwm;
57 char *sidstr, *numstr;
58 struct autorid_domain_config *cfg;
60 cfg = (struct autorid_domain_config *)private_data;
61 sidstr = dom_sid_string(talloc_tos(), &(cfg->sid));
64 return NT_STATUS_NO_MEMORY;
67 if (!dbwrap_fetch_uint32(db, sidstr, &domainnum)) {
68 DEBUG(10, ("Acquiring new range for domain %s\n", sidstr));
70 /* fetch the current HWM */
71 if (!dbwrap_fetch_uint32(db, HWM, &hwm)) {
72 DEBUG(1, ("Fatal error while fetching current "
74 ret = NT_STATUS_INTERNAL_ERROR;
78 /* do we have a range left? */
79 if (hwm >= cfg->globalcfg->maxranges) {
80 DEBUG(1, ("No more domain ranges available!\n"));
81 ret = NT_STATUS_NO_MEMORY;
85 /* increase the HWM */
86 ret = dbwrap_change_uint32_atomic(db, HWM, &domainnum, 1);
87 if (!NT_STATUS_IS_OK(ret)) {
88 DEBUG(1, ("Fatal error while fetching a new "
89 "domain range value!\n"));
93 /* store away the new mapping in both directions */
94 ret = dbwrap_trans_store_uint32(db, sidstr, domainnum);
95 if (!NT_STATUS_IS_OK(ret)) {
96 DEBUG(1, ("Fatal error while storing new "
97 "domain->range assignment!\n"));
101 numstr = talloc_asprintf(db, "%u", domainnum);
103 ret = NT_STATUS_NO_MEMORY;
107 ret = dbwrap_trans_store_bystring(db, numstr,
108 string_term_tdb_data(sidstr),
110 if (!NT_STATUS_IS_OK(ret)) {
112 DEBUG(1, ("Fatal error while storing "
113 "new domain->range assignment!\n"));
117 DEBUG(5, ("Acquired new range #%d for domain %s\n",
121 DEBUG(10, ("Using range #%d for domain %s\n", domainnum, sidstr));
122 cfg->domainnum = domainnum;
133 static NTSTATUS idmap_autorid_id_to_sid(TALLOC_CTX * memctx,
134 struct autorid_global_config *cfg,
142 /* can this be one of our ids? */
143 if (map->xid.id < cfg->minvalue) {
144 DEBUG(10, ("id %d is lower than minimum value, "
145 "ignoring mapping request\n", map->xid.id));
146 map->status = ID_UNKNOWN;
150 if (map->xid.id > (cfg->minvalue + cfg->rangesize * cfg->maxranges)) {
151 DEBUG(10, ("id %d is outside of maximum id value, "
152 "ignoring mapping request\n", map->xid.id));
153 map->status = ID_UNKNOWN;
157 /* determine the range of this uid */
158 range = ((map->xid.id - cfg->minvalue) / cfg->rangesize);
160 keystr = talloc_asprintf(memctx, "%u", range);
162 return NT_STATUS_NO_MEMORY;
165 data = dbwrap_fetch_bystring(autorid_db, memctx, keystr);
168 DEBUG(4, ("id %d belongs to range %d which does not have "
169 "domain mapping, ignoring mapping request\n",
170 map->xid.id, range));
172 string_to_sid(&sid, (const char *)data.dptr);
174 sid_compose(map->sid, &sid,
175 (map->xid.id - cfg->minvalue -
176 range * cfg->rangesize));
178 /* We **really** should have some way of validating
179 the SID exists and is the correct type here. But
180 that is a deficiency in the idmap_rid design. */
182 map->status = ID_MAPPED;
187 /**********************************
188 Single sid to id lookup function.
189 **********************************/
191 static NTSTATUS idmap_autorid_sid_to_id(TALLOC_CTX * memctx,
192 struct autorid_global_config *global,
193 struct autorid_domain_config *domain,
198 sid_peek_rid(map->sid, &rid);
200 /* if the rid is higher than the size of the range, we cannot map it */
201 if (rid >= global->rangesize) {
202 map->status = ID_UNKNOWN;
203 DEBUG(2, ("RID %d is larger then size of range (%d), "
204 "user cannot be mapped\n", rid, global->rangesize));
205 return NT_STATUS_UNSUCCESSFUL;
207 map->xid.id = global->minvalue +
208 (global->rangesize * domain->domainnum)+rid;
210 /* We **really** should have some way of validating
211 the SID exists and is the correct type here. But
212 that is a deficiency in the idmap_rid design. */
214 map->status = ID_MAPPED;
219 /**********************************
220 lookup a set of unix ids.
221 **********************************/
223 static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
226 struct autorid_global_config *globalcfg;
231 /* initialize the status to avoid surprise */
232 for (i = 0; ids[i]; i++) {
233 ids[i]->status = ID_UNKNOWN;
236 globalcfg = talloc_get_type(dom->private_data,
237 struct autorid_global_config);
239 ctx = talloc_new(dom);
241 DEBUG(0, ("Out of memory!\n"));
242 return NT_STATUS_NO_MEMORY;
245 for (i = 0; ids[i]; i++) {
247 ret = idmap_autorid_id_to_sid(ctx, globalcfg, ids[i]);
249 if ((!NT_STATUS_IS_OK(ret)) &&
250 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
251 /* some fatal error occurred, log it */
252 DEBUG(3, ("Unexpected error resolving an ID "
253 " (%d)\n", ids[i]->xid.id));
267 /**********************************
268 lookup a set of sids.
269 **********************************/
271 static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
274 struct autorid_global_config *global;
275 struct winbindd_tdc_domain *domain;
280 ctx = talloc_new(dom);
282 DEBUG(0, ("Out of memory!\n"));
283 ret = NT_STATUS_NO_MEMORY;
287 /* initialize the status to avoid surprise */
288 for (i = 0; ids[i]; i++) {
289 ids[i]->status = ID_UNKNOWN;
292 global = talloc_get_type(dom->private_data,
293 struct autorid_global_config);
295 for (i = 0; ids[i]; i++) {
296 struct autorid_domain_config domaincfg;
297 struct dom_sid domainsid;
300 sid_copy(&domainsid, ids[i]->sid);
301 if (!sid_split_rid(&domainsid, &rid)) {
302 DEBUG(4, ("Could not determine domain SID from %s, "
303 "ignoring mapping request\n",
304 sid_string_dbg(ids[i]->sid)));
308 domain = wcache_tdc_fetch_domainbysid(ctx, &domainsid);
309 if (domain == NULL) {
310 DEBUG(10, ("Ignoring unknown domain sid %s\n",
311 sid_string_dbg(&domainsid)));
315 ZERO_STRUCT(domaincfg);
316 domaincfg.sid = domain->sid;
317 domaincfg.globalcfg = global;
319 ret = dbwrap_trans_do(autorid_db,
320 idmap_autorid_get_domainrange,
323 if (!NT_STATUS_IS_OK(ret)) {
324 DEBUG(3, ("Could not determine range for domain, "
325 "check previous messages for reason\n"));
329 ret = idmap_autorid_sid_to_id(ctx, global, &domaincfg, ids[i]);
331 if ((!NT_STATUS_IS_OK(ret)) &&
332 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
333 /* some fatal error occurred, log it */
334 DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
335 sid_string_dbg(ids[i]->sid)));
350 * open and initialize the database which stores the ranges for the domains
352 static NTSTATUS idmap_autorid_db_init(void)
357 /* its already open */
361 /* Open idmap repository */
362 autorid_db = db_open(NULL, state_path("autorid.tdb"), 0,
363 TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
366 DEBUG(0, ("Unable to open idmap_autorid database '%s'\n",
367 state_path("autorid.tdb")));
368 return NT_STATUS_UNSUCCESSFUL;
371 /* Initialize high water mark for the currently used range to 0 */
372 hwm = dbwrap_fetch_int32(autorid_db, HWM);
375 (dbwrap_trans_store_int32(autorid_db, HWM, 0))) {
377 ("Unable to initialise HWM in autorid "
379 return NT_STATUS_INTERNAL_DB_ERROR;
386 static struct autorid_global_config *idmap_autorid_loadconfig(TALLOC_CTX * ctx)
390 struct autorid_global_config *cfg;
391 unsigned long minvalue, rangesize, maxranges;
393 data = dbwrap_fetch_bystring(autorid_db, ctx, CONFIGKEY);
396 DEBUG(10, ("No saved config found\n"));
400 cfg = TALLOC_ZERO_P(ctx, struct autorid_global_config);
405 if (sscanf((char *)data.dptr,
406 "minvalue:%lu rangesize:%lu maxranges:%lu",
407 &minvalue, &rangesize, &maxranges) != 3) {
409 ("Found invalid configuration data"
410 "creating new config\n"));
414 cfg->minvalue = minvalue;
415 cfg->rangesize = rangesize;
416 cfg->maxranges = maxranges;
418 DEBUG(10, ("Loaded previously stored configuration "
419 "minvalue:%d rangesize:%d\n",
420 cfg->minvalue, cfg->rangesize));
426 static NTSTATUS idmap_autorid_saveconfig(struct autorid_global_config *cfg)
434 talloc_asprintf(talloc_tos(),
435 "minvalue:%u rangesize:%u maxranges:%u",
436 cfg->minvalue, cfg->rangesize, cfg->maxranges);
439 return NT_STATUS_NO_MEMORY;
442 data = string_tdb_data(cfgstr);
444 status = dbwrap_trans_store_bystring(autorid_db, CONFIGKEY,
452 static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom,
455 struct autorid_global_config *config;
456 struct autorid_global_config *storedconfig = NULL;
460 config = TALLOC_ZERO_P(dom, struct autorid_global_config);
462 DEBUG(0, ("Out of memory!\n"));
463 return NT_STATUS_NO_MEMORY;
466 status = idmap_autorid_db_init();
467 if (!NT_STATUS_IS_OK(status)) {
471 config->minvalue = dom->low_id;
472 config->rangesize = lp_parm_int(-1, "autorid", "rangesize", 100000);
474 if (config->rangesize < 2000) {
475 DEBUG(1, ("autorid rangesize must be at least 2000\n"));
476 status = NT_STATUS_INVALID_PARAMETER;
480 config->maxranges = (dom->high_id - dom->low_id + 1) /
483 if (config->maxranges == 0) {
484 DEBUG(1, ("allowed uid range is smaller then rangesize, "
485 "increase uid range or decrease rangesize\n"));
486 status = NT_STATUS_INVALID_PARAMETER;
490 /* check if the high-low limit is a multiple of the rangesize */
491 if ((dom->high_id - dom->low_id + 1) % config->rangesize != 0) {
492 DEBUG(5, ("High uid-low uid difference of %d "
493 "is not a multiple of the rangesize %d, "
494 "limiting ranges to lower boundary number of %d\n",
495 (dom->high_id - dom->low_id + 1), config->rangesize,
499 DEBUG(10, ("Current configuration in config is "
500 "minvalue:%d rangesize:%d maxranges:%d\n",
501 config->minvalue, config->rangesize, config->maxranges));
503 /* read previously stored config and current HWM */
504 storedconfig = idmap_autorid_loadconfig(talloc_tos());
506 if (!dbwrap_fetch_uint32(autorid_db, HWM, &hwm)) {
507 DEBUG(1, ("Fatal error while fetching current "
509 status = NT_STATUS_INTERNAL_ERROR;
513 /* did the minimum value or rangesize change? */
515 ((storedconfig->minvalue != config->minvalue) ||
516 (storedconfig->rangesize != config->rangesize))) {
517 DEBUG(1, ("New configuration values for rangesize or "
518 "minimum uid value conflict with previously "
519 "used values! Aborting initialization\n"));
520 status = NT_STATUS_INVALID_PARAMETER;
525 * has the highest uid value been reduced to setting that is not
526 * sufficient any more for already existing ranges?
528 if (hwm > config->maxranges) {
529 DEBUG(1, ("New upper uid limit is too low to cover "
530 "existing mappings! Aborting initialization\n"));
531 status = NT_STATUS_INVALID_PARAMETER;
535 status = idmap_autorid_saveconfig(config);
537 if (!NT_STATUS_IS_OK(status)) {
538 DEBUG(1, ("Failed to store configuration data!\n"));
542 DEBUG(5, ("%d domain ranges with a size of %d are available\n",
543 config->maxranges, config->rangesize));
545 dom->private_data = config;
547 if (!NT_STATUS_IS_OK(status)) {
555 talloc_free(storedconfig);
561 Close the idmap tdb instance
563 static struct idmap_methods autorid_methods = {
564 .init = idmap_autorid_initialize,
565 .unixids_to_sids = idmap_autorid_unixids_to_sids,
566 .sids_to_unixids = idmap_autorid_sids_to_unixids,
569 NTSTATUS idmap_autorid_init(void)
571 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
572 "autorid", &autorid_methods);