s3:smbd: change blocking.c to use fsp_fnum_dbg() for fsp->fnum logging.
[Samba/gebeck_regimport.git] / source3 / winbindd / idmap_tdb_common.c
blobca6f6d8e0829d3ddb3ba6531a6b328a157d0aade
1 /*
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
9 which are:
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.
33 #include "includes.h"
34 #include "idmap_tdb_common.h"
35 #include "dbwrap/dbwrap.h"
36 #include "util_tdb.h"
37 #include "idmap_rw.h"
38 #include "../libcli/security/dom_sid.h"
40 #undef DBGC_CLASS
41 #define DBGC_CLASS DBGC_IDMAP
43 struct idmap_tdb_common_allocate_id_context {
44 const char *hwmkey;
45 const char *hwmtype;
46 uint32_t high_hwm;
47 uint32_t hwm;
50 static NTSTATUS idmap_tdb_common_allocate_id_action(struct db_context *db,
51 void *private_data)
53 NTSTATUS ret;
54 struct idmap_tdb_common_allocate_id_context *state;
55 uint32_t hwm;
57 state = (struct idmap_tdb_common_allocate_id_context *)private_data;
59 ret = dbwrap_fetch_uint32(db, state->hwmkey, &hwm);
60 if (!NT_STATUS_IS_OK(ret)) {
61 ret = NT_STATUS_INTERNAL_DB_ERROR;
62 goto done;
65 /* check it is in the range */
66 if (hwm > state->high_hwm) {
67 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
68 state->hwmtype, (unsigned long)state->high_hwm));
69 ret = NT_STATUS_UNSUCCESSFUL;
70 goto done;
73 /* fetch a new id and increment it */
74 ret = dbwrap_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
75 if (!NT_STATUS_IS_OK(ret)) {
76 DEBUG(1, ("Fatal error while fetching a new %s value\n!",
77 state->hwmtype));
78 goto done;
81 /* recheck it is in the range */
82 if (hwm > state->high_hwm) {
83 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
84 state->hwmtype, (unsigned long)state->high_hwm));
85 ret = NT_STATUS_UNSUCCESSFUL;
86 goto done;
89 ret = NT_STATUS_OK;
90 state->hwm = hwm;
92 done:
93 return ret;
96 static NTSTATUS idmap_tdb_common_allocate_id(struct idmap_domain *dom,
97 struct unixid *xid)
99 const char *hwmkey;
100 const char *hwmtype;
101 uint32_t hwm = 0;
102 NTSTATUS status;
103 struct idmap_tdb_common_allocate_id_context state;
104 struct idmap_tdb_common_context *ctx;
106 ctx =
107 talloc_get_type_abort(dom->private_data,
108 struct idmap_tdb_common_context);
110 /* Get current high water mark */
111 switch (xid->type) {
113 case ID_TYPE_UID:
114 hwmkey = ctx->hwmkey_uid;
115 hwmtype = "UID";
116 break;
118 case ID_TYPE_GID:
119 hwmkey = ctx->hwmkey_gid;
120 hwmtype = "GID";
121 break;
123 default:
124 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
125 return NT_STATUS_INVALID_PARAMETER;
128 state.hwm = hwm;
129 state.high_hwm = ctx->max_id;
130 state.hwmtype = hwmtype;
131 state.hwmkey = hwmkey;
133 status = dbwrap_trans_do(ctx->db, idmap_tdb_common_allocate_id_action,
134 &state);
136 if (NT_STATUS_IS_OK(status)) {
137 xid->id = state.hwm;
138 DEBUG(10, ("New %s = %d\n", hwmtype, state.hwm));
139 } else {
140 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
143 return status;
147 * Allocate a new unix-ID.
148 * For now this is for the default idmap domain only.
149 * Should be extended later on.
151 NTSTATUS idmap_tdb_common_get_new_id(struct idmap_domain * dom,
152 struct unixid * id)
154 NTSTATUS ret;
156 if (!strequal(dom->name, "*")) {
157 DEBUG(3, ("idmap_tdb_common_get_new_id: "
158 "Refusing allocation of a new unixid for domain'%s'. "
159 "Currently only supported for the default "
160 "domain \"*\".\n", dom->name));
161 return NT_STATUS_NOT_IMPLEMENTED;
164 ret = idmap_tdb_common_allocate_id(dom, id);
166 return ret;
170 * store a mapping in the database.
173 struct idmap_tdb_common_set_mapping_context {
174 const char *ksidstr;
175 const char *kidstr;
178 static NTSTATUS idmap_tdb_common_set_mapping_action(struct db_context *db,
179 void *private_data)
181 TDB_DATA data;
182 NTSTATUS ret;
183 struct idmap_tdb_common_set_mapping_context *state;
184 TALLOC_CTX *tmp_ctx = talloc_stackframe();
186 state = (struct idmap_tdb_common_set_mapping_context *)private_data;
188 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
190 /* check whether sid mapping is already present in db */
191 ret = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr, &data);
192 if (NT_STATUS_IS_OK(ret)) {
193 ret = NT_STATUS_OBJECT_NAME_COLLISION;
194 goto done;
197 ret = dbwrap_store_bystring(db, state->ksidstr,
198 string_term_tdb_data(state->kidstr),
199 TDB_INSERT);
200 if (!NT_STATUS_IS_OK(ret)) {
201 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
202 goto done;
205 ret = dbwrap_store_bystring(db, state->kidstr,
206 string_term_tdb_data(state->ksidstr),
207 TDB_INSERT);
208 if (!NT_STATUS_IS_OK(ret)) {
209 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
210 /* try to remove the previous stored SID -> ID map */
211 dbwrap_delete_bystring(db, state->ksidstr);
212 goto done;
215 DEBUG(10, ("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
217 done:
218 talloc_free(tmp_ctx);
219 return ret;
222 NTSTATUS idmap_tdb_common_set_mapping(struct idmap_domain * dom,
223 const struct id_map * map)
225 struct idmap_tdb_common_context *ctx;
226 struct idmap_tdb_common_set_mapping_context state;
227 NTSTATUS ret;
228 char *ksidstr, *kidstr;
230 if (!map || !map->sid) {
231 return NT_STATUS_INVALID_PARAMETER;
234 ksidstr = kidstr = NULL;
236 /* TODO: should we filter a set_mapping using low/high filters ? */
238 ctx =
239 talloc_get_type_abort(dom->private_data,
240 struct idmap_tdb_common_context);
242 switch (map->xid.type) {
244 case ID_TYPE_UID:
245 kidstr =
246 talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
247 break;
249 case ID_TYPE_GID:
250 kidstr =
251 talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
252 break;
254 default:
255 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
256 return NT_STATUS_INVALID_PARAMETER;
259 if (kidstr == NULL) {
260 DEBUG(0, ("ERROR: Out of memory!\n"));
261 ret = NT_STATUS_NO_MEMORY;
262 goto done;
265 ksidstr = sid_string_talloc(ctx, map->sid);
266 if (ksidstr == NULL) {
267 DEBUG(0, ("Out of memory!\n"));
268 ret = NT_STATUS_NO_MEMORY;
269 goto done;
272 state.ksidstr = ksidstr;
273 state.kidstr = kidstr;
275 ret = dbwrap_trans_do(ctx->db, idmap_tdb_common_set_mapping_action,
276 &state);
278 done:
279 talloc_free(ksidstr);
280 talloc_free(kidstr);
281 return ret;
285 * Create a new mapping for an unmapped SID, also allocating a new ID.
286 * This should be run inside a transaction.
288 * TODO:
289 * Properly integrate this with multi domain idmap config:
290 * Currently, the allocator is default-config only.
292 NTSTATUS idmap_tdb_common_new_mapping(struct idmap_domain * dom,
293 struct id_map * map)
295 NTSTATUS ret;
296 struct idmap_tdb_common_context *ctx;
298 ctx =
299 talloc_get_type_abort(dom->private_data,
300 struct idmap_tdb_common_context);
302 ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
304 return ret;
308 lookup a set of unix ids
310 NTSTATUS idmap_tdb_common_unixids_to_sids(struct idmap_domain * dom,
311 struct id_map ** ids)
313 NTSTATUS ret;
314 int i, num_mapped = 0;
315 struct idmap_tdb_common_context *ctx;
317 NTSTATUS(*unixid_to_sid_fn) (struct idmap_domain * dom,
318 struct id_map * map);
319 ctx =
320 talloc_get_type_abort(dom->private_data,
321 struct idmap_tdb_common_context);
323 unixid_to_sid_fn =
324 (ctx->unixid_to_sid_fn ==
325 NULL) ? idmap_tdb_common_unixid_to_sid : ctx->unixid_to_sid_fn;
327 /* initialize the status to avoid surprise */
328 for (i = 0; ids[i]; i++) {
329 ids[i]->status = ID_UNKNOWN;
332 for (i = 0; ids[i]; i++) {
333 ret = unixid_to_sid_fn(dom, ids[i]);
334 if (!NT_STATUS_IS_OK(ret)) {
336 /* if it is just a failed mapping continue */
337 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
339 /* make sure it is marked as unmapped */
340 ids[i]->status = ID_UNMAPPED;
341 continue;
344 /* some fatal error occurred, return immediately */
345 goto done;
348 /* all ok, id is mapped */
349 ids[i]->status = ID_MAPPED;
350 num_mapped += 1;
353 ret = NT_STATUS_OK;
355 done:
357 if (NT_STATUS_IS_OK(ret)) {
358 if (i == 0 || num_mapped == 0) {
359 ret = NT_STATUS_NONE_MAPPED;
360 } else if (num_mapped < i) {
361 ret = STATUS_SOME_UNMAPPED;
362 } else {
363 ret = NT_STATUS_OK;
367 return ret;
371 default single id to sid lookup function
373 NTSTATUS idmap_tdb_common_unixid_to_sid(struct idmap_domain * dom,
374 struct id_map * map)
376 NTSTATUS ret;
377 TDB_DATA data;
378 char *keystr;
379 struct idmap_tdb_common_context *ctx;
381 if (!dom || !map) {
382 return NT_STATUS_INVALID_PARAMETER;
384 /* TODO: do we really make sure the database is open again? should have been
385 * done while initing the module
386 status = idmap_tdb2_open_db(dom);
387 NT_STATUS_NOT_OK_RETURN(status);
389 ctx =
390 talloc_get_type_abort(dom->private_data,
391 struct idmap_tdb_common_context);
393 /* apply filters before checking */
394 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
395 DEBUG(5,
396 ("Requested id (%u) out of range (%u - %u). Filtered!\n",
397 map->xid.id, dom->low_id, dom->high_id));
398 return NT_STATUS_NONE_MAPPED;
401 switch (map->xid.type) {
403 case ID_TYPE_UID:
404 keystr =
405 talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
406 break;
408 case ID_TYPE_GID:
409 keystr =
410 talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
411 break;
413 default:
414 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
415 return NT_STATUS_INVALID_PARAMETER;
418 if (keystr == NULL) {
419 DEBUG(0, ("Out of memory!\n"));
420 ret = NT_STATUS_NO_MEMORY;
421 goto done;
424 DEBUG(10, ("Fetching record %s\n", keystr));
426 /* Check if the mapping exists */
427 ret = dbwrap_fetch_bystring(ctx->db, keystr, keystr, &data);
429 if (!NT_STATUS_IS_OK(ret)) {
430 DEBUG(10, ("Record %s not found\n", keystr));
431 ret = NT_STATUS_NONE_MAPPED;
432 goto done;
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;
439 goto done;
442 DEBUG(10, ("Found record %s -> %s\n", keystr, (const char *)data.dptr));
443 ret = NT_STATUS_OK;
445 done:
446 talloc_free(keystr);
447 return ret;
450 /**********************************
451 Single sid to id lookup function.
452 **********************************/
454 NTSTATUS idmap_tdb_common_sid_to_unixid(struct idmap_domain * dom,
455 struct id_map * map)
457 NTSTATUS ret;
458 TDB_DATA data;
459 char *keystr;
460 unsigned long rec_id = 0;
461 struct idmap_tdb_common_context *ctx;
462 TALLOC_CTX *tmp_ctx = talloc_stackframe();
464 if (!dom || !map) {
465 return NT_STATUS_INVALID_PARAMETER;
468 ctx =
469 talloc_get_type_abort(dom->private_data,
470 struct idmap_tdb_common_context);
472 keystr = sid_string_talloc(tmp_ctx, map->sid);
473 if (keystr == NULL) {
474 DEBUG(0, ("Out of memory!\n"));
475 ret = NT_STATUS_NO_MEMORY;
476 goto done;
479 DEBUG(10, ("Fetching record %s\n", keystr));
481 /* Check if sid is present in database */
482 ret = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr, &data);
483 if (!NT_STATUS_IS_OK(ret)) {
484 DEBUG(10, ("Record %s not found\n", keystr));
485 ret = NT_STATUS_NONE_MAPPED;
486 goto done;
489 /* What type of record is this ? */
490 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) {
491 /* Try a UID record. */
492 map->xid.id = rec_id;
493 map->xid.type = ID_TYPE_UID;
494 DEBUG(10,
495 ("Found uid record %s -> %s \n", keystr,
496 (const char *)data.dptr));
497 ret = NT_STATUS_OK;
499 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) {
500 /* Try a GID record. */
501 map->xid.id = rec_id;
502 map->xid.type = ID_TYPE_GID;
503 DEBUG(10,
504 ("Found gid record %s -> %s \n", keystr,
505 (const char *)data.dptr));
506 ret = NT_STATUS_OK;
508 } else { /* Unknown record type ! */
509 DEBUG(2,
510 ("Found INVALID record %s -> %s\n", keystr,
511 (const char *)data.dptr));
512 ret = NT_STATUS_INTERNAL_DB_ERROR;
513 goto done;
516 /* apply filters before returning result */
517 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
518 DEBUG(5,
519 ("Requested id (%u) out of range (%u - %u). Filtered!\n",
520 map->xid.id, dom->low_id, dom->high_id));
521 ret = NT_STATUS_NONE_MAPPED;
524 done:
525 talloc_free(tmp_ctx);
526 return ret;
529 /**********************************
530 lookup a set of sids
531 **********************************/
533 struct idmap_tdb_common_sids_to_unixids_context {
534 struct idmap_domain *dom;
535 struct id_map **ids;
536 bool allocate_unmapped;
537 NTSTATUS(*sid_to_unixid_fn) (struct idmap_domain * dom,
538 struct id_map * map);
541 static NTSTATUS idmap_tdb_common_sids_to_unixids_action(struct db_context *db,
542 void *private_data)
544 struct idmap_tdb_common_sids_to_unixids_context *state;
545 int i, num_mapped = 0;
546 NTSTATUS ret = NT_STATUS_OK;
548 state = (struct idmap_tdb_common_sids_to_unixids_context *)private_data;
550 DEBUG(10, ("idmap_tdb_common_sids_to_unixids: "
551 " domain: [%s], allocate: %s\n",
552 state->dom->name, state->allocate_unmapped ? "yes" : "no"));
554 for (i = 0; state->ids[i]; i++) {
555 if ((state->ids[i]->status == ID_UNKNOWN) ||
556 /* retry if we could not map in previous run: */
557 (state->ids[i]->status == ID_UNMAPPED)) {
558 NTSTATUS ret2;
560 ret2 = state->sid_to_unixid_fn(state->dom,
561 state->ids[i]);
563 if (!NT_STATUS_IS_OK(ret2)) {
565 /* if it is just a failed mapping, continue */
566 if (NT_STATUS_EQUAL
567 (ret2, NT_STATUS_NONE_MAPPED)) {
569 /* make sure it is marked as unmapped */
570 state->ids[i]->status = ID_UNMAPPED;
571 ret = STATUS_SOME_UNMAPPED;
572 } else {
574 * some fatal error occurred,
575 * return immediately
577 ret = ret2;
578 goto done;
580 } else {
581 /* all ok, id is mapped */
582 state->ids[i]->status = ID_MAPPED;
586 if ((state->ids[i]->status == ID_MAPPED)) {
587 num_mapped += 1;
590 if ((state->ids[i]->status == ID_UNMAPPED) &&
591 state->allocate_unmapped) {
592 ret =
593 idmap_tdb_common_new_mapping(state->dom,
594 state->ids[i]);
595 if (!NT_STATUS_IS_OK(ret)) {
596 goto done;
598 num_mapped += 1;
602 done:
604 if (NT_STATUS_IS_OK(ret) ||
605 NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED)) {
606 if (i == 0 || num_mapped == 0) {
607 ret = NT_STATUS_NONE_MAPPED;
608 } else if (num_mapped < i) {
609 ret = STATUS_SOME_UNMAPPED;
610 } else {
611 ret = NT_STATUS_OK;
615 return ret;
618 NTSTATUS idmap_tdb_common_sids_to_unixids(struct idmap_domain * dom,
619 struct id_map ** ids)
621 NTSTATUS ret;
622 int i;
623 struct idmap_tdb_common_sids_to_unixids_context state;
624 struct idmap_tdb_common_context *ctx;
626 ctx =
627 talloc_get_type_abort(dom->private_data,
628 struct idmap_tdb_common_context);
630 /* initialize the status to avoid surprise */
631 for (i = 0; ids[i]; i++) {
632 ids[i]->status = ID_UNKNOWN;
635 state.dom = dom;
636 state.ids = ids;
637 state.allocate_unmapped = false;
638 state.sid_to_unixid_fn =
639 (ctx->sid_to_unixid_fn ==
640 NULL) ? idmap_tdb_common_sid_to_unixid : ctx->sid_to_unixid_fn;
642 ret = idmap_tdb_common_sids_to_unixids_action(ctx->db, &state);
644 if ( (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) ||
645 NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) &&
646 !dom->read_only) {
647 state.allocate_unmapped = true;
648 ret = dbwrap_trans_do(ctx->db,
649 idmap_tdb_common_sids_to_unixids_action,
650 &state);
653 return ret;