Index: mapiproxy/servers/default/nspi/dcesrv_exchange_nsp.c
===================================================================
--- mapiproxy/servers/default/nspi/dcesrv_exchange_nsp.c	(revision 1356)
+++ mapiproxy/servers/default/nspi/dcesrv_exchange_nsp.c	(working copy)
@@ -429,10 +429,10 @@
 			r->out.ppMIds[0]->aulPropTag[i] = 0;
 		} else {
 			dn = ldb_msg_find_attr_as_string(msg, "distinguishedName", NULL);
-			retval = emsabp_tdb_fetch_MId(emsabp_ctx->tdb_ctx, dn, &MId);
+			retval = emsabp_tdb_fetch_MId(emsabp_ctx->ttdb_ctx, dn, &MId);
 			if (retval) {
-				retval = emsabp_tdb_insert(emsabp_ctx->tdb_ctx, dn);
-				retval = emsabp_tdb_fetch_MId(emsabp_ctx->tdb_ctx, dn, &MId);
+				retval = emsabp_tdb_insert(emsabp_ctx->ttdb_ctx, dn);
+				retval = emsabp_tdb_fetch_MId(emsabp_ctx->ttdb_ctx, dn, &MId);
 			}
 			r->out.ppMIds[0]->aulPropTag[i] = MId;
 		}
@@ -479,7 +479,7 @@
 	struct dcesrv_handle	*h;
 	struct emsabp_context	*emsabp_ctx;
 	uint32_t		MId;
-	char			*dn;
+	int 			i;
 
 	DEBUG(3, ("exchange_nsp: NspiGetProps (0x9)\n"));
 
@@ -498,35 +498,56 @@
 	/* Step 1. Sanity Checks (MS-NSPI Server Processing Rules) */
 	if (r->in.pStat->ContainerID && (emsabp_tdb_lookup_MId(emsabp_ctx->tdb_ctx, r->in.pStat->ContainerID) == false)) {
 		retval = MAPI_E_INVALID_BOOKMARK;
-		goto failure;
+		r->out.result = retval;
+		return retval;
 	}
 
-	retval = emsabp_tdb_fetch_dn_from_MId(mem_ctx, emsabp_ctx->tdb_ctx, MId, &dn);
-	if (retval != MAPI_E_SUCCESS) {
-	failure:
-		r->out.ppRows = talloc_array(mem_ctx, struct SRow *, 2);
-		r->out.ppRows[0] = NULL;
-		r->out.result = MAPI_E_INVALID_BOOKMARK;
-		return r->out.result;
-	}
 
 	/* Step 2. Fetch properties */
 	r->out.ppRows = talloc_array(mem_ctx, struct SRow *, 2);
 	r->out.ppRows[0] = talloc_zero(r->out.ppRows, struct SRow);
 	r->out.ppRows[0]->ulAdrEntryPad = 0;
-	r->out.ppRows[0]->cValues = r->in.pPropTags->cValues;
-	r->out.ppRows[0]->lpProps = talloc_array(r->out.ppRows[0], struct SPropValue, r->in.pPropTags->cValues);
 
 	retval = emsabp_fetch_attrs(mem_ctx, emsabp_ctx, r->out.ppRows[0], MId, r->in.pPropTags);
 	if (retval != MAPI_E_SUCCESS) {
-		talloc_free(r->out.ppRows);
-		r->out.result = MAPI_W_ERRORS_RETURNED;
-		goto failure;
+		// if Mid is not found, we should proceed as though no attributes are found
+		if (retval == MAPI_E_INVALID_BOOKMARK) {
+			// set all requested prop values to error
+			uint32_t 	ulPropTag;
+			struct SRow *aRow;
+			aRow = r->out.ppRows[0];
+			aRow->ulAdrEntryPad = 0x0;
+			aRow->cValues = r->in.pPropTags->cValues;
+			aRow->lpProps = talloc_array(mem_ctx, struct SPropValue, aRow->cValues);
+			for (i = 0; i < aRow->cValues; i++) {
+				ulPropTag = r->in.pPropTags->aulPropTag[i];
+				ulPropTag = (ulPropTag & 0xFFFF0000) | PT_ERROR;
+
+				aRow->lpProps[i].ulPropTag = ulPropTag;
+				aRow->lpProps[i].dwAlignPad = 0x0;
+				set_SPropValue(&(aRow->lpProps[i]), NULL);
+			}
+			retval = MAPI_W_ERRORS_RETURNED;
+		} else {
+			talloc_free(r->out.ppRows);
+			r->out.ppRows = NULL;
+		}
+		r->out.result = retval;
+		return r->out.result;
 	}
 
