s3: Add connections_forall_read()
[Samba/nascimento.git] / source3 / winbindd / idmap_tdb.c
blob8bfe751a8ba9c829b0721b2e29fc7d6be89cf3b4
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_compose(&sid, &domain->sid, rid);
102 sid_to_fstring(keystr, &sid);
103 key2 = string_term_tdb_data(keystr);
105 status = dbwrap_store(s->db, key2, rec->value, TDB_INSERT);
106 if (!NT_STATUS_IS_OK(status)) {
107 DEBUG(0,("Unable to add record %s:%s\n",
108 (const char *)key2.dptr,
109 nt_errstr(status)));
110 s->failed = true;
111 return -1;
114 status = dbwrap_store(s->db, rec->value, key2, TDB_REPLACE);
115 if (!NT_STATUS_IS_OK(status)) {
116 DEBUG(0,("Unable to update record %s:%s\n",
117 (const char *)rec->value.dptr,
118 nt_errstr(status)));
119 s->failed = true;
120 return -1;
123 status = rec->delete_rec(rec);
124 if (!NT_STATUS_IS_OK(status)) {
125 DEBUG(0,("Unable to delete record %s:%s\n",
126 (const char *)rec->key.dptr,
127 nt_errstr(status)));
128 s->failed = true;
129 return -1;
132 return 0;
135 /*****************************************************************************
136 Convert the idmap database from an older version.
137 *****************************************************************************/
139 static bool idmap_tdb_upgrade(struct db_context *db)
141 int32 vers;
142 bool bigendianheader;
143 struct convert_fn_state s;
145 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
147 bigendianheader = (db->get_flags(db) & TDB_BIGENDIAN) ? True : False;
149 vers = dbwrap_fetch_int32(db, "IDMAP_VERSION");
151 if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
152 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
154 * high and low records were created on a
155 * big endian machine and will need byte-reversing.
158 int32 wm;
160 wm = dbwrap_fetch_int32(db, HWM_USER);
162 if (wm != -1) {
163 wm = IREV(wm);
164 } else {
165 wm = idmap_tdb_state.low_uid;
168 if (dbwrap_store_int32(db, HWM_USER, wm) == -1) {
169 DEBUG(0, ("Unable to byteswap user hwm in idmap database\n"));
170 return False;
173 wm = dbwrap_fetch_int32(db, HWM_GROUP);
174 if (wm != -1) {
175 wm = IREV(wm);
176 } else {
177 wm = idmap_tdb_state.low_gid;
180 if (dbwrap_store_int32(db, HWM_GROUP, wm) == -1) {
181 DEBUG(0, ("Unable to byteswap group hwm in idmap database\n"));
182 return False;
186 s.db = db;
187 s.failed = false;
189 /* the old format stored as DOMAIN/rid - now we store the SID direct */
190 db->traverse(db, convert_fn, &s);
192 if (s.failed) {
193 DEBUG(0, ("Problem during conversion\n"));
194 return False;
197 if (dbwrap_store_int32(db, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
198 DEBUG(0, ("Unable to store idmap version in databse\n"));
199 return False;
202 return True;
205 static NTSTATUS idmap_tdb_load_ranges(void)
207 uid_t low_uid = 0;
208 uid_t high_uid = 0;
209 gid_t low_gid = 0;
210 gid_t high_gid = 0;
212 if (!lp_idmap_uid(&low_uid, &high_uid)) {
213 DEBUG(1, ("idmap uid missing\n"));
214 return NT_STATUS_UNSUCCESSFUL;
217 if (!lp_idmap_gid(&low_gid, &high_gid)) {
218 DEBUG(1, ("idmap gid missing\n"));
219 return NT_STATUS_UNSUCCESSFUL;
222 idmap_tdb_state.low_uid = low_uid;
223 idmap_tdb_state.high_uid = high_uid;
224 idmap_tdb_state.low_gid = low_gid;
225 idmap_tdb_state.high_gid = high_gid;
227 if (idmap_tdb_state.high_uid <= idmap_tdb_state.low_uid) {
228 DEBUG(1, ("idmap uid range missing or invalid\n"));
229 return NT_STATUS_UNSUCCESSFUL;
232 if (idmap_tdb_state.high_gid <= idmap_tdb_state.low_gid) {
233 DEBUG(1, ("idmap gid range missing or invalid\n"));
234 return NT_STATUS_UNSUCCESSFUL;
237 return NT_STATUS_OK;
240 static NTSTATUS idmap_tdb_open_db(TALLOC_CTX *memctx,
241 bool check_config,
242 struct db_context **dbctx)
244 NTSTATUS ret;
245 TALLOC_CTX *ctx;
246 char *tdbfile = NULL;
247 struct db_context *db = NULL;
248 int32_t version;
249 bool config_error = false;
251 ret = idmap_tdb_load_ranges();
252 if (!NT_STATUS_IS_OK(ret)) {
253 config_error = true;
254 if (check_config) {
255 return ret;
259 /* use our own context here */
260 ctx = talloc_stackframe();
262 /* use the old database if present */
263 tdbfile = state_path("winbindd_idmap.tdb");
264 if (!tdbfile) {
265 DEBUG(0, ("Out of memory!\n"));
266 ret = NT_STATUS_NO_MEMORY;
267 goto done;
270 DEBUG(10,("Opening tdbfile %s\n", tdbfile ));
272 /* Open idmap repository */
273 db = db_open(ctx, tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
274 if (!db) {
275 DEBUG(0, ("Unable to open idmap database\n"));
276 ret = NT_STATUS_UNSUCCESSFUL;
277 goto done;
280 /* check against earlier versions */
281 version = dbwrap_fetch_int32(db, "IDMAP_VERSION");
282 if (version != IDMAP_VERSION) {
283 if (config_error) {
284 DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
285 "possible with incomplete configuration\n",
286 version, IDMAP_VERSION));
287 ret = NT_STATUS_UNSUCCESSFUL;
288 goto done;
290 if (db->transaction_start(db) != 0) {
291 DEBUG(0, ("Unable to start upgrade transaction!\n"));
292 ret = NT_STATUS_INTERNAL_DB_ERROR;
293 goto done;
296 if (!idmap_tdb_upgrade(db)) {
297 db->transaction_cancel(db);
298 DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
299 ret = NT_STATUS_INTERNAL_DB_ERROR;
300 goto done;
303 if (db->transaction_commit(db) != 0) {
304 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
305 ret = NT_STATUS_INTERNAL_DB_ERROR;
306 goto done;
310 *dbctx = talloc_move(memctx, &db);
311 ret = NT_STATUS_OK;
313 done:
314 talloc_free(ctx);
315 return ret;
318 /**********************************************************************
319 IDMAP ALLOC TDB BACKEND
320 **********************************************************************/
322 static struct db_context *idmap_alloc_db;
324 /**********************************
325 Initialise idmap alloc database.
326 **********************************/
328 static NTSTATUS idmap_tdb_alloc_init( const char *params )
330 int ret;
331 NTSTATUS status;
332 uint32_t low_uid;
333 uint32_t low_gid;
334 bool update_uid = false;
335 bool update_gid = false;
337 status = idmap_tdb_open_db(NULL, true, &idmap_alloc_db);
338 if (!NT_STATUS_IS_OK(status)) {
339 DEBUG(0, ("idmap will be unable to map foreign SIDs: %s\n",
340 nt_errstr(status)));
341 return status;
344 low_uid = dbwrap_fetch_int32(idmap_alloc_db, HWM_USER);
345 if (low_uid == -1 || low_uid < idmap_tdb_state.low_uid) {
346 update_uid = true;
349 low_gid = dbwrap_fetch_int32(idmap_alloc_db, HWM_GROUP);
350 if (low_gid == -1 || low_gid < idmap_tdb_state.low_gid) {
351 update_gid = true;
354 if (!update_uid && !update_gid) {
355 return NT_STATUS_OK;
358 if (idmap_alloc_db->transaction_start(idmap_alloc_db) != 0) {
359 TALLOC_FREE(idmap_alloc_db);
360 DEBUG(0, ("Unable to start upgrade transaction!\n"));
361 return NT_STATUS_INTERNAL_DB_ERROR;
364 if (update_uid) {
365 ret = dbwrap_store_int32(idmap_alloc_db, HWM_USER,
366 idmap_tdb_state.low_uid);
367 if (ret == -1) {
368 idmap_alloc_db->transaction_cancel(idmap_alloc_db);
369 TALLOC_FREE(idmap_alloc_db);
370 DEBUG(0, ("Unable to initialise user hwm in idmap "
371 "database\n"));
372 return NT_STATUS_INTERNAL_DB_ERROR;
376 if (update_gid) {
377 ret = dbwrap_store_int32(idmap_alloc_db, HWM_GROUP,
378 idmap_tdb_state.low_gid);
379 if (ret == -1) {
380 idmap_alloc_db->transaction_cancel(idmap_alloc_db);
381 TALLOC_FREE(idmap_alloc_db);
382 DEBUG(0, ("Unable to initialise group hwm in idmap "
383 "database\n"));
384 return NT_STATUS_INTERNAL_DB_ERROR;
388 if (idmap_alloc_db->transaction_commit(idmap_alloc_db) != 0) {
389 TALLOC_FREE(idmap_alloc_db);
390 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
391 return NT_STATUS_INTERNAL_DB_ERROR;
394 return NT_STATUS_OK;
397 /**********************************
398 Allocate a new id.
399 **********************************/
401 static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
403 NTSTATUS ret;
404 const char *hwmkey;
405 const char *hwmtype;
406 uint32_t high_hwm;
407 uint32_t hwm;
408 int res;
410 /* Get current high water mark */
411 switch (xid->type) {
413 case ID_TYPE_UID:
414 hwmkey = HWM_USER;
415 hwmtype = "UID";
416 high_hwm = idmap_tdb_state.high_uid;
417 break;
419 case ID_TYPE_GID:
420 hwmkey = HWM_GROUP;
421 hwmtype = "GID";
422 high_hwm = idmap_tdb_state.high_gid;
423 break;
425 default:
426 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
427 return NT_STATUS_INVALID_PARAMETER;
430 res = idmap_alloc_db->transaction_start(idmap_alloc_db);
431 if (res != 0) {
432 DEBUG(1, (__location__ " Failed to start transaction.\n"));
433 return NT_STATUS_UNSUCCESSFUL;
436 if ((hwm = dbwrap_fetch_int32(idmap_alloc_db, hwmkey)) == -1) {
437 idmap_alloc_db->transaction_cancel(idmap_alloc_db);
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 idmap_alloc_db->transaction_cancel(idmap_alloc_db);
446 return NT_STATUS_UNSUCCESSFUL;
449 /* fetch a new id and increment it */
450 ret = dbwrap_change_uint32_atomic(idmap_alloc_db, hwmkey, &hwm, 1);
451 if (!NT_STATUS_IS_OK(ret)) {
452 DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
453 hwmtype, nt_errstr(ret)));
454 idmap_alloc_db->transaction_cancel(idmap_alloc_db);
455 return ret;
458 /* recheck it is in the range */
459 if (hwm > high_hwm) {
460 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
461 hwmtype, (unsigned long)high_hwm));
462 idmap_alloc_db->transaction_cancel(idmap_alloc_db);
463 return NT_STATUS_UNSUCCESSFUL;
466 res = idmap_alloc_db->transaction_commit(idmap_alloc_db);
467 if (res != 0) {
468 DEBUG(1, (__location__ " Failed to commit transaction.\n"));
469 return NT_STATUS_UNSUCCESSFUL;
472 xid->id = hwm;
473 DEBUG(10,("New %s = %d\n", hwmtype, hwm));
475 return NT_STATUS_OK;
478 /**********************************
479 Get current highest id.
480 **********************************/
482 static NTSTATUS idmap_tdb_get_hwm(struct unixid *xid)
484 const char *hwmkey;
485 const char *hwmtype;
486 uint32_t hwm;
487 uint32_t high_hwm;
489 /* Get current high water mark */
490 switch (xid->type) {
492 case ID_TYPE_UID:
493 hwmkey = HWM_USER;
494 hwmtype = "UID";
495 high_hwm = idmap_tdb_state.high_uid;
496 break;
498 case ID_TYPE_GID:
499 hwmkey = HWM_GROUP;
500 hwmtype = "GID";
501 high_hwm = idmap_tdb_state.high_gid;
502 break;
504 default:
505 return NT_STATUS_INVALID_PARAMETER;
508 if ((hwm = dbwrap_fetch_int32(idmap_alloc_db, hwmkey)) == -1) {
509 return NT_STATUS_INTERNAL_DB_ERROR;
512 xid->id = hwm;
514 /* Warn if it is out of range */
515 if (hwm >= high_hwm) {
516 DEBUG(0, ("Warning: %s range full!! (max: %lu)\n",
517 hwmtype, (unsigned long)high_hwm));
520 return NT_STATUS_OK;
523 /**********************************
524 Set high id.
525 **********************************/
527 static NTSTATUS idmap_tdb_set_hwm(struct unixid *xid)
529 const char *hwmkey;
530 const char *hwmtype;
531 uint32_t hwm;
532 uint32_t high_hwm;
533 NTSTATUS ret;
535 /* Get current high water mark */
536 switch (xid->type) {
538 case ID_TYPE_UID:
539 hwmkey = HWM_USER;
540 hwmtype = "UID";
541 high_hwm = idmap_tdb_state.high_uid;
542 break;
544 case ID_TYPE_GID:
545 hwmkey = HWM_GROUP;
546 hwmtype = "GID";
547 high_hwm = idmap_tdb_state.high_gid;
548 break;
550 default:
551 return NT_STATUS_INVALID_PARAMETER;
554 hwm = xid->id;
556 /* Warn if it is out of range */
557 if (hwm >= high_hwm) {
558 DEBUG(0, ("Warning: %s range full!! (max: %lu)\n",
559 hwmtype, (unsigned long)high_hwm));
562 ret = dbwrap_trans_store_uint32(idmap_alloc_db, hwmkey, hwm);
564 return ret;
567 /**********************************
568 Close the alloc tdb
569 **********************************/
571 static NTSTATUS idmap_tdb_alloc_close(void)
573 TALLOC_FREE(idmap_alloc_db);
574 return NT_STATUS_OK;
577 /**********************************************************************
578 IDMAP MAPPING TDB BACKEND
579 **********************************************************************/
581 struct idmap_tdb_context {
582 struct db_context *db;
583 uint32_t filter_low_id;
584 uint32_t filter_high_id;
587 /*****************************
588 Initialise idmap database.
589 *****************************/
591 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom, const char *params)
593 NTSTATUS ret;
594 struct idmap_tdb_context *ctx;
596 DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom->name));
598 ctx = talloc(dom, struct idmap_tdb_context);
599 if ( ! ctx) {
600 DEBUG(0, ("Out of memory!\n"));
601 return NT_STATUS_NO_MEMORY;
604 if (strequal(dom->name, "*")) {
605 uid_t low_uid = 0;
606 uid_t high_uid = 0;
607 gid_t low_gid = 0;
608 gid_t high_gid = 0;
610 ctx->filter_low_id = 0;
611 ctx->filter_high_id = 0;
613 if (lp_idmap_uid(&low_uid, &high_uid)) {
614 ctx->filter_low_id = low_uid;
615 ctx->filter_high_id = high_uid;
616 } else {
617 DEBUG(3, ("Warning: 'idmap uid' not set!\n"));
620 if (lp_idmap_gid(&low_gid, &high_gid)) {
621 if ((low_gid != low_uid) || (high_gid != high_uid)) {
622 DEBUG(1, ("Warning: 'idmap uid' and 'idmap gid'"
623 " ranges do not agree -- building "
624 "intersection\n"));
625 ctx->filter_low_id = MAX(ctx->filter_low_id,
626 low_gid);
627 ctx->filter_high_id = MIN(ctx->filter_high_id,
628 high_gid);
630 } else {
631 DEBUG(3, ("Warning: 'idmap gid' not set!\n"));
633 } else {
634 char *config_option = NULL;
635 const char *range;
637 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
638 if ( ! config_option) {
639 DEBUG(0, ("Out of memory!\n"));
640 ret = NT_STATUS_NO_MEMORY;
641 goto failed;
644 range = lp_parm_const_string(-1, config_option, "range", NULL);
645 if (( ! range) ||
646 (sscanf(range, "%u - %u", &ctx->filter_low_id, &ctx->filter_high_id) != 2))
648 ctx->filter_low_id = 0;
649 ctx->filter_high_id = 0;
652 talloc_free(config_option);
655 if (ctx->filter_low_id > ctx->filter_high_id) {
656 ctx->filter_low_id = 0;
657 ctx->filter_high_id = 0;
660 DEBUG(10, ("idmap_tdb_db_init: filter range %u-%u loaded for domain "
661 "'%s'\n", ctx->filter_low_id, ctx->filter_high_id, dom->name));
663 ret = idmap_tdb_open_db(ctx, false, &ctx->db);
664 if ( ! NT_STATUS_IS_OK(ret)) {
665 goto failed;
668 dom->private_data = ctx;
670 return NT_STATUS_OK;
672 failed:
673 talloc_free(ctx);
674 return ret;
677 /**********************************
678 Single id to sid lookup function.
679 **********************************/
681 static NTSTATUS idmap_tdb_id_to_sid(struct idmap_tdb_context *ctx, struct id_map *map)
683 NTSTATUS ret;
684 TDB_DATA data;
685 char *keystr;
687 if (!ctx || !map) {
688 return NT_STATUS_INVALID_PARAMETER;
691 /* apply filters before checking */
692 if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
693 (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
694 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
695 map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
696 return NT_STATUS_NONE_MAPPED;
699 switch (map->xid.type) {
701 case ID_TYPE_UID:
702 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
703 break;
705 case ID_TYPE_GID:
706 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
707 break;
709 default:
710 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
711 return NT_STATUS_INVALID_PARAMETER;
714 /* final SAFE_FREE safe */
715 data.dptr = NULL;
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 the mapping exists */
726 data = dbwrap_fetch_bystring(ctx->db, NULL, keystr);
728 if (!data.dptr) {
729 DEBUG(10,("Record %s not found\n", keystr));
730 ret = NT_STATUS_NONE_MAPPED;
731 goto done;
734 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
735 DEBUG(10,("INVALID SID (%s) in record %s\n",
736 (const char *)data.dptr, keystr));
737 ret = NT_STATUS_INTERNAL_DB_ERROR;
738 goto done;
741 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
742 ret = NT_STATUS_OK;
744 done:
745 talloc_free(data.dptr);
746 talloc_free(keystr);
747 return ret;
750 /**********************************
751 Single sid to id lookup function.
752 **********************************/
754 static NTSTATUS idmap_tdb_sid_to_id(struct idmap_tdb_context *ctx, struct id_map *map)
756 NTSTATUS ret;
757 TDB_DATA data;
758 char *keystr;
759 unsigned long rec_id = 0;
760 TALLOC_CTX *tmp_ctx = talloc_stackframe();
762 keystr = sid_string_talloc(tmp_ctx, map->sid);
763 if (keystr == NULL) {
764 DEBUG(0, ("Out of memory!\n"));
765 ret = NT_STATUS_NO_MEMORY;
766 goto done;
769 DEBUG(10,("Fetching record %s\n", keystr));
771 /* Check if sid is present in database */
772 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
773 if (!data.dptr) {
774 DEBUG(10,("Record %s not found\n", keystr));
775 ret = NT_STATUS_NONE_MAPPED;
776 goto done;
779 /* What type of record is this ? */
780 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
781 map->xid.id = rec_id;
782 map->xid.type = ID_TYPE_UID;
783 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
784 ret = NT_STATUS_OK;
786 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
787 map->xid.id = rec_id;
788 map->xid.type = ID_TYPE_GID;
789 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
790 ret = NT_STATUS_OK;
792 } else { /* Unknown record type ! */
793 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
794 ret = NT_STATUS_INTERNAL_DB_ERROR;
797 /* apply filters before returning result */
798 if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
799 (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
800 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
801 map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
802 ret = NT_STATUS_NONE_MAPPED;
805 done:
806 talloc_free(tmp_ctx);
807 return ret;
810 /**********************************
811 lookup a set of unix ids.
812 **********************************/
814 static NTSTATUS idmap_tdb_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
816 struct idmap_tdb_context *ctx;
817 NTSTATUS ret;
818 int i;
820 /* initialize the status to avoid suprise */
821 for (i = 0; ids[i]; i++) {
822 ids[i]->status = ID_UNKNOWN;
825 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
827 for (i = 0; ids[i]; i++) {
828 ret = idmap_tdb_id_to_sid(ctx, ids[i]);
829 if ( ! NT_STATUS_IS_OK(ret)) {
831 /* if it is just a failed mapping continue */
832 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
834 /* make sure it is marked as unmapped */
835 ids[i]->status = ID_UNMAPPED;
836 continue;
839 /* some fatal error occurred, return immediately */
840 goto done;
843 /* all ok, id is mapped */
844 ids[i]->status = ID_MAPPED;
847 ret = NT_STATUS_OK;
849 done:
850 return ret;
853 /**********************************
854 lookup a set of sids.
855 **********************************/
857 static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
859 struct idmap_tdb_context *ctx;
860 NTSTATUS ret;
861 int i;
863 /* initialize the status to avoid suprise */
864 for (i = 0; ids[i]; i++) {
865 ids[i]->status = ID_UNKNOWN;
868 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
870 for (i = 0; ids[i]; i++) {
871 ret = idmap_tdb_sid_to_id(ctx, ids[i]);
872 if ( ! NT_STATUS_IS_OK(ret)) {
874 /* if it is just a failed mapping continue */
875 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
877 /* make sure it is marked as unmapped */
878 ids[i]->status = ID_UNMAPPED;
879 continue;
882 /* some fatal error occurred, return immediately */
883 goto done;
886 /* all ok, id is mapped */
887 ids[i]->status = ID_MAPPED;
890 ret = NT_STATUS_OK;
892 done:
893 return ret;
896 /**********************************
897 set a mapping.
898 **********************************/
900 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
901 const struct id_map *map)
903 struct idmap_tdb_context *ctx;
904 NTSTATUS ret;
905 TDB_DATA ksid, kid;
906 char *ksidstr, *kidstr;
907 fstring tmp;
909 if (!map || !map->sid) {
910 return NT_STATUS_INVALID_PARAMETER;
913 ksidstr = kidstr = NULL;
915 /* TODO: should we filter a set_mapping using low/high filters ? */
917 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
919 switch (map->xid.type) {
921 case ID_TYPE_UID:
922 kidstr = talloc_asprintf(ctx, "UID %lu",
923 (unsigned long)map->xid.id);
924 break;
926 case ID_TYPE_GID:
927 kidstr = talloc_asprintf(ctx, "GID %lu",
928 (unsigned long)map->xid.id);
929 break;
931 default:
932 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
933 return NT_STATUS_INVALID_PARAMETER;
936 if (kidstr == NULL) {
937 DEBUG(0, ("ERROR: Out of memory!\n"));
938 ret = NT_STATUS_NO_MEMORY;
939 goto done;
942 if ((ksidstr = talloc_asprintf(
943 ctx, "%s", sid_to_fstring(tmp, map->sid))) == NULL) {
944 DEBUG(0, ("Out of memory!\n"));
945 ret = NT_STATUS_NO_MEMORY;
946 goto done;
949 DEBUG(10, ("Storing %s <-> %s map\n", ksidstr, kidstr));
950 kid = string_term_tdb_data(kidstr);
951 ksid = string_term_tdb_data(ksidstr);
953 if (ctx->db->transaction_start(ctx->db) != 0) {
954 DEBUG(0, ("Failed to start transaction for %s\n",
955 ksidstr));
956 ret = NT_STATUS_INTERNAL_DB_ERROR;
957 goto done;
960 ret = dbwrap_store(ctx->db, ksid, kid, TDB_REPLACE);
961 if (!NT_STATUS_IS_OK(ret)) {
962 ctx->db->transaction_cancel(ctx->db);
963 DEBUG(0, ("Error storing SID -> ID (%s -> %s): %s\n",
964 ksidstr, kidstr, nt_errstr(ret)));
965 goto done;
967 ret = dbwrap_store(ctx->db, kid, ksid, TDB_REPLACE);
968 if (!NT_STATUS_IS_OK(ret)) {
969 ctx->db->transaction_cancel(ctx->db);
970 DEBUG(0, ("Error storing ID -> SID (%s -> %s): %s\n",
971 kidstr, ksidstr, nt_errstr(ret)));
972 goto done;
975 if (ctx->db->transaction_commit(ctx->db) != 0) {
976 DEBUG(0, ("Failed to commit transaction for (%s -> %s)\n",
977 ksidstr, kidstr));
978 ret = NT_STATUS_INTERNAL_DB_ERROR;
979 goto done;
982 DEBUG(10,("Stored %s <-> %s\n", ksidstr, kidstr));
983 ret = NT_STATUS_OK;
985 done:
986 talloc_free(ksidstr);
987 talloc_free(kidstr);
988 return ret;
991 /**********************************
992 remove a mapping.
993 **********************************/
995 static NTSTATUS idmap_tdb_remove_mapping(struct idmap_domain *dom,
996 const struct id_map *map)
998 struct idmap_tdb_context *ctx;
999 NTSTATUS ret;
1000 TDB_DATA ksid, kid, data;
1001 char *ksidstr, *kidstr;
1002 fstring tmp;
1004 if (!map || !map->sid) {
1005 return NT_STATUS_INVALID_PARAMETER;
1008 ksidstr = kidstr = NULL;
1009 data.dptr = NULL;
1011 /* TODO: should we filter a remove_mapping using low/high filters ? */
1013 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
1015 switch (map->xid.type) {
1017 case ID_TYPE_UID:
1018 kidstr = talloc_asprintf(ctx, "UID %lu",
1019 (unsigned long)map->xid.id);
1020 break;
1022 case ID_TYPE_GID:
1023 kidstr = talloc_asprintf(ctx, "GID %lu",
1024 (unsigned long)map->xid.id);
1025 break;
1027 default:
1028 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
1029 return NT_STATUS_INVALID_PARAMETER;
1032 if (kidstr == NULL) {
1033 DEBUG(0, ("ERROR: Out of memory!\n"));
1034 ret = NT_STATUS_NO_MEMORY;
1035 goto done;
1038 if ((ksidstr = talloc_asprintf(
1039 ctx, "%s", sid_to_fstring(tmp, map->sid))) == NULL) {
1040 DEBUG(0, ("Out of memory!\n"));
1041 ret = NT_STATUS_NO_MEMORY;
1042 goto done;
1045 DEBUG(10, ("Checking %s <-> %s map\n", ksidstr, kidstr));
1046 ksid = string_term_tdb_data(ksidstr);
1047 kid = string_term_tdb_data(kidstr);
1049 if (ctx->db->transaction_start(ctx->db) != 0) {
1050 DEBUG(0, ("Failed to start transaction for %s\n",
1051 ksidstr));
1052 return NT_STATUS_INTERNAL_DB_ERROR;
1055 /* Check if sid is present in database */
1056 data = dbwrap_fetch(ctx->db, NULL, ksid);
1057 if (!data.dptr) {
1058 ctx->db->transaction_cancel(ctx->db);
1059 DEBUG(10,("Record %s not found\n", ksidstr));
1060 ret = NT_STATUS_NONE_MAPPED;
1061 goto done;
1064 /* Check if sid is mapped to the specified ID */
1065 if ((data.dsize != kid.dsize) ||
1066 (memcmp(data.dptr, kid.dptr, data.dsize) != 0)) {
1067 ctx->db->transaction_cancel(ctx->db);
1068 DEBUG(10,("Specified SID does not map to specified ID\n"));
1069 DEBUGADD(10,("Actual mapping is %s -> %s\n", ksidstr,
1070 (const char *)data.dptr));
1071 ret = NT_STATUS_NONE_MAPPED;
1072 goto done;
1075 DEBUG(10, ("Removing %s <-> %s map\n", ksidstr, kidstr));
1077 /* Delete previous mappings. */
1079 DEBUG(10, ("Deleting existing mapping %s -> %s\n", ksidstr, kidstr ));
1080 ret = dbwrap_delete(ctx->db, ksid);
1081 if (!NT_STATUS_IS_OK(ret)) {
1082 DEBUG(0,("Warning: Failed to delete %s: %s\n",
1083 ksidstr, nt_errstr(ret)));
1086 DEBUG(10,("Deleting existing mapping %s -> %s\n", kidstr, ksidstr ));
1087 ret = dbwrap_delete(ctx->db, kid);
1088 if (!NT_STATUS_IS_OK(ret)) {
1089 DEBUG(0,("Warning: Failed to delete %s: %s\n",
1090 kidstr, nt_errstr(ret)));
1093 if (ctx->db->transaction_commit(ctx->db) != 0) {
1094 DEBUG(0, ("Failed to commit transaction for (%s -> %s)\n",
1095 ksidstr, kidstr));
1096 ret = NT_STATUS_INTERNAL_DB_ERROR;
1097 goto done;
1100 ret = NT_STATUS_OK;
1102 done:
1103 talloc_free(ksidstr);
1104 talloc_free(kidstr);
1105 talloc_free(data.dptr);
1106 return ret;
1109 /**********************************
1110 Close the idmap tdb instance
1111 **********************************/
1113 static NTSTATUS idmap_tdb_close(struct idmap_domain *dom)
1115 struct idmap_tdb_context *ctx;
1117 if (dom->private_data) {
1118 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
1120 TALLOC_FREE(ctx->db);
1122 return NT_STATUS_OK;
1125 struct dump_data {
1126 TALLOC_CTX *memctx;
1127 struct id_map **maps;
1128 int *num_maps;
1129 NTSTATUS ret;
1132 static int idmap_tdb_dump_one_entry(struct db_record *rec, void *pdata)
1134 struct dump_data *data = talloc_get_type(pdata, struct dump_data);
1135 struct id_map *maps;
1136 int num_maps = *data->num_maps;
1138 /* ignore any record but the ones with a SID as key */
1139 if (strncmp((const char *)rec->key.dptr, "S-", 2) == 0) {
1141 maps = talloc_realloc(NULL, *data->maps, struct id_map, num_maps+1);
1142 if ( ! maps) {
1143 DEBUG(0, ("Out of memory!\n"));
1144 data->ret = NT_STATUS_NO_MEMORY;
1145 return -1;
1147 *data->maps = maps;
1148 maps[num_maps].sid = talloc(maps, DOM_SID);
1149 if ( ! maps[num_maps].sid) {
1150 DEBUG(0, ("Out of memory!\n"));
1151 data->ret = NT_STATUS_NO_MEMORY;
1152 return -1;
1155 if (!string_to_sid(maps[num_maps].sid, (const char *)rec->key.dptr)) {
1156 DEBUG(10,("INVALID record %s\n", (const char *)rec->key.dptr));
1157 /* continue even with errors */
1158 return 0;
1161 /* Try a UID record. */
1162 if (sscanf((const char *)rec->value.dptr, "UID %u", &(maps[num_maps].xid.id)) == 1) {
1163 maps[num_maps].xid.type = ID_TYPE_UID;
1164 maps[num_maps].status = ID_MAPPED;
1165 *data->num_maps = num_maps + 1;
1167 /* Try a GID record. */
1168 } else
1169 if (sscanf((const char *)rec->value.dptr, "GID %u", &(maps[num_maps].xid.id)) == 1) {
1170 maps[num_maps].xid.type = ID_TYPE_GID;
1171 maps[num_maps].status = ID_MAPPED;
1172 *data->num_maps = num_maps + 1;
1174 /* Unknown record type ! */
1175 } else {
1176 maps[num_maps].status = ID_UNKNOWN;
1177 DEBUG(2, ("Found INVALID record %s -> %s\n",
1178 (const char *)rec->key.dptr,
1179 (const char *)rec->value.dptr));
1180 /* do not increment num_maps */
1184 return 0;
1187 /**********************************
1188 Dump all mappings out
1189 **********************************/
1191 static NTSTATUS idmap_tdb_dump_data(struct idmap_domain *dom, struct id_map **maps, int *num_maps)
1193 struct idmap_tdb_context *ctx;
1194 struct dump_data *data;
1195 NTSTATUS ret = NT_STATUS_OK;
1197 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
1199 data = TALLOC_ZERO_P(ctx, struct dump_data);
1200 if ( ! data) {
1201 DEBUG(0, ("Out of memory!\n"));
1202 return NT_STATUS_NO_MEMORY;
1204 data->maps = maps;
1205 data->num_maps = num_maps;
1206 data->ret = NT_STATUS_OK;
1208 ctx->db->traverse_read(ctx->db, idmap_tdb_dump_one_entry, data);
1210 if ( ! NT_STATUS_IS_OK(data->ret)) {
1211 ret = data->ret;
1214 talloc_free(data);
1215 return ret;
1218 static struct idmap_methods db_methods = {
1220 .init = idmap_tdb_db_init,
1221 .unixids_to_sids = idmap_tdb_unixids_to_sids,
1222 .sids_to_unixids = idmap_tdb_sids_to_unixids,
1223 .set_mapping = idmap_tdb_set_mapping,
1224 .remove_mapping = idmap_tdb_remove_mapping,
1225 .dump_data = idmap_tdb_dump_data,
1226 .close_fn = idmap_tdb_close
1229 static struct idmap_alloc_methods db_alloc_methods = {
1231 .init = idmap_tdb_alloc_init,
1232 .allocate_id = idmap_tdb_allocate_id,
1233 .get_id_hwm = idmap_tdb_get_hwm,
1234 .set_id_hwm = idmap_tdb_set_hwm,
1235 .close_fn = idmap_tdb_alloc_close
1238 NTSTATUS idmap_alloc_tdb_init(void)
1240 return smb_register_idmap_alloc(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_alloc_methods);
1243 NTSTATUS idmap_tdb_init(void)
1245 NTSTATUS ret;
1247 DEBUG(10, ("calling idmap_tdb_init\n"));
1249 /* FIXME: bad hack to actually register also the alloc_tdb module without changining configure.in */
1250 ret = idmap_alloc_tdb_init();
1251 if (! NT_STATUS_IS_OK(ret)) {
1252 return ret;
1254 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);