test3: Fix usage check for test_sharesec.sh
[Samba.git] / source3 / winbindd / idmap_tdb_common.c
blob34269e3fe56e18db8af444f00b193bc10e65e9c1
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 = private_data;
55 uint32_t hwm;
57 ret = dbwrap_fetch_uint32_bystring(db, state->hwmkey, &hwm);
58 if (!NT_STATUS_IS_OK(ret)) {
59 ret = NT_STATUS_INTERNAL_DB_ERROR;
60 goto done;
63 /* check it is in the range */
64 if (hwm > state->high_hwm) {
65 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
66 state->hwmtype, (unsigned long)state->high_hwm));
67 ret = NT_STATUS_UNSUCCESSFUL;
68 goto done;
71 /* fetch a new id and increment it */
72 ret = dbwrap_change_uint32_atomic_bystring(db, state->hwmkey, &hwm, 1);
73 if (!NT_STATUS_IS_OK(ret)) {
74 DEBUG(1, ("Fatal error while fetching a new %s value\n!",
75 state->hwmtype));
76 goto done;
79 /* recheck it is in the range */
80 if (hwm > state->high_hwm) {
81 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
82 state->hwmtype, (unsigned long)state->high_hwm));
83 ret = NT_STATUS_UNSUCCESSFUL;
84 goto done;
87 ret = NT_STATUS_OK;
88 state->hwm = hwm;
90 done:
91 return ret;
94 static NTSTATUS idmap_tdb_common_allocate_id(struct idmap_domain *dom,
95 struct unixid *xid)
97 const char *hwmkey;
98 const char *hwmtype;
99 uint32_t hwm = 0;
100 NTSTATUS status;
101 struct idmap_tdb_common_allocate_id_context state;
102 struct idmap_tdb_common_context *ctx;
104 ctx =
105 talloc_get_type_abort(dom->private_data,
106 struct idmap_tdb_common_context);
108 /* Get current high water mark */
109 switch (xid->type) {
111 case ID_TYPE_UID:
112 hwmkey = ctx->hwmkey_uid;
113 hwmtype = "UID";
114 break;
116 case ID_TYPE_GID:
117 hwmkey = ctx->hwmkey_gid;
118 hwmtype = "GID";
119 break;
121 default:
122 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
123 return NT_STATUS_INVALID_PARAMETER;
126 state.hwm = hwm;
127 state.high_hwm = ctx->max_id;
128 state.hwmtype = hwmtype;
129 state.hwmkey = hwmkey;
131 status = dbwrap_trans_do(ctx->db, idmap_tdb_common_allocate_id_action,
132 &state);
134 if (NT_STATUS_IS_OK(status)) {
135 xid->id = state.hwm;
136 DEBUG(10, ("New %s = %d\n", hwmtype, state.hwm));
137 } else {
138 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
141 return status;
145 * Allocate a new unix-ID.
146 * For now this is for the default idmap domain only.
147 * Should be extended later on.
149 NTSTATUS idmap_tdb_common_get_new_id(struct idmap_domain * dom,
150 struct unixid * id)
152 NTSTATUS ret;
154 if (!strequal(dom->name, "*")) {
155 DEBUG(3, ("idmap_tdb_common_get_new_id: "
156 "Refusing allocation of a new unixid for domain'%s'. "
157 "Currently only supported for the default "
158 "domain \"*\".\n", dom->name));
159 return NT_STATUS_NOT_IMPLEMENTED;
162 ret = idmap_tdb_common_allocate_id(dom, id);
164 return ret;
168 * store a mapping in the database.
171 struct idmap_tdb_common_set_mapping_context {
172 const char *ksidstr;
173 const char *kidstr;
176 static NTSTATUS idmap_tdb_common_set_mapping_action(struct db_context *db,
177 void *private_data)
179 TDB_DATA data;
180 NTSTATUS ret;
181 struct idmap_tdb_common_set_mapping_context *state = private_data;
182 TALLOC_CTX *tmp_ctx = talloc_stackframe();
184 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
186 /* check whether sid mapping is already present in db */
187 ret = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr, &data);
188 if (NT_STATUS_IS_OK(ret)) {
189 ret = NT_STATUS_OBJECT_NAME_COLLISION;
190 goto done;
193 ret = dbwrap_store_bystring(db, state->ksidstr,
194 string_term_tdb_data(state->kidstr),
195 TDB_INSERT);
196 if (!NT_STATUS_IS_OK(ret)) {
197 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
198 goto done;
201 ret = dbwrap_store_bystring(db, state->kidstr,
202 string_term_tdb_data(state->ksidstr),
203 TDB_INSERT);
204 if (!NT_STATUS_IS_OK(ret)) {
205 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
206 /* try to remove the previous stored SID -> ID map */
207 dbwrap_delete_bystring(db, state->ksidstr);
208 goto done;
211 DEBUG(10, ("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
213 done:
214 talloc_free(tmp_ctx);
215 return ret;
218 NTSTATUS idmap_tdb_common_set_mapping(struct idmap_domain * dom,
219 const struct id_map * map)
221 struct idmap_tdb_common_context *ctx;
222 struct idmap_tdb_common_set_mapping_context state;
223 NTSTATUS ret;
224 struct dom_sid_buf ksidstr;
225 char *kidstr = NULL;
227 if (!map || !map->sid) {
228 return NT_STATUS_INVALID_PARAMETER;
231 /* TODO: should we filter a set_mapping using low/high filters ? */
233 ctx =
234 talloc_get_type_abort(dom->private_data,
235 struct idmap_tdb_common_context);
237 switch (map->xid.type) {
239 case ID_TYPE_UID:
240 kidstr =
241 talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
242 break;
244 case ID_TYPE_GID:
245 kidstr =
246 talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
247 break;
249 default:
250 DEBUG(2, ("INVALID unix ID type: 0x%02x\n", map->xid.type));
251 return NT_STATUS_INVALID_PARAMETER;
254 if (kidstr == NULL) {
255 DEBUG(0, ("ERROR: Out of memory!\n"));
256 ret = NT_STATUS_NO_MEMORY;
257 goto done;
260 state.ksidstr = dom_sid_str_buf(map->sid, &ksidstr);
261 state.kidstr = kidstr;
263 ret = dbwrap_trans_do(ctx->db, idmap_tdb_common_set_mapping_action,
264 &state);
266 done:
267 talloc_free(kidstr);
268 return ret;
272 * Create a new mapping for an unmapped SID, also allocating a new ID.
273 * This should be run inside a transaction.
275 * TODO:
276 * Properly integrate this with multi domain idmap config:
277 * Currently, the allocator is default-config only.
279 NTSTATUS idmap_tdb_common_new_mapping(struct idmap_domain * dom,
280 struct id_map * map)
282 NTSTATUS ret;
283 struct idmap_tdb_common_context *ctx;
285 ctx =
286 talloc_get_type_abort(dom->private_data,
287 struct idmap_tdb_common_context);
289 ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
291 return ret;
295 lookup a set of unix ids
297 NTSTATUS idmap_tdb_common_unixids_to_sids(struct idmap_domain * dom,
298 struct id_map ** ids)
300 NTSTATUS ret;
301 size_t i, num_mapped = 0;
302 struct idmap_tdb_common_context *ctx;
304 NTSTATUS(*unixid_to_sid_fn) (struct idmap_domain * dom,
305 struct id_map * map);
306 ctx =
307 talloc_get_type_abort(dom->private_data,
308 struct idmap_tdb_common_context);
310 if (ctx->unixid_to_sid_fn == NULL) {
311 unixid_to_sid_fn = idmap_tdb_common_unixid_to_sid;
312 } else {
313 unixid_to_sid_fn = ctx->unixid_to_sid_fn;
316 /* initialize the status to avoid surprise */
317 for (i = 0; ids[i]; i++) {
318 ids[i]->status = ID_UNKNOWN;
321 for (i = 0; ids[i]; i++) {
322 ret = unixid_to_sid_fn(dom, ids[i]);
323 if (!NT_STATUS_IS_OK(ret)) {
325 /* if it is just a failed mapping continue */
326 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
328 /* make sure it is marked as unmapped */
329 ids[i]->status = ID_UNMAPPED;
330 continue;
333 /* some fatal error occurred, return immediately */
334 goto done;
337 /* all ok, id is mapped */
338 ids[i]->status = ID_MAPPED;
339 num_mapped += 1;
342 ret = NT_STATUS_OK;
344 done:
346 if (NT_STATUS_IS_OK(ret)) {
347 if (i == 0 || num_mapped == 0) {
348 ret = NT_STATUS_NONE_MAPPED;
349 } else if (num_mapped < i) {
350 ret = STATUS_SOME_UNMAPPED;
351 } else {
352 ret = NT_STATUS_OK;
356 return ret;
360 default single id to sid lookup function
362 NTSTATUS idmap_tdb_common_unixid_to_sid(struct idmap_domain * dom,
363 struct id_map * map)
365 NTSTATUS ret;
366 TDB_DATA data;
367 char *keystr;
368 struct idmap_tdb_common_context *ctx;
370 if (!dom || !map) {
371 return NT_STATUS_INVALID_PARAMETER;
374 ctx =
375 talloc_get_type_abort(dom->private_data,
376 struct idmap_tdb_common_context);
378 /* apply filters before checking */
379 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
380 DEBUG(5,
381 ("Requested id (%u) out of range (%u - %u). Filtered!\n",
382 map->xid.id, dom->low_id, dom->high_id));
383 return NT_STATUS_NONE_MAPPED;
386 switch (map->xid.type) {
388 case ID_TYPE_UID:
389 keystr =
390 talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
391 break;
393 case ID_TYPE_GID:
394 keystr =
395 talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
396 break;
398 default:
399 DEBUG(2, ("INVALID unix ID type: 0x%02x\n", map->xid.type));
400 return NT_STATUS_INVALID_PARAMETER;
403 if (keystr == NULL) {
404 DEBUG(0, ("Out of memory!\n"));
405 ret = NT_STATUS_NO_MEMORY;
406 goto done;
409 DEBUG(10, ("Fetching record %s\n", keystr));
411 /* Check if the mapping exists */
412 ret = dbwrap_fetch_bystring(ctx->db, keystr, keystr, &data);
414 if (!NT_STATUS_IS_OK(ret)) {
415 DEBUG(10, ("Record %s not found\n", keystr));
416 ret = NT_STATUS_NONE_MAPPED;
417 goto done;
420 if ((data.dsize == 0) || (data.dptr[data.dsize-1] != '\0')) {
421 DBG_DEBUG("Invalid record length %zu\n", data.dsize);
422 ret = NT_STATUS_INTERNAL_DB_ERROR;
423 goto done;
426 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
427 DEBUG(10, ("INVALID SID (%s) in record %s\n",
428 (const char *)data.dptr, keystr));
429 ret = NT_STATUS_INTERNAL_DB_ERROR;
430 goto done;
433 DEBUG(10, ("Found record %s -> %s\n", keystr, (const char *)data.dptr));
434 ret = NT_STATUS_OK;
436 done:
437 talloc_free(keystr);
438 return ret;
441 /**********************************
442 Single sid to id lookup function.
443 **********************************/
445 NTSTATUS idmap_tdb_common_sid_to_unixid(struct idmap_domain * dom,
446 struct id_map * map)
448 NTSTATUS ret;
449 TDB_DATA data;
450 struct dom_sid_buf keystr;
451 unsigned long rec_id = 0;
452 struct idmap_tdb_common_context *ctx;
453 TALLOC_CTX *tmp_ctx = talloc_stackframe();
455 if (!dom || !map) {
456 talloc_free(tmp_ctx);
457 return NT_STATUS_INVALID_PARAMETER;
460 ctx =
461 talloc_get_type_abort(dom->private_data,
462 struct idmap_tdb_common_context);
464 dom_sid_str_buf(map->sid, &keystr);
466 DEBUG(10, ("Fetching record %s\n", keystr.buf));
468 /* Check if sid is present in database */
469 ret = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr.buf, &data);
470 if (!NT_STATUS_IS_OK(ret)) {
471 DEBUG(10, ("Record %s not found\n", keystr.buf));
472 ret = NT_STATUS_NONE_MAPPED;
473 goto done;
476 /* What type of record is this ? */
477 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) {
478 /* Try a UID record. */
479 map->xid.id = rec_id;
480 map->xid.type = ID_TYPE_UID;
481 DEBUG(10,
482 ("Found uid record %s -> %s \n", keystr.buf,
483 (const char *)data.dptr));
484 ret = NT_STATUS_OK;
486 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) {
487 /* Try a GID record. */
488 map->xid.id = rec_id;
489 map->xid.type = ID_TYPE_GID;
490 DEBUG(10,
491 ("Found gid record %s -> %s \n", keystr.buf,
492 (const char *)data.dptr));
493 ret = NT_STATUS_OK;
495 } else { /* Unknown record type ! */
496 DEBUG(2,
497 ("Found INVALID record %s -> %s\n", keystr.buf,
498 (const char *)data.dptr));
499 ret = NT_STATUS_INTERNAL_DB_ERROR;
500 goto done;
503 /* apply filters before returning result */
504 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
505 DEBUG(5,
506 ("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;
511 done:
512 talloc_free(tmp_ctx);
513 return ret;
516 /**********************************
517 lookup a set of sids
518 **********************************/
520 struct idmap_tdb_common_sids_to_unixids_context {
521 struct idmap_domain *dom;
522 struct id_map **ids;
523 bool allocate_unmapped;
524 NTSTATUS(*sid_to_unixid_fn) (struct idmap_domain * dom,
525 struct id_map * map);
528 static NTSTATUS idmap_tdb_common_sids_to_unixids_action(struct db_context *db,
529 void *private_data)
531 struct idmap_tdb_common_sids_to_unixids_context *state = private_data;
532 size_t i, num_mapped = 0;
533 NTSTATUS ret = NT_STATUS_OK;
535 DEBUG(10, ("idmap_tdb_common_sids_to_unixids: "
536 " domain: [%s], allocate: %s\n",
537 state->dom->name, state->allocate_unmapped ? "yes" : "no"));
539 for (i = 0; state->ids[i]; i++) {
540 if ((state->ids[i]->status == ID_UNKNOWN) ||
541 /* retry if we could not map in previous run: */
542 (state->ids[i]->status == ID_UNMAPPED)) {
543 NTSTATUS ret2;
545 ret2 = state->sid_to_unixid_fn(state->dom,
546 state->ids[i]);
548 if (!NT_STATUS_IS_OK(ret2)) {
550 /* if it is just a failed mapping, continue */
551 if (NT_STATUS_EQUAL
552 (ret2, NT_STATUS_NONE_MAPPED)) {
554 /* make sure it is marked as unmapped */
555 state->ids[i]->status = ID_UNMAPPED;
556 ret = STATUS_SOME_UNMAPPED;
557 } else {
559 * some fatal error occurred,
560 * return immediately
562 ret = ret2;
563 goto done;
565 } else {
566 /* all ok, id is mapped */
567 state->ids[i]->status = ID_MAPPED;
571 if (state->ids[i]->status == ID_MAPPED) {
572 num_mapped += 1;
575 if ((state->ids[i]->status == ID_UNMAPPED) &&
576 state->allocate_unmapped) {
577 ret =
578 idmap_tdb_common_new_mapping(state->dom,
579 state->ids[i]);
580 DBG_DEBUG("idmap_tdb_common_new_mapping returned %s\n",
581 nt_errstr(ret));
582 if (!NT_STATUS_IS_OK(ret)) {
583 ret = STATUS_SOME_UNMAPPED;
584 continue;
586 num_mapped += 1;
590 done:
592 if (NT_STATUS_IS_OK(ret) ||
593 NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED)) {
594 if (i == 0 || num_mapped == 0) {
595 ret = NT_STATUS_NONE_MAPPED;
596 } else if (num_mapped < i) {
597 ret = STATUS_SOME_UNMAPPED;
598 } else {
599 ret = NT_STATUS_OK;
603 return ret;
606 NTSTATUS idmap_tdb_common_sids_to_unixids(struct idmap_domain * dom,
607 struct id_map ** ids)
609 NTSTATUS ret;
610 int i;
611 struct idmap_tdb_common_sids_to_unixids_context state;
612 struct idmap_tdb_common_context *ctx;
614 ctx =
615 talloc_get_type_abort(dom->private_data,
616 struct idmap_tdb_common_context);
618 /* initialize the status to avoid surprise */
619 for (i = 0; ids[i]; i++) {
620 ids[i]->status = ID_UNKNOWN;
623 state.dom = dom;
624 state.ids = ids;
625 state.allocate_unmapped = false;
626 if (ctx->sid_to_unixid_fn == NULL) {
627 state.sid_to_unixid_fn = idmap_tdb_common_sid_to_unixid;
628 } else {
629 state.sid_to_unixid_fn = ctx->sid_to_unixid_fn;
632 ret = idmap_tdb_common_sids_to_unixids_action(ctx->db, &state);
634 if ( (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) ||
635 NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) &&
636 !dom->read_only) {
637 state.allocate_unmapped = true;
638 ret = dbwrap_trans_do(ctx->db,
639 idmap_tdb_common_sids_to_unixids_action,
640 &state);
643 return ret;