-	r->out.result = MAPI_E_SUCCESS;
+	// Step 3. Properties are fetched. Make proper return value.
+	//         We should return ErrorsReturned if at least one property is not found.
+	for (i = 0; i < r->out.ppRows[0]->cValues; i++) {
+		if ((r->out.ppRows[0]->lpProps[i].ulPropTag & 0xFFFF) == PT_ERROR) {
+			retval = MAPI_W_ERRORS_RETURNED;
+			break;
+		}
+	}
 
-	return MAPI_E_SUCCESS;
+	r->out.result = retval;
+
+	return retval;
 }
 
 
Index: mapiproxy/servers/default/nspi/emsabp.c
===================================================================
--- mapiproxy/servers/default/nspi/emsabp.c	(revision 1356)
+++ mapiproxy/servers/default/nspi/emsabp.c	(working copy)
@@ -28,6 +28,7 @@
 
 #include "mapiproxy/dcesrv_mapiproxy.h"
 #include "dcesrv_exchange_nsp.h"
+#include <ldb_wrap.h>
 
 /**
    \details Initialize the EMSABP context and open connections to
@@ -44,9 +45,6 @@
 	TALLOC_CTX		*mem_ctx;
 	struct emsabp_context	*emsabp_ctx;
 	struct tevent_context	*ev;
-	char			*configuration = NULL;
-	char			*users = NULL;
-	int			ret;
 
 	/* Sanity checks */
 	if (!lp_ctx) return NULL;
@@ -71,34 +69,20 @@
 	emsabp_ctx->lp_ctx = lp_ctx;
 
 	/* Return an opaque context pointer on the configuration database */
-	configuration = private_path(mem_ctx, lp_ctx, "configuration.ldb");
-	emsabp_ctx->conf_ctx = ldb_init(mem_ctx, ev);
+	emsabp_ctx->conf_ctx = ldb_wrap_connect(mem_ctx, ev, lp_ctx, "configuration.ldb", 
+						NULL, NULL, 
+						LDB_FLG_RDONLY, NULL);
 	if (!emsabp_ctx->conf_ctx) {
-		talloc_free(configuration);
-		talloc_free(mem_ctx);
-		return NULL;
-	}
-
-	ret = ldb_connect(emsabp_ctx->conf_ctx, configuration, LDB_FLG_RDONLY, NULL);
-	talloc_free(configuration);
-	if (ret != LDB_SUCCESS) {
 		DEBUG(0, ("[%s:%d]: Connection to \"configuration.ldb\" failed\n", __FUNCTION__, __LINE__));
 		talloc_free(mem_ctx);
 		return NULL;
 	}
 
 	/* Return an opaque context pointer to the users database */
-	users = private_path(mem_ctx, lp_ctx, "users.ldb");
-	emsabp_ctx->users_ctx = ldb_init(mem_ctx, ev);
+	emsabp_ctx->users_ctx = ldb_wrap_connect(mem_ctx, ev, lp_ctx, "users.ldb", 
+						 NULL, NULL, 
+						 LDB_FLG_RDONLY, NULL);
 	if (!emsabp_ctx->users_ctx) {
-		talloc_free(users);
-		talloc_free(mem_ctx);
-		return NULL;
-	}
-
-	ret = ldb_connect(emsabp_ctx->users_ctx, users, LDB_FLG_RDONLY, NULL);
-	talloc_free(users);
-	if (ret != LDB_SUCCESS) {
 		DEBUG(0, ("[%s:%d]: Connection to \"users.ldb\" failed\n", __FUNCTION__, __LINE__));
 		talloc_free(mem_ctx);
 		return NULL;
@@ -213,54 +197,50 @@
 	struct loadparm_context	*lp_ctx;
 	struct GUID		*guid = (struct GUID *) NULL;
 	const char		*netbiosname = NULL;
-	const char		*guid_str = NULL;
-	enum ldb_scope		scope = LDB_SCOPE_SUBTREE;
+	const struct ldb_val	*ldb_value = NULL;
 	struct ldb_result	*res = NULL;
-	char			*dn = NULL;
 	struct ldb_dn		*ldb_dn = NULL;
 	const char * const	recipient_attrs[] = { "*", NULL };
-	const char		*firstorgdn = NULL;
+	const char		*server_entry = NULL;
 
 	lp_ctx = emsabp_ctx->lp_ctx;
 
 	netbiosname = lp_netbios_name(lp_ctx);
 	if (!netbiosname) return NULL;
 
-	/* Step 1. Find the Exchange Organization */
+	/* Step 1. Find server record in CN=Servers,CN=Default-First-Site-Name,CN=Sites,<CONFIG_DN> sub-tree */
 	ret = ldb_search(emsabp_ctx->conf_ctx, emsabp_ctx->mem_ctx, &res,
 			 ldb_get_default_basedn(emsabp_ctx->conf_ctx),
-			 scope, recipient_attrs, "(objectClass=msExchOrganizationContainer)");
+			 LDB_SCOPE_SUBTREE, recipient_attrs, 
+			 "(&(objectClass=server)(cn=%s))", netbiosname);
 
 	if (ret != LDB_SUCCESS || !res->count) {
 		return NULL;
 	}
 
