2 Unix SMB/CIFS implementation.
4 common functions for TDB based idmapping backends
6 Copyright (C) Christian Ambach 2012
8 These functions were initially copied over from idmap_tdb.c and idmap_tdb2.c
11 Copyright (C) Tim Potter 2000
12 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
13 Copyright (C) Jeremy Allison 2006
14 Copyright (C) Simo Sorce 2003-2006
15 Copyright (C) Michael Adam 2009-2010
16 Copyright (C) Andrew Tridgell 2007
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include "idmap_tdb_common.h"
35 #include "dbwrap/dbwrap.h"
38 #include "../libcli/security/dom_sid.h"
41 #define DBGC_CLASS DBGC_IDMAP
43 struct idmap_tdb_common_allocate_id_context
{
50 static NTSTATUS
idmap_tdb_common_allocate_id_action(struct db_context
*db
,
54 struct idmap_tdb_common_allocate_id_context
*state
= private_data
;
57 ret
= dbwrap_fetch_uint32_bystring(db
, state
->hwmkey
, &hwm
);
58 if (!NT_STATUS_IS_OK(ret
)) {
59 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
63 /* check it is in the range */
64 if (hwm
> state
->high_hwm
) {
65 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
66 state
->hwmtype
, (unsigned long)state
->high_hwm
));
67 ret
= NT_STATUS_UNSUCCESSFUL
;
71 /* fetch a new id and increment it */
72 ret
= dbwrap_change_uint32_atomic_bystring(db
, state
->hwmkey
, &hwm
, 1);
73 if (!NT_STATUS_IS_OK(ret
)) {
74 DEBUG(1, ("Fatal error while fetching a new %s value\n!",
79 /* recheck it is in the range */
80 if (hwm
> state
->high_hwm
) {
81 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
82 state
->hwmtype
, (unsigned long)state
->high_hwm
));
83 ret
= NT_STATUS_UNSUCCESSFUL
;
94 static NTSTATUS
idmap_tdb_common_allocate_id(struct idmap_domain
*dom
,
101 struct idmap_tdb_common_allocate_id_context state
;
102 struct idmap_tdb_common_context
*ctx
;
105 talloc_get_type_abort(dom
->private_data
,
106 struct idmap_tdb_common_context
);
108 /* Get current high water mark */
112 hwmkey
= ctx
->hwmkey_uid
;
117 hwmkey
= ctx
->hwmkey_gid
;
122 DEBUG(2, ("Invalid ID type (0x%x)\n", xid
->type
));
123 return NT_STATUS_INVALID_PARAMETER
;
127 state
.high_hwm
= ctx
->max_id
;
128 state
.hwmtype
= hwmtype
;
129 state
.hwmkey
= hwmkey
;
131 status
= dbwrap_trans_do(ctx
->db
, idmap_tdb_common_allocate_id_action
,
134 if (NT_STATUS_IS_OK(status
)) {
136 DEBUG(10, ("New %s = %d\n", hwmtype
, state
.hwm
));
138 DEBUG(1, ("Error allocating a new %s\n", hwmtype
));
145 * Allocate a new unix-ID.
146 * For now this is for the default idmap domain only.
147 * Should be extended later on.
149 NTSTATUS
idmap_tdb_common_get_new_id(struct idmap_domain
* dom
,
154 if (!strequal(dom
->name
, "*")) {
155 DEBUG(3, ("idmap_tdb_common_get_new_id: "
156 "Refusing allocation of a new unixid for domain'%s'. "
157 "Currently only supported for the default "
158 "domain \"*\".\n", dom
->name
));
159 return NT_STATUS_NOT_IMPLEMENTED
;
162 ret
= idmap_tdb_common_allocate_id(dom
, id
);
168 * store a mapping in the database.
171 struct idmap_tdb_common_set_mapping_context
{
176 static NTSTATUS
idmap_tdb_common_set_mapping_action(struct db_context
*db
,
181 struct idmap_tdb_common_set_mapping_context
*state
= private_data
;
182 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
184 DEBUG(10, ("Storing %s <-> %s map\n", state
->ksidstr
, state
->kidstr
));
186 /* check whether sid mapping is already present in db */
187 ret
= dbwrap_fetch_bystring(db
, tmp_ctx
, state
->ksidstr
, &data
);
188 if (NT_STATUS_IS_OK(ret
)) {
189 ret
= NT_STATUS_OBJECT_NAME_COLLISION
;
193 ret
= dbwrap_store_bystring(db
, state
->ksidstr
,
194 string_term_tdb_data(state
->kidstr
),
196 if (!NT_STATUS_IS_OK(ret
)) {
197 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret
)));
201 ret
= dbwrap_store_bystring(db
, state
->kidstr
,
202 string_term_tdb_data(state
->ksidstr
),
204 if (!NT_STATUS_IS_OK(ret
)) {
205 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret
)));
206 /* try to remove the previous stored SID -> ID map */
207 dbwrap_delete_bystring(db
, state
->ksidstr
);
211 DEBUG(10, ("Stored %s <-> %s\n", state
->ksidstr
, state
->kidstr
));
214 talloc_free(tmp_ctx
);
218 NTSTATUS
idmap_tdb_common_set_mapping(struct idmap_domain
* dom
,
219 const struct id_map
* map
)
221 struct idmap_tdb_common_context
*ctx
;
222 struct idmap_tdb_common_set_mapping_context state
;
224 char *ksidstr
, *kidstr
;
226 if (!map
|| !map
->sid
) {
227 return NT_STATUS_INVALID_PARAMETER
;
230 ksidstr
= kidstr
= NULL
;
232 /* TODO: should we filter a set_mapping using low/high filters ? */
235 talloc_get_type_abort(dom
->private_data
,
236 struct idmap_tdb_common_context
);
238 switch (map
->xid
.type
) {
242 talloc_asprintf(ctx
, "UID %lu", (unsigned long)map
->xid
.id
);
247 talloc_asprintf(ctx
, "GID %lu", (unsigned long)map
->xid
.id
);
251 DEBUG(2, ("INVALID unix ID type: 0x%02x\n", map
->xid
.type
));
252 return NT_STATUS_INVALID_PARAMETER
;
255 if (kidstr
== NULL
) {
256 DEBUG(0, ("ERROR: Out of memory!\n"));
257 ret
= NT_STATUS_NO_MEMORY
;
261 ksidstr
= sid_string_talloc(ctx
, map
->sid
);
262 if (ksidstr
== NULL
) {
263 DEBUG(0, ("Out of memory!\n"));
264 ret
= NT_STATUS_NO_MEMORY
;
268 state
.ksidstr
= ksidstr
;
269 state
.kidstr
= kidstr
;
271 ret
= dbwrap_trans_do(ctx
->db
, idmap_tdb_common_set_mapping_action
,
275 talloc_free(ksidstr
);
281 * Create a new mapping for an unmapped SID, also allocating a new ID.
282 * This should be run inside a transaction.
285 * Properly integrate this with multi domain idmap config:
286 * Currently, the allocator is default-config only.
288 NTSTATUS
idmap_tdb_common_new_mapping(struct idmap_domain
* dom
,
292 struct idmap_tdb_common_context
*ctx
;
295 talloc_get_type_abort(dom
->private_data
,
296 struct idmap_tdb_common_context
);
298 ret
= idmap_rw_new_mapping(dom
, ctx
->rw_ops
, map
);
304 lookup a set of unix ids
306 NTSTATUS
idmap_tdb_common_unixids_to_sids(struct idmap_domain
* dom
,
307 struct id_map
** ids
)
310 size_t i
, num_mapped
= 0;
311 struct idmap_tdb_common_context
*ctx
;
313 NTSTATUS(*unixid_to_sid_fn
) (struct idmap_domain
* dom
,
314 struct id_map
* map
);
316 talloc_get_type_abort(dom
->private_data
,
317 struct idmap_tdb_common_context
);
319 if (ctx
->unixid_to_sid_fn
== NULL
) {
320 unixid_to_sid_fn
= idmap_tdb_common_unixid_to_sid
;
322 unixid_to_sid_fn
= ctx
->unixid_to_sid_fn
;
325 /* initialize the status to avoid surprise */
326 for (i
= 0; ids
[i
]; i
++) {
327 ids
[i
]->status
= ID_UNKNOWN
;
330 for (i
= 0; ids
[i
]; i
++) {
331 ret
= unixid_to_sid_fn(dom
, ids
[i
]);
332 if (!NT_STATUS_IS_OK(ret
)) {
334 /* if it is just a failed mapping continue */
335 if (NT_STATUS_EQUAL(ret
, NT_STATUS_NONE_MAPPED
)) {
337 /* make sure it is marked as unmapped */
338 ids
[i
]->status
= ID_UNMAPPED
;
342 /* some fatal error occurred, return immediately */
346 /* all ok, id is mapped */
347 ids
[i
]->status
= ID_MAPPED
;
355 if (NT_STATUS_IS_OK(ret
)) {
356 if (i
== 0 || num_mapped
== 0) {
357 ret
= NT_STATUS_NONE_MAPPED
;
358 } else if (num_mapped
< i
) {
359 ret
= STATUS_SOME_UNMAPPED
;
369 default single id to sid lookup function
371 NTSTATUS
idmap_tdb_common_unixid_to_sid(struct idmap_domain
* dom
,
377 struct idmap_tdb_common_context
*ctx
;
380 return NT_STATUS_INVALID_PARAMETER
;
384 talloc_get_type_abort(dom
->private_data
,
385 struct idmap_tdb_common_context
);
387 /* apply filters before checking */
388 if (!idmap_unix_id_is_in_range(map
->xid
.id
, dom
)) {
390 ("Requested id (%u) out of range (%u - %u). Filtered!\n",
391 map
->xid
.id
, dom
->low_id
, dom
->high_id
));
392 return NT_STATUS_NONE_MAPPED
;
395 switch (map
->xid
.type
) {
399 talloc_asprintf(ctx
, "UID %lu", (unsigned long)map
->xid
.id
);
404 talloc_asprintf(ctx
, "GID %lu", (unsigned long)map
->xid
.id
);
408 DEBUG(2, ("INVALID unix ID type: 0x%02x\n", map
->xid
.type
));
409 return NT_STATUS_INVALID_PARAMETER
;
412 if (keystr
== NULL
) {
413 DEBUG(0, ("Out of memory!\n"));
414 ret
= NT_STATUS_NO_MEMORY
;
418 DEBUG(10, ("Fetching record %s\n", keystr
));
420 /* Check if the mapping exists */
421 ret
= dbwrap_fetch_bystring(ctx
->db
, keystr
, keystr
, &data
);
423 if (!NT_STATUS_IS_OK(ret
)) {
424 DEBUG(10, ("Record %s not found\n", keystr
));
425 ret
= NT_STATUS_NONE_MAPPED
;
429 if ((data
.dsize
== 0) || (data
.dptr
[data
.dsize
-1] != '\0')) {
430 DBG_DEBUG("Invalid record length %zu\n", data
.dsize
);
431 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
435 if (!string_to_sid(map
->sid
, (const char *)data
.dptr
)) {
436 DEBUG(10, ("INVALID SID (%s) in record %s\n",
437 (const char *)data
.dptr
, keystr
));
438 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
442 DEBUG(10, ("Found record %s -> %s\n", keystr
, (const char *)data
.dptr
));
450 /**********************************
451 Single sid to id lookup function.
452 **********************************/
454 NTSTATUS
idmap_tdb_common_sid_to_unixid(struct idmap_domain
* dom
,
460 unsigned long rec_id
= 0;
461 struct idmap_tdb_common_context
*ctx
;
462 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
465 talloc_free(tmp_ctx
);
466 return NT_STATUS_INVALID_PARAMETER
;
470 talloc_get_type_abort(dom
->private_data
,
471 struct idmap_tdb_common_context
);
473 keystr
= sid_string_talloc(tmp_ctx
, map
->sid
);
474 if (keystr
== NULL
) {
475 DEBUG(0, ("Out of memory!\n"));
476 ret
= NT_STATUS_NO_MEMORY
;
480 DEBUG(10, ("Fetching record %s\n", keystr
));
482 /* Check if sid is present in database */
483 ret
= dbwrap_fetch_bystring(ctx
->db
, tmp_ctx
, keystr
, &data
);
484 if (!NT_STATUS_IS_OK(ret
)) {
485 DEBUG(10, ("Record %s not found\n", keystr
));
486 ret
= NT_STATUS_NONE_MAPPED
;
490 /* What type of record is this ? */
491 if (sscanf((const char *)data
.dptr
, "UID %lu", &rec_id
) == 1) {
492 /* Try a UID record. */
493 map
->xid
.id
= rec_id
;
494 map
->xid
.type
= ID_TYPE_UID
;
496 ("Found uid record %s -> %s \n", keystr
,
497 (const char *)data
.dptr
));
500 } else if (sscanf((const char *)data
.dptr
, "GID %lu", &rec_id
) == 1) {
501 /* Try a GID record. */
502 map
->xid
.id
= rec_id
;
503 map
->xid
.type
= ID_TYPE_GID
;
505 ("Found gid record %s -> %s \n", keystr
,
506 (const char *)data
.dptr
));
509 } else { /* Unknown record type ! */
511 ("Found INVALID record %s -> %s\n", keystr
,
512 (const char *)data
.dptr
));
513 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
517 /* apply filters before returning result */
518 if (!idmap_unix_id_is_in_range(map
->xid
.id
, dom
)) {
520 ("Requested id (%u) out of range (%u - %u). Filtered!\n",
521 map
->xid
.id
, dom
->low_id
, dom
->high_id
));
522 ret
= NT_STATUS_NONE_MAPPED
;
526 talloc_free(tmp_ctx
);
530 /**********************************
532 **********************************/
534 struct idmap_tdb_common_sids_to_unixids_context
{
535 struct idmap_domain
*dom
;
537 bool allocate_unmapped
;
538 NTSTATUS(*sid_to_unixid_fn
) (struct idmap_domain
* dom
,
539 struct id_map
* map
);
542 static NTSTATUS
idmap_tdb_common_sids_to_unixids_action(struct db_context
*db
,
545 struct idmap_tdb_common_sids_to_unixids_context
*state
= private_data
;
546 size_t i
, num_mapped
= 0;
547 NTSTATUS ret
= NT_STATUS_OK
;
549 DEBUG(10, ("idmap_tdb_common_sids_to_unixids: "
550 " domain: [%s], allocate: %s\n",
551 state
->dom
->name
, state
->allocate_unmapped
? "yes" : "no"));
553 for (i
= 0; state
->ids
[i
]; i
++) {
554 if ((state
->ids
[i
]->status
== ID_UNKNOWN
) ||
555 /* retry if we could not map in previous run: */
556 (state
->ids
[i
]->status
== ID_UNMAPPED
)) {
559 ret2
= state
->sid_to_unixid_fn(state
->dom
,
562 if (!NT_STATUS_IS_OK(ret2
)) {
564 /* if it is just a failed mapping, continue */
566 (ret2
, NT_STATUS_NONE_MAPPED
)) {
568 /* make sure it is marked as unmapped */
569 state
->ids
[i
]->status
= ID_UNMAPPED
;
570 ret
= STATUS_SOME_UNMAPPED
;
573 * some fatal error occurred,
580 /* all ok, id is mapped */
581 state
->ids
[i
]->status
= ID_MAPPED
;
585 if (state
->ids
[i
]->status
== ID_MAPPED
) {
589 if ((state
->ids
[i
]->status
== ID_UNMAPPED
) &&
590 state
->allocate_unmapped
) {
592 idmap_tdb_common_new_mapping(state
->dom
,
594 if (!NT_STATUS_IS_OK(ret
)) {
603 if (NT_STATUS_IS_OK(ret
) ||
604 NT_STATUS_EQUAL(ret
, STATUS_SOME_UNMAPPED
)) {
605 if (i
== 0 || num_mapped
== 0) {
606 ret
= NT_STATUS_NONE_MAPPED
;
607 } else if (num_mapped
< i
) {
608 ret
= STATUS_SOME_UNMAPPED
;
617 NTSTATUS
idmap_tdb_common_sids_to_unixids(struct idmap_domain
* dom
,
618 struct id_map
** ids
)
622 struct idmap_tdb_common_sids_to_unixids_context state
;
623 struct idmap_tdb_common_context
*ctx
;
626 talloc_get_type_abort(dom
->private_data
,
627 struct idmap_tdb_common_context
);
629 /* initialize the status to avoid surprise */
630 for (i
= 0; ids
[i
]; i
++) {
631 ids
[i
]->status
= ID_UNKNOWN
;
636 state
.allocate_unmapped
= false;
637 if (ctx
->sid_to_unixid_fn
== NULL
) {
638 state
.sid_to_unixid_fn
= idmap_tdb_common_sid_to_unixid
;
640 state
.sid_to_unixid_fn
= ctx
->sid_to_unixid_fn
;
643 ret
= idmap_tdb_common_sids_to_unixids_action(ctx
->db
, &state
);
645 if ( (NT_STATUS_EQUAL(ret
, STATUS_SOME_UNMAPPED
) ||
646 NT_STATUS_EQUAL(ret
, NT_STATUS_NONE_MAPPED
)) &&
648 state
.allocate_unmapped
= true;
649 ret
= dbwrap_trans_do(ctx
->db
,
650 idmap_tdb_common_sids_to_unixids_action
,