source: trunk/server/source3/winbindd/idmap_autorid.c

Last change on this file was 751, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.9

File size: 17.3 KB
Line 
1/*
2 * idmap_autorid: static map between Active Directory/NT RIDs
3 * and RFC 2307 accounts
4 *
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
7 *
8 * Copyright (C) Christian Ambach, 2010-2011
9 *
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.
14 *
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.
19 *
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/>.
22 *
23 */
24
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"
32
33#undef DBGC_CLASS
34#define DBGC_CLASS DBGC_IDMAP
35
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"
41
42struct autorid_global_config {
43 uint32_t minvalue;
44 uint32_t rangesize;
45 uint32_t maxranges;
46};
47
48struct autorid_domain_config {
49 fstring sid;
50 uint32_t domainnum;
51 struct autorid_global_config *globalcfg;
52};
53
54/* handle to the tdb storing domain <-> range assignments */
55static struct db_context *autorid_db;
56
57static NTSTATUS idmap_autorid_get_domainrange(struct db_context *db,
58 void *private_data)
59{
60 NTSTATUS ret;
61 uint32_t domainnum, hwm;
62 char *numstr;
63 struct autorid_domain_config *cfg;
64
65 cfg = (struct autorid_domain_config *)private_data;
66
67 if (!dbwrap_fetch_uint32(db, cfg->sid, &domainnum)) {
68 DEBUG(10, ("Acquiring new range for domain %s\n", cfg->sid));
69
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;
76 }
77
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;
83 }
84
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;
91 }
92
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;
99 }
100
101 numstr = talloc_asprintf(db, "%u", domainnum);
102 if (!numstr) {
103 ret = NT_STATUS_NO_MEMORY;
104 goto error;
105 }
106
107 ret = dbwrap_trans_store_bystring(db, numstr,
108 string_term_tdb_data(cfg->sid), TDB_INSERT);
109
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;
115 }
116 DEBUG(5, ("Acquired new range #%d for domain %s\n",
117 domainnum, cfg->sid));
118 }
119
120 DEBUG(10, ("Using range #%d for domain %s\n", domainnum, cfg->sid));
121 cfg->domainnum = domainnum;
122
123 return NT_STATUS_OK;
124
125 error:
126 return ret;
127
128}
129
130static NTSTATUS idmap_autorid_id_to_sid(struct autorid_global_config *cfg,
131 struct id_map *map)
132{
133 uint32_t range;
134 TDB_DATA data;
135 char *keystr;
136 struct dom_sid sid;
137
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;
144 }
145
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;
151 }
152
153 /* determine the range of this uid */
154 range = ((map->xid.id - cfg->minvalue) / cfg->rangesize);
155
156 keystr = talloc_asprintf(talloc_tos(), "%u", range);
157 if (!keystr) {
158 return NT_STATUS_NO_MEMORY;
159 }
160
161 data = dbwrap_fetch_bystring(autorid_db, talloc_tos(), keystr);
162 TALLOC_FREE(keystr);
163
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;
171 }
172
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;
182 }
183
184 string_to_sid(&sid, (const char *)data.dptr);
185 TALLOC_FREE(data.dptr);
186
187 sid_compose(map->sid, &sid,
188 (map->xid.id - cfg->minvalue -
189 range * cfg->rangesize));
190
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. */
194
195 map->status = ID_MAPPED;
196 return NT_STATUS_OK;
197}
198
199/**********************************
200 Single sid to id lookup function.
201**********************************/
202
203static NTSTATUS idmap_autorid_sid_to_id(struct autorid_global_config *global,
204 struct autorid_domain_config *domain,
205 struct id_map *map)
206{
207 uint32_t rid;
208
209 sid_peek_rid(map->sid, &rid);
210
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;
217 }
218 map->xid.id = global->minvalue +
219 (global->rangesize * domain->domainnum)+rid;
220
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. */
224
225 map->status = ID_MAPPED;
226
227 return NT_STATUS_OK;
228}
229
230/**********************************
231 lookup a set of unix ids.
232**********************************/
233
234static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
235 struct id_map **ids)
236{
237 struct autorid_global_config *globalcfg;
238 NTSTATUS ret;
239 int i;
240
241 /* initialize the status to avoid surprise */
242 for (i = 0; ids[i]; i++) {
243 ids[i]->status = ID_UNKNOWN;
244 }
245
246 globalcfg = talloc_get_type(dom->private_data,
247 struct autorid_global_config);
248
249 for (i = 0; ids[i]; i++) {
250
251 ret = idmap_autorid_id_to_sid(globalcfg, ids[i]);
252
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;
259 }
260 }
261 return NT_STATUS_OK;
262
263 failure:
264 return ret;
265}
266
267/**********************************
268 lookup a set of sids.
269**********************************/
270
271static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
272 struct id_map **ids)
273{
274 struct autorid_global_config *global;
275 NTSTATUS ret;
276 int i;
277
278 /* initialize the status to avoid surprise */
279 for (i = 0; ids[i]; i++) {
280 ids[i]->status = ID_UNKNOWN;
281 }
282
283 global = talloc_get_type(dom->private_data,
284 struct autorid_global_config);
285
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;
291
292 ZERO_STRUCT(domaincfg);
293
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;
300 }
301
302 /*
303 * Check if the domain is around
304 */
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;
311 }
312 TALLOC_FREE(domain);
313
314 domaincfg.globalcfg = global;
315 sid_to_fstring(domaincfg.sid, &domainsid);
316
317 ret = dbwrap_trans_do(autorid_db,
318 idmap_autorid_get_domainrange,
319 &domaincfg);
320
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;
325 }
326
327 ret = idmap_autorid_sid_to_id(global, &domaincfg, ids[i]);
328
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;
335 }
336 }
337 return NT_STATUS_OK;
338
339 failure:
340 return ret;
341
342}
343
344/* initialize the given HWM to 0 if it does not exist yet */
345static NTSTATUS idmap_autorid_init_hwm(const char *hwm) {
346
347 NTSTATUS status;
348 int32_t hwmval;
349
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;
358 }
359 }
360
361 return NT_STATUS_OK;
362}
363
364/*
365 * open and initialize the database which stores the ranges for the domains
366 */
367static NTSTATUS idmap_autorid_db_init(void)
368{
369 NTSTATUS status;
370
371 if (autorid_db) {
372 /* its already open */
373 return NT_STATUS_OK;
374 }
375
376 /* Open idmap repository */
377 autorid_db = db_open(NULL, state_path("autorid.tdb"), 0,
378 TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
379
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;
384 }
385
386 /* Initialize high water mark for the currently used range to 0 */
387
388 status = idmap_autorid_init_hwm(HWM);
389 NT_STATUS_NOT_OK_RETURN(status);
390
391 status = idmap_autorid_init_hwm(ALLOC_HWM_UID);
392 NT_STATUS_NOT_OK_RETURN(status);
393
394 status = idmap_autorid_init_hwm(ALLOC_HWM_GID);
395
396 return status;
397}
398
399static struct autorid_global_config *idmap_autorid_loadconfig(TALLOC_CTX * ctx)
400{
401
402 TDB_DATA data;
403 struct autorid_global_config *cfg;
404 unsigned long minvalue, rangesize, maxranges;
405
406 data = dbwrap_fetch_bystring(autorid_db, ctx, CONFIGKEY);
407
408 if (!data.dptr) {
409 DEBUG(10, ("No saved config found\n"));
410 return NULL;
411 }
412
413 cfg = TALLOC_ZERO_P(ctx, struct autorid_global_config);
414 if (!cfg) {
415 return NULL;
416 }
417
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;
425 }
426
427 cfg->minvalue = minvalue;
428 cfg->rangesize = rangesize;
429 cfg->maxranges = maxranges;
430
431 DEBUG(10, ("Loaded previously stored configuration "
432 "minvalue:%d rangesize:%d\n",
433 cfg->minvalue, cfg->rangesize));
434
435 return cfg;
436
437}
438
439static NTSTATUS idmap_autorid_saveconfig(struct autorid_global_config *cfg)
440{
441
442 NTSTATUS status;
443 TDB_DATA data;
444 char *cfgstr;
445
446 cfgstr =
447 talloc_asprintf(talloc_tos(),
448 "minvalue:%u rangesize:%u maxranges:%u",
449 cfg->minvalue, cfg->rangesize, cfg->maxranges);
450
451 if (!cfgstr) {
452 return NT_STATUS_NO_MEMORY;
453 }
454
455 data = string_tdb_data(cfgstr);
456
457 status = dbwrap_trans_store_bystring(autorid_db, CONFIGKEY,
458 data, TDB_REPLACE);
459
460 talloc_free(cfgstr);
461
462 return status;
463}
464
465static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
466{
467 struct autorid_global_config *config;
468 struct autorid_global_config *storedconfig = NULL;
469 NTSTATUS status;
470 uint32_t hwm;
471
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;
477 }
478
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;
483 }
484
485 status = idmap_autorid_db_init();
486 if (!NT_STATUS_IS_OK(status)) {
487 goto error;
488 }
489
490 config->minvalue = dom->low_id;
491 config->rangesize = lp_parm_int(-1, "idmap config *", "rangesize", 100000);
492
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;
497 }
498
499 config->maxranges = (dom->high_id - dom->low_id + 1) /
500 config->rangesize;
501
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;
507 }
508
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));
516 }
517
518 DEBUG(10, ("Current configuration in config is "
519 "minvalue:%d rangesize:%d maxranges:%d\n",
520 config->minvalue, config->rangesize, config->maxranges));
521
522 /* read previously stored config and current HWM */
523 storedconfig = idmap_autorid_loadconfig(talloc_tos());
524
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;
530 }
531
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;
541 }
542
543 /*
544 * has the highest uid value been reduced to setting that is not
545 * sufficient any more for already existing ranges?
546 */
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;
552 }
553
554 status = idmap_autorid_saveconfig(config);
555
556 if (!NT_STATUS_IS_OK(status)) {
557 DEBUG(1, ("Failed to store configuration data!\n"));
558 goto error;
559 }
560
561 DEBUG(5, ("%d domain ranges with a size of %d are available\n",
562 config->maxranges, config->rangesize));
563
564 dom->private_data = config;
565
566 goto done;
567
568error:
569 talloc_free(config);
570
571done:
572 talloc_free(storedconfig);
573
574 return status;
575}
576
577static NTSTATUS idmap_autorid_allocate_id(struct idmap_domain *dom,
578 struct unixid *xid) {
579
580 NTSTATUS ret;
581 struct autorid_global_config *globalcfg;
582 struct autorid_domain_config domaincfg;
583 uint32_t hwm;
584 const char *hwmkey;
585
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;
593 }
594
595 if ((xid->type != ID_TYPE_UID) && (xid->type != ID_TYPE_GID)) {
596 return NT_STATUS_INVALID_PARAMETER;
597 }
598
599
600 globalcfg = talloc_get_type(dom->private_data,
601 struct autorid_global_config);
602
603 /* fetch the range for the allocation pool */
604
605 ZERO_STRUCT(domaincfg);
606
607 domaincfg.globalcfg = globalcfg;
608 fstrcpy(domaincfg.sid, ALLOC_RANGE);
609
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;
617 }
618
619 /* fetch the current HWM */
620 hwmkey = (xid->type==ID_TYPE_UID)?ALLOC_HWM_UID:ALLOC_HWM_GID;
621
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;
626 }
627
628 if (hwm >= globalcfg->rangesize) {
629 DEBUG(1, ("allocation range is depleted!\n"));
630 return NT_STATUS_NO_MEMORY;
631 }
632
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;
637 }
638
639 xid->id = globalcfg->minvalue +
640 globalcfg->rangesize * domaincfg.domainnum +
641 xid->id;
642
643 DEBUG(10, ("Returned new %s %d from allocation range\n",
644 (xid->type==ID_TYPE_UID)?"uid":"gid", xid->id));
645
646 return ret;
647}
648
649/*
650 Close the idmap tdb instance
651*/
652static 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
657};
658
659NTSTATUS idmap_autorid_init(void)
660{
661 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
662 "autorid", &autorid_methods);
663}
Note: See TracBrowser for help on using the repository browser.