idmap_autorid: add idmap_autorid_delete_range_by_sid()
[sfrench/samba-autobuild/.git] / source3 / winbindd / idmap_autorid_tdb.c
1 /*
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
5  *  on autorid.tdb
6  *
7  *  Copyright (C) Christian Ambach, 2010-2012
8  *  Copyright (C) Atul Kulkarni, 2013
9  *  Copyright (C) Michael Adam, 2012-2013
10  *
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.
15  *
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.
20  *
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/>.
23  *
24  */
25
26 #include "idmap_autorid_tdb.h"
27 #include "../libcli/security/dom_sid.h"
28
29 /**
30  * Build the database keystring for getting a range
31  * belonging to a domain sid and a range index.
32  */
33 static void idmap_autorid_build_keystr(const char *domsid,
34                                        uint32_t domain_range_index,
35                                        fstring keystr)
36 {
37         if (domain_range_index > 0) {
38                 fstr_sprintf(keystr, "%s#%"PRIu32,
39                              domsid, domain_range_index);
40         } else {
41                 fstrcpy(keystr, domsid);
42         }
43 }
44
45 static char *idmap_autorid_build_keystr_talloc(TALLOC_CTX *mem_ctx,
46                                               const char *domsid,
47                                               uint32_t domain_range_index)
48 {
49         char *keystr;
50
51         if (domain_range_index > 0) {
52                 keystr = talloc_asprintf(mem_ctx, "%s#%"PRIu32, domsid,
53                                          domain_range_index);
54         } else {
55                 keystr = talloc_strdup(mem_ctx, domsid);
56         }
57
58         return keystr;
59 }
60
61
62 static bool idmap_autorid_validate_sid(const char *sid)
63 {
64         struct dom_sid ignore;
65         if (sid == NULL) {
66                 return false;
67         }
68
69         if (strcmp(sid, ALLOC_RANGE) == 0) {
70                 return true;
71         }
72
73         return dom_sid_parse(sid, &ignore);
74 }
75
76 struct idmap_autorid_addrange_ctx {
77         struct autorid_range_config *range;
78         bool acquire;
79 };
80
81 static NTSTATUS idmap_autorid_addrange_action(struct db_context *db,
82                                               void *private_data)
83 {
84         struct idmap_autorid_addrange_ctx *ctx;
85         uint32_t requested_rangenum, stored_rangenum;
86         struct autorid_range_config *range;
87         bool acquire;
88         NTSTATUS ret;
89         uint32_t hwm;
90         char *numstr;
91         struct autorid_global_config *globalcfg;
92         fstring keystr;
93         uint32_t increment;
94         TALLOC_CTX *mem_ctx = NULL;
95
96         ctx = (struct idmap_autorid_addrange_ctx *)private_data;
97         range = ctx->range;
98         acquire = ctx->acquire;
99         requested_rangenum = range->rangenum;
100
101         if (db == NULL) {
102                 DEBUG(3, ("Invalid database argument: NULL"));
103                 return NT_STATUS_INVALID_PARAMETER;
104         }
105
106         if (range == NULL) {
107                 DEBUG(3, ("Invalid range argument: NULL"));
108                 return NT_STATUS_INVALID_PARAMETER;
109         }
110
111         DEBUG(10, ("Adding new range for domain %s "
112                    "(domain_range_index=%"PRIu32")\n",
113                    range->domsid, range->domain_range_index));
114
115         if (!idmap_autorid_validate_sid(range->domsid)) {
116                 DEBUG(3, ("Invalid SID: %s\n", range->domsid));
117                 return NT_STATUS_INVALID_PARAMETER;
118         }
119
120         idmap_autorid_build_keystr(range->domsid, range->domain_range_index,
121                                    keystr);
122
123         ret = dbwrap_fetch_uint32_bystring(db, keystr, &stored_rangenum);
124
125         if (NT_STATUS_IS_OK(ret)) {
126                 /* entry is already present*/
127                 if (acquire) {
128                         DEBUG(10, ("domain range already allocated - "
129                                    "Not adding!\n"));
130                         return NT_STATUS_OK;
131                 }
132
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;
138                 }
139
140                 DEBUG(10, ("Note: stored range agrees with requested "
141                            "one - ok\n"));
142                 return NT_STATUS_OK;
143         }
144
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;
151         }
152
153         mem_ctx = talloc_stackframe();
154
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",
158                           nt_errstr(ret)));
159                 goto error;
160         }
161
162         if (acquire) {
163                 /*
164                  * automatically acquire the next range
165                  */
166                 requested_rangenum = hwm;
167         }
168
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 "
172                           "(%u).\n",
173                           requested_rangenum, globalcfg->maxranges));
174                 ret = NT_STATUS_NO_MEMORY;
175                 goto error;
176         }
177
178         if (requested_rangenum < hwm) {
179                 /*
180                  * Set a specified range below the HWM:
181                  * We need to check that it is not yet taken.
182                  */
183
184                 numstr = talloc_asprintf(mem_ctx, "%u", requested_rangenum);
185                 if (!numstr) {
186                         ret = NT_STATUS_NO_MEMORY;
187                         goto error;
188                 }
189
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;
193                         goto error;
194                 }
195
196                 TALLOC_FREE(numstr);
197         } else {
198                 /*
199                  * requested or automatic range >= HWM:
200                  * increment the HWM.
201                  */
202
203                 /* HWM always contains current max range + 1 */
204                 increment = requested_rangenum + 1 - hwm;
205
206                 /* increase the HWM */
207                 ret = dbwrap_change_uint32_atomic_bystring(db, HWM, &hwm,
208                                                            increment);
209                 if (!NT_STATUS_IS_OK(ret)) {
210                         DEBUG(1, ("Fatal error while incrementing the HWM "
211                                   "value in the database: %s\n",
212                                   nt_errstr(ret)));
213                         goto error;
214                 }
215         }
216
217         /*
218          * store away the new mapping in both directions
219          */
220
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)));
225                 goto error;
226         }
227
228         numstr = talloc_asprintf(mem_ctx, "%u", requested_rangenum);
229         if (!numstr) {
230                 ret = NT_STATUS_NO_MEMORY;
231                 goto error;
232         }
233
234         ret = dbwrap_store_bystring(db, numstr,
235                         string_term_tdb_data(keystr), TDB_INSERT);
236
237         if (!NT_STATUS_IS_OK(ret)) {
238                 DEBUG(1, ("Fatal error while storing new "
239                           "domain->range assignment: %s\n", nt_errstr(ret)));
240                 goto error;
241         }
242         DEBUG(5, ("Acquired new range #%d for domain %s "
243                   "(domain_range_index=%"PRIu32")\n", requested_rangenum, keystr,
244                   range->domain_range_index));
245
246         range->rangenum = requested_rangenum;
247
248         range->low_id = globalcfg->minvalue
249                       + range->rangenum * globalcfg->rangesize;
250
251         ret = NT_STATUS_OK;
252
253 error:
254         talloc_free(mem_ctx);
255         return ret;
256 }
257
258 static NTSTATUS idmap_autorid_addrange(struct db_context *db,
259                                        struct autorid_range_config *range,
260                                        bool acquire)
261 {
262         NTSTATUS status;
263         struct idmap_autorid_addrange_ctx ctx;
264
265         ctx.acquire = acquire;
266         ctx.range = range;
267
268         status = dbwrap_trans_do(db, idmap_autorid_addrange_action, &ctx);
269         return status;
270 }
271
272 NTSTATUS idmap_autorid_setrange(struct db_context *db,
273                                 const char *domsid,
274                                 uint32_t domain_range_index,
275                                 uint32_t rangenum)
276 {
277         NTSTATUS status;
278         struct autorid_range_config range;
279
280         ZERO_STRUCT(range);
281         fstrcpy(range.domsid, domsid);
282         range.domain_range_index = domain_range_index;
283         range.rangenum = rangenum;
284
285         status = idmap_autorid_addrange(db, &range, false);
286         return status;
287 }
288
289 static NTSTATUS idmap_autorid_acquire_range(struct db_context *db,
290                                             struct autorid_range_config *range)
291 {
292         return idmap_autorid_addrange(db, range, true);
293 }
294
295 static NTSTATUS idmap_autorid_getrange_int(struct db_context *db,
296                                            struct autorid_range_config *range)
297 {
298         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
299         struct autorid_global_config *globalcfg = NULL;
300         fstring keystr;
301
302         if (db == NULL || range == NULL) {
303                 DEBUG(3, ("Invalid arguments received\n"));
304                 goto done;
305         }
306
307         idmap_autorid_build_keystr(range->domsid, range->domain_range_index,
308                                    keystr);
309
310         DEBUG(10, ("reading domain range for key %s\n", keystr));
311         status = dbwrap_fetch_uint32_bystring(db, keystr, &(range->rangenum));
312         if (!NT_STATUS_IS_OK(status)) {
313                 DEBUG(1, ("Failed to read database for key '%s': %s\n",
314                           keystr, nt_errstr(status)));
315                 goto done;
316         }
317
318         status = idmap_autorid_loadconfig(db, talloc_tos(), &globalcfg);
319         if (!NT_STATUS_IS_OK(status)) {
320                 DEBUG(1, ("Failed to read global configuration"));
321                 goto done;
322         }
323         range->low_id = globalcfg->minvalue
324                       + range->rangenum * globalcfg->rangesize;
325
326         TALLOC_FREE(globalcfg);
327 done:
328         return status;
329 }
330
331 NTSTATUS idmap_autorid_getrange(struct db_context *db,
332                                 const char *domsid,
333                                 uint32_t domain_range_index,
334                                 uint32_t *rangenum,
335                                 uint32_t *low_id)
336 {
337         NTSTATUS status;
338         struct autorid_range_config range;
339
340         if (rangenum == NULL) {
341                 return NT_STATUS_INVALID_PARAMETER;
342         }
343
344         ZERO_STRUCT(range);
345         fstrcpy(range.domsid, domsid);
346         range.domain_range_index = domain_range_index;
347
348         status = idmap_autorid_getrange_int(db, &range);
349         if (!NT_STATUS_IS_OK(status)) {
350                 return status;
351         }
352
353         *rangenum = range.rangenum;
354
355         if (low_id != NULL) {
356                 *low_id = range.low_id;
357         }
358
359         return NT_STATUS_OK;
360 }
361
362 NTSTATUS idmap_autorid_get_domainrange(struct db_context *db,
363                                        struct autorid_range_config *range,
364                                        bool read_only)
365 {
366         NTSTATUS ret;
367
368         ret = idmap_autorid_getrange_int(db, range);
369         if (!NT_STATUS_IS_OK(ret)) {
370                 if (read_only) {
371                         return NT_STATUS_NOT_FOUND;
372                 }
373
374                 ret = idmap_autorid_acquire_range(db, range);
375         }
376
377         DEBUG(10, ("Using range #%d for domain %s "
378                    "(domain_range_index=%"PRIu32", low_id=%"PRIu32")\n",
379                    range->rangenum, range->domsid, range->domain_range_index,
380                    range->low_id));
381
382         return ret;
383 }
384
385 /* initialize the given HWM to 0 if it does not exist yet */
386 NTSTATUS idmap_autorid_init_hwm(struct db_context *db, const char *hwm)
387 {
388         NTSTATUS status;
389         uint32_t hwmval;
390
391         status = dbwrap_fetch_uint32_bystring(db, hwm, &hwmval);
392         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND))  {
393                 status = dbwrap_trans_store_int32_bystring(db, hwm, 0);
394                 if (!NT_STATUS_IS_OK(status)) {
395                         DEBUG(0,
396                               ("Unable to initialise HWM (%s) in autorid "
397                                "database: %s\n", hwm, nt_errstr(status)));
398                         return NT_STATUS_INTERNAL_DB_ERROR;
399                 }
400         } else if (!NT_STATUS_IS_OK(status)) {
401                 DEBUG(0, ("unable to fetch HWM (%s) from autorid "
402                           "database: %s\n", hwm,  nt_errstr(status)));
403                 return status;
404         }
405
406         return NT_STATUS_OK;
407 }
408
409 /*
410  * Delete a domain#index <-> range mapping from the database.
411  * The mapping is specified by the sid and index.
412  * If force == true, invalid mapping records are deleted as far
413  * as possible, otherwise they are left untouched.
414  */
415
416 struct idmap_autorid_delete_range_by_sid_ctx {
417         const char *domsid;
418         uint32_t domain_range_index;
419         bool force;
420 };
421
422 static NTSTATUS idmap_autorid_delete_range_by_sid_action(struct db_context *db,
423                                                          void *private_data)
424 {
425         struct idmap_autorid_delete_range_by_sid_ctx *ctx =
426                 (struct idmap_autorid_delete_range_by_sid_ctx *)private_data;
427         const char *domsid;
428         uint32_t domain_range_index;
429         uint32_t rangenum;
430         char *keystr;
431         char *range_keystr;
432         TDB_DATA data;
433         NTSTATUS status;
434         TALLOC_CTX *frame = talloc_stackframe();
435         bool is_valid_range_mapping = true;
436         bool force;
437
438         domsid = ctx->domsid;
439         domain_range_index = ctx->domain_range_index;
440         force = ctx->force;
441
442         keystr = idmap_autorid_build_keystr_talloc(frame, domsid,
443                                                    domain_range_index);
444         if (keystr == NULL) {
445                 status = NT_STATUS_NO_MEMORY;
446                 goto done;
447         }
448
449         status = dbwrap_fetch_uint32_bystring(db, keystr, &rangenum);
450         if (!NT_STATUS_IS_OK(status)) {
451                 goto done;
452         }
453
454         range_keystr = talloc_asprintf(frame, "%"PRIu32, rangenum);
455         if (range_keystr == NULL) {
456                 status = NT_STATUS_NO_MEMORY;
457                 goto done;
458         }
459
460         status = dbwrap_fetch_bystring(db, frame, range_keystr, &data);
461         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
462                 DEBUG(1, ("Incomplete mapping %s -> %s: no backward mapping\n",
463                           keystr, range_keystr));
464                 is_valid_range_mapping = false;
465         } else if (!NT_STATUS_IS_OK(status)) {
466                 DEBUG(1, ("Error fetching reverse mapping for %s -> %s:  %s\n",
467                           keystr, range_keystr, nt_errstr(status)));
468                 goto done;
469         } else if (strncmp((const char *)data.dptr, keystr, strlen(keystr))
470                    != 0)
471         {
472                 DEBUG(1, ("Invalid mapping: %s -> %s -> %s\n",
473                           keystr, range_keystr, (const char *)data.dptr));
474                 is_valid_range_mapping = false;
475         }
476
477         if (!is_valid_range_mapping && !force) {
478                 DEBUG(10, ("Not deleting invalid mapping, since not in force "
479                            "mode.\n"));
480                 status = NT_STATUS_FILE_INVALID;
481                 goto done;
482         }
483
484         status = dbwrap_delete_bystring(db, keystr);
485         if (!NT_STATUS_IS_OK(status)) {
486                 DEBUG(1, ("Deletion of '%s' failed: %s\n",
487                           keystr, nt_errstr(status)));
488                 goto done;
489         }
490
491         if (!is_valid_range_mapping) {
492                 goto done;
493         }
494
495         status = dbwrap_delete_bystring(db, range_keystr);
496         if (!NT_STATUS_IS_OK(status)) {
497                 DEBUG(1, ("Deletion of '%s' failed: %s\n",
498                           range_keystr, nt_errstr(status)));
499                 goto done;
500         }
501
502         DEBUG(10, ("Deleted range mapping %s <--> %s\n", keystr,
503                    range_keystr));
504
505 done:
506         TALLOC_FREE(frame);
507         return status;
508 }
509
510 NTSTATUS idmap_autorid_delete_range_by_sid(struct db_context *db,
511                                            const char *domsid,
512                                            uint32_t domain_range_index,
513                                            bool force)
514 {
515         NTSTATUS status;
516         struct idmap_autorid_delete_range_by_sid_ctx ctx;
517
518         ctx.domain_range_index = domain_range_index;
519         ctx.domsid = domsid;
520         ctx.force = force;
521
522         status = dbwrap_trans_do(db, idmap_autorid_delete_range_by_sid_action,
523                                  &ctx);
524         return status;
525 }
526
527
528 /*
529  * open and initialize the database which stores the ranges for the domains
530  */
531 NTSTATUS idmap_autorid_db_init(const char *path,
532                                TALLOC_CTX *mem_ctx,
533                                struct db_context **db)
534 {
535         NTSTATUS status;
536
537         if (*db != NULL) {
538                 /* its already open */
539                 return NT_STATUS_OK;
540         }
541
542         /* Open idmap repository */
543         *db = db_open(mem_ctx, path, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644,
544                       DBWRAP_LOCK_ORDER_1);
545
546         if (*db == NULL) {
547                 DEBUG(0, ("Unable to open idmap_autorid database '%s'\n", path));
548                 return NT_STATUS_UNSUCCESSFUL;
549         }
550
551         /* Initialize high water mark for the currently used range to 0 */
552
553         status = idmap_autorid_init_hwm(*db, HWM);
554         NT_STATUS_NOT_OK_RETURN(status);
555
556         status = idmap_autorid_init_hwm(*db, ALLOC_HWM_UID);
557         NT_STATUS_NOT_OK_RETURN(status);
558
559         status = idmap_autorid_init_hwm(*db, ALLOC_HWM_GID);
560
561         return status;
562 }
563
564 struct idmap_autorid_fetch_config_state {
565         TALLOC_CTX *mem_ctx;
566         char *configstr;
567 };
568
569 static void idmap_autorid_config_parser(TDB_DATA key, TDB_DATA value,
570                                         void *private_data)
571 {
572         struct idmap_autorid_fetch_config_state *state;
573
574         state = (struct idmap_autorid_fetch_config_state *)private_data;
575
576         /*
577          * strndup because we have non-nullterminated strings in the db
578          */
579         state->configstr = talloc_strndup(
580                 state->mem_ctx, (const char *)value.dptr, value.dsize);
581 }
582
583 NTSTATUS idmap_autorid_getconfigstr(struct db_context *db, TALLOC_CTX *mem_ctx,
584                                     char **result)
585 {
586         TDB_DATA key;
587         NTSTATUS status;
588         struct idmap_autorid_fetch_config_state state;
589
590         if (result == NULL) {
591                 return NT_STATUS_INVALID_PARAMETER;
592         }
593
594         key = string_term_tdb_data(CONFIGKEY);
595
596         state.mem_ctx = mem_ctx;
597         state.configstr = NULL;
598
599         status = dbwrap_parse_record(db, key, idmap_autorid_config_parser,
600                                      &state);
601         if (!NT_STATUS_IS_OK(status)) {
602                 DEBUG(1, ("Error while retrieving config: %s\n",
603                           nt_errstr(status)));
604                 return status;
605         }
606
607         if (state.configstr == NULL) {
608                 DEBUG(1, ("Error while retrieving config\n"));
609                 return NT_STATUS_NO_MEMORY;
610         }
611
612         DEBUG(5, ("found CONFIG: %s\n", state.configstr));
613
614         *result = state.configstr;
615         return NT_STATUS_OK;
616 }
617
618 bool idmap_autorid_parse_configstr(const char *configstr,
619                                    struct autorid_global_config *cfg)
620 {
621         unsigned long minvalue, rangesize, maxranges;
622
623         if (sscanf(configstr,
624                    "minvalue:%lu rangesize:%lu maxranges:%lu",
625                    &minvalue, &rangesize, &maxranges) != 3) {
626                 DEBUG(1,
627                       ("Found invalid configuration data"
628                        "creating new config\n"));
629                 return false;
630         }
631
632         cfg->minvalue = minvalue;
633         cfg->rangesize = rangesize;
634         cfg->maxranges = maxranges;
635
636         return true;
637 }
638
639 NTSTATUS idmap_autorid_loadconfig(struct db_context *db,
640                                   TALLOC_CTX *mem_ctx,
641                                   struct autorid_global_config **result)
642 {
643         struct autorid_global_config *cfg;
644         NTSTATUS status;
645         bool ok;
646         char *configstr = NULL;
647
648         if (result == NULL) {
649                 return NT_STATUS_INVALID_PARAMETER;
650         }
651
652         status = idmap_autorid_getconfigstr(db, mem_ctx, &configstr);
653         if (!NT_STATUS_IS_OK(status)) {
654                 return status;
655         }
656
657         cfg = talloc_zero(mem_ctx, struct autorid_global_config);
658         if (cfg == NULL) {
659                 return NT_STATUS_NO_MEMORY;
660         }
661
662         ok = idmap_autorid_parse_configstr(configstr, cfg);
663         if (!ok) {
664                 talloc_free(cfg);
665                 return NT_STATUS_INVALID_PARAMETER;
666         }
667
668         DEBUG(10, ("Loaded previously stored configuration "
669                    "minvalue:%d rangesize:%d\n",
670                    cfg->minvalue, cfg->rangesize));
671
672         *result = cfg;
673
674         return NT_STATUS_OK;
675 }
676
677 NTSTATUS idmap_autorid_saveconfig(struct db_context *db,
678                                   struct autorid_global_config *cfg)
679 {
680
681         struct autorid_global_config *storedconfig = NULL;
682         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
683         TDB_DATA data;
684         char *cfgstr;
685         uint32_t hwm;
686         TALLOC_CTX *frame = talloc_stackframe();
687
688         DEBUG(10, ("New configuration provided for storing is "
689                    "minvalue:%d rangesize:%d maxranges:%d\n",
690                    cfg->minvalue, cfg->rangesize, cfg->maxranges));
691
692         if (cfg->rangesize < 2000) {
693                 DEBUG(1, ("autorid rangesize must be at least 2000\n"));
694                 goto done;
695         }
696
697         if (cfg->maxranges == 0) {
698                 DEBUG(1, ("An autorid maxranges value of 0 is invalid. "
699                           "Must have at least one range available.\n"));
700                 goto done;
701         }
702
703         status = idmap_autorid_loadconfig(db, frame, &storedconfig);
704         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
705                 DEBUG(5, ("No configuration found. Storing initial "
706                           "configuration.\n"));
707         } else if (!NT_STATUS_IS_OK(status)) {
708                 goto done;
709         }
710
711         /* did the minimum value or rangesize change? */
712         if (storedconfig &&
713             ((storedconfig->minvalue != cfg->minvalue) ||
714              (storedconfig->rangesize != cfg->rangesize)))
715         {
716                 DEBUG(1, ("New configuration values for rangesize or "
717                           "minimum uid value conflict with previously "
718                           "used values! Not storing new config.\n"));
719                 status = NT_STATUS_INVALID_PARAMETER;
720                 goto done;
721         }
722
723         status = dbwrap_fetch_uint32_bystring(db, HWM, &hwm);
724         if (!NT_STATUS_IS_OK(status)) {
725                 DEBUG(1, ("Fatal error while fetching current "
726                           "HWM value: %s\n", nt_errstr(status)));
727                 status = NT_STATUS_INTERNAL_ERROR;
728                 goto done;
729         }
730
731         /*
732          * has the highest uid value been reduced to setting that is not
733          * sufficient any more for already existing ranges?
734          */
735         if (hwm > cfg->maxranges) {
736                 DEBUG(1, ("New upper uid limit is too low to cover "
737                           "existing mappings! Not storing new config.\n"));
738                 status = NT_STATUS_INVALID_PARAMETER;
739                 goto done;
740         }
741
742         cfgstr =
743             talloc_asprintf(frame,
744                             "minvalue:%u rangesize:%u maxranges:%u",
745                             cfg->minvalue, cfg->rangesize, cfg->maxranges);
746
747         if (cfgstr == NULL) {
748                 status = NT_STATUS_NO_MEMORY;
749                 goto done;
750         }
751
752         data = string_tdb_data(cfgstr);
753
754         status = dbwrap_trans_store_bystring(db, CONFIGKEY, data, TDB_REPLACE);
755
756 done:
757         TALLOC_FREE(frame);
758         return status;
759 }
760
761 NTSTATUS idmap_autorid_saveconfigstr(struct db_context *db,
762                                      const char *configstr)
763 {
764         bool ok;
765         NTSTATUS status;
766         struct autorid_global_config cfg;
767
768         ok = idmap_autorid_parse_configstr(configstr, &cfg);
769         if (!ok) {
770                 return NT_STATUS_INVALID_PARAMETER;
771         }
772
773         status = idmap_autorid_saveconfig(db, &cfg);
774         return status;
775 }