static bool winsdb_check_or_add_module_list(struct tevent_context *ev_ctx, 
					    struct loadparm_context *lp_ctx, struct winsdb_handle *h,
					    const char *wins_path)
{
	int trans;
	int ret;
	struct ldb_dn *dn;
	struct ldb_result *res = NULL;
	struct ldb_message *msg = NULL;
	TALLOC_CTX *tmp_ctx = talloc_new(h);
	unsigned int flags = 0;

	trans = ldb_transaction_start(h->ldb);
	if (trans != LDB_SUCCESS) goto failed;

	/* check if we have a special @MODULES record already */
	dn = ldb_dn_new(tmp_ctx, h->ldb, "@MODULES");
	if (!dn) goto failed;

	/* find the record in the WINS database */
	ret = ldb_search(h->ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
	if (ret != LDB_SUCCESS) goto failed;

	if (res->count > 0) goto skip;

	/* if there's no record, add one */
	msg = ldb_msg_new(tmp_ctx);
	if (!msg) goto failed;
	msg->dn = dn;

	ret = ldb_msg_add_string(msg, "@LIST", "wins_ldb");
	if (ret != LDB_SUCCESS) goto failed;

	ret = ldb_add(h->ldb, msg);
	if (ret != LDB_SUCCESS) goto failed;

	trans = ldb_transaction_commit(h->ldb);
	if (trans != LDB_SUCCESS) goto failed;

	/* close and reopen the database, with the modules */
	trans = LDB_ERR_OTHER;
	talloc_free(h->ldb);
	h->ldb = NULL;

	if (lpcfg_parm_bool(lp_ctx, NULL,"winsdb", "nosync", false)) {
		flags |= LDB_FLG_NOSYNC;
	}

	h->ldb = ldb_wrap_connect(h, ev_ctx, lp_ctx, wins_path,
				  NULL, NULL, flags);
	if (!h->ldb) goto failed;

	talloc_free(tmp_ctx);
	return true;

skip:
	if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
	talloc_free(tmp_ctx);
	return true;

failed:
	if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
	talloc_free(tmp_ctx);
	return false;
}
static bool winsdb_check_or_add_module_list(struct tevent_context *ev_ctx, 
					    struct loadparm_context *lp_ctx, struct winsdb_handle *h,
					    const char *wins_path)
{
	int trans;
	int ret;
	struct ldb_dn *dn;
	struct ldb_result *res = NULL;
	struct ldb_message *msg = NULL;
	TALLOC_CTX *tmp_ctx = talloc_new(h);
	unsigned int flags = 0;

	trans = ldb_transaction_start(h->ldb);
	if (trans != LDB_SUCCESS) goto failed;

	 
	dn = ldb_dn_new(tmp_ctx, h->ldb, "@MODULES");
	if (!dn) goto failed;

	 
	ret = ldb_search(h->ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
	if (ret != LDB_SUCCESS) goto failed;

	if (res->count > 0) goto skip;

	 
	msg = ldb_msg_new(tmp_ctx);
	if (!msg) goto failed;
	msg->dn = dn;

	ret = ldb_msg_add_string(msg, "@LIST", "wins_ldb");
	if (ret != LDB_SUCCESS) goto failed;

	ret = ldb_add(h->ldb, msg);
	if (ret != LDB_SUCCESS) goto failed;

	trans = ldb_transaction_commit(h->ldb);
	if (trans != LDB_SUCCESS) goto failed;

	 
	trans = LDB_ERR_OTHER;
	talloc_free(h->ldb);
	h->ldb = NULL;

	if (lpcfg_parm_bool(lp_ctx, NULL,"winsdb", "nosync", false)) {
		flags |= LDB_FLG_NOSYNC;
	}

	h->ldb = ldb_wrap_connect(h, ev_ctx, lp_ctx, wins_path,
				  NULL, NULL, flags);
	if (!h->ldb) goto failed;

	talloc_free(tmp_ctx);
	return true;

skip:
	if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
	talloc_free(tmp_ctx);
	return true;

failed:
	if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
	talloc_free(tmp_ctx);
	return false;
}