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