idmap_autorid: change idmap_autorid_loadconfig() to return NTSTATUS
[Samba.git] / source3 / winbindd / idmap_autorid_tdb.c
blob7ded36f84753ccbd7e68f9ca32ac1575c8b252d1
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
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"
28 static NTSTATUS idmap_autorid_get_domainrange_action(struct db_context *db,
29 void *private_data)
31 NTSTATUS ret;
32 uint32_t rangenum, hwm;
33 char *numstr;
34 struct autorid_range_config *range;
36 range = (struct autorid_range_config *)private_data;
38 ret = dbwrap_fetch_uint32_bystring(db, range->keystr,
39 &(range->rangenum));
41 if (NT_STATUS_IS_OK(ret)) {
42 /* entry is already present*/
43 return ret;
46 DEBUG(10, ("Acquiring new range for domain %s "
47 "(domain_range_index=%"PRIu32")\n",
48 range->domsid, range->domain_range_index));
50 /* fetch the current HWM */
51 ret = dbwrap_fetch_uint32_bystring(db, HWM, &hwm);
52 if (!NT_STATUS_IS_OK(ret)) {
53 DEBUG(1, ("Fatal error while fetching current "
54 "HWM value: %s\n", nt_errstr(ret)));
55 ret = NT_STATUS_INTERNAL_ERROR;
56 goto error;
59 /* do we have a range left? */
60 if (hwm >= range->globalcfg->maxranges) {
61 DEBUG(1, ("No more domain ranges available!\n"));
62 ret = NT_STATUS_NO_MEMORY;
63 goto error;
66 /* increase the HWM */
67 ret = dbwrap_change_uint32_atomic_bystring(db, HWM, &rangenum, 1);
68 if (!NT_STATUS_IS_OK(ret)) {
69 DEBUG(1, ("Fatal error while fetching a new "
70 "domain range value!\n"));
71 goto error;
74 /* store away the new mapping in both directions */
75 ret = dbwrap_store_uint32_bystring(db, range->keystr, rangenum);
76 if (!NT_STATUS_IS_OK(ret)) {
77 DEBUG(1, ("Fatal error while storing new "
78 "domain->range assignment!\n"));
79 goto error;
82 numstr = talloc_asprintf(db, "%u", rangenum);
83 if (!numstr) {
84 ret = NT_STATUS_NO_MEMORY;
85 goto error;
88 ret = dbwrap_store_bystring(db, numstr,
89 string_term_tdb_data(range->keystr), TDB_INSERT);
91 talloc_free(numstr);
92 if (!NT_STATUS_IS_OK(ret)) {
93 DEBUG(1, ("Fatal error while storing "
94 "new domain->range assignment!\n"));
95 goto error;
97 DEBUG(5, ("Acquired new range #%d for domain %s "
98 "(domain_range_index=%"PRIu32")\n", rangenum, range->keystr,
99 range->domain_range_index));
101 range->rangenum = rangenum;
103 return NT_STATUS_OK;
105 error:
106 return ret;
110 NTSTATUS idmap_autorid_get_domainrange(struct db_context *db,
111 struct autorid_range_config *range,
112 bool read_only)
114 NTSTATUS ret;
117 * try to find mapping without locking the database,
118 * if it is not found create a mapping in a transaction unless
119 * read-only mode has been set
121 if (range->domain_range_index > 0) {
122 snprintf(range->keystr, FSTRING_LEN, "%s#%"PRIu32,
123 range->domsid, range->domain_range_index);
124 } else {
125 fstrcpy(range->keystr, range->domsid);
128 ret = dbwrap_fetch_uint32_bystring(db, range->keystr,
129 &(range->rangenum));
131 if (!NT_STATUS_IS_OK(ret)) {
132 if (read_only) {
133 return NT_STATUS_NOT_FOUND;
135 ret = dbwrap_trans_do(db,
136 idmap_autorid_get_domainrange_action, range);
139 range->low_id = range->globalcfg->minvalue
140 + range->rangenum * range->globalcfg->rangesize;
142 DEBUG(10, ("Using range #%d for domain %s "
143 "(domain_range_index=%"PRIu32", low_id=%"PRIu32")\n",
144 range->rangenum, range->domsid, range->domain_range_index,
145 range->low_id));
147 return ret;
150 /* initialize the given HWM to 0 if it does not exist yet */
151 NTSTATUS idmap_autorid_init_hwm(struct db_context *db, const char *hwm)
153 NTSTATUS status;
154 uint32_t hwmval;
156 status = dbwrap_fetch_uint32_bystring(db, hwm, &hwmval);
157 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
158 status = dbwrap_trans_store_int32_bystring(db, hwm, 0);
159 if (!NT_STATUS_IS_OK(status)) {
160 DEBUG(0,
161 ("Unable to initialise HWM (%s) in autorid "
162 "database: %s\n", hwm, nt_errstr(status)));
163 return NT_STATUS_INTERNAL_DB_ERROR;
165 } else if (!NT_STATUS_IS_OK(status)) {
166 DEBUG(0, ("unable to fetch HWM (%s) from autorid "
167 "database: %s\n", hwm, nt_errstr(status)));
168 return status;
171 return NT_STATUS_OK;
175 * open and initialize the database which stores the ranges for the domains
177 NTSTATUS idmap_autorid_db_init(const char *path,
178 TALLOC_CTX *mem_ctx,
179 struct db_context **db)
181 NTSTATUS status;
183 if (*db != NULL) {
184 /* its already open */
185 return NT_STATUS_OK;
188 /* Open idmap repository */
189 *db = db_open(mem_ctx, path, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644,
190 DBWRAP_LOCK_ORDER_1);
192 if (*db == NULL) {
193 DEBUG(0, ("Unable to open idmap_autorid database '%s'\n", path));
194 return NT_STATUS_UNSUCCESSFUL;
197 /* Initialize high water mark for the currently used range to 0 */
199 status = idmap_autorid_init_hwm(*db, HWM);
200 NT_STATUS_NOT_OK_RETURN(status);
202 status = idmap_autorid_init_hwm(*db, ALLOC_HWM_UID);
203 NT_STATUS_NOT_OK_RETURN(status);
205 status = idmap_autorid_init_hwm(*db, ALLOC_HWM_GID);
207 return status;
210 struct idmap_autorid_fetch_config_state {
211 TALLOC_CTX *mem_ctx;
212 char *configstr;
215 static void idmap_autorid_config_parser(TDB_DATA key, TDB_DATA value,
216 void *private_data)
218 struct idmap_autorid_fetch_config_state *state;
220 state = (struct idmap_autorid_fetch_config_state *)private_data;
223 * strndup because we have non-nullterminated strings in the db
225 state->configstr = talloc_strndup(
226 state->mem_ctx, (const char *)value.dptr, value.dsize);
229 NTSTATUS idmap_autorid_getconfigstr(struct db_context *db, TALLOC_CTX *mem_ctx,
230 char **result)
232 TDB_DATA key;
233 NTSTATUS status;
234 struct idmap_autorid_fetch_config_state state;
236 if (result == NULL) {
237 return NT_STATUS_INVALID_PARAMETER;
240 key = string_term_tdb_data(CONFIGKEY);
242 state.mem_ctx = mem_ctx;
243 state.configstr = NULL;
245 status = dbwrap_parse_record(db, key, idmap_autorid_config_parser,
246 &state);
247 if (!NT_STATUS_IS_OK(status)) {
248 DEBUG(1, ("Error while retrieving config: %s\n",
249 nt_errstr(status)));
250 return status;
253 if (state.configstr == NULL) {
254 DEBUG(1, ("Error while retrieving config\n"));
255 return NT_STATUS_NO_MEMORY;
258 DEBUG(5, ("found CONFIG: %s\n", state.configstr));
260 *result = state.configstr;
261 return NT_STATUS_OK;
264 bool idmap_autorid_parse_configstr(const char *configstr,
265 struct autorid_global_config *cfg)
267 unsigned long minvalue, rangesize, maxranges;
269 if (sscanf(configstr,
270 "minvalue:%lu rangesize:%lu maxranges:%lu",
271 &minvalue, &rangesize, &maxranges) != 3) {
272 DEBUG(1,
273 ("Found invalid configuration data"
274 "creating new config\n"));
275 return false;
278 cfg->minvalue = minvalue;
279 cfg->rangesize = rangesize;
280 cfg->maxranges = maxranges;
282 return true;
285 NTSTATUS idmap_autorid_loadconfig(struct db_context *db,
286 TALLOC_CTX *mem_ctx,
287 struct autorid_global_config **result)
289 struct autorid_global_config *cfg;
290 NTSTATUS status;
291 bool ok;
292 char *configstr = NULL;
294 if (result == NULL) {
295 return NT_STATUS_INVALID_PARAMETER;
298 status = idmap_autorid_getconfigstr(db, mem_ctx, &configstr);
299 if (!NT_STATUS_IS_OK(status)) {
300 return status;
303 cfg = talloc_zero(mem_ctx, struct autorid_global_config);
304 if (cfg == NULL) {
305 return NT_STATUS_NO_MEMORY;
308 ok = idmap_autorid_parse_configstr(configstr, cfg);
309 if (!ok) {
310 talloc_free(cfg);
311 return NT_STATUS_INVALID_PARAMETER;
314 DEBUG(10, ("Loaded previously stored configuration "
315 "minvalue:%d rangesize:%d\n",
316 cfg->minvalue, cfg->rangesize));
318 *result = cfg;
320 return NT_STATUS_OK;
323 NTSTATUS idmap_autorid_saveconfig(struct db_context *db,
324 struct autorid_global_config *cfg)
327 NTSTATUS status;
328 TDB_DATA data;
329 char *cfgstr;
331 cfgstr =
332 talloc_asprintf(talloc_tos(),
333 "minvalue:%u rangesize:%u maxranges:%u",
334 cfg->minvalue, cfg->rangesize, cfg->maxranges);
336 if (!cfgstr) {
337 return NT_STATUS_NO_MEMORY;
340 data = string_tdb_data(cfgstr);
342 status = dbwrap_trans_store_bystring(db, CONFIGKEY, data, TDB_REPLACE);
344 talloc_free(cfgstr);
346 return status;