struct samdb_context {
struct ldb_context *ldb;
+ struct samdb_context **static_ptr;
};
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;
}
*/
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
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;
}
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);
}
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;
}
/* 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));
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);
}
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);
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);
}
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);
}
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);
}
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) {
{
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);
}
{
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);
}
{
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);
}