lib/util: new merged debug system
[Samba.git] / source3 / winbindd / idmap_autorid.c
blobae65647ed63091207b910d476b846149e5800d74
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-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/>.
25 #include "includes.h"
26 #include "winbindd.h"
27 #include "dbwrap.h"
28 #include "idmap.h"
29 #include "../libcli/security/dom_sid.h"
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_IDMAP
34 #define HWM "NEXT RANGE"
35 #define CONFIGKEY "CONFIG"
37 struct autorid_global_config {
38 uint32_t minvalue;
39 uint32_t rangesize;
40 uint32_t maxranges;
43 struct autorid_domain_config {
44 struct dom_sid sid;
45 uint32_t domainnum;
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,
53 void *private_data)
55 NTSTATUS ret;
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));
63 if (!sidstr) {
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 "
73 "HWM value!\n"));
74 ret = NT_STATUS_INTERNAL_ERROR;
75 goto 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;
82 goto error;
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"));
90 goto error;
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"));
98 goto error;
101 numstr = talloc_asprintf(db, "%u", domainnum);
102 if (!numstr) {
103 ret = NT_STATUS_NO_MEMORY;
104 goto error;
107 ret = dbwrap_trans_store_bystring(db, numstr,
108 string_term_tdb_data(sidstr),
109 TDB_INSERT);
110 if (!NT_STATUS_IS_OK(ret)) {
111 talloc_free(numstr);
112 DEBUG(1, ("Fatal error while storing "
113 "new domain->range assignment!\n"));
114 goto error;
116 talloc_free(numstr);
117 DEBUG(5, ("Acquired new range #%d for domain %s\n",
118 domainnum, sidstr));
121 DEBUG(10, ("Using range #%d for domain %s\n", domainnum, sidstr));
122 cfg->domainnum = domainnum;
124 talloc_free(sidstr);
125 return NT_STATUS_OK;
127 error:
128 talloc_free(sidstr);
129 return ret;
133 static NTSTATUS idmap_autorid_id_to_sid(TALLOC_CTX * memctx,
134 struct autorid_global_config *cfg,
135 struct id_map *map)
137 uint32_t range;
138 TDB_DATA data;
139 char *keystr;
140 struct dom_sid sid;
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;
147 return NT_STATUS_OK;
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;
154 return NT_STATUS_OK;
157 /* determine the range of this uid */
158 range = ((map->xid.id - cfg->minvalue) / cfg->rangesize);
160 keystr = talloc_asprintf(memctx, "%u", range);
161 if (!keystr) {
162 return NT_STATUS_NO_MEMORY;
165 data = dbwrap_fetch_bystring(autorid_db, memctx, keystr);
167 if (!data.dptr) {
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));
171 } else {
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;
184 return NT_STATUS_OK;
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,
194 struct id_map *map)
196 uint32_t rid;
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;
216 return NT_STATUS_OK;
219 /**********************************
220 lookup a set of unix ids.
221 **********************************/
223 static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
224 struct id_map **ids)
226 struct autorid_global_config *globalcfg;
227 TALLOC_CTX *ctx;
228 NTSTATUS ret;
229 int i;
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);
240 if (!ctx) {
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));
254 goto failure;
258 talloc_free(ctx);
259 return NT_STATUS_OK;
261 failure:
262 talloc_free(ctx);
263 return ret;
267 /**********************************
268 lookup a set of sids.
269 **********************************/
271 static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
272 struct id_map **ids)
274 struct autorid_global_config *global;
275 struct autorid_domain_config *domaincfg;
276 struct dom_sid *domainsid;
277 struct winbindd_tdc_domain *domain;
278 uint32_t rid;
279 TALLOC_CTX *ctx;
280 NTSTATUS ret;
281 int i;
283 ctx = talloc_new(dom);
284 domainsid = talloc(ctx, struct dom_sid);
285 if (!ctx || !domainsid) {
286 DEBUG(0, ("Out of memory!\n"));
287 ret = NT_STATUS_NO_MEMORY;
288 goto failure;
291 if ((domaincfg = TALLOC_ZERO_P(ctx,
292 struct autorid_domain_config)) == NULL) {
293 DEBUG(0, ("Out of memory!\n"));
294 ret = NT_STATUS_NO_MEMORY;
295 goto failure;
298 /* initialize the status to avoid surprise */
299 for (i = 0; ids[i]; i++) {
300 ids[i]->status = ID_UNKNOWN;
303 global = talloc_get_type(dom->private_data,
304 struct autorid_global_config);
306 for (i = 0; ids[i]; i++) {
307 ZERO_STRUCTP(domainsid);
308 sid_copy(domainsid, ids[i]->sid);
309 if (!sid_split_rid(domainsid, &rid)) {
310 DEBUG(4, ("Could not determine domain SID from %s, "
311 "ignoring mapping request\n",
312 sid_string_dbg(ids[i]->sid)));
313 continue;
316 domain = wcache_tdc_fetch_domainbysid(ctx, domainsid);
317 if (domain == NULL) {
318 DEBUG(10, ("Ignoring unknown domain sid %s\n",
319 sid_string_dbg(domainsid)));
320 continue;
323 ZERO_STRUCTP(domaincfg);
324 domaincfg->sid = domain->sid;
325 domaincfg->globalcfg = global;
327 ret = dbwrap_trans_do(autorid_db, idmap_autorid_get_domainrange,
328 domaincfg);
330 if (!NT_STATUS_IS_OK(ret)) {
331 DEBUG(3, ("Could not determine range for domain, "
332 "check previous messages for reason\n"));
333 goto failure;
336 ret = idmap_autorid_sid_to_id(ctx, global, domaincfg, ids[i]);
338 if ((!NT_STATUS_IS_OK(ret)) &&
339 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
340 /* some fatal error occurred, log it */
341 DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
342 sid_string_dbg(ids[i]->sid)));
343 goto failure;
347 talloc_free(ctx);
348 return NT_STATUS_OK;
350 failure:
351 talloc_free(ctx);
352 return ret;
357 * open and initialize the database which stores the ranges for the domains
359 static NTSTATUS idmap_autorid_db_init(void)
361 int32_t hwm;
363 if (autorid_db) {
364 /* its already open */
365 return NT_STATUS_OK;
368 /* Open idmap repository */
369 autorid_db = db_open(NULL, state_path("autorid.tdb"), 0,
370 TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
372 if (!autorid_db) {
373 DEBUG(0, ("Unable to open idmap_autorid database '%s'\n",
374 state_path("autorid.tdb")));
375 return NT_STATUS_UNSUCCESSFUL;
378 /* Initialize high water mark for the currently used range to 0 */
379 hwm = dbwrap_fetch_int32(autorid_db, HWM);
380 if ((hwm < 0)) {
381 if (!NT_STATUS_IS_OK
382 (dbwrap_trans_store_int32(autorid_db, HWM, 0))) {
383 DEBUG(0,
384 ("Unable to initialise HWM in autorid "
385 "database\n"));
386 return NT_STATUS_INTERNAL_DB_ERROR;
390 return NT_STATUS_OK;
393 static struct autorid_global_config *idmap_autorid_loadconfig(TALLOC_CTX * ctx)
396 TDB_DATA data;
397 struct autorid_global_config *cfg;
398 unsigned long minvalue, rangesize, maxranges;
400 data = dbwrap_fetch_bystring(autorid_db, ctx, CONFIGKEY);
402 if (!data.dptr) {
403 DEBUG(10, ("No saved config found\n"));
404 return NULL;
407 cfg = TALLOC_ZERO_P(ctx, struct autorid_global_config);
408 if (!cfg) {
409 return NULL;
412 if (sscanf((char *)data.dptr,
413 "minvalue:%lu rangesize:%lu maxranges:%lu",
414 &minvalue, &rangesize, &maxranges) != 3) {
415 DEBUG(1,
416 ("Found invalid configuration data"
417 "creating new config\n"));
418 return NULL;
421 cfg->minvalue = minvalue;
422 cfg->rangesize = rangesize;
423 cfg->maxranges = maxranges;
425 DEBUG(10, ("Loaded previously stored configuration "
426 "minvalue:%d rangesize:%d\n",
427 cfg->minvalue, cfg->rangesize));
429 return cfg;
433 static NTSTATUS idmap_autorid_saveconfig(struct autorid_global_config *cfg)
436 NTSTATUS status;
437 TDB_DATA data;
438 char *cfgstr;
440 cfgstr =
441 talloc_asprintf(talloc_tos(),
442 "minvalue:%u rangesize:%u maxranges:%u",
443 cfg->minvalue, cfg->rangesize, cfg->maxranges);
445 if (!cfgstr) {
446 return NT_STATUS_NO_MEMORY;
449 data = string_tdb_data(cfgstr);
451 status = dbwrap_trans_store_bystring(autorid_db, CONFIGKEY,
452 data, TDB_REPLACE);
454 talloc_free(cfgstr);
456 return status;
459 static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom,
460 const char *params)
462 struct autorid_global_config *config;
463 struct autorid_global_config *storedconfig = NULL;
464 NTSTATUS status;
465 uint32_t hwm;
467 config = TALLOC_ZERO_P(dom, struct autorid_global_config);
468 if (!config) {
469 DEBUG(0, ("Out of memory!\n"));
470 return NT_STATUS_NO_MEMORY;
473 status = idmap_autorid_db_init();
474 if (!NT_STATUS_IS_OK(status)) {
475 goto error;
478 config->minvalue = dom->low_id;
479 config->rangesize = lp_parm_int(-1, "autorid", "rangesize", 100000);
481 if (config->rangesize < 2000) {
482 DEBUG(1, ("autorid rangesize must be at least 2000\n"));
483 status = NT_STATUS_INVALID_PARAMETER;
484 goto error;
487 config->maxranges = (dom->high_id - dom->low_id + 1) /
488 config->rangesize;
490 if (config->maxranges == 0) {
491 DEBUG(1, ("allowed uid range is smaller then rangesize, "
492 "increase uid range or decrease rangesize\n"));
493 status = NT_STATUS_INVALID_PARAMETER;
494 goto error;
497 /* check if the high-low limit is a multiple of the rangesize */
498 if ((dom->high_id - dom->low_id + 1) % config->rangesize != 0) {
499 DEBUG(5, ("High uid-low uid difference of %d "
500 "is not a multiple of the rangesize %d, "
501 "limiting ranges to lower boundary number of %d\n",
502 (dom->high_id - dom->low_id + 1), config->rangesize,
503 config->maxranges));
506 DEBUG(10, ("Current configuration in config is "
507 "minvalue:%d rangesize:%d maxranges:%d\n",
508 config->minvalue, config->rangesize, config->maxranges));
510 /* read previously stored config and current HWM */
511 storedconfig = idmap_autorid_loadconfig(talloc_tos());
513 if (!dbwrap_fetch_uint32(autorid_db, HWM, &hwm)) {
514 DEBUG(1, ("Fatal error while fetching current "
515 "HWM value!\n"));
516 status = NT_STATUS_INTERNAL_ERROR;
517 goto error;
520 /* did the minimum value or rangesize change? */
521 if (storedconfig &&
522 ((storedconfig->minvalue != config->minvalue) ||
523 (storedconfig->rangesize != config->rangesize))) {
524 DEBUG(1, ("New configuration values for rangesize or "
525 "minimum uid value conflict with previously "
526 "used values! Aborting initialization\n"));
527 status = NT_STATUS_INVALID_PARAMETER;
528 goto error;
532 * has the highest uid value been reduced to setting that is not
533 * sufficient any more for already existing ranges?
535 if (hwm > config->maxranges) {
536 DEBUG(1, ("New upper uid limit is too low to cover "
537 "existing mappings! Aborting initialization\n"));
538 status = NT_STATUS_INVALID_PARAMETER;
539 goto error;
542 status = idmap_autorid_saveconfig(config);
544 if (!NT_STATUS_IS_OK(status)) {
545 DEBUG(1, ("Failed to store configuration data!\n"));
546 goto error;
549 DEBUG(5, ("%d domain ranges with a size of %d are available\n",
550 config->maxranges, config->rangesize));
552 dom->private_data = config;
554 if (!NT_STATUS_IS_OK(status)) {
555 goto error;
558 return NT_STATUS_OK;
560 error:
561 talloc_free(config);
562 talloc_free(storedconfig);
564 return status;
568 Close the idmap tdb instance
570 static NTSTATUS idmap_autorid_close(struct idmap_domain *dom)
572 /* don't do anything */
573 return NT_STATUS_OK;
576 static struct idmap_methods autorid_methods = {
577 .init = idmap_autorid_initialize,
578 .unixids_to_sids = idmap_autorid_unixids_to_sids,
579 .sids_to_unixids = idmap_autorid_sids_to_unixids,
580 .close_fn = idmap_autorid_close
583 NTSTATUS idmap_autorid_init(void)
585 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
586 "autorid", &autorid_methods);