s3:idmap: add a new ID mapping module autorid
[Samba.git] / source3 / winbindd / idmap_autorid.c
blobc856ae00c37bf87034944e3554acfff5a0cfbe8b
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
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 struct autorid_global_config {
36 uint32_t minvalue;
37 uint32_t rangesize;
38 uint32_t maxranges;
41 struct autorid_domain_config {
42 struct dom_sid sid;
43 uint32_t domainnum;
44 struct autorid_global_config *globalcfg;
47 /* handle to the tdb storing domain <-> range assignments */
48 static struct db_context *autorid_db;
50 static NTSTATUS idmap_autorid_get_domainrange(struct db_context *db,
51 void *private_data)
53 NTSTATUS ret;
54 uint32_t domainnum, hwm;
55 char *sidstr, *numstr;
56 struct autorid_domain_config *cfg;
58 cfg = (struct autorid_domain_config *)private_data;
59 sidstr = dom_sid_string(talloc_tos(), &(cfg->sid));
61 if (!sidstr) {
62 return NT_STATUS_NO_MEMORY;
65 if (!dbwrap_fetch_uint32(db, sidstr, &domainnum)) {
66 DEBUG(10, ("Acquiring new range for domain %s\n", sidstr));
68 /* fetch the current HWM */
69 if (!dbwrap_fetch_uint32(db, HWM, &hwm)) {
70 DEBUG(1, ("Fatal error while fetching current "
71 "HWM value!\n"));
72 goto error;
75 /* do we have a range left? */
76 if (hwm >= cfg->globalcfg->maxranges) {
77 DEBUG(1, ("No more domain ranges available!\n"));
78 ret = NT_STATUS_NO_MEMORY;
79 goto error;
82 /* increase the HWM */
83 ret = dbwrap_change_uint32_atomic(db, HWM, &domainnum, 1);
84 if (!NT_STATUS_IS_OK(ret)) {
85 DEBUG(1, ("Fatal error while fetching a new "
86 "domain range value!\n"));
87 goto error;
90 /* store away the new mapping in both directions */
91 ret = dbwrap_trans_store_uint32(db, sidstr, domainnum);
92 if (!NT_STATUS_IS_OK(ret)) {
93 DEBUG(1, ("Fatal error while storing new "
94 "domain->range assignment!\n"));
95 goto error;
98 numstr = talloc_asprintf(db, "%u", domainnum);
99 if (!numstr) {
100 ret = NT_STATUS_NO_MEMORY;
101 goto error;
104 ret = dbwrap_trans_store_bystring(db, numstr,
105 string_term_tdb_data(sidstr),
106 TDB_INSERT);
107 if (!NT_STATUS_IS_OK(ret)) {
108 talloc_free(numstr);
109 DEBUG(1, ("Fatal error while storing "
110 "new domain->range assignment!\n"));
111 goto error;
113 talloc_free(numstr);
114 DEBUG(5, ("Acquired new range #%d for domain %s\n",
115 domainnum, sidstr));
118 DEBUG(10, ("Using range #%d for domain %s\n", domainnum, sidstr));
119 cfg->domainnum = domainnum;
121 talloc_free(sidstr);
122 return NT_STATUS_OK;
124 error:
125 talloc_free(sidstr);
126 return ret;
130 static NTSTATUS idmap_autorid_id_to_sid(TALLOC_CTX * memctx,
131 struct autorid_global_config *cfg,
132 struct id_map *map)
134 uint32_t range;
135 TDB_DATA data;
136 char *keystr;
137 struct dom_sid sid;
139 /* can this be one of our ids? */
140 if (map->xid.id < cfg->minvalue) {
141 DEBUG(10, ("id %d is lower than minimum value, "
142 "ignoring mapping request\n", map->xid.id));
143 map->status = ID_UNKNOWN;
144 return NT_STATUS_OK;
147 if (map->xid.id > (cfg->minvalue + cfg->rangesize * cfg->maxranges)) {
148 DEBUG(10, ("id %d is outside of maximum id value, "
149 "ignoring mapping request\n", map->xid.id));
150 map->status = ID_UNKNOWN;
151 return NT_STATUS_OK;
154 /* determine the range of this uid */
155 range = ((map->xid.id - cfg->minvalue) / cfg->rangesize);
157 keystr = talloc_asprintf(memctx, "%u", range);
158 if (!keystr) {
159 return NT_STATUS_NO_MEMORY;
162 data = dbwrap_fetch_bystring(autorid_db, memctx, 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 } else {
169 string_to_sid(&sid, (const char *)data.dptr);
171 sid_compose(map->sid, &sid,
172 (map->xid.id - cfg->minvalue -
173 range * cfg->rangesize));
175 /* We **really** should have some way of validating
176 the SID exists and is the correct type here. But
177 that is a deficiency in the idmap_rid design. */
179 map->status = ID_MAPPED;
181 return NT_STATUS_OK;
184 /**********************************
185 Single sid to id lookup function.
186 **********************************/
188 static NTSTATUS idmap_autorid_sid_to_id(TALLOC_CTX * memctx,
189 struct autorid_global_config *global,
190 struct autorid_domain_config *domain,
191 struct id_map *map)
193 uint32_t rid;
195 sid_peek_rid(map->sid, &rid);
197 /* if the rid is higher than the size of the range, we cannot map it */
198 if (rid >= global->rangesize) {
199 map->status = ID_UNKNOWN;
200 DEBUG(2, ("RID %d is larger then size of range (%d), "
201 "user cannot be mapped\n", rid, global->rangesize));
202 return NT_STATUS_UNSUCCESSFUL;
204 map->xid.id = global->minvalue +
205 (global->rangesize * domain->domainnum)+rid;
207 /* We **really** should have some way of validating
208 the SID exists and is the correct type here. But
209 that is a deficiency in the idmap_rid design. */
211 map->status = ID_MAPPED;
213 return NT_STATUS_OK;
216 /**********************************
217 lookup a set of unix ids.
218 **********************************/
220 static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
221 struct id_map **ids)
223 struct autorid_global_config *globalcfg;
224 TALLOC_CTX *ctx;
225 NTSTATUS ret;
226 int i;
228 /* initialize the status to avoid surprise */
229 for (i = 0; ids[i]; i++) {
230 ids[i]->status = ID_UNKNOWN;
233 globalcfg = talloc_get_type(dom->private_data,
234 struct autorid_global_config);
236 ctx = talloc_new(dom);
237 if (!ctx) {
238 DEBUG(0, ("Out of memory!\n"));
239 return NT_STATUS_NO_MEMORY;
242 for (i = 0; ids[i]; i++) {
244 ret = idmap_autorid_id_to_sid(ctx, 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));
251 goto failure;
255 talloc_free(ctx);
256 return NT_STATUS_OK;
258 failure:
259 talloc_free(ctx);
260 return ret;
264 /**********************************
265 lookup a set of sids.
266 **********************************/
268 static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
269 struct id_map **ids)
271 struct autorid_global_config *global;
272 struct autorid_domain_config *domaincfg;
273 struct dom_sid *domainsid;
274 struct winbindd_tdc_domain *domain;
275 uint32_t rid;
276 TALLOC_CTX *ctx;
277 NTSTATUS ret;
278 int i;
280 ctx = talloc_new(dom);
281 domainsid = talloc(ctx, struct dom_sid);
282 if (!ctx || !domainsid) {
283 DEBUG(0, ("Out of memory!\n"));
284 ret = NT_STATUS_NO_MEMORY;
285 goto failure;
288 if ((domaincfg = TALLOC_ZERO_P(ctx,
289 struct autorid_domain_config)) == NULL) {
290 DEBUG(0, ("Out of memory!\n"));
291 ret = NT_STATUS_NO_MEMORY;
292 goto failure;
295 /* initialize the status to avoid surprise */
296 for (i = 0; ids[i]; i++) {
297 ids[i]->status = ID_UNKNOWN;
300 global = talloc_get_type(dom->private_data,
301 struct autorid_global_config);
303 for (i = 0; ids[i]; i++) {
304 ZERO_STRUCTP(domainsid);
305 sid_copy(domainsid, ids[i]->sid);
306 if (!sid_split_rid(domainsid, &rid)) {
307 DEBUG(4, ("Could not determine domain SID from %s, "
308 "ignoring mapping request\n",
309 sid_string_dbg(ids[i]->sid)));
310 continue;
313 domain = wcache_tdc_fetch_domainbysid(ctx, domainsid);
314 if (domain == NULL) {
315 DEBUG(10, ("Ignoring unknown domain sid %s\n",
316 sid_string_dbg(domainsid)));
317 continue;
320 ZERO_STRUCTP(domaincfg);
321 domaincfg->sid = domain->sid;
322 domaincfg->globalcfg = global;
324 ret = dbwrap_trans_do(autorid_db, idmap_autorid_get_domainrange,
325 domaincfg);
327 if (!NT_STATUS_IS_OK(ret)) {
328 DEBUG(3, ("Could not determine range for domain, "
329 "check previous messages for reason\n"));
330 goto failure;
333 ret = idmap_autorid_sid_to_id(ctx, global, domaincfg, ids[i]);
335 if ((!NT_STATUS_IS_OK(ret)) &&
336 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
337 /* some fatal error occurred, log it */
338 DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
339 sid_string_dbg(ids[i]->sid)));
340 goto failure;
344 talloc_free(ctx);
345 return NT_STATUS_OK;
347 failure:
348 talloc_free(ctx);
349 return ret;
354 * open and initialize the database which stores the ranges for the domains
356 static NTSTATUS idmap_autorid_db_init(void)
358 int32_t hwm;
360 if (autorid_db) {
361 /* its already open */
362 return NT_STATUS_OK;
365 /* Open idmap repository */
366 autorid_db = db_open(NULL, state_path("autorid.tdb"), 0,
367 TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
369 if (!autorid_db) {
370 DEBUG(0, ("Unable to open idmap_autorid database '%s'\n",
371 state_path("autorid.tdb")));
372 return NT_STATUS_UNSUCCESSFUL;
375 /* Initialize high water mark for the currently used range to 0 */
376 hwm = dbwrap_fetch_int32(autorid_db, HWM);
377 if ((hwm < 0)) {
378 if (!NT_STATUS_IS_OK
379 (dbwrap_trans_store_int32(autorid_db, HWM, 0))) {
380 DEBUG(0,
381 ("Unable to initialise HWM in autorid "
382 "database\n"));
383 return NT_STATUS_INTERNAL_DB_ERROR;
387 return NT_STATUS_OK;
390 static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom,
391 const char *params)
393 struct autorid_global_config *config;
394 NTSTATUS status;
396 config = TALLOC_ZERO_P(dom, struct autorid_global_config);
397 if (!config) {
398 DEBUG(0, ("Out of memory!\n"));
399 return NT_STATUS_NO_MEMORY;
402 status = idmap_autorid_db_init();
403 if (!NT_STATUS_IS_OK(status)) {
404 goto error;
407 config->minvalue = dom->low_id;
408 config->rangesize = lp_parm_int(-1, "autorid", "rangesize", 100000);
410 if (config->rangesize < 2000) {
411 DEBUG(1, ("autorid rangesize must be at least 2000\n"));
412 status = NT_STATUS_INVALID_PARAMETER;
413 goto error;
416 config->maxranges = (dom->high_id - dom->low_id + 1) /
417 config->rangesize;
419 if (config->maxranges == 0) {
420 DEBUG(1, ("allowed uid range is smaller then rangesize, "
421 "increase uid range or decrease rangesize\n"));
422 status = NT_STATUS_INVALID_PARAMETER;
423 goto error;
426 /* check if the high-low limit is a multiple of the rangesize */
427 if ((dom->high_id - dom->low_id + 1) % config->rangesize != 0) {
428 DEBUG(5, ("High uid-low uid difference of %d "
429 "is not a multiple of the rangesize %d, "
430 "limiting ranges to lower boundary number of %d\n",
431 (dom->high_id - dom->low_id + 1), config->rangesize,
432 config->maxranges));
435 DEBUG(5, ("%d domain ranges with a size of %d are available\n",
436 config->maxranges, config->rangesize));
438 dom->private_data = config;
440 if (!NT_STATUS_IS_OK(status)) {
441 goto error;
444 return NT_STATUS_OK;
446 error:
447 talloc_free(config);
448 return status;
452 Close the idmap tdb instance
454 static NTSTATUS idmap_autorid_close(struct idmap_domain *dom)
456 /* don't do anything */
457 return NT_STATUS_OK;
460 static struct idmap_methods autorid_methods = {
461 .init = idmap_autorid_initialize,
462 .unixids_to_sids = idmap_autorid_unixids_to_sids,
463 .sids_to_unixids = idmap_autorid_sids_to_unixids,
464 .close_fn = idmap_autorid_close
467 NTSTATUS idmap_autorid_init(void)
469 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
470 "autorid", &autorid_methods);