s3-ldapsam: Fix Bug #6313: ldapsam_update_sam_account() crashes while doing talloc_fr...
[Samba.git] / source3 / winbindd / idmap_tdb2.c
blobb2723270ebc532da512d261c5dbf5ce3797e8bec
1 /*
2 Unix SMB/CIFS implementation.
4 idmap TDB2 backend, used for clustered Samba setups.
6 This uses dbwrap to access tdb files. The location can be set
7 using tdb:idmap2.tdb =" in smb.conf
9 Copyright (C) Andrew Tridgell 2007
11 This is heavily based upon idmap_tdb.c, which is:
13 Copyright (C) Tim Potter 2000
14 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
15 Copyright (C) Jeremy Allison 2006
16 Copyright (C) Simo Sorce 2003-2006
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 "winbindd.h"
36 #undef DBGC_CLASS
37 #define DBGC_CLASS DBGC_IDMAP
39 /* High water mark keys */
40 #define HWM_GROUP "GROUP HWM"
41 #define HWM_USER "USER HWM"
43 static struct idmap_tdb2_state {
44 /* User and group id pool */
45 uid_t low_uid, high_uid; /* Range of uids to allocate */
46 gid_t low_gid, high_gid; /* Range of gids to allocate */
47 const char *idmap_script;
48 } idmap_tdb2_state;
52 /* handle to the permanent tdb */
53 static struct db_context *idmap_tdb2;
55 static NTSTATUS idmap_tdb2_alloc_load(void);
57 static NTSTATUS idmap_tdb2_load_ranges(void)
59 uid_t low_uid = 0;
60 uid_t high_uid = 0;
61 gid_t low_gid = 0;
62 gid_t high_gid = 0;
64 if (!lp_idmap_uid(&low_uid, &high_uid)) {
65 DEBUG(1, ("idmap uid missing\n"));
66 return NT_STATUS_UNSUCCESSFUL;
69 if (!lp_idmap_gid(&low_gid, &high_gid)) {
70 DEBUG(1, ("idmap gid missing\n"));
71 return NT_STATUS_UNSUCCESSFUL;
74 idmap_tdb2_state.low_uid = low_uid;
75 idmap_tdb2_state.high_uid = high_uid;
76 idmap_tdb2_state.low_gid = low_gid;
77 idmap_tdb2_state.high_gid = high_gid;
79 if (idmap_tdb2_state.high_uid <= idmap_tdb2_state.low_uid) {
80 DEBUG(1, ("idmap uid range missing or invalid\n"));
81 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
82 return NT_STATUS_UNSUCCESSFUL;
85 if (idmap_tdb2_state.high_gid <= idmap_tdb2_state.low_gid) {
86 DEBUG(1, ("idmap gid range missing or invalid\n"));
87 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
88 return NT_STATUS_UNSUCCESSFUL;
91 return NT_STATUS_OK;
95 open the permanent tdb
97 static NTSTATUS idmap_tdb2_open_db(void)
99 char *db_path;
101 if (idmap_tdb2) {
102 /* its already open */
103 return NT_STATUS_OK;
106 db_path = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
107 if (db_path == NULL) {
108 /* fall back to the private directory, which, despite
109 its name, is usually on shared storage */
110 db_path = talloc_asprintf(NULL, "%s/idmap2.tdb", lp_private_dir());
112 NT_STATUS_HAVE_NO_MEMORY(db_path);
114 /* Open idmap repository */
115 idmap_tdb2 = db_open(NULL, db_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
116 TALLOC_FREE(db_path);
118 if (idmap_tdb2 == NULL) {
119 DEBUG(0, ("Unable to open idmap_tdb2 database '%s'\n",
120 db_path));
121 return NT_STATUS_UNSUCCESSFUL;
124 /* load the ranges and high/low water marks */
125 return idmap_tdb2_alloc_load();
130 load the idmap allocation ranges and high/low water marks
132 static NTSTATUS idmap_tdb2_alloc_load(void)
134 NTSTATUS status;
135 uint32 low_id;
137 /* see if a idmap script is configured */
138 idmap_tdb2_state.idmap_script = lp_parm_const_string(-1, "idmap",
139 "script", NULL);
141 if (idmap_tdb2_state.idmap_script) {
142 DEBUG(1, ("using idmap script '%s'\n",
143 idmap_tdb2_state.idmap_script));
146 /* load ranges */
148 status = idmap_tdb2_load_ranges();
149 if (!NT_STATUS_IS_OK(status)) {
150 return status;
153 /* Create high water marks for group and user id */
155 low_id = dbwrap_fetch_int32(idmap_tdb2, HWM_USER);
156 if ((low_id == -1) || (low_id < idmap_tdb2_state.low_uid)) {
157 if (!NT_STATUS_IS_OK(dbwrap_trans_store_int32(
158 idmap_tdb2, HWM_USER,
159 idmap_tdb2_state.low_uid))) {
160 DEBUG(0, ("Unable to initialise user hwm in idmap "
161 "database\n"));
162 return NT_STATUS_INTERNAL_DB_ERROR;
166 low_id = dbwrap_fetch_int32(idmap_tdb2, HWM_GROUP);
167 if ((low_id == -1) || (low_id < idmap_tdb2_state.low_gid)) {
168 if (!NT_STATUS_IS_OK(dbwrap_trans_store_int32(
169 idmap_tdb2, HWM_GROUP,
170 idmap_tdb2_state.low_gid))) {
171 DEBUG(0, ("Unable to initialise group hwm in idmap "
172 "database\n"));
173 return NT_STATUS_INTERNAL_DB_ERROR;
177 return NT_STATUS_OK;
182 Initialise idmap alloc database.
184 static NTSTATUS idmap_tdb2_alloc_init(const char *params)
186 /* nothing to do - we want to avoid opening the permanent
187 database if possible. Instead we load the params when we
188 first need it. */
189 return NT_STATUS_OK;
194 Allocate a new id.
196 static NTSTATUS idmap_tdb2_allocate_id(struct unixid *xid)
198 bool ret;
199 const char *hwmkey;
200 const char *hwmtype;
201 uint32_t high_hwm;
202 uint32_t hwm;
203 int res;
204 NTSTATUS status;
206 status = idmap_tdb2_open_db();
207 NT_STATUS_NOT_OK_RETURN(status);
209 /* Get current high water mark */
210 switch (xid->type) {
212 case ID_TYPE_UID:
213 hwmkey = HWM_USER;
214 hwmtype = "UID";
215 high_hwm = idmap_tdb2_state.high_uid;
216 break;
218 case ID_TYPE_GID:
219 hwmkey = HWM_GROUP;
220 hwmtype = "GID";
221 high_hwm = idmap_tdb2_state.high_gid;
222 break;
224 default:
225 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
226 return NT_STATUS_INVALID_PARAMETER;
229 res = idmap_tdb2->transaction_start(idmap_tdb2);
230 if (res != 0) {
231 DEBUG(1,(__location__ " Failed to start transaction\n"));
232 return NT_STATUS_UNSUCCESSFUL;
235 if ((hwm = dbwrap_fetch_int32(idmap_tdb2, hwmkey)) == -1) {
236 idmap_tdb2->transaction_cancel(idmap_tdb2);
237 return NT_STATUS_INTERNAL_DB_ERROR;
240 /* check it is in the range */
241 if (hwm > high_hwm) {
242 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
243 hwmtype, (unsigned long)high_hwm));
244 idmap_tdb2->transaction_cancel(idmap_tdb2);
245 return NT_STATUS_UNSUCCESSFUL;
248 /* fetch a new id and increment it */
249 ret = dbwrap_change_uint32_atomic(idmap_tdb2, hwmkey, &hwm, 1);
250 if (ret == -1) {
251 DEBUG(1, ("Fatal error while fetching a new %s value\n!", hwmtype));
252 idmap_tdb2->transaction_cancel(idmap_tdb2);
253 return NT_STATUS_UNSUCCESSFUL;
256 /* recheck it is in the range */
257 if (hwm > high_hwm) {
258 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
259 hwmtype, (unsigned long)high_hwm));
260 idmap_tdb2->transaction_cancel(idmap_tdb2);
261 return NT_STATUS_UNSUCCESSFUL;
264 res = idmap_tdb2->transaction_commit(idmap_tdb2);
265 if (res != 0) {
266 DEBUG(1,(__location__ " Failed to commit transaction\n"));
267 return NT_STATUS_UNSUCCESSFUL;
270 xid->id = hwm;
271 DEBUG(10,("New %s = %d\n", hwmtype, hwm));
273 return NT_STATUS_OK;
277 Get current highest id.
279 static NTSTATUS idmap_tdb2_get_hwm(struct unixid *xid)
281 const char *hwmkey;
282 const char *hwmtype;
283 uint32_t hwm;
284 uint32_t high_hwm;
285 NTSTATUS status;
287 status = idmap_tdb2_open_db();
288 NT_STATUS_NOT_OK_RETURN(status);
290 /* Get current high water mark */
291 switch (xid->type) {
293 case ID_TYPE_UID:
294 hwmkey = HWM_USER;
295 hwmtype = "UID";
296 high_hwm = idmap_tdb2_state.high_uid;
297 break;
299 case ID_TYPE_GID:
300 hwmkey = HWM_GROUP;
301 hwmtype = "GID";
302 high_hwm = idmap_tdb2_state.high_gid;
303 break;
305 default:
306 return NT_STATUS_INVALID_PARAMETER;
309 if ((hwm = dbwrap_fetch_int32(idmap_tdb2, hwmkey)) == -1) {
310 return NT_STATUS_INTERNAL_DB_ERROR;
313 xid->id = hwm;
315 /* Warn if it is out of range */
316 if (hwm >= high_hwm) {
317 DEBUG(0, ("Warning: %s range full!! (max: %lu)\n",
318 hwmtype, (unsigned long)high_hwm));
321 return NT_STATUS_OK;
325 Set high id.
327 static NTSTATUS idmap_tdb2_set_hwm(struct unixid *xid)
329 /* not supported, or we would invalidate the cache tdb on
330 other nodes */
331 DEBUG(0,("idmap_tdb2_set_hwm not supported\n"));
332 return NT_STATUS_NOT_SUPPORTED;
336 Close the alloc tdb
338 static NTSTATUS idmap_tdb2_alloc_close(void)
340 /* don't actually close it */
341 return NT_STATUS_OK;
345 IDMAP MAPPING TDB BACKEND
347 struct idmap_tdb2_context {
348 uint32_t filter_low_id;
349 uint32_t filter_high_id;
353 Initialise idmap database.
355 static NTSTATUS idmap_tdb2_db_init(struct idmap_domain *dom,
356 const char *params)
358 NTSTATUS ret;
359 struct idmap_tdb2_context *ctx;
360 char *config_option = NULL;
361 const char *range;
362 NTSTATUS status;
364 status = idmap_tdb2_open_db();
365 NT_STATUS_NOT_OK_RETURN(status);
367 ctx = talloc(dom, struct idmap_tdb2_context);
368 if ( ! ctx) {
369 DEBUG(0, ("Out of memory!\n"));
370 return NT_STATUS_NO_MEMORY;
373 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
374 if ( ! config_option) {
375 DEBUG(0, ("Out of memory!\n"));
376 ret = NT_STATUS_NO_MEMORY;
377 goto failed;
380 range = lp_parm_const_string(-1, config_option, "range", NULL);
381 if (( ! range) ||
382 (sscanf(range, "%u - %u", &ctx->filter_low_id, &ctx->filter_high_id) != 2) ||
383 (ctx->filter_low_id > ctx->filter_high_id)) {
384 ctx->filter_low_id = 0;
385 ctx->filter_high_id = 0;
388 dom->private_data = ctx;
390 talloc_free(config_option);
391 return NT_STATUS_OK;
393 failed:
394 talloc_free(ctx);
395 return ret;
400 run a script to perform a mapping
402 The script should the following command lines:
404 SIDTOID S-1-xxxx
405 IDTOSID UID xxxx
406 IDTOSID GID xxxx
408 and should return one of the following as a single line of text
409 UID:xxxx
410 GID:xxxx
411 SID:xxxx
412 ERR:xxxx
414 static NTSTATUS idmap_tdb2_script(struct idmap_tdb2_context *ctx, struct id_map *map,
415 const char *fmt, ...)
417 va_list ap;
418 char *cmd;
419 FILE *p;
420 char line[64];
421 unsigned long v;
423 cmd = talloc_asprintf(ctx, "%s ", idmap_tdb2_state.idmap_script);
424 NT_STATUS_HAVE_NO_MEMORY(cmd);
426 va_start(ap, fmt);
427 cmd = talloc_vasprintf_append(cmd, fmt, ap);
428 va_end(ap);
429 NT_STATUS_HAVE_NO_MEMORY(cmd);
431 p = popen(cmd, "r");
432 talloc_free(cmd);
433 if (p == NULL) {
434 return NT_STATUS_NONE_MAPPED;
437 if (fgets(line, sizeof(line)-1, p) == NULL) {
438 pclose(p);
439 return NT_STATUS_NONE_MAPPED;
441 pclose(p);
443 DEBUG(10,("idmap script gave: %s\n", line));
445 if (sscanf(line, "UID:%lu", &v) == 1) {
446 map->xid.id = v;
447 map->xid.type = ID_TYPE_UID;
448 } else if (sscanf(line, "GID:%lu", &v) == 1) {
449 map->xid.id = v;
450 map->xid.type = ID_TYPE_GID;
451 } else if (strncmp(line, "SID:S-", 6) == 0) {
452 if (!string_to_sid(map->sid, &line[4])) {
453 DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
454 line, idmap_tdb2_state.idmap_script));
455 return NT_STATUS_NONE_MAPPED;
457 } else {
458 DEBUG(0,("Bad reply '%s' from idmap script %s\n",
459 line, idmap_tdb2_state.idmap_script));
460 return NT_STATUS_NONE_MAPPED;
463 return NT_STATUS_OK;
469 Single id to sid lookup function.
471 static NTSTATUS idmap_tdb2_id_to_sid(struct idmap_tdb2_context *ctx, struct id_map *map)
473 NTSTATUS ret;
474 TDB_DATA data;
475 char *keystr;
476 NTSTATUS status;
478 status = idmap_tdb2_open_db();
479 NT_STATUS_NOT_OK_RETURN(status);
481 if (!ctx || !map) {
482 return NT_STATUS_INVALID_PARAMETER;
485 /* apply filters before checking */
486 if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
487 (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
488 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
489 map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
490 return NT_STATUS_NONE_MAPPED;
493 switch (map->xid.type) {
495 case ID_TYPE_UID:
496 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
497 break;
499 case ID_TYPE_GID:
500 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
501 break;
503 default:
504 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
505 return NT_STATUS_INVALID_PARAMETER;
508 /* final SAFE_FREE safe */
509 data.dptr = NULL;
511 if (keystr == NULL) {
512 DEBUG(0, ("Out of memory!\n"));
513 ret = NT_STATUS_NO_MEMORY;
514 goto done;
517 DEBUG(10,("Fetching record %s\n", keystr));
519 /* Check if the mapping exists */
520 data = dbwrap_fetch_bystring(idmap_tdb2, keystr, keystr);
522 if (!data.dptr) {
523 fstring sidstr;
525 DEBUG(10,("Record %s not found\n", keystr));
526 if (idmap_tdb2_state.idmap_script == NULL) {
527 ret = NT_STATUS_NONE_MAPPED;
528 goto done;
531 ret = idmap_tdb2_script(ctx, map, "IDTOSID %s", keystr);
533 /* store it on shared storage */
534 if (!NT_STATUS_IS_OK(ret)) {
535 goto done;
538 if (sid_to_fstring(sidstr, map->sid)) {
539 /* both forward and reverse mappings */
540 dbwrap_store_bystring(idmap_tdb2, keystr,
541 string_term_tdb_data(sidstr),
542 TDB_REPLACE);
543 dbwrap_store_bystring(idmap_tdb2, sidstr,
544 string_term_tdb_data(keystr),
545 TDB_REPLACE);
547 goto done;
550 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
551 DEBUG(10,("INVALID SID (%s) in record %s\n",
552 (const char *)data.dptr, keystr));
553 ret = NT_STATUS_INTERNAL_DB_ERROR;
554 goto done;
557 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
558 ret = NT_STATUS_OK;
560 done:
561 talloc_free(keystr);
562 return ret;
567 Single sid to id lookup function.
569 static NTSTATUS idmap_tdb2_sid_to_id(struct idmap_tdb2_context *ctx, struct id_map *map)
571 NTSTATUS ret;
572 TDB_DATA data;
573 char *keystr;
574 unsigned long rec_id = 0;
575 TALLOC_CTX *tmp_ctx = talloc_stackframe();
577 ret = idmap_tdb2_open_db();
578 NT_STATUS_NOT_OK_RETURN(ret);
580 keystr = sid_string_talloc(tmp_ctx, map->sid);
581 if (keystr == NULL) {
582 DEBUG(0, ("Out of memory!\n"));
583 ret = NT_STATUS_NO_MEMORY;
584 goto done;
587 DEBUG(10,("Fetching record %s\n", keystr));
589 /* Check if sid is present in database */
590 data = dbwrap_fetch_bystring(idmap_tdb2, tmp_ctx, keystr);
591 if (!data.dptr) {
592 fstring idstr;
594 DEBUG(10,(__location__ " Record %s not found\n", keystr));
596 if (idmap_tdb2_state.idmap_script == NULL) {
597 ret = NT_STATUS_NONE_MAPPED;
598 goto done;
601 ret = idmap_tdb2_script(ctx, map, "SIDTOID %s", keystr);
602 /* store it on shared storage */
603 if (!NT_STATUS_IS_OK(ret)) {
604 goto done;
607 snprintf(idstr, sizeof(idstr), "%cID %lu",
608 map->xid.type == ID_TYPE_UID?'U':'G',
609 (unsigned long)map->xid.id);
610 /* store both forward and reverse mappings */
611 dbwrap_store_bystring(idmap_tdb2, keystr, string_term_tdb_data(idstr),
612 TDB_REPLACE);
613 dbwrap_store_bystring(idmap_tdb2, idstr, string_term_tdb_data(keystr),
614 TDB_REPLACE);
615 goto done;
618 /* What type of record is this ? */
619 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
620 map->xid.id = rec_id;
621 map->xid.type = ID_TYPE_UID;
622 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
623 ret = NT_STATUS_OK;
625 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
626 map->xid.id = rec_id;
627 map->xid.type = ID_TYPE_GID;
628 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
629 ret = NT_STATUS_OK;
631 } else { /* Unknown record type ! */
632 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
633 ret = NT_STATUS_INTERNAL_DB_ERROR;
636 /* apply filters before returning result */
637 if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
638 (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
639 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
640 map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
641 ret = NT_STATUS_NONE_MAPPED;
644 done:
645 talloc_free(tmp_ctx);
646 return ret;
650 lookup a set of unix ids.
652 static NTSTATUS idmap_tdb2_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
654 struct idmap_tdb2_context *ctx;
655 NTSTATUS ret;
656 int i;
658 /* initialize the status to avoid suprise */
659 for (i = 0; ids[i]; i++) {
660 ids[i]->status = ID_UNKNOWN;
663 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
665 for (i = 0; ids[i]; i++) {
666 ret = idmap_tdb2_id_to_sid(ctx, ids[i]);
667 if ( ! NT_STATUS_IS_OK(ret)) {
669 /* if it is just a failed mapping continue */
670 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
672 /* make sure it is marked as unmapped */
673 ids[i]->status = ID_UNMAPPED;
674 continue;
677 /* some fatal error occurred, return immediately */
678 goto done;
681 /* all ok, id is mapped */
682 ids[i]->status = ID_MAPPED;
685 ret = NT_STATUS_OK;
687 done:
688 return ret;
692 lookup a set of sids.
694 static NTSTATUS idmap_tdb2_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
696 struct idmap_tdb2_context *ctx;
697 NTSTATUS ret;
698 int i;
700 /* initialize the status to avoid suprise */
701 for (i = 0; ids[i]; i++) {
702 ids[i]->status = ID_UNKNOWN;
705 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
707 for (i = 0; ids[i]; i++) {
708 ret = idmap_tdb2_sid_to_id(ctx, ids[i]);
709 if ( ! NT_STATUS_IS_OK(ret)) {
711 /* if it is just a failed mapping continue */
712 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
714 /* make sure it is marked as unmapped */
715 ids[i]->status = ID_UNMAPPED;
716 continue;
719 /* some fatal error occurred, return immediately */
720 goto done;
723 /* all ok, id is mapped */
724 ids[i]->status = ID_MAPPED;
727 ret = NT_STATUS_OK;
729 done:
730 return ret;
735 set a mapping.
737 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom, const struct id_map *map)
739 struct idmap_tdb2_context *ctx;
740 NTSTATUS ret;
741 TDB_DATA data;
742 char *ksidstr, *kidstr;
743 int res;
744 bool started_transaction = false;
746 if (!map || !map->sid) {
747 return NT_STATUS_INVALID_PARAMETER;
750 ksidstr = kidstr = NULL;
752 /* TODO: should we filter a set_mapping using low/high filters ? */
754 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
756 switch (map->xid.type) {
758 case ID_TYPE_UID:
759 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
760 break;
762 case ID_TYPE_GID:
763 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
764 break;
766 default:
767 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
768 return NT_STATUS_INVALID_PARAMETER;
771 if (kidstr == NULL) {
772 DEBUG(0, ("ERROR: Out of memory!\n"));
773 ret = NT_STATUS_NO_MEMORY;
774 goto done;
777 if (!(ksidstr = sid_string_talloc(ctx, map->sid))) {
778 DEBUG(0, ("Out of memory!\n"));
779 ret = NT_STATUS_NO_MEMORY;
780 goto done;
783 DEBUG(10, ("Storing %s <-> %s map\n", ksidstr, kidstr));
785 res = idmap_tdb2->transaction_start(idmap_tdb2);
786 if (res != 0) {
787 DEBUG(1,(__location__ " Failed to start transaction\n"));
788 ret = NT_STATUS_UNSUCCESSFUL;
789 goto done;
792 started_transaction = true;
794 /* check wheter sid mapping is already present in db */
795 data = dbwrap_fetch_bystring(idmap_tdb2, ksidstr, ksidstr);
796 if (data.dptr) {
797 ret = NT_STATUS_OBJECT_NAME_COLLISION;
798 goto done;
801 ret = dbwrap_store_bystring(idmap_tdb2, ksidstr, string_term_tdb_data(kidstr),
802 TDB_INSERT);
803 if (!NT_STATUS_IS_OK(ret)) {
804 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
805 goto done;
807 ret = dbwrap_store_bystring(idmap_tdb2, kidstr, string_term_tdb_data(ksidstr),
808 TDB_INSERT);
809 if (!NT_STATUS_IS_OK(ret)) {
810 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
811 /* try to remove the previous stored SID -> ID map */
812 dbwrap_delete_bystring(idmap_tdb2, ksidstr);
813 goto done;
816 started_transaction = false;
818 res = idmap_tdb2->transaction_commit(idmap_tdb2);
819 if (res != 0) {
820 DEBUG(1,(__location__ " Failed to commit transaction\n"));
821 ret = NT_STATUS_UNSUCCESSFUL;
822 goto done;
825 DEBUG(10,("Stored %s <-> %s\n", ksidstr, kidstr));
826 ret = NT_STATUS_OK;
828 done:
829 if (started_transaction) {
830 idmap_tdb2->transaction_cancel(idmap_tdb2);
832 talloc_free(ksidstr);
833 talloc_free(kidstr);
834 return ret;
838 remove a mapping.
840 static NTSTATUS idmap_tdb2_remove_mapping(struct idmap_domain *dom, const struct id_map *map)
842 /* not supported as it would invalidate the cache tdb on other
843 nodes */
844 DEBUG(0,("idmap_tdb2_remove_mapping not supported\n"));
845 return NT_STATUS_NOT_SUPPORTED;
849 Close the idmap tdb instance
851 static NTSTATUS idmap_tdb2_close(struct idmap_domain *dom)
853 /* don't do anything */
854 return NT_STATUS_OK;
859 Dump all mappings out
861 static NTSTATUS idmap_tdb2_dump_data(struct idmap_domain *dom, struct id_map **maps, int *num_maps)
863 DEBUG(0,("idmap_tdb2_dump_data not supported\n"));
864 return NT_STATUS_NOT_SUPPORTED;
867 static struct idmap_methods db_methods = {
868 .init = idmap_tdb2_db_init,
869 .unixids_to_sids = idmap_tdb2_unixids_to_sids,
870 .sids_to_unixids = idmap_tdb2_sids_to_unixids,
871 .set_mapping = idmap_tdb2_set_mapping,
872 .remove_mapping = idmap_tdb2_remove_mapping,
873 .dump_data = idmap_tdb2_dump_data,
874 .close_fn = idmap_tdb2_close
877 static struct idmap_alloc_methods db_alloc_methods = {
878 .init = idmap_tdb2_alloc_init,
879 .allocate_id = idmap_tdb2_allocate_id,
880 .get_id_hwm = idmap_tdb2_get_hwm,
881 .set_id_hwm = idmap_tdb2_set_hwm,
882 .close_fn = idmap_tdb2_alloc_close
885 NTSTATUS idmap_tdb2_init(void)
887 NTSTATUS ret;
889 /* register both backends */
890 ret = smb_register_idmap_alloc(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_alloc_methods);
891 if (! NT_STATUS_IS_OK(ret)) {
892 DEBUG(0, ("Unable to register idmap alloc tdb2 module: %s\n", get_friendly_nt_error_msg(ret)));
893 return ret;
896 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_methods);