s3:idmap_tdb2: remove a legacy comment (cherry picked from commit 67cd2f9d867fad1f7df...
[Samba.git] / source3 / winbindd / idmap_tdb2.c
blob723afbc01ff16688b180afd20d7ac7212a4b06b1
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"
41 #include "util_tdb.h"
43 #undef DBGC_CLASS
44 #define DBGC_CLASS DBGC_IDMAP
46 struct idmap_tdb2_context {
47 struct db_context *db;
48 const char *script; /* script to provide idmaps */
49 struct idmap_rw_ops *rw_ops;
52 /* High water mark keys */
53 #define HWM_GROUP "GROUP HWM"
54 #define HWM_USER "USER HWM"
58 * check and initialize high/low water marks in the db
60 static NTSTATUS idmap_tdb2_init_hwm(struct idmap_domain *dom)
62 NTSTATUS status;
63 uint32 low_id;
64 struct idmap_tdb2_context *ctx;
66 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
68 /* Create high water marks for group and user id */
70 low_id = dbwrap_fetch_int32(ctx->db, HWM_USER);
71 if ((low_id == -1) || (low_id < dom->low_id)) {
72 status = dbwrap_trans_store_int32(ctx->db, HWM_USER,
73 dom->low_id);
74 if (!NT_STATUS_IS_OK(status)) {
75 DEBUG(0, ("Unable to initialise user hwm in idmap "
76 "database: %s\n", nt_errstr(status)));
77 return NT_STATUS_INTERNAL_DB_ERROR;
81 low_id = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
82 if ((low_id == -1) || (low_id < dom->low_id)) {
83 status = dbwrap_trans_store_int32(ctx->db, HWM_GROUP,
84 dom->low_id);
85 if (!NT_STATUS_IS_OK(status)) {
86 DEBUG(0, ("Unable to initialise group hwm in idmap "
87 "database: %s\n", nt_errstr(status)));
88 return NT_STATUS_INTERNAL_DB_ERROR;
92 return NT_STATUS_OK;
97 open the permanent tdb
99 static NTSTATUS idmap_tdb2_open_db(struct idmap_domain *dom)
101 char *db_path;
102 struct idmap_tdb2_context *ctx;
104 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
106 if (ctx->db) {
107 /* its already open */
108 return NT_STATUS_OK;
111 db_path = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
112 if (db_path == NULL) {
113 /* fall back to the private directory */
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;
281 char *config_option = NULL;
282 const char * idmap_script = NULL;
284 ctx = talloc_zero(dom, struct idmap_tdb2_context);
285 if ( ! ctx) {
286 DEBUG(0, ("Out of memory!\n"));
287 return NT_STATUS_NO_MEMORY;
290 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
291 if (config_option == NULL) {
292 DEBUG(0, ("Out of memory!\n"));
293 ret = NT_STATUS_NO_MEMORY;
294 goto failed;
296 ctx->script = lp_parm_const_string(-1, config_option, "script", "NULL");
297 talloc_free(config_option);
299 idmap_script = lp_parm_const_string(-1, "idmap", "script", NULL);
300 if (idmap_script != NULL) {
301 DEBUG(0, ("Warning: 'idmap:script' is deprecated. "
302 " Please use 'idmap config * : script' instead!\n"));
305 if (strequal(dom->name, "*") && ctx->script == NULL) {
306 /* fall back to idmap:script for backwards compatibility */
307 ctx->script = idmap_script;
310 if (ctx->script) {
311 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
314 ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
315 if (ctx->rw_ops == NULL) {
316 DEBUG(0, ("Out of memory!\n"));
317 ret = NT_STATUS_NO_MEMORY;
318 goto failed;
321 ctx->rw_ops->get_new_id = idmap_tdb2_get_new_id;
322 ctx->rw_ops->set_mapping = idmap_tdb2_set_mapping;
324 dom->private_data = ctx;
326 ret = idmap_tdb2_open_db(dom);
327 if (!NT_STATUS_IS_OK(ret)) {
328 goto failed;
331 return NT_STATUS_OK;
333 failed:
334 talloc_free(ctx);
335 return ret;
340 * store a mapping in the database.
343 struct idmap_tdb2_set_mapping_context {
344 const char *ksidstr;
345 const char *kidstr;
348 static NTSTATUS idmap_tdb2_set_mapping_action(struct db_context *db,
349 void *private_data)
351 TDB_DATA data;
352 NTSTATUS ret;
353 struct idmap_tdb2_set_mapping_context *state;
354 TALLOC_CTX *tmp_ctx = talloc_stackframe();
356 state = (struct idmap_tdb2_set_mapping_context *)private_data;
358 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
360 /* check wheter sid mapping is already present in db */
361 data = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr);
362 if (data.dptr) {
363 ret = NT_STATUS_OBJECT_NAME_COLLISION;
364 goto done;
367 ret = dbwrap_store_bystring(db, state->ksidstr,
368 string_term_tdb_data(state->kidstr),
369 TDB_INSERT);
370 if (!NT_STATUS_IS_OK(ret)) {
371 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
372 goto done;
375 ret = dbwrap_store_bystring(db, state->kidstr,
376 string_term_tdb_data(state->ksidstr),
377 TDB_INSERT);
378 if (!NT_STATUS_IS_OK(ret)) {
379 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
380 /* try to remove the previous stored SID -> ID map */
381 dbwrap_delete_bystring(db, state->ksidstr);
382 goto done;
385 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
387 done:
388 talloc_free(tmp_ctx);
389 return ret;
392 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom, const struct id_map *map)
394 struct idmap_tdb2_context *ctx;
395 NTSTATUS ret;
396 char *ksidstr, *kidstr;
397 struct idmap_tdb2_set_mapping_context state;
399 if (!map || !map->sid) {
400 return NT_STATUS_INVALID_PARAMETER;
403 ksidstr = kidstr = NULL;
405 /* TODO: should we filter a set_mapping using low/high filters ? */
407 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
409 switch (map->xid.type) {
411 case ID_TYPE_UID:
412 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
413 break;
415 case ID_TYPE_GID:
416 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
417 break;
419 default:
420 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
421 return NT_STATUS_INVALID_PARAMETER;
424 if (kidstr == NULL) {
425 DEBUG(0, ("ERROR: Out of memory!\n"));
426 ret = NT_STATUS_NO_MEMORY;
427 goto done;
430 ksidstr = sid_string_talloc(ctx, map->sid);
431 if (ksidstr == NULL) {
432 DEBUG(0, ("Out of memory!\n"));
433 ret = NT_STATUS_NO_MEMORY;
434 goto done;
437 state.ksidstr = ksidstr;
438 state.kidstr = kidstr;
440 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
441 &state);
443 done:
444 talloc_free(ksidstr);
445 talloc_free(kidstr);
446 return ret;
450 * Create a new mapping for an unmapped SID, also allocating a new ID.
451 * This should be run inside a transaction.
453 * TODO:
454 * Properly integrate this with multi domain idmap config:
455 * Currently, the allocator is default-config only.
457 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom, struct id_map *map)
459 NTSTATUS ret;
460 struct idmap_tdb2_context *ctx;
462 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
464 ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
466 return ret;
471 run a script to perform a mapping
473 The script should the following command lines:
475 SIDTOID S-1-xxxx
476 IDTOSID UID xxxx
477 IDTOSID GID xxxx
479 and should return one of the following as a single line of text
480 UID:xxxx
481 GID:xxxx
482 SID:xxxx
483 ERR:xxxx
485 static NTSTATUS idmap_tdb2_script(struct idmap_tdb2_context *ctx, struct id_map *map,
486 const char *fmt, ...)
488 va_list ap;
489 char *cmd;
490 FILE *p;
491 char line[64];
492 unsigned long v;
494 cmd = talloc_asprintf(ctx, "%s ", ctx->script);
495 NT_STATUS_HAVE_NO_MEMORY(cmd);
497 va_start(ap, fmt);
498 cmd = talloc_vasprintf_append(cmd, fmt, ap);
499 va_end(ap);
500 NT_STATUS_HAVE_NO_MEMORY(cmd);
502 p = popen(cmd, "r");
503 talloc_free(cmd);
504 if (p == NULL) {
505 return NT_STATUS_NONE_MAPPED;
508 if (fgets(line, sizeof(line)-1, p) == NULL) {
509 pclose(p);
510 return NT_STATUS_NONE_MAPPED;
512 pclose(p);
514 DEBUG(10,("idmap script gave: %s\n", line));
516 if (sscanf(line, "UID:%lu", &v) == 1) {
517 map->xid.id = v;
518 map->xid.type = ID_TYPE_UID;
519 } else if (sscanf(line, "GID:%lu", &v) == 1) {
520 map->xid.id = v;
521 map->xid.type = ID_TYPE_GID;
522 } else if (strncmp(line, "SID:S-", 6) == 0) {
523 if (!string_to_sid(map->sid, &line[4])) {
524 DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
525 line, ctx->script));
526 return NT_STATUS_NONE_MAPPED;
528 } else {
529 DEBUG(0,("Bad reply '%s' from idmap script %s\n",
530 line, ctx->script));
531 return NT_STATUS_NONE_MAPPED;
534 return NT_STATUS_OK;
540 Single id to sid lookup function.
542 static NTSTATUS idmap_tdb2_id_to_sid(struct idmap_domain *dom, struct id_map *map)
544 NTSTATUS ret;
545 TDB_DATA data;
546 char *keystr;
547 NTSTATUS status;
548 struct idmap_tdb2_context *ctx;
551 if (!dom || !map) {
552 return NT_STATUS_INVALID_PARAMETER;
555 status = idmap_tdb2_open_db(dom);
556 NT_STATUS_NOT_OK_RETURN(status);
558 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
560 /* apply filters before checking */
561 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
562 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
563 map->xid.id, dom->low_id, dom->high_id));
564 return NT_STATUS_NONE_MAPPED;
567 switch (map->xid.type) {
569 case ID_TYPE_UID:
570 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
571 break;
573 case ID_TYPE_GID:
574 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
575 break;
577 default:
578 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
579 return NT_STATUS_INVALID_PARAMETER;
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);
604 if (!NT_STATUS_IS_OK(ret)) {
605 goto done;
608 sidstr = sid_string_talloc(keystr, map->sid);
609 if (!sidstr) {
610 ret = NT_STATUS_NO_MEMORY;
611 goto done;
614 store_state.ksidstr = sidstr;
615 store_state.kidstr = keystr;
617 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
618 &store_state);
619 goto done;
622 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
623 DEBUG(10,("INVALID SID (%s) in record %s\n",
624 (const char *)data.dptr, keystr));
625 ret = NT_STATUS_INTERNAL_DB_ERROR;
626 goto done;
629 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
630 ret = NT_STATUS_OK;
632 done:
633 talloc_free(keystr);
634 return ret;
639 Single sid to id lookup function.
641 static NTSTATUS idmap_tdb2_sid_to_id(struct idmap_domain *dom, struct id_map *map)
643 NTSTATUS ret;
644 TDB_DATA data;
645 char *keystr;
646 unsigned long rec_id = 0;
647 struct idmap_tdb2_context *ctx;
648 TALLOC_CTX *tmp_ctx = talloc_stackframe();
650 ret = idmap_tdb2_open_db(dom);
651 NT_STATUS_NOT_OK_RETURN(ret);
653 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
655 keystr = sid_string_talloc(tmp_ctx, map->sid);
656 if (keystr == NULL) {
657 DEBUG(0, ("Out of memory!\n"));
658 ret = NT_STATUS_NO_MEMORY;
659 goto done;
662 DEBUG(10,("Fetching record %s\n", keystr));
664 /* Check if sid is present in database */
665 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
666 if (!data.dptr) {
667 char *idstr;
668 struct idmap_tdb2_set_mapping_context store_state;
670 DEBUG(10,(__location__ " Record %s not found\n", keystr));
672 if (ctx->script == NULL) {
673 ret = NT_STATUS_NONE_MAPPED;
674 goto done;
677 ret = idmap_tdb2_script(ctx, map, "SIDTOID %s", keystr);
678 if (!NT_STATUS_IS_OK(ret)) {
679 goto done;
682 /* apply filters before returning result */
683 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
684 DEBUG(5, ("Script returned id (%u) out of range "
685 "(%u - %u). Filtered!\n",
686 map->xid.id, dom->low_id, dom->high_id));
687 ret = NT_STATUS_NONE_MAPPED;
688 goto done;
691 idstr = talloc_asprintf(tmp_ctx, "%cID %lu",
692 map->xid.type == ID_TYPE_UID?'U':'G',
693 (unsigned long)map->xid.id);
694 if (idstr == NULL) {
695 ret = NT_STATUS_NO_MEMORY;
696 goto done;
699 store_state.ksidstr = keystr;
700 store_state.kidstr = idstr;
702 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
703 &store_state);
704 goto done;
707 /* What type of record is this ? */
708 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
709 map->xid.id = rec_id;
710 map->xid.type = ID_TYPE_UID;
711 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
712 ret = NT_STATUS_OK;
714 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
715 map->xid.id = rec_id;
716 map->xid.type = ID_TYPE_GID;
717 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
718 ret = NT_STATUS_OK;
720 } else { /* Unknown record type ! */
721 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
722 ret = NT_STATUS_INTERNAL_DB_ERROR;
723 goto done;
726 /* apply filters before returning result */
727 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
728 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
729 map->xid.id, dom->low_id, dom->high_id));
730 ret = NT_STATUS_NONE_MAPPED;
733 done:
734 talloc_free(tmp_ctx);
735 return ret;
739 lookup a set of unix ids.
741 static NTSTATUS idmap_tdb2_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
743 NTSTATUS ret;
744 int i;
746 /* initialize the status to avoid suprise */
747 for (i = 0; ids[i]; i++) {
748 ids[i]->status = ID_UNKNOWN;
751 for (i = 0; ids[i]; i++) {
752 ret = idmap_tdb2_id_to_sid(dom, ids[i]);
753 if ( ! NT_STATUS_IS_OK(ret)) {
755 /* if it is just a failed mapping continue */
756 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
758 /* make sure it is marked as unmapped */
759 ids[i]->status = ID_UNMAPPED;
760 continue;
763 /* some fatal error occurred, return immediately */
764 goto done;
767 /* all ok, id is mapped */
768 ids[i]->status = ID_MAPPED;
771 ret = NT_STATUS_OK;
773 done:
774 return ret;
778 lookup a set of sids.
781 struct idmap_tdb2_sids_to_unixids_context {
782 struct idmap_domain *dom;
783 struct id_map **ids;
784 bool allocate_unmapped;
787 static NTSTATUS idmap_tdb2_sids_to_unixids_action(struct db_context *db,
788 void *private_data)
790 struct idmap_tdb2_sids_to_unixids_context *state;
791 int i;
792 NTSTATUS ret = NT_STATUS_OK;
794 state = (struct idmap_tdb2_sids_to_unixids_context *)private_data;
796 DEBUG(10, ("idmap_tdb2_sids_to_unixids_action: "
797 " domain: [%s], allocate: %s\n",
798 state->dom->name,
799 state->allocate_unmapped ? "yes" : "no"));
801 for (i = 0; state->ids[i]; i++) {
802 if ((state->ids[i]->status == ID_UNKNOWN) ||
803 /* retry if we could not map in previous run: */
804 (state->ids[i]->status == ID_UNMAPPED))
806 NTSTATUS ret2;
808 ret2 = idmap_tdb2_sid_to_id(state->dom, state->ids[i]);
809 if (!NT_STATUS_IS_OK(ret2)) {
811 /* if it is just a failed mapping, continue */
812 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
814 /* make sure it is marked as unmapped */
815 state->ids[i]->status = ID_UNMAPPED;
816 ret = STATUS_SOME_UNMAPPED;
817 } else {
818 /* some fatal error occurred, return immediately */
819 ret = ret2;
820 goto done;
822 } else {
823 /* all ok, id is mapped */
824 state->ids[i]->status = ID_MAPPED;
828 if ((state->ids[i]->status == ID_UNMAPPED) &&
829 state->allocate_unmapped)
831 ret = idmap_tdb2_new_mapping(state->dom, state->ids[i]);
832 if (!NT_STATUS_IS_OK(ret)) {
833 goto done;
838 done:
839 return ret;
842 static NTSTATUS idmap_tdb2_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
844 NTSTATUS ret;
845 int i;
846 struct idmap_tdb2_sids_to_unixids_context state;
847 struct idmap_tdb2_context *ctx;
849 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
851 /* initialize the status to avoid suprise */
852 for (i = 0; ids[i]; i++) {
853 ids[i]->status = ID_UNKNOWN;
856 state.dom = dom;
857 state.ids = ids;
858 state.allocate_unmapped = false;
860 ret = idmap_tdb2_sids_to_unixids_action(ctx->db, &state);
862 if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
863 state.allocate_unmapped = true;
864 ret = dbwrap_trans_do(ctx->db,
865 idmap_tdb2_sids_to_unixids_action,
866 &state);
869 return ret;
873 static struct idmap_methods db_methods = {
874 .init = idmap_tdb2_db_init,
875 .unixids_to_sids = idmap_tdb2_unixids_to_sids,
876 .sids_to_unixids = idmap_tdb2_sids_to_unixids,
877 .allocate_id = idmap_tdb2_get_new_id
880 NTSTATUS idmap_tdb2_init(void)
882 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_methods);