Replace a very unusual variable declaration by a more conventional one
[Samba.git] / source / winbindd / idmap_tdb.c
blobe5f605361b4ebb8028f5ce6f2312dbfffa5cf82e
1 /*
2 Unix SMB/CIFS implementation.
4 idmap TDB backend
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/>.
25 #include "includes.h"
26 #include "winbindd.h"
28 #undef DBGC_CLASS
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 */
41 } idmap_tdb_state;
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
46 instead of the SID.
47 *****************************************************************************/
48 static int convert_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA data, void *state)
50 struct winbindd_domain *domain;
51 char *p;
52 DOM_SID sid;
53 uint32 rid;
54 fstring keystr;
55 fstring dom_name;
56 TDB_DATA key2;
57 bool *failed = (bool *)state;
59 DEBUG(10,("Converting %s\n", (const char *)key.dptr));
61 p = strchr((const char *)key.dptr, '/');
62 if (!p)
63 return 0;
65 *p = 0;
66 fstrcpy(dom_name, (const char *)key.dptr);
67 *p++ = '/';
69 domain = find_domain_from_name(dom_name);
70 if (domain == NULL) {
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));
77 *failed = True;
78 return -1;
81 return 0;
84 rid = atoi(p);
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 ));
94 *failed = True;
95 return -1;
98 if (tdb_store(tdb, data, key2, TDB_REPLACE) != 0) {
99 DEBUG(0,("Unable to update record %s\n", (const char *)data.dptr ));
100 *failed = True;
101 return -1;
104 if (tdb_delete(tdb, key) != 0) {
105 DEBUG(0,("Unable to delete record %s\n", (const char *)key.dptr ));
106 *failed = True;
107 return -1;
110 return 0;
113 /*****************************************************************************
114 Convert the idmap database from an older version.
115 *****************************************************************************/
117 static bool idmap_tdb_upgrade(const char *idmap_name)
119 int32 vers;
120 bool bigendianheader;
121 bool failed = False;
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,
127 TDB_DEFAULT, O_RDWR,
128 0600))) {
129 DEBUG(0, ("Unable to open idmap database\n"));
130 return False;
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.
144 int32 wm;
146 wm = tdb_fetch_int32(idmap_tdb, HWM_USER);
148 if (wm != -1) {
149 wm = IREV(wm);
150 } else {
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);
157 return False;
160 wm = tdb_fetch_int32(idmap_tdb, HWM_GROUP);
161 if (wm != -1) {
162 wm = IREV(wm);
163 } else {
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);
170 return False;
174 /* the old format stored as DOMAIN/rid - now we store the SID direct */
175 tdb_traverse(idmap_tdb, convert_fn, &failed);
177 if (failed) {
178 DEBUG(0, ("Problem during conversion\n"));
179 tdb_close(idmap_tdb);
180 return False;
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);
186 return False;
189 tdb_close(idmap_tdb);
190 return True;
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)
203 NTSTATUS ret;
204 TALLOC_CTX *ctx;
205 SMB_STRUCT_STAT stbuf;
206 char *tdbfile = NULL;
207 int32 version;
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;
213 return NT_STATUS_OK;
216 /* use our own context here */
217 ctx = talloc_new(memctx);
218 if (!ctx) {
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"));
225 if (!tdbfile) {
226 DEBUG(0, ("Out of memory!\n"));
227 ret = NT_STATUS_NO_MEMORY;
228 goto done;
231 if (!file_exist(tdbfile, &stbuf)) {
232 tdb_is_new = True;
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;
241 goto done;
244 if (tdb_is_new) {
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;
264 goto done;
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;
271 goto done;
275 *tdbctx = idmap_tdb_common_ctx;
276 idmap_tdb_open_ref_count++;
277 ret = NT_STATUS_OK;
279 done:
280 talloc_free(ctx);
281 return ret;
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!"));
291 return False;
294 idmap_tdb_open_ref_count--;
295 if (idmap_tdb_open_ref_count) {
296 return True;
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 )
314 NTSTATUS ret;
315 TALLOC_CTX *ctx;
316 const char *range;
317 uid_t low_uid = 0;
318 uid_t high_uid = 0;
319 gid_t low_gid = 0;
320 gid_t high_gid = 0;
322 /* use our own context here */
323 ctx = talloc_new(NULL);
324 if (!ctx) {
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)) {
331 talloc_free(ctx);
332 return ret;
335 talloc_free(ctx);
337 /* load ranges */
338 idmap_tdb_state.low_uid = 0;
339 idmap_tdb_state.high_uid = 0;
340 idmap_tdb_state.low_gid = 0;
341 idmap_tdb_state.high_gid = 0;
343 range = lp_parm_const_string(-1, "idmap alloc config", "range", NULL);
344 if (range && range[0]) {
345 unsigned low_id, high_id;
347 if (sscanf(range, "%u - %u", &low_id, &high_id) == 2) {
348 if (low_id < high_id) {
349 idmap_tdb_state.low_gid = idmap_tdb_state.low_uid = low_id;
350 idmap_tdb_state.high_gid = idmap_tdb_state.high_uid = high_id;
351 } else {
352 DEBUG(1, ("ERROR: invalid idmap alloc range [%s]", range));
354 } else {
355 DEBUG(1, ("ERROR: invalid syntax for idmap alloc config:range [%s]", range));
359 /* Create high water marks for group and user id */
360 if (lp_idmap_uid(&low_uid, &high_uid)) {
361 idmap_tdb_state.low_uid = low_uid;
362 idmap_tdb_state.high_uid = high_uid;
365 if (lp_idmap_gid(&low_gid, &high_gid)) {
366 idmap_tdb_state.low_gid = low_gid;
367 idmap_tdb_state.high_gid = high_gid;
370 if (idmap_tdb_state.high_uid <= idmap_tdb_state.low_uid) {
371 DEBUG(1, ("idmap uid range missing or invalid\n"));
372 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
373 return NT_STATUS_UNSUCCESSFUL;
374 } else {
375 uint32 low_id;
377 if (((low_id = tdb_fetch_int32(idmap_alloc_tdb, HWM_USER)) == -1) ||
378 (low_id < idmap_tdb_state.low_uid)) {
379 if (tdb_store_int32(idmap_alloc_tdb, HWM_USER, idmap_tdb_state.low_uid) == -1) {
380 DEBUG(0, ("Unable to initialise user hwm in idmap database\n"));
381 return NT_STATUS_INTERNAL_DB_ERROR;
386 if (idmap_tdb_state.high_gid <= idmap_tdb_state.low_gid) {
387 DEBUG(1, ("idmap gid range missing or invalid\n"));
388 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
389 return NT_STATUS_UNSUCCESSFUL;
390 } else {
391 uint32 low_id;
393 if (((low_id = tdb_fetch_int32(idmap_alloc_tdb, HWM_GROUP)) == -1) ||
394 (low_id < idmap_tdb_state.low_gid)) {
395 if (tdb_store_int32(idmap_alloc_tdb, HWM_GROUP, idmap_tdb_state.low_gid) == -1) {
396 DEBUG(0, ("Unable to initialise group hwm in idmap database\n"));
397 return NT_STATUS_INTERNAL_DB_ERROR;
402 return NT_STATUS_OK;
405 /**********************************
406 Allocate a new id.
407 **********************************/
409 static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
411 bool ret;
412 const char *hwmkey;
413 const char *hwmtype;
414 uint32_t high_hwm;
415 uint32_t hwm;
417 /* Get current high water mark */
418 switch (xid->type) {
420 case ID_TYPE_UID:
421 hwmkey = HWM_USER;
422 hwmtype = "UID";
423 high_hwm = idmap_tdb_state.high_uid;
424 break;
426 case ID_TYPE_GID:
427 hwmkey = HWM_GROUP;
428 hwmtype = "GID";
429 high_hwm = idmap_tdb_state.high_gid;
430 break;
432 default:
433 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
434 return NT_STATUS_INVALID_PARAMETER;
437 if ((hwm = tdb_fetch_int32(idmap_alloc_tdb, hwmkey)) == -1) {
438 return NT_STATUS_INTERNAL_DB_ERROR;
441 /* check it is in the range */
442 if (hwm > high_hwm) {
443 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
444 hwmtype, (unsigned long)high_hwm));
445 return NT_STATUS_UNSUCCESSFUL;
448 /* fetch a new id and increment it */
449 ret = tdb_change_uint32_atomic(idmap_alloc_tdb, hwmkey, &hwm, 1);
450 if (!ret) {
451 DEBUG(1, ("Fatal error while fetching a new %s value\n!", hwmtype));
452 return NT_STATUS_UNSUCCESSFUL;
455 /* recheck it is in the range */
456 if (hwm > high_hwm) {
457 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
458 hwmtype, (unsigned long)high_hwm));
459 return NT_STATUS_UNSUCCESSFUL;
462 xid->id = hwm;
463 DEBUG(10,("New %s = %d\n", hwmtype, hwm));
465 return NT_STATUS_OK;
468 /**********************************
469 Get current highest id.
470 **********************************/
472 static NTSTATUS idmap_tdb_get_hwm(struct unixid *xid)
474 const char *hwmkey;
475 const char *hwmtype;
476 uint32_t hwm;
477 uint32_t high_hwm;
479 /* Get current high water mark */
480 switch (xid->type) {
482 case ID_TYPE_UID:
483 hwmkey = HWM_USER;
484 hwmtype = "UID";
485 high_hwm = idmap_tdb_state.high_uid;
486 break;
488 case ID_TYPE_GID:
489 hwmkey = HWM_GROUP;
490 hwmtype = "GID";
491 high_hwm = idmap_tdb_state.high_gid;
492 break;
494 default:
495 return NT_STATUS_INVALID_PARAMETER;
498 if ((hwm = tdb_fetch_int32(idmap_alloc_tdb, hwmkey)) == -1) {
499 return NT_STATUS_INTERNAL_DB_ERROR;
502 xid->id = hwm;
504 /* Warn if it is out of range */
505 if (hwm >= high_hwm) {
506 DEBUG(0, ("Warning: %s range full!! (max: %lu)\n",
507 hwmtype, (unsigned long)high_hwm));
510 return NT_STATUS_OK;
513 /**********************************
514 Set high id.
515 **********************************/
517 static NTSTATUS idmap_tdb_set_hwm(struct unixid *xid)
519 const char *hwmkey;
520 const char *hwmtype;
521 uint32_t hwm;
522 uint32_t high_hwm;
524 /* Get current high water mark */
525 switch (xid->type) {
527 case ID_TYPE_UID:
528 hwmkey = HWM_USER;
529 hwmtype = "UID";
530 high_hwm = idmap_tdb_state.high_uid;
531 break;
533 case ID_TYPE_GID:
534 hwmkey = HWM_GROUP;
535 hwmtype = "GID";
536 high_hwm = idmap_tdb_state.high_gid;
537 break;
539 default:
540 return NT_STATUS_INVALID_PARAMETER;
543 hwm = xid->id;
545 if ((hwm = tdb_store_int32(idmap_alloc_tdb, hwmkey, hwm)) == -1) {
546 return NT_STATUS_INTERNAL_DB_ERROR;
549 /* Warn if it is out of range */
550 if (hwm >= high_hwm) {
551 DEBUG(0, ("Warning: %s range full!! (max: %lu)\n",
552 hwmtype, (unsigned long)high_hwm));
555 return NT_STATUS_OK;
558 /**********************************
559 Close the alloc tdb
560 **********************************/
562 static NTSTATUS idmap_tdb_alloc_close(void)
564 if (idmap_alloc_tdb) {
565 if (idmap_tdb_tdb_close(idmap_alloc_tdb) == 0) {
566 return NT_STATUS_OK;
567 } else {
568 return NT_STATUS_UNSUCCESSFUL;
571 return NT_STATUS_OK;
574 /**********************************************************************
575 IDMAP MAPPING TDB BACKEND
576 **********************************************************************/
578 struct idmap_tdb_context {
579 TDB_CONTEXT *tdb;
580 uint32_t filter_low_id;
581 uint32_t filter_high_id;
584 /*****************************
585 Initialise idmap database.
586 *****************************/
588 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom)
590 NTSTATUS ret;
591 struct idmap_tdb_context *ctx;
592 char *config_option = NULL;
593 const char *range;
595 ctx = talloc(dom, struct idmap_tdb_context);
596 if ( ! ctx) {
597 DEBUG(0, ("Out of memory!\n"));
598 return NT_STATUS_NO_MEMORY;
601 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
602 if ( ! config_option) {
603 DEBUG(0, ("Out of memory!\n"));
604 ret = NT_STATUS_NO_MEMORY;
605 goto failed;
608 ret = idmap_tdb_open_db(ctx, &ctx->tdb);
609 if ( ! NT_STATUS_IS_OK(ret)) {
610 goto failed;
613 range = lp_parm_const_string(-1, config_option, "range", NULL);
614 if (( ! range) ||
615 (sscanf(range, "%u - %u", &ctx->filter_low_id, &ctx->filter_high_id) != 2) ||
616 (ctx->filter_low_id > ctx->filter_high_id)) {
617 ctx->filter_low_id = 0;
618 ctx->filter_high_id = 0;
621 dom->private_data = ctx;
622 dom->initialized = True;
624 talloc_free(config_option);
625 return NT_STATUS_OK;
627 failed:
628 talloc_free(ctx);
629 return ret;
632 /**********************************
633 Single id to sid lookup function.
634 **********************************/
636 static NTSTATUS idmap_tdb_id_to_sid(struct idmap_tdb_context *ctx, struct id_map *map)
638 NTSTATUS ret;
639 TDB_DATA data;
640 char *keystr;
642 if (!ctx || !map) {
643 return NT_STATUS_INVALID_PARAMETER;
646 /* apply filters before checking */
647 if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
648 (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
649 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
650 map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
651 return NT_STATUS_NONE_MAPPED;
654 switch (map->xid.type) {
656 case ID_TYPE_UID:
657 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
658 break;
660 case ID_TYPE_GID:
661 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
662 break;
664 default:
665 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
666 return NT_STATUS_INVALID_PARAMETER;
669 /* final SAFE_FREE safe */
670 data.dptr = NULL;
672 if (keystr == NULL) {
673 DEBUG(0, ("Out of memory!\n"));
674 ret = NT_STATUS_NO_MEMORY;
675 goto done;
678 DEBUG(10,("Fetching record %s\n", keystr));
680 /* Check if the mapping exists */
681 data = tdb_fetch_bystring(ctx->tdb, keystr);
683 if (!data.dptr) {
684 DEBUG(10,("Record %s not found\n", keystr));
685 ret = NT_STATUS_NONE_MAPPED;
686 goto done;
689 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
690 DEBUG(10,("INVALID SID (%s) in record %s\n",
691 (const char *)data.dptr, keystr));
692 ret = NT_STATUS_INTERNAL_DB_ERROR;
693 goto done;
696 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
697 ret = NT_STATUS_OK;
699 done:
700 SAFE_FREE(data.dptr);
701 talloc_free(keystr);
702 return ret;
705 /**********************************
706 Single sid to id lookup function.
707 **********************************/
709 static NTSTATUS idmap_tdb_sid_to_id(struct idmap_tdb_context *ctx, struct id_map *map)
711 NTSTATUS ret;
712 TDB_DATA data;
713 char *keystr;
714 unsigned long rec_id = 0;
715 fstring tmp;
717 if ((keystr = talloc_asprintf(
718 ctx, "%s", sid_to_fstring(tmp, map->sid))) == NULL) {
719 DEBUG(0, ("Out of memory!\n"));
720 ret = NT_STATUS_NO_MEMORY;
721 goto done;
724 DEBUG(10,("Fetching record %s\n", keystr));
726 /* Check if sid is present in database */
727 data = tdb_fetch_bystring(ctx->tdb, keystr);
728 if (!data.dptr) {
729 DEBUG(10,("Record %s not found\n", keystr));
730 ret = NT_STATUS_NONE_MAPPED;
731 goto done;
734 /* What type of record is this ? */
735 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
736 map->xid.id = rec_id;
737 map->xid.type = ID_TYPE_UID;
738 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
739 ret = NT_STATUS_OK;
741 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
742 map->xid.id = rec_id;
743 map->xid.type = ID_TYPE_GID;
744 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
745 ret = NT_STATUS_OK;
747 } else { /* Unknown record type ! */
748 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
749 ret = NT_STATUS_INTERNAL_DB_ERROR;
752 SAFE_FREE(data.dptr);
754 /* apply filters before returning result */
755 if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
756 (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
757 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
758 map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
759 ret = NT_STATUS_NONE_MAPPED;
762 done:
763 talloc_free(keystr);
764 return ret;
767 /**********************************
768 lookup a set of unix ids.
769 **********************************/
771 static NTSTATUS idmap_tdb_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
773 struct idmap_tdb_context *ctx;
774 NTSTATUS ret;
775 int i;
777 /* make sure we initialized */
778 if ( ! dom->initialized) {
779 ret = idmap_tdb_db_init(dom);
780 if ( ! NT_STATUS_IS_OK(ret)) {
781 return ret;
785 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
787 for (i = 0; ids[i]; i++) {
788 ret = idmap_tdb_id_to_sid(ctx, ids[i]);
789 if ( ! NT_STATUS_IS_OK(ret)) {
791 /* if it is just a failed mapping continue */
792 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
794 /* make sure it is marked as unmapped */
795 ids[i]->status = ID_UNMAPPED;
796 continue;
799 /* some fatal error occurred, return immediately */
800 goto done;
803 /* all ok, id is mapped */
804 ids[i]->status = ID_MAPPED;
807 ret = NT_STATUS_OK;
809 done:
810 return ret;
813 /**********************************
814 lookup a set of sids.
815 **********************************/
817 static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
819 struct idmap_tdb_context *ctx;
820 NTSTATUS ret;
821 int i;
823 /* make sure we initialized */
824 if ( ! dom->initialized) {
825 ret = idmap_tdb_db_init(dom);
826 if ( ! NT_STATUS_IS_OK(ret)) {
827 return ret;
831 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
833 for (i = 0; ids[i]; i++) {
834 ret = idmap_tdb_sid_to_id(ctx, ids[i]);
835 if ( ! NT_STATUS_IS_OK(ret)) {
837 /* if it is just a failed mapping continue */
838 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
840 /* make sure it is marked as unmapped */
841 ids[i]->status = ID_UNMAPPED;
842 continue;
845 /* some fatal error occurred, return immediately */
846 goto done;
849 /* all ok, id is mapped */
850 ids[i]->status = ID_MAPPED;
853 ret = NT_STATUS_OK;
855 done:
856 return ret;
859 /**********************************
860 set a mapping.
861 **********************************/
863 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom, const struct id_map *map)
865 struct idmap_tdb_context *ctx;
866 NTSTATUS ret;
867 TDB_DATA ksid, kid, data;
868 char *ksidstr, *kidstr;
869 fstring tmp;
871 /* make sure we initialized */
872 if ( ! dom->initialized) {
873 ret = idmap_tdb_db_init(dom);
874 if ( ! NT_STATUS_IS_OK(ret)) {
875 return ret;
879 if (!map || !map->sid) {
880 return NT_STATUS_INVALID_PARAMETER;
883 ksidstr = kidstr = NULL;
884 data.dptr = NULL;
886 /* TODO: should we filter a set_mapping using low/high filters ? */
888 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
890 switch (map->xid.type) {
892 case ID_TYPE_UID:
893 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
894 break;
896 case ID_TYPE_GID:
897 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
898 break;
900 default:
901 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
902 return NT_STATUS_INVALID_PARAMETER;
905 if (kidstr == NULL) {
906 DEBUG(0, ("ERROR: Out of memory!\n"));
907 ret = NT_STATUS_NO_MEMORY;
908 goto done;
911 if ((ksidstr = talloc_asprintf(
912 ctx, "%s", sid_to_fstring(tmp, map->sid))) == NULL) {
913 DEBUG(0, ("Out of memory!\n"));
914 ret = NT_STATUS_NO_MEMORY;
915 goto done;
918 DEBUG(10, ("Storing %s <-> %s map\n", ksidstr, kidstr));
919 kid = string_term_tdb_data(kidstr);
920 ksid = string_term_tdb_data(ksidstr);
922 /* *DELETE* previous mappings if any.
923 * This is done both SID and [U|G]ID passed in */
925 /* Lock the record for this SID. */
926 if (tdb_chainlock(ctx->tdb, ksid) != 0) {
927 DEBUG(10,("Failed to lock record %s. Error %s\n",
928 ksidstr, tdb_errorstr(ctx->tdb) ));
929 return NT_STATUS_UNSUCCESSFUL;
932 data = tdb_fetch(ctx->tdb, ksid);
933 if (data.dptr) {
934 DEBUG(10, ("Deleting existing mapping %s <-> %s\n", (const char *)data.dptr, ksidstr ));
935 tdb_delete(ctx->tdb, data);
936 tdb_delete(ctx->tdb, ksid);
937 SAFE_FREE(data.dptr);
940 data = tdb_fetch(ctx->tdb, kid);
941 if (data.dptr) {
942 DEBUG(10,("Deleting existing mapping %s <-> %s\n", (const char *)data.dptr, kidstr ));
943 tdb_delete(ctx->tdb, data);
944 tdb_delete(ctx->tdb, kid);
945 SAFE_FREE(data.dptr);
948 if (tdb_store(ctx->tdb, ksid, kid, TDB_INSERT) == -1) {
949 DEBUG(0, ("Error storing SID -> ID: %s\n", tdb_errorstr(ctx->tdb)));
950 tdb_chainunlock(ctx->tdb, ksid);
951 ret = NT_STATUS_UNSUCCESSFUL;
952 goto done;
954 if (tdb_store(ctx->tdb, kid, ksid, TDB_INSERT) == -1) {
955 DEBUG(0, ("Error stroing ID -> SID: %s\n", tdb_errorstr(ctx->tdb)));
956 /* try to remove the previous stored SID -> ID map */
957 tdb_delete(ctx->tdb, ksid);
958 tdb_chainunlock(ctx->tdb, ksid);
959 ret = NT_STATUS_UNSUCCESSFUL;
960 goto done;
963 tdb_chainunlock(ctx->tdb, ksid);
964 DEBUG(10,("Stored %s <-> %s\n", ksidstr, kidstr));
965 ret = NT_STATUS_OK;
967 done:
968 talloc_free(ksidstr);
969 talloc_free(kidstr);
970 SAFE_FREE(data.dptr);
971 return ret;
974 /**********************************
975 remove a mapping.
976 **********************************/
978 static NTSTATUS idmap_tdb_remove_mapping(struct idmap_domain *dom, const struct id_map *map)
980 struct idmap_tdb_context *ctx;
981 NTSTATUS ret;
982 TDB_DATA ksid, kid, data;
983 char *ksidstr, *kidstr;
984 fstring tmp;
986 /* make sure we initialized */
987 if ( ! dom->initialized) {
988 ret = idmap_tdb_db_init(dom);
989 if ( ! NT_STATUS_IS_OK(ret)) {
990 return ret;
994 if (!map || !map->sid) {
995 return NT_STATUS_INVALID_PARAMETER;
998 ksidstr = kidstr = NULL;
999 data.dptr = NULL;
1001 /* TODO: should we filter a remove_mapping using low/high filters ? */
1003 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
1005 switch (map->xid.type) {
1007 case ID_TYPE_UID:
1008 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
1009 break;
1011 case ID_TYPE_GID:
1012 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
1013 break;
1015 default:
1016 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
1017 return NT_STATUS_INVALID_PARAMETER;
1020 if (kidstr == NULL) {
1021 DEBUG(0, ("ERROR: Out of memory!\n"));
1022 ret = NT_STATUS_NO_MEMORY;
1023 goto done;
1026 if ((ksidstr = talloc_asprintf(
1027 ctx, "%s", sid_to_fstring(tmp, map->sid))) == NULL) {
1028 DEBUG(0, ("Out of memory!\n"));
1029 ret = NT_STATUS_NO_MEMORY;
1030 goto done;
1033 DEBUG(10, ("Checking %s <-> %s map\n", ksidstr, kidstr));
1034 ksid = string_term_tdb_data(ksidstr);
1035 kid = string_term_tdb_data(kidstr);
1037 /* Lock the record for this SID. */
1038 if (tdb_chainlock(ctx->tdb, ksid) != 0) {
1039 DEBUG(10,("Failed to lock record %s. Error %s\n",
1040 ksidstr, tdb_errorstr(ctx->tdb) ));
1041 return NT_STATUS_UNSUCCESSFUL;
1044 /* Check if sid is present in database */
1045 data = tdb_fetch(ctx->tdb, ksid);
1046 if (!data.dptr) {
1047 DEBUG(10,("Record %s not found\n", ksidstr));
1048 tdb_chainunlock(ctx->tdb, ksid);
1049 ret = NT_STATUS_NONE_MAPPED;
1050 goto done;
1053 /* Check if sid is mapped to the specified ID */
1054 if ((data.dsize != kid.dsize) ||
1055 (memcmp(data.dptr, kid.dptr, data.dsize) != 0)) {
1056 DEBUG(10,("Specified SID does not map to specified ID\n"));
1057 DEBUGADD(10,("Actual mapping is %s -> %s\n", ksidstr, (const char *)data.dptr));
1058 tdb_chainunlock(ctx->tdb, ksid);
1059 ret = NT_STATUS_NONE_MAPPED;
1060 goto done;
1063 DEBUG(10, ("Removing %s <-> %s map\n", ksidstr, kidstr));
1065 /* Delete previous mappings. */
1067 DEBUG(10, ("Deleting existing mapping %s -> %s\n", ksidstr, kidstr ));
1068 tdb_delete(ctx->tdb, ksid);
1070 DEBUG(10,("Deleting existing mapping %s -> %s\n", kidstr, ksidstr ));
1071 tdb_delete(ctx->tdb, kid);
1073 tdb_chainunlock(ctx->tdb, ksid);
1074 ret = NT_STATUS_OK;
1076 done:
1077 talloc_free(ksidstr);
1078 talloc_free(kidstr);
1079 SAFE_FREE(data.dptr);
1080 return ret;
1083 /**********************************
1084 Close the idmap tdb instance
1085 **********************************/
1087 static NTSTATUS idmap_tdb_close(struct idmap_domain *dom)
1089 struct idmap_tdb_context *ctx;
1091 if (dom->private_data) {
1092 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
1094 if (idmap_tdb_tdb_close(ctx->tdb) == 0) {
1095 return NT_STATUS_OK;
1096 } else {
1097 return NT_STATUS_UNSUCCESSFUL;
1100 return NT_STATUS_OK;
1103 struct dump_data {
1104 TALLOC_CTX *memctx;
1105 struct id_map **maps;
1106 int *num_maps;
1107 NTSTATUS ret;
1110 static int idmap_tdb_dump_one_entry(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA value, void *pdata)
1112 struct dump_data *data = talloc_get_type(pdata, struct dump_data);
1113 struct id_map *maps;
1114 int num_maps = *data->num_maps;
1116 /* ignore any record but the ones with a SID as key */
1117 if (strncmp((const char *)key.dptr, "S-", 2) == 0) {
1119 maps = talloc_realloc(NULL, *data->maps, struct id_map, num_maps+1);
1120 if ( ! maps) {
1121 DEBUG(0, ("Out of memory!\n"));
1122 data->ret = NT_STATUS_NO_MEMORY;
1123 return -1;
1125 *data->maps = maps;
1126 maps[num_maps].sid = talloc(maps, DOM_SID);
1127 if ( ! maps[num_maps].sid) {
1128 DEBUG(0, ("Out of memory!\n"));
1129 data->ret = NT_STATUS_NO_MEMORY;
1130 return -1;
1133 if (!string_to_sid(maps[num_maps].sid, (const char *)key.dptr)) {
1134 DEBUG(10,("INVALID record %s\n", (const char *)key.dptr));
1135 /* continue even with errors */
1136 return 0;
1139 /* Try a UID record. */
1140 if (sscanf((const char *)value.dptr, "UID %u", &(maps[num_maps].xid.id)) == 1) {
1141 maps[num_maps].xid.type = ID_TYPE_UID;
1142 maps[num_maps].status = ID_MAPPED;
1143 *data->num_maps = num_maps + 1;
1145 /* Try a GID record. */
1146 } else
1147 if (sscanf((const char *)value.dptr, "GID %u", &(maps[num_maps].xid.id)) == 1) {
1148 maps[num_maps].xid.type = ID_TYPE_GID;
1149 maps[num_maps].status = ID_MAPPED;
1150 *data->num_maps = num_maps + 1;
1152 /* Unknown record type ! */
1153 } else {
1154 maps[num_maps].status = ID_UNKNOWN;
1155 DEBUG(2, ("Found INVALID record %s -> %s\n",
1156 (const char *)key.dptr, (const char *)value.dptr));
1157 /* do not increment num_maps */
1161 return 0;
1164 /**********************************
1165 Dump all mappings out
1166 **********************************/
1168 static NTSTATUS idmap_tdb_dump_data(struct idmap_domain *dom, struct id_map **maps, int *num_maps)
1170 struct idmap_tdb_context *ctx;
1171 struct dump_data *data;
1172 NTSTATUS ret = NT_STATUS_OK;
1174 /* make sure we initialized */
1175 if ( ! dom->initialized) {
1176 ret = idmap_tdb_db_init(dom);
1177 if ( ! NT_STATUS_IS_OK(ret)) {
1178 return ret;
1182 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
1184 data = TALLOC_ZERO_P(ctx, struct dump_data);
1185 if ( ! data) {
1186 DEBUG(0, ("Out of memory!\n"));
1187 return NT_STATUS_NO_MEMORY;
1189 data->maps = maps;
1190 data->num_maps = num_maps;
1191 data->ret = NT_STATUS_OK;
1193 tdb_traverse(ctx->tdb, idmap_tdb_dump_one_entry, data);
1195 if ( ! NT_STATUS_IS_OK(data->ret)) {
1196 ret = data->ret;
1199 talloc_free(data);
1200 return ret;
1203 static struct idmap_methods db_methods = {
1205 .init = idmap_tdb_db_init,
1206 .unixids_to_sids = idmap_tdb_unixids_to_sids,
1207 .sids_to_unixids = idmap_tdb_sids_to_unixids,
1208 .set_mapping = idmap_tdb_set_mapping,
1209 .remove_mapping = idmap_tdb_remove_mapping,
1210 .dump_data = idmap_tdb_dump_data,
1211 .close_fn = idmap_tdb_close
1214 static struct idmap_alloc_methods db_alloc_methods = {
1216 .init = idmap_tdb_alloc_init,
1217 .allocate_id = idmap_tdb_allocate_id,
1218 .get_id_hwm = idmap_tdb_get_hwm,
1219 .set_id_hwm = idmap_tdb_set_hwm,
1220 .close_fn = idmap_tdb_alloc_close
1223 NTSTATUS idmap_alloc_tdb_init(void)
1225 return smb_register_idmap_alloc(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_alloc_methods);
1228 NTSTATUS idmap_tdb_init(void)
1230 NTSTATUS ret;
1232 /* FIXME: bad hack to actually register also the alloc_tdb module without changining configure.in */
1233 ret = idmap_alloc_tdb_init();
1234 if (! NT_STATUS_IS_OK(ret)) {
1235 return ret;
1237 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);