1 /*
2    Unix SMB/CIFS implementation.
3 
4    idmap TDB backend
5 
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
11 
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.
16 
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.
21 
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/>.
24 */
25 
26 #include "includes.h"
27 #include "system/filesys.h"
28 #include "winbindd.h"
29 #include "idmap.h"
30 #include "idmap_rw.h"
31 #include "idmap_tdb_common.h"
32 #include "dbwrap/dbwrap.h"
33 #include "dbwrap/dbwrap_open.h"
34 #include "../libcli/security/security.h"
35 #include "util_tdb.h"
36 
37 #undef DBGC_CLASS
38 #define DBGC_CLASS DBGC_IDMAP
39 
40 /* idmap version determines auto-conversion - this is the database
41    structure version specifier. */
42 
43 #define IDMAP_VERSION 2
44 
45 /* High water mark keys */
46 #define HWM_GROUP  "GROUP HWM"
47 #define HWM_USER   "USER HWM"
48 
49 struct convert_fn_state {
50 	struct db_context *db;
51 	bool failed;
52 };
53 
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
57  instead of the SID.
58 *****************************************************************************/
convert_fn(struct db_record * rec,void * private_data)59 static int convert_fn(struct db_record *rec, void *private_data)
60 {
61 	struct winbindd_domain *domain;
62 	char *p;
63 	NTSTATUS status;
64 	struct dom_sid sid;
65 	uint32_t rid;
66 	struct dom_sid_buf keystr;
67 	fstring dom_name;
68 	TDB_DATA key;
69 	TDB_DATA key2;
70 	TDB_DATA value;
71 	struct convert_fn_state *s = (struct convert_fn_state *)private_data;
72 
73 	key = dbwrap_record_get_key(rec);
74 
75 	DEBUG(10,("Converting %s\n", (const char *)key.dptr));
76 
77 	p = strchr((const char *)key.dptr, '/');
78 	if (!p)
79 		return 0;
80 
81 	*p = 0;
82 	fstrcpy(dom_name, (const char *)key.dptr);
83 	*p++ = '/';
84 
85 	domain = find_domain_from_name(dom_name);
86 	if (domain == NULL) {
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 ));
90 
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,
95 				nt_errstr(status)));
96 			s->failed = true;
97 			return -1;
98 		}
99 
100 		return 0;
101 	}
102 
103 	rid = atoi(p);
104 
105 	sid_compose(&sid, &domain->sid, rid);
106 
107 	key2 = string_term_tdb_data(dom_sid_str_buf(&sid, &keystr));
108 
109 	value = dbwrap_record_get_value(rec);
110 
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,
115 			nt_errstr(status)));
116 		s->failed = true;
117 		return -1;
118 	}
119 
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,
124 			nt_errstr(status)));
125 		s->failed = true;
126 		return -1;
127 	}
128 
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,
133 			nt_errstr(status)));
134 		s->failed = true;
135 		return -1;
136 	}
137 
138 	return 0;
139 }
140 
141 /*****************************************************************************
142  Convert the idmap database from an older version.
143 *****************************************************************************/
144 
idmap_tdb_upgrade(struct idmap_domain * dom,struct db_context * db)145 static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db)
146 {
147 	int32_t vers;
148 	struct convert_fn_state s;
149 	NTSTATUS status;
150 
151 	status = dbwrap_fetch_int32_bystring(db, "IDMAP_VERSION", &vers);
152 	if (!NT_STATUS_IS_OK(status)) {
153 		vers = -1;
154 	}
155 
156 	if (IREV(vers) == IDMAP_VERSION) {
157 		/* Arrggghh ! Bytereversed - make order independent ! */
158 		/*
159 		 * high and low records were created on a
160 		 * big endian machine and will need byte-reversing.
161 		 */
162 
163 		int32_t wm;
164 
165 		status = dbwrap_fetch_int32_bystring(db, HWM_USER, &wm);
166 		if (!NT_STATUS_IS_OK(status)) {
167 			wm = -1;
168 		}
169 
170 		if (wm != -1) {
171 			wm = IREV(wm);
172 		}  else {
173 			wm = dom->low_id;
174 		}
175 
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)));
180 			return False;
181 		}
182 
183 		status = dbwrap_fetch_int32_bystring(db, HWM_GROUP, &wm);
184 		if (!NT_STATUS_IS_OK(status)) {
185 			wm = -1;
186 		}
187 
188 		if (wm != -1) {
189 			wm = IREV(wm);
190 		} else {
191 			wm = dom->low_id;
192 		}
193 
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)));
198 			return False;
199 		}
200 	}
201 
202 	s.db = db;
203 	s.failed = false;
204 
205 	/* the old format stored as DOMAIN/rid - now we store the SID direct */
206 	status = dbwrap_traverse(db, convert_fn, &s, NULL);
207 
208 	if (!NT_STATUS_IS_OK(status)) {
209 		DEBUG(0, ("Database traverse failed during conversion\n"));
210 		return false;
211 	}
212 
213 	if (s.failed) {
214 		DEBUG(0, ("Problem during conversion\n"));
215 		return False;
216 	}
217 
218 	status = dbwrap_store_int32_bystring(db, "IDMAP_VERSION",
219 					     IDMAP_VERSION);
220 	if (!NT_STATUS_IS_OK(status)) {
221 		DEBUG(0, ("Unable to store idmap version in database: %s\n",
222 			  nt_errstr(status)));
223 		return False;
224 	}
225 
226 	return True;
227 }
228 
idmap_tdb_init_hwm(struct idmap_domain * dom)229 static NTSTATUS idmap_tdb_init_hwm(struct idmap_domain *dom)
230 {
231 	uint32_t low_uid;
232 	uint32_t low_gid;
233 	bool update_uid = false;
234 	bool update_gid = false;
235 	struct idmap_tdb_common_context *ctx;
236 	NTSTATUS status;
237 
238 	ctx = talloc_get_type(dom->private_data,
239 			      struct idmap_tdb_common_context);
240 
241 	status = dbwrap_fetch_uint32_bystring(ctx->db, HWM_USER, &low_uid);
242 	if (!NT_STATUS_IS_OK(status) || low_uid < dom->low_id) {
243 		update_uid = true;
244 	}
245 
246 	status = dbwrap_fetch_uint32_bystring(ctx->db, HWM_GROUP, &low_gid);
247 	if (!NT_STATUS_IS_OK(status) || low_gid < dom->low_id) {
248 		update_gid = true;
249 	}
250 
251 	if (!update_uid && !update_gid) {
252 		return NT_STATUS_OK;
253 	}
254 
255 	if (dbwrap_transaction_start(ctx->db) != 0) {
256 		DEBUG(0, ("Unable to start upgrade transaction!\n"));
257 		return NT_STATUS_INTERNAL_DB_ERROR;
258 	}
259 
260 	if (update_uid) {
261 		status = dbwrap_store_uint32_bystring(ctx->db, HWM_USER,
262 						      dom->low_id);
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;
268 		}
269 	}
270 
271 	if (update_gid) {
272 		status = dbwrap_store_uint32_bystring(ctx->db, HWM_GROUP,
273 						      dom->low_id);
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;
279 		}
280 	}
281 
282 	if (dbwrap_transaction_commit(ctx->db) != 0) {
283 		DEBUG(0, ("Unable to commit upgrade transaction!\n"));
284 		return NT_STATUS_INTERNAL_DB_ERROR;
285 	}
286 
287 	return NT_STATUS_OK;
288 }
289 
idmap_tdb_open_db(struct idmap_domain * dom)290 static NTSTATUS idmap_tdb_open_db(struct idmap_domain *dom)
291 {
292 	NTSTATUS ret;
293 	TALLOC_CTX *mem_ctx;
294 	char *tdbfile = NULL;
295 	struct db_context *db = NULL;
296 	int32_t version;
297 	bool config_error = false;
298 	struct idmap_tdb_common_context *ctx;
299 
300 	ctx = talloc_get_type(dom->private_data,
301 			      struct idmap_tdb_common_context);
302 
303 	if (ctx->db) {
304 		/* it is already open */
305 		return NT_STATUS_OK;
306 	}
307 
308 	/* use our own context here */
309 	mem_ctx = talloc_stackframe();
310 
311 	/* use the old database if present */
312 	tdbfile = state_path(talloc_tos(), "winbindd_idmap.tdb");
313 	if (!tdbfile) {
314 		DEBUG(0, ("Out of memory!\n"));
315 		ret = NT_STATUS_NO_MEMORY;
316 		goto done;
317 	}
318 
319 	DEBUG(10,("Opening tdbfile %s\n", tdbfile ));
320 
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);
324 	if (!db) {
325 		DEBUG(0, ("Unable to open idmap database\n"));
326 		ret = NT_STATUS_UNSUCCESSFUL;
327 		goto done;
328 	}
329 
330 	/* check against earlier versions */
331 	ret = dbwrap_fetch_int32_bystring(db, "IDMAP_VERSION", &version);
332 	if (!NT_STATUS_IS_OK(ret)) {
333 		version = -1;
334 	}
335 
336 	if (version != IDMAP_VERSION) {
337 		if (config_error) {
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;
342 			goto done;
343 		}
344 		if (dbwrap_transaction_start(db) != 0) {
345 			DEBUG(0, ("Unable to start upgrade transaction!\n"));
346 			ret = NT_STATUS_INTERNAL_DB_ERROR;
347 			goto done;
348 		}
349 
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;
354 			goto done;
355 		}
356 
357 		if (dbwrap_transaction_commit(db) != 0) {
358 			DEBUG(0, ("Unable to commit upgrade transaction!\n"));
359 			ret = NT_STATUS_INTERNAL_DB_ERROR;
360 			goto done;
361 		}
362 	}
363 
364 	ctx->db = talloc_move(ctx, &db);
365 
366 	ret = idmap_tdb_init_hwm(dom);
367 
368 done:
369 	talloc_free(mem_ctx);
370 	return ret;
371 }
372 
373 /**********************************************************************
374  IDMAP MAPPING TDB BACKEND
375 **********************************************************************/
376 
377 /*****************************
378  Initialise idmap database.
379 *****************************/
380 
idmap_tdb_db_init(struct idmap_domain * dom)381 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom)
382 {
383 	NTSTATUS ret;
384 	struct idmap_tdb_common_context *ctx;
385 
386 	DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom->name));
387 
388 	ctx = talloc_zero(dom, struct idmap_tdb_common_context);
389 	if ( ! ctx) {
390 		DEBUG(0, ("Out of memory!\n"));
391 		return NT_STATUS_NO_MEMORY;
392 	}
393 
394 	/* load backend specific configuration here: */
395 #if 0
396 	if (strequal(dom->name, "*")) {
397 	} else {
398 	}
399 #endif
400 
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;
405 		goto failed;
406 	}
407 
408 	ctx->max_id = dom->high_id;
409 	ctx->hwmkey_uid = HWM_USER;
410 	ctx->hwmkey_gid = HWM_GROUP;
411 
412 	ctx->rw_ops->get_new_id = idmap_tdb_common_get_new_id;
413 	ctx->rw_ops->set_mapping = idmap_tdb_common_set_mapping;
414 
415 	dom->private_data = ctx;
416 
417 	ret = idmap_tdb_open_db(dom);
418 	if ( ! NT_STATUS_IS_OK(ret)) {
419 		goto failed;
420 	}
421 
422 	return NT_STATUS_OK;
423 
424 failed:
425 	talloc_free(ctx);
426 	return ret;
427 }
428 
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,
434 };
435 
idmap_tdb_init(TALLOC_CTX * mem_ctx)436 NTSTATUS idmap_tdb_init(TALLOC_CTX *mem_ctx)
437 {
438 	DEBUG(10, ("calling idmap_tdb_init\n"));
439 
440 	return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);
441 }
442