2 Unix SMB/CIFS implementation.
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
8 Copyright (C) Jeremy Allison 2006
9 Copyright (C) Simo Sorce 2003-2006
10 Copyright (C) Michael Adam 2009-2010
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "system/filesys.h"
31 #include "dbwrap/dbwrap.h"
32 #include "dbwrap/dbwrap_open.h"
33 #include "../libcli/security/security.h"
37 #define DBGC_CLASS DBGC_IDMAP
39 /* idmap version determines auto-conversion - this is the database
40 structure version specifier. */
42 #define IDMAP_VERSION 2
44 struct idmap_tdb_context
{
45 struct db_context
*db
;
46 struct idmap_rw_ops
*rw_ops
;
49 /* High water mark keys */
50 #define HWM_GROUP "GROUP HWM"
51 #define HWM_USER "USER HWM"
53 struct convert_fn_state
{
54 struct db_context
*db
;
58 /*****************************************************************************
59 For idmap conversion: convert one record to new format
60 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
62 *****************************************************************************/
63 static int convert_fn(struct db_record
*rec
, void *private_data
)
65 struct winbindd_domain
*domain
;
75 struct convert_fn_state
*s
= (struct convert_fn_state
*)private_data
;
77 key
= dbwrap_record_get_key(rec
);
79 DEBUG(10,("Converting %s\n", (const char *)key
.dptr
));
81 p
= strchr((const char *)key
.dptr
, '/');
86 fstrcpy(dom_name
, (const char *)key
.dptr
);
89 domain
= find_domain_from_name(dom_name
);
91 /* We must delete the old record. */
92 DEBUG(0,("Unable to find domain %s\n", dom_name
));
93 DEBUG(0,("deleting record %s\n", (const char *)key
.dptr
));
95 status
= dbwrap_record_delete(rec
);
96 if (!NT_STATUS_IS_OK(status
)) {
97 DEBUG(0, ("Unable to delete record %s:%s\n",
98 (const char *)key
.dptr
,
109 sid_compose(&sid
, &domain
->sid
, rid
);
111 sid_to_fstring(keystr
, &sid
);
112 key2
= string_term_tdb_data(keystr
);
114 value
= dbwrap_record_get_value(rec
);
116 status
= dbwrap_store(s
->db
, key2
, value
, TDB_INSERT
);
117 if (!NT_STATUS_IS_OK(status
)) {
118 DEBUG(0,("Unable to add record %s:%s\n",
119 (const char *)key2
.dptr
,
125 status
= dbwrap_store(s
->db
, value
, key2
, TDB_REPLACE
);
126 if (!NT_STATUS_IS_OK(status
)) {
127 DEBUG(0,("Unable to update record %s:%s\n",
128 (const char *)value
.dptr
,
134 status
= dbwrap_record_delete(rec
);
135 if (!NT_STATUS_IS_OK(status
)) {
136 DEBUG(0,("Unable to delete record %s:%s\n",
137 (const char *)key
.dptr
,
146 /*****************************************************************************
147 Convert the idmap database from an older version.
148 *****************************************************************************/
150 static bool idmap_tdb_upgrade(struct idmap_domain
*dom
, struct db_context
*db
)
153 bool bigendianheader
;
154 struct convert_fn_state s
;
158 /* If we are bigendian, tdb is bigendian if NOT converted. */
161 unsigned char small
[2];
164 if (u
.small
[0] == 0x01)
165 bigendianheader
= !(dbwrap_get_flags(db
) & TDB_CONVERT
);
167 assert(u
.small
[0] == 0x02);
168 bigendianheader
= (dbwrap_get_flags(db
) & TDB_CONVERT
);
171 bigendianheader
= (dbwrap_get_flags(db
) & TDB_BIGENDIAN
) ? True
: False
;
173 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
175 status
= dbwrap_fetch_int32(db
, "IDMAP_VERSION", &vers
);
176 if (!NT_STATUS_IS_OK(status
)) {
180 if (((vers
== -1) && bigendianheader
) || (IREV(vers
) == IDMAP_VERSION
)) {
181 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
183 * high and low records were created on a
184 * big endian machine and will need byte-reversing.
189 status
= dbwrap_fetch_int32(db
, HWM_USER
, &wm
);
190 if (!NT_STATUS_IS_OK(status
)) {
200 status
= dbwrap_store_int32(db
, HWM_USER
, wm
);
201 if (!NT_STATUS_IS_OK(status
)) {
202 DEBUG(0, ("Unable to byteswap user hwm in idmap "
203 "database: %s\n", nt_errstr(status
)));
207 status
= dbwrap_fetch_int32(db
, HWM_GROUP
, &wm
);
208 if (!NT_STATUS_IS_OK(status
)) {
218 status
= dbwrap_store_int32(db
, HWM_GROUP
, wm
);
219 if (!NT_STATUS_IS_OK(status
)) {
220 DEBUG(0, ("Unable to byteswap group hwm in idmap "
221 "database: %s\n", nt_errstr(status
)));
229 /* the old format stored as DOMAIN/rid - now we store the SID direct */
230 status
= dbwrap_traverse(db
, convert_fn
, &s
, NULL
);
232 if (!NT_STATUS_IS_OK(status
)) {
233 DEBUG(0, ("Database traverse failed during conversion\n"));
238 DEBUG(0, ("Problem during conversion\n"));
242 status
= dbwrap_store_int32(db
, "IDMAP_VERSION", IDMAP_VERSION
);
243 if (!NT_STATUS_IS_OK(status
)) {
244 DEBUG(0, ("Unable to store idmap version in database: %s\n",
252 static NTSTATUS
idmap_tdb_init_hwm(struct idmap_domain
*dom
)
256 bool update_uid
= false;
257 bool update_gid
= false;
258 struct idmap_tdb_context
*ctx
;
261 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
263 status
= dbwrap_fetch_uint32(ctx
->db
, HWM_USER
, &low_uid
);
264 if (!NT_STATUS_IS_OK(status
) || low_uid
< dom
->low_id
) {
268 status
= dbwrap_fetch_uint32(ctx
->db
, HWM_GROUP
, &low_gid
);
269 if (!NT_STATUS_IS_OK(status
) || low_gid
< dom
->low_id
) {
273 if (!update_uid
&& !update_gid
) {
277 if (dbwrap_transaction_start(ctx
->db
) != 0) {
278 DEBUG(0, ("Unable to start upgrade transaction!\n"));
279 return NT_STATUS_INTERNAL_DB_ERROR
;
283 status
= dbwrap_store_uint32(ctx
->db
, HWM_USER
, dom
->low_id
);
284 if (!NT_STATUS_IS_OK(status
)) {
285 dbwrap_transaction_cancel(ctx
->db
);
286 DEBUG(0, ("Unable to initialise user hwm in idmap "
287 "database: %s\n", nt_errstr(status
)));
288 return NT_STATUS_INTERNAL_DB_ERROR
;
293 status
= dbwrap_store_uint32(ctx
->db
, HWM_GROUP
, dom
->low_id
);
294 if (!NT_STATUS_IS_OK(status
)) {
295 dbwrap_transaction_cancel(ctx
->db
);
296 DEBUG(0, ("Unable to initialise group hwm in idmap "
297 "database: %s\n", nt_errstr(status
)));
298 return NT_STATUS_INTERNAL_DB_ERROR
;
302 if (dbwrap_transaction_commit(ctx
->db
) != 0) {
303 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
304 return NT_STATUS_INTERNAL_DB_ERROR
;
310 static NTSTATUS
idmap_tdb_open_db(struct idmap_domain
*dom
)
314 char *tdbfile
= NULL
;
315 struct db_context
*db
= NULL
;
317 bool config_error
= false;
318 struct idmap_tdb_context
*ctx
;
320 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
323 /* it is already open */
327 /* use our own context here */
328 mem_ctx
= talloc_stackframe();
330 /* use the old database if present */
331 tdbfile
= state_path("winbindd_idmap.tdb");
333 DEBUG(0, ("Out of memory!\n"));
334 ret
= NT_STATUS_NO_MEMORY
;
338 DEBUG(10,("Opening tdbfile %s\n", tdbfile
));
340 /* Open idmap repository */
341 db
= db_open(mem_ctx
, tdbfile
, 0, TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0644,
342 DBWRAP_LOCK_ORDER_1
);
344 DEBUG(0, ("Unable to open idmap database\n"));
345 ret
= NT_STATUS_UNSUCCESSFUL
;
349 /* check against earlier versions */
350 ret
= dbwrap_fetch_int32(db
, "IDMAP_VERSION", &version
);
351 if (!NT_STATUS_IS_OK(ret
)) {
355 if (version
!= IDMAP_VERSION
) {
357 DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
358 "possible with incomplete configuration\n",
359 version
, IDMAP_VERSION
));
360 ret
= NT_STATUS_UNSUCCESSFUL
;
363 if (dbwrap_transaction_start(db
) != 0) {
364 DEBUG(0, ("Unable to start upgrade transaction!\n"));
365 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
369 if (!idmap_tdb_upgrade(dom
, db
)) {
370 dbwrap_transaction_cancel(db
);
371 DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
372 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
376 if (dbwrap_transaction_commit(db
) != 0) {
377 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
378 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
383 ctx
->db
= talloc_move(ctx
, &db
);
385 ret
= idmap_tdb_init_hwm(dom
);
388 talloc_free(mem_ctx
);
392 /**********************************************************************
393 IDMAP ALLOC TDB BACKEND
394 **********************************************************************/
396 /**********************************
398 **********************************/
400 struct idmap_tdb_allocate_id_context
{
407 static NTSTATUS
idmap_tdb_allocate_id_action(struct db_context
*db
,
411 struct idmap_tdb_allocate_id_context
*state
;
414 state
= (struct idmap_tdb_allocate_id_context
*)private_data
;
416 ret
= dbwrap_fetch_uint32(db
, state
->hwmkey
, &hwm
);
417 if (!NT_STATUS_IS_OK(ret
)) {
418 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
422 /* check it is in the range */
423 if (hwm
> state
->high_hwm
) {
424 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
425 state
->hwmtype
, (unsigned long)state
->high_hwm
));
426 ret
= NT_STATUS_UNSUCCESSFUL
;
430 /* fetch a new id and increment it */
431 ret
= dbwrap_trans_change_uint32_atomic(db
, state
->hwmkey
, &hwm
, 1);
432 if (!NT_STATUS_IS_OK(ret
)) {
433 DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
434 state
->hwmtype
, nt_errstr(ret
)));
438 /* recheck it is in the range */
439 if (hwm
> state
->high_hwm
) {
440 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
441 state
->hwmtype
, (unsigned long)state
->high_hwm
));
442 ret
= NT_STATUS_UNSUCCESSFUL
;
453 static NTSTATUS
idmap_tdb_allocate_id(struct idmap_domain
*dom
,
461 struct idmap_tdb_allocate_id_context state
;
462 struct idmap_tdb_context
*ctx
;
464 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
466 /* Get current high water mark */
480 DEBUG(2, ("Invalid ID type (0x%x)\n", xid
->type
));
481 return NT_STATUS_INVALID_PARAMETER
;
484 high_hwm
= dom
->high_id
;
487 state
.high_hwm
= high_hwm
;
488 state
.hwmtype
= hwmtype
;
489 state
.hwmkey
= hwmkey
;
491 status
= dbwrap_trans_do(ctx
->db
, idmap_tdb_allocate_id_action
,
494 if (NT_STATUS_IS_OK(status
)) {
496 DEBUG(10,("New %s = %d\n", hwmtype
, state
.hwm
));
498 DEBUG(1, ("Error allocating a new %s\n", hwmtype
));
505 * Allocate a new unix-ID.
506 * For now this is for the default idmap domain only.
507 * Should be extended later on.
509 static NTSTATUS
idmap_tdb_get_new_id(struct idmap_domain
*dom
,
514 if (!strequal(dom
->name
, "*")) {
515 DEBUG(3, ("idmap_tdb_get_new_id: "
516 "Refusing allocation of a new unixid for domain'%s'. "
517 "Currently only supported for the default "
520 return NT_STATUS_NOT_IMPLEMENTED
;
523 ret
= idmap_tdb_allocate_id(dom
, id
);
528 /**********************************************************************
529 IDMAP MAPPING TDB BACKEND
530 **********************************************************************/
532 /*****************************
533 Initialise idmap database.
534 *****************************/
536 static NTSTATUS
idmap_tdb_set_mapping(struct idmap_domain
*dom
,
537 const struct id_map
*map
);
539 static NTSTATUS
idmap_tdb_db_init(struct idmap_domain
*dom
)
542 struct idmap_tdb_context
*ctx
;
544 DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom
->name
));
546 ctx
= talloc_zero(dom
, struct idmap_tdb_context
);
548 DEBUG(0, ("Out of memory!\n"));
549 return NT_STATUS_NO_MEMORY
;
552 /* load backend specific configuration here: */
554 if (strequal(dom
->name
, "*")) {
559 ctx
->rw_ops
= talloc_zero(ctx
, struct idmap_rw_ops
);
560 if (ctx
->rw_ops
== NULL
) {
561 DEBUG(0, ("Out of memory!\n"));
562 ret
= NT_STATUS_NO_MEMORY
;
566 ctx
->rw_ops
->get_new_id
= idmap_tdb_get_new_id
;
567 ctx
->rw_ops
->set_mapping
= idmap_tdb_set_mapping
;
569 dom
->private_data
= ctx
;
571 ret
= idmap_tdb_open_db(dom
);
572 if ( ! NT_STATUS_IS_OK(ret
)) {
585 * store a mapping in the database
588 struct idmap_tdb_set_mapping_context
{
593 static NTSTATUS
idmap_tdb_set_mapping_action(struct db_context
*db
,
597 struct idmap_tdb_set_mapping_context
*state
;
599 state
= (struct idmap_tdb_set_mapping_context
*)private_data
;
601 DEBUG(10, ("Storing %s <-> %s map\n", state
->ksidstr
, state
->kidstr
));
603 ret
= dbwrap_store_bystring(db
, state
->ksidstr
,
604 string_term_tdb_data(state
->kidstr
),
606 if (!NT_STATUS_IS_OK(ret
)) {
607 DEBUG(0, ("Error storing SID -> ID (%s -> %s): %s\n",
608 state
->ksidstr
, state
->kidstr
, nt_errstr(ret
)));
612 ret
= dbwrap_store_bystring(db
, state
->kidstr
,
613 string_term_tdb_data(state
->ksidstr
),
615 if (!NT_STATUS_IS_OK(ret
)) {
616 DEBUG(0, ("Error storing ID -> SID (%s -> %s): %s\n",
617 state
->kidstr
, state
->ksidstr
, nt_errstr(ret
)));
621 DEBUG(10,("Stored %s <-> %s\n", state
->ksidstr
, state
->kidstr
));
628 static NTSTATUS
idmap_tdb_set_mapping(struct idmap_domain
*dom
,
629 const struct id_map
*map
)
631 struct idmap_tdb_context
*ctx
;
633 char *ksidstr
, *kidstr
;
634 struct idmap_tdb_set_mapping_context state
;
636 if (!map
|| !map
->sid
) {
637 return NT_STATUS_INVALID_PARAMETER
;
640 ksidstr
= kidstr
= NULL
;
642 /* TODO: should we filter a set_mapping using low/high filters ? */
644 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
646 switch (map
->xid
.type
) {
649 kidstr
= talloc_asprintf(ctx
, "UID %lu",
650 (unsigned long)map
->xid
.id
);
654 kidstr
= talloc_asprintf(ctx
, "GID %lu",
655 (unsigned long)map
->xid
.id
);
659 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map
->xid
.type
));
660 return NT_STATUS_INVALID_PARAMETER
;
663 if (kidstr
== NULL
) {
664 DEBUG(0, ("ERROR: Out of memory!\n"));
665 ret
= NT_STATUS_NO_MEMORY
;
669 ksidstr
= sid_string_talloc(ctx
, map
->sid
);
670 if (ksidstr
== NULL
) {
671 DEBUG(0, ("Out of memory!\n"));
672 ret
= NT_STATUS_NO_MEMORY
;
676 state
.ksidstr
= ksidstr
;
677 state
.kidstr
= kidstr
;
679 ret
= dbwrap_trans_do(ctx
->db
, idmap_tdb_set_mapping_action
, &state
);
682 talloc_free(ksidstr
);
688 * Create a new mapping for an unmapped SID, also allocating a new ID.
689 * This should be run inside a transaction.
692 * Properly integrate this with multi domain idmap config:
693 * Currently, the allocator is default-config only.
695 static NTSTATUS
idmap_tdb_new_mapping(struct idmap_domain
*dom
, struct id_map
*map
)
698 struct idmap_tdb_context
*ctx
;
700 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
702 ret
= idmap_rw_new_mapping(dom
, ctx
->rw_ops
, map
);
708 /**********************************
709 Single id to sid lookup function.
710 **********************************/
712 static NTSTATUS
idmap_tdb_id_to_sid(struct idmap_domain
*dom
, struct id_map
*map
)
717 struct idmap_tdb_context
*ctx
;
720 return NT_STATUS_INVALID_PARAMETER
;
723 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
725 /* apply filters before checking */
726 if (!idmap_unix_id_is_in_range(map
->xid
.id
, dom
)) {
727 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
728 map
->xid
.id
, dom
->low_id
, dom
->high_id
));
729 return NT_STATUS_NONE_MAPPED
;
732 switch (map
->xid
.type
) {
735 keystr
= talloc_asprintf(ctx
, "UID %lu", (unsigned long)map
->xid
.id
);
739 keystr
= talloc_asprintf(ctx
, "GID %lu", (unsigned long)map
->xid
.id
);
743 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map
->xid
.type
));
744 return NT_STATUS_INVALID_PARAMETER
;
747 /* final SAFE_FREE safe */
750 if (keystr
== NULL
) {
751 DEBUG(0, ("Out of memory!\n"));
752 ret
= NT_STATUS_NO_MEMORY
;
756 DEBUG(10,("Fetching record %s\n", keystr
));
758 /* Check if the mapping exists */
759 ret
= dbwrap_fetch_bystring(ctx
->db
, NULL
, keystr
, &data
);
761 if (!NT_STATUS_IS_OK(ret
)) {
762 DEBUG(10,("Record %s not found\n", keystr
));
763 ret
= NT_STATUS_NONE_MAPPED
;
767 if (!string_to_sid(map
->sid
, (const char *)data
.dptr
)) {
768 DEBUG(10,("INVALID SID (%s) in record %s\n",
769 (const char *)data
.dptr
, keystr
));
770 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
774 DEBUG(10,("Found record %s -> %s\n", keystr
, (const char *)data
.dptr
));
778 talloc_free(data
.dptr
);
783 /**********************************
784 Single sid to id lookup function.
785 **********************************/
787 static NTSTATUS
idmap_tdb_sid_to_id(struct idmap_domain
*dom
, struct id_map
*map
)
792 unsigned long rec_id
= 0;
793 struct idmap_tdb_context
*ctx
;
794 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
796 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
798 keystr
= sid_string_talloc(tmp_ctx
, map
->sid
);
799 if (keystr
== NULL
) {
800 DEBUG(0, ("Out of memory!\n"));
801 ret
= NT_STATUS_NO_MEMORY
;
805 DEBUG(10,("Fetching record %s\n", keystr
));
807 /* Check if sid is present in database */
808 ret
= dbwrap_fetch_bystring(ctx
->db
, tmp_ctx
, keystr
, &data
);
809 if (!NT_STATUS_IS_OK(ret
)) {
810 DEBUG(10,("Record %s not found\n", keystr
));
811 ret
= NT_STATUS_NONE_MAPPED
;
815 /* What type of record is this ? */
816 if (sscanf((const char *)data
.dptr
, "UID %lu", &rec_id
) == 1) { /* Try a UID record. */
817 map
->xid
.id
= rec_id
;
818 map
->xid
.type
= ID_TYPE_UID
;
819 DEBUG(10,("Found uid record %s -> %s \n", keystr
, (const char *)data
.dptr
));
822 } else if (sscanf((const char *)data
.dptr
, "GID %lu", &rec_id
) == 1) { /* Try a GID record. */
823 map
->xid
.id
= rec_id
;
824 map
->xid
.type
= ID_TYPE_GID
;
825 DEBUG(10,("Found gid record %s -> %s \n", keystr
, (const char *)data
.dptr
));
828 } else { /* Unknown record type ! */
829 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr
, (const char *)data
.dptr
));
830 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
834 /* apply filters before returning result */
835 if (!idmap_unix_id_is_in_range(map
->xid
.id
, dom
)) {
836 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
837 map
->xid
.id
, dom
->low_id
, dom
->high_id
));
838 ret
= NT_STATUS_NONE_MAPPED
;
842 talloc_free(tmp_ctx
);
846 /**********************************
847 lookup a set of unix ids.
848 **********************************/
850 static NTSTATUS
idmap_tdb_unixids_to_sids(struct idmap_domain
*dom
, struct id_map
**ids
)
855 /* initialize the status to avoid suprise */
856 for (i
= 0; ids
[i
]; i
++) {
857 ids
[i
]->status
= ID_UNKNOWN
;
860 for (i
= 0; ids
[i
]; i
++) {
861 ret
= idmap_tdb_id_to_sid(dom
, ids
[i
]);
862 if ( ! NT_STATUS_IS_OK(ret
)) {
864 /* if it is just a failed mapping continue */
865 if (NT_STATUS_EQUAL(ret
, NT_STATUS_NONE_MAPPED
)) {
867 /* make sure it is marked as unmapped */
868 ids
[i
]->status
= ID_UNMAPPED
;
872 /* some fatal error occurred, return immediately */
876 /* all ok, id is mapped */
877 ids
[i
]->status
= ID_MAPPED
;
886 /**********************************
887 lookup a set of sids.
888 **********************************/
890 struct idmap_tdb_sids_to_unixids_context
{
891 struct idmap_domain
*dom
;
893 bool allocate_unmapped
;
896 static NTSTATUS
idmap_tdb_sids_to_unixids_action(struct db_context
*db
,
899 struct idmap_tdb_sids_to_unixids_context
*state
;
901 NTSTATUS ret
= NT_STATUS_OK
;
903 state
= (struct idmap_tdb_sids_to_unixids_context
*)private_data
;
905 DEBUG(10, ("idmap_tdb_sids_to_unixids_action: "
906 " domain: [%s], allocate: %s\n",
908 state
->allocate_unmapped
? "yes" : "no"));
910 for (i
= 0; state
->ids
[i
]; i
++) {
911 if ((state
->ids
[i
]->status
== ID_UNKNOWN
) ||
912 /* retry if we could not map in previous run: */
913 (state
->ids
[i
]->status
== ID_UNMAPPED
))
917 ret2
= idmap_tdb_sid_to_id(state
->dom
, state
->ids
[i
]);
918 if (!NT_STATUS_IS_OK(ret2
)) {
920 /* if it is just a failed mapping, continue */
921 if (NT_STATUS_EQUAL(ret2
, NT_STATUS_NONE_MAPPED
)) {
923 /* make sure it is marked as unmapped */
924 state
->ids
[i
]->status
= ID_UNMAPPED
;
925 ret
= STATUS_SOME_UNMAPPED
;
927 /* some fatal error occurred, return immediately */
932 /* all ok, id is mapped */
933 state
->ids
[i
]->status
= ID_MAPPED
;
937 if ((state
->ids
[i
]->status
== ID_UNMAPPED
) &&
938 state
->allocate_unmapped
)
940 ret
= idmap_tdb_new_mapping(state
->dom
, state
->ids
[i
]);
941 if (!NT_STATUS_IS_OK(ret
)) {
951 static NTSTATUS
idmap_tdb_sids_to_unixids(struct idmap_domain
*dom
, struct id_map
**ids
)
953 struct idmap_tdb_context
*ctx
;
956 struct idmap_tdb_sids_to_unixids_context state
;
958 /* initialize the status to avoid suprise */
959 for (i
= 0; ids
[i
]; i
++) {
960 ids
[i
]->status
= ID_UNKNOWN
;
963 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
967 state
.allocate_unmapped
= false;
969 ret
= idmap_tdb_sids_to_unixids_action(ctx
->db
, &state
);
971 if (NT_STATUS_EQUAL(ret
, STATUS_SOME_UNMAPPED
) && !dom
->read_only
) {
972 state
.allocate_unmapped
= true;
973 ret
= dbwrap_trans_do(ctx
->db
,
974 idmap_tdb_sids_to_unixids_action
,
982 /**********************************
983 Close the idmap tdb instance
984 **********************************/
986 static struct idmap_methods db_methods
= {
987 .init
= idmap_tdb_db_init
,
988 .unixids_to_sids
= idmap_tdb_unixids_to_sids
,
989 .sids_to_unixids
= idmap_tdb_sids_to_unixids
,
990 .allocate_id
= idmap_tdb_get_new_id
,
993 NTSTATUS
idmap_tdb_init(void)
995 DEBUG(10, ("calling idmap_tdb_init\n"));
997 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
, "tdb", &db_methods
);