VERSION: Bump version up to 3.6.20.
[Samba.git] / source3 / winbindd / idmap_autorid.c
blob6bb35de744ef187611ad48a89568782f54d5c5b2
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 "system/filesys.h"
27 #include "winbindd.h"
28 #include "dbwrap.h"
29 #include "idmap.h"
30 #include "../libcli/security/dom_sid.h"
31 #include "util_tdb.h"
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_IDMAP
36 #define HWM "NEXT RANGE"
37 #define ALLOC_HWM_UID "NEXT ALLOC UID"
38 #define ALLOC_HWM_GID "NEXT ALLOC GID"
39 #define ALLOC_RANGE "ALLOC"
40 #define CONFIGKEY "CONFIG"
42 struct autorid_global_config {
43 uint32_t minvalue;
44 uint32_t rangesize;
45 uint32_t maxranges;
48 struct autorid_domain_config {
49 fstring sid;
50 uint32_t domainnum;
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,
58 void *private_data)
60 NTSTATUS ret;
61 uint32_t domainnum, hwm;
62 char *numstr;
63 struct autorid_domain_config *cfg;
65 cfg = (struct autorid_domain_config *)private_data;
67 if (!dbwrap_fetch_uint32(db, cfg->sid, &domainnum)) {
68 DEBUG(10, ("Acquiring new range for domain %s\n", cfg->sid));
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, cfg->sid, 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(cfg->sid), TDB_INSERT);
110 talloc_free(numstr);
111 if (!NT_STATUS_IS_OK(ret)) {
112 DEBUG(1, ("Fatal error while storing "
113 "new domain->range assignment!\n"));
114 goto error;
116 DEBUG(5, ("Acquired new range #%d for domain %s\n",
117 domainnum, cfg->sid));
120 DEBUG(10, ("Using range #%d for domain %s\n", domainnum, cfg->sid));
121 cfg->domainnum = domainnum;
123 return NT_STATUS_OK;
125 error:
126 return ret;
130 static NTSTATUS idmap_autorid_id_to_sid(struct autorid_global_config *cfg,
131 struct id_map *map)
133 uint32_t range;
134 TDB_DATA data;
135 char *keystr;
136 struct dom_sid sid;
138 /* can this be one of our ids? */
139 if (map->xid.id < cfg->minvalue) {
140 DEBUG(10, ("id %d is lower than minimum value, "
141 "ignoring mapping request\n", map->xid.id));
142 map->status = ID_UNKNOWN;
143 return NT_STATUS_OK;
146 if (map->xid.id > (cfg->minvalue + cfg->rangesize * cfg->maxranges)) {
147 DEBUG(10, ("id %d is outside of maximum id value, "
148 "ignoring mapping request\n", map->xid.id));
149 map->status = ID_UNKNOWN;
150 return NT_STATUS_OK;
153 /* determine the range of this uid */
154 range = ((map->xid.id - cfg->minvalue) / cfg->rangesize);
156 keystr = talloc_asprintf(talloc_tos(), "%u", range);
157 if (!keystr) {
158 return NT_STATUS_NO_MEMORY;
161 data = dbwrap_fetch_bystring(autorid_db, talloc_tos(), keystr);
162 TALLOC_FREE(keystr);
164 if (!data.dptr) {
165 DEBUG(4, ("id %d belongs to range %d which does not have "
166 "domain mapping, ignoring mapping request\n",
167 map->xid.id, range));
168 TALLOC_FREE(data.dptr);
169 map->status = ID_UNKNOWN;
170 return NT_STATUS_OK;
173 if (strncmp((const char *)data.dptr,
174 ALLOC_RANGE,
175 strlen(ALLOC_RANGE)) == 0) {
176 /* this is from the alloc range, there is no mapping back */
177 DEBUG(5, ("id %d belongs to alloc range, cannot map back\n",
178 map->xid.id));
179 TALLOC_FREE(data.dptr);
180 map->status = ID_UNKNOWN;
181 return NT_STATUS_OK;
184 string_to_sid(&sid, (const char *)data.dptr);
185 TALLOC_FREE(data.dptr);
187 sid_compose(map->sid, &sid,
188 (map->xid.id - cfg->minvalue -
189 range * cfg->rangesize));
191 /* We **really** should have some way of validating
192 the SID exists and is the correct type here. But
193 that is a deficiency in the idmap_rid design. */
195 map->status = ID_MAPPED;
196 return NT_STATUS_OK;
199 /**********************************
200 Single sid to id lookup function.
201 **********************************/
203 static NTSTATUS idmap_autorid_sid_to_id(struct autorid_global_config *global,
204 struct autorid_domain_config *domain,
205 struct id_map *map)
207 uint32_t rid;
209 sid_peek_rid(map->sid, &rid);
211 /* if the rid is higher than the size of the range, we cannot map it */
212 if (rid >= global->rangesize) {
213 map->status = ID_UNKNOWN;
214 DEBUG(2, ("RID %d is larger then size of range (%d), "
215 "user cannot be mapped\n", rid, global->rangesize));
216 return NT_STATUS_UNSUCCESSFUL;
218 map->xid.id = global->minvalue +
219 (global->rangesize * domain->domainnum)+rid;
221 /* We **really** should have some way of validating
222 the SID exists and is the correct type here. But
223 that is a deficiency in the idmap_rid design. */
225 map->status = ID_MAPPED;
227 return NT_STATUS_OK;
230 /**********************************
231 lookup a set of unix ids.
232 **********************************/
234 static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
235 struct id_map **ids)
237 struct autorid_global_config *globalcfg;
238 NTSTATUS ret;
239 int i;
241 /* initialize the status to avoid surprise */
242 for (i = 0; ids[i]; i++) {
243 ids[i]->status = ID_UNKNOWN;
246 globalcfg = talloc_get_type(dom->private_data,
247 struct autorid_global_config);
249 for (i = 0; ids[i]; i++) {
251 ret = idmap_autorid_id_to_sid(globalcfg, ids[i]);
253 if ((!NT_STATUS_IS_OK(ret)) &&
254 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
255 /* some fatal error occurred, log it */
256 DEBUG(3, ("Unexpected error resolving an ID "
257 " (%d)\n", ids[i]->xid.id));
258 goto failure;
261 return NT_STATUS_OK;
263 failure:
264 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 NTSTATUS ret;
276 int i;
278 /* initialize the status to avoid surprise */
279 for (i = 0; ids[i]; i++) {
280 ids[i]->status = ID_UNKNOWN;
283 global = talloc_get_type(dom->private_data,
284 struct autorid_global_config);
286 for (i = 0; ids[i]; i++) {
287 struct winbindd_tdc_domain *domain;
288 struct autorid_domain_config domaincfg;
289 uint32_t rid;
290 struct dom_sid domainsid;
292 ZERO_STRUCT(domaincfg);
294 sid_copy(&domainsid, ids[i]->sid);
295 if (!sid_split_rid(&domainsid, &rid)) {
296 DEBUG(4, ("Could not determine domain SID from %s, "
297 "ignoring mapping request\n",
298 sid_string_dbg(ids[i]->sid)));
299 continue;
303 * Check if the domain is around
305 domain = wcache_tdc_fetch_domainbysid(talloc_tos(),
306 &domainsid);
307 if (domain == NULL) {
308 DEBUG(10, ("Ignoring unknown domain sid %s\n",
309 sid_string_dbg(&domainsid)));
310 continue;
312 TALLOC_FREE(domain);
314 domaincfg.globalcfg = global;
315 sid_to_fstring(domaincfg.sid, &domainsid);
317 ret = dbwrap_trans_do(autorid_db,
318 idmap_autorid_get_domainrange,
319 &domaincfg);
321 if (!NT_STATUS_IS_OK(ret)) {
322 DEBUG(3, ("Could not determine range for domain, "
323 "check previous messages for reason\n"));
324 goto failure;
327 ret = idmap_autorid_sid_to_id(global, &domaincfg, ids[i]);
329 if ((!NT_STATUS_IS_OK(ret)) &&
330 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
331 /* some fatal error occurred, log it */
332 DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
333 sid_string_dbg(ids[i]->sid)));
334 goto failure;
337 return NT_STATUS_OK;
339 failure:
340 return ret;
344 /* initialize the given HWM to 0 if it does not exist yet */
345 static NTSTATUS idmap_autorid_init_hwm(const char *hwm) {
347 NTSTATUS status;
348 int32_t hwmval;
350 hwmval = dbwrap_fetch_int32(autorid_db, hwm);
351 if ((hwmval < 0)) {
352 status = dbwrap_trans_store_int32(autorid_db, hwm, 0);
353 if (!NT_STATUS_IS_OK(status)) {
354 DEBUG(0,
355 ("Unable to initialise HWM (%s) in autorid "
356 "database: %s\n", hwm, nt_errstr(status)));
357 return NT_STATUS_INTERNAL_DB_ERROR;
361 return NT_STATUS_OK;
365 * open and initialize the database which stores the ranges for the domains
367 static NTSTATUS idmap_autorid_db_init(void)
369 NTSTATUS status;
371 if (autorid_db) {
372 /* its already open */
373 return NT_STATUS_OK;
376 /* Open idmap repository */
377 autorid_db = db_open(NULL, state_path("autorid.tdb"), 0,
378 TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
380 if (!autorid_db) {
381 DEBUG(0, ("Unable to open idmap_autorid database '%s'\n",
382 state_path("autorid.tdb")));
383 return NT_STATUS_UNSUCCESSFUL;
386 /* Initialize high water mark for the currently used range to 0 */
388 status = idmap_autorid_init_hwm(HWM);
389 NT_STATUS_NOT_OK_RETURN(status);
391 status = idmap_autorid_init_hwm(ALLOC_HWM_UID);
392 NT_STATUS_NOT_OK_RETURN(status);
394 status = idmap_autorid_init_hwm(ALLOC_HWM_GID);
396 return status;
399 static struct autorid_global_config *idmap_autorid_loadconfig(TALLOC_CTX * ctx)
402 TDB_DATA data;
403 struct autorid_global_config *cfg;
404 unsigned long minvalue, rangesize, maxranges;
406 data = dbwrap_fetch_bystring(autorid_db, ctx, CONFIGKEY);
408 if (!data.dptr) {
409 DEBUG(10, ("No saved config found\n"));
410 return NULL;
413 cfg = TALLOC_ZERO_P(ctx, struct autorid_global_config);
414 if (!cfg) {
415 return NULL;
418 if (sscanf((char *)data.dptr,
419 "minvalue:%lu rangesize:%lu maxranges:%lu",
420 &minvalue, &rangesize, &maxranges) != 3) {
421 DEBUG(1,
422 ("Found invalid configuration data"
423 "creating new config\n"));
424 return NULL;
427 cfg->minvalue = minvalue;
428 cfg->rangesize = rangesize;
429 cfg->maxranges = maxranges;
431 DEBUG(10, ("Loaded previously stored configuration "
432 "minvalue:%d rangesize:%d\n",
433 cfg->minvalue, cfg->rangesize));
435 return cfg;
439 static NTSTATUS idmap_autorid_saveconfig(struct autorid_global_config *cfg)
442 NTSTATUS status;
443 TDB_DATA data;
444 char *cfgstr;
446 cfgstr =
447 talloc_asprintf(talloc_tos(),
448 "minvalue:%u rangesize:%u maxranges:%u",
449 cfg->minvalue, cfg->rangesize, cfg->maxranges);
451 if (!cfgstr) {
452 return NT_STATUS_NO_MEMORY;
455 data = string_tdb_data(cfgstr);
457 status = dbwrap_trans_store_bystring(autorid_db, CONFIGKEY,
458 data, TDB_REPLACE);
460 talloc_free(cfgstr);
462 return status;
465 static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
467 struct autorid_global_config *config;
468 struct autorid_global_config *storedconfig = NULL;
469 NTSTATUS status;
470 uint32_t hwm;
472 if (!strequal(dom->name, "*")) {
473 DEBUG(0, ("idmap_autorid_initialize: Error: autorid configured "
474 "for domain '%s'. But autorid can only be used for "
475 "the default idmap configuration.\n", dom->name));
476 return NT_STATUS_INVALID_PARAMETER;
479 config = TALLOC_ZERO_P(dom, struct autorid_global_config);
480 if (!config) {
481 DEBUG(0, ("Out of memory!\n"));
482 return NT_STATUS_NO_MEMORY;
485 status = idmap_autorid_db_init();
486 if (!NT_STATUS_IS_OK(status)) {
487 goto error;
490 config->minvalue = dom->low_id;
491 config->rangesize = lp_parm_int(-1, "idmap config *", "rangesize", 100000);
493 if (config->rangesize < 2000) {
494 DEBUG(1, ("autorid rangesize must be at least 2000\n"));
495 status = NT_STATUS_INVALID_PARAMETER;
496 goto error;
499 config->maxranges = (dom->high_id - dom->low_id + 1) /
500 config->rangesize;
502 if (config->maxranges == 0) {
503 DEBUG(1, ("allowed uid range is smaller then rangesize, "
504 "increase uid range or decrease rangesize\n"));
505 status = NT_STATUS_INVALID_PARAMETER;
506 goto error;
509 /* check if the high-low limit is a multiple of the rangesize */
510 if ((dom->high_id - dom->low_id + 1) % config->rangesize != 0) {
511 DEBUG(5, ("High uid-low uid difference of %d "
512 "is not a multiple of the rangesize %d, "
513 "limiting ranges to lower boundary number of %d\n",
514 (dom->high_id - dom->low_id + 1), config->rangesize,
515 config->maxranges));
518 DEBUG(10, ("Current configuration in config is "
519 "minvalue:%d rangesize:%d maxranges:%d\n",
520 config->minvalue, config->rangesize, config->maxranges));
522 /* read previously stored config and current HWM */
523 storedconfig = idmap_autorid_loadconfig(talloc_tos());
525 if (!dbwrap_fetch_uint32(autorid_db, HWM, &hwm)) {
526 DEBUG(1, ("Fatal error while fetching current "
527 "HWM value!\n"));
528 status = NT_STATUS_INTERNAL_ERROR;
529 goto error;
532 /* did the minimum value or rangesize change? */
533 if (storedconfig &&
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;
540 goto error;
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;
551 goto error;
554 status = idmap_autorid_saveconfig(config);
556 if (!NT_STATUS_IS_OK(status)) {
557 DEBUG(1, ("Failed to store configuration data!\n"));
558 goto error;
561 DEBUG(5, ("%d domain ranges with a size of %d are available\n",
562 config->maxranges, config->rangesize));
564 dom->private_data = config;
566 goto done;
568 error:
569 talloc_free(config);
571 done:
572 talloc_free(storedconfig);
574 return status;
577 static NTSTATUS idmap_autorid_allocate_id(struct idmap_domain *dom,
578 struct unixid *xid) {
580 NTSTATUS ret;
581 struct autorid_global_config *globalcfg;
582 struct autorid_domain_config domaincfg;
583 uint32_t hwm;
584 const char *hwmkey;
586 if (!strequal(dom->name, "*")) {
587 DEBUG(3, ("idmap_autorid_allocate_id: "
588 "Refusing creation of mapping for domain'%s'. "
589 "Currently only supported for the default "
590 "domain \"*\".\n",
591 dom->name));
592 return NT_STATUS_NOT_IMPLEMENTED;
595 if ((xid->type != ID_TYPE_UID) && (xid->type != ID_TYPE_GID)) {
596 return NT_STATUS_INVALID_PARAMETER;
600 globalcfg = talloc_get_type(dom->private_data,
601 struct autorid_global_config);
603 /* fetch the range for the allocation pool */
605 ZERO_STRUCT(domaincfg);
607 domaincfg.globalcfg = globalcfg;
608 fstrcpy(domaincfg.sid, ALLOC_RANGE);
610 ret = dbwrap_trans_do(autorid_db,
611 idmap_autorid_get_domainrange,
612 &domaincfg);
613 if (!NT_STATUS_IS_OK(ret)) {
614 DEBUG(3, ("Could not determine range for allocation pool, "
615 "check previous messages for reason\n"));
616 return ret;
619 /* fetch the current HWM */
620 hwmkey = (xid->type==ID_TYPE_UID)?ALLOC_HWM_UID:ALLOC_HWM_GID;
622 if (!dbwrap_fetch_uint32(autorid_db, hwmkey, &hwm)) {
623 DEBUG(1, ("Failed to fetch current allocation HWM value: %s\n",
624 nt_errstr(ret)));
625 return NT_STATUS_INTERNAL_ERROR;
628 if (hwm >= globalcfg->rangesize) {
629 DEBUG(1, ("allocation range is depleted!\n"));
630 return NT_STATUS_NO_MEMORY;
633 ret = dbwrap_change_uint32_atomic(autorid_db, hwmkey, &(xid->id), 1);
634 if (!NT_STATUS_IS_OK(ret)) {
635 DEBUG(1, ("Fatal error while allocating new ID!\n"));
636 return ret;
639 xid->id = globalcfg->minvalue +
640 globalcfg->rangesize * domaincfg.domainnum +
641 xid->id;
643 DEBUG(10, ("Returned new %s %d from allocation range\n",
644 (xid->type==ID_TYPE_UID)?"uid":"gid", xid->id));
646 return ret;
650 Close the idmap tdb instance
652 static struct idmap_methods autorid_methods = {
653 .init = idmap_autorid_initialize,
654 .unixids_to_sids = idmap_autorid_unixids_to_sids,
655 .sids_to_unixids = idmap_autorid_sids_to_unixids,
656 .allocate_id = idmap_autorid_allocate_id
659 NTSTATUS idmap_autorid_init(void)
661 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
662 "autorid", &autorid_methods);