s3-printing: remove old entries in pcap_cache_replace
[Samba.git] / source3 / winbindd / idmap_tdb.c
blob6d2ab8ef37eba9ed3abf97315a69809645f4798c
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
10 Copyright (C) Michael Adam 2009-2010
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "includes.h"
27 #include "winbindd.h"
28 #include "idmap.h"
29 #include "idmap_rw.h"
30 #include "dbwrap.h"
31 #include "../libcli/security/security.h"
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_IDMAP
36 /* idmap version determines auto-conversion - this is the database
37 structure version specifier. */
39 #define IDMAP_VERSION 2
41 struct idmap_tdb_context {
42 struct db_context *db;
43 struct idmap_rw_ops *rw_ops;
46 /* High water mark keys */
47 #define HWM_GROUP "GROUP HWM"
48 #define HWM_USER "USER HWM"
50 struct convert_fn_state {
51 struct db_context *db;
52 bool failed;
55 /*****************************************************************************
56 For idmap conversion: convert one record to new format
57 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
58 instead of the SID.
59 *****************************************************************************/
60 static int convert_fn(struct db_record *rec, void *private_data)
62 struct winbindd_domain *domain;
63 char *p;
64 NTSTATUS status;
65 struct dom_sid sid;
66 uint32 rid;
67 fstring keystr;
68 fstring dom_name;
69 TDB_DATA key2;
70 struct convert_fn_state *s = (struct convert_fn_state *)private_data;
72 DEBUG(10,("Converting %s\n", (const char *)rec->key.dptr));
74 p = strchr((const char *)rec->key.dptr, '/');
75 if (!p)
76 return 0;
78 *p = 0;
79 fstrcpy(dom_name, (const char *)rec->key.dptr);
80 *p++ = '/';
82 domain = find_domain_from_name(dom_name);
83 if (domain == NULL) {
84 /* We must delete the old record. */
85 DEBUG(0,("Unable to find domain %s\n", dom_name ));
86 DEBUG(0,("deleting record %s\n", (const char *)rec->key.dptr ));
88 status = rec->delete_rec(rec);
89 if (!NT_STATUS_IS_OK(status)) {
90 DEBUG(0, ("Unable to delete record %s:%s\n",
91 (const char *)rec->key.dptr,
92 nt_errstr(status)));
93 s->failed = true;
94 return -1;
97 return 0;
100 rid = atoi(p);
102 sid_compose(&sid, &domain->sid, rid);
104 sid_to_fstring(keystr, &sid);
105 key2 = string_term_tdb_data(keystr);
107 status = dbwrap_store(s->db, key2, rec->value, TDB_INSERT);
108 if (!NT_STATUS_IS_OK(status)) {
109 DEBUG(0,("Unable to add record %s:%s\n",
110 (const char *)key2.dptr,
111 nt_errstr(status)));
112 s->failed = true;
113 return -1;
116 status = dbwrap_store(s->db, rec->value, key2, TDB_REPLACE);
117 if (!NT_STATUS_IS_OK(status)) {
118 DEBUG(0,("Unable to update record %s:%s\n",
119 (const char *)rec->value.dptr,
120 nt_errstr(status)));
121 s->failed = true;
122 return -1;
125 status = rec->delete_rec(rec);
126 if (!NT_STATUS_IS_OK(status)) {
127 DEBUG(0,("Unable to delete record %s:%s\n",
128 (const char *)rec->key.dptr,
129 nt_errstr(status)));
130 s->failed = true;
131 return -1;
134 return 0;
137 /*****************************************************************************
138 Convert the idmap database from an older version.
139 *****************************************************************************/
141 static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db)
143 int32 vers;
144 bool bigendianheader;
145 struct convert_fn_state s;
147 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
149 bigendianheader = (db->get_flags(db) & TDB_BIGENDIAN) ? True : False;
151 vers = dbwrap_fetch_int32(db, "IDMAP_VERSION");
153 if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
154 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
156 * high and low records were created on a
157 * big endian machine and will need byte-reversing.
160 int32 wm;
162 wm = dbwrap_fetch_int32(db, HWM_USER);
164 if (wm != -1) {
165 wm = IREV(wm);
166 } else {
167 wm = dom->low_id;
170 if (dbwrap_store_int32(db, HWM_USER, wm) == -1) {
171 DEBUG(0, ("Unable to byteswap user hwm in idmap database\n"));
172 return False;
175 wm = dbwrap_fetch_int32(db, HWM_GROUP);
176 if (wm != -1) {
177 wm = IREV(wm);
178 } else {
179 wm = dom->low_id;
182 if (dbwrap_store_int32(db, HWM_GROUP, wm) == -1) {
183 DEBUG(0, ("Unable to byteswap group hwm in idmap database\n"));
184 return False;
188 s.db = db;
189 s.failed = false;
191 /* the old format stored as DOMAIN/rid - now we store the SID direct */
192 db->traverse(db, convert_fn, &s);
194 if (s.failed) {
195 DEBUG(0, ("Problem during conversion\n"));
196 return False;
199 if (dbwrap_store_int32(db, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
200 DEBUG(0, ("Unable to store idmap version in database\n"));
201 return False;
204 return True;
207 static NTSTATUS idmap_tdb_init_hwm(struct idmap_domain *dom)
209 int ret;
210 uint32_t low_uid;
211 uint32_t low_gid;
212 bool update_uid = false;
213 bool update_gid = false;
214 struct idmap_tdb_context *ctx;
216 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
218 low_uid = dbwrap_fetch_int32(ctx->db, HWM_USER);
219 if (low_uid == -1 || low_uid < dom->low_id) {
220 update_uid = true;
223 low_gid = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
224 if (low_gid == -1 || low_gid < dom->low_id) {
225 update_gid = true;
228 if (!update_uid && !update_gid) {
229 return NT_STATUS_OK;
232 if (ctx->db->transaction_start(ctx->db) != 0) {
233 DEBUG(0, ("Unable to start upgrade transaction!\n"));
234 return NT_STATUS_INTERNAL_DB_ERROR;
237 if (update_uid) {
238 ret = dbwrap_store_int32(ctx->db, HWM_USER, dom->low_id);
239 if (ret == -1) {
240 ctx->db->transaction_cancel(ctx->db);
241 DEBUG(0, ("Unable to initialise user hwm in idmap "
242 "database\n"));
243 return NT_STATUS_INTERNAL_DB_ERROR;
247 if (update_gid) {
248 ret = dbwrap_store_int32(ctx->db, HWM_GROUP, dom->low_id);
249 if (ret == -1) {
250 ctx->db->transaction_cancel(ctx->db);
251 DEBUG(0, ("Unable to initialise group hwm in idmap "
252 "database\n"));
253 return NT_STATUS_INTERNAL_DB_ERROR;
257 if (ctx->db->transaction_commit(ctx->db) != 0) {
258 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
259 return NT_STATUS_INTERNAL_DB_ERROR;
262 return NT_STATUS_OK;
265 static NTSTATUS idmap_tdb_open_db(struct idmap_domain *dom)
267 NTSTATUS ret;
268 TALLOC_CTX *mem_ctx;
269 char *tdbfile = NULL;
270 struct db_context *db = NULL;
271 int32_t version;
272 bool config_error = false;
273 struct idmap_tdb_context *ctx;
275 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
277 if (ctx->db) {
278 /* it is already open */
279 return NT_STATUS_OK;
282 /* use our own context here */
283 mem_ctx = talloc_stackframe();
285 /* use the old database if present */
286 tdbfile = state_path("winbindd_idmap.tdb");
287 if (!tdbfile) {
288 DEBUG(0, ("Out of memory!\n"));
289 ret = NT_STATUS_NO_MEMORY;
290 goto done;
293 DEBUG(10,("Opening tdbfile %s\n", tdbfile ));
295 /* Open idmap repository */
296 db = db_open(mem_ctx, tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
297 if (!db) {
298 DEBUG(0, ("Unable to open idmap database\n"));
299 ret = NT_STATUS_UNSUCCESSFUL;
300 goto done;
303 /* check against earlier versions */
304 version = dbwrap_fetch_int32(db, "IDMAP_VERSION");
305 if (version != IDMAP_VERSION) {
306 if (config_error) {
307 DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
308 "possible with incomplete configuration\n",
309 version, IDMAP_VERSION));
310 ret = NT_STATUS_UNSUCCESSFUL;
311 goto done;
313 if (db->transaction_start(db) != 0) {
314 DEBUG(0, ("Unable to start upgrade transaction!\n"));
315 ret = NT_STATUS_INTERNAL_DB_ERROR;
316 goto done;
319 if (!idmap_tdb_upgrade(dom, db)) {
320 db->transaction_cancel(db);
321 DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
322 ret = NT_STATUS_INTERNAL_DB_ERROR;
323 goto done;
326 if (db->transaction_commit(db) != 0) {
327 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
328 ret = NT_STATUS_INTERNAL_DB_ERROR;
329 goto done;
333 ctx->db = talloc_move(ctx, &db);
335 ret = idmap_tdb_init_hwm(dom);
337 done:
338 talloc_free(mem_ctx);
339 return ret;
342 /**********************************************************************
343 IDMAP ALLOC TDB BACKEND
344 **********************************************************************/
346 /**********************************
347 Allocate a new id.
348 **********************************/
350 struct idmap_tdb_allocate_id_context {
351 const char *hwmkey;
352 const char *hwmtype;
353 uint32_t high_hwm;
354 uint32_t hwm;
357 static NTSTATUS idmap_tdb_allocate_id_action(struct db_context *db,
358 void *private_data)
360 NTSTATUS ret;
361 struct idmap_tdb_allocate_id_context *state;
362 uint32_t hwm;
364 state = (struct idmap_tdb_allocate_id_context *)private_data;
366 hwm = dbwrap_fetch_int32(db, state->hwmkey);
367 if (hwm == -1) {
368 ret = NT_STATUS_INTERNAL_DB_ERROR;
369 goto done;
372 /* check it is in the range */
373 if (hwm > state->high_hwm) {
374 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
375 state->hwmtype, (unsigned long)state->high_hwm));
376 ret = NT_STATUS_UNSUCCESSFUL;
377 goto done;
380 /* fetch a new id and increment it */
381 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
382 if (!NT_STATUS_IS_OK(ret)) {
383 DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
384 state->hwmtype, nt_errstr(ret)));
385 goto done;
388 /* recheck it is in the range */
389 if (hwm > state->high_hwm) {
390 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
391 state->hwmtype, (unsigned long)state->high_hwm));
392 ret = NT_STATUS_UNSUCCESSFUL;
393 goto done;
396 ret = NT_STATUS_OK;
397 state->hwm = hwm;
399 done:
400 return ret;
403 static NTSTATUS idmap_tdb_allocate_id(struct idmap_domain *dom,
404 struct unixid *xid)
406 const char *hwmkey;
407 const char *hwmtype;
408 uint32_t high_hwm;
409 uint32_t hwm = 0;
410 NTSTATUS status;
411 struct idmap_tdb_allocate_id_context state;
412 struct idmap_tdb_context *ctx;
414 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
416 /* Get current high water mark */
417 switch (xid->type) {
419 case ID_TYPE_UID:
420 hwmkey = HWM_USER;
421 hwmtype = "UID";
422 break;
424 case ID_TYPE_GID:
425 hwmkey = HWM_GROUP;
426 hwmtype = "GID";
427 break;
429 default:
430 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
431 return NT_STATUS_INVALID_PARAMETER;
434 high_hwm = dom->high_id;
436 state.hwm = hwm;
437 state.high_hwm = high_hwm;
438 state.hwmtype = hwmtype;
439 state.hwmkey = hwmkey;
441 status = dbwrap_trans_do(ctx->db, idmap_tdb_allocate_id_action,
442 &state);
444 if (NT_STATUS_IS_OK(status)) {
445 xid->id = state.hwm;
446 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
447 } else {
448 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
451 return status;
455 * Allocate a new unix-ID.
456 * For now this is for the default idmap domain only.
457 * Should be extended later on.
459 static NTSTATUS idmap_tdb_get_new_id(struct idmap_domain *dom,
460 struct unixid *id)
462 NTSTATUS ret;
464 if (!strequal(dom->name, "*")) {
465 DEBUG(3, ("idmap_tdb_get_new_id: "
466 "Refusing allocation of a new unixid for domain'%s'. "
467 "Currently only supported for the default "
468 "domain \"*\".\n",
469 dom->name));
470 return NT_STATUS_NOT_IMPLEMENTED;
473 ret = idmap_tdb_allocate_id(dom, id);
475 return ret;
478 /**********************************************************************
479 IDMAP MAPPING TDB BACKEND
480 **********************************************************************/
482 /*****************************
483 Initialise idmap database.
484 *****************************/
486 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
487 const struct id_map *map);
489 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom, const char *params)
491 NTSTATUS ret;
492 struct idmap_tdb_context *ctx;
494 DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom->name));
496 ctx = talloc_zero(dom, struct idmap_tdb_context);
497 if ( ! ctx) {
498 DEBUG(0, ("Out of memory!\n"));
499 return NT_STATUS_NO_MEMORY;
502 /* load backend specific configuration here: */
503 #if 0
504 if (strequal(dom->name, "*")) {
505 } else {
507 #endif
509 ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
510 if (ctx->rw_ops == NULL) {
511 DEBUG(0, ("Out of memory!\n"));
512 ret = NT_STATUS_NO_MEMORY;
513 goto failed;
516 ctx->rw_ops->get_new_id = idmap_tdb_get_new_id;
517 ctx->rw_ops->set_mapping = idmap_tdb_set_mapping;
519 dom->private_data = ctx;
521 ret = idmap_tdb_open_db(dom);
522 if ( ! NT_STATUS_IS_OK(ret)) {
523 goto failed;
526 return NT_STATUS_OK;
528 failed:
529 talloc_free(ctx);
530 return ret;
535 * store a mapping in the database
538 struct idmap_tdb_set_mapping_context {
539 const char *ksidstr;
540 const char *kidstr;
543 static NTSTATUS idmap_tdb_set_mapping_action(struct db_context *db,
544 void *private_data)
546 NTSTATUS ret;
547 struct idmap_tdb_set_mapping_context *state;
549 state = (struct idmap_tdb_set_mapping_context *)private_data;
551 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
553 ret = dbwrap_store_bystring(db, state->ksidstr,
554 string_term_tdb_data(state->kidstr),
555 TDB_REPLACE);
556 if (!NT_STATUS_IS_OK(ret)) {
557 DEBUG(0, ("Error storing SID -> ID (%s -> %s): %s\n",
558 state->ksidstr, state->kidstr, nt_errstr(ret)));
559 goto done;
562 ret = dbwrap_store_bystring(db, state->kidstr,
563 string_term_tdb_data(state->ksidstr),
564 TDB_REPLACE);
565 if (!NT_STATUS_IS_OK(ret)) {
566 DEBUG(0, ("Error storing ID -> SID (%s -> %s): %s\n",
567 state->kidstr, state->ksidstr, nt_errstr(ret)));
568 goto done;
571 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
572 ret = NT_STATUS_OK;
574 done:
575 return ret;
578 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
579 const struct id_map *map)
581 struct idmap_tdb_context *ctx;
582 NTSTATUS ret;
583 char *ksidstr, *kidstr;
584 struct idmap_tdb_set_mapping_context state;
586 if (!map || !map->sid) {
587 return NT_STATUS_INVALID_PARAMETER;
590 ksidstr = kidstr = NULL;
592 /* TODO: should we filter a set_mapping using low/high filters ? */
594 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
596 switch (map->xid.type) {
598 case ID_TYPE_UID:
599 kidstr = talloc_asprintf(ctx, "UID %lu",
600 (unsigned long)map->xid.id);
601 break;
603 case ID_TYPE_GID:
604 kidstr = talloc_asprintf(ctx, "GID %lu",
605 (unsigned long)map->xid.id);
606 break;
608 default:
609 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
610 return NT_STATUS_INVALID_PARAMETER;
613 if (kidstr == NULL) {
614 DEBUG(0, ("ERROR: Out of memory!\n"));
615 ret = NT_STATUS_NO_MEMORY;
616 goto done;
619 ksidstr = sid_string_talloc(ctx, map->sid);
620 if (ksidstr == NULL) {
621 DEBUG(0, ("Out of memory!\n"));
622 ret = NT_STATUS_NO_MEMORY;
623 goto done;
626 state.ksidstr = ksidstr;
627 state.kidstr = kidstr;
629 ret = dbwrap_trans_do(ctx->db, idmap_tdb_set_mapping_action, &state);
631 done:
632 talloc_free(ksidstr);
633 talloc_free(kidstr);
634 return ret;
638 * Create a new mapping for an unmapped SID, also allocating a new ID.
639 * This should be run inside a transaction.
641 * TODO:
642 * Properly integrate this with multi domain idmap config:
643 * Currently, the allocator is default-config only.
645 static NTSTATUS idmap_tdb_new_mapping(struct idmap_domain *dom, struct id_map *map)
647 NTSTATUS ret;
648 struct idmap_tdb_context *ctx;
650 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
652 ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
654 return ret;
658 /**********************************
659 Single id to sid lookup function.
660 **********************************/
662 static NTSTATUS idmap_tdb_id_to_sid(struct idmap_domain *dom, struct id_map *map)
664 NTSTATUS ret;
665 TDB_DATA data;
666 char *keystr;
667 struct idmap_tdb_context *ctx;
669 if (!dom || !map) {
670 return NT_STATUS_INVALID_PARAMETER;
673 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
675 /* apply filters before checking */
676 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
677 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
678 map->xid.id, dom->low_id, dom->high_id));
679 return NT_STATUS_NONE_MAPPED;
682 switch (map->xid.type) {
684 case ID_TYPE_UID:
685 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
686 break;
688 case ID_TYPE_GID:
689 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
690 break;
692 default:
693 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
694 return NT_STATUS_INVALID_PARAMETER;
697 /* final SAFE_FREE safe */
698 data.dptr = NULL;
700 if (keystr == NULL) {
701 DEBUG(0, ("Out of memory!\n"));
702 ret = NT_STATUS_NO_MEMORY;
703 goto done;
706 DEBUG(10,("Fetching record %s\n", keystr));
708 /* Check if the mapping exists */
709 data = dbwrap_fetch_bystring(ctx->db, NULL, keystr);
711 if (!data.dptr) {
712 DEBUG(10,("Record %s not found\n", keystr));
713 ret = NT_STATUS_NONE_MAPPED;
714 goto done;
717 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
718 DEBUG(10,("INVALID SID (%s) in record %s\n",
719 (const char *)data.dptr, keystr));
720 ret = NT_STATUS_INTERNAL_DB_ERROR;
721 goto done;
724 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
725 ret = NT_STATUS_OK;
727 done:
728 talloc_free(data.dptr);
729 talloc_free(keystr);
730 return ret;
733 /**********************************
734 Single sid to id lookup function.
735 **********************************/
737 static NTSTATUS idmap_tdb_sid_to_id(struct idmap_domain *dom, struct id_map *map)
739 NTSTATUS ret;
740 TDB_DATA data;
741 char *keystr;
742 unsigned long rec_id = 0;
743 struct idmap_tdb_context *ctx;
744 TALLOC_CTX *tmp_ctx = talloc_stackframe();
746 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
748 keystr = sid_string_talloc(tmp_ctx, map->sid);
749 if (keystr == NULL) {
750 DEBUG(0, ("Out of memory!\n"));
751 ret = NT_STATUS_NO_MEMORY;
752 goto done;
755 DEBUG(10,("Fetching record %s\n", keystr));
757 /* Check if sid is present in database */
758 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
759 if (!data.dptr) {
760 DEBUG(10,("Record %s not found\n", keystr));
761 ret = NT_STATUS_NONE_MAPPED;
762 goto done;
765 /* What type of record is this ? */
766 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
767 map->xid.id = rec_id;
768 map->xid.type = ID_TYPE_UID;
769 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
770 ret = NT_STATUS_OK;
772 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
773 map->xid.id = rec_id;
774 map->xid.type = ID_TYPE_GID;
775 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
776 ret = NT_STATUS_OK;
778 } else { /* Unknown record type ! */
779 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
780 ret = NT_STATUS_INTERNAL_DB_ERROR;
781 goto done;
784 /* apply filters before returning result */
785 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
786 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
787 map->xid.id, dom->low_id, dom->high_id));
788 ret = NT_STATUS_NONE_MAPPED;
791 done:
792 talloc_free(tmp_ctx);
793 return ret;
796 /**********************************
797 lookup a set of unix ids.
798 **********************************/
800 static NTSTATUS idmap_tdb_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
802 struct idmap_tdb_context *ctx;
803 NTSTATUS ret;
804 int i;
806 /* initialize the status to avoid suprise */
807 for (i = 0; ids[i]; i++) {
808 ids[i]->status = ID_UNKNOWN;
811 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
813 for (i = 0; ids[i]; i++) {
814 ret = idmap_tdb_id_to_sid(dom, ids[i]);
815 if ( ! NT_STATUS_IS_OK(ret)) {
817 /* if it is just a failed mapping continue */
818 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
820 /* make sure it is marked as unmapped */
821 ids[i]->status = ID_UNMAPPED;
822 continue;
825 /* some fatal error occurred, return immediately */
826 goto done;
829 /* all ok, id is mapped */
830 ids[i]->status = ID_MAPPED;
833 ret = NT_STATUS_OK;
835 done:
836 return ret;
839 /**********************************
840 lookup a set of sids.
841 **********************************/
843 struct idmap_tdb_sids_to_unixids_context {
844 struct idmap_domain *dom;
845 struct id_map **ids;
846 bool allocate_unmapped;
849 static NTSTATUS idmap_tdb_sids_to_unixids_action(struct db_context *db,
850 void *private_data)
852 struct idmap_tdb_sids_to_unixids_context *state;
853 int i;
854 NTSTATUS ret = NT_STATUS_OK;
856 state = (struct idmap_tdb_sids_to_unixids_context *)private_data;
858 DEBUG(10, ("idmap_tdb_sids_to_unixids_action: "
859 " domain: [%s], allocate: %s\n",
860 state->dom->name,
861 state->allocate_unmapped ? "yes" : "no"));
863 for (i = 0; state->ids[i]; i++) {
864 if ((state->ids[i]->status == ID_UNKNOWN) ||
865 /* retry if we could not map in previous run: */
866 (state->ids[i]->status == ID_UNMAPPED))
868 NTSTATUS ret2;
870 ret2 = idmap_tdb_sid_to_id(state->dom, state->ids[i]);
871 if (!NT_STATUS_IS_OK(ret2)) {
873 /* if it is just a failed mapping, continue */
874 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
876 /* make sure it is marked as unmapped */
877 state->ids[i]->status = ID_UNMAPPED;
878 ret = STATUS_SOME_UNMAPPED;
879 } else {
880 /* some fatal error occurred, return immediately */
881 ret = ret2;
882 goto done;
884 } else {
885 /* all ok, id is mapped */
886 state->ids[i]->status = ID_MAPPED;
890 if ((state->ids[i]->status == ID_UNMAPPED) &&
891 state->allocate_unmapped)
893 ret = idmap_tdb_new_mapping(state->dom, state->ids[i]);
894 if (!NT_STATUS_IS_OK(ret)) {
895 goto done;
900 done:
901 return ret;
904 static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
906 struct idmap_tdb_context *ctx;
907 NTSTATUS ret;
908 int i;
909 struct idmap_tdb_sids_to_unixids_context state;
911 /* initialize the status to avoid suprise */
912 for (i = 0; ids[i]; i++) {
913 ids[i]->status = ID_UNKNOWN;
916 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
918 state.dom = dom;
919 state.ids = ids;
920 state.allocate_unmapped = false;
922 ret = idmap_tdb_sids_to_unixids_action(ctx->db, &state);
924 if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
925 state.allocate_unmapped = true;
926 ret = dbwrap_trans_do(ctx->db,
927 idmap_tdb_sids_to_unixids_action,
928 &state);
931 return ret;
935 /**********************************
936 Close the idmap tdb instance
937 **********************************/
939 static NTSTATUS idmap_tdb_close(struct idmap_domain *dom)
941 struct idmap_tdb_context *ctx;
943 if (dom->private_data) {
944 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
946 TALLOC_FREE(ctx->db);
948 return NT_STATUS_OK;
951 static struct idmap_methods db_methods = {
952 .init = idmap_tdb_db_init,
953 .unixids_to_sids = idmap_tdb_unixids_to_sids,
954 .sids_to_unixids = idmap_tdb_sids_to_unixids,
955 .allocate_id = idmap_tdb_get_new_id,
956 .close_fn = idmap_tdb_close
959 NTSTATUS idmap_tdb_init(void)
961 DEBUG(10, ("calling idmap_tdb_init\n"));
963 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);