s3:idmap_tdb: use filter from idmap_domain rather than from idmap_tdb_context
[Samba/gbeck.git] / source3 / winbindd / idmap_tdb.c
blob5f769206f5cbe49bdab52bf069f92ecc99758ba2
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 struct 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 struct idmap_tdb_allocate_id_context {
402 const char *hwmkey;
403 const char *hwmtype;
404 uint32_t high_hwm;
405 uint32_t hwm;
408 static NTSTATUS idmap_tdb_allocate_id_action(struct db_context *db,
409 void *private_data)
411 NTSTATUS ret;
412 struct idmap_tdb_allocate_id_context *state;
413 uint32_t hwm;
415 state = (struct idmap_tdb_allocate_id_context *)private_data;
417 hwm = dbwrap_fetch_int32(db, state->hwmkey);
418 if (hwm == -1) {
419 ret = NT_STATUS_INTERNAL_DB_ERROR;
420 goto done;
423 /* check it is in the range */
424 if (hwm > state->high_hwm) {
425 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
426 state->hwmtype, (unsigned long)state->high_hwm));
427 ret = NT_STATUS_UNSUCCESSFUL;
428 goto done;
431 /* fetch a new id and increment it */
432 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
433 if (!NT_STATUS_IS_OK(ret)) {
434 DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
435 state->hwmtype, nt_errstr(ret)));
436 goto done;
439 /* recheck it is in the range */
440 if (hwm > state->high_hwm) {
441 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
442 state->hwmtype, (unsigned long)state->high_hwm));
443 ret = NT_STATUS_UNSUCCESSFUL;
444 goto done;
447 ret = NT_STATUS_OK;
448 state->hwm = hwm;
450 done:
451 return ret;
454 static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
456 const char *hwmkey;
457 const char *hwmtype;
458 uint32_t high_hwm;
459 uint32_t hwm = 0;
460 NTSTATUS status;
461 struct idmap_tdb_allocate_id_context state;
463 /* Get current high water mark */
464 switch (xid->type) {
466 case ID_TYPE_UID:
467 hwmkey = HWM_USER;
468 hwmtype = "UID";
469 high_hwm = idmap_tdb_state.high_uid;
470 break;
472 case ID_TYPE_GID:
473 hwmkey = HWM_GROUP;
474 hwmtype = "GID";
475 high_hwm = idmap_tdb_state.high_gid;
476 break;
478 default:
479 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
480 return NT_STATUS_INVALID_PARAMETER;
483 state.hwm = hwm;
484 state.high_hwm = high_hwm;
485 state.hwmtype = hwmtype;
486 state.hwmkey = hwmkey;
488 status = dbwrap_trans_do(idmap_alloc_db, idmap_tdb_allocate_id_action,
489 &state);
491 if (NT_STATUS_IS_OK(status)) {
492 xid->id = state.hwm;
493 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
494 } else {
495 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
498 return status;
502 * Allocate a new unix-ID.
503 * For now this is for the default idmap domain only.
504 * Should be extended later on.
506 static NTSTATUS idmap_tdb_get_new_id(struct idmap_domain *dom,
507 struct unixid *id)
509 NTSTATUS ret;
511 if (!strequal(dom->name, "*")) {
512 DEBUG(3, ("idmap_tdb_get_new_id: "
513 "Refusing allocation of a new unixid for domain'%s'. "
514 "Currently only supported for the default "
515 "domain \"*\".\n",
516 dom->name));
517 return NT_STATUS_NOT_IMPLEMENTED;
520 ret = idmap_tdb_allocate_id(id);
522 return ret;
525 /**********************************
526 Close the alloc tdb
527 **********************************/
529 static NTSTATUS idmap_tdb_alloc_close(void)
531 TALLOC_FREE(idmap_alloc_db);
532 return NT_STATUS_OK;
535 /**********************************************************************
536 IDMAP MAPPING TDB BACKEND
537 **********************************************************************/
539 struct idmap_tdb_context {
540 struct db_context *db;
541 uint32_t filter_low_id;
542 uint32_t filter_high_id;
545 /*****************************
546 Initialise idmap database.
547 *****************************/
549 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom, const char *params)
551 NTSTATUS ret;
552 struct idmap_tdb_context *ctx;
554 DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom->name));
556 ctx = talloc(dom, struct idmap_tdb_context);
557 if ( ! ctx) {
558 DEBUG(0, ("Out of memory!\n"));
559 return NT_STATUS_NO_MEMORY;
562 if (strequal(dom->name, "*")) {
563 uid_t low_uid = 0;
564 uid_t high_uid = 0;
565 gid_t low_gid = 0;
566 gid_t high_gid = 0;
568 ctx->filter_low_id = 0;
569 ctx->filter_high_id = 0;
571 if (lp_idmap_uid(&low_uid, &high_uid)) {
572 ctx->filter_low_id = low_uid;
573 ctx->filter_high_id = high_uid;
574 } else {
575 DEBUG(3, ("Warning: 'idmap uid' not set!\n"));
578 if (lp_idmap_gid(&low_gid, &high_gid)) {
579 if ((low_gid != low_uid) || (high_gid != high_uid)) {
580 DEBUG(1, ("Warning: 'idmap uid' and 'idmap gid'"
581 " ranges do not agree -- building "
582 "intersection\n"));
583 ctx->filter_low_id = MAX(ctx->filter_low_id,
584 low_gid);
585 ctx->filter_high_id = MIN(ctx->filter_high_id,
586 high_gid);
588 } else {
589 DEBUG(3, ("Warning: 'idmap gid' not set!\n"));
591 } else {
592 char *config_option = NULL;
593 const char *range;
595 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
596 if ( ! config_option) {
597 DEBUG(0, ("Out of memory!\n"));
598 ret = NT_STATUS_NO_MEMORY;
599 goto failed;
602 range = lp_parm_const_string(-1, config_option, "range", NULL);
603 if (( ! range) ||
604 (sscanf(range, "%u - %u", &ctx->filter_low_id, &ctx->filter_high_id) != 2))
606 ctx->filter_low_id = 0;
607 ctx->filter_high_id = 0;
610 talloc_free(config_option);
613 if (ctx->filter_low_id > ctx->filter_high_id) {
614 ctx->filter_low_id = 0;
615 ctx->filter_high_id = 0;
618 DEBUG(10, ("idmap_tdb_db_init: filter range %u-%u loaded for domain "
619 "'%s'\n", ctx->filter_low_id, ctx->filter_high_id, dom->name));
621 ret = idmap_tdb_open_db(ctx, false, &ctx->db);
622 if ( ! NT_STATUS_IS_OK(ret)) {
623 goto failed;
626 dom->private_data = ctx;
628 return NT_STATUS_OK;
630 failed:
631 talloc_free(ctx);
632 return ret;
635 /**********************************
636 Single id to sid lookup function.
637 **********************************/
639 static NTSTATUS idmap_tdb_id_to_sid(struct idmap_domain *dom, struct id_map *map)
641 NTSTATUS ret;
642 TDB_DATA data;
643 char *keystr;
644 struct idmap_tdb_context *ctx;
646 if (!dom || !map) {
647 return NT_STATUS_INVALID_PARAMETER;
650 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
652 /* apply filters before checking */
653 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
654 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
655 map->xid.id, dom->low_id, dom->high_id));
656 return NT_STATUS_NONE_MAPPED;
659 switch (map->xid.type) {
661 case ID_TYPE_UID:
662 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
663 break;
665 case ID_TYPE_GID:
666 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
667 break;
669 default:
670 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
671 return NT_STATUS_INVALID_PARAMETER;
674 /* final SAFE_FREE safe */
675 data.dptr = NULL;
677 if (keystr == NULL) {
678 DEBUG(0, ("Out of memory!\n"));
679 ret = NT_STATUS_NO_MEMORY;
680 goto done;
683 DEBUG(10,("Fetching record %s\n", keystr));
685 /* Check if the mapping exists */
686 data = dbwrap_fetch_bystring(ctx->db, NULL, keystr);
688 if (!data.dptr) {
689 DEBUG(10,("Record %s not found\n", keystr));
690 ret = NT_STATUS_NONE_MAPPED;
691 goto done;
694 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
695 DEBUG(10,("INVALID SID (%s) in record %s\n",
696 (const char *)data.dptr, keystr));
697 ret = NT_STATUS_INTERNAL_DB_ERROR;
698 goto done;
701 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
702 ret = NT_STATUS_OK;
704 done:
705 talloc_free(data.dptr);
706 talloc_free(keystr);
707 return ret;
710 /**********************************
711 Single sid to id lookup function.
712 **********************************/
714 static NTSTATUS idmap_tdb_sid_to_id(struct idmap_domain *dom, struct id_map *map)
716 NTSTATUS ret;
717 TDB_DATA data;
718 char *keystr;
719 unsigned long rec_id = 0;
720 struct idmap_tdb_context *ctx;
721 TALLOC_CTX *tmp_ctx = talloc_stackframe();
723 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
725 keystr = sid_string_talloc(tmp_ctx, map->sid);
726 if (keystr == NULL) {
727 DEBUG(0, ("Out of memory!\n"));
728 ret = NT_STATUS_NO_MEMORY;
729 goto done;
732 DEBUG(10,("Fetching record %s\n", keystr));
734 /* Check if sid is present in database */
735 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
736 if (!data.dptr) {
737 DEBUG(10,("Record %s not found\n", keystr));
738 ret = NT_STATUS_NONE_MAPPED;
739 goto done;
742 /* What type of record is this ? */
743 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
744 map->xid.id = rec_id;
745 map->xid.type = ID_TYPE_UID;
746 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
747 ret = NT_STATUS_OK;
749 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
750 map->xid.id = rec_id;
751 map->xid.type = ID_TYPE_GID;
752 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
753 ret = NT_STATUS_OK;
755 } else { /* Unknown record type ! */
756 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
757 ret = NT_STATUS_INTERNAL_DB_ERROR;
758 goto done;
761 /* apply filters before returning result */
762 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
763 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
764 map->xid.id, dom->low_id, dom->high_id));
765 ret = NT_STATUS_NONE_MAPPED;
768 done:
769 talloc_free(tmp_ctx);
770 return ret;
773 /**********************************
774 lookup a set of unix ids.
775 **********************************/
777 static NTSTATUS idmap_tdb_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
779 struct idmap_tdb_context *ctx;
780 NTSTATUS ret;
781 int i;
783 /* initialize the status to avoid suprise */
784 for (i = 0; ids[i]; i++) {
785 ids[i]->status = ID_UNKNOWN;
788 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
790 for (i = 0; ids[i]; i++) {
791 ret = idmap_tdb_id_to_sid(dom, ids[i]);
792 if ( ! NT_STATUS_IS_OK(ret)) {
794 /* if it is just a failed mapping continue */
795 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
797 /* make sure it is marked as unmapped */
798 ids[i]->status = ID_UNMAPPED;
799 continue;
802 /* some fatal error occurred, return immediately */
803 goto done;
806 /* all ok, id is mapped */
807 ids[i]->status = ID_MAPPED;
810 ret = NT_STATUS_OK;
812 done:
813 return ret;
816 /**********************************
817 lookup a set of sids.
818 **********************************/
820 static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
822 struct idmap_tdb_context *ctx;
823 NTSTATUS ret;
824 int i;
826 /* initialize the status to avoid suprise */
827 for (i = 0; ids[i]; i++) {
828 ids[i]->status = ID_UNKNOWN;
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(dom, 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,
864 const struct id_map *map)
866 struct idmap_tdb_context *ctx;
867 NTSTATUS ret;
868 TDB_DATA ksid, kid;
869 char *ksidstr, *kidstr;
870 fstring tmp;
872 if (!map || !map->sid) {
873 return NT_STATUS_INVALID_PARAMETER;
876 ksidstr = kidstr = NULL;
878 /* TODO: should we filter a set_mapping using low/high filters ? */
880 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
882 switch (map->xid.type) {
884 case ID_TYPE_UID:
885 kidstr = talloc_asprintf(ctx, "UID %lu",
886 (unsigned long)map->xid.id);
887 break;
889 case ID_TYPE_GID:
890 kidstr = talloc_asprintf(ctx, "GID %lu",
891 (unsigned long)map->xid.id);
892 break;
894 default:
895 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
896 return NT_STATUS_INVALID_PARAMETER;
899 if (kidstr == NULL) {
900 DEBUG(0, ("ERROR: Out of memory!\n"));
901 ret = NT_STATUS_NO_MEMORY;
902 goto done;
905 if ((ksidstr = talloc_asprintf(
906 ctx, "%s", sid_to_fstring(tmp, map->sid))) == NULL) {
907 DEBUG(0, ("Out of memory!\n"));
908 ret = NT_STATUS_NO_MEMORY;
909 goto done;
912 DEBUG(10, ("Storing %s <-> %s map\n", ksidstr, kidstr));
913 kid = string_term_tdb_data(kidstr);
914 ksid = string_term_tdb_data(ksidstr);
916 if (ctx->db->transaction_start(ctx->db) != 0) {
917 DEBUG(0, ("Failed to start transaction for %s\n",
918 ksidstr));
919 ret = NT_STATUS_INTERNAL_DB_ERROR;
920 goto done;
923 ret = dbwrap_store(ctx->db, ksid, kid, TDB_REPLACE);
924 if (!NT_STATUS_IS_OK(ret)) {
925 ctx->db->transaction_cancel(ctx->db);
926 DEBUG(0, ("Error storing SID -> ID (%s -> %s): %s\n",
927 ksidstr, kidstr, nt_errstr(ret)));
928 goto done;
930 ret = dbwrap_store(ctx->db, kid, ksid, TDB_REPLACE);
931 if (!NT_STATUS_IS_OK(ret)) {
932 ctx->db->transaction_cancel(ctx->db);
933 DEBUG(0, ("Error storing ID -> SID (%s -> %s): %s\n",
934 kidstr, ksidstr, nt_errstr(ret)));
935 goto done;
938 if (ctx->db->transaction_commit(ctx->db) != 0) {
939 DEBUG(0, ("Failed to commit transaction for (%s -> %s)\n",
940 ksidstr, kidstr));
941 ret = NT_STATUS_INTERNAL_DB_ERROR;
942 goto done;
945 DEBUG(10,("Stored %s <-> %s\n", ksidstr, kidstr));
946 ret = NT_STATUS_OK;
948 done:
949 talloc_free(ksidstr);
950 talloc_free(kidstr);
951 return ret;
954 /**********************************
955 Close the idmap tdb instance
956 **********************************/
958 static NTSTATUS idmap_tdb_close(struct idmap_domain *dom)
960 struct idmap_tdb_context *ctx;
962 if (dom->private_data) {
963 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
965 TALLOC_FREE(ctx->db);
967 return NT_STATUS_OK;
970 static struct idmap_methods db_methods = {
971 .init = idmap_tdb_db_init,
972 .unixids_to_sids = idmap_tdb_unixids_to_sids,
973 .sids_to_unixids = idmap_tdb_sids_to_unixids,
974 .allocate_id = idmap_tdb_get_new_id,
975 .close_fn = idmap_tdb_close
978 NTSTATUS idmap_tdb_init(void)
980 DEBUG(10, ("calling idmap_tdb_init\n"));
982 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);