Creating the Content Filter Object
Topic Last Modified: 2006-11-30
Once the UCE Content Filter object has been created, you can set the ms-Exch-Uce-Store-Action-Threshold Attribute, ms-Exch-Uce-Block-Threshold Attribute, and ms-Exch-Uce-Enabled Attribute on it. The following example code shows how to search for the UCE Content Filter object and, if it exists, sets these attributes on it. If the UCE Content Filter object does not already exist, the code creates the object and sets the attributes on it.
// This program must be linked with the wldap32.lib library
//
// usage:
// SetSVMProps <Enabled> <BlockThreshold> <StoreActionThreshold>
//
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <winldap.h>
#define MAX_STRING_LEN 512
void Error(char *str);
int _cdecl main(int argc, char **argv)
{
PLDAPMessage plmsgSearchResponse = NULL; // Server allocated response to search request
PLDAPMessage plmsgEntry = NULL; // Server allocated response to entry request
PCHAR* ppszConfigDN = NULL; // Config DN (string allocated by LDAP library)
PLDAP pLdapSession = NULL; // LDAP session data
ULONG lRtn = LDAP_SUCCESS;
CHAR szSearch[MAX_STRING_LEN] = "";
CHAR szFilter[MAX_STRING_LEN] = "";
CHAR szUceDN[MAX_STRING_LEN] = "";
PCHAR pszMessageDeliveryDN = NULL;
PCHAR pszUceDN = NULL;
PCHAR pszEnabled = NULL;
PCHAR pszBlockThreshold = NULL;
PCHAR pszStoreActionThreshold = NULL;
LDAPMod *attributes[5];
LDAPMod uceEnabled;
LDAPMod blockThreshold;
LDAPMod storeActionThreshold;
LDAPMod objectClass;
char *uceEnabledValues[2];
char *blockThresholdValues[2];
char *storeActionThresholdValues[2];
char *objectClassValues[2];
// It takes three command line parameters - the values to set for the attributes
// msExchUceEnabled, msExchUceBlockThreshold, and msExchUceStoreActionThreshold.
if (4 == argc)
{
pszEnabled = argv[1];
pszBlockThreshold = argv[2];
pszStoreActionThreshold = argv[3];
}
else
{
Error("Invalid command line parameters");
goto CLEANUP;
}
// Start an LDAP session to nearest LDAP server.
pLdapSession = ldap_init( NULL, LDAP_PORT );
if (NULL == pLdapSession)
{
Error("ldap_init failed");
goto CLEANUP;
}
// Authenticate using the user's current credenticals.
lRtn = ldap_bind_s(pLdapSession, NULL, NULL, LDAP_AUTH_NEGOTIATE );
if (LDAP_SUCCESS != lRtn)
{
Error("ldap_bind_s failed");
goto CLEANUP;
}
// Search the root of the LDAP server.
lRtn = ldap_search_s (
pLdapSession, // Session handle
NULL, // Location to start search, NULL specifies top level
LDAP_SCOPE_BASE, // Search only the root entry (rootDSE)
NULL, // Search for all objects (only one for the RootDSE)
NULL, // No attributes specified, return all attributes
FALSE, // Return attributes types and values
&plmsgSearchResponse ); // Server allocates and fills with search results
if (LDAP_SUCCESS != lRtn)
{
Error("ldap_search_s for rootDSE failed");
goto CLEANUP;
}
// Get the naming context.
ppszConfigDN = ldap_get_values( pLdapSession, plmsgSearchResponse, "configurationNamingContext");
if (NULL == ppszConfigDN)
{
Error("Could not determine naming context");
goto CLEANUP;
}
// Delete the previous search result.
ldap_msgfree(plmsgSearchResponse);
plmsgSearchResponse = NULL;
// Search for the Uce Content Filter object.
sprintf(szSearch, "CN=Microsoft Exchange,CN=Services,%s", *ppszConfigDN);
sprintf(szFilter, "(&(objectClass=msExchUce)(CN=Uce Content Filter))");
lRtn = ldap_search_s(
pLdapSession, // Session handle
szSearch, // Start search at Exchange object
LDAP_SCOPE_SUBTREE, // Search the entire tree below the base
szFilter, // Search for Uce Content Filter object
NULL, // Return all attributes
FALSE, // Return attribute types and values
&plmsgSearchResponse ); // Result of search
if (LDAP_SUCCESS != lRtn)
{
Error("ldap_search_s failed to find Exchange object");
goto CLEANUP;
}
plmsgEntry = ldap_first_entry(
pLdapSession,
plmsgSearchResponse);
// Set up the attributes to be modified.
objectClassValues[0] = "msExchUce";
objectClassValues[1] = NULL;
objectClass.mod_type = "objectClass";
objectClass.mod_vals.modv_strvals = objectClassValues;
uceEnabledValues[0] = pszEnabled;
uceEnabledValues[1] = NULL;
uceEnabled.mod_type = "msExchUceEnabled";
uceEnabled.mod_vals.modv_strvals = uceEnabledValues;
blockThresholdValues[0] = pszBlockThreshold;
blockThresholdValues[1] = NULL;
blockThreshold.mod_type = "msExchUceBlockThreshold";
blockThreshold.mod_vals.modv_strvals = blockThresholdValues;
storeActionThresholdValues[0] = pszStoreActionThreshold;
storeActionThresholdValues[1] = NULL;
storeActionThreshold.mod_type = "msExchUceStoreActionThreshold";
storeActionThreshold.mod_vals.modv_strvals = storeActionThresholdValues;
if (NULL != plmsgEntry)
{
// Found the Uce Content Filter object.
// Get its DN.
pszUceDN = ldap_get_dn(
pLdapSession,
plmsgEntry);
if (NULL == pszUceDN)
{
Error("ldap_get_dn failed for Uce Content Filter object");
goto CLEANUP;
}
// Modify the attributes.
uceEnabled.mod_op = LDAP_MOD_REPLACE; // This will also add if it does not exist.
blockThreshold.mod_op = LDAP_MOD_REPLACE;
storeActionThreshold.mod_op = LDAP_MOD_REPLACE;
attributes[0] = &uceEnabled;
attributes[1] = &blockThreshold;
attributes[2] = &storeActionThreshold;
attributes[3] = NULL;
lRtn = ldap_modify_s(
pLdapSession,
pszUceDN,
attributes);
if (LDAP_SUCCESS != lRtn)
{
Error("ldap_modify_s failed");
goto CLEANUP;
}
// Success - the attributes have been modified.
}
else
{
// The Uce Content Filter object does not exist so create it.
// Delete the previous search result.
ldap_msgfree(plmsgSearchResponse);
plmsgSearchResponse = NULL;
// Search for the Message Delivery object.
sprintf(szFilter, "(&(objectClass=msExchMessageDeliveryConfig)(CN=Message Delivery))");
lRtn = ldap_search_s(
pLdapSession, // Session handle
szSearch, // Start search at Exchange object
LDAP_SCOPE_SUBTREE, // Search the entire tree below the base
szFilter, // Search for Message Delivery object
NULL, // Return all attributes
FALSE, // Return attribute types and values
&plmsgSearchResponse ); // Result of search
if (LDAP_SUCCESS != lRtn)
{
Error("ldap_search_s for Message Delivery failed");
goto CLEANUP;
}
plmsgEntry = ldap_first_entry(
pLdapSession,
plmsgSearchResponse);
if (NULL == plmsgEntry)
{
Error("ldap_first_entry failed");
goto CLEANUP;
}
// Found the Message Delivery object.
// Get its DN.
pszMessageDeliveryDN = ldap_get_dn(
pLdapSession,
plmsgEntry);
if (NULL == pszMessageDeliveryDN)
{
Error("ldap_get_dn failed for Message Delivery object");
goto CLEANUP;
}
// Create the Uce Content Filter object.
sprintf(szUceDN, "CN=Uce Content Filter,%s", pszMessageDeliveryDN);
// Add the attributes.
objectClass.mod_op = LDAP_MOD_ADD;
uceEnabled.mod_op = LDAP_MOD_ADD;
blockThreshold.mod_op = LDAP_MOD_ADD;
storeActionThreshold.mod_op = LDAP_MOD_ADD;
attributes[0] = &objectClass;
attributes[1] = &uceEnabled;
attributes[2] = &blockThreshold;
attributes[3] = &storeActionThreshold;
attributes[4] = NULL;
lRtn = ldap_add_s(
pLdapSession,
szUceDN,
attributes);
if (LDAP_SUCCESS != lRtn)
{
Error("ldap_add_s failed");
goto CLEANUP;
}
// Success - Uce Content Filter object has been created and the correct attributes added.
}
CLEANUP:
// Free strings allocated by the LDAP API.
if (ppszConfigDN)
ldap_value_free( ppszConfigDN );
if (pszMessageDeliveryDN)
ldap_memfree( pszMessageDeliveryDN );
if (pszUceDN)
ldap_memfree( pszUceDN );
// Free the search response.
if (plmsgSearchResponse)
ldap_msgfree( plmsgSearchResponse );
// Close the session.
if (pLdapSession)
ldap_unbind( pLdapSession );
return 0;
}
void Error(char *str)
{
// Perform an appropriate action, such as logging the error.
printf(str);
}