s3:idmap_tdb: add idmap_rw_ops to idmap_tdb_context and initialize them in init_db
[metze/samba/wip.git] / source3 / winbindd / idmap_tdb.c
index 831db9d05c390bd1d47673a9d50f4db627a1c5e9..2c9d1cc0a1e97c4053b394459be045bbcf6debad 100644 (file)
@@ -7,6 +7,7 @@
    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
    Copyright (C) Jeremy Allison 2006
    Copyright (C) Simo Sorce 2003-2006
+   Copyright (C) Michael Adam 2009-2010
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -24,6 +25,7 @@
 
 #include "includes.h"
 #include "winbindd.h"
+#include "idmap_rw.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_IDMAP
@@ -35,6 +37,7 @@
 
 struct idmap_tdb_context {
        struct db_context *db;
+       struct idmap_rw_ops *rw_ops;
 };
 
 /* High water mark keys */
@@ -269,6 +272,11 @@ static NTSTATUS idmap_tdb_open_db(struct idmap_domain *dom)
 
        ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
 
+       if (ctx->db) {
+               /* it is already open */
+               return NT_STATUS_OK;
+       }
+
        /* use our own context here */
        mem_ctx = talloc_stackframe();
 
@@ -473,12 +481,15 @@ static NTSTATUS idmap_tdb_get_new_id(struct idmap_domain *dom,
  Initialise idmap database. 
 *****************************/
 
+static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
+                                     const struct id_map *map);
+
 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom, const char *params)
 {
        NTSTATUS ret;
        struct idmap_tdb_context *ctx;
 
-       ctx = talloc(dom, struct idmap_tdb_context);
+       ctx = talloc_zero(dom, struct idmap_tdb_context);
        if ( ! ctx) {
                DEBUG(0, ("Out of memory!\n"));
                return NT_STATUS_NO_MEMORY;
@@ -491,6 +502,16 @@ static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom, const char *params)
        }
 #endif
 
+       ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
+       if (ctx->rw_ops == NULL) {
+               DEBUG(0, ("Out of memory!\n"));
+               ret = NT_STATUS_NO_MEMORY;
+               goto failed;
+       }
+
+       ctx->rw_ops->get_new_id = idmap_tdb_get_new_id;
+       ctx->rw_ops->set_mapping = idmap_tdb_set_mapping;
+
        dom->private_data = ctx;
 
        ret = idmap_tdb_open_db(dom);