2 Unix SMB/CIFS implementation.
4 idmap TDB2 backend, used for clustered Samba setups.
6 This uses dbwrap to access tdb files. The location can be set
7 using tdb:idmap2.tdb =" in smb.conf
9 Copyright (C) Andrew Tridgell 2007
11 This is heavily based upon idmap_tdb.c, which is:
13 Copyright (C) Tim Potter 2000
14 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
15 Copyright (C) Jeremy Allison 2006
16 Copyright (C) Simo Sorce 2003-2006
17 Copyright (C) Michael Adam 2009-2010
19 This program is free software; you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation; either version 2 of the License, or
22 (at your option) any later version.
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include "system/filesys.h"
39 #include "dbwrap/dbwrap.h"
40 #include "dbwrap/dbwrap_open.h"
41 #include "../libcli/security/dom_sid.h"
43 #include "idmap_tdb_common.h"
46 #define DBGC_CLASS DBGC_IDMAP
48 struct idmap_tdb2_context
{
49 const char *script
; /* script to provide idmaps */
52 /* High water mark keys */
53 #define HWM_GROUP "GROUP HWM"
54 #define HWM_USER "USER HWM"
57 * check and initialize high/low water marks in the db
59 static NTSTATUS
idmap_tdb2_init_hwm(struct idmap_domain
*dom
)
63 struct idmap_tdb_common_context
*ctx
;
65 ctx
= talloc_get_type(dom
->private_data
,
66 struct idmap_tdb_common_context
);
68 /* Create high water marks for group and user id */
70 status
= dbwrap_fetch_uint32_bystring(ctx
->db
, HWM_USER
, &low_id
);
71 if (!NT_STATUS_IS_OK(status
) || (low_id
< dom
->low_id
)) {
72 status
= dbwrap_trans_store_uint32_bystring(ctx
->db
, HWM_USER
,
74 if (!NT_STATUS_IS_OK(status
)) {
75 DEBUG(0, ("Unable to initialise user hwm in idmap "
76 "database: %s\n", nt_errstr(status
)));
77 return NT_STATUS_INTERNAL_DB_ERROR
;
81 status
= dbwrap_fetch_uint32_bystring(ctx
->db
, HWM_GROUP
, &low_id
);
82 if (!NT_STATUS_IS_OK(status
) || (low_id
< dom
->low_id
)) {
83 status
= dbwrap_trans_store_uint32_bystring(ctx
->db
, HWM_GROUP
,
85 if (!NT_STATUS_IS_OK(status
)) {
86 DEBUG(0, ("Unable to initialise group hwm in idmap "
87 "database: %s\n", nt_errstr(status
)));
88 return NT_STATUS_INTERNAL_DB_ERROR
;
97 open the permanent tdb
99 static NTSTATUS
idmap_tdb2_open_db(struct idmap_domain
*dom
)
102 struct idmap_tdb_common_context
*ctx
;
104 ctx
= talloc_get_type(dom
->private_data
,
105 struct idmap_tdb_common_context
);
108 /* its already open */
112 db_path
= talloc_asprintf(NULL
, "%s/idmap2.tdb", lp_private_dir());
113 NT_STATUS_HAVE_NO_MEMORY(db_path
);
115 /* Open idmap repository */
116 ctx
->db
= db_open(ctx
, db_path
, 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0644,
117 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
118 if (ctx
->db
== NULL
) {
119 DEBUG(0, ("Unable to open idmap_tdb2 database '%s'\n",
121 TALLOC_FREE(db_path
);
122 return NT_STATUS_UNSUCCESSFUL
;
124 TALLOC_FREE(db_path
);
126 return idmap_tdb2_init_hwm(dom
);
130 * store a mapping in the database.
133 struct idmap_tdb2_set_mapping_context
{
138 static NTSTATUS
idmap_tdb2_set_mapping_action(struct db_context
*db
,
143 struct idmap_tdb2_set_mapping_context
*state
;
144 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
146 state
= (struct idmap_tdb2_set_mapping_context
*)private_data
;
148 DEBUG(10, ("Storing %s <-> %s map\n", state
->ksidstr
, state
->kidstr
));
150 /* check wheter sid mapping is already present in db */
151 ret
= dbwrap_fetch_bystring(db
, tmp_ctx
, state
->ksidstr
, &data
);
152 if (NT_STATUS_IS_OK(ret
)) {
153 ret
= NT_STATUS_OBJECT_NAME_COLLISION
;
157 ret
= dbwrap_store_bystring(db
, state
->ksidstr
,
158 string_term_tdb_data(state
->kidstr
),
160 if (!NT_STATUS_IS_OK(ret
)) {
161 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret
)));
165 ret
= dbwrap_store_bystring(db
, state
->kidstr
,
166 string_term_tdb_data(state
->ksidstr
),
168 if (!NT_STATUS_IS_OK(ret
)) {
169 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret
)));
170 /* try to remove the previous stored SID -> ID map */
171 dbwrap_delete_bystring(db
, state
->ksidstr
);
175 DEBUG(10,("Stored %s <-> %s\n", state
->ksidstr
, state
->kidstr
));
178 talloc_free(tmp_ctx
);
182 static NTSTATUS
idmap_tdb2_set_mapping(struct idmap_domain
*dom
, const struct id_map
*map
)
184 struct idmap_tdb2_context
*ctx
;
187 struct dom_sid_buf sid_str
;
188 struct idmap_tdb_common_context
*commonctx
;
189 struct idmap_tdb2_set_mapping_context state
;
191 if (!map
|| !map
->sid
) {
192 return NT_STATUS_INVALID_PARAMETER
;
197 /* TODO: should we filter a set_mapping using low/high filters ? */
199 commonctx
= talloc_get_type(dom
->private_data
,
200 struct idmap_tdb_common_context
);
202 ctx
= talloc_get_type(commonctx
->private_data
,
203 struct idmap_tdb2_context
);
205 switch (map
->xid
.type
) {
208 kidstr
= talloc_asprintf(ctx
, "UID %lu", (unsigned long)map
->xid
.id
);
212 kidstr
= talloc_asprintf(ctx
, "GID %lu", (unsigned long)map
->xid
.id
);
216 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map
->xid
.type
));
217 return NT_STATUS_INVALID_PARAMETER
;
220 if (kidstr
== NULL
) {
221 DEBUG(0, ("ERROR: Out of memory!\n"));
222 ret
= NT_STATUS_NO_MEMORY
;
226 state
.ksidstr
= dom_sid_str_buf(map
->sid
, &sid_str
);
227 state
.kidstr
= kidstr
;
229 ret
= dbwrap_trans_do(commonctx
->db
, idmap_tdb2_set_mapping_action
,
238 run a script to perform a mapping
240 The script should the following command lines:
246 and should return one of the following as a single line of text
252 static NTSTATUS
idmap_tdb2_script(struct idmap_tdb2_context
*ctx
,
253 struct id_map
*map
, const char *fmt
, ...)
254 PRINTF_ATTRIBUTE(3,4);
256 static NTSTATUS
idmap_tdb2_script(struct idmap_tdb2_context
*ctx
, struct id_map
*map
,
257 const char *fmt
, ...)
265 cmd
= talloc_asprintf(ctx
, "%s ", ctx
->script
);
266 NT_STATUS_HAVE_NO_MEMORY(cmd
);
269 cmd
= talloc_vasprintf_append(cmd
, fmt
, ap
);
271 NT_STATUS_HAVE_NO_MEMORY(cmd
);
276 return NT_STATUS_NONE_MAPPED
;
279 if (fgets(line
, sizeof(line
)-1, p
) == NULL
) {
281 return NT_STATUS_NONE_MAPPED
;
285 DEBUG(10,("idmap script gave: %s\n", line
));
287 if (sscanf(line
, "UID:%lu", &v
) == 1) {
289 map
->xid
.type
= ID_TYPE_UID
;
290 } else if (sscanf(line
, "GID:%lu", &v
) == 1) {
292 map
->xid
.type
= ID_TYPE_GID
;
293 } else if (strncmp(line
, "SID:S-", 6) == 0) {
294 if (!string_to_sid(map
->sid
, &line
[4])) {
295 DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
297 return NT_STATUS_NONE_MAPPED
;
300 DEBUG(0,("Bad reply '%s' from idmap script %s\n",
302 return NT_STATUS_NONE_MAPPED
;
311 Single id to sid lookup function.
313 static NTSTATUS
idmap_tdb2_id_to_sid(struct idmap_domain
*dom
, struct id_map
*map
)
319 struct idmap_tdb_common_context
*commonctx
;
320 struct idmap_tdb2_context
*ctx
;
324 return NT_STATUS_INVALID_PARAMETER
;
327 status
= idmap_tdb2_open_db(dom
);
328 NT_STATUS_NOT_OK_RETURN(status
);
330 commonctx
= talloc_get_type(dom
->private_data
,
331 struct idmap_tdb_common_context
);
333 ctx
= talloc_get_type(commonctx
->private_data
,
334 struct idmap_tdb2_context
);
336 /* apply filters before checking */
337 if (!idmap_unix_id_is_in_range(map
->xid
.id
, dom
)) {
338 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
339 map
->xid
.id
, dom
->low_id
, dom
->high_id
));
340 return NT_STATUS_NONE_MAPPED
;
343 switch (map
->xid
.type
) {
346 keystr
= talloc_asprintf(ctx
, "UID %lu", (unsigned long)map
->xid
.id
);
350 keystr
= talloc_asprintf(ctx
, "GID %lu", (unsigned long)map
->xid
.id
);
354 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map
->xid
.type
));
355 return NT_STATUS_INVALID_PARAMETER
;
358 if (keystr
== NULL
) {
359 DEBUG(0, ("Out of memory!\n"));
360 ret
= NT_STATUS_NO_MEMORY
;
364 DEBUG(10,("Fetching record %s\n", keystr
));
366 /* Check if the mapping exists */
367 status
= dbwrap_fetch_bystring(commonctx
->db
, keystr
, keystr
, &data
);
369 if (!NT_STATUS_IS_OK(status
)) {
370 struct dom_sid_buf sidstr
;
371 struct idmap_tdb2_set_mapping_context store_state
;
373 DEBUG(10,("Record %s not found\n", keystr
));
374 if (ctx
->script
== NULL
) {
375 ret
= NT_STATUS_NONE_MAPPED
;
379 ret
= idmap_tdb2_script(ctx
, map
, "IDTOSID %s", keystr
);
380 if (!NT_STATUS_IS_OK(ret
)) {
384 store_state
.ksidstr
= dom_sid_str_buf(map
->sid
, &sidstr
);
385 store_state
.kidstr
= keystr
;
387 ret
= dbwrap_trans_do(commonctx
->db
,
388 idmap_tdb2_set_mapping_action
,
393 if (!string_to_sid(map
->sid
, (const char *)data
.dptr
)) {
394 DEBUG(10,("INVALID SID (%s) in record %s\n",
395 (const char *)data
.dptr
, keystr
));
396 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
400 DEBUG(10,("Found record %s -> %s\n", keystr
, (const char *)data
.dptr
));
410 Single sid to id lookup function.
412 static NTSTATUS
idmap_tdb2_sid_to_id(struct idmap_domain
*dom
, struct id_map
*map
)
416 struct dom_sid_buf keystr
;
417 unsigned long rec_id
= 0;
418 struct idmap_tdb_common_context
*commonctx
;
419 struct idmap_tdb2_context
*ctx
;
420 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
422 ret
= idmap_tdb2_open_db(dom
);
423 NT_STATUS_NOT_OK_RETURN(ret
);
425 commonctx
= talloc_get_type(dom
->private_data
,
426 struct idmap_tdb_common_context
);
428 ctx
= talloc_get_type(commonctx
->private_data
,
429 struct idmap_tdb2_context
);
431 dom_sid_str_buf(map
->sid
, &keystr
);
433 DEBUG(10, ("Fetching record %s\n", keystr
.buf
));
435 /* Check if sid is present in database */
436 ret
= dbwrap_fetch_bystring(commonctx
->db
, tmp_ctx
, keystr
.buf
, &data
);
437 if (!NT_STATUS_IS_OK(ret
)) {
439 struct idmap_tdb2_set_mapping_context store_state
;
441 DBG_DEBUG("Record %s not found\n", keystr
.buf
);
443 if (ctx
->script
== NULL
) {
444 ret
= NT_STATUS_NONE_MAPPED
;
448 ret
= idmap_tdb2_script(ctx
, map
, "SIDTOID %s", keystr
.buf
);
449 if (!NT_STATUS_IS_OK(ret
)) {
453 /* apply filters before returning result */
454 if (!idmap_unix_id_is_in_range(map
->xid
.id
, dom
)) {
455 DEBUG(5, ("Script returned id (%u) out of range "
456 "(%u - %u). Filtered!\n",
457 map
->xid
.id
, dom
->low_id
, dom
->high_id
));
458 ret
= NT_STATUS_NONE_MAPPED
;
462 idstr
= talloc_asprintf(tmp_ctx
, "%cID %lu",
463 map
->xid
.type
== ID_TYPE_UID
?'U':'G',
464 (unsigned long)map
->xid
.id
);
466 ret
= NT_STATUS_NO_MEMORY
;
470 store_state
.ksidstr
= keystr
.buf
;
471 store_state
.kidstr
= idstr
;
473 ret
= dbwrap_trans_do(commonctx
->db
,
474 idmap_tdb2_set_mapping_action
,
479 /* What type of record is this ? */
480 if (sscanf((const char *)data
.dptr
, "UID %lu", &rec_id
) == 1) { /* Try a UID record. */
481 map
->xid
.id
= rec_id
;
482 map
->xid
.type
= ID_TYPE_UID
;
483 DBG_DEBUG("Found uid record %s -> %s \n",
485 (const char *)data
.dptr
);
488 } else if (sscanf((const char *)data
.dptr
, "GID %lu", &rec_id
) == 1) { /* Try a GID record. */
489 map
->xid
.id
= rec_id
;
490 map
->xid
.type
= ID_TYPE_GID
;
491 DBG_DEBUG("Found gid record %s -> %s \n",
493 (const char *)data
.dptr
);
496 } else { /* Unknown record type ! */
497 DBG_WARNING("Found INVALID record %s -> %s\n",
499 (const char *)data
.dptr
);
500 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
504 /* apply filters before returning result */
505 if (!idmap_unix_id_is_in_range(map
->xid
.id
, dom
)) {
506 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
507 map
->xid
.id
, dom
->low_id
, dom
->high_id
));
508 ret
= NT_STATUS_NONE_MAPPED
;
512 talloc_free(tmp_ctx
);
517 Initialise idmap database.
519 static NTSTATUS
idmap_tdb2_db_init(struct idmap_domain
*dom
)
522 struct idmap_tdb_common_context
*commonctx
;
523 struct idmap_tdb2_context
*ctx
;
524 const char * idmap_script
= NULL
;
525 const char *ctx_script
= NULL
;
527 commonctx
= talloc_zero(dom
, struct idmap_tdb_common_context
);
529 DEBUG(0, ("Out of memory!\n"));
530 return NT_STATUS_NO_MEMORY
;
533 commonctx
->rw_ops
= talloc_zero(commonctx
, struct idmap_rw_ops
);
534 if (commonctx
->rw_ops
== NULL
) {
535 DEBUG(0, ("Out of memory!\n"));
536 ret
= NT_STATUS_NO_MEMORY
;
540 ctx
= talloc_zero(commonctx
, struct idmap_tdb2_context
);
542 DEBUG(0, ("Out of memory!\n"));
543 ret
= NT_STATUS_NO_MEMORY
;
547 ctx_script
= idmap_config_const_string(dom
->name
, "script", NULL
);
549 idmap_script
= lp_parm_const_string(-1, "idmap", "script", NULL
);
550 if (idmap_script
!= NULL
) {
551 DEBUG(0, ("Warning: 'idmap:script' is deprecated. "
552 " Please use 'idmap config * : script' instead!\n"));
555 if (strequal(dom
->name
, "*") && ctx_script
== NULL
) {
556 /* fall back to idmap:script for backwards compatibility */
557 ctx_script
= idmap_script
;
561 DEBUG(1, ("using idmap script '%s'\n", ctx_script
));
563 * We must ensure this memory is owned by ctx.
564 * The ctx_script const pointer is a pointer into
565 * the config file data and may become invalid
566 * on config file reload. BUG: 13956
568 ctx
->script
= talloc_strdup(ctx
, ctx_script
);
569 if (ctx
->script
== NULL
) {
570 ret
= NT_STATUS_NO_MEMORY
;
575 commonctx
->max_id
= dom
->high_id
;
576 commonctx
->hwmkey_uid
= HWM_USER
;
577 commonctx
->hwmkey_gid
= HWM_GROUP
;
579 commonctx
->sid_to_unixid_fn
= idmap_tdb2_sid_to_id
;
580 commonctx
->unixid_to_sid_fn
= idmap_tdb2_id_to_sid
;
582 commonctx
->rw_ops
->get_new_id
= idmap_tdb_common_get_new_id
;
583 commonctx
->rw_ops
->set_mapping
= idmap_tdb2_set_mapping
;
585 commonctx
->private_data
= ctx
;
586 dom
->private_data
= commonctx
;
588 ret
= idmap_tdb2_open_db(dom
);
589 if (!NT_STATUS_IS_OK(ret
)) {
596 talloc_free(commonctx
);
601 static struct idmap_methods db_methods
= {
602 .init
= idmap_tdb2_db_init
,
603 .unixids_to_sids
= idmap_tdb_common_unixids_to_sids
,
604 .sids_to_unixids
= idmap_tdb_common_sids_to_unixids
,
605 .allocate_id
= idmap_tdb_common_get_new_id
609 NTSTATUS
idmap_tdb2_init(TALLOC_CTX
*ctx
)
611 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
, "tdb2", &db_methods
);