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
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/>.
29 #include "../libcli/security/dom_sid.h"
32 #define DBGC_CLASS DBGC_IDMAP
34 #define HWM "NEXT RANGE"
35 struct autorid_global_config
{
41 struct autorid_domain_config
{
44 struct autorid_global_config
*globalcfg
;
47 /* handle to the tdb storing domain <-> range assignments */
48 static struct db_context
*autorid_db
;
50 static NTSTATUS
idmap_autorid_get_domainrange(struct db_context
*db
,
54 uint32_t domainnum
, hwm
;
55 char *sidstr
, *numstr
;
56 struct autorid_domain_config
*cfg
;
58 cfg
= (struct autorid_domain_config
*)private_data
;
59 sidstr
= dom_sid_string(talloc_tos(), &(cfg
->sid
));
62 return NT_STATUS_NO_MEMORY
;
65 if (!dbwrap_fetch_uint32(db
, sidstr
, &domainnum
)) {
66 DEBUG(10, ("Acquiring new range for domain %s\n", sidstr
));
68 /* fetch the current HWM */
69 if (!dbwrap_fetch_uint32(db
, HWM
, &hwm
)) {
70 DEBUG(1, ("Fatal error while fetching current "
75 /* do we have a range left? */
76 if (hwm
>= cfg
->globalcfg
->maxranges
) {
77 DEBUG(1, ("No more domain ranges available!\n"));
78 ret
= NT_STATUS_NO_MEMORY
;
82 /* increase the HWM */
83 ret
= dbwrap_change_uint32_atomic(db
, HWM
, &domainnum
, 1);
84 if (!NT_STATUS_IS_OK(ret
)) {
85 DEBUG(1, ("Fatal error while fetching a new "
86 "domain range value!\n"));
90 /* store away the new mapping in both directions */
91 ret
= dbwrap_trans_store_uint32(db
, sidstr
, domainnum
);
92 if (!NT_STATUS_IS_OK(ret
)) {
93 DEBUG(1, ("Fatal error while storing new "
94 "domain->range assignment!\n"));
98 numstr
= talloc_asprintf(db
, "%u", domainnum
);
100 ret
= NT_STATUS_NO_MEMORY
;
104 ret
= dbwrap_trans_store_bystring(db
, numstr
,
105 string_term_tdb_data(sidstr
),
107 if (!NT_STATUS_IS_OK(ret
)) {
109 DEBUG(1, ("Fatal error while storing "
110 "new domain->range assignment!\n"));
114 DEBUG(5, ("Acquired new range #%d for domain %s\n",
118 DEBUG(10, ("Using range #%d for domain %s\n", domainnum
, sidstr
));
119 cfg
->domainnum
= domainnum
;
130 static NTSTATUS
idmap_autorid_id_to_sid(TALLOC_CTX
* memctx
,
131 struct autorid_global_config
*cfg
,
139 /* can this be one of our ids? */
140 if (map
->xid
.id
< cfg
->minvalue
) {
141 DEBUG(10, ("id %d is lower than minimum value, "
142 "ignoring mapping request\n", map
->xid
.id
));
143 map
->status
= ID_UNKNOWN
;
147 if (map
->xid
.id
> (cfg
->minvalue
+ cfg
->rangesize
* cfg
->maxranges
)) {
148 DEBUG(10, ("id %d is outside of maximum id value, "
149 "ignoring mapping request\n", map
->xid
.id
));
150 map
->status
= ID_UNKNOWN
;
154 /* determine the range of this uid */
155 range
= ((map
->xid
.id
- cfg
->minvalue
) / cfg
->rangesize
);
157 keystr
= talloc_asprintf(memctx
, "%u", range
);
159 return NT_STATUS_NO_MEMORY
;
162 data
= dbwrap_fetch_bystring(autorid_db
, memctx
, 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
));
169 string_to_sid(&sid
, (const char *)data
.dptr
);
171 sid_compose(map
->sid
, &sid
,
172 (map
->xid
.id
- cfg
->minvalue
-
173 range
* cfg
->rangesize
));
175 /* We **really** should have some way of validating
176 the SID exists and is the correct type here. But
177 that is a deficiency in the idmap_rid design. */
179 map
->status
= ID_MAPPED
;
184 /**********************************
185 Single sid to id lookup function.
186 **********************************/
188 static NTSTATUS
idmap_autorid_sid_to_id(TALLOC_CTX
* memctx
,
189 struct autorid_global_config
*global
,
190 struct autorid_domain_config
*domain
,
195 sid_peek_rid(map
->sid
, &rid
);
197 /* if the rid is higher than the size of the range, we cannot map it */
198 if (rid
>= global
->rangesize
) {
199 map
->status
= ID_UNKNOWN
;
200 DEBUG(2, ("RID %d is larger then size of range (%d), "
201 "user cannot be mapped\n", rid
, global
->rangesize
));
202 return NT_STATUS_UNSUCCESSFUL
;
204 map
->xid
.id
= global
->minvalue
+
205 (global
->rangesize
* domain
->domainnum
)+rid
;
207 /* We **really** should have some way of validating
208 the SID exists and is the correct type here. But
209 that is a deficiency in the idmap_rid design. */
211 map
->status
= ID_MAPPED
;
216 /**********************************
217 lookup a set of unix ids.
218 **********************************/
220 static NTSTATUS
idmap_autorid_unixids_to_sids(struct idmap_domain
*dom
,
223 struct autorid_global_config
*globalcfg
;
228 /* initialize the status to avoid surprise */
229 for (i
= 0; ids
[i
]; i
++) {
230 ids
[i
]->status
= ID_UNKNOWN
;
233 globalcfg
= talloc_get_type(dom
->private_data
,
234 struct autorid_global_config
);
236 ctx
= talloc_new(dom
);
238 DEBUG(0, ("Out of memory!\n"));
239 return NT_STATUS_NO_MEMORY
;
242 for (i
= 0; ids
[i
]; i
++) {
244 ret
= idmap_autorid_id_to_sid(ctx
, globalcfg
, ids
[i
]);
246 if ((!NT_STATUS_IS_OK(ret
)) &&
247 (!NT_STATUS_EQUAL(ret
, NT_STATUS_NONE_MAPPED
))) {
248 /* some fatal error occurred, log it */
249 DEBUG(3, ("Unexpected error resolving an ID "
250 " (%d)\n", ids
[i
]->xid
.id
));
264 /**********************************
265 lookup a set of sids.
266 **********************************/
268 static NTSTATUS
idmap_autorid_sids_to_unixids(struct idmap_domain
*dom
,
271 struct autorid_global_config
*global
;
272 struct autorid_domain_config
*domaincfg
;
273 struct dom_sid
*domainsid
;
274 struct winbindd_tdc_domain
*domain
;
280 ctx
= talloc_new(dom
);
281 domainsid
= talloc(ctx
, struct dom_sid
);
282 if (!ctx
|| !domainsid
) {
283 DEBUG(0, ("Out of memory!\n"));
284 ret
= NT_STATUS_NO_MEMORY
;
288 if ((domaincfg
= TALLOC_ZERO_P(ctx
,
289 struct autorid_domain_config
)) == NULL
) {
290 DEBUG(0, ("Out of memory!\n"));
291 ret
= NT_STATUS_NO_MEMORY
;
295 /* initialize the status to avoid surprise */
296 for (i
= 0; ids
[i
]; i
++) {
297 ids
[i
]->status
= ID_UNKNOWN
;
300 global
= talloc_get_type(dom
->private_data
,
301 struct autorid_global_config
);
303 for (i
= 0; ids
[i
]; i
++) {
304 ZERO_STRUCTP(domainsid
);
305 sid_copy(domainsid
, ids
[i
]->sid
);
306 if (!sid_split_rid(domainsid
, &rid
)) {
307 DEBUG(4, ("Could not determine domain SID from %s, "
308 "ignoring mapping request\n",
309 sid_string_dbg(ids
[i
]->sid
)));
313 domain
= wcache_tdc_fetch_domainbysid(ctx
, domainsid
);
314 if (domain
== NULL
) {
315 DEBUG(10, ("Ignoring unknown domain sid %s\n",
316 sid_string_dbg(domainsid
)));
320 ZERO_STRUCTP(domaincfg
);
321 domaincfg
->sid
= domain
->sid
;
322 domaincfg
->globalcfg
= global
;
324 ret
= dbwrap_trans_do(autorid_db
, idmap_autorid_get_domainrange
,
327 if (!NT_STATUS_IS_OK(ret
)) {
328 DEBUG(3, ("Could not determine range for domain, "
329 "check previous messages for reason\n"));
333 ret
= idmap_autorid_sid_to_id(ctx
, global
, domaincfg
, ids
[i
]);
335 if ((!NT_STATUS_IS_OK(ret
)) &&
336 (!NT_STATUS_EQUAL(ret
, NT_STATUS_NONE_MAPPED
))) {
337 /* some fatal error occurred, log it */
338 DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
339 sid_string_dbg(ids
[i
]->sid
)));
354 * open and initialize the database which stores the ranges for the domains
356 static NTSTATUS
idmap_autorid_db_init(void)
361 /* its already open */
365 /* Open idmap repository */
366 autorid_db
= db_open(NULL
, state_path("autorid.tdb"), 0,
367 TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0644);
370 DEBUG(0, ("Unable to open idmap_autorid database '%s'\n",
371 state_path("autorid.tdb")));
372 return NT_STATUS_UNSUCCESSFUL
;
375 /* Initialize high water mark for the currently used range to 0 */
376 hwm
= dbwrap_fetch_int32(autorid_db
, HWM
);
379 (dbwrap_trans_store_int32(autorid_db
, HWM
, 0))) {
381 ("Unable to initialise HWM in autorid "
383 return NT_STATUS_INTERNAL_DB_ERROR
;
390 static NTSTATUS
idmap_autorid_initialize(struct idmap_domain
*dom
,
393 struct autorid_global_config
*config
;
396 config
= TALLOC_ZERO_P(dom
, struct autorid_global_config
);
398 DEBUG(0, ("Out of memory!\n"));
399 return NT_STATUS_NO_MEMORY
;
402 status
= idmap_autorid_db_init();
403 if (!NT_STATUS_IS_OK(status
)) {
407 config
->minvalue
= dom
->low_id
;
408 config
->rangesize
= lp_parm_int(-1, "autorid", "rangesize", 100000);
410 if (config
->rangesize
< 2000) {
411 DEBUG(1, ("autorid rangesize must be at least 2000\n"));
412 status
= NT_STATUS_INVALID_PARAMETER
;
416 config
->maxranges
= (dom
->high_id
- dom
->low_id
+ 1) /
419 if (config
->maxranges
== 0) {
420 DEBUG(1, ("allowed uid range is smaller then rangesize, "
421 "increase uid range or decrease rangesize\n"));
422 status
= NT_STATUS_INVALID_PARAMETER
;
426 /* check if the high-low limit is a multiple of the rangesize */
427 if ((dom
->high_id
- dom
->low_id
+ 1) % config
->rangesize
!= 0) {
428 DEBUG(5, ("High uid-low uid difference of %d "
429 "is not a multiple of the rangesize %d, "
430 "limiting ranges to lower boundary number of %d\n",
431 (dom
->high_id
- dom
->low_id
+ 1), config
->rangesize
,
435 DEBUG(5, ("%d domain ranges with a size of %d are available\n",
436 config
->maxranges
, config
->rangesize
));
438 dom
->private_data
= config
;
440 if (!NT_STATUS_IS_OK(status
)) {
452 Close the idmap tdb instance
454 static NTSTATUS
idmap_autorid_close(struct idmap_domain
*dom
)
456 /* don't do anything */
460 static struct idmap_methods autorid_methods
= {
461 .init
= idmap_autorid_initialize
,
462 .unixids_to_sids
= idmap_autorid_unixids_to_sids
,
463 .sids_to_unixids
= idmap_autorid_sids_to_unixids
,
464 .close_fn
= idmap_autorid_close
467 NTSTATUS
idmap_autorid_init(void)
469 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
,
470 "autorid", &autorid_methods
);