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
;
66 struct dom_sid_buf keystr
;
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 key2
= string_term_tdb_data(dom_sid_str_buf(&sid
, &keystr
));
109 value
= dbwrap_record_get_value(rec
);
111 status
= dbwrap_store(s
->db
, key2
, value
, TDB_INSERT
);
112 if (!NT_STATUS_IS_OK(status
)) {
113 DEBUG(0,("Unable to add record %s:%s\n",
114 (const char *)key2
.dptr
,
120 status
= dbwrap_store(s
->db
, value
, key2
, TDB_REPLACE
);
121 if (!NT_STATUS_IS_OK(status
)) {
122 DEBUG(0,("Unable to update record %s:%s\n",
123 (const char *)value
.dptr
,
129 status
= dbwrap_record_delete(rec
);
130 if (!NT_STATUS_IS_OK(status
)) {
131 DEBUG(0,("Unable to delete record %s:%s\n",
132 (const char *)key
.dptr
,
141 /*****************************************************************************
142 Convert the idmap database from an older version.
143 *****************************************************************************/
145 static bool idmap_tdb_upgrade(struct idmap_domain
*dom
, struct db_context
*db
)
148 struct convert_fn_state s
;
151 status
= dbwrap_fetch_int32_bystring(db
, "IDMAP_VERSION", &vers
);
152 if (!NT_STATUS_IS_OK(status
)) {
156 if (IREV(vers
) == IDMAP_VERSION
) {
157 /* Arrggghh ! Bytereversed - make order independent ! */
159 * high and low records were created on a
160 * big endian machine and will need byte-reversing.
165 status
= dbwrap_fetch_int32_bystring(db
, HWM_USER
, &wm
);
166 if (!NT_STATUS_IS_OK(status
)) {
176 status
= dbwrap_store_int32_bystring(db
, HWM_USER
, wm
);
177 if (!NT_STATUS_IS_OK(status
)) {
178 DEBUG(0, ("Unable to byteswap user hwm in idmap "
179 "database: %s\n", nt_errstr(status
)));
183 status
= dbwrap_fetch_int32_bystring(db
, HWM_GROUP
, &wm
);
184 if (!NT_STATUS_IS_OK(status
)) {
194 status
= dbwrap_store_int32_bystring(db
, HWM_GROUP
, wm
);
195 if (!NT_STATUS_IS_OK(status
)) {
196 DEBUG(0, ("Unable to byteswap group hwm in idmap "
197 "database: %s\n", nt_errstr(status
)));
205 /* the old format stored as DOMAIN/rid - now we store the SID direct */
206 status
= dbwrap_traverse(db
, convert_fn
, &s
, NULL
);
208 if (!NT_STATUS_IS_OK(status
)) {
209 DEBUG(0, ("Database traverse failed during conversion\n"));
214 DEBUG(0, ("Problem during conversion\n"));
218 status
= dbwrap_store_int32_bystring(db
, "IDMAP_VERSION",
220 if (!NT_STATUS_IS_OK(status
)) {
221 DEBUG(0, ("Unable to store idmap version in database: %s\n",
229 static NTSTATUS
idmap_tdb_init_hwm(struct idmap_domain
*dom
)
233 bool update_uid
= false;
234 bool update_gid
= false;
235 struct idmap_tdb_common_context
*ctx
;
238 ctx
= talloc_get_type(dom
->private_data
,
239 struct idmap_tdb_common_context
);
241 status
= dbwrap_fetch_uint32_bystring(ctx
->db
, HWM_USER
, &low_uid
);
242 if (!NT_STATUS_IS_OK(status
) || low_uid
< dom
->low_id
) {
246 status
= dbwrap_fetch_uint32_bystring(ctx
->db
, HWM_GROUP
, &low_gid
);
247 if (!NT_STATUS_IS_OK(status
) || low_gid
< dom
->low_id
) {
251 if (!update_uid
&& !update_gid
) {
255 if (dbwrap_transaction_start(ctx
->db
) != 0) {
256 DEBUG(0, ("Unable to start upgrade transaction!\n"));
257 return NT_STATUS_INTERNAL_DB_ERROR
;
261 status
= dbwrap_store_uint32_bystring(ctx
->db
, HWM_USER
,
263 if (!NT_STATUS_IS_OK(status
)) {
264 dbwrap_transaction_cancel(ctx
->db
);
265 DEBUG(0, ("Unable to initialise user hwm in idmap "
266 "database: %s\n", nt_errstr(status
)));
267 return NT_STATUS_INTERNAL_DB_ERROR
;
272 status
= dbwrap_store_uint32_bystring(ctx
->db
, HWM_GROUP
,
274 if (!NT_STATUS_IS_OK(status
)) {
275 dbwrap_transaction_cancel(ctx
->db
);
276 DEBUG(0, ("Unable to initialise group hwm in idmap "
277 "database: %s\n", nt_errstr(status
)));
278 return NT_STATUS_INTERNAL_DB_ERROR
;
282 if (dbwrap_transaction_commit(ctx
->db
) != 0) {
283 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
284 return NT_STATUS_INTERNAL_DB_ERROR
;
290 static NTSTATUS
idmap_tdb_open_db(struct idmap_domain
*dom
)
294 char *tdbfile
= NULL
;
295 struct db_context
*db
= NULL
;
297 bool config_error
= false;
298 struct idmap_tdb_common_context
*ctx
;
300 ctx
= talloc_get_type(dom
->private_data
,
301 struct idmap_tdb_common_context
);
304 /* it is already open */
308 /* use our own context here */
309 mem_ctx
= talloc_stackframe();
311 /* use the old database if present */
312 tdbfile
= state_path(talloc_tos(), "winbindd_idmap.tdb");
314 DEBUG(0, ("Out of memory!\n"));
315 ret
= NT_STATUS_NO_MEMORY
;
319 DEBUG(10,("Opening tdbfile %s\n", tdbfile
));
321 /* Open idmap repository */
322 db
= db_open(mem_ctx
, tdbfile
, 0, TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0644,
323 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
325 DEBUG(0, ("Unable to open idmap database\n"));
326 ret
= NT_STATUS_UNSUCCESSFUL
;
330 /* check against earlier versions */
331 ret
= dbwrap_fetch_int32_bystring(db
, "IDMAP_VERSION", &version
);
332 if (!NT_STATUS_IS_OK(ret
)) {
336 if (version
!= IDMAP_VERSION
) {
338 DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
339 "possible with incomplete configuration\n",
340 version
, IDMAP_VERSION
));
341 ret
= NT_STATUS_UNSUCCESSFUL
;
344 if (dbwrap_transaction_start(db
) != 0) {
345 DEBUG(0, ("Unable to start upgrade transaction!\n"));
346 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
350 if (!idmap_tdb_upgrade(dom
, db
)) {
351 dbwrap_transaction_cancel(db
);
352 DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
353 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
357 if (dbwrap_transaction_commit(db
) != 0) {
358 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
359 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
364 ctx
->db
= talloc_move(ctx
, &db
);
366 ret
= idmap_tdb_init_hwm(dom
);
369 talloc_free(mem_ctx
);
373 /**********************************************************************
374 IDMAP MAPPING TDB BACKEND
375 **********************************************************************/
377 /*****************************
378 Initialise idmap database.
379 *****************************/
381 static NTSTATUS
idmap_tdb_db_init(struct idmap_domain
*dom
)
384 struct idmap_tdb_common_context
*ctx
;
386 DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom
->name
));
388 ctx
= talloc_zero(dom
, struct idmap_tdb_common_context
);
390 DEBUG(0, ("Out of memory!\n"));
391 return NT_STATUS_NO_MEMORY
;
394 /* load backend specific configuration here: */
396 if (strequal(dom
->name
, "*")) {
401 ctx
->rw_ops
= talloc_zero(ctx
, struct idmap_rw_ops
);
402 if (ctx
->rw_ops
== NULL
) {
403 DEBUG(0, ("Out of memory!\n"));
404 ret
= NT_STATUS_NO_MEMORY
;
408 ctx
->max_id
= dom
->high_id
;
409 ctx
->hwmkey_uid
= HWM_USER
;
410 ctx
->hwmkey_gid
= HWM_GROUP
;
412 ctx
->rw_ops
->get_new_id
= idmap_tdb_common_get_new_id
;
413 ctx
->rw_ops
->set_mapping
= idmap_tdb_common_set_mapping
;
415 dom
->private_data
= ctx
;
417 ret
= idmap_tdb_open_db(dom
);
418 if ( ! NT_STATUS_IS_OK(ret
)) {
429 static struct idmap_methods db_methods
= {
430 .init
= idmap_tdb_db_init
,
431 .unixids_to_sids
= idmap_tdb_common_unixids_to_sids
,
432 .sids_to_unixids
= idmap_tdb_common_sids_to_unixids
,
433 .allocate_id
= idmap_tdb_common_get_new_id
,
436 NTSTATUS
idmap_tdb_init(TALLOC_CTX
*mem_ctx
)
438 DEBUG(10, ("calling idmap_tdb_init\n"));
440 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
, "tdb", &db_methods
);