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
;
178 if (requested_rangenum
< hwm
) {
180 * Set a specified range below the HWM:
181 * We need to check that it is not yet taken.
184 numstr
= talloc_asprintf(mem_ctx
, "%u", requested_rangenum
);
186 ret
= NT_STATUS_NO_MEMORY
;
190 if (dbwrap_exists(db
, string_term_tdb_data(numstr
))) {
191 DEBUG(1, ("Requested range already in use.\n"));
192 ret
= NT_STATUS_INVALID_PARAMETER
;
199 * requested or automatic range >= HWM:
203 /* HWM always contains current max range + 1 */
204 increment
= requested_rangenum
+ 1 - hwm
;
206 /* increase the HWM */
207 ret
= dbwrap_change_uint32_atomic_bystring(db
, HWM
, &hwm
,
209 if (!NT_STATUS_IS_OK(ret
)) {
210 DEBUG(1, ("Fatal error while incrementing the HWM "
211 "value in the database: %s\n",
218 * store away the new mapping in both directions
221 ret
= dbwrap_store_uint32_bystring(db
, keystr
, requested_rangenum
);
222 if (!NT_STATUS_IS_OK(ret
)) {
223 DEBUG(1, ("Fatal error while storing new "
224 "domain->range assignment: %s\n", nt_errstr(ret
)));
228 numstr
= talloc_asprintf(mem_ctx
, "%u", requested_rangenum
);
230 ret
= NT_STATUS_NO_MEMORY
;
234 ret
= dbwrap_store_bystring(db
, numstr
,
235 string_term_tdb_data(keystr
), TDB_INSERT
);
237 if (!NT_STATUS_IS_OK(ret
)) {
238 DEBUG(1, ("Fatal error while storing new "
239 "domain->range assignment: %s\n", nt_errstr(ret
)));
242 DEBUG(5, ("Acquired new range #%d for domain %s "
243 "(domain_range_index=%"PRIu32
")\n", requested_rangenum
, keystr
,
244 range
->domain_range_index
));
246 range
->rangenum
= requested_rangenum
;
248 range
->low_id
= globalcfg
->minvalue
249 + range
->rangenum
* globalcfg
->rangesize
;
254 talloc_free(mem_ctx
);
258 static NTSTATUS
idmap_autorid_addrange(struct db_context
*db
,
259 struct autorid_range_config
*range
,
263 struct idmap_autorid_addrange_ctx ctx
;
265 ctx
.acquire
= acquire
;
268 status
= dbwrap_trans_do(db
, idmap_autorid_addrange_action
, &ctx
);
272 NTSTATUS
idmap_autorid_setrange(struct db_context
*db
,
274 uint32_t domain_range_index
,
278 struct autorid_range_config range
;
281 fstrcpy(range
.domsid
, domsid
);
282 range
.domain_range_index
= domain_range_index
;
283 range
.rangenum
= rangenum
;
285 status
= idmap_autorid_addrange(db
, &range
, false);
289 static NTSTATUS
idmap_autorid_acquire_range(struct db_context
*db
,
290 struct autorid_range_config
*range
)
292 return idmap_autorid_addrange(db
, range
, true);
295 static NTSTATUS
idmap_autorid_getrange_int(struct db_context
*db
,
296 struct autorid_range_config
*range
)
298 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
299 struct autorid_global_config
*globalcfg
= NULL
;
302 if (db
== NULL
|| range
== NULL
) {
303 DEBUG(3, ("Invalid arguments received\n"));
307 if (!idmap_autorid_validate_sid(range
->domsid
)) {
308 DEBUG(3, ("Invalid SID: '%s'\n", range
->domsid
));
309 status
= NT_STATUS_INVALID_PARAMETER
;
313 idmap_autorid_build_keystr(range
->domsid
, range
->domain_range_index
,
316 DEBUG(10, ("reading domain range for key %s\n", keystr
));
317 status
= dbwrap_fetch_uint32_bystring(db
, keystr
, &(range
->rangenum
));
318 if (!NT_STATUS_IS_OK(status
)) {
319 DEBUG(1, ("Failed to read database for key '%s': %s\n",
320 keystr
, nt_errstr(status
)));
324 status
= idmap_autorid_loadconfig(db
, talloc_tos(), &globalcfg
);
325 if (!NT_STATUS_IS_OK(status
)) {
326 DEBUG(1, ("Failed to read global configuration"));
329 range
->low_id
= globalcfg
->minvalue
330 + range
->rangenum
* globalcfg
->rangesize
;
332 TALLOC_FREE(globalcfg
);
337 NTSTATUS
idmap_autorid_getrange(struct db_context
*db
,
339 uint32_t domain_range_index
,
344 struct autorid_range_config range
;
346 if (rangenum
== NULL
) {
347 return NT_STATUS_INVALID_PARAMETER
;
351 fstrcpy(range
.domsid
, domsid
);
352 range
.domain_range_index
= domain_range_index
;
354 status
= idmap_autorid_getrange_int(db
, &range
);
355 if (!NT_STATUS_IS_OK(status
)) {
359 *rangenum
= range
.rangenum
;
361 if (low_id
!= NULL
) {
362 *low_id
= range
.low_id
;
368 NTSTATUS
idmap_autorid_get_domainrange(struct db_context
*db
,
369 struct autorid_range_config
*range
,
374 ret
= idmap_autorid_getrange_int(db
, range
);
375 if (!NT_STATUS_IS_OK(ret
)) {
377 return NT_STATUS_NOT_FOUND
;
380 ret
= idmap_autorid_acquire_range(db
, range
);
383 DEBUG(10, ("Using range #%d for domain %s "
384 "(domain_range_index=%"PRIu32
", low_id=%"PRIu32
")\n",
385 range
->rangenum
, range
->domsid
, range
->domain_range_index
,
391 /* initialize the given HWM to 0 if it does not exist yet */
392 NTSTATUS
idmap_autorid_init_hwm(struct db_context
*db
, const char *hwm
)
397 status
= dbwrap_fetch_uint32_bystring(db
, hwm
, &hwmval
);
398 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
399 status
= dbwrap_trans_store_int32_bystring(db
, hwm
, 0);
400 if (!NT_STATUS_IS_OK(status
)) {
402 ("Unable to initialise HWM (%s) in autorid "
403 "database: %s\n", hwm
, nt_errstr(status
)));
404 return NT_STATUS_INTERNAL_DB_ERROR
;
406 } else if (!NT_STATUS_IS_OK(status
)) {
407 DEBUG(0, ("unable to fetch HWM (%s) from autorid "
408 "database: %s\n", hwm
, nt_errstr(status
)));
416 * Delete a domain#index <-> range mapping from the database.
417 * The mapping is specified by the sid and index.
418 * If force == true, invalid mapping records are deleted as far
419 * as possible, otherwise they are left untouched.
422 struct idmap_autorid_delete_range_by_sid_ctx
{
424 uint32_t domain_range_index
;
428 static NTSTATUS
idmap_autorid_delete_range_by_sid_action(struct db_context
*db
,
431 struct idmap_autorid_delete_range_by_sid_ctx
*ctx
=
432 (struct idmap_autorid_delete_range_by_sid_ctx
*)private_data
;
434 uint32_t domain_range_index
;
440 TALLOC_CTX
*frame
= talloc_stackframe();
441 bool is_valid_range_mapping
= true;
444 domsid
= ctx
->domsid
;
445 domain_range_index
= ctx
->domain_range_index
;
448 keystr
= idmap_autorid_build_keystr_talloc(frame
, domsid
,
450 if (keystr
== NULL
) {
451 status
= NT_STATUS_NO_MEMORY
;
455 status
= dbwrap_fetch_uint32_bystring(db
, keystr
, &rangenum
);
456 if (!NT_STATUS_IS_OK(status
)) {
460 range_keystr
= talloc_asprintf(frame
, "%"PRIu32
, rangenum
);
461 if (range_keystr
== NULL
) {
462 status
= NT_STATUS_NO_MEMORY
;
466 status
= dbwrap_fetch_bystring(db
, frame
, range_keystr
, &data
);
467 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
468 DEBUG(1, ("Incomplete mapping %s -> %s: no backward mapping\n",
469 keystr
, range_keystr
));
470 is_valid_range_mapping
= false;
471 } else if (!NT_STATUS_IS_OK(status
)) {
472 DEBUG(1, ("Error fetching reverse mapping for %s -> %s: %s\n",
473 keystr
, range_keystr
, nt_errstr(status
)));
475 } else if (strncmp((const char *)data
.dptr
, keystr
, strlen(keystr
))
478 DEBUG(1, ("Invalid mapping: %s -> %s -> %s\n",
479 keystr
, range_keystr
, (const char *)data
.dptr
));
480 is_valid_range_mapping
= false;
483 if (!is_valid_range_mapping
&& !force
) {
484 DEBUG(10, ("Not deleting invalid mapping, since not in force "
486 status
= NT_STATUS_FILE_INVALID
;
490 status
= dbwrap_delete_bystring(db
, keystr
);
491 if (!NT_STATUS_IS_OK(status
)) {
492 DEBUG(1, ("Deletion of '%s' failed: %s\n",
493 keystr
, nt_errstr(status
)));
497 if (!is_valid_range_mapping
) {
501 status
= dbwrap_delete_bystring(db
, range_keystr
);
502 if (!NT_STATUS_IS_OK(status
)) {
503 DEBUG(1, ("Deletion of '%s' failed: %s\n",
504 range_keystr
, nt_errstr(status
)));
508 DEBUG(10, ("Deleted range mapping %s <--> %s\n", keystr
,
516 NTSTATUS
idmap_autorid_delete_range_by_sid(struct db_context
*db
,
518 uint32_t domain_range_index
,
522 struct idmap_autorid_delete_range_by_sid_ctx ctx
;
524 ctx
.domain_range_index
= domain_range_index
;
528 status
= dbwrap_trans_do(db
, idmap_autorid_delete_range_by_sid_action
,
534 * Delete a domain#index <-> range mapping from the database.
535 * The mapping is specified by the range number.
536 * If force == true, invalid mapping records are deleted as far
537 * as possible, otherwise they are left untouched.
539 struct idmap_autorid_delete_range_by_num_ctx
{
544 static NTSTATUS
idmap_autorid_delete_range_by_num_action(struct db_context
*db
,
547 struct idmap_autorid_delete_range_by_num_ctx
*ctx
=
548 (struct idmap_autorid_delete_range_by_num_ctx
*)private_data
;
554 TALLOC_CTX
*frame
= talloc_stackframe();
555 bool is_valid_range_mapping
= true;
558 rangenum
= ctx
->rangenum
;
561 range_keystr
= talloc_asprintf(frame
, "%"PRIu32
, rangenum
);
562 if (range_keystr
== NULL
) {
563 status
= NT_STATUS_NO_MEMORY
;
569 status
= dbwrap_fetch_bystring(db
, frame
, range_keystr
, &val
);
570 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
571 DEBUG(10, ("Did not find range '%s' in database.\n",
574 } else if (!NT_STATUS_IS_OK(status
)) {
575 DEBUG(5, ("Error fetching rang key: %s\n", nt_errstr(status
)));
579 if (val
.dptr
== NULL
) {
580 DEBUG(1, ("Invalid mapping: %s -> empty value\n",
582 is_valid_range_mapping
= false;
584 uint32_t reverse_rangenum
= 0;
586 keystr
= (char *)val
.dptr
;
588 status
= dbwrap_fetch_uint32_bystring(db
, keystr
,
590 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
591 DEBUG(1, ("Incomplete mapping %s -> %s: "
592 "no backward mapping\n",
593 range_keystr
, keystr
));
594 is_valid_range_mapping
= false;
595 } else if (!NT_STATUS_IS_OK(status
)) {
596 DEBUG(1, ("Error fetching reverse mapping for "
598 range_keystr
, keystr
, nt_errstr(status
)));
600 } else if (rangenum
!= reverse_rangenum
) {
601 is_valid_range_mapping
= false;
605 if (!is_valid_range_mapping
&& !force
) {
606 DEBUG(10, ("Not deleting invalid mapping, since not in force "
608 status
= NT_STATUS_FILE_INVALID
;
612 status
= dbwrap_delete_bystring(db
, range_keystr
);
613 if (!NT_STATUS_IS_OK(status
)) {
614 DEBUG(1, ("Deletion of '%s' failed: %s\n",
615 range_keystr
, nt_errstr(status
)));
619 if (!is_valid_range_mapping
) {
623 status
= dbwrap_delete_bystring(db
, keystr
);
624 if (!NT_STATUS_IS_OK(status
)) {
625 DEBUG(1, ("Deletion of '%s' failed: %s\n",
626 keystr
, nt_errstr(status
)));
630 DEBUG(10, ("Deleted range mapping %s <--> %s\n", range_keystr
,
638 NTSTATUS
idmap_autorid_delete_range_by_num(struct db_context
*db
,
643 struct idmap_autorid_delete_range_by_num_ctx ctx
;
645 ctx
.rangenum
= rangenum
;
648 status
= dbwrap_trans_do(db
, idmap_autorid_delete_range_by_num_action
,
654 * open and initialize the database which stores the ranges for the domains
656 NTSTATUS
idmap_autorid_db_init(const char *path
,
658 struct db_context
**db
)
663 /* its already open */
667 /* Open idmap repository */
668 *db
= db_open(mem_ctx
, path
, 0, TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0644,
669 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
672 DEBUG(0, ("Unable to open idmap_autorid database '%s'\n", path
));
673 return NT_STATUS_UNSUCCESSFUL
;
676 /* Initialize high water mark for the currently used range to 0 */
678 status
= idmap_autorid_init_hwm(*db
, HWM
);
679 NT_STATUS_NOT_OK_RETURN(status
);
681 status
= idmap_autorid_init_hwm(*db
, ALLOC_HWM_UID
);
682 NT_STATUS_NOT_OK_RETURN(status
);
684 status
= idmap_autorid_init_hwm(*db
, ALLOC_HWM_GID
);
689 struct idmap_autorid_fetch_config_state
{
694 static void idmap_autorid_config_parser(TDB_DATA key
, TDB_DATA value
,
697 struct idmap_autorid_fetch_config_state
*state
;
699 state
= (struct idmap_autorid_fetch_config_state
*)private_data
;
702 * strndup because we have non-nullterminated strings in the db
704 state
->configstr
= talloc_strndup(
705 state
->mem_ctx
, (const char *)value
.dptr
, value
.dsize
);
708 NTSTATUS
idmap_autorid_getconfigstr(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
713 struct idmap_autorid_fetch_config_state state
;
715 if (result
== NULL
) {
716 return NT_STATUS_INVALID_PARAMETER
;
719 key
= string_term_tdb_data(CONFIGKEY
);
721 state
.mem_ctx
= mem_ctx
;
722 state
.configstr
= NULL
;
724 status
= dbwrap_parse_record(db
, key
, idmap_autorid_config_parser
,
726 if (!NT_STATUS_IS_OK(status
)) {
727 DEBUG(1, ("Error while retrieving config: %s\n",
732 if (state
.configstr
== NULL
) {
733 DEBUG(1, ("Error while retrieving config\n"));
734 return NT_STATUS_NO_MEMORY
;
737 DEBUG(5, ("found CONFIG: %s\n", state
.configstr
));
739 *result
= state
.configstr
;
743 bool idmap_autorid_parse_configstr(const char *configstr
,
744 struct autorid_global_config
*cfg
)
746 unsigned long minvalue
, rangesize
, maxranges
;
748 if (sscanf(configstr
,
749 "minvalue:%lu rangesize:%lu maxranges:%lu",
750 &minvalue
, &rangesize
, &maxranges
) != 3) {
752 ("Found invalid configuration data. "
753 "Creating new config\n"));
757 cfg
->minvalue
= minvalue
;
758 cfg
->rangesize
= rangesize
;
759 cfg
->maxranges
= maxranges
;
764 NTSTATUS
idmap_autorid_loadconfig(struct db_context
*db
,
766 struct autorid_global_config
**result
)
768 struct autorid_global_config
*cfg
;
771 char *configstr
= NULL
;
773 if (result
== NULL
) {
774 return NT_STATUS_INVALID_PARAMETER
;
777 status
= idmap_autorid_getconfigstr(db
, mem_ctx
, &configstr
);
778 if (!NT_STATUS_IS_OK(status
)) {
782 cfg
= talloc_zero(mem_ctx
, struct autorid_global_config
);
784 return NT_STATUS_NO_MEMORY
;
787 ok
= idmap_autorid_parse_configstr(configstr
, cfg
);
790 return NT_STATUS_INVALID_PARAMETER
;
793 DEBUG(10, ("Loaded previously stored configuration "
794 "minvalue:%d rangesize:%d\n",
795 cfg
->minvalue
, cfg
->rangesize
));
802 NTSTATUS
idmap_autorid_saveconfig(struct db_context
*db
,
803 struct autorid_global_config
*cfg
)
806 struct autorid_global_config
*storedconfig
= NULL
;
807 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
811 TALLOC_CTX
*frame
= talloc_stackframe();
813 DEBUG(10, ("New configuration provided for storing is "
814 "minvalue:%d rangesize:%d maxranges:%d\n",
815 cfg
->minvalue
, cfg
->rangesize
, cfg
->maxranges
));
817 if (cfg
->rangesize
< 2000) {
818 DEBUG(1, ("autorid rangesize must be at least 2000\n"));
822 if (cfg
->maxranges
== 0) {
823 DEBUG(1, ("An autorid maxranges value of 0 is invalid. "
824 "Must have at least one range available.\n"));
828 status
= idmap_autorid_loadconfig(db
, frame
, &storedconfig
);
829 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
830 DEBUG(5, ("No configuration found. Storing initial "
831 "configuration.\n"));
832 } else if (!NT_STATUS_IS_OK(status
)) {
836 /* did the minimum value or rangesize change? */
838 ((storedconfig
->minvalue
!= cfg
->minvalue
) ||
839 (storedconfig
->rangesize
!= cfg
->rangesize
)))
841 DEBUG(1, ("New configuration values for rangesize or "
842 "minimum uid value conflict with previously "
843 "used values! Not storing new config.\n"));
844 status
= NT_STATUS_INVALID_PARAMETER
;
848 status
= dbwrap_fetch_uint32_bystring(db
, HWM
, &hwm
);
849 if (!NT_STATUS_IS_OK(status
)) {
850 DEBUG(1, ("Fatal error while fetching current "
851 "HWM value: %s\n", nt_errstr(status
)));
852 status
= NT_STATUS_INTERNAL_ERROR
;
857 * has the highest uid value been reduced to setting that is not
858 * sufficient any more for already existing ranges?
860 if (hwm
> cfg
->maxranges
) {
861 DEBUG(1, ("New upper uid limit is too low to cover "
862 "existing mappings! Not storing new config.\n"));
863 status
= NT_STATUS_INVALID_PARAMETER
;
868 talloc_asprintf(frame
,
869 "minvalue:%u rangesize:%u maxranges:%u",
870 cfg
->minvalue
, cfg
->rangesize
, cfg
->maxranges
);
872 if (cfgstr
== NULL
) {
873 status
= NT_STATUS_NO_MEMORY
;
877 data
= string_tdb_data(cfgstr
);
879 status
= dbwrap_trans_store_bystring(db
, CONFIGKEY
, data
, TDB_REPLACE
);
886 NTSTATUS
idmap_autorid_saveconfigstr(struct db_context
*db
,
887 const char *configstr
)
891 struct autorid_global_config cfg
;
893 ok
= idmap_autorid_parse_configstr(configstr
, &cfg
);
895 return NT_STATUS_INVALID_PARAMETER
;
898 status
= idmap_autorid_saveconfig(db
, &cfg
);
904 * iteration: Work on all range mappings for a given domain
907 struct domain_range_visitor_ctx
{
909 NTSTATUS (*fn
)(struct db_context
*db
,
915 int count
; /* number of records worked on */
918 static int idmap_autorid_visit_domain_range(struct db_record
*rec
,
921 struct domain_range_visitor_ctx
*vi
;
924 uint32_t range_index
= 0;
925 uint32_t rangenum
= 0;
929 struct db_context
*db
;
931 vi
= talloc_get_type_abort(private_data
,
932 struct domain_range_visitor_ctx
);
934 key
= dbwrap_record_get_key(rec
);
937 * split string "<sid>[#<index>]" into sid string and index number
940 domsid
= (char *)key
.dptr
;
942 DEBUG(10, ("idmap_autorid_visit_domain_range: visiting key '%s'\n",
945 sep
= strrchr(domsid
, '#');
950 if (sscanf(index_str
, "%"SCNu32
, &range_index
) != 1) {
951 DEBUG(10, ("Found separator '#' but '%s' is not a "
952 "valid range index. Skipping record\n",
958 if (!idmap_autorid_validate_sid(domsid
)) {
959 DEBUG(10, ("String '%s' is not a valid sid. "
960 "Skipping record.\n", domsid
));
964 if ((vi
->domsid
!= NULL
) && (strcmp(domsid
, vi
->domsid
) != 0)) {
965 DEBUG(10, ("key sid '%s' does not match requested sid '%s'.\n",
966 domsid
, vi
->domsid
));
970 value
= dbwrap_record_get_value(rec
);
972 if (value
.dsize
!= sizeof(uint32_t)) {
973 /* it might be a mapping of a well known sid */
974 DEBUG(10, ("value size %u != sizeof(uint32_t) for sid '%s', "
975 "skipping.\n", (unsigned)value
.dsize
, vi
->domsid
));
979 rangenum
= IVAL(value
.dptr
, 0);
981 db
= dbwrap_record_get_db(rec
);
983 status
= vi
->fn(db
, domsid
, range_index
, rangenum
, vi
->private_data
);
984 if (!NT_STATUS_IS_OK(status
)) {
996 static NTSTATUS
idmap_autorid_iterate_domain_ranges_int(struct db_context
*db
,
998 NTSTATUS (*fn
)(struct db_context
*db
,
1002 void *private_data
),
1005 NTSTATUS (*traverse
)(struct db_context
*db
,
1006 int (*f
)(struct db_record
*, void *),
1011 struct domain_range_visitor_ctx
*vi
;
1012 TALLOC_CTX
*frame
= talloc_stackframe();
1014 if (domsid
== NULL
) {
1015 DEBUG(10, ("No sid provided, operating on all ranges\n"));
1019 DEBUG(1, ("Error: missing visitor callback\n"));
1020 status
= NT_STATUS_INVALID_PARAMETER
;
1024 vi
= talloc_zero(frame
, struct domain_range_visitor_ctx
);
1026 status
= NT_STATUS_NO_MEMORY
;
1030 vi
->domsid
= domsid
;
1032 vi
->private_data
= private_data
;
1034 status
= traverse(db
, idmap_autorid_visit_domain_range
, vi
, NULL
);
1035 if (!NT_STATUS_IS_OK(status
)) {
1039 if (count
!= NULL
) {
1048 NTSTATUS
idmap_autorid_iterate_domain_ranges(struct db_context
*db
,
1050 NTSTATUS (*fn
)(struct db_context
*db
,
1054 void *private_data
),
1060 status
= idmap_autorid_iterate_domain_ranges_int(db
,
1071 NTSTATUS
idmap_autorid_iterate_domain_ranges_read(struct db_context
*db
,
1073 NTSTATUS (*fn
)(struct db_context
*db
,
1083 status
= idmap_autorid_iterate_domain_ranges_int(db
,
1088 dbwrap_traverse_read
);
1095 * Delete all ranges configured for a given domain
1098 struct delete_domain_ranges_visitor_ctx
{
1102 static NTSTATUS
idmap_autorid_delete_domain_ranges_visitor(
1103 struct db_context
*db
,
1105 uint32_t domain_range_index
,
1109 struct delete_domain_ranges_visitor_ctx
*ctx
;
1112 ctx
= (struct delete_domain_ranges_visitor_ctx
*)private_data
;
1114 status
= idmap_autorid_delete_range_by_sid(
1115 db
, domsid
, domain_range_index
, ctx
->force
);
1119 struct idmap_autorid_delete_domain_ranges_ctx
{
1122 int count
; /* output: count records operated on */
1125 static NTSTATUS
idmap_autorid_delete_domain_ranges_action(struct db_context
*db
,
1128 struct idmap_autorid_delete_domain_ranges_ctx
*ctx
;
1129 struct delete_domain_ranges_visitor_ctx visitor_ctx
;
1133 ctx
= (struct idmap_autorid_delete_domain_ranges_ctx
*)private_data
;
1135 ZERO_STRUCT(visitor_ctx
);
1136 visitor_ctx
.force
= ctx
->force
;
1138 status
= idmap_autorid_iterate_domain_ranges(db
,
1140 idmap_autorid_delete_domain_ranges_visitor
,
1143 if (!NT_STATUS_IS_OK(status
)) {
1149 return NT_STATUS_OK
;
1152 NTSTATUS
idmap_autorid_delete_domain_ranges(struct db_context
*db
,
1158 struct idmap_autorid_delete_domain_ranges_ctx ctx
;
1161 ctx
.domsid
= domsid
;
1164 status
= dbwrap_trans_do(db
, idmap_autorid_delete_domain_ranges_action
,
1166 if (!NT_STATUS_IS_OK(status
)) {
1172 return NT_STATUS_OK
;