idmap-autorid: Remove an unused parameter
[Samba.git] / source3 / winbindd / idmap_autorid.c
blob786bdfc5f1003f3624153f98e32bb3a297bf37e1
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(struct autorid_global_config *global,
192 struct autorid_domain_config *domain,
193 struct id_map *map)
195 uint32_t rid;
197 sid_peek_rid(map->sid, &rid);
199 /* if the rid is higher than the size of the range, we cannot map it */
200 if (rid >= global->rangesize) {
201 map->status = ID_UNKNOWN;
202 DEBUG(2, ("RID %d is larger then size of range (%d), "
203 "user cannot be mapped\n", rid, global->rangesize));
204 return NT_STATUS_UNSUCCESSFUL;
206 map->xid.id = global->minvalue +
207 (global->rangesize * domain->domainnum)+rid;
209 /* We **really** should have some way of validating
210 the SID exists and is the correct type here. But
211 that is a deficiency in the idmap_rid design. */
213 map->status = ID_MAPPED;
215 return NT_STATUS_OK;
218 /**********************************
219 lookup a set of unix ids.
220 **********************************/
222 static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
223 struct id_map **ids)
225 struct autorid_global_config *globalcfg;
226 TALLOC_CTX *ctx;
227 NTSTATUS ret;
228 int i;
230 /* initialize the status to avoid surprise */
231 for (i = 0; ids[i]; i++) {
232 ids[i]->status = ID_UNKNOWN;
235 globalcfg = talloc_get_type(dom->private_data,
236 struct autorid_global_config);
238 ctx = talloc_new(dom);
239 if (!ctx) {
240 DEBUG(0, ("Out of memory!\n"));
241 return NT_STATUS_NO_MEMORY;
244 for (i = 0; ids[i]; i++) {
246 ret = idmap_autorid_id_to_sid(ctx, globalcfg, ids[i]);
248 if ((!NT_STATUS_IS_OK(ret)) &&
249 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
250 /* some fatal error occurred, log it */
251 DEBUG(3, ("Unexpected error resolving an ID "
252 " (%d)\n", ids[i]->xid.id));
253 goto failure;
257 talloc_free(ctx);
258 return NT_STATUS_OK;
260 failure:
261 talloc_free(ctx);
262 return ret;
266 /**********************************
267 lookup a set of sids.
268 **********************************/
270 static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
271 struct id_map **ids)
273 struct autorid_global_config *global;
274 TALLOC_CTX *ctx;
275 NTSTATUS ret;
276 int i;
278 ctx = talloc_new(dom);
279 if (!ctx) {
280 DEBUG(0, ("Out of memory!\n"));
281 ret = NT_STATUS_NO_MEMORY;
282 goto failure;
285 /* initialize the status to avoid surprise */
286 for (i = 0; ids[i]; i++) {
287 ids[i]->status = ID_UNKNOWN;
290 global = talloc_get_type(dom->private_data,
291 struct autorid_global_config);
293 for (i = 0; ids[i]; i++) {
294 struct winbindd_tdc_domain *domain;
295 struct autorid_domain_config domaincfg;
296 uint32_t rid;
298 ZERO_STRUCT(domaincfg);
300 sid_copy(&domaincfg.sid, ids[i]->sid);
301 if (!sid_split_rid(&domaincfg.sid, &rid)) {
302 DEBUG(4, ("Could not determine domain SID from %s, "
303 "ignoring mapping request\n",
304 sid_string_dbg(ids[i]->sid)));
305 continue;
309 * Check if the domain is around
311 domain = wcache_tdc_fetch_domainbysid(talloc_tos(),
312 &domaincfg.sid);
313 if (domain == NULL) {
314 DEBUG(10, ("Ignoring unknown domain sid %s\n",
315 sid_string_dbg(&domaincfg.sid)));
316 continue;
318 TALLOC_FREE(domain);
320 domaincfg.globalcfg = global;
322 ret = dbwrap_trans_do(autorid_db,
323 idmap_autorid_get_domainrange,
324 &domaincfg);
326 if (!NT_STATUS_IS_OK(ret)) {
327 DEBUG(3, ("Could not determine range for domain, "
328 "check previous messages for reason\n"));
329 goto failure;
332 ret = idmap_autorid_sid_to_id(global, &domaincfg, ids[i]);
334 if ((!NT_STATUS_IS_OK(ret)) &&
335 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
336 /* some fatal error occurred, log it */
337 DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
338 sid_string_dbg(ids[i]->sid)));
339 goto failure;
343 talloc_free(ctx);
344 return NT_STATUS_OK;
346 failure:
347 talloc_free(ctx);
348 return ret;
353 * open and initialize the database which stores the ranges for the domains
355 static NTSTATUS idmap_autorid_db_init(void)
357 int32_t hwm;
359 if (autorid_db) {
360 /* its already open */
361 return NT_STATUS_OK;
364 /* Open idmap repository */
365 autorid_db = db_open(NULL, state_path("autorid.tdb"), 0,
366 TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
368 if (!autorid_db) {
369 DEBUG(0, ("Unable to open idmap_autorid database '%s'\n",
370 state_path("autorid.tdb")));
371 return NT_STATUS_UNSUCCESSFUL;
374 /* Initialize high water mark for the currently used range to 0 */
375 hwm = dbwrap_fetch_int32(autorid_db, HWM);
376 if ((hwm < 0)) {
377 if (!NT_STATUS_IS_OK
378 (dbwrap_trans_store_int32(autorid_db, HWM, 0))) {
379 DEBUG(0,
380 ("Unable to initialise HWM in autorid "
381 "database\n"));
382 return NT_STATUS_INTERNAL_DB_ERROR;
386 return NT_STATUS_OK;
389 static struct autorid_global_config *idmap_autorid_loadconfig(TALLOC_CTX * ctx)
392 TDB_DATA data;
393 struct autorid_global_config *cfg;
394 unsigned long minvalue, rangesize, maxranges;
396 data = dbwrap_fetch_bystring(autorid_db, ctx, CONFIGKEY);
398 if (!data.dptr) {
399 DEBUG(10, ("No saved config found\n"));
400 return NULL;
403 cfg = TALLOC_ZERO_P(ctx, struct autorid_global_config);
404 if (!cfg) {
405 return NULL;
408 if (sscanf((char *)data.dptr,
409 "minvalue:%lu rangesize:%lu maxranges:%lu",
410 &minvalue, &rangesize, &maxranges) != 3) {
411 DEBUG(1,
412 ("Found invalid configuration data"
413 "creating new config\n"));
414 return NULL;
417 cfg->minvalue = minvalue;
418 cfg->rangesize = rangesize;
419 cfg->maxranges = maxranges;
421 DEBUG(10, ("Loaded previously stored configuration "
422 "minvalue:%d rangesize:%d\n",
423 cfg->minvalue, cfg->rangesize));
425 return cfg;
429 static NTSTATUS idmap_autorid_saveconfig(struct autorid_global_config *cfg)
432 NTSTATUS status;
433 TDB_DATA data;
434 char *cfgstr;
436 cfgstr =
437 talloc_asprintf(talloc_tos(),
438 "minvalue:%u rangesize:%u maxranges:%u",
439 cfg->minvalue, cfg->rangesize, cfg->maxranges);
441 if (!cfgstr) {
442 return NT_STATUS_NO_MEMORY;
445 data = string_tdb_data(cfgstr);
447 status = dbwrap_trans_store_bystring(autorid_db, CONFIGKEY,
448 data, TDB_REPLACE);
450 talloc_free(cfgstr);
452 return status;
455 static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom,
456 const char *params)
458 struct autorid_global_config *config;
459 struct autorid_global_config *storedconfig = NULL;
460 NTSTATUS status;
461 uint32_t hwm;
463 config = TALLOC_ZERO_P(dom, struct autorid_global_config);
464 if (!config) {
465 DEBUG(0, ("Out of memory!\n"));
466 return NT_STATUS_NO_MEMORY;
469 status = idmap_autorid_db_init();
470 if (!NT_STATUS_IS_OK(status)) {
471 goto error;
474 config->minvalue = dom->low_id;
475 config->rangesize = lp_parm_int(-1, "autorid", "rangesize", 100000);
477 if (config->rangesize < 2000) {
478 DEBUG(1, ("autorid rangesize must be at least 2000\n"));
479 status = NT_STATUS_INVALID_PARAMETER;
480 goto error;
483 config->maxranges = (dom->high_id - dom->low_id + 1) /
484 config->rangesize;
486 if (config->maxranges == 0) {
487 DEBUG(1, ("allowed uid range is smaller then rangesize, "
488 "increase uid range or decrease rangesize\n"));
489 status = NT_STATUS_INVALID_PARAMETER;
490 goto error;
493 /* check if the high-low limit is a multiple of the rangesize */
494 if ((dom->high_id - dom->low_id + 1) % config->rangesize != 0) {
495 DEBUG(5, ("High uid-low uid difference of %d "
496 "is not a multiple of the rangesize %d, "
497 "limiting ranges to lower boundary number of %d\n",
498 (dom->high_id - dom->low_id + 1), config->rangesize,
499 config->maxranges));
502 DEBUG(10, ("Current configuration in config is "
503 "minvalue:%d rangesize:%d maxranges:%d\n",
504 config->minvalue, config->rangesize, config->maxranges));
506 /* read previously stored config and current HWM */
507 storedconfig = idmap_autorid_loadconfig(talloc_tos());
509 if (!dbwrap_fetch_uint32(autorid_db, HWM, &hwm)) {
510 DEBUG(1, ("Fatal error while fetching current "
511 "HWM value!\n"));
512 status = NT_STATUS_INTERNAL_ERROR;
513 goto error;
516 /* did the minimum value or rangesize change? */
517 if (storedconfig &&
518 ((storedconfig->minvalue != config->minvalue) ||
519 (storedconfig->rangesize != config->rangesize))) {
520 DEBUG(1, ("New configuration values for rangesize or "
521 "minimum uid value conflict with previously "
522 "used values! Aborting initialization\n"));
523 status = NT_STATUS_INVALID_PARAMETER;
524 goto error;
528 * has the highest uid value been reduced to setting that is not
529 * sufficient any more for already existing ranges?
531 if (hwm > config->maxranges) {
532 DEBUG(1, ("New upper uid limit is too low to cover "
533 "existing mappings! Aborting initialization\n"));
534 status = NT_STATUS_INVALID_PARAMETER;
535 goto error;
538 status = idmap_autorid_saveconfig(config);
540 if (!NT_STATUS_IS_OK(status)) {
541 DEBUG(1, ("Failed to store configuration data!\n"));
542 goto error;
545 DEBUG(5, ("%d domain ranges with a size of %d are available\n",
546 config->maxranges, config->rangesize));
548 dom->private_data = config;
550 if (!NT_STATUS_IS_OK(status)) {
551 goto error;
554 return NT_STATUS_OK;
556 error:
557 talloc_free(config);
558 talloc_free(storedconfig);
560 return status;
564 Close the idmap tdb instance
566 static struct idmap_methods autorid_methods = {
567 .init = idmap_autorid_initialize,
568 .unixids_to_sids = idmap_autorid_unixids_to_sids,
569 .sids_to_unixids = idmap_autorid_sids_to_unixids,
572 NTSTATUS idmap_autorid_init(void)
574 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
575 "autorid", &autorid_methods);