s3:idmap_tdb2: move idmap_tdb2_set_mapping() up to its _action callback.
[Samba/gbeck.git] / source3 / winbindd / idmap_tdb2.c
blob096e3a941144511389b6d75ea135879dc9ae32e7
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 struct idmap_tdb2_context {
40 struct db_context *db;
41 const char *script; /* script to provide idmaps */
44 /* High water mark keys */
45 #define HWM_GROUP "GROUP HWM"
46 #define HWM_USER "USER HWM"
49 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom,
50 struct id_map *map);
54 * check and initialize high/low water marks in the db
56 static NTSTATUS idmap_tdb2_init_hwm(struct idmap_domain *dom)
58 uint32 low_id;
59 struct idmap_tdb2_context *ctx;
61 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
63 /* Create high water marks for group and user id */
65 low_id = dbwrap_fetch_int32(ctx->db, HWM_USER);
66 if ((low_id == -1) || (low_id < dom->low_id)) {
67 if (!NT_STATUS_IS_OK(dbwrap_trans_store_int32(
68 ctx->db, HWM_USER,
69 dom->low_id))) {
70 DEBUG(0, ("Unable to initialise user hwm in idmap "
71 "database\n"));
72 return NT_STATUS_INTERNAL_DB_ERROR;
76 low_id = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
77 if ((low_id == -1) || (low_id < dom->low_id)) {
78 if (!NT_STATUS_IS_OK(dbwrap_trans_store_int32(
79 ctx->db, HWM_GROUP,
80 dom->low_id))) {
81 DEBUG(0, ("Unable to initialise group hwm in idmap "
82 "database\n"));
83 return NT_STATUS_INTERNAL_DB_ERROR;
87 return NT_STATUS_OK;
92 open the permanent tdb
94 static NTSTATUS idmap_tdb2_open_db(struct idmap_domain *dom)
96 char *db_path;
97 struct idmap_tdb2_context *ctx;
99 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
101 if (ctx->db) {
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 ctx->db = db_open(ctx, db_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
116 TALLOC_FREE(db_path);
118 if (ctx->db == NULL) {
119 DEBUG(0, ("Unable to open idmap_tdb2 database '%s'\n",
120 db_path));
121 return NT_STATUS_UNSUCCESSFUL;
124 return idmap_tdb2_init_hwm(dom);
129 Allocate a new id.
132 struct idmap_tdb2_allocate_id_context {
133 const char *hwmkey;
134 const char *hwmtype;
135 uint32_t high_hwm;
136 uint32_t hwm;
139 static NTSTATUS idmap_tdb2_allocate_id_action(struct db_context *db,
140 void *private_data)
142 NTSTATUS ret;
143 struct idmap_tdb2_allocate_id_context *state;
144 uint32_t hwm;
146 state = (struct idmap_tdb2_allocate_id_context *)private_data;
148 hwm = dbwrap_fetch_int32(db, state->hwmkey);
149 if (hwm == -1) {
150 ret = NT_STATUS_INTERNAL_DB_ERROR;
151 goto done;
154 /* check it is in the range */
155 if (hwm > state->high_hwm) {
156 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
157 state->hwmtype, (unsigned long)state->high_hwm));
158 ret = NT_STATUS_UNSUCCESSFUL;
159 goto done;
162 /* fetch a new id and increment it */
163 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
164 if (!NT_STATUS_IS_OK(ret)) {
165 DEBUG(1, ("Fatal error while fetching a new %s value\n!",
166 state->hwmtype));
167 goto done;
170 /* recheck it is in the range */
171 if (hwm > state->high_hwm) {
172 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
173 state->hwmtype, (unsigned long)state->high_hwm));
174 ret = NT_STATUS_UNSUCCESSFUL;
175 goto done;
178 ret = NT_STATUS_OK;
179 state->hwm = hwm;
181 done:
182 return ret;
185 static NTSTATUS idmap_tdb2_allocate_id(struct idmap_domain *dom,
186 struct unixid *xid)
188 const char *hwmkey;
189 const char *hwmtype;
190 uint32_t high_hwm;
191 uint32_t hwm = 0;
192 NTSTATUS status;
193 struct idmap_tdb2_allocate_id_context state;
194 struct idmap_tdb2_context *ctx;
196 status = idmap_tdb2_open_db(dom);
197 NT_STATUS_NOT_OK_RETURN(status);
199 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
201 /* Get current high water mark */
202 switch (xid->type) {
204 case ID_TYPE_UID:
205 hwmkey = HWM_USER;
206 hwmtype = "UID";
207 break;
209 case ID_TYPE_GID:
210 hwmkey = HWM_GROUP;
211 hwmtype = "GID";
212 break;
214 default:
215 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
216 return NT_STATUS_INVALID_PARAMETER;
219 high_hwm = dom->high_id;
221 state.hwm = hwm;
222 state.high_hwm = high_hwm;
223 state.hwmtype = hwmtype;
224 state.hwmkey = hwmkey;
226 status = dbwrap_trans_do(ctx->db, idmap_tdb2_allocate_id_action,
227 &state);
229 if (NT_STATUS_IS_OK(status)) {
230 xid->id = state.hwm;
231 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
232 } else {
233 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
236 return status;
240 * Allocate a new unix-ID.
241 * For now this is for the default idmap domain only.
242 * Should be extended later on.
244 static NTSTATUS idmap_tdb2_get_new_id(struct idmap_domain *dom,
245 struct unixid *id)
247 NTSTATUS ret;
249 if (!strequal(dom->name, "*")) {
250 DEBUG(3, ("idmap_tdb2_get_new_id: "
251 "Refusing creation of mapping for domain'%s'. "
252 "Currently only supported for the default "
253 "domain \"*\".\n",
254 dom->name));
255 return NT_STATUS_NOT_IMPLEMENTED;
258 ret = idmap_tdb2_allocate_id(dom, id);
260 return ret;
264 IDMAP MAPPING TDB BACKEND
268 Initialise idmap database.
270 static NTSTATUS idmap_tdb2_db_init(struct idmap_domain *dom,
271 const char *params)
273 NTSTATUS ret;
274 struct idmap_tdb2_context *ctx;
275 NTSTATUS status;
277 ctx = talloc_zero(dom, struct idmap_tdb2_context);
278 if ( ! ctx) {
279 DEBUG(0, ("Out of memory!\n"));
280 return NT_STATUS_NO_MEMORY;
283 if (strequal(dom->name, "*")) {
284 ctx->script = lp_parm_const_string(-1, "idmap", "script", NULL);
285 if (ctx->script) {
286 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
288 } else {
289 char *config_option = NULL;
291 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
292 if ( ! config_option) {
293 DEBUG(0, ("Out of memory!\n"));
294 ret = NT_STATUS_NO_MEMORY;
295 goto failed;
298 ctx->script = lp_parm_const_string(-1, config_option, "script", NULL);
299 if (ctx->script) {
300 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
303 talloc_free(config_option);
306 dom->private_data = ctx;
308 ret = idmap_tdb2_open_db(dom);
309 if (!NT_STATUS_IS_OK(ret)) {
310 goto failed;
313 return NT_STATUS_OK;
315 failed:
316 talloc_free(ctx);
317 return ret;
322 * store a mapping in the database.
325 struct idmap_tdb2_set_mapping_context {
326 const char *ksidstr;
327 const char *kidstr;
330 static NTSTATUS idmap_tdb2_set_mapping_action(struct db_context *db,
331 void *private_data)
333 TDB_DATA data;
334 NTSTATUS ret;
335 struct idmap_tdb2_set_mapping_context *state;
336 TALLOC_CTX *tmp_ctx = talloc_stackframe();
338 state = (struct idmap_tdb2_set_mapping_context *)private_data;
340 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
342 /* check wheter sid mapping is already present in db */
343 data = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr);
344 if (data.dptr) {
345 ret = NT_STATUS_OBJECT_NAME_COLLISION;
346 goto done;
349 ret = dbwrap_store_bystring(db, state->ksidstr,
350 string_term_tdb_data(state->kidstr),
351 TDB_INSERT);
352 if (!NT_STATUS_IS_OK(ret)) {
353 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
354 goto done;
357 ret = dbwrap_store_bystring(db, state->kidstr,
358 string_term_tdb_data(state->ksidstr),
359 TDB_INSERT);
360 if (!NT_STATUS_IS_OK(ret)) {
361 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
362 /* try to remove the previous stored SID -> ID map */
363 dbwrap_delete_bystring(db, state->ksidstr);
364 goto done;
367 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
369 done:
370 talloc_free(tmp_ctx);
371 return ret;
374 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom, const struct id_map *map)
376 struct idmap_tdb2_context *ctx;
377 NTSTATUS ret;
378 char *ksidstr, *kidstr;
379 struct idmap_tdb2_set_mapping_context state;
381 if (!map || !map->sid) {
382 return NT_STATUS_INVALID_PARAMETER;
385 ksidstr = kidstr = NULL;
387 /* TODO: should we filter a set_mapping using low/high filters ? */
389 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
391 switch (map->xid.type) {
393 case ID_TYPE_UID:
394 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
395 break;
397 case ID_TYPE_GID:
398 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
399 break;
401 default:
402 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
403 return NT_STATUS_INVALID_PARAMETER;
406 if (kidstr == NULL) {
407 DEBUG(0, ("ERROR: Out of memory!\n"));
408 ret = NT_STATUS_NO_MEMORY;
409 goto done;
412 ksidstr = sid_string_talloc(ctx, map->sid);
413 if (ksidstr == NULL) {
414 DEBUG(0, ("Out of memory!\n"));
415 ret = NT_STATUS_NO_MEMORY;
416 goto done;
419 state.ksidstr = ksidstr;
420 state.kidstr = kidstr;
422 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
423 &state);
425 done:
426 talloc_free(ksidstr);
427 talloc_free(kidstr);
428 return ret;
433 run a script to perform a mapping
435 The script should the following command lines:
437 SIDTOID S-1-xxxx
438 IDTOSID UID xxxx
439 IDTOSID GID xxxx
441 and should return one of the following as a single line of text
442 UID:xxxx
443 GID:xxxx
444 SID:xxxx
445 ERR:xxxx
447 static NTSTATUS idmap_tdb2_script(struct idmap_tdb2_context *ctx, struct id_map *map,
448 const char *fmt, ...)
450 va_list ap;
451 char *cmd;
452 FILE *p;
453 char line[64];
454 unsigned long v;
456 cmd = talloc_asprintf(ctx, "%s ", ctx->script);
457 NT_STATUS_HAVE_NO_MEMORY(cmd);
459 va_start(ap, fmt);
460 cmd = talloc_vasprintf_append(cmd, fmt, ap);
461 va_end(ap);
462 NT_STATUS_HAVE_NO_MEMORY(cmd);
464 p = popen(cmd, "r");
465 talloc_free(cmd);
466 if (p == NULL) {
467 return NT_STATUS_NONE_MAPPED;
470 if (fgets(line, sizeof(line)-1, p) == NULL) {
471 pclose(p);
472 return NT_STATUS_NONE_MAPPED;
474 pclose(p);
476 DEBUG(10,("idmap script gave: %s\n", line));
478 if (sscanf(line, "UID:%lu", &v) == 1) {
479 map->xid.id = v;
480 map->xid.type = ID_TYPE_UID;
481 } else if (sscanf(line, "GID:%lu", &v) == 1) {
482 map->xid.id = v;
483 map->xid.type = ID_TYPE_GID;
484 } else if (strncmp(line, "SID:S-", 6) == 0) {
485 if (!string_to_sid(map->sid, &line[4])) {
486 DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
487 line, ctx->script));
488 return NT_STATUS_NONE_MAPPED;
490 } else {
491 DEBUG(0,("Bad reply '%s' from idmap script %s\n",
492 line, ctx->script));
493 return NT_STATUS_NONE_MAPPED;
496 return NT_STATUS_OK;
502 Single id to sid lookup function.
504 static NTSTATUS idmap_tdb2_id_to_sid(struct idmap_domain *dom, struct id_map *map)
506 NTSTATUS ret;
507 TDB_DATA data;
508 char *keystr;
509 NTSTATUS status;
510 struct idmap_tdb2_context *ctx;
513 if (!dom || !map) {
514 return NT_STATUS_INVALID_PARAMETER;
517 status = idmap_tdb2_open_db(dom);
518 NT_STATUS_NOT_OK_RETURN(status);
520 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
522 /* apply filters before checking */
523 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
524 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
525 map->xid.id, dom->low_id, dom->high_id));
526 return NT_STATUS_NONE_MAPPED;
529 switch (map->xid.type) {
531 case ID_TYPE_UID:
532 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
533 break;
535 case ID_TYPE_GID:
536 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
537 break;
539 default:
540 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
541 return NT_STATUS_INVALID_PARAMETER;
544 /* final SAFE_FREE safe */
545 data.dptr = NULL;
547 if (keystr == NULL) {
548 DEBUG(0, ("Out of memory!\n"));
549 ret = NT_STATUS_NO_MEMORY;
550 goto done;
553 DEBUG(10,("Fetching record %s\n", keystr));
555 /* Check if the mapping exists */
556 data = dbwrap_fetch_bystring(ctx->db, keystr, keystr);
558 if (!data.dptr) {
559 char *sidstr;
560 struct idmap_tdb2_set_mapping_context store_state;
562 DEBUG(10,("Record %s not found\n", keystr));
563 if (ctx->script == NULL) {
564 ret = NT_STATUS_NONE_MAPPED;
565 goto done;
568 ret = idmap_tdb2_script(ctx, map, "IDTOSID %s", keystr);
570 /* store it on shared storage */
571 if (!NT_STATUS_IS_OK(ret)) {
572 goto done;
575 sidstr = sid_string_talloc(keystr, map->sid);
576 if (!sidstr) {
577 ret = NT_STATUS_NO_MEMORY;
578 goto done;
581 store_state.ksidstr = sidstr;
582 store_state.kidstr = keystr;
584 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
585 &store_state);
586 goto done;
589 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
590 DEBUG(10,("INVALID SID (%s) in record %s\n",
591 (const char *)data.dptr, keystr));
592 ret = NT_STATUS_INTERNAL_DB_ERROR;
593 goto done;
596 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
597 ret = NT_STATUS_OK;
599 done:
600 talloc_free(keystr);
601 return ret;
606 Single sid to id lookup function.
608 static NTSTATUS idmap_tdb2_sid_to_id(struct idmap_domain *dom, struct id_map *map)
610 NTSTATUS ret;
611 TDB_DATA data;
612 char *keystr;
613 unsigned long rec_id = 0;
614 struct idmap_tdb2_context *ctx;
615 TALLOC_CTX *tmp_ctx = talloc_stackframe();
617 ret = idmap_tdb2_open_db(dom);
618 NT_STATUS_NOT_OK_RETURN(ret);
620 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
622 keystr = sid_string_talloc(tmp_ctx, map->sid);
623 if (keystr == NULL) {
624 DEBUG(0, ("Out of memory!\n"));
625 ret = NT_STATUS_NO_MEMORY;
626 goto done;
629 DEBUG(10,("Fetching record %s\n", keystr));
631 /* Check if sid is present in database */
632 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
633 if (!data.dptr) {
634 char *idstr;
635 struct idmap_tdb2_set_mapping_context store_state;
637 DEBUG(10,(__location__ " Record %s not found\n", keystr));
639 if (ctx->script == NULL) {
640 ret = NT_STATUS_NONE_MAPPED;
641 goto done;
644 ret = idmap_tdb2_script(ctx, map, "SIDTOID %s", keystr);
645 /* store it on shared storage */
646 if (!NT_STATUS_IS_OK(ret)) {
647 goto done;
650 /* apply filters before returning result */
651 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
652 DEBUG(5, ("Script returned id (%u) out of range "
653 "(%u - %u). Filtered!\n",
654 map->xid.id, dom->low_id, dom->high_id));
655 ret = NT_STATUS_NONE_MAPPED;
656 goto done;
659 idstr = talloc_asprintf(tmp_ctx, "%cID %lu",
660 map->xid.type == ID_TYPE_UID?'U':'G',
661 (unsigned long)map->xid.id);
662 if (idstr == NULL) {
663 ret = NT_STATUS_NO_MEMORY;
664 goto done;
667 store_state.ksidstr = keystr;
668 store_state.kidstr = idstr;
670 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
671 &store_state);
672 goto done;
675 /* What type of record is this ? */
676 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
677 map->xid.id = rec_id;
678 map->xid.type = ID_TYPE_UID;
679 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
680 ret = NT_STATUS_OK;
682 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
683 map->xid.id = rec_id;
684 map->xid.type = ID_TYPE_GID;
685 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
686 ret = NT_STATUS_OK;
688 } else { /* Unknown record type ! */
689 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
690 ret = NT_STATUS_INTERNAL_DB_ERROR;
691 goto done;
694 /* apply filters before returning result */
695 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
696 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
697 map->xid.id, dom->low_id, dom->high_id));
698 ret = NT_STATUS_NONE_MAPPED;
701 done:
702 talloc_free(tmp_ctx);
703 return ret;
707 lookup a set of unix ids.
709 static NTSTATUS idmap_tdb2_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
711 NTSTATUS ret;
712 int i;
714 /* initialize the status to avoid suprise */
715 for (i = 0; ids[i]; i++) {
716 ids[i]->status = ID_UNKNOWN;
719 for (i = 0; ids[i]; i++) {
720 ret = idmap_tdb2_id_to_sid(dom, ids[i]);
721 if ( ! NT_STATUS_IS_OK(ret)) {
723 /* if it is just a failed mapping continue */
724 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
726 /* make sure it is marked as unmapped */
727 ids[i]->status = ID_UNMAPPED;
728 continue;
731 /* some fatal error occurred, return immediately */
732 goto done;
735 /* all ok, id is mapped */
736 ids[i]->status = ID_MAPPED;
739 ret = NT_STATUS_OK;
741 done:
742 return ret;
746 lookup a set of sids.
749 struct idmap_tdb2_sids_to_unixids_context {
750 struct idmap_domain *dom;
751 struct id_map **ids;
752 bool allocate_unmapped;
755 static NTSTATUS idmap_tdb2_sids_to_unixids_action(struct db_context *db,
756 void *private_data)
758 struct idmap_tdb2_sids_to_unixids_context *state;
759 int i;
760 NTSTATUS ret = NT_STATUS_OK;
762 state = (struct idmap_tdb2_sids_to_unixids_context *)private_data;
764 DEBUG(10, ("idmap_tdb2_sids_to_unixids_action: "
765 " domain: [%s], allocate: %s\n",
766 state->dom->name,
767 state->allocate_unmapped ? "yes" : "no"));
769 for (i = 0; state->ids[i]; i++) {
770 if ((state->ids[i]->status == ID_UNKNOWN) ||
771 /* retry if we could not map in previous run: */
772 (state->ids[i]->status == ID_UNMAPPED))
774 NTSTATUS ret2;
776 ret2 = idmap_tdb2_sid_to_id(state->dom, state->ids[i]);
777 if (!NT_STATUS_IS_OK(ret2)) {
779 /* if it is just a failed mapping, continue */
780 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
782 /* make sure it is marked as unmapped */
783 state->ids[i]->status = ID_UNMAPPED;
784 ret = STATUS_SOME_UNMAPPED;
785 } else {
786 /* some fatal error occurred, return immediately */
787 ret = ret2;
788 goto done;
790 } else {
791 /* all ok, id is mapped */
792 state->ids[i]->status = ID_MAPPED;
796 if ((state->ids[i]->status == ID_UNMAPPED) &&
797 state->allocate_unmapped)
799 ret = idmap_tdb2_new_mapping(state->dom, state->ids[i]);
800 if (!NT_STATUS_IS_OK(ret)) {
801 goto done;
806 done:
807 return ret;
810 static NTSTATUS idmap_tdb2_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
812 NTSTATUS ret;
813 int i;
814 struct idmap_tdb2_sids_to_unixids_context state;
815 struct idmap_tdb2_context *ctx;
817 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
819 /* initialize the status to avoid suprise */
820 for (i = 0; ids[i]; i++) {
821 ids[i]->status = ID_UNKNOWN;
824 state.dom = dom;
825 state.ids = ids;
826 state.allocate_unmapped = false;
828 ret = idmap_tdb2_sids_to_unixids_action(ctx->db, &state);
830 if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
831 state.allocate_unmapped = true;
832 ret = dbwrap_trans_do(ctx->db,
833 idmap_tdb2_sids_to_unixids_action,
834 &state);
837 return ret;
842 * Create a new mapping for an unmapped SID, also allocating a new ID.
843 * This should be run inside a transaction.
845 * TODO:
846 * Properly integrate this with multi domain idmap config:
847 * Currently, the allocator is default-config only.
849 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom, struct id_map *map)
851 NTSTATUS ret;
853 if (map == NULL) {
854 ret = NT_STATUS_INVALID_PARAMETER;
855 goto done;
858 if ((map->xid.type != ID_TYPE_UID) && (map->xid.type != ID_TYPE_GID)) {
859 ret = NT_STATUS_INVALID_PARAMETER;
860 goto done;
863 if (map->sid == NULL) {
864 ret = NT_STATUS_INVALID_PARAMETER;
865 goto done;
868 ret = idmap_tdb2_get_new_id(dom, &map->xid);
869 if (!NT_STATUS_IS_OK(ret)) {
870 DEBUG(3, ("Could not allocate id: %s\n", nt_errstr(ret)));
871 goto done;
874 DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
875 sid_string_dbg(map->sid),
876 (map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
877 (unsigned long)map->xid.id));
879 map->status = ID_MAPPED;
881 /* store the mapping */
882 ret = idmap_tdb2_set_mapping(dom, map);
883 if (!NT_STATUS_IS_OK(ret)) {
884 DEBUG(3, ("Could not store the new mapping: %s\n",
885 nt_errstr(ret)));
888 done:
889 return ret;
893 Close the idmap tdb instance
895 static NTSTATUS idmap_tdb2_close(struct idmap_domain *dom)
897 /* don't do anything */
898 return NT_STATUS_OK;
901 static struct idmap_methods db_methods = {
902 .init = idmap_tdb2_db_init,
903 .unixids_to_sids = idmap_tdb2_unixids_to_sids,
904 .sids_to_unixids = idmap_tdb2_sids_to_unixids,
905 .allocate_id = idmap_tdb2_get_new_id,
906 .close_fn = idmap_tdb2_close
909 NTSTATUS idmap_tdb2_init(void)
911 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_methods);