s3:idmap_tdb2: fix build of tdb2
[Samba.git] / source3 / winbindd / idmap_tdb2.c
blob1a5a51a5b443c6f37327327e5d3c94f797ee27e6
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
17 Copyright (C) Michael Adam 2009-2010
19 This program is free software; you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation; either version 2 of the License, or
22 (at your option) any later version.
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include "includes.h"
35 #include "system/filesys.h"
36 #include "winbindd.h"
37 #include "idmap.h"
38 #include "idmap_rw.h"
39 #include "dbwrap.h"
40 #include "../libcli/security/dom_sid.h"
42 #undef DBGC_CLASS
43 #define DBGC_CLASS DBGC_IDMAP
45 struct idmap_tdb2_context {
46 struct db_context *db;
47 const char *script; /* script to provide idmaps */
48 struct idmap_rw_ops *rw_ops;
51 /* High water mark keys */
52 #define HWM_GROUP "GROUP HWM"
53 #define HWM_USER "USER HWM"
57 * check and initialize high/low water marks in the db
59 static NTSTATUS idmap_tdb2_init_hwm(struct idmap_domain *dom)
61 NTSTATUS status;
62 uint32 low_id;
63 struct idmap_tdb2_context *ctx;
65 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
67 /* Create high water marks for group and user id */
69 low_id = dbwrap_fetch_int32(ctx->db, HWM_USER);
70 if ((low_id == -1) || (low_id < dom->low_id)) {
71 status = dbwrap_trans_store_int32(ctx->db, HWM_USER,
72 dom->low_id);
73 if (!NT_STATUS_IS_OK(status)) {
74 DEBUG(0, ("Unable to initialise user hwm in idmap "
75 "database: %s\n", nt_errstr(status)));
76 return NT_STATUS_INTERNAL_DB_ERROR;
80 low_id = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
81 if ((low_id == -1) || (low_id < dom->low_id)) {
82 status = dbwrap_trans_store_int32(ctx->db, HWM_GROUP,
83 dom->low_id);
84 if (!NT_STATUS_IS_OK(status)) {
85 DEBUG(0, ("Unable to initialise group hwm in idmap "
86 "database: %s\n", nt_errstr(status)));
87 return NT_STATUS_INTERNAL_DB_ERROR;
91 return NT_STATUS_OK;
96 open the permanent tdb
98 static NTSTATUS idmap_tdb2_open_db(struct idmap_domain *dom)
100 char *db_path;
101 struct idmap_tdb2_context *ctx;
103 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
105 if (ctx->db) {
106 /* its already open */
107 return NT_STATUS_OK;
110 db_path = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
111 if (db_path == NULL) {
112 /* fall back to the private directory, which, despite
113 its name, is usually on shared storage */
114 db_path = talloc_asprintf(NULL, "%s/idmap2.tdb", lp_private_dir());
116 NT_STATUS_HAVE_NO_MEMORY(db_path);
118 /* Open idmap repository */
119 ctx->db = db_open(ctx, db_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
120 TALLOC_FREE(db_path);
122 if (ctx->db == NULL) {
123 DEBUG(0, ("Unable to open idmap_tdb2 database '%s'\n",
124 db_path));
125 return NT_STATUS_UNSUCCESSFUL;
128 return idmap_tdb2_init_hwm(dom);
133 Allocate a new id.
136 struct idmap_tdb2_allocate_id_context {
137 const char *hwmkey;
138 const char *hwmtype;
139 uint32_t high_hwm;
140 uint32_t hwm;
143 static NTSTATUS idmap_tdb2_allocate_id_action(struct db_context *db,
144 void *private_data)
146 NTSTATUS ret;
147 struct idmap_tdb2_allocate_id_context *state;
148 uint32_t hwm;
150 state = (struct idmap_tdb2_allocate_id_context *)private_data;
152 hwm = dbwrap_fetch_int32(db, state->hwmkey);
153 if (hwm == -1) {
154 ret = NT_STATUS_INTERNAL_DB_ERROR;
155 goto done;
158 /* check it is in the range */
159 if (hwm > state->high_hwm) {
160 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
161 state->hwmtype, (unsigned long)state->high_hwm));
162 ret = NT_STATUS_UNSUCCESSFUL;
163 goto done;
166 /* fetch a new id and increment it */
167 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
168 if (!NT_STATUS_IS_OK(ret)) {
169 DEBUG(1, ("Fatal error while fetching a new %s value\n!",
170 state->hwmtype));
171 goto done;
174 /* recheck it is in the range */
175 if (hwm > state->high_hwm) {
176 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
177 state->hwmtype, (unsigned long)state->high_hwm));
178 ret = NT_STATUS_UNSUCCESSFUL;
179 goto done;
182 ret = NT_STATUS_OK;
183 state->hwm = hwm;
185 done:
186 return ret;
189 static NTSTATUS idmap_tdb2_allocate_id(struct idmap_domain *dom,
190 struct unixid *xid)
192 const char *hwmkey;
193 const char *hwmtype;
194 uint32_t high_hwm;
195 uint32_t hwm = 0;
196 NTSTATUS status;
197 struct idmap_tdb2_allocate_id_context state;
198 struct idmap_tdb2_context *ctx;
200 status = idmap_tdb2_open_db(dom);
201 NT_STATUS_NOT_OK_RETURN(status);
203 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
205 /* Get current high water mark */
206 switch (xid->type) {
208 case ID_TYPE_UID:
209 hwmkey = HWM_USER;
210 hwmtype = "UID";
211 break;
213 case ID_TYPE_GID:
214 hwmkey = HWM_GROUP;
215 hwmtype = "GID";
216 break;
218 default:
219 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
220 return NT_STATUS_INVALID_PARAMETER;
223 high_hwm = dom->high_id;
225 state.hwm = hwm;
226 state.high_hwm = high_hwm;
227 state.hwmtype = hwmtype;
228 state.hwmkey = hwmkey;
230 status = dbwrap_trans_do(ctx->db, idmap_tdb2_allocate_id_action,
231 &state);
233 if (NT_STATUS_IS_OK(status)) {
234 xid->id = state.hwm;
235 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
236 } else {
237 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
240 return status;
244 * Allocate a new unix-ID.
245 * For now this is for the default idmap domain only.
246 * Should be extended later on.
248 static NTSTATUS idmap_tdb2_get_new_id(struct idmap_domain *dom,
249 struct unixid *id)
251 NTSTATUS ret;
253 if (!strequal(dom->name, "*")) {
254 DEBUG(3, ("idmap_tdb2_get_new_id: "
255 "Refusing creation of mapping for domain'%s'. "
256 "Currently only supported for the default "
257 "domain \"*\".\n",
258 dom->name));
259 return NT_STATUS_NOT_IMPLEMENTED;
262 ret = idmap_tdb2_allocate_id(dom, id);
264 return ret;
268 IDMAP MAPPING TDB BACKEND
271 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom,
272 const struct id_map *map);
275 Initialise idmap database.
277 static NTSTATUS idmap_tdb2_db_init(struct idmap_domain *dom)
279 NTSTATUS ret;
280 struct idmap_tdb2_context *ctx;
282 ctx = talloc_zero(dom, struct idmap_tdb2_context);
283 if ( ! ctx) {
284 DEBUG(0, ("Out of memory!\n"));
285 return NT_STATUS_NO_MEMORY;
288 if (strequal(dom->name, "*")) {
289 ctx->script = lp_parm_const_string(-1, "idmap", "script", NULL);
290 if (ctx->script) {
291 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
293 } else {
294 char *config_option = NULL;
296 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
297 if ( ! config_option) {
298 DEBUG(0, ("Out of memory!\n"));
299 ret = NT_STATUS_NO_MEMORY;
300 goto failed;
303 ctx->script = lp_parm_const_string(-1, config_option, "script", NULL);
304 if (ctx->script) {
305 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
308 talloc_free(config_option);
311 ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
312 if (ctx->rw_ops == NULL) {
313 DEBUG(0, ("Out of memory!\n"));
314 ret = NT_STATUS_NO_MEMORY;
315 goto failed;
318 ctx->rw_ops->get_new_id = idmap_tdb2_get_new_id;
319 ctx->rw_ops->set_mapping = idmap_tdb2_set_mapping;
321 dom->private_data = ctx;
323 ret = idmap_tdb2_open_db(dom);
324 if (!NT_STATUS_IS_OK(ret)) {
325 goto failed;
328 return NT_STATUS_OK;
330 failed:
331 talloc_free(ctx);
332 return ret;
337 * store a mapping in the database.
340 struct idmap_tdb2_set_mapping_context {
341 const char *ksidstr;
342 const char *kidstr;
345 static NTSTATUS idmap_tdb2_set_mapping_action(struct db_context *db,
346 void *private_data)
348 TDB_DATA data;
349 NTSTATUS ret;
350 struct idmap_tdb2_set_mapping_context *state;
351 TALLOC_CTX *tmp_ctx = talloc_stackframe();
353 state = (struct idmap_tdb2_set_mapping_context *)private_data;
355 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
357 /* check wheter sid mapping is already present in db */
358 data = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr);
359 if (data.dptr) {
360 ret = NT_STATUS_OBJECT_NAME_COLLISION;
361 goto done;
364 ret = dbwrap_store_bystring(db, state->ksidstr,
365 string_term_tdb_data(state->kidstr),
366 TDB_INSERT);
367 if (!NT_STATUS_IS_OK(ret)) {
368 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
369 goto done;
372 ret = dbwrap_store_bystring(db, state->kidstr,
373 string_term_tdb_data(state->ksidstr),
374 TDB_INSERT);
375 if (!NT_STATUS_IS_OK(ret)) {
376 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
377 /* try to remove the previous stored SID -> ID map */
378 dbwrap_delete_bystring(db, state->ksidstr);
379 goto done;
382 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
384 done:
385 talloc_free(tmp_ctx);
386 return ret;
389 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom, const struct id_map *map)
391 struct idmap_tdb2_context *ctx;
392 NTSTATUS ret;
393 char *ksidstr, *kidstr;
394 struct idmap_tdb2_set_mapping_context state;
396 if (!map || !map->sid) {
397 return NT_STATUS_INVALID_PARAMETER;
400 ksidstr = kidstr = NULL;
402 /* TODO: should we filter a set_mapping using low/high filters ? */
404 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
406 switch (map->xid.type) {
408 case ID_TYPE_UID:
409 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
410 break;
412 case ID_TYPE_GID:
413 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
414 break;
416 default:
417 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
418 return NT_STATUS_INVALID_PARAMETER;
421 if (kidstr == NULL) {
422 DEBUG(0, ("ERROR: Out of memory!\n"));
423 ret = NT_STATUS_NO_MEMORY;
424 goto done;
427 ksidstr = sid_string_talloc(ctx, map->sid);
428 if (ksidstr == NULL) {
429 DEBUG(0, ("Out of memory!\n"));
430 ret = NT_STATUS_NO_MEMORY;
431 goto done;
434 state.ksidstr = ksidstr;
435 state.kidstr = kidstr;
437 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
438 &state);
440 done:
441 talloc_free(ksidstr);
442 talloc_free(kidstr);
443 return ret;
447 * Create a new mapping for an unmapped SID, also allocating a new ID.
448 * This should be run inside a transaction.
450 * TODO:
451 * Properly integrate this with multi domain idmap config:
452 * Currently, the allocator is default-config only.
454 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom, struct id_map *map)
456 NTSTATUS ret;
457 struct idmap_tdb2_context *ctx;
459 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
461 ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
463 return ret;
468 run a script to perform a mapping
470 The script should the following command lines:
472 SIDTOID S-1-xxxx
473 IDTOSID UID xxxx
474 IDTOSID GID xxxx
476 and should return one of the following as a single line of text
477 UID:xxxx
478 GID:xxxx
479 SID:xxxx
480 ERR:xxxx
482 static NTSTATUS idmap_tdb2_script(struct idmap_tdb2_context *ctx, struct id_map *map,
483 const char *fmt, ...)
485 va_list ap;
486 char *cmd;
487 FILE *p;
488 char line[64];
489 unsigned long v;
491 cmd = talloc_asprintf(ctx, "%s ", ctx->script);
492 NT_STATUS_HAVE_NO_MEMORY(cmd);
494 va_start(ap, fmt);
495 cmd = talloc_vasprintf_append(cmd, fmt, ap);
496 va_end(ap);
497 NT_STATUS_HAVE_NO_MEMORY(cmd);
499 p = popen(cmd, "r");
500 talloc_free(cmd);
501 if (p == NULL) {
502 return NT_STATUS_NONE_MAPPED;
505 if (fgets(line, sizeof(line)-1, p) == NULL) {
506 pclose(p);
507 return NT_STATUS_NONE_MAPPED;
509 pclose(p);
511 DEBUG(10,("idmap script gave: %s\n", line));
513 if (sscanf(line, "UID:%lu", &v) == 1) {
514 map->xid.id = v;
515 map->xid.type = ID_TYPE_UID;
516 } else if (sscanf(line, "GID:%lu", &v) == 1) {
517 map->xid.id = v;
518 map->xid.type = ID_TYPE_GID;
519 } else if (strncmp(line, "SID:S-", 6) == 0) {
520 if (!string_to_sid(map->sid, &line[4])) {
521 DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
522 line, ctx->script));
523 return NT_STATUS_NONE_MAPPED;
525 } else {
526 DEBUG(0,("Bad reply '%s' from idmap script %s\n",
527 line, ctx->script));
528 return NT_STATUS_NONE_MAPPED;
531 return NT_STATUS_OK;
537 Single id to sid lookup function.
539 static NTSTATUS idmap_tdb2_id_to_sid(struct idmap_domain *dom, struct id_map *map)
541 NTSTATUS ret;
542 TDB_DATA data;
543 char *keystr;
544 NTSTATUS status;
545 struct idmap_tdb2_context *ctx;
548 if (!dom || !map) {
549 return NT_STATUS_INVALID_PARAMETER;
552 status = idmap_tdb2_open_db(dom);
553 NT_STATUS_NOT_OK_RETURN(status);
555 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
557 /* apply filters before checking */
558 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
559 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
560 map->xid.id, dom->low_id, dom->high_id));
561 return NT_STATUS_NONE_MAPPED;
564 switch (map->xid.type) {
566 case ID_TYPE_UID:
567 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
568 break;
570 case ID_TYPE_GID:
571 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
572 break;
574 default:
575 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
576 return NT_STATUS_INVALID_PARAMETER;
579 /* final SAFE_FREE safe */
580 data.dptr = NULL;
582 if (keystr == NULL) {
583 DEBUG(0, ("Out of memory!\n"));
584 ret = NT_STATUS_NO_MEMORY;
585 goto done;
588 DEBUG(10,("Fetching record %s\n", keystr));
590 /* Check if the mapping exists */
591 data = dbwrap_fetch_bystring(ctx->db, keystr, keystr);
593 if (!data.dptr) {
594 char *sidstr;
595 struct idmap_tdb2_set_mapping_context store_state;
597 DEBUG(10,("Record %s not found\n", keystr));
598 if (ctx->script == NULL) {
599 ret = NT_STATUS_NONE_MAPPED;
600 goto done;
603 ret = idmap_tdb2_script(ctx, map, "IDTOSID %s", keystr);
605 /* store it on shared storage */
606 if (!NT_STATUS_IS_OK(ret)) {
607 goto done;
610 sidstr = sid_string_talloc(keystr, map->sid);
611 if (!sidstr) {
612 ret = NT_STATUS_NO_MEMORY;
613 goto done;
616 store_state.ksidstr = sidstr;
617 store_state.kidstr = keystr;
619 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
620 &store_state);
621 goto done;
624 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
625 DEBUG(10,("INVALID SID (%s) in record %s\n",
626 (const char *)data.dptr, keystr));
627 ret = NT_STATUS_INTERNAL_DB_ERROR;
628 goto done;
631 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
632 ret = NT_STATUS_OK;
634 done:
635 talloc_free(keystr);
636 return ret;
641 Single sid to id lookup function.
643 static NTSTATUS idmap_tdb2_sid_to_id(struct idmap_domain *dom, struct id_map *map)
645 NTSTATUS ret;
646 TDB_DATA data;
647 char *keystr;
648 unsigned long rec_id = 0;
649 struct idmap_tdb2_context *ctx;
650 TALLOC_CTX *tmp_ctx = talloc_stackframe();
652 ret = idmap_tdb2_open_db(dom);
653 NT_STATUS_NOT_OK_RETURN(ret);
655 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
657 keystr = sid_string_talloc(tmp_ctx, map->sid);
658 if (keystr == NULL) {
659 DEBUG(0, ("Out of memory!\n"));
660 ret = NT_STATUS_NO_MEMORY;
661 goto done;
664 DEBUG(10,("Fetching record %s\n", keystr));
666 /* Check if sid is present in database */
667 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
668 if (!data.dptr) {
669 char *idstr;
670 struct idmap_tdb2_set_mapping_context store_state;
672 DEBUG(10,(__location__ " Record %s not found\n", keystr));
674 if (ctx->script == NULL) {
675 ret = NT_STATUS_NONE_MAPPED;
676 goto done;
679 ret = idmap_tdb2_script(ctx, map, "SIDTOID %s", keystr);
680 /* store it on shared storage */
681 if (!NT_STATUS_IS_OK(ret)) {
682 goto done;
685 /* apply filters before returning result */
686 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
687 DEBUG(5, ("Script returned id (%u) out of range "
688 "(%u - %u). Filtered!\n",
689 map->xid.id, dom->low_id, dom->high_id));
690 ret = NT_STATUS_NONE_MAPPED;
691 goto done;
694 idstr = talloc_asprintf(tmp_ctx, "%cID %lu",
695 map->xid.type == ID_TYPE_UID?'U':'G',
696 (unsigned long)map->xid.id);
697 if (idstr == NULL) {
698 ret = NT_STATUS_NO_MEMORY;
699 goto done;
702 store_state.ksidstr = keystr;
703 store_state.kidstr = idstr;
705 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
706 &store_state);
707 goto done;
710 /* What type of record is this ? */
711 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
712 map->xid.id = rec_id;
713 map->xid.type = ID_TYPE_UID;
714 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
715 ret = NT_STATUS_OK;
717 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
718 map->xid.id = rec_id;
719 map->xid.type = ID_TYPE_GID;
720 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
721 ret = NT_STATUS_OK;
723 } else { /* Unknown record type ! */
724 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
725 ret = NT_STATUS_INTERNAL_DB_ERROR;
726 goto done;
729 /* apply filters before returning result */
730 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
731 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
732 map->xid.id, dom->low_id, dom->high_id));
733 ret = NT_STATUS_NONE_MAPPED;
736 done:
737 talloc_free(tmp_ctx);
738 return ret;
742 lookup a set of unix ids.
744 static NTSTATUS idmap_tdb2_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
746 NTSTATUS ret;
747 int i;
749 /* initialize the status to avoid suprise */
750 for (i = 0; ids[i]; i++) {
751 ids[i]->status = ID_UNKNOWN;
754 for (i = 0; ids[i]; i++) {
755 ret = idmap_tdb2_id_to_sid(dom, ids[i]);
756 if ( ! NT_STATUS_IS_OK(ret)) {
758 /* if it is just a failed mapping continue */
759 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
761 /* make sure it is marked as unmapped */
762 ids[i]->status = ID_UNMAPPED;
763 continue;
766 /* some fatal error occurred, return immediately */
767 goto done;
770 /* all ok, id is mapped */
771 ids[i]->status = ID_MAPPED;
774 ret = NT_STATUS_OK;
776 done:
777 return ret;
781 lookup a set of sids.
784 struct idmap_tdb2_sids_to_unixids_context {
785 struct idmap_domain *dom;
786 struct id_map **ids;
787 bool allocate_unmapped;
790 static NTSTATUS idmap_tdb2_sids_to_unixids_action(struct db_context *db,
791 void *private_data)
793 struct idmap_tdb2_sids_to_unixids_context *state;
794 int i;
795 NTSTATUS ret = NT_STATUS_OK;
797 state = (struct idmap_tdb2_sids_to_unixids_context *)private_data;
799 DEBUG(10, ("idmap_tdb2_sids_to_unixids_action: "
800 " domain: [%s], allocate: %s\n",
801 state->dom->name,
802 state->allocate_unmapped ? "yes" : "no"));
804 for (i = 0; state->ids[i]; i++) {
805 if ((state->ids[i]->status == ID_UNKNOWN) ||
806 /* retry if we could not map in previous run: */
807 (state->ids[i]->status == ID_UNMAPPED))
809 NTSTATUS ret2;
811 ret2 = idmap_tdb2_sid_to_id(state->dom, state->ids[i]);
812 if (!NT_STATUS_IS_OK(ret2)) {
814 /* if it is just a failed mapping, continue */
815 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
817 /* make sure it is marked as unmapped */
818 state->ids[i]->status = ID_UNMAPPED;
819 ret = STATUS_SOME_UNMAPPED;
820 } else {
821 /* some fatal error occurred, return immediately */
822 ret = ret2;
823 goto done;
825 } else {
826 /* all ok, id is mapped */
827 state->ids[i]->status = ID_MAPPED;
831 if ((state->ids[i]->status == ID_UNMAPPED) &&
832 state->allocate_unmapped)
834 ret = idmap_tdb2_new_mapping(state->dom, state->ids[i]);
835 if (!NT_STATUS_IS_OK(ret)) {
836 goto done;
841 done:
842 return ret;
845 static NTSTATUS idmap_tdb2_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
847 NTSTATUS ret;
848 int i;
849 struct idmap_tdb2_sids_to_unixids_context state;
850 struct idmap_tdb2_context *ctx;
852 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
854 /* initialize the status to avoid suprise */
855 for (i = 0; ids[i]; i++) {
856 ids[i]->status = ID_UNKNOWN;
859 state.dom = dom;
860 state.ids = ids;
861 state.allocate_unmapped = false;
863 ret = idmap_tdb2_sids_to_unixids_action(ctx->db, &state);
865 if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
866 state.allocate_unmapped = true;
867 ret = dbwrap_trans_do(ctx->db,
868 idmap_tdb2_sids_to_unixids_action,
869 &state);
872 return ret;
876 static struct idmap_methods db_methods = {
877 .init = idmap_tdb2_db_init,
878 .unixids_to_sids = idmap_tdb2_unixids_to_sids,
879 .sids_to_unixids = idmap_tdb2_sids_to_unixids,
880 .allocate_id = idmap_tdb2_get_new_id
883 NTSTATUS idmap_tdb2_init(void)
885 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_methods);