1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
/*
* idmap_autorid_tdb: This file contains common code used by
* idmap_autorid and net idmap autorid utilities. The common
* code provides functions for performing various operations
* on autorid.tdb
*
* Copyright (C) Christian Ambach, 2010-2012
* Copyright (C) Atul Kulkarni, 2013
* Copyright (C) Michael Adam, 2012-2013
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include "idmap_autorid_tdb.h"
/**
* Build the database keystring for getting a range
* belonging to a domain sid and a range index.
*/
static void idmap_autorid_build_keystr(const char *domsid,
uint32_t domain_range_index,
fstring keystr)
{
if (domain_range_index > 0) {
fstr_sprintf(keystr, "%s#%"PRIu32,
domsid, domain_range_index);
} else {
fstrcpy(keystr, domsid);
}
}
static NTSTATUS idmap_autorid_get_domainrange_action(struct db_context *db,
void *private_data)
{
NTSTATUS ret;
uint32_t rangenum, hwm;
char *numstr;
struct autorid_range_config *range;
struct autorid_global_config *globalcfg;
fstring keystr;
range = (struct autorid_range_config *)private_data;
idmap_autorid_build_keystr(range->domsid, range->domain_range_index,
keystr);
ret = dbwrap_fetch_uint32_bystring(db, keystr,
&(range->rangenum));
if (NT_STATUS_IS_OK(ret)) {
/* entry is already present*/
return ret;
}
DEBUG(10, ("Acquiring new range for domain %s "
"(domain_range_index=%"PRIu32")\n",
range->domsid, range->domain_range_index));
/* fetch the current HWM */
ret = dbwrap_fetch_uint32_bystring(db, HWM, &hwm);
if (!NT_STATUS_IS_OK(ret)) {
DEBUG(1, ("Fatal error while fetching current "
"HWM value: %s\n", nt_errstr(ret)));
ret = NT_STATUS_INTERNAL_ERROR;
goto error;
}
ret = idmap_autorid_loadconfig(db, talloc_tos(), &globalcfg);
if (!NT_STATUS_IS_OK(ret)) {
return ret;
}
/* do we have a range left? */
if (hwm >= globalcfg->maxranges) {
DEBUG(1, ("No more domain ranges available!\n"));
talloc_free(globalcfg);
ret = NT_STATUS_NO_MEMORY;
goto error;
}
TALLOC_FREE(globalcfg);
/* increase the HWM */
ret = dbwrap_change_uint32_atomic_bystring(db, HWM, &rangenum, 1);
if (!NT_STATUS_IS_OK(ret)) {
DEBUG(1, ("Fatal error while fetching a new "
"domain range value!\n"));
goto error;
}
/* store away the new mapping in both directions */
ret = dbwrap_store_uint32_bystring(db, keystr, rangenum);
if (!NT_STATUS_IS_OK(ret)) {
DEBUG(1, ("Fatal error while storing new "
"domain->range assignment!\n"));
goto error;
}
numstr = talloc_asprintf(db, "%u", rangenum);
if (!numstr) {
ret = NT_STATUS_NO_MEMORY;
goto error;
}
ret = dbwrap_store_bystring(db, numstr,
string_term_tdb_data(keystr), TDB_INSERT);
talloc_free(numstr);
if (!NT_STATUS_IS_OK(ret)) {
DEBUG(1, ("Fatal error while storing "
"new domain->range assignment!\n"));
goto error;
}
DEBUG(5, ("Acquired new range #%d for domain %s "
"(domain_range_index=%"PRIu32")\n", rangenum, keystr,
range->domain_range_index));
range->rangenum = rangenum;
return NT_STATUS_OK;
error:
return ret;
}
NTSTATUS idmap_autorid_get_domainrange(struct db_context *db,
struct autorid_range_config *range,
bool read_only)
{
NTSTATUS ret;
struct autorid_global_config *globalcfg;
fstring keystr;
/*
* try to find mapping without locking the database,
* if it is not found create a mapping in a transaction unless
* read-only mode has been set
*/
idmap_autorid_build_keystr(range->domsid, range->domain_range_index,
keystr);
ret = dbwrap_fetch_uint32_bystring(db, keystr,
&(range->rangenum));
if (!NT_STATUS_IS_OK(ret)) {
if (read_only) {
return NT_STATUS_NOT_FOUND;
}
ret = dbwrap_trans_do(db,
idmap_autorid_get_domainrange_action, range);
}
ret = idmap_autorid_loadconfig(db, talloc_tos(), &globalcfg);
if (!NT_STATUS_IS_OK(ret)) {
return ret;
}
range->low_id = globalcfg->minvalue
+ range->rangenum * globalcfg->rangesize;
DEBUG(10, ("Using range #%d for domain %s "
"(domain_range_index=%"PRIu32", low_id=%"PRIu32")\n",
range->rangenum, range->domsid, range->domain_range_index,
range->low_id));
TALLOC_FREE(globalcfg);
return ret;
}
/* initialize the given HWM to 0 if it does not exist yet */
NTSTATUS idmap_autorid_init_hwm(struct db_context *db, const char *hwm)
{
NTSTATUS status;
uint32_t hwmval;
status = dbwrap_fetch_uint32_bystring(db, hwm, &hwmval);
if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
status = dbwrap_trans_store_int32_bystring(db, hwm, 0);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0,
("Unable to initialise HWM (%s) in autorid "
"database: %s\n", hwm, nt_errstr(status)));
return NT_STATUS_INTERNAL_DB_ERROR;
}
} else if (!NT_STATUS_IS_OK(status)) {
DEBUG(0, ("unable to fetch HWM (%s) from autorid "
"database: %s\n", hwm, nt_errstr(status)));
return status;
}
return NT_STATUS_OK;
}
/*
* open and initialize the database which stores the ranges for the domains
*/
NTSTATUS idmap_autorid_db_init(const char *path,
TALLOC_CTX *mem_ctx,
struct db_context **db)
{
NTSTATUS status;
if (*db != NULL) {
/* its already open */
return NT_STATUS_OK;
}
/* Open idmap repository */
*db = db_open(mem_ctx, path, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644,
DBWRAP_LOCK_ORDER_1);
if (*db == NULL) {
DEBUG(0, ("Unable to open idmap_autorid database '%s'\n", path));
return NT_STATUS_UNSUCCESSFUL;
}
/* Initialize high water mark for the currently used range to 0 */
status = idmap_autorid_init_hwm(*db, HWM);
NT_STATUS_NOT_OK_RETURN(status);
status = idmap_autorid_init_hwm(*db, ALLOC_HWM_UID);
NT_STATUS_NOT_OK_RETURN(status);
status = idmap_autorid_init_hwm(*db, ALLOC_HWM_GID);
return status;
}
struct idmap_autorid_fetch_config_state {
TALLOC_CTX *mem_ctx;
char *configstr;
};
static void idmap_autorid_config_parser(TDB_DATA key, TDB_DATA value,
void *private_data)
{
struct idmap_autorid_fetch_config_state *state;
state = (struct idmap_autorid_fetch_config_state *)private_data;
/*
* strndup because we have non-nullterminated strings in the db
*/
state->configstr = talloc_strndup(
state->mem_ctx, (const char *)value.dptr, value.dsize);
}
NTSTATUS idmap_autorid_getconfigstr(struct db_context *db, TALLOC_CTX *mem_ctx,
char **result)
{
TDB_DATA key;
NTSTATUS status;
struct idmap_autorid_fetch_config_state state;
if (result == NULL) {
return NT_STATUS_INVALID_PARAMETER;
}
key = string_term_tdb_data(CONFIGKEY);
state.mem_ctx = mem_ctx;
state.configstr = NULL;
status = dbwrap_parse_record(db, key, idmap_autorid_config_parser,
&state);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(1, ("Error while retrieving config: %s\n",
nt_errstr(status)));
return status;
}
if (state.configstr == NULL) {
DEBUG(1, ("Error while retrieving config\n"));
return NT_STATUS_NO_MEMORY;
}
DEBUG(5, ("found CONFIG: %s\n", state.configstr));
*result = state.configstr;
return NT_STATUS_OK;
}
bool idmap_autorid_parse_configstr(const char *configstr,
struct autorid_global_config *cfg)
{
unsigned long minvalue, rangesize, maxranges;
if (sscanf(configstr,
"minvalue:%lu rangesize:%lu maxranges:%lu",
&minvalue, &rangesize, &maxranges) != 3) {
DEBUG(1,
("Found invalid configuration data"
"creating new config\n"));
return false;
}
cfg->minvalue = minvalue;
cfg->rangesize = rangesize;
cfg->maxranges = maxranges;
return true;
}
NTSTATUS idmap_autorid_loadconfig(struct db_context *db,
TALLOC_CTX *mem_ctx,
struct autorid_global_config **result)
{
struct autorid_global_config *cfg;
NTSTATUS status;
bool ok;
char *configstr = NULL;
if (result == NULL) {
return NT_STATUS_INVALID_PARAMETER;
}
status = idmap_autorid_getconfigstr(db, mem_ctx, &configstr);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
cfg = talloc_zero(mem_ctx, struct autorid_global_config);
if (cfg == NULL) {
return NT_STATUS_NO_MEMORY;
}
ok = idmap_autorid_parse_configstr(configstr, cfg);
if (!ok) {
talloc_free(cfg);
return NT_STATUS_INVALID_PARAMETER;
}
DEBUG(10, ("Loaded previously stored configuration "
"minvalue:%d rangesize:%d\n",
cfg->minvalue, cfg->rangesize));
*result = cfg;
return NT_STATUS_OK;
}
NTSTATUS idmap_autorid_saveconfig(struct db_context *db,
struct autorid_global_config *cfg)
{
struct autorid_global_config *storedconfig = NULL;
NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
TDB_DATA data;
char *cfgstr;
uint32_t hwm;
TALLOC_CTX *frame = talloc_stackframe();
DEBUG(10, ("New configuration provided for storing is "
"minvalue:%d rangesize:%d maxranges:%d\n",
cfg->minvalue, cfg->rangesize, cfg->maxranges));
if (cfg->rangesize < 2000) {
DEBUG(1, ("autorid rangesize must be at least 2000\n"));
goto done;
}
if (cfg->maxranges == 0) {
DEBUG(1, ("An autorid maxranges value of 0 is invalid. "
"Must have at least one range available.\n"));
goto done;
}
status = idmap_autorid_loadconfig(db, frame, &storedconfig);
if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
DEBUG(5, ("No configuration found. Storing initial "
"configuration.\n"));
} else if (!NT_STATUS_IS_OK(status)) {
goto done;
}
/* did the minimum value or rangesize change? */
if (storedconfig &&
((storedconfig->minvalue != cfg->minvalue) ||
(storedconfig->rangesize != cfg->rangesize)))
{
DEBUG(1, ("New configuration values for rangesize or "
"minimum uid value conflict with previously "
"used values! Not storing new config.\n"));
status = NT_STATUS_INVALID_PARAMETER;
goto done;
}
status = dbwrap_fetch_uint32_bystring(db, HWM, &hwm);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(1, ("Fatal error while fetching current "
"HWM value: %s\n", nt_errstr(status)));
status = NT_STATUS_INTERNAL_ERROR;
goto done;
}
/*
* has the highest uid value been reduced to setting that is not
* sufficient any more for already existing ranges?
*/
if (hwm > cfg->maxranges) {
DEBUG(1, ("New upper uid limit is too low to cover "
"existing mappings! Not storing new config.\n"));
status = NT_STATUS_INVALID_PARAMETER;
goto done;
}
cfgstr =
talloc_asprintf(frame,
"minvalue:%u rangesize:%u maxranges:%u",
cfg->minvalue, cfg->rangesize, cfg->maxranges);
if (cfgstr == NULL) {
status = NT_STATUS_NO_MEMORY;
goto done;
}
data = string_tdb_data(cfgstr);
status = dbwrap_trans_store_bystring(db, CONFIGKEY, data, TDB_REPLACE);
done:
TALLOC_FREE(frame);
return status;
}
|