s3:idmap_tdb: use transactions in idmap_tdb_allocate_id()
[Samba/gebeck_regimport.git] / source3 / winbindd / idmap_tdb.c
blob8e7137d579d39f1b2916227e0431186a2dd97499
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 /* idmap version determines auto-conversion - this is the database
32 structure version specifier. */
34 #define IDMAP_VERSION 2
36 /* High water mark keys */
37 #define HWM_GROUP "GROUP HWM"
38 #define HWM_USER "USER HWM"
40 static struct idmap_tdb_state {
42 /* User and group id pool */
43 uid_t low_uid, high_uid; /* Range of uids to allocate */
44 gid_t low_gid, high_gid; /* Range of gids to allocate */
46 } idmap_tdb_state;
48 struct convert_fn_state {
49 struct db_context *db;
50 bool failed;
53 /*****************************************************************************
54 For idmap conversion: convert one record to new format
55 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
56 instead of the SID.
57 *****************************************************************************/
58 static int convert_fn(struct db_record *rec, void *private_data)
60 struct winbindd_domain *domain;
61 char *p;
62 NTSTATUS status;
63 DOM_SID sid;
64 uint32 rid;
65 fstring keystr;
66 fstring dom_name;
67 TDB_DATA key2;
68 struct convert_fn_state *s = (struct convert_fn_state *)private_data;
70 DEBUG(10,("Converting %s\n", (const char *)rec->key.dptr));
72 p = strchr((const char *)rec->key.dptr, '/');
73 if (!p)
74 return 0;
76 *p = 0;
77 fstrcpy(dom_name, (const char *)rec->key.dptr);
78 *p++ = '/';
80 domain = find_domain_from_name(dom_name);
81 if (domain == NULL) {
82 /* We must delete the old record. */
83 DEBUG(0,("Unable to find domain %s\n", dom_name ));
84 DEBUG(0,("deleting record %s\n", (const char *)rec->key.dptr ));
86 status = rec->delete_rec(rec);
87 if (!NT_STATUS_IS_OK(status)) {
88 DEBUG(0, ("Unable to delete record %s:%s\n",
89 (const char *)rec->key.dptr,
90 nt_errstr(status)));
91 s->failed = true;
92 return -1;
95 return 0;
98 rid = atoi(p);
100 sid_copy(&sid, &domain->sid);
101 sid_append_rid(&sid, rid);
103 sid_to_fstring(keystr, &sid);
104 key2 = string_term_tdb_data(keystr);
106 status = dbwrap_store(s->db, key2, rec->value, TDB_INSERT);
107 if (!NT_STATUS_IS_OK(status)) {
108 DEBUG(0,("Unable to add record %s:%s\n",
109 (const char *)key2.dptr,
110 nt_errstr(status)));
111 s->failed = true;
112 return -1;
115 status = dbwrap_store(s->db, rec->value, key2, TDB_REPLACE);
116 if (!NT_STATUS_IS_OK(status)) {
117 DEBUG(0,("Unable to update record %s:%s\n",
118 (const char *)rec->value.dptr,
119 nt_errstr(status)));
120 s->failed = true;
121 return -1;
124 status = rec->delete_rec(rec);
125 if (!NT_STATUS_IS_OK(status)) {
126 DEBUG(0,("Unable to delete record %s:%s\n",
127 (const char *)rec->key.dptr,
128 nt_errstr(status)));
129 s->failed = true;
130 return -1;
133 return 0;
136 /*****************************************************************************
137 Convert the idmap database from an older version.
138 *****************************************************************************/
140 static bool idmap_tdb_upgrade(struct db_context *db)
142 int32 vers;
143 bool bigendianheader;
144 struct convert_fn_state s;
146 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
148 bigendianheader = (db->get_flags(db) & TDB_BIGENDIAN) ? True : False;
150 vers = dbwrap_fetch_int32(db, "IDMAP_VERSION");
152 if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
153 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
155 * high and low records were created on a
156 * big endian machine and will need byte-reversing.
159 int32 wm;
161 wm = dbwrap_fetch_int32(db, HWM_USER);
163 if (wm != -1) {
164 wm = IREV(wm);
165 } else {
166 wm = idmap_tdb_state.low_uid;
169 if (dbwrap_store_int32(db, HWM_USER, wm) == -1) {
170 DEBUG(0, ("Unable to byteswap user hwm in idmap database\n"));
171 return False;
174 wm = dbwrap_fetch_int32(db, HWM_GROUP);
175 if (wm != -1) {
176 wm = IREV(wm);
177 } else {
178 wm = idmap_tdb_state.low_gid;
181 if (dbwrap_store_int32(db, HWM_GROUP, wm) == -1) {
182 DEBUG(0, ("Unable to byteswap group hwm in idmap database\n"));
183 return False;
187 s.db = db;
188 s.failed = false;
190 /* the old format stored as DOMAIN/rid - now we store the SID direct */
191 db->traverse(db, convert_fn, &s);
193 if (s.failed) {
194 DEBUG(0, ("Problem during conversion\n"));
195 return False;
198 if (dbwrap_store_int32(db, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
199 DEBUG(0, ("Unable to dtore idmap version in databse\n"));
200 return False;
203 return True;
206 static NTSTATUS idmap_tdb_open_db(TALLOC_CTX *memctx,
207 bool check_config,
208 struct db_context **dbctx)
210 NTSTATUS ret;
211 TALLOC_CTX *ctx;
212 char *tdbfile = NULL;
213 struct db_context *db = NULL;
214 int32_t version;
215 uid_t low_uid = 0;
216 uid_t high_uid = 0;
217 gid_t low_gid = 0;
218 gid_t high_gid = 0;
219 bool config_error = false;
221 /* load ranges */
222 if (!lp_idmap_uid(&low_uid, &high_uid)
223 || !lp_idmap_gid(&low_gid, &high_gid)) {
224 DEBUG(1, ("idmap uid or idmap gid missing\n"));
225 config_error = true;
226 if (check_config) {
227 return NT_STATUS_UNSUCCESSFUL;
231 idmap_tdb_state.low_uid = low_uid;
232 idmap_tdb_state.high_uid = high_uid;
233 idmap_tdb_state.low_gid = low_gid;
234 idmap_tdb_state.high_gid = high_gid;
236 if (idmap_tdb_state.high_uid <= idmap_tdb_state.low_uid) {
237 DEBUG(1, ("idmap uid range missing or invalid\n"));
238 config_error = true;
239 if (check_config) {
240 return NT_STATUS_UNSUCCESSFUL;
244 if (idmap_tdb_state.high_gid <= idmap_tdb_state.low_gid) {
245 DEBUG(1, ("idmap gid range missing or invalid\n"));
246 config_error = true;
247 if (check_config) {
248 return NT_STATUS_UNSUCCESSFUL;
252 /* use our own context here */
253 ctx = talloc_new(memctx);
254 if (!ctx) {
255 DEBUG(0, ("Out of memory!\n"));
256 return NT_STATUS_NO_MEMORY;
259 /* use the old database if present */
260 tdbfile = talloc_strdup(ctx, state_path("winbindd_idmap.tdb"));
261 if (!tdbfile) {
262 DEBUG(0, ("Out of memory!\n"));
263 ret = NT_STATUS_NO_MEMORY;
264 goto done;
267 DEBUG(10,("Opening tdbfile %s\n", tdbfile ));
269 /* Open idmap repository */
270 db = db_open(ctx, tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
271 if (!db) {
272 DEBUG(0, ("Unable to open idmap database\n"));
273 ret = NT_STATUS_UNSUCCESSFUL;
274 goto done;
277 /* check against earlier versions */
278 version = dbwrap_fetch_int32(db, "IDMAP_VERSION");
279 if (version != IDMAP_VERSION) {
280 if (config_error) {
281 DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
282 "possible with incomplete configuration\n",
283 version, IDMAP_VERSION));
284 ret = NT_STATUS_UNSUCCESSFUL;
285 goto done;
287 if (db->transaction_start(db) != 0) {
288 DEBUG(0, ("Unable to start upgrade transaction!\n"));
289 ret = NT_STATUS_INTERNAL_DB_ERROR;
290 goto done;
293 if (!idmap_tdb_upgrade(db)) {
294 db->transaction_cancel(db);
295 DEBUG(0, ("Unable to open idmap database, it's in an old formati, and upgrade failed!\n"));
296 ret = NT_STATUS_INTERNAL_DB_ERROR;
297 goto done;
300 if (db->transaction_commit(db) != 0) {
301 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
302 ret = NT_STATUS_INTERNAL_DB_ERROR;
303 goto done;
307 *dbctx = talloc_move(memctx, &db);
308 ret = NT_STATUS_OK;
310 done:
311 talloc_free(ctx);
312 return ret;
315 /**********************************************************************
316 IDMAP ALLOC TDB BACKEND
317 **********************************************************************/
319 static struct db_context *idmap_alloc_db;
321 /**********************************
322 Initialise idmap alloc database.
323 **********************************/
325 static NTSTATUS idmap_tdb_alloc_init( const char *params )
327 int ret;
328 NTSTATUS status;
329 uint32_t low_uid;
330 uint32_t low_gid;
331 bool update_uid = false;
332 bool update_gid = false;
334 status = idmap_tdb_open_db(NULL, true, &idmap_alloc_db);
335 if (!NT_STATUS_IS_OK(status)) {
336 DEBUG(0, ("idmap will be unable to map foreign SIDs: %s\n",
337 nt_errstr(status)));
338 return status;
341 low_uid = dbwrap_fetch_int32(idmap_alloc_db, HWM_USER);
342 if (low_uid == -1 || low_uid < idmap_tdb_state.low_uid) {
343 update_uid = true;
346 low_gid = dbwrap_fetch_int32(idmap_alloc_db, HWM_GROUP);
347 if (low_gid == -1 || low_gid < idmap_tdb_state.low_gid) {
348 update_gid = true;
351 if (!update_uid && !update_gid) {
352 return NT_STATUS_OK;
355 if (idmap_alloc_db->transaction_start(idmap_alloc_db) != 0) {
356 TALLOC_FREE(idmap_alloc_db);
357 DEBUG(0, ("Unable to start upgrade transaction!\n"));
358 return NT_STATUS_INTERNAL_DB_ERROR;
361 if (update_uid) {
362 ret = dbwrap_store_int32(idmap_alloc_db, HWM_USER,
363 idmap_tdb_state.low_uid);
364 if (ret == -1) {
365 idmap_alloc_db->transaction_cancel(idmap_alloc_db);
366 TALLOC_FREE(idmap_alloc_db);
367 DEBUG(0, ("Unable to initialise user hwm in idmap "
368 "database\n"));
369 return NT_STATUS_INTERNAL_DB_ERROR;
373 if (update_gid) {
374 ret = dbwrap_store_int32(idmap_alloc_db, HWM_GROUP,
375 idmap_tdb_state.low_gid);
376 if (ret == -1) {
377 idmap_alloc_db->transaction_cancel(idmap_alloc_db);
378 TALLOC_FREE(idmap_alloc_db);
379 DEBUG(0, ("Unable to initialise group hwm in idmap "
380 "database\n"));
381 return NT_STATUS_INTERNAL_DB_ERROR;
385 if (idmap_alloc_db->transaction_commit(idmap_alloc_db) != 0) {
386 TALLOC_FREE(idmap_alloc_db);
387 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
388 return NT_STATUS_INTERNAL_DB_ERROR;
391 return NT_STATUS_OK;
394 /**********************************
395 Allocate a new id.
396 **********************************/
398 static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
400 bool ret;
401 const char *hwmkey;
402 const char *hwmtype;
403 uint32_t high_hwm;
404 uint32_t hwm;
405 int res;
407 /* Get current high water mark */
408 switch (xid->type) {
410 case ID_TYPE_UID:
411 hwmkey = HWM_USER;
412 hwmtype = "UID";
413 high_hwm = idmap_tdb_state.high_uid;
414 break;
416 case ID_TYPE_GID:
417 hwmkey = HWM_GROUP;
418 hwmtype = "GID";
419 high_hwm = idmap_tdb_state.high_gid;
420 break;
422 default:
423 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
424 return NT_STATUS_INVALID_PARAMETER;
427 res = idmap_alloc_db->transaction_start(idmap_alloc_db);
428 if (res != 0) {
429 DEBUG(1, (__location__ " Failed to start transaction.\n"));
430 return NT_STATUS_UNSUCCESSFUL;
433 if ((hwm = dbwrap_fetch_int32(idmap_alloc_db, hwmkey)) == -1) {
434 idmap_alloc_db->transaction_cancel(idmap_alloc_db);
435 return NT_STATUS_INTERNAL_DB_ERROR;
438 /* check it is in the range */
439 if (hwm > high_hwm) {
440 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
441 hwmtype, (unsigned long)high_hwm));
442 idmap_alloc_db->transaction_cancel(idmap_alloc_db);
443 return NT_STATUS_UNSUCCESSFUL;
446 /* fetch a new id and increment it */
447 ret = dbwrap_change_uint32_atomic(idmap_alloc_db, hwmkey, &hwm, 1);
448 if (ret != 0) {
449 DEBUG(0, ("Fatal error while fetching a new %s value\n!", hwmtype));
450 idmap_alloc_db->transaction_cancel(idmap_alloc_db);
451 return NT_STATUS_UNSUCCESSFUL;
454 /* recheck it is in the range */
455 if (hwm > high_hwm) {
456 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
457 hwmtype, (unsigned long)high_hwm));
458 idmap_alloc_db->transaction_cancel(idmap_alloc_db);
459 return NT_STATUS_UNSUCCESSFUL;
462 res = idmap_alloc_db->transaction_commit(idmap_alloc_db);
463 if (res != 0) {
464 DEBUG(1, (__location__ " Failed to commit transaction.\n"));
465 return NT_STATUS_UNSUCCESSFUL;
468 xid->id = hwm;
469 DEBUG(10,("New %s = %d\n", hwmtype, hwm));
471 return NT_STATUS_OK;
474 /**********************************
475 Get current highest id.
476 **********************************/
478 static NTSTATUS idmap_tdb_get_hwm(struct unixid *xid)
480 const char *hwmkey;
481 const char *hwmtype;
482 uint32_t hwm;
483 uint32_t high_hwm;
485 /* Get current high water mark */
486 switch (xid->type) {
488 case ID_TYPE_UID:
489 hwmkey = HWM_USER;
490 hwmtype = "UID";
491 high_hwm = idmap_tdb_state.high_uid;
492 break;
494 case ID_TYPE_GID:
495 hwmkey = HWM_GROUP;
496 hwmtype = "GID";
497 high_hwm = idmap_tdb_state.high_gid;
498 break;
500 default:
501 return NT_STATUS_INVALID_PARAMETER;
504 if ((hwm = dbwrap_fetch_int32(idmap_alloc_db, hwmkey)) == -1) {
505 return NT_STATUS_INTERNAL_DB_ERROR;
508 xid->id = hwm;
510 /* Warn if it is out of range */
511 if (hwm >= high_hwm) {
512 DEBUG(0, ("Warning: %s range full!! (max: %lu)\n",
513 hwmtype, (unsigned long)high_hwm));
516 return NT_STATUS_OK;
519 /**********************************
520 Set high id.
521 **********************************/
523 static NTSTATUS idmap_tdb_set_hwm(struct unixid *xid)
525 const char *hwmkey;
526 const char *hwmtype;
527 uint32_t hwm;
528 uint32_t high_hwm;
530 /* Get current high water mark */
531 switch (xid->type) {
533 case ID_TYPE_UID:
534 hwmkey = HWM_USER;
535 hwmtype = "UID";
536 high_hwm = idmap_tdb_state.high_uid;
537 break;
539 case ID_TYPE_GID:
540 hwmkey = HWM_GROUP;
541 hwmtype = "GID";
542 high_hwm = idmap_tdb_state.high_gid;
543 break;
545 default:
546 return NT_STATUS_INVALID_PARAMETER;
549 hwm = xid->id;
551 if ((hwm = dbwrap_store_uint32(idmap_alloc_db, hwmkey, hwm)) == -1) {
552 return NT_STATUS_INTERNAL_DB_ERROR;
555 /* Warn if it is out of range */
556 if (hwm >= high_hwm) {
557 DEBUG(0, ("Warning: %s range full!! (max: %lu)\n",
558 hwmtype, (unsigned long)high_hwm));
561 return NT_STATUS_OK;
564 /**********************************
565 Close the alloc tdb
566 **********************************/
568 static NTSTATUS idmap_tdb_alloc_close(void)
570 TALLOC_FREE(idmap_alloc_db);
571 return NT_STATUS_OK;
574 /**********************************************************************
575 IDMAP MAPPING TDB BACKEND
576 **********************************************************************/
578 struct idmap_tdb_context {
579 struct db_context *db;
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, const char *params)
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, false, &ctx->db);
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;
623 talloc_free(config_option);
624 return NT_STATUS_OK;
626 failed:
627 talloc_free(ctx);
628 return ret;
631 /**********************************
632 Single id to sid lookup function.
633 **********************************/
635 static NTSTATUS idmap_tdb_id_to_sid(struct idmap_tdb_context *ctx, struct id_map *map)
637 NTSTATUS ret;
638 TDB_DATA data;
639 char *keystr;
641 if (!ctx || !map) {
642 return NT_STATUS_INVALID_PARAMETER;
645 /* apply filters before checking */
646 if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
647 (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
648 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
649 map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
650 return NT_STATUS_NONE_MAPPED;
653 switch (map->xid.type) {
655 case ID_TYPE_UID:
656 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
657 break;
659 case ID_TYPE_GID:
660 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
661 break;
663 default:
664 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
665 return NT_STATUS_INVALID_PARAMETER;
668 /* final SAFE_FREE safe */
669 data.dptr = NULL;
671 if (keystr == NULL) {
672 DEBUG(0, ("Out of memory!\n"));
673 ret = NT_STATUS_NO_MEMORY;
674 goto done;
677 DEBUG(10,("Fetching record %s\n", keystr));
679 /* Check if the mapping exists */
680 data = dbwrap_fetch_bystring(ctx->db, NULL, keystr);
682 if (!data.dptr) {
683 DEBUG(10,("Record %s not found\n", keystr));
684 ret = NT_STATUS_NONE_MAPPED;
685 goto done;
688 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
689 DEBUG(10,("INVALID SID (%s) in record %s\n",
690 (const char *)data.dptr, keystr));
691 ret = NT_STATUS_INTERNAL_DB_ERROR;
692 goto done;
695 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
696 ret = NT_STATUS_OK;
698 done:
699 talloc_free(data.dptr);
700 talloc_free(keystr);
701 return ret;
704 /**********************************
705 Single sid to id lookup function.
706 **********************************/
708 static NTSTATUS idmap_tdb_sid_to_id(struct idmap_tdb_context *ctx, struct id_map *map)
710 NTSTATUS ret;
711 TDB_DATA data;
712 char *keystr;
713 unsigned long rec_id = 0;
714 TALLOC_CTX *tmp_ctx = talloc_stackframe();
716 keystr = sid_string_talloc(tmp_ctx, map->sid);
717 if (keystr == NULL) {
718 DEBUG(0, ("Out of memory!\n"));
719 ret = NT_STATUS_NO_MEMORY;
720 goto done;
723 DEBUG(10,("Fetching record %s\n", keystr));
725 /* Check if sid is present in database */
726 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
727 if (!data.dptr) {
728 DEBUG(10,("Record %s not found\n", keystr));
729 ret = NT_STATUS_NONE_MAPPED;
730 goto done;
733 /* What type of record is this ? */
734 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
735 map->xid.id = rec_id;
736 map->xid.type = ID_TYPE_UID;
737 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
738 ret = NT_STATUS_OK;
740 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
741 map->xid.id = rec_id;
742 map->xid.type = ID_TYPE_GID;
743 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
744 ret = NT_STATUS_OK;
746 } else { /* Unknown record type ! */
747 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
748 ret = NT_STATUS_INTERNAL_DB_ERROR;
751 /* apply filters before returning result */
752 if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
753 (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
754 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
755 map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
756 ret = NT_STATUS_NONE_MAPPED;
759 done:
760 talloc_free(tmp_ctx);
761 return ret;
764 /**********************************
765 lookup a set of unix ids.
766 **********************************/
768 static NTSTATUS idmap_tdb_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
770 struct idmap_tdb_context *ctx;
771 NTSTATUS ret;
772 int i;
774 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
776 for (i = 0; ids[i]; i++) {
777 ret = idmap_tdb_id_to_sid(ctx, ids[i]);
778 if ( ! NT_STATUS_IS_OK(ret)) {
780 /* if it is just a failed mapping continue */
781 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
783 /* make sure it is marked as unmapped */
784 ids[i]->status = ID_UNMAPPED;
785 continue;
788 /* some fatal error occurred, return immediately */
789 goto done;
792 /* all ok, id is mapped */
793 ids[i]->status = ID_MAPPED;
796 ret = NT_STATUS_OK;
798 done:
799 return ret;
802 /**********************************
803 lookup a set of sids.
804 **********************************/
806 static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
808 struct idmap_tdb_context *ctx;
809 NTSTATUS ret;
810 int i;
812 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
814 for (i = 0; ids[i]; i++) {
815 ret = idmap_tdb_sid_to_id(ctx, ids[i]);
816 if ( ! NT_STATUS_IS_OK(ret)) {
818 /* if it is just a failed mapping continue */
819 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
821 /* make sure it is marked as unmapped */
822 ids[i]->status = ID_UNMAPPED;
823 continue;
826 /* some fatal error occurred, return immediately */
827 goto done;
830 /* all ok, id is mapped */
831 ids[i]->status = ID_MAPPED;
834 ret = NT_STATUS_OK;
836 done:
837 return ret;
840 /**********************************
841 set a mapping.
842 **********************************/
844 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
845 const struct id_map *map)
847 struct idmap_tdb_context *ctx;
848 NTSTATUS ret;
849 TDB_DATA ksid, kid;
850 char *ksidstr, *kidstr;
851 fstring tmp;
853 if (!map || !map->sid) {
854 return NT_STATUS_INVALID_PARAMETER;
857 ksidstr = kidstr = NULL;
859 /* TODO: should we filter a set_mapping using low/high filters ? */
861 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
863 switch (map->xid.type) {
865 case ID_TYPE_UID:
866 kidstr = talloc_asprintf(ctx, "UID %lu",
867 (unsigned long)map->xid.id);
868 break;
870 case ID_TYPE_GID:
871 kidstr = talloc_asprintf(ctx, "GID %lu",
872 (unsigned long)map->xid.id);
873 break;
875 default:
876 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
877 return NT_STATUS_INVALID_PARAMETER;
880 if (kidstr == NULL) {
881 DEBUG(0, ("ERROR: Out of memory!\n"));
882 ret = NT_STATUS_NO_MEMORY;
883 goto done;
886 if ((ksidstr = talloc_asprintf(
887 ctx, "%s", sid_to_fstring(tmp, map->sid))) == NULL) {
888 DEBUG(0, ("Out of memory!\n"));
889 ret = NT_STATUS_NO_MEMORY;
890 goto done;
893 DEBUG(10, ("Storing %s <-> %s map\n", ksidstr, kidstr));
894 kid = string_term_tdb_data(kidstr);
895 ksid = string_term_tdb_data(ksidstr);
897 if (ctx->db->transaction_start(ctx->db) != 0) {
898 DEBUG(0, ("Failed to start transaction for %s\n",
899 ksidstr));
900 ret = NT_STATUS_INTERNAL_DB_ERROR;
901 goto done;
904 ret = dbwrap_store(ctx->db, ksid, kid, TDB_REPLACE);
905 if (!NT_STATUS_IS_OK(ret)) {
906 ctx->db->transaction_cancel(ctx->db);
907 DEBUG(0, ("Error storing SID -> ID (%s -> %s): %s\n",
908 ksidstr, kidstr, nt_errstr(ret)));
909 goto done;
911 ret = dbwrap_store(ctx->db, kid, ksid, TDB_REPLACE);
912 if (!NT_STATUS_IS_OK(ret)) {
913 ctx->db->transaction_cancel(ctx->db);
914 DEBUG(0, ("Error storing ID -> SID (%s -> %s): %s\n",
915 kidstr, ksidstr, nt_errstr(ret)));
916 goto done;
919 if (ctx->db->transaction_commit(ctx->db) != 0) {
920 DEBUG(0, ("Failed to commit transaction for (%s -> %s)\n",
921 ksidstr, kidstr));
922 ret = NT_STATUS_INTERNAL_DB_ERROR;
923 goto done;
926 DEBUG(10,("Stored %s <-> %s\n", ksidstr, kidstr));
927 ret = NT_STATUS_OK;
929 done:
930 talloc_free(ksidstr);
931 talloc_free(kidstr);
932 return ret;
935 /**********************************
936 remove a mapping.
937 **********************************/
939 static NTSTATUS idmap_tdb_remove_mapping(struct idmap_domain *dom,
940 const struct id_map *map)
942 struct idmap_tdb_context *ctx;
943 NTSTATUS ret;
944 TDB_DATA ksid, kid, data;
945 char *ksidstr, *kidstr;
946 fstring tmp;
948 if (!map || !map->sid) {
949 return NT_STATUS_INVALID_PARAMETER;
952 ksidstr = kidstr = NULL;
953 data.dptr = NULL;
955 /* TODO: should we filter a remove_mapping using low/high filters ? */
957 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
959 switch (map->xid.type) {
961 case ID_TYPE_UID:
962 kidstr = talloc_asprintf(ctx, "UID %lu",
963 (unsigned long)map->xid.id);
964 break;
966 case ID_TYPE_GID:
967 kidstr = talloc_asprintf(ctx, "GID %lu",
968 (unsigned long)map->xid.id);
969 break;
971 default:
972 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
973 return NT_STATUS_INVALID_PARAMETER;
976 if (kidstr == NULL) {
977 DEBUG(0, ("ERROR: Out of memory!\n"));
978 ret = NT_STATUS_NO_MEMORY;
979 goto done;
982 if ((ksidstr = talloc_asprintf(
983 ctx, "%s", sid_to_fstring(tmp, map->sid))) == NULL) {
984 DEBUG(0, ("Out of memory!\n"));
985 ret = NT_STATUS_NO_MEMORY;
986 goto done;
989 DEBUG(10, ("Checking %s <-> %s map\n", ksidstr, kidstr));
990 ksid = string_term_tdb_data(ksidstr);
991 kid = string_term_tdb_data(kidstr);
993 if (ctx->db->transaction_start(ctx->db) != 0) {
994 DEBUG(0, ("Failed to start transaction for %s\n",
995 ksidstr));
996 return NT_STATUS_INTERNAL_DB_ERROR;
999 /* Check if sid is present in database */
1000 data = dbwrap_fetch(ctx->db, NULL, ksid);
1001 if (!data.dptr) {
1002 ctx->db->transaction_cancel(ctx->db);
1003 DEBUG(10,("Record %s not found\n", ksidstr));
1004 ret = NT_STATUS_NONE_MAPPED;
1005 goto done;
1008 /* Check if sid is mapped to the specified ID */
1009 if ((data.dsize != kid.dsize) ||
1010 (memcmp(data.dptr, kid.dptr, data.dsize) != 0)) {
1011 ctx->db->transaction_cancel(ctx->db);
1012 DEBUG(10,("Specified SID does not map to specified ID\n"));
1013 DEBUGADD(10,("Actual mapping is %s -> %s\n", ksidstr,
1014 (const char *)data.dptr));
1015 ret = NT_STATUS_NONE_MAPPED;
1016 goto done;
1019 DEBUG(10, ("Removing %s <-> %s map\n", ksidstr, kidstr));
1021 /* Delete previous mappings. */
1023 DEBUG(10, ("Deleting existing mapping %s -> %s\n", ksidstr, kidstr ));
1024 ret = dbwrap_delete(ctx->db, ksid);
1025 if (!NT_STATUS_IS_OK(ret)) {
1026 DEBUG(0,("Warning: Failed to delete %s: %s\n",
1027 ksidstr, nt_errstr(ret)));
1030 DEBUG(10,("Deleting existing mapping %s -> %s\n", kidstr, ksidstr ));
1031 ret = dbwrap_delete(ctx->db, kid);
1032 if (!NT_STATUS_IS_OK(ret)) {
1033 DEBUG(0,("Warning: Failed to delete %s: %s\n",
1034 kidstr, nt_errstr(ret)));
1037 if (ctx->db->transaction_commit(ctx->db) != 0) {
1038 DEBUG(0, ("Failed to commit transaction for (%s -> %s)\n",
1039 ksidstr, kidstr));
1040 ret = NT_STATUS_INTERNAL_DB_ERROR;
1041 goto done;
1044 ret = NT_STATUS_OK;
1046 done:
1047 talloc_free(ksidstr);
1048 talloc_free(kidstr);
1049 talloc_free(data.dptr);
1050 return ret;
1053 /**********************************
1054 Close the idmap tdb instance
1055 **********************************/
1057 static NTSTATUS idmap_tdb_close(struct idmap_domain *dom)
1059 struct idmap_tdb_context *ctx;
1061 if (dom->private_data) {
1062 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
1064 TALLOC_FREE(ctx->db);
1066 return NT_STATUS_OK;
1069 struct dump_data {
1070 TALLOC_CTX *memctx;
1071 struct id_map **maps;
1072 int *num_maps;
1073 NTSTATUS ret;
1076 static int idmap_tdb_dump_one_entry(struct db_record *rec, void *pdata)
1078 struct dump_data *data = talloc_get_type(pdata, struct dump_data);
1079 struct id_map *maps;
1080 int num_maps = *data->num_maps;
1082 /* ignore any record but the ones with a SID as key */
1083 if (strncmp((const char *)rec->key.dptr, "S-", 2) == 0) {
1085 maps = talloc_realloc(NULL, *data->maps, struct id_map, num_maps+1);
1086 if ( ! maps) {
1087 DEBUG(0, ("Out of memory!\n"));
1088 data->ret = NT_STATUS_NO_MEMORY;
1089 return -1;
1091 *data->maps = maps;
1092 maps[num_maps].sid = talloc(maps, DOM_SID);
1093 if ( ! maps[num_maps].sid) {
1094 DEBUG(0, ("Out of memory!\n"));
1095 data->ret = NT_STATUS_NO_MEMORY;
1096 return -1;
1099 if (!string_to_sid(maps[num_maps].sid, (const char *)rec->key.dptr)) {
1100 DEBUG(10,("INVALID record %s\n", (const char *)rec->key.dptr));
1101 /* continue even with errors */
1102 return 0;
1105 /* Try a UID record. */
1106 if (sscanf((const char *)rec->value.dptr, "UID %u", &(maps[num_maps].xid.id)) == 1) {
1107 maps[num_maps].xid.type = ID_TYPE_UID;
1108 maps[num_maps].status = ID_MAPPED;
1109 *data->num_maps = num_maps + 1;
1111 /* Try a GID record. */
1112 } else
1113 if (sscanf((const char *)rec->value.dptr, "GID %u", &(maps[num_maps].xid.id)) == 1) {
1114 maps[num_maps].xid.type = ID_TYPE_GID;
1115 maps[num_maps].status = ID_MAPPED;
1116 *data->num_maps = num_maps + 1;
1118 /* Unknown record type ! */
1119 } else {
1120 maps[num_maps].status = ID_UNKNOWN;
1121 DEBUG(2, ("Found INVALID record %s -> %s\n",
1122 (const char *)rec->key.dptr,
1123 (const char *)rec->value.dptr));
1124 /* do not increment num_maps */
1128 return 0;
1131 /**********************************
1132 Dump all mappings out
1133 **********************************/
1135 static NTSTATUS idmap_tdb_dump_data(struct idmap_domain *dom, struct id_map **maps, int *num_maps)
1137 struct idmap_tdb_context *ctx;
1138 struct dump_data *data;
1139 NTSTATUS ret = NT_STATUS_OK;
1141 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
1143 data = TALLOC_ZERO_P(ctx, struct dump_data);
1144 if ( ! data) {
1145 DEBUG(0, ("Out of memory!\n"));
1146 return NT_STATUS_NO_MEMORY;
1148 data->maps = maps;
1149 data->num_maps = num_maps;
1150 data->ret = NT_STATUS_OK;
1152 ctx->db->traverse_read(ctx->db, idmap_tdb_dump_one_entry, data);
1154 if ( ! NT_STATUS_IS_OK(data->ret)) {
1155 ret = data->ret;
1158 talloc_free(data);
1159 return ret;
1162 static struct idmap_methods db_methods = {
1164 .init = idmap_tdb_db_init,
1165 .unixids_to_sids = idmap_tdb_unixids_to_sids,
1166 .sids_to_unixids = idmap_tdb_sids_to_unixids,
1167 .set_mapping = idmap_tdb_set_mapping,
1168 .remove_mapping = idmap_tdb_remove_mapping,
1169 .dump_data = idmap_tdb_dump_data,
1170 .close_fn = idmap_tdb_close
1173 static struct idmap_alloc_methods db_alloc_methods = {
1175 .init = idmap_tdb_alloc_init,
1176 .allocate_id = idmap_tdb_allocate_id,
1177 .get_id_hwm = idmap_tdb_get_hwm,
1178 .set_id_hwm = idmap_tdb_set_hwm,
1179 .close_fn = idmap_tdb_alloc_close
1182 NTSTATUS idmap_alloc_tdb_init(void)
1184 return smb_register_idmap_alloc(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_alloc_methods);
1187 NTSTATUS idmap_tdb_init(void)
1189 NTSTATUS ret;
1191 DEBUG(10, ("calling idmap_tdb_init\n"));
1193 /* FIXME: bad hack to actually register also the alloc_tdb module without changining configure.in */
1194 ret = idmap_alloc_tdb_init();
1195 if (! NT_STATUS_IS_OK(ret)) {
1196 return ret;
1198 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);