NTSTATUS winsdb_lookup(struct winsdb_handle *h, 
		       const struct nbt_name *name,
		       TALLOC_CTX *mem_ctx,
		       struct winsdb_record **_rec)
{
	NTSTATUS status;
	struct ldb_result *res = NULL;
	int ret;
	struct winsdb_record *rec;
	struct ldb_context *wins_db = h->ldb;
	TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
	time_t now = time(NULL);

	/* find the record in the WINS database */
	ret = ldb_search(wins_db, tmp_ctx, &res,
			 winsdb_dn(tmp_ctx, wins_db, name),
			 LDB_SCOPE_BASE, NULL, NULL);

	if (ret != LDB_SUCCESS || res->count > 1) {
		status = NT_STATUS_INTERNAL_DB_CORRUPTION;
		goto failed;
	} else if (res->count== 0) {
		status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
		goto failed;
	}

	status = winsdb_record(h, res->msgs[0], tmp_ctx, now, &rec);
	if (!NT_STATUS_IS_OK(status)) goto failed;

	talloc_steal(mem_ctx, rec);
	talloc_free(tmp_ctx);
	*_rec = rec;
	return NT_STATUS_OK;

failed:
	talloc_free(tmp_ctx);
	return status;
}
NTSTATUS winsdb_lookup(struct winsdb_handle *h, 
		       const struct nbt_name *name,
		       TALLOC_CTX *mem_ctx,
		       struct winsdb_record **_rec)
{
	NTSTATUS status;
	struct ldb_result *res = NULL;
	int ret;
	struct winsdb_record *rec;
	struct ldb_context *wins_db = h->ldb;
	TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
	time_t now = time(NULL);

	 
	ret = ldb_search(wins_db, tmp_ctx, &res,
			 winsdb_dn(tmp_ctx, wins_db, name),
			 LDB_SCOPE_BASE, NULL, NULL);

	if (ret != LDB_SUCCESS || res->count > 1) {
		status = NT_STATUS_INTERNAL_DB_CORRUPTION;
		goto failed;
	} else if (res->count== 0) {
		status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
		goto failed;
	}

	status = winsdb_record(h, res->msgs[0], tmp_ctx, now, &rec);
	if (!NT_STATUS_IS_OK(status)) goto failed;

	talloc_steal(mem_ctx, rec);
	talloc_free(tmp_ctx);
	*_rec = rec;
	return NT_STATUS_OK;

failed:
	talloc_free(tmp_ctx);
	return status;
}