source: trunk/server/source3/winbindd/idmap_tdb.c

Last change on this file was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 23.4 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 idmap TDB backend
5
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
11
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.
16
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.
21
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/>.
24*/
25
26#include "includes.h"
27#include "system/filesys.h"
28#include "winbindd.h"
29#include "idmap.h"
30#include "idmap_rw.h"
31#include "dbwrap.h"
32#include "../libcli/security/security.h"
33#include "util_tdb.h"
34
35#undef DBGC_CLASS
36#define DBGC_CLASS DBGC_IDMAP
37
38/* idmap version determines auto-conversion - this is the database
39 structure version specifier. */
40
41#define IDMAP_VERSION 2
42
43struct idmap_tdb_context {
44 struct db_context *db;
45 struct idmap_rw_ops *rw_ops;
46};
47
48/* High water mark keys */
49#define HWM_GROUP "GROUP HWM"
50#define HWM_USER "USER HWM"
51
52struct convert_fn_state {
53 struct db_context *db;
54 bool failed;
55};
56
57/*****************************************************************************
58 For idmap conversion: convert one record to new format
59 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
60 instead of the SID.
61*****************************************************************************/
62static int convert_fn(struct db_record *rec, void *private_data)
63{
64 struct winbindd_domain *domain;
65 char *p;
66 NTSTATUS status;
67 struct dom_sid sid;
68 uint32 rid;
69 fstring keystr;
70 fstring dom_name;
71 TDB_DATA key2;
72 struct convert_fn_state *s = (struct convert_fn_state *)private_data;
73
74 DEBUG(10,("Converting %s\n", (const char *)rec->key.dptr));
75
76 p = strchr((const char *)rec->key.dptr, '/');
77 if (!p)
78 return 0;
79
80 *p = 0;
81 fstrcpy(dom_name, (const char *)rec->key.dptr);
82 *p++ = '/';
83
84 domain = find_domain_from_name(dom_name);
85 if (domain == NULL) {
86 /* We must delete the old record. */
87 DEBUG(0,("Unable to find domain %s\n", dom_name ));
88 DEBUG(0,("deleting record %s\n", (const char *)rec->key.dptr ));
89
90 status = rec->delete_rec(rec);
91 if (!NT_STATUS_IS_OK(status)) {
92 DEBUG(0, ("Unable to delete record %s:%s\n",
93 (const char *)rec->key.dptr,
94 nt_errstr(status)));
95 s->failed = true;
96 return -1;
97 }
98
99 return 0;
100 }
101
102 rid = atoi(p);
103
104 sid_compose(&sid, &domain->sid, rid);
105
106 sid_to_fstring(keystr, &sid);
107 key2 = string_term_tdb_data(keystr);
108
109 status = dbwrap_store(s->db, key2, rec->value, TDB_INSERT);
110 if (!NT_STATUS_IS_OK(status)) {
111 DEBUG(0,("Unable to add record %s:%s\n",
112 (const char *)key2.dptr,
113 nt_errstr(status)));
114 s->failed = true;
115 return -1;
116 }
117
118 status = dbwrap_store(s->db, rec->value, key2, TDB_REPLACE);
119 if (!NT_STATUS_IS_OK(status)) {
120 DEBUG(0,("Unable to update record %s:%s\n",
121 (const char *)rec->value.dptr,
122 nt_errstr(status)));
123 s->failed = true;
124 return -1;
125 }
126
127 status = rec->delete_rec(rec);
128 if (!NT_STATUS_IS_OK(status)) {
129 DEBUG(0,("Unable to delete record %s:%s\n",
130 (const char *)rec->key.dptr,
131 nt_errstr(status)));
132 s->failed = true;
133 return -1;
134 }
135
136 return 0;
137}
138
139/*****************************************************************************
140 Convert the idmap database from an older version.
141*****************************************************************************/
142
143static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db)
144{
145 int32 vers;
146 bool bigendianheader;
147 struct convert_fn_state s;
148
149 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
150
151 bigendianheader = (db->get_flags(db) & TDB_BIGENDIAN) ? True : False;
152
153 vers = dbwrap_fetch_int32(db, "IDMAP_VERSION");
154
155 if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
156 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
157 /*
158 * high and low records were created on a
159 * big endian machine and will need byte-reversing.
160 */
161
162 int32 wm;
163
164 wm = dbwrap_fetch_int32(db, HWM_USER);
165
166 if (wm != -1) {
167 wm = IREV(wm);
168 } else {
169 wm = dom->low_id;
170 }
171
172 if (dbwrap_store_int32(db, HWM_USER, wm) == -1) {
173 DEBUG(0, ("Unable to byteswap user hwm in idmap database\n"));
174 return False;
175 }
176
177 wm = dbwrap_fetch_int32(db, HWM_GROUP);
178 if (wm != -1) {
179 wm = IREV(wm);
180 } else {
181 wm = dom->low_id;
182 }
183
184 if (dbwrap_store_int32(db, HWM_GROUP, wm) == -1) {
185 DEBUG(0, ("Unable to byteswap group hwm in idmap database\n"));
186 return False;
187 }
188 }
189
190 s.db = db;
191 s.failed = false;
192
193 /* the old format stored as DOMAIN/rid - now we store the SID direct */
194 db->traverse(db, convert_fn, &s);
195
196 if (s.failed) {
197 DEBUG(0, ("Problem during conversion\n"));
198 return False;
199 }
200
201 if (dbwrap_store_int32(db, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
202 DEBUG(0, ("Unable to store idmap version in database\n"));
203 return False;
204 }
205
206 return True;
207}
208
209static NTSTATUS idmap_tdb_init_hwm(struct idmap_domain *dom)
210{
211 int ret;
212 uint32_t low_uid;
213 uint32_t low_gid;
214 bool update_uid = false;
215 bool update_gid = false;
216 struct idmap_tdb_context *ctx;
217
218 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
219
220 low_uid = dbwrap_fetch_int32(ctx->db, HWM_USER);
221 if (low_uid == -1 || low_uid < dom->low_id) {
222 update_uid = true;
223 }
224
225 low_gid = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
226 if (low_gid == -1 || low_gid < dom->low_id) {
227 update_gid = true;
228 }
229
230 if (!update_uid && !update_gid) {
231 return NT_STATUS_OK;
232 }
233
234 if (ctx->db->transaction_start(ctx->db) != 0) {
235 DEBUG(0, ("Unable to start upgrade transaction!\n"));
236 return NT_STATUS_INTERNAL_DB_ERROR;
237 }
238
239 if (update_uid) {
240 ret = dbwrap_store_int32(ctx->db, HWM_USER, dom->low_id);
241 if (ret == -1) {
242 ctx->db->transaction_cancel(ctx->db);
243 DEBUG(0, ("Unable to initialise user hwm in idmap "
244 "database\n"));
245 return NT_STATUS_INTERNAL_DB_ERROR;
246 }
247 }
248
249 if (update_gid) {
250 ret = dbwrap_store_int32(ctx->db, HWM_GROUP, dom->low_id);
251 if (ret == -1) {
252 ctx->db->transaction_cancel(ctx->db);
253 DEBUG(0, ("Unable to initialise group hwm in idmap "
254 "database\n"));
255 return NT_STATUS_INTERNAL_DB_ERROR;
256 }
257 }
258
259 if (ctx->db->transaction_commit(ctx->db) != 0) {
260 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
261 return NT_STATUS_INTERNAL_DB_ERROR;
262 }
263
264 return NT_STATUS_OK;
265}
266
267static NTSTATUS idmap_tdb_open_db(struct idmap_domain *dom)
268{
269 NTSTATUS ret;
270 TALLOC_CTX *mem_ctx;
271 char *tdbfile = NULL;
272 struct db_context *db = NULL;
273 int32_t version;
274 bool config_error = false;
275 struct idmap_tdb_context *ctx;
276
277 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
278
279 if (ctx->db) {
280 /* it is already open */
281 return NT_STATUS_OK;
282 }
283
284 /* use our own context here */
285 mem_ctx = talloc_stackframe();
286
287 /* use the old database if present */
288 tdbfile = state_path("winbindd_idmap.tdb");
289 if (!tdbfile) {
290 DEBUG(0, ("Out of memory!\n"));
291 ret = NT_STATUS_NO_MEMORY;
292 goto done;
293 }
294
295 DEBUG(10,("Opening tdbfile %s\n", tdbfile ));
296
297 /* Open idmap repository */
298 db = db_open(mem_ctx, tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
299 if (!db) {
300 DEBUG(0, ("Unable to open idmap database\n"));
301 ret = NT_STATUS_UNSUCCESSFUL;
302 goto done;
303 }
304
305 /* check against earlier versions */
306 version = dbwrap_fetch_int32(db, "IDMAP_VERSION");
307 if (version != IDMAP_VERSION) {
308 if (config_error) {
309 DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
310 "possible with incomplete configuration\n",
311 version, IDMAP_VERSION));
312 ret = NT_STATUS_UNSUCCESSFUL;
313 goto done;
314 }
315 if (db->transaction_start(db) != 0) {
316 DEBUG(0, ("Unable to start upgrade transaction!\n"));
317 ret = NT_STATUS_INTERNAL_DB_ERROR;
318 goto done;
319 }
320
321 if (!idmap_tdb_upgrade(dom, db)) {
322 db->transaction_cancel(db);
323 DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
324 ret = NT_STATUS_INTERNAL_DB_ERROR;
325 goto done;
326 }
327
328 if (db->transaction_commit(db) != 0) {
329 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
330 ret = NT_STATUS_INTERNAL_DB_ERROR;
331 goto done;
332 }
333 }
334
335 ctx->db = talloc_move(ctx, &db);
336
337 ret = idmap_tdb_init_hwm(dom);
338
339done:
340 talloc_free(mem_ctx);
341 return ret;
342}
343
344/**********************************************************************
345 IDMAP ALLOC TDB BACKEND
346**********************************************************************/
347
348/**********************************
349 Allocate a new id.
350**********************************/
351
352struct idmap_tdb_allocate_id_context {
353 const char *hwmkey;
354 const char *hwmtype;
355 uint32_t high_hwm;
356 uint32_t hwm;
357};
358
359static NTSTATUS idmap_tdb_allocate_id_action(struct db_context *db,
360 void *private_data)
361{
362 NTSTATUS ret;
363 struct idmap_tdb_allocate_id_context *state;
364 uint32_t hwm;
365
366 state = (struct idmap_tdb_allocate_id_context *)private_data;
367
368 hwm = dbwrap_fetch_int32(db, state->hwmkey);
369 if (hwm == -1) {
370 ret = NT_STATUS_INTERNAL_DB_ERROR;
371 goto done;
372 }
373
374 /* check it is in the range */
375 if (hwm > state->high_hwm) {
376 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
377 state->hwmtype, (unsigned long)state->high_hwm));
378 ret = NT_STATUS_UNSUCCESSFUL;
379 goto done;
380 }
381
382 /* fetch a new id and increment it */
383 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
384 if (!NT_STATUS_IS_OK(ret)) {
385 DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
386 state->hwmtype, nt_errstr(ret)));
387 goto done;
388 }
389
390 /* recheck it is in the range */
391 if (hwm > state->high_hwm) {
392 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
393 state->hwmtype, (unsigned long)state->high_hwm));
394 ret = NT_STATUS_UNSUCCESSFUL;
395 goto done;
396 }
397
398 ret = NT_STATUS_OK;
399 state->hwm = hwm;
400
401done:
402 return ret;
403}
404
405static NTSTATUS idmap_tdb_allocate_id(struct idmap_domain *dom,
406 struct unixid *xid)
407{
408 const char *hwmkey;
409 const char *hwmtype;
410 uint32_t high_hwm;
411 uint32_t hwm = 0;
412 NTSTATUS status;
413 struct idmap_tdb_allocate_id_context state;
414 struct idmap_tdb_context *ctx;
415
416 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
417
418 /* Get current high water mark */
419 switch (xid->type) {
420
421 case ID_TYPE_UID:
422 hwmkey = HWM_USER;
423 hwmtype = "UID";
424 break;
425
426 case ID_TYPE_GID:
427 hwmkey = HWM_GROUP;
428 hwmtype = "GID";
429 break;
430
431 default:
432 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
433 return NT_STATUS_INVALID_PARAMETER;
434 }
435
436 high_hwm = dom->high_id;
437
438 state.hwm = hwm;
439 state.high_hwm = high_hwm;
440 state.hwmtype = hwmtype;
441 state.hwmkey = hwmkey;
442
443 status = dbwrap_trans_do(ctx->db, idmap_tdb_allocate_id_action,
444 &state);
445
446 if (NT_STATUS_IS_OK(status)) {
447 xid->id = state.hwm;
448 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
449 } else {
450 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
451 }
452
453 return status;
454}
455
456/**
457 * Allocate a new unix-ID.
458 * For now this is for the default idmap domain only.
459 * Should be extended later on.
460 */
461static NTSTATUS idmap_tdb_get_new_id(struct idmap_domain *dom,
462 struct unixid *id)
463{
464 NTSTATUS ret;
465
466 if (!strequal(dom->name, "*")) {
467 DEBUG(3, ("idmap_tdb_get_new_id: "
468 "Refusing allocation of a new unixid for domain'%s'. "
469 "Currently only supported for the default "
470 "domain \"*\".\n",
471 dom->name));
472 return NT_STATUS_NOT_IMPLEMENTED;
473 }
474
475 ret = idmap_tdb_allocate_id(dom, id);
476
477 return ret;
478}
479
480/**********************************************************************
481 IDMAP MAPPING TDB BACKEND
482**********************************************************************/
483
484/*****************************
485 Initialise idmap database.
486*****************************/
487
488static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
489 const struct id_map *map);
490
491static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom)
492{
493 NTSTATUS ret;
494 struct idmap_tdb_context *ctx;
495
496 DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom->name));
497
498 ctx = talloc_zero(dom, struct idmap_tdb_context);
499 if ( ! ctx) {
500 DEBUG(0, ("Out of memory!\n"));
501 return NT_STATUS_NO_MEMORY;
502 }
503
504 /* load backend specific configuration here: */
505#if 0
506 if (strequal(dom->name, "*")) {
507 } else {
508 }
509#endif
510
511 ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
512 if (ctx->rw_ops == NULL) {
513 DEBUG(0, ("Out of memory!\n"));
514 ret = NT_STATUS_NO_MEMORY;
515 goto failed;
516 }
517
518 ctx->rw_ops->get_new_id = idmap_tdb_get_new_id;
519 ctx->rw_ops->set_mapping = idmap_tdb_set_mapping;
520
521 dom->private_data = ctx;
522
523 ret = idmap_tdb_open_db(dom);
524 if ( ! NT_STATUS_IS_OK(ret)) {
525 goto failed;
526 }
527
528 return NT_STATUS_OK;
529
530failed:
531 talloc_free(ctx);
532 return ret;
533}
534
535
536/**
537 * store a mapping in the database
538 */
539
540struct idmap_tdb_set_mapping_context {
541 const char *ksidstr;
542 const char *kidstr;
543};
544
545static NTSTATUS idmap_tdb_set_mapping_action(struct db_context *db,
546 void *private_data)
547{
548 NTSTATUS ret;
549 struct idmap_tdb_set_mapping_context *state;
550
551 state = (struct idmap_tdb_set_mapping_context *)private_data;
552
553 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
554
555 ret = dbwrap_store_bystring(db, state->ksidstr,
556 string_term_tdb_data(state->kidstr),
557 TDB_REPLACE);
558 if (!NT_STATUS_IS_OK(ret)) {
559 DEBUG(0, ("Error storing SID -> ID (%s -> %s): %s\n",
560 state->ksidstr, state->kidstr, nt_errstr(ret)));
561 goto done;
562 }
563
564 ret = dbwrap_store_bystring(db, state->kidstr,
565 string_term_tdb_data(state->ksidstr),
566 TDB_REPLACE);
567 if (!NT_STATUS_IS_OK(ret)) {
568 DEBUG(0, ("Error storing ID -> SID (%s -> %s): %s\n",
569 state->kidstr, state->ksidstr, nt_errstr(ret)));
570 goto done;
571 }
572
573 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
574 ret = NT_STATUS_OK;
575
576done:
577 return ret;
578}
579
580static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
581 const struct id_map *map)
582{
583 struct idmap_tdb_context *ctx;
584 NTSTATUS ret;
585 char *ksidstr, *kidstr;
586 struct idmap_tdb_set_mapping_context state;
587
588 if (!map || !map->sid) {
589 return NT_STATUS_INVALID_PARAMETER;
590 }
591
592 ksidstr = kidstr = NULL;
593
594 /* TODO: should we filter a set_mapping using low/high filters ? */
595
596 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
597
598 switch (map->xid.type) {
599
600 case ID_TYPE_UID:
601 kidstr = talloc_asprintf(ctx, "UID %lu",
602 (unsigned long)map->xid.id);
603 break;
604
605 case ID_TYPE_GID:
606 kidstr = talloc_asprintf(ctx, "GID %lu",
607 (unsigned long)map->xid.id);
608 break;
609
610 default:
611 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
612 return NT_STATUS_INVALID_PARAMETER;
613 }
614
615 if (kidstr == NULL) {
616 DEBUG(0, ("ERROR: Out of memory!\n"));
617 ret = NT_STATUS_NO_MEMORY;
618 goto done;
619 }
620
621 ksidstr = sid_string_talloc(ctx, map->sid);
622 if (ksidstr == NULL) {
623 DEBUG(0, ("Out of memory!\n"));
624 ret = NT_STATUS_NO_MEMORY;
625 goto done;
626 }
627
628 state.ksidstr = ksidstr;
629 state.kidstr = kidstr;
630
631 ret = dbwrap_trans_do(ctx->db, idmap_tdb_set_mapping_action, &state);
632
633done:
634 talloc_free(ksidstr);
635 talloc_free(kidstr);
636 return ret;
637}
638
639/**
640 * Create a new mapping for an unmapped SID, also allocating a new ID.
641 * This should be run inside a transaction.
642 *
643 * TODO:
644 * Properly integrate this with multi domain idmap config:
645 * Currently, the allocator is default-config only.
646 */
647static NTSTATUS idmap_tdb_new_mapping(struct idmap_domain *dom, struct id_map *map)
648{
649 NTSTATUS ret;
650 struct idmap_tdb_context *ctx;
651
652 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
653
654 ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
655
656 return ret;
657}
658
659
660/**********************************
661 Single id to sid lookup function.
662**********************************/
663
664static NTSTATUS idmap_tdb_id_to_sid(struct idmap_domain *dom, struct id_map *map)
665{
666 NTSTATUS ret;
667 TDB_DATA data;
668 char *keystr;
669 struct idmap_tdb_context *ctx;
670
671 if (!dom || !map) {
672 return NT_STATUS_INVALID_PARAMETER;
673 }
674
675 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
676
677 /* apply filters before checking */
678 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
679 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
680 map->xid.id, dom->low_id, dom->high_id));
681 return NT_STATUS_NONE_MAPPED;
682 }
683
684 switch (map->xid.type) {
685
686 case ID_TYPE_UID:
687 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
688 break;
689
690 case ID_TYPE_GID:
691 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
692 break;
693
694 default:
695 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
696 return NT_STATUS_INVALID_PARAMETER;
697 }
698
699 /* final SAFE_FREE safe */
700 data.dptr = NULL;
701
702 if (keystr == NULL) {
703 DEBUG(0, ("Out of memory!\n"));
704 ret = NT_STATUS_NO_MEMORY;
705 goto done;
706 }
707
708 DEBUG(10,("Fetching record %s\n", keystr));
709
710 /* Check if the mapping exists */
711 data = dbwrap_fetch_bystring(ctx->db, NULL, keystr);
712
713 if (!data.dptr) {
714 DEBUG(10,("Record %s not found\n", keystr));
715 ret = NT_STATUS_NONE_MAPPED;
716 goto done;
717 }
718
719 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
720 DEBUG(10,("INVALID SID (%s) in record %s\n",
721 (const char *)data.dptr, keystr));
722 ret = NT_STATUS_INTERNAL_DB_ERROR;
723 goto done;
724 }
725
726 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
727 ret = NT_STATUS_OK;
728
729done:
730 talloc_free(data.dptr);
731 talloc_free(keystr);
732 return ret;
733}
734
735/**********************************
736 Single sid to id lookup function.
737**********************************/
738
739static NTSTATUS idmap_tdb_sid_to_id(struct idmap_domain *dom, struct id_map *map)
740{
741 NTSTATUS ret;
742 TDB_DATA data;
743 char *keystr;
744 unsigned long rec_id = 0;
745 struct idmap_tdb_context *ctx;
746 TALLOC_CTX *tmp_ctx = talloc_stackframe();
747
748 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
749
750 keystr = sid_string_talloc(tmp_ctx, map->sid);
751 if (keystr == NULL) {
752 DEBUG(0, ("Out of memory!\n"));
753 ret = NT_STATUS_NO_MEMORY;
754 goto done;
755 }
756
757 DEBUG(10,("Fetching record %s\n", keystr));
758
759 /* Check if sid is present in database */
760 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
761 if (!data.dptr) {
762 DEBUG(10,("Record %s not found\n", keystr));
763 ret = NT_STATUS_NONE_MAPPED;
764 goto done;
765 }
766
767 /* What type of record is this ? */
768 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
769 map->xid.id = rec_id;
770 map->xid.type = ID_TYPE_UID;
771 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
772 ret = NT_STATUS_OK;
773
774 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
775 map->xid.id = rec_id;
776 map->xid.type = ID_TYPE_GID;
777 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
778 ret = NT_STATUS_OK;
779
780 } else { /* Unknown record type ! */
781 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
782 ret = NT_STATUS_INTERNAL_DB_ERROR;
783 goto done;
784 }
785
786 /* apply filters before returning result */
787 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
788 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
789 map->xid.id, dom->low_id, dom->high_id));
790 ret = NT_STATUS_NONE_MAPPED;
791 }
792
793done:
794 talloc_free(tmp_ctx);
795 return ret;
796}
797
798/**********************************
799 lookup a set of unix ids.
800**********************************/
801
802static NTSTATUS idmap_tdb_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
803{
804 NTSTATUS ret;
805 int i;
806
807 /* initialize the status to avoid suprise */
808 for (i = 0; ids[i]; i++) {
809 ids[i]->status = ID_UNKNOWN;
810 }
811
812 for (i = 0; ids[i]; i++) {
813 ret = idmap_tdb_id_to_sid(dom, ids[i]);
814 if ( ! NT_STATUS_IS_OK(ret)) {
815
816 /* if it is just a failed mapping continue */
817 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
818
819 /* make sure it is marked as unmapped */
820 ids[i]->status = ID_UNMAPPED;
821 continue;
822 }
823
824 /* some fatal error occurred, return immediately */
825 goto done;
826 }
827
828 /* all ok, id is mapped */
829 ids[i]->status = ID_MAPPED;
830 }
831
832 ret = NT_STATUS_OK;
833
834done:
835 return ret;
836}
837
838/**********************************
839 lookup a set of sids.
840**********************************/
841
842struct idmap_tdb_sids_to_unixids_context {
843 struct idmap_domain *dom;
844 struct id_map **ids;
845 bool allocate_unmapped;
846};
847
848static NTSTATUS idmap_tdb_sids_to_unixids_action(struct db_context *db,
849 void *private_data)
850{
851 struct idmap_tdb_sids_to_unixids_context *state;
852 int i;
853 NTSTATUS ret = NT_STATUS_OK;
854
855 state = (struct idmap_tdb_sids_to_unixids_context *)private_data;
856
857 DEBUG(10, ("idmap_tdb_sids_to_unixids_action: "
858 " domain: [%s], allocate: %s\n",
859 state->dom->name,
860 state->allocate_unmapped ? "yes" : "no"));
861
862 for (i = 0; state->ids[i]; i++) {
863 if ((state->ids[i]->status == ID_UNKNOWN) ||
864 /* retry if we could not map in previous run: */
865 (state->ids[i]->status == ID_UNMAPPED))
866 {
867 NTSTATUS ret2;
868
869 ret2 = idmap_tdb_sid_to_id(state->dom, state->ids[i]);
870 if (!NT_STATUS_IS_OK(ret2)) {
871
872 /* if it is just a failed mapping, continue */
873 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
874
875 /* make sure it is marked as unmapped */
876 state->ids[i]->status = ID_UNMAPPED;
877 ret = STATUS_SOME_UNMAPPED;
878 } else {
879 /* some fatal error occurred, return immediately */
880 ret = ret2;
881 goto done;
882 }
883 } else {
884 /* all ok, id is mapped */
885 state->ids[i]->status = ID_MAPPED;
886 }
887 }
888
889 if ((state->ids[i]->status == ID_UNMAPPED) &&
890 state->allocate_unmapped)
891 {
892 ret = idmap_tdb_new_mapping(state->dom, state->ids[i]);
893 if (!NT_STATUS_IS_OK(ret)) {
894 goto done;
895 }
896 }
897 }
898
899done:
900 return ret;
901}
902
903static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
904{
905 struct idmap_tdb_context *ctx;
906 NTSTATUS ret;
907 int i;
908 struct idmap_tdb_sids_to_unixids_context state;
909
910 /* initialize the status to avoid suprise */
911 for (i = 0; ids[i]; i++) {
912 ids[i]->status = ID_UNKNOWN;
913 }
914
915 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
916
917 state.dom = dom;
918 state.ids = ids;
919 state.allocate_unmapped = false;
920
921 ret = idmap_tdb_sids_to_unixids_action(ctx->db, &state);
922
923 if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
924 state.allocate_unmapped = true;
925 ret = dbwrap_trans_do(ctx->db,
926 idmap_tdb_sids_to_unixids_action,
927 &state);
928 }
929
930 return ret;
931}
932
933
934/**********************************
935 Close the idmap tdb instance
936**********************************/
937
938static struct idmap_methods db_methods = {
939 .init = idmap_tdb_db_init,
940 .unixids_to_sids = idmap_tdb_unixids_to_sids,
941 .sids_to_unixids = idmap_tdb_sids_to_unixids,
942 .allocate_id = idmap_tdb_get_new_id,
943};
944
945NTSTATUS idmap_tdb_init(void)
946{
947 DEBUG(10, ("calling idmap_tdb_init\n"));
948
949 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);
950}
Note: See TracBrowser for help on using the repository browser.