-	firstorgdn = ldb_msg_find_attr_as_string(res->msgs[0], "distinguishedName", NULL);
-	if (!firstorgdn) {
+	server_entry = ldb_msg_find_attr_as_string(res->msgs[0], "distinguishedName", NULL);
+	if (!server_entry) {
 		return NULL;
 	}
 
-	/* Step 2. Find the OpenChange Server object */
-	dn = talloc_asprintf(emsabp_ctx->mem_ctx, "CN=Servers,CN=First Administrative Group,CN=Administrative Groups,%s",
-			     firstorgdn);
-	ldb_dn = ldb_dn_new(emsabp_ctx->mem_ctx, emsabp_ctx->conf_ctx, dn);
-	talloc_free(dn);
+	/* Step 2. Fetch "NTDS Settings" info for the server */
+	ldb_dn = ldb_dn_new(emsabp_ctx->mem_ctx, emsabp_ctx->conf_ctx, server_entry);
 	if (!ldb_dn_validate(ldb_dn)) {
 		return NULL;
 	}
 
 	ret = ldb_search(emsabp_ctx->conf_ctx, emsabp_ctx->mem_ctx, &res, ldb_dn, 
-			 scope, recipient_attrs, "(cn=%s)", netbiosname);
+			 LDB_SCOPE_ONELEVEL, recipient_attrs, "(cn=NTDS Settings)");
 	if (ret != LDB_SUCCESS || !res->count) {
 		return NULL;
 	}
 
 	/* Step 3. Retrieve the objectGUID GUID */
-	guid_str = ldb_msg_find_attr_as_string(res->msgs[0], "objectGUID", NULL);
-	if (!guid_str) return NULL;
+	ldb_value = ldb_msg_find_ldb_val(res->msgs[0], "objectGUID");
+	if (!ldb_value)	return NULL;
 
 	guid = talloc_zero(emsabp_ctx->mem_ctx, struct GUID);
-	GUID_from_string(guid_str, guid);
+	GUID_from_data_blob(ldb_value, guid);
 	
 	return guid;
 }
@@ -610,7 +590,7 @@
 		retval = emsabp_tdb_fetch_dn_from_MId(mem_ctx, emsabp_ctx->tdb_ctx, MId, &dn);
 		ldb_ctx = emsabp_ctx->conf_ctx;
 	}
-	OPENCHANGE_RETVAL_IF(retval, MAPI_E_CORRUPT_STORE, NULL);
+	OPENCHANGE_RETVAL_IF(retval, MAPI_E_INVALID_BOOKMARK, NULL);
 
 	/* Step 1. Fetch the LDB record */
 	ldb_dn = ldb_dn_new(mem_ctx, ldb_ctx, dn);
@@ -1099,8 +1079,8 @@
 	OPENCHANGE_RETVAL_IF(!legacyDN, MAPI_E_INVALID_PARAMETER, NULL);
 	OPENCHANGE_RETVAL_IF(!ldb_res, MAPI_E_INVALID_PARAMETER, NULL);
 
-	ret = ldb_search(emsabp_ctx->conf_ctx, emsabp_ctx->mem_ctx, &res,
-			 ldb_get_default_basedn(emsabp_ctx->conf_ctx), 
+	ret = ldb_search(emsabp_ctx->users_ctx, emsabp_ctx->mem_ctx, &res,
+			 ldb_get_default_basedn(emsabp_ctx->users_ctx),
 			 LDB_SCOPE_SUBTREE, recipient_attrs, "(legacyExchangeDN=%s)",
 			 legacyDN);
 
