source: branches/samba-3.3.x/source/winbindd/idmap_tdb2.c

Last change on this file was 274, checked in by Herwig Bauernfeind, 16 years ago

Update 3.3 branch to 3.3.5

File size: 22.5 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 idmap TDB2 backend, used for clustered Samba setups.
5
6 This uses dbwrap to access tdb files. The location can be set
7 using tdb:idmap2.tdb =" in smb.conf
8
9 Copyright (C) Andrew Tridgell 2007
10
11 This is heavily based upon idmap_tdb.c, which is:
12
13 Copyright (C) Tim Potter 2000
14 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
15 Copyright (C) Jeremy Allison 2006
16 Copyright (C) Simo Sorce 2003-2006
17
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31*/
32
33#include "includes.h"
34#include "winbindd.h"
35
36#undef DBGC_CLASS
37#define DBGC_CLASS DBGC_IDMAP
38
39/* High water mark keys */
40#define HWM_GROUP "GROUP HWM"
41#define HWM_USER "USER HWM"
42
43static struct idmap_tdb2_state {
44 /* User and group id pool */
45 uid_t low_uid, high_uid; /* Range of uids to allocate */
46 gid_t low_gid, high_gid; /* Range of gids to allocate */
47 const char *idmap_script;
48} idmap_tdb2_state;
49
50
51
52/* handle to the permanent tdb */
53static struct db_context *idmap_tdb2;
54
55static NTSTATUS idmap_tdb2_alloc_load(void);
56
57/*
58 open the permanent tdb
59 */
60static NTSTATUS idmap_tdb2_open_db(void)
61{
62 char *db_path;
63
64 if (idmap_tdb2) {
65 /* its already open */
66 return NT_STATUS_OK;
67 }
68
69 db_path = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
70 if (db_path == NULL) {
71 /* fall back to the private directory, which, despite
72 its name, is usually on shared storage */
73 db_path = talloc_asprintf(NULL, "%s/idmap2.tdb", lp_private_dir());
74 }
75 NT_STATUS_HAVE_NO_MEMORY(db_path);
76
77 /* Open idmap repository */
78 idmap_tdb2 = db_open(NULL, db_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
79 TALLOC_FREE(db_path);
80
81 if (idmap_tdb2 == NULL) {
82 DEBUG(0, ("Unable to open idmap_tdb2 database '%s'\n",
83 db_path));
84 return NT_STATUS_UNSUCCESSFUL;
85 }
86
87 /* load the ranges and high/low water marks */
88 return idmap_tdb2_alloc_load();
89}
90
91
92/*
93 load the idmap allocation ranges and high/low water marks
94*/
95static NTSTATUS idmap_tdb2_alloc_load(void)
96{
97 uid_t low_uid = 0;
98 uid_t high_uid = 0;
99 gid_t low_gid = 0;
100 gid_t high_gid = 0;
101 uint32 low_id;
102
103 /* see if a idmap script is configured */
104 idmap_tdb2_state.idmap_script = lp_parm_const_string(-1, "idmap",
105 "script", NULL);
106
107 if (idmap_tdb2_state.idmap_script) {
108 DEBUG(1, ("using idmap script '%s'\n",
109 idmap_tdb2_state.idmap_script));
110 }
111
112 /* load ranges */
113
114 /* Create high water marks for group and user id */
115 if (!lp_idmap_uid(&low_uid, &high_uid)
116 || !lp_idmap_gid(&low_gid, &high_gid)) {
117 DEBUG(1, ("idmap uid or idmap gid missing\n"));
118 return NT_STATUS_UNSUCCESSFUL;
119 }
120
121 idmap_tdb2_state.low_uid = low_uid;
122 idmap_tdb2_state.high_uid = high_uid;
123 idmap_tdb2_state.low_gid = low_gid;
124 idmap_tdb2_state.high_gid = high_gid;
125
126 if (idmap_tdb2_state.high_uid <= idmap_tdb2_state.low_uid) {
127 DEBUG(1, ("idmap uid range missing or invalid\n"));
128 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
129 return NT_STATUS_UNSUCCESSFUL;
130 }
131
132 if (((low_id = dbwrap_fetch_int32(idmap_tdb2,
133 HWM_USER)) == -1) ||
134 (low_id < idmap_tdb2_state.low_uid)) {
135 if (!NT_STATUS_IS_OK(dbwrap_trans_store_int32(
136 idmap_tdb2, HWM_USER,
137 idmap_tdb2_state.low_uid))) {
138 DEBUG(0, ("Unable to initialise user hwm in idmap "
139 "database\n"));
140 return NT_STATUS_INTERNAL_DB_ERROR;
141 }
142 }
143
144 if (idmap_tdb2_state.high_gid <= idmap_tdb2_state.low_gid) {
145 DEBUG(1, ("idmap gid range missing or invalid\n"));
146 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
147 return NT_STATUS_UNSUCCESSFUL;
148 }
149
150 if (((low_id = dbwrap_fetch_int32(idmap_tdb2,
151 HWM_GROUP)) == -1) ||
152 (low_id < idmap_tdb2_state.low_gid)) {
153 if (!NT_STATUS_IS_OK(dbwrap_trans_store_int32(
154 idmap_tdb2, HWM_GROUP,
155 idmap_tdb2_state.low_gid))) {
156 DEBUG(0, ("Unable to initialise group hwm in idmap "
157 "database\n"));
158 return NT_STATUS_INTERNAL_DB_ERROR;
159 }
160 }
161
162 return NT_STATUS_OK;
163}
164
165
166/*
167 Initialise idmap alloc database.
168*/
169static NTSTATUS idmap_tdb2_alloc_init(const char *params)
170{
171 /* nothing to do - we want to avoid opening the permanent
172 database if possible. Instead we load the params when we
173 first need it. */
174 return NT_STATUS_OK;
175}
176
177
178/*
179 Allocate a new id.
180*/
181static NTSTATUS idmap_tdb2_allocate_id(struct unixid *xid)
182{
183 bool ret;
184 const char *hwmkey;
185 const char *hwmtype;
186 uint32_t high_hwm;
187 uint32_t hwm;
188 int res;
189 NTSTATUS status;
190
191 status = idmap_tdb2_open_db();
192 NT_STATUS_NOT_OK_RETURN(status);
193
194 /* Get current high water mark */
195 switch (xid->type) {
196
197 case ID_TYPE_UID:
198 hwmkey = HWM_USER;
199 hwmtype = "UID";
200 high_hwm = idmap_tdb2_state.high_uid;
201 break;
202
203 case ID_TYPE_GID:
204 hwmkey = HWM_GROUP;
205 hwmtype = "GID";
206 high_hwm = idmap_tdb2_state.high_gid;
207 break;
208
209 default:
210 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
211 return NT_STATUS_INVALID_PARAMETER;
212 }
213
214 res = idmap_tdb2->transaction_start(idmap_tdb2);
215 if (res != 0) {
216 DEBUG(1,(__location__ " Failed to start transaction\n"));
217 return NT_STATUS_UNSUCCESSFUL;
218 }
219
220 if ((hwm = dbwrap_fetch_int32(idmap_tdb2, hwmkey)) == -1) {
221 idmap_tdb2->transaction_cancel(idmap_tdb2);
222 return NT_STATUS_INTERNAL_DB_ERROR;
223 }
224
225 /* check it is in the range */
226 if (hwm > high_hwm) {
227 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
228 hwmtype, (unsigned long)high_hwm));
229 idmap_tdb2->transaction_cancel(idmap_tdb2);
230 return NT_STATUS_UNSUCCESSFUL;
231 }
232
233 /* fetch a new id and increment it */
234 ret = dbwrap_change_uint32_atomic(idmap_tdb2, hwmkey, &hwm, 1);
235 if (ret == -1) {
236 DEBUG(1, ("Fatal error while fetching a new %s value\n!", hwmtype));
237 idmap_tdb2->transaction_cancel(idmap_tdb2);
238 return NT_STATUS_UNSUCCESSFUL;
239 }
240
241 /* recheck it is in the range */
242 if (hwm > high_hwm) {
243 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
244 hwmtype, (unsigned long)high_hwm));
245 idmap_tdb2->transaction_cancel(idmap_tdb2);
246 return NT_STATUS_UNSUCCESSFUL;
247 }
248
249 res = idmap_tdb2->transaction_commit(idmap_tdb2);
250 if (res != 0) {
251 DEBUG(1,(__location__ " Failed to commit transaction\n"));
252 return NT_STATUS_UNSUCCESSFUL;
253 }
254
255 xid->id = hwm;
256 DEBUG(10,("New %s = %d\n", hwmtype, hwm));
257
258 return NT_STATUS_OK;
259}
260
261/*
262 Get current highest id.
263*/
264static NTSTATUS idmap_tdb2_get_hwm(struct unixid *xid)
265{
266 const char *hwmkey;
267 const char *hwmtype;
268 uint32_t hwm;
269 uint32_t high_hwm;
270 NTSTATUS status;
271
272 status = idmap_tdb2_open_db();
273 NT_STATUS_NOT_OK_RETURN(status);
274
275 /* Get current high water mark */
276 switch (xid->type) {
277
278 case ID_TYPE_UID:
279 hwmkey = HWM_USER;
280 hwmtype = "UID";
281 high_hwm = idmap_tdb2_state.high_uid;
282 break;
283
284 case ID_TYPE_GID:
285 hwmkey = HWM_GROUP;
286 hwmtype = "GID";
287 high_hwm = idmap_tdb2_state.high_gid;
288 break;
289
290 default:
291 return NT_STATUS_INVALID_PARAMETER;
292 }
293
294 if ((hwm = dbwrap_fetch_int32(idmap_tdb2, hwmkey)) == -1) {
295 return NT_STATUS_INTERNAL_DB_ERROR;
296 }
297
298 xid->id = hwm;
299
300 /* Warn if it is out of range */
301 if (hwm >= high_hwm) {
302 DEBUG(0, ("Warning: %s range full!! (max: %lu)\n",
303 hwmtype, (unsigned long)high_hwm));
304 }
305
306 return NT_STATUS_OK;
307}
308
309/*
310 Set high id.
311*/
312static NTSTATUS idmap_tdb2_set_hwm(struct unixid *xid)
313{
314 /* not supported, or we would invalidate the cache tdb on
315 other nodes */
316 DEBUG(0,("idmap_tdb2_set_hwm not supported\n"));
317 return NT_STATUS_NOT_SUPPORTED;
318}
319
320/*
321 Close the alloc tdb
322*/
323static NTSTATUS idmap_tdb2_alloc_close(void)
324{
325 /* don't actually close it */
326 return NT_STATUS_OK;
327}
328
329/*
330 IDMAP MAPPING TDB BACKEND
331*/
332struct idmap_tdb2_context {
333 uint32_t filter_low_id;
334 uint32_t filter_high_id;
335};
336
337/*
338 Initialise idmap database.
339*/
340static NTSTATUS idmap_tdb2_db_init(struct idmap_domain *dom,
341 const char *params)
342{
343 NTSTATUS ret;
344 struct idmap_tdb2_context *ctx;
345 NTSTATUS status;
346
347 status = idmap_tdb2_open_db();
348 NT_STATUS_NOT_OK_RETURN(status);
349
350 ctx = talloc(dom, struct idmap_tdb2_context);
351 if ( ! ctx) {
352 DEBUG(0, ("Out of memory!\n"));
353 return NT_STATUS_NO_MEMORY;
354 }
355
356 if (strequal(dom->name, "*")) {
357 uid_t low_uid = 0;
358 uid_t high_uid = 0;
359 gid_t low_gid = 0;
360 gid_t high_gid = 0;
361
362 ctx->filter_low_id = 0;
363 ctx->filter_high_id = 0;
364
365 if (lp_idmap_uid(&low_uid, &high_uid)) {
366 ctx->filter_low_id = low_uid;
367 ctx->filter_high_id = high_uid;
368 } else {
369 DEBUG(3, ("Warning: 'idmap uid' not set!\n"));
370 }
371
372 if (lp_idmap_gid(&low_gid, &high_gid)) {
373 if ((low_gid != low_uid) || (high_gid != high_uid)) {
374 DEBUG(1, ("Warning: 'idmap uid' and 'idmap gid'"
375 " ranges do not agree -- building "
376 "intersection\n"));
377 ctx->filter_low_id = MAX(ctx->filter_low_id,
378 low_gid);
379 ctx->filter_high_id = MIN(ctx->filter_high_id,
380 high_gid);
381 }
382 } else {
383 DEBUG(3, ("Warning: 'idmap gid' not set!\n"));
384 }
385 } else {
386 char *config_option = NULL;
387 const char *range;
388 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
389 if ( ! config_option) {
390 DEBUG(0, ("Out of memory!\n"));
391 ret = NT_STATUS_NO_MEMORY;
392 goto failed;
393 }
394
395 range = lp_parm_const_string(-1, config_option, "range", NULL);
396 if (( ! range) ||
397 (sscanf(range, "%u - %u", &ctx->filter_low_id, &ctx->filter_high_id) != 2))
398 {
399 ctx->filter_low_id = 0;
400 ctx->filter_high_id = 0;
401 }
402
403 talloc_free(config_option);
404 }
405
406 if (ctx->filter_low_id > ctx->filter_high_id) {
407 ctx->filter_low_id = 0;
408 ctx->filter_high_id = 0;
409 }
410
411 dom->private_data = ctx;
412
413 return NT_STATUS_OK;
414
415failed:
416 talloc_free(ctx);
417 return ret;
418}
419
420
421/*
422 run a script to perform a mapping
423
424 The script should the following command lines:
425
426 SIDTOID S-1-xxxx
427 IDTOSID UID xxxx
428 IDTOSID GID xxxx
429
430 and should return one of the following as a single line of text
431 UID:xxxx
432 GID:xxxx
433 SID:xxxx
434 ERR:xxxx
435 */
436static NTSTATUS idmap_tdb2_script(struct idmap_tdb2_context *ctx, struct id_map *map,
437 const char *fmt, ...)
438{
439 va_list ap;
440 char *cmd;
441 FILE *p;
442 char line[64];
443 unsigned long v;
444
445 cmd = talloc_asprintf(ctx, "%s ", idmap_tdb2_state.idmap_script);
446 NT_STATUS_HAVE_NO_MEMORY(cmd);
447
448 va_start(ap, fmt);
449 cmd = talloc_vasprintf_append(cmd, fmt, ap);
450 va_end(ap);
451 NT_STATUS_HAVE_NO_MEMORY(cmd);
452
453 p = popen(cmd, "r");
454 talloc_free(cmd);
455 if (p == NULL) {
456 return NT_STATUS_NONE_MAPPED;
457 }
458
459 if (fgets(line, sizeof(line)-1, p) == NULL) {
460 pclose(p);
461 return NT_STATUS_NONE_MAPPED;
462 }
463 pclose(p);
464
465 DEBUG(10,("idmap script gave: %s\n", line));
466
467 if (sscanf(line, "UID:%lu", &v) == 1) {
468 map->xid.id = v;
469 map->xid.type = ID_TYPE_UID;
470 } else if (sscanf(line, "GID:%lu", &v) == 1) {
471 map->xid.id = v;
472 map->xid.type = ID_TYPE_GID;
473 } else if (strncmp(line, "SID:S-", 6) == 0) {
474 if (!string_to_sid(map->sid, &line[4])) {
475 DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
476 line, idmap_tdb2_state.idmap_script));
477 return NT_STATUS_NONE_MAPPED;
478 }
479 } else {
480 DEBUG(0,("Bad reply '%s' from idmap script %s\n",
481 line, idmap_tdb2_state.idmap_script));
482 return NT_STATUS_NONE_MAPPED;
483 }
484
485 return NT_STATUS_OK;
486}
487
488
489
490/*
491 Single id to sid lookup function.
492*/
493static NTSTATUS idmap_tdb2_id_to_sid(struct idmap_tdb2_context *ctx, struct id_map *map)
494{
495 NTSTATUS ret;
496 TDB_DATA data;
497 char *keystr;
498 NTSTATUS status;
499
500 status = idmap_tdb2_open_db();
501 NT_STATUS_NOT_OK_RETURN(status);
502
503 if (!ctx || !map) {
504 return NT_STATUS_INVALID_PARAMETER;
505 }
506
507 /* apply filters before checking */
508 if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
509 (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
510 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
511 map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
512 return NT_STATUS_NONE_MAPPED;
513 }
514
515 switch (map->xid.type) {
516
517 case ID_TYPE_UID:
518 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
519 break;
520
521 case ID_TYPE_GID:
522 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
523 break;
524
525 default:
526 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
527 return NT_STATUS_INVALID_PARAMETER;
528 }
529
530 /* final SAFE_FREE safe */
531 data.dptr = NULL;
532
533 if (keystr == NULL) {
534 DEBUG(0, ("Out of memory!\n"));
535 ret = NT_STATUS_NO_MEMORY;
536 goto done;
537 }
538
539 DEBUG(10,("Fetching record %s\n", keystr));
540
541 /* Check if the mapping exists */
542 data = dbwrap_fetch_bystring(idmap_tdb2, keystr, keystr);
543
544 if (!data.dptr) {
545 fstring sidstr;
546
547 DEBUG(10,("Record %s not found\n", keystr));
548 if (idmap_tdb2_state.idmap_script == NULL) {
549 ret = NT_STATUS_NONE_MAPPED;
550 goto done;
551 }
552
553 ret = idmap_tdb2_script(ctx, map, "IDTOSID %s", keystr);
554
555 /* store it on shared storage */
556 if (!NT_STATUS_IS_OK(ret)) {
557 goto done;
558 }
559
560 if (sid_to_fstring(sidstr, map->sid)) {
561 /* both forward and reverse mappings */
562 dbwrap_store_bystring(idmap_tdb2, keystr,
563 string_term_tdb_data(sidstr),
564 TDB_REPLACE);
565 dbwrap_store_bystring(idmap_tdb2, sidstr,
566 string_term_tdb_data(keystr),
567 TDB_REPLACE);
568 }
569 goto done;
570 }
571
572 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
573 DEBUG(10,("INVALID SID (%s) in record %s\n",
574 (const char *)data.dptr, keystr));
575 ret = NT_STATUS_INTERNAL_DB_ERROR;
576 goto done;
577 }
578
579 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
580 ret = NT_STATUS_OK;
581
582done:
583 talloc_free(keystr);
584 return ret;
585}
586
587
588/*
589 Single sid to id lookup function.
590*/
591static NTSTATUS idmap_tdb2_sid_to_id(struct idmap_tdb2_context *ctx, struct id_map *map)
592{
593 NTSTATUS ret;
594 TDB_DATA data;
595 char *keystr;
596 unsigned long rec_id = 0;
597 NTSTATUS status;
598
599 status = idmap_tdb2_open_db();
600 NT_STATUS_NOT_OK_RETURN(status);
601
602 if ((keystr = sid_string_talloc(ctx, map->sid)) == NULL) {
603 DEBUG(0, ("Out of memory!\n"));
604 ret = NT_STATUS_NO_MEMORY;
605 goto done;
606 }
607
608 DEBUG(10,("Fetching record %s\n", keystr));
609
610 /* Check if sid is present in database */
611 data = dbwrap_fetch_bystring(idmap_tdb2, keystr, keystr);
612 if (!data.dptr) {
613 fstring idstr;
614
615 DEBUG(10,(__location__ " Record %s not found\n", keystr));
616
617 if (idmap_tdb2_state.idmap_script == NULL) {
618 ret = NT_STATUS_NONE_MAPPED;
619 goto done;
620 }
621
622 ret = idmap_tdb2_script(ctx, map, "SIDTOID %s", keystr);
623 /* store it on shared storage */
624 if (!NT_STATUS_IS_OK(ret)) {
625 goto done;
626 }
627
628 snprintf(idstr, sizeof(idstr), "%cID %lu",
629 map->xid.type == ID_TYPE_UID?'U':'G',
630 (unsigned long)map->xid.id);
631 /* store both forward and reverse mappings */
632 dbwrap_store_bystring(idmap_tdb2, keystr, string_term_tdb_data(idstr),
633 TDB_REPLACE);
634 dbwrap_store_bystring(idmap_tdb2, idstr, string_term_tdb_data(keystr),
635 TDB_REPLACE);
636 goto done;
637 }
638
639 /* What type of record is this ? */
640 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
641 map->xid.id = rec_id;
642 map->xid.type = ID_TYPE_UID;
643 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
644 ret = NT_STATUS_OK;
645
646 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
647 map->xid.id = rec_id;
648 map->xid.type = ID_TYPE_GID;
649 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
650 ret = NT_STATUS_OK;
651
652 } else { /* Unknown record type ! */
653 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
654 ret = NT_STATUS_INTERNAL_DB_ERROR;
655 }
656
657 /* apply filters before returning result */
658 if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
659 (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
660 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
661 map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
662 ret = NT_STATUS_NONE_MAPPED;
663 }
664
665done:
666 talloc_free(keystr);
667 return ret;
668}
669
670/*
671 lookup a set of unix ids.
672*/
673static NTSTATUS idmap_tdb2_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
674{
675 struct idmap_tdb2_context *ctx;
676 NTSTATUS ret;
677 int i;
678
679 /* initialize the status to avoid suprise */
680 for (i = 0; ids[i]; i++) {
681 ids[i]->status = ID_UNKNOWN;
682 }
683
684 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
685
686 for (i = 0; ids[i]; i++) {
687 ret = idmap_tdb2_id_to_sid(ctx, ids[i]);
688 if ( ! NT_STATUS_IS_OK(ret)) {
689
690 /* if it is just a failed mapping continue */
691 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
692
693 /* make sure it is marked as unmapped */
694 ids[i]->status = ID_UNMAPPED;
695 continue;
696 }
697
698 /* some fatal error occurred, return immediately */
699 goto done;
700 }
701
702 /* all ok, id is mapped */
703 ids[i]->status = ID_MAPPED;
704 }
705
706 ret = NT_STATUS_OK;
707
708done:
709 return ret;
710}
711
712/*
713 lookup a set of sids.
714*/
715static NTSTATUS idmap_tdb2_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
716{
717 struct idmap_tdb2_context *ctx;
718 NTSTATUS ret;
719 int i;
720
721 /* initialize the status to avoid suprise */
722 for (i = 0; ids[i]; i++) {
723 ids[i]->status = ID_UNKNOWN;
724 }
725
726 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
727
728 for (i = 0; ids[i]; i++) {
729 ret = idmap_tdb2_sid_to_id(ctx, ids[i]);
730 if ( ! NT_STATUS_IS_OK(ret)) {
731
732 /* if it is just a failed mapping continue */
733 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
734
735 /* make sure it is marked as unmapped */
736 ids[i]->status = ID_UNMAPPED;
737 continue;
738 }
739
740 /* some fatal error occurred, return immediately */
741 goto done;
742 }
743
744 /* all ok, id is mapped */
745 ids[i]->status = ID_MAPPED;
746 }
747
748 ret = NT_STATUS_OK;
749
750done:
751 return ret;
752}
753
754
755/*
756 set a mapping.
757*/
758static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom, const struct id_map *map)
759{
760 struct idmap_tdb2_context *ctx;
761 NTSTATUS ret;
762 TDB_DATA data;
763 char *ksidstr, *kidstr;
764 int res;
765 bool started_transaction = false;
766
767 if (!map || !map->sid) {
768 return NT_STATUS_INVALID_PARAMETER;
769 }
770
771 ksidstr = kidstr = NULL;
772
773 /* TODO: should we filter a set_mapping using low/high filters ? */
774
775 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
776
777 switch (map->xid.type) {
778
779 case ID_TYPE_UID:
780 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
781 break;
782
783 case ID_TYPE_GID:
784 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
785 break;
786
787 default:
788 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
789 return NT_STATUS_INVALID_PARAMETER;
790 }
791
792 if (kidstr == NULL) {
793 DEBUG(0, ("ERROR: Out of memory!\n"));
794 ret = NT_STATUS_NO_MEMORY;
795 goto done;
796 }
797
798 if (!(ksidstr = sid_string_talloc(ctx, map->sid))) {
799 DEBUG(0, ("Out of memory!\n"));
800 ret = NT_STATUS_NO_MEMORY;
801 goto done;
802 }
803
804 DEBUG(10, ("Storing %s <-> %s map\n", ksidstr, kidstr));
805
806 res = idmap_tdb2->transaction_start(idmap_tdb2);
807 if (res != 0) {
808 DEBUG(1,(__location__ " Failed to start transaction\n"));
809 ret = NT_STATUS_UNSUCCESSFUL;
810 goto done;
811 }
812
813 started_transaction = true;
814
815 /* check wheter sid mapping is already present in db */
816 data = dbwrap_fetch_bystring(idmap_tdb2, ksidstr, ksidstr);
817 if (data.dptr) {
818 ret = NT_STATUS_OBJECT_NAME_COLLISION;
819 goto done;
820 }
821
822 ret = dbwrap_store_bystring(idmap_tdb2, ksidstr, string_term_tdb_data(kidstr),
823 TDB_INSERT);
824 if (!NT_STATUS_IS_OK(ret)) {
825 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
826 goto done;
827 }
828 ret = dbwrap_store_bystring(idmap_tdb2, kidstr, string_term_tdb_data(ksidstr),
829 TDB_INSERT);
830 if (!NT_STATUS_IS_OK(ret)) {
831 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
832 /* try to remove the previous stored SID -> ID map */
833 dbwrap_delete_bystring(idmap_tdb2, ksidstr);
834 goto done;
835 }
836
837 started_transaction = false;
838
839 res = idmap_tdb2->transaction_commit(idmap_tdb2);
840 if (res != 0) {
841 DEBUG(1,(__location__ " Failed to commit transaction\n"));
842 ret = NT_STATUS_UNSUCCESSFUL;
843 goto done;
844 }
845
846 DEBUG(10,("Stored %s <-> %s\n", ksidstr, kidstr));
847 ret = NT_STATUS_OK;
848
849done:
850 if (started_transaction) {
851 idmap_tdb2->transaction_cancel(idmap_tdb2);
852 }
853 talloc_free(ksidstr);
854 talloc_free(kidstr);
855 return ret;
856}
857
858/*
859 remove a mapping.
860*/
861static NTSTATUS idmap_tdb2_remove_mapping(struct idmap_domain *dom, const struct id_map *map)
862{
863 /* not supported as it would invalidate the cache tdb on other
864 nodes */
865 DEBUG(0,("idmap_tdb2_remove_mapping not supported\n"));
866 return NT_STATUS_NOT_SUPPORTED;
867}
868
869/*
870 Close the idmap tdb instance
871*/
872static NTSTATUS idmap_tdb2_close(struct idmap_domain *dom)
873{
874 /* don't do anything */
875 return NT_STATUS_OK;
876}
877
878
879/*
880 Dump all mappings out
881*/
882static NTSTATUS idmap_tdb2_dump_data(struct idmap_domain *dom, struct id_map **maps, int *num_maps)
883{
884 DEBUG(0,("idmap_tdb2_dump_data not supported\n"));
885 return NT_STATUS_NOT_SUPPORTED;
886}
887
888static struct idmap_methods db_methods = {
889 .init = idmap_tdb2_db_init,
890 .unixids_to_sids = idmap_tdb2_unixids_to_sids,
891 .sids_to_unixids = idmap_tdb2_sids_to_unixids,
892 .set_mapping = idmap_tdb2_set_mapping,
893 .remove_mapping = idmap_tdb2_remove_mapping,
894 .dump_data = idmap_tdb2_dump_data,
895 .close_fn = idmap_tdb2_close
896};
897
898static struct idmap_alloc_methods db_alloc_methods = {
899 .init = idmap_tdb2_alloc_init,
900 .allocate_id = idmap_tdb2_allocate_id,
901 .get_id_hwm = idmap_tdb2_get_hwm,
902 .set_id_hwm = idmap_tdb2_set_hwm,
903 .close_fn = idmap_tdb2_alloc_close
904};
905
906NTSTATUS idmap_tdb2_init(void)
907{
908 NTSTATUS ret;
909
910 /* register both backends */
911 ret = smb_register_idmap_alloc(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_alloc_methods);
912 if (! NT_STATUS_IS_OK(ret)) {
913 DEBUG(0, ("Unable to register idmap alloc tdb2 module: %s\n", get_friendly_nt_error_msg(ret)));
914 return ret;
915 }
916
917 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_methods);
918}
Note: See TracBrowser for help on using the repository browser.