2 Unix SMB/CIFS implementation.
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
8 Copyright (C) Jeremy Allison 2006
9 Copyright (C) Simo Sorce 2003-2006
10 Copyright (C) Michael Adam 2009-2010
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "system/filesys.h"
31 #include "idmap_tdb_common.h"
32 #include "dbwrap/dbwrap.h"
33 #include "dbwrap/dbwrap_open.h"
34 #include "../libcli/security/security.h"
38 #define DBGC_CLASS DBGC_IDMAP
40 /* idmap version determines auto-conversion - this is the database
41 structure version specifier. */
43 #define IDMAP_VERSION 2
45 /* High water mark keys */
46 #define HWM_GROUP "GROUP HWM"
47 #define HWM_USER "USER HWM"
49 struct convert_fn_state
{
50 struct db_context
*db
;
54 /*****************************************************************************
55 For idmap conversion: convert one record to new format
56 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
58 *****************************************************************************/
59 static int convert_fn(struct db_record
*rec
, void *private_data
)
61 struct winbindd_domain
*domain
;
71 struct convert_fn_state
*s
= (struct convert_fn_state
*)private_data
;
73 key
= dbwrap_record_get_key(rec
);
75 DEBUG(10,("Converting %s\n", (const char *)key
.dptr
));
77 p
= strchr((const char *)key
.dptr
, '/');
82 fstrcpy(dom_name
, (const char *)key
.dptr
);
85 domain
= find_domain_from_name(dom_name
);
87 /* We must delete the old record. */
88 DEBUG(0,("Unable to find domain %s\n", dom_name
));
89 DEBUG(0,("deleting record %s\n", (const char *)key
.dptr
));
91 status
= dbwrap_record_delete(rec
);
92 if (!NT_STATUS_IS_OK(status
)) {
93 DEBUG(0, ("Unable to delete record %s:%s\n",
94 (const char *)key
.dptr
,
105 sid_compose(&sid
, &domain
->sid
, rid
);
107 sid_to_fstring(keystr
, &sid
);
108 key2
= string_term_tdb_data(keystr
);
110 value
= dbwrap_record_get_value(rec
);
112 status
= dbwrap_store(s
->db
, key2
, value
, TDB_INSERT
);
113 if (!NT_STATUS_IS_OK(status
)) {
114 DEBUG(0,("Unable to add record %s:%s\n",
115 (const char *)key2
.dptr
,
121 status
= dbwrap_store(s
->db
, value
, key2
, TDB_REPLACE
);
122 if (!NT_STATUS_IS_OK(status
)) {
123 DEBUG(0,("Unable to update record %s:%s\n",
124 (const char *)value
.dptr
,
130 status
= dbwrap_record_delete(rec
);
131 if (!NT_STATUS_IS_OK(status
)) {
132 DEBUG(0,("Unable to delete record %s:%s\n",
133 (const char *)key
.dptr
,
142 /*****************************************************************************
143 Convert the idmap database from an older version.
144 *****************************************************************************/
146 static bool idmap_tdb_upgrade(struct idmap_domain
*dom
, struct db_context
*db
)
149 bool bigendianheader
;
150 struct convert_fn_state s
;
154 /* If we are bigendian, tdb is bigendian if NOT converted. */
157 unsigned char small
[2];
160 if (u
.small
[0] == 0x01)
161 bigendianheader
= !(dbwrap_get_flags(db
) & TDB_CONVERT
);
163 assert(u
.small
[0] == 0x02);
164 bigendianheader
= (dbwrap_get_flags(db
) & TDB_CONVERT
);
167 bigendianheader
= (dbwrap_get_flags(db
) & TDB_BIGENDIAN
) ? True
: False
;
169 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
171 status
= dbwrap_fetch_int32(db
, "IDMAP_VERSION", &vers
);
172 if (!NT_STATUS_IS_OK(status
)) {
176 if (((vers
== -1) && bigendianheader
) || (IREV(vers
) == IDMAP_VERSION
)) {
177 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
179 * high and low records were created on a
180 * big endian machine and will need byte-reversing.
185 status
= dbwrap_fetch_int32(db
, HWM_USER
, &wm
);
186 if (!NT_STATUS_IS_OK(status
)) {
196 status
= dbwrap_store_int32(db
, HWM_USER
, wm
);
197 if (!NT_STATUS_IS_OK(status
)) {
198 DEBUG(0, ("Unable to byteswap user hwm in idmap "
199 "database: %s\n", nt_errstr(status
)));
203 status
= dbwrap_fetch_int32(db
, HWM_GROUP
, &wm
);
204 if (!NT_STATUS_IS_OK(status
)) {
214 status
= dbwrap_store_int32(db
, HWM_GROUP
, wm
);
215 if (!NT_STATUS_IS_OK(status
)) {
216 DEBUG(0, ("Unable to byteswap group hwm in idmap "
217 "database: %s\n", nt_errstr(status
)));
225 /* the old format stored as DOMAIN/rid - now we store the SID direct */
226 status
= dbwrap_traverse(db
, convert_fn
, &s
, NULL
);
228 if (!NT_STATUS_IS_OK(status
)) {
229 DEBUG(0, ("Database traverse failed during conversion\n"));
234 DEBUG(0, ("Problem during conversion\n"));
238 status
= dbwrap_store_int32(db
, "IDMAP_VERSION", IDMAP_VERSION
);
239 if (!NT_STATUS_IS_OK(status
)) {
240 DEBUG(0, ("Unable to store idmap version in database: %s\n",
248 static NTSTATUS
idmap_tdb_init_hwm(struct idmap_domain
*dom
)
252 bool update_uid
= false;
253 bool update_gid
= false;
254 struct idmap_tdb_common_context
*ctx
;
257 ctx
= talloc_get_type(dom
->private_data
,
258 struct idmap_tdb_common_context
);
260 status
= dbwrap_fetch_uint32(ctx
->db
, HWM_USER
, &low_uid
);
261 if (!NT_STATUS_IS_OK(status
) || low_uid
< dom
->low_id
) {
265 status
= dbwrap_fetch_uint32(ctx
->db
, HWM_GROUP
, &low_gid
);
266 if (!NT_STATUS_IS_OK(status
) || low_gid
< dom
->low_id
) {
270 if (!update_uid
&& !update_gid
) {
274 if (dbwrap_transaction_start(ctx
->db
) != 0) {
275 DEBUG(0, ("Unable to start upgrade transaction!\n"));
276 return NT_STATUS_INTERNAL_DB_ERROR
;
280 status
= dbwrap_store_uint32(ctx
->db
, HWM_USER
, dom
->low_id
);
281 if (!NT_STATUS_IS_OK(status
)) {
282 dbwrap_transaction_cancel(ctx
->db
);
283 DEBUG(0, ("Unable to initialise user hwm in idmap "
284 "database: %s\n", nt_errstr(status
)));
285 return NT_STATUS_INTERNAL_DB_ERROR
;
290 status
= dbwrap_store_uint32(ctx
->db
, HWM_GROUP
, dom
->low_id
);
291 if (!NT_STATUS_IS_OK(status
)) {
292 dbwrap_transaction_cancel(ctx
->db
);
293 DEBUG(0, ("Unable to initialise group hwm in idmap "
294 "database: %s\n", nt_errstr(status
)));
295 return NT_STATUS_INTERNAL_DB_ERROR
;
299 if (dbwrap_transaction_commit(ctx
->db
) != 0) {
300 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
301 return NT_STATUS_INTERNAL_DB_ERROR
;
307 static NTSTATUS
idmap_tdb_open_db(struct idmap_domain
*dom
)
311 char *tdbfile
= NULL
;
312 struct db_context
*db
= NULL
;
314 bool config_error
= false;
315 struct idmap_tdb_common_context
*ctx
;
317 ctx
= talloc_get_type(dom
->private_data
,
318 struct idmap_tdb_common_context
);
321 /* it is already open */
325 /* use our own context here */
326 mem_ctx
= talloc_stackframe();
328 /* use the old database if present */
329 tdbfile
= state_path("winbindd_idmap.tdb");
331 DEBUG(0, ("Out of memory!\n"));
332 ret
= NT_STATUS_NO_MEMORY
;
336 DEBUG(10,("Opening tdbfile %s\n", tdbfile
));
338 /* Open idmap repository */
339 db
= db_open(mem_ctx
, tdbfile
, 0, TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0644,
340 DBWRAP_LOCK_ORDER_1
);
342 DEBUG(0, ("Unable to open idmap database\n"));
343 ret
= NT_STATUS_UNSUCCESSFUL
;
347 /* check against earlier versions */
348 ret
= dbwrap_fetch_int32(db
, "IDMAP_VERSION", &version
);
349 if (!NT_STATUS_IS_OK(ret
)) {
353 if (version
!= IDMAP_VERSION
) {
355 DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
356 "possible with incomplete configuration\n",
357 version
, IDMAP_VERSION
));
358 ret
= NT_STATUS_UNSUCCESSFUL
;
361 if (dbwrap_transaction_start(db
) != 0) {
362 DEBUG(0, ("Unable to start upgrade transaction!\n"));
363 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
367 if (!idmap_tdb_upgrade(dom
, db
)) {
368 dbwrap_transaction_cancel(db
);
369 DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
370 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
374 if (dbwrap_transaction_commit(db
) != 0) {
375 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
376 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
381 ctx
->db
= talloc_move(ctx
, &db
);
383 ret
= idmap_tdb_init_hwm(dom
);
386 talloc_free(mem_ctx
);
390 /**********************************************************************
391 IDMAP MAPPING TDB BACKEND
392 **********************************************************************/
394 /*****************************
395 Initialise idmap database.
396 *****************************/
398 static NTSTATUS
idmap_tdb_db_init(struct idmap_domain
*dom
)
401 struct idmap_tdb_common_context
*ctx
;
403 DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom
->name
));
405 ctx
= talloc_zero(dom
, struct idmap_tdb_common_context
);
407 DEBUG(0, ("Out of memory!\n"));
408 return NT_STATUS_NO_MEMORY
;
411 /* load backend specific configuration here: */
413 if (strequal(dom
->name
, "*")) {
418 ctx
->rw_ops
= talloc_zero(ctx
, struct idmap_rw_ops
);
419 if (ctx
->rw_ops
== NULL
) {
420 DEBUG(0, ("Out of memory!\n"));
421 ret
= NT_STATUS_NO_MEMORY
;
425 ctx
->max_id
= dom
->high_id
;
426 ctx
->hwmkey_uid
= HWM_USER
;
427 ctx
->hwmkey_gid
= HWM_GROUP
;
429 ctx
->rw_ops
->get_new_id
= idmap_tdb_common_get_new_id
;
430 ctx
->rw_ops
->set_mapping
= idmap_tdb_common_set_mapping
;
432 dom
->private_data
= ctx
;
434 ret
= idmap_tdb_open_db(dom
);
435 if ( ! NT_STATUS_IS_OK(ret
)) {
446 static struct idmap_methods db_methods
= {
447 .init
= idmap_tdb_db_init
,
448 .unixids_to_sids
= idmap_tdb_common_unixids_to_sids
,
449 .sids_to_unixids
= idmap_tdb_common_sids_to_unixids
,
450 .allocate_id
= idmap_tdb_common_get_new_id
,
453 NTSTATUS
idmap_tdb_init(void)
455 DEBUG(10, ("calling idmap_tdb_init\n"));
457 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
, "tdb", &db_methods
);