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
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #define DBGC_CLASS DBGC_IDMAP
31 /* High water mark keys */
32 #define HWM_GROUP "GROUP HWM"
33 #define HWM_USER "USER HWM"
35 static struct idmap_tdb_state
{
37 /* User and group id pool */
38 uid_t low_uid
, high_uid
; /* Range of uids to allocate */
39 gid_t low_gid
, high_gid
; /* Range of gids to allocate */
43 /*****************************************************************************
44 For idmap conversion: convert one record to new format
45 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
47 *****************************************************************************/
48 static int convert_fn(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA data
, void *state
)
50 struct winbindd_domain
*domain
;
57 bool *failed
= (bool *)state
;
59 DEBUG(10,("Converting %s\n", (const char *)key
.dptr
));
61 p
= strchr((const char *)key
.dptr
, '/');
66 fstrcpy(dom_name
, (const char *)key
.dptr
);
69 domain
= find_domain_from_name(dom_name
);
71 /* We must delete the old record. */
72 DEBUG(0,("Unable to find domain %s\n", dom_name
));
73 DEBUG(0,("deleting record %s\n", (const char *)key
.dptr
));
75 if (tdb_delete(tdb
, key
) != 0) {
76 DEBUG(0, ("Unable to delete record %s\n", (const char *)key
.dptr
));
86 sid_copy(&sid
, &domain
->sid
);
87 sid_append_rid(&sid
, rid
);
89 sid_to_fstring(keystr
, &sid
);
90 key2
= string_term_tdb_data(keystr
);
92 if (tdb_store(tdb
, key2
, data
, TDB_INSERT
) != 0) {
93 DEBUG(0,("Unable to add record %s\n", (const char *)key2
.dptr
));
98 if (tdb_store(tdb
, data
, key2
, TDB_REPLACE
) != 0) {
99 DEBUG(0,("Unable to update record %s\n", (const char *)data
.dptr
));
104 if (tdb_delete(tdb
, key
) != 0) {
105 DEBUG(0,("Unable to delete record %s\n", (const char *)key
.dptr
));
113 /*****************************************************************************
114 Convert the idmap database from an older version.
115 *****************************************************************************/
117 static bool idmap_tdb_upgrade(const char *idmap_name
)
120 bool bigendianheader
;
122 TDB_CONTEXT
*idmap_tdb
;
124 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
126 if (!(idmap_tdb
= tdb_open_log(idmap_name
, 0,
129 DEBUG(0, ("Unable to open idmap database\n"));
133 bigendianheader
= (tdb_get_flags(idmap_tdb
) & TDB_BIGENDIAN
) ? True
: False
;
135 vers
= tdb_fetch_int32(idmap_tdb
, "IDMAP_VERSION");
137 if (((vers
== -1) && bigendianheader
) || (IREV(vers
) == IDMAP_VERSION
)) {
138 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
140 * high and low records were created on a
141 * big endian machine and will need byte-reversing.
146 wm
= tdb_fetch_int32(idmap_tdb
, HWM_USER
);
151 wm
= idmap_tdb_state
.low_uid
;
154 if (tdb_store_int32(idmap_tdb
, HWM_USER
, wm
) == -1) {
155 DEBUG(0, ("Unable to byteswap user hwm in idmap database\n"));
156 tdb_close(idmap_tdb
);
160 wm
= tdb_fetch_int32(idmap_tdb
, HWM_GROUP
);
164 wm
= idmap_tdb_state
.low_gid
;
167 if (tdb_store_int32(idmap_tdb
, HWM_GROUP
, wm
) == -1) {
168 DEBUG(0, ("Unable to byteswap group hwm in idmap database\n"));
169 tdb_close(idmap_tdb
);
174 /* the old format stored as DOMAIN/rid - now we store the SID direct */
175 tdb_traverse(idmap_tdb
, convert_fn
, &failed
);
178 DEBUG(0, ("Problem during conversion\n"));
179 tdb_close(idmap_tdb
);
183 if (tdb_store_int32(idmap_tdb
, "IDMAP_VERSION", IDMAP_VERSION
) == -1) {
184 DEBUG(0, ("Unable to dtore idmap version in databse\n"));
185 tdb_close(idmap_tdb
);
189 tdb_close(idmap_tdb
);
193 /* WARNING: We can't open a tdb twice inthe same process, for that reason
194 * I'm going to use a hack with open ref counts to open the winbindd_idmap.tdb
195 * only once. We will later decide whether to split the db in multiple files
196 * or come up with a better solution to share them. */
198 static TDB_CONTEXT
*idmap_tdb_common_ctx
;
199 static int idmap_tdb_open_ref_count
= 0;
201 static NTSTATUS
idmap_tdb_open_db(TALLOC_CTX
*memctx
, TDB_CONTEXT
**tdbctx
)
205 SMB_STRUCT_STAT stbuf
;
206 char *tdbfile
= NULL
;
208 bool tdb_is_new
= False
;
210 if (idmap_tdb_open_ref_count
) { /* the tdb has already been opened */
211 idmap_tdb_open_ref_count
++;
212 *tdbctx
= idmap_tdb_common_ctx
;
216 /* use our own context here */
217 ctx
= talloc_new(memctx
);
219 DEBUG(0, ("Out of memory!\n"));
220 return NT_STATUS_NO_MEMORY
;
223 /* use the old database if present */
224 tdbfile
= talloc_strdup(ctx
, state_path("winbindd_idmap.tdb"));
226 DEBUG(0, ("Out of memory!\n"));
227 ret
= NT_STATUS_NO_MEMORY
;
231 if (!file_exist(tdbfile
, &stbuf
)) {
235 DEBUG(10,("Opening tdbfile %s\n", tdbfile
));
237 /* Open idmap repository */
238 if (!(idmap_tdb_common_ctx
= tdb_open_log(tdbfile
, 0, TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0644))) {
239 DEBUG(0, ("Unable to open idmap database\n"));
240 ret
= NT_STATUS_UNSUCCESSFUL
;
245 /* the file didn't existed before opening it, let's
246 * store idmap version as nobody else yet opened and
247 * stored it. I do not like this method but didn't
248 * found a way to understand if an opened tdb have
249 * been just created or not --- SSS */
250 tdb_store_int32(idmap_tdb_common_ctx
, "IDMAP_VERSION", IDMAP_VERSION
);
253 /* check against earlier versions */
254 version
= tdb_fetch_int32(idmap_tdb_common_ctx
, "IDMAP_VERSION");
255 if (version
!= IDMAP_VERSION
) {
257 /* backup_tdb expects the tdb not to be open */
258 tdb_close(idmap_tdb_common_ctx
);
260 if ( ! idmap_tdb_upgrade(tdbfile
)) {
262 DEBUG(0, ("Unable to open idmap database, it's in an old formati, and upgrade failed!\n"));
263 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
267 /* Re-Open idmap repository */
268 if (!(idmap_tdb_common_ctx
= tdb_open_log(tdbfile
, 0, TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0644))) {
269 DEBUG(0, ("Unable to open idmap database\n"));
270 ret
= NT_STATUS_UNSUCCESSFUL
;
275 *tdbctx
= idmap_tdb_common_ctx
;
276 idmap_tdb_open_ref_count
++;
284 /* NEVER use tdb_close() except for the conversion routines that are guaranteed
285 * to run only when the database is opened the first time, always use this function. */
287 bool idmap_tdb_tdb_close(TDB_CONTEXT
*tdbctx
)
289 if (tdbctx
!= idmap_tdb_common_ctx
) {
290 DEBUG(0, ("ERROR: Invalid tdb context!"));
294 idmap_tdb_open_ref_count
--;
295 if (idmap_tdb_open_ref_count
) {
299 return tdb_close(idmap_tdb_common_ctx
);
302 /**********************************************************************
303 IDMAP ALLOC TDB BACKEND
304 **********************************************************************/
306 static TDB_CONTEXT
*idmap_alloc_tdb
;
308 /**********************************
309 Initialise idmap alloc database.
310 **********************************/
312 static NTSTATUS
idmap_tdb_alloc_init( const char *params
)
322 /* use our own context here */
323 ctx
= talloc_new(NULL
);
325 DEBUG(0, ("Out of memory!\n"));
326 return NT_STATUS_NO_MEMORY
;
329 ret
= idmap_tdb_open_db(ctx
, &idmap_alloc_tdb
);
330 if ( ! NT_STATUS_IS_OK(ret
)) {
339 if (!lp_idmap_uid(&low_uid
, &high_uid
)
340 || !lp_idmap_gid(&low_gid
, &high_gid
)) {
341 DEBUG(1, ("idmap uid or idmap gid missing\n"));
342 return NT_STATUS_UNSUCCESSFUL
;
345 idmap_tdb_state
.low_uid
= low_uid
;
346 idmap_tdb_state
.high_uid
= high_uid
;
347 idmap_tdb_state
.low_gid
= low_gid
;
348 idmap_tdb_state
.high_gid
= high_gid
;
350 if (idmap_tdb_state
.high_uid
<= idmap_tdb_state
.low_uid
) {
351 DEBUG(1, ("idmap uid range missing or invalid\n"));
352 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
353 return NT_STATUS_UNSUCCESSFUL
;
356 if (idmap_tdb_state
.high_gid
<= idmap_tdb_state
.low_gid
) {
357 DEBUG(1, ("idmap gid range missing or invalid\n"));
358 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
359 return NT_STATUS_UNSUCCESSFUL
;
362 if (((low_id
= tdb_fetch_int32(idmap_alloc_tdb
, HWM_USER
)) == -1) ||
363 (low_id
< idmap_tdb_state
.low_uid
)) {
364 if (tdb_store_int32(idmap_alloc_tdb
, HWM_USER
,
365 idmap_tdb_state
.low_uid
) == -1) {
366 DEBUG(0, ("Unable to initialise user hwm in idmap "
368 return NT_STATUS_INTERNAL_DB_ERROR
;
372 if (((low_id
= tdb_fetch_int32(idmap_alloc_tdb
, HWM_GROUP
)) == -1) ||
373 (low_id
< idmap_tdb_state
.low_gid
)) {
374 if (tdb_store_int32(idmap_alloc_tdb
, HWM_GROUP
,
375 idmap_tdb_state
.low_gid
) == -1) {
376 DEBUG(0, ("Unable to initialise group hwm in idmap "
378 return NT_STATUS_INTERNAL_DB_ERROR
;
385 /**********************************
387 **********************************/
389 static NTSTATUS
idmap_tdb_allocate_id(struct unixid
*xid
)
397 /* Get current high water mark */
403 high_hwm
= idmap_tdb_state
.high_uid
;
409 high_hwm
= idmap_tdb_state
.high_gid
;
413 DEBUG(2, ("Invalid ID type (0x%x)\n", xid
->type
));
414 return NT_STATUS_INVALID_PARAMETER
;
417 if ((hwm
= tdb_fetch_int32(idmap_alloc_tdb
, hwmkey
)) == -1) {
418 return NT_STATUS_INTERNAL_DB_ERROR
;
421 /* check it is in the range */
422 if (hwm
> high_hwm
) {
423 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
424 hwmtype
, (unsigned long)high_hwm
));
425 return NT_STATUS_UNSUCCESSFUL
;
428 /* fetch a new id and increment it */
429 ret
= tdb_change_uint32_atomic(idmap_alloc_tdb
, hwmkey
, &hwm
, 1);
431 DEBUG(1, ("Fatal error while fetching a new %s value\n!", hwmtype
));
432 return NT_STATUS_UNSUCCESSFUL
;
435 /* recheck it is in the range */
436 if (hwm
> high_hwm
) {
437 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
438 hwmtype
, (unsigned long)high_hwm
));
439 return NT_STATUS_UNSUCCESSFUL
;
443 DEBUG(10,("New %s = %d\n", hwmtype
, hwm
));
448 /**********************************
449 Get current highest id.
450 **********************************/
452 static NTSTATUS
idmap_tdb_get_hwm(struct unixid
*xid
)
459 /* Get current high water mark */
465 high_hwm
= idmap_tdb_state
.high_uid
;
471 high_hwm
= idmap_tdb_state
.high_gid
;
475 return NT_STATUS_INVALID_PARAMETER
;
478 if ((hwm
= tdb_fetch_int32(idmap_alloc_tdb
, hwmkey
)) == -1) {
479 return NT_STATUS_INTERNAL_DB_ERROR
;
484 /* Warn if it is out of range */
485 if (hwm
>= high_hwm
) {
486 DEBUG(0, ("Warning: %s range full!! (max: %lu)\n",
487 hwmtype
, (unsigned long)high_hwm
));
493 /**********************************
495 **********************************/
497 static NTSTATUS
idmap_tdb_set_hwm(struct unixid
*xid
)
504 /* Get current high water mark */
510 high_hwm
= idmap_tdb_state
.high_uid
;
516 high_hwm
= idmap_tdb_state
.high_gid
;
520 return NT_STATUS_INVALID_PARAMETER
;
525 if ((hwm
= tdb_store_int32(idmap_alloc_tdb
, hwmkey
, hwm
)) == -1) {
526 return NT_STATUS_INTERNAL_DB_ERROR
;
529 /* Warn if it is out of range */
530 if (hwm
>= high_hwm
) {
531 DEBUG(0, ("Warning: %s range full!! (max: %lu)\n",
532 hwmtype
, (unsigned long)high_hwm
));
538 /**********************************
540 **********************************/
542 static NTSTATUS
idmap_tdb_alloc_close(void)
544 if (idmap_alloc_tdb
) {
545 if (idmap_tdb_tdb_close(idmap_alloc_tdb
) == 0) {
548 return NT_STATUS_UNSUCCESSFUL
;
554 /**********************************************************************
555 IDMAP MAPPING TDB BACKEND
556 **********************************************************************/
558 struct idmap_tdb_context
{
560 uint32_t filter_low_id
;
561 uint32_t filter_high_id
;
564 /*****************************
565 Initialise idmap database.
566 *****************************/
568 static NTSTATUS
idmap_tdb_db_init(struct idmap_domain
*dom
, const char *params
)
571 struct idmap_tdb_context
*ctx
;
572 char *config_option
= NULL
;
575 ctx
= talloc(dom
, struct idmap_tdb_context
);
577 DEBUG(0, ("Out of memory!\n"));
578 return NT_STATUS_NO_MEMORY
;
581 config_option
= talloc_asprintf(ctx
, "idmap config %s", dom
->name
);
582 if ( ! config_option
) {
583 DEBUG(0, ("Out of memory!\n"));
584 ret
= NT_STATUS_NO_MEMORY
;
588 ret
= idmap_tdb_open_db(ctx
, &ctx
->tdb
);
589 if ( ! NT_STATUS_IS_OK(ret
)) {
593 range
= lp_parm_const_string(-1, config_option
, "range", NULL
);
595 (sscanf(range
, "%u - %u", &ctx
->filter_low_id
, &ctx
->filter_high_id
) != 2) ||
596 (ctx
->filter_low_id
> ctx
->filter_high_id
)) {
597 ctx
->filter_low_id
= 0;
598 ctx
->filter_high_id
= 0;
601 dom
->private_data
= ctx
;
603 talloc_free(config_option
);
611 /**********************************
612 Single id to sid lookup function.
613 **********************************/
615 static NTSTATUS
idmap_tdb_id_to_sid(struct idmap_tdb_context
*ctx
, struct id_map
*map
)
622 return NT_STATUS_INVALID_PARAMETER
;
625 /* apply filters before checking */
626 if ((ctx
->filter_low_id
&& (map
->xid
.id
< ctx
->filter_low_id
)) ||
627 (ctx
->filter_high_id
&& (map
->xid
.id
> ctx
->filter_high_id
))) {
628 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
629 map
->xid
.id
, ctx
->filter_low_id
, ctx
->filter_high_id
));
630 return NT_STATUS_NONE_MAPPED
;
633 switch (map
->xid
.type
) {
636 keystr
= talloc_asprintf(ctx
, "UID %lu", (unsigned long)map
->xid
.id
);
640 keystr
= talloc_asprintf(ctx
, "GID %lu", (unsigned long)map
->xid
.id
);
644 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map
->xid
.type
));
645 return NT_STATUS_INVALID_PARAMETER
;
648 /* final SAFE_FREE safe */
651 if (keystr
== NULL
) {
652 DEBUG(0, ("Out of memory!\n"));
653 ret
= NT_STATUS_NO_MEMORY
;
657 DEBUG(10,("Fetching record %s\n", keystr
));
659 /* Check if the mapping exists */
660 data
= tdb_fetch_bystring(ctx
->tdb
, keystr
);
663 DEBUG(10,("Record %s not found\n", keystr
));
664 ret
= NT_STATUS_NONE_MAPPED
;
668 if (!string_to_sid(map
->sid
, (const char *)data
.dptr
)) {
669 DEBUG(10,("INVALID SID (%s) in record %s\n",
670 (const char *)data
.dptr
, keystr
));
671 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
675 DEBUG(10,("Found record %s -> %s\n", keystr
, (const char *)data
.dptr
));
679 SAFE_FREE(data
.dptr
);
684 /**********************************
685 Single sid to id lookup function.
686 **********************************/
688 static NTSTATUS
idmap_tdb_sid_to_id(struct idmap_tdb_context
*ctx
, struct id_map
*map
)
693 unsigned long rec_id
= 0;
696 if ((keystr
= talloc_asprintf(
697 ctx
, "%s", sid_to_fstring(tmp
, map
->sid
))) == NULL
) {
698 DEBUG(0, ("Out of memory!\n"));
699 ret
= NT_STATUS_NO_MEMORY
;
703 DEBUG(10,("Fetching record %s\n", keystr
));
705 /* Check if sid is present in database */
706 data
= tdb_fetch_bystring(ctx
->tdb
, keystr
);
708 DEBUG(10,("Record %s not found\n", keystr
));
709 ret
= NT_STATUS_NONE_MAPPED
;
713 /* What type of record is this ? */
714 if (sscanf((const char *)data
.dptr
, "UID %lu", &rec_id
) == 1) { /* Try a UID record. */
715 map
->xid
.id
= rec_id
;
716 map
->xid
.type
= ID_TYPE_UID
;
717 DEBUG(10,("Found uid record %s -> %s \n", keystr
, (const char *)data
.dptr
));
720 } else if (sscanf((const char *)data
.dptr
, "GID %lu", &rec_id
) == 1) { /* Try a GID record. */
721 map
->xid
.id
= rec_id
;
722 map
->xid
.type
= ID_TYPE_GID
;
723 DEBUG(10,("Found gid record %s -> %s \n", keystr
, (const char *)data
.dptr
));
726 } else { /* Unknown record type ! */
727 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr
, (const char *)data
.dptr
));
728 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
731 SAFE_FREE(data
.dptr
);
733 /* apply filters before returning result */
734 if ((ctx
->filter_low_id
&& (map
->xid
.id
< ctx
->filter_low_id
)) ||
735 (ctx
->filter_high_id
&& (map
->xid
.id
> ctx
->filter_high_id
))) {
736 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
737 map
->xid
.id
, ctx
->filter_low_id
, ctx
->filter_high_id
));
738 ret
= NT_STATUS_NONE_MAPPED
;
746 /**********************************
747 lookup a set of unix ids.
748 **********************************/
750 static NTSTATUS
idmap_tdb_unixids_to_sids(struct idmap_domain
*dom
, struct id_map
**ids
)
752 struct idmap_tdb_context
*ctx
;
756 /* initialize the status to avoid suprise */
757 for (i
= 0; ids
[i
]; i
++) {
758 ids
[i
]->status
= ID_UNKNOWN
;
761 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
763 for (i
= 0; ids
[i
]; i
++) {
764 ret
= idmap_tdb_id_to_sid(ctx
, ids
[i
]);
765 if ( ! NT_STATUS_IS_OK(ret
)) {
767 /* if it is just a failed mapping continue */
768 if (NT_STATUS_EQUAL(ret
, NT_STATUS_NONE_MAPPED
)) {
770 /* make sure it is marked as unmapped */
771 ids
[i
]->status
= ID_UNMAPPED
;
775 /* some fatal error occurred, return immediately */
779 /* all ok, id is mapped */
780 ids
[i
]->status
= ID_MAPPED
;
789 /**********************************
790 lookup a set of sids.
791 **********************************/
793 static NTSTATUS
idmap_tdb_sids_to_unixids(struct idmap_domain
*dom
, struct id_map
**ids
)
795 struct idmap_tdb_context
*ctx
;
799 /* initialize the status to avoid suprise */
800 for (i
= 0; ids
[i
]; i
++) {
801 ids
[i
]->status
= ID_UNKNOWN
;
804 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
806 for (i
= 0; ids
[i
]; i
++) {
807 ret
= idmap_tdb_sid_to_id(ctx
, ids
[i
]);
808 if ( ! NT_STATUS_IS_OK(ret
)) {
810 /* if it is just a failed mapping continue */
811 if (NT_STATUS_EQUAL(ret
, NT_STATUS_NONE_MAPPED
)) {
813 /* make sure it is marked as unmapped */
814 ids
[i
]->status
= ID_UNMAPPED
;
818 /* some fatal error occurred, return immediately */
822 /* all ok, id is mapped */
823 ids
[i
]->status
= ID_MAPPED
;
832 /**********************************
834 **********************************/
836 static NTSTATUS
idmap_tdb_set_mapping(struct idmap_domain
*dom
,
837 const struct id_map
*map
)
839 struct idmap_tdb_context
*ctx
;
841 TDB_DATA ksid
, kid
, data
;
842 char *ksidstr
, *kidstr
;
845 if (!map
|| !map
->sid
) {
846 return NT_STATUS_INVALID_PARAMETER
;
849 ksidstr
= kidstr
= NULL
;
852 /* TODO: should we filter a set_mapping using low/high filters ? */
854 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
856 switch (map
->xid
.type
) {
859 kidstr
= talloc_asprintf(ctx
, "UID %lu",
860 (unsigned long)map
->xid
.id
);
864 kidstr
= talloc_asprintf(ctx
, "GID %lu",
865 (unsigned long)map
->xid
.id
);
869 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map
->xid
.type
));
870 return NT_STATUS_INVALID_PARAMETER
;
873 if (kidstr
== NULL
) {
874 DEBUG(0, ("ERROR: Out of memory!\n"));
875 ret
= NT_STATUS_NO_MEMORY
;
879 if ((ksidstr
= talloc_asprintf(
880 ctx
, "%s", sid_to_fstring(tmp
, map
->sid
))) == NULL
) {
881 DEBUG(0, ("Out of memory!\n"));
882 ret
= NT_STATUS_NO_MEMORY
;
886 DEBUG(10, ("Storing %s <-> %s map\n", ksidstr
, kidstr
));
887 kid
= string_term_tdb_data(kidstr
);
888 ksid
= string_term_tdb_data(ksidstr
);
890 /* *DELETE* previous mappings if any.
891 * This is done for both the SID and [U|G]ID passed in */
893 /* NOTE: We should lock both the ksid and kid records here, before
894 * making modifications. However, because tdb_chainlock() is a
895 * blocking call we could create an unrecoverable deadlock, so for now
896 * we only lock the ksid record. */
898 /* Lock the record for this SID. */
899 if (tdb_chainlock(ctx
->tdb
, ksid
) != 0) {
900 DEBUG(10,("Failed to lock record %s. Error %s\n",
901 ksidstr
, tdb_errorstr(ctx
->tdb
) ));
902 return NT_STATUS_UNSUCCESSFUL
;
905 data
= tdb_fetch(ctx
->tdb
, ksid
);
907 DEBUG(10, ("Deleting existing mapping %s <-> %s\n",
908 (const char *)data
.dptr
, ksidstr
));
909 tdb_delete(ctx
->tdb
, data
);
910 tdb_delete(ctx
->tdb
, ksid
);
911 SAFE_FREE(data
.dptr
);
914 data
= tdb_fetch(ctx
->tdb
, kid
);
916 DEBUG(10,("Deleting existing mapping %s <-> %s\n",
917 (const char *)data
.dptr
, kidstr
));
918 tdb_delete(ctx
->tdb
, data
);
919 tdb_delete(ctx
->tdb
, kid
);
920 SAFE_FREE(data
.dptr
);
923 if (tdb_store(ctx
->tdb
, ksid
, kid
, TDB_INSERT
) == -1) {
924 DEBUG(0, ("Error storing SID -> ID: %s\n",
925 tdb_errorstr(ctx
->tdb
)));
926 tdb_chainunlock(ctx
->tdb
, ksid
);
927 ret
= NT_STATUS_UNSUCCESSFUL
;
930 if (tdb_store(ctx
->tdb
, kid
, ksid
, TDB_INSERT
) == -1) {
931 DEBUG(0, ("Error storing ID -> SID: %s\n",
932 tdb_errorstr(ctx
->tdb
)));
933 /* try to remove the previous stored SID -> ID map */
934 tdb_delete(ctx
->tdb
, ksid
);
935 tdb_chainunlock(ctx
->tdb
, ksid
);
936 ret
= NT_STATUS_UNSUCCESSFUL
;
940 tdb_chainunlock(ctx
->tdb
, ksid
);
941 DEBUG(10,("Stored %s <-> %s\n", ksidstr
, kidstr
));
945 talloc_free(ksidstr
);
947 SAFE_FREE(data
.dptr
);
951 /**********************************
953 **********************************/
955 static NTSTATUS
idmap_tdb_remove_mapping(struct idmap_domain
*dom
,
956 const struct id_map
*map
)
958 struct idmap_tdb_context
*ctx
;
960 TDB_DATA ksid
, kid
, data
;
961 char *ksidstr
, *kidstr
;
964 if (!map
|| !map
->sid
) {
965 return NT_STATUS_INVALID_PARAMETER
;
968 ksidstr
= kidstr
= NULL
;
971 /* TODO: should we filter a remove_mapping using low/high filters ? */
973 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
975 switch (map
->xid
.type
) {
978 kidstr
= talloc_asprintf(ctx
, "UID %lu",
979 (unsigned long)map
->xid
.id
);
983 kidstr
= talloc_asprintf(ctx
, "GID %lu",
984 (unsigned long)map
->xid
.id
);
988 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map
->xid
.type
));
989 return NT_STATUS_INVALID_PARAMETER
;
992 if (kidstr
== NULL
) {
993 DEBUG(0, ("ERROR: Out of memory!\n"));
994 ret
= NT_STATUS_NO_MEMORY
;
998 if ((ksidstr
= talloc_asprintf(
999 ctx
, "%s", sid_to_fstring(tmp
, map
->sid
))) == NULL
) {
1000 DEBUG(0, ("Out of memory!\n"));
1001 ret
= NT_STATUS_NO_MEMORY
;
1005 DEBUG(10, ("Checking %s <-> %s map\n", ksidstr
, kidstr
));
1006 ksid
= string_term_tdb_data(ksidstr
);
1007 kid
= string_term_tdb_data(kidstr
);
1009 /* NOTE: We should lock both the ksid and kid records here, before
1010 * making modifications. However, because tdb_chainlock() is a
1011 * blocking call we could create an unrecoverable deadlock, so for now
1012 * we only lock the ksid record. */
1014 /* Lock the record for this SID. */
1015 if (tdb_chainlock(ctx
->tdb
, ksid
) != 0) {
1016 DEBUG(10,("Failed to lock record %s. Error %s\n",
1017 ksidstr
, tdb_errorstr(ctx
->tdb
) ));
1018 return NT_STATUS_UNSUCCESSFUL
;
1021 /* Check if sid is present in database */
1022 data
= tdb_fetch(ctx
->tdb
, ksid
);
1024 DEBUG(10,("Record %s not found\n", ksidstr
));
1025 tdb_chainunlock(ctx
->tdb
, ksid
);
1026 ret
= NT_STATUS_NONE_MAPPED
;
1030 /* Check if sid is mapped to the specified ID */
1031 if ((data
.dsize
!= kid
.dsize
) ||
1032 (memcmp(data
.dptr
, kid
.dptr
, data
.dsize
) != 0)) {
1033 DEBUG(10,("Specified SID does not map to specified ID\n"));
1034 DEBUGADD(10,("Actual mapping is %s -> %s\n", ksidstr
,
1035 (const char *)data
.dptr
));
1036 tdb_chainunlock(ctx
->tdb
, ksid
);
1037 ret
= NT_STATUS_NONE_MAPPED
;
1041 DEBUG(10, ("Removing %s <-> %s map\n", ksidstr
, kidstr
));
1043 /* Delete previous mappings. */
1045 DEBUG(10, ("Deleting existing mapping %s -> %s\n", ksidstr
, kidstr
));
1046 tdb_delete(ctx
->tdb
, ksid
);
1048 DEBUG(10,("Deleting existing mapping %s -> %s\n", kidstr
, ksidstr
));
1049 tdb_delete(ctx
->tdb
, kid
);
1051 tdb_chainunlock(ctx
->tdb
, ksid
);
1055 talloc_free(ksidstr
);
1056 talloc_free(kidstr
);
1057 SAFE_FREE(data
.dptr
);
1061 /**********************************
1062 Close the idmap tdb instance
1063 **********************************/
1065 static NTSTATUS
idmap_tdb_close(struct idmap_domain
*dom
)
1067 struct idmap_tdb_context
*ctx
;
1069 if (dom
->private_data
) {
1070 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
1072 if (idmap_tdb_tdb_close(ctx
->tdb
) == 0) {
1073 return NT_STATUS_OK
;
1075 return NT_STATUS_UNSUCCESSFUL
;
1078 return NT_STATUS_OK
;
1083 struct id_map
**maps
;
1088 static int idmap_tdb_dump_one_entry(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA value
, void *pdata
)
1090 struct dump_data
*data
= talloc_get_type(pdata
, struct dump_data
);
1091 struct id_map
*maps
;
1092 int num_maps
= *data
->num_maps
;
1094 /* ignore any record but the ones with a SID as key */
1095 if (strncmp((const char *)key
.dptr
, "S-", 2) == 0) {
1097 maps
= talloc_realloc(NULL
, *data
->maps
, struct id_map
, num_maps
+1);
1099 DEBUG(0, ("Out of memory!\n"));
1100 data
->ret
= NT_STATUS_NO_MEMORY
;
1104 maps
[num_maps
].sid
= talloc(maps
, DOM_SID
);
1105 if ( ! maps
[num_maps
].sid
) {
1106 DEBUG(0, ("Out of memory!\n"));
1107 data
->ret
= NT_STATUS_NO_MEMORY
;
1111 if (!string_to_sid(maps
[num_maps
].sid
, (const char *)key
.dptr
)) {
1112 DEBUG(10,("INVALID record %s\n", (const char *)key
.dptr
));
1113 /* continue even with errors */
1117 /* Try a UID record. */
1118 if (sscanf((const char *)value
.dptr
, "UID %u", &(maps
[num_maps
].xid
.id
)) == 1) {
1119 maps
[num_maps
].xid
.type
= ID_TYPE_UID
;
1120 maps
[num_maps
].status
= ID_MAPPED
;
1121 *data
->num_maps
= num_maps
+ 1;
1123 /* Try a GID record. */
1125 if (sscanf((const char *)value
.dptr
, "GID %u", &(maps
[num_maps
].xid
.id
)) == 1) {
1126 maps
[num_maps
].xid
.type
= ID_TYPE_GID
;
1127 maps
[num_maps
].status
= ID_MAPPED
;
1128 *data
->num_maps
= num_maps
+ 1;
1130 /* Unknown record type ! */
1132 maps
[num_maps
].status
= ID_UNKNOWN
;
1133 DEBUG(2, ("Found INVALID record %s -> %s\n",
1134 (const char *)key
.dptr
, (const char *)value
.dptr
));
1135 /* do not increment num_maps */
1142 /**********************************
1143 Dump all mappings out
1144 **********************************/
1146 static NTSTATUS
idmap_tdb_dump_data(struct idmap_domain
*dom
, struct id_map
**maps
, int *num_maps
)
1148 struct idmap_tdb_context
*ctx
;
1149 struct dump_data
*data
;
1150 NTSTATUS ret
= NT_STATUS_OK
;
1152 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
1154 data
= TALLOC_ZERO_P(ctx
, struct dump_data
);
1156 DEBUG(0, ("Out of memory!\n"));
1157 return NT_STATUS_NO_MEMORY
;
1160 data
->num_maps
= num_maps
;
1161 data
->ret
= NT_STATUS_OK
;
1163 tdb_traverse(ctx
->tdb
, idmap_tdb_dump_one_entry
, data
);
1165 if ( ! NT_STATUS_IS_OK(data
->ret
)) {
1173 static struct idmap_methods db_methods
= {
1175 .init
= idmap_tdb_db_init
,
1176 .unixids_to_sids
= idmap_tdb_unixids_to_sids
,
1177 .sids_to_unixids
= idmap_tdb_sids_to_unixids
,
1178 .set_mapping
= idmap_tdb_set_mapping
,
1179 .remove_mapping
= idmap_tdb_remove_mapping
,
1180 .dump_data
= idmap_tdb_dump_data
,
1181 .close_fn
= idmap_tdb_close
1184 static struct idmap_alloc_methods db_alloc_methods
= {
1186 .init
= idmap_tdb_alloc_init
,
1187 .allocate_id
= idmap_tdb_allocate_id
,
1188 .get_id_hwm
= idmap_tdb_get_hwm
,
1189 .set_id_hwm
= idmap_tdb_set_hwm
,
1190 .close_fn
= idmap_tdb_alloc_close
1193 NTSTATUS
idmap_alloc_tdb_init(void)
1195 return smb_register_idmap_alloc(SMB_IDMAP_INTERFACE_VERSION
, "tdb", &db_alloc_methods
);
1198 NTSTATUS
idmap_tdb_init(void)
1202 DEBUG(10, ("calling idmap_tdb_init\n"));
1204 /* FIXME: bad hack to actually register also the alloc_tdb module without changining configure.in */
1205 ret
= idmap_alloc_tdb_init();
1206 if (! NT_STATUS_IS_OK(ret
)) {
1209 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
, "tdb", &db_methods
);