r20116: Start merging in the work done to create the new idmap subsystem.
[samba.git] / source / nsswitch / idmap_tdb.c
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    
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 2 of the License, or
14    (at your option) any later version.
15    
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.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #include "includes.h"
27 #include "winbindd.h"
28
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_IDMAP
31
32 /* High water mark keys */
33 #define HWM_GROUP  "GROUP HWM"
34 #define HWM_USER   "USER HWM"
35
36 static struct idmap_tdb_state {
37
38         /* User and group id pool */
39         uid_t low_uid, high_uid;               /* Range of uids to allocate */
40         gid_t low_gid, high_gid;               /* Range of gids to allocate */
41
42 } idmap_tdb_state;
43
44 /*****************************************************************************
45  For idmap conversion: convert one record to new format
46  Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
47  instead of the SID.
48 *****************************************************************************/
49 static int convert_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA data, void *state)
50 {
51         struct winbindd_domain *domain;
52         char *p;
53         DOM_SID sid;
54         uint32 rid;
55         fstring keystr;
56         fstring dom_name;
57         TDB_DATA key2;
58         BOOL *failed = (BOOL *)state;
59
60         DEBUG(10,("Converting %s\n", key.dptr));
61
62         p = strchr(key.dptr, '/');
63         if (!p)
64                 return 0;
65
66         *p = 0;
67         fstrcpy(dom_name, key.dptr);
68         *p++ = '/';
69
70         domain = find_domain_from_name(dom_name);
71         if (domain == NULL) {
72                 /* We must delete the old record. */
73                 DEBUG(0,("Unable to find domain %s\n", dom_name ));
74                 DEBUG(0,("deleting record %s\n", key.dptr ));
75
76                 if (tdb_delete(tdb, key) != 0) {
77                         DEBUG(0, ("Unable to delete record %s\n", key.dptr));
78                         *failed = True;
79                         return -1;
80                 }
81
82                 return 0;
83         }
84
85         rid = atoi(p);
86
87         sid_copy(&sid, &domain->sid);
88         sid_append_rid(&sid, rid);
89
90         sid_to_string(keystr, &sid);
91         key2.dptr = keystr;
92         key2.dsize = strlen(keystr) + 1;
93
94         if (tdb_store(tdb, key2, data, TDB_INSERT) != 0) {
95                 DEBUG(0,("Unable to add record %s\n", key2.dptr ));
96                 *failed = True;
97                 return -1;
98         }
99
100         if (tdb_store(tdb, data, key2, TDB_REPLACE) != 0) {
101                 DEBUG(0,("Unable to update record %s\n", data.dptr ));
102                 *failed = True;
103                 return -1;
104         }
105
106         if (tdb_delete(tdb, key) != 0) {
107                 DEBUG(0,("Unable to delete record %s\n", key.dptr ));
108                 *failed = True;
109                 return -1;
110         }
111
112         return 0;
113 }
114
115 /*****************************************************************************
116  Convert the idmap database from an older version.
117 *****************************************************************************/
118
119 static BOOL idmap_tdb_convert(const char *idmap_name)
120 {
121         int32 vers;
122         BOOL bigendianheader;
123         BOOL failed = False;
124         TDB_CONTEXT *idmap_tdb;
125
126         if (!(idmap_tdb = tdb_open_log(idmap_name, 0,
127                                         TDB_DEFAULT, O_RDWR,
128                                         0600))) {
129                 DEBUG(0, ("Unable to open idmap database\n"));
130                 return False;
131         }
132
133         bigendianheader = (tdb_get_flags(idmap_tdb) & TDB_BIGENDIAN) ? True : False;
134
135         vers = tdb_fetch_int32(idmap_tdb, "IDMAP_VERSION");
136
137         if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
138                 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
139                 /*
140                  * high and low records were created on a
141                  * big endian machine and will need byte-reversing.
142                  */
143
144                 int32 wm;
145
146                 wm = tdb_fetch_int32(idmap_tdb, HWM_USER);
147
148                 if (wm != -1) {
149                         wm = IREV(wm);
150                 }  else {
151                         wm = idmap_tdb_state.low_uid;
152                 }
153
154                 if (tdb_store_int32(idmap_tdb, HWM_USER, wm) == -1) {
155                         DEBUG(0, ("Unable to byteswap user hwm in idmap database\n"));
156                         tdb_close(idmap_tdb);
157                         return False;
158                 }
159
160                 wm = tdb_fetch_int32(idmap_tdb, HWM_GROUP);
161                 if (wm != -1) {
162                         wm = IREV(wm);
163                 } else {
164                         wm = idmap_tdb_state.low_gid;
165                 }
166
167                 if (tdb_store_int32(idmap_tdb, HWM_GROUP, wm) == -1) {
168                         DEBUG(0, ("Unable to byteswap group hwm in idmap database\n"));
169                         tdb_close(idmap_tdb);
170                         return False;
171                 }
172         }
173
174         /* the old format stored as DOMAIN/rid - now we store the SID direct */
175         tdb_traverse(idmap_tdb, convert_fn, &failed);
176
177         if (failed) {
178                 DEBUG(0, ("Problem during conversion\n"));
179                 tdb_close(idmap_tdb);
180                 return False;
181         }
182
183         if (tdb_store_int32(idmap_tdb, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
184                 DEBUG(0, ("Unable to dtore idmap version in databse\n"));
185                 tdb_close(idmap_tdb);
186                 return False;
187         }
188
189         tdb_close(idmap_tdb);
190         return True;
191 }
192
193 /*****************************************************************************
194  Convert the idmap database from an older version if necessary
195 *****************************************************************************/
196
197 BOOL idmap_tdb_upgrade(TALLOC_CTX *ctx, const char *tdbfile)
198 {
199         char *backup_name;
200
201         DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
202
203         backup_name = talloc_asprintf(ctx, "%s.bak", tdbfile);
204         if ( ! backup_name) {
205                 DEBUG(0, ("Out of memory!\n"));
206                 return False;
207         }
208
209         if (backup_tdb(tdbfile, backup_name, 0) != 0) {
210                 DEBUG(0, ("Could not backup idmap database\n"));
211                 talloc_free(backup_name);
212                 return False;
213         }
214
215         talloc_free(backup_name);
216         return idmap_tdb_convert(tdbfile);
217 }
218
219 /* WARNING: We can't open a tdb twice inthe same process, for that reason
220  * I'm going to use a hack with open ref counts to open the winbindd_idmap.tdb
221  * only once. We will later decide whether to split the db in multiple files
222  * or come up with a better solution to share them. */
223
224 static TDB_CONTEXT *idmap_tdb_common_ctx;
225 static int idmap_tdb_open_ref_count = 0;
226
227 static NTSTATUS idmap_tdb_open_db(TALLOC_CTX *memctx, TDB_CONTEXT **tdbctx)
228 {
229         NTSTATUS ret;
230         TALLOC_CTX *ctx;
231         SMB_STRUCT_STAT stbuf;
232         char *tdbfile = NULL;
233         int32 version;
234         BOOL tdb_is_new = False;
235
236         if (idmap_tdb_open_ref_count) { /* the tdb has already been opened */
237                 idmap_tdb_open_ref_count++;
238                 *tdbctx = idmap_tdb_common_ctx;
239                 return NT_STATUS_OK;
240         }
241
242         /* use our own context here */
243         ctx = talloc_new(memctx);
244         if (!ctx) {
245                 DEBUG(0, ("Out of memory!\n"));
246                 return NT_STATUS_NO_MEMORY;
247         }
248
249         /* use the old database if present */
250         tdbfile = talloc_strdup(ctx, lock_path("winbindd_idmap.tdb"));
251         if (!tdbfile) {
252                 DEBUG(0, ("Out of memory!\n"));
253                 ret = NT_STATUS_NO_MEMORY;
254                 goto done;
255         }
256
257         if (!file_exist(tdbfile, &stbuf)) {
258                 tdb_is_new = True;
259         }
260
261         DEBUG(10,("Opening tdbfile %s\n", tdbfile ));
262
263         /* Open idmap repository */
264         if (!(idmap_tdb_common_ctx = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644))) {
265                 DEBUG(0, ("Unable to open idmap database\n"));
266                 ret = NT_STATUS_UNSUCCESSFUL;
267                 goto done;
268         }
269
270         if (tdb_is_new) {
271                 /* the file didn't existed before opening it, let's
272                  * store idmap version as nobody else yet opened and
273                  * stored it. I do not like this method but didn't
274                  * found a way to understand if an opened tdb have
275                  * been just created or not --- SSS */
276                 tdb_store_int32(idmap_tdb_common_ctx, "IDMAP_VERSION", IDMAP_VERSION);
277         }
278
279         /* check against earlier versions */
280         version = tdb_fetch_int32(idmap_tdb_common_ctx, "IDMAP_VERSION");
281         if (version != IDMAP_VERSION) {
282                 
283                 /* backup_tdb expects the tdb not to be open */
284                 tdb_close(idmap_tdb_common_ctx);
285
286                 if ( ! idmap_tdb_upgrade(ctx, tdbfile)) {
287                 
288                         DEBUG(0, ("Unable to open idmap database, it's in an old formati, and upgrade failed!\n"));
289                         ret = NT_STATUS_INTERNAL_DB_ERROR;
290                         goto done;
291                 }
292
293                 /* Re-Open idmap repository */
294                 if (!(idmap_tdb_common_ctx = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644))) {
295                         DEBUG(0, ("Unable to open idmap database\n"));
296                         ret = NT_STATUS_UNSUCCESSFUL;
297                         goto done;
298                 }
299         }
300
301         *tdbctx = idmap_tdb_common_ctx;
302         idmap_tdb_open_ref_count++;
303         ret = NT_STATUS_OK;
304
305 done:
306         talloc_free(ctx);
307         return ret;
308 }
309
310  /* NEVER use tdb_close() except for the conversion routines that are guaranteed
311  * to run only when the database is opened the first time, always use this function. */ 
312
313 BOOL idmap_tdb_tdb_close(TDB_CONTEXT *tdbctx)
314 {
315         if (tdbctx != idmap_tdb_common_ctx) {
316                 DEBUG(0, ("ERROR: Invalid tdb context!"));
317                 return False;
318         }
319
320         idmap_tdb_open_ref_count--;
321         if (idmap_tdb_open_ref_count) {
322                 return True;
323         }
324
325         return tdb_close(idmap_tdb_common_ctx);
326 }
327
328 /**********************************************************************
329  IDMAP ALLOC TDB BACKEND
330 **********************************************************************/
331  
332 static TDB_CONTEXT *idmap_alloc_tdb;
333
334 /**********************************
335  Initialise idmap alloc database. 
336 **********************************/
337
338 static NTSTATUS idmap_tdb_alloc_init( const char *params )
339 {
340         NTSTATUS ret;
341         TALLOC_CTX *ctx;
342         const char *range;
343         uint32_t low_id = 0;
344         uint32_t high_id = 0;
345
346         /* use our own context here */
347         ctx = talloc_new(NULL);
348         if (!ctx) {
349                 DEBUG(0, ("Out of memory!\n"));
350                 return NT_STATUS_NO_MEMORY;
351         }
352
353         ret = idmap_tdb_open_db(ctx, &idmap_alloc_tdb);
354         if ( ! NT_STATUS_IS_OK(ret)) {
355                 talloc_free(ctx);
356                 return ret;
357         }
358
359         talloc_free(ctx);
360
361         /* load ranges */
362         idmap_tdb_state.low_uid = 0;
363         idmap_tdb_state.high_uid = 0;
364         idmap_tdb_state.low_gid = 0;
365         idmap_tdb_state.high_gid = 0;
366
367         range = lp_parm_const_string(-1, "idmap alloc config", "range", NULL);
368         if (range && range[0]) {
369                 if (sscanf(range, "%u - %u", &low_id, &high_id) == 2) {
370                         if (low_id < high_id) {
371                                 idmap_tdb_state.low_gid = idmap_tdb_state.low_uid = low_id;
372                                 idmap_tdb_state.high_gid = idmap_tdb_state.high_uid = high_id;
373                         } else {
374                                 DEBUG(1, ("ERROR: invalid idmap alloc range [%s]", range));
375                         }
376                 } else {
377                         DEBUG(1, ("ERROR: invalid syntax for idmap alloc config:range [%s]", range));
378                 }
379         }
380
381         /* Create high water marks for group and user id */
382         if (lp_idmap_uid(&low_id, &high_id)) {
383                 idmap_tdb_state.low_uid = low_id;
384                 idmap_tdb_state.high_uid = high_id;
385         }
386
387         if (lp_idmap_gid(&low_id, &high_id)) {
388                 idmap_tdb_state.low_gid = low_id;
389                 idmap_tdb_state.high_gid = high_id;
390         }
391
392         if (idmap_tdb_state.high_uid <= idmap_tdb_state.low_uid) {
393                 DEBUG(1, ("idmap uid range missing or invalid\n"));
394                 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
395                 return NT_STATUS_UNSUCCESSFUL;
396         } else {
397                 if (((low_id = tdb_fetch_int32(idmap_alloc_tdb, HWM_USER)) == -1) ||
398                     (low_id < idmap_tdb_state.low_uid)) {
399                         if (tdb_store_int32(idmap_alloc_tdb, HWM_USER, idmap_tdb_state.low_uid) == -1) {
400                                 DEBUG(0, ("Unable to initialise user hwm in idmap database\n"));
401                                 return NT_STATUS_INTERNAL_DB_ERROR;
402                         }
403                 }
404         }
405
406         if (idmap_tdb_state.high_gid <= idmap_tdb_state.low_gid) {
407                 DEBUG(1, ("idmap gid range missing or invalid\n"));
408                 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
409                 return NT_STATUS_UNSUCCESSFUL;
410         } else {
411                 if (((low_id = tdb_fetch_int32(idmap_alloc_tdb, HWM_GROUP)) == -1) ||
412                     (low_id < idmap_tdb_state.low_gid)) {
413                         if (tdb_store_int32(idmap_alloc_tdb, HWM_GROUP, idmap_tdb_state.low_gid) == -1) {
414                                 DEBUG(0, ("Unable to initialise group hwm in idmap database\n"));
415                                 return NT_STATUS_INTERNAL_DB_ERROR;
416                         }
417                 }
418         }
419
420         return NT_STATUS_OK;
421 }
422
423 /**********************************
424  Allocate a new id. 
425 **********************************/
426
427 static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
428 {
429         BOOL ret;
430         const char *hwmkey;
431         const char *hwmtype;
432         uint32_t high_hwm;
433         uint32_t hwm;
434
435         /* Get current high water mark */
436         switch (xid->type) {
437
438         case ID_TYPE_UID:
439                 hwmkey = HWM_USER;
440                 hwmtype = "UID";
441                 high_hwm = idmap_tdb_state.high_uid;
442                 break;
443
444         case ID_TYPE_GID:
445                 hwmkey = HWM_GROUP;
446                 hwmtype = "GID";
447                 high_hwm = idmap_tdb_state.high_gid;
448                 break;
449
450         default:
451                 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
452                 return NT_STATUS_INVALID_PARAMETER;
453         }
454
455         if ((hwm = tdb_fetch_int32(idmap_alloc_tdb, hwmkey)) == -1) {
456                 return NT_STATUS_INTERNAL_DB_ERROR;
457         }
458
459         /* check it is in the range */
460         if (hwm > high_hwm) {
461                 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n", 
462                           hwmtype, (unsigned long)high_hwm));
463                 return NT_STATUS_UNSUCCESSFUL;
464         }
465
466         /* fetch a new id and increment it */
467         ret = tdb_change_uint32_atomic(idmap_alloc_tdb, hwmkey, &hwm, 1);
468         if (!ret) {
469                 DEBUG(1, ("Fatal error while fetching a new %s value\n!", hwmtype));
470                 return NT_STATUS_UNSUCCESSFUL;
471         }
472
473         /* recheck it is in the range */
474         if (hwm > high_hwm) {
475                 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n", 
476                           hwmtype, (unsigned long)high_hwm));
477                 return NT_STATUS_UNSUCCESSFUL;
478         }
479         
480         xid->id = hwm;
481         DEBUG(10,("New %s = %d\n", hwmtype, hwm));
482
483         return NT_STATUS_OK;
484 }
485
486 /**********************************
487  Get current highest id. 
488 **********************************/
489
490 static NTSTATUS idmap_tdb_get_hwm(struct unixid *xid)
491 {
492         const char *hwmkey;
493         const char *hwmtype;
494         uint32_t hwm;
495         uint32_t high_hwm;
496
497         /* Get current high water mark */
498         switch (xid->type) {
499
500         case ID_TYPE_UID:
501                 hwmkey = HWM_USER;
502                 hwmtype = "UID";
503                 high_hwm = idmap_tdb_state.high_uid;
504                 break;
505
506         case ID_TYPE_GID:
507                 hwmkey = HWM_GROUP;
508                 hwmtype = "GID";
509                 high_hwm = idmap_tdb_state.high_gid;
510                 break;
511
512         default:
513                 return NT_STATUS_INVALID_PARAMETER;
514         }
515
516         if ((hwm = tdb_fetch_int32(idmap_alloc_tdb, hwmkey)) == -1) {
517                 return NT_STATUS_INTERNAL_DB_ERROR;
518         }
519
520         xid->id = hwm;
521
522         /* Warn if it is out of range */
523         if (hwm >= high_hwm) {
524                 DEBUG(0, ("Warning: %s range full!! (max: %lu)\n", 
525                           hwmtype, (unsigned long)high_hwm));
526         }
527
528         return NT_STATUS_OK;
529 }
530
531 /**********************************
532  Set high id. 
533 **********************************/
534
535 static NTSTATUS idmap_tdb_set_hwm(struct unixid *xid)
536 {
537         const char *hwmkey;
538         const char *hwmtype;
539         uint32_t hwm;
540         uint32_t high_hwm;
541
542         /* Get current high water mark */
543         switch (xid->type) {
544
545         case ID_TYPE_UID:
546                 hwmkey = HWM_USER;
547                 hwmtype = "UID";
548                 high_hwm = idmap_tdb_state.high_uid;
549                 break;
550
551         case ID_TYPE_GID:
552                 hwmkey = HWM_GROUP;
553                 hwmtype = "GID";
554                 high_hwm = idmap_tdb_state.high_gid;
555                 break;
556
557         default:
558                 return NT_STATUS_INVALID_PARAMETER;
559         }
560
561         hwm = xid->id;
562
563         if ((hwm = tdb_store_int32(idmap_alloc_tdb, hwmkey, hwm)) == -1) {
564                 return NT_STATUS_INTERNAL_DB_ERROR;
565         }
566
567         /* Warn if it is out of range */
568         if (hwm >= high_hwm) {
569                 DEBUG(0, ("Warning: %s range full!! (max: %lu)\n", 
570                           hwmtype, (unsigned long)high_hwm));
571         }
572
573         return NT_STATUS_OK;
574 }
575
576 /**********************************
577  Close the alloc tdb 
578 **********************************/
579
580 static NTSTATUS idmap_tdb_alloc_close(void)
581 {
582         if (idmap_alloc_tdb) {
583                 if (idmap_tdb_tdb_close(idmap_alloc_tdb) == 0) {
584                         return NT_STATUS_OK;
585                 } else {
586                         return NT_STATUS_UNSUCCESSFUL;
587                 }
588         }
589         return NT_STATUS_OK;
590 }
591
592 /**********************************************************************
593  IDMAP MAPPING TDB BACKEND
594 **********************************************************************/
595  
596 struct idmap_tdb_context {
597         TDB_CONTEXT *tdb;
598         uint32_t filter_low_id;
599         uint32_t filter_high_id;
600 };
601
602 /*****************************
603  Initialise idmap database. 
604 *****************************/
605
606 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom, const char *compat_params)
607 {
608         NTSTATUS ret;
609         struct idmap_tdb_context *ctx;
610         char *config_option = NULL;
611         const char *range;
612
613         ctx = talloc(dom, struct idmap_tdb_context);
614         if ( ! ctx) {
615                 DEBUG(0, ("Out of memory!\n"));
616                 return NT_STATUS_NO_MEMORY;
617         }
618
619         config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
620         if ( ! config_option) {
621                 DEBUG(0, ("Out of memory!\n"));
622                 ret = NT_STATUS_NO_MEMORY;
623                 goto failed;
624         }
625
626         ret = idmap_tdb_open_db(ctx, &ctx->tdb);
627         if ( ! NT_STATUS_IS_OK(ret)) {
628                 goto failed;
629         }
630
631         range = lp_parm_const_string(-1, config_option, "range", NULL);
632         if (( ! range) ||
633             (sscanf(range, "%u - %u", &ctx->filter_low_id, &ctx->filter_high_id) != 2) ||
634             (ctx->filter_low_id > ctx->filter_high_id)) {
635                 ctx->filter_low_id = 0;
636                 ctx->filter_high_id = 0;
637         }
638
639         dom->private_data = ctx;
640
641         talloc_free(config_option);
642         return NT_STATUS_OK;
643
644 failed:
645         talloc_free(ctx);
646         return ret;
647 }
648
649 /**********************************
650  Single id to sid lookup function. 
651 **********************************/
652
653 static NTSTATUS idmap_tdb_id_to_sid(struct idmap_tdb_context *ctx, struct id_map *map)
654 {
655         NTSTATUS ret;
656         TDB_DATA key, data;
657
658         if (!ctx || !map) {
659                 return NT_STATUS_INVALID_PARAMETER;
660         }
661
662         /* apply filters before checking */
663         if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
664             (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
665                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
666                                 map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
667                 return NT_STATUS_NONE_MAPPED;
668         }
669
670         switch (map->xid.type) {
671
672         case ID_TYPE_UID:
673                 key.dptr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
674                 break;
675                 
676         case ID_TYPE_GID:
677                 key.dptr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
678                 break;
679
680         default:
681                 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
682                 return NT_STATUS_INVALID_PARAMETER;
683         }
684
685         /* final SAFE_FREE safe */
686         data.dptr = NULL;
687
688         if (key.dptr == NULL) {
689                 DEBUG(0, ("Out of memory!\n"));
690                 ret = NT_STATUS_NO_MEMORY;
691                 goto done;
692         }
693
694         key.dsize = strlen(key.dptr) + 1;
695
696         DEBUG(10,("Fetching record %s\n", key.dptr));
697
698         /* Check if the mapping exists */
699         data = tdb_fetch(ctx->tdb, key);
700
701         if (!data.dptr) {
702                 DEBUG(10,("Record %s not found\n", key.dptr));
703                 ret = NT_STATUS_NONE_MAPPED;
704                 goto done;
705         }
706                 
707         if (!string_to_sid(map->sid, data.dptr)) {
708                 DEBUG(10,("INVALID SID (%s) in record %s\n",
709                                 data.dptr, key.dptr));
710                 ret = NT_STATUS_INTERNAL_DB_ERROR;
711                 goto done;
712         }
713
714         DEBUG(10,("Found record %s -> %s\n", key.dptr, data.dptr));
715         ret = NT_STATUS_OK;
716
717 done:
718         SAFE_FREE(data.dptr);
719         talloc_free(key.dptr);
720         return ret;
721 }
722
723 /**********************************
724  Single sid to id lookup function. 
725 **********************************/
726
727 static NTSTATUS idmap_tdb_sid_to_id(struct idmap_tdb_context *ctx, struct id_map *map)
728 {
729         NTSTATUS ret;
730         TDB_DATA key, data;
731         unsigned long rec_id = 0;
732
733         if ((key.dptr = talloc_asprintf(ctx, "%s", sid_string_static(map->sid))) == NULL) {
734                 DEBUG(0, ("Out of memory!\n"));
735                 ret = NT_STATUS_NO_MEMORY;
736                 goto done;
737         }
738
739         key.dsize = strlen(key.dptr) + 1;
740
741         DEBUG(10,("Fetching record %s\n", key.dptr));
742
743         /* Check if sid is present in database */
744         data = tdb_fetch(ctx->tdb, key);
745         if (!data.dptr) {
746                 DEBUG(10,("Record %s not found\n", key.dptr));
747                 ret = NT_STATUS_NONE_MAPPED;
748                 goto done;
749         }
750
751         /* What type of record is this ? */
752         if (sscanf(data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
753                 map->xid.id = rec_id;
754                 map->xid.type = ID_TYPE_UID;
755                 DEBUG(10,("Found uid record %s -> %s \n", key.dptr, data.dptr ));
756                 ret = NT_STATUS_OK;
757
758         } else if (sscanf(data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
759                 map->xid.id = rec_id;
760                 map->xid.type = ID_TYPE_GID;
761                 DEBUG(10,("Found gid record %s -> %s \n", key.dptr, data.dptr ));
762                 ret = NT_STATUS_OK;
763
764         } else { /* Unknown record type ! */
765                 DEBUG(2, ("Found INVALID record %s -> %s\n", key.dptr, data.dptr));
766                 ret = NT_STATUS_INTERNAL_DB_ERROR;
767         }
768         
769         SAFE_FREE(data.dptr);
770
771         /* apply filters before returning result */
772         if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
773             (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
774                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
775                                 map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
776                 ret = NT_STATUS_NONE_MAPPED;
777         }
778
779 done:
780         talloc_free(key.dptr);
781         return ret;
782 }
783
784 /**********************************
785  lookup a set of unix ids. 
786 **********************************/
787
788 static NTSTATUS idmap_tdb_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
789 {
790         struct idmap_tdb_context *ctx;
791         NTSTATUS ret;
792         int i;
793
794         ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
795
796         for (i = 0; ids[i]; i++) {
797                 ret = idmap_tdb_id_to_sid(ctx, ids[i]);
798                 if ( ! NT_STATUS_IS_OK(ret)) {
799
800                         /* if it is just a failed mapping continue */
801                         if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
802
803                                 /* make sure it is marked as unmapped */
804                                 ids[i]->mapped = False;
805                                 continue;
806                         }
807                         
808                         /* some fatal error occurred, return immediately */
809                         goto done;
810                 }
811
812                 /* all ok, id is mapped */
813                 ids[i]->mapped = True;
814         }
815
816         ret = NT_STATUS_OK;
817
818 done:
819         return ret;
820 }
821
822 /**********************************
823  lookup a set of sids. 
824 **********************************/
825
826 static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
827 {
828         struct idmap_tdb_context *ctx;
829         NTSTATUS ret;
830         int i;
831
832         ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
833
834         for (i = 0; ids[i]; i++) {
835                 ret = idmap_tdb_sid_to_id(ctx, ids[i]);
836                 if ( ! NT_STATUS_IS_OK(ret)) {
837
838                         /* if it is just a failed mapping continue */
839                         if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
840
841                                 /* make sure it is marked as unmapped */
842                                 ids[i]->mapped = False;
843                                 continue;
844                         }
845                         
846                         /* some fatal error occurred, return immediately */
847                         goto done;
848                 }
849
850                 /* all ok, id is mapped */
851                 ids[i]->mapped = True;
852         }
853
854         ret = NT_STATUS_OK;
855
856 done:
857         return ret;
858 }
859
860 /**********************************
861  set a mapping. 
862 **********************************/
863
864 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom, const struct id_map *map)
865 {
866         struct idmap_tdb_context *ctx;
867         NTSTATUS ret;
868         TDB_DATA ksid, kid, data;
869
870         if (!map || !map->sid) {
871                 return NT_STATUS_INVALID_PARAMETER;
872         }
873
874         ksid.dptr = kid.dptr = data.dptr = NULL;
875
876         /* TODO: should we filter a set_mapping using low/high filters ? */
877         
878         ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
879
880         switch (map->xid.type) {
881
882         case ID_TYPE_UID:
883                 kid.dptr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
884                 break;
885                 
886         case ID_TYPE_GID:
887                 kid.dptr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
888                 break;
889
890         default:
891                 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
892                 return NT_STATUS_INVALID_PARAMETER;
893         }
894
895         if (kid.dptr == NULL) {
896                 DEBUG(0, ("ERROR: Out of memory!\n"));
897                 ret = NT_STATUS_NO_MEMORY;
898                 goto done;
899         }
900         kid.dsize = strlen(kid.dptr) + 1;
901
902         if ((ksid.dptr = talloc_asprintf(ctx, "%s", sid_string_static(map->sid))) == NULL) {
903                 DEBUG(0, ("Out of memory!\n"));
904                 ret = NT_STATUS_NO_MEMORY;
905                 goto done;
906         }
907         ksid.dsize = strlen(ksid.dptr) + 1;
908
909         DEBUG(10, ("Storing %s <-> %s map\n", ksid.dptr, kid.dptr));
910
911         /* *DELETE* previous mappings if any.
912          * This is done both SID and [U|G]ID passed in */
913         
914         /* Lock the record for this SID. */
915         if (tdb_chainlock(ctx->tdb, ksid) != 0) {
916                 DEBUG(10,("Failed to lock record %s. Error %s\n",
917                                 ksid.dptr, tdb_errorstr(ctx->tdb) ));
918                 return NT_STATUS_UNSUCCESSFUL;
919         }
920
921         data = tdb_fetch(ctx->tdb, ksid);
922         if (data.dptr) {
923                 DEBUG(10, ("Deleting existing mapping %s <-> %s\n", data.dptr, ksid.dptr ));
924                 tdb_delete(ctx->tdb, data);
925                 tdb_delete(ctx->tdb, ksid);
926                 SAFE_FREE(data.dptr);
927         }
928
929         data = tdb_fetch(ctx->tdb, kid);
930         if (data.dptr) {
931                 DEBUG(10,("Deleting existing mapping %s <-> %s\n", data.dptr, kid.dptr ));
932                 tdb_delete(ctx->tdb, data);
933                 tdb_delete(ctx->tdb, kid);
934                 SAFE_FREE(data.dptr);
935         }
936
937         if (tdb_store(ctx->tdb, ksid, kid, TDB_INSERT) == -1) {
938                 DEBUG(0, ("Error storing SID -> ID: %s\n", tdb_errorstr(ctx->tdb)));
939                 tdb_chainunlock(ctx->tdb, ksid);
940                 ret = NT_STATUS_UNSUCCESSFUL;
941                 goto done;
942         }
943         if (tdb_store(ctx->tdb, kid, ksid, TDB_INSERT) == -1) {
944                 DEBUG(0, ("Error stroing ID -> SID: %s\n", tdb_errorstr(ctx->tdb)));
945                 /* try to remove the previous stored SID -> ID map */
946                 tdb_delete(ctx->tdb, ksid);
947                 tdb_chainunlock(ctx->tdb, ksid);
948                 ret = NT_STATUS_UNSUCCESSFUL;
949                 goto done;
950         }
951
952         tdb_chainunlock(ctx->tdb, ksid);
953         DEBUG(10,("Stored %s <-> %s\n", ksid.dptr, kid.dptr));
954         ret = NT_STATUS_OK;
955
956 done:
957         talloc_free(ksid.dptr);
958         talloc_free(kid.dptr);
959         SAFE_FREE(data.dptr);
960         return ret;
961 }
962
963 /**********************************
964  remove a mapping. 
965 **********************************/
966
967 static NTSTATUS idmap_tdb_remove_mapping(struct idmap_domain *dom, const struct id_map *map)
968 {
969         struct idmap_tdb_context *ctx;
970         NTSTATUS ret;
971         TDB_DATA ksid, kid, data;
972
973         if (!map || !map->sid) {
974                 return NT_STATUS_INVALID_PARAMETER;
975         }
976
977         ksid.dptr = kid.dptr = data.dptr = NULL;
978
979         /* TODO: should we filter a remove_mapping using low/high filters ? */
980         
981         ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
982
983         switch (map->xid.type) {
984
985         case ID_TYPE_UID:
986                 kid.dptr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
987                 break;
988                 
989         case ID_TYPE_GID:
990                 kid.dptr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
991                 break;
992
993         default:
994                 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
995                 return NT_STATUS_INVALID_PARAMETER;
996         }
997
998         if (kid.dptr == NULL) {
999                 DEBUG(0, ("ERROR: Out of memory!\n"));
1000                 ret = NT_STATUS_NO_MEMORY;
1001                 goto done;
1002         }
1003         kid.dsize = strlen(kid.dptr) + 1;
1004
1005         if ((ksid.dptr = talloc_asprintf(ctx, "%s", sid_string_static(map->sid))) == NULL) {
1006                 DEBUG(0, ("Out of memory!\n"));
1007                 ret = NT_STATUS_NO_MEMORY;
1008                 goto done;
1009         }
1010         ksid.dsize = strlen(ksid.dptr) + 1;
1011
1012         DEBUG(10, ("Checking %s <-> %s map\n", ksid.dptr, kid.dptr));
1013
1014         /* Lock the record for this SID. */
1015         if (tdb_chainlock(ctx->tdb, ksid) != 0) {
1016                 DEBUG(10,("Failed to lock record %s. Error %s\n",
1017                                 ksid.dptr, tdb_errorstr(ctx->tdb) ));
1018                 return NT_STATUS_UNSUCCESSFUL;
1019         }
1020
1021         /* Check if sid is present in database */
1022         data = tdb_fetch(ctx->tdb, ksid);
1023         if (!data.dptr) {
1024                 DEBUG(10,("Record %s not found\n", ksid.dptr));
1025                 tdb_chainunlock(ctx->tdb, ksid);
1026                 ret = NT_STATUS_NONE_MAPPED;
1027                 goto done;
1028         }
1029
1030         /* Check if sid is mapped to the specified ID */
1031         if ((data.dsize != kid.dsize) ||
1032             (memcmp(data.dptr, kid.dptr, data.dsize) != 0)) {
1033                 DEBUG(10,("Specified SID does not map to specified ID\n"));
1034                 DEBUGADD(10,("Actual mapping is %s -> %s\n", ksid.dptr, data.dptr));
1035                 tdb_chainunlock(ctx->tdb, ksid);
1036                 ret = NT_STATUS_NONE_MAPPED;
1037                 goto done;
1038         }
1039         
1040         DEBUG(10, ("Removing %s <-> %s map\n", ksid.dptr, kid.dptr));
1041
1042         /* Delete previous mappings. */
1043         
1044         data = tdb_fetch(ctx->tdb, ksid);
1045         if (data.dptr) {
1046                 DEBUG(10, ("Deleting existing mapping %s -> %s\n", ksid.dptr, kid.dptr ));
1047                 tdb_delete(ctx->tdb, ksid);
1048                 SAFE_FREE(data.dptr);
1049         }
1050
1051         data = tdb_fetch(ctx->tdb, kid);
1052         if (data.dptr) {
1053                 DEBUG(10,("Deleting existing mapping %s -> %s\n", kid.dptr, ksid.dptr ));
1054                 tdb_delete(ctx->tdb, kid);
1055                 SAFE_FREE(data.dptr);
1056         }
1057
1058         tdb_chainunlock(ctx->tdb, ksid);
1059         ret = NT_STATUS_OK;
1060
1061 done:
1062         talloc_free(ksid.dptr);
1063         talloc_free(kid.dptr);
1064         SAFE_FREE(data.dptr);
1065         return ret;
1066 }
1067
1068 /**********************************
1069  Close the idmap tdb instance
1070 **********************************/
1071
1072 static NTSTATUS idmap_tdb_close(struct idmap_domain *dom)
1073 {
1074         struct idmap_tdb_context *ctx;
1075
1076         if (dom->private_data) {
1077                 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
1078
1079                 if (idmap_tdb_tdb_close(ctx->tdb) == 0) {
1080                         return NT_STATUS_OK;
1081                 } else {
1082                         return NT_STATUS_UNSUCCESSFUL;
1083                 }
1084         }
1085         return NT_STATUS_OK;
1086 }
1087
1088 struct dump_data {
1089         TALLOC_CTX *memctx;
1090         struct id_map **maps;
1091         int *num_maps;
1092         NTSTATUS ret;
1093 };
1094
1095 static int idmap_tdb_dump_one_entry(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA value, void *pdata)
1096 {
1097         struct dump_data *data = talloc_get_type(pdata, struct dump_data);
1098         struct id_map *maps;
1099         int num_maps = *data->num_maps;
1100
1101         /* ignore any record but the ones with a SID as key */
1102         if (strncmp(key.dptr, "S-", 2) == 0) {
1103
1104                 maps = talloc_realloc(NULL, *data->maps, struct id_map, num_maps+1);
1105                 if ( ! maps) {
1106                         DEBUG(0, ("Out of memory!\n"));
1107                         data->ret = NT_STATUS_NO_MEMORY;
1108                         return -1;
1109                 }
1110                 *data->maps = maps;
1111                 maps[num_maps].sid = talloc(maps, DOM_SID);
1112                 if ( ! maps[num_maps].sid) {
1113                         DEBUG(0, ("Out of memory!\n"));
1114                         data->ret = NT_STATUS_NO_MEMORY;
1115                         return -1;
1116                 }
1117
1118                 if (!string_to_sid(maps[num_maps].sid, key.dptr)) {
1119                         DEBUG(10,("INVALID record %s\n", key.dptr));
1120                         /* continue even with errors */
1121                         return 0;
1122                 }
1123
1124                 /* Try a UID record. */
1125                 if (sscanf(value.dptr, "UID %u", &(maps[num_maps].xid.id)) == 1) {
1126                         maps[num_maps].xid.type = ID_TYPE_UID;
1127                         maps[num_maps].mapped = True;
1128                         *data->num_maps = num_maps + 1;
1129
1130                 /* Try a GID record. */
1131                 } else
1132                 if (sscanf(value.dptr, "GID %u", &(maps[num_maps].xid.id)) == 1) {
1133                         maps[num_maps].xid.type = ID_TYPE_GID;
1134                         maps[num_maps].mapped = True;
1135                         *data->num_maps = num_maps + 1;
1136
1137                 /* Unknown record type ! */
1138                 } else {
1139                         DEBUG(2, ("Found INVALID record %s -> %s\n", key.dptr, value.dptr));
1140                         /* do not increment num_maps */
1141                 }
1142         }
1143
1144         return 0;
1145 }
1146
1147 /**********************************
1148  Dump all mappings out
1149 **********************************/
1150
1151 static NTSTATUS idmap_tdb_dump_data(struct idmap_domain *dom, struct id_map **maps, int *num_maps)
1152 {
1153         struct idmap_tdb_context *ctx;
1154         struct dump_data *data;
1155         NTSTATUS ret = NT_STATUS_OK;
1156
1157         ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
1158
1159         data = talloc_zero(ctx, struct dump_data);
1160         if ( ! data) {
1161                 DEBUG(0, ("Out of memory!\n"));
1162                 return NT_STATUS_NO_MEMORY;
1163         }
1164         data->maps = maps;
1165         data->num_maps = num_maps;
1166         data->ret = NT_STATUS_OK;
1167
1168         tdb_traverse(ctx->tdb, idmap_tdb_dump_one_entry, data);
1169
1170         if ( ! NT_STATUS_IS_OK(data->ret)) {
1171                 ret = data->ret;
1172         }
1173
1174         talloc_free(data);
1175         return ret;
1176 }
1177
1178 static struct idmap_methods db_methods = {
1179
1180         .init = idmap_tdb_db_init,
1181         .unixids_to_sids = idmap_tdb_unixids_to_sids,
1182         .sids_to_unixids = idmap_tdb_sids_to_unixids,
1183         .set_mapping = idmap_tdb_set_mapping,
1184         .remove_mapping = idmap_tdb_remove_mapping,
1185         .dump_data = idmap_tdb_dump_data,
1186         .close_fn = idmap_tdb_close
1187 };
1188
1189 static struct idmap_alloc_methods db_alloc_methods = {
1190
1191         .init = idmap_tdb_alloc_init,
1192         .allocate_id = idmap_tdb_allocate_id,
1193         .get_id_hwm = idmap_tdb_get_hwm,
1194         .set_id_hwm = idmap_tdb_set_hwm,
1195         .close_fn = idmap_tdb_alloc_close
1196 };
1197
1198 NTSTATUS idmap_alloc_tdb_init(void)
1199 {
1200         return smb_register_idmap_alloc(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_alloc_methods);
1201 }
1202
1203 NTSTATUS idmap_tdb_init(void)
1204 {
1205         NTSTATUS ret;
1206
1207         /* FIXME: bad hack to actually register also the alloc_tdb module without changining configure.in */
1208         ret = idmap_alloc_tdb_init();
1209         if (! NT_STATUS_IS_OK(ret)) {
1210                 return ret;
1211         }
1212         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);
1213 }