2 * idmap_autorid: static map between Active Directory/NT RIDs
3 * and RFC 2307 accounts
5 * based on the idmap_rid module, but this module defines the ranges
6 * for the domains by automatically allocating a range for each domain
8 * Copyright (C) Christian Ambach, 2010-2011
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
26 #include "system/filesys.h"
30 #include "../libcli/security/dom_sid.h"
34 #define DBGC_CLASS DBGC_IDMAP
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"
40 #define CONFIGKEY "CONFIG"
42 struct autorid_global_config
{
48 struct autorid_domain_config
{
51 struct autorid_global_config
*globalcfg
;
54 /* handle to the tdb storing domain <-> range assignments */
55 static struct db_context
*autorid_db
;
57 static NTSTATUS
idmap_autorid_get_domainrange(struct db_context
*db
,
61 uint32_t domainnum
, hwm
;
63 struct autorid_domain_config
*cfg
;
65 cfg
= (struct autorid_domain_config
*)private_data
;
67 if (!dbwrap_fetch_uint32(db
, cfg
->sid
, &domainnum
)) {
68 DEBUG(10, ("Acquiring new range for domain %s\n", cfg
->sid
));
70 /* fetch the current HWM */
71 if (!dbwrap_fetch_uint32(db
, HWM
, &hwm
)) {
72 DEBUG(1, ("Fatal error while fetching current "
74 ret
= NT_STATUS_INTERNAL_ERROR
;
78 /* do we have a range left? */
79 if (hwm
>= cfg
->globalcfg
->maxranges
) {
80 DEBUG(1, ("No more domain ranges available!\n"));
81 ret
= NT_STATUS_NO_MEMORY
;
85 /* increase the HWM */
86 ret
= dbwrap_change_uint32_atomic(db
, HWM
, &domainnum
, 1);
87 if (!NT_STATUS_IS_OK(ret
)) {
88 DEBUG(1, ("Fatal error while fetching a new "
89 "domain range value!\n"));
93 /* store away the new mapping in both directions */
94 ret
= dbwrap_trans_store_uint32(db
, cfg
->sid
, domainnum
);
95 if (!NT_STATUS_IS_OK(ret
)) {
96 DEBUG(1, ("Fatal error while storing new "
97 "domain->range assignment!\n"));
101 numstr
= talloc_asprintf(db
, "%u", domainnum
);
103 ret
= NT_STATUS_NO_MEMORY
;
107 ret
= dbwrap_trans_store_bystring(db
, numstr
,
108 string_term_tdb_data(cfg
->sid
), TDB_INSERT
);
111 if (!NT_STATUS_IS_OK(ret
)) {
112 DEBUG(1, ("Fatal error while storing "
113 "new domain->range assignment!\n"));
116 DEBUG(5, ("Acquired new range #%d for domain %s\n",
117 domainnum
, cfg
->sid
));
120 DEBUG(10, ("Using range #%d for domain %s\n", domainnum
, cfg
->sid
));
121 cfg
->domainnum
= domainnum
;
130 static NTSTATUS
idmap_autorid_id_to_sid(struct autorid_global_config
*cfg
,
138 /* can this be one of our ids? */
139 if (map
->xid
.id
< cfg
->minvalue
) {
140 DEBUG(10, ("id %d is lower than minimum value, "
141 "ignoring mapping request\n", map
->xid
.id
));
142 map
->status
= ID_UNKNOWN
;
146 if (map
->xid
.id
> (cfg
->minvalue
+ cfg
->rangesize
* cfg
->maxranges
)) {
147 DEBUG(10, ("id %d is outside of maximum id value, "
148 "ignoring mapping request\n", map
->xid
.id
));
149 map
->status
= ID_UNKNOWN
;
153 /* determine the range of this uid */
154 range
= ((map
->xid
.id
- cfg
->minvalue
) / cfg
->rangesize
);
156 keystr
= talloc_asprintf(talloc_tos(), "%u", range
);
158 return NT_STATUS_NO_MEMORY
;
161 data
= dbwrap_fetch_bystring(autorid_db
, talloc_tos(), keystr
);
165 DEBUG(4, ("id %d belongs to range %d which does not have "
166 "domain mapping, ignoring mapping request\n",
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
);
180 map
->status
= ID_UNKNOWN
;
184 string_to_sid(&sid
, (const char *)data
.dptr
);
185 TALLOC_FREE(data
.dptr
);
187 sid_compose(map
->sid
, &sid
,
188 (map
->xid
.id
- cfg
->minvalue
-
189 range
* cfg
->rangesize
));
191 /* We **really** should have some way of validating
192 the SID exists and is the correct type here. But
193 that is a deficiency in the idmap_rid design. */
195 map
->status
= ID_MAPPED
;
199 /**********************************
200 Single sid to id lookup function.
201 **********************************/
203 static NTSTATUS
idmap_autorid_sid_to_id(struct autorid_global_config
*global
,
204 struct autorid_domain_config
*domain
,
209 sid_peek_rid(map
->sid
, &rid
);
211 /* if the rid is higher than the size of the range, we cannot map it */
212 if (rid
>= global
->rangesize
) {
213 map
->status
= ID_UNKNOWN
;
214 DEBUG(2, ("RID %d is larger then size of range (%d), "
215 "user cannot be mapped\n", rid
, global
->rangesize
));
216 return NT_STATUS_UNSUCCESSFUL
;
218 map
->xid
.id
= global
->minvalue
+
219 (global
->rangesize
* domain
->domainnum
)+rid
;
221 /* We **really** should have some way of validating
222 the SID exists and is the correct type here. But
223 that is a deficiency in the idmap_rid design. */
225 map
->status
= ID_MAPPED
;
230 /**********************************
231 lookup a set of unix ids.
232 **********************************/
234 static NTSTATUS
idmap_autorid_unixids_to_sids(struct idmap_domain
*dom
,
237 struct autorid_global_config
*globalcfg
;
241 /* initialize the status to avoid surprise */
242 for (i
= 0; ids
[i
]; i
++) {
243 ids
[i
]->status
= ID_UNKNOWN
;
246 globalcfg
= talloc_get_type(dom
->private_data
,
247 struct autorid_global_config
);
249 for (i
= 0; ids
[i
]; i
++) {
251 ret
= idmap_autorid_id_to_sid(globalcfg
, ids
[i
]);
253 if ((!NT_STATUS_IS_OK(ret
)) &&
254 (!NT_STATUS_EQUAL(ret
, NT_STATUS_NONE_MAPPED
))) {
255 /* some fatal error occurred, log it */
256 DEBUG(3, ("Unexpected error resolving an ID "
257 " (%d)\n", ids
[i
]->xid
.id
));
267 /**********************************
268 lookup a set of sids.
269 **********************************/
271 static NTSTATUS
idmap_autorid_sids_to_unixids(struct idmap_domain
*dom
,
274 struct autorid_global_config
*global
;
278 /* initialize the status to avoid surprise */
279 for (i
= 0; ids
[i
]; i
++) {
280 ids
[i
]->status
= ID_UNKNOWN
;
283 global
= talloc_get_type(dom
->private_data
,
284 struct autorid_global_config
);
286 for (i
= 0; ids
[i
]; i
++) {
287 struct winbindd_tdc_domain
*domain
;
288 struct autorid_domain_config domaincfg
;
290 struct dom_sid domainsid
;
292 ZERO_STRUCT(domaincfg
);
294 sid_copy(&domainsid
, ids
[i
]->sid
);
295 if (!sid_split_rid(&domainsid
, &rid
)) {
296 DEBUG(4, ("Could not determine domain SID from %s, "
297 "ignoring mapping request\n",
298 sid_string_dbg(ids
[i
]->sid
)));
303 * Check if the domain is around
305 domain
= wcache_tdc_fetch_domainbysid(talloc_tos(),
307 if (domain
== NULL
) {
308 DEBUG(10, ("Ignoring unknown domain sid %s\n",
309 sid_string_dbg(&domainsid
)));
314 domaincfg
.globalcfg
= global
;
315 sid_to_fstring(domaincfg
.sid
, &domainsid
);
317 ret
= dbwrap_trans_do(autorid_db
,
318 idmap_autorid_get_domainrange
,
321 if (!NT_STATUS_IS_OK(ret
)) {
322 DEBUG(3, ("Could not determine range for domain, "
323 "check previous messages for reason\n"));
327 ret
= idmap_autorid_sid_to_id(global
, &domaincfg
, ids
[i
]);
329 if ((!NT_STATUS_IS_OK(ret
)) &&
330 (!NT_STATUS_EQUAL(ret
, NT_STATUS_NONE_MAPPED
))) {
331 /* some fatal error occurred, log it */
332 DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
333 sid_string_dbg(ids
[i
]->sid
)));
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
;
365 * open and initialize the database which stores the ranges for the domains
367 static NTSTATUS
idmap_autorid_db_init(void)
372 /* its already open */
376 /* Open idmap repository */
377 autorid_db
= db_open(NULL
, state_path("autorid.tdb"), 0,
378 TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0644);
381 DEBUG(0, ("Unable to open idmap_autorid database '%s'\n",
382 state_path("autorid.tdb")));
383 return NT_STATUS_UNSUCCESSFUL
;
386 /* Initialize high water mark for the currently used range to 0 */
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
);
399 static struct autorid_global_config
*idmap_autorid_loadconfig(TALLOC_CTX
* ctx
)
403 struct autorid_global_config
*cfg
;
404 unsigned long minvalue
, rangesize
, maxranges
;
406 data
= dbwrap_fetch_bystring(autorid_db
, ctx
, CONFIGKEY
);
409 DEBUG(10, ("No saved config found\n"));
413 cfg
= TALLOC_ZERO_P(ctx
, struct autorid_global_config
);
418 if (sscanf((char *)data
.dptr
,
419 "minvalue:%lu rangesize:%lu maxranges:%lu",
420 &minvalue
, &rangesize
, &maxranges
) != 3) {
422 ("Found invalid configuration data"
423 "creating new config\n"));
427 cfg
->minvalue
= minvalue
;
428 cfg
->rangesize
= rangesize
;
429 cfg
->maxranges
= maxranges
;
431 DEBUG(10, ("Loaded previously stored configuration "
432 "minvalue:%d rangesize:%d\n",
433 cfg
->minvalue
, cfg
->rangesize
));
439 static NTSTATUS
idmap_autorid_saveconfig(struct autorid_global_config
*cfg
)
447 talloc_asprintf(talloc_tos(),
448 "minvalue:%u rangesize:%u maxranges:%u",
449 cfg
->minvalue
, cfg
->rangesize
, cfg
->maxranges
);
452 return NT_STATUS_NO_MEMORY
;
455 data
= string_tdb_data(cfgstr
);
457 status
= dbwrap_trans_store_bystring(autorid_db
, CONFIGKEY
,
465 static NTSTATUS
idmap_autorid_initialize(struct idmap_domain
*dom
)
467 struct autorid_global_config
*config
;
468 struct autorid_global_config
*storedconfig
= NULL
;
472 if (!strequal(dom
->name
, "*")) {
473 DEBUG(0, ("idmap_autorid_initialize: Error: autorid configured "
474 "for domain '%s'. But autorid can only be used for "
475 "the default idmap configuration.\n", dom
->name
));
476 return NT_STATUS_INVALID_PARAMETER
;
479 config
= TALLOC_ZERO_P(dom
, struct autorid_global_config
);
481 DEBUG(0, ("Out of memory!\n"));
482 return NT_STATUS_NO_MEMORY
;
485 status
= idmap_autorid_db_init();
486 if (!NT_STATUS_IS_OK(status
)) {
490 config
->minvalue
= dom
->low_id
;
491 config
->rangesize
= lp_parm_int(-1, "idmap config *", "rangesize", 100000);
493 if (config
->rangesize
< 2000) {
494 DEBUG(1, ("autorid rangesize must be at least 2000\n"));
495 status
= NT_STATUS_INVALID_PARAMETER
;
499 config
->maxranges
= (dom
->high_id
- dom
->low_id
+ 1) /
502 if (config
->maxranges
== 0) {
503 DEBUG(1, ("allowed uid range is smaller then rangesize, "
504 "increase uid range or decrease rangesize\n"));
505 status
= NT_STATUS_INVALID_PARAMETER
;
509 /* check if the high-low limit is a multiple of the rangesize */
510 if ((dom
->high_id
- dom
->low_id
+ 1) % config
->rangesize
!= 0) {
511 DEBUG(5, ("High uid-low uid difference of %d "
512 "is not a multiple of the rangesize %d, "
513 "limiting ranges to lower boundary number of %d\n",
514 (dom
->high_id
- dom
->low_id
+ 1), config
->rangesize
,
518 DEBUG(10, ("Current configuration in config is "
519 "minvalue:%d rangesize:%d maxranges:%d\n",
520 config
->minvalue
, config
->rangesize
, config
->maxranges
));
522 /* read previously stored config and current HWM */
523 storedconfig
= idmap_autorid_loadconfig(talloc_tos());
525 if (!dbwrap_fetch_uint32(autorid_db
, HWM
, &hwm
)) {
526 DEBUG(1, ("Fatal error while fetching current "
528 status
= NT_STATUS_INTERNAL_ERROR
;
532 /* did the minimum value or rangesize change? */
534 ((storedconfig
->minvalue
!= config
->minvalue
) ||
535 (storedconfig
->rangesize
!= config
->rangesize
))) {
536 DEBUG(1, ("New configuration values for rangesize or "
537 "minimum uid value conflict with previously "
538 "used values! Aborting initialization\n"));
539 status
= NT_STATUS_INVALID_PARAMETER
;
544 * has the highest uid value been reduced to setting that is not
545 * sufficient any more for already existing ranges?
547 if (hwm
> config
->maxranges
) {
548 DEBUG(1, ("New upper uid limit is too low to cover "
549 "existing mappings! Aborting initialization\n"));
550 status
= NT_STATUS_INVALID_PARAMETER
;
554 status
= idmap_autorid_saveconfig(config
);
556 if (!NT_STATUS_IS_OK(status
)) {
557 DEBUG(1, ("Failed to store configuration data!\n"));
561 DEBUG(5, ("%d domain ranges with a size of %d are available\n",
562 config
->maxranges
, config
->rangesize
));
564 dom
->private_data
= config
;
572 talloc_free(storedconfig
);
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
));
650 Close the idmap tdb instance
652 static struct idmap_methods autorid_methods
= {
653 .init
= idmap_autorid_initialize
,
654 .unixids_to_sids
= idmap_autorid_unixids_to_sids
,
655 .sids_to_unixids
= idmap_autorid_sids_to_unixids
,
656 .allocate_id
= idmap_autorid_allocate_id
659 NTSTATUS
idmap_autorid_init(void)
661 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
,
662 "autorid", &autorid_methods
);