s3-smbd: Remove OSF1 support from old (pre-sysquotas) code
[Samba.git] / source3 / winbindd / idmap_autorid.c
blob3f3f649a224b005aef8540d165c04587a4cffd6b
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-2012
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/dbwrap.h"
29 #include "dbwrap/dbwrap_open.h"
30 #include "idmap.h"
31 #include "idmap_rw.h"
32 #include "../libcli/security/dom_sid.h"
33 #include "util_tdb.h"
34 #include "winbindd/idmap_tdb_common.h"
36 #undef DBGC_CLASS
37 #define DBGC_CLASS DBGC_IDMAP
39 #define HWM "NEXT RANGE"
40 #define ALLOC_HWM_UID "NEXT ALLOC UID"
41 #define ALLOC_HWM_GID "NEXT ALLOC GID"
42 #define ALLOC_RANGE "ALLOC"
43 #define CONFIGKEY "CONFIG"
45 struct autorid_global_config {
46 uint32_t minvalue;
47 uint32_t rangesize;
48 uint32_t maxranges;
49 bool ignore_builtin;
52 struct autorid_domain_config {
53 fstring sid;
54 uint32_t domainnum;
55 struct autorid_global_config *globalcfg;
58 /* handle to the tdb storing domain <-> range assignments */
59 static struct db_context *autorid_db;
61 static NTSTATUS idmap_autorid_get_domainrange_action(struct db_context *db,
62 void *private_data)
64 NTSTATUS ret;
65 uint32_t domainnum, hwm;
66 char *numstr;
67 struct autorid_domain_config *cfg;
69 cfg = (struct autorid_domain_config *)private_data;
71 ret = dbwrap_fetch_uint32_bystring(db, cfg->sid, &(cfg->domainnum));
73 if (NT_STATUS_IS_OK(ret)) {
74 /* entry is already present*/
75 return ret;
78 DEBUG(10, ("Acquiring new range for domain %s\n", cfg->sid));
80 /* fetch the current HWM */
81 ret = dbwrap_fetch_uint32_bystring(db, HWM, &hwm);
82 if (!NT_STATUS_IS_OK(ret)) {
83 DEBUG(1, ("Fatal error while fetching current "
84 "HWM value: %s\n", nt_errstr(ret)));
85 ret = NT_STATUS_INTERNAL_ERROR;
86 goto error;
89 /* do we have a range left? */
90 if (hwm >= cfg->globalcfg->maxranges) {
91 DEBUG(1, ("No more domain ranges available!\n"));
92 ret = NT_STATUS_NO_MEMORY;
93 goto error;
96 /* increase the HWM */
97 ret = dbwrap_change_uint32_atomic_bystring(db, HWM, &domainnum, 1);
98 if (!NT_STATUS_IS_OK(ret)) {
99 DEBUG(1, ("Fatal error while fetching a new "
100 "domain range value!\n"));
101 goto error;
104 /* store away the new mapping in both directions */
105 ret = dbwrap_store_uint32_bystring(db, cfg->sid, domainnum);
106 if (!NT_STATUS_IS_OK(ret)) {
107 DEBUG(1, ("Fatal error while storing new "
108 "domain->range assignment!\n"));
109 goto error;
112 numstr = talloc_asprintf(db, "%u", domainnum);
113 if (!numstr) {
114 ret = NT_STATUS_NO_MEMORY;
115 goto error;
118 ret = dbwrap_store_bystring(db, numstr,
119 string_term_tdb_data(cfg->sid), TDB_INSERT);
121 talloc_free(numstr);
122 if (!NT_STATUS_IS_OK(ret)) {
123 DEBUG(1, ("Fatal error while storing "
124 "new domain->range assignment!\n"));
125 goto error;
127 DEBUG(5, ("Acquired new range #%d for domain %s\n",
128 domainnum, cfg->sid));
130 cfg->domainnum = domainnum;
132 return NT_STATUS_OK;
134 error:
135 return ret;
139 static NTSTATUS idmap_autorid_get_domainrange(struct autorid_domain_config *dom,
140 bool read_only)
142 NTSTATUS ret;
145 * try to find mapping without locking the database,
146 * if it is not found create a mapping in a transaction unless
147 * read-only mode has been set
149 ret = dbwrap_fetch_uint32_bystring(autorid_db, dom->sid,
150 &(dom->domainnum));
152 if (!NT_STATUS_IS_OK(ret)) {
153 if (read_only) {
154 return NT_STATUS_NOT_FOUND;
156 ret = dbwrap_trans_do(autorid_db,
157 idmap_autorid_get_domainrange_action, dom);
160 DEBUG(10, ("Using range #%d for domain %s\n", dom->domainnum,
161 dom->sid));
163 return ret;
166 static NTSTATUS idmap_autorid_allocate_id(struct idmap_domain *dom,
167 struct unixid *xid) {
169 NTSTATUS ret;
170 struct idmap_tdb_common_context *commoncfg;
171 struct autorid_global_config *globalcfg;
172 struct autorid_domain_config domaincfg;
174 commoncfg =
175 talloc_get_type_abort(dom->private_data,
176 struct idmap_tdb_common_context);
178 globalcfg = talloc_get_type(commoncfg->private_data,
179 struct autorid_global_config);
181 if (dom->read_only) {
182 DEBUG(3, ("Backend is read-only, refusing "
183 "new allocation request\n"));
184 return NT_STATUS_UNSUCCESSFUL;
187 /* fetch the range for the allocation pool */
189 ZERO_STRUCT(domaincfg);
191 domaincfg.globalcfg = globalcfg;
192 fstrcpy(domaincfg.sid, ALLOC_RANGE);
194 ret = idmap_autorid_get_domainrange(&domaincfg, dom->read_only);
196 if (!NT_STATUS_IS_OK(ret)) {
197 DEBUG(3, ("Could not determine range for allocation pool, "
198 "check previous messages for reason\n"));
199 return ret;
202 ret = idmap_tdb_common_get_new_id(dom, xid);
204 if (!NT_STATUS_IS_OK(ret)) {
205 DEBUG(1, ("Fatal error while allocating new ID!\n"));
206 return ret;
209 xid->id = globalcfg->minvalue +
210 globalcfg->rangesize * domaincfg.domainnum +
211 xid->id;
213 DEBUG(10, ("Returned new %s %d from allocation range\n",
214 (xid->type==ID_TYPE_UID)?"uid":"gid", xid->id));
216 return ret;
220 * map a SID to xid using the idmap_tdb like pool
222 static NTSTATUS idmap_autorid_map_id_to_sid(struct idmap_domain *dom,
223 struct id_map *map)
225 NTSTATUS ret;
227 /* look out for the mapping */
228 ret = idmap_tdb_common_unixid_to_sid(dom, map);
230 if (NT_STATUS_IS_OK(ret)) {
231 map->status = ID_MAPPED;
232 return ret;
235 map->status = ID_UNKNOWN;
237 DEBUG(10, ("no ID->SID mapping for %d could be found\n", map->xid.id));
239 return ret;
242 static NTSTATUS idmap_autorid_id_to_sid(struct autorid_global_config *cfg,
243 struct idmap_domain *dom,
244 struct id_map *map)
246 uint32_t range;
247 TDB_DATA data;
248 char *keystr;
249 struct dom_sid sid;
250 NTSTATUS status;
252 /* can this be one of our ids? */
253 if (map->xid.id < cfg->minvalue) {
254 DEBUG(10, ("id %d is lower than minimum value, "
255 "ignoring mapping request\n", map->xid.id));
256 map->status = ID_UNKNOWN;
257 return NT_STATUS_OK;
260 if (map->xid.id > (cfg->minvalue + cfg->rangesize * cfg->maxranges)) {
261 DEBUG(10, ("id %d is outside of maximum id value, "
262 "ignoring mapping request\n", map->xid.id));
263 map->status = ID_UNKNOWN;
264 return NT_STATUS_OK;
267 /* determine the range of this uid */
268 range = ((map->xid.id - cfg->minvalue) / cfg->rangesize);
270 keystr = talloc_asprintf(talloc_tos(), "%u", range);
271 if (!keystr) {
272 return NT_STATUS_NO_MEMORY;
275 status = dbwrap_fetch_bystring(autorid_db, talloc_tos(), keystr, &data);
276 TALLOC_FREE(keystr);
278 if (!NT_STATUS_IS_OK(status)) {
279 DEBUG(4, ("id %d belongs to range %d which does not have "
280 "domain mapping, ignoring mapping request\n",
281 map->xid.id, range));
282 TALLOC_FREE(data.dptr);
283 map->status = ID_UNKNOWN;
284 return NT_STATUS_OK;
287 if (strncmp((const char *)data.dptr,
288 ALLOC_RANGE,
289 strlen(ALLOC_RANGE)) == 0) {
291 * this is from the alloc range, check if there is a mapping
293 DEBUG(5, ("id %d belongs to allocation range, "
294 "checking for mapping\n",
295 map->xid.id));
296 TALLOC_FREE(data.dptr);
297 return idmap_autorid_map_id_to_sid(dom, map);
300 string_to_sid(&sid, (const char *)data.dptr);
301 TALLOC_FREE(data.dptr);
303 sid_compose(map->sid, &sid,
304 (map->xid.id - cfg->minvalue -
305 range * cfg->rangesize));
307 /* We **really** should have some way of validating
308 the SID exists and is the correct type here. But
309 that is a deficiency in the idmap_rid design. */
311 map->status = ID_MAPPED;
312 return NT_STATUS_OK;
315 /**********************************
316 Single sid to id lookup function.
317 **********************************/
319 static NTSTATUS idmap_autorid_sid_to_id(struct autorid_global_config *global,
320 struct autorid_domain_config *domain,
321 struct id_map *map)
323 uint32_t rid;
325 sid_peek_rid(map->sid, &rid);
327 /* if the rid is higher than the size of the range, we cannot map it */
328 if (rid >= global->rangesize) {
329 map->status = ID_UNKNOWN;
330 DEBUG(2, ("RID %d is larger then size of range (%d), "
331 "user cannot be mapped\n", rid, global->rangesize));
332 return NT_STATUS_UNSUCCESSFUL;
334 map->xid.id = global->minvalue +
335 (global->rangesize * domain->domainnum)+rid;
337 /* We **really** should have some way of validating
338 the SID exists and is the correct type here. But
339 that is a deficiency in the idmap_rid design. */
341 map->status = ID_MAPPED;
343 return NT_STATUS_OK;
346 /**********************************
347 lookup a set of unix ids.
348 **********************************/
350 static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
351 struct id_map **ids)
353 struct idmap_tdb_common_context *commoncfg;
354 struct autorid_global_config *globalcfg;
355 NTSTATUS ret;
356 int i;
357 int num_tomap = 0;
358 int num_mapped = 0;
360 /* initialize the status to avoid surprise */
361 for (i = 0; ids[i]; i++) {
362 ids[i]->status = ID_UNKNOWN;
363 num_tomap++;
366 commoncfg =
367 talloc_get_type_abort(dom->private_data,
368 struct idmap_tdb_common_context);
370 globalcfg = talloc_get_type(commoncfg->private_data,
371 struct autorid_global_config);
373 for (i = 0; ids[i]; i++) {
375 ret = idmap_autorid_id_to_sid(globalcfg, dom, ids[i]);
377 if ((!NT_STATUS_IS_OK(ret)) &&
378 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
379 /* some fatal error occurred, log it */
380 DEBUG(3, ("Unexpected error resolving an ID "
381 " (%d)\n", ids[i]->xid.id));
382 goto failure;
385 if (NT_STATUS_IS_OK(ret) && ids[i]->status == ID_MAPPED) {
386 num_mapped++;
391 if (num_tomap == num_mapped) {
392 return NT_STATUS_OK;
393 } else if (num_mapped == 0) {
394 return NT_STATUS_NONE_MAPPED;
397 return STATUS_SOME_UNMAPPED;
400 failure:
401 return ret;
405 * map a SID to xid using the idmap_tdb like pool
407 static NTSTATUS idmap_autorid_map_sid_to_id(struct idmap_domain *dom,
408 struct id_map *map,
409 struct idmap_tdb_common_context *ctx)
411 NTSTATUS ret;
412 int res;
414 /* see if we already have a mapping */
415 ret = idmap_tdb_common_sid_to_unixid(dom, map);
417 if (NT_STATUS_IS_OK(ret)) {
418 map->status = ID_MAPPED;
419 return ret;
422 /* bad things happened */
423 if (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
424 DEBUG(1, ("Looking up SID->ID mapping for %s failed\n",
425 sid_string_dbg(map->sid)));
426 return ret;
429 if (dom->read_only) {
430 DEBUG(3, ("Not allocating new mapping for %s, because backend "
431 "is read-only\n", sid_string_dbg(map->sid)));
432 return NT_STATUS_NONE_MAPPED;
435 DEBUG(10, ("Creating new mapping in pool for %s\n",
436 sid_string_dbg(map->sid)));
438 /* create new mapping */
439 res = dbwrap_transaction_start(ctx->db);
440 if (res != 0) {
441 DEBUG(2, ("transaction_start failed\n"));
442 return NT_STATUS_INTERNAL_DB_CORRUPTION;
445 ret = idmap_tdb_common_new_mapping(dom, map);
447 map->status = (NT_STATUS_IS_OK(ret))?ID_MAPPED:ID_UNMAPPED;
449 if (!NT_STATUS_IS_OK(ret)) {
450 if (dbwrap_transaction_cancel(ctx->db) != 0) {
451 smb_panic("Cancelling transaction failed");
453 return ret;
456 res = dbwrap_transaction_commit(ctx->db);
457 if (res == 0) {
458 return ret;
461 DEBUG(2, ("transaction_commit failed\n"));
462 return NT_STATUS_INTERNAL_DB_CORRUPTION;
466 /**********************************
467 lookup a set of sids.
468 **********************************/
470 static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
471 struct id_map **ids)
473 struct idmap_tdb_common_context *commoncfg;
474 struct autorid_global_config *global;
475 NTSTATUS ret;
476 int i;
477 int num_tomap = 0;
478 int num_mapped = 0;
480 /* initialize the status to avoid surprise */
481 for (i = 0; ids[i]; i++) {
482 ids[i]->status = ID_UNKNOWN;
483 num_tomap++;
486 commoncfg =
487 talloc_get_type_abort(dom->private_data,
488 struct idmap_tdb_common_context);
490 global = talloc_get_type(commoncfg->private_data,
491 struct autorid_global_config);
493 for (i = 0; ids[i]; i++) {
494 struct winbindd_tdc_domain *domain;
495 struct autorid_domain_config domaincfg;
496 uint32_t rid;
497 struct dom_sid domainsid;
499 ZERO_STRUCT(domaincfg);
501 DEBUG(10, ("Trying to map %s\n", sid_string_dbg(ids[i]->sid)));
503 sid_copy(&domainsid, ids[i]->sid);
504 if (!sid_split_rid(&domainsid, &rid)) {
505 DEBUG(4, ("Could not determine domain SID from %s, "
506 "ignoring mapping request\n",
507 sid_string_dbg(ids[i]->sid)));
508 continue;
511 /* is this a well-known SID? */
513 if (sid_check_is_wellknown_domain(&domainsid, NULL)) {
515 DEBUG(10, ("SID %s is well-known, using pool\n",
516 sid_string_dbg(ids[i]->sid)));
518 ret = idmap_autorid_map_sid_to_id(dom, ids[i],
519 commoncfg);
521 if (!NT_STATUS_IS_OK(ret) &&
522 !NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
523 DEBUG(3, ("Unexpected error resolving "
524 "SID (%s)\n",
525 sid_string_dbg(ids[i]->sid)));
526 goto failure;
529 if (ids[i]->status == ID_MAPPED) {
530 num_mapped++;
533 continue;
536 /* BUILTIN is passdb's job */
537 if (dom_sid_equal(&domainsid, &global_sid_Builtin) &&
538 global->ignore_builtin) {
539 DEBUG(10, ("Ignoring request for BUILTIN domain\n"));
540 continue;
544 * Check if the domain is around
546 domain = wcache_tdc_fetch_domainbysid(talloc_tos(),
547 &domainsid);
548 if (domain == NULL) {
549 DEBUG(10, ("Ignoring unknown domain sid %s\n",
550 sid_string_dbg(&domainsid)));
551 continue;
553 TALLOC_FREE(domain);
555 domaincfg.globalcfg = global;
556 sid_to_fstring(domaincfg.sid, &domainsid);
558 ret = idmap_autorid_get_domainrange(&domaincfg, dom->read_only);
560 /* read-only mode and a new domain range would be required? */
561 if (NT_STATUS_EQUAL(ret, NT_STATUS_NOT_FOUND) &&
562 dom->read_only) {
563 DEBUG(10, ("read-only is enabled, did not allocate "
564 "new range for domain %s\n",
565 sid_string_dbg(&domainsid)));
566 continue;
569 if (!NT_STATUS_IS_OK(ret)) {
570 DEBUG(3, ("Could not determine range for domain, "
571 "check previous messages for reason\n"));
572 goto failure;
575 ret = idmap_autorid_sid_to_id(global, &domaincfg, ids[i]);
577 if ((!NT_STATUS_IS_OK(ret)) &&
578 (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
579 /* some fatal error occurred, log it */
580 DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
581 sid_string_dbg(ids[i]->sid)));
582 goto failure;
585 if (NT_STATUS_IS_OK(ret)) {
586 num_mapped++;
590 if (num_tomap == num_mapped) {
591 return NT_STATUS_OK;
592 } else if (num_mapped == 0) {
593 return NT_STATUS_NONE_MAPPED;
596 return STATUS_SOME_UNMAPPED;
598 failure:
599 return ret;
603 /* initialize the given HWM to 0 if it does not exist yet */
604 static NTSTATUS idmap_autorid_init_hwm(const char *hwm) {
606 NTSTATUS status;
607 uint32_t hwmval;
609 status = dbwrap_fetch_uint32_bystring(autorid_db, hwm, &hwmval);
610 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
611 status = dbwrap_trans_store_int32_bystring(autorid_db, hwm, 0);
612 if (!NT_STATUS_IS_OK(status)) {
613 DEBUG(0,
614 ("Unable to initialise HWM (%s) in autorid "
615 "database: %s\n", hwm, nt_errstr(status)));
616 return NT_STATUS_INTERNAL_DB_ERROR;
618 } else if (!NT_STATUS_IS_OK(status)) {
619 DEBUG(0, ("unable to fetch HWM (%s) from autorid "
620 "database: %s\n", hwm, nt_errstr(status)));
621 return status;
624 return NT_STATUS_OK;
628 * open and initialize the database which stores the ranges for the domains
630 static NTSTATUS idmap_autorid_db_init(void)
632 NTSTATUS status;
634 if (autorid_db) {
635 /* its already open */
636 return NT_STATUS_OK;
639 /* Open idmap repository */
640 autorid_db = db_open(NULL, state_path("autorid.tdb"), 0,
641 TDB_DEFAULT, O_RDWR | O_CREAT, 0644,
642 DBWRAP_LOCK_ORDER_1);
644 if (!autorid_db) {
645 DEBUG(0, ("Unable to open idmap_autorid database '%s'\n",
646 state_path("autorid.tdb")));
647 return NT_STATUS_UNSUCCESSFUL;
650 /* Initialize high water mark for the currently used range to 0 */
652 status = idmap_autorid_init_hwm(HWM);
653 NT_STATUS_NOT_OK_RETURN(status);
655 status = idmap_autorid_init_hwm(ALLOC_HWM_UID);
656 NT_STATUS_NOT_OK_RETURN(status);
658 status = idmap_autorid_init_hwm(ALLOC_HWM_GID);
660 return status;
663 static struct autorid_global_config *idmap_autorid_loadconfig(TALLOC_CTX * ctx)
666 TDB_DATA data;
667 struct autorid_global_config *cfg;
668 unsigned long minvalue, rangesize, maxranges;
669 NTSTATUS status;
671 status = dbwrap_fetch_bystring(autorid_db, ctx, CONFIGKEY, &data);
673 if (!NT_STATUS_IS_OK(status)) {
674 DEBUG(10, ("No saved config found\n"));
675 return NULL;
678 cfg = talloc_zero(ctx, struct autorid_global_config);
679 if (!cfg) {
680 return NULL;
683 if (sscanf((char *)data.dptr,
684 "minvalue:%lu rangesize:%lu maxranges:%lu",
685 &minvalue, &rangesize, &maxranges) != 3) {
686 DEBUG(1,
687 ("Found invalid configuration data"
688 "creating new config\n"));
689 return NULL;
692 cfg->minvalue = minvalue;
693 cfg->rangesize = rangesize;
694 cfg->maxranges = maxranges;
696 DEBUG(10, ("Loaded previously stored configuration "
697 "minvalue:%d rangesize:%d\n",
698 cfg->minvalue, cfg->rangesize));
700 return cfg;
704 static NTSTATUS idmap_autorid_saveconfig(struct autorid_global_config *cfg)
707 NTSTATUS status;
708 TDB_DATA data;
709 char *cfgstr;
711 cfgstr =
712 talloc_asprintf(talloc_tos(),
713 "minvalue:%u rangesize:%u maxranges:%u",
714 cfg->minvalue, cfg->rangesize, cfg->maxranges);
716 if (!cfgstr) {
717 return NT_STATUS_NO_MEMORY;
720 data = string_tdb_data(cfgstr);
722 status = dbwrap_trans_store_bystring(autorid_db, CONFIGKEY,
723 data, TDB_REPLACE);
725 talloc_free(cfgstr);
727 return status;
730 static NTSTATUS idmap_autorid_preallocate_wellknown(struct idmap_domain *dom)
732 const char *groups[] = { "S-1-1-0", "S-1-2-0", "S-1-2-1",
733 "S-1-3-0", "S-1-3-1", "S-1-3-2", "S-1-3-3", "S-1-3-4",
734 "S-1-5-1", "S-1-5-2", "S-1-5-3", "S-1-5-4", "S-1-5-6",
735 "S-1-5-7", "S-1-5-8", "S-1-5-9", "S-1-5-10", "S-1-5-11",
736 "S-1-5-12", "S-1-5-13", "S-1-5-14", "S-1-5-15",
737 "S-1-5-17", "S-1-5-18", "S-1-5-19", "S-1-5-20"
740 struct id_map **maps;
741 int i, num;
742 NTSTATUS status;
744 if (dom->read_only) {
745 return NT_STATUS_OK;
748 num = sizeof(groups)/sizeof(char*);
750 maps = talloc_zero_array(talloc_tos(), struct id_map*, num+1);
751 if (!maps) {
752 return NT_STATUS_NO_MEMORY;
755 for (i = 0; i < num; i++) {
756 maps[i] = talloc(maps, struct id_map);
757 maps[i]->xid.type = ID_TYPE_GID;
758 maps[i]->sid = dom_sid_parse_talloc(maps, groups[i]);
761 maps[num] = NULL;
763 status = idmap_autorid_sids_to_unixids(dom, maps);
765 DEBUG(10,("Preallocation run finished with status %s\n",
766 nt_errstr(status)));
768 talloc_free(maps);
770 return NT_STATUS_IS_OK(status)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
773 static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
775 struct idmap_tdb_common_context *commonconfig;
776 struct autorid_global_config *config;
777 struct autorid_global_config *storedconfig = NULL;
778 NTSTATUS status;
779 uint32_t hwm;
781 if (!strequal(dom->name, "*")) {
782 DEBUG(0, ("idmap_autorid_initialize: Error: autorid configured "
783 "for domain '%s'. But autorid can only be used for "
784 "the default idmap configuration.\n", dom->name));
785 return NT_STATUS_INVALID_PARAMETER;
788 commonconfig = talloc_zero(dom, struct idmap_tdb_common_context);
789 if (!commonconfig) {
790 DEBUG(0, ("Out of memory!\n"));
791 return NT_STATUS_NO_MEMORY;
794 commonconfig->rw_ops = talloc_zero(commonconfig, struct idmap_rw_ops);
795 if (commonconfig->rw_ops == NULL) {
796 DEBUG(0, ("Out of memory!\n"));
797 return NT_STATUS_NO_MEMORY;
800 config = talloc_zero(commonconfig, struct autorid_global_config);
801 if (!config) {
802 DEBUG(0, ("Out of memory!\n"));
803 return NT_STATUS_NO_MEMORY;
806 status = idmap_autorid_db_init();
807 if (!NT_STATUS_IS_OK(status)) {
808 goto error;
811 config->minvalue = dom->low_id;
812 config->rangesize = lp_parm_int(-1, "idmap config *",
813 "rangesize", 100000);
815 if (config->rangesize < 2000) {
816 DEBUG(1, ("autorid rangesize must be at least 2000\n"));
817 status = NT_STATUS_INVALID_PARAMETER;
818 goto error;
821 config->maxranges = (dom->high_id - dom->low_id + 1) /
822 config->rangesize;
824 if (config->maxranges == 0) {
825 DEBUG(1, ("allowed uid range is smaller then rangesize, "
826 "increase uid range or decrease rangesize\n"));
827 status = NT_STATUS_INVALID_PARAMETER;
828 goto error;
831 /* check if the high-low limit is a multiple of the rangesize */
832 if ((dom->high_id - dom->low_id + 1) % config->rangesize != 0) {
833 DEBUG(5, ("High uid-low uid difference of %d "
834 "is not a multiple of the rangesize %d, "
835 "limiting ranges to lower boundary number of %d\n",
836 (dom->high_id - dom->low_id + 1), config->rangesize,
837 config->maxranges));
840 DEBUG(10, ("Current configuration in config is "
841 "minvalue:%d rangesize:%d maxranges:%d\n",
842 config->minvalue, config->rangesize, config->maxranges));
844 /* read previously stored config and current HWM */
845 storedconfig = idmap_autorid_loadconfig(talloc_tos());
847 status = dbwrap_fetch_uint32_bystring(autorid_db, HWM, &hwm);
848 if (!NT_STATUS_IS_OK(status)) {
849 DEBUG(1, ("Fatal error while fetching current "
850 "HWM value: %s\n", nt_errstr(status)));
851 status = NT_STATUS_INTERNAL_ERROR;
852 goto error;
855 /* did the minimum value or rangesize change? */
856 if (storedconfig &&
857 ((storedconfig->minvalue != config->minvalue) ||
858 (storedconfig->rangesize != config->rangesize))) {
859 DEBUG(1, ("New configuration values for rangesize or "
860 "minimum uid value conflict with previously "
861 "used values! Aborting initialization\n"));
862 status = NT_STATUS_INVALID_PARAMETER;
863 goto error;
867 * has the highest uid value been reduced to setting that is not
868 * sufficient any more for already existing ranges?
870 if (hwm > config->maxranges) {
871 DEBUG(1, ("New upper uid limit is too low to cover "
872 "existing mappings! Aborting initialization\n"));
873 status = NT_STATUS_INVALID_PARAMETER;
874 goto error;
877 status = idmap_autorid_saveconfig(config);
879 if (!NT_STATUS_IS_OK(status)) {
880 DEBUG(1, ("Failed to store configuration data!\n"));
881 goto error;
884 DEBUG(5, ("%d domain ranges with a size of %d are available\n",
885 config->maxranges, config->rangesize));
887 config->ignore_builtin = lp_parm_bool(-1, "idmap config *",
888 "ignore builtin", false);
890 /* fill the TDB common configuration */
891 commonconfig->private_data = config;
893 commonconfig->db = autorid_db;
894 commonconfig->max_id = config->rangesize -1;
895 commonconfig->hwmkey_uid = ALLOC_HWM_UID;
896 commonconfig->hwmkey_gid = ALLOC_HWM_GID;
897 commonconfig->rw_ops->get_new_id = idmap_autorid_allocate_id;
898 commonconfig->rw_ops->set_mapping = idmap_tdb_common_set_mapping;
900 dom->private_data = commonconfig;
902 /* preallocate well-known SIDs in the pool */
903 status = idmap_autorid_preallocate_wellknown(dom);
905 goto done;
907 error:
908 talloc_free(config);
910 done:
911 talloc_free(storedconfig);
913 return status;
917 Close the idmap tdb instance
919 static struct idmap_methods autorid_methods = {
920 .init = idmap_autorid_initialize,
921 .unixids_to_sids = idmap_autorid_unixids_to_sids,
922 .sids_to_unixids = idmap_autorid_sids_to_unixids,
923 .allocate_id = idmap_autorid_allocate_id
926 NTSTATUS samba_init_module(void)
928 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
929 "autorid", &autorid_methods);