[libgda] Ldap provider: added the TIME_LIMIT and SIZE_LIMIT connection options



commit 621febd16af07e4769aa87b2f839390aee7f3145
Author: Vivien Malerba <malerba gnome-db org>
Date:   Fri Mar 23 20:58:50 2012 +0100

    Ldap provider: added the TIME_LIMIT and SIZE_LIMIT connection options

 providers/ldap/gda-ldap-provider.c       |   51 ++++++++++++++++++++++++++++-
 providers/ldap/gda-ldap.h                |    3 ++
 providers/ldap/gdaprov-data-model-ldap.c |    6 ++--
 providers/ldap/ldap_specs_dsn.xml.in     |    2 +
 4 files changed, 57 insertions(+), 5 deletions(-)
---
diff --git a/providers/ldap/gda-ldap-provider.c b/providers/ldap/gda-ldap-provider.c
index 4c9dc55..416f8f9 100644
--- a/providers/ldap/gda-ldap-provider.c
+++ b/providers/ldap/gda-ldap-provider.c
@@ -362,6 +362,8 @@ gda_ldap_provider_open_connection (GdaServerProvider *provider, GdaConnection *c
 	const gchar *user = NULL;
 	gchar *dnuser = NULL;
         const gchar *pwd = NULL;
+        const gchar *time_limit = NULL;
+        const gchar *size_limit = NULL;
         const gchar *tls_method = NULL;
         const gchar *tls_cacert = NULL;
 	int rtls_method = -1;
@@ -416,6 +418,8 @@ gda_ldap_provider_open_connection (GdaServerProvider *provider, GdaConnection *c
 			return FALSE;
 		}
 	}
+	time_limit = gda_quark_list_find (params, "TIME_LIMIT");
+	size_limit = gda_quark_list_find (params, "SIZE_LIMIT");
 
 	/* open LDAP connection */
 	LdapConnectionData *cdata;
@@ -456,7 +460,7 @@ gda_ldap_provider_open_connection (GdaServerProvider *provider, GdaConnection *c
 	else
 		url = g_strdup_printf ("ldap://%s:%d";, host, rport);
 
-	if (! gda_ldap_parse_dn (user, NULL) && *user) {
+	if (user && *user && ! gda_ldap_parse_dn (user, NULL)) {
 		/* analysing the @user parameter */
 		/* the user name is not a DN => we need to fetch the DN of the entry
 		 * using filters defined in the "mappings" array @user */
@@ -499,6 +503,8 @@ gda_ldap_provider_open_connection (GdaServerProvider *provider, GdaConnection *c
 	cdata = g_new0 (LdapConnectionData, 1);
 	cdata->handle = ld;
 	cdata->url = url;
+	cdata->time_limit = 0;
+	cdata->size_limit = 0;
 	cdata->base_dn = g_strdup (base_dn);
 	if (use_cache)
 		cdata->attributes_cache_file = compute_data_file_name (params, TRUE, "attrs");
@@ -518,7 +524,32 @@ gda_ldap_provider_open_connection (GdaServerProvider *provider, GdaConnection *c
 			return FALSE;
 		}
         }
-	res = ldap_set_option (cdata->handle, LDAP_OPT_RESTART, LDAP_OPT_ON);
+
+	/* time limit */
+	if (time_limit && *time_limit) {
+		int limit = atoi (time_limit);
+		res = ldap_set_option (cdata->handle, LDAP_OPT_TIMELIMIT, &limit);
+		if (res != LDAP_SUCCESS) {
+			gda_connection_add_event_string (cnc, ldap_err2string (res));
+			gda_ldap_free_cnc_data (cdata);
+			g_free (dnuser);
+			return FALSE;
+		}
+		cdata->time_limit = limit;
+	}
+
+	/* size limit */
+	if (size_limit && *size_limit) {
+		int limit = atoi (size_limit);
+		res = ldap_set_option (cdata->handle, LDAP_OPT_SIZELIMIT, &limit);
+		if (res != LDAP_SUCCESS) {
+			gda_connection_add_event_string (cnc, ldap_err2string (res));
+			gda_ldap_free_cnc_data (cdata);
+			g_free (dnuser);
+			return FALSE;
+		}
+		cdata->size_limit = limit;
+	}
 
 	/* authentication */
 	struct berval cred;
@@ -607,6 +638,22 @@ gda_ldap_silently_rebind (LdapConnectionData *cdata)
                 return FALSE;
 	}
 
+	/* time limit */
+	int limit = cdata->time_limit;
+	res = ldap_set_option (cdata->handle, LDAP_OPT_TIMELIMIT, &limit);
+	if (res != LDAP_SUCCESS) {
+		ldap_unbind_ext (ld, NULL, NULL);
+                return FALSE;
+	}
+
+	/* size limit */
+	limit = cdata->size_limit;
+	res = ldap_set_option (cdata->handle, LDAP_OPT_SIZELIMIT, &limit);
+	if (res != LDAP_SUCCESS) {
+		ldap_unbind_ext (ld, NULL, NULL);
+                return FALSE;
+	}
+
 	/* all ok */
 	if (cdata->handle) {
 		/* don't call ldap_unbind_ext() as it often crashed the application */
diff --git a/providers/ldap/gda-ldap.h b/providers/ldap/gda-ldap.h
index 98b231e..bbd4388 100644
--- a/providers/ldap/gda-ldap.h
+++ b/providers/ldap/gda-ldap.h
@@ -43,6 +43,9 @@ typedef struct {
 	gchar        *user;
 	gchar        *pass;
 
+	int           time_limit;
+	int           size_limit;
+
 	GHashTable   *attributes_hash; /* key = attribute name, value = a #LdapAttribute */
 	gchar        *attributes_cache_file;
 
diff --git a/providers/ldap/gdaprov-data-model-ldap.c b/providers/ldap/gdaprov-data-model-ldap.c
index 37d99d7..3cdabd3 100644
--- a/providers/ldap/gdaprov-data-model-ldap.c
+++ b/providers/ldap/gdaprov-data-model-ldap.c
@@ -302,7 +302,7 @@ gda_data_model_ldap_dispose (GObject * object)
 		g_free (model->priv->filter);
 
 		if (model->priv->exceptions) {
-			gint i; 
+			gint i;
 			for (i = 0; i < model->priv->exceptions->len; i++) {
 				GError *e;
 				e = g_array_index (model->priv->exceptions, GError*, i);
@@ -987,7 +987,6 @@ execute_ldap_search (GdaDataModelLdap *model)
 			 model->priv->current_exec->nb_entries);
 #endif
 		break;
-
 	case LDAP_ADMINLIMIT_EXCEEDED:
 	case LDAP_SIZELIMIT_EXCEEDED:
 	case LDAP_TIMELIMIT_EXCEEDED: {
@@ -995,7 +994,8 @@ execute_ldap_search (GdaDataModelLdap *model)
 		g_print ("LIMIT_EXCEEDED!\n");
 #endif
 		gboolean handled = FALSE;
-		if (model->priv->scope == GDA_LDAP_SEARCH_SUBTREE) {
+		if ((cdata->time_limit == 0) && (cdata->size_limit == 0) &&
+		    (model->priv->scope == GDA_LDAP_SEARCH_SUBTREE)) {
 			gboolean split_error;
 			if (ldap_part_split (model->priv->current_exec, model, &split_error)) {
 				/* create some children to re-run the search */
diff --git a/providers/ldap/ldap_specs_dsn.xml.in b/providers/ldap/ldap_specs_dsn.xml.in
index b0fd8f4..144ba34 100644
--- a/providers/ldap/ldap_specs_dsn.xml.in
+++ b/providers/ldap/ldap_specs_dsn.xml.in
@@ -10,6 +10,8 @@
     <parameter id="USE_CACHE" _name="Cache server data" _descr="Use a cache to store some static server data (the cached files are in the user's cache directory), default is TRUE" gdatype="gboolean">
       <gda_value>TRUE</gda_value>
     </parameter>
+    <parameter id="TIME_LIMIT" _name="Time limit" _descr="Time limit after which a search operation should be terminated by the server (leave undefined or 0 for unlimited)" gdatype="gint"/>
+    <parameter id="SIZE_LIMIT" _name="Size limit" _descr="Maximum number of entries that can be returned on a search operation" gdatype="gint"/>
   </parameters>
   <sources>
     <gda_array name="methods">



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]