r2792: got rid of talloc_ldb_alloc() and instead created talloc_realloc_fn(),
[bbaumbach/samba-autobuild/.git] / source4 / rpc_server / samr / samdb.c
index d23118f1f07ab6cb00096da6accd362cc73a08de..330741e29d418c1ef34e02747393bf8e494d49fe 100644 (file)
@@ -24,6 +24,7 @@
 
 struct samdb_context {
        struct ldb_context *ldb;
+       struct samdb_context **static_ptr;
 };
 
 
@@ -42,12 +43,12 @@ void samdb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_
        free(s);
 }
 
-/* close a connection to the sam */
-int samdb_destructor(void *ctx)
+/* destroy the last connection to the sam */
+static int samdb_destructor(void *ctx)
 {
        struct samdb_context *sam_ctx = ctx;
-       /* we don't actually close due to broken posix locking semantics */
-       sam_ctx->ldb = NULL;
+       ldb_close(sam_ctx->ldb);
+       *(sam_ctx->static_ptr) = NULL;
        return 0;
 }                               
 
@@ -57,7 +58,7 @@ int samdb_destructor(void *ctx)
  */
 void *samdb_connect(TALLOC_CTX *mem_ctx)
 {
-       struct samdb_context *ctx;
+       static struct samdb_context *ctx;
        /*
          the way that unix fcntl locking works forces us to have a
          static ldb handle here rather than a much more sensible
@@ -66,25 +67,26 @@ void *samdb_connect(TALLOC_CTX *mem_ctx)
          the ldb more than once, and tdb would rightly refuse the
          second open due to the broken nature of unix locking.
        */
-       static struct ldb_context *static_sam_db;
+       if (ctx != NULL) {
+               return talloc_reference(mem_ctx, ctx);
+       }
 
-       if (static_sam_db == NULL) {
-               static_sam_db = ldb_connect(lp_sam_url(), 0, NULL);
-               if (static_sam_db == NULL) {
-                       return NULL;
-               }
+       ctx = talloc_p(mem_ctx, struct samdb_context);
+       if (ctx == NULL) {
+               errno = ENOMEM;
+               return NULL;
        }
 
-       ldb_set_debug(static_sam_db, samdb_debug, NULL);
+       ctx->static_ptr = &ctx;
 
-       ctx = talloc_p(NULL, struct samdb_context);
-       if (!ctx) {
-               errno = ENOMEM;
+       ctx->ldb = ldb_connect(lp_sam_url(), 0, NULL);
+       if (ctx->ldb == NULL) {
+               talloc_free(ctx);
                return NULL;
        }
 
-       ctx->ldb = static_sam_db;
        talloc_set_destructor(ctx, samdb_destructor);
+       ldb_set_debug(ctx->ldb, samdb_debug, NULL);
 
        return ctx;
 }
@@ -117,7 +119,7 @@ int samdb_search_free(void *ctx,
                      TALLOC_CTX *mem_ctx, struct ldb_message **res)
 {
        struct samdb_context *sam_ctx = ctx;
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_search_free(sam_ctx->ldb, res);
 }
 
@@ -462,7 +464,7 @@ struct samr_Password samdb_result_hash(struct ldb_message *msg, const char *attr
        const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
        ZERO_STRUCT(hash);
        if (val) {
-               memcpy(hash.hash, val->data, MIN(val->length, 16));
+               memcpy(hash.hash, val->data, MIN(val->length, sizeof(hash.hash)));
        }
        return hash;
 }
@@ -602,7 +604,7 @@ int samdb_copy_template(void *ctx, TALLOC_CTX *mem_ctx,
        
 
        /* pull the template record */
-       ret = samdb_search(ctx, mem_ctx, NULL, &res, NULL, expression);
+       ret = samdb_search(ctx, mem_ctx, NULL, &res, NULL, "%s", expression);
        if (ret != 1) {
                DEBUG(1,("samdb: ERROR: template '%s' matched %d records\n", 
                         expression, ret));
@@ -744,7 +746,7 @@ int samdb_msg_add_string(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg
        if (s == NULL || a == NULL) {
                return -1;
        }
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_msg_add_string(sam_ctx->ldb, msg, a, s);
 }
 
@@ -759,7 +761,7 @@ int samdb_msg_add_delete(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg
        if (a == NULL) {
                return -1;
        }
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        /* we use an empty replace rather than a delete, as it allows for 
           samdb_replace() to be used everywhere */
        return ldb_msg_add_empty(sam_ctx->ldb, msg, a, LDB_FLAG_MOD_REPLACE);
@@ -809,7 +811,7 @@ int samdb_msg_add_hash(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
                return -1;
        }
        memcpy(val.data, hash.hash, 16);
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_msg_add_value(sam_ctx->ldb, msg, attr_name, &val);
 }
 
@@ -830,7 +832,7 @@ int samdb_msg_add_hashes(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg
        for (i=0;i<count;i++) {
                memcpy(i*16 + (char *)val.data, hashes[i].hash, 16);
        }
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_msg_add_value(sam_ctx->ldb, msg, attr_name, &val);
 }
 
@@ -853,7 +855,7 @@ int samdb_msg_add_logon_hours(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message
        struct ldb_val val;
        val.length = hours.units_per_week / 8;
        val.data = hours.bitmap;
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_msg_add_value(sam_ctx->ldb, msg, attr_name, &val);
 }
 
@@ -866,7 +868,7 @@ int samdb_msg_set_string(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg
        struct samdb_context *sam_ctx = ctx;
        struct ldb_message_element *el;
 
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
 
        el = ldb_msg_find_element(msg, attr_name);
        if (el) {
@@ -895,7 +897,7 @@ int samdb_add(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg)
 {
        struct samdb_context *sam_ctx = ctx;
 
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_add(sam_ctx->ldb, msg);
 }
 
@@ -906,7 +908,7 @@ int samdb_delete(void *ctx, TALLOC_CTX *mem_ctx, const char *dn)
 {
        struct samdb_context *sam_ctx = ctx;
 
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_delete(sam_ctx->ldb, dn);
 }
 
@@ -917,7 +919,7 @@ int samdb_modify(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg)
 {
        struct samdb_context *sam_ctx = ctx;
 
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_modify(sam_ctx->ldb, msg);
 }