34
34
#define DBGC_CLASS DBGC_IDMAP
36
36
#define HWM "NEXT RANGE"
37
#define ALLOC_HWM_UID "NEXT ALLOC UID"
38
#define ALLOC_HWM_GID "NEXT ALLOC GID"
39
#define ALLOC_RANGE "ALLOC"
37
40
#define CONFIGKEY "CONFIG"
39
42
struct autorid_global_config {
58
61
uint32_t domainnum, hwm;
61
63
struct autorid_domain_config *cfg;
63
65
cfg = (struct autorid_domain_config *)private_data;
64
dom_sid_string_buf(&(cfg->sid), sidstr, sizeof(sidstr));
66
if (!dbwrap_fetch_uint32(db, sidstr, &domainnum)) {
67
DEBUG(10, ("Acquiring new range for domain %s\n", sidstr));
67
if (!dbwrap_fetch_uint32(db, cfg->sid, &domainnum)) {
68
DEBUG(10, ("Acquiring new range for domain %s\n", cfg->sid));
69
70
/* fetch the current HWM */
70
71
if (!dbwrap_fetch_uint32(db, HWM, &hwm)) {
92
93
/* store away the new mapping in both directions */
93
ret = dbwrap_trans_store_uint32(db, sidstr, domainnum);
94
ret = dbwrap_trans_store_uint32(db, cfg->sid, domainnum);
94
95
if (!NT_STATUS_IS_OK(ret)) {
95
96
DEBUG(1, ("Fatal error while storing new "
96
97
"domain->range assignment!\n"));
106
107
ret = dbwrap_trans_store_bystring(db, numstr,
107
string_term_tdb_data(sidstr),
108
string_term_tdb_data(cfg->sid), TDB_INSERT);
109
110
talloc_free(numstr);
110
111
if (!NT_STATUS_IS_OK(ret)) {
111
112
DEBUG(1, ("Fatal error while storing "
115
116
DEBUG(5, ("Acquired new range #%d for domain %s\n",
117
domainnum, cfg->sid));
119
DEBUG(10, ("Using range #%d for domain %s\n", domainnum, sidstr));
120
DEBUG(10, ("Using range #%d for domain %s\n", domainnum, cfg->sid));
120
121
cfg->domainnum = domainnum;
122
123
return NT_STATUS_OK;
164
165
DEBUG(4, ("id %d belongs to range %d which does not have "
165
166
"domain mapping, ignoring mapping request\n",
166
167
map->xid.id, range));
168
TALLOC_FREE(data.dptr);
169
map->status = ID_UNKNOWN;
173
if (strncmp((const char *)data.dptr,
175
strlen(ALLOC_RANGE)) == 0) {
176
/* this is from the alloc range, there is no mapping back */
177
DEBUG(5, ("id %d belongs to alloc range, cannot map back\n",
179
TALLOC_FREE(data.dptr);
167
180
map->status = ID_UNKNOWN;
168
181
return NT_STATUS_OK;
274
287
struct winbindd_tdc_domain *domain;
275
288
struct autorid_domain_config domaincfg;
290
struct dom_sid domainsid;
278
292
ZERO_STRUCT(domaincfg);
280
sid_copy(&domaincfg.sid, ids[i]->sid);
281
if (!sid_split_rid(&domaincfg.sid, &rid)) {
294
sid_copy(&domainsid, ids[i]->sid);
295
if (!sid_split_rid(&domainsid, &rid)) {
282
296
DEBUG(4, ("Could not determine domain SID from %s, "
283
297
"ignoring mapping request\n",
284
298
sid_string_dbg(ids[i]->sid)));
289
303
* Check if the domain is around
291
305
domain = wcache_tdc_fetch_domainbysid(talloc_tos(),
293
307
if (domain == NULL) {
294
308
DEBUG(10, ("Ignoring unknown domain sid %s\n",
295
sid_string_dbg(&domaincfg.sid)));
309
sid_string_dbg(&domainsid)));
298
312
TALLOC_FREE(domain);
300
314
domaincfg.globalcfg = global;
315
sid_to_fstring(domaincfg.sid, &domainsid);
302
317
ret = dbwrap_trans_do(autorid_db,
303
318
idmap_autorid_get_domainrange,
344
/* initialize the given HWM to 0 if it does not exist yet */
345
static NTSTATUS idmap_autorid_init_hwm(const char *hwm) {
350
hwmval = dbwrap_fetch_int32(autorid_db, hwm);
352
status = dbwrap_trans_store_int32(autorid_db, hwm, 0);
353
if (!NT_STATUS_IS_OK(status)) {
355
("Unable to initialise HWM (%s) in autorid "
356
"database: %s\n", hwm, nt_errstr(status)));
357
return NT_STATUS_INTERNAL_DB_ERROR;
330
365
* open and initialize the database which stores the ranges for the domains
332
367
static NTSTATUS idmap_autorid_db_init(void)
336
371
if (autorid_db) {
337
372
/* its already open */
351
386
/* Initialize high water mark for the currently used range to 0 */
352
hwm = dbwrap_fetch_int32(autorid_db, HWM);
355
(dbwrap_trans_store_int32(autorid_db, HWM, 0))) {
357
("Unable to initialise HWM in autorid "
359
return NT_STATUS_INTERNAL_DB_ERROR;
388
status = idmap_autorid_init_hwm(HWM);
389
NT_STATUS_NOT_OK_RETURN(status);
391
status = idmap_autorid_init_hwm(ALLOC_HWM_UID);
392
NT_STATUS_NOT_OK_RETURN(status);
394
status = idmap_autorid_init_hwm(ALLOC_HWM_GID);
366
399
static struct autorid_global_config *idmap_autorid_loadconfig(TALLOC_CTX * ctx)
577
static NTSTATUS idmap_autorid_allocate_id(struct idmap_domain *dom,
578
struct unixid *xid) {
581
struct autorid_global_config *globalcfg;
582
struct autorid_domain_config domaincfg;
586
if (!strequal(dom->name, "*")) {
587
DEBUG(3, ("idmap_autorid_allocate_id: "
588
"Refusing creation of mapping for domain'%s'. "
589
"Currently only supported for the default "
592
return NT_STATUS_NOT_IMPLEMENTED;
595
if ((xid->type != ID_TYPE_UID) && (xid->type != ID_TYPE_GID)) {
596
return NT_STATUS_INVALID_PARAMETER;
600
globalcfg = talloc_get_type(dom->private_data,
601
struct autorid_global_config);
603
/* fetch the range for the allocation pool */
605
ZERO_STRUCT(domaincfg);
607
domaincfg.globalcfg = globalcfg;
608
fstrcpy(domaincfg.sid, ALLOC_RANGE);
610
ret = dbwrap_trans_do(autorid_db,
611
idmap_autorid_get_domainrange,
613
if (!NT_STATUS_IS_OK(ret)) {
614
DEBUG(3, ("Could not determine range for allocation pool, "
615
"check previous messages for reason\n"));
619
/* fetch the current HWM */
620
hwmkey = (xid->type==ID_TYPE_UID)?ALLOC_HWM_UID:ALLOC_HWM_GID;
622
if (!dbwrap_fetch_uint32(autorid_db, hwmkey, &hwm)) {
623
DEBUG(1, ("Failed to fetch current allocation HWM value: %s\n",
625
return NT_STATUS_INTERNAL_ERROR;
628
if (hwm >= globalcfg->rangesize) {
629
DEBUG(1, ("allocation range is depleted!\n"));
630
return NT_STATUS_NO_MEMORY;
633
ret = dbwrap_change_uint32_atomic(autorid_db, hwmkey, &(xid->id), 1);
634
if (!NT_STATUS_IS_OK(ret)) {
635
DEBUG(1, ("Fatal error while allocating new ID!\n"));
639
xid->id = globalcfg->minvalue +
640
globalcfg->rangesize * domaincfg.domainnum +
643
DEBUG(10, ("Returned new %s %d from allocation range\n",
644
(xid->type==ID_TYPE_UID)?"uid":"gid", xid->id));
545
650
Close the idmap tdb instance
548
653
.init = idmap_autorid_initialize,
549
654
.unixids_to_sids = idmap_autorid_unixids_to_sids,
550
655
.sids_to_unixids = idmap_autorid_sids_to_unixids,
656
.allocate_id = idmap_autorid_allocate_id
553
659
NTSTATUS idmap_autorid_init(void)