s3:idmap_tdb: use idmap_rw_new_mapping in idmap_tdb_new_mapping
[amitay/samba.git] / source3 / winbindd / idmap_tdb.c
index 854b24c601788ee9d61e3ff8c7ffa9911042d0ad..a41f6e9fe37f050c900e5981d6ef44d35f49ae48 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 */
@@ -268,6 +271,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();
 
@@ -472,6 +480,9 @@ 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;
@@ -479,7 +490,7 @@ static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom, const char *params)
 
        DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom->name));
 
-       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;
@@ -492,6 +503,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);
@@ -621,43 +642,12 @@ done:
 static NTSTATUS idmap_tdb_new_mapping(struct idmap_domain *dom, struct id_map *map)
 {
        NTSTATUS ret;
+       struct idmap_tdb_context *ctx;
 
-       if (map == NULL) {
-               ret = NT_STATUS_INVALID_PARAMETER;
-               goto done;
-       }
-
-       if ((map->xid.type != ID_TYPE_UID) && (map->xid.type != ID_TYPE_GID)) {
-               ret = NT_STATUS_INVALID_PARAMETER;
-               goto done;
-       }
-
-       if (map->sid == NULL) {
-               ret = NT_STATUS_INVALID_PARAMETER;
-               goto done;
-       }
-
-       ret = idmap_tdb_get_new_id(dom, &map->xid);
-       if (!NT_STATUS_IS_OK(ret)) {
-               DEBUG(3, ("Could not allocate id: %s\n", nt_errstr(ret)));
-               goto done;
-       }
-
-       DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
-                  sid_string_dbg(map->sid),
-                  (map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
-                  (unsigned long)map->xid.id));
-
-       map->status = ID_MAPPED;
+       ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
 
-       /* store the mapping */
-       ret = idmap_tdb_set_mapping(dom, map);
-       if (!NT_STATUS_IS_OK(ret)) {
-               DEBUG(3, ("Could not store the new mapping: %s\n",
-                         nt_errstr(ret)));
-       }
+       ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
 
-done:
        return ret;
 }
 
@@ -847,11 +837,73 @@ done:
  lookup a set of sids. 
 **********************************/
 
+struct idmap_tdb_sids_to_unixids_context {
+       struct idmap_domain *dom;
+       struct id_map **ids;
+       bool allocate_unmapped;
+};
+
+static NTSTATUS idmap_tdb_sids_to_unixids_action(struct db_context *db,
+                                                void *private_data)
+{
+       struct idmap_tdb_sids_to_unixids_context *state;
+       int i;
+       NTSTATUS ret;
+
+       state = (struct idmap_tdb_sids_to_unixids_context *)private_data;
+
+       DEBUG(10, ("idmap_tdb_sids_to_unixids_action: "
+                  " domain: [%s], allocate: %s\n",
+                  state->dom->name,
+                  state->allocate_unmapped ? "yes" : "no"));
+
+       for (i = 0; state->ids[i]; i++) {
+               if ((state->ids[i]->status == ID_UNKNOWN) ||
+                   /* retry if we could not map in previous run: */
+                   (state->ids[i]->status == ID_UNMAPPED))
+               {
+                       NTSTATUS ret2;
+
+                       ret2 = idmap_tdb_sid_to_id(state->dom, state->ids[i]);
+                       if (!NT_STATUS_IS_OK(ret2)) {
+
+                               /* if it is just a failed mapping, continue */
+                               if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
+
+                                       /* make sure it is marked as unmapped */
+                                       state->ids[i]->status = ID_UNMAPPED;
+                                       ret = STATUS_SOME_UNMAPPED;
+                               } else {
+                                       /* some fatal error occurred, return immediately */
+                                       ret = ret2;
+                                       goto done;
+                               }
+                       } else {
+                               /* all ok, id is mapped */
+                               state->ids[i]->status = ID_MAPPED;
+                       }
+               }
+
+               if ((state->ids[i]->status == ID_UNMAPPED) &&
+                   state->allocate_unmapped)
+               {
+                       ret = idmap_tdb_new_mapping(state->dom, state->ids[i]);
+                       if (!NT_STATUS_IS_OK(ret)) {
+                               goto done;
+                       }
+               }
+       }
+
+done:
+       return ret;
+}
+
 static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
 {
        struct idmap_tdb_context *ctx;
        NTSTATUS ret;
        int i;
+       struct idmap_tdb_sids_to_unixids_context state;
 
        /* initialize the status to avoid suprise */
        for (i = 0; ids[i]; i++) {
@@ -860,32 +912,23 @@ static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_ma
        
        ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
 
-       for (i = 0; ids[i]; i++) {
-               ret = idmap_tdb_sid_to_id(dom, ids[i]);
-               if ( ! NT_STATUS_IS_OK(ret)) {
+       state.dom = dom;
+       state.ids = ids;
+       state.allocate_unmapped = false;
 
-                       /* if it is just a failed mapping continue */
-                       if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
-
-                               /* make sure it is marked as unmapped */
-                               ids[i]->status = ID_UNMAPPED;
-                               continue;
-                       }
-                       
-                       /* some fatal error occurred, return immediately */
-                       goto done;
-               }
+       ret = idmap_tdb_sids_to_unixids_action(ctx->db, &state);
 
-               /* all ok, id is mapped */
-               ids[i]->status = ID_MAPPED;
+       if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
+               state.allocate_unmapped = true;
+               ret = dbwrap_trans_do(ctx->db,
+                                     idmap_tdb_sids_to_unixids_action,
+                                     &state);
        }
 
-       ret = NT_STATUS_OK;
-
-done:
        return ret;
 }
 
+
 /**********************************
  Close the idmap tdb instance
 **********************************/