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