2 * idmap_autorid_tdb: This file contains common code used by
3 * idmap_autorid and net idmap autorid utilities. The common
4 * code provides functions for performing various operations
7 * Copyright (C) Christian Ambach, 2010-2012
8 * Copyright (C) Atul Kulkarni, 2013
9 * Copyright (C) Michael Adam, 2012-2013
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <http://www.gnu.org/licenses/>.
26 #include "idmap_autorid_tdb.h"
27 #include "../libcli/security/dom_sid.h"
30 * Build the database keystring for getting a range
31 * belonging to a domain sid and a range index.
33 static void idmap_autorid_build_keystr(const char *domsid
,
34 uint32_t domain_range_index
,
37 if (domain_range_index
> 0) {
38 fstr_sprintf(keystr
, "%s#%"PRIu32
,
39 domsid
, domain_range_index
);
41 fstrcpy(keystr
, domsid
);
45 static char *idmap_autorid_build_keystr_talloc(TALLOC_CTX
*mem_ctx
,
47 uint32_t domain_range_index
)
51 if (domain_range_index
> 0) {
52 keystr
= talloc_asprintf(mem_ctx
, "%s#%"PRIu32
, domsid
,
55 keystr
= talloc_strdup(mem_ctx
, domsid
);
62 static bool idmap_autorid_validate_sid(const char *sid
)
64 struct dom_sid ignore
;
69 if (strcmp(sid
, ALLOC_RANGE
) == 0) {
73 return dom_sid_parse(sid
, &ignore
);
76 struct idmap_autorid_addrange_ctx
{
77 struct autorid_range_config
*range
;
81 static NTSTATUS
idmap_autorid_addrange_action(struct db_context
*db
,
84 struct idmap_autorid_addrange_ctx
*ctx
;
85 uint32_t requested_rangenum
, stored_rangenum
;
86 struct autorid_range_config
*range
;
91 struct autorid_global_config
*globalcfg
;
94 TALLOC_CTX
*mem_ctx
= NULL
;
96 ctx
= (struct idmap_autorid_addrange_ctx
*)private_data
;
98 acquire
= ctx
->acquire
;
99 requested_rangenum
= range
->rangenum
;
102 DEBUG(3, ("Invalid database argument: NULL"));
103 return NT_STATUS_INVALID_PARAMETER
;
107 DEBUG(3, ("Invalid range argument: NULL"));
108 return NT_STATUS_INVALID_PARAMETER
;
111 DEBUG(10, ("Adding new range for domain %s "
112 "(domain_range_index=%"PRIu32
")\n",
113 range
->domsid
, range
->domain_range_index
));
115 if (!idmap_autorid_validate_sid(range
->domsid
)) {
116 DEBUG(3, ("Invalid SID: %s\n", range
->domsid
));
117 return NT_STATUS_INVALID_PARAMETER
;
120 idmap_autorid_build_keystr(range
->domsid
, range
->domain_range_index
,
123 ret
= dbwrap_fetch_uint32_bystring(db
, keystr
, &stored_rangenum
);
125 if (NT_STATUS_IS_OK(ret
)) {
126 /* entry is already present*/
128 DEBUG(10, ("domain range already allocated - "
133 if (stored_rangenum
!= requested_rangenum
) {
134 DEBUG(1, ("Error: requested rangenumber (%u) differs "
135 "from stored one (%u).\n",
136 requested_rangenum
, stored_rangenum
));
137 return NT_STATUS_UNSUCCESSFUL
;
140 DEBUG(10, ("Note: stored range agrees with requested "
145 /* fetch the current HWM */
146 ret
= dbwrap_fetch_uint32_bystring(db
, HWM
, &hwm
);
147 if (!NT_STATUS_IS_OK(ret
)) {
148 DEBUG(1, ("Fatal error while fetching current "
149 "HWM value: %s\n", nt_errstr(ret
)));
150 return NT_STATUS_INTERNAL_ERROR
;
153 mem_ctx
= talloc_stackframe();
155 ret
= idmap_autorid_loadconfig(db
, mem_ctx
, &globalcfg
);
156 if (!NT_STATUS_IS_OK(ret
)) {
157 DEBUG(1, ("Fatal error while fetching configuration: %s\n",
164 * automatically acquire the next range
166 requested_rangenum
= hwm
;
169 if (requested_rangenum
>= globalcfg
->maxranges
) {
170 DEBUG(1, ("Not enough ranges available: New range %u must be "
171 "smaller than configured maximum number of ranges "
173 requested_rangenum
, globalcfg
->maxranges
));
174 ret
= NT_STATUS_NO_MEMORY
;
179 * Check that it is not yet taken.
180 * If the range is requested and < HWM, we need
181 * to check anyways, and otherwise, we also better
182 * check in order to prevent further corruption
183 * in case the db has been externally modified.
186 numstr
= talloc_asprintf(mem_ctx
, "%u", requested_rangenum
);
188 DEBUG(1, ("Talloc failed!\n"));
189 ret
= NT_STATUS_NO_MEMORY
;
193 if (dbwrap_exists(db
, string_term_tdb_data(numstr
))) {
194 DEBUG(1, ("Requested range '%s' is already in use.\n", numstr
));
196 if (requested_rangenum
< hwm
) {
197 ret
= NT_STATUS_INVALID_PARAMETER
;
199 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
205 if (requested_rangenum
>= hwm
) {
207 * requested or automatic range >= HWM:
211 /* HWM always contains current max range + 1 */
212 increment
= requested_rangenum
+ 1 - hwm
;
214 /* increase the HWM */
215 ret
= dbwrap_change_uint32_atomic_bystring(db
, HWM
, &hwm
,
217 if (!NT_STATUS_IS_OK(ret
)) {
218 DEBUG(1, ("Fatal error while incrementing the HWM "
219 "value in the database: %s\n",
226 * store away the new mapping in both directions
229 ret
= dbwrap_store_uint32_bystring(db
, keystr
, requested_rangenum
);
230 if (!NT_STATUS_IS_OK(ret
)) {
231 DEBUG(1, ("Fatal error while storing new "
232 "domain->range assignment: %s\n", nt_errstr(ret
)));
236 numstr
= talloc_asprintf(mem_ctx
, "%u", requested_rangenum
);
238 ret
= NT_STATUS_NO_MEMORY
;
242 ret
= dbwrap_store_bystring(db
, numstr
,
243 string_term_tdb_data(keystr
), TDB_INSERT
);
245 if (!NT_STATUS_IS_OK(ret
)) {
246 DEBUG(1, ("Fatal error while storing new "
247 "domain->range assignment: %s\n", nt_errstr(ret
)));
251 DEBUG(5, ("%s new range #%d for domain %s "
252 "(domain_range_index=%"PRIu32
")\n",
253 (acquire
?"Acquired":"Stored"),
254 requested_rangenum
, keystr
,
255 range
->domain_range_index
));
257 range
->rangenum
= requested_rangenum
;
259 range
->low_id
= globalcfg
->minvalue
260 + range
->rangenum
* globalcfg
->rangesize
;
265 talloc_free(mem_ctx
);
269 static NTSTATUS
idmap_autorid_addrange(struct db_context
*db
,
270 struct autorid_range_config
*range
,
274 struct idmap_autorid_addrange_ctx ctx
;
276 ctx
.acquire
= acquire
;
279 status
= dbwrap_trans_do(db
, idmap_autorid_addrange_action
, &ctx
);
283 NTSTATUS
idmap_autorid_setrange(struct db_context
*db
,
285 uint32_t domain_range_index
,
289 struct autorid_range_config range
;
292 fstrcpy(range
.domsid
, domsid
);
293 range
.domain_range_index
= domain_range_index
;
294 range
.rangenum
= rangenum
;
296 status
= idmap_autorid_addrange(db
, &range
, false);
300 static NTSTATUS
idmap_autorid_acquire_range(struct db_context
*db
,
301 struct autorid_range_config
*range
)
303 return idmap_autorid_addrange(db
, range
, true);
306 static NTSTATUS
idmap_autorid_getrange_int(struct db_context
*db
,
307 struct autorid_range_config
*range
)
309 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
310 struct autorid_global_config
*globalcfg
= NULL
;
313 if (db
== NULL
|| range
== NULL
) {
314 DEBUG(3, ("Invalid arguments received\n"));
318 if (!idmap_autorid_validate_sid(range
->domsid
)) {
319 DEBUG(3, ("Invalid SID: '%s'\n", range
->domsid
));
320 status
= NT_STATUS_INVALID_PARAMETER
;
324 idmap_autorid_build_keystr(range
->domsid
, range
->domain_range_index
,
327 DEBUG(10, ("reading domain range for key %s\n", keystr
));
328 status
= dbwrap_fetch_uint32_bystring(db
, keystr
, &(range
->rangenum
));
329 if (!NT_STATUS_IS_OK(status
)) {
330 DEBUG(1, ("Failed to read database record for key '%s': %s\n",
331 keystr
, nt_errstr(status
)));
335 status
= idmap_autorid_loadconfig(db
, talloc_tos(), &globalcfg
);
336 if (!NT_STATUS_IS_OK(status
)) {
337 DEBUG(1, ("Failed to read global configuration"));
340 range
->low_id
= globalcfg
->minvalue
341 + range
->rangenum
* globalcfg
->rangesize
;
343 TALLOC_FREE(globalcfg
);
348 NTSTATUS
idmap_autorid_getrange(struct db_context
*db
,
350 uint32_t domain_range_index
,
355 struct autorid_range_config range
;
357 if (rangenum
== NULL
) {
358 return NT_STATUS_INVALID_PARAMETER
;
362 fstrcpy(range
.domsid
, domsid
);
363 range
.domain_range_index
= domain_range_index
;
365 status
= idmap_autorid_getrange_int(db
, &range
);
366 if (!NT_STATUS_IS_OK(status
)) {
370 *rangenum
= range
.rangenum
;
372 if (low_id
!= NULL
) {
373 *low_id
= range
.low_id
;
379 NTSTATUS
idmap_autorid_get_domainrange(struct db_context
*db
,
380 struct autorid_range_config
*range
,
385 ret
= idmap_autorid_getrange_int(db
, range
);
386 if (!NT_STATUS_IS_OK(ret
)) {
388 return NT_STATUS_NOT_FOUND
;
391 ret
= idmap_autorid_acquire_range(db
, range
);
394 DEBUG(10, ("Using range #%d for domain %s "
395 "(domain_range_index=%"PRIu32
", low_id=%"PRIu32
")\n",
396 range
->rangenum
, range
->domsid
, range
->domain_range_index
,
402 /* initialize the given HWM to 0 if it does not exist yet */
403 static NTSTATUS
idmap_autorid_init_hwm_action(struct db_context
*db
,
410 hwm
= (char *)private_data
;
412 status
= dbwrap_fetch_uint32_bystring(db
, hwm
, &hwmval
);
413 if (NT_STATUS_IS_OK(status
)) {
414 DEBUG(1, ("HWM (%s) already initialized in autorid database "
415 "(value %"PRIu32
").\n", hwm
, hwmval
));
418 if (!NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
419 DEBUG(0, ("Error fetching HWM (%s) from autorid "
420 "database: %s\n", hwm
, nt_errstr(status
)));
424 status
= dbwrap_trans_store_uint32_bystring(db
, hwm
, 0);
425 if (!NT_STATUS_IS_OK(status
)) {
426 DEBUG(0, ("Error storing HWM (%s) in autorid database: %s\n",
427 hwm
, nt_errstr(status
)));
434 NTSTATUS
idmap_autorid_init_hwm(struct db_context
*db
, const char *hwm
)
439 status
= dbwrap_fetch_uint32_bystring(db
, hwm
, &hwmval
);
440 if (NT_STATUS_IS_OK(status
)) {
441 DEBUG(1, ("HWM (%s) already initialized in autorid database "
442 "(value %"PRIu32
").\n", hwm
, hwmval
));
445 if (!NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
446 DEBUG(0, ("unable to fetch HWM (%s) from autorid "
447 "database: %s\n", hwm
, nt_errstr(status
)));
451 status
= dbwrap_trans_do(db
, idmap_autorid_init_hwm_action
,
453 if (!NT_STATUS_IS_OK(status
)) {
454 DEBUG(0, ("Error initializing HWM (%s) in autorid database: "
455 "%s\n", hwm
, nt_errstr(status
)));
456 return NT_STATUS_INTERNAL_DB_ERROR
;
459 DEBUG(1, ("Initialized HWM (%s) in autorid database.\n", hwm
));
465 * Delete a domain#index <-> range mapping from the database.
466 * The mapping is specified by the sid and index.
467 * If force == true, invalid mapping records are deleted as far
468 * as possible, otherwise they are left untouched.
471 struct idmap_autorid_delete_range_by_sid_ctx
{
473 uint32_t domain_range_index
;
477 static NTSTATUS
idmap_autorid_delete_range_by_sid_action(struct db_context
*db
,
480 struct idmap_autorid_delete_range_by_sid_ctx
*ctx
=
481 (struct idmap_autorid_delete_range_by_sid_ctx
*)private_data
;
483 uint32_t domain_range_index
;
489 TALLOC_CTX
*frame
= talloc_stackframe();
490 bool is_valid_range_mapping
= true;
493 domsid
= ctx
->domsid
;
494 domain_range_index
= ctx
->domain_range_index
;
497 keystr
= idmap_autorid_build_keystr_talloc(frame
, domsid
,
499 if (keystr
== NULL
) {
500 status
= NT_STATUS_NO_MEMORY
;
504 status
= dbwrap_fetch_uint32_bystring(db
, keystr
, &rangenum
);
505 if (!NT_STATUS_IS_OK(status
)) {
509 range_keystr
= talloc_asprintf(frame
, "%"PRIu32
, rangenum
);
510 if (range_keystr
== NULL
) {
511 status
= NT_STATUS_NO_MEMORY
;
515 status
= dbwrap_fetch_bystring(db
, frame
, range_keystr
, &data
);
516 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
517 DEBUG(1, ("Incomplete mapping %s -> %s: no backward mapping\n",
518 keystr
, range_keystr
));
519 is_valid_range_mapping
= false;
520 } else if (!NT_STATUS_IS_OK(status
)) {
521 DEBUG(1, ("Error fetching reverse mapping for %s -> %s: %s\n",
522 keystr
, range_keystr
, nt_errstr(status
)));
524 } else if (strncmp((const char *)data
.dptr
, keystr
, strlen(keystr
))
527 DEBUG(1, ("Invalid mapping: %s -> %s -> %s\n",
528 keystr
, range_keystr
, (const char *)data
.dptr
));
529 is_valid_range_mapping
= false;
532 if (!is_valid_range_mapping
&& !force
) {
533 DEBUG(10, ("Not deleting invalid mapping, since not in force "
535 status
= NT_STATUS_FILE_INVALID
;
539 status
= dbwrap_delete_bystring(db
, keystr
);
540 if (!NT_STATUS_IS_OK(status
)) {
541 DEBUG(1, ("Deletion of '%s' failed: %s\n",
542 keystr
, nt_errstr(status
)));
546 if (!is_valid_range_mapping
) {
550 status
= dbwrap_delete_bystring(db
, range_keystr
);
551 if (!NT_STATUS_IS_OK(status
)) {
552 DEBUG(1, ("Deletion of '%s' failed: %s\n",
553 range_keystr
, nt_errstr(status
)));
557 DEBUG(10, ("Deleted range mapping %s <--> %s\n", keystr
,
565 NTSTATUS
idmap_autorid_delete_range_by_sid(struct db_context
*db
,
567 uint32_t domain_range_index
,
571 struct idmap_autorid_delete_range_by_sid_ctx ctx
;
573 ctx
.domain_range_index
= domain_range_index
;
577 status
= dbwrap_trans_do(db
, idmap_autorid_delete_range_by_sid_action
,
583 * Delete a domain#index <-> range mapping from the database.
584 * The mapping is specified by the range number.
585 * If force == true, invalid mapping records are deleted as far
586 * as possible, otherwise they are left untouched.
588 struct idmap_autorid_delete_range_by_num_ctx
{
593 static NTSTATUS
idmap_autorid_delete_range_by_num_action(struct db_context
*db
,
596 struct idmap_autorid_delete_range_by_num_ctx
*ctx
=
597 (struct idmap_autorid_delete_range_by_num_ctx
*)private_data
;
603 TALLOC_CTX
*frame
= talloc_stackframe();
604 bool is_valid_range_mapping
= true;
607 rangenum
= ctx
->rangenum
;
610 range_keystr
= talloc_asprintf(frame
, "%"PRIu32
, rangenum
);
611 if (range_keystr
== NULL
) {
612 status
= NT_STATUS_NO_MEMORY
;
618 status
= dbwrap_fetch_bystring(db
, frame
, range_keystr
, &val
);
619 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
620 DEBUG(10, ("Did not find range '%s' in database.\n",
623 } else if (!NT_STATUS_IS_OK(status
)) {
624 DEBUG(5, ("Error fetching rang key: %s\n", nt_errstr(status
)));
628 if (val
.dptr
== NULL
) {
629 DEBUG(1, ("Invalid mapping: %s -> empty value\n",
631 is_valid_range_mapping
= false;
633 uint32_t reverse_rangenum
= 0;
635 keystr
= (char *)val
.dptr
;
637 status
= dbwrap_fetch_uint32_bystring(db
, keystr
,
639 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
640 DEBUG(1, ("Incomplete mapping %s -> %s: "
641 "no backward mapping\n",
642 range_keystr
, keystr
));
643 is_valid_range_mapping
= false;
644 } else if (!NT_STATUS_IS_OK(status
)) {
645 DEBUG(1, ("Error fetching reverse mapping for "
647 range_keystr
, keystr
, nt_errstr(status
)));
649 } else if (rangenum
!= reverse_rangenum
) {
650 is_valid_range_mapping
= false;
654 if (!is_valid_range_mapping
&& !force
) {
655 DEBUG(10, ("Not deleting invalid mapping, since not in force "
657 status
= NT_STATUS_FILE_INVALID
;
661 status
= dbwrap_delete_bystring(db
, range_keystr
);
662 if (!NT_STATUS_IS_OK(status
)) {
663 DEBUG(1, ("Deletion of '%s' failed: %s\n",
664 range_keystr
, nt_errstr(status
)));
668 if (!is_valid_range_mapping
) {
672 status
= dbwrap_delete_bystring(db
, keystr
);
673 if (!NT_STATUS_IS_OK(status
)) {
674 DEBUG(1, ("Deletion of '%s' failed: %s\n",
675 keystr
, nt_errstr(status
)));
679 DEBUG(10, ("Deleted range mapping %s <--> %s\n", range_keystr
,
687 NTSTATUS
idmap_autorid_delete_range_by_num(struct db_context
*db
,
692 struct idmap_autorid_delete_range_by_num_ctx ctx
;
694 ctx
.rangenum
= rangenum
;
697 status
= dbwrap_trans_do(db
, idmap_autorid_delete_range_by_num_action
,
703 * Open and possibly create the database.
705 NTSTATUS
idmap_autorid_db_open(const char *path
,
707 struct db_context
**db
)
712 /* its already open */
716 /* Open idmap repository */
717 *db
= db_open(mem_ctx
, path
, 0, TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0644,
718 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
721 DEBUG(0, ("Unable to open idmap_autorid database '%s'\n", path
));
722 return NT_STATUS_UNSUCCESSFUL
;
729 * Initialize the high watermark records in the database.
731 NTSTATUS
idmap_autorid_init_hwms(struct db_context
*db
)
735 status
= idmap_autorid_init_hwm(db
, HWM
);
736 if (!NT_STATUS_IS_OK(status
)) {
740 status
= idmap_autorid_init_hwm(db
, ALLOC_HWM_UID
);
741 if (!NT_STATUS_IS_OK(status
)) {
745 status
= idmap_autorid_init_hwm(db
, ALLOC_HWM_GID
);
750 NTSTATUS
idmap_autorid_db_init(const char *path
,
752 struct db_context
**db
)
756 status
= idmap_autorid_db_open(path
, mem_ctx
, db
);
757 if (!NT_STATUS_IS_OK(status
)) {
761 status
= idmap_autorid_init_hwms(*db
);
767 struct idmap_autorid_fetch_config_state
{
772 static void idmap_autorid_config_parser(TDB_DATA key
, TDB_DATA value
,
775 struct idmap_autorid_fetch_config_state
*state
;
777 state
= (struct idmap_autorid_fetch_config_state
*)private_data
;
780 * strndup because we have non-nullterminated strings in the db
782 state
->configstr
= talloc_strndup(
783 state
->mem_ctx
, (const char *)value
.dptr
, value
.dsize
);
786 NTSTATUS
idmap_autorid_getconfigstr(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
791 struct idmap_autorid_fetch_config_state state
;
793 if (result
== NULL
) {
794 return NT_STATUS_INVALID_PARAMETER
;
797 key
= string_term_tdb_data(CONFIGKEY
);
799 state
.mem_ctx
= mem_ctx
;
800 state
.configstr
= NULL
;
802 status
= dbwrap_parse_record(db
, key
, idmap_autorid_config_parser
,
804 if (!NT_STATUS_IS_OK(status
)) {
805 DEBUG(1, ("Error while retrieving config: %s\n",
810 if (state
.configstr
== NULL
) {
811 DEBUG(1, ("Error while retrieving config\n"));
812 return NT_STATUS_NO_MEMORY
;
815 DEBUG(5, ("found CONFIG: %s\n", state
.configstr
));
817 *result
= state
.configstr
;
821 bool idmap_autorid_parse_configstr(const char *configstr
,
822 struct autorid_global_config
*cfg
)
824 unsigned long minvalue
, rangesize
, maxranges
;
826 if (sscanf(configstr
,
827 "minvalue:%lu rangesize:%lu maxranges:%lu",
828 &minvalue
, &rangesize
, &maxranges
) != 3) {
830 ("Found invalid configuration data. "
831 "Creating new config\n"));
835 cfg
->minvalue
= minvalue
;
836 cfg
->rangesize
= rangesize
;
837 cfg
->maxranges
= maxranges
;
842 NTSTATUS
idmap_autorid_loadconfig(struct db_context
*db
,
844 struct autorid_global_config
**result
)
846 struct autorid_global_config
*cfg
;
849 char *configstr
= NULL
;
851 if (result
== NULL
) {
852 return NT_STATUS_INVALID_PARAMETER
;
855 status
= idmap_autorid_getconfigstr(db
, mem_ctx
, &configstr
);
856 if (!NT_STATUS_IS_OK(status
)) {
860 cfg
= talloc_zero(mem_ctx
, struct autorid_global_config
);
862 return NT_STATUS_NO_MEMORY
;
865 ok
= idmap_autorid_parse_configstr(configstr
, cfg
);
868 return NT_STATUS_INVALID_PARAMETER
;
871 DEBUG(10, ("Loaded previously stored configuration "
872 "minvalue:%d rangesize:%d\n",
873 cfg
->minvalue
, cfg
->rangesize
));
880 NTSTATUS
idmap_autorid_saveconfig(struct db_context
*db
,
881 struct autorid_global_config
*cfg
)
884 struct autorid_global_config
*storedconfig
= NULL
;
885 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
889 TALLOC_CTX
*frame
= talloc_stackframe();
891 DEBUG(10, ("New configuration provided for storing is "
892 "minvalue:%d rangesize:%d maxranges:%d\n",
893 cfg
->minvalue
, cfg
->rangesize
, cfg
->maxranges
));
895 if (cfg
->rangesize
< 2000) {
896 DEBUG(1, ("autorid rangesize must be at least 2000\n"));
900 if (cfg
->maxranges
== 0) {
901 DEBUG(1, ("An autorid maxranges value of 0 is invalid. "
902 "Must have at least one range available.\n"));
906 status
= idmap_autorid_loadconfig(db
, frame
, &storedconfig
);
907 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
908 DEBUG(5, ("No configuration found. Storing initial "
909 "configuration.\n"));
910 } else if (!NT_STATUS_IS_OK(status
)) {
911 DEBUG(1, ("Error loading configuration: %s\n",
916 /* did the minimum value or rangesize change? */
918 ((storedconfig
->minvalue
!= cfg
->minvalue
) ||
919 (storedconfig
->rangesize
!= cfg
->rangesize
)))
921 DEBUG(1, ("New configuration values for rangesize or "
922 "minimum uid value conflict with previously "
923 "used values! Not storing new config.\n"));
924 status
= NT_STATUS_INVALID_PARAMETER
;
928 status
= dbwrap_fetch_uint32_bystring(db
, HWM
, &hwm
);
929 if (!NT_STATUS_IS_OK(status
)) {
930 DEBUG(1, ("Fatal error while fetching current "
931 "HWM value: %s\n", nt_errstr(status
)));
932 status
= NT_STATUS_INTERNAL_ERROR
;
937 * has the highest uid value been reduced to setting that is not
938 * sufficient any more for already existing ranges?
940 if (hwm
> cfg
->maxranges
) {
941 DEBUG(1, ("New upper uid limit is too low to cover "
942 "existing mappings! Not storing new config.\n"));
943 status
= NT_STATUS_INVALID_PARAMETER
;
948 talloc_asprintf(frame
,
949 "minvalue:%u rangesize:%u maxranges:%u",
950 cfg
->minvalue
, cfg
->rangesize
, cfg
->maxranges
);
952 if (cfgstr
== NULL
) {
953 status
= NT_STATUS_NO_MEMORY
;
957 data
= string_tdb_data(cfgstr
);
959 status
= dbwrap_trans_store_bystring(db
, CONFIGKEY
, data
, TDB_REPLACE
);
966 NTSTATUS
idmap_autorid_saveconfigstr(struct db_context
*db
,
967 const char *configstr
)
971 struct autorid_global_config cfg
;
973 ok
= idmap_autorid_parse_configstr(configstr
, &cfg
);
975 return NT_STATUS_INVALID_PARAMETER
;
978 status
= idmap_autorid_saveconfig(db
, &cfg
);
984 * iteration: Work on all range mappings for a given domain
987 struct domain_range_visitor_ctx
{
989 NTSTATUS (*fn
)(struct db_context
*db
,
995 int count
; /* number of records worked on */
998 static int idmap_autorid_visit_domain_range(struct db_record
*rec
,
1001 struct domain_range_visitor_ctx
*vi
;
1004 uint32_t range_index
= 0;
1005 uint32_t rangenum
= 0;
1006 TDB_DATA key
, value
;
1009 struct db_context
*db
;
1011 vi
= talloc_get_type_abort(private_data
,
1012 struct domain_range_visitor_ctx
);
1014 key
= dbwrap_record_get_key(rec
);
1017 * split string "<sid>[#<index>]" into sid string and index number
1020 domsid
= (char *)key
.dptr
;
1022 DEBUG(10, ("idmap_autorid_visit_domain_range: visiting key '%s'\n",
1025 sep
= strrchr(domsid
, '#');
1030 if (sscanf(index_str
, "%"SCNu32
, &range_index
) != 1) {
1031 DEBUG(10, ("Found separator '#' but '%s' is not a "
1032 "valid range index. Skipping record\n",
1038 if (!idmap_autorid_validate_sid(domsid
)) {
1039 DEBUG(10, ("String '%s' is not a valid sid. "
1040 "Skipping record.\n", domsid
));
1044 if ((vi
->domsid
!= NULL
) && (strcmp(domsid
, vi
->domsid
) != 0)) {
1045 DEBUG(10, ("key sid '%s' does not match requested sid '%s'.\n",
1046 domsid
, vi
->domsid
));
1050 value
= dbwrap_record_get_value(rec
);
1052 if (value
.dsize
!= sizeof(uint32_t)) {
1053 /* it might be a mapping of a well known sid */
1054 DEBUG(10, ("value size %u != sizeof(uint32_t) for sid '%s', "
1055 "skipping.\n", (unsigned)value
.dsize
, vi
->domsid
));
1059 rangenum
= IVAL(value
.dptr
, 0);
1061 db
= dbwrap_record_get_db(rec
);
1063 status
= vi
->fn(db
, domsid
, range_index
, rangenum
, vi
->private_data
);
1064 if (!NT_STATUS_IS_OK(status
)) {
1076 static NTSTATUS
idmap_autorid_iterate_domain_ranges_int(struct db_context
*db
,
1078 NTSTATUS (*fn
)(struct db_context
*db
,
1082 void *private_data
),
1085 NTSTATUS (*traverse
)(struct db_context
*db
,
1086 int (*f
)(struct db_record
*, void *),
1091 struct domain_range_visitor_ctx
*vi
;
1092 TALLOC_CTX
*frame
= talloc_stackframe();
1094 if (domsid
== NULL
) {
1095 DEBUG(10, ("No sid provided, operating on all ranges\n"));
1099 DEBUG(1, ("Error: missing visitor callback\n"));
1100 status
= NT_STATUS_INVALID_PARAMETER
;
1104 vi
= talloc_zero(frame
, struct domain_range_visitor_ctx
);
1106 status
= NT_STATUS_NO_MEMORY
;
1110 vi
->domsid
= domsid
;
1112 vi
->private_data
= private_data
;
1114 status
= traverse(db
, idmap_autorid_visit_domain_range
, vi
, NULL
);
1115 if (!NT_STATUS_IS_OK(status
)) {
1119 if (count
!= NULL
) {
1128 NTSTATUS
idmap_autorid_iterate_domain_ranges(struct db_context
*db
,
1130 NTSTATUS (*fn
)(struct db_context
*db
,
1134 void *private_data
),
1140 status
= idmap_autorid_iterate_domain_ranges_int(db
,
1151 NTSTATUS
idmap_autorid_iterate_domain_ranges_read(struct db_context
*db
,
1153 NTSTATUS (*fn
)(struct db_context
*db
,
1163 status
= idmap_autorid_iterate_domain_ranges_int(db
,
1168 dbwrap_traverse_read
);
1175 * Delete all ranges configured for a given domain
1178 struct delete_domain_ranges_visitor_ctx
{
1182 static NTSTATUS
idmap_autorid_delete_domain_ranges_visitor(
1183 struct db_context
*db
,
1185 uint32_t domain_range_index
,
1189 struct delete_domain_ranges_visitor_ctx
*ctx
;
1192 ctx
= (struct delete_domain_ranges_visitor_ctx
*)private_data
;
1194 status
= idmap_autorid_delete_range_by_sid(
1195 db
, domsid
, domain_range_index
, ctx
->force
);
1199 struct idmap_autorid_delete_domain_ranges_ctx
{
1202 int count
; /* output: count records operated on */
1205 static NTSTATUS
idmap_autorid_delete_domain_ranges_action(struct db_context
*db
,
1208 struct idmap_autorid_delete_domain_ranges_ctx
*ctx
;
1209 struct delete_domain_ranges_visitor_ctx visitor_ctx
;
1213 ctx
= (struct idmap_autorid_delete_domain_ranges_ctx
*)private_data
;
1215 ZERO_STRUCT(visitor_ctx
);
1216 visitor_ctx
.force
= ctx
->force
;
1218 status
= idmap_autorid_iterate_domain_ranges(db
,
1220 idmap_autorid_delete_domain_ranges_visitor
,
1223 if (!NT_STATUS_IS_OK(status
)) {
1229 return NT_STATUS_OK
;
1232 NTSTATUS
idmap_autorid_delete_domain_ranges(struct db_context
*db
,
1238 struct idmap_autorid_delete_domain_ranges_ctx ctx
;
1241 ctx
.domsid
= domsid
;
1244 status
= dbwrap_trans_do(db
, idmap_autorid_delete_domain_ranges_action
,
1246 if (!NT_STATUS_IS_OK(status
)) {
1252 return NT_STATUS_OK